tv_gen.c 27 KB

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