tv_gen.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. #include <tomcrypt.h>
  2. void reg_algs(void)
  3. {
  4. int err;
  5. #ifdef RIJNDAEL
  6. register_cipher (&aes_desc);
  7. #endif
  8. #ifdef BLOWFISH
  9. register_cipher (&blowfish_desc);
  10. #endif
  11. #ifdef XTEA
  12. register_cipher (&xtea_desc);
  13. #endif
  14. #ifdef RC5
  15. register_cipher (&rc5_desc);
  16. #endif
  17. #ifdef RC6
  18. register_cipher (&rc6_desc);
  19. #endif
  20. #ifdef SAFERP
  21. register_cipher (&saferp_desc);
  22. #endif
  23. #ifdef TWOFISH
  24. register_cipher (&twofish_desc);
  25. #endif
  26. #ifdef SAFER
  27. register_cipher (&safer_k64_desc);
  28. register_cipher (&safer_sk64_desc);
  29. register_cipher (&safer_k128_desc);
  30. register_cipher (&safer_sk128_desc);
  31. #endif
  32. #ifdef RC2
  33. register_cipher (&rc2_desc);
  34. #endif
  35. #ifdef DES
  36. register_cipher (&des_desc);
  37. register_cipher (&des3_desc);
  38. #endif
  39. #ifdef CAST5
  40. register_cipher (&cast5_desc);
  41. #endif
  42. #ifdef NOEKEON
  43. register_cipher (&noekeon_desc);
  44. #endif
  45. #ifdef SKIPJACK
  46. register_cipher (&skipjack_desc);
  47. #endif
  48. #ifdef ANUBIS
  49. register_cipher (&anubis_desc);
  50. #endif
  51. #ifdef KHAZAD
  52. register_cipher (&khazad_desc);
  53. #endif
  54. #ifdef TIGER
  55. register_hash (&tiger_desc);
  56. #endif
  57. #ifdef MD2
  58. register_hash (&md2_desc);
  59. #endif
  60. #ifdef MD4
  61. register_hash (&md4_desc);
  62. #endif
  63. #ifdef MD5
  64. register_hash (&md5_desc);
  65. #endif
  66. #ifdef SHA1
  67. register_hash (&sha1_desc);
  68. #endif
  69. #ifdef SHA224
  70. register_hash (&sha224_desc);
  71. #endif
  72. #ifdef SHA256
  73. register_hash (&sha256_desc);
  74. #endif
  75. #ifdef SHA384
  76. register_hash (&sha384_desc);
  77. #endif
  78. #ifdef SHA512
  79. register_hash (&sha512_desc);
  80. #endif
  81. #ifdef RIPEMD128
  82. register_hash (&rmd128_desc);
  83. #endif
  84. #ifdef RIPEMD160
  85. register_hash (&rmd160_desc);
  86. #endif
  87. #ifdef WHIRLPOOL
  88. register_hash (&whirlpool_desc);
  89. #endif
  90. #ifdef CHC_HASH
  91. register_hash(&chc_desc);
  92. if ((err = chc_register(register_cipher(&aes_desc))) != CRYPT_OK) {
  93. printf("chc_register error: %s\n", error_to_string(err));
  94. exit(EXIT_FAILURE);
  95. }
  96. #endif
  97. }
  98. void hash_gen(void)
  99. {
  100. unsigned char md[MAXBLOCKSIZE], *buf;
  101. unsigned long outlen, x, y, z;
  102. FILE *out;
  103. int err;
  104. out = fopen("hash_tv.txt", "w");
  105. if (out == NULL) {
  106. perror("can't open hash_tv");
  107. }
  108. fprintf(out, "Hash Test Vectors:\n\nThese are the hashes of nn bytes '00 01 02 03 .. (nn-1)'\n\n");
  109. for (x = 0; hash_descriptor[x].name != NULL; x++) {
  110. buf = XMALLOC(2 * hash_descriptor[x].blocksize + 1);
  111. if (buf == NULL) {
  112. perror("can't alloc mem");
  113. exit(EXIT_FAILURE);
  114. }
  115. fprintf(out, "Hash: %s\n", hash_descriptor[x].name);
  116. for (y = 0; y <= (hash_descriptor[x].blocksize * 2); y++) {
  117. for (z = 0; z < y; z++) {
  118. buf[z] = (unsigned char)(z & 255);
  119. }
  120. outlen = sizeof(md);
  121. if ((err = hash_memory(x, buf, y, md, &outlen)) != CRYPT_OK) {
  122. printf("hash_memory error: %s\n", error_to_string(err));
  123. exit(EXIT_FAILURE);
  124. }
  125. fprintf(out, "%3lu: ", y);
  126. for (z = 0; z < outlen; z++) {
  127. fprintf(out, "%02X", md[z]);
  128. }
  129. fprintf(out, "\n");
  130. }
  131. fprintf(out, "\n");
  132. XFREE(buf);
  133. }
  134. fclose(out);
  135. }
  136. void cipher_gen(void)
  137. {
  138. unsigned char *key, pt[MAXBLOCKSIZE];
  139. unsigned long x, y, z, w;
  140. int err, kl, lastkl;
  141. FILE *out;
  142. symmetric_key skey;
  143. out = fopen("cipher_tv.txt", "w");
  144. fprintf(out,
  145. "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"
  146. "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");
  147. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  148. fprintf(out, "Cipher: %s\n", cipher_descriptor[x].name);
  149. /* three modes, smallest, medium, large keys */
  150. lastkl = 10000;
  151. for (y = 0; y < 3; y++) {
  152. switch (y) {
  153. case 0: kl = cipher_descriptor[x].min_key_length; break;
  154. case 1: kl = (cipher_descriptor[x].min_key_length + cipher_descriptor[x].max_key_length)/2; break;
  155. case 2: kl = cipher_descriptor[x].max_key_length; break;
  156. }
  157. if ((err = cipher_descriptor[x].keysize(&kl)) != CRYPT_OK) {
  158. printf("keysize error: %s\n", error_to_string(err));
  159. exit(EXIT_FAILURE);
  160. }
  161. if (kl == lastkl) break;
  162. lastkl = kl;
  163. fprintf(out, "Key Size: %d bytes\n", kl);
  164. key = XMALLOC(kl);
  165. if (key == NULL) {
  166. perror("can't malloc memory");
  167. exit(EXIT_FAILURE);
  168. }
  169. for (z = 0; (int)z < kl; z++) {
  170. key[z] = (unsigned char)z;
  171. }
  172. if ((err = cipher_descriptor[x].setup(key, kl, 0, &skey)) != CRYPT_OK) {
  173. printf("setup error: %s\n", error_to_string(err));
  174. exit(EXIT_FAILURE);
  175. }
  176. for (z = 0; (int)z < cipher_descriptor[x].block_length; z++) {
  177. pt[z] = (unsigned char)z;
  178. }
  179. for (w = 0; w < 50; w++) {
  180. cipher_descriptor[x].ecb_encrypt(pt, pt, &skey);
  181. fprintf(out, "%2lu: ", w);
  182. for (z = 0; (int)z < cipher_descriptor[x].block_length; z++) {
  183. fprintf(out, "%02X", pt[z]);
  184. }
  185. fprintf(out, "\n");
  186. /* reschedule a new key */
  187. for (z = 0; z < (unsigned long)kl; z++) {
  188. key[z] = pt[z % cipher_descriptor[x].block_length];
  189. }
  190. if ((err = cipher_descriptor[x].setup(key, kl, 0, &skey)) != CRYPT_OK) {
  191. printf("cipher setup2 error: %s\n", error_to_string(err));
  192. exit(EXIT_FAILURE);
  193. }
  194. }
  195. fprintf(out, "\n");
  196. XFREE(key);
  197. }
  198. fprintf(out, "\n");
  199. }
  200. fclose(out);
  201. }
  202. void hmac_gen(void)
  203. {
  204. unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], *input;
  205. int x, y, z, err;
  206. FILE *out;
  207. unsigned long len;
  208. out = fopen("hmac_tv.txt", "w");
  209. fprintf(out,
  210. "HMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are HMACed. The initial key is\n"
  211. "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"
  212. "step N.\n\n");
  213. for (x = 0; hash_descriptor[x].name != NULL; x++) {
  214. fprintf(out, "HMAC-%s\n", hash_descriptor[x].name);
  215. /* initial key */
  216. for (y = 0; y < (int)hash_descriptor[x].hashsize; y++) {
  217. key[y] = (y&255);
  218. }
  219. input = XMALLOC(hash_descriptor[x].blocksize * 2 + 1);
  220. if (input == NULL) {
  221. perror("Can't malloc memory");
  222. exit(EXIT_FAILURE);
  223. }
  224. for (y = 0; y <= (int)(hash_descriptor[x].blocksize * 2); y++) {
  225. for (z = 0; z < y; z++) {
  226. input[z] = (unsigned char)(z & 255);
  227. }
  228. len = sizeof(output);
  229. if ((err = hmac_memory(x, key, hash_descriptor[x].hashsize, input, y, output, &len)) != CRYPT_OK) {
  230. printf("Error hmacing: %s\n", error_to_string(err));
  231. exit(EXIT_FAILURE);
  232. }
  233. fprintf(out, "%3d: ", y);
  234. for (z = 0; z <(int) len; z++) {
  235. fprintf(out, "%02X", output[z]);
  236. }
  237. fprintf(out, "\n");
  238. /* forward the key */
  239. memcpy(key, output, hash_descriptor[x].hashsize);
  240. }
  241. XFREE(input);
  242. fprintf(out, "\n");
  243. }
  244. fclose(out);
  245. }
  246. void omac_gen(void)
  247. {
  248. unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], input[MAXBLOCKSIZE*2+2];
  249. int err, x, y, z, kl;
  250. FILE *out;
  251. unsigned long len;
  252. out = fopen("omac_tv.txt", "w");
  253. fprintf(out,
  254. "OMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are OMAC'ed. The initial key is\n"
  255. "of the same format (length specified per cipher). The OMAC key in step N+1 is the OMAC output of\n"
  256. "step N (repeated as required to fill the array).\n\n");
  257. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  258. kl = cipher_descriptor[x].block_length;
  259. /* skip ciphers which do not have 64 or 128 bit block sizes */
  260. if (kl != 8 && kl != 16) continue;
  261. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  262. kl = cipher_descriptor[x].max_key_length;
  263. }
  264. fprintf(out, "OMAC-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  265. /* initial key/block */
  266. for (y = 0; y < kl; y++) {
  267. key[y] = (y & 255);
  268. }
  269. for (y = 0; y <= (int)(cipher_descriptor[x].block_length*2); y++) {
  270. for (z = 0; z < y; z++) {
  271. input[z] = (unsigned char)(z & 255);
  272. }
  273. len = sizeof(output);
  274. if ((err = omac_memory(x, key, kl, input, y, output, &len)) != CRYPT_OK) {
  275. printf("Error omacing: %s\n", error_to_string(err));
  276. exit(EXIT_FAILURE);
  277. }
  278. fprintf(out, "%3d: ", y);
  279. for (z = 0; z <(int)len; z++) {
  280. fprintf(out, "%02X", output[z]);
  281. }
  282. fprintf(out, "\n");
  283. /* forward the key */
  284. for (z = 0; z < kl; z++) {
  285. key[z] = output[z % len];
  286. }
  287. }
  288. fprintf(out, "\n");
  289. }
  290. fclose(out);
  291. }
  292. void pmac_gen(void)
  293. {
  294. unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], input[MAXBLOCKSIZE*2+2];
  295. int err, x, y, z, kl;
  296. FILE *out;
  297. unsigned long len;
  298. out = fopen("pmac_tv.txt", "w");
  299. fprintf(out,
  300. "PMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are OMAC'ed. The initial key is\n"
  301. "of the same format (length specified per cipher). The OMAC key in step N+1 is the OMAC output of\n"
  302. "step N (repeated as required to fill the array).\n\n");
  303. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  304. kl = cipher_descriptor[x].block_length;
  305. /* skip ciphers which do not have 64 or 128 bit block sizes */
  306. if (kl != 8 && kl != 16) continue;
  307. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  308. kl = cipher_descriptor[x].max_key_length;
  309. }
  310. fprintf(out, "PMAC-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  311. /* initial key/block */
  312. for (y = 0; y < kl; y++) {
  313. key[y] = (y & 255);
  314. }
  315. for (y = 0; y <= (int)(cipher_descriptor[x].block_length*2); y++) {
  316. for (z = 0; z < y; z++) {
  317. input[z] = (unsigned char)(z & 255);
  318. }
  319. len = sizeof(output);
  320. if ((err = pmac_memory(x, key, kl, input, y, output, &len)) != CRYPT_OK) {
  321. printf("Error omacing: %s\n", error_to_string(err));
  322. exit(EXIT_FAILURE);
  323. }
  324. fprintf(out, "%3d: ", y);
  325. for (z = 0; z <(int)len; z++) {
  326. fprintf(out, "%02X", output[z]);
  327. }
  328. fprintf(out, "\n");
  329. /* forward the key */
  330. for (z = 0; z < kl; z++) {
  331. key[z] = output[z % len];
  332. }
  333. }
  334. fprintf(out, "\n");
  335. }
  336. fclose(out);
  337. }
  338. void eax_gen(void)
  339. {
  340. int err, kl, x, y1, z;
  341. FILE *out;
  342. unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2], header[MAXBLOCKSIZE*2],
  343. plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
  344. unsigned long len;
  345. out = fopen("eax_tv.txt", "w");
  346. fprintf(out, "EAX Test Vectors. Uses the 00010203...NN-1 pattern for header/nonce/plaintext/key. The outputs\n"
  347. "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
  348. "step repeated sufficiently.\n\n");
  349. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  350. kl = cipher_descriptor[x].block_length;
  351. /* skip ciphers which do not have 64 or 128 bit block sizes */
  352. if (kl != 8 && kl != 16) continue;
  353. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  354. kl = cipher_descriptor[x].max_key_length;
  355. }
  356. fprintf(out, "EAX-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  357. /* the key */
  358. for (z = 0; z < kl; z++) {
  359. key[z] = (z & 255);
  360. }
  361. for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
  362. for (z = 0; z < y1; z++) {
  363. plaintext[z] = (unsigned char)(z & 255);
  364. nonce[z] = (unsigned char)(z & 255);
  365. header[z] = (unsigned char)(z & 255);
  366. }
  367. len = sizeof(tag);
  368. if ((err = eax_encrypt_authenticate_memory(x, key, kl, nonce, y1, header, y1, plaintext, y1, plaintext, tag, &len)) != CRYPT_OK) {
  369. printf("Error EAX'ing: %s\n", error_to_string(err));
  370. exit(EXIT_FAILURE);
  371. }
  372. fprintf(out, "%3d: ", y1);
  373. for (z = 0; z < y1; z++) {
  374. fprintf(out, "%02X", plaintext[z]);
  375. }
  376. fprintf(out, ", ");
  377. for (z = 0; z <(int)len; z++) {
  378. fprintf(out, "%02X", tag[z]);
  379. }
  380. fprintf(out, "\n");
  381. /* forward the key */
  382. for (z = 0; z < kl; z++) {
  383. key[z] = tag[z % len];
  384. }
  385. }
  386. fprintf(out, "\n");
  387. }
  388. fclose(out);
  389. }
  390. void ocb_gen(void)
  391. {
  392. int err, kl, x, y1, z;
  393. FILE *out;
  394. unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2],
  395. plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
  396. unsigned long len;
  397. out = fopen("ocb_tv.txt", "w");
  398. fprintf(out, "OCB Test Vectors. Uses the 00010203...NN-1 pattern for nonce/plaintext/key. The outputs\n"
  399. "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
  400. "step repeated sufficiently. The nonce is fixed throughout.\n\n");
  401. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  402. kl = cipher_descriptor[x].block_length;
  403. /* skip ciphers which do not have 64 or 128 bit block sizes */
  404. if (kl != 8 && kl != 16) continue;
  405. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  406. kl = cipher_descriptor[x].max_key_length;
  407. }
  408. fprintf(out, "OCB-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  409. /* the key */
  410. for (z = 0; z < kl; z++) {
  411. key[z] = (z & 255);
  412. }
  413. /* fixed nonce */
  414. for (z = 0; z < cipher_descriptor[x].block_length; z++) {
  415. nonce[z] = z;
  416. }
  417. for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
  418. for (z = 0; z < y1; z++) {
  419. plaintext[z] = (unsigned char)(z & 255);
  420. }
  421. len = sizeof(tag);
  422. if ((err = ocb_encrypt_authenticate_memory(x, key, kl, nonce, plaintext, y1, plaintext, tag, &len)) != CRYPT_OK) {
  423. printf("Error OCB'ing: %s\n", error_to_string(err));
  424. exit(EXIT_FAILURE);
  425. }
  426. fprintf(out, "%3d: ", y1);
  427. for (z = 0; z < y1; z++) {
  428. fprintf(out, "%02X", plaintext[z]);
  429. }
  430. fprintf(out, ", ");
  431. for (z = 0; z <(int)len; z++) {
  432. fprintf(out, "%02X", tag[z]);
  433. }
  434. fprintf(out, "\n");
  435. /* forward the key */
  436. for (z = 0; z < kl; z++) {
  437. key[z] = tag[z % len];
  438. }
  439. }
  440. fprintf(out, "\n");
  441. }
  442. fclose(out);
  443. }
  444. void ccm_gen(void)
  445. {
  446. int err, kl, x, y1, z;
  447. FILE *out;
  448. unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2],
  449. plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
  450. unsigned long len;
  451. out = fopen("ccm_tv.txt", "w");
  452. fprintf(out, "CCM Test Vectors. Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key. The outputs\n"
  453. "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
  454. "step repeated sufficiently. The nonce is fixed throughout at 13 bytes 000102...\n\n");
  455. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  456. kl = cipher_descriptor[x].block_length;
  457. /* skip ciphers which do not have 128 bit block sizes */
  458. if (kl != 16) continue;
  459. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  460. kl = cipher_descriptor[x].max_key_length;
  461. }
  462. fprintf(out, "CCM-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  463. /* the key */
  464. for (z = 0; z < kl; z++) {
  465. key[z] = (z & 255);
  466. }
  467. /* fixed nonce */
  468. for (z = 0; z < cipher_descriptor[x].block_length; z++) {
  469. nonce[z] = z;
  470. }
  471. for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
  472. for (z = 0; z < y1; z++) {
  473. plaintext[z] = (unsigned char)(z & 255);
  474. }
  475. len = sizeof(tag);
  476. if ((err = ccm_memory(x, key, kl, nonce, 13, plaintext, y1, plaintext, y1, plaintext, tag, &len, CCM_ENCRYPT)) != CRYPT_OK) {
  477. printf("Error CCM'ing: %s\n", error_to_string(err));
  478. exit(EXIT_FAILURE);
  479. }
  480. fprintf(out, "%3d: ", y1);
  481. for (z = 0; z < y1; z++) {
  482. fprintf(out, "%02X", plaintext[z]);
  483. }
  484. fprintf(out, ", ");
  485. for (z = 0; z <(int)len; z++) {
  486. fprintf(out, "%02X", tag[z]);
  487. }
  488. fprintf(out, "\n");
  489. /* forward the key */
  490. for (z = 0; z < kl; z++) {
  491. key[z] = tag[z % len];
  492. }
  493. }
  494. fprintf(out, "\n");
  495. }
  496. fclose(out);
  497. }
  498. void gcm_gen(void)
  499. {
  500. int err, kl, x, y1, z;
  501. FILE *out;
  502. unsigned char key[MAXBLOCKSIZE], plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
  503. unsigned long len;
  504. out = fopen("gcm_tv.txt", "w");
  505. fprintf(out, "GCM Test Vectors. Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key. The outputs\n"
  506. "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
  507. "step repeated sufficiently. The nonce is fixed throughout at 13 bytes 000102...\n\n");
  508. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  509. kl = cipher_descriptor[x].block_length;
  510. /* skip ciphers which do not have 128 bit block sizes */
  511. if (kl != 16) continue;
  512. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  513. kl = cipher_descriptor[x].max_key_length;
  514. }
  515. fprintf(out, "GCM-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  516. /* the key */
  517. for (z = 0; z < kl; z++) {
  518. key[z] = (z & 255);
  519. }
  520. for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
  521. for (z = 0; z < y1; z++) {
  522. plaintext[z] = (unsigned char)(z & 255);
  523. }
  524. len = sizeof(tag);
  525. if ((err = gcm_memory(x, key, kl, plaintext, y1, plaintext, y1, plaintext, y1, plaintext, tag, &len, GCM_ENCRYPT)) != CRYPT_OK) {
  526. printf("Error GCM'ing: %s\n", error_to_string(err));
  527. exit(EXIT_FAILURE);
  528. }
  529. fprintf(out, "%3d: ", y1);
  530. for (z = 0; z < y1; z++) {
  531. fprintf(out, "%02X", plaintext[z]);
  532. }
  533. fprintf(out, ", ");
  534. for (z = 0; z <(int)len; z++) {
  535. fprintf(out, "%02X", tag[z]);
  536. }
  537. fprintf(out, "\n");
  538. /* forward the key */
  539. for (z = 0; z < kl; z++) {
  540. key[z] = tag[z % len];
  541. }
  542. }
  543. fprintf(out, "\n");
  544. }
  545. fclose(out);
  546. }
  547. void base64_gen(void)
  548. {
  549. FILE *out;
  550. unsigned char dst[256], src[32];
  551. unsigned long x, y, len;
  552. out = fopen("base64_tv.txt", "w");
  553. fprintf(out, "Base64 vectors. These are the base64 encodings of the strings 00,01,02...NN-1\n\n");
  554. for (x = 0; x <= 32; x++) {
  555. for (y = 0; y < x; y++) {
  556. src[y] = y;
  557. }
  558. len = sizeof(dst);
  559. base64_encode(src, x, dst, &len);
  560. fprintf(out, "%2lu: %s\n", x, dst);
  561. }
  562. fclose(out);
  563. }
  564. int main(void)
  565. {
  566. reg_algs();
  567. printf("Generating hash vectors..."); fflush(stdout); hash_gen(); printf("done\n");
  568. printf("Generating cipher vectors..."); fflush(stdout); cipher_gen(); printf("done\n");
  569. printf("Generating HMAC vectors..."); fflush(stdout); hmac_gen(); printf("done\n");
  570. printf("Generating OMAC vectors..."); fflush(stdout); omac_gen(); printf("done\n");
  571. printf("Generating PMAC vectors..."); fflush(stdout); pmac_gen(); printf("done\n");
  572. printf("Generating EAX vectors..."); fflush(stdout); eax_gen(); printf("done\n");
  573. printf("Generating OCB vectors..."); fflush(stdout); ocb_gen(); printf("done\n");
  574. printf("Generating CCM vectors..."); fflush(stdout); ccm_gen(); printf("done\n");
  575. printf("Generating GCM vectors..."); fflush(stdout); gcm_gen(); printf("done\n");
  576. printf("Generating BASE64 vectors..."); fflush(stdout); base64_gen(); printf("done\n");
  577. return 0;
  578. }