comparison demos/test.c @ 0:d7da3b1e1540 libtomcrypt

put back the 0.95 makefile which was inadvertently merged over
author Matt Johnston <matt@ucc.asn.au>
date Mon, 31 May 2004 18:21:40 +0000
parents
children 6362d3854bb4
comparison
equal deleted inserted replaced
-1:000000000000 0:d7da3b1e1540
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 GF
1206 void
1207 gf_tests (void)
1208 {
1209 gf_int a, b, c, d;
1210 int n;
1211 unsigned char buf[1024];
1212
1213 printf ("GF tests\n");
1214 gf_zero (a);
1215 gf_zero (b);
1216 gf_zero (c);
1217 gf_zero (d);
1218
1219 /* a == 0x18000000b */
1220 a[1] = 1;
1221 a[0] = 0x8000000bUL;
1222
1223 /* b == 0x012345678 */
1224 b[0] = 0x012345678UL;
1225
1226 /* find 1/b mod a */
1227 gf_invmod (b, a, c);
1228
1229 /* find 1/1/b mod a */
1230 gf_invmod (c, a, d);
1231
1232 /* display them */
1233 printf (" %08lx %08lx\n", c[0], d[0]);
1234
1235 /* store as binary string */
1236 n = gf_size (a);
1237 printf (" a takes %d bytes\n", n);
1238 gf_toraw (a, buf);
1239 gf_readraw (a, buf, n);
1240 printf (" a == %08lx%08lx\n", a[1], a[0]);
1241
1242 /* primality testing */
1243 gf_zero (a);
1244 a[0] = 0x169;
1245 printf (" GF prime: %s, ", gf_is_prime (a) ? "passed" : "failed");
1246 a[0] = 0x168;
1247 printf (" %s\n", gf_is_prime (a) ? "failed" : "passed");
1248
1249 /* test sqrt code */
1250 gf_zero (a);
1251 a[1] = 0x00000001;
1252 a[0] = 0x8000000bUL;
1253 gf_zero (b);
1254 b[0] = 0x12345678UL;
1255
1256 gf_sqrt (b, a, c);
1257 gf_mulmod (c, c, a, b);
1258 printf (" (%08lx)^2 = %08lx (mod %08lx%08lx) \n", c[0], b[0], a[1], a[0]);
1259 }
1260 #else
1261 void
1262 gf_tests (void)
1263 {
1264 printf ("GF not compiled in\n");
1265 }
1266 #endif
1267
1268 #ifdef MPI
1269 void
1270 test_prime (void)
1271 {
1272 char buf[1024];
1273 mp_int a;
1274 int x;
1275
1276 /* make a 1024 bit prime */
1277 mp_init (&a);
1278 rand_prime (&a, 128*8, &prng, find_prng ("yarrow"));
1279
1280 /* dump it */
1281 mp_todecimal (&a, buf);
1282 printf ("1024-bit prime:\n");
1283 for (x = 0; x < (int) strlen (buf);) {
1284 printf ("%c", buf[x]);
1285 if (!(++x % 60))
1286 printf ("\\ \n");
1287 }
1288 printf ("\n\n");
1289
1290 mp_clear (&a);
1291 }
1292 #else
1293 void
1294 test_prime (void)
1295 {
1296 printf ("MPI not compiled in\n");
1297 }
1298 #endif
1299
1300 void
1301 register_all_algs (void)
1302 {
1303 #ifdef RIJNDAEL
1304 register_cipher (&aes_desc);
1305 #endif
1306 #ifdef BLOWFISH
1307 register_cipher (&blowfish_desc);
1308 #endif
1309 #ifdef XTEA
1310 register_cipher (&xtea_desc);
1311 #endif
1312 #ifdef RC5
1313 register_cipher (&rc5_desc);
1314 #endif
1315 #ifdef RC6
1316 register_cipher (&rc6_desc);
1317 #endif
1318 #ifdef SAFERP
1319 register_cipher (&saferp_desc);
1320 #endif
1321 #ifdef TWOFISH
1322 register_cipher (&twofish_desc);
1323 #endif
1324 #ifdef SAFER
1325 register_cipher (&safer_k64_desc);
1326 register_cipher (&safer_sk64_desc);
1327 register_cipher (&safer_k128_desc);
1328 register_cipher (&safer_sk128_desc);
1329 #endif
1330 #ifdef RC2
1331 register_cipher (&rc2_desc);
1332 #endif
1333 #ifdef DES
1334 register_cipher (&des_desc);
1335 register_cipher (&des3_desc);
1336 #endif
1337 #ifdef CAST5
1338 register_cipher (&cast5_desc);
1339 #endif
1340 #ifdef NOEKEON
1341 register_cipher (&noekeon_desc);
1342 #endif
1343 #ifdef SKIPJACK
1344 register_cipher (&skipjack_desc);
1345 #endif
1346 register_cipher (&null_desc);
1347
1348 #ifdef TIGER
1349 register_hash (&tiger_desc);
1350 #endif
1351 #ifdef MD2
1352 register_hash (&md2_desc);
1353 #endif
1354 #ifdef MD4
1355 register_hash (&md4_desc);
1356 #endif
1357 #ifdef MD5
1358 register_hash (&md5_desc);
1359 #endif
1360 #ifdef SHA1
1361 register_hash (&sha1_desc);
1362 #endif
1363 #ifdef SHA256
1364 register_hash (&sha256_desc);
1365 #endif
1366 #ifdef SHA224
1367 register_hash (&sha224_desc);
1368 #endif
1369 #ifdef SHA384
1370 register_hash (&sha384_desc);
1371 #endif
1372 #ifdef SHA512
1373 register_hash (&sha512_desc);
1374 #endif
1375 #ifdef RIPEMD128
1376 register_hash (&rmd128_desc);
1377 #endif
1378 #ifdef RIPEMD160
1379 register_hash (&rmd160_desc);
1380 #endif
1381 #ifdef WHIRLPOOL
1382 register_hash (&whirlpool_desc);
1383 #endif
1384
1385 #ifdef YARROW
1386 register_prng (&yarrow_desc);
1387 #endif
1388 #ifdef SPRNG
1389 register_prng (&sprng_desc);
1390 #endif
1391 }
1392
1393 #ifdef KR
1394 void
1395 kr_display (pk_key * kr)
1396 {
1397 static const char *sys[] = { "NON-KEY", "RSA", "DH", "ECC" };
1398 static const char *type[] = { "PRIVATE", "PUBLIC", "PRIVATE_OPTIMIZED" };
1399
1400 while (kr->system != NON_KEY) {
1401 printf ("CRC [%08lx], System [%10s], Type [%20s], %s, %s, %s\n", kr->ID,
1402 sys[kr->system], type[kr->key_type], kr->name, kr->email,
1403 kr->description);
1404 kr = kr->next;
1405 }
1406 printf ("\n");
1407 }
1408
1409 void
1410 kr_test_makekeys (pk_key ** kr)
1411 {
1412 if ((errnum = kr_init (kr)) != CRYPT_OK) {
1413 printf ("KR init error %s\n", error_to_string (errnum));
1414 exit (-1);
1415 }
1416
1417 /* make a DH key */
1418 printf ("KR: Making DH key...\n");
1419 if ((errnum =
1420 kr_make_key (*kr, &prng, find_prng ("yarrow"), DH_KEY, 128, "dhkey",
1421 "[email protected]", "dhkey one")) != CRYPT_OK) {
1422 printf ("Make key error: %s\n", error_to_string (errnum));
1423 exit (-1);
1424 }
1425
1426 /* make a ECC key */
1427 printf ("KR: Making ECC key...\n");
1428 if ((errnum =
1429 kr_make_key (*kr, &prng, find_prng ("yarrow"), ECC_KEY, 20, "ecckey",
1430 "[email protected]", "ecckey one")) != CRYPT_OK) {
1431 printf ("Make key error: %s\n", error_to_string (errnum));
1432 exit (-1);
1433 }
1434
1435 /* make a RSA key */
1436 printf ("KR: Making RSA key...\n");
1437 if ((errnum =
1438 kr_make_key (*kr, &prng, find_prng ("yarrow"), RSA_KEY, 128, "rsakey",
1439 "[email protected]", "rsakey one")) != CRYPT_OK) {
1440 printf ("Make key error: %s\n", error_to_string (errnum));
1441 exit (-1);
1442 }
1443
1444 }
1445
1446 void
1447 kr_test (void)
1448 {
1449 pk_key *kr, *_kr;
1450 unsigned char buf[8192], buf2[8192], buf3[8192];
1451 unsigned long len;
1452 int i, j, stat;
1453 #ifndef NO_FILE
1454 FILE *f;
1455 #endif
1456
1457 kr_test_makekeys (&kr);
1458
1459 printf ("The original list:\n");
1460 kr_display (kr);
1461
1462 for (i = 0; i < 3; i++) {
1463 len = sizeof (buf);
1464 if ((errnum = kr_export (kr, kr->ID, kr->key_type, buf, &len)) != CRYPT_OK) {
1465 printf ("Error exporting key %d, %s\n", i, error_to_string (errnum));
1466 exit (-1);
1467 }
1468 printf ("Exported key was: %lu bytes\n", len);
1469 if ((errnum = kr_del (&kr, kr->ID)) != CRYPT_OK) {
1470 printf ("Error deleting key %d, %s\n", i, error_to_string (errnum));
1471 exit (-1);
1472 }
1473 kr_display (kr);
1474 if ((errnum = kr_import (kr, buf, len)) != CRYPT_OK) {
1475 printf ("Error importing key %d, %s\n", i, error_to_string (errnum));
1476 exit (-1);
1477 }
1478 kr_display (kr);
1479 }
1480
1481 for (i = 0; i < 3; i++) {
1482 len = sizeof (buf);
1483 if ((errnum = kr_export (kr, kr->ID, PK_PUBLIC, buf, &len)) != CRYPT_OK) {
1484 printf ("Error exporting key %d, %s\n", i, error_to_string (errnum));
1485 exit (-1);
1486 }
1487 printf ("Exported key was: %lu bytes\n", len);
1488 if ((errnum = kr_del (&kr, kr->ID)) != CRYPT_OK) {
1489 printf ("Error deleting key %d, %s\n", i, error_to_string (errnum));
1490 exit (-1);
1491 }
1492 kr_display (kr);
1493 if ((errnum = kr_import (kr, buf, len)) != CRYPT_OK) {
1494 printf ("Error importing key %d, %s\n", i, error_to_string (errnum));
1495 exit (-1);
1496 }
1497 kr_display (kr);
1498 }
1499
1500 if ((errnum = kr_clear (&kr)) != CRYPT_OK) {
1501 printf ("Error clearing ring: %s\n", error_to_string (errnum));
1502 exit (-1);
1503 }
1504
1505
1506 /* TEST output to file */
1507 #ifndef NO_FILE
1508
1509 if ((errnum = kr_init (&kr)) != CRYPT_OK) {
1510 printf ("KR init error %s\n", error_to_string (errnum));
1511 exit (-1);
1512 }
1513 kr_test_makekeys (&kr);
1514
1515 /* save to file */
1516 f = fopen ("ring.dat", "wb");
1517 if ((errnum = kr_save (kr, f, NULL)) != CRYPT_OK) {
1518 printf ("kr_save error %s\n", error_to_string (errnum));
1519 exit (-1);
1520 }
1521 fclose (f);
1522
1523 /* delete and load */
1524 if ((errnum = kr_clear (&kr)) != CRYPT_OK) {
1525 printf ("clear error: %s\n", error_to_string (errnum));
1526 exit (-1);
1527 }
1528
1529 f = fopen ("ring.dat", "rb");
1530 if ((errnum = kr_load (&kr, f, NULL)) != CRYPT_OK) {
1531 printf ("kr_load error %s\n", error_to_string (errnum));
1532 exit (-1);
1533 }
1534 fclose (f);
1535 remove ("ring.dat");
1536 printf ("After load and save...\n");
1537 kr_display (kr);
1538
1539 if ((errnum = kr_clear (&kr)) != CRYPT_OK) {
1540 printf ("clear error: %s\n", error_to_string (errnum));
1541 exit (-1);
1542 }
1543 #endif
1544
1545 /* test the packet encryption/sign stuff */
1546 for (i = 0; i < 32; i++)
1547 buf[i] = i;
1548 kr_test_makekeys (&kr);
1549 _kr = kr;
1550 for (i = 0; i < 3; i++) {
1551 printf ("Testing a key with system %d, type %d:\t", _kr->system,
1552 _kr->key_type);
1553 len = sizeof (buf2);
1554 if ((errnum =
1555 kr_encrypt_key (kr, _kr->ID, buf, 16, buf2, &len, &prng,
1556 find_prng ("yarrow"),
1557 find_hash ("md5"))) != CRYPT_OK) {
1558 printf ("Encrypt error, %d, %s\n", i, error_to_string (errnum));
1559 exit (-1);
1560 }
1561 len = sizeof (buf3);
1562 if ((errnum = kr_decrypt_key (kr, buf2, buf3, &len)) != CRYPT_OK) {
1563 printf ("decrypt error, %d, %s\n", i, error_to_string (errnum));
1564 exit (-1);
1565 }
1566 if (len != 16 || memcmp (buf3, buf, 16)) {
1567 printf ("kr_decrypt_key failed, %i, %lu\n", i, len);
1568 exit (-1);
1569 }
1570 printf ("kr_encrypt_key passed, ");
1571
1572 len = sizeof (buf2);
1573 if ((errnum =
1574 kr_sign_hash (kr, _kr->ID, buf, 32, buf2, &len, &prng,
1575 find_prng ("yarrow"))) != CRYPT_OK) {
1576 printf ("kr_sign_hash failed, %i, %s\n", i, error_to_string (errnum));
1577 exit (-1);
1578 }
1579 printf ("kr_sign_hash: ");
1580 if ((errnum = kr_verify_hash (kr, buf2, buf, 32, &stat)) != CRYPT_OK) {
1581 printf ("kr_sign_hash failed, %i, %s\n", i, error_to_string (errnum));
1582 exit (-1);
1583 }
1584 printf ("%s, ", stat ? "passed" : "failed");
1585 buf[15] ^= 1;
1586 if ((errnum = kr_verify_hash (kr, buf2, buf, 32, &stat)) != CRYPT_OK) {
1587 printf ("kr_sign_hash failed, %i, %s\n", i, error_to_string (errnum));
1588 exit (-1);
1589 }
1590 printf ("%s\n", (!stat) ? "passed" : "failed");
1591 buf[15] ^= 1;
1592
1593 len = sizeof (buf);
1594 if ((errnum =
1595 kr_fingerprint (kr, _kr->ID, find_hash ("sha1"), buf,
1596 &len)) != CRYPT_OK) {
1597 printf ("kr_fingerprint failed, %i, %lu\n", i, len);
1598 exit (-1);
1599 }
1600 printf ("Fingerprint: ");
1601 for (j = 0; j < 20; j++) {
1602 printf ("%02x", buf[j]);
1603 if (j < 19)
1604 printf (":");
1605 }
1606 printf ("\n\n");
1607
1608 _kr = _kr->next;
1609 }
1610
1611 /* Test encrypting/decrypting to a public key */
1612 /* first dump the other two keys */
1613 kr_del (&kr, kr->ID);
1614 kr_del (&kr, kr->ID);
1615 kr_display (kr);
1616
1617 /* now export it as public and private */
1618 len = sizeof (buf);
1619 if ((errnum = kr_export (kr, kr->ID, PK_PUBLIC, buf, &len)) != CRYPT_OK) {
1620 printf ("Error exporting key %d, %s\n", i, error_to_string (errnum));
1621 exit (-1);
1622 }
1623
1624 /* check boundaries */
1625 memset (buf + len, 0, sizeof (buf) - len);
1626
1627 len = sizeof (buf2);
1628 if ((errnum = kr_export (kr, kr->ID, PK_PRIVATE, buf2, &len)) != CRYPT_OK) {
1629 printf ("Error exporting key %s\n", error_to_string (errnum));
1630 exit (-1);
1631 }
1632
1633 /* check boundaries */
1634 memset (buf2 + len, 0, sizeof (buf2) - len);
1635
1636 /* delete the key and import the public */
1637 kr_clear (&kr);
1638 kr_init (&kr);
1639 kr_display (kr);
1640 if ((errnum = kr_import (kr, buf, len)) != CRYPT_OK) {
1641 printf ("Error importing key %s\n", error_to_string (errnum));
1642 exit (-1);
1643 }
1644 kr_display (kr);
1645
1646 /* now encrypt a buffer */
1647 for (i = 0; i < 16; i++)
1648 buf[i] = i;
1649 len = sizeof (buf3);
1650 if ((errnum =
1651 kr_encrypt_key (kr, kr->ID, buf, 16, buf3, &len, &prng,
1652 find_prng ("yarrow"),
1653 find_hash ("md5"))) != CRYPT_OK) {
1654 printf ("Encrypt error, %d, %s\n", i, error_to_string (errnum));
1655 exit (-1);
1656 }
1657
1658 /* now delete the key and import the private one */
1659 kr_clear (&kr);
1660 kr_init (&kr);
1661 kr_display (kr);
1662 if ((errnum = kr_import (kr, buf2, len)) != CRYPT_OK) {
1663 printf ("Error importing key %s\n", error_to_string (errnum));
1664 exit (-1);
1665 }
1666 kr_display (kr);
1667
1668 /* now decrypt */
1669 len = sizeof (buf2);
1670 if ((errnum = kr_decrypt_key (kr, buf3, buf2, &len)) != CRYPT_OK) {
1671 printf ("decrypt error, %s\n", error_to_string (errnum));
1672 exit (-1);
1673 }
1674
1675 printf ("KR encrypt to public, decrypt with private: ");
1676 if (len == 16 && !memcmp (buf2, buf, 16)) {
1677 printf ("passed\n");
1678 } else {
1679 printf ("failed\n");
1680 }
1681
1682 kr_clear (&kr);
1683 }
1684 #endif
1685
1686 void
1687 test_errs (void)
1688 {
1689 #define ERR(x) printf("%25s => %s\n", #x, error_to_string(x));
1690
1691 ERR (CRYPT_OK);
1692 ERR (CRYPT_ERROR);
1693
1694 ERR (CRYPT_INVALID_KEYSIZE);
1695 ERR (CRYPT_INVALID_ROUNDS);
1696 ERR (CRYPT_FAIL_TESTVECTOR);
1697
1698 ERR (CRYPT_BUFFER_OVERFLOW);
1699 ERR (CRYPT_INVALID_PACKET);
1700
1701 ERR (CRYPT_INVALID_PRNGSIZE);
1702 ERR (CRYPT_ERROR_READPRNG);
1703
1704 ERR (CRYPT_INVALID_CIPHER);
1705 ERR (CRYPT_INVALID_HASH);
1706 ERR (CRYPT_INVALID_PRNG);
1707
1708 ERR (CRYPT_MEM);
1709
1710 ERR (CRYPT_PK_TYPE_MISMATCH);
1711 ERR (CRYPT_PK_NOT_PRIVATE);
1712
1713 ERR (CRYPT_INVALID_ARG);
1714 ERR (CRYPT_FILE_NOTFOUND);
1715
1716 ERR (CRYPT_PK_INVALID_TYPE);
1717 ERR (CRYPT_PK_INVALID_SYSTEM);
1718 ERR (CRYPT_PK_DUP);
1719 ERR (CRYPT_PK_NOT_FOUND);
1720 ERR (CRYPT_PK_INVALID_SIZE);
1721
1722 ERR (CRYPT_INVALID_PRIME_SIZE);
1723 }
1724
1725
1726 void dsa_tests(void)
1727 {
1728 unsigned char msg[16], out[1024], out2[1024];
1729 unsigned long x, y;
1730 int err, stat1, stat2;
1731 dsa_key key, key2;
1732
1733 /* make a random key */
1734 if ((err = dsa_make_key(&prng, find_prng("yarrow"), 20, 128, &key)) != CRYPT_OK) {
1735 printf("Error making DSA key: %s\n", error_to_string(err));
1736 exit(-1);
1737 }
1738 printf("DSA Key Made\n");
1739
1740 /* verify it */
1741 if ((err = dsa_verify_key(&key, &stat1)) != CRYPT_OK) {
1742 printf("Error verifying DSA key: %s\n", error_to_string(err));
1743 exit(-1);
1744 }
1745 printf("DSA key verification: %s\n", stat1 == 1 ? "passed" : "failed");
1746 if (stat1 == 0) exit(-1);
1747
1748 /* sign the message */
1749 x = sizeof(out);
1750 if ((err = dsa_sign_hash(msg, sizeof(msg), out, &x, &prng, find_prng("yarrow"), &key)) != CRYPT_OK) {
1751 printf("Error signing with DSA key: %s\n", error_to_string(err));
1752 exit(-1);
1753 }
1754 printf("DSA 160/1024 signature is %lu bytes long\n", x);
1755
1756 /* verify it once */
1757 if ((err = dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key)) != CRYPT_OK) {
1758 printf("Error verifying with DSA key 1: %s\n", error_to_string(err));
1759 exit(-1);
1760 }
1761
1762 /* Modify and verify again */
1763 msg[0] ^= 1;
1764 if ((err = dsa_verify_hash(out, x, msg, sizeof(msg), &stat2, &key)) != CRYPT_OK) {
1765 printf("Error verifying with DSA key 2: %s\n", error_to_string(err));
1766 exit(-1);
1767 }
1768 msg[0] ^= 1;
1769 printf("DSA Verification: %d, %d, %s\n", stat1, stat2, (stat1 == 1 && stat2 == 0) ? "passed" : "failed");
1770 if (!(stat1 == 1 && stat2 == 0)) exit(-1);
1771
1772 /* test exporting it */
1773 x = sizeof(out2);
1774 if ((err = dsa_export(out2, &x, PK_PRIVATE, &key)) != CRYPT_OK) {
1775 printf("Error export PK_PRIVATE DSA key: %s\n", error_to_string(err));
1776 exit(-1);
1777 }
1778 printf("Exported PK_PRIVATE DSA key in %lu bytes\n", x);
1779 if ((err = dsa_import(out2, x, &key2)) != CRYPT_OK) {
1780 printf("Error importing PK_PRIVATE DSA key: %s\n", error_to_string(err));
1781 exit(-1);
1782 }
1783 /* verify a signature with it */
1784 if ((err = dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key2)) != CRYPT_OK) {
1785 printf("Error verifying with DSA key 3: %s\n", error_to_string(err));
1786 exit(-1);
1787 }
1788 printf("PRIVATE Import Test: %s\n", stat1 == 1 ? "passed" : "failed");
1789 if (stat1 == 0) exit(-1);
1790 dsa_free(&key2);
1791
1792 /* export as public now */
1793 x = sizeof(out2);
1794 if ((err = dsa_export(out2, &x, PK_PUBLIC, &key)) != CRYPT_OK) {
1795 printf("Error export PK_PUBLIC DSA key: %s\n", error_to_string(err));
1796 exit(-1);
1797 }
1798 printf("Exported PK_PUBLIC DSA key in %lu bytes\n", x);
1799 if ((err = dsa_import(out2, x, &key2)) != CRYPT_OK) {
1800 printf("Error importing PK_PUBLIC DSA key: %s\n", error_to_string(err));
1801 exit(-1);
1802 }
1803 /* verify a signature with it */
1804 if ((err = dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key2)) != CRYPT_OK) {
1805 printf("Error verifying with DSA key 4: %s\n", error_to_string(err));
1806 exit(-1);
1807 }
1808 printf("PUBLIC Import Test: %s\n", stat1 == 1 ? "passed" : "failed");
1809 if (stat1 == 0) exit(-1);
1810
1811 dsa_free(&key2);
1812 dsa_free(&key);
1813 }
1814
1815 #ifdef PKCS_1
1816 void pkcs1_test(void)
1817 {
1818 unsigned char buf[3][128];
1819 int err, res1, res2, res3, prng_idx, hash_idx;
1820 unsigned long x, y, l1, l2, l3, i1, i2;
1821
1822 /* get hash/prng */
1823 hash_idx = find_hash("sha1");
1824 prng_idx = find_prng("yarrow");
1825
1826 /* do many tests */
1827 for (x = 0; x < 10000; x++) {
1828 zeromem(buf, sizeof(buf));
1829
1830 /* make a dummy message (of random length) */
1831 l3 = (rand() & 31) + 8;
1832 for (y = 0; y < l3; y++) buf[0][y] = rand() & 255;
1833
1834 /* encode it */
1835 l1 = sizeof(buf[1]);
1836 if ((err = pkcs_1_oaep_encode(buf[0], l3, NULL, 0, 1024, hash_idx, prng_idx, &prng, buf[1], &l1)) != CRYPT_OK) {
1837 printf("OAEP encode: %s\n", error_to_string(err));
1838 exit(-1);
1839 }
1840
1841 /* decode it */
1842 l2 = sizeof(buf[2]);
1843 if ((err = pkcs_1_oaep_decode(buf[1], l1, NULL, 0, 1024, hash_idx, buf[2], &l2)) != CRYPT_OK) {
1844 printf("OAEP decode: %s\n", error_to_string(err));
1845 exit(-1);
1846 }
1847
1848 if (l2 != l3 || memcmp(buf[2], buf[0], l3) != 0) {
1849 printf("Outsize == %lu, should have been %lu, msg contents follow.\n", l2, l3);
1850 printf("ORIGINAL:\n");
1851 for (x = 0; x < l3; x++) {
1852 printf("%02x ", buf[0][x]);
1853 }
1854 printf("\nRESULT:\n");
1855 for (x = 0; x < l2; x++) {
1856 printf("%02x ", buf[2][x]);
1857 }
1858 printf("\n\n");
1859 exit(-1);
1860 }
1861
1862 /* test PSS */
1863 l1 = sizeof(buf[1]);
1864 if ((err = pkcs_1_pss_encode(buf[0], l3, l3>>2, hash_idx, prng_idx, &prng, 1024, buf[1], &l1)) != CRYPT_OK) {
1865 printf("PSS encode: %s\n", error_to_string(err));
1866 exit(-1);
1867 }
1868
1869 if ((err = pkcs_1_pss_decode(buf[0], l3, buf[1], l1, l3>>2, hash_idx, 1024, &res1)) != CRYPT_OK) {
1870 printf("PSS decode1: %s\n", error_to_string(err));
1871 exit(-1);
1872 }
1873
1874 buf[0][i1 = abs(rand()) % l3] ^= 1;
1875 if ((err = pkcs_1_pss_decode(buf[0], l3, buf[1], l1, l3>>2, hash_idx, 1024, &res2)) != CRYPT_OK) {
1876 printf("PSS decode2: %s\n", error_to_string(err));
1877 exit(-1);
1878 }
1879
1880 buf[0][i1] ^= 1;
1881 buf[1][i2 = abs(rand()) % l1] ^= 1;
1882 if ((err = pkcs_1_pss_decode(buf[0], l3, buf[1], l1, l3>>2, hash_idx, 1024, &res3)) != CRYPT_OK) {
1883 printf("PSS decode3: %s\n", error_to_string(err));
1884 exit(-1);
1885 }
1886
1887 if (!(res1 == 1 && res2 == 0 && res3 == 0)) {
1888 printf("PSS failed: %d, %d, %d, %lu\n", res1, res2, res3, l3);
1889 exit(-1);
1890 }
1891 }
1892 printf("PKCS #1: Passed\n");
1893 }
1894
1895 #endif /* PKCS_1 */
1896
1897 int
1898 main (void)
1899 {
1900 #ifdef SONY_PS2
1901 TIMER_Init ();
1902 #endif
1903 srand(time(NULL));
1904
1905 register_all_algs ();
1906
1907 if ((errnum = yarrow_start (&prng)) != CRYPT_OK) {
1908 printf ("yarrow_start: %s\n", error_to_string (errnum));
1909 }
1910 if ((errnum = yarrow_add_entropy ((unsigned char *)"hello", 5, &prng)) != CRYPT_OK) {
1911 printf ("yarrow_add_entropy: %s\n", error_to_string (errnum));
1912 }
1913 if ((errnum = yarrow_ready (&prng)) != CRYPT_OK) {
1914 printf ("yarrow_ready: %s\n", error_to_string (errnum));
1915 }
1916
1917 printf (crypt_build_settings);
1918 test_errs ();
1919
1920 #ifdef HMAC
1921 printf ("HMAC: %s\n", hmac_test () == CRYPT_OK ? "passed" : "failed");
1922 if (hmac_test() != CRYPT_OK) exit(EXIT_FAILURE);
1923 #endif
1924
1925 #ifdef OMAC
1926 printf ("OMAC: %s\n", omac_test () == CRYPT_OK ? "passed" : "failed");
1927 if (omac_test() != CRYPT_OK) exit(EXIT_FAILURE);
1928 #endif
1929
1930 #ifdef PMAC
1931 printf ("PMAC: %s\n", pmac_test () == CRYPT_OK ? "passed" : "failed");
1932 if (pmac_test() != CRYPT_OK) exit(EXIT_FAILURE);
1933 #endif
1934
1935 #ifdef EAX_MODE
1936 printf ("EAX : %s\n", eax_test () == CRYPT_OK ? "passed" : "failed");
1937 if (eax_test() != CRYPT_OK) exit(EXIT_FAILURE);
1938 #endif
1939
1940 #ifdef OCB_MODE
1941 printf ("OCB : %s\n", ocb_test () == CRYPT_OK ? "passed" : "failed");
1942 if (ocb_test() != CRYPT_OK) exit(EXIT_FAILURE);
1943 #endif
1944
1945 store_tests ();
1946 cipher_tests ();
1947 hash_tests ();
1948
1949 #ifdef PKCS_1
1950 pkcs1_test();
1951 #endif
1952
1953 ecb_tests ();
1954 cbc_tests ();
1955 ctr_tests ();
1956 ofb_tests ();
1957 cfb_tests ();
1958
1959 rng_tests ();
1960 test_prime();
1961
1962 #ifdef KR
1963 kr_test ();
1964 #endif
1965 dsa_tests();
1966 rsa_test ();
1967 pad_test ();
1968 ecc_tests ();
1969 dh_tests ();
1970
1971 gf_tests ();
1972 base64_test ();
1973
1974 time_ecb ();
1975 time_hash ();
1976
1977 #ifdef SONY_PS2
1978 TIMER_Shutdown ();
1979 #endif
1980
1981 return 0;
1982 }