comparison demos/test/rsa_test.c @ 143:5d99163f7e32 libtomcrypt-orig

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