15
|
1 #include "test.h" |
|
2 |
|
3 test_entry tests[26]; |
|
4 |
|
5 test_entry test_list[26] = { |
|
6 |
|
7 /* test name provides requires entry */ |
|
8 {"store_test", "a", "", store_test }, |
|
9 {"cipher_hash_test", "b", "a", cipher_hash_test }, |
|
10 {"modes_test", "c", "b", modes_test }, |
|
11 {"mac_test", "d", "c", mac_test }, |
143
|
12 {"der_test", "e", "", der_tests }, |
|
13 |
|
14 {"pkcs_1_test", "f", "e", pkcs_1_test }, |
|
15 {"rsa_test", "g", "e", rsa_test }, |
|
16 {"ecc_test", "h", "a", ecc_tests }, |
|
17 {"dsa_test", "i", "a", dsa_test }, |
|
18 {"dh_test", "j", "a", dh_tests }, |
15
|
19 |
|
20 {NULL, NULL, NULL, NULL} |
|
21 }; |
|
22 |
|
23 prng_state test_yarrow; |
|
24 static int current_test; |
|
25 |
|
26 void run_cmd(int res, int line, char *file, char *cmd) |
|
27 { |
|
28 if (res != CRYPT_OK) { |
|
29 fprintf(stderr, "[%s]: %s (%d)\n%s:%d:%s\n", tests[current_test].name, error_to_string(res), res, file, line, cmd); |
|
30 exit(EXIT_FAILURE); |
|
31 } |
|
32 } |
|
33 |
|
34 void register_algs(void) |
|
35 { |
143
|
36 int err; |
|
37 |
15
|
38 #ifdef RIJNDAEL |
|
39 register_cipher (&aes_desc); |
|
40 #endif |
|
41 #ifdef BLOWFISH |
|
42 register_cipher (&blowfish_desc); |
|
43 #endif |
|
44 #ifdef XTEA |
|
45 register_cipher (&xtea_desc); |
|
46 #endif |
|
47 #ifdef RC5 |
|
48 register_cipher (&rc5_desc); |
|
49 #endif |
|
50 #ifdef RC6 |
|
51 register_cipher (&rc6_desc); |
|
52 #endif |
|
53 #ifdef SAFERP |
|
54 register_cipher (&saferp_desc); |
|
55 #endif |
|
56 #ifdef TWOFISH |
|
57 register_cipher (&twofish_desc); |
|
58 #endif |
|
59 #ifdef SAFER |
|
60 register_cipher (&safer_k64_desc); |
|
61 register_cipher (&safer_sk64_desc); |
|
62 register_cipher (&safer_k128_desc); |
|
63 register_cipher (&safer_sk128_desc); |
|
64 #endif |
|
65 #ifdef RC2 |
|
66 register_cipher (&rc2_desc); |
|
67 #endif |
|
68 #ifdef DES |
|
69 register_cipher (&des_desc); |
|
70 register_cipher (&des3_desc); |
|
71 #endif |
|
72 #ifdef CAST5 |
|
73 register_cipher (&cast5_desc); |
|
74 #endif |
|
75 #ifdef NOEKEON |
|
76 register_cipher (&noekeon_desc); |
|
77 #endif |
|
78 #ifdef SKIPJACK |
|
79 register_cipher (&skipjack_desc); |
|
80 #endif |
|
81 #ifdef TIGER |
|
82 register_hash (&tiger_desc); |
|
83 #endif |
|
84 #ifdef MD2 |
|
85 register_hash (&md2_desc); |
|
86 #endif |
|
87 #ifdef MD4 |
|
88 register_hash (&md4_desc); |
|
89 #endif |
|
90 #ifdef MD5 |
|
91 register_hash (&md5_desc); |
|
92 #endif |
|
93 #ifdef SHA1 |
|
94 register_hash (&sha1_desc); |
|
95 #endif |
|
96 #ifdef SHA256 |
|
97 register_hash (&sha256_desc); |
|
98 #endif |
|
99 #ifdef SHA224 |
|
100 register_hash (&sha224_desc); |
|
101 #endif |
|
102 #ifdef SHA384 |
|
103 register_hash (&sha384_desc); |
|
104 #endif |
|
105 #ifdef SHA512 |
|
106 register_hash (&sha512_desc); |
|
107 #endif |
|
108 #ifdef RIPEMD128 |
|
109 register_hash (&rmd128_desc); |
|
110 #endif |
|
111 #ifdef RIPEMD160 |
|
112 register_hash (&rmd160_desc); |
|
113 #endif |
|
114 #ifdef WHIRLPOOL |
|
115 register_hash (&whirlpool_desc); |
|
116 #endif |
143
|
117 #ifdef CHC_HASH |
|
118 register_hash(&chc_desc); |
|
119 if ((err = chc_register(register_cipher(&aes_enc_desc))) != CRYPT_OK) { |
|
120 printf("chc_register error: %s\n", error_to_string(err)); |
|
121 exit(EXIT_FAILURE); |
|
122 } |
|
123 #endif |
15
|
124 |
|
125 |
143
|
126 #ifdef YARROW |
|
127 register_prng(&yarrow_desc); |
|
128 #endif |
|
129 #ifdef FORTUNA |
|
130 register_prng(&fortuna_desc); |
|
131 #endif |
|
132 #ifdef RC4 |
|
133 register_prng(&rc4_desc); |
|
134 #endif |
|
135 #ifdef SPRNG |
|
136 register_prng(&sprng_desc); |
|
137 #endif |
|
138 #ifdef SOBER128 |
|
139 register_prng(&sober128_desc); |
|
140 #endif |
15
|
141 } |
|
142 |
|
143 /* sort tests based on their requirement/services. Helps make sure dependencies are tested first */ |
|
144 void sort(void) |
|
145 { |
|
146 unsigned x, y, z, a, pidx[26]; |
|
147 |
|
148 /* find out where things are provided */ |
|
149 zeromem(pidx, sizeof(pidx)); |
|
150 z = 0; |
|
151 do { |
|
152 y = 0; |
|
153 for (x = 0; test_list[x].name != NULL; x++) { |
|
154 if (test_list[x].entry == NULL) continue; |
|
155 if (strlen(test_list[x].prov) == 0) { |
|
156 y = 1; |
|
157 tests[z++] = test_list[x]; test_list[x].entry = NULL; |
|
158 pidx[test_list[x].prov[0]-'a'] = 1; |
|
159 break; |
|
160 } else { |
|
161 for (a = 0; a < strlen(test_list[x].req); a++) { |
|
162 if (pidx[test_list[x].req[a]-'a'] == 0) break; |
|
163 } |
|
164 if (a == strlen(test_list[x].req)) { |
|
165 y = 1; |
|
166 tests[z++] = test_list[x]; test_list[x].entry = NULL; |
|
167 pidx[test_list[x].prov[0]-'a'] = 1; |
|
168 break; |
|
169 } |
|
170 } |
|
171 } |
|
172 } while (y == 1); |
|
173 } |
143
|
174 |
|
175 #define STACKBLOCK 8 |
|
176 #define STACK_EST_USAGE 32768 |
|
177 |
|
178 unsigned char stack_mask[STACKBLOCK]; |
|
179 unsigned long stack_cur=0; |
|
180 |
|
181 void stack_masker(void) |
|
182 { |
|
183 #ifdef STACK_TEST |
|
184 volatile unsigned char M[STACK_EST_USAGE]; |
|
185 stack_cur = 0; |
|
186 for (stack_cur = 0; stack_cur < STACK_EST_USAGE/STACKBLOCK; stack_cur++) { |
|
187 memcpy(M+(stack_cur*STACKBLOCK), stack_mask, STACKBLOCK); |
|
188 } |
|
189 #endif |
|
190 } |
|
191 |
|
192 void stack_check(void) |
|
193 { |
|
194 #ifdef STACK_TEST |
|
195 unsigned char M[STACK_EST_USAGE]; |
|
196 stack_cur = 0; |
|
197 #ifdef STACK_DOWN |
|
198 while (memcmp(M+(STACK_EST_USAGE-STACKBLOCK-stack_cur), stack_mask, STACKBLOCK) && |
|
199 #else |
|
200 while (memcmp(M+stack_cur, stack_mask, STACKBLOCK) && |
|
201 #endif |
|
202 stack_cur < (STACK_EST_USAGE - STACKBLOCK)) { |
|
203 ++stack_cur; |
|
204 } |
|
205 #endif |
|
206 } |
|
207 |
15
|
208 int main(void) |
|
209 { |
143
|
210 int x; |
|
211 unsigned char buf[16]; |
|
212 |
|
213 /* setup stack checker */ |
|
214 srand(time(NULL)); |
|
215 for (x = 0; x < STACKBLOCK; x++) { |
|
216 stack_mask[x] = rand() & 255; |
|
217 } |
|
218 stack_masker(); |
|
219 |
15
|
220 printf("Built with\n%s\n", crypt_build_settings); |
|
221 |
|
222 sort(); |
|
223 register_algs(); |
|
224 |
|
225 // start dummy yarrow for internal use |
|
226 DO(yarrow_start(&test_yarrow)); |
143
|
227 sprng_read(buf, 16, NULL); |
|
228 DO(yarrow_add_entropy(buf, 16, &test_yarrow)); |
15
|
229 DO(yarrow_ready(&test_yarrow)); |
|
230 |
143
|
231 // output sizes |
|
232 printf("Sizes of objects (in bytes)\n"); |
|
233 printf("\tsymmetric_key\t=\t%5lu\n", sizeof(symmetric_key)); |
|
234 printf("\thash_state\t=\t%5lu\n", sizeof(hash_state)); |
|
235 printf("\thmac_state\t=\t%5lu\n", sizeof(hmac_state)); |
|
236 printf("\tomac_state\t=\t%5lu\n", sizeof(omac_state)); |
|
237 printf("\tpmac_state\t=\t%5lu\n", sizeof(pmac_state)); |
|
238 printf("\tocb_state\t=\t%5lu\n", sizeof(ocb_state)); |
|
239 printf("\teax_state\t=\t%5lu\n", sizeof(eax_state)); |
|
240 printf("\tmp_int\t\t=\t%5lu\n", sizeof(mp_int)); |
|
241 #ifdef MRSA |
|
242 printf("\trsa_key\t\t=\t%5lu\n", sizeof(rsa_key)); |
|
243 #endif |
|
244 #ifdef MDSA |
|
245 printf("\tdsa_key\t\t=\t%5lu\n", sizeof(dsa_key)); |
|
246 #endif |
|
247 #ifdef MDH |
|
248 printf("\tdh_key\t\t=\t%5lu\n", sizeof(dh_key)); |
|
249 #endif |
|
250 #ifdef MECC |
|
251 printf("\tecc_key\t\t=\t%5lu\n", sizeof(ecc_key)); |
|
252 #endif |
|
253 |
|
254 printf("\n\n"); |
15
|
255 // do tests |
|
256 for (current_test = 0; tests[current_test].name != NULL; current_test++) { |
|
257 printf("[%-20s]: ", tests[current_test].name); fflush(stdout); |
|
258 printf("\t%s\n", tests[current_test].entry()==0?"passed":"failed"); |
|
259 } |
|
260 |
|
261 return 0; |
|
262 } |