comparison testprof/rsa_test.c @ 191:1c15b283127b libtomcrypt-orig

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