rune: avoid shadowing var in test/run.c
[ccan] / ccan / crypto / sha512 / test / run-test-vectors.c
1 #include <ccan/crypto/sha512/sha512.h>
2 #include <ccan/str/hex/hex.h>
3
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7
8 /* Include the C files directly. */
9 #include <ccan/crypto/sha512/sha512.c>
10 #include <ccan/tap/tap.h>
11
12 /* Test vectors. */
13 struct test {
14         const char *vector;
15         size_t repetitions;
16         const char *expected;
17 };
18
19 static const char ZEROES[] =
20         "0000000000000000000000000000000000000000000000000000000000000000"
21         "0000000000000000000000000000000000000000000000000000000000000000";
22
23 static struct test tests[] = {
24         /* http://csrc.nist.gov/groups/STM/cavp/secure-hashing.html ShortMsg */
25         { "21", 1,
26           "3831a6a6155e509dee59a7f451eb35324d8f8f2df6e3708894740f98fdee2388"
27           "9f4de5adb0c5010dfb555cda77c8ab5dc902094c52de3278f35a75ebc25f093a" },
28         { "9083", 1,
29           "55586ebba48768aeb323655ab6f4298fc9f670964fc2e5f2731e34dfa4b0c09e"
30           "6e1e12e3d7286b3145c61c2047fb1a2a1297f36da64160b31fa4c8c2cddd2fb4" },
31         { "0a55db", 1,
32           "7952585e5330cb247d72bae696fc8a6b0f7d0804577e347d99bc1b11e52f3849"
33           "85a428449382306a89261ae143c2f3fb613804ab20b42dc097e5bf4a96ef919b" },
34         { "23be86d5", 1,
35           "76d42c8eadea35a69990c63a762f330614a4699977f058adb988f406fb0be8f2"
36           "ea3dce3a2bbd1d827b70b9b299ae6f9e5058ee97b50bd4922d6d37ddc761f8eb" },
37         { "eb0ca946c1", 1,
38           "d39ecedfe6e705a821aee4f58bfc489c3d9433eb4ac1b03a97e321a2586b40dd"
39           "0522f40fa5aef36afff591a78c916bfc6d1ca515c4983dd8695b1ec7951d723e" },
40         { "38667f39277b", 1,
41           "85708b8ff05d974d6af0801c152b95f5fa5c06af9a35230c5bea2752f031f9bd"
42           "84bd844717b3add308a70dc777f90813c20b47b16385664eefc88449f04f2131" },
43         { "b39f71aaa8a108", 1,
44           "258b8efa05b4a06b1e63c7a3f925c5ef11fa03e3d47d631bf4d474983783d8c0"
45           "b09449009e842fc9fa15de586c67cf8955a17d790b20f41dadf67ee8cdcdfce6" },
46         { "dc28484ebfd293d62ac759d5754bdf502423e4d419fa79020805134b2ce3dff7"
47           "38c7556c91d810adbad8dd210f041296b73c2185d4646c97fc0a5b69ed49ac8c"
48           "7ced0bd1cfd7e3c3cca47374d189247da6811a40b0ab097067ed4ad40ade2e47"
49           "91e39204e398b3204971445822a1be0dd93af8", 1,
50           "615115d2e8b62e345adaa4bdb95395a3b4fe27d71c4a111b86c1841463c5f03d"
51           "6b20d164a39948ab08ae060720d05c10f6022e5c8caf2fa3bca2e04d9c539ded" },
52         { "fd2203e467574e834ab07c9097ae164532f24be1eb5d88f1af7748ceff0d2c67"
53           "a21f4e4097f9d3bb4e9fbf97186e0db6db0100230a52b453d421f8ab9c9a6043"
54           "aa3295ea20d2f06a2f37470d8a99075f1b8a8336f6228cf08b5942fc1fb4299c"
55           "7d2480e8e82bce175540bdfad7752bc95b577f229515394f3ae5cec870a4b2f8",
56           1,
57           "a21b1077d52b27ac545af63b32746c6e3c51cb0cb9f281eb9f3580a6d4996d5c"
58           "9917d2a6e484627a9d5a06fa1b25327a9d710e027387fc3e07d7c4d14c6086cc" },
59         /* http://www.di-mgt.com.au/sha_testvectors.html */
60         { ZEROES, 1,
61           "7be9fda48f4179e611c698a73cff09faf72869431efee6eaad14de0cb44bbf66"
62           "503f752b7a8eb17083355f3ce6eb7d2806f236b25af96a24e22b887405c20081" }
63 #if 0 /* This test is rather slow */
64         ,
65         { ZEROES, 100000,
66           "23b8521df55569c4e55c7be36d4ad106e338b0799d5e105058aaa1a95737c25d"
67           "b77af240849ae7283ea0b7cbf196f5e4bd78aca19af97eb6e364ada4d12c1178" }
68 #endif
69 };
70
71 static void *xmalloc(size_t size)
72 {
73         char * ret;
74         ret = malloc(size);
75         if (ret == NULL) {
76                 perror("malloc");
77                 abort();
78         }
79         return ret;
80 }
81
82 static bool do_test(const struct test *t)
83 {
84         struct sha512 h;
85         char got[128 + 1];
86         bool passed;
87         size_t i, vector_len = strlen(t->vector) / 2;
88         void *vector = xmalloc(vector_len);
89
90         hex_decode(t->vector, vector_len * 2, vector, vector_len);
91
92         for (i = 0; i < t->repetitions; i++) {
93                 sha512(&h, vector, vector_len);
94                 if (t->repetitions > 1)
95                         memcpy(vector, &h, sizeof(h));
96         }
97
98         hex_encode(&h, sizeof(h), got, sizeof(got));
99
100         passed = strcmp(t->expected, got) == 0;
101         free(vector);
102         return passed;
103 }
104
105 int main(void)
106 {
107         const size_t num_tests = sizeof(tests) / sizeof(tests[0]);
108         size_t i;
109
110         /* This is how many tests you plan to run */
111         plan_tests(num_tests);
112
113         for (i = 0; i < num_tests; i++)
114                 ok1(do_test(&tests[i]));
115
116         /* This exits depending on whether all tests passed */
117         return exit_status();
118 }