]>
git.ipfire.org Git - thirdparty/hostap.git/blob - tests/test-rsa-sig-ver.c
2 * Testing tool for RSA PKCS #1 v1.5 signature verification
3 * Copyright (c) 2014, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "crypto/crypto.h"
15 #include "tls/pkcs1.h"
18 static int cavp_rsa_sig_ver(const char *fname
)
22 char buf
[15000], *pos
, *pos2
;
23 u8 msg
[200], n
[512], s
[512], em
[512], e
[512];
24 size_t msg_len
= 0, n_len
= 0, s_len
= 0, em_len
, e_len
= 0;
29 printf("CAVP RSA SigVer test vectors from %s\n", fname
);
31 f
= fopen(fname
, "r");
33 printf("%s does not exist - cannot validate CAVP RSA SigVer test vectors\n",
38 while (fgets(buf
, sizeof(buf
), f
)) {
39 pos
= os_strchr(buf
, '=');
43 while (pos2
>= buf
&& *pos2
== ' ')
48 pos2
= os_strchr(pos
, '\r');
50 pos2
= os_strchr(pos
, '\n');
54 pos2
= pos
+ os_strlen(pos
);
56 if (os_strcmp(buf
, "SHAAlg") == 0) {
57 os_strlcpy(sha_alg
, pos
, sizeof(sha_alg
));
58 } else if (os_strcmp(buf
, "Msg") == 0) {
59 tmp_len
= os_strlen(pos
);
60 if (tmp_len
> sizeof(msg
) * 2) {
61 printf("Too long Msg\n");
65 msg_len
= tmp_len
/ 2;
66 if (hexstr2bin(pos
, msg
, msg_len
) < 0) {
67 printf("Invalid hex string '%s'\n", pos
);
71 } else if (os_strcmp(buf
, "n") == 0) {
72 tmp_len
= os_strlen(pos
);
73 if (tmp_len
> sizeof(n
) * 2) {
74 printf("Too long n\n");
79 if (hexstr2bin(pos
, n
, n_len
) < 0) {
80 printf("Invalid hex string '%s'\n", pos
);
84 } else if (os_strcmp(buf
, "e") == 0) {
85 tmp_len
= os_strlen(pos
);
86 if (tmp_len
> sizeof(e
) * 2) {
87 printf("Too long e\n");
92 if (hexstr2bin(pos
, e
, e_len
) < 0) {
93 printf("Invalid hex string '%s'\n", pos
);
97 } else if (os_strcmp(buf
, "S") == 0) {
98 tmp_len
= os_strlen(pos
);
99 if (tmp_len
> sizeof(s
) * 2) {
100 printf("Too long S\n");
105 if (hexstr2bin(pos
, s
, s_len
) < 0) {
106 printf("Invalid hex string '%s'\n", pos
);
110 } else if (os_strncmp(buf
, "EM", 2) == 0) {
111 tmp_len
= os_strlen(pos
);
112 if (tmp_len
> sizeof(em
) * 2) {
116 em_len
= tmp_len
/ 2;
117 if (hexstr2bin(pos
, em
, em_len
) < 0) {
118 printf("Invalid hex string '%s'\n", pos
);
122 } else if (os_strcmp(buf
, "Result") == 0) {
125 struct crypto_public_key
*pk
;
129 const struct asn1_oid
*alg
;
133 if (os_strcmp(sha_alg
, "SHA1") == 0) {
134 if (sha1_vector(1, addr
, len
, hash
) < 0) {
139 alg
= &asn1_sha1_oid
;
140 } else if (os_strcmp(sha_alg
, "SHA256") == 0) {
141 if (sha256_vector(1, addr
, len
, hash
) < 0) {
146 alg
= &asn1_sha256_oid
;
151 printf("\nExpected result: %s\n", pos
);
152 wpa_hexdump(MSG_INFO
, "Hash(Msg)", hash
, hash_len
);
154 pk
= crypto_public_key_import_parts(n
, n_len
,
157 printf("Failed to import public key\n");
162 res
= pkcs1_v15_sig_ver(pk
, s
, s_len
, alg
,
164 crypto_public_key_free(pk
);
165 if ((*pos
== 'F' && !res
) || (*pos
!= 'F' && res
)) {
179 printf("Test case failed\n");
181 printf("%d test vectors OK\n", ok
);
187 int main(int argc
, char *argv
[])
193 if (cavp_rsa_sig_ver("CAVP/SigVer15_186-3.rsp"))
195 if (cavp_rsa_sig_ver("CAVP/SigVer15EMTest.txt"))