3
|
1 /* This is the worst code you have ever seen written on purpose.... this code is just a big hack to test |
|
2 out the functionality of the library */ |
|
3 |
|
4 #ifdef SONY_PS2 |
|
5 #include <eetypes.h> |
|
6 #include <eeregs.h> |
|
7 #include "timer.h" |
|
8 #endif |
|
9 |
|
10 #include <mycrypt.h> |
|
11 |
|
12 int errnum; |
|
13 |
|
14 |
|
15 int |
|
16 null_setup (const unsigned char *key, int keylen, int num_rounds, |
|
17 symmetric_key * skey) |
|
18 { |
|
19 return CRYPT_OK; |
|
20 } |
|
21 |
|
22 void |
|
23 null_ecb_encrypt (const unsigned char *pt, unsigned char *ct, |
|
24 symmetric_key * key) |
|
25 { |
|
26 memcpy (ct, pt, 8); |
|
27 } |
|
28 |
|
29 void |
|
30 null_ecb_decrypt (const unsigned char *ct, unsigned char *pt, |
|
31 symmetric_key * key) |
|
32 { |
|
33 memcpy (pt, ct, 8); |
|
34 } |
|
35 |
|
36 int |
|
37 null_test (void) |
|
38 { |
|
39 return CRYPT_OK; |
|
40 } |
|
41 |
|
42 int |
|
43 null_keysize (int *desired_keysize) |
|
44 { |
|
45 return CRYPT_OK; |
|
46 } |
|
47 |
|
48 const struct _cipher_descriptor null_desc = { |
|
49 "memcpy()", |
|
50 255, |
|
51 8, 8, 8, 1, |
|
52 &null_setup, |
|
53 &null_ecb_encrypt, |
|
54 &null_ecb_decrypt, |
|
55 &null_test, |
|
56 &null_keysize |
|
57 }; |
|
58 |
|
59 |
|
60 prng_state prng; |
|
61 |
|
62 void |
|
63 store_tests (void) |
|
64 { |
|
65 unsigned char buf[8]; |
|
66 unsigned long L; |
|
67 ulong64 LL; |
|
68 |
|
69 printf ("LOAD32/STORE32 tests\n"); |
|
70 L = 0x12345678UL; |
|
71 STORE32L (L, &buf[0]); |
|
72 L = 0; |
|
73 LOAD32L (L, &buf[0]); |
|
74 if (L != 0x12345678UL) { |
|
75 printf ("LOAD/STORE32 Little don't work\n"); |
|
76 exit (-1); |
|
77 } |
|
78 LL = CONST64 (0x01020304050607); |
|
79 STORE64L (LL, &buf[0]); |
|
80 LL = 0; |
|
81 LOAD64L (LL, &buf[0]) |
|
82 if (LL != CONST64 (0x01020304050607)) { |
|
83 printf ("LOAD/STORE64 Little don't work\n"); |
|
84 exit (-1); |
|
85 } |
|
86 |
|
87 L = 0x12345678UL; |
|
88 STORE32H (L, &buf[0]); |
|
89 L = 0; |
|
90 LOAD32H (L, &buf[0]); |
|
91 if (L != 0x12345678UL) { |
|
92 printf ("LOAD/STORE32 High don't work, %08lx\n", L); |
|
93 exit (-1); |
|
94 } |
|
95 LL = CONST64 (0x01020304050607); |
|
96 STORE64H (LL, &buf[0]); |
|
97 LL = 0; |
|
98 LOAD64H (LL, &buf[0]) |
|
99 if (LL != CONST64 (0x01020304050607)) { |
|
100 printf ("LOAD/STORE64 High don't work\n"); |
|
101 exit (-1); |
|
102 } |
|
103 } |
|
104 |
|
105 void |
|
106 cipher_tests (void) |
|
107 { |
|
108 int x; |
|
109 |
|
110 printf ("Ciphers compiled in\n"); |
|
111 for (x = 0; cipher_descriptor[x].name != NULL; x++) { |
|
112 printf |
|
113 (" %12s (%2d) Key Size: %4d to %4d, Block Size: %3d, Default # of rounds: %2d\n", |
|
114 cipher_descriptor[x].name, cipher_descriptor[x].ID, |
|
115 cipher_descriptor[x].min_key_length * 8, |
|
116 cipher_descriptor[x].max_key_length * 8, |
|
117 cipher_descriptor[x].block_length * 8, |
|
118 cipher_descriptor[x].default_rounds); |
|
119 } |
|
120 |
|
121 } |
|
122 |
|
123 void |
|
124 ecb_tests (void) |
|
125 { |
|
126 int x; |
|
127 |
|
128 printf ("ECB tests\n"); |
|
129 for (x = 0; cipher_descriptor[x].name != NULL; x++) { |
|
130 printf (" %12s: ", cipher_descriptor[x].name); |
|
131 if ((errnum = cipher_descriptor[x].test ()) != CRYPT_OK) { |
|
132 printf (" **failed** Reason: %s\n", error_to_string (errnum)); |
|
133 exit (-1); |
|
134 } else { |
|
135 printf ("passed\n"); |
|
136 } |
|
137 } |
|
138 } |
|
139 |
|
140 #ifdef CBC |
|
141 void |
|
142 cbc_tests (void) |
|
143 { |
|
144 symmetric_CBC cbc; |
|
145 int x, y; |
|
146 unsigned char blk[32], ct[32], key[32], IV[32]; |
|
147 const unsigned char test[] = |
|
148 { 0XFF, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
|
149 |
|
150 printf ("CBC tests\n"); |
|
151 /* ---- CBC ENCODING ---- */ |
|
152 /* make up a block and IV */ |
|
153 for (x = 0; x < 32; x++) |
|
154 blk[x] = IV[x] = x; |
|
155 |
|
156 /* now lets start a cbc session */ |
|
157 if ((errnum = |
|
158 cbc_start (find_cipher ("blowfish"), IV, key, 16, 0, |
|
159 &cbc)) != CRYPT_OK) { |
|
160 printf ("CBC Setup: %s\n", error_to_string (errnum)); |
|
161 exit (-1); |
|
162 } |
|
163 |
|
164 /* now lets encode 32 bytes */ |
|
165 for (x = 0; x < 4; x++) { |
|
166 if ((errnum = cbc_encrypt (blk + 8 * x, ct + 8 * x, &cbc)) != CRYPT_OK) { |
|
167 printf ("CBC encrypt: %s\n", error_to_string (errnum)); |
|
168 exit (-1); |
|
169 } |
|
170 } |
|
171 |
|
172 zeromem (blk, sizeof (blk)); |
|
173 |
|
174 /* ---- CBC DECODING ---- */ |
|
175 /* make up a IV */ |
|
176 for (x = 0; x < 32; x++) |
|
177 IV[x] = x; |
|
178 |
|
179 /* now lets start a cbc session */ |
|
180 if ((errnum = |
|
181 cbc_start (find_cipher ("blowfish"), IV, key, 16, 0, |
|
182 &cbc)) != CRYPT_OK) { |
|
183 printf ("CBC Setup: %s\n", error_to_string (errnum)); |
|
184 exit (-1); |
|
185 } |
|
186 |
|
187 /* now lets decode 32 bytes */ |
|
188 for (x = 0; x < 4; x++) { |
|
189 if ((errnum = cbc_decrypt (ct + 8 * x, blk + 8 * x, &cbc)) != CRYPT_OK) { |
|
190 printf ("CBC decrypt: %s\n", error_to_string (errnum)); |
|
191 exit (-1); |
|
192 } |
|
193 } |
|
194 |
|
195 |
|
196 /* print output */ |
|
197 for (x = y = 0; x < 32; x++) |
|
198 if (blk[x] != x) |
|
199 y = 1; |
|
200 printf (" %s\n", y ? "failed" : "passed"); |
|
201 |
|
202 /* lets actually check the bytes */ |
|
203 memset (IV, 0, 8); |
|
204 IV[0] = 0xFF; /* IV = FF 00 00 00 00 00 00 00 */ |
|
205 memset (blk, 0, 32); |
|
206 blk[8] = 0xFF; /* BLK = 00 00 00 00 00 00 00 00 FF 00 00 00 00 00 00 00 */ |
|
207 cbc_start (find_cipher ("memcpy()"), IV, key, 8, 0, &cbc); |
|
208 cbc_encrypt (blk, ct, &cbc); /* expect: FF 00 00 00 00 00 00 00 */ |
|
209 cbc_encrypt (blk + 8, ct + 8, &cbc); /* expect: 00 00 00 00 00 00 00 00 */ |
|
210 if (memcmp (ct, test, 16)) { |
|
211 printf ("CBC failed logical testing.\n"); |
|
212 for (x = 0; x < 16; x++) |
|
213 printf ("%02x ", ct[x]); |
|
214 printf ("\n"); |
|
215 exit (-1); |
|
216 } else { |
|
217 printf ("CBC passed logical testing.\n"); |
|
218 } |
|
219 } |
|
220 #else |
|
221 void |
|
222 cbc_tests (void) |
|
223 { |
|
224 printf ("CBC not compiled in\n"); |
|
225 } |
|
226 #endif |
|
227 |
|
228 #ifdef OFB |
|
229 void |
|
230 ofb_tests (void) |
|
231 { |
|
232 symmetric_OFB ofb; |
|
233 int x, y; |
|
234 unsigned char blk[32], ct[32], key[32], IV[32]; |
|
235 |
|
236 printf ("OFB tests\n"); |
|
237 /* ---- ofb ENCODING ---- */ |
|
238 /* make up a block and IV */ |
|
239 for (x = 0; x < 32; x++) |
|
240 blk[x] = IV[x] = x; |
|
241 |
|
242 /* now lets start a ofb session */ |
|
243 if ((errnum = |
|
244 ofb_start (find_cipher ("cast5"), IV, key, 16, 0, &ofb)) != CRYPT_OK) { |
|
245 printf ("OFB Setup: %s\n", error_to_string (errnum)); |
|
246 exit (-1); |
|
247 } |
|
248 |
|
249 /* now lets encode 32 bytes */ |
|
250 for (x = 0; x < 4; x++) { |
|
251 if ((errnum = ofb_encrypt (blk + 8 * x, ct + 8 * x, 8, &ofb)) != CRYPT_OK) { |
|
252 printf ("OFB encrypt: %s\n", error_to_string (errnum)); |
|
253 exit (-1); |
|
254 } |
|
255 } |
|
256 |
|
257 zeromem (blk, sizeof (blk)); |
|
258 |
|
259 /* ---- ofb DECODING ---- */ |
|
260 /* make up a IV */ |
|
261 for (x = 0; x < 32; x++) |
|
262 IV[x] = x; |
|
263 |
|
264 /* now lets start a ofb session */ |
|
265 if ((errnum = |
|
266 ofb_start (find_cipher ("cast5"), IV, key, 16, 0, &ofb)) != CRYPT_OK) { |
|
267 printf ("OFB setup: %s\n", error_to_string (errnum)); |
|
268 exit (-1); |
|
269 } |
|
270 |
|
271 /* now lets decode 32 bytes */ |
|
272 for (x = 0; x < 4; x++) { |
|
273 if ((errnum = ofb_decrypt (ct + 8 * x, blk + 8 * x, 8, &ofb)) != CRYPT_OK) { |
|
274 printf ("OFB decrypt: %s\n", error_to_string (errnum)); |
|
275 exit (-1); |
|
276 } |
|
277 } |
|
278 |
|
279 /* print output */ |
|
280 for (x = y = 0; x < 32; x++) |
|
281 if (blk[x] != x) |
|
282 y = 1; |
|
283 printf (" %s\n", y ? "failed" : "passed"); |
|
284 if (y) |
|
285 exit (-1); |
|
286 } |
|
287 #else |
|
288 void |
|
289 ofb_tests (void) |
|
290 { |
|
291 printf ("OFB not compiled in\n"); |
|
292 } |
|
293 #endif |
|
294 |
|
295 #ifdef CFB |
|
296 void |
|
297 cfb_tests (void) |
|
298 { |
|
299 symmetric_CFB cfb; |
|
300 int x, y; |
|
301 unsigned char blk[32], ct[32], key[32], IV[32]; |
|
302 |
|
303 printf ("CFB tests\n"); |
|
304 /* ---- cfb ENCODING ---- */ |
|
305 /* make up a block and IV */ |
|
306 for (x = 0; x < 32; x++) |
|
307 blk[x] = IV[x] = x; |
|
308 |
|
309 /* now lets start a cfb session */ |
|
310 if ((errnum = |
|
311 cfb_start (find_cipher ("blowfish"), IV, key, 16, 0, |
|
312 &cfb)) != CRYPT_OK) { |
|
313 printf ("CFB setup: %s\n", error_to_string (errnum)); |
|
314 exit (-1); |
|
315 } |
|
316 |
|
317 /* now lets encode 32 bytes */ |
|
318 for (x = 0; x < 4; x++) { |
|
319 if ((errnum = cfb_encrypt (blk + 8 * x, ct + 8 * x, 8, &cfb)) != CRYPT_OK) { |
|
320 printf ("CFB encrypt: %s\n", error_to_string (errnum)); |
|
321 exit (-1); |
|
322 } |
|
323 } |
|
324 |
|
325 zeromem (blk, sizeof (blk)); |
|
326 |
|
327 /* ---- cfb DECODING ---- */ |
|
328 /* make up ahash_descriptor[prng->yarrow.hash].hashsize IV */ |
|
329 for (x = 0; x < 32; x++) |
|
330 IV[x] = x; |
|
331 |
|
332 /* now lets start a cfb session */ |
|
333 if ((errnum = |
|
334 cfb_start (find_cipher ("blowfish"), IV, key, 16, 0, |
|
335 &cfb)) != CRYPT_OK) { |
|
336 printf ("CFB Setup: %s\n", error_to_string (errnum)); |
|
337 exit (-1); |
|
338 } |
|
339 |
|
340 /* now lets decode 32 bytes */ |
|
341 for (x = 0; x < 4; x++) { |
|
342 if ((errnum = cfb_decrypt (ct + 8 * x, blk + 8 * x, 8, &cfb)) != CRYPT_OK) { |
|
343 printf ("CFB decrypt: %s\n", error_to_string (errnum)); |
|
344 exit (-1); |
|
345 } |
|
346 } |
|
347 |
|
348 /* print output */ |
|
349 for (x = y = 0; x < 32; x++) |
|
350 if (blk[x] != x) |
|
351 y = 1; |
|
352 printf (" %s\n", y ? "failed" : "passed"); |
|
353 if (y) |
|
354 exit (-1); |
|
355 } |
|
356 #else |
|
357 void |
|
358 cfb_tests (void) |
|
359 { |
|
360 printf ("CFB not compiled in\n"); |
|
361 } |
|
362 #endif |
|
363 |
|
364 #ifdef CTR |
|
365 void |
|
366 ctr_tests (void) |
|
367 { |
|
368 symmetric_CTR ctr; |
|
369 int x, y; |
|
370 unsigned char blk[32], ct[32], key[32], count[32]; |
|
371 const unsigned char test[] = |
|
372 { 0xFF, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0 }; |
|
373 |
|
374 printf ("CTR tests\n"); |
|
375 /* ---- CTR ENCODING ---- */ |
|
376 /* make up a block and IV */ |
|
377 for (x = 0; x < 32; x++) |
|
378 blk[x] = count[x] = x; |
|
379 |
|
380 /* now lets start a ctr session */ |
|
381 if ((errnum = |
|
382 ctr_start (find_cipher ("xtea"), count, key, 16, 0, |
|
383 &ctr)) != CRYPT_OK) { |
|
384 printf ("CTR Setup: %s\n", error_to_string (errnum)); |
|
385 exit (-1); |
|
386 } |
|
387 |
|
388 /* now lets encode 32 bytes */ |
|
389 for (x = 0; x < 4; x++) { |
|
390 if ((errnum = ctr_encrypt (blk + 8 * x, ct + 8 * x, 8, &ctr)) != CRYPT_OK) { |
|
391 printf ("CTR encrypt: %s\n", error_to_string (errnum)); |
|
392 exit (-1); |
|
393 } |
|
394 } |
|
395 |
|
396 zeromem (blk, sizeof (blk)); |
|
397 |
|
398 /* ---- CTR DECODING ---- */ |
|
399 /* make up a IV */ |
|
400 for (x = 0; x < 32; x++) |
|
401 count[x] = x; |
|
402 |
|
403 /* now lets start a cbc session */ |
|
404 if ((errnum = |
|
405 ctr_start (find_cipher ("xtea"), count, key, 16, 0, |
|
406 &ctr)) != CRYPT_OK) { |
|
407 printf ("CTR Setup: %s\n", error_to_string (errnum)); |
|
408 exit (-1); |
|
409 } |
|
410 |
|
411 /* now lets decode 32 bytes */ |
|
412 for (x = 0; x < 4; x++) { |
|
413 if ((errnum = ctr_decrypt (ct + 8 * x, blk + 8 * x, 8, &ctr)) != CRYPT_OK) { |
|
414 printf ("CTR decrypt: %s\n", error_to_string (errnum)); |
|
415 exit (-1); |
|
416 } |
|
417 } |
|
418 |
|
419 /* print output */ |
|
420 for (x = y = 0; x < 32; x++) |
|
421 if (blk[x] != x) |
|
422 y = 1; |
|
423 printf (" %s\n", y ? "failed" : "passed"); |
|
424 if (y) |
|
425 exit (-1); |
|
426 |
|
427 /* lets actually check the bytes */ |
|
428 memset (count, 0, 8); |
|
429 count[0] = 0xFF; /* IV = FF 00 00 00 00 00 00 00 */ |
|
430 memset (blk, 0, 32); |
|
431 blk[9] = 2; /* BLK = 00 00 00 00 00 00 00 00 00 02 00 00 00 00 00 00 */ |
|
432 ctr_start (find_cipher ("memcpy()"), count, key, 8, 0, &ctr); |
|
433 ctr_encrypt (blk, ct, 8, &ctr); /* expect: FF 00 00 00 00 00 00 00 */ |
|
434 ctr_encrypt (blk + 8, ct + 8, 8, &ctr); /* expect: 00 03 00 00 00 00 00 00 */ |
|
435 if (memcmp (ct, test, 16)) { |
|
436 printf ("CTR failed logical testing.\n"); |
|
437 for (x = 0; x < 16; x++) |
|
438 printf ("%02x ", ct[x]); |
|
439 printf ("\n"); |
|
440 } else { |
|
441 printf ("CTR passed logical testing.\n"); |
|
442 } |
|
443 |
|
444 } |
|
445 #else |
|
446 void |
|
447 ctr_tests (void) |
|
448 { |
|
449 printf ("CTR not compiled in\n"); |
|
450 } |
|
451 #endif |
|
452 |
|
453 void |
|
454 hash_tests (void) |
|
455 { |
|
456 int x; |
|
457 printf ("Hash tests\n"); |
|
458 for (x = 0; hash_descriptor[x].name != NULL; x++) { |
|
459 printf (" %10s (%2d) ", hash_descriptor[x].name, hash_descriptor[x].ID); |
|
460 if ((errnum = hash_descriptor[x].test ()) != CRYPT_OK) { |
|
461 printf ("**failed** Reason: %s\n", error_to_string (errnum)); |
|
462 exit(-1); |
|
463 } else { |
|
464 printf ("passed\n"); |
|
465 } |
|
466 } |
|
467 } |
|
468 |
|
469 #ifdef MRSA |
|
470 void |
|
471 pad_test (void) |
|
472 { |
|
473 unsigned char in[100], out[100]; |
|
474 unsigned long x, y; |
|
475 |
|
476 /* make a dummy message */ |
|
477 for (x = 0; x < 16; x++) |
|
478 in[x] = (unsigned char) x; |
|
479 |
|
480 /* pad the message so that random filler is placed before and after it */ |
|
481 y = 100; |
|
482 if ((errnum = |
|
483 rsa_pad (in, 16, out, &y, find_prng ("yarrow"), &prng)) != CRYPT_OK) { |
|
484 printf ("Error: %s\n", error_to_string (errnum)); |
|
485 exit (-1); |
|
486 } |
|
487 |
|
488 /* depad the message to get the original content */ |
|
489 memset (in, 0, sizeof (in)); |
|
490 x = 100; |
|
491 if ((errnum = rsa_depad (out, y, in, &x)) != CRYPT_OK) { |
|
492 printf ("Error: %s\n", error_to_string (errnum)); |
|
493 exit (-1); |
|
494 } |
|
495 |
|
496 /* check outcome */ |
|
497 printf ("rsa_pad: "); |
|
498 if (x != 16) { |
|
499 printf ("Failed. Wrong size.\n"); |
|
500 exit (-1); |
|
501 } |
|
502 for (x = 0; x < 16; x++) |
|
503 if (in[x] != x) { |
|
504 printf ("Failed. Expected %02lx and got %02x.\n", x, in[x]); |
|
505 exit (-1); |
|
506 } |
|
507 printf ("passed.\n"); |
|
508 } |
|
509 void |
|
510 rsa_test (void) |
|
511 { |
|
512 unsigned char in[520], out[520]; |
|
513 unsigned long x, y, z, limit; |
|
514 int stat; |
|
515 rsa_key key; |
|
516 clock_t t; |
|
517 |
|
518 /* ---- SINGLE ENCRYPT ---- */ |
|
519 /* encrypt a short 8 byte string */ |
|
520 if ((errnum = |
|
521 rsa_make_key (&prng, find_prng ("yarrow"), 1024 / 8, 65537, |
|
522 &key)) != CRYPT_OK) { |
|
523 printf ("Error: %s\n", error_to_string (errnum)); |
|
524 exit (-1); |
|
525 } |
|
526 for (x = 0; x < 8; x++) |
|
527 in[x] = (unsigned char) (x + 1); |
|
528 y = sizeof (in); |
|
529 if ((errnum = rsa_exptmod (in, 8, out, &y, PK_PUBLIC, &key)) != CRYPT_OK) { |
|
530 printf ("Error: %s\n", error_to_string (errnum)); |
|
531 exit (-1); |
|
532 } |
|
533 |
|
534 /* decrypt it */ |
|
535 zeromem (in, sizeof (in)); |
|
536 x = sizeof (out); |
|
537 if ((errnum = rsa_exptmod (out, y, in, &x, PK_PRIVATE, &key)) != CRYPT_OK) { |
|
538 printf ("Error: %s\n", error_to_string (errnum)); |
|
539 exit (-1); |
|
540 } |
|
541 |
|
542 /* compare */ |
|
543 printf ("RSA : "); |
|
544 for (x = 0; x < 8; x++) |
|
545 if (in[x] != (x + 1)) { |
|
546 printf ("Failed. x==%02lx, in[%ld]==%02x\n", x, x, in[x]); |
|
547 exit (-1); |
|
548 } |
|
549 printf ("passed.\n"); |
|
550 |
|
551 /* test the rsa_encrypt_key functions */ |
|
552 for (x = 0; x < 16; x++) |
|
553 in[x] = x; |
|
554 y = sizeof (out); |
|
555 if ((errnum = |
|
556 rsa_encrypt_key (in, 16, out, &y, &prng, find_prng ("yarrow"), |
|
557 &key)) != CRYPT_OK) { |
|
558 printf ("Error: %s\n", error_to_string (errnum)); |
|
559 exit (-1); |
|
560 } |
|
561 zeromem (in, sizeof (in)); |
|
562 x = sizeof (in); |
|
563 if ((errnum = rsa_decrypt_key (out, y, in, &x, &key)) != CRYPT_OK) { |
|
564 printf ("Error: %s\n", error_to_string (errnum)); |
|
565 exit (-1); |
|
566 } |
|
567 printf ("RSA en/de crypt key routines: "); |
|
568 if (x != 16) { |
|
569 printf ("Failed (length)\n"); |
|
570 exit (-1); |
|
571 } |
|
572 for (x = 0; x < 16; x++) |
|
573 if (in[x] != x) { |
|
574 printf ("Failed (contents)\n"); |
|
575 exit (-1); |
|
576 } |
|
577 printf ("Passed\n"); |
|
578 |
|
579 /* test sign_hash functions */ |
|
580 for (x = 0; x < 16; x++) |
|
581 in[x] = x; |
|
582 x = sizeof (in); |
|
583 if ((errnum = rsa_sign_hash (in, 16, out, &x, &key)) != CRYPT_OK) { |
|
584 printf ("Error: %s\n", error_to_string (errnum)); |
|
585 exit (-1); |
|
586 } |
|
587 printf ("RSA signed hash: %lu bytes\n", x); |
|
588 if ((errnum = rsa_verify_hash (out, x, in, &stat, &key)) != CRYPT_OK) { |
|
589 printf ("Error: %s\n", error_to_string (errnum)); |
|
590 exit (-1); |
|
591 } |
|
592 printf ("Verify hash: %s, ", stat ? "passed" : "failed"); |
|
593 in[0] ^= 1; |
|
594 if ((errnum = rsa_verify_hash (out, x, in, &stat, &key)) != CRYPT_OK) { |
|
595 printf ("Error: %s\n", error_to_string (errnum)); |
|
596 exit (-1); |
|
597 } |
|
598 printf ("%s\n", (!stat) ? "passed" : "failed"); |
|
599 if (stat) |
|
600 exit (-1); |
|
601 rsa_free (&key); |
|
602 |
|
603 /* make a RSA key */ |
|
604 #ifdef SONY_PS2_NOPE |
|
605 limit = 1024; |
|
606 #else |
|
607 limit = 2048; |
|
608 #endif |
|
609 |
|
610 { |
|
611 int tt; |
|
612 |
|
613 for (z = 1024; z <= limit; z += 512) { |
|
614 t = XCLOCK (); |
|
615 for (tt = 0; tt < 3; tt++) { |
|
616 if ((errnum = rsa_make_key (&prng, find_prng ("yarrow"), z / 8, 65537, &key)) != CRYPT_OK) { |
|
617 printf ("Error: %s\n", error_to_string (errnum)); |
|
618 exit (-1); |
|
619 } |
|
620 |
|
621 /* check modulus size */ |
|
622 if (mp_unsigned_bin_size(&key.N) != (int)(z/8)) { |
|
623 printf("\nRSA key supposed to be %lu bits but was %d bits\n", z, mp_count_bits(&key.N)); |
|
624 exit(EXIT_FAILURE); |
|
625 } |
|
626 |
|
627 if (tt < 2) { |
|
628 rsa_free (&key); |
|
629 } |
|
630 } |
|
631 t = XCLOCK () - t; |
|
632 printf ("Took %.0f ms to make a %ld-bit RSA key.\n", 1000.0 * (((double) t / 3.0) / (double) XCLOCKS_PER_SEC), z); |
|
633 |
|
634 /* time encryption */ |
|
635 t = XCLOCK (); |
|
636 |
|
637 for (tt = 0; tt < 20; tt++) { |
|
638 y = sizeof (in); |
|
639 if ((errnum = rsa_exptmod (in, 8, out, &y, PK_PUBLIC, &key)) != CRYPT_OK) { |
|
640 printf ("Error: %s\n", error_to_string (errnum)); |
|
641 exit (-1); |
|
642 } |
|
643 } |
|
644 t = XCLOCK () - t; |
|
645 printf ("Took %.0f ms to encrypt with a %ld-bit RSA key.\n", |
|
646 1000.0 * (((double) t / 20.0) / (double) XCLOCKS_PER_SEC), z); |
|
647 |
|
648 /* time decryption */ |
|
649 t = XCLOCK (); |
|
650 for (tt = 0; tt < 20; tt++) { |
|
651 x = sizeof (out); |
|
652 if ((errnum = rsa_exptmod (out, y, in, &x, PK_PRIVATE, &key)) != CRYPT_OK) { |
|
653 printf ("Error: %s\n", error_to_string (errnum)); |
|
654 exit (-1); |
|
655 } |
|
656 } |
|
657 t = XCLOCK () - t; |
|
658 printf ("Took %.0f ms to decrypt with a %ld-bit RSA key.\n", |
|
659 1000.0 * (((double) t / 20.0) / (double) XCLOCKS_PER_SEC), z); |
|
660 rsa_free (&key); |
|
661 } |
|
662 } |
|
663 } |
|
664 #else |
|
665 void |
|
666 pad_test (void) |
|
667 { |
|
668 printf ("MRSA not compiled in\n"); |
|
669 } |
|
670 |
|
671 void |
|
672 rsa_test (void) |
|
673 { |
|
674 printf ("MRSA not compiled in\n"); |
|
675 } |
|
676 #endif |
|
677 |
|
678 #ifdef BASE64 |
|
679 void |
|
680 base64_test (void) |
|
681 { |
|
682 unsigned char buf[2][100]; |
|
683 unsigned long x, y; |
|
684 |
|
685 printf ("Base64 tests\n"); |
|
686 zeromem (buf, sizeof (buf)); |
|
687 for (x = 0; x < 16; x++) |
|
688 buf[0][x] = (unsigned char) x; |
|
689 |
|
690 x = 100; |
|
691 if (base64_encode (buf[0], 16, buf[1], &x) != CRYPT_OK) { |
|
692 printf (" error: %s\n", error_to_string (errnum)); |
|
693 exit (-1); |
|
694 } |
|
695 printf (" encoded 16 bytes to %ld bytes...[%s]\n", x, buf[1]); |
|
696 memset (buf[0], 0, 100); |
|
697 y = 100; |
|
698 if (base64_decode (buf[1], x, buf[0], &y) != CRYPT_OK) { |
|
699 printf (" error: %s\n", error_to_string (errnum)); |
|
700 exit (-1); |
|
701 } |
|
702 printf (" decoded %ld bytes to %ld bytes\n", x, y); |
|
703 for (x = 0; x < 16; x++) |
|
704 if (buf[0][x] != x) { |
|
705 printf (" **failed**\n"); |
|
706 exit (-1); |
|
707 } |
|
708 printf (" passed\n"); |
|
709 } |
|
710 #else |
|
711 void |
|
712 base64_test (void) |
|
713 { |
|
714 printf ("Base64 not compiled in\n"); |
|
715 } |
|
716 #endif |
|
717 |
|
718 void |
|
719 time_hash (void) |
|
720 { |
|
721 clock_t t1; |
|
722 int x, y; |
|
723 unsigned long z; |
|
724 unsigned char input[4096], out[MAXBLOCKSIZE]; |
|
725 printf ("Hash Time Trials (4KB blocks):\n"); |
|
726 for (x = 0; hash_descriptor[x].name != NULL; x++) { |
|
727 t1 = XCLOCK (); |
|
728 z = sizeof (out); |
|
729 y = 0; |
|
730 while (XCLOCK () - t1 < (5 * XCLOCKS_PER_SEC)) { |
|
731 hash_memory (x, input, 4096, out, &z); |
|
732 hash_memory (x, input, 4096, out, &z); |
|
733 hash_memory (x, input, 4096, out, &z); |
|
734 hash_memory (x, input, 4096, out, &z); |
|
735 hash_memory (x, input, 4096, out, &z); |
|
736 hash_memory (x, input, 4096, out, &z); |
|
737 hash_memory (x, input, 4096, out, &z); |
|
738 hash_memory (x, input, 4096, out, &z); |
|
739 hash_memory (x, input, 4096, out, &z); |
|
740 hash_memory (x, input, 4096, out, &z); |
|
741 hash_memory (x, input, 4096, out, &z); |
|
742 hash_memory (x, input, 4096, out, &z); |
|
743 hash_memory (x, input, 4096, out, &z); |
|
744 hash_memory (x, input, 4096, out, &z); |
|
745 hash_memory (x, input, 4096, out, &z); |
|
746 hash_memory (x, input, 4096, out, &z); |
|
747 hash_memory (x, input, 4096, out, &z); |
|
748 hash_memory (x, input, 4096, out, &z); |
|
749 hash_memory (x, input, 4096, out, &z); |
|
750 hash_memory (x, input, 4096, out, &z); |
|
751 hash_memory (x, input, 4096, out, &z); |
|
752 hash_memory (x, input, 4096, out, &z); |
|
753 hash_memory (x, input, 4096, out, &z); |
|
754 hash_memory (x, input, 4096, out, &z); |
|
755 hash_memory (x, input, 4096, out, &z); |
|
756 hash_memory (x, input, 4096, out, &z); |
|
757 hash_memory (x, input, 4096, out, &z); |
|
758 hash_memory (x, input, 4096, out, &z); |
|
759 hash_memory (x, input, 4096, out, &z); |
|
760 hash_memory (x, input, 4096, out, &z); |
|
761 hash_memory (x, input, 4096, out, &z); |
|
762 hash_memory (x, input, 4096, out, &z); |
|
763 y += 32; |
|
764 } |
|
765 t1 = XCLOCK () - t1; |
|
766 printf ("%-20s: Hash at %5.2f Mbit/sec\n", hash_descriptor[x].name, |
|
767 ((8.0 * 4096.0) * |
|
768 ((double) y / ((double) t1 / (double) XCLOCKS_PER_SEC))) / |
|
769 1000000.0); |
|
770 } |
|
771 } |
|
772 |
|
773 void |
|
774 time_ecb (void) |
|
775 { |
|
776 clock_t t1, t2; |
|
777 long x, y1, y2; |
|
778 unsigned char pt[32], key[32]; |
|
779 symmetric_key skey; |
|
780 void (*func) (const unsigned char *, unsigned char *, symmetric_key *); |
|
781 |
|
782 printf ("ECB Time Trials for the Symmetric Ciphers:\n"); |
|
783 for (x = 0; cipher_descriptor[x].name != NULL; x++) { |
|
784 cipher_descriptor[x].setup (key, cipher_descriptor[x].min_key_length, 0, |
|
785 &skey); |
|
786 |
|
787 #define DO1 func(pt,pt,&skey); |
|
788 #define DO2 DO1 DO1 |
|
789 #define DO4 DO2 DO2 |
|
790 #define DO8 DO4 DO4 |
|
791 #define DO16 DO8 DO8 |
|
792 #define DO32 DO16 DO16 |
|
793 #define DO64 DO32 DO32 |
|
794 #define DO128 DO64 DO64 |
|
795 #define DO256 DO128 DO128 |
|
796 |
|
797 func = cipher_descriptor[x].ecb_encrypt; |
|
798 y1 = 0; |
|
799 t1 = XCLOCK (); |
|
800 while (XCLOCK () - t1 < 3 * XCLOCKS_PER_SEC) { |
|
801 DO256; |
|
802 y1 += 256; |
|
803 } |
|
804 t1 = XCLOCK () - t1; |
|
805 |
|
806 func = cipher_descriptor[x].ecb_decrypt; |
|
807 y2 = 0; |
|
808 t2 = XCLOCK (); |
|
809 while (XCLOCK () - t2 < 3 * XCLOCKS_PER_SEC) { |
|
810 DO256; |
|
811 y2 += 256; |
|
812 } |
|
813 t2 = XCLOCK () - t2; |
|
814 printf |
|
815 ("%-20s: Encrypt at %5.2f Mbit/sec and Decrypt at %5.2f Mbit/sec\n", |
|
816 cipher_descriptor[x].name, |
|
817 ((8.0 * (double) cipher_descriptor[x].block_length) * |
|
818 ((double) y1 / ((double) t1 / (double) XCLOCKS_PER_SEC))) / 1000000.0, |
|
819 ((8.0 * (double) cipher_descriptor[x].block_length) * |
|
820 ((double) y2 / ((double) t2 / (double) XCLOCKS_PER_SEC))) / |
|
821 1000000.0); |
|
822 |
|
823 #undef DO256 |
|
824 #undef DO128 |
|
825 #undef DO64 |
|
826 #undef DO32 |
|
827 #undef DO16 |
|
828 #undef DO8 |
|
829 #undef DO4 |
|
830 #undef DO2 |
|
831 #undef DO1 |
|
832 } |
|
833 } |
|
834 |
|
835 #ifdef MDH |
|
836 void |
|
837 dh_tests (void) |
|
838 { |
|
839 unsigned char buf[3][4096]; |
|
840 unsigned long x, y, z; |
|
841 int low, high, stat, stat2; |
|
842 dh_key usera, userb; |
|
843 clock_t t1; |
|
844 |
|
845 printf("Testing builting DH parameters...."); fflush(stdout); |
|
846 if ((errnum = dh_test()) != CRYPT_OK) { |
|
847 printf("DH Error: %s\n", error_to_string(errnum)); |
|
848 exit(-1); |
|
849 } |
|
850 printf("Passed.\n"); |
|
851 |
|
852 dh_sizes (&low, &high); |
|
853 printf ("DH Keys from %d to %d supported.\n", low * 8, high * 8); |
|
854 |
|
855 /* make up two keys */ |
|
856 if ((errnum = |
|
857 dh_make_key (&prng, find_prng ("yarrow"), 96, &usera)) != CRYPT_OK) { |
|
858 printf ("Error: %s\n", error_to_string (errnum)); |
|
859 exit (-1); |
|
860 } |
|
861 if ((errnum = |
|
862 dh_make_key (&prng, find_prng ("yarrow"), 96, &userb)) != CRYPT_OK) { |
|
863 printf ("Error: %s\n", error_to_string (errnum)); |
|
864 exit (-1); |
|
865 } |
|
866 |
|
867 /* make the shared secret */ |
|
868 x = 4096; |
|
869 if ((errnum = dh_shared_secret (&usera, &userb, buf[0], &x)) != CRYPT_OK) { |
|
870 printf ("Error: %s\n", error_to_string (errnum)); |
|
871 exit (-1); |
|
872 } |
|
873 |
|
874 y = 4096; |
|
875 if ((errnum = dh_shared_secret (&userb, &usera, buf[1], &y)) != CRYPT_OK) { |
|
876 printf ("Error: %s\n", error_to_string (errnum)); |
|
877 exit (-1); |
|
878 } |
|
879 if (y != x) { |
|
880 printf ("DH Shared keys are not same size.\n"); |
|
881 exit (-1); |
|
882 } |
|
883 if (memcmp (buf[0], buf[1], x)) { |
|
884 printf ("DH Shared keys not same contents.\n"); |
|
885 exit (-1); |
|
886 } |
|
887 |
|
888 /* now export userb */ |
|
889 y = 4096; |
|
890 if ((errnum = dh_export (buf[1], &y, PK_PUBLIC, &userb)) != CRYPT_OK) { |
|
891 printf ("Error: %s\n", error_to_string (errnum)); |
|
892 exit (-1); |
|
893 } |
|
894 dh_free (&userb); |
|
895 |
|
896 /* import and make the shared secret again */ |
|
897 if ((errnum = dh_import (buf[1], y, &userb)) != CRYPT_OK) { |
|
898 printf ("Error: %s\n", error_to_string (errnum)); |
|
899 exit (-1); |
|
900 } |
|
901 z = 4096; |
|
902 if ((errnum = dh_shared_secret (&usera, &userb, buf[2], &z)) != CRYPT_OK) { |
|
903 printf ("Error: %s\n", error_to_string (errnum)); |
|
904 exit (-1); |
|
905 } |
|
906 |
|
907 printf ("DH routines: "); |
|
908 if (z != x) { |
|
909 printf ("failed. Size don't match?\n"); |
|
910 exit (-1); |
|
911 } |
|
912 if (memcmp (buf[0], buf[2], x)) { |
|
913 printf ("Failed. Content didn't match.\n"); |
|
914 exit (-1); |
|
915 } |
|
916 printf ("Passed\n"); |
|
917 dh_free (&usera); |
|
918 dh_free (&userb); |
|
919 |
|
920 /* time stuff */ |
|
921 { |
|
922 static int sizes[] = { 96, 128, 160, 192, 224, 256, 320, 384, 512 }; |
|
923 int ii, tt; |
|
924 |
|
925 for (ii = 0; ii < (int) (sizeof (sizes) / sizeof (sizes[0])); ii++) { |
|
926 t1 = XCLOCK (); |
|
927 for (tt = 0; tt < 25; tt++) { |
|
928 dh_make_key (&prng, find_prng ("yarrow"), sizes[ii], &usera); |
|
929 dh_free (&usera); |
|
930 } |
|
931 t1 = XCLOCK () - t1; |
|
932 printf ("Make dh-%d key took %f msec\n", sizes[ii] * 8, |
|
933 1000.0 * (((double) t1 / 25.0) / (double) XCLOCKS_PER_SEC)); |
|
934 } |
|
935 } |
|
936 |
|
937 /* test encrypt_key */ |
|
938 dh_make_key (&prng, find_prng ("yarrow"), 128, &usera); |
|
939 for (x = 0; x < 16; x++) |
|
940 buf[0][x] = x; |
|
941 y = sizeof (buf[1]); |
|
942 if ((errnum = |
|
943 dh_encrypt_key (buf[0], 16, buf[1], &y, &prng, find_prng ("yarrow"), |
|
944 find_hash ("md5"), &usera)) != CRYPT_OK) { |
|
945 printf ("Error: %s\n", error_to_string (errnum)); |
|
946 exit (-1); |
|
947 } |
|
948 zeromem (buf[0], sizeof (buf[0])); |
|
949 x = sizeof (buf[0]); |
|
950 if ((errnum = dh_decrypt_key (buf[1], y, buf[0], &x, &usera)) != CRYPT_OK) { |
|
951 printf ("Error: %s\n", error_to_string (errnum)); |
|
952 exit (-1); |
|
953 } |
|
954 printf ("DH en/de crypt key routines: "); |
|
955 if (x != 16) { |
|
956 printf ("Failed (length)\n"); |
|
957 exit (-1); |
|
958 } |
|
959 for (x = 0; x < 16; x++) |
|
960 if (buf[0][x] != x) { |
|
961 printf ("Failed (contents)\n"); |
|
962 exit (-1); |
|
963 } |
|
964 printf ("Passed (size %lu)\n", y); |
|
965 |
|
966 /* test sign_hash */ |
|
967 for (x = 0; x < 16; x++) |
|
968 buf[0][x] = x; |
|
969 x = sizeof (buf[1]); |
|
970 if ((errnum = |
|
971 dh_sign_hash (buf[0], 16, buf[1], &x, &prng, find_prng ("yarrow"), |
|
972 &usera)) != CRYPT_OK) { |
|
973 printf ("Error: %s\n", error_to_string (errnum)); |
|
974 exit (-1); |
|
975 } |
|
976 if ((errnum = dh_verify_hash (buf[1], x, buf[0], 16, &stat, &usera)) != CRYPT_OK) { |
|
977 printf ("Error: %s\n", error_to_string (errnum)); |
|
978 exit (-1); |
|
979 } |
|
980 buf[0][0] ^= 1; |
|
981 if ((errnum = dh_verify_hash (buf[1], x, buf[0], 16, &stat2, &usera)) != CRYPT_OK) { |
|
982 printf ("Error: %s\n", error_to_string (errnum)); |
|
983 exit (-1); |
|
984 } |
|
985 printf ("dh_sign/verify_hash: %s (%d,%d), %lu\n", |
|
986 ((stat == 1) |
|
987 && (stat2 == 0)) ? "passed" : "failed", stat, stat2, x); |
|
988 dh_free (&usera); |
|
989 } |
|
990 #else |
|
991 void |
|
992 dh_tests (void) |
|
993 { |
|
994 printf ("MDH not compiled in\n"); |
|
995 } |
|
996 #endif |
|
997 |
|
998 int callback_x = 0; |
|
999 void |
|
1000 callback (void) |
|
1001 { |
|
1002 printf ("%c\x08", "-\\|/"[++callback_x & 3]); |
|
1003 #ifndef SONY_PS2 |
|
1004 fflush (stdout); |
|
1005 #endif |
|
1006 } |
|
1007 |
|
1008 void |
|
1009 rng_tests (void) |
|
1010 { |
|
1011 unsigned char buf[16]; |
|
1012 clock_t t1; |
|
1013 int x, y; |
|
1014 |
|
1015 printf ("RNG tests\n"); |
|
1016 t1 = XCLOCK (); |
|
1017 x = rng_get_bytes (buf, sizeof (buf), &callback); |
|
1018 t1 = XCLOCK () - t1; |
|
1019 printf (" %f bytes per second...", |
|
1020 (double) x / ((double) t1 / (double) XCLOCKS_PER_SEC)); |
|
1021 printf ("read %d bytes.\n ", x); |
|
1022 for (y = 0; y < x; y++) |
|
1023 printf ("%02x ", buf[y]); |
|
1024 printf ("\n"); |
|
1025 |
|
1026 #ifdef YARROW |
|
1027 if ((errnum = |
|
1028 rng_make_prng (128, find_prng ("yarrow"), &prng, |
|
1029 &callback)) != CRYPT_OK) { |
|
1030 printf (" starting yarrow error: %s\n", error_to_string (errnum)); |
|
1031 exit (-1); |
|
1032 } |
|
1033 #endif |
|
1034 } |
|
1035 |
|
1036 #ifdef MECC |
|
1037 void |
|
1038 ecc_tests (void) |
|
1039 { |
|
1040 unsigned char buf[4][4096]; |
|
1041 unsigned long x, y, z; |
|
1042 int stat, stat2, low, high; |
|
1043 ecc_key usera, userb; |
|
1044 clock_t t1; |
|
1045 |
|
1046 if ((errnum = ecc_test ()) != CRYPT_OK) { |
|
1047 printf ("ecc Error: %s\n", error_to_string (errnum)); |
|
1048 exit (-1); |
|
1049 } |
|
1050 |
|
1051 ecc_sizes (&low, &high); |
|
1052 printf ("ecc Keys from %d to %d supported.\n", low * 8, high * 8); |
|
1053 |
|
1054 /* make up two keys */ |
|
1055 if ((errnum = |
|
1056 ecc_make_key (&prng, find_prng ("yarrow"), 24, &usera)) != CRYPT_OK) { |
|
1057 printf ("Error: %s\n", error_to_string (errnum)); |
|
1058 exit (-1); |
|
1059 } |
|
1060 if ((errnum = |
|
1061 ecc_make_key (&prng, find_prng ("yarrow"), 24, &userb)) != CRYPT_OK) { |
|
1062 printf ("Error: %s\n", error_to_string (errnum)); |
|
1063 exit (-1); |
|
1064 } |
|
1065 |
|
1066 /* make the shared secret */ |
|
1067 x = 4096; |
|
1068 if ((errnum = ecc_shared_secret (&usera, &userb, buf[0], &x)) != CRYPT_OK) { |
|
1069 printf ("Error: %s\n", error_to_string (errnum)); |
|
1070 exit (-1); |
|
1071 } |
|
1072 |
|
1073 y = 4096; |
|
1074 if ((errnum = ecc_shared_secret (&userb, &usera, buf[1], &y)) != CRYPT_OK) { |
|
1075 printf ("Error: %s\n", error_to_string (errnum)); |
|
1076 exit (-1); |
|
1077 } |
|
1078 |
|
1079 if (y != x) { |
|
1080 printf ("ecc Shared keys are not same size.\n"); |
|
1081 exit (-1); |
|
1082 } |
|
1083 |
|
1084 if (memcmp (buf[0], buf[1], x)) { |
|
1085 printf ("ecc Shared keys not same contents.\n"); |
|
1086 exit (-1); |
|
1087 } |
|
1088 |
|
1089 /* now export userb */ |
|
1090 y = 4096; |
|
1091 if ((errnum = ecc_export (buf[1], &y, PK_PUBLIC, &userb)) != CRYPT_OK) { |
|
1092 printf ("Error: %s\n", error_to_string (errnum)); |
|
1093 exit (-1); |
|
1094 } |
|
1095 ecc_free (&userb); |
|
1096 printf ("ECC-192 export took %ld bytes\n", y); |
|
1097 |
|
1098 /* import and make the shared secret again */ |
|
1099 if ((errnum = ecc_import (buf[1], y, &userb)) != CRYPT_OK) { |
|
1100 printf ("Error: %s\n", error_to_string (errnum)); |
|
1101 exit (-1); |
|
1102 } |
|
1103 |
|
1104 z = 4096; |
|
1105 if ((errnum = ecc_shared_secret (&usera, &userb, buf[2], &z)) != CRYPT_OK) { |
|
1106 printf ("Error: %s\n", error_to_string (errnum)); |
|
1107 exit (-1); |
|
1108 } |
|
1109 |
|
1110 printf ("ecc routines: "); |
|
1111 if (z != x) { |
|
1112 printf ("failed. Size don't match?\n"); |
|
1113 exit (-1); |
|
1114 } |
|
1115 if (memcmp (buf[0], buf[2], x)) { |
|
1116 printf ("Failed. Content didn't match.\n"); |
|
1117 exit (-1); |
|
1118 } |
|
1119 printf ("Passed\n"); |
|
1120 ecc_free (&usera); |
|
1121 ecc_free (&userb); |
|
1122 |
|
1123 /* time stuff */ |
|
1124 { |
|
1125 static int sizes[] = { 20, 24, 28, 32, 48, 65 }; |
|
1126 int ii, tt; |
|
1127 |
|
1128 for (ii = 0; ii < (int) (sizeof (sizes) / sizeof (sizes[0])); ii++) { |
|
1129 t1 = XCLOCK (); |
|
1130 for (tt = 0; tt < 10; tt++) { |
|
1131 if ((errnum = |
|
1132 ecc_make_key (&prng, find_prng ("yarrow"), sizes[ii], |
|
1133 &usera)) != CRYPT_OK) { |
|
1134 printf ("Error: %s\n", error_to_string (errnum)); |
|
1135 exit (-1); |
|
1136 } |
|
1137 ecc_free (&usera); |
|
1138 } |
|
1139 t1 = XCLOCK () - t1; |
|
1140 printf ("Make ECC-%d key took %f msec\n", sizes[ii] * 8, |
|
1141 1000.0 * (((double) t1 / 10.0) / (double) XCLOCKS_PER_SEC)); |
|
1142 } |
|
1143 } |
|
1144 |
|
1145 /* test encrypt_key */ |
|
1146 ecc_make_key (&prng, find_prng ("yarrow"), 20, &usera); |
|
1147 for (x = 0; x < 32; x++) |
|
1148 buf[0][x] = x; |
|
1149 y = sizeof (buf[1]); |
|
1150 if ((errnum = |
|
1151 ecc_encrypt_key (buf[0], 32, buf[1], &y, &prng, find_prng ("yarrow"), |
|
1152 find_hash ("sha256"), &usera)) != CRYPT_OK) { |
|
1153 printf ("Error: %s\n", error_to_string (errnum)); |
|
1154 exit (-1); |
|
1155 } |
|
1156 zeromem (buf[0], sizeof (buf[0])); |
|
1157 x = sizeof (buf[0]); |
|
1158 if ((errnum = ecc_decrypt_key (buf[1], y, buf[0], &x, &usera)) != CRYPT_OK) { |
|
1159 printf ("Error: %s\n", error_to_string (errnum)); |
|
1160 exit (-1); |
|
1161 } |
|
1162 printf ("ECC en/de crypt key routines: "); |
|
1163 if (x != 32) { |
|
1164 printf ("Failed (length)\n"); |
|
1165 exit (-1); |
|
1166 } |
|
1167 for (x = 0; x < 32; x++) |
|
1168 if (buf[0][x] != x) { |
|
1169 printf ("Failed (contents)\n"); |
|
1170 exit (-1); |
|
1171 } |
|
1172 printf ("Passed (size: %lu)\n", y); |
|
1173 /* test sign_hash */ |
|
1174 for (x = 0; x < 16; x++) |
|
1175 buf[0][x] = x; |
|
1176 x = sizeof (buf[1]); |
|
1177 if ((errnum = |
|
1178 ecc_sign_hash (buf[0], 16, buf[1], &x, &prng, find_prng ("yarrow"), |
|
1179 &usera)) != CRYPT_OK) { |
|
1180 printf ("Error: %s\n", error_to_string (errnum)); |
|
1181 exit (-1); |
|
1182 } |
|
1183 printf("Signature size: %lu\n", x); |
|
1184 if (ecc_verify_hash (buf[1], x, buf[0], 16, &stat, &usera)) { |
|
1185 printf ("Error: %s\n", error_to_string (errnum)); |
|
1186 exit (-1); |
|
1187 } |
|
1188 buf[0][0] ^= 1; |
|
1189 if (ecc_verify_hash (buf[1], x, buf[0], 16, &stat2, &usera)) { |
|
1190 printf ("Error: %s\n", error_to_string (errnum)); |
|
1191 exit (-1); |
|
1192 } |
|
1193 printf ("ecc_sign/verify_hash: %s (%d,%d)\n", |
|
1194 ((stat == 1) && (stat2 == 0)) ? "passed" : "failed", stat, stat2); |
|
1195 ecc_free (&usera); |
|
1196 } |
|
1197 #else |
|
1198 void |
|
1199 ecc_tests (void) |
|
1200 { |
|
1201 printf ("MECC not compiled in\n"); |
|
1202 } |
|
1203 #endif |
|
1204 |
|
1205 #ifdef MPI |
|
1206 void |
|
1207 test_prime (void) |
|
1208 { |
|
1209 char buf[1024]; |
|
1210 mp_int a; |
|
1211 int x; |
|
1212 |
|
1213 /* make a 1024 bit prime */ |
|
1214 mp_init (&a); |
|
1215 rand_prime (&a, 128*8, &prng, find_prng ("yarrow")); |
|
1216 |
|
1217 /* dump it */ |
|
1218 mp_todecimal (&a, buf); |
|
1219 printf ("1024-bit prime:\n"); |
|
1220 for (x = 0; x < (int) strlen (buf);) { |
|
1221 printf ("%c", buf[x]); |
|
1222 if (!(++x % 60)) |
|
1223 printf ("\\ \n"); |
|
1224 } |
|
1225 printf ("\n\n"); |
|
1226 |
|
1227 mp_clear (&a); |
|
1228 } |
|
1229 #else |
|
1230 void |
|
1231 test_prime (void) |
|
1232 { |
|
1233 printf ("MPI not compiled in\n"); |
|
1234 } |
|
1235 #endif |
|
1236 |
|
1237 void |
|
1238 register_all_algs (void) |
|
1239 { |
|
1240 #ifdef RIJNDAEL |
|
1241 register_cipher (&aes_desc); |
|
1242 #endif |
|
1243 #ifdef BLOWFISH |
|
1244 register_cipher (&blowfish_desc); |
|
1245 #endif |
|
1246 #ifdef XTEA |
|
1247 register_cipher (&xtea_desc); |
|
1248 #endif |
|
1249 #ifdef RC5 |
|
1250 register_cipher (&rc5_desc); |
|
1251 #endif |
|
1252 #ifdef RC6 |
|
1253 register_cipher (&rc6_desc); |
|
1254 #endif |
|
1255 #ifdef SAFERP |
|
1256 register_cipher (&saferp_desc); |
|
1257 #endif |
|
1258 #ifdef TWOFISH |
|
1259 register_cipher (&twofish_desc); |
|
1260 #endif |
|
1261 #ifdef SAFER |
|
1262 register_cipher (&safer_k64_desc); |
|
1263 register_cipher (&safer_sk64_desc); |
|
1264 register_cipher (&safer_k128_desc); |
|
1265 register_cipher (&safer_sk128_desc); |
|
1266 #endif |
|
1267 #ifdef RC2 |
|
1268 register_cipher (&rc2_desc); |
|
1269 #endif |
|
1270 #ifdef DES |
|
1271 register_cipher (&des_desc); |
|
1272 register_cipher (&des3_desc); |
|
1273 #endif |
|
1274 #ifdef CAST5 |
|
1275 register_cipher (&cast5_desc); |
|
1276 #endif |
|
1277 #ifdef NOEKEON |
|
1278 register_cipher (&noekeon_desc); |
|
1279 #endif |
|
1280 #ifdef SKIPJACK |
|
1281 register_cipher (&skipjack_desc); |
|
1282 #endif |
|
1283 register_cipher (&null_desc); |
|
1284 |
|
1285 #ifdef TIGER |
|
1286 register_hash (&tiger_desc); |
|
1287 #endif |
|
1288 #ifdef MD2 |
|
1289 register_hash (&md2_desc); |
|
1290 #endif |
|
1291 #ifdef MD4 |
|
1292 register_hash (&md4_desc); |
|
1293 #endif |
|
1294 #ifdef MD5 |
|
1295 register_hash (&md5_desc); |
|
1296 #endif |
|
1297 #ifdef SHA1 |
|
1298 register_hash (&sha1_desc); |
|
1299 #endif |
|
1300 #ifdef SHA256 |
|
1301 register_hash (&sha256_desc); |
|
1302 #endif |
|
1303 #ifdef SHA224 |
|
1304 register_hash (&sha224_desc); |
|
1305 #endif |
|
1306 #ifdef SHA384 |
|
1307 register_hash (&sha384_desc); |
|
1308 #endif |
|
1309 #ifdef SHA512 |
|
1310 register_hash (&sha512_desc); |
|
1311 #endif |
|
1312 #ifdef RIPEMD128 |
|
1313 register_hash (&rmd128_desc); |
|
1314 #endif |
|
1315 #ifdef RIPEMD160 |
|
1316 register_hash (&rmd160_desc); |
|
1317 #endif |
|
1318 #ifdef WHIRLPOOL |
|
1319 register_hash (&whirlpool_desc); |
|
1320 #endif |
|
1321 |
|
1322 #ifdef YARROW |
|
1323 register_prng (&yarrow_desc); |
|
1324 #endif |
|
1325 #ifdef SPRNG |
|
1326 register_prng (&sprng_desc); |
|
1327 #endif |
|
1328 } |
|
1329 |
|
1330 void |
|
1331 test_errs (void) |
|
1332 { |
|
1333 #define ERR(x) printf("%25s => %s\n", #x, error_to_string(x)); |
|
1334 |
|
1335 ERR (CRYPT_OK); |
|
1336 ERR (CRYPT_ERROR); |
|
1337 |
|
1338 ERR (CRYPT_INVALID_KEYSIZE); |
|
1339 ERR (CRYPT_INVALID_ROUNDS); |
|
1340 ERR (CRYPT_FAIL_TESTVECTOR); |
|
1341 |
|
1342 ERR (CRYPT_BUFFER_OVERFLOW); |
|
1343 ERR (CRYPT_INVALID_PACKET); |
|
1344 |
|
1345 ERR (CRYPT_INVALID_PRNGSIZE); |
|
1346 ERR (CRYPT_ERROR_READPRNG); |
|
1347 |
|
1348 ERR (CRYPT_INVALID_CIPHER); |
|
1349 ERR (CRYPT_INVALID_HASH); |
|
1350 ERR (CRYPT_INVALID_PRNG); |
|
1351 |
|
1352 ERR (CRYPT_MEM); |
|
1353 |
|
1354 ERR (CRYPT_PK_TYPE_MISMATCH); |
|
1355 ERR (CRYPT_PK_NOT_PRIVATE); |
|
1356 |
|
1357 ERR (CRYPT_INVALID_ARG); |
|
1358 ERR (CRYPT_FILE_NOTFOUND); |
|
1359 |
|
1360 ERR (CRYPT_PK_INVALID_TYPE); |
|
1361 ERR (CRYPT_PK_INVALID_SYSTEM); |
|
1362 ERR (CRYPT_PK_DUP); |
|
1363 ERR (CRYPT_PK_NOT_FOUND); |
|
1364 ERR (CRYPT_PK_INVALID_SIZE); |
|
1365 |
|
1366 ERR (CRYPT_INVALID_PRIME_SIZE); |
|
1367 } |
|
1368 |
|
1369 |
|
1370 void dsa_tests(void) |
|
1371 { |
|
1372 unsigned char msg[16], out[1024], out2[1024]; |
|
1373 unsigned long x, y; |
|
1374 int err, stat1, stat2; |
|
1375 dsa_key key, key2; |
|
1376 |
|
1377 /* make a random key */ |
|
1378 if ((err = dsa_make_key(&prng, find_prng("yarrow"), 20, 128, &key)) != CRYPT_OK) { |
|
1379 printf("Error making DSA key: %s\n", error_to_string(err)); |
|
1380 exit(-1); |
|
1381 } |
|
1382 printf("DSA Key Made\n"); |
|
1383 |
|
1384 /* verify it */ |
|
1385 if ((err = dsa_verify_key(&key, &stat1)) != CRYPT_OK) { |
|
1386 printf("Error verifying DSA key: %s\n", error_to_string(err)); |
|
1387 exit(-1); |
|
1388 } |
|
1389 printf("DSA key verification: %s\n", stat1 == 1 ? "passed" : "failed"); |
|
1390 if (stat1 == 0) exit(-1); |
|
1391 |
|
1392 /* sign the message */ |
|
1393 x = sizeof(out); |
|
1394 if ((err = dsa_sign_hash(msg, sizeof(msg), out, &x, &prng, find_prng("yarrow"), &key)) != CRYPT_OK) { |
|
1395 printf("Error signing with DSA key: %s\n", error_to_string(err)); |
|
1396 exit(-1); |
|
1397 } |
|
1398 printf("DSA 160/1024 signature is %lu bytes long\n", x); |
|
1399 |
|
1400 /* verify it once */ |
|
1401 if ((err = dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key)) != CRYPT_OK) { |
|
1402 printf("Error verifying with DSA key 1: %s\n", error_to_string(err)); |
|
1403 exit(-1); |
|
1404 } |
|
1405 |
|
1406 /* Modify and verify again */ |
|
1407 msg[0] ^= 1; |
|
1408 if ((err = dsa_verify_hash(out, x, msg, sizeof(msg), &stat2, &key)) != CRYPT_OK) { |
|
1409 printf("Error verifying with DSA key 2: %s\n", error_to_string(err)); |
|
1410 exit(-1); |
|
1411 } |
|
1412 msg[0] ^= 1; |
|
1413 printf("DSA Verification: %d, %d, %s\n", stat1, stat2, (stat1 == 1 && stat2 == 0) ? "passed" : "failed"); |
|
1414 if (!(stat1 == 1 && stat2 == 0)) exit(-1); |
|
1415 |
|
1416 /* test exporting it */ |
|
1417 x = sizeof(out2); |
|
1418 if ((err = dsa_export(out2, &x, PK_PRIVATE, &key)) != CRYPT_OK) { |
|
1419 printf("Error export PK_PRIVATE DSA key: %s\n", error_to_string(err)); |
|
1420 exit(-1); |
|
1421 } |
|
1422 printf("Exported PK_PRIVATE DSA key in %lu bytes\n", x); |
|
1423 if ((err = dsa_import(out2, x, &key2)) != CRYPT_OK) { |
|
1424 printf("Error importing PK_PRIVATE DSA key: %s\n", error_to_string(err)); |
|
1425 exit(-1); |
|
1426 } |
|
1427 /* verify a signature with it */ |
|
1428 if ((err = dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key2)) != CRYPT_OK) { |
|
1429 printf("Error verifying with DSA key 3: %s\n", error_to_string(err)); |
|
1430 exit(-1); |
|
1431 } |
|
1432 printf("PRIVATE Import Test: %s\n", stat1 == 1 ? "passed" : "failed"); |
|
1433 if (stat1 == 0) exit(-1); |
|
1434 dsa_free(&key2); |
|
1435 |
|
1436 /* export as public now */ |
|
1437 x = sizeof(out2); |
|
1438 if ((err = dsa_export(out2, &x, PK_PUBLIC, &key)) != CRYPT_OK) { |
|
1439 printf("Error export PK_PUBLIC DSA key: %s\n", error_to_string(err)); |
|
1440 exit(-1); |
|
1441 } |
|
1442 printf("Exported PK_PUBLIC DSA key in %lu bytes\n", x); |
|
1443 if ((err = dsa_import(out2, x, &key2)) != CRYPT_OK) { |
|
1444 printf("Error importing PK_PUBLIC DSA key: %s\n", error_to_string(err)); |
|
1445 exit(-1); |
|
1446 } |
|
1447 /* verify a signature with it */ |
|
1448 if ((err = dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key2)) != CRYPT_OK) { |
|
1449 printf("Error verifying with DSA key 4: %s\n", error_to_string(err)); |
|
1450 exit(-1); |
|
1451 } |
|
1452 printf("PUBLIC Import Test: %s\n", stat1 == 1 ? "passed" : "failed"); |
|
1453 if (stat1 == 0) exit(-1); |
|
1454 |
|
1455 dsa_free(&key2); |
|
1456 dsa_free(&key); |
|
1457 } |
|
1458 |
|
1459 #ifdef PKCS_1 |
|
1460 void pkcs1_test(void) |
|
1461 { |
|
1462 unsigned char buf[3][128]; |
|
1463 int err, res1, res2, res3, prng_idx, hash_idx; |
|
1464 unsigned long x, y, l1, l2, l3, i1, i2; |
|
1465 |
|
1466 /* get hash/prng */ |
|
1467 hash_idx = find_hash("sha1"); |
|
1468 prng_idx = find_prng("yarrow"); |
|
1469 |
|
1470 /* do many tests */ |
|
1471 for (x = 0; x < 10000; x++) { |
|
1472 zeromem(buf, sizeof(buf)); |
|
1473 |
|
1474 /* make a dummy message (of random length) */ |
|
1475 l3 = (rand() & 31) + 8; |
|
1476 for (y = 0; y < l3; y++) buf[0][y] = rand() & 255; |
|
1477 |
|
1478 /* encode it */ |
|
1479 l1 = sizeof(buf[1]); |
|
1480 if ((err = pkcs_1_oaep_encode(buf[0], l3, NULL, 0, 1024, hash_idx, prng_idx, &prng, buf[1], &l1)) != CRYPT_OK) { |
|
1481 printf("OAEP encode: %s\n", error_to_string(err)); |
|
1482 exit(-1); |
|
1483 } |
|
1484 |
|
1485 /* decode it */ |
|
1486 l2 = sizeof(buf[2]); |
15
|
1487 if ((err = pkcs_1_oaep_decode(buf[1], l1, NULL, 0, 1024, hash_idx, buf[2], &l2, &res1)) != CRYPT_OK) { |
3
|
1488 printf("OAEP decode: %s\n", error_to_string(err)); |
|
1489 exit(-1); |
|
1490 } |
|
1491 |
15
|
1492 if (res1 != 1 || l2 != l3 || memcmp(buf[2], buf[0], l3) != 0) { |
|
1493 printf("res == %d, Outsize == %lu, should have been %lu, msg contents follow.\n", res1, l2, l3); |
3
|
1494 printf("ORIGINAL:\n"); |
|
1495 for (x = 0; x < l3; x++) { |
|
1496 printf("%02x ", buf[0][x]); |
|
1497 } |
|
1498 printf("\nRESULT:\n"); |
|
1499 for (x = 0; x < l2; x++) { |
|
1500 printf("%02x ", buf[2][x]); |
|
1501 } |
|
1502 printf("\n\n"); |
|
1503 exit(-1); |
|
1504 } |
|
1505 |
|
1506 /* test PSS */ |
|
1507 l1 = sizeof(buf[1]); |
|
1508 if ((err = pkcs_1_pss_encode(buf[0], l3, l3>>2, hash_idx, prng_idx, &prng, 1024, buf[1], &l1)) != CRYPT_OK) { |
|
1509 printf("PSS encode: %s\n", error_to_string(err)); |
|
1510 exit(-1); |
|
1511 } |
|
1512 |
|
1513 if ((err = pkcs_1_pss_decode(buf[0], l3, buf[1], l1, l3>>2, hash_idx, 1024, &res1)) != CRYPT_OK) { |
|
1514 printf("PSS decode1: %s\n", error_to_string(err)); |
|
1515 exit(-1); |
|
1516 } |
|
1517 |
|
1518 buf[0][i1 = abs(rand()) % l3] ^= 1; |
|
1519 if ((err = pkcs_1_pss_decode(buf[0], l3, buf[1], l1, l3>>2, hash_idx, 1024, &res2)) != CRYPT_OK) { |
|
1520 printf("PSS decode2: %s\n", error_to_string(err)); |
|
1521 exit(-1); |
|
1522 } |
|
1523 |
|
1524 buf[0][i1] ^= 1; |
|
1525 buf[1][i2 = abs(rand()) % l1] ^= 1; |
|
1526 if ((err = pkcs_1_pss_decode(buf[0], l3, buf[1], l1, l3>>2, hash_idx, 1024, &res3)) != CRYPT_OK) { |
|
1527 printf("PSS decode3: %s\n", error_to_string(err)); |
|
1528 exit(-1); |
|
1529 } |
|
1530 |
|
1531 if (!(res1 == 1 && res2 == 0 && res3 == 0)) { |
|
1532 printf("PSS failed: %d, %d, %d, %lu\n", res1, res2, res3, l3); |
|
1533 exit(-1); |
|
1534 } |
|
1535 } |
|
1536 printf("PKCS #1: Passed\n"); |
|
1537 } |
|
1538 |
|
1539 #endif /* PKCS_1 */ |
|
1540 |
|
1541 int |
|
1542 main (void) |
|
1543 { |
|
1544 #ifdef SONY_PS2 |
|
1545 TIMER_Init (); |
|
1546 #endif |
|
1547 srand(time(NULL)); |
|
1548 |
|
1549 register_all_algs (); |
|
1550 |
|
1551 if ((errnum = yarrow_start (&prng)) != CRYPT_OK) { |
|
1552 printf ("yarrow_start: %s\n", error_to_string (errnum)); |
|
1553 } |
|
1554 if ((errnum = yarrow_add_entropy ((unsigned char *)"hello", 5, &prng)) != CRYPT_OK) { |
|
1555 printf ("yarrow_add_entropy: %s\n", error_to_string (errnum)); |
|
1556 } |
|
1557 if ((errnum = yarrow_ready (&prng)) != CRYPT_OK) { |
|
1558 printf ("yarrow_ready: %s\n", error_to_string (errnum)); |
|
1559 } |
|
1560 |
|
1561 printf (crypt_build_settings); |
|
1562 test_errs (); |
|
1563 |
|
1564 #ifdef HMAC |
|
1565 printf ("HMAC: %s\n", hmac_test () == CRYPT_OK ? "passed" : "failed"); |
|
1566 if (hmac_test() != CRYPT_OK) exit(EXIT_FAILURE); |
|
1567 #endif |
|
1568 |
|
1569 #ifdef OMAC |
|
1570 printf ("OMAC: %s\n", omac_test () == CRYPT_OK ? "passed" : "failed"); |
|
1571 if (omac_test() != CRYPT_OK) exit(EXIT_FAILURE); |
|
1572 #endif |
|
1573 |
|
1574 #ifdef PMAC |
|
1575 printf ("PMAC: %s\n", pmac_test () == CRYPT_OK ? "passed" : "failed"); |
|
1576 if (pmac_test() != CRYPT_OK) exit(EXIT_FAILURE); |
|
1577 #endif |
|
1578 |
|
1579 #ifdef EAX_MODE |
|
1580 printf ("EAX : %s\n", eax_test () == CRYPT_OK ? "passed" : "failed"); |
|
1581 if (eax_test() != CRYPT_OK) exit(EXIT_FAILURE); |
|
1582 #endif |
|
1583 |
|
1584 #ifdef OCB_MODE |
|
1585 printf ("OCB : %s\n", ocb_test () == CRYPT_OK ? "passed" : "failed"); |
|
1586 if (ocb_test() != CRYPT_OK) exit(EXIT_FAILURE); |
|
1587 #endif |
|
1588 |
|
1589 store_tests (); |
|
1590 cipher_tests (); |
|
1591 hash_tests (); |
|
1592 |
|
1593 #ifdef PKCS_1 |
|
1594 pkcs1_test(); |
|
1595 #endif |
|
1596 |
|
1597 ecb_tests (); |
|
1598 cbc_tests (); |
|
1599 ctr_tests (); |
|
1600 ofb_tests (); |
|
1601 cfb_tests (); |
|
1602 |
|
1603 rng_tests (); |
|
1604 test_prime(); |
|
1605 |
|
1606 dsa_tests(); |
|
1607 rsa_test (); |
|
1608 pad_test (); |
|
1609 ecc_tests (); |
|
1610 dh_tests (); |
|
1611 |
|
1612 base64_test (); |
|
1613 |
|
1614 time_ecb (); |
|
1615 time_hash (); |
|
1616 |
|
1617 #ifdef SONY_PS2 |
|
1618 TIMER_Shutdown (); |
|
1619 #endif |
|
1620 |
|
1621 return 0; |
|
1622 } |