Skip to content

Commit

Permalink
Add SHA256 counter tests
Browse files Browse the repository at this point in the history
Those tests verify that the SHA256 bit counter wraps correctly at bit lengths
20 to 34. This wide range aims at being independent of the implementation of the
counter and it catches multiple natural 32-bit overflows (e.g., counting bits,
counting bytes, counting blocks, ...).

The test vectors have been generated using
cloudtools/sha256@e0645d1
and the following Python script

```
from sha256 import sha256
from copy import copy

def midstate_c_definition(hasher):
    ret  = '    {{UINT32_C(0x' + hasher.state[0].hex('_', 4).replace('_', '), UINT32_C(0x') + ')},\n'
    ret += '    {0x00}, UINT64_C(' + str(hasher.state[1]) + ')}'
    return ret

def output_c_literal(hasher):
    return '{0x' + hasher.digest().hex('_').replace('_', ', 0x') + '}'

MESSAGE = b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno'
ITER_BOUNDARIES = [(2**b)//len(MESSAGE) - 1 for b in range(20, 34)]

midstates = []
digests = []
hasher = sha256()
for i in range(ITER_BOUNDARIES[-1] + 1):
    if i in ITER_BOUNDARIES:
        midstates.append(midstate_c_definition(hasher))
        hasher_copy = copy(hasher)
        hasher_copy.update(MESSAGE)
        digests.append(output_c_literal(hasher_copy))
    hasher.update(MESSAGE)

for x in midstates:
    print(x + ',')

for x in digests:
    print(x + ',')
```
  • Loading branch information
real-or-random committed Apr 22, 2020
1 parent 03bd12f commit 768b850
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,65 @@ void run_sha256_tests(void) {
}
}

void run_sha256_counter_tests(void) {
static const char *input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno";
static const secp256k1_sha256 midstates[14] = {
{{UINT32_C(0xa2b5c8bb), UINT32_C(0x26c88bb3), UINT32_C(0x2abdc3d2), UINT32_C(0x9def99a3), UINT32_C(0xdfd21a6e), UINT32_C(0x41fe585b), UINT32_C(0x7ef2c440), UINT32_C(0x2b79adda)},
{0x00}, UINT64_C(1048512)},
{{UINT32_C(0xa0d29445), UINT32_C(0x9287de66), UINT32_C(0x76aabd71), UINT32_C(0x41acd765), UINT32_C(0x0c7528b4), UINT32_C(0x84e14906), UINT32_C(0x942faec6), UINT32_C(0xcc5a7b26)},
{0x00}, UINT64_C(2097088)},
{{UINT32_C(0x50449526), UINT32_C(0xb9f1d657), UINT32_C(0xa0fc13e9), UINT32_C(0x50860f10), UINT32_C(0xa550c431), UINT32_C(0x3fbc97c1), UINT32_C(0x7bbb2d89), UINT32_C(0xdb67bac1)},
{0x00}, UINT64_C(4194240)},
{{UINT32_C(0x54a6efdc), UINT32_C(0x46762e7b), UINT32_C(0x88bfe73f), UINT32_C(0xbbd149c7), UINT32_C(0x41620c43), UINT32_C(0x1168da7b), UINT32_C(0x2c5960f9), UINT32_C(0xeccffda6)},
{0x00}, UINT64_C(8388544)},
{{UINT32_C(0x2515a8f5), UINT32_C(0x5faa2977), UINT32_C(0x3a850486), UINT32_C(0xac858cad), UINT32_C(0x7b7276ee), UINT32_C(0x235c0385), UINT32_C(0xc53a157c), UINT32_C(0x7cb3e69c)},
{0x00}, UINT64_C(16777152)},
{{UINT32_C(0x34f39828), UINT32_C(0x409fedb7), UINT32_C(0x4bbdd0fb), UINT32_C(0x3b643634), UINT32_C(0x7806bf2e), UINT32_C(0xe0d1b713), UINT32_C(0xca3f2e1e), UINT32_C(0xe38722c2)},
{0x00}, UINT64_C(33554368)},
{{UINT32_C(0x389ef5c5), UINT32_C(0x38c54167), UINT32_C(0x8f5d56ab), UINT32_C(0x582a75cc), UINT32_C(0x8217caef), UINT32_C(0xf10947dd), UINT32_C(0x6a1998a8), UINT32_C(0x048f0b8c)},
{0x00}, UINT64_C(67108800)},
{{UINT32_C(0xd6c3f394), UINT32_C(0x0bee43b9), UINT32_C(0x6783f497), UINT32_C(0x29fa9e21), UINT32_C(0x6ce491c1), UINT32_C(0xa81fe45e), UINT32_C(0x2fc3859a), UINT32_C(0x269012d0)},
{0x00}, UINT64_C(134217664)},
{{UINT32_C(0x6dd3c526), UINT32_C(0x44d88aa0), UINT32_C(0x806a1bae), UINT32_C(0xfbcc0d32), UINT32_C(0x9d6144f3), UINT32_C(0x9d2bd757), UINT32_C(0x9851a957), UINT32_C(0xb50430ad)},
{0x00}, UINT64_C(268435392)},
{{UINT32_C(0x2add4021), UINT32_C(0xdfe8a9e6), UINT32_C(0xa56317c6), UINT32_C(0x7a15f5bb), UINT32_C(0x4a48aacd), UINT32_C(0x5d368414), UINT32_C(0x4f00e6f0), UINT32_C(0xd9355023)},
{0x00}, UINT64_C(536870848)},
{{UINT32_C(0xb66666b4), UINT32_C(0xdbeac32b), UINT32_C(0x0ea351ae), UINT32_C(0xcba9da46), UINT32_C(0x6278b874), UINT32_C(0x8c508e23), UINT32_C(0xe16ca776), UINT32_C(0x8465bac1)},
{0x00}, UINT64_C(1073741760)},
{{UINT32_C(0xb6744789), UINT32_C(0x9cce87aa), UINT32_C(0xc4c478b7), UINT32_C(0xf38404d8), UINT32_C(0x2e38ba62), UINT32_C(0xa3f7019b), UINT32_C(0x50458fe7), UINT32_C(0x3047dbec)},
{0x00}, UINT64_C(2147483584)},
{{UINT32_C(0x8b1297ba), UINT32_C(0xba261a80), UINT32_C(0x2ba1b0dd), UINT32_C(0xfbc67d6d), UINT32_C(0x61072c4e), UINT32_C(0x4b5a2a0f), UINT32_C(0x52872760), UINT32_C(0x2dfeb162)},
{0x00}, UINT64_C(4294967232)},
{{UINT32_C(0x24f33cf7), UINT32_C(0x41ad6583), UINT32_C(0x41c8ff5d), UINT32_C(0xca7ef35f), UINT32_C(0x50395756), UINT32_C(0x021b743e), UINT32_C(0xd7126cd7), UINT32_C(0xd037473a)},
{0x00}, UINT64_C(8589934528)}
};
static const unsigned char outputs[14][32] = {
{0x0e, 0x83, 0xe2, 0xc9, 0x4f, 0xb2, 0xb8, 0x2b, 0x89, 0x06, 0x92, 0x78, 0x04, 0x03, 0x48, 0x5c, 0x48, 0x44, 0x67, 0x61, 0x77, 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xf6, 0x15, 0x25, 0x80, 0x70, 0x76, 0x99, 0x7d, 0xf1, 0xc3, 0x2f, 0xa3, 0x31, 0xff, 0x92, 0x35, 0x2e, 0x8d, 0x04, 0x13, 0x33, 0xd8, 0x0d, 0xdb, 0x4a, 0xf6, 0x8c, 0x03, 0x34},
{0xec, 0x12, 0x24, 0x9f, 0x35, 0xa4, 0x29, 0x8b, 0x9e, 0x4a, 0x95, 0xf8, 0x61, 0xaf, 0x61, 0xc5, 0x66, 0x55, 0x3e, 0x3f, 0x2a, 0x98, 0xea, 0x71, 0x16, 0x6b, 0x1c, 0xd9, 0xe4, 0x09, 0xd2, 0x8e}
};
unsigned int i;
for (i = 0; i < sizeof(midstates)/sizeof(midstates[0]); i++) {
unsigned char out[32];
secp256k1_sha256 hasher;
secp256k1_sha256_initialize_midstate(&hasher, &midstates[i]);
secp256k1_sha256_write(&hasher, (const unsigned char*)input, strlen(input));
secp256k1_sha256_finalize(&hasher, out);
CHECK(memcmp(out, outputs[i], 32) == 0);
}
}

void run_hmac_sha256_tests(void) {
static const char *keys[6] = {
"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
Expand Down Expand Up @@ -5269,6 +5328,7 @@ int main(int argc, char **argv) {
run_rand_int();

run_sha256_tests();
run_sha256_counter_tests();
run_hmac_sha256_tests();
run_rfc6979_hmac_sha256_tests();

Expand Down

0 comments on commit 768b850

Please sign in to comment.