tv_gen.c 26 KB

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