tv_gen.c 26 KB

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