]> git.ozlabs.org Git - ccan/blob - ccan/crypto/ripemd160/test/run-test-vectors.c
time: timemono_add.
[ccan] / ccan / crypto / ripemd160 / test / run-test-vectors.c
1 #include <ccan/crypto/ripemd160/ripemd160.h>
2 /* Include the C files directly. */
3 #include <ccan/crypto/ripemd160/ripemd160.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[5];
11 };
12
13 /* Test vectors from: http://homes.esat.kuleuven.be/~bosselae/ripemd160.html */
14 static struct test tests[] = {
15         { "", 1,
16           { CPU_TO_BE32(0x9c1185a5), CPU_TO_BE32(0xc5e9fc54),
17             CPU_TO_BE32(0x61280897), CPU_TO_BE32(0x7ee8f548),
18             CPU_TO_BE32(0xb2258d31) } },
19         { "abc", 1,
20           { CPU_TO_BE32(0x8eb208f7), CPU_TO_BE32(0xe05d987a),
21             CPU_TO_BE32(0x9b044a8e), CPU_TO_BE32(0x98c6b087),
22             CPU_TO_BE32(0xf15a0bfc) } },
23         { "message digest", 1,
24           { CPU_TO_BE32(0x5d0689ef), CPU_TO_BE32(0x49d2fae5),
25             CPU_TO_BE32(0x72b881b1), CPU_TO_BE32(0x23a85ffa),
26             CPU_TO_BE32(0x21595f36) } },
27         { "abcdefghijklmnopqrstuvwxyz", 1,
28           { CPU_TO_BE32(0xf71c2710), CPU_TO_BE32(0x9c692c1b),
29             CPU_TO_BE32(0x56bbdceb), CPU_TO_BE32(0x5b9d2865),
30             CPU_TO_BE32(0xb3708dbc) } },
31         { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1,
32           { CPU_TO_BE32(0x12a05338), CPU_TO_BE32(0x4a9c0c88),
33             CPU_TO_BE32(0xe405a06c), CPU_TO_BE32(0x27dcf49a),
34             CPU_TO_BE32(0xda62eb2b) } },
35         { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 1,
36           { CPU_TO_BE32(0xb0e20b6e), CPU_TO_BE32(0x31166402),
37             CPU_TO_BE32(0x86ed3a87), CPU_TO_BE32(0xa5713079),
38             CPU_TO_BE32(0xb21f5189) } },
39         { "1234567890", 8,
40           { CPU_TO_BE32(0x9b752e45), CPU_TO_BE32(0x573d4b39),
41             CPU_TO_BE32(0xf4dbd332), CPU_TO_BE32(0x3cab82bf),
42             CPU_TO_BE32(0x63326bfb) } },
43         { "a", 1000000,
44           { CPU_TO_BE32(0x52783243), CPU_TO_BE32(0xc1697bdb),
45             CPU_TO_BE32(0xe16d37f9), CPU_TO_BE32(0x7f68f083),
46             CPU_TO_BE32(0x25dc1528) } }
47 };
48
49 static bool do_test(const struct test *t, bool single)
50 {
51         struct ripemd160 h;
52
53         if (single) {
54                 if (t->repetitions != 1)
55                         return true;
56                 ripemd160(&h, t->test, strlen(t->test));
57         } else {
58                 struct ripemd160_ctx ctx = RIPEMD160_INIT;
59                 size_t i;
60
61                 for (i = 0; i < t->repetitions; i++)
62                         ripemd160_update(&ctx, t->test, strlen(t->test));
63                 ripemd160_done(&ctx, &h);
64         }
65
66         return memcmp(&h.u, t->result, sizeof(t->result)) == 0;
67 }
68
69 int main(void)
70 {
71         size_t i;
72
73         /* This is how many tests you plan to run */
74         plan_tests(sizeof(tests) / sizeof(struct test) * 2);
75
76         for (i = 0; i < sizeof(tests) / sizeof(struct test); i++)
77                 ok1(do_test(&tests[i], false));
78
79         for (i = 0; i < sizeof(tests) / sizeof(struct test); i++)
80                 ok1(do_test(&tests[i], true));
81
82         /* This exits depending on whether all tests passed */
83         return exit_status();
84 }