tv_gen.c 24 KB

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