I2C toy code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

798 lines
25 KiB

  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) break;
  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;
  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 != 8 && kl != 16) continue;
  380. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  381. kl = cipher_descriptor[x].max_key_length;
  382. }
  383. fprintf(out, "OCB-%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. for (z = 0; z < cipher_descriptor[x].block_length; z++) {
  390. nonce[z] = z;
  391. }
  392. for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
  393. for (z = 0; z < y1; z++) {
  394. plaintext[z] = (unsigned char)(z & 255);
  395. }
  396. len = sizeof(tag);
  397. 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) {
  398. printf("Error OCB'ing: %s\n", error_to_string(err));
  399. exit(EXIT_FAILURE);
  400. }
  401. fprintf(out, "%3d: ", y1);
  402. for (z = 0; z < y1; z++) {
  403. fprintf(out, "%02X", plaintext[z]);
  404. }
  405. fprintf(out, ", ");
  406. for (z = 0; z <(int)len; z++) {
  407. fprintf(out, "%02X", tag[z]);
  408. }
  409. fprintf(out, "\n");
  410. /* forward the key */
  411. for (z = 0; z < kl; z++) {
  412. key[z] = tag[z % len];
  413. }
  414. }
  415. fprintf(out, "\n");
  416. }
  417. fclose(out);
  418. #endif
  419. }
  420. void ccm_gen(void)
  421. {
  422. #ifdef LTC_CCM_MODE
  423. int err, kl, x, y1, z;
  424. FILE *out;
  425. unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2],
  426. plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
  427. unsigned long len;
  428. out = fopen("ccm_tv.txt", "w");
  429. fprintf(out, "CCM Test Vectors. Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key. The outputs\n"
  430. "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
  431. "step repeated sufficiently. The nonce is fixed throughout at 13 bytes 000102...\n\n");
  432. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  433. kl = cipher_descriptor[x].block_length;
  434. /* skip ciphers which do not have 128 bit block sizes */
  435. if (kl != 16) continue;
  436. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  437. kl = cipher_descriptor[x].max_key_length;
  438. }
  439. fprintf(out, "CCM-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  440. /* the key */
  441. for (z = 0; z < kl; z++) {
  442. key[z] = (z & 255);
  443. }
  444. /* fixed nonce */
  445. for (z = 0; z < cipher_descriptor[x].block_length; z++) {
  446. nonce[z] = z;
  447. }
  448. for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
  449. for (z = 0; z < y1; z++) {
  450. plaintext[z] = (unsigned char)(z & 255);
  451. }
  452. len = sizeof(tag);
  453. if ((err = ccm_memory(x, key, kl, NULL, nonce, 13, plaintext, y1, plaintext, y1, plaintext, tag, &len, CCM_ENCRYPT)) != CRYPT_OK) {
  454. printf("Error CCM'ing: %s\n", error_to_string(err));
  455. exit(EXIT_FAILURE);
  456. }
  457. fprintf(out, "%3d: ", y1);
  458. for (z = 0; z < y1; z++) {
  459. fprintf(out, "%02X", plaintext[z]);
  460. }
  461. fprintf(out, ", ");
  462. for (z = 0; z <(int)len; z++) {
  463. fprintf(out, "%02X", tag[z]);
  464. }
  465. fprintf(out, "\n");
  466. /* forward the key */
  467. for (z = 0; z < kl; z++) {
  468. key[z] = tag[z % len];
  469. }
  470. }
  471. fprintf(out, "\n");
  472. }
  473. fclose(out);
  474. #endif
  475. }
  476. void gcm_gen(void)
  477. {
  478. #ifdef LTC_GCM_MODE
  479. int err, kl, x, y1, z;
  480. FILE *out;
  481. unsigned char key[MAXBLOCKSIZE], plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
  482. unsigned long len;
  483. out = fopen("gcm_tv.txt", "w");
  484. fprintf(out, "GCM Test Vectors. Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key. The outputs\n"
  485. "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
  486. "step repeated sufficiently. The nonce is fixed throughout at 13 bytes 000102...\n\n");
  487. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  488. kl = cipher_descriptor[x].block_length;
  489. /* skip ciphers which do not have 128 bit block sizes */
  490. if (kl != 16) continue;
  491. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  492. kl = cipher_descriptor[x].max_key_length;
  493. }
  494. fprintf(out, "GCM-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  495. /* the key */
  496. for (z = 0; z < kl; z++) {
  497. key[z] = (z & 255);
  498. }
  499. for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
  500. for (z = 0; z < y1; z++) {
  501. plaintext[z] = (unsigned char)(z & 255);
  502. }
  503. len = sizeof(tag);
  504. if ((err = gcm_memory(x, key, kl, plaintext, y1, plaintext, y1, plaintext, y1, plaintext, tag, &len, GCM_ENCRYPT)) != CRYPT_OK) {
  505. printf("Error GCM'ing: %s\n", error_to_string(err));
  506. exit(EXIT_FAILURE);
  507. }
  508. if (len == 0) {
  509. printf("Error GCM'ing: zero length\n");
  510. exit(EXIT_FAILURE);
  511. }
  512. fprintf(out, "%3d: ", y1);
  513. for (z = 0; z < y1; z++) {
  514. fprintf(out, "%02X", plaintext[z]);
  515. }
  516. fprintf(out, ", ");
  517. for (z = 0; z <(int)len; z++) {
  518. fprintf(out, "%02X", tag[z]);
  519. }
  520. fprintf(out, "\n");
  521. /* forward the key */
  522. for (z = 0; z < kl; z++) {
  523. key[z] = tag[z % len];
  524. }
  525. }
  526. fprintf(out, "\n");
  527. }
  528. fclose(out);
  529. #endif
  530. }
  531. void base64_gen(void)
  532. {
  533. FILE *out;
  534. unsigned char dst[256], src[32], ch;
  535. unsigned long x, len;
  536. out = fopen("base64_tv.txt", "w");
  537. fprintf(out, "Base64 vectors. These are the base64 encodings of the strings 00,01,02...NN-1\n\n");
  538. for (x = 0; x <= 32; x++) {
  539. for (ch = 0; ch < x; ch++) {
  540. src[ch] = ch;
  541. }
  542. len = sizeof(dst);
  543. base64_encode(src, x, dst, &len);
  544. fprintf(out, "%2lu: %s\n", x, dst);
  545. }
  546. fclose(out);
  547. }
  548. void math_gen(void)
  549. {
  550. }
  551. void ecc_gen(void)
  552. {
  553. FILE *out;
  554. unsigned char str[512];
  555. void *k, *order, *modulus;
  556. ecc_point *G, *R;
  557. int x;
  558. out = fopen("ecc_tv.txt", "w");
  559. 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");
  560. G = ltc_ecc_new_point();
  561. R = ltc_ecc_new_point();
  562. mp_init(&k);
  563. mp_init(&order);
  564. mp_init(&modulus);
  565. for (x = 0; ltc_ecc_sets[x].size != 0; x++) {
  566. fprintf(out, "ECC-%d\n", ltc_ecc_sets[x].size*8);
  567. mp_set(k, 1);
  568. mp_read_radix(order, (char *)ltc_ecc_sets[x].order, 16);
  569. mp_read_radix(modulus, (char *)ltc_ecc_sets[x].prime, 16);
  570. mp_read_radix(G->x, (char *)ltc_ecc_sets[x].Gx, 16);
  571. mp_read_radix(G->y, (char *)ltc_ecc_sets[x].Gy, 16);
  572. mp_set(G->z, 1);
  573. while (mp_cmp(k, order) == LTC_MP_LT) {
  574. ltc_mp.ecc_ptmul(k, G, R, modulus, 1);
  575. mp_tohex(k, (char*)str); fprintf(out, "%s, ", (char*)str);
  576. mp_tohex(R->x, (char*)str); fprintf(out, "%s, ", (char*)str);
  577. mp_tohex(R->y, (char*)str); fprintf(out, "%s\n", (char*)str);
  578. mp_mul_d(k, 3, k);
  579. }
  580. }
  581. mp_clear_multi(k, order, modulus, NULL);
  582. ltc_ecc_del_point(G);
  583. ltc_ecc_del_point(R);
  584. fclose(out);
  585. }
  586. void lrw_gen(void)
  587. {
  588. #ifdef LTC_LRW_MODE
  589. FILE *out;
  590. unsigned char tweak[16], key[16], iv[16], buf[1024];
  591. int x, y, err;
  592. symmetric_LRW lrw;
  593. /* initialize default key and tweak */
  594. for (x = 0; x < 16; x++) {
  595. tweak[x] = key[x] = iv[x] = x;
  596. }
  597. out = fopen("lrw_tv.txt", "w");
  598. for (x = 16; x < (int)(sizeof(buf)); x += 16) {
  599. if ((err = lrw_start(find_cipher("aes"), iv, key, 16, tweak, 0, &lrw)) != CRYPT_OK) {
  600. fprintf(stderr, "Error starting LRW-AES: %s\n", error_to_string(err));
  601. exit(EXIT_FAILURE);
  602. }
  603. /* encrypt incremental */
  604. for (y = 0; y < x; y++) {
  605. buf[y] = y & 255;
  606. }
  607. if ((err = lrw_encrypt(buf, buf, x, &lrw)) != CRYPT_OK) {
  608. fprintf(stderr, "Error encrypting with LRW-AES: %s\n", error_to_string(err));
  609. exit(EXIT_FAILURE);
  610. }
  611. /* display it */
  612. fprintf(out, "%d:", x);
  613. for (y = 0; y < x; y++) {
  614. fprintf(out, "%02x", buf[y]);
  615. }
  616. fprintf(out, "\n");
  617. /* reset IV */
  618. if ((err = lrw_setiv(iv, 16, &lrw)) != CRYPT_OK) {
  619. fprintf(stderr, "Error setting IV: %s\n", error_to_string(err));
  620. exit(EXIT_FAILURE);
  621. }
  622. /* copy new tweak, iv and key */
  623. for (y = 0; y < 16; y++) {
  624. key[y] = buf[y];
  625. iv[y] = buf[(y+16)%x];
  626. tweak[y] = buf[(y+32)%x];
  627. }
  628. if ((err = lrw_decrypt(buf, buf, x, &lrw)) != CRYPT_OK) {
  629. fprintf(stderr, "Error decrypting with LRW-AES: %s\n", error_to_string(err));
  630. exit(EXIT_FAILURE);
  631. }
  632. /* display it */
  633. fprintf(out, "%d:", x);
  634. for (y = 0; y < x; y++) {
  635. fprintf(out, "%02x", buf[y]);
  636. }
  637. fprintf(out, "\n");
  638. lrw_done(&lrw);
  639. }
  640. fclose(out);
  641. #endif
  642. }
  643. int main(void)
  644. {
  645. register_all_ciphers();
  646. register_all_hashes();
  647. register_all_prngs();
  648. #ifdef USE_LTM
  649. ltc_mp = ltm_desc;
  650. #elif defined(USE_TFM)
  651. ltc_mp = tfm_desc;
  652. #elif defined(USE_GMP)
  653. ltc_mp = gmp_desc;
  654. #elif defined(EXT_MATH_LIB)
  655. extern ltc_math_descriptor EXT_MATH_LIB;
  656. ltc_mp = EXT_MATH_LIB;
  657. #else
  658. fprintf(stderr, "No MPI provider available\n");
  659. exit(EXIT_FAILURE);
  660. #endif
  661. printf("Generating hash vectors..."); fflush(stdout); hash_gen(); printf("done\n");
  662. printf("Generating cipher vectors..."); fflush(stdout); cipher_gen(); printf("done\n");
  663. printf("Generating HMAC vectors..."); fflush(stdout); hmac_gen(); printf("done\n");
  664. #ifdef LTC_OMAC
  665. printf("Generating OMAC vectors..."); fflush(stdout); omac_gen(); printf("done\n");
  666. #endif
  667. #ifdef LTC_PMAC
  668. printf("Generating PMAC vectors..."); fflush(stdout); pmac_gen(); printf("done\n");
  669. #endif
  670. #ifdef LTC_EAX_MODE
  671. printf("Generating EAX vectors..."); fflush(stdout); eax_gen(); printf("done\n");
  672. #endif
  673. #ifdef LTC_OCB_MODE
  674. printf("Generating OCB vectors..."); fflush(stdout); ocb_gen(); printf("done\n");
  675. #endif
  676. #ifdef LTC_OCB3_MODE
  677. printf("Generating OCB3 vectors..."); fflush(stdout); ocb3_gen(); printf("done\n");
  678. #endif
  679. #ifdef LTC_CCM_MODE
  680. printf("Generating CCM vectors..."); fflush(stdout); ccm_gen(); printf("done\n");
  681. #endif
  682. #ifdef LTC_GCM_MODE
  683. printf("Generating GCM vectors..."); fflush(stdout); gcm_gen(); printf("done\n");
  684. #endif
  685. printf("Generating BASE64 vectors..."); fflush(stdout); base64_gen(); printf("done\n");
  686. printf("Generating MATH vectors..."); fflush(stdout); math_gen(); printf("done\n");
  687. printf("Generating ECC vectors..."); fflush(stdout); ecc_gen(); printf("done\n");
  688. #ifdef LTC_LRW_MODE
  689. printf("Generating LRW vectors..."); fflush(stdout); lrw_gen(); printf("done\n");
  690. #endif
  691. return 0;
  692. }
  693. /* ref: $Format:%D$ */
  694. /* git commit: $Format:%H$ */
  695. /* commit time: $Format:%ai$ */