tv_gen.c 24 KB

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