]>
Commit | Line | Data |
---|---|---|
6fc6879b JM |
1 | /* |
2 | * Test program for SHA256 | |
3 | * Copyright (c) 2006, Jouni Malinen <j@w1.fi> | |
4 | * | |
0f3d578e JM |
5 | * This software may be distributed under the terms of the BSD license. |
6 | * See README for more details. | |
6fc6879b JM |
7 | */ |
8 | ||
9 | #include "includes.h" | |
10 | ||
11 | #include "common.h" | |
03da66bd | 12 | #include "crypto/crypto.h" |
6fc6879b | 13 | |
6fc6879b | 14 | |
e90d955f JM |
15 | static int cavp_shavs(const char *fname) |
16 | { | |
17 | FILE *f; | |
18 | int ret = 0; | |
19 | char buf[15000], *pos, *pos2; | |
20 | u8 msg[6400]; | |
21 | int msg_len = 0, tmp_len; | |
22 | u8 md[32], hash[32]; | |
23 | int ok = 0; | |
24 | ||
25 | printf("CAVP SHAVS test vectors from %s\n", fname); | |
26 | ||
27 | f = fopen(fname, "r"); | |
28 | if (f == NULL) { | |
29 | printf("%s does not exist - cannot validate CAVP SHAVS test vectors\n", | |
30 | fname); | |
31 | return 0; | |
32 | } | |
33 | ||
34 | while (fgets(buf, sizeof(buf), f)) { | |
35 | pos = os_strchr(buf, '='); | |
36 | if (pos == NULL) | |
37 | continue; | |
38 | pos2 = pos - 1; | |
39 | while (pos2 >= buf && *pos2 == ' ') | |
40 | *pos2-- = '\0'; | |
41 | *pos++ = '\0'; | |
42 | while (*pos == ' ') | |
43 | *pos++ = '\0'; | |
44 | pos2 = os_strchr(pos, '\r'); | |
45 | if (!pos2) | |
46 | pos2 = os_strchr(pos, '\n'); | |
47 | if (pos2) | |
48 | *pos2 = '\0'; | |
49 | else | |
50 | pos2 = pos + os_strlen(pos); | |
51 | ||
52 | if (os_strcmp(buf, "Len") == 0) { | |
53 | msg_len = atoi(pos); | |
54 | } else if (os_strcmp(buf, "Msg") == 0) { | |
55 | tmp_len = os_strlen(pos); | |
56 | if (msg_len == 0 && tmp_len == 2) | |
57 | tmp_len = 0; | |
58 | if (msg_len != tmp_len * 4) { | |
59 | printf("Unexpected Msg length (msg_len=%u tmp_len=%u, Msg='%s'\n", | |
60 | msg_len, tmp_len, pos); | |
61 | ret++; | |
62 | break; | |
63 | } | |
64 | ||
65 | if (hexstr2bin(pos, msg, msg_len / 8) < 0) { | |
66 | printf("Invalid hex string '%s'\n", pos); | |
67 | ret++; | |
68 | break; | |
69 | } | |
70 | } else if (os_strcmp(buf, "MD") == 0) { | |
71 | const u8 *addr[1]; | |
72 | size_t len[1]; | |
73 | ||
74 | tmp_len = os_strlen(pos); | |
75 | if (tmp_len != 2 * 32) { | |
76 | printf("Unexpected MD length (MD='%s'\n", | |
77 | pos); | |
78 | ret++; | |
79 | break; | |
80 | } | |
81 | ||
82 | if (hexstr2bin(pos, md, 32) < 0) { | |
83 | printf("Invalid hex string '%s'\n", pos); | |
84 | ret++; | |
85 | break; | |
86 | } | |
87 | ||
88 | addr[0] = msg; | |
89 | len[0] = msg_len / 8; | |
90 | if (sha256_vector(1, addr, len, hash) < 0 || | |
91 | os_memcmp(hash, md, 32) != 0) | |
92 | ret++; | |
93 | else | |
94 | ok++; | |
95 | } | |
96 | } | |
97 | ||
98 | fclose(f); | |
99 | ||
100 | if (ret) | |
101 | printf("Test case failed\n"); | |
102 | else | |
103 | printf("%d test vectors OK\n", ok); | |
104 | ||
105 | return ret; | |
106 | } | |
107 | ||
108 | ||
6fc6879b JM |
109 | int main(int argc, char *argv[]) |
110 | { | |
6fc6879b JM |
111 | int errors = 0; |
112 | ||
e90d955f JM |
113 | if (cavp_shavs("CAVP/SHA256ShortMsg.rsp")) |
114 | errors++; | |
115 | if (cavp_shavs("CAVP/SHA256LongMsg.rsp")) | |
116 | errors++; | |
117 | ||
6fc6879b JM |
118 | return errors; |
119 | } |