2
0

tv_gen.c 26 KB

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