comparison libtomcrypt/testprof/rsa_test.c @ 389:5ff8218bcee9

propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 2af95f00ebd5bb7a28b3817db1218442c935388e) to branch 'au.asn.ucc.matt.dropbear' (head ecd779509ef23a8cdf64888904fc9b31d78aa933)
author Matt Johnston <matt@ucc.asn.au>
date Thu, 11 Jan 2007 03:14:55 +0000
parents 0cbe8f6dbf9e
children f849a5ca2efc
comparison
equal deleted inserted replaced
388:fb54020f78e1 389:5ff8218bcee9
1 #include <tomcrypt_test.h>
2
3 #ifdef MRSA
4
5 #define RSA_MSGSIZE 78
6
7 /* These are test keys [see file test.key] that I use to test my import/export against */
8 static const unsigned char openssl_private_rsa[] = {
9 0x30, 0x82, 0x02, 0x5e, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xcf, 0x9a, 0xde, 0x64, 0x8a,
10 0xda, 0xc8, 0x33, 0x20, 0xa9, 0xd7, 0x83, 0x31, 0x19, 0x54, 0xb2, 0x9a, 0x85, 0xa7, 0xa1, 0xb7,
11 0x75, 0x33, 0xb6, 0xa9, 0xac, 0x84, 0x24, 0xb3, 0xde, 0xdb, 0x7d, 0x85, 0x2d, 0x96, 0x65, 0xe5,
12 0x3f, 0x72, 0x95, 0x24, 0x9f, 0x28, 0x68, 0xca, 0x4f, 0xdb, 0x44, 0x1c, 0x3e, 0x60, 0x12, 0x8a,
13 0xdd, 0x26, 0xa5, 0xeb, 0xff, 0x0b, 0x5e, 0xd4, 0x88, 0x38, 0x49, 0x2a, 0x6e, 0x5b, 0xbf, 0x12,
14 0x37, 0x47, 0xbd, 0x05, 0x6b, 0xbc, 0xdb, 0xf3, 0xee, 0xe4, 0x11, 0x8e, 0x41, 0x68, 0x7c, 0x61,
15 0x13, 0xd7, 0x42, 0xc8, 0x80, 0xbe, 0x36, 0x8f, 0xdc, 0x08, 0x8b, 0x4f, 0xac, 0xa4, 0xe2, 0x76,
16 0x0c, 0xc9, 0x63, 0x6c, 0x49, 0x58, 0x93, 0xed, 0xcc, 0xaa, 0xdc, 0x25, 0x3b, 0x0a, 0x60, 0x3f,
17 0x8b, 0x54, 0x3a, 0xc3, 0x4d, 0x31, 0xe7, 0x94, 0xa4, 0x44, 0xfd, 0x02, 0x03, 0x01, 0x00, 0x01,
18 0x02, 0x81, 0x81, 0x00, 0xc8, 0x62, 0xb9, 0xea, 0xde, 0x44, 0x53, 0x1d, 0x56, 0x97, 0xd9, 0x97,
19 0x9e, 0x1a, 0xcf, 0x30, 0x1e, 0x0a, 0x88, 0x45, 0x86, 0x29, 0x30, 0xa3, 0x4d, 0x9f, 0x61, 0x65,
20 0x73, 0xe0, 0xd6, 0x87, 0x8f, 0xb6, 0xf3, 0x06, 0xa3, 0x82, 0xdc, 0x7c, 0xac, 0xfe, 0x9b, 0x28,
21 0x9a, 0xae, 0xfd, 0xfb, 0xfe, 0x2f, 0x0e, 0xd8, 0x97, 0x04, 0xe3, 0xbb, 0x1f, 0xd1, 0xec, 0x0d,
22 0xba, 0xa3, 0x49, 0x7f, 0x47, 0xac, 0x8a, 0x44, 0x04, 0x7e, 0x86, 0xb7, 0x39, 0x42, 0x3f, 0xad,
23 0x1e, 0xb7, 0x0e, 0xa5, 0x51, 0xf4, 0x40, 0x63, 0x1e, 0xfd, 0xbd, 0xea, 0x9f, 0x41, 0x9f, 0xa8,
24 0x90, 0x1d, 0x6f, 0x0a, 0x5a, 0x95, 0x13, 0x11, 0x0d, 0x80, 0xaf, 0x5f, 0x64, 0x98, 0x8a, 0x2c,
25 0x78, 0x68, 0x65, 0xb0, 0x2b, 0x8b, 0xa2, 0x53, 0x87, 0xca, 0xf1, 0x64, 0x04, 0xab, 0xf2, 0x7b,
26 0xdb, 0x83, 0xc8, 0x81, 0x02, 0x41, 0x00, 0xf7, 0xbe, 0x5e, 0x23, 0xc3, 0x32, 0x3f, 0xbf, 0x8b,
27 0x8e, 0x3a, 0xee, 0xfc, 0xfc, 0xcb, 0xe5, 0xf7, 0xf1, 0x0b, 0xbc, 0x42, 0x82, 0xae, 0xd5, 0x7a,
28 0x3e, 0xca, 0xf7, 0xd5, 0x69, 0x3f, 0x64, 0x25, 0xa2, 0x1f, 0xb7, 0x75, 0x75, 0x05, 0x92, 0x42,
29 0xeb, 0xb8, 0xf1, 0xf3, 0x0a, 0x05, 0xe3, 0x94, 0xd1, 0x55, 0x78, 0x35, 0xa0, 0x36, 0xa0, 0x9b,
30 0x7c, 0x92, 0x84, 0x6c, 0xdd, 0xdc, 0x4d, 0x02, 0x41, 0x00, 0xd6, 0x86, 0x0e, 0x85, 0x42, 0x0b,
31 0x04, 0x08, 0x84, 0x21, 0x60, 0xf0, 0x0e, 0x0d, 0x88, 0xfd, 0x1e, 0x36, 0x10, 0x65, 0x4f, 0x1e,
32 0x53, 0xb4, 0x08, 0x72, 0x80, 0x5c, 0x3f, 0x59, 0x66, 0x17, 0xe6, 0x98, 0xf2, 0xe9, 0x6c, 0x7a,
33 0x06, 0x4c, 0xac, 0x76, 0x3d, 0xed, 0x8c, 0xa1, 0xce, 0xad, 0x1b, 0xbd, 0xb4, 0x7d, 0x28, 0xbc,
34 0xe3, 0x0e, 0x38, 0x8d, 0x99, 0xd8, 0x05, 0xb5, 0xa3, 0x71, 0x02, 0x40, 0x6d, 0xeb, 0xc3, 0x2d,
35 0x2e, 0xf0, 0x5e, 0xa4, 0x88, 0x31, 0x05, 0x29, 0x00, 0x8a, 0xd1, 0x95, 0x29, 0x9b, 0x83, 0xcf,
36 0x75, 0xdb, 0x31, 0xe3, 0x7a, 0x27, 0xde, 0x3a, 0x74, 0x30, 0x0c, 0x76, 0x4c, 0xd4, 0x50, 0x2a,
37 0x40, 0x2d, 0x39, 0xd9, 0x99, 0x63, 0xa9, 0x5d, 0x80, 0xae, 0x53, 0xca, 0x94, 0x3f, 0x05, 0x23,
38 0x1e, 0xf8, 0x05, 0x04, 0xe1, 0xb8, 0x35, 0xf2, 0x17, 0xb3, 0xa0, 0x89, 0x02, 0x41, 0x00, 0xab,
39 0x90, 0x88, 0xfa, 0x60, 0x08, 0x29, 0x50, 0x9a, 0x43, 0x8b, 0xa0, 0x50, 0xcc, 0xd8, 0x5a, 0xfe,
40 0x97, 0x64, 0x63, 0x71, 0x74, 0x22, 0xa3, 0x20, 0x02, 0x5a, 0xcf, 0xeb, 0xc6, 0x16, 0x95, 0x54,
41 0xd1, 0xcb, 0xab, 0x8d, 0x1a, 0xc6, 0x00, 0xfa, 0x08, 0x92, 0x9c, 0x71, 0xd5, 0x52, 0x52, 0x35,
42 0x96, 0x71, 0x4b, 0x8b, 0x92, 0x0c, 0xd0, 0xe9, 0xbf, 0xad, 0x63, 0x0b, 0xa5, 0xe9, 0xb1, 0x02,
43 0x41, 0x00, 0xdc, 0xcc, 0x27, 0xc8, 0xe4, 0xdc, 0x62, 0x48, 0xd5, 0x9b, 0xaf, 0xf5, 0xab, 0x60,
44 0xf6, 0x21, 0xfd, 0x53, 0xe2, 0xb7, 0x5d, 0x09, 0xc9, 0x1a, 0xa1, 0x04, 0xa9, 0xfc, 0x61, 0x2c,
45 0x5d, 0x04, 0x58, 0x3a, 0x5a, 0x39, 0xf1, 0x4a, 0x21, 0x56, 0x67, 0xfd, 0xcc, 0x20, 0xa3, 0x8f,
46 0x78, 0x18, 0x5a, 0x79, 0x3d, 0x2e, 0x8e, 0x7e, 0x86, 0x0a, 0xe6, 0xa8, 0x33, 0xc1, 0x04, 0x17,
47 0x4a, 0x9f, };
48
49
50 /*** openssl public RSA key in DER format */
51 static const unsigned char openssl_public_rsa[] = {
52 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
53 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xcf, 0x9a, 0xde,
54 0x64, 0x8a, 0xda, 0xc8, 0x33, 0x20, 0xa9, 0xd7, 0x83, 0x31, 0x19, 0x54, 0xb2, 0x9a, 0x85, 0xa7,
55 0xa1, 0xb7, 0x75, 0x33, 0xb6, 0xa9, 0xac, 0x84, 0x24, 0xb3, 0xde, 0xdb, 0x7d, 0x85, 0x2d, 0x96,
56 0x65, 0xe5, 0x3f, 0x72, 0x95, 0x24, 0x9f, 0x28, 0x68, 0xca, 0x4f, 0xdb, 0x44, 0x1c, 0x3e, 0x60,
57 0x12, 0x8a, 0xdd, 0x26, 0xa5, 0xeb, 0xff, 0x0b, 0x5e, 0xd4, 0x88, 0x38, 0x49, 0x2a, 0x6e, 0x5b,
58 0xbf, 0x12, 0x37, 0x47, 0xbd, 0x05, 0x6b, 0xbc, 0xdb, 0xf3, 0xee, 0xe4, 0x11, 0x8e, 0x41, 0x68,
59 0x7c, 0x61, 0x13, 0xd7, 0x42, 0xc8, 0x80, 0xbe, 0x36, 0x8f, 0xdc, 0x08, 0x8b, 0x4f, 0xac, 0xa4,
60 0xe2, 0x76, 0x0c, 0xc9, 0x63, 0x6c, 0x49, 0x58, 0x93, 0xed, 0xcc, 0xaa, 0xdc, 0x25, 0x3b, 0x0a,
61 0x60, 0x3f, 0x8b, 0x54, 0x3a, 0xc3, 0x4d, 0x31, 0xe7, 0x94, 0xa4, 0x44, 0xfd, 0x02, 0x03, 0x01,
62 0x00, 0x01, };
63
64 /* same key but with extra headers stripped */
65 static const unsigned char openssl_public_rsa_stripped[] = {
66 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xcf, 0x9a, 0xde,
67 0x64, 0x8a, 0xda, 0xc8, 0x33, 0x20, 0xa9, 0xd7, 0x83, 0x31, 0x19, 0x54, 0xb2, 0x9a, 0x85, 0xa7,
68 0xa1, 0xb7, 0x75, 0x33, 0xb6, 0xa9, 0xac, 0x84, 0x24, 0xb3, 0xde, 0xdb, 0x7d, 0x85, 0x2d, 0x96,
69 0x65, 0xe5, 0x3f, 0x72, 0x95, 0x24, 0x9f, 0x28, 0x68, 0xca, 0x4f, 0xdb, 0x44, 0x1c, 0x3e, 0x60,
70 0x12, 0x8a, 0xdd, 0x26, 0xa5, 0xeb, 0xff, 0x0b, 0x5e, 0xd4, 0x88, 0x38, 0x49, 0x2a, 0x6e, 0x5b,
71 0xbf, 0x12, 0x37, 0x47, 0xbd, 0x05, 0x6b, 0xbc, 0xdb, 0xf3, 0xee, 0xe4, 0x11, 0x8e, 0x41, 0x68,
72 0x7c, 0x61, 0x13, 0xd7, 0x42, 0xc8, 0x80, 0xbe, 0x36, 0x8f, 0xdc, 0x08, 0x8b, 0x4f, 0xac, 0xa4,
73 0xe2, 0x76, 0x0c, 0xc9, 0x63, 0x6c, 0x49, 0x58, 0x93, 0xed, 0xcc, 0xaa, 0xdc, 0x25, 0x3b, 0x0a,
74 0x60, 0x3f, 0x8b, 0x54, 0x3a, 0xc3, 0x4d, 0x31, 0xe7, 0x94, 0xa4, 0x44, 0xfd, 0x02, 0x03, 0x01,
75 0x00, 0x01, };
76
77 static int rsa_compat_test(void)
78 {
79 rsa_key key;
80 unsigned char buf[1024];
81 unsigned long len;
82
83 /* try reading the key */
84 DO(rsa_import(openssl_private_rsa, sizeof(openssl_private_rsa), &key));
85
86 /* now try to export private/public and compare */
87 len = sizeof(buf);
88 DO(rsa_export(buf, &len, PK_PRIVATE, &key));
89 if (len != sizeof(openssl_private_rsa) || memcmp(buf, openssl_private_rsa, len)) {
90 fprintf(stderr, "RSA private export failed to match OpenSSL output, %lu, %lu\n", len, (unsigned long)sizeof(openssl_private_rsa));
91 return 1;
92 }
93
94 len = sizeof(buf);
95 DO(rsa_export(buf, &len, PK_PUBLIC, &key));
96 if (len != sizeof(openssl_public_rsa_stripped) || memcmp(buf, openssl_public_rsa_stripped, len)) {
97 fprintf(stderr, "RSA(private) public export failed to match OpenSSL output\n");
98 return 1;
99 }
100 rsa_free(&key);
101
102 /* try reading the public key */
103 DO(rsa_import(openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), &key));
104 len = sizeof(buf);
105 DO(rsa_export(buf, &len, PK_PUBLIC, &key));
106 if (len != sizeof(openssl_public_rsa_stripped) || memcmp(buf, openssl_public_rsa_stripped, len)) {
107 fprintf(stderr, "RSA(public) stripped public import failed to match OpenSSL output\n");
108 return 1;
109 }
110 rsa_free(&key);
111
112 /* try reading the public key */
113 DO(rsa_import(openssl_public_rsa, sizeof(openssl_public_rsa), &key));
114 len = sizeof(buf);
115 DO(rsa_export(buf, &len, PK_PUBLIC, &key));
116 if (len != sizeof(openssl_public_rsa_stripped) || memcmp(buf, openssl_public_rsa_stripped, len)) {
117 fprintf(stderr, "RSA(public) SSL public import failed to match OpenSSL output\n");
118 return 1;
119 }
120 rsa_free(&key);
121
122 return 0;
123 }
124
125 int rsa_test(void)
126 {
127 unsigned char in[1024], out[1024], tmp[1024];
128 rsa_key key, privKey, pubKey;
129 int hash_idx, prng_idx, stat, stat2;
130 unsigned long rsa_msgsize, len, len2, cnt;
131 static unsigned char lparam[] = { 0x01, 0x02, 0x03, 0x04 };
132
133 if (rsa_compat_test() != 0) {
134 return 1;
135 }
136
137 hash_idx = find_hash("sha1");
138 prng_idx = find_prng("yarrow");
139 if (hash_idx == -1 || prng_idx == -1) {
140 fprintf(stderr, "rsa_test requires SHA1 and yarrow");
141 return 1;
142 }
143
144 /* make 10 random key */
145 for (cnt = 0; cnt < 10; cnt++) {
146 DO(rsa_make_key(&yarrow_prng, prng_idx, 1024/8, 65537, &key));
147 if (mp_count_bits(key.N) != 1024) {
148 fprintf(stderr, "rsa_1024 key modulus has %d bits\n", mp_count_bits(key.N));
149
150 len = mp_unsigned_bin_size(key.N);
151 mp_to_unsigned_bin(key.N, tmp);
152 fprintf(stderr, "N == \n");
153 for (cnt = 0; cnt < len; ) {
154 fprintf(stderr, "%02x ", tmp[cnt]);
155 if (!(++cnt & 15)) fprintf(stderr, "\n");
156 }
157
158 len = mp_unsigned_bin_size(key.p);
159 mp_to_unsigned_bin(key.p, tmp);
160 fprintf(stderr, "p == \n");
161 for (cnt = 0; cnt < len; ) {
162 fprintf(stderr, "%02x ", tmp[cnt]);
163 if (!(++cnt & 15)) fprintf(stderr, "\n");
164 }
165
166 len = mp_unsigned_bin_size(key.q);
167 mp_to_unsigned_bin(key.q, tmp);
168 fprintf(stderr, "\nq == \n");
169 for (cnt = 0; cnt < len; ) {
170 fprintf(stderr, "%02x ", tmp[cnt]);
171 if (!(++cnt & 15)) fprintf(stderr, "\n");
172 }
173 fprintf(stderr, "\n");
174
175
176 return 1;
177 }
178 if (cnt != 9) {
179 rsa_free(&key);
180 }
181 }
182
183 /* encrypt the key (without lparam) */
184 for (cnt = 0; cnt < 4; cnt++) {
185 for (rsa_msgsize = 1; rsa_msgsize <= 86; rsa_msgsize++) {
186 /* make a random key/msg */
187 yarrow_read(in, rsa_msgsize, &yarrow_prng);
188
189 len = sizeof(out);
190 len2 = rsa_msgsize;
191
192 DO(rsa_encrypt_key(in, rsa_msgsize, out, &len, NULL, 0, &yarrow_prng, prng_idx, hash_idx, &key));
193 /* change a byte */
194 out[8] ^= 1;
195 DO(rsa_decrypt_key(out, len, tmp, &len2, NULL, 0, hash_idx, &stat2, &key));
196 /* change a byte back */
197 out[8] ^= 1;
198 if (len2 != rsa_msgsize) {
199 fprintf(stderr, "\nrsa_decrypt_key mismatch len %lu (first decrypt)", len2);
200 return 1;
201 }
202
203 len2 = rsa_msgsize;
204 DO(rsa_decrypt_key(out, len, tmp, &len2, NULL, 0, hash_idx, &stat, &key));
205 if (!(stat == 1 && stat2 == 0)) {
206 fprintf(stderr, "rsa_decrypt_key failed");
207 return 1;
208 }
209 if (len2 != rsa_msgsize || memcmp(tmp, in, rsa_msgsize)) {
210 unsigned long x;
211 fprintf(stderr, "\nrsa_decrypt_key mismatch, len %lu (second decrypt)\n", len2);
212 fprintf(stderr, "Original contents: \n");
213 for (x = 0; x < rsa_msgsize; ) {
214 fprintf(stderr, "%02x ", in[x]);
215 if (!(++x % 16)) {
216 fprintf(stderr, "\n");
217 }
218 }
219 fprintf(stderr, "\n");
220 fprintf(stderr, "Output contents: \n");
221 for (x = 0; x < rsa_msgsize; ) {
222 fprintf(stderr, "%02x ", out[x]);
223 if (!(++x % 16)) {
224 fprintf(stderr, "\n");
225 }
226 }
227 fprintf(stderr, "\n");
228 return 1;
229 }
230 }
231 }
232
233 /* encrypt the key (with lparam) */
234 for (rsa_msgsize = 1; rsa_msgsize <= 86; rsa_msgsize++) {
235 len = sizeof(out);
236 len2 = rsa_msgsize;
237 DO(rsa_encrypt_key(in, rsa_msgsize, out, &len, lparam, sizeof(lparam), &yarrow_prng, prng_idx, hash_idx, &key));
238 /* change a byte */
239 out[8] ^= 1;
240 DO(rsa_decrypt_key(out, len, tmp, &len2, lparam, sizeof(lparam), hash_idx, &stat2, &key));
241 if (len2 != rsa_msgsize) {
242 fprintf(stderr, "\nrsa_decrypt_key mismatch len %lu (first decrypt)", len2);
243 return 1;
244 }
245 /* change a byte back */
246 out[8] ^= 1;
247
248 len2 = rsa_msgsize;
249 DO(rsa_decrypt_key(out, len, tmp, &len2, lparam, sizeof(lparam), hash_idx, &stat, &key));
250 if (!(stat == 1 && stat2 == 0)) {
251 fprintf(stderr, "rsa_decrypt_key failed");
252 return 1;
253 }
254 if (len2 != rsa_msgsize || memcmp(tmp, in, rsa_msgsize)) {
255 fprintf(stderr, "rsa_decrypt_key mismatch len %lu", len2);
256 return 1;
257 }
258 }
259
260 /* encrypt the key PKCS #1 v1.5 (payload from 1 to 117 bytes) */
261 for (rsa_msgsize = 1; rsa_msgsize <= 117; rsa_msgsize++) {
262 len = sizeof(out);
263 len2 = rsa_msgsize;
264 DO(rsa_encrypt_key_ex(in, rsa_msgsize, out, &len, NULL, 0, &yarrow_prng, prng_idx, 0, LTC_PKCS_1_V1_5, &key));
265
266 len2 = rsa_msgsize;
267 DO(rsa_decrypt_key_ex(out, len, tmp, &len2, NULL, 0, 0, LTC_PKCS_1_V1_5, &stat, &key));
268 if (!(stat == 1 && stat2 == 0)) {
269 fprintf(stderr, "rsa_decrypt_key_ex failed, %d, %d", stat, stat2);
270 return 1;
271 }
272 if (len2 != rsa_msgsize || memcmp(tmp, in, rsa_msgsize)) {
273 fprintf(stderr, "rsa_decrypt_key_ex mismatch len %lu", len2);
274 return 1;
275 }
276 }
277
278 /* sign a message (unsalted, lower cholestorol and Atkins approved) now */
279 len = sizeof(out);
280 DO(rsa_sign_hash(in, 20, out, &len, &yarrow_prng, prng_idx, hash_idx, 0, &key));
281
282 /* export key and import as both private and public */
283 len2 = sizeof(tmp);
284 DO(rsa_export(tmp, &len2, PK_PRIVATE, &key));
285 DO(rsa_import(tmp, len2, &privKey));
286 len2 = sizeof(tmp);
287 DO(rsa_export(tmp, &len2, PK_PUBLIC, &key));
288 DO(rsa_import(tmp, len2, &pubKey));
289
290 /* verify with original */
291 DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &key));
292 /* change a byte */
293 in[0] ^= 1;
294 DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &key));
295
296 if (!(stat == 1 && stat2 == 0)) {
297 fprintf(stderr, "rsa_verify_hash (unsalted, origKey) failed, %d, %d", stat, stat2);
298 rsa_free(&key);
299 rsa_free(&pubKey);
300 rsa_free(&privKey);
301 return 1;
302 }
303
304 /* verify with privKey */
305 /* change a byte */
306 in[0] ^= 1;
307 DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &privKey));
308 /* change a byte */
309 in[0] ^= 1;
310 DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &privKey));
311
312 if (!(stat == 1 && stat2 == 0)) {
313 fprintf(stderr, "rsa_verify_hash (unsalted, privKey) failed, %d, %d", stat, stat2);
314 rsa_free(&key);
315 rsa_free(&pubKey);
316 rsa_free(&privKey);
317 return 1;
318 }
319
320 /* verify with pubKey */
321 /* change a byte */
322 in[0] ^= 1;
323 DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &pubKey));
324 /* change a byte */
325 in[0] ^= 1;
326 DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &pubKey));
327
328 if (!(stat == 1 && stat2 == 0)) {
329 fprintf(stderr, "rsa_verify_hash (unsalted, pubkey) failed, %d, %d", stat, stat2);
330 rsa_free(&key);
331 rsa_free(&pubKey);
332 rsa_free(&privKey);
333 return 1;
334 }
335
336 /* sign a message (salted) now (use privKey to make, pubKey to verify) */
337 len = sizeof(out);
338 DO(rsa_sign_hash(in, 20, out, &len, &yarrow_prng, prng_idx, hash_idx, 8, &privKey));
339 DO(rsa_verify_hash(out, len, in, 20, hash_idx, 8, &stat, &pubKey));
340 /* change a byte */
341 in[0] ^= 1;
342 DO(rsa_verify_hash(out, len, in, 20, hash_idx, 8, &stat2, &pubKey));
343
344 if (!(stat == 1 && stat2 == 0)) {
345 fprintf(stderr, "rsa_verify_hash (salted) failed, %d, %d", stat, stat2);
346 rsa_free(&key);
347 rsa_free(&pubKey);
348 rsa_free(&privKey);
349 return 1;
350 }
351
352 /* sign a message with PKCS #1 v1.5 */
353 len = sizeof(out);
354 DO(rsa_sign_hash_ex(in, 20, out, &len, LTC_PKCS_1_V1_5, &yarrow_prng, prng_idx, hash_idx, 8, &privKey));
355 DO(rsa_verify_hash_ex(out, len, in, 20, LTC_PKCS_1_V1_5, hash_idx, 8, &stat, &pubKey));
356 /* change a byte */
357 in[0] ^= 1;
358 DO(rsa_verify_hash_ex(out, len, in, 20, LTC_PKCS_1_V1_5, hash_idx, 8, &stat2, &pubKey));
359
360 if (!(stat == 1 && stat2 == 0)) {
361 fprintf(stderr, "rsa_verify_hash_ex failed, %d, %d", stat, stat2);
362 rsa_free(&key);
363 rsa_free(&pubKey);
364 rsa_free(&privKey);
365 return 1;
366 }
367
368 /* free the key and return */
369 rsa_free(&key);
370 rsa_free(&pubKey);
371 rsa_free(&privKey);
372 return 0;
373 }
374
375 #else
376
377 int rsa_test(void)
378 {
379 fprintf(stderr, "NOP");
380 return 0;
381 }
382
383 #endif
384
385 /* $Source: /cvs/libtom/libtomcrypt/testprof/rsa_test.c,v $ */
386 /* $Revision: 1.18 $ */
387 /* $Date: 2006/11/21 00:10:18 $ */