1
0

api.c 582 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215
  1. /* api.c API unit tests
  2. *
  3. * Copyright (C) 2006-2017 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL 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. * wolfSSL 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /*----------------------------------------------------------------------------*
  22. | Includes
  23. *----------------------------------------------------------------------------*/
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #include <wolfssl/wolfcrypt/settings.h>
  28. #ifndef FOURK_BUF
  29. #define FOURK_BUF 4096
  30. #endif
  31. #ifndef TWOK_BUF
  32. #define TWOK_BUF 2048
  33. #endif
  34. #ifndef ONEK_BUF
  35. #define ONEK_BUF 1024
  36. #endif
  37. #if defined(WOLFSSL_STATIC_MEMORY)
  38. #include <wolfssl/wolfcrypt/memory.h>
  39. #endif /* WOLFSSL_STATIC_MEMORY */
  40. #ifndef HEAP_HINT
  41. #define HEAP_HINT NULL
  42. #endif /* WOLFSSL_STAIC_MEMORY */
  43. #ifdef WOLFSSL_ASNC_CRYPT
  44. #include <wolfssl/wolfcrypt/async.h>
  45. #endif
  46. #ifdef HAVE_ECC
  47. #include <wolfssl/wolfcrypt/ecc.h> /* wc_ecc_fp_free */
  48. #ifndef ECC_ASN963_MAX_BUF_SZ
  49. #define ECC_ASN963_MAX_BUF_SZ 133
  50. #endif
  51. #ifndef ECC_PRIV_KEY_BUF
  52. #define ECC_PRIV_KEY_BUF 66 /* For non user defined curves. */
  53. #endif
  54. #ifdef HAVE_ALL_CURVES
  55. /* ecc key sizes: 14, 16, 20, 24, 28, 30, 32, 40, 48, 64*/
  56. #ifndef KEY14
  57. #define KEY14 14
  58. #endif
  59. #if !defined(KEY16)
  60. #define KEY16 16
  61. #endif
  62. #if !defined(KEY20)
  63. #define KEY20 20
  64. #endif
  65. #if !defined(KEY24)
  66. #define KEY24 24
  67. #endif
  68. #if !defined(KEY28)
  69. #define KEY28 28
  70. #endif
  71. #if !defined(KEY30)
  72. #define KEY30 30
  73. #endif
  74. #if !defined(KEY32)
  75. #define KEY32 32
  76. #endif
  77. #if !defined(KEY40)
  78. #define KEY40 40
  79. #endif
  80. #if !defined(KEY48)
  81. #define KEY48 48
  82. #endif
  83. #if !defined(KEY64)
  84. #define KEY64 64
  85. #endif
  86. #else
  87. /* ecc key sizes: 14, 16, 20, 24, 28, 30, 32, 40, 48, 64*/
  88. #ifndef KEY14
  89. #define KEY14 32
  90. #endif
  91. #if !defined(KEY16)
  92. #define KEY16 32
  93. #endif
  94. #if !defined(KEY20)
  95. #define KEY20 32
  96. #endif
  97. #if !defined(KEY24)
  98. #define KEY24 32
  99. #endif
  100. #if !defined(KEY28)
  101. #define KEY28 32
  102. #endif
  103. #if !defined(KEY30)
  104. #define KEY30 32
  105. #endif
  106. #if !defined(KEY32)
  107. #define KEY32 32
  108. #endif
  109. #if !defined(KEY40)
  110. #define KEY40 32
  111. #endif
  112. #if !defined(KEY48)
  113. #define KEY48 32
  114. #endif
  115. #if !defined(KEY64)
  116. #define KEY64 32
  117. #endif
  118. #endif
  119. #if !defined(HAVE_COMP_KEY)
  120. #if !defined(NOCOMP)
  121. #define NOCOMP 0
  122. #endif
  123. #else
  124. #if !defined(COMP)
  125. #define COMP 1
  126. #endif
  127. #endif
  128. #if !defined(DER_SZ)
  129. #define DER_SZ (keySz * 2 + 1)
  130. #endif
  131. #endif
  132. #ifndef NO_ASN
  133. #include <wolfssl/wolfcrypt/asn_public.h>
  134. #endif
  135. #include <wolfssl/error-ssl.h>
  136. #include <stdlib.h>
  137. #include <wolfssl/ssl.h> /* compatibility layer */
  138. #include <wolfssl/test.h>
  139. #include <tests/unit.h>
  140. #include "examples/server/server.h"
  141. /* for testing compatibility layer callbacks */
  142. #ifndef NO_MD5
  143. #include <wolfssl/wolfcrypt/md5.h>
  144. #endif
  145. #ifndef NO_SHA
  146. #include <wolfssl/wolfcrypt/sha.h>
  147. #endif
  148. #ifndef NO_SHA256
  149. #include <wolfssl/wolfcrypt/sha256.h>
  150. #endif
  151. #ifdef WOLFSSL_SHA512
  152. #include <wolfssl/wolfcrypt/sha512.h>
  153. #endif
  154. #ifdef WOLFSSL_SHA384
  155. #include <wolfssl/wolfcrypt/sha512.h>
  156. #endif
  157. #ifdef WOLFSSL_SHA3
  158. #include <wolfssl/wolfcrypt/sha3.h>
  159. #ifndef HEAP_HINT
  160. #define HEAP_HINT NULL
  161. #endif
  162. #endif
  163. #ifndef NO_AES
  164. #include <wolfssl/wolfcrypt/aes.h>
  165. #ifdef HAVE_AES_DECRYPT
  166. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  167. #endif
  168. #endif
  169. #ifdef WOLFSSL_RIPEMD
  170. #include <wolfssl/wolfcrypt/ripemd.h>
  171. #endif
  172. #ifdef HAVE_IDEA
  173. #include <wolfssl/wolfcrypt/idea.h>
  174. #endif
  175. #ifndef NO_DES3
  176. #include <wolfssl/wolfcrypt/des3.h>
  177. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  178. #endif
  179. #ifndef NO_HMAC
  180. #include <wolfssl/wolfcrypt/hmac.h>
  181. #endif
  182. #ifdef HAVE_CHACHA
  183. #include <wolfssl/wolfcrypt/chacha.h>
  184. #endif
  185. #ifdef HAVE_POLY1305
  186. #include <wolfssl/wolfcrypt/poly1305.h>
  187. #endif
  188. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  189. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  190. #endif
  191. #ifdef HAVE_CAMELLIA
  192. #include <wolfssl/wolfcrypt/camellia.h>
  193. #endif
  194. #ifndef NO_RABBIT
  195. #include <wolfssl/wolfcrypt/rabbit.h>
  196. #endif
  197. #ifndef NO_RC4
  198. #include <wolfssl/wolfcrypt/arc4.h>
  199. #endif
  200. #ifdef HAVE_BLAKE2
  201. #include <wolfssl/wolfcrypt/blake2.h>
  202. #endif
  203. #ifndef NO_RSA
  204. #include <wolfssl/wolfcrypt/rsa.h>
  205. #include <wolfssl/wolfcrypt/hash.h>
  206. #define FOURK_BUF 4096
  207. #define GEN_BUF 294
  208. #ifndef USER_CRYPTO_ERROR
  209. #define USER_CRYPTO_ERROR -101 /* error returned by IPP lib. */
  210. #endif
  211. #endif
  212. #ifndef NO_SIG_WRAPPER
  213. #include <wolfssl/wolfcrypt/signature.h>
  214. #endif
  215. #ifdef HAVE_AESCCM
  216. #include <wolfssl/wolfcrypt/aes.h>
  217. #endif
  218. #ifdef HAVE_HC128
  219. #include <wolfssl/wolfcrypt/hc128.h>
  220. #endif
  221. #ifdef HAVE_PKCS7
  222. #include <wolfssl/wolfcrypt/pkcs7.h>
  223. #include <wolfssl/wolfcrypt/asn.h>
  224. #endif
  225. #if defined(WOLFSSL_SHA3) || defined(HAVE_PKCS7) || !defined(NO_RSA)
  226. static int devId = INVALID_DEVID;
  227. #endif
  228. #ifndef NO_DSA
  229. #include <wolfssl/wolfcrypt/dsa.h>
  230. #ifndef ONEK_BUF
  231. #define ONEK_BUF 1024
  232. #endif
  233. #ifndef TWOK_BUF
  234. #define TWOK_BUF 2048
  235. #endif
  236. #ifndef FOURK_BUF
  237. #define FOURK_BUF 4096
  238. #endif
  239. #ifndef DSA_SIG_SIZE
  240. #define DSA_SIG_SIZE 40
  241. #endif
  242. #ifndef MAX_DSA_PARAM_SIZE
  243. #define MAX_DSA_PARAM_SIZE 256
  244. #endif
  245. #endif
  246. #ifdef WOLFSSL_CMAC
  247. #include <wolfssl/wolfcrypt/cmac.h>
  248. #endif
  249. #ifdef HAVE_ED25519
  250. #include <wolfssl/wolfcrypt/ed25519.h>
  251. #endif
  252. #ifdef HAVE_CURVE25519
  253. #include <wolfssl/wolfcrypt/curve25519.h>
  254. #endif
  255. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL))
  256. #include <wolfssl/openssl/ssl.h>
  257. #ifndef NO_ASN
  258. /* for ASN_COMMON_NAME DN_tags enum */
  259. #include <wolfssl/wolfcrypt/asn.h>
  260. #endif
  261. #endif
  262. #ifdef OPENSSL_EXTRA
  263. #include <wolfssl/openssl/asn1.h>
  264. #include <wolfssl/openssl/crypto.h>
  265. #include <wolfssl/openssl/pkcs12.h>
  266. #include <wolfssl/openssl/evp.h>
  267. #include <wolfssl/openssl/dh.h>
  268. #include <wolfssl/openssl/bn.h>
  269. #include <wolfssl/openssl/buffer.h>
  270. #include <wolfssl/openssl/pem.h>
  271. #include <wolfssl/openssl/ec.h>
  272. #include <wolfssl/openssl/engine.h>
  273. #include <wolfssl/openssl/crypto.h>
  274. #include <wolfssl/openssl/hmac.h>
  275. #include <wolfssl/openssl/objects.h>
  276. #ifndef NO_AES
  277. #include <wolfssl/openssl/aes.h>
  278. #endif
  279. #ifndef NO_DES3
  280. #include <wolfssl/openssl/des.h>
  281. #endif
  282. #endif /* OPENSSL_EXTRA */
  283. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  284. && !defined(NO_SHA256) && !defined(RC_NO_RNG)
  285. #include <wolfssl/wolfcrypt/srp.h>
  286. #endif
  287. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  288. #include "wolfssl/internal.h" /* for testing SSL_get_peer_cert_chain */
  289. #endif
  290. /* force enable test buffers */
  291. #ifndef USE_CERT_BUFFERS_2048
  292. #define USE_CERT_BUFFERS_2048
  293. #endif
  294. #ifndef USE_CERT_BUFFERS_256
  295. #define USE_CERT_BUFFERS_256
  296. #endif
  297. #include <wolfssl/certs_test.h>
  298. typedef struct testVector {
  299. const char* input;
  300. const char* output;
  301. size_t inLen;
  302. size_t outLen;
  303. } testVector;
  304. #if defined(HAVE_PKCS7)
  305. typedef struct {
  306. const byte* content;
  307. word32 contentSz;
  308. int contentOID;
  309. int encryptOID;
  310. int keyWrapOID;
  311. int keyAgreeOID;
  312. byte* cert;
  313. size_t certSz;
  314. byte* privateKey;
  315. word32 privateKeySz;
  316. } pkcs7EnvelopedVector;
  317. #ifndef NO_PKCS7_ENCRYPTED_DATA
  318. typedef struct {
  319. const byte* content;
  320. word32 contentSz;
  321. int contentOID;
  322. int encryptOID;
  323. byte* encryptionKey;
  324. word32 encryptionKeySz;
  325. } pkcs7EncryptedVector;
  326. #endif
  327. #endif /* HAVE_PKCS7 */
  328. /*----------------------------------------------------------------------------*
  329. | Constants
  330. *----------------------------------------------------------------------------*/
  331. #define TEST_SUCCESS (1)
  332. #define TEST_FAIL (0)
  333. #define testingFmt " %s:"
  334. #define resultFmt " %s\n"
  335. static const char* passed = "passed";
  336. static const char* failed = "failed";
  337. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  338. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  339. static const char* bogusFile =
  340. #ifdef _WIN32
  341. "NUL"
  342. #else
  343. "/dev/null"
  344. #endif
  345. ;
  346. #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
  347. enum {
  348. TESTING_RSA = 1,
  349. TESTING_ECC = 2
  350. };
  351. /*----------------------------------------------------------------------------*
  352. | Setup
  353. *----------------------------------------------------------------------------*/
  354. static int test_wolfSSL_Init(void)
  355. {
  356. int result;
  357. printf(testingFmt, "wolfSSL_Init()");
  358. result = wolfSSL_Init();
  359. printf(resultFmt, result == WOLFSSL_SUCCESS ? passed : failed);
  360. return result;
  361. }
  362. static int test_wolfSSL_Cleanup(void)
  363. {
  364. int result;
  365. printf(testingFmt, "wolfSSL_Cleanup()");
  366. result = wolfSSL_Cleanup();
  367. printf(resultFmt, result == WOLFSSL_SUCCESS ? passed : failed);
  368. return result;
  369. }
  370. /* Initialize the wolfCrypt state.
  371. * POST: 0 success.
  372. */
  373. static int test_wolfCrypt_Init(void)
  374. {
  375. int result;
  376. printf(testingFmt, "wolfCrypt_Init()");
  377. result = wolfCrypt_Init();
  378. printf(resultFmt, result == 0 ? passed : failed);
  379. return result;
  380. } /* END test_wolfCrypt_Init */
  381. /*----------------------------------------------------------------------------*
  382. | Method Allocators
  383. *----------------------------------------------------------------------------*/
  384. static void test_wolfSSL_Method_Allocators(void)
  385. {
  386. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  387. do { \
  388. WOLFSSL_METHOD *method; \
  389. condition(method = allocator()); \
  390. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  391. } while(0)
  392. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  393. TEST_METHOD_ALLOCATOR(a, AssertNotNull)
  394. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  395. TEST_METHOD_ALLOCATOR(a, AssertNull)
  396. #ifndef NO_OLD_TLS
  397. #ifdef WOLFSSL_ALLOW_SSLV3
  398. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  399. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  400. #endif
  401. #ifdef WOLFSL_ALLOW_TLSV10
  402. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  403. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  404. #endif
  405. #ifndef NO_WOLFSSL_SERVER
  406. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  407. #endif
  408. #ifndef NO_WOLFSSL_CLIENT
  409. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  410. #endif
  411. #endif
  412. #ifndef WOLFSSL_NO_TLS12
  413. #ifndef NO_WOLFSSL_SERVER
  414. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  415. #endif
  416. #ifndef NO_WOLFSSL_CLIENT
  417. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  418. #endif
  419. #endif
  420. #ifdef WOLFSSL_TLS13
  421. #ifndef NO_WOLFSSL_SERVER
  422. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  423. #endif
  424. #ifndef NO_WOLFSSL_CLIENT
  425. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  426. #endif
  427. #endif
  428. #ifndef NO_WOLFSSL_SERVER
  429. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  430. #endif
  431. #ifndef NO_WOLFSSL_CLIENT
  432. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  433. #endif
  434. #ifdef WOLFSSL_DTLS
  435. #ifndef NO_OLD_TLS
  436. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  437. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  438. #endif
  439. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  440. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  441. #endif
  442. #ifdef OPENSSL_EXTRA
  443. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  444. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  445. #endif
  446. }
  447. /*----------------------------------------------------------------------------*
  448. | Context
  449. *----------------------------------------------------------------------------*/
  450. #ifndef NO_WOLFSSL_SERVER
  451. static void test_wolfSSL_CTX_new(WOLFSSL_METHOD *method)
  452. {
  453. WOLFSSL_CTX *ctx;
  454. AssertNull(ctx = wolfSSL_CTX_new(NULL));
  455. AssertNotNull(method);
  456. AssertNotNull(ctx = wolfSSL_CTX_new(method));
  457. wolfSSL_CTX_free(ctx);
  458. }
  459. #endif
  460. static void test_wolfSSL_CTX_use_certificate_file(void)
  461. {
  462. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  463. WOLFSSL_CTX *ctx;
  464. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  465. /* invalid context */
  466. AssertFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  467. WOLFSSL_FILETYPE_PEM));
  468. /* invalid cert file */
  469. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  470. WOLFSSL_FILETYPE_PEM));
  471. /* invalid cert type */
  472. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  473. #ifdef NO_RSA
  474. /* rsa needed */
  475. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,WOLFSSL_FILETYPE_PEM));
  476. #else
  477. /* success */
  478. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  479. #endif
  480. wolfSSL_CTX_free(ctx);
  481. #endif
  482. }
  483. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  484. * context using buffer.
  485. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  486. * --enable-testcert flag.
  487. */
  488. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  489. {
  490. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  491. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  492. WOLFSSL_CTX* ctx;
  493. int ret;
  494. printf(testingFmt, "wolfSSL_CTX_use_certificate_buffer()");
  495. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  496. ret = wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  497. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
  498. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  499. wolfSSL_CTX_free(ctx);
  500. return ret;
  501. #else
  502. return WOLFSSL_SUCCESS;
  503. #endif
  504. } /*END test_wolfSSL_CTX_use_certificate_buffer*/
  505. static void test_wolfSSL_CTX_use_PrivateKey_file(void)
  506. {
  507. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  508. WOLFSSL_CTX *ctx;
  509. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  510. /* invalid context */
  511. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  512. WOLFSSL_FILETYPE_PEM));
  513. /* invalid key file */
  514. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  515. WOLFSSL_FILETYPE_PEM));
  516. /* invalid key type */
  517. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  518. /* success */
  519. #ifdef NO_RSA
  520. /* rsa needed */
  521. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  522. #else
  523. /* success */
  524. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  525. #endif
  526. wolfSSL_CTX_free(ctx);
  527. #endif
  528. }
  529. /* test both file and buffer versions along with unloading trusted peer certs */
  530. static void test_wolfSSL_CTX_trust_peer_cert(void)
  531. {
  532. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && !defined(NO_WOLFSSL_CLIENT)
  533. WOLFSSL_CTX *ctx;
  534. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  535. #if !defined(NO_FILESYSTEM)
  536. /* invalid file */
  537. assert(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  538. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS);
  539. assert(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  540. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS);
  541. assert(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  542. WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS);
  543. /* success */
  544. assert(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile, WOLFSSL_FILETYPE_PEM)
  545. == WOLFSSL_SUCCESS);
  546. /* unload cert */
  547. assert(wolfSSL_CTX_Unload_trust_peers(NULL) != WOLFSSL_SUCCESS);
  548. assert(wolfSSL_CTX_Unload_trust_peers(ctx) == WOLFSSL_SUCCESS);
  549. #endif
  550. /* Test of loading certs from buffers */
  551. /* invalid buffer */
  552. assert(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  553. WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS);
  554. /* success */
  555. #ifdef USE_CERT_BUFFERS_1024
  556. assert(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  557. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1) == WOLFSSL_SUCCESS);
  558. #endif
  559. #ifdef USE_CERT_BUFFERS_2048
  560. assert(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  561. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1) == WOLFSSL_SUCCESS);
  562. #endif
  563. /* unload cert */
  564. assert(wolfSSL_CTX_Unload_trust_peers(NULL) != WOLFSSL_SUCCESS);
  565. assert(wolfSSL_CTX_Unload_trust_peers(ctx) == WOLFSSL_SUCCESS);
  566. wolfSSL_CTX_free(ctx);
  567. #endif
  568. }
  569. static void test_wolfSSL_CTX_load_verify_locations(void)
  570. {
  571. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  572. WOLFSSL_CTX *ctx;
  573. WOLFSSL_CERT_MANAGER* cm;
  574. #ifdef PERSIST_CERT_CACHE
  575. int cacheSz;
  576. #endif
  577. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  578. /* invalid context */
  579. AssertFalse(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, 0));
  580. /* invalid ca file */
  581. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx, NULL, 0));
  582. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx, bogusFile, 0));
  583. #ifndef WOLFSSL_TIRTOS
  584. /* invalid path */
  585. /* not working... investigate! */
  586. /* AssertFalse(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, bogusFile)); */
  587. #endif
  588. /* load ca cert */
  589. AssertTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  590. #ifdef PERSIST_CERT_CACHE
  591. /* Get cert cache size */
  592. cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx);
  593. #endif
  594. /* Test unloading CA's */
  595. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UnloadCAs(ctx));
  596. #ifdef PERSIST_CERT_CACHE
  597. /* Verify no certs (result is less than cacheSz) */
  598. AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  599. #endif
  600. /* load ca cert again */
  601. AssertTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  602. /* Test getting CERT_MANAGER */
  603. AssertNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  604. /* Test unloading CA's using CM */
  605. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerUnloadCAs(cm));
  606. #ifdef PERSIST_CERT_CACHE
  607. /* Verify no certs (result is less than cacheSz) */
  608. AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  609. #endif
  610. wolfSSL_CTX_free(ctx);
  611. #endif
  612. }
  613. static void test_wolfSSL_CTX_SetTmpDH_file(void)
  614. {
  615. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  616. !defined(NO_WOLFSSL_CLIENT)
  617. WOLFSSL_CTX *ctx;
  618. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  619. /* invalid context */
  620. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  621. dhParamFile, WOLFSSL_FILETYPE_PEM));
  622. /* invalid dhParamFile file */
  623. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  624. NULL, WOLFSSL_FILETYPE_PEM));
  625. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  626. bogusFile, WOLFSSL_FILETYPE_PEM));
  627. /* success */
  628. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  629. WOLFSSL_FILETYPE_PEM));
  630. wolfSSL_CTX_free(ctx);
  631. #endif
  632. }
  633. static void test_wolfSSL_CTX_SetTmpDH_buffer(void)
  634. {
  635. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_CLIENT)
  636. WOLFSSL_CTX *ctx;
  637. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  638. /* invalid context */
  639. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, dh_key_der_2048,
  640. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  641. /* invalid dhParamFile file */
  642. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  643. 0, WOLFSSL_FILETYPE_ASN1));
  644. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dsa_key_der_2048,
  645. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  646. /* success */
  647. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  648. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  649. wolfSSL_CTX_free(ctx);
  650. #endif
  651. }
  652. /*----------------------------------------------------------------------------*
  653. | SSL
  654. *----------------------------------------------------------------------------*/
  655. static void test_server_wolfSSL_new(void)
  656. {
  657. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  658. !defined(NO_WOLFSSL_SERVER)
  659. WOLFSSL_CTX *ctx;
  660. WOLFSSL_CTX *ctx_nocert;
  661. WOLFSSL *ssl;
  662. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  663. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  664. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  665. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  666. /* invalid context */
  667. AssertNull(ssl = wolfSSL_new(NULL));
  668. #ifndef WOLFSSL_SESSION_EXPORT
  669. AssertNull(ssl = wolfSSL_new(ctx_nocert));
  670. #endif
  671. /* success */
  672. AssertNotNull(ssl = wolfSSL_new(ctx));
  673. wolfSSL_free(ssl);
  674. wolfSSL_CTX_free(ctx);
  675. wolfSSL_CTX_free(ctx_nocert);
  676. #endif
  677. }
  678. static void test_client_wolfSSL_new(void)
  679. {
  680. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  681. !defined(NO_WOLFSSL_CLIENT)
  682. WOLFSSL_CTX *ctx;
  683. WOLFSSL_CTX *ctx_nocert;
  684. WOLFSSL *ssl;
  685. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  686. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  687. AssertTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  688. /* invalid context */
  689. AssertNull(ssl = wolfSSL_new(NULL));
  690. /* success */
  691. AssertNotNull(ssl = wolfSSL_new(ctx_nocert));
  692. wolfSSL_free(ssl);
  693. /* success */
  694. AssertNotNull(ssl = wolfSSL_new(ctx));
  695. wolfSSL_free(ssl);
  696. wolfSSL_CTX_free(ctx);
  697. wolfSSL_CTX_free(ctx_nocert);
  698. #endif
  699. }
  700. static void test_wolfSSL_SetTmpDH_file(void)
  701. {
  702. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  703. !defined(NO_WOLFSSL_SERVER)
  704. WOLFSSL_CTX *ctx;
  705. WOLFSSL *ssl;
  706. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  707. #ifndef NO_RSA
  708. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  709. WOLFSSL_FILETYPE_PEM));
  710. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  711. WOLFSSL_FILETYPE_PEM));
  712. #else
  713. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  714. WOLFSSL_FILETYPE_PEM));
  715. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  716. WOLFSSL_FILETYPE_PEM));
  717. #endif
  718. AssertNotNull(ssl = wolfSSL_new(ctx));
  719. /* invalid ssl */
  720. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  721. dhParamFile, WOLFSSL_FILETYPE_PEM));
  722. /* invalid dhParamFile file */
  723. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  724. NULL, WOLFSSL_FILETYPE_PEM));
  725. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  726. bogusFile, WOLFSSL_FILETYPE_PEM));
  727. /* success */
  728. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  729. WOLFSSL_FILETYPE_PEM));
  730. wolfSSL_free(ssl);
  731. wolfSSL_CTX_free(ctx);
  732. #endif
  733. }
  734. static void test_wolfSSL_SetTmpDH_buffer(void)
  735. {
  736. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  737. WOLFSSL_CTX *ctx;
  738. WOLFSSL *ssl;
  739. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  740. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  741. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  742. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  743. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  744. AssertNotNull(ssl = wolfSSL_new(ctx));
  745. /* invalid ssl */
  746. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  747. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  748. /* invalid dhParamFile file */
  749. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  750. 0, WOLFSSL_FILETYPE_ASN1));
  751. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  752. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  753. /* success */
  754. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  755. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  756. wolfSSL_free(ssl);
  757. wolfSSL_CTX_free(ctx);
  758. #endif
  759. }
  760. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  761. * allowed.
  762. * POST: return 1 on success.
  763. */
  764. static int test_wolfSSL_SetMinVersion(void)
  765. {
  766. int failFlag = WOLFSSL_SUCCESS;
  767. #ifndef NO_WOLFSSL_CLIENT
  768. WOLFSSL_CTX* ctx;
  769. WOLFSSL* ssl;
  770. int itr;
  771. #ifndef NO_OLD_TLS
  772. const int versions[] = { WOLFSSL_TLSV1, WOLFSSL_TLSV1_1,
  773. WOLFSSL_TLSV1_2};
  774. #elif !defined(WOLFSSL_NO_TLS12)
  775. const int versions[] = { WOLFSSL_TLSV1_2 };
  776. #else
  777. const int versions[] = { WOLFSSL_TLSV1_3 };
  778. #endif
  779. AssertTrue(wolfSSL_Init());
  780. #ifndef WOLFSSL_NO_TLS12
  781. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  782. #else
  783. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  784. #endif
  785. ssl = wolfSSL_new(ctx);
  786. printf(testingFmt, "wolfSSL_SetMinVersion()");
  787. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++){
  788. if(wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS){
  789. failFlag = WOLFSSL_FAILURE;
  790. }
  791. }
  792. printf(resultFmt, failFlag == WOLFSSL_SUCCESS ? passed : failed);
  793. wolfSSL_free(ssl);
  794. wolfSSL_CTX_free(ctx);
  795. AssertTrue(wolfSSL_Cleanup());
  796. #endif
  797. return failFlag;
  798. } /* END test_wolfSSL_SetMinVersion */
  799. /*----------------------------------------------------------------------------*
  800. | EC
  801. *----------------------------------------------------------------------------*/
  802. /* Test function for EC_POINT_new, EC_POINT_mul, EC_POINT_free,
  803. EC_GROUP_new_by_curve_name
  804. */
  805. # if defined(OPENSSL_EXTRA)
  806. static void test_wolfSSL_EC(void)
  807. {
  808. #ifdef HAVE_ECC
  809. BN_CTX *ctx;
  810. EC_GROUP *group;
  811. EC_POINT *Gxy, *new_point;
  812. BIGNUM *k = NULL, *Gx = NULL, *Gy = NULL, *Gz = NULL;
  813. BIGNUM *X, *Y;
  814. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) || defined(DEBUG_WOLFSSL)
  815. char* hexStr;
  816. #endif
  817. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD17AF381913FF7A96314EA47055EA0FD0";
  818. /* NISTP256R1 Gx/Gy */
  819. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
  820. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
  821. AssertNotNull(ctx = BN_CTX_new());
  822. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  823. AssertNotNull(Gxy = EC_POINT_new(group));
  824. AssertNotNull(new_point = EC_POINT_new(group));
  825. AssertNotNull(X = BN_new());
  826. AssertNotNull(Y = BN_new());
  827. /* load test values */
  828. AssertIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  829. AssertIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  830. AssertIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  831. AssertIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  832. /* populate coordinates for input point */
  833. Gxy->X = Gx;
  834. Gxy->Y = Gy;
  835. Gxy->Z = Gz;
  836. /* perform point multiplication */
  837. AssertIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), WOLFSSL_SUCCESS);
  838. /* check if point X coordinate is zero */
  839. AssertIntEQ(BN_is_zero(new_point->X), WOLFSSL_FAILURE);
  840. /* extract the coordinates from point */
  841. AssertIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y, ctx), WOLFSSL_SUCCESS);
  842. /* check if point X coordinate is zero */
  843. AssertIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  844. /* check bx2hex */
  845. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) || defined(DEBUG_WOLFSSL)
  846. hexStr = BN_bn2hex(k);
  847. AssertStrEQ(hexStr, kTest);
  848. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  849. hexStr = BN_bn2hex(Gx);
  850. AssertStrEQ(hexStr, kGx);
  851. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  852. hexStr = BN_bn2hex(Gy);
  853. AssertStrEQ(hexStr, kGy);
  854. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  855. #endif
  856. /* cleanup */
  857. BN_free(X);
  858. BN_free(Y);
  859. BN_free(k);
  860. EC_POINT_free(new_point);
  861. EC_POINT_free(Gxy);
  862. EC_GROUP_free(group);
  863. BN_CTX_free(ctx);
  864. #endif /* HAVE_ECC */
  865. }
  866. #endif
  867. #include <wolfssl/openssl/pem.h>
  868. /*----------------------------------------------------------------------------*
  869. | EVP
  870. *----------------------------------------------------------------------------*/
  871. /* Test function for wolfSSL_EVP_get_cipherbynid.
  872. */
  873. # if defined(OPENSSL_EXTRA)
  874. static void test_wolfSSL_EVP_get_cipherbynid(void)
  875. {
  876. #ifndef NO_AES
  877. const WOLFSSL_EVP_CIPHER* c;
  878. c = wolfSSL_EVP_get_cipherbynid(419);
  879. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  880. AssertNotNull(c);
  881. AssertNotNull(strcmp("EVP_AES_128_CBC", c));
  882. #else
  883. AssertNull(c);
  884. #endif
  885. c = wolfSSL_EVP_get_cipherbynid(423);
  886. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_192)
  887. AssertNotNull(c);
  888. AssertNotNull(strcmp("EVP_AES_192_CBC", c));
  889. #else
  890. AssertNull(c);
  891. #endif
  892. c = wolfSSL_EVP_get_cipherbynid(427);
  893. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  894. AssertNotNull(c);
  895. AssertNotNull(strcmp("EVP_AES_256_CBC", c));
  896. #else
  897. AssertNull(c);
  898. #endif
  899. c = wolfSSL_EVP_get_cipherbynid(904);
  900. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  901. AssertNotNull(c);
  902. AssertNotNull(strcmp("EVP_AES_128_CTR", c));
  903. #else
  904. AssertNull(c);
  905. #endif
  906. c = wolfSSL_EVP_get_cipherbynid(905);
  907. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  908. AssertNotNull(c);
  909. AssertNotNull(strcmp("EVP_AES_192_CTR", c));
  910. #else
  911. AssertNull(c);
  912. #endif
  913. c = wolfSSL_EVP_get_cipherbynid(906);
  914. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  915. AssertNotNull(c);
  916. AssertNotNull(strcmp("EVP_AES_256_CTR", c));
  917. #else
  918. AssertNull(c);
  919. #endif
  920. c = wolfSSL_EVP_get_cipherbynid(418);
  921. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  922. AssertNotNull(c);
  923. AssertNotNull(strcmp("EVP_AES_128_ECB", c));
  924. #else
  925. AssertNull(c);
  926. #endif
  927. c = wolfSSL_EVP_get_cipherbynid(422);
  928. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  929. AssertNotNull(c);
  930. AssertNotNull(strcmp("EVP_AES_192_ECB", c));
  931. #else
  932. AssertNull(c);
  933. #endif
  934. c = wolfSSL_EVP_get_cipherbynid(426);
  935. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  936. AssertNotNull(c);
  937. AssertNotNull(strcmp("EVP_AES_256_ECB", c));
  938. #else
  939. AssertNull(c);
  940. #endif
  941. #endif
  942. #ifndef NO_DES3
  943. AssertNotNull(strcmp("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  944. #ifdef WOLFSSL_DES_ECB
  945. AssertNotNull(strcmp("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  946. #endif
  947. AssertNotNull(strcmp("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  948. #ifdef WOLFSSL_DES_ECB
  949. AssertNotNull(strcmp("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  950. #endif
  951. #endif /*NO_DES3*/
  952. #ifdef HAVE_IDEA
  953. AssertNotNull(strcmp("EVP_IDEA_CBC", wolfSSL_EVP_get_cipherbynid(34)));
  954. #endif
  955. /* test for nid is out of range */
  956. AssertNull(wolfSSL_EVP_get_cipherbynid(1));
  957. }
  958. #endif
  959. /*----------------------------------------------------------------------------*
  960. | IO
  961. *----------------------------------------------------------------------------*/
  962. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  963. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  964. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  965. #define HAVE_IO_TESTS_DEPENDENCIES
  966. #endif
  967. /* helper functions */
  968. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  969. #ifdef WOLFSSL_SESSION_EXPORT
  970. /* set up function for sending session information */
  971. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  972. {
  973. WOLFSSL_CTX* ctx;
  974. WOLFSSL* ssl;
  975. AssertNotNull(inSsl);
  976. AssertNotNull(buf);
  977. AssertIntNE(0, sz);
  978. /* Set ctx to DTLS 1.2 */
  979. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  980. AssertNotNull(ctx);
  981. ssl = wolfSSL_new(ctx);
  982. AssertNotNull(ssl);
  983. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  984. wolfSSL_free(ssl);
  985. wolfSSL_CTX_free(ctx);
  986. (void)userCtx;
  987. return WOLFSSL_SUCCESS;
  988. }
  989. #endif
  990. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  991. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  992. {
  993. SOCKET_T sockfd = 0;
  994. SOCKET_T clientfd = 0;
  995. word16 port;
  996. callback_functions* cbf = NULL;
  997. WOLFSSL_METHOD* method = 0;
  998. WOLFSSL_CTX* ctx = 0;
  999. WOLFSSL* ssl = 0;
  1000. char msg[] = "I hear you fa shizzle!";
  1001. char input[1024];
  1002. int idx;
  1003. int ret, err = 0;
  1004. #ifdef WOLFSSL_TIRTOS
  1005. fdOpenSession(Task_self());
  1006. #endif
  1007. ((func_args*)args)->return_code = TEST_FAIL;
  1008. cbf = ((func_args*)args)->callbacks;
  1009. if (cbf != NULL && cbf->method != NULL) {
  1010. method = cbf->method();
  1011. }
  1012. else {
  1013. method = wolfSSLv23_server_method();
  1014. }
  1015. ctx = wolfSSL_CTX_new(method);
  1016. #if defined(USE_WINDOWS_API)
  1017. port = ((func_args*)args)->signal->port;
  1018. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  1019. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  1020. /* Let tcp_listen assign port */
  1021. port = 0;
  1022. #else
  1023. /* Use default port */
  1024. port = wolfSSLPort;
  1025. #endif
  1026. wolfSSL_CTX_set_verify(ctx,
  1027. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  1028. #ifdef WOLFSSL_ENCRYPTED_KEYS
  1029. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  1030. #endif
  1031. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0) != WOLFSSL_SUCCESS)
  1032. {
  1033. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  1034. goto done;
  1035. }
  1036. if (wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM)
  1037. != WOLFSSL_SUCCESS)
  1038. {
  1039. /*err_sys("can't load server cert chain file, "
  1040. "Please run from wolfSSL home dir");*/
  1041. goto done;
  1042. }
  1043. if (wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM)
  1044. != WOLFSSL_SUCCESS)
  1045. {
  1046. /*err_sys("can't load server key file, "
  1047. "Please run from wolfSSL home dir");*/
  1048. goto done;
  1049. }
  1050. /* call ctx setup callback */
  1051. if (cbf != NULL && cbf->ctx_ready != NULL) {
  1052. cbf->ctx_ready(ctx);
  1053. }
  1054. ssl = wolfSSL_new(ctx);
  1055. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1);
  1056. CloseSocket(sockfd);
  1057. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  1058. /*err_sys("SSL_set_fd failed");*/
  1059. goto done;
  1060. }
  1061. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  1062. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  1063. #elif !defined(NO_DH)
  1064. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  1065. #endif
  1066. /* call ssl setup callback */
  1067. if (cbf != NULL && cbf->ssl_ready != NULL) {
  1068. cbf->ssl_ready(ssl);
  1069. }
  1070. do {
  1071. #ifdef WOLFSSL_ASYNC_CRYPT
  1072. if (err == WC_PENDING_E) {
  1073. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  1074. if (ret < 0) { break; } else if (ret == 0) { continue; }
  1075. }
  1076. #endif
  1077. err = 0; /* Reset error */
  1078. ret = wolfSSL_accept(ssl);
  1079. if (ret != WOLFSSL_SUCCESS) {
  1080. err = wolfSSL_get_error(ssl, 0);
  1081. }
  1082. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  1083. if (ret != WOLFSSL_SUCCESS) {
  1084. char buff[WOLFSSL_MAX_ERROR_SZ];
  1085. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  1086. /*err_sys("SSL_accept failed");*/
  1087. goto done;
  1088. }
  1089. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  1090. if (idx > 0) {
  1091. input[idx] = 0;
  1092. printf("Client message: %s\n", input);
  1093. }
  1094. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg))
  1095. {
  1096. /*err_sys("SSL_write failed");*/
  1097. #ifdef WOLFSSL_TIRTOS
  1098. return;
  1099. #else
  1100. return 0;
  1101. #endif
  1102. }
  1103. #ifdef WOLFSSL_TIRTOS
  1104. Task_yield();
  1105. #endif
  1106. ((func_args*)args)->return_code = TEST_SUCCESS;
  1107. done:
  1108. wolfSSL_shutdown(ssl);
  1109. wolfSSL_free(ssl);
  1110. wolfSSL_CTX_free(ctx);
  1111. CloseSocket(clientfd);
  1112. #ifdef WOLFSSL_TIRTOS
  1113. fdCloseSession(Task_self());
  1114. #endif
  1115. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  1116. && defined(HAVE_THREAD_LS)
  1117. wc_ecc_fp_free(); /* free per thread cache */
  1118. #endif
  1119. #ifndef WOLFSSL_TIRTOS
  1120. return 0;
  1121. #endif
  1122. }
  1123. typedef int (*cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  1124. static void test_client_nofail(void* args, void *cb)
  1125. {
  1126. SOCKET_T sockfd = 0;
  1127. callback_functions* cbf = NULL;
  1128. WOLFSSL_METHOD* method = 0;
  1129. WOLFSSL_CTX* ctx = 0;
  1130. WOLFSSL* ssl = 0;
  1131. WOLFSSL_CIPHER* cipher;
  1132. char msg[64] = "hello wolfssl!";
  1133. char reply[1024];
  1134. int input;
  1135. int msgSz = (int)XSTRLEN(msg);
  1136. int ret, err = 0;
  1137. int cipherSuite;
  1138. const char* cipherName1, *cipherName2;
  1139. #ifdef WOLFSSL_TIRTOS
  1140. fdOpenSession(Task_self());
  1141. #endif
  1142. if (((func_args*)args)->callbacks != NULL) {
  1143. cbf = ((func_args*)args)->callbacks;
  1144. }
  1145. ((func_args*)args)->return_code = TEST_FAIL;
  1146. if (cbf != NULL && cbf->method != NULL) {
  1147. method = cbf->method();
  1148. }
  1149. else {
  1150. method = wolfSSLv23_client_method();
  1151. }
  1152. ctx = wolfSSL_CTX_new(method);
  1153. #ifdef WOLFSSL_ENCRYPTED_KEYS
  1154. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  1155. #endif
  1156. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  1157. {
  1158. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  1159. goto done2;
  1160. }
  1161. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, WOLFSSL_FILETYPE_PEM)
  1162. != WOLFSSL_SUCCESS)
  1163. {
  1164. /*err_sys("can't load client cert file, "
  1165. "Please run from wolfSSL home dir");*/
  1166. goto done2;
  1167. }
  1168. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, WOLFSSL_FILETYPE_PEM)
  1169. != WOLFSSL_SUCCESS)
  1170. {
  1171. /*err_sys("can't load client key file, "
  1172. "Please run from wolfSSL home dir");*/
  1173. goto done2;
  1174. }
  1175. /* call ctx setup callback */
  1176. if (cbf != NULL && cbf->ctx_ready != NULL) {
  1177. cbf->ctx_ready(ctx);
  1178. }
  1179. ssl = wolfSSL_new(ctx);
  1180. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  1181. 0, 0, ssl);
  1182. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  1183. /*err_sys("SSL_set_fd failed");*/
  1184. goto done2;
  1185. }
  1186. /* call ssl setup callback */
  1187. if (cbf != NULL && cbf->ssl_ready != NULL) {
  1188. cbf->ssl_ready(ssl);
  1189. }
  1190. do {
  1191. #ifdef WOLFSSL_ASYNC_CRYPT
  1192. if (err == WC_PENDING_E) {
  1193. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  1194. if (ret < 0) { break; } else if (ret == 0) { continue; }
  1195. }
  1196. #endif
  1197. err = 0; /* Reset error */
  1198. ret = wolfSSL_connect(ssl);
  1199. if (ret != WOLFSSL_SUCCESS) {
  1200. err = wolfSSL_get_error(ssl, 0);
  1201. }
  1202. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  1203. if (ret != WOLFSSL_SUCCESS) {
  1204. char buff[WOLFSSL_MAX_ERROR_SZ];
  1205. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  1206. /*err_sys("SSL_connect failed");*/
  1207. goto done2;
  1208. }
  1209. /* test the various get cipher methods */
  1210. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  1211. cipherName1 = wolfSSL_get_cipher_name(ssl);
  1212. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  1213. (cipherSuite >> 8), cipherSuite & 0xFF);
  1214. AssertStrEQ(cipherName1, cipherName2);
  1215. cipher = wolfSSL_get_current_cipher(ssl);
  1216. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  1217. cipherName2 = wolfSSL_get_cipher(ssl);
  1218. #ifdef NO_ERROR_STRINGS
  1219. AssertNull(cipherName1);
  1220. AssertNull(cipherName2);
  1221. #else
  1222. AssertStrEQ(cipherName1, cipherName2);
  1223. #endif
  1224. if(cb != NULL)((cbType)cb)(ctx, ssl);
  1225. if (wolfSSL_write(ssl, msg, msgSz) != msgSz)
  1226. {
  1227. /*err_sys("SSL_write failed");*/
  1228. goto done2;
  1229. }
  1230. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  1231. if (input > 0)
  1232. {
  1233. reply[input] = 0;
  1234. printf("Server response: %s\n", reply);
  1235. }
  1236. ((func_args*)args)->return_code = TEST_SUCCESS;
  1237. done2:
  1238. wolfSSL_free(ssl);
  1239. wolfSSL_CTX_free(ctx);
  1240. CloseSocket(sockfd);
  1241. #ifdef WOLFSSL_TIRTOS
  1242. fdCloseSession(Task_self());
  1243. #endif
  1244. return;
  1245. }
  1246. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  1247. /* SNI / ALPN / session export helper functions */
  1248. #if defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLFSSL_SESSION_EXPORT)
  1249. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  1250. {
  1251. callback_functions* callbacks = ((func_args*)args)->callbacks;
  1252. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
  1253. WOLFSSL* ssl = NULL;
  1254. SOCKET_T sfd = 0;
  1255. SOCKET_T cfd = 0;
  1256. word16 port;
  1257. char msg[] = "I hear you fa shizzle!";
  1258. int len = (int) XSTRLEN(msg);
  1259. char input[1024];
  1260. int idx;
  1261. int ret, err = 0;
  1262. #ifdef WOLFSSL_TIRTOS
  1263. fdOpenSession(Task_self());
  1264. #endif
  1265. ((func_args*)args)->return_code = TEST_FAIL;
  1266. #if defined(USE_WINDOWS_API)
  1267. port = ((func_args*)args)->signal->port;
  1268. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  1269. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  1270. /* Let tcp_listen assign port */
  1271. port = 0;
  1272. #else
  1273. /* Use default port */
  1274. port = wolfSSLPort;
  1275. #endif
  1276. wolfSSL_CTX_set_verify(ctx,
  1277. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  1278. #ifdef WOLFSSL_ENCRYPTED_KEYS
  1279. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  1280. #endif
  1281. #ifdef WOLFSSL_SESSION_EXPORT
  1282. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_dtls_set_export(ctx, test_export));
  1283. #endif
  1284. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  1285. AssertIntEQ(WOLFSSL_SUCCESS,
  1286. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  1287. AssertIntEQ(WOLFSSL_SUCCESS,
  1288. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1289. if (callbacks->ctx_ready)
  1290. callbacks->ctx_ready(ctx);
  1291. ssl = wolfSSL_new(ctx);
  1292. if (wolfSSL_dtls(ssl)) {
  1293. SOCKADDR_IN_T cliAddr;
  1294. socklen_t cliLen;
  1295. cliLen = sizeof(cliAddr);
  1296. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0);
  1297. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  1298. (struct sockaddr*)&cliAddr, &cliLen);
  1299. AssertIntGT(idx, 0);
  1300. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  1301. }
  1302. else {
  1303. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1);
  1304. CloseSocket(sfd);
  1305. }
  1306. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  1307. #ifdef NO_PSK
  1308. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  1309. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  1310. #elif !defined(NO_DH)
  1311. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  1312. #endif
  1313. #endif
  1314. if (callbacks->ssl_ready)
  1315. callbacks->ssl_ready(ssl);
  1316. do {
  1317. #ifdef WOLFSSL_ASYNC_CRYPT
  1318. if (err == WC_PENDING_E) {
  1319. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  1320. if (ret < 0) { break; } else if (ret == 0) { continue; }
  1321. }
  1322. #endif
  1323. err = 0; /* Reset error */
  1324. ret = wolfSSL_accept(ssl);
  1325. if (ret != WOLFSSL_SUCCESS) {
  1326. err = wolfSSL_get_error(ssl, 0);
  1327. }
  1328. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  1329. if (ret != WOLFSSL_SUCCESS) {
  1330. char buff[WOLFSSL_MAX_ERROR_SZ];
  1331. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  1332. /*err_sys("SSL_accept failed");*/
  1333. }
  1334. else {
  1335. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  1336. input[idx] = 0;
  1337. printf("Client message: %s\n", input);
  1338. }
  1339. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  1340. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL)
  1341. if (wolfSSL_dtls(ssl)) {
  1342. byte* import;
  1343. word32 sz;
  1344. wolfSSL_dtls_export(ssl, NULL, &sz);
  1345. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1346. AssertNotNull(import);
  1347. idx = wolfSSL_dtls_export(ssl, import, &sz);
  1348. AssertIntGE(idx, 0);
  1349. AssertIntGE(wolfSSL_dtls_import(ssl, import, idx), 0);
  1350. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1351. }
  1352. #endif
  1353. #ifdef WOLFSSL_TIRTOS
  1354. Task_yield();
  1355. #endif
  1356. ((func_args*)args)->return_code = TEST_SUCCESS;
  1357. }
  1358. if (callbacks->on_result)
  1359. callbacks->on_result(ssl);
  1360. wolfSSL_shutdown(ssl);
  1361. wolfSSL_free(ssl);
  1362. wolfSSL_CTX_free(ctx);
  1363. CloseSocket(cfd);
  1364. #ifdef WOLFSSL_TIRTOS
  1365. fdCloseSession(Task_self());
  1366. #endif
  1367. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  1368. && defined(HAVE_THREAD_LS)
  1369. wc_ecc_fp_free(); /* free per thread cache */
  1370. #endif
  1371. #ifndef WOLFSSL_TIRTOS
  1372. return 0;
  1373. #endif
  1374. }
  1375. static void run_wolfssl_client(void* args)
  1376. {
  1377. callback_functions* callbacks = ((func_args*)args)->callbacks;
  1378. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
  1379. WOLFSSL* ssl = NULL;
  1380. SOCKET_T sfd = 0;
  1381. char msg[] = "hello wolfssl server!";
  1382. int len = (int) XSTRLEN(msg);
  1383. char input[1024];
  1384. int idx;
  1385. int ret, err = 0;
  1386. #ifdef WOLFSSL_TIRTOS
  1387. fdOpenSession(Task_self());
  1388. #endif
  1389. ((func_args*)args)->return_code = TEST_FAIL;
  1390. #ifdef WOLFSSL_ENCRYPTED_KEYS
  1391. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  1392. #endif
  1393. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  1394. AssertIntEQ(WOLFSSL_SUCCESS,
  1395. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, WOLFSSL_FILETYPE_PEM));
  1396. AssertIntEQ(WOLFSSL_SUCCESS,
  1397. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, WOLFSSL_FILETYPE_PEM));
  1398. if (callbacks->ctx_ready)
  1399. callbacks->ctx_ready(ctx);
  1400. ssl = wolfSSL_new(ctx);
  1401. if (wolfSSL_dtls(ssl)) {
  1402. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  1403. 1, 0, ssl);
  1404. }
  1405. else {
  1406. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  1407. 0, 0, ssl);
  1408. }
  1409. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  1410. if (callbacks->ssl_ready)
  1411. callbacks->ssl_ready(ssl);
  1412. do {
  1413. #ifdef WOLFSSL_ASYNC_CRYPT
  1414. if (err == WC_PENDING_E) {
  1415. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  1416. if (ret < 0) { break; } else if (ret == 0) { continue; }
  1417. }
  1418. #endif
  1419. err = 0; /* Reset error */
  1420. ret = wolfSSL_connect(ssl);
  1421. if (ret != WOLFSSL_SUCCESS) {
  1422. err = wolfSSL_get_error(ssl, 0);
  1423. }
  1424. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  1425. if (ret != WOLFSSL_SUCCESS) {
  1426. char buff[WOLFSSL_MAX_ERROR_SZ];
  1427. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  1428. /*err_sys("SSL_connect failed");*/
  1429. }
  1430. else {
  1431. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  1432. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  1433. input[idx] = 0;
  1434. printf("Server response: %s\n", input);
  1435. }
  1436. ((func_args*)args)->return_code = TEST_SUCCESS;
  1437. }
  1438. if (callbacks->on_result)
  1439. callbacks->on_result(ssl);
  1440. wolfSSL_free(ssl);
  1441. wolfSSL_CTX_free(ctx);
  1442. CloseSocket(sfd);
  1443. #ifdef WOLFSSL_TIRTOS
  1444. fdCloseSession(Task_self());
  1445. #endif
  1446. }
  1447. #endif /* defined(HAVE_SNI) || defined(HAVE_ALPN) ||
  1448. defined(WOLFSSL_SESSION_EXPORT) */
  1449. #endif /* io tests dependencies */
  1450. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  1451. static void test_wolfSSL_read_write(void)
  1452. {
  1453. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  1454. /* The unit testing for read and write shall happen simutaneously, since
  1455. * one can't do anything with one without the other. (Except for a failure
  1456. * test case.) This function will call all the others that will set up,
  1457. * execute, and report their test findings.
  1458. *
  1459. * Set up the success case first. This function will become the template
  1460. * for the other tests. This should eventually be renamed
  1461. *
  1462. * The success case isn't interesting, how can this fail?
  1463. * - Do not give the client context a CA certificate. The connect should
  1464. * fail. Do not need server for this?
  1465. * - Using NULL for the ssl object on server. Do not need client for this.
  1466. * - Using NULL for the ssl object on client. Do not need server for this.
  1467. * - Good ssl objects for client and server. Client write() without server
  1468. * read().
  1469. * - Good ssl objects for client and server. Server write() without client
  1470. * read().
  1471. * - Forgetting the password callback?
  1472. */
  1473. tcp_ready ready;
  1474. func_args client_args;
  1475. func_args server_args;
  1476. THREAD_TYPE serverThread;
  1477. XMEMSET(&client_args, 0, sizeof(func_args));
  1478. XMEMSET(&server_args, 0, sizeof(func_args));
  1479. #ifdef WOLFSSL_TIRTOS
  1480. fdOpenSession(Task_self());
  1481. #endif
  1482. StartTCP();
  1483. InitTcpReady(&ready);
  1484. #if defined(USE_WINDOWS_API)
  1485. /* use RNG to get random port if using windows */
  1486. ready.port = GetRandomPort();
  1487. #endif
  1488. server_args.signal = &ready;
  1489. client_args.signal = &ready;
  1490. start_thread(test_server_nofail, &server_args, &serverThread);
  1491. wait_tcp_ready(&server_args);
  1492. test_client_nofail(&client_args, NULL);
  1493. join_thread(serverThread);
  1494. AssertTrue(client_args.return_code);
  1495. AssertTrue(server_args.return_code);
  1496. FreeTcpReady(&ready);
  1497. #ifdef WOLFSSL_TIRTOS
  1498. fdOpenSession(Task_self());
  1499. #endif
  1500. #endif
  1501. }
  1502. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  1503. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) && \
  1504. defined(WOLFSSL_SESSION_EXPORT)
  1505. /* canned export of a session using older version 3 */
  1506. static unsigned char version_3[] = {
  1507. 0xA5, 0xA3, 0x01, 0x87, 0x00, 0x39, 0x00, 0x01,
  1508. 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80,
  1509. 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  1510. 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  1511. 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
  1512. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1513. 0x00, 0x00, 0x00, 0xC0, 0x30, 0x05, 0x09, 0x0A,
  1514. 0x01, 0x01, 0x00, 0x0D, 0x05, 0xFE, 0xFD, 0x01,
  1515. 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1516. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1517. 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  1518. 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  1519. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
  1520. 0x05, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
  1521. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
  1522. 0x01, 0x00, 0x07, 0x00, 0x00, 0x00, 0x30, 0x00,
  1523. 0x00, 0x00, 0x10, 0x01, 0x01, 0x00, 0x02, 0x00,
  1524. 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  1525. 0x02, 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00,
  1526. 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00,
  1527. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1528. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1529. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1530. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1531. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1532. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1533. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1534. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1535. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1536. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1537. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1538. 0x00, 0x00, 0x00, 0x20, 0x05, 0x12, 0xCF, 0x22,
  1539. 0xA1, 0x9F, 0x1C, 0x39, 0x1D, 0x31, 0x11, 0x12,
  1540. 0x1D, 0x11, 0x18, 0x0D, 0x0B, 0xF3, 0xE1, 0x4D,
  1541. 0xDC, 0xB1, 0xF1, 0x39, 0x98, 0x91, 0x6C, 0x48,
  1542. 0xE5, 0xED, 0x11, 0x12, 0xA0, 0x00, 0xF2, 0x25,
  1543. 0x4C, 0x09, 0x26, 0xD1, 0x74, 0xDF, 0x23, 0x40,
  1544. 0x15, 0x6A, 0x42, 0x2A, 0x26, 0xA5, 0xAC, 0x56,
  1545. 0xD5, 0x4A, 0x20, 0xB7, 0xE9, 0xEF, 0xEB, 0xAF,
  1546. 0xA8, 0x1E, 0x23, 0x7C, 0x04, 0xAA, 0xA1, 0x6D,
  1547. 0x92, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  1548. 0x00, 0x00, 0x00, 0x00, 0x01, 0x0C, 0x79, 0x7B,
  1549. 0xFA, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1550. 0x00, 0x00, 0xAA, 0xA1, 0x6D, 0x92, 0x00, 0x00,
  1551. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  1552. 0x00, 0x20, 0x00, 0x04, 0x00, 0x10, 0x00, 0x10,
  1553. 0x08, 0x02, 0x05, 0x08, 0x01, 0x30, 0x28, 0x00,
  1554. 0x00, 0x0F, 0x00, 0x02, 0x00, 0x09, 0x31, 0x32,
  1555. 0x37, 0x2E, 0x30, 0x2E, 0x30, 0x2E, 0x31, 0xED,
  1556. 0x4F
  1557. };
  1558. #endif /* defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) && \
  1559. defined(WOLFSSL_SESSION_EXPORT) */
  1560. static void test_wolfSSL_dtls_export(void)
  1561. {
  1562. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) && \
  1563. defined(WOLFSSL_SESSION_EXPORT)
  1564. tcp_ready ready;
  1565. func_args client_args;
  1566. func_args server_args;
  1567. THREAD_TYPE serverThread;
  1568. callback_functions server_cbf;
  1569. callback_functions client_cbf;
  1570. #ifdef WOLFSSL_TIRTOS
  1571. fdOpenSession(Task_self());
  1572. #endif
  1573. InitTcpReady(&ready);
  1574. #if defined(USE_WINDOWS_API)
  1575. /* use RNG to get random port if using windows */
  1576. ready.port = GetRandomPort();
  1577. #endif
  1578. /* set using dtls */
  1579. XMEMSET(&client_args, 0, sizeof(func_args));
  1580. XMEMSET(&server_args, 0, sizeof(func_args));
  1581. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  1582. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  1583. server_cbf.method = wolfDTLSv1_2_server_method;
  1584. client_cbf.method = wolfDTLSv1_2_client_method;
  1585. server_args.callbacks = &server_cbf;
  1586. client_args.callbacks = &client_cbf;
  1587. server_args.signal = &ready;
  1588. client_args.signal = &ready;
  1589. start_thread(run_wolfssl_server, &server_args, &serverThread);
  1590. wait_tcp_ready(&server_args);
  1591. run_wolfssl_client(&client_args);
  1592. join_thread(serverThread);
  1593. AssertTrue(client_args.return_code);
  1594. AssertTrue(server_args.return_code);
  1595. FreeTcpReady(&ready);
  1596. #ifdef WOLFSSL_TIRTOS
  1597. fdOpenSession(Task_self());
  1598. #endif
  1599. {
  1600. WOLFSSL_CTX* ctx;
  1601. WOLFSSL* ssl;
  1602. /* Set ctx to DTLS 1.2 */
  1603. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  1604. AssertNotNull(ssl = wolfSSL_new(ctx));
  1605. /* test importing version 3 */
  1606. AssertIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  1607. /* test importing bad length and bad version */
  1608. version_3[2] += 1;
  1609. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  1610. version_3[2] -= 1; version_3[1] = 0XA0;
  1611. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  1612. wolfSSL_free(ssl);
  1613. wolfSSL_CTX_free(ctx);
  1614. }
  1615. printf(testingFmt, "wolfSSL_dtls_export()");
  1616. printf(resultFmt, passed);
  1617. #endif
  1618. }
  1619. /*----------------------------------------------------------------------------*
  1620. | TLS extensions tests
  1621. *----------------------------------------------------------------------------*/
  1622. #if defined(HAVE_SNI) || defined(HAVE_ALPN)
  1623. /* connection test runner */
  1624. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  1625. callback_functions* server_callbacks)
  1626. {
  1627. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  1628. tcp_ready ready;
  1629. func_args client_args;
  1630. func_args server_args;
  1631. THREAD_TYPE serverThread;
  1632. XMEMSET(&client_args, 0, sizeof(func_args));
  1633. XMEMSET(&server_args, 0, sizeof(func_args));
  1634. StartTCP();
  1635. client_args.callbacks = client_callbacks;
  1636. server_args.callbacks = server_callbacks;
  1637. #ifdef WOLFSSL_TIRTOS
  1638. fdOpenSession(Task_self());
  1639. #endif
  1640. /* RUN Server side */
  1641. InitTcpReady(&ready);
  1642. #if defined(USE_WINDOWS_API)
  1643. /* use RNG to get random port if using windows */
  1644. ready.port = GetRandomPort();
  1645. #endif
  1646. server_args.signal = &ready;
  1647. client_args.signal = &ready;
  1648. start_thread(run_wolfssl_server, &server_args, &serverThread);
  1649. wait_tcp_ready(&server_args);
  1650. /* RUN Client side */
  1651. run_wolfssl_client(&client_args);
  1652. join_thread(serverThread);
  1653. FreeTcpReady(&ready);
  1654. #ifdef WOLFSSL_TIRTOS
  1655. fdCloseSession(Task_self());
  1656. #endif
  1657. #else
  1658. (void)client_callbacks;
  1659. (void)server_callbacks;
  1660. #endif
  1661. }
  1662. #endif /* defined(HAVE_SNI) || defined(HAVE_ALPN) */
  1663. #ifdef HAVE_SNI
  1664. static void test_wolfSSL_UseSNI_params(void)
  1665. {
  1666. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1667. WOLFSSL *ssl = wolfSSL_new(ctx);
  1668. AssertNotNull(ctx);
  1669. AssertNotNull(ssl);
  1670. /* invalid [ctx|ssl] */
  1671. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  1672. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  1673. /* invalid type */
  1674. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  1675. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  1676. /* invalid data */
  1677. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  1678. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  1679. /* success case */
  1680. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  1681. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  1682. wolfSSL_free(ssl);
  1683. wolfSSL_CTX_free(ctx);
  1684. }
  1685. /* BEGIN of connection tests callbacks */
  1686. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  1687. {
  1688. AssertIntEQ(WOLFSSL_SUCCESS,
  1689. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  1690. }
  1691. static void use_SNI_at_ssl(WOLFSSL* ssl)
  1692. {
  1693. AssertIntEQ(WOLFSSL_SUCCESS,
  1694. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  1695. }
  1696. static void different_SNI_at_ssl(WOLFSSL* ssl)
  1697. {
  1698. AssertIntEQ(WOLFSSL_SUCCESS,
  1699. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  1700. }
  1701. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  1702. {
  1703. use_SNI_at_ssl(ssl);
  1704. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  1705. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  1706. }
  1707. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  1708. {
  1709. use_SNI_at_ssl(ssl);
  1710. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  1711. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  1712. }
  1713. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  1714. {
  1715. use_SNI_at_ctx(ctx);
  1716. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  1717. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  1718. }
  1719. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  1720. {
  1721. use_SNI_at_ssl(ssl);
  1722. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  1723. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  1724. }
  1725. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  1726. {
  1727. use_SNI_at_ctx(ctx);
  1728. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  1729. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  1730. }
  1731. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  1732. {
  1733. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  1734. }
  1735. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  1736. {
  1737. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  1738. }
  1739. static void verify_SNI_no_matching(WOLFSSL* ssl)
  1740. {
  1741. byte type = WOLFSSL_SNI_HOST_NAME;
  1742. char* request = (char*) &type; /* to be overwriten */
  1743. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  1744. AssertNotNull(request);
  1745. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  1746. AssertNull(request);
  1747. }
  1748. static void verify_SNI_real_matching(WOLFSSL* ssl)
  1749. {
  1750. byte type = WOLFSSL_SNI_HOST_NAME;
  1751. char* request = NULL;
  1752. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  1753. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  1754. AssertNotNull(request);
  1755. AssertStrEQ("www.wolfssl.com", request);
  1756. }
  1757. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  1758. {
  1759. byte type = WOLFSSL_SNI_HOST_NAME;
  1760. char* request = NULL;
  1761. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  1762. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  1763. AssertNotNull(request);
  1764. AssertStrEQ("ww2.wolfssl.com", request);
  1765. }
  1766. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  1767. {
  1768. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  1769. }
  1770. /* END of connection tests callbacks */
  1771. static void test_wolfSSL_UseSNI_connection(void)
  1772. {
  1773. unsigned long i;
  1774. callback_functions callbacks[] = {
  1775. /* success case at ctx */
  1776. {0, use_SNI_at_ctx, 0, 0},
  1777. {0, use_SNI_at_ctx, 0, verify_SNI_real_matching},
  1778. /* success case at ssl */
  1779. {0, 0, use_SNI_at_ssl, verify_SNI_real_matching},
  1780. {0, 0, use_SNI_at_ssl, verify_SNI_real_matching},
  1781. /* default missmatch behavior */
  1782. {0, 0, different_SNI_at_ssl, verify_FATAL_ERROR_on_client},
  1783. {0, 0, use_SNI_at_ssl, verify_UNKNOWN_SNI_on_server},
  1784. /* continue on missmatch */
  1785. {0, 0, different_SNI_at_ssl, 0},
  1786. {0, 0, use_SNI_WITH_CONTINUE_at_ssl, verify_SNI_no_matching},
  1787. /* fake answer on missmatch */
  1788. {0, 0, different_SNI_at_ssl, 0},
  1789. {0, 0, use_SNI_WITH_FAKE_ANSWER_at_ssl, verify_SNI_fake_matching},
  1790. /* sni abort - success */
  1791. {0, use_SNI_at_ctx, 0, 0},
  1792. {0, use_MANDATORY_SNI_at_ctx, 0, verify_SNI_real_matching},
  1793. /* sni abort - abort when absent (ctx) */
  1794. {0, 0, 0, verify_FATAL_ERROR_on_client},
  1795. {0, use_MANDATORY_SNI_at_ctx, 0, verify_SNI_ABSENT_on_server},
  1796. /* sni abort - abort when absent (ssl) */
  1797. {0, 0, 0, verify_FATAL_ERROR_on_client},
  1798. {0, 0, use_MANDATORY_SNI_at_ssl, verify_SNI_ABSENT_on_server},
  1799. /* sni abort - success when overwriten */
  1800. {0, 0, 0, 0},
  1801. {0, use_MANDATORY_SNI_at_ctx, use_SNI_at_ssl, verify_SNI_no_matching},
  1802. /* sni abort - success when allowing missmatches */
  1803. {0, 0, different_SNI_at_ssl, 0},
  1804. {0, use_PSEUDO_MANDATORY_SNI_at_ctx, 0, verify_SNI_fake_matching},
  1805. };
  1806. for (i = 0; i < sizeof(callbacks) / sizeof(callback_functions); i += 2) {
  1807. callbacks[i ].method = wolfSSLv23_client_method;
  1808. callbacks[i + 1].method = wolfSSLv23_server_method;
  1809. test_wolfSSL_client_server(&callbacks[i], &callbacks[i + 1]);
  1810. }
  1811. }
  1812. static void test_wolfSSL_SNI_GetFromBuffer(void)
  1813. {
  1814. byte buffer[] = { /* www.paypal.com */
  1815. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  1816. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  1817. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  1818. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  1819. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  1820. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  1821. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  1822. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  1823. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  1824. };
  1825. byte buffer2[] = { /* api.textmate.org */
  1826. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  1827. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  1828. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  1829. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  1830. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  1831. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  1832. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  1833. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  1834. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  1835. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  1836. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  1837. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  1838. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  1839. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  1840. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  1841. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  1842. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  1843. };
  1844. byte buffer3[] = { /* no sni extension */
  1845. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  1846. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  1847. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  1848. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  1849. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  1850. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  1851. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  1852. };
  1853. byte buffer4[] = { /* last extension has zero size */
  1854. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  1855. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  1856. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  1857. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  1858. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  1859. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  1860. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  1861. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  1862. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  1863. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  1864. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  1865. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  1866. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  1867. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  1868. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  1869. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  1870. 0x12, 0x00, 0x00
  1871. };
  1872. byte buffer5[] = { /* SSL v2.0 client hello */
  1873. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  1874. /* dummy bytes bellow, just to pass size check */
  1875. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  1876. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  1877. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  1878. };
  1879. byte result[32] = {0};
  1880. word32 length = 32;
  1881. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buffer4, sizeof(buffer4),
  1882. 0, result, &length));
  1883. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buffer3, sizeof(buffer3),
  1884. 0, result, &length));
  1885. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buffer2, sizeof(buffer2),
  1886. 1, result, &length));
  1887. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buffer, sizeof(buffer),
  1888. 0, result, &length));
  1889. buffer[0] = 0x16;
  1890. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buffer, sizeof(buffer),
  1891. 0, result, &length));
  1892. buffer[1] = 0x03;
  1893. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buffer,
  1894. sizeof(buffer), 0, result, &length));
  1895. buffer[2] = 0x03;
  1896. AssertIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buffer,
  1897. sizeof(buffer), 0, result, &length));
  1898. buffer[4] = 0x64;
  1899. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buffer, sizeof(buffer),
  1900. 0, result, &length));
  1901. result[length] = 0;
  1902. AssertStrEQ("www.paypal.com", (const char*) result);
  1903. length = 32;
  1904. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buffer2, sizeof(buffer2),
  1905. 0, result, &length));
  1906. result[length] = 0;
  1907. AssertStrEQ("api.textmate.org", (const char*) result);
  1908. /* SSL v2.0 tests */
  1909. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buffer5,
  1910. sizeof(buffer5), 0, result, &length));
  1911. buffer5[2] = 0x02;
  1912. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buffer5,
  1913. sizeof(buffer5), 0, result, &length));
  1914. buffer5[2] = 0x01; buffer5[6] = 0x08;
  1915. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buffer5,
  1916. sizeof(buffer5), 0, result, &length));
  1917. buffer5[6] = 0x09; buffer5[8] = 0x01;
  1918. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buffer5,
  1919. sizeof(buffer5), 0, result, &length));
  1920. }
  1921. #endif /* HAVE_SNI */
  1922. static void test_wolfSSL_UseSNI(void)
  1923. {
  1924. #ifdef HAVE_SNI
  1925. test_wolfSSL_UseSNI_params();
  1926. test_wolfSSL_UseSNI_connection();
  1927. test_wolfSSL_SNI_GetFromBuffer();
  1928. #endif
  1929. }
  1930. static void test_wolfSSL_UseMaxFragment(void)
  1931. {
  1932. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_WOLFSSL_CLIENT)
  1933. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1934. WOLFSSL *ssl = wolfSSL_new(ctx);
  1935. AssertNotNull(ctx);
  1936. AssertNotNull(ssl);
  1937. /* error cases */
  1938. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  1939. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  1940. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, 0));
  1941. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, 6));
  1942. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment(ssl, 0));
  1943. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment(ssl, 6));
  1944. /* success case */
  1945. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  1946. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  1947. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  1948. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  1949. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  1950. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  1951. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  1952. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  1953. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  1954. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  1955. wolfSSL_free(ssl);
  1956. wolfSSL_CTX_free(ctx);
  1957. #endif
  1958. }
  1959. static void test_wolfSSL_UseTruncatedHMAC(void)
  1960. {
  1961. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_WOLFSSL_CLIENT)
  1962. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1963. WOLFSSL *ssl = wolfSSL_new(ctx);
  1964. AssertNotNull(ctx);
  1965. AssertNotNull(ssl);
  1966. /* error cases */
  1967. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  1968. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  1969. /* success case */
  1970. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  1971. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  1972. wolfSSL_free(ssl);
  1973. wolfSSL_CTX_free(ctx);
  1974. #endif
  1975. }
  1976. static void test_wolfSSL_UseSupportedCurve(void)
  1977. {
  1978. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT)
  1979. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1980. WOLFSSL *ssl = wolfSSL_new(ctx);
  1981. AssertNotNull(ctx);
  1982. AssertNotNull(ssl);
  1983. /* error cases */
  1984. AssertIntNE(WOLFSSL_SUCCESS,
  1985. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  1986. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  1987. AssertIntNE(WOLFSSL_SUCCESS,
  1988. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  1989. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  1990. /* success case */
  1991. AssertIntEQ(WOLFSSL_SUCCESS,
  1992. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  1993. AssertIntEQ(WOLFSSL_SUCCESS,
  1994. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  1995. wolfSSL_free(ssl);
  1996. wolfSSL_CTX_free(ctx);
  1997. #endif
  1998. }
  1999. #ifdef HAVE_ALPN
  2000. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  2001. {
  2002. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  2003. }
  2004. static void use_ALPN_all(WOLFSSL* ssl)
  2005. {
  2006. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  2007. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  2008. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  2009. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  2010. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  2011. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  2012. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  2013. }
  2014. static void use_ALPN_all_continue(WOLFSSL* ssl)
  2015. {
  2016. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  2017. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  2018. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  2019. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  2020. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  2021. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  2022. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  2023. }
  2024. static void use_ALPN_one(WOLFSSL* ssl)
  2025. {
  2026. /* spdy/2 */
  2027. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  2028. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  2029. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  2030. }
  2031. static void use_ALPN_unknown(WOLFSSL* ssl)
  2032. {
  2033. /* http/2.0 */
  2034. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  2035. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  2036. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  2037. }
  2038. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  2039. {
  2040. /* http/2.0 */
  2041. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  2042. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  2043. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  2044. }
  2045. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  2046. {
  2047. /* spdy/3 */
  2048. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  2049. char *proto;
  2050. word16 protoSz = 0;
  2051. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  2052. /* check value */
  2053. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  2054. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  2055. }
  2056. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  2057. {
  2058. char *proto = NULL;
  2059. word16 protoSz = 0;
  2060. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  2061. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  2062. /* check value */
  2063. AssertIntEQ(1, (0 == protoSz));
  2064. AssertIntEQ(1, (NULL == proto));
  2065. }
  2066. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  2067. {
  2068. /* http/1.1 */
  2069. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  2070. char *proto;
  2071. word16 protoSz = 0;
  2072. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  2073. /* check value */
  2074. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  2075. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  2076. }
  2077. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  2078. {
  2079. /* spdy/2 */
  2080. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  2081. char *proto;
  2082. word16 protoSz = 0;
  2083. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  2084. /* check value */
  2085. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  2086. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  2087. }
  2088. static void verify_ALPN_client_list(WOLFSSL* ssl)
  2089. {
  2090. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  2091. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  2092. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  2093. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  2094. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  2095. char *clist = NULL;
  2096. word16 clistSz = 0;
  2097. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  2098. &clistSz));
  2099. /* check value */
  2100. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  2101. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  2102. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  2103. }
  2104. static void test_wolfSSL_UseALPN_connection(void)
  2105. {
  2106. unsigned long i;
  2107. callback_functions callbacks[] = {
  2108. /* success case same list */
  2109. {0, 0, use_ALPN_all, 0},
  2110. {0, 0, use_ALPN_all, verify_ALPN_matching_http1},
  2111. /* success case only one for server */
  2112. {0, 0, use_ALPN_all, 0},
  2113. {0, 0, use_ALPN_one, verify_ALPN_matching_spdy2},
  2114. /* success case only one for client */
  2115. {0, 0, use_ALPN_one, 0},
  2116. {0, 0, use_ALPN_all, verify_ALPN_matching_spdy2},
  2117. /* success case none for client */
  2118. {0, 0, 0, 0},
  2119. {0, 0, use_ALPN_all, 0},
  2120. /* success case missmatch behavior but option 'continue' set */
  2121. {0, 0, use_ALPN_all_continue, verify_ALPN_not_matching_continue},
  2122. {0, 0, use_ALPN_unknown_continue, 0},
  2123. /* success case read protocol send by client */
  2124. {0, 0, use_ALPN_all, 0},
  2125. {0, 0, use_ALPN_one, verify_ALPN_client_list},
  2126. /* missmatch behavior with same list
  2127. * the first and only this one must be taken */
  2128. {0, 0, use_ALPN_all, 0},
  2129. {0, 0, use_ALPN_all, verify_ALPN_not_matching_spdy3},
  2130. /* default missmatch behavior */
  2131. {0, 0, use_ALPN_all, 0},
  2132. {0, 0, use_ALPN_unknown, verify_ALPN_FATAL_ERROR_on_client},
  2133. };
  2134. for (i = 0; i < sizeof(callbacks) / sizeof(callback_functions); i += 2) {
  2135. callbacks[i ].method = wolfSSLv23_client_method;
  2136. callbacks[i + 1].method = wolfSSLv23_server_method;
  2137. test_wolfSSL_client_server(&callbacks[i], &callbacks[i + 1]);
  2138. }
  2139. }
  2140. static void test_wolfSSL_UseALPN_params(void)
  2141. {
  2142. #ifndef NO_WOLFSSL_CLIENT
  2143. /* "http/1.1" */
  2144. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  2145. /* "spdy/1" */
  2146. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  2147. /* "spdy/2" */
  2148. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  2149. /* "spdy/3" */
  2150. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  2151. char buff[256];
  2152. word32 idx;
  2153. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2154. WOLFSSL *ssl = wolfSSL_new(ctx);
  2155. AssertNotNull(ctx);
  2156. AssertNotNull(ssl);
  2157. /* error cases */
  2158. AssertIntNE(WOLFSSL_SUCCESS,
  2159. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  2160. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  2161. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  2162. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  2163. /* success case */
  2164. /* http1 only */
  2165. AssertIntEQ(WOLFSSL_SUCCESS,
  2166. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  2167. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  2168. /* http1, spdy1 */
  2169. XMEMCPY(buff, http1, sizeof(http1));
  2170. idx = sizeof(http1);
  2171. buff[idx++] = ',';
  2172. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  2173. idx += sizeof(spdy1);
  2174. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  2175. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  2176. /* http1, spdy2, spdy1 */
  2177. XMEMCPY(buff, http1, sizeof(http1));
  2178. idx = sizeof(http1);
  2179. buff[idx++] = ',';
  2180. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  2181. idx += sizeof(spdy2);
  2182. buff[idx++] = ',';
  2183. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  2184. idx += sizeof(spdy1);
  2185. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  2186. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  2187. /* spdy3, http1, spdy2, spdy1 */
  2188. XMEMCPY(buff, spdy3, sizeof(spdy3));
  2189. idx = sizeof(spdy3);
  2190. buff[idx++] = ',';
  2191. XMEMCPY(buff+idx, http1, sizeof(http1));
  2192. idx += sizeof(http1);
  2193. buff[idx++] = ',';
  2194. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  2195. idx += sizeof(spdy2);
  2196. buff[idx++] = ',';
  2197. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  2198. idx += sizeof(spdy1);
  2199. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  2200. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  2201. wolfSSL_free(ssl);
  2202. wolfSSL_CTX_free(ctx);
  2203. #endif
  2204. }
  2205. #endif /* HAVE_ALPN */
  2206. static void test_wolfSSL_UseALPN(void)
  2207. {
  2208. #ifdef HAVE_ALPN
  2209. test_wolfSSL_UseALPN_connection();
  2210. test_wolfSSL_UseALPN_params();
  2211. #endif
  2212. }
  2213. static void test_wolfSSL_DisableExtendedMasterSecret(void)
  2214. {
  2215. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  2216. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2217. WOLFSSL *ssl = wolfSSL_new(ctx);
  2218. AssertNotNull(ctx);
  2219. AssertNotNull(ssl);
  2220. /* error cases */
  2221. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  2222. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  2223. /* success cases */
  2224. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  2225. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  2226. wolfSSL_free(ssl);
  2227. wolfSSL_CTX_free(ctx);
  2228. #endif
  2229. }
  2230. /*----------------------------------------------------------------------------*
  2231. | X509 Tests
  2232. *----------------------------------------------------------------------------*/
  2233. static void test_wolfSSL_X509_NAME_get_entry(void)
  2234. {
  2235. #if !defined(NO_CERTS) && !defined(NO_RSA)
  2236. #if defined(OPENSSL_ALL) || \
  2237. (defined(OPENSSL_EXTRA) && \
  2238. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  2239. printf(testingFmt, "wolfSSL_X509_NAME_get_entry()");
  2240. {
  2241. /* use openssl like name to test mapping */
  2242. X509_NAME_ENTRY* ne = NULL;
  2243. X509_NAME* name = NULL;
  2244. char* subCN = NULL;
  2245. X509* x509;
  2246. ASN1_STRING* asn;
  2247. int idx;
  2248. #ifndef NO_FILESYSTEM
  2249. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  2250. AssertNotNull(x509);
  2251. name = X509_get_subject_name(x509);
  2252. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  2253. AssertIntGE(idx, 0);
  2254. ne = X509_NAME_get_entry(name, idx);
  2255. AssertNotNull(ne);
  2256. asn = X509_NAME_ENTRY_get_data(ne);
  2257. AssertNotNull(asn);
  2258. subCN = (char*)ASN1_STRING_data(asn);
  2259. AssertNotNull(subCN);
  2260. wolfSSL_FreeX509(x509);
  2261. #endif
  2262. }
  2263. printf(resultFmt, passed);
  2264. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  2265. #endif /* !NO_CERTS && !NO_RSA */
  2266. }
  2267. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  2268. static void test_wolfSSL_PKCS12(void)
  2269. {
  2270. /* .p12 file is encrypted with DES3 */
  2271. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  2272. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA)
  2273. byte buffer[5300];
  2274. char file[] = "./certs/test-servercert.p12";
  2275. char order[] = "./certs/ecc-rsa-server.p12";
  2276. char pass[] = "a password";
  2277. WOLFSSL_X509_NAME* subject;
  2278. FILE *f;
  2279. int bytes, ret;
  2280. WOLFSSL_BIO *bio;
  2281. WOLFSSL_EVP_PKEY *pkey;
  2282. WC_PKCS12 *pkcs12;
  2283. WC_PKCS12 *pkcs12_2;
  2284. WOLFSSL_X509 *cert;
  2285. WOLFSSL_X509 *x509;
  2286. WOLFSSL_X509 *tmp;
  2287. WOLF_STACK_OF(WOLFSSL_X509) *ca;
  2288. printf(testingFmt, "wolfSSL_PKCS12()");
  2289. f = fopen(file, "rb");
  2290. AssertNotNull(f);
  2291. bytes = (int)fread(buffer, 1, sizeof(buffer), f);
  2292. fclose(f);
  2293. bio = BIO_new_mem_buf((void*)buffer, bytes);
  2294. AssertNotNull(bio);
  2295. pkcs12 = d2i_PKCS12_bio(bio, NULL);
  2296. AssertNotNull(pkcs12);
  2297. PKCS12_free(pkcs12);
  2298. d2i_PKCS12_bio(bio, &pkcs12);
  2299. AssertNotNull(pkcs12);
  2300. /* check verify MAC fail case */
  2301. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  2302. AssertIntEQ(ret, 0);
  2303. AssertNull(pkey);
  2304. AssertNull(cert);
  2305. /* check parse with no extra certs kept */
  2306. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  2307. AssertIntEQ(ret, 1);
  2308. AssertNotNull(pkey);
  2309. AssertNotNull(cert);
  2310. wolfSSL_EVP_PKEY_free(pkey);
  2311. wolfSSL_X509_free(cert);
  2312. /* check parse with extra certs kept */
  2313. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  2314. AssertIntEQ(ret, 1);
  2315. AssertNotNull(pkey);
  2316. AssertNotNull(cert);
  2317. AssertNotNull(ca);
  2318. /* should be 2 other certs on stack */
  2319. tmp = sk_X509_pop(ca);
  2320. AssertNotNull(tmp);
  2321. X509_free(tmp);
  2322. tmp = sk_X509_pop(ca);
  2323. AssertNotNull(tmp);
  2324. X509_free(tmp);
  2325. AssertNull(sk_X509_pop(ca));
  2326. EVP_PKEY_free(pkey);
  2327. X509_free(cert);
  2328. sk_X509_pop_free(ca, X509_free);
  2329. /* check PKCS12_create */
  2330. AssertNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  2331. AssertIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  2332. SSL_SUCCESS);
  2333. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  2334. -1, -1, 100, -1, 0)));
  2335. EVP_PKEY_free(pkey);
  2336. X509_free(cert);
  2337. sk_X509_free(ca);
  2338. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  2339. SSL_SUCCESS);
  2340. PKCS12_free(pkcs12_2);
  2341. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  2342. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  2343. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  2344. 2000, 1, 0)));
  2345. EVP_PKEY_free(pkey);
  2346. X509_free(cert);
  2347. sk_X509_free(ca);
  2348. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  2349. SSL_SUCCESS);
  2350. /* should be 2 other certs on stack */
  2351. tmp = sk_X509_pop(ca);
  2352. AssertNotNull(tmp);
  2353. X509_free(tmp);
  2354. tmp = sk_X509_pop(ca);
  2355. AssertNotNull(tmp);
  2356. X509_free(tmp);
  2357. AssertNull(sk_X509_pop(ca));
  2358. #ifndef NO_RC4
  2359. PKCS12_free(pkcs12_2);
  2360. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  2361. NID_pbe_WithSHA1And128BitRC4,
  2362. NID_pbe_WithSHA1And128BitRC4,
  2363. 2000, 1, 0)));
  2364. EVP_PKEY_free(pkey);
  2365. X509_free(cert);
  2366. sk_X509_free(ca);
  2367. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  2368. SSL_SUCCESS);
  2369. #endif /* NO_RC4 */
  2370. EVP_PKEY_free(pkey);
  2371. X509_free(cert);
  2372. BIO_free(bio);
  2373. PKCS12_free(pkcs12);
  2374. PKCS12_free(pkcs12_2);
  2375. sk_X509_free(ca);
  2376. #ifdef HAVE_ECC
  2377. /* test order of parsing */
  2378. f = fopen(order, "rb");
  2379. AssertNotNull(f);
  2380. bytes = (int)fread(buffer, 1, sizeof(buffer), f);
  2381. fclose(f);
  2382. AssertNotNull(bio = BIO_new_mem_buf((void*)buffer, bytes));
  2383. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  2384. AssertIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  2385. WOLFSSL_SUCCESS);
  2386. AssertNotNull(pkey);
  2387. AssertNotNull(cert);
  2388. AssertNotNull(ca);
  2389. /* compare subject lines of certificates */
  2390. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  2391. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  2392. SSL_FILETYPE_PEM));
  2393. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  2394. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  2395. X509_free(x509);
  2396. /* test expected fail case */
  2397. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  2398. SSL_FILETYPE_PEM));
  2399. AssertIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  2400. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  2401. X509_free(x509);
  2402. X509_free(cert);
  2403. /* get subject line from ca stack */
  2404. AssertNotNull(cert = sk_X509_pop(ca));
  2405. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  2406. /* compare subject from certificate in ca to expected */
  2407. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  2408. SSL_FILETYPE_PEM));
  2409. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  2410. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  2411. EVP_PKEY_free(pkey);
  2412. X509_free(x509);
  2413. X509_free(cert);
  2414. BIO_free(bio);
  2415. PKCS12_free(pkcs12);
  2416. sk_X509_free(ca);
  2417. #endif /* HAVE_ECC */
  2418. (void)x509;
  2419. (void)subject;
  2420. (void)order;
  2421. printf(resultFmt, passed);
  2422. #endif /* OPENSSL_EXTRA */
  2423. }
  2424. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  2425. !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  2426. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA)
  2427. /* for PKCS8 test case */
  2428. static WC_INLINE int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  2429. {
  2430. int flag = 0;
  2431. (void)rw;
  2432. if (userdata != NULL) {
  2433. flag = *((int*)userdata); /* user set data */
  2434. }
  2435. switch (flag) {
  2436. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  2437. * can be anything the user wishes to be passed to the callback
  2438. * associated with the WOLFSSL_CTX */
  2439. strncpy(passwd, "yassl123", sz);
  2440. return 8;
  2441. default:
  2442. return BAD_FUNC_ARG;
  2443. }
  2444. }
  2445. #endif
  2446. /* Testing functions dealing with PKCS8 */
  2447. static void test_wolfSSL_PKCS8(void)
  2448. {
  2449. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  2450. !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  2451. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  2452. defined(WOLFSSL_ENCRYPTED_KEYS)
  2453. byte buffer[FOURK_BUF];
  2454. byte der[FOURK_BUF];
  2455. char file[] = "./certs/server-keyPkcs8Enc.pem";
  2456. FILE *f;
  2457. int flag = 1;
  2458. int bytes;
  2459. WOLFSSL_CTX* ctx;
  2460. printf(testingFmt, "wolfSSL_PKCS8()");
  2461. f = fopen(file, "rb");
  2462. AssertNotNull(f);
  2463. bytes = (int)fread(buffer, 1, sizeof(buffer), f);
  2464. fclose(f);
  2465. /* Note that wolfSSL_Init() or wolfCrypt_Init() has been called before these
  2466. * function calls */
  2467. #ifndef NO_WOLFSSL_CLIENT
  2468. #ifndef WOLFSSL_NO_TLS12
  2469. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  2470. #else
  2471. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  2472. #endif
  2473. #else
  2474. #ifndef WOLFSSL_NO_TLS12
  2475. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  2476. #else
  2477. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  2478. #endif
  2479. #endif
  2480. wolfSSL_CTX_set_default_passwd_cb(ctx, &PKCS8TestCallBack);
  2481. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  2482. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  2483. SSL_FILETYPE_PEM), SSL_SUCCESS);
  2484. /* this next case should fail if setting the user flag to a value other
  2485. * than 1 due to the password callback functions return value */
  2486. flag = 0;
  2487. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  2488. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  2489. SSL_FILETYPE_PEM), SSL_SUCCESS);
  2490. wolfSSL_CTX_free(ctx);
  2491. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  2492. AssertIntGT(wc_KeyPemToDer(buffer, bytes, der, FOURK_BUF, "yassl123"),
  2493. 0);
  2494. /* test that error value is returned with a bad password */
  2495. AssertIntLT(wc_KeyPemToDer(buffer, bytes, der, FOURK_BUF, "bad"), 0);
  2496. printf(resultFmt, passed);
  2497. #endif /* OPENSSL_EXTRA */
  2498. }
  2499. /* Testing functions dealing with PKCS5 */
  2500. static void test_wolfSSL_PKCS5(void)
  2501. {
  2502. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  2503. const char *passwd = "pass1234";
  2504. const unsigned char *salt = (unsigned char *)"salt1234";
  2505. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  2506. DYNAMIC_TYPE_TMP_BUFFER);
  2507. int ret = 0;
  2508. AssertNotNull(out);
  2509. ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  2510. (int)XSTRLEN((const char *) salt), 10,
  2511. WC_SHA_DIGEST_SIZE,out);
  2512. AssertIntEQ(ret, SSL_SUCCESS);
  2513. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2514. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  2515. }
  2516. /* test parsing URI from certificate */
  2517. static void test_wolfSSL_URI(void)
  2518. {
  2519. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  2520. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  2521. defined(OPENSSL_EXTRA))
  2522. WOLFSSL_X509* x509;
  2523. const char uri[] = "./certs/client-uri-cert.pem";
  2524. const char badUri[] = "./certs/client-relative-uri.pem";
  2525. printf(testingFmt, "wolfSSL URI parse");
  2526. x509 = wolfSSL_X509_load_certificate_file(uri, WOLFSSL_FILETYPE_PEM);
  2527. AssertNotNull(x509);
  2528. wolfSSL_FreeX509(x509);
  2529. x509 = wolfSSL_X509_load_certificate_file(badUri, WOLFSSL_FILETYPE_PEM);
  2530. #ifndef IGNORE_NAME_CONSTRAINTS
  2531. AssertNull(x509);
  2532. #else
  2533. AssertNotNull(x509);
  2534. #endif
  2535. printf(resultFmt, passed);
  2536. #endif
  2537. }
  2538. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  2539. * version allowed.
  2540. * POST: 1 on success.
  2541. */
  2542. static int test_wolfSSL_CTX_SetMinVersion(void)
  2543. {
  2544. int failFlag = WOLFSSL_SUCCESS;
  2545. #ifndef NO_WOLFSSL_CLIENT
  2546. WOLFSSL_CTX* ctx;
  2547. int itr;
  2548. #ifndef NO_OLD_TLS
  2549. const int versions[] = { WOLFSSL_TLSV1, WOLFSSL_TLSV1_1,
  2550. WOLFSSL_TLSV1_2 };
  2551. #elif !defined(WOLFSSL_NO_TLS12)
  2552. const int versions[] = { WOLFSSL_TLSV1_2 };
  2553. #elif defined(WOLFSSL_TLS13)
  2554. const int versions[] = { WOLFSSL_TLSV1_3 };
  2555. #endif
  2556. failFlag = WOLFSSL_SUCCESS;
  2557. AssertTrue(wolfSSL_Init());
  2558. #ifndef WOLFSSL_NO_TLS12
  2559. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  2560. #else
  2561. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  2562. #endif
  2563. printf(testingFmt, "wolfSSL_CTX_SetMinVersion()");
  2564. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++){
  2565. if(wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr)) != WOLFSSL_SUCCESS){
  2566. failFlag = WOLFSSL_FAILURE;
  2567. }
  2568. }
  2569. printf(resultFmt, failFlag == WOLFSSL_SUCCESS ? passed : failed);
  2570. wolfSSL_CTX_free(ctx);
  2571. AssertTrue(wolfSSL_Cleanup());
  2572. #endif
  2573. return failFlag;
  2574. } /* END test_wolfSSL_CTX_SetMinVersion */
  2575. /*----------------------------------------------------------------------------*
  2576. | OCSP Stapling
  2577. *----------------------------------------------------------------------------*/
  2578. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  2579. * need to contact the CA, lowering the cost of cert revocation checking.
  2580. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  2581. * POST: 1 returned for success.
  2582. */
  2583. static int test_wolfSSL_UseOCSPStapling(void)
  2584. {
  2585. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  2586. !defined(NO_WOLFSSL_CLIENT)
  2587. int ret;
  2588. WOLFSSL_CTX* ctx;
  2589. WOLFSSL* ssl;
  2590. wolfSSL_Init();
  2591. #ifndef NO_WOLFSSL_CLIENT
  2592. #ifndef WOLFSSL_NO_TLS12
  2593. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  2594. #else
  2595. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  2596. #endif
  2597. #else
  2598. #ifndef WOLFSSL_NO_TLS12
  2599. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  2600. #else
  2601. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  2602. #endif
  2603. #endif
  2604. ssl = wolfSSL_new(ctx);
  2605. printf(testingFmt, "wolfSSL_UseOCSPStapling()");
  2606. ret = wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  2607. WOLFSSL_CSR2_OCSP_USE_NONCE);
  2608. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  2609. wolfSSL_free(ssl);
  2610. wolfSSL_CTX_free(ctx);
  2611. if(ret != WOLFSSL_SUCCESS){
  2612. wolfSSL_Cleanup();
  2613. return WOLFSSL_FAILURE;
  2614. }
  2615. return wolfSSL_Cleanup();
  2616. #else
  2617. return WOLFSSL_SUCCESS;
  2618. #endif
  2619. } /*END test_wolfSSL_UseOCSPStapling */
  2620. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 funciton. OCSP
  2621. * stapling eliminates the need ot contact the CA and lowers cert revocation
  2622. * check.
  2623. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  2624. */
  2625. static int test_wolfSSL_UseOCSPStaplingV2 (void)
  2626. {
  2627. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  2628. !defined(NO_WOLFSSL_CLIENT)
  2629. int ret;
  2630. WOLFSSL_CTX* ctx;
  2631. WOLFSSL* ssl;
  2632. wolfSSL_Init();
  2633. #ifndef NO_WOLFSSL_CLIENT
  2634. #ifndef WOLFSSL_NO_TLS12
  2635. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  2636. #else
  2637. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  2638. #endif
  2639. #else
  2640. #ifndef WOLFSSL_NO_TLS12
  2641. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  2642. #else
  2643. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  2644. #endif
  2645. #endif
  2646. ssl = wolfSSL_new(ctx);
  2647. printf(testingFmt, "wolfSSL_UseOCSPStaplingV2()");
  2648. ret = wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  2649. WOLFSSL_CSR2_OCSP_USE_NONCE );
  2650. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  2651. wolfSSL_free(ssl);
  2652. wolfSSL_CTX_free(ctx);
  2653. if (ret != WOLFSSL_SUCCESS){
  2654. wolfSSL_Cleanup();
  2655. return WOLFSSL_FAILURE;
  2656. }
  2657. return wolfSSL_Cleanup();
  2658. #else
  2659. return WOLFSSL_SUCCESS;
  2660. #endif
  2661. } /*END test_wolfSSL_UseOCSPStaplingV2*/
  2662. /*----------------------------------------------------------------------------*
  2663. | Multicast Tests
  2664. *----------------------------------------------------------------------------*/
  2665. static void test_wolfSSL_mcast(void)
  2666. {
  2667. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST)
  2668. WOLFSSL_CTX* ctx;
  2669. WOLFSSL* ssl;
  2670. int result;
  2671. byte preMasterSecret[512];
  2672. byte clientRandom[32];
  2673. byte serverRandom[32];
  2674. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  2675. byte buf[256];
  2676. word16 newId;
  2677. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method());
  2678. AssertNotNull(ctx);
  2679. result = wolfSSL_CTX_mcast_set_member_id(ctx, 0);
  2680. AssertIntEQ(result, WOLFSSL_SUCCESS);
  2681. ssl = wolfSSL_new(ctx);
  2682. AssertNotNull(ssl);
  2683. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  2684. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  2685. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  2686. result = wolfSSL_set_secret(ssl, 23,
  2687. preMasterSecret, sizeof(preMasterSecret),
  2688. clientRandom, serverRandom, suite);
  2689. AssertIntEQ(result, WOLFSSL_SUCCESS);
  2690. result = wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf));
  2691. AssertIntLE(result, 0);
  2692. AssertIntLE(newId, 100);
  2693. wolfSSL_free(ssl);
  2694. wolfSSL_CTX_free(ctx);
  2695. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST */
  2696. }
  2697. /*----------------------------------------------------------------------------*
  2698. | Wolfcrypt
  2699. *----------------------------------------------------------------------------*/
  2700. /*
  2701. * Unit test for the wc_InitBlake2b()
  2702. */
  2703. static int test_wc_InitBlake2b (void)
  2704. {
  2705. int ret = 0;
  2706. #ifdef HAVE_BLAKE2
  2707. Blake2b blake2;
  2708. printf(testingFmt, "wc_InitBlake2B()");
  2709. /* Test good arg. */
  2710. ret = wc_InitBlake2b(&blake2, 64);
  2711. if (ret != 0) {
  2712. ret = WOLFSSL_FATAL_ERROR;
  2713. }
  2714. /* Test bad arg. */
  2715. if (!ret) {
  2716. ret = wc_InitBlake2b(NULL, 64);
  2717. if (ret == 0) {
  2718. ret = WOLFSSL_FATAL_ERROR;
  2719. } else {
  2720. ret = 0;
  2721. }
  2722. }
  2723. if (!ret) {
  2724. ret = wc_InitBlake2b(NULL, 128);
  2725. if (ret == 0) {
  2726. ret = WOLFSSL_FATAL_ERROR;
  2727. } else {
  2728. ret = 0;
  2729. }
  2730. }
  2731. if (!ret) {
  2732. ret = wc_InitBlake2b(&blake2, 128);
  2733. if (ret == 0) {
  2734. ret = WOLFSSL_FATAL_ERROR;
  2735. } else {
  2736. ret = 0;
  2737. }
  2738. }
  2739. if (!ret) {
  2740. ret = wc_InitBlake2b(NULL, 0);
  2741. if (ret == 0) {
  2742. ret = WOLFSSL_FATAL_ERROR;
  2743. } else {
  2744. ret = 0;
  2745. }
  2746. }
  2747. if (!ret) {
  2748. ret = wc_InitBlake2b(&blake2, 0);
  2749. if (ret == 0) {
  2750. ret = WOLFSSL_FATAL_ERROR;
  2751. } else {
  2752. ret = 0;
  2753. }
  2754. }
  2755. printf(resultFmt, ret == 0 ? passed : failed);
  2756. #endif
  2757. return ret;
  2758. } /*END test_wc_InitBlake2b*/
  2759. /*
  2760. * Unit test for the wc_InitMd5()
  2761. */
  2762. static int test_wc_InitMd5 (void)
  2763. {
  2764. int flag = 0;
  2765. #ifndef NO_MD5
  2766. wc_Md5 md5;
  2767. int ret;
  2768. printf(testingFmt, "wc_InitMd5()");
  2769. /* Test good arg. */
  2770. ret = wc_InitMd5(&md5);
  2771. if (ret != 0) {
  2772. flag = WOLFSSL_FATAL_ERROR;
  2773. }
  2774. /* Test bad arg. */
  2775. if (!flag) {
  2776. ret = wc_InitMd5(NULL);
  2777. if (ret != BAD_FUNC_ARG) {
  2778. flag = WOLFSSL_FATAL_ERROR;
  2779. }
  2780. }
  2781. wc_Md5Free(&md5);
  2782. printf(resultFmt, flag == 0 ? passed : failed);
  2783. #endif
  2784. return flag;
  2785. } /* END test_wc_InitMd5 */
  2786. /*
  2787. * Testing wc_UpdateMd5()
  2788. */
  2789. static int test_wc_Md5Update (void)
  2790. {
  2791. int flag = 0;
  2792. #ifndef NO_MD5
  2793. wc_Md5 md5;
  2794. byte hash[WC_MD5_DIGEST_SIZE];
  2795. testVector a, b, c;
  2796. int ret;
  2797. ret = wc_InitMd5(&md5);
  2798. if (ret != 0) {
  2799. flag = ret;
  2800. }
  2801. printf(testingFmt, "wc_Md5Update()");
  2802. /* Input */
  2803. if (!flag) {
  2804. a.input = "a";
  2805. a.inLen = XSTRLEN(a.input);
  2806. }
  2807. if (!flag){
  2808. ret = wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen);
  2809. if (ret != 0) {
  2810. flag = ret;
  2811. }
  2812. }
  2813. if (!flag) {
  2814. ret = wc_Md5Final(&md5, hash);
  2815. if (ret != 0) {
  2816. flag = ret;
  2817. }
  2818. }
  2819. /* Update input. */
  2820. if (!flag) {
  2821. a.input = "abc";
  2822. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  2823. "\x72";
  2824. a.inLen = XSTRLEN(a.input);
  2825. a.outLen = XSTRLEN(a.output);
  2826. ret = wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen);
  2827. if (ret != 0) {
  2828. flag = ret;
  2829. }
  2830. }
  2831. if (!flag) {
  2832. ret = wc_Md5Final(&md5, hash);
  2833. if (ret != 0) {
  2834. flag = ret;
  2835. }
  2836. }
  2837. if (!flag) {
  2838. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  2839. flag = WOLFSSL_FATAL_ERROR;
  2840. }
  2841. }
  2842. /*Pass in bad values. */
  2843. if (!flag) {
  2844. b.input = NULL;
  2845. b.inLen = 0;
  2846. ret = wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen);
  2847. if (ret != 0) {
  2848. flag = ret;
  2849. }
  2850. }
  2851. if (!flag) {
  2852. c.input = NULL;
  2853. c.inLen = WC_MD5_DIGEST_SIZE;
  2854. ret = wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen);
  2855. if (ret != BAD_FUNC_ARG) {
  2856. flag = WOLFSSL_FATAL_ERROR;
  2857. }
  2858. }
  2859. if (!flag) {
  2860. ret = wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen);
  2861. if (ret != BAD_FUNC_ARG) {
  2862. flag = WOLFSSL_FATAL_ERROR;
  2863. }
  2864. }
  2865. wc_Md5Free(&md5);
  2866. printf(resultFmt, flag == 0 ? passed : failed);
  2867. #endif
  2868. return flag;
  2869. } /* END test_wc_Md5Update() */
  2870. /*
  2871. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  2872. */
  2873. static int test_wc_Md5Final (void)
  2874. {
  2875. int flag = 0;
  2876. #ifndef NO_MD5
  2877. /* Instantiate */
  2878. wc_Md5 md5;
  2879. byte* hash_test[3];
  2880. byte hash1[WC_MD5_DIGEST_SIZE];
  2881. byte hash2[2*WC_MD5_DIGEST_SIZE];
  2882. byte hash3[5*WC_MD5_DIGEST_SIZE];
  2883. int times, i, ret;
  2884. /* Initialize */
  2885. ret = wc_InitMd5(&md5);
  2886. if (ret != 0) {
  2887. flag = ret;
  2888. }
  2889. if (!flag) {
  2890. hash_test[0] = hash1;
  2891. hash_test[1] = hash2;
  2892. hash_test[2] = hash3;
  2893. }
  2894. times = sizeof(hash_test)/sizeof(byte*);
  2895. /* Test good args. */
  2896. printf(testingFmt, "wc_Md5Final()");
  2897. for (i = 0; i < times; i++) {
  2898. if (!flag) {
  2899. ret = wc_Md5Final(&md5, hash_test[i]);
  2900. if (ret != 0) {
  2901. flag = WOLFSSL_FATAL_ERROR;
  2902. }
  2903. }
  2904. }
  2905. /* Test bad args. */
  2906. if (!flag) {
  2907. ret = wc_Md5Final(NULL, NULL);
  2908. if (ret != BAD_FUNC_ARG) {
  2909. flag = WOLFSSL_FATAL_ERROR;
  2910. }
  2911. }
  2912. if (!flag) {
  2913. ret = wc_Md5Final(NULL, hash1);
  2914. if (ret != BAD_FUNC_ARG) {
  2915. flag = WOLFSSL_FATAL_ERROR;
  2916. }
  2917. }
  2918. if (!flag) {
  2919. ret = wc_Md5Final(&md5, NULL);
  2920. if (ret != BAD_FUNC_ARG) {
  2921. flag = WOLFSSL_FATAL_ERROR;
  2922. }
  2923. }
  2924. wc_Md5Free(&md5);
  2925. printf(resultFmt, flag == 0 ? passed : failed);
  2926. #endif
  2927. return flag;
  2928. }
  2929. /*
  2930. * Unit test for the wc_InitSha()
  2931. */
  2932. static int test_wc_InitSha(void)
  2933. {
  2934. int flag = 0;
  2935. #ifndef NO_SHA
  2936. wc_Sha sha;
  2937. int ret;
  2938. printf(testingFmt, "wc_InitSha()");
  2939. /* Test good arg. */
  2940. ret = wc_InitSha(&sha);
  2941. if (ret != 0) {
  2942. flag = WOLFSSL_FATAL_ERROR;
  2943. }
  2944. /* Test bad arg. */
  2945. if (!flag) {
  2946. ret = wc_InitSha(NULL);
  2947. if (ret != BAD_FUNC_ARG) {
  2948. flag = WOLFSSL_FATAL_ERROR;
  2949. }
  2950. }
  2951. wc_ShaFree(&sha);
  2952. printf(resultFmt, flag == 0 ? passed : failed);
  2953. #endif
  2954. return flag;
  2955. } /* END test_wc_InitSha */
  2956. /*
  2957. * Tesing wc_ShaUpdate()
  2958. */
  2959. static int test_wc_ShaUpdate (void)
  2960. {
  2961. int flag = 0;
  2962. #ifndef NO_SHA
  2963. wc_Sha sha;
  2964. byte hash[WC_SHA_DIGEST_SIZE];
  2965. testVector a, b, c;
  2966. int ret;
  2967. ret = wc_InitSha(&sha);
  2968. if (ret != 0) {
  2969. flag = ret;
  2970. }
  2971. printf(testingFmt, "wc_ShaUpdate()");
  2972. /* Input. */
  2973. if (!flag) {
  2974. a.input = "a";
  2975. a.inLen = XSTRLEN(a.input);
  2976. }
  2977. if (!flag) {
  2978. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  2979. if (ret != 0) {
  2980. flag = ret;
  2981. }
  2982. }
  2983. if (!flag) {
  2984. ret = wc_ShaFinal(&sha, hash);
  2985. if (ret != 0) {
  2986. flag = ret;
  2987. }
  2988. }
  2989. /* Update input. */
  2990. if (!flag) {
  2991. a.input = "abc";
  2992. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  2993. "\x6C\x9C\xD0\xD8\x9D";
  2994. a.inLen = XSTRLEN(a.input);
  2995. a.outLen = XSTRLEN(a.output);
  2996. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  2997. if (ret != 0) {
  2998. flag = ret;
  2999. }
  3000. }
  3001. if (!flag) {
  3002. ret = wc_ShaFinal(&sha, hash);
  3003. if (ret !=0) {
  3004. flag = ret;
  3005. }
  3006. }
  3007. if (!flag) {
  3008. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  3009. flag = WOLFSSL_FATAL_ERROR;
  3010. }
  3011. }
  3012. /* Try passing in bad values. */
  3013. if (!flag) {
  3014. b.input = NULL;
  3015. b.inLen = 0;
  3016. ret = wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen);
  3017. if (ret != 0) {
  3018. flag = ret;
  3019. }
  3020. }
  3021. if (!flag) {
  3022. c.input = NULL;
  3023. c.inLen = WC_SHA_DIGEST_SIZE;
  3024. ret = wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen);
  3025. if (ret != BAD_FUNC_ARG) {
  3026. flag = WOLFSSL_FATAL_ERROR;
  3027. }
  3028. }
  3029. if (!flag) {
  3030. ret = wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  3031. if (ret != BAD_FUNC_ARG) {
  3032. flag = WOLFSSL_FATAL_ERROR;
  3033. }
  3034. }
  3035. wc_ShaFree(&sha);
  3036. /* If not returned then the unit test passed test vectors. */
  3037. printf(resultFmt, flag == 0 ? passed : failed);
  3038. #endif
  3039. return flag;
  3040. } /* END test_wc_ShaUpdate() */
  3041. /*
  3042. * Unit test on wc_ShaFinal
  3043. */
  3044. static int test_wc_ShaFinal (void)
  3045. {
  3046. int flag = 0;
  3047. #ifndef NO_SHA
  3048. wc_Sha sha;
  3049. byte* hash_test[3];
  3050. byte hash1[WC_SHA_DIGEST_SIZE];
  3051. byte hash2[2*WC_SHA_DIGEST_SIZE];
  3052. byte hash3[5*WC_SHA_DIGEST_SIZE];
  3053. int times, i, ret;
  3054. /*Initialize*/
  3055. ret = wc_InitSha(&sha);
  3056. if (ret) {
  3057. flag = ret;
  3058. }
  3059. if (!flag) {
  3060. hash_test[0] = hash1;
  3061. hash_test[1] = hash2;
  3062. hash_test[2] = hash3;
  3063. }
  3064. times = sizeof(hash_test)/sizeof(byte*);
  3065. /* Good test args. */
  3066. printf(testingFmt, "wc_ShaFinal()");
  3067. for (i = 0; i < times; i++) {
  3068. if (!flag) {
  3069. ret = wc_ShaFinal(&sha, hash_test[i]);
  3070. if (ret != 0) {
  3071. flag = WOLFSSL_FATAL_ERROR;
  3072. }
  3073. }
  3074. }
  3075. /* Test bad args. */
  3076. if (!flag) {
  3077. ret = wc_ShaFinal(NULL, NULL);
  3078. if (ret != BAD_FUNC_ARG) {
  3079. flag = WOLFSSL_FATAL_ERROR;
  3080. }
  3081. }
  3082. if (!flag) {
  3083. ret = wc_ShaFinal(NULL, hash1);
  3084. if (ret != BAD_FUNC_ARG) {
  3085. flag = WOLFSSL_FATAL_ERROR;
  3086. }
  3087. }
  3088. if (!flag) {
  3089. ret = wc_ShaFinal(&sha, NULL);
  3090. if (ret != BAD_FUNC_ARG) {
  3091. flag = WOLFSSL_FATAL_ERROR;
  3092. }
  3093. }
  3094. wc_ShaFree(&sha);
  3095. printf(resultFmt, flag == 0 ? passed : failed);
  3096. #endif
  3097. return flag;
  3098. } /* END test_wc_ShaFinal */
  3099. /*
  3100. * Unit test for wc_InitSha256()
  3101. */
  3102. static int test_wc_InitSha256 (void)
  3103. {
  3104. int flag = 0;
  3105. #ifndef NO_SHA256
  3106. wc_Sha256 sha256;
  3107. int ret;
  3108. printf(testingFmt, "wc_InitSha256()");
  3109. /* Test good arg. */
  3110. ret = wc_InitSha256(&sha256);
  3111. if (ret != 0) {
  3112. flag = WOLFSSL_FATAL_ERROR;
  3113. }
  3114. /* Test bad arg. */
  3115. if (!flag) {
  3116. ret = wc_InitSha256(NULL);
  3117. if (ret != BAD_FUNC_ARG) {
  3118. flag = WOLFSSL_FATAL_ERROR;
  3119. }
  3120. }
  3121. wc_Sha256Free(&sha256);
  3122. printf(resultFmt, flag == 0 ? passed : failed);
  3123. #endif
  3124. return flag;
  3125. } /* END test_wc_InitSha256 */
  3126. /*
  3127. * Unit test for wc_Sha256Update()
  3128. */
  3129. static int test_wc_Sha256Update (void)
  3130. {
  3131. int flag = 0;
  3132. #ifndef NO_SHA256
  3133. wc_Sha256 sha256;
  3134. byte hash[WC_SHA256_DIGEST_SIZE];
  3135. testVector a, b, c;
  3136. int ret;
  3137. ret = wc_InitSha256(&sha256);
  3138. if (ret != 0) {
  3139. flag = ret;
  3140. }
  3141. printf(testingFmt, "wc_Sha256Update()");
  3142. /* Input. */
  3143. if (!flag) {
  3144. a.input = "a";
  3145. a.inLen = XSTRLEN(a.input);
  3146. }
  3147. if (!flag) {
  3148. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  3149. if (ret != 0) {
  3150. flag = ret;
  3151. }
  3152. }
  3153. if (!flag) {
  3154. ret = wc_Sha256Final(&sha256, hash);
  3155. if (ret != 0) {
  3156. flag = ret;
  3157. }
  3158. }
  3159. /* Update input. */
  3160. if (!flag) {
  3161. a.input = "abc";
  3162. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  3163. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  3164. "\x15\xAD";
  3165. a.inLen = XSTRLEN(a.input);
  3166. a.outLen = XSTRLEN(a.output);
  3167. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  3168. if (ret != 0) {
  3169. flag = ret;
  3170. }
  3171. }
  3172. if (!flag) {
  3173. ret = wc_Sha256Final(&sha256, hash);
  3174. if (ret != 0) {
  3175. flag = ret;
  3176. }
  3177. }
  3178. if (!flag) {
  3179. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  3180. flag = WOLFSSL_FATAL_ERROR;
  3181. }
  3182. }
  3183. /* Try passing in bad values */
  3184. if (!flag) {
  3185. b.input = NULL;
  3186. b.inLen = 0;
  3187. ret = wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen);
  3188. if (ret != 0) {
  3189. flag = ret;
  3190. }
  3191. }
  3192. if (!flag) {
  3193. c.input = NULL;
  3194. c.inLen = WC_SHA256_DIGEST_SIZE;
  3195. ret = wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen);
  3196. if (ret != BAD_FUNC_ARG) {
  3197. flag = WOLFSSL_FATAL_ERROR;
  3198. }
  3199. }
  3200. if (!flag) {
  3201. ret = wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen);
  3202. if (ret != BAD_FUNC_ARG) {
  3203. flag = WOLFSSL_FATAL_ERROR;
  3204. }
  3205. }
  3206. wc_Sha256Free(&sha256);
  3207. /* If not returned then the unit test passed. */
  3208. printf(resultFmt, flag == 0 ? passed : failed);
  3209. #endif
  3210. return flag;
  3211. } /* END test_wc_Sha256Update */
  3212. /*
  3213. * Unit test function for wc_Sha256Final()
  3214. */
  3215. static int test_wc_Sha256Final (void)
  3216. {
  3217. int flag = 0;
  3218. #ifndef NO_SHA256
  3219. wc_Sha256 sha256;
  3220. byte* hash_test[3];
  3221. byte hash1[WC_SHA256_DIGEST_SIZE];
  3222. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  3223. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  3224. int times, i, ret;
  3225. /* Initialize */
  3226. ret = wc_InitSha256(&sha256);
  3227. if (ret != 0) {
  3228. flag = ret;
  3229. }
  3230. if (!flag) {
  3231. hash_test[0] = hash1;
  3232. hash_test[1] = hash2;
  3233. hash_test[2] = hash3;
  3234. }
  3235. times = sizeof(hash_test) / sizeof(byte*);
  3236. /* Good test args. */
  3237. printf(testingFmt, "wc_Sha256Final()");
  3238. for (i = 0; i < times; i++) {
  3239. if (!flag) {
  3240. ret = wc_Sha256Final(&sha256, hash_test[i]);
  3241. if (ret != 0) {
  3242. flag = WOLFSSL_FATAL_ERROR;
  3243. }
  3244. }
  3245. }
  3246. /* Test bad args. */
  3247. if (!flag ) {
  3248. ret = wc_Sha256Final(NULL, NULL);
  3249. if (ret != BAD_FUNC_ARG) {
  3250. flag = WOLFSSL_FATAL_ERROR;
  3251. }
  3252. }
  3253. if (!flag) {
  3254. ret = wc_Sha256Final(NULL, hash1);
  3255. if (ret != BAD_FUNC_ARG) {
  3256. flag = WOLFSSL_FATAL_ERROR;
  3257. }
  3258. }
  3259. if (!flag) {
  3260. ret = wc_Sha256Final(&sha256, NULL);
  3261. if (ret != BAD_FUNC_ARG) {
  3262. flag = WOLFSSL_FATAL_ERROR;
  3263. }
  3264. }
  3265. wc_Sha256Free(&sha256);
  3266. printf(resultFmt, flag == 0 ? passed : failed);
  3267. #endif
  3268. return flag;
  3269. } /* END test_wc_Sha256Final */
  3270. /*
  3271. * Testing wc_InitSha512()
  3272. */
  3273. static int test_wc_InitSha512 (void)
  3274. {
  3275. int flag = 0;
  3276. #ifdef WOLFSSL_SHA512
  3277. wc_Sha512 sha512;
  3278. int ret;
  3279. printf(testingFmt, "wc_InitSha512()");
  3280. /* Test good arg. */
  3281. ret = wc_InitSha512(&sha512);
  3282. if (ret != 0) {
  3283. flag = WOLFSSL_FATAL_ERROR;
  3284. }
  3285. /* Test bad arg. */
  3286. if (!flag) {
  3287. ret = wc_InitSha512(NULL);
  3288. if (ret != BAD_FUNC_ARG) {
  3289. flag = WOLFSSL_FATAL_ERROR;
  3290. }
  3291. }
  3292. wc_Sha512Free(&sha512);
  3293. printf(resultFmt, flag == 0 ? passed : failed);
  3294. #endif
  3295. return flag;
  3296. } /* END test_wc_InitSha512 */
  3297. /*
  3298. * wc_Sha512Update() test.
  3299. */
  3300. static int test_wc_Sha512Update (void)
  3301. {
  3302. int flag = 0;
  3303. #ifdef WOLFSSL_SHA512
  3304. wc_Sha512 sha512;
  3305. byte hash[WC_SHA512_DIGEST_SIZE];
  3306. testVector a, b, c;
  3307. int ret;
  3308. ret = wc_InitSha512(&sha512);
  3309. if (ret != 0) {
  3310. flag = ret;
  3311. }
  3312. printf(testingFmt, "wc_Sha512Update()");
  3313. /* Input. */
  3314. if (!flag) {
  3315. a.input = "a";
  3316. a.inLen = XSTRLEN(a.input);
  3317. }
  3318. if (!flag) {
  3319. ret = wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen);
  3320. if (ret != 0) {
  3321. flag = ret;
  3322. }
  3323. }
  3324. if (!flag) {
  3325. ret = wc_Sha512Final(&sha512, hash);
  3326. if (ret != 0) {
  3327. flag = ret;
  3328. }
  3329. }
  3330. /* Update input. */
  3331. if (!flag) {
  3332. a.input = "abc";
  3333. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  3334. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  3335. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  3336. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  3337. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  3338. a.inLen = XSTRLEN(a.input);
  3339. a.outLen = XSTRLEN(a.output);
  3340. ret = wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen);
  3341. if (ret != 0) {
  3342. flag = ret;
  3343. }
  3344. }
  3345. if (!flag) {
  3346. ret = wc_Sha512Final(&sha512, hash);
  3347. if (ret != 0) {
  3348. flag = ret;
  3349. }
  3350. }
  3351. if (!flag) {
  3352. if (XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE) != 0) {
  3353. flag = WOLFSSL_FATAL_ERROR;
  3354. }
  3355. }
  3356. /* Try passing in bad values */
  3357. if (!flag) {
  3358. b.input = NULL;
  3359. b.inLen = 0;
  3360. ret = wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen);
  3361. if (ret != 0) {
  3362. flag = ret;
  3363. }
  3364. }
  3365. if (!flag) {
  3366. c.input = NULL;
  3367. c.inLen = WC_SHA512_DIGEST_SIZE;
  3368. ret = wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen);
  3369. if (ret != BAD_FUNC_ARG) {
  3370. flag = WOLFSSL_FATAL_ERROR;
  3371. }
  3372. }
  3373. if (!flag) {
  3374. ret = wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen);
  3375. if (ret != BAD_FUNC_ARG) {
  3376. flag = WOLFSSL_FATAL_ERROR;
  3377. }
  3378. }
  3379. wc_Sha512Free(&sha512);
  3380. /* If not returned then the unit test passed test vectors. */
  3381. printf(resultFmt, flag == 0 ? passed : failed);
  3382. #endif
  3383. return flag;
  3384. } /* END test_wc_Sha512Update */
  3385. /*
  3386. * Unit test function for wc_Sha512Final()
  3387. */
  3388. static int test_wc_Sha512Final (void)
  3389. {
  3390. int flag = 0;
  3391. #ifdef WOLFSSL_SHA512
  3392. wc_Sha512 sha512;
  3393. byte* hash_test[3];
  3394. byte hash1[WC_SHA512_DIGEST_SIZE];
  3395. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  3396. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  3397. int times, i, ret;
  3398. /* Initialize */
  3399. ret = wc_InitSha512(&sha512);
  3400. if (ret != 0) {
  3401. flag = ret;
  3402. }
  3403. if (!flag) {
  3404. hash_test[0] = hash1;
  3405. hash_test[1] = hash2;
  3406. hash_test[2] = hash3;
  3407. }
  3408. times = sizeof(hash_test) / sizeof(byte *);
  3409. /* Good test args. */
  3410. printf(testingFmt, "wc_Sha512Final()");
  3411. for (i = 0; i < times; i++) {
  3412. if (!flag) {
  3413. ret = wc_Sha512Final(&sha512, hash_test[i]);
  3414. if (ret != 0) {
  3415. flag = WOLFSSL_FATAL_ERROR;
  3416. }
  3417. }
  3418. }
  3419. /* Test bad args. */
  3420. if (!flag) {
  3421. ret = wc_Sha512Final(NULL, NULL);
  3422. if (ret != BAD_FUNC_ARG) {
  3423. flag = WOLFSSL_FATAL_ERROR;
  3424. }
  3425. if (!flag) {}
  3426. ret = wc_Sha512Final(NULL, hash1);
  3427. if (ret != BAD_FUNC_ARG) {
  3428. flag = WOLFSSL_FATAL_ERROR;
  3429. }
  3430. }
  3431. if (!flag) {
  3432. ret = wc_Sha512Final(&sha512, NULL);
  3433. if (ret != BAD_FUNC_ARG) {
  3434. flag = WOLFSSL_FATAL_ERROR;
  3435. }
  3436. }
  3437. wc_Sha512Free(&sha512);
  3438. printf(resultFmt, flag == 0 ? passed : failed);
  3439. #endif
  3440. return flag;
  3441. } /* END test_wc_Sha512Final */
  3442. /*
  3443. * Testing wc_InitSha384()
  3444. */
  3445. static int test_wc_InitSha384 (void)
  3446. {
  3447. int flag = 0;
  3448. #ifdef WOLFSSL_SHA384
  3449. wc_Sha384 sha384;
  3450. int ret;
  3451. printf(testingFmt, "wc_InitSha384()");
  3452. /* Test good arg. */
  3453. ret = wc_InitSha384(&sha384);
  3454. if (ret != 0) {
  3455. flag = WOLFSSL_FATAL_ERROR;
  3456. }
  3457. /* Test bad arg. */
  3458. if (!flag) {
  3459. ret = wc_InitSha384(NULL);
  3460. if (ret != BAD_FUNC_ARG) {
  3461. flag = WOLFSSL_FATAL_ERROR;
  3462. }
  3463. }
  3464. wc_Sha384Free(&sha384);
  3465. printf(resultFmt, flag == 0 ? passed : failed);
  3466. #endif
  3467. return flag;
  3468. } /* END test_wc_InitSha384 */
  3469. /*
  3470. * test wc_Sha384Update()
  3471. */
  3472. static int test_wc_Sha384Update (void)
  3473. {
  3474. int flag = 0;
  3475. #ifdef WOLFSSL_SHA384
  3476. wc_Sha384 sha384;
  3477. byte hash[WC_SHA384_DIGEST_SIZE];
  3478. testVector a, b, c;
  3479. int ret;
  3480. ret = wc_InitSha384(&sha384);
  3481. if (ret != 0) {
  3482. flag = ret;
  3483. }
  3484. printf(testingFmt, "wc_Sha384Update()");
  3485. /* Input */
  3486. if (!flag) {
  3487. a.input = "a";
  3488. a.inLen = XSTRLEN(a.input);
  3489. }
  3490. if (!flag) {
  3491. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  3492. if (ret != 0) {
  3493. flag = ret;
  3494. }
  3495. }
  3496. if (!flag) {
  3497. ret = wc_Sha384Final(&sha384, hash);
  3498. if (ret != 0) {
  3499. flag = ret;
  3500. }
  3501. }
  3502. /* Update input. */
  3503. if (!flag) {
  3504. a.input = "abc";
  3505. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  3506. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  3507. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  3508. "\xc8\x25\xa7";
  3509. a.inLen = XSTRLEN(a.input);
  3510. a.outLen = XSTRLEN(a.output);
  3511. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  3512. if (ret != 0) {
  3513. flag = ret;
  3514. }
  3515. }
  3516. if (!flag) {
  3517. ret = wc_Sha384Final(&sha384, hash);
  3518. if (ret != 0) {
  3519. flag = ret;
  3520. }
  3521. }
  3522. if (!flag) {
  3523. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  3524. flag = WOLFSSL_FATAL_ERROR;
  3525. }
  3526. }
  3527. /* Pass in bad values. */
  3528. if (!flag) {
  3529. b.input = NULL;
  3530. b.inLen = 0;
  3531. ret = wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen);
  3532. if (ret != 0) {
  3533. flag = ret;
  3534. }
  3535. }
  3536. if (!flag) {
  3537. c.input = NULL;
  3538. c.inLen = WC_SHA384_DIGEST_SIZE;
  3539. ret = wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen);
  3540. if (ret != BAD_FUNC_ARG) {
  3541. flag = WOLFSSL_FATAL_ERROR;
  3542. }
  3543. }
  3544. if (!flag) {
  3545. ret = wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen);
  3546. if (ret != BAD_FUNC_ARG) {
  3547. flag = WOLFSSL_FATAL_ERROR;
  3548. }
  3549. }
  3550. wc_Sha384Free(&sha384);
  3551. /* If not returned then the unit test passed test vectors. */
  3552. printf(resultFmt, flag == 0 ? passed : failed);
  3553. #endif
  3554. return flag;
  3555. } /* END test_wc_Sha384Update */
  3556. /*
  3557. * Unit test function for wc_Sha384Final();
  3558. */
  3559. static int test_wc_Sha384Final (void)
  3560. {
  3561. int flag = 0;
  3562. #ifdef WOLFSSL_SHA384
  3563. wc_Sha384 sha384;
  3564. byte* hash_test[3];
  3565. byte hash1[WC_SHA384_DIGEST_SIZE];
  3566. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  3567. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  3568. int times, i, ret;
  3569. /* Initialize */
  3570. ret = wc_InitSha384(&sha384);
  3571. if (ret) {
  3572. flag = ret;
  3573. }
  3574. if (!flag) {
  3575. hash_test[0] = hash1;
  3576. hash_test[1] = hash2;
  3577. hash_test[2] = hash3;
  3578. }
  3579. times = sizeof(hash_test) / sizeof(byte*);
  3580. /* Good test args. */
  3581. printf(testingFmt, "wc_Sha384Final()");
  3582. for (i = 0; i < times; i++) {
  3583. if (!flag) {
  3584. ret = wc_Sha384Final(&sha384, hash_test[i]);
  3585. if (ret != 0) {
  3586. flag = WOLFSSL_FATAL_ERROR;
  3587. }
  3588. }
  3589. }
  3590. /* Test bad args. */
  3591. if (!flag) {
  3592. ret = wc_Sha384Final(NULL, NULL);
  3593. if (ret != BAD_FUNC_ARG) {
  3594. flag = WOLFSSL_FATAL_ERROR;
  3595. }
  3596. }
  3597. if (!flag) {
  3598. ret = wc_Sha384Final(NULL, hash1);
  3599. if (ret != BAD_FUNC_ARG) {
  3600. flag = WOLFSSL_FATAL_ERROR;
  3601. }
  3602. }
  3603. if (!flag) {
  3604. ret = wc_Sha384Final(&sha384, NULL);
  3605. if (ret != BAD_FUNC_ARG) {
  3606. flag = WOLFSSL_FATAL_ERROR;
  3607. }
  3608. }
  3609. wc_Sha384Free(&sha384);
  3610. printf(resultFmt, flag == 0 ? passed : failed);
  3611. #endif
  3612. return flag;
  3613. } /* END test_wc_Sha384Final */
  3614. /*
  3615. * Testing wc_InitSha224();
  3616. */
  3617. static int test_wc_InitSha224 (void)
  3618. {
  3619. int flag = 0;
  3620. #ifdef WOLFSSL_SHA224
  3621. wc_Sha224 sha224;
  3622. int ret;
  3623. printf(testingFmt, "wc_InitSha224()");
  3624. /* Test good arg. */
  3625. ret = wc_InitSha224(&sha224);
  3626. if (ret != 0) {
  3627. flag = WOLFSSL_FATAL_ERROR;
  3628. }
  3629. /* Test bad arg. */
  3630. if (!flag) {
  3631. ret = wc_InitSha224(NULL);
  3632. if (ret != BAD_FUNC_ARG) {
  3633. flag = WOLFSSL_FATAL_ERROR;
  3634. }
  3635. }
  3636. wc_Sha224Free(&sha224);
  3637. printf(resultFmt, flag == 0 ? passed : failed);
  3638. #endif
  3639. return flag;
  3640. } /* END test_wc_InitSha224 */
  3641. /*
  3642. * Unit test on wc_Sha224Update
  3643. */
  3644. static int test_wc_Sha224Update (void)
  3645. {
  3646. int flag = 0;
  3647. #ifdef WOLFSSL_SHA224
  3648. wc_Sha224 sha224;
  3649. byte hash[WC_SHA224_DIGEST_SIZE];
  3650. testVector a, b, c;
  3651. int ret;
  3652. ret = wc_InitSha224(&sha224);
  3653. if (ret != 0) {
  3654. flag = ret;
  3655. }
  3656. printf(testingFmt, "wc_Sha224Update()");
  3657. /* Input. */
  3658. if (!flag) {
  3659. a.input = "a";
  3660. a.inLen = XSTRLEN(a.input);
  3661. }
  3662. if (!flag) {
  3663. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  3664. if (ret != 0) {
  3665. flag = ret;
  3666. }
  3667. }
  3668. if (!flag) {
  3669. ret = wc_Sha224Final(&sha224, hash);
  3670. if (ret != 0) {
  3671. flag = ret;
  3672. }
  3673. }
  3674. /* Update input. */
  3675. if (!flag) {
  3676. a.input = "abc";
  3677. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  3678. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  3679. a.inLen = XSTRLEN(a.input);
  3680. a.outLen = XSTRLEN(a.output);
  3681. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  3682. if (ret != 0) {
  3683. flag = ret;
  3684. }
  3685. }
  3686. if (!flag) {
  3687. ret = wc_Sha224Final(&sha224, hash);
  3688. if (ret != 0) {
  3689. flag = ret;
  3690. }
  3691. }
  3692. if (!flag) {
  3693. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  3694. flag = WOLFSSL_FATAL_ERROR;
  3695. }
  3696. }
  3697. /* Pass in bad values. */
  3698. if (!flag) {
  3699. b.input = NULL;
  3700. b.inLen = 0;
  3701. ret = wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen);
  3702. if (ret != 0) {
  3703. flag = ret;
  3704. }
  3705. }
  3706. if (!flag) {
  3707. c.input = NULL;
  3708. c.inLen = WC_SHA224_DIGEST_SIZE;
  3709. ret = wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen);
  3710. if (ret != BAD_FUNC_ARG) {
  3711. flag = WOLFSSL_FATAL_ERROR;
  3712. }
  3713. }
  3714. if (!flag) {
  3715. ret = wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen);
  3716. if (ret != BAD_FUNC_ARG) {
  3717. flag = WOLFSSL_FATAL_ERROR;
  3718. }
  3719. }
  3720. wc_Sha224Free(&sha224);
  3721. /* If not returned then the unit test passed test vectors. */
  3722. printf(resultFmt, flag == 0 ? passed : failed);
  3723. #endif
  3724. return flag;
  3725. } /* END test_wc_Sha224Update */
  3726. /*
  3727. * Unit test for wc_Sha224Final();
  3728. */
  3729. static int test_wc_Sha224Final (void)
  3730. {
  3731. int flag = 0;
  3732. #ifdef WOLFSSL_SHA224
  3733. wc_Sha224 sha224;
  3734. byte* hash_test[3];
  3735. byte hash1[WC_SHA224_DIGEST_SIZE];
  3736. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  3737. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  3738. int times, i, ret;
  3739. /* Initialize */
  3740. ret = wc_InitSha224(&sha224);
  3741. if (ret) {
  3742. flag = ret;
  3743. }
  3744. if (!flag) {
  3745. hash_test[0] = hash1;
  3746. hash_test[1] = hash2;
  3747. hash_test[2] = hash3;
  3748. }
  3749. times = sizeof(hash_test) / sizeof(byte*);
  3750. /* Good test args. */
  3751. printf(testingFmt, "wc_sha224Final()");
  3752. /* Testing oversized buffers. */
  3753. for (i = 0; i < times; i++) {
  3754. if (!flag) {
  3755. ret = wc_Sha224Final(&sha224, hash_test[i]);
  3756. if (ret != 0) {
  3757. flag = WOLFSSL_FATAL_ERROR;
  3758. }
  3759. }
  3760. }
  3761. /* Test bad args. */
  3762. if (!flag) {
  3763. ret = wc_Sha224Final(NULL, NULL);
  3764. if (ret != BAD_FUNC_ARG) {
  3765. flag = WOLFSSL_FATAL_ERROR;
  3766. }
  3767. }
  3768. if (!flag) {
  3769. ret = wc_Sha224Final(NULL, hash1);
  3770. if (ret != BAD_FUNC_ARG) {
  3771. flag = WOLFSSL_FATAL_ERROR;
  3772. }
  3773. }
  3774. if (!flag) {
  3775. ret = wc_Sha224Final(&sha224, NULL);
  3776. if (ret != BAD_FUNC_ARG) {
  3777. flag = WOLFSSL_FATAL_ERROR;
  3778. }
  3779. }
  3780. wc_Sha224Free(&sha224);
  3781. printf(resultFmt, flag == 0 ? passed : failed);
  3782. #endif
  3783. return flag;
  3784. } /* END test_wc_Sha224Final */
  3785. /*
  3786. * Testing wc_InitRipeMd()
  3787. */
  3788. static int test_wc_InitRipeMd (void)
  3789. {
  3790. int flag = 0;
  3791. #ifdef WOLFSSL_RIPEMD
  3792. RipeMd ripemd;
  3793. int ret;
  3794. printf(testingFmt, "wc_InitRipeMd()");
  3795. /* Test good arg. */
  3796. ret = wc_InitRipeMd(&ripemd);
  3797. if (ret != 0) {
  3798. flag = WOLFSSL_FATAL_ERROR;
  3799. }
  3800. /* Test bad arg. */
  3801. if (!flag) {
  3802. ret = wc_InitRipeMd(NULL);
  3803. if (ret != BAD_FUNC_ARG) {
  3804. flag = WOLFSSL_FATAL_ERROR;
  3805. }
  3806. }
  3807. printf(resultFmt, flag == 0 ? passed : failed);
  3808. #endif
  3809. return flag;
  3810. } /* END test_wc_InitRipeMd */
  3811. /*
  3812. * Testing wc_RipeMdUpdate()
  3813. */
  3814. static int test_wc_RipeMdUpdate (void)
  3815. {
  3816. int flag = 0;
  3817. #ifdef WOLFSSL_RIPEMD
  3818. RipeMd ripemd;
  3819. byte hash[RIPEMD_DIGEST_SIZE];
  3820. testVector a, b, c;
  3821. int ret;
  3822. ret = wc_InitRipeMd(&ripemd);
  3823. if (ret != 0) {
  3824. flag = ret;
  3825. }
  3826. printf(testingFmt, "wc_RipeMdUpdate()");
  3827. /* Input */
  3828. if (!flag) {
  3829. a.input = "a";
  3830. a.inLen = XSTRLEN(a.input);
  3831. }
  3832. if (!flag) {
  3833. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  3834. if (ret != 0) {
  3835. flag = ret;
  3836. }
  3837. }
  3838. if (!flag) {
  3839. ret = wc_RipeMdFinal(&ripemd, hash);
  3840. if (ret != 0) {
  3841. flag = ret;
  3842. }
  3843. }
  3844. /* Update input. */
  3845. if (!flag) {
  3846. a.input = "abc";
  3847. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  3848. "\xb0\x87\xf1\x5a\x0b\xfc";
  3849. a.inLen = XSTRLEN(a.input);
  3850. a.outLen = XSTRLEN(a.output);
  3851. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  3852. if (ret != 0) {
  3853. flag = ret;
  3854. }
  3855. }
  3856. if (!flag) {
  3857. ret = wc_RipeMdFinal(&ripemd, hash);
  3858. if (ret != 0) {
  3859. flag = ret;
  3860. }
  3861. }
  3862. if (!flag) {
  3863. if (XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE) != 0) {
  3864. flag = WOLFSSL_FATAL_ERROR;
  3865. }
  3866. }
  3867. /* Pass in bad values. */
  3868. if (!flag) {
  3869. b.input = NULL;
  3870. b.inLen = 0;
  3871. ret = wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen);
  3872. if (ret != 0) {
  3873. flag = ret;
  3874. }
  3875. }
  3876. if (!flag) {
  3877. c.input = NULL;
  3878. c.inLen = RIPEMD_DIGEST_SIZE;
  3879. ret = wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen);
  3880. if (ret != BAD_FUNC_ARG) {
  3881. flag = WOLFSSL_FATAL_ERROR;
  3882. }
  3883. }
  3884. if (!flag) {
  3885. ret = wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  3886. if (ret != BAD_FUNC_ARG) {
  3887. flag = WOLFSSL_FATAL_ERROR;
  3888. }
  3889. }
  3890. printf(resultFmt, flag == 0 ? passed : failed);
  3891. #endif
  3892. return flag;
  3893. } /* END test_wc_RipeMdUdpate */
  3894. /*
  3895. * Unit test function for wc_RipeMdFinal()
  3896. */
  3897. static int test_wc_RipeMdFinal (void)
  3898. {
  3899. int flag = 0;
  3900. #ifdef WOLFSSL_RIPEMD
  3901. RipeMd ripemd;
  3902. byte* hash_test[3];
  3903. byte hash1[RIPEMD_DIGEST_SIZE];
  3904. byte hash2[2*RIPEMD_DIGEST_SIZE];
  3905. byte hash3[5*RIPEMD_DIGEST_SIZE];
  3906. int times, i, ret;
  3907. /* Initialize */
  3908. ret = wc_InitRipeMd(&ripemd);
  3909. if (ret != 0) {
  3910. flag = ret;
  3911. }
  3912. if (!flag) {
  3913. hash_test[0] = hash1;
  3914. hash_test[1] = hash2;
  3915. hash_test[2] = hash3;
  3916. }
  3917. times = sizeof(hash_test) / sizeof(byte*);
  3918. /* Good test args. */
  3919. printf(testingFmt, "wc_RipeMdFinal()");
  3920. /* Testing oversized buffers. */
  3921. for (i = 0; i < times; i++) {
  3922. if (!flag) {
  3923. ret = wc_RipeMdFinal(&ripemd, hash_test[i]);
  3924. if (ret != 0) {
  3925. flag = WOLFSSL_FATAL_ERROR;
  3926. }
  3927. }
  3928. }
  3929. /* Test bad args. */
  3930. if (!flag) {
  3931. ret = wc_RipeMdFinal(NULL, NULL);
  3932. if (ret != BAD_FUNC_ARG) {
  3933. flag = WOLFSSL_FATAL_ERROR;
  3934. }
  3935. }
  3936. if (!flag) {
  3937. ret = wc_RipeMdFinal(NULL, hash1);
  3938. if (ret != BAD_FUNC_ARG) {
  3939. flag = WOLFSSL_FATAL_ERROR;
  3940. }
  3941. }
  3942. if (!flag) {
  3943. ret = wc_RipeMdFinal(&ripemd, NULL);
  3944. if (ret != BAD_FUNC_ARG) {
  3945. flag = WOLFSSL_FATAL_ERROR;
  3946. }
  3947. }
  3948. printf(resultFmt, flag == 0 ? passed : failed);
  3949. #endif
  3950. return flag;
  3951. } /* END test_wc_RipeMdFinal */
  3952. /*
  3953. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  3954. * wc_InitSha3_512
  3955. */
  3956. static int test_wc_InitSha3 (void)
  3957. {
  3958. int ret = 0;
  3959. #if defined(WOLFSSL_SHA3)
  3960. wc_Sha3 sha3;
  3961. #if !defined(WOLFSSL_NOSHA3_224)
  3962. printf(testingFmt, "wc_InitSha3_224()");
  3963. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  3964. /* Test bad args. */
  3965. if (ret == 0) {
  3966. ret = wc_InitSha3_224(NULL, HEAP_HINT, devId);
  3967. if (ret == BAD_FUNC_ARG) {
  3968. ret = 0;
  3969. } else if (ret == 0) {
  3970. ret = WOLFSSL_FATAL_ERROR;
  3971. }
  3972. }
  3973. wc_Sha3_224_Free(&sha3);
  3974. printf(resultFmt, ret == 0 ? passed : failed);
  3975. #endif /* NOSHA3_224 */
  3976. #if !defined(WOLFSSL_NOSHA3_256)
  3977. if (ret == 0) {
  3978. printf(testingFmt, "wc_InitSha3_256()");
  3979. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  3980. /* Test bad args. */
  3981. if (ret == 0) {
  3982. ret = wc_InitSha3_256(NULL, HEAP_HINT, devId);
  3983. if (ret == BAD_FUNC_ARG) {
  3984. ret = 0;
  3985. } else if (ret == 0) {
  3986. ret = WOLFSSL_FATAL_ERROR;
  3987. }
  3988. }
  3989. wc_Sha3_256_Free(&sha3);
  3990. printf(resultFmt, ret == 0 ? passed : failed);
  3991. } /* END sha3_256 */
  3992. #endif /* NOSHA3_256 */
  3993. #if !defined(WOLFSSL_NOSHA3_384)
  3994. if (ret == 0) {
  3995. printf(testingFmt, "wc_InitSha3_384()");
  3996. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  3997. /* Test bad args. */
  3998. if (ret == 0) {
  3999. ret = wc_InitSha3_384(NULL, HEAP_HINT, devId);
  4000. if (ret == BAD_FUNC_ARG) {
  4001. ret = 0;
  4002. } else if (ret == 0) {
  4003. ret = WOLFSSL_FATAL_ERROR;
  4004. }
  4005. }
  4006. wc_Sha3_384_Free(&sha3);
  4007. printf(resultFmt, ret == 0 ? passed : failed);
  4008. } /* END sha3_384 */
  4009. #endif /* NOSHA3_384 */
  4010. #if !defined(WOLFSSL_NOSHA3_512)
  4011. if (ret == 0) {
  4012. printf(testingFmt, "wc_InitSha3_512()");
  4013. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  4014. /* Test bad args. */
  4015. if (ret == 0) {
  4016. ret = wc_InitSha3_512(NULL, HEAP_HINT, devId);
  4017. if (ret == BAD_FUNC_ARG) {
  4018. ret = 0;
  4019. } else if (ret == 0) {
  4020. ret = WOLFSSL_FATAL_ERROR;
  4021. }
  4022. }
  4023. wc_Sha3_512_Free(&sha3);
  4024. printf(resultFmt, ret == 0 ? passed : failed);
  4025. } /* END sha3_512 */
  4026. #endif /* NOSHA3_512 */
  4027. #endif
  4028. return ret;
  4029. } /* END test_wc_InitSha3 */
  4030. /*
  4031. * Testing wc_Sha3_Update()
  4032. */
  4033. static int testing_wc_Sha3_Update (void)
  4034. {
  4035. int ret = 0;
  4036. #if defined(WOLFSSL_SHA3)
  4037. wc_Sha3 sha3;
  4038. byte msg[] = "Everybody's working for the weekend.";
  4039. byte msg2[] = "Everybody gets Friday off.";
  4040. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  4041. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  4042. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  4043. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  4044. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  4045. word32 msglen = sizeof(msg) - 1;
  4046. word32 msg2len = sizeof(msg2);
  4047. word32 msgCmplen = sizeof(msgCmp);
  4048. #if !defined(WOLFSSL_NOSHA3_224)
  4049. printf(testingFmt, "wc_Sha3_224_Update()");
  4050. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  4051. if (ret != 0) {
  4052. return ret;
  4053. }
  4054. ret = wc_Sha3_224_Update(&sha3, msg, msglen);
  4055. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  4056. ret = WOLFSSL_FATAL_ERROR;
  4057. }
  4058. if (ret == 0) {
  4059. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  4060. if (ret == 0 && XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  4061. ret = WOLFSSL_FATAL_ERROR;
  4062. }
  4063. }
  4064. /* Pass bad args. */
  4065. if (ret == 0) {
  4066. ret = wc_Sha3_224_Update(NULL, msg2, msg2len);
  4067. if (ret == BAD_FUNC_ARG) {
  4068. ret = wc_Sha3_224_Update(&sha3, NULL, 5);
  4069. }
  4070. if (ret == BAD_FUNC_ARG) {
  4071. wc_Sha3_224_Free(&sha3);
  4072. if (wc_InitSha3_224(&sha3, HEAP_HINT, devId)) {
  4073. return ret;
  4074. }
  4075. ret = wc_Sha3_224_Update(&sha3, NULL, 0);
  4076. if (ret == 0) {
  4077. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  4078. }
  4079. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  4080. ret = WOLFSSL_FATAL_ERROR;
  4081. }
  4082. }
  4083. }
  4084. wc_Sha3_224_Free(&sha3);
  4085. printf(resultFmt, ret == 0 ? passed : failed);
  4086. #endif /* SHA3_224 */
  4087. #if !defined(WOLFSSL_NOSHA3_256)
  4088. if (ret == 0) {
  4089. printf(testingFmt, "wc_Sha3_256_Update()");
  4090. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  4091. if (ret != 0) {
  4092. return ret;
  4093. }
  4094. ret = wc_Sha3_256_Update(&sha3, msg, msglen);
  4095. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  4096. ret = WOLFSSL_FATAL_ERROR;
  4097. }
  4098. if (ret == 0) {
  4099. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  4100. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  4101. ret = WOLFSSL_FATAL_ERROR;
  4102. }
  4103. }
  4104. /* Pass bad args. */
  4105. if (ret == 0) {
  4106. ret = wc_Sha3_256_Update(NULL, msg2, msg2len);
  4107. if (ret == BAD_FUNC_ARG) {
  4108. ret = wc_Sha3_256_Update(&sha3, NULL, 5);
  4109. }
  4110. if (ret == BAD_FUNC_ARG) {
  4111. wc_Sha3_256_Free(&sha3);
  4112. if (wc_InitSha3_256(&sha3, HEAP_HINT, devId)) {
  4113. return ret;
  4114. }
  4115. ret = wc_Sha3_256_Update(&sha3, NULL, 0);
  4116. if (ret == 0) {
  4117. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  4118. }
  4119. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  4120. ret = WOLFSSL_FATAL_ERROR;
  4121. }
  4122. }
  4123. }
  4124. wc_Sha3_256_Free(&sha3);
  4125. printf(resultFmt, ret == 0 ? passed : failed);
  4126. }
  4127. #endif /* SHA3_256 */
  4128. #if !defined(WOLFSSL_NOSHA3_384)
  4129. if (ret == 0) {
  4130. printf(testingFmt, "wc_Sha3_384_Update()");
  4131. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  4132. if (ret != 0) {
  4133. return ret;
  4134. }
  4135. ret = wc_Sha3_384_Update(&sha3, msg, msglen);
  4136. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  4137. ret = WOLFSSL_FATAL_ERROR;
  4138. }
  4139. if (ret == 0) {
  4140. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  4141. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  4142. ret = WOLFSSL_FATAL_ERROR;
  4143. }
  4144. }
  4145. /* Pass bad args. */
  4146. if (ret == 0) {
  4147. ret = wc_Sha3_384_Update(NULL, msg2, msg2len);
  4148. if (ret == BAD_FUNC_ARG) {
  4149. ret = wc_Sha3_384_Update(&sha3, NULL, 5);
  4150. }
  4151. if (ret == BAD_FUNC_ARG) {
  4152. wc_Sha3_384_Free(&sha3);
  4153. if (wc_InitSha3_384(&sha3, HEAP_HINT, devId)) {
  4154. return ret;
  4155. }
  4156. ret = wc_Sha3_384_Update(&sha3, NULL, 0);
  4157. if (ret == 0) {
  4158. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  4159. }
  4160. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  4161. ret = WOLFSSL_FATAL_ERROR;
  4162. }
  4163. }
  4164. }
  4165. wc_Sha3_384_Free(&sha3);
  4166. printf(resultFmt, ret == 0 ? passed : failed);
  4167. }
  4168. #endif /* SHA3_384 */
  4169. #if !defined(WOLFSSL_NOSHA3_512)
  4170. if (ret == 0) {
  4171. printf(testingFmt, "wc_Sha3_512_Update()");
  4172. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  4173. if (ret != 0) {
  4174. return ret;
  4175. }
  4176. ret = wc_Sha3_512_Update(&sha3, msg, msglen);
  4177. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  4178. ret = WOLFSSL_FATAL_ERROR;
  4179. }
  4180. if (ret == 0) {
  4181. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  4182. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  4183. ret = WOLFSSL_FATAL_ERROR;
  4184. }
  4185. }
  4186. /* Pass bad args. */
  4187. if (ret == 0) {
  4188. ret = wc_Sha3_512_Update(NULL, msg2, msg2len);
  4189. if (ret == BAD_FUNC_ARG) {
  4190. ret = wc_Sha3_512_Update(&sha3, NULL, 5);
  4191. }
  4192. if (ret == BAD_FUNC_ARG) {
  4193. wc_Sha3_512_Free(&sha3);
  4194. if (wc_InitSha3_512(&sha3, HEAP_HINT, devId)) {
  4195. return ret;
  4196. }
  4197. ret = wc_Sha3_512_Update(&sha3, NULL, 0);
  4198. if (ret == 0) {
  4199. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  4200. }
  4201. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  4202. ret = WOLFSSL_FATAL_ERROR;
  4203. }
  4204. }
  4205. }
  4206. wc_Sha3_512_Free(&sha3);
  4207. printf(resultFmt, ret == 0 ? passed : failed);
  4208. }
  4209. #endif /* SHA3_512 */
  4210. #endif /* WOLFSSL_SHA3 */
  4211. return ret;
  4212. } /* END testing_wc_Sha3_Update */
  4213. /*
  4214. * Testing wc_Sha3_224_Final()
  4215. */
  4216. static int test_wc_Sha3_224_Final (void)
  4217. {
  4218. int ret = 0;
  4219. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  4220. wc_Sha3 sha3;
  4221. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  4222. "nopnopq";
  4223. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  4224. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  4225. "\x64\xea\xd0\xfc\xce\x33";
  4226. byte hash[WC_SHA3_224_DIGEST_SIZE];
  4227. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  4228. /* Init stack variables. */
  4229. XMEMSET(hash, 0, sizeof(hash));
  4230. printf(testingFmt, "wc_Sha3_224_Final()");
  4231. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  4232. if (ret != 0) {
  4233. return ret;
  4234. }
  4235. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  4236. if (ret == 0) {
  4237. ret = wc_Sha3_224_Final(&sha3, hash);
  4238. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE) != 0) {
  4239. ret = WOLFSSL_FATAL_ERROR;
  4240. }
  4241. }
  4242. /* Test bad args. */
  4243. if (ret == 0) {
  4244. ret = wc_Sha3_224_Final(NULL, hash);
  4245. if (ret == 0) {
  4246. ret = wc_Sha3_224_Final(&sha3, NULL);
  4247. }
  4248. if (ret == BAD_FUNC_ARG) {
  4249. ret = 0;
  4250. } else if (ret == 0) {
  4251. ret = WOLFSSL_FATAL_ERROR;
  4252. }
  4253. }
  4254. printf(resultFmt, ret == 0 ? passed : failed);
  4255. if (ret == 0) {
  4256. printf(testingFmt, "wc_Sha3_224_GetHash()");
  4257. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  4258. if (ret != 0) {
  4259. return ret;
  4260. }
  4261. /* Init stack variables. */
  4262. XMEMSET(hash, 0, sizeof(hash));
  4263. XMEMSET(hashRet, 0, sizeof(hashRet));
  4264. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  4265. if (ret == 0) {
  4266. ret = wc_Sha3_224_GetHash(&sha3, hashRet);
  4267. }
  4268. if (ret == 0) {
  4269. ret = wc_Sha3_224_Final(&sha3, hash);
  4270. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE) != 0) {
  4271. ret = WOLFSSL_FATAL_ERROR;
  4272. }
  4273. }
  4274. if (ret == 0) {
  4275. /* Test bad args. */
  4276. ret = wc_Sha3_224_GetHash(NULL, hashRet);
  4277. if (ret == BAD_FUNC_ARG) {
  4278. ret = wc_Sha3_224_GetHash(&sha3, NULL);
  4279. }
  4280. if (ret == BAD_FUNC_ARG) {
  4281. ret = 0;
  4282. } else if (ret == 0) {
  4283. ret = WOLFSSL_FATAL_ERROR;
  4284. }
  4285. }
  4286. printf(resultFmt, ret == 0 ? passed : failed);
  4287. }
  4288. wc_Sha3_224_Free(&sha3);
  4289. #endif
  4290. return ret;
  4291. } /* END test_wc_Sha3_224_Final */
  4292. /*
  4293. * Testing wc_Sha3_256_Final()
  4294. */
  4295. static int test_wc_Sha3_256_Final (void)
  4296. {
  4297. int ret = 0;
  4298. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  4299. wc_Sha3 sha3;
  4300. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  4301. "nopnopq";
  4302. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  4303. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  4304. "\xdd\x97\x49\x6d\x33\x76";
  4305. byte hash[WC_SHA3_256_DIGEST_SIZE];
  4306. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  4307. /* Init stack variables. */
  4308. XMEMSET(hash, 0, sizeof(hash));
  4309. printf(testingFmt, "wc_Sha3_256_Final()");
  4310. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  4311. if (ret != 0) {
  4312. return ret;
  4313. }
  4314. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  4315. if (ret == 0) {
  4316. ret = wc_Sha3_256_Final(&sha3, hash);
  4317. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE) != 0) {
  4318. ret = WOLFSSL_FATAL_ERROR;
  4319. }
  4320. }
  4321. /* Test bad args. */
  4322. if (ret == 0) {
  4323. ret = wc_Sha3_256_Final(NULL, hash);
  4324. if (ret == 0) {
  4325. ret = wc_Sha3_256_Final(&sha3, NULL);
  4326. }
  4327. if (ret == BAD_FUNC_ARG) {
  4328. ret = 0;
  4329. } else if (ret == 0) {
  4330. ret = WOLFSSL_FATAL_ERROR;
  4331. }
  4332. }
  4333. printf(resultFmt, ret == 0 ? passed : failed);
  4334. if (ret == 0) {
  4335. printf(testingFmt, "wc_Sha3_256_GetHash()");
  4336. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  4337. if (ret != 0) {
  4338. return ret;
  4339. }
  4340. /* Init stack variables. */
  4341. XMEMSET(hash, 0, sizeof(hash));
  4342. XMEMSET(hashRet, 0, sizeof(hashRet));
  4343. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  4344. if (ret == 0) {
  4345. ret = wc_Sha3_256_GetHash(&sha3, hashRet);
  4346. }
  4347. if (ret == 0) {
  4348. ret = wc_Sha3_256_Final(&sha3, hash);
  4349. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE) != 0) {
  4350. ret = WOLFSSL_FATAL_ERROR;
  4351. }
  4352. }
  4353. if (ret == 0) {
  4354. /* Test bad args. */
  4355. ret = wc_Sha3_256_GetHash(NULL, hashRet);
  4356. if (ret == BAD_FUNC_ARG) {
  4357. ret = wc_Sha3_256_GetHash(&sha3, NULL);
  4358. }
  4359. if (ret == BAD_FUNC_ARG) {
  4360. ret = 0;
  4361. } else if (ret == 0) {
  4362. ret = WOLFSSL_FATAL_ERROR;
  4363. }
  4364. }
  4365. printf(resultFmt, ret == 0 ? passed : failed);
  4366. }
  4367. wc_Sha3_256_Free(&sha3);
  4368. #endif
  4369. return ret;
  4370. } /* END test_wc_Sha3_256_Final */
  4371. /*
  4372. * Testing wc_Sha3_384_Final()
  4373. */
  4374. static int test_wc_Sha3_384_Final (void)
  4375. {
  4376. int ret = 0;
  4377. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  4378. wc_Sha3 sha3;
  4379. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  4380. "nopnopq";
  4381. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  4382. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  4383. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  4384. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  4385. byte hash[WC_SHA3_384_DIGEST_SIZE];
  4386. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  4387. /* Init stack variables. */
  4388. XMEMSET(hash, 0, sizeof(hash));
  4389. printf(testingFmt, "wc_Sha3_384_Final()");
  4390. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  4391. if (ret != 0) {
  4392. return ret;
  4393. }
  4394. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  4395. if (ret == 0) {
  4396. ret = wc_Sha3_384_Final(&sha3, hash);
  4397. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE) != 0) {
  4398. ret = WOLFSSL_FATAL_ERROR;
  4399. }
  4400. }
  4401. /* Test bad args. */
  4402. if (ret == 0) {
  4403. ret = wc_Sha3_384_Final(NULL, hash);
  4404. if (ret == 0) {
  4405. ret = wc_Sha3_384_Final(&sha3, NULL);
  4406. }
  4407. if (ret == BAD_FUNC_ARG) {
  4408. ret = 0;
  4409. } else if (ret == 0) {
  4410. ret = WOLFSSL_FATAL_ERROR;
  4411. }
  4412. }
  4413. printf(resultFmt, ret == 0 ? passed : failed);
  4414. if (ret == 0) {
  4415. printf(testingFmt, "wc_Sha3_384_GetHash()");
  4416. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  4417. if (ret != 0) {
  4418. return ret;
  4419. }
  4420. /* Init stack variables. */
  4421. XMEMSET(hash, 0, sizeof(hash));
  4422. XMEMSET(hashRet, 0, sizeof(hashRet));
  4423. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  4424. if (ret == 0) {
  4425. ret = wc_Sha3_384_GetHash(&sha3, hashRet);
  4426. }
  4427. if (ret == 0) {
  4428. ret = wc_Sha3_384_Final(&sha3, hash);
  4429. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE) != 0) {
  4430. ret = WOLFSSL_FATAL_ERROR;
  4431. }
  4432. }
  4433. if (ret == 0) {
  4434. /* Test bad args. */
  4435. ret = wc_Sha3_384_GetHash(NULL, hashRet);
  4436. if (ret == BAD_FUNC_ARG) {
  4437. ret = wc_Sha3_384_GetHash(&sha3, NULL);
  4438. }
  4439. if (ret == BAD_FUNC_ARG) {
  4440. ret = 0;
  4441. } else if (ret == 0) {
  4442. ret = WOLFSSL_FATAL_ERROR;
  4443. }
  4444. }
  4445. printf(resultFmt, ret == 0 ? passed : failed);
  4446. }
  4447. wc_Sha3_384_Free(&sha3);
  4448. #endif
  4449. return ret;
  4450. } /* END test_wc_Sha3_384_Final */
  4451. /*
  4452. * Testing wc_Sha3_512_Final()
  4453. */
  4454. static int test_wc_Sha3_512_Final (void)
  4455. {
  4456. int ret = 0;
  4457. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  4458. wc_Sha3 sha3;
  4459. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  4460. "nopnopq";
  4461. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  4462. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  4463. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  4464. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  4465. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  4466. byte hash[WC_SHA3_512_DIGEST_SIZE];
  4467. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  4468. /* Init stack variables. */
  4469. XMEMSET(hash, 0, sizeof(hash));
  4470. printf(testingFmt, "wc_Sha3_512_Final()");
  4471. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  4472. if (ret != 0) {
  4473. return ret;
  4474. }
  4475. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  4476. if (ret == 0) {
  4477. ret = wc_Sha3_512_Final(&sha3, hash);
  4478. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE) != 0) {
  4479. ret = WOLFSSL_FATAL_ERROR;
  4480. }
  4481. }
  4482. /* Test bad args. */
  4483. if (ret == 0) {
  4484. ret = wc_Sha3_512_Final(NULL, hash);
  4485. if (ret == 0) {
  4486. ret = wc_Sha3_384_Final(&sha3, NULL);
  4487. }
  4488. if (ret == BAD_FUNC_ARG) {
  4489. ret = 0;
  4490. } else if (ret == 0) {
  4491. ret = WOLFSSL_FATAL_ERROR;
  4492. }
  4493. }
  4494. printf(resultFmt, ret == 0 ? passed : failed);
  4495. if (ret == 0) {
  4496. printf(testingFmt, "wc_Sha3_512_GetHash()");
  4497. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  4498. if (ret != 0) {
  4499. return ret;
  4500. }
  4501. /* Init stack variables. */
  4502. XMEMSET(hash, 0, sizeof(hash));
  4503. XMEMSET(hashRet, 0, sizeof(hashRet));
  4504. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  4505. if (ret == 0) {
  4506. ret = wc_Sha3_512_GetHash(&sha3, hashRet);
  4507. }
  4508. if (ret == 0) {
  4509. ret = wc_Sha3_512_Final(&sha3, hash);
  4510. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE) != 0) {
  4511. ret = WOLFSSL_FATAL_ERROR;
  4512. }
  4513. }
  4514. if (ret == 0) {
  4515. /* Test bad args. */
  4516. ret = wc_Sha3_512_GetHash(NULL, hashRet);
  4517. if (ret == BAD_FUNC_ARG) {
  4518. ret = wc_Sha3_512_GetHash(&sha3, NULL);
  4519. }
  4520. if (ret == BAD_FUNC_ARG) {
  4521. ret = 0;
  4522. } else if (ret == 0) {
  4523. ret = WOLFSSL_FATAL_ERROR;
  4524. }
  4525. }
  4526. printf(resultFmt, ret == 0 ? passed : failed);
  4527. }
  4528. wc_Sha3_512_Free(&sha3);
  4529. #endif
  4530. return ret;
  4531. } /* END test_wc_Sha3_512_Final */
  4532. /*
  4533. * Testing wc_Sha3_224_Copy()
  4534. */
  4535. static int test_wc_Sha3_224_Copy (void)
  4536. {
  4537. int ret = 0;
  4538. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  4539. wc_Sha3 sha3, sha3Cpy;
  4540. const char* msg = "Everyone gets Friday off.";
  4541. word32 msglen = (word32)XSTRLEN(msg);
  4542. byte hash[WC_SHA3_224_DIGEST_SIZE];
  4543. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  4544. XMEMSET(hash, 0, sizeof(hash));
  4545. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  4546. printf(testingFmt, "wc_Sha3_224_Copy()");
  4547. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  4548. if (ret != 0) {
  4549. return ret;
  4550. }
  4551. ret = wc_InitSha3_224(&sha3Cpy, HEAP_HINT, devId);
  4552. if (ret != 0) {
  4553. wc_Sha3_224_Free(&sha3);
  4554. return ret;
  4555. }
  4556. ret = wc_Sha3_224_Update(&sha3, (byte*)msg, msglen);
  4557. if (ret == 0) {
  4558. ret = wc_Sha3_224_Copy(&sha3Cpy, &sha3);
  4559. if (ret == 0) {
  4560. ret = wc_Sha3_224_Final(&sha3, hash);
  4561. if (ret == 0) {
  4562. ret = wc_Sha3_224_Final(&sha3Cpy, hashCpy);
  4563. }
  4564. }
  4565. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  4566. ret = WOLFSSL_FATAL_ERROR;
  4567. }
  4568. }
  4569. /* Test bad args. */
  4570. if (ret == 0) {
  4571. ret = wc_Sha3_224_Copy(NULL, &sha3);
  4572. if (ret == BAD_FUNC_ARG) {
  4573. ret = wc_Sha3_224_Copy(&sha3Cpy, NULL);
  4574. }
  4575. if (ret == BAD_FUNC_ARG) {
  4576. ret = 0;
  4577. } else if (ret == 0) {
  4578. ret = WOLFSSL_FATAL_ERROR;
  4579. }
  4580. }
  4581. printf(resultFmt, ret == 0 ? passed : failed);
  4582. #endif
  4583. return ret;
  4584. } /* END test_wc_Sha3_224_Copy */
  4585. /*
  4586. * Testing wc_Sha3_256_Copy()
  4587. */
  4588. static int test_wc_Sha3_256_Copy (void)
  4589. {
  4590. int ret = 0;
  4591. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  4592. wc_Sha3 sha3, sha3Cpy;
  4593. const char* msg = "Everyone gets Friday off.";
  4594. word32 msglen = (word32)XSTRLEN(msg);
  4595. byte hash[WC_SHA3_256_DIGEST_SIZE];
  4596. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  4597. XMEMSET(hash, 0, sizeof(hash));
  4598. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  4599. printf(testingFmt, "wc_Sha3_256_Copy()");
  4600. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  4601. if (ret != 0) {
  4602. return ret;
  4603. }
  4604. ret = wc_InitSha3_256(&sha3Cpy, HEAP_HINT, devId);
  4605. if (ret != 0) {
  4606. wc_Sha3_256_Free(&sha3);
  4607. return ret;
  4608. }
  4609. ret = wc_Sha3_256_Update(&sha3, (byte*)msg, msglen);
  4610. if (ret == 0) {
  4611. ret = wc_Sha3_256_Copy(&sha3Cpy, &sha3);
  4612. if (ret == 0) {
  4613. ret = wc_Sha3_256_Final(&sha3, hash);
  4614. if (ret == 0) {
  4615. ret = wc_Sha3_256_Final(&sha3Cpy, hashCpy);
  4616. }
  4617. }
  4618. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  4619. ret = WOLFSSL_FATAL_ERROR;
  4620. }
  4621. }
  4622. /* Test bad args. */
  4623. if (ret == 0) {
  4624. ret = wc_Sha3_256_Copy(NULL, &sha3);
  4625. if (ret == BAD_FUNC_ARG) {
  4626. ret = wc_Sha3_256_Copy(&sha3Cpy, NULL);
  4627. }
  4628. if (ret == BAD_FUNC_ARG) {
  4629. ret = 0;
  4630. } else if (ret == 0) {
  4631. ret = WOLFSSL_FATAL_ERROR;
  4632. }
  4633. }
  4634. printf(resultFmt, ret == 0 ? passed : failed);
  4635. #endif
  4636. return ret;
  4637. } /* END test_wc_Sha3_256_Copy */
  4638. /*
  4639. * Testing wc_Sha3_384_Copy()
  4640. */
  4641. static int test_wc_Sha3_384_Copy (void)
  4642. {
  4643. int ret = 0;
  4644. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  4645. wc_Sha3 sha3, sha3Cpy;
  4646. const char* msg = "Everyone gets Friday off.";
  4647. word32 msglen = (word32)XSTRLEN(msg);
  4648. byte hash[WC_SHA3_384_DIGEST_SIZE];
  4649. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  4650. XMEMSET(hash, 0, sizeof(hash));
  4651. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  4652. printf(testingFmt, "wc_Sha3_384_Copy()");
  4653. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  4654. if (ret != 0) {
  4655. return ret;
  4656. }
  4657. ret = wc_InitSha3_384(&sha3Cpy, HEAP_HINT, devId);
  4658. if (ret != 0) {
  4659. wc_Sha3_384_Free(&sha3);
  4660. return ret;
  4661. }
  4662. ret = wc_Sha3_384_Update(&sha3, (byte*)msg, msglen);
  4663. if (ret == 0) {
  4664. ret = wc_Sha3_384_Copy(&sha3Cpy, &sha3);
  4665. if (ret == 0) {
  4666. ret = wc_Sha3_384_Final(&sha3, hash);
  4667. if (ret == 0) {
  4668. ret = wc_Sha3_384_Final(&sha3Cpy, hashCpy);
  4669. }
  4670. }
  4671. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  4672. ret = WOLFSSL_FATAL_ERROR;
  4673. }
  4674. }
  4675. /* Test bad args. */
  4676. if (ret == 0) {
  4677. ret = wc_Sha3_384_Copy(NULL, &sha3);
  4678. if (ret == BAD_FUNC_ARG) {
  4679. ret = wc_Sha3_384_Copy(&sha3Cpy, NULL);
  4680. }
  4681. if (ret == BAD_FUNC_ARG) {
  4682. ret = 0;
  4683. } else if (ret == 0) {
  4684. ret = WOLFSSL_FATAL_ERROR;
  4685. }
  4686. }
  4687. printf(resultFmt, ret == 0 ? passed : failed);
  4688. #endif
  4689. return ret;
  4690. } /* END test_wc_Sha3_384_Copy */
  4691. /*
  4692. * Testing wc_Sha3_512_Copy()
  4693. */
  4694. static int test_wc_Sha3_512_Copy (void)
  4695. {
  4696. int ret = 0;
  4697. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  4698. wc_Sha3 sha3, sha3Cpy;
  4699. const char* msg = "Everyone gets Friday off.";
  4700. word32 msglen = (word32)XSTRLEN(msg);
  4701. byte hash[WC_SHA3_512_DIGEST_SIZE];
  4702. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  4703. XMEMSET(hash, 0, sizeof(hash));
  4704. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  4705. printf(testingFmt, "wc_Sha3_512_Copy()");
  4706. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  4707. if (ret != 0) {
  4708. return ret;
  4709. }
  4710. ret = wc_InitSha3_512(&sha3Cpy, HEAP_HINT, devId);
  4711. if (ret != 0) {
  4712. wc_Sha3_512_Free(&sha3);
  4713. return ret;
  4714. }
  4715. ret = wc_Sha3_512_Update(&sha3, (byte*)msg, msglen);
  4716. if (ret == 0) {
  4717. ret = wc_Sha3_512_Copy(&sha3Cpy, &sha3);
  4718. if (ret == 0) {
  4719. ret = wc_Sha3_512_Final(&sha3, hash);
  4720. if (ret == 0) {
  4721. ret = wc_Sha3_512_Final(&sha3Cpy, hashCpy);
  4722. }
  4723. }
  4724. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  4725. ret = WOLFSSL_FATAL_ERROR;
  4726. }
  4727. }
  4728. /* Test bad args. */
  4729. if (ret == 0) {
  4730. ret = wc_Sha3_512_Copy(NULL, &sha3);
  4731. if (ret == BAD_FUNC_ARG) {
  4732. ret = wc_Sha3_512_Copy(&sha3Cpy, NULL);
  4733. }
  4734. if (ret == BAD_FUNC_ARG) {
  4735. ret = 0;
  4736. } else if (ret == 0) {
  4737. ret = WOLFSSL_FATAL_ERROR;
  4738. }
  4739. }
  4740. printf(resultFmt, ret == 0 ? passed : failed);
  4741. #endif
  4742. return ret;
  4743. } /* END test_wc_Sha3_512_Copy */
  4744. /*
  4745. * unit test for wc_IdeaSetKey()
  4746. */
  4747. static int test_wc_IdeaSetKey (void)
  4748. {
  4749. int ret = 0;
  4750. #ifdef HAVE_IDEA
  4751. Idea idea;
  4752. const byte key[] =
  4753. {
  4754. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
  4755. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37
  4756. };
  4757. int flag = 0;
  4758. printf(testingFmt, "wc_IdeaSetKey()");
  4759. /*IV can be NULL, default value is 0*/
  4760. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
  4761. if (ret == 0) {
  4762. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_DECRYPTION);
  4763. }
  4764. /* Bad args. */
  4765. if (ret == 0) {
  4766. ret = wc_IdeaSetKey(NULL, key, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
  4767. if (ret != BAD_FUNC_ARG) {
  4768. flag = 1;
  4769. }
  4770. ret = wc_IdeaSetKey(&idea, NULL, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
  4771. if (ret != BAD_FUNC_ARG) {
  4772. flag = 1;
  4773. }
  4774. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE - 1,
  4775. NULL, IDEA_ENCRYPTION);
  4776. if (ret != BAD_FUNC_ARG) {
  4777. flag = 1;
  4778. }
  4779. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, -1);
  4780. if (ret != BAD_FUNC_ARG) {
  4781. flag = 1;
  4782. }
  4783. if (flag == 1) {
  4784. ret = WOLFSSL_FATAL_ERROR;
  4785. } else {
  4786. ret = 0;
  4787. }
  4788. } /* END Test Bad Args. */
  4789. printf(resultFmt, ret == 0 ? passed : failed);
  4790. #endif
  4791. return ret;
  4792. } /* END test_wc_IdeaSetKey */
  4793. /*
  4794. * Unit test for wc_IdeaSetIV()
  4795. */
  4796. static int test_wc_IdeaSetIV (void)
  4797. {
  4798. int ret = 0;
  4799. #ifdef HAVE_IDEA
  4800. Idea idea;
  4801. printf(testingFmt, "wc_IdeaSetIV()");
  4802. ret = wc_IdeaSetIV(&idea, NULL);
  4803. /* Test bad args. */
  4804. if (ret == 0) {
  4805. ret = wc_IdeaSetIV(NULL, NULL);
  4806. if (ret == BAD_FUNC_ARG) {
  4807. ret = 0;
  4808. } else {
  4809. ret = WOLFSSL_FATAL_ERROR;
  4810. }
  4811. }
  4812. printf(resultFmt, ret == 0 ? passed : failed);
  4813. #endif
  4814. return ret;
  4815. } /* END test_wc_IdeaSetIV */
  4816. /*
  4817. * Unit test for wc_IdeaCipher()
  4818. */
  4819. static int test_wc_IdeaCipher (void)
  4820. {
  4821. int ret = 0;
  4822. #ifdef HAVE_IDEA
  4823. Idea idea;
  4824. const byte key[] =
  4825. {
  4826. 0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
  4827. 0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48
  4828. };
  4829. const byte plain[] =
  4830. {
  4831. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37
  4832. };
  4833. byte enc[sizeof(plain)];
  4834. byte dec[sizeof(enc)];
  4835. printf(testingFmt, "wc_IdeaCipher()");
  4836. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
  4837. if (ret == 0) {
  4838. ret = wc_IdeaCipher(&idea, enc, plain);
  4839. if (ret != 0) {
  4840. ret = WOLFSSL_FATAL_ERROR;
  4841. }
  4842. }
  4843. if (ret == 0) {
  4844. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_DECRYPTION);
  4845. if (ret == 0) {
  4846. ret = wc_IdeaCipher(&idea, dec, enc);
  4847. }
  4848. if (ret == 0) {
  4849. ret = XMEMCMP(plain, dec, IDEA_BLOCK_SIZE);
  4850. }
  4851. if (ret != 0) {
  4852. ret = WOLFSSL_FATAL_ERROR;
  4853. }
  4854. }
  4855. /* Pass Bad Args. */
  4856. if (ret == 0) {
  4857. ret = wc_IdeaCipher(NULL, enc, dec);
  4858. if (ret == BAD_FUNC_ARG) {
  4859. ret = wc_IdeaCipher(&idea, NULL, dec);
  4860. }
  4861. if (ret == BAD_FUNC_ARG) {
  4862. ret = wc_IdeaCipher(&idea, enc, NULL);
  4863. }
  4864. if (ret == BAD_FUNC_ARG) {
  4865. ret = 0;
  4866. } else {
  4867. ret = WOLFSSL_FATAL_ERROR;
  4868. }
  4869. }
  4870. printf(resultFmt, ret == 0 ? passed : failed);
  4871. #endif
  4872. return ret;
  4873. } /* END test_wc_IdeaCipher */
  4874. /*
  4875. * Unit test for functions wc_IdeaCbcEncrypt and wc_IdeaCbcDecrypt
  4876. */
  4877. static int test_wc_IdeaCbcEncyptDecrypt (void)
  4878. {
  4879. int ret = 0;
  4880. #ifdef HAVE_IDEA
  4881. Idea idea;
  4882. const byte key[] =
  4883. {
  4884. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
  4885. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37
  4886. };
  4887. const char* message = "International Data Encryption Algorithm";
  4888. byte msg_enc[40];
  4889. byte msg_dec[40];
  4890. printf(testingFmt, "wc_IdeaCbcEncrypt()");
  4891. ret = wc_IdeaSetKey(&idea, key, sizeof(key), NULL, IDEA_ENCRYPTION);
  4892. if (ret == 0) {
  4893. ret = wc_IdeaCbcEncrypt(&idea, msg_enc, (byte *)message,
  4894. (word32)XSTRLEN(message) + 1);
  4895. }
  4896. if (ret == 0) {
  4897. ret = wc_IdeaSetKey(&idea, key, sizeof(key), NULL, IDEA_DECRYPTION);
  4898. }
  4899. if (ret == 0) {
  4900. ret = wc_IdeaCbcDecrypt(&idea, msg_dec, msg_enc,
  4901. (word32)XSTRLEN(message) + 1);
  4902. if (XMEMCMP(message, msg_dec, (word32)XSTRLEN(message))) {
  4903. ret = WOLFSSL_FATAL_ERROR;
  4904. }
  4905. }
  4906. /* Test bad args. Enc */
  4907. if (ret == 0) {
  4908. ret = wc_IdeaCbcEncrypt(NULL, msg_enc, (byte*)message,
  4909. (word32)XSTRLEN(message) + 1);
  4910. if (ret == BAD_FUNC_ARG) {
  4911. ret = wc_IdeaCbcEncrypt(&idea, NULL, (byte*)message,
  4912. (word32)XSTRLEN(message) + 1);
  4913. }
  4914. if (ret == BAD_FUNC_ARG) {
  4915. ret = wc_IdeaCbcEncrypt(&idea, msg_enc, NULL,
  4916. (word32)XSTRLEN(message) + 1);
  4917. }
  4918. if (ret != BAD_FUNC_ARG) {
  4919. ret = WOLFSSL_FATAL_ERROR;
  4920. } else {
  4921. ret = 0;
  4922. }
  4923. } /* END test bad args ENC */
  4924. /* Test bad args DEC */
  4925. if (ret == 0) {
  4926. ret = wc_IdeaCbcDecrypt(NULL, msg_dec, msg_enc,
  4927. (word32)XSTRLEN(message) + 1);
  4928. if (ret == BAD_FUNC_ARG) {
  4929. ret = wc_IdeaCbcDecrypt(&idea, NULL, msg_enc,
  4930. (word32)XSTRLEN(message) + 1);
  4931. }
  4932. if (ret == BAD_FUNC_ARG) {
  4933. ret = wc_IdeaCbcDecrypt(&idea, msg_dec, NULL,
  4934. (word32)XSTRLEN(message) + 1);
  4935. }
  4936. if (ret != BAD_FUNC_ARG) {
  4937. ret = WOLFSSL_FATAL_ERROR;
  4938. } else {
  4939. ret = 0;
  4940. }
  4941. }
  4942. printf(resultFmt, ret == 0 ? passed : failed);
  4943. #endif
  4944. return ret;
  4945. } /* END test_wc_IdeaCbcEncryptDecrypt */
  4946. /*
  4947. * Test function for wc_HmacSetKey
  4948. */
  4949. static int test_wc_Md5HmacSetKey (void)
  4950. {
  4951. int flag = 0;
  4952. #if !defined(NO_HMAC) && !defined(NO_MD5)
  4953. Hmac hmac;
  4954. int ret, times, itr;
  4955. const char* keys[]=
  4956. {
  4957. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  4958. #ifndef HAVE_FIPS
  4959. "Jefe", /* smaller than minumum FIPS key size */
  4960. #endif
  4961. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4962. };
  4963. times = sizeof(keys) / sizeof(char*);
  4964. flag = 0;
  4965. printf(testingFmt, "wc_HmacSetKey() with MD5");
  4966. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  4967. if (ret != 0)
  4968. return ret;
  4969. for (itr = 0; itr < times; itr++) {
  4970. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  4971. (word32)XSTRLEN(keys[itr]));
  4972. if (ret != 0) {
  4973. flag = ret;
  4974. }
  4975. }
  4976. /* Bad args. */
  4977. if (!flag) {
  4978. ret = wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  4979. (word32)XSTRLEN(keys[0]));
  4980. if (ret != BAD_FUNC_ARG) {
  4981. flag = WOLFSSL_FATAL_ERROR;
  4982. }
  4983. }
  4984. if (!flag) {
  4985. ret = wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0]));
  4986. if (ret != BAD_FUNC_ARG) {
  4987. flag = WOLFSSL_FATAL_ERROR;
  4988. }
  4989. }
  4990. if (!flag) {
  4991. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  4992. (word32)XSTRLEN(keys[0]));
  4993. if (ret != BAD_FUNC_ARG) {
  4994. flag = WOLFSSL_FATAL_ERROR;
  4995. }
  4996. }
  4997. if (!flag) {
  4998. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  4999. #ifdef HAVE_FIPS
  5000. if (ret != HMAC_MIN_KEYLEN_E) {
  5001. flag = WOLFSSL_FATAL_ERROR;
  5002. }
  5003. #else
  5004. if (ret != 0) {
  5005. flag = WOLFSSL_FATAL_ERROR;
  5006. }
  5007. #endif
  5008. }
  5009. wc_HmacFree(&hmac);
  5010. printf(resultFmt, flag == 0 ? passed : failed);
  5011. #endif
  5012. return flag;
  5013. } /* END test_wc_Md5HmacSetKey */
  5014. /*
  5015. * testing wc_HmacSetKey() on wc_Sha hash.
  5016. */
  5017. static int test_wc_ShaHmacSetKey (void)
  5018. {
  5019. int flag = 0;
  5020. #if !defined(NO_HMAC) && !defined(NO_SHA)
  5021. Hmac hmac;
  5022. int ret, times, itr;
  5023. const char* keys[]=
  5024. {
  5025. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5026. "\x0b\x0b\x0b",
  5027. #ifndef HAVE_FIPS
  5028. "Jefe", /* smaller than minumum FIPS key size */
  5029. #endif
  5030. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5031. "\xAA\xAA\xAA"
  5032. };
  5033. times = sizeof(keys) / sizeof(char*);
  5034. flag = 0;
  5035. printf(testingFmt, "wc_HmacSetKey() with SHA");
  5036. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  5037. if (ret != 0)
  5038. return ret;
  5039. for (itr = 0; itr < times; itr++) {
  5040. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  5041. (word32)XSTRLEN(keys[itr]));
  5042. if (ret != 0) {
  5043. flag = ret;
  5044. }
  5045. }
  5046. /* Bad args. */
  5047. if (!flag) {
  5048. ret = wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  5049. (word32)XSTRLEN(keys[0]));
  5050. if (ret != BAD_FUNC_ARG) {
  5051. flag = WOLFSSL_FATAL_ERROR;
  5052. }
  5053. }
  5054. if (!flag) {
  5055. ret = wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0]));
  5056. if (ret != BAD_FUNC_ARG) {
  5057. flag = WOLFSSL_FATAL_ERROR;
  5058. }
  5059. }
  5060. if (!flag) {
  5061. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  5062. (word32)XSTRLEN(keys[0]));
  5063. if (ret != BAD_FUNC_ARG) {
  5064. flag = WOLFSSL_FATAL_ERROR;
  5065. }
  5066. }
  5067. if (!flag) {
  5068. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  5069. #ifdef HAVE_FIPS
  5070. if (ret != HMAC_MIN_KEYLEN_E) {
  5071. flag = WOLFSSL_FATAL_ERROR;
  5072. }
  5073. #else
  5074. if (ret != 0) {
  5075. flag = WOLFSSL_FATAL_ERROR;
  5076. }
  5077. #endif
  5078. }
  5079. wc_HmacFree(&hmac);
  5080. printf(resultFmt, flag == 0 ? passed : failed);
  5081. #endif
  5082. return flag;
  5083. } /* END test_wc_ShaHmacSetKey() */
  5084. /*
  5085. * testing wc_HmacSetKey() on Sha224 hash.
  5086. */
  5087. static int test_wc_Sha224HmacSetKey (void)
  5088. {
  5089. int flag = 0;
  5090. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  5091. Hmac hmac;
  5092. int ret, times, itr;
  5093. const char* keys[]=
  5094. {
  5095. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5096. "\x0b\x0b\x0b",
  5097. #ifndef HAVE_FIPS
  5098. "Jefe", /* smaller than minumum FIPS key size */
  5099. #endif
  5100. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5101. "\xAA\xAA\xAA"
  5102. };
  5103. times = sizeof(keys) / sizeof(char*);
  5104. flag = 0;
  5105. printf(testingFmt, "wc_HmacSetKey() with SHA 224");
  5106. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  5107. if (ret != 0)
  5108. return ret;
  5109. for (itr = 0; itr < times; itr++) {
  5110. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  5111. (word32)XSTRLEN(keys[itr]));
  5112. if (ret != 0) {
  5113. flag = ret;
  5114. }
  5115. }
  5116. /* Bad args. */
  5117. if (!flag) {
  5118. ret = wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  5119. (word32)XSTRLEN(keys[0]));
  5120. if (ret != BAD_FUNC_ARG) {
  5121. flag = WOLFSSL_FATAL_ERROR;
  5122. }
  5123. }
  5124. if (!flag) {
  5125. ret = wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0]));
  5126. if (ret != BAD_FUNC_ARG) {
  5127. flag = WOLFSSL_FATAL_ERROR;
  5128. }
  5129. }
  5130. if (!flag) {
  5131. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  5132. (word32)XSTRLEN(keys[0]));
  5133. if (ret != BAD_FUNC_ARG) {
  5134. flag = WOLFSSL_FATAL_ERROR;
  5135. }
  5136. }
  5137. if (!flag) {
  5138. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  5139. #ifdef HAVE_FIPS
  5140. if (ret != HMAC_MIN_KEYLEN_E) {
  5141. flag = WOLFSSL_FATAL_ERROR;
  5142. }
  5143. #else
  5144. if (ret != 0) {
  5145. flag = WOLFSSL_FATAL_ERROR;
  5146. }
  5147. #endif
  5148. }
  5149. wc_HmacFree(&hmac);
  5150. printf(resultFmt, flag == 0 ? passed : failed);
  5151. #endif
  5152. return flag;
  5153. } /* END test_wc_Sha224HmacSetKey() */
  5154. /*
  5155. * testing wc_HmacSetKey() on Sha256 hash
  5156. */
  5157. static int test_wc_Sha256HmacSetKey (void)
  5158. {
  5159. int flag = 0;
  5160. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  5161. Hmac hmac;
  5162. int ret, times, itr;
  5163. const char* keys[]=
  5164. {
  5165. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5166. "\x0b\x0b\x0b",
  5167. #ifndef HAVE_FIPS
  5168. "Jefe", /* smaller than minumum FIPS key size */
  5169. #endif
  5170. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5171. "\xAA\xAA\xAA"
  5172. };
  5173. times = sizeof(keys) / sizeof(char*);
  5174. flag = 0;
  5175. printf(testingFmt, "wc_HmacSetKey() with SHA256");
  5176. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  5177. if (ret != 0)
  5178. return ret;
  5179. for (itr = 0; itr < times; itr++) {
  5180. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  5181. (word32)XSTRLEN(keys[itr]));
  5182. if (ret != 0) {
  5183. flag = ret;
  5184. }
  5185. }
  5186. /* Bad args. */
  5187. if (!flag) {
  5188. ret = wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  5189. (word32)XSTRLEN(keys[0]));
  5190. if (ret != BAD_FUNC_ARG) {
  5191. flag = WOLFSSL_FATAL_ERROR;
  5192. }
  5193. }
  5194. if (!flag) {
  5195. ret = wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0]));
  5196. if (ret != BAD_FUNC_ARG) {
  5197. flag = WOLFSSL_FATAL_ERROR;
  5198. }
  5199. }
  5200. if (!flag) {
  5201. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  5202. (word32)XSTRLEN(keys[0]));
  5203. if (ret != BAD_FUNC_ARG) {
  5204. flag = WOLFSSL_FATAL_ERROR;
  5205. }
  5206. }
  5207. if (!flag) {
  5208. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  5209. #ifdef HAVE_FIPS
  5210. if (ret != HMAC_MIN_KEYLEN_E) {
  5211. flag = WOLFSSL_FATAL_ERROR;
  5212. }
  5213. #else
  5214. if (ret != 0) {
  5215. flag = WOLFSSL_FATAL_ERROR;
  5216. }
  5217. #endif
  5218. }
  5219. wc_HmacFree(&hmac);
  5220. printf(resultFmt, flag == 0 ? passed : failed);
  5221. #endif
  5222. return flag;
  5223. } /* END test_wc_Sha256HmacSetKey() */
  5224. /*
  5225. * testing wc_HmacSetKey on Sha384 hash.
  5226. */
  5227. static int test_wc_Sha384HmacSetKey (void)
  5228. {
  5229. int flag = 0;
  5230. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  5231. Hmac hmac;
  5232. int ret, times, itr;
  5233. const char* keys[]=
  5234. {
  5235. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5236. "\x0b\x0b\x0b",
  5237. #ifndef HAVE_FIPS
  5238. "Jefe", /* smaller than minumum FIPS key size */
  5239. #endif
  5240. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5241. "\xAA\xAA\xAA"
  5242. };
  5243. times = sizeof(keys) / sizeof(char*);
  5244. flag = 0;
  5245. printf(testingFmt, "wc_HmacSetKey() with SHA384");
  5246. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  5247. if (ret != 0)
  5248. return ret;
  5249. for (itr = 0; itr < times; itr++) {
  5250. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  5251. (word32)XSTRLEN(keys[itr]));
  5252. if (ret != 0) {
  5253. flag = ret;
  5254. }
  5255. }
  5256. /* Bad args. */
  5257. if (!flag) {
  5258. ret = wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  5259. (word32)XSTRLEN(keys[0]));
  5260. if (ret != BAD_FUNC_ARG) {
  5261. flag = WOLFSSL_FATAL_ERROR;
  5262. }
  5263. }
  5264. if (!flag) {
  5265. ret = wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0]));
  5266. if (ret != BAD_FUNC_ARG) {
  5267. flag = WOLFSSL_FATAL_ERROR;
  5268. }
  5269. }
  5270. if (!flag) {
  5271. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  5272. (word32)XSTRLEN(keys[0]));
  5273. if (ret != BAD_FUNC_ARG) {
  5274. flag = WOLFSSL_FATAL_ERROR;
  5275. }
  5276. }
  5277. if (!flag) {
  5278. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  5279. #ifdef HAVE_FIPS
  5280. if (ret != HMAC_MIN_KEYLEN_E) {
  5281. flag = WOLFSSL_FATAL_ERROR;
  5282. }
  5283. #else
  5284. if (ret != 0) {
  5285. flag = WOLFSSL_FATAL_ERROR;
  5286. }
  5287. #endif
  5288. }
  5289. wc_HmacFree(&hmac);
  5290. printf(resultFmt, flag == 0 ? passed : failed);
  5291. #endif
  5292. return flag;
  5293. } /* END test_wc_Sha384HmacSetKey() */
  5294. /*
  5295. * testing wc_HmacUpdate on wc_Md5 hash.
  5296. */
  5297. static int test_wc_Md5HmacUpdate (void)
  5298. {
  5299. int flag = 0;
  5300. #if !defined(NO_HMAC) && !defined(NO_MD5)
  5301. Hmac hmac;
  5302. testVector a, b;
  5303. int ret;
  5304. #ifdef HAVE_FIPS
  5305. const char* keys =
  5306. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  5307. #else
  5308. const char* keys = "Jefe";
  5309. #endif
  5310. a.input = "what do ya want for nothing?";
  5311. a.inLen = XSTRLEN(a.input);
  5312. b.input = "Hi There";
  5313. b.inLen = XSTRLEN(b.input);
  5314. flag = 0;
  5315. printf(testingFmt, "wc_HmacUpdate() with MD5");
  5316. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  5317. if (ret != 0)
  5318. return ret;
  5319. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys, (word32)XSTRLEN(keys));
  5320. if (ret != 0) {
  5321. flag = ret;
  5322. }
  5323. if (!flag) {
  5324. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  5325. if (ret != 0) {
  5326. flag = ret;
  5327. }
  5328. }
  5329. /* Update Hmac. */
  5330. if (!flag) {
  5331. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  5332. if (ret != 0) {
  5333. flag = ret;
  5334. }
  5335. }
  5336. /* Test bad args. */
  5337. if (!flag) {
  5338. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  5339. if (ret != BAD_FUNC_ARG) {
  5340. flag = WOLFSSL_FATAL_ERROR;
  5341. }
  5342. }
  5343. if (!flag) {
  5344. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  5345. if (ret != BAD_FUNC_ARG) {
  5346. flag = WOLFSSL_FATAL_ERROR;
  5347. }
  5348. }
  5349. if (!flag) {
  5350. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  5351. if (ret != 0) {
  5352. flag = ret;
  5353. }
  5354. }
  5355. wc_HmacFree(&hmac);
  5356. printf(resultFmt, flag == 0 ? passed : failed);
  5357. #endif
  5358. return flag;
  5359. } /* END test_wc_Md5HmacUpdate */
  5360. /*
  5361. * testing wc_HmacUpdate on SHA hash.
  5362. */
  5363. static int test_wc_ShaHmacUpdate (void)
  5364. {
  5365. int flag = 0;
  5366. #if !defined(NO_HMAC) && !defined(NO_SHA)
  5367. Hmac hmac;
  5368. testVector a, b;
  5369. int ret;
  5370. #ifdef HAVE_FIPS
  5371. const char* keys =
  5372. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  5373. #else
  5374. const char* keys = "Jefe";
  5375. #endif
  5376. a.input = "what do ya want for nothing?";
  5377. a.inLen = XSTRLEN(a.input);
  5378. b.input = "Hi There";
  5379. b.inLen = XSTRLEN(b.input);
  5380. flag = 0;
  5381. printf(testingFmt, "wc_HmacUpdate() with SHA");
  5382. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  5383. if (ret != 0)
  5384. return ret;
  5385. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys, (word32)XSTRLEN(keys));
  5386. if (ret != 0) {
  5387. flag = ret;
  5388. }
  5389. if (!flag) {
  5390. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  5391. if (ret != 0) {
  5392. flag = ret;
  5393. }
  5394. }
  5395. /* Update Hmac. */
  5396. if (!flag) {
  5397. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  5398. if (ret != 0) {
  5399. flag = ret;
  5400. }
  5401. }
  5402. /* Test bad args. */
  5403. if (!flag) {
  5404. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  5405. if (ret != BAD_FUNC_ARG) {
  5406. flag = WOLFSSL_FATAL_ERROR;
  5407. }
  5408. }
  5409. if (!flag) {
  5410. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  5411. if (ret != BAD_FUNC_ARG) {
  5412. flag = WOLFSSL_FATAL_ERROR;
  5413. }
  5414. }
  5415. if (!flag) {
  5416. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  5417. if (ret != 0) {
  5418. flag = ret;
  5419. }
  5420. }
  5421. wc_HmacFree(&hmac);
  5422. printf(resultFmt, flag == 0 ? passed : failed);
  5423. #endif
  5424. return flag;
  5425. } /* END test_wc_ShaHmacUpdate */
  5426. /*
  5427. * testing wc_HmacUpdate on SHA224 hash.
  5428. */
  5429. static int test_wc_Sha224HmacUpdate (void)
  5430. {
  5431. int flag = 0;
  5432. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  5433. Hmac hmac;
  5434. testVector a, b;
  5435. int ret;
  5436. #ifdef HAVE_FIPS
  5437. const char* keys =
  5438. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  5439. #else
  5440. const char* keys = "Jefe";
  5441. #endif
  5442. a.input = "what do ya want for nothing?";
  5443. a.inLen = XSTRLEN(a.input);
  5444. b.input = "Hi There";
  5445. b.inLen = XSTRLEN(b.input);
  5446. flag = 0;
  5447. printf(testingFmt, "wc_HmacUpdate() with SHA224");
  5448. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  5449. if (ret != 0)
  5450. return ret;
  5451. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys, (word32)XSTRLEN(keys));
  5452. if (ret != 0) {
  5453. flag = ret;
  5454. }
  5455. if (!flag) {
  5456. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  5457. if (ret != 0) {
  5458. flag = ret;
  5459. }
  5460. }
  5461. /* Update Hmac. */
  5462. if (!flag) {
  5463. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  5464. if (ret != 0) {
  5465. flag = ret;
  5466. }
  5467. }
  5468. /* Test bad args. */
  5469. if (!flag) {
  5470. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  5471. if (ret != BAD_FUNC_ARG) {
  5472. flag = WOLFSSL_FATAL_ERROR;
  5473. }
  5474. }
  5475. if (!flag) {
  5476. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  5477. if (ret != BAD_FUNC_ARG) {
  5478. flag = WOLFSSL_FATAL_ERROR;
  5479. }
  5480. }
  5481. if (!flag) {
  5482. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  5483. if (ret != 0) {
  5484. flag = ret;
  5485. }
  5486. }
  5487. wc_HmacFree(&hmac);
  5488. printf(resultFmt, flag == 0 ? passed : failed);
  5489. #endif
  5490. return flag;
  5491. } /* END test_wc_Sha224HmacUpdate */
  5492. /*
  5493. * testing wc_HmacUpdate on SHA256 hash.
  5494. */
  5495. static int test_wc_Sha256HmacUpdate (void)
  5496. {
  5497. int flag = 0;
  5498. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  5499. Hmac hmac;
  5500. testVector a, b;
  5501. int ret;
  5502. #ifdef HAVE_FIPS
  5503. const char* keys =
  5504. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  5505. #else
  5506. const char* keys = "Jefe";
  5507. #endif
  5508. a.input = "what do ya want for nothing?";
  5509. a.inLen = XSTRLEN(a.input);
  5510. b.input = "Hi There";
  5511. b.inLen = XSTRLEN(b.input);
  5512. flag = 0;
  5513. printf(testingFmt, "wc_HmacUpdate() with WC_SHA256");
  5514. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  5515. if (ret != 0)
  5516. return ret;
  5517. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys, (word32)XSTRLEN(keys));
  5518. if (ret != 0) {
  5519. flag = ret;
  5520. }
  5521. if (!flag) {
  5522. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  5523. if (ret != 0) {
  5524. flag = ret;
  5525. }
  5526. }
  5527. /* Update Hmac. */
  5528. if (!flag) {
  5529. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  5530. if (ret != 0) {
  5531. flag = ret;
  5532. }
  5533. }
  5534. /* Test bad args. */
  5535. if (!flag) {
  5536. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  5537. if (ret != BAD_FUNC_ARG) {
  5538. flag = WOLFSSL_FATAL_ERROR;
  5539. }
  5540. }
  5541. if (!flag) {
  5542. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  5543. if (ret != BAD_FUNC_ARG) {
  5544. flag = WOLFSSL_FATAL_ERROR;
  5545. }
  5546. }
  5547. if (!flag) {
  5548. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  5549. if (ret != 0) {
  5550. flag = ret;
  5551. }
  5552. }
  5553. wc_HmacFree(&hmac);
  5554. printf(resultFmt, flag == 0 ? passed : failed);
  5555. #endif
  5556. return flag;
  5557. } /* END test_wc_Sha256HmacUpdate */
  5558. /*
  5559. * testing wc_HmacUpdate on SHA384 hash.
  5560. */
  5561. static int test_wc_Sha384HmacUpdate (void)
  5562. {
  5563. int flag = 0;
  5564. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  5565. Hmac hmac;
  5566. testVector a, b;
  5567. int ret;
  5568. #ifdef HAVE_FIPS
  5569. const char* keys =
  5570. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  5571. #else
  5572. const char* keys = "Jefe";
  5573. #endif
  5574. a.input = "what do ya want for nothing?";
  5575. a.inLen = XSTRLEN(a.input);
  5576. b.input = "Hi There";
  5577. b.inLen = XSTRLEN(b.input);
  5578. flag = 0;
  5579. printf(testingFmt, "wc_HmacUpdate() with SHA384");
  5580. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  5581. if (ret != 0)
  5582. return ret;
  5583. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys, (word32)XSTRLEN(keys));
  5584. if (ret != 0) {
  5585. flag = ret;
  5586. }
  5587. if (!flag) {
  5588. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  5589. if (ret != 0) {
  5590. flag = ret;
  5591. }
  5592. }
  5593. /* Update Hmac. */
  5594. if (!flag) {
  5595. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  5596. if (ret != 0) {
  5597. flag = ret;
  5598. }
  5599. }
  5600. /* Test bad args. */
  5601. if (!flag) {
  5602. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  5603. if (ret != BAD_FUNC_ARG) {
  5604. flag = WOLFSSL_FATAL_ERROR;
  5605. }
  5606. }
  5607. if (!flag) {
  5608. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  5609. if (ret != BAD_FUNC_ARG) {
  5610. flag = WOLFSSL_FATAL_ERROR;
  5611. }
  5612. }
  5613. if (!flag) {
  5614. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  5615. if (ret != 0) {
  5616. flag = ret;
  5617. }
  5618. }
  5619. wc_HmacFree(&hmac);
  5620. printf(resultFmt, flag == 0 ? passed : failed);
  5621. #endif
  5622. return flag;
  5623. } /* END test_wc_Sha384HmacUpdate */
  5624. /*
  5625. * Testing wc_HmacFinal() with MD5
  5626. */
  5627. static int test_wc_Md5HmacFinal (void)
  5628. {
  5629. int flag = 0;
  5630. #if !defined(NO_HMAC) && !defined(NO_MD5)
  5631. Hmac hmac;
  5632. byte hash[WC_MD5_DIGEST_SIZE];
  5633. testVector a;
  5634. int ret;
  5635. const char* key;
  5636. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  5637. a.input = "Hi There";
  5638. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  5639. "\x9d";
  5640. a.inLen = XSTRLEN(a.input);
  5641. a.outLen = XSTRLEN(a.output);
  5642. flag = 0;
  5643. printf(testingFmt, "wc_HmacFinal() with MD5");
  5644. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  5645. if (ret != 0)
  5646. return ret;
  5647. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key));
  5648. if (ret != 0) {
  5649. flag = ret;
  5650. }
  5651. if (!flag) {
  5652. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  5653. if (ret != 0) {
  5654. flag = ret;
  5655. }
  5656. }
  5657. if (!flag) {
  5658. ret = wc_HmacFinal(&hmac, hash);
  5659. if (ret != 0) {
  5660. flag = ret;
  5661. }
  5662. }
  5663. if (!flag) {
  5664. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  5665. flag = WOLFSSL_FATAL_ERROR;
  5666. }
  5667. }
  5668. /* Try bad parameters. */
  5669. if (!flag) {
  5670. ret = wc_HmacFinal(NULL, hash);
  5671. if (ret != BAD_FUNC_ARG) {
  5672. flag = WOLFSSL_FATAL_ERROR;
  5673. }
  5674. }
  5675. #ifndef HAVE_FIPS
  5676. if (!flag) {
  5677. ret = wc_HmacFinal(&hmac, NULL);
  5678. if (ret != BAD_FUNC_ARG) {
  5679. flag = WOLFSSL_FATAL_ERROR;
  5680. }
  5681. }
  5682. #endif
  5683. wc_HmacFree(&hmac);
  5684. printf(resultFmt, flag == 0 ? passed : failed);
  5685. #endif
  5686. return flag;
  5687. } /* END test_wc_Md5HmacFinal */
  5688. /*
  5689. * Testing wc_HmacFinal() with SHA
  5690. */
  5691. static int test_wc_ShaHmacFinal (void)
  5692. {
  5693. int flag = 0;
  5694. #if !defined(NO_HMAC) && !defined(NO_SHA)
  5695. Hmac hmac;
  5696. byte hash[WC_SHA_DIGEST_SIZE];
  5697. testVector a;
  5698. int ret;
  5699. const char* key;
  5700. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5701. "\x0b\x0b\x0b";
  5702. a.input = "Hi There";
  5703. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  5704. "\x8e\xf1\x46\xbe\x00";
  5705. a.inLen = XSTRLEN(a.input);
  5706. a.outLen = XSTRLEN(a.output);
  5707. flag = 0;
  5708. printf(testingFmt, "wc_HmacFinal() with SHA");
  5709. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  5710. if (ret != 0)
  5711. return ret;
  5712. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key));
  5713. if (ret != 0) {
  5714. flag = ret;
  5715. }
  5716. if (!flag) {
  5717. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  5718. if (ret != 0) {
  5719. flag = ret;
  5720. }
  5721. }
  5722. if (!flag) {
  5723. ret = wc_HmacFinal(&hmac, hash);
  5724. if (ret != 0) {
  5725. flag = ret;
  5726. }
  5727. }
  5728. if (!flag) {
  5729. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  5730. flag = WOLFSSL_FATAL_ERROR;
  5731. }
  5732. }
  5733. /* Try bad parameters. */
  5734. if (!flag) {
  5735. ret = wc_HmacFinal(NULL, hash);
  5736. if (ret != BAD_FUNC_ARG) {
  5737. flag = WOLFSSL_FATAL_ERROR;
  5738. }
  5739. }
  5740. #ifndef HAVE_FIPS
  5741. if (!flag) {
  5742. ret = wc_HmacFinal(&hmac, NULL);
  5743. if (ret != BAD_FUNC_ARG) {
  5744. flag = WOLFSSL_FATAL_ERROR;
  5745. }
  5746. }
  5747. #endif
  5748. wc_HmacFree(&hmac);
  5749. printf(resultFmt, flag == 0 ? passed : failed);
  5750. #endif
  5751. return flag;
  5752. } /* END test_wc_ShaHmacFinal */
  5753. /*
  5754. * Testing wc_HmacFinal() with SHA224
  5755. */
  5756. static int test_wc_Sha224HmacFinal (void)
  5757. {
  5758. int flag = 0;
  5759. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  5760. Hmac hmac;
  5761. byte hash[WC_SHA224_DIGEST_SIZE];
  5762. testVector a;
  5763. int ret;
  5764. const char* key;
  5765. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5766. "\x0b\x0b\x0b";
  5767. a.input = "Hi There";
  5768. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  5769. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  5770. a.inLen = XSTRLEN(a.input);
  5771. a.outLen = XSTRLEN(a.output);
  5772. flag = 0;
  5773. printf(testingFmt, "wc_HmacFinal() with SHA224");
  5774. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  5775. if (ret != 0)
  5776. return ret;
  5777. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key, (word32)XSTRLEN(key));
  5778. if (ret != 0) {
  5779. flag = ret;
  5780. }
  5781. if (!flag) {
  5782. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  5783. if (ret != 0) {
  5784. flag = ret;
  5785. }
  5786. }
  5787. if (!flag) {
  5788. ret = wc_HmacFinal(&hmac, hash);
  5789. if (ret != 0) {
  5790. flag = ret;
  5791. }
  5792. }
  5793. if (!flag) {
  5794. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  5795. flag = WOLFSSL_FATAL_ERROR;
  5796. }
  5797. }
  5798. /* Try bad parameters. */
  5799. if (!flag) {
  5800. ret = wc_HmacFinal(NULL, hash);
  5801. if (ret != BAD_FUNC_ARG) {
  5802. flag = WOLFSSL_FATAL_ERROR;
  5803. }
  5804. }
  5805. #ifndef HAVE_FIPS
  5806. if (!flag) {
  5807. ret = wc_HmacFinal(&hmac, NULL);
  5808. if (ret != BAD_FUNC_ARG) {
  5809. flag = WOLFSSL_FATAL_ERROR;
  5810. }
  5811. }
  5812. #endif
  5813. wc_HmacFree(&hmac);
  5814. printf(resultFmt, flag == 0 ? passed : failed);
  5815. #endif
  5816. return flag;
  5817. } /* END test_wc_Sha224HmacFinal */
  5818. /*
  5819. * Testing wc_HmacFinal() with SHA256
  5820. */
  5821. static int test_wc_Sha256HmacFinal (void)
  5822. {
  5823. int flag = 0;
  5824. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  5825. Hmac hmac;
  5826. byte hash[WC_SHA256_DIGEST_SIZE];
  5827. testVector a;
  5828. int ret;
  5829. const char* key;
  5830. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5831. "\x0b\x0b\x0b";
  5832. a.input = "Hi There";
  5833. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  5834. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  5835. "\xcf\xf7";
  5836. a.inLen = XSTRLEN(a.input);
  5837. a.outLen = XSTRLEN(a.output);
  5838. flag = 0;
  5839. printf(testingFmt, "wc_HmacFinal() with WC_SHA256");
  5840. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  5841. if (ret != 0)
  5842. return ret;
  5843. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key, (word32)XSTRLEN(key));
  5844. if (ret != 0) {
  5845. flag = ret;
  5846. }
  5847. if (!flag) {
  5848. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  5849. if (ret != 0) {
  5850. flag = ret;
  5851. }
  5852. }
  5853. if (!flag) {
  5854. ret = wc_HmacFinal(&hmac, hash);
  5855. if (ret != 0) {
  5856. flag = ret;
  5857. }
  5858. }
  5859. if (!flag) {
  5860. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  5861. flag = WOLFSSL_FATAL_ERROR;
  5862. }
  5863. }
  5864. /* Try bad parameters. */
  5865. if (!flag) {
  5866. ret = wc_HmacFinal(NULL, hash);
  5867. if (ret != BAD_FUNC_ARG) {
  5868. flag = WOLFSSL_FATAL_ERROR;
  5869. }
  5870. }
  5871. #ifndef HAVE_FIPS
  5872. if (!flag) {
  5873. ret = wc_HmacFinal(&hmac, NULL);
  5874. if (ret != BAD_FUNC_ARG) {
  5875. flag = WOLFSSL_FATAL_ERROR;
  5876. }
  5877. }
  5878. #endif
  5879. wc_HmacFree(&hmac);
  5880. printf(resultFmt, flag == 0 ? passed : failed);
  5881. #endif
  5882. return flag;
  5883. } /* END test_wc_Sha256HmacFinal */
  5884. /*
  5885. * Testing wc_HmacFinal() with SHA384
  5886. */
  5887. static int test_wc_Sha384HmacFinal (void)
  5888. {
  5889. int flag = 0;
  5890. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  5891. Hmac hmac;
  5892. byte hash[WC_SHA384_DIGEST_SIZE];
  5893. testVector a;
  5894. int ret;
  5895. const char* key;
  5896. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5897. "\x0b\x0b\x0b";
  5898. a.input = "Hi There";
  5899. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  5900. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  5901. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  5902. "\xfa\x9c\xb6";
  5903. a.inLen = XSTRLEN(a.input);
  5904. a.outLen = XSTRLEN(a.output);
  5905. flag = 0;
  5906. printf(testingFmt, "wc_HmacFinal() with SHA384");
  5907. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  5908. if (ret != 0)
  5909. return ret;
  5910. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key, (word32)XSTRLEN(key));
  5911. if (ret != 0) {
  5912. flag = ret;
  5913. }
  5914. if (!flag) {
  5915. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  5916. if (ret != 0) {
  5917. flag = ret;
  5918. }
  5919. }
  5920. if (!flag) {
  5921. ret = wc_HmacFinal(&hmac, hash);
  5922. if (ret != 0) {
  5923. flag = ret;
  5924. }
  5925. }
  5926. if (!flag) {
  5927. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  5928. flag = WOLFSSL_FATAL_ERROR;
  5929. }
  5930. }
  5931. /* Try bad parameters. */
  5932. if (!flag) {
  5933. ret = wc_HmacFinal(NULL, hash);
  5934. if (ret != BAD_FUNC_ARG) {
  5935. flag = WOLFSSL_FATAL_ERROR;
  5936. }
  5937. }
  5938. #ifndef HAVE_FIPS
  5939. if (!flag) {
  5940. ret = wc_HmacFinal(&hmac, NULL);
  5941. if (ret != BAD_FUNC_ARG) {
  5942. flag = WOLFSSL_FATAL_ERROR;
  5943. }
  5944. }
  5945. #endif
  5946. wc_HmacFree(&hmac);
  5947. printf(resultFmt, flag == 0 ? passed : failed);
  5948. #endif
  5949. return flag;
  5950. } /* END test_wc_Sha384HmacFinal */
  5951. /*
  5952. * Testing wc_InitCmac()
  5953. */
  5954. static int test_wc_InitCmac (void)
  5955. {
  5956. int ret = 0;
  5957. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  5958. Cmac cmac1, cmac2, cmac3;
  5959. /* AES 128 key. */
  5960. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  5961. "\x09\x10\x11\x12\x13\x14\x15\x16";
  5962. /* AES 192 key. */
  5963. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  5964. "\x09\x01\x11\x12\x13\x14\x15\x16"
  5965. "\x01\x02\x03\x04\x05\x06\x07\x08";
  5966. /* AES 256 key. */
  5967. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  5968. "\x09\x01\x11\x12\x13\x14\x15\x16"
  5969. "\x01\x02\x03\x04\x05\x06\x07\x08"
  5970. "\x09\x01\x11\x12\x13\x14\x15\x16";
  5971. word32 key1Sz = (word32)sizeof(key1) - 1;
  5972. word32 key2Sz = (word32)sizeof(key2) - 1;
  5973. word32 key3Sz = (word32)sizeof(key3) - 1;
  5974. int type = WC_CMAC_AES;
  5975. printf(testingFmt, "wc_InitCmac()");
  5976. #ifdef WOLFSSL_AES_128
  5977. ret = wc_InitCmac(&cmac1, key1, key1Sz, type, NULL);
  5978. #endif
  5979. #ifdef WOLFSSL_AES_192
  5980. if (ret == 0)
  5981. ret = wc_InitCmac(&cmac2, key2, key2Sz, type, NULL);
  5982. #endif
  5983. #ifdef WOLFSSL_AES_256
  5984. if (ret == 0)
  5985. ret = wc_InitCmac(&cmac3, key3, key3Sz, type, NULL);
  5986. #endif
  5987. /* Test bad args. */
  5988. if (ret == 0) {
  5989. ret = wc_InitCmac(NULL, key3, key3Sz, type, NULL);
  5990. if (ret == BAD_FUNC_ARG) {
  5991. ret = wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL);
  5992. }
  5993. if (ret == BAD_FUNC_ARG) {
  5994. ret = wc_InitCmac(&cmac3, key3, 0, type, NULL);
  5995. }
  5996. if (ret == BAD_FUNC_ARG) {
  5997. ret = wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL);
  5998. }
  5999. if (ret == BAD_FUNC_ARG) {
  6000. ret = 0;
  6001. } else {
  6002. ret = SSL_FATAL_ERROR;
  6003. }
  6004. }
  6005. (void)key1;
  6006. (void)key1Sz;
  6007. (void)key2;
  6008. (void)key2Sz;
  6009. (void)cmac1;
  6010. (void)cmac2;
  6011. printf(resultFmt, ret == 0 ? passed : failed);
  6012. #endif
  6013. return ret;
  6014. } /* END test_wc_InitCmac */
  6015. /*
  6016. * Testing wc_CmacUpdate()
  6017. */
  6018. static int test_wc_CmacUpdate (void)
  6019. {
  6020. int ret = 0;
  6021. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  6022. Cmac cmac;
  6023. byte key[] =
  6024. {
  6025. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  6026. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  6027. };
  6028. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  6029. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  6030. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  6031. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  6032. "\xf4";
  6033. word32 inSz = (word32)sizeof(in) - 1;
  6034. word32 keySz = (word32)sizeof(key);
  6035. int type = WC_CMAC_AES;
  6036. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  6037. if (ret != 0) {
  6038. return ret;
  6039. }
  6040. printf(testingFmt, "wc_CmacUpdate()");
  6041. ret = wc_CmacUpdate(&cmac, in, inSz);
  6042. /* Test bad args. */
  6043. if (ret == 0) {
  6044. ret = wc_CmacUpdate(NULL, in, inSz);
  6045. if (ret == BAD_FUNC_ARG) {
  6046. ret = wc_CmacUpdate(&cmac, NULL, 30);
  6047. }
  6048. if (ret == BAD_FUNC_ARG) {
  6049. ret = 0;
  6050. } else if (ret == 0) {
  6051. ret = SSL_FATAL_ERROR;
  6052. }
  6053. }
  6054. printf(resultFmt, ret == 0 ? passed : failed);
  6055. #endif
  6056. return ret;
  6057. } /* END test_wc_CmacUpdate */
  6058. /*
  6059. * Testing wc_CmacFinal()
  6060. */
  6061. static int test_wc_CmacFinal (void)
  6062. {
  6063. int ret = 0;
  6064. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  6065. Cmac cmac;
  6066. byte key[] =
  6067. {
  6068. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  6069. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  6070. };
  6071. byte msg[] =
  6072. {
  6073. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  6074. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  6075. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  6076. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  6077. 0xf4
  6078. };
  6079. /* Test vectors from CMACGenAES128.rsp from
  6080. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  6081. * Per RFC4493 truncation of lsb is possible.
  6082. */
  6083. byte expMac[] =
  6084. {
  6085. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  6086. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  6087. };
  6088. byte mac[AES_BLOCK_SIZE];
  6089. word32 msgSz = (word32)sizeof(msg);
  6090. word32 keySz = (word32)sizeof(key);
  6091. word32 macSz = sizeof(mac);
  6092. word32 badMacSz = 17;
  6093. int expMacSz = sizeof(expMac);
  6094. int type = WC_CMAC_AES;
  6095. XMEMSET(mac, 0, macSz);
  6096. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  6097. if (ret != 0) {
  6098. return ret;
  6099. }
  6100. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  6101. printf(testingFmt, "wc_CmacFinal()");
  6102. if (ret == 0) {
  6103. ret = wc_CmacFinal(&cmac, mac, &macSz);
  6104. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  6105. ret = SSL_FATAL_ERROR;
  6106. }
  6107. /* Pass in bad args. */
  6108. if (ret == 0) {
  6109. ret = wc_CmacFinal(NULL, mac, &macSz);
  6110. if (ret == BAD_FUNC_ARG) {
  6111. ret = wc_CmacFinal(&cmac, NULL, &macSz);
  6112. }
  6113. if (ret == BAD_FUNC_ARG) {
  6114. ret = wc_CmacFinal(&cmac, mac, &badMacSz);
  6115. if (ret == BUFFER_E) {
  6116. ret = 0;
  6117. }
  6118. } else if (ret == 0) {
  6119. ret = SSL_FATAL_ERROR;
  6120. }
  6121. }
  6122. }
  6123. printf(resultFmt, ret == 0 ? passed : failed);
  6124. #endif
  6125. return ret;
  6126. } /* END test_wc_CmacFinal */
  6127. /*
  6128. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  6129. */
  6130. static int test_wc_AesCmacGenerate (void)
  6131. {
  6132. int ret = 0;
  6133. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  6134. Cmac cmac;
  6135. byte key[] =
  6136. {
  6137. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  6138. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  6139. };
  6140. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  6141. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  6142. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  6143. "\x3d\x32\x65\x4c\x66\x23\xc5";
  6144. byte mac[AES_BLOCK_SIZE];
  6145. word32 keySz = sizeof(key);
  6146. word32 macSz = sizeof(mac);
  6147. word32 msgSz = sizeof(msg) - 1;
  6148. word32 expMacSz = sizeof(expMac) - 1;
  6149. int type = WC_CMAC_AES;
  6150. XMEMSET(mac, 0, macSz);
  6151. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  6152. if (ret != 0) {
  6153. return ret;
  6154. }
  6155. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  6156. if (ret != 0) {
  6157. return ret;
  6158. }
  6159. printf(testingFmt, "wc_AesCmacGenerate()");
  6160. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz);
  6161. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  6162. ret = SSL_FATAL_ERROR;
  6163. }
  6164. /* Pass in bad args. */
  6165. if (ret == 0) {
  6166. ret = wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz);
  6167. if (ret == BAD_FUNC_ARG) {
  6168. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz);
  6169. }
  6170. if (ret == BAD_FUNC_ARG) {
  6171. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0);
  6172. }
  6173. if (ret == BAD_FUNC_ARG) {
  6174. ret = wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz);
  6175. }
  6176. if (ret == BAD_FUNC_ARG) {
  6177. ret = 0;
  6178. } else if (ret == 0) {
  6179. ret = SSL_FATAL_ERROR;
  6180. }
  6181. }
  6182. printf(resultFmt, ret == 0 ? passed : failed);
  6183. if (ret == 0) {
  6184. printf(testingFmt, "wc_AesCmacVerify()");
  6185. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz);
  6186. /* Test bad args. */
  6187. if (ret == 0) {
  6188. ret = wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz);
  6189. if (ret == BAD_FUNC_ARG) {
  6190. ret = wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz);
  6191. }
  6192. if (ret == BAD_FUNC_ARG) {
  6193. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz);
  6194. }
  6195. if (ret == BAD_FUNC_ARG) {
  6196. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0);
  6197. }
  6198. if (ret == BAD_FUNC_ARG) {
  6199. ret = wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz);
  6200. }
  6201. if (ret == BAD_FUNC_ARG) {
  6202. ret = 0;
  6203. } else if (ret == 0) {
  6204. ret = SSL_FATAL_ERROR;
  6205. }
  6206. }
  6207. printf(resultFmt, ret == 0 ? passed : failed);
  6208. }
  6209. #endif
  6210. return ret;
  6211. } /* END test_wc_AesCmacGenerate */
  6212. /*
  6213. * unit test for wc_Des3_SetIV()
  6214. */
  6215. static int test_wc_Des3_SetIV (void)
  6216. {
  6217. int ret = 0;
  6218. #ifndef NO_DES3
  6219. Des3 des;
  6220. const byte key[] =
  6221. {
  6222. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  6223. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  6224. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  6225. };
  6226. const byte iv[] =
  6227. {
  6228. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  6229. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  6230. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  6231. };
  6232. printf(testingFmt, "wc_Des3_SetIV()");
  6233. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  6234. if (ret != 0)
  6235. return ret;
  6236. /* DES_ENCRYPTION or DES_DECRYPTION */
  6237. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  6238. if (ret == 0) {
  6239. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  6240. ret = WOLFSSL_FATAL_ERROR;
  6241. }
  6242. }
  6243. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  6244. /* Test explicitly wc_Des3_SetIV() */
  6245. if (ret == 0) {
  6246. ret = wc_Des3_SetIV(NULL, iv);
  6247. if (ret == BAD_FUNC_ARG) {
  6248. ret = wc_Des3_SetIV(&des, NULL);
  6249. } else if (ret == 0) {
  6250. ret = WOLFSSL_FATAL_ERROR;
  6251. }
  6252. }
  6253. #endif
  6254. wc_Des3Free(&des);
  6255. printf(resultFmt, ret == 0 ? passed : failed);
  6256. #endif
  6257. return ret;
  6258. } /* END test_wc_Des3_SetIV */
  6259. /*
  6260. * unit test for wc_Des3_SetKey()
  6261. */
  6262. static int test_wc_Des3_SetKey (void)
  6263. {
  6264. int ret = 0;
  6265. #ifndef NO_DES3
  6266. Des3 des;
  6267. const byte key[] =
  6268. {
  6269. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  6270. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  6271. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  6272. };
  6273. const byte iv[] =
  6274. {
  6275. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  6276. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  6277. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  6278. };
  6279. printf(testingFmt, "wc_Des3_SetKey()");
  6280. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  6281. if (ret != 0)
  6282. return ret;
  6283. /* DES_ENCRYPTION or DES_DECRYPTION */
  6284. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  6285. if (ret == 0) {
  6286. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  6287. ret = WOLFSSL_FATAL_ERROR;
  6288. }
  6289. }
  6290. /* Test bad args. */
  6291. if (ret == 0) {
  6292. ret = wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION);
  6293. if (ret == BAD_FUNC_ARG) {
  6294. ret = wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION);
  6295. }
  6296. if (ret == BAD_FUNC_ARG) {
  6297. ret = wc_Des3_SetKey(&des, key, iv, -1);
  6298. }
  6299. if (ret == BAD_FUNC_ARG) {
  6300. /* Default case. Should return 0. */
  6301. ret = wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION);
  6302. }
  6303. } /* END if ret != 0 */
  6304. wc_Des3Free(&des);
  6305. printf(resultFmt, ret == 0 ? passed : failed);
  6306. #endif
  6307. return ret;
  6308. } /* END test_wc_Des3_SetKey */
  6309. /*
  6310. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  6311. */
  6312. static int test_wc_Des3_CbcEncryptDecrypt (void)
  6313. {
  6314. int ret = 0;
  6315. #ifndef NO_DES3
  6316. Des3 des;
  6317. byte cipher[24];
  6318. byte plain[24];
  6319. const byte key[] =
  6320. {
  6321. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  6322. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  6323. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  6324. };
  6325. const byte iv[] =
  6326. {
  6327. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  6328. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  6329. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  6330. };
  6331. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  6332. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  6333. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  6334. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  6335. };
  6336. printf(testingFmt, "wc_Des3_CbcEncrypt()");
  6337. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  6338. if (ret != 0)
  6339. return ret;
  6340. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  6341. if (ret == 0) {
  6342. ret = wc_Des3_CbcEncrypt(&des, cipher, vector, 24);
  6343. if (ret == 0) {
  6344. ret = wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION);
  6345. }
  6346. if (ret == 0) {
  6347. ret = wc_Des3_CbcDecrypt(&des, plain, cipher, 24);
  6348. }
  6349. }
  6350. if (ret == 0) {
  6351. if (XMEMCMP(plain, vector, 24) != 0) {
  6352. ret = WOLFSSL_FATAL_ERROR;
  6353. }
  6354. }
  6355. /* Pass in bad args. */
  6356. if (ret == 0) {
  6357. ret = wc_Des3_CbcEncrypt(NULL, cipher, vector, 24);
  6358. if (ret == BAD_FUNC_ARG) {
  6359. ret = wc_Des3_CbcEncrypt(&des, NULL, vector, 24);
  6360. }
  6361. if (ret == BAD_FUNC_ARG) {
  6362. ret = wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector));
  6363. }
  6364. if (ret != BAD_FUNC_ARG) {
  6365. ret = WOLFSSL_FATAL_ERROR;;
  6366. } else {
  6367. ret = 0;
  6368. }
  6369. }
  6370. if (ret == 0) {
  6371. ret = wc_Des3_CbcDecrypt(NULL, plain, cipher, 24);
  6372. if (ret == BAD_FUNC_ARG) {
  6373. ret = wc_Des3_CbcDecrypt(&des, NULL, cipher, 24);
  6374. }
  6375. if (ret == BAD_FUNC_ARG) {
  6376. ret = wc_Des3_CbcDecrypt(&des, plain, NULL, 24);
  6377. }
  6378. if (ret != BAD_FUNC_ARG) {
  6379. ret = WOLFSSL_FATAL_ERROR;
  6380. } else {
  6381. ret = 0;
  6382. }
  6383. }
  6384. wc_Des3Free(&des);
  6385. printf(resultFmt, ret == 0 ? passed : failed);
  6386. #endif
  6387. return ret;
  6388. } /* END wc_Des3_CbcEncrypt */
  6389. /*
  6390. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  6391. */
  6392. static int test_wc_Des3_CbcEncryptDecryptWithKey (void)
  6393. {
  6394. int ret = 0;
  6395. #ifndef NO_DES3
  6396. word32 vectorSz, cipherSz;
  6397. byte cipher[24];
  6398. byte plain[24];
  6399. byte vector[] = /* Now is the time for all w/o trailing 0 */
  6400. {
  6401. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  6402. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  6403. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  6404. };
  6405. byte key[] =
  6406. {
  6407. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  6408. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  6409. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  6410. };
  6411. byte iv[] =
  6412. {
  6413. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  6414. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  6415. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  6416. };
  6417. vectorSz = sizeof(byte) * 24;
  6418. cipherSz = sizeof(byte) * 24;
  6419. printf(testingFmt, "wc_Des3_CbcEncryptWithKey()");
  6420. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv);
  6421. if (ret == 0) {
  6422. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv);
  6423. if (ret == 0) {
  6424. if (XMEMCMP(plain, vector, 24) != 0) {
  6425. ret = WOLFSSL_FATAL_ERROR;
  6426. }
  6427. }
  6428. }
  6429. /* pass in bad args. */
  6430. if (ret == 0) {
  6431. ret = wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv);
  6432. if (ret == BAD_FUNC_ARG) {
  6433. ret = wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv);
  6434. }
  6435. if (ret == BAD_FUNC_ARG) {
  6436. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv);
  6437. }
  6438. if (ret == BAD_FUNC_ARG) {
  6439. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz,
  6440. key, NULL);
  6441. } else {
  6442. /* Return code catch. */
  6443. ret = WOLFSSL_FAILURE;
  6444. }
  6445. }
  6446. if (ret == 0) {
  6447. ret = wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv);
  6448. if (ret == BAD_FUNC_ARG) {
  6449. ret = wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv);
  6450. }
  6451. if (ret == BAD_FUNC_ARG) {
  6452. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv);
  6453. }
  6454. if (ret == BAD_FUNC_ARG) {
  6455. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL);
  6456. } else {
  6457. ret = WOLFSSL_FAILURE;
  6458. }
  6459. }
  6460. printf(resultFmt, ret == 0 ? passed : failed);
  6461. #endif
  6462. return ret;
  6463. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  6464. /*
  6465. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  6466. */
  6467. static int test_wc_Chacha_SetKey (void)
  6468. {
  6469. int ret = 0;
  6470. #ifdef HAVE_CHACHA
  6471. ChaCha ctx;
  6472. const byte key[] =
  6473. {
  6474. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6475. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6476. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6477. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  6478. };
  6479. byte cipher[128];
  6480. printf(testingFmt, "wc_Chacha_SetKey()");
  6481. ret = wc_Chacha_SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  6482. /* Test bad args. */
  6483. if (ret == 0) {
  6484. ret = wc_Chacha_SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  6485. if (ret == BAD_FUNC_ARG) {
  6486. ret = wc_Chacha_SetKey(&ctx, key, 18);
  6487. }
  6488. if (ret == BAD_FUNC_ARG) {
  6489. ret = 0;
  6490. } else {
  6491. ret = WOLFSSL_FATAL_ERROR;
  6492. }
  6493. }
  6494. printf(resultFmt, ret == 0 ? passed : failed);
  6495. if (ret != 0) {
  6496. return ret;
  6497. }
  6498. printf(testingFmt, "wc_Chacha_SetIV");
  6499. ret = wc_Chacha_SetIV(&ctx, cipher, 0);
  6500. if (ret == 0) {
  6501. /* Test bad args. */
  6502. ret = wc_Chacha_SetIV(NULL, cipher, 0);
  6503. if (ret == BAD_FUNC_ARG) {
  6504. ret = 0;
  6505. } else {
  6506. ret = WOLFSSL_FAILURE;
  6507. }
  6508. }
  6509. printf(resultFmt, ret == 0 ? passed : failed);
  6510. #endif
  6511. return ret;
  6512. } /* END test_wc_Chacha_SetKey */
  6513. /*
  6514. * unit test for wc_Poly1305SetKey()
  6515. */
  6516. static int test_wc_Poly1305SetKey(void)
  6517. {
  6518. int ret = 0;
  6519. #ifdef HAVE_POLY1305
  6520. Poly1305 ctx;
  6521. const byte key[] =
  6522. {
  6523. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6524. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6525. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6526. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  6527. };
  6528. printf(testingFmt, "wc_Poly1305_SetKey()");
  6529. ret = wc_Poly1305SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  6530. /* Test bad args. */
  6531. if (ret == 0) {
  6532. ret = wc_Poly1305SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  6533. if(ret == BAD_FUNC_ARG) {
  6534. ret = wc_Poly1305SetKey(&ctx, NULL, (word32)(sizeof(key)/sizeof(byte)));
  6535. }
  6536. if (ret == BAD_FUNC_ARG) {
  6537. ret = wc_Poly1305SetKey(&ctx, key, 18);
  6538. }
  6539. if (ret == BAD_FUNC_ARG) {
  6540. ret = 0;
  6541. } else {
  6542. ret = WOLFSSL_FATAL_ERROR;
  6543. }
  6544. }
  6545. printf(resultFmt, ret == 0 ? passed : failed);
  6546. #endif
  6547. return ret;
  6548. } /* END test_wc_Poly1305_SetKey() */
  6549. /*
  6550. * Testing wc_Chacha_Process()
  6551. */
  6552. static int test_wc_Chacha_Process (void)
  6553. {
  6554. int ret = 0;
  6555. #ifdef HAVE_CHACHA
  6556. ChaCha enc, dec;
  6557. byte cipher[128];
  6558. byte plain[128];
  6559. const byte key[] =
  6560. {
  6561. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6562. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6563. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6564. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  6565. };
  6566. const char* input = "Everybody gets Friday off.";
  6567. word32 keySz = sizeof(key)/sizeof(byte);
  6568. unsigned long int inlen = XSTRLEN(input);
  6569. /*Initialize stack varialbes.*/
  6570. XMEMSET(cipher, 0, 128);
  6571. XMEMSET(plain, 0, 128);
  6572. printf(testingFmt, "wc_Chacha_Process()");
  6573. ret = wc_Chacha_SetKey(&enc, key, keySz);
  6574. if (ret == 0) {
  6575. ret = wc_Chacha_SetKey(&dec, key, keySz);
  6576. if (ret == 0) {
  6577. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  6578. }
  6579. if (ret == 0) {
  6580. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  6581. }
  6582. }
  6583. if (ret == 0) {
  6584. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen);
  6585. if (ret == 0) {
  6586. ret = wc_Chacha_Process(&dec, plain, cipher, (word32)inlen);
  6587. if (ret == 0) {
  6588. ret = XMEMCMP(input, plain, (int)inlen);
  6589. }
  6590. }
  6591. }
  6592. /* Test bad args. */
  6593. if (ret == 0) {
  6594. ret = wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen);
  6595. if (ret == BAD_FUNC_ARG) {
  6596. ret = 0;
  6597. } else {
  6598. ret = WOLFSSL_FATAL_ERROR;
  6599. }
  6600. }
  6601. printf(resultFmt, ret == 0 ? passed : failed);
  6602. #endif
  6603. return ret;
  6604. } /* END test_wc_Chacha_Process */
  6605. /*
  6606. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  6607. */
  6608. static int test_wc_ChaCha20Poly1305_aead (void)
  6609. {
  6610. int ret = 0;
  6611. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  6612. const byte key[] = {
  6613. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  6614. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  6615. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  6616. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  6617. };
  6618. const byte plaintext[] = {
  6619. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  6620. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  6621. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  6622. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  6623. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  6624. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  6625. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  6626. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  6627. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  6628. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  6629. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  6630. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  6631. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  6632. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  6633. 0x74, 0x2e
  6634. };
  6635. const byte iv[] = {
  6636. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  6637. 0x44, 0x45, 0x46, 0x47
  6638. };
  6639. const byte aad[] = { /* additional data */
  6640. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  6641. 0xc4, 0xc5, 0xc6, 0xc7
  6642. };
  6643. const byte cipher[] = { /* expected output from operation */
  6644. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  6645. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  6646. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  6647. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  6648. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  6649. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  6650. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  6651. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  6652. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  6653. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  6654. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  6655. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  6656. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  6657. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  6658. 0x61, 0x16
  6659. };
  6660. const byte authTag[] = { /* expected output from operation */
  6661. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  6662. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  6663. };
  6664. byte generatedCiphertext[272];
  6665. byte generatedPlaintext[272];
  6666. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  6667. /* Initialize stack variables. */
  6668. XMEMSET(generatedCiphertext, 0, 272);
  6669. XMEMSET(generatedPlaintext, 0, 272);
  6670. /* Test Encrypt */
  6671. printf(testingFmt, "wc_ChaCha20Poly1305_Encrypt()");
  6672. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), plaintext,
  6673. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  6674. if (ret == 0) {
  6675. ret = XMEMCMP(generatedCiphertext, cipher, sizeof(cipher)/sizeof(byte));
  6676. }
  6677. /* Test bad args. */
  6678. if (ret == 0) {
  6679. ret = wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad), plaintext,
  6680. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  6681. if (ret == BAD_FUNC_ARG) {
  6682. ret = wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  6683. plaintext, sizeof(plaintext),
  6684. generatedCiphertext, generatedAuthTag);
  6685. }
  6686. if (ret == BAD_FUNC_ARG) {
  6687. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  6688. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  6689. }
  6690. if (ret == BAD_FUNC_ARG) {
  6691. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  6692. plaintext, 0, generatedCiphertext, generatedAuthTag);
  6693. }
  6694. if (ret == BAD_FUNC_ARG) {
  6695. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  6696. plaintext, sizeof(plaintext), NULL, generatedAuthTag);
  6697. }
  6698. if (ret == BAD_FUNC_ARG) {
  6699. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  6700. plaintext, sizeof(plaintext), generatedCiphertext, NULL);
  6701. }
  6702. if (ret == BAD_FUNC_ARG) {
  6703. ret = 0;
  6704. } else {
  6705. ret = WOLFSSL_FATAL_ERROR;
  6706. }
  6707. }
  6708. printf(resultFmt, ret == 0 ? passed : failed);
  6709. if (ret != 0) {
  6710. return ret;
  6711. }
  6712. printf(testingFmt, "wc_ChaCha20Poly1305_Decrypt()");
  6713. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  6714. sizeof(cipher), authTag, generatedPlaintext);
  6715. if (ret == 0) {
  6716. ret = XMEMCMP(generatedPlaintext, plaintext,
  6717. sizeof(plaintext)/sizeof(byte));
  6718. }
  6719. /* Test bad args. */
  6720. if (ret == 0) {
  6721. ret = wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  6722. sizeof(cipher), authTag, generatedPlaintext);
  6723. if (ret == BAD_FUNC_ARG) {
  6724. ret = wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  6725. cipher, sizeof(cipher), authTag, generatedPlaintext);
  6726. }
  6727. if (ret == BAD_FUNC_ARG) {
  6728. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  6729. sizeof(cipher), authTag, generatedPlaintext);
  6730. }
  6731. if (ret == BAD_FUNC_ARG) {
  6732. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  6733. sizeof(cipher), NULL, generatedPlaintext);
  6734. }
  6735. if (ret == BAD_FUNC_ARG) {
  6736. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  6737. sizeof(cipher), authTag, NULL);
  6738. }
  6739. if (ret == BAD_FUNC_ARG) {
  6740. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  6741. 0, authTag, generatedPlaintext);
  6742. }
  6743. if (ret == BAD_FUNC_ARG) {
  6744. ret = 0;
  6745. } else {
  6746. ret = WOLFSSL_FATAL_ERROR;
  6747. }
  6748. }
  6749. printf(resultFmt, ret == 0 ? passed : failed);
  6750. #endif
  6751. return ret;
  6752. } /* END test-wc_ChaCha20Poly1305_EncryptDecrypt */
  6753. /*
  6754. * Testing function for wc_AesSetIV
  6755. */
  6756. static int test_wc_AesSetIV (void)
  6757. {
  6758. int ret = 0;
  6759. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  6760. Aes aes;
  6761. byte key16[] =
  6762. {
  6763. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6764. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  6765. };
  6766. byte iv1[] = "1234567890abcdef";
  6767. byte iv2[] = "0987654321fedcba";
  6768. printf(testingFmt, "wc_AesSetIV()");
  6769. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  6770. if (ret != 0)
  6771. return ret;
  6772. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  6773. iv1, AES_ENCRYPTION);
  6774. if(ret == 0) {
  6775. ret = wc_AesSetIV(&aes, iv2);
  6776. }
  6777. /* Test bad args. */
  6778. if(ret == 0) {
  6779. ret = wc_AesSetIV(NULL, iv1);
  6780. if(ret == BAD_FUNC_ARG) {
  6781. /* NULL iv should return 0. */
  6782. ret = wc_AesSetIV(&aes, NULL);
  6783. } else {
  6784. ret = WOLFSSL_FATAL_ERROR;
  6785. }
  6786. }
  6787. wc_AesFree(&aes);
  6788. printf(resultFmt, ret == 0 ? passed : failed);
  6789. #endif
  6790. return ret;
  6791. } /* test_wc_AesSetIV */
  6792. /*
  6793. * Testing function for wc_AesSetKey().
  6794. */
  6795. static int test_wc_AesSetKey (void)
  6796. {
  6797. int ret = 0;
  6798. #ifndef NO_AES
  6799. Aes aes;
  6800. byte key16[] =
  6801. {
  6802. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6803. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  6804. };
  6805. #ifdef WOLFSSL_AES_192
  6806. byte key24[] =
  6807. {
  6808. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6809. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  6810. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  6811. };
  6812. #endif
  6813. #ifdef WOLFSSL_AES_256
  6814. byte key32[] =
  6815. {
  6816. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6817. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  6818. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6819. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  6820. };
  6821. #endif
  6822. byte badKey16[] =
  6823. {
  6824. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6825. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  6826. };
  6827. byte iv[] = "1234567890abcdef";
  6828. printf(testingFmt, "wc_AesSetKey()");
  6829. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  6830. if (ret != 0)
  6831. return ret;
  6832. #ifdef WOLFSSL_AES_128
  6833. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  6834. iv, AES_ENCRYPTION);
  6835. #endif
  6836. #ifdef WOLFSSL_AES_192
  6837. if (ret == 0) {
  6838. ret = wc_AesSetKey (&aes, key24, (word32) sizeof(key24) / sizeof(byte),
  6839. iv, AES_ENCRYPTION);
  6840. }
  6841. #endif
  6842. #ifdef WOLFSSL_AES_256
  6843. if (ret == 0) {
  6844. ret = wc_AesSetKey (&aes, key32, (word32) sizeof(key32) / sizeof(byte),
  6845. iv, AES_ENCRYPTION);
  6846. }
  6847. #endif
  6848. /* Pass in bad args. */
  6849. if (ret == 0) {
  6850. ret = wc_AesSetKey (NULL, key16, (word32) sizeof(key16) / sizeof(byte),
  6851. iv, AES_ENCRYPTION);
  6852. if (ret == BAD_FUNC_ARG) {
  6853. ret = wc_AesSetKey(&aes, badKey16,
  6854. (word32) sizeof(badKey16) / sizeof(byte),
  6855. iv, AES_ENCRYPTION);
  6856. }
  6857. if (ret == BAD_FUNC_ARG) {
  6858. ret = 0;
  6859. } else {
  6860. ret = WOLFSSL_FATAL_ERROR;
  6861. }
  6862. }
  6863. wc_AesFree(&aes);
  6864. printf(resultFmt, ret == 0 ? passed : failed);
  6865. #endif
  6866. return ret;
  6867. } /* END test_wc_AesSetKey */
  6868. /*
  6869. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  6870. * and wc_AesCbcDecryptWithKey()
  6871. */
  6872. static int test_wc_AesCbcEncryptDecrypt (void)
  6873. {
  6874. int ret = 0;
  6875. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  6876. defined(WOLFSSL_AES_256)
  6877. Aes aes;
  6878. byte key32[] =
  6879. {
  6880. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6881. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  6882. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6883. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  6884. };
  6885. byte vector[] = /* Now is the time for all w/o trailing 0 */
  6886. {
  6887. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  6888. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  6889. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  6890. };
  6891. byte iv[] = "1234567890abcdef";
  6892. byte enc[sizeof(vector)];
  6893. byte dec[sizeof(vector)];
  6894. int cbcE = WOLFSSL_FATAL_ERROR;
  6895. int cbcD = WOLFSSL_FATAL_ERROR;
  6896. int cbcDWK = WOLFSSL_FATAL_ERROR;
  6897. byte dec2[sizeof(vector)];
  6898. /* Init stack variables. */
  6899. XMEMSET(enc, 0, sizeof(enc));
  6900. XMEMSET(dec, 0, sizeof(vector));
  6901. XMEMSET(dec2, 0, sizeof(vector));
  6902. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  6903. if (ret != 0)
  6904. return ret;
  6905. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv, AES_ENCRYPTION);
  6906. if (ret == 0) {
  6907. ret = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector));
  6908. if (ret == 0) {
  6909. /* Re init for decrypt and set flag. */
  6910. cbcE = 0;
  6911. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2,
  6912. iv, AES_DECRYPTION);
  6913. }
  6914. if (ret == 0) {
  6915. ret = wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE);
  6916. if (ret != 0 || XMEMCMP(vector, dec, AES_BLOCK_SIZE) != 0) {
  6917. ret = WOLFSSL_FATAL_ERROR;
  6918. } else {
  6919. /* Set flag. */
  6920. cbcD = 0;
  6921. }
  6922. }
  6923. }
  6924. /* If encrypt succeeds but cbc decrypt fails, we can still test. */
  6925. if (ret == 0 || (ret != 0 && cbcE == 0)) {
  6926. ret = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  6927. key32, sizeof(key32)/sizeof(byte), iv);
  6928. if (ret == 0 || XMEMCMP(vector, dec2, AES_BLOCK_SIZE) == 0) {
  6929. cbcDWK = 0;
  6930. }
  6931. }
  6932. printf(testingFmt, "wc_AesCbcEncrypt()");
  6933. /* Pass in bad args */
  6934. if (cbcE == 0) {
  6935. cbcE = wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector));
  6936. if (cbcE == BAD_FUNC_ARG) {
  6937. cbcE = wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector));
  6938. }
  6939. if (cbcE == BAD_FUNC_ARG) {
  6940. cbcE = wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector));
  6941. }
  6942. if (cbcE == BAD_FUNC_ARG) {
  6943. cbcE = 0;
  6944. } else {
  6945. cbcE = WOLFSSL_FATAL_ERROR;
  6946. }
  6947. }
  6948. printf(resultFmt, cbcE == 0 ? passed : failed);
  6949. if (cbcE != 0) {
  6950. wc_AesFree(&aes);
  6951. return cbcE;
  6952. }
  6953. printf(testingFmt, "wc_AesCbcDecrypt()");
  6954. if (cbcD == 0) {
  6955. cbcD = wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE);
  6956. if (cbcD == BAD_FUNC_ARG) {
  6957. cbcD = wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE);
  6958. }
  6959. if (cbcD == BAD_FUNC_ARG) {
  6960. cbcD = wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE);
  6961. }
  6962. if (cbcD == BAD_FUNC_ARG) {
  6963. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1);
  6964. }
  6965. if (cbcD == BAD_FUNC_ARG) {
  6966. cbcD = 0;
  6967. } else {
  6968. cbcD = WOLFSSL_FATAL_ERROR;
  6969. }
  6970. }
  6971. printf(resultFmt, cbcD == 0 ? passed : failed);
  6972. if (cbcD != 0) {
  6973. wc_AesFree(&aes);
  6974. return cbcD;
  6975. }
  6976. printf(testingFmt, "wc_AesCbcDecryptWithKey()");
  6977. if (cbcDWK == 0) {
  6978. cbcDWK = wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  6979. key32, sizeof(key32)/sizeof(byte), iv);
  6980. if (cbcDWK == BAD_FUNC_ARG) {
  6981. cbcDWK = wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  6982. key32, sizeof(key32)/sizeof(byte), iv);
  6983. }
  6984. if (cbcDWK == BAD_FUNC_ARG) {
  6985. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  6986. NULL, sizeof(key32)/sizeof(byte), iv);
  6987. }
  6988. if (cbcDWK == BAD_FUNC_ARG) {
  6989. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  6990. key32, sizeof(key32)/sizeof(byte), NULL);
  6991. }
  6992. if (cbcDWK == BAD_FUNC_ARG) {
  6993. cbcDWK = 0;
  6994. } else {
  6995. cbcDWK = WOLFSSL_FATAL_ERROR;
  6996. }
  6997. }
  6998. wc_AesFree(&aes);
  6999. printf(resultFmt, cbcDWK == 0 ? passed : failed);
  7000. if (cbcDWK != 0) {
  7001. return cbcDWK;
  7002. }
  7003. #endif
  7004. return ret;
  7005. } /* END test_wc_AesCbcEncryptDecrypt */
  7006. /*
  7007. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  7008. */
  7009. static int test_wc_AesCtrEncryptDecrypt (void)
  7010. {
  7011. int ret = 0;
  7012. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  7013. Aes aesEnc, aesDec;
  7014. byte key32[] =
  7015. {
  7016. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7017. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7018. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7019. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  7020. };
  7021. byte vector[] = /* Now is the time for all w/o trailing 0 */
  7022. {
  7023. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7024. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7025. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7026. };
  7027. byte iv[] = "1234567890abcdef";
  7028. byte enc[AES_BLOCK_SIZE * 2];
  7029. byte dec[AES_BLOCK_SIZE * 2];
  7030. /* Init stack variables. */
  7031. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  7032. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  7033. printf(testingFmt, "wc_AesCtrEncrypt()");
  7034. ret = wc_AesInit(&aesEnc, NULL, INVALID_DEVID);
  7035. if (ret != 0)
  7036. return ret;
  7037. ret = wc_AesInit(&aesDec, NULL, INVALID_DEVID);
  7038. if (ret != 0) {
  7039. wc_AesFree(&aesEnc);
  7040. return ret;
  7041. }
  7042. ret = wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2,
  7043. iv, AES_ENCRYPTION);
  7044. if (ret == 0) {
  7045. ret = wc_AesCtrEncrypt(&aesEnc, enc, vector,
  7046. sizeof(vector)/sizeof(byte));
  7047. if (ret == 0) {
  7048. /* Decrypt with wc_AesCtrEncrypt() */
  7049. ret = wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2,
  7050. iv, AES_ENCRYPTION);
  7051. }
  7052. if (ret == 0) {
  7053. ret = wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte));
  7054. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector))) {
  7055. ret = WOLFSSL_FATAL_ERROR;
  7056. }
  7057. }
  7058. }
  7059. /* Test bad args. */
  7060. if (ret == 0) {
  7061. ret = wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte));
  7062. if (ret == BAD_FUNC_ARG) {
  7063. ret = wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte));
  7064. }
  7065. if (ret == BAD_FUNC_ARG) {
  7066. ret = wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte));
  7067. }
  7068. if (ret == BAD_FUNC_ARG) {
  7069. ret = 0;
  7070. } else {
  7071. ret = WOLFSSL_FATAL_ERROR;
  7072. }
  7073. }
  7074. wc_AesFree(&aesEnc);
  7075. wc_AesFree(&aesDec);
  7076. printf(resultFmt, ret == 0 ? passed : failed);
  7077. #endif
  7078. return ret;
  7079. } /* END test_wc_AesCtrEncryptDecrypt */
  7080. /*
  7081. * test function for wc_AesGcmSetKey()
  7082. */
  7083. static int test_wc_AesGcmSetKey (void)
  7084. {
  7085. int ret = 0;
  7086. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  7087. Aes aes;
  7088. #ifdef WOLFSSL_AES_128
  7089. byte key16[] =
  7090. {
  7091. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7092. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  7093. };
  7094. #endif
  7095. #ifdef WOLFSSL_AES_192
  7096. byte key24[] =
  7097. {
  7098. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7099. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7100. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  7101. };
  7102. #endif
  7103. #ifdef WOLFSSL_AES_256
  7104. byte key32[] =
  7105. {
  7106. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7107. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7108. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7109. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  7110. };
  7111. #endif
  7112. byte badKey16[] =
  7113. {
  7114. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7115. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  7116. };
  7117. byte badKey24[] =
  7118. {
  7119. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7120. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7121. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  7122. };
  7123. byte badKey32[] =
  7124. {
  7125. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  7126. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7127. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7128. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  7129. };
  7130. printf(testingFmt, "wc_AesGcmSetKey()");
  7131. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  7132. if (ret != 0)
  7133. return ret;
  7134. #ifdef WOLFSSL_AES_128
  7135. ret = wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte));
  7136. #endif
  7137. #ifdef WOLFSSL_AES_192
  7138. if (ret == 0) {
  7139. ret = wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte));
  7140. }
  7141. #endif
  7142. #ifdef WOLFSSL_AES_256
  7143. if (ret == 0) {
  7144. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  7145. }
  7146. #endif
  7147. /* Pass in bad args. */
  7148. if (ret == 0) {
  7149. ret = wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte));
  7150. if (ret == BAD_FUNC_ARG) {
  7151. ret = wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte));
  7152. }
  7153. if (ret == BAD_FUNC_ARG) {
  7154. ret = wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte));
  7155. }
  7156. if (ret == BAD_FUNC_ARG) {
  7157. ret = 0;
  7158. } else {
  7159. ret = WOLFSSL_FATAL_ERROR;
  7160. }
  7161. }
  7162. wc_AesFree(&aes);
  7163. printf(resultFmt, ret == 0 ? passed : failed);
  7164. #endif
  7165. return ret;
  7166. } /* END test_wc_AesGcmSetKey */
  7167. /*
  7168. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  7169. */
  7170. static int test_wc_AesGcmEncryptDecrypt (void)
  7171. {
  7172. int ret = 0;
  7173. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256)
  7174. Aes aes;
  7175. byte key32[] =
  7176. {
  7177. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7178. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7179. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7180. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  7181. };
  7182. byte vector[] = /* Now is the time for all w/o trailing 0 */
  7183. {
  7184. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7185. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7186. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7187. };
  7188. const byte a[] =
  7189. {
  7190. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  7191. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  7192. 0xab, 0xad, 0xda, 0xd2
  7193. };
  7194. byte iv[] = "1234567890a";
  7195. byte longIV[] = "1234567890abcdefghij";
  7196. byte enc[sizeof(vector)];
  7197. byte resultT[AES_BLOCK_SIZE];
  7198. byte dec[sizeof(vector)];
  7199. int gcmD = WOLFSSL_FATAL_ERROR;
  7200. int gcmE = WOLFSSL_FATAL_ERROR;
  7201. /* Init stack variables. */
  7202. XMEMSET(enc, 0, sizeof(vector));
  7203. XMEMSET(dec, 0, sizeof(vector));
  7204. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  7205. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  7206. if (ret != 0)
  7207. return ret;
  7208. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  7209. if (ret == 0) {
  7210. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector),
  7211. iv, sizeof(iv)/sizeof(byte), resultT,
  7212. sizeof(resultT), a, sizeof(a));
  7213. }
  7214. if (gcmE == 0) { /* If encrypt fails, no decrypt. */
  7215. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector),
  7216. iv, sizeof(iv)/sizeof(byte), resultT,
  7217. sizeof(resultT), a, sizeof(a));
  7218. if(gcmD == 0 && (XMEMCMP(vector, dec, sizeof(vector)) != 0)) {
  7219. gcmD = WOLFSSL_FATAL_ERROR;
  7220. }
  7221. }
  7222. printf(testingFmt, "wc_AesGcmEncrypt()");
  7223. /*Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  7224. if (gcmE == 0) {
  7225. gcmE = wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector),
  7226. iv, sizeof(iv)/sizeof(byte), resultT, sizeof(resultT),
  7227. a, sizeof(a));
  7228. if (gcmE == BAD_FUNC_ARG) {
  7229. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  7230. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  7231. resultT, sizeof(resultT) + 1, a, sizeof(a));
  7232. }
  7233. if (gcmE == BAD_FUNC_ARG) {
  7234. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  7235. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  7236. resultT, sizeof(resultT) - 5, a, sizeof(a));
  7237. }
  7238. if (gcmE == BAD_FUNC_ARG) {
  7239. gcmE = 0;
  7240. } else {
  7241. gcmE = WOLFSSL_FATAL_ERROR;
  7242. }
  7243. }
  7244. /* This case is now considered good. Long IVs are now allowed.
  7245. * Except for the original FIPS release, it still has an upper
  7246. * bound on the IV length. */
  7247. #if !defined(HAVE_FIPS) || \
  7248. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  7249. if (gcmE == 0) {
  7250. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  7251. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT),
  7252. a, sizeof(a));
  7253. }
  7254. #else
  7255. (void)longIV;
  7256. #endif /* Old FIPS */
  7257. /* END wc_AesGcmEncrypt */
  7258. printf(resultFmt, gcmE == 0 ? passed : failed);
  7259. if (gcmE != 0) {
  7260. wc_AesFree(&aes);
  7261. return gcmE;
  7262. }
  7263. #ifdef HAVE_AES_DECRYPT
  7264. printf(testingFmt, "wc_AesGcmDecrypt()");
  7265. if (gcmD == 0) {
  7266. gcmD = wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte),
  7267. iv, sizeof(iv)/sizeof(byte), resultT,
  7268. sizeof(resultT), a, sizeof(a));
  7269. if (gcmD == BAD_FUNC_ARG) {
  7270. gcmD = wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte),
  7271. iv, sizeof(iv)/sizeof(byte), resultT,
  7272. sizeof(resultT), a, sizeof(a));
  7273. }
  7274. if (gcmD == BAD_FUNC_ARG) {
  7275. gcmD = wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte),
  7276. iv, sizeof(iv)/sizeof(byte), resultT,
  7277. sizeof(resultT), a, sizeof(a));
  7278. }
  7279. if (gcmD == BAD_FUNC_ARG) {
  7280. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  7281. NULL, sizeof(iv)/sizeof(byte), resultT,
  7282. sizeof(resultT), a, sizeof(a));
  7283. }
  7284. if (gcmD == BAD_FUNC_ARG) {
  7285. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  7286. iv, sizeof(iv)/sizeof(byte), NULL,
  7287. sizeof(resultT), a, sizeof(a));
  7288. }
  7289. if (gcmD == BAD_FUNC_ARG) {
  7290. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  7291. iv, sizeof(iv)/sizeof(byte), resultT,
  7292. sizeof(resultT) + 1, a, sizeof(a));
  7293. }
  7294. if (gcmD == BAD_FUNC_ARG) {
  7295. gcmD = 0;
  7296. } else {
  7297. gcmD = WOLFSSL_FATAL_ERROR;
  7298. }
  7299. } /* END wc_AesGcmDecrypt */
  7300. printf(resultFmt, gcmD == 0 ? passed : failed);
  7301. #endif /* HAVE_AES_DECRYPT */
  7302. wc_AesFree(&aes);
  7303. #endif
  7304. return ret;
  7305. } /* END test_wc_AesGcmEncryptDecrypt */
  7306. /*
  7307. * unit test for wc_GmacSetKey()
  7308. */
  7309. static int test_wc_GmacSetKey (void)
  7310. {
  7311. int ret = 0;
  7312. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  7313. Gmac gmac;
  7314. byte key16[] =
  7315. {
  7316. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7317. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  7318. };
  7319. #ifdef WOLFSSL_AES_192
  7320. byte key24[] =
  7321. {
  7322. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7323. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7324. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  7325. };
  7326. #endif
  7327. #ifdef WOLFSSL_AES_256
  7328. byte key32[] =
  7329. {
  7330. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7331. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7332. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7333. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  7334. };
  7335. #endif
  7336. byte badKey16[] =
  7337. {
  7338. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7339. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  7340. };
  7341. byte badKey24[] =
  7342. {
  7343. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  7344. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7345. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  7346. };
  7347. byte badKey32[] =
  7348. {
  7349. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7350. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  7351. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7352. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  7353. };
  7354. printf(testingFmt, "wc_GmacSetKey()");
  7355. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  7356. if (ret != 0)
  7357. return ret;
  7358. #ifdef WOLFSSL_AES_128
  7359. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte));
  7360. #endif
  7361. #ifdef WOLFSSL_AES_192
  7362. if (ret == 0) {
  7363. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  7364. }
  7365. #endif
  7366. #ifdef WOLFSSL_AES_256
  7367. if (ret == 0) {
  7368. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  7369. }
  7370. #endif
  7371. /* Pass in bad args. */
  7372. if (ret == 0) {
  7373. ret = wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte));
  7374. if (ret == BAD_FUNC_ARG) {
  7375. ret = wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte));
  7376. }
  7377. if (ret == BAD_FUNC_ARG) {
  7378. ret = wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte));
  7379. }
  7380. if (ret == BAD_FUNC_ARG) {
  7381. ret = wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte));
  7382. }
  7383. if (ret == BAD_FUNC_ARG) {
  7384. ret = wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte));
  7385. }
  7386. if (ret == BAD_FUNC_ARG) {
  7387. ret = 0;
  7388. } else {
  7389. ret = WOLFSSL_FATAL_ERROR;
  7390. }
  7391. }
  7392. wc_AesFree(&gmac.aes);
  7393. printf(resultFmt, ret == 0 ? passed : failed);
  7394. #endif
  7395. return ret;
  7396. } /* END test_wc_GmacSetKey */
  7397. /*
  7398. * unit test for wc_GmacUpdate
  7399. */
  7400. static int test_wc_GmacUpdate (void)
  7401. {
  7402. int ret = 0;
  7403. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  7404. Gmac gmac;
  7405. #ifdef WOLFSSL_AES_128
  7406. const byte key16[] =
  7407. {
  7408. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  7409. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  7410. };
  7411. #endif
  7412. #ifdef WOLFSSL_AES_192
  7413. byte key24[] =
  7414. {
  7415. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  7416. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  7417. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  7418. };
  7419. #endif
  7420. #ifdef WOLFSSL_AES_256
  7421. byte key32[] =
  7422. {
  7423. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  7424. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  7425. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  7426. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  7427. };
  7428. #endif
  7429. #ifdef WOLFSSL_AES_128
  7430. const byte authIn[] =
  7431. {
  7432. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  7433. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  7434. };
  7435. #endif
  7436. #ifdef WOLFSSL_AES_192
  7437. const byte authIn2[] =
  7438. {
  7439. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  7440. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  7441. };
  7442. #endif
  7443. const byte authIn3[] =
  7444. {
  7445. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  7446. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  7447. };
  7448. #ifdef WOLFSSL_AES_128
  7449. const byte tag1[] = /* Known. */
  7450. {
  7451. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  7452. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  7453. };
  7454. #endif
  7455. #ifdef WOLFSSL_AES_192
  7456. const byte tag2[] = /* Known */
  7457. {
  7458. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  7459. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  7460. };
  7461. #endif
  7462. const byte tag3[] = /* Known */
  7463. {
  7464. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  7465. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  7466. };
  7467. #ifdef WOLFSSL_AES_128
  7468. const byte iv[] =
  7469. {
  7470. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  7471. 0xe2, 0x8c, 0x8f, 0x16
  7472. };
  7473. #endif
  7474. #ifdef WOLFSSL_AES_192
  7475. const byte iv2[] =
  7476. {
  7477. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  7478. 0x7e, 0x1a, 0x6f, 0xbc
  7479. };
  7480. #endif
  7481. const byte iv3[] =
  7482. {
  7483. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  7484. 0xc3, 0xfb, 0x6c, 0x8a
  7485. };
  7486. byte tagOut[16];
  7487. byte tagOut2[24];
  7488. byte tagOut3[32];
  7489. /* Init stack varaibles. */
  7490. XMEMSET(tagOut, 0, sizeof(tagOut));
  7491. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  7492. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  7493. printf(testingFmt, "wc_GmacUpdate()");
  7494. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  7495. if (ret != 0)
  7496. return ret;
  7497. #ifdef WOLFSSL_AES_128
  7498. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16));
  7499. if (ret == 0) {
  7500. ret = wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  7501. tagOut, sizeof(tag1));
  7502. if (ret == 0) {
  7503. ret = XMEMCMP(tag1, tagOut, sizeof(tag1));
  7504. }
  7505. }
  7506. #endif
  7507. #ifdef WOLFSSL_AES_192
  7508. if (ret == 0) {
  7509. XMEMSET(&gmac, 0, sizeof(Gmac));
  7510. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  7511. }
  7512. if (ret == 0) {
  7513. ret = wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2,
  7514. sizeof(authIn2), tagOut2, sizeof(tag2));
  7515. }
  7516. if (ret == 0) {
  7517. ret = XMEMCMP(tagOut2, tag2, sizeof(tag2));
  7518. }
  7519. #endif
  7520. #ifdef WOLFSSL_AES_256
  7521. if (ret == 0) {
  7522. XMEMSET(&gmac, 0, sizeof(Gmac));
  7523. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  7524. }
  7525. if (ret == 0) {
  7526. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  7527. sizeof(authIn3), tagOut3, sizeof(tag3));
  7528. }
  7529. if (ret == 0) {
  7530. ret = XMEMCMP(tag3, tagOut3, sizeof(tag3));
  7531. }
  7532. #endif
  7533. /*Pass bad args. */
  7534. if (ret == 0) {
  7535. ret = wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3,
  7536. sizeof(authIn3), tagOut3, sizeof(tag3));
  7537. if (ret == BAD_FUNC_ARG) {
  7538. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  7539. sizeof(authIn3), tagOut3, sizeof(tag3) - 5);
  7540. }
  7541. if (ret == BAD_FUNC_ARG) {
  7542. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  7543. sizeof(authIn3), tagOut3, sizeof(tag3) + 1);
  7544. }
  7545. if (ret == BAD_FUNC_ARG) {
  7546. ret = 0;
  7547. } else {
  7548. ret = WOLFSSL_FATAL_ERROR;
  7549. }
  7550. }
  7551. wc_AesFree(&gmac.aes);
  7552. printf(resultFmt, ret == 0 ? passed : failed);
  7553. #endif
  7554. return ret;
  7555. } /* END test_wc_GmacUpdate */
  7556. /*
  7557. * testing wc_CamelliaSetKey
  7558. */
  7559. static int test_wc_CamelliaSetKey (void)
  7560. {
  7561. int ret = 0;
  7562. #ifdef HAVE_CAMELLIA
  7563. Camellia camellia;
  7564. /*128-bit key*/
  7565. static const byte key16[] =
  7566. {
  7567. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  7568. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  7569. };
  7570. /* 192-bit key */
  7571. static const byte key24[] =
  7572. {
  7573. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  7574. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  7575. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  7576. };
  7577. /* 256-bit key */
  7578. static const byte key32[] =
  7579. {
  7580. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  7581. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  7582. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  7583. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  7584. };
  7585. static const byte iv[] =
  7586. {
  7587. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  7588. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  7589. };
  7590. printf(testingFmt, "wc_CamelliaSetKey()");
  7591. ret = wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv);
  7592. if (ret == 0) {
  7593. ret = wc_CamelliaSetKey(&camellia, key16,
  7594. (word32)sizeof(key16), NULL);
  7595. if (ret == 0) {
  7596. ret = wc_CamelliaSetKey(&camellia, key24,
  7597. (word32)sizeof(key24), iv);
  7598. }
  7599. if (ret == 0) {
  7600. ret = wc_CamelliaSetKey(&camellia, key24,
  7601. (word32)sizeof(key24), NULL);
  7602. }
  7603. if (ret == 0) {
  7604. ret = wc_CamelliaSetKey(&camellia, key32,
  7605. (word32)sizeof(key32), iv);
  7606. }
  7607. if (ret == 0) {
  7608. ret = wc_CamelliaSetKey(&camellia, key32,
  7609. (word32)sizeof(key32), NULL);
  7610. }
  7611. }
  7612. /* Bad args. */
  7613. if (ret == 0) {
  7614. ret = wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv);
  7615. if (ret != BAD_FUNC_ARG) {
  7616. ret = WOLFSSL_FATAL_ERROR;
  7617. } else {
  7618. ret = 0;
  7619. }
  7620. } /* END bad args. */
  7621. #endif
  7622. return ret;
  7623. } /* END test_wc_CammeliaSetKey */
  7624. /*
  7625. * Testing wc_CamelliaSetIV()
  7626. */
  7627. static int test_wc_CamelliaSetIV (void)
  7628. {
  7629. int ret = 0;
  7630. #ifdef HAVE_CAMELLIA
  7631. Camellia camellia;
  7632. static const byte iv[] =
  7633. {
  7634. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  7635. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  7636. };
  7637. printf(testingFmt, "wc_CamelliaSetIV()");
  7638. ret = wc_CamelliaSetIV(&camellia, iv);
  7639. if (ret == 0) {
  7640. ret = wc_CamelliaSetIV(&camellia, NULL);
  7641. }
  7642. /* Bad args. */
  7643. if (ret == 0) {
  7644. ret = wc_CamelliaSetIV(NULL, NULL);
  7645. if (ret != BAD_FUNC_ARG) {
  7646. ret = WOLFSSL_FATAL_ERROR;
  7647. } else {
  7648. ret = 0;
  7649. }
  7650. }
  7651. printf(resultFmt, ret == 0 ? passed : failed);
  7652. #endif
  7653. return ret;
  7654. } /*END test_wc_CamelliaSetIV*/
  7655. /*
  7656. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  7657. */
  7658. static int test_wc_CamelliaEncryptDecryptDirect (void)
  7659. {
  7660. int ret = 0;
  7661. #ifdef HAVE_CAMELLIA
  7662. Camellia camellia;
  7663. static const byte key24[] =
  7664. {
  7665. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  7666. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  7667. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  7668. };
  7669. static const byte iv[] =
  7670. {
  7671. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  7672. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  7673. };
  7674. static const byte plainT[] =
  7675. {
  7676. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  7677. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  7678. };
  7679. byte enc[sizeof(plainT)];
  7680. byte dec[sizeof(enc)];
  7681. int camE = WOLFSSL_FATAL_ERROR;
  7682. int camD = WOLFSSL_FATAL_ERROR;
  7683. /*Init stack variables.*/
  7684. XMEMSET(enc, 0, 16);
  7685. XMEMSET(enc, 0, 16);
  7686. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv);
  7687. if (ret == 0) {
  7688. ret = wc_CamelliaEncryptDirect(&camellia, enc, plainT);
  7689. if (ret == 0) {
  7690. ret = wc_CamelliaDecryptDirect(&camellia, dec, enc);
  7691. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  7692. ret = WOLFSSL_FATAL_ERROR;
  7693. }
  7694. }
  7695. }
  7696. printf(testingFmt, "wc_CamelliaEncryptDirect()");
  7697. /* Pass bad args. */
  7698. if (ret == 0) {
  7699. camE = wc_CamelliaEncryptDirect(NULL, enc, plainT);
  7700. if (camE == BAD_FUNC_ARG) {
  7701. camE = wc_CamelliaEncryptDirect(&camellia, NULL, plainT);
  7702. }
  7703. if (camE == BAD_FUNC_ARG) {
  7704. camE = wc_CamelliaEncryptDirect(&camellia, enc, NULL);
  7705. }
  7706. if (camE == BAD_FUNC_ARG) {
  7707. camE = 0;
  7708. } else {
  7709. camE = WOLFSSL_FATAL_ERROR;
  7710. }
  7711. }
  7712. printf(resultFmt, camE == 0 ? passed : failed);
  7713. if (camE != 0) {
  7714. return camE;
  7715. }
  7716. printf(testingFmt, "wc_CamelliaDecryptDirect()");
  7717. if (ret == 0) {
  7718. camD = wc_CamelliaDecryptDirect(NULL, dec, enc);
  7719. if (camD == BAD_FUNC_ARG) {
  7720. camD = wc_CamelliaDecryptDirect(&camellia, NULL, enc);
  7721. }
  7722. if (camD == BAD_FUNC_ARG) {
  7723. camD = wc_CamelliaDecryptDirect(&camellia, dec, NULL);
  7724. }
  7725. if (camD == BAD_FUNC_ARG) {
  7726. camD = 0;
  7727. } else {
  7728. camD = WOLFSSL_FATAL_ERROR;
  7729. }
  7730. }
  7731. printf(resultFmt, camD == 0 ? passed : failed);
  7732. if (camD != 0) {
  7733. return camD;
  7734. }
  7735. #endif
  7736. return ret;
  7737. } /* END test-wc_CamelliaEncryptDecryptDirect */
  7738. /*
  7739. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  7740. */
  7741. static int test_wc_CamelliaCbcEncryptDecrypt (void)
  7742. {
  7743. int ret = 0;
  7744. #ifdef HAVE_CAMELLIA
  7745. Camellia camellia;
  7746. static const byte key24[] =
  7747. {
  7748. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  7749. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  7750. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  7751. };
  7752. static const byte plainT[] =
  7753. {
  7754. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  7755. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  7756. };
  7757. byte enc[CAMELLIA_BLOCK_SIZE];
  7758. byte dec[CAMELLIA_BLOCK_SIZE];
  7759. int camCbcE = WOLFSSL_FATAL_ERROR;
  7760. int camCbcD = WOLFSSL_FATAL_ERROR;
  7761. /* Init stack variables. */
  7762. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  7763. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  7764. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  7765. if (ret == 0) {
  7766. ret = wc_CamelliaCbcEncrypt(&camellia, enc, plainT, CAMELLIA_BLOCK_SIZE);
  7767. if (ret != 0) {
  7768. ret = WOLFSSL_FATAL_ERROR;
  7769. }
  7770. }
  7771. if (ret == 0) {
  7772. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  7773. if (ret == 0) {
  7774. ret = wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE);
  7775. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  7776. ret = WOLFSSL_FATAL_ERROR;
  7777. }
  7778. }
  7779. }
  7780. printf(testingFmt, "wc_CamelliaCbcEncrypt");
  7781. /* Pass in bad args. */
  7782. if (ret == 0) {
  7783. camCbcE = wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE);
  7784. if (camCbcE == BAD_FUNC_ARG) {
  7785. camCbcE = wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  7786. CAMELLIA_BLOCK_SIZE);
  7787. }
  7788. if (camCbcE == BAD_FUNC_ARG) {
  7789. camCbcE = wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  7790. CAMELLIA_BLOCK_SIZE);
  7791. }
  7792. if (camCbcE == BAD_FUNC_ARG) {
  7793. camCbcE = 0;
  7794. } else {
  7795. camCbcE = WOLFSSL_FATAL_ERROR;
  7796. }
  7797. }
  7798. printf(resultFmt, camCbcE == 0 ? passed : failed);
  7799. if (camCbcE != 0) {
  7800. return camCbcE;
  7801. }
  7802. printf(testingFmt, "wc_CamelliaCbcDecrypt()");
  7803. if (ret == 0) {
  7804. camCbcD = wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE);
  7805. if (camCbcD == BAD_FUNC_ARG) {
  7806. camCbcD = wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  7807. CAMELLIA_BLOCK_SIZE);
  7808. }
  7809. if (camCbcD == BAD_FUNC_ARG) {
  7810. camCbcD = wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  7811. CAMELLIA_BLOCK_SIZE);
  7812. }
  7813. if (camCbcD == BAD_FUNC_ARG) {
  7814. camCbcD = 0;
  7815. } else {
  7816. camCbcD = WOLFSSL_FATAL_ERROR;
  7817. }
  7818. } /* END bad args. */
  7819. printf(resultFmt, camCbcD == 0 ? passed : failed);
  7820. if (camCbcD != 0) {
  7821. return camCbcD;
  7822. }
  7823. #endif
  7824. return ret;
  7825. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  7826. /*
  7827. * Testing wc_RabbitSetKey()
  7828. */
  7829. static int test_wc_RabbitSetKey (void)
  7830. {
  7831. int ret = 0;
  7832. #ifndef NO_RABBIT
  7833. Rabbit rabbit;
  7834. const char* key = "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B"
  7835. "\xFE\x36\x3D\x2E\x29\x13\x28\x91";
  7836. const char* iv = "\x59\x7E\x26\xC1\x75\xF5\x73\xC3";
  7837. printf(testingFmt, "wc_RabbitSetKey()");
  7838. ret = wc_RabbitSetKey(&rabbit, (byte*)key, (byte*)iv);
  7839. /* Test bad args. */
  7840. if (ret == 0) {
  7841. ret = wc_RabbitSetKey(NULL, (byte*)key, (byte*)iv);
  7842. if (ret == BAD_FUNC_ARG) {
  7843. ret = wc_RabbitSetKey(&rabbit, NULL, (byte*)iv);
  7844. }
  7845. if (ret == BAD_FUNC_ARG) {
  7846. ret = wc_RabbitSetKey(&rabbit, (byte*)key, NULL);
  7847. }
  7848. }
  7849. printf(resultFmt, ret == 0 ? passed : failed);
  7850. #endif
  7851. return ret;
  7852. } /* END test_wc_RabbitSetKey */
  7853. /*
  7854. * Test wc_RabbitProcess()
  7855. */
  7856. static int test_wc_RabbitProcess (void)
  7857. {
  7858. int ret = 0;
  7859. #ifndef NO_RABBIT
  7860. Rabbit enc, dec;
  7861. byte cipher[25];
  7862. byte plain[25];
  7863. const char* key = "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B"
  7864. "\xFE\x36\x3D\x2E\x29\x13\x28\x91";
  7865. const char* iv = "\x59\x7E\x26\xC1\x75\xF5\x73\xC3";
  7866. const char* input = "Everyone gets Friday off.";
  7867. unsigned long int inlen = XSTRLEN(input);
  7868. /* Initialize stack variables. */
  7869. XMEMSET(cipher, 0, sizeof(cipher));
  7870. XMEMSET(plain, 0, sizeof(plain));
  7871. printf(testingFmt, "wc_RabbitProcess()");
  7872. ret = wc_RabbitSetKey(&enc, (byte*)key, (byte*)iv);
  7873. if (ret == 0) {
  7874. ret = wc_RabbitSetKey(&dec, (byte*)key, (byte*)iv);
  7875. }
  7876. if (ret == 0) {
  7877. ret = wc_RabbitProcess(&enc, cipher, (byte*)input, (word32)inlen);
  7878. }
  7879. if (ret == 0) {
  7880. ret = wc_RabbitProcess(&dec, plain, cipher, (word32)inlen);
  7881. if (ret != 0 || XMEMCMP(input, plain, inlen)) {
  7882. ret = WOLFSSL_FATAL_ERROR;
  7883. } else {
  7884. ret = 0;
  7885. }
  7886. }
  7887. /* Test bad args. */
  7888. if (ret == 0) {
  7889. ret = wc_RabbitProcess(NULL, plain, cipher, (word32)inlen);
  7890. if (ret == BAD_FUNC_ARG) {
  7891. ret = wc_RabbitProcess(&dec, NULL, cipher, (word32)inlen);
  7892. }
  7893. if (ret == BAD_FUNC_ARG) {
  7894. ret = wc_RabbitProcess(&dec, plain, NULL, (word32)inlen);
  7895. }
  7896. if (ret == BAD_FUNC_ARG) {
  7897. ret = 0;
  7898. } else {
  7899. ret = WOLFSSL_FATAL_ERROR;
  7900. }
  7901. }
  7902. printf(resultFmt, ret == 0 ? passed : failed);
  7903. #endif
  7904. return ret;
  7905. } /* END test_wc_RabbitProcess */
  7906. /*
  7907. * Testing wc_Arc4SetKey()
  7908. */
  7909. static int test_wc_Arc4SetKey (void)
  7910. {
  7911. int ret = 0;
  7912. #ifndef NO_RC4
  7913. Arc4 arc;
  7914. const char* key[] =
  7915. {
  7916. "\x01\x23\x45\x67\x89\xab\xcd\xef"
  7917. };
  7918. int keyLen = 8;
  7919. printf(testingFmt, "wc_Arch4SetKey()");
  7920. ret = wc_Arc4SetKey(&arc, (byte*)key, keyLen);
  7921. /* Test bad args. */
  7922. if (ret == 0) {
  7923. ret = wc_Arc4SetKey(NULL, (byte*)key, keyLen);
  7924. if (ret == BAD_FUNC_ARG) {
  7925. ret = wc_Arc4SetKey(&arc, NULL, keyLen);
  7926. }
  7927. if (ret == BAD_FUNC_ARG) {
  7928. /* Exits normally if keyLen is incorrect. */
  7929. ret = wc_Arc4SetKey(&arc, (byte*)key, 0);
  7930. } else {
  7931. ret = WOLFSSL_FATAL_ERROR;
  7932. }
  7933. } /* END test bad args. */
  7934. printf(resultFmt, ret == 0 ? passed : failed);
  7935. #endif
  7936. return ret;
  7937. } /* END test_wc_Arc4SetKey */
  7938. /*
  7939. * Testing wc_Arc4Process for ENC/DEC.
  7940. */
  7941. static int test_wc_Arc4Process (void)
  7942. {
  7943. int ret = 0;
  7944. #ifndef NO_RC4
  7945. Arc4 enc, dec;
  7946. const char* key[] = {"\x01\x23\x45\x67\x89\xab\xcd\xef"};
  7947. int keyLen = 8;
  7948. const char* input[] = {"\x01\x23\x45\x67\x89\xab\xcd\xef"};
  7949. byte cipher[8];
  7950. byte plain[8];
  7951. /* Init stack variables */
  7952. XMEMSET(cipher, 0, sizeof(cipher));
  7953. XMEMSET(plain, 0, sizeof(plain));
  7954. /* Use for async. */
  7955. ret = wc_Arc4Init(&enc, NULL, INVALID_DEVID);
  7956. if (ret == 0) {
  7957. ret = wc_Arc4Init(&dec, NULL, INVALID_DEVID);
  7958. }
  7959. printf(testingFmt, "wc_Arc4Process()");
  7960. if (ret == 0) {
  7961. ret = wc_Arc4SetKey(&enc, (byte*)key, keyLen);
  7962. }
  7963. if (ret == 0) {
  7964. ret = wc_Arc4SetKey(&dec, (byte*)key, keyLen);
  7965. }
  7966. if (ret == 0) {
  7967. ret = wc_Arc4Process(&enc, cipher, (byte*)input, keyLen);
  7968. }
  7969. if (ret == 0) {
  7970. ret = wc_Arc4Process(&dec, plain, cipher, keyLen);
  7971. if (ret != 0 || XMEMCMP(plain, input, keyLen)) {
  7972. ret = WOLFSSL_FATAL_ERROR;
  7973. } else {
  7974. ret = 0;
  7975. }
  7976. }
  7977. /* Bad args. */
  7978. if (ret == 0) {
  7979. ret = wc_Arc4Process(NULL, plain, cipher, keyLen);
  7980. if (ret == BAD_FUNC_ARG) {
  7981. ret = wc_Arc4Process(&dec, NULL, cipher, keyLen);
  7982. }
  7983. if (ret == BAD_FUNC_ARG) {
  7984. ret = wc_Arc4Process(&dec, plain, NULL, keyLen);
  7985. }
  7986. if (ret == BAD_FUNC_ARG) {
  7987. ret = 0;
  7988. } else {
  7989. ret = WOLFSSL_FATAL_ERROR;
  7990. }
  7991. }
  7992. printf(resultFmt, ret == 0 ? passed : failed);
  7993. wc_Arc4Free(&enc);
  7994. wc_Arc4Free(&dec);
  7995. #endif
  7996. return ret;
  7997. }/* END test_wc_Arc4Process */
  7998. /*
  7999. * Testing wc_Init RsaKey()
  8000. */
  8001. static int test_wc_InitRsaKey (void)
  8002. {
  8003. int ret = 0;
  8004. #ifndef NO_RSA
  8005. RsaKey key;
  8006. printf(testingFmt, "wc_InitRsaKey()");
  8007. ret = wc_InitRsaKey(&key, NULL);
  8008. /* Test bad args. */
  8009. if (ret == 0) {
  8010. ret = wc_InitRsaKey(NULL, NULL);
  8011. #ifndef HAVE_USER_RSA
  8012. if (ret == BAD_FUNC_ARG) {
  8013. ret = 0;
  8014. } else {
  8015. #else
  8016. if (ret == USER_CRYPTO_ERROR) {
  8017. ret = 0;
  8018. } else {
  8019. #endif
  8020. ret = WOLFSSL_FATAL_ERROR;
  8021. }
  8022. } /* end if */
  8023. if (wc_FreeRsaKey(&key) || ret != 0) {
  8024. ret = WOLFSSL_FATAL_ERROR;
  8025. }
  8026. printf(resultFmt, ret == 0 ? passed : failed);
  8027. #endif
  8028. return ret;
  8029. } /* END test_wc_InitRsaKey */
  8030. /*
  8031. * Testing wc_RsaPrivateKeyDecode()
  8032. */
  8033. static int test_wc_RsaPrivateKeyDecode (void)
  8034. {
  8035. int ret = 0;
  8036. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  8037. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  8038. RsaKey key;
  8039. byte* tmp;
  8040. word32 idx = 0;
  8041. int bytes = 0;
  8042. printf(testingFmt, "wc_RsaPrivateKeyDecode()");
  8043. tmp = (byte*)XMALLOC(FOURK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8044. if (tmp == NULL) {
  8045. ret = WOLFSSL_FATAL_ERROR;
  8046. }
  8047. if (ret == 0) {
  8048. ret = wc_InitRsaKey(&key, NULL);
  8049. }
  8050. if (ret == 0) {
  8051. #ifdef USE_CERT_BUFFERS_1024
  8052. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  8053. bytes = sizeof_client_key_der_1024;
  8054. #else
  8055. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  8056. bytes = sizeof_client_key_der_2048;
  8057. #endif /* Use cert buffers. */
  8058. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  8059. }
  8060. #ifndef HAVE_USER_RSA
  8061. /* Test bad args. */
  8062. if (ret == 0) {
  8063. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  8064. if (ret == ASN_PARSE_E) {
  8065. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  8066. }
  8067. if (ret == BAD_FUNC_ARG) {
  8068. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  8069. }
  8070. if (ret == ASN_PARSE_E) {
  8071. ret = 0;
  8072. } else {
  8073. ret = WOLFSSL_FATAL_ERROR;
  8074. }
  8075. }
  8076. #else
  8077. /* Test bad args. User RSA. */
  8078. if (ret == 0) {
  8079. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  8080. if (ret == USER_CRYPTO_ERROR) {
  8081. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  8082. }
  8083. if (ret == USER_CRYPTO_ERROR) {
  8084. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  8085. }
  8086. if (ret == USER_CRYPTO_ERROR) {
  8087. ret = 0;
  8088. } else {
  8089. ret = WOLFSSL_FATAL_ERROR;
  8090. }
  8091. }
  8092. #endif
  8093. if (tmp != NULL) {
  8094. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8095. }
  8096. if (wc_FreeRsaKey(&key) || ret != 0) {
  8097. ret = WOLFSSL_FATAL_ERROR;
  8098. }
  8099. printf(resultFmt, ret == 0 ? passed : failed);
  8100. #endif
  8101. return ret;
  8102. } /* END test_wc_RsaPrivateKeyDecode */
  8103. /*
  8104. * Testing wc_RsaPublicKeyDecode()
  8105. */
  8106. static int test_wc_RsaPublicKeyDecode (void)
  8107. {
  8108. int ret = 0;
  8109. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  8110. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  8111. RsaKey keyPub;
  8112. byte* tmp;
  8113. word32 idx = 0;
  8114. int bytes = 0;
  8115. tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8116. if (tmp == NULL) {
  8117. ret = WOLFSSL_FATAL_ERROR;
  8118. }
  8119. if (ret == 0) {
  8120. ret = wc_InitRsaKey(&keyPub, NULL);
  8121. }
  8122. if (ret == 0) {
  8123. #ifdef USE_CERT_BUFFERS_1024
  8124. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  8125. bytes = sizeof_client_keypub_der_1024;
  8126. #else
  8127. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  8128. bytes = sizeof_client_keypub_der_2048;
  8129. #endif
  8130. printf(testingFmt, "wc_RsaPublicKeyDecode()");
  8131. ret = wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes);
  8132. }
  8133. #ifndef HAVE_USER_RSA
  8134. /* Pass in bad args. */
  8135. if (ret == 0) {
  8136. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  8137. if (ret == BAD_FUNC_ARG) {
  8138. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  8139. }
  8140. if (ret == BAD_FUNC_ARG) {
  8141. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  8142. }
  8143. if (ret == BAD_FUNC_ARG) {
  8144. ret = 0;
  8145. } else {
  8146. ret = WOLFSSL_FATAL_ERROR;
  8147. }
  8148. }
  8149. #else
  8150. /* Pass in bad args. */
  8151. if (ret == 0) {
  8152. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  8153. if (ret == USER_CRYPTO_ERROR) {
  8154. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  8155. }
  8156. if (ret == USER_CRYPTO_ERROR) {
  8157. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  8158. }
  8159. if (ret == USER_CRYPTO_ERROR) {
  8160. ret = 0;
  8161. } else {
  8162. ret = WOLFSSL_FATAL_ERROR;
  8163. }
  8164. }
  8165. #endif
  8166. if (tmp != NULL) {
  8167. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8168. }
  8169. if (wc_FreeRsaKey(&keyPub) || ret != 0) {
  8170. ret = WOLFSSL_FATAL_ERROR;
  8171. }
  8172. printf(resultFmt, ret == 0 ? passed : failed);
  8173. #endif
  8174. return ret;
  8175. } /* END test_wc_RsaPublicKeyDecode */
  8176. /*
  8177. * Testing wc_RsaPublicKeyDecodeRaw()
  8178. */
  8179. static int test_wc_RsaPublicKeyDecodeRaw (void)
  8180. {
  8181. int ret = 0;
  8182. #if !defined(NO_RSA)
  8183. RsaKey key;
  8184. const byte n = 0x23;
  8185. const byte e = 0x03;
  8186. int nSz = sizeof(n);
  8187. int eSz = sizeof(e);
  8188. printf(testingFmt, "wc_RsaPublicKeyDecodeRaw()");
  8189. ret = wc_InitRsaKey(&key, NULL);
  8190. if (ret == 0) {
  8191. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key);
  8192. }
  8193. #ifndef HAVE_USER_RSA
  8194. /* Pass in bad args. */
  8195. if (ret == 0) {
  8196. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  8197. if (ret == BAD_FUNC_ARG) {
  8198. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  8199. }
  8200. if (ret == BAD_FUNC_ARG) {
  8201. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  8202. }
  8203. if (ret == BAD_FUNC_ARG) {
  8204. ret = 0;
  8205. } else {
  8206. ret = WOLFSSL_FATAL_ERROR;
  8207. }
  8208. }
  8209. #else
  8210. /* Pass in bad args. User RSA. */
  8211. if (ret == 0) {
  8212. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  8213. if (ret == USER_CRYPTO_ERROR) {
  8214. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  8215. }
  8216. if (ret == USER_CRYPTO_ERROR) {
  8217. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  8218. }
  8219. if (ret == USER_CRYPTO_ERROR) {
  8220. ret = 0;
  8221. } else {
  8222. ret = WOLFSSL_FATAL_ERROR;
  8223. }
  8224. }
  8225. #endif
  8226. if (wc_FreeRsaKey(&key) || ret != 0) {
  8227. ret = WOLFSSL_FATAL_ERROR;
  8228. }
  8229. printf(resultFmt, ret == 0 ? passed : failed);
  8230. #endif
  8231. return ret;
  8232. } /* END test_wc_RsaPublicKeyDecodeRaw */
  8233. #if (!defined(NO_RSA) || !defined(HAVE_FAST_RSA)) && (defined(WOLFSSL_KEY_GEN) || \
  8234. defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL))
  8235. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  8236. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  8237. * trying until it gets a probable prime. */
  8238. #ifdef WOLFSSL_FIPS
  8239. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  8240. {
  8241. int ret;
  8242. for (;;) {
  8243. ret = wc_MakeRsaKey(key, size, e, rng);
  8244. if (ret != PRIME_GEN_E) break;
  8245. printf("MakeRsaKey couldn't find prime; trying again.\n");
  8246. }
  8247. return ret;
  8248. }
  8249. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  8250. #else
  8251. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  8252. #endif
  8253. #endif
  8254. /*
  8255. * Testing wc_MakeRsaKey()
  8256. */
  8257. static int test_wc_MakeRsaKey (void)
  8258. {
  8259. int ret = 0;
  8260. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  8261. RsaKey genKey;
  8262. WC_RNG rng;
  8263. printf(testingFmt, "wc_MakeRsaKey()");
  8264. ret = wc_InitRsaKey(&genKey, NULL);
  8265. if (ret == 0) {
  8266. ret = wc_InitRng(&rng);
  8267. if (ret == 0) {
  8268. ret = MAKE_RSA_KEY(&genKey, 1024, WC_RSA_EXPONENT, &rng);
  8269. if (ret == 0 && wc_FreeRsaKey(&genKey) != 0) {
  8270. ret = WOLFSSL_FATAL_ERROR;
  8271. }
  8272. }
  8273. }
  8274. #ifndef HAVE_USER_RSA
  8275. /* Test bad args. */
  8276. if (ret == 0) {
  8277. ret = MAKE_RSA_KEY(NULL, 1024, WC_RSA_EXPONENT, &rng);
  8278. if (ret == BAD_FUNC_ARG) {
  8279. ret = MAKE_RSA_KEY(&genKey, 1024, WC_RSA_EXPONENT, NULL);
  8280. }
  8281. if (ret == BAD_FUNC_ARG) {
  8282. /* e < 3 */
  8283. ret = MAKE_RSA_KEY(&genKey, 1024, 2, &rng);
  8284. }
  8285. if (ret == BAD_FUNC_ARG) {
  8286. /* e & 1 == 0 */
  8287. ret = MAKE_RSA_KEY(&genKey, 1024, 6, &rng);
  8288. }
  8289. if (ret == BAD_FUNC_ARG) {
  8290. ret = 0;
  8291. } else {
  8292. ret = WOLFSSL_FATAL_ERROR;
  8293. }
  8294. }
  8295. #else
  8296. /* Test bad args. */
  8297. if (ret == 0) {
  8298. ret = MAKE_RSA_KEY(NULL, 1024, WC_RSA_EXPONENT, &rng);
  8299. if (ret == USER_CRYPTO_ERROR) {
  8300. ret = MAKE_RSA_KEY(&genKey, 1024, WC_RSA_EXPONENT, NULL);
  8301. }
  8302. if (ret == USER_CRYPTO_ERROR) {
  8303. /* e < 3 */
  8304. ret = MAKE_RSA_KEY(&genKey, 1024, 2, &rng);
  8305. }
  8306. if (ret == USER_CRYPTO_ERROR) {
  8307. /* e & 1 == 0 */
  8308. ret = MAKE_RSA_KEY(&genKey, 1024, 6, &rng);
  8309. }
  8310. if (ret == USER_CRYPTO_ERROR) {
  8311. ret = 0;
  8312. } else {
  8313. ret = WOLFSSL_FATAL_ERROR;
  8314. }
  8315. }
  8316. #endif
  8317. if (wc_FreeRng(&rng) || ret != 0) {
  8318. ret = WOLFSSL_FATAL_ERROR;
  8319. }
  8320. printf(resultFmt, ret == 0 ? passed : failed);
  8321. #endif
  8322. return ret;
  8323. } /* END test_wc_MakeRsaKey */
  8324. /*
  8325. * Test the bounds checking on the cipher text versus the key modulus.
  8326. * 1. Make a new RSA key.
  8327. * 2. Set c to 1.
  8328. * 3. Decrypt c into k. (error)
  8329. * 4. Copy the key modulus to c and sub 1 from the copy.
  8330. * 5. Decrypt c into k. (error)
  8331. * Valid bounds test cases are covered by all the other RSA tests.
  8332. */
  8333. static int test_RsaDecryptBoundsCheck(void)
  8334. {
  8335. int ret = 0;
  8336. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  8337. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  8338. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  8339. RsaKey key;
  8340. byte flatC[256];
  8341. word32 flatCSz;
  8342. byte out[256];
  8343. word32 outSz = sizeof(out);
  8344. WC_RNG rng;
  8345. printf(testingFmt, "RSA decrypt bounds check");
  8346. ret = wc_InitRng(&rng);
  8347. if (ret == 0)
  8348. ret = wc_InitRsaKey(&key, NULL);
  8349. if (ret == 0) {
  8350. const byte* derKey;
  8351. word32 derKeySz;
  8352. word32 idx = 0;
  8353. #ifdef USE_CERT_BUFFERS_1024
  8354. derKey = server_key_der_1024;
  8355. derKeySz = (word32)sizeof_server_key_der_1024;
  8356. flatCSz = 128;
  8357. #else
  8358. derKey = server_key_der_2048;
  8359. derKeySz = (word32)sizeof_server_key_der_2048;
  8360. flatCSz = 256;
  8361. #endif
  8362. ret = wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz);
  8363. }
  8364. if (ret == 0) {
  8365. XMEMSET(flatC, 0, flatCSz);
  8366. flatC[flatCSz-1] = 1;
  8367. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  8368. RSA_PRIVATE_DECRYPT, &rng);
  8369. }
  8370. if (ret == RSA_OUT_OF_RANGE_E) {
  8371. mp_int c;
  8372. mp_init_copy(&c, &key.n);
  8373. mp_sub_d(&c, 1, &c);
  8374. mp_to_unsigned_bin(&c, flatC);
  8375. ret = wc_RsaDirect(flatC, sizeof(flatC), out, &outSz, &key,
  8376. RSA_PRIVATE_DECRYPT, NULL);
  8377. mp_clear(&c);
  8378. }
  8379. if (ret == RSA_OUT_OF_RANGE_E)
  8380. ret = 0;
  8381. if (wc_FreeRsaKey(&key) || wc_FreeRng(&rng) || ret != 0)
  8382. ret = WOLFSSL_FATAL_ERROR;
  8383. printf(resultFmt, ret == 0 ? passed : failed);
  8384. #endif
  8385. return ret;
  8386. } /* END test_wc_RsaDecryptBoundsCheck */
  8387. /*
  8388. * Testing wc_SetKeyUsage()
  8389. */
  8390. static int test_wc_SetKeyUsage (void)
  8391. {
  8392. int ret = 0;
  8393. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && !defined(HAVE_FIPS)
  8394. Cert myCert;
  8395. ret = wc_InitCert(&myCert);
  8396. printf(testingFmt, "wc_SetKeyUsage()");
  8397. if (ret == 0) {
  8398. ret = wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement");
  8399. if (ret == 0) {
  8400. ret = wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation");
  8401. }
  8402. if (ret == 0) {
  8403. ret = wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly");
  8404. }
  8405. if (ret == 0) {
  8406. ret = wc_SetKeyUsage(&myCert, "decipherOnly");
  8407. }
  8408. if (ret == 0) {
  8409. ret = wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign");
  8410. }
  8411. }
  8412. /* Test bad args. */
  8413. if (ret == 0) {
  8414. ret = wc_SetKeyUsage(NULL, "decipherOnly");
  8415. if (ret == BAD_FUNC_ARG) {
  8416. ret = wc_SetKeyUsage(&myCert, NULL);
  8417. }
  8418. if (ret == BAD_FUNC_ARG) {
  8419. ret = wc_SetKeyUsage(&myCert, "");
  8420. }
  8421. if (ret == KEYUSAGE_E) {
  8422. ret = wc_SetKeyUsage(&myCert, ",");
  8423. }
  8424. if (ret == KEYUSAGE_E) {
  8425. ret = wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign");
  8426. }
  8427. if (ret == KEYUSAGE_E) {
  8428. ret = 0;
  8429. } else {
  8430. ret = WOLFSSL_FATAL_ERROR;
  8431. }
  8432. }
  8433. printf(resultFmt, ret == 0 ? passed : failed);
  8434. #endif
  8435. return ret;
  8436. } /* END test_wc_SetKeyUsage */
  8437. /*
  8438. * Testing wc_RsaKeyToDer()
  8439. */
  8440. static int test_wc_RsaKeyToDer (void)
  8441. {
  8442. int ret = 0;
  8443. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  8444. RsaKey genKey;
  8445. WC_RNG rng;
  8446. byte* der;
  8447. word32 derSz = 611;
  8448. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  8449. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  8450. der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8451. if (der == NULL) {
  8452. ret = WOLFSSL_FATAL_ERROR;
  8453. }
  8454. /* Init structures. */
  8455. if (ret == 0) {
  8456. ret = wc_InitRsaKey(&genKey, NULL);
  8457. }
  8458. if (ret == 0) {
  8459. ret = wc_InitRng(&rng);
  8460. }
  8461. /* Make key. */
  8462. if (ret == 0) {
  8463. ret = MAKE_RSA_KEY(&genKey, 1024, WC_RSA_EXPONENT, &rng);
  8464. if (ret != 0) {
  8465. ret = WOLFSSL_FATAL_ERROR;
  8466. }
  8467. }
  8468. printf(testingFmt, "wc_RsaKeyToDer()");
  8469. if (ret == 0) {
  8470. ret = wc_RsaKeyToDer(&genKey, der, derSz);
  8471. if (ret > 0) {
  8472. ret = 0;
  8473. } else {
  8474. ret = WOLFSSL_FATAL_ERROR;
  8475. }
  8476. }
  8477. #ifndef HAVE_USER_RSA
  8478. /* Pass bad args. */
  8479. if (ret == 0) {
  8480. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  8481. if (ret == BAD_FUNC_ARG) {
  8482. ret = wc_RsaKeyToDer(&genKey, NULL, FOURK_BUF);
  8483. }
  8484. if (ret == BAD_FUNC_ARG) {
  8485. /* Try Public Key. */
  8486. genKey.type = 0;
  8487. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  8488. }
  8489. if (ret == BAD_FUNC_ARG) {
  8490. ret = 0;
  8491. } else {
  8492. ret = WOLFSSL_FATAL_ERROR;
  8493. }
  8494. }
  8495. #else
  8496. /* Pass bad args. */
  8497. if (ret == 0) {
  8498. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  8499. if (ret == USER_CRYPTO_ERROR) {
  8500. ret = wc_RsaKeyToDer(&genKey, NULL, FOURK_BUF);
  8501. }
  8502. if (ret == USER_CRYPTO_ERROR) {
  8503. /* Try Public Key. */
  8504. genKey.type = 0;
  8505. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  8506. }
  8507. if (ret == USER_CRYPTO_ERROR) {
  8508. ret = 0;
  8509. } else {
  8510. ret = WOLFSSL_FATAL_ERROR;
  8511. }
  8512. }
  8513. #endif
  8514. if (der != NULL) {
  8515. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8516. }
  8517. if (wc_FreeRsaKey(&genKey) || ret != 0) {
  8518. ret = WOLFSSL_FATAL_ERROR;
  8519. }
  8520. if (wc_FreeRng(&rng) || ret != 0) {
  8521. ret = WOLFSSL_FATAL_ERROR;
  8522. }
  8523. printf(resultFmt, ret == 0 ? passed : failed);
  8524. #endif
  8525. return ret;
  8526. } /* END test_wc_RsaKeyToDer */
  8527. /*
  8528. * Testing wc_RsaKeyToPublicDer()
  8529. */
  8530. static int test_wc_RsaKeyToPublicDer (void)
  8531. {
  8532. int ret = 0;
  8533. #if !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) &&\
  8534. (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL))
  8535. RsaKey key;
  8536. WC_RNG rng;
  8537. byte* der;
  8538. word32 derLen = 162;
  8539. der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8540. if (der == NULL) {
  8541. ret = WOLFSSL_FATAL_ERROR;
  8542. }
  8543. if (ret == 0) {
  8544. ret = wc_InitRsaKey(&key, NULL);
  8545. }
  8546. if (ret == 0) {
  8547. ret = wc_InitRng(&rng);
  8548. }
  8549. if (ret == 0) {
  8550. ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
  8551. }
  8552. printf(testingFmt, "wc_RsaKeyToPublicDer()");
  8553. if (ret == 0) {
  8554. ret = wc_RsaKeyToPublicDer(&key, der, derLen);
  8555. if (ret >= 0) {
  8556. ret = 0;
  8557. } else {
  8558. ret = WOLFSSL_FATAL_ERROR;
  8559. }
  8560. }
  8561. #ifndef HAVE_USER_RSA
  8562. /* Pass in bad args. */
  8563. if (ret == 0) {
  8564. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  8565. if (ret == BAD_FUNC_ARG) {
  8566. ret = wc_RsaKeyToPublicDer(&key, NULL, derLen);
  8567. }
  8568. if (ret == BAD_FUNC_ARG) {
  8569. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  8570. }
  8571. if (ret == BAD_FUNC_ARG) {
  8572. ret = 0;
  8573. } else {
  8574. ret = WOLFSSL_FATAL_ERROR;
  8575. }
  8576. }
  8577. #else
  8578. /* Pass in bad args. */
  8579. if (ret == 0) {
  8580. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  8581. if (ret == USER_CRYPTO_ERROR) {
  8582. ret = wc_RsaKeyToPublicDer(&key, NULL, derLen);
  8583. }
  8584. if (ret == USER_CRYPTO_ERROR) {
  8585. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  8586. }
  8587. if (ret == USER_CRYPTO_ERROR) {
  8588. ret = 0;
  8589. } else {
  8590. ret = WOLFSSL_FATAL_ERROR;
  8591. }
  8592. }
  8593. #endif
  8594. if (der != NULL) {
  8595. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8596. }
  8597. if (wc_FreeRsaKey(&key) || ret != 0) {
  8598. ret = WOLFSSL_FATAL_ERROR;
  8599. }
  8600. if (wc_FreeRng(&rng) || ret != 0) {
  8601. ret = WOLFSSL_FATAL_ERROR;
  8602. }
  8603. printf(resultFmt, ret == 0 ? passed : failed);
  8604. #endif
  8605. return ret;
  8606. } /* END test_wc_RsaKeyToPublicDer */
  8607. /*
  8608. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  8609. */
  8610. static int test_wc_RsaPublicEncryptDecrypt (void)
  8611. {
  8612. int ret = 0;
  8613. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  8614. RsaKey key;
  8615. WC_RNG rng;
  8616. const char* inStr = "Everyone gets Friday off.";
  8617. word32 cipherLen = 128;
  8618. word32 plainLen = 25;
  8619. word32 inLen = (word32)XSTRLEN(inStr);
  8620. DECLARE_VAR_INIT(in, byte, inLen, inStr, NULL);
  8621. DECLARE_VAR(plain, byte, plainLen, NULL);
  8622. DECLARE_VAR(cipher, byte, cipherLen, NULL);
  8623. ret = wc_InitRsaKey(&key, NULL);
  8624. if (ret == 0) {
  8625. ret = wc_InitRng(&rng);
  8626. }
  8627. if (ret == 0) {
  8628. ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
  8629. }
  8630. /* Encrypt. */
  8631. printf(testingFmt, "wc_RsaPublicEncrypt()");
  8632. if (ret == 0) {
  8633. ret = wc_RsaPublicEncrypt(in, inLen, cipher, cipherLen, &key, &rng);
  8634. if (ret >= 0) {
  8635. cipherLen = ret;
  8636. ret = 0;
  8637. } else {
  8638. ret = WOLFSSL_FATAL_ERROR;
  8639. }
  8640. }
  8641. /* Pass bad args. */
  8642. /* Tests PsaPublicEncryptEx() which, is tested by another fn. No need dup.*/
  8643. printf(resultFmt, ret == 0 ? passed : failed);
  8644. if (ret != 0) {
  8645. return ret;
  8646. }
  8647. /* Decrypt */
  8648. printf(testingFmt, "wc_RsaPrivateDecrypt()");
  8649. #if defined(WC_RSA_BLINDING)
  8650. /* Bind rng */
  8651. if (ret == 0) {
  8652. ret = wc_RsaSetRNG(&key, &rng);
  8653. }
  8654. #endif
  8655. if (ret == 0) {
  8656. ret = wc_RsaPrivateDecrypt(cipher, cipherLen, plain, plainLen, &key);
  8657. }
  8658. if (ret >= 0) {
  8659. ret = XMEMCMP(plain, inStr, plainLen);
  8660. }
  8661. /* Pass in bad args. */
  8662. /* Tests RsaPrivateDecryptEx() which, is tested by another fn. No need dup.*/
  8663. FREE_VAR(in, NULL);
  8664. FREE_VAR(plain, NULL);
  8665. FREE_VAR(cipher, NULL);
  8666. if (wc_FreeRsaKey(&key) || ret != 0) {
  8667. ret = WOLFSSL_FATAL_ERROR;
  8668. }
  8669. if (wc_FreeRng(&rng) || ret != 0) {
  8670. ret = WOLFSSL_FATAL_ERROR;
  8671. }
  8672. printf(resultFmt, ret == 0 ? passed : failed);
  8673. #endif
  8674. return ret;
  8675. } /* END test_wc_RsaPublicEncryptDecrypt */
  8676. /*
  8677. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  8678. */
  8679. static int test_wc_RsaPublicEncryptDecrypt_ex (void)
  8680. {
  8681. int ret = 0;
  8682. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  8683. && !defined(WC_NO_RSA_OAEP) && !defined(HAVE_USER_RSA)\
  8684. && !defined(NO_SHA)
  8685. RsaKey key;
  8686. WC_RNG rng;
  8687. const char* inStr = "Everyone gets Friday off.";
  8688. word32 inLen = (word32)XSTRLEN(inStr);
  8689. const word32 cipherSz = 128;
  8690. const word32 plainSz = 25;
  8691. byte* res = NULL;
  8692. int idx = 0;
  8693. DECLARE_VAR_INIT(in, byte, inLen, inStr, NULL);
  8694. DECLARE_VAR(plain, byte, plainSz, NULL);
  8695. DECLARE_VAR(cipher, byte, cipherSz, NULL);
  8696. /* Initialize stack structures. */
  8697. XMEMSET(&rng, 0, sizeof(rng));
  8698. XMEMSET(&key, 0, sizeof(key));
  8699. ret = wc_InitRsaKey_ex(&key, NULL, INVALID_DEVID);
  8700. if (ret == 0) {
  8701. ret = wc_InitRng(&rng);
  8702. }
  8703. if (ret == 0) {
  8704. ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
  8705. }
  8706. /* Encrypt */
  8707. printf(testingFmt, "wc_RsaPublicEncrypt_ex()");
  8708. if (ret == 0) {
  8709. ret = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key, &rng,
  8710. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  8711. if (ret >= 0) {
  8712. idx = ret;
  8713. ret = 0;
  8714. } else {
  8715. ret = WOLFSSL_FATAL_ERROR;
  8716. }
  8717. }
  8718. /*Pass bad args.*/
  8719. /* Tests RsaPublicEncryptEx again. No need duplicate. */
  8720. printf(resultFmt, ret == 0 ? passed : failed);
  8721. if (ret != 0) {
  8722. return ret;
  8723. }
  8724. /* Decrypt */
  8725. printf(testingFmt, "wc_RsaPrivateDecrypt_ex()");
  8726. #if defined(WC_RSA_BLINDING)
  8727. if (ret == 0) {
  8728. ret = wc_RsaSetRNG(&key, &rng);
  8729. }
  8730. #endif
  8731. if (ret == 0) {
  8732. ret = wc_RsaPrivateDecrypt_ex(cipher, (word32)idx,
  8733. plain, plainSz, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA,
  8734. WC_MGF1SHA1, NULL, 0);
  8735. }
  8736. if (ret >= 0) {
  8737. if (!XMEMCMP(plain, inStr, plainSz)) {
  8738. ret = 0;
  8739. } else {
  8740. ret = WOLFSSL_FATAL_ERROR;
  8741. }
  8742. }
  8743. /*Pass bad args.*/
  8744. /* Tests RsaPrivateDecryptEx() again. No need duplicate. */
  8745. printf(resultFmt, ret == 0 ? passed : failed);
  8746. if (ret != 0) {
  8747. return ret;
  8748. }
  8749. printf(testingFmt, "wc_RsaPrivateDecryptInline_ex()");
  8750. if (ret == 0) {
  8751. ret = wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx,
  8752. &res, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA,
  8753. WC_MGF1SHA1, NULL, 0);
  8754. if (ret >= 0) {
  8755. if (!XMEMCMP(inStr, res, plainSz)) {
  8756. ret = 0;
  8757. } else {
  8758. ret = WOLFSSL_FATAL_ERROR;
  8759. }
  8760. }
  8761. }
  8762. FREE_VAR(in, NULL);
  8763. FREE_VAR(plain, NULL);
  8764. FREE_VAR(cipher, NULL);
  8765. if (wc_FreeRsaKey(&key) || ret != 0) {
  8766. ret = WOLFSSL_FATAL_ERROR;
  8767. }
  8768. if (wc_FreeRng(&rng) || ret != 0) {
  8769. ret = WOLFSSL_FATAL_ERROR;
  8770. }
  8771. printf(resultFmt, ret == 0 ? passed : failed);
  8772. #endif
  8773. return ret;
  8774. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  8775. /*
  8776. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  8777. */
  8778. static int test_wc_RsaSSL_SignVerify (void)
  8779. {
  8780. int ret = 0;
  8781. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  8782. RsaKey key;
  8783. WC_RNG rng;
  8784. const char* inStr = "Everyone gets Friday off.";
  8785. const word32 outSz = 128;
  8786. const word32 plainSz = 25;
  8787. word32 inLen = (word32)XSTRLEN(inStr);
  8788. word32 idx = 0;
  8789. DECLARE_VAR_INIT(in, byte, inLen, inStr, NULL);
  8790. DECLARE_VAR(out, byte, outSz, NULL);
  8791. DECLARE_VAR(plain, byte, plainSz, NULL);
  8792. ret = wc_InitRsaKey(&key, NULL);
  8793. if (ret == 0) {
  8794. ret = wc_InitRng(&rng);
  8795. }
  8796. if (ret == 0) {
  8797. ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
  8798. }
  8799. /* Sign. */
  8800. printf(testingFmt, "wc_RsaSSL_Sign()");
  8801. if (ret == 0) {
  8802. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng);
  8803. if (ret == (int)outSz) {
  8804. idx = ret;
  8805. ret = 0;
  8806. } else {
  8807. ret = WOLFSSL_FATAL_ERROR;
  8808. }
  8809. }
  8810. #ifndef HAVE_USER_RSA
  8811. /* Test bad args. */
  8812. if (ret == 0) {
  8813. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  8814. if (ret == BAD_FUNC_ARG) {
  8815. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  8816. }
  8817. if (ret == BAD_FUNC_ARG) {
  8818. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  8819. }
  8820. if (ret == BAD_FUNC_ARG) {
  8821. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  8822. }
  8823. if (ret == BAD_FUNC_ARG) {
  8824. ret = 0;
  8825. } else {
  8826. ret = WOLFSSL_FATAL_ERROR;
  8827. }
  8828. }
  8829. #else
  8830. /* Test bad args. */
  8831. if (ret == 0) {
  8832. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  8833. if (ret == USER_CRYPTO_ERROR) {
  8834. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  8835. }
  8836. if (ret == USER_CRYPTO_ERROR) {
  8837. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  8838. }
  8839. if (ret == USER_CRYPTO_ERROR) {
  8840. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  8841. }
  8842. if (ret == USER_CRYPTO_ERROR) {
  8843. ret = 0;
  8844. } else {
  8845. ret = WOLFSSL_FATAL_ERROR;
  8846. }
  8847. }
  8848. #endif
  8849. printf(resultFmt, ret == 0 ? passed : failed);
  8850. if (ret != 0) {
  8851. return ret;
  8852. }
  8853. /* Verify. */
  8854. printf(testingFmt, "wc_RsaSSL_Verify()");
  8855. if (ret == 0) {
  8856. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, &key);
  8857. if (ret == (int)inLen) {
  8858. ret = 0;
  8859. } else {
  8860. ret = WOLFSSL_FATAL_ERROR;
  8861. }
  8862. }
  8863. #ifndef HAVE_USER_RSA
  8864. /* Pass bad args. */
  8865. if (ret == 0) {
  8866. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  8867. if (ret == BAD_FUNC_ARG) {
  8868. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  8869. }
  8870. if (ret == BAD_FUNC_ARG) {
  8871. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  8872. }
  8873. if (ret == BAD_FUNC_ARG) {
  8874. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  8875. }
  8876. if (ret == BAD_FUNC_ARG) {
  8877. ret = 0;
  8878. } else {
  8879. ret = WOLFSSL_FATAL_ERROR;
  8880. }
  8881. }
  8882. #else
  8883. /* Pass bad args. */
  8884. if (ret == 0) {
  8885. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  8886. if (ret == USER_CRYPTO_ERROR) {
  8887. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  8888. }
  8889. if (ret == USER_CRYPTO_ERROR) {
  8890. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  8891. }
  8892. if (ret == USER_CRYPTO_ERROR) {
  8893. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  8894. }
  8895. if (ret == USER_CRYPTO_ERROR) {
  8896. ret = 0;
  8897. } else {
  8898. ret = WOLFSSL_FATAL_ERROR;
  8899. }
  8900. }
  8901. #endif
  8902. FREE_VAR(in, NULL);
  8903. FREE_VAR(out, NULL);
  8904. FREE_VAR(plain, NULL);
  8905. if (wc_FreeRsaKey(&key) || ret != 0) {
  8906. ret = WOLFSSL_FATAL_ERROR;
  8907. }
  8908. if (wc_FreeRng(&rng) || ret != 0) {
  8909. ret = WOLFSSL_FATAL_ERROR;
  8910. }
  8911. printf(resultFmt, ret == 0 ? passed : failed);
  8912. #endif
  8913. return ret;
  8914. } /* END test_wc_RsaSSL_SignVerify */
  8915. /*
  8916. * Testing wc_RsaEncryptSize()
  8917. */
  8918. static int test_wc_RsaEncryptSize (void)
  8919. {
  8920. int ret = 0;
  8921. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  8922. RsaKey key;
  8923. WC_RNG rng;
  8924. ret = wc_InitRsaKey(&key, NULL);
  8925. if (ret == 0) {
  8926. ret = wc_InitRng(&rng);
  8927. }
  8928. printf(testingFmt, "wc_RsaEncryptSize()");
  8929. if (ret == 0) {
  8930. ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
  8931. if (ret == 0) {
  8932. ret = wc_RsaEncryptSize(&key);
  8933. }
  8934. if (ret == 128) {
  8935. ret = 0;
  8936. } else {
  8937. ret = WOLFSSL_FATAL_ERROR;
  8938. }
  8939. }
  8940. if (wc_FreeRsaKey(&key) || ret != 0) {
  8941. ret = WOLFSSL_FATAL_ERROR;
  8942. } else {
  8943. ret = 0;
  8944. }
  8945. if (ret == 0) {
  8946. ret = MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng);
  8947. if (ret == 0) {
  8948. ret = wc_RsaEncryptSize(&key);
  8949. }
  8950. if (ret == 256) {
  8951. ret = 0;
  8952. } else {
  8953. ret = WOLFSSL_FATAL_ERROR;
  8954. }
  8955. }
  8956. /* Pass in bad arg. */
  8957. if (ret == 0) {
  8958. ret = wc_RsaEncryptSize(NULL);
  8959. #ifndef HAVE_USER_RSA
  8960. if (ret == BAD_FUNC_ARG) {
  8961. ret = 0;
  8962. } else {
  8963. ret = WOLFSSL_FATAL_ERROR;
  8964. }
  8965. #endif
  8966. }
  8967. if (wc_FreeRsaKey(&key) || ret != 0) {
  8968. ret = WOLFSSL_FATAL_ERROR;
  8969. }
  8970. if (wc_FreeRng(&rng) || ret != 0) {
  8971. ret = WOLFSSL_FATAL_ERROR;
  8972. }
  8973. printf(resultFmt, ret == 0 ? passed : failed);
  8974. #endif
  8975. return ret;
  8976. } /* END test_wc_RsaEncryptSize*/
  8977. /*
  8978. * Testing wc_RsaFlattenPublicKey()
  8979. */
  8980. static int test_wc_RsaFlattenPublicKey (void)
  8981. {
  8982. int ret = 0;
  8983. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  8984. RsaKey key;
  8985. WC_RNG rng;
  8986. byte e[256];
  8987. byte n[256];
  8988. word32 eSz = sizeof(e);
  8989. word32 nSz = sizeof(n);
  8990. ret = wc_InitRsaKey(&key, NULL);
  8991. if (ret == 0) {
  8992. ret = wc_InitRng(&rng);
  8993. }
  8994. if (ret == 0) {
  8995. ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
  8996. if (ret >= 0) {
  8997. ret = 0;
  8998. } else {
  8999. ret = WOLFSSL_FATAL_ERROR;
  9000. }
  9001. }
  9002. printf(testingFmt, "wc_RsaFlattenPublicKey()");
  9003. if (ret == 0) {
  9004. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz);
  9005. }
  9006. #ifndef HAVE_USER_RSA
  9007. /* Pass bad args. */
  9008. if (ret == 0) {
  9009. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  9010. if (ret == BAD_FUNC_ARG) {
  9011. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  9012. }
  9013. if (ret == BAD_FUNC_ARG) {
  9014. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  9015. }
  9016. if (ret == BAD_FUNC_ARG) {
  9017. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  9018. }
  9019. if (ret == BAD_FUNC_ARG) {
  9020. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  9021. }
  9022. if (ret == BAD_FUNC_ARG) {
  9023. ret = 0;
  9024. } else {
  9025. ret = WOLFSSL_FATAL_ERROR;
  9026. }
  9027. }
  9028. #else
  9029. /* Pass bad args. */
  9030. if (ret == 0) {
  9031. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  9032. if (ret == USER_CRYPTO_ERROR) {
  9033. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  9034. }
  9035. if (ret == USER_CRYPTO_ERROR) {
  9036. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  9037. }
  9038. if (ret == USER_CRYPTO_ERROR) {
  9039. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  9040. }
  9041. if (ret == USER_CRYPTO_ERROR) {
  9042. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  9043. }
  9044. if (ret == USER_CRYPTO_ERROR) {
  9045. ret = 0;
  9046. } else {
  9047. ret = WOLFSSL_FATAL_ERROR;
  9048. }
  9049. }
  9050. #endif
  9051. if (wc_FreeRsaKey(&key) || ret != 0) {
  9052. ret = WOLFSSL_FATAL_ERROR;
  9053. }
  9054. if (wc_FreeRng(&rng) || ret != 0) {
  9055. ret = WOLFSSL_FATAL_ERROR;
  9056. }
  9057. printf(resultFmt, ret == 0 ? passed : failed);
  9058. #endif
  9059. return ret;
  9060. } /* END test_wc_RsaFlattenPublicKey */
  9061. /*
  9062. * unit test for wc_AesCcmSetKey
  9063. */
  9064. static int test_wc_AesCcmSetKey (void)
  9065. {
  9066. int ret = 0;
  9067. #ifdef HAVE_AESCCM
  9068. Aes aes;
  9069. const byte key16[] =
  9070. {
  9071. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  9072. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  9073. };
  9074. const byte key24[] =
  9075. {
  9076. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  9077. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  9078. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  9079. };
  9080. const byte key32[] =
  9081. {
  9082. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  9083. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  9084. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  9085. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  9086. };
  9087. printf(testingFmt, "wc_AesCcmSetKey()");
  9088. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  9089. if (ret != 0)
  9090. return ret;
  9091. #ifdef WOLFSSL_AES_128
  9092. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  9093. #endif
  9094. #ifdef WOLFSSL_AES_192
  9095. if (ret == 0) {
  9096. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24));
  9097. }
  9098. #endif
  9099. #ifdef WOLFSSL_AES_256
  9100. if (ret == 0) {
  9101. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32));
  9102. }
  9103. #endif
  9104. /* Test bad args. */
  9105. if (ret == 0) {
  9106. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1);
  9107. if (ret == BAD_FUNC_ARG) {
  9108. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1);
  9109. }
  9110. if (ret == BAD_FUNC_ARG) {
  9111. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1);
  9112. }
  9113. if (ret != BAD_FUNC_ARG) {
  9114. ret = WOLFSSL_FATAL_ERROR;
  9115. } else {
  9116. ret = 0;
  9117. }
  9118. }
  9119. wc_AesFree(&aes);
  9120. printf(resultFmt, ret == 0 ? passed : failed);
  9121. #endif
  9122. return ret;
  9123. } /* END test_wc_AesCcmSetKey */
  9124. /*
  9125. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  9126. */
  9127. static int test_wc_AesCcmEncryptDecrypt (void)
  9128. {
  9129. int ret = 0;
  9130. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  9131. Aes aes;
  9132. const byte key16[] =
  9133. {
  9134. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  9135. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  9136. };
  9137. /* plaintext */
  9138. const byte plainT[] =
  9139. {
  9140. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  9141. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  9142. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  9143. };
  9144. /* nonce */
  9145. const byte iv[] =
  9146. {
  9147. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  9148. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  9149. };
  9150. const byte c[] = /* cipher text. */
  9151. {
  9152. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  9153. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  9154. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  9155. };
  9156. const byte t[] = /* Auth tag */
  9157. {
  9158. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  9159. };
  9160. const byte authIn[] =
  9161. {
  9162. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  9163. };
  9164. byte cipherOut[sizeof(plainT)];
  9165. byte authTag[sizeof(t)];
  9166. int ccmE = WOLFSSL_FATAL_ERROR;
  9167. #ifdef HAVE_AES_DECRYPT
  9168. int ccmD = WOLFSSL_FATAL_ERROR;
  9169. byte plainOut[sizeof(cipherOut)];
  9170. #endif
  9171. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  9172. if (ret != 0)
  9173. return ret;
  9174. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  9175. if (ret == 0) {
  9176. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  9177. iv, sizeof(iv), authTag, sizeof(authTag),
  9178. authIn , sizeof(authIn));
  9179. if ((XMEMCMP(cipherOut, c, sizeof(c)) && ccmE == 0) ||
  9180. XMEMCMP(t, authTag, sizeof(t))) {
  9181. ccmE = WOLFSSL_FATAL_ERROR;
  9182. ret = WOLFSSL_FATAL_ERROR;
  9183. }
  9184. #ifdef HAVE_AES_DECRYPT
  9185. if (ret == 0) {
  9186. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  9187. sizeof(plainOut), iv, sizeof(iv),
  9188. authTag, sizeof(authTag),
  9189. authIn, sizeof(authIn));
  9190. }
  9191. if (XMEMCMP(plainOut, plainT, sizeof(plainT)) && ccmD == 0) {
  9192. ccmD = WOLFSSL_FATAL_ERROR;
  9193. }
  9194. #endif
  9195. }
  9196. printf(testingFmt, "wc_AesCcmEncrypt()");
  9197. /* Pass in bad args. Encrypt*/
  9198. if (ret == 0 && ccmE == 0) {
  9199. ccmE = wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  9200. iv, sizeof(iv), authTag, sizeof(authTag),
  9201. authIn , sizeof(authIn));
  9202. if (ccmE == BAD_FUNC_ARG) {
  9203. ccmE = wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  9204. iv, sizeof(iv), authTag, sizeof(authTag),
  9205. authIn , sizeof(authIn));
  9206. }
  9207. if (ccmE == BAD_FUNC_ARG) {
  9208. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  9209. iv, sizeof(iv), authTag, sizeof(authTag),
  9210. authIn , sizeof(authIn));
  9211. }
  9212. if (ccmE == BAD_FUNC_ARG) {
  9213. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  9214. NULL, sizeof(iv), authTag, sizeof(authTag),
  9215. authIn , sizeof(authIn));
  9216. }
  9217. if (ccmE == BAD_FUNC_ARG) {
  9218. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  9219. iv, sizeof(iv), NULL, sizeof(authTag),
  9220. authIn , sizeof(authIn));
  9221. }
  9222. if (ccmE == BAD_FUNC_ARG) {
  9223. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  9224. iv, sizeof(iv) + 1, authTag, sizeof(authTag),
  9225. authIn , sizeof(authIn));
  9226. }
  9227. if (ccmE == BAD_FUNC_ARG) {
  9228. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  9229. iv, sizeof(iv) - 7, authTag, sizeof(authTag),
  9230. authIn , sizeof(authIn));
  9231. }
  9232. if (ccmE != BAD_FUNC_ARG) {
  9233. ccmE = WOLFSSL_FATAL_ERROR;
  9234. } else {
  9235. ccmE = 0;
  9236. }
  9237. } /* End Encrypt */
  9238. printf(resultFmt, ccmE == 0 ? passed : failed);
  9239. if (ccmE != 0) {
  9240. wc_AesFree(&aes);
  9241. return ccmE;
  9242. }
  9243. #ifdef HAVE_AES_DECRYPT
  9244. printf(testingFmt, "wc_AesCcmDecrypt()");
  9245. /* Pass in bad args. Decrypt*/
  9246. if (ret == 0 && ccmD == 0) {
  9247. ccmD = wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  9248. iv, sizeof(iv), authTag, sizeof(authTag),
  9249. authIn, sizeof(authIn));
  9250. if (ccmD == BAD_FUNC_ARG) {
  9251. ccmD = wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  9252. iv, sizeof(iv), authTag, sizeof(authTag),
  9253. authIn, sizeof(authIn));
  9254. }
  9255. if (ccmD == BAD_FUNC_ARG) {
  9256. ccmD = wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  9257. iv, sizeof(iv), authTag, sizeof(authTag),
  9258. authIn, sizeof(authIn));
  9259. }
  9260. if (ccmD == BAD_FUNC_ARG) {
  9261. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  9262. sizeof(plainOut), NULL, sizeof(iv),
  9263. authTag, sizeof(authTag),
  9264. authIn, sizeof(authIn));
  9265. }
  9266. if (ccmD == BAD_FUNC_ARG) {
  9267. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  9268. sizeof(plainOut), iv, sizeof(iv), NULL,
  9269. sizeof(authTag), authIn, sizeof(authIn));
  9270. }
  9271. if (ccmD == BAD_FUNC_ARG) {
  9272. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  9273. sizeof(plainOut), iv, sizeof(iv) + 1,
  9274. authTag, sizeof(authTag),
  9275. authIn, sizeof(authIn));
  9276. }
  9277. if (ccmD == BAD_FUNC_ARG) {
  9278. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  9279. sizeof(plainOut), iv, sizeof(iv) - 7,
  9280. authTag, sizeof(authTag),
  9281. authIn, sizeof(authIn));
  9282. }
  9283. if (ccmD != BAD_FUNC_ARG) {
  9284. ccmD = WOLFSSL_FATAL_ERROR;
  9285. } else {
  9286. ccmD = 0;
  9287. }
  9288. } /* END Decrypt */
  9289. printf(resultFmt, ccmD == 0 ? passed : failed);
  9290. if (ccmD != 0) {
  9291. return ccmD;
  9292. }
  9293. #endif
  9294. wc_AesFree(&aes);
  9295. #endif /* HAVE_AESCCM */
  9296. return ret;
  9297. } /* END test_wc_AesCcmEncryptDecrypt */
  9298. /*
  9299. * Test wc_Hc128_SetKey()
  9300. */
  9301. static int test_wc_Hc128_SetKey (void)
  9302. {
  9303. int ret = 0;
  9304. #ifdef HAVE_HC128
  9305. HC128 ctx;
  9306. const char* key = "\x80\x00\x00\x00\x00\x00\x00\x00"
  9307. "\x00\x00\x00\x00\x00\x00\x00\x00";
  9308. const char* iv = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  9309. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  9310. printf(testingFmt, "wc_Hc128_SetKey()");
  9311. ret = wc_Hc128_SetKey(&ctx, (byte*)key, (byte*)iv);
  9312. /* Test bad args. */
  9313. if (ret == 0) {
  9314. ret = wc_Hc128_SetKey(NULL, (byte*)key, (byte*)iv);
  9315. if (ret == BAD_FUNC_ARG) {
  9316. ret = wc_Hc128_SetKey(&ctx, NULL, (byte*)iv);
  9317. }
  9318. if (ret == BAD_FUNC_ARG) {
  9319. ret = wc_Hc128_SetKey(&ctx, (byte*)key, NULL);
  9320. }
  9321. }
  9322. printf(resultFmt, ret == 0 ? passed : failed);
  9323. #endif
  9324. return ret;
  9325. } /* END test_wc_Hc128_SetKey */
  9326. /*
  9327. * Testing wc_Hc128_Process()
  9328. */
  9329. static int test_wc_Hc128_Process (void)
  9330. {
  9331. int ret = 0;
  9332. #ifdef HAVE_HC128
  9333. HC128 enc;
  9334. HC128 dec;
  9335. const char* key = "\x0F\x62\xB5\x08\x5B\xAE\x01\x54"
  9336. "\xA7\xFA\x4D\xA0\xF3\x46\x99\xEC";
  9337. const char* input = "Encrypt Hc128, and then Decrypt.";
  9338. size_t inlen = XSTRLEN(input) + 1; /* Add null terminator */
  9339. byte cipher[inlen];
  9340. byte plain[inlen];
  9341. printf(testingFmt, "wc_Hc128_Process()");
  9342. ret = wc_Hc128_SetKey(&enc, (byte*)key, NULL);
  9343. if (ret == 0) {
  9344. ret = wc_Hc128_SetKey(&dec, (byte*)key, NULL);
  9345. }
  9346. if (ret == 0) {
  9347. ret = wc_Hc128_Process(&enc, cipher, (byte*)input, (word32)inlen);
  9348. if (ret == 0) {
  9349. ret = wc_Hc128_Process(&dec, plain, cipher, (word32)inlen);
  9350. }
  9351. }
  9352. /* Bad args. */
  9353. if (ret == 0) {
  9354. ret = wc_Hc128_Process(NULL, plain, cipher, (word32)inlen);
  9355. if (ret == BAD_FUNC_ARG) {
  9356. ret = wc_Hc128_Process(&dec, NULL, cipher, (word32)inlen);
  9357. }
  9358. if (ret == BAD_FUNC_ARG) {
  9359. ret = wc_Hc128_Process(&dec, plain, NULL, (word32)inlen);
  9360. }
  9361. if (ret == BAD_FUNC_ARG) {
  9362. ret = 0;
  9363. } else {
  9364. ret = WOLFSSL_FATAL_ERROR;
  9365. }
  9366. }
  9367. printf(resultFmt, ret == 0 ? passed : failed);
  9368. #endif
  9369. return ret;
  9370. } /* END test_wc_Hc128_Process */
  9371. /*
  9372. * Testing wc_InitDsaKey()
  9373. */
  9374. static int test_wc_InitDsaKey (void)
  9375. {
  9376. int ret = 0;
  9377. #ifndef NO_DSA
  9378. DsaKey key;
  9379. printf(testingFmt, "wc_InitDsaKey()");
  9380. ret = wc_InitDsaKey(&key);
  9381. /* Pass in bad args. */
  9382. if (ret == 0) {
  9383. ret = wc_InitDsaKey(NULL);
  9384. if (ret == BAD_FUNC_ARG) {
  9385. ret = 0;
  9386. } else {
  9387. ret = WOLFSSL_FATAL_ERROR;
  9388. }
  9389. }
  9390. printf(resultFmt, ret == 0 ? passed : failed);
  9391. wc_FreeDsaKey(&key);
  9392. #endif
  9393. return ret;
  9394. } /* END test_wc_InitDsaKey */
  9395. /*
  9396. * Testing wc_DsaSign() and wc_DsaVerify()
  9397. */
  9398. static int test_wc_DsaSignVerify (void)
  9399. {
  9400. int ret = 0;
  9401. #if !defined(NO_DSA)
  9402. DsaKey key;
  9403. WC_RNG rng;
  9404. wc_Sha sha;
  9405. byte signature[DSA_SIG_SIZE];
  9406. byte hash[WC_SHA_DIGEST_SIZE];
  9407. word32 idx = 0;
  9408. word32 bytes;
  9409. int answer;
  9410. #ifdef USE_CERT_BUFFERS_1024
  9411. byte tmp[ONEK_BUF];
  9412. XMEMSET(tmp, 0, sizeof(tmp));
  9413. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  9414. bytes = sizeof_dsa_key_der_1024;
  9415. #elif defined(USE_CERT_BUFFERS_2048)
  9416. byte tmp[TWOK_BUF];
  9417. XMEMSET(tmp, 0, sizeof(tmp));
  9418. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  9419. bytes = sizeof_dsa_key_der_2048;
  9420. #else
  9421. byte tmp[TWOK_BUF];
  9422. XMEMSET(tmp, 0, sizeof(tmp));
  9423. FILE* fp = fopen("./certs/dsa2048.der", "rb");
  9424. if (!fp) {
  9425. return WOLFSSL_BAD_FILE;
  9426. }
  9427. bytes = (word32) fread(tmp, 1, sizeof(tmp), fp);
  9428. fclose(fp);
  9429. #endif /* END USE_CERT_BUFFERS_1024 */
  9430. ret = wc_InitSha(&sha);
  9431. if (ret == 0) {
  9432. ret = wc_ShaUpdate(&sha, tmp, bytes);
  9433. if (ret == 0) {
  9434. ret = wc_ShaFinal(&sha, hash);
  9435. }
  9436. if (ret == 0) {
  9437. ret = wc_InitDsaKey(&key);
  9438. }
  9439. if (ret == 0) {
  9440. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  9441. }
  9442. if (ret == 0) {
  9443. ret = wc_InitRng(&rng);
  9444. }
  9445. }
  9446. printf(testingFmt, "wc_DsaSign()");
  9447. /* Sign. */
  9448. if (ret == 0) {
  9449. ret = wc_DsaSign(hash, signature, &key, &rng);
  9450. }
  9451. /* Test bad args. */
  9452. if (ret == 0) {
  9453. ret = wc_DsaSign(NULL, signature, &key, &rng);
  9454. if (ret == BAD_FUNC_ARG) {
  9455. ret = wc_DsaSign(hash, NULL, &key, &rng);
  9456. }
  9457. if (ret == BAD_FUNC_ARG) {
  9458. ret = wc_DsaSign(hash, signature, NULL, &rng);
  9459. }
  9460. if (ret == BAD_FUNC_ARG) {
  9461. ret = wc_DsaSign(hash, signature, &key, NULL);
  9462. }
  9463. if (ret == BAD_FUNC_ARG) {
  9464. ret = 0;
  9465. } else {
  9466. ret = WOLFSSL_FATAL_ERROR;
  9467. }
  9468. }
  9469. printf(resultFmt, ret == 0 ? passed : failed);
  9470. if (ret != 0) {
  9471. return ret;
  9472. }
  9473. /* Verify. */
  9474. printf(testingFmt, "wc_DsaVerify()");
  9475. ret = wc_DsaVerify(hash, signature, &key, &answer);
  9476. if (ret != 0 || answer != 1) {
  9477. ret = WOLFSSL_FATAL_ERROR;
  9478. } else {
  9479. ret = 0;
  9480. }
  9481. /* Pass in bad args. */
  9482. if (ret == 0) {
  9483. ret = wc_DsaVerify(NULL, signature, &key, &answer);
  9484. if (ret == BAD_FUNC_ARG) {
  9485. ret = wc_DsaVerify(hash, NULL, &key, &answer);
  9486. }
  9487. if (ret == BAD_FUNC_ARG) {
  9488. ret = wc_DsaVerify(hash, signature, NULL, &answer);
  9489. }
  9490. if (ret == BAD_FUNC_ARG) {
  9491. ret = wc_DsaVerify(hash, signature, &key, NULL);
  9492. }
  9493. if (ret == BAD_FUNC_ARG) {
  9494. ret = 0;
  9495. } else {
  9496. ret = WOLFSSL_FATAL_ERROR;
  9497. }
  9498. }
  9499. if (wc_FreeRng(&rng) && ret == 0) {
  9500. ret = WOLFSSL_FATAL_ERROR;
  9501. }
  9502. printf(resultFmt, ret == 0 ? passed : failed);
  9503. wc_FreeDsaKey(&key);
  9504. wc_ShaFree(&sha);
  9505. #endif
  9506. return ret;
  9507. } /* END test_wc_DsaSign */
  9508. /*
  9509. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  9510. */
  9511. static int test_wc_DsaPublicPrivateKeyDecode (void)
  9512. {
  9513. int ret = 0;
  9514. #if !defined(NO_DSA)
  9515. DsaKey key;
  9516. word32 bytes;
  9517. word32 idx = 0;
  9518. int priv = WOLFSSL_FATAL_ERROR;
  9519. int pub = WOLFSSL_FATAL_ERROR;
  9520. #ifdef USE_CERT_BUFFERS_1024
  9521. byte tmp[ONEK_BUF];
  9522. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  9523. bytes = sizeof_dsa_key_der_1024;
  9524. #elif defined(USE_CERT_BUFFERS_2048)
  9525. byte tmp[TWOK_BUF];
  9526. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  9527. bytes = sizeof_dsa_key_der_2048;
  9528. #else
  9529. byte tmp[TWOK_BUF];
  9530. XMEMSET(tmp, 0, sizeof(tmp));
  9531. FILE* fp = fopen("./certs/dsa2048.der", "rb");
  9532. if (!fp) {
  9533. return WOLFSSL_BAD_FILE;
  9534. }
  9535. bytes = (word32) fread(tmp, 1, sizeof(tmp), fp);
  9536. fclose(fp);
  9537. #endif /* END USE_CERT_BUFFERS_1024 */
  9538. ret = wc_InitDsaKey(&key);
  9539. printf(testingFmt, "wc_DsaPrivateKeyDecode()");
  9540. if (ret == 0) {
  9541. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  9542. /* Test bad args. */
  9543. if (priv == 0) {
  9544. priv = wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes);
  9545. if (priv == BAD_FUNC_ARG) {
  9546. priv = wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes);
  9547. }
  9548. if (priv == BAD_FUNC_ARG) {
  9549. priv = wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes);
  9550. }
  9551. if (priv == BAD_FUNC_ARG) {
  9552. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  9553. }
  9554. if (priv == ASN_PARSE_E) {
  9555. priv = 0;
  9556. } else {
  9557. priv = WOLFSSL_FATAL_ERROR;
  9558. }
  9559. }
  9560. } /* END Private Key */
  9561. if (ret == 0) {
  9562. wc_FreeDsaKey(&key);
  9563. ret = wc_InitDsaKey(&key);
  9564. }
  9565. printf(resultFmt, priv == 0 ? passed : failed);
  9566. printf(testingFmt, "wc_DsaPublicKeyDecode()");
  9567. if (ret == 0) {
  9568. idx = 0; /* Reset */
  9569. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  9570. /* Test bad args. */
  9571. if (pub == 0) {
  9572. pub = wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes);
  9573. if (pub == BAD_FUNC_ARG) {
  9574. pub = wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes);
  9575. }
  9576. if (pub == BAD_FUNC_ARG) {
  9577. pub = wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes);
  9578. }
  9579. if (pub == BAD_FUNC_ARG) {
  9580. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  9581. }
  9582. if (pub == ASN_PARSE_E) {
  9583. pub = 0;
  9584. } else {
  9585. pub = WOLFSSL_FATAL_ERROR;
  9586. }
  9587. }
  9588. } /* END Public Key */
  9589. printf(resultFmt, pub == 0 ? passed : failed);
  9590. wc_FreeDsaKey(&key);
  9591. #endif
  9592. return ret;
  9593. } /* END test_wc_DsaPublicPrivateKeyDecode */
  9594. /*
  9595. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  9596. */
  9597. static int test_wc_MakeDsaKey (void)
  9598. {
  9599. int ret = 0;
  9600. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  9601. DsaKey genKey;
  9602. WC_RNG rng;
  9603. ret = wc_InitRng(&rng);
  9604. if (ret == 0) {
  9605. ret = wc_InitDsaKey(&genKey);
  9606. }
  9607. printf(testingFmt, "wc_MakeDsaParameters()");
  9608. if (ret == 0) {
  9609. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  9610. }
  9611. /* Test bad args. */
  9612. if (ret == 0) {
  9613. ret = wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey);
  9614. if (ret == BAD_FUNC_ARG) {
  9615. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, NULL);
  9616. }
  9617. if (ret == BAD_FUNC_ARG) {
  9618. ret = wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey);
  9619. }
  9620. if (ret == BAD_FUNC_ARG) {
  9621. ret = 0;
  9622. } else {
  9623. ret = WOLFSSL_FATAL_ERROR;
  9624. }
  9625. }
  9626. printf(resultFmt, ret == 0 ? passed : failed);
  9627. printf(testingFmt, "wc_MakeDsaKey()");
  9628. if (ret == 0) {
  9629. ret = wc_MakeDsaKey(&rng, &genKey);
  9630. }
  9631. /* Test bad args. */
  9632. if (ret == 0) {
  9633. ret = wc_MakeDsaKey(NULL, &genKey);
  9634. if (ret == BAD_FUNC_ARG) {
  9635. ret = wc_MakeDsaKey(&rng, NULL);
  9636. }
  9637. if (ret == BAD_FUNC_ARG) {
  9638. ret = 0;
  9639. } else {
  9640. ret = WOLFSSL_FATAL_ERROR;
  9641. }
  9642. }
  9643. if (wc_FreeRng(&rng) && ret == 0) {
  9644. ret = WOLFSSL_FAILURE;
  9645. }
  9646. printf(resultFmt, ret == 0 ? passed : failed);
  9647. wc_FreeDsaKey(&genKey);
  9648. #endif
  9649. return ret;
  9650. } /* END test_wc_MakeDsaKey */
  9651. /*
  9652. * Testing wc_DsaKeyToDer()
  9653. */
  9654. static int test_wc_DsaKeyToDer (void)
  9655. {
  9656. int ret = 0;
  9657. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  9658. DsaKey genKey;
  9659. WC_RNG rng;
  9660. word32 bytes;
  9661. word32 idx = 0;
  9662. #ifdef USE_CERT_BUFFERS_1024
  9663. byte tmp[ONEK_BUF];
  9664. byte der[ONEK_BUF];
  9665. XMEMSET(tmp, 0, sizeof(tmp));
  9666. XMEMSET(der, 0, sizeof(der));
  9667. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  9668. bytes = sizeof_dsa_key_der_1024;
  9669. #elif defined(USE_CERT_BUFFERS_2048)
  9670. byte tmp[TWOK_BUF];
  9671. byte der[TWOK_BUF];
  9672. XMEMSET(tmp, 0, sizeof(tmp));
  9673. XMEMSET(der, 0, sizeof(der));
  9674. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  9675. bytes = sizeof_dsa_key_der_2048;
  9676. #else
  9677. byte tmp[TWOK_BUF];
  9678. byte der[TWOK_BUF];
  9679. XMEMSET(tmp, 0, sizeof(tmp));
  9680. XMEMSET(der, 0, sizeof(der));
  9681. FILE* fp = fopen("./certs/dsa2048.der", "rb");
  9682. if (!fp) {
  9683. return WOLFSSL_BAD_FILE;
  9684. }
  9685. bytes = (word32) fread(tmp, 1, sizeof(tmp), fp);
  9686. fclose(fp);
  9687. #endif /* END USE_CERT_BUFFERS_1024 */
  9688. ret = wc_InitRng(&rng);
  9689. if (ret == 0) {
  9690. ret = wc_InitDsaKey(&genKey);
  9691. }
  9692. if (ret == 0) {
  9693. ret = wc_MakeDsaParameters(&rng, sizeof(tmp), &genKey);
  9694. if (ret == 0) {
  9695. wc_FreeDsaKey(&genKey);
  9696. ret = wc_InitDsaKey(&genKey);
  9697. }
  9698. }
  9699. if (ret == 0) {
  9700. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &genKey, bytes);
  9701. }
  9702. printf(testingFmt, "wc_DsaKeyToDer()");
  9703. if (ret == 0) {
  9704. ret = wc_DsaKeyToDer(&genKey, der, bytes);
  9705. if ( ret >= 0 && ( ret = XMEMCMP(der, tmp, bytes) ) == 0 ) {
  9706. ret = 0;
  9707. }
  9708. }
  9709. /* Test bad args. */
  9710. if (ret == 0) {
  9711. ret = wc_DsaKeyToDer(NULL, der, FOURK_BUF);
  9712. if (ret == BAD_FUNC_ARG) {
  9713. ret = wc_DsaKeyToDer(&genKey, NULL, FOURK_BUF);
  9714. }
  9715. if (ret == BAD_FUNC_ARG) {
  9716. ret = 0;
  9717. } else {
  9718. ret = WOLFSSL_FATAL_ERROR;
  9719. }
  9720. }
  9721. if (wc_FreeRng(&rng) && ret == 0) {
  9722. ret = WOLFSSL_FATAL_ERROR;
  9723. }
  9724. printf(resultFmt, ret == 0 ? passed : failed);
  9725. wc_FreeDsaKey(&genKey);
  9726. #endif
  9727. return ret;
  9728. } /* END test_wc_DsaKeyToDer */
  9729. /*
  9730. * Testing wc_DsaImportParamsRaw()
  9731. */
  9732. static int test_wc_DsaImportParamsRaw (void)
  9733. {
  9734. int ret = 0;
  9735. #if !defined(NO_DSA)
  9736. DsaKey key;
  9737. /* [mod = L=1024, N=160], from CAVP KeyPair */
  9738. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  9739. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  9740. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  9741. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  9742. "47123188f8dc551054ee162b634d60f097f719076640e209"
  9743. "80a0093113a8bd73";
  9744. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  9745. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  9746. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  9747. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  9748. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  9749. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  9750. "76341a7e7d9";
  9751. /* invalid p and q parameters */
  9752. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  9753. const char* invalidQ = "96c5390a";
  9754. printf(testingFmt, "wc_DsaImportParamsRaw()");
  9755. ret = wc_InitDsaKey(&key);
  9756. if (ret == 0) {
  9757. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  9758. }
  9759. /* test bad args */
  9760. if (ret == 0) {
  9761. /* null key struct */
  9762. ret = wc_DsaImportParamsRaw(NULL, p, q, g);
  9763. if (ret == BAD_FUNC_ARG) {
  9764. /* null param pointers */
  9765. ret = wc_DsaImportParamsRaw(&key, NULL, NULL, NULL);
  9766. }
  9767. if (ret == BAD_FUNC_ARG) {
  9768. /* illegal p length */
  9769. ret = wc_DsaImportParamsRaw(&key, invalidP, q, g);
  9770. }
  9771. if (ret == BAD_FUNC_ARG) {
  9772. /* illegal q length */
  9773. ret = wc_DsaImportParamsRaw(&key, p, invalidQ, g);
  9774. if (ret == BAD_FUNC_ARG)
  9775. ret = 0;
  9776. }
  9777. }
  9778. printf(resultFmt, ret == 0 ? passed : failed);
  9779. wc_FreeDsaKey(&key);
  9780. #endif
  9781. return ret;
  9782. } /* END test_wc_DsaImportParamsRaw */
  9783. /*
  9784. * Testing wc_DsaExportParamsRaw()
  9785. */
  9786. static int test_wc_DsaExportParamsRaw (void)
  9787. {
  9788. int ret = 0;
  9789. #if !defined(NO_DSA)
  9790. DsaKey key;
  9791. /* [mod = L=1024, N=160], from CAVP KeyPair */
  9792. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  9793. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  9794. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  9795. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  9796. "47123188f8dc551054ee162b634d60f097f719076640e209"
  9797. "80a0093113a8bd73";
  9798. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  9799. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  9800. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  9801. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  9802. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  9803. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  9804. "76341a7e7d9";
  9805. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  9806. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  9807. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  9808. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  9809. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  9810. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  9811. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  9812. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  9813. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  9814. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  9815. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  9816. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  9817. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  9818. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  9819. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  9820. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  9821. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  9822. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  9823. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  9824. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  9825. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  9826. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  9827. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  9828. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  9829. byte pOut[MAX_DSA_PARAM_SIZE];
  9830. byte qOut[MAX_DSA_PARAM_SIZE];
  9831. byte gOut[MAX_DSA_PARAM_SIZE];
  9832. word32 pOutSz, qOutSz, gOutSz;
  9833. printf(testingFmt, "wc_DsaExportParamsRaw()");
  9834. ret = wc_InitDsaKey(&key);
  9835. if (ret == 0) {
  9836. /* first test using imported raw parameters, for expected */
  9837. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  9838. }
  9839. if (ret == 0) {
  9840. pOutSz = sizeof(pOut);
  9841. qOutSz = sizeof(qOut);
  9842. gOutSz = sizeof(gOut);
  9843. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  9844. gOut, &gOutSz);
  9845. }
  9846. if (ret == 0) {
  9847. /* validate exported parameters are correct */
  9848. if ((XMEMCMP(pOut, pCompare, pOutSz) != 0) ||
  9849. (XMEMCMP(qOut, qCompare, qOutSz) != 0) ||
  9850. (XMEMCMP(gOut, gCompare, gOutSz) != 0) ) {
  9851. ret = -1;
  9852. }
  9853. }
  9854. /* test bad args */
  9855. if (ret == 0) {
  9856. /* null key struct */
  9857. ret = wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz,
  9858. gOut, &gOutSz);
  9859. if (ret == BAD_FUNC_ARG) {
  9860. /* null output pointers */
  9861. ret = wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz,
  9862. NULL, &gOutSz);
  9863. }
  9864. if (ret == LENGTH_ONLY_E) {
  9865. /* null output size pointers */
  9866. ret = wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL,
  9867. gOut, NULL);
  9868. }
  9869. if (ret == BAD_FUNC_ARG) {
  9870. /* p output buffer size too small */
  9871. pOutSz = 1;
  9872. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  9873. gOut, &gOutSz);
  9874. pOutSz = sizeof(pOut);
  9875. }
  9876. if (ret == BUFFER_E) {
  9877. /* q output buffer size too small */
  9878. qOutSz = 1;
  9879. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  9880. gOut, &gOutSz);
  9881. qOutSz = sizeof(qOut);
  9882. }
  9883. if (ret == BUFFER_E) {
  9884. /* g output buffer size too small */
  9885. gOutSz = 1;
  9886. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  9887. gOut, &gOutSz);
  9888. if (ret == BUFFER_E)
  9889. ret = 0;
  9890. }
  9891. }
  9892. printf(resultFmt, ret == 0 ? passed : failed);
  9893. wc_FreeDsaKey(&key);
  9894. #endif
  9895. return ret;
  9896. } /* END test_wc_DsaExportParamsRaw */
  9897. /*
  9898. * Testing wc_DsaExportKeyRaw()
  9899. */
  9900. static int test_wc_DsaExportKeyRaw (void)
  9901. {
  9902. int ret = 0;
  9903. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  9904. DsaKey key;
  9905. WC_RNG rng;
  9906. byte xOut[MAX_DSA_PARAM_SIZE];
  9907. byte yOut[MAX_DSA_PARAM_SIZE];
  9908. word32 xOutSz, yOutSz;
  9909. printf(testingFmt, "wc_DsaExportKeyRaw()");
  9910. ret = wc_InitRng(&rng);
  9911. if (ret == 0) {
  9912. ret = wc_InitDsaKey(&key);
  9913. }
  9914. if (ret == 0) {
  9915. ret = wc_MakeDsaParameters(&rng, 1024, &key);
  9916. if (ret == 0) {
  9917. ret = wc_MakeDsaKey(&rng, &key);
  9918. }
  9919. }
  9920. /* try successful export */
  9921. if (ret == 0) {
  9922. xOutSz = sizeof(xOut);
  9923. yOutSz = sizeof(yOut);
  9924. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  9925. }
  9926. /* test bad args */
  9927. if (ret == 0) {
  9928. /* null key struct */
  9929. ret = wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz);
  9930. if (ret == BAD_FUNC_ARG) {
  9931. /* null output pointers */
  9932. ret = wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz);
  9933. }
  9934. if (ret == LENGTH_ONLY_E) {
  9935. /* null output size pointers */
  9936. ret = wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL);
  9937. }
  9938. if (ret == BAD_FUNC_ARG) {
  9939. /* x output buffer size too small */
  9940. xOutSz = 1;
  9941. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  9942. xOutSz = sizeof(xOut);
  9943. }
  9944. if (ret == BUFFER_E) {
  9945. /* y output buffer size too small */
  9946. yOutSz = 1;
  9947. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  9948. if (ret == BUFFER_E)
  9949. ret = 0;
  9950. }
  9951. }
  9952. printf(resultFmt, ret == 0 ? passed : failed);
  9953. wc_FreeDsaKey(&key);
  9954. wc_FreeRng(&rng);
  9955. #endif
  9956. return ret;
  9957. } /* END test_wc_DsaExportParamsRaw */
  9958. /*
  9959. * Testing wc_ed25519_make_key().
  9960. */
  9961. static int test_wc_ed25519_make_key (void)
  9962. {
  9963. int ret = 0;
  9964. #if defined(HAVE_ED25519)
  9965. ed25519_key key;
  9966. WC_RNG rng;
  9967. ret = wc_InitRng(&rng);
  9968. if (ret == 0) {
  9969. ret = wc_ed25519_init(&key);
  9970. }
  9971. printf(testingFmt, "wc_ed25519_make_key()");
  9972. if (ret == 0) {
  9973. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  9974. }
  9975. /* Test bad args. */
  9976. if (ret == 0) {
  9977. ret = wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key);
  9978. if (ret == BAD_FUNC_ARG) {
  9979. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL);
  9980. }
  9981. if (ret == BAD_FUNC_ARG) {
  9982. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key);
  9983. }
  9984. if (ret == BAD_FUNC_ARG) {
  9985. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key);
  9986. }
  9987. if (ret == BAD_FUNC_ARG) {
  9988. ret = 0;
  9989. } else if (ret == 0) {
  9990. ret = SSL_FATAL_ERROR;
  9991. }
  9992. }
  9993. printf(resultFmt, ret == 0 ? passed : failed);
  9994. if (wc_FreeRng(&rng) && ret == 0) {
  9995. ret = SSL_FATAL_ERROR;
  9996. }
  9997. wc_ed25519_free(&key);
  9998. #endif
  9999. return ret;
  10000. } /* END test_wc_ed25519_make_key */
  10001. /*
  10002. * Testing wc_ed25519_init()
  10003. */
  10004. static int test_wc_ed25519_init (void)
  10005. {
  10006. int ret = 0;
  10007. #if defined(HAVE_ED25519)
  10008. ed25519_key key;
  10009. printf(testingFmt, "wc_ed25519_init()");
  10010. ret = wc_ed25519_init(&key);
  10011. /* Test bad args. */
  10012. if (ret == 0) {
  10013. ret = wc_ed25519_init(NULL);
  10014. if (ret == BAD_FUNC_ARG) {
  10015. ret = 0;
  10016. } else if (ret == 0) {
  10017. ret = SSL_FATAL_ERROR;
  10018. }
  10019. }
  10020. printf(resultFmt, ret == 0 ? passed : failed);
  10021. wc_ed25519_free(&key);
  10022. #endif
  10023. return ret;
  10024. } /* END test_wc_ed25519_init */
  10025. /*
  10026. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  10027. */
  10028. static int test_wc_ed25519_sign_msg (void)
  10029. {
  10030. int ret = 0;
  10031. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  10032. WC_RNG rng;
  10033. ed25519_key key;
  10034. byte msg[] = "Everybody gets Friday off.\n";
  10035. byte sig[ED25519_SIG_SIZE];
  10036. word32 msglen = sizeof(msg);
  10037. word32 siglen = sizeof(sig);
  10038. word32 badSigLen = sizeof(sig) - 1;
  10039. int stat = 0; /*1 = Verify success.*/
  10040. /* Initialize stack variables. */
  10041. XMEMSET(sig, 0, siglen);
  10042. /* Initialize key. */
  10043. ret = wc_InitRng(&rng);
  10044. if (ret == 0) {
  10045. ret = wc_ed25519_init(&key);
  10046. if (ret == 0) {
  10047. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  10048. }
  10049. }
  10050. printf(testingFmt, "wc_ed25519_sign_msg()");
  10051. if (ret == 0) {
  10052. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key);
  10053. }
  10054. /* Test bad args. */
  10055. if (ret == 0 && siglen == ED25519_SIG_SIZE) {
  10056. ret = wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key);
  10057. if (ret == BAD_FUNC_ARG) {
  10058. ret = wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key);
  10059. }
  10060. if (ret == BAD_FUNC_ARG) {
  10061. ret = wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key);
  10062. }
  10063. if (ret == BAD_FUNC_ARG) {
  10064. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL);
  10065. }
  10066. if (ret == BAD_FUNC_ARG) {
  10067. ret = wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key);
  10068. }
  10069. if (ret == BUFFER_E && badSigLen == ED25519_SIG_SIZE) {
  10070. badSigLen -= 1;
  10071. ret = 0;
  10072. } else if (ret == 0) {
  10073. ret = SSL_FATAL_ERROR;
  10074. }
  10075. } /* END sign */
  10076. printf(resultFmt, ret == 0 ? passed : failed);
  10077. #ifdef HAVE_ED25519_VERIFY
  10078. printf(testingFmt, "wc_ed25519_verify_msg()");
  10079. if (ret == 0) {
  10080. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen, &stat, &key);
  10081. if (ret == 0 && stat == 1) {
  10082. ret = 0;
  10083. } else if (ret == 0) {
  10084. ret = SSL_FATAL_ERROR;
  10085. }
  10086. /* Test bad args. */
  10087. if (ret == 0) {
  10088. ret = wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &stat,
  10089. &key);
  10090. if (ret == BAD_FUNC_ARG) {
  10091. ret = wc_ed25519_verify_msg(sig, siglen, NULL, msglen,
  10092. &stat, &key);
  10093. }
  10094. if (ret == BAD_FUNC_ARG) {
  10095. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  10096. NULL, &key);
  10097. }
  10098. if (ret == BAD_FUNC_ARG) {
  10099. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  10100. &stat, NULL);
  10101. }
  10102. if (ret == BAD_FUNC_ARG) {
  10103. ret = wc_ed25519_verify_msg(sig, badSigLen, msg, msglen,
  10104. &stat, &key);
  10105. }
  10106. if (ret == BAD_FUNC_ARG) {
  10107. ret = 0;
  10108. } else if (ret == 0) {
  10109. ret = SSL_FATAL_ERROR;
  10110. }
  10111. }
  10112. } /* END verify. */
  10113. printf(resultFmt, ret == 0 ? passed : failed);
  10114. #endif /* Verify. */
  10115. if (wc_FreeRng(&rng) && ret == 0) {
  10116. ret = SSL_FATAL_ERROR;
  10117. }
  10118. wc_ed25519_free(&key);
  10119. #endif
  10120. return ret;
  10121. } /* END test_wc_ed25519_sign_msg */
  10122. /*
  10123. * Testing wc_ed25519_import_public()
  10124. */
  10125. static int test_wc_ed25519_import_public (void)
  10126. {
  10127. int ret = 0;
  10128. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  10129. WC_RNG rng;
  10130. ed25519_key pubKey;
  10131. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  10132. word32 inlen = sizeof(in);
  10133. ret = wc_InitRng(&rng);
  10134. if (ret == 0) {
  10135. ret = wc_ed25519_init(&pubKey);
  10136. if (ret == 0) {
  10137. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey);
  10138. }
  10139. }
  10140. printf(testingFmt, "wc_ed25519_import_public()");
  10141. if (ret == 0) {
  10142. ret = wc_ed25519_import_public(in, inlen, &pubKey);
  10143. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  10144. ret = 0;
  10145. } else {
  10146. ret = SSL_FATAL_ERROR;
  10147. }
  10148. /* Test bad args. */
  10149. if (ret == 0) {
  10150. ret = wc_ed25519_import_public(NULL, inlen, &pubKey);
  10151. if (ret == BAD_FUNC_ARG) {
  10152. ret = wc_ed25519_import_public(in, inlen, NULL);
  10153. }
  10154. if (ret == BAD_FUNC_ARG) {
  10155. ret = wc_ed25519_import_public(in, inlen - 1, &pubKey);
  10156. }
  10157. if (ret == BAD_FUNC_ARG) {
  10158. ret = 0;
  10159. } else if (ret == 0) {
  10160. ret = SSL_FATAL_ERROR;
  10161. }
  10162. }
  10163. }
  10164. printf(resultFmt, ret == 0 ? passed : failed);
  10165. if (wc_FreeRng(&rng) && ret == 0) {
  10166. ret = SSL_FATAL_ERROR;
  10167. }
  10168. wc_ed25519_free(&pubKey);
  10169. #endif
  10170. return ret;
  10171. } /* END wc_ed25519_import_public */
  10172. /*
  10173. * Testing wc_ed25519_import_private_key()
  10174. */
  10175. static int test_wc_ed25519_import_private_key (void)
  10176. {
  10177. int ret = 0;
  10178. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  10179. WC_RNG rng;
  10180. ed25519_key key;
  10181. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  10182. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  10183. word32 privKeySz = sizeof(privKey);
  10184. word32 pubKeySz = sizeof(pubKey);
  10185. ret = wc_InitRng(&rng);
  10186. if (ret != 0) {
  10187. return ret;
  10188. }
  10189. ret = wc_ed25519_init(&key);
  10190. if (ret != 0) {
  10191. wc_FreeRng(&rng);
  10192. return ret;
  10193. }
  10194. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  10195. printf(testingFmt, "wc_ed25519_import_private_key()");
  10196. if (ret == 0) {
  10197. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  10198. pubKeySz, &key);
  10199. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  10200. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  10201. ret = SSL_FATAL_ERROR;
  10202. }
  10203. }
  10204. /* Test bad args. */
  10205. if (ret == 0) {
  10206. ret = wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  10207. &key);
  10208. if (ret == BAD_FUNC_ARG) {
  10209. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  10210. pubKeySz, &key);
  10211. }
  10212. if (ret == BAD_FUNC_ARG) {
  10213. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  10214. pubKeySz, NULL);
  10215. }
  10216. if (ret == BAD_FUNC_ARG) {
  10217. ret = wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  10218. pubKeySz, &key);
  10219. }
  10220. if (ret == BAD_FUNC_ARG) {
  10221. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  10222. pubKeySz - 1, &key);
  10223. }
  10224. if (ret == BAD_FUNC_ARG) {
  10225. ret = 0;
  10226. } else if (ret == 0) {
  10227. ret = SSL_FATAL_ERROR;
  10228. }
  10229. }
  10230. printf(resultFmt, ret == 0 ? passed : failed);
  10231. if (wc_FreeRng(&rng) && ret == 0) {
  10232. ret = SSL_FATAL_ERROR;
  10233. }
  10234. wc_ed25519_free(&key);
  10235. #endif
  10236. return ret;
  10237. } /* END test_wc_ed25519_import_private_key */
  10238. /*
  10239. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  10240. */
  10241. static int test_wc_ed25519_export (void)
  10242. {
  10243. int ret = 0;
  10244. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  10245. WC_RNG rng;
  10246. ed25519_key key;
  10247. byte priv[ED25519_PRV_KEY_SIZE];
  10248. byte pub[ED25519_PUB_KEY_SIZE];
  10249. word32 privSz = sizeof(priv);
  10250. word32 pubSz = sizeof(pub);
  10251. ret = wc_InitRng(&rng);
  10252. if (ret != 0) {
  10253. return ret;
  10254. }
  10255. ret = wc_ed25519_init(&key);
  10256. if (ret != 0) {
  10257. wc_FreeRng(&rng);
  10258. return ret;
  10259. }
  10260. if (ret == 0) {
  10261. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  10262. }
  10263. printf(testingFmt, "wc_ed25519_export_public()");
  10264. if (ret == 0) {
  10265. ret = wc_ed25519_export_public(&key, pub, &pubSz);
  10266. if (ret == 0 && (pubSz != ED25519_KEY_SIZE
  10267. || XMEMCMP(key.p, pub, pubSz) != 0)) {
  10268. ret = SSL_FATAL_ERROR;
  10269. }
  10270. if (ret == 0) {
  10271. ret = wc_ed25519_export_public(NULL, pub, &pubSz);
  10272. if (ret == BAD_FUNC_ARG) {
  10273. ret = wc_ed25519_export_public(&key, NULL, &pubSz);
  10274. }
  10275. if (ret == BAD_FUNC_ARG) {
  10276. ret = wc_ed25519_export_public(&key, pub, NULL);
  10277. }
  10278. if (ret == BAD_FUNC_ARG) {
  10279. ret = 0;
  10280. } else if (ret == 0) {
  10281. ret = SSL_FATAL_ERROR;
  10282. }
  10283. }
  10284. }
  10285. printf(resultFmt, ret == 0 ? passed : failed);
  10286. printf(testingFmt, "wc_ed25519_export_private_only()");
  10287. if (ret == 0) {
  10288. ret = wc_ed25519_export_private_only(&key, priv, &privSz);
  10289. if (ret == 0 && (privSz != ED25519_KEY_SIZE
  10290. || XMEMCMP(key.k, priv, privSz) != 0)) {
  10291. ret = SSL_FATAL_ERROR;
  10292. }
  10293. if (ret == 0) {
  10294. ret = wc_ed25519_export_private_only(NULL, priv, &privSz);
  10295. if (ret == BAD_FUNC_ARG) {
  10296. ret = wc_ed25519_export_private_only(&key, NULL, &privSz);
  10297. }
  10298. if (ret == BAD_FUNC_ARG) {
  10299. ret = wc_ed25519_export_private_only(&key, priv, NULL);
  10300. }
  10301. if (ret == BAD_FUNC_ARG) {
  10302. ret = 0;
  10303. } else if (ret == 0) {
  10304. ret = SSL_FATAL_ERROR;
  10305. }
  10306. }
  10307. }
  10308. printf(resultFmt, ret == 0 ? passed : failed);
  10309. if (wc_FreeRng(&rng) && ret == 0) {
  10310. ret = SSL_FATAL_ERROR;
  10311. }
  10312. wc_ed25519_free(&key);
  10313. #endif
  10314. return ret;
  10315. } /* END test_wc_ed25519_export */
  10316. /*
  10317. * Testing wc_ed25519_size()
  10318. */
  10319. static int test_wc_ed25519_size (void)
  10320. {
  10321. int ret = 0;
  10322. #if defined(HAVE_ED25519)
  10323. WC_RNG rng;
  10324. ed25519_key key;
  10325. ret = wc_InitRng(&rng);
  10326. if (ret != 0) {
  10327. return ret;
  10328. }
  10329. ret = wc_ed25519_init(&key);
  10330. if (ret != 0) {
  10331. wc_FreeRng(&rng);
  10332. return ret;
  10333. }
  10334. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  10335. if (ret != 0) {
  10336. wc_FreeRng(&rng);
  10337. wc_ed25519_free(&key);
  10338. return ret;
  10339. }
  10340. printf(testingFmt, "wc_ed25519_size()");
  10341. ret = wc_ed25519_size(&key);
  10342. /* Test bad args. */
  10343. if (ret == ED25519_KEY_SIZE) {
  10344. ret = wc_ed25519_size(NULL);
  10345. if (ret == BAD_FUNC_ARG) {
  10346. ret = 0;
  10347. }
  10348. }
  10349. printf(resultFmt, ret == 0 ? passed : failed);
  10350. if (ret == 0) {
  10351. printf(testingFmt, "wc_ed25519_sig_size()");
  10352. ret = wc_ed25519_sig_size(&key);
  10353. if (ret == ED25519_SIG_SIZE) {
  10354. ret = 0;
  10355. }
  10356. /* Test bad args. */
  10357. if (ret == 0) {
  10358. ret = wc_ed25519_sig_size(NULL);
  10359. if (ret == BAD_FUNC_ARG) {
  10360. ret = 0;
  10361. }
  10362. }
  10363. printf(resultFmt, ret == 0 ? passed : failed);
  10364. } /* END wc_ed25519_sig_size() */
  10365. if (ret == 0) {
  10366. printf(testingFmt, "wc_ed25519_pub_size");
  10367. ret = wc_ed25519_pub_size(&key);
  10368. if (ret == ED25519_PUB_KEY_SIZE) {
  10369. ret = 0;
  10370. }
  10371. if (ret == 0) {
  10372. ret = wc_ed25519_pub_size(NULL);
  10373. if (ret == BAD_FUNC_ARG) {
  10374. ret = 0;
  10375. }
  10376. }
  10377. printf(resultFmt, ret == 0 ? passed : failed);
  10378. } /* END wc_ed25519_pub_size */
  10379. if (ret == 0) {
  10380. printf(testingFmt, "wc_ed25519_priv_size");
  10381. ret = wc_ed25519_priv_size(&key);
  10382. if (ret == ED25519_PRV_KEY_SIZE) {
  10383. ret = 0;
  10384. }
  10385. if (ret == 0) {
  10386. ret = wc_ed25519_priv_size(NULL);
  10387. if (ret == BAD_FUNC_ARG) {
  10388. ret = 0;
  10389. }
  10390. }
  10391. printf(resultFmt, ret == 0 ? passed : failed);
  10392. } /* END wc_ed25519_pub_size */
  10393. if (wc_FreeRng(&rng) && ret == 0) {
  10394. ret = SSL_FATAL_ERROR;
  10395. }
  10396. wc_ed25519_free(&key);
  10397. #endif
  10398. return ret;
  10399. } /* END test_wc_ed25519_size */
  10400. /*
  10401. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  10402. */
  10403. static int test_wc_ed25519_exportKey (void)
  10404. {
  10405. int ret = 0;
  10406. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  10407. WC_RNG rng;
  10408. ed25519_key key;
  10409. byte priv[ED25519_PRV_KEY_SIZE];
  10410. byte pub[ED25519_PUB_KEY_SIZE];
  10411. byte privOnly[ED25519_PRV_KEY_SIZE];
  10412. word32 privSz = sizeof(priv);
  10413. word32 pubSz = sizeof(pub);
  10414. word32 privOnlySz = sizeof(privOnly);
  10415. ret = wc_InitRng(&rng);
  10416. if (ret != 0) {
  10417. return ret;
  10418. }
  10419. ret = wc_ed25519_init(&key);
  10420. if (ret != 0) {
  10421. wc_FreeRng(&rng);
  10422. return ret;
  10423. }
  10424. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  10425. if (ret != 0) {
  10426. wc_FreeRng(&rng);
  10427. wc_ed25519_free(&key);
  10428. return ret;
  10429. }
  10430. printf(testingFmt, "wc_ed25519_export_private()");
  10431. ret = wc_ed25519_export_private(&key, privOnly, &privOnlySz);
  10432. if (ret == 0) {
  10433. ret = wc_ed25519_export_private(NULL, privOnly, &privOnlySz);
  10434. if (ret == BAD_FUNC_ARG) {
  10435. ret = wc_ed25519_export_private(&key, NULL, &privOnlySz);
  10436. }
  10437. if (ret == BAD_FUNC_ARG) {
  10438. ret = wc_ed25519_export_private(&key, privOnly, NULL);
  10439. }
  10440. if (ret == BAD_FUNC_ARG) {
  10441. ret = 0;
  10442. } else if (ret == 0) {
  10443. ret = SSL_FATAL_ERROR;
  10444. }
  10445. }
  10446. printf(resultFmt, ret == 0 ? passed : failed);
  10447. if (ret == 0) {
  10448. printf(testingFmt, "wc_ed25519_export_key()");
  10449. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz);
  10450. if (ret == 0) {
  10451. ret = wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz);
  10452. if (ret == BAD_FUNC_ARG) {
  10453. ret = wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz);
  10454. }
  10455. if (ret == BAD_FUNC_ARG) {
  10456. ret = wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz);
  10457. }
  10458. if (ret == BAD_FUNC_ARG) {
  10459. ret = wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz);
  10460. }
  10461. if (ret == BAD_FUNC_ARG) {
  10462. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, NULL);
  10463. }
  10464. if (ret == BAD_FUNC_ARG) {
  10465. ret = 0;
  10466. } else if (ret == 0) {
  10467. ret = SSL_FATAL_ERROR;
  10468. }
  10469. }
  10470. printf(resultFmt, ret == 0 ? passed : failed);
  10471. } /* END wc_ed25519_export_key() */
  10472. /* Cross check output. */
  10473. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  10474. ret = SSL_FATAL_ERROR;
  10475. }
  10476. if (wc_FreeRng(&rng) && ret == 0) {
  10477. ret = SSL_FATAL_ERROR;
  10478. }
  10479. wc_ed25519_free(&key);
  10480. #endif
  10481. return ret;
  10482. } /* END test_wc_ed25519_exportKey */
  10483. /*
  10484. * Testing wc_curve25519_init and wc_curve25519_free.
  10485. */
  10486. static int test_wc_curve25519_init (void)
  10487. {
  10488. int ret = 0;
  10489. #if defined(HAVE_CURVE25519)
  10490. curve25519_key key;
  10491. printf(testingFmt, "wc_curve25519_init()");
  10492. ret = wc_curve25519_init(&key);
  10493. /* Test bad args for wc_curve25519_init */
  10494. if (ret == 0) {
  10495. ret = wc_curve25519_init(NULL);
  10496. if (ret == BAD_FUNC_ARG) {
  10497. ret = 0;
  10498. } else if (ret == 0) {
  10499. ret = SSL_FATAL_ERROR;
  10500. }
  10501. }
  10502. printf(resultFmt, ret == 0 ? passed : failed);
  10503. /* Test good args for wc_curve_25519_free */
  10504. wc_curve25519_free(&key);
  10505. wc_curve25519_free(NULL);
  10506. #endif
  10507. return ret;
  10508. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  10509. /*
  10510. * Testing wc_ecc_make_key.
  10511. */
  10512. static int test_wc_ecc_make_key (void)
  10513. {
  10514. int ret = 0;
  10515. #if defined(HAVE_ECC)
  10516. WC_RNG rng;
  10517. ecc_key key;
  10518. ret = wc_InitRng(&rng);
  10519. if (ret == 0) {
  10520. ret = wc_ecc_init(&key);
  10521. }
  10522. printf(testingFmt, "wc_ecc_make_key()");
  10523. if (ret == 0) {
  10524. ret = wc_ecc_make_key(&rng, KEY14, &key);
  10525. }
  10526. /* Pass in bad args. */
  10527. if (ret == 0) {
  10528. ret = wc_ecc_make_key(NULL, KEY14, &key);
  10529. if (ret == BAD_FUNC_ARG) {
  10530. ret = wc_ecc_make_key(&rng, KEY14, NULL);
  10531. }
  10532. if (ret == BAD_FUNC_ARG) {
  10533. ret = 0;
  10534. } else if (ret == 0) {
  10535. ret = WOLFSSL_FATAL_ERROR;
  10536. }
  10537. }
  10538. if (wc_FreeRng(&rng) && ret == 0) {
  10539. ret = WOLFSSL_FATAL_ERROR;
  10540. }
  10541. printf(resultFmt, ret == 0 ? passed : failed);
  10542. wc_ecc_free(&key);
  10543. #endif
  10544. return ret;
  10545. } /* END test_wc_ecc_make_key */
  10546. /*
  10547. * Testing wc_ecc_init()
  10548. */
  10549. static int test_wc_ecc_init (void)
  10550. {
  10551. int ret = 0;
  10552. #ifdef HAVE_ECC
  10553. ecc_key key;
  10554. printf(testingFmt, "wc_ecc_init()");
  10555. ret = wc_ecc_init(&key);
  10556. /* Pass in bad args. */
  10557. if (ret == 0) {
  10558. ret = wc_ecc_init(NULL);
  10559. if (ret == BAD_FUNC_ARG) {
  10560. ret = 0;
  10561. } else if (ret == 0) {
  10562. ret = WOLFSSL_FATAL_ERROR;
  10563. }
  10564. }
  10565. printf(resultFmt, ret == 0 ? passed : failed);
  10566. wc_ecc_free(&key);
  10567. #endif
  10568. return ret;
  10569. } /* END test_wc_ecc_init */
  10570. /*
  10571. * Testing wc_ecc_check_key()
  10572. */
  10573. static int test_wc_ecc_check_key (void)
  10574. {
  10575. int ret = 0;
  10576. #if defined(HAVE_ECC)
  10577. WC_RNG rng;
  10578. ecc_key key;
  10579. ret = wc_InitRng(&rng);
  10580. if (ret == 0) {
  10581. ret = wc_ecc_init(&key);
  10582. if (ret == 0) {
  10583. ret = wc_ecc_make_key(&rng, KEY14, &key);
  10584. }
  10585. }
  10586. printf(testingFmt, "wc_ecc_check_key()");
  10587. if (ret == 0) {
  10588. ret = wc_ecc_check_key(&key);
  10589. }
  10590. /* Pass in bad args. */
  10591. if (ret == 0) {
  10592. ret = wc_ecc_check_key(NULL);
  10593. if (ret == BAD_FUNC_ARG) {
  10594. ret = 0;
  10595. } else if (ret == 0) {
  10596. ret = WOLFSSL_FATAL_ERROR;
  10597. }
  10598. }
  10599. printf(resultFmt, ret == 0 ? passed : failed);
  10600. if (wc_FreeRng(&rng) && ret == 0) {
  10601. ret = WOLFSSL_FATAL_ERROR;
  10602. }
  10603. wc_ecc_free(&key);
  10604. #endif
  10605. return ret;
  10606. } /* END test_wc_ecc_check_key */
  10607. /*
  10608. * Testing wc_ecc_size()
  10609. */
  10610. static int test_wc_ecc_size (void)
  10611. {
  10612. int ret = 0;
  10613. #if defined(HAVE_ECC)
  10614. WC_RNG rng;
  10615. ecc_key key;
  10616. ret = wc_InitRng(&rng);
  10617. if (ret == 0) {
  10618. ret = wc_ecc_init(&key);
  10619. if (ret == 0) {
  10620. ret = wc_ecc_make_key(&rng, KEY14, &key);
  10621. }
  10622. }
  10623. printf(testingFmt, "wc_ecc_size()");
  10624. if (ret == 0) {
  10625. ret = wc_ecc_size(&key);
  10626. if (ret == KEY14) {
  10627. ret = 0;
  10628. } else if (ret == 0){
  10629. ret = WOLFSSL_FATAL_ERROR;
  10630. }
  10631. }
  10632. /* Test bad args. */
  10633. if (ret == 0) {
  10634. /* Returns Zero for bad arg. */
  10635. ret = wc_ecc_size(NULL);
  10636. }
  10637. printf(resultFmt, ret == 0 ? passed : failed);
  10638. if (wc_FreeRng(&rng) && ret == 0) {
  10639. ret = WOLFSSL_FATAL_ERROR;
  10640. }
  10641. wc_ecc_free(&key);
  10642. #endif
  10643. return ret;
  10644. } /* END test_wc_ecc_size */
  10645. /*
  10646. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  10647. */
  10648. static int test_wc_ecc_signVerify_hash (void)
  10649. {
  10650. int ret = 0;
  10651. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN)
  10652. WC_RNG rng;
  10653. ecc_key key;
  10654. int signH = WOLFSSL_FATAL_ERROR;
  10655. #ifdef HAVE_ECC_VERIFY
  10656. int verifyH = WOLFSSL_FATAL_ERROR;
  10657. int verify = 0;
  10658. #endif
  10659. word32 siglen = ECC_BUFSIZE;
  10660. byte sig[ECC_BUFSIZE];
  10661. byte digest[] = "Everyone gets Friday off.";
  10662. word32 digestlen = (word32)XSTRLEN((char*)digest);
  10663. /* Init stack var */
  10664. XMEMSET(sig, 0, siglen);
  10665. /* Init structs. */
  10666. ret = wc_InitRng(&rng);
  10667. if (ret == 0) {
  10668. ret = wc_ecc_init(&key);
  10669. if (ret == 0) {
  10670. ret = wc_ecc_make_key(&rng, KEY14, &key);
  10671. }
  10672. }
  10673. printf(testingFmt, "wc_ecc_sign_hash()");
  10674. if (ret == 0) {
  10675. ret = wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key);
  10676. }
  10677. /* Checkk bad args. */
  10678. if (ret == 0) {
  10679. signH = wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key);
  10680. if (signH == ECC_BAD_ARG_E) {
  10681. signH = wc_ecc_sign_hash(digest, digestlen, NULL, &siglen,
  10682. &rng, &key);
  10683. }
  10684. if (signH == ECC_BAD_ARG_E) {
  10685. signH = wc_ecc_sign_hash(digest, digestlen, sig, NULL,
  10686. &rng, &key);
  10687. }
  10688. if (signH == ECC_BAD_ARG_E) {
  10689. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  10690. NULL, &key);
  10691. }
  10692. if (signH == ECC_BAD_ARG_E) {
  10693. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  10694. &rng, NULL);
  10695. }
  10696. if (signH == ECC_BAD_ARG_E) {
  10697. signH = 0;
  10698. } else if (ret == 0) {
  10699. signH = WOLFSSL_FATAL_ERROR;
  10700. }
  10701. }
  10702. printf(resultFmt, signH == 0 ? passed : failed);
  10703. #ifdef HAVE_ECC_VERIFY
  10704. printf(testingFmt, "wc_ecc_verify_hash()");
  10705. ret = wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify, &key);
  10706. if (verify != 1 && ret == 0) {
  10707. ret = WOLFSSL_FATAL_ERROR;
  10708. }
  10709. /* Test bad args. */
  10710. if (ret == 0) {
  10711. verifyH = wc_ecc_verify_hash(NULL, siglen, digest, digestlen,
  10712. &verify, &key);
  10713. if (verifyH == ECC_BAD_ARG_E) {
  10714. verifyH = wc_ecc_verify_hash(sig, siglen, NULL, digestlen,
  10715. &verify, &key);
  10716. }
  10717. if (verifyH == ECC_BAD_ARG_E) {
  10718. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  10719. NULL, &key);
  10720. }
  10721. if (verifyH == ECC_BAD_ARG_E) {
  10722. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  10723. &verify, NULL);
  10724. }
  10725. if (verifyH == ECC_BAD_ARG_E) {
  10726. verifyH = 0;
  10727. } else if (ret == 0) {
  10728. verifyH = WOLFSSL_FATAL_ERROR;
  10729. }
  10730. }
  10731. printf(resultFmt, verifyH == 0 ? passed : failed);
  10732. #endif /* HAVE_ECC_VERIFY */
  10733. if (wc_FreeRng(&rng) && ret == 0) {
  10734. ret = WOLFSSL_FATAL_ERROR;
  10735. }
  10736. wc_ecc_free(&key);
  10737. #endif
  10738. return ret;
  10739. } /* END test_wc_ecc_sign_hash */
  10740. /*
  10741. * Testing wc_ecc_shared_secret()
  10742. */
  10743. static int test_wc_ecc_shared_secret (void)
  10744. {
  10745. int ret = 0;
  10746. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE)
  10747. ecc_key key, pubKey;
  10748. WC_RNG rng;
  10749. int keySz = KEY16;
  10750. byte out[keySz];
  10751. word32 outlen = (word32)sizeof(out);
  10752. /* Initialize variables. */
  10753. XMEMSET(out, 0, keySz);
  10754. ret = wc_InitRng(&rng);
  10755. if (ret == 0) {
  10756. ret = wc_ecc_init(&key);
  10757. if (ret == 0) {
  10758. ret = wc_ecc_init(&pubKey);
  10759. }
  10760. }
  10761. if (ret == 0) {
  10762. ret = wc_ecc_make_key(&rng, keySz, &key);
  10763. }
  10764. if (ret == 0) {
  10765. ret = wc_ecc_make_key(&rng, keySz, &pubKey);
  10766. }
  10767. printf(testingFmt, "wc_ecc_shared_secret()");
  10768. if (ret == 0) {
  10769. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  10770. /* Test bad args. */
  10771. if (ret == 0) {
  10772. ret = wc_ecc_shared_secret(NULL, &pubKey, out, &outlen);
  10773. if (ret == BAD_FUNC_ARG) {
  10774. ret = wc_ecc_shared_secret(&key, NULL, out, &outlen);
  10775. }
  10776. if (ret == BAD_FUNC_ARG) {
  10777. ret = wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen);
  10778. }
  10779. if (ret == BAD_FUNC_ARG) {
  10780. ret = wc_ecc_shared_secret(&key, &pubKey, out, NULL);
  10781. }
  10782. if (ret == BAD_FUNC_ARG) {
  10783. ret = 0;
  10784. } else if (ret == 0) {
  10785. ret = WOLFSSL_FATAL_ERROR;
  10786. }
  10787. }
  10788. }
  10789. printf(resultFmt, ret == 0 ? passed : failed);
  10790. if (wc_FreeRng(&rng) && ret == 0) {
  10791. ret = WOLFSSL_FATAL_ERROR;
  10792. }
  10793. wc_ecc_free(&key);
  10794. wc_ecc_free(&pubKey);
  10795. #endif
  10796. return ret;
  10797. } /* END tests_wc_ecc_shared_secret */
  10798. /*
  10799. * testint wc_ecc_export_x963()
  10800. */
  10801. static int test_wc_ecc_export_x963 (void)
  10802. {
  10803. int ret = 0;
  10804. #ifdef HAVE_ECC
  10805. ecc_key key;
  10806. WC_RNG rng;
  10807. byte out[ECC_ASN963_MAX_BUF_SZ];
  10808. word32 outlen = sizeof(out);
  10809. /* Initialize variables. */
  10810. XMEMSET(out, 0, outlen);
  10811. ret = wc_InitRng(&rng);
  10812. if (ret == 0) {
  10813. ret = wc_ecc_init(&key);
  10814. if (ret == 0) {
  10815. ret = wc_ecc_make_key(&rng, KEY20, &key);
  10816. }
  10817. }
  10818. printf(testingFmt, "wc_ecc_export_x963()");
  10819. if (ret == 0) {
  10820. ret = wc_ecc_export_x963(&key, out, &outlen);
  10821. }
  10822. /* Test bad args. */
  10823. if (ret == 0) {
  10824. ret = wc_ecc_export_x963(NULL, out, &outlen);
  10825. if (ret == ECC_BAD_ARG_E) {
  10826. ret = wc_ecc_export_x963(&key, NULL, &outlen);
  10827. }
  10828. if (ret == LENGTH_ONLY_E) {
  10829. ret = wc_ecc_export_x963(&key, out, NULL);
  10830. }
  10831. if (ret == ECC_BAD_ARG_E) {
  10832. key.idx = -4;
  10833. ret = wc_ecc_export_x963(&key, out, &outlen);
  10834. }
  10835. if (ret == ECC_BAD_ARG_E) {
  10836. ret = 0;
  10837. } else {
  10838. ret = WOLFSSL_FATAL_ERROR;
  10839. }
  10840. }
  10841. printf(resultFmt, ret == 0 ? passed : failed);
  10842. if (wc_FreeRng(&rng) && ret == 0) {
  10843. ret = WOLFSSL_FATAL_ERROR;
  10844. }
  10845. wc_ecc_free(&key);
  10846. #endif
  10847. return ret;
  10848. } /* END test_wc_ecc_export_x963 */
  10849. /*
  10850. * Testing wc_ecc_export_x963_ex()
  10851. * compile with --enable-compkey will use compression.
  10852. */
  10853. static int test_wc_ecc_export_x963_ex (void)
  10854. {
  10855. int ret = 0;
  10856. #if defined(HAVE_ECC)
  10857. ecc_key key;
  10858. WC_RNG rng;
  10859. byte out[ECC_ASN963_MAX_BUF_SZ];
  10860. word32 outlen = sizeof(out);
  10861. #ifdef HAVE_COMP_KEY
  10862. word32 badOutLen = 5;
  10863. #endif
  10864. /* Init stack variables. */
  10865. XMEMSET(out, 0, outlen);
  10866. ret = wc_InitRng(&rng);
  10867. if (ret == 0) {
  10868. ret = wc_ecc_init(&key);
  10869. if (ret == 0) {
  10870. ret = wc_ecc_make_key(&rng, KEY64, &key);
  10871. }
  10872. }
  10873. printf(testingFmt, "wc_ecc_export_x963_ex()");
  10874. #ifdef HAVE_COMP_KEY
  10875. if (ret == 0) {
  10876. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  10877. }
  10878. #else
  10879. if (ret == 0) {
  10880. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  10881. }
  10882. #endif
  10883. /* Test bad args. */
  10884. #ifdef HAVE_COMP_KEY
  10885. if (ret == 0) {
  10886. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, COMP);
  10887. if (ret == BAD_FUNC_ARG) {
  10888. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP);
  10889. }
  10890. if (ret == BAD_FUNC_ARG) {
  10891. ret = wc_ecc_export_x963_ex(&key, out, NULL, COMP);
  10892. }
  10893. if (ret == BAD_FUNC_ARG) {
  10894. ret = wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP);
  10895. }
  10896. if (ret == BUFFER_E) {
  10897. key.idx = -4;
  10898. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  10899. }
  10900. if (ret == ECC_BAD_ARG_E) {
  10901. ret = 0;
  10902. } else {
  10903. ret = WOLFSSL_FATAL_ERROR;
  10904. }
  10905. }
  10906. #else
  10907. if (ret == 0) {
  10908. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP);
  10909. if (ret == BAD_FUNC_ARG) {
  10910. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP);
  10911. }
  10912. if (ret == BAD_FUNC_ARG) {
  10913. ret = wc_ecc_export_x963_ex(&key, out, &outlen, 1);
  10914. }
  10915. if (ret == NOT_COMPILED_IN) {
  10916. ret = wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP);
  10917. }
  10918. if (ret == BAD_FUNC_ARG) {
  10919. key.idx = -4;
  10920. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  10921. }
  10922. if (ret == ECC_BAD_ARG_E) {
  10923. ret = 0;
  10924. } else if (ret == 0) {
  10925. ret = WOLFSSL_FATAL_ERROR;
  10926. }
  10927. }
  10928. #endif
  10929. printf(resultFmt, ret == 0 ? passed : failed);
  10930. if (wc_FreeRng(&rng) && ret == 0) {
  10931. ret = WOLFSSL_FATAL_ERROR;
  10932. }
  10933. wc_ecc_free(&key);
  10934. #endif
  10935. return ret;
  10936. } /* END test_wc_ecc_export_x963_ex */
  10937. /*
  10938. * testing wc_ecc_import_x963()
  10939. */
  10940. static int test_wc_ecc_import_x963 (void)
  10941. {
  10942. int ret = 0;
  10943. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT)
  10944. ecc_key pubKey, key;
  10945. WC_RNG rng;
  10946. byte x963[ECC_ASN963_MAX_BUF_SZ];
  10947. word32 x963Len = (word32)sizeof(x963);
  10948. /* Init stack variables. */
  10949. XMEMSET(x963, 0, x963Len);
  10950. ret = wc_InitRng(&rng);
  10951. if (ret == 0) {
  10952. ret = wc_ecc_init(&pubKey);
  10953. if (ret == 0) {
  10954. ret = wc_ecc_init(&key);
  10955. }
  10956. if (ret == 0) {
  10957. ret = wc_ecc_make_key(&rng, KEY24, &key);
  10958. }
  10959. if (ret == 0) {
  10960. ret = wc_ecc_export_x963(&key, x963, &x963Len);
  10961. }
  10962. }
  10963. printf(testingFmt, "wc_ecc_import_x963()");
  10964. if (ret == 0) {
  10965. ret = wc_ecc_import_x963(x963, x963Len, &pubKey);
  10966. }
  10967. /* Test bad args. */
  10968. if (ret == 0) {
  10969. ret = wc_ecc_import_x963(NULL, x963Len, &pubKey);
  10970. if (ret == BAD_FUNC_ARG) {
  10971. ret = wc_ecc_import_x963(x963, x963Len, NULL);
  10972. }
  10973. if (ret == BAD_FUNC_ARG) {
  10974. ret = wc_ecc_import_x963(x963, x963Len + 1, &pubKey);
  10975. }
  10976. if (ret == ECC_BAD_ARG_E) {
  10977. ret = 0;
  10978. } else if (ret == 0) {
  10979. ret = WOLFSSL_FATAL_ERROR;
  10980. }
  10981. }
  10982. printf(resultFmt, ret == 0 ? passed : failed);
  10983. if (wc_FreeRng(&rng) && ret == 0) {
  10984. ret = WOLFSSL_FATAL_ERROR;
  10985. }
  10986. wc_ecc_free(&key);
  10987. wc_ecc_free(&pubKey);
  10988. #endif
  10989. return ret;
  10990. } /* END wc_ecc_import_x963 */
  10991. /*
  10992. * testing wc_ecc_import_private_key()
  10993. */
  10994. static int ecc_import_private_key (void)
  10995. {
  10996. int ret = 0;
  10997. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT)
  10998. ecc_key key, keyImp;
  10999. WC_RNG rng;
  11000. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  11001. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  11002. word32 privKeySz = (word32)sizeof(privKey);
  11003. word32 x963KeySz = (word32)sizeof(x963Key);
  11004. /* Init stack variables. */
  11005. XMEMSET(privKey, 0, privKeySz);
  11006. XMEMSET(x963Key, 0, x963KeySz);
  11007. ret = wc_InitRng(&rng);
  11008. if (ret == 0) {
  11009. ret = wc_ecc_init(&key);
  11010. if (ret == 0) {
  11011. ret = wc_ecc_init(&keyImp);
  11012. }
  11013. if (ret == 0) {
  11014. ret = wc_ecc_make_key(&rng, KEY48, &key);
  11015. }
  11016. if (ret == 0) {
  11017. ret = wc_ecc_export_x963(&key, x963Key, &x963KeySz);
  11018. }
  11019. if (ret == 0) {
  11020. ret = wc_ecc_export_private_only(&key, privKey, &privKeySz);
  11021. }
  11022. }
  11023. printf(testingFmt, "wc_ecc_import_private_key()");
  11024. if (ret == 0) {
  11025. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  11026. x963KeySz, &keyImp);
  11027. }
  11028. /* Pass in bad args. */
  11029. if (ret == 0) {
  11030. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  11031. x963KeySz, NULL);
  11032. if (ret == BAD_FUNC_ARG) {
  11033. ret = wc_ecc_import_private_key(NULL, privKeySz, x963Key,
  11034. x963KeySz, &keyImp);
  11035. }
  11036. if (ret == BAD_FUNC_ARG) {
  11037. ret = 0;
  11038. } else if (ret == 0) {
  11039. ret = WOLFSSL_FATAL_ERROR;
  11040. }
  11041. }
  11042. printf(resultFmt, ret == 0 ? passed : failed);
  11043. if (wc_FreeRng(&rng) && ret == 0) {
  11044. ret = WOLFSSL_FATAL_ERROR;
  11045. }
  11046. wc_ecc_free(&key);
  11047. wc_ecc_free(&keyImp);
  11048. #endif
  11049. return ret;
  11050. } /* END wc_ecc_import_private_key */
  11051. /*
  11052. * Testing wc_ecc_export_private_only()
  11053. */
  11054. static int test_wc_ecc_export_private_only (void)
  11055. {
  11056. int ret = 0;
  11057. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT)
  11058. ecc_key key;
  11059. WC_RNG rng;
  11060. byte out[ECC_PRIV_KEY_BUF];
  11061. word32 outlen = sizeof(out);
  11062. /* Init stack variables. */
  11063. XMEMSET(out, 0, outlen);
  11064. ret = wc_InitRng(&rng);
  11065. if (ret == 0) {
  11066. ret = wc_ecc_init(&key);
  11067. if (ret == 0) {
  11068. ret = wc_ecc_make_key(&rng, KEY32, &key);
  11069. }
  11070. }
  11071. printf(testingFmt, "wc_ecc_export_private_only()");
  11072. if (ret == 0) {
  11073. ret = wc_ecc_export_private_only(&key, out, &outlen);
  11074. }
  11075. /* Pass in bad args. */
  11076. if (ret == 0) {
  11077. ret = wc_ecc_export_private_only(NULL, out, &outlen);
  11078. if (ret == BAD_FUNC_ARG) {
  11079. ret = wc_ecc_export_private_only(&key, NULL, &outlen);
  11080. }
  11081. if (ret == BAD_FUNC_ARG) {
  11082. ret = wc_ecc_export_private_only(&key, out, NULL);
  11083. }
  11084. if (ret == BAD_FUNC_ARG) {
  11085. ret = 0;
  11086. } else if (ret == 0) {
  11087. ret = WOLFSSL_FATAL_ERROR;
  11088. }
  11089. }
  11090. printf(resultFmt, ret == 0 ? passed : failed);
  11091. if (wc_FreeRng(&rng) && ret == 0) {
  11092. ret = WOLFSSL_FATAL_ERROR;
  11093. }
  11094. wc_ecc_free(&key);
  11095. #endif
  11096. return ret;
  11097. } /* END test_wc_ecc_export_private_only */
  11098. /*
  11099. * Testing wc_ecc_rs_to_sig()
  11100. */
  11101. static int test_wc_ecc_rs_to_sig (void)
  11102. {
  11103. int ret = 0;
  11104. #if defined(HAVE_ECC) && !defined(NO_ASN)
  11105. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  11106. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  11107. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  11108. byte sig[ECC_MAX_SIG_SIZE];
  11109. word32 siglen = (word32)sizeof(sig);
  11110. /*R and S max size is the order of curve. 2^192.*/
  11111. int keySz = KEY24;
  11112. byte r[keySz];
  11113. byte s[keySz];
  11114. word32 rlen = (word32)sizeof(r);
  11115. word32 slen = (word32)sizeof(s);
  11116. /* Init stack variables. */
  11117. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  11118. XMEMSET(r, 0, keySz);
  11119. XMEMSET(s, 0, keySz);
  11120. printf(testingFmt, "wc_ecc_rs_to_sig()");
  11121. ret = wc_ecc_rs_to_sig(R, S, sig, &siglen);
  11122. /* Test bad args. */
  11123. if (ret == 0) {
  11124. ret = wc_ecc_rs_to_sig(NULL, S, sig, &siglen);
  11125. if (ret == ECC_BAD_ARG_E) {
  11126. ret = wc_ecc_rs_to_sig(R, NULL, sig, &siglen);
  11127. }
  11128. if (ret == ECC_BAD_ARG_E) {
  11129. ret = wc_ecc_rs_to_sig(R, S, sig, NULL);
  11130. }
  11131. if (ret == ECC_BAD_ARG_E) {
  11132. ret = wc_ecc_rs_to_sig(R, S, NULL, &siglen);
  11133. }
  11134. if (ret == ECC_BAD_ARG_E) {
  11135. ret = 0;
  11136. } else {
  11137. ret = WOLFSSL_FATAL_ERROR;
  11138. }
  11139. }
  11140. printf(resultFmt, ret == 0 ? passed : failed);
  11141. printf(testingFmt, "wc_ecc_sig_to_rs()");
  11142. if (ret == 0) {
  11143. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen);
  11144. }
  11145. /* Test bad args. */
  11146. if (ret == 0) {
  11147. ret = wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen);
  11148. if (ret == ECC_BAD_ARG_E) {
  11149. ret = wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen);
  11150. }
  11151. if (ret == ECC_BAD_ARG_E) {
  11152. ret = wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen);
  11153. }
  11154. if (ret == ECC_BAD_ARG_E) {
  11155. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen);
  11156. }
  11157. if (ret == ECC_BAD_ARG_E) {
  11158. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL);
  11159. }
  11160. if (ret == ECC_BAD_ARG_E) {
  11161. ret = 0;
  11162. } else if (ret == 0) {
  11163. ret = WOLFSSL_FATAL_ERROR;
  11164. }
  11165. }
  11166. printf(resultFmt, ret == 0 ? passed : failed);
  11167. #endif
  11168. return ret;
  11169. } /* END test_wc_ecc_rs_to_sig */
  11170. static int test_wc_ecc_import_raw (void)
  11171. {
  11172. int ret = 0;
  11173. #ifdef HAVE_ECC
  11174. ecc_key key;
  11175. #ifdef HAVE_ALL_CURVES
  11176. const char* qx = "07008ea40b08dbe76432096e80a2494c94982d2d5bcf98e6";
  11177. const char* qy = "76fab681d00b414ea636ba215de26d98c41bd7f2e4d65477";
  11178. const char* d = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3";
  11179. const char* curveName = "SECP192R1";
  11180. #else
  11181. const char* qx =
  11182. "6c450448386596485678dcf46ccf75e80ff292443cddab1ff216d0c72cd9341";
  11183. const char* qy =
  11184. "9cac72ff8a90e4939e37714bfa07ae4612588535c3fdeab63ceb29b1d80f0d1";
  11185. const char* d =
  11186. "1e1dd938e15bdd036b0b0e2a6dc62fe7b46dbe042ac42310c6d5db0cda63e807";
  11187. const char* curveName = "SECP256R1";
  11188. #endif
  11189. ret = wc_ecc_init(&key);
  11190. printf(testingFmt, "wc_ecc_import_raw()");
  11191. if (ret == 0) {
  11192. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  11193. }
  11194. /* Test bad args. */
  11195. if (ret == 0) {
  11196. ret = wc_ecc_import_raw(NULL, qx, qy, d, curveName);
  11197. if (ret == BAD_FUNC_ARG) {
  11198. ret = wc_ecc_import_raw(&key, NULL, qy, d, curveName);
  11199. }
  11200. if (ret == BAD_FUNC_ARG) {
  11201. ret = wc_ecc_import_raw(&key, qx, NULL, d, curveName);
  11202. }
  11203. if (ret == BAD_FUNC_ARG) {
  11204. ret = wc_ecc_import_raw(&key, qx, qy, d, NULL);
  11205. }
  11206. if (ret == BAD_FUNC_ARG) {
  11207. ret = 0;
  11208. } else if (ret == 0) {
  11209. ret = WOLFSSL_FATAL_ERROR;
  11210. }
  11211. }
  11212. printf(resultFmt, ret == 0 ? passed : failed);
  11213. wc_ecc_free(&key);
  11214. #endif
  11215. return ret;
  11216. } /* END test_wc_ecc_import_raw */
  11217. /*
  11218. * Testing wc_ecc_sig_size()
  11219. */
  11220. static int test_wc_ecc_sig_size (void)
  11221. {
  11222. int ret = 0;
  11223. #ifdef HAVE_ECC
  11224. ecc_key key;
  11225. WC_RNG rng;
  11226. int keySz = KEY16;
  11227. ret = wc_InitRng(&rng);
  11228. if (ret == 0) {
  11229. ret = wc_ecc_init(&key);
  11230. if (ret == 0) {
  11231. ret = wc_ecc_make_key(&rng, keySz, &key);
  11232. }
  11233. }
  11234. printf(testingFmt, "wc_ecc_sig_size()");
  11235. if (ret == 0) {
  11236. ret = wc_ecc_sig_size(&key);
  11237. if (ret == (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ)) {
  11238. ret = 0;
  11239. }
  11240. }
  11241. printf(resultFmt, ret == 0 ? passed : failed);
  11242. if (wc_FreeRng(&rng) && ret == 0) {
  11243. ret = WOLFSSL_FATAL_ERROR;
  11244. }
  11245. wc_ecc_free(&key);
  11246. #endif
  11247. return ret;
  11248. } /* END test_wc_ecc_sig_size */
  11249. /*
  11250. * Testing wc_ecc_ctx_new()
  11251. */
  11252. static int test_wc_ecc_ctx_new (void)
  11253. {
  11254. int ret = 0;
  11255. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT)
  11256. WC_RNG rng;
  11257. ecEncCtx* cli = NULL;
  11258. ecEncCtx* srv = NULL;
  11259. ret = wc_InitRng(&rng);
  11260. printf(testingFmt, "wc_ecc_ctx_new()");
  11261. if (ret == 0) {
  11262. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  11263. srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
  11264. }
  11265. if (ret == 0 && (cli == NULL || srv == NULL)) {
  11266. ret = WOLFSSL_FATAL_ERROR;
  11267. }
  11268. wc_ecc_ctx_free(cli);
  11269. wc_ecc_ctx_free(srv);
  11270. /* Test bad args. */
  11271. if (ret == 0) {
  11272. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  11273. cli = wc_ecc_ctx_new(0, &rng);
  11274. if (cli != NULL) {
  11275. ret = WOLFSSL_FATAL_ERROR;
  11276. }
  11277. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL);
  11278. if (cli != NULL) {
  11279. ret = WOLFSSL_FATAL_ERROR;
  11280. }
  11281. }
  11282. printf(resultFmt, ret == 0 ? passed : failed);
  11283. if (wc_FreeRng(&rng) && ret == 0) {
  11284. ret = WOLFSSL_FATAL_ERROR;
  11285. }
  11286. wc_ecc_ctx_free(cli);
  11287. #endif
  11288. return ret;
  11289. } /* END test_wc_ecc_ctx_new */
  11290. /*
  11291. * Tesing wc_ecc_reset()
  11292. */
  11293. static int test_wc_ecc_ctx_reset (void)
  11294. {
  11295. int ret = 0;
  11296. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT)
  11297. ecEncCtx* ctx = NULL;
  11298. WC_RNG rng;
  11299. ret = wc_InitRng(&rng);
  11300. if (ret == 0) {
  11301. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) {
  11302. ret = WOLFSSL_FATAL_ERROR;
  11303. }
  11304. }
  11305. printf(testingFmt, "wc_ecc_ctx_reset()");
  11306. if (ret == 0) {
  11307. ret = wc_ecc_ctx_reset(ctx, &rng);
  11308. }
  11309. /* Pass in bad args. */
  11310. if (ret == 0) {
  11311. ret = wc_ecc_ctx_reset(NULL, &rng);
  11312. if (ret == BAD_FUNC_ARG) {
  11313. ret = wc_ecc_ctx_reset(ctx, NULL);
  11314. }
  11315. if (ret == BAD_FUNC_ARG) {
  11316. ret = 0;
  11317. } else if (ret == 0) {
  11318. ret = WOLFSSL_FATAL_ERROR;
  11319. }
  11320. }
  11321. printf(resultFmt, ret == 0 ? passed : failed);
  11322. if (wc_FreeRng(&rng) && ret == 0) {
  11323. ret = WOLFSSL_FATAL_ERROR;
  11324. }
  11325. wc_ecc_ctx_free(ctx);
  11326. #endif
  11327. return ret;
  11328. } /* END test_wc_ecc_ctx_reset */
  11329. /*
  11330. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  11331. */
  11332. static int test_wc_ecc_ctx_set_peer_salt (void)
  11333. {
  11334. int ret = 0;
  11335. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT)
  11336. WC_RNG rng;
  11337. ecEncCtx* cliCtx = NULL;
  11338. ecEncCtx* servCtx = NULL;
  11339. const byte* cliSalt = NULL;
  11340. const byte* servSalt = NULL;
  11341. ret = wc_InitRng(&rng);
  11342. if (ret == 0) {
  11343. if ( ( (cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) ||
  11344. ( (servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng)) == NULL) ) {
  11345. ret = WOLFSSL_FATAL_ERROR;
  11346. }
  11347. }
  11348. printf(testingFmt, "wc_ecc_ctx_get_own_salt()");
  11349. /* Test bad args. */
  11350. if (ret == 0) {
  11351. cliSalt = wc_ecc_ctx_get_own_salt(NULL);
  11352. if (cliSalt != NULL) {
  11353. ret = WOLFSSL_FATAL_ERROR;
  11354. }
  11355. }
  11356. if (ret == 0) {
  11357. cliSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  11358. servSalt = wc_ecc_ctx_get_own_salt(servCtx);
  11359. if (cliSalt == NULL || servSalt == NULL) {
  11360. ret = WOLFSSL_FATAL_ERROR;
  11361. }
  11362. }
  11363. printf(resultFmt, ret == 0 ? passed : failed);
  11364. printf(testingFmt, "wc_ecc_ctx_set_peer_salt()");
  11365. if (ret == 0) {
  11366. ret = wc_ecc_ctx_set_peer_salt(cliCtx, servSalt);
  11367. }
  11368. /* Test bad args. */
  11369. if (ret == 0) {
  11370. ret = wc_ecc_ctx_set_peer_salt(NULL, servSalt);
  11371. if (ret == BAD_FUNC_ARG) {
  11372. ret = wc_ecc_ctx_set_peer_salt(cliCtx, NULL);
  11373. }
  11374. if (ret == BAD_FUNC_ARG) {
  11375. ret = 0;
  11376. } else if (ret == 0) {
  11377. ret = WOLFSSL_FATAL_ERROR;
  11378. }
  11379. }
  11380. printf(resultFmt, ret == 0 ? passed : failed);
  11381. if (wc_FreeRng(&rng) && ret == 0) {
  11382. ret = WOLFSSL_FATAL_ERROR;
  11383. }
  11384. wc_ecc_ctx_free(cliCtx);
  11385. wc_ecc_ctx_free(servCtx);
  11386. #endif
  11387. return ret;
  11388. } /* END test_wc_ecc_ctx_set_peer_salt */
  11389. /*
  11390. * Testing wc_ecc_ctx_set_info()
  11391. */
  11392. static int test_wc_ecc_ctx_set_info (void)
  11393. {
  11394. int ret = 0;
  11395. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT)
  11396. ecEncCtx* ctx = NULL;
  11397. WC_RNG rng;
  11398. const char* optInfo = "Optional Test Info.";
  11399. int optInfoSz = (int)XSTRLEN(optInfo);
  11400. const char* badOptInfo = NULL;
  11401. ret = wc_InitRng(&rng);
  11402. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL || ret != 0 ) {
  11403. ret = WOLFSSL_FATAL_ERROR;
  11404. }
  11405. printf(testingFmt, "wc_ecc_ctx_set_info()");
  11406. if (ret == 0) {
  11407. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz);
  11408. }
  11409. /* Test bad args. */
  11410. if (ret == 0) {
  11411. ret = wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz);
  11412. if (ret == BAD_FUNC_ARG) {
  11413. ret = wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz);
  11414. }
  11415. if (ret == BAD_FUNC_ARG) {
  11416. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1);
  11417. }
  11418. if (ret == BAD_FUNC_ARG) {
  11419. ret = 0;
  11420. } else if (ret == 0) {
  11421. ret = WOLFSSL_FATAL_ERROR;
  11422. }
  11423. }
  11424. printf(resultFmt, ret == 0 ? passed : failed);
  11425. if (wc_FreeRng(&rng) && ret == 0) {
  11426. ret = WOLFSSL_FATAL_ERROR;
  11427. }
  11428. wc_ecc_ctx_free(ctx);
  11429. #endif
  11430. return ret;
  11431. } /* END test_wc_ecc_ctx_set_info */
  11432. /*
  11433. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  11434. */
  11435. static int test_wc_ecc_encryptDecrypt (void)
  11436. {
  11437. int ret = 0;
  11438. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && defined(WOLFSSL_AES_128)
  11439. ecc_key srvKey, cliKey;
  11440. WC_RNG rng;
  11441. const char* msg = "EccBlock Size 16";
  11442. word32 msgSz = (word32)XSTRLEN(msg);
  11443. byte out[XSTRLEN(msg) + WC_SHA256_DIGEST_SIZE];
  11444. word32 outSz = (word32)sizeof(out);
  11445. byte plain[XSTRLEN(msg) + 1];
  11446. word32 plainSz = (word32)sizeof(plain);
  11447. int keySz = KEY20;
  11448. /* Init stack variables. */
  11449. XMEMSET(out, 0, outSz);
  11450. XMEMSET(plain, 0, plainSz);
  11451. ret = wc_InitRng(&rng);
  11452. if (ret == 0) {
  11453. ret = wc_ecc_init(&cliKey);
  11454. if (ret == 0) {
  11455. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  11456. }
  11457. if (ret == 0) {
  11458. ret = wc_ecc_init(&srvKey);
  11459. }
  11460. if (ret == 0) {
  11461. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  11462. }
  11463. }
  11464. printf(testingFmt, "wc_ecc_encrypt()");
  11465. if (ret == 0) {
  11466. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  11467. &outSz, NULL);
  11468. }
  11469. if (ret == 0) {
  11470. ret = wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out,
  11471. &outSz, NULL);
  11472. if (ret == BAD_FUNC_ARG) {
  11473. ret = wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out,
  11474. &outSz, NULL);
  11475. }
  11476. if (ret == BAD_FUNC_ARG) {
  11477. ret = wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out,
  11478. &outSz, NULL);
  11479. }
  11480. if (ret == BAD_FUNC_ARG) {
  11481. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  11482. &outSz, NULL);
  11483. }
  11484. if (ret == BAD_FUNC_ARG) {
  11485. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  11486. NULL, NULL);
  11487. }
  11488. if (ret == BAD_FUNC_ARG) {
  11489. ret = 0;
  11490. } else if (ret == 0) {
  11491. ret = WOLFSSL_FATAL_ERROR;
  11492. }
  11493. }
  11494. printf(resultFmt, ret == 0 ? passed : failed);
  11495. printf(testingFmt, "wc_ecc_decrypt()");
  11496. if (ret == 0) {
  11497. ret = wc_ecc_decrypt(&srvKey, &cliKey, out, outSz, plain,
  11498. &plainSz, NULL);
  11499. }
  11500. if (ret == 0) {
  11501. ret = wc_ecc_decrypt(NULL, &cliKey, out, outSz, plain,
  11502. &plainSz, NULL);
  11503. if (ret == BAD_FUNC_ARG) {
  11504. ret = wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain,
  11505. &plainSz, NULL);
  11506. }
  11507. if (ret == BAD_FUNC_ARG) {
  11508. ret = wc_ecc_decrypt(&srvKey, &cliKey, NULL, outSz, plain,
  11509. &plainSz, NULL);
  11510. }
  11511. if (ret == BAD_FUNC_ARG) {
  11512. ret = wc_ecc_decrypt(&srvKey, &cliKey, out, outSz, NULL,
  11513. &plainSz, NULL);
  11514. }
  11515. if (ret == BAD_FUNC_ARG) {
  11516. ret = wc_ecc_decrypt(&srvKey, &cliKey, out, outSz,
  11517. plain, NULL, NULL);
  11518. }
  11519. if (ret == BAD_FUNC_ARG) {
  11520. ret = 0;
  11521. } else if (ret == 0) {
  11522. ret = WOLFSSL_FATAL_ERROR;
  11523. }
  11524. }
  11525. if (XMEMCMP(msg, plain, msgSz) != 0) {
  11526. ret = WOLFSSL_FATAL_ERROR;
  11527. }
  11528. printf(resultFmt, ret == 0 ? passed : failed);
  11529. if (wc_FreeRng(&rng) && ret == 0) {
  11530. ret = WOLFSSL_FATAL_ERROR;
  11531. }
  11532. wc_ecc_free(&cliKey);
  11533. wc_ecc_free(&srvKey);
  11534. #endif
  11535. return ret;
  11536. } /* END test_wc_ecc_encryptDecrypt */
  11537. /*
  11538. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  11539. */
  11540. static int test_wc_ecc_del_point (void)
  11541. {
  11542. int ret = 0;
  11543. #if defined(HAVE_ECC)
  11544. ecc_point* pt;
  11545. printf(testingFmt, "wc_ecc_new_point()");
  11546. pt = wc_ecc_new_point();
  11547. if (!pt) {
  11548. ret = WOLFSSL_FATAL_ERROR;
  11549. }
  11550. printf(resultFmt, ret == 0 ? passed : failed);
  11551. wc_ecc_del_point(pt);
  11552. #endif
  11553. return ret;
  11554. } /* END test_wc_ecc_del_point */
  11555. /*
  11556. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  11557. * wc_ecc_import_point_der(), wc_ecc_copy_point(), and wc_ecc_cmp_point()
  11558. */
  11559. static int test_wc_ecc_pointFns (void)
  11560. {
  11561. int ret = 0;
  11562. #if defined(HAVE_ECC)
  11563. ecc_key key;
  11564. WC_RNG rng;
  11565. ecc_point* point = NULL;
  11566. ecc_point* cpypt = NULL;
  11567. int idx = 0;
  11568. int keySz = KEY32;
  11569. byte der[DER_SZ];
  11570. word32 derlenChk = 0;
  11571. word32 derSz = (int)sizeof(der);
  11572. /* Init stack variables. */
  11573. XMEMSET(der, 0, derSz);
  11574. ret = wc_InitRng(&rng);
  11575. if (ret == 0) {
  11576. ret = wc_ecc_init(&key);
  11577. if (ret == 0) {
  11578. ret = wc_ecc_make_key(&rng, keySz, &key);
  11579. }
  11580. }
  11581. if (ret == 0) {
  11582. point = wc_ecc_new_point();
  11583. if (!point) {
  11584. ret = WOLFSSL_FATAL_ERROR;
  11585. }
  11586. }
  11587. if (ret == 0) {
  11588. cpypt = wc_ecc_new_point();
  11589. if (!cpypt) {
  11590. ret = WOLFSSL_FATAL_ERROR;
  11591. }
  11592. }
  11593. /* Export */
  11594. printf(testingFmt, "wc_ecc_export_point_der()");
  11595. if (ret == 0) {
  11596. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  11597. NULL, &derlenChk);
  11598. /* Check length value. */
  11599. if (derSz == derlenChk && ret == LENGTH_ONLY_E) {
  11600. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  11601. der, &derSz);
  11602. }
  11603. }
  11604. /* Test bad args. */
  11605. if (ret == 0) {
  11606. ret = wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz);
  11607. if (ret == ECC_BAD_ARG_E) {
  11608. ret = wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz);
  11609. }
  11610. if (ret == ECC_BAD_ARG_E) {
  11611. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  11612. der, NULL);
  11613. }
  11614. if (ret == ECC_BAD_ARG_E) {
  11615. ret = 0;
  11616. } else if (ret == 0) {
  11617. ret = WOLFSSL_FATAL_ERROR;
  11618. }
  11619. }
  11620. printf(resultFmt, ret == 0 ? passed : failed);
  11621. /* Import */
  11622. printf(testingFmt, "wc_ecc_import_point_der()");
  11623. if (ret == 0) {
  11624. ret = wc_ecc_import_point_der(der, derSz, idx, point);
  11625. /* Condition double checks wc_ecc_cmp_point(). */
  11626. if (ret == 0 && XMEMCMP(&key.pubkey, point, sizeof(key.pubkey))) {
  11627. ret = wc_ecc_cmp_point(&key.pubkey, point);
  11628. }
  11629. }
  11630. /* Test bad args. */
  11631. if (ret == 0) {
  11632. ret = wc_ecc_import_point_der(NULL, derSz, idx, point);
  11633. if (ret == ECC_BAD_ARG_E) {
  11634. ret = wc_ecc_import_point_der(der, derSz, idx, NULL);
  11635. }
  11636. if (ret == ECC_BAD_ARG_E) {
  11637. ret = wc_ecc_import_point_der(der, derSz, -1, point);
  11638. }
  11639. if (ret == ECC_BAD_ARG_E) {
  11640. ret = wc_ecc_import_point_der(der, derSz + 1, idx, point);
  11641. }
  11642. if (ret == ECC_BAD_ARG_E) {
  11643. ret = 0;
  11644. } else if (ret == 0) {
  11645. ret = WOLFSSL_FATAL_ERROR;
  11646. }
  11647. }
  11648. printf(resultFmt, ret == 0 ? passed : failed);
  11649. /* Copy */
  11650. printf(testingFmt, "wc_ecc_copy_point()");
  11651. if (ret == 0) {
  11652. ret = wc_ecc_copy_point(point, cpypt);
  11653. }
  11654. /* Test bad args. */
  11655. if (ret == 0) {
  11656. ret = wc_ecc_copy_point(NULL, cpypt);
  11657. if (ret == ECC_BAD_ARG_E) {
  11658. ret = wc_ecc_copy_point(point, NULL);
  11659. }
  11660. if (ret == ECC_BAD_ARG_E) {
  11661. ret = 0;
  11662. } else if (ret == 0) {
  11663. ret = WOLFSSL_FATAL_ERROR;
  11664. }
  11665. }
  11666. printf(resultFmt, ret == 0 ? passed : failed);
  11667. printf(testingFmt, "wc_ecc_cmp_point()");
  11668. /* Compare point */
  11669. if (ret == 0) {
  11670. ret = wc_ecc_cmp_point(point, cpypt);
  11671. }
  11672. /* Test bad args. */
  11673. if (ret == 0) {
  11674. ret = wc_ecc_cmp_point(NULL, cpypt);
  11675. if (ret == BAD_FUNC_ARG) {
  11676. ret = wc_ecc_cmp_point(point, NULL);
  11677. }
  11678. if (ret == BAD_FUNC_ARG) {
  11679. ret = 0;
  11680. } else if (ret == 0) {
  11681. ret = WOLFSSL_FATAL_ERROR;
  11682. }
  11683. }
  11684. printf(resultFmt, ret == 0 ? passed : failed);
  11685. printf(testingFmt, "wc_ecc_point_is_at_infinity()");
  11686. /* At infinity if return == 1, otherwise return == 0. */
  11687. if (ret == 0) {
  11688. ret = wc_ecc_point_is_at_infinity(point);
  11689. }
  11690. /* Test bad args. */
  11691. if (ret == 0) {
  11692. ret = wc_ecc_point_is_at_infinity(NULL);
  11693. if (ret == BAD_FUNC_ARG) {
  11694. ret = 0;
  11695. } else if (ret == 0) {
  11696. ret = WOLFSSL_FATAL_ERROR;
  11697. }
  11698. }
  11699. printf(resultFmt, ret == 0 ? passed : failed);
  11700. /* Free */
  11701. wc_ecc_del_point(point);
  11702. wc_ecc_del_point(cpypt);
  11703. wc_ecc_free(&key);
  11704. if (wc_FreeRng(&rng) && ret == 0) {
  11705. ret = WOLFSSL_FATAL_ERROR;
  11706. }
  11707. #endif
  11708. return ret;
  11709. } /* END test_wc_ecc_pointFns */
  11710. /*
  11711. * Testing wc_ecc_sahred_secret_ssh()
  11712. */
  11713. static int test_wc_ecc_shared_secret_ssh (void)
  11714. {
  11715. int ret = 0;
  11716. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE)
  11717. ecc_key key, key2;
  11718. WC_RNG rng;
  11719. int keySz = KEY32;
  11720. int key2Sz = KEY24;
  11721. byte secret[keySz];
  11722. word32 secretLen = keySz;
  11723. /* Init stack variables. */
  11724. XMEMSET(secret, 0, secretLen);
  11725. /* Make keys */
  11726. ret = wc_InitRng(&rng);
  11727. if (ret == 0) {
  11728. ret = wc_ecc_init(&key);
  11729. if (ret == 0) {
  11730. ret = wc_ecc_make_key(&rng, keySz, &key);
  11731. }
  11732. if (wc_FreeRng(&rng) && ret == 0) {
  11733. ret = WOLFSSL_FATAL_ERROR;
  11734. }
  11735. }
  11736. if (ret == 0) {
  11737. ret = wc_InitRng(&rng);
  11738. if (ret == 0) {
  11739. ret = wc_ecc_init(&key2);
  11740. }
  11741. if (ret == 0) {
  11742. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  11743. }
  11744. }
  11745. printf(testingFmt, "ecc_shared_secret_ssh()");
  11746. if (ret == 0) {
  11747. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  11748. }
  11749. /* Pass in bad args. */
  11750. if (ret == 0) {
  11751. ret = wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret, &secretLen);
  11752. if (ret == BAD_FUNC_ARG) {
  11753. ret = wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen);
  11754. }
  11755. if (ret == BAD_FUNC_ARG) {
  11756. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen);
  11757. }
  11758. if (ret == BAD_FUNC_ARG) {
  11759. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL);
  11760. }
  11761. if (ret == BAD_FUNC_ARG) {
  11762. key.type = ECC_PUBLICKEY;
  11763. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  11764. if (ret == ECC_BAD_ARG_E) {
  11765. ret = 0;
  11766. } else if (ret == 0) {
  11767. ret = WOLFSSL_FATAL_ERROR;
  11768. }
  11769. } else if (ret == 0) {
  11770. ret = WOLFSSL_FATAL_ERROR;
  11771. }
  11772. }
  11773. printf(resultFmt, ret == 0 ? passed : failed);
  11774. if (wc_FreeRng(&rng) && ret == 0) {
  11775. ret = WOLFSSL_FATAL_ERROR;
  11776. }
  11777. wc_ecc_free(&key);
  11778. wc_ecc_free(&key2);
  11779. #endif
  11780. return ret;
  11781. } /* END test_wc_ecc_shared_secret_ssh */
  11782. /*
  11783. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  11784. */
  11785. static int test_wc_ecc_verify_hash_ex (void)
  11786. {
  11787. int ret = 0;
  11788. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP)
  11789. ecc_key key;
  11790. WC_RNG rng;
  11791. mp_int r;
  11792. mp_int s;
  11793. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  11794. unsigned char iHash[] = "Everyone gets Friday off.......";
  11795. unsigned char shortHash[] = "Everyone gets Friday off.";
  11796. word32 hashlen = sizeof(hash);
  11797. word32 iHashLen = sizeof(iHash);
  11798. word32 shortHashLen = sizeof(shortHash);
  11799. int keySz = KEY32;
  11800. int sig = WOLFSSL_FATAL_ERROR;
  11801. int ver = WOLFSSL_FATAL_ERROR;
  11802. int stat = 0;
  11803. /* Initialize r and s. */
  11804. ret = mp_init_multi(&r, &s, NULL, NULL, NULL, NULL);
  11805. if (ret != MP_OKAY) {
  11806. return MP_INIT_E;
  11807. }
  11808. ret = wc_InitRng(&rng);
  11809. if (ret == 0) {
  11810. ret = wc_ecc_init(&key);
  11811. if (ret == 0) {
  11812. ret = wc_ecc_make_key(&rng, keySz, &key);
  11813. }
  11814. }
  11815. if (ret == 0) {
  11816. ret = wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s);
  11817. if (ret == 0) {
  11818. /* stat should be 1. */
  11819. ret = wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &stat, &key);
  11820. if (stat != 1 && ret == 0) {
  11821. ret = WOLFSSL_FATAL_ERROR;
  11822. }
  11823. }
  11824. if (ret == 0) {
  11825. /* stat should be 0 */
  11826. ret = wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen,
  11827. &stat, &key);
  11828. if (stat != 0 && ret == 0) {
  11829. ret = WOLFSSL_FATAL_ERROR;
  11830. }
  11831. }
  11832. if (ret == 0) {
  11833. /* stat should be 0. */
  11834. ret = wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  11835. &stat, &key);
  11836. if (stat != 0 && ret == 0) {
  11837. ret = WOLFSSL_FATAL_ERROR;
  11838. }
  11839. }
  11840. }
  11841. printf(testingFmt, "wc_ecc_sign_hash_ex()");
  11842. /* Test bad args. */
  11843. if (ret == 0) {
  11844. if (wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s)
  11845. == ECC_BAD_ARG_E) {
  11846. sig = 0;
  11847. }
  11848. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s)
  11849. != ECC_BAD_ARG_E) {
  11850. sig = WOLFSSL_FATAL_ERROR;
  11851. }
  11852. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s)
  11853. != ECC_BAD_ARG_E) {
  11854. sig = WOLFSSL_FATAL_ERROR;
  11855. }
  11856. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s)
  11857. != ECC_BAD_ARG_E) {
  11858. sig = WOLFSSL_FATAL_ERROR;
  11859. }
  11860. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL)
  11861. != ECC_BAD_ARG_E) {
  11862. sig = WOLFSSL_FATAL_ERROR;
  11863. }
  11864. }
  11865. printf(resultFmt, sig == 0 ? passed : failed);
  11866. printf(testingFmt, "wc_ecc_verify_hash_ex()");
  11867. /* Test bad args. */
  11868. if (ret == 0) {
  11869. if (wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen, &stat, &key)
  11870. == ECC_BAD_ARG_E) {
  11871. ver = 0;
  11872. }
  11873. if (ver == 0 && wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  11874. &stat, &key) != ECC_BAD_ARG_E) {
  11875. ver = WOLFSSL_FATAL_ERROR;
  11876. }
  11877. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &stat,
  11878. &key) != ECC_BAD_ARG_E) {
  11879. ver = WOLFSSL_FATAL_ERROR;
  11880. }
  11881. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  11882. NULL, &key) != ECC_BAD_ARG_E) {
  11883. ver = WOLFSSL_FATAL_ERROR;
  11884. }
  11885. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  11886. &stat, NULL) != ECC_BAD_ARG_E) {
  11887. ver = WOLFSSL_FATAL_ERROR;
  11888. }
  11889. }
  11890. printf(resultFmt, ver == 0 ? passed : failed);
  11891. wc_ecc_free(&key);
  11892. mp_free(&r);
  11893. mp_free(&s);
  11894. if (wc_FreeRng(&rng)) {
  11895. return WOLFSSL_FATAL_ERROR;
  11896. }
  11897. if (ret == 0 && (sig != 0 || ver != 0)) {
  11898. ret = WOLFSSL_FATAL_ERROR;
  11899. }
  11900. #endif
  11901. return ret;
  11902. } /* END test_wc_ecc_verify_hash_ex */
  11903. /*
  11904. * Testing wc_ecc_mulmod()
  11905. */
  11906. static int test_wc_ecc_mulmod (void)
  11907. {
  11908. int ret = 0;
  11909. #if defined(HAVE_ECC) && \
  11910. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  11911. ecc_key key1, key2, key3;
  11912. WC_RNG rng;
  11913. ret = wc_InitRng(&rng);
  11914. if (ret == 0) {
  11915. if (ret == 0) {
  11916. ret = wc_ecc_init(&key1);
  11917. }
  11918. if (ret == 0) {
  11919. ret = wc_ecc_init(&key2);
  11920. }
  11921. if (ret == 0) {
  11922. ret = wc_ecc_init(&key3);
  11923. }
  11924. if (ret == 0) {
  11925. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  11926. }
  11927. }
  11928. if (ret == 0) {
  11929. ret = wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy, key1.dp->Af,
  11930. ECC_SECP256R1);
  11931. if (ret == 0) {
  11932. ret = wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  11933. key1.dp->prime, ECC_SECP256R1);
  11934. }
  11935. }
  11936. printf(testingFmt, "wc_ecc_mulmod()");
  11937. if (ret == 0) {
  11938. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey, &key2.k,
  11939. &key3.k, 1);
  11940. }
  11941. /* Test bad args. */
  11942. if (ret == 0) {
  11943. ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey, &key2.k,
  11944. &key3.k, 1);
  11945. if (ret == ECC_BAD_ARG_E) {
  11946. ret = wc_ecc_mulmod(&key1.k, NULL, &key3.pubkey, &key2.k,
  11947. &key3.k, 1);
  11948. }
  11949. if (ret == ECC_BAD_ARG_E) {
  11950. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, NULL, &key2.k,
  11951. &key3.k, 1);
  11952. }
  11953. if (ret == ECC_BAD_ARG_E) {
  11954. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey,
  11955. &key2.k, NULL, 1);
  11956. }
  11957. if (ret == ECC_BAD_ARG_E) {
  11958. ret = 0;
  11959. } else if (ret == 0) {
  11960. ret = WOLFSSL_FATAL_ERROR;
  11961. }
  11962. }
  11963. printf(resultFmt, ret == 0 ? passed : failed);
  11964. if (wc_FreeRng(&rng) && ret == 0) {
  11965. ret = WOLFSSL_FATAL_ERROR;
  11966. }
  11967. wc_ecc_free(&key1);
  11968. wc_ecc_free(&key2);
  11969. wc_ecc_free(&key3);
  11970. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  11971. return ret;
  11972. } /* END test_wc_ecc_mulmod */
  11973. /*
  11974. * Testing wc_ecc_is_valid_idx()
  11975. */
  11976. static int test_wc_ecc_is_valid_idx (void)
  11977. {
  11978. int ret = 0;
  11979. #if defined(HAVE_ECC)
  11980. ecc_key key;
  11981. WC_RNG rng;
  11982. int iVal = -2;
  11983. int iVal2 = 3000;
  11984. ret = wc_InitRng(&rng);
  11985. if (ret == 0) {
  11986. ret = wc_ecc_init(&key);
  11987. if (ret == 0) {
  11988. ret = wc_ecc_make_key(&rng, 32, &key);
  11989. }
  11990. }
  11991. printf(testingFmt, "wc_ecc_is_valid_idx()");
  11992. if (ret == 0) {
  11993. ret = wc_ecc_is_valid_idx(key.idx);
  11994. if (ret == 1) {
  11995. ret = 0;
  11996. } else {
  11997. ret = WOLFSSL_FATAL_ERROR;
  11998. }
  11999. }
  12000. /* Test bad args. */
  12001. if (ret == 0) {
  12002. ret = wc_ecc_is_valid_idx(iVal); /* should return 0 */
  12003. if (ret == 0) {
  12004. ret = wc_ecc_is_valid_idx(iVal2);
  12005. }
  12006. if (ret != 0) {
  12007. ret = WOLFSSL_FATAL_ERROR;
  12008. }
  12009. }
  12010. printf(resultFmt, ret == 0 ? passed : failed);
  12011. if (wc_FreeRng(&rng) && ret == 0) {
  12012. ret = WOLFSSL_FATAL_ERROR;
  12013. }
  12014. wc_ecc_free(&key);
  12015. #endif
  12016. return ret;
  12017. } /* END test_wc_ecc_is_valid_idx */
  12018. /*
  12019. * Testing wc_PKCS7_New()
  12020. */
  12021. static void test_wc_PKCS7_New (void)
  12022. {
  12023. #if defined(HAVE_PKCS7)
  12024. PKCS7* pkcs7;
  12025. void* heap = NULL;
  12026. printf(testingFmt, "wc_PKCS7_New()");
  12027. pkcs7 = wc_PKCS7_New(heap, devId);
  12028. AssertNotNull(pkcs7);
  12029. printf(resultFmt, passed);
  12030. wc_PKCS7_Free(pkcs7);
  12031. #endif
  12032. } /* END test-wc_PKCS7_New */
  12033. /*
  12034. * Testing wc_PKCS7_Init()
  12035. */
  12036. static void test_wc_PKCS7_Init (void)
  12037. {
  12038. #if defined(HAVE_PKCS7)
  12039. PKCS7 pkcs7;
  12040. void* heap = NULL;
  12041. printf(testingFmt, "wc_PKCS7_Init()");
  12042. AssertIntEQ(wc_PKCS7_Init(&pkcs7, heap, devId), 0);
  12043. /* Pass in bad args. */
  12044. AssertIntEQ(wc_PKCS7_Init(NULL, heap, devId), BAD_FUNC_ARG);
  12045. printf(resultFmt, passed);
  12046. wc_PKCS7_Free(&pkcs7);
  12047. #endif
  12048. } /* END test-wc_PKCS7_Init */
  12049. /*
  12050. * Testing wc_PKCS7_InitWithCert()
  12051. */
  12052. static void test_wc_PKCS7_InitWithCert (void)
  12053. {
  12054. #if defined(HAVE_PKCS7)
  12055. PKCS7 pkcs7;
  12056. #ifndef NO_RSA
  12057. #if defined(USE_CERT_BUFFERS_2048)
  12058. unsigned char cert[sizeof_client_cert_der_2048];
  12059. int certSz = (int)sizeof(cert);
  12060. XMEMSET(cert, 0, certSz);
  12061. XMEMCPY(cert, client_cert_der_2048, sizeof_client_cert_der_2048);
  12062. #elif defined(USE_CERT_BUFFERS_1024)
  12063. unsigned char cert[sizeof_client_cert_der_1024];
  12064. int certSz = (int)sizeof(cert);
  12065. XMEMSET(cert, 0, certSz);
  12066. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  12067. #else
  12068. unsigned char cert[ONEK_BUF];
  12069. FILE* fp;
  12070. int certSz;
  12071. fp = fopen("./certs/1024/client-cert.der", "rb");
  12072. AssertNotNull(fp);
  12073. certSz = fread(cert, 1, sizeof_client_cert_der_1024, fp);
  12074. fclose(fp);
  12075. #endif
  12076. #elif defined(HAVE_ECC)
  12077. #if defined(USE_CERT_BUFFERS_256)
  12078. unsigned char cert[sizeof_cliecc_cert_der_256];
  12079. int certSz = (int)sizeof(cert);
  12080. XMEMSET(cert, 0, certSz);
  12081. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  12082. #else
  12083. unsigned char cert[ONEK_BUF];
  12084. FILE* fp;
  12085. int certSz;
  12086. fp = fopen("./certs/client-ecc-cert.der", "rb");
  12087. AssertNotNull(fp);
  12088. certSz = fread(cert, 1, sizeof_cliecc_cert_der_256, fp);
  12089. fclose(fp);
  12090. #endif
  12091. #else
  12092. #error PKCS7 requires ECC or RSA
  12093. #endif
  12094. printf(testingFmt, "wc_PKCS7_InitWithCert()");
  12095. /* If initialization is not successful, it's free'd in init func. */
  12096. pkcs7.isDynamic = 0;
  12097. AssertIntEQ(wc_PKCS7_InitWithCert(&pkcs7, (byte*)cert, (word32)certSz), 0);
  12098. wc_PKCS7_Free(&pkcs7);
  12099. /* Valid initialization usage. */
  12100. AssertIntEQ(wc_PKCS7_InitWithCert(&pkcs7, NULL, 0), 0);
  12101. /* Pass in bad args. No need free for null checks, free at end.*/
  12102. AssertIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  12103. BAD_FUNC_ARG);
  12104. AssertIntEQ(wc_PKCS7_InitWithCert(&pkcs7, NULL, (word32)certSz),
  12105. BAD_FUNC_ARG);
  12106. printf(resultFmt, passed);
  12107. wc_PKCS7_Free(&pkcs7);
  12108. #endif
  12109. } /* END test_wc_PKCS7_InitWithCert */
  12110. /*
  12111. * Testing wc_PKCS7_EncodeData()
  12112. */
  12113. static void test_wc_PKCS7_EncodeData (void)
  12114. {
  12115. #if defined(HAVE_PKCS7)
  12116. PKCS7 pkcs7;
  12117. byte output[FOURK_BUF];
  12118. byte data[] = "My encoded DER cert.";
  12119. #ifndef NO_RSA
  12120. #if defined(USE_CERT_BUFFERS_2048)
  12121. unsigned char cert[sizeof_client_cert_der_2048];
  12122. unsigned char key[sizeof_client_key_der_2048];
  12123. int certSz = (int)sizeof(cert);
  12124. int keySz = (int)sizeof(key);
  12125. XMEMSET(cert, 0, certSz);
  12126. XMEMSET(key, 0, keySz);
  12127. XMEMCPY(cert, client_cert_der_2048, certSz);
  12128. XMEMCPY(key, client_key_der_2048, keySz);
  12129. #elif defined(USE_CERT_BUFFERS_1024)
  12130. unsigned char cert[sizeof_client_cert_der_1024];
  12131. unsigned char key[sizeof_client_key_der_1024];
  12132. int certSz = (int)sizeof(cert);
  12133. int keySz = (int)sizeof(key);
  12134. XMEMSET(cert, 0, certSz);
  12135. XMEMSET(key, 0, keySz);
  12136. XMEMCPY(cert, client_cert_der_1024, certSz);
  12137. XMEMCPY(key, client_key_der_1024, keySz);
  12138. #else
  12139. unsigned char cert[ONEK_BUF];
  12140. unsigned char key[ONEK_BUF];
  12141. FILE* fp;
  12142. int certSz;
  12143. int keySz;
  12144. fp = fopen("./certs/1024/client-cert.der", "rb");
  12145. AssertNotNull(fp);
  12146. certSz = fread(cert, 1, sizeof_client_cert_der_1024, fp);
  12147. fclose(fp);
  12148. fp = fopen("./certs/1024/client-key.der", "rb");
  12149. AssertNotNull(fp);
  12150. keySz = fread(key, 1, sizeof_client_key_der_1024, fp);
  12151. fclose(fp);
  12152. #endif
  12153. #elif defined(HAVE_ECC)
  12154. #if defined(USE_CERT_BUFFERS_256)
  12155. unsigned char cert[sizeof_cliecc_cert_der_256];
  12156. unsigned char key[sizeof_ecc_clikey_der_256];
  12157. int certSz = (int)sizeof(cert);
  12158. int keySz = (int)sizeof(key);
  12159. XMEMSET(cert, 0, certSz);
  12160. XMEMSET(key, 0, keySz);
  12161. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  12162. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  12163. #else
  12164. unsigned char cert[ONEK_BUF];
  12165. unsigned char key[ONEK_BUF];
  12166. FILE* fp;
  12167. int certSz, keySz;
  12168. fp = fopen("./certs/client-ecc-cert.der", "rb");
  12169. AssertNotNull(fp);
  12170. certSz = fread(cert, 1, sizeof_cliecc_cert_der_256, fp);
  12171. fclose(fp);
  12172. fp = fopen("./certs/client-ecc-key.der", "rb");
  12173. AssertNotNull(fp);
  12174. keySz = fread(key, 1, sizeof_ecc_clikey_der_256, fp);
  12175. fclose(fp);
  12176. #endif
  12177. #endif
  12178. XMEMSET(output, 0, sizeof(output));
  12179. AssertIntEQ(wc_PKCS7_Init(&pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  12180. AssertIntEQ(wc_PKCS7_InitWithCert(&pkcs7, (byte*)cert, certSz), 0);
  12181. printf(testingFmt, "wc_PKCS7_EncodeData()");
  12182. pkcs7.content = data;
  12183. pkcs7.contentSz = sizeof(data);
  12184. pkcs7.privateKey = key;
  12185. pkcs7.privateKeySz = keySz;
  12186. AssertIntGT(wc_PKCS7_EncodeData(&pkcs7, output, (word32)sizeof(output)), 0);
  12187. /* Test bad args. */
  12188. AssertIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  12189. BAD_FUNC_ARG);
  12190. AssertIntEQ(wc_PKCS7_EncodeData(&pkcs7, NULL, (word32)sizeof(output)),
  12191. BAD_FUNC_ARG);
  12192. AssertIntEQ(wc_PKCS7_EncodeData(&pkcs7, output, 5), BUFFER_E);
  12193. printf(resultFmt, passed);
  12194. wc_PKCS7_Free(&pkcs7);
  12195. #endif
  12196. } /* END test_wc_PKCS7_EncodeData */
  12197. /*
  12198. * Testing wc_PKCS7_EncodeSignedData()
  12199. */
  12200. static void test_wc_PKCS7_EncodeSignedData (void)
  12201. {
  12202. #if defined(HAVE_PKCS7)
  12203. PKCS7 pkcs7;
  12204. WC_RNG rng;
  12205. byte output[FOURK_BUF];
  12206. byte badOut[0];
  12207. word32 outputSz = (word32)sizeof(output);
  12208. word32 badOutSz = (word32)sizeof(badOut);
  12209. byte data[] = "Test data to encode.";
  12210. #ifndef NO_RSA
  12211. #if defined(USE_CERT_BUFFERS_2048)
  12212. byte key[sizeof_client_key_der_2048];
  12213. byte cert[sizeof_client_cert_der_2048];
  12214. word32 keySz = (word32)sizeof(key);
  12215. word32 certSz = (word32)sizeof(cert);
  12216. XMEMSET(key, 0, keySz);
  12217. XMEMSET(cert, 0, certSz);
  12218. XMEMCPY(key, client_key_der_2048, keySz);
  12219. XMEMCPY(cert, client_cert_der_2048, certSz);
  12220. #elif defined(USE_CERT_BUFFERS_1024)
  12221. byte key[sizeof_client_key_der_1024];
  12222. byte cert[sizeof_client_cert_der_1024];
  12223. word32 keySz = (word32)sizeof(key);
  12224. word32 certSz = (word32)sizeof(cert);
  12225. XMEMSET(key, 0, keySz);
  12226. XMEMSET(cert, 0, certSz);
  12227. XMEMCPY(key, client_key_der_1024, keySz);
  12228. XMEMCPY(cert, client_cert_der_1024, certSz);
  12229. #else
  12230. unsigned char cert[ONEK_BUF];
  12231. unsigned char key[ONEK_BUF];
  12232. FILE* fp;
  12233. int certSz;
  12234. int keySz;
  12235. fp = fopen("./certs/1024/client-cert.der", "rb");
  12236. AssertNotNull(fp);
  12237. certSz = fread(cert, 1, sizeof_client_cert_der_1024, fp);
  12238. fclose(fp);
  12239. fp = fopen("./certs/1024/client-key.der", "rb");
  12240. AssertNotNull(fp);
  12241. keySz = fread(key, 1, sizeof_client_key_der_1024, fp);
  12242. fclose(fp);
  12243. #endif
  12244. #elif defined(HAVE_ECC)
  12245. #if defined(USE_CERT_BUFFERS_256)
  12246. unsigned char cert[sizeof_cliecc_cert_der_256];
  12247. unsigned char key[sizeof_ecc_clikey_der_256];
  12248. int certSz = (int)sizeof(cert);
  12249. int keySz = (int)sizeof(key);
  12250. XMEMSET(cert, 0, certSz);
  12251. XMEMSET(key, 0, keySz);
  12252. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  12253. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  12254. #else
  12255. unsigned char cert[ONEK_BUF];
  12256. unsigned char key[ONEK_BUF];
  12257. FILE* fp;
  12258. int certSz, keySz;
  12259. fp = fopen("./certs/client-ecc-cert.der", "rb");
  12260. AssertNotNull(fp);
  12261. certSz = fread(cert, 1, sizeof_cliecc_cert_der_256, fp);
  12262. fclose(fp);
  12263. fp = fopen("./certs/client-ecc-key.der", "rb");
  12264. AssertNotNull(fp);
  12265. keySz = fread(key, 1, sizeof_ecc_clikey_der_256, fp);
  12266. fclose(fp);
  12267. #endif
  12268. #endif
  12269. XMEMSET(output, 0, outputSz);
  12270. AssertIntEQ(wc_InitRng(&rng), 0);
  12271. AssertIntEQ(wc_PKCS7_Init(&pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  12272. AssertIntEQ(wc_PKCS7_InitWithCert(&pkcs7, cert, certSz), 0);
  12273. printf(testingFmt, "wc_PKCS7_EncodeSignedData()");
  12274. pkcs7.content = data;
  12275. pkcs7.contentSz = (word32)sizeof(data);
  12276. pkcs7.privateKey = key;
  12277. pkcs7.privateKeySz = (word32)sizeof(key);
  12278. pkcs7.encryptOID = RSAk;
  12279. pkcs7.hashOID = SHAh;
  12280. pkcs7.rng = &rng;
  12281. AssertIntGT(wc_PKCS7_EncodeSignedData(&pkcs7, output, outputSz), 0);
  12282. wc_PKCS7_Free(&pkcs7);
  12283. AssertIntEQ(wc_PKCS7_InitWithCert(&pkcs7, NULL, 0), 0);
  12284. AssertIntEQ(wc_PKCS7_VerifySignedData(&pkcs7, output, outputSz), 0);
  12285. /* Pass in bad args. */
  12286. AssertIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  12287. AssertIntEQ(wc_PKCS7_EncodeSignedData(&pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  12288. AssertIntEQ(wc_PKCS7_EncodeSignedData(&pkcs7, badOut,
  12289. badOutSz), BAD_FUNC_ARG);
  12290. printf(resultFmt, passed);
  12291. wc_PKCS7_Free(&pkcs7);
  12292. wc_FreeRng(&rng);
  12293. #endif
  12294. } /* END test_wc_PKCS7_EncodeSignedData */
  12295. /*
  12296. * Testing wc_PKCS_VerifySignedData()
  12297. */
  12298. static void test_wc_PKCS7_VerifySignedData(void)
  12299. {
  12300. #if defined(HAVE_PKCS7)
  12301. PKCS7 pkcs7;
  12302. WC_RNG rng;
  12303. byte output[FOURK_BUF];
  12304. byte badOut[0];
  12305. word32 outputSz = (word32)sizeof(output);
  12306. word32 badOutSz = (word32)sizeof(badOut);
  12307. byte data[] = "Test data to encode.";
  12308. #ifndef NO_RSA
  12309. #if defined(USE_CERT_BUFFERS_2048)
  12310. byte key[sizeof_client_key_der_2048];
  12311. byte cert[sizeof_client_cert_der_2048];
  12312. word32 keySz = (word32)sizeof(key);
  12313. word32 certSz = (word32)sizeof(cert);
  12314. XMEMSET(key, 0, keySz);
  12315. XMEMSET(cert, 0, certSz);
  12316. XMEMCPY(key, client_key_der_2048, keySz);
  12317. XMEMCPY(cert, client_cert_der_2048, certSz);
  12318. #elif defined(USE_CERT_BUFFERS_1024)
  12319. byte key[sizeof_client_key_der_1024];
  12320. byte cert[sizeof_client_cert_der_1024];
  12321. word32 keySz = (word32)sizeof(key);
  12322. word32 certSz = (word32)sizeof(cert);
  12323. XMEMSET(key, 0, keySz);
  12324. XMEMSET(cert, 0, certSz);
  12325. XMEMCPY(key, client_key_der_1024, keySz);
  12326. XMEMCPY(cert, client_cert_der_1024, certSz);
  12327. #else
  12328. unsigned char cert[ONEK_BUF];
  12329. unsigned char key[ONEK_BUF];
  12330. FILE* fp;
  12331. int certSz;
  12332. int keySz;
  12333. fp = fopen("./certs/1024/client-cert.der", "rb");
  12334. AssertNotNull(fp);
  12335. certSz = fread(cert, 1, sizeof_client_cert_der_1024, fp);
  12336. fclose(fp);
  12337. fp = fopen("./certs/1024/client-key.der", "rb");
  12338. AssertNotNull(fp);
  12339. keySz = fread(key, 1, sizeof_client_key_der_1024, fp);
  12340. fclose(fp);
  12341. #endif
  12342. #elif defined(HAVE_ECC)
  12343. #if defined(USE_CERT_BUFFERS_256)
  12344. unsigned char cert[sizeof_cliecc_cert_der_256];
  12345. unsigned char key[sizeof_ecc_clikey_der_256];
  12346. int certSz = (int)sizeof(cert);
  12347. int keySz = (int)sizeof(key);
  12348. XMEMSET(cert, 0, certSz);
  12349. XMEMSET(key, 0, keySz);
  12350. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  12351. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  12352. #else
  12353. unsigned char cert[ONEK_BUF];
  12354. unsigned char key[ONEK_BUF];
  12355. FILE* fp;
  12356. int certSz, keySz;
  12357. fp = fopen("./certs/client-ecc-cert.der", "rb");
  12358. AssertNotNull(fp);
  12359. certSz = fread(cert, 1, sizeof_cliecc_cert_der_256, fp);
  12360. fclose(fp);
  12361. fp = fopen("./certs/client-ecc-key.der", "rb");
  12362. AssertNotNull(fp);
  12363. keySz = fread(key, 1, sizeof_ecc_clikey_der_256, fp);
  12364. fclose(fp);
  12365. #endif
  12366. #endif
  12367. XMEMSET(output, 0, outputSz);
  12368. AssertIntEQ(wc_InitRng(&rng), 0);
  12369. AssertIntEQ(wc_PKCS7_Init(&pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  12370. AssertIntEQ(wc_PKCS7_InitWithCert(&pkcs7, cert, certSz), 0);
  12371. printf(testingFmt, "wc_PKCS7_VerifySignedData()");
  12372. pkcs7.content = data;
  12373. pkcs7.contentSz = (word32)sizeof(data);
  12374. pkcs7.privateKey = key;
  12375. pkcs7.privateKeySz = (word32)sizeof(key);
  12376. pkcs7.encryptOID = RSAk;
  12377. pkcs7.hashOID = SHAh;
  12378. pkcs7.rng = &rng;
  12379. AssertIntGT(wc_PKCS7_EncodeSignedData(&pkcs7, output, outputSz), 0);
  12380. wc_PKCS7_Free(&pkcs7);
  12381. AssertIntEQ(wc_PKCS7_InitWithCert(&pkcs7, NULL, 0), 0);
  12382. AssertIntEQ(wc_PKCS7_VerifySignedData(&pkcs7, output, outputSz), 0);
  12383. /* Test bad args. */
  12384. AssertIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  12385. AssertIntEQ(wc_PKCS7_VerifySignedData(&pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  12386. AssertIntEQ(wc_PKCS7_VerifySignedData(&pkcs7, badOut,
  12387. badOutSz), BAD_FUNC_ARG);
  12388. printf(resultFmt, passed);
  12389. wc_PKCS7_Free(&pkcs7);
  12390. wc_FreeRng(&rng);
  12391. #endif
  12392. } /* END test_wc_PKCS7_VerifySignedData() */
  12393. /*
  12394. * Testing wc_PKCS7_EncodeEnvelopedData()
  12395. */
  12396. static void test_wc_PKCS7_EncodeDecodeEnvelopedData (void)
  12397. {
  12398. #if defined(HAVE_PKCS7)
  12399. PKCS7 pkcs7;
  12400. word32 tempWrd32 = 0;
  12401. byte* tmpBytePtr = NULL;
  12402. const char input[] = "Test data to encode.";
  12403. int i;
  12404. int testSz = 0;
  12405. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  12406. !defined(NO_SHA256) || !defined(NO_SHA512)))
  12407. byte* rsaCert = NULL;
  12408. byte* rsaPrivKey = NULL;
  12409. word32 rsaCertSz;
  12410. word32 rsaPrivKeySz;
  12411. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  12412. !defined(USE_CERT_BUFFERS_2048) )
  12413. static const char* rsaClientCert = "./certs/client-cert.der";
  12414. static const char* rsaClientKey = "./certs/client-key.der";
  12415. rsaCertSz = (word32)sizeof(rsaClientCert);
  12416. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  12417. #endif
  12418. #endif
  12419. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  12420. !defined(NO_SHA256) || !defined(NO_SHA512)))
  12421. byte* eccCert = NULL;
  12422. byte* eccPrivKey = NULL;
  12423. word32 eccCertSz;
  12424. word32 eccPrivKeySz;
  12425. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  12426. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  12427. static const char* eccClientKey = "./certs/ecc-client-key.der";
  12428. #endif
  12429. #endif
  12430. /* Generic buffer size. */
  12431. byte output[ONEK_BUF];
  12432. byte decoded[sizeof(input)/sizeof(char)];
  12433. int decodedSz = 0;
  12434. #ifndef NO_FILESYSTEM
  12435. FILE* certFile;
  12436. FILE* keyFile;
  12437. #endif
  12438. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  12439. !defined(NO_SHA256) || !defined(NO_SHA512)))
  12440. /* RSA certs and keys. */
  12441. #if defined(USE_CERT_BUFFERS_1024)
  12442. /* Allocate buffer space. */
  12443. AssertNotNull(rsaCert =
  12444. (byte*)XMALLOC(ONEK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  12445. /* Init buffer. */
  12446. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  12447. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  12448. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(ONEK_BUF, HEAP_HINT,
  12449. DYNAMIC_TYPE_TMP_BUFFER));
  12450. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  12451. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  12452. #elif defined(USE_CERT_BUFFERS_2048)
  12453. /* Allocate buffer */
  12454. AssertNotNull(rsaCert =
  12455. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  12456. /* Init buffer. */
  12457. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  12458. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  12459. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  12460. DYNAMIC_TYPE_TMP_BUFFER));
  12461. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  12462. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  12463. #else
  12464. /* File system. */
  12465. certFile = fopen(rsaClientCert, "rb");
  12466. AssertNotNull(certFile);
  12467. rsaCertSz = (word32)FOURK_BUF;
  12468. AssertNotNull(rsaCert =
  12469. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  12470. rsaCertSz = (word32)fread(rsaCert, 1, rsaCertSz, certFile);
  12471. fclose(certFile);
  12472. keyFile = fopen(rsaClientKey, "rb");
  12473. AssertNotNull(keyFile);
  12474. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  12475. DYNAMIC_TYPE_TMP_BUFFER));
  12476. rsaPrivKeySz = (word32)FOURK_BUF;
  12477. rsaPrivKeySz = (word32)fread(rsaPrivKey, 1, rsaPrivKeySz, keyFile);
  12478. fclose(keyFile);
  12479. #endif /* USE_CERT_BUFFERS */
  12480. #endif /* NO_RSA */
  12481. /* ECC */
  12482. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  12483. !defined(NO_SHA256) || !defined(NO_SHA512)))
  12484. #ifdef USE_CERT_BUFFERS_256
  12485. AssertNotNull(eccCert =
  12486. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  12487. /* Init buffer. */
  12488. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  12489. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  12490. AssertNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  12491. DYNAMIC_TYPE_TMP_BUFFER));
  12492. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  12493. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  12494. #else /* File system. */
  12495. certFile = fopen(eccClientCert, "rb");
  12496. AssertNotNull(certFile);
  12497. eccCertSz = (word32)FOURK_BUF;
  12498. AssertNotNull(eccCert =
  12499. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  12500. eccCertSz = (word32)fread(eccCert, 1, eccCertSz, certFile);
  12501. fclose(certFile);
  12502. keyFile = fopen(eccClientKey, "rb");
  12503. AssertNotNull(keyFile);
  12504. eccPrivKeySz = (word32)FOURK_BUF;
  12505. AssertNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  12506. DYNAMIC_TYPE_TMP_BUFFER));
  12507. eccPrivKeySz = (word32)fread(eccPrivKey, 1, eccPrivKeySz, keyFile);
  12508. fclose(keyFile);
  12509. #endif /* USE_CERT_BUFFERS_256 */
  12510. #endif /* END HAVE_ECC */
  12511. /* Silence. */
  12512. (void)keyFile;
  12513. (void)certFile;
  12514. const pkcs7EnvelopedVector testVectors[] = {
  12515. /* DATA is a global variable defined in the makefile. */
  12516. #if !defined(NO_RSA)
  12517. #ifndef NO_DES3
  12518. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  12519. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  12520. #endif /* NO_DES3 */
  12521. #ifndef NO_AES
  12522. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  12523. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  12524. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  12525. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  12526. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  12527. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  12528. #endif /* NO_AES */
  12529. #endif /* NO_RSA */
  12530. #if defined(HAVE_ECC)
  12531. #ifndef NO_AES
  12532. #ifndef NO_SHA
  12533. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  12534. AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme, eccCert,
  12535. eccCertSz, eccPrivKey, eccPrivKeySz},
  12536. #endif
  12537. #ifndef NO_SHA256
  12538. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  12539. AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme, eccCert,
  12540. eccCertSz, eccPrivKey, eccPrivKeySz},
  12541. #endif
  12542. #ifdef WOLFSSL_SHA512
  12543. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  12544. AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme, eccCert,
  12545. eccCertSz, eccPrivKey, eccPrivKeySz},
  12546. #endif
  12547. #endif /* NO_AES */
  12548. #endif /* END HAVE_ECC */
  12549. }; /* END pkcs7EnvelopedVector */
  12550. printf(testingFmt, "wc_PKCS7_EncodeEnvelopedData()");
  12551. AssertIntEQ(wc_PKCS7_Init(&pkcs7, HEAP_HINT, devId), 0);
  12552. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  12553. for (i = 0; i < testSz; i++) {
  12554. AssertIntEQ(wc_PKCS7_InitWithCert(&pkcs7, (testVectors + i)->cert,
  12555. (word32)(testVectors + i)->certSz), 0);
  12556. pkcs7.content = (byte*)(testVectors + i)->content;
  12557. pkcs7.contentSz = (testVectors + i)->contentSz;
  12558. pkcs7.contentOID = (testVectors + i)->contentOID;
  12559. pkcs7.encryptOID = (testVectors + i)->encryptOID;
  12560. pkcs7.keyWrapOID = (testVectors + i)->keyWrapOID;
  12561. pkcs7.keyAgreeOID = (testVectors + i)->keyAgreeOID;
  12562. pkcs7.privateKey = (testVectors + i)->privateKey;
  12563. pkcs7.privateKeySz = (testVectors + i)->privateKeySz;
  12564. AssertIntGE(wc_PKCS7_EncodeEnvelopedData(&pkcs7, output,
  12565. (word32)sizeof(output)), 0);
  12566. decodedSz = wc_PKCS7_DecodeEnvelopedData(&pkcs7, output,
  12567. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  12568. AssertIntGE(decodedSz, 0);
  12569. /* Verify the size of each buffer. */
  12570. AssertIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  12571. /* Don't free the last time through the loop. */
  12572. if (i < testSz - 1 ){
  12573. wc_PKCS7_Free(&pkcs7);
  12574. }
  12575. } /* END test loop. */
  12576. /* Test bad args. */
  12577. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  12578. (word32)sizeof(output)), BAD_FUNC_ARG);
  12579. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(&pkcs7, NULL,
  12580. (word32)sizeof(output)), BAD_FUNC_ARG);
  12581. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(&pkcs7, output, 0), BAD_FUNC_ARG);
  12582. printf(resultFmt, passed);
  12583. /* Decode. */
  12584. printf(testingFmt, "wc_PKCS7_DecodeEnvelopedData()");
  12585. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  12586. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  12587. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(&pkcs7, output,
  12588. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  12589. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(&pkcs7, output,
  12590. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  12591. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(&pkcs7, NULL,
  12592. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  12593. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(&pkcs7, output, 0, decoded,
  12594. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  12595. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  12596. tempWrd32 = pkcs7.singleCertSz;
  12597. pkcs7.singleCertSz = 0;
  12598. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(&pkcs7, output,
  12599. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  12600. pkcs7.singleCertSz = tempWrd32;
  12601. tempWrd32 = pkcs7.privateKeySz;
  12602. pkcs7.privateKeySz = 0;
  12603. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(&pkcs7, output,
  12604. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  12605. pkcs7.privateKeySz = tempWrd32;
  12606. tmpBytePtr = pkcs7.singleCert;
  12607. pkcs7.singleCert = NULL;
  12608. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(&pkcs7, output,
  12609. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  12610. pkcs7.singleCert = tmpBytePtr;
  12611. tmpBytePtr = pkcs7.privateKey;
  12612. pkcs7.privateKey = NULL;
  12613. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(&pkcs7, output,
  12614. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  12615. pkcs7.privateKey = tmpBytePtr;
  12616. printf(resultFmt, passed);
  12617. wc_PKCS7_Free(&pkcs7);
  12618. #ifndef NO_RSA
  12619. if (rsaCert) {
  12620. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12621. }
  12622. if (rsaPrivKey) {
  12623. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12624. }
  12625. #endif /*NO_RSA */
  12626. #ifdef HAVE_ECC
  12627. if (eccCert) {
  12628. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12629. }
  12630. if (eccPrivKey) {
  12631. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12632. }
  12633. #endif /* HAVE_ECC */
  12634. #endif /* HAVE_PKCS7 */
  12635. } /* END test_wc_PKCS7_EncodeEnvelopedData() */
  12636. /*
  12637. * Testing wc_PKCS7_EncodeEncryptedData()
  12638. */
  12639. static void test_wc_PKCS7_EncodeEncryptedData (void)
  12640. {
  12641. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  12642. PKCS7 pkcs7;
  12643. byte* tmpBytePtr = NULL;
  12644. byte encrypted[TWOK_BUF];
  12645. byte decoded[TWOK_BUF];
  12646. word32 tmpWrd32 = 0;
  12647. int tmpInt = 0;
  12648. int decodedSz;
  12649. int encryptedSz;
  12650. int testSz;
  12651. int i;
  12652. const byte data[] = { /* Hello World */
  12653. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  12654. 0x72,0x6c,0x64
  12655. };
  12656. #ifndef NO_DES3
  12657. byte desKey[] = {
  12658. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  12659. };
  12660. byte des3Key[] = {
  12661. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  12662. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  12663. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  12664. };
  12665. #endif
  12666. #ifndef NO_AES
  12667. byte aes128Key[] = {
  12668. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  12669. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  12670. };
  12671. byte aes192Key[] = {
  12672. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  12673. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  12674. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  12675. };
  12676. byte aes256Key[] = {
  12677. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  12678. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  12679. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  12680. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  12681. };
  12682. #endif
  12683. const pkcs7EncryptedVector testVectors[] =
  12684. {
  12685. #ifndef NO_DES3
  12686. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  12687. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  12688. #endif /* NO_DES3 */
  12689. #ifndef NO_AES
  12690. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  12691. sizeof(aes128Key)},
  12692. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  12693. sizeof(aes192Key)},
  12694. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  12695. sizeof(aes256Key)},
  12696. #endif /* NO_AES */
  12697. };
  12698. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  12699. for (i = 0; i < testSz; i++) {
  12700. AssertIntEQ(wc_PKCS7_Init(&pkcs7, HEAP_HINT, devId), 0);
  12701. pkcs7.content = (byte*)testVectors[i].content;
  12702. pkcs7.contentSz = testVectors[i].contentSz;
  12703. pkcs7.contentOID = testVectors[i].contentOID;
  12704. pkcs7.encryptOID = testVectors[i].encryptOID;
  12705. pkcs7.encryptionKey = testVectors[i].encryptionKey;
  12706. pkcs7.encryptionKeySz = testVectors[i].encryptionKeySz;
  12707. pkcs7.heap = HEAP_HINT;
  12708. /* encode encryptedData */
  12709. encryptedSz = wc_PKCS7_EncodeEncryptedData(&pkcs7, encrypted,
  12710. sizeof(encrypted));
  12711. AssertIntGT(encryptedSz, 0);
  12712. /* Decode encryptedData */
  12713. decodedSz = wc_PKCS7_DecodeEncryptedData(&pkcs7, encrypted, encryptedSz,
  12714. decoded, sizeof(decoded));
  12715. AssertIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  12716. /* Keep values for last itr. */
  12717. if (i < testSz - 1) {
  12718. wc_PKCS7_Free(&pkcs7);
  12719. }
  12720. }
  12721. printf(testingFmt, "wc_PKCS7_EncodeEncryptedData()");
  12722. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  12723. sizeof(encrypted)),BAD_FUNC_ARG);
  12724. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(&pkcs7, NULL,
  12725. sizeof(encrypted)), BAD_FUNC_ARG);
  12726. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(&pkcs7, encrypted,
  12727. 0), BAD_FUNC_ARG);
  12728. /* Testing the struct. */
  12729. tmpBytePtr = pkcs7.content;
  12730. pkcs7.content = NULL;
  12731. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(&pkcs7, encrypted,
  12732. sizeof(encrypted)), BAD_FUNC_ARG);
  12733. pkcs7.content = tmpBytePtr;
  12734. tmpWrd32 = pkcs7.contentSz;
  12735. pkcs7.contentSz = 0;
  12736. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(&pkcs7, encrypted,
  12737. sizeof(encrypted)), BAD_FUNC_ARG);
  12738. pkcs7.contentSz = tmpWrd32;
  12739. tmpInt = pkcs7.encryptOID;
  12740. pkcs7.encryptOID = 0;
  12741. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(&pkcs7, encrypted,
  12742. sizeof(encrypted)), BAD_FUNC_ARG);
  12743. pkcs7.encryptOID = tmpInt;
  12744. tmpBytePtr = pkcs7.encryptionKey;
  12745. pkcs7.encryptionKey = NULL;
  12746. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(&pkcs7, encrypted,
  12747. sizeof(encrypted)), BAD_FUNC_ARG);
  12748. pkcs7.encryptionKey = tmpBytePtr;
  12749. tmpWrd32 = pkcs7.encryptionKeySz;
  12750. pkcs7.encryptionKeySz = 0;
  12751. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(&pkcs7, encrypted,
  12752. sizeof(encrypted)), BAD_FUNC_ARG);
  12753. pkcs7.encryptionKeySz = tmpWrd32;
  12754. printf(resultFmt, passed);
  12755. printf(testingFmt, "wc_PKCS7_EncodeEncryptedData()");
  12756. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  12757. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  12758. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(&pkcs7, NULL, encryptedSz,
  12759. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  12760. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(&pkcs7, encrypted, 0,
  12761. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  12762. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(&pkcs7, encrypted, encryptedSz,
  12763. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  12764. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(&pkcs7, encrypted, encryptedSz,
  12765. decoded, 0), BAD_FUNC_ARG);
  12766. /* Test struct fields */
  12767. tmpBytePtr = pkcs7.encryptionKey;
  12768. pkcs7.encryptionKey = NULL;
  12769. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(&pkcs7, encrypted, encryptedSz,
  12770. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  12771. pkcs7.encryptionKey = tmpBytePtr;
  12772. pkcs7.encryptionKeySz = 0;
  12773. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(&pkcs7, encrypted, encryptedSz,
  12774. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  12775. printf(resultFmt, passed);
  12776. wc_PKCS7_Free(&pkcs7);
  12777. #endif
  12778. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  12779. /* Testing wc_SignatureGetSize() for signature type ECC */
  12780. static int test_wc_SignatureGetSize_ecc(void)
  12781. {
  12782. int ret = 0;
  12783. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  12784. enum wc_SignatureType sig_type;
  12785. word32 key_len;
  12786. /* Initialize ECC Key */
  12787. ecc_key ecc;
  12788. const char* qx =
  12789. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  12790. const char* qy =
  12791. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  12792. const char* d =
  12793. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  12794. ret = wc_ecc_init(&ecc);
  12795. if (ret == 0) {
  12796. ret = wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1");
  12797. }
  12798. printf(testingFmt, "wc_SigntureGetSize_ecc()");
  12799. if (ret == 0) {
  12800. /* Input for signature type ECC */
  12801. sig_type = WC_SIGNATURE_TYPE_ECC;
  12802. key_len = sizeof(ecc_key);
  12803. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  12804. /* Test bad args */
  12805. if (ret > 0) {
  12806. sig_type = (enum wc_SignatureType) 100;
  12807. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  12808. if (ret == BAD_FUNC_ARG) {
  12809. sig_type = WC_SIGNATURE_TYPE_ECC;
  12810. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  12811. }
  12812. if (ret >= 0) {
  12813. key_len = (word32) 0;
  12814. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  12815. }
  12816. if (ret == BAD_FUNC_ARG) {
  12817. ret = SIG_TYPE_E;
  12818. }
  12819. }
  12820. } else {
  12821. ret = WOLFSSL_FATAL_ERROR;
  12822. }
  12823. wc_ecc_free(&ecc);
  12824. #else
  12825. ret = SIG_TYPE_E;
  12826. #endif
  12827. if (ret == SIG_TYPE_E) {
  12828. ret = 0;
  12829. }
  12830. else {
  12831. ret = WOLFSSL_FATAL_ERROR;
  12832. }
  12833. printf(resultFmt, ret == 0 ? passed : failed);
  12834. return ret;
  12835. }/* END test_wc_SignatureGetSize_ecc() */
  12836. /* Testing wc_SignatureGetSize() for signature type rsa */
  12837. static int test_wc_SignatureGetSize_rsa(void)
  12838. {
  12839. int ret = 0;
  12840. #ifndef NO_RSA
  12841. enum wc_SignatureType sig_type;
  12842. word32 key_len;
  12843. word32 idx = 0;
  12844. /* Initialize RSA Key */
  12845. RsaKey rsa_key;
  12846. byte* tmp = NULL;
  12847. size_t bytes;
  12848. #ifdef USE_CERT_BUFFERS_1024
  12849. bytes = (size_t)sizeof_client_key_der_1024;
  12850. if (bytes < (size_t)sizeof_client_key_der_1024)
  12851. bytes = (size_t)sizeof_client_cert_der_1024;
  12852. #elif defined(USE_CERT_BUFFERS_2048)
  12853. bytes = (size_t)sizeof_client_key_der_2048;
  12854. if (bytes < (size_t)sizeof_client_cert_der_2048)
  12855. bytes = (size_t)sizeof_client_cert_der_2048;
  12856. #else
  12857. bytes = FOURK_BUF;
  12858. #endif
  12859. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12860. if (tmp != NULL) {
  12861. #ifdef USE_CERT_BUFFERS_1024
  12862. XMEMCPY(tmp, client_key_der_1024,
  12863. (size_t)sizeof_client_key_der_1024);
  12864. #elif defined(USE_CERT_BUFFERS_2048)
  12865. XMEMCPY(tmp, client_key_der_2048,
  12866. (size_t)sizeof_client_key_der_2048);
  12867. #elif !defined(NO_FILESYSTEM)
  12868. file = fopen(clientKey, "rb");
  12869. if (file != NULL) {
  12870. bytes = fread(tmp, 1, FOURK_BUF, file);
  12871. fclose(file);
  12872. }
  12873. else {
  12874. ret = WOLFSSL_FATAL_ERROR;
  12875. }
  12876. #else
  12877. ret = WOLFSSL_FATAL_ERROR;
  12878. #endif
  12879. if (ret == 0) {
  12880. ret = wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, devId);
  12881. if (ret == 0) {
  12882. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key,
  12883. (word32)bytes);
  12884. }
  12885. }
  12886. } else {
  12887. ret = WOLFSSL_FATAL_ERROR;
  12888. }
  12889. printf(testingFmt, "wc_SigntureGetSize_rsa()");
  12890. if (ret == 0) {
  12891. /* Input for signature type RSA */
  12892. sig_type = WC_SIGNATURE_TYPE_RSA;
  12893. key_len = sizeof(RsaKey);
  12894. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  12895. /* Test bad args */
  12896. if (ret > 0) {
  12897. sig_type = (enum wc_SignatureType) 100;
  12898. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  12899. if (ret == BAD_FUNC_ARG) {
  12900. sig_type = WC_SIGNATURE_TYPE_RSA;
  12901. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  12902. }
  12903. #ifndef HAVE_USER_RSA
  12904. if (ret == BAD_FUNC_ARG) {
  12905. #else
  12906. if (ret == 0) {
  12907. #endif
  12908. key_len = (word32)0;
  12909. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  12910. }
  12911. if (ret == BAD_FUNC_ARG) {
  12912. ret = SIG_TYPE_E;
  12913. }
  12914. }
  12915. } else {
  12916. ret = WOLFSSL_FATAL_ERROR;
  12917. }
  12918. wc_FreeRsaKey(&rsa_key);
  12919. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12920. #else
  12921. ret = SIG_TYPE_E;
  12922. #endif
  12923. if (ret == SIG_TYPE_E) {
  12924. ret = 0;
  12925. }else {
  12926. ret = WOLFSSL_FATAL_ERROR;
  12927. }
  12928. printf(resultFmt, ret == 0 ? passed : failed);
  12929. return ret;
  12930. }/* END test_wc_SignatureGetSize_rsa(void) */
  12931. /*----------------------------------------------------------------------------*
  12932. | hash.h Tests
  12933. *----------------------------------------------------------------------------*/
  12934. static int test_wc_HashInit(void)
  12935. {
  12936. int ret = 0, i; /* 0 indicates tests passed, 1 indicates failure */
  12937. wc_HashAlg hash;
  12938. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  12939. enum wc_HashType enumArray[] = {
  12940. #ifndef NO_MD5
  12941. WC_HASH_TYPE_MD5,
  12942. #endif
  12943. #ifndef NO_SHA
  12944. WC_HASH_TYPE_SHA,
  12945. #endif
  12946. #ifndef WOLFSSL_SHA224
  12947. WC_HASH_TYPE_SHA224,
  12948. #endif
  12949. #ifndef NO_SHA256
  12950. WC_HASH_TYPE_SHA256,
  12951. #endif
  12952. #ifndef WOLFSSL_SHA384
  12953. WC_HASH_TYPE_SHA384,
  12954. #endif
  12955. #ifndef WOLFSSL_SHA512
  12956. WC_HASH_TYPE_SHA512,
  12957. #endif
  12958. };
  12959. /* dynamically finds the length */
  12960. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  12961. /* For loop to test various arguments... */
  12962. for (i = 0; i < enumlen; i++) {
  12963. /* check for bad args */
  12964. if (wc_HashInit(&hash, enumArray[i]) == BAD_FUNC_ARG) {
  12965. ret = 1;
  12966. break;
  12967. }
  12968. /* check for null ptr */
  12969. if (wc_HashInit(NULL, enumArray[i]) != BAD_FUNC_ARG) {
  12970. ret = 1;
  12971. break;
  12972. }
  12973. } /* end of for loop */
  12974. printf(testingFmt, "wc_HashInit()");
  12975. if (ret==0) { /* all tests have passed */
  12976. printf(resultFmt, passed);
  12977. }
  12978. else { /* a test has failed */
  12979. printf(resultFmt, failed);
  12980. }
  12981. return ret;
  12982. } /* end of test_wc_HashInit */
  12983. /*----------------------------------------------------------------------------*
  12984. | Compatibility Tests
  12985. *----------------------------------------------------------------------------*/
  12986. static void test_wolfSSL_X509_NAME(void)
  12987. {
  12988. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  12989. && !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  12990. X509* x509;
  12991. const unsigned char* c;
  12992. unsigned char buf[4096];
  12993. int bytes;
  12994. FILE* f;
  12995. const X509_NAME* a;
  12996. const X509_NAME* b;
  12997. int sz;
  12998. unsigned char* tmp;
  12999. char file[] = "./certs/ca-cert.der";
  13000. printf(testingFmt, "wolfSSL_X509_NAME()");
  13001. /* test compile of depricated function, returns 0 */
  13002. AssertIntEQ(CRYPTO_thread_id(), 0);
  13003. AssertNotNull(a = X509_NAME_new());
  13004. X509_NAME_free((X509_NAME*)a);
  13005. f = fopen(file, "rb");
  13006. AssertNotNull(f);
  13007. bytes = (int)fread(buf, 1, sizeof(buf), f);
  13008. fclose(f);
  13009. c = buf;
  13010. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(c, bytes,
  13011. SSL_FILETYPE_ASN1));
  13012. /* test cmp function */
  13013. AssertNotNull(a = X509_get_issuer_name(x509));
  13014. AssertNotNull(b = X509_get_subject_name(x509));
  13015. AssertIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  13016. tmp = buf;
  13017. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  13018. if (tmp == buf) {
  13019. printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \
  13020. printf(" Expected pointer to be incremented\n");
  13021. abort();
  13022. }
  13023. /* retry but with the function creating a buffer */
  13024. tmp = NULL;
  13025. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  13026. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  13027. X509_free(x509);
  13028. printf(resultFmt, passed);
  13029. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  13030. }
  13031. static void test_wolfSSL_DES(void)
  13032. {
  13033. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  13034. const_DES_cblock myDes;
  13035. DES_cblock iv;
  13036. DES_key_schedule key;
  13037. word32 i;
  13038. DES_LONG dl;
  13039. unsigned char msg[] = "hello wolfssl";
  13040. printf(testingFmt, "wolfSSL_DES()");
  13041. DES_check_key(1);
  13042. DES_set_key(&myDes, &key);
  13043. /* check, check of odd parity */
  13044. XMEMSET(myDes, 4, sizeof(const_DES_cblock)); myDes[0] = 6; /*set even parity*/
  13045. XMEMSET(key, 5, sizeof(DES_key_schedule));
  13046. AssertIntEQ(DES_set_key_checked(&myDes, &key), -1);
  13047. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  13048. /* set odd parity for success case */
  13049. DES_set_odd_parity(&myDes);
  13050. printf("%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2], myDes[3]);
  13051. AssertIntEQ(DES_set_key_checked(&myDes, &key), 0);
  13052. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  13053. AssertIntEQ(key[i], myDes[i]);
  13054. }
  13055. AssertIntEQ(DES_is_weak_key(&myDes), 0);
  13056. /* check weak key */
  13057. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  13058. XMEMSET(key, 5, sizeof(DES_key_schedule));
  13059. AssertIntEQ(DES_set_key_checked(&myDes, &key), -2);
  13060. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  13061. /* now do unchecked copy of a weak key over */
  13062. DES_set_key_unchecked(&myDes, &key);
  13063. /* compare arrays, should be the same */
  13064. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  13065. AssertIntEQ(key[i], myDes[i]);
  13066. }
  13067. AssertIntEQ(DES_is_weak_key(&myDes), 1);
  13068. /* check DES_key_sched API */
  13069. XMEMSET(key, 1, sizeof(DES_key_schedule));
  13070. AssertIntEQ(DES_key_sched(&myDes, NULL), 0);
  13071. AssertIntEQ(DES_key_sched(NULL, &key), 0);
  13072. AssertIntEQ(DES_key_sched(&myDes, &key), 0);
  13073. /* compare arrays, should be the same */
  13074. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  13075. AssertIntEQ(key[i], myDes[i]);
  13076. }
  13077. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  13078. * DES_cbc_encrypt on the input */
  13079. XMEMSET(iv, 0, sizeof(DES_cblock));
  13080. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  13081. AssertIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  13082. AssertIntEQ(dl, 480052723);
  13083. printf(resultFmt, passed);
  13084. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  13085. }
  13086. static void test_wolfSSL_certs(void)
  13087. {
  13088. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13089. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  13090. X509* x509;
  13091. WOLFSSL* ssl;
  13092. WOLFSSL_CTX* ctx;
  13093. WOLF_STACK_OF(ASN1_OBJECT)* sk;
  13094. int crit;
  13095. printf(testingFmt, "wolfSSL_certs()");
  13096. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  13097. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  13098. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  13099. #ifndef HAVE_USER_RSA
  13100. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  13101. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  13102. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  13103. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  13104. #endif
  13105. AssertNotNull(ssl = SSL_new(ctx));
  13106. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  13107. #ifdef HAVE_PK_CALLBACKS
  13108. AssertIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  13109. #endif /* HAVE_PK_CALLBACKS */
  13110. /* create and use x509 */
  13111. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  13112. AssertNotNull(x509);
  13113. AssertIntEQ(SSL_use_certificate(ssl, x509), WOLFSSL_SUCCESS);
  13114. #ifndef HAVE_USER_RSA
  13115. /* with loading in a new cert the check on private key should now fail */
  13116. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  13117. #endif
  13118. #if defined(USE_CERT_BUFFERS_2048)
  13119. AssertIntEQ(SSL_use_certificate_ASN1(ssl,
  13120. (unsigned char*)server_cert_der_2048,
  13121. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  13122. #endif
  13123. #if !defined(NO_SHA) && !defined(NO_SHA256)
  13124. /************* Get Digest of Certificate ******************/
  13125. {
  13126. byte digest[64]; /* max digest size */
  13127. word32 digestSz;
  13128. XMEMSET(digest, 0, sizeof(digest));
  13129. AssertIntEQ(X509_digest(x509, wolfSSL_EVP_sha1(), digest, &digestSz),
  13130. WOLFSSL_SUCCESS);
  13131. AssertIntEQ(X509_digest(x509, wolfSSL_EVP_sha256(), digest, &digestSz),
  13132. WOLFSSL_SUCCESS);
  13133. AssertIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  13134. WOLFSSL_FAILURE);
  13135. }
  13136. #endif /* !NO_SHA && !NO_SHA256*/
  13137. /* test and checkout X509 extensions */
  13138. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_basic_constraints,
  13139. &crit, NULL);
  13140. AssertNotNull(sk);
  13141. AssertIntEQ(crit, 0);
  13142. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13143. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_key_usage,
  13144. &crit, NULL);
  13145. /* AssertNotNull(sk); NID not yet supported */
  13146. AssertIntEQ(crit, -1);
  13147. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13148. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_ext_key_usage,
  13149. &crit, NULL);
  13150. /* AssertNotNull(sk); no extension set */
  13151. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13152. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  13153. NID_authority_key_identifier, &crit, NULL);
  13154. AssertNotNull(sk);
  13155. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13156. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  13157. NID_private_key_usage_period, &crit, NULL);
  13158. /* AssertNotNull(sk); NID not yet supported */
  13159. AssertIntEQ(crit, -1);
  13160. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13161. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_subject_alt_name,
  13162. &crit, NULL);
  13163. /* AssertNotNull(sk); no alt names set */
  13164. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13165. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_issuer_alt_name,
  13166. &crit, NULL);
  13167. /* AssertNotNull(sk); NID not yet supported */
  13168. AssertIntEQ(crit, -1);
  13169. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13170. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_info_access, &crit,
  13171. NULL);
  13172. /* AssertNotNull(sk); no auth info set */
  13173. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13174. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_sinfo_access,
  13175. &crit, NULL);
  13176. /* AssertNotNull(sk); NID not yet supported */
  13177. AssertIntEQ(crit, -1);
  13178. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13179. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_name_constraints,
  13180. &crit, NULL);
  13181. /* AssertNotNull(sk); NID not yet supported */
  13182. AssertIntEQ(crit, -1);
  13183. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13184. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  13185. NID_certificate_policies, &crit, NULL);
  13186. #if !defined(WOLFSSL_SEP) && !defined(WOLFSSL_CERT_EXT)
  13187. AssertNull(sk);
  13188. #else
  13189. /* AssertNotNull(sk); no cert policy set */
  13190. #endif
  13191. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13192. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_policy_mappings,
  13193. &crit, NULL);
  13194. /* AssertNotNull(sk); NID not yet supported */
  13195. AssertIntEQ(crit, -1);
  13196. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13197. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_policy_constraints,
  13198. &crit, NULL);
  13199. /* AssertNotNull(sk); NID not yet supported */
  13200. AssertIntEQ(crit, -1);
  13201. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13202. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_inhibit_any_policy,
  13203. &crit, NULL);
  13204. /* AssertNotNull(sk); NID not yet supported */
  13205. AssertIntEQ(crit, -1);
  13206. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13207. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_tlsfeature, &crit,
  13208. NULL);
  13209. /* AssertNotNull(sk); NID not yet supported */
  13210. AssertIntEQ(crit, -1);
  13211. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13212. /* test invalid cases */
  13213. crit = 0;
  13214. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, -1, &crit, NULL);
  13215. AssertNull(sk);
  13216. AssertIntEQ(crit, -1);
  13217. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL, NID_tlsfeature,
  13218. NULL, NULL);
  13219. AssertNull(sk);
  13220. AssertIntEQ(SSL_get_hit(ssl), 0);
  13221. X509_free(x509);
  13222. SSL_free(ssl);
  13223. SSL_CTX_free(ctx);
  13224. printf(resultFmt, passed);
  13225. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  13226. }
  13227. static void test_wolfSSL_ASN1_TIME_print()
  13228. {
  13229. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) \
  13230. && (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  13231. defined(WOLFSSL_HAPROXY)) && defined(USE_CERT_BUFFERS_2048)
  13232. BIO* bio;
  13233. X509* x509;
  13234. const unsigned char* der = client_cert_der_2048;
  13235. ASN1_TIME* t;
  13236. unsigned char buf[25];
  13237. printf(testingFmt, "wolfSSL_ASN1_TIME_print()");
  13238. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  13239. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  13240. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  13241. AssertIntEQ(ASN1_TIME_print(bio, X509_get_notBefore(x509)), 1);
  13242. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  13243. AssertIntEQ(XMEMCMP(buf, "Apr 13 15:23:09 2018 GMT", sizeof(buf) - 1), 0);
  13244. /* create a bad time and test results */
  13245. AssertNotNull(t = X509_get_notAfter(x509));
  13246. t->data[10] = 0;
  13247. t->data[5] = 0;
  13248. AssertIntNE(ASN1_TIME_print(bio, t), 1);
  13249. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  13250. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  13251. BIO_free(bio);
  13252. X509_free(x509);
  13253. printf(resultFmt, passed);
  13254. #endif
  13255. }
  13256. static void test_wolfSSL_ASN1_GENERALIZEDTIME_free(){
  13257. #if defined(OPENSSL_EXTRA)
  13258. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime;
  13259. unsigned char nullstr[32];
  13260. XMEMSET(nullstr, 0, 32);
  13261. asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  13262. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL,
  13263. DYNAMIC_TYPE_TMP_BUFFER);
  13264. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  13265. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  13266. AssertIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  13267. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  13268. #endif /* OPENSSL_EXTRA */
  13269. }
  13270. static void test_wolfSSL_private_keys(void)
  13271. {
  13272. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13273. !defined(NO_FILESYSTEM)
  13274. WOLFSSL* ssl;
  13275. WOLFSSL_CTX* ctx;
  13276. EVP_PKEY* pkey = NULL;
  13277. printf(testingFmt, "wolfSSL_private_keys()");
  13278. OpenSSL_add_all_digests();
  13279. OpenSSL_add_all_algorithms();
  13280. #ifndef NO_RSA
  13281. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  13282. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  13283. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  13284. AssertNotNull(ssl = SSL_new(ctx));
  13285. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  13286. #ifdef USE_CERT_BUFFERS_2048
  13287. {
  13288. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  13289. unsigned char buf[FOURK_BUF];
  13290. word32 bufSz;
  13291. AssertIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  13292. (unsigned char*)client_key_der_2048,
  13293. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  13294. #ifndef HAVE_USER_RSA
  13295. /* Should missmatch now that a different private key loaded */
  13296. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  13297. #endif
  13298. AssertIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  13299. (unsigned char*)server_key,
  13300. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  13301. /* After loading back in DER format of original key, should match */
  13302. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  13303. /* pkey not set yet, expecting to fail */
  13304. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  13305. /* set PKEY and test again */
  13306. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  13307. &server_key, (long)sizeof_server_key_der_2048));
  13308. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  13309. /* reuse PKEY structure and test
  13310. * this should be checked with a memory management sanity checker */
  13311. AssertFalse(server_key == (const unsigned char*)server_key_der_2048);
  13312. server_key = (const unsigned char*)server_key_der_2048;
  13313. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  13314. &server_key, (long)sizeof_server_key_der_2048));
  13315. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  13316. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  13317. bufSz = FOURK_BUF;
  13318. AssertIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  13319. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  13320. RSAk, NULL, 0)), 0);
  13321. server_key = (const unsigned char*)buf;
  13322. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  13323. (long)bufSz));
  13324. }
  13325. #endif
  13326. EVP_PKEY_free(pkey);
  13327. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  13328. SSL_CTX_free(ctx);
  13329. #endif /* end of RSA private key match tests */
  13330. #ifdef HAVE_ECC
  13331. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  13332. AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  13333. WOLFSSL_FILETYPE_PEM));
  13334. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  13335. WOLFSSL_FILETYPE_PEM));
  13336. AssertNotNull(ssl = SSL_new(ctx));
  13337. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  13338. SSL_free(ssl);
  13339. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  13340. WOLFSSL_FILETYPE_PEM));
  13341. AssertNotNull(ssl = SSL_new(ctx));
  13342. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  13343. SSL_free(ssl);
  13344. SSL_CTX_free(ctx);
  13345. #endif /* end of ECC private key match tests */
  13346. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  13347. CONF_modules_free();
  13348. ENGINE_cleanup();
  13349. CONF_modules_unload();
  13350. (void)ssl;
  13351. (void)ctx;
  13352. (void)pkey;
  13353. printf(resultFmt, passed);
  13354. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  13355. }
  13356. static void test_wolfSSL_PEM_PrivateKey(void)
  13357. {
  13358. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13359. (!defined(NO_RSA) || defined(HAVE_ECC)) && \
  13360. defined(USE_CERT_BUFFERS_2048)
  13361. BIO* bio = NULL;
  13362. EVP_PKEY* pkey = NULL;
  13363. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  13364. /* test creating new EVP_PKEY with bad arg */
  13365. AssertNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  13366. /* test loading RSA key using BIO */
  13367. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  13368. {
  13369. XFILE file;
  13370. const char* fname = "./certs/server-key.pem";
  13371. size_t sz;
  13372. byte* buf;
  13373. file = XFOPEN(fname, "rb");
  13374. AssertTrue((file != XBADFILE));
  13375. XFSEEK(file, 0, XSEEK_END);
  13376. sz = XFTELL(file);
  13377. XREWIND(file);
  13378. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  13379. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  13380. XFCLOSE(file);
  13381. /* Test using BIO new mem and loading PEM private key */
  13382. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  13383. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  13384. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  13385. BIO_free(bio);
  13386. bio = NULL;
  13387. EVP_PKEY_free(pkey);
  13388. pkey = NULL;
  13389. }
  13390. #endif
  13391. /* test loading ECC key using BIO */
  13392. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  13393. {
  13394. XFILE file;
  13395. const char* fname = "./certs/ecc-key.pem";
  13396. size_t sz;
  13397. byte* buf;
  13398. file = XFOPEN(fname, "rb");
  13399. AssertTrue((file != XBADFILE));
  13400. XFSEEK(file, 0, XSEEK_END);
  13401. sz = XFTELL(file);
  13402. XREWIND(file);
  13403. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  13404. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  13405. XFCLOSE(file);
  13406. /* Test using BIO new mem and loading PEM private key */
  13407. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  13408. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  13409. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  13410. BIO_free(bio);
  13411. bio = NULL;
  13412. EVP_PKEY_free(pkey);
  13413. pkey = NULL;
  13414. }
  13415. #endif
  13416. #if !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN))
  13417. {
  13418. EVP_PKEY* pkey2 = NULL;
  13419. unsigned char extra[10];
  13420. int i;
  13421. printf(testingFmt, "wolfSSL_PEM_PrivateKey()");
  13422. XMEMSET(extra, 0, sizeof(extra));
  13423. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  13424. AssertIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  13425. AssertNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  13426. &server_key, (long)sizeof_server_key_der_2048));
  13427. AssertNull(pkey);
  13428. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  13429. &server_key, (long)sizeof_server_key_der_2048));
  13430. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  13431. WOLFSSL_SUCCESS);
  13432. /* test creating new EVP_PKEY with good args */
  13433. AssertNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  13434. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  13435. /* test of reuse of EVP_PKEY */
  13436. AssertNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  13437. AssertIntEQ(BIO_pending(bio), 0);
  13438. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  13439. SSL_SUCCESS);
  13440. AssertIntEQ(BIO_write(bio, extra, 10), 10); /*add 10 extra bytes after PEM*/
  13441. AssertNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  13442. AssertNotNull(pkey);
  13443. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  13444. AssertIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  13445. AssertIntEQ(BIO_read(bio, extra, 10), 10);
  13446. for (i = 0; i < 10; i++) {
  13447. AssertIntEQ(extra[i], 0);
  13448. }
  13449. BIO_free(bio);
  13450. bio = NULL;
  13451. EVP_PKEY_free(pkey);
  13452. pkey = NULL;
  13453. EVP_PKEY_free(pkey2);
  13454. }
  13455. #endif
  13456. /* key is DES encrypted */
  13457. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_FILESYSTEM)
  13458. {
  13459. pem_password_cb* passwd_cb;
  13460. void* passwd_cb_userdata;
  13461. SSL_CTX* ctx;
  13462. char passwd[] = "bad password";
  13463. #ifndef WOLFSSL_NO_TLS12
  13464. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  13465. #else
  13466. AssertNotNull(ctx = SSL_CTX_new(TLSv1_3_server_method()));
  13467. #endif
  13468. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  13469. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  13470. AssertNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  13471. AssertNull(passwd_cb_userdata =
  13472. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  13473. /* fail case with password call back */
  13474. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  13475. (void*)passwd));
  13476. BIO_free(bio);
  13477. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  13478. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  13479. (void*)passwd));
  13480. BIO_free(bio);
  13481. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  13482. /* use callback that works */
  13483. AssertNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  13484. (void*)"yassl123"));
  13485. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  13486. EVP_PKEY_free(pkey);
  13487. pkey = NULL;
  13488. BIO_free(bio);
  13489. bio = NULL;
  13490. SSL_CTX_free(ctx);
  13491. }
  13492. #endif /* !defined(NO_DES3) */
  13493. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  13494. {
  13495. unsigned char buf[2048];
  13496. size_t bytes;
  13497. XFILE f;
  13498. SSL_CTX* ctx;
  13499. #ifndef WOLFSSL_NO_TLS12
  13500. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  13501. #else
  13502. AssertNotNull(ctx = SSL_CTX_new(TLSv1_3_server_method()));
  13503. #endif
  13504. AssertNotNull(f = XFOPEN("./certs/ecc-key.der", "rb"));
  13505. bytes = XFREAD(buf, 1, sizeof(buf), f);
  13506. XFCLOSE(f);
  13507. server_key = buf;
  13508. pkey = NULL;
  13509. AssertNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  13510. AssertNull(pkey);
  13511. AssertNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  13512. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  13513. EVP_PKEY_free(pkey);
  13514. pkey = NULL;
  13515. SSL_CTX_free(ctx);
  13516. }
  13517. #endif
  13518. printf(resultFmt, passed);
  13519. (void)server_key;
  13520. (void)bio;
  13521. (void)pkey;
  13522. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  13523. }
  13524. static void test_wolfSSL_PEM_RSAPrivateKey(void)
  13525. {
  13526. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13527. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  13528. RSA* rsa = NULL;
  13529. BIO* bio = NULL;
  13530. printf(testingFmt, "wolfSSL_PEM_RSAPrivateKey()");
  13531. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  13532. AssertNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  13533. AssertIntEQ(RSA_size(rsa), 256);
  13534. BIO_free(bio);
  13535. RSA_free(rsa);
  13536. #ifdef HAVE_ECC
  13537. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  13538. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  13539. BIO_free(bio);
  13540. #endif /* HAVE_ECC */
  13541. printf(resultFmt, passed);
  13542. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  13543. }
  13544. static void test_wolfSSL_tmp_dh(void)
  13545. {
  13546. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13547. !defined(NO_FILESYSTEM) && !defined(NO_DSA) && !defined(NO_RSA) && \
  13548. !defined(NO_DH)
  13549. byte buffer[5300];
  13550. char file[] = "./certs/dsaparams.pem";
  13551. FILE *f;
  13552. int bytes;
  13553. DSA* dsa;
  13554. DH* dh;
  13555. BIO* bio;
  13556. SSL* ssl;
  13557. SSL_CTX* ctx;
  13558. printf(testingFmt, "wolfSSL_tmp_dh()");
  13559. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  13560. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  13561. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  13562. AssertNotNull(ssl = SSL_new(ctx));
  13563. f = fopen(file, "rb");
  13564. AssertNotNull(f);
  13565. bytes = (int)fread(buffer, 1, sizeof(buffer), f);
  13566. fclose(f);
  13567. bio = BIO_new_mem_buf((void*)buffer, bytes);
  13568. AssertNotNull(bio);
  13569. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  13570. AssertNotNull(dsa);
  13571. dh = wolfSSL_DSA_dup_DH(dsa);
  13572. AssertNotNull(dh);
  13573. AssertIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  13574. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  13575. BIO_free(bio);
  13576. DSA_free(dsa);
  13577. DH_free(dh);
  13578. SSL_free(ssl);
  13579. SSL_CTX_free(ctx);
  13580. printf(resultFmt, passed);
  13581. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  13582. }
  13583. static void test_wolfSSL_ctrl(void)
  13584. {
  13585. #if defined(OPENSSL_EXTRA)
  13586. byte buff[5300];
  13587. BIO* bio;
  13588. int bytes;
  13589. BUF_MEM* ptr = NULL;
  13590. printf(testingFmt, "wolfSSL_crtl()");
  13591. bytes = sizeof(buff);
  13592. bio = BIO_new_mem_buf((void*)buff, bytes);
  13593. AssertNotNull(bio);
  13594. AssertNotNull(BIO_s_socket());
  13595. AssertIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  13596. /* needs tested after stubs filled out @TODO
  13597. SSL_ctrl
  13598. SSL_CTX_ctrl
  13599. */
  13600. BIO_free(bio);
  13601. printf(resultFmt, passed);
  13602. #endif /* defined(OPENSSL_EXTRA) */
  13603. }
  13604. static void test_wolfSSL_EVP_PKEY_new_mac_key(void)
  13605. {
  13606. #ifdef OPENSSL_EXTRA
  13607. static const unsigned char pw[] = "password";
  13608. static const int pwSz = sizeof(pw) - 1;
  13609. size_t checkPwSz = 0;
  13610. const unsigned char* checkPw = NULL;
  13611. WOLFSSL_EVP_PKEY* key = NULL;
  13612. printf(testingFmt, "wolfSSL_EVP_PKEY_new_mac_key()");
  13613. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  13614. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  13615. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, pwSz));
  13616. AssertIntEQ(key->type, EVP_PKEY_HMAC);
  13617. AssertIntEQ(key->save_type, EVP_PKEY_HMAC);
  13618. AssertIntEQ(key->pkey_sz, pwSz);
  13619. AssertIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  13620. AssertNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  13621. AssertIntEQ((int)checkPwSz, pwSz);
  13622. AssertIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  13623. wolfSSL_EVP_PKEY_free(key);
  13624. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, 0));
  13625. AssertIntEQ(key->pkey_sz, 0);
  13626. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  13627. (void)checkPw;
  13628. AssertIntEQ((int)checkPwSz, 0);
  13629. wolfSSL_EVP_PKEY_free(key);
  13630. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL, 0));
  13631. AssertIntEQ(key->pkey_sz, 0);
  13632. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  13633. (void)checkPw;
  13634. AssertIntEQ((int)checkPwSz, 0);
  13635. wolfSSL_EVP_PKEY_free(key);
  13636. printf(resultFmt, passed);
  13637. #endif /* OPENSSL_EXTRA */
  13638. }
  13639. static void test_wolfSSL_EVP_MD_hmac_signing(void)
  13640. {
  13641. #ifdef OPENSSL_EXTRA
  13642. const unsigned char testKey[] =
  13643. {
  13644. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  13645. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  13646. 0x0b, 0x0b, 0x0b, 0x0b
  13647. };
  13648. const char testData[] = "Hi There";
  13649. const unsigned char testResult[] =
  13650. {
  13651. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  13652. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  13653. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  13654. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  13655. };
  13656. unsigned char check[sizeof(testResult)];
  13657. size_t checkSz = -1;
  13658. WOLFSSL_EVP_PKEY* key;
  13659. WOLFSSL_EVP_MD_CTX mdCtx;
  13660. printf(testingFmt, "wolfSSL_EVP_MD_hmac_signing()");
  13661. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  13662. testKey, (int)sizeof(testKey)));
  13663. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  13664. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  13665. NULL, key), 1);
  13666. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  13667. (unsigned int)XSTRLEN(testData)), 1);
  13668. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  13669. AssertIntEQ((int)checkSz, sizeof(testResult));
  13670. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  13671. AssertIntEQ((int)checkSz,(int)sizeof(testResult));
  13672. AssertIntEQ(XMEMCMP(testResult, check, sizeof(testResult)), 0);
  13673. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  13674. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  13675. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  13676. NULL, key), 1);
  13677. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  13678. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  13679. AssertIntEQ((int)checkSz, sizeof(testResult));
  13680. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  13681. AssertIntEQ((int)checkSz,(int)sizeof(testResult));
  13682. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  13683. (unsigned int)XSTRLEN(testData) - 4), 1);
  13684. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  13685. AssertIntEQ((int)checkSz,(int)sizeof(testResult));
  13686. AssertIntEQ(XMEMCMP(testResult, check, sizeof(testResult)), 0);
  13687. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  13688. wolfSSL_EVP_PKEY_free(key);
  13689. printf(resultFmt, passed);
  13690. #endif /* OPENSSL_EXTRA */
  13691. }
  13692. static void test_wolfSSL_CTX_add_extra_chain_cert(void)
  13693. {
  13694. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13695. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  13696. char caFile[] = "./certs/client-ca.pem";
  13697. char clientFile[] = "./certs/client-cert.pem";
  13698. SSL_CTX* ctx;
  13699. X509* x509 = NULL;
  13700. printf(testingFmt, "wolfSSL_CTX_add_extra_chain_cert()");
  13701. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  13702. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  13703. AssertNotNull(x509);
  13704. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  13705. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  13706. AssertNotNull(x509);
  13707. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  13708. /* additional test of getting EVP_PKEY key size from X509
  13709. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  13710. * allowed with user RSA */
  13711. {
  13712. EVP_PKEY* pkey;
  13713. #if defined(HAVE_ECC)
  13714. X509* ecX509;
  13715. #endif /* HAVE_ECC */
  13716. AssertNotNull(pkey = X509_get_pubkey(x509));
  13717. /* current RSA key is 2048 bit (256 bytes) */
  13718. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  13719. EVP_PKEY_free(pkey);
  13720. #if defined(HAVE_ECC)
  13721. #if defined(USE_CERT_BUFFERS_256)
  13722. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  13723. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  13724. SSL_FILETYPE_ASN1));
  13725. #else
  13726. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(cliEccCertFile,
  13727. SSL_FILETYPE_PEM));
  13728. #endif
  13729. AssertNotNull(pkey = X509_get_pubkey(ecX509));
  13730. /* current ECC key is 256 bit (32 bytes) */
  13731. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  13732. X509_free(ecX509);
  13733. EVP_PKEY_free(pkey);
  13734. #endif /* HAVE_ECC */
  13735. }
  13736. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  13737. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  13738. #ifdef WOLFSSL_ENCRYPTED_KEYS
  13739. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  13740. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  13741. #endif
  13742. SSL_CTX_free(ctx);
  13743. printf(resultFmt, passed);
  13744. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13745. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  13746. }
  13747. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  13748. static void test_wolfSSL_ERR_peek_last_error_line(void)
  13749. {
  13750. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13751. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  13752. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  13753. defined(HAVE_IO_TESTS_DEPENDENCIES)
  13754. tcp_ready ready;
  13755. func_args client_args;
  13756. func_args server_args;
  13757. #ifndef SINGLE_THREADED
  13758. THREAD_TYPE serverThread;
  13759. #endif
  13760. callback_functions client_cb;
  13761. callback_functions server_cb;
  13762. int line = 0;
  13763. int flag = ERR_TXT_STRING;
  13764. const char* file = NULL;
  13765. const char* data = NULL;
  13766. printf(testingFmt, "wolfSSL_ERR_peek_last_error_line()");
  13767. /* create a failed connection and inspect the error */
  13768. #ifdef WOLFSSL_TIRTOS
  13769. fdOpenSession(Task_self());
  13770. #endif
  13771. XMEMSET(&client_args, 0, sizeof(func_args));
  13772. XMEMSET(&server_args, 0, sizeof(func_args));
  13773. StartTCP();
  13774. InitTcpReady(&ready);
  13775. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  13776. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  13777. client_cb.method = wolfTLSv1_1_client_method;
  13778. server_cb.method = wolfTLSv1_2_server_method;
  13779. server_args.signal = &ready;
  13780. server_args.callbacks = &server_cb;
  13781. client_args.signal = &ready;
  13782. client_args.callbacks = &client_cb;
  13783. #ifndef SINGLE_THREADED
  13784. start_thread(test_server_nofail, &server_args, &serverThread);
  13785. wait_tcp_ready(&server_args);
  13786. test_client_nofail(&client_args, NULL);
  13787. join_thread(serverThread);
  13788. #endif
  13789. FreeTcpReady(&ready);
  13790. AssertIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  13791. AssertNotNull(data);
  13792. /* check clearing error state */
  13793. ERR_remove_state(0);
  13794. AssertIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  13795. ERR_peek_last_error_line(NULL, &line);
  13796. AssertIntEQ(line, 0);
  13797. ERR_peek_last_error_line(&file, NULL);
  13798. AssertNull(file);
  13799. /* retry connection to fill error queue */
  13800. XMEMSET(&client_args, 0, sizeof(func_args));
  13801. XMEMSET(&server_args, 0, sizeof(func_args));
  13802. StartTCP();
  13803. InitTcpReady(&ready);
  13804. client_cb.method = wolfTLSv1_1_client_method;
  13805. server_cb.method = wolfTLSv1_2_server_method;
  13806. server_args.signal = &ready;
  13807. server_args.callbacks = &server_cb;
  13808. client_args.signal = &ready;
  13809. client_args.callbacks = &client_cb;
  13810. start_thread(test_server_nofail, &server_args, &serverThread);
  13811. wait_tcp_ready(&server_args);
  13812. test_client_nofail(&client_args, NULL);
  13813. join_thread(serverThread);
  13814. FreeTcpReady(&ready);
  13815. /* check that error code was stored */
  13816. AssertIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  13817. ERR_peek_last_error_line(NULL, &line);
  13818. AssertIntNE(line, 0);
  13819. ERR_peek_last_error_line(&file, NULL);
  13820. AssertNotNull(file);
  13821. #ifdef WOLFSSL_TIRTOS
  13822. fdOpenSession(Task_self());
  13823. #endif
  13824. printf(resultFmt, passed);
  13825. printf("\nTesting error print out\n");
  13826. ERR_print_errors_fp(stdout);
  13827. printf("Done testing print out\n\n");
  13828. fflush(stdout);
  13829. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13830. !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  13831. }
  13832. #endif
  13833. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13834. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  13835. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  13836. {
  13837. (void) ok;
  13838. (void) ctx;
  13839. printf("ENTER verify_cb\n");
  13840. return SSL_SUCCESS;
  13841. }
  13842. #endif
  13843. static void test_wolfSSL_X509_STORE_CTX(void)
  13844. {
  13845. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13846. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  13847. X509_STORE_CTX* ctx;
  13848. X509_STORE* str;
  13849. X509* x509;
  13850. printf(testingFmt, "wolfSSL_X509_STORE_CTX()");
  13851. AssertNotNull(ctx = X509_STORE_CTX_new());
  13852. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  13853. AssertNotNull((x509 =
  13854. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  13855. AssertIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  13856. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  13857. AssertIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  13858. X509_STORE_CTX_set_error(ctx, -5);
  13859. X509_STORE_CTX_set_error(NULL, -5);
  13860. X509_STORE_CTX_free(ctx);
  13861. AssertNotNull(ctx = X509_STORE_CTX_new());
  13862. X509_STORE_CTX_set_verify_cb(ctx, (void *)verify_cb);
  13863. X509_STORE_CTX_free(ctx);
  13864. printf(resultFmt, passed);
  13865. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13866. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  13867. }
  13868. static void test_wolfSSL_X509_STORE_set_flags(void)
  13869. {
  13870. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13871. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  13872. X509_STORE* store;
  13873. X509* x509;
  13874. printf(testingFmt, "wolfSSL_X509_STORE_set_flags()");
  13875. AssertNotNull((store = wolfSSL_X509_STORE_new()));
  13876. AssertNotNull((x509 =
  13877. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM)));
  13878. AssertIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  13879. #ifdef HAVE_CRL
  13880. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL), WOLFSSL_SUCCESS);
  13881. #else
  13882. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  13883. NOT_COMPILED_IN);
  13884. #endif
  13885. wolfSSL_X509_free(x509);
  13886. wolfSSL_X509_STORE_free(store);
  13887. printf(resultFmt, passed);
  13888. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13889. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  13890. }
  13891. static void test_wolfSSL_X509_LOOKUP_load_file(void)
  13892. {
  13893. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  13894. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  13895. WOLFSSL_X509_STORE* store;
  13896. WOLFSSL_X509_LOOKUP* lookup;
  13897. printf(testingFmt, "wolfSSL_X509_LOOKUP_load_file()");
  13898. AssertNotNull(store = wolfSSL_X509_STORE_new());
  13899. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  13900. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  13901. X509_FILETYPE_PEM), 1);
  13902. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  13903. X509_FILETYPE_PEM), 1);
  13904. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  13905. WOLFSSL_FILETYPE_PEM), 1);
  13906. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  13907. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  13908. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  13909. X509_FILETYPE_PEM), 1);
  13910. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  13911. WOLFSSL_FILETYPE_PEM), 1);
  13912. wolfSSL_X509_STORE_free(store);
  13913. printf(resultFmt, passed);
  13914. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  13915. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  13916. }
  13917. static void test_wolfSSL_X509_STORE_CTX_set_time(void)
  13918. {
  13919. #if defined(OPENSSL_EXTRA)
  13920. WOLFSSL_X509_STORE_CTX* ctx;
  13921. time_t c_time;
  13922. printf(testingFmt, "wolfSSL_X509_set_time()");
  13923. AssertNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  13924. c_time = 365*24*60*60;
  13925. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  13926. AssertTrue(
  13927. (ctx->param->flags & WOLFSSL_USE_CHECK_TIME) == WOLFSSL_USE_CHECK_TIME);
  13928. AssertTrue(ctx->param->check_time == c_time);
  13929. wolfSSL_X509_STORE_CTX_free(ctx);
  13930. printf(resultFmt, passed);
  13931. #endif /* OPENSSL_EXTRA */
  13932. }
  13933. static void test_wolfSSL_CTX_set_client_CA_list(void)
  13934. {
  13935. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS)
  13936. WOLFSSL_CTX* ctx;
  13937. WOLF_STACK_OF(WOLFSSL_X509_NAME)* names = NULL;
  13938. WOLF_STACK_OF(WOLFSSL_X509_NAME)* ca_list = NULL;
  13939. printf(testingFmt, "wolfSSL_CTX_set_client_CA_list()");
  13940. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  13941. names = wolfSSL_load_client_CA_file(cliCertFile);
  13942. AssertNotNull(names);
  13943. wolfSSL_CTX_set_client_CA_list(ctx,names);
  13944. AssertNotNull(ca_list = wolfSSL_SSL_CTX_get_client_CA_list(ctx));
  13945. wolfSSL_CTX_free(ctx);
  13946. printf(resultFmt, passed);
  13947. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS */
  13948. }
  13949. static void test_wolfSSL_CTX_add_client_CA(void)
  13950. {
  13951. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS)
  13952. WOLFSSL_CTX* ctx;
  13953. WOLFSSL_X509* x509;
  13954. WOLFSSL_X509* x509_a;
  13955. WOLF_STACK_OF(WOLFSSLX509_NAME)* ca_list;
  13956. int ret = 0;
  13957. printf(testingFmt, "wolfSSL_CTX_add_client_CA()");
  13958. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  13959. /* Add client cert */
  13960. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  13961. SSL_FILETYPE_PEM));
  13962. ret = wolfSSL_CTX_add_client_CA(ctx, x509);
  13963. AssertIntEQ(ret ,SSL_SUCCESS);
  13964. AssertNotNull(ca_list = wolfSSL_SSL_CTX_get_client_CA_list(ctx));
  13965. /* Add another client cert */
  13966. AssertNotNull(x509_a = wolfSSL_X509_load_certificate_file(cliCertFile,
  13967. SSL_FILETYPE_PEM));
  13968. AssertIntEQ(wolfSSL_CTX_add_client_CA(ctx, x509_a),SSL_SUCCESS);
  13969. wolfSSL_X509_free(x509);
  13970. wolfSSL_X509_free(x509_a);
  13971. wolfSSL_CTX_free(ctx);
  13972. printf(resultFmt, passed);
  13973. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS */
  13974. }
  13975. static void test_wolfSSL_X509_NID(void)
  13976. {
  13977. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  13978. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  13979. int sigType;
  13980. int nameSz;
  13981. X509* cert;
  13982. EVP_PKEY* pubKeyTmp;
  13983. X509_NAME* name;
  13984. char commonName[80];
  13985. char countryName[80];
  13986. char localityName[80];
  13987. char stateName[80];
  13988. char orgName[80];
  13989. char orgUnit[80];
  13990. printf(testingFmt, "wolfSSL_X509_NID()");
  13991. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  13992. /* convert cert from DER to internal WOLFSSL_X509 struct */
  13993. AssertNotNull(cert = wolfSSL_X509_d2i(&cert, client_cert_der_2048,
  13994. sizeof_client_cert_der_2048));
  13995. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  13996. /* extract PUBLIC KEY from cert */
  13997. AssertNotNull(pubKeyTmp = X509_get_pubkey(cert));
  13998. /* extract signatureType */
  13999. AssertIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  14000. /* extract subjectName info */
  14001. AssertNotNull(name = X509_get_subject_name(cert));
  14002. AssertIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  14003. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, ASN_COMMON_NAME,
  14004. NULL, 0)), 0);
  14005. AssertIntEQ(nameSz, 15);
  14006. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, ASN_COMMON_NAME,
  14007. commonName, sizeof(commonName))), 0);
  14008. AssertIntEQ(nameSz, 15);
  14009. AssertIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  14010. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, ASN_COMMON_NAME,
  14011. commonName, 9)), 0);
  14012. AssertIntEQ(nameSz, 8);
  14013. AssertIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  14014. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, ASN_COUNTRY_NAME,
  14015. countryName, sizeof(countryName))), 0);
  14016. AssertIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  14017. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, ASN_LOCALITY_NAME,
  14018. localityName, sizeof(localityName))), 0);
  14019. AssertIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  14020. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, ASN_STATE_NAME,
  14021. stateName, sizeof(stateName))), 0);
  14022. AssertIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  14023. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, ASN_ORG_NAME,
  14024. orgName, sizeof(orgName))), 0);
  14025. AssertIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  14026. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, ASN_ORGUNIT_NAME,
  14027. orgUnit, sizeof(orgUnit))), 0);
  14028. AssertIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  14029. EVP_PKEY_free(pubKeyTmp);
  14030. X509_free(cert);
  14031. printf(resultFmt, passed);
  14032. #endif
  14033. }
  14034. static void test_wolfSSL_CTX_set_srp_username(void)
  14035. {
  14036. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  14037. && !defined(NO_SHA256) && !defined(WC_NO_RNG)
  14038. WOLFSSL_CTX* ctx;
  14039. const char *username = "TESTUSER";
  14040. const char *password = "TESTPASSWORD";
  14041. int r;
  14042. printf(testingFmt, "wolfSSL_CTX_set_srp_username()");
  14043. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  14044. AssertNotNull(ctx);
  14045. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  14046. AssertIntEQ(r,SSL_SUCCESS);
  14047. wolfSSL_CTX_free(ctx);
  14048. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  14049. AssertNotNull(ctx);
  14050. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  14051. AssertIntEQ(r,SSL_SUCCESS);
  14052. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  14053. AssertIntEQ(r,SSL_SUCCESS);
  14054. wolfSSL_CTX_free(ctx);
  14055. printf(resultFmt, passed);
  14056. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  14057. /* && !NO_SHA256 && !WC_NO_RNG */
  14058. }
  14059. static void test_wolfSSL_CTX_set_srp_password(void)
  14060. {
  14061. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  14062. && !defined(NO_SHA256) && !defined(WC_NO_RNG)
  14063. WOLFSSL_CTX* ctx;
  14064. const char *username = "TESTUSER";
  14065. const char *password = "TESTPASSWORD";
  14066. int r;
  14067. printf(testingFmt, "wolfSSL_CTX_set_srp_password()");
  14068. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  14069. AssertNotNull(ctx);
  14070. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  14071. AssertIntEQ(r,SSL_SUCCESS);
  14072. wolfSSL_CTX_free(ctx);
  14073. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  14074. AssertNotNull(ctx);
  14075. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  14076. AssertIntEQ(r,SSL_SUCCESS);
  14077. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  14078. AssertIntEQ(r,SSL_SUCCESS);
  14079. wolfSSL_CTX_free(ctx);
  14080. printf(resultFmt, passed);
  14081. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  14082. /* && !NO_SHA256 && !WC_NO_RNG */
  14083. }
  14084. static void test_wolfSSL_X509_STORE(void)
  14085. {
  14086. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  14087. X509_STORE *store;
  14088. X509_CRL *crl;
  14089. X509 *x509;
  14090. const char crl_pem[] = "./certs/crl/crl.pem";
  14091. const char svrCert[] = "./certs/server-cert.pem";
  14092. XFILE fp;
  14093. printf(testingFmt, "test_wolfSSL_X509_STORE");
  14094. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  14095. AssertNotNull((x509 =
  14096. wolfSSL_X509_load_certificate_file(svrCert, SSL_FILETYPE_PEM)));
  14097. AssertIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  14098. X509_free(x509);
  14099. AssertNotNull(fp = XFOPEN(crl_pem, "rb"));
  14100. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  14101. XFCLOSE(fp);
  14102. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  14103. X509_CRL_free(crl);
  14104. X509_STORE_free(store);
  14105. printf(resultFmt, passed);
  14106. #endif
  14107. return;
  14108. }
  14109. static void test_wolfSSL_BN(void)
  14110. {
  14111. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  14112. BIGNUM* a;
  14113. BIGNUM* b;
  14114. BIGNUM* c;
  14115. BIGNUM* d;
  14116. ASN1_INTEGER* ai;
  14117. unsigned char value[1];
  14118. printf(testingFmt, "wolfSSL_BN()");
  14119. AssertNotNull(b = BN_new());
  14120. AssertNotNull(c = BN_new());
  14121. AssertNotNull(d = BN_new());
  14122. value[0] = 0x03;
  14123. AssertNotNull(ai = ASN1_INTEGER_new());
  14124. /* at the moment hard setting since no set function */
  14125. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  14126. ai->data[1] = 0x01; /* length of integer */
  14127. ai->data[2] = value[0];
  14128. AssertNotNull(a = ASN1_INTEGER_to_BN(ai, NULL));
  14129. ASN1_INTEGER_free(ai);
  14130. value[0] = 0x02;
  14131. AssertNotNull(BN_bin2bn(value, sizeof(value), b));
  14132. value[0] = 0x05;
  14133. AssertNotNull(BN_bin2bn(value, sizeof(value), c));
  14134. /* a^b mod c = */
  14135. AssertIntEQ(BN_mod_exp(d, NULL, b, c, NULL), WOLFSSL_FAILURE);
  14136. AssertIntEQ(BN_mod_exp(d, a, b, c, NULL), WOLFSSL_SUCCESS);
  14137. /* check result 3^2 mod 5 */
  14138. value[0] = 0;
  14139. AssertIntEQ(BN_bn2bin(d, value), WOLFSSL_SUCCESS);
  14140. AssertIntEQ(BN_bn2bin(d, value), SSL_SUCCESS);
  14141. AssertIntEQ((int)(value[0]), 4);
  14142. /* a*b mod c = */
  14143. AssertIntEQ(BN_mod_mul(d, NULL, b, c, NULL), SSL_FAILURE);
  14144. AssertIntEQ(BN_mod_mul(d, a, b, c, NULL), SSL_SUCCESS);
  14145. /* check result 3*2 mod 5 */
  14146. value[0] = 0;
  14147. AssertIntEQ(BN_bn2bin(d, value), SSL_SUCCESS);
  14148. AssertIntEQ((int)(value[0]), 1);
  14149. /* BN_mod_inverse test */
  14150. value[0] = 0;
  14151. BIGNUM *r = BN_new();
  14152. BIGNUM *val = BN_mod_inverse(r,b,c,NULL);
  14153. AssertIntEQ(BN_bn2bin(r, value), 1);
  14154. AssertIntEQ((int)(value[0] & 0x03), 3);
  14155. BN_free(val);
  14156. AssertIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  14157. AssertIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  14158. AssertIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  14159. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  14160. {
  14161. char* ret;
  14162. AssertNotNull(ret = BN_bn2dec(c));
  14163. AssertIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  14164. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  14165. }
  14166. #endif
  14167. AssertIntEQ(BN_get_word(c), 4);
  14168. BN_free(a);
  14169. BN_free(b);
  14170. BN_free(c);
  14171. BN_clear_free(d);
  14172. /* check that converting NULL and the null string returns an error */
  14173. a = NULL;
  14174. AssertIntLE(BN_hex2bn(&a, NULL), 0);
  14175. AssertIntLE(BN_hex2bn(&a, ""), 0);
  14176. AssertNull(a);
  14177. /* check that getting a string and a bin of the same number are equal,
  14178. * and that the comparison works EQ, LT and GT */
  14179. AssertIntGT(BN_hex2bn(&a, "03"), 0);
  14180. value[0] = 0x03;
  14181. AssertNotNull(b = BN_new());
  14182. AssertNotNull(BN_bin2bn(value, sizeof(value), b));
  14183. value[0] = 0x04;
  14184. AssertNotNull(c = BN_new());
  14185. AssertNotNull(BN_bin2bn(value, sizeof(value), c));
  14186. AssertIntEQ(BN_cmp(a, b), 0);
  14187. AssertIntLT(BN_cmp(a, c), 0);
  14188. AssertIntGT(BN_cmp(c, b), 0);
  14189. BN_free(a);
  14190. BN_free(b);
  14191. BN_free(c);
  14192. printf(resultFmt, passed);
  14193. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  14194. }
  14195. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  14196. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  14197. #define TEST_ARG 0x1234
  14198. static void msg_cb(int write_p, int version, int content_type,
  14199. const void *buf, size_t len, SSL *ssl, void *arg)
  14200. {
  14201. (void)write_p;
  14202. (void)version;
  14203. (void)content_type;
  14204. (void)buf;
  14205. (void)len;
  14206. (void)ssl;
  14207. AssertTrue(arg == (void*)TEST_ARG);
  14208. }
  14209. #endif
  14210. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  14211. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  14212. defined(HAVE_IO_TESTS_DEPENDENCIES)
  14213. #ifndef SINGLE_THREADED
  14214. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  14215. {
  14216. (void) ctx;
  14217. (void) ssl;
  14218. printf("\n===== msgcb called ====\n");
  14219. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  14220. AssertTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  14221. AssertIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 1);
  14222. #endif
  14223. return SSL_SUCCESS;
  14224. }
  14225. #endif
  14226. #endif
  14227. static void test_wolfSSL_msgCb(void)
  14228. {
  14229. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  14230. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  14231. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  14232. !defined(NO_WOLFSSL_SERVER)
  14233. tcp_ready ready;
  14234. func_args client_args;
  14235. func_args server_args;
  14236. #ifndef SINGLE_THREADED
  14237. THREAD_TYPE serverThread;
  14238. #endif
  14239. callback_functions client_cb;
  14240. callback_functions server_cb;
  14241. printf(testingFmt, "test_wolfSSL_msgCb");
  14242. /* create a failed connection and inspect the error */
  14243. #ifdef WOLFSSL_TIRTOS
  14244. fdOpenSession(Task_self());
  14245. #endif
  14246. XMEMSET(&client_args, 0, sizeof(func_args));
  14247. XMEMSET(&server_args, 0, sizeof(func_args));
  14248. StartTCP();
  14249. InitTcpReady(&ready);
  14250. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  14251. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  14252. #ifndef WOLFSSL_NO_TLS12
  14253. client_cb.method = wolfTLSv1_2_client_method;
  14254. server_cb.method = wolfTLSv1_2_server_method;
  14255. #else
  14256. client_cb.method = wolfTLSv1_3_client_method;
  14257. server_cb.method = wolfTLSv1_3_server_method;
  14258. #endif
  14259. server_args.signal = &ready;
  14260. server_args.callbacks = &server_cb;
  14261. client_args.signal = &ready;
  14262. client_args.callbacks = &client_cb;
  14263. client_args.return_code = TEST_FAIL;
  14264. #ifndef SINGLE_THREADED
  14265. start_thread(test_server_nofail, &server_args, &serverThread);
  14266. wait_tcp_ready(&server_args);
  14267. test_client_nofail(&client_args, (void *)msgCb);
  14268. join_thread(serverThread);
  14269. AssertTrue(client_args.return_code);
  14270. AssertTrue(server_args.return_code);
  14271. #endif
  14272. FreeTcpReady(&ready);
  14273. #ifdef WOLFSSL_TIRTOS
  14274. fdOpenSession(Task_self());
  14275. #endif
  14276. printf(resultFmt, passed);
  14277. #endif
  14278. }
  14279. static void test_wolfSSL_set_options(void)
  14280. {
  14281. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  14282. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  14283. SSL* ssl;
  14284. SSL_CTX* ctx;
  14285. char appData[] = "extra msg";
  14286. unsigned char protos[] = {
  14287. 7, 't', 'l', 's', '/', '1', '.', '2',
  14288. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  14289. };
  14290. unsigned int len = sizeof(protos);
  14291. void *arg = (void *)TEST_ARG;
  14292. printf(testingFmt, "wolfSSL_set_options()");
  14293. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  14294. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  14295. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  14296. AssertTrue(SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1) == SSL_OP_NO_TLSv1);
  14297. AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  14298. AssertIntGT((int)SSL_CTX_set_options(ctx, (SSL_OP_COOKIE_EXCHANGE |
  14299. SSL_OP_NO_SSLv2)), 0);
  14300. AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_COOKIE_EXCHANGE) &
  14301. SSL_OP_COOKIE_EXCHANGE) == SSL_OP_COOKIE_EXCHANGE);
  14302. AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_2) &
  14303. SSL_OP_NO_TLSv1_2) == SSL_OP_NO_TLSv1_2);
  14304. AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_NO_COMPRESSION) &
  14305. SSL_OP_NO_COMPRESSION) == SSL_OP_NO_COMPRESSION);
  14306. AssertNull((SSL_CTX_clear_options(ctx, SSL_OP_NO_COMPRESSION) &
  14307. SSL_OP_NO_COMPRESSION));
  14308. SSL_CTX_free(ctx);
  14309. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  14310. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  14311. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  14312. AssertNotNull(ssl = SSL_new(ctx));
  14313. #if defined(HAVE_EX_DATA) || defined(FORTRESS)
  14314. AssertIntEQ(SSL_set_app_data(ssl, (void*)appData), SSL_SUCCESS);
  14315. AssertNotNull(SSL_get_app_data((const WOLFSSL*)ssl));
  14316. AssertIntEQ(XMEMCMP(SSL_get_app_data((const WOLFSSL*)ssl),
  14317. appData, sizeof(appData)), 0);
  14318. #else
  14319. AssertIntEQ(SSL_set_app_data(ssl, (void*)appData), SSL_FAILURE);
  14320. AssertNull(SSL_get_app_data((const WOLFSSL*)ssl));
  14321. #endif
  14322. AssertTrue(SSL_set_options(ssl, SSL_OP_NO_TLSv1) == SSL_OP_NO_TLSv1);
  14323. AssertTrue(SSL_get_options(ssl) == SSL_OP_NO_TLSv1);
  14324. AssertIntGT((int)SSL_set_options(ssl, (SSL_OP_COOKIE_EXCHANGE |
  14325. WOLFSSL_OP_NO_SSLv2)), 0);
  14326. AssertTrue((SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE) &
  14327. SSL_OP_COOKIE_EXCHANGE) == SSL_OP_COOKIE_EXCHANGE);
  14328. AssertTrue((SSL_set_options(ssl, SSL_OP_NO_TLSv1_2) &
  14329. SSL_OP_NO_TLSv1_2) == SSL_OP_NO_TLSv1_2);
  14330. AssertTrue((SSL_set_options(ssl, SSL_OP_NO_COMPRESSION) &
  14331. SSL_OP_NO_COMPRESSION) == SSL_OP_NO_COMPRESSION);
  14332. AssertNull((SSL_clear_options(ssl, SSL_OP_NO_COMPRESSION) &
  14333. SSL_OP_NO_COMPRESSION));
  14334. AssertTrue(SSL_set_msg_callback(ssl, msg_cb) == SSL_SUCCESS);
  14335. SSL_set_msg_callback_arg(ssl, arg);
  14336. AssertTrue(SSL_CTX_set_alpn_protos(ctx, protos, len) == SSL_SUCCESS);
  14337. SSL_free(ssl);
  14338. SSL_CTX_free(ctx);
  14339. printf(resultFmt, passed);
  14340. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  14341. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  14342. }
  14343. /* Testing wolfSSL_set_tlsext_status_type funciton.
  14344. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  14345. */
  14346. static void test_wolfSSL_set_tlsext_status_type(void){
  14347. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST)
  14348. SSL* ssl;
  14349. SSL_CTX* ctx;
  14350. printf(testingFmt, "wolfSSL_set_tlsext_status_type()");
  14351. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  14352. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  14353. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  14354. AssertNotNull(ssl = SSL_new(ctx));
  14355. AssertTrue(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp)
  14356. == SSL_SUCCESS);
  14357. SSL_free(ssl);
  14358. SSL_CTX_free(ctx);
  14359. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST */
  14360. }
  14361. static void test_wolfSSL_PEM_read_bio(void)
  14362. {
  14363. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  14364. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  14365. byte buff[5300];
  14366. FILE *f;
  14367. int bytes;
  14368. X509* x509;
  14369. BIO* bio = NULL;
  14370. BUF_MEM* buf;
  14371. printf(testingFmt, "wolfSSL_PEM_read_bio()");
  14372. AssertNotNull(f = fopen(cliCertFile, "rb"));
  14373. bytes = (int)fread(buff, 1, sizeof(buff), f);
  14374. fclose(f);
  14375. AssertNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  14376. AssertNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  14377. AssertNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  14378. AssertIntEQ((int)BIO_set_fd(bio, 0, BIO_NOCLOSE), 1);
  14379. AssertIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  14380. BIO_free(bio);
  14381. BUF_MEM_free(buf);
  14382. X509_free(x509);
  14383. printf(resultFmt, passed);
  14384. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  14385. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  14386. }
  14387. static void test_wolfSSL_BIO(void)
  14388. {
  14389. #if defined(OPENSSL_EXTRA)
  14390. byte buff[20];
  14391. BIO* bio1;
  14392. BIO* bio2;
  14393. BIO* bio3;
  14394. char* bufPt;
  14395. int i;
  14396. printf(testingFmt, "wolfSSL_BIO()");
  14397. for (i = 0; i < 20; i++) {
  14398. buff[i] = i;
  14399. }
  14400. /* Creating and testing type BIO_s_bio */
  14401. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  14402. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  14403. AssertNotNull(bio3 = BIO_new(BIO_s_bio()));
  14404. /* read/write before set up */
  14405. AssertIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  14406. AssertIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  14407. AssertIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  14408. AssertIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  14409. AssertIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  14410. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  14411. XMEMCPY(bufPt, buff, 10);
  14412. AssertIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  14413. /* write buffer full */
  14414. AssertIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  14415. AssertIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  14416. AssertIntEQ((int)BIO_ctrl_pending(bio1), 0);
  14417. /* write the other direction with pair */
  14418. AssertIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  14419. XMEMCPY(bufPt, buff, 8);
  14420. AssertIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  14421. /* try read */
  14422. AssertIntEQ((int)BIO_ctrl_pending(bio1), 8);
  14423. AssertIntEQ((int)BIO_ctrl_pending(bio2), 20);
  14424. AssertIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  14425. for (i = 0; i < 20; i++) {
  14426. AssertIntEQ((int)bufPt[i], i);
  14427. }
  14428. AssertIntEQ(BIO_nread(bio2, &bufPt, 1), WOLFSSL_BIO_ERROR);
  14429. AssertIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  14430. for (i = 0; i < 8; i++) {
  14431. AssertIntEQ((int)bufPt[i], i);
  14432. }
  14433. AssertIntEQ(BIO_nread(bio1, &bufPt, 1), WOLFSSL_BIO_ERROR);
  14434. AssertIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  14435. /* new pair */
  14436. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  14437. BIO_free(bio2); /* free bio2 and automaticly remove from pair */
  14438. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  14439. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  14440. AssertIntEQ(BIO_nread(bio3, &bufPt, 10), WOLFSSL_BIO_ERROR);
  14441. /* test wrap around... */
  14442. AssertIntEQ(BIO_reset(bio1), 0);
  14443. AssertIntEQ(BIO_reset(bio3), 0);
  14444. /* fill write buffer, read only small amount then write again */
  14445. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  14446. XMEMCPY(bufPt, buff, 20);
  14447. AssertIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  14448. for (i = 0; i < 4; i++) {
  14449. AssertIntEQ(bufPt[i], i);
  14450. }
  14451. /* try writing over read index */
  14452. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  14453. XMEMSET(bufPt, 0, 4);
  14454. AssertIntEQ((int)BIO_ctrl_pending(bio3), 20);
  14455. /* read and write 0 bytes */
  14456. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  14457. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  14458. /* should read only to end of write buffer then need to read again */
  14459. AssertIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  14460. for (i = 0; i < 16; i++) {
  14461. AssertIntEQ(bufPt[i], buff[4 + i]);
  14462. }
  14463. AssertIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  14464. AssertIntEQ(BIO_nread0(bio3, &bufPt), 4);
  14465. for (i = 0; i < 4; i++) {
  14466. AssertIntEQ(bufPt[i], 0);
  14467. }
  14468. /* read index should not have advanced with nread0 */
  14469. AssertIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  14470. for (i = 0; i < 4; i++) {
  14471. AssertIntEQ(bufPt[i], 0);
  14472. }
  14473. /* write and fill up buffer checking reset of index state */
  14474. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  14475. XMEMCPY(bufPt, buff, 20);
  14476. /* test reset on data in bio1 write buffer */
  14477. AssertIntEQ(BIO_reset(bio1), 0);
  14478. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  14479. AssertIntEQ(BIO_nread(bio3, &bufPt, 3), WOLFSSL_BIO_ERROR);
  14480. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  14481. XMEMCPY(bufPt, buff, 20);
  14482. AssertIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  14483. for (i = 0; i < 6; i++) {
  14484. AssertIntEQ(bufPt[i], i);
  14485. }
  14486. /* test case of writing twice with offset read index */
  14487. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  14488. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  14489. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  14490. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  14491. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  14492. AssertIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  14493. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  14494. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  14495. BIO_free(bio1);
  14496. BIO_free(bio3);
  14497. /* BIOs with file pointers */
  14498. #if !defined(NO_FILESYSTEM)
  14499. {
  14500. XFILE f1;
  14501. XFILE f2;
  14502. BIO* f_bio1;
  14503. BIO* f_bio2;
  14504. unsigned char cert[300];
  14505. char testFile[] = "tests/bio_write_test.txt";
  14506. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  14507. AssertNotNull(f_bio1 = BIO_new(BIO_s_file()));
  14508. AssertNotNull(f_bio2 = BIO_new(BIO_s_file()));
  14509. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  14510. AssertIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  14511. f1 = XFOPEN(svrCertFile, "rwb");
  14512. AssertIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  14513. AssertIntEQ(BIO_write_filename(f_bio2, testFile),
  14514. WOLFSSL_SUCCESS);
  14515. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  14516. AssertIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  14517. AssertIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  14518. AssertIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  14519. AssertIntEQ(BIO_reset(f_bio2), 0);
  14520. AssertIntEQ(BIO_seek(f_bio2, 4), 0);
  14521. BIO_free(f_bio1);
  14522. BIO_free(f_bio2);
  14523. AssertNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  14524. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  14525. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  14526. BIO_free(f_bio1);
  14527. }
  14528. #endif /* !defined(NO_FILESYSTEM) */
  14529. printf(resultFmt, passed);
  14530. #endif
  14531. }
  14532. static void test_wolfSSL_ASN1_STRING(void)
  14533. {
  14534. #if defined(OPENSSL_EXTRA)
  14535. ASN1_STRING* str = NULL;
  14536. const char data[] = "hello wolfSSL";
  14537. printf(testingFmt, "wolfSSL_ASN1_STRING()");
  14538. AssertNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  14539. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  14540. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  14541. AssertIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  14542. ASN1_STRING_free(str);
  14543. printf(resultFmt, passed);
  14544. #endif
  14545. }
  14546. static void test_wolfSSL_DES_ecb_encrypt(void)
  14547. {
  14548. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  14549. WOLFSSL_DES_cblock input1,input2,output1,output2,back1,back2;
  14550. WOLFSSL_DES_key_schedule key;
  14551. printf(testingFmt, "wolfSSL_DES_ecb_encrypt()");
  14552. XMEMCPY(key,"12345678",sizeof(WOLFSSL_DES_key_schedule));
  14553. XMEMCPY(input1, "Iamhuman",sizeof(WOLFSSL_DES_cblock));
  14554. XMEMCPY(input2, "Whoisit?",sizeof(WOLFSSL_DES_cblock));
  14555. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  14556. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  14557. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  14558. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  14559. /* Encrypt messages */
  14560. wolfSSL_DES_ecb_encrypt(&input1,&output1,&key,DES_ENCRYPT);
  14561. wolfSSL_DES_ecb_encrypt(&input2,&output2,&key,DES_ENCRYPT);
  14562. /* Decrypt messages */
  14563. int ret1 = 0;
  14564. int ret2 = 0;
  14565. wolfSSL_DES_ecb_encrypt(&output1,&back1,&key,DES_DECRYPT);
  14566. ret1 = XMEMCMP((unsigned char *) back1,(unsigned char *) input1,sizeof(WOLFSSL_DES_cblock));
  14567. AssertIntEQ(ret1,0);
  14568. wolfSSL_DES_ecb_encrypt(&output2,&back2,&key,DES_DECRYPT);
  14569. ret2 = XMEMCMP((unsigned char *) back2,(unsigned char *) input2,sizeof(WOLFSSL_DES_cblock));
  14570. AssertIntEQ(ret2,0);
  14571. printf(resultFmt, passed);
  14572. #endif
  14573. }
  14574. static void test_wolfSSL_ASN1_TIME_adj(void)
  14575. {
  14576. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN1_TIME) \
  14577. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  14578. const int year = 365*24*60*60;
  14579. const int day = 24*60*60;
  14580. const int hour = 60*60;
  14581. const int mini = 60;
  14582. const byte asn_utc_time = ASN_UTC_TIME;
  14583. #if !defined(TIME_T_NOT_LONG) && !defined(NO_64BIT)
  14584. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  14585. #endif
  14586. WOLFSSL_ASN1_TIME *asn_time, *s;
  14587. int offset_day;
  14588. long offset_sec;
  14589. char date_str[20];
  14590. time_t t;
  14591. printf(testingFmt, "wolfSSL_ASN1_TIME_adj()");
  14592. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  14593. DYNAMIC_TYPE_OPENSSL);
  14594. /* UTC notation test */
  14595. /* 2000/2/15 20:30:00 */
  14596. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  14597. offset_day = 7;
  14598. offset_sec = 45 * mini;
  14599. /* offset_sec = -45 * min;*/
  14600. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  14601. AssertTrue(asn_time->data[0] == asn_utc_time);
  14602. XSTRNCPY(date_str,(const char*) &asn_time->data+2,13);
  14603. AssertIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  14604. /* negative offset */
  14605. offset_sec = -45 * mini;
  14606. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  14607. AssertTrue(asn_time->data[0] == asn_utc_time);
  14608. XSTRNCPY(date_str,(const char*) &asn_time->data+2,13);
  14609. AssertIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  14610. XFREE(s,NULL,DYNAMIC_TYPE_OPENSSL);
  14611. XMEMSET(date_str, 0, sizeof(date_str));
  14612. /* Generalized time will overflow time_t if not long */
  14613. #if !defined(TIME_T_NOT_LONG) && !defined(NO_64BIT)
  14614. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  14615. DYNAMIC_TYPE_OPENSSL);
  14616. /* GeneralizedTime notation test */
  14617. /* 2055/03/01 09:00:00 */
  14618. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  14619. offset_day = 12;
  14620. offset_sec = 10 * mini;
  14621. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  14622. AssertTrue(asn_time->data[0] == asn_gen_time);
  14623. XSTRNCPY(date_str,(const char*) &asn_time->data+2, 15);
  14624. AssertIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  14625. XFREE(s,NULL,DYNAMIC_TYPE_OPENSSL);
  14626. XMEMSET(date_str, 0, sizeof(date_str));
  14627. #endif /* !TIME_T_NOT_LONG && !NO_64BIT */
  14628. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  14629. s = NULL;
  14630. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  14631. offset_day = 7;
  14632. offset_sec = 45 * mini;
  14633. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  14634. AssertTrue(asn_time->data[0] == asn_utc_time);
  14635. XSTRNCPY(date_str,(const char*) &asn_time->data+2,13);
  14636. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  14637. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  14638. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, offset_sec);
  14639. AssertTrue(asn_time->data[0] == asn_utc_time);
  14640. XSTRNCPY(date_str,(const char*) &asn_time->data+2,13);
  14641. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  14642. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  14643. printf(resultFmt, passed);
  14644. #endif
  14645. }
  14646. static void test_wolfSSL_X509(void)
  14647. {
  14648. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)\
  14649. && !defined(NO_RSA)
  14650. X509* x509;
  14651. BIO* bio;
  14652. X509_STORE_CTX* ctx;
  14653. X509_STORE* store;
  14654. char der[] = "certs/ca-cert.der";
  14655. XFILE fp;
  14656. printf(testingFmt, "wolfSSL_X509()");
  14657. AssertNotNull(x509 = X509_new());
  14658. X509_free(x509);
  14659. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  14660. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  14661. AssertIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  14662. AssertNotNull(ctx = X509_STORE_CTX_new());
  14663. AssertIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  14664. AssertNotNull(store = X509_STORE_new());
  14665. AssertIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  14666. AssertIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  14667. AssertIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  14668. X509_STORE_CTX_free(ctx);
  14669. BIO_free(bio);
  14670. /** d2i_X509_fp test **/
  14671. AssertNotNull(fp = XFOPEN(der, "rb"));
  14672. AssertNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  14673. AssertNotNull(x509);
  14674. X509_free(x509);
  14675. XFCLOSE(fp);
  14676. AssertNotNull(fp = XFOPEN(der, "rb"));
  14677. AssertNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  14678. AssertNotNull(x509);
  14679. X509_free(x509);
  14680. XFCLOSE(fp);
  14681. printf(resultFmt, passed);
  14682. #endif
  14683. }
  14684. static void test_wolfSSL_RAND(void)
  14685. {
  14686. #if defined(OPENSSL_EXTRA)
  14687. byte seed[16];
  14688. printf(testingFmt, "wolfSSL_RAND()");
  14689. RAND_seed(seed, sizeof(seed));
  14690. AssertIntEQ(RAND_poll(), 1);
  14691. RAND_cleanup();
  14692. AssertIntEQ(RAND_egd(NULL), -1);
  14693. #ifndef NO_FILESYSTEM
  14694. {
  14695. char fname[100];
  14696. AssertNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  14697. AssertIntEQ(RAND_write_file(NULL), 0);
  14698. }
  14699. #endif
  14700. printf(resultFmt, passed);
  14701. #endif
  14702. }
  14703. static void test_wolfSSL_BUF(void)
  14704. {
  14705. #if defined(OPENSSL_EXTRA)
  14706. BUF_MEM* buf;
  14707. AssertNotNull(buf = BUF_MEM_new());
  14708. AssertIntEQ(BUF_MEM_grow(buf, 10), 10);
  14709. AssertIntEQ(BUF_MEM_grow(buf, -1), 0);
  14710. BUF_MEM_free(buf);
  14711. #endif /* OPENSSL_EXTRA */
  14712. }
  14713. static void test_wolfSSL_pseudo_rand(void)
  14714. {
  14715. #if defined(OPENSSL_EXTRA)
  14716. BIGNUM* bn;
  14717. unsigned char bin[8];
  14718. int i;
  14719. printf(testingFmt, "wolfSSL_pseudo_rand()");
  14720. /* BN_pseudo_rand returns 1 on success 0 on failure
  14721. * int BN_pseudo_rand(BIGNUM* bn, int bits, int top, int bottom) */
  14722. for (i = 0; i < 10; i++) {
  14723. AssertNotNull(bn = BN_new());
  14724. AssertIntEQ(BN_pseudo_rand(bn, 8, 0, 0), SSL_SUCCESS);
  14725. AssertIntGT(BN_bn2bin(bn, bin),0);
  14726. AssertIntEQ((bin[0] & 0x80), 0x80); /* top bit should be set */
  14727. BN_free(bn);
  14728. }
  14729. for (i = 0; i < 10; i++) {
  14730. AssertNotNull(bn = BN_new());
  14731. AssertIntEQ(BN_pseudo_rand(bn, 8, 1, 1), SSL_SUCCESS);
  14732. AssertIntGT(BN_bn2bin(bn, bin),0);
  14733. AssertIntEQ((bin[0] & 0xc1), 0xc1); /* top bit should be set */
  14734. BN_free(bn);
  14735. }
  14736. printf(resultFmt, passed);
  14737. #endif
  14738. }
  14739. static void test_wolfSSL_PKCS8_Compat(void)
  14740. {
  14741. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  14742. PKCS8_PRIV_KEY_INFO* pt;
  14743. BIO* bio;
  14744. FILE* f;
  14745. int bytes;
  14746. char buffer[512];
  14747. printf(testingFmt, "wolfSSL_pkcs8()");
  14748. /* file from wolfssl/certs/ directory */
  14749. AssertNotNull(f = fopen("./certs/ecc-keyPkcs8.pem", "rb"));
  14750. AssertIntGT((bytes = (int)fread(buffer, 1, sizeof(buffer), f)), 0);
  14751. fclose(f);
  14752. AssertNotNull(bio = BIO_new_mem_buf((void*)buffer, bytes));
  14753. AssertNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  14754. BIO_free(bio);
  14755. PKCS8_PRIV_KEY_INFO_free(pt);
  14756. printf(resultFmt, passed);
  14757. #endif
  14758. }
  14759. static void test_wolfSSL_ERR_put_error(void)
  14760. {
  14761. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  14762. const char* file;
  14763. int line;
  14764. printf(testingFmt, "wolfSSL_ERR_put_error()");
  14765. ERR_clear_error(); /* clear out any error nodes */
  14766. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  14767. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  14768. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  14769. AssertIntEQ(ERR_get_error_line(&file, &line), 1);
  14770. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  14771. AssertIntEQ(ERR_get_error_line(&file, &line), 2);
  14772. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  14773. AssertIntEQ(ERR_get_error_line(&file, &line), 3);
  14774. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  14775. AssertIntEQ(ERR_get_error_line(&file, &line), 4);
  14776. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  14777. AssertIntEQ(ERR_get_error_line(&file, &line), 5);
  14778. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  14779. AssertIntEQ(ERR_get_error_line(&file, &line), 6);
  14780. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  14781. AssertIntEQ(ERR_get_error_line(&file, &line), 7);
  14782. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  14783. AssertIntEQ(ERR_get_error_line(&file, &line), 8);
  14784. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  14785. AssertIntEQ(ERR_get_error_line(&file, &line), 9);
  14786. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  14787. AssertIntEQ(ERR_get_error_line(&file, &line), 10);
  14788. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  14789. AssertIntEQ(ERR_get_error_line(&file, &line), 11);
  14790. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  14791. AssertIntEQ(ERR_get_error_line(&file, &line), 12);
  14792. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  14793. AssertIntEQ(ERR_get_error_line(&file, &line), 13);
  14794. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  14795. AssertIntEQ(ERR_get_error_line(&file, &line), 14);
  14796. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  14797. AssertIntEQ(ERR_get_error_line(&file, &line), 15);
  14798. /* try reading past end of error queue */
  14799. file = NULL;
  14800. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  14801. AssertNull(file);
  14802. AssertIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  14803. /* Empty and free up all error nodes */
  14804. ERR_clear_error();
  14805. printf(resultFmt, passed);
  14806. #endif
  14807. }
  14808. static void test_wolfSSL_HMAC(void)
  14809. {
  14810. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  14811. HMAC_CTX hmac;
  14812. ENGINE* e = NULL;
  14813. const unsigned char key[] = "simple test key";
  14814. unsigned char hash[WC_MAX_DIGEST_SIZE];
  14815. unsigned int len;
  14816. printf(testingFmt, "wolfSSL_HMAC()");
  14817. HMAC_CTX_init(&hmac);
  14818. AssertIntEQ(HMAC_Init_ex(&hmac, (void*)key, (int)sizeof(key),
  14819. EVP_sha256(), e), SSL_SUCCESS);
  14820. /* re-using test key as data to hash */
  14821. AssertIntEQ(HMAC_Update(&hmac, key, (int)sizeof(key)), SSL_SUCCESS);
  14822. AssertIntEQ(HMAC_Update(&hmac, NULL, 0), SSL_SUCCESS);
  14823. AssertIntEQ(HMAC_Final(&hmac, hash, &len), SSL_SUCCESS);
  14824. AssertIntEQ(len, (int)WC_SHA256_DIGEST_SIZE);
  14825. HMAC_cleanup(&hmac);
  14826. #endif
  14827. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  14828. len = 0;
  14829. AssertNotNull(HMAC(EVP_sha256(), key, (int)sizeof(key), NULL, 0, hash, &len));
  14830. AssertIntEQ(len, (int)WC_SHA256_DIGEST_SIZE);
  14831. #endif
  14832. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224)
  14833. len = 0;
  14834. AssertNotNull(HMAC(EVP_sha224(), key, (int)sizeof(key), NULL, 0, hash, &len));
  14835. AssertIntEQ(len, (int)WC_SHA224_DIGEST_SIZE);
  14836. #endif
  14837. #if defined(OPENSSL_EXTRA) && (defined(WOLFSSL_SHA384) && defined(WOLFSSL_SHA512))
  14838. len = 0;
  14839. AssertNotNull(HMAC(EVP_sha384(), key, (int)sizeof(key), NULL, 0, hash, &len));
  14840. AssertIntEQ(len, (int)WC_SHA384_DIGEST_SIZE);
  14841. #endif
  14842. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  14843. len = 0;
  14844. AssertNotNull(HMAC(EVP_sha512(), key, (int)sizeof(key), NULL, 0, hash, &len));
  14845. AssertIntEQ(len, (int)WC_SHA512_DIGEST_SIZE);
  14846. #endif
  14847. printf(resultFmt, passed);
  14848. }
  14849. static void test_wolfSSL_OBJ(void)
  14850. {
  14851. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  14852. ASN1_OBJECT* obj = NULL;
  14853. char buf[50];
  14854. printf(testingFmt, "wolfSSL_OBJ()");
  14855. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  14856. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  14857. AssertIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  14858. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  14859. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  14860. ASN1_OBJECT_free(obj);
  14861. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  14862. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256);
  14863. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  14864. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  14865. ASN1_OBJECT_free(obj);
  14866. printf(resultFmt, passed);
  14867. #endif
  14868. }
  14869. static void test_wolfSSL_X509_NAME_ENTRY(void)
  14870. {
  14871. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) \
  14872. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  14873. X509* x509;
  14874. BIO* bio;
  14875. X509_NAME* nm;
  14876. X509_NAME_ENTRY* entry;
  14877. unsigned char cn[] = "another name to add";
  14878. printf(testingFmt, "wolfSSL_X509_NAME_ENTRY()");
  14879. AssertNotNull(x509 =
  14880. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  14881. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  14882. AssertIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  14883. #ifdef WOLFSSL_CERT_REQ
  14884. {
  14885. X509_REQ* req;
  14886. BIO* bReq;
  14887. AssertNotNull(req =
  14888. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  14889. AssertNotNull(bReq = BIO_new(BIO_s_mem()));
  14890. AssertIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  14891. BIO_free(bReq);
  14892. X509_free(req);
  14893. }
  14894. #endif
  14895. AssertNotNull(nm = X509_get_subject_name(x509));
  14896. AssertNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  14897. 0x0c, cn, (int)sizeof(cn)));
  14898. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  14899. X509_NAME_ENTRY_free(entry);
  14900. BIO_free(bio);
  14901. X509_free(x509);
  14902. printf(resultFmt, passed);
  14903. #endif
  14904. }
  14905. static void test_wolfSSL_BIO_gets(void)
  14906. {
  14907. #if defined(OPENSSL_EXTRA)
  14908. BIO* bio;
  14909. BIO* bio2;
  14910. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  14911. char emp[] = "";
  14912. char buffer[20];
  14913. int bufferSz = 20;
  14914. printf(testingFmt, "wolfSSL_X509_BIO_gets()");
  14915. /* try with bad args */
  14916. AssertNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  14917. AssertNull(bio = BIO_new_mem_buf((void*)msg, -1));
  14918. /* try with real msg */
  14919. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, sizeof(msg)));
  14920. XMEMSET(buffer, 0, bufferSz);
  14921. AssertNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  14922. AssertNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  14923. AssertNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  14924. AssertFalse(bio2 != BIO_next(bio));
  14925. /* make buffer filled with no terminating characters */
  14926. XMEMSET(buffer, 1, bufferSz);
  14927. /* BIO_gets reads a line of data */
  14928. AssertIntEQ(BIO_gets(bio, buffer, -3), 0);
  14929. AssertIntEQ(BIO_gets(bio, buffer, bufferSz), 1);
  14930. AssertIntEQ(BIO_gets(bio, buffer, bufferSz), 14);
  14931. AssertStrEQ(buffer, "hello wolfSSL\n");
  14932. AssertIntEQ(BIO_gets(bio, buffer, bufferSz), 19);
  14933. AssertIntEQ(BIO_gets(bio, buffer, bufferSz), 8);
  14934. AssertIntEQ(BIO_gets(bio, buffer, -1), 0);
  14935. /* check not null terminated string */
  14936. BIO_free(bio);
  14937. msg[0] = 0x33;
  14938. msg[1] = 0x33;
  14939. msg[2] = 0x33;
  14940. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  14941. AssertIntEQ(BIO_gets(bio, buffer, 3), 2);
  14942. AssertIntEQ(buffer[0], msg[0]);
  14943. AssertIntEQ(buffer[1], msg[1]);
  14944. AssertIntNE(buffer[2], msg[2]);
  14945. BIO_free(bio);
  14946. msg[3] = 0x33;
  14947. buffer[3] = 0x33;
  14948. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  14949. AssertIntEQ(BIO_gets(bio, buffer, bufferSz), 3);
  14950. AssertIntEQ(buffer[0], msg[0]);
  14951. AssertIntEQ(buffer[1], msg[1]);
  14952. AssertIntEQ(buffer[2], msg[2]);
  14953. AssertIntNE(buffer[3], 0x33); /* make sure null terminator was set */
  14954. /* check reading an empty string */
  14955. BIO_free(bio);
  14956. AssertNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  14957. AssertIntEQ(BIO_gets(bio, buffer, bufferSz), 1); /* just terminator */
  14958. AssertStrEQ(emp, buffer);
  14959. /* check error cases */
  14960. BIO_free(bio);
  14961. AssertIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  14962. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  14963. AssertIntEQ(BIO_gets(bio, buffer, 2), -1); /* nothing to read */
  14964. #if !defined(NO_FILESYSTEM)
  14965. {
  14966. BIO* f_bio;
  14967. XFILE f;
  14968. AssertNotNull(f_bio = BIO_new(BIO_s_file()));
  14969. AssertIntLE(BIO_gets(f_bio, buffer, bufferSz), 0);
  14970. f = XFOPEN(svrCertFile, "rb");
  14971. AssertIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  14972. AssertIntGT(BIO_gets(f_bio, buffer, bufferSz), 0);
  14973. BIO_free(f_bio);
  14974. }
  14975. #endif /* NO_FILESYSTEM */
  14976. BIO_free(bio);
  14977. BIO_free(bio2);
  14978. /* try with type BIO */
  14979. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  14980. sizeof(msg));
  14981. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  14982. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  14983. AssertIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  14984. AssertIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  14985. AssertIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  14986. AssertIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  14987. AssertIntEQ(BIO_gets(bio, buffer, -3), 0);
  14988. AssertIntEQ(BIO_gets(bio, buffer, bufferSz), 1);
  14989. AssertIntEQ(BIO_gets(bio, buffer, bufferSz), 14);
  14990. AssertStrEQ(buffer, "hello wolfSSL\n");
  14991. AssertIntEQ(BIO_gets(bio, buffer, bufferSz), 19);
  14992. AssertIntEQ(BIO_gets(bio, buffer, bufferSz), 8);
  14993. AssertIntEQ(BIO_gets(bio, buffer, -1), 0);
  14994. BIO_free(bio);
  14995. BIO_free(bio2);
  14996. printf(resultFmt, passed);
  14997. #endif
  14998. }
  14999. static void test_wolfSSL_BIO_write(void)
  15000. {
  15001. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  15002. BIO* bio;
  15003. BIO* bio64;
  15004. BIO* ptr;
  15005. int sz;
  15006. char msg[] = "conversion test";
  15007. char out[40];
  15008. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  15009. printf(testingFmt, "wolfSSL_BIO_write()");
  15010. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  15011. AssertNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  15012. /* now should convert to base64 then write to memory */
  15013. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  15014. BIO_flush(bio);
  15015. AssertNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  15016. sz = sizeof(out);
  15017. XMEMSET(out, 0, sz);
  15018. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  15019. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  15020. /* write then read should return the same message */
  15021. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  15022. sz = sizeof(out);
  15023. XMEMSET(out, 0, sz);
  15024. AssertIntEQ(BIO_read(bio, out, sz), 16);
  15025. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  15026. /* now try encoding with no line ending */
  15027. BIO_set_flags(bio64, BIO_FLAG_BASE64_NO_NL);
  15028. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 24);
  15029. BIO_flush(bio);
  15030. sz = sizeof(out);
  15031. XMEMSET(out, 0, sz);
  15032. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  15033. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  15034. BIO_free_all(bio); /* frees bio64 also */
  15035. /* test with more than one bio64 in list */
  15036. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  15037. AssertNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  15038. AssertNotNull(BIO_push(bio64, BIO_new(BIO_s_mem())));
  15039. /* now should convert to base64(x2) when stored and then decode with read */
  15040. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 37);
  15041. BIO_flush(bio);
  15042. sz = sizeof(out);
  15043. XMEMSET(out, 0, sz);
  15044. AssertIntEQ((sz = BIO_read(bio, out, sz)), 16);
  15045. AssertIntEQ(XMEMCMP(out, msg, sz), 0);
  15046. BIO_free_all(bio); /* frees bio64s also */
  15047. printf(resultFmt, passed);
  15048. #endif
  15049. }
  15050. static void test_wolfSSL_SESSION(void)
  15051. {
  15052. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  15053. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  15054. defined(HAVE_IO_TESTS_DEPENDENCIES)
  15055. WOLFSSL* ssl;
  15056. WOLFSSL_CTX* ctx;
  15057. WOLFSSL_SESSION* sess;
  15058. const unsigned char context[] = "user app context";
  15059. unsigned char* sessDer = NULL;
  15060. unsigned char* ptr = NULL;
  15061. unsigned int contextSz = (unsigned int)sizeof(context);
  15062. int ret, err, sockfd, sz;
  15063. tcp_ready ready;
  15064. func_args server_args;
  15065. THREAD_TYPE serverThread;
  15066. printf(testingFmt, "wolfSSL_SESSION()");
  15067. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  15068. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  15069. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  15070. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  15071. #ifdef WOLFSSL_ENCRYPTED_KEYS
  15072. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  15073. #endif
  15074. XMEMSET(&server_args, 0, sizeof(func_args));
  15075. #ifdef WOLFSSL_TIRTOS
  15076. fdOpenSession(Task_self());
  15077. #endif
  15078. StartTCP();
  15079. InitTcpReady(&ready);
  15080. #if defined(USE_WINDOWS_API)
  15081. /* use RNG to get random port if using windows */
  15082. ready.port = GetRandomPort();
  15083. #endif
  15084. server_args.signal = &ready;
  15085. start_thread(test_server_nofail, &server_args, &serverThread);
  15086. wait_tcp_ready(&server_args);
  15087. /* client connection */
  15088. ssl = wolfSSL_new(ctx);
  15089. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  15090. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), SSL_SUCCESS);
  15091. err = 0; /* Reset error */
  15092. do {
  15093. #ifdef WOLFSSL_ASYNC_CRYPT
  15094. if (err == WC_PENDING_E) {
  15095. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  15096. if (ret < 0) { break; } else if (ret == 0) { continue; }
  15097. }
  15098. #endif
  15099. ret = wolfSSL_connect(ssl);
  15100. if (ret != SSL_SUCCESS) {
  15101. err = wolfSSL_get_error(ssl, 0);
  15102. }
  15103. } while (ret != SSL_SUCCESS && err == WC_PENDING_E);
  15104. AssertIntEQ(ret, SSL_SUCCESS);
  15105. sess = wolfSSL_get_session(ssl);
  15106. wolfSSL_shutdown(ssl);
  15107. wolfSSL_free(ssl);
  15108. join_thread(serverThread);
  15109. FreeTcpReady(&ready);
  15110. #ifdef WOLFSSL_TIRTOS
  15111. fdOpenSession(Task_self());
  15112. #endif
  15113. /* get session from DER and update the timeout */
  15114. AssertIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  15115. AssertIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  15116. wolfSSL_SESSION_free(sess);
  15117. ptr = sessDer;
  15118. AssertNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  15119. AssertNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  15120. (const unsigned char**)&ptr, sz));
  15121. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  15122. AssertIntGT(wolfSSL_SESSION_get_time(sess), 0);
  15123. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  15124. /* successful set session test */
  15125. AssertNotNull(ssl = wolfSSL_new(ctx));
  15126. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_SUCCESS);
  15127. /* fail case with miss match session context IDs (use compatibility API) */
  15128. AssertIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  15129. SSL_SUCCESS);
  15130. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  15131. wolfSSL_free(ssl);
  15132. AssertIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  15133. SSL_FAILURE);
  15134. AssertIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  15135. SSL_SUCCESS);
  15136. AssertNotNull(ssl = wolfSSL_new(ctx));
  15137. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  15138. wolfSSL_free(ssl);
  15139. SSL_SESSION_free(sess);
  15140. wolfSSL_CTX_free(ctx);
  15141. printf(resultFmt, passed);
  15142. #endif
  15143. }
  15144. static void test_wolfSSL_d2i_PUBKEY(void)
  15145. {
  15146. #if defined(OPENSSL_EXTRA)
  15147. BIO* bio;
  15148. EVP_PKEY* pkey;
  15149. printf(testingFmt, "wolfSSL_d2i_PUBKEY()");
  15150. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  15151. AssertNull(d2i_PUBKEY_bio(NULL, NULL));
  15152. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  15153. /* RSA PUBKEY test */
  15154. AssertIntGT(BIO_write(bio, client_keypub_der_2048,
  15155. sizeof_client_keypub_der_2048), 0);
  15156. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  15157. EVP_PKEY_free(pkey);
  15158. #endif
  15159. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  15160. /* ECC PUBKEY test */
  15161. AssertIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  15162. sizeof_ecc_clikeypub_der_256), 0);
  15163. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  15164. EVP_PKEY_free(pkey);
  15165. #endif
  15166. BIO_free(bio);
  15167. (void)pkey;
  15168. printf(resultFmt, passed);
  15169. #endif
  15170. }
  15171. static void test_wolfSSL_sk_GENERAL_NAME(void)
  15172. {
  15173. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  15174. !defined(NO_RSA)
  15175. X509* x509;
  15176. unsigned char buf[4096];
  15177. const unsigned char* bufPt;
  15178. int bytes;
  15179. XFILE f;
  15180. STACK_OF(GENERAL_NAME)* sk;
  15181. printf(testingFmt, "wolfSSL_sk_GENERAL_NAME()");
  15182. AssertNotNull(f = XFOPEN(cliCertDerFile, "rb"));
  15183. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  15184. XFCLOSE(f);
  15185. bufPt = buf;
  15186. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  15187. /* current cert has no alt names */
  15188. AssertNull(sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  15189. NID_subject_alt_name, NULL, NULL));
  15190. AssertIntEQ(sk_GENERAL_NAME_num(sk), -1);
  15191. #if 0
  15192. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  15193. GENERAL_NAME* gn = sk_GENERAL_NAME_value(sk, i);
  15194. if (gn == NULL) {
  15195. printf("massive falure\n");
  15196. return -1;
  15197. }
  15198. if (gn->type == GEN_DNS) {
  15199. printf("found type GEN_DNS\n");
  15200. printf("length = %d\n", gn->d.ia5->length);
  15201. printf("data = %s\n", (char*)gn->d.ia5->data);
  15202. }
  15203. if (gn->type == GEN_EMAIL) {
  15204. printf("found type GEN_EMAIL\n");
  15205. printf("length = %d\n", gn->d.ia5->length);
  15206. printf("data = %s\n", (char*)gn->d.ia5->data);
  15207. }
  15208. if (gn->type == GEN_URI) {
  15209. printf("found type GEN_URI\n");
  15210. printf("length = %d\n", gn->d.ia5->length);
  15211. printf("data = %s\n", (char*)gn->d.ia5->data);
  15212. }
  15213. }
  15214. #endif
  15215. X509_free(x509);
  15216. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  15217. printf(resultFmt, passed);
  15218. #endif
  15219. }
  15220. static void test_wolfSSL_MD4(void)
  15221. {
  15222. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  15223. MD4_CTX md4;
  15224. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  15225. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  15226. "789012345678901234567890";
  15227. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  15228. "\xcc\x05\x36";
  15229. int msgSz = (int)XSTRLEN(msg);
  15230. printf(testingFmt, "wolfSSL_MD4()");
  15231. XMEMSET(out, 0, sizeof(out));
  15232. MD4_Init(&md4);
  15233. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  15234. MD4_Final(out, &md4);
  15235. AssertIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  15236. printf(resultFmt, passed);
  15237. #endif
  15238. }
  15239. static void test_wolfSSL_RSA(void)
  15240. {
  15241. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  15242. RSA* rsa;
  15243. printf(testingFmt, "wolfSSL_RSA()");
  15244. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  15245. AssertIntEQ(RSA_size(rsa), 256);
  15246. RSA_free(rsa);
  15247. AssertNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  15248. AssertIntEQ(RSA_size(rsa), 384);
  15249. RSA_free(rsa);
  15250. /* remove for now with odd key size until adjusting rsa key size check with
  15251. wc_MakeRsaKey()
  15252. AssertNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  15253. RSA_free(rsa);
  15254. */
  15255. AssertNull(RSA_generate_key(-1, 3, NULL, NULL));
  15256. AssertNull(RSA_generate_key(511, 3, NULL, NULL)); /* RSA_MIN_SIZE - 1 */
  15257. AssertNull(RSA_generate_key(4097, 3, NULL, NULL)); /* RSA_MAX_SIZE + 1 */
  15258. AssertNull(RSA_generate_key(2048, 0, NULL, NULL));
  15259. printf(resultFmt, passed);
  15260. #endif
  15261. }
  15262. static void test_wolfSSL_RSA_DER(void)
  15263. {
  15264. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  15265. RSA *rsa;
  15266. int i;
  15267. struct
  15268. {
  15269. const unsigned char *der;
  15270. int sz;
  15271. } tbl[] = {
  15272. #ifdef USE_CERT_BUFFERS_1024
  15273. {client_key_der_1024, sizeof_client_key_der_1024},
  15274. {server_key_der_1024, sizeof_server_key_der_1024},
  15275. #endif
  15276. #ifdef USE_CERT_BUFFERS_2048
  15277. {client_key_der_2048, sizeof_client_key_der_2048},
  15278. {server_key_der_2048, sizeof_server_key_der_2048},
  15279. #endif
  15280. {NULL, 0}
  15281. };
  15282. printf(testingFmt, "test_wolfSSL_RSA_DER()");
  15283. for (i = 0; tbl[i].der != NULL; i++)
  15284. {
  15285. AssertNotNull(d2i_RSAPublicKey(&rsa, &tbl[i].der, tbl[i].sz));
  15286. AssertNotNull(rsa);
  15287. RSA_free(rsa);
  15288. }
  15289. printf(resultFmt, passed);
  15290. #endif
  15291. }
  15292. static void test_wolfSSL_verify_depth(void)
  15293. {
  15294. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  15295. WOLFSSL* ssl;
  15296. WOLFSSL_CTX* ctx;
  15297. long depth;
  15298. printf(testingFmt, "test_wolfSSL_verify_depth()");
  15299. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  15300. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  15301. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  15302. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  15303. AssertIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  15304. AssertNotNull(ssl = SSL_new(ctx));
  15305. AssertIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  15306. SSL_free(ssl);
  15307. SSL_CTX_set_verify_depth(ctx, -1);
  15308. AssertIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  15309. SSL_CTX_set_verify_depth(ctx, 2);
  15310. AssertIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  15311. AssertNotNull(ssl = SSL_new(ctx));
  15312. AssertIntEQ(2, SSL_get_verify_depth(ssl));
  15313. SSL_free(ssl);
  15314. SSL_CTX_free(ctx);
  15315. printf(resultFmt, passed);
  15316. #endif
  15317. }
  15318. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  15319. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  15320. * buffer of 64 bytes.
  15321. *
  15322. * returns the size of the digest buffer on success and a negative value on
  15323. * failure.
  15324. */
  15325. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest)
  15326. {
  15327. HMAC_CTX ctx1;
  15328. HMAC_CTX ctx2;
  15329. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  15330. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  15331. unsigned char long_key[] =
  15332. "0123456789012345678901234567890123456789"
  15333. "0123456789012345678901234567890123456789"
  15334. "0123456789012345678901234567890123456789"
  15335. "0123456789012345678901234567890123456789";
  15336. unsigned char msg[] = "message to hash";
  15337. unsigned int digestSz = 64;
  15338. int keySz = sizeof(key);
  15339. int long_keySz = sizeof(long_key);
  15340. int msgSz = sizeof(msg);
  15341. unsigned char digest2[64];
  15342. unsigned int digestSz2 = 64;
  15343. HMAC_CTX_init(&ctx1);
  15344. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  15345. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  15346. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  15347. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  15348. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  15349. HMAC_CTX_cleanup(&ctx1);
  15350. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  15351. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  15352. HMAC_CTX_cleanup(&ctx2);
  15353. AssertIntEQ(digestSz, digestSz2);
  15354. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  15355. /* test HMAC_Init with NULL key */
  15356. /* init after copy */
  15357. printf("test HMAC_Init with NULL key (0)\n");
  15358. HMAC_CTX_init(&ctx1);
  15359. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  15360. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  15361. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  15362. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  15363. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  15364. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  15365. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  15366. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  15367. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  15368. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  15369. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  15370. HMAC_CTX_cleanup(&ctx2);
  15371. AssertIntEQ(digestSz, digestSz2);
  15372. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  15373. /* long key */
  15374. printf("test HMAC_Init with NULL key (1)\n");
  15375. HMAC_CTX_init(&ctx1);
  15376. AssertIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type), SSL_SUCCESS);
  15377. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  15378. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  15379. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  15380. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  15381. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  15382. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  15383. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  15384. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  15385. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  15386. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  15387. HMAC_CTX_cleanup(&ctx2);
  15388. AssertIntEQ(digestSz, digestSz2);
  15389. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  15390. /* init before copy */
  15391. printf("test HMAC_Init with NULL key (2)\n");
  15392. HMAC_CTX_init(&ctx1);
  15393. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  15394. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  15395. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  15396. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  15397. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  15398. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  15399. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  15400. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  15401. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  15402. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  15403. HMAC_CTX_cleanup(&ctx2);
  15404. AssertIntEQ(digestSz, digestSz2);
  15405. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  15406. return digestSz;
  15407. }
  15408. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  15409. static void test_wolfSSL_HMAC_CTX(void)
  15410. {
  15411. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  15412. unsigned char digest[64];
  15413. int digestSz;
  15414. printf(testingFmt, "wolfSSL_HMAC_CTX()");
  15415. #ifndef NO_SHA
  15416. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha1(), digest)), 20);
  15417. AssertIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  15418. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  15419. #endif /* !NO_SHA */
  15420. #ifdef WOLFSSL_SHA224
  15421. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha224(), digest)), 28);
  15422. AssertIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  15423. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  15424. "\x02\x0E", digest, digestSz), 0);
  15425. #endif /* WOLFSSL_SHA224 */
  15426. #ifndef NO_SHA256
  15427. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha256(), digest)), 32);
  15428. AssertIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  15429. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  15430. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  15431. #endif /* !NO_SHA256 */
  15432. #ifdef WOLFSSL_SHA512
  15433. #ifdef WOLFSSL_SHA384
  15434. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha384(), digest)), 48);
  15435. AssertIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  15436. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  15437. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  15438. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  15439. digest, digestSz), 0);
  15440. #endif /* WOLFSSL_SHA384 */
  15441. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha512(), digest)), 64);
  15442. AssertIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  15443. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  15444. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  15445. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  15446. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  15447. digest, digestSz), 0);
  15448. #endif /* WOLFSSL_SHA512 */
  15449. #ifndef NO_MD5
  15450. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_md5(), digest)), 16);
  15451. AssertIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  15452. "\xE4\x98\xDD", digest, digestSz), 0);
  15453. #endif /* !NO_MD5 */
  15454. printf(resultFmt, passed);
  15455. #endif
  15456. }
  15457. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  15458. static void sslMsgCb(int w, int version, int type, const void* buf,
  15459. size_t sz, SSL* ssl, void* arg)
  15460. {
  15461. int i;
  15462. unsigned char* pt = (unsigned char*)buf;
  15463. printf("%s %d bytes of version %d , type %d : ", (w)?"Writing":"Reading",
  15464. (int)sz, version, type);
  15465. for (i = 0; i < (int)sz; i++) printf("%02X", pt[i]);
  15466. printf("\n");
  15467. (void)ssl;
  15468. (void)arg;
  15469. }
  15470. #endif /* OPENSSL_EXTRA */
  15471. static void test_wolfSSL_msg_callback(void)
  15472. {
  15473. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  15474. WOLFSSL* ssl;
  15475. WOLFSSL_CTX* ctx;
  15476. printf(testingFmt, "wolfSSL_msg_callback()");
  15477. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  15478. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  15479. SSL_FILETYPE_PEM));
  15480. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  15481. SSL_FILETYPE_PEM));
  15482. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  15483. SSL_SUCCESS);
  15484. AssertNotNull(ssl = SSL_new(ctx));
  15485. AssertIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  15486. AssertIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  15487. AssertIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  15488. SSL_CTX_free(ctx);
  15489. SSL_free(ssl);
  15490. printf(resultFmt, passed);
  15491. #endif
  15492. }
  15493. static void test_wolfSSL_SHA(void)
  15494. {
  15495. #if defined(OPENSSL_EXTRA)
  15496. printf(testingFmt, "wolfSSL_SHA()");
  15497. #if !defined(NO_SHA)
  15498. {
  15499. const unsigned char in[] = "abc";
  15500. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  15501. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  15502. unsigned char out[WC_SHA_DIGEST_SIZE];
  15503. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  15504. AssertNotNull(SHA1(in, XSTRLEN((char*)in), out));
  15505. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  15506. }
  15507. #endif
  15508. #if !defined(NO_SHA256)
  15509. {
  15510. const unsigned char in[] = "abc";
  15511. unsigned char expected[] = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  15512. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  15513. "\x15\xAD";
  15514. unsigned char out[WC_SHA256_DIGEST_SIZE];
  15515. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  15516. AssertNotNull(SHA256(in, XSTRLEN((char*)in), out));
  15517. AssertIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  15518. }
  15519. #endif
  15520. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_SHA512)
  15521. {
  15522. const unsigned char in[] = "abc";
  15523. unsigned char expected[] = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  15524. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  15525. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  15526. "\xc8\x25\xa7";
  15527. unsigned char out[WC_SHA384_DIGEST_SIZE];
  15528. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  15529. AssertNotNull(SHA384(in, XSTRLEN((char*)in), out));
  15530. AssertIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  15531. }
  15532. #endif
  15533. #if defined(WOLFSSL_SHA512)
  15534. {
  15535. const unsigned char in[] = "abc";
  15536. unsigned char expected[] = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  15537. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  15538. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  15539. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  15540. "\xa5\x4c\xa4\x9f";
  15541. unsigned char out[WC_SHA512_DIGEST_SIZE];
  15542. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  15543. AssertNotNull(SHA512(in, XSTRLEN((char*)in), out));
  15544. AssertIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  15545. }
  15546. #endif
  15547. printf(resultFmt, passed);
  15548. #endif
  15549. }
  15550. static void test_wolfSSL_DH_1536_prime(void)
  15551. {
  15552. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  15553. BIGNUM* bn;
  15554. unsigned char bits[200];
  15555. int sz = 192; /* known binary size */
  15556. const byte expected[] = {
  15557. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  15558. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  15559. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  15560. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  15561. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  15562. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  15563. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  15564. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  15565. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  15566. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  15567. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  15568. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  15569. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  15570. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  15571. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  15572. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  15573. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  15574. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  15575. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  15576. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  15577. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  15578. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  15579. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  15580. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  15581. };
  15582. printf(testingFmt, "wolfSSL_DH_1536_prime()");
  15583. AssertNotNull(bn = get_rfc3526_prime_1536(NULL));
  15584. AssertIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  15585. AssertIntEQ(0, XMEMCMP(expected, bits, sz));
  15586. BN_free(bn);
  15587. printf(resultFmt, passed);
  15588. #endif
  15589. }
  15590. static void test_wolfSSL_AES_ecb_encrypt(void)
  15591. {
  15592. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB)
  15593. AES_KEY aes;
  15594. const byte msg[] =
  15595. {
  15596. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15597. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  15598. };
  15599. const byte verify[] =
  15600. {
  15601. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  15602. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  15603. };
  15604. const byte key[] =
  15605. {
  15606. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  15607. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  15608. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  15609. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  15610. };
  15611. byte out[AES_BLOCK_SIZE];
  15612. printf(testingFmt, "wolfSSL_AES_ecb_encrypt()");
  15613. AssertIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  15614. XMEMSET(out, 0, AES_BLOCK_SIZE);
  15615. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  15616. AssertIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  15617. #ifdef HAVE_AES_DECRYPT
  15618. AssertIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  15619. XMEMSET(out, 0, AES_BLOCK_SIZE);
  15620. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  15621. AssertIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  15622. #endif
  15623. /* test bad arguments */
  15624. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  15625. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  15626. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  15627. printf(resultFmt, passed);
  15628. #endif
  15629. }
  15630. static void test_wolfSSL_SHA256(void)
  15631. {
  15632. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  15633. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS)
  15634. unsigned char input[] =
  15635. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  15636. unsigned char output[] =
  15637. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  15638. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  15639. "\x06\xC1";
  15640. size_t inLen;
  15641. byte hash[WC_SHA256_DIGEST_SIZE];
  15642. printf(testingFmt, "wolfSSL_SHA256()");
  15643. inLen = XSTRLEN((char*)input);
  15644. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  15645. AssertNotNull(SHA256(input, inLen, hash));
  15646. AssertIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  15647. printf(resultFmt, passed);
  15648. #endif
  15649. }
  15650. static void test_wolfSSL_X509_get_serialNumber(void)
  15651. {
  15652. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  15653. !defined(NO_RSA)
  15654. ASN1_INTEGER* a;
  15655. BIGNUM* bn;
  15656. X509* x509;
  15657. printf(testingFmt, "wolfSSL_X509_get_serialNumber()");
  15658. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  15659. SSL_FILETYPE_PEM));
  15660. AssertNotNull(a = X509_get_serialNumber(x509));
  15661. X509_free(x509);
  15662. /* check on value of ASN1 Integer */
  15663. AssertNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  15664. AssertIntEQ(BN_get_word(bn), 1);
  15665. BN_free(bn);
  15666. ASN1_INTEGER_free(a);
  15667. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  15668. a = ASN1_INTEGER_new();
  15669. AssertNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  15670. DYNAMIC_TYPE_OPENSSL));
  15671. a->isDynamic = 1;
  15672. ASN1_INTEGER_free(a);
  15673. printf(resultFmt, passed);
  15674. #endif
  15675. }
  15676. static void test_wolfSSL_OPENSSL_add_all_algorithms(void){
  15677. #if defined(OPENSSL_EXTRA)
  15678. printf(testingFmt, "wolfSSL_OPENSSL_add_all_algorithms()");
  15679. AssertIntEQ(wolfSSL_OPENSSL_add_all_algorithms_noconf(),WOLFSSL_SUCCESS);
  15680. wolfSSL_Cleanup();
  15681. printf(resultFmt, passed);
  15682. #endif
  15683. }
  15684. static void test_wolfSSL_ASN1_STRING_print_ex(void){
  15685. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  15686. ASN1_STRING* asn_str = NULL;
  15687. const char data[] = "Hello wolfSSL!";
  15688. ASN1_STRING* esc_str = NULL;
  15689. const char esc_data[] = "a+;<>";
  15690. BIO *bio;
  15691. unsigned long flags;
  15692. int p_len;
  15693. unsigned char rbuf[255];
  15694. printf(testingFmt, "wolfSSL_ASN1_STRING_print_ex()");
  15695. /* setup */
  15696. XMEMSET(rbuf, 0, 255);
  15697. bio = BIO_new(BIO_s_mem());
  15698. BIO_set_write_buf_size(bio,255);
  15699. asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  15700. ASN1_STRING_set(asn_str, (const void*)data, sizeof(data));
  15701. esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  15702. ASN1_STRING_set(esc_str, (const void*)esc_data, sizeof(esc_data));
  15703. /* no flags */
  15704. XMEMSET(rbuf, 0, 255);
  15705. flags = 0;
  15706. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  15707. AssertIntEQ(p_len, 15);
  15708. BIO_read(bio, (void*)rbuf, 15);
  15709. AssertStrEQ((char*)rbuf, "Hello wolfSSL!");
  15710. /* RFC2253 Escape */
  15711. XMEMSET(rbuf, 0, 255);
  15712. flags = ASN1_STRFLGS_ESC_2253;
  15713. p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags);
  15714. AssertIntEQ(p_len, 9);
  15715. BIO_read(bio, (void*)rbuf, 9);
  15716. AssertStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  15717. /* Show type */
  15718. XMEMSET(rbuf, 0, 255);
  15719. flags = ASN1_STRFLGS_SHOW_TYPE;
  15720. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  15721. AssertIntEQ(p_len, 28);
  15722. BIO_read(bio, (void*)rbuf, 28);
  15723. AssertStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  15724. /* Dump All */
  15725. XMEMSET(rbuf, 0, 255);
  15726. flags = ASN1_STRFLGS_DUMP_ALL;
  15727. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  15728. AssertIntEQ(p_len, 31);
  15729. BIO_read(bio, (void*)rbuf, 31);
  15730. AssertStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  15731. /* Dump Der */
  15732. XMEMSET(rbuf, 0, 255);
  15733. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  15734. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  15735. AssertIntEQ(p_len, 35);
  15736. BIO_read(bio, (void*)rbuf, 35);
  15737. AssertStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  15738. /* Dump All + Show type */
  15739. XMEMSET(rbuf, 0, 255);
  15740. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  15741. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  15742. AssertIntEQ(p_len, 44);
  15743. BIO_read(bio, (void*)rbuf, 44);
  15744. AssertStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  15745. BIO_free(bio);
  15746. ASN1_STRING_free(asn_str);
  15747. ASN1_STRING_free(esc_str);
  15748. printf(resultFmt, passed);
  15749. #endif
  15750. }
  15751. static void test_wolfSSL_ASN1_TIME_to_generalizedtime(void){
  15752. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN1_TIME)
  15753. WOLFSSL_ASN1_TIME *t;
  15754. WOLFSSL_ASN1_TIME *out;
  15755. WOLFSSL_ASN1_TIME *gtime;
  15756. printf(testingFmt, "wolfSSL_ASN1_TIME_to_generalizedtime()");
  15757. /* UTC Time test */
  15758. AssertNotNull(t = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME),
  15759. NULL, DYNAMIC_TYPE_TMP_BUFFER));
  15760. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  15761. AssertNotNull(out = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME),
  15762. NULL, DYNAMIC_TYPE_TMP_BUFFER));
  15763. t->data[0] = ASN_UTC_TIME;
  15764. t->data[1] = ASN_UTC_TIME_SIZE;
  15765. XMEMCPY(t->data + 2,"050727123456Z",ASN_UTC_TIME_SIZE);
  15766. AssertNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  15767. AssertIntEQ(gtime->data[0], ASN_GENERALIZED_TIME);
  15768. AssertIntEQ(gtime->data[1], ASN_GENERALIZED_TIME_SIZE);
  15769. AssertStrEQ((char*)gtime->data + 2, "20050727123456Z");
  15770. /* Generalized Time test */
  15771. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  15772. XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE);
  15773. gtime = NULL;
  15774. t->data[0] = ASN_GENERALIZED_TIME;
  15775. t->data[1] = ASN_GENERALIZED_TIME_SIZE;
  15776. XMEMCPY(t->data + 2,"20050727123456Z",ASN_GENERALIZED_TIME_SIZE);
  15777. AssertNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  15778. AssertIntEQ(gtime->data[0], ASN_GENERALIZED_TIME);
  15779. AssertIntEQ(gtime->data[1], ASN_GENERALIZED_TIME_SIZE);
  15780. AssertStrEQ((char*)gtime->data + 2, "20050727123456Z");
  15781. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15782. /* Null parameter test */
  15783. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  15784. gtime = NULL;
  15785. out = NULL;
  15786. t->data[0] = ASN_UTC_TIME;
  15787. t->data[1] = ASN_UTC_TIME_SIZE;
  15788. XMEMCPY(t->data + 2,"050727123456Z",ASN_UTC_TIME_SIZE);
  15789. AssertNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  15790. AssertIntEQ(gtime->data[0], ASN_GENERALIZED_TIME);
  15791. AssertIntEQ(gtime->data[1], ASN_GENERALIZED_TIME_SIZE);
  15792. AssertStrEQ((char*)gtime->data + 2, "20050727123456Z");
  15793. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15794. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15795. printf(resultFmt, passed);
  15796. #endif
  15797. }
  15798. static void test_wolfSSL_X509_check_ca(void){
  15799. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  15800. WOLFSSL_X509 *x509;
  15801. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  15802. AssertIntEQ(wolfSSL_X509_check_ca(x509), 1);
  15803. wolfSSL_X509_free(x509);
  15804. x509 = wolfSSL_X509_load_certificate_file(ntruCertFile, WOLFSSL_FILETYPE_PEM);
  15805. AssertIntEQ(wolfSSL_X509_check_ca(x509), 0);
  15806. wolfSSL_X509_free(x509);
  15807. #endif
  15808. }
  15809. static void test_no_op_functions(void)
  15810. {
  15811. #if defined(OPENSSL_EXTRA)
  15812. printf(testingFmt, "no_op_functions()");
  15813. /* this makes sure wolfSSL can compile and run these no-op functions */
  15814. SSL_load_error_strings();
  15815. ENGINE_load_builtin_engines();
  15816. OpenSSL_add_all_ciphers();
  15817. CRYPTO_malloc_init();
  15818. printf(resultFmt, passed);
  15819. #endif
  15820. }
  15821. /*----------------------------------------------------------------------------*
  15822. | wolfCrypt ASN
  15823. *----------------------------------------------------------------------------*/
  15824. static void test_wc_GetPkcs8TraditionalOffset(void)
  15825. {
  15826. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM)
  15827. int length, derSz;
  15828. word32 inOutIdx;
  15829. const char* path = "./certs/server-keyPkcs8.der";
  15830. FILE* file;
  15831. byte der[2048];
  15832. printf(testingFmt, "wc_GetPkcs8TraditionalOffset");
  15833. file = fopen(path, "rb");
  15834. AssertNotNull(file);
  15835. derSz = (int)fread(der, 1, sizeof(der), file);
  15836. fclose(file);
  15837. /* valid case */
  15838. inOutIdx = 0;
  15839. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  15840. AssertIntGT(length, 0);
  15841. /* inOutIdx > sz */
  15842. inOutIdx = 4000;
  15843. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  15844. AssertIntEQ(length, BAD_FUNC_ARG);
  15845. /* null input */
  15846. inOutIdx = 0;
  15847. length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0);
  15848. AssertIntEQ(length, BAD_FUNC_ARG);
  15849. /* invalid input, fill buffer with 1's */
  15850. XMEMSET(der, 1, sizeof(der));
  15851. inOutIdx = 0;
  15852. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  15853. AssertIntEQ(length, ASN_PARSE_E);
  15854. printf(resultFmt, passed);
  15855. #endif /* NO_ASN */
  15856. }
  15857. /*----------------------------------------------------------------------------*
  15858. | wolfCrypt ECC
  15859. *----------------------------------------------------------------------------*/
  15860. static void test_wc_ecc_get_curve_size_from_name(void)
  15861. {
  15862. #ifdef HAVE_ECC
  15863. int ret;
  15864. printf(testingFmt, "wc_ecc_get_curve_size_from_name");
  15865. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  15866. ret = wc_ecc_get_curve_size_from_name("SECP256R1");
  15867. AssertIntEQ(ret, 32);
  15868. #endif
  15869. /* invalid case */
  15870. ret = wc_ecc_get_curve_size_from_name("BADCURVE");
  15871. AssertIntEQ(ret, -1);
  15872. /* NULL input */
  15873. ret = wc_ecc_get_curve_size_from_name(NULL);
  15874. AssertIntEQ(ret, BAD_FUNC_ARG);
  15875. printf(resultFmt, passed);
  15876. #endif /* HAVE_ECC */
  15877. }
  15878. static void test_wc_ecc_get_curve_id_from_name(void)
  15879. {
  15880. #ifdef HAVE_ECC
  15881. int id;
  15882. printf(testingFmt, "wc_ecc_get_curve_id_from_name");
  15883. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  15884. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  15885. AssertIntEQ(id, ECC_SECP256R1);
  15886. #endif
  15887. /* invalid case */
  15888. id = wc_ecc_get_curve_id_from_name("BADCURVE");
  15889. AssertIntEQ(id, -1);
  15890. /* NULL input */
  15891. id = wc_ecc_get_curve_id_from_name(NULL);
  15892. AssertIntEQ(id, BAD_FUNC_ARG);
  15893. printf(resultFmt, passed);
  15894. #endif /* HAVE_ECC */
  15895. }
  15896. static void test_wc_ecc_get_curve_id_from_params(void)
  15897. {
  15898. #ifdef HAVE_ECC
  15899. int id;
  15900. const byte prime[] =
  15901. {
  15902. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  15903. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15904. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  15905. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  15906. };
  15907. const byte primeInvalid[] =
  15908. {
  15909. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  15910. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15911. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  15912. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  15913. };
  15914. const byte Af[] =
  15915. {
  15916. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  15917. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15918. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  15919. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  15920. };
  15921. const byte Bf[] =
  15922. {
  15923. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  15924. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  15925. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  15926. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  15927. };
  15928. const byte order[] =
  15929. {
  15930. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  15931. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  15932. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  15933. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  15934. };
  15935. const byte Gx[] =
  15936. {
  15937. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  15938. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  15939. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  15940. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  15941. };
  15942. const byte Gy[] =
  15943. {
  15944. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  15945. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  15946. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  15947. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  15948. };
  15949. int cofactor = 1;
  15950. int fieldSize = 256;
  15951. printf(testingFmt, "wc_ecc_get_curve_id_from_params");
  15952. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  15953. id = wc_ecc_get_curve_id_from_params(fieldSize, prime, sizeof(prime),
  15954. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  15955. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  15956. AssertIntEQ(id, ECC_SECP256R1);
  15957. #endif
  15958. /* invalid case, fieldSize = 0 */
  15959. id = wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  15960. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  15961. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  15962. AssertIntEQ(id, ECC_CURVE_INVALID);
  15963. /* invalid case, NULL prime */
  15964. id = wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  15965. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  15966. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  15967. AssertIntEQ(id, BAD_FUNC_ARG);
  15968. /* invalid case, invalid prime */
  15969. id = wc_ecc_get_curve_id_from_params(fieldSize,
  15970. primeInvalid, sizeof(primeInvalid),
  15971. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  15972. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  15973. AssertIntEQ(id, ECC_CURVE_INVALID);
  15974. printf(resultFmt, passed);
  15975. #endif
  15976. }
  15977. /*----------------------------------------------------------------------------*
  15978. | Certficate Failure Checks
  15979. *----------------------------------------------------------------------------*/
  15980. #ifndef NO_CERTS
  15981. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  15982. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  15983. int type)
  15984. {
  15985. int ret;
  15986. WOLFSSL_CERT_MANAGER* cm = NULL;
  15987. switch (type) {
  15988. case TESTING_RSA:
  15989. #ifdef NO_RSA
  15990. printf("RSA disabled, skipping test\n");
  15991. return ASN_SIG_CONFIRM_E;
  15992. #else
  15993. break;
  15994. #endif
  15995. case TESTING_ECC:
  15996. #ifndef HAVE_ECC
  15997. printf("ECC disabled, skipping test\n");
  15998. return ASN_SIG_CONFIRM_E;
  15999. #else
  16000. break;
  16001. #endif
  16002. default:
  16003. printf("Bad function argument\n");
  16004. return BAD_FUNC_ARG;
  16005. }
  16006. cm = wolfSSL_CertManagerNew();
  16007. if (cm == NULL) {
  16008. printf("wolfSSL_CertManagerNew failed\n");
  16009. return -1;
  16010. }
  16011. #ifndef NO_FILESYSTEM
  16012. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  16013. if (ret != WOLFSSL_SUCCESS) {
  16014. printf("wolfSSL_CertManagerLoadCA failed\n");
  16015. wolfSSL_CertManagerFree(cm);
  16016. return ret;
  16017. }
  16018. #else
  16019. (void)ca;
  16020. #endif
  16021. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz, WOLFSSL_FILETYPE_ASN1);
  16022. /* Let AssertIntEQ handle return code */
  16023. wolfSSL_CertManagerFree(cm);
  16024. return ret;
  16025. }
  16026. static int test_RsaSigFailure_cm(void)
  16027. {
  16028. int ret = 0;
  16029. const char* ca_cert = "./certs/ca-cert.pem";
  16030. const char* server_cert = "./certs/server-cert.der";
  16031. byte* cert_buf = NULL;
  16032. size_t cert_sz = 0;
  16033. ret = load_file(server_cert, &cert_buf, &cert_sz);
  16034. if (ret == 0) {
  16035. /* corrupt DER - invert last byte, which is signature */
  16036. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  16037. /* test bad cert */
  16038. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA);
  16039. }
  16040. printf("Signature failure test: RSA: Ret %d\n", ret);
  16041. if (cert_buf)
  16042. free(cert_buf);
  16043. return ret;
  16044. }
  16045. static int test_EccSigFailure_cm(void)
  16046. {
  16047. int ret = 0;
  16048. /* self-signed ECC cert, so use server cert as CA */
  16049. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  16050. const char* server_cert = "./certs/server-ecc.der";
  16051. byte* cert_buf = NULL;
  16052. size_t cert_sz = 0;
  16053. ret = load_file(server_cert, &cert_buf, &cert_sz);
  16054. if (ret == 0) {
  16055. /* corrupt DER - invert last byte, which is signature */
  16056. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  16057. /* test bad cert */
  16058. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC);
  16059. }
  16060. printf("Signature failure test: ECC: Ret %d\n", ret);
  16061. if (cert_buf)
  16062. free(cert_buf);
  16063. return ret;
  16064. }
  16065. #endif /* NO_CERTS */
  16066. #ifdef WOLFSSL_TLS13
  16067. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  16068. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  16069. #endif
  16070. #ifdef WOLFSSL_EARLY_DATA
  16071. static const char earlyData[] = "Early Data";
  16072. static char earlyDataBuffer[1];
  16073. #endif
  16074. static int test_tls13_apis(void)
  16075. {
  16076. int ret = 0;
  16077. #ifndef WOLFSSL_NO_TLS12
  16078. #ifndef NO_WOLFSSL_CLIENT
  16079. WOLFSSL_CTX* clientTls12Ctx;
  16080. WOLFSSL* clientTls12Ssl;
  16081. #endif
  16082. #ifndef NO_WOLFSSL_SERVER
  16083. WOLFSSL_CTX* serverTls12Ctx;
  16084. WOLFSSL* serverTls12Ssl;
  16085. #endif
  16086. #endif
  16087. #ifndef NO_WOLFSSL_CLIENT
  16088. WOLFSSL_CTX* clientCtx;
  16089. WOLFSSL* clientSsl;
  16090. #endif
  16091. #ifndef NO_WOLFSSL_SERVER
  16092. WOLFSSL_CTX* serverCtx;
  16093. WOLFSSL* serverSsl;
  16094. #ifndef NO_CERTS
  16095. const char* ourCert = svrCertFile;
  16096. const char* ourKey = svrKeyFile;
  16097. #endif
  16098. #endif
  16099. #ifdef WOLFSSL_EARLY_DATA
  16100. int outSz;
  16101. #endif
  16102. int groups[1] = { WOLFSSL_ECC_X25519 };
  16103. int numGroups = 1;
  16104. #ifndef WOLFSSL_NO_TLS12
  16105. #ifndef NO_WOLFSSL_CLIENT
  16106. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  16107. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  16108. #endif
  16109. #ifndef NO_WOLFSSL_SERVER
  16110. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  16111. #ifndef NO_CERTS
  16112. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  16113. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey, WOLFSSL_FILETYPE_PEM);
  16114. #endif
  16115. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  16116. #endif
  16117. #endif
  16118. #ifndef NO_WOLFSSL_CLIENT
  16119. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  16120. clientSsl = wolfSSL_new(clientCtx);
  16121. #endif
  16122. #ifndef NO_WOLFSSL_SERVER
  16123. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  16124. #ifndef NO_CERTS
  16125. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  16126. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  16127. #endif
  16128. serverSsl = wolfSSL_new(serverCtx);
  16129. #endif
  16130. #ifdef WOLFSSL_SEND_HRR_COOKIE
  16131. AssertIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  16132. #ifndef NO_WOLFSSL_CLIENT
  16133. AssertIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  16134. #endif
  16135. #ifndef NO_WOLFSSL_SERVER
  16136. #ifndef WOLFSSL_NO_TLS12
  16137. AssertIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0), BAD_FUNC_ARG);
  16138. #endif
  16139. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  16140. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  16141. WOLFSSL_SUCCESS);
  16142. #endif
  16143. #endif
  16144. #ifdef HAVE_ECC
  16145. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  16146. #ifndef NO_WOLFSSL_SERVER
  16147. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1),
  16148. WOLFSSL_SUCCESS);
  16149. #endif
  16150. #ifndef NO_WOLFSSL_CLIENT
  16151. #ifndef WOLFSSL_NO_TLS12
  16152. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  16153. WOLFSSL_SUCCESS);
  16154. #endif
  16155. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  16156. WOLFSSL_SUCCESS);
  16157. #endif
  16158. #elif defined(HAVE_CURVE25519)
  16159. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  16160. #ifndef NO_WOLFSSL_SERVER
  16161. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  16162. WOLFSSL_SUCCESS);
  16163. #endif
  16164. #ifndef NO_WOLFSSL_CLIENT
  16165. #ifndef WOLFSSL_NO_TLS12
  16166. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  16167. WOLFSSL_SUCCESS);
  16168. #endif
  16169. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  16170. WOLFSSL_SUCCESS);
  16171. #endif
  16172. #else
  16173. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  16174. #ifndef NO_WOLFSSL_CLIENT
  16175. #ifndef WOLFSSL_NO_TLS12
  16176. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  16177. NOT_COMPILED_IN);
  16178. #endif
  16179. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  16180. NOT_COMPILED_IN);
  16181. #endif
  16182. #endif
  16183. AssertIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  16184. #ifndef NO_WOLFSSL_SERVER
  16185. AssertIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  16186. #endif
  16187. #ifndef NO_WOLFSSL_CLIENT
  16188. #ifndef WOLFSSL_NO_TLS12
  16189. AssertIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  16190. #endif
  16191. AssertIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  16192. #endif
  16193. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  16194. #ifndef NO_WOLFSSL_CLIENT
  16195. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  16196. #endif
  16197. #ifndef NO_WOLFSSL_SERVER
  16198. #ifndef WOLFSSL_NO_TLS12
  16199. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  16200. #endif
  16201. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  16202. #endif
  16203. AssertIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  16204. #ifndef NO_WOLFSSL_CLIENT
  16205. AssertIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  16206. #endif
  16207. #ifndef NO_WOLFSSL_SERVER
  16208. #ifndef WOLFSSL_NO_TLS12
  16209. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  16210. #endif
  16211. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  16212. #endif
  16213. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  16214. #ifndef NO_WOLFSSL_CLIENT
  16215. #ifndef WOLFSSL_NO_TLS12
  16216. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  16217. #endif
  16218. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  16219. #endif
  16220. #ifndef NO_WOLFSSL_SERVER
  16221. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  16222. #endif
  16223. AssertIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  16224. #ifndef NO_WOLFSSL_CLIENT
  16225. #ifndef WOLFSSL_NO_TLS12
  16226. AssertIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  16227. #endif
  16228. AssertIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  16229. #endif
  16230. #ifndef NO_WOLFSSL_SERVER
  16231. AssertIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  16232. #endif
  16233. AssertIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  16234. #ifndef NO_WOLFSSL_CLIENT
  16235. #ifndef WOLFSSL_NO_TLS12
  16236. AssertIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  16237. #endif
  16238. AssertIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  16239. #endif
  16240. #ifndef NO_WOLFSSL_SERVER
  16241. AssertIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  16242. #endif
  16243. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  16244. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  16245. #ifndef NO_WOLFSSL_SERVER
  16246. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  16247. #endif
  16248. #ifndef NO_WOLFSSL_CLIENT
  16249. #ifndef WOLFSSL_NO_TLS12
  16250. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  16251. BAD_FUNC_ARG);
  16252. #endif
  16253. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  16254. #endif
  16255. AssertIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  16256. #ifndef NO_WOLFSSL_SERVER
  16257. AssertIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  16258. #endif
  16259. #ifndef NO_WOLFSSL_CLIENT
  16260. #ifndef WOLFSSL_NO_TLS12
  16261. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  16262. BAD_FUNC_ARG);
  16263. #endif
  16264. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  16265. #endif
  16266. AssertIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  16267. #ifndef NO_WOLFSSL_CLIENT
  16268. AssertIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  16269. #endif
  16270. #ifndef NO_WOLFSSL_SERVER
  16271. #ifndef WOLFSSL_NO_TLS12
  16272. AssertIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  16273. BAD_FUNC_ARG);
  16274. #endif
  16275. AssertIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  16276. #endif
  16277. #endif
  16278. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  16279. AssertIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  16280. #ifndef NO_WOLFSSL_SERVER
  16281. AssertIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  16282. #endif
  16283. #ifndef NO_WOLFSSL_CLIENT
  16284. #ifndef WOLFSSL_NO_TLS12
  16285. AssertIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  16286. #endif
  16287. AssertIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  16288. #endif
  16289. #endif
  16290. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  16291. #ifndef NO_WOLFSSL_CLIENT
  16292. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  16293. #endif
  16294. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  16295. #ifndef NO_WOLFSSL_CLIENT
  16296. #ifndef WOLFSSL_NO_TLS12
  16297. AssertIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  16298. BAD_FUNC_ARG);
  16299. #endif
  16300. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  16301. WOLFSSL_MAX_GROUP_COUNT + 1),
  16302. BAD_FUNC_ARG);
  16303. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  16304. WOLFSSL_SUCCESS);
  16305. #endif
  16306. #ifndef NO_WOLFSSL_SERVER
  16307. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  16308. WOLFSSL_SUCCESS);
  16309. #endif
  16310. AssertIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  16311. #ifndef NO_WOLFSSL_CLIENT
  16312. AssertIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  16313. #endif
  16314. AssertIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  16315. #ifndef NO_WOLFSSL_CLIENT
  16316. #ifndef WOLFSSL_NO_TLS12
  16317. AssertIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  16318. BAD_FUNC_ARG);
  16319. #endif
  16320. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups,
  16321. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  16322. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  16323. WOLFSSL_SUCCESS);
  16324. #endif
  16325. #ifndef NO_WOLFSSL_SERVER
  16326. AssertIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  16327. WOLFSSL_SUCCESS);
  16328. #endif
  16329. #ifdef WOLFSSL_EARLY_DATA
  16330. AssertIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  16331. #ifndef NO_WOLFSSL_CLIENT
  16332. AssertIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  16333. #endif
  16334. #ifndef NO_WOLFSSL_SERVER
  16335. #ifndef WOLFSSL_NO_TLS12
  16336. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  16337. BAD_FUNC_ARG);
  16338. #endif
  16339. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 0), 0);
  16340. #endif
  16341. AssertIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  16342. #ifndef NO_WOLFSSL_CLIENT
  16343. AssertIntEQ(wolfSSL_set_max_early_data(clientSsl, 0), SIDE_ERROR);
  16344. #endif
  16345. #ifndef NO_WOLFSSL_SERVER
  16346. #ifndef WOLFSSL_NO_TLS12
  16347. AssertIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  16348. #endif
  16349. AssertIntEQ(wolfSSL_set_max_early_data(serverSsl, 0), 0);
  16350. #endif
  16351. AssertIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  16352. &outSz), BAD_FUNC_ARG);
  16353. #ifndef NO_WOLFSSL_CLIENT
  16354. AssertIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  16355. &outSz), BAD_FUNC_ARG);
  16356. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  16357. BAD_FUNC_ARG);
  16358. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  16359. sizeof(earlyData), NULL),
  16360. BAD_FUNC_ARG);
  16361. #endif
  16362. #ifndef NO_WOLFSSL_SERVER
  16363. AssertIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  16364. sizeof(earlyData), &outSz),
  16365. SIDE_ERROR);
  16366. #endif
  16367. #ifndef NO_WOLFSSL_CLIENT
  16368. #ifndef WOLFSSL_NO_TLS12
  16369. AssertIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  16370. sizeof(earlyData), &outSz),
  16371. BAD_FUNC_ARG);
  16372. #endif
  16373. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  16374. sizeof(earlyData), &outSz),
  16375. WOLFSSL_FATAL_ERROR);
  16376. #endif
  16377. AssertIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  16378. sizeof(earlyDataBuffer), &outSz),
  16379. BAD_FUNC_ARG);
  16380. #ifndef NO_WOLFSSL_SERVER
  16381. AssertIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  16382. sizeof(earlyDataBuffer), &outSz),
  16383. BAD_FUNC_ARG);
  16384. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1, &outSz),
  16385. BAD_FUNC_ARG);
  16386. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  16387. sizeof(earlyDataBuffer), NULL),
  16388. BAD_FUNC_ARG);
  16389. #endif
  16390. #ifndef NO_WOLFSSL_CLIENT
  16391. AssertIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  16392. sizeof(earlyDataBuffer), &outSz),
  16393. SIDE_ERROR);
  16394. #endif
  16395. #ifndef NO_WOLFSSL_SERVER
  16396. #ifndef WOLFSSL_NO_TLS12
  16397. AssertIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  16398. sizeof(earlyDataBuffer), &outSz),
  16399. BAD_FUNC_ARG);
  16400. #endif
  16401. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  16402. sizeof(earlyDataBuffer), &outSz),
  16403. WOLFSSL_FATAL_ERROR);
  16404. #endif
  16405. #endif
  16406. #ifndef NO_WOLFSSL_SERVER
  16407. wolfSSL_free(serverSsl);
  16408. wolfSSL_CTX_free(serverCtx);
  16409. #endif
  16410. #ifndef NO_WOLFSSL_CLIENT
  16411. wolfSSL_free(clientSsl);
  16412. wolfSSL_CTX_free(clientCtx);
  16413. #endif
  16414. #ifndef WOLFSSL_NO_TLS12
  16415. #ifndef NO_WOLFSSL_SERVER
  16416. wolfSSL_free(serverTls12Ssl);
  16417. wolfSSL_CTX_free(serverTls12Ctx);
  16418. #endif
  16419. #ifndef NO_WOLFSSL_CLIENT
  16420. wolfSSL_free(clientTls12Ssl);
  16421. wolfSSL_CTX_free(clientTls12Ctx);
  16422. #endif
  16423. #endif
  16424. return ret;
  16425. }
  16426. #endif
  16427. #ifdef HAVE_PK_CALLBACKS
  16428. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  16429. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  16430. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  16431. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  16432. const unsigned char* priv, unsigned int privSz,
  16433. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  16434. unsigned char* out, unsigned int* outlen,
  16435. void* ctx)
  16436. {
  16437. /* Test fail when context associated with WOLFSSL is NULL */
  16438. if (ctx == NULL) {
  16439. return -1;
  16440. }
  16441. (void)ssl;
  16442. /* return 0 on success */
  16443. return wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  16444. };
  16445. static void test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  16446. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  16447. #ifdef WOLFSSL_AES_128
  16448. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  16449. WOLFSSL_SUCCESS);
  16450. #endif
  16451. #ifdef WOLFSSL_AES_256
  16452. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  16453. WOLFSSL_SUCCESS);
  16454. #endif
  16455. }
  16456. static void test_dh_ssl_setup(WOLFSSL* ssl)
  16457. {
  16458. static int dh_test_ctx = 1;
  16459. int ret;
  16460. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  16461. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  16462. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  16463. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  16464. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  16465. }
  16466. }
  16467. static void test_dh_ssl_setup_fail(WOLFSSL* ssl)
  16468. {
  16469. int ret;
  16470. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  16471. AssertNull(wolfSSL_GetDhAgreeCtx(ssl));
  16472. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  16473. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  16474. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  16475. }
  16476. }
  16477. #endif
  16478. static void test_DhCallbacks(void)
  16479. {
  16480. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  16481. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  16482. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  16483. WOLFSSL_CTX *ctx;
  16484. WOLFSSL *ssl;
  16485. tcp_ready ready;
  16486. func_args server_args;
  16487. func_args client_args;
  16488. THREAD_TYPE serverThread;
  16489. callback_functions func_cb_client;
  16490. callback_functions func_cb_server;
  16491. int test;
  16492. printf(testingFmt, "test_DhCallbacks");
  16493. #ifndef NO_WOLFSSL_CLIENT
  16494. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  16495. #else
  16496. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  16497. #endif
  16498. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  16499. /* load client ca cert */
  16500. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  16501. WOLFSSL_SUCCESS);
  16502. /* test with NULL arguments */
  16503. wolfSSL_SetDhAgreeCtx(NULL, &test);
  16504. AssertNull(wolfSSL_GetDhAgreeCtx(NULL));
  16505. /* test success case */
  16506. test = 1;
  16507. AssertNotNull(ssl = wolfSSL_new(ctx));
  16508. wolfSSL_SetDhAgreeCtx(ssl, &test);
  16509. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  16510. wolfSSL_free(ssl);
  16511. wolfSSL_CTX_free(ctx);
  16512. /* test a connection where callback is used */
  16513. #ifdef WOLFSSL_TIRTOS
  16514. fdOpenSession(Task_self());
  16515. #endif
  16516. XMEMSET(&server_args, 0, sizeof(func_args));
  16517. XMEMSET(&client_args, 0, sizeof(func_args));
  16518. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  16519. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  16520. StartTCP();
  16521. InitTcpReady(&ready);
  16522. #if defined(USE_WINDOWS_API)
  16523. /* use RNG to get random port if using windows */
  16524. ready.port = GetRandomPort();
  16525. #endif
  16526. server_args.signal = &ready;
  16527. client_args.signal = &ready;
  16528. server_args.return_code = TEST_FAIL;
  16529. client_args.return_code = TEST_FAIL;
  16530. /* set callbacks to use DH functions */
  16531. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  16532. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  16533. #ifndef WOLFSSL_NO_TLS12
  16534. func_cb_client.method = wolfTLSv1_2_client_method;
  16535. #else
  16536. func_cb_client.method = wolfTLSv1_3_client_method;
  16537. #endif
  16538. client_args.callbacks = &func_cb_client;
  16539. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  16540. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  16541. #ifndef WOLFSSL_NO_TLS12
  16542. func_cb_server.method = wolfTLSv1_2_server_method;
  16543. #else
  16544. func_cb_server.method = wolfTLSv1_3_server_method;
  16545. #endif
  16546. server_args.callbacks = &func_cb_server;
  16547. start_thread(test_server_nofail, &server_args, &serverThread);
  16548. wait_tcp_ready(&server_args);
  16549. test_client_nofail(&client_args, NULL);
  16550. join_thread(serverThread);
  16551. AssertTrue(client_args.return_code);
  16552. AssertTrue(server_args.return_code);
  16553. FreeTcpReady(&ready);
  16554. #ifdef WOLFSSL_TIRTOS
  16555. fdOpenSession(Task_self());
  16556. #endif
  16557. /* now set user ctx to not be 1 so that the callback returns fail case */
  16558. #ifdef WOLFSSL_TIRTOS
  16559. fdOpenSession(Task_self());
  16560. #endif
  16561. XMEMSET(&server_args, 0, sizeof(func_args));
  16562. XMEMSET(&client_args, 0, sizeof(func_args));
  16563. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  16564. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  16565. StartTCP();
  16566. InitTcpReady(&ready);
  16567. #if defined(USE_WINDOWS_API)
  16568. /* use RNG to get random port if using windows */
  16569. ready.port = GetRandomPort();
  16570. #endif
  16571. server_args.signal = &ready;
  16572. client_args.signal = &ready;
  16573. server_args.return_code = TEST_FAIL;
  16574. client_args.return_code = TEST_FAIL;
  16575. /* set callbacks to use DH functions */
  16576. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  16577. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  16578. #ifndef WOLFSSL_NO_TLS12
  16579. func_cb_client.method = wolfTLSv1_2_client_method;
  16580. #else
  16581. func_cb_client.method = wolfTLSv1_3_client_method;
  16582. #endif
  16583. client_args.callbacks = &func_cb_client;
  16584. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  16585. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  16586. #ifndef WOLFSSL_NO_TLS12
  16587. func_cb_server.method = wolfTLSv1_2_server_method;
  16588. #else
  16589. func_cb_server.method = wolfTLSv1_3_server_method;
  16590. #endif
  16591. server_args.callbacks = &func_cb_server;
  16592. start_thread(test_server_nofail, &server_args, &serverThread);
  16593. wait_tcp_ready(&server_args);
  16594. test_client_nofail(&client_args, NULL);
  16595. join_thread(serverThread);
  16596. AssertIntEQ(client_args.return_code, TEST_FAIL);
  16597. AssertIntEQ(server_args.return_code, TEST_FAIL);
  16598. FreeTcpReady(&ready);
  16599. #ifdef WOLFSSL_TIRTOS
  16600. fdOpenSession(Task_self());
  16601. #endif
  16602. printf(resultFmt, passed);
  16603. #endif
  16604. }
  16605. #endif /* HAVE_PK_CALLBACKS */
  16606. #ifdef HAVE_HASHDRBG
  16607. #ifdef TEST_RESEED_INTERVAL
  16608. static int test_wc_RNG_GenerateBlock_Reseed()
  16609. {
  16610. int i, ret;
  16611. WC_RNG rng;
  16612. byte key[32];
  16613. ret = wc_InitRng(&rng);
  16614. if (ret == 0) {
  16615. for(i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  16616. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  16617. if (ret != 0) {
  16618. break;
  16619. }
  16620. }
  16621. }
  16622. wc_FreeRng(&rng);
  16623. return ret;
  16624. }
  16625. #endif /* TEST_RESEED_INTERVAL */
  16626. static int test_wc_RNG_GenerateBlock()
  16627. {
  16628. int i, ret;
  16629. WC_RNG rng;
  16630. byte key[32];
  16631. ret = wc_InitRng(&rng);
  16632. if (ret == 0) {
  16633. for(i = 0; i < 10; i++) {
  16634. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  16635. if (ret != 0) {
  16636. break;
  16637. }
  16638. }
  16639. }
  16640. wc_FreeRng(&rng);
  16641. return ret;
  16642. }
  16643. #endif
  16644. static void test_wolfSSL_X509_CRL(void)
  16645. {
  16646. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  16647. X509_CRL *crl;
  16648. char pem[][100] = {
  16649. "./certs/crl/crl.pem",
  16650. "./certs/crl/crl2.pem",
  16651. "./certs/crl/caEccCrl.pem",
  16652. "./certs/crl/eccCliCRL.pem",
  16653. "./certs/crl/eccSrvCRL.pem",
  16654. ""
  16655. };
  16656. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  16657. char der[][100] = {
  16658. "./certs/crl/crl.der",
  16659. "./certs/crl/crl2.der",
  16660. ""};
  16661. #endif
  16662. XFILE fp;
  16663. int i;
  16664. printf(testingFmt, "test_wolfSSL_X509_CRL");
  16665. for (i = 0; pem[i][0] != '\0'; i++)
  16666. {
  16667. AssertNotNull(fp = XFOPEN(pem[i], "rb"));
  16668. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  16669. AssertNotNull(crl);
  16670. X509_CRL_free(crl);
  16671. XFCLOSE(fp);
  16672. AssertNotNull(fp = XFOPEN(pem[i], "rb"));
  16673. AssertNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL, NULL));
  16674. AssertNotNull(crl);
  16675. X509_CRL_free(crl);
  16676. XFCLOSE(fp);
  16677. }
  16678. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  16679. for(i = 0; der[i][0] != '\0'; i++){
  16680. AssertNotNull(fp = XFOPEN(der[i], "rb"));
  16681. AssertNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  16682. AssertNotNull(crl);
  16683. X509_CRL_free(crl);
  16684. XFCLOSE(fp);
  16685. AssertNotNull(fp = XFOPEN(der[i], "rb"));
  16686. AssertNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  16687. AssertNotNull(crl);
  16688. X509_CRL_free(crl);
  16689. XFCLOSE(fp);
  16690. }
  16691. #endif
  16692. printf(resultFmt, passed);
  16693. #endif
  16694. return;
  16695. }
  16696. static void test_wolfSSL_i2c_ASN1_INTEGER()
  16697. {
  16698. #ifdef OPENSSL_EXTRA
  16699. ASN1_INTEGER *a;
  16700. unsigned char *pp,*tpp;
  16701. int ret;
  16702. a = wolfSSL_ASN1_INTEGER_new();
  16703. /* 40 */
  16704. a->intData[0] = ASN_INTEGER;
  16705. a->intData[1] = 1;
  16706. a->intData[2] = 40;
  16707. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  16708. AssertIntEQ(ret, 1);
  16709. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  16710. DYNAMIC_TYPE_TMP_BUFFER));
  16711. tpp = pp;
  16712. XMEMSET(pp, 0, ret + 1);
  16713. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  16714. pp--;
  16715. AssertIntEQ(*pp, 40);
  16716. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16717. /* 128 */
  16718. a->intData[0] = ASN_INTEGER;
  16719. a->intData[1] = 1;
  16720. a->intData[2] = 128;
  16721. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  16722. AssertIntEQ(ret, 2);
  16723. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  16724. DYNAMIC_TYPE_TMP_BUFFER));
  16725. tpp = pp;
  16726. XMEMSET(pp, 0, ret + 1);
  16727. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  16728. pp--;
  16729. AssertIntEQ(*(pp--), 128);
  16730. AssertIntEQ(*pp, 0);
  16731. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16732. /* -40 */
  16733. a->intData[0] = ASN_INTEGER;
  16734. a->intData[1] = 1;
  16735. a->intData[2] = 40;
  16736. a->negative = 1;
  16737. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  16738. AssertIntEQ(ret, 1);
  16739. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  16740. DYNAMIC_TYPE_TMP_BUFFER));
  16741. tpp = pp;
  16742. XMEMSET(pp, 0, ret + 1);
  16743. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  16744. pp--;
  16745. AssertIntEQ(*pp, 216);
  16746. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16747. /* -128 */
  16748. a->intData[0] = ASN_INTEGER;
  16749. a->intData[1] = 1;
  16750. a->intData[2] = 128;
  16751. a->negative = 1;
  16752. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  16753. AssertIntEQ(ret, 1);
  16754. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  16755. DYNAMIC_TYPE_TMP_BUFFER));
  16756. tpp = pp;
  16757. XMEMSET(pp, 0, ret + 1);
  16758. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  16759. pp--;
  16760. AssertIntEQ(*pp, 128);
  16761. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16762. /* -200 */
  16763. a->intData[0] = ASN_INTEGER;
  16764. a->intData[1] = 1;
  16765. a->intData[2] = 200;
  16766. a->negative = 1;
  16767. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  16768. AssertIntEQ(ret, 2);
  16769. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  16770. DYNAMIC_TYPE_TMP_BUFFER));
  16771. tpp = pp;
  16772. XMEMSET(pp, 0, ret + 1);
  16773. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  16774. pp--;
  16775. AssertIntEQ(*(pp--), 56);
  16776. AssertIntEQ(*pp, 255);
  16777. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16778. wolfSSL_ASN1_INTEGER_free(a);
  16779. printf(resultFmt, passed);
  16780. #endif /* OPENSSL_EXTRA */
  16781. }
  16782. /*----------------------------------------------------------------------------*
  16783. | Main
  16784. *----------------------------------------------------------------------------*/
  16785. void ApiTest(void)
  16786. {
  16787. printf(" Begin API Tests\n");
  16788. AssertIntEQ(test_wolfSSL_Init(), WOLFSSL_SUCCESS);
  16789. /* wolfcrypt initialization tests */
  16790. test_wolfSSL_Method_Allocators();
  16791. #ifndef NO_WOLFSSL_SERVER
  16792. test_wolfSSL_CTX_new(wolfSSLv23_server_method());
  16793. #endif
  16794. test_wolfSSL_CTX_use_certificate_file();
  16795. AssertIntEQ(test_wolfSSL_CTX_use_certificate_buffer(), WOLFSSL_SUCCESS);
  16796. test_wolfSSL_CTX_use_PrivateKey_file();
  16797. test_wolfSSL_CTX_load_verify_locations();
  16798. test_wolfSSL_CTX_trust_peer_cert();
  16799. test_wolfSSL_CTX_SetTmpDH_file();
  16800. test_wolfSSL_CTX_SetTmpDH_buffer();
  16801. test_server_wolfSSL_new();
  16802. test_client_wolfSSL_new();
  16803. test_wolfSSL_SetTmpDH_file();
  16804. test_wolfSSL_SetTmpDH_buffer();
  16805. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  16806. test_wolfSSL_read_write();
  16807. #endif
  16808. test_wolfSSL_dtls_export();
  16809. AssertIntEQ(test_wolfSSL_SetMinVersion(), WOLFSSL_SUCCESS);
  16810. AssertIntEQ(test_wolfSSL_CTX_SetMinVersion(), WOLFSSL_SUCCESS);
  16811. /* TLS extensions tests */
  16812. test_wolfSSL_UseSNI();
  16813. test_wolfSSL_UseMaxFragment();
  16814. test_wolfSSL_UseTruncatedHMAC();
  16815. test_wolfSSL_UseSupportedCurve();
  16816. test_wolfSSL_UseALPN();
  16817. test_wolfSSL_DisableExtendedMasterSecret();
  16818. /* X509 tests */
  16819. test_wolfSSL_X509_NAME_get_entry();
  16820. test_wolfSSL_PKCS12();
  16821. test_wolfSSL_PKCS8();
  16822. test_wolfSSL_PKCS5();
  16823. test_wolfSSL_URI();
  16824. /*OCSP Stapling. */
  16825. AssertIntEQ(test_wolfSSL_UseOCSPStapling(), WOLFSSL_SUCCESS);
  16826. AssertIntEQ(test_wolfSSL_UseOCSPStaplingV2(), WOLFSSL_SUCCESS);
  16827. /* Multicast */
  16828. test_wolfSSL_mcast();
  16829. /* compatibility tests */
  16830. test_wolfSSL_X509_NAME();
  16831. test_wolfSSL_DES();
  16832. test_wolfSSL_certs();
  16833. test_wolfSSL_ASN1_TIME_print();
  16834. test_wolfSSL_ASN1_GENERALIZEDTIME_free();
  16835. test_wolfSSL_private_keys();
  16836. test_wolfSSL_PEM_PrivateKey();
  16837. test_wolfSSL_PEM_RSAPrivateKey();
  16838. test_wolfSSL_tmp_dh();
  16839. test_wolfSSL_ctrl();
  16840. test_wolfSSL_EVP_PKEY_new_mac_key();
  16841. test_wolfSSL_EVP_MD_hmac_signing();
  16842. test_wolfSSL_CTX_add_extra_chain_cert();
  16843. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  16844. test_wolfSSL_ERR_peek_last_error_line();
  16845. #endif
  16846. test_wolfSSL_set_options();
  16847. test_wolfSSL_X509_STORE_CTX();
  16848. test_wolfSSL_msgCb();
  16849. test_wolfSSL_X509_STORE_set_flags();
  16850. test_wolfSSL_X509_LOOKUP_load_file();
  16851. test_wolfSSL_X509_NID();
  16852. test_wolfSSL_X509_STORE_CTX_set_time();
  16853. test_wolfSSL_X509_STORE();
  16854. test_wolfSSL_BN();
  16855. test_wolfSSL_PEM_read_bio();
  16856. test_wolfSSL_BIO();
  16857. test_wolfSSL_ASN1_STRING();
  16858. test_wolfSSL_X509();
  16859. test_wolfSSL_RAND();
  16860. test_wolfSSL_BUF();
  16861. test_wolfSSL_set_tlsext_status_type();
  16862. test_wolfSSL_ASN1_TIME_adj();
  16863. test_wolfSSL_CTX_set_client_CA_list();
  16864. test_wolfSSL_CTX_add_client_CA();
  16865. test_wolfSSL_CTX_set_srp_username();
  16866. test_wolfSSL_CTX_set_srp_password();
  16867. test_wolfSSL_pseudo_rand();
  16868. test_wolfSSL_PKCS8_Compat();
  16869. test_wolfSSL_ERR_put_error();
  16870. test_wolfSSL_HMAC();
  16871. test_wolfSSL_OBJ();
  16872. test_wolfSSL_X509_NAME_ENTRY();
  16873. test_wolfSSL_BIO_gets();
  16874. test_wolfSSL_d2i_PUBKEY();
  16875. test_wolfSSL_BIO_write();
  16876. test_wolfSSL_SESSION();
  16877. test_wolfSSL_DES_ecb_encrypt();
  16878. test_wolfSSL_sk_GENERAL_NAME();
  16879. test_wolfSSL_MD4();
  16880. test_wolfSSL_RSA();
  16881. test_wolfSSL_RSA_DER();
  16882. test_wolfSSL_verify_depth();
  16883. test_wolfSSL_HMAC_CTX();
  16884. test_wolfSSL_msg_callback();
  16885. test_wolfSSL_SHA();
  16886. test_wolfSSL_DH_1536_prime();
  16887. test_wolfSSL_AES_ecb_encrypt();
  16888. test_wolfSSL_SHA256();
  16889. test_wolfSSL_X509_get_serialNumber();
  16890. test_wolfSSL_X509_CRL();
  16891. test_wolfSSL_OPENSSL_add_all_algorithms();
  16892. test_wolfSSL_ASN1_STRING_print_ex();
  16893. test_wolfSSL_ASN1_TIME_to_generalizedtime();
  16894. test_wolfSSL_i2c_ASN1_INTEGER();
  16895. test_wolfSSL_X509_check_ca();
  16896. /* test the no op functions for compatibility */
  16897. test_no_op_functions();
  16898. AssertIntEQ(test_wolfSSL_Cleanup(), WOLFSSL_SUCCESS);
  16899. /* wolfCrypt ASN tests */
  16900. test_wc_GetPkcs8TraditionalOffset();
  16901. /* wolfCrypt ECC tests */
  16902. test_wc_ecc_get_curve_size_from_name();
  16903. test_wc_ecc_get_curve_id_from_name();
  16904. test_wc_ecc_get_curve_id_from_params();
  16905. #ifdef WOLFSSL_TLS13
  16906. /* TLS v1.3 API tests */
  16907. test_tls13_apis();
  16908. #endif
  16909. #ifndef NO_CERTS
  16910. /* Bad certificate signature tests */
  16911. AssertIntEQ(test_EccSigFailure_cm(), ASN_SIG_CONFIRM_E);
  16912. AssertIntEQ(test_RsaSigFailure_cm(), ASN_SIG_CONFIRM_E);
  16913. #endif /* NO_CERTS */
  16914. #ifdef HAVE_PK_CALLBACKS
  16915. /* public key callback tests */
  16916. test_DhCallbacks();
  16917. #endif
  16918. /*wolfcrypt */
  16919. printf("\n-----------------wolfcrypt unit tests------------------\n");
  16920. AssertFalse(test_wolfCrypt_Init());
  16921. AssertFalse(test_wc_InitMd5());
  16922. AssertFalse(test_wc_Md5Update());
  16923. AssertFalse(test_wc_Md5Final());
  16924. AssertFalse(test_wc_InitSha());
  16925. AssertFalse(test_wc_ShaUpdate());
  16926. AssertFalse(test_wc_ShaFinal());
  16927. AssertFalse(test_wc_InitSha256());
  16928. AssertFalse(test_wc_Sha256Update());
  16929. AssertFalse(test_wc_Sha256Final());
  16930. AssertFalse(test_wc_InitSha512());
  16931. AssertFalse(test_wc_Sha512Update());
  16932. AssertFalse(test_wc_Sha512Final());
  16933. AssertFalse(test_wc_InitSha384());
  16934. AssertFalse(test_wc_Sha384Update());
  16935. AssertFalse(test_wc_Sha384Final());
  16936. AssertFalse(test_wc_InitSha224());
  16937. AssertFalse(test_wc_Sha224Update());
  16938. AssertFalse(test_wc_Sha224Final());
  16939. AssertFalse(test_wc_InitBlake2b());
  16940. AssertFalse(test_wc_InitRipeMd());
  16941. AssertFalse(test_wc_RipeMdUpdate());
  16942. AssertFalse(test_wc_RipeMdFinal());
  16943. AssertIntEQ(test_wc_InitSha3(), 0);
  16944. AssertIntEQ(testing_wc_Sha3_Update(), 0);
  16945. AssertIntEQ(test_wc_Sha3_224_Final(), 0);
  16946. AssertIntEQ(test_wc_Sha3_256_Final(), 0);
  16947. AssertIntEQ(test_wc_Sha3_384_Final(), 0);
  16948. AssertIntEQ(test_wc_Sha3_512_Final(), 0);
  16949. AssertIntEQ(test_wc_Sha3_224_Copy(), 0);
  16950. AssertIntEQ(test_wc_Sha3_256_Copy(), 0);
  16951. AssertIntEQ(test_wc_Sha3_384_Copy(), 0);
  16952. AssertIntEQ(test_wc_Sha3_512_Copy(), 0);
  16953. AssertFalse(test_wc_Md5HmacSetKey());
  16954. AssertFalse(test_wc_Md5HmacUpdate());
  16955. AssertFalse(test_wc_Md5HmacFinal());
  16956. AssertFalse(test_wc_ShaHmacSetKey());
  16957. AssertFalse(test_wc_ShaHmacUpdate());
  16958. AssertFalse(test_wc_ShaHmacFinal());
  16959. AssertFalse(test_wc_Sha224HmacSetKey());
  16960. AssertFalse(test_wc_Sha224HmacUpdate());
  16961. AssertFalse(test_wc_Sha224HmacFinal());
  16962. AssertFalse(test_wc_Sha256HmacSetKey());
  16963. AssertFalse(test_wc_Sha256HmacUpdate());
  16964. AssertFalse(test_wc_Sha256HmacFinal());
  16965. AssertFalse(test_wc_Sha384HmacSetKey());
  16966. AssertFalse(test_wc_Sha384HmacUpdate());
  16967. AssertFalse(test_wc_Sha384HmacFinal());
  16968. AssertIntEQ(test_wc_HashInit(), 0);
  16969. AssertIntEQ(test_wc_InitCmac(), 0);
  16970. AssertIntEQ(test_wc_CmacUpdate(), 0);
  16971. AssertIntEQ(test_wc_CmacFinal(), 0);
  16972. AssertIntEQ(test_wc_AesCmacGenerate(), 0);
  16973. AssertIntEQ(test_wc_Des3_SetIV(), 0);
  16974. AssertIntEQ(test_wc_Des3_SetKey(), 0);
  16975. AssertIntEQ(test_wc_Des3_CbcEncryptDecrypt(), 0);
  16976. AssertIntEQ(test_wc_Des3_CbcEncryptDecryptWithKey(), 0);
  16977. AssertIntEQ(test_wc_IdeaSetKey(), 0);
  16978. AssertIntEQ(test_wc_IdeaSetIV(), 0);
  16979. AssertIntEQ(test_wc_IdeaCipher(), 0);
  16980. AssertIntEQ(test_wc_IdeaCbcEncyptDecrypt(), 0);
  16981. AssertIntEQ(test_wc_Chacha_SetKey(), 0);
  16982. AssertIntEQ(test_wc_Chacha_Process(), 0);
  16983. AssertIntEQ(test_wc_ChaCha20Poly1305_aead(), 0);
  16984. AssertIntEQ(test_wc_Poly1305SetKey(), 0);
  16985. AssertIntEQ(test_wc_CamelliaSetKey(), 0);
  16986. AssertIntEQ(test_wc_CamelliaSetIV(), 0);
  16987. AssertIntEQ(test_wc_CamelliaEncryptDecryptDirect(), 0);
  16988. AssertIntEQ(test_wc_CamelliaCbcEncryptDecrypt(), 0);
  16989. AssertIntEQ(test_wc_RabbitSetKey(), 0);
  16990. AssertIntEQ(test_wc_RabbitProcess(), 0);
  16991. AssertIntEQ(test_wc_Arc4SetKey(), 0);
  16992. AssertIntEQ(test_wc_Arc4Process(), 0);
  16993. AssertIntEQ(test_wc_AesSetKey(), 0);
  16994. AssertIntEQ(test_wc_AesSetIV(), 0);
  16995. AssertIntEQ(test_wc_AesCbcEncryptDecrypt(), 0);
  16996. AssertIntEQ(test_wc_AesCtrEncryptDecrypt(), 0);
  16997. AssertIntEQ(test_wc_AesGcmSetKey(), 0);
  16998. AssertIntEQ(test_wc_AesGcmEncryptDecrypt(), 0);
  16999. AssertIntEQ(test_wc_GmacSetKey(), 0);
  17000. AssertIntEQ(test_wc_GmacUpdate(), 0);
  17001. AssertIntEQ(test_wc_InitRsaKey(), 0);
  17002. AssertIntEQ(test_wc_RsaPrivateKeyDecode(), 0);
  17003. AssertIntEQ(test_wc_RsaPublicKeyDecode(), 0);
  17004. AssertIntEQ(test_wc_RsaPublicKeyDecodeRaw(), 0);
  17005. AssertIntEQ(test_wc_MakeRsaKey(), 0);
  17006. AssertIntEQ(test_wc_SetKeyUsage (), 0);
  17007. AssertIntEQ(test_wc_RsaKeyToDer(), 0);
  17008. AssertIntEQ(test_wc_RsaKeyToPublicDer(), 0);
  17009. AssertIntEQ(test_wc_RsaPublicEncryptDecrypt(), 0);
  17010. AssertIntEQ(test_wc_RsaPublicEncryptDecrypt_ex(), 0);
  17011. AssertIntEQ(test_wc_RsaEncryptSize(), 0);
  17012. AssertIntEQ(test_wc_RsaSSL_SignVerify(), 0);
  17013. AssertIntEQ(test_wc_RsaFlattenPublicKey(), 0);
  17014. AssertIntEQ(test_RsaDecryptBoundsCheck(), 0);
  17015. AssertIntEQ(test_wc_AesCcmSetKey(), 0);
  17016. AssertIntEQ(test_wc_AesCcmEncryptDecrypt(), 0);
  17017. AssertIntEQ(test_wc_Hc128_SetKey(), 0);
  17018. AssertIntEQ(test_wc_Hc128_Process(), 0);
  17019. AssertIntEQ(test_wc_InitDsaKey(), 0);
  17020. AssertIntEQ(test_wc_DsaSignVerify(), 0);
  17021. AssertIntEQ(test_wc_DsaPublicPrivateKeyDecode(), 0);
  17022. AssertIntEQ(test_wc_MakeDsaKey(), 0);
  17023. AssertIntEQ(test_wc_DsaKeyToDer(), 0);
  17024. AssertIntEQ(test_wc_DsaImportParamsRaw(), 0);
  17025. AssertIntEQ(test_wc_DsaExportParamsRaw(), 0);
  17026. AssertIntEQ(test_wc_DsaExportKeyRaw(), 0);
  17027. AssertIntEQ(test_wc_SignatureGetSize_ecc(), 0);
  17028. AssertIntEQ(test_wc_SignatureGetSize_rsa(), 0);
  17029. #ifdef OPENSSL_EXTRA
  17030. /*wolfSSS_EVP_get_cipherbynid test*/
  17031. test_wolfSSL_EVP_get_cipherbynid();
  17032. test_wolfSSL_EC();
  17033. #endif
  17034. #ifdef HAVE_HASHDRBG
  17035. #ifdef TEST_RESEED_INTERVAL
  17036. AssertIntEQ(test_wc_RNG_GenerateBlock_Reseed(), 0);
  17037. #endif
  17038. AssertIntEQ(test_wc_RNG_GenerateBlock(), 0);
  17039. #endif
  17040. AssertIntEQ(test_wc_ed25519_make_key(), 0);
  17041. AssertIntEQ(test_wc_ed25519_init(), 0);
  17042. AssertIntEQ(test_wc_ed25519_sign_msg(), 0);
  17043. AssertIntEQ(test_wc_ed25519_import_public(), 0);
  17044. AssertIntEQ(test_wc_ed25519_import_private_key(), 0);
  17045. AssertIntEQ(test_wc_ed25519_export(), 0);
  17046. AssertIntEQ(test_wc_ed25519_size(), 0);
  17047. AssertIntEQ(test_wc_ed25519_exportKey(), 0);
  17048. AssertIntEQ(test_wc_curve25519_init(), 0);
  17049. AssertIntEQ(test_wc_ecc_make_key(), 0);
  17050. AssertIntEQ(test_wc_ecc_init(), 0);
  17051. AssertIntEQ(test_wc_ecc_check_key(), 0);
  17052. AssertIntEQ(test_wc_ecc_size(), 0);
  17053. AssertIntEQ(test_wc_ecc_signVerify_hash(), 0);
  17054. AssertIntEQ(test_wc_ecc_shared_secret(), 0);
  17055. AssertIntEQ(test_wc_ecc_export_x963(), 0);
  17056. AssertIntEQ(test_wc_ecc_export_x963_ex(), 0);
  17057. AssertIntEQ(test_wc_ecc_import_x963(), 0);
  17058. AssertIntEQ(ecc_import_private_key(), 0);
  17059. AssertIntEQ(test_wc_ecc_export_private_only(), 0);
  17060. AssertIntEQ(test_wc_ecc_rs_to_sig(), 0);
  17061. AssertIntEQ(test_wc_ecc_import_raw(), 0);
  17062. AssertIntEQ(test_wc_ecc_sig_size(), 0);
  17063. AssertIntEQ(test_wc_ecc_ctx_new(), 0);
  17064. AssertIntEQ(test_wc_ecc_ctx_reset(), 0);
  17065. AssertIntEQ(test_wc_ecc_ctx_set_peer_salt(), 0);
  17066. AssertIntEQ(test_wc_ecc_ctx_set_info(), 0);
  17067. AssertIntEQ(test_wc_ecc_encryptDecrypt(), 0);
  17068. AssertIntEQ(test_wc_ecc_del_point(), 0);
  17069. AssertIntEQ(test_wc_ecc_pointFns(), 0);
  17070. AssertIntEQ(test_wc_ecc_shared_secret_ssh(), 0);
  17071. AssertIntEQ(test_wc_ecc_verify_hash_ex(), 0);
  17072. AssertIntEQ(test_wc_ecc_mulmod(), 0);
  17073. AssertIntEQ(test_wc_ecc_is_valid_idx(), 0);
  17074. test_wc_PKCS7_New();
  17075. test_wc_PKCS7_Init();
  17076. test_wc_PKCS7_InitWithCert();
  17077. test_wc_PKCS7_EncodeData();
  17078. test_wc_PKCS7_EncodeSignedData();
  17079. test_wc_PKCS7_VerifySignedData();
  17080. test_wc_PKCS7_EncodeDecodeEnvelopedData();
  17081. test_wc_PKCS7_EncodeEncryptedData();
  17082. printf(" End API Tests\n");
  17083. }