comparison testprof/pkcs_1_test.c @ 209:39d5d58461d6 libtomcrypt-orig LTC_1.05

Import of libtomcrypt 1.05
author Matt Johnston <matt@ucc.asn.au>
date Wed, 06 Jul 2005 03:53:40 +0000
parents 1c15b283127b
children
comparison
equal deleted inserted replaced
191:1c15b283127b 209:39d5d58461d6
12 /* get hash/prng */ 12 /* get hash/prng */
13 hash_idx = find_hash("sha1"); 13 hash_idx = find_hash("sha1");
14 prng_idx = find_prng("yarrow"); 14 prng_idx = find_prng("yarrow");
15 15
16 if (hash_idx == -1 || prng_idx == -1) { 16 if (hash_idx == -1 || prng_idx == -1) {
17 printf("pkcs_1 tests require sha1/yarrow"); 17 fprintf(stderr, "pkcs_1 tests require sha1/yarrow");
18 return 1; 18 return 1;
19 } 19 }
20 20
21 /* do many tests */ 21 /* do many tests */
22 for (x = 0; x < 100; x++) { 22 for (x = 0; x < 100; x++) {
26 l3 = (rand() & 31) + 8; 26 l3 = (rand() & 31) + 8;
27 for (y = 0; y < l3; y++) buf[0][y] = rand() & 255; 27 for (y = 0; y < l3; y++) buf[0][y] = rand() & 255;
28 28
29 /* random modulus len (v1.5 must be multiple of 8 though arbitrary sizes seem to work) */ 29 /* random modulus len (v1.5 must be multiple of 8 though arbitrary sizes seem to work) */
30 modlen = 800 + 8 * (abs(rand()) % 28); 30 modlen = 800 + 8 * (abs(rand()) % 28);
31
32 /* PKCS v1.5 testing (encryption) */
33 l1 = sizeof(buf[1]);
34 DO(pkcs_1_v15_es_encode(buf[0], l3, modlen, &yarrow_prng, prng_idx, buf[1], &l1));
35 DO(pkcs_1_v15_es_decode(buf[1], l1, modlen, buf[2], l3, &res1));
36 if (res1 != 1 || memcmp(buf[0], buf[2], l3)) {
37 printf("pkcs v1.5 encrypt failed %d, %lu, %lu ", res1, l1, l3);
38 return 1;
39 }
40
41 /* PKCS v1.5 testing (signatures) */
42 l1 = sizeof(buf[1]);
43 DO(pkcs_1_v15_sa_encode(buf[0], l3, hash_idx, modlen, buf[1], &l1));
44 DO(pkcs_1_v15_sa_decode(buf[0], l3, buf[1], l1, hash_idx, modlen, &res1));
45 buf[0][i1 = abs(rand()) % l3] ^= 1;
46 DO(pkcs_1_v15_sa_decode(buf[0], l3, buf[1], l1, hash_idx, modlen, &res2));
47 buf[0][i1] ^= 1;
48 buf[1][i2 = abs(rand()) % l1] ^= 1;
49 DO(pkcs_1_v15_sa_decode(buf[0], l3, buf[1], l1, hash_idx, modlen, &res3));
50
51 if (!(res1 == 1 && res2 == 0 && res3 == 0)) {
52 printf("pkcs v1.5 sign failed %d %d %d ", res1, res2, res3);
53 return 1;
54 }
55 31
56 /* pick a random lparam len [0..16] */ 32 /* pick a random lparam len [0..16] */
57 lparamlen = abs(rand()) % 17; 33 lparamlen = abs(rand()) % 17;
58 34
59 /* pick a random saltlen 0..16 */ 35 /* pick a random saltlen 0..16 */
69 /* decode it */ 45 /* decode it */
70 l2 = sizeof(buf[2]); 46 l2 = sizeof(buf[2]);
71 DO(pkcs_1_oaep_decode(buf[1], l1, lparam, lparamlen, modlen, hash_idx, buf[2], &l2, &res1)); 47 DO(pkcs_1_oaep_decode(buf[1], l1, lparam, lparamlen, modlen, hash_idx, buf[2], &l2, &res1));
72 48
73 if (res1 != 1 || l2 != l3 || memcmp(buf[2], buf[0], l3) != 0) { 49 if (res1 != 1 || l2 != l3 || memcmp(buf[2], buf[0], l3) != 0) {
74 printf("Outsize == %lu, should have been %lu, res1 = %d, lparamlen = %lu, msg contents follow.\n", l2, l3, res1, lparamlen); 50 fprintf(stderr, "Outsize == %lu, should have been %lu, res1 = %d, lparamlen = %lu, msg contents follow.\n", l2, l3, res1, lparamlen);
75 printf("ORIGINAL:\n"); 51 fprintf(stderr, "ORIGINAL:\n");
76 for (x = 0; x < l3; x++) { 52 for (x = 0; x < l3; x++) {
77 printf("%02x ", buf[0][x]); 53 fprintf(stderr, "%02x ", buf[0][x]);
78 } 54 }
79 printf("\nRESULT:\n"); 55 fprintf(stderr, "\nRESULT:\n");
80 for (x = 0; x < l2; x++) { 56 for (x = 0; x < l2; x++) {
81 printf("%02x ", buf[2][x]); 57 fprintf(stderr, "%02x ", buf[2][x]);
82 } 58 }
83 printf("\n\n"); 59 fprintf(stderr, "\n\n");
84 return 1; 60 return 1;
85 } 61 }
86 62
87 /* test PSS */ 63 /* test PSS */
88 l1 = sizeof(buf[1]); 64 l1 = sizeof(buf[1]);
95 buf[0][i1] ^= 1; 71 buf[0][i1] ^= 1;
96 buf[1][i2 = abs(rand()) % l1] ^= 1; 72 buf[1][i2 = abs(rand()) % l1] ^= 1;
97 DO(pkcs_1_pss_decode(buf[0], l3, buf[1], l1, saltlen, hash_idx, modlen, &res3)); 73 DO(pkcs_1_pss_decode(buf[0], l3, buf[1], l1, saltlen, hash_idx, modlen, &res3));
98 74
99 if (!(res1 == 1 && res2 == 0 && res3 == 0)) { 75 if (!(res1 == 1 && res2 == 0 && res3 == 0)) {
100 printf("PSS failed: %d, %d, %d, %lu, %lu\n", res1, res2, res3, l3, saltlen); 76 fprintf(stderr, "PSS failed: %d, %d, %d, %lu, %lu\n", res1, res2, res3, l3, saltlen);
101 return 1; 77 return 1;
102 } 78 }
103 } 79 }
104 return 0; 80 return 0;
105 } 81 }
106 82
107 #else 83 #else
108 84
109 int pkcs_1_test(void) 85 int pkcs_1_test(void)
110 { 86 {
111 printf("NOP"); 87 fprintf(stderr, "NOP");
112 return 0; 88 return 0;
113 } 89 }
114 90
115 #endif 91 #endif
116 92
93
94 /* $Source: /cvs/libtom/libtomcrypt/testprof/pkcs_1_test.c,v $ */
95 /* $Revision: 1.6 $ */
96 /* $Date: 2005/05/21 12:51:25 $ */