tv_gen.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. #include <mycrypt.h>
  2. void reg_algs(void)
  3. {
  4. int err;
  5. #ifdef RIJNDAEL
  6. register_cipher (&aes_desc);
  7. #endif
  8. #ifdef BLOWFISH
  9. register_cipher (&blowfish_desc);
  10. #endif
  11. #ifdef XTEA
  12. register_cipher (&xtea_desc);
  13. #endif
  14. #ifdef RC5
  15. register_cipher (&rc5_desc);
  16. #endif
  17. #ifdef RC6
  18. register_cipher (&rc6_desc);
  19. #endif
  20. #ifdef SAFERP
  21. register_cipher (&saferp_desc);
  22. #endif
  23. #ifdef TWOFISH
  24. register_cipher (&twofish_desc);
  25. #endif
  26. #ifdef SAFER
  27. register_cipher (&safer_k64_desc);
  28. register_cipher (&safer_sk64_desc);
  29. register_cipher (&safer_k128_desc);
  30. register_cipher (&safer_sk128_desc);
  31. #endif
  32. #ifdef RC2
  33. register_cipher (&rc2_desc);
  34. #endif
  35. #ifdef DES
  36. register_cipher (&des_desc);
  37. register_cipher (&des3_desc);
  38. #endif
  39. #ifdef CAST5
  40. register_cipher (&cast5_desc);
  41. #endif
  42. #ifdef NOEKEON
  43. register_cipher (&noekeon_desc);
  44. #endif
  45. #ifdef SKIPJACK
  46. register_cipher (&skipjack_desc);
  47. #endif
  48. #ifdef TIGER
  49. register_hash (&tiger_desc);
  50. #endif
  51. #ifdef MD2
  52. register_hash (&md2_desc);
  53. #endif
  54. #ifdef MD4
  55. register_hash (&md4_desc);
  56. #endif
  57. #ifdef MD5
  58. register_hash (&md5_desc);
  59. #endif
  60. #ifdef SHA1
  61. register_hash (&sha1_desc);
  62. #endif
  63. #ifdef SHA224
  64. register_hash (&sha224_desc);
  65. #endif
  66. #ifdef SHA256
  67. register_hash (&sha256_desc);
  68. #endif
  69. #ifdef SHA384
  70. register_hash (&sha384_desc);
  71. #endif
  72. #ifdef SHA512
  73. register_hash (&sha512_desc);
  74. #endif
  75. #ifdef RIPEMD128
  76. register_hash (&rmd128_desc);
  77. #endif
  78. #ifdef RIPEMD160
  79. register_hash (&rmd160_desc);
  80. #endif
  81. #ifdef WHIRLPOOL
  82. register_hash (&whirlpool_desc);
  83. #endif
  84. #ifdef CHC_HASH
  85. register_hash(&chc_desc);
  86. if ((err = chc_register(register_cipher(&aes_desc))) != CRYPT_OK) {
  87. printf("chc_register error: %s\n", error_to_string(err));
  88. exit(EXIT_FAILURE);
  89. }
  90. #endif
  91. }
  92. void hash_gen(void)
  93. {
  94. unsigned char md[MAXBLOCKSIZE], *buf;
  95. unsigned long outlen, x, y, z;
  96. FILE *out;
  97. int err;
  98. out = fopen("hash_tv.txt", "w");
  99. if (out == NULL) {
  100. perror("can't open hash_tv");
  101. }
  102. fprintf(out, "Hash Test Vectors:\n\nThese are the hashes of nn bytes '00 01 02 03 .. (nn-1)'\n\n");
  103. for (x = 0; hash_descriptor[x].name != NULL; x++) {
  104. buf = XMALLOC(2 * hash_descriptor[x].blocksize + 1);
  105. if (buf == NULL) {
  106. perror("can't alloc mem");
  107. exit(EXIT_FAILURE);
  108. }
  109. fprintf(out, "Hash: %s\n", hash_descriptor[x].name);
  110. for (y = 0; y <= (hash_descriptor[x].blocksize * 2); y++) {
  111. for (z = 0; z < y; z++) {
  112. buf[z] = (unsigned char)(z & 255);
  113. }
  114. outlen = sizeof(md);
  115. if ((err = hash_memory(x, buf, y, md, &outlen)) != CRYPT_OK) {
  116. printf("hash_memory error: %s\n", error_to_string(err));
  117. exit(EXIT_FAILURE);
  118. }
  119. fprintf(out, "%3lu: ", y);
  120. for (z = 0; z < outlen; z++) {
  121. fprintf(out, "%02X", md[z]);
  122. }
  123. fprintf(out, "\n");
  124. }
  125. fprintf(out, "\n");
  126. XFREE(buf);
  127. }
  128. fclose(out);
  129. }
  130. void cipher_gen(void)
  131. {
  132. unsigned char *key, pt[MAXBLOCKSIZE];
  133. unsigned long x, y, z, w;
  134. int err, kl, lastkl;
  135. FILE *out;
  136. symmetric_key skey;
  137. out = fopen("cipher_tv.txt", "w");
  138. fprintf(out,
  139. "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"
  140. "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");
  141. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  142. fprintf(out, "Cipher: %s\n", cipher_descriptor[x].name);
  143. /* three modes, smallest, medium, large keys */
  144. lastkl = 10000;
  145. for (y = 0; y < 3; y++) {
  146. switch (y) {
  147. case 0: kl = cipher_descriptor[x].min_key_length; break;
  148. case 1: kl = (cipher_descriptor[x].min_key_length + cipher_descriptor[x].max_key_length)/2; break;
  149. case 2: kl = cipher_descriptor[x].max_key_length; break;
  150. }
  151. if ((err = cipher_descriptor[x].keysize(&kl)) != CRYPT_OK) {
  152. printf("keysize error: %s\n", error_to_string(err));
  153. exit(EXIT_FAILURE);
  154. }
  155. if (kl == lastkl) break;
  156. lastkl = kl;
  157. fprintf(out, "Key Size: %d bytes\n", kl);
  158. key = XMALLOC(kl);
  159. if (key == NULL) {
  160. perror("can't malloc memory");
  161. exit(EXIT_FAILURE);
  162. }
  163. for (z = 0; (int)z < kl; z++) {
  164. key[z] = (unsigned char)z;
  165. }
  166. if ((err = cipher_descriptor[x].setup(key, kl, 0, &skey)) != CRYPT_OK) {
  167. printf("setup error: %s\n", error_to_string(err));
  168. exit(EXIT_FAILURE);
  169. }
  170. for (z = 0; (int)z < cipher_descriptor[x].block_length; z++) {
  171. pt[z] = (unsigned char)z;
  172. }
  173. for (w = 0; w < 50; w++) {
  174. cipher_descriptor[x].ecb_encrypt(pt, pt, &skey);
  175. fprintf(out, "%2lu: ", w);
  176. for (z = 0; (int)z < cipher_descriptor[x].block_length; z++) {
  177. fprintf(out, "%02X", pt[z]);
  178. }
  179. fprintf(out, "\n");
  180. /* reschedule a new key */
  181. for (z = 0; z < (unsigned long)kl; z++) {
  182. key[z] = pt[z % cipher_descriptor[x].block_length];
  183. }
  184. if ((err = cipher_descriptor[x].setup(key, kl, 0, &skey)) != CRYPT_OK) {
  185. printf("cipher setup2 error: %s\n", error_to_string(err));
  186. exit(EXIT_FAILURE);
  187. }
  188. }
  189. fprintf(out, "\n");
  190. XFREE(key);
  191. }
  192. fprintf(out, "\n");
  193. }
  194. fclose(out);
  195. }
  196. void hmac_gen(void)
  197. {
  198. unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], *input;
  199. int x, y, z, err;
  200. FILE *out;
  201. unsigned long len;
  202. out = fopen("hmac_tv.txt", "w");
  203. fprintf(out,
  204. "HMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are HMACed. The initial key is\n"
  205. "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"
  206. "step N.\n\n");
  207. for (x = 0; hash_descriptor[x].name != NULL; x++) {
  208. fprintf(out, "HMAC-%s\n", hash_descriptor[x].name);
  209. /* initial key */
  210. for (y = 0; y < (int)hash_descriptor[x].hashsize; y++) {
  211. key[y] = (y&255);
  212. }
  213. input = XMALLOC(hash_descriptor[x].blocksize * 2 + 1);
  214. if (input == NULL) {
  215. perror("Can't malloc memory");
  216. exit(EXIT_FAILURE);
  217. }
  218. for (y = 0; y <= (int)(hash_descriptor[x].blocksize * 2); y++) {
  219. for (z = 0; z < y; z++) {
  220. input[z] = (unsigned char)(z & 255);
  221. }
  222. len = sizeof(output);
  223. if ((err = hmac_memory(x, key, hash_descriptor[x].hashsize, input, y, output, &len)) != CRYPT_OK) {
  224. printf("Error hmacing: %s\n", error_to_string(err));
  225. exit(EXIT_FAILURE);
  226. }
  227. fprintf(out, "%3d: ", y);
  228. for (z = 0; z <(int) len; z++) {
  229. fprintf(out, "%02X", output[z]);
  230. }
  231. fprintf(out, "\n");
  232. /* forward the key */
  233. memcpy(key, output, hash_descriptor[x].hashsize);
  234. }
  235. XFREE(input);
  236. fprintf(out, "\n");
  237. }
  238. fclose(out);
  239. }
  240. void omac_gen(void)
  241. {
  242. unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], input[MAXBLOCKSIZE*2+2];
  243. int err, x, y, z, kl;
  244. FILE *out;
  245. unsigned long len;
  246. out = fopen("omac_tv.txt", "w");
  247. fprintf(out,
  248. "OMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are OMAC'ed. The initial key is\n"
  249. "of the same format (length specified per cipher). The OMAC key in step N+1 is the OMAC output of\n"
  250. "step N (repeated as required to fill the array).\n\n");
  251. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  252. kl = cipher_descriptor[x].block_length;
  253. /* skip ciphers which do not have 64 or 128 bit block sizes */
  254. if (kl != 8 && kl != 16) continue;
  255. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  256. kl = cipher_descriptor[x].max_key_length;
  257. }
  258. fprintf(out, "OMAC-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  259. /* initial key/block */
  260. for (y = 0; y < kl; y++) {
  261. key[y] = (y & 255);
  262. }
  263. for (y = 0; y <= (int)(cipher_descriptor[x].block_length*2); y++) {
  264. for (z = 0; z < y; z++) {
  265. input[z] = (unsigned char)(z & 255);
  266. }
  267. len = sizeof(output);
  268. if ((err = omac_memory(x, key, kl, input, y, output, &len)) != CRYPT_OK) {
  269. printf("Error omacing: %s\n", error_to_string(err));
  270. exit(EXIT_FAILURE);
  271. }
  272. fprintf(out, "%3d: ", y);
  273. for (z = 0; z <(int)len; z++) {
  274. fprintf(out, "%02X", output[z]);
  275. }
  276. fprintf(out, "\n");
  277. /* forward the key */
  278. for (z = 0; z < kl; z++) {
  279. key[z] = output[z % len];
  280. }
  281. }
  282. fprintf(out, "\n");
  283. }
  284. fclose(out);
  285. }
  286. void pmac_gen(void)
  287. {
  288. unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], input[MAXBLOCKSIZE*2+2];
  289. int err, x, y, z, kl;
  290. FILE *out;
  291. unsigned long len;
  292. out = fopen("pmac_tv.txt", "w");
  293. fprintf(out,
  294. "PMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are OMAC'ed. The initial key is\n"
  295. "of the same format (length specified per cipher). The OMAC key in step N+1 is the OMAC output of\n"
  296. "step N (repeated as required to fill the array).\n\n");
  297. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  298. kl = cipher_descriptor[x].block_length;
  299. /* skip ciphers which do not have 64 or 128 bit block sizes */
  300. if (kl != 8 && kl != 16) continue;
  301. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  302. kl = cipher_descriptor[x].max_key_length;
  303. }
  304. fprintf(out, "PMAC-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  305. /* initial key/block */
  306. for (y = 0; y < kl; y++) {
  307. key[y] = (y & 255);
  308. }
  309. for (y = 0; y <= (int)(cipher_descriptor[x].block_length*2); y++) {
  310. for (z = 0; z < y; z++) {
  311. input[z] = (unsigned char)(z & 255);
  312. }
  313. len = sizeof(output);
  314. if ((err = pmac_memory(x, key, kl, input, y, output, &len)) != CRYPT_OK) {
  315. printf("Error omacing: %s\n", error_to_string(err));
  316. exit(EXIT_FAILURE);
  317. }
  318. fprintf(out, "%3d: ", y);
  319. for (z = 0; z <(int)len; z++) {
  320. fprintf(out, "%02X", output[z]);
  321. }
  322. fprintf(out, "\n");
  323. /* forward the key */
  324. for (z = 0; z < kl; z++) {
  325. key[z] = output[z % len];
  326. }
  327. }
  328. fprintf(out, "\n");
  329. }
  330. fclose(out);
  331. }
  332. void eax_gen(void)
  333. {
  334. int err, kl, x, y1, z;
  335. FILE *out;
  336. unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2], header[MAXBLOCKSIZE*2],
  337. plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
  338. unsigned long len;
  339. out = fopen("eax_tv.txt", "w");
  340. fprintf(out, "EAX Test Vectors. Uses the 00010203...NN-1 pattern for header/nonce/plaintext/key. The outputs\n"
  341. "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
  342. "step repeated sufficiently.\n\n");
  343. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  344. kl = cipher_descriptor[x].block_length;
  345. /* skip ciphers which do not have 64 or 128 bit block sizes */
  346. if (kl != 8 && kl != 16) continue;
  347. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  348. kl = cipher_descriptor[x].max_key_length;
  349. }
  350. fprintf(out, "EAX-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  351. /* the key */
  352. for (z = 0; z < kl; z++) {
  353. key[z] = (z & 255);
  354. }
  355. for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
  356. for (z = 0; z < y1; z++) {
  357. plaintext[z] = (unsigned char)(z & 255);
  358. nonce[z] = (unsigned char)(z & 255);
  359. header[z] = (unsigned char)(z & 255);
  360. }
  361. len = sizeof(tag);
  362. if ((err = eax_encrypt_authenticate_memory(x, key, kl, nonce, y1, header, y1, plaintext, y1, plaintext, tag, &len)) != CRYPT_OK) {
  363. printf("Error EAX'ing: %s\n", error_to_string(err));
  364. exit(EXIT_FAILURE);
  365. }
  366. fprintf(out, "%3d: ", y1);
  367. for (z = 0; z < y1; z++) {
  368. fprintf(out, "%02X", plaintext[z]);
  369. }
  370. fprintf(out, ", ");
  371. for (z = 0; z <(int)len; z++) {
  372. fprintf(out, "%02X", tag[z]);
  373. }
  374. fprintf(out, "\n");
  375. /* forward the key */
  376. for (z = 0; z < kl; z++) {
  377. key[z] = tag[z % len];
  378. }
  379. }
  380. fprintf(out, "\n");
  381. }
  382. fclose(out);
  383. }
  384. void ocb_gen(void)
  385. {
  386. int err, kl, x, y1, z;
  387. FILE *out;
  388. unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2],
  389. plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
  390. unsigned long len;
  391. out = fopen("ocb_tv.txt", "w");
  392. fprintf(out, "OCB Test Vectors. Uses the 00010203...NN-1 pattern for nonce/plaintext/key. The outputs\n"
  393. "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
  394. "step repeated sufficiently. The nonce is fixed throughout.\n\n");
  395. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  396. kl = cipher_descriptor[x].block_length;
  397. /* skip ciphers which do not have 64 or 128 bit block sizes */
  398. if (kl != 8 && kl != 16) continue;
  399. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  400. kl = cipher_descriptor[x].max_key_length;
  401. }
  402. fprintf(out, "OCB-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  403. /* the key */
  404. for (z = 0; z < kl; z++) {
  405. key[z] = (z & 255);
  406. }
  407. /* fixed nonce */
  408. for (z = 0; z < cipher_descriptor[x].block_length; z++) {
  409. nonce[z] = z;
  410. }
  411. for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
  412. for (z = 0; z < y1; z++) {
  413. plaintext[z] = (unsigned char)(z & 255);
  414. }
  415. len = sizeof(tag);
  416. if ((err = ocb_encrypt_authenticate_memory(x, key, kl, nonce, plaintext, y1, plaintext, tag, &len)) != CRYPT_OK) {
  417. printf("Error OCB'ing: %s\n", error_to_string(err));
  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. void base64_gen(void)
  439. {
  440. FILE *out;
  441. unsigned char dst[256], src[32];
  442. unsigned long x, y, len;
  443. out = fopen("base64_tv.txt", "w");
  444. fprintf(out, "Base64 vectors. These are the base64 encodings of the strings 00,01,02...NN-1\n\n");
  445. for (x = 0; x <= 32; x++) {
  446. for (y = 0; y < x; y++) {
  447. src[y] = y;
  448. }
  449. len = sizeof(dst);
  450. base64_encode(src, x, dst, &len);
  451. fprintf(out, "%2lu: %s\n", x, dst);
  452. }
  453. fclose(out);
  454. }
  455. int main(void)
  456. {
  457. reg_algs();
  458. printf("Generating hash vectors..."); fflush(stdout); hash_gen(); printf("done\n");
  459. printf("Generating cipher vectors..."); fflush(stdout); cipher_gen(); printf("done\n");
  460. printf("Generating HMAC vectors..."); fflush(stdout); hmac_gen(); printf("done\n");
  461. printf("Generating OMAC vectors..."); fflush(stdout); omac_gen(); printf("done\n");
  462. printf("Generating PMAC vectors..."); fflush(stdout); pmac_gen(); printf("done\n");
  463. printf("Generating EAX vectors..."); fflush(stdout); eax_gen(); printf("done\n");
  464. printf("Generating OCB vectors..."); fflush(stdout); ocb_gen(); printf("done\n");
  465. printf("Generating BASE64 vectors..."); fflush(stdout); base64_gen(); printf("done\n");
  466. return 0;
  467. }