]> git.ozlabs.org Git - ccan/blob - ccan/crypto/sha256/test/run-test-vectors.c
sha256: Simplify test
[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)
49 {
50         struct sha256 h;
51
52         if (t->repetitions == 1)
53                 sha256(&h, t->test, strlen(t->test));
54         else {
55                 struct sha256_ctx ctx = SHA256_INIT;
56                 size_t i;
57
58                 for (i = 0; i < t->repetitions; i++)
59                         sha256_update(&ctx, t->test, strlen(t->test));
60                 sha256_done(&ctx, &h);
61         }
62
63         return memcmp(&h, t->result, sizeof(h)) == 0;
64 }
65
66 int main(void)
67 {
68         size_t i;
69
70         /* This is how many tests you plan to run */
71         plan_tests(sizeof(tests) / sizeof(tests[0]));
72
73         for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
74                 ok1(do_test(&tests[i]));
75
76         /* This exits depending on whether all tests passed */
77         return exit_status();
78 }