PageRenderTime 59ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/cyassl-1.2.0/ctaocrypt/test/test.c

https://bitbucket.org/mkramer/cyassl
C | 1166 lines | 888 code | 273 blank | 5 comment | 95 complexity | fffed5f62fcf801b691cbfa4b6e49af9 MD5 | raw file
Possible License(s): GPL-2.0
  1. /* test.c */
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "md5.h"
  6. #include "md4.h"
  7. #include "sha.h"
  8. #include "sha256.h"
  9. #include "arc4.h"
  10. #include "random.h"
  11. #include "coding.h"
  12. #include "asn.h"
  13. #include "des3.h"
  14. #include "aes.h"
  15. #include "hmac.h"
  16. #include "dh.h"
  17. #include "dsa.h"
  18. #include "hc128.h"
  19. #include "rabbit.h"
  20. #ifdef _MSC_VER
  21. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  22. #pragma warning(disable: 4996)
  23. #endif
  24. #ifdef OPENSSL_EXTRA
  25. #include "../../include/openssl/evp.h"
  26. #include "../../include/openssl/rand.h"
  27. #include "../../include/openssl/hmac.h"
  28. #include "../../include/openssl/des.h"
  29. #endif
  30. typedef struct testVector {
  31. char* input;
  32. char* output;
  33. size_t inLen;
  34. size_t outLen;
  35. } testVector;
  36. int md5_test();
  37. int md4_test();
  38. int sha_test();
  39. int sha256_test();
  40. int hmac_test();
  41. int arc4_test();
  42. int hc128_test();
  43. int rabbit_test();
  44. int des_test();
  45. int des3_test();
  46. int aes_test();
  47. int rsa_test();
  48. int dh_test();
  49. int dsa_test();
  50. int random_test();
  51. int openssl_test(); /* test mini api */
  52. int PemToDer(const char* inName, const char* outName);
  53. void err_sys(const char* msg, int es)
  54. {
  55. printf("%s error = %d\n", msg, es);
  56. exit(es);
  57. }
  58. /* func_args from test.h, so don't have to pull in other junk */
  59. typedef struct func_args {
  60. int argc;
  61. char** argv;
  62. int return_code;
  63. } func_args;
  64. void ctaocrypt_test(void* args)
  65. {
  66. int ret = 0;
  67. ((func_args*)args)->return_code = -1; /* error state */
  68. if ( (ret = md5_test()) )
  69. err_sys("MD5 test failed!\n", ret);
  70. else
  71. printf( "MD5 test passed!\n");
  72. #ifndef NO_MD4
  73. if ( (ret = md4_test()) )
  74. err_sys("MD4 test failed!\n", ret);
  75. else
  76. printf( "MD4 test passed!\n");
  77. #endif
  78. if ( (ret = sha_test()) )
  79. err_sys("SHA test failed!\n", ret);
  80. else
  81. printf( "SHA test passed!\n");
  82. #ifndef NO_SHA256
  83. if ( (ret = sha256_test()) )
  84. err_sys("SHA-256 test failed!\n", ret);
  85. else
  86. printf( "SHA-256 test passed!\n");
  87. #endif
  88. #ifndef NO_HMAC
  89. if ( (ret = hmac_test()) )
  90. err_sys("HMAC test failed!\n", ret);
  91. else
  92. printf( "HMAC test passed!\n");
  93. #endif
  94. if ( (ret = arc4_test()) )
  95. err_sys("ARC4 test failed!\n", ret);
  96. else
  97. printf( "ARC4 test passed!\n");
  98. #ifndef NO_HC128
  99. if ( (ret = hc128_test()) )
  100. err_sys("HC-128 test failed!\n", ret);
  101. else
  102. printf( "HC-128 test passed!\n");
  103. #endif
  104. #ifndef NO_RABBIT
  105. if ( (ret = rabbit_test()) )
  106. err_sys("Rabbit test failed!\n", ret);
  107. else
  108. printf( "Rabbit test passed!\n");
  109. #endif
  110. #ifndef NO_DES3
  111. if ( (ret = des_test()) )
  112. err_sys("DES test failed!\n", ret);
  113. else
  114. printf( "DES test passed!\n");
  115. #endif
  116. #ifndef NO_DES3
  117. if ( (ret = des3_test()) )
  118. err_sys("DES3 test failed!\n", ret);
  119. else
  120. printf( "DES3 test passed!\n");
  121. #endif
  122. #ifndef NO_AES
  123. if ( (ret = aes_test()) )
  124. err_sys("AES test failed!\n", ret);
  125. else
  126. printf( "AES test passed!\n");
  127. #endif
  128. if ( (ret = random_test()) )
  129. err_sys("RANDOM test failed!\n", ret);
  130. else
  131. printf( "RANDOM test passed!\n");
  132. if ( (ret = rsa_test()) )
  133. err_sys("RSA test failed!\n", ret);
  134. else
  135. printf( "RSA test passed!\n");
  136. #ifndef NO_DH
  137. if ( (ret = dh_test()) )
  138. err_sys("DH test failed!\n", ret);
  139. else
  140. printf( "DH test passed!\n");
  141. #endif
  142. #ifndef NO_DSA
  143. if ( (ret = dsa_test()) )
  144. err_sys("DSA test failed!\n", ret);
  145. else
  146. printf( "DSA test passed!\n");
  147. #endif
  148. #ifdef OPENSSL_EXTRA
  149. if ( (ret = openssl_test()) )
  150. err_sys("OPENSSL test failed!\n", ret);
  151. else
  152. printf( "OPENSSL test passed!\n");
  153. #endif
  154. ((func_args*)args)->return_code = ret;
  155. }
  156. /* so overall tests can pull in test function */
  157. #ifndef NO_MAIN_DRIVER
  158. int main(int argc, char** argv)
  159. {
  160. func_args args;
  161. args.argc = argc;
  162. args.argv = argv;
  163. ctaocrypt_test(&args);
  164. return args.return_code;
  165. }
  166. #endif /* NO_MAIN_DRIVER */
  167. int md5_test()
  168. {
  169. Md5 md5;
  170. byte hash[MD5_DIGEST_SIZE];
  171. testVector a, b, c, d, e;
  172. testVector test_md5[5];
  173. int times = sizeof(test_md5) / sizeof(testVector), i;
  174. a.input = "abc";
  175. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  176. "\x72";
  177. a.inLen = strlen(a.input);
  178. a.outLen = strlen(a.output);
  179. b.input = "message digest";
  180. b.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  181. "\xd0";
  182. b.inLen = strlen(b.input);
  183. b.outLen = strlen(b.output);
  184. c.input = "abcdefghijklmnopqrstuvwxyz";
  185. c.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  186. "\x3b";
  187. c.inLen = strlen(c.input);
  188. c.outLen = strlen(c.output);
  189. d.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  190. "6789";
  191. d.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  192. "\x9f";
  193. d.inLen = strlen(d.input);
  194. d.outLen = strlen(d.output);
  195. e.input = "1234567890123456789012345678901234567890123456789012345678"
  196. "9012345678901234567890";
  197. e.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  198. "\x7a";
  199. e.inLen = strlen(e.input);
  200. e.outLen = strlen(e.output);
  201. test_md5[0] = a;
  202. test_md5[1] = b;
  203. test_md5[2] = c;
  204. test_md5[3] = d;
  205. test_md5[4] = e;
  206. InitMd5(&md5);
  207. for (i = 0; i < times; ++i) {
  208. Md5Update(&md5, (byte*)test_md5[i].input, (word32)test_md5[i].inLen);
  209. Md5Final(&md5, hash);
  210. if (memcmp(hash, test_md5[i].output, MD5_DIGEST_SIZE) != 0)
  211. return -5 - i;
  212. }
  213. return 0;
  214. }
  215. #ifndef NO_MD4
  216. int md4_test()
  217. {
  218. Md4 md4;
  219. byte hash[MD4_DIGEST_SIZE];
  220. testVector a, b, c, d, e, f, g;
  221. testVector test_md4[7];
  222. int times = sizeof(test_md4) / sizeof(testVector), i;
  223. a.input = "";
  224. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  225. "\xc0";
  226. a.inLen = strlen(a.input);
  227. a.outLen = strlen(a.output);
  228. b.input = "a";
  229. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  230. "\x24";
  231. b.inLen = strlen(b.input);
  232. b.outLen = strlen(b.output);
  233. c.input = "abc";
  234. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  235. "\x9d";
  236. c.inLen = strlen(c.input);
  237. c.outLen = strlen(c.output);
  238. d.input = "message digest";
  239. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  240. "\x4b";
  241. d.inLen = strlen(d.input);
  242. d.outLen = strlen(d.output);
  243. e.input = "abcdefghijklmnopqrstuvwxyz";
  244. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  245. "\xa9";
  246. e.inLen = strlen(e.input);
  247. e.outLen = strlen(e.output);
  248. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  249. "6789";
  250. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  251. "\xe4";
  252. f.inLen = strlen(f.input);
  253. f.outLen = strlen(f.output);
  254. g.input = "1234567890123456789012345678901234567890123456789012345678"
  255. "9012345678901234567890";
  256. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  257. "\x36";
  258. g.inLen = strlen(g.input);
  259. g.outLen = strlen(g.output);
  260. test_md4[0] = a;
  261. test_md4[1] = b;
  262. test_md4[2] = c;
  263. test_md4[3] = d;
  264. test_md4[4] = e;
  265. test_md4[5] = f;
  266. test_md4[6] = g;
  267. InitMd4(&md4);
  268. for (i = 0; i < times; ++i) {
  269. Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  270. Md4Final(&md4, hash);
  271. if (memcmp(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  272. return -205 - i;
  273. }
  274. return 0;
  275. }
  276. #endif /* NO_MD4 */
  277. int sha_test()
  278. {
  279. Sha sha;
  280. byte hash[SHA_DIGEST_SIZE];
  281. testVector a, b, c, d;
  282. testVector test_sha[4];
  283. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  284. a.input = "abc";
  285. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  286. "\x6C\x9C\xD0\xD8\x9D";
  287. a.inLen = strlen(a.input);
  288. a.outLen = strlen(a.output);
  289. b.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  290. b.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  291. "\xE5\xE5\x46\x70\xF1";
  292. b.inLen = strlen(b.input);
  293. b.outLen = strlen(b.output);
  294. c.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  295. "aaaaaa";
  296. c.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  297. "\x2A\x25\xEC\x64\x4D";
  298. c.inLen = strlen(c.input);
  299. c.outLen = strlen(c.output);
  300. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  301. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  302. "aaaaaaaaaa";
  303. d.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  304. "\x53\x99\x5E\x26\xA0";
  305. d.inLen = strlen(d.input);
  306. d.outLen = strlen(d.output);
  307. test_sha[0] = a;
  308. test_sha[1] = b;
  309. test_sha[2] = c;
  310. test_sha[3] = d;
  311. InitSha(&sha);
  312. for (i = 0; i < times; ++i) {
  313. ShaUpdate(&sha, (byte*)test_sha[i].input, (word32)test_sha[i].inLen);
  314. ShaFinal(&sha, hash);
  315. if (memcmp(hash, test_sha[i].output, SHA_DIGEST_SIZE) != 0)
  316. return -10 - i;
  317. }
  318. return 0;
  319. }
  320. #ifndef NO_SHA256
  321. int sha256_test()
  322. {
  323. Sha256 sha;
  324. byte hash[SHA256_DIGEST_SIZE];
  325. testVector a, b;
  326. testVector test_sha[2];
  327. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  328. a.input = "abc";
  329. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  330. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  331. "\x15\xAD";
  332. a.inLen = strlen(a.input);
  333. a.outLen = strlen(a.output);
  334. b.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  335. b.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  336. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  337. "\x06\xC1";
  338. b.inLen = strlen(b.input);
  339. b.outLen = strlen(b.output);
  340. test_sha[0] = a;
  341. test_sha[1] = b;
  342. InitSha256(&sha);
  343. for (i = 0; i < times; ++i) {
  344. Sha256Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
  345. Sha256Final(&sha, hash);
  346. if (memcmp(hash, test_sha[i].output, SHA256_DIGEST_SIZE) != 0)
  347. return -10 - i;
  348. }
  349. return 0;
  350. }
  351. #endif
  352. #ifndef NO_HMAC
  353. int hmac_test()
  354. {
  355. Hmac hmac;
  356. byte hash[MD5_DIGEST_SIZE];
  357. const char* keys[]=
  358. {
  359. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  360. "Jefe",
  361. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  362. };
  363. testVector a, b, c;
  364. testVector test_hmac[3];
  365. int times = sizeof(test_hmac) / sizeof(testVector), i;
  366. a.input = "Hi There";
  367. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  368. "\x9d";
  369. a.inLen = strlen(a.input);
  370. a.outLen = strlen(a.output);
  371. b.input = "what do ya want for nothing?";
  372. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  373. "\x38";
  374. b.inLen = strlen(b.input);
  375. b.outLen = strlen(b.output);
  376. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  377. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  378. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  379. "\xDD\xDD\xDD\xDD\xDD\xDD";
  380. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  381. "\xf6";
  382. c.inLen = strlen(c.input);
  383. c.outLen = strlen(c.output);
  384. test_hmac[0] = a;
  385. test_hmac[1] = b;
  386. test_hmac[2] = c;
  387. for (i = 0; i < times; ++i) {
  388. HmacSetKey(&hmac, MD5, (byte*)keys[i], (word32)strlen(keys[i]));
  389. HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  390. (word32)test_hmac[i].inLen);
  391. HmacFinal(&hmac, hash);
  392. if (memcmp(hash, test_hmac[i].output, MD5_DIGEST_SIZE) != 0)
  393. return -20 - i;
  394. }
  395. return 0;
  396. }
  397. #endif
  398. int arc4_test()
  399. {
  400. byte cipher[16];
  401. byte plain[16];
  402. const char* keys[] =
  403. {
  404. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  405. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  406. "\x00\x00\x00\x00\x00\x00\x00\x00",
  407. "\xef\x01\x23\x45"
  408. };
  409. testVector a, b, c, d;
  410. testVector test_arc4[4];
  411. int times = sizeof(test_arc4) / sizeof(testVector), i;
  412. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  413. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  414. a.inLen = strlen(a.input);
  415. a.outLen = strlen(a.output);
  416. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  417. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  418. b.inLen = strlen(b.input);
  419. b.outLen = strlen(b.output);
  420. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  421. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  422. c.inLen = strlen(c.input);
  423. c.outLen = strlen(c.output);
  424. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  425. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  426. d.inLen = strlen(d.input);
  427. d.outLen = strlen(d.output);
  428. test_arc4[0] = a;
  429. test_arc4[1] = b;
  430. test_arc4[2] = c;
  431. test_arc4[3] = d;
  432. for (i = 0; i < times; ++i) {
  433. Arc4 enc;
  434. Arc4 dec;
  435. Arc4SetKey(&enc, (byte*)keys[i], (word32)strlen(keys[i]));
  436. Arc4SetKey(&dec, (byte*)keys[i], (word32)strlen(keys[i]));
  437. Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  438. (word32)test_arc4[i].outLen);
  439. Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  440. if (memcmp(plain, test_arc4[i].input, test_arc4[i].outLen))
  441. return -20 - i;
  442. if (memcmp(cipher, test_arc4[i].output, test_arc4[i].outLen))
  443. return -20 - 5 - i;
  444. }
  445. return 0;
  446. }
  447. #ifndef NO_HC128
  448. int hc128_test()
  449. {
  450. byte cipher[16];
  451. byte plain[16];
  452. const char* keys[] =
  453. {
  454. "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  455. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  456. "\x00\x53\xA6\xF9\x4C\x9F\xF2\x45\x98\xEB\x3E\x91\xE4\x37\x8A\xDD",
  457. "\x0F\x62\xB5\x08\x5B\xAE\x01\x54\xA7\xFA\x4D\xA0\xF3\x46\x99\xEC"
  458. };
  459. const char* ivs[] =
  460. {
  461. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  462. "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  463. "\x0D\x74\xDB\x42\xA9\x10\x77\xDE\x45\xAC\x13\x7A\xE1\x48\xAF\x16",
  464. "\x28\x8F\xF6\x5D\xC4\x2B\x92\xF9\x60\xC7\x2E\x95\xFC\x63\xCA\x31"
  465. };
  466. testVector a, b, c, d;
  467. testVector test_hc128[4];
  468. int times = sizeof(test_hc128) / sizeof(testVector), i;
  469. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  470. a.output = "\x37\x86\x02\xB9\x8F\x32\xA7\x48";
  471. a.inLen = strlen(a.input);
  472. a.outLen = strlen(a.output);
  473. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  474. b.output = "\x33\x7F\x86\x11\xC6\xED\x61\x5F";
  475. b.inLen = strlen(b.input);
  476. b.outLen = strlen(b.output);
  477. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  478. c.output = "\x2E\x1E\xD1\x2A\x85\x51\xC0\x5A";
  479. c.inLen = strlen(c.input);
  480. c.outLen = strlen(c.output);
  481. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  482. d.output = "\x1C\xD8\xAE\xDD\xFE\x52\xE2\x17\xE8\x35\xD0\xB7\xE8\x4E\x29";
  483. d.inLen = strlen(d.input);
  484. d.outLen = strlen(d.output);
  485. test_hc128[0] = a;
  486. test_hc128[1] = b;
  487. test_hc128[2] = c;
  488. test_hc128[3] = d;
  489. for (i = 0; i < times; ++i) {
  490. HC128 enc;
  491. HC128 dec;
  492. Hc128_SetKey(&enc, (byte*)keys[i], (byte*)ivs[i]);
  493. Hc128_SetKey(&dec, (byte*)keys[i], (byte*)ivs[i]);
  494. Hc128_Process(&enc, cipher, (byte*)test_hc128[i].input,
  495. (word32)test_hc128[i].outLen);
  496. Hc128_Process(&dec, plain, cipher, (word32)test_hc128[i].outLen);
  497. if (memcmp(plain, test_hc128[i].input, test_hc128[i].outLen))
  498. return -120 - i;
  499. if (memcmp(cipher, test_hc128[i].output, test_hc128[i].outLen))
  500. return -120 - 5 - i;
  501. }
  502. return 0;
  503. }
  504. #endif /* NO_HC128 */
  505. #ifndef NO_RABBIT
  506. int rabbit_test()
  507. {
  508. byte cipher[16];
  509. byte plain[16];
  510. const char* keys[] =
  511. {
  512. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  513. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  514. "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B\xFE\x36\x3D\x2E\x29\x13\x28\x91"
  515. };
  516. const char* ivs[] =
  517. {
  518. "\x00\x00\x00\x00\x00\x00\x00\x00",
  519. "\x59\x7E\x26\xC1\x75\xF5\x73\xC3",
  520. 0
  521. };
  522. testVector a, b, c;
  523. testVector test_rabbit[3];
  524. int times = sizeof(test_rabbit) / sizeof(testVector), i;
  525. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  526. a.output = "\xED\xB7\x05\x67\x37\x5D\xCD\x7C";
  527. a.inLen = strlen(a.input);
  528. a.outLen = strlen(a.output);
  529. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  530. b.output = "\x6D\x7D\x01\x22\x92\xCC\xDC\xE0";
  531. b.inLen = strlen(b.input);
  532. b.outLen = strlen(b.output);
  533. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  534. c.output = "\x9C\x51\xE2\x87\x84\xC3\x7F\xE9";
  535. c.inLen = strlen(c.input);
  536. c.outLen = strlen(c.output);
  537. test_rabbit[0] = a;
  538. test_rabbit[1] = b;
  539. test_rabbit[2] = c;
  540. for (i = 0; i < times; ++i) {
  541. Rabbit enc;
  542. Rabbit dec;
  543. RabbitSetKey(&enc, (byte*)keys[i], (byte*)ivs[i]);
  544. RabbitSetKey(&dec, (byte*)keys[i], (byte*)ivs[i]);
  545. RabbitProcess(&enc, cipher, (byte*)test_rabbit[i].input,
  546. (word32)test_rabbit[i].outLen);
  547. RabbitProcess(&dec, plain, cipher, (word32)test_rabbit[i].outLen);
  548. if (memcmp(plain, test_rabbit[i].input, test_rabbit[i].outLen))
  549. return -130 - i;
  550. if (memcmp(cipher, test_rabbit[i].output, test_rabbit[i].outLen))
  551. return -130 - 5 - i;
  552. }
  553. return 0;
  554. }
  555. #endif /* NO_RABBIT */
  556. #ifndef NO_DES3
  557. int des_test()
  558. {
  559. const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  560. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  561. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  562. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  563. };
  564. byte plain[24];
  565. byte cipher[24];
  566. Des enc;
  567. Des dec;
  568. const byte key[] =
  569. {
  570. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  571. };
  572. const byte iv[] =
  573. {
  574. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  575. };
  576. const byte verify[] =
  577. {
  578. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  579. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  580. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  581. };
  582. Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  583. Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  584. Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  585. Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  586. if (memcmp(plain, vector, sizeof(plain)))
  587. return -31;
  588. if (memcmp(cipher, verify, sizeof(cipher)))
  589. return -32;
  590. return 0;
  591. }
  592. #endif /* NO_DES3 */
  593. #ifndef NO_DES3
  594. int des3_test()
  595. {
  596. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  597. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  598. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  599. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  600. };
  601. byte plain[24];
  602. byte cipher[24];
  603. Des3 enc;
  604. Des3 dec;
  605. const byte key3[] =
  606. {
  607. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  608. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  609. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  610. };
  611. const byte iv3[] =
  612. {
  613. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  614. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  615. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  616. };
  617. const byte verify3[] =
  618. {
  619. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  620. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  621. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  622. };
  623. Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  624. Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  625. Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  626. Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  627. if (memcmp(plain, vector, sizeof(plain)))
  628. return -33;
  629. if (memcmp(cipher, verify3, sizeof(cipher)))
  630. return -34;
  631. return 0;
  632. }
  633. #endif /* NO_DES */
  634. #ifndef NO_AES
  635. int aes_test()
  636. {
  637. Aes enc;
  638. Aes dec;
  639. const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  640. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  641. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  642. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  643. };
  644. const byte verify[] =
  645. {
  646. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  647. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  648. };
  649. byte key[] = "0123456789abcdef "; /* align */
  650. byte iv[] = "1234567890abcdef "; /* align */
  651. byte cipher[AES_BLOCK_SIZE];
  652. byte plain [AES_BLOCK_SIZE];
  653. AesSetKey(&enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  654. AesSetKey(&dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  655. AesCbcEncrypt(&enc, cipher, msg, AES_BLOCK_SIZE);
  656. AesCbcDecrypt(&dec, plain, cipher, AES_BLOCK_SIZE);
  657. if (memcmp(plain, msg, AES_BLOCK_SIZE))
  658. return -60;
  659. if (memcmp(cipher, verify, AES_BLOCK_SIZE))
  660. return -61;
  661. return 0;
  662. }
  663. #endif /* NO_AES */
  664. int random_test()
  665. {
  666. RNG rng;
  667. byte block[32];
  668. int ret = InitRng(&rng);
  669. if (ret != 0) return -39;
  670. RNG_GenerateBlock(&rng, block, sizeof(block));
  671. return 0;
  672. }
  673. #ifndef NO_MAIN_DRIVER
  674. static const char* clientKey = "../../certs/client-key.der";
  675. static const char* clientCert = "../../certs/client-cert.der";
  676. #else
  677. static const char* clientKey = "../certs/client-key.der";
  678. static const char* clientCert = "../certs/client-cert.der";
  679. #endif
  680. int rsa_test()
  681. {
  682. byte tmp[1024], tmp2[2048];
  683. size_t bytes, bytes2;
  684. RsaKey key;
  685. RNG rng;
  686. word32 idx = 0;
  687. int ret;
  688. byte in[] = "Everyone gets Friday off.";
  689. word32 inLen = (word32)strlen((char*)in);
  690. byte out[64];
  691. byte plain[64];
  692. DecodedCert cert;
  693. FILE* file = fopen(clientKey, "rb"), * file2;
  694. if (!file)
  695. return -40;
  696. bytes = fread(tmp, 1, 1024, file);
  697. InitRsaKey(&key, 0);
  698. ret = RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  699. if (ret != 0) return -41;
  700. ret = InitRng(&rng);
  701. if (ret != 0) return -42;
  702. ret = RsaPublicEncrypt(in, inLen, out, sizeof(out), &key, &rng);
  703. ret = RsaPrivateDecrypt(out, 64, plain, sizeof(plain), &key);
  704. if (memcmp(plain, in, inLen)) return -45;
  705. ret = RsaSSL_Sign(in, inLen, out, sizeof(out), &key, &rng);
  706. memset(plain, 0, sizeof(plain));
  707. ret = RsaSSL_Verify(out, 64, plain, sizeof(plain), &key);
  708. if (memcmp(plain, in, ret)) return -46;
  709. file2 = fopen(clientCert, "rb");
  710. if (!file2)
  711. return -47;
  712. bytes2 = fread(tmp2, 1, 2048, file2);
  713. InitDecodedCert(&cert, (byte*)&tmp2, 0);
  714. ret = ParseCert(&cert, (word32)bytes2, CERT_TYPE, NO_VERIFY, 0);
  715. if (ret != 0) return -48;
  716. FreeDecodedCert(&cert);
  717. FreeRsaKey(&key);
  718. fclose(file2);
  719. fclose(file);
  720. return 0;
  721. }
  722. #ifndef NO_MAIN_DRIVER
  723. static const char* dhKey = "../../certs/dh1024.der";
  724. #else
  725. static const char* dhKey = "../certs/dh1024.der";
  726. #endif
  727. #ifndef NO_DH
  728. int dh_test()
  729. {
  730. int ret;
  731. word32 bytes;
  732. word32 idx = 0, privSz, pubSz, privSz2, pubSz2, agreeSz, agreeSz2;
  733. byte tmp[1024];
  734. byte priv[128];
  735. byte pub[128];
  736. byte priv2[128];
  737. byte pub2[128];
  738. byte agree[128];
  739. byte agree2[128];
  740. DhKey key;
  741. DhKey key2;
  742. RNG rng;
  743. FILE* file = fopen(dhKey, "rb");
  744. if (!file)
  745. return -50;
  746. bytes = (word32) fread(tmp, 1, 1024, file);
  747. InitDhKey(&key);
  748. InitDhKey(&key2);
  749. ret = DhKeyDecode(tmp, &idx, &key, bytes);
  750. if (ret != 0)
  751. return -51;
  752. idx = 0;
  753. ret = DhKeyDecode(tmp, &idx, &key2, bytes);
  754. if (ret != 0)
  755. return -52;
  756. ret = InitRng(&rng);
  757. if (ret != 0)
  758. return -53;
  759. ret = DhGenerateKeyPair(&key, &rng, priv, &privSz, pub, &pubSz);
  760. ret = DhGenerateKeyPair(&key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  761. if (ret != 0)
  762. return -54;
  763. ret = DhAgree(&key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  764. ret = DhAgree(&key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  765. if (ret != 0)
  766. return -55;
  767. if (memcmp(agree, agree2, agreeSz))
  768. return -56;
  769. FreeDhKey(&key);
  770. FreeDhKey(&key2);
  771. fclose(file);
  772. return 0;
  773. }
  774. #endif /* NO_DH */
  775. #ifndef NO_MAIN_DRIVER
  776. static const char* dsaKey = "../../certs/dsa512.der";
  777. #else
  778. static const char* dsaKey = "../certs/dsa512.der";
  779. #endif
  780. #ifndef NO_DSA
  781. int dsa_test()
  782. {
  783. int ret, answer;
  784. word32 bytes;
  785. word32 idx = 0;
  786. byte tmp[1024];
  787. DsaKey key;
  788. RNG rng;
  789. FILE* file = fopen(dsaKey, "rb");
  790. Sha sha;
  791. byte hash[SHA_DIGEST_SIZE];
  792. byte signature[40];
  793. if (!file)
  794. return -60;
  795. bytes = (word32) fread(tmp, 1, sizeof(tmp), file);
  796. InitSha(&sha);
  797. ShaUpdate(&sha, tmp, bytes);
  798. ShaFinal(&sha, hash);
  799. InitDsaKey(&key);
  800. ret = DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  801. if (ret != 0) return -61;
  802. ret = InitRng(&rng);
  803. if (ret != 0) return -62;
  804. ret = DsaSign(hash, signature, &key, &rng);
  805. if (ret != 0) return -63;
  806. ret = DsaVerify(hash, signature, &key, &answer);
  807. if (ret != 0) return -64;
  808. if (answer != 1) return -65;
  809. FreeDsaKey(&key);
  810. fclose(file);
  811. return 0;
  812. }
  813. #endif /* NO_DSA */
  814. #ifdef OPENSSL_EXTRA
  815. int openssl_test()
  816. {
  817. EVP_MD_CTX md_ctx;
  818. testVector a, b, c;
  819. byte hash[SHA_DIGEST_SIZE];
  820. a.input = "1234567890123456789012345678901234567890123456789012345678"
  821. "9012345678901234567890";
  822. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  823. "\x7a";
  824. a.inLen = strlen(a.input);
  825. a.outLen = strlen(a.output);
  826. EVP_MD_CTX_init(&md_ctx);
  827. EVP_DigestInit(&md_ctx, EVP_md5());
  828. EVP_DigestUpdate(&md_ctx, a.input, a.inLen);
  829. EVP_DigestFinal(&md_ctx, hash, 0);
  830. if (memcmp(hash, a.output, MD5_DIGEST_SIZE) != 0)
  831. return -71;
  832. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  833. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  834. "aaaaaaaaaa";
  835. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  836. "\x53\x99\x5E\x26\xA0";
  837. b.inLen = strlen(b.input);
  838. b.outLen = strlen(b.output);
  839. EVP_MD_CTX_init(&md_ctx);
  840. EVP_DigestInit(&md_ctx, EVP_sha1());
  841. EVP_DigestUpdate(&md_ctx, b.input, b.inLen);
  842. EVP_DigestFinal(&md_ctx, hash, 0);
  843. if (memcmp(hash, b.output, SHA_DIGEST_SIZE) != 0)
  844. return -72;
  845. if (RAND_bytes(hash, sizeof(hash)) != 1)
  846. return -73;
  847. c.input = "what do ya want for nothing?";
  848. c.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  849. "\x38";
  850. c.inLen = strlen(c.input);
  851. c.outLen = strlen(c.output);
  852. HMAC(EVP_md5(), "Jefe", 4, (byte*)c.input, (int)c.inLen, hash, 0);
  853. if (memcmp(hash, c.output, MD5_DIGEST_SIZE) != 0)
  854. return -74;
  855. { /* des test */
  856. const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  857. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  858. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  859. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  860. };
  861. byte plain[24];
  862. byte cipher[24];
  863. const_DES_cblock key =
  864. {
  865. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  866. };
  867. DES_cblock iv =
  868. {
  869. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  870. };
  871. DES_key_schedule sched;
  872. const byte verify[] =
  873. {
  874. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  875. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  876. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  877. };
  878. DES_key_sched(&key, &sched);
  879. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  880. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  881. if (memcmp(plain, vector, sizeof(vector)) != 0)
  882. return -75;
  883. if (memcmp(cipher, verify, sizeof(verify)) != 0)
  884. return -76;
  885. /* test changing iv */
  886. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  887. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  888. if (memcmp(cipher, verify, sizeof(verify)) != 0)
  889. return -77;
  890. } /* end des test */
  891. return 0;
  892. }
  893. #endif /* OPENSSL_EXTRA */