Mercurial > dropbear
comparison testprof/rsa_test.c @ 380:d5faf4814ddb libtomcrypt-orig libtomcrypt-1.16
Update to LibTomCrypt 1.16
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 11 Jan 2007 02:22:00 +0000 |
parents | 59400faa4b44 |
children |
comparison
equal
deleted
inserted
replaced
280:59400faa4b44 | 380:d5faf4814ddb |
---|---|
45 0x5d, 0x04, 0x58, 0x3a, 0x5a, 0x39, 0xf1, 0x4a, 0x21, 0x56, 0x67, 0xfd, 0xcc, 0x20, 0xa3, 0x8f, | 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, | 46 0x78, 0x18, 0x5a, 0x79, 0x3d, 0x2e, 0x8e, 0x7e, 0x86, 0x0a, 0xe6, 0xa8, 0x33, 0xc1, 0x04, 0x17, |
47 0x4a, 0x9f, }; | 47 0x4a, 0x9f, }; |
48 | 48 |
49 | 49 |
50 /*** NOTE: OpenSSL seems to have more to their public key format. I've stripped the extra headers... */ | 50 /*** openssl public RSA key in DER format */ |
51 static const unsigned char openssl_public_rsa[] = { | 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[] = { | |
52 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xcf, 0x9a, 0xde, | 66 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xcf, 0x9a, 0xde, |
53 0x64, 0x8a, 0xda, 0xc8, 0x33, 0x20, 0xa9, 0xd7, 0x83, 0x31, 0x19, 0x54, 0xb2, 0x9a, 0x85, 0xa7, | 67 0x64, 0x8a, 0xda, 0xc8, 0x33, 0x20, 0xa9, 0xd7, 0x83, 0x31, 0x19, 0x54, 0xb2, 0x9a, 0x85, 0xa7, |
54 0xa1, 0xb7, 0x75, 0x33, 0xb6, 0xa9, 0xac, 0x84, 0x24, 0xb3, 0xde, 0xdb, 0x7d, 0x85, 0x2d, 0x96, | 68 0xa1, 0xb7, 0x75, 0x33, 0xb6, 0xa9, 0xac, 0x84, 0x24, 0xb3, 0xde, 0xdb, 0x7d, 0x85, 0x2d, 0x96, |
55 0x65, 0xe5, 0x3f, 0x72, 0x95, 0x24, 0x9f, 0x28, 0x68, 0xca, 0x4f, 0xdb, 0x44, 0x1c, 0x3e, 0x60, | 69 0x65, 0xe5, 0x3f, 0x72, 0x95, 0x24, 0x9f, 0x28, 0x68, 0xca, 0x4f, 0xdb, 0x44, 0x1c, 0x3e, 0x60, |
56 0x12, 0x8a, 0xdd, 0x26, 0xa5, 0xeb, 0xff, 0x0b, 0x5e, 0xd4, 0x88, 0x38, 0x49, 0x2a, 0x6e, 0x5b, | 70 0x12, 0x8a, 0xdd, 0x26, 0xa5, 0xeb, 0xff, 0x0b, 0x5e, 0xd4, 0x88, 0x38, 0x49, 0x2a, 0x6e, 0x5b, |
71 | 85 |
72 /* now try to export private/public and compare */ | 86 /* now try to export private/public and compare */ |
73 len = sizeof(buf); | 87 len = sizeof(buf); |
74 DO(rsa_export(buf, &len, PK_PRIVATE, &key)); | 88 DO(rsa_export(buf, &len, PK_PRIVATE, &key)); |
75 if (len != sizeof(openssl_private_rsa) || memcmp(buf, openssl_private_rsa, len)) { | 89 if (len != sizeof(openssl_private_rsa) || memcmp(buf, openssl_private_rsa, len)) { |
76 fprintf(stderr, "RSA private export failed to match OpenSSL output, %lu, %lu\n", len, sizeof(openssl_private_rsa)); | 90 fprintf(stderr, "RSA private export failed to match OpenSSL output, %lu, %lu\n", len, (unsigned long)sizeof(openssl_private_rsa)); |
77 | |
78 | |
79 { | |
80 int x; | |
81 printf("\n\n"); | |
82 for (x = 0; x < len; ) { if (buf[x] == openssl_private_rsa[x]) printf("-- "); else printf("%02x ", buf[x]^openssl_private_rsa[x]); if (!(++x & 15)) printf("\n"); } | |
83 } | |
84 printf("\n\n"); | |
85 | |
86 return 1; | 91 return 1; |
87 } | 92 } |
88 | 93 |
89 len = sizeof(buf); | 94 len = sizeof(buf); |
90 DO(rsa_export(buf, &len, PK_PUBLIC, &key)); | 95 DO(rsa_export(buf, &len, PK_PUBLIC, &key)); |
91 if (len != sizeof(openssl_public_rsa) || memcmp(buf, openssl_public_rsa, len)) { | 96 if (len != sizeof(openssl_public_rsa_stripped) || memcmp(buf, openssl_public_rsa_stripped, len)) { |
92 fprintf(stderr, "RSA(private) public export failed to match OpenSSL output\n"); | 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"); | |
93 return 1; | 108 return 1; |
94 } | 109 } |
95 rsa_free(&key); | 110 rsa_free(&key); |
96 | 111 |
97 /* try reading the public key */ | 112 /* try reading the public key */ |
98 DO(rsa_import(openssl_public_rsa, sizeof(openssl_public_rsa), &key)); | 113 DO(rsa_import(openssl_public_rsa, sizeof(openssl_public_rsa), &key)); |
99 len = sizeof(buf); | 114 len = sizeof(buf); |
100 DO(rsa_export(buf, &len, PK_PUBLIC, &key)); | 115 DO(rsa_export(buf, &len, PK_PUBLIC, &key)); |
101 if (len != sizeof(openssl_public_rsa) || memcmp(buf, openssl_public_rsa, len)) { | 116 if (len != sizeof(openssl_public_rsa_stripped) || memcmp(buf, openssl_public_rsa_stripped, len)) { |
102 fprintf(stderr, "RSA(public) public export failed to match OpenSSL output\n"); | 117 fprintf(stderr, "RSA(public) SSL public import failed to match OpenSSL output\n"); |
103 return 1; | 118 return 1; |
104 } | 119 } |
105 rsa_free(&key); | 120 rsa_free(&key); |
106 | 121 |
107 return 0; | 122 return 0; |
127 } | 142 } |
128 | 143 |
129 /* make 10 random key */ | 144 /* make 10 random key */ |
130 for (cnt = 0; cnt < 10; cnt++) { | 145 for (cnt = 0; cnt < 10; cnt++) { |
131 DO(rsa_make_key(&yarrow_prng, prng_idx, 1024/8, 65537, &key)); | 146 DO(rsa_make_key(&yarrow_prng, prng_idx, 1024/8, 65537, &key)); |
132 if (mp_count_bits(&key.N) != 1024) { | 147 if (mp_count_bits(key.N) != 1024) { |
133 fprintf(stderr, "rsa_1024 key modulus has %d bits\n", mp_count_bits(&key.N)); | 148 fprintf(stderr, "rsa_1024 key modulus has %d bits\n", mp_count_bits(key.N)); |
134 | 149 |
135 len = mp_unsigned_bin_size(&key.N); | 150 len = mp_unsigned_bin_size(key.N); |
136 mp_to_unsigned_bin(&key.N, tmp); | 151 mp_to_unsigned_bin(key.N, tmp); |
137 fprintf(stderr, "N == \n"); | 152 fprintf(stderr, "N == \n"); |
138 for (cnt = 0; cnt < len; ) { | 153 for (cnt = 0; cnt < len; ) { |
139 fprintf(stderr, "%02x ", tmp[cnt]); | 154 fprintf(stderr, "%02x ", tmp[cnt]); |
140 if (!(++cnt & 15)) fprintf(stderr, "\n"); | 155 if (!(++cnt & 15)) fprintf(stderr, "\n"); |
141 } | 156 } |
142 | 157 |
143 len = mp_unsigned_bin_size(&key.p); | 158 len = mp_unsigned_bin_size(key.p); |
144 mp_to_unsigned_bin(&key.p, tmp); | 159 mp_to_unsigned_bin(key.p, tmp); |
145 fprintf(stderr, "p == \n"); | 160 fprintf(stderr, "p == \n"); |
146 for (cnt = 0; cnt < len; ) { | 161 for (cnt = 0; cnt < len; ) { |
147 fprintf(stderr, "%02x ", tmp[cnt]); | 162 fprintf(stderr, "%02x ", tmp[cnt]); |
148 if (!(++cnt & 15)) fprintf(stderr, "\n"); | 163 if (!(++cnt & 15)) fprintf(stderr, "\n"); |
149 } | 164 } |
150 | 165 |
151 len = mp_unsigned_bin_size(&key.q); | 166 len = mp_unsigned_bin_size(key.q); |
152 mp_to_unsigned_bin(&key.q, tmp); | 167 mp_to_unsigned_bin(key.q, tmp); |
153 fprintf(stderr, "\nq == \n"); | 168 fprintf(stderr, "\nq == \n"); |
154 for (cnt = 0; cnt < len; ) { | 169 for (cnt = 0; cnt < len; ) { |
155 fprintf(stderr, "%02x ", tmp[cnt]); | 170 fprintf(stderr, "%02x ", tmp[cnt]); |
156 if (!(++cnt & 15)) fprintf(stderr, "\n"); | 171 if (!(++cnt & 15)) fprintf(stderr, "\n"); |
157 } | 172 } |
240 fprintf(stderr, "rsa_decrypt_key mismatch len %lu", len2); | 255 fprintf(stderr, "rsa_decrypt_key mismatch len %lu", len2); |
241 return 1; | 256 return 1; |
242 } | 257 } |
243 } | 258 } |
244 | 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 | |
245 /* sign a message (unsalted, lower cholestorol and Atkins approved) now */ | 278 /* sign a message (unsalted, lower cholestorol and Atkins approved) now */ |
246 len = sizeof(out); | 279 len = sizeof(out); |
247 DO(rsa_sign_hash(in, 20, out, &len, &yarrow_prng, prng_idx, hash_idx, 0, &key)); | 280 DO(rsa_sign_hash(in, 20, out, &len, &yarrow_prng, prng_idx, hash_idx, 0, &key)); |
248 | 281 |
249 /* export key and import as both private and public */ | 282 /* export key and import as both private and public */ |
314 rsa_free(&pubKey); | 347 rsa_free(&pubKey); |
315 rsa_free(&privKey); | 348 rsa_free(&privKey); |
316 return 1; | 349 return 1; |
317 } | 350 } |
318 | 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 | |
319 /* free the key and return */ | 368 /* free the key and return */ |
320 rsa_free(&key); | 369 rsa_free(&key); |
321 rsa_free(&pubKey); | 370 rsa_free(&pubKey); |
322 rsa_free(&privKey); | 371 rsa_free(&privKey); |
323 return 0; | 372 return 0; |
332 } | 381 } |
333 | 382 |
334 #endif | 383 #endif |
335 | 384 |
336 /* $Source: /cvs/libtom/libtomcrypt/testprof/rsa_test.c,v $ */ | 385 /* $Source: /cvs/libtom/libtomcrypt/testprof/rsa_test.c,v $ */ |
337 /* $Revision: 1.10 $ */ | 386 /* $Revision: 1.18 $ */ |
338 /* $Date: 2005/06/03 19:18:33 $ */ | 387 /* $Date: 2006/11/21 00:10:18 $ */ |