tv_gen.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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.h>
  10. 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. 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. 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. void omac_gen(void)
  159. {
  160. #ifdef LTC_OMAC
  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 omacing: %s\n", error_to_string(err));
  189. exit(EXIT_FAILURE);
  190. }
  191. fprintf(out, "%3d: ", y);
  192. for (z = 0; z <(int)len; z++) {
  193. fprintf(out, "%02X", output[z]);
  194. }
  195. fprintf(out, "\n");
  196. /* forward the key */
  197. for (z = 0; z < kl; z++) {
  198. key[z] = output[z % len];
  199. }
  200. }
  201. fprintf(out, "\n");
  202. }
  203. fclose(out);
  204. #endif
  205. }
  206. void pmac_gen(void)
  207. {
  208. #ifdef LTC_PMAC
  209. unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], input[MAXBLOCKSIZE*2+2];
  210. int err, x, y, z, kl;
  211. FILE *out;
  212. unsigned long len;
  213. out = fopen("pmac_tv.txt", "w");
  214. fprintf(out,
  215. "PMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are PMAC'ed. The initial key is\n"
  216. "of the same format (length specified per cipher). The PMAC key in step N+1 is the PMAC output of\n"
  217. "step N (repeated as required to fill the array).\n\n");
  218. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  219. kl = cipher_descriptor[x].block_length;
  220. /* skip ciphers which do not have 64 or 128 bit block sizes */
  221. if (kl != 8 && kl != 16) continue;
  222. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  223. kl = cipher_descriptor[x].max_key_length;
  224. }
  225. fprintf(out, "PMAC-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  226. /* initial key/block */
  227. for (y = 0; y < kl; y++) {
  228. key[y] = (y & 255);
  229. }
  230. for (y = 0; y <= (int)(cipher_descriptor[x].block_length*2); y++) {
  231. for (z = 0; z < y; z++) {
  232. input[z] = (unsigned char)(z & 255);
  233. }
  234. len = sizeof(output);
  235. if ((err = pmac_memory(x, key, kl, input, y, output, &len)) != CRYPT_OK) {
  236. printf("Error omacing: %s\n", error_to_string(err));
  237. exit(EXIT_FAILURE);
  238. }
  239. fprintf(out, "%3d: ", y);
  240. for (z = 0; z <(int)len; z++) {
  241. fprintf(out, "%02X", output[z]);
  242. }
  243. fprintf(out, "\n");
  244. /* forward the key */
  245. for (z = 0; z < kl; z++) {
  246. key[z] = output[z % len];
  247. }
  248. }
  249. fprintf(out, "\n");
  250. }
  251. fclose(out);
  252. #endif
  253. }
  254. void eax_gen(void)
  255. {
  256. #ifdef LTC_EAX_MODE
  257. int err, kl, x, y1, z;
  258. FILE *out;
  259. unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2], header[MAXBLOCKSIZE*2],
  260. plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
  261. unsigned long len;
  262. out = fopen("eax_tv.txt", "w");
  263. fprintf(out, "EAX Test Vectors. Uses the 00010203...NN-1 pattern for header/nonce/plaintext/key. The outputs\n"
  264. "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
  265. "step repeated sufficiently.\n\n");
  266. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  267. kl = cipher_descriptor[x].block_length;
  268. /* skip ciphers which do not have 64 or 128 bit block sizes */
  269. if (kl != 8 && kl != 16) continue;
  270. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  271. kl = cipher_descriptor[x].max_key_length;
  272. }
  273. fprintf(out, "EAX-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  274. /* the key */
  275. for (z = 0; z < kl; z++) {
  276. key[z] = (z & 255);
  277. }
  278. for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
  279. for (z = 0; z < y1; z++) {
  280. plaintext[z] = (unsigned char)(z & 255);
  281. nonce[z] = (unsigned char)(z & 255);
  282. header[z] = (unsigned char)(z & 255);
  283. }
  284. len = sizeof(tag);
  285. if ((err = eax_encrypt_authenticate_memory(x, key, kl, nonce, y1, header, y1, plaintext, y1, plaintext, tag, &len)) != CRYPT_OK) {
  286. printf("Error EAX'ing: %s\n", error_to_string(err));
  287. exit(EXIT_FAILURE);
  288. }
  289. fprintf(out, "%3d: ", y1);
  290. for (z = 0; z < y1; z++) {
  291. fprintf(out, "%02X", plaintext[z]);
  292. }
  293. fprintf(out, ", ");
  294. for (z = 0; z <(int)len; z++) {
  295. fprintf(out, "%02X", tag[z]);
  296. }
  297. fprintf(out, "\n");
  298. /* forward the key */
  299. for (z = 0; z < kl; z++) {
  300. key[z] = tag[z % len];
  301. }
  302. }
  303. fprintf(out, "\n");
  304. }
  305. fclose(out);
  306. #endif
  307. }
  308. void ocb_gen(void)
  309. {
  310. #ifdef LTC_OCB_MODE
  311. int err, kl, x, y1, z;
  312. FILE *out;
  313. unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2],
  314. plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
  315. unsigned long len;
  316. out = fopen("ocb_tv.txt", "w");
  317. fprintf(out, "OCB Test Vectors. Uses the 00010203...NN-1 pattern for nonce/plaintext/key. The outputs\n"
  318. "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
  319. "step repeated sufficiently. The nonce is fixed throughout.\n\n");
  320. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  321. kl = cipher_descriptor[x].block_length;
  322. /* skip ciphers which do not have 64 or 128 bit block sizes */
  323. if (kl != 8 && kl != 16) continue;
  324. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  325. kl = cipher_descriptor[x].max_key_length;
  326. }
  327. fprintf(out, "OCB-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  328. /* the key */
  329. for (z = 0; z < kl; z++) {
  330. key[z] = (z & 255);
  331. }
  332. /* fixed nonce */
  333. for (z = 0; z < cipher_descriptor[x].block_length; z++) {
  334. nonce[z] = z;
  335. }
  336. for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
  337. for (z = 0; z < y1; z++) {
  338. plaintext[z] = (unsigned char)(z & 255);
  339. }
  340. len = sizeof(tag);
  341. if ((err = ocb_encrypt_authenticate_memory(x, key, kl, nonce, plaintext, y1, plaintext, tag, &len)) != CRYPT_OK) {
  342. printf("Error OCB'ing: %s\n", error_to_string(err));
  343. exit(EXIT_FAILURE);
  344. }
  345. fprintf(out, "%3d: ", y1);
  346. for (z = 0; z < y1; z++) {
  347. fprintf(out, "%02X", plaintext[z]);
  348. }
  349. fprintf(out, ", ");
  350. for (z = 0; z <(int)len; z++) {
  351. fprintf(out, "%02X", tag[z]);
  352. }
  353. fprintf(out, "\n");
  354. /* forward the key */
  355. for (z = 0; z < kl; z++) {
  356. key[z] = tag[z % len];
  357. }
  358. }
  359. fprintf(out, "\n");
  360. }
  361. fclose(out);
  362. #endif
  363. }
  364. void ocb3_gen(void)
  365. {
  366. #ifdef LTC_OCB3_MODE
  367. int err, kl, x, y1, z, noncelen;
  368. FILE *out;
  369. unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2],
  370. plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
  371. unsigned long len;
  372. out = fopen("ocb3_tv.txt", "w");
  373. fprintf(out, "OCB3 Test Vectors. Uses the 00010203...NN-1 pattern for nonce/plaintext/key. The outputs\n"
  374. "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
  375. "step repeated sufficiently. The nonce is fixed throughout. AAD is fixed to 3 bytes (ASCII) 'AAD'.\n\n");
  376. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  377. kl = cipher_descriptor[x].block_length;
  378. /* skip ciphers which do not have 64 or 128 bit block sizes */
  379. if (kl != 16) continue;
  380. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  381. kl = cipher_descriptor[x].max_key_length;
  382. }
  383. fprintf(out, "OCB3-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  384. /* the key */
  385. for (z = 0; z < kl; z++) {
  386. key[z] = (z & 255);
  387. }
  388. /* fixed nonce */
  389. noncelen = MIN(15, cipher_descriptor[x].block_length);
  390. for (z = 0; z < noncelen; z++) {
  391. nonce[z] = z;
  392. }
  393. for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
  394. for (z = 0; z < y1; z++) {
  395. plaintext[z] = (unsigned char)(z & 255);
  396. }
  397. len = 16;
  398. if ((err = ocb3_encrypt_authenticate_memory(x, key, kl, nonce, noncelen, (unsigned char*)"AAD", 3, plaintext, y1, plaintext, tag, &len)) != CRYPT_OK) {
  399. printf("Error OCB3'ing: %s\n", error_to_string(err));
  400. exit(EXIT_FAILURE);
  401. }
  402. fprintf(out, "%3d: ", y1);
  403. for (z = 0; z < y1; z++) {
  404. fprintf(out, "%02X", plaintext[z]);
  405. }
  406. fprintf(out, ", ");
  407. for (z = 0; z <(int)len; z++) {
  408. fprintf(out, "%02X", tag[z]);
  409. }
  410. fprintf(out, "\n");
  411. /* forward the key */
  412. for (z = 0; z < kl; z++) {
  413. key[z] = tag[z % len];
  414. }
  415. }
  416. fprintf(out, "\n");
  417. }
  418. fclose(out);
  419. #endif
  420. }
  421. void ccm_gen(void)
  422. {
  423. #ifdef LTC_CCM_MODE
  424. int err, kl, x, y1, z;
  425. FILE *out;
  426. unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2],
  427. plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
  428. unsigned long len;
  429. out = fopen("ccm_tv.txt", "w");
  430. fprintf(out, "CCM Test Vectors. Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key. The outputs\n"
  431. "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
  432. "step repeated sufficiently. The nonce is fixed throughout at 13 bytes 000102...\n\n");
  433. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  434. kl = cipher_descriptor[x].block_length;
  435. /* skip ciphers which do not have 128 bit block sizes */
  436. if (kl != 16) continue;
  437. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  438. kl = cipher_descriptor[x].max_key_length;
  439. }
  440. fprintf(out, "CCM-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  441. /* the key */
  442. for (z = 0; z < kl; z++) {
  443. key[z] = (z & 255);
  444. }
  445. /* fixed nonce */
  446. for (z = 0; z < cipher_descriptor[x].block_length; z++) {
  447. nonce[z] = z;
  448. }
  449. for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
  450. for (z = 0; z < y1; z++) {
  451. plaintext[z] = (unsigned char)(z & 255);
  452. }
  453. len = sizeof(tag);
  454. if ((err = ccm_memory(x, key, kl, NULL, nonce, 13, plaintext, y1, plaintext, y1, plaintext, tag, &len, CCM_ENCRYPT)) != CRYPT_OK) {
  455. printf("Error CCM'ing: %s\n", error_to_string(err));
  456. exit(EXIT_FAILURE);
  457. }
  458. if (len == 0) {
  459. printf("Error CCM'ing: zero length\n");
  460. exit(EXIT_FAILURE);
  461. }
  462. fprintf(out, "%3d: ", y1);
  463. for (z = 0; z < y1; z++) {
  464. fprintf(out, "%02X", plaintext[z]);
  465. }
  466. fprintf(out, ", ");
  467. for (z = 0; z <(int)len; z++) {
  468. fprintf(out, "%02X", tag[z]);
  469. }
  470. fprintf(out, "\n");
  471. /* forward the key */
  472. for (z = 0; z < kl; z++) {
  473. key[z] = tag[z % len];
  474. }
  475. }
  476. fprintf(out, "\n");
  477. }
  478. fclose(out);
  479. #endif
  480. }
  481. void gcm_gen(void)
  482. {
  483. #ifdef LTC_GCM_MODE
  484. int err, kl, x, y1, z;
  485. FILE *out;
  486. unsigned char key[MAXBLOCKSIZE], plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
  487. unsigned long len;
  488. out = fopen("gcm_tv.txt", "w");
  489. fprintf(out, "GCM Test Vectors. Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key. The outputs\n"
  490. "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
  491. "step repeated sufficiently. The nonce is fixed throughout at 13 bytes 000102...\n\n");
  492. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  493. kl = cipher_descriptor[x].block_length;
  494. /* skip ciphers which do not have 128 bit block sizes */
  495. if (kl != 16) continue;
  496. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  497. kl = cipher_descriptor[x].max_key_length;
  498. }
  499. fprintf(out, "GCM-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  500. /* the key */
  501. for (z = 0; z < kl; z++) {
  502. key[z] = (z & 255);
  503. }
  504. for (y1 = 1; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
  505. for (z = 0; z < y1; z++) {
  506. plaintext[z] = (unsigned char)(z & 255);
  507. }
  508. len = sizeof(tag);
  509. if ((err = gcm_memory(x, key, kl, plaintext, y1, plaintext, y1, plaintext, y1, plaintext, tag, &len, GCM_ENCRYPT)) != CRYPT_OK) {
  510. printf("Error GCM'ing: %s\n", error_to_string(err));
  511. exit(EXIT_FAILURE);
  512. }
  513. if (len == 0) {
  514. printf("Error GCM'ing: zero length\n");
  515. exit(EXIT_FAILURE);
  516. }
  517. fprintf(out, "%3d: ", y1);
  518. for (z = 0; z < y1; z++) {
  519. fprintf(out, "%02X", plaintext[z]);
  520. }
  521. fprintf(out, ", ");
  522. for (z = 0; z <(int)len; z++) {
  523. fprintf(out, "%02X", tag[z]);
  524. }
  525. fprintf(out, "\n");
  526. /* forward the key */
  527. for (z = 0; z < kl; z++) {
  528. key[z] = tag[z % len];
  529. }
  530. }
  531. fprintf(out, "\n");
  532. }
  533. fclose(out);
  534. #endif
  535. }
  536. void base64_gen(void)
  537. {
  538. FILE *out;
  539. unsigned char dst[256], src[32], ch;
  540. unsigned long x, len;
  541. out = fopen("base64_tv.txt", "w");
  542. fprintf(out, "Base64 vectors. These are the base64 encodings of the strings 00,01,02...NN-1\n\n");
  543. for (x = 0; x <= 32; x++) {
  544. for (ch = 0; ch < x; ch++) {
  545. src[ch] = ch;
  546. }
  547. len = sizeof(dst);
  548. base64_encode(src, x, dst, &len);
  549. fprintf(out, "%2lu: %s\n", x, dst);
  550. }
  551. fclose(out);
  552. }
  553. void math_gen(void)
  554. {
  555. }
  556. void ecc_gen(void)
  557. {
  558. FILE *out;
  559. unsigned char str[512];
  560. void *k, *order, *modulus;
  561. ecc_point *G, *R;
  562. int x;
  563. out = fopen("ecc_tv.txt", "w");
  564. 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");
  565. G = ltc_ecc_new_point();
  566. R = ltc_ecc_new_point();
  567. mp_init(&k);
  568. mp_init(&order);
  569. mp_init(&modulus);
  570. for (x = 0; ltc_ecc_sets[x].size != 0; x++) {
  571. fprintf(out, "ECC-%d\n", ltc_ecc_sets[x].size*8);
  572. mp_set(k, 1);
  573. mp_read_radix(order, (char *)ltc_ecc_sets[x].order, 16);
  574. mp_read_radix(modulus, (char *)ltc_ecc_sets[x].prime, 16);
  575. mp_read_radix(G->x, (char *)ltc_ecc_sets[x].Gx, 16);
  576. mp_read_radix(G->y, (char *)ltc_ecc_sets[x].Gy, 16);
  577. mp_set(G->z, 1);
  578. while (mp_cmp(k, order) == LTC_MP_LT) {
  579. ltc_mp.ecc_ptmul(k, G, R, modulus, 1);
  580. mp_tohex(k, (char*)str); fprintf(out, "%s, ", (char*)str);
  581. mp_tohex(R->x, (char*)str); fprintf(out, "%s, ", (char*)str);
  582. mp_tohex(R->y, (char*)str); fprintf(out, "%s\n", (char*)str);
  583. mp_mul_d(k, 3, k);
  584. }
  585. }
  586. mp_clear_multi(k, order, modulus, NULL);
  587. ltc_ecc_del_point(G);
  588. ltc_ecc_del_point(R);
  589. fclose(out);
  590. }
  591. void lrw_gen(void)
  592. {
  593. #ifdef LTC_LRW_MODE
  594. FILE *out;
  595. unsigned char tweak[16], key[16], iv[16], buf[1024];
  596. int x, y, err;
  597. symmetric_LRW lrw;
  598. /* initialize default key and tweak */
  599. for (x = 0; x < 16; x++) {
  600. tweak[x] = key[x] = iv[x] = x;
  601. }
  602. out = fopen("lrw_tv.txt", "w");
  603. for (x = 16; x < (int)(sizeof(buf)); x += 16) {
  604. if ((err = lrw_start(find_cipher("aes"), iv, key, 16, tweak, 0, &lrw)) != CRYPT_OK) {
  605. fprintf(stderr, "Error starting LRW-AES: %s\n", error_to_string(err));
  606. exit(EXIT_FAILURE);
  607. }
  608. /* encrypt incremental */
  609. for (y = 0; y < x; y++) {
  610. buf[y] = y & 255;
  611. }
  612. if ((err = lrw_encrypt(buf, buf, x, &lrw)) != CRYPT_OK) {
  613. fprintf(stderr, "Error encrypting with LRW-AES: %s\n", error_to_string(err));
  614. exit(EXIT_FAILURE);
  615. }
  616. /* display it */
  617. fprintf(out, "%d:", x);
  618. for (y = 0; y < x; y++) {
  619. fprintf(out, "%02x", buf[y]);
  620. }
  621. fprintf(out, "\n");
  622. /* reset IV */
  623. if ((err = lrw_setiv(iv, 16, &lrw)) != CRYPT_OK) {
  624. fprintf(stderr, "Error setting IV: %s\n", error_to_string(err));
  625. exit(EXIT_FAILURE);
  626. }
  627. /* copy new tweak, iv and key */
  628. for (y = 0; y < 16; y++) {
  629. key[y] = buf[y];
  630. iv[y] = buf[(y+16)%x];
  631. tweak[y] = buf[(y+32)%x];
  632. }
  633. if ((err = lrw_decrypt(buf, buf, x, &lrw)) != CRYPT_OK) {
  634. fprintf(stderr, "Error decrypting 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. lrw_done(&lrw);
  644. }
  645. fclose(out);
  646. #endif
  647. }
  648. int main(void)
  649. {
  650. register_all_ciphers();
  651. register_all_hashes();
  652. register_all_prngs();
  653. #ifdef USE_LTM
  654. ltc_mp = ltm_desc;
  655. #elif defined(USE_TFM)
  656. ltc_mp = tfm_desc;
  657. #elif defined(USE_GMP)
  658. ltc_mp = gmp_desc;
  659. #elif defined(EXT_MATH_LIB)
  660. extern ltc_math_descriptor EXT_MATH_LIB;
  661. ltc_mp = EXT_MATH_LIB;
  662. #else
  663. fprintf(stderr, "No MPI provider available\n");
  664. exit(EXIT_FAILURE);
  665. #endif
  666. printf("Generating hash vectors..."); fflush(stdout); hash_gen(); printf("done\n");
  667. printf("Generating cipher vectors..."); fflush(stdout); cipher_gen(); printf("done\n");
  668. printf("Generating HMAC vectors..."); fflush(stdout); hmac_gen(); printf("done\n");
  669. #ifdef LTC_OMAC
  670. printf("Generating OMAC vectors..."); fflush(stdout); omac_gen(); printf("done\n");
  671. #endif
  672. #ifdef LTC_PMAC
  673. printf("Generating PMAC vectors..."); fflush(stdout); pmac_gen(); printf("done\n");
  674. #endif
  675. #ifdef LTC_EAX_MODE
  676. printf("Generating EAX vectors..."); fflush(stdout); eax_gen(); printf("done\n");
  677. #endif
  678. #ifdef LTC_OCB_MODE
  679. printf("Generating OCB vectors..."); fflush(stdout); ocb_gen(); printf("done\n");
  680. #endif
  681. #ifdef LTC_OCB3_MODE
  682. printf("Generating OCB3 vectors..."); fflush(stdout); ocb3_gen(); printf("done\n");
  683. #endif
  684. #ifdef LTC_CCM_MODE
  685. printf("Generating CCM vectors..."); fflush(stdout); ccm_gen(); printf("done\n");
  686. #endif
  687. #ifdef LTC_GCM_MODE
  688. printf("Generating GCM vectors..."); fflush(stdout); gcm_gen(); printf("done\n");
  689. #endif
  690. printf("Generating BASE64 vectors..."); fflush(stdout); base64_gen(); printf("done\n");
  691. printf("Generating MATH vectors..."); fflush(stdout); math_gen(); printf("done\n");
  692. printf("Generating ECC vectors..."); fflush(stdout); ecc_gen(); printf("done\n");
  693. #ifdef LTC_LRW_MODE
  694. printf("Generating LRW vectors..."); fflush(stdout); lrw_gen(); printf("done\n");
  695. #endif
  696. return 0;
  697. }
  698. /* ref: $Format:%D$ */
  699. /* git commit: $Format:%H$ */
  700. /* commit time: $Format:%ai$ */