test.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948
  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. #ifdef CYASSL_TEST_CERT
  25. #include <cyassl/ctaocrypt/asn.h>
  26. #else
  27. #include <cyassl/ctaocrypt/asn_public.h>
  28. #endif
  29. #include <cyassl/ctaocrypt/md2.h>
  30. #include <cyassl/ctaocrypt/md5.h>
  31. #include <cyassl/ctaocrypt/md4.h>
  32. #include <cyassl/ctaocrypt/sha.h>
  33. #include <cyassl/ctaocrypt/sha256.h>
  34. #include <cyassl/ctaocrypt/sha512.h>
  35. #include <cyassl/ctaocrypt/arc4.h>
  36. #include <cyassl/ctaocrypt/random.h>
  37. #include <cyassl/ctaocrypt/coding.h>
  38. #include <cyassl/ctaocrypt/rsa.h>
  39. #include <cyassl/ctaocrypt/des3.h>
  40. #include <cyassl/ctaocrypt/aes.h>
  41. #include <cyassl/ctaocrypt/camellia.h>
  42. #include <cyassl/ctaocrypt/hmac.h>
  43. #include <cyassl/ctaocrypt/dh.h>
  44. #include <cyassl/ctaocrypt/dsa.h>
  45. #include <cyassl/ctaocrypt/hc128.h>
  46. #include <cyassl/ctaocrypt/rabbit.h>
  47. #include <cyassl/ctaocrypt/pwdbased.h>
  48. #include <cyassl/ctaocrypt/ripemd.h>
  49. #ifdef HAVE_ECC
  50. #include <cyassl/ctaocrypt/ecc.h>
  51. #endif
  52. #ifdef _MSC_VER
  53. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  54. #pragma warning(disable: 4996)
  55. #endif
  56. #ifdef OPENSSL_EXTRA
  57. #include <cyassl/openssl/evp.h>
  58. #include <cyassl/openssl/rand.h>
  59. #include <cyassl/openssl/hmac.h>
  60. #include <cyassl/openssl/des.h>
  61. #endif
  62. #if defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)
  63. /* include test cert and key buffers for use with NO_FILESYSTEM */
  64. #include <cyassl/certs_test.h>
  65. #endif
  66. #ifdef HAVE_NTRU
  67. #include "crypto_ntru.h"
  68. #endif
  69. #include <string.h>
  70. #ifdef FREESCALE_MQX
  71. #include <mqx.h>
  72. #include <fio.h>
  73. #else
  74. #include <stdio.h>
  75. #endif
  76. #include <stdlib.h>
  77. #ifdef THREADX
  78. /* since just testing, use THREADX log printf instead */
  79. int dc_log_printf(char*, ...);
  80. #undef printf
  81. #define printf dc_log_printf
  82. #endif
  83. #include "ctaocrypt/test/test.h"
  84. typedef struct testVector {
  85. const char* input;
  86. const char* output;
  87. size_t inLen;
  88. size_t outLen;
  89. } testVector;
  90. int md2_test(void);
  91. int md5_test(void);
  92. int md4_test(void);
  93. int sha_test(void);
  94. int sha256_test(void);
  95. int sha512_test(void);
  96. int sha384_test(void);
  97. int hmac_md5_test(void);
  98. int hmac_sha_test(void);
  99. int hmac_sha256_test(void);
  100. int hmac_sha384_test(void);
  101. int arc4_test(void);
  102. int hc128_test(void);
  103. int rabbit_test(void);
  104. int des_test(void);
  105. int des3_test(void);
  106. int aes_test(void);
  107. int aesgcm_test(void);
  108. int aesccm_test(void);
  109. int camellia_test(void);
  110. int rsa_test(void);
  111. int dh_test(void);
  112. int dsa_test(void);
  113. int random_test(void);
  114. int pwdbased_test(void);
  115. int ripemd_test(void);
  116. int openssl_test(void); /* test mini api */
  117. int pbkdf1_test(void);
  118. int pkcs12_test(void);
  119. int pbkdf2_test(void);
  120. #ifdef HAVE_ECC
  121. int ecc_test(void);
  122. #endif
  123. static void err_sys(const char* msg, int es)
  124. {
  125. printf("%s error = %d\n", msg, es);
  126. #ifndef THREADX
  127. if (msg)
  128. exit(es);
  129. #endif
  130. return;
  131. }
  132. /* func_args from test.h, so don't have to pull in other junk */
  133. typedef struct func_args {
  134. int argc;
  135. char** argv;
  136. int return_code;
  137. } func_args;
  138. void ctaocrypt_test(void* args)
  139. {
  140. int ret = 0;
  141. ((func_args*)args)->return_code = -1; /* error state */
  142. #if !defined(CYASSL_LEANPSK)
  143. if (CheckCtcSettings() != 1)
  144. err_sys("Build vs runtime math mismatch\n", -1234);
  145. #ifdef USE_FAST_MATH
  146. if (CheckFastMathSettings() != 1)
  147. err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n", -1235);
  148. #endif /* USE_FAST_MATH */
  149. #endif /* !CYASSL_LEANPSK */
  150. #ifndef NO_MD5
  151. if ( (ret = md5_test()) )
  152. err_sys("MD5 test failed!\n", ret);
  153. else
  154. printf( "MD5 test passed!\n");
  155. #endif
  156. #ifdef CYASSL_MD2
  157. if ( (ret = md2_test()) )
  158. err_sys("MD2 test failed!\n", ret);
  159. else
  160. printf( "MD2 test passed!\n");
  161. #endif
  162. #ifndef NO_MD4
  163. if ( (ret = md4_test()) )
  164. err_sys("MD4 test failed!\n", ret);
  165. else
  166. printf( "MD4 test passed!\n");
  167. #endif
  168. if ( (ret = sha_test()) )
  169. err_sys("SHA test failed!\n", ret);
  170. else
  171. printf( "SHA test passed!\n");
  172. #ifndef NO_SHA256
  173. if ( (ret = sha256_test()) )
  174. err_sys("SHA-256 test failed!\n", ret);
  175. else
  176. printf( "SHA-256 test passed!\n");
  177. #endif
  178. #ifdef CYASSL_SHA384
  179. if ( (ret = sha384_test()) )
  180. err_sys("SHA-384 test failed!\n", ret);
  181. else
  182. printf( "SHA-384 test passed!\n");
  183. #endif
  184. #ifdef CYASSL_SHA512
  185. if ( (ret = sha512_test()) )
  186. err_sys("SHA-512 test failed!\n", ret);
  187. else
  188. printf( "SHA-512 test passed!\n");
  189. #endif
  190. #ifdef CYASSL_RIPEMD
  191. if ( (ret = ripemd_test()) )
  192. err_sys("RIPEMD test failed!\n", ret);
  193. else
  194. printf( "RIPEMD test passed!\n");
  195. #endif
  196. #ifndef NO_HMAC
  197. #ifndef NO_MD5
  198. if ( (ret = hmac_md5_test()) )
  199. err_sys("HMAC-MD5 test failed!\n", ret);
  200. else
  201. printf( "HMAC-MD5 test passed!\n");
  202. #endif
  203. if ( (ret = hmac_sha_test()) )
  204. err_sys("HMAC-SHA test failed!\n", ret);
  205. else
  206. printf( "HMAC-SHA test passed!\n");
  207. #ifndef NO_SHA256
  208. if ( (ret = hmac_sha256_test()) )
  209. err_sys("HMAC-SHA256 test failed!\n", ret);
  210. else
  211. printf( "HMAC-SHA256 test passed!\n");
  212. #endif
  213. #ifdef CYASSL_SHA384
  214. if ( (ret = hmac_sha384_test()) )
  215. err_sys("HMAC-SHA384 test failed!\n", ret);
  216. else
  217. printf( "HMAC-SHA384 test passed!\n");
  218. #endif
  219. #endif
  220. #ifndef NO_RC4
  221. if ( (ret = arc4_test()) )
  222. err_sys("ARC4 test failed!\n", ret);
  223. else
  224. printf( "ARC4 test passed!\n");
  225. #endif
  226. #ifndef NO_HC128
  227. if ( (ret = hc128_test()) )
  228. err_sys("HC-128 test failed!\n", ret);
  229. else
  230. printf( "HC-128 test passed!\n");
  231. #endif
  232. #ifndef NO_RABBIT
  233. if ( (ret = rabbit_test()) )
  234. err_sys("Rabbit test failed!\n", ret);
  235. else
  236. printf( "Rabbit test passed!\n");
  237. #endif
  238. #ifndef NO_DES3
  239. if ( (ret = des_test()) )
  240. err_sys("DES test failed!\n", ret);
  241. else
  242. printf( "DES test passed!\n");
  243. #endif
  244. #ifndef NO_DES3
  245. if ( (ret = des3_test()) )
  246. err_sys("DES3 test failed!\n", ret);
  247. else
  248. printf( "DES3 test passed!\n");
  249. #endif
  250. #ifndef NO_AES
  251. if ( (ret = aes_test()) )
  252. err_sys("AES test failed!\n", ret);
  253. else
  254. printf( "AES test passed!\n");
  255. #ifdef HAVE_AESGCM
  256. if ( (ret = aesgcm_test()) )
  257. err_sys("AES-GCM test failed!\n", ret);
  258. else
  259. printf( "AES-GCM test passed!\n");
  260. #endif
  261. #ifdef HAVE_AESCCM
  262. if ( (ret = aesccm_test()) )
  263. err_sys("AES-CCM test failed!\n", ret);
  264. else
  265. printf( "AES-CCM test passed!\n");
  266. #endif
  267. #endif
  268. #ifdef HAVE_CAMELLIA
  269. if ( (ret = camellia_test()) )
  270. err_sys("CAMELLIA test failed!\n", ret);
  271. else
  272. printf( "CAMELLIA test passed!\n");
  273. #endif
  274. if ( (ret = random_test()) )
  275. err_sys("RANDOM test failed!\n", ret);
  276. else
  277. printf( "RANDOM test passed!\n");
  278. #ifndef NO_RSA
  279. if ( (ret = rsa_test()) )
  280. err_sys("RSA test failed!\n", ret);
  281. else
  282. printf( "RSA test passed!\n");
  283. #endif
  284. #ifndef NO_DH
  285. if ( (ret = dh_test()) )
  286. err_sys("DH test failed!\n", ret);
  287. else
  288. printf( "DH test passed!\n");
  289. #endif
  290. #ifndef NO_DSA
  291. if ( (ret = dsa_test()) )
  292. err_sys("DSA test failed!\n", ret);
  293. else
  294. printf( "DSA test passed!\n");
  295. #endif
  296. #ifndef NO_PWDBASED
  297. if ( (ret = pwdbased_test()) )
  298. err_sys("PWDBASED test failed!\n", ret);
  299. else
  300. printf( "PWDBASED test passed!\n");
  301. #endif
  302. #ifdef OPENSSL_EXTRA
  303. if ( (ret = openssl_test()) )
  304. err_sys("OPENSSL test failed!\n", ret);
  305. else
  306. printf( "OPENSSL test passed!\n");
  307. #endif
  308. #ifdef HAVE_ECC
  309. if ( (ret = ecc_test()) )
  310. err_sys("ECC test failed!\n", ret);
  311. else
  312. printf( "ECC test passed!\n");
  313. #endif
  314. ((func_args*)args)->return_code = ret;
  315. }
  316. /* so overall tests can pull in test function */
  317. #ifndef NO_MAIN_DRIVER
  318. int main(int argc, char** argv)
  319. {
  320. func_args args;
  321. args.argc = argc;
  322. args.argv = argv;
  323. ctaocrypt_test(&args);
  324. return args.return_code;
  325. }
  326. #endif /* NO_MAIN_DRIVER */
  327. #ifdef CYASSL_MD2
  328. int md2_test()
  329. {
  330. Md2 md2;
  331. byte hash[MD2_DIGEST_SIZE];
  332. testVector a, b, c, d, e, f, g;
  333. testVector test_md2[7];
  334. int times = sizeof(test_md2) / sizeof(testVector), i;
  335. a.input = "";
  336. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  337. "\x27\x73";
  338. a.inLen = strlen(a.input);
  339. a.outLen = strlen(a.output);
  340. b.input = "a";
  341. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  342. "\xb5\xd1";
  343. b.inLen = strlen(b.input);
  344. b.outLen = strlen(b.output);
  345. c.input = "abc";
  346. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  347. "\xd6\xbb";
  348. c.inLen = strlen(c.input);
  349. c.outLen = strlen(c.output);
  350. d.input = "message digest";
  351. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  352. "\x06\xb0";
  353. d.inLen = strlen(d.input);
  354. d.outLen = strlen(d.output);
  355. e.input = "abcdefghijklmnopqrstuvwxyz";
  356. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  357. "\x94\x0b";
  358. e.inLen = strlen(e.input);
  359. e.outLen = strlen(e.output);
  360. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  361. "6789";
  362. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  363. "\x38\xcd";
  364. f.inLen = strlen(f.input);
  365. f.outLen = strlen(f.output);
  366. g.input = "1234567890123456789012345678901234567890123456789012345678"
  367. "9012345678901234567890";
  368. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  369. "\xef\xd8";
  370. g.inLen = strlen(g.input);
  371. g.outLen = strlen(g.output);
  372. test_md2[0] = a;
  373. test_md2[1] = b;
  374. test_md2[2] = c;
  375. test_md2[3] = d;
  376. test_md2[4] = e;
  377. test_md2[5] = f;
  378. test_md2[6] = g;
  379. InitMd2(&md2);
  380. for (i = 0; i < times; ++i) {
  381. Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  382. Md2Final(&md2, hash);
  383. if (memcmp(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  384. return -155 - i;
  385. }
  386. return 0;
  387. }
  388. #endif
  389. #ifndef NO_MD5
  390. int md5_test(void)
  391. {
  392. Md5 md5;
  393. byte hash[MD5_DIGEST_SIZE];
  394. testVector a, b, c, d, e;
  395. testVector test_md5[5];
  396. int times = sizeof(test_md5) / sizeof(testVector), i;
  397. a.input = "abc";
  398. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  399. "\x72";
  400. a.inLen = strlen(a.input);
  401. a.outLen = strlen(a.output);
  402. b.input = "message digest";
  403. b.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  404. "\xd0";
  405. b.inLen = strlen(b.input);
  406. b.outLen = strlen(b.output);
  407. c.input = "abcdefghijklmnopqrstuvwxyz";
  408. c.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  409. "\x3b";
  410. c.inLen = strlen(c.input);
  411. c.outLen = strlen(c.output);
  412. d.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  413. "6789";
  414. d.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  415. "\x9f";
  416. d.inLen = strlen(d.input);
  417. d.outLen = strlen(d.output);
  418. e.input = "1234567890123456789012345678901234567890123456789012345678"
  419. "9012345678901234567890";
  420. e.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  421. "\x7a";
  422. e.inLen = strlen(e.input);
  423. e.outLen = strlen(e.output);
  424. test_md5[0] = a;
  425. test_md5[1] = b;
  426. test_md5[2] = c;
  427. test_md5[3] = d;
  428. test_md5[4] = e;
  429. InitMd5(&md5);
  430. for (i = 0; i < times; ++i) {
  431. Md5Update(&md5, (byte*)test_md5[i].input, (word32)test_md5[i].inLen);
  432. Md5Final(&md5, hash);
  433. if (memcmp(hash, test_md5[i].output, MD5_DIGEST_SIZE) != 0)
  434. return -5 - i;
  435. }
  436. return 0;
  437. }
  438. #endif /* NO_MD5 */
  439. #ifndef NO_MD4
  440. int md4_test(void)
  441. {
  442. Md4 md4;
  443. byte hash[MD4_DIGEST_SIZE];
  444. testVector a, b, c, d, e, f, g;
  445. testVector test_md4[7];
  446. int times = sizeof(test_md4) / sizeof(testVector), i;
  447. a.input = "";
  448. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  449. "\xc0";
  450. a.inLen = strlen(a.input);
  451. a.outLen = strlen(a.output);
  452. b.input = "a";
  453. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  454. "\x24";
  455. b.inLen = strlen(b.input);
  456. b.outLen = strlen(b.output);
  457. c.input = "abc";
  458. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  459. "\x9d";
  460. c.inLen = strlen(c.input);
  461. c.outLen = strlen(c.output);
  462. d.input = "message digest";
  463. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  464. "\x4b";
  465. d.inLen = strlen(d.input);
  466. d.outLen = strlen(d.output);
  467. e.input = "abcdefghijklmnopqrstuvwxyz";
  468. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  469. "\xa9";
  470. e.inLen = strlen(e.input);
  471. e.outLen = strlen(e.output);
  472. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  473. "6789";
  474. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  475. "\xe4";
  476. f.inLen = strlen(f.input);
  477. f.outLen = strlen(f.output);
  478. g.input = "1234567890123456789012345678901234567890123456789012345678"
  479. "9012345678901234567890";
  480. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  481. "\x36";
  482. g.inLen = strlen(g.input);
  483. g.outLen = strlen(g.output);
  484. test_md4[0] = a;
  485. test_md4[1] = b;
  486. test_md4[2] = c;
  487. test_md4[3] = d;
  488. test_md4[4] = e;
  489. test_md4[5] = f;
  490. test_md4[6] = g;
  491. InitMd4(&md4);
  492. for (i = 0; i < times; ++i) {
  493. Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  494. Md4Final(&md4, hash);
  495. if (memcmp(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  496. return -205 - i;
  497. }
  498. return 0;
  499. }
  500. #endif /* NO_MD4 */
  501. int sha_test(void)
  502. {
  503. Sha sha;
  504. byte hash[SHA_DIGEST_SIZE];
  505. testVector a, b, c, d;
  506. testVector test_sha[4];
  507. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  508. a.input = "abc";
  509. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  510. "\x6C\x9C\xD0\xD8\x9D";
  511. a.inLen = strlen(a.input);
  512. a.outLen = strlen(a.output);
  513. b.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  514. b.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  515. "\xE5\xE5\x46\x70\xF1";
  516. b.inLen = strlen(b.input);
  517. b.outLen = strlen(b.output);
  518. c.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  519. "aaaaaa";
  520. c.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  521. "\x2A\x25\xEC\x64\x4D";
  522. c.inLen = strlen(c.input);
  523. c.outLen = strlen(c.output);
  524. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  525. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  526. "aaaaaaaaaa";
  527. d.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  528. "\x53\x99\x5E\x26\xA0";
  529. d.inLen = strlen(d.input);
  530. d.outLen = strlen(d.output);
  531. test_sha[0] = a;
  532. test_sha[1] = b;
  533. test_sha[2] = c;
  534. test_sha[3] = d;
  535. InitSha(&sha);
  536. for (i = 0; i < times; ++i) {
  537. ShaUpdate(&sha, (byte*)test_sha[i].input, (word32)test_sha[i].inLen);
  538. ShaFinal(&sha, hash);
  539. if (memcmp(hash, test_sha[i].output, SHA_DIGEST_SIZE) != 0)
  540. return -10 - i;
  541. }
  542. return 0;
  543. }
  544. #ifdef CYASSL_RIPEMD
  545. int ripemd_test(void)
  546. {
  547. RipeMd ripemd;
  548. byte hash[RIPEMD_DIGEST_SIZE];
  549. testVector a, b, c, d;
  550. testVector test_ripemd[4];
  551. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  552. a.input = "abc";
  553. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  554. "\xb0\x87\xf1\x5a\x0b\xfc";
  555. a.inLen = strlen(a.input);
  556. a.outLen = strlen(a.output);
  557. b.input = "message digest";
  558. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  559. "\x5f\xfa\x21\x59\x5f\x36";
  560. b.inLen = strlen(b.input);
  561. b.outLen = strlen(b.output);
  562. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  563. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  564. "\xf4\x9a\xda\x62\xeb\x2b";
  565. c.inLen = strlen(c.input);
  566. c.outLen = strlen(c.output);
  567. d.input = "12345678901234567890123456789012345678901234567890123456"
  568. "789012345678901234567890";
  569. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  570. "\x82\xbf\x63\x32\x6b\xfb";
  571. d.inLen = strlen(d.input);
  572. d.outLen = strlen(d.output);
  573. test_ripemd[0] = a;
  574. test_ripemd[1] = b;
  575. test_ripemd[2] = c;
  576. test_ripemd[3] = d;
  577. InitRipeMd(&ripemd);
  578. for (i = 0; i < times; ++i) {
  579. RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  580. (word32)test_ripemd[i].inLen);
  581. RipeMdFinal(&ripemd, hash);
  582. if (memcmp(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  583. return -10 - i;
  584. }
  585. return 0;
  586. }
  587. #endif /* CYASSL_RIPEMD */
  588. #ifndef NO_SHA256
  589. int sha256_test(void)
  590. {
  591. Sha256 sha;
  592. byte hash[SHA256_DIGEST_SIZE];
  593. testVector a, b;
  594. testVector test_sha[2];
  595. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  596. a.input = "abc";
  597. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  598. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  599. "\x15\xAD";
  600. a.inLen = strlen(a.input);
  601. a.outLen = strlen(a.output);
  602. b.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  603. b.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  604. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  605. "\x06\xC1";
  606. b.inLen = strlen(b.input);
  607. b.outLen = strlen(b.output);
  608. test_sha[0] = a;
  609. test_sha[1] = b;
  610. InitSha256(&sha);
  611. for (i = 0; i < times; ++i) {
  612. Sha256Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
  613. Sha256Final(&sha, hash);
  614. if (memcmp(hash, test_sha[i].output, SHA256_DIGEST_SIZE) != 0)
  615. return -10 - i;
  616. }
  617. return 0;
  618. }
  619. #endif
  620. #ifdef CYASSL_SHA512
  621. int sha512_test(void)
  622. {
  623. Sha512 sha;
  624. byte hash[SHA512_DIGEST_SIZE];
  625. testVector a, b;
  626. testVector test_sha[2];
  627. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  628. a.input = "abc";
  629. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  630. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  631. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  632. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  633. "\xa5\x4c\xa4\x9f";
  634. a.inLen = strlen(a.input);
  635. a.outLen = strlen(a.output);
  636. b.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  637. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  638. b.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  639. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  640. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  641. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  642. "\x87\x4b\xe9\x09";
  643. b.inLen = strlen(b.input);
  644. b.outLen = strlen(b.output);
  645. test_sha[0] = a;
  646. test_sha[1] = b;
  647. InitSha512(&sha);
  648. for (i = 0; i < times; ++i) {
  649. Sha512Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
  650. Sha512Final(&sha, hash);
  651. if (memcmp(hash, test_sha[i].output, SHA512_DIGEST_SIZE) != 0)
  652. return -10 - i;
  653. }
  654. return 0;
  655. }
  656. #endif
  657. #ifdef CYASSL_SHA384
  658. int sha384_test(void)
  659. {
  660. Sha384 sha;
  661. byte hash[SHA384_DIGEST_SIZE];
  662. testVector a, b;
  663. testVector test_sha[2];
  664. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  665. a.input = "abc";
  666. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  667. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  668. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  669. "\xc8\x25\xa7";
  670. a.inLen = strlen(a.input);
  671. a.outLen = strlen(a.output);
  672. b.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  673. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  674. b.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  675. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  676. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  677. "\x74\x60\x39";
  678. b.inLen = strlen(b.input);
  679. b.outLen = strlen(b.output);
  680. test_sha[0] = a;
  681. test_sha[1] = b;
  682. InitSha384(&sha);
  683. for (i = 0; i < times; ++i) {
  684. Sha384Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
  685. Sha384Final(&sha, hash);
  686. if (memcmp(hash, test_sha[i].output, SHA384_DIGEST_SIZE) != 0)
  687. return -10 - i;
  688. }
  689. return 0;
  690. }
  691. #endif /* CYASSL_SHA384 */
  692. #if !defined(NO_HMAC) && !defined(NO_MD5)
  693. int hmac_md5_test(void)
  694. {
  695. Hmac hmac;
  696. byte hash[MD5_DIGEST_SIZE];
  697. const char* keys[]=
  698. {
  699. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  700. "Jefe",
  701. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  702. };
  703. testVector a, b, c;
  704. testVector test_hmac[3];
  705. int times = sizeof(test_hmac) / sizeof(testVector), i;
  706. a.input = "Hi There";
  707. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  708. "\x9d";
  709. a.inLen = strlen(a.input);
  710. a.outLen = strlen(a.output);
  711. b.input = "what do ya want for nothing?";
  712. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  713. "\x38";
  714. b.inLen = strlen(b.input);
  715. b.outLen = strlen(b.output);
  716. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  717. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  718. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  719. "\xDD\xDD\xDD\xDD\xDD\xDD";
  720. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  721. "\xf6";
  722. c.inLen = strlen(c.input);
  723. c.outLen = strlen(c.output);
  724. test_hmac[0] = a;
  725. test_hmac[1] = b;
  726. test_hmac[2] = c;
  727. for (i = 0; i < times; ++i) {
  728. HmacSetKey(&hmac, MD5, (byte*)keys[i], (word32)strlen(keys[i]));
  729. HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  730. (word32)test_hmac[i].inLen);
  731. HmacFinal(&hmac, hash);
  732. if (memcmp(hash, test_hmac[i].output, MD5_DIGEST_SIZE) != 0)
  733. return -20 - i;
  734. }
  735. return 0;
  736. }
  737. #endif /* NO_HMAC && NO_MD5 */
  738. #ifndef NO_HMAC
  739. int hmac_sha_test(void)
  740. {
  741. Hmac hmac;
  742. byte hash[SHA_DIGEST_SIZE];
  743. const char* keys[]=
  744. {
  745. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  746. "\x0b\x0b\x0b",
  747. "Jefe",
  748. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  749. "\xAA\xAA\xAA"
  750. };
  751. testVector a, b, c;
  752. testVector test_hmac[3];
  753. int times = sizeof(test_hmac) / sizeof(testVector), i;
  754. a.input = "Hi There";
  755. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  756. "\x8e\xf1\x46\xbe\x00";
  757. a.inLen = strlen(a.input);
  758. a.outLen = strlen(a.output);
  759. b.input = "what do ya want for nothing?";
  760. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  761. "\x9c\x25\x9a\x7c\x79";
  762. b.inLen = strlen(b.input);
  763. b.outLen = strlen(b.output);
  764. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  765. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  766. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  767. "\xDD\xDD\xDD\xDD\xDD\xDD";
  768. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  769. "\x4f\x63\xf1\x75\xd3";
  770. c.inLen = strlen(c.input);
  771. c.outLen = strlen(c.output);
  772. test_hmac[0] = a;
  773. test_hmac[1] = b;
  774. test_hmac[2] = c;
  775. for (i = 0; i < times; ++i) {
  776. HmacSetKey(&hmac, SHA, (byte*)keys[i], (word32)strlen(keys[i]));
  777. HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  778. (word32)test_hmac[i].inLen);
  779. HmacFinal(&hmac, hash);
  780. if (memcmp(hash, test_hmac[i].output, SHA_DIGEST_SIZE) != 0)
  781. return -20 - i;
  782. }
  783. return 0;
  784. }
  785. #endif
  786. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  787. int hmac_sha256_test(void)
  788. {
  789. Hmac hmac;
  790. byte hash[SHA256_DIGEST_SIZE];
  791. const char* keys[]=
  792. {
  793. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  794. "\x0b\x0b\x0b",
  795. "Jefe",
  796. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  797. "\xAA\xAA\xAA"
  798. };
  799. testVector a, b, c;
  800. testVector test_hmac[3];
  801. int times = sizeof(test_hmac) / sizeof(testVector), i;
  802. a.input = "Hi There";
  803. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  804. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  805. "\xcf\xf7";
  806. a.inLen = strlen(a.input);
  807. a.outLen = strlen(a.output);
  808. b.input = "what do ya want for nothing?";
  809. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  810. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  811. "\x38\x43";
  812. b.inLen = strlen(b.input);
  813. b.outLen = strlen(b.output);
  814. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  815. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  816. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  817. "\xDD\xDD\xDD\xDD\xDD\xDD";
  818. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  819. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  820. "\x65\xfe";
  821. c.inLen = strlen(c.input);
  822. c.outLen = strlen(c.output);
  823. test_hmac[0] = a;
  824. test_hmac[1] = b;
  825. test_hmac[2] = c;
  826. for (i = 0; i < times; ++i) {
  827. HmacSetKey(&hmac, SHA256, (byte*)keys[i], (word32)strlen(keys[i]));
  828. HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  829. (word32)test_hmac[i].inLen);
  830. HmacFinal(&hmac, hash);
  831. if (memcmp(hash, test_hmac[i].output, SHA256_DIGEST_SIZE) != 0)
  832. return -20 - i;
  833. }
  834. return 0;
  835. }
  836. #endif
  837. #if !defined(NO_HMAC) && defined(CYASSL_SHA384)
  838. int hmac_sha384_test(void)
  839. {
  840. Hmac hmac;
  841. byte hash[SHA384_DIGEST_SIZE];
  842. const char* keys[]=
  843. {
  844. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  845. "\x0b\x0b\x0b",
  846. "Jefe",
  847. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  848. "\xAA\xAA\xAA"
  849. };
  850. testVector a, b, c;
  851. testVector test_hmac[3];
  852. int times = sizeof(test_hmac) / sizeof(testVector), i;
  853. a.input = "Hi There";
  854. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  855. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  856. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  857. "\xfa\x9c\xb6";
  858. a.inLen = strlen(a.input);
  859. a.outLen = strlen(a.output);
  860. b.input = "what do ya want for nothing?";
  861. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  862. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  863. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  864. "\xb2\x16\x49";
  865. b.inLen = strlen(b.input);
  866. b.outLen = strlen(b.output);
  867. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  868. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  869. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  870. "\xDD\xDD\xDD\xDD\xDD\xDD";
  871. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  872. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  873. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  874. "\xa3\x4f\x27";
  875. c.inLen = strlen(c.input);
  876. c.outLen = strlen(c.output);
  877. test_hmac[0] = a;
  878. test_hmac[1] = b;
  879. test_hmac[2] = c;
  880. for (i = 0; i < times; ++i) {
  881. HmacSetKey(&hmac, SHA384, (byte*)keys[i], (word32)strlen(keys[i]));
  882. HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  883. (word32)test_hmac[i].inLen);
  884. HmacFinal(&hmac, hash);
  885. if (memcmp(hash, test_hmac[i].output, SHA384_DIGEST_SIZE) != 0)
  886. return -20 - i;
  887. }
  888. return 0;
  889. }
  890. #endif
  891. #ifndef NO_RC4
  892. int arc4_test(void)
  893. {
  894. byte cipher[16];
  895. byte plain[16];
  896. const char* keys[] =
  897. {
  898. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  899. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  900. "\x00\x00\x00\x00\x00\x00\x00\x00",
  901. "\xef\x01\x23\x45"
  902. };
  903. testVector a, b, c, d;
  904. testVector test_arc4[4];
  905. int times = sizeof(test_arc4) / sizeof(testVector), i;
  906. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  907. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  908. a.inLen = strlen(a.input);
  909. a.outLen = strlen(a.output);
  910. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  911. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  912. b.inLen = strlen(b.input);
  913. b.outLen = strlen(b.output);
  914. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  915. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  916. c.inLen = strlen(c.input);
  917. c.outLen = strlen(c.output);
  918. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  919. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  920. d.inLen = strlen(d.input);
  921. d.outLen = strlen(d.output);
  922. test_arc4[0] = a;
  923. test_arc4[1] = b;
  924. test_arc4[2] = c;
  925. test_arc4[3] = d;
  926. for (i = 0; i < times; ++i) {
  927. Arc4 enc;
  928. Arc4 dec;
  929. Arc4SetKey(&enc, (byte*)keys[i], (word32)strlen(keys[i]));
  930. Arc4SetKey(&dec, (byte*)keys[i], (word32)strlen(keys[i]));
  931. Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  932. (word32)test_arc4[i].outLen);
  933. Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  934. if (memcmp(plain, test_arc4[i].input, test_arc4[i].outLen))
  935. return -20 - i;
  936. if (memcmp(cipher, test_arc4[i].output, test_arc4[i].outLen))
  937. return -20 - 5 - i;
  938. }
  939. return 0;
  940. }
  941. #endif
  942. int hc128_test(void)
  943. {
  944. #ifdef HAVE_HC128
  945. byte cipher[16];
  946. byte plain[16];
  947. const char* keys[] =
  948. {
  949. "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  950. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  951. "\x00\x53\xA6\xF9\x4C\x9F\xF2\x45\x98\xEB\x3E\x91\xE4\x37\x8A\xDD",
  952. "\x0F\x62\xB5\x08\x5B\xAE\x01\x54\xA7\xFA\x4D\xA0\xF3\x46\x99\xEC"
  953. };
  954. const char* ivs[] =
  955. {
  956. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  957. "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  958. "\x0D\x74\xDB\x42\xA9\x10\x77\xDE\x45\xAC\x13\x7A\xE1\x48\xAF\x16",
  959. "\x28\x8F\xF6\x5D\xC4\x2B\x92\xF9\x60\xC7\x2E\x95\xFC\x63\xCA\x31"
  960. };
  961. testVector a, b, c, d;
  962. testVector test_hc128[4];
  963. int times = sizeof(test_hc128) / sizeof(testVector), i;
  964. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  965. a.output = "\x37\x86\x02\xB9\x8F\x32\xA7\x48";
  966. a.inLen = strlen(a.input);
  967. a.outLen = strlen(a.output);
  968. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  969. b.output = "\x33\x7F\x86\x11\xC6\xED\x61\x5F";
  970. b.inLen = strlen(b.input);
  971. b.outLen = strlen(b.output);
  972. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  973. c.output = "\x2E\x1E\xD1\x2A\x85\x51\xC0\x5A";
  974. c.inLen = strlen(c.input);
  975. c.outLen = strlen(c.output);
  976. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  977. d.output = "\x1C\xD8\xAE\xDD\xFE\x52\xE2\x17\xE8\x35\xD0\xB7\xE8\x4E\x29";
  978. d.inLen = strlen(d.input);
  979. d.outLen = strlen(d.output);
  980. test_hc128[0] = a;
  981. test_hc128[1] = b;
  982. test_hc128[2] = c;
  983. test_hc128[3] = d;
  984. for (i = 0; i < times; ++i) {
  985. HC128 enc;
  986. HC128 dec;
  987. /* align keys/ivs in plain/cipher buffers */
  988. memcpy(plain, keys[i], 16);
  989. memcpy(cipher, ivs[i], 16);
  990. Hc128_SetKey(&enc, plain, cipher);
  991. Hc128_SetKey(&dec, plain, cipher);
  992. /* align input */
  993. memcpy(plain, test_hc128[i].input, test_hc128[i].outLen);
  994. Hc128_Process(&enc, cipher, plain, (word32)test_hc128[i].outLen);
  995. Hc128_Process(&dec, plain, cipher, (word32)test_hc128[i].outLen);
  996. if (memcmp(plain, test_hc128[i].input, test_hc128[i].outLen))
  997. return -120 - i;
  998. if (memcmp(cipher, test_hc128[i].output, test_hc128[i].outLen))
  999. return -120 - 5 - i;
  1000. }
  1001. #endif /* HAVE_HC128 */
  1002. return 0;
  1003. }
  1004. #ifndef NO_RABBIT
  1005. int rabbit_test(void)
  1006. {
  1007. byte cipher[16];
  1008. byte plain[16];
  1009. const char* keys[] =
  1010. {
  1011. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  1012. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  1013. "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B\xFE\x36\x3D\x2E\x29\x13\x28\x91"
  1014. };
  1015. const char* ivs[] =
  1016. {
  1017. "\x00\x00\x00\x00\x00\x00\x00\x00",
  1018. "\x59\x7E\x26\xC1\x75\xF5\x73\xC3",
  1019. 0
  1020. };
  1021. testVector a, b, c;
  1022. testVector test_rabbit[3];
  1023. int times = sizeof(test_rabbit) / sizeof(testVector), i;
  1024. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  1025. a.output = "\xED\xB7\x05\x67\x37\x5D\xCD\x7C";
  1026. a.inLen = strlen(a.input);
  1027. a.outLen = strlen(a.output);
  1028. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  1029. b.output = "\x6D\x7D\x01\x22\x92\xCC\xDC\xE0";
  1030. b.inLen = strlen(b.input);
  1031. b.outLen = strlen(b.output);
  1032. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  1033. c.output = "\x9C\x51\xE2\x87\x84\xC3\x7F\xE9";
  1034. c.inLen = strlen(c.input);
  1035. c.outLen = strlen(c.output);
  1036. test_rabbit[0] = a;
  1037. test_rabbit[1] = b;
  1038. test_rabbit[2] = c;
  1039. for (i = 0; i < times; ++i) {
  1040. Rabbit enc;
  1041. Rabbit dec;
  1042. byte* iv;
  1043. /* align keys/ivs in plain/cipher buffers */
  1044. memcpy(plain, keys[i], 16);
  1045. if (ivs[i]) {
  1046. memcpy(cipher, ivs[i], 8);
  1047. iv = cipher;
  1048. } else
  1049. iv = NULL;
  1050. RabbitSetKey(&enc, plain, iv);
  1051. RabbitSetKey(&dec, plain, iv);
  1052. /* align input */
  1053. memcpy(plain, test_rabbit[i].input, test_rabbit[i].outLen);
  1054. RabbitProcess(&enc, cipher, plain, (word32)test_rabbit[i].outLen);
  1055. RabbitProcess(&dec, plain, cipher, (word32)test_rabbit[i].outLen);
  1056. if (memcmp(plain, test_rabbit[i].input, test_rabbit[i].outLen))
  1057. return -130 - i;
  1058. if (memcmp(cipher, test_rabbit[i].output, test_rabbit[i].outLen))
  1059. return -130 - 5 - i;
  1060. }
  1061. return 0;
  1062. }
  1063. #endif /* NO_RABBIT */
  1064. #ifndef NO_DES3
  1065. int des_test(void)
  1066. {
  1067. const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  1068. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  1069. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  1070. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  1071. };
  1072. byte plain[24];
  1073. byte cipher[24];
  1074. Des enc;
  1075. Des dec;
  1076. const byte key[] =
  1077. {
  1078. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  1079. };
  1080. const byte iv[] =
  1081. {
  1082. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  1083. };
  1084. const byte verify[] =
  1085. {
  1086. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  1087. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  1088. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  1089. };
  1090. Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  1091. Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  1092. Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  1093. Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  1094. if (memcmp(plain, vector, sizeof(plain)))
  1095. return -31;
  1096. if (memcmp(cipher, verify, sizeof(cipher)))
  1097. return -32;
  1098. return 0;
  1099. }
  1100. #endif /* NO_DES3 */
  1101. #ifndef NO_DES3
  1102. int des3_test(void)
  1103. {
  1104. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  1105. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  1106. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  1107. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  1108. };
  1109. byte plain[24];
  1110. byte cipher[24];
  1111. Des3 enc;
  1112. Des3 dec;
  1113. const byte key3[] =
  1114. {
  1115. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  1116. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  1117. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  1118. };
  1119. const byte iv3[] =
  1120. {
  1121. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  1122. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1123. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  1124. };
  1125. const byte verify3[] =
  1126. {
  1127. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  1128. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  1129. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  1130. };
  1131. Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  1132. Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  1133. Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  1134. Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  1135. if (memcmp(plain, vector, sizeof(plain)))
  1136. return -33;
  1137. if (memcmp(cipher, verify3, sizeof(cipher)))
  1138. return -34;
  1139. return 0;
  1140. }
  1141. #endif /* NO_DES */
  1142. #ifndef NO_AES
  1143. int aes_test(void)
  1144. {
  1145. Aes enc;
  1146. Aes dec;
  1147. const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  1148. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  1149. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  1150. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  1151. };
  1152. const byte verify[] =
  1153. {
  1154. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  1155. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  1156. };
  1157. byte key[] = "0123456789abcdef "; /* align */
  1158. byte iv[] = "1234567890abcdef "; /* align */
  1159. byte cipher[AES_BLOCK_SIZE * 4];
  1160. byte plain [AES_BLOCK_SIZE * 4];
  1161. AesSetKey(&enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  1162. AesSetKey(&dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  1163. AesCbcEncrypt(&enc, cipher, msg, AES_BLOCK_SIZE);
  1164. AesCbcDecrypt(&dec, plain, cipher, AES_BLOCK_SIZE);
  1165. if (memcmp(plain, msg, AES_BLOCK_SIZE))
  1166. return -60;
  1167. if (memcmp(cipher, verify, AES_BLOCK_SIZE))
  1168. return -61;
  1169. #ifdef CYASSL_AES_COUNTER
  1170. {
  1171. const byte ctrKey[] =
  1172. {
  1173. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  1174. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  1175. };
  1176. const byte ctrIv[] =
  1177. {
  1178. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  1179. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  1180. };
  1181. const byte ctrPlain[] =
  1182. {
  1183. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  1184. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  1185. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  1186. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  1187. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  1188. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  1189. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  1190. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  1191. };
  1192. const byte ctrCipher[] =
  1193. {
  1194. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  1195. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  1196. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  1197. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  1198. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  1199. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  1200. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  1201. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  1202. };
  1203. AesSetKeyDirect(&enc, ctrKey, AES_BLOCK_SIZE, ctrIv, AES_ENCRYPTION);
  1204. /* Ctr only uses encrypt, even on key setup */
  1205. AesSetKeyDirect(&dec, ctrKey, AES_BLOCK_SIZE, ctrIv, AES_ENCRYPTION);
  1206. AesCtrEncrypt(&enc, cipher, ctrPlain, AES_BLOCK_SIZE*4);
  1207. AesCtrEncrypt(&dec, plain, cipher, AES_BLOCK_SIZE*4);
  1208. if (memcmp(plain, ctrPlain, AES_BLOCK_SIZE*4))
  1209. return -66;
  1210. if (memcmp(cipher, ctrCipher, AES_BLOCK_SIZE*4))
  1211. return -67;
  1212. }
  1213. #endif /* CYASSL_AES_COUNTER */
  1214. return 0;
  1215. }
  1216. #ifdef HAVE_AESGCM
  1217. int aesgcm_test(void)
  1218. {
  1219. Aes enc;
  1220. /*
  1221. * This is Test Case 16 from the document Galois/
  1222. * Counter Mode of Operation (GCM) by McGrew and
  1223. * Viega.
  1224. */
  1225. const byte k[] =
  1226. {
  1227. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  1228. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  1229. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  1230. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  1231. };
  1232. const byte iv[] =
  1233. {
  1234. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  1235. 0xde, 0xca, 0xf8, 0x88
  1236. };
  1237. const byte p[] =
  1238. {
  1239. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  1240. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  1241. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  1242. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  1243. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  1244. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  1245. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  1246. 0xba, 0x63, 0x7b, 0x39
  1247. };
  1248. const byte a[] =
  1249. {
  1250. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  1251. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  1252. 0xab, 0xad, 0xda, 0xd2
  1253. };
  1254. const byte c[] =
  1255. {
  1256. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  1257. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  1258. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  1259. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  1260. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  1261. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  1262. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  1263. 0xbc, 0xc9, 0xf6, 0x62
  1264. };
  1265. const byte t[] =
  1266. {
  1267. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  1268. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  1269. };
  1270. byte t2[sizeof(t)];
  1271. byte p2[sizeof(c)];
  1272. byte c2[sizeof(p)];
  1273. int result;
  1274. memset(t2, 0, sizeof(t2));
  1275. memset(c2, 0, sizeof(c2));
  1276. memset(p2, 0, sizeof(p2));
  1277. AesGcmSetKey(&enc, k, sizeof(k));
  1278. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  1279. AesGcmEncrypt(&enc, c2, p, sizeof(c2), iv, sizeof(iv),
  1280. t2, sizeof(t2), a, sizeof(a));
  1281. if (memcmp(c, c2, sizeof(c2)))
  1282. return -68;
  1283. if (memcmp(t, t2, sizeof(t2)))
  1284. return -69;
  1285. result = AesGcmDecrypt(&enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  1286. t2, sizeof(t2), a, sizeof(a));
  1287. if (result != 0)
  1288. return -70;
  1289. if (memcmp(p, p2, sizeof(p2)))
  1290. return -71;
  1291. return 0;
  1292. }
  1293. #endif /* HAVE_AESGCM */
  1294. #ifdef HAVE_AESCCM
  1295. int aesccm_test(void)
  1296. {
  1297. Aes enc;
  1298. /* key */
  1299. const byte k[] =
  1300. {
  1301. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  1302. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  1303. };
  1304. /* nonce */
  1305. const byte iv[] =
  1306. {
  1307. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  1308. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  1309. };
  1310. /* plaintext */
  1311. const byte p[] =
  1312. {
  1313. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1314. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  1315. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  1316. };
  1317. const byte a[] =
  1318. {
  1319. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  1320. };
  1321. const byte c[] =
  1322. {
  1323. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  1324. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  1325. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  1326. };
  1327. const byte t[] =
  1328. {
  1329. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  1330. };
  1331. byte t2[sizeof(t)];
  1332. byte p2[sizeof(p)];
  1333. byte c2[sizeof(c)];
  1334. int result;
  1335. memset(t2, 0, sizeof(t2));
  1336. memset(c2, 0, sizeof(c2));
  1337. memset(p2, 0, sizeof(p2));
  1338. AesCcmSetKey(&enc, k, sizeof(k));
  1339. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  1340. AesCcmEncrypt(&enc, c2, p, sizeof(c2), iv, sizeof(iv),
  1341. t2, sizeof(t2), a, sizeof(a));
  1342. if (memcmp(c, c2, sizeof(c2)))
  1343. return -107;
  1344. if (memcmp(t, t2, sizeof(t2)))
  1345. return -108;
  1346. result = AesCcmDecrypt(&enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  1347. t2, sizeof(t2), a, sizeof(a));
  1348. if (result != 0)
  1349. return -109;
  1350. if (memcmp(p, p2, sizeof(p2)))
  1351. return -110;
  1352. /* Test the authentication failure */
  1353. t2[0]++; /* Corrupt the authentication tag. */
  1354. result = AesCcmDecrypt(&enc, p2, c, sizeof(p2), iv, sizeof(iv),
  1355. t2, sizeof(t2), a, sizeof(a));
  1356. if (result == 0)
  1357. return -111;
  1358. /* Clear c2 to compare against p2. p2 should be set to zero in case of
  1359. * authentication fail. */
  1360. memset(c2, 0, sizeof(c2));
  1361. if (memcmp(p2, c2, sizeof(p2)))
  1362. return -112;
  1363. return 0;
  1364. }
  1365. #endif /* HAVE_AESCCM */
  1366. #endif /* NO_AES */
  1367. #ifdef HAVE_CAMELLIA
  1368. enum {
  1369. CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
  1370. };
  1371. typedef struct {
  1372. int type;
  1373. const byte* plaintext;
  1374. const byte* iv;
  1375. const byte* ciphertext;
  1376. const byte* key;
  1377. word32 keySz;
  1378. int errorCode;
  1379. } test_vector_t;
  1380. int camellia_test(void)
  1381. {
  1382. /* Camellia ECB Test Plaintext */
  1383. const byte pte[] =
  1384. {
  1385. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  1386. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  1387. };
  1388. /* Camellia ECB Test Initialization Vector */
  1389. const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  1390. /* Test 1: Camellia ECB 128-bit key */
  1391. const byte k1[] =
  1392. {
  1393. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  1394. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  1395. };
  1396. const byte c1[] =
  1397. {
  1398. 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  1399. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
  1400. };
  1401. /* Test 2: Camellia ECB 192-bit key */
  1402. const byte k2[] =
  1403. {
  1404. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  1405. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  1406. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  1407. };
  1408. const byte c2[] =
  1409. {
  1410. 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  1411. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
  1412. };
  1413. /* Test 3: Camellia ECB 256-bit key */
  1414. const byte k3[] =
  1415. {
  1416. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  1417. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  1418. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  1419. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  1420. };
  1421. const byte c3[] =
  1422. {
  1423. 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  1424. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
  1425. };
  1426. /* Camellia CBC Test Plaintext */
  1427. const byte ptc[] =
  1428. {
  1429. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  1430. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  1431. };
  1432. /* Camellia CBC Test Initialization Vector */
  1433. const byte ivc[] =
  1434. {
  1435. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  1436. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  1437. };
  1438. /* Test 4: Camellia-CBC 128-bit key */
  1439. const byte k4[] =
  1440. {
  1441. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  1442. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
  1443. };
  1444. const byte c4[] =
  1445. {
  1446. 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  1447. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
  1448. };
  1449. /* Test 5: Camellia-CBC 192-bit key */
  1450. const byte k5[] =
  1451. {
  1452. 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  1453. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  1454. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
  1455. };
  1456. const byte c5[] =
  1457. {
  1458. 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  1459. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
  1460. };
  1461. /* Test 6: CBC 256-bit key */
  1462. const byte k6[] =
  1463. {
  1464. 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  1465. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  1466. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  1467. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
  1468. };
  1469. const byte c6[] =
  1470. {
  1471. 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  1472. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
  1473. };
  1474. byte out[CAMELLIA_BLOCK_SIZE];
  1475. Camellia cam;
  1476. int i, testsSz;
  1477. const test_vector_t testVectors[] =
  1478. {
  1479. {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
  1480. {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
  1481. {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
  1482. {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
  1483. {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
  1484. {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
  1485. {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
  1486. {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
  1487. {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
  1488. {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
  1489. {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
  1490. {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
  1491. };
  1492. if ((sizeof(pte) != CAMELLIA_BLOCK_SIZE) ||
  1493. (sizeof(ptc) != CAMELLIA_BLOCK_SIZE))
  1494. return -113;
  1495. testsSz = sizeof(testVectors)/sizeof(test_vector_t);
  1496. for (i = 0; i < testsSz; i++) {
  1497. CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
  1498. testVectors[i].iv);
  1499. switch (testVectors[i].type) {
  1500. case CAM_ECB_ENC:
  1501. CamelliaEncryptDirect(&cam, out, testVectors[i].plaintext);
  1502. if (memcmp(out, testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE))
  1503. return testVectors[i].errorCode;
  1504. break;
  1505. case CAM_ECB_DEC:
  1506. CamelliaDecryptDirect(&cam, out, testVectors[i].ciphertext);
  1507. if (memcmp(out, testVectors[i].plaintext, CAMELLIA_BLOCK_SIZE))
  1508. return testVectors[i].errorCode;
  1509. break;
  1510. case CAM_CBC_ENC:
  1511. CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
  1512. CAMELLIA_BLOCK_SIZE);
  1513. if (memcmp(out, testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE))
  1514. return testVectors[i].errorCode;
  1515. break;
  1516. case CAM_CBC_DEC:
  1517. CamelliaCbcDecrypt(&cam, out, testVectors[i].ciphertext,
  1518. CAMELLIA_BLOCK_SIZE);
  1519. if (memcmp(out, testVectors[i].plaintext, CAMELLIA_BLOCK_SIZE))
  1520. return testVectors[i].errorCode;
  1521. break;
  1522. default:
  1523. break;
  1524. }
  1525. }
  1526. /* Setting the IV and checking it was actually set. */
  1527. CamelliaSetIV(&cam, ivc);
  1528. if (XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE))
  1529. return -1;
  1530. /* Setting the IV to NULL should leave the IV unchanged */
  1531. if (CamelliaSetIV(&cam, NULL) != 0 ||
  1532. XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE))
  1533. return -1;
  1534. /* First parameter should never be null */
  1535. if (CamelliaSetIV(NULL, NULL) == 0)
  1536. return -1;
  1537. /* First parameter should never be null, check it fails */
  1538. if (CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
  1539. return -1;
  1540. /* Key should have a size of 16, 24, or 32 */
  1541. if (CamelliaSetKey(&cam, k1, 0, NULL) == 0)
  1542. return -1;
  1543. return 0;
  1544. }
  1545. #endif /* HAVE_CAMELLIA */
  1546. int random_test(void)
  1547. {
  1548. RNG rng;
  1549. byte block[32];
  1550. int ret = InitRng(&rng);
  1551. if (ret != 0) return -39;
  1552. RNG_GenerateBlock(&rng, block, sizeof(block));
  1553. return 0;
  1554. }
  1555. #ifdef HAVE_NTRU
  1556. byte GetEntropy(ENTROPY_CMD cmd, byte* out);
  1557. byte GetEntropy(ENTROPY_CMD cmd, byte* out)
  1558. {
  1559. static RNG rng;
  1560. if (cmd == INIT) {
  1561. int ret = InitRng(&rng);
  1562. if (ret == 0)
  1563. return 1;
  1564. else
  1565. return 0;
  1566. }
  1567. if (out == NULL)
  1568. return 0;
  1569. if (cmd == GET_BYTE_OF_ENTROPY) {
  1570. RNG_GenerateBlock(&rng, out, 1);
  1571. return 1;
  1572. }
  1573. if (cmd == GET_NUM_BYTES_PER_BYTE_OF_ENTROPY) {
  1574. *out = 1;
  1575. return 1;
  1576. }
  1577. return 0;
  1578. }
  1579. #endif /* HAVE_NTRU */
  1580. #ifndef NO_RSA
  1581. #ifdef FREESCALE_MQX
  1582. static const char* clientKey = "a:\certs\\client-key.der";
  1583. static const char* clientCert = "a:\certs\\client-cert.der";
  1584. #ifdef CYASSL_CERT_GEN
  1585. static const char* caKeyFile = "a:\certs\\ca-key.der";
  1586. static const char* caCertFile = "a:\certs\\ca-cert.pem";
  1587. #endif
  1588. #elif !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  1589. static const char* clientKey = "./certs/client-key.der";
  1590. static const char* clientCert = "./certs/client-cert.der";
  1591. #ifdef CYASSL_CERT_GEN
  1592. static const char* caKeyFile = "./certs/ca-key.der";
  1593. static const char* caCertFile = "./certs/ca-cert.pem";
  1594. #endif
  1595. #endif
  1596. #define FOURK_BUF 4096
  1597. int rsa_test(void)
  1598. {
  1599. byte* tmp;
  1600. size_t bytes;
  1601. RsaKey key;
  1602. RNG rng;
  1603. word32 idx = 0;
  1604. int ret;
  1605. byte in[] = "Everyone gets Friday off.";
  1606. word32 inLen = (word32)strlen((char*)in);
  1607. byte out[256];
  1608. byte plain[256];
  1609. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  1610. FILE* file, * file2;
  1611. #endif
  1612. #ifdef CYASSL_TEST_CERT
  1613. DecodedCert cert;
  1614. #endif
  1615. tmp = (byte*)malloc(FOURK_BUF);
  1616. if (tmp == NULL)
  1617. return -40;
  1618. #ifdef USE_CERT_BUFFERS_1024
  1619. XMEMCPY(tmp, client_key_der_1024, sizeof(client_key_der_1024));
  1620. bytes = sizeof(client_key_der_1024);
  1621. #elif defined(USE_CERT_BUFFERS_2048)
  1622. XMEMCPY(tmp, client_key_der_2048, sizeof(client_key_der_2048));
  1623. bytes = sizeof(client_key_der_2048);
  1624. #else
  1625. file = fopen(clientKey, "rb");
  1626. if (!file)
  1627. err_sys("can't open ./certs/client-key.der, "
  1628. "Please run from CyaSSL home dir", -40);
  1629. bytes = fread(tmp, 1, FOURK_BUF, file);
  1630. #endif /* USE_CERT_BUFFERS */
  1631. InitRsaKey(&key, 0);
  1632. ret = RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  1633. if (ret != 0) return -41;
  1634. ret = InitRng(&rng);
  1635. if (ret != 0) return -42;
  1636. ret = RsaPublicEncrypt(in, inLen, out, sizeof(out), &key, &rng);
  1637. if (ret < 0) return -43;
  1638. ret = RsaPrivateDecrypt(out, ret, plain, sizeof(plain), &key);
  1639. if (ret < 0) return -44;
  1640. if (memcmp(plain, in, inLen)) return -45;
  1641. ret = RsaSSL_Sign(in, inLen, out, sizeof(out), &key, &rng);
  1642. if (ret < 0) return -46;
  1643. memset(plain, 0, sizeof(plain));
  1644. ret = RsaSSL_Verify(out, ret, plain, sizeof(plain), &key);
  1645. if (ret < 0) return -47;
  1646. if (memcmp(plain, in, ret)) return -48;
  1647. #ifdef USE_CERT_BUFFERS_1024
  1648. XMEMCPY(tmp, client_cert_der_1024, sizeof(client_cert_der_1024));
  1649. bytes = sizeof(client_cert_der_1024);
  1650. #elif defined(USE_CERT_BUFFERS_2048)
  1651. XMEMCPY(tmp, client_cert_der_2048, sizeof(client_cert_der_2048));
  1652. bytes = sizeof(client_cert_der_2048);
  1653. #else
  1654. file2 = fopen(clientCert, "rb");
  1655. if (!file2)
  1656. return -49;
  1657. bytes = fread(tmp, 1, FOURK_BUF, file2);
  1658. #endif
  1659. #ifdef CYASSL_TEST_CERT
  1660. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  1661. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  1662. if (ret != 0) return -491;
  1663. FreeDecodedCert(&cert);
  1664. #else
  1665. (void)bytes;
  1666. #endif
  1667. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  1668. fclose(file2);
  1669. fclose(file);
  1670. #endif
  1671. #ifdef CYASSL_KEY_GEN
  1672. {
  1673. byte* der;
  1674. byte* pem;
  1675. int derSz = 0;
  1676. int pemSz = 0;
  1677. RsaKey derIn;
  1678. RsaKey genKey;
  1679. FILE* keyFile;
  1680. FILE* pemFile;
  1681. InitRsaKey(&genKey, 0);
  1682. ret = MakeRsaKey(&genKey, 1024, 65537, &rng);
  1683. if (ret != 0)
  1684. return -301;
  1685. der = (byte*)malloc(FOURK_BUF);
  1686. if (der == NULL)
  1687. return -307;
  1688. pem = (byte*)malloc(FOURK_BUF);
  1689. if (pem == NULL)
  1690. return -308;
  1691. derSz = RsaKeyToDer(&genKey, der, FOURK_BUF);
  1692. if (derSz < 0)
  1693. return -302;
  1694. keyFile = fopen("./key.der", "wb");
  1695. if (!keyFile)
  1696. return -303;
  1697. ret = (int)fwrite(der, derSz, 1, keyFile);
  1698. fclose(keyFile);
  1699. pemSz = DerToPem(der, derSz, pem, FOURK_BUF, PRIVATEKEY_TYPE);
  1700. if (pemSz < 0)
  1701. return -304;
  1702. pemFile = fopen("./key.pem", "wb");
  1703. if (!pemFile)
  1704. return -305;
  1705. ret = (int)fwrite(pem, pemSz, 1, pemFile);
  1706. fclose(pemFile);
  1707. InitRsaKey(&derIn, 0);
  1708. idx = 0;
  1709. ret = RsaPrivateKeyDecode(der, &idx, &derIn, derSz);
  1710. if (ret != 0)
  1711. return -306;
  1712. FreeRsaKey(&derIn);
  1713. FreeRsaKey(&genKey);
  1714. free(pem);
  1715. free(der);
  1716. }
  1717. #endif /* CYASSL_KEY_GEN */
  1718. #ifdef CYASSL_CERT_GEN
  1719. /* self signed */
  1720. {
  1721. Cert myCert;
  1722. byte* derCert;
  1723. byte* pem;
  1724. FILE* derFile;
  1725. FILE* pemFile;
  1726. int certSz;
  1727. int pemSz;
  1728. #ifdef CYASSL_TEST_CERT
  1729. DecodedCert decode;
  1730. #endif
  1731. derCert = (byte*)malloc(FOURK_BUF);
  1732. if (derCert == NULL)
  1733. return -309;
  1734. pem = (byte*)malloc(FOURK_BUF);
  1735. if (pem == NULL)
  1736. return -310;
  1737. InitCert(&myCert);
  1738. strncpy(myCert.subject.country, "US", CTC_NAME_SIZE);
  1739. strncpy(myCert.subject.state, "OR", CTC_NAME_SIZE);
  1740. strncpy(myCert.subject.locality, "Portland", CTC_NAME_SIZE);
  1741. strncpy(myCert.subject.org, "yaSSL", CTC_NAME_SIZE);
  1742. strncpy(myCert.subject.unit, "Development", CTC_NAME_SIZE);
  1743. strncpy(myCert.subject.commonName, "www.yassl.com", CTC_NAME_SIZE);
  1744. strncpy(myCert.subject.email, "info@yassl.com", CTC_NAME_SIZE);
  1745. myCert.isCA = 1;
  1746. myCert.sigType = CTC_SHA256wRSA;
  1747. certSz = MakeSelfCert(&myCert, derCert, FOURK_BUF, &key, &rng);
  1748. if (certSz < 0)
  1749. return -401;
  1750. #ifdef CYASSL_TEST_CERT
  1751. InitDecodedCert(&decode, derCert, certSz, 0);
  1752. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  1753. if (ret != 0)
  1754. return -402;
  1755. FreeDecodedCert(&decode);
  1756. #endif
  1757. derFile = fopen("./cert.der", "wb");
  1758. if (!derFile)
  1759. return -403;
  1760. ret = (int)fwrite(derCert, certSz, 1, derFile);
  1761. fclose(derFile);
  1762. pemSz = DerToPem(derCert, certSz, pem, FOURK_BUF, CERT_TYPE);
  1763. if (pemSz < 0)
  1764. return -404;
  1765. pemFile = fopen("./cert.pem", "wb");
  1766. if (!pemFile)
  1767. return -405;
  1768. ret = (int)fwrite(pem, pemSz, 1, pemFile);
  1769. fclose(pemFile);
  1770. free(pem);
  1771. free(derCert);
  1772. }
  1773. /* CA style */
  1774. {
  1775. RsaKey caKey;
  1776. Cert myCert;
  1777. byte* derCert;
  1778. byte* pem;
  1779. FILE* derFile;
  1780. FILE* pemFile;
  1781. int certSz;
  1782. int pemSz;
  1783. size_t bytes3;
  1784. word32 idx3 = 0;
  1785. #ifdef CYASSL_TEST_CERT
  1786. DecodedCert decode;
  1787. #endif
  1788. derCert = (byte*)malloc(FOURK_BUF);
  1789. if (derCert == NULL)
  1790. return -311;
  1791. pem = (byte*)malloc(FOURK_BUF);
  1792. if (pem == NULL)
  1793. return -312;
  1794. FILE* file3 = fopen(caKeyFile, "rb");
  1795. if (!file3)
  1796. return -412;
  1797. bytes3 = fread(tmp, 1, FOURK_BUF, file3);
  1798. fclose(file3);
  1799. InitRsaKey(&caKey, 0);
  1800. ret = RsaPrivateKeyDecode(tmp, &idx3, &caKey, (word32)bytes3);
  1801. if (ret != 0) return -413;
  1802. InitCert(&myCert);
  1803. strncpy(myCert.subject.country, "US", CTC_NAME_SIZE);
  1804. strncpy(myCert.subject.state, "OR", CTC_NAME_SIZE);
  1805. strncpy(myCert.subject.locality, "Portland", CTC_NAME_SIZE);
  1806. strncpy(myCert.subject.org, "yaSSL", CTC_NAME_SIZE);
  1807. strncpy(myCert.subject.unit, "Development", CTC_NAME_SIZE);
  1808. strncpy(myCert.subject.commonName, "www.yassl.com", CTC_NAME_SIZE);
  1809. strncpy(myCert.subject.email, "info@yassl.com", CTC_NAME_SIZE);
  1810. ret = SetIssuer(&myCert, caCertFile);
  1811. if (ret < 0)
  1812. return -405;
  1813. certSz = MakeCert(&myCert, derCert, FOURK_BUF, &key, &rng);
  1814. if (certSz < 0)
  1815. return -407;
  1816. certSz = SignCert(&myCert, derCert, FOURK_BUF, &caKey, &rng);
  1817. if (certSz < 0)
  1818. return -408;
  1819. #ifdef CYASSL_TEST_CERT
  1820. InitDecodedCert(&decode, derCert, certSz, 0);
  1821. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  1822. if (ret != 0)
  1823. return -409;
  1824. FreeDecodedCert(&decode);
  1825. #endif
  1826. derFile = fopen("./othercert.der", "wb");
  1827. if (!derFile)
  1828. return -410;
  1829. ret = (int)fwrite(derCert, certSz, 1, derFile);
  1830. fclose(derFile);
  1831. pemSz = DerToPem(derCert, certSz, pem, FOURK_BUF, CERT_TYPE);
  1832. if (pemSz < 0)
  1833. return -411;
  1834. pemFile = fopen("./othercert.pem", "wb");
  1835. if (!pemFile)
  1836. return -412;
  1837. ret = (int)fwrite(pem, pemSz, 1, pemFile);
  1838. fclose(pemFile);
  1839. free(pem);
  1840. free(derCert);
  1841. FreeRsaKey(&caKey);
  1842. }
  1843. #ifdef HAVE_NTRU
  1844. {
  1845. RsaKey caKey;
  1846. Cert myCert;
  1847. byte* derCert;
  1848. byte* pem;
  1849. FILE* derFile;
  1850. FILE* pemFile;
  1851. FILE* caFile;
  1852. FILE* ntruPrivFile;
  1853. int certSz;
  1854. int pemSz;
  1855. size_t bytes;
  1856. word32 idx = 0;
  1857. #ifdef CYASSL_TEST_CERT
  1858. DecodedCert decode;
  1859. #endif
  1860. derCert = (byte*)malloc(FOURK_BUF);
  1861. if (derCert == NULL)
  1862. return -311;
  1863. pem = (byte*)malloc(FOURK_BUF);
  1864. if (pem == NULL)
  1865. return -312;
  1866. byte public_key[557]; /* sized for EES401EP2 */
  1867. word16 public_key_len; /* no. of octets in public key */
  1868. byte private_key[607]; /* sized for EES401EP2 */
  1869. word16 private_key_len; /* no. of octets in private key */
  1870. DRBG_HANDLE drbg;
  1871. static uint8_t const pers_str[] = {
  1872. 'C', 'y', 'a', 'S', 'S', 'L', ' ', 't', 'e', 's', 't'
  1873. };
  1874. word32 rc = crypto_drbg_instantiate(112, pers_str, sizeof(pers_str),
  1875. GetEntropy, &drbg);
  1876. if (rc != DRBG_OK)
  1877. return -450;
  1878. rc = crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2, &public_key_len,
  1879. NULL, &private_key_len, NULL);
  1880. if (rc != NTRU_OK)
  1881. return -451;
  1882. rc = crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2, &public_key_len,
  1883. public_key, &private_key_len, private_key);
  1884. crypto_drbg_uninstantiate(drbg);
  1885. if (rc != NTRU_OK)
  1886. return -452;
  1887. caFile = fopen(caKeyFile, "rb");
  1888. if (!caFile)
  1889. return -453;
  1890. bytes = fread(tmp, 1, FOURK_BUF, caFile);
  1891. fclose(caFile);
  1892. InitRsaKey(&caKey, 0);
  1893. ret = RsaPrivateKeyDecode(tmp, &idx, &caKey, (word32)bytes);
  1894. if (ret != 0) return -454;
  1895. InitCert(&myCert);
  1896. strncpy(myCert.subject.country, "US", CTC_NAME_SIZE);
  1897. strncpy(myCert.subject.state, "OR", CTC_NAME_SIZE);
  1898. strncpy(myCert.subject.locality, "Portland", CTC_NAME_SIZE);
  1899. strncpy(myCert.subject.org, "yaSSL", CTC_NAME_SIZE);
  1900. strncpy(myCert.subject.unit, "Development", CTC_NAME_SIZE);
  1901. strncpy(myCert.subject.commonName, "www.yassl.com", CTC_NAME_SIZE);
  1902. strncpy(myCert.subject.email, "info@yassl.com", CTC_NAME_SIZE);
  1903. ret = SetIssuer(&myCert, caCertFile);
  1904. if (ret < 0)
  1905. return -455;
  1906. certSz = MakeNtruCert(&myCert, derCert, FOURK_BUF, public_key,
  1907. public_key_len, &rng);
  1908. if (certSz < 0)
  1909. return -456;
  1910. certSz = SignCert(&myCert, derCert, FOURK_BUF, &caKey, &rng);
  1911. if (certSz < 0)
  1912. return -457;
  1913. #ifdef CYASSL_TEST_CERT
  1914. InitDecodedCert(&decode, derCert, certSz, 0);
  1915. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  1916. if (ret != 0)
  1917. return -458;
  1918. FreeDecodedCert(&decode);
  1919. #endif
  1920. derFile = fopen("./ntru-cert.der", "wb");
  1921. if (!derFile)
  1922. return -459;
  1923. ret = fwrite(derCert, certSz, 1, derFile);
  1924. fclose(derFile);
  1925. pemSz = DerToPem(derCert, certSz, pem, FOURK_BUF, CERT_TYPE);
  1926. if (pemSz < 0)
  1927. return -460;
  1928. pemFile = fopen("./ntru-cert.pem", "wb");
  1929. if (!pemFile)
  1930. return -461;
  1931. ret = fwrite(pem, pemSz, 1, pemFile);
  1932. fclose(pemFile);
  1933. ntruPrivFile = fopen("./ntru-key.raw", "wb");
  1934. if (!ntruPrivFile)
  1935. return -462;
  1936. ret = fwrite(private_key, private_key_len, 1, ntruPrivFile);
  1937. fclose(ntruPrivFile);
  1938. free(pem);
  1939. free(derCert);
  1940. FreeRsaKey(&caKey);
  1941. }
  1942. #endif /* HAVE_NTRU */
  1943. #endif /* CYASSL_CERT_GEN */
  1944. FreeRsaKey(&key);
  1945. free(tmp);
  1946. return 0;
  1947. }
  1948. #endif
  1949. #ifndef NO_DH
  1950. #ifdef FREESCALE_MQX
  1951. static const char* dhKey = "a:\certs\\dh2048.der";
  1952. #elif !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  1953. static const char* dhKey = "./certs/dh2048.der";
  1954. #endif
  1955. int dh_test(void)
  1956. {
  1957. int ret;
  1958. word32 bytes;
  1959. word32 idx = 0, privSz, pubSz, privSz2, pubSz2, agreeSz, agreeSz2;
  1960. byte tmp[1024];
  1961. byte priv[256];
  1962. byte pub[256];
  1963. byte priv2[256];
  1964. byte pub2[256];
  1965. byte agree[256];
  1966. byte agree2[256];
  1967. DhKey key;
  1968. DhKey key2;
  1969. RNG rng;
  1970. #ifdef USE_CERT_BUFFERS_1024
  1971. XMEMCPY(tmp, dh_key_der_1024, sizeof(dh_key_der_1024));
  1972. bytes = sizeof(dh_key_der_1024);
  1973. #elif defined(USE_CERT_BUFFERS_2048)
  1974. XMEMCPY(tmp, dh_key_der_2048, sizeof(dh_key_der_2048));
  1975. bytes = sizeof(dh_key_der_2048);
  1976. #else
  1977. FILE* file = fopen(dhKey, "rb");
  1978. if (!file)
  1979. return -50;
  1980. bytes = (word32) fread(tmp, 1, sizeof(tmp), file);
  1981. #endif /* USE_CERT_BUFFERS */
  1982. InitDhKey(&key);
  1983. InitDhKey(&key2);
  1984. ret = DhKeyDecode(tmp, &idx, &key, bytes);
  1985. if (ret != 0)
  1986. return -51;
  1987. idx = 0;
  1988. ret = DhKeyDecode(tmp, &idx, &key2, bytes);
  1989. if (ret != 0)
  1990. return -52;
  1991. ret = InitRng(&rng);
  1992. if (ret != 0)
  1993. return -53;
  1994. ret = DhGenerateKeyPair(&key, &rng, priv, &privSz, pub, &pubSz);
  1995. ret += DhGenerateKeyPair(&key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  1996. if (ret != 0)
  1997. return -54;
  1998. ret = DhAgree(&key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  1999. ret += DhAgree(&key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  2000. if (ret != 0)
  2001. return -55;
  2002. if (memcmp(agree, agree2, agreeSz))
  2003. return -56;
  2004. FreeDhKey(&key);
  2005. FreeDhKey(&key2);
  2006. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  2007. fclose(file);
  2008. #endif
  2009. return 0;
  2010. }
  2011. #endif /* NO_DH */
  2012. #ifndef NO_DSA
  2013. #ifdef FREESCALE_MQX
  2014. static const char* dsaKey = "a:\certs\\dsa2048.der";
  2015. #elif !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  2016. static const char* dsaKey = "./certs/dsa2048.der";
  2017. #endif
  2018. int dsa_test(void)
  2019. {
  2020. int ret, answer;
  2021. word32 bytes;
  2022. word32 idx = 0;
  2023. byte tmp[1024];
  2024. DsaKey key;
  2025. RNG rng;
  2026. Sha sha;
  2027. byte hash[SHA_DIGEST_SIZE];
  2028. byte signature[40];
  2029. #ifdef USE_CERT_BUFFERS_1024
  2030. XMEMCPY(tmp, dsa_key_der_1024, sizeof(dsa_key_der_1024));
  2031. bytes = sizeof(dsa_key_der_1024);
  2032. #elif defined(USE_CERT_BUFFERS_2048)
  2033. XMEMCPY(tmp, dsa_key_der_2048, sizeof(dsa_key_der_2048));
  2034. bytes = sizeof(dsa_key_der_2048);
  2035. #else
  2036. FILE* file = fopen(dsaKey, "rb");
  2037. if (!file)
  2038. return -60;
  2039. bytes = (word32) fread(tmp, 1, sizeof(tmp), file);
  2040. #endif /* USE_CERT_BUFFERS */
  2041. InitSha(&sha);
  2042. ShaUpdate(&sha, tmp, bytes);
  2043. ShaFinal(&sha, hash);
  2044. InitDsaKey(&key);
  2045. ret = DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  2046. if (ret != 0) return -61;
  2047. ret = InitRng(&rng);
  2048. if (ret != 0) return -62;
  2049. ret = DsaSign(hash, signature, &key, &rng);
  2050. if (ret != 0) return -63;
  2051. ret = DsaVerify(hash, signature, &key, &answer);
  2052. if (ret != 0) return -64;
  2053. if (answer != 1) return -65;
  2054. FreeDsaKey(&key);
  2055. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  2056. fclose(file);
  2057. #endif
  2058. return 0;
  2059. }
  2060. #endif /* NO_DSA */
  2061. #ifdef OPENSSL_EXTRA
  2062. int openssl_test(void)
  2063. {
  2064. EVP_MD_CTX md_ctx;
  2065. testVector a, b, c, d, e, f;
  2066. byte hash[SHA_DIGEST_SIZE*4]; /* max size */
  2067. (void)e;
  2068. (void)f;
  2069. a.input = "1234567890123456789012345678901234567890123456789012345678"
  2070. "9012345678901234567890";
  2071. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  2072. "\x7a";
  2073. a.inLen = strlen(a.input);
  2074. a.outLen = strlen(a.output);
  2075. EVP_MD_CTX_init(&md_ctx);
  2076. EVP_DigestInit(&md_ctx, EVP_md5());
  2077. EVP_DigestUpdate(&md_ctx, a.input, a.inLen);
  2078. EVP_DigestFinal(&md_ctx, hash, 0);
  2079. if (memcmp(hash, a.output, MD5_DIGEST_SIZE) != 0)
  2080. return -71;
  2081. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2082. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2083. "aaaaaaaaaa";
  2084. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  2085. "\x53\x99\x5E\x26\xA0";
  2086. b.inLen = strlen(b.input);
  2087. b.outLen = strlen(b.output);
  2088. EVP_MD_CTX_init(&md_ctx);
  2089. EVP_DigestInit(&md_ctx, EVP_sha1());
  2090. EVP_DigestUpdate(&md_ctx, b.input, b.inLen);
  2091. EVP_DigestFinal(&md_ctx, hash, 0);
  2092. if (memcmp(hash, b.output, SHA_DIGEST_SIZE) != 0)
  2093. return -72;
  2094. d.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2095. d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  2096. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  2097. "\x06\xC1";
  2098. d.inLen = strlen(d.input);
  2099. d.outLen = strlen(d.output);
  2100. EVP_MD_CTX_init(&md_ctx);
  2101. EVP_DigestInit(&md_ctx, EVP_sha256());
  2102. EVP_DigestUpdate(&md_ctx, d.input, d.inLen);
  2103. EVP_DigestFinal(&md_ctx, hash, 0);
  2104. if (memcmp(hash, d.output, SHA256_DIGEST_SIZE) != 0)
  2105. return -78;
  2106. #ifdef CYASSL_SHA384
  2107. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2108. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2109. e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  2110. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  2111. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  2112. "\x74\x60\x39";
  2113. e.inLen = strlen(e.input);
  2114. e.outLen = strlen(e.output);
  2115. EVP_MD_CTX_init(&md_ctx);
  2116. EVP_DigestInit(&md_ctx, EVP_sha384());
  2117. EVP_DigestUpdate(&md_ctx, e.input, e.inLen);
  2118. EVP_DigestFinal(&md_ctx, hash, 0);
  2119. if (memcmp(hash, e.output, SHA384_DIGEST_SIZE) != 0)
  2120. return -79;
  2121. #endif /* CYASSL_SHA384 */
  2122. #ifdef CYASSL_SHA512
  2123. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2124. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2125. f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  2126. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  2127. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  2128. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  2129. "\x87\x4b\xe9\x09";
  2130. f.inLen = strlen(f.input);
  2131. f.outLen = strlen(f.output);
  2132. EVP_MD_CTX_init(&md_ctx);
  2133. EVP_DigestInit(&md_ctx, EVP_sha512());
  2134. EVP_DigestUpdate(&md_ctx, f.input, f.inLen);
  2135. EVP_DigestFinal(&md_ctx, hash, 0);
  2136. if (memcmp(hash, f.output, SHA512_DIGEST_SIZE) != 0)
  2137. return -80;
  2138. #endif /* CYASSL_SHA512 */
  2139. if (RAND_bytes(hash, sizeof(hash)) != 1)
  2140. return -73;
  2141. c.input = "what do ya want for nothing?";
  2142. c.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  2143. "\x38";
  2144. c.inLen = strlen(c.input);
  2145. c.outLen = strlen(c.output);
  2146. HMAC(EVP_md5(), "Jefe", 4, (byte*)c.input, (int)c.inLen, hash, 0);
  2147. if (memcmp(hash, c.output, MD5_DIGEST_SIZE) != 0)
  2148. return -74;
  2149. { /* des test */
  2150. const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  2151. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  2152. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  2153. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  2154. };
  2155. byte plain[24];
  2156. byte cipher[24];
  2157. const_DES_cblock key =
  2158. {
  2159. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  2160. };
  2161. DES_cblock iv =
  2162. {
  2163. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  2164. };
  2165. DES_key_schedule sched;
  2166. const byte verify[] =
  2167. {
  2168. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  2169. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  2170. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  2171. };
  2172. DES_key_sched(&key, &sched);
  2173. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  2174. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  2175. if (memcmp(plain, vector, sizeof(vector)) != 0)
  2176. return -75;
  2177. if (memcmp(cipher, verify, sizeof(verify)) != 0)
  2178. return -76;
  2179. /* test changing iv */
  2180. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  2181. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  2182. if (memcmp(cipher, verify, sizeof(verify)) != 0)
  2183. return -77;
  2184. } /* end des test */
  2185. { /* evp_cipher test */
  2186. EVP_CIPHER_CTX ctx;
  2187. const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  2188. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  2189. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  2190. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  2191. };
  2192. const byte verify[] =
  2193. {
  2194. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  2195. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  2196. };
  2197. byte key[] = "0123456789abcdef "; /* align */
  2198. byte iv[] = "1234567890abcdef "; /* align */
  2199. byte cipher[AES_BLOCK_SIZE * 4];
  2200. byte plain [AES_BLOCK_SIZE * 4];
  2201. EVP_CIPHER_CTX_init(&ctx);
  2202. if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  2203. return -81;
  2204. if (EVP_Cipher(&ctx, cipher, (byte*)msg, 16) == 0)
  2205. return -82;
  2206. if (memcmp(cipher, verify, AES_BLOCK_SIZE))
  2207. return -83;
  2208. EVP_CIPHER_CTX_init(&ctx);
  2209. if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  2210. return -84;
  2211. if (EVP_Cipher(&ctx, plain, cipher, 16) == 0)
  2212. return -85;
  2213. if (memcmp(plain, msg, AES_BLOCK_SIZE))
  2214. return -86;
  2215. } /* end evp_cipher test */
  2216. return 0;
  2217. }
  2218. #endif /* OPENSSL_EXTRA */
  2219. #ifndef NO_PWDBASED
  2220. int pkcs12_test(void)
  2221. {
  2222. const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
  2223. 0x00, 0x00 };
  2224. const byte salt[] = { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
  2225. const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
  2226. 0x00, 0x67, 0x00, 0x00 };
  2227. const byte salt2[] = { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
  2228. byte derived[64];
  2229. const byte verify[] = {
  2230. 0x8A, 0xAA, 0xE6, 0x29, 0x7B, 0x6C, 0xB0, 0x46,
  2231. 0x42, 0xAB, 0x5B, 0x07, 0x78, 0x51, 0x28, 0x4E,
  2232. 0xB7, 0x12, 0x8F, 0x1A, 0x2A, 0x7F, 0xBC, 0xA3
  2233. };
  2234. const byte verify2[] = {
  2235. 0x48, 0x3D, 0xD6, 0xE9, 0x19, 0xD7, 0xDE, 0x2E,
  2236. 0x8E, 0x64, 0x8B, 0xA8, 0xF8, 0x62, 0xF3, 0xFB,
  2237. 0xFB, 0xDC, 0x2B, 0xCB, 0x2C, 0x02, 0x95, 0x7F
  2238. };
  2239. int id = 1;
  2240. int kLen = 24;
  2241. int iterations = 1;
  2242. int ret = PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8, iterations,
  2243. kLen, SHA, id);
  2244. if (ret < 0)
  2245. return -103;
  2246. if ( (ret = memcmp(derived, verify, kLen)) != 0)
  2247. return -104;
  2248. iterations = 1000;
  2249. ret = PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8, iterations,
  2250. kLen, SHA, id);
  2251. if (ret < 0)
  2252. return -105;
  2253. if ( (ret = memcmp(derived, verify2, 24)) != 0)
  2254. return -106;
  2255. return 0;
  2256. }
  2257. int pbkdf2_test(void)
  2258. {
  2259. char passwd[] = "password";
  2260. const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  2261. int iterations = 2048;
  2262. int kLen = 24;
  2263. byte derived[64];
  2264. const byte verify[] = {
  2265. 0xBF, 0xDE, 0x6B, 0xE9, 0x4D, 0xF7, 0xE1, 0x1D, 0xD4, 0x09, 0xBC, 0xE2,
  2266. 0x0A, 0x02, 0x55, 0xEC, 0x32, 0x7C, 0xB9, 0x36, 0xFF, 0xE9, 0x36, 0x43
  2267. };
  2268. PBKDF2(derived, (byte*)passwd, (int)strlen(passwd), salt, 8, iterations,
  2269. kLen, SHA);
  2270. if (memcmp(derived, verify, sizeof(verify)) != 0)
  2271. return -102;
  2272. return 0;
  2273. }
  2274. int pbkdf1_test(void)
  2275. {
  2276. char passwd[] = "password";
  2277. const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  2278. int iterations = 1000;
  2279. int kLen = 16;
  2280. byte derived[16];
  2281. const byte verify[] = {
  2282. 0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F, 0xAF, 0x10, 0xEB, 0xFB,
  2283. 0x4A, 0x3D, 0x2A, 0x20
  2284. };
  2285. PBKDF1(derived, (byte*)passwd, (int)strlen(passwd), salt, 8, iterations,
  2286. kLen, SHA);
  2287. if (memcmp(derived, verify, sizeof(verify)) != 0)
  2288. return -101;
  2289. return 0;
  2290. }
  2291. int pwdbased_test(void)
  2292. {
  2293. int ret = pbkdf1_test();
  2294. ret += pbkdf2_test();
  2295. return ret + pkcs12_test();
  2296. }
  2297. #endif /* NO_PWDBASED */
  2298. #ifdef HAVE_ECC
  2299. int ecc_test(void)
  2300. {
  2301. RNG rng;
  2302. byte sharedA[1024];
  2303. byte sharedB[1024];
  2304. byte sig[1024];
  2305. byte digest[20];
  2306. byte exportBuf[1024];
  2307. word32 x, y;
  2308. int i, verify, ret;
  2309. ecc_key userA, userB, pubKey;
  2310. ret = InitRng(&rng);
  2311. if (ret != 0)
  2312. return -1001;
  2313. ecc_init(&userA);
  2314. ecc_init(&userB);
  2315. ecc_init(&pubKey);
  2316. ret = ecc_make_key(&rng, 32, &userA);
  2317. ret = ecc_make_key(&rng, 32, &userB);
  2318. if (ret != 0)
  2319. return -1002;
  2320. x = sizeof(sharedA);
  2321. ret = ecc_shared_secret(&userA, &userB, sharedA, &x);
  2322. y = sizeof(sharedB);
  2323. ret = ecc_shared_secret(&userB, &userA, sharedB, &y);
  2324. if (ret != 0)
  2325. return -1003;
  2326. if (y != x)
  2327. return -1004;
  2328. if (memcmp(sharedA, sharedB, x))
  2329. return -1005;
  2330. x = sizeof(exportBuf);
  2331. ret = ecc_export_x963(&userA, exportBuf, &x);
  2332. if (ret != 0)
  2333. return -1006;
  2334. ret = ecc_import_x963(exportBuf, x, &pubKey);
  2335. if (ret != 0)
  2336. return -1007;
  2337. y = sizeof(sharedB);
  2338. ret = ecc_shared_secret(&userB, &pubKey, sharedB, &y);
  2339. if (ret != 0)
  2340. return -1008;
  2341. if (memcmp(sharedA, sharedB, y))
  2342. return -1010;
  2343. /* test DSA sign hash */
  2344. for (i = 0; i < (int)sizeof(digest); i++)
  2345. digest[i] = i;
  2346. x = sizeof(sig);
  2347. ret = ecc_sign_hash(digest, sizeof(digest), sig, &x, &rng, &userA);
  2348. verify = 0;
  2349. ret = ecc_verify_hash(sig, x, digest, sizeof(digest), &verify, &userA);
  2350. if (ret != 0)
  2351. return -1011;
  2352. if (verify != 1)
  2353. return -1012;
  2354. ecc_free(&pubKey);
  2355. ecc_free(&userB);
  2356. ecc_free(&userA);
  2357. return 0;
  2358. }
  2359. #endif /* HAVE_ECC */