api.c 581 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189
  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_Init()
  12020. */
  12021. static void test_wc_PKCS7_Init (void)
  12022. {
  12023. #if defined(HAVE_PKCS7)
  12024. PKCS7 pkcs7;
  12025. void* heap = NULL;
  12026. printf(testingFmt, "wc_PKCS7_Init()");
  12027. AssertIntEQ(wc_PKCS7_Init(&pkcs7, heap, devId), 0);
  12028. /* Pass in bad args. */
  12029. AssertIntEQ(wc_PKCS7_Init(NULL, heap, devId), BAD_FUNC_ARG);
  12030. printf(resultFmt, passed);
  12031. wc_PKCS7_Free(&pkcs7);
  12032. #endif
  12033. } /* END test-wc_PKCS7_Init */
  12034. /*
  12035. * Testing wc_PKCS7_InitWithCert()
  12036. */
  12037. static void test_wc_PKCS7_InitWithCert (void)
  12038. {
  12039. #if defined(HAVE_PKCS7)
  12040. PKCS7 pkcs7;
  12041. #ifndef NO_RSA
  12042. #if defined(USE_CERT_BUFFERS_2048)
  12043. unsigned char cert[sizeof_client_cert_der_2048];
  12044. int certSz = (int)sizeof(cert);
  12045. XMEMSET(cert, 0, certSz);
  12046. XMEMCPY(cert, client_cert_der_2048, sizeof_client_cert_der_2048);
  12047. #elif defined(USE_CERT_BUFFERS_1024)
  12048. unsigned char cert[sizeof_client_cert_der_1024];
  12049. int certSz = (int)sizeof(cert);
  12050. XMEMSET(cert, 0, certSz);
  12051. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  12052. #else
  12053. unsigned char cert[ONEK_BUF];
  12054. FILE* fp;
  12055. int certSz;
  12056. fp = fopen("./certs/1024/client-cert.der", "rb");
  12057. AssertNotNull(fp);
  12058. certSz = fread(cert, 1, sizeof_client_cert_der_1024, fp);
  12059. fclose(fp);
  12060. #endif
  12061. #elif defined(HAVE_ECC)
  12062. #if defined(USE_CERT_BUFFERS_256)
  12063. unsigned char cert[sizeof_cliecc_cert_der_256];
  12064. int certSz = (int)sizeof(cert);
  12065. XMEMSET(cert, 0, certSz);
  12066. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  12067. #else
  12068. unsigned char cert[ONEK_BUF];
  12069. FILE* fp;
  12070. int certSz;
  12071. fp = fopen("./certs/client-ecc-cert.der", "rb");
  12072. AssertNotNull(fp);
  12073. certSz = fread(cert, 1, sizeof_cliecc_cert_der_256, fp);
  12074. fclose(fp);
  12075. #endif
  12076. #else
  12077. #error PKCS7 requires ECC or RSA
  12078. #endif
  12079. printf(testingFmt, "wc_PKCS7_InitWithCert()");
  12080. /* If initialization is not successful, it's free'd in init func. */
  12081. AssertIntEQ(wc_PKCS7_InitWithCert(&pkcs7, (byte*)cert, (word32)certSz), 0);
  12082. wc_PKCS7_Free(&pkcs7);
  12083. /* Valid initialization usage. */
  12084. AssertIntEQ(wc_PKCS7_InitWithCert(&pkcs7, NULL, 0), 0);
  12085. /* Pass in bad args. No need free for null checks, free at end.*/
  12086. AssertIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  12087. BAD_FUNC_ARG);
  12088. AssertIntEQ(wc_PKCS7_InitWithCert(&pkcs7, NULL, (word32)certSz),
  12089. BAD_FUNC_ARG);
  12090. printf(resultFmt, passed);
  12091. wc_PKCS7_Free(&pkcs7);
  12092. #endif
  12093. } /* END test_wc_PKCS7_InitWithCert */
  12094. /*
  12095. * Testing wc_PKCS7_EncodeData()
  12096. */
  12097. static void test_wc_PKCS7_EncodeData (void)
  12098. {
  12099. #if defined(HAVE_PKCS7)
  12100. PKCS7 pkcs7;
  12101. byte output[FOURK_BUF];
  12102. byte data[] = "My encoded DER cert.";
  12103. #ifndef NO_RSA
  12104. #if defined(USE_CERT_BUFFERS_2048)
  12105. unsigned char cert[sizeof_client_cert_der_2048];
  12106. unsigned char key[sizeof_client_key_der_2048];
  12107. int certSz = (int)sizeof(cert);
  12108. int keySz = (int)sizeof(key);
  12109. XMEMSET(cert, 0, certSz);
  12110. XMEMSET(key, 0, keySz);
  12111. XMEMCPY(cert, client_cert_der_2048, certSz);
  12112. XMEMCPY(key, client_key_der_2048, keySz);
  12113. #elif defined(USE_CERT_BUFFERS_1024)
  12114. unsigned char cert[sizeof_client_cert_der_1024];
  12115. unsigned char key[sizeof_client_key_der_1024];
  12116. int certSz = (int)sizeof(cert);
  12117. int keySz = (int)sizeof(key);
  12118. XMEMSET(cert, 0, certSz);
  12119. XMEMSET(key, 0, keySz);
  12120. XMEMCPY(cert, client_cert_der_1024, certSz);
  12121. XMEMCPY(key, client_key_der_1024, keySz);
  12122. #else
  12123. unsigned char cert[ONEK_BUF];
  12124. unsigned char key[ONEK_BUF];
  12125. FILE* fp;
  12126. int certSz;
  12127. int keySz;
  12128. fp = fopen("./certs/1024/client-cert.der", "rb");
  12129. AssertNotNull(fp);
  12130. certSz = fread(cert, 1, sizeof_client_cert_der_1024, fp);
  12131. fclose(fp);
  12132. fp = fopen("./certs/1024/client-key.der", "rb");
  12133. AssertNotNull(fp);
  12134. keySz = fread(key, 1, sizeof_client_key_der_1024, fp);
  12135. fclose(fp);
  12136. #endif
  12137. #elif defined(HAVE_ECC)
  12138. #if defined(USE_CERT_BUFFERS_256)
  12139. unsigned char cert[sizeof_cliecc_cert_der_256];
  12140. unsigned char key[sizeof_ecc_clikey_der_256];
  12141. int certSz = (int)sizeof(cert);
  12142. int keySz = (int)sizeof(key);
  12143. XMEMSET(cert, 0, certSz);
  12144. XMEMSET(key, 0, keySz);
  12145. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  12146. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  12147. #else
  12148. unsigned char cert[ONEK_BUF];
  12149. unsigned char key[ONEK_BUF];
  12150. FILE* fp;
  12151. int certSz, keySz;
  12152. fp = fopen("./certs/client-ecc-cert.der", "rb");
  12153. AssertNotNull(fp);
  12154. certSz = fread(cert, 1, sizeof_cliecc_cert_der_256, fp);
  12155. fclose(fp);
  12156. fp = fopen("./certs/client-ecc-key.der", "rb");
  12157. AssertNotNull(fp);
  12158. keySz = fread(key, 1, sizeof_ecc_clikey_der_256, fp);
  12159. fclose(fp);
  12160. #endif
  12161. #endif
  12162. XMEMSET(output, 0, sizeof(output));
  12163. AssertIntEQ(wc_PKCS7_InitWithCert(&pkcs7, (byte*)cert, certSz), 0);
  12164. printf(testingFmt, "wc_PKCS7_EncodeData()");
  12165. pkcs7.content = data;
  12166. pkcs7.contentSz = sizeof(data);
  12167. pkcs7.privateKey = key;
  12168. pkcs7.privateKeySz = keySz;
  12169. AssertIntGT(wc_PKCS7_EncodeData(&pkcs7, output, (word32)sizeof(output)), 0);
  12170. /* Test bad args. */
  12171. AssertIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  12172. BAD_FUNC_ARG);
  12173. AssertIntEQ(wc_PKCS7_EncodeData(&pkcs7, NULL, (word32)sizeof(output)),
  12174. BAD_FUNC_ARG);
  12175. AssertIntEQ(wc_PKCS7_EncodeData(&pkcs7, output, 5), BUFFER_E);
  12176. printf(resultFmt, passed);
  12177. wc_PKCS7_Free(&pkcs7);
  12178. #endif
  12179. } /* END test_wc_PKCS7_EncodeData */
  12180. /*
  12181. * Testing wc_PKCS7_EncodeSignedData()
  12182. */
  12183. static void test_wc_PKCS7_EncodeSignedData (void)
  12184. {
  12185. #if defined(HAVE_PKCS7)
  12186. PKCS7 pkcs7;
  12187. WC_RNG rng;
  12188. byte output[FOURK_BUF];
  12189. byte badOut[0];
  12190. word32 outputSz = (word32)sizeof(output);
  12191. word32 badOutSz = (word32)sizeof(badOut);
  12192. byte data[] = "Test data to encode.";
  12193. #ifndef NO_RSA
  12194. #if defined(USE_CERT_BUFFERS_2048)
  12195. byte key[sizeof_client_key_der_2048];
  12196. byte cert[sizeof_client_cert_der_2048];
  12197. word32 keySz = (word32)sizeof(key);
  12198. word32 certSz = (word32)sizeof(cert);
  12199. XMEMSET(key, 0, keySz);
  12200. XMEMSET(cert, 0, certSz);
  12201. XMEMCPY(key, client_key_der_2048, keySz);
  12202. XMEMCPY(cert, client_cert_der_2048, certSz);
  12203. #elif defined(USE_CERT_BUFFERS_1024)
  12204. byte key[sizeof_client_key_der_1024];
  12205. byte cert[sizeof_client_cert_der_1024];
  12206. word32 keySz = (word32)sizeof(key);
  12207. word32 certSz = (word32)sizeof(cert);
  12208. XMEMSET(key, 0, keySz);
  12209. XMEMSET(cert, 0, certSz);
  12210. XMEMCPY(key, client_key_der_1024, keySz);
  12211. XMEMCPY(cert, client_cert_der_1024, certSz);
  12212. #else
  12213. unsigned char cert[ONEK_BUF];
  12214. unsigned char key[ONEK_BUF];
  12215. FILE* fp;
  12216. int certSz;
  12217. int keySz;
  12218. fp = fopen("./certs/1024/client-cert.der", "rb");
  12219. AssertNotNull(fp);
  12220. certSz = fread(cert, 1, sizeof_client_cert_der_1024, fp);
  12221. fclose(fp);
  12222. fp = fopen("./certs/1024/client-key.der", "rb");
  12223. AssertNotNull(fp);
  12224. keySz = fread(key, 1, sizeof_client_key_der_1024, fp);
  12225. fclose(fp);
  12226. #endif
  12227. #elif defined(HAVE_ECC)
  12228. #if defined(USE_CERT_BUFFERS_256)
  12229. unsigned char cert[sizeof_cliecc_cert_der_256];
  12230. unsigned char key[sizeof_ecc_clikey_der_256];
  12231. int certSz = (int)sizeof(cert);
  12232. int keySz = (int)sizeof(key);
  12233. XMEMSET(cert, 0, certSz);
  12234. XMEMSET(key, 0, keySz);
  12235. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  12236. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  12237. #else
  12238. unsigned char cert[ONEK_BUF];
  12239. unsigned char key[ONEK_BUF];
  12240. FILE* fp;
  12241. int certSz, keySz;
  12242. fp = fopen("./certs/client-ecc-cert.der", "rb");
  12243. AssertNotNull(fp);
  12244. certSz = fread(cert, 1, sizeof_cliecc_cert_der_256, fp);
  12245. fclose(fp);
  12246. fp = fopen("./certs/client-ecc-key.der", "rb");
  12247. AssertNotNull(fp);
  12248. keySz = fread(key, 1, sizeof_ecc_clikey_der_256, fp);
  12249. fclose(fp);
  12250. #endif
  12251. #endif
  12252. XMEMSET(output, 0, outputSz);
  12253. AssertIntEQ(wc_InitRng(&rng), 0);
  12254. AssertIntEQ(wc_PKCS7_InitWithCert(&pkcs7, cert, certSz), 0);
  12255. printf(testingFmt, "wc_PKCS7_EncodeSignedData()");
  12256. pkcs7.content = data;
  12257. pkcs7.contentSz = (word32)sizeof(data);
  12258. pkcs7.privateKey = key;
  12259. pkcs7.privateKeySz = (word32)sizeof(key);
  12260. pkcs7.encryptOID = RSAk;
  12261. pkcs7.hashOID = SHAh;
  12262. pkcs7.rng = &rng;
  12263. pkcs7.devId = INVALID_DEVID;
  12264. AssertIntGT(wc_PKCS7_EncodeSignedData(&pkcs7, output, outputSz), 0);
  12265. wc_PKCS7_Free(&pkcs7);
  12266. AssertIntEQ(wc_PKCS7_InitWithCert(&pkcs7, NULL, 0), 0);
  12267. AssertIntEQ(wc_PKCS7_VerifySignedData(&pkcs7, output, outputSz), 0);
  12268. /* Pass in bad args. */
  12269. AssertIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  12270. AssertIntEQ(wc_PKCS7_EncodeSignedData(&pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  12271. AssertIntEQ(wc_PKCS7_EncodeSignedData(&pkcs7, badOut,
  12272. badOutSz), BAD_FUNC_ARG);
  12273. printf(resultFmt, passed);
  12274. wc_PKCS7_Free(&pkcs7);
  12275. wc_FreeRng(&rng);
  12276. #endif
  12277. } /* END test_wc_PKCS7_EncodeSignedData */
  12278. /*
  12279. * Testing wc_PKCS_VerifySignedData()
  12280. */
  12281. static void test_wc_PKCS7_VerifySignedData(void)
  12282. {
  12283. #if defined(HAVE_PKCS7)
  12284. PKCS7 pkcs7;
  12285. WC_RNG rng;
  12286. byte output[FOURK_BUF];
  12287. byte badOut[0];
  12288. word32 outputSz = (word32)sizeof(output);
  12289. word32 badOutSz = (word32)sizeof(badOut);
  12290. byte data[] = "Test data to encode.";
  12291. #ifndef NO_RSA
  12292. #if defined(USE_CERT_BUFFERS_2048)
  12293. byte key[sizeof_client_key_der_2048];
  12294. byte cert[sizeof_client_cert_der_2048];
  12295. word32 keySz = (word32)sizeof(key);
  12296. word32 certSz = (word32)sizeof(cert);
  12297. XMEMSET(key, 0, keySz);
  12298. XMEMSET(cert, 0, certSz);
  12299. XMEMCPY(key, client_key_der_2048, keySz);
  12300. XMEMCPY(cert, client_cert_der_2048, certSz);
  12301. #elif defined(USE_CERT_BUFFERS_1024)
  12302. byte key[sizeof_client_key_der_1024];
  12303. byte cert[sizeof_client_cert_der_1024];
  12304. word32 keySz = (word32)sizeof(key);
  12305. word32 certSz = (word32)sizeof(cert);
  12306. XMEMSET(key, 0, keySz);
  12307. XMEMSET(cert, 0, certSz);
  12308. XMEMCPY(key, client_key_der_1024, keySz);
  12309. XMEMCPY(cert, client_cert_der_1024, certSz);
  12310. #else
  12311. unsigned char cert[ONEK_BUF];
  12312. unsigned char key[ONEK_BUF];
  12313. FILE* fp;
  12314. int certSz;
  12315. int keySz;
  12316. fp = fopen("./certs/1024/client-cert.der", "rb");
  12317. AssertNotNull(fp);
  12318. certSz = fread(cert, 1, sizeof_client_cert_der_1024, fp);
  12319. fclose(fp);
  12320. fp = fopen("./certs/1024/client-key.der", "rb");
  12321. AssertNotNull(fp);
  12322. keySz = fread(key, 1, sizeof_client_key_der_1024, fp);
  12323. fclose(fp);
  12324. #endif
  12325. #elif defined(HAVE_ECC)
  12326. #if defined(USE_CERT_BUFFERS_256)
  12327. unsigned char cert[sizeof_cliecc_cert_der_256];
  12328. unsigned char key[sizeof_ecc_clikey_der_256];
  12329. int certSz = (int)sizeof(cert);
  12330. int keySz = (int)sizeof(key);
  12331. XMEMSET(cert, 0, certSz);
  12332. XMEMSET(key, 0, keySz);
  12333. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  12334. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  12335. #else
  12336. unsigned char cert[ONEK_BUF];
  12337. unsigned char key[ONEK_BUF];
  12338. FILE* fp;
  12339. int certSz, keySz;
  12340. fp = fopen("./certs/client-ecc-cert.der", "rb");
  12341. AssertNotNull(fp);
  12342. certSz = fread(cert, 1, sizeof_cliecc_cert_der_256, fp);
  12343. fclose(fp);
  12344. fp = fopen("./certs/client-ecc-key.der", "rb");
  12345. AssertNotNull(fp);
  12346. keySz = fread(key, 1, sizeof_ecc_clikey_der_256, fp);
  12347. fclose(fp);
  12348. #endif
  12349. #endif
  12350. XMEMSET(output, 0, outputSz);
  12351. AssertIntEQ(wc_InitRng(&rng), 0);
  12352. AssertIntEQ(wc_PKCS7_InitWithCert(&pkcs7, cert, certSz), 0);
  12353. printf(testingFmt, "wc_PKCS7_VerifySignedData()");
  12354. pkcs7.content = data;
  12355. pkcs7.contentSz = (word32)sizeof(data);
  12356. pkcs7.privateKey = key;
  12357. pkcs7.privateKeySz = (word32)sizeof(key);
  12358. pkcs7.encryptOID = RSAk;
  12359. pkcs7.hashOID = SHAh;
  12360. pkcs7.rng = &rng;
  12361. AssertIntGT(wc_PKCS7_EncodeSignedData(&pkcs7, output, outputSz), 0);
  12362. wc_PKCS7_Free(&pkcs7);
  12363. AssertIntEQ(wc_PKCS7_InitWithCert(&pkcs7, NULL, 0), 0);
  12364. AssertIntEQ(wc_PKCS7_VerifySignedData(&pkcs7, output, outputSz), 0);
  12365. /* Test bad args. */
  12366. AssertIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  12367. AssertIntEQ(wc_PKCS7_VerifySignedData(&pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  12368. AssertIntEQ(wc_PKCS7_VerifySignedData(&pkcs7, badOut,
  12369. badOutSz), BAD_FUNC_ARG);
  12370. printf(resultFmt, passed);
  12371. wc_PKCS7_Free(&pkcs7);
  12372. wc_FreeRng(&rng);
  12373. #endif
  12374. } /* END test_wc_PKCS7_VerifySignedData() */
  12375. /*
  12376. * Testing wc_PKCS7_EncodeEnvelopedData()
  12377. */
  12378. static void test_wc_PKCS7_EncodeDecodeEnvelopedData (void)
  12379. {
  12380. #if defined(HAVE_PKCS7)
  12381. PKCS7 pkcs7;
  12382. word32 tempWrd32 = 0;
  12383. byte* tmpBytePtr = NULL;
  12384. const char input[] = "Test data to encode.";
  12385. int i;
  12386. int testSz = 0;
  12387. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  12388. !defined(NO_SHA256) || !defined(NO_SHA512)))
  12389. byte* rsaCert = NULL;
  12390. byte* rsaPrivKey = NULL;
  12391. word32 rsaCertSz;
  12392. word32 rsaPrivKeySz;
  12393. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  12394. !defined(USE_CERT_BUFFERS_2048) )
  12395. static const char* rsaClientCert = "./certs/client-cert.der";
  12396. static const char* rsaClientKey = "./certs/client-key.der";
  12397. rsaCertSz = (word32)sizeof(rsaClientCert);
  12398. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  12399. #endif
  12400. #endif
  12401. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  12402. !defined(NO_SHA256) || !defined(NO_SHA512)))
  12403. byte* eccCert = NULL;
  12404. byte* eccPrivKey = NULL;
  12405. word32 eccCertSz;
  12406. word32 eccPrivKeySz;
  12407. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  12408. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  12409. static const char* eccClientKey = "./certs/ecc-client-key.der";
  12410. #endif
  12411. #endif
  12412. /* Generic buffer size. */
  12413. byte output[ONEK_BUF];
  12414. byte decoded[sizeof(input)/sizeof(char)];
  12415. int decodedSz = 0;
  12416. #ifndef NO_FILESYSTEM
  12417. FILE* certFile;
  12418. FILE* keyFile;
  12419. #endif
  12420. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  12421. !defined(NO_SHA256) || !defined(NO_SHA512)))
  12422. /* RSA certs and keys. */
  12423. #if defined(USE_CERT_BUFFERS_1024)
  12424. /* Allocate buffer space. */
  12425. AssertNotNull(rsaCert =
  12426. (byte*)XMALLOC(ONEK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  12427. /* Init buffer. */
  12428. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  12429. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  12430. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(ONEK_BUF, HEAP_HINT,
  12431. DYNAMIC_TYPE_TMP_BUFFER));
  12432. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  12433. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  12434. #elif defined(USE_CERT_BUFFERS_2048)
  12435. /* Allocate buffer */
  12436. AssertNotNull(rsaCert =
  12437. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  12438. /* Init buffer. */
  12439. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  12440. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  12441. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  12442. DYNAMIC_TYPE_TMP_BUFFER));
  12443. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  12444. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  12445. #else
  12446. /* File system. */
  12447. certFile = fopen(rsaClientCert, "rb");
  12448. AssertNotNull(certFile);
  12449. rsaCertSz = (word32)FOURK_BUF;
  12450. AssertNotNull(rsaCert =
  12451. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  12452. rsaCertSz = (word32)fread(rsaCert, 1, rsaCertSz, certFile);
  12453. fclose(certFile);
  12454. keyFile = fopen(rsaClientKey, "rb");
  12455. AssertNotNull(keyFile);
  12456. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  12457. DYNAMIC_TYPE_TMP_BUFFER));
  12458. rsaPrivKeySz = (word32)FOURK_BUF;
  12459. rsaPrivKeySz = (word32)fread(rsaPrivKey, 1, rsaPrivKeySz, keyFile);
  12460. fclose(keyFile);
  12461. #endif /* USE_CERT_BUFFERS */
  12462. #endif /* NO_RSA */
  12463. /* ECC */
  12464. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  12465. !defined(NO_SHA256) || !defined(NO_SHA512)))
  12466. #ifdef USE_CERT_BUFFERS_256
  12467. AssertNotNull(eccCert =
  12468. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  12469. /* Init buffer. */
  12470. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  12471. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  12472. AssertNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  12473. DYNAMIC_TYPE_TMP_BUFFER));
  12474. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  12475. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  12476. #else /* File system. */
  12477. certFile = fopen(eccClientCert, "rb");
  12478. AssertNotNull(certFile);
  12479. eccCertSz = (word32)FOURK_BUF;
  12480. AssertNotNull(eccCert =
  12481. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  12482. eccCertSz = (word32)fread(eccCert, 1, eccCertSz, certFile);
  12483. fclose(certFile);
  12484. keyFile = fopen(eccClientKey, "rb");
  12485. AssertNotNull(keyFile);
  12486. eccPrivKeySz = (word32)FOURK_BUF;
  12487. AssertNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  12488. DYNAMIC_TYPE_TMP_BUFFER));
  12489. eccPrivKeySz = (word32)fread(eccPrivKey, 1, eccPrivKeySz, keyFile);
  12490. fclose(keyFile);
  12491. #endif /* USE_CERT_BUFFERS_256 */
  12492. #endif /* END HAVE_ECC */
  12493. /* Silence. */
  12494. (void)keyFile;
  12495. (void)certFile;
  12496. const pkcs7EnvelopedVector testVectors[] = {
  12497. /* DATA is a global variable defined in the makefile. */
  12498. #if !defined(NO_RSA)
  12499. #ifndef NO_DES3
  12500. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  12501. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  12502. #endif /* NO_DES3 */
  12503. #ifndef NO_AES
  12504. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  12505. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  12506. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  12507. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  12508. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  12509. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  12510. #endif /* NO_AES */
  12511. #endif /* NO_RSA */
  12512. #if defined(HAVE_ECC)
  12513. #ifndef NO_AES
  12514. #ifndef NO_SHA
  12515. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  12516. AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme, eccCert,
  12517. eccCertSz, eccPrivKey, eccPrivKeySz},
  12518. #endif
  12519. #ifndef NO_SHA256
  12520. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  12521. AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme, eccCert,
  12522. eccCertSz, eccPrivKey, eccPrivKeySz},
  12523. #endif
  12524. #ifdef WOLFSSL_SHA512
  12525. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  12526. AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme, eccCert,
  12527. eccCertSz, eccPrivKey, eccPrivKeySz},
  12528. #endif
  12529. #endif /* NO_AES */
  12530. #endif /* END HAVE_ECC */
  12531. }; /* END pkcs7EnvelopedVector */
  12532. printf(testingFmt, "wc_PKCS7_EncodeEnvelopedData()");
  12533. AssertIntEQ(wc_PKCS7_Init(&pkcs7, HEAP_HINT, devId), 0);
  12534. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  12535. for (i = 0; i < testSz; i++) {
  12536. AssertIntEQ(wc_PKCS7_InitWithCert(&pkcs7, (testVectors + i)->cert,
  12537. (word32)(testVectors + i)->certSz), 0);
  12538. pkcs7.content = (byte*)(testVectors + i)->content;
  12539. pkcs7.contentSz = (testVectors + i)->contentSz;
  12540. pkcs7.contentOID = (testVectors + i)->contentOID;
  12541. pkcs7.encryptOID = (testVectors + i)->encryptOID;
  12542. pkcs7.keyWrapOID = (testVectors + i)->keyWrapOID;
  12543. pkcs7.keyAgreeOID = (testVectors + i)->keyAgreeOID;
  12544. pkcs7.privateKey = (testVectors + i)->privateKey;
  12545. pkcs7.privateKeySz = (testVectors + i)->privateKeySz;
  12546. AssertIntGE(wc_PKCS7_EncodeEnvelopedData(&pkcs7, output,
  12547. (word32)sizeof(output)), 0);
  12548. decodedSz = wc_PKCS7_DecodeEnvelopedData(&pkcs7, output,
  12549. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  12550. AssertIntGE(decodedSz, 0);
  12551. /* Verify the size of each buffer. */
  12552. AssertIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  12553. /* Don't free the last time through the loop. */
  12554. if (i < testSz - 1 ){
  12555. wc_PKCS7_Free(&pkcs7);
  12556. }
  12557. } /* END test loop. */
  12558. /* Test bad args. */
  12559. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  12560. (word32)sizeof(output)), BAD_FUNC_ARG);
  12561. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(&pkcs7, NULL,
  12562. (word32)sizeof(output)), BAD_FUNC_ARG);
  12563. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(&pkcs7, output, 0), BAD_FUNC_ARG);
  12564. printf(resultFmt, passed);
  12565. /* Decode. */
  12566. printf(testingFmt, "wc_PKCS7_DecodeEnvelopedData()");
  12567. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  12568. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  12569. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(&pkcs7, output,
  12570. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  12571. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(&pkcs7, output,
  12572. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  12573. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(&pkcs7, NULL,
  12574. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  12575. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(&pkcs7, output, 0, decoded,
  12576. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  12577. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  12578. tempWrd32 = pkcs7.singleCertSz;
  12579. pkcs7.singleCertSz = 0;
  12580. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(&pkcs7, output,
  12581. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  12582. pkcs7.singleCertSz = tempWrd32;
  12583. tempWrd32 = pkcs7.privateKeySz;
  12584. pkcs7.privateKeySz = 0;
  12585. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(&pkcs7, output,
  12586. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  12587. pkcs7.privateKeySz = tempWrd32;
  12588. tmpBytePtr = pkcs7.singleCert;
  12589. pkcs7.singleCert = NULL;
  12590. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(&pkcs7, output,
  12591. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  12592. pkcs7.singleCert = tmpBytePtr;
  12593. tmpBytePtr = pkcs7.privateKey;
  12594. pkcs7.privateKey = NULL;
  12595. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(&pkcs7, output,
  12596. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  12597. pkcs7.privateKey = tmpBytePtr;
  12598. printf(resultFmt, passed);
  12599. wc_PKCS7_Free(&pkcs7);
  12600. #ifndef NO_RSA
  12601. if (rsaCert) {
  12602. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12603. }
  12604. if (rsaPrivKey) {
  12605. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12606. }
  12607. #endif /*NO_RSA */
  12608. #ifdef HAVE_ECC
  12609. if (eccCert) {
  12610. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12611. }
  12612. if (eccPrivKey) {
  12613. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12614. }
  12615. #endif /* HAVE_ECC */
  12616. #endif /* HAVE_PKCS7 */
  12617. } /* END test_wc_PKCS7_EncodeEnvelopedData() */
  12618. /*
  12619. * Testing wc_PKCS7_EncodeEncryptedData()
  12620. */
  12621. static void test_wc_PKCS7_EncodeEncryptedData (void)
  12622. {
  12623. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  12624. PKCS7 pkcs7;
  12625. byte* tmpBytePtr = NULL;
  12626. byte encrypted[TWOK_BUF];
  12627. byte decoded[TWOK_BUF];
  12628. word32 tmpWrd32 = 0;
  12629. int tmpInt = 0;
  12630. int decodedSz;
  12631. int encryptedSz;
  12632. int testSz;
  12633. int i;
  12634. const byte data[] = { /* Hello World */
  12635. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  12636. 0x72,0x6c,0x64
  12637. };
  12638. #ifndef NO_DES3
  12639. byte desKey[] = {
  12640. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  12641. };
  12642. byte des3Key[] = {
  12643. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  12644. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  12645. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  12646. };
  12647. #endif
  12648. #ifndef NO_AES
  12649. byte aes128Key[] = {
  12650. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  12651. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  12652. };
  12653. byte aes192Key[] = {
  12654. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  12655. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  12656. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  12657. };
  12658. byte aes256Key[] = {
  12659. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  12660. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  12661. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  12662. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  12663. };
  12664. #endif
  12665. const pkcs7EncryptedVector testVectors[] =
  12666. {
  12667. #ifndef NO_DES3
  12668. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  12669. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  12670. #endif /* NO_DES3 */
  12671. #ifndef NO_AES
  12672. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  12673. sizeof(aes128Key)},
  12674. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  12675. sizeof(aes192Key)},
  12676. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  12677. sizeof(aes256Key)},
  12678. #endif /* NO_AES */
  12679. };
  12680. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  12681. for (i = 0; i < testSz; i++) {
  12682. AssertIntEQ(wc_PKCS7_Init(&pkcs7, HEAP_HINT, devId), 0);
  12683. pkcs7.content = (byte*)testVectors[i].content;
  12684. pkcs7.contentSz = testVectors[i].contentSz;
  12685. pkcs7.contentOID = testVectors[i].contentOID;
  12686. pkcs7.encryptOID = testVectors[i].encryptOID;
  12687. pkcs7.encryptionKey = testVectors[i].encryptionKey;
  12688. pkcs7.encryptionKeySz = testVectors[i].encryptionKeySz;
  12689. pkcs7.heap = HEAP_HINT;
  12690. /* encode encryptedData */
  12691. encryptedSz = wc_PKCS7_EncodeEncryptedData(&pkcs7, encrypted,
  12692. sizeof(encrypted));
  12693. AssertIntGT(encryptedSz, 0);
  12694. /* Decode encryptedData */
  12695. decodedSz = wc_PKCS7_DecodeEncryptedData(&pkcs7, encrypted, encryptedSz,
  12696. decoded, sizeof(decoded));
  12697. AssertIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  12698. /* Keep values for last itr. */
  12699. if (i < testSz - 1) {
  12700. wc_PKCS7_Free(&pkcs7);
  12701. }
  12702. }
  12703. printf(testingFmt, "wc_PKCS7_EncodeEncryptedData()");
  12704. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  12705. sizeof(encrypted)),BAD_FUNC_ARG);
  12706. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(&pkcs7, NULL,
  12707. sizeof(encrypted)), BAD_FUNC_ARG);
  12708. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(&pkcs7, encrypted,
  12709. 0), BAD_FUNC_ARG);
  12710. /* Testing the struct. */
  12711. tmpBytePtr = pkcs7.content;
  12712. pkcs7.content = NULL;
  12713. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(&pkcs7, encrypted,
  12714. sizeof(encrypted)), BAD_FUNC_ARG);
  12715. pkcs7.content = tmpBytePtr;
  12716. tmpWrd32 = pkcs7.contentSz;
  12717. pkcs7.contentSz = 0;
  12718. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(&pkcs7, encrypted,
  12719. sizeof(encrypted)), BAD_FUNC_ARG);
  12720. pkcs7.contentSz = tmpWrd32;
  12721. tmpInt = pkcs7.encryptOID;
  12722. pkcs7.encryptOID = 0;
  12723. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(&pkcs7, encrypted,
  12724. sizeof(encrypted)), BAD_FUNC_ARG);
  12725. pkcs7.encryptOID = tmpInt;
  12726. tmpBytePtr = pkcs7.encryptionKey;
  12727. pkcs7.encryptionKey = NULL;
  12728. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(&pkcs7, encrypted,
  12729. sizeof(encrypted)), BAD_FUNC_ARG);
  12730. pkcs7.encryptionKey = tmpBytePtr;
  12731. tmpWrd32 = pkcs7.encryptionKeySz;
  12732. pkcs7.encryptionKeySz = 0;
  12733. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(&pkcs7, encrypted,
  12734. sizeof(encrypted)), BAD_FUNC_ARG);
  12735. pkcs7.encryptionKeySz = tmpWrd32;
  12736. printf(resultFmt, passed);
  12737. printf(testingFmt, "wc_PKCS7_EncodeEncryptedData()");
  12738. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  12739. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  12740. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(&pkcs7, NULL, encryptedSz,
  12741. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  12742. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(&pkcs7, encrypted, 0,
  12743. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  12744. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(&pkcs7, encrypted, encryptedSz,
  12745. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  12746. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(&pkcs7, encrypted, encryptedSz,
  12747. decoded, 0), BAD_FUNC_ARG);
  12748. /* Test struct fields */
  12749. tmpBytePtr = pkcs7.encryptionKey;
  12750. pkcs7.encryptionKey = NULL;
  12751. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(&pkcs7, encrypted, encryptedSz,
  12752. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  12753. pkcs7.encryptionKey = tmpBytePtr;
  12754. pkcs7.encryptionKeySz = 0;
  12755. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(&pkcs7, encrypted, encryptedSz,
  12756. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  12757. printf(resultFmt, passed);
  12758. wc_PKCS7_Free(&pkcs7);
  12759. #endif
  12760. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  12761. /* Testing wc_SignatureGetSize() for signature type ECC */
  12762. static int test_wc_SignatureGetSize_ecc(void)
  12763. {
  12764. int ret = 0;
  12765. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  12766. enum wc_SignatureType sig_type;
  12767. word32 key_len;
  12768. /* Initialize ECC Key */
  12769. ecc_key ecc;
  12770. const char* qx =
  12771. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  12772. const char* qy =
  12773. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  12774. const char* d =
  12775. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  12776. ret = wc_ecc_init(&ecc);
  12777. if (ret == 0) {
  12778. ret = wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1");
  12779. }
  12780. printf(testingFmt, "wc_SigntureGetSize_ecc()");
  12781. if (ret == 0) {
  12782. /* Input for signature type ECC */
  12783. sig_type = WC_SIGNATURE_TYPE_ECC;
  12784. key_len = sizeof(ecc_key);
  12785. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  12786. /* Test bad args */
  12787. if (ret > 0) {
  12788. sig_type = (enum wc_SignatureType) 100;
  12789. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  12790. if (ret == BAD_FUNC_ARG) {
  12791. sig_type = WC_SIGNATURE_TYPE_ECC;
  12792. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  12793. }
  12794. if (ret >= 0) {
  12795. key_len = (word32) 0;
  12796. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  12797. }
  12798. if (ret == BAD_FUNC_ARG) {
  12799. ret = SIG_TYPE_E;
  12800. }
  12801. }
  12802. } else {
  12803. ret = WOLFSSL_FATAL_ERROR;
  12804. }
  12805. wc_ecc_free(&ecc);
  12806. #else
  12807. ret = SIG_TYPE_E;
  12808. #endif
  12809. if (ret == SIG_TYPE_E) {
  12810. ret = 0;
  12811. }
  12812. else {
  12813. ret = WOLFSSL_FATAL_ERROR;
  12814. }
  12815. printf(resultFmt, ret == 0 ? passed : failed);
  12816. return ret;
  12817. }/* END test_wc_SignatureGetSize_ecc() */
  12818. /* Testing wc_SignatureGetSize() for signature type rsa */
  12819. static int test_wc_SignatureGetSize_rsa(void)
  12820. {
  12821. int ret = 0;
  12822. #ifndef NO_RSA
  12823. enum wc_SignatureType sig_type;
  12824. word32 key_len;
  12825. word32 idx = 0;
  12826. /* Initialize RSA Key */
  12827. RsaKey rsa_key;
  12828. byte* tmp = NULL;
  12829. size_t bytes;
  12830. #ifdef USE_CERT_BUFFERS_1024
  12831. bytes = (size_t)sizeof_client_key_der_1024;
  12832. if (bytes < (size_t)sizeof_client_key_der_1024)
  12833. bytes = (size_t)sizeof_client_cert_der_1024;
  12834. #elif defined(USE_CERT_BUFFERS_2048)
  12835. bytes = (size_t)sizeof_client_key_der_2048;
  12836. if (bytes < (size_t)sizeof_client_cert_der_2048)
  12837. bytes = (size_t)sizeof_client_cert_der_2048;
  12838. #else
  12839. bytes = FOURK_BUF;
  12840. #endif
  12841. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12842. if (tmp != NULL) {
  12843. #ifdef USE_CERT_BUFFERS_1024
  12844. XMEMCPY(tmp, client_key_der_1024,
  12845. (size_t)sizeof_client_key_der_1024);
  12846. #elif defined(USE_CERT_BUFFERS_2048)
  12847. XMEMCPY(tmp, client_key_der_2048,
  12848. (size_t)sizeof_client_key_der_2048);
  12849. #elif !defined(NO_FILESYSTEM)
  12850. file = fopen(clientKey, "rb");
  12851. if (file != NULL) {
  12852. bytes = fread(tmp, 1, FOURK_BUF, file);
  12853. fclose(file);
  12854. }
  12855. else {
  12856. ret = WOLFSSL_FATAL_ERROR;
  12857. }
  12858. #else
  12859. ret = WOLFSSL_FATAL_ERROR;
  12860. #endif
  12861. if (ret == 0) {
  12862. ret = wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, devId);
  12863. if (ret == 0) {
  12864. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key,
  12865. (word32)bytes);
  12866. }
  12867. }
  12868. } else {
  12869. ret = WOLFSSL_FATAL_ERROR;
  12870. }
  12871. printf(testingFmt, "wc_SigntureGetSize_rsa()");
  12872. if (ret == 0) {
  12873. /* Input for signature type RSA */
  12874. sig_type = WC_SIGNATURE_TYPE_RSA;
  12875. key_len = sizeof(RsaKey);
  12876. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  12877. /* Test bad args */
  12878. if (ret > 0) {
  12879. sig_type = (enum wc_SignatureType) 100;
  12880. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  12881. if (ret == BAD_FUNC_ARG) {
  12882. sig_type = WC_SIGNATURE_TYPE_RSA;
  12883. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  12884. }
  12885. #ifndef HAVE_USER_RSA
  12886. if (ret == BAD_FUNC_ARG) {
  12887. #else
  12888. if (ret == 0) {
  12889. #endif
  12890. key_len = (word32)0;
  12891. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  12892. }
  12893. if (ret == BAD_FUNC_ARG) {
  12894. ret = SIG_TYPE_E;
  12895. }
  12896. }
  12897. } else {
  12898. ret = WOLFSSL_FATAL_ERROR;
  12899. }
  12900. wc_FreeRsaKey(&rsa_key);
  12901. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12902. #else
  12903. ret = SIG_TYPE_E;
  12904. #endif
  12905. if (ret == SIG_TYPE_E) {
  12906. ret = 0;
  12907. }else {
  12908. ret = WOLFSSL_FATAL_ERROR;
  12909. }
  12910. printf(resultFmt, ret == 0 ? passed : failed);
  12911. return ret;
  12912. }/* END test_wc_SignatureGetSize_rsa(void) */
  12913. /*----------------------------------------------------------------------------*
  12914. | hash.h Tests
  12915. *----------------------------------------------------------------------------*/
  12916. static int test_wc_HashInit(void)
  12917. {
  12918. int ret = 0, i; /* 0 indicates tests passed, 1 indicates failure */
  12919. wc_HashAlg hash;
  12920. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  12921. enum wc_HashType enumArray[] = {
  12922. #ifndef NO_MD5
  12923. WC_HASH_TYPE_MD5,
  12924. #endif
  12925. #ifndef NO_SHA
  12926. WC_HASH_TYPE_SHA,
  12927. #endif
  12928. #ifndef WOLFSSL_SHA224
  12929. WC_HASH_TYPE_SHA224,
  12930. #endif
  12931. #ifndef NO_SHA256
  12932. WC_HASH_TYPE_SHA256,
  12933. #endif
  12934. #ifndef WOLFSSL_SHA384
  12935. WC_HASH_TYPE_SHA384,
  12936. #endif
  12937. #ifndef WOLFSSL_SHA512
  12938. WC_HASH_TYPE_SHA512,
  12939. #endif
  12940. };
  12941. /* dynamically finds the length */
  12942. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  12943. /* For loop to test various arguments... */
  12944. for (i = 0; i < enumlen; i++) {
  12945. /* check for bad args */
  12946. if (wc_HashInit(&hash, enumArray[i]) == BAD_FUNC_ARG) {
  12947. ret = 1;
  12948. break;
  12949. }
  12950. /* check for null ptr */
  12951. if (wc_HashInit(NULL, enumArray[i]) != BAD_FUNC_ARG) {
  12952. ret = 1;
  12953. break;
  12954. }
  12955. } /* end of for loop */
  12956. printf(testingFmt, "wc_HashInit()");
  12957. if (ret==0) { /* all tests have passed */
  12958. printf(resultFmt, passed);
  12959. }
  12960. else { /* a test has failed */
  12961. printf(resultFmt, failed);
  12962. }
  12963. return ret;
  12964. } /* end of test_wc_HashInit */
  12965. /*----------------------------------------------------------------------------*
  12966. | Compatibility Tests
  12967. *----------------------------------------------------------------------------*/
  12968. static void test_wolfSSL_X509_NAME(void)
  12969. {
  12970. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  12971. && !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  12972. X509* x509;
  12973. const unsigned char* c;
  12974. unsigned char buf[4096];
  12975. int bytes;
  12976. FILE* f;
  12977. const X509_NAME* a;
  12978. const X509_NAME* b;
  12979. int sz;
  12980. unsigned char* tmp;
  12981. char file[] = "./certs/ca-cert.der";
  12982. printf(testingFmt, "wolfSSL_X509_NAME()");
  12983. /* test compile of depricated function, returns 0 */
  12984. AssertIntEQ(CRYPTO_thread_id(), 0);
  12985. AssertNotNull(a = X509_NAME_new());
  12986. X509_NAME_free((X509_NAME*)a);
  12987. f = fopen(file, "rb");
  12988. AssertNotNull(f);
  12989. bytes = (int)fread(buf, 1, sizeof(buf), f);
  12990. fclose(f);
  12991. c = buf;
  12992. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(c, bytes,
  12993. SSL_FILETYPE_ASN1));
  12994. /* test cmp function */
  12995. AssertNotNull(a = X509_get_issuer_name(x509));
  12996. AssertNotNull(b = X509_get_subject_name(x509));
  12997. AssertIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  12998. tmp = buf;
  12999. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  13000. if (tmp == buf) {
  13001. printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \
  13002. printf(" Expected pointer to be incremented\n");
  13003. abort();
  13004. }
  13005. /* retry but with the function creating a buffer */
  13006. tmp = NULL;
  13007. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  13008. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  13009. X509_free(x509);
  13010. printf(resultFmt, passed);
  13011. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  13012. }
  13013. static void test_wolfSSL_DES(void)
  13014. {
  13015. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  13016. const_DES_cblock myDes;
  13017. DES_cblock iv;
  13018. DES_key_schedule key;
  13019. word32 i;
  13020. DES_LONG dl;
  13021. unsigned char msg[] = "hello wolfssl";
  13022. printf(testingFmt, "wolfSSL_DES()");
  13023. DES_check_key(1);
  13024. DES_set_key(&myDes, &key);
  13025. /* check, check of odd parity */
  13026. XMEMSET(myDes, 4, sizeof(const_DES_cblock)); myDes[0] = 6; /*set even parity*/
  13027. XMEMSET(key, 5, sizeof(DES_key_schedule));
  13028. AssertIntEQ(DES_set_key_checked(&myDes, &key), -1);
  13029. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  13030. /* set odd parity for success case */
  13031. DES_set_odd_parity(&myDes);
  13032. printf("%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2], myDes[3]);
  13033. AssertIntEQ(DES_set_key_checked(&myDes, &key), 0);
  13034. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  13035. AssertIntEQ(key[i], myDes[i]);
  13036. }
  13037. AssertIntEQ(DES_is_weak_key(&myDes), 0);
  13038. /* check weak key */
  13039. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  13040. XMEMSET(key, 5, sizeof(DES_key_schedule));
  13041. AssertIntEQ(DES_set_key_checked(&myDes, &key), -2);
  13042. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  13043. /* now do unchecked copy of a weak key over */
  13044. DES_set_key_unchecked(&myDes, &key);
  13045. /* compare arrays, should be the same */
  13046. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  13047. AssertIntEQ(key[i], myDes[i]);
  13048. }
  13049. AssertIntEQ(DES_is_weak_key(&myDes), 1);
  13050. /* check DES_key_sched API */
  13051. XMEMSET(key, 1, sizeof(DES_key_schedule));
  13052. AssertIntEQ(DES_key_sched(&myDes, NULL), 0);
  13053. AssertIntEQ(DES_key_sched(NULL, &key), 0);
  13054. AssertIntEQ(DES_key_sched(&myDes, &key), 0);
  13055. /* compare arrays, should be the same */
  13056. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  13057. AssertIntEQ(key[i], myDes[i]);
  13058. }
  13059. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  13060. * DES_cbc_encrypt on the input */
  13061. XMEMSET(iv, 0, sizeof(DES_cblock));
  13062. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  13063. AssertIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  13064. AssertIntEQ(dl, 480052723);
  13065. printf(resultFmt, passed);
  13066. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  13067. }
  13068. static void test_wolfSSL_certs(void)
  13069. {
  13070. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13071. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  13072. X509* x509;
  13073. WOLFSSL* ssl;
  13074. WOLFSSL_CTX* ctx;
  13075. WOLF_STACK_OF(ASN1_OBJECT)* sk;
  13076. int crit;
  13077. printf(testingFmt, "wolfSSL_certs()");
  13078. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  13079. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  13080. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  13081. #ifndef HAVE_USER_RSA
  13082. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  13083. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  13084. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  13085. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  13086. #endif
  13087. AssertNotNull(ssl = SSL_new(ctx));
  13088. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  13089. #ifdef HAVE_PK_CALLBACKS
  13090. AssertIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  13091. #endif /* HAVE_PK_CALLBACKS */
  13092. /* create and use x509 */
  13093. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  13094. AssertNotNull(x509);
  13095. AssertIntEQ(SSL_use_certificate(ssl, x509), WOLFSSL_SUCCESS);
  13096. #ifndef HAVE_USER_RSA
  13097. /* with loading in a new cert the check on private key should now fail */
  13098. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  13099. #endif
  13100. #if defined(USE_CERT_BUFFERS_2048)
  13101. AssertIntEQ(SSL_use_certificate_ASN1(ssl,
  13102. (unsigned char*)server_cert_der_2048,
  13103. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  13104. #endif
  13105. #if !defined(NO_SHA) && !defined(NO_SHA256)
  13106. /************* Get Digest of Certificate ******************/
  13107. {
  13108. byte digest[64]; /* max digest size */
  13109. word32 digestSz;
  13110. XMEMSET(digest, 0, sizeof(digest));
  13111. AssertIntEQ(X509_digest(x509, wolfSSL_EVP_sha1(), digest, &digestSz),
  13112. WOLFSSL_SUCCESS);
  13113. AssertIntEQ(X509_digest(x509, wolfSSL_EVP_sha256(), digest, &digestSz),
  13114. WOLFSSL_SUCCESS);
  13115. AssertIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  13116. WOLFSSL_FAILURE);
  13117. }
  13118. #endif /* !NO_SHA && !NO_SHA256*/
  13119. /* test and checkout X509 extensions */
  13120. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_basic_constraints,
  13121. &crit, NULL);
  13122. AssertNotNull(sk);
  13123. AssertIntEQ(crit, 0);
  13124. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13125. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_key_usage,
  13126. &crit, NULL);
  13127. /* AssertNotNull(sk); NID not yet supported */
  13128. AssertIntEQ(crit, -1);
  13129. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13130. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_ext_key_usage,
  13131. &crit, NULL);
  13132. /* AssertNotNull(sk); no extension set */
  13133. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13134. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  13135. NID_authority_key_identifier, &crit, NULL);
  13136. AssertNotNull(sk);
  13137. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13138. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  13139. NID_private_key_usage_period, &crit, NULL);
  13140. /* AssertNotNull(sk); NID not yet supported */
  13141. AssertIntEQ(crit, -1);
  13142. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13143. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_subject_alt_name,
  13144. &crit, NULL);
  13145. /* AssertNotNull(sk); no alt names set */
  13146. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13147. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_issuer_alt_name,
  13148. &crit, NULL);
  13149. /* AssertNotNull(sk); NID not yet supported */
  13150. AssertIntEQ(crit, -1);
  13151. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13152. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_info_access, &crit,
  13153. NULL);
  13154. /* AssertNotNull(sk); no auth info set */
  13155. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13156. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_sinfo_access,
  13157. &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_name_constraints,
  13162. &crit, NULL);
  13163. /* AssertNotNull(sk); NID not yet supported */
  13164. AssertIntEQ(crit, -1);
  13165. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13166. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  13167. NID_certificate_policies, &crit, NULL);
  13168. #if !defined(WOLFSSL_SEP) && !defined(WOLFSSL_CERT_EXT)
  13169. AssertNull(sk);
  13170. #else
  13171. /* AssertNotNull(sk); no cert policy set */
  13172. #endif
  13173. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13174. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_policy_mappings,
  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_policy_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, NID_inhibit_any_policy,
  13185. &crit, NULL);
  13186. /* AssertNotNull(sk); NID not yet supported */
  13187. AssertIntEQ(crit, -1);
  13188. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13189. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_tlsfeature, &crit,
  13190. NULL);
  13191. /* AssertNotNull(sk); NID not yet supported */
  13192. AssertIntEQ(crit, -1);
  13193. wolfSSL_sk_ASN1_OBJECT_free(sk);
  13194. /* test invalid cases */
  13195. crit = 0;
  13196. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, -1, &crit, NULL);
  13197. AssertNull(sk);
  13198. AssertIntEQ(crit, -1);
  13199. sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL, NID_tlsfeature,
  13200. NULL, NULL);
  13201. AssertNull(sk);
  13202. AssertIntEQ(SSL_get_hit(ssl), 0);
  13203. X509_free(x509);
  13204. SSL_free(ssl);
  13205. SSL_CTX_free(ctx);
  13206. printf(resultFmt, passed);
  13207. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  13208. }
  13209. static void test_wolfSSL_ASN1_TIME_print()
  13210. {
  13211. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) \
  13212. && (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  13213. defined(WOLFSSL_HAPROXY)) && defined(USE_CERT_BUFFERS_2048)
  13214. BIO* bio;
  13215. X509* x509;
  13216. const unsigned char* der = client_cert_der_2048;
  13217. ASN1_TIME* t;
  13218. unsigned char buf[25];
  13219. printf(testingFmt, "wolfSSL_ASN1_TIME_print()");
  13220. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  13221. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  13222. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  13223. AssertIntEQ(ASN1_TIME_print(bio, X509_get_notBefore(x509)), 1);
  13224. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  13225. AssertIntEQ(XMEMCMP(buf, "Apr 13 15:23:09 2018 GMT", sizeof(buf) - 1), 0);
  13226. /* create a bad time and test results */
  13227. AssertNotNull(t = X509_get_notAfter(x509));
  13228. t->data[10] = 0;
  13229. t->data[5] = 0;
  13230. AssertIntNE(ASN1_TIME_print(bio, t), 1);
  13231. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  13232. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  13233. BIO_free(bio);
  13234. X509_free(x509);
  13235. printf(resultFmt, passed);
  13236. #endif
  13237. }
  13238. static void test_wolfSSL_ASN1_GENERALIZEDTIME_free(){
  13239. #if defined(OPENSSL_EXTRA)
  13240. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime;
  13241. unsigned char nullstr[32];
  13242. XMEMSET(nullstr, 0, 32);
  13243. asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  13244. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL,
  13245. DYNAMIC_TYPE_TMP_BUFFER);
  13246. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  13247. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  13248. AssertIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  13249. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  13250. #endif /* OPENSSL_EXTRA */
  13251. }
  13252. static void test_wolfSSL_private_keys(void)
  13253. {
  13254. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13255. !defined(NO_FILESYSTEM)
  13256. WOLFSSL* ssl;
  13257. WOLFSSL_CTX* ctx;
  13258. EVP_PKEY* pkey = NULL;
  13259. printf(testingFmt, "wolfSSL_private_keys()");
  13260. OpenSSL_add_all_digests();
  13261. OpenSSL_add_all_algorithms();
  13262. #ifndef NO_RSA
  13263. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  13264. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  13265. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  13266. AssertNotNull(ssl = SSL_new(ctx));
  13267. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  13268. #ifdef USE_CERT_BUFFERS_2048
  13269. {
  13270. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  13271. unsigned char buf[FOURK_BUF];
  13272. word32 bufSz;
  13273. AssertIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  13274. (unsigned char*)client_key_der_2048,
  13275. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  13276. #ifndef HAVE_USER_RSA
  13277. /* Should missmatch now that a different private key loaded */
  13278. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  13279. #endif
  13280. AssertIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  13281. (unsigned char*)server_key,
  13282. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  13283. /* After loading back in DER format of original key, should match */
  13284. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  13285. /* pkey not set yet, expecting to fail */
  13286. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  13287. /* set PKEY and test again */
  13288. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  13289. &server_key, (long)sizeof_server_key_der_2048));
  13290. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  13291. /* reuse PKEY structure and test
  13292. * this should be checked with a memory management sanity checker */
  13293. AssertFalse(server_key == (const unsigned char*)server_key_der_2048);
  13294. server_key = (const unsigned char*)server_key_der_2048;
  13295. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  13296. &server_key, (long)sizeof_server_key_der_2048));
  13297. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  13298. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  13299. bufSz = FOURK_BUF;
  13300. AssertIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  13301. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  13302. RSAk, NULL, 0)), 0);
  13303. server_key = (const unsigned char*)buf;
  13304. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  13305. (long)bufSz));
  13306. }
  13307. #endif
  13308. EVP_PKEY_free(pkey);
  13309. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  13310. SSL_CTX_free(ctx);
  13311. #endif /* end of RSA private key match tests */
  13312. #ifdef HAVE_ECC
  13313. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  13314. AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  13315. WOLFSSL_FILETYPE_PEM));
  13316. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  13317. WOLFSSL_FILETYPE_PEM));
  13318. AssertNotNull(ssl = SSL_new(ctx));
  13319. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  13320. SSL_free(ssl);
  13321. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  13322. WOLFSSL_FILETYPE_PEM));
  13323. AssertNotNull(ssl = SSL_new(ctx));
  13324. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  13325. SSL_free(ssl);
  13326. SSL_CTX_free(ctx);
  13327. #endif /* end of ECC private key match tests */
  13328. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  13329. CONF_modules_free();
  13330. ENGINE_cleanup();
  13331. CONF_modules_unload();
  13332. (void)ssl;
  13333. (void)ctx;
  13334. (void)pkey;
  13335. printf(resultFmt, passed);
  13336. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  13337. }
  13338. static void test_wolfSSL_PEM_PrivateKey(void)
  13339. {
  13340. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13341. (!defined(NO_RSA) || defined(HAVE_ECC)) && \
  13342. defined(USE_CERT_BUFFERS_2048)
  13343. BIO* bio = NULL;
  13344. EVP_PKEY* pkey = NULL;
  13345. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  13346. /* test creating new EVP_PKEY with bad arg */
  13347. AssertNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  13348. /* test loading RSA key using BIO */
  13349. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  13350. {
  13351. XFILE file;
  13352. const char* fname = "./certs/server-key.pem";
  13353. size_t sz;
  13354. byte* buf;
  13355. file = XFOPEN(fname, "rb");
  13356. AssertTrue((file != XBADFILE));
  13357. XFSEEK(file, 0, XSEEK_END);
  13358. sz = XFTELL(file);
  13359. XREWIND(file);
  13360. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  13361. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  13362. XFCLOSE(file);
  13363. /* Test using BIO new mem and loading PEM private key */
  13364. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  13365. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  13366. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  13367. BIO_free(bio);
  13368. bio = NULL;
  13369. EVP_PKEY_free(pkey);
  13370. pkey = NULL;
  13371. }
  13372. #endif
  13373. /* test loading ECC key using BIO */
  13374. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  13375. {
  13376. XFILE file;
  13377. const char* fname = "./certs/ecc-key.pem";
  13378. size_t sz;
  13379. byte* buf;
  13380. file = XFOPEN(fname, "rb");
  13381. AssertTrue((file != XBADFILE));
  13382. XFSEEK(file, 0, XSEEK_END);
  13383. sz = XFTELL(file);
  13384. XREWIND(file);
  13385. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  13386. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  13387. XFCLOSE(file);
  13388. /* Test using BIO new mem and loading PEM private key */
  13389. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  13390. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  13391. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  13392. BIO_free(bio);
  13393. bio = NULL;
  13394. EVP_PKEY_free(pkey);
  13395. pkey = NULL;
  13396. }
  13397. #endif
  13398. #if !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN))
  13399. {
  13400. EVP_PKEY* pkey2 = NULL;
  13401. unsigned char extra[10];
  13402. int i;
  13403. printf(testingFmt, "wolfSSL_PEM_PrivateKey()");
  13404. XMEMSET(extra, 0, sizeof(extra));
  13405. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  13406. AssertIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  13407. AssertNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  13408. &server_key, (long)sizeof_server_key_der_2048));
  13409. AssertNull(pkey);
  13410. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  13411. &server_key, (long)sizeof_server_key_der_2048));
  13412. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  13413. WOLFSSL_SUCCESS);
  13414. /* test creating new EVP_PKEY with good args */
  13415. AssertNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  13416. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  13417. /* test of reuse of EVP_PKEY */
  13418. AssertNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  13419. AssertIntEQ(BIO_pending(bio), 0);
  13420. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  13421. SSL_SUCCESS);
  13422. AssertIntEQ(BIO_write(bio, extra, 10), 10); /*add 10 extra bytes after PEM*/
  13423. AssertNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  13424. AssertNotNull(pkey);
  13425. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  13426. AssertIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  13427. AssertIntEQ(BIO_read(bio, extra, 10), 10);
  13428. for (i = 0; i < 10; i++) {
  13429. AssertIntEQ(extra[i], 0);
  13430. }
  13431. BIO_free(bio);
  13432. bio = NULL;
  13433. EVP_PKEY_free(pkey);
  13434. pkey = NULL;
  13435. EVP_PKEY_free(pkey2);
  13436. }
  13437. #endif
  13438. /* key is DES encrypted */
  13439. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_FILESYSTEM)
  13440. {
  13441. pem_password_cb* passwd_cb;
  13442. void* passwd_cb_userdata;
  13443. SSL_CTX* ctx;
  13444. char passwd[] = "bad password";
  13445. #ifndef WOLFSSL_NO_TLS12
  13446. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  13447. #else
  13448. AssertNotNull(ctx = SSL_CTX_new(TLSv1_3_server_method()));
  13449. #endif
  13450. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  13451. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  13452. AssertNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  13453. AssertNull(passwd_cb_userdata =
  13454. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  13455. /* fail case with password call back */
  13456. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  13457. (void*)passwd));
  13458. BIO_free(bio);
  13459. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  13460. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  13461. (void*)passwd));
  13462. BIO_free(bio);
  13463. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  13464. /* use callback that works */
  13465. AssertNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  13466. (void*)"yassl123"));
  13467. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  13468. EVP_PKEY_free(pkey);
  13469. pkey = NULL;
  13470. BIO_free(bio);
  13471. bio = NULL;
  13472. SSL_CTX_free(ctx);
  13473. }
  13474. #endif /* !defined(NO_DES3) */
  13475. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  13476. {
  13477. unsigned char buf[2048];
  13478. size_t bytes;
  13479. XFILE f;
  13480. SSL_CTX* ctx;
  13481. #ifndef WOLFSSL_NO_TLS12
  13482. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  13483. #else
  13484. AssertNotNull(ctx = SSL_CTX_new(TLSv1_3_server_method()));
  13485. #endif
  13486. AssertNotNull(f = XFOPEN("./certs/ecc-key.der", "rb"));
  13487. bytes = XFREAD(buf, 1, sizeof(buf), f);
  13488. XFCLOSE(f);
  13489. server_key = buf;
  13490. pkey = NULL;
  13491. AssertNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  13492. AssertNull(pkey);
  13493. AssertNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  13494. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  13495. EVP_PKEY_free(pkey);
  13496. pkey = NULL;
  13497. SSL_CTX_free(ctx);
  13498. }
  13499. #endif
  13500. printf(resultFmt, passed);
  13501. (void)server_key;
  13502. (void)bio;
  13503. (void)pkey;
  13504. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  13505. }
  13506. static void test_wolfSSL_PEM_RSAPrivateKey(void)
  13507. {
  13508. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13509. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  13510. RSA* rsa = NULL;
  13511. BIO* bio = NULL;
  13512. printf(testingFmt, "wolfSSL_PEM_RSAPrivateKey()");
  13513. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  13514. AssertNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  13515. AssertIntEQ(RSA_size(rsa), 256);
  13516. BIO_free(bio);
  13517. RSA_free(rsa);
  13518. #ifdef HAVE_ECC
  13519. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  13520. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  13521. BIO_free(bio);
  13522. #endif /* HAVE_ECC */
  13523. printf(resultFmt, passed);
  13524. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  13525. }
  13526. static void test_wolfSSL_tmp_dh(void)
  13527. {
  13528. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13529. !defined(NO_FILESYSTEM) && !defined(NO_DSA) && !defined(NO_RSA) && \
  13530. !defined(NO_DH)
  13531. byte buffer[5300];
  13532. char file[] = "./certs/dsaparams.pem";
  13533. FILE *f;
  13534. int bytes;
  13535. DSA* dsa;
  13536. DH* dh;
  13537. BIO* bio;
  13538. SSL* ssl;
  13539. SSL_CTX* ctx;
  13540. printf(testingFmt, "wolfSSL_tmp_dh()");
  13541. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  13542. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  13543. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  13544. AssertNotNull(ssl = SSL_new(ctx));
  13545. f = fopen(file, "rb");
  13546. AssertNotNull(f);
  13547. bytes = (int)fread(buffer, 1, sizeof(buffer), f);
  13548. fclose(f);
  13549. bio = BIO_new_mem_buf((void*)buffer, bytes);
  13550. AssertNotNull(bio);
  13551. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  13552. AssertNotNull(dsa);
  13553. dh = wolfSSL_DSA_dup_DH(dsa);
  13554. AssertNotNull(dh);
  13555. AssertIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  13556. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  13557. BIO_free(bio);
  13558. DSA_free(dsa);
  13559. DH_free(dh);
  13560. SSL_free(ssl);
  13561. SSL_CTX_free(ctx);
  13562. printf(resultFmt, passed);
  13563. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  13564. }
  13565. static void test_wolfSSL_ctrl(void)
  13566. {
  13567. #if defined(OPENSSL_EXTRA)
  13568. byte buff[5300];
  13569. BIO* bio;
  13570. int bytes;
  13571. BUF_MEM* ptr = NULL;
  13572. printf(testingFmt, "wolfSSL_crtl()");
  13573. bytes = sizeof(buff);
  13574. bio = BIO_new_mem_buf((void*)buff, bytes);
  13575. AssertNotNull(bio);
  13576. AssertNotNull(BIO_s_socket());
  13577. AssertIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  13578. /* needs tested after stubs filled out @TODO
  13579. SSL_ctrl
  13580. SSL_CTX_ctrl
  13581. */
  13582. BIO_free(bio);
  13583. printf(resultFmt, passed);
  13584. #endif /* defined(OPENSSL_EXTRA) */
  13585. }
  13586. static void test_wolfSSL_EVP_PKEY_new_mac_key(void)
  13587. {
  13588. #ifdef OPENSSL_EXTRA
  13589. static const unsigned char pw[] = "password";
  13590. static const int pwSz = sizeof(pw) - 1;
  13591. size_t checkPwSz = 0;
  13592. const unsigned char* checkPw = NULL;
  13593. WOLFSSL_EVP_PKEY* key = NULL;
  13594. printf(testingFmt, "wolfSSL_EVP_PKEY_new_mac_key()");
  13595. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  13596. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  13597. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, pwSz));
  13598. AssertIntEQ(key->type, EVP_PKEY_HMAC);
  13599. AssertIntEQ(key->save_type, EVP_PKEY_HMAC);
  13600. AssertIntEQ(key->pkey_sz, pwSz);
  13601. AssertIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  13602. AssertNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  13603. AssertIntEQ((int)checkPwSz, pwSz);
  13604. AssertIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  13605. wolfSSL_EVP_PKEY_free(key);
  13606. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, 0));
  13607. AssertIntEQ(key->pkey_sz, 0);
  13608. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  13609. (void)checkPw;
  13610. AssertIntEQ((int)checkPwSz, 0);
  13611. wolfSSL_EVP_PKEY_free(key);
  13612. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL, 0));
  13613. AssertIntEQ(key->pkey_sz, 0);
  13614. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  13615. (void)checkPw;
  13616. AssertIntEQ((int)checkPwSz, 0);
  13617. wolfSSL_EVP_PKEY_free(key);
  13618. printf(resultFmt, passed);
  13619. #endif /* OPENSSL_EXTRA */
  13620. }
  13621. static void test_wolfSSL_EVP_MD_hmac_signing(void)
  13622. {
  13623. #ifdef OPENSSL_EXTRA
  13624. const unsigned char testKey[] =
  13625. {
  13626. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  13627. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  13628. 0x0b, 0x0b, 0x0b, 0x0b
  13629. };
  13630. const char testData[] = "Hi There";
  13631. const unsigned char testResult[] =
  13632. {
  13633. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  13634. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  13635. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  13636. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  13637. };
  13638. unsigned char check[sizeof(testResult)];
  13639. size_t checkSz = -1;
  13640. WOLFSSL_EVP_PKEY* key;
  13641. WOLFSSL_EVP_MD_CTX mdCtx;
  13642. printf(testingFmt, "wolfSSL_EVP_MD_hmac_signing()");
  13643. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  13644. testKey, (int)sizeof(testKey)));
  13645. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  13646. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  13647. NULL, key), 1);
  13648. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  13649. (unsigned int)XSTRLEN(testData)), 1);
  13650. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  13651. AssertIntEQ((int)checkSz, sizeof(testResult));
  13652. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  13653. AssertIntEQ((int)checkSz,(int)sizeof(testResult));
  13654. AssertIntEQ(XMEMCMP(testResult, check, sizeof(testResult)), 0);
  13655. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  13656. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  13657. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  13658. NULL, key), 1);
  13659. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  13660. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  13661. AssertIntEQ((int)checkSz, sizeof(testResult));
  13662. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  13663. AssertIntEQ((int)checkSz,(int)sizeof(testResult));
  13664. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  13665. (unsigned int)XSTRLEN(testData) - 4), 1);
  13666. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  13667. AssertIntEQ((int)checkSz,(int)sizeof(testResult));
  13668. AssertIntEQ(XMEMCMP(testResult, check, sizeof(testResult)), 0);
  13669. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  13670. wolfSSL_EVP_PKEY_free(key);
  13671. printf(resultFmt, passed);
  13672. #endif /* OPENSSL_EXTRA */
  13673. }
  13674. static void test_wolfSSL_CTX_add_extra_chain_cert(void)
  13675. {
  13676. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13677. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  13678. char caFile[] = "./certs/client-ca.pem";
  13679. char clientFile[] = "./certs/client-cert.pem";
  13680. SSL_CTX* ctx;
  13681. X509* x509 = NULL;
  13682. printf(testingFmt, "wolfSSL_CTX_add_extra_chain_cert()");
  13683. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  13684. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  13685. AssertNotNull(x509);
  13686. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  13687. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  13688. AssertNotNull(x509);
  13689. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  13690. /* additional test of getting EVP_PKEY key size from X509
  13691. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  13692. * allowed with user RSA */
  13693. {
  13694. EVP_PKEY* pkey;
  13695. #if defined(HAVE_ECC)
  13696. X509* ecX509;
  13697. #endif /* HAVE_ECC */
  13698. AssertNotNull(pkey = X509_get_pubkey(x509));
  13699. /* current RSA key is 2048 bit (256 bytes) */
  13700. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  13701. EVP_PKEY_free(pkey);
  13702. #if defined(HAVE_ECC)
  13703. #if defined(USE_CERT_BUFFERS_256)
  13704. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  13705. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  13706. SSL_FILETYPE_ASN1));
  13707. #else
  13708. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(cliEccCertFile,
  13709. SSL_FILETYPE_PEM));
  13710. #endif
  13711. AssertNotNull(pkey = X509_get_pubkey(ecX509));
  13712. /* current ECC key is 256 bit (32 bytes) */
  13713. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  13714. X509_free(ecX509);
  13715. EVP_PKEY_free(pkey);
  13716. #endif /* HAVE_ECC */
  13717. }
  13718. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  13719. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  13720. #ifdef WOLFSSL_ENCRYPTED_KEYS
  13721. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  13722. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  13723. #endif
  13724. SSL_CTX_free(ctx);
  13725. printf(resultFmt, passed);
  13726. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13727. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  13728. }
  13729. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  13730. static void test_wolfSSL_ERR_peek_last_error_line(void)
  13731. {
  13732. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13733. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  13734. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  13735. defined(HAVE_IO_TESTS_DEPENDENCIES)
  13736. tcp_ready ready;
  13737. func_args client_args;
  13738. func_args server_args;
  13739. #ifndef SINGLE_THREADED
  13740. THREAD_TYPE serverThread;
  13741. #endif
  13742. callback_functions client_cb;
  13743. callback_functions server_cb;
  13744. int line = 0;
  13745. int flag = ERR_TXT_STRING;
  13746. const char* file = NULL;
  13747. const char* data = NULL;
  13748. printf(testingFmt, "wolfSSL_ERR_peek_last_error_line()");
  13749. /* create a failed connection and inspect the error */
  13750. #ifdef WOLFSSL_TIRTOS
  13751. fdOpenSession(Task_self());
  13752. #endif
  13753. XMEMSET(&client_args, 0, sizeof(func_args));
  13754. XMEMSET(&server_args, 0, sizeof(func_args));
  13755. StartTCP();
  13756. InitTcpReady(&ready);
  13757. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  13758. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  13759. client_cb.method = wolfTLSv1_1_client_method;
  13760. server_cb.method = wolfTLSv1_2_server_method;
  13761. server_args.signal = &ready;
  13762. server_args.callbacks = &server_cb;
  13763. client_args.signal = &ready;
  13764. client_args.callbacks = &client_cb;
  13765. #ifndef SINGLE_THREADED
  13766. start_thread(test_server_nofail, &server_args, &serverThread);
  13767. wait_tcp_ready(&server_args);
  13768. test_client_nofail(&client_args, NULL);
  13769. join_thread(serverThread);
  13770. #endif
  13771. FreeTcpReady(&ready);
  13772. AssertIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  13773. AssertNotNull(data);
  13774. /* check clearing error state */
  13775. ERR_remove_state(0);
  13776. AssertIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  13777. ERR_peek_last_error_line(NULL, &line);
  13778. AssertIntEQ(line, 0);
  13779. ERR_peek_last_error_line(&file, NULL);
  13780. AssertNull(file);
  13781. /* retry connection to fill error queue */
  13782. XMEMSET(&client_args, 0, sizeof(func_args));
  13783. XMEMSET(&server_args, 0, sizeof(func_args));
  13784. StartTCP();
  13785. InitTcpReady(&ready);
  13786. client_cb.method = wolfTLSv1_1_client_method;
  13787. server_cb.method = wolfTLSv1_2_server_method;
  13788. server_args.signal = &ready;
  13789. server_args.callbacks = &server_cb;
  13790. client_args.signal = &ready;
  13791. client_args.callbacks = &client_cb;
  13792. start_thread(test_server_nofail, &server_args, &serverThread);
  13793. wait_tcp_ready(&server_args);
  13794. test_client_nofail(&client_args, NULL);
  13795. join_thread(serverThread);
  13796. FreeTcpReady(&ready);
  13797. /* check that error code was stored */
  13798. AssertIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  13799. ERR_peek_last_error_line(NULL, &line);
  13800. AssertIntNE(line, 0);
  13801. ERR_peek_last_error_line(&file, NULL);
  13802. AssertNotNull(file);
  13803. #ifdef WOLFSSL_TIRTOS
  13804. fdOpenSession(Task_self());
  13805. #endif
  13806. printf(resultFmt, passed);
  13807. printf("\nTesting error print out\n");
  13808. ERR_print_errors_fp(stdout);
  13809. printf("Done testing print out\n\n");
  13810. fflush(stdout);
  13811. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13812. !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  13813. }
  13814. #endif
  13815. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13816. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  13817. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  13818. {
  13819. (void) ok;
  13820. (void) ctx;
  13821. printf("ENTER verify_cb\n");
  13822. return SSL_SUCCESS;
  13823. }
  13824. #endif
  13825. static void test_wolfSSL_X509_STORE_CTX(void)
  13826. {
  13827. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13828. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  13829. X509_STORE_CTX* ctx;
  13830. X509_STORE* str;
  13831. X509* x509;
  13832. printf(testingFmt, "wolfSSL_X509_STORE_CTX()");
  13833. AssertNotNull(ctx = X509_STORE_CTX_new());
  13834. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  13835. AssertNotNull((x509 =
  13836. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  13837. AssertIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  13838. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  13839. AssertIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  13840. X509_STORE_CTX_set_error(ctx, -5);
  13841. X509_STORE_CTX_set_error(NULL, -5);
  13842. X509_STORE_CTX_free(ctx);
  13843. AssertNotNull(ctx = X509_STORE_CTX_new());
  13844. X509_STORE_CTX_set_verify_cb(ctx, (void *)verify_cb);
  13845. X509_STORE_CTX_free(ctx);
  13846. printf(resultFmt, passed);
  13847. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13848. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  13849. }
  13850. static void test_wolfSSL_X509_STORE_set_flags(void)
  13851. {
  13852. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13853. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  13854. X509_STORE* store;
  13855. X509* x509;
  13856. printf(testingFmt, "wolfSSL_X509_STORE_set_flags()");
  13857. AssertNotNull((store = wolfSSL_X509_STORE_new()));
  13858. AssertNotNull((x509 =
  13859. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM)));
  13860. AssertIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  13861. #ifdef HAVE_CRL
  13862. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL), WOLFSSL_SUCCESS);
  13863. #else
  13864. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  13865. NOT_COMPILED_IN);
  13866. #endif
  13867. wolfSSL_X509_free(x509);
  13868. wolfSSL_X509_STORE_free(store);
  13869. printf(resultFmt, passed);
  13870. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  13871. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  13872. }
  13873. static void test_wolfSSL_X509_LOOKUP_load_file(void)
  13874. {
  13875. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  13876. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  13877. WOLFSSL_X509_STORE* store;
  13878. WOLFSSL_X509_LOOKUP* lookup;
  13879. printf(testingFmt, "wolfSSL_X509_LOOKUP_load_file()");
  13880. AssertNotNull(store = wolfSSL_X509_STORE_new());
  13881. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  13882. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  13883. X509_FILETYPE_PEM), 1);
  13884. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  13885. X509_FILETYPE_PEM), 1);
  13886. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  13887. WOLFSSL_FILETYPE_PEM), 1);
  13888. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  13889. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  13890. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  13891. X509_FILETYPE_PEM), 1);
  13892. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  13893. WOLFSSL_FILETYPE_PEM), 1);
  13894. wolfSSL_X509_STORE_free(store);
  13895. printf(resultFmt, passed);
  13896. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  13897. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  13898. }
  13899. static void test_wolfSSL_X509_STORE_CTX_set_time(void)
  13900. {
  13901. #if defined(OPENSSL_EXTRA)
  13902. WOLFSSL_X509_STORE_CTX* ctx;
  13903. time_t c_time;
  13904. printf(testingFmt, "wolfSSL_X509_set_time()");
  13905. AssertNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  13906. c_time = 365*24*60*60;
  13907. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  13908. AssertTrue(
  13909. (ctx->param->flags & WOLFSSL_USE_CHECK_TIME) == WOLFSSL_USE_CHECK_TIME);
  13910. AssertTrue(ctx->param->check_time == c_time);
  13911. wolfSSL_X509_STORE_CTX_free(ctx);
  13912. printf(resultFmt, passed);
  13913. #endif /* OPENSSL_EXTRA */
  13914. }
  13915. static void test_wolfSSL_CTX_set_client_CA_list(void)
  13916. {
  13917. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS)
  13918. WOLFSSL_CTX* ctx;
  13919. WOLF_STACK_OF(WOLFSSL_X509_NAME)* names = NULL;
  13920. WOLF_STACK_OF(WOLFSSL_X509_NAME)* ca_list = NULL;
  13921. printf(testingFmt, "wolfSSL_CTX_set_client_CA_list()");
  13922. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  13923. names = wolfSSL_load_client_CA_file(cliCertFile);
  13924. AssertNotNull(names);
  13925. wolfSSL_CTX_set_client_CA_list(ctx,names);
  13926. AssertNotNull(ca_list = wolfSSL_SSL_CTX_get_client_CA_list(ctx));
  13927. wolfSSL_CTX_free(ctx);
  13928. printf(resultFmt, passed);
  13929. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS */
  13930. }
  13931. static void test_wolfSSL_CTX_add_client_CA(void)
  13932. {
  13933. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS)
  13934. WOLFSSL_CTX* ctx;
  13935. WOLFSSL_X509* x509;
  13936. WOLFSSL_X509* x509_a;
  13937. WOLF_STACK_OF(WOLFSSLX509_NAME)* ca_list;
  13938. int ret = 0;
  13939. printf(testingFmt, "wolfSSL_CTX_add_client_CA()");
  13940. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  13941. /* Add client cert */
  13942. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  13943. SSL_FILETYPE_PEM));
  13944. ret = wolfSSL_CTX_add_client_CA(ctx, x509);
  13945. AssertIntEQ(ret ,SSL_SUCCESS);
  13946. AssertNotNull(ca_list = wolfSSL_SSL_CTX_get_client_CA_list(ctx));
  13947. /* Add another client cert */
  13948. AssertNotNull(x509_a = wolfSSL_X509_load_certificate_file(cliCertFile,
  13949. SSL_FILETYPE_PEM));
  13950. AssertIntEQ(wolfSSL_CTX_add_client_CA(ctx, x509_a),SSL_SUCCESS);
  13951. wolfSSL_X509_free(x509);
  13952. wolfSSL_X509_free(x509_a);
  13953. wolfSSL_CTX_free(ctx);
  13954. printf(resultFmt, passed);
  13955. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS */
  13956. }
  13957. static void test_wolfSSL_X509_NID(void)
  13958. {
  13959. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  13960. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  13961. int sigType;
  13962. int nameSz;
  13963. X509* cert;
  13964. EVP_PKEY* pubKeyTmp;
  13965. X509_NAME* name;
  13966. char commonName[80];
  13967. char countryName[80];
  13968. char localityName[80];
  13969. char stateName[80];
  13970. char orgName[80];
  13971. char orgUnit[80];
  13972. printf(testingFmt, "wolfSSL_X509_NID()");
  13973. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  13974. /* convert cert from DER to internal WOLFSSL_X509 struct */
  13975. AssertNotNull(cert = wolfSSL_X509_d2i(&cert, client_cert_der_2048,
  13976. sizeof_client_cert_der_2048));
  13977. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  13978. /* extract PUBLIC KEY from cert */
  13979. AssertNotNull(pubKeyTmp = X509_get_pubkey(cert));
  13980. /* extract signatureType */
  13981. AssertIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  13982. /* extract subjectName info */
  13983. AssertNotNull(name = X509_get_subject_name(cert));
  13984. AssertIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  13985. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, ASN_COMMON_NAME,
  13986. NULL, 0)), 0);
  13987. AssertIntEQ(nameSz, 15);
  13988. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, ASN_COMMON_NAME,
  13989. commonName, sizeof(commonName))), 0);
  13990. AssertIntEQ(nameSz, 15);
  13991. AssertIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  13992. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, ASN_COMMON_NAME,
  13993. commonName, 9)), 0);
  13994. AssertIntEQ(nameSz, 8);
  13995. AssertIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  13996. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, ASN_COUNTRY_NAME,
  13997. countryName, sizeof(countryName))), 0);
  13998. AssertIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  13999. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, ASN_LOCALITY_NAME,
  14000. localityName, sizeof(localityName))), 0);
  14001. AssertIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  14002. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, ASN_STATE_NAME,
  14003. stateName, sizeof(stateName))), 0);
  14004. AssertIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  14005. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, ASN_ORG_NAME,
  14006. orgName, sizeof(orgName))), 0);
  14007. AssertIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  14008. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, ASN_ORGUNIT_NAME,
  14009. orgUnit, sizeof(orgUnit))), 0);
  14010. AssertIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  14011. EVP_PKEY_free(pubKeyTmp);
  14012. X509_free(cert);
  14013. printf(resultFmt, passed);
  14014. #endif
  14015. }
  14016. static void test_wolfSSL_CTX_set_srp_username(void)
  14017. {
  14018. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  14019. && !defined(NO_SHA256) && !defined(WC_NO_RNG)
  14020. WOLFSSL_CTX* ctx;
  14021. const char *username = "TESTUSER";
  14022. const char *password = "TESTPASSWORD";
  14023. int r;
  14024. printf(testingFmt, "wolfSSL_CTX_set_srp_username()");
  14025. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  14026. AssertNotNull(ctx);
  14027. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  14028. AssertIntEQ(r,SSL_SUCCESS);
  14029. wolfSSL_CTX_free(ctx);
  14030. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  14031. AssertNotNull(ctx);
  14032. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  14033. AssertIntEQ(r,SSL_SUCCESS);
  14034. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  14035. AssertIntEQ(r,SSL_SUCCESS);
  14036. wolfSSL_CTX_free(ctx);
  14037. printf(resultFmt, passed);
  14038. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  14039. /* && !NO_SHA256 && !WC_NO_RNG */
  14040. }
  14041. static void test_wolfSSL_CTX_set_srp_password(void)
  14042. {
  14043. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  14044. && !defined(NO_SHA256) && !defined(WC_NO_RNG)
  14045. WOLFSSL_CTX* ctx;
  14046. const char *username = "TESTUSER";
  14047. const char *password = "TESTPASSWORD";
  14048. int r;
  14049. printf(testingFmt, "wolfSSL_CTX_set_srp_password()");
  14050. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  14051. AssertNotNull(ctx);
  14052. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  14053. AssertIntEQ(r,SSL_SUCCESS);
  14054. wolfSSL_CTX_free(ctx);
  14055. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  14056. AssertNotNull(ctx);
  14057. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  14058. AssertIntEQ(r,SSL_SUCCESS);
  14059. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  14060. AssertIntEQ(r,SSL_SUCCESS);
  14061. wolfSSL_CTX_free(ctx);
  14062. printf(resultFmt, passed);
  14063. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  14064. /* && !NO_SHA256 && !WC_NO_RNG */
  14065. }
  14066. static void test_wolfSSL_X509_STORE(void)
  14067. {
  14068. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  14069. X509_STORE *store;
  14070. X509_CRL *crl;
  14071. X509 *x509;
  14072. const char crl_pem[] = "./certs/crl/crl.pem";
  14073. const char svrCert[] = "./certs/server-cert.pem";
  14074. XFILE fp;
  14075. printf(testingFmt, "test_wolfSSL_X509_STORE");
  14076. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  14077. AssertNotNull((x509 =
  14078. wolfSSL_X509_load_certificate_file(svrCert, SSL_FILETYPE_PEM)));
  14079. AssertIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  14080. X509_free(x509);
  14081. AssertNotNull(fp = XFOPEN(crl_pem, "rb"));
  14082. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  14083. XFCLOSE(fp);
  14084. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  14085. X509_CRL_free(crl);
  14086. X509_STORE_free(store);
  14087. printf(resultFmt, passed);
  14088. #endif
  14089. return;
  14090. }
  14091. static void test_wolfSSL_BN(void)
  14092. {
  14093. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  14094. BIGNUM* a;
  14095. BIGNUM* b;
  14096. BIGNUM* c;
  14097. BIGNUM* d;
  14098. ASN1_INTEGER* ai;
  14099. unsigned char value[1];
  14100. printf(testingFmt, "wolfSSL_BN()");
  14101. AssertNotNull(b = BN_new());
  14102. AssertNotNull(c = BN_new());
  14103. AssertNotNull(d = BN_new());
  14104. value[0] = 0x03;
  14105. AssertNotNull(ai = ASN1_INTEGER_new());
  14106. /* at the moment hard setting since no set function */
  14107. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  14108. ai->data[1] = 0x01; /* length of integer */
  14109. ai->data[2] = value[0];
  14110. AssertNotNull(a = ASN1_INTEGER_to_BN(ai, NULL));
  14111. ASN1_INTEGER_free(ai);
  14112. value[0] = 0x02;
  14113. AssertNotNull(BN_bin2bn(value, sizeof(value), b));
  14114. value[0] = 0x05;
  14115. AssertNotNull(BN_bin2bn(value, sizeof(value), c));
  14116. /* a^b mod c = */
  14117. AssertIntEQ(BN_mod_exp(d, NULL, b, c, NULL), WOLFSSL_FAILURE);
  14118. AssertIntEQ(BN_mod_exp(d, a, b, c, NULL), WOLFSSL_SUCCESS);
  14119. /* check result 3^2 mod 5 */
  14120. value[0] = 0;
  14121. AssertIntEQ(BN_bn2bin(d, value), WOLFSSL_SUCCESS);
  14122. AssertIntEQ(BN_bn2bin(d, value), SSL_SUCCESS);
  14123. AssertIntEQ((int)(value[0]), 4);
  14124. /* a*b mod c = */
  14125. AssertIntEQ(BN_mod_mul(d, NULL, b, c, NULL), SSL_FAILURE);
  14126. AssertIntEQ(BN_mod_mul(d, a, b, c, NULL), SSL_SUCCESS);
  14127. /* check result 3*2 mod 5 */
  14128. value[0] = 0;
  14129. AssertIntEQ(BN_bn2bin(d, value), SSL_SUCCESS);
  14130. AssertIntEQ((int)(value[0]), 1);
  14131. /* BN_mod_inverse test */
  14132. value[0] = 0;
  14133. BIGNUM *r = BN_new();
  14134. BIGNUM *val = BN_mod_inverse(r,b,c,NULL);
  14135. AssertIntEQ(BN_bn2bin(r, value), 1);
  14136. AssertIntEQ((int)(value[0] & 0x03), 3);
  14137. BN_free(val);
  14138. AssertIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  14139. AssertIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  14140. AssertIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  14141. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  14142. {
  14143. char* ret;
  14144. AssertNotNull(ret = BN_bn2dec(c));
  14145. AssertIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  14146. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  14147. }
  14148. #endif
  14149. AssertIntEQ(BN_get_word(c), 4);
  14150. BN_free(a);
  14151. BN_free(b);
  14152. BN_free(c);
  14153. BN_clear_free(d);
  14154. /* check that converting NULL and the null string returns an error */
  14155. a = NULL;
  14156. AssertIntLE(BN_hex2bn(&a, NULL), 0);
  14157. AssertIntLE(BN_hex2bn(&a, ""), 0);
  14158. AssertNull(a);
  14159. /* check that getting a string and a bin of the same number are equal,
  14160. * and that the comparison works EQ, LT and GT */
  14161. AssertIntGT(BN_hex2bn(&a, "03"), 0);
  14162. value[0] = 0x03;
  14163. AssertNotNull(b = BN_new());
  14164. AssertNotNull(BN_bin2bn(value, sizeof(value), b));
  14165. value[0] = 0x04;
  14166. AssertNotNull(c = BN_new());
  14167. AssertNotNull(BN_bin2bn(value, sizeof(value), c));
  14168. AssertIntEQ(BN_cmp(a, b), 0);
  14169. AssertIntLT(BN_cmp(a, c), 0);
  14170. AssertIntGT(BN_cmp(c, b), 0);
  14171. BN_free(a);
  14172. BN_free(b);
  14173. BN_free(c);
  14174. printf(resultFmt, passed);
  14175. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  14176. }
  14177. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  14178. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  14179. #define TEST_ARG 0x1234
  14180. static void msg_cb(int write_p, int version, int content_type,
  14181. const void *buf, size_t len, SSL *ssl, void *arg)
  14182. {
  14183. (void)write_p;
  14184. (void)version;
  14185. (void)content_type;
  14186. (void)buf;
  14187. (void)len;
  14188. (void)ssl;
  14189. AssertTrue(arg == (void*)TEST_ARG);
  14190. }
  14191. #endif
  14192. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  14193. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  14194. defined(HAVE_IO_TESTS_DEPENDENCIES)
  14195. #ifndef SINGLE_THREADED
  14196. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  14197. {
  14198. (void) ctx;
  14199. (void) ssl;
  14200. printf("\n===== msgcb called ====\n");
  14201. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  14202. AssertTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  14203. AssertIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 1);
  14204. #endif
  14205. return SSL_SUCCESS;
  14206. }
  14207. #endif
  14208. #endif
  14209. static void test_wolfSSL_msgCb(void)
  14210. {
  14211. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  14212. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  14213. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  14214. !defined(NO_WOLFSSL_SERVER)
  14215. tcp_ready ready;
  14216. func_args client_args;
  14217. func_args server_args;
  14218. #ifndef SINGLE_THREADED
  14219. THREAD_TYPE serverThread;
  14220. #endif
  14221. callback_functions client_cb;
  14222. callback_functions server_cb;
  14223. printf(testingFmt, "test_wolfSSL_msgCb");
  14224. /* create a failed connection and inspect the error */
  14225. #ifdef WOLFSSL_TIRTOS
  14226. fdOpenSession(Task_self());
  14227. #endif
  14228. XMEMSET(&client_args, 0, sizeof(func_args));
  14229. XMEMSET(&server_args, 0, sizeof(func_args));
  14230. StartTCP();
  14231. InitTcpReady(&ready);
  14232. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  14233. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  14234. #ifndef WOLFSSL_NO_TLS12
  14235. client_cb.method = wolfTLSv1_2_client_method;
  14236. server_cb.method = wolfTLSv1_2_server_method;
  14237. #else
  14238. client_cb.method = wolfTLSv1_3_client_method;
  14239. server_cb.method = wolfTLSv1_3_server_method;
  14240. #endif
  14241. server_args.signal = &ready;
  14242. server_args.callbacks = &server_cb;
  14243. client_args.signal = &ready;
  14244. client_args.callbacks = &client_cb;
  14245. client_args.return_code = TEST_FAIL;
  14246. #ifndef SINGLE_THREADED
  14247. start_thread(test_server_nofail, &server_args, &serverThread);
  14248. wait_tcp_ready(&server_args);
  14249. test_client_nofail(&client_args, (void *)msgCb);
  14250. join_thread(serverThread);
  14251. AssertTrue(client_args.return_code);
  14252. AssertTrue(server_args.return_code);
  14253. #endif
  14254. FreeTcpReady(&ready);
  14255. #ifdef WOLFSSL_TIRTOS
  14256. fdOpenSession(Task_self());
  14257. #endif
  14258. printf(resultFmt, passed);
  14259. #endif
  14260. }
  14261. static void test_wolfSSL_set_options(void)
  14262. {
  14263. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  14264. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  14265. SSL* ssl;
  14266. SSL_CTX* ctx;
  14267. char appData[] = "extra msg";
  14268. unsigned char protos[] = {
  14269. 7, 't', 'l', 's', '/', '1', '.', '2',
  14270. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  14271. };
  14272. unsigned int len = sizeof(protos);
  14273. void *arg = (void *)TEST_ARG;
  14274. printf(testingFmt, "wolfSSL_set_options()");
  14275. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  14276. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  14277. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  14278. AssertTrue(SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1) == SSL_OP_NO_TLSv1);
  14279. AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  14280. AssertIntGT((int)SSL_CTX_set_options(ctx, (SSL_OP_COOKIE_EXCHANGE |
  14281. SSL_OP_NO_SSLv2)), 0);
  14282. AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_COOKIE_EXCHANGE) &
  14283. SSL_OP_COOKIE_EXCHANGE) == SSL_OP_COOKIE_EXCHANGE);
  14284. AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_2) &
  14285. SSL_OP_NO_TLSv1_2) == SSL_OP_NO_TLSv1_2);
  14286. AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_NO_COMPRESSION) &
  14287. SSL_OP_NO_COMPRESSION) == SSL_OP_NO_COMPRESSION);
  14288. AssertNull((SSL_CTX_clear_options(ctx, SSL_OP_NO_COMPRESSION) &
  14289. SSL_OP_NO_COMPRESSION));
  14290. SSL_CTX_free(ctx);
  14291. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  14292. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  14293. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  14294. AssertNotNull(ssl = SSL_new(ctx));
  14295. #if defined(HAVE_EX_DATA) || defined(FORTRESS)
  14296. AssertIntEQ(SSL_set_app_data(ssl, (void*)appData), SSL_SUCCESS);
  14297. AssertNotNull(SSL_get_app_data((const WOLFSSL*)ssl));
  14298. AssertIntEQ(XMEMCMP(SSL_get_app_data((const WOLFSSL*)ssl),
  14299. appData, sizeof(appData)), 0);
  14300. #else
  14301. AssertIntEQ(SSL_set_app_data(ssl, (void*)appData), SSL_FAILURE);
  14302. AssertNull(SSL_get_app_data((const WOLFSSL*)ssl));
  14303. #endif
  14304. AssertTrue(SSL_set_options(ssl, SSL_OP_NO_TLSv1) == SSL_OP_NO_TLSv1);
  14305. AssertTrue(SSL_get_options(ssl) == SSL_OP_NO_TLSv1);
  14306. AssertIntGT((int)SSL_set_options(ssl, (SSL_OP_COOKIE_EXCHANGE |
  14307. WOLFSSL_OP_NO_SSLv2)), 0);
  14308. AssertTrue((SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE) &
  14309. SSL_OP_COOKIE_EXCHANGE) == SSL_OP_COOKIE_EXCHANGE);
  14310. AssertTrue((SSL_set_options(ssl, SSL_OP_NO_TLSv1_2) &
  14311. SSL_OP_NO_TLSv1_2) == SSL_OP_NO_TLSv1_2);
  14312. AssertTrue((SSL_set_options(ssl, SSL_OP_NO_COMPRESSION) &
  14313. SSL_OP_NO_COMPRESSION) == SSL_OP_NO_COMPRESSION);
  14314. AssertNull((SSL_clear_options(ssl, SSL_OP_NO_COMPRESSION) &
  14315. SSL_OP_NO_COMPRESSION));
  14316. AssertTrue(SSL_set_msg_callback(ssl, msg_cb) == SSL_SUCCESS);
  14317. SSL_set_msg_callback_arg(ssl, arg);
  14318. AssertTrue(SSL_CTX_set_alpn_protos(ctx, protos, len) == SSL_SUCCESS);
  14319. SSL_free(ssl);
  14320. SSL_CTX_free(ctx);
  14321. printf(resultFmt, passed);
  14322. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  14323. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  14324. }
  14325. /* Testing wolfSSL_set_tlsext_status_type funciton.
  14326. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  14327. */
  14328. static void test_wolfSSL_set_tlsext_status_type(void){
  14329. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST)
  14330. SSL* ssl;
  14331. SSL_CTX* ctx;
  14332. printf(testingFmt, "wolfSSL_set_tlsext_status_type()");
  14333. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  14334. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  14335. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  14336. AssertNotNull(ssl = SSL_new(ctx));
  14337. AssertTrue(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp)
  14338. == SSL_SUCCESS);
  14339. SSL_free(ssl);
  14340. SSL_CTX_free(ctx);
  14341. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST */
  14342. }
  14343. static void test_wolfSSL_PEM_read_bio(void)
  14344. {
  14345. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  14346. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  14347. byte buff[5300];
  14348. FILE *f;
  14349. int bytes;
  14350. X509* x509;
  14351. BIO* bio = NULL;
  14352. BUF_MEM* buf;
  14353. printf(testingFmt, "wolfSSL_PEM_read_bio()");
  14354. AssertNotNull(f = fopen(cliCertFile, "rb"));
  14355. bytes = (int)fread(buff, 1, sizeof(buff), f);
  14356. fclose(f);
  14357. AssertNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  14358. AssertNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  14359. AssertNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  14360. AssertIntEQ((int)BIO_set_fd(bio, 0, BIO_NOCLOSE), 1);
  14361. AssertIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  14362. BIO_free(bio);
  14363. BUF_MEM_free(buf);
  14364. X509_free(x509);
  14365. printf(resultFmt, passed);
  14366. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  14367. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  14368. }
  14369. static void test_wolfSSL_BIO(void)
  14370. {
  14371. #if defined(OPENSSL_EXTRA)
  14372. byte buff[20];
  14373. BIO* bio1;
  14374. BIO* bio2;
  14375. BIO* bio3;
  14376. char* bufPt;
  14377. int i;
  14378. printf(testingFmt, "wolfSSL_BIO()");
  14379. for (i = 0; i < 20; i++) {
  14380. buff[i] = i;
  14381. }
  14382. /* Creating and testing type BIO_s_bio */
  14383. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  14384. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  14385. AssertNotNull(bio3 = BIO_new(BIO_s_bio()));
  14386. /* read/write before set up */
  14387. AssertIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  14388. AssertIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  14389. AssertIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  14390. AssertIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  14391. AssertIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  14392. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  14393. XMEMCPY(bufPt, buff, 10);
  14394. AssertIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  14395. /* write buffer full */
  14396. AssertIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  14397. AssertIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  14398. AssertIntEQ((int)BIO_ctrl_pending(bio1), 0);
  14399. /* write the other direction with pair */
  14400. AssertIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  14401. XMEMCPY(bufPt, buff, 8);
  14402. AssertIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  14403. /* try read */
  14404. AssertIntEQ((int)BIO_ctrl_pending(bio1), 8);
  14405. AssertIntEQ((int)BIO_ctrl_pending(bio2), 20);
  14406. AssertIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  14407. for (i = 0; i < 20; i++) {
  14408. AssertIntEQ((int)bufPt[i], i);
  14409. }
  14410. AssertIntEQ(BIO_nread(bio2, &bufPt, 1), WOLFSSL_BIO_ERROR);
  14411. AssertIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  14412. for (i = 0; i < 8; i++) {
  14413. AssertIntEQ((int)bufPt[i], i);
  14414. }
  14415. AssertIntEQ(BIO_nread(bio1, &bufPt, 1), WOLFSSL_BIO_ERROR);
  14416. AssertIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  14417. /* new pair */
  14418. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  14419. BIO_free(bio2); /* free bio2 and automaticly remove from pair */
  14420. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  14421. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  14422. AssertIntEQ(BIO_nread(bio3, &bufPt, 10), WOLFSSL_BIO_ERROR);
  14423. /* test wrap around... */
  14424. AssertIntEQ(BIO_reset(bio1), 0);
  14425. AssertIntEQ(BIO_reset(bio3), 0);
  14426. /* fill write buffer, read only small amount then write again */
  14427. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  14428. XMEMCPY(bufPt, buff, 20);
  14429. AssertIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  14430. for (i = 0; i < 4; i++) {
  14431. AssertIntEQ(bufPt[i], i);
  14432. }
  14433. /* try writing over read index */
  14434. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  14435. XMEMSET(bufPt, 0, 4);
  14436. AssertIntEQ((int)BIO_ctrl_pending(bio3), 20);
  14437. /* read and write 0 bytes */
  14438. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  14439. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  14440. /* should read only to end of write buffer then need to read again */
  14441. AssertIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  14442. for (i = 0; i < 16; i++) {
  14443. AssertIntEQ(bufPt[i], buff[4 + i]);
  14444. }
  14445. AssertIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  14446. AssertIntEQ(BIO_nread0(bio3, &bufPt), 4);
  14447. for (i = 0; i < 4; i++) {
  14448. AssertIntEQ(bufPt[i], 0);
  14449. }
  14450. /* read index should not have advanced with nread0 */
  14451. AssertIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  14452. for (i = 0; i < 4; i++) {
  14453. AssertIntEQ(bufPt[i], 0);
  14454. }
  14455. /* write and fill up buffer checking reset of index state */
  14456. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  14457. XMEMCPY(bufPt, buff, 20);
  14458. /* test reset on data in bio1 write buffer */
  14459. AssertIntEQ(BIO_reset(bio1), 0);
  14460. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  14461. AssertIntEQ(BIO_nread(bio3, &bufPt, 3), WOLFSSL_BIO_ERROR);
  14462. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  14463. XMEMCPY(bufPt, buff, 20);
  14464. AssertIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  14465. for (i = 0; i < 6; i++) {
  14466. AssertIntEQ(bufPt[i], i);
  14467. }
  14468. /* test case of writing twice with offset read index */
  14469. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  14470. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  14471. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  14472. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  14473. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  14474. AssertIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  14475. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  14476. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  14477. BIO_free(bio1);
  14478. BIO_free(bio3);
  14479. /* BIOs with file pointers */
  14480. #if !defined(NO_FILESYSTEM)
  14481. {
  14482. XFILE f1;
  14483. XFILE f2;
  14484. BIO* f_bio1;
  14485. BIO* f_bio2;
  14486. unsigned char cert[300];
  14487. char testFile[] = "tests/bio_write_test.txt";
  14488. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  14489. AssertNotNull(f_bio1 = BIO_new(BIO_s_file()));
  14490. AssertNotNull(f_bio2 = BIO_new(BIO_s_file()));
  14491. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  14492. AssertIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  14493. f1 = XFOPEN(svrCertFile, "rwb");
  14494. AssertIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  14495. AssertIntEQ(BIO_write_filename(f_bio2, testFile),
  14496. WOLFSSL_SUCCESS);
  14497. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  14498. AssertIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  14499. AssertIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  14500. AssertIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  14501. AssertIntEQ(BIO_reset(f_bio2), 0);
  14502. AssertIntEQ(BIO_seek(f_bio2, 4), 0);
  14503. BIO_free(f_bio1);
  14504. BIO_free(f_bio2);
  14505. AssertNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  14506. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  14507. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  14508. BIO_free(f_bio1);
  14509. }
  14510. #endif /* !defined(NO_FILESYSTEM) */
  14511. printf(resultFmt, passed);
  14512. #endif
  14513. }
  14514. static void test_wolfSSL_ASN1_STRING(void)
  14515. {
  14516. #if defined(OPENSSL_EXTRA)
  14517. ASN1_STRING* str = NULL;
  14518. const char data[] = "hello wolfSSL";
  14519. printf(testingFmt, "wolfSSL_ASN1_STRING()");
  14520. AssertNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  14521. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  14522. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  14523. AssertIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  14524. ASN1_STRING_free(str);
  14525. printf(resultFmt, passed);
  14526. #endif
  14527. }
  14528. static void test_wolfSSL_DES_ecb_encrypt(void)
  14529. {
  14530. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  14531. WOLFSSL_DES_cblock input1,input2,output1,output2,back1,back2;
  14532. WOLFSSL_DES_key_schedule key;
  14533. printf(testingFmt, "wolfSSL_DES_ecb_encrypt()");
  14534. XMEMCPY(key,"12345678",sizeof(WOLFSSL_DES_key_schedule));
  14535. XMEMCPY(input1, "Iamhuman",sizeof(WOLFSSL_DES_cblock));
  14536. XMEMCPY(input2, "Whoisit?",sizeof(WOLFSSL_DES_cblock));
  14537. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  14538. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  14539. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  14540. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  14541. /* Encrypt messages */
  14542. wolfSSL_DES_ecb_encrypt(&input1,&output1,&key,DES_ENCRYPT);
  14543. wolfSSL_DES_ecb_encrypt(&input2,&output2,&key,DES_ENCRYPT);
  14544. /* Decrypt messages */
  14545. int ret1 = 0;
  14546. int ret2 = 0;
  14547. wolfSSL_DES_ecb_encrypt(&output1,&back1,&key,DES_DECRYPT);
  14548. ret1 = XMEMCMP((unsigned char *) back1,(unsigned char *) input1,sizeof(WOLFSSL_DES_cblock));
  14549. AssertIntEQ(ret1,0);
  14550. wolfSSL_DES_ecb_encrypt(&output2,&back2,&key,DES_DECRYPT);
  14551. ret2 = XMEMCMP((unsigned char *) back2,(unsigned char *) input2,sizeof(WOLFSSL_DES_cblock));
  14552. AssertIntEQ(ret2,0);
  14553. printf(resultFmt, passed);
  14554. #endif
  14555. }
  14556. static void test_wolfSSL_ASN1_TIME_adj(void)
  14557. {
  14558. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN1_TIME) \
  14559. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  14560. const int year = 365*24*60*60;
  14561. const int day = 24*60*60;
  14562. const int hour = 60*60;
  14563. const int mini = 60;
  14564. const byte asn_utc_time = ASN_UTC_TIME;
  14565. #if !defined(TIME_T_NOT_LONG) && !defined(NO_64BIT)
  14566. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  14567. #endif
  14568. WOLFSSL_ASN1_TIME *asn_time, *s;
  14569. int offset_day;
  14570. long offset_sec;
  14571. char date_str[20];
  14572. time_t t;
  14573. printf(testingFmt, "wolfSSL_ASN1_TIME_adj()");
  14574. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  14575. DYNAMIC_TYPE_OPENSSL);
  14576. /* UTC notation test */
  14577. /* 2000/2/15 20:30:00 */
  14578. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  14579. offset_day = 7;
  14580. offset_sec = 45 * mini;
  14581. /* offset_sec = -45 * min;*/
  14582. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  14583. AssertTrue(asn_time->data[0] == asn_utc_time);
  14584. XSTRNCPY(date_str,(const char*) &asn_time->data+2,13);
  14585. AssertIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  14586. /* negative offset */
  14587. offset_sec = -45 * mini;
  14588. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  14589. AssertTrue(asn_time->data[0] == asn_utc_time);
  14590. XSTRNCPY(date_str,(const char*) &asn_time->data+2,13);
  14591. AssertIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  14592. XFREE(s,NULL,DYNAMIC_TYPE_OPENSSL);
  14593. XMEMSET(date_str, 0, sizeof(date_str));
  14594. /* Generalized time will overflow time_t if not long */
  14595. #if !defined(TIME_T_NOT_LONG) && !defined(NO_64BIT)
  14596. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  14597. DYNAMIC_TYPE_OPENSSL);
  14598. /* GeneralizedTime notation test */
  14599. /* 2055/03/01 09:00:00 */
  14600. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  14601. offset_day = 12;
  14602. offset_sec = 10 * mini;
  14603. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  14604. AssertTrue(asn_time->data[0] == asn_gen_time);
  14605. XSTRNCPY(date_str,(const char*) &asn_time->data+2, 15);
  14606. AssertIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  14607. XFREE(s,NULL,DYNAMIC_TYPE_OPENSSL);
  14608. XMEMSET(date_str, 0, sizeof(date_str));
  14609. #endif /* !TIME_T_NOT_LONG && !NO_64BIT */
  14610. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  14611. s = NULL;
  14612. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  14613. offset_day = 7;
  14614. offset_sec = 45 * mini;
  14615. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  14616. AssertTrue(asn_time->data[0] == asn_utc_time);
  14617. XSTRNCPY(date_str,(const char*) &asn_time->data+2,13);
  14618. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  14619. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  14620. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, offset_sec);
  14621. AssertTrue(asn_time->data[0] == asn_utc_time);
  14622. XSTRNCPY(date_str,(const char*) &asn_time->data+2,13);
  14623. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  14624. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  14625. printf(resultFmt, passed);
  14626. #endif
  14627. }
  14628. static void test_wolfSSL_X509(void)
  14629. {
  14630. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)\
  14631. && !defined(NO_RSA)
  14632. X509* x509;
  14633. BIO* bio;
  14634. X509_STORE_CTX* ctx;
  14635. X509_STORE* store;
  14636. char der[] = "certs/ca-cert.der";
  14637. XFILE fp;
  14638. printf(testingFmt, "wolfSSL_X509()");
  14639. AssertNotNull(x509 = X509_new());
  14640. X509_free(x509);
  14641. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  14642. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  14643. AssertIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  14644. AssertNotNull(ctx = X509_STORE_CTX_new());
  14645. AssertIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  14646. AssertNotNull(store = X509_STORE_new());
  14647. AssertIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  14648. AssertIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  14649. AssertIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  14650. X509_STORE_CTX_free(ctx);
  14651. BIO_free(bio);
  14652. /** d2i_X509_fp test **/
  14653. AssertNotNull(fp = XFOPEN(der, "rb"));
  14654. AssertNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  14655. AssertNotNull(x509);
  14656. X509_free(x509);
  14657. XFCLOSE(fp);
  14658. AssertNotNull(fp = XFOPEN(der, "rb"));
  14659. AssertNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  14660. AssertNotNull(x509);
  14661. X509_free(x509);
  14662. XFCLOSE(fp);
  14663. printf(resultFmt, passed);
  14664. #endif
  14665. }
  14666. static void test_wolfSSL_RAND(void)
  14667. {
  14668. #if defined(OPENSSL_EXTRA)
  14669. byte seed[16];
  14670. printf(testingFmt, "wolfSSL_RAND()");
  14671. RAND_seed(seed, sizeof(seed));
  14672. AssertIntEQ(RAND_poll(), 1);
  14673. RAND_cleanup();
  14674. AssertIntEQ(RAND_egd(NULL), -1);
  14675. #ifndef NO_FILESYSTEM
  14676. {
  14677. char fname[100];
  14678. AssertNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  14679. AssertIntEQ(RAND_write_file(NULL), 0);
  14680. }
  14681. #endif
  14682. printf(resultFmt, passed);
  14683. #endif
  14684. }
  14685. static void test_wolfSSL_BUF(void)
  14686. {
  14687. #if defined(OPENSSL_EXTRA)
  14688. BUF_MEM* buf;
  14689. AssertNotNull(buf = BUF_MEM_new());
  14690. AssertIntEQ(BUF_MEM_grow(buf, 10), 10);
  14691. AssertIntEQ(BUF_MEM_grow(buf, -1), 0);
  14692. BUF_MEM_free(buf);
  14693. #endif /* OPENSSL_EXTRA */
  14694. }
  14695. static void test_wolfSSL_pseudo_rand(void)
  14696. {
  14697. #if defined(OPENSSL_EXTRA)
  14698. BIGNUM* bn;
  14699. unsigned char bin[8];
  14700. int i;
  14701. printf(testingFmt, "wolfSSL_pseudo_rand()");
  14702. /* BN_pseudo_rand returns 1 on success 0 on failure
  14703. * int BN_pseudo_rand(BIGNUM* bn, int bits, int top, int bottom) */
  14704. for (i = 0; i < 10; i++) {
  14705. AssertNotNull(bn = BN_new());
  14706. AssertIntEQ(BN_pseudo_rand(bn, 8, 0, 0), SSL_SUCCESS);
  14707. AssertIntGT(BN_bn2bin(bn, bin),0);
  14708. AssertIntEQ((bin[0] & 0x80), 0x80); /* top bit should be set */
  14709. BN_free(bn);
  14710. }
  14711. for (i = 0; i < 10; i++) {
  14712. AssertNotNull(bn = BN_new());
  14713. AssertIntEQ(BN_pseudo_rand(bn, 8, 1, 1), SSL_SUCCESS);
  14714. AssertIntGT(BN_bn2bin(bn, bin),0);
  14715. AssertIntEQ((bin[0] & 0xc1), 0xc1); /* top bit should be set */
  14716. BN_free(bn);
  14717. }
  14718. printf(resultFmt, passed);
  14719. #endif
  14720. }
  14721. static void test_wolfSSL_PKCS8_Compat(void)
  14722. {
  14723. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  14724. PKCS8_PRIV_KEY_INFO* pt;
  14725. BIO* bio;
  14726. FILE* f;
  14727. int bytes;
  14728. char buffer[512];
  14729. printf(testingFmt, "wolfSSL_pkcs8()");
  14730. /* file from wolfssl/certs/ directory */
  14731. AssertNotNull(f = fopen("./certs/ecc-keyPkcs8.pem", "rb"));
  14732. AssertIntGT((bytes = (int)fread(buffer, 1, sizeof(buffer), f)), 0);
  14733. fclose(f);
  14734. AssertNotNull(bio = BIO_new_mem_buf((void*)buffer, bytes));
  14735. AssertNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  14736. BIO_free(bio);
  14737. PKCS8_PRIV_KEY_INFO_free(pt);
  14738. printf(resultFmt, passed);
  14739. #endif
  14740. }
  14741. static void test_wolfSSL_ERR_put_error(void)
  14742. {
  14743. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  14744. const char* file;
  14745. int line;
  14746. printf(testingFmt, "wolfSSL_ERR_put_error()");
  14747. ERR_clear_error(); /* clear out any error nodes */
  14748. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  14749. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  14750. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  14751. AssertIntEQ(ERR_get_error_line(&file, &line), 1);
  14752. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  14753. AssertIntEQ(ERR_get_error_line(&file, &line), 2);
  14754. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  14755. AssertIntEQ(ERR_get_error_line(&file, &line), 3);
  14756. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  14757. AssertIntEQ(ERR_get_error_line(&file, &line), 4);
  14758. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  14759. AssertIntEQ(ERR_get_error_line(&file, &line), 5);
  14760. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  14761. AssertIntEQ(ERR_get_error_line(&file, &line), 6);
  14762. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  14763. AssertIntEQ(ERR_get_error_line(&file, &line), 7);
  14764. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  14765. AssertIntEQ(ERR_get_error_line(&file, &line), 8);
  14766. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  14767. AssertIntEQ(ERR_get_error_line(&file, &line), 9);
  14768. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  14769. AssertIntEQ(ERR_get_error_line(&file, &line), 10);
  14770. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  14771. AssertIntEQ(ERR_get_error_line(&file, &line), 11);
  14772. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  14773. AssertIntEQ(ERR_get_error_line(&file, &line), 12);
  14774. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  14775. AssertIntEQ(ERR_get_error_line(&file, &line), 13);
  14776. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  14777. AssertIntEQ(ERR_get_error_line(&file, &line), 14);
  14778. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  14779. AssertIntEQ(ERR_get_error_line(&file, &line), 15);
  14780. /* try reading past end of error queue */
  14781. file = NULL;
  14782. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  14783. AssertNull(file);
  14784. AssertIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  14785. /* Empty and free up all error nodes */
  14786. ERR_clear_error();
  14787. printf(resultFmt, passed);
  14788. #endif
  14789. }
  14790. static void test_wolfSSL_HMAC(void)
  14791. {
  14792. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  14793. HMAC_CTX hmac;
  14794. ENGINE* e = NULL;
  14795. const unsigned char key[] = "simple test key";
  14796. unsigned char hash[WC_MAX_DIGEST_SIZE];
  14797. unsigned int len;
  14798. printf(testingFmt, "wolfSSL_HMAC()");
  14799. HMAC_CTX_init(&hmac);
  14800. AssertIntEQ(HMAC_Init_ex(&hmac, (void*)key, (int)sizeof(key),
  14801. EVP_sha256(), e), SSL_SUCCESS);
  14802. /* re-using test key as data to hash */
  14803. AssertIntEQ(HMAC_Update(&hmac, key, (int)sizeof(key)), SSL_SUCCESS);
  14804. AssertIntEQ(HMAC_Update(&hmac, NULL, 0), SSL_SUCCESS);
  14805. AssertIntEQ(HMAC_Final(&hmac, hash, &len), SSL_SUCCESS);
  14806. AssertIntEQ(len, (int)WC_SHA256_DIGEST_SIZE);
  14807. HMAC_cleanup(&hmac);
  14808. #endif
  14809. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  14810. len = 0;
  14811. AssertNotNull(HMAC(EVP_sha256(), key, (int)sizeof(key), NULL, 0, hash, &len));
  14812. AssertIntEQ(len, (int)WC_SHA256_DIGEST_SIZE);
  14813. #endif
  14814. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224)
  14815. len = 0;
  14816. AssertNotNull(HMAC(EVP_sha224(), key, (int)sizeof(key), NULL, 0, hash, &len));
  14817. AssertIntEQ(len, (int)WC_SHA224_DIGEST_SIZE);
  14818. #endif
  14819. #if defined(OPENSSL_EXTRA) && (defined(WOLFSSL_SHA384) && defined(WOLFSSL_SHA512))
  14820. len = 0;
  14821. AssertNotNull(HMAC(EVP_sha384(), key, (int)sizeof(key), NULL, 0, hash, &len));
  14822. AssertIntEQ(len, (int)WC_SHA384_DIGEST_SIZE);
  14823. #endif
  14824. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  14825. len = 0;
  14826. AssertNotNull(HMAC(EVP_sha512(), key, (int)sizeof(key), NULL, 0, hash, &len));
  14827. AssertIntEQ(len, (int)WC_SHA512_DIGEST_SIZE);
  14828. #endif
  14829. printf(resultFmt, passed);
  14830. }
  14831. static void test_wolfSSL_OBJ(void)
  14832. {
  14833. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  14834. ASN1_OBJECT* obj = NULL;
  14835. char buf[50];
  14836. printf(testingFmt, "wolfSSL_OBJ()");
  14837. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  14838. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  14839. AssertIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  14840. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  14841. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  14842. ASN1_OBJECT_free(obj);
  14843. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  14844. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256);
  14845. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  14846. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  14847. ASN1_OBJECT_free(obj);
  14848. printf(resultFmt, passed);
  14849. #endif
  14850. }
  14851. static void test_wolfSSL_X509_NAME_ENTRY(void)
  14852. {
  14853. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) \
  14854. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  14855. X509* x509;
  14856. BIO* bio;
  14857. X509_NAME* nm;
  14858. X509_NAME_ENTRY* entry;
  14859. unsigned char cn[] = "another name to add";
  14860. printf(testingFmt, "wolfSSL_X509_NAME_ENTRY()");
  14861. AssertNotNull(x509 =
  14862. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  14863. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  14864. AssertIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  14865. #ifdef WOLFSSL_CERT_REQ
  14866. {
  14867. X509_REQ* req;
  14868. BIO* bReq;
  14869. AssertNotNull(req =
  14870. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  14871. AssertNotNull(bReq = BIO_new(BIO_s_mem()));
  14872. AssertIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  14873. BIO_free(bReq);
  14874. X509_free(req);
  14875. }
  14876. #endif
  14877. AssertNotNull(nm = X509_get_subject_name(x509));
  14878. AssertNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  14879. 0x0c, cn, (int)sizeof(cn)));
  14880. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  14881. X509_NAME_ENTRY_free(entry);
  14882. BIO_free(bio);
  14883. X509_free(x509);
  14884. printf(resultFmt, passed);
  14885. #endif
  14886. }
  14887. static void test_wolfSSL_BIO_gets(void)
  14888. {
  14889. #if defined(OPENSSL_EXTRA)
  14890. BIO* bio;
  14891. BIO* bio2;
  14892. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  14893. char emp[] = "";
  14894. char buffer[20];
  14895. int bufferSz = 20;
  14896. printf(testingFmt, "wolfSSL_X509_BIO_gets()");
  14897. /* try with bad args */
  14898. AssertNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  14899. AssertNull(bio = BIO_new_mem_buf((void*)msg, -1));
  14900. /* try with real msg */
  14901. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, sizeof(msg)));
  14902. XMEMSET(buffer, 0, bufferSz);
  14903. AssertNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  14904. AssertNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  14905. AssertNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  14906. AssertFalse(bio2 != BIO_next(bio));
  14907. /* make buffer filled with no terminating characters */
  14908. XMEMSET(buffer, 1, bufferSz);
  14909. /* BIO_gets reads a line of data */
  14910. AssertIntEQ(BIO_gets(bio, buffer, -3), 0);
  14911. AssertIntEQ(BIO_gets(bio, buffer, bufferSz), 1);
  14912. AssertIntEQ(BIO_gets(bio, buffer, bufferSz), 14);
  14913. AssertStrEQ(buffer, "hello wolfSSL\n");
  14914. AssertIntEQ(BIO_gets(bio, buffer, bufferSz), 19);
  14915. AssertIntEQ(BIO_gets(bio, buffer, bufferSz), 8);
  14916. AssertIntEQ(BIO_gets(bio, buffer, -1), 0);
  14917. /* check not null terminated string */
  14918. BIO_free(bio);
  14919. msg[0] = 0x33;
  14920. msg[1] = 0x33;
  14921. msg[2] = 0x33;
  14922. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  14923. AssertIntEQ(BIO_gets(bio, buffer, 3), 2);
  14924. AssertIntEQ(buffer[0], msg[0]);
  14925. AssertIntEQ(buffer[1], msg[1]);
  14926. AssertIntNE(buffer[2], msg[2]);
  14927. BIO_free(bio);
  14928. msg[3] = 0x33;
  14929. buffer[3] = 0x33;
  14930. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  14931. AssertIntEQ(BIO_gets(bio, buffer, bufferSz), 3);
  14932. AssertIntEQ(buffer[0], msg[0]);
  14933. AssertIntEQ(buffer[1], msg[1]);
  14934. AssertIntEQ(buffer[2], msg[2]);
  14935. AssertIntNE(buffer[3], 0x33); /* make sure null terminator was set */
  14936. /* check reading an empty string */
  14937. BIO_free(bio);
  14938. AssertNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  14939. AssertIntEQ(BIO_gets(bio, buffer, bufferSz), 1); /* just terminator */
  14940. AssertStrEQ(emp, buffer);
  14941. /* check error cases */
  14942. BIO_free(bio);
  14943. AssertIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  14944. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  14945. AssertIntEQ(BIO_gets(bio, buffer, 2), -1); /* nothing to read */
  14946. #if !defined(NO_FILESYSTEM)
  14947. {
  14948. BIO* f_bio;
  14949. XFILE f;
  14950. AssertNotNull(f_bio = BIO_new(BIO_s_file()));
  14951. AssertIntLE(BIO_gets(f_bio, buffer, bufferSz), 0);
  14952. f = XFOPEN(svrCertFile, "rb");
  14953. AssertIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  14954. AssertIntGT(BIO_gets(f_bio, buffer, bufferSz), 0);
  14955. BIO_free(f_bio);
  14956. }
  14957. #endif /* NO_FILESYSTEM */
  14958. BIO_free(bio);
  14959. BIO_free(bio2);
  14960. /* try with type BIO */
  14961. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  14962. sizeof(msg));
  14963. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  14964. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  14965. AssertIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  14966. AssertIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  14967. AssertIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  14968. AssertIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  14969. AssertIntEQ(BIO_gets(bio, buffer, -3), 0);
  14970. AssertIntEQ(BIO_gets(bio, buffer, bufferSz), 1);
  14971. AssertIntEQ(BIO_gets(bio, buffer, bufferSz), 14);
  14972. AssertStrEQ(buffer, "hello wolfSSL\n");
  14973. AssertIntEQ(BIO_gets(bio, buffer, bufferSz), 19);
  14974. AssertIntEQ(BIO_gets(bio, buffer, bufferSz), 8);
  14975. AssertIntEQ(BIO_gets(bio, buffer, -1), 0);
  14976. BIO_free(bio);
  14977. BIO_free(bio2);
  14978. printf(resultFmt, passed);
  14979. #endif
  14980. }
  14981. static void test_wolfSSL_BIO_write(void)
  14982. {
  14983. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  14984. BIO* bio;
  14985. BIO* bio64;
  14986. BIO* ptr;
  14987. int sz;
  14988. char msg[] = "conversion test";
  14989. char out[40];
  14990. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  14991. printf(testingFmt, "wolfSSL_BIO_write()");
  14992. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  14993. AssertNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  14994. /* now should convert to base64 then write to memory */
  14995. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  14996. BIO_flush(bio);
  14997. AssertNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  14998. sz = sizeof(out);
  14999. XMEMSET(out, 0, sz);
  15000. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  15001. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  15002. /* write then read should return the same message */
  15003. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  15004. sz = sizeof(out);
  15005. XMEMSET(out, 0, sz);
  15006. AssertIntEQ(BIO_read(bio, out, sz), 16);
  15007. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  15008. /* now try encoding with no line ending */
  15009. BIO_set_flags(bio64, BIO_FLAG_BASE64_NO_NL);
  15010. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 24);
  15011. BIO_flush(bio);
  15012. sz = sizeof(out);
  15013. XMEMSET(out, 0, sz);
  15014. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  15015. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  15016. BIO_free_all(bio); /* frees bio64 also */
  15017. /* test with more than one bio64 in list */
  15018. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  15019. AssertNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  15020. AssertNotNull(BIO_push(bio64, BIO_new(BIO_s_mem())));
  15021. /* now should convert to base64(x2) when stored and then decode with read */
  15022. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 37);
  15023. BIO_flush(bio);
  15024. sz = sizeof(out);
  15025. XMEMSET(out, 0, sz);
  15026. AssertIntEQ((sz = BIO_read(bio, out, sz)), 16);
  15027. AssertIntEQ(XMEMCMP(out, msg, sz), 0);
  15028. BIO_free_all(bio); /* frees bio64s also */
  15029. printf(resultFmt, passed);
  15030. #endif
  15031. }
  15032. static void test_wolfSSL_SESSION(void)
  15033. {
  15034. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  15035. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  15036. defined(HAVE_IO_TESTS_DEPENDENCIES)
  15037. WOLFSSL* ssl;
  15038. WOLFSSL_CTX* ctx;
  15039. WOLFSSL_SESSION* sess;
  15040. const unsigned char context[] = "user app context";
  15041. unsigned char* sessDer = NULL;
  15042. unsigned char* ptr = NULL;
  15043. unsigned int contextSz = (unsigned int)sizeof(context);
  15044. int ret, err, sockfd, sz;
  15045. tcp_ready ready;
  15046. func_args server_args;
  15047. THREAD_TYPE serverThread;
  15048. printf(testingFmt, "wolfSSL_SESSION()");
  15049. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  15050. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  15051. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  15052. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  15053. #ifdef WOLFSSL_ENCRYPTED_KEYS
  15054. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  15055. #endif
  15056. XMEMSET(&server_args, 0, sizeof(func_args));
  15057. #ifdef WOLFSSL_TIRTOS
  15058. fdOpenSession(Task_self());
  15059. #endif
  15060. StartTCP();
  15061. InitTcpReady(&ready);
  15062. #if defined(USE_WINDOWS_API)
  15063. /* use RNG to get random port if using windows */
  15064. ready.port = GetRandomPort();
  15065. #endif
  15066. server_args.signal = &ready;
  15067. start_thread(test_server_nofail, &server_args, &serverThread);
  15068. wait_tcp_ready(&server_args);
  15069. /* client connection */
  15070. ssl = wolfSSL_new(ctx);
  15071. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  15072. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), SSL_SUCCESS);
  15073. err = 0; /* Reset error */
  15074. do {
  15075. #ifdef WOLFSSL_ASYNC_CRYPT
  15076. if (err == WC_PENDING_E) {
  15077. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  15078. if (ret < 0) { break; } else if (ret == 0) { continue; }
  15079. }
  15080. #endif
  15081. ret = wolfSSL_connect(ssl);
  15082. if (ret != SSL_SUCCESS) {
  15083. err = wolfSSL_get_error(ssl, 0);
  15084. }
  15085. } while (ret != SSL_SUCCESS && err == WC_PENDING_E);
  15086. AssertIntEQ(ret, SSL_SUCCESS);
  15087. sess = wolfSSL_get_session(ssl);
  15088. wolfSSL_shutdown(ssl);
  15089. wolfSSL_free(ssl);
  15090. join_thread(serverThread);
  15091. FreeTcpReady(&ready);
  15092. #ifdef WOLFSSL_TIRTOS
  15093. fdOpenSession(Task_self());
  15094. #endif
  15095. /* get session from DER and update the timeout */
  15096. AssertIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  15097. AssertIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  15098. wolfSSL_SESSION_free(sess);
  15099. ptr = sessDer;
  15100. AssertNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  15101. AssertNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  15102. (const unsigned char**)&ptr, sz));
  15103. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  15104. AssertIntGT(wolfSSL_SESSION_get_time(sess), 0);
  15105. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  15106. /* successful set session test */
  15107. AssertNotNull(ssl = wolfSSL_new(ctx));
  15108. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_SUCCESS);
  15109. /* fail case with miss match session context IDs (use compatibility API) */
  15110. AssertIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  15111. SSL_SUCCESS);
  15112. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  15113. wolfSSL_free(ssl);
  15114. AssertIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  15115. SSL_FAILURE);
  15116. AssertIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  15117. SSL_SUCCESS);
  15118. AssertNotNull(ssl = wolfSSL_new(ctx));
  15119. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  15120. wolfSSL_free(ssl);
  15121. SSL_SESSION_free(sess);
  15122. wolfSSL_CTX_free(ctx);
  15123. printf(resultFmt, passed);
  15124. #endif
  15125. }
  15126. static void test_wolfSSL_d2i_PUBKEY(void)
  15127. {
  15128. #if defined(OPENSSL_EXTRA)
  15129. BIO* bio;
  15130. EVP_PKEY* pkey;
  15131. printf(testingFmt, "wolfSSL_d2i_PUBKEY()");
  15132. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  15133. AssertNull(d2i_PUBKEY_bio(NULL, NULL));
  15134. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  15135. /* RSA PUBKEY test */
  15136. AssertIntGT(BIO_write(bio, client_keypub_der_2048,
  15137. sizeof_client_keypub_der_2048), 0);
  15138. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  15139. EVP_PKEY_free(pkey);
  15140. #endif
  15141. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  15142. /* ECC PUBKEY test */
  15143. AssertIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  15144. sizeof_ecc_clikeypub_der_256), 0);
  15145. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  15146. EVP_PKEY_free(pkey);
  15147. #endif
  15148. BIO_free(bio);
  15149. (void)pkey;
  15150. printf(resultFmt, passed);
  15151. #endif
  15152. }
  15153. static void test_wolfSSL_sk_GENERAL_NAME(void)
  15154. {
  15155. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  15156. !defined(NO_RSA)
  15157. X509* x509;
  15158. unsigned char buf[4096];
  15159. const unsigned char* bufPt;
  15160. int bytes;
  15161. XFILE f;
  15162. STACK_OF(GENERAL_NAME)* sk;
  15163. printf(testingFmt, "wolfSSL_sk_GENERAL_NAME()");
  15164. AssertNotNull(f = XFOPEN(cliCertDerFile, "rb"));
  15165. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  15166. XFCLOSE(f);
  15167. bufPt = buf;
  15168. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  15169. /* current cert has no alt names */
  15170. AssertNull(sk = (WOLF_STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  15171. NID_subject_alt_name, NULL, NULL));
  15172. AssertIntEQ(sk_GENERAL_NAME_num(sk), -1);
  15173. #if 0
  15174. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  15175. GENERAL_NAME* gn = sk_GENERAL_NAME_value(sk, i);
  15176. if (gn == NULL) {
  15177. printf("massive falure\n");
  15178. return -1;
  15179. }
  15180. if (gn->type == GEN_DNS) {
  15181. printf("found type GEN_DNS\n");
  15182. printf("length = %d\n", gn->d.ia5->length);
  15183. printf("data = %s\n", (char*)gn->d.ia5->data);
  15184. }
  15185. if (gn->type == GEN_EMAIL) {
  15186. printf("found type GEN_EMAIL\n");
  15187. printf("length = %d\n", gn->d.ia5->length);
  15188. printf("data = %s\n", (char*)gn->d.ia5->data);
  15189. }
  15190. if (gn->type == GEN_URI) {
  15191. printf("found type GEN_URI\n");
  15192. printf("length = %d\n", gn->d.ia5->length);
  15193. printf("data = %s\n", (char*)gn->d.ia5->data);
  15194. }
  15195. }
  15196. #endif
  15197. X509_free(x509);
  15198. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  15199. printf(resultFmt, passed);
  15200. #endif
  15201. }
  15202. static void test_wolfSSL_MD4(void)
  15203. {
  15204. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  15205. MD4_CTX md4;
  15206. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  15207. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  15208. "789012345678901234567890";
  15209. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  15210. "\xcc\x05\x36";
  15211. int msgSz = (int)XSTRLEN(msg);
  15212. printf(testingFmt, "wolfSSL_MD4()");
  15213. XMEMSET(out, 0, sizeof(out));
  15214. MD4_Init(&md4);
  15215. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  15216. MD4_Final(out, &md4);
  15217. AssertIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  15218. printf(resultFmt, passed);
  15219. #endif
  15220. }
  15221. static void test_wolfSSL_RSA(void)
  15222. {
  15223. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  15224. RSA* rsa;
  15225. printf(testingFmt, "wolfSSL_RSA()");
  15226. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  15227. AssertIntEQ(RSA_size(rsa), 256);
  15228. RSA_free(rsa);
  15229. AssertNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  15230. AssertIntEQ(RSA_size(rsa), 384);
  15231. RSA_free(rsa);
  15232. /* remove for now with odd key size until adjusting rsa key size check with
  15233. wc_MakeRsaKey()
  15234. AssertNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  15235. RSA_free(rsa);
  15236. */
  15237. AssertNull(RSA_generate_key(-1, 3, NULL, NULL));
  15238. AssertNull(RSA_generate_key(511, 3, NULL, NULL)); /* RSA_MIN_SIZE - 1 */
  15239. AssertNull(RSA_generate_key(4097, 3, NULL, NULL)); /* RSA_MAX_SIZE + 1 */
  15240. AssertNull(RSA_generate_key(2048, 0, NULL, NULL));
  15241. printf(resultFmt, passed);
  15242. #endif
  15243. }
  15244. static void test_wolfSSL_RSA_DER(void)
  15245. {
  15246. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  15247. RSA *rsa;
  15248. int i;
  15249. struct
  15250. {
  15251. const unsigned char *der;
  15252. int sz;
  15253. } tbl[] = {
  15254. #ifdef USE_CERT_BUFFERS_1024
  15255. {client_key_der_1024, sizeof_client_key_der_1024},
  15256. {server_key_der_1024, sizeof_server_key_der_1024},
  15257. #endif
  15258. #ifdef USE_CERT_BUFFERS_2048
  15259. {client_key_der_2048, sizeof_client_key_der_2048},
  15260. {server_key_der_2048, sizeof_server_key_der_2048},
  15261. #endif
  15262. {NULL, 0}
  15263. };
  15264. printf(testingFmt, "test_wolfSSL_RSA_DER()");
  15265. for (i = 0; tbl[i].der != NULL; i++)
  15266. {
  15267. AssertNotNull(d2i_RSAPublicKey(&rsa, &tbl[i].der, tbl[i].sz));
  15268. AssertNotNull(rsa);
  15269. RSA_free(rsa);
  15270. }
  15271. printf(resultFmt, passed);
  15272. #endif
  15273. }
  15274. static void test_wolfSSL_verify_depth(void)
  15275. {
  15276. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  15277. WOLFSSL* ssl;
  15278. WOLFSSL_CTX* ctx;
  15279. long depth;
  15280. printf(testingFmt, "test_wolfSSL_verify_depth()");
  15281. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  15282. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  15283. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  15284. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  15285. AssertIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  15286. AssertNotNull(ssl = SSL_new(ctx));
  15287. AssertIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  15288. SSL_free(ssl);
  15289. SSL_CTX_set_verify_depth(ctx, -1);
  15290. AssertIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  15291. SSL_CTX_set_verify_depth(ctx, 2);
  15292. AssertIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  15293. AssertNotNull(ssl = SSL_new(ctx));
  15294. AssertIntEQ(2, SSL_get_verify_depth(ssl));
  15295. SSL_free(ssl);
  15296. SSL_CTX_free(ctx);
  15297. printf(resultFmt, passed);
  15298. #endif
  15299. }
  15300. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  15301. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  15302. * buffer of 64 bytes.
  15303. *
  15304. * returns the size of the digest buffer on success and a negative value on
  15305. * failure.
  15306. */
  15307. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest)
  15308. {
  15309. HMAC_CTX ctx1;
  15310. HMAC_CTX ctx2;
  15311. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  15312. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  15313. unsigned char long_key[] =
  15314. "0123456789012345678901234567890123456789"
  15315. "0123456789012345678901234567890123456789"
  15316. "0123456789012345678901234567890123456789"
  15317. "0123456789012345678901234567890123456789";
  15318. unsigned char msg[] = "message to hash";
  15319. unsigned int digestSz = 64;
  15320. int keySz = sizeof(key);
  15321. int long_keySz = sizeof(long_key);
  15322. int msgSz = sizeof(msg);
  15323. unsigned char digest2[64];
  15324. unsigned int digestSz2 = 64;
  15325. HMAC_CTX_init(&ctx1);
  15326. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  15327. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  15328. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  15329. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  15330. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  15331. HMAC_CTX_cleanup(&ctx1);
  15332. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  15333. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  15334. HMAC_CTX_cleanup(&ctx2);
  15335. AssertIntEQ(digestSz, digestSz2);
  15336. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  15337. /* test HMAC_Init with NULL key */
  15338. /* init after copy */
  15339. printf("test HMAC_Init with NULL key (0)\n");
  15340. HMAC_CTX_init(&ctx1);
  15341. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  15342. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  15343. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  15344. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  15345. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  15346. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  15347. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  15348. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  15349. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  15350. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  15351. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  15352. HMAC_CTX_cleanup(&ctx2);
  15353. AssertIntEQ(digestSz, digestSz2);
  15354. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  15355. /* long key */
  15356. printf("test HMAC_Init with NULL key (1)\n");
  15357. HMAC_CTX_init(&ctx1);
  15358. AssertIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type), SSL_SUCCESS);
  15359. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  15360. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  15361. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  15362. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  15363. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  15364. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  15365. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  15366. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  15367. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  15368. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  15369. HMAC_CTX_cleanup(&ctx2);
  15370. AssertIntEQ(digestSz, digestSz2);
  15371. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  15372. /* init before copy */
  15373. printf("test HMAC_Init with NULL key (2)\n");
  15374. HMAC_CTX_init(&ctx1);
  15375. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  15376. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  15377. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  15378. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  15379. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  15380. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  15381. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  15382. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  15383. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  15384. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  15385. HMAC_CTX_cleanup(&ctx2);
  15386. AssertIntEQ(digestSz, digestSz2);
  15387. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  15388. return digestSz;
  15389. }
  15390. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  15391. static void test_wolfSSL_HMAC_CTX(void)
  15392. {
  15393. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  15394. unsigned char digest[64];
  15395. int digestSz;
  15396. printf(testingFmt, "wolfSSL_HMAC_CTX()");
  15397. #ifndef NO_SHA
  15398. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha1(), digest)), 20);
  15399. AssertIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  15400. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  15401. #endif /* !NO_SHA */
  15402. #ifdef WOLFSSL_SHA224
  15403. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha224(), digest)), 28);
  15404. AssertIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  15405. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  15406. "\x02\x0E", digest, digestSz), 0);
  15407. #endif /* WOLFSSL_SHA224 */
  15408. #ifndef NO_SHA256
  15409. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha256(), digest)), 32);
  15410. AssertIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  15411. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  15412. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  15413. #endif /* !NO_SHA256 */
  15414. #ifdef WOLFSSL_SHA512
  15415. #ifdef WOLFSSL_SHA384
  15416. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha384(), digest)), 48);
  15417. AssertIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  15418. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  15419. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  15420. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  15421. digest, digestSz), 0);
  15422. #endif /* WOLFSSL_SHA384 */
  15423. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha512(), digest)), 64);
  15424. AssertIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  15425. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  15426. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  15427. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  15428. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  15429. digest, digestSz), 0);
  15430. #endif /* WOLFSSL_SHA512 */
  15431. #ifndef NO_MD5
  15432. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_md5(), digest)), 16);
  15433. AssertIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  15434. "\xE4\x98\xDD", digest, digestSz), 0);
  15435. #endif /* !NO_MD5 */
  15436. printf(resultFmt, passed);
  15437. #endif
  15438. }
  15439. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  15440. static void sslMsgCb(int w, int version, int type, const void* buf,
  15441. size_t sz, SSL* ssl, void* arg)
  15442. {
  15443. int i;
  15444. unsigned char* pt = (unsigned char*)buf;
  15445. printf("%s %d bytes of version %d , type %d : ", (w)?"Writing":"Reading",
  15446. (int)sz, version, type);
  15447. for (i = 0; i < (int)sz; i++) printf("%02X", pt[i]);
  15448. printf("\n");
  15449. (void)ssl;
  15450. (void)arg;
  15451. }
  15452. #endif /* OPENSSL_EXTRA */
  15453. static void test_wolfSSL_msg_callback(void)
  15454. {
  15455. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  15456. WOLFSSL* ssl;
  15457. WOLFSSL_CTX* ctx;
  15458. printf(testingFmt, "wolfSSL_msg_callback()");
  15459. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  15460. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  15461. SSL_FILETYPE_PEM));
  15462. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  15463. SSL_FILETYPE_PEM));
  15464. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  15465. SSL_SUCCESS);
  15466. AssertNotNull(ssl = SSL_new(ctx));
  15467. AssertIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  15468. AssertIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  15469. AssertIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  15470. SSL_CTX_free(ctx);
  15471. SSL_free(ssl);
  15472. printf(resultFmt, passed);
  15473. #endif
  15474. }
  15475. static void test_wolfSSL_SHA(void)
  15476. {
  15477. #if defined(OPENSSL_EXTRA)
  15478. printf(testingFmt, "wolfSSL_SHA()");
  15479. #if !defined(NO_SHA)
  15480. {
  15481. const unsigned char in[] = "abc";
  15482. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  15483. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  15484. unsigned char out[WC_SHA_DIGEST_SIZE];
  15485. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  15486. AssertNotNull(SHA1(in, XSTRLEN((char*)in), out));
  15487. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  15488. }
  15489. #endif
  15490. #if !defined(NO_SHA256)
  15491. {
  15492. const unsigned char in[] = "abc";
  15493. unsigned char expected[] = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  15494. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  15495. "\x15\xAD";
  15496. unsigned char out[WC_SHA256_DIGEST_SIZE];
  15497. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  15498. AssertNotNull(SHA256(in, XSTRLEN((char*)in), out));
  15499. AssertIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  15500. }
  15501. #endif
  15502. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_SHA512)
  15503. {
  15504. const unsigned char in[] = "abc";
  15505. unsigned char expected[] = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  15506. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  15507. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  15508. "\xc8\x25\xa7";
  15509. unsigned char out[WC_SHA384_DIGEST_SIZE];
  15510. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  15511. AssertNotNull(SHA384(in, XSTRLEN((char*)in), out));
  15512. AssertIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  15513. }
  15514. #endif
  15515. #if defined(WOLFSSL_SHA512)
  15516. {
  15517. const unsigned char in[] = "abc";
  15518. unsigned char expected[] = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  15519. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  15520. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  15521. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  15522. "\xa5\x4c\xa4\x9f";
  15523. unsigned char out[WC_SHA512_DIGEST_SIZE];
  15524. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  15525. AssertNotNull(SHA512(in, XSTRLEN((char*)in), out));
  15526. AssertIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  15527. }
  15528. #endif
  15529. printf(resultFmt, passed);
  15530. #endif
  15531. }
  15532. static void test_wolfSSL_DH_1536_prime(void)
  15533. {
  15534. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  15535. BIGNUM* bn;
  15536. unsigned char bits[200];
  15537. int sz = 192; /* known binary size */
  15538. const byte expected[] = {
  15539. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  15540. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  15541. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  15542. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  15543. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  15544. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  15545. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  15546. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  15547. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  15548. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  15549. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  15550. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  15551. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  15552. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  15553. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  15554. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  15555. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  15556. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  15557. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  15558. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  15559. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  15560. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  15561. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  15562. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  15563. };
  15564. printf(testingFmt, "wolfSSL_DH_1536_prime()");
  15565. AssertNotNull(bn = get_rfc3526_prime_1536(NULL));
  15566. AssertIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  15567. AssertIntEQ(0, XMEMCMP(expected, bits, sz));
  15568. BN_free(bn);
  15569. printf(resultFmt, passed);
  15570. #endif
  15571. }
  15572. static void test_wolfSSL_AES_ecb_encrypt(void)
  15573. {
  15574. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB)
  15575. AES_KEY aes;
  15576. const byte msg[] =
  15577. {
  15578. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15579. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  15580. };
  15581. const byte verify[] =
  15582. {
  15583. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  15584. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  15585. };
  15586. const byte key[] =
  15587. {
  15588. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  15589. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  15590. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  15591. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  15592. };
  15593. byte out[AES_BLOCK_SIZE];
  15594. printf(testingFmt, "wolfSSL_AES_ecb_encrypt()");
  15595. AssertIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  15596. XMEMSET(out, 0, AES_BLOCK_SIZE);
  15597. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  15598. AssertIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  15599. #ifdef HAVE_AES_DECRYPT
  15600. AssertIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  15601. XMEMSET(out, 0, AES_BLOCK_SIZE);
  15602. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  15603. AssertIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  15604. #endif
  15605. /* test bad arguments */
  15606. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  15607. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  15608. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  15609. printf(resultFmt, passed);
  15610. #endif
  15611. }
  15612. static void test_wolfSSL_SHA256(void)
  15613. {
  15614. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  15615. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS)
  15616. unsigned char input[] =
  15617. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  15618. unsigned char output[] =
  15619. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  15620. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  15621. "\x06\xC1";
  15622. size_t inLen;
  15623. byte hash[WC_SHA256_DIGEST_SIZE];
  15624. printf(testingFmt, "wolfSSL_SHA256()");
  15625. inLen = XSTRLEN((char*)input);
  15626. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  15627. AssertNotNull(SHA256(input, inLen, hash));
  15628. AssertIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  15629. printf(resultFmt, passed);
  15630. #endif
  15631. }
  15632. static void test_wolfSSL_X509_get_serialNumber(void)
  15633. {
  15634. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  15635. !defined(NO_RSA)
  15636. ASN1_INTEGER* a;
  15637. BIGNUM* bn;
  15638. X509* x509;
  15639. printf(testingFmt, "wolfSSL_X509_get_serialNumber()");
  15640. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  15641. SSL_FILETYPE_PEM));
  15642. AssertNotNull(a = X509_get_serialNumber(x509));
  15643. X509_free(x509);
  15644. /* check on value of ASN1 Integer */
  15645. AssertNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  15646. AssertIntEQ(BN_get_word(bn), 1);
  15647. BN_free(bn);
  15648. ASN1_INTEGER_free(a);
  15649. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  15650. a = ASN1_INTEGER_new();
  15651. AssertNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  15652. DYNAMIC_TYPE_OPENSSL));
  15653. a->isDynamic = 1;
  15654. ASN1_INTEGER_free(a);
  15655. printf(resultFmt, passed);
  15656. #endif
  15657. }
  15658. static void test_wolfSSL_OPENSSL_add_all_algorithms(void){
  15659. #if defined(OPENSSL_EXTRA)
  15660. printf(testingFmt, "wolfSSL_OPENSSL_add_all_algorithms()");
  15661. AssertIntEQ(wolfSSL_OPENSSL_add_all_algorithms_noconf(),WOLFSSL_SUCCESS);
  15662. wolfSSL_Cleanup();
  15663. printf(resultFmt, passed);
  15664. #endif
  15665. }
  15666. static void test_wolfSSL_ASN1_STRING_print_ex(void){
  15667. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  15668. ASN1_STRING* asn_str = NULL;
  15669. const char data[] = "Hello wolfSSL!";
  15670. ASN1_STRING* esc_str = NULL;
  15671. const char esc_data[] = "a+;<>";
  15672. BIO *bio;
  15673. unsigned long flags;
  15674. int p_len;
  15675. unsigned char rbuf[255];
  15676. printf(testingFmt, "wolfSSL_ASN1_STRING_print_ex()");
  15677. /* setup */
  15678. XMEMSET(rbuf, 0, 255);
  15679. bio = BIO_new(BIO_s_mem());
  15680. BIO_set_write_buf_size(bio,255);
  15681. asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  15682. ASN1_STRING_set(asn_str, (const void*)data, sizeof(data));
  15683. esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  15684. ASN1_STRING_set(esc_str, (const void*)esc_data, sizeof(esc_data));
  15685. /* no flags */
  15686. XMEMSET(rbuf, 0, 255);
  15687. flags = 0;
  15688. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  15689. AssertIntEQ(p_len, 15);
  15690. BIO_read(bio, (void*)rbuf, 15);
  15691. AssertStrEQ((char*)rbuf, "Hello wolfSSL!");
  15692. /* RFC2253 Escape */
  15693. XMEMSET(rbuf, 0, 255);
  15694. flags = ASN1_STRFLGS_ESC_2253;
  15695. p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags);
  15696. AssertIntEQ(p_len, 9);
  15697. BIO_read(bio, (void*)rbuf, 9);
  15698. AssertStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  15699. /* Show type */
  15700. XMEMSET(rbuf, 0, 255);
  15701. flags = ASN1_STRFLGS_SHOW_TYPE;
  15702. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  15703. AssertIntEQ(p_len, 28);
  15704. BIO_read(bio, (void*)rbuf, 28);
  15705. AssertStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  15706. /* Dump All */
  15707. XMEMSET(rbuf, 0, 255);
  15708. flags = ASN1_STRFLGS_DUMP_ALL;
  15709. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  15710. AssertIntEQ(p_len, 31);
  15711. BIO_read(bio, (void*)rbuf, 31);
  15712. AssertStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  15713. /* Dump Der */
  15714. XMEMSET(rbuf, 0, 255);
  15715. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  15716. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  15717. AssertIntEQ(p_len, 35);
  15718. BIO_read(bio, (void*)rbuf, 35);
  15719. AssertStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  15720. /* Dump All + Show type */
  15721. XMEMSET(rbuf, 0, 255);
  15722. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  15723. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  15724. AssertIntEQ(p_len, 44);
  15725. BIO_read(bio, (void*)rbuf, 44);
  15726. AssertStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  15727. BIO_free(bio);
  15728. ASN1_STRING_free(asn_str);
  15729. ASN1_STRING_free(esc_str);
  15730. printf(resultFmt, passed);
  15731. #endif
  15732. }
  15733. static void test_wolfSSL_ASN1_TIME_to_generalizedtime(void){
  15734. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN1_TIME)
  15735. WOLFSSL_ASN1_TIME *t;
  15736. WOLFSSL_ASN1_TIME *out;
  15737. WOLFSSL_ASN1_TIME *gtime;
  15738. printf(testingFmt, "wolfSSL_ASN1_TIME_to_generalizedtime()");
  15739. /* UTC Time test */
  15740. AssertNotNull(t = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME),
  15741. NULL, DYNAMIC_TYPE_TMP_BUFFER));
  15742. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  15743. AssertNotNull(out = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME),
  15744. NULL, DYNAMIC_TYPE_TMP_BUFFER));
  15745. t->data[0] = ASN_UTC_TIME;
  15746. t->data[1] = ASN_UTC_TIME_SIZE;
  15747. XMEMCPY(t->data + 2,"050727123456Z",ASN_UTC_TIME_SIZE);
  15748. AssertNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  15749. AssertIntEQ(gtime->data[0], ASN_GENERALIZED_TIME);
  15750. AssertIntEQ(gtime->data[1], ASN_GENERALIZED_TIME_SIZE);
  15751. AssertStrEQ((char*)gtime->data + 2, "20050727123456Z");
  15752. /* Generalized Time test */
  15753. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  15754. XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE);
  15755. gtime = NULL;
  15756. t->data[0] = ASN_GENERALIZED_TIME;
  15757. t->data[1] = ASN_GENERALIZED_TIME_SIZE;
  15758. XMEMCPY(t->data + 2,"20050727123456Z",ASN_GENERALIZED_TIME_SIZE);
  15759. AssertNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  15760. AssertIntEQ(gtime->data[0], ASN_GENERALIZED_TIME);
  15761. AssertIntEQ(gtime->data[1], ASN_GENERALIZED_TIME_SIZE);
  15762. AssertStrEQ((char*)gtime->data + 2, "20050727123456Z");
  15763. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15764. /* Null parameter test */
  15765. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  15766. gtime = NULL;
  15767. out = NULL;
  15768. t->data[0] = ASN_UTC_TIME;
  15769. t->data[1] = ASN_UTC_TIME_SIZE;
  15770. XMEMCPY(t->data + 2,"050727123456Z",ASN_UTC_TIME_SIZE);
  15771. AssertNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  15772. AssertIntEQ(gtime->data[0], ASN_GENERALIZED_TIME);
  15773. AssertIntEQ(gtime->data[1], ASN_GENERALIZED_TIME_SIZE);
  15774. AssertStrEQ((char*)gtime->data + 2, "20050727123456Z");
  15775. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15776. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15777. printf(resultFmt, passed);
  15778. #endif
  15779. }
  15780. static void test_wolfSSL_X509_check_ca(void){
  15781. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  15782. WOLFSSL_X509 *x509;
  15783. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  15784. AssertIntEQ(wolfSSL_X509_check_ca(x509), 1);
  15785. wolfSSL_X509_free(x509);
  15786. x509 = wolfSSL_X509_load_certificate_file(ntruCertFile, WOLFSSL_FILETYPE_PEM);
  15787. AssertIntEQ(wolfSSL_X509_check_ca(x509), 0);
  15788. wolfSSL_X509_free(x509);
  15789. #endif
  15790. }
  15791. static void test_no_op_functions(void)
  15792. {
  15793. #if defined(OPENSSL_EXTRA)
  15794. printf(testingFmt, "no_op_functions()");
  15795. /* this makes sure wolfSSL can compile and run these no-op functions */
  15796. SSL_load_error_strings();
  15797. ENGINE_load_builtin_engines();
  15798. OpenSSL_add_all_ciphers();
  15799. CRYPTO_malloc_init();
  15800. printf(resultFmt, passed);
  15801. #endif
  15802. }
  15803. /*----------------------------------------------------------------------------*
  15804. | wolfCrypt ASN
  15805. *----------------------------------------------------------------------------*/
  15806. static void test_wc_GetPkcs8TraditionalOffset(void)
  15807. {
  15808. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM)
  15809. int length, derSz;
  15810. word32 inOutIdx;
  15811. const char* path = "./certs/server-keyPkcs8.der";
  15812. FILE* file;
  15813. byte der[2048];
  15814. printf(testingFmt, "wc_GetPkcs8TraditionalOffset");
  15815. file = fopen(path, "rb");
  15816. AssertNotNull(file);
  15817. derSz = (int)fread(der, 1, sizeof(der), file);
  15818. fclose(file);
  15819. /* valid case */
  15820. inOutIdx = 0;
  15821. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  15822. AssertIntGT(length, 0);
  15823. /* inOutIdx > sz */
  15824. inOutIdx = 4000;
  15825. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  15826. AssertIntEQ(length, BAD_FUNC_ARG);
  15827. /* null input */
  15828. inOutIdx = 0;
  15829. length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0);
  15830. AssertIntEQ(length, BAD_FUNC_ARG);
  15831. /* invalid input, fill buffer with 1's */
  15832. XMEMSET(der, 1, sizeof(der));
  15833. inOutIdx = 0;
  15834. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  15835. AssertIntEQ(length, ASN_PARSE_E);
  15836. printf(resultFmt, passed);
  15837. #endif /* NO_ASN */
  15838. }
  15839. /*----------------------------------------------------------------------------*
  15840. | wolfCrypt ECC
  15841. *----------------------------------------------------------------------------*/
  15842. static void test_wc_ecc_get_curve_size_from_name(void)
  15843. {
  15844. #ifdef HAVE_ECC
  15845. int ret;
  15846. printf(testingFmt, "wc_ecc_get_curve_size_from_name");
  15847. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  15848. ret = wc_ecc_get_curve_size_from_name("SECP256R1");
  15849. AssertIntEQ(ret, 32);
  15850. #endif
  15851. /* invalid case */
  15852. ret = wc_ecc_get_curve_size_from_name("BADCURVE");
  15853. AssertIntEQ(ret, -1);
  15854. /* NULL input */
  15855. ret = wc_ecc_get_curve_size_from_name(NULL);
  15856. AssertIntEQ(ret, BAD_FUNC_ARG);
  15857. printf(resultFmt, passed);
  15858. #endif /* HAVE_ECC */
  15859. }
  15860. static void test_wc_ecc_get_curve_id_from_name(void)
  15861. {
  15862. #ifdef HAVE_ECC
  15863. int id;
  15864. printf(testingFmt, "wc_ecc_get_curve_id_from_name");
  15865. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  15866. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  15867. AssertIntEQ(id, ECC_SECP256R1);
  15868. #endif
  15869. /* invalid case */
  15870. id = wc_ecc_get_curve_id_from_name("BADCURVE");
  15871. AssertIntEQ(id, -1);
  15872. /* NULL input */
  15873. id = wc_ecc_get_curve_id_from_name(NULL);
  15874. AssertIntEQ(id, BAD_FUNC_ARG);
  15875. printf(resultFmt, passed);
  15876. #endif /* HAVE_ECC */
  15877. }
  15878. static void test_wc_ecc_get_curve_id_from_params(void)
  15879. {
  15880. #ifdef HAVE_ECC
  15881. int id;
  15882. const byte prime[] =
  15883. {
  15884. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  15885. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15886. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  15887. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  15888. };
  15889. const byte primeInvalid[] =
  15890. {
  15891. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  15892. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15893. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  15894. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  15895. };
  15896. const byte Af[] =
  15897. {
  15898. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  15899. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15900. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  15901. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  15902. };
  15903. const byte Bf[] =
  15904. {
  15905. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  15906. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  15907. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  15908. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  15909. };
  15910. const byte order[] =
  15911. {
  15912. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  15913. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  15914. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  15915. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  15916. };
  15917. const byte Gx[] =
  15918. {
  15919. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  15920. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  15921. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  15922. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  15923. };
  15924. const byte Gy[] =
  15925. {
  15926. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  15927. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  15928. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  15929. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  15930. };
  15931. int cofactor = 1;
  15932. int fieldSize = 256;
  15933. printf(testingFmt, "wc_ecc_get_curve_id_from_params");
  15934. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  15935. id = wc_ecc_get_curve_id_from_params(fieldSize, prime, sizeof(prime),
  15936. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  15937. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  15938. AssertIntEQ(id, ECC_SECP256R1);
  15939. #endif
  15940. /* invalid case, fieldSize = 0 */
  15941. id = wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  15942. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  15943. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  15944. AssertIntEQ(id, ECC_CURVE_INVALID);
  15945. /* invalid case, NULL prime */
  15946. id = wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  15947. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  15948. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  15949. AssertIntEQ(id, BAD_FUNC_ARG);
  15950. /* invalid case, invalid prime */
  15951. id = wc_ecc_get_curve_id_from_params(fieldSize,
  15952. primeInvalid, sizeof(primeInvalid),
  15953. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  15954. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  15955. AssertIntEQ(id, ECC_CURVE_INVALID);
  15956. printf(resultFmt, passed);
  15957. #endif
  15958. }
  15959. /*----------------------------------------------------------------------------*
  15960. | Certficate Failure Checks
  15961. *----------------------------------------------------------------------------*/
  15962. #ifndef NO_CERTS
  15963. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  15964. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  15965. int type)
  15966. {
  15967. int ret;
  15968. WOLFSSL_CERT_MANAGER* cm = NULL;
  15969. switch (type) {
  15970. case TESTING_RSA:
  15971. #ifdef NO_RSA
  15972. printf("RSA disabled, skipping test\n");
  15973. return ASN_SIG_CONFIRM_E;
  15974. #else
  15975. break;
  15976. #endif
  15977. case TESTING_ECC:
  15978. #ifndef HAVE_ECC
  15979. printf("ECC disabled, skipping test\n");
  15980. return ASN_SIG_CONFIRM_E;
  15981. #else
  15982. break;
  15983. #endif
  15984. default:
  15985. printf("Bad function argument\n");
  15986. return BAD_FUNC_ARG;
  15987. }
  15988. cm = wolfSSL_CertManagerNew();
  15989. if (cm == NULL) {
  15990. printf("wolfSSL_CertManagerNew failed\n");
  15991. return -1;
  15992. }
  15993. #ifndef NO_FILESYSTEM
  15994. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  15995. if (ret != WOLFSSL_SUCCESS) {
  15996. printf("wolfSSL_CertManagerLoadCA failed\n");
  15997. wolfSSL_CertManagerFree(cm);
  15998. return ret;
  15999. }
  16000. #else
  16001. (void)ca;
  16002. #endif
  16003. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz, WOLFSSL_FILETYPE_ASN1);
  16004. /* Let AssertIntEQ handle return code */
  16005. wolfSSL_CertManagerFree(cm);
  16006. return ret;
  16007. }
  16008. static int test_RsaSigFailure_cm(void)
  16009. {
  16010. int ret = 0;
  16011. const char* ca_cert = "./certs/ca-cert.pem";
  16012. const char* server_cert = "./certs/server-cert.der";
  16013. byte* cert_buf = NULL;
  16014. size_t cert_sz = 0;
  16015. ret = load_file(server_cert, &cert_buf, &cert_sz);
  16016. if (ret == 0) {
  16017. /* corrupt DER - invert last byte, which is signature */
  16018. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  16019. /* test bad cert */
  16020. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA);
  16021. }
  16022. printf("Signature failure test: RSA: Ret %d\n", ret);
  16023. if (cert_buf)
  16024. free(cert_buf);
  16025. return ret;
  16026. }
  16027. static int test_EccSigFailure_cm(void)
  16028. {
  16029. int ret = 0;
  16030. /* self-signed ECC cert, so use server cert as CA */
  16031. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  16032. const char* server_cert = "./certs/server-ecc.der";
  16033. byte* cert_buf = NULL;
  16034. size_t cert_sz = 0;
  16035. ret = load_file(server_cert, &cert_buf, &cert_sz);
  16036. if (ret == 0) {
  16037. /* corrupt DER - invert last byte, which is signature */
  16038. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  16039. /* test bad cert */
  16040. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC);
  16041. }
  16042. printf("Signature failure test: ECC: Ret %d\n", ret);
  16043. if (cert_buf)
  16044. free(cert_buf);
  16045. return ret;
  16046. }
  16047. #endif /* NO_CERTS */
  16048. #ifdef WOLFSSL_TLS13
  16049. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  16050. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  16051. #endif
  16052. #ifdef WOLFSSL_EARLY_DATA
  16053. static const char earlyData[] = "Early Data";
  16054. static char earlyDataBuffer[1];
  16055. #endif
  16056. static int test_tls13_apis(void)
  16057. {
  16058. int ret = 0;
  16059. #ifndef WOLFSSL_NO_TLS12
  16060. #ifndef NO_WOLFSSL_CLIENT
  16061. WOLFSSL_CTX* clientTls12Ctx;
  16062. WOLFSSL* clientTls12Ssl;
  16063. #endif
  16064. #ifndef NO_WOLFSSL_SERVER
  16065. WOLFSSL_CTX* serverTls12Ctx;
  16066. WOLFSSL* serverTls12Ssl;
  16067. #endif
  16068. #endif
  16069. #ifndef NO_WOLFSSL_CLIENT
  16070. WOLFSSL_CTX* clientCtx;
  16071. WOLFSSL* clientSsl;
  16072. #endif
  16073. #ifndef NO_WOLFSSL_SERVER
  16074. WOLFSSL_CTX* serverCtx;
  16075. WOLFSSL* serverSsl;
  16076. #ifndef NO_CERTS
  16077. const char* ourCert = svrCertFile;
  16078. const char* ourKey = svrKeyFile;
  16079. #endif
  16080. #endif
  16081. #ifdef WOLFSSL_EARLY_DATA
  16082. int outSz;
  16083. #endif
  16084. int groups[1] = { WOLFSSL_ECC_X25519 };
  16085. int numGroups = 1;
  16086. #ifndef WOLFSSL_NO_TLS12
  16087. #ifndef NO_WOLFSSL_CLIENT
  16088. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  16089. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  16090. #endif
  16091. #ifndef NO_WOLFSSL_SERVER
  16092. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  16093. #ifndef NO_CERTS
  16094. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  16095. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey, WOLFSSL_FILETYPE_PEM);
  16096. #endif
  16097. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  16098. #endif
  16099. #endif
  16100. #ifndef NO_WOLFSSL_CLIENT
  16101. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  16102. clientSsl = wolfSSL_new(clientCtx);
  16103. #endif
  16104. #ifndef NO_WOLFSSL_SERVER
  16105. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  16106. #ifndef NO_CERTS
  16107. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  16108. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  16109. #endif
  16110. serverSsl = wolfSSL_new(serverCtx);
  16111. #endif
  16112. #ifdef WOLFSSL_SEND_HRR_COOKIE
  16113. AssertIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  16114. #ifndef NO_WOLFSSL_CLIENT
  16115. AssertIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  16116. #endif
  16117. #ifndef NO_WOLFSSL_SERVER
  16118. #ifndef WOLFSSL_NO_TLS12
  16119. AssertIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0), BAD_FUNC_ARG);
  16120. #endif
  16121. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  16122. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  16123. WOLFSSL_SUCCESS);
  16124. #endif
  16125. #endif
  16126. #ifdef HAVE_ECC
  16127. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  16128. #ifndef NO_WOLFSSL_SERVER
  16129. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1),
  16130. WOLFSSL_SUCCESS);
  16131. #endif
  16132. #ifndef NO_WOLFSSL_CLIENT
  16133. #ifndef WOLFSSL_NO_TLS12
  16134. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  16135. WOLFSSL_SUCCESS);
  16136. #endif
  16137. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  16138. WOLFSSL_SUCCESS);
  16139. #endif
  16140. #elif defined(HAVE_CURVE25519)
  16141. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  16142. #ifndef NO_WOLFSSL_SERVER
  16143. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  16144. WOLFSSL_SUCCESS);
  16145. #endif
  16146. #ifndef NO_WOLFSSL_CLIENT
  16147. #ifndef WOLFSSL_NO_TLS12
  16148. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  16149. WOLFSSL_SUCCESS);
  16150. #endif
  16151. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  16152. WOLFSSL_SUCCESS);
  16153. #endif
  16154. #else
  16155. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  16156. #ifndef NO_WOLFSSL_CLIENT
  16157. #ifndef WOLFSSL_NO_TLS12
  16158. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  16159. NOT_COMPILED_IN);
  16160. #endif
  16161. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  16162. NOT_COMPILED_IN);
  16163. #endif
  16164. #endif
  16165. AssertIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  16166. #ifndef NO_WOLFSSL_SERVER
  16167. AssertIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  16168. #endif
  16169. #ifndef NO_WOLFSSL_CLIENT
  16170. #ifndef WOLFSSL_NO_TLS12
  16171. AssertIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  16172. #endif
  16173. AssertIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  16174. #endif
  16175. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  16176. #ifndef NO_WOLFSSL_CLIENT
  16177. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  16178. #endif
  16179. #ifndef NO_WOLFSSL_SERVER
  16180. #ifndef WOLFSSL_NO_TLS12
  16181. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  16182. #endif
  16183. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  16184. #endif
  16185. AssertIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  16186. #ifndef NO_WOLFSSL_CLIENT
  16187. AssertIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  16188. #endif
  16189. #ifndef NO_WOLFSSL_SERVER
  16190. #ifndef WOLFSSL_NO_TLS12
  16191. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  16192. #endif
  16193. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  16194. #endif
  16195. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  16196. #ifndef NO_WOLFSSL_CLIENT
  16197. #ifndef WOLFSSL_NO_TLS12
  16198. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  16199. #endif
  16200. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  16201. #endif
  16202. #ifndef NO_WOLFSSL_SERVER
  16203. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  16204. #endif
  16205. AssertIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  16206. #ifndef NO_WOLFSSL_CLIENT
  16207. #ifndef WOLFSSL_NO_TLS12
  16208. AssertIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  16209. #endif
  16210. AssertIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  16211. #endif
  16212. #ifndef NO_WOLFSSL_SERVER
  16213. AssertIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  16214. #endif
  16215. AssertIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  16216. #ifndef NO_WOLFSSL_CLIENT
  16217. #ifndef WOLFSSL_NO_TLS12
  16218. AssertIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  16219. #endif
  16220. AssertIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  16221. #endif
  16222. #ifndef NO_WOLFSSL_SERVER
  16223. AssertIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  16224. #endif
  16225. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  16226. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  16227. #ifndef NO_WOLFSSL_SERVER
  16228. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  16229. #endif
  16230. #ifndef NO_WOLFSSL_CLIENT
  16231. #ifndef WOLFSSL_NO_TLS12
  16232. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  16233. BAD_FUNC_ARG);
  16234. #endif
  16235. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  16236. #endif
  16237. AssertIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  16238. #ifndef NO_WOLFSSL_SERVER
  16239. AssertIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  16240. #endif
  16241. #ifndef NO_WOLFSSL_CLIENT
  16242. #ifndef WOLFSSL_NO_TLS12
  16243. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  16244. BAD_FUNC_ARG);
  16245. #endif
  16246. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  16247. #endif
  16248. AssertIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  16249. #ifndef NO_WOLFSSL_CLIENT
  16250. AssertIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  16251. #endif
  16252. #ifndef NO_WOLFSSL_SERVER
  16253. #ifndef WOLFSSL_NO_TLS12
  16254. AssertIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  16255. BAD_FUNC_ARG);
  16256. #endif
  16257. AssertIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  16258. #endif
  16259. #endif
  16260. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  16261. AssertIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  16262. #ifndef NO_WOLFSSL_SERVER
  16263. AssertIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  16264. #endif
  16265. #ifndef NO_WOLFSSL_CLIENT
  16266. #ifndef WOLFSSL_NO_TLS12
  16267. AssertIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  16268. #endif
  16269. AssertIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  16270. #endif
  16271. #endif
  16272. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  16273. #ifndef NO_WOLFSSL_CLIENT
  16274. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  16275. #endif
  16276. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  16277. #ifndef NO_WOLFSSL_CLIENT
  16278. #ifndef WOLFSSL_NO_TLS12
  16279. AssertIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  16280. BAD_FUNC_ARG);
  16281. #endif
  16282. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  16283. WOLFSSL_MAX_GROUP_COUNT + 1),
  16284. BAD_FUNC_ARG);
  16285. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  16286. WOLFSSL_SUCCESS);
  16287. #endif
  16288. #ifndef NO_WOLFSSL_SERVER
  16289. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  16290. WOLFSSL_SUCCESS);
  16291. #endif
  16292. AssertIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  16293. #ifndef NO_WOLFSSL_CLIENT
  16294. AssertIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  16295. #endif
  16296. AssertIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  16297. #ifndef NO_WOLFSSL_CLIENT
  16298. #ifndef WOLFSSL_NO_TLS12
  16299. AssertIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  16300. BAD_FUNC_ARG);
  16301. #endif
  16302. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups,
  16303. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  16304. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  16305. WOLFSSL_SUCCESS);
  16306. #endif
  16307. #ifndef NO_WOLFSSL_SERVER
  16308. AssertIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  16309. WOLFSSL_SUCCESS);
  16310. #endif
  16311. #ifdef WOLFSSL_EARLY_DATA
  16312. AssertIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  16313. #ifndef NO_WOLFSSL_CLIENT
  16314. AssertIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  16315. #endif
  16316. #ifndef NO_WOLFSSL_SERVER
  16317. #ifndef WOLFSSL_NO_TLS12
  16318. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  16319. BAD_FUNC_ARG);
  16320. #endif
  16321. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 0), 0);
  16322. #endif
  16323. AssertIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  16324. #ifndef NO_WOLFSSL_CLIENT
  16325. AssertIntEQ(wolfSSL_set_max_early_data(clientSsl, 0), SIDE_ERROR);
  16326. #endif
  16327. #ifndef NO_WOLFSSL_SERVER
  16328. #ifndef WOLFSSL_NO_TLS12
  16329. AssertIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  16330. #endif
  16331. AssertIntEQ(wolfSSL_set_max_early_data(serverSsl, 0), 0);
  16332. #endif
  16333. AssertIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  16334. &outSz), BAD_FUNC_ARG);
  16335. #ifndef NO_WOLFSSL_CLIENT
  16336. AssertIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  16337. &outSz), BAD_FUNC_ARG);
  16338. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  16339. BAD_FUNC_ARG);
  16340. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  16341. sizeof(earlyData), NULL),
  16342. BAD_FUNC_ARG);
  16343. #endif
  16344. #ifndef NO_WOLFSSL_SERVER
  16345. AssertIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  16346. sizeof(earlyData), &outSz),
  16347. SIDE_ERROR);
  16348. #endif
  16349. #ifndef NO_WOLFSSL_CLIENT
  16350. #ifndef WOLFSSL_NO_TLS12
  16351. AssertIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  16352. sizeof(earlyData), &outSz),
  16353. BAD_FUNC_ARG);
  16354. #endif
  16355. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  16356. sizeof(earlyData), &outSz),
  16357. WOLFSSL_FATAL_ERROR);
  16358. #endif
  16359. AssertIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  16360. sizeof(earlyDataBuffer), &outSz),
  16361. BAD_FUNC_ARG);
  16362. #ifndef NO_WOLFSSL_SERVER
  16363. AssertIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  16364. sizeof(earlyDataBuffer), &outSz),
  16365. BAD_FUNC_ARG);
  16366. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1, &outSz),
  16367. BAD_FUNC_ARG);
  16368. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  16369. sizeof(earlyDataBuffer), NULL),
  16370. BAD_FUNC_ARG);
  16371. #endif
  16372. #ifndef NO_WOLFSSL_CLIENT
  16373. AssertIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  16374. sizeof(earlyDataBuffer), &outSz),
  16375. SIDE_ERROR);
  16376. #endif
  16377. #ifndef NO_WOLFSSL_SERVER
  16378. #ifndef WOLFSSL_NO_TLS12
  16379. AssertIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  16380. sizeof(earlyDataBuffer), &outSz),
  16381. BAD_FUNC_ARG);
  16382. #endif
  16383. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  16384. sizeof(earlyDataBuffer), &outSz),
  16385. WOLFSSL_FATAL_ERROR);
  16386. #endif
  16387. #endif
  16388. #ifndef NO_WOLFSSL_SERVER
  16389. wolfSSL_free(serverSsl);
  16390. wolfSSL_CTX_free(serverCtx);
  16391. #endif
  16392. #ifndef NO_WOLFSSL_CLIENT
  16393. wolfSSL_free(clientSsl);
  16394. wolfSSL_CTX_free(clientCtx);
  16395. #endif
  16396. #ifndef WOLFSSL_NO_TLS12
  16397. #ifndef NO_WOLFSSL_SERVER
  16398. wolfSSL_free(serverTls12Ssl);
  16399. wolfSSL_CTX_free(serverTls12Ctx);
  16400. #endif
  16401. #ifndef NO_WOLFSSL_CLIENT
  16402. wolfSSL_free(clientTls12Ssl);
  16403. wolfSSL_CTX_free(clientTls12Ctx);
  16404. #endif
  16405. #endif
  16406. return ret;
  16407. }
  16408. #endif
  16409. #ifdef HAVE_PK_CALLBACKS
  16410. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  16411. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  16412. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  16413. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  16414. const unsigned char* priv, unsigned int privSz,
  16415. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  16416. unsigned char* out, unsigned int* outlen,
  16417. void* ctx)
  16418. {
  16419. /* Test fail when context associated with WOLFSSL is NULL */
  16420. if (ctx == NULL) {
  16421. return -1;
  16422. }
  16423. (void)ssl;
  16424. /* return 0 on success */
  16425. return wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  16426. };
  16427. static void test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  16428. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  16429. #ifdef WOLFSSL_AES_128
  16430. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  16431. WOLFSSL_SUCCESS);
  16432. #endif
  16433. #ifdef WOLFSSL_AES_256
  16434. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  16435. WOLFSSL_SUCCESS);
  16436. #endif
  16437. }
  16438. static void test_dh_ssl_setup(WOLFSSL* ssl)
  16439. {
  16440. static int dh_test_ctx = 1;
  16441. int ret;
  16442. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  16443. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  16444. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  16445. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  16446. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  16447. }
  16448. }
  16449. static void test_dh_ssl_setup_fail(WOLFSSL* ssl)
  16450. {
  16451. int ret;
  16452. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  16453. AssertNull(wolfSSL_GetDhAgreeCtx(ssl));
  16454. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  16455. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  16456. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  16457. }
  16458. }
  16459. #endif
  16460. static void test_DhCallbacks(void)
  16461. {
  16462. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  16463. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  16464. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  16465. WOLFSSL_CTX *ctx;
  16466. WOLFSSL *ssl;
  16467. tcp_ready ready;
  16468. func_args server_args;
  16469. func_args client_args;
  16470. THREAD_TYPE serverThread;
  16471. callback_functions func_cb_client;
  16472. callback_functions func_cb_server;
  16473. int test;
  16474. printf(testingFmt, "test_DhCallbacks");
  16475. #ifndef NO_WOLFSSL_CLIENT
  16476. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  16477. #else
  16478. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  16479. #endif
  16480. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  16481. /* load client ca cert */
  16482. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  16483. WOLFSSL_SUCCESS);
  16484. /* test with NULL arguments */
  16485. wolfSSL_SetDhAgreeCtx(NULL, &test);
  16486. AssertNull(wolfSSL_GetDhAgreeCtx(NULL));
  16487. /* test success case */
  16488. test = 1;
  16489. AssertNotNull(ssl = wolfSSL_new(ctx));
  16490. wolfSSL_SetDhAgreeCtx(ssl, &test);
  16491. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  16492. wolfSSL_free(ssl);
  16493. wolfSSL_CTX_free(ctx);
  16494. /* test a connection where callback is used */
  16495. #ifdef WOLFSSL_TIRTOS
  16496. fdOpenSession(Task_self());
  16497. #endif
  16498. XMEMSET(&server_args, 0, sizeof(func_args));
  16499. XMEMSET(&client_args, 0, sizeof(func_args));
  16500. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  16501. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  16502. StartTCP();
  16503. InitTcpReady(&ready);
  16504. #if defined(USE_WINDOWS_API)
  16505. /* use RNG to get random port if using windows */
  16506. ready.port = GetRandomPort();
  16507. #endif
  16508. server_args.signal = &ready;
  16509. client_args.signal = &ready;
  16510. server_args.return_code = TEST_FAIL;
  16511. client_args.return_code = TEST_FAIL;
  16512. /* set callbacks to use DH functions */
  16513. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  16514. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  16515. #ifndef WOLFSSL_NO_TLS12
  16516. func_cb_client.method = wolfTLSv1_2_client_method;
  16517. #else
  16518. func_cb_client.method = wolfTLSv1_3_client_method;
  16519. #endif
  16520. client_args.callbacks = &func_cb_client;
  16521. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  16522. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  16523. #ifndef WOLFSSL_NO_TLS12
  16524. func_cb_server.method = wolfTLSv1_2_server_method;
  16525. #else
  16526. func_cb_server.method = wolfTLSv1_3_server_method;
  16527. #endif
  16528. server_args.callbacks = &func_cb_server;
  16529. start_thread(test_server_nofail, &server_args, &serverThread);
  16530. wait_tcp_ready(&server_args);
  16531. test_client_nofail(&client_args, NULL);
  16532. join_thread(serverThread);
  16533. AssertTrue(client_args.return_code);
  16534. AssertTrue(server_args.return_code);
  16535. FreeTcpReady(&ready);
  16536. #ifdef WOLFSSL_TIRTOS
  16537. fdOpenSession(Task_self());
  16538. #endif
  16539. /* now set user ctx to not be 1 so that the callback returns fail case */
  16540. #ifdef WOLFSSL_TIRTOS
  16541. fdOpenSession(Task_self());
  16542. #endif
  16543. XMEMSET(&server_args, 0, sizeof(func_args));
  16544. XMEMSET(&client_args, 0, sizeof(func_args));
  16545. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  16546. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  16547. StartTCP();
  16548. InitTcpReady(&ready);
  16549. #if defined(USE_WINDOWS_API)
  16550. /* use RNG to get random port if using windows */
  16551. ready.port = GetRandomPort();
  16552. #endif
  16553. server_args.signal = &ready;
  16554. client_args.signal = &ready;
  16555. server_args.return_code = TEST_FAIL;
  16556. client_args.return_code = TEST_FAIL;
  16557. /* set callbacks to use DH functions */
  16558. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  16559. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  16560. #ifndef WOLFSSL_NO_TLS12
  16561. func_cb_client.method = wolfTLSv1_2_client_method;
  16562. #else
  16563. func_cb_client.method = wolfTLSv1_3_client_method;
  16564. #endif
  16565. client_args.callbacks = &func_cb_client;
  16566. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  16567. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  16568. #ifndef WOLFSSL_NO_TLS12
  16569. func_cb_server.method = wolfTLSv1_2_server_method;
  16570. #else
  16571. func_cb_server.method = wolfTLSv1_3_server_method;
  16572. #endif
  16573. server_args.callbacks = &func_cb_server;
  16574. start_thread(test_server_nofail, &server_args, &serverThread);
  16575. wait_tcp_ready(&server_args);
  16576. test_client_nofail(&client_args, NULL);
  16577. join_thread(serverThread);
  16578. AssertIntEQ(client_args.return_code, TEST_FAIL);
  16579. AssertIntEQ(server_args.return_code, TEST_FAIL);
  16580. FreeTcpReady(&ready);
  16581. #ifdef WOLFSSL_TIRTOS
  16582. fdOpenSession(Task_self());
  16583. #endif
  16584. printf(resultFmt, passed);
  16585. #endif
  16586. }
  16587. #endif /* HAVE_PK_CALLBACKS */
  16588. #ifdef HAVE_HASHDRBG
  16589. #ifdef TEST_RESEED_INTERVAL
  16590. static int test_wc_RNG_GenerateBlock_Reseed()
  16591. {
  16592. int i, ret;
  16593. WC_RNG rng;
  16594. byte key[32];
  16595. ret = wc_InitRng(&rng);
  16596. if (ret == 0) {
  16597. for(i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  16598. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  16599. if (ret != 0) {
  16600. break;
  16601. }
  16602. }
  16603. }
  16604. wc_FreeRng(&rng);
  16605. return ret;
  16606. }
  16607. #endif /* TEST_RESEED_INTERVAL */
  16608. static int test_wc_RNG_GenerateBlock()
  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 < 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
  16626. static void test_wolfSSL_X509_CRL(void)
  16627. {
  16628. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  16629. X509_CRL *crl;
  16630. char pem[][100] = {
  16631. "./certs/crl/crl.pem",
  16632. "./certs/crl/crl2.pem",
  16633. "./certs/crl/caEccCrl.pem",
  16634. "./certs/crl/eccCliCRL.pem",
  16635. "./certs/crl/eccSrvCRL.pem",
  16636. ""
  16637. };
  16638. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  16639. char der[][100] = {
  16640. "./certs/crl/crl.der",
  16641. "./certs/crl/crl2.der",
  16642. ""};
  16643. #endif
  16644. XFILE fp;
  16645. int i;
  16646. printf(testingFmt, "test_wolfSSL_X509_CRL");
  16647. for (i = 0; pem[i][0] != '\0'; i++)
  16648. {
  16649. AssertNotNull(fp = XFOPEN(pem[i], "rb"));
  16650. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  16651. AssertNotNull(crl);
  16652. X509_CRL_free(crl);
  16653. XFCLOSE(fp);
  16654. AssertNotNull(fp = XFOPEN(pem[i], "rb"));
  16655. AssertNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL, NULL));
  16656. AssertNotNull(crl);
  16657. X509_CRL_free(crl);
  16658. XFCLOSE(fp);
  16659. }
  16660. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  16661. for(i = 0; der[i][0] != '\0'; i++){
  16662. AssertNotNull(fp = XFOPEN(der[i], "rb"));
  16663. AssertNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  16664. AssertNotNull(crl);
  16665. X509_CRL_free(crl);
  16666. XFCLOSE(fp);
  16667. AssertNotNull(fp = XFOPEN(der[i], "rb"));
  16668. AssertNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  16669. AssertNotNull(crl);
  16670. X509_CRL_free(crl);
  16671. XFCLOSE(fp);
  16672. }
  16673. #endif
  16674. printf(resultFmt, passed);
  16675. #endif
  16676. return;
  16677. }
  16678. static void test_wolfSSL_i2c_ASN1_INTEGER()
  16679. {
  16680. #ifdef OPENSSL_EXTRA
  16681. ASN1_INTEGER *a;
  16682. unsigned char *pp,*tpp;
  16683. int ret;
  16684. a = wolfSSL_ASN1_INTEGER_new();
  16685. /* 40 */
  16686. a->intData[0] = ASN_INTEGER;
  16687. a->intData[1] = 1;
  16688. a->intData[2] = 40;
  16689. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  16690. AssertIntEQ(ret, 1);
  16691. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  16692. DYNAMIC_TYPE_TMP_BUFFER));
  16693. tpp = pp;
  16694. XMEMSET(pp, 0, ret + 1);
  16695. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  16696. pp--;
  16697. AssertIntEQ(*pp, 40);
  16698. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16699. /* 128 */
  16700. a->intData[0] = ASN_INTEGER;
  16701. a->intData[1] = 1;
  16702. a->intData[2] = 128;
  16703. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  16704. AssertIntEQ(ret, 2);
  16705. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  16706. DYNAMIC_TYPE_TMP_BUFFER));
  16707. tpp = pp;
  16708. XMEMSET(pp, 0, ret + 1);
  16709. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  16710. pp--;
  16711. AssertIntEQ(*(pp--), 128);
  16712. AssertIntEQ(*pp, 0);
  16713. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16714. /* -40 */
  16715. a->intData[0] = ASN_INTEGER;
  16716. a->intData[1] = 1;
  16717. a->intData[2] = 40;
  16718. a->negative = 1;
  16719. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  16720. AssertIntEQ(ret, 1);
  16721. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  16722. DYNAMIC_TYPE_TMP_BUFFER));
  16723. tpp = pp;
  16724. XMEMSET(pp, 0, ret + 1);
  16725. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  16726. pp--;
  16727. AssertIntEQ(*pp, 216);
  16728. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16729. /* -128 */
  16730. a->intData[0] = ASN_INTEGER;
  16731. a->intData[1] = 1;
  16732. a->intData[2] = 128;
  16733. a->negative = 1;
  16734. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  16735. AssertIntEQ(ret, 1);
  16736. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  16737. DYNAMIC_TYPE_TMP_BUFFER));
  16738. tpp = pp;
  16739. XMEMSET(pp, 0, ret + 1);
  16740. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  16741. pp--;
  16742. AssertIntEQ(*pp, 128);
  16743. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16744. /* -200 */
  16745. a->intData[0] = ASN_INTEGER;
  16746. a->intData[1] = 1;
  16747. a->intData[2] = 200;
  16748. a->negative = 1;
  16749. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  16750. AssertIntEQ(ret, 2);
  16751. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  16752. DYNAMIC_TYPE_TMP_BUFFER));
  16753. tpp = pp;
  16754. XMEMSET(pp, 0, ret + 1);
  16755. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  16756. pp--;
  16757. AssertIntEQ(*(pp--), 56);
  16758. AssertIntEQ(*pp, 255);
  16759. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16760. wolfSSL_ASN1_INTEGER_free(a);
  16761. printf(resultFmt, passed);
  16762. #endif /* OPENSSL_EXTRA */
  16763. }
  16764. /*----------------------------------------------------------------------------*
  16765. | Main
  16766. *----------------------------------------------------------------------------*/
  16767. void ApiTest(void)
  16768. {
  16769. printf(" Begin API Tests\n");
  16770. AssertIntEQ(test_wolfSSL_Init(), WOLFSSL_SUCCESS);
  16771. /* wolfcrypt initialization tests */
  16772. test_wolfSSL_Method_Allocators();
  16773. #ifndef NO_WOLFSSL_SERVER
  16774. test_wolfSSL_CTX_new(wolfSSLv23_server_method());
  16775. #endif
  16776. test_wolfSSL_CTX_use_certificate_file();
  16777. AssertIntEQ(test_wolfSSL_CTX_use_certificate_buffer(), WOLFSSL_SUCCESS);
  16778. test_wolfSSL_CTX_use_PrivateKey_file();
  16779. test_wolfSSL_CTX_load_verify_locations();
  16780. test_wolfSSL_CTX_trust_peer_cert();
  16781. test_wolfSSL_CTX_SetTmpDH_file();
  16782. test_wolfSSL_CTX_SetTmpDH_buffer();
  16783. test_server_wolfSSL_new();
  16784. test_client_wolfSSL_new();
  16785. test_wolfSSL_SetTmpDH_file();
  16786. test_wolfSSL_SetTmpDH_buffer();
  16787. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  16788. test_wolfSSL_read_write();
  16789. #endif
  16790. test_wolfSSL_dtls_export();
  16791. AssertIntEQ(test_wolfSSL_SetMinVersion(), WOLFSSL_SUCCESS);
  16792. AssertIntEQ(test_wolfSSL_CTX_SetMinVersion(), WOLFSSL_SUCCESS);
  16793. /* TLS extensions tests */
  16794. test_wolfSSL_UseSNI();
  16795. test_wolfSSL_UseMaxFragment();
  16796. test_wolfSSL_UseTruncatedHMAC();
  16797. test_wolfSSL_UseSupportedCurve();
  16798. test_wolfSSL_UseALPN();
  16799. test_wolfSSL_DisableExtendedMasterSecret();
  16800. /* X509 tests */
  16801. test_wolfSSL_X509_NAME_get_entry();
  16802. test_wolfSSL_PKCS12();
  16803. test_wolfSSL_PKCS8();
  16804. test_wolfSSL_PKCS5();
  16805. test_wolfSSL_URI();
  16806. /*OCSP Stapling. */
  16807. AssertIntEQ(test_wolfSSL_UseOCSPStapling(), WOLFSSL_SUCCESS);
  16808. AssertIntEQ(test_wolfSSL_UseOCSPStaplingV2(), WOLFSSL_SUCCESS);
  16809. /* Multicast */
  16810. test_wolfSSL_mcast();
  16811. /* compatibility tests */
  16812. test_wolfSSL_X509_NAME();
  16813. test_wolfSSL_DES();
  16814. test_wolfSSL_certs();
  16815. test_wolfSSL_ASN1_TIME_print();
  16816. test_wolfSSL_ASN1_GENERALIZEDTIME_free();
  16817. test_wolfSSL_private_keys();
  16818. test_wolfSSL_PEM_PrivateKey();
  16819. test_wolfSSL_PEM_RSAPrivateKey();
  16820. test_wolfSSL_tmp_dh();
  16821. test_wolfSSL_ctrl();
  16822. test_wolfSSL_EVP_PKEY_new_mac_key();
  16823. test_wolfSSL_EVP_MD_hmac_signing();
  16824. test_wolfSSL_CTX_add_extra_chain_cert();
  16825. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  16826. test_wolfSSL_ERR_peek_last_error_line();
  16827. #endif
  16828. test_wolfSSL_set_options();
  16829. test_wolfSSL_X509_STORE_CTX();
  16830. test_wolfSSL_msgCb();
  16831. test_wolfSSL_X509_STORE_set_flags();
  16832. test_wolfSSL_X509_LOOKUP_load_file();
  16833. test_wolfSSL_X509_NID();
  16834. test_wolfSSL_X509_STORE_CTX_set_time();
  16835. test_wolfSSL_X509_STORE();
  16836. test_wolfSSL_BN();
  16837. test_wolfSSL_PEM_read_bio();
  16838. test_wolfSSL_BIO();
  16839. test_wolfSSL_ASN1_STRING();
  16840. test_wolfSSL_X509();
  16841. test_wolfSSL_RAND();
  16842. test_wolfSSL_BUF();
  16843. test_wolfSSL_set_tlsext_status_type();
  16844. test_wolfSSL_ASN1_TIME_adj();
  16845. test_wolfSSL_CTX_set_client_CA_list();
  16846. test_wolfSSL_CTX_add_client_CA();
  16847. test_wolfSSL_CTX_set_srp_username();
  16848. test_wolfSSL_CTX_set_srp_password();
  16849. test_wolfSSL_pseudo_rand();
  16850. test_wolfSSL_PKCS8_Compat();
  16851. test_wolfSSL_ERR_put_error();
  16852. test_wolfSSL_HMAC();
  16853. test_wolfSSL_OBJ();
  16854. test_wolfSSL_X509_NAME_ENTRY();
  16855. test_wolfSSL_BIO_gets();
  16856. test_wolfSSL_d2i_PUBKEY();
  16857. test_wolfSSL_BIO_write();
  16858. test_wolfSSL_SESSION();
  16859. test_wolfSSL_DES_ecb_encrypt();
  16860. test_wolfSSL_sk_GENERAL_NAME();
  16861. test_wolfSSL_MD4();
  16862. test_wolfSSL_RSA();
  16863. test_wolfSSL_RSA_DER();
  16864. test_wolfSSL_verify_depth();
  16865. test_wolfSSL_HMAC_CTX();
  16866. test_wolfSSL_msg_callback();
  16867. test_wolfSSL_SHA();
  16868. test_wolfSSL_DH_1536_prime();
  16869. test_wolfSSL_AES_ecb_encrypt();
  16870. test_wolfSSL_SHA256();
  16871. test_wolfSSL_X509_get_serialNumber();
  16872. test_wolfSSL_X509_CRL();
  16873. test_wolfSSL_OPENSSL_add_all_algorithms();
  16874. test_wolfSSL_ASN1_STRING_print_ex();
  16875. test_wolfSSL_ASN1_TIME_to_generalizedtime();
  16876. test_wolfSSL_i2c_ASN1_INTEGER();
  16877. test_wolfSSL_X509_check_ca();
  16878. /* test the no op functions for compatibility */
  16879. test_no_op_functions();
  16880. AssertIntEQ(test_wolfSSL_Cleanup(), WOLFSSL_SUCCESS);
  16881. /* wolfCrypt ASN tests */
  16882. test_wc_GetPkcs8TraditionalOffset();
  16883. /* wolfCrypt ECC tests */
  16884. test_wc_ecc_get_curve_size_from_name();
  16885. test_wc_ecc_get_curve_id_from_name();
  16886. test_wc_ecc_get_curve_id_from_params();
  16887. #ifdef WOLFSSL_TLS13
  16888. /* TLS v1.3 API tests */
  16889. test_tls13_apis();
  16890. #endif
  16891. #ifndef NO_CERTS
  16892. /* Bad certificate signature tests */
  16893. AssertIntEQ(test_EccSigFailure_cm(), ASN_SIG_CONFIRM_E);
  16894. AssertIntEQ(test_RsaSigFailure_cm(), ASN_SIG_CONFIRM_E);
  16895. #endif /* NO_CERTS */
  16896. #ifdef HAVE_PK_CALLBACKS
  16897. /* public key callback tests */
  16898. test_DhCallbacks();
  16899. #endif
  16900. /*wolfcrypt */
  16901. printf("\n-----------------wolfcrypt unit tests------------------\n");
  16902. AssertFalse(test_wolfCrypt_Init());
  16903. AssertFalse(test_wc_InitMd5());
  16904. AssertFalse(test_wc_Md5Update());
  16905. AssertFalse(test_wc_Md5Final());
  16906. AssertFalse(test_wc_InitSha());
  16907. AssertFalse(test_wc_ShaUpdate());
  16908. AssertFalse(test_wc_ShaFinal());
  16909. AssertFalse(test_wc_InitSha256());
  16910. AssertFalse(test_wc_Sha256Update());
  16911. AssertFalse(test_wc_Sha256Final());
  16912. AssertFalse(test_wc_InitSha512());
  16913. AssertFalse(test_wc_Sha512Update());
  16914. AssertFalse(test_wc_Sha512Final());
  16915. AssertFalse(test_wc_InitSha384());
  16916. AssertFalse(test_wc_Sha384Update());
  16917. AssertFalse(test_wc_Sha384Final());
  16918. AssertFalse(test_wc_InitSha224());
  16919. AssertFalse(test_wc_Sha224Update());
  16920. AssertFalse(test_wc_Sha224Final());
  16921. AssertFalse(test_wc_InitBlake2b());
  16922. AssertFalse(test_wc_InitRipeMd());
  16923. AssertFalse(test_wc_RipeMdUpdate());
  16924. AssertFalse(test_wc_RipeMdFinal());
  16925. AssertIntEQ(test_wc_InitSha3(), 0);
  16926. AssertIntEQ(testing_wc_Sha3_Update(), 0);
  16927. AssertIntEQ(test_wc_Sha3_224_Final(), 0);
  16928. AssertIntEQ(test_wc_Sha3_256_Final(), 0);
  16929. AssertIntEQ(test_wc_Sha3_384_Final(), 0);
  16930. AssertIntEQ(test_wc_Sha3_512_Final(), 0);
  16931. AssertIntEQ(test_wc_Sha3_224_Copy(), 0);
  16932. AssertIntEQ(test_wc_Sha3_256_Copy(), 0);
  16933. AssertIntEQ(test_wc_Sha3_384_Copy(), 0);
  16934. AssertIntEQ(test_wc_Sha3_512_Copy(), 0);
  16935. AssertFalse(test_wc_Md5HmacSetKey());
  16936. AssertFalse(test_wc_Md5HmacUpdate());
  16937. AssertFalse(test_wc_Md5HmacFinal());
  16938. AssertFalse(test_wc_ShaHmacSetKey());
  16939. AssertFalse(test_wc_ShaHmacUpdate());
  16940. AssertFalse(test_wc_ShaHmacFinal());
  16941. AssertFalse(test_wc_Sha224HmacSetKey());
  16942. AssertFalse(test_wc_Sha224HmacUpdate());
  16943. AssertFalse(test_wc_Sha224HmacFinal());
  16944. AssertFalse(test_wc_Sha256HmacSetKey());
  16945. AssertFalse(test_wc_Sha256HmacUpdate());
  16946. AssertFalse(test_wc_Sha256HmacFinal());
  16947. AssertFalse(test_wc_Sha384HmacSetKey());
  16948. AssertFalse(test_wc_Sha384HmacUpdate());
  16949. AssertFalse(test_wc_Sha384HmacFinal());
  16950. AssertIntEQ(test_wc_HashInit(), 0);
  16951. AssertIntEQ(test_wc_InitCmac(), 0);
  16952. AssertIntEQ(test_wc_CmacUpdate(), 0);
  16953. AssertIntEQ(test_wc_CmacFinal(), 0);
  16954. AssertIntEQ(test_wc_AesCmacGenerate(), 0);
  16955. AssertIntEQ(test_wc_Des3_SetIV(), 0);
  16956. AssertIntEQ(test_wc_Des3_SetKey(), 0);
  16957. AssertIntEQ(test_wc_Des3_CbcEncryptDecrypt(), 0);
  16958. AssertIntEQ(test_wc_Des3_CbcEncryptDecryptWithKey(), 0);
  16959. AssertIntEQ(test_wc_IdeaSetKey(), 0);
  16960. AssertIntEQ(test_wc_IdeaSetIV(), 0);
  16961. AssertIntEQ(test_wc_IdeaCipher(), 0);
  16962. AssertIntEQ(test_wc_IdeaCbcEncyptDecrypt(), 0);
  16963. AssertIntEQ(test_wc_Chacha_SetKey(), 0);
  16964. AssertIntEQ(test_wc_Chacha_Process(), 0);
  16965. AssertIntEQ(test_wc_ChaCha20Poly1305_aead(), 0);
  16966. AssertIntEQ(test_wc_Poly1305SetKey(), 0);
  16967. AssertIntEQ(test_wc_CamelliaSetKey(), 0);
  16968. AssertIntEQ(test_wc_CamelliaSetIV(), 0);
  16969. AssertIntEQ(test_wc_CamelliaEncryptDecryptDirect(), 0);
  16970. AssertIntEQ(test_wc_CamelliaCbcEncryptDecrypt(), 0);
  16971. AssertIntEQ(test_wc_RabbitSetKey(), 0);
  16972. AssertIntEQ(test_wc_RabbitProcess(), 0);
  16973. AssertIntEQ(test_wc_Arc4SetKey(), 0);
  16974. AssertIntEQ(test_wc_Arc4Process(), 0);
  16975. AssertIntEQ(test_wc_AesSetKey(), 0);
  16976. AssertIntEQ(test_wc_AesSetIV(), 0);
  16977. AssertIntEQ(test_wc_AesCbcEncryptDecrypt(), 0);
  16978. AssertIntEQ(test_wc_AesCtrEncryptDecrypt(), 0);
  16979. AssertIntEQ(test_wc_AesGcmSetKey(), 0);
  16980. AssertIntEQ(test_wc_AesGcmEncryptDecrypt(), 0);
  16981. AssertIntEQ(test_wc_GmacSetKey(), 0);
  16982. AssertIntEQ(test_wc_GmacUpdate(), 0);
  16983. AssertIntEQ(test_wc_InitRsaKey(), 0);
  16984. AssertIntEQ(test_wc_RsaPrivateKeyDecode(), 0);
  16985. AssertIntEQ(test_wc_RsaPublicKeyDecode(), 0);
  16986. AssertIntEQ(test_wc_RsaPublicKeyDecodeRaw(), 0);
  16987. AssertIntEQ(test_wc_MakeRsaKey(), 0);
  16988. AssertIntEQ(test_wc_SetKeyUsage (), 0);
  16989. AssertIntEQ(test_wc_RsaKeyToDer(), 0);
  16990. AssertIntEQ(test_wc_RsaKeyToPublicDer(), 0);
  16991. AssertIntEQ(test_wc_RsaPublicEncryptDecrypt(), 0);
  16992. AssertIntEQ(test_wc_RsaPublicEncryptDecrypt_ex(), 0);
  16993. AssertIntEQ(test_wc_RsaEncryptSize(), 0);
  16994. AssertIntEQ(test_wc_RsaSSL_SignVerify(), 0);
  16995. AssertIntEQ(test_wc_RsaFlattenPublicKey(), 0);
  16996. AssertIntEQ(test_RsaDecryptBoundsCheck(), 0);
  16997. AssertIntEQ(test_wc_AesCcmSetKey(), 0);
  16998. AssertIntEQ(test_wc_AesCcmEncryptDecrypt(), 0);
  16999. AssertIntEQ(test_wc_Hc128_SetKey(), 0);
  17000. AssertIntEQ(test_wc_Hc128_Process(), 0);
  17001. AssertIntEQ(test_wc_InitDsaKey(), 0);
  17002. AssertIntEQ(test_wc_DsaSignVerify(), 0);
  17003. AssertIntEQ(test_wc_DsaPublicPrivateKeyDecode(), 0);
  17004. AssertIntEQ(test_wc_MakeDsaKey(), 0);
  17005. AssertIntEQ(test_wc_DsaKeyToDer(), 0);
  17006. AssertIntEQ(test_wc_DsaImportParamsRaw(), 0);
  17007. AssertIntEQ(test_wc_DsaExportParamsRaw(), 0);
  17008. AssertIntEQ(test_wc_DsaExportKeyRaw(), 0);
  17009. AssertIntEQ(test_wc_SignatureGetSize_ecc(), 0);
  17010. AssertIntEQ(test_wc_SignatureGetSize_rsa(), 0);
  17011. #ifdef OPENSSL_EXTRA
  17012. /*wolfSSS_EVP_get_cipherbynid test*/
  17013. test_wolfSSL_EVP_get_cipherbynid();
  17014. test_wolfSSL_EC();
  17015. #endif
  17016. #ifdef HAVE_HASHDRBG
  17017. #ifdef TEST_RESEED_INTERVAL
  17018. AssertIntEQ(test_wc_RNG_GenerateBlock_Reseed(), 0);
  17019. #endif
  17020. AssertIntEQ(test_wc_RNG_GenerateBlock(), 0);
  17021. #endif
  17022. AssertIntEQ(test_wc_ed25519_make_key(), 0);
  17023. AssertIntEQ(test_wc_ed25519_init(), 0);
  17024. AssertIntEQ(test_wc_ed25519_sign_msg(), 0);
  17025. AssertIntEQ(test_wc_ed25519_import_public(), 0);
  17026. AssertIntEQ(test_wc_ed25519_import_private_key(), 0);
  17027. AssertIntEQ(test_wc_ed25519_export(), 0);
  17028. AssertIntEQ(test_wc_ed25519_size(), 0);
  17029. AssertIntEQ(test_wc_ed25519_exportKey(), 0);
  17030. AssertIntEQ(test_wc_curve25519_init(), 0);
  17031. AssertIntEQ(test_wc_ecc_make_key(), 0);
  17032. AssertIntEQ(test_wc_ecc_init(), 0);
  17033. AssertIntEQ(test_wc_ecc_check_key(), 0);
  17034. AssertIntEQ(test_wc_ecc_size(), 0);
  17035. AssertIntEQ(test_wc_ecc_signVerify_hash(), 0);
  17036. AssertIntEQ(test_wc_ecc_shared_secret(), 0);
  17037. AssertIntEQ(test_wc_ecc_export_x963(), 0);
  17038. AssertIntEQ(test_wc_ecc_export_x963_ex(), 0);
  17039. AssertIntEQ(test_wc_ecc_import_x963(), 0);
  17040. AssertIntEQ(ecc_import_private_key(), 0);
  17041. AssertIntEQ(test_wc_ecc_export_private_only(), 0);
  17042. AssertIntEQ(test_wc_ecc_rs_to_sig(), 0);
  17043. AssertIntEQ(test_wc_ecc_import_raw(), 0);
  17044. AssertIntEQ(test_wc_ecc_sig_size(), 0);
  17045. AssertIntEQ(test_wc_ecc_ctx_new(), 0);
  17046. AssertIntEQ(test_wc_ecc_ctx_reset(), 0);
  17047. AssertIntEQ(test_wc_ecc_ctx_set_peer_salt(), 0);
  17048. AssertIntEQ(test_wc_ecc_ctx_set_info(), 0);
  17049. AssertIntEQ(test_wc_ecc_encryptDecrypt(), 0);
  17050. AssertIntEQ(test_wc_ecc_del_point(), 0);
  17051. AssertIntEQ(test_wc_ecc_pointFns(), 0);
  17052. AssertIntEQ(test_wc_ecc_shared_secret_ssh(), 0);
  17053. AssertIntEQ(test_wc_ecc_verify_hash_ex(), 0);
  17054. AssertIntEQ(test_wc_ecc_mulmod(), 0);
  17055. AssertIntEQ(test_wc_ecc_is_valid_idx(), 0);
  17056. test_wc_PKCS7_Init();
  17057. test_wc_PKCS7_InitWithCert();
  17058. test_wc_PKCS7_EncodeData();
  17059. test_wc_PKCS7_EncodeSignedData();
  17060. test_wc_PKCS7_VerifySignedData();
  17061. test_wc_PKCS7_EncodeDecodeEnvelopedData();
  17062. test_wc_PKCS7_EncodeEncryptedData();
  17063. printf(" End API Tests\n");
  17064. }