tv_gen.c 26 KB

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