tv_gen.c 16 KB

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