test.c 48 KB

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