tv_gen.c 26 KB

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