Mercurial > dropbear
comparison libtomcrypt/demos/tv_gen.c @ 1478:3a933956437e coverity
update coverity
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 09 Feb 2018 23:49:22 +0800 |
parents | 6dba84798cd5 |
children | e9dba7abd939 |
comparison
equal
deleted
inserted
replaced
1439:8d24733026c5 | 1478:3a933956437e |
---|---|
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis | |
2 * | |
3 * LibTomCrypt is a library that provides various cryptographic | |
4 * algorithms in a highly modular and flexible manner. | |
5 * | |
6 * The library is free for all purposes without any express | |
7 * guarantee it works. | |
8 */ | |
1 #include <tomcrypt.h> | 9 #include <tomcrypt.h> |
2 | |
3 void reg_algs(void) | |
4 { | |
5 int err; | |
6 | |
7 #ifdef LTC_RIJNDAEL | |
8 register_cipher (&aes_desc); | |
9 #endif | |
10 #ifdef LTC_BLOWFISH | |
11 register_cipher (&blowfish_desc); | |
12 #endif | |
13 #ifdef LTC_XTEA | |
14 register_cipher (&xtea_desc); | |
15 #endif | |
16 #ifdef LTC_RC5 | |
17 register_cipher (&rc5_desc); | |
18 #endif | |
19 #ifdef LTC_RC6 | |
20 register_cipher (&rc6_desc); | |
21 #endif | |
22 #ifdef LTC_SAFERP | |
23 register_cipher (&saferp_desc); | |
24 #endif | |
25 #ifdef LTC_TWOFISH | |
26 register_cipher (&twofish_desc); | |
27 #endif | |
28 #ifdef LTC_SAFER | |
29 register_cipher (&safer_k64_desc); | |
30 register_cipher (&safer_sk64_desc); | |
31 register_cipher (&safer_k128_desc); | |
32 register_cipher (&safer_sk128_desc); | |
33 #endif | |
34 #ifdef LTC_RC2 | |
35 register_cipher (&rc2_desc); | |
36 #endif | |
37 #ifdef LTC_DES | |
38 register_cipher (&des_desc); | |
39 register_cipher (&des3_desc); | |
40 #endif | |
41 #ifdef LTC_CAST5 | |
42 register_cipher (&cast5_desc); | |
43 #endif | |
44 #ifdef LTC_NOEKEON | |
45 register_cipher (&noekeon_desc); | |
46 #endif | |
47 #ifdef LTC_SKIPJACK | |
48 register_cipher (&skipjack_desc); | |
49 #endif | |
50 #ifdef LTC_ANUBIS | |
51 register_cipher (&anubis_desc); | |
52 #endif | |
53 #ifdef LTC_KHAZAD | |
54 register_cipher (&khazad_desc); | |
55 #endif | |
56 | |
57 #ifdef LTC_TIGER | |
58 register_hash (&tiger_desc); | |
59 #endif | |
60 #ifdef LTC_MD2 | |
61 register_hash (&md2_desc); | |
62 #endif | |
63 #ifdef LTC_MD4 | |
64 register_hash (&md4_desc); | |
65 #endif | |
66 #ifdef LTC_MD5 | |
67 register_hash (&md5_desc); | |
68 #endif | |
69 #ifdef LTC_SHA1 | |
70 register_hash (&sha1_desc); | |
71 #endif | |
72 #ifdef LTC_SHA224 | |
73 register_hash (&sha224_desc); | |
74 #endif | |
75 #ifdef LTC_SHA256 | |
76 register_hash (&sha256_desc); | |
77 #endif | |
78 #ifdef LTC_SHA384 | |
79 register_hash (&sha384_desc); | |
80 #endif | |
81 #ifdef LTC_SHA512 | |
82 register_hash (&sha512_desc); | |
83 #endif | |
84 #ifdef LTC_RIPEMD128 | |
85 register_hash (&rmd128_desc); | |
86 #endif | |
87 #ifdef LTC_RIPEMD160 | |
88 register_hash (&rmd160_desc); | |
89 #endif | |
90 #ifdef LTC_WHIRLPOOL | |
91 register_hash (&whirlpool_desc); | |
92 #endif | |
93 #ifdef LTC_CHC_HASH | |
94 register_hash(&chc_desc); | |
95 if ((err = chc_register(register_cipher(&aes_desc))) != CRYPT_OK) { | |
96 printf("chc_register error: %s\n", error_to_string(err)); | |
97 exit(EXIT_FAILURE); | |
98 } | |
99 #endif | |
100 | |
101 #ifdef USE_LTM | |
102 ltc_mp = ltm_desc; | |
103 #elif defined(USE_TFM) | |
104 ltc_mp = tfm_desc; | |
105 #elif defined(USE_GMP) | |
106 ltc_mp = gmp_desc; | |
107 #else | |
108 extern ltc_math_descriptor EXT_MATH_LIB; | |
109 ltc_mp = EXT_MATH_LIB; | |
110 #endif | |
111 | |
112 | |
113 } | |
114 | 10 |
115 void hash_gen(void) | 11 void hash_gen(void) |
116 { | 12 { |
117 unsigned char md[MAXBLOCKSIZE], *buf; | 13 unsigned char md[MAXBLOCKSIZE], *buf; |
118 unsigned long outlen, x, y, z; | 14 unsigned long outlen, x, y, z; |
119 FILE *out; | 15 FILE *out; |
120 int err; | 16 int err; |
121 | 17 |
122 out = fopen("hash_tv.txt", "w"); | 18 out = fopen("hash_tv.txt", "w"); |
123 if (out == NULL) { | 19 if (out == NULL) { |
124 perror("can't open hash_tv"); | 20 perror("can't open hash_tv"); |
125 } | 21 } |
126 | 22 |
127 fprintf(out, "Hash Test Vectors:\n\nThese are the hashes of nn bytes '00 01 02 03 .. (nn-1)'\n\n"); | 23 fprintf(out, "Hash Test Vectors:\n\nThese are the hashes of nn bytes '00 01 02 03 .. (nn-1)'\n\n"); |
128 for (x = 0; hash_descriptor[x].name != NULL; x++) { | 24 for (x = 0; hash_descriptor[x].name != NULL; x++) { |
129 buf = XMALLOC(2 * hash_descriptor[x].blocksize + 1); | 25 buf = XMALLOC(2 * hash_descriptor[x].blocksize + 1); |
130 if (buf == NULL) { | 26 if (buf == NULL) { |
131 perror("can't alloc mem"); | 27 perror("can't alloc mem"); |
158 unsigned char *key, pt[MAXBLOCKSIZE]; | 54 unsigned char *key, pt[MAXBLOCKSIZE]; |
159 unsigned long x, y, z, w; | 55 unsigned long x, y, z, w; |
160 int err, kl, lastkl; | 56 int err, kl, lastkl; |
161 FILE *out; | 57 FILE *out; |
162 symmetric_key skey; | 58 symmetric_key skey; |
163 | 59 |
164 out = fopen("cipher_tv.txt", "w"); | 60 out = fopen("cipher_tv.txt", "w"); |
165 | 61 |
166 fprintf(out, | 62 fprintf(out, |
167 "Cipher Test Vectors\n\nThese are test encryptions with key of nn bytes '00 01 02 03 .. (nn-1)' and original PT of the same style.\n" | 63 "Cipher Test Vectors\n\nThese are test encryptions with key of nn bytes '00 01 02 03 .. (nn-1)' and original PT of the same style.\n" |
168 "The output of step N is used as the key and plaintext for step N+1 (key bytes repeated as required to fill the key)\n\n"); | 64 "The output of step N is used as the key and plaintext for step N+1 (key bytes repeated as required to fill the key)\n\n"); |
169 | 65 |
170 for (x = 0; cipher_descriptor[x].name != NULL; x++) { | 66 for (x = 0; cipher_descriptor[x].name != NULL; x++) { |
171 fprintf(out, "Cipher: %s\n", cipher_descriptor[x].name); | 67 fprintf(out, "Cipher: %s\n", cipher_descriptor[x].name); |
172 | 68 |
173 /* three modes, smallest, medium, large keys */ | 69 /* three modes, smallest, medium, large keys */ |
174 lastkl = 10000; | 70 lastkl = 10000; |
175 for (y = 0; y < 3; y++) { | 71 for (y = 0; y < 3; y++) { |
176 switch (y) { | 72 switch (y) { |
177 case 0: kl = cipher_descriptor[x].min_key_length; break; | 73 case 0: kl = cipher_descriptor[x].min_key_length; break; |
197 } | 93 } |
198 if ((err = cipher_descriptor[x].setup(key, kl, 0, &skey)) != CRYPT_OK) { | 94 if ((err = cipher_descriptor[x].setup(key, kl, 0, &skey)) != CRYPT_OK) { |
199 printf("setup error: %s\n", error_to_string(err)); | 95 printf("setup error: %s\n", error_to_string(err)); |
200 exit(EXIT_FAILURE); | 96 exit(EXIT_FAILURE); |
201 } | 97 } |
202 | 98 |
203 for (z = 0; (int)z < cipher_descriptor[x].block_length; z++) { | 99 for (z = 0; (int)z < cipher_descriptor[x].block_length; z++) { |
204 pt[z] = (unsigned char)z; | 100 pt[z] = (unsigned char)z; |
205 } | 101 } |
206 for (w = 0; w < 50; w++) { | 102 for (w = 0; w < 50; w++) { |
207 cipher_descriptor[x].ecb_encrypt(pt, pt, &skey); | 103 cipher_descriptor[x].ecb_encrypt(pt, pt, &skey); |
224 XFREE(key); | 120 XFREE(key); |
225 } | 121 } |
226 fprintf(out, "\n"); | 122 fprintf(out, "\n"); |
227 } | 123 } |
228 fclose(out); | 124 fclose(out); |
229 } | 125 } |
230 | 126 |
231 void hmac_gen(void) | 127 void hmac_gen(void) |
232 { | 128 { |
233 unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], *input; | 129 unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], *input; |
234 int x, y, z, err; | 130 int x, y, z, err; |
235 FILE *out; | 131 FILE *out; |
236 unsigned long len; | 132 unsigned long len; |
237 | 133 |
238 out = fopen("hmac_tv.txt", "w"); | 134 out = fopen("hmac_tv.txt", "w"); |
239 | 135 |
240 fprintf(out, | 136 fprintf(out, |
241 "LTC_HMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are LTC_HMACed. The initial key is\n" | 137 "HMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are HMACed. The initial key is\n" |
242 "of the same format (the same length as the HASH output size). The LTC_HMAC key in step N+1 is the LTC_HMAC output of\n" | 138 "of the same format (the same length as the HASH output size). The HMAC key in step N+1 is the HMAC output of\n" |
243 "step N.\n\n"); | 139 "step N.\n\n"); |
244 | 140 |
245 for (x = 0; hash_descriptor[x].name != NULL; x++) { | 141 for (x = 0; hash_descriptor[x].name != NULL; x++) { |
246 fprintf(out, "LTC_HMAC-%s\n", hash_descriptor[x].name); | 142 fprintf(out, "HMAC-%s\n", hash_descriptor[x].name); |
247 | 143 |
248 /* initial key */ | 144 /* initial key */ |
249 for (y = 0; y < (int)hash_descriptor[x].hashsize; y++) { | 145 for (y = 0; y < (int)hash_descriptor[x].hashsize; y++) { |
250 key[y] = (y&255); | 146 key[y] = (y&255); |
251 } | 147 } |
252 | 148 |
253 input = XMALLOC(hash_descriptor[x].blocksize * 2 + 1); | 149 input = XMALLOC(hash_descriptor[x].blocksize * 2 + 1); |
254 if (input == NULL) { | 150 if (input == NULL) { |
255 perror("Can't malloc memory"); | 151 perror("Can't malloc memory"); |
256 exit(EXIT_FAILURE); | 152 exit(EXIT_FAILURE); |
257 } | 153 } |
258 | 154 |
259 for (y = 0; y <= (int)(hash_descriptor[x].blocksize * 2); y++) { | 155 for (y = 0; y <= (int)(hash_descriptor[x].blocksize * 2); y++) { |
260 for (z = 0; z < y; z++) { | 156 for (z = 0; z < y; z++) { |
261 input[z] = (unsigned char)(z & 255); | 157 input[z] = (unsigned char)(z & 255); |
262 } | 158 } |
263 len = sizeof(output); | 159 len = sizeof(output); |
277 XFREE(input); | 173 XFREE(input); |
278 fprintf(out, "\n"); | 174 fprintf(out, "\n"); |
279 } | 175 } |
280 fclose(out); | 176 fclose(out); |
281 } | 177 } |
282 | 178 |
283 void omac_gen(void) | 179 void omac_gen(void) |
284 { | 180 { |
181 #ifdef LTC_OMAC | |
285 unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], input[MAXBLOCKSIZE*2+2]; | 182 unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], input[MAXBLOCKSIZE*2+2]; |
286 int err, x, y, z, kl; | 183 int err, x, y, z, kl; |
287 FILE *out; | 184 FILE *out; |
288 unsigned long len; | 185 unsigned long len; |
289 | 186 |
290 out = fopen("omac_tv.txt", "w"); | 187 out = fopen("omac_tv.txt", "w"); |
291 | 188 |
292 fprintf(out, | 189 fprintf(out, |
293 "LTC_OMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are LTC_OMAC'ed. The initial key is\n" | 190 "OMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are OMAC'ed. The initial key is\n" |
294 "of the same format (length specified per cipher). The LTC_OMAC key in step N+1 is the LTC_OMAC output of\n" | 191 "of the same format (length specified per cipher). The OMAC key in step N+1 is the OMAC output of\n" |
295 "step N (repeated as required to fill the array).\n\n"); | 192 "step N (repeated as required to fill the array).\n\n"); |
296 | 193 |
297 for (x = 0; cipher_descriptor[x].name != NULL; x++) { | 194 for (x = 0; cipher_descriptor[x].name != NULL; x++) { |
298 kl = cipher_descriptor[x].block_length; | 195 kl = cipher_descriptor[x].block_length; |
299 | 196 |
301 if (kl != 8 && kl != 16) continue; | 198 if (kl != 8 && kl != 16) continue; |
302 | 199 |
303 if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) { | 200 if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) { |
304 kl = cipher_descriptor[x].max_key_length; | 201 kl = cipher_descriptor[x].max_key_length; |
305 } | 202 } |
306 fprintf(out, "LTC_OMAC-%s (%d byte key)\n", cipher_descriptor[x].name, kl); | 203 fprintf(out, "OMAC-%s (%d byte key)\n", cipher_descriptor[x].name, kl); |
307 | 204 |
308 /* initial key/block */ | 205 /* initial key/block */ |
309 for (y = 0; y < kl; y++) { | 206 for (y = 0; y < kl; y++) { |
310 key[y] = (y & 255); | 207 key[y] = (y & 255); |
311 } | 208 } |
312 | 209 |
313 for (y = 0; y <= (int)(cipher_descriptor[x].block_length*2); y++) { | 210 for (y = 0; y <= (int)(cipher_descriptor[x].block_length*2); y++) { |
314 for (z = 0; z < y; z++) { | 211 for (z = 0; z < y; z++) { |
315 input[z] = (unsigned char)(z & 255); | 212 input[z] = (unsigned char)(z & 255); |
316 } | 213 } |
317 len = sizeof(output); | 214 len = sizeof(output); |
331 } | 228 } |
332 } | 229 } |
333 fprintf(out, "\n"); | 230 fprintf(out, "\n"); |
334 } | 231 } |
335 fclose(out); | 232 fclose(out); |
233 #endif | |
336 } | 234 } |
337 | 235 |
338 void pmac_gen(void) | 236 void pmac_gen(void) |
339 { | 237 { |
238 #ifdef LTC_PMAC | |
340 unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], input[MAXBLOCKSIZE*2+2]; | 239 unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], input[MAXBLOCKSIZE*2+2]; |
341 int err, x, y, z, kl; | 240 int err, x, y, z, kl; |
342 FILE *out; | 241 FILE *out; |
343 unsigned long len; | 242 unsigned long len; |
344 | 243 |
345 out = fopen("pmac_tv.txt", "w"); | 244 out = fopen("pmac_tv.txt", "w"); |
346 | 245 |
347 fprintf(out, | 246 fprintf(out, |
348 "PMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are LTC_OMAC'ed. The initial key is\n" | 247 "PMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are PMAC'ed. The initial key is\n" |
349 "of the same format (length specified per cipher). The LTC_OMAC key in step N+1 is the LTC_OMAC output of\n" | 248 "of the same format (length specified per cipher). The PMAC key in step N+1 is the PMAC output of\n" |
350 "step N (repeated as required to fill the array).\n\n"); | 249 "step N (repeated as required to fill the array).\n\n"); |
351 | 250 |
352 for (x = 0; cipher_descriptor[x].name != NULL; x++) { | 251 for (x = 0; cipher_descriptor[x].name != NULL; x++) { |
353 kl = cipher_descriptor[x].block_length; | 252 kl = cipher_descriptor[x].block_length; |
354 | 253 |
357 | 256 |
358 if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) { | 257 if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) { |
359 kl = cipher_descriptor[x].max_key_length; | 258 kl = cipher_descriptor[x].max_key_length; |
360 } | 259 } |
361 fprintf(out, "PMAC-%s (%d byte key)\n", cipher_descriptor[x].name, kl); | 260 fprintf(out, "PMAC-%s (%d byte key)\n", cipher_descriptor[x].name, kl); |
362 | 261 |
363 /* initial key/block */ | 262 /* initial key/block */ |
364 for (y = 0; y < kl; y++) { | 263 for (y = 0; y < kl; y++) { |
365 key[y] = (y & 255); | 264 key[y] = (y & 255); |
366 } | 265 } |
367 | 266 |
368 for (y = 0; y <= (int)(cipher_descriptor[x].block_length*2); y++) { | 267 for (y = 0; y <= (int)(cipher_descriptor[x].block_length*2); y++) { |
369 for (z = 0; z < y; z++) { | 268 for (z = 0; z < y; z++) { |
370 input[z] = (unsigned char)(z & 255); | 269 input[z] = (unsigned char)(z & 255); |
371 } | 270 } |
372 len = sizeof(output); | 271 len = sizeof(output); |
386 } | 285 } |
387 } | 286 } |
388 fprintf(out, "\n"); | 287 fprintf(out, "\n"); |
389 } | 288 } |
390 fclose(out); | 289 fclose(out); |
290 #endif | |
391 } | 291 } |
392 | 292 |
393 void eax_gen(void) | 293 void eax_gen(void) |
394 { | 294 { |
295 #ifdef LTC_EAX_MODE | |
395 int err, kl, x, y1, z; | 296 int err, kl, x, y1, z; |
396 FILE *out; | 297 FILE *out; |
397 unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2], header[MAXBLOCKSIZE*2], | 298 unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2], header[MAXBLOCKSIZE*2], |
398 plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE]; | 299 plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE]; |
399 unsigned long len; | 300 unsigned long len; |
400 | 301 |
401 out = fopen("eax_tv.txt", "w"); | 302 out = fopen("eax_tv.txt", "w"); |
402 fprintf(out, "EAX Test Vectors. Uses the 00010203...NN-1 pattern for header/nonce/plaintext/key. The outputs\n" | 303 fprintf(out, "EAX Test Vectors. Uses the 00010203...NN-1 pattern for header/nonce/plaintext/key. The outputs\n" |
416 | 317 |
417 /* the key */ | 318 /* the key */ |
418 for (z = 0; z < kl; z++) { | 319 for (z = 0; z < kl; z++) { |
419 key[z] = (z & 255); | 320 key[z] = (z & 255); |
420 } | 321 } |
421 | 322 |
422 for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){ | 323 for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){ |
423 for (z = 0; z < y1; z++) { | 324 for (z = 0; z < y1; z++) { |
424 plaintext[z] = (unsigned char)(z & 255); | 325 plaintext[z] = (unsigned char)(z & 255); |
425 nonce[z] = (unsigned char)(z & 255); | 326 nonce[z] = (unsigned char)(z & 255); |
426 header[z] = (unsigned char)(z & 255); | 327 header[z] = (unsigned char)(z & 255); |
446 } | 347 } |
447 } | 348 } |
448 fprintf(out, "\n"); | 349 fprintf(out, "\n"); |
449 } | 350 } |
450 fclose(out); | 351 fclose(out); |
352 #endif | |
451 } | 353 } |
452 | 354 |
453 void ocb_gen(void) | 355 void ocb_gen(void) |
454 { | 356 { |
357 #ifdef LTC_OCB_MODE | |
455 int err, kl, x, y1, z; | 358 int err, kl, x, y1, z; |
456 FILE *out; | 359 FILE *out; |
457 unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2], | 360 unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2], |
458 plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE]; | 361 plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE]; |
459 unsigned long len; | 362 unsigned long len; |
460 | 363 |
461 out = fopen("ocb_tv.txt", "w"); | 364 out = fopen("ocb_tv.txt", "w"); |
462 fprintf(out, "OCB Test Vectors. Uses the 00010203...NN-1 pattern for nonce/plaintext/key. The outputs\n" | 365 fprintf(out, "OCB Test Vectors. Uses the 00010203...NN-1 pattern for nonce/plaintext/key. The outputs\n" |
481 | 384 |
482 /* fixed nonce */ | 385 /* fixed nonce */ |
483 for (z = 0; z < cipher_descriptor[x].block_length; z++) { | 386 for (z = 0; z < cipher_descriptor[x].block_length; z++) { |
484 nonce[z] = z; | 387 nonce[z] = z; |
485 } | 388 } |
486 | 389 |
487 for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){ | 390 for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){ |
488 for (z = 0; z < y1; z++) { | 391 for (z = 0; z < y1; z++) { |
489 plaintext[z] = (unsigned char)(z & 255); | 392 plaintext[z] = (unsigned char)(z & 255); |
490 } | 393 } |
491 len = sizeof(tag); | 394 len = sizeof(tag); |
509 } | 412 } |
510 } | 413 } |
511 fprintf(out, "\n"); | 414 fprintf(out, "\n"); |
512 } | 415 } |
513 fclose(out); | 416 fclose(out); |
514 } | 417 #endif |
515 | 418 } |
419 | |
420 void ocb3_gen(void) | |
421 { | |
422 #ifdef LTC_OCB3_MODE | |
423 int err, kl, x, y1, z, noncelen; | |
424 FILE *out; | |
425 unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2], | |
426 plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE]; | |
427 unsigned long len; | |
428 | |
429 out = fopen("ocb3_tv.txt", "w"); | |
430 fprintf(out, "OCB3 Test Vectors. Uses the 00010203...NN-1 pattern for nonce/plaintext/key. The outputs\n" | |
431 "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n" | |
432 "step repeated sufficiently. The nonce is fixed throughout. AAD is fixed to 3 bytes (ASCII) 'AAD'.\n\n"); | |
433 | |
434 for (x = 0; cipher_descriptor[x].name != NULL; x++) { | |
435 kl = cipher_descriptor[x].block_length; | |
436 | |
437 /* skip ciphers which do not have 64 or 128 bit block sizes */ | |
438 if (kl != 16) continue; | |
439 | |
440 if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) { | |
441 kl = cipher_descriptor[x].max_key_length; | |
442 } | |
443 fprintf(out, "OCB3-%s (%d byte key)\n", cipher_descriptor[x].name, kl); | |
444 | |
445 /* the key */ | |
446 for (z = 0; z < kl; z++) { | |
447 key[z] = (z & 255); | |
448 } | |
449 | |
450 /* fixed nonce */ | |
451 noncelen = MIN(15, cipher_descriptor[x].block_length); | |
452 for (z = 0; z < noncelen; z++) { | |
453 nonce[z] = z; | |
454 } | |
455 | |
456 for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){ | |
457 for (z = 0; z < y1; z++) { | |
458 plaintext[z] = (unsigned char)(z & 255); | |
459 } | |
460 len = 16; | |
461 if ((err = ocb3_encrypt_authenticate_memory(x, key, kl, nonce, noncelen, (unsigned char*)"AAD", 3, plaintext, y1, plaintext, tag, &len)) != CRYPT_OK) { | |
462 printf("Error OCB3'ing: %s\n", error_to_string(err)); | |
463 exit(EXIT_FAILURE); | |
464 } | |
465 fprintf(out, "%3d: ", y1); | |
466 for (z = 0; z < y1; z++) { | |
467 fprintf(out, "%02X", plaintext[z]); | |
468 } | |
469 fprintf(out, ", "); | |
470 for (z = 0; z <(int)len; z++) { | |
471 fprintf(out, "%02X", tag[z]); | |
472 } | |
473 fprintf(out, "\n"); | |
474 | |
475 /* forward the key */ | |
476 for (z = 0; z < kl; z++) { | |
477 key[z] = tag[z % len]; | |
478 } | |
479 } | |
480 fprintf(out, "\n"); | |
481 } | |
482 fclose(out); | |
483 #endif | |
484 } | |
516 | 485 |
517 void ccm_gen(void) | 486 void ccm_gen(void) |
518 { | 487 { |
488 #ifdef LTC_CCM_MODE | |
519 int err, kl, x, y1, z; | 489 int err, kl, x, y1, z; |
520 FILE *out; | 490 FILE *out; |
521 unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2], | 491 unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2], |
522 plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE]; | 492 plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE]; |
523 unsigned long len; | 493 unsigned long len; |
524 | 494 |
525 out = fopen("ccm_tv.txt", "w"); | 495 out = fopen("ccm_tv.txt", "w"); |
526 fprintf(out, "CCM Test Vectors. Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key. The outputs\n" | 496 fprintf(out, "CCM Test Vectors. Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key. The outputs\n" |
545 | 515 |
546 /* fixed nonce */ | 516 /* fixed nonce */ |
547 for (z = 0; z < cipher_descriptor[x].block_length; z++) { | 517 for (z = 0; z < cipher_descriptor[x].block_length; z++) { |
548 nonce[z] = z; | 518 nonce[z] = z; |
549 } | 519 } |
550 | 520 |
551 for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){ | 521 for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){ |
552 for (z = 0; z < y1; z++) { | 522 for (z = 0; z < y1; z++) { |
553 plaintext[z] = (unsigned char)(z & 255); | 523 plaintext[z] = (unsigned char)(z & 255); |
554 } | 524 } |
555 len = sizeof(tag); | 525 len = sizeof(tag); |
556 if ((err = ccm_memory(x, key, kl, NULL, nonce, 13, plaintext, y1, plaintext, y1, plaintext, tag, &len, CCM_ENCRYPT)) != CRYPT_OK) { | 526 if ((err = ccm_memory(x, key, kl, NULL, nonce, 13, plaintext, y1, plaintext, y1, plaintext, tag, &len, CCM_ENCRYPT)) != CRYPT_OK) { |
557 printf("Error CCM'ing: %s\n", error_to_string(err)); | 527 printf("Error CCM'ing: %s\n", error_to_string(err)); |
558 exit(EXIT_FAILURE); | 528 exit(EXIT_FAILURE); |
559 } | 529 } |
530 if (len == 0) { | |
531 printf("Error CCM'ing: zero length\n"); | |
532 exit(EXIT_FAILURE); | |
533 } | |
560 fprintf(out, "%3d: ", y1); | 534 fprintf(out, "%3d: ", y1); |
561 for (z = 0; z < y1; z++) { | 535 for (z = 0; z < y1; z++) { |
562 fprintf(out, "%02X", plaintext[z]); | 536 fprintf(out, "%02X", plaintext[z]); |
563 } | 537 } |
564 fprintf(out, ", "); | 538 fprintf(out, ", "); |
573 } | 547 } |
574 } | 548 } |
575 fprintf(out, "\n"); | 549 fprintf(out, "\n"); |
576 } | 550 } |
577 fclose(out); | 551 fclose(out); |
552 #endif | |
578 } | 553 } |
579 | 554 |
580 void gcm_gen(void) | 555 void gcm_gen(void) |
581 { | 556 { |
557 #ifdef LTC_GCM_MODE | |
582 int err, kl, x, y1, z; | 558 int err, kl, x, y1, z; |
583 FILE *out; | 559 FILE *out; |
584 unsigned char key[MAXBLOCKSIZE], plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE]; | 560 unsigned char key[MAXBLOCKSIZE], plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE]; |
585 unsigned long len; | 561 unsigned long len; |
586 | 562 |
602 | 578 |
603 /* the key */ | 579 /* the key */ |
604 for (z = 0; z < kl; z++) { | 580 for (z = 0; z < kl; z++) { |
605 key[z] = (z & 255); | 581 key[z] = (z & 255); |
606 } | 582 } |
607 | 583 |
608 for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){ | 584 for (y1 = 1; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){ |
609 for (z = 0; z < y1; z++) { | 585 for (z = 0; z < y1; z++) { |
610 plaintext[z] = (unsigned char)(z & 255); | 586 plaintext[z] = (unsigned char)(z & 255); |
611 } | 587 } |
612 len = sizeof(tag); | 588 len = sizeof(tag); |
613 if ((err = gcm_memory(x, key, kl, plaintext, y1, plaintext, y1, plaintext, y1, plaintext, tag, &len, GCM_ENCRYPT)) != CRYPT_OK) { | 589 if ((err = gcm_memory(x, key, kl, plaintext, y1, plaintext, y1, plaintext, y1, plaintext, tag, &len, GCM_ENCRYPT)) != CRYPT_OK) { |
614 printf("Error GCM'ing: %s\n", error_to_string(err)); | 590 printf("Error GCM'ing: %s\n", error_to_string(err)); |
615 exit(EXIT_FAILURE); | 591 exit(EXIT_FAILURE); |
616 } | 592 } |
593 if (len == 0) { | |
594 printf("Error GCM'ing: zero length\n"); | |
595 exit(EXIT_FAILURE); | |
596 } | |
617 fprintf(out, "%3d: ", y1); | 597 fprintf(out, "%3d: ", y1); |
618 for (z = 0; z < y1; z++) { | 598 for (z = 0; z < y1; z++) { |
619 fprintf(out, "%02X", plaintext[z]); | 599 fprintf(out, "%02X", plaintext[z]); |
620 } | 600 } |
621 fprintf(out, ", "); | 601 fprintf(out, ", "); |
630 } | 610 } |
631 } | 611 } |
632 fprintf(out, "\n"); | 612 fprintf(out, "\n"); |
633 } | 613 } |
634 fclose(out); | 614 fclose(out); |
615 #endif | |
635 } | 616 } |
636 | 617 |
637 void base64_gen(void) | 618 void base64_gen(void) |
638 { | 619 { |
639 FILE *out; | 620 FILE *out; |
640 unsigned char dst[256], src[32]; | 621 unsigned char dst[256], src[32], ch; |
641 unsigned long x, y, len; | 622 unsigned long x, len; |
642 | 623 |
643 out = fopen("base64_tv.txt", "w"); | 624 out = fopen("base64_tv.txt", "w"); |
644 fprintf(out, "Base64 vectors. These are the base64 encodings of the strings 00,01,02...NN-1\n\n"); | 625 fprintf(out, "Base64 vectors. These are the base64 encodings of the strings 00,01,02...NN-1\n\n"); |
645 for (x = 0; x <= 32; x++) { | 626 for (x = 0; x <= 32; x++) { |
646 for (y = 0; y < x; y++) { | 627 for (ch = 0; ch < x; ch++) { |
647 src[y] = y; | 628 src[ch] = ch; |
648 } | 629 } |
649 len = sizeof(dst); | 630 len = sizeof(dst); |
650 base64_encode(src, x, dst, &len); | 631 base64_encode(src, x, dst, &len); |
651 fprintf(out, "%2lu: %s\n", x, dst); | 632 fprintf(out, "%2lu: %s\n", x, dst); |
652 } | 633 } |
679 | 660 |
680 mp_read_radix(order, (char *)ltc_ecc_sets[x].order, 16); | 661 mp_read_radix(order, (char *)ltc_ecc_sets[x].order, 16); |
681 mp_read_radix(modulus, (char *)ltc_ecc_sets[x].prime, 16); | 662 mp_read_radix(modulus, (char *)ltc_ecc_sets[x].prime, 16); |
682 mp_read_radix(G->x, (char *)ltc_ecc_sets[x].Gx, 16); | 663 mp_read_radix(G->x, (char *)ltc_ecc_sets[x].Gx, 16); |
683 mp_read_radix(G->y, (char *)ltc_ecc_sets[x].Gy, 16); | 664 mp_read_radix(G->y, (char *)ltc_ecc_sets[x].Gy, 16); |
684 mp_set(G->z, 1); | 665 mp_set(G->z, 1); |
685 | 666 |
686 while (mp_cmp(k, order) == LTC_MP_LT) { | 667 while (mp_cmp(k, order) == LTC_MP_LT) { |
687 ltc_mp.ecc_ptmul(k, G, R, modulus, 1); | 668 ltc_mp.ecc_ptmul(k, G, R, modulus, 1); |
688 mp_tohex(k, (char*)str); fprintf(out, "%s, ", (char*)str); | 669 mp_tohex(k, (char*)str); fprintf(out, "%s, ", (char*)str); |
689 mp_tohex(R->x, (char*)str); fprintf(out, "%s, ", (char*)str); | 670 mp_tohex(R->x, (char*)str); fprintf(out, "%s, ", (char*)str); |
697 fclose(out); | 678 fclose(out); |
698 } | 679 } |
699 | 680 |
700 void lrw_gen(void) | 681 void lrw_gen(void) |
701 { | 682 { |
683 #ifdef LTC_LRW_MODE | |
702 FILE *out; | 684 FILE *out; |
703 unsigned char tweak[16], key[16], iv[16], buf[1024]; | 685 unsigned char tweak[16], key[16], iv[16], buf[1024]; |
704 int x, y, err; | 686 int x, y, err; |
705 symmetric_LRW lrw; | 687 symmetric_LRW lrw; |
706 | 688 |
707 /* initialize default key and tweak */ | 689 /* initialize default key and tweak */ |
708 for (x = 0; x < 16; x++) { | 690 for (x = 0; x < 16; x++) { |
709 tweak[x] = key[x] = iv[x] = x; | 691 tweak[x] = key[x] = iv[x] = x; |
710 } | 692 } |
711 | 693 |
758 } | 740 } |
759 fprintf(out, "\n"); | 741 fprintf(out, "\n"); |
760 lrw_done(&lrw); | 742 lrw_done(&lrw); |
761 } | 743 } |
762 fclose(out); | 744 fclose(out); |
763 } | 745 #endif |
746 } | |
764 | 747 |
765 int main(void) | 748 int main(void) |
766 { | 749 { |
767 reg_algs(); | 750 register_all_ciphers(); |
751 register_all_hashes(); | |
752 register_all_prngs(); | |
753 #ifdef USE_LTM | |
754 ltc_mp = ltm_desc; | |
755 #elif defined(USE_TFM) | |
756 ltc_mp = tfm_desc; | |
757 #elif defined(USE_GMP) | |
758 ltc_mp = gmp_desc; | |
759 #elif defined(EXT_MATH_LIB) | |
760 extern ltc_math_descriptor EXT_MATH_LIB; | |
761 ltc_mp = EXT_MATH_LIB; | |
762 #else | |
763 fprintf(stderr, "No MPI provider available\n"); | |
764 exit(EXIT_FAILURE); | |
765 #endif | |
766 | |
768 printf("Generating hash vectors..."); fflush(stdout); hash_gen(); printf("done\n"); | 767 printf("Generating hash vectors..."); fflush(stdout); hash_gen(); printf("done\n"); |
769 printf("Generating cipher vectors..."); fflush(stdout); cipher_gen(); printf("done\n"); | 768 printf("Generating cipher vectors..."); fflush(stdout); cipher_gen(); printf("done\n"); |
770 printf("Generating LTC_HMAC vectors..."); fflush(stdout); hmac_gen(); printf("done\n"); | 769 printf("Generating HMAC vectors..."); fflush(stdout); hmac_gen(); printf("done\n"); |
771 printf("Generating LTC_OMAC vectors..."); fflush(stdout); omac_gen(); printf("done\n"); | 770 #ifdef LTC_OMAC |
771 printf("Generating OMAC vectors..."); fflush(stdout); omac_gen(); printf("done\n"); | |
772 #endif | |
773 #ifdef LTC_PMAC | |
772 printf("Generating PMAC vectors..."); fflush(stdout); pmac_gen(); printf("done\n"); | 774 printf("Generating PMAC vectors..."); fflush(stdout); pmac_gen(); printf("done\n"); |
775 #endif | |
776 #ifdef LTC_EAX_MODE | |
773 printf("Generating EAX vectors..."); fflush(stdout); eax_gen(); printf("done\n"); | 777 printf("Generating EAX vectors..."); fflush(stdout); eax_gen(); printf("done\n"); |
778 #endif | |
779 #ifdef LTC_OCB_MODE | |
774 printf("Generating OCB vectors..."); fflush(stdout); ocb_gen(); printf("done\n"); | 780 printf("Generating OCB vectors..."); fflush(stdout); ocb_gen(); printf("done\n"); |
781 #endif | |
782 #ifdef LTC_OCB3_MODE | |
783 printf("Generating OCB3 vectors..."); fflush(stdout); ocb3_gen(); printf("done\n"); | |
784 #endif | |
785 #ifdef LTC_CCM_MODE | |
775 printf("Generating CCM vectors..."); fflush(stdout); ccm_gen(); printf("done\n"); | 786 printf("Generating CCM vectors..."); fflush(stdout); ccm_gen(); printf("done\n"); |
787 #endif | |
788 #ifdef LTC_GCM_MODE | |
776 printf("Generating GCM vectors..."); fflush(stdout); gcm_gen(); printf("done\n"); | 789 printf("Generating GCM vectors..."); fflush(stdout); gcm_gen(); printf("done\n"); |
777 printf("Generating LTC_BASE64 vectors..."); fflush(stdout); base64_gen(); printf("done\n"); | 790 #endif |
791 printf("Generating BASE64 vectors..."); fflush(stdout); base64_gen(); printf("done\n"); | |
778 printf("Generating MATH vectors..."); fflush(stdout); math_gen(); printf("done\n"); | 792 printf("Generating MATH vectors..."); fflush(stdout); math_gen(); printf("done\n"); |
779 printf("Generating ECC vectors..."); fflush(stdout); ecc_gen(); printf("done\n"); | 793 printf("Generating ECC vectors..."); fflush(stdout); ecc_gen(); printf("done\n"); |
794 #ifdef LTC_LRW_MODE | |
780 printf("Generating LRW vectors..."); fflush(stdout); lrw_gen(); printf("done\n"); | 795 printf("Generating LRW vectors..."); fflush(stdout); lrw_gen(); printf("done\n"); |
796 #endif | |
781 return 0; | 797 return 0; |
782 } | 798 } |
783 | 799 |
784 /* $Source$ */ | 800 /* ref: $Format:%D$ */ |
785 /* $Revision$ */ | 801 /* git commit: $Format:%H$ */ |
786 /* $Date$ */ | 802 /* commit time: $Format:%ai$ */ |