PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/FreeRTOS-Plus/CyaSSL/ctaocrypt/test/test.c

https://bitbucket.org/minux/freertos
C | 2276 lines | 1740 code | 501 blank | 35 comment | 227 complexity | 0c6f899162068073949b84b121607b2d MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /* test.c
  2. *
  3. * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
  4. *
  5. * This file is part of CyaSSL.
  6. *
  7. * CyaSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * CyaSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #ifdef CYASSL_TEST_CERT
  28. #include <cyassl/ctaocrypt/asn.h>
  29. #else
  30. #include <cyassl/ctaocrypt/asn_public.h>
  31. #endif
  32. #include <cyassl/ctaocrypt/md2.h>
  33. #include <cyassl/ctaocrypt/md5.h>
  34. #include <cyassl/ctaocrypt/md4.h>
  35. #include <cyassl/ctaocrypt/sha.h>
  36. #include <cyassl/ctaocrypt/sha256.h>
  37. #include <cyassl/ctaocrypt/sha512.h>
  38. #include <cyassl/ctaocrypt/arc4.h>
  39. #include <cyassl/ctaocrypt/random.h>
  40. #include <cyassl/ctaocrypt/coding.h>
  41. #include <cyassl/ctaocrypt/rsa.h>
  42. #include <cyassl/ctaocrypt/des3.h>
  43. #include <cyassl/ctaocrypt/aes.h>
  44. #include <cyassl/ctaocrypt/hmac.h>
  45. #include <cyassl/ctaocrypt/dh.h>
  46. #include <cyassl/ctaocrypt/dsa.h>
  47. #include <cyassl/ctaocrypt/hc128.h>
  48. #include <cyassl/ctaocrypt/rabbit.h>
  49. #include <cyassl/ctaocrypt/pwdbased.h>
  50. #include <cyassl/ctaocrypt/ripemd.h>
  51. #ifdef HAVE_ECC
  52. #include <cyassl/ctaocrypt/ecc.h>
  53. #endif
  54. #ifdef _MSC_VER
  55. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  56. #pragma warning(disable: 4996)
  57. #endif
  58. #ifdef OPENSSL_EXTRA
  59. #include <cyassl/openssl/evp.h>
  60. #include <cyassl/openssl/rand.h>
  61. #include <cyassl/openssl/hmac.h>
  62. #include <cyassl/openssl/des.h>
  63. #endif
  64. #ifdef HAVE_NTRU
  65. #include "crypto_ntru.h"
  66. #endif
  67. #ifdef THREADX
  68. /* since just testing, use THREADX log printf instead */
  69. int dc_log_printf(char*, ...);
  70. #undef printf
  71. #define printf dc_log_printf
  72. #endif
  73. typedef struct testVector {
  74. char* input;
  75. char* output;
  76. size_t inLen;
  77. size_t outLen;
  78. } testVector;
  79. int md2_test();
  80. int md5_test();
  81. int md4_test();
  82. int sha_test();
  83. int sha256_test();
  84. int sha512_test();
  85. int sha384_test();
  86. int hmac_test();
  87. int arc4_test();
  88. int hc128_test();
  89. int rabbit_test();
  90. int des_test();
  91. int des3_test();
  92. int aes_test();
  93. int aesgcm_test();
  94. int rsa_test();
  95. int dh_test();
  96. int dsa_test();
  97. int random_test();
  98. int pwdbased_test();
  99. int ripemd_test();
  100. int openssl_test(); /* test mini api */
  101. #ifdef HAVE_ECC
  102. int ecc_test();
  103. #endif
  104. int PemToDer(const char* inName, const char* outName);
  105. void err_sys(const char* msg, int es)
  106. {
  107. printf("%s error = %d\n", msg, es);
  108. #ifndef THREADX
  109. exit(es);
  110. #endif
  111. }
  112. /* func_args from test.h, so don't have to pull in other junk */
  113. typedef struct func_args {
  114. int argc;
  115. char** argv;
  116. int return_code;
  117. } func_args;
  118. void ctaocrypt_test(void* args)
  119. {
  120. int ret = 0;
  121. ((func_args*)args)->return_code = -1; /* error state */
  122. if (CheckCtcSettings() != 1)
  123. err_sys("Build vs runtime math mismatch\n", -1234);
  124. #ifdef USE_FAST_MATH
  125. if (CheckFastMathSettings() != 1)
  126. err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n", -1235);
  127. #endif
  128. if ( (ret = md5_test()) )
  129. err_sys("MD5 test failed!\n", ret);
  130. else
  131. printf( "MD5 test passed!\n");
  132. #ifdef CYASSL_MD2
  133. if ( (ret = md2_test()) )
  134. err_sys("MD2 test failed!\n", ret);
  135. else
  136. printf( "MD2 test passed!\n");
  137. #endif
  138. #ifndef NO_MD4
  139. if ( (ret = md4_test()) )
  140. err_sys("MD4 test failed!\n", ret);
  141. else
  142. printf( "MD4 test passed!\n");
  143. #endif
  144. if ( (ret = sha_test()) )
  145. err_sys("SHA test failed!\n", ret);
  146. else
  147. printf( "SHA test passed!\n");
  148. #ifndef NO_SHA256
  149. if ( (ret = sha256_test()) )
  150. err_sys("SHA-256 test failed!\n", ret);
  151. else
  152. printf( "SHA-256 test passed!\n");
  153. #endif
  154. #ifdef CYASSL_SHA384
  155. if ( (ret = sha384_test()) )
  156. err_sys("SHA-384 test failed!\n", ret);
  157. else
  158. printf( "SHA-384 test passed!\n");
  159. #endif
  160. #ifdef CYASSL_SHA512
  161. if ( (ret = sha512_test()) )
  162. err_sys("SHA-512 test failed!\n", ret);
  163. else
  164. printf( "SHA-512 test passed!\n");
  165. #endif
  166. #ifdef CYASSL_RIPEMD
  167. if ( (ret = ripemd_test()) )
  168. err_sys("RIPEMD test failed!\n", ret);
  169. else
  170. printf( "RIPEMD test passed!\n");
  171. #endif
  172. #ifndef NO_HMAC
  173. if ( (ret = hmac_test()) )
  174. err_sys("HMAC test failed!\n", ret);
  175. else
  176. printf( "HMAC test passed!\n");
  177. #endif
  178. if ( (ret = arc4_test()) )
  179. err_sys("ARC4 test failed!\n", ret);
  180. else
  181. printf( "ARC4 test passed!\n");
  182. #ifndef NO_HC128
  183. if ( (ret = hc128_test()) )
  184. err_sys("HC-128 test failed!\n", ret);
  185. else
  186. printf( "HC-128 test passed!\n");
  187. #endif
  188. #ifndef NO_RABBIT
  189. if ( (ret = rabbit_test()) )
  190. err_sys("Rabbit test failed!\n", ret);
  191. else
  192. printf( "Rabbit test passed!\n");
  193. #endif
  194. #ifndef NO_DES3
  195. if ( (ret = des_test()) )
  196. err_sys("DES test failed!\n", ret);
  197. else
  198. printf( "DES test passed!\n");
  199. #endif
  200. #ifndef NO_DES3
  201. if ( (ret = des3_test()) )
  202. err_sys("DES3 test failed!\n", ret);
  203. else
  204. printf( "DES3 test passed!\n");
  205. #endif
  206. #ifndef NO_AES
  207. if ( (ret = aes_test()) )
  208. err_sys("AES test failed!\n", ret);
  209. else
  210. printf( "AES test passed!\n");
  211. #ifdef HAVE_AESGCM
  212. if ( (ret = aesgcm_test()) )
  213. err_sys("AES-GCM test failed!\n", ret);
  214. else
  215. printf( "AES-GCM test passed!\n");
  216. #endif
  217. #endif
  218. if ( (ret = random_test()) )
  219. err_sys("RANDOM test failed!\n", ret);
  220. else
  221. printf( "RANDOM test passed!\n");
  222. if ( (ret = rsa_test()) )
  223. err_sys("RSA test failed!\n", ret);
  224. else
  225. printf( "RSA test passed!\n");
  226. #ifndef NO_DH
  227. if ( (ret = dh_test()) )
  228. err_sys("DH test failed!\n", ret);
  229. else
  230. printf( "DH test passed!\n");
  231. #endif
  232. #ifndef NO_DSA
  233. if ( (ret = dsa_test()) )
  234. err_sys("DSA test failed!\n", ret);
  235. else
  236. printf( "DSA test passed!\n");
  237. #endif
  238. #ifndef NO_PWDBASED
  239. if ( (ret = pwdbased_test()) )
  240. err_sys("PWDBASED test failed!\n", ret);
  241. else
  242. printf( "PWDBASED test passed!\n");
  243. #endif
  244. #ifdef OPENSSL_EXTRA
  245. if ( (ret = openssl_test()) )
  246. err_sys("OPENSSL test failed!\n", ret);
  247. else
  248. printf( "OPENSSL test passed!\n");
  249. #endif
  250. #ifdef HAVE_ECC
  251. if ( (ret = ecc_test()) )
  252. err_sys("ECC test failed!\n", ret);
  253. else
  254. printf( "ECC test passed!\n");
  255. #endif
  256. ((func_args*)args)->return_code = ret;
  257. }
  258. /* so overall tests can pull in test function */
  259. #ifndef NO_MAIN_DRIVER
  260. int main(int argc, char** argv)
  261. {
  262. func_args args;
  263. args.argc = argc;
  264. args.argv = argv;
  265. ctaocrypt_test(&args);
  266. return args.return_code;
  267. }
  268. #endif /* NO_MAIN_DRIVER */
  269. #ifdef CYASSL_MD2
  270. int md2_test()
  271. {
  272. Md2 md2;
  273. byte hash[MD2_DIGEST_SIZE];
  274. testVector a, b, c, d, e, f, g;
  275. testVector test_md2[7];
  276. int times = sizeof(test_md2) / sizeof(testVector), i;
  277. a.input = "";
  278. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  279. "\x27\x73";
  280. a.inLen = strlen(a.input);
  281. a.outLen = strlen(a.output);
  282. b.input = "a";
  283. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  284. "\xb5\xd1";
  285. b.inLen = strlen(b.input);
  286. b.outLen = strlen(b.output);
  287. c.input = "abc";
  288. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  289. "\xd6\xbb";
  290. c.inLen = strlen(c.input);
  291. c.outLen = strlen(c.output);
  292. d.input = "message digest";
  293. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  294. "\x06\xb0";
  295. d.inLen = strlen(d.input);
  296. d.outLen = strlen(d.output);
  297. e.input = "abcdefghijklmnopqrstuvwxyz";
  298. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  299. "\x94\x0b";
  300. e.inLen = strlen(e.input);
  301. e.outLen = strlen(e.output);
  302. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  303. "6789";
  304. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  305. "\x38\xcd";
  306. f.inLen = strlen(f.input);
  307. f.outLen = strlen(f.output);
  308. g.input = "1234567890123456789012345678901234567890123456789012345678"
  309. "9012345678901234567890";
  310. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  311. "\xef\xd8";
  312. g.inLen = strlen(g.input);
  313. g.outLen = strlen(g.output);
  314. test_md2[0] = a;
  315. test_md2[1] = b;
  316. test_md2[2] = c;
  317. test_md2[3] = d;
  318. test_md2[4] = e;
  319. test_md2[5] = f;
  320. test_md2[6] = g;
  321. InitMd2(&md2);
  322. for (i = 0; i < times; ++i) {
  323. Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  324. Md2Final(&md2, hash);
  325. if (memcmp(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  326. return -155 - i;
  327. }
  328. return 0;
  329. }
  330. #endif
  331. int md5_test()
  332. {
  333. Md5 md5;
  334. byte hash[MD5_DIGEST_SIZE];
  335. testVector a, b, c, d, e;
  336. testVector test_md5[5];
  337. int times = sizeof(test_md5) / sizeof(testVector), i;
  338. a.input = "abc";
  339. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  340. "\x72";
  341. a.inLen = strlen(a.input);
  342. a.outLen = strlen(a.output);
  343. b.input = "message digest";
  344. b.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  345. "\xd0";
  346. b.inLen = strlen(b.input);
  347. b.outLen = strlen(b.output);
  348. c.input = "abcdefghijklmnopqrstuvwxyz";
  349. c.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  350. "\x3b";
  351. c.inLen = strlen(c.input);
  352. c.outLen = strlen(c.output);
  353. d.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  354. "6789";
  355. d.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  356. "\x9f";
  357. d.inLen = strlen(d.input);
  358. d.outLen = strlen(d.output);
  359. e.input = "1234567890123456789012345678901234567890123456789012345678"
  360. "9012345678901234567890";
  361. e.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  362. "\x7a";
  363. e.inLen = strlen(e.input);
  364. e.outLen = strlen(e.output);
  365. test_md5[0] = a;
  366. test_md5[1] = b;
  367. test_md5[2] = c;
  368. test_md5[3] = d;
  369. test_md5[4] = e;
  370. InitMd5(&md5);
  371. for (i = 0; i < times; ++i) {
  372. Md5Update(&md5, (byte*)test_md5[i].input, (word32)test_md5[i].inLen);
  373. Md5Final(&md5, hash);
  374. if (memcmp(hash, test_md5[i].output, MD5_DIGEST_SIZE) != 0)
  375. return -5 - i;
  376. }
  377. return 0;
  378. }
  379. #ifndef NO_MD4
  380. int md4_test()
  381. {
  382. Md4 md4;
  383. byte hash[MD4_DIGEST_SIZE];
  384. testVector a, b, c, d, e, f, g;
  385. testVector test_md4[7];
  386. int times = sizeof(test_md4) / sizeof(testVector), i;
  387. a.input = "";
  388. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  389. "\xc0";
  390. a.inLen = strlen(a.input);
  391. a.outLen = strlen(a.output);
  392. b.input = "a";
  393. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  394. "\x24";
  395. b.inLen = strlen(b.input);
  396. b.outLen = strlen(b.output);
  397. c.input = "abc";
  398. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  399. "\x9d";
  400. c.inLen = strlen(c.input);
  401. c.outLen = strlen(c.output);
  402. d.input = "message digest";
  403. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  404. "\x4b";
  405. d.inLen = strlen(d.input);
  406. d.outLen = strlen(d.output);
  407. e.input = "abcdefghijklmnopqrstuvwxyz";
  408. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  409. "\xa9";
  410. e.inLen = strlen(e.input);
  411. e.outLen = strlen(e.output);
  412. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  413. "6789";
  414. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  415. "\xe4";
  416. f.inLen = strlen(f.input);
  417. f.outLen = strlen(f.output);
  418. g.input = "1234567890123456789012345678901234567890123456789012345678"
  419. "9012345678901234567890";
  420. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  421. "\x36";
  422. g.inLen = strlen(g.input);
  423. g.outLen = strlen(g.output);
  424. test_md4[0] = a;
  425. test_md4[1] = b;
  426. test_md4[2] = c;
  427. test_md4[3] = d;
  428. test_md4[4] = e;
  429. test_md4[5] = f;
  430. test_md4[6] = g;
  431. InitMd4(&md4);
  432. for (i = 0; i < times; ++i) {
  433. Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  434. Md4Final(&md4, hash);
  435. if (memcmp(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  436. return -205 - i;
  437. }
  438. return 0;
  439. }
  440. #endif /* NO_MD4 */
  441. int sha_test()
  442. {
  443. Sha sha;
  444. byte hash[SHA_DIGEST_SIZE];
  445. testVector a, b, c, d;
  446. testVector test_sha[4];
  447. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  448. a.input = "abc";
  449. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  450. "\x6C\x9C\xD0\xD8\x9D";
  451. a.inLen = strlen(a.input);
  452. a.outLen = strlen(a.output);
  453. b.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  454. b.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  455. "\xE5\xE5\x46\x70\xF1";
  456. b.inLen = strlen(b.input);
  457. b.outLen = strlen(b.output);
  458. c.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  459. "aaaaaa";
  460. c.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  461. "\x2A\x25\xEC\x64\x4D";
  462. c.inLen = strlen(c.input);
  463. c.outLen = strlen(c.output);
  464. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  465. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  466. "aaaaaaaaaa";
  467. d.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  468. "\x53\x99\x5E\x26\xA0";
  469. d.inLen = strlen(d.input);
  470. d.outLen = strlen(d.output);
  471. test_sha[0] = a;
  472. test_sha[1] = b;
  473. test_sha[2] = c;
  474. test_sha[3] = d;
  475. InitSha(&sha);
  476. for (i = 0; i < times; ++i) {
  477. ShaUpdate(&sha, (byte*)test_sha[i].input, (word32)test_sha[i].inLen);
  478. ShaFinal(&sha, hash);
  479. if (memcmp(hash, test_sha[i].output, SHA_DIGEST_SIZE) != 0)
  480. return -10 - i;
  481. }
  482. return 0;
  483. }
  484. #ifdef CYASSL_RIPEMD
  485. int ripemd_test()
  486. {
  487. RipeMd ripemd;
  488. byte hash[RIPEMD_DIGEST_SIZE];
  489. testVector a, b, c, d;
  490. testVector test_ripemd[4];
  491. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  492. a.input = "abc";
  493. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  494. "\xb0\x87\xf1\x5a\x0b\xfc";
  495. a.inLen = strlen(a.input);
  496. a.outLen = strlen(a.output);
  497. b.input = "message digest";
  498. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  499. "\x5f\xfa\x21\x59\x5f\x36";
  500. b.inLen = strlen(b.input);
  501. b.outLen = strlen(b.output);
  502. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  503. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  504. "\xf4\x9a\xda\x62\xeb\x2b";
  505. c.inLen = strlen(c.input);
  506. c.outLen = strlen(c.output);
  507. d.input = "12345678901234567890123456789012345678901234567890123456"
  508. "789012345678901234567890";
  509. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  510. "\x82\xbf\x63\x32\x6b\xfb";
  511. d.inLen = strlen(d.input);
  512. d.outLen = strlen(d.output);
  513. test_ripemd[0] = a;
  514. test_ripemd[1] = b;
  515. test_ripemd[2] = c;
  516. test_ripemd[3] = d;
  517. InitRipeMd(&ripemd);
  518. for (i = 0; i < times; ++i) {
  519. RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  520. (word32)test_ripemd[i].inLen);
  521. RipeMdFinal(&ripemd, hash);
  522. if (memcmp(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  523. return -10 - i;
  524. }
  525. return 0;
  526. }
  527. #endif /* CYASSL_RIPEMD */
  528. #ifndef NO_SHA256
  529. int sha256_test()
  530. {
  531. Sha256 sha;
  532. byte hash[SHA256_DIGEST_SIZE];
  533. testVector a, b;
  534. testVector test_sha[2];
  535. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  536. a.input = "abc";
  537. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  538. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  539. "\x15\xAD";
  540. a.inLen = strlen(a.input);
  541. a.outLen = strlen(a.output);
  542. b.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  543. b.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  544. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  545. "\x06\xC1";
  546. b.inLen = strlen(b.input);
  547. b.outLen = strlen(b.output);
  548. test_sha[0] = a;
  549. test_sha[1] = b;
  550. InitSha256(&sha);
  551. for (i = 0; i < times; ++i) {
  552. Sha256Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
  553. Sha256Final(&sha, hash);
  554. if (memcmp(hash, test_sha[i].output, SHA256_DIGEST_SIZE) != 0)
  555. return -10 - i;
  556. }
  557. return 0;
  558. }
  559. #endif
  560. #ifdef CYASSL_SHA512
  561. int sha512_test()
  562. {
  563. Sha512 sha;
  564. byte hash[SHA512_DIGEST_SIZE];
  565. testVector a, b;
  566. testVector test_sha[2];
  567. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  568. a.input = "abc";
  569. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  570. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  571. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  572. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  573. "\xa5\x4c\xa4\x9f";
  574. a.inLen = strlen(a.input);
  575. a.outLen = strlen(a.output);
  576. b.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  577. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  578. b.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  579. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  580. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  581. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  582. "\x87\x4b\xe9\x09";
  583. b.inLen = strlen(b.input);
  584. b.outLen = strlen(b.output);
  585. test_sha[0] = a;
  586. test_sha[1] = b;
  587. InitSha512(&sha);
  588. for (i = 0; i < times; ++i) {
  589. Sha512Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
  590. Sha512Final(&sha, hash);
  591. if (memcmp(hash, test_sha[i].output, SHA512_DIGEST_SIZE) != 0)
  592. return -10 - i;
  593. }
  594. return 0;
  595. }
  596. #endif
  597. #ifdef CYASSL_SHA384
  598. int sha384_test()
  599. {
  600. Sha384 sha;
  601. byte hash[SHA384_DIGEST_SIZE];
  602. testVector a, b;
  603. testVector test_sha[2];
  604. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  605. a.input = "abc";
  606. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  607. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  608. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  609. "\xc8\x25\xa7";
  610. a.inLen = strlen(a.input);
  611. a.outLen = strlen(a.output);
  612. b.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  613. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  614. b.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  615. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  616. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  617. "\x74\x60\x39";
  618. b.inLen = strlen(b.input);
  619. b.outLen = strlen(b.output);
  620. test_sha[0] = a;
  621. test_sha[1] = b;
  622. InitSha384(&sha);
  623. for (i = 0; i < times; ++i) {
  624. Sha384Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
  625. Sha384Final(&sha, hash);
  626. if (memcmp(hash, test_sha[i].output, SHA384_DIGEST_SIZE) != 0)
  627. return -10 - i;
  628. }
  629. return 0;
  630. }
  631. #endif /* CYASSL_SHA384 */
  632. #ifndef NO_HMAC
  633. int hmac_test()
  634. {
  635. Hmac hmac;
  636. byte hash[MD5_DIGEST_SIZE];
  637. const char* keys[]=
  638. {
  639. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  640. "Jefe",
  641. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  642. };
  643. testVector a, b, c;
  644. testVector test_hmac[3];
  645. int times = sizeof(test_hmac) / sizeof(testVector), i;
  646. a.input = "Hi There";
  647. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  648. "\x9d";
  649. a.inLen = strlen(a.input);
  650. a.outLen = strlen(a.output);
  651. b.input = "what do ya want for nothing?";
  652. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  653. "\x38";
  654. b.inLen = strlen(b.input);
  655. b.outLen = strlen(b.output);
  656. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  657. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  658. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  659. "\xDD\xDD\xDD\xDD\xDD\xDD";
  660. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  661. "\xf6";
  662. c.inLen = strlen(c.input);
  663. c.outLen = strlen(c.output);
  664. test_hmac[0] = a;
  665. test_hmac[1] = b;
  666. test_hmac[2] = c;
  667. for (i = 0; i < times; ++i) {
  668. HmacSetKey(&hmac, MD5, (byte*)keys[i], (word32)strlen(keys[i]));
  669. HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  670. (word32)test_hmac[i].inLen);
  671. HmacFinal(&hmac, hash);
  672. if (memcmp(hash, test_hmac[i].output, MD5_DIGEST_SIZE) != 0)
  673. return -20 - i;
  674. }
  675. return 0;
  676. }
  677. #endif
  678. int arc4_test()
  679. {
  680. byte cipher[16];
  681. byte plain[16];
  682. const char* keys[] =
  683. {
  684. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  685. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  686. "\x00\x00\x00\x00\x00\x00\x00\x00",
  687. "\xef\x01\x23\x45"
  688. };
  689. testVector a, b, c, d;
  690. testVector test_arc4[4];
  691. int times = sizeof(test_arc4) / sizeof(testVector), i;
  692. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  693. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  694. a.inLen = strlen(a.input);
  695. a.outLen = strlen(a.output);
  696. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  697. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  698. b.inLen = strlen(b.input);
  699. b.outLen = strlen(b.output);
  700. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  701. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  702. c.inLen = strlen(c.input);
  703. c.outLen = strlen(c.output);
  704. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  705. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  706. d.inLen = strlen(d.input);
  707. d.outLen = strlen(d.output);
  708. test_arc4[0] = a;
  709. test_arc4[1] = b;
  710. test_arc4[2] = c;
  711. test_arc4[3] = d;
  712. for (i = 0; i < times; ++i) {
  713. Arc4 enc;
  714. Arc4 dec;
  715. Arc4SetKey(&enc, (byte*)keys[i], (word32)strlen(keys[i]));
  716. Arc4SetKey(&dec, (byte*)keys[i], (word32)strlen(keys[i]));
  717. Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  718. (word32)test_arc4[i].outLen);
  719. Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  720. if (memcmp(plain, test_arc4[i].input, test_arc4[i].outLen))
  721. return -20 - i;
  722. if (memcmp(cipher, test_arc4[i].output, test_arc4[i].outLen))
  723. return -20 - 5 - i;
  724. }
  725. return 0;
  726. }
  727. int hc128_test()
  728. {
  729. #ifdef HAVE_HC128
  730. byte cipher[16];
  731. byte plain[16];
  732. const char* keys[] =
  733. {
  734. "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  735. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  736. "\x00\x53\xA6\xF9\x4C\x9F\xF2\x45\x98\xEB\x3E\x91\xE4\x37\x8A\xDD",
  737. "\x0F\x62\xB5\x08\x5B\xAE\x01\x54\xA7\xFA\x4D\xA0\xF3\x46\x99\xEC"
  738. };
  739. const char* ivs[] =
  740. {
  741. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  742. "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  743. "\x0D\x74\xDB\x42\xA9\x10\x77\xDE\x45\xAC\x13\x7A\xE1\x48\xAF\x16",
  744. "\x28\x8F\xF6\x5D\xC4\x2B\x92\xF9\x60\xC7\x2E\x95\xFC\x63\xCA\x31"
  745. };
  746. testVector a, b, c, d;
  747. testVector test_hc128[4];
  748. int times = sizeof(test_hc128) / sizeof(testVector), i;
  749. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  750. a.output = "\x37\x86\x02\xB9\x8F\x32\xA7\x48";
  751. a.inLen = strlen(a.input);
  752. a.outLen = strlen(a.output);
  753. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  754. b.output = "\x33\x7F\x86\x11\xC6\xED\x61\x5F";
  755. b.inLen = strlen(b.input);
  756. b.outLen = strlen(b.output);
  757. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  758. c.output = "\x2E\x1E\xD1\x2A\x85\x51\xC0\x5A";
  759. c.inLen = strlen(c.input);
  760. c.outLen = strlen(c.output);
  761. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  762. d.output = "\x1C\xD8\xAE\xDD\xFE\x52\xE2\x17\xE8\x35\xD0\xB7\xE8\x4E\x29";
  763. d.inLen = strlen(d.input);
  764. d.outLen = strlen(d.output);
  765. test_hc128[0] = a;
  766. test_hc128[1] = b;
  767. test_hc128[2] = c;
  768. test_hc128[3] = d;
  769. for (i = 0; i < times; ++i) {
  770. HC128 enc;
  771. HC128 dec;
  772. Hc128_SetKey(&enc, (byte*)keys[i], (byte*)ivs[i]);
  773. Hc128_SetKey(&dec, (byte*)keys[i], (byte*)ivs[i]);
  774. Hc128_Process(&enc, cipher, (byte*)test_hc128[i].input,
  775. (word32)test_hc128[i].outLen);
  776. Hc128_Process(&dec, plain, cipher, (word32)test_hc128[i].outLen);
  777. if (memcmp(plain, test_hc128[i].input, test_hc128[i].outLen))
  778. return -120 - i;
  779. if (memcmp(cipher, test_hc128[i].output, test_hc128[i].outLen))
  780. return -120 - 5 - i;
  781. }
  782. #endif /* HAVE_HC128 */
  783. return 0;
  784. }
  785. #ifndef NO_RABBIT
  786. int rabbit_test()
  787. {
  788. byte cipher[16];
  789. byte plain[16];
  790. const char* keys[] =
  791. {
  792. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  793. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  794. "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B\xFE\x36\x3D\x2E\x29\x13\x28\x91"
  795. };
  796. const char* ivs[] =
  797. {
  798. "\x00\x00\x00\x00\x00\x00\x00\x00",
  799. "\x59\x7E\x26\xC1\x75\xF5\x73\xC3",
  800. 0
  801. };
  802. testVector a, b, c;
  803. testVector test_rabbit[3];
  804. int times = sizeof(test_rabbit) / sizeof(testVector), i;
  805. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  806. a.output = "\xED\xB7\x05\x67\x37\x5D\xCD\x7C";
  807. a.inLen = strlen(a.input);
  808. a.outLen = strlen(a.output);
  809. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  810. b.output = "\x6D\x7D\x01\x22\x92\xCC\xDC\xE0";
  811. b.inLen = strlen(b.input);
  812. b.outLen = strlen(b.output);
  813. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  814. c.output = "\x9C\x51\xE2\x87\x84\xC3\x7F\xE9";
  815. c.inLen = strlen(c.input);
  816. c.outLen = strlen(c.output);
  817. test_rabbit[0] = a;
  818. test_rabbit[1] = b;
  819. test_rabbit[2] = c;
  820. for (i = 0; i < times; ++i) {
  821. Rabbit enc;
  822. Rabbit dec;
  823. RabbitSetKey(&enc, (byte*)keys[i], (byte*)ivs[i]);
  824. RabbitSetKey(&dec, (byte*)keys[i], (byte*)ivs[i]);
  825. RabbitProcess(&enc, cipher, (byte*)test_rabbit[i].input,
  826. (word32)test_rabbit[i].outLen);
  827. RabbitProcess(&dec, plain, cipher, (word32)test_rabbit[i].outLen);
  828. if (memcmp(plain, test_rabbit[i].input, test_rabbit[i].outLen))
  829. return -130 - i;
  830. if (memcmp(cipher, test_rabbit[i].output, test_rabbit[i].outLen))
  831. return -130 - 5 - i;
  832. }
  833. return 0;
  834. }
  835. #endif /* NO_RABBIT */
  836. #ifndef NO_DES3
  837. int des_test()
  838. {
  839. const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  840. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  841. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  842. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  843. };
  844. byte plain[24];
  845. byte cipher[24];
  846. Des enc;
  847. Des dec;
  848. const byte key[] =
  849. {
  850. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  851. };
  852. const byte iv[] =
  853. {
  854. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  855. };
  856. const byte verify[] =
  857. {
  858. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  859. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  860. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  861. };
  862. Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  863. Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  864. Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  865. Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  866. if (memcmp(plain, vector, sizeof(plain)))
  867. return -31;
  868. if (memcmp(cipher, verify, sizeof(cipher)))
  869. return -32;
  870. return 0;
  871. }
  872. #endif /* NO_DES3 */
  873. #ifndef NO_DES3
  874. int des3_test()
  875. {
  876. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  877. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  878. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  879. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  880. };
  881. byte plain[24];
  882. byte cipher[24];
  883. Des3 enc;
  884. Des3 dec;
  885. const byte key3[] =
  886. {
  887. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  888. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  889. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  890. };
  891. const byte iv3[] =
  892. {
  893. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  894. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  895. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  896. };
  897. const byte verify3[] =
  898. {
  899. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  900. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  901. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  902. };
  903. Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  904. Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  905. Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  906. Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  907. if (memcmp(plain, vector, sizeof(plain)))
  908. return -33;
  909. if (memcmp(cipher, verify3, sizeof(cipher)))
  910. return -34;
  911. return 0;
  912. }
  913. #endif /* NO_DES */
  914. #ifndef NO_AES
  915. int aes_test()
  916. {
  917. Aes enc;
  918. Aes dec;
  919. const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  920. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  921. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  922. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  923. };
  924. const byte verify[] =
  925. {
  926. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  927. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  928. };
  929. byte key[] = "0123456789abcdef "; /* align */
  930. byte iv[] = "1234567890abcdef "; /* align */
  931. byte cipher[AES_BLOCK_SIZE * 4];
  932. byte plain [AES_BLOCK_SIZE * 4];
  933. AesSetKey(&enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  934. AesSetKey(&dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  935. AesCbcEncrypt(&enc, cipher, msg, AES_BLOCK_SIZE);
  936. AesCbcDecrypt(&dec, plain, cipher, AES_BLOCK_SIZE);
  937. if (memcmp(plain, msg, AES_BLOCK_SIZE))
  938. return -60;
  939. if (memcmp(cipher, verify, AES_BLOCK_SIZE))
  940. return -61;
  941. #ifdef CYASSL_AES_COUNTER
  942. {
  943. const byte ctrKey[] =
  944. {
  945. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  946. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  947. };
  948. const byte ctrIv[] =
  949. {
  950. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  951. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  952. };
  953. const byte ctrPlain[] =
  954. {
  955. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  956. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  957. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  958. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  959. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  960. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  961. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  962. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  963. };
  964. const byte ctrCipher[] =
  965. {
  966. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  967. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  968. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  969. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  970. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  971. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  972. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  973. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  974. };
  975. AesSetKey(&enc, ctrKey, AES_BLOCK_SIZE, ctrIv, AES_ENCRYPTION);
  976. /* Ctr only uses encrypt, even on key setup */
  977. AesSetKey(&dec, ctrKey, AES_BLOCK_SIZE, ctrIv, AES_ENCRYPTION);
  978. AesCtrEncrypt(&enc, cipher, ctrPlain, AES_BLOCK_SIZE*4);
  979. AesCtrEncrypt(&dec, plain, cipher, AES_BLOCK_SIZE*4);
  980. if (memcmp(plain, ctrPlain, AES_BLOCK_SIZE*4))
  981. return -66;
  982. if (memcmp(cipher, ctrCipher, AES_BLOCK_SIZE*4))
  983. return -67;
  984. }
  985. #endif /* CYASSL_AES_COUNTER */
  986. return 0;
  987. }
  988. #ifdef HAVE_AESGCM
  989. int aesgcm_test()
  990. {
  991. Aes enc;
  992. /*
  993. * This is Test Case 16 from the document Galois/
  994. * Counter Mode of Operation (GCM) by McGrew and
  995. * Viega.
  996. */
  997. const byte k[] =
  998. {
  999. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  1000. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  1001. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  1002. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  1003. };
  1004. const byte iv[] =
  1005. {
  1006. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  1007. 0xde, 0xca, 0xf8, 0x88, 0x00, 0x00, 0x00, 0x00
  1008. };
  1009. const byte p[] =
  1010. {
  1011. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  1012. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  1013. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  1014. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  1015. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  1016. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  1017. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  1018. 0xba, 0x63, 0x7b, 0x39
  1019. };
  1020. const byte a[] =
  1021. {
  1022. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  1023. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  1024. 0xab, 0xad, 0xda, 0xd2
  1025. };
  1026. const byte c[] =
  1027. {
  1028. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  1029. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  1030. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  1031. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  1032. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  1033. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  1034. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  1035. 0xbc, 0xc9, 0xf6, 0x62
  1036. };
  1037. const byte t[] =
  1038. {
  1039. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  1040. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  1041. };
  1042. byte t2[16];
  1043. byte p2[60];
  1044. byte c2[60];
  1045. int result;
  1046. memset(t2, 0, 16);
  1047. memset(c2, 0, 60);
  1048. memset(p2, 0, 60);
  1049. AesGcmSetKey(&enc, k, sizeof(k), iv);
  1050. AesGcmSetExpIV(&enc, iv + /*AES_GCM_IMP_IV_SZ*/ 4);
  1051. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  1052. AesGcmEncrypt(&enc, c2, p, sizeof(c2), t2, sizeof(t2), a, sizeof(a));
  1053. if (memcmp(c, c2, sizeof(c2)))
  1054. return -68;
  1055. if (memcmp(t, t2, sizeof(t2)))
  1056. return -69;
  1057. result = AesGcmDecrypt(&enc,
  1058. p2, c2, sizeof(p2), t2, sizeof(t2), a, sizeof(a));
  1059. if (result != 0)
  1060. return -70;
  1061. if (memcmp(p, p2, sizeof(p2)))
  1062. return -71;
  1063. return 0;
  1064. }
  1065. #endif /* HAVE_AESGCM */
  1066. #endif /* NO_AES */
  1067. int random_test()
  1068. {
  1069. RNG rng;
  1070. byte block[32];
  1071. int ret = InitRng(&rng);
  1072. if (ret != 0) return -39;
  1073. RNG_GenerateBlock(&rng, block, sizeof(block));
  1074. return 0;
  1075. }
  1076. static const char* clientKey = "./certs/client-key.der";
  1077. static const char* clientCert = "./certs/client-cert.der";
  1078. #ifdef CYASSL_CERT_GEN
  1079. static const char* caKeyFile = "./certs/ca-key.der";
  1080. static const char* caCertFile = "./certs/ca-cert.pem";
  1081. #endif
  1082. #ifdef HAVE_NTRU
  1083. static byte GetEntropy(ENTROPY_CMD cmd, byte* out)
  1084. {
  1085. static RNG rng;
  1086. if (cmd == INIT) {
  1087. int ret = InitRng(&rng);
  1088. if (ret == 0)
  1089. return 1;
  1090. else
  1091. return 0;
  1092. }
  1093. if (out == NULL)
  1094. return 0;
  1095. if (cmd == GET_BYTE_OF_ENTROPY) {
  1096. RNG_GenerateBlock(&rng, out, 1);
  1097. return 1;
  1098. }
  1099. if (cmd == GET_NUM_BYTES_PER_BYTE_OF_ENTROPY) {
  1100. *out = 1;
  1101. return 1;
  1102. }
  1103. return 0;
  1104. }
  1105. #endif /* HAVE_NTRU */
  1106. int rsa_test()
  1107. {
  1108. byte tmp[2048], tmp2[2048];
  1109. size_t bytes, bytes2;
  1110. RsaKey key;
  1111. RNG rng;
  1112. word32 idx = 0;
  1113. int ret;
  1114. byte in[] = "Everyone gets Friday off.";
  1115. word32 inLen = (word32)strlen((char*)in);
  1116. byte out[256];
  1117. byte plain[256];
  1118. #ifdef CYASSL_TEST_CERT
  1119. DecodedCert cert;
  1120. #endif
  1121. FILE* file = fopen(clientKey, "rb"), * file2;
  1122. if (!file)
  1123. err_sys("can't open ./certs/client-key.der, "
  1124. "Please run from CyaSSL home dir", -40);
  1125. bytes = fread(tmp, 1, sizeof(tmp), file);
  1126. InitRsaKey(&key, 0);
  1127. ret = RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  1128. if (ret != 0) return -41;
  1129. ret = InitRng(&rng);
  1130. if (ret != 0) return -42;
  1131. ret = RsaPublicEncrypt(in, inLen, out, sizeof(out), &key, &rng);
  1132. if (ret < 0) return -43;
  1133. ret = RsaPrivateDecrypt(out, ret, plain, sizeof(plain), &key);
  1134. if (ret < 0) return -44;
  1135. if (memcmp(plain, in, inLen)) return -45;
  1136. ret = RsaSSL_Sign(in, inLen, out, sizeof(out), &key, &rng);
  1137. if (ret < 0) return -46;
  1138. memset(plain, 0, sizeof(plain));
  1139. ret = RsaSSL_Verify(out, ret, plain, sizeof(plain), &key);
  1140. if (ret < 0) return -47;
  1141. if (memcmp(plain, in, ret)) return -48;
  1142. file2 = fopen(clientCert, "rb");
  1143. if (!file2)
  1144. return -49;
  1145. bytes2 = fread(tmp2, 1, sizeof(tmp2), file2);
  1146. #ifdef CYASSL_TEST_CERT
  1147. InitDecodedCert(&cert, (byte*)&tmp2, (word32)bytes2, 0);
  1148. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  1149. if (ret != 0) return -491;
  1150. FreeDecodedCert(&cert);
  1151. #endif
  1152. fclose(file2);
  1153. fclose(file);
  1154. #ifdef CYASSL_KEY_GEN
  1155. {
  1156. byte der[4096];
  1157. byte pem[4096];
  1158. word32 derSz = 0;
  1159. word32 pemSz = 0;
  1160. RsaKey derIn;
  1161. RsaKey genKey;
  1162. FILE* keyFile;
  1163. FILE* pemFile;
  1164. InitRsaKey(&genKey, 0);
  1165. ret = MakeRsaKey(&genKey, 1024, 65537, &rng);
  1166. if (ret != 0)
  1167. return -301;
  1168. derSz = RsaKeyToDer(&genKey, der, sizeof(der));
  1169. if (derSz < 0)
  1170. return -302;
  1171. keyFile = fopen("./key.der", "wb");
  1172. if (!keyFile)
  1173. return -303;
  1174. ret = fwrite(der, derSz, 1, keyFile);
  1175. fclose(keyFile);
  1176. pemSz = DerToPem(der, derSz, pem, sizeof(pem), PRIVATEKEY_TYPE);
  1177. if (pemSz < 0)
  1178. return -304;
  1179. pemFile = fopen("./key.pem", "wb");
  1180. if (!pemFile)
  1181. return -305;
  1182. ret = fwrite(pem, pemSz, 1, pemFile);
  1183. fclose(pemFile);
  1184. InitRsaKey(&derIn, 0);
  1185. idx = 0;
  1186. ret = RsaPrivateKeyDecode(der, &idx, &derIn, derSz);
  1187. if (ret != 0)
  1188. return -306;
  1189. FreeRsaKey(&derIn);
  1190. FreeRsaKey(&genKey);
  1191. }
  1192. #endif /* CYASSL_KEY_GEN */
  1193. #ifdef CYASSL_CERT_GEN
  1194. /* self signed */
  1195. {
  1196. Cert myCert;
  1197. byte derCert[4096];
  1198. byte pem[4096];
  1199. FILE* derFile;
  1200. FILE* pemFile;
  1201. int certSz;
  1202. int pemSz;
  1203. #ifdef CYASSL_TEST_CERT
  1204. DecodedCert decode;
  1205. #endif
  1206. InitCert(&myCert);
  1207. strncpy(myCert.subject.country, "US", CTC_NAME_SIZE);
  1208. strncpy(myCert.subject.state, "OR", CTC_NAME_SIZE);
  1209. strncpy(myCert.subject.locality, "Portland", CTC_NAME_SIZE);
  1210. strncpy(myCert.subject.org, "yaSSL", CTC_NAME_SIZE);
  1211. strncpy(myCert.subject.unit, "Development", CTC_NAME_SIZE);
  1212. strncpy(myCert.subject.commonName, "www.yassl.com", CTC_NAME_SIZE);
  1213. strncpy(myCert.subject.email, "info@yassl.com", CTC_NAME_SIZE);
  1214. myCert.isCA = 1;
  1215. myCert.sigType = CTC_SHA256wRSA;
  1216. certSz = MakeSelfCert(&myCert, derCert, sizeof(derCert), &key, &rng);
  1217. if (certSz < 0)
  1218. return -401;
  1219. #ifdef CYASSL_TEST_CERT
  1220. InitDecodedCert(&decode, derCert, certSz, 0);
  1221. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  1222. if (ret != 0)
  1223. return -402;
  1224. FreeDecodedCert(&decode);
  1225. #endif
  1226. derFile = fopen("./cert.der", "wb");
  1227. if (!derFile)
  1228. return -403;
  1229. ret = fwrite(derCert, certSz, 1, derFile);
  1230. fclose(derFile);
  1231. pemSz = DerToPem(derCert, certSz, pem, sizeof(pem), CERT_TYPE);
  1232. if (pemSz < 0)
  1233. return -404;
  1234. pemFile = fopen("./cert.pem", "wb");
  1235. if (!pemFile)
  1236. return -405;
  1237. ret = fwrite(pem, pemSz, 1, pemFile);
  1238. fclose(pemFile);
  1239. }
  1240. /* CA style */
  1241. {
  1242. RsaKey caKey;
  1243. Cert myCert;
  1244. byte derCert[4096];
  1245. byte pem[4096];
  1246. FILE* derFile;
  1247. FILE* pemFile;
  1248. int certSz;
  1249. int pemSz;
  1250. byte tmp[2048];
  1251. size_t bytes;
  1252. word32 idx = 0;
  1253. #ifdef CYASSL_TEST_CERT
  1254. DecodedCert decode;
  1255. #endif
  1256. FILE* file = fopen(caKeyFile, "rb");
  1257. if (!file)
  1258. return -412;
  1259. bytes = fread(tmp, 1, sizeof(tmp), file);
  1260. InitRsaKey(&caKey, 0);
  1261. ret = RsaPrivateKeyDecode(tmp, &idx, &caKey, (word32)bytes);
  1262. if (ret != 0) return -413;
  1263. InitCert(&myCert);
  1264. strncpy(myCert.subject.country, "US", CTC_NAME_SIZE);
  1265. strncpy(myCert.subject.state, "OR", CTC_NAME_SIZE);
  1266. strncpy(myCert.subject.locality, "Portland", CTC_NAME_SIZE);
  1267. strncpy(myCert.subject.org, "yaSSL", CTC_NAME_SIZE);
  1268. strncpy(myCert.subject.unit, "Development", CTC_NAME_SIZE);
  1269. strncpy(myCert.subject.commonName, "www.yassl.com", CTC_NAME_SIZE);
  1270. strncpy(myCert.subject.email, "info@yassl.com", CTC_NAME_SIZE);
  1271. ret = SetIssuer(&myCert, caCertFile);
  1272. if (ret < 0)
  1273. return -405;
  1274. certSz = MakeCert(&myCert, derCert, sizeof(derCert), &key, &rng);
  1275. if (certSz < 0)
  1276. return -407;
  1277. certSz = SignCert(&myCert, derCert, sizeof(derCert), &caKey, &rng);
  1278. if (certSz < 0)
  1279. return -408;
  1280. #ifdef CYASSL_TEST_CERT
  1281. InitDecodedCert(&decode, derCert, certSz, 0);
  1282. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  1283. if (ret != 0)
  1284. return -409;
  1285. FreeDecodedCert(&decode);
  1286. #endif
  1287. derFile = fopen("./othercert.der", "wb");
  1288. if (!derFile)
  1289. return -410;
  1290. ret = fwrite(derCert, certSz, 1, derFile);
  1291. fclose(derFile);
  1292. pemSz = DerToPem(derCert, certSz, pem, sizeof(pem), CERT_TYPE);
  1293. if (pemSz < 0)
  1294. return -411;
  1295. pemFile = fopen("./othercert.pem", "wb");
  1296. if (!pemFile)
  1297. return -412;
  1298. ret = fwrite(pem, pemSz, 1, pemFile);
  1299. fclose(pemFile);
  1300. }
  1301. #ifdef HAVE_NTRU
  1302. {
  1303. RsaKey caKey;
  1304. Cert myCert;
  1305. byte derCert[4096];
  1306. byte pem[4096];
  1307. FILE* derFile;
  1308. FILE* pemFile;
  1309. FILE* caFile;
  1310. FILE* ntruPrivFile;
  1311. int certSz;
  1312. int pemSz;
  1313. byte tmp[2048];
  1314. size_t bytes;
  1315. word32 idx = 0;
  1316. #ifdef CYASSL_TEST_CERT
  1317. DecodedCert decode;
  1318. #endif
  1319. byte public_key[557]; /* sized for EES401EP2 */
  1320. word16 public_key_len; /* no. of octets in public key */
  1321. byte private_key[607]; /* sized for EES401EP2 */
  1322. word16 private_key_len; /* no. of octets in private key */
  1323. DRBG_HANDLE drbg;
  1324. static uint8_t const pers_str[] = {
  1325. 'C', 'y', 'a', 'S', 'S', 'L', ' ', 't', 'e', 's', 't'
  1326. };
  1327. word32 rc = crypto_drbg_instantiate(112, pers_str, sizeof(pers_str),
  1328. GetEntropy, &drbg);
  1329. if (rc != DRBG_OK)
  1330. return -450;
  1331. rc = crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2, &public_key_len,
  1332. NULL, &private_key_len, NULL);
  1333. if (rc != NTRU_OK)
  1334. return -451;
  1335. rc = crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2, &public_key_len,
  1336. public_key, &private_key_len, private_key);
  1337. crypto_drbg_uninstantiate(drbg);
  1338. if (rc != NTRU_OK)
  1339. return -452;
  1340. caFile = fopen(caKeyFile, "rb");
  1341. if (!caFile)
  1342. return -453;
  1343. bytes = fread(tmp, 1, sizeof(tmp), caFile);
  1344. fclose(caFile);
  1345. InitRsaKey(&caKey, 0);
  1346. ret = RsaPrivateKeyDecode(tmp, &idx, &caKey, (word32)bytes);
  1347. if (ret != 0) return -454;
  1348. InitCert(&myCert);
  1349. strncpy(myCert.subject.country, "US", CTC_NAME_SIZE);
  1350. strncpy(myCert.subject.state, "OR", CTC_NAME_SIZE);
  1351. strncpy(myCert.subject.locality, "Portland", CTC_NAME_SIZE);
  1352. strncpy(myCert.subject.org, "yaSSL", CTC_NAME_SIZE);
  1353. strncpy(myCert.subject.unit, "Development", CTC_NAME_SIZE);
  1354. strncpy(myCert.subject.commonName, "www.yassl.com", CTC_NAME_SIZE);
  1355. strncpy(myCert.subject.email, "info@yassl.com", CTC_NAME_SIZE);
  1356. ret = SetIssuer(&myCert, caCertFile);
  1357. if (ret < 0)
  1358. return -455;
  1359. certSz = MakeNtruCert(&myCert, derCert, sizeof(derCert), public_key,
  1360. public_key_len, &rng);
  1361. if (certSz < 0)
  1362. return -456;
  1363. certSz = SignCert(&myCert, derCert, sizeof(derCert), &caKey, &rng);
  1364. if (certSz < 0)
  1365. return -457;
  1366. #ifdef CYASSL_TEST_CERT
  1367. InitDecodedCert(&decode, derCert, certSz, 0);
  1368. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  1369. if (ret != 0)
  1370. return -458;
  1371. FreeDecodedCert(&decode);
  1372. #endif
  1373. derFile = fopen("./ntru-cert.der", "wb");
  1374. if (!derFile)
  1375. return -459;
  1376. ret = fwrite(derCert, certSz, 1, derFile);
  1377. fclose(derFile);
  1378. pemSz = DerToPem(derCert, certSz, pem, sizeof(pem), CERT_TYPE);
  1379. if (pemSz < 0)
  1380. return -460;
  1381. pemFile = fopen("./ntru-cert.pem", "wb");
  1382. if (!pemFile)
  1383. return -461;
  1384. ret = fwrite(pem, pemSz, 1, pemFile);
  1385. fclose(pemFile);
  1386. ntruPrivFile = fopen("./ntru-key.raw", "wb");
  1387. if (!ntruPrivFile)
  1388. return -462;
  1389. ret = fwrite(private_key, private_key_len, 1, ntruPrivFile);
  1390. fclose(ntruPrivFile);
  1391. }
  1392. #endif /* HAVE_NTRU */
  1393. #endif /* CYASSL_CERT_GEN */
  1394. FreeRsaKey(&key);
  1395. return 0;
  1396. }
  1397. static const char* dhKey = "./certs/dh2048.der";
  1398. #ifndef NO_DH
  1399. int dh_test()
  1400. {
  1401. int ret;
  1402. word32 bytes;
  1403. word32 idx = 0, privSz, pubSz, privSz2, pubSz2, agreeSz, agreeSz2;
  1404. byte tmp[1024];
  1405. byte priv[256];
  1406. byte pub[256];
  1407. byte priv2[256];
  1408. byte pub2[256];
  1409. byte agree[256];
  1410. byte agree2[256];
  1411. DhKey key;
  1412. DhKey key2;
  1413. RNG rng;
  1414. FILE* file = fopen(dhKey, "rb");
  1415. if (!file)
  1416. return -50;
  1417. bytes = (word32) fread(tmp, 1, sizeof(tmp), file);
  1418. InitDhKey(&key);
  1419. InitDhKey(&key2);
  1420. ret = DhKeyDecode(tmp, &idx, &key, bytes);
  1421. if (ret != 0)
  1422. return -51;
  1423. idx = 0;
  1424. ret = DhKeyDecode(tmp, &idx, &key2, bytes);
  1425. if (ret != 0)
  1426. return -52;
  1427. ret = InitRng(&rng);
  1428. if (ret != 0)
  1429. return -53;
  1430. ret = DhGenerateKeyPair(&key, &rng, priv, &privSz, pub, &pubSz);
  1431. ret = DhGenerateKeyPair(&key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  1432. if (ret != 0)
  1433. return -54;
  1434. ret = DhAgree(&key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  1435. ret = DhAgree(&key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  1436. if (ret != 0)
  1437. return -55;
  1438. if (memcmp(agree, agree2, agreeSz))
  1439. return -56;
  1440. FreeDhKey(&key);
  1441. FreeDhKey(&key2);
  1442. fclose(file);
  1443. return 0;
  1444. }
  1445. #endif /* NO_DH */
  1446. static const char* dsaKey = "./certs/dsa2048.der";
  1447. #ifndef NO_DSA
  1448. int dsa_test()
  1449. {
  1450. int ret, answer;
  1451. word32 bytes;
  1452. word32 idx = 0;
  1453. byte tmp[1024];
  1454. DsaKey key;
  1455. RNG rng;
  1456. FILE* file = fopen(dsaKey, "rb");
  1457. Sha sha;
  1458. byte hash[SHA_DIGEST_SIZE];
  1459. byte signature[40];
  1460. if (!file)
  1461. return -60;
  1462. bytes = (word32) fread(tmp, 1, sizeof(tmp), file);
  1463. InitSha(&sha);
  1464. ShaUpdate(&sha, tmp, bytes);
  1465. ShaFinal(&sha, hash);
  1466. InitDsaKey(&key);
  1467. ret = DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  1468. if (ret != 0) return -61;
  1469. ret = InitRng(&rng);
  1470. if (ret != 0) return -62;
  1471. ret = DsaSign(hash, signature, &key, &rng);
  1472. if (ret != 0) return -63;
  1473. r

Large files files are truncated, but you can click here to view the full file