Skip to content

Commit efb0069

Browse files
Add test vectors for extremely long SHA256 messages
One of the new vectors is a message of size 2^33 bits and verifies that we handle the bit counter in the SHA256 correctly. The vectors have been taken from https://www.di-mgt.com.au/sha_testvectors.html. They can be independently verified using the following Python code. ``` h = hashlib.sha256() for i in range(1_000_000): h.update(b'a') print(h.hexdigest()) h = hashlib.sha256() for i in range(16_777_216): h.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno') print(h.hexdigest()) ```
1 parent 33d0871 commit efb0069

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

src/tests.c

+31-12
Original file line numberDiff line numberDiff line change
@@ -408,35 +408,54 @@ void run_scratch_tests(void) {
408408
/***** HASH TESTS *****/
409409

410410
void run_sha256_tests(void) {
411-
static const char *inputs[8] = {
411+
static const char *inputs[10] = {
412412
"", "abc", "message digest", "secure hash algorithm", "SHA256 is considered to be safe",
413413
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
414414
"For this sample, this 63-byte string will be used as input data",
415-
"This is exactly 64 bytes long, not counting the terminating byte"
415+
"This is exactly 64 bytes long, not counting the terminating byte",
416+
"aaaaa",
417+
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"
416418
};
417-
static const unsigned char outputs[8][32] = {
419+
static const unsigned int repeat[10] = {
420+
1, 1, 1, 1, 1, 1, 1, 1, 1000000/5, 16777216
421+
};
422+
static const unsigned char outputs[10][32] = {
418423
{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},
419424
{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},
420425
{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},
421426
{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},
422427
{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},
423428
{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},
424429
{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},
425-
{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}
430+
{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},
431+
{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},
432+
{0x50, 0xe7, 0x2a, 0x0e, 0x26, 0x44, 0x2f, 0xe2, 0x55, 0x2d, 0xc3, 0x93, 0x8a, 0xc5, 0x86, 0x58, 0x22, 0x8c, 0x0c, 0xbf ,0xb1, 0xd2, 0xca, 0x87, 0x2a, 0xe4, 0x35, 0x26, 0x6f, 0xcd, 0x05, 0x5e}
426433
};
427-
int i;
428-
for (i = 0; i < 8; i++) {
434+
unsigned int i;
435+
for (i = 0; i < sizeof(inputs)/sizeof(inputs[0]); i++) {
429436
unsigned char out[32];
430437
secp256k1_sha256 hasher;
431-
secp256k1_sha256_initialize(&hasher);
432-
secp256k1_sha256_write(&hasher, (const unsigned char*)(inputs[i]), strlen(inputs[i]));
433-
secp256k1_sha256_finalize(&hasher, out);
434-
CHECK(memcmp(out, outputs[i], 32) == 0);
438+
unsigned int j;
439+
/* Skip the simple test (and rely only on the "split" test below) for long inputs to save time. */
440+
if (repeat[i] <= 1000000) {
441+
j = repeat[i];
442+
secp256k1_sha256_initialize(&hasher);
443+
while (j > 0) {
444+
secp256k1_sha256_write(&hasher, (const unsigned char*)(inputs[i]), strlen(inputs[i]));
445+
j--;
446+
}
447+
secp256k1_sha256_finalize(&hasher, out);
448+
CHECK(memcmp(out, outputs[i], 32) == 0);
449+
}
435450
if (strlen(inputs[i]) > 0) {
436451
int split = secp256k1_rand_int(strlen(inputs[i]));
437452
secp256k1_sha256_initialize(&hasher);
438-
secp256k1_sha256_write(&hasher, (const unsigned char*)(inputs[i]), split);
439-
secp256k1_sha256_write(&hasher, (const unsigned char*)(inputs[i] + split), strlen(inputs[i]) - split);
453+
j = repeat[i];
454+
while (j > 0) {
455+
secp256k1_sha256_write(&hasher, (const unsigned char*)(inputs[i]), split);
456+
secp256k1_sha256_write(&hasher, (const unsigned char*)(inputs[i] + split), strlen(inputs[i]) - split);
457+
j--;
458+
}
440459
secp256k1_sha256_finalize(&hasher, out);
441460
CHECK(memcmp(out, outputs[i], 32) == 0);
442461
}

0 commit comments

Comments
 (0)