@@ -451,36 +451,57 @@ void run_ctz_tests(void) {
451
451
452
452
/***** HASH TESTS *****/
453
453
454
- void run_sha256_tests (void ) {
455
- static const char * inputs [8 ] = {
454
+ void run_sha256_known_output_tests (void ) {
455
+ static const char * inputs [] = {
456
456
"" , "abc" , "message digest" , "secure hash algorithm" , "SHA256 is considered to be safe" ,
457
457
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" ,
458
458
"For this sample, this 63-byte string will be used as input data" ,
459
- "This is exactly 64 bytes long, not counting the terminating byte"
459
+ "This is exactly 64 bytes long, not counting the terminating byte" ,
460
+ "aaaaa" ,
460
461
};
461
- static const unsigned char outputs [8 ][32 ] = {
462
+ static const unsigned int repeat [] = {
463
+ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1000000 /5
464
+ };
465
+ static const unsigned char outputs [][32 ] = {
462
466
{0xe3 , 0xb0 , 0xc4 , 0x42 , 0x98 , 0xfc , 0x1c , 0x14 , 0x9a , 0xfb , 0xf4 , 0xc8 , 0x99 , 0x6f , 0xb9 , 0x24 , 0x27 , 0xae , 0x41 , 0xe4 , 0x64 , 0x9b , 0x93 , 0x4c , 0xa4 , 0x95 , 0x99 , 0x1b , 0x78 , 0x52 , 0xb8 , 0x55 },
463
467
{0xba , 0x78 , 0x16 , 0xbf , 0x8f , 0x01 , 0xcf , 0xea , 0x41 , 0x41 , 0x40 , 0xde , 0x5d , 0xae , 0x22 , 0x23 , 0xb0 , 0x03 , 0x61 , 0xa3 , 0x96 , 0x17 , 0x7a , 0x9c , 0xb4 , 0x10 , 0xff , 0x61 , 0xf2 , 0x00 , 0x15 , 0xad },
464
468
{0xf7 , 0x84 , 0x6f , 0x55 , 0xcf , 0x23 , 0xe1 , 0x4e , 0xeb , 0xea , 0xb5 , 0xb4 , 0xe1 , 0x55 , 0x0c , 0xad , 0x5b , 0x50 , 0x9e , 0x33 , 0x48 , 0xfb , 0xc4 , 0xef , 0xa3 , 0xa1 , 0x41 , 0x3d , 0x39 , 0x3c , 0xb6 , 0x50 },
465
469
{0xf3 , 0x0c , 0xeb , 0x2b , 0xb2 , 0x82 , 0x9e , 0x79 , 0xe4 , 0xca , 0x97 , 0x53 , 0xd3 , 0x5a , 0x8e , 0xcc , 0x00 , 0x26 , 0x2d , 0x16 , 0x4c , 0xc0 , 0x77 , 0x08 , 0x02 , 0x95 , 0x38 , 0x1c , 0xbd , 0x64 , 0x3f , 0x0d },
466
470
{0x68 , 0x19 , 0xd9 , 0x15 , 0xc7 , 0x3f , 0x4d , 0x1e , 0x77 , 0xe4 , 0xe1 , 0xb5 , 0x2d , 0x1f , 0xa0 , 0xf9 , 0xcf , 0x9b , 0xea , 0xea , 0xd3 , 0x93 , 0x9f , 0x15 , 0x87 , 0x4b , 0xd9 , 0x88 , 0xe2 , 0xa2 , 0x36 , 0x30 },
467
471
{0x24 , 0x8d , 0x6a , 0x61 , 0xd2 , 0x06 , 0x38 , 0xb8 , 0xe5 , 0xc0 , 0x26 , 0x93 , 0x0c , 0x3e , 0x60 , 0x39 , 0xa3 , 0x3c , 0xe4 , 0x59 , 0x64 , 0xff , 0x21 , 0x67 , 0xf6 , 0xec , 0xed , 0xd4 , 0x19 , 0xdb , 0x06 , 0xc1 },
468
472
{0xf0 , 0x8a , 0x78 , 0xcb , 0xba , 0xee , 0x08 , 0x2b , 0x05 , 0x2a , 0xe0 , 0x70 , 0x8f , 0x32 , 0xfa , 0x1e , 0x50 , 0xc5 , 0xc4 , 0x21 , 0xaa , 0x77 , 0x2b , 0xa5 , 0xdb , 0xb4 , 0x06 , 0xa2 , 0xea , 0x6b , 0xe3 , 0x42 },
469
- {0xab , 0x64 , 0xef , 0xf7 , 0xe8 , 0x8e , 0x2e , 0x46 , 0x16 , 0x5e , 0x29 , 0xf2 , 0xbc , 0xe4 , 0x18 , 0x26 , 0xbd , 0x4c , 0x7b , 0x35 , 0x52 , 0xf6 , 0xb3 , 0x82 , 0xa9 , 0xe7 , 0xd3 , 0xaf , 0x47 , 0xc2 , 0x45 , 0xf8 }
473
+ {0xab , 0x64 , 0xef , 0xf7 , 0xe8 , 0x8e , 0x2e , 0x46 , 0x16 , 0x5e , 0x29 , 0xf2 , 0xbc , 0xe4 , 0x18 , 0x26 , 0xbd , 0x4c , 0x7b , 0x35 , 0x52 , 0xf6 , 0xb3 , 0x82 , 0xa9 , 0xe7 , 0xd3 , 0xaf , 0x47 , 0xc2 , 0x45 , 0xf8 },
474
+ {0xcd , 0xc7 , 0x6e , 0x5c , 0x99 , 0x14 , 0xfb , 0x92 , 0x81 , 0xa1 , 0xc7 , 0xe2 , 0x84 , 0xd7 , 0x3e , 0x67 , 0xf1 , 0x80 , 0x9a , 0x48 , 0xa4 , 0x97 , 0x20 , 0x0e , 0x04 , 0x6d , 0x39 , 0xcc , 0xc7 , 0x11 , 0x2c , 0xd0 },
470
475
};
471
- int i ;
472
- for (i = 0 ; i < 8 ; i ++ ) {
476
+ unsigned int i , ninputs ;
477
+
478
+ /* Skip last input vector for low iteration counts */
479
+ ninputs = sizeof (inputs )/sizeof (inputs [0 ]) - 1 ;
480
+ CONDITIONAL_TEST (16 , "run_sha256_known_output_tests 1000000" ) ninputs ++ ;
481
+
482
+ for (i = 0 ; i < ninputs ; i ++ ) {
473
483
unsigned char out [32 ];
474
484
secp256k1_sha256 hasher ;
485
+ unsigned int j ;
486
+ /* 1. Run: simply write the input bytestrings */
487
+ j = repeat [i ];
475
488
secp256k1_sha256_initialize (& hasher );
476
- secp256k1_sha256_write (& hasher , (const unsigned char * )(inputs [i ]), strlen (inputs [i ]));
489
+ while (j > 0 ) {
490
+ secp256k1_sha256_write (& hasher , (const unsigned char * )(inputs [i ]), strlen (inputs [i ]));
491
+ j -- ;
492
+ }
477
493
secp256k1_sha256_finalize (& hasher , out );
478
494
CHECK (secp256k1_memcmp_var (out , outputs [i ], 32 ) == 0 );
495
+ /* 2. Run: split the input bytestrings randomly before writing */
479
496
if (strlen (inputs [i ]) > 0 ) {
480
497
int split = secp256k1_testrand_int (strlen (inputs [i ]));
481
498
secp256k1_sha256_initialize (& hasher );
482
- secp256k1_sha256_write (& hasher , (const unsigned char * )(inputs [i ]), split );
483
- secp256k1_sha256_write (& hasher , (const unsigned char * )(inputs [i ] + split ), strlen (inputs [i ]) - split );
499
+ j = repeat [i ];
500
+ while (j > 0 ) {
501
+ secp256k1_sha256_write (& hasher , (const unsigned char * )(inputs [i ]), split );
502
+ secp256k1_sha256_write (& hasher , (const unsigned char * )(inputs [i ] + split ), strlen (inputs [i ]) - split );
503
+ j -- ;
504
+ }
484
505
secp256k1_sha256_finalize (& hasher , out );
485
506
CHECK (secp256k1_memcmp_var (out , outputs [i ], 32 ) == 0 );
486
507
}
@@ -6964,7 +6985,7 @@ int main(int argc, char **argv) {
6964
6985
run_modinv_tests ();
6965
6986
run_inverse_tests ();
6966
6987
6967
- run_sha256_tests ();
6988
+ run_sha256_known_output_tests ();
6968
6989
run_hmac_sha256_tests ();
6969
6990
run_rfc6979_hmac_sha256_tests ();
6970
6991
run_tagged_sha256_tests ();
0 commit comments