tv_gen.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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. #ifdef USE_LTM
  98. ltc_mp = ltm_desc;
  99. #elif defined(USE_TFM)
  100. ltc_mp = tfm_desc;
  101. #else
  102. extern ltc_math_descriptor EXT_MATH_LIB;
  103. ltc_mp = EXT_MATH_LIB;
  104. #endif
  105. }
  106. void hash_gen(void)
  107. {
  108. unsigned char md[MAXBLOCKSIZE], *buf;
  109. unsigned long outlen, x, y, z;
  110. FILE *out;
  111. int err;
  112. out = fopen("hash_tv.txt", "w");
  113. if (out == NULL) {
  114. perror("can't open hash_tv");
  115. }
  116. fprintf(out, "Hash Test Vectors:\n\nThese are the hashes of nn bytes '00 01 02 03 .. (nn-1)'\n\n");
  117. for (x = 0; hash_descriptor[x].name != NULL; x++) {
  118. buf = XMALLOC(2 * hash_descriptor[x].blocksize + 1);
  119. if (buf == NULL) {
  120. perror("can't alloc mem");
  121. exit(EXIT_FAILURE);
  122. }
  123. fprintf(out, "Hash: %s\n", hash_descriptor[x].name);
  124. for (y = 0; y <= (hash_descriptor[x].blocksize * 2); y++) {
  125. for (z = 0; z < y; z++) {
  126. buf[z] = (unsigned char)(z & 255);
  127. }
  128. outlen = sizeof(md);
  129. if ((err = hash_memory(x, buf, y, md, &outlen)) != CRYPT_OK) {
  130. printf("hash_memory error: %s\n", error_to_string(err));
  131. exit(EXIT_FAILURE);
  132. }
  133. fprintf(out, "%3lu: ", y);
  134. for (z = 0; z < outlen; z++) {
  135. fprintf(out, "%02X", md[z]);
  136. }
  137. fprintf(out, "\n");
  138. }
  139. fprintf(out, "\n");
  140. XFREE(buf);
  141. }
  142. fclose(out);
  143. }
  144. void cipher_gen(void)
  145. {
  146. unsigned char *key, pt[MAXBLOCKSIZE];
  147. unsigned long x, y, z, w;
  148. int err, kl, lastkl;
  149. FILE *out;
  150. symmetric_key skey;
  151. out = fopen("cipher_tv.txt", "w");
  152. fprintf(out,
  153. "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"
  154. "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");
  155. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  156. fprintf(out, "Cipher: %s\n", cipher_descriptor[x].name);
  157. /* three modes, smallest, medium, large keys */
  158. lastkl = 10000;
  159. for (y = 0; y < 3; y++) {
  160. switch (y) {
  161. case 0: kl = cipher_descriptor[x].min_key_length; break;
  162. case 1: kl = (cipher_descriptor[x].min_key_length + cipher_descriptor[x].max_key_length)/2; break;
  163. case 2: kl = cipher_descriptor[x].max_key_length; break;
  164. }
  165. if ((err = cipher_descriptor[x].keysize(&kl)) != CRYPT_OK) {
  166. printf("keysize error: %s\n", error_to_string(err));
  167. exit(EXIT_FAILURE);
  168. }
  169. if (kl == lastkl) break;
  170. lastkl = kl;
  171. fprintf(out, "Key Size: %d bytes\n", kl);
  172. key = XMALLOC(kl);
  173. if (key == NULL) {
  174. perror("can't malloc memory");
  175. exit(EXIT_FAILURE);
  176. }
  177. for (z = 0; (int)z < kl; z++) {
  178. key[z] = (unsigned char)z;
  179. }
  180. if ((err = cipher_descriptor[x].setup(key, kl, 0, &skey)) != CRYPT_OK) {
  181. printf("setup error: %s\n", error_to_string(err));
  182. exit(EXIT_FAILURE);
  183. }
  184. for (z = 0; (int)z < cipher_descriptor[x].block_length; z++) {
  185. pt[z] = (unsigned char)z;
  186. }
  187. for (w = 0; w < 50; w++) {
  188. cipher_descriptor[x].ecb_encrypt(pt, pt, &skey);
  189. fprintf(out, "%2lu: ", w);
  190. for (z = 0; (int)z < cipher_descriptor[x].block_length; z++) {
  191. fprintf(out, "%02X", pt[z]);
  192. }
  193. fprintf(out, "\n");
  194. /* reschedule a new key */
  195. for (z = 0; z < (unsigned long)kl; z++) {
  196. key[z] = pt[z % cipher_descriptor[x].block_length];
  197. }
  198. if ((err = cipher_descriptor[x].setup(key, kl, 0, &skey)) != CRYPT_OK) {
  199. printf("cipher setup2 error: %s\n", error_to_string(err));
  200. exit(EXIT_FAILURE);
  201. }
  202. }
  203. fprintf(out, "\n");
  204. XFREE(key);
  205. }
  206. fprintf(out, "\n");
  207. }
  208. fclose(out);
  209. }
  210. void hmac_gen(void)
  211. {
  212. unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], *input;
  213. int x, y, z, err;
  214. FILE *out;
  215. unsigned long len;
  216. out = fopen("hmac_tv.txt", "w");
  217. fprintf(out,
  218. "HMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are HMACed. The initial key is\n"
  219. "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"
  220. "step N.\n\n");
  221. for (x = 0; hash_descriptor[x].name != NULL; x++) {
  222. fprintf(out, "HMAC-%s\n", hash_descriptor[x].name);
  223. /* initial key */
  224. for (y = 0; y < (int)hash_descriptor[x].hashsize; y++) {
  225. key[y] = (y&255);
  226. }
  227. input = XMALLOC(hash_descriptor[x].blocksize * 2 + 1);
  228. if (input == NULL) {
  229. perror("Can't malloc memory");
  230. exit(EXIT_FAILURE);
  231. }
  232. for (y = 0; y <= (int)(hash_descriptor[x].blocksize * 2); y++) {
  233. for (z = 0; z < y; z++) {
  234. input[z] = (unsigned char)(z & 255);
  235. }
  236. len = sizeof(output);
  237. if ((err = hmac_memory(x, key, hash_descriptor[x].hashsize, input, y, output, &len)) != CRYPT_OK) {
  238. printf("Error hmacing: %s\n", error_to_string(err));
  239. exit(EXIT_FAILURE);
  240. }
  241. fprintf(out, "%3d: ", y);
  242. for (z = 0; z <(int) len; z++) {
  243. fprintf(out, "%02X", output[z]);
  244. }
  245. fprintf(out, "\n");
  246. /* forward the key */
  247. memcpy(key, output, hash_descriptor[x].hashsize);
  248. }
  249. XFREE(input);
  250. fprintf(out, "\n");
  251. }
  252. fclose(out);
  253. }
  254. void omac_gen(void)
  255. {
  256. unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], input[MAXBLOCKSIZE*2+2];
  257. int err, x, y, z, kl;
  258. FILE *out;
  259. unsigned long len;
  260. out = fopen("omac_tv.txt", "w");
  261. fprintf(out,
  262. "OMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are OMAC'ed. The initial key is\n"
  263. "of the same format (length specified per cipher). The OMAC key in step N+1 is the OMAC output of\n"
  264. "step N (repeated as required to fill the array).\n\n");
  265. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  266. kl = cipher_descriptor[x].block_length;
  267. /* skip ciphers which do not have 64 or 128 bit block sizes */
  268. if (kl != 8 && kl != 16) continue;
  269. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  270. kl = cipher_descriptor[x].max_key_length;
  271. }
  272. fprintf(out, "OMAC-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  273. /* initial key/block */
  274. for (y = 0; y < kl; y++) {
  275. key[y] = (y & 255);
  276. }
  277. for (y = 0; y <= (int)(cipher_descriptor[x].block_length*2); y++) {
  278. for (z = 0; z < y; z++) {
  279. input[z] = (unsigned char)(z & 255);
  280. }
  281. len = sizeof(output);
  282. if ((err = omac_memory(x, key, kl, input, y, output, &len)) != CRYPT_OK) {
  283. printf("Error omacing: %s\n", error_to_string(err));
  284. exit(EXIT_FAILURE);
  285. }
  286. fprintf(out, "%3d: ", y);
  287. for (z = 0; z <(int)len; z++) {
  288. fprintf(out, "%02X", output[z]);
  289. }
  290. fprintf(out, "\n");
  291. /* forward the key */
  292. for (z = 0; z < kl; z++) {
  293. key[z] = output[z % len];
  294. }
  295. }
  296. fprintf(out, "\n");
  297. }
  298. fclose(out);
  299. }
  300. void pmac_gen(void)
  301. {
  302. unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], input[MAXBLOCKSIZE*2+2];
  303. int err, x, y, z, kl;
  304. FILE *out;
  305. unsigned long len;
  306. out = fopen("pmac_tv.txt", "w");
  307. fprintf(out,
  308. "PMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are OMAC'ed. The initial key is\n"
  309. "of the same format (length specified per cipher). The OMAC key in step N+1 is the OMAC output of\n"
  310. "step N (repeated as required to fill the array).\n\n");
  311. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  312. kl = cipher_descriptor[x].block_length;
  313. /* skip ciphers which do not have 64 or 128 bit block sizes */
  314. if (kl != 8 && kl != 16) continue;
  315. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  316. kl = cipher_descriptor[x].max_key_length;
  317. }
  318. fprintf(out, "PMAC-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  319. /* initial key/block */
  320. for (y = 0; y < kl; y++) {
  321. key[y] = (y & 255);
  322. }
  323. for (y = 0; y <= (int)(cipher_descriptor[x].block_length*2); y++) {
  324. for (z = 0; z < y; z++) {
  325. input[z] = (unsigned char)(z & 255);
  326. }
  327. len = sizeof(output);
  328. if ((err = pmac_memory(x, key, kl, input, y, output, &len)) != CRYPT_OK) {
  329. printf("Error omacing: %s\n", error_to_string(err));
  330. exit(EXIT_FAILURE);
  331. }
  332. fprintf(out, "%3d: ", y);
  333. for (z = 0; z <(int)len; z++) {
  334. fprintf(out, "%02X", output[z]);
  335. }
  336. fprintf(out, "\n");
  337. /* forward the key */
  338. for (z = 0; z < kl; z++) {
  339. key[z] = output[z % len];
  340. }
  341. }
  342. fprintf(out, "\n");
  343. }
  344. fclose(out);
  345. }
  346. void eax_gen(void)
  347. {
  348. int err, kl, x, y1, z;
  349. FILE *out;
  350. unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2], header[MAXBLOCKSIZE*2],
  351. plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
  352. unsigned long len;
  353. out = fopen("eax_tv.txt", "w");
  354. fprintf(out, "EAX Test Vectors. Uses the 00010203...NN-1 pattern for header/nonce/plaintext/key. The outputs\n"
  355. "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
  356. "step repeated sufficiently.\n\n");
  357. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  358. kl = cipher_descriptor[x].block_length;
  359. /* skip ciphers which do not have 64 or 128 bit block sizes */
  360. if (kl != 8 && kl != 16) continue;
  361. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  362. kl = cipher_descriptor[x].max_key_length;
  363. }
  364. fprintf(out, "EAX-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  365. /* the key */
  366. for (z = 0; z < kl; z++) {
  367. key[z] = (z & 255);
  368. }
  369. for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
  370. for (z = 0; z < y1; z++) {
  371. plaintext[z] = (unsigned char)(z & 255);
  372. nonce[z] = (unsigned char)(z & 255);
  373. header[z] = (unsigned char)(z & 255);
  374. }
  375. len = sizeof(tag);
  376. if ((err = eax_encrypt_authenticate_memory(x, key, kl, nonce, y1, header, y1, plaintext, y1, plaintext, tag, &len)) != CRYPT_OK) {
  377. printf("Error EAX'ing: %s\n", error_to_string(err));
  378. exit(EXIT_FAILURE);
  379. }
  380. fprintf(out, "%3d: ", y1);
  381. for (z = 0; z < y1; z++) {
  382. fprintf(out, "%02X", plaintext[z]);
  383. }
  384. fprintf(out, ", ");
  385. for (z = 0; z <(int)len; z++) {
  386. fprintf(out, "%02X", tag[z]);
  387. }
  388. fprintf(out, "\n");
  389. /* forward the key */
  390. for (z = 0; z < kl; z++) {
  391. key[z] = tag[z % len];
  392. }
  393. }
  394. fprintf(out, "\n");
  395. }
  396. fclose(out);
  397. }
  398. void ocb_gen(void)
  399. {
  400. int err, kl, x, y1, z;
  401. FILE *out;
  402. unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2],
  403. plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
  404. unsigned long len;
  405. out = fopen("ocb_tv.txt", "w");
  406. fprintf(out, "OCB Test Vectors. Uses the 00010203...NN-1 pattern for nonce/plaintext/key. The outputs\n"
  407. "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
  408. "step repeated sufficiently. The nonce is fixed throughout.\n\n");
  409. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  410. kl = cipher_descriptor[x].block_length;
  411. /* skip ciphers which do not have 64 or 128 bit block sizes */
  412. if (kl != 8 && kl != 16) continue;
  413. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  414. kl = cipher_descriptor[x].max_key_length;
  415. }
  416. fprintf(out, "OCB-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  417. /* the key */
  418. for (z = 0; z < kl; z++) {
  419. key[z] = (z & 255);
  420. }
  421. /* fixed nonce */
  422. for (z = 0; z < cipher_descriptor[x].block_length; z++) {
  423. nonce[z] = z;
  424. }
  425. for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
  426. for (z = 0; z < y1; z++) {
  427. plaintext[z] = (unsigned char)(z & 255);
  428. }
  429. len = sizeof(tag);
  430. if ((err = ocb_encrypt_authenticate_memory(x, key, kl, nonce, plaintext, y1, plaintext, tag, &len)) != CRYPT_OK) {
  431. printf("Error OCB'ing: %s\n", error_to_string(err));
  432. exit(EXIT_FAILURE);
  433. }
  434. fprintf(out, "%3d: ", y1);
  435. for (z = 0; z < y1; z++) {
  436. fprintf(out, "%02X", plaintext[z]);
  437. }
  438. fprintf(out, ", ");
  439. for (z = 0; z <(int)len; z++) {
  440. fprintf(out, "%02X", tag[z]);
  441. }
  442. fprintf(out, "\n");
  443. /* forward the key */
  444. for (z = 0; z < kl; z++) {
  445. key[z] = tag[z % len];
  446. }
  447. }
  448. fprintf(out, "\n");
  449. }
  450. fclose(out);
  451. }
  452. void ccm_gen(void)
  453. {
  454. int err, kl, x, y1, z;
  455. FILE *out;
  456. unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2],
  457. plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
  458. unsigned long len;
  459. out = fopen("ccm_tv.txt", "w");
  460. fprintf(out, "CCM Test Vectors. Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key. The outputs\n"
  461. "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
  462. "step repeated sufficiently. The nonce is fixed throughout at 13 bytes 000102...\n\n");
  463. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  464. kl = cipher_descriptor[x].block_length;
  465. /* skip ciphers which do not have 128 bit block sizes */
  466. if (kl != 16) continue;
  467. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  468. kl = cipher_descriptor[x].max_key_length;
  469. }
  470. fprintf(out, "CCM-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  471. /* the key */
  472. for (z = 0; z < kl; z++) {
  473. key[z] = (z & 255);
  474. }
  475. /* fixed nonce */
  476. for (z = 0; z < cipher_descriptor[x].block_length; z++) {
  477. nonce[z] = z;
  478. }
  479. for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
  480. for (z = 0; z < y1; z++) {
  481. plaintext[z] = (unsigned char)(z & 255);
  482. }
  483. len = sizeof(tag);
  484. if ((err = ccm_memory(x, key, kl, NULL, nonce, 13, plaintext, y1, plaintext, y1, plaintext, tag, &len, CCM_ENCRYPT)) != CRYPT_OK) {
  485. printf("Error CCM'ing: %s\n", error_to_string(err));
  486. exit(EXIT_FAILURE);
  487. }
  488. fprintf(out, "%3d: ", y1);
  489. for (z = 0; z < y1; z++) {
  490. fprintf(out, "%02X", plaintext[z]);
  491. }
  492. fprintf(out, ", ");
  493. for (z = 0; z <(int)len; z++) {
  494. fprintf(out, "%02X", tag[z]);
  495. }
  496. fprintf(out, "\n");
  497. /* forward the key */
  498. for (z = 0; z < kl; z++) {
  499. key[z] = tag[z % len];
  500. }
  501. }
  502. fprintf(out, "\n");
  503. }
  504. fclose(out);
  505. }
  506. void gcm_gen(void)
  507. {
  508. int err, kl, x, y1, z;
  509. FILE *out;
  510. unsigned char key[MAXBLOCKSIZE], plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
  511. unsigned long len;
  512. out = fopen("gcm_tv.txt", "w");
  513. fprintf(out, "GCM Test Vectors. Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key. The outputs\n"
  514. "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
  515. "step repeated sufficiently. The nonce is fixed throughout at 13 bytes 000102...\n\n");
  516. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  517. kl = cipher_descriptor[x].block_length;
  518. /* skip ciphers which do not have 128 bit block sizes */
  519. if (kl != 16) continue;
  520. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  521. kl = cipher_descriptor[x].max_key_length;
  522. }
  523. fprintf(out, "GCM-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  524. /* the key */
  525. for (z = 0; z < kl; z++) {
  526. key[z] = (z & 255);
  527. }
  528. for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
  529. for (z = 0; z < y1; z++) {
  530. plaintext[z] = (unsigned char)(z & 255);
  531. }
  532. len = sizeof(tag);
  533. if ((err = gcm_memory(x, key, kl, plaintext, y1, plaintext, y1, plaintext, y1, plaintext, tag, &len, GCM_ENCRYPT)) != CRYPT_OK) {
  534. printf("Error GCM'ing: %s\n", error_to_string(err));
  535. exit(EXIT_FAILURE);
  536. }
  537. fprintf(out, "%3d: ", y1);
  538. for (z = 0; z < y1; z++) {
  539. fprintf(out, "%02X", plaintext[z]);
  540. }
  541. fprintf(out, ", ");
  542. for (z = 0; z <(int)len; z++) {
  543. fprintf(out, "%02X", tag[z]);
  544. }
  545. fprintf(out, "\n");
  546. /* forward the key */
  547. for (z = 0; z < kl; z++) {
  548. key[z] = tag[z % len];
  549. }
  550. }
  551. fprintf(out, "\n");
  552. }
  553. fclose(out);
  554. }
  555. void base64_gen(void)
  556. {
  557. FILE *out;
  558. unsigned char dst[256], src[32];
  559. unsigned long x, y, len;
  560. out = fopen("base64_tv.txt", "w");
  561. fprintf(out, "Base64 vectors. These are the base64 encodings of the strings 00,01,02...NN-1\n\n");
  562. for (x = 0; x <= 32; x++) {
  563. for (y = 0; y < x; y++) {
  564. src[y] = y;
  565. }
  566. len = sizeof(dst);
  567. base64_encode(src, x, dst, &len);
  568. fprintf(out, "%2lu: %s\n", x, dst);
  569. }
  570. fclose(out);
  571. }
  572. void math_gen(void)
  573. {
  574. }
  575. void ecc_gen(void)
  576. {
  577. FILE *out;
  578. unsigned char str[512];
  579. void *k, *order, *modulus;
  580. ecc_point *G, *R;
  581. int x;
  582. out = fopen("ecc_tv.txt", "w");
  583. fprintf(out, "ecc vectors. These are for kG for k=1,3,9,27,...,3**n until k > order of the curve outputs are <k,x,y> triplets\n\n");
  584. G = ltc_ecc_new_point();
  585. R = ltc_ecc_new_point();
  586. mp_init(&k);
  587. mp_init(&order);
  588. mp_init(&modulus);
  589. for (x = 0; ltc_ecc_sets[x].size != 0; x++) {
  590. fprintf(out, "ECC-%d\n", ltc_ecc_sets[x].size*8);
  591. mp_set(k, 1);
  592. mp_read_radix(order, (char *)ltc_ecc_sets[x].order, 64);
  593. mp_read_radix(modulus, (char *)ltc_ecc_sets[x].prime, 64);
  594. mp_read_radix(G->x, (char *)ltc_ecc_sets[x].Gx, 64);
  595. mp_read_radix(G->y, (char *)ltc_ecc_sets[x].Gy, 64);
  596. mp_set(G->z, 1);
  597. while (mp_cmp(k, order) == LTC_MP_LT) {
  598. ltc_mp.ecc_ptmul(k, G, R, modulus, 1);
  599. mp_tohex(k, str); fprintf(out, "%s, ", str);
  600. mp_tohex(R->x, str); fprintf(out, "%s, ", str);
  601. mp_tohex(R->y, str); fprintf(out, "%s\n", str);
  602. mp_mul_d(k, 3, k);
  603. }
  604. }
  605. mp_clear_multi(k, order, modulus, NULL);
  606. ltc_ecc_del_point(G);
  607. ltc_ecc_del_point(R);
  608. fclose(out);
  609. }
  610. int main(void)
  611. {
  612. reg_algs();
  613. printf("Generating hash vectors..."); fflush(stdout); hash_gen(); printf("done\n");
  614. printf("Generating cipher vectors..."); fflush(stdout); cipher_gen(); printf("done\n");
  615. printf("Generating HMAC vectors..."); fflush(stdout); hmac_gen(); printf("done\n");
  616. printf("Generating OMAC vectors..."); fflush(stdout); omac_gen(); printf("done\n");
  617. printf("Generating PMAC vectors..."); fflush(stdout); pmac_gen(); printf("done\n");
  618. printf("Generating EAX vectors..."); fflush(stdout); eax_gen(); printf("done\n");
  619. printf("Generating OCB vectors..."); fflush(stdout); ocb_gen(); printf("done\n");
  620. printf("Generating CCM vectors..."); fflush(stdout); ccm_gen(); printf("done\n");
  621. printf("Generating GCM vectors..."); fflush(stdout); gcm_gen(); printf("done\n");
  622. printf("Generating BASE64 vectors..."); fflush(stdout); base64_gen(); printf("done\n");
  623. printf("Generating MATH vectors..."); fflush(stdout); math_gen(); printf("done\n");
  624. printf("Generating ECC vectors..."); fflush(stdout); ecc_gen(); printf("done\n");
  625. return 0;
  626. }
  627. /* $Source$ */
  628. /* $Revision$ */
  629. /* $Date$ */