a64c35e468698632cf897e225834e02f2d0d5aef
[ccan] / ccan / crypto / sha256 / test / run-test-vectors.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
6 /* Test vectors. */
7 struct test {
8         const char *test;
9         size_t repetitions;
10         beint32_t result[8];
11 };
12
13 static struct test tests[] = {
14         { "", 1,
15           { CPU_TO_BE32(0xe3b0c442), CPU_TO_BE32(0x98fc1c14),
16             CPU_TO_BE32(0x9afbf4c8), CPU_TO_BE32(0x996fb924),
17             CPU_TO_BE32(0x27ae41e4), CPU_TO_BE32(0x649b934c),
18             CPU_TO_BE32(0xa495991b), CPU_TO_BE32(0x7852b855) } },
19         { "abc", 1,
20           { CPU_TO_BE32(0xba7816bf), CPU_TO_BE32(0x8f01cfea),
21             CPU_TO_BE32(0x414140de), CPU_TO_BE32(0x5dae2223),
22             CPU_TO_BE32(0xb00361a3), CPU_TO_BE32(0x96177a9c),
23             CPU_TO_BE32(0xb410ff61), CPU_TO_BE32(0xf20015ad) } },
24         { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1,
25           { CPU_TO_BE32(0x248d6a61), CPU_TO_BE32(0xd20638b8),
26             CPU_TO_BE32(0xe5c02693), CPU_TO_BE32(0x0c3e6039),
27             CPU_TO_BE32(0xa33ce459), CPU_TO_BE32(0x64ff2167),
28             CPU_TO_BE32(0xf6ecedd4), CPU_TO_BE32(0x19db06c1) } },
29         { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 1,
30           { CPU_TO_BE32(0xcf5b16a7), CPU_TO_BE32(0x78af8380),
31             CPU_TO_BE32(0x036ce59e), CPU_TO_BE32(0x7b049237),
32             CPU_TO_BE32(0x0b249b11), CPU_TO_BE32(0xe8f07a51),
33             CPU_TO_BE32(0xafac4503), CPU_TO_BE32(0x7afee9d1) } },
34         { "a", 1000000,
35           { CPU_TO_BE32(0xcdc76e5c), CPU_TO_BE32(0x9914fb92),
36             CPU_TO_BE32(0x81a1c7e2), CPU_TO_BE32(0x84d73e67),
37             CPU_TO_BE32(0xf1809a48), CPU_TO_BE32(0xa497200e),
38             CPU_TO_BE32(0x046d39cc), CPU_TO_BE32(0xc7112cd0) } }
39 #if 0 /* Good test, but takes ages! */
40         , { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno", 16777216,
41             { CPU_TO_BE32(0x50e72a0e), CPU_TO_BE32(0x26442fe2),
42               CPU_TO_BE32(0x552dc393), CPU_TO_BE32(0x8ac58658),
43               CPU_TO_BE32(0x228c0cbf), CPU_TO_BE32(0xb1d2ca87),
44               CPU_TO_BE32(0x2ae43526), CPU_TO_BE32(0x6fcd055e) } }
45 #endif
46 };
47
48 static bool do_test(const struct test *t, bool single)
49 {
50         struct sha256 h;
51
52         if (single) {
53                 if (t->repetitions != 1)
54                         return true;
55                 sha256(&h, t->test, strlen(t->test));
56         } else {
57                 struct sha256_ctx ctx = SHA256_INIT;
58                 size_t i;
59
60                 for (i = 0; i < t->repetitions; i++)
61                         sha256_update(&ctx, t->test, strlen(t->test));
62                 sha256_done(&ctx, &h);
63         }
64
65         return memcmp(&h.u, t->result, sizeof(t->result)) == 0;
66 }
67
68 int main(void)
69 {
70         size_t i;
71
72         /* This is how many tests you plan to run */
73         plan_tests(sizeof(tests) / sizeof(struct test) * 2);
74
75         for (i = 0; i < sizeof(tests) / sizeof(struct test); i++)
76                 ok1(do_test(&tests[i], false));
77
78         for (i = 0; i < sizeof(tests) / sizeof(struct test); i++)
79                 ok1(do_test(&tests[i], true));
80
81         /* This exits depending on whether all tests passed */
82         return exit_status();
83 }