tv_gen.c 26 KB

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