28
28
#endif
29
29
30
30
/**
31
- * @define bb
32
31
* @brief the size of a data block in bytes
33
32
*/
34
33
#define bb 128
35
34
36
35
/**
37
- * @define KK_MAX
38
36
* @brief max key length for BLAKE2b
39
37
*/
40
38
#define KK_MAX 64
41
39
42
40
/**
43
- * @define NN_MAX
44
41
* @brief max length of BLAKE2b digest in bytes
45
42
*/
46
43
#define NN_MAX 64
47
44
48
45
/**
49
- * @define CEIL
50
46
* @brief ceiling division macro without floats
51
47
*
52
48
* @param a dividend
53
- * @param divisor
49
+ * @param b divisor
54
50
*/
55
51
#define CEIL (a , b ) (((a) / (b)) + ((a) % (b) != 0))
56
52
57
53
/**
58
- * @define MIN
59
54
* @brief returns minimum value
60
55
*/
61
56
#define MIN (a , b ) ((a) < (b) ? (a) : (b))
62
57
63
58
/**
64
- * @define MAX
65
59
* @brief returns maximum value
66
60
*/
67
61
#define MAX (a , b ) ((a) > (b) ? (a) : (b))
68
62
69
63
/**
70
- * @define ROTR64
71
64
* @brief macro to rotate 64-bit ints to the right
72
65
* Ripped from RFC 7693
73
66
*/
74
67
#define ROTR64 (n , offset ) (((n) >> (offset)) ^ ((n) << (64 - (offset))))
75
68
76
69
/**
77
- * @define U128_ZERO
78
70
* @brief zero-value initializer for u128 type
79
71
*/
80
72
#define U128_ZERO \
@@ -344,7 +336,8 @@ static int BLAKE2B(uint8_t *dest, block_t *d, size_t dd, u128 ll, uint8_t kk,
344
336
return 0 ;
345
337
}
346
338
347
- /* @brief blake2b hash function
339
+ /**
340
+ * @brief blake2b hash function
348
341
*
349
342
* This is the front-end function that sets up the argument for BLAKE2B().
350
343
*
@@ -398,7 +391,7 @@ uint8_t *blake2b(const uint8_t *message, size_t len, const uint8_t *key,
398
391
/* If there is a secret key it occupies the first block */
399
392
for (i = 0 ; i < kk ; i ++ )
400
393
{
401
- long_hold = message [i ];
394
+ long_hold = key [i ];
402
395
long_hold <<= 8 * (i % 8 );
403
396
404
397
word_in_block = (i % bb ) / 8 ;
@@ -449,15 +442,14 @@ static void assert_bytes(const uint8_t *expected, const uint8_t *actual,
449
442
{
450
443
assert (expected [i ] == actual [i ]);
451
444
}
452
-
453
- printf ("All tests have successfully passed!\n" );
454
445
}
455
446
456
447
/**
457
- * @brief Main function
458
- * @returns 0 on exit
448
+ * @brief testing function
449
+ *
450
+ * @returns void
459
451
*/
460
- int main ()
452
+ static void test ()
461
453
{
462
454
uint8_t * digest = NULL ;
463
455
@@ -476,5 +468,84 @@ int main()
476
468
477
469
free (digest );
478
470
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 ();
479
550
return 0 ;
480
551
}
0 commit comments