Skip to content

Commit 0c5eccc

Browse files
docs: fixed some documentation errors in BLAKE2b (#1234)
* docs: Slight modifications Changed #include comments from doc to regular because it messed up the generated documentation. Changed blake2b() comment from regular to doc * docs: Removed @define's Doxygen doesn't seem to like them. Also fixed param on CEIL * chore: made it so math directory gets built * docs: added back third slash for includes * feat: added extended Euclidean algorithm * fix: key wasn't being considered in the algorithm * chore: added more tests * chore: Deleted file accidentally added from different branch * chore: moved tests to their own function * chore: apply suggestions from code review --------- Co-authored-by: David Leal <[email protected]>
1 parent a537cf3 commit 0c5eccc

File tree

1 file changed

+87
-16
lines changed

1 file changed

+87
-16
lines changed

hash/hash_blake2b.c

+87-16
Original file line numberDiff line numberDiff line change
@@ -28,53 +28,45 @@
2828
#endif
2929

3030
/**
31-
* @define bb
3231
* @brief the size of a data block in bytes
3332
*/
3433
#define bb 128
3534

3635
/**
37-
* @define KK_MAX
3836
* @brief max key length for BLAKE2b
3937
*/
4038
#define KK_MAX 64
4139

4240
/**
43-
* @define NN_MAX
4441
* @brief max length of BLAKE2b digest in bytes
4542
*/
4643
#define NN_MAX 64
4744

4845
/**
49-
* @define CEIL
5046
* @brief ceiling division macro without floats
5147
*
5248
* @param a dividend
53-
* @param divisor
49+
* @param b divisor
5450
*/
5551
#define CEIL(a, b) (((a) / (b)) + ((a) % (b) != 0))
5652

5753
/**
58-
* @define MIN
5954
* @brief returns minimum value
6055
*/
6156
#define MIN(a, b) ((a) < (b) ? (a) : (b))
6257

6358
/**
64-
* @define MAX
6559
* @brief returns maximum value
6660
*/
6761
#define MAX(a, b) ((a) > (b) ? (a) : (b))
6862

6963
/**
70-
* @define ROTR64
7164
* @brief macro to rotate 64-bit ints to the right
7265
* Ripped from RFC 7693
7366
*/
7467
#define ROTR64(n, offset) (((n) >> (offset)) ^ ((n) << (64 - (offset))))
7568

7669
/**
77-
* @define U128_ZERO
7870
* @brief zero-value initializer for u128 type
7971
*/
8072
#define U128_ZERO \
@@ -344,7 +336,8 @@ static int BLAKE2B(uint8_t *dest, block_t *d, size_t dd, u128 ll, uint8_t kk,
344336
return 0;
345337
}
346338

347-
/* @brief blake2b hash function
339+
/**
340+
* @brief blake2b hash function
348341
*
349342
* This is the front-end function that sets up the argument for BLAKE2B().
350343
*
@@ -398,7 +391,7 @@ uint8_t *blake2b(const uint8_t *message, size_t len, const uint8_t *key,
398391
/* If there is a secret key it occupies the first block */
399392
for (i = 0; i < kk; i++)
400393
{
401-
long_hold = message[i];
394+
long_hold = key[i];
402395
long_hold <<= 8 * (i % 8);
403396

404397
word_in_block = (i % bb) / 8;
@@ -449,15 +442,14 @@ static void assert_bytes(const uint8_t *expected, const uint8_t *actual,
449442
{
450443
assert(expected[i] == actual[i]);
451444
}
452-
453-
printf("All tests have successfully passed!\n");
454445
}
455446

456447
/**
457-
* @brief Main function
458-
* @returns 0 on exit
448+
* @brief testing function
449+
*
450+
* @returns void
459451
*/
460-
int main()
452+
static void test()
461453
{
462454
uint8_t *digest = NULL;
463455

@@ -476,5 +468,84 @@ int main()
476468

477469
free(digest);
478470

471+
uint8_t key[64] = {
472+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
473+
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
474+
0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
475+
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
476+
0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
477+
0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f};
478+
uint8_t key_answer[64] = {
479+
0x10, 0xeb, 0xb6, 0x77, 0x00, 0xb1, 0x86, 0x8e, 0xfb, 0x44, 0x17,
480+
0x98, 0x7a, 0xcf, 0x46, 0x90, 0xae, 0x9d, 0x97, 0x2f, 0xb7, 0xa5,
481+
0x90, 0xc2, 0xf0, 0x28, 0x71, 0x79, 0x9a, 0xaa, 0x47, 0x86, 0xb5,
482+
0xe9, 0x96, 0xe8, 0xf0, 0xf4, 0xeb, 0x98, 0x1f, 0xc2, 0x14, 0xb0,
483+
0x05, 0xf4, 0x2d, 0x2f, 0xf4, 0x23, 0x34, 0x99, 0x39, 0x16, 0x53,
484+
0xdf, 0x7a, 0xef, 0xcb, 0xc1, 0x3f, 0xc5, 0x15, 0x68};
485+
486+
digest = blake2b(NULL, 0, key, 64, 64);
487+
assert_bytes(key_answer, digest, 64);
488+
489+
free(digest);
490+
491+
uint8_t zero[1] = {0};
492+
uint8_t zero_key[64] = {
493+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
494+
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
495+
0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
496+
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
497+
0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
498+
0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f};
499+
uint8_t zero_answer[64] = {
500+
0x96, 0x1f, 0x6d, 0xd1, 0xe4, 0xdd, 0x30, 0xf6, 0x39, 0x01, 0x69,
501+
0x0c, 0x51, 0x2e, 0x78, 0xe4, 0xb4, 0x5e, 0x47, 0x42, 0xed, 0x19,
502+
0x7c, 0x3c, 0x5e, 0x45, 0xc5, 0x49, 0xfd, 0x25, 0xf2, 0xe4, 0x18,
503+
0x7b, 0x0b, 0xc9, 0xfe, 0x30, 0x49, 0x2b, 0x16, 0xb0, 0xd0, 0xbc,
504+
0x4e, 0xf9, 0xb0, 0xf3, 0x4c, 0x70, 0x03, 0xfa, 0xc0, 0x9a, 0x5e,
505+
0xf1, 0x53, 0x2e, 0x69, 0x43, 0x02, 0x34, 0xce, 0xbd};
506+
507+
digest = blake2b(zero, 1, zero_key, 64, 64);
508+
assert_bytes(zero_answer, digest, 64);
509+
510+
free(digest);
511+
512+
uint8_t filled[64] = {
513+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
514+
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
515+
0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
516+
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
517+
0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
518+
0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f};
519+
uint8_t filled_key[64] = {
520+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
521+
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
522+
0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
523+
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
524+
0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
525+
0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f};
526+
uint8_t filled_answer[64] = {
527+
0x65, 0x67, 0x6d, 0x80, 0x06, 0x17, 0x97, 0x2f, 0xbd, 0x87, 0xe4,
528+
0xb9, 0x51, 0x4e, 0x1c, 0x67, 0x40, 0x2b, 0x7a, 0x33, 0x10, 0x96,
529+
0xd3, 0xbf, 0xac, 0x22, 0xf1, 0xab, 0xb9, 0x53, 0x74, 0xab, 0xc9,
530+
0x42, 0xf1, 0x6e, 0x9a, 0xb0, 0xea, 0xd3, 0x3b, 0x87, 0xc9, 0x19,
531+
0x68, 0xa6, 0xe5, 0x09, 0xe1, 0x19, 0xff, 0x07, 0x78, 0x7b, 0x3e,
532+
0xf4, 0x83, 0xe1, 0xdc, 0xdc, 0xcf, 0x6e, 0x30, 0x22};
533+
534+
digest = blake2b(filled, 64, filled_key, 64, 64);
535+
assert_bytes(filled_answer, digest, 64);
536+
537+
free(digest);
538+
539+
printf("All tests have successfully passed!\n");
540+
}
541+
542+
/**
543+
* @brief main function
544+
*
545+
* @returns 0 on successful program exit
546+
*/
547+
int main()
548+
{
549+
test();
479550
return 0;
480551
}

0 commit comments

Comments
 (0)