]> git.ipfire.org Git - thirdparty/hostap.git/blob - tests/test-sha256.c
tests: sigma_dut controlled STA and beacon protection
[thirdparty/hostap.git] / tests / test-sha256.c
1 /*
2 * Test program for SHA256
3 * Copyright (c) 2006, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "includes.h"
10
11 #include "common.h"
12 #include "crypto/crypto.h"
13
14
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
109 int main(int argc, char *argv[])
110 {
111 int errors = 0;
112
113 if (cavp_shavs("CAVP/SHA256ShortMsg.rsp"))
114 errors++;
115 if (cavp_shavs("CAVP/SHA256LongMsg.rsp"))
116 errors++;
117
118 return errors;
119 }