Merge remote-tracking branch 'origin/pr/47'
[ccan] / ccan / crypto / sha256 / test / run-33-bit-test.c
1 #include <ccan/crypto/sha256/sha256.h>
2 /* Include the C files directly. */
3 #include <ccan/crypto/sha256/sha256.c>
4 #include <ccan/tap/tap.h>
5 #include <stdio.h>
6
7 /* This is the test introduced for SHA-3, which checks for 33-bit overflow:
8    "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"
9    16777216 times.
10 */
11 static uint32_t expected[] = {
12         CPU_TO_BE32(0x50e72a0e), CPU_TO_BE32(0x26442fe2),
13         CPU_TO_BE32(0x552dc393), CPU_TO_BE32(0x8ac58658),
14         CPU_TO_BE32(0x228c0cbf), CPU_TO_BE32(0xb1d2ca87),
15         CPU_TO_BE32(0x2ae43526), CPU_TO_BE32(0x6fcd055e)
16 };
17
18 /* Produced by actually running the code on x86. */
19 static const struct sha256_ctx after_16M_by_64 = {
20 #ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
21         { { LE32_TO_CPU(0x515e3215), LE32_TO_CPU(0x592f4ae0),
22             LE32_TO_CPU(0xd407a8fc), LE32_TO_CPU(0x1fad409b),
23             LE32_TO_CPU(0x51fa46cc), LE32_TO_CPU(0xea528ae5),
24             LE32_TO_CPU(0x5fa58ebb), LE32_TO_CPU(0x8be97931) },
25           0x0, 0x2,
26           { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
27           0x0, 0x20 }
28 #else
29         { LE32_TO_CPU(0x515e3215), LE32_TO_CPU(0x592f4ae0),
30           LE32_TO_CPU(0xd407a8fc), LE32_TO_CPU(0x1fad409b),
31           LE32_TO_CPU(0x51fa46cc), LE32_TO_CPU(0xea528ae5),
32           LE32_TO_CPU(0x5fa58ebb), LE32_TO_CPU(0x8be97931) },
33         { .u32 = { 0x64636261, 0x68676665, 0x65646362, 0x69686766,
34                    0x66656463, 0x6a696867, 0x67666564, 0x6b6a6968 } },
35         1073741824
36 #endif
37 };
38
39 int main(void)
40 {
41         struct sha256 h;
42         struct sha256_ctx ctx;
43
44         /* This is how many tests you plan to run */
45         plan_tests(1);
46
47         ctx = after_16M_by_64;
48         sha256_done(&ctx, &h);
49
50         ok1(memcmp(&h.u, expected, sizeof(expected)) == 0);
51
52         /* This exits depending on whether all tests passed */
53         return exit_status();
54 }