]>
git.ipfire.org Git - thirdparty/hostap.git/blob - tests/test-sha1.c
2 * Test program for SHA1 and MD5
3 * Copyright (c) 2003-2006, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "crypto/crypto.h"
15 static int cavp_shavs(const char *fname
)
19 char buf
[15000], *pos
, *pos2
;
21 int msg_len
= 0, tmp_len
;
25 printf("CAVP SHAVS test vectors from %s\n", fname
);
27 f
= fopen(fname
, "r");
29 printf("%s does not exist - cannot validate CAVP SHAVS test vectors\n",
34 while (fgets(buf
, sizeof(buf
), f
)) {
35 pos
= os_strchr(buf
, '=');
39 while (pos2
>= buf
&& *pos2
== ' ')
44 pos2
= os_strchr(pos
, '\r');
46 pos2
= os_strchr(pos
, '\n');
50 pos2
= pos
+ os_strlen(pos
);
52 if (os_strcmp(buf
, "Len") == 0) {
54 } else if (os_strcmp(buf
, "Msg") == 0) {
55 tmp_len
= os_strlen(pos
);
56 if (msg_len
== 0 && tmp_len
== 2)
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
);
65 if (hexstr2bin(pos
, msg
, msg_len
/ 8) < 0) {
66 printf("Invalid hex string '%s'\n", pos
);
70 } else if (os_strcmp(buf
, "MD") == 0) {
74 tmp_len
= os_strlen(pos
);
75 if (tmp_len
!= 2 * 20) {
76 printf("Unexpected MD length (MD='%s'\n",
82 if (hexstr2bin(pos
, md
, 20) < 0) {
83 printf("Invalid hex string '%s'\n", pos
);
90 if (sha1_vector(1, addr
, len
, hash
) < 0 ||
91 os_memcmp(hash
, md
, 20) != 0)
101 printf("Test case failed\n");
103 printf("%d test vectors OK\n", ok
);
109 int main(int argc
, char *argv
[])
113 if (cavp_shavs("CAVP/SHA1ShortMsg.rsp"))
115 if (cavp_shavs("CAVP/SHA1LongMsg.rsp"))