15
|
1 #include "test.h" |
|
2 |
143
|
3 #ifdef MRSA |
|
4 |
|
5 #define RSA_MSGSIZE 78 |
|
6 |
15
|
7 int rsa_test(void) |
|
8 { |
|
9 unsigned char in[1024], out[1024], tmp[1024]; |
143
|
10 rsa_key key, privKey, pubKey; |
15
|
11 int hash_idx, prng_idx, stat, stat2; |
143
|
12 unsigned long rsa_msgsize, len, len2; |
15
|
13 static unsigned char lparam[] = { 0x01, 0x02, 0x03, 0x04 }; |
|
14 |
|
15 hash_idx = find_hash("sha1"); |
|
16 prng_idx = find_prng("yarrow"); |
|
17 if (hash_idx == -1 || prng_idx == -1) { |
|
18 printf("rsa_test requires SHA1 and yarrow"); |
|
19 return 1; |
|
20 } |
|
21 |
|
22 /* make a random key */ |
|
23 DO(rsa_make_key(&test_yarrow, prng_idx, 1024/8, 65537, &key)); |
|
24 |
143
|
25 /* test PKCS #1 v1.5 */ |
|
26 for (rsa_msgsize = 1; rsa_msgsize <= 117; rsa_msgsize++) { |
|
27 /* make a random key/msg */ |
|
28 yarrow_read(in, rsa_msgsize, &test_yarrow); |
|
29 |
|
30 len = sizeof(out); |
|
31 len2 = rsa_msgsize; |
|
32 |
|
33 /* encrypt */ |
|
34 DO(rsa_v15_encrypt_key(in, rsa_msgsize, out, &len, &test_yarrow, prng_idx, &key)); |
|
35 DO(rsa_v15_decrypt_key(out, len, tmp, rsa_msgsize, &test_yarrow, prng_idx, &stat, &key)); |
|
36 if (stat != 1 || memcmp(tmp, in, rsa_msgsize)) { |
|
37 printf("PKCS #1 v1.5 encrypt/decrypt failure (rsa_msgsize: %lu, stat: %d)\n", rsa_msgsize, stat); |
|
38 return 1; |
|
39 } |
|
40 } |
|
41 |
|
42 /* signature */ |
|
43 len = sizeof(out); |
|
44 DO(rsa_v15_sign_hash(in, 20, out, &len, &test_yarrow, prng_idx, hash_idx, &key)); |
|
45 in[1] ^= 1; |
|
46 DO(rsa_v15_verify_hash(out, len, in, 20, &test_yarrow, prng_idx, hash_idx, &stat, &key)); |
|
47 in[1] ^= 1; |
|
48 DO(rsa_v15_verify_hash(out, len, in, 20, &test_yarrow, prng_idx, hash_idx, &stat2, &key)); |
|
49 if (!(stat == 0 && stat2 == 1)) { |
|
50 printf("PKCS #1 v1.5 sign/verify failure (stat %d, stat2 %d)\n", stat, stat2); |
15
|
51 return 1; |
|
52 } |
143
|
53 |
|
54 /* encrypt the key (without lparam) */ |
|
55 for (rsa_msgsize = 1; rsa_msgsize <= 86; rsa_msgsize++) { |
|
56 /* make a random key/msg */ |
|
57 yarrow_read(in, rsa_msgsize, &test_yarrow); |
|
58 |
|
59 len = sizeof(out); |
|
60 len2 = rsa_msgsize; |
|
61 |
|
62 DO(rsa_encrypt_key(in, rsa_msgsize, out, &len, NULL, 0, &test_yarrow, prng_idx, hash_idx, &key)); |
|
63 /* change a byte */ |
|
64 out[8] ^= 1; |
|
65 DO(rsa_decrypt_key(out, len, tmp, &len2, NULL, 0, &test_yarrow, prng_idx, hash_idx, &stat2, &key)); |
|
66 /* change a byte back */ |
|
67 out[8] ^= 1; |
|
68 if (len2 != rsa_msgsize) { |
|
69 printf("\nrsa_decrypt_key mismatch len %lu (first decrypt)", len2); |
|
70 return 1; |
|
71 } |
|
72 |
|
73 len2 = rsa_msgsize; |
|
74 DO(rsa_decrypt_key(out, len, tmp, &len2, NULL, 0, &test_yarrow, prng_idx, hash_idx, &stat, &key)); |
|
75 if (!(stat == 1 && stat2 == 0)) { |
|
76 printf("rsa_decrypt_key failed"); |
|
77 return 1; |
|
78 } |
|
79 if (len2 != rsa_msgsize || memcmp(tmp, in, rsa_msgsize)) { |
|
80 unsigned long x; |
|
81 printf("\nrsa_decrypt_key mismatch, len %lu (second decrypt)\n", len2); |
|
82 printf("Original contents: \n"); |
|
83 for (x = 0; x < rsa_msgsize; ) { |
|
84 printf("%02x ", in[x]); |
|
85 if (!(++x % 16)) { |
|
86 printf("\n"); |
|
87 } |
|
88 } |
|
89 printf("\n"); |
|
90 printf("Output contents: \n"); |
|
91 for (x = 0; x < rsa_msgsize; ) { |
|
92 printf("%02x ", out[x]); |
|
93 if (!(++x % 16)) { |
|
94 printf("\n"); |
|
95 } |
|
96 } |
|
97 printf("\n"); |
|
98 return 1; |
|
99 } |
15
|
100 } |
|
101 |
|
102 /* encrypt the key (with lparam) */ |
143
|
103 for (rsa_msgsize = 1; rsa_msgsize <= 86; rsa_msgsize++) { |
|
104 len = sizeof(out); |
|
105 len2 = rsa_msgsize; |
|
106 DO(rsa_encrypt_key(in, rsa_msgsize, out, &len, lparam, sizeof(lparam), &test_yarrow, prng_idx, hash_idx, &key)); |
|
107 /* change a byte */ |
|
108 out[8] ^= 1; |
|
109 DO(rsa_decrypt_key(out, len, tmp, &len2, lparam, sizeof(lparam), &test_yarrow, prng_idx, hash_idx, &stat2, &key)); |
|
110 if (len2 != rsa_msgsize) { |
|
111 printf("\nrsa_decrypt_key mismatch len %lu (first decrypt)", len2); |
|
112 return 1; |
|
113 } |
|
114 /* change a byte back */ |
|
115 out[8] ^= 1; |
|
116 |
|
117 len2 = rsa_msgsize; |
|
118 DO(rsa_decrypt_key(out, len, tmp, &len2, lparam, sizeof(lparam), &test_yarrow, prng_idx, hash_idx, &stat, &key)); |
|
119 if (!(stat == 1 && stat2 == 0)) { |
|
120 printf("rsa_decrypt_key failed"); |
|
121 return 1; |
|
122 } |
|
123 if (len2 != rsa_msgsize || memcmp(tmp, in, rsa_msgsize)) { |
|
124 printf("rsa_decrypt_key mismatch len %lu", len2); |
|
125 return 1; |
|
126 } |
15
|
127 } |
|
128 |
|
129 /* sign a message (unsalted, lower cholestorol and Atkins approved) now */ |
|
130 len = sizeof(out); |
|
131 DO(rsa_sign_hash(in, 20, out, &len, &test_yarrow, prng_idx, hash_idx, 0, &key)); |
143
|
132 |
|
133 /* export key and import as both private and public */ |
|
134 len2 = sizeof(tmp); |
|
135 DO(rsa_export(tmp, &len2, PK_PRIVATE, &key)); |
|
136 DO(rsa_import(tmp, len2, &privKey)); |
|
137 len2 = sizeof(tmp); |
|
138 DO(rsa_export(tmp, &len2, PK_PUBLIC, &key)); |
|
139 DO(rsa_import(tmp, len2, &pubKey)); |
|
140 |
|
141 /* verify with original */ |
15
|
142 DO(rsa_verify_hash(out, len, in, 20, &test_yarrow, prng_idx, hash_idx, 0, &stat, &key)); |
|
143 /* change a byte */ |
|
144 in[0] ^= 1; |
|
145 DO(rsa_verify_hash(out, len, in, 20, &test_yarrow, prng_idx, hash_idx, 0, &stat2, &key)); |
|
146 |
|
147 if (!(stat == 1 && stat2 == 0)) { |
143
|
148 printf("rsa_verify_hash (unsalted, origKey) failed, %d, %d", stat, stat2); |
|
149 rsa_free(&key); |
|
150 rsa_free(&pubKey); |
|
151 rsa_free(&privKey); |
|
152 return 1; |
|
153 } |
|
154 |
|
155 /* verify with privKey */ |
|
156 /* change a byte */ |
|
157 in[0] ^= 1; |
|
158 DO(rsa_verify_hash(out, len, in, 20, &test_yarrow, prng_idx, hash_idx, 0, &stat, &privKey)); |
|
159 /* change a byte */ |
|
160 in[0] ^= 1; |
|
161 DO(rsa_verify_hash(out, len, in, 20, &test_yarrow, prng_idx, hash_idx, 0, &stat2, &privKey)); |
|
162 |
|
163 if (!(stat == 1 && stat2 == 0)) { |
|
164 printf("rsa_verify_hash (unsalted, privKey) failed, %d, %d", stat, stat2); |
|
165 rsa_free(&key); |
|
166 rsa_free(&pubKey); |
|
167 rsa_free(&privKey); |
15
|
168 return 1; |
|
169 } |
|
170 |
143
|
171 /* verify with pubKey */ |
|
172 /* change a byte */ |
|
173 in[0] ^= 1; |
|
174 DO(rsa_verify_hash(out, len, in, 20, &test_yarrow, prng_idx, hash_idx, 0, &stat, &pubKey)); |
15
|
175 /* change a byte */ |
|
176 in[0] ^= 1; |
143
|
177 DO(rsa_verify_hash(out, len, in, 20, &test_yarrow, prng_idx, hash_idx, 0, &stat2, &pubKey)); |
|
178 |
|
179 if (!(stat == 1 && stat2 == 0)) { |
|
180 printf("rsa_verify_hash (unsalted, pubkey) failed, %d, %d", stat, stat2); |
|
181 rsa_free(&key); |
|
182 rsa_free(&pubKey); |
|
183 rsa_free(&privKey); |
|
184 return 1; |
|
185 } |
|
186 |
|
187 /* sign a message (salted) now (use privKey to make, pubKey to verify) */ |
|
188 len = sizeof(out); |
|
189 DO(rsa_sign_hash(in, 20, out, &len, &test_yarrow, prng_idx, hash_idx, 8, &privKey)); |
|
190 DO(rsa_verify_hash(out, len, in, 20, &test_yarrow, prng_idx, hash_idx, 8, &stat, &pubKey)); |
|
191 /* change a byte */ |
|
192 in[0] ^= 1; |
|
193 DO(rsa_verify_hash(out, len, in, 20, &test_yarrow, prng_idx, hash_idx, 8, &stat2, &pubKey)); |
15
|
194 |
|
195 if (!(stat == 1 && stat2 == 0)) { |
|
196 printf("rsa_verify_hash (salted) failed, %d, %d", stat, stat2); |
143
|
197 rsa_free(&key); |
|
198 rsa_free(&pubKey); |
|
199 rsa_free(&privKey); |
15
|
200 return 1; |
|
201 } |
|
202 |
|
203 /* free the key and return */ |
|
204 rsa_free(&key); |
143
|
205 rsa_free(&pubKey); |
|
206 rsa_free(&privKey); |
15
|
207 return 0; |
|
208 } |
143
|
209 |
|
210 #else |
|
211 |
|
212 int rsa_test(void) |
|
213 { |
|
214 printf("NOP"); |
|
215 return 0; |
|
216 } |
|
217 |
|
218 #endif |