tv_gen.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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[MAXBLOCKSIZE*2+2];
  87. unsigned long outlen, x, y, z;
  88. FILE *out;
  89. out = fopen("hash_tv.txt", "w");
  90. fprintf(out, "Hash Test Vectors:\n\nThese are the hashes of nn bytes '00 01 02 03 .. (nn-1)'\n\n");
  91. for (x = 0; hash_descriptor[x].name != NULL; x++) {
  92. fprintf(out, "Hash: %s\n", hash_descriptor[x].name);
  93. for (y = 0; y <= (hash_descriptor[x].blocksize * 2); y++) {
  94. for (z = 0; z < y; z++) {
  95. buf[z] = (unsigned char)(z & 255);
  96. }
  97. outlen = sizeof(md);
  98. hash_memory(x, buf, y, md, &outlen);
  99. fprintf(out, "%3lu: ", y);
  100. for (z = 0; z < outlen; z++) {
  101. fprintf(out, "%02X", md[z]);
  102. }
  103. fprintf(out, "\n");
  104. }
  105. fprintf(out, "\n");
  106. }
  107. fclose(out);
  108. }
  109. void cipher_gen(void)
  110. {
  111. unsigned char key[MAXBLOCKSIZE], pt[MAXBLOCKSIZE];
  112. unsigned long x, y, z, w;
  113. int kl, lastkl;
  114. FILE *out;
  115. symmetric_key skey;
  116. out = fopen("cipher_tv.txt", "w");
  117. fprintf(out,
  118. "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"
  119. "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");
  120. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  121. fprintf(out, "Cipher: %s\n", cipher_descriptor[x].name);
  122. /* three modes, smallest, medium, large keys */
  123. lastkl = 10000;
  124. for (y = 0; y < 3; y++) {
  125. switch (y) {
  126. case 0: kl = cipher_descriptor[x].min_key_length; break;
  127. case 1: kl = (cipher_descriptor[x].min_key_length + cipher_descriptor[x].max_key_length)/2; break;
  128. case 2: kl = cipher_descriptor[x].max_key_length; break;
  129. }
  130. cipher_descriptor[x].keysize(&kl);
  131. if (kl == lastkl) break;
  132. lastkl = kl;
  133. fprintf(out, "Key Size: %d bytes\n", kl);
  134. for (z = 0; (int)z < kl; z++) {
  135. key[z] = (unsigned char)z;
  136. }
  137. cipher_descriptor[x].setup(key, kl, 0, &skey);
  138. for (z = 0; (int)z < cipher_descriptor[x].block_length; z++) {
  139. pt[z] = (unsigned char)z;
  140. }
  141. for (w = 0; w < 50; w++) {
  142. cipher_descriptor[x].ecb_encrypt(pt, pt, &skey);
  143. fprintf(out, "%2lu: ", w);
  144. for (z = 0; (int)z < cipher_descriptor[x].block_length; z++) {
  145. fprintf(out, "%02X", pt[z]);
  146. }
  147. fprintf(out, "\n");
  148. /* reschedule a new key */
  149. for (z = 0; z < (unsigned long)kl; z++) {
  150. key[z] = pt[z % cipher_descriptor[x].block_length];
  151. }
  152. cipher_descriptor[x].setup(key, kl, 0, &skey);
  153. }
  154. fprintf(out, "\n");
  155. }
  156. fprintf(out, "\n");
  157. }
  158. fclose(out);
  159. }
  160. void hmac_gen(void)
  161. {
  162. unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], input[MAXBLOCKSIZE*2+2];
  163. int x, y, z, kl, err;
  164. FILE *out;
  165. unsigned long len;
  166. out = fopen("hmac_tv.txt", "w");
  167. fprintf(out,
  168. "HMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are HMACed. The initial key is\n"
  169. "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"
  170. "step N.\n\n");
  171. for (x = 0; hash_descriptor[x].name != NULL; x++) {
  172. fprintf(out, "HMAC-%s\n", hash_descriptor[x].name);
  173. /* initial key */
  174. for (y = 0; y < (int)hash_descriptor[x].hashsize; y++) {
  175. key[y] = (y&255);
  176. }
  177. for (y = 0; y <= (int)(hash_descriptor[x].blocksize * 2); y++) {
  178. for (z = 0; z < y; z++) {
  179. input[z] = (unsigned char)(z & 255);
  180. }
  181. len = sizeof(output);
  182. if ((err = hmac_memory(x, key, hash_descriptor[x].hashsize, input, y, output, &len)) != CRYPT_OK) {
  183. printf("Error hmacing: %s\n", error_to_string(err));
  184. exit(EXIT_FAILURE);
  185. }
  186. fprintf(out, "%3d: ", y);
  187. for (z = 0; z <(int) len; z++) {
  188. fprintf(out, "%02X", output[z]);
  189. }
  190. fprintf(out, "\n");
  191. /* forward the key */
  192. memcpy(key, output, hash_descriptor[x].hashsize);
  193. }
  194. fprintf(out, "\n");
  195. }
  196. fclose(out);
  197. }
  198. void omac_gen(void)
  199. {
  200. unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], input[MAXBLOCKSIZE*2+2];
  201. int err, x, y, z, kl;
  202. FILE *out;
  203. unsigned long len;
  204. out = fopen("omac_tv.txt", "w");
  205. fprintf(out,
  206. "OMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are OMAC'ed. The initial key is\n"
  207. "of the same format (length specified per cipher). The OMAC key in step N+1 is the OMAC output of\n"
  208. "step N (repeated as required to fill the array).\n\n");
  209. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  210. kl = cipher_descriptor[x].block_length;
  211. /* skip ciphers which do not have 64 or 128 bit block sizes */
  212. if (kl != 8 && kl != 16) continue;
  213. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  214. kl = cipher_descriptor[x].max_key_length;
  215. }
  216. fprintf(out, "OMAC-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  217. /* initial key/block */
  218. for (y = 0; y < kl; y++) {
  219. key[y] = (y & 255);
  220. }
  221. for (y = 0; y <= (int)(cipher_descriptor[x].block_length*2); y++) {
  222. for (z = 0; z < y; z++) {
  223. input[z] = (unsigned char)(z & 255);
  224. }
  225. len = sizeof(output);
  226. if ((err = omac_memory(x, key, kl, input, y, output, &len)) != CRYPT_OK) {
  227. printf("Error omacing: %s\n", error_to_string(err));
  228. exit(EXIT_FAILURE);
  229. }
  230. fprintf(out, "%3d: ", y);
  231. for (z = 0; z <(int)len; z++) {
  232. fprintf(out, "%02X", output[z]);
  233. }
  234. fprintf(out, "\n");
  235. /* forward the key */
  236. for (z = 0; z < kl; z++) {
  237. key[z] = output[z % len];
  238. }
  239. }
  240. fprintf(out, "\n");
  241. }
  242. fclose(out);
  243. }
  244. void pmac_gen(void)
  245. {
  246. unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], input[MAXBLOCKSIZE*2+2];
  247. int err, x, y, z, kl;
  248. FILE *out;
  249. unsigned long len;
  250. out = fopen("pmac_tv.txt", "w");
  251. fprintf(out,
  252. "PMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are OMAC'ed. The initial key is\n"
  253. "of the same format (length specified per cipher). The OMAC key in step N+1 is the OMAC output of\n"
  254. "step N (repeated as required to fill the array).\n\n");
  255. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  256. kl = cipher_descriptor[x].block_length;
  257. /* skip ciphers which do not have 64 or 128 bit block sizes */
  258. if (kl != 8 && kl != 16) continue;
  259. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  260. kl = cipher_descriptor[x].max_key_length;
  261. }
  262. fprintf(out, "PMAC-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  263. /* initial key/block */
  264. for (y = 0; y < kl; y++) {
  265. key[y] = (y & 255);
  266. }
  267. for (y = 0; y <= (int)(cipher_descriptor[x].block_length*2); y++) {
  268. for (z = 0; z < y; z++) {
  269. input[z] = (unsigned char)(z & 255);
  270. }
  271. len = sizeof(output);
  272. if ((err = pmac_memory(x, key, kl, input, y, output, &len)) != CRYPT_OK) {
  273. printf("Error omacing: %s\n", error_to_string(err));
  274. exit(EXIT_FAILURE);
  275. }
  276. fprintf(out, "%3d: ", y);
  277. for (z = 0; z <(int)len; z++) {
  278. fprintf(out, "%02X", output[z]);
  279. }
  280. fprintf(out, "\n");
  281. /* forward the key */
  282. for (z = 0; z < kl; z++) {
  283. key[z] = output[z % len];
  284. }
  285. }
  286. fprintf(out, "\n");
  287. }
  288. fclose(out);
  289. }
  290. void eax_gen(void)
  291. {
  292. int err, kl, x, y1, z;
  293. FILE *out;
  294. unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2], header[MAXBLOCKSIZE*2],
  295. plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
  296. unsigned long len;
  297. out = fopen("eax_tv.txt", "w");
  298. fprintf(out, "EAX Test Vectors. Uses the 00010203...NN-1 pattern for header/nonce/plaintext/key. The outputs\n"
  299. "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
  300. "step repeated sufficiently.\n\n");
  301. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  302. kl = cipher_descriptor[x].block_length;
  303. /* skip ciphers which do not have 64 or 128 bit block sizes */
  304. if (kl != 8 && kl != 16) continue;
  305. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  306. kl = cipher_descriptor[x].max_key_length;
  307. }
  308. fprintf(out, "EAX-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  309. /* the key */
  310. for (z = 0; z < kl; z++) {
  311. key[z] = (z & 255);
  312. }
  313. for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
  314. for (z = 0; z < y1; z++) {
  315. plaintext[z] = (unsigned char)(z & 255);
  316. nonce[z] = (unsigned char)(z & 255);
  317. header[z] = (unsigned char)(z & 255);
  318. }
  319. len = sizeof(tag);
  320. if ((err = eax_encrypt_authenticate_memory(x, key, kl, nonce, y1, header, y1, plaintext, y1, plaintext, tag, &len)) != CRYPT_OK) {
  321. printf("Error EAX'ing: %s\n", error_to_string(err));
  322. exit(EXIT_FAILURE);
  323. }
  324. fprintf(out, "%3d: ", y1);
  325. for (z = 0; z < y1; z++) {
  326. fprintf(out, "%02X", plaintext[z]);
  327. }
  328. fprintf(out, ", ");
  329. for (z = 0; z <(int)len; z++) {
  330. fprintf(out, "%02X", tag[z]);
  331. }
  332. fprintf(out, "\n");
  333. /* forward the key */
  334. for (z = 0; z < kl; z++) {
  335. key[z] = tag[z % len];
  336. }
  337. }
  338. fprintf(out, "\n");
  339. }
  340. fclose(out);
  341. }
  342. void ocb_gen(void)
  343. {
  344. int err, kl, x, y1, z;
  345. FILE *out;
  346. unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2],
  347. plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
  348. unsigned long len;
  349. out = fopen("ocb_tv.txt", "w");
  350. fprintf(out, "OCB Test Vectors. Uses the 00010203...NN-1 pattern for nonce/plaintext/key. The outputs\n"
  351. "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
  352. "step repeated sufficiently. The nonce is fixed throughout.\n\n");
  353. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  354. kl = cipher_descriptor[x].block_length;
  355. /* skip ciphers which do not have 64 or 128 bit block sizes */
  356. if (kl != 8 && kl != 16) continue;
  357. if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
  358. kl = cipher_descriptor[x].max_key_length;
  359. }
  360. fprintf(out, "OCB-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
  361. /* the key */
  362. for (z = 0; z < kl; z++) {
  363. key[z] = (z & 255);
  364. }
  365. /* fixed nonce */
  366. for (z = 0; z < cipher_descriptor[x].block_length; z++) {
  367. nonce[z] = z;
  368. }
  369. for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
  370. for (z = 0; z < y1; z++) {
  371. plaintext[z] = (unsigned char)(z & 255);
  372. }
  373. len = sizeof(tag);
  374. if ((err = ocb_encrypt_authenticate_memory(x, key, kl, nonce, plaintext, y1, plaintext, tag, &len)) != CRYPT_OK) {
  375. printf("Error OCB'ing: %s\n", error_to_string(err));
  376. exit(EXIT_FAILURE);
  377. }
  378. fprintf(out, "%3d: ", y1);
  379. for (z = 0; z < y1; z++) {
  380. fprintf(out, "%02X", plaintext[z]);
  381. }
  382. fprintf(out, ", ");
  383. for (z = 0; z <(int)len; z++) {
  384. fprintf(out, "%02X", tag[z]);
  385. }
  386. fprintf(out, "\n");
  387. /* forward the key */
  388. for (z = 0; z < kl; z++) {
  389. key[z] = tag[z % len];
  390. }
  391. }
  392. fprintf(out, "\n");
  393. }
  394. fclose(out);
  395. }
  396. void base64_gen(void)
  397. {
  398. FILE *out;
  399. unsigned char dst[256], src[32];
  400. unsigned long x, y, len;
  401. out = fopen("base64_tv.txt", "w");
  402. fprintf(out, "Base64 vectors. These are the base64 encodings of the strings 00,01,02...NN-1\n\n");
  403. for (x = 0; x <= 32; x++) {
  404. for (y = 0; y < x; y++) {
  405. src[y] = y;
  406. }
  407. len = sizeof(dst);
  408. base64_encode(src, x, dst, &len);
  409. fprintf(out, "%2lu: %s\n", x, dst);
  410. }
  411. fclose(out);
  412. }
  413. int main(void)
  414. {
  415. reg_algs();
  416. printf("Generating hash vectors..."); fflush(stdout); hash_gen(); printf("done\n");
  417. printf("Generating cipher vectors..."); fflush(stdout); cipher_gen(); printf("done\n");
  418. printf("Generating HMAC vectors..."); fflush(stdout); hmac_gen(); printf("done\n");
  419. printf("Generating OMAC vectors..."); fflush(stdout); omac_gen(); printf("done\n");
  420. printf("Generating PMAC vectors..."); fflush(stdout); pmac_gen(); printf("done\n");
  421. printf("Generating EAX vectors..."); fflush(stdout); eax_gen(); printf("done\n");
  422. printf("Generating OCB vectors..."); fflush(stdout); ocb_gen(); printf("done\n");
  423. printf("Generating BASE64 vectors..."); fflush(stdout); base64_gen(); printf("done\n");
  424. return 0;
  425. }