1
0

test.c 1.8 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856358573585835859358603586135862358633586435865358663586735868358693587035871358723587335874358753587635877358783587935880358813588235883358843588535886358873588835889358903589135892358933589435895358963589735898358993590035901359023590335904359053590635907359083590935910359113591235913359143591535916359173591835919359203592135922359233592435925359263592735928359293593035931359323593335934359353593635937359383593935940359413594235943359443594535946359473594835949359503595135952359533595435955359563595735958359593596035961359623596335964359653596635967359683596935970359713597235973359743597535976359773597835979359803598135982359833598435985359863598735988359893599035991359923599335994359953599635997359983599936000360013600236003360043600536006360073600836009360103601136012360133601436015360163601736018360193602036021360223602336024360253602636027360283602936030360313603236033360343603536036360373603836039360403604136042360433604436045360463604736048360493605036051360523605336054360553605636057360583605936060360613606236063360643606536066360673606836069360703607136072360733607436075360763607736078360793608036081360823608336084360853608636087360883608936090360913609236093360943609536096360973609836099361003610136102361033610436105361063610736108361093611036111361123611336114361153611636117361183611936120361213612236123361243612536126361273612836129361303613136132361333613436135361363613736138361393614036141361423614336144361453614636147361483614936150361513615236153361543615536156361573615836159361603616136162361633616436165361663616736168361693617036171361723617336174361753617636177361783617936180361813618236183361843618536186361873618836189361903619136192361933619436195361963619736198361993620036201362023620336204362053620636207362083620936210362113621236213362143621536216362173621836219362203622136222362233622436225362263622736228362293623036231362323623336234362353623636237362383623936240362413624236243362443624536246362473624836249362503625136252362533625436255362563625736258362593626036261362623626336264362653626636267362683626936270362713627236273362743627536276362773627836279362803628136282362833628436285362863628736288362893629036291362923629336294362953629636297362983629936300363013630236303363043630536306363073630836309363103631136312363133631436315363163631736318363193632036321363223632336324363253632636327363283632936330363313633236333363343633536336363373633836339363403634136342363433634436345363463634736348363493635036351363523635336354363553635636357363583635936360363613636236363363643636536366363673636836369363703637136372363733637436375363763637736378363793638036381363823638336384363853638636387363883638936390363913639236393363943639536396363973639836399364003640136402364033640436405364063640736408364093641036411364123641336414364153641636417364183641936420364213642236423364243642536426364273642836429364303643136432364333643436435364363643736438364393644036441364423644336444364453644636447364483644936450364513645236453364543645536456364573645836459364603646136462364633646436465364663646736468364693647036471364723647336474364753647636477364783647936480364813648236483364843648536486364873648836489364903649136492364933649436495364963649736498364993650036501365023650336504365053650636507365083650936510365113651236513365143651536516365173651836519365203652136522365233652436525365263652736528365293653036531365323653336534365353653636537365383653936540365413654236543365443654536546365473654836549365503655136552365533655436555365563655736558365593656036561365623656336564365653656636567365683656936570365713657236573365743657536576365773657836579365803658136582365833658436585365863658736588365893659036591365923659336594365953659636597365983659936600366013660236603366043660536606366073660836609366103661136612366133661436615366163661736618366193662036621366223662336624366253662636627366283662936630366313663236633366343663536636366373663836639366403664136642366433664436645366463664736648366493665036651366523665336654366553665636657366583665936660366613666236663366643666536666366673666836669366703667136672366733667436675366763667736678366793668036681366823668336684366853668636687366883668936690366913669236693366943669536696366973669836699367003670136702367033670436705367063670736708367093671036711367123671336714367153671636717367183671936720367213672236723367243672536726367273672836729367303673136732367333673436735367363673736738367393674036741367423674336744367453674636747367483674936750367513675236753367543675536756367573675836759367603676136762367633676436765367663676736768367693677036771367723677336774367753677636777367783677936780367813678236783367843678536786367873678836789367903679136792367933679436795367963679736798367993680036801368023680336804368053680636807368083680936810368113681236813368143681536816368173681836819368203682136822368233682436825368263682736828368293683036831368323683336834368353683636837368383683936840368413684236843368443684536846368473684836849368503685136852368533685436855368563685736858368593686036861368623686336864368653686636867368683686936870368713687236873368743687536876368773687836879368803688136882368833688436885368863688736888368893689036891368923689336894368953689636897368983689936900369013690236903369043690536906369073690836909369103691136912369133691436915369163691736918369193692036921369223692336924369253692636927369283692936930369313693236933369343693536936369373693836939369403694136942369433694436945369463694736948369493695036951369523695336954369553695636957369583695936960369613696236963369643696536966369673696836969369703697136972369733697436975369763697736978369793698036981369823698336984369853698636987369883698936990369913699236993369943699536996369973699836999370003700137002370033700437005370063700737008370093701037011370123701337014370153701637017370183701937020370213702237023370243702537026370273702837029370303703137032370333703437035370363703737038370393704037041370423704337044370453704637047370483704937050370513705237053370543705537056370573705837059370603706137062370633706437065370663706737068370693707037071370723707337074370753707637077370783707937080370813708237083370843708537086370873708837089370903709137092370933709437095370963709737098370993710037101371023710337104371053710637107371083710937110371113711237113371143711537116371173711837119371203712137122371233712437125371263712737128371293713037131371323713337134371353713637137371383713937140371413714237143371443714537146371473714837149371503715137152371533715437155371563715737158371593716037161371623716337164371653716637167371683716937170371713717237173371743717537176371773717837179371803718137182371833718437185371863718737188371893719037191371923719337194371953719637197371983719937200372013720237203372043720537206372073720837209372103721137212372133721437215372163721737218372193722037221372223722337224372253722637227372283722937230372313723237233372343723537236372373723837239372403724137242372433724437245372463724737248372493725037251372523725337254372553725637257372583725937260372613726237263372643726537266372673726837269372703727137272372733727437275372763727737278372793728037281372823728337284372853728637287372883728937290372913729237293372943729537296372973729837299373003730137302373033730437305373063730737308373093731037311373123731337314373153731637317373183731937320373213732237323373243732537326373273732837329373303733137332373333733437335373363733737338373393734037341373423734337344373453734637347373483734937350373513735237353373543735537356373573735837359373603736137362373633736437365373663736737368373693737037371373723737337374373753737637377373783737937380373813738237383373843738537386373873738837389373903739137392373933739437395373963739737398373993740037401374023740337404374053740637407374083740937410374113741237413374143741537416374173741837419374203742137422374233742437425374263742737428374293743037431374323743337434374353743637437374383743937440374413744237443374443744537446374473744837449374503745137452374533745437455374563745737458374593746037461374623746337464374653746637467374683746937470374713747237473374743747537476374773747837479374803748137482374833748437485374863748737488374893749037491374923749337494374953749637497374983749937500375013750237503375043750537506375073750837509375103751137512375133751437515375163751737518375193752037521375223752337524375253752637527375283752937530375313753237533375343753537536375373753837539375403754137542375433754437545375463754737548375493755037551375523755337554375553755637557375583755937560375613756237563375643756537566375673756837569375703757137572375733757437575375763757737578375793758037581375823758337584375853758637587375883758937590375913759237593375943759537596375973759837599376003760137602376033760437605376063760737608376093761037611376123761337614376153761637617376183761937620376213762237623376243762537626376273762837629376303763137632376333763437635376363763737638376393764037641376423764337644376453764637647376483764937650376513765237653376543765537656376573765837659376603766137662376633766437665376663766737668376693767037671376723767337674376753767637677376783767937680376813768237683376843768537686376873768837689376903769137692376933769437695376963769737698376993770037701377023770337704377053770637707377083770937710377113771237713377143771537716377173771837719377203772137722377233772437725377263772737728377293773037731377323773337734377353773637737377383773937740377413774237743377443774537746377473774837749377503775137752377533775437755377563775737758377593776037761377623776337764377653776637767377683776937770377713777237773377743777537776377773777837779377803778137782377833778437785377863778737788377893779037791377923779337794377953779637797377983779937800378013780237803378043780537806378073780837809378103781137812378133781437815378163781737818378193782037821378223782337824378253782637827378283782937830378313783237833378343783537836378373783837839378403784137842378433784437845378463784737848378493785037851378523785337854378553785637857378583785937860378613786237863378643786537866378673786837869378703787137872378733787437875378763787737878378793788037881378823788337884378853788637887378883788937890378913789237893378943789537896378973789837899379003790137902379033790437905379063790737908379093791037911379123791337914379153791637917379183791937920379213792237923379243792537926379273792837929379303793137932379333793437935379363793737938379393794037941379423794337944379453794637947379483794937950379513795237953379543795537956379573795837959379603796137962379633796437965379663796737968379693797037971379723797337974379753797637977379783797937980379813798237983379843798537986379873798837989379903799137992379933799437995379963799737998379993800038001380023800338004380053800638007380083800938010380113801238013380143801538016380173801838019380203802138022380233802438025380263802738028380293803038031380323803338034380353803638037380383803938040380413804238043380443804538046380473804838049380503805138052380533805438055380563805738058380593806038061380623806338064380653806638067380683806938070380713807238073380743807538076380773807838079380803808138082380833808438085380863808738088380893809038091380923809338094380953809638097380983809938100381013810238103381043810538106381073810838109381103811138112381133811438115381163811738118381193812038121381223812338124381253812638127381283812938130381313813238133381343813538136381373813838139381403814138142381433814438145381463814738148381493815038151381523815338154381553815638157381583815938160381613816238163381643816538166381673816838169381703817138172381733817438175381763817738178381793818038181381823818338184381853818638187381883818938190381913819238193381943819538196381973819838199382003820138202382033820438205382063820738208382093821038211382123821338214382153821638217382183821938220382213822238223382243822538226382273822838229382303823138232382333823438235382363823738238382393824038241382423824338244382453824638247382483824938250382513825238253382543825538256382573825838259382603826138262382633826438265382663826738268382693827038271382723827338274382753827638277382783827938280382813828238283382843828538286382873828838289382903829138292382933829438295382963829738298382993830038301383023830338304383053830638307383083830938310383113831238313383143831538316383173831838319383203832138322383233832438325383263832738328383293833038331383323833338334383353833638337383383833938340383413834238343383443834538346383473834838349383503835138352383533835438355383563835738358383593836038361383623836338364383653836638367383683836938370383713837238373383743837538376383773837838379383803838138382383833838438385383863838738388383893839038391383923839338394383953839638397383983839938400384013840238403384043840538406384073840838409384103841138412384133841438415384163841738418384193842038421384223842338424384253842638427384283842938430384313843238433384343843538436384373843838439384403844138442384433844438445384463844738448384493845038451384523845338454384553845638457384583845938460384613846238463384643846538466384673846838469384703847138472384733847438475384763847738478384793848038481384823848338484384853848638487384883848938490384913849238493384943849538496384973849838499385003850138502385033850438505385063850738508385093851038511385123851338514385153851638517385183851938520385213852238523385243852538526385273852838529385303853138532385333853438535385363853738538385393854038541385423854338544385453854638547385483854938550385513855238553385543855538556385573855838559385603856138562385633856438565385663856738568385693857038571385723857338574385753857638577385783857938580385813858238583385843858538586385873858838589385903859138592385933859438595385963859738598385993860038601386023860338604386053860638607386083860938610386113861238613386143861538616386173861838619386203862138622386233862438625386263862738628386293863038631386323863338634386353863638637386383863938640386413864238643386443864538646386473864838649386503865138652386533865438655386563865738658386593866038661386623866338664386653866638667386683866938670386713867238673386743867538676386773867838679386803868138682386833868438685386863868738688386893869038691386923869338694386953869638697386983869938700387013870238703387043870538706387073870838709387103871138712387133871438715387163871738718387193872038721387223872338724387253872638727387283872938730387313873238733387343873538736387373873838739387403874138742387433874438745387463874738748387493875038751387523875338754387553875638757387583875938760387613876238763387643876538766387673876838769387703877138772387733877438775387763877738778387793878038781387823878338784387853878638787387883878938790387913879238793387943879538796387973879838799388003880138802388033880438805388063880738808388093881038811388123881338814388153881638817388183881938820388213882238823388243882538826388273882838829388303883138832388333883438835388363883738838388393884038841388423884338844388453884638847388483884938850388513885238853388543885538856388573885838859388603886138862388633886438865388663886738868388693887038871388723887338874388753887638877388783887938880388813888238883388843888538886388873888838889388903889138892388933889438895388963889738898388993890038901389023890338904389053890638907389083890938910389113891238913389143891538916389173891838919389203892138922389233892438925389263892738928389293893038931389323893338934389353893638937389383893938940389413894238943389443894538946389473894838949389503895138952389533895438955389563895738958389593896038961389623896338964389653896638967389683896938970389713897238973389743897538976389773897838979389803898138982389833898438985389863898738988389893899038991389923899338994389953899638997389983899939000390013900239003390043900539006390073900839009390103901139012390133901439015390163901739018390193902039021390223902339024390253902639027390283902939030390313903239033390343903539036390373903839039390403904139042390433904439045390463904739048390493905039051390523905339054390553905639057390583905939060390613906239063390643906539066390673906839069390703907139072390733907439075390763907739078390793908039081390823908339084390853908639087390883908939090390913909239093390943909539096390973909839099391003910139102391033910439105391063910739108391093911039111391123911339114391153911639117391183911939120391213912239123391243912539126391273912839129391303913139132391333913439135391363913739138391393914039141391423914339144391453914639147391483914939150391513915239153391543915539156391573915839159391603916139162391633916439165391663916739168391693917039171391723917339174391753917639177391783917939180391813918239183391843918539186391873918839189391903919139192391933919439195391963919739198391993920039201392023920339204392053920639207392083920939210392113921239213392143921539216392173921839219392203922139222392233922439225392263922739228392293923039231392323923339234392353923639237392383923939240392413924239243392443924539246392473924839249392503925139252392533925439255392563925739258392593926039261392623926339264392653926639267392683926939270392713927239273392743927539276392773927839279392803928139282392833928439285392863928739288392893929039291392923929339294392953929639297392983929939300393013930239303393043930539306393073930839309393103931139312393133931439315393163931739318393193932039321393223932339324393253932639327393283932939330393313933239333393343933539336393373933839339393403934139342393433934439345393463934739348393493935039351393523935339354393553935639357393583935939360393613936239363393643936539366393673936839369393703937139372393733937439375393763937739378393793938039381393823938339384393853938639387393883938939390393913939239393393943939539396393973939839399394003940139402394033940439405394063940739408394093941039411394123941339414394153941639417394183941939420394213942239423394243942539426394273942839429394303943139432394333943439435394363943739438394393944039441394423944339444394453944639447394483944939450394513945239453394543945539456394573945839459394603946139462394633946439465394663946739468394693947039471394723947339474394753947639477394783947939480394813948239483394843948539486394873948839489394903949139492394933949439495394963949739498394993950039501395023950339504395053950639507395083950939510395113951239513395143951539516395173951839519395203952139522395233952439525395263952739528395293953039531395323953339534395353953639537395383953939540395413954239543395443954539546395473954839549395503955139552395533955439555395563955739558395593956039561395623956339564395653956639567395683956939570395713957239573395743957539576395773957839579395803958139582395833958439585395863958739588395893959039591395923959339594395953959639597395983959939600396013960239603396043960539606396073960839609396103961139612396133961439615396163961739618396193962039621396223962339624396253962639627396283962939630396313963239633396343963539636396373963839639396403964139642396433964439645396463964739648396493965039651396523965339654396553965639657396583965939660396613966239663396643966539666396673966839669396703967139672396733967439675396763967739678396793968039681396823968339684396853968639687396883968939690396913969239693396943969539696396973969839699397003970139702397033970439705397063970739708397093971039711397123971339714397153971639717397183971939720397213972239723397243972539726397273972839729397303973139732397333973439735397363973739738397393974039741397423974339744397453974639747397483974939750397513975239753397543975539756397573975839759397603976139762397633976439765397663976739768397693977039771397723977339774397753977639777397783977939780397813978239783397843978539786397873978839789397903979139792397933979439795397963979739798397993980039801398023980339804398053980639807398083980939810398113981239813398143981539816398173981839819398203982139822398233982439825398263982739828398293983039831398323983339834398353983639837398383983939840398413984239843398443984539846398473984839849398503985139852398533985439855398563985739858398593986039861398623986339864398653986639867398683986939870398713987239873398743987539876398773987839879398803988139882398833988439885398863988739888398893989039891398923989339894398953989639897398983989939900399013990239903399043990539906399073990839909399103991139912399133991439915399163991739918399193992039921399223992339924399253992639927399283992939930399313993239933399343993539936399373993839939399403994139942399433994439945399463994739948399493995039951399523995339954399553995639957399583995939960399613996239963399643996539966399673996839969399703997139972399733997439975399763997739978399793998039981399823998339984399853998639987399883998939990399913999239993399943999539996399973999839999400004000140002400034000440005400064000740008400094001040011400124001340014400154001640017400184001940020400214002240023400244002540026400274002840029400304003140032400334003440035400364003740038400394004040041400424004340044400454004640047400484004940050400514005240053400544005540056400574005840059400604006140062400634006440065400664006740068400694007040071400724007340074400754007640077400784007940080400814008240083400844008540086400874008840089400904009140092400934009440095400964009740098400994010040101401024010340104401054010640107401084010940110401114011240113401144011540116401174011840119401204012140122401234012440125401264012740128401294013040131401324013340134401354013640137401384013940140401414014240143401444014540146401474014840149401504015140152401534015440155401564015740158401594016040161401624016340164401654016640167401684016940170401714017240173401744017540176401774017840179401804018140182401834018440185401864018740188401894019040191401924019340194401954019640197401984019940200402014020240203402044020540206402074020840209402104021140212402134021440215402164021740218402194022040221402224022340224402254022640227402284022940230402314023240233402344023540236402374023840239402404024140242402434024440245402464024740248402494025040251402524025340254402554025640257402584025940260402614026240263402644026540266402674026840269402704027140272402734027440275402764027740278402794028040281402824028340284402854028640287402884028940290402914029240293402944029540296402974029840299403004030140302403034030440305403064030740308403094031040311403124031340314403154031640317403184031940320403214032240323403244032540326403274032840329403304033140332403334033440335403364033740338403394034040341403424034340344403454034640347403484034940350403514035240353403544035540356403574035840359403604036140362403634036440365403664036740368403694037040371403724037340374403754037640377403784037940380403814038240383403844038540386403874038840389403904039140392403934039440395403964039740398403994040040401404024040340404404054040640407404084040940410404114041240413404144041540416404174041840419404204042140422404234042440425404264042740428404294043040431404324043340434404354043640437404384043940440404414044240443404444044540446404474044840449404504045140452404534045440455404564045740458404594046040461404624046340464404654046640467404684046940470404714047240473404744047540476404774047840479404804048140482404834048440485404864048740488404894049040491404924049340494404954049640497404984049940500405014050240503405044050540506405074050840509405104051140512405134051440515405164051740518405194052040521405224052340524405254052640527405284052940530405314053240533405344053540536405374053840539405404054140542405434054440545405464054740548405494055040551405524055340554405554055640557405584055940560405614056240563405644056540566405674056840569405704057140572405734057440575405764057740578405794058040581405824058340584405854058640587405884058940590405914059240593405944059540596405974059840599406004060140602406034060440605406064060740608406094061040611406124061340614406154061640617406184061940620406214062240623406244062540626406274062840629406304063140632406334063440635406364063740638406394064040641406424064340644406454064640647406484064940650406514065240653406544065540656406574065840659406604066140662406634066440665406664066740668406694067040671406724067340674406754067640677406784067940680406814068240683406844068540686406874068840689406904069140692406934069440695406964069740698406994070040701407024070340704407054070640707407084070940710407114071240713407144071540716407174071840719407204072140722407234072440725407264072740728407294073040731407324073340734407354073640737407384073940740407414074240743407444074540746407474074840749407504075140752407534075440755407564075740758407594076040761407624076340764407654076640767407684076940770407714077240773407744077540776407774077840779407804078140782407834078440785407864078740788407894079040791407924079340794407954079640797407984079940800408014080240803408044080540806408074080840809408104081140812408134081440815408164081740818408194082040821408224082340824408254082640827408284082940830408314083240833408344083540836408374083840839408404084140842408434084440845408464084740848408494085040851408524085340854408554085640857408584085940860408614086240863408644086540866408674086840869408704087140872408734087440875408764087740878408794088040881408824088340884408854088640887408884088940890408914089240893408944089540896408974089840899409004090140902409034090440905409064090740908409094091040911409124091340914409154091640917409184091940920409214092240923409244092540926409274092840929409304093140932409334093440935409364093740938409394094040941409424094340944409454094640947409484094940950409514095240953409544095540956409574095840959409604096140962409634096440965409664096740968409694097040971409724097340974409754097640977409784097940980409814098240983409844098540986409874098840989409904099140992409934099440995409964099740998409994100041001410024100341004410054100641007410084100941010410114101241013410144101541016410174101841019410204102141022410234102441025410264102741028410294103041031410324103341034410354103641037410384103941040410414104241043410444104541046410474104841049410504105141052410534105441055410564105741058410594106041061410624106341064410654106641067410684106941070410714107241073410744107541076410774107841079410804108141082410834108441085410864108741088410894109041091410924109341094410954109641097410984109941100411014110241103411044110541106411074110841109411104111141112411134111441115411164111741118411194112041121411224112341124411254112641127411284112941130411314113241133411344113541136411374113841139411404114141142411434114441145411464114741148411494115041151411524115341154411554115641157411584115941160411614116241163411644116541166411674116841169411704117141172411734117441175411764117741178411794118041181411824118341184411854118641187411884118941190411914119241193411944119541196411974119841199412004120141202412034120441205412064120741208412094121041211412124121341214412154121641217412184121941220412214122241223412244122541226412274122841229412304123141232412334123441235412364123741238412394124041241412424124341244412454124641247412484124941250412514125241253412544125541256412574125841259412604126141262412634126441265412664126741268412694127041271412724127341274412754127641277412784127941280412814128241283412844128541286412874128841289412904129141292412934129441295412964129741298412994130041301413024130341304413054130641307413084130941310413114131241313413144131541316413174131841319413204132141322413234132441325413264132741328413294133041331413324133341334413354133641337413384133941340413414134241343413444134541346413474134841349413504135141352413534135441355413564135741358413594136041361413624136341364413654136641367413684136941370413714137241373413744137541376413774137841379413804138141382413834138441385413864138741388413894139041391413924139341394413954139641397413984139941400414014140241403414044140541406414074140841409414104141141412414134141441415414164141741418414194142041421414224142341424414254142641427414284142941430414314143241433414344143541436414374143841439414404144141442414434144441445414464144741448414494145041451414524145341454414554145641457414584145941460414614146241463414644146541466414674146841469414704147141472414734147441475414764147741478414794148041481414824148341484414854148641487414884148941490414914149241493414944149541496414974149841499415004150141502415034150441505415064150741508415094151041511415124151341514415154151641517415184151941520415214152241523415244152541526415274152841529415304153141532415334153441535415364153741538415394154041541415424154341544415454154641547415484154941550415514155241553415544155541556415574155841559415604156141562415634156441565415664156741568415694157041571415724157341574415754157641577415784157941580415814158241583415844158541586415874158841589415904159141592415934159441595415964159741598415994160041601416024160341604416054160641607416084160941610416114161241613416144161541616416174161841619416204162141622416234162441625416264162741628416294163041631416324163341634416354163641637416384163941640416414164241643416444164541646416474164841649416504165141652416534165441655416564165741658416594166041661416624166341664416654166641667416684166941670416714167241673416744167541676416774167841679416804168141682416834168441685416864168741688416894169041691416924169341694416954169641697416984169941700417014170241703417044170541706417074170841709417104171141712417134171441715417164171741718417194172041721417224172341724417254172641727417284172941730417314173241733417344173541736417374173841739417404174141742417434174441745417464174741748417494175041751417524175341754417554175641757417584175941760417614176241763417644176541766417674176841769417704177141772417734177441775417764177741778417794178041781417824178341784417854178641787417884178941790417914179241793417944179541796417974179841799418004180141802418034180441805418064180741808418094181041811418124181341814418154181641817418184181941820418214182241823418244182541826418274182841829418304183141832418334183441835418364183741838418394184041841418424184341844418454184641847418484184941850418514185241853418544185541856418574185841859418604186141862418634186441865418664186741868418694187041871418724187341874418754187641877418784187941880418814188241883418844188541886418874188841889418904189141892418934189441895418964189741898418994190041901419024190341904419054190641907419084190941910419114191241913419144191541916419174191841919419204192141922419234192441925419264192741928419294193041931419324193341934419354193641937419384193941940419414194241943419444194541946419474194841949419504195141952419534195441955419564195741958419594196041961419624196341964419654196641967419684196941970419714197241973419744197541976419774197841979419804198141982419834198441985419864198741988419894199041991419924199341994419954199641997419984199942000420014200242003420044200542006420074200842009420104201142012420134201442015420164201742018420194202042021420224202342024420254202642027420284202942030420314203242033420344203542036420374203842039420404204142042420434204442045420464204742048420494205042051420524205342054420554205642057420584205942060420614206242063420644206542066420674206842069420704207142072420734207442075420764207742078420794208042081420824208342084420854208642087420884208942090420914209242093420944209542096420974209842099421004210142102421034210442105421064210742108421094211042111421124211342114421154211642117421184211942120421214212242123421244212542126421274212842129421304213142132421334213442135421364213742138421394214042141421424214342144421454214642147421484214942150421514215242153421544215542156421574215842159421604216142162421634216442165421664216742168421694217042171421724217342174421754217642177421784217942180421814218242183421844218542186421874218842189421904219142192421934219442195421964219742198421994220042201422024220342204422054220642207422084220942210422114221242213422144221542216422174221842219422204222142222422234222442225422264222742228422294223042231422324223342234422354223642237422384223942240422414224242243422444224542246422474224842249422504225142252422534225442255422564225742258422594226042261422624226342264422654226642267422684226942270422714227242273422744227542276422774227842279422804228142282422834228442285422864228742288422894229042291422924229342294422954229642297422984229942300423014230242303423044230542306423074230842309423104231142312423134231442315423164231742318423194232042321423224232342324423254232642327423284232942330423314233242333423344233542336423374233842339423404234142342423434234442345423464234742348423494235042351423524235342354423554235642357423584235942360423614236242363423644236542366423674236842369423704237142372423734237442375423764237742378423794238042381423824238342384423854238642387423884238942390423914239242393423944239542396423974239842399424004240142402424034240442405424064240742408424094241042411424124241342414424154241642417424184241942420424214242242423424244242542426424274242842429424304243142432424334243442435424364243742438424394244042441424424244342444424454244642447424484244942450424514245242453424544245542456424574245842459424604246142462424634246442465424664246742468424694247042471424724247342474424754247642477424784247942480424814248242483424844248542486424874248842489424904249142492424934249442495424964249742498424994250042501425024250342504425054250642507425084250942510425114251242513425144251542516425174251842519425204252142522425234252442525425264252742528425294253042531425324253342534425354253642537425384253942540425414254242543425444254542546425474254842549425504255142552425534255442555425564255742558425594256042561425624256342564425654256642567425684256942570425714257242573425744257542576425774257842579425804258142582425834258442585425864258742588425894259042591425924259342594425954259642597425984259942600426014260242603426044260542606426074260842609426104261142612426134261442615426164261742618426194262042621426224262342624426254262642627426284262942630426314263242633426344263542636426374263842639426404264142642426434264442645426464264742648426494265042651426524265342654426554265642657426584265942660426614266242663426644266542666426674266842669426704267142672426734267442675426764267742678426794268042681426824268342684426854268642687426884268942690426914269242693426944269542696426974269842699427004270142702427034270442705427064270742708427094271042711427124271342714427154271642717427184271942720427214272242723427244272542726427274272842729427304273142732427334273442735427364273742738427394274042741427424274342744427454274642747427484274942750427514275242753427544275542756427574275842759427604276142762427634276442765427664276742768427694277042771427724277342774427754277642777427784277942780427814278242783427844278542786427874278842789427904279142792427934279442795427964279742798427994280042801428024280342804428054280642807428084280942810428114281242813428144281542816428174281842819428204282142822428234282442825428264282742828428294283042831428324283342834428354283642837428384283942840428414284242843428444284542846428474284842849428504285142852428534285442855428564285742858428594286042861428624286342864428654286642867428684286942870428714287242873428744287542876428774287842879428804288142882428834288442885428864288742888428894289042891428924289342894428954289642897428984289942900429014290242903429044290542906429074290842909429104291142912429134291442915429164291742918429194292042921429224292342924429254292642927429284292942930429314293242933429344293542936429374293842939429404294142942429434294442945429464294742948429494295042951429524295342954429554295642957429584295942960429614296242963429644296542966429674296842969429704297142972429734297442975429764297742978429794298042981429824298342984429854298642987429884298942990429914299242993429944299542996429974299842999430004300143002430034300443005430064300743008430094301043011430124301343014430154301643017430184301943020430214302243023430244302543026430274302843029430304303143032430334303443035430364303743038430394304043041430424304343044430454304643047430484304943050430514305243053430544305543056430574305843059430604306143062430634306443065430664306743068430694307043071430724307343074430754307643077430784307943080430814308243083430844308543086430874308843089430904309143092430934309443095430964309743098430994310043101431024310343104431054310643107431084310943110431114311243113431144311543116431174311843119431204312143122431234312443125431264312743128431294313043131431324313343134431354313643137431384313943140431414314243143431444314543146431474314843149431504315143152431534315443155431564315743158431594316043161431624316343164431654316643167431684316943170431714317243173431744317543176431774317843179431804318143182431834318443185431864318743188431894319043191431924319343194431954319643197431984319943200432014320243203432044320543206432074320843209432104321143212432134321443215432164321743218432194322043221432224322343224432254322643227432284322943230432314323243233432344323543236432374323843239432404324143242432434324443245432464324743248432494325043251432524325343254432554325643257432584325943260432614326243263432644326543266432674326843269432704327143272432734327443275432764327743278432794328043281432824328343284432854328643287432884328943290432914329243293432944329543296432974329843299433004330143302433034330443305433064330743308433094331043311433124331343314433154331643317433184331943320433214332243323433244332543326433274332843329433304333143332433334333443335433364333743338433394334043341433424334343344433454334643347433484334943350433514335243353433544335543356433574335843359433604336143362433634336443365433664336743368433694337043371433724337343374433754337643377433784337943380433814338243383433844338543386433874338843389433904339143392433934339443395433964339743398433994340043401434024340343404434054340643407434084340943410434114341243413434144341543416434174341843419434204342143422434234342443425434264342743428434294343043431434324343343434434354343643437434384343943440434414344243443434444344543446434474344843449434504345143452434534345443455434564345743458434594346043461434624346343464434654346643467434684346943470434714347243473434744347543476434774347843479434804348143482434834348443485434864348743488434894349043491434924349343494434954349643497434984349943500435014350243503435044350543506435074350843509435104351143512435134351443515435164351743518435194352043521435224352343524435254352643527435284352943530435314353243533435344353543536435374353843539435404354143542435434354443545435464354743548435494355043551435524355343554435554355643557435584355943560435614356243563435644356543566435674356843569435704357143572435734357443575435764357743578435794358043581435824358343584435854358643587435884358943590435914359243593435944359543596435974359843599436004360143602436034360443605436064360743608436094361043611436124361343614436154361643617436184361943620436214362243623436244362543626436274362843629436304363143632436334363443635436364363743638436394364043641436424364343644436454364643647436484364943650436514365243653436544365543656436574365843659436604366143662436634366443665436664366743668436694367043671436724367343674436754367643677436784367943680436814368243683436844368543686436874368843689436904369143692436934369443695436964369743698436994370043701437024370343704437054370643707437084370943710437114371243713437144371543716437174371843719437204372143722437234372443725437264372743728437294373043731437324373343734437354373643737437384373943740437414374243743437444374543746437474374843749437504375143752437534375443755437564375743758437594376043761437624376343764437654376643767437684376943770437714377243773437744377543776437774377843779437804378143782437834378443785437864378743788437894379043791437924379343794437954379643797437984379943800438014380243803438044380543806438074380843809438104381143812438134381443815438164381743818438194382043821438224382343824438254382643827438284382943830438314383243833438344383543836438374383843839438404384143842438434384443845438464384743848438494385043851438524385343854438554385643857438584385943860438614386243863438644386543866438674386843869438704387143872438734387443875438764387743878438794388043881438824388343884438854388643887438884388943890438914389243893438944389543896438974389843899439004390143902439034390443905439064390743908439094391043911439124391343914439154391643917439184391943920439214392243923439244392543926439274392843929439304393143932439334393443935439364393743938439394394043941439424394343944439454394643947439484394943950439514395243953439544395543956439574395843959439604396143962439634396443965439664396743968439694397043971439724397343974439754397643977439784397943980439814398243983439844398543986439874398843989439904399143992439934399443995439964399743998439994400044001440024400344004440054400644007440084400944010440114401244013440144401544016440174401844019440204402144022440234402444025440264402744028440294403044031440324403344034440354403644037440384403944040440414404244043440444404544046440474404844049440504405144052440534405444055440564405744058440594406044061440624406344064440654406644067440684406944070440714407244073440744407544076440774407844079440804408144082440834408444085440864408744088440894409044091440924409344094440954409644097440984409944100441014410244103441044410544106441074410844109441104411144112441134411444115441164411744118441194412044121441224412344124441254412644127441284412944130441314413244133441344413544136441374413844139441404414144142441434414444145441464414744148441494415044151441524415344154441554415644157441584415944160441614416244163441644416544166441674416844169441704417144172441734417444175441764417744178441794418044181441824418344184441854418644187441884418944190441914419244193441944419544196441974419844199442004420144202442034420444205442064420744208442094421044211442124421344214442154421644217442184421944220442214422244223442244422544226442274422844229442304423144232442334423444235442364423744238442394424044241442424424344244442454424644247442484424944250442514425244253442544425544256442574425844259442604426144262442634426444265442664426744268442694427044271442724427344274442754427644277442784427944280442814428244283442844428544286442874428844289442904429144292442934429444295442964429744298442994430044301443024430344304443054430644307443084430944310443114431244313443144431544316443174431844319443204432144322443234432444325443264432744328443294433044331443324433344334443354433644337443384433944340443414434244343443444434544346443474434844349443504435144352443534435444355443564435744358443594436044361443624436344364443654436644367443684436944370443714437244373443744437544376443774437844379443804438144382443834438444385443864438744388443894439044391443924439344394443954439644397443984439944400444014440244403444044440544406444074440844409444104441144412444134441444415444164441744418444194442044421444224442344424444254442644427444284442944430444314443244433444344443544436444374443844439444404444144442444434444444445444464444744448444494445044451444524445344454444554445644457444584445944460444614446244463444644446544466444674446844469444704447144472444734447444475444764447744478444794448044481444824448344484444854448644487444884448944490444914449244493444944449544496444974449844499445004450144502445034450444505445064450744508445094451044511445124451344514445154451644517445184451944520445214452244523445244452544526445274452844529445304453144532445334453444535445364453744538445394454044541445424454344544445454454644547445484454944550445514455244553445544455544556445574455844559445604456144562445634456444565445664456744568445694457044571445724457344574445754457644577445784457944580445814458244583445844458544586445874458844589445904459144592445934459444595445964459744598445994460044601446024460344604446054460644607446084460944610446114461244613446144461544616446174461844619446204462144622446234462444625446264462744628446294463044631446324463344634446354463644637446384463944640446414464244643446444464544646446474464844649446504465144652446534465444655446564465744658446594466044661446624466344664446654466644667446684466944670446714467244673446744467544676446774467844679446804468144682446834468444685446864468744688446894469044691446924469344694446954469644697446984469944700447014470244703447044470544706447074470844709447104471144712447134471444715447164471744718447194472044721447224472344724447254472644727447284472944730447314473244733447344473544736447374473844739447404474144742447434474444745447464474744748447494475044751447524475344754447554475644757447584475944760447614476244763447644476544766447674476844769447704477144772447734477444775447764477744778447794478044781447824478344784447854478644787447884478944790447914479244793447944479544796447974479844799448004480144802448034480444805448064480744808448094481044811448124481344814448154481644817448184481944820448214482244823448244482544826448274482844829448304483144832448334483444835448364483744838448394484044841448424484344844448454484644847448484484944850448514485244853448544485544856448574485844859448604486144862448634486444865448664486744868448694487044871448724487344874448754487644877448784487944880448814488244883448844488544886448874488844889448904489144892448934489444895448964489744898448994490044901449024490344904449054490644907449084490944910449114491244913449144491544916449174491844919449204492144922449234492444925449264492744928449294493044931449324493344934449354493644937449384493944940449414494244943449444494544946449474494844949449504495144952449534495444955449564495744958449594496044961449624496344964449654496644967449684496944970449714497244973449744497544976449774497844979449804498144982449834498444985449864498744988449894499044991449924499344994449954499644997449984499945000450014500245003450044500545006450074500845009450104501145012450134501445015450164501745018450194502045021450224502345024450254502645027450284502945030450314503245033450344503545036450374503845039450404504145042450434504445045450464504745048450494505045051450524505345054450554505645057450584505945060450614506245063450644506545066450674506845069450704507145072450734507445075450764507745078450794508045081450824508345084450854508645087450884508945090450914509245093450944509545096450974509845099451004510145102451034510445105451064510745108451094511045111451124511345114451154511645117451184511945120451214512245123451244512545126451274512845129451304513145132451334513445135451364513745138451394514045141451424514345144451454514645147451484514945150451514515245153451544515545156451574515845159451604516145162451634516445165451664516745168451694517045171451724517345174451754517645177451784517945180451814518245183451844518545186451874518845189451904519145192451934519445195451964519745198451994520045201452024520345204452054520645207452084520945210452114521245213452144521545216452174521845219452204522145222452234522445225452264522745228452294523045231452324523345234452354523645237452384523945240452414524245243452444524545246452474524845249452504525145252452534525445255452564525745258452594526045261452624526345264452654526645267452684526945270452714527245273452744527545276452774527845279452804528145282452834528445285452864528745288452894529045291452924529345294452954529645297452984529945300453014530245303453044530545306453074530845309453104531145312453134531445315453164531745318453194532045321453224532345324453254532645327453284532945330453314533245333453344533545336453374533845339453404534145342453434534445345453464534745348453494535045351453524535345354453554535645357453584535945360453614536245363453644536545366453674536845369453704537145372453734537445375453764537745378453794538045381453824538345384453854538645387453884538945390453914539245393453944539545396453974539845399454004540145402454034540445405454064540745408454094541045411454124541345414454154541645417454184541945420454214542245423454244542545426454274542845429454304543145432454334543445435454364543745438454394544045441454424544345444454454544645447454484544945450454514545245453454544545545456454574545845459454604546145462454634546445465454664546745468454694547045471454724547345474454754547645477454784547945480454814548245483454844548545486454874548845489454904549145492454934549445495454964549745498454994550045501455024550345504455054550645507455084550945510455114551245513455144551545516455174551845519455204552145522455234552445525455264552745528455294553045531455324553345534455354553645537455384553945540455414554245543455444554545546455474554845549455504555145552455534555445555455564555745558455594556045561455624556345564455654556645567455684556945570455714557245573455744557545576455774557845579455804558145582455834558445585455864558745588455894559045591455924559345594455954559645597455984559945600456014560245603456044560545606456074560845609456104561145612456134561445615456164561745618456194562045621456224562345624456254562645627456284562945630456314563245633456344563545636456374563845639456404564145642456434564445645456464564745648456494565045651456524565345654456554565645657456584565945660456614566245663456644566545666456674566845669456704567145672456734567445675456764567745678456794568045681456824568345684456854568645687456884568945690456914569245693456944569545696456974569845699457004570145702457034570445705457064570745708457094571045711457124571345714457154571645717457184571945720457214572245723457244572545726457274572845729457304573145732457334573445735457364573745738457394574045741457424574345744457454574645747457484574945750457514575245753457544575545756457574575845759457604576145762457634576445765457664576745768457694577045771457724577345774457754577645777457784577945780457814578245783457844578545786457874578845789457904579145792457934579445795457964579745798457994580045801458024580345804458054580645807458084580945810458114581245813458144581545816458174581845819458204582145822458234582445825458264582745828458294583045831458324583345834458354583645837458384583945840458414584245843458444584545846458474584845849458504585145852458534585445855458564585745858458594586045861458624586345864458654586645867458684586945870458714587245873458744587545876458774587845879458804588145882458834588445885458864588745888458894589045891458924589345894458954589645897458984589945900459014590245903459044590545906459074590845909459104591145912459134591445915459164591745918459194592045921459224592345924459254592645927459284592945930459314593245933459344593545936459374593845939459404594145942459434594445945459464594745948459494595045951459524595345954459554595645957459584595945960459614596245963459644596545966459674596845969459704597145972459734597445975459764597745978459794598045981459824598345984459854598645987459884598945990459914599245993459944599545996459974599845999460004600146002460034600446005460064600746008460094601046011460124601346014460154601646017460184601946020460214602246023460244602546026460274602846029460304603146032460334603446035460364603746038460394604046041460424604346044460454604646047460484604946050460514605246053460544605546056460574605846059460604606146062460634606446065460664606746068460694607046071460724607346074460754607646077460784607946080460814608246083460844608546086460874608846089460904609146092460934609446095460964609746098460994610046101461024610346104461054610646107461084610946110461114611246113461144611546116461174611846119461204612146122461234612446125461264612746128461294613046131461324613346134461354613646137461384613946140461414614246143461444614546146461474614846149461504615146152461534615446155461564615746158461594616046161461624616346164461654616646167461684616946170461714617246173461744617546176461774617846179461804618146182461834618446185461864618746188461894619046191461924619346194461954619646197461984619946200462014620246203462044620546206462074620846209462104621146212462134621446215462164621746218462194622046221462224622346224462254622646227462284622946230462314623246233462344623546236462374623846239462404624146242462434624446245462464624746248462494625046251462524625346254462554625646257462584625946260462614626246263462644626546266462674626846269462704627146272462734627446275462764627746278462794628046281462824628346284462854628646287462884628946290462914629246293462944629546296462974629846299463004630146302463034630446305463064630746308463094631046311463124631346314463154631646317463184631946320463214632246323463244632546326463274632846329463304633146332463334633446335463364633746338463394634046341463424634346344463454634646347463484634946350463514635246353463544635546356463574635846359463604636146362463634636446365463664636746368463694637046371463724637346374463754637646377463784637946380463814638246383463844638546386463874638846389463904639146392463934639446395463964639746398463994640046401464024640346404464054640646407464084640946410464114641246413464144641546416464174641846419464204642146422464234642446425464264642746428464294643046431464324643346434464354643646437464384643946440464414644246443464444644546446464474644846449464504645146452464534645446455464564645746458464594646046461464624646346464464654646646467464684646946470464714647246473464744647546476464774647846479464804648146482464834648446485464864648746488464894649046491464924649346494464954649646497464984649946500465014650246503465044650546506465074650846509465104651146512465134651446515465164651746518465194652046521465224652346524465254652646527465284652946530465314653246533465344653546536465374653846539465404654146542465434654446545465464654746548465494655046551465524655346554465554655646557465584655946560465614656246563465644656546566465674656846569465704657146572465734657446575465764657746578465794658046581465824658346584465854658646587465884658946590465914659246593465944659546596465974659846599466004660146602466034660446605466064660746608466094661046611466124661346614466154661646617466184661946620466214662246623466244662546626466274662846629466304663146632466334663446635466364663746638466394664046641466424664346644466454664646647466484664946650466514665246653466544665546656466574665846659466604666146662466634666446665466664666746668466694667046671466724667346674466754667646677466784667946680466814668246683466844668546686466874668846689466904669146692466934669446695466964669746698466994670046701467024670346704467054670646707467084670946710467114671246713467144671546716467174671846719467204672146722467234672446725467264672746728467294673046731467324673346734467354673646737467384673946740467414674246743467444674546746467474674846749467504675146752467534675446755467564675746758467594676046761467624676346764467654676646767467684676946770467714677246773467744677546776467774677846779467804678146782467834678446785467864678746788467894679046791467924679346794467954679646797467984679946800468014680246803468044680546806468074680846809468104681146812468134681446815468164681746818468194682046821468224682346824468254682646827468284682946830468314683246833468344683546836468374683846839468404684146842468434684446845468464684746848468494685046851468524685346854468554685646857468584685946860468614686246863468644686546866468674686846869468704687146872468734687446875468764687746878468794688046881468824688346884468854688646887468884688946890468914689246893468944689546896468974689846899469004690146902469034690446905469064690746908469094691046911469124691346914469154691646917469184691946920469214692246923469244692546926469274692846929469304693146932469334693446935469364693746938469394694046941469424694346944469454694646947469484694946950469514695246953469544695546956469574695846959469604696146962469634696446965469664696746968469694697046971469724697346974469754697646977469784697946980469814698246983469844698546986469874698846989469904699146992469934699446995469964699746998469994700047001470024700347004470054700647007470084700947010470114701247013470144701547016470174701847019470204702147022470234702447025470264702747028470294703047031470324703347034470354703647037470384703947040470414704247043470444704547046470474704847049470504705147052470534705447055470564705747058470594706047061470624706347064470654706647067470684706947070470714707247073470744707547076470774707847079470804708147082470834708447085470864708747088470894709047091470924709347094470954709647097470984709947100471014710247103471044710547106471074710847109471104711147112471134711447115471164711747118471194712047121471224712347124471254712647127471284712947130471314713247133471344713547136471374713847139471404714147142471434714447145471464714747148471494715047151471524715347154471554715647157471584715947160471614716247163471644716547166471674716847169471704717147172471734717447175471764717747178471794718047181471824718347184471854718647187471884718947190471914719247193471944719547196471974719847199472004720147202472034720447205472064720747208472094721047211472124721347214472154721647217472184721947220472214722247223472244722547226472274722847229472304723147232472334723447235472364723747238472394724047241472424724347244472454724647247472484724947250472514725247253472544725547256472574725847259472604726147262472634726447265472664726747268472694727047271472724727347274472754727647277472784727947280472814728247283472844728547286472874728847289472904729147292472934729447295472964729747298472994730047301473024730347304473054730647307473084730947310473114731247313473144731547316473174731847319473204732147322473234732447325473264732747328473294733047331473324733347334473354733647337473384733947340473414734247343473444734547346473474734847349473504735147352473534735447355473564735747358473594736047361473624736347364473654736647367473684736947370473714737247373473744737547376473774737847379473804738147382473834738447385473864738747388473894739047391473924739347394473954739647397473984739947400474014740247403474044740547406474074740847409474104741147412474134741447415474164741747418474194742047421474224742347424474254742647427474284742947430474314743247433474344743547436474374743847439474404744147442474434744447445474464744747448474494745047451474524745347454474554745647457474584745947460474614746247463474644746547466474674746847469474704747147472474734747447475474764747747478474794748047481474824748347484474854748647487474884748947490474914749247493474944749547496474974749847499475004750147502475034750447505475064750747508475094751047511475124751347514475154751647517475184751947520475214752247523475244752547526475274752847529475304753147532475334753447535475364753747538475394754047541475424754347544475454754647547475484754947550475514755247553475544755547556475574755847559475604756147562475634756447565475664756747568475694757047571475724757347574475754757647577475784757947580475814758247583475844758547586475874758847589475904759147592475934759447595475964759747598475994760047601476024760347604476054760647607476084760947610476114761247613476144761547616476174761847619476204762147622476234762447625476264762747628476294763047631476324763347634476354763647637476384763947640476414764247643476444764547646476474764847649476504765147652476534765447655476564765747658476594766047661476624766347664476654766647667476684766947670476714767247673476744767547676476774767847679476804768147682476834768447685476864768747688476894769047691476924769347694476954769647697476984769947700477014770247703477044770547706477074770847709477104771147712477134771447715477164771747718477194772047721477224772347724477254772647727477284772947730477314773247733477344773547736477374773847739477404774147742477434774447745477464774747748477494775047751477524775347754477554775647757477584775947760477614776247763477644776547766477674776847769477704777147772477734777447775477764777747778477794778047781477824778347784477854778647787477884778947790477914779247793477944779547796477974779847799478004780147802478034780447805478064780747808478094781047811478124781347814478154781647817478184781947820478214782247823478244782547826478274782847829478304783147832478334783447835478364783747838478394784047841478424784347844478454784647847478484784947850478514785247853478544785547856478574785847859478604786147862478634786447865478664786747868478694787047871478724787347874478754787647877478784787947880478814788247883478844788547886478874788847889478904789147892478934789447895478964789747898478994790047901479024790347904479054790647907479084790947910479114791247913479144791547916479174791847919479204792147922479234792447925479264792747928479294793047931479324793347934479354793647937479384793947940479414794247943479444794547946479474794847949479504795147952479534795447955479564795747958479594796047961479624796347964479654796647967479684796947970479714797247973479744797547976479774797847979479804798147982479834798447985479864798747988479894799047991479924799347994479954799647997479984799948000480014800248003480044800548006480074800848009480104801148012480134801448015480164801748018480194802048021480224802348024480254802648027480284802948030480314803248033480344803548036480374803848039480404804148042480434804448045480464804748048480494805048051480524805348054480554805648057480584805948060480614806248063480644806548066480674806848069480704807148072480734807448075480764807748078480794808048081480824808348084480854808648087480884808948090480914809248093480944809548096480974809848099481004810148102481034810448105481064810748108481094811048111481124811348114481154811648117481184811948120481214812248123481244812548126481274812848129481304813148132481334813448135481364813748138481394814048141481424814348144481454814648147481484814948150481514815248153481544815548156481574815848159481604816148162481634816448165481664816748168481694817048171481724817348174481754817648177481784817948180481814818248183481844818548186481874818848189481904819148192481934819448195481964819748198481994820048201482024820348204482054820648207482084820948210482114821248213482144821548216482174821848219482204822148222482234822448225482264822748228482294823048231482324823348234482354823648237482384823948240482414824248243482444824548246482474824848249482504825148252482534825448255482564825748258482594826048261482624826348264482654826648267482684826948270482714827248273482744827548276482774827848279482804828148282482834828448285482864828748288482894829048291482924829348294482954829648297482984829948300483014830248303483044830548306483074830848309483104831148312483134831448315483164831748318483194832048321483224832348324483254832648327483284832948330483314833248333483344833548336483374833848339483404834148342483434834448345483464834748348483494835048351483524835348354483554835648357483584835948360483614836248363483644836548366483674836848369483704837148372483734837448375483764837748378483794838048381483824838348384483854838648387483884838948390483914839248393483944839548396483974839848399484004840148402484034840448405484064840748408484094841048411484124841348414484154841648417484184841948420484214842248423484244842548426484274842848429484304843148432484334843448435484364843748438484394844048441484424844348444484454844648447484484844948450484514845248453484544845548456484574845848459484604846148462484634846448465484664846748468484694847048471484724847348474484754847648477484784847948480484814848248483484844848548486484874848848489484904849148492484934849448495484964849748498484994850048501485024850348504485054850648507485084850948510485114851248513485144851548516485174851848519485204852148522485234852448525485264852748528485294853048531485324853348534485354853648537485384853948540485414854248543485444854548546485474854848549485504855148552485534855448555485564855748558485594856048561485624856348564485654856648567485684856948570485714857248573485744857548576485774857848579485804858148582485834858448585485864858748588485894859048591485924859348594485954859648597485984859948600486014860248603486044860548606486074860848609486104861148612486134861448615486164861748618486194862048621486224862348624486254862648627486284862948630486314863248633486344863548636486374863848639486404864148642486434864448645486464864748648486494865048651486524865348654486554865648657486584865948660486614866248663486644866548666486674866848669486704867148672486734867448675486764867748678486794868048681486824868348684486854868648687486884868948690486914869248693486944869548696486974869848699487004870148702487034870448705487064870748708487094871048711487124871348714487154871648717487184871948720487214872248723487244872548726487274872848729487304873148732487334873448735487364873748738487394874048741487424874348744487454874648747487484874948750487514875248753487544875548756487574875848759487604876148762487634876448765487664876748768487694877048771487724877348774487754877648777487784877948780487814878248783487844878548786487874878848789487904879148792487934879448795487964879748798487994880048801488024880348804488054880648807488084880948810488114881248813488144881548816488174881848819488204882148822488234882448825488264882748828488294883048831488324883348834488354883648837488384883948840488414884248843488444884548846488474884848849488504885148852488534885448855488564885748858488594886048861488624886348864488654886648867488684886948870488714887248873488744887548876488774887848879488804888148882488834888448885488864888748888488894889048891488924889348894488954889648897488984889948900489014890248903489044890548906489074890848909489104891148912489134891448915489164891748918489194892048921489224892348924489254892648927489284892948930489314893248933489344893548936489374893848939489404894148942489434894448945489464894748948489494895048951489524895348954489554895648957489584895948960489614896248963489644896548966489674896848969489704897148972489734897448975489764897748978489794898048981489824898348984489854898648987489884898948990489914899248993489944899548996489974899848999490004900149002490034900449005490064900749008490094901049011490124901349014490154901649017490184901949020490214902249023490244902549026490274902849029490304903149032490334903449035490364903749038490394904049041490424904349044490454904649047490484904949050490514905249053490544905549056490574905849059490604906149062490634906449065490664906749068490694907049071490724907349074490754907649077490784907949080490814908249083490844908549086490874908849089490904909149092490934909449095490964909749098490994910049101491024910349104491054910649107491084910949110491114911249113491144911549116491174911849119491204912149122491234912449125491264912749128491294913049131491324913349134491354913649137491384913949140491414914249143491444914549146491474914849149491504915149152491534915449155491564915749158491594916049161491624916349164491654916649167491684916949170491714917249173491744917549176491774917849179491804918149182491834918449185491864918749188491894919049191491924919349194491954919649197491984919949200492014920249203492044920549206492074920849209492104921149212492134921449215492164921749218492194922049221492224922349224492254922649227492284922949230492314923249233492344923549236492374923849239492404924149242492434924449245492464924749248492494925049251492524925349254492554925649257492584925949260492614926249263492644926549266492674926849269492704927149272492734927449275492764927749278492794928049281492824928349284492854928649287492884928949290492914929249293492944929549296492974929849299493004930149302493034930449305493064930749308493094931049311493124931349314493154931649317493184931949320493214932249323493244932549326493274932849329493304933149332493334933449335493364933749338493394934049341493424934349344493454934649347493484934949350493514935249353493544935549356493574935849359493604936149362493634936449365493664936749368493694937049371493724937349374493754937649377493784937949380493814938249383493844938549386493874938849389493904939149392493934939449395493964939749398493994940049401494024940349404494054940649407494084940949410494114941249413494144941549416494174941849419494204942149422494234942449425494264942749428494294943049431494324943349434494354943649437494384943949440494414944249443494444944549446494474944849449494504945149452494534945449455494564945749458494594946049461494624946349464494654946649467494684946949470494714947249473494744947549476494774947849479494804948149482494834948449485494864948749488494894949049491494924949349494494954949649497494984949949500495014950249503495044950549506495074950849509495104951149512495134951449515495164951749518495194952049521495224952349524495254952649527495284952949530495314953249533495344953549536495374953849539495404954149542495434954449545495464954749548495494955049551495524955349554495554955649557495584955949560495614956249563495644956549566495674956849569495704957149572495734957449575495764957749578495794958049581495824958349584495854958649587495884958949590495914959249593495944959549596495974959849599496004960149602496034960449605496064960749608496094961049611496124961349614496154961649617496184961949620496214962249623496244962549626496274962849629496304963149632496334963449635496364963749638496394964049641496424964349644496454964649647496484964949650496514965249653496544965549656496574965849659496604966149662496634966449665496664966749668496694967049671496724967349674496754967649677496784967949680496814968249683496844968549686496874968849689496904969149692496934969449695496964969749698496994970049701497024970349704497054970649707497084970949710497114971249713497144971549716497174971849719497204972149722497234972449725497264972749728497294973049731497324973349734497354973649737497384973949740497414974249743497444974549746497474974849749497504975149752497534975449755497564975749758497594976049761497624976349764497654976649767497684976949770497714977249773497744977549776497774977849779497804978149782497834978449785497864978749788497894979049791497924979349794497954979649797497984979949800498014980249803498044980549806498074980849809498104981149812498134981449815498164981749818498194982049821498224982349824498254982649827498284982949830498314983249833498344983549836498374983849839498404984149842498434984449845498464984749848498494985049851498524985349854498554985649857498584985949860498614986249863498644986549866498674986849869498704987149872498734987449875498764987749878498794988049881498824988349884498854988649887498884988949890498914989249893498944989549896498974989849899499004990149902499034990449905499064990749908499094991049911499124991349914499154991649917499184991949920499214992249923499244992549926499274992849929499304993149932499334993449935499364993749938499394994049941499424994349944499454994649947499484994949950499514995249953499544995549956499574995849959499604996149962499634996449965499664996749968499694997049971499724997349974499754997649977499784997949980499814998249983499844998549986499874998849989499904999149992499934999449995499964999749998499995000050001500025000350004500055000650007500085000950010500115001250013500145001550016500175001850019500205002150022500235002450025500265002750028500295003050031500325003350034500355003650037500385003950040500415004250043500445004550046500475004850049500505005150052500535005450055500565005750058500595006050061500625006350064500655006650067500685006950070500715007250073500745007550076500775007850079500805008150082500835008450085500865008750088500895009050091500925009350094500955009650097500985009950100501015010250103501045010550106501075010850109501105011150112501135011450115501165011750118501195012050121501225012350124501255012650127501285012950130501315013250133501345013550136501375013850139501405014150142501435014450145501465014750148501495015050151501525015350154501555015650157501585015950160501615016250163501645016550166501675016850169501705017150172501735017450175501765017750178501795018050181501825018350184501855018650187501885018950190501915019250193501945019550196501975019850199502005020150202502035020450205502065020750208502095021050211502125021350214502155021650217502185021950220502215022250223502245022550226502275022850229502305023150232502335023450235502365023750238502395024050241502425024350244502455024650247502485024950250502515025250253502545025550256502575025850259502605026150262502635026450265502665026750268502695027050271502725027350274502755027650277502785027950280502815028250283502845028550286502875028850289502905029150292502935029450295502965029750298502995030050301503025030350304503055030650307503085030950310503115031250313503145031550316503175031850319503205032150322503235032450325503265032750328503295033050331503325033350334503355033650337503385033950340503415034250343503445034550346503475034850349503505035150352503535035450355503565035750358503595036050361503625036350364503655036650367503685036950370503715037250373503745037550376503775037850379503805038150382503835038450385503865038750388503895039050391503925039350394503955039650397503985039950400504015040250403504045040550406504075040850409504105041150412504135041450415504165041750418504195042050421504225042350424504255042650427504285042950430504315043250433504345043550436504375043850439504405044150442504435044450445504465044750448504495045050451504525045350454504555045650457504585045950460504615046250463504645046550466504675046850469504705047150472504735047450475504765047750478504795048050481504825048350484504855048650487504885048950490504915049250493504945049550496504975049850499505005050150502505035050450505505065050750508505095051050511505125051350514505155051650517505185051950520505215052250523505245052550526505275052850529505305053150532505335053450535505365053750538505395054050541505425054350544505455054650547505485054950550505515055250553505545055550556505575055850559505605056150562505635056450565505665056750568505695057050571505725057350574505755057650577505785057950580505815058250583505845058550586505875058850589505905059150592505935059450595505965059750598505995060050601506025060350604506055060650607506085060950610506115061250613506145061550616506175061850619506205062150622506235062450625506265062750628506295063050631506325063350634506355063650637506385063950640506415064250643506445064550646506475064850649506505065150652506535065450655506565065750658506595066050661506625066350664506655066650667506685066950670506715067250673506745067550676506775067850679506805068150682506835068450685506865068750688506895069050691506925069350694506955069650697506985069950700507015070250703507045070550706507075070850709507105071150712507135071450715507165071750718507195072050721507225072350724507255072650727507285072950730507315073250733507345073550736507375073850739507405074150742507435074450745507465074750748507495075050751507525075350754507555075650757507585075950760507615076250763507645076550766507675076850769507705077150772507735077450775507765077750778507795078050781507825078350784507855078650787507885078950790507915079250793507945079550796507975079850799508005080150802508035080450805508065080750808508095081050811508125081350814508155081650817508185081950820508215082250823508245082550826508275082850829508305083150832508335083450835508365083750838508395084050841508425084350844508455084650847508485084950850508515085250853508545085550856508575085850859508605086150862508635086450865508665086750868508695087050871508725087350874508755087650877508785087950880508815088250883508845088550886508875088850889508905089150892508935089450895508965089750898508995090050901509025090350904509055090650907509085090950910509115091250913509145091550916509175091850919509205092150922509235092450925509265092750928509295093050931509325093350934509355093650937509385093950940509415094250943509445094550946509475094850949509505095150952509535095450955509565095750958509595096050961509625096350964509655096650967509685096950970509715097250973509745097550976509775097850979509805098150982509835098450985509865098750988509895099050991509925099350994509955099650997509985099951000510015100251003510045100551006510075100851009510105101151012510135101451015510165101751018510195102051021510225102351024510255102651027510285102951030510315103251033510345103551036510375103851039510405104151042510435104451045510465104751048510495105051051510525105351054510555105651057510585105951060510615106251063510645106551066510675106851069510705107151072510735107451075510765107751078510795108051081510825108351084510855108651087510885108951090510915109251093510945109551096510975109851099511005110151102511035110451105511065110751108511095111051111511125111351114511155111651117511185111951120511215112251123511245112551126511275112851129511305113151132511335113451135511365113751138511395114051141511425114351144511455114651147511485114951150511515115251153511545115551156511575115851159511605116151162511635116451165511665116751168511695117051171511725117351174511755117651177511785117951180511815118251183511845118551186511875118851189511905119151192511935119451195511965119751198511995120051201512025120351204512055120651207512085120951210512115121251213512145121551216512175121851219512205122151222512235122451225512265122751228512295123051231512325123351234512355123651237512385123951240512415124251243512445124551246512475124851249512505125151252512535125451255512565125751258512595126051261512625126351264512655126651267512685126951270512715127251273512745127551276512775127851279512805128151282512835128451285512865128751288512895129051291512925129351294512955129651297512985129951300513015130251303513045130551306513075130851309513105131151312513135131451315513165131751318513195132051321513225132351324513255132651327513285132951330513315133251333513345133551336513375133851339513405134151342513435134451345513465134751348513495135051351513525135351354513555135651357513585135951360513615136251363513645136551366513675136851369513705137151372513735137451375513765137751378513795138051381513825138351384513855138651387513885138951390513915139251393513945139551396513975139851399514005140151402514035140451405514065140751408514095141051411514125141351414514155141651417514185141951420514215142251423514245142551426514275142851429514305143151432514335143451435514365143751438514395144051441514425144351444514455144651447514485144951450514515145251453514545145551456514575145851459514605146151462514635146451465514665146751468514695147051471514725147351474514755147651477514785147951480514815148251483514845148551486514875148851489514905149151492514935149451495514965149751498514995150051501515025150351504515055150651507515085150951510515115151251513515145151551516515175151851519515205152151522515235152451525515265152751528515295153051531515325153351534515355153651537515385153951540515415154251543515445154551546515475154851549515505155151552515535155451555515565155751558515595156051561515625156351564515655156651567515685156951570515715157251573515745157551576515775157851579515805158151582515835158451585515865158751588515895159051591515925159351594515955159651597515985159951600516015160251603516045160551606516075160851609516105161151612516135161451615516165161751618516195162051621516225162351624516255162651627516285162951630516315163251633516345163551636516375163851639516405164151642516435164451645516465164751648516495165051651516525165351654516555165651657516585165951660516615166251663516645166551666516675166851669516705167151672516735167451675516765167751678516795168051681516825168351684516855168651687516885168951690516915169251693516945169551696516975169851699517005170151702517035170451705517065170751708517095171051711517125171351714517155171651717517185171951720517215172251723517245172551726517275172851729517305173151732517335173451735517365173751738517395174051741517425174351744517455174651747517485174951750517515175251753517545175551756517575175851759517605176151762517635176451765517665176751768517695177051771517725177351774517755177651777517785177951780517815178251783517845178551786517875178851789517905179151792517935179451795517965179751798517995180051801518025180351804518055180651807518085180951810518115181251813518145181551816518175181851819518205182151822518235182451825518265182751828518295183051831518325183351834518355183651837518385183951840518415184251843518445184551846518475184851849518505185151852518535185451855518565185751858518595186051861518625186351864518655186651867518685186951870518715187251873518745187551876518775187851879518805188151882518835188451885518865188751888518895189051891518925189351894518955189651897518985189951900519015190251903519045190551906519075190851909519105191151912519135191451915519165191751918519195192051921519225192351924519255192651927519285192951930519315193251933519345193551936519375193851939519405194151942519435194451945519465194751948519495195051951519525195351954519555195651957519585195951960519615196251963519645196551966519675196851969519705197151972519735197451975519765197751978519795198051981519825198351984519855198651987519885198951990519915199251993519945199551996519975199851999520005200152002520035200452005520065200752008520095201052011520125201352014520155201652017520185201952020520215202252023520245202552026520275202852029520305203152032520335203452035520365203752038520395204052041520425204352044520455204652047520485204952050520515205252053520545205552056520575205852059520605206152062520635206452065520665206752068520695207052071520725207352074520755207652077520785207952080520815208252083520845208552086520875208852089520905209152092520935209452095520965209752098520995210052101521025210352104521055210652107521085210952110521115211252113521145211552116521175211852119521205212152122521235212452125521265212752128521295213052131521325213352134521355213652137521385213952140521415214252143521445214552146521475214852149521505215152152521535215452155521565215752158521595216052161521625216352164521655216652167521685216952170521715217252173521745217552176521775217852179521805218152182521835218452185521865218752188521895219052191521925219352194521955219652197521985219952200522015220252203522045220552206522075220852209522105221152212522135221452215522165221752218522195222052221522225222352224522255222652227522285222952230522315223252233522345223552236522375223852239522405224152242522435224452245522465224752248522495225052251522525225352254522555225652257522585225952260522615226252263522645226552266522675226852269522705227152272522735227452275522765227752278522795228052281522825228352284522855228652287522885228952290522915229252293522945229552296522975229852299523005230152302523035230452305523065230752308523095231052311523125231352314523155231652317523185231952320523215232252323523245232552326523275232852329523305233152332523335233452335523365233752338523395234052341523425234352344523455234652347523485234952350523515235252353523545235552356523575235852359523605236152362523635236452365523665236752368523695237052371523725237352374523755237652377523785237952380523815238252383523845238552386523875238852389523905239152392523935239452395523965239752398523995240052401524025240352404524055240652407524085240952410524115241252413524145241552416524175241852419524205242152422524235242452425524265242752428524295243052431524325243352434524355243652437524385243952440524415244252443524445244552446524475244852449524505245152452524535245452455524565245752458524595246052461524625246352464524655246652467524685246952470524715247252473524745247552476524775247852479524805248152482524835248452485524865248752488524895249052491524925249352494524955249652497524985249952500525015250252503525045250552506525075250852509525105251152512525135251452515525165251752518525195252052521525225252352524525255252652527525285252952530525315253252533525345253552536525375253852539525405254152542525435254452545525465254752548525495255052551525525255352554525555255652557525585255952560525615256252563525645256552566525675256852569525705257152572525735257452575525765257752578525795258052581525825258352584525855258652587525885258952590525915259252593525945259552596525975259852599526005260152602526035260452605526065260752608526095261052611526125261352614526155261652617526185261952620526215262252623526245262552626526275262852629526305263152632526335263452635526365263752638526395264052641526425264352644526455264652647526485264952650526515265252653526545265552656526575265852659526605266152662526635266452665526665266752668526695267052671526725267352674526755267652677526785267952680526815268252683526845268552686526875268852689526905269152692526935269452695526965269752698526995270052701527025270352704527055270652707527085270952710527115271252713527145271552716527175271852719527205272152722527235272452725527265272752728527295273052731527325273352734527355273652737527385273952740527415274252743527445274552746527475274852749527505275152752527535275452755527565275752758527595276052761527625276352764527655276652767527685276952770527715277252773527745277552776527775277852779527805278152782527835278452785527865278752788527895279052791527925279352794527955279652797527985279952800528015280252803528045280552806528075280852809528105281152812528135281452815528165281752818528195282052821528225282352824528255282652827528285282952830528315283252833528345283552836528375283852839528405284152842528435284452845528465284752848528495285052851528525285352854528555285652857528585285952860528615286252863528645286552866528675286852869528705287152872528735287452875528765287752878528795288052881528825288352884528855288652887528885288952890528915289252893528945289552896528975289852899529005290152902529035290452905529065290752908529095291052911529125291352914529155291652917529185291952920529215292252923529245292552926529275292852929529305293152932529335293452935529365293752938529395294052941529425294352944529455294652947529485294952950529515295252953529545295552956529575295852959529605296152962529635296452965529665296752968529695297052971529725297352974529755297652977529785297952980529815298252983529845298552986529875298852989529905299152992529935299452995529965299752998529995300053001530025300353004530055300653007530085300953010530115301253013530145301553016530175301853019530205302153022530235302453025530265302753028530295303053031530325303353034530355303653037530385303953040530415304253043530445304553046530475304853049530505305153052530535305453055530565305753058530595306053061530625306353064530655306653067530685306953070530715307253073530745307553076530775307853079530805308153082530835308453085530865308753088530895309053091530925309353094530955309653097530985309953100531015310253103531045310553106531075310853109531105311153112531135311453115531165311753118531195312053121531225312353124531255312653127531285312953130531315313253133531345313553136531375313853139531405314153142531435314453145531465314753148531495315053151531525315353154531555315653157531585315953160531615316253163531645316553166531675316853169531705317153172531735317453175531765317753178531795318053181531825318353184531855318653187531885318953190531915319253193531945319553196531975319853199532005320153202532035320453205532065320753208532095321053211532125321353214532155321653217532185321953220532215322253223532245322553226532275322853229532305323153232532335323453235532365323753238532395324053241532425324353244532455324653247532485324953250532515325253253532545325553256532575325853259532605326153262532635326453265532665326753268532695327053271532725327353274532755327653277532785327953280532815328253283532845328553286532875328853289532905329153292532935329453295532965329753298532995330053301533025330353304533055330653307533085330953310533115331253313533145331553316533175331853319533205332153322533235332453325533265332753328533295333053331533325333353334533355333653337533385333953340533415334253343533445334553346533475334853349533505335153352533535335453355533565335753358533595336053361533625336353364533655336653367533685336953370533715337253373533745337553376533775337853379533805338153382533835338453385533865338753388533895339053391533925339353394533955339653397533985339953400534015340253403534045340553406534075340853409534105341153412534135341453415534165341753418534195342053421
  1. /* test.c
  2. *
  3. * Copyright (C) 2006-2024 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. * Some common, optional build settings:
  23. * these can also be set in wolfssl/options.h or user_settings.h
  24. * -------------------------------------------------------------
  25. *
  26. * set the default devId for cryptocb to the value instead of INVALID_DEVID
  27. * WC_USE_DEVID=0x1234
  28. */
  29. #ifdef HAVE_CONFIG_H
  30. #include <config.h>
  31. #endif
  32. #ifndef WOLFSSL_USER_SETTINGS
  33. #include <wolfssl/options.h>
  34. #endif
  35. #include <wolfssl/wolfcrypt/settings.h>
  36. #ifndef NO_CRYPT_TEST
  37. #include <wolfssl/version.h>
  38. #include <wolfssl/wolfcrypt/types.h>
  39. #include <wolfssl/wolfcrypt/wc_port.h>
  40. #include <wolfssl/wolfcrypt/mem_track.h>
  41. #if defined(HAVE_WOLFCRYPT_TEST_OPTIONS)
  42. #include <wolfssl/ssl.h>
  43. #define err_sys err_sys_remap /* remap err_sys */
  44. #include <wolfssl/test.h>
  45. #undef err_sys
  46. #endif
  47. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  48. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  49. #include <stdint.h>
  50. #endif
  51. #ifdef HAVE_STACK_SIZE_VERBOSE
  52. #ifdef WOLFSSL_TEST_MAX_RELATIVE_STACK_BYTES
  53. static WC_MAYBE_UNUSED ssize_t max_relative_stack =
  54. WOLFSSL_TEST_MAX_RELATIVE_STACK_BYTES;
  55. #else
  56. static WC_MAYBE_UNUSED ssize_t max_relative_stack = -1;
  57. #endif
  58. #endif
  59. const byte const_byte_array[] = "A+Gd\0\0\0";
  60. #define CBPTR_EXPECTED 'A'
  61. #if defined(WOLFSSL_TRACK_MEMORY_VERBOSE) && !defined(WOLFSSL_STATIC_MEMORY)
  62. #ifdef WOLFSSL_TEST_MAX_RELATIVE_HEAP_ALLOCS
  63. static ssize_t max_relative_heap_allocs = WOLFSSL_TEST_MAX_RELATIVE_HEAP_ALLOCS;
  64. #else
  65. static ssize_t max_relative_heap_allocs = -1;
  66. #endif
  67. #ifdef WOLFSSL_TEST_MAX_RELATIVE_HEAP_BYTES
  68. static ssize_t max_relative_heap_bytes = WOLFSSL_TEST_MAX_RELATIVE_HEAP_BYTES;
  69. #else
  70. static ssize_t max_relative_heap_bytes = -1;
  71. #endif
  72. #define PRINT_HEAP_CHECKPOINT() { \
  73. const ssize_t _rha = wolfCrypt_heap_peakAllocs_checkpoint() - heap_baselineAllocs; \
  74. const ssize_t _rhb = wolfCrypt_heap_peakBytes_checkpoint() - heap_baselineBytes; \
  75. printf(" relative heap peak usage: %ld alloc%s, %ld bytes\n", \
  76. (long int)_rha, \
  77. _rha == 1 ? "" : "s", \
  78. (long int)_rhb); \
  79. if ((max_relative_heap_allocs > 0) && (_rha > max_relative_heap_allocs)) \
  80. return err_sys("heap allocs exceed designated max.", \
  81. WC_TEST_RET_ENC_NC); \
  82. if ((max_relative_heap_bytes > 0) && (_rhb > max_relative_heap_bytes)) \
  83. return err_sys("heap bytes exceed designated max.", \
  84. WC_TEST_RET_ENC_NC); \
  85. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint(); \
  86. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint(); \
  87. }
  88. #else
  89. #define PRINT_HEAP_CHECKPOINT() WC_DO_NOTHING
  90. #endif /* WOLFSSL_TRACK_MEMORY_VERBOSE && !WOLFSSL_STATIC_MEMORY */
  91. #ifdef USE_FLAT_TEST_H
  92. #ifdef HAVE_CONFIG_H
  93. #include "test_paths.h"
  94. #endif
  95. #include "test.h"
  96. #else
  97. #ifdef HAVE_CONFIG_H
  98. #include "wolfcrypt/test/test_paths.h"
  99. #endif
  100. #include "wolfcrypt/test/test.h"
  101. #endif
  102. /* printf mappings */
  103. #ifndef WOLFSSL_LOG_PRINTF
  104. #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  105. #include <mqx.h>
  106. #include <stdlib.h>
  107. /* see wc_port.h for fio.h and nio.h includes */
  108. #elif defined(FREESCALE_KSDK_BM)
  109. #include "fsl_debug_console.h"
  110. #undef printf
  111. #define printf PRINTF
  112. #elif defined(WOLFSSL_APACHE_MYNEWT)
  113. #include <assert.h>
  114. #include <string.h>
  115. #include "sysinit/sysinit.h"
  116. #include "os/os.h"
  117. #ifdef ARCH_sim
  118. #include "mcu/mcu_sim.h"
  119. #endif
  120. #include "os/os_time.h"
  121. #elif defined(WOLFSSL_ESPIDF)
  122. #include <time.h>
  123. #include <sys/time.h>
  124. #include <esp_log.h>
  125. #include <wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h>
  126. #define ESPIDF_TAG "wc_test"
  127. #elif defined(WOLFSSL_ZEPHYR)
  128. #include <stdio.h>
  129. #define printf printk
  130. #elif defined(MICRIUM)
  131. #include <os.h>
  132. #if (OS_VERSION < 50000)
  133. #include <bsp_ser.h>
  134. void BSP_Ser_Printf (CPU_CHAR* format, ...);
  135. #undef printf
  136. #define printf BSP_Ser_Printf
  137. #else
  138. #include <stdio.h>
  139. #endif
  140. #elif defined(WOLFSSL_PB)
  141. #include <stdarg.h>
  142. int wolfssl_pb_print(const char*, ...);
  143. #undef printf
  144. #define printf wolfssl_pb_print
  145. #elif defined(WOLFSSL_TELIT_M2MB)
  146. #include "wolfssl/wolfcrypt/wc_port.h" /* for m2mb headers */
  147. #include "m2m_log.h" /* for M2M_LOG_INFO - not standard API */
  148. /* remap printf */
  149. #undef printf
  150. #define printf M2M_LOG_INFO
  151. /* OS requires occasional sleep() */
  152. #ifndef TEST_SLEEP_MS
  153. #define TEST_SLEEP_MS 50
  154. #endif
  155. #define TEST_SLEEP() m2mb_os_taskSleep(M2MB_OS_MS2TICKS(TEST_SLEEP_MS))
  156. /* don't use file system for these tests, since ./certs dir isn't loaded */
  157. #undef NO_FILESYSTEM
  158. #define NO_FILESYSTEM
  159. #elif defined(THREADX) && !defined(WOLFSSL_WICED) && \
  160. !defined(THREADX_NO_DC_PRINTF)
  161. #ifndef NETOS
  162. /* since just testing, use THREADX log printf instead (NETOS prototypes
  163. * this elsewhere) */
  164. int dc_log_printf(char*, ...);
  165. #endif
  166. #undef printf
  167. #define printf dc_log_printf
  168. #elif defined(ANDROID)
  169. #ifdef XMALLOC_USER
  170. #include <stdlib.h> /* we're using malloc / free direct here */
  171. #endif
  172. #ifndef STRING_USER
  173. #include <stdio.h>
  174. #endif
  175. #include <android/log.h>
  176. #ifdef ANDROID_V454 /* See fips/android/wolfCrypt_v454_android */
  177. #ifndef NO_FILESYSTEM
  178. #define NO_FILESYSTEM /* Turn off tests that want to call SaveDerAndPem() */
  179. #endif
  180. #else
  181. #define printf(...) \
  182. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  183. #define fprintf(fp, ...) \
  184. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  185. #endif
  186. #elif defined(WOLFSSL_DEOS)
  187. #include <printx.h>
  188. #undef printf
  189. #define printf printx
  190. #elif defined(WOLFSSL_RENESAS_RSIP)
  191. #ifndef TEST_SLEEP
  192. #define TEST_SLEEP() vTaskDelay(50)
  193. #endif
  194. #undef vprintf
  195. #define vprintf rsip_vprintf
  196. #include <stdarg.h> /* for var args */
  197. int rsip_vprintf(const char* restrict format, va_list args)
  198. {
  199. int ret;
  200. char tmpBuf[80];
  201. ret = XVSNPRINTF(tmpBuf, sizeof(tmpBuf), format, args);
  202. printf(tmpBuf);
  203. return ret;
  204. }
  205. #else
  206. #ifdef XMALLOC_USER
  207. #include <stdlib.h> /* we're using malloc / free direct here */
  208. #endif
  209. #if !defined(STRING_USER) && !defined(WOLFSSL_LINUXKM)
  210. #include <stdio.h>
  211. #endif
  212. #if defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_LINUXKM_VERBOSE_DEBUG)
  213. #undef printf
  214. #define printf(...) ({})
  215. #endif
  216. /* enable way for customer to override test/bench printf */
  217. #ifdef XPRINTF
  218. #undef printf
  219. #define printf XPRINTF
  220. #elif !defined(printf)
  221. /* arrange for printf() to flush after every message -- this assures
  222. * redirected output (to a log file) records progress right up to the
  223. * moment of a crash/abort(); otherwise anything queued in stdout would
  224. * be lost.
  225. */
  226. #define printf(...) ( printf(__VA_ARGS__), fflush(stdout) )
  227. #endif
  228. #endif
  229. #endif /* !WOLFSSL_LOG_PRINTF */
  230. #include <wolfssl/wolfcrypt/memory.h>
  231. #include <wolfssl/wolfcrypt/wc_port.h>
  232. #include <wolfssl/wolfcrypt/logging.h>
  233. #include <wolfssl/wolfcrypt/types.h>
  234. #include <wolfssl/wolfcrypt/asn.h>
  235. #include <wolfssl/wolfcrypt/md2.h>
  236. #include <wolfssl/wolfcrypt/md5.h>
  237. #include <wolfssl/wolfcrypt/md4.h>
  238. #include <wolfssl/wolfcrypt/sha.h>
  239. #include <wolfssl/wolfcrypt/sha256.h>
  240. #include <wolfssl/wolfcrypt/sha512.h>
  241. #include <wolfssl/wolfcrypt/hash.h>
  242. #include <wolfssl/wolfcrypt/rc2.h>
  243. #include <wolfssl/wolfcrypt/arc4.h>
  244. #if !defined(WC_NO_RNG)
  245. #include <wolfssl/wolfcrypt/random.h>
  246. #endif
  247. #include <wolfssl/wolfcrypt/wolfmath.h>
  248. #include <wolfssl/wolfcrypt/coding.h>
  249. #include <wolfssl/wolfcrypt/signature.h>
  250. #include <wolfssl/wolfcrypt/rsa.h>
  251. #include <wolfssl/wolfcrypt/des3.h>
  252. #include <wolfssl/wolfcrypt/aes.h>
  253. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  254. #include <wolfssl/wolfcrypt/cmac.h>
  255. #include <wolfssl/wolfcrypt/siphash.h>
  256. #include <wolfssl/wolfcrypt/poly1305.h>
  257. #include <wolfssl/wolfcrypt/camellia.h>
  258. #include <wolfssl/wolfcrypt/hmac.h>
  259. #include <wolfssl/wolfcrypt/kdf.h>
  260. #include <wolfssl/wolfcrypt/dh.h>
  261. #include <wolfssl/wolfcrypt/dsa.h>
  262. #include <wolfssl/wolfcrypt/srp.h>
  263. #include <wolfssl/wolfcrypt/chacha.h>
  264. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  265. #include <wolfssl/wolfcrypt/pwdbased.h>
  266. #include <wolfssl/wolfcrypt/ripemd.h>
  267. #include <wolfssl/wolfcrypt/error-crypt.h>
  268. #ifdef HAVE_ECC
  269. #include <wolfssl/wolfcrypt/ecc.h>
  270. #endif
  271. #ifdef WOLFSSL_SM2
  272. #include <wolfssl/wolfcrypt/sm2.h>
  273. #endif
  274. #ifdef HAVE_HPKE
  275. #include <wolfssl/wolfcrypt/hpke.h>
  276. #endif
  277. #ifdef HAVE_CURVE25519
  278. #include <wolfssl/wolfcrypt/curve25519.h>
  279. #endif
  280. #ifdef HAVE_ED25519
  281. #include <wolfssl/wolfcrypt/ed25519.h>
  282. #endif
  283. #ifdef HAVE_CURVE448
  284. #include <wolfssl/wolfcrypt/curve448.h>
  285. #endif
  286. #ifdef HAVE_ED448
  287. #include <wolfssl/wolfcrypt/ed448.h>
  288. #endif
  289. #ifdef WOLFSSL_HAVE_KYBER
  290. #include <wolfssl/wolfcrypt/kyber.h>
  291. #ifdef WOLFSSL_WC_KYBER
  292. #include <wolfssl/wolfcrypt/wc_kyber.h>
  293. #endif
  294. #if defined(HAVE_LIBOQS) || defined(HAVE_PQM4)
  295. #include <wolfssl/wolfcrypt/ext_kyber.h>
  296. #endif
  297. #endif
  298. #if defined(WOLFSSL_HAVE_XMSS)
  299. #include <wolfssl/wolfcrypt/xmss.h>
  300. #ifdef HAVE_LIBXMSS
  301. #include <wolfssl/wolfcrypt/ext_xmss.h>
  302. #else
  303. #include <wolfssl/wolfcrypt/wc_xmss.h>
  304. #endif
  305. #endif
  306. #if defined(WOLFSSL_HAVE_LMS)
  307. #include <wolfssl/wolfcrypt/lms.h>
  308. #ifdef HAVE_LIBLMS
  309. #include <wolfssl/wolfcrypt/ext_lms.h>
  310. #else
  311. #include <wolfssl/wolfcrypt/wc_lms.h>
  312. #endif
  313. #endif
  314. #ifdef WOLFCRYPT_HAVE_ECCSI
  315. #include <wolfssl/wolfcrypt/eccsi.h>
  316. #endif
  317. #ifdef WOLFCRYPT_HAVE_SAKKE
  318. #include <wolfssl/wolfcrypt/sakke.h>
  319. #endif
  320. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  321. #include <wolfssl/wolfcrypt/blake2.h>
  322. #endif
  323. #ifdef WOLFSSL_SHA3
  324. #include <wolfssl/wolfcrypt/sha3.h>
  325. #endif
  326. #ifdef WOLFSSL_SM3
  327. #include <wolfssl/wolfcrypt/sm3.h>
  328. #endif
  329. #ifdef WOLFSSL_SM4
  330. #include <wolfssl/wolfcrypt/sm4.h>
  331. #endif
  332. #ifdef HAVE_LIBZ
  333. #include <wolfssl/wolfcrypt/compress.h>
  334. #endif
  335. #ifdef HAVE_PKCS7
  336. #include <wolfssl/wolfcrypt/pkcs7.h>
  337. #endif
  338. #ifdef HAVE_PKCS12
  339. #include <wolfssl/wolfcrypt/pkcs12.h>
  340. #endif
  341. #ifdef HAVE_FIPS
  342. #include <wolfssl/wolfcrypt/fips_test.h>
  343. #endif
  344. #ifdef HAVE_SELFTEST
  345. #include <wolfssl/wolfcrypt/selftest.h>
  346. #endif
  347. #ifdef WOLFSSL_ASYNC_CRYPT
  348. #include <wolfssl/wolfcrypt/async.h>
  349. #endif
  350. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  351. #include <wolfssl/wolfcrypt/logging.h>
  352. #endif
  353. #ifdef WOLFSSL_CAAM
  354. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  355. #endif
  356. #ifdef WOLF_CRYPTO_CB
  357. #include <wolfssl/wolfcrypt/cryptocb.h>
  358. #ifdef HAVE_INTEL_QA_SYNC
  359. #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
  360. #endif
  361. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  362. #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
  363. #endif
  364. #ifdef HAVE_RENESAS_SYNC
  365. #include <wolfssl/wolfcrypt/port/renesas/renesas_sync.h>
  366. #endif
  367. #endif
  368. #ifdef _MSC_VER
  369. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  370. #pragma warning(disable: 4996)
  371. #endif
  372. #ifdef OPENSSL_EXTRA
  373. #ifndef WOLFCRYPT_ONLY
  374. #include <wolfssl/openssl/evp.h>
  375. #include <wolfssl/openssl/hmac.h>
  376. #endif
  377. #include <wolfssl/openssl/rand.h>
  378. #include <wolfssl/openssl/aes.h>
  379. #include <wolfssl/openssl/des.h>
  380. #endif
  381. #if defined(NO_FILESYSTEM) || defined(WC_NO_RNG)
  382. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  383. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  384. #define USE_CERT_BUFFERS_2048
  385. #endif
  386. #if !defined(USE_CERT_BUFFERS_256)
  387. #define USE_CERT_BUFFERS_256
  388. #endif
  389. #endif
  390. #if defined(WOLFSSL_CERT_GEN) && (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  391. #define ENABLE_ECC384_CERT_GEN_TEST
  392. #endif
  393. #include <wolfssl/certs_test.h>
  394. #ifdef DEVKITPRO
  395. #include <wiiuse/wpad.h>
  396. #endif
  397. #ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  398. /* FIPS build has replaced ecc.h. */
  399. #define wc_ecc_key_get_priv(key) (&((key)->k))
  400. #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  401. #endif
  402. #ifdef WOLFSSL_STATIC_MEMORY
  403. static WOLFSSL_HEAP_HINT* HEAP_HINT;
  404. #else
  405. #define HEAP_HINT NULL
  406. #endif /* WOLFSSL_STATIC_MEMORY */
  407. /* these cases do not have intermediate hashing support */
  408. #if (defined(WOLFSSL_AFALG_XILINX_SHA3) && !defined(WOLFSSL_AFALG_HASH_KEEP)) \
  409. && !defined(WOLFSSL_XILINX_CRYPT) || defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  410. #define NO_INTM_HASH_TEST
  411. #endif
  412. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  413. defined(WOLFSSL_RENESAS_SCEPROTECT_CRYPTONLY) || \
  414. defined(WOLFSSL_SECO_CAAM)
  415. #define HASH_SIZE_LIMIT
  416. #endif
  417. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  418. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  419. static void initDefaultName(void);
  420. #endif
  421. /* for async devices */
  422. #ifdef WOLFSSL_CAAM_DEVID
  423. static int devId = WOLFSSL_CAAM_DEVID;
  424. #else
  425. #ifdef WC_USE_DEVID
  426. static int devId = WC_USE_DEVID;
  427. #else
  428. static int devId = INVALID_DEVID;
  429. #endif
  430. #endif
  431. #ifdef HAVE_WNR
  432. const char* wnrConfigFile = "wnr-example.conf";
  433. #endif
  434. #define TEST_STRING "Everyone gets Friday off."
  435. #define TEST_STRING_SZ 25
  436. typedef struct testVector {
  437. const char* input;
  438. const char* output;
  439. size_t inLen;
  440. size_t outLen;
  441. } testVector;
  442. #ifndef WOLFSSL_TEST_SUBROUTINE
  443. #define WOLFSSL_TEST_SUBROUTINE
  444. #endif
  445. PRAGMA_GCC("GCC diagnostic ignored \"-Wunused-function\"")
  446. PRAGMA_CLANG("clang diagnostic ignored \"-Wunused-function\"")
  447. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t error_test(void);
  448. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base64_test(void);
  449. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base16_test(void);
  450. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t asn_test(void);
  451. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md2_test(void);
  452. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md5_test(void);
  453. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md4_test(void);
  454. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha_test(void);
  455. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha224_test(void);
  456. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha256_test(void);
  457. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_test(void);
  458. #if !defined(WOLFSSL_NOSHA512_224) && \
  459. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  460. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_224_test(void);
  461. #endif
  462. #if !defined(WOLFSSL_NOSHA512_256) && \
  463. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  464. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_256_test(void);
  465. #endif
  466. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha384_test(void);
  467. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha3_test(void);
  468. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake128_test(void);
  469. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake256_test(void);
  470. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm3_test(void);
  471. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hash_test(void);
  472. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_md5_test(void);
  473. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha_test(void);
  474. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha224_test(void);
  475. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha256_test(void);
  476. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha384_test(void);
  477. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha512_test(void);
  478. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha3_test(void);
  479. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  480. #if defined(WOLFSSL_AFALG_XILINX) || defined(WOLFSSL_AFALG_XILINX_AES) || \
  481. defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_AFALG_HASH_KEEP) || \
  482. defined(WOLFSSL_AFALG_XILINX_RSA)
  483. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  484. static wc_test_ret_t hkdf_test(void);
  485. #else
  486. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hkdf_test(void);
  487. #endif
  488. #endif /* HAVE_HKDF && ! NO_HMAC */
  489. #ifdef WOLFSSL_HAVE_PRF
  490. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  491. #ifdef WOLFSSL_BASE16
  492. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t tls12_kdf_test(void);
  493. #endif /* WOLFSSL_BASE16 */
  494. #endif /* WOLFSSL_HAVE_HKDF && !NO_HMAC */
  495. #endif /* WOLFSSL_HAVE_PRF */
  496. #if defined(WOLFSSL_HAVE_PRF) && !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  497. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t prf_test(void);
  498. #endif
  499. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sshkdf_test(void);
  500. #ifdef WOLFSSL_TLS13
  501. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t tls13_kdf_test(void);
  502. #endif
  503. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t x963kdf_test(void);
  504. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hpke_test(void);
  505. #ifdef WC_SRTP_KDF
  506. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srtpkdf_test(void);
  507. #endif
  508. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t arc4_test(void);
  509. #ifdef WC_RC2
  510. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rc2_test(void);
  511. #endif
  512. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha_test(void);
  513. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha_test(void);
  514. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha20_poly1305_aead_test(void);
  515. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha20Poly1305_test(void);
  516. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des_test(void);
  517. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des3_test(void);
  518. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_test(void);
  519. #if defined(WOLFSSL_AES_CFB)
  520. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_cfb_test(void);
  521. #endif
  522. #ifdef WOLFSSL_AES_XTS
  523. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_xts_test(void);
  524. #endif
  525. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes192_test(void);
  526. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes256_test(void);
  527. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesofb_test(void);
  528. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cmac_test(void);
  529. #if defined(WOLFSSL_SIPHASH)
  530. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t siphash_test(void);
  531. #endif
  532. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t poly1305_test(void);
  533. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_test(void);
  534. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_default_test(void);
  535. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t gmac_test(void);
  536. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesccm_test(void);
  537. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aeskeywrap_test(void);
  538. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t camellia_test(void);
  539. #ifdef WOLFSSL_SM4
  540. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm4_test(void);
  541. #endif
  542. #ifdef WC_RSA_NO_PADDING
  543. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_no_pad_test(void);
  544. #endif
  545. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void);
  546. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dh_test(void);
  547. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dsa_test(void);
  548. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srp_test(void);
  549. #ifndef WC_NO_RNG
  550. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_test(void);
  551. #endif /* WC_NO_RNG */
  552. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pwdbased_test(void);
  553. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_test(void);
  554. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ripemd_test(void);
  555. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  556. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_test(void); /* test mini api */
  557. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey0_test(void);
  558. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey1_test(void);
  559. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openSSL_evpMD_test(void);
  560. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_evpSig_test(void);
  561. #endif
  562. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf1_test(void);
  563. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_pbkdf_test(void);
  564. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  565. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf2_test(void);
  566. #endif
  567. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t scrypt_test(void);
  568. #ifdef HAVE_ECC
  569. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test(void);
  570. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  571. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  572. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_encrypt_test(void);
  573. #endif
  574. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  575. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  576. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  577. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  578. /* skip for ATECC508/608A, cannot import private key buffers */
  579. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test_buffers(void);
  580. #endif
  581. #endif
  582. #ifdef HAVE_CURVE25519
  583. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve25519_test(void);
  584. #endif
  585. #ifdef HAVE_ED25519
  586. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed25519_test(void);
  587. #endif
  588. #ifdef HAVE_CURVE448
  589. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve448_test(void);
  590. #endif
  591. #ifdef HAVE_ED448
  592. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed448_test(void);
  593. #endif
  594. #ifdef WOLFSSL_HAVE_KYBER
  595. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t kyber_test(void);
  596. #endif
  597. #if defined(WOLFSSL_HAVE_XMSS)
  598. #if !defined(WOLFSSL_SMALL_STACK) && WOLFSSL_XMSS_MIN_HEIGHT <= 10
  599. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t xmss_test_verify_only(void);
  600. #endif
  601. #if !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  602. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t xmss_test(void);
  603. #endif
  604. #endif
  605. #if defined(WOLFSSL_HAVE_LMS)
  606. #if !defined(WOLFSSL_SMALL_STACK)
  607. #if (defined(WOLFSSL_WC_LMS) && (LMS_MAX_HEIGHT >= 10)) || \
  608. defined(HAVE_LIBLMS)
  609. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t lms_test_verify_only(void);
  610. #endif
  611. #endif
  612. #if !defined(WOLFSSL_LMS_VERIFY_ONLY)
  613. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t lms_test(void);
  614. #endif
  615. #endif
  616. #ifdef WOLFCRYPT_HAVE_ECCSI
  617. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t eccsi_test(void);
  618. #endif
  619. #ifdef WOLFCRYPT_HAVE_SAKKE
  620. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sakke_test(void);
  621. #endif
  622. #ifdef HAVE_BLAKE2
  623. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2b_test(void);
  624. #endif
  625. #ifdef HAVE_BLAKE2S
  626. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2s_test(void);
  627. #endif
  628. #ifdef HAVE_LIBZ
  629. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t compress_test(void);
  630. #endif
  631. #ifdef HAVE_PKCS7
  632. #ifndef NO_PKCS7_ENCRYPTED_DATA
  633. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7encrypted_test(void);
  634. #endif
  635. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  636. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7compressed_test(void);
  637. #endif
  638. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7signed_test(void);
  639. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7enveloped_test(void);
  640. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  641. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7authenveloped_test(void);
  642. #endif
  643. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  644. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7callback_test(byte* cert, word32 certSz, byte* key,
  645. word32 keySz);
  646. #endif
  647. #endif
  648. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  649. !defined(NO_FILESYSTEM)
  650. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cert_test(void);
  651. #endif
  652. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  653. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  654. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certext_test(void);
  655. #endif
  656. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  657. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  658. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t decodedCertCache_test(void);
  659. #endif
  660. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memory_test(void);
  661. #if defined(WOLFSSL_PUBLIC_MP) && \
  662. ((defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  663. defined(USE_FAST_MATH))
  664. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mp_test(void);
  665. #endif
  666. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  667. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t prime_test(void);
  668. #endif
  669. #if defined(ASN_BER_TO_DER) && \
  670. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  671. defined(OPENSSL_EXTRA_X509_SMALL))
  672. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t berder_test(void);
  673. #endif
  674. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t logging_test(void);
  675. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  676. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t time_test(void);
  677. #endif
  678. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  679. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t wolfcrypt_mutex_test(void);
  680. #else
  681. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mutex_test(void);
  682. #endif
  683. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  684. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memcb_test(void);
  685. #endif
  686. #ifdef WOLFSSL_CAAM_BLOB
  687. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blob_test(void);
  688. #endif
  689. #ifdef HAVE_ARIA
  690. #include "wolfssl/wolfcrypt/port/aria/aria-crypt.h"
  691. void printOutput(const char *strName, unsigned char *data, unsigned int dataSz);
  692. WOLFSSL_TEST_SUBROUTINE int ariagcm_test(MC_ALGID);
  693. #endif
  694. #ifdef WOLF_CRYPTO_CB
  695. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cryptocb_test(void);
  696. #endif
  697. #ifdef WOLFSSL_CERT_PIV
  698. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certpiv_test(void);
  699. #endif
  700. #ifdef WOLFSSL_AES_SIV
  701. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_siv_test(void);
  702. #endif
  703. #if defined(WOLFSSL_AES_EAX) && \
  704. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  705. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_eax_test(void);
  706. #endif /* WOLFSSL_AES_EAX */
  707. /* General big buffer size for many tests. */
  708. #define FOURK_BUF 4096
  709. /* If not defined in user_settings, the ERROR_OUT pause is 120 seconds. */
  710. #ifndef WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION
  711. #define WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION 120
  712. #endif
  713. #if defined(WOLFSSL_ESPIDF_ERROR_PAUSE)
  714. #if defined(CONFIG_FREERTOS_HZ)
  715. #define WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION_TICKS \
  716. (WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION * CONFIG_FREERTOS_HZ)
  717. #else
  718. /* If not defined, assume RTOS is 1000 ticks per second. */
  719. #define WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION_TICKS \
  720. (WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION * 1000)
  721. #endif
  722. /* When defined, pause at error condition rather than exit with error. */
  723. #define ERROR_OUT(err, eLabel) \
  724. do { \
  725. ret = (err); \
  726. ESP_LOGE(ESPIDF_TAG, "Failed: Error = %d during %s, line %d", \
  727. err, __FUNCTION__, __LINE__); \
  728. ESP_LOGI(ESPIDF_TAG, "Extended system info:"); \
  729. esp_ShowExtendedSystemInfo(); \
  730. ESP_LOGW(ESPIDF_TAG, "Paused for %d seconds! " \
  731. "WOLFSSL_ESPIDF_ERROR_PAUSE is enabled.", \
  732. WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION); \
  733. vTaskDelay(WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION_TICKS); \
  734. goto eLabel; \
  735. } while (0)
  736. #else
  737. #define ERROR_OUT(err, eLabel) do { ret = (err); goto eLabel; } while (0)
  738. #endif
  739. /* Not all unexpected conditions are actually errors .*/
  740. #define WARNING_OUT(err, eLabel) do { ret = (err); goto eLabel; } while (0)
  741. static void render_error_message(const char* msg, wc_test_ret_t es)
  742. {
  743. (void)msg;
  744. (void)es;
  745. #ifdef WOLFSSL_LINUXKM
  746. #define err_sys_printf lkm_printf
  747. #else
  748. #define err_sys_printf printf
  749. #endif
  750. switch (WC_TEST_RET_DEC_TAG(es)) {
  751. case WC_TEST_RET_TAG_NC:
  752. err_sys_printf("%s error L=%d\n", msg, WC_TEST_RET_DEC_LN(es));
  753. break;
  754. case WC_TEST_RET_TAG_EC:
  755. #ifdef NO_ERROR_STRINGS
  756. err_sys_printf("%s error L=%d code=%d\n", msg,
  757. WC_TEST_RET_DEC_LN(es), -WC_TEST_RET_DEC_I(es));
  758. #else
  759. err_sys_printf("%s error L=%d code=%d (%s)\n", msg,
  760. WC_TEST_RET_DEC_LN(es), -WC_TEST_RET_DEC_I(es),
  761. wc_GetErrorString(-WC_TEST_RET_DEC_I(es)));
  762. #endif
  763. break;
  764. case WC_TEST_RET_TAG_ERRNO:
  765. {
  766. /* strerror_r() comes in two mutually incompatible flavors, a native glibc
  767. * flavor that always returns a non-null char pointer that must be used
  768. * directly, and a POSIX flavor that returns an error int, and iff success,
  769. * stores an error string in the supplied buffer. this is all most
  770. * infelicitous...
  771. */
  772. #if !defined(STRING_USER) && !defined(NO_ERROR_STRINGS) && \
  773. (defined(__STDC_VERSION__) && (__STDC_VERSION__ > 199901L)) && \
  774. ((defined(__GLIBC__) && (__GLIBC__ >= 2)) || \
  775. (defined(__USE_XOPEN2K) && \
  776. defined(_POSIX_C_SOURCE) && \
  777. (_POSIX_C_SOURCE >= 200112L)))
  778. char errno_buf[64], *errno_string;
  779. /* precisely mirror the gate used in glibc string.h */
  780. #if defined __USE_XOPEN2K && !defined __USE_GNU
  781. if (strerror_r(WC_TEST_RET_DEC_I(es),
  782. errno_buf, sizeof(errno_buf)) != 0)
  783. XSTRLCPY(errno_buf, "?", sizeof(errno_buf));
  784. errno_string = errno_buf;
  785. #else
  786. errno_string = strerror_r(WC_TEST_RET_DEC_I(es),
  787. errno_buf, sizeof(errno_buf));
  788. #endif
  789. err_sys_printf("%s error L=%d errno=%d (%s)\n", msg,
  790. WC_TEST_RET_DEC_LN(es), WC_TEST_RET_DEC_I(es),
  791. errno_string);
  792. #else /* can't figure out how to strerror_r(), or don't want error strings */
  793. err_sys_printf("%s error L=%d errno=%d\n", msg,
  794. WC_TEST_RET_DEC_LN(es), WC_TEST_RET_DEC_I(es));
  795. #endif
  796. break;
  797. }
  798. case WC_TEST_RET_TAG_I:
  799. err_sys_printf("%s error L=%d i=%d\n", msg,
  800. WC_TEST_RET_DEC_LN(es), WC_TEST_RET_DEC_I(es));
  801. break;
  802. }
  803. #undef err_sys_printf
  804. }
  805. static void print_fiducials(void);
  806. #ifdef HAVE_STACK_SIZE
  807. static THREAD_RETURN err_sys(const char* msg, int es)
  808. #else
  809. static wc_test_ret_t err_sys(const char* msg, wc_test_ret_t es)
  810. #endif
  811. {
  812. render_error_message(msg, es);
  813. print_fiducials();
  814. #ifdef WOLFSSL_LINUXKM
  815. EXIT_TEST(es);
  816. #else
  817. EXIT_TEST(-1);
  818. #endif
  819. }
  820. #ifndef HAVE_WOLFCRYPT_TEST_OPTIONS
  821. /* func_args from test.h, so don't have to pull in other stuff */
  822. typedef struct func_args {
  823. int argc;
  824. char** argv;
  825. wc_test_ret_t return_code;
  826. } func_args;
  827. #endif /* !HAVE_WOLFCRYPT_TEST_OPTIONS */
  828. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  829. static void myFipsCb(int ok, int err, const char* hash)
  830. {
  831. printf("in my Fips callback, ok = %d, err = %d\n", ok, err);
  832. printf("message = %s\n", wc_GetErrorString(err));
  833. printf("hash = %s\n", hash);
  834. if (err == IN_CORE_FIPS_E) {
  835. printf("In core integrity hash check failure, copy above hash\n");
  836. printf("into verifyCore[] in fips_test.c and rebuild\n");
  837. }
  838. }
  839. #endif /* HAVE_FIPS && !WOLFSSL_LINUXKM */
  840. #ifdef WOLFSSL_STATIC_MEMORY
  841. #if defined(WOLFSSL_STATIC_MEMORY_TEST_SZ)
  842. static byte gTestMemory[WOLFSSL_STATIC_MEMORY_TEST_SZ];
  843. #elif defined(BENCH_EMBEDDED)
  844. static byte gTestMemory[14000];
  845. #elif defined(WOLFSSL_CERT_EXT)
  846. static byte gTestMemory[140000];
  847. #elif (defined(WOLFSSL_SP_MATH_ALL) || defined(USE_FAST_MATH)) && \
  848. !defined(ALT_ECC_SIZE)
  849. static byte gTestMemory[160000];
  850. #else
  851. static byte gTestMemory[80000];
  852. #endif
  853. #endif
  854. #ifdef WOLFSSL_PB
  855. static int wolfssl_pb_print(const char* msg, ...)
  856. {
  857. int ret;
  858. va_list args;
  859. char tmpBuf[80];
  860. va_start(args, msg);
  861. ret = vsprint(tmpBuf, msg, args);
  862. va_end(args);
  863. fnDumpStringToSystemLog(tmpBuf);
  864. return ret;
  865. }
  866. #endif /* WOLFSSL_PB */
  867. #if defined(WOLF_CRYPTO_CB) && !defined(HAVE_HASHDRBG) && \
  868. !defined(WC_NO_RNG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  869. /* Enable support for RNG with crypto callback */
  870. static int rng_crypto_cb(int thisDevId, wc_CryptoInfo* info, void* ctx)
  871. {
  872. int rc = CRYPTOCB_UNAVAILABLE;
  873. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  874. rc = wc_GenerateSeed(&info->rng.rng->seed, info->rng.out, info->rng.sz);
  875. }
  876. (void)ctx;
  877. (void)thisDevId;
  878. return rc;
  879. }
  880. #endif
  881. #if defined(WC_KDF_NIST_SP_800_56C)
  882. #define INIT_SP80056C_TEST_VECTOR(_z, _fixedInfo, _derivedKey, _hashType) \
  883. { \
  884. .z = (const byte*)_z, .zSz = sizeof(_z) - 1, \
  885. .fixedInfo = (const byte*)_fixedInfo, \
  886. .fixedInfoSz = sizeof(_fixedInfo) - 1, \
  887. .derivedKey = (const byte*)_derivedKey, \
  888. .derivedKeySz = sizeof(_derivedKey) - 1, .hashType = _hashType, \
  889. }
  890. #define SP800_56C_MAX_OUT 128
  891. static WOLFSSL_TEST_SUBROUTINE wc_test_ret_t nist_sp80056c_kdf_test(void)
  892. {
  893. struct sp800_56c_test_vector {
  894. const byte* z;
  895. word32 zSz;
  896. const byte* fixedInfo;
  897. word32 fixedInfoSz;
  898. const byte* derivedKey;
  899. word32 derivedKeySz;
  900. enum wc_HashType hashType;
  901. };
  902. struct sp800_56c_test_vector* v;
  903. byte output[SP800_56C_MAX_OUT];
  904. word32 i;
  905. int ret;
  906. /* vectors from
  907. * https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/example-values
  908. *
  909. * (KeyManagement) */
  910. struct sp800_56c_test_vector vctors[] = {
  911. #if !defined(NO_SHA)
  912. /* SHA-1 */
  913. INIT_SP80056C_TEST_VECTOR(
  914. "\xad\x42\x01\x82\x63\x3f\x85\x26\xbf\xe9\x54\xac\xda\x37\x6f\x05"
  915. "\xe5\xff\x4f\x83\x7f\x54\xfe\xbe\x0f\xb1\x2a\x1b\x3b\xeb\xf2\x63"
  916. "\xee\x21\x64\x13\xed\x06\xa8\x4a\x12\xeb\x51\x11\x59\xf1\x33\x7d",
  917. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  918. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  919. "\x6f\xef\x44\x2f\xc1\x7a\x7e\x2b\x0c\x9d\xec\xe0\xe4\x7a\x57\x48"
  920. "\xac\xb4\x6a\xf1\x98\xd7\x67\x47\x0f\x28\xa1\x04\xb5\x61\x30\xae"
  921. "\xb0\x10\x09\xa4\x56\x82\xa5\xe1",
  922. WC_HASH_TYPE_SHA),
  923. INIT_SP80056C_TEST_VECTOR(
  924. "\xae\x64\xab\x2b\x2b\x75\xa9\x4c\xf8\xef\x24\xda\x24\x56\xbd\x3a"
  925. "\xa3\x6d\xb6\x14\x29\xea\x55\x21",
  926. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  927. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  928. "\xcc\x96\x5a\x52\xd0\x5c\x94\x9e\x52\xc0\x35\xfd\x03\x53\x0d\xb7"
  929. "\xea\xa4\x08\x70\x2c\x9d\x35\x21\x1e\x67\x21\x54\x12\x45\x91\x51"
  930. "\xba\x22\x62\xbd\x1e\x28\xe5\x6b",
  931. WC_HASH_TYPE_SHA),
  932. INIT_SP80056C_TEST_VECTOR(
  933. "\x71\x5d\xc0\xea\x24\x6b\x46\x56\x63\xa8\x9c\xde\x04\x12\xc1\x92"
  934. "\xe1\x9e\x58\xd5\xb0\xb6\x36\x6d\xa7\x89\xad\xbf\x44\x9a\x38\xaa"
  935. "\x46\x69\xfe\x36\x30\xa2\x0f\x7f\xa3\x14\x9c\x9b\x4b\x0a\xb5\xcd"
  936. "\x3e\x14\x18\x2b\x75\x04\xd5\xd2\x75\x2b\xf6\x58\x7a\xab\xc9\xf4"
  937. "\xcb\x8f\xe5\x29\x23\x6a\xb8\x15\x36\xad\xd2\xbd\x25\xd6\xbf\x9d"
  938. "\x5f\x1d\xf5\x76\x16\x5a\xa5\x5c\x24\x99\x61\xd8\xf8\x75\x00\xed"
  939. "\x8d\xbf\xc5\xd2\x50\x53\x4c\x07\xd9\x9a\xc9\x17\xf9\x84\x60\x46"
  940. "\xac\x5c\xb8\xa2\x98\x74\x26\x22\xd3\xc9\x86\x18\x06\x92\x46\xe8"
  941. "\xad\x37\x11\x25\x57\xe0\xe6\x34\xb5\x81\x32\x7a\x4a\xd3\x2c\x7c"
  942. "\x76\x4b\xe8\xf8\x08\x0d\x37\x2c\x63\x20\x93\xa7\x67\xf1\x55\xbd"
  943. "\x22\xec\x00\x3c\xa6\x1c\x8b\x43\x32\x0f\x3a\xbe\xb5\xdd\xc4\xa3"
  944. "\xb1\x89\x82\xfd\xd6\x51\x0f\x88\x3c\x8d\xc1\xe0\xb1\x57\xff\xb9"
  945. "\xcf\xc0\xa5\x9c\xe2\xd4\x05\x5f\xfc\x73\xe7\x15\x2a\x6a\x95\x43"
  946. "\xb9\x19\xe7\x94\xe9\x49\x61\x33\xbe\x2a\x23\x18\xd9\x05\x6e\xfd"
  947. "\x74\x48\x2d\xc6\x3c\x0d\xb5\x8f\xe6\x42\x6b\x0f\xe7\x35\x45\x42"
  948. "\xc2\x19\x7a\xb6\xbd\x35\xf1\xa9\x2d\xce\x90\xb1\xc4\x6d\x32\xc1",
  949. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  950. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  951. "\x1b\x5f\xcc\x8d\x81\xa5\xd9\xa9\x36\x94\xe6\x47\x77\x88\xd8\x03"
  952. "\x15\x84\xc3\xf5\x2e\x9f\x11\x7a\xe8\x18\x4d\xba\x56\x47\x9d\x87"
  953. "\x66\x76\x92\xf1\x2d\x7b\xd3\x8b",
  954. WC_HASH_TYPE_SHA),
  955. INIT_SP80056C_TEST_VECTOR(
  956. "\x33\xe0\x50\xbd\x20\x9f\x2d\xf2\x77\x19\x78\xfc\xd1\xd4\xc8\x2e"
  957. "\x49\xd0\x1d\x65\xbb\x62\x03\x20\xd3\x0b\xfe\xa8\x7a\xa8\x69\xe1"
  958. "\x07\xa5\x17\xa4\xc8\x5b\x69\x28\x45\x21\xca\x54\xb7\x7f\x59\xe9"
  959. "\x4a\x85\x6d\xaa\x30\xa3\x85\xa5\x25\xd8\xa3\xf7\xe1\x5e\xe5\xe9"
  960. "\xaa\x12\x8d\x45\xef\x63\xf9\x0c\x10\xe0\x8f\xc5\x26\x36\x13\x77"
  961. "\x81\x54\x7a\x58\x9f\x97\x87\xf9\xd7\xdd\x61\x43\x41\x9a\x26\x16"
  962. "\x80\x16\x82\x40\xaa\xb2\x01\x3d\x80\x20\xdb\xe8\x4b\x7e\x2b\xed"
  963. "\xce\x67\x1b\x94\x03\xbd\x1f\x91\x71\xa2\x57\x90\xce\x66\x7d\xed",
  964. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  965. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  966. "\x35\x10\x85\x85\xfc\x55\x62\xb5\xf1\x8f\x20\x7b\xa8\x35\x63\xe4"
  967. "\x49\x28\x45\x6a\x5e\x53\x63\x7e\xae\x6b\xb5\xfc\x33\x88\xae\x02"
  968. "\x91\x79\x0a\x1d\xc8\x31\x4e\x28",
  969. WC_HASH_TYPE_SHA),
  970. INIT_SP80056C_TEST_VECTOR(
  971. "\x5c\x80\x4f\x45\x4d\x30\xd9\xc4\xdf\x85\x27\x1f\x93\x52\x8c\x91"
  972. "\xdf\x6b\x48\xab\x5f\x80\xb3\xb5\x9c\xaa\xc1\xb2\x8f\x8a\xcb\xa9"
  973. "\xcd\x3e\x39\xf3\xcb\x61\x45\x25\xd9\x52\x1d\x2e\x64\x4c\x53\xb8"
  974. "\x07\xb8\x10\xf3\x40\x06\x2f\x25\x7d\x7d\x6f\xbf\xe8\xd5\xe8\xf0"
  975. "\x72\xe9\xb6\xe9\xaf\xda\x94\x13\xea\xfb\x2e\x8b\x06\x99\xb1\xfb"
  976. "\x5a\x0c\xac\xed\xde\xae\xad\x7e\x9c\xfb\xb3\x6a\xe2\xb4\x20\x83"
  977. "\x5b\xd8\x3a\x19\xfb\x0b\x5e\x96\xbf\x8f\xa4\xd0\x9e\x34\x55\x25"
  978. "\x16\x7e\xcd\x91\x55\x41\x6f\x46\xf4\x08\xed\x31\xb6\x3c\x6e\x6d",
  979. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  980. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  981. "\xfa\xa0\x22\xce\x7f\xa9\xba\x95\xeb\xa3\x9f\x3f\x44\xf3\xee\x14"
  982. "\x96\x0a\x0b\x23\x9d\x01\x4b\x57\x70\xe4\x71\xd7\x5a\x99\xea\x87"
  983. "\x10\xe3\x8f\x0c\xef\x0f\xfc\x67",
  984. WC_HASH_TYPE_SHA),
  985. INIT_SP80056C_TEST_VECTOR(
  986. "\xad\x42\x01\x82\x63\x3f\x85\x26\xbf\xe9\x54\xac\xda\x37\x6f\x05"
  987. "\xe5\xff\x4f\x83\x7f\x54\xfe\xbe\x0f\xb1\x2a\x1b\x3b\xeb\xf2\x63"
  988. "\xee\x21\x64\x13\xed\x06\xa8\x4a\x12\xeb\x51\x11\x59\xf1\x33\x7d",
  989. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  990. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  991. "\x6f\xef\x44\x2f\xc1\x7a\x7e\x2b\x0c\x9d\xec\xe0\xe4\x7a\x57\x48"
  992. "\xac\xb4\x6a\xf1\x98\xd7\x67\x47\x0f\x28\xa1\x04\xb5\x61\x30\xae"
  993. "\xb0\x10\x09\xa4\x56\x82\xa5\xe1",
  994. WC_HASH_TYPE_SHA),
  995. #endif
  996. #if defined(WOLFSSL_SHA224)
  997. /* SHA-224*/
  998. INIT_SP80056C_TEST_VECTOR(
  999. "\x8c\x5d\x6e\x5d\x36\x06\x83\xba\x55\xb0\x9d\xb1\x69\x6d\x7c\x64"
  1000. "\x02\xff\x87\x88\x5f\xf5\x07\x70\xf2\x76\x7b\x75\x54\x60\x20\x7e"
  1001. "\xd5\xc7\x43\xfd\x27\xe7\xeb\x1d\x0c\xa5\x91\xf8\x56\x38\x93\x11"
  1002. "\x73\x07\x44\xf2\x04\xd2\xe5\x5b\x8b\xd4\x46\xce\xca\x03\x1f\x7b"
  1003. "\xac\xcf\xf1\xa7\x1b\x68\x34\x59\xcc\x54\xd5\x01\xda\xbf\x4a\x84"
  1004. "\xcd\xb8\x6d\xfa\xda\xff\x31\x0f\xbd\xba\xf7\x4d\xd5\x1b\xa1\xe1"
  1005. "\xe1\x19\x1a\xf1\x4c\x9b\xf8\x94\x43\xbf\x58\x8e\x9c\xe3\x30\x34"
  1006. "\xaf\x5e\x89\xbf\x6f\xfc\x47\xd7\xd9\xca\x4a\x5e\x8f\xf8\xa0\x50"
  1007. "\x20\xbb\x0f\x95\xbc\xde\x01\x56\xd8\x7f\xb8\x60\xbd\x40\x83\xfa"
  1008. "\x5b\x53\x1a\x08\xa4\xfb\x7e\xe0\x20\x1a\xe8\xb3\xcc\xff\xe9\x9f"
  1009. "\x27\x0b\xc3\x53\x4b\xaa\xcf\xc0\x01\xcd\xd8\x0a\xd8\x7c\xce\x71"
  1010. "\xf0\x91\xe7\x66\xca\x5c\xc2\x75\xcb\x49\x14\x5a\x5e\xe6\x16\x2e"
  1011. "\xcc\xf5\x58\xce\xc4\xd3\xee\x53\x1e\x91\xe9\xa5\x29\x69\x63\x4d"
  1012. "\x3a\xf8\xd2\x6f\x8d\x15\xdc\x0d\x6f\x6e\x0a\x97\x4b\xe4\x34\x1b"
  1013. "\x68\xa0\x19\x90\xdb\xb8\x64\x95\x89\x1a\xd3\xaf\xc1\xe4\xce\xdf"
  1014. "\x4c\x6a\xe1\xf1\xcd\x60\x81\xcd\xee\xd8\xe6\xb3\x26\x4e\xc3\xbe"
  1015. "\x24\x58\xd1\xc0\xca\x24\x43\x41\x0c\xf3\xb4\x7c\x0c\x25\x4c\x7d"
  1016. "\xc8\xec\xb4\x3e\x6c\x23\x64\xe1\xc0\x62\x19\xcc\x7e\xfb\xff\xbb"
  1017. "\x63\xd7\xff\xfc\x74\x58\x12\xfd\x24\x0c\x33\xd4\x96\xb9\x99\x2f"
  1018. "\x96\x80\xa6\x3c\x07\x96\x3c\x0c\x49\xf3\xc1\xba\xef\xec\xaf\x32"
  1019. "\xe2\xaa\x8a\x2f\x7c\xd3\x0d\x8f\x05\x1e\xe2\xf5\x0f\xbf\x05\xab"
  1020. "\x13\x96\xa4\xea\x87\x44\x7d\x7b\x98\x1b\x5e\x46\x14\x28\x18\x71"
  1021. "\xa6\xf0\xf6\xbf\x1f\xe0\x02\x2f\x7e\xa1\x32\xbe\x0a\xe9\x19\x26"
  1022. "\xab\x12\xaf\x6d\xc4\x50\x64\xaa\xd5\x6b\x84\xb9\x0c\x70\x08\x37"
  1023. "\x09\xcf\xf7\xe3\x1b\x54\x8f\xb7\xfb\x2c\xf7\x5a\xbf\x96\xe0\x1c"
  1024. "\xcd\x3e\x94\x2e\xed\x91\x48\x0d\x4c\x24\xc6\xb7\xf9\x79\xfb\xbe"
  1025. "\x5d\xa2\x39\xb3\x76\x16\x7d\x68\x57\x35\x24\xff\xcb\x50\x99\x54"
  1026. "\xcc\x80\xa0\xe1\xa7\x1c\x40\xc4\xda\x17\xb8\xd1\x57\x2b\x21\x58"
  1027. "\x7a\x8d\x66\xcc\x62\x1c\x7c\xd1\x0f\x49\xab\xd5\xef\x86\x31\x13"
  1028. "\xe6\x19\x21\x08\x6f\xac\x25\x31\x2b\x74\x1c\x11\xa8\xfb\xc1\xe3"
  1029. "\x3c\x34\xd9\xda\x14\xa8\x22\x47\x7f\xcc\x36\x66\x70\x25\xc4\xf1"
  1030. "\x30\xae\x10\x0e\x36\xf1\x5d\xa0\x03\x74\xce\x87\x41\x67\x9f\x61",
  1031. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  1032. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  1033. "\x4b\x99\x6a\x60\xc0\x4a\x35\xc5\xe6\xd4\x74\xb1\x0a\x25\x8d\x56"
  1034. "\x2e\xa6\xdc\x52\xf6\xc6\x9b\xf3\x9e\xf8\x8c\x89\xe3\xcc\x8a\x54"
  1035. "\xda\x2f\x3c\x0b\x56\x1b\x53\xfe\x76\x55\x13\x63\xd6\x9c\x3c\xef"
  1036. "\x74\xe3\x4f\xe8\x8e\xb3\xac\x51",
  1037. WC_HASH_TYPE_SHA224),
  1038. INIT_SP80056C_TEST_VECTOR(
  1039. "\x52\x27\x2f\x50\xf4\x6f\x4e\xdc\x91\x51\x56\x90\x92\xf4\x6d\xf2"
  1040. "\xd9\x6e\xcc\x3b\x6d\xc1\x71\x4a\x4e\xa9\x49\xfa\x9f\x18\xff\x54"
  1041. "\xf8\x87\x23\x07\x3f\x64\xa6\x95\x3d\x04\x91\x4f\x45\xa2\x3e\xee"
  1042. "\x7c\xfc\x46\x67\x08\x0a\xa0\xf9",
  1043. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  1044. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  1045. "\x77\x56\xbc\xfd\xef\x3e\xe6\x9f\x6a\xc2\x3c\xd2\xdc\x60\x7d\x01"
  1046. "\xfa\x8c\xe1\xb2\x4f\x5c\xaa\xaa\x48\xe0\x4b\x81\x63\xe1\x73\x3a"
  1047. "\xed\x7a\x04\x0e\x73\xf2\xb5\x42\x36\x8f\x00\x54\x8b\x16\x3c\x3d"
  1048. "\xc9\x6d\x70\x09\x99\x16\xf1\x6b",
  1049. WC_HASH_TYPE_SHA224),
  1050. #endif
  1051. #if !defined(NO_SHA256)
  1052. /* SHA-256 */
  1053. INIT_SP80056C_TEST_VECTOR(
  1054. "\xdd\x0f\x53\x96\x21\x9d\x1e\xa3\x93\x31\x04\x12\xd1\x9a\x08\xf1"
  1055. "\xf5\x81\x1e\x9d\xc8\xec\x8e\xea\x7f\x80\xd2\x1c\x82\x0c\x27\x88"
  1056. "\x22\x76\x84\xe7\x1f\x5c\x31\x3f\xad\xc9\x1e\x52\x98\x07\xe3\x14"
  1057. "\x7d\x53\x14\x5b\x15\xab\xd6\xed\x41\x6a\xd3\x5c\xd7\xe6\x83\x8f",
  1058. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  1059. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  1060. "\xc0\x8b\x3d\xe2\x4f\x1a\x38\x1e\x7a\x56\x75\xa2\xa6\x52\x3b\x08"
  1061. "\xf3\x54\x60\x5e\xee\x46\xb9\xf3\x9e\xad\xb1\xe9\x75\x34\x41\x6d"
  1062. "\x98\xb4\x3c\xae\x8a\xb0\x4a\xfd\x53\xde\xb3\x7f\x44\x02\x23\x52"
  1063. "\xc3\xfb\xde\x1e\x2f\x2c\xec\x53\x1c\xfc\x32\x4f\xdd\x0f\xcc\xa6",
  1064. WC_HASH_TYPE_SHA256),
  1065. INIT_SP80056C_TEST_VECTOR(
  1066. "\x44\xf4\x84\x09\xf3\x1b\xf3\x50\x94\x51\xdb\x4d\x30\x4b\xeb\xd8"
  1067. "\x3c\x2a\xd6\x50\x1c\x1b\x85\xe6\x32\xbc\x92\x58\x8e\x2d\x48\xb5"
  1068. "\xd2\xb8\x44\x44\x62\xad\x94\xe8\xa4\x44\x94\x1e\xd4\x97\x5c\x97"
  1069. "\x91\x17\xd7\x5a\x0a\x2b\xa8\x10\xdf\xa8\x80\x4a\x0f\xe9\x42\x6d"
  1070. "\xd7\xeb\x95\xf2\x9c\xa4\x30\xda\x37\xef\xa5\x2c\x42\xda\x1d\xe2"
  1071. "\x23\x76\x3b\xdd\xc9\x5e\x46\x6a\xa3\xb8\xd2\x06\xb8\x21\x8e\xdf"
  1072. "\x23\x97\x3d\x05\xf3\xc7\xc2\x2b\x22\x46\x53\xd4\xf9\x21\x85\x45"
  1073. "\x79\x83\x26\x2f\x27\xbc\x55\xa0\xa7\xae\xe5\x43\x54\x55\xd4\x3c"
  1074. "\x0e\x0c\x6d\x80\x67\xad\xaf\x90\x42\xb6\xb7\x77\x7e\x19\x8d\x67"
  1075. "\x60\x83\x0d\x96\xb2\x9a\x6a\xae\xf5\x74\xd7\x5e\x2d\x65\x43\x55"
  1076. "\x0f\x75\x55\xcf\x89\x82\xc8\x21\xc4\xec\x9b\x82\x66\x2b\x09\x36"
  1077. "\x69\xfd\x24\x6d\x4d\xcc\xbf\xf3\x1d\x98\x4e\xf6\x3c\x4f\x95\xd7"
  1078. "\x39\xc7\x7e\x66\xf6\x9b\x6d\xb0\xd9\xfc\x1a\x97\x3f\x52\x33\xd1"
  1079. "\x1f\xe7\x15\x4b\x8a\x6b\xe1\x50\x46\x7d\x92\xda\xf0\x91\x44\x34"
  1080. "\xf0\xcf\x17\x6e\x96\x1b\x31\x3f\xe3\xe2\x3b\xfe\x37\x8b\x87\xba"
  1081. "\xf2\x73\x93\x7f\xfa\xa6\x85\x65\x8b\x09\x78\x1d\x26\x97\x86\x4b"
  1082. "\x0d\x82\x48\x5c\xf7\xd6\x2f\xfc\xea\x34\x33\x88\xcf\xec\x15\x27"
  1083. "\x3a\x39\x4b\x84\x32\xee\xb6\x32\x1b\xe3\x1d\x4f\xcd\x16\x15\xc8"
  1084. "\x1c\x69\x37\xc0\x8d\x92\x41\x6e\xdb\xdd\x20\x10\xfa\x8b\x6e\x0a"
  1085. "\x8a\x60\x3d\xad\xe0\x10\xbc\x9c\xd7\x6b\x14\xe3\xe2\x19\x0e\x3b"
  1086. "\xa3\x00\xe7\x1a\xdd\xbf\x24\x4c\xf8\x06\x29\x49\x76\xea\xf0\x7d"
  1087. "\x02\x3b\xdb\x57\xfc\x5d\x19\x64\xc7\xd4\x20\x6e\x72\x06\x1b\xfe"
  1088. "\x1e\xe4\xee\xbc\x92\x00\xe1\x3a\x6c\xba\x32\x86\x9d\xbe\x80\x82"
  1089. "\xcd\xf3\x64\x5b\x5a\x72\x7d\xd2\x7c\xa5\x03\xf4\xed\xeb\x73\xe8"
  1090. "\x8a\x3a\x55\x2c\x7e\x00\xd4\xee\x72\x42\x13\x72\x36\xa0\x96\x5c"
  1091. "\x1e\xc3\xeb\xc0\xb4\x8c\x2b\x46\x7e\xb8\x42\x41\x5a\x28\x3f\x55"
  1092. "\xe2\x20\xff\xd1\x88\x19\x25\x6d\xa2\x47\x4d\x28\xfc\x3b\x04\xe0"
  1093. "\xc0\x7e\x4d\x25\xc1\x74\x93\x41\xd2\x22\x97\x01\x5c\xd8\x17\x8c"
  1094. "\x39\x18\xbe\x8c\x5c\xdf\x0f\xbf\xbb\x9a\x5a\xcc\xdd\x82\xaf\x07"
  1095. "\x83\xef\xe4\xdf\x64\xa8\xd8\x92\x82\x8f\x8d\xe5\x8c\x5d\x56\x9b"
  1096. "\x5b\x08\x45\x58\x96\xc4\xd3\xc3\x4f\xd3\xce\x93\xc4\x34\xc3\x8e"
  1097. "\xf5\x6c\xed\x30\x56\x1c\x37\x1a\xf9\xf2\xd8\x64\xfd\xc5\xb6\x2f",
  1098. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  1099. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  1100. "\x42\x35\xac\x89\xc9\xf3\x3e\x5d\xbb\x11\x60\x1a\x29\x83\xe3\x76"
  1101. "\xe2\x15\x39\x97\x87\xd8\x39\xa0\x38\x55\xfe\x04\x53\x3e\x1a\x67"
  1102. "\x66\x91\x5e\x67\xc2\x5e\xeb\x04\x08\x28\xe9\x6c\xd3\xd6\xef\x0a"
  1103. "\xb7\xd1\x7b\x43\x13\x43\xa7\xf3\xaa\xc6\x8f\x0c\x4a\x7e\x77\x9b",
  1104. WC_HASH_TYPE_SHA256),
  1105. /* slightly modified vector to test generation when derivedKeySz %
  1106. * hashOutSz != 0*/
  1107. INIT_SP80056C_TEST_VECTOR(
  1108. "\xdd\x0f\x53\x96\x21\x9d\x1e\xa3\x93\x31\x04\x12\xd1\x9a\x08\xf1"
  1109. "\xf5\x81\x1e\x9d\xc8\xec\x8e\xea\x7f\x80\xd2\x1c\x82\x0c\x27\x88"
  1110. "\x22\x76\x84\xe7\x1f\x5c\x31\x3f\xad\xc9\x1e\x52\x98\x07\xe3\x14"
  1111. "\x7d\x53\x14\x5b\x15\xab\xd6\xed\x41\x6a\xd3\x5c\xd7\xe6\x83\x8f",
  1112. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  1113. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  1114. "\xc0\x8b\x3d\xe2\x4f\x1a\x38\x1e\x7a\x56\x75\xa2\xa6\x52\x3b\x08"
  1115. "\xf3\x54\x60\x5e\xee\x46\xb9\xf3\x9e\xad\xb1\xe9\x75\x34\x41\x6d"
  1116. "\x98\xb4\x3c\xae\x8a\xb0\x4a\xfd\x53\xde\xb3\x7f\x44\x02\x23\x52"
  1117. "\xc3\xfb\xde\x1e\x2f\x2c\xec\x53\x1c\xfc\x32\x4f\xdd\x0f\xcc",
  1118. WC_HASH_TYPE_SHA256),
  1119. #endif
  1120. #if defined(WOLFSSL_SHA384)
  1121. /* SHA-384 */
  1122. INIT_SP80056C_TEST_VECTOR(
  1123. "\x5e\xa1\xfc\x4a\xf7\x25\x6d\x20\x55\x98\x1b\x11\x05\x75\xe0\xa8"
  1124. "\xca\xe5\x31\x60\x13\x7d\x90\x4c\x59\xd9\x26\xeb\x1b\x84\x56\xe4"
  1125. "\x27\xaa\x8a\x45\x40\x88\x4c\x37\xde\x15\x9a\x58\x02\x8a\xbc\x0e"
  1126. "\x88\x76\x9c\xb7\x2f\xc5\xac\x45\x7c\xd5\x8e\x89\x08\x9b\x19\x6a"
  1127. "\x70\xbf\x53\x3c\x6d\xc9\x1c\x9c\x7e\x17\x41\xdb\x5e\x7a\xb6\xb0"
  1128. "\x84\x9f\x01\xde\xa6\x5f\xed\xd0\x6c\x77\x18\x7c\xd8\x8e\xd0\x30",
  1129. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  1130. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  1131. "\x8e\x6e\x26\x5f\x20\x82\xf1\x4d\x34\xda\x23\xe1\x03\x2c\x90\x24"
  1132. "\x83\x4a\xf0\x15\x72\xb6\x64\x77\x82\x41\x1b\xdd\xcb\x84\xa5\xda"
  1133. "\xee\x11\x7b\xa6\xfb\xa6\xd0\xeb\x28\x08\xef\x8a\xb0\x70\x05\xee"
  1134. "\xab\xe5\x2d\x2e\xfd\x31\x12\x1c\x7b\xf9\xd5\xfa\xfc\x40\xe0\x0c"
  1135. "\x6d\x6d\xbf\x39\xef\x43\xfe\x97\x15\xc7\x20\x2c\xdc\x2d\xb7\xe8"
  1136. "\x2b\x88\xd7\x48\xeb\x84\x25\x8b\xf8\x4d\x85\x82\xf2\xbf\xd9\x40",
  1137. WC_HASH_TYPE_SHA384),
  1138. #endif
  1139. #if defined(WOLFSSL_SHA512)
  1140. /* SHA-512 */
  1141. INIT_SP80056C_TEST_VECTOR(
  1142. "\x00\xcd\xea\x89\x62\x1c\xfa\x46\xb1\x32\xf9\xe4\xcf\xe2\x26\x1c"
  1143. "\xde\x2d\x43\x68\xeb\x56\x56\x63\x4c\x7c\xc9\x8c\x7a\x00\xcd\xe5"
  1144. "\x4e\xd1\x86\x6a\x0d\xd3\xe6\x12\x6c\x9d\x2f\x84\x5d\xaf\xf8\x2c"
  1145. "\xeb\x1d\xa0\x8f\x5d\x87\x52\x1b\xb0\xeb\xec\xa7\x79\x11\x16\x9c"
  1146. "\x20\xcc\x01\x38\xa6\x72\xb6\x95\x8b\xd7\x84\xe5\xd7\xfa\x83\x73"
  1147. "\x8a\xc6\x8f\x9b\x34\x23\xb4\x83\xf9\xbf\x53\x9e\x71\x14\x1e\x45"
  1148. "\xdb\xfb\x7a\xfe\xd1\x8b\x11\xc0\x02\x8b\x13\xf1\xf8\x60\xef\x43"
  1149. "\xc4\x80\xf4\xda\xcd\xa2\x08\x10\x59\xd3\x97\x8c\x99\x9d\x5d\x1a"
  1150. "\xde\x34\x54\xe4",
  1151. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  1152. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  1153. "\x2d\x4a\x46\xa1\x70\x99\xba\xa8\x33\x0b\xc5\x9d\x4a\x1c\xf5\xae"
  1154. "\x3a\x30\x75\xb4\xc6\x2b\xb2\x6e\x7f\xc9\x89\x24\x72\x6d\x27\x4c"
  1155. "\x09\x64\x6f\x44\x08\xe6\x85\x8c\x43\xb4\x2d\xae\xd0\x15\xef\x26"
  1156. "\x17\x08\xd5\x5e\xf2\x4d\xaa\x7d\x3e\xa3\xd1\xc4\xa0\x8c\xfd\x24"
  1157. "\xdb\x60\x00\xa5\xb8\xa6\x7d\xe7\x46\xf3\xd3\xf4\xff\x34\x85\x15"
  1158. "\x8f\xd3\xb6\x91\x55\x79\x1d\xf4\x67\x47\xd4\xdb\xbe\x17\xc4\xb5"
  1159. "\x58\x46\x2e\x26\xbe\x5e\xd3\x5f\xe6\x80\xe2\x97\x14\x22\xc3\xb0"
  1160. "\x1b\x17\xe1\x67\xfc\x43\x7f\x84\x86\x9d\x85\x49\x53\x7b\x33\x38",
  1161. WC_HASH_TYPE_SHA512),
  1162. #endif
  1163. INIT_SP80056C_TEST_VECTOR(
  1164. "\x00\xcd\xea\x89\x62\x1c\xfa\x46\xb1\x32\xf9\xe4\xcf\xe2\x26\x1c"
  1165. "\xde\x2d\x43\x68\xeb\x56\x56\x63\x4c\x7c\xc9\x8c\x7a\x00\xcd\xe5"
  1166. "\x4e\xd1\x86\x6a\x0d\xd3\xe6\x12\x6c\x9d\x2f\x84\x5d\xaf\xf8\x2c"
  1167. "\xeb\x1d\xa0\x8f\x5d\x87\x52\x1b\xb0\xeb\xec\xa7\x79\x11\x16\x9c"
  1168. "\x20\xcc\x01\x38\xa6\x72\xb6\x95\x8b\xd7\x84\xe5\xd7\xfa\x83\x73"
  1169. "\x8a\xc6\x8f\x9b\x34\x23\xb4\x83\xf9\xbf\x53\x9e\x71\x14\x1e\x45"
  1170. "\xdb\xfb\x7a\xfe\xd1\x8b\x11\xc0\x02\x8b\x13\xf1\xf8\x60\xef\x43"
  1171. "\xc4\x80\xf4\xda\xcd\xa2\x08\x10\x59\xd3\x97\x8c\x99\x9d\x5d\x1a"
  1172. "\xde\x34\x54\xe4",
  1173. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  1174. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  1175. "\x2d\x4a",
  1176. WC_HASH_TYPE_SHA512),
  1177. };
  1178. for (i = 0; i < sizeof(vctors) / sizeof(vctors[0]); i++) {
  1179. v = &vctors[i];
  1180. ret = wc_KDA_KDF_onestep(v->z, v->zSz, v->fixedInfo, v->fixedInfoSz,
  1181. v->derivedKeySz, v->hashType, output,
  1182. /* use derivedKeySz to force the function to use a temporary buff
  1183. for the last block */
  1184. v->derivedKeySz);
  1185. if (ret != 0)
  1186. return WC_TEST_RET_ENC_EC(ret);
  1187. if (XMEMCMP(output, v->derivedKey, v->derivedKeySz) != 0)
  1188. return WC_TEST_RET_ENC_NC;
  1189. }
  1190. /* negative tests */
  1191. ret = wc_KDA_KDF_onestep(NULL, 0, (byte*)"fixed_info",
  1192. sizeof("fixed_info"), 16, WC_HASH_TYPE_SHA256, output, 16);
  1193. if (ret != BAD_FUNC_ARG)
  1194. return WC_TEST_RET_ENC_NC;
  1195. ret = wc_KDA_KDF_onestep((byte*)"secret", sizeof("secret"), NULL, 1, 16,
  1196. WC_HASH_TYPE_SHA256, output, 16);
  1197. if (ret != BAD_FUNC_ARG)
  1198. return WC_TEST_RET_ENC_NC;
  1199. /* allow empty FixedInfo */
  1200. ret = wc_KDA_KDF_onestep((byte*)"secret", sizeof("secret"), NULL, 0, 16,
  1201. WC_HASH_TYPE_SHA256, output, 16);
  1202. if (ret != 0)
  1203. return WC_TEST_RET_ENC_EC(ret);
  1204. return 0;
  1205. }
  1206. #endif /* WC_KDF_NIST_SP_800_56C */
  1207. /* optional macro to add sleep between tests */
  1208. #ifndef TEST_SLEEP
  1209. #define TEST_SLEEP() WC_DO_NOTHING
  1210. #else
  1211. #define TEST_PASS test_pass
  1212. #include <stdarg.h> /* for var args */
  1213. static WC_INLINE void test_pass(const char* fmt, ...)
  1214. {
  1215. va_list args;
  1216. va_start(args, fmt);
  1217. STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max_relative_stack, vprintf(fmt, args));
  1218. va_end(args);
  1219. PRINT_HEAP_CHECKPOINT();
  1220. TEST_SLEEP();
  1221. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1););
  1222. }
  1223. #endif
  1224. /* set test pass output to printf if not overridden */
  1225. #ifndef TEST_PASS
  1226. /* redirect to printf */
  1227. #define TEST_PASS(...) { \
  1228. if (STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK \
  1229. (max_relative_stack, printf(__VA_ARGS__)) < 0) { \
  1230. return err_sys("post-test check failed", WC_TEST_RET_ENC_NC);\
  1231. } \
  1232. PRINT_HEAP_CHECKPOINT(); \
  1233. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1);); \
  1234. }
  1235. #endif
  1236. #ifdef TEST_ALWAYS_RUN_TO_END
  1237. #define TEST_FAIL(msg, retval) do { last_failed_test_ret = (retval); render_error_message(msg, retval); } while (0)
  1238. #elif !defined(TEST_FAIL)
  1239. #define TEST_FAIL(msg, retval) return err_sys(msg, retval)
  1240. #endif
  1241. #ifdef HAVE_STACK_SIZE
  1242. THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args)
  1243. #else
  1244. wc_test_ret_t wolfcrypt_test(void* args)
  1245. #endif
  1246. {
  1247. wc_test_ret_t ret;
  1248. #if defined(WOLFSSL_TRACK_MEMORY_VERBOSE) && !defined(WOLFSSL_STATIC_MEMORY)
  1249. long heap_baselineAllocs, heap_baselineBytes;
  1250. #endif
  1251. #ifdef TEST_ALWAYS_RUN_TO_END
  1252. int last_failed_test_ret = 0;
  1253. #endif
  1254. STACK_SIZE_INIT();
  1255. #if defined(WOLFSSL_TRACK_MEMORY_VERBOSE) && !defined(WOLFSSL_STATIC_MEMORY)
  1256. (void)wolfCrypt_heap_peakAllocs_checkpoint();
  1257. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint();
  1258. (void)wolfCrypt_heap_peakBytes_checkpoint();
  1259. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint();
  1260. #endif
  1261. #ifdef WC_RNG_SEED_CB
  1262. wc_SetSeed_Cb(wc_GenerateSeed);
  1263. #endif
  1264. printf("------------------------------------------------------------------------------\n");
  1265. printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING);
  1266. #ifdef WOLF_CRYPTO_CB
  1267. if (devId != INVALID_DEVID)
  1268. printf(" CryptoCB with DevID:%X\n", devId);
  1269. #endif
  1270. printf("------------------------------------------------------------------------------\n");
  1271. if (args) {
  1272. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  1273. int ch;
  1274. #endif
  1275. ((func_args*)args)->return_code = -1; /* error state */
  1276. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  1277. while ((ch = mygetopt(((func_args*)args)->argc, ((func_args*)args)->argv, "s:m:a:h")) != -1) {
  1278. switch(ch) {
  1279. case 's':
  1280. #ifdef HAVE_STACK_SIZE_VERBOSE
  1281. max_relative_stack = (ssize_t)atoi(myoptarg);
  1282. break;
  1283. #else
  1284. return err_sys("-s (max relative stack bytes) requires HAVE_STACK_SIZE_VERBOSE (--enable-stacksize=verbose).", WC_TEST_RET_ENC_NC);
  1285. #endif
  1286. case 'm':
  1287. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  1288. max_relative_heap_bytes = (ssize_t)atoi(myoptarg);
  1289. break;
  1290. #else
  1291. return err_sys("-m (max relative heap memory bytes) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", WC_TEST_RET_ENC_NC);
  1292. #endif
  1293. case 'a':
  1294. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  1295. max_relative_heap_allocs = (ssize_t)atoi(myoptarg);
  1296. break;
  1297. #else
  1298. return err_sys("-a (max relative heap allocs) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", WC_TEST_RET_ENC_NC);
  1299. #endif
  1300. case 'h':
  1301. return err_sys("\
  1302. options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
  1303. [-a max_relative_heap_allocs] [-h]\n", 0);
  1304. default:
  1305. return err_sys("unknown test option. try -h.", WC_TEST_RET_ENC_NC);
  1306. }
  1307. }
  1308. #endif
  1309. }
  1310. #ifdef WOLFSSL_STATIC_MEMORY
  1311. if (wc_LoadStaticMemory(&HEAP_HINT, gTestMemory, sizeof(gTestMemory),
  1312. WOLFMEM_GENERAL, 1) != 0) {
  1313. printf("unable to load static memory.\n");
  1314. return(EXIT_FAILURE);
  1315. }
  1316. #ifndef OPENSSL_EXTRA
  1317. wolfSSL_SetGlobalHeapHint(HEAP_HINT);
  1318. #endif
  1319. #endif
  1320. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  1321. wolfSSL_Debugging_ON();
  1322. #endif
  1323. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  1324. wc_SetLoggingHeap(HEAP_HINT);
  1325. #endif
  1326. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  1327. wolfCrypt_SetCb_fips(myFipsCb);
  1328. #if FIPS_VERSION3_GE(6,0,0)
  1329. printf("FIPS module version in use: %s\n",
  1330. wolfCrypt_GetVersion_fips());
  1331. #endif
  1332. #endif
  1333. #if !defined(NO_BIG_INT)
  1334. if (CheckCtcSettings() != 1) {
  1335. printf("Sizeof mismatch (build) %x != (run) %lx\n",
  1336. CTC_SETTINGS, (unsigned long)CheckRunTimeSettings());
  1337. return err_sys("Build vs runtime math mismatch\n", WC_TEST_RET_ENC_NC);
  1338. }
  1339. #if defined(USE_FAST_MATH) && \
  1340. (!defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC))
  1341. if (CheckFastMathSettings() != 1)
  1342. return err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n",
  1343. WC_TEST_RET_ENC_NC);
  1344. #endif /* USE_FAST_MATH */
  1345. #endif /* !NO_BIG_INT */
  1346. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  1347. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  1348. initDefaultName();
  1349. #endif
  1350. #ifdef WOLFSSL_ASYNC_CRYPT
  1351. ret = wolfAsync_DevOpen(&devId);
  1352. if (ret < 0) {
  1353. printf("Async device open failed\nRunning without async\n");
  1354. }
  1355. #else
  1356. (void)devId;
  1357. #endif /* WOLFSSL_ASYNC_CRYPT */
  1358. #ifdef WOLF_CRYPTO_CB
  1359. #ifdef HAVE_INTEL_QA_SYNC
  1360. devId = wc_CryptoCb_InitIntelQa();
  1361. if (INVALID_DEVID == devId) {
  1362. printf("Couldn't init the Intel QA\n");
  1363. }
  1364. #endif
  1365. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  1366. devId = wc_CryptoCb_InitOcteon();
  1367. if (INVALID_DEVID == devId) {
  1368. printf("Couldn't init the Cavium Octeon\n");
  1369. }
  1370. #endif
  1371. #ifdef HAVE_RENESAS_SYNC
  1372. devId = wc_CryptoCb_CryptInitRenesasCmn(NULL, &guser_PKCbInfo);
  1373. if (devId == INVALID_DEVID) {
  1374. printf("Couldn't get the Renesas device ID\n");
  1375. }
  1376. #endif
  1377. #endif
  1378. #if defined(WOLF_CRYPTO_CB) && !defined(HAVE_HASHDRBG) && \
  1379. !defined(WC_NO_RNG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  1380. if (devId == INVALID_DEVID) {
  1381. /* for testing RNG with crypto callback register function */
  1382. devId = 100; /* any value beside -2 (INVALID_DEVID) */
  1383. wc_CryptoCb_RegisterDevice(devId, rng_crypto_cb, NULL);
  1384. }
  1385. #endif
  1386. #ifdef HAVE_SELFTEST
  1387. if ( (ret = wolfCrypt_SelfTest()) != 0)
  1388. TEST_FAIL("CAVP selftest failed!\n", ret);
  1389. else
  1390. TEST_PASS("CAVP selftest passed!\n");
  1391. #endif
  1392. if ( (ret = error_test()) != 0)
  1393. TEST_FAIL("error test failed!\n", ret);
  1394. else
  1395. TEST_PASS("error test passed!\n");
  1396. if ( (ret = memory_test()) != 0)
  1397. TEST_FAIL("MEMORY test failed!\n", ret);
  1398. else
  1399. TEST_PASS("MEMORY test passed!\n");
  1400. #ifndef NO_CODING
  1401. if ( (ret = base64_test()) != 0)
  1402. TEST_FAIL("base64 test failed!\n", ret);
  1403. else
  1404. TEST_PASS("base64 test passed!\n");
  1405. #ifdef WOLFSSL_BASE16
  1406. if ( (ret = base16_test()) != 0)
  1407. TEST_FAIL("base16 test failed!\n", ret);
  1408. else
  1409. TEST_PASS("base16 test passed!\n");
  1410. #endif
  1411. #endif /* !NO_CODING */
  1412. #ifndef NO_ASN
  1413. if ( (ret = asn_test()) != 0)
  1414. TEST_FAIL("asn test failed!\n", ret);
  1415. else
  1416. TEST_PASS("asn test passed!\n");
  1417. #endif
  1418. #ifndef WC_NO_RNG
  1419. if ( (ret = random_test()) != 0)
  1420. TEST_FAIL("RANDOM test failed!\n", ret);
  1421. else
  1422. TEST_PASS("RANDOM test passed!\n");
  1423. #endif /* WC_NO_RNG */
  1424. #ifndef NO_MD5
  1425. if ( (ret = md5_test()) != 0)
  1426. TEST_FAIL("MD5 test failed!\n", ret);
  1427. else
  1428. TEST_PASS("MD5 test passed!\n");
  1429. #endif
  1430. #ifdef WOLFSSL_MD2
  1431. if ( (ret = md2_test()) != 0)
  1432. TEST_FAIL("MD2 test failed!\n", ret);
  1433. else
  1434. TEST_PASS("MD2 test passed!\n");
  1435. #endif
  1436. #ifndef NO_MD4
  1437. if ( (ret = md4_test()) != 0)
  1438. TEST_FAIL("MD4 test failed!\n", ret);
  1439. else
  1440. TEST_PASS("MD4 test passed!\n");
  1441. #endif
  1442. #ifndef NO_SHA
  1443. if ( (ret = sha_test()) != 0)
  1444. TEST_FAIL("SHA test failed!\n", ret);
  1445. else
  1446. TEST_PASS("SHA test passed!\n");
  1447. #endif
  1448. #ifdef WOLFSSL_SHA224
  1449. if ( (ret = sha224_test()) != 0)
  1450. TEST_FAIL("SHA-224 test failed!\n", ret);
  1451. else
  1452. TEST_PASS("SHA-224 test passed!\n");
  1453. #endif
  1454. #ifndef NO_SHA256
  1455. if ( (ret = sha256_test()) != 0)
  1456. TEST_FAIL("SHA-256 test failed!\n", ret);
  1457. else
  1458. TEST_PASS("SHA-256 test passed!\n");
  1459. #endif
  1460. #ifdef WOLFSSL_SHA384
  1461. if ( (ret = sha384_test()) != 0)
  1462. TEST_FAIL("SHA-384 test failed!\n", ret);
  1463. else
  1464. TEST_PASS("SHA-384 test passed!\n");
  1465. #endif
  1466. #ifdef WOLFSSL_SHA512
  1467. if ((ret = sha512_test()) != 0) {
  1468. TEST_FAIL("SHA-512 test failed!\n", ret);
  1469. }
  1470. else {
  1471. TEST_PASS("SHA-512 test passed!\n");
  1472. }
  1473. #if !defined(WOLFSSL_NOSHA512_224) && \
  1474. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  1475. if ((ret = sha512_224_test()) != 0) {
  1476. TEST_FAIL("SHA-512/224 test failed!\n", ret);
  1477. }
  1478. else
  1479. TEST_PASS("SHA-512/224 test passed!\n");
  1480. #endif /* !defined(WOLFSSL_NOSHA512_224) && !FIPS ... */
  1481. #if !defined(WOLFSSL_NOSHA512_256) && \
  1482. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  1483. if ((ret = sha512_256_test()) != 0) {
  1484. TEST_FAIL("SHA-512/256 test failed!\n", ret);
  1485. }
  1486. else
  1487. TEST_PASS("SHA-512/256 test passed!\n");
  1488. #endif /* !defined(WOLFSSL_NOSHA512_256) & !FIPS ... */
  1489. #endif /* WOLFSSL_SHA512 */
  1490. #ifdef WOLFSSL_SHA3
  1491. if ( (ret = sha3_test()) != 0)
  1492. TEST_FAIL("SHA-3 test failed!\n", ret);
  1493. else
  1494. TEST_PASS("SHA-3 test passed!\n");
  1495. #endif
  1496. #ifdef WOLFSSL_SHAKE128
  1497. if ( (ret = shake128_test()) != 0)
  1498. TEST_FAIL("SHAKE128 test failed!\n", ret);
  1499. else
  1500. TEST_PASS("SHAKE128 test passed!\n");
  1501. #endif
  1502. #ifdef WOLFSSL_SHAKE256
  1503. if ( (ret = shake256_test()) != 0)
  1504. TEST_FAIL("SHAKE256 test failed!\n", ret);
  1505. else
  1506. TEST_PASS("SHAKE256 test passed!\n");
  1507. #endif
  1508. #ifdef WOLFSSL_SM3
  1509. if ( (ret = sm3_test()) != 0)
  1510. return err_sys("SM-3 test failed!\n", ret);
  1511. else
  1512. TEST_PASS("SM-3 test passed!\n");
  1513. #endif
  1514. #ifndef NO_HASH_WRAPPER
  1515. if ( (ret = hash_test()) != 0)
  1516. TEST_FAIL("Hash test failed!\n", ret);
  1517. else
  1518. TEST_PASS("Hash test passed!\n");
  1519. #endif
  1520. #ifdef WOLFSSL_RIPEMD
  1521. if ( (ret = ripemd_test()) != 0)
  1522. TEST_FAIL("RIPEMD test failed!\n", ret);
  1523. else
  1524. TEST_PASS("RIPEMD test passed!\n");
  1525. #endif
  1526. #ifdef HAVE_BLAKE2
  1527. if ( (ret = blake2b_test()) != 0)
  1528. TEST_FAIL("BLAKE2b test failed!\n", ret);
  1529. else
  1530. TEST_PASS("BLAKE2b test passed!\n");
  1531. #endif
  1532. #ifdef HAVE_BLAKE2S
  1533. if ( (ret = blake2s_test()) != 0)
  1534. TEST_FAIL("BLAKE2s test failed!\n", ret);
  1535. else
  1536. TEST_PASS("BLAKE2s test passed!\n");
  1537. #endif
  1538. #ifndef NO_HMAC
  1539. #if !defined(NO_MD5) && !(defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) \
  1540. && (HAVE_FIPS_VERSION >= 5))
  1541. if ( (ret = hmac_md5_test()) != 0)
  1542. TEST_FAIL("HMAC-MD5 test failed!\n", ret);
  1543. else
  1544. TEST_PASS("HMAC-MD5 test passed!\n");
  1545. #endif
  1546. #ifndef NO_SHA
  1547. if ( (ret = hmac_sha_test()) != 0)
  1548. TEST_FAIL("HMAC-SHA test failed!\n", ret);
  1549. else
  1550. TEST_PASS("HMAC-SHA test passed!\n");
  1551. #endif
  1552. #ifdef WOLFSSL_SHA224
  1553. if ( (ret = hmac_sha224_test()) != 0)
  1554. TEST_FAIL("HMAC-SHA224 test failed!\n", ret);
  1555. else
  1556. TEST_PASS("HMAC-SHA224 test passed!\n");
  1557. #endif
  1558. #ifndef NO_SHA256
  1559. if ( (ret = hmac_sha256_test()) != 0)
  1560. TEST_FAIL("HMAC-SHA256 test failed!\n", ret);
  1561. else
  1562. TEST_PASS("HMAC-SHA256 test passed!\n");
  1563. #endif
  1564. #ifdef WOLFSSL_SHA384
  1565. if ( (ret = hmac_sha384_test()) != 0)
  1566. TEST_FAIL("HMAC-SHA384 test failed!\n", ret);
  1567. else
  1568. TEST_PASS("HMAC-SHA384 test passed!\n");
  1569. #endif
  1570. #ifdef WOLFSSL_SHA512
  1571. if ( (ret = hmac_sha512_test()) != 0)
  1572. TEST_FAIL("HMAC-SHA512 test failed!\n", ret);
  1573. else
  1574. TEST_PASS("HMAC-SHA512 test passed!\n");
  1575. #endif
  1576. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  1577. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  1578. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  1579. if ( (ret = hmac_sha3_test()) != 0)
  1580. TEST_FAIL("HMAC-SHA3 test failed!\n", ret);
  1581. else
  1582. TEST_PASS("HMAC-SHA3 test passed!\n");
  1583. #endif
  1584. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  1585. PRIVATE_KEY_UNLOCK();
  1586. if ( (ret = hkdf_test()) != 0)
  1587. TEST_FAIL("HMAC-KDF test failed!\n", ret);
  1588. else
  1589. TEST_PASS("HMAC-KDF test passed!\n");
  1590. PRIVATE_KEY_LOCK();
  1591. #endif
  1592. #endif /* !NO_HMAC */
  1593. #ifdef WOLFSSL_WOLFSSH
  1594. PRIVATE_KEY_UNLOCK();
  1595. if ( (ret = sshkdf_test()) != 0)
  1596. TEST_FAIL("SSH-KDF test failed!\n", ret);
  1597. else
  1598. TEST_PASS("SSH-KDF test passed!\n");
  1599. PRIVATE_KEY_LOCK();
  1600. #endif /* WOLFSSL_WOLFSSH */
  1601. #if defined(WOLFSSL_HAVE_PRF) && !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  1602. PRIVATE_KEY_UNLOCK();
  1603. if ( (ret = prf_test()) != 0)
  1604. TEST_FAIL("PRF test failed!\n", ret);
  1605. else
  1606. TEST_PASS("PRF test passed!\n");
  1607. PRIVATE_KEY_LOCK();
  1608. #endif
  1609. #ifdef WOLFSSL_HAVE_PRF
  1610. #if defined (HAVE_HKDF) && !defined(NO_HMAC)
  1611. #ifdef WOLFSSL_BASE16
  1612. PRIVATE_KEY_UNLOCK();
  1613. if ( (ret = tls12_kdf_test()) != 0)
  1614. TEST_FAIL("TLSv1.2 KDF test failed!\n", ret);
  1615. else
  1616. TEST_PASS("TLSv1.2 KDF test passed!\n");
  1617. PRIVATE_KEY_LOCK();
  1618. #endif /* WOLFSSL_BASE16 */
  1619. #endif /* WOLFSSL_HAVE_HKDF && !NO_HMAC */
  1620. #endif /* WOLFSSL_HAVE_PRF */
  1621. #ifdef WOLFSSL_TLS13
  1622. PRIVATE_KEY_UNLOCK();
  1623. if ( (ret = tls13_kdf_test()) != 0)
  1624. TEST_FAIL("TLSv1.3 KDF test failed!\n", ret);
  1625. else
  1626. TEST_PASS("TLSv1.3 KDF test passed!\n");
  1627. PRIVATE_KEY_LOCK();
  1628. #endif /* WOLFSSL_TLS13 */
  1629. #if defined(HAVE_X963_KDF) && defined(HAVE_ECC)
  1630. if ( (ret = x963kdf_test()) != 0)
  1631. TEST_FAIL("X963-KDF test failed!\n", ret);
  1632. else
  1633. TEST_PASS("X963-KDF test passed!\n");
  1634. #endif
  1635. #if defined(HAVE_HPKE) && defined(HAVE_ECC) && defined(HAVE_AESGCM)
  1636. if ( (ret = hpke_test()) != 0)
  1637. TEST_FAIL("HPKE test failed!\n", ret);
  1638. else
  1639. TEST_PASS("HPKE test passed!\n");
  1640. #endif
  1641. #if defined(WC_SRTP_KDF)
  1642. PRIVATE_KEY_UNLOCK();
  1643. if ( (ret = srtpkdf_test()) != 0)
  1644. TEST_FAIL("SRTP KDF test failed!\n", ret);
  1645. else
  1646. TEST_PASS("SRTP KDF test passed!\n");
  1647. PRIVATE_KEY_LOCK();
  1648. #endif
  1649. #if defined(WC_KDF_NIST_SP_800_56C)
  1650. if ( (ret = nist_sp80056c_kdf_test()) != 0)
  1651. TEST_FAIL("NIST SP 800-56C KDF test failed!\n", ret);
  1652. else
  1653. TEST_PASS("NIST SP 800-56C KDF test passed!\n");
  1654. #endif
  1655. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128) && \
  1656. !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  1657. !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
  1658. if ( (ret = gmac_test()) != 0)
  1659. TEST_FAIL("GMAC test failed!\n", ret);
  1660. else
  1661. TEST_PASS("GMAC test passed!\n");
  1662. #endif
  1663. #ifdef WC_RC2
  1664. if ( (ret = rc2_test()) != 0)
  1665. TEST_FAIL("RC2 test failed!\n", ret);
  1666. else
  1667. TEST_PASS("RC2 test passed!\n");
  1668. #endif
  1669. #ifndef NO_RC4
  1670. if ( (ret = arc4_test()) != 0)
  1671. TEST_FAIL("ARC4 test failed!\n", ret);
  1672. else
  1673. TEST_PASS("ARC4 test passed!\n");
  1674. #endif
  1675. #ifdef HAVE_CHACHA
  1676. if ( (ret = chacha_test()) != 0)
  1677. TEST_FAIL("Chacha test failed!\n", ret);
  1678. else
  1679. TEST_PASS("Chacha test passed!\n");
  1680. #endif
  1681. #ifdef HAVE_XCHACHA
  1682. if ( (ret = XChaCha_test()) != 0)
  1683. TEST_FAIL("XChacha test failed!\n", ret);
  1684. else
  1685. TEST_PASS("XChacha test passed!\n");
  1686. #endif
  1687. #ifdef HAVE_POLY1305
  1688. if ( (ret = poly1305_test()) != 0)
  1689. TEST_FAIL("POLY1305 test failed!\n", ret);
  1690. else
  1691. TEST_PASS("POLY1305 test passed!\n");
  1692. #endif
  1693. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  1694. if ( (ret = chacha20_poly1305_aead_test()) != 0)
  1695. TEST_FAIL("ChaCha20-Poly1305 AEAD test failed!\n", ret);
  1696. else
  1697. TEST_PASS("ChaCha20-Poly1305 AEAD test passed!\n");
  1698. #endif
  1699. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  1700. if ( (ret = XChaCha20Poly1305_test()) != 0)
  1701. TEST_FAIL("XChaCha20-Poly1305 AEAD test failed!\n", ret);
  1702. else
  1703. TEST_PASS("XChaCha20-Poly1305 AEAD test passed!\n");
  1704. #endif
  1705. #ifndef NO_DES3
  1706. if ( (ret = des_test()) != 0)
  1707. TEST_FAIL("DES test failed!\n", ret);
  1708. else
  1709. TEST_PASS("DES test passed!\n");
  1710. #endif
  1711. #ifndef NO_DES3
  1712. if ( (ret = des3_test()) != 0)
  1713. TEST_FAIL("DES3 test failed!\n", ret);
  1714. else
  1715. TEST_PASS("DES3 test passed!\n");
  1716. #endif
  1717. #ifndef NO_AES
  1718. if ( (ret = aes_test()) != 0)
  1719. TEST_FAIL("AES test failed!\n", ret);
  1720. else
  1721. TEST_PASS("AES test passed!\n");
  1722. #if defined(WOLFSSL_AES_192) && \
  1723. !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
  1724. if ( (ret = aes192_test()) != 0)
  1725. TEST_FAIL("AES192 test failed!\n", ret);
  1726. else
  1727. TEST_PASS("AES192 test passed!\n");
  1728. #endif
  1729. #if defined(WOLFSSL_AES_256)
  1730. if ( (ret = aes256_test()) != 0)
  1731. TEST_FAIL("AES256 test failed!\n", ret);
  1732. else
  1733. TEST_PASS("AES256 test passed!\n");
  1734. #endif
  1735. #ifdef WOLFSSL_AES_OFB
  1736. if ( (ret = aesofb_test()) != 0)
  1737. TEST_FAIL("AES-OFB test failed!\n", ret);
  1738. else
  1739. TEST_PASS("AES-OFB test passed!\n");
  1740. #endif
  1741. #ifdef HAVE_AESGCM
  1742. #if !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO)
  1743. if ( (ret = aesgcm_test()) != 0)
  1744. TEST_FAIL("AES-GCM test failed!\n", ret);
  1745. #endif
  1746. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  1747. !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY) && \
  1748. !defined(WOLFSSL_KCAPI_AES) && !(defined(WOLF_CRYPTO_CB) && \
  1749. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  1750. if ((ret = aesgcm_default_test()) != 0) {
  1751. TEST_FAIL("AES-GCM test failed!\n", ret);
  1752. }
  1753. #endif
  1754. if (ret == 0) {
  1755. TEST_PASS("AES-GCM test passed!\n");
  1756. }
  1757. #endif
  1758. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  1759. if ( (ret = aesccm_test()) != 0)
  1760. TEST_FAIL("AES-CCM test failed!\n", ret);
  1761. else
  1762. TEST_PASS("AES-CCM test passed!\n");
  1763. #endif
  1764. #ifdef WOLFSSL_AES_CFB
  1765. if ( (ret = aes_cfb_test()) != 0)
  1766. TEST_FAIL("AES-CFB test failed!\n", ret);
  1767. else
  1768. TEST_PASS("AES-CFB test passed!\n");
  1769. #endif
  1770. #ifdef WOLFSSL_AES_XTS
  1771. if ( (ret = aes_xts_test()) != 0)
  1772. TEST_FAIL("AES-XTS test failed!\n", ret);
  1773. else
  1774. TEST_PASS("AES-XTS test passed!\n");
  1775. #endif
  1776. #ifdef HAVE_AES_KEYWRAP
  1777. if ( (ret = aeskeywrap_test()) != 0)
  1778. TEST_FAIL("AES Key Wrap test failed!\n", ret);
  1779. else
  1780. TEST_PASS("AES Key Wrap test passed!\n");
  1781. #endif
  1782. #ifdef WOLFSSL_AES_SIV
  1783. if ( (ret = aes_siv_test()) != 0)
  1784. TEST_FAIL("AES-SIV test failed!\n", ret);
  1785. else
  1786. TEST_PASS("AES-SIV test passed!\n");
  1787. #endif
  1788. #endif
  1789. #if defined(WOLFSSL_AES_EAX) && \
  1790. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  1791. if ( (ret = aes_eax_test()) != 0)
  1792. TEST_FAIL("AES-EAX test failed!\n", ret);
  1793. else
  1794. TEST_PASS("AES-EAX test passed!\n");
  1795. #endif /* WOLFSSL_AES_EAX */
  1796. #ifdef HAVE_ARIA
  1797. if ( (ret = ariagcm_test(MC_ALGID_ARIA_128BITKEY)) != 0)
  1798. TEST_FAIL("ARIA128 test failed!\n", ret);
  1799. else
  1800. TEST_PASS("ARIA128 test passed!\n");
  1801. if ( (ret = ariagcm_test(MC_ALGID_ARIA_192BITKEY)) != 0)
  1802. TEST_FAIL("ARIA192 test failed!\n", ret);
  1803. else
  1804. TEST_PASS("ARIA192 test passed!\n");
  1805. if ( (ret = ariagcm_test(MC_ALGID_ARIA_256BITKEY)) != 0)
  1806. TEST_FAIL("ARIA256 test failed!\n", ret);
  1807. else
  1808. TEST_PASS("ARIA256 test passed!\n");
  1809. #endif
  1810. #ifdef HAVE_CAMELLIA
  1811. if ( (ret = camellia_test()) != 0)
  1812. TEST_FAIL("CAMELLIA test failed!\n", ret);
  1813. else
  1814. TEST_PASS("CAMELLIA test passed!\n");
  1815. #endif
  1816. #ifdef WOLFSSL_SM4
  1817. if ( (ret = sm4_test()) != 0)
  1818. return err_sys("SM-4 test failed!\n", ret);
  1819. else
  1820. TEST_PASS("SM-4 test passed!\n");
  1821. #endif
  1822. #if !defined(NO_RSA) && !defined(HAVE_RENESAS_SYNC)
  1823. #ifdef WC_RSA_NO_PADDING
  1824. if ( (ret = rsa_no_pad_test()) != 0)
  1825. TEST_FAIL("RSA NOPAD test failed!\n", ret);
  1826. else
  1827. TEST_PASS("RSA NOPAD test passed!\n");
  1828. #endif
  1829. if ( (ret = rsa_test()) != 0)
  1830. TEST_FAIL("RSA test failed!\n", ret);
  1831. else
  1832. TEST_PASS("RSA test passed!\n");
  1833. #endif
  1834. #ifndef NO_DH
  1835. PRIVATE_KEY_UNLOCK();
  1836. if ( (ret = dh_test()) != 0)
  1837. TEST_FAIL("DH test failed!\n", ret);
  1838. else
  1839. TEST_PASS("DH test passed!\n");
  1840. PRIVATE_KEY_LOCK();
  1841. #endif
  1842. #ifndef NO_DSA
  1843. if ( (ret = dsa_test()) != 0)
  1844. TEST_FAIL("DSA test failed!\n", ret);
  1845. else
  1846. TEST_PASS("DSA test passed!\n");
  1847. #endif
  1848. #ifdef WOLFCRYPT_HAVE_SRP
  1849. if ( (ret = srp_test()) != 0)
  1850. TEST_FAIL("SRP test failed!\n", ret);
  1851. else
  1852. TEST_PASS("SRP test passed!\n");
  1853. #endif
  1854. #ifndef NO_PWDBASED
  1855. PRIVATE_KEY_UNLOCK();
  1856. if ( (ret = pwdbased_test()) != 0)
  1857. TEST_FAIL("PWDBASED test failed!\n", ret);
  1858. else
  1859. TEST_PASS("PWDBASED test passed!\n");
  1860. PRIVATE_KEY_LOCK();
  1861. #endif
  1862. #if defined(USE_CERT_BUFFERS_2048) && \
  1863. defined(HAVE_PKCS12) && \
  1864. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_HMAC) && \
  1865. !defined(NO_CERTS) && !defined(NO_DES3)
  1866. if ( (ret = pkcs12_test()) != 0)
  1867. TEST_FAIL("PKCS12 test failed!\n", ret);
  1868. else
  1869. TEST_PASS("PKCS12 test passed!\n");
  1870. #endif
  1871. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  1872. if ( (ret = openssl_test()) != 0)
  1873. TEST_FAIL("OPENSSL test failed!\n", ret);
  1874. else
  1875. TEST_PASS("OPENSSL test passed!\n");
  1876. if ( (ret = openSSL_evpMD_test()) != 0)
  1877. TEST_FAIL("OPENSSL (EVP MD) test failed!\n", ret);
  1878. else
  1879. TEST_PASS("OPENSSL (EVP MD) passed!\n");
  1880. if ( (ret = openssl_pkey0_test()) != 0)
  1881. TEST_FAIL("OPENSSL (PKEY0) test failed!\n", ret);
  1882. else
  1883. TEST_PASS("OPENSSL (PKEY0) passed!\n");
  1884. if ( (ret = openssl_pkey1_test()) != 0)
  1885. TEST_FAIL("OPENSSL (PKEY1) test failed!\n", ret);
  1886. else
  1887. TEST_PASS("OPENSSL (PKEY1) passed!\n");
  1888. #if !defined(WOLF_CRYPTO_CB_ONLY_RSA) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1889. if ( (ret = openssl_evpSig_test()) != 0)
  1890. TEST_FAIL("OPENSSL (EVP Sign/Verify) test failed!\n", ret);
  1891. else
  1892. TEST_PASS("OPENSSL (EVP Sign/Verify) passed!\n");
  1893. #endif
  1894. #endif
  1895. #if defined(HAVE_ECC)
  1896. PRIVATE_KEY_UNLOCK();
  1897. if ( (ret = ecc_test()) != 0)
  1898. TEST_FAIL("ECC test failed!\n", ret);
  1899. else
  1900. TEST_PASS("ECC test passed!\n");
  1901. PRIVATE_KEY_LOCK();
  1902. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  1903. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  1904. if ( (ret = ecc_encrypt_test()) != 0)
  1905. TEST_FAIL("ECC Enc test failed!\n", ret);
  1906. else
  1907. TEST_PASS("ECC Enc test passed!\n");
  1908. #endif
  1909. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  1910. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  1911. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  1912. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  1913. /* skip for ATECC508/608A, cannot import private key buffers */
  1914. if ( (ret = ecc_test_buffers()) != 0)
  1915. TEST_FAIL("ECC buffer test failed!\n", ret);
  1916. else
  1917. TEST_PASS("ECC buffer test passed!\n");
  1918. #endif
  1919. #endif
  1920. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  1921. !defined(NO_FILESYSTEM)
  1922. if ( (ret = cert_test()) != 0)
  1923. TEST_FAIL("CERT test failed!\n", ret);
  1924. else
  1925. TEST_PASS("CERT test passed!\n");
  1926. #endif
  1927. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  1928. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(WOLFSSL_GEN_CERT)
  1929. if ( (ret = certext_test()) != 0)
  1930. TEST_FAIL("CERT EXT test failed!\n", ret);
  1931. else
  1932. TEST_PASS("CERT EXT test passed!\n");
  1933. #endif
  1934. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  1935. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  1936. if ( (ret = decodedCertCache_test()) != 0)
  1937. TEST_FAIL("DECODED CERT CACHE test failed!\n", ret);
  1938. else
  1939. TEST_PASS("DECODED CERT CACHE test passed!\n");
  1940. #endif
  1941. #ifdef HAVE_CURVE25519
  1942. if ( (ret = curve25519_test()) != 0)
  1943. TEST_FAIL("CURVE25519 test failed!\n", ret);
  1944. else
  1945. TEST_PASS("CURVE25519 test passed!\n");
  1946. #endif
  1947. #ifdef HAVE_ED25519
  1948. PRIVATE_KEY_UNLOCK();
  1949. if ( (ret = ed25519_test()) != 0)
  1950. TEST_FAIL("ED25519 test failed!\n", ret);
  1951. else
  1952. TEST_PASS("ED25519 test passed!\n");
  1953. PRIVATE_KEY_LOCK();
  1954. #endif
  1955. #ifdef HAVE_CURVE448
  1956. if ( (ret = curve448_test()) != 0)
  1957. TEST_FAIL("CURVE448 test failed!\n", ret);
  1958. else
  1959. TEST_PASS("CURVE448 test passed!\n");
  1960. #endif
  1961. #ifdef HAVE_ED448
  1962. PRIVATE_KEY_UNLOCK();
  1963. if ( (ret = ed448_test()) != 0)
  1964. TEST_FAIL("ED448 test failed!\n", ret);
  1965. else
  1966. TEST_PASS("ED448 test passed!\n");
  1967. PRIVATE_KEY_LOCK();
  1968. #endif
  1969. #ifdef WOLFSSL_HAVE_KYBER
  1970. if ( (ret = kyber_test()) != 0)
  1971. TEST_FAIL("KYBER test failed!\n", ret);
  1972. else
  1973. TEST_PASS("KYBER test passed!\n");
  1974. #endif
  1975. #if defined(WOLFSSL_HAVE_XMSS)
  1976. #if !defined(WOLFSSL_SMALL_STACK) && WOLFSSL_XMSS_MIN_HEIGHT <= 10
  1977. if ( (ret = xmss_test_verify_only()) != 0)
  1978. TEST_FAIL("XMSS Vfy test failed!\n", ret);
  1979. else
  1980. TEST_PASS("XMSS Vfy test passed!\n");
  1981. #endif
  1982. #if !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  1983. if ( (ret = xmss_test()) != 0)
  1984. TEST_FAIL("XMSS test failed!\n", ret);
  1985. else
  1986. TEST_PASS("XMSS test passed!\n");
  1987. #endif
  1988. #endif /* if defined(WOLFSSL_HAVE_XMSS) */
  1989. #if defined(WOLFSSL_HAVE_LMS)
  1990. #if !defined(WOLFSSL_SMALL_STACK)
  1991. #if (defined(WOLFSSL_WC_LMS) && (LMS_MAX_HEIGHT >= 10)) || \
  1992. defined(HAVE_LIBLMS)
  1993. if ( (ret = lms_test_verify_only()) != 0)
  1994. TEST_FAIL("LMS Vfy test failed!\n", ret);
  1995. else
  1996. TEST_PASS("LMS Vfy test passed!\n");
  1997. #endif
  1998. #endif
  1999. #if !defined(WOLFSSL_LMS_VERIFY_ONLY)
  2000. if ( (ret = lms_test()) != 0)
  2001. TEST_FAIL("LMS test failed!\n", ret);
  2002. else
  2003. TEST_PASS("LMS test passed!\n");
  2004. #endif
  2005. #endif /* if defined(WOLFSSL_HAVE_LMS) */
  2006. #ifdef WOLFCRYPT_HAVE_ECCSI
  2007. if ( (ret = eccsi_test()) != 0)
  2008. TEST_FAIL("ECCSI test failed!\n", ret);
  2009. else
  2010. TEST_PASS("ECCSI test passed!\n");
  2011. #endif
  2012. #ifdef WOLFCRYPT_HAVE_SAKKE
  2013. if ( (ret = sakke_test()) != 0)
  2014. TEST_FAIL("SAKKE test failed!\n", ret);
  2015. else
  2016. TEST_PASS("SAKKE test passed!\n");
  2017. #endif
  2018. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  2019. if ( (ret = cmac_test()) != 0)
  2020. TEST_FAIL("CMAC test failed!\n", ret);
  2021. else
  2022. TEST_PASS("CMAC test passed!\n");
  2023. #endif
  2024. #if defined(WOLFSSL_SIPHASH)
  2025. if ( (ret = siphash_test()) != 0)
  2026. TEST_FAIL("SipHash test failed!\n", ret);
  2027. else
  2028. TEST_PASS("SipHash test passed!\n");
  2029. #endif
  2030. #ifdef HAVE_LIBZ
  2031. if ( (ret = compress_test()) != 0)
  2032. TEST_FAIL("COMPRESS test failed!\n", ret);
  2033. else
  2034. TEST_PASS("COMPRESS test passed!\n");
  2035. #endif
  2036. #ifdef HAVE_PKCS7
  2037. #ifndef NO_PKCS7_ENCRYPTED_DATA
  2038. if ( (ret = pkcs7encrypted_test()) != 0)
  2039. TEST_FAIL("PKCS7encrypted test failed!\n", ret);
  2040. else
  2041. TEST_PASS("PKCS7encrypted test passed!\n");
  2042. #endif
  2043. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  2044. if ( (ret = pkcs7compressed_test()) != 0)
  2045. TEST_FAIL("PKCS7compressed test failed!\n", ret);
  2046. else
  2047. TEST_PASS("PKCS7compressed test passed!\n");
  2048. #endif
  2049. if ( (ret = pkcs7signed_test()) != 0)
  2050. TEST_FAIL("PKCS7signed test failed!\n", ret);
  2051. else
  2052. TEST_PASS("PKCS7signed test passed!\n");
  2053. if ( (ret = pkcs7enveloped_test()) != 0)
  2054. TEST_FAIL("PKCS7enveloped test failed!\n", ret);
  2055. else
  2056. TEST_PASS("PKCS7enveloped test passed!\n");
  2057. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  2058. if ( (ret = pkcs7authenveloped_test()) != 0)
  2059. TEST_FAIL("PKCS7authenveloped test failed!\n", ret);
  2060. else
  2061. TEST_PASS("PKCS7authenveloped test passed!\n");
  2062. #endif
  2063. #endif
  2064. #if defined(WOLFSSL_PUBLIC_MP) && \
  2065. ((defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  2066. defined(USE_FAST_MATH))
  2067. if ( (ret = mp_test()) != 0)
  2068. TEST_FAIL("mp test failed!\n", ret);
  2069. else
  2070. TEST_PASS("mp test passed!\n");
  2071. #endif
  2072. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  2073. if ( (ret = prime_test()) != 0)
  2074. TEST_FAIL("prime test failed!\n", ret);
  2075. else
  2076. TEST_PASS("prime test passed!\n");
  2077. #endif
  2078. #if defined(ASN_BER_TO_DER) && \
  2079. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  2080. defined(OPENSSL_EXTRA_X509_SMALL))
  2081. if ( (ret = berder_test()) != 0)
  2082. TEST_FAIL("ber-der test failed!\n", ret);
  2083. else
  2084. TEST_PASS("ber-der test passed!\n");
  2085. #endif
  2086. if ( (ret = logging_test()) != 0)
  2087. TEST_FAIL("logging test failed!\n", ret);
  2088. else
  2089. TEST_PASS("logging test passed!\n");
  2090. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  2091. if ( (ret = time_test()) != 0)
  2092. TEST_FAIL("time test failed!\n", ret);
  2093. else
  2094. TEST_PASS("time test passed!\n");
  2095. #endif
  2096. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  2097. if ((ret = wolfcrypt_mutex_test()) != 0)
  2098. #else
  2099. if ((ret = mutex_test()) != 0)
  2100. #endif
  2101. TEST_FAIL("mutex test failed!\n", ret);
  2102. else
  2103. TEST_PASS("mutex test passed!\n");
  2104. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  2105. if ( (ret = memcb_test()) != 0)
  2106. TEST_FAIL("memcb test failed!\n", ret);
  2107. else
  2108. TEST_PASS("memcb test passed!\n");
  2109. #endif
  2110. #ifdef WOLFSSL_CAAM_BLOB
  2111. if ( (ret = blob_test()) != 0)
  2112. TEST_FAIL("blob test failed!\n", ret);
  2113. else
  2114. TEST_PASS("blob test passed!\n");
  2115. #endif
  2116. #if defined(WOLF_CRYPTO_CB) && \
  2117. !(defined(HAVE_INTEL_QAT_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC) || \
  2118. defined(WOLFSSL_QNX_CAAM) || defined(HAVE_RENESAS_SYNC))
  2119. if ( (ret = cryptocb_test()) != 0)
  2120. TEST_FAIL("crypto callback test failed!\n", ret);
  2121. else
  2122. TEST_PASS("crypto callback test passed!\n");
  2123. #endif
  2124. #ifdef WOLFSSL_CERT_PIV
  2125. if ( (ret = certpiv_test()) != 0)
  2126. TEST_FAIL("cert piv test failed!\n", ret);
  2127. else
  2128. TEST_PASS("cert piv test passed!\n");
  2129. #endif
  2130. #ifdef WOLF_CRYPTO_CB
  2131. #ifdef HAVE_INTEL_QA_SYNC
  2132. wc_CryptoCb_CleanupIntelQa(&devId);
  2133. #endif
  2134. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  2135. wc_CryptoCb_CleanupOcteon(&devId);
  2136. #endif
  2137. #endif
  2138. #ifdef WOLFSSL_ASYNC_CRYPT
  2139. wolfAsync_DevClose(&devId);
  2140. #endif
  2141. /* cleanup the thread if fixed point cache is enabled and have thread local */
  2142. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  2143. wc_ecc_fp_free();
  2144. #endif
  2145. #ifdef TEST_ALWAYS_RUN_TO_END
  2146. if (last_failed_test_ret != 0)
  2147. ret = last_failed_test_ret;
  2148. #endif
  2149. if (args)
  2150. ((func_args*)args)->return_code = ret;
  2151. /* If hardware acceleration and respective metrics tracked, show results: */
  2152. #ifdef WOLFSSL_HW_METRICS
  2153. #if defined(WOLFSSL_ESP32_CRYPT_RSA_PRI) && defined(WOLFSSL_HW_METRICS)
  2154. esp_hw_show_mp_metrics();
  2155. #endif
  2156. #endif
  2157. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(OPENSSL_EXTRA)
  2158. wolfSSL_SetGlobalHeapHint(NULL);
  2159. #endif
  2160. TEST_PASS("Test complete\n");
  2161. EXIT_TEST(ret);
  2162. } /* end of wolfcrypt_test() */
  2163. #ifndef NO_MAIN_DRIVER
  2164. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  2165. int myoptind = 0;
  2166. char* myoptarg = NULL;
  2167. #endif
  2168. /* so overall tests can pull in test function */
  2169. #if defined(WOLFSSL_ESPIDF) || defined(_WIN32_WCE)
  2170. wc_test_ret_t wolf_test_task(void)
  2171. #else
  2172. #ifndef NO_MAIN_FUNCTION
  2173. int main(int argc, char** argv)
  2174. {
  2175. return (int)wolfcrypt_test_main(argc, argv);
  2176. }
  2177. #endif
  2178. wc_test_ret_t wolfcrypt_test_main(int argc, char** argv)
  2179. #endif
  2180. {
  2181. wc_test_ret_t ret;
  2182. func_args args = { 0, 0, 0 };
  2183. #if defined(WOLFSSL_ESPIDF) || defined(WOLFSSL_SE050)
  2184. /* set dummy wallclock time. */
  2185. struct timeval utctime;
  2186. struct timezone tz;
  2187. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  2188. utctime.tv_usec = 0;
  2189. tz.tz_minuteswest = 0;
  2190. tz.tz_dsttime = 0;
  2191. settimeofday(&utctime, &tz);
  2192. #endif
  2193. #ifdef WOLFSSL_APACHE_MYNEWT
  2194. #ifdef ARCH_sim
  2195. mcu_sim_parse_args(argc, argv);
  2196. #endif
  2197. sysinit();
  2198. /* set dummy wallclock time. */
  2199. struct os_timeval utctime;
  2200. struct os_timezone tz;
  2201. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  2202. utctime.tv_usec = 0;
  2203. tz.tz_minuteswest = 0;
  2204. tz.tz_dsttime = 0;
  2205. os_settimeofday(&utctime, &tz);
  2206. #endif
  2207. #ifdef WOLFSSL_ZEPHYR
  2208. /* set dummy wallclock time. */
  2209. struct timespec utctime;
  2210. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  2211. utctime.tv_nsec = 0;
  2212. clock_settime(CLOCK_REALTIME, &utctime);
  2213. #endif
  2214. #ifdef DEVKITPRO
  2215. void *framebuffer;
  2216. GXRModeObj *rmode = NULL;
  2217. VIDEO_Init();
  2218. WPAD_Init();
  2219. rmode = VIDEO_GetPreferredMode(NULL);
  2220. #pragma GCC diagnostic ignored "-Wbad-function-cast"
  2221. framebuffer = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
  2222. #pragma GCC diagnostic pop
  2223. console_init(framebuffer,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
  2224. VIDEO_Configure(rmode);
  2225. VIDEO_SetNextFramebuffer(framebuffer);
  2226. VIDEO_SetBlack(FALSE);
  2227. VIDEO_Flush();
  2228. VIDEO_WaitVSync();
  2229. if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
  2230. #endif
  2231. #ifdef HAVE_WNR
  2232. if ((ret = wc_InitNetRandom(wnrConfigFile, NULL, 5000)) != 0) {
  2233. err_sys("Whitewood netRandom global config failed",
  2234. WC_TEST_RET_ENC_EC(ret));
  2235. return WC_TEST_RET_ENC_EC(ret);
  2236. }
  2237. #endif
  2238. #ifndef WOLFSSL_ESPIDF
  2239. args.argc = argc;
  2240. args.argv = argv;
  2241. #endif
  2242. if ((ret = wolfCrypt_Init()) != 0) {
  2243. printf("wolfCrypt_Init failed %d\n", (int)ret);
  2244. err_sys("Error with wolfCrypt_Init!\n", WC_TEST_RET_ENC_EC(ret));
  2245. }
  2246. #ifdef HAVE_WC_INTROSPECTION
  2247. printf("Math: %s\n", wc_GetMathInfo());
  2248. #endif
  2249. #ifdef HAVE_STACK_SIZE
  2250. StackSizeCheck(&args, wolfcrypt_test);
  2251. #else
  2252. wolfcrypt_test(&args);
  2253. #endif
  2254. if ((ret = wolfCrypt_Cleanup()) != 0) {
  2255. printf("wolfCrypt_Cleanup failed %d\n", (int)ret);
  2256. err_sys("Error with wolfCrypt_Cleanup!\n", WC_TEST_RET_ENC_EC(ret));
  2257. }
  2258. #ifdef HAVE_WNR
  2259. if ((ret = wc_FreeNetRandom()) < 0)
  2260. err_sys("Failed to free netRandom context",
  2261. WC_TEST_RET_ENC_EC(ret));
  2262. #endif /* HAVE_WNR */
  2263. #ifdef DOLPHIN_EMULATOR
  2264. /* Returning from main panics the emulator. Just hang
  2265. * and let the user force quit the emulator window. */
  2266. printf("args.return_code: %d\n", args.return_code);
  2267. printf("Testing complete. You may close the window now\n");
  2268. while (1);
  2269. #endif
  2270. #if defined(WOLFSSL_ESPIDF)
  2271. /* ESP_LOGI to print takes up a lot less memory than printf */
  2272. ESP_LOGI(ESPIDF_TAG, "Exiting main with return code: % d\n",
  2273. args.return_code);
  2274. #else
  2275. /* gate this for target platforms wishing to avoid printf reference */
  2276. printf("Exiting main with return code: %ld\n", (long int)args.return_code);
  2277. #endif
  2278. return args.return_code;
  2279. } /* wolfcrypt_test_main or wolf_test_task */
  2280. #endif /* NO_MAIN_DRIVER */
  2281. /* helper to save DER, convert to PEM and save PEM */
  2282. #if !defined(NO_ASN) && (defined(HAVE_ECC) || !defined(NO_DSA) || \
  2283. (!defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)))) \
  2284. && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  2285. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  2286. #define SaveDerAndPem(d, dSz, fD, fP, pT) _SaveDerAndPem(d, dSz, fD, fP, pT, WC_TEST_RET_LN)
  2287. #else
  2288. #define SaveDerAndPem(d, dSz, fD, fP, pT) _SaveDerAndPem(d, dSz, NULL, NULL, pT, WC_TEST_RET_LN)
  2289. #endif
  2290. static wc_test_ret_t _SaveDerAndPem(const byte* der, int derSz,
  2291. const char* fileDer, const char* filePem, int pemType, int calling_line)
  2292. {
  2293. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  2294. wc_test_ret_t ret;
  2295. XFILE derFile;
  2296. derFile = XFOPEN(fileDer, "wb");
  2297. if (!derFile) {
  2298. return WC_TEST_RET_ENC(calling_line, 0, WC_TEST_RET_TAG_I);
  2299. }
  2300. ret = (int)XFWRITE(der, 1, (size_t)derSz, derFile);
  2301. XFCLOSE(derFile);
  2302. if (ret != derSz) {
  2303. return WC_TEST_RET_ENC(calling_line, 1, WC_TEST_RET_TAG_I);
  2304. }
  2305. #endif
  2306. #ifdef WOLFSSL_DER_TO_PEM
  2307. if (filePem) {
  2308. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  2309. XFILE pemFile;
  2310. #endif
  2311. #ifndef WOLFSSL_NO_MALLOC
  2312. byte* pem;
  2313. #else
  2314. byte pem[1024];
  2315. #endif
  2316. int pemSz;
  2317. /* calculate PEM size */
  2318. pemSz = wc_DerToPem(der, (word32)derSz, NULL, 0, pemType);
  2319. if (pemSz < 0) {
  2320. return WC_TEST_RET_ENC(calling_line, 2, WC_TEST_RET_TAG_I);
  2321. }
  2322. #ifndef WOLFSSL_NO_MALLOC
  2323. pem = (byte*)XMALLOC(pemSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2324. if (pem == NULL) {
  2325. return WC_TEST_RET_ENC(calling_line, 3, WC_TEST_RET_TAG_I);
  2326. }
  2327. #else
  2328. if (pemSz > (int)sizeof(pem))
  2329. return BAD_FUNC_ARG;
  2330. #endif
  2331. /* Convert to PEM */
  2332. pemSz = wc_DerToPem(der, (word32)derSz, pem, pemSz, pemType);
  2333. if (pemSz < 0) {
  2334. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2335. return WC_TEST_RET_ENC(calling_line, 4, WC_TEST_RET_TAG_I);
  2336. }
  2337. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  2338. pemFile = XFOPEN(filePem, "wb");
  2339. if (!pemFile) {
  2340. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2341. return WC_TEST_RET_ENC(calling_line, 5, WC_TEST_RET_TAG_I);
  2342. }
  2343. ret = (int)XFWRITE(pem, 1, (size_t)pemSz, pemFile);
  2344. XFCLOSE(pemFile);
  2345. if (ret != pemSz) {
  2346. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2347. return WC_TEST_RET_ENC(calling_line, 6, WC_TEST_RET_TAG_I);
  2348. }
  2349. #endif
  2350. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2351. }
  2352. #endif /* WOLFSSL_DER_TO_PEM */
  2353. /* suppress unused variable warnings */
  2354. (void)der;
  2355. (void)derSz;
  2356. (void)filePem;
  2357. (void)fileDer;
  2358. (void)pemType;
  2359. (void)calling_line;
  2360. return 0;
  2361. }
  2362. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  2363. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t error_test(void)
  2364. {
  2365. const char* errStr;
  2366. char out[WOLFSSL_MAX_ERROR_SZ];
  2367. const char* unknownStr = wc_GetErrorString(0);
  2368. #ifdef NO_ERROR_STRINGS
  2369. /* Ensure a valid error code's string matches an invalid code's.
  2370. * The string is that error strings are not available.
  2371. */
  2372. WOLFSSL_ENTER("error_test NO_ERROR_STRINGS");
  2373. errStr = wc_GetErrorString(OPEN_RAN_E);
  2374. wc_ErrorString(OPEN_RAN_E, out);
  2375. if (XSTRCMP(errStr, unknownStr) != 0)
  2376. return WC_TEST_RET_ENC_NC;
  2377. if (XSTRCMP(out, unknownStr) != 0)
  2378. return WC_TEST_RET_ENC_NC;
  2379. #else
  2380. int i;
  2381. int j = 0;
  2382. /* Values that are not or no longer error codes. */
  2383. int missing[] = { -124, -166, -167, -168, -169, 0 };
  2384. /* Check that all errors have a string and it's the same through the two
  2385. * APIs. Check that the values that are not errors map to the unknown
  2386. * string.
  2387. */
  2388. for (i = MAX_CODE_E-1; i >= WC_LAST_E; i--) {
  2389. errStr = wc_GetErrorString(i);
  2390. wc_ErrorString(i, out);
  2391. if (i != missing[j]) {
  2392. if (XSTRCMP(errStr, unknownStr) == 0) {
  2393. WOLFSSL_MSG("errStr unknown");
  2394. return WC_TEST_RET_ENC_NC;
  2395. }
  2396. if (XSTRCMP(out, unknownStr) == 0) {
  2397. WOLFSSL_MSG("out unknown");
  2398. return WC_TEST_RET_ENC_NC;
  2399. }
  2400. if (XSTRCMP(errStr, out) != 0) {
  2401. WOLFSSL_MSG("errStr does not match output");
  2402. return WC_TEST_RET_ENC_NC;
  2403. }
  2404. if (XSTRLEN(errStr) >= WOLFSSL_MAX_ERROR_SZ) {
  2405. WOLFSSL_MSG("errStr too long");
  2406. return WC_TEST_RET_ENC_NC;
  2407. }
  2408. }
  2409. else {
  2410. j++;
  2411. if (XSTRCMP(errStr, unknownStr) != 0)
  2412. return WC_TEST_RET_ENC_NC;
  2413. if (XSTRCMP(out, unknownStr) != 0)
  2414. return WC_TEST_RET_ENC_NC;
  2415. }
  2416. }
  2417. /* Check if the next possible value has been given a string. */
  2418. errStr = wc_GetErrorString(i);
  2419. wc_ErrorString(i, out);
  2420. if (XSTRCMP(errStr, unknownStr) != 0)
  2421. return WC_TEST_RET_ENC_NC;
  2422. if (XSTRCMP(out, unknownStr) != 0)
  2423. return WC_TEST_RET_ENC_NC;
  2424. #endif
  2425. return 0;
  2426. }
  2427. #ifndef NO_CODING
  2428. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base64_test(void)
  2429. {
  2430. wc_test_ret_t ret;
  2431. WOLFSSL_SMALL_STACK_STATIC const byte good[] = "A+Gd\0\0\0";
  2432. WOLFSSL_SMALL_STACK_STATIC const byte goodEnd[] = "A+Gd \r\n";
  2433. WOLFSSL_SMALL_STACK_STATIC const byte good_spaces[] = " A + G d \0";
  2434. byte out[128];
  2435. word32 outLen;
  2436. #ifdef WOLFSSL_BASE64_ENCODE
  2437. byte data[3];
  2438. word32 dataLen;
  2439. byte longData[79] = { 0 };
  2440. WOLFSSL_SMALL_STACK_STATIC const byte symbols[] = "+/A=";
  2441. #endif
  2442. WOLFSSL_SMALL_STACK_STATIC const byte badSmall[] = "AAA!Gdj=";
  2443. WOLFSSL_SMALL_STACK_STATIC const byte badLarge[] = "AAA~Gdj=";
  2444. WOLFSSL_SMALL_STACK_STATIC const byte badEOL[] = "A+Gd!AA";
  2445. WOLFSSL_SMALL_STACK_STATIC const byte badPadding[] = "AA=A";
  2446. WOLFSSL_SMALL_STACK_STATIC const byte badChar[] = ",-.:;<=>?@[\\]^_`";
  2447. byte goodChar[] =
  2448. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  2449. "abcdefghijklmnopqrstuvwxyz"
  2450. "0123456789+/;";
  2451. byte charTest[] = "A+Gd\0\0\0";
  2452. int i;
  2453. WOLFSSL_ENTER("base64_test");
  2454. /* Good Base64 encodings. */
  2455. outLen = sizeof(out);
  2456. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  2457. if (ret != 0)
  2458. return WC_TEST_RET_ENC_EC(ret);
  2459. outLen = sizeof(out);
  2460. ret = Base64_Decode(goodEnd, sizeof(goodEnd), out, &outLen);
  2461. if (ret != 0)
  2462. return WC_TEST_RET_ENC_EC(ret);
  2463. outLen = sizeof(goodChar);
  2464. ret = Base64_Decode(goodChar, sizeof(goodChar), goodChar, &outLen);
  2465. if (ret != 0)
  2466. return WC_TEST_RET_ENC_EC(ret);
  2467. if (outLen != 64 / 4 * 3)
  2468. return WC_TEST_RET_ENC_NC;
  2469. outLen = sizeof(out);
  2470. ret = Base64_Decode(good_spaces, sizeof(good_spaces), out, &outLen);
  2471. if (ret != 0)
  2472. return WC_TEST_RET_ENC_EC(ret);
  2473. /* Bad parameters. */
  2474. outLen = 1;
  2475. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  2476. if (ret != BAD_FUNC_ARG)
  2477. return WC_TEST_RET_ENC_EC(ret);
  2478. outLen = sizeof(out);
  2479. ret = Base64_Decode(badEOL, sizeof(badEOL), out, &outLen);
  2480. if (ret != ASN_INPUT_E)
  2481. return WC_TEST_RET_ENC_EC(ret);
  2482. outLen = sizeof(out);
  2483. ret = Base64_Decode(badPadding, sizeof(badPadding), out, &outLen);
  2484. if (ret != ASN_INPUT_E)
  2485. return WC_TEST_RET_ENC_EC(ret);
  2486. /* Bad character at each offset 0-3. */
  2487. for (i = 0; i < 4; i++) {
  2488. outLen = sizeof(out);
  2489. ret = Base64_Decode(badSmall + i, 4, out, &outLen);
  2490. if (ret != ASN_INPUT_E)
  2491. return WC_TEST_RET_ENC_I(i);
  2492. ret = Base64_Decode(badLarge + i, 4, out, &outLen);
  2493. if (ret != ASN_INPUT_E)
  2494. return WC_TEST_RET_ENC_I(i);
  2495. }
  2496. /* Invalid character less than 0x2b */
  2497. for (i = 1; i < 0x2b; i++) {
  2498. outLen = sizeof(out);
  2499. charTest[0] = (byte)i;
  2500. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  2501. if (ret != ASN_INPUT_E)
  2502. return WC_TEST_RET_ENC_I(i);
  2503. }
  2504. /* Bad characters in range 0x2b - 0x7a. */
  2505. for (i = 0; i < (int)sizeof(badChar) - 1; i++) {
  2506. outLen = sizeof(out);
  2507. charTest[0] = badChar[i];
  2508. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  2509. if (ret != ASN_INPUT_E)
  2510. return WC_TEST_RET_ENC_I(i);
  2511. }
  2512. /* Invalid character greater than 0x7a */
  2513. for (i = 0x7b; i < 0x100; i++) {
  2514. outLen = sizeof(out);
  2515. charTest[0] = (byte)i;
  2516. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  2517. if (ret != ASN_INPUT_E)
  2518. return WC_TEST_RET_ENC_I(i);
  2519. }
  2520. #ifdef WOLFSSL_BASE64_ENCODE
  2521. /* Decode and encode all symbols - non-alphanumeric. */
  2522. dataLen = sizeof(data);
  2523. ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen);
  2524. if (ret != 0)
  2525. return WC_TEST_RET_ENC_EC(ret);
  2526. outLen = sizeof(out);
  2527. ret = Base64_Encode(data, dataLen, NULL, &outLen);
  2528. if (ret != LENGTH_ONLY_E)
  2529. return WC_TEST_RET_ENC_EC(ret);
  2530. outLen = sizeof(out);
  2531. ret = Base64_Encode(data, dataLen, out, &outLen);
  2532. if (ret != 0)
  2533. return WC_TEST_RET_ENC_EC(ret);
  2534. outLen = 7;
  2535. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  2536. if (ret != BUFFER_E)
  2537. return WC_TEST_RET_ENC_EC(ret);
  2538. outLen = sizeof(out);
  2539. ret = Base64_EncodeEsc(data, dataLen, NULL, &outLen);
  2540. if (ret != LENGTH_ONLY_E)
  2541. return WC_TEST_RET_ENC_EC(ret);
  2542. outLen = sizeof(out);
  2543. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  2544. if (ret != 0)
  2545. return WC_TEST_RET_ENC_EC(ret);
  2546. outLen = sizeof(out);
  2547. ret = Base64_Encode_NoNl(data, dataLen, out, &outLen);
  2548. if (ret != 0)
  2549. return WC_TEST_RET_ENC_EC(ret);
  2550. /* Data that results in an encoding longer than one line. */
  2551. outLen = sizeof(out);
  2552. dataLen = sizeof(longData);
  2553. ret = Base64_Encode(longData, dataLen, out, &outLen);
  2554. if (ret != 0)
  2555. return WC_TEST_RET_ENC_EC(ret);
  2556. outLen = sizeof(out);
  2557. ret = Base64_EncodeEsc(longData, dataLen, out, &outLen);
  2558. if (ret != 0)
  2559. return WC_TEST_RET_ENC_EC(ret);
  2560. outLen = sizeof(out);
  2561. ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen);
  2562. if (ret != 0)
  2563. return WC_TEST_RET_ENC_EC(ret);
  2564. #endif
  2565. return 0;
  2566. }
  2567. #ifdef WOLFSSL_BASE16
  2568. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base16_test(void)
  2569. {
  2570. wc_test_ret_t ret;
  2571. WOLFSSL_SMALL_STACK_STATIC const byte testData[] = "SomeDataToEncode\n";
  2572. WOLFSSL_SMALL_STACK_STATIC const byte encodedTestData[] = "536F6D6544617461546F456E636F64650A00";
  2573. byte encoded[40];
  2574. word32 encodedLen;
  2575. byte plain[40];
  2576. word32 len;
  2577. WOLFSSL_ENTER("base16_test");
  2578. /* length returned includes null termination */
  2579. encodedLen = sizeof(encoded);
  2580. ret = Base16_Encode(testData, sizeof(testData), encoded, &encodedLen);
  2581. if (ret != 0)
  2582. return WC_TEST_RET_ENC_EC(ret);
  2583. len = (word32)XSTRLEN((char*)encoded);
  2584. if (len != encodedLen - 1)
  2585. return WC_TEST_RET_ENC_NC;
  2586. len = sizeof(plain);
  2587. ret = Base16_Decode(encoded, encodedLen - 1, plain, &len);
  2588. if (ret != 0)
  2589. return WC_TEST_RET_ENC_EC(ret);
  2590. if (len != sizeof(testData) || XMEMCMP(testData, plain, len) != 0)
  2591. return WC_TEST_RET_ENC_NC;
  2592. if (encodedLen != sizeof(encodedTestData) ||
  2593. XMEMCMP(encoded, encodedTestData, encodedLen) != 0) {
  2594. return WC_TEST_RET_ENC_NC;
  2595. }
  2596. return 0;
  2597. }
  2598. #endif /* WOLFSSL_BASE16 */
  2599. #endif /* !NO_CODING */
  2600. #ifndef NO_ASN
  2601. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t asn_test(void)
  2602. {
  2603. wc_test_ret_t ret;
  2604. /* ASN1 encoded date buffer */
  2605. WOLFSSL_SMALL_STACK_STATIC const byte dateBuf[] = {
  2606. 0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31,
  2607. 0x32, 0x30, 0x30, 0x37, 0x33, 0x37, 0x5a
  2608. };
  2609. byte format;
  2610. int length;
  2611. const byte* datePart;
  2612. #ifndef NO_ASN_TIME
  2613. struct tm timearg;
  2614. time_t now;
  2615. #endif
  2616. WOLFSSL_ENTER("asn_test");
  2617. ret = wc_GetDateInfo(dateBuf, (int)sizeof(dateBuf), &datePart, &format,
  2618. &length);
  2619. if (ret != 0)
  2620. return WC_TEST_RET_ENC_EC(ret);
  2621. #ifndef NO_ASN_TIME
  2622. /* Parameter Validation tests. */
  2623. if ((ret = wc_GetTime(NULL, sizeof(now))) != BAD_FUNC_ARG)
  2624. return WC_TEST_RET_ENC_EC(ret);
  2625. if ((ret = wc_GetTime(&now, 0)) != BUFFER_E)
  2626. return WC_TEST_RET_ENC_EC(ret);
  2627. now = 0;
  2628. if ((ret = wc_GetTime(&now, sizeof(now))) != 0) {
  2629. return WC_TEST_RET_ENC_EC(ret);
  2630. }
  2631. if (now == 0) {
  2632. printf("RTC/Time not set!\n");
  2633. return WC_TEST_RET_ENC_NC;
  2634. }
  2635. ret = wc_GetDateAsCalendarTime(datePart, length, format, &timearg);
  2636. if (ret != 0)
  2637. return WC_TEST_RET_ENC_EC(ret);
  2638. #endif /* !NO_ASN_TIME */
  2639. return 0;
  2640. }
  2641. #endif /* !NO_ASN */
  2642. #ifdef WOLFSSL_MD2
  2643. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md2_test(void)
  2644. {
  2645. wc_test_ret_t ret = 0;
  2646. Md2 md2;
  2647. byte hash[MD2_DIGEST_SIZE];
  2648. testVector a, b, c, d, e, f, g;
  2649. testVector test_md2[7];
  2650. int times = sizeof(test_md2) / sizeof(testVector), i;
  2651. WOLFSSL_ENTER("md2_test");
  2652. a.input = "";
  2653. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  2654. "\x27\x73";
  2655. a.inLen = XSTRLEN(a.input);
  2656. a.outLen = MD2_DIGEST_SIZE;
  2657. b.input = "a";
  2658. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  2659. "\xb5\xd1";
  2660. b.inLen = XSTRLEN(b.input);
  2661. b.outLen = MD2_DIGEST_SIZE;
  2662. c.input = "abc";
  2663. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  2664. "\xd6\xbb";
  2665. c.inLen = XSTRLEN(c.input);
  2666. c.outLen = MD2_DIGEST_SIZE;
  2667. d.input = "message digest";
  2668. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  2669. "\x06\xb0";
  2670. d.inLen = XSTRLEN(d.input);
  2671. d.outLen = MD2_DIGEST_SIZE;
  2672. e.input = "abcdefghijklmnopqrstuvwxyz";
  2673. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  2674. "\x94\x0b";
  2675. e.inLen = XSTRLEN(e.input);
  2676. e.outLen = MD2_DIGEST_SIZE;
  2677. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2678. "6789";
  2679. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  2680. "\x38\xcd";
  2681. f.inLen = XSTRLEN(f.input);
  2682. f.outLen = MD2_DIGEST_SIZE;
  2683. g.input = "1234567890123456789012345678901234567890123456789012345678"
  2684. "9012345678901234567890";
  2685. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  2686. "\xef\xd8";
  2687. g.inLen = XSTRLEN(g.input);
  2688. g.outLen = MD2_DIGEST_SIZE;
  2689. test_md2[0] = a;
  2690. test_md2[1] = b;
  2691. test_md2[2] = c;
  2692. test_md2[3] = d;
  2693. test_md2[4] = e;
  2694. test_md2[5] = f;
  2695. test_md2[6] = g;
  2696. wc_InitMd2(&md2);
  2697. for (i = 0; i < times; ++i) {
  2698. wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  2699. wc_Md2Final(&md2, hash);
  2700. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  2701. return WC_TEST_RET_ENC_I(i);
  2702. }
  2703. for (i = 0; i < times; ++i) {
  2704. ret = wc_Md2Hash((byte*)test_md2[i].input, (word32)test_md2[i].inLen, hash);
  2705. if (ret != 0) {
  2706. return WC_TEST_RET_ENC_I(i);
  2707. }
  2708. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0) {
  2709. return WC_TEST_RET_ENC_I(i);
  2710. }
  2711. }
  2712. return 0;
  2713. }
  2714. #endif
  2715. #ifndef NO_MD5
  2716. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md5_test(void)
  2717. {
  2718. wc_test_ret_t ret = 0;
  2719. wc_Md5 md5, md5Copy;
  2720. byte hash[WC_MD5_DIGEST_SIZE];
  2721. byte hashcopy[WC_MD5_DIGEST_SIZE];
  2722. testVector a, b, c, d, e, f;
  2723. testVector test_md5[6];
  2724. int times = sizeof(test_md5) / sizeof(testVector), i;
  2725. WOLFSSL_ENTER("md5_test");
  2726. a.input = "";
  2727. a.output = "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42"
  2728. "\x7e";
  2729. a.inLen = XSTRLEN(a.input);
  2730. a.outLen = WC_MD5_DIGEST_SIZE;
  2731. b.input = "abc";
  2732. b.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  2733. "\x72";
  2734. b.inLen = XSTRLEN(b.input);
  2735. b.outLen = WC_MD5_DIGEST_SIZE;
  2736. c.input = "message digest";
  2737. c.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  2738. "\xd0";
  2739. c.inLen = XSTRLEN(c.input);
  2740. c.outLen = WC_MD5_DIGEST_SIZE;
  2741. d.input = "abcdefghijklmnopqrstuvwxyz";
  2742. d.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  2743. "\x3b";
  2744. d.inLen = XSTRLEN(d.input);
  2745. d.outLen = WC_MD5_DIGEST_SIZE;
  2746. e.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2747. "6789";
  2748. e.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  2749. "\x9f";
  2750. e.inLen = XSTRLEN(e.input);
  2751. e.outLen = WC_MD5_DIGEST_SIZE;
  2752. f.input = "1234567890123456789012345678901234567890123456789012345678"
  2753. "9012345678901234567890";
  2754. f.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  2755. "\x7a";
  2756. f.inLen = XSTRLEN(f.input);
  2757. f.outLen = WC_MD5_DIGEST_SIZE;
  2758. test_md5[0] = a;
  2759. test_md5[1] = b;
  2760. test_md5[2] = c;
  2761. test_md5[3] = d;
  2762. test_md5[4] = e;
  2763. test_md5[5] = f;
  2764. ret = wc_InitMd5_ex(&md5, HEAP_HINT, devId);
  2765. if (ret != 0)
  2766. return WC_TEST_RET_ENC_EC(ret);
  2767. ret = wc_InitMd5_ex(&md5Copy, HEAP_HINT, devId);
  2768. if (ret != 0) {
  2769. wc_Md5Free(&md5);
  2770. return WC_TEST_RET_ENC_EC(ret);
  2771. }
  2772. for (i = 0; i < times; ++i) {
  2773. ret = wc_Md5Update(&md5, (byte*)test_md5[i].input,
  2774. (word32)test_md5[i].inLen);
  2775. if (ret != 0)
  2776. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2777. ret = wc_Md5GetHash(&md5, hashcopy);
  2778. if (ret != 0)
  2779. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2780. ret = wc_Md5Copy(&md5, &md5Copy);
  2781. if (ret != 0)
  2782. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2783. ret = wc_Md5Final(&md5, hash);
  2784. if (ret != 0)
  2785. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2786. wc_Md5Free(&md5Copy);
  2787. if (XMEMCMP(hash, test_md5[i].output, WC_MD5_DIGEST_SIZE) != 0)
  2788. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2789. if (XMEMCMP(hash, hashcopy, WC_MD5_DIGEST_SIZE) != 0)
  2790. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2791. }
  2792. #ifndef NO_LARGE_HASH_TEST
  2793. /* BEGIN LARGE HASH TEST */ {
  2794. byte large_input[1024];
  2795. const char* large_digest =
  2796. "\x44\xd0\x88\xce\xf1\x36\xd1\x78\xe9\xc8\xba\x84\xc3\xfd\xf6\xca";
  2797. for (i = 0; i < (int)sizeof(large_input); i++) {
  2798. large_input[i] = (byte)(i & 0xFF);
  2799. }
  2800. times = 100;
  2801. #ifdef WOLFSSL_PIC32MZ_HASH
  2802. wc_Md5SizeSet(&md5, times * sizeof(large_input));
  2803. #endif
  2804. for (i = 0; i < times; ++i) {
  2805. ret = wc_Md5Update(&md5, (byte*)large_input,
  2806. (word32)sizeof(large_input));
  2807. if (ret != 0)
  2808. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2809. }
  2810. ret = wc_Md5Final(&md5, hash);
  2811. if (ret != 0)
  2812. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2813. if (XMEMCMP(hash, large_digest, WC_MD5_DIGEST_SIZE) != 0)
  2814. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  2815. } /* END LARGE HASH TEST */
  2816. #endif /* NO_LARGE_HASH_TEST */
  2817. exit:
  2818. wc_Md5Free(&md5);
  2819. wc_Md5Free(&md5Copy);
  2820. return ret;
  2821. }
  2822. #endif /* NO_MD5 */
  2823. #ifndef NO_MD4
  2824. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md4_test(void)
  2825. {
  2826. Md4 md4;
  2827. byte hash[MD4_DIGEST_SIZE];
  2828. testVector a, b, c, d, e, f, g;
  2829. testVector test_md4[7];
  2830. int times = sizeof(test_md4) / sizeof(testVector), i;
  2831. WOLFSSL_ENTER("md4_test");
  2832. a.input = "";
  2833. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  2834. "\xc0";
  2835. a.inLen = XSTRLEN(a.input);
  2836. a.outLen = MD4_DIGEST_SIZE;
  2837. b.input = "a";
  2838. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  2839. "\x24";
  2840. b.inLen = XSTRLEN(b.input);
  2841. b.outLen = MD4_DIGEST_SIZE;
  2842. c.input = "abc";
  2843. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  2844. "\x9d";
  2845. c.inLen = XSTRLEN(c.input);
  2846. c.outLen = MD4_DIGEST_SIZE;
  2847. d.input = "message digest";
  2848. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  2849. "\x4b";
  2850. d.inLen = XSTRLEN(d.input);
  2851. d.outLen = MD4_DIGEST_SIZE;
  2852. e.input = "abcdefghijklmnopqrstuvwxyz";
  2853. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  2854. "\xa9";
  2855. e.inLen = XSTRLEN(e.input);
  2856. e.outLen = MD4_DIGEST_SIZE;
  2857. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2858. "6789";
  2859. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  2860. "\xe4";
  2861. f.inLen = XSTRLEN(f.input);
  2862. f.outLen = MD4_DIGEST_SIZE;
  2863. g.input = "1234567890123456789012345678901234567890123456789012345678"
  2864. "9012345678901234567890";
  2865. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  2866. "\x36";
  2867. g.inLen = XSTRLEN(g.input);
  2868. g.outLen = MD4_DIGEST_SIZE;
  2869. test_md4[0] = a;
  2870. test_md4[1] = b;
  2871. test_md4[2] = c;
  2872. test_md4[3] = d;
  2873. test_md4[4] = e;
  2874. test_md4[5] = f;
  2875. test_md4[6] = g;
  2876. wc_InitMd4(&md4);
  2877. for (i = 0; i < times; ++i) {
  2878. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  2879. wc_Md4Final(&md4, hash);
  2880. if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  2881. return WC_TEST_RET_ENC_I(i);
  2882. }
  2883. return 0;
  2884. }
  2885. #endif /* NO_MD4 */
  2886. #ifndef NO_SHA
  2887. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha_test(void)
  2888. {
  2889. wc_test_ret_t ret = 0;
  2890. wc_Sha sha, shaCopy;
  2891. byte hash[WC_SHA_DIGEST_SIZE];
  2892. byte hashcopy[WC_SHA_DIGEST_SIZE];
  2893. testVector a, b, c, d, e;
  2894. testVector test_sha[5];
  2895. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2896. WOLFSSL_ENTER("sha_test");
  2897. a.input = "";
  2898. a.output = "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18"
  2899. "\x90\xaf\xd8\x07\x09";
  2900. a.inLen = XSTRLEN(a.input);
  2901. a.outLen = WC_SHA_DIGEST_SIZE;
  2902. b.input = "abc";
  2903. b.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  2904. "\x6C\x9C\xD0\xD8\x9D";
  2905. b.inLen = XSTRLEN(b.input);
  2906. b.outLen = WC_SHA_DIGEST_SIZE;
  2907. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2908. c.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  2909. "\xE5\xE5\x46\x70\xF1";
  2910. c.inLen = XSTRLEN(c.input);
  2911. c.outLen = WC_SHA_DIGEST_SIZE;
  2912. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2913. "aaaaaa";
  2914. d.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  2915. "\x2A\x25\xEC\x64\x4D";
  2916. d.inLen = XSTRLEN(d.input);
  2917. d.outLen = WC_SHA_DIGEST_SIZE;
  2918. e.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2919. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2920. "aaaaaaaaaa";
  2921. e.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  2922. "\x53\x99\x5E\x26\xA0";
  2923. e.inLen = XSTRLEN(e.input);
  2924. e.outLen = WC_SHA_DIGEST_SIZE;
  2925. test_sha[0] = a;
  2926. test_sha[1] = b;
  2927. test_sha[2] = c;
  2928. test_sha[3] = d;
  2929. test_sha[4] = e;
  2930. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  2931. if (ret != 0)
  2932. return WC_TEST_RET_ENC_EC(ret);
  2933. ret = wc_InitSha_ex(&shaCopy, HEAP_HINT, devId);
  2934. if (ret != 0) {
  2935. wc_ShaFree(&sha);
  2936. return WC_TEST_RET_ENC_EC(ret);
  2937. }
  2938. for (i = 0; i < times; ++i) {
  2939. ret = wc_ShaUpdate(&sha, (byte*)test_sha[i].input,
  2940. (word32)test_sha[i].inLen);
  2941. if (ret != 0)
  2942. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2943. ret = wc_ShaGetHash(&sha, hashcopy);
  2944. if (ret != 0)
  2945. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2946. ret = wc_ShaCopy(&sha, &shaCopy);
  2947. if (ret != 0)
  2948. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2949. ret = wc_ShaFinal(&sha, hash);
  2950. if (ret != 0)
  2951. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2952. wc_ShaFree(&shaCopy);
  2953. if (XMEMCMP(hash, test_sha[i].output, WC_SHA_DIGEST_SIZE) != 0)
  2954. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2955. if (XMEMCMP(hash, hashcopy, WC_SHA_DIGEST_SIZE) != 0)
  2956. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2957. }
  2958. #ifndef NO_LARGE_HASH_TEST
  2959. /* BEGIN LARGE HASH TEST */ {
  2960. byte large_input[1024];
  2961. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2962. defined(HASH_SIZE_LIMIT)
  2963. const char* large_digest =
  2964. "\x1d\x6a\x5a\xf6\xe5\x7c\x86\xce\x7f\x7c\xaf\xd5\xdb\x08\xcd\x59"
  2965. "\x15\x8c\x6d\xb6";
  2966. #else
  2967. const char* large_digest =
  2968. "\x8b\x77\x02\x48\x39\xe8\xdb\xd3\x9a\xf4\x05\x24\x66\x12\x2d\x9e"
  2969. "\xc5\xd9\x0a\xac";
  2970. #endif
  2971. for (i = 0; i < (int)sizeof(large_input); i++) {
  2972. large_input[i] = (byte)(i & 0xFF);
  2973. }
  2974. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2975. defined(HASH_SIZE_LIMIT)
  2976. times = 20;
  2977. #else
  2978. times = 100;
  2979. #endif
  2980. #ifdef WOLFSSL_PIC32MZ_HASH
  2981. wc_ShaSizeSet(&sha, times * sizeof(large_input));
  2982. #endif
  2983. for (i = 0; i < times; ++i) {
  2984. ret = wc_ShaUpdate(&sha, (byte*)large_input,
  2985. (word32)sizeof(large_input));
  2986. if (ret != 0)
  2987. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2988. }
  2989. ret = wc_ShaFinal(&sha, hash);
  2990. if (ret != 0)
  2991. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2992. if (XMEMCMP(hash, large_digest, WC_SHA_DIGEST_SIZE) != 0)
  2993. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  2994. } /* END LARGE HASH TEST */
  2995. #endif /* NO_LARGE_HASH_TEST */
  2996. exit:
  2997. wc_ShaFree(&sha);
  2998. wc_ShaFree(&shaCopy);
  2999. return ret;
  3000. }
  3001. #endif /* NO_SHA */
  3002. #ifdef WOLFSSL_RIPEMD
  3003. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ripemd_test(void)
  3004. {
  3005. RipeMd ripemd;
  3006. wc_test_ret_t ret;
  3007. byte hash[RIPEMD_DIGEST_SIZE];
  3008. testVector a, b, c, d;
  3009. testVector test_ripemd[4];
  3010. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  3011. WOLFSSL_ENTER("ripemd_test");
  3012. a.input = "abc";
  3013. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  3014. "\xb0\x87\xf1\x5a\x0b\xfc";
  3015. a.inLen = XSTRLEN(a.input);
  3016. a.outLen = RIPEMD_DIGEST_SIZE;
  3017. b.input = "message digest";
  3018. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  3019. "\x5f\xfa\x21\x59\x5f\x36";
  3020. b.inLen = XSTRLEN(b.input);
  3021. b.outLen = RIPEMD_DIGEST_SIZE;
  3022. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3023. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  3024. "\xf4\x9a\xda\x62\xeb\x2b";
  3025. c.inLen = XSTRLEN(c.input);
  3026. c.outLen = RIPEMD_DIGEST_SIZE;
  3027. d.input = "12345678901234567890123456789012345678901234567890123456"
  3028. "789012345678901234567890";
  3029. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  3030. "\x82\xbf\x63\x32\x6b\xfb";
  3031. d.inLen = XSTRLEN(d.input);
  3032. d.outLen = RIPEMD_DIGEST_SIZE;
  3033. test_ripemd[0] = a;
  3034. test_ripemd[1] = b;
  3035. test_ripemd[2] = c;
  3036. test_ripemd[3] = d;
  3037. ret = wc_InitRipeMd(&ripemd);
  3038. if (ret != 0) {
  3039. return WC_TEST_RET_ENC_EC(ret);
  3040. }
  3041. for (i = 0; i < times; ++i) {
  3042. ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  3043. (word32)test_ripemd[i].inLen);
  3044. if (ret != 0) {
  3045. return WC_TEST_RET_ENC_I(i);
  3046. }
  3047. ret = wc_RipeMdFinal(&ripemd, hash);
  3048. if (ret != 0) {
  3049. return WC_TEST_RET_ENC_I(i);
  3050. }
  3051. if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  3052. return WC_TEST_RET_ENC_I(i);
  3053. }
  3054. return 0;
  3055. }
  3056. #endif /* WOLFSSL_RIPEMD */
  3057. #ifdef HAVE_BLAKE2
  3058. #define BLAKE2B_TESTS 3
  3059. static const byte blake2b_vec[BLAKE2B_TESTS][BLAKE2B_OUTBYTES] =
  3060. {
  3061. {
  3062. 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
  3063. 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
  3064. 0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
  3065. 0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
  3066. 0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
  3067. 0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
  3068. 0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
  3069. 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
  3070. },
  3071. {
  3072. 0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
  3073. 0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
  3074. 0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
  3075. 0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
  3076. 0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
  3077. 0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
  3078. 0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
  3079. 0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
  3080. },
  3081. {
  3082. 0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
  3083. 0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
  3084. 0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
  3085. 0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
  3086. 0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
  3087. 0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
  3088. 0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
  3089. 0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
  3090. }
  3091. };
  3092. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2b_test(void)
  3093. {
  3094. Blake2b b2b;
  3095. byte digest[64];
  3096. byte input[64];
  3097. int i, ret;
  3098. WOLFSSL_ENTER("blake2b_test");
  3099. for (i = 0; i < (int)sizeof(input); i++)
  3100. input[i] = (byte)i;
  3101. for (i = 0; i < BLAKE2B_TESTS; i++) {
  3102. ret = wc_InitBlake2b(&b2b, 64);
  3103. if (ret != 0)
  3104. return WC_TEST_RET_ENC_I(i);
  3105. ret = wc_Blake2bUpdate(&b2b, input, (word32)i);
  3106. if (ret != 0)
  3107. return WC_TEST_RET_ENC_I(i);
  3108. ret = wc_Blake2bFinal(&b2b, digest, 64);
  3109. if (ret != 0)
  3110. return WC_TEST_RET_ENC_I(i);
  3111. if (XMEMCMP(digest, blake2b_vec[i], 64) != 0) {
  3112. return WC_TEST_RET_ENC_I(i);
  3113. }
  3114. }
  3115. return 0;
  3116. }
  3117. #endif /* HAVE_BLAKE2 */
  3118. #ifdef HAVE_BLAKE2S
  3119. #define BLAKE2S_TESTS 3
  3120. static const byte blake2s_vec[BLAKE2S_TESTS][BLAKE2S_OUTBYTES] =
  3121. {
  3122. {
  3123. 0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94,
  3124. 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
  3125. 0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e,
  3126. 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
  3127. },
  3128. {
  3129. 0xe3, 0x4d, 0x74, 0xdb, 0xaf, 0x4f, 0xf4, 0xc6,
  3130. 0xab, 0xd8, 0x71, 0xcc, 0x22, 0x04, 0x51, 0xd2,
  3131. 0xea, 0x26, 0x48, 0x84, 0x6c, 0x77, 0x57, 0xfb,
  3132. 0xaa, 0xc8, 0x2f, 0xe5, 0x1a, 0xd6, 0x4b, 0xea,
  3133. },
  3134. {
  3135. 0xdd, 0xad, 0x9a, 0xb1, 0x5d, 0xac, 0x45, 0x49,
  3136. 0xba, 0x42, 0xf4, 0x9d, 0x26, 0x24, 0x96, 0xbe,
  3137. 0xf6, 0xc0, 0xba, 0xe1, 0xdd, 0x34, 0x2a, 0x88,
  3138. 0x08, 0xf8, 0xea, 0x26, 0x7c, 0x6e, 0x21, 0x0c,
  3139. }
  3140. };
  3141. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2s_test(void)
  3142. {
  3143. Blake2s b2s;
  3144. byte digest[32];
  3145. byte input[64];
  3146. int i, ret;
  3147. WOLFSSL_ENTER("blake2s_test");
  3148. for (i = 0; i < (int)sizeof(input); i++)
  3149. input[i] = (byte)i;
  3150. for (i = 0; i < BLAKE2S_TESTS; i++) {
  3151. ret = wc_InitBlake2s(&b2s, 32);
  3152. if (ret != 0)
  3153. return WC_TEST_RET_ENC_I(i);
  3154. ret = wc_Blake2sUpdate(&b2s, input, (word32)i);
  3155. if (ret != 0)
  3156. return WC_TEST_RET_ENC_I(i);
  3157. ret = wc_Blake2sFinal(&b2s, digest, 32);
  3158. if (ret != 0)
  3159. return WC_TEST_RET_ENC_I(i);
  3160. if (XMEMCMP(digest, blake2s_vec[i], 32) != 0) {
  3161. return WC_TEST_RET_ENC_I(i);
  3162. }
  3163. }
  3164. return 0;
  3165. }
  3166. #endif /* HAVE_BLAKE2S */
  3167. #ifdef WOLFSSL_SHA224
  3168. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha224_test(void)
  3169. {
  3170. wc_Sha224 sha, shaCopy;
  3171. byte hash[WC_SHA224_DIGEST_SIZE];
  3172. byte hashcopy[WC_SHA224_DIGEST_SIZE];
  3173. wc_test_ret_t ret = 0;
  3174. testVector a, b, c;
  3175. testVector test_sha[3];
  3176. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3177. WOLFSSL_ENTER("sha224_test");
  3178. a.input = "";
  3179. a.output = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34"
  3180. "\xc4\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
  3181. a.inLen = XSTRLEN(a.input);
  3182. a.outLen = WC_SHA224_DIGEST_SIZE;
  3183. b.input = "abc";
  3184. b.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
  3185. "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  3186. b.inLen = XSTRLEN(b.input);
  3187. b.outLen = WC_SHA224_DIGEST_SIZE;
  3188. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3189. c.output = "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  3190. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  3191. c.inLen = XSTRLEN(c.input);
  3192. c.outLen = WC_SHA224_DIGEST_SIZE;
  3193. test_sha[0] = a;
  3194. test_sha[1] = b;
  3195. test_sha[2] = c;
  3196. ret = wc_InitSha224_ex(&sha, HEAP_HINT, devId);
  3197. if (ret != 0)
  3198. return WC_TEST_RET_ENC_EC(ret);
  3199. ret = wc_InitSha224_ex(&shaCopy, HEAP_HINT, devId);
  3200. if (ret != 0) {
  3201. wc_Sha224Free(&sha);
  3202. return WC_TEST_RET_ENC_EC(ret);
  3203. }
  3204. for (i = 0; i < times; ++i) {
  3205. ret = wc_Sha224Update(&sha, (byte*)test_sha[i].input,
  3206. (word32)test_sha[i].inLen);
  3207. if (ret != 0)
  3208. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3209. ret = wc_Sha224GetHash(&sha, hashcopy);
  3210. if (ret != 0)
  3211. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3212. ret = wc_Sha224Copy(&sha, &shaCopy);
  3213. if (ret != 0)
  3214. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3215. ret = wc_Sha224Final(&sha, hash);
  3216. if (ret != 0)
  3217. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3218. wc_Sha224Free(&shaCopy);
  3219. if (XMEMCMP(hash, test_sha[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  3220. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3221. if (XMEMCMP(hash, hashcopy, WC_SHA224_DIGEST_SIZE) != 0)
  3222. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3223. }
  3224. exit:
  3225. wc_Sha224Free(&sha);
  3226. wc_Sha224Free(&shaCopy);
  3227. return ret;
  3228. }
  3229. #endif
  3230. #ifndef NO_SHA256
  3231. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha256_test(void)
  3232. {
  3233. wc_Sha256 sha, shaCopy;
  3234. byte hash[WC_SHA256_DIGEST_SIZE];
  3235. byte hashcopy[WC_SHA256_DIGEST_SIZE];
  3236. wc_test_ret_t ret = 0;
  3237. testVector a, b, c, d;
  3238. testVector test_sha[4];
  3239. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3240. byte i_hash[WC_SHA256_DIGEST_SIZE];
  3241. byte i_hashcopy[WC_SHA256_DIGEST_SIZE];
  3242. testVector interleave_test_sha[4];
  3243. wc_Sha256 i_sha, i_shaCopy;
  3244. #endif
  3245. #ifndef NO_LARGE_HASH_TEST
  3246. #define LARGE_HASH_TEST_INPUT_SZ 1024
  3247. #ifdef WOLFSSL_SMALL_STACK
  3248. byte *large_input = NULL;
  3249. #else
  3250. byte large_input[LARGE_HASH_TEST_INPUT_SZ];
  3251. #endif
  3252. #endif
  3253. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3254. WOLFSSL_ENTER("sha256_test");
  3255. a.input = "";
  3256. a.output = "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9"
  3257. "\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52"
  3258. "\xb8\x55";
  3259. a.inLen = XSTRLEN(a.input);
  3260. a.outLen = WC_SHA256_DIGEST_SIZE;
  3261. b.input = "abc";
  3262. b.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  3263. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  3264. "\x15\xAD";
  3265. b.inLen = XSTRLEN(b.input);
  3266. b.outLen = WC_SHA256_DIGEST_SIZE;
  3267. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3268. c.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  3269. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  3270. "\x06\xC1";
  3271. c.inLen = XSTRLEN(c.input);
  3272. c.outLen = WC_SHA256_DIGEST_SIZE;
  3273. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  3274. "aaaaaa"; /* this is BLOCKSIZE length */
  3275. d.output = "\xFF\xE0\x54\xFE\x7A\xE0\xCB\x6D\xC6\x5C\x3A\xF9\xB6\x1D\x52"
  3276. "\x09\xF4\x39\x85\x1D\xB4\x3D\x0B\xA5\x99\x73\x37\xDF\x15\x46"
  3277. "\x68\xEB";
  3278. d.inLen = XSTRLEN(d.input);
  3279. d.outLen = WC_SHA256_DIGEST_SIZE;
  3280. test_sha[0] = a;
  3281. test_sha[1] = b;
  3282. test_sha[2] = c;
  3283. test_sha[3] = d;
  3284. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3285. interleave_test_sha[0] = a;
  3286. interleave_test_sha[1] = b;
  3287. interleave_test_sha[2] = c;
  3288. interleave_test_sha[3] = d;
  3289. #endif
  3290. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  3291. if (ret != 0)
  3292. return WC_TEST_RET_ENC_EC(ret);
  3293. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3294. ret = wc_InitSha256_ex(&i_sha, HEAP_HINT, devId);
  3295. if (ret != 0)
  3296. return WC_TEST_RET_ENC_EC(ret);
  3297. #endif
  3298. ret = wc_InitSha256_ex(&shaCopy, HEAP_HINT, devId);
  3299. if (ret != 0) {
  3300. wc_Sha256Free(&sha);
  3301. return WC_TEST_RET_ENC_EC(ret);
  3302. }
  3303. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3304. ret = wc_InitSha256_ex(&i_shaCopy, HEAP_HINT, devId);
  3305. if (ret != 0) {
  3306. wc_Sha256Free(&sha);
  3307. wc_Sha256Free(&i_sha);
  3308. return WC_TEST_RET_ENC_EC(ret);
  3309. }
  3310. #endif
  3311. for (i = 0; i < times; ++i) {
  3312. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,
  3313. (word32)test_sha[i].inLen);
  3314. if (ret != 0) {
  3315. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3316. }
  3317. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3318. ret = wc_Sha256Update(&i_sha, (byte*)interleave_test_sha[i].input,
  3319. (word32)interleave_test_sha[i].inLen);
  3320. if (ret != 0) {
  3321. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3322. }
  3323. #endif
  3324. ret = wc_Sha256GetHash(&sha, hashcopy);
  3325. if (ret != 0)
  3326. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3327. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3328. ret = wc_Sha256GetHash(&i_sha, i_hashcopy);
  3329. if (ret != 0)
  3330. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3331. #endif
  3332. ret = wc_Sha256Copy(&sha, &shaCopy);
  3333. if (ret != 0)
  3334. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3335. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3336. ret = wc_Sha256Copy(&i_sha, &i_shaCopy);
  3337. if (ret != 0)
  3338. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3339. #endif
  3340. ret = wc_Sha256Final(&sha, hash);
  3341. if (ret != 0)
  3342. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3343. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3344. ret = wc_Sha256Final(&i_sha, i_hash);
  3345. if (ret != 0)
  3346. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3347. #endif
  3348. wc_Sha256Free(&shaCopy);
  3349. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3350. wc_Sha256Free(&i_shaCopy);
  3351. #endif
  3352. if (XMEMCMP(hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0) {
  3353. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3354. }
  3355. if (XMEMCMP(hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0) {
  3356. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3357. }
  3358. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3359. if (XMEMCMP(i_hash, interleave_test_sha[i].output,
  3360. WC_SHA256_DIGEST_SIZE) != 0) {
  3361. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3362. }
  3363. if (XMEMCMP(i_hash, i_hashcopy, WC_SHA256_DIGEST_SIZE) != 0) {
  3364. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3365. }
  3366. if (XMEMCMP(i_hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0) {
  3367. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3368. }
  3369. if (XMEMCMP(i_hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0) {
  3370. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3371. }
  3372. #endif
  3373. }
  3374. #ifndef NO_LARGE_HASH_TEST
  3375. /* BEGIN LARGE HASH TEST */ {
  3376. #ifdef HASH_SIZE_LIMIT
  3377. WOLFSSL_SMALL_STACK_STATIC const char* large_digest =
  3378. "\xa4\x75\x9e\x7a\xa2\x03\x38\x32\x88\x66\xa2\xea\x17\xea\xf8\xc7"
  3379. "\xfe\x4e\xc6\xbb\xe3\xbb\x71\xce\xe7\xdf\x7c\x04\x61\xb3\xc2\x2f";
  3380. #else
  3381. WOLFSSL_SMALL_STACK_STATIC const char* large_digest =
  3382. "\x27\x78\x3e\x87\x96\x3a\x4e\xfb\x68\x29\xb5\x31\xc9\xba\x57\xb4"
  3383. "\x4f\x45\x79\x7f\x67\x70\xbd\x63\x7f\xbf\x0d\x80\x7c\xbd\xba\xe0";
  3384. #endif
  3385. #ifdef WOLFSSL_SMALL_STACK
  3386. large_input = (byte *)XMALLOC(LARGE_HASH_TEST_INPUT_SZ, HEAP_HINT,
  3387. DYNAMIC_TYPE_TMP_BUFFER);
  3388. if (large_input == NULL) {
  3389. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), exit);
  3390. }
  3391. #endif
  3392. for (i = 0; i < LARGE_HASH_TEST_INPUT_SZ; i++) {
  3393. large_input[i] = (byte)(i & 0xFF);
  3394. }
  3395. #ifdef HASH_SIZE_LIMIT
  3396. times = 20;
  3397. #else
  3398. times = 100;
  3399. #endif
  3400. #ifdef WOLFSSL_PIC32MZ_HASH
  3401. wc_Sha256SizeSet(&sha, times * LARGE_HASH_TEST_INPUT_SZ);
  3402. #endif
  3403. for (i = 0; i < times; ++i) {
  3404. ret = wc_Sha256Update(&sha, (byte*)large_input,
  3405. LARGE_HASH_TEST_INPUT_SZ);
  3406. if (ret != 0)
  3407. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3408. }
  3409. ret = wc_Sha256Final(&sha, hash);
  3410. if (ret != 0)
  3411. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3412. if (XMEMCMP(hash, large_digest, WC_SHA256_DIGEST_SIZE) != 0)
  3413. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3414. } /* END LARGE HASH TEST */
  3415. #undef LARGE_HASH_TEST_INPUT_SZ
  3416. #endif /* NO_LARGE_HASH_TEST */
  3417. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_FULL_HASH)
  3418. unsigned char data_hb[WC_SHA256_BLOCK_SIZE] = {
  3419. 0x61, 0x62, 0x63, 0x80, 0x00, 0x00, 0x00, 0x00,
  3420. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3421. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3422. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3423. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3424. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3425. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3426. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18
  3427. };
  3428. ret = wc_Sha256HashBlock(&sha, data_hb, hash);
  3429. if (ret != 0) {
  3430. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3431. }
  3432. if (XMEMCMP(hash, b.output, WC_SHA256_DIGEST_SIZE) != 0) {
  3433. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3434. }
  3435. #endif
  3436. exit:
  3437. #if !defined(NO_LARGE_HASH_TEST) && defined(WOLFSSL_SMALL_STACK)
  3438. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3439. #endif
  3440. wc_Sha256Free(&sha);
  3441. wc_Sha256Free(&shaCopy);
  3442. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3443. wc_Sha256Free(&i_sha);
  3444. wc_Sha256Free(&i_shaCopy);
  3445. #endif
  3446. return ret;
  3447. }
  3448. #endif
  3449. #ifdef WOLFSSL_SHA512
  3450. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_test(void)
  3451. {
  3452. /*
  3453. ** See https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512.pdf
  3454. */
  3455. wc_Sha512 sha, shaCopy;
  3456. byte hash[WC_SHA512_DIGEST_SIZE];
  3457. byte hashcopy[WC_SHA512_DIGEST_SIZE];
  3458. wc_test_ret_t ret = 0;
  3459. testVector a, b, c;
  3460. testVector test_sha[3];
  3461. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3462. wc_Sha512 i_sha, i_shaCopy;
  3463. byte i_hash[WC_SHA512_DIGEST_SIZE];
  3464. byte i_hashcopy[WC_SHA512_DIGEST_SIZE];
  3465. testVector interleave_test_sha[3];
  3466. #endif
  3467. #ifndef NO_LARGE_HASH_TEST
  3468. #define LARGE_HASH_TEST_INPUT_SZ 1024
  3469. #ifdef WOLFSSL_SMALL_STACK
  3470. byte *large_input = NULL;
  3471. #else
  3472. byte large_input[LARGE_HASH_TEST_INPUT_SZ];
  3473. #endif
  3474. #endif
  3475. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3476. WOLFSSL_ENTER("sha512_test");
  3477. a.input = "";
  3478. a.output = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80"
  3479. "\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c"
  3480. "\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87"
  3481. "\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a"
  3482. "\xf9\x27\xda\x3e";
  3483. a.inLen = XSTRLEN(a.input);
  3484. a.outLen = WC_SHA512_DIGEST_SIZE;
  3485. b.input = "abc";
  3486. b.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  3487. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  3488. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  3489. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  3490. "\xa5\x4c\xa4\x9f";
  3491. b.inLen = XSTRLEN(b.input);
  3492. b.outLen = WC_SHA512_DIGEST_SIZE;
  3493. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3494. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3495. c.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  3496. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  3497. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  3498. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  3499. "\x87\x4b\xe9\x09";
  3500. c.inLen = XSTRLEN(c.input);
  3501. c.outLen = WC_SHA512_DIGEST_SIZE;
  3502. test_sha[0] = a;
  3503. test_sha[1] = b;
  3504. test_sha[2] = c;
  3505. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3506. interleave_test_sha[0] = a;
  3507. interleave_test_sha[1] = b;
  3508. interleave_test_sha[2] = c;
  3509. #endif
  3510. ret = wc_InitSha512_ex(&sha, HEAP_HINT, devId);
  3511. if (ret != 0)
  3512. return WC_TEST_RET_ENC_EC(ret);
  3513. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3514. ret = wc_InitSha512_ex(&i_sha, HEAP_HINT, devId);
  3515. if (ret != 0)
  3516. return WC_TEST_RET_ENC_EC(ret);
  3517. #endif
  3518. ret = wc_InitSha512_ex(&shaCopy, HEAP_HINT, devId);
  3519. if (ret != 0) {
  3520. wc_Sha512Free(&sha);
  3521. return WC_TEST_RET_ENC_EC(ret);
  3522. }
  3523. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3524. ret = wc_InitSha512_ex(&i_shaCopy, HEAP_HINT, devId);
  3525. if (ret != 0) {
  3526. wc_Sha512Free(&sha);
  3527. wc_Sha512Free(&i_sha);
  3528. return WC_TEST_RET_ENC_EC(ret);
  3529. }
  3530. #endif
  3531. for (i = 0; i < times; ++i) {
  3532. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,
  3533. (word32)test_sha[i].inLen);
  3534. if (ret != 0)
  3535. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3536. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3537. ret = wc_Sha512Update(&i_sha, (byte*)interleave_test_sha[i].input,
  3538. (word32)interleave_test_sha[i].inLen);
  3539. if (ret != 0)
  3540. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3541. #endif
  3542. ret = wc_Sha512GetHash(&sha, hashcopy);
  3543. if (ret != 0)
  3544. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3545. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3546. ret = wc_Sha512GetHash(&i_sha, i_hashcopy);
  3547. if (ret != 0)
  3548. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3549. #endif
  3550. ret = wc_Sha512Copy(&sha, &shaCopy);
  3551. if (ret != 0)
  3552. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3553. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3554. ret = wc_Sha512Copy(&i_sha, &i_shaCopy);
  3555. if (ret != 0)
  3556. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3557. #endif
  3558. ret = wc_Sha512Final(&sha, hash);
  3559. if (ret != 0)
  3560. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3561. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3562. ret = wc_Sha512Final(&i_sha, i_hash);
  3563. if (ret != 0)
  3564. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3565. #endif
  3566. wc_Sha512Free(&shaCopy);
  3567. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3568. wc_Sha512Free(&i_shaCopy);
  3569. #endif
  3570. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  3571. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3572. if (XMEMCMP(hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0)
  3573. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3574. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3575. if (XMEMCMP(i_hash, interleave_test_sha[i].output,
  3576. WC_SHA512_DIGEST_SIZE) != 0) {
  3577. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3578. }
  3579. if (XMEMCMP(i_hash, i_hashcopy, WC_SHA512_DIGEST_SIZE) != 0) {
  3580. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3581. }
  3582. if (XMEMCMP(i_hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0) {
  3583. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3584. }
  3585. if (XMEMCMP(i_hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0) {
  3586. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3587. }
  3588. #endif
  3589. }
  3590. #ifndef NO_LARGE_HASH_TEST
  3591. /* BEGIN LARGE HASH TEST */ {
  3592. #ifdef HASH_SIZE_LIMIT
  3593. WOLFSSL_SMALL_STACK_STATIC const char* large_digest =
  3594. "\x30\x9B\x96\xA6\xE9\x43\x78\x30\xA3\x71\x51\x61\xC1\xEB\xE1\xBE"
  3595. "\xC8\xA5\xF9\x13\x5A\xD6\x6D\x9E\x46\x31\x31\x67\x8D\xE2\xC0\x0B"
  3596. "\x2A\x1A\x03\xE1\xF3\x48\xA7\x33\xBD\x49\xF8\xFF\xF1\xC2\xC2\x95"
  3597. "\xCB\xF0\xAF\x87\x61\x85\x58\x63\x6A\xCA\x70\x9C\x8B\x83\x3F\x5D";
  3598. #else
  3599. WOLFSSL_SMALL_STACK_STATIC const char* large_digest =
  3600. "\x5a\x1f\x73\x90\xbd\x8c\xe4\x63\x54\xce\xa0\x9b\xef\x32\x78\x2d"
  3601. "\x2e\xe7\x0d\x5e\x2f\x9d\x15\x1b\xdd\x2d\xde\x65\x0c\x7b\xfa\x83"
  3602. "\x5e\x80\x02\x13\x84\xb8\x3f\xff\x71\x62\xb5\x09\x89\x63\xe1\xdc"
  3603. "\xa5\xdc\xfc\xfa\x9d\x1a\x4d\xc0\xfa\x3a\x14\xf6\x01\x51\x90\xa4";
  3604. #endif
  3605. #ifdef WOLFSSL_SMALL_STACK
  3606. large_input = (byte *)XMALLOC(LARGE_HASH_TEST_INPUT_SZ, HEAP_HINT,
  3607. DYNAMIC_TYPE_TMP_BUFFER);
  3608. if (large_input == NULL) {
  3609. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), exit);
  3610. }
  3611. #endif
  3612. for (i = 0; i < LARGE_HASH_TEST_INPUT_SZ; i++) {
  3613. large_input[i] = (byte)(i & 0xFF);
  3614. }
  3615. #ifdef HASH_SIZE_LIMIT
  3616. times = 20;
  3617. #else
  3618. times = 100;
  3619. #endif
  3620. for (i = 0; i < times; ++i) {
  3621. ret = wc_Sha512Update(&sha, (byte*)large_input,
  3622. LARGE_HASH_TEST_INPUT_SZ);
  3623. if (ret != 0)
  3624. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3625. }
  3626. ret = wc_Sha512Final(&sha, hash);
  3627. if (ret != 0)
  3628. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3629. if (XMEMCMP(hash, large_digest, WC_SHA512_DIGEST_SIZE) != 0)
  3630. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3631. #ifndef NO_UNALIGNED_MEMORY_TEST
  3632. /* Unaligned memory access test */
  3633. for (i = 1; i < 16; i++) {
  3634. ret = wc_Sha512Update(&sha, (byte*)large_input + i,
  3635. LARGE_HASH_TEST_INPUT_SZ - i);
  3636. if (ret != 0)
  3637. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3638. ret = wc_Sha512Final(&sha, hash);
  3639. }
  3640. #endif
  3641. } /* END LARGE HASH TEST */
  3642. #undef LARGE_HASH_TEST_INPUT_SZ
  3643. #endif /* NO_LARGE_HASH_TEST */
  3644. exit:
  3645. #if !defined(NO_LARGE_HASH_TEST) && defined(WOLFSSL_SMALL_STACK)
  3646. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3647. #endif
  3648. wc_Sha512Free(&sha);
  3649. wc_Sha512Free(&shaCopy);
  3650. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3651. wc_Sha512Free(&i_sha);
  3652. wc_Sha512Free(&i_shaCopy);
  3653. #endif
  3654. return ret;
  3655. }
  3656. #if !defined(WOLFSSL_NOSHA512_224) && \
  3657. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  3658. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_224_test(void)
  3659. {
  3660. /*
  3661. ** See https://csrc.nist.gov/Projects/cryptographic-standards-and-guidelines/example-values
  3662. **
  3663. ** NIST SHA512/224 Document Example:
  3664. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512_224.pdf
  3665. */
  3666. wc_Sha512 sha, shaCopy;
  3667. byte hash[WC_SHA512_224_DIGEST_SIZE];
  3668. byte hashcopy[WC_SHA512_224_DIGEST_SIZE];
  3669. wc_test_ret_t ret = 0;
  3670. testVector a, b, c;
  3671. testVector test_sha[3];
  3672. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3673. WOLFSSL_ENTER("sha512_224_test");
  3674. a.input = "";
  3675. a.output = "\x6e\xd0\xdd\x02"
  3676. "\x80\x6f\xa8\x9e" "\x25\xde\x06\x0c"
  3677. "\x19\xd3\xac\x86" "\xca\xbb\x87\xd6"
  3678. "\xa0\xdd\xd0\x5c" "\x33\x3b\x84\xf4";
  3679. a.inLen = XSTRLEN(a.input);
  3680. a.outLen = WC_SHA512_224_DIGEST_SIZE;
  3681. /*
  3682. ** See page 1 in above document for the SHA512/224 "abc" test:
  3683. */
  3684. b.input = "abc";
  3685. /*
  3686. ** See page 1 in above document for the SHA512/224 "abc" test digest:
  3687. */
  3688. b.output = "\x46\x34\x27\x0f"
  3689. "\x70\x7b\x6a\x54" "\xda\xae\x75\x30"
  3690. "\x46\x08\x42\xe2" "\x0e\x37\xed\x26"
  3691. "\x5c\xee\xe9\xa4" "\x3e\x89\x24\xaa";
  3692. b.inLen = XSTRLEN(b.input);
  3693. b.outLen = WC_SHA512_224_DIGEST_SIZE;
  3694. /*
  3695. ** See page 4 in above for the 2-block test:
  3696. */
  3697. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3698. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3699. /*
  3700. ** see page 9 in above document for the 2-block test message digest:
  3701. */
  3702. c.output = "\x23\xfe\xc5\xbb"
  3703. "\x94\xd6\x0b\x23" "\x30\x81\x92\x64"
  3704. "\x0b\x0c\x45\x33" "\x35\xd6\x64\x73"
  3705. "\x4f\xe4\x0e\x72" "\x68\x67\x4a\xf9";
  3706. c.inLen = XSTRLEN(c.input);
  3707. c.outLen = WC_SHA512_224_DIGEST_SIZE;
  3708. test_sha[0] = a;
  3709. test_sha[1] = b;
  3710. test_sha[2] = c;
  3711. ret = wc_InitSha512_224_ex(&sha, HEAP_HINT, devId);
  3712. if (ret != 0)
  3713. return WC_TEST_RET_ENC_EC(ret);
  3714. ret = wc_InitSha512_224_ex(&shaCopy, HEAP_HINT, devId);
  3715. if (ret != 0) {
  3716. wc_Sha512_224Free(&sha);
  3717. return WC_TEST_RET_ENC_EC(ret);
  3718. }
  3719. for (i = 0; i < times; ++i) {
  3720. ret = wc_Sha512_224Update(&sha, (byte*)test_sha[i].input,
  3721. (word32)test_sha[i].inLen);
  3722. if (ret != 0)
  3723. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3724. ret = wc_Sha512_224GetHash(&sha, hashcopy);
  3725. if (ret != 0)
  3726. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3727. ret = wc_Sha512_224Copy(&sha, &shaCopy);
  3728. if (ret != 0)
  3729. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3730. ret = wc_Sha512_224Final(&sha, hash);
  3731. if (ret != 0)
  3732. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3733. wc_Sha512_224Free(&shaCopy);
  3734. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_224_DIGEST_SIZE) != 0)
  3735. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3736. if (XMEMCMP(hash, hashcopy, WC_SHA512_224_DIGEST_SIZE) != 0)
  3737. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3738. }
  3739. #ifndef NO_LARGE_HASH_TEST
  3740. /* BEGIN LARGE HASH TEST */ {
  3741. byte large_input[1024];
  3742. #ifdef HASH_SIZE_LIMIT
  3743. const char* large_digest =
  3744. "\x98\x68\xc3\xd9\xb9\xef\x17\x53"
  3745. "\x43\x66\x0e\x60\xdf\x29\xf8\xef"
  3746. "\x96\xe3\x93\x34\x8c\x6f\xc0\xeb"
  3747. "\x14\x6c\xcf\x6a";
  3748. #else
  3749. const char* large_digest =
  3750. "\x26\x5f\x98\xd1\x76\x49\x71\x4e"
  3751. "\x82\xb7\x9d\x52\x32\x67\x9d\x56"
  3752. "\x91\xf5\x88\xc3\x05\xbb\x3f\x90"
  3753. "\xe2\x4e\x85\x05";
  3754. #endif
  3755. for (i = 0; i < (int)sizeof(large_input); i++) {
  3756. large_input[i] = (byte)(i & 0xFF);
  3757. }
  3758. #ifdef HASH_SIZE_LIMIT
  3759. times = 20;
  3760. #else
  3761. times = 100;
  3762. #endif
  3763. for (i = 0; i < times; ++i) {
  3764. ret = wc_Sha512_224Update(&sha, (byte*)large_input,
  3765. (word32)sizeof(large_input));
  3766. if (ret != 0)
  3767. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3768. }
  3769. ret = wc_Sha512_224Final(&sha, hash);
  3770. if (ret != 0)
  3771. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3772. if (XMEMCMP(hash, large_digest, WC_SHA512_224_DIGEST_SIZE) != 0)
  3773. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3774. #ifndef NO_UNALIGNED_MEMORY_TEST
  3775. /* Unaligned memory access test */
  3776. for (i = 1; i < 16; i++) {
  3777. ret = wc_Sha512_224Update(&sha, (byte*)large_input + i,
  3778. (word32)sizeof(large_input) - i);
  3779. if (ret != 0)
  3780. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3781. ret = wc_Sha512_224Final(&sha, hash);
  3782. }
  3783. #endif
  3784. } /* END LARGE HASH TEST */
  3785. #endif /* NO_LARGE_HASH_TEST */
  3786. exit:
  3787. wc_Sha512_224Free(&sha);
  3788. wc_Sha512_224Free(&shaCopy);
  3789. return ret;
  3790. } /* sha512_224_test */
  3791. #endif /* !defined(WOLFSSL_NOSHA512_224) && !FIPS ... */
  3792. #if !defined(WOLFSSL_NOSHA512_256) && \
  3793. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  3794. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_256_test(void)
  3795. {
  3796. /*
  3797. ** See https://csrc.nist.gov/Projects/cryptographic-standards-and-guidelines/example-values
  3798. ** NIST SHA512/256 Document Example:
  3799. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512_256.pdf
  3800. */
  3801. wc_Sha512 sha, shaCopy;
  3802. byte hash[WC_SHA512_256_DIGEST_SIZE];
  3803. byte hashcopy[WC_SHA512_256_DIGEST_SIZE];
  3804. wc_test_ret_t ret = 0;
  3805. testVector a, b, c;
  3806. testVector test_sha[3];
  3807. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3808. WOLFSSL_ENTER("sha512_256_test");
  3809. a.input = "";
  3810. a.output = "\xc6\x72\xb8\xd1" "\xef\x56\xed\x28"
  3811. "\xab\x87\xc3\x62" "\x2c\x51\x14\x06"
  3812. "\x9b\xdd\x3a\xd7" "\xb8\xf9\x73\x74"
  3813. "\x98\xd0\xc0\x1e" "\xce\xf0\x96\x7a";
  3814. a.inLen = XSTRLEN(a.input);
  3815. a.outLen = WC_SHA512_256_DIGEST_SIZE;
  3816. /*
  3817. ** See page 1 of above document for "abc" example:
  3818. */
  3819. b.input = "abc";
  3820. /*
  3821. ** See page 4 of above document for "abc" example digest:
  3822. */
  3823. b.output = "\x53\x04\x8e\x26" "\x81\x94\x1e\xf9"
  3824. "\x9b\x2e\x29\xb7" "\x6b\x4c\x7d\xab"
  3825. "\xe4\xc2\xd0\xc6" "\x34\xfc\x6d\x46"
  3826. "\xe0\xe2\xf1\x31" "\x07\xe7\xaf\x23";
  3827. b.inLen = XSTRLEN(b.input);
  3828. b.outLen = WC_SHA512_256_DIGEST_SIZE;
  3829. /*
  3830. ** See page 4 of above document for Two Block Message Sample:
  3831. */
  3832. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3833. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3834. /*
  3835. ** See page 10 of above document for Two Block Message Sample digest:
  3836. */
  3837. c.output = "\x39\x28\xe1\x84" "\xfb\x86\x90\xf8"
  3838. "\x40\xda\x39\x88" "\x12\x1d\x31\xbe"
  3839. "\x65\xcb\x9d\x3e" "\xf8\x3e\xe6\x14"
  3840. "\x6f\xea\xc8\x61" "\xe1\x9b\x56\x3a";
  3841. c.inLen = XSTRLEN(c.input);
  3842. c.outLen = WC_SHA512_256_DIGEST_SIZE;
  3843. test_sha[0] = a;
  3844. test_sha[1] = b;
  3845. test_sha[2] = c;
  3846. ret = wc_InitSha512_256_ex(&sha, HEAP_HINT, devId);
  3847. if (ret != 0)
  3848. return WC_TEST_RET_ENC_EC(ret);
  3849. ret = wc_InitSha512_256_ex(&shaCopy, HEAP_HINT, devId);
  3850. if (ret != 0) {
  3851. wc_Sha512_256Free(&sha);
  3852. return WC_TEST_RET_ENC_EC(ret);
  3853. }
  3854. for (i = 0; i < times; ++i) {
  3855. ret = wc_Sha512_256Update(&sha, (byte*)test_sha[i].input,
  3856. (word32)test_sha[i].inLen);
  3857. if (ret != 0)
  3858. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3859. ret = wc_Sha512_256GetHash(&sha, hashcopy);
  3860. if (ret != 0)
  3861. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3862. ret = wc_Sha512_256Copy(&sha, &shaCopy);
  3863. if (ret != 0)
  3864. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3865. ret = wc_Sha512_256Final(&sha, hash);
  3866. if (ret != 0)
  3867. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3868. wc_Sha512_256Free(&shaCopy);
  3869. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_256_DIGEST_SIZE) != 0)
  3870. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3871. if (XMEMCMP(hash, hashcopy, WC_SHA512_256_DIGEST_SIZE) != 0)
  3872. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3873. }
  3874. #ifndef NO_LARGE_HASH_TEST
  3875. /* BEGIN LARGE HASH TEST */ {
  3876. byte large_input[1024];
  3877. #ifdef HASH_SIZE_LIMIT
  3878. const char* large_digest =
  3879. "\x49\xcc\xbc\x7a\x93\x0b\x02\xb8"
  3880. "\xad\x9a\x46\x51\x00\x1f\x13\x80"
  3881. "\x35\x84\x36\xf1\xf2\x3c\xeb\xd8"
  3882. "\x41\xd4\x06\x8b\x1d\x19\xad\x72";
  3883. #else
  3884. const char* large_digest =
  3885. "\x7a\xe3\x84\x05\xcb\x06\x22\x08"
  3886. "\x7e\x2c\x65\x89\x1f\x26\x45\xfd"
  3887. "\xad\xbc\x2e\x29\x83\x12\x84\x4b"
  3888. "\xf2\xa0\xde\xbe\x06\x11\xd7\x44";
  3889. #endif
  3890. for (i = 0; i < (int)sizeof(large_input); i++) {
  3891. large_input[i] = (byte)(i & 0xFF);
  3892. }
  3893. #ifdef HASH_SIZE_LIMIT
  3894. times = 20;
  3895. #else
  3896. times = 100;
  3897. #endif
  3898. for (i = 0; i < times; ++i) {
  3899. ret = wc_Sha512_256Update(&sha, (byte*)large_input,
  3900. (word32)sizeof(large_input));
  3901. if (ret != 0)
  3902. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3903. }
  3904. ret = wc_Sha512_256Final(&sha, hash);
  3905. if (ret != 0)
  3906. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3907. if (XMEMCMP(hash, large_digest, WC_SHA512_256_DIGEST_SIZE) != 0)
  3908. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3909. #ifndef NO_UNALIGNED_MEMORY_TEST
  3910. /* Unaligned memory access test */
  3911. for (i = 1; i < 16; i++) {
  3912. ret = wc_Sha512_256Update(&sha, (byte*)large_input + i,
  3913. (word32)sizeof(large_input) - i);
  3914. if (ret != 0)
  3915. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3916. ret = wc_Sha512_256Final(&sha, hash);
  3917. }
  3918. #endif
  3919. } /* END LARGE HASH TEST */
  3920. #endif /* NO_LARGE_HASH_TEST */
  3921. exit:
  3922. wc_Sha512_256Free(&sha);
  3923. wc_Sha512_256Free(&shaCopy);
  3924. return ret;
  3925. } /* sha512_256_test */
  3926. #endif /* !defined(WOLFSSL_NOSHA512_256) && !FIPS ... */
  3927. #endif /* WOLFSSL_SHA512 */
  3928. #ifdef WOLFSSL_SHA384
  3929. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha384_test(void)
  3930. {
  3931. wc_Sha384 sha, shaCopy;
  3932. byte hash[WC_SHA384_DIGEST_SIZE];
  3933. byte hashcopy[WC_SHA384_DIGEST_SIZE];
  3934. wc_test_ret_t ret = 0;
  3935. testVector a, b, c;
  3936. testVector test_sha[3];
  3937. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3938. WOLFSSL_ENTER("sha384_test");
  3939. a.input = "";
  3940. a.output = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e\xb1\xb1\xe3"
  3941. "\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c\xc7\xbf\x63\xf6"
  3942. "\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb\xd5\x1a\xd2\xf1\x48"
  3943. "\x98\xb9\x5b";
  3944. a.inLen = XSTRLEN(a.input);
  3945. a.outLen = WC_SHA384_DIGEST_SIZE;
  3946. b.input = "abc";
  3947. b.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  3948. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  3949. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  3950. "\xc8\x25\xa7";
  3951. b.inLen = XSTRLEN(b.input);
  3952. b.outLen = WC_SHA384_DIGEST_SIZE;
  3953. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3954. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3955. c.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  3956. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  3957. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  3958. "\x74\x60\x39";
  3959. c.inLen = XSTRLEN(c.input);
  3960. c.outLen = WC_SHA384_DIGEST_SIZE;
  3961. test_sha[0] = a;
  3962. test_sha[1] = b;
  3963. test_sha[2] = c;
  3964. ret = wc_InitSha384_ex(&sha, HEAP_HINT, devId);
  3965. if (ret != 0)
  3966. return WC_TEST_RET_ENC_EC(ret);
  3967. ret = wc_InitSha384_ex(&shaCopy, HEAP_HINT, devId);
  3968. if (ret != 0) {
  3969. wc_Sha384Free(&sha);
  3970. return WC_TEST_RET_ENC_EC(ret);
  3971. }
  3972. for (i = 0; i < times; ++i) {
  3973. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,
  3974. (word32)test_sha[i].inLen);
  3975. if (ret != 0)
  3976. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3977. ret = wc_Sha384GetHash(&sha, hashcopy);
  3978. if (ret != 0)
  3979. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3980. ret = wc_Sha384Copy(&sha, &shaCopy);
  3981. if (ret != 0)
  3982. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3983. ret = wc_Sha384Final(&sha, hash);
  3984. if (ret != 0)
  3985. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3986. wc_Sha384Free(&shaCopy);
  3987. if (XMEMCMP(hash, test_sha[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  3988. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3989. if (XMEMCMP(hash, hashcopy, WC_SHA384_DIGEST_SIZE) != 0)
  3990. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3991. }
  3992. #ifndef NO_LARGE_HASH_TEST
  3993. /* BEGIN LARGE HASH TEST */ {
  3994. byte large_input[1024];
  3995. #ifdef HASH_SIZE_LIMIT
  3996. const char* large_digest =
  3997. "\xB5\xAD\x66\x6F\xD9\x58\x5E\x68\xDD\x5E\x30\xD3\x95\x72\x33\xA4"
  3998. "\xE9\x4B\x99\x3A\xEF\xF8\xE1\xBF\x1F\x05\x32\xAA\x16\x00\x82\xEC"
  3999. "\x15\xDA\xF2\x75\xEE\xE9\x06\xAF\x52\x8A\x5C\xEF\x72\x81\x80\xD6";
  4000. #else
  4001. const char* large_digest =
  4002. "\x37\x01\xdb\xff\x1e\x40\x4f\xe1\xe2\xea\x0b\x40\xbb\x3b\x39\x9a"
  4003. "\xcc\xe8\x44\x8e\x7e\xe5\x64\xb5\x6b\x7f\x56\x64\xa7\x2b\x84\xe3"
  4004. "\xc5\xd7\x79\x03\x25\x90\xf7\xa4\x58\xcb\x97\xa8\x8b\xb1\xa4\x81";
  4005. #endif
  4006. for (i = 0; i < (int)sizeof(large_input); i++) {
  4007. large_input[i] = (byte)(i & 0xFF);
  4008. }
  4009. #ifdef HASH_SIZE_LIMIT
  4010. times = 20;
  4011. #else
  4012. times = 100;
  4013. #endif
  4014. for (i = 0; i < times; ++i) {
  4015. ret = wc_Sha384Update(&sha, (byte*)large_input,
  4016. (word32)sizeof(large_input));
  4017. if (ret != 0)
  4018. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4019. }
  4020. ret = wc_Sha384Final(&sha, hash);
  4021. if (ret != 0)
  4022. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4023. if (XMEMCMP(hash, large_digest, WC_SHA384_DIGEST_SIZE) != 0)
  4024. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4025. } /* END LARGE HASH TEST */
  4026. #endif /* NO_LARGE_HASH_TEST */
  4027. exit:
  4028. wc_Sha384Free(&sha);
  4029. wc_Sha384Free(&shaCopy);
  4030. return ret;
  4031. }
  4032. #endif /* WOLFSSL_SHA384 */
  4033. #ifdef WOLFSSL_SHA3
  4034. #ifndef WOLFSSL_NOSHA3_224
  4035. static wc_test_ret_t sha3_224_test(void)
  4036. {
  4037. wc_Sha3 sha;
  4038. byte hash[WC_SHA3_224_DIGEST_SIZE];
  4039. byte hashcopy[WC_SHA3_224_DIGEST_SIZE];
  4040. testVector a, b, c;
  4041. testVector test_sha[3];
  4042. wc_test_ret_t ret = 0;
  4043. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4044. a.input = "";
  4045. a.output = "\x6b\x4e\x03\x42\x36\x67\xdb\xb7\x3b\x6e\x15\x45\x4f\x0e\xb1"
  4046. "\xab\xd4\x59\x7f\x9a\x1b\x07\x8e\x3f\x5b\x5a\x6b\xc7";
  4047. a.inLen = XSTRLEN(a.input);
  4048. a.outLen = WC_SHA3_224_DIGEST_SIZE;
  4049. b.input = "abc";
  4050. b.output = "\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76"
  4051. "\x6f\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf";
  4052. b.inLen = XSTRLEN(b.input);
  4053. b.outLen = WC_SHA3_224_DIGEST_SIZE;
  4054. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4055. c.output = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55\x74\x49\x44\x79"
  4056. "\xba\x5c\x7e\x7a\xb7\x6e\xf2\x64\xea\xd0\xfc\xce\x33";
  4057. c.inLen = XSTRLEN(c.input);
  4058. c.outLen = WC_SHA3_224_DIGEST_SIZE;
  4059. test_sha[0] = a;
  4060. test_sha[1] = b;
  4061. test_sha[2] = c;
  4062. ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
  4063. if (ret != 0)
  4064. return WC_TEST_RET_ENC_EC(ret);
  4065. for (i = 0; i < times; ++i) {
  4066. ret = wc_Sha3_224_Update(&sha, (byte*)test_sha[i].input,
  4067. (word32)test_sha[i].inLen);
  4068. if (ret != 0)
  4069. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4070. ret = wc_Sha3_224_GetHash(&sha, hashcopy);
  4071. if (ret != 0)
  4072. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4073. ret = wc_Sha3_224_Final(&sha, hash);
  4074. if (ret != 0)
  4075. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4076. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_224_DIGEST_SIZE) != 0)
  4077. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4078. if (XMEMCMP(hash, hashcopy, WC_SHA3_224_DIGEST_SIZE) != 0)
  4079. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4080. }
  4081. #ifndef NO_LARGE_HASH_TEST
  4082. /* BEGIN LARGE HASH TEST */ {
  4083. byte large_input[1024];
  4084. const char* large_digest =
  4085. "\x13\xe5\xd3\x98\x7b\x94\xda\x41\x12\xc7\x1e\x92\x3a\x19"
  4086. "\x21\x20\x86\x6f\x24\xbf\x0a\x31\xbc\xfd\xd6\x70\x36\xf3";
  4087. for (i = 0; i < (int)sizeof(large_input); i++) {
  4088. large_input[i] = (byte)(i & 0xFF);
  4089. }
  4090. times = 100;
  4091. for (i = 0; i < times; ++i) {
  4092. ret = wc_Sha3_224_Update(&sha, (byte*)large_input,
  4093. (word32)sizeof(large_input));
  4094. if (ret != 0)
  4095. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4096. }
  4097. ret = wc_Sha3_224_Final(&sha, hash);
  4098. if (ret != 0)
  4099. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4100. if (XMEMCMP(hash, large_digest, WC_SHA3_224_DIGEST_SIZE) != 0)
  4101. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4102. } /* END LARGE HASH TEST */
  4103. #endif /* NO_LARGE_HASH_TEST */
  4104. exit:
  4105. wc_Sha3_224_Free(&sha);
  4106. return ret;
  4107. }
  4108. #endif /* WOLFSSL_NOSHA3_224 */
  4109. #ifndef WOLFSSL_NOSHA3_256
  4110. static wc_test_ret_t sha3_256_test(void)
  4111. {
  4112. wc_Sha3 sha;
  4113. byte hash[WC_SHA3_256_DIGEST_SIZE];
  4114. byte hashcopy[WC_SHA3_256_DIGEST_SIZE];
  4115. testVector a, b, c;
  4116. testVector test_sha[3];
  4117. wc_test_ret_t ret = 0;
  4118. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4119. byte large_input[1024];
  4120. const char* large_digest =
  4121. "\xdc\x90\xc0\xb1\x25\xdb\x2c\x34\x81\xa3\xff\xbc\x1e\x2e\x87\xeb"
  4122. "\x6d\x70\x85\x61\xe0\xe9\x63\x61\xff\xe5\x84\x4b\x1f\x68\x05\x15";
  4123. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  4124. /* test vector with hash of empty string */
  4125. const char* Keccak256EmptyOut =
  4126. "\xc5\xd2\x46\x01\x86\xf7\x23\x3c\x92\x7e\x7d\xb2\xdc\xc7\x03\xc0"
  4127. "\xe5\x00\xb6\x53\xca\x82\x27\x3b\x7b\xfa\xd8\x04\x5d\x85\xa4\x70";
  4128. #endif
  4129. /*
  4130. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-256_Msg0.pdf
  4131. */
  4132. a.input = "";
  4133. a.output = "\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6"
  4134. "\x62\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8"
  4135. "\x43\x4a";
  4136. a.inLen = XSTRLEN(a.input);
  4137. a.outLen = WC_SHA3_256_DIGEST_SIZE;
  4138. b.input = "abc";
  4139. b.output = "\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90"
  4140. "\xbd\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43"
  4141. "\x15\x32";
  4142. b.inLen = XSTRLEN(b.input);
  4143. b.outLen = WC_SHA3_256_DIGEST_SIZE;
  4144. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4145. c.output = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8\x23\x5e"
  4146. "\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32\xdd\x97\x49\x6d"
  4147. "\x33\x76";
  4148. c.inLen = XSTRLEN(c.input);
  4149. c.outLen = WC_SHA3_256_DIGEST_SIZE;
  4150. test_sha[0] = a;
  4151. test_sha[1] = b;
  4152. test_sha[2] = c;
  4153. ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
  4154. if (ret != 0)
  4155. return WC_TEST_RET_ENC_EC(ret);
  4156. for (i = 0; i < times; ++i) {
  4157. ret = wc_Sha3_256_Update(&sha, (byte*)test_sha[i].input,
  4158. (word32)test_sha[i].inLen);
  4159. if (ret != 0)
  4160. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4161. ret = wc_Sha3_256_GetHash(&sha, hashcopy);
  4162. if (ret != 0)
  4163. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4164. ret = wc_Sha3_256_Final(&sha, hash);
  4165. if (ret != 0)
  4166. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4167. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_256_DIGEST_SIZE) != 0)
  4168. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4169. if (XMEMCMP(hash, hashcopy, WC_SHA3_256_DIGEST_SIZE) != 0)
  4170. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4171. }
  4172. #ifndef NO_LARGE_HASH_TEST
  4173. /* BEGIN LARGE HASH TEST */ {
  4174. for (i = 0; i < (int)sizeof(large_input); i++) {
  4175. large_input[i] = (byte)(i & 0xFF);
  4176. }
  4177. times = 100;
  4178. for (i = 0; i < times; ++i) {
  4179. ret = wc_Sha3_256_Update(&sha, (byte*)large_input,
  4180. (word32)sizeof(large_input));
  4181. if (ret != 0)
  4182. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4183. }
  4184. ret = wc_Sha3_256_Final(&sha, hash);
  4185. if (ret != 0)
  4186. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4187. if (XMEMCMP(hash, large_digest, WC_SHA3_256_DIGEST_SIZE) != 0)
  4188. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4189. } /* END LARGE HASH TEST */
  4190. #endif /* NO_LARGE_HASH_TEST */
  4191. /* this is a software only variant of SHA3 not supported by external hardware devices */
  4192. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  4193. /* Test for Keccak256 */
  4194. ret = wc_Sha3_SetFlags(&sha, WC_HASH_SHA3_KECCAK256);
  4195. if (ret != 0) {
  4196. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4197. }
  4198. ret = wc_Sha3_256_Update(&sha, (byte*)"", 0);
  4199. if (ret != 0) {
  4200. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4201. }
  4202. ret = wc_Sha3_256_Final(&sha, hash);
  4203. if (ret != 0) {
  4204. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4205. }
  4206. if (XMEMCMP(hash, Keccak256EmptyOut, WC_SHA3_256_DIGEST_SIZE) != 0) {
  4207. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4208. }
  4209. #endif /* WOLFSSL_HASH_FLAGS && !WOLFSSL_ASYNC_CRYPT */
  4210. exit:
  4211. wc_Sha3_256_Free(&sha);
  4212. return ret;
  4213. }
  4214. #endif /* WOLFSSL_NOSHA3_256 */
  4215. #ifndef WOLFSSL_NOSHA3_384
  4216. static wc_test_ret_t sha3_384_test(void)
  4217. {
  4218. wc_Sha3 sha;
  4219. byte hash[WC_SHA3_384_DIGEST_SIZE];
  4220. byte buf[64];
  4221. #ifndef NO_INTM_HASH_TEST
  4222. byte hashcopy[WC_SHA3_384_DIGEST_SIZE];
  4223. #endif
  4224. testVector a, b, c;
  4225. testVector test_sha[3];
  4226. wc_test_ret_t ret;
  4227. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4228. /*
  4229. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-384_Msg0.pdf
  4230. */
  4231. a.input = "";
  4232. a.output = "\x0c\x63\xa7\x5b\x84\x5e\x4f\x7d\x01\x10\x7d\x85\x2e\x4c\x24"
  4233. "\x85\xc5\x1a\x50\xaa\xaa\x94\xfc\x61\x99\x5e\x71\xbb\xee\x98"
  4234. "\x3a\x2a\xc3\x71\x38\x31\x26\x4a\xdb\x47\xfb\x6b\xd1\xe0\x58"
  4235. "\xd5\xf0\x04";
  4236. a.inLen = XSTRLEN(a.input);
  4237. a.outLen = WC_SHA3_384_DIGEST_SIZE;
  4238. #if defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  4239. /* NIST test vector with a length that is a multiple of 4 */
  4240. b.input = "\x7d\x80\xb1\x60\xc4\xb5\x36\xa3\xbe\xb7\x99\x80\x59\x93\x44"
  4241. "\x04\x7c\x5f\x82\xa1\xdf\xc3\xee\xd4";
  4242. b.output = "\x04\x1c\xc5\x86\x1b\xa3\x34\x56\x3c\x61\xd4\xef\x97\x10\xd4"
  4243. "\x89\x6c\x31\x1c\x92\xed\xbe\x0d\x7c\xd5\x3e\x80\x3b\xf2\xf4"
  4244. "\xeb\x60\x57\x23\x55\x70\x77\x0c\xe8\x7c\x55\x20\xd7\xec\x14"
  4245. "\x19\x87\x22";
  4246. b.inLen = XSTRLEN(b.input);
  4247. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  4248. #else
  4249. b.input = "abc";
  4250. b.output = "\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad"
  4251. "\x8d\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b"
  4252. "\xe4\xb2\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28"
  4253. "\x37\x6d\x25";
  4254. b.inLen = XSTRLEN(b.input);
  4255. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  4256. #endif
  4257. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4258. c.output = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7\x8a\x49"
  4259. "\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd\xbc\x32\xb9\xd4"
  4260. "\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe\xa1\x9e\xef\x51\xac\xd0"
  4261. "\x65\x7c\x22";
  4262. c.inLen = XSTRLEN(c.input);
  4263. c.outLen = WC_SHA3_384_DIGEST_SIZE;
  4264. #if defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  4265. test_sha[0] = b; /* hardware acc. pre-Versal can not handle "" string */
  4266. #else
  4267. test_sha[0] = a;
  4268. #endif
  4269. test_sha[1] = b;
  4270. test_sha[2] = c;
  4271. ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
  4272. if (ret != 0)
  4273. return WC_TEST_RET_ENC_EC(ret);
  4274. for (i = 0; i < times; ++i) {
  4275. XMEMCPY(buf, test_sha[i].input, test_sha[i].inLen);
  4276. ret = wc_Sha3_384_Update(&sha, buf,
  4277. (word32)test_sha[i].inLen);
  4278. if (ret != 0)
  4279. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4280. #ifndef NO_INTM_HASH_TEST
  4281. ret = wc_Sha3_384_GetHash(&sha, hashcopy);
  4282. if (ret != 0)
  4283. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4284. #endif
  4285. ret = wc_Sha3_384_Final(&sha, hash);
  4286. if (ret != 0)
  4287. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4288. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_384_DIGEST_SIZE) != 0)
  4289. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4290. #ifndef NO_INTM_HASH_TEST
  4291. if (XMEMCMP(hash, hashcopy, WC_SHA3_384_DIGEST_SIZE) != 0)
  4292. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4293. #endif
  4294. }
  4295. #ifndef NO_LARGE_HASH_TEST
  4296. /* BEGIN LARGE HASH TEST */ {
  4297. byte large_input[1024];
  4298. const char* large_digest =
  4299. "\x30\x44\xec\x17\xef\x47\x9f\x55\x36\x11\xd6\x3f\x8a\x31\x5a\x71"
  4300. "\x8a\x71\xa7\x1d\x8e\x84\xe8\x6c\x24\x02\x2f\x7a\x08\x4e\xea\xd7"
  4301. "\x42\x36\x5d\xa8\xc2\xb7\x42\xad\xec\x19\xfb\xca\xc6\x64\xb3\xa4";
  4302. for (i = 0; i < (int)sizeof(large_input); i++) {
  4303. large_input[i] = (byte)(i & 0xFF);
  4304. }
  4305. times = 100;
  4306. for (i = 0; i < times; ++i) {
  4307. ret = wc_Sha3_384_Update(&sha, (byte*)large_input,
  4308. (word32)sizeof(large_input));
  4309. if (ret != 0)
  4310. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4311. }
  4312. ret = wc_Sha3_384_Final(&sha, hash);
  4313. if (ret != 0)
  4314. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4315. if (XMEMCMP(hash, large_digest, WC_SHA3_384_DIGEST_SIZE) != 0)
  4316. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4317. } /* END LARGE HASH TEST */
  4318. #endif /* NO_LARGE_HASH_TEST */
  4319. exit:
  4320. wc_Sha3_384_Free(&sha);
  4321. return ret;
  4322. }
  4323. #endif /* WOLFSSL_NOSHA3_384 */
  4324. #ifndef WOLFSSL_NOSHA3_512
  4325. static wc_test_ret_t sha3_512_test(void)
  4326. {
  4327. wc_Sha3 sha;
  4328. byte hash[WC_SHA3_512_DIGEST_SIZE];
  4329. byte hashcopy[WC_SHA3_512_DIGEST_SIZE];
  4330. testVector a, b, c;
  4331. testVector test_sha[3];
  4332. wc_test_ret_t ret;
  4333. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4334. /*
  4335. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-512_Msg0.pdf
  4336. */
  4337. a.input = "";
  4338. a.output = "\xa6\x9f\x73\xcc\xa2\x3a\x9a\xc5\xc8\xb5\x67\xdc\x18\x5a\x75"
  4339. "\x6e\x97\xc9\x82\x16\x4f\xe2\x58\x59\xe0\xd1\xdc\xc1\x47\x5c"
  4340. "\x80\xa6\x15\xb2\x12\x3a\xf1\xf5\xf9\x4c\x11\xe3\xe9\x40\x2c"
  4341. "\x3a\xc5\x58\xf5\x00\x19\x9d\x95\xb6\xd3\xe3\x01\x75\x85\x86"
  4342. "\x28\x1d\xcd\x26";
  4343. a.inLen = XSTRLEN(a.input);
  4344. a.outLen = WC_SHA3_512_DIGEST_SIZE;
  4345. b.input = "abc";
  4346. b.output = "\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09"
  4347. "\x6e\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2"
  4348. "\x71\x2e\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47"
  4349. "\xe3\x93\x40\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27"
  4350. "\x4e\xec\x53\xf0";
  4351. b.inLen = XSTRLEN(b.input);
  4352. b.outLen = WC_SHA3_512_DIGEST_SIZE;
  4353. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4354. c.output = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10\xfc\xa8"
  4355. "\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7\xec\x2f\x1e\x91"
  4356. "\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53\x02\xba\x1b\x0d\x8d\xc7"
  4357. "\x8c\x08\x63\x46\xb5\x33\xb4\x9c\x03\x0d\x99\xa2\x7d\xaf\x11"
  4358. "\x39\xd6\xe7\x5e";
  4359. c.inLen = XSTRLEN(c.input);
  4360. c.outLen = WC_SHA3_512_DIGEST_SIZE;
  4361. test_sha[0] = a;
  4362. test_sha[1] = b;
  4363. test_sha[2] = c;
  4364. ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
  4365. if (ret != 0)
  4366. return WC_TEST_RET_ENC_EC(ret);
  4367. for (i = 0; i < times; ++i) {
  4368. ret = wc_Sha3_512_Update(&sha, (byte*)test_sha[i].input,
  4369. (word32)test_sha[i].inLen);
  4370. if (ret != 0)
  4371. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4372. ret = wc_Sha3_512_GetHash(&sha, hashcopy);
  4373. if (ret != 0)
  4374. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4375. ret = wc_Sha3_512_Final(&sha, hash);
  4376. if (ret != 0)
  4377. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4378. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_512_DIGEST_SIZE) != 0)
  4379. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4380. if (XMEMCMP(hash, hashcopy, WC_SHA3_512_DIGEST_SIZE) != 0)
  4381. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4382. }
  4383. #ifndef NO_LARGE_HASH_TEST
  4384. /* BEGIN LARGE HASH TEST */ {
  4385. byte large_input[1024];
  4386. const char* large_digest =
  4387. "\x9c\x13\x26\xb6\x26\xb2\x94\x31\xbc\xf4\x34\xe9\x6f\xf2\xd6\x29"
  4388. "\x9a\xd0\x9b\x32\x63\x2f\x18\xa7\x5f\x23\xc9\x60\xc2\x32\x0c\xbc"
  4389. "\x57\x77\x33\xf1\x83\x81\x8a\xd3\x15\x7c\x93\xdc\x80\x9f\xed\x61"
  4390. "\x41\xa7\x5b\xfd\x32\x0e\x38\x15\xb0\x46\x3b\x7a\x4f\xfd\x44\x88";
  4391. for (i = 0; i < (int)sizeof(large_input); i++) {
  4392. large_input[i] = (byte)(i & 0xFF);
  4393. }
  4394. times = 100;
  4395. for (i = 0; i < times; ++i) {
  4396. ret = wc_Sha3_512_Update(&sha, (byte*)large_input,
  4397. (word32)sizeof(large_input));
  4398. if (ret != 0)
  4399. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4400. }
  4401. ret = wc_Sha3_512_Final(&sha, hash);
  4402. if (ret != 0)
  4403. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4404. if (XMEMCMP(hash, large_digest, WC_SHA3_512_DIGEST_SIZE) != 0)
  4405. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4406. } /* END LARGE HASH TEST */
  4407. #endif /* NO_LARGE_HASH_TEST */
  4408. exit:
  4409. wc_Sha3_512_Free(&sha);
  4410. return ret;
  4411. }
  4412. #endif /* WOLFSSL_NOSHA3_512 */
  4413. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha3_test(void)
  4414. {
  4415. wc_test_ret_t ret;
  4416. (void)ret;
  4417. WOLFSSL_ENTER("sha3_test");
  4418. #ifndef WOLFSSL_NOSHA3_224
  4419. if ((ret = sha3_224_test()) != 0)
  4420. return ret;
  4421. #endif
  4422. #ifndef WOLFSSL_NOSHA3_256
  4423. if ((ret = sha3_256_test()) != 0)
  4424. return ret;
  4425. #endif
  4426. #ifndef WOLFSSL_NOSHA3_384
  4427. if ((ret = sha3_384_test()) != 0)
  4428. return ret;
  4429. #endif
  4430. #ifndef WOLFSSL_NOSHA3_512
  4431. if ((ret = sha3_512_test()) != 0)
  4432. return ret;
  4433. #endif
  4434. return 0;
  4435. }
  4436. #endif /* WOLFSSL_SHA3 */
  4437. #ifdef WOLFSSL_SHAKE128
  4438. static wc_test_ret_t shake128_absorb_test(wc_Shake* sha, byte *large_input_buf,
  4439. size_t large_input_buf_size)
  4440. {
  4441. testVector a, b, c, d, e;
  4442. testVector test_sha[5];
  4443. wc_test_ret_t ret = 0;
  4444. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4445. static const char large_digest[] =
  4446. "\x2b\xd1\x69\x9f\xb3\x75\x40\x74\xb8\xb2\xd2\x0b\x92\x47\x9b\xfe"
  4447. "\xc9\x91\x48\xbe\xda\xa4\x09\xd7\x61\x35\x18\x05\x07\x71\xa5\x61"
  4448. "\x4d\xc4\x94\xad\xbe\x04\x7d\xad\x95\x2f\xeb\x2c\xc0\x10\x67\x43"
  4449. "\x40\xf1\x4a\x58\x1c\x54\xfa\x24\x1c\x1a\x4e\x8d\x9b\xbc\xea\xa7"
  4450. "\x32\xf2\x4c\xc7\x86\x05\x36\xdc\xb4\x42\xd8\x35\xd1\xb4\xa2\x79"
  4451. "\xa2\xe6\xee\x67\x4f\xbf\x2a\x93\x41\x88\x25\x56\x29\x90\x1a\x06"
  4452. "\xba\xfe\x9f\xa6\x1a\x74\xe8\x7e\x85\x4a\xc8\x58\x60\xb1\x7b\x18"
  4453. "\xdf\x77\x59\x46\x04\xc1\xff\x4b\x9b\xcb\xad\xfe\x91\x28\xf0\x01"
  4454. "\xc1\x33\xd0\x99\x99\x2e\x0c\x86\x84\x67\x4d\x37\xa4\x42\x45\x10"
  4455. "\xdc\x8f\xdb\x6f\xa6\x9b\xee\x8a\x60\xa5\x1f\x95\x3f\x8f\xf5\x31"
  4456. "\x4b\x1d\x48\x1e\x45\xff\x79\x5c\xbe\x72\xfc\x56\xed\x6d\x1a\x99"
  4457. "\x7f\x23\x7c\xd1\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b"
  4458. "\xa3\x60\x51\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41"
  4459. "\x77\x2c\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59"
  4460. "\x0d\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  4461. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d\xa8"
  4462. "\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb\x67\x86"
  4463. "\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b\x3d\xca\x95"
  4464. "\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47\xcf\x56\xba\x71"
  4465. "\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a\xff\xb4\xbe\x72\x26"
  4466. "\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e\xd9\xe9\xe6\xf9\xff\xa5";
  4467. byte hash[sizeof(large_digest) - 1];
  4468. /*
  4469. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE128_Msg0.pdf
  4470. */
  4471. a.input = "";
  4472. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  4473. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  4474. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  4475. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  4476. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  4477. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  4478. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  4479. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a\x04\x30\x2e\x10\xc8\xbc"
  4480. "\x1c\xbf\x1a\x0b\x3a\x51\x20\xea\x17\xcd\xa7\xcf\xad\x76\x5f"
  4481. "\x56\x23\x47\x4d\x36\x8c\xcc\xa8\xaf\x00\x07\xcd\x9f\x5e\x4c"
  4482. "\x84\x9f\x16\x7a\x58\x0b\x14\xaa\xbd\xef\xae\xe7\xee\xf4\x7c"
  4483. "\xb0\xfc\xa9";
  4484. a.inLen = XSTRLEN(a.input);
  4485. a.outLen = WC_SHA3_128_BLOCK_SIZE;
  4486. b.input = "abc";
  4487. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  4488. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  4489. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  4490. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  4491. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  4492. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  4493. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  4494. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3\x77\xcf\xb9\xcd\xe4\xc6"
  4495. "\x35\x99\xb2\x7f\x34\x62\xbb\xa4\xa0\xed\x29\x6c\x80\x1f\x9f"
  4496. "\xf7\xf5\x73\x02\xbb\x30\x76\xee\x14\x5f\x97\xa3\x2a\xe6\x8e"
  4497. "\x76\xab\x66\xc4\x8d\x51\x67\x5b\xd4\x9a\xcc\x29\x08\x2f\x56"
  4498. "\x47\x58\x4e";
  4499. b.inLen = XSTRLEN(b.input);
  4500. b.outLen = WC_SHA3_128_BLOCK_SIZE;
  4501. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4502. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  4503. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  4504. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  4505. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  4506. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  4507. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  4508. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  4509. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa\x54\x56\x54\xf7\x0a\xe5"
  4510. "\x86\xff\x10\x13\x14\x20\x77\x14\x83\xec\x92\xed\xab\x40\x8c"
  4511. "\x76\x7b\xf4\xc5\xb4\xff\xfa\xa8\x0c\x8c\xa2\x14\xd8\x4c\x4d"
  4512. "\xc7\x00\xd0\xc5\x06\x30\xb2\xff\xc3\x79\x3e\xa4\xd8\x72\x58"
  4513. "\xb4\xc9\x54";
  4514. c.inLen = XSTRLEN(c.input);
  4515. c.outLen = WC_SHA3_128_BLOCK_SIZE;
  4516. /* Taken from NIST CAVP test vectors - full rate output. */
  4517. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  4518. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  4519. "\x85\xe0";
  4520. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  4521. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  4522. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  4523. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  4524. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  4525. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  4526. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  4527. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  4528. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  4529. "\xa1\x41\x9b\xaf\x60\x52\xc0\xc1\xb4\x45\xf8\x35\x17\x57\xb0"
  4530. "\xd0\x22\x87\x21\x89\xe2\xc0\x27\x3f\x82\xd9\x69\x69\x66\x3e"
  4531. "\x55\x4d\x09";
  4532. d.inLen = 32;
  4533. d.outLen = WC_SHA3_128_BLOCK_SIZE;
  4534. /* Taken from NIST CAVP test vectors - more than one output block. */
  4535. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  4536. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  4537. "\x47\xe4";
  4538. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  4539. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  4540. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  4541. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  4542. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  4543. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  4544. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  4545. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  4546. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  4547. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  4548. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  4549. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  4550. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  4551. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  4552. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  4553. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  4554. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d"
  4555. "\xa8\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb"
  4556. "\x67\x86\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b"
  4557. "\x3d\xca\x95\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47"
  4558. "\xcf\x56\xba\x71\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a"
  4559. "\xff\xb4\xbe\x72\x26\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e"
  4560. "\xd9\xe9\xe6\xf9\xff\xa5";
  4561. e.inLen = 32;
  4562. e.outLen = 2 * WC_SHA3_128_BLOCK_SIZE;
  4563. test_sha[0] = a;
  4564. test_sha[1] = b;
  4565. test_sha[2] = c;
  4566. test_sha[3] = d;
  4567. test_sha[4] = e;
  4568. for (i = 0; i < times; ++i) {
  4569. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  4570. if (ret != 0)
  4571. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4572. ret = wc_Shake128_Absorb(sha, (byte*)test_sha[i].input,
  4573. (word32)test_sha[i].inLen);
  4574. if (ret != 0)
  4575. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4576. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  4577. (word32)test_sha[i].outLen / WC_SHA3_128_BLOCK_SIZE);
  4578. if (ret != 0)
  4579. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4580. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  4581. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4582. }
  4583. #ifndef NO_LARGE_HASH_TEST
  4584. /* BEGIN LARGE HASH TEST */ {
  4585. for (i = 0; i < (int)large_input_buf_size; i++) {
  4586. large_input_buf[i] = (byte)(i & 0xFF);
  4587. }
  4588. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  4589. if (ret != 0)
  4590. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4591. /* Absorb is non-incremental. */
  4592. ret = wc_Shake128_Absorb(sha, (byte*)large_input_buf,
  4593. (word32)large_input_buf_size);
  4594. if (ret != 0)
  4595. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4596. /* Able to squeeze out blocks incrementally. */
  4597. ret = wc_Shake128_SqueezeBlocks(sha, hash, 1);
  4598. if (ret != 0)
  4599. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4600. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  4601. ((word32)sizeof(hash) / WC_SHA3_128_BLOCK_SIZE) - 1);
  4602. if (ret != 0)
  4603. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4604. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  4605. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4606. } /* END LARGE HASH TEST */
  4607. #endif /* NO_LARGE_HASH_TEST */
  4608. exit:
  4609. return ret;
  4610. }
  4611. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake128_test(void)
  4612. {
  4613. wc_Shake sha;
  4614. byte hash[250];
  4615. testVector a, b, c, d, e;
  4616. testVector test_sha[5];
  4617. wc_test_ret_t ret = 0;
  4618. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4619. #define SHAKE128_LARGE_INPUT_BUFSIZ 1024
  4620. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4621. byte *large_input = NULL;
  4622. #else
  4623. byte large_input[SHAKE128_LARGE_INPUT_BUFSIZ];
  4624. #endif
  4625. static const char large_digest[] =
  4626. "\x88\xd7\x0e\x86\x46\x72\x6b\x3d\x7d\x22\xe1\xa9\x2d\x02\xdb\x35"
  4627. "\x92\x4f\x1b\x03\x90\xee\xa3\xce\xd1\x3a\x08\x3a\xd7\x4e\x10\xdf"
  4628. "\x09\x67\x33\x35\x4f\xdd\x38\x50\x5b\xcb\x75\xc7\xba\x65\xe5\xe8"
  4629. "\xb8\x76\xde\xc5\xee\xd7\xf1\x65\x93\x4e\x5e\xc4\xb1\xd7\x6b\xee"
  4630. "\x4b\x57\x48\xf5\x38\x49\x9e\x45\xa0\xf7\x32\xe9\x05\x26\x6a\x10"
  4631. "\x70\xd4\x7c\x19\x01\x1f\x6d\x37\xba\x7b\x74\xc2\xbc\xb6\xbc\x74"
  4632. "\xa3\x66\x6c\x9b\x11\x84\x9d\x4a\x36\xbc\x8a\x0d\x4c\xe3\x39\xfa"
  4633. "\xfa\x1b";
  4634. WOLFSSL_ENTER("shake128_test");
  4635. /*
  4636. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE128_Msg0.pdf
  4637. */
  4638. a.input = "";
  4639. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  4640. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  4641. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  4642. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  4643. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  4644. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  4645. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  4646. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a";
  4647. a.inLen = XSTRLEN(a.input);
  4648. a.outLen = 114;
  4649. b.input = "abc";
  4650. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  4651. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  4652. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  4653. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  4654. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  4655. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  4656. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  4657. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3";
  4658. b.inLen = XSTRLEN(b.input);
  4659. b.outLen = 114;
  4660. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4661. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  4662. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  4663. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  4664. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  4665. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  4666. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  4667. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  4668. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa";
  4669. c.inLen = XSTRLEN(c.input);
  4670. c.outLen = 114;
  4671. /* Taken from NIST CAVP test vectors - full rate output. */
  4672. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  4673. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  4674. "\x85\xe0";
  4675. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  4676. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  4677. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  4678. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  4679. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  4680. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  4681. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  4682. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  4683. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  4684. "\xa1";
  4685. d.inLen = 32;
  4686. d.outLen = 136;
  4687. /* Taken from NIST CAVP test vectors - more than one output block. */
  4688. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  4689. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  4690. "\x47\xe4";
  4691. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  4692. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  4693. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  4694. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  4695. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  4696. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  4697. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  4698. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  4699. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  4700. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  4701. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  4702. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  4703. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  4704. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  4705. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  4706. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  4707. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4";
  4708. e.inLen = 32;
  4709. e.outLen = 250;
  4710. test_sha[0] = a;
  4711. test_sha[1] = b;
  4712. test_sha[2] = c;
  4713. test_sha[3] = d;
  4714. test_sha[4] = e;
  4715. ret = wc_InitShake128(&sha, HEAP_HINT, devId);
  4716. if (ret != 0)
  4717. return WC_TEST_RET_ENC_EC(ret);
  4718. for (i = 0; i < times; ++i) {
  4719. ret = wc_Shake128_Update(&sha, (byte*)test_sha[i].input,
  4720. (word32)test_sha[i].inLen);
  4721. if (ret != 0)
  4722. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4723. ret = wc_Shake128_Final(&sha, hash, (word32)test_sha[i].outLen);
  4724. if (ret != 0)
  4725. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4726. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  4727. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4728. }
  4729. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4730. large_input = (byte *)XMALLOC(SHAKE128_LARGE_INPUT_BUFSIZ, NULL,
  4731. DYNAMIC_TYPE_TMP_BUFFER);
  4732. if (large_input == NULL)
  4733. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  4734. #endif
  4735. #ifndef NO_LARGE_HASH_TEST
  4736. /* BEGIN LARGE HASH TEST */ {
  4737. for (i = 0; i < SHAKE128_LARGE_INPUT_BUFSIZ; i++) {
  4738. large_input[i] = (byte)(i & 0xFF);
  4739. }
  4740. times = 100;
  4741. for (i = 0; i < times; ++i) {
  4742. ret = wc_Shake128_Update(&sha, (byte*)large_input,
  4743. SHAKE128_LARGE_INPUT_BUFSIZ);
  4744. if (ret != 0)
  4745. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4746. }
  4747. ret = wc_Shake128_Final(&sha, hash, (word32)sizeof(hash));
  4748. if (ret != 0)
  4749. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4750. if (XMEMCMP(hash, large_digest, sizeof(large_digest) - 1) != 0)
  4751. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4752. } /* END LARGE HASH TEST */
  4753. #endif /* NO_LARGE_HASH_TEST */
  4754. ret = shake128_absorb_test(&sha, large_input, SHAKE128_LARGE_INPUT_BUFSIZ);
  4755. exit:
  4756. wc_Shake128_Free(&sha);
  4757. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4758. if (large_input != NULL)
  4759. XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4760. #endif
  4761. return ret;
  4762. }
  4763. #endif
  4764. #ifdef WOLFSSL_SHAKE256
  4765. static wc_test_ret_t shake256_absorb_test(wc_Shake* sha, byte *large_input_buf,
  4766. size_t large_input_buf_size)
  4767. {
  4768. testVector a, b, c, d, e;
  4769. testVector test_sha[5];
  4770. wc_test_ret_t ret = 0;
  4771. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4772. static const char large_digest[] =
  4773. "\x21\x25\x8e\xae\x6e\x4f\xa7\xe1\xb9\x6d\xa7\xc9\x7d\x46\x03\x69"
  4774. "\x29\x0d\x81\x49\xba\x5d\xaf\x37\xfd\xeb\x25\x52\x1d\xd9\xbd\x65"
  4775. "\xfa\x99\xb9\xd1\x70\x6b\xeb\xd4\xc1\x2c\xea\x24\x20\x27\xa7\xcd"
  4776. "\xfa\xe1\x81\xd9\xd5\xc1\x1c\xc7\xe9\x70\xc3\xc7\x21\x6f\x32\x22"
  4777. "\xe3\x27\xdb\x58\x5e\xea\x18\x2d\x63\x4d\x14\x6c\x94\xcf\x2b\x7e"
  4778. "\x6e\x2a\x74\xf3\xe0\xac\xb3\xb2\xcc\xef\x38\xe9\xe7\x35\xb3\xc5"
  4779. "\x77\x9d\xff\xe3\x08\x8e\xf8\x2c\x89\xbb\x45\x22\x16\x99\x91\xc0"
  4780. "\xe7\x71\x57\x75\xc5\xb1\xc6\xaf\x27\xcb\x64\x8c\xc4\xee\x3d\x5f"
  4781. "\x4c\x35\xfb\x1c\xf3\xf8\x0e\xfd\x5e\xfc\x07\xd8\x4d\x55\x32\x49"
  4782. "\x45\x0d\xab\x4a\x49\xc4\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93"
  4783. "\x7a\xe6\x6b\xb4\x36\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43"
  4784. "\x2f\x3b\xfc\x09\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48"
  4785. "\x3d\x0e\xda\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08"
  4786. "\xd9\xdc\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a"
  4787. "\xb7\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  4788. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4\xe6"
  4789. "\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97\x68\x44";
  4790. byte hash[sizeof(large_digest) - 1];
  4791. /*
  4792. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE256_Msg0.pdf
  4793. */
  4794. a.input = "";
  4795. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  4796. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  4797. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  4798. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  4799. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  4800. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  4801. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  4802. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46\xc1\x85\xc1\x51\x11\xe5"
  4803. "\x95\x52\x2a\x6b\xcd\x16\xcf\x86\xf3\xd1\x22\x10\x9e\x3b\x1f"
  4804. "\xdd";
  4805. a.inLen = XSTRLEN(a.input);
  4806. a.outLen = WC_SHA3_256_BLOCK_SIZE;
  4807. b.input = "abc";
  4808. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  4809. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  4810. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  4811. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  4812. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  4813. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  4814. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  4815. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0\x9a\x3e\x6d\x51\xcb\xfc"
  4816. "\x62\x27\x20\xd7\xa7\x5c\x63\x34\xe8\xa2\xd7\xec\x71\xa7\xcc"
  4817. "\x29";
  4818. b.inLen = XSTRLEN(b.input);
  4819. b.outLen = WC_SHA3_256_BLOCK_SIZE;
  4820. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4821. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  4822. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  4823. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  4824. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  4825. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  4826. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  4827. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  4828. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4\x9d\xd3\xef\x7e\x18\x2b"
  4829. "\x15\x24\xdf\x82\xea\x1c\xef\xe1\xc6\xc3\x96\x61\x75\xf0\x22"
  4830. "\x8d";
  4831. c.inLen = XSTRLEN(c.input);
  4832. c.outLen = WC_SHA3_256_BLOCK_SIZE;
  4833. /* Taken from NIST CAVP test vectors - full rate output. */
  4834. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  4835. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  4836. "\x85\xe0";
  4837. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  4838. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  4839. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  4840. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  4841. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  4842. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  4843. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  4844. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  4845. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  4846. "\xc2";
  4847. d.inLen = 32;
  4848. d.outLen = WC_SHA3_256_BLOCK_SIZE;
  4849. /* Taken from NIST CAVP test vectors - more than one output block. */
  4850. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  4851. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  4852. "\x47\xe4";
  4853. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  4854. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  4855. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  4856. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  4857. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  4858. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  4859. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  4860. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  4861. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  4862. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  4863. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  4864. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  4865. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  4866. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  4867. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  4868. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  4869. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4"
  4870. "\xe6\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97"
  4871. "\x68\x44";
  4872. e.inLen = 32;
  4873. e.outLen = 2 * WC_SHA3_256_BLOCK_SIZE;
  4874. test_sha[0] = a;
  4875. test_sha[1] = b;
  4876. test_sha[2] = c;
  4877. test_sha[3] = d;
  4878. test_sha[4] = e;
  4879. for (i = 0; i < times; ++i) {
  4880. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  4881. if (ret != 0)
  4882. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4883. ret = wc_Shake256_Absorb(sha, (byte*)test_sha[i].input,
  4884. (word32)test_sha[i].inLen);
  4885. if (ret != 0)
  4886. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4887. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  4888. (word32)test_sha[i].outLen / WC_SHA3_256_BLOCK_SIZE);
  4889. if (ret != 0)
  4890. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4891. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  4892. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4893. }
  4894. #ifndef NO_LARGE_HASH_TEST
  4895. /* BEGIN LARGE HASH TEST */ {
  4896. for (i = 0; i < (int)large_input_buf_size; i++) {
  4897. large_input_buf[i] = (byte)(i & 0xFF);
  4898. }
  4899. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  4900. if (ret != 0)
  4901. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4902. /* Absorb is non-incremental. */
  4903. ret = wc_Shake256_Absorb(sha, large_input_buf,
  4904. (word32)large_input_buf_size);
  4905. if (ret != 0)
  4906. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4907. /* Able to squeeze out blocks incrementally. */
  4908. ret = wc_Shake256_SqueezeBlocks(sha, hash, 1);
  4909. if (ret != 0)
  4910. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4911. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  4912. ((word32)sizeof(hash) / WC_SHA3_256_BLOCK_SIZE) - 1);
  4913. if (ret != 0)
  4914. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4915. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  4916. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4917. } /* END LARGE HASH TEST */
  4918. #endif /* NO_LARGE_HASH_TEST */
  4919. exit:
  4920. return ret;
  4921. }
  4922. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake256_test(void)
  4923. {
  4924. wc_Shake sha;
  4925. byte hash[250];
  4926. testVector a, b, c, d, e;
  4927. testVector test_sha[5];
  4928. wc_test_ret_t ret = 0;
  4929. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4930. #define SHAKE256_LARGE_INPUT_BUFSIZ 1024
  4931. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4932. byte *large_input = NULL;
  4933. #else
  4934. byte large_input[SHAKE256_LARGE_INPUT_BUFSIZ];
  4935. #endif
  4936. const char* large_digest =
  4937. "\x90\x32\x4a\xcc\xd1\xdf\xb8\x0b\x79\x1f\xb8\xc8\x5b\x54\xc8\xe7"
  4938. "\x45\xf5\x60\x6b\x38\x26\xb2\x0a\xee\x38\x01\xf3\xd9\xfa\x96\x9f"
  4939. "\x6a\xd7\x15\xdf\xb6\xc2\xf4\x20\x33\x44\x55\xe8\x2a\x09\x2b\x68"
  4940. "\x2e\x18\x65\x5e\x65\x93\x28\xbc\xb1\x9e\xe2\xb1\x92\xea\x98\xac"
  4941. "\x21\xef\x4c\xe1\xb4\xb7\xbe\x81\x5c\x1d\xd3\xb7\x17\xe5\xbb\xc5"
  4942. "\x8c\x68\xb7\xfb\xac\x55\x8a\x9b\x4d\x91\xe4\x9f\x72\xbb\x6e\x38"
  4943. "\xaf\x21\x7d\x21\xaa\x98\x4e\x75\xc4\xb4\x1c\x7c\x50\x45\x54\xf9"
  4944. "\xea\x26";
  4945. WOLFSSL_ENTER("shake256_test");
  4946. /*
  4947. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE256_Msg0.pdf
  4948. */
  4949. a.input = "";
  4950. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  4951. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  4952. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  4953. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  4954. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  4955. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  4956. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  4957. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46";
  4958. a.inLen = XSTRLEN(a.input);
  4959. a.outLen = 114;
  4960. b.input = "abc";
  4961. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  4962. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  4963. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  4964. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  4965. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  4966. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  4967. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  4968. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0";
  4969. b.inLen = XSTRLEN(b.input);
  4970. b.outLen = 114;
  4971. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4972. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  4973. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  4974. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  4975. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  4976. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  4977. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  4978. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  4979. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  4980. c.inLen = XSTRLEN(c.input);
  4981. c.outLen = 114;
  4982. /* Taken from NIST CAVP test vectors - full rate output. */
  4983. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  4984. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  4985. "\x85\xe0";
  4986. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  4987. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  4988. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  4989. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  4990. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  4991. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  4992. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  4993. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  4994. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  4995. "\xc2";
  4996. d.inLen = 32;
  4997. d.outLen = 136;
  4998. /* Taken from NIST CAVP test vectors - more than one output block. */
  4999. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  5000. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  5001. "\x47\xe4";
  5002. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  5003. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  5004. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  5005. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  5006. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  5007. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  5008. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  5009. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  5010. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  5011. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  5012. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  5013. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  5014. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  5015. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  5016. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  5017. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  5018. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c";
  5019. e.inLen = 32;
  5020. e.outLen = 250;
  5021. test_sha[0] = a;
  5022. test_sha[1] = b;
  5023. test_sha[2] = c;
  5024. test_sha[3] = d;
  5025. test_sha[4] = e;
  5026. ret = wc_InitShake256(&sha, HEAP_HINT, devId);
  5027. if (ret != 0)
  5028. return WC_TEST_RET_ENC_EC(ret);
  5029. for (i = 0; i < times; ++i) {
  5030. ret = wc_Shake256_Update(&sha, (byte*)test_sha[i].input,
  5031. (word32)test_sha[i].inLen);
  5032. if (ret != 0)
  5033. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5034. ret = wc_Shake256_Final(&sha, hash, (word32)test_sha[i].outLen);
  5035. if (ret != 0)
  5036. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5037. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  5038. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5039. }
  5040. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  5041. large_input = (byte *)XMALLOC(SHAKE256_LARGE_INPUT_BUFSIZ, NULL,
  5042. DYNAMIC_TYPE_TMP_BUFFER);
  5043. if (large_input == NULL)
  5044. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  5045. #endif
  5046. #ifndef NO_LARGE_HASH_TEST
  5047. /* BEGIN LARGE HASH TEST */ {
  5048. for (i = 0; i < SHAKE256_LARGE_INPUT_BUFSIZ; i++) {
  5049. large_input[i] = (byte)(i & 0xFF);
  5050. }
  5051. times = 100;
  5052. for (i = 0; i < times; ++i) {
  5053. ret = wc_Shake256_Update(&sha, (byte*)large_input,
  5054. SHAKE256_LARGE_INPUT_BUFSIZ);
  5055. if (ret != 0)
  5056. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  5057. }
  5058. ret = wc_Shake256_Final(&sha, hash, (word32)sizeof(hash));
  5059. if (ret != 0)
  5060. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  5061. if (XMEMCMP(hash, large_digest, 114) != 0)
  5062. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  5063. } /* END LARGE HASH TEST */
  5064. #endif /* NO_LARGE_HASH_TEST */
  5065. ret = shake256_absorb_test(&sha, large_input, SHAKE256_LARGE_INPUT_BUFSIZ);
  5066. exit:
  5067. wc_Shake256_Free(&sha);
  5068. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  5069. if (large_input != NULL)
  5070. XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5071. #endif
  5072. return ret;
  5073. }
  5074. #endif
  5075. #ifdef WOLFSSL_SM3
  5076. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm3_test(void)
  5077. {
  5078. wc_Sm3 sm3, sm3Copy;
  5079. byte hash[WC_SM3_DIGEST_SIZE];
  5080. byte hashGet[WC_SM3_DIGEST_SIZE];
  5081. byte hashCopy[WC_SM3_DIGEST_SIZE];
  5082. wc_test_ret_t ret = 0;
  5083. WOLFSSL_ENTER("sm3_test");
  5084. testVector a, b, c;
  5085. testVector test_sm3[3];
  5086. int times = sizeof(test_sm3) / sizeof(struct testVector), i;
  5087. a.input = "";
  5088. a.output = "\x1a\xb2\x1d\x83\x55\xcf\xa1\x7f\x8e\x61\x19\x48\x31\xe8\x1a"
  5089. "\x8f\x22\xbe\xc8\xc7\x28\xfe\xfb\x74\x7e\xd0\x35\xeb\x50\x82"
  5090. "\xaa\x2b";
  5091. a.inLen = XSTRLEN(a.input);
  5092. a.outLen = WC_SM3_DIGEST_SIZE;
  5093. b.input = "abc";
  5094. b.output = "\x66\xc7\xf0\xf4\x62\xee\xed\xd9\xd1\xf2\xd4\x6b\xdc\x10\xe4"
  5095. "\xe2\x41\x67\xc4\x87\x5c\xf2\xf7\xa2\x29\x7d\xa0\x2b\x8f\x4b"
  5096. "\xa8\xe0";
  5097. b.inLen = XSTRLEN(b.input);
  5098. b.outLen = WC_SM3_DIGEST_SIZE;
  5099. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  5100. c.output = "\x63\x9b\x6c\xc5\xe6\x4d\x9e\x37\xa3\x90\xb1\x92\xdf\x4f\xa1"
  5101. "\xea\x07\x20\xab\x74\x7f\xf6\x92\xb9\xf3\x8c\x4e\x66\xad\x7b"
  5102. "\x8c\x05";
  5103. c.inLen = XSTRLEN(c.input);
  5104. c.outLen = WC_SM3_DIGEST_SIZE;
  5105. test_sm3[0] = a;
  5106. test_sm3[1] = b;
  5107. test_sm3[2] = c;
  5108. ret = wc_InitSm3(&sm3, HEAP_HINT, devId);
  5109. if (ret != 0)
  5110. return WC_TEST_RET_ENC_EC(ret);
  5111. ret = wc_InitSm3(&sm3Copy, HEAP_HINT, devId);
  5112. if (ret != 0) {
  5113. wc_Sm3Free(&sm3);
  5114. return WC_TEST_RET_ENC_EC(ret);
  5115. }
  5116. /* Test all the KATs. */
  5117. for (i = 0; i < times; ++i) {
  5118. ret = wc_Sm3Update(&sm3, (byte*)test_sm3[i].input,
  5119. (word32)test_sm3[i].inLen);
  5120. if (ret != 0) {
  5121. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5122. }
  5123. /* Get the final hash but leave ready for more updates. */
  5124. ret = wc_Sm3GetHash(&sm3, hashGet);
  5125. if (ret != 0)
  5126. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5127. /* Make a copy of the hash. */
  5128. ret = wc_Sm3Copy(&sm3, &sm3Copy);
  5129. if (ret != 0)
  5130. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5131. /* Get the final hash with original. */
  5132. ret = wc_Sm3Final(&sm3, hash);
  5133. if (ret != 0)
  5134. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5135. /* Get the final hash with copy. */
  5136. ret = wc_Sm3Final(&sm3Copy, hashCopy);
  5137. if (ret != 0)
  5138. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5139. /* Dispose of copy. */
  5140. wc_Sm3Free(&sm3Copy);
  5141. /* Check hashes match expected. */
  5142. if (XMEMCMP(hash, test_sm3[i].output, WC_SM3_DIGEST_SIZE) != 0)
  5143. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5144. if (XMEMCMP(hash, hashGet, WC_SM3_DIGEST_SIZE) != 0)
  5145. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5146. if (XMEMCMP(hash, hashCopy, WC_SM3_DIGEST_SIZE) != 0)
  5147. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5148. }
  5149. #ifndef NO_LARGE_HASH_TEST
  5150. {
  5151. word32 sz;
  5152. byte large_input[1024];
  5153. #ifdef HASH_SIZE_LIMIT
  5154. const char* large_digest =
  5155. "\x6c\x42\x57\x64\x8e\x45\xf3\xb6\xc0\x83\xd3\x41\x83\x66\x51\xb4"
  5156. "\x50\xfe\x06\xb5\xb7\x1e\xd5\x0d\x41\xfc\x1e\xe5\xc6\x57\x95\x0f";
  5157. times = 20;
  5158. #else
  5159. const char* large_digest =
  5160. "\x34\x51\x3c\xde\x7c\x30\xb7\xc5\xaa\x97\x3b\xed\xb3\x16\xb9\x76"
  5161. "\x35\x46\x14\x80\x2a\x57\xca\xd9\x48\xf9\x93\xcc\x1f\xdd\xab\x79";
  5162. times = 100;
  5163. #endif
  5164. /* Set large input to something. */
  5165. for (i = 0; i < (int)sizeof(large_input); i++) {
  5166. large_input[i] = (byte)(i & 0xFF);
  5167. }
  5168. /* Hash a large number of times. */
  5169. for (i = 0; i < times; ++i) {
  5170. ret = wc_Sm3Update(&sm3, (byte*)large_input,
  5171. (word32)sizeof(large_input));
  5172. if (ret != 0)
  5173. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5174. }
  5175. /* Calculate hash and compare to expected. */
  5176. ret = wc_Sm3Final(&sm3, hash);
  5177. if (ret != 0)
  5178. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  5179. if (XMEMCMP(hash, large_digest, WC_SM3_DIGEST_SIZE) != 0)
  5180. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  5181. /* Check updating with various sizes works. */
  5182. for (sz = 1; sz <= 64; sz++) {
  5183. /* Hash a large number of times. */
  5184. for (i = 0; i < times; ++i) {
  5185. word32 o;
  5186. /* Update sz bytes at a time from large input buffer. */
  5187. for (o = 0; o + sz <= (word32)sizeof(large_input); o += sz) {
  5188. ret = wc_Sm3Update(&sm3, (byte*)(large_input + o), sz);
  5189. if (ret != 0)
  5190. ERROR_OUT(WC_TEST_RET_ENC_I(o), exit);
  5191. }
  5192. /* Check for left-overs. */
  5193. if (o < (word32)sizeof(large_input)) {
  5194. ret = wc_Sm3Update(&sm3, (byte*)(large_input + o),
  5195. (word32)sizeof(large_input) - o);
  5196. if (ret != 0)
  5197. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5198. }
  5199. }
  5200. /* Calculate hash and compare to expected. */
  5201. ret = wc_Sm3Final(&sm3, hash);
  5202. if (ret != 0)
  5203. ERROR_OUT(WC_TEST_RET_ENC_I(sz), exit);
  5204. if (XMEMCMP(hash, large_digest, WC_SM3_DIGEST_SIZE) != 0)
  5205. ERROR_OUT(WC_TEST_RET_ENC_I(sz), exit);
  5206. }
  5207. }
  5208. #endif /* NO_LARGE_HASH_TEST */
  5209. exit:
  5210. wc_Sm3Free(&sm3);
  5211. wc_Sm3Free(&sm3Copy);
  5212. return ret;
  5213. }
  5214. #endif
  5215. #ifndef NO_HASH_WRAPPER
  5216. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hash_test(void)
  5217. {
  5218. wc_HashAlg hash;
  5219. int ret, exp_ret;
  5220. int i, j;
  5221. int digestSz;
  5222. byte data[] = "0123456789abcdef0123456789abcdef0123456";
  5223. byte out[WC_MAX_DIGEST_SIZE];
  5224. byte hashOut[WC_MAX_DIGEST_SIZE];
  5225. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  5226. enum wc_HashType hashType;
  5227. #endif
  5228. enum wc_HashType typesGood[] = { WC_HASH_TYPE_MD5, WC_HASH_TYPE_SHA,
  5229. WC_HASH_TYPE_SHA224, WC_HASH_TYPE_SHA256,
  5230. WC_HASH_TYPE_SHA384, WC_HASH_TYPE_SHA512,
  5231. WC_HASH_TYPE_SHA3_224,
  5232. WC_HASH_TYPE_SHA3_256,
  5233. WC_HASH_TYPE_SHA3_384,
  5234. WC_HASH_TYPE_SHA3_512 };
  5235. enum wc_HashType typesNoImpl[] = {
  5236. #ifdef NO_MD5
  5237. WC_HASH_TYPE_MD5,
  5238. #endif
  5239. #ifdef NO_SHA
  5240. WC_HASH_TYPE_SHA,
  5241. #endif
  5242. #ifndef WOLFSSL_SHA224
  5243. WC_HASH_TYPE_SHA224,
  5244. #endif
  5245. #ifdef NO_SHA256
  5246. WC_HASH_TYPE_SHA256,
  5247. #endif
  5248. #ifndef WOLFSSL_SHA384
  5249. WC_HASH_TYPE_SHA384,
  5250. #endif
  5251. #ifndef WOLFSSL_SHA512
  5252. WC_HASH_TYPE_SHA512,
  5253. #endif
  5254. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_224)
  5255. WC_HASH_TYPE_SHA3_224,
  5256. #endif
  5257. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_256)
  5258. WC_HASH_TYPE_SHA3_256,
  5259. #endif
  5260. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_384)
  5261. WC_HASH_TYPE_SHA3_384,
  5262. #endif
  5263. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_512)
  5264. WC_HASH_TYPE_SHA3_512,
  5265. #endif
  5266. WC_HASH_TYPE_NONE
  5267. };
  5268. enum wc_HashType typesBad[] = { WC_HASH_TYPE_NONE, WC_HASH_TYPE_MD5_SHA,
  5269. WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4 };
  5270. enum wc_HashType typesHashBad[] = { WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4,
  5271. WC_HASH_TYPE_BLAKE2B,
  5272. WC_HASH_TYPE_NONE };
  5273. WOLFSSL_ENTER("hash_test");
  5274. /* Parameter Validation testing. */
  5275. ret = wc_HashInit(NULL, WC_HASH_TYPE_SHA256);
  5276. if (ret != BAD_FUNC_ARG)
  5277. return WC_TEST_RET_ENC_EC(ret);
  5278. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  5279. if (ret != BAD_FUNC_ARG)
  5280. return WC_TEST_RET_ENC_EC(ret);
  5281. ret = wc_HashUpdate(&hash, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  5282. if (ret != BAD_FUNC_ARG)
  5283. return WC_TEST_RET_ENC_EC(ret);
  5284. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, data, sizeof(data));
  5285. if (ret != BAD_FUNC_ARG)
  5286. return WC_TEST_RET_ENC_EC(ret);
  5287. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, NULL);
  5288. if (ret != BAD_FUNC_ARG)
  5289. return WC_TEST_RET_ENC_EC(ret);
  5290. ret = wc_HashFinal(&hash, WC_HASH_TYPE_SHA256, NULL);
  5291. if (ret != BAD_FUNC_ARG)
  5292. return WC_TEST_RET_ENC_EC(ret);
  5293. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, out);
  5294. if (ret != BAD_FUNC_ARG)
  5295. return WC_TEST_RET_ENC_EC(ret);
  5296. /* Try invalid hash algorithms. */
  5297. for (i = 0; i < (int)(sizeof(typesBad)/sizeof(*typesBad)); i++) {
  5298. ret = wc_HashInit(&hash, typesBad[i]);
  5299. if (ret != BAD_FUNC_ARG)
  5300. return WC_TEST_RET_ENC_I(i);
  5301. ret = wc_HashUpdate(&hash, typesBad[i], data, sizeof(data));
  5302. if (ret != BAD_FUNC_ARG)
  5303. return WC_TEST_RET_ENC_I(i);
  5304. ret = wc_HashFinal(&hash, typesBad[i], out);
  5305. if (ret != BAD_FUNC_ARG)
  5306. return WC_TEST_RET_ENC_I(i);
  5307. wc_HashFree(&hash, typesBad[i]);
  5308. }
  5309. /* Try valid hash algorithms. */
  5310. for (i = 0, j = 0; i < (int)(sizeof(typesGood)/sizeof(*typesGood)); i++) {
  5311. exp_ret = 0;
  5312. if (typesGood[i] == typesNoImpl[j]) {
  5313. /* Recognized but no implementation compiled in. */
  5314. exp_ret = HASH_TYPE_E;
  5315. j++;
  5316. }
  5317. ret = wc_HashInit(&hash, typesGood[i]);
  5318. if (ret != exp_ret)
  5319. return WC_TEST_RET_ENC_I(i);
  5320. ret = wc_HashUpdate(&hash, typesGood[i], data, sizeof(data));
  5321. if (ret != exp_ret)
  5322. return WC_TEST_RET_ENC_I(i);
  5323. ret = wc_HashFinal(&hash, typesGood[i], out);
  5324. if (ret != exp_ret)
  5325. return WC_TEST_RET_ENC_I(i);
  5326. wc_HashFree(&hash, typesGood[i]);
  5327. digestSz = wc_HashGetDigestSize(typesGood[i]);
  5328. if (exp_ret < 0 && digestSz != exp_ret)
  5329. return WC_TEST_RET_ENC_I(i);
  5330. if (exp_ret == 0 && digestSz < 0)
  5331. return WC_TEST_RET_ENC_I(i);
  5332. if (exp_ret == 0) {
  5333. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut,
  5334. digestSz - 1);
  5335. if (ret != BUFFER_E)
  5336. return WC_TEST_RET_ENC_I(i);
  5337. }
  5338. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut, (word32)digestSz);
  5339. if (ret != exp_ret)
  5340. return WC_TEST_RET_ENC_I(i);
  5341. if (exp_ret == 0 && XMEMCMP(out, hashOut, digestSz) != 0)
  5342. return WC_TEST_RET_ENC_I(i);
  5343. ret = wc_HashGetBlockSize(typesGood[i]);
  5344. if (exp_ret < 0 && ret != exp_ret)
  5345. return WC_TEST_RET_ENC_I(i);
  5346. if (exp_ret == 0 && ret < 0)
  5347. return WC_TEST_RET_ENC_I(i);
  5348. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  5349. ret = wc_HashGetOID(typesGood[i]);
  5350. if (ret == BAD_FUNC_ARG ||
  5351. (exp_ret == 0 && ret == HASH_TYPE_E) ||
  5352. (exp_ret != 0 && ret != HASH_TYPE_E)) {
  5353. return WC_TEST_RET_ENC_I(i);
  5354. }
  5355. hashType = wc_OidGetHash(ret);
  5356. if (exp_ret == 0 && hashType != typesGood[i])
  5357. return WC_TEST_RET_ENC_I(i);
  5358. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  5359. }
  5360. for (i = 0; i < (int)(sizeof(typesHashBad)/sizeof(*typesHashBad)); i++) {
  5361. ret = wc_Hash(typesHashBad[i], data, sizeof(data), out, sizeof(out));
  5362. if ((ret != BAD_FUNC_ARG) && (ret != BUFFER_E) && (ret != HASH_TYPE_E))
  5363. return WC_TEST_RET_ENC_I(i);
  5364. }
  5365. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  5366. ret = wc_HashGetOID(WC_HASH_TYPE_MD2);
  5367. #ifdef WOLFSSL_MD2
  5368. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5369. return WC_TEST_RET_ENC_EC(ret);
  5370. #else
  5371. if (ret != HASH_TYPE_E)
  5372. return WC_TEST_RET_ENC_EC(ret);
  5373. #endif
  5374. hashType = wc_OidGetHash(646); /* Md2h */
  5375. #ifdef WOLFSSL_MD2
  5376. if (hashType != WC_HASH_TYPE_MD2)
  5377. return WC_TEST_RET_ENC_NC;
  5378. #else
  5379. if (hashType != WC_HASH_TYPE_NONE)
  5380. return WC_TEST_RET_ENC_NC;
  5381. #endif
  5382. ret = wc_HashGetOID(WC_HASH_TYPE_MD5_SHA);
  5383. #ifndef NO_MD5
  5384. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5385. return WC_TEST_RET_ENC_EC(ret);
  5386. #else
  5387. if (ret != HASH_TYPE_E)
  5388. return WC_TEST_RET_ENC_EC(ret);
  5389. #endif
  5390. ret = wc_HashGetOID(WC_HASH_TYPE_MD4);
  5391. if (ret != BAD_FUNC_ARG)
  5392. return WC_TEST_RET_ENC_EC(ret);
  5393. ret = wc_HashGetOID(WC_HASH_TYPE_NONE);
  5394. if (ret != BAD_FUNC_ARG)
  5395. return WC_TEST_RET_ENC_EC(ret);
  5396. hashType = wc_OidGetHash(0);
  5397. if (hashType != WC_HASH_TYPE_NONE)
  5398. return WC_TEST_RET_ENC_NC;
  5399. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  5400. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD2);
  5401. #ifdef WOLFSSL_MD2
  5402. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5403. return WC_TEST_RET_ENC_EC(ret);
  5404. #else
  5405. if (ret != HASH_TYPE_E)
  5406. return WC_TEST_RET_ENC_EC(ret);
  5407. #endif
  5408. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD2);
  5409. #ifdef WOLFSSL_MD2
  5410. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5411. return WC_TEST_RET_ENC_EC(ret);
  5412. #else
  5413. if (ret != HASH_TYPE_E)
  5414. return WC_TEST_RET_ENC_EC(ret);
  5415. #endif
  5416. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD4);
  5417. #ifndef NO_MD4
  5418. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5419. return WC_TEST_RET_ENC_EC(ret);
  5420. #else
  5421. if (ret != HASH_TYPE_E)
  5422. return WC_TEST_RET_ENC_EC(ret);
  5423. #endif
  5424. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD4);
  5425. #ifndef NO_MD4
  5426. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5427. return WC_TEST_RET_ENC_EC(ret);
  5428. #else
  5429. if (ret != HASH_TYPE_E)
  5430. return WC_TEST_RET_ENC_EC(ret);
  5431. #endif
  5432. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD5_SHA);
  5433. #if !defined(NO_MD5) && !defined(NO_SHA)
  5434. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5435. return WC_TEST_RET_ENC_EC(ret);
  5436. #else
  5437. if (ret != HASH_TYPE_E)
  5438. return WC_TEST_RET_ENC_EC(ret);
  5439. #endif
  5440. ret = wc_HashGetBlockSize(WC_HASH_TYPE_BLAKE2B);
  5441. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  5442. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5443. return WC_TEST_RET_ENC_EC(ret);
  5444. #else
  5445. if (ret != HASH_TYPE_E)
  5446. return WC_TEST_RET_ENC_EC(ret);
  5447. #endif
  5448. ret = wc_HashGetDigestSize(WC_HASH_TYPE_BLAKE2B);
  5449. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  5450. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5451. return WC_TEST_RET_ENC_EC(ret);
  5452. #else
  5453. if (ret != HASH_TYPE_E)
  5454. return WC_TEST_RET_ENC_EC(ret);
  5455. #endif
  5456. ret = wc_HashGetBlockSize(WC_HASH_TYPE_NONE);
  5457. if (ret != BAD_FUNC_ARG)
  5458. return WC_TEST_RET_ENC_EC(ret);
  5459. ret = wc_HashGetDigestSize(WC_HASH_TYPE_NONE);
  5460. if (ret != BAD_FUNC_ARG)
  5461. return WC_TEST_RET_ENC_EC(ret);
  5462. #if !defined(NO_CERTS) && !defined(NO_ASN)
  5463. #if defined(WOLFSSL_MD2) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5464. ret = wc_GetCTC_HashOID(MD2);
  5465. if (ret == 0)
  5466. return WC_TEST_RET_ENC_EC(ret);
  5467. #endif
  5468. #ifndef NO_MD5
  5469. ret = wc_GetCTC_HashOID(WC_MD5);
  5470. if (ret == 0)
  5471. return WC_TEST_RET_ENC_EC(ret);
  5472. #endif
  5473. #ifndef NO_SHA
  5474. ret = wc_GetCTC_HashOID(WC_SHA);
  5475. if (ret == 0)
  5476. return WC_TEST_RET_ENC_EC(ret);
  5477. #endif
  5478. #ifdef WOLFSSL_SHA224
  5479. ret = wc_GetCTC_HashOID(WC_SHA224);
  5480. if (ret == 0)
  5481. return WC_TEST_RET_ENC_EC(ret);
  5482. #endif
  5483. #ifndef NO_SHA256
  5484. ret = wc_GetCTC_HashOID(WC_SHA256);
  5485. if (ret == 0)
  5486. return WC_TEST_RET_ENC_EC(ret);
  5487. #endif
  5488. #ifdef WOLFSSL_SHA384
  5489. ret = wc_GetCTC_HashOID(WC_SHA384);
  5490. if (ret == 0)
  5491. return WC_TEST_RET_ENC_EC(ret);
  5492. #endif
  5493. #ifdef WOLFSSL_SHA512
  5494. ret = wc_GetCTC_HashOID(WC_SHA512);
  5495. if (ret == 0)
  5496. return WC_TEST_RET_ENC_EC(ret);
  5497. #endif
  5498. ret = wc_GetCTC_HashOID(-1);
  5499. if (ret != 0)
  5500. return WC_TEST_RET_ENC_EC(ret);
  5501. #endif
  5502. return 0;
  5503. }
  5504. #endif /* !NO_HASH_WRAPPER */
  5505. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS) && \
  5506. defined(HAVE_FIPS_VERSION) && \
  5507. (HAVE_FIPS_VERSION >= 5))
  5508. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_md5_test(void)
  5509. {
  5510. Hmac hmac;
  5511. byte hash[WC_MD5_DIGEST_SIZE];
  5512. const char* keys[]=
  5513. {
  5514. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  5515. "Jefe",
  5516. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA",
  5517. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5518. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5519. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5520. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5521. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5522. };
  5523. testVector a, b, c, d;
  5524. testVector test_hmac[4];
  5525. wc_test_ret_t ret;
  5526. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5527. WOLFSSL_ENTER("hmac_md5_test");
  5528. /* Following test vectors are from RFC 2202 section 2 */
  5529. a.input = "Hi There";
  5530. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  5531. "\x9d";
  5532. a.inLen = XSTRLEN(a.input);
  5533. a.outLen = WC_MD5_DIGEST_SIZE;
  5534. b.input = "what do ya want for nothing?";
  5535. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  5536. "\x38";
  5537. b.inLen = XSTRLEN(b.input);
  5538. b.outLen = WC_MD5_DIGEST_SIZE;
  5539. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5540. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5541. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5542. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5543. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  5544. "\xf6";
  5545. c.inLen = XSTRLEN(c.input);
  5546. c.outLen = WC_MD5_DIGEST_SIZE;
  5547. d.input = "Test Using Larger Than Block-Size Key - Hash Key First";
  5548. d.output = "\x6b\x1a\xb7\xfe\x4b\xd7\xbf\x8f\x0b\x62\xe6\xce\x61\xb9\xd0"
  5549. "\xcd";
  5550. d.inLen = XSTRLEN(d.input);
  5551. d.outLen = WC_MD5_DIGEST_SIZE;
  5552. test_hmac[0] = a;
  5553. test_hmac[1] = b;
  5554. test_hmac[2] = c;
  5555. test_hmac[3] = d;
  5556. for (i = 0; i < times; ++i) {
  5557. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  5558. if (i == 1) {
  5559. continue; /* cavium can't handle short keys, fips not allowed */
  5560. }
  5561. #endif
  5562. ret = wc_HmacInit(&hmac, HEAP_HINT, devId);
  5563. if (ret != 0)
  5564. return WC_TEST_RET_ENC_EC(ret);
  5565. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[i],
  5566. (word32)XSTRLEN(keys[i]));
  5567. if (ret != 0)
  5568. return WC_TEST_RET_ENC_EC(ret);
  5569. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5570. (word32)test_hmac[i].inLen);
  5571. if (ret != 0)
  5572. return WC_TEST_RET_ENC_EC(ret);
  5573. ret = wc_HmacFinal(&hmac, hash);
  5574. if (ret != 0)
  5575. return WC_TEST_RET_ENC_EC(ret);
  5576. if (XMEMCMP(hash, test_hmac[i].output, WC_MD5_DIGEST_SIZE) != 0)
  5577. return WC_TEST_RET_ENC_I(i);
  5578. wc_HmacFree(&hmac);
  5579. }
  5580. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  5581. if ((ret = wc_HmacSizeByType(WC_MD5)) != WC_MD5_DIGEST_SIZE)
  5582. return WC_TEST_RET_ENC_EC(ret);
  5583. #endif
  5584. return 0;
  5585. }
  5586. #endif /* !NO_HMAC && !NO_MD5 && (!HAVE_FIPS || (HAVE_FIPS_VERSION < 5)) */
  5587. #if !defined(NO_HMAC) && !defined(NO_SHA)
  5588. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha_test(void)
  5589. {
  5590. Hmac hmac;
  5591. byte hash[WC_SHA_DIGEST_SIZE];
  5592. const char* keys[]=
  5593. {
  5594. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5595. "\x0b\x0b\x0b",
  5596. "Jefe",
  5597. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5598. "\xAA\xAA\xAA",
  5599. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5600. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5601. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5602. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5603. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5604. };
  5605. testVector a, b, c, d;
  5606. testVector test_hmac[4];
  5607. wc_test_ret_t ret;
  5608. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5609. WOLFSSL_ENTER("hmac_sha_test");
  5610. /* Following test vectors are from RFC 2202 section 3 */
  5611. a.input = "Hi There";
  5612. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  5613. "\x8e\xf1\x46\xbe\x00";
  5614. a.inLen = XSTRLEN(a.input);
  5615. a.outLen = WC_SHA_DIGEST_SIZE;
  5616. b.input = "what do ya want for nothing?";
  5617. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  5618. "\x9c\x25\x9a\x7c\x79";
  5619. b.inLen = XSTRLEN(b.input);
  5620. b.outLen = WC_SHA_DIGEST_SIZE;
  5621. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5622. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5623. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5624. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5625. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  5626. "\x4f\x63\xf1\x75\xd3";
  5627. c.inLen = XSTRLEN(c.input);
  5628. c.outLen = WC_SHA_DIGEST_SIZE;
  5629. d.input = "Test Using Larger Than Block-Size Key - Hash Key First";
  5630. d.output = "\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b"
  5631. "\x55\xed\x40\x21\x12";
  5632. d.inLen = XSTRLEN(d.input);
  5633. d.outLen = WC_SHA_DIGEST_SIZE;
  5634. test_hmac[0] = a;
  5635. test_hmac[1] = b;
  5636. test_hmac[2] = c;
  5637. test_hmac[3] = d;
  5638. #if FIPS_VERSION3_GE(6,0,0)
  5639. int allowShortKeyWithFips = 1;
  5640. #endif
  5641. for (i = 0; i < times; ++i) {
  5642. #if defined(HAVE_CAVIUM) || (defined(HAVE_FIPS) && FIPS_VERSION3_LT(6,0,0))
  5643. if (i == 1)
  5644. continue; /* cavium can't handle short keys, fips not allowed */
  5645. #endif
  5646. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5647. return WC_TEST_RET_ENC_EC(ret);
  5648. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[i],
  5649. (word32)XSTRLEN(keys[i]));
  5650. #if FIPS_VERSION3_GE(6,0,0)
  5651. if (i == 1) {
  5652. if (ret != HMAC_MIN_KEYLEN_E)
  5653. return WC_TEST_RET_ENC_EC(ret);
  5654. /* Now use the ex and allow short keys with FIPS option */
  5655. ret = wc_HmacSetKey_ex(&hmac, WC_SHA, (byte*) keys[i],
  5656. (word32)XSTRLEN(keys[i]), allowShortKeyWithFips);
  5657. }
  5658. #endif
  5659. if (ret != 0)
  5660. return WC_TEST_RET_ENC_EC(ret);
  5661. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5662. (word32)test_hmac[i].inLen);
  5663. if (ret != 0)
  5664. return WC_TEST_RET_ENC_EC(ret);
  5665. ret = wc_HmacFinal(&hmac, hash);
  5666. if (ret != 0)
  5667. return WC_TEST_RET_ENC_EC(ret);
  5668. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA_DIGEST_SIZE) != 0)
  5669. return WC_TEST_RET_ENC_I(i);
  5670. wc_HmacFree(&hmac);
  5671. }
  5672. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  5673. if ((ret = wc_HmacSizeByType(WC_SHA)) != WC_SHA_DIGEST_SIZE)
  5674. return WC_TEST_RET_ENC_EC(ret);
  5675. #endif
  5676. return 0;
  5677. }
  5678. #endif
  5679. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  5680. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha224_test(void)
  5681. {
  5682. Hmac hmac;
  5683. byte hash[WC_SHA224_DIGEST_SIZE];
  5684. const char* keys[]=
  5685. {
  5686. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5687. "\x0b\x0b\x0b",
  5688. "Jefe",
  5689. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5690. "\xAA\xAA\xAA",
  5691. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5692. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5693. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5694. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5695. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5696. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5697. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5698. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5699. "\xAA\xAA\xAA"
  5700. };
  5701. testVector a, b, c, d;
  5702. testVector test_hmac[4];
  5703. wc_test_ret_t ret;
  5704. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5705. WOLFSSL_ENTER("hmac_sha224_test");
  5706. /* Following test vectors are from RFC 4231 section 4 */
  5707. a.input = "Hi There";
  5708. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  5709. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  5710. a.inLen = XSTRLEN(a.input);
  5711. a.outLen = WC_SHA224_DIGEST_SIZE;
  5712. b.input = "what do ya want for nothing?";
  5713. b.output = "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d"
  5714. "\x0f\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44";
  5715. b.inLen = XSTRLEN(b.input);
  5716. b.outLen = WC_SHA224_DIGEST_SIZE;
  5717. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5718. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5719. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5720. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5721. c.output = "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2"
  5722. "\x64\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea";
  5723. c.inLen = XSTRLEN(c.input);
  5724. c.outLen = WC_SHA224_DIGEST_SIZE;
  5725. d.input = "Test Using Larger Than Block-Size Key - Hash Key First";
  5726. d.output = "\x95\xe9\xa0\xdb\x96\x20\x95\xad\xae\xbe\x9b\x2d\x6f\x0d\xbc\xe2\xd4\x99\xf1\x12\xf2\xd2\xb7\x27\x3f\xa6\x87\x0e";
  5727. d.inLen = XSTRLEN(d.input);
  5728. d.outLen = WC_SHA224_DIGEST_SIZE;
  5729. test_hmac[0] = a;
  5730. test_hmac[1] = b;
  5731. test_hmac[2] = c;
  5732. test_hmac[3] = d;
  5733. for (i = 0; i < times; ++i) {
  5734. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  5735. if (i == 1)
  5736. continue; /* cavium can't handle short keys, fips not allowed */
  5737. #endif
  5738. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5739. return WC_TEST_RET_ENC_EC(ret);
  5740. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[i],
  5741. (word32)XSTRLEN(keys[i]));
  5742. if (ret != 0)
  5743. return WC_TEST_RET_ENC_EC(ret);
  5744. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5745. (word32)test_hmac[i].inLen);
  5746. if (ret != 0)
  5747. return WC_TEST_RET_ENC_EC(ret);
  5748. ret = wc_HmacFinal(&hmac, hash);
  5749. if (ret != 0)
  5750. return WC_TEST_RET_ENC_EC(ret);
  5751. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  5752. return WC_TEST_RET_ENC_I(i);
  5753. wc_HmacFree(&hmac);
  5754. }
  5755. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  5756. if ((ret = wc_HmacSizeByType(WC_SHA224)) != WC_SHA224_DIGEST_SIZE)
  5757. return WC_TEST_RET_ENC_EC(ret);
  5758. #endif
  5759. return 0;
  5760. }
  5761. #endif
  5762. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  5763. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha256_test(void)
  5764. {
  5765. Hmac hmac;
  5766. byte hash[WC_SHA256_DIGEST_SIZE];
  5767. const char* keys[]=
  5768. {
  5769. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5770. "\x0b\x0b\x0b",
  5771. "Jefe",
  5772. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5773. "\xAA\xAA\xAA",
  5774. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5775. "\xAA\xAA\xAA",
  5776. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5777. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5778. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5779. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5780. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5781. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5782. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5783. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5784. "\xAA\xAA\xAA"
  5785. };
  5786. testVector a, b, c, d, e;
  5787. testVector test_hmac[5];
  5788. wc_test_ret_t ret;
  5789. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5790. WOLFSSL_ENTER("hmac_sha256_test");
  5791. /* Following test vectors are from RFC 4231 section 4 */
  5792. a.input = "Hi There";
  5793. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  5794. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  5795. "\xcf\xf7";
  5796. a.inLen = XSTRLEN(a.input);
  5797. a.outLen = WC_SHA256_DIGEST_SIZE;
  5798. b.input = "what do ya want for nothing?";
  5799. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  5800. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  5801. "\x38\x43";
  5802. b.inLen = XSTRLEN(b.input);
  5803. b.outLen = WC_SHA256_DIGEST_SIZE;
  5804. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5805. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5806. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5807. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5808. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  5809. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  5810. "\x65\xfe";
  5811. c.inLen = XSTRLEN(c.input);
  5812. c.outLen = WC_SHA256_DIGEST_SIZE;
  5813. d.input = 0;
  5814. d.output = "\x86\xe5\x4f\xd4\x48\x72\x5d\x7e\x5d\xcf\xe2\x23\x53\xc8\x28"
  5815. "\xaf\x48\x78\x1e\xb4\x8c\xae\x81\x06\xa7\xe1\xd4\x98\x94\x9f"
  5816. "\x3e\x46";
  5817. d.inLen = 0;
  5818. d.outLen = WC_SHA256_DIGEST_SIZE;
  5819. e.input = "Test Using Larger Than Block-Size Key - Hash Key First";
  5820. e.output = "\x60\xe4\x31\x59\x1e\xe0\xb6\x7f\x0d\x8a\x26\xaa\xcb\xf5\xb7"
  5821. "\x7f\x8e\x0b\xc6\x21\x37\x28\xc5\x14\x05\x46\x04\x0f\x0e\xe3"
  5822. "\x7f\x54";
  5823. e.inLen = XSTRLEN(e.input);;
  5824. e.outLen = WC_SHA256_DIGEST_SIZE;
  5825. test_hmac[0] = a;
  5826. test_hmac[1] = b;
  5827. test_hmac[2] = c;
  5828. test_hmac[3] = d;
  5829. test_hmac[4] = e;
  5830. for (i = 0; i < times; ++i) {
  5831. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  5832. if (i == 1)
  5833. continue; /* cavium can't handle short keys, fips not allowed */
  5834. #endif
  5835. #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
  5836. if (i == 3)
  5837. continue; /* QuickAssist can't handle empty HMAC */
  5838. #endif
  5839. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  5840. return WC_TEST_RET_ENC_I(i);
  5841. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[i],
  5842. (word32)XSTRLEN(keys[i]));
  5843. if (ret != 0)
  5844. return WC_TEST_RET_ENC_I(i);
  5845. if (test_hmac[i].input != NULL) {
  5846. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5847. (word32)test_hmac[i].inLen);
  5848. if (ret != 0)
  5849. return WC_TEST_RET_ENC_I(i);
  5850. }
  5851. ret = wc_HmacFinal(&hmac, hash);
  5852. if (ret != 0)
  5853. return WC_TEST_RET_ENC_I(i);
  5854. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  5855. return WC_TEST_RET_ENC_I(i);
  5856. wc_HmacFree(&hmac);
  5857. }
  5858. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  5859. if ((ret = wc_HmacSizeByType(WC_SHA256)) != WC_SHA256_DIGEST_SIZE)
  5860. return WC_TEST_RET_ENC_EC(ret);
  5861. #if FIPS_VERSION3_GE(6,0,0)
  5862. if ((ret = wc_HmacSizeByType(21)) != HMAC_KAT_FIPS_E)
  5863. #else
  5864. if ((ret = wc_HmacSizeByType(21)) != BAD_FUNC_ARG)
  5865. #endif
  5866. {
  5867. return WC_TEST_RET_ENC_EC(ret);
  5868. }
  5869. #endif
  5870. if ((ret = wolfSSL_GetHmacMaxSize()) != WC_MAX_DIGEST_SIZE)
  5871. return WC_TEST_RET_ENC_EC(ret);
  5872. return 0;
  5873. }
  5874. #endif
  5875. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  5876. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha384_test(void)
  5877. {
  5878. Hmac hmac;
  5879. byte hash[WC_SHA384_DIGEST_SIZE];
  5880. const char* keys[]=
  5881. {
  5882. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5883. "\x0b\x0b\x0b",
  5884. "Jefe",
  5885. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5886. "\xAA\xAA\xAA",
  5887. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5888. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5889. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5890. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5891. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5892. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5893. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5894. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5895. "\xAA\xAA\xAA"
  5896. };
  5897. testVector a, b, c, d;
  5898. testVector test_hmac[4];
  5899. wc_test_ret_t ret;
  5900. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5901. WOLFSSL_ENTER("hmac_sha384_test");
  5902. /* Following test vectors are from RFC 4231 section 4 */
  5903. a.input = "Hi There";
  5904. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  5905. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  5906. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  5907. "\xfa\x9c\xb6";
  5908. a.inLen = XSTRLEN(a.input);
  5909. a.outLen = WC_SHA384_DIGEST_SIZE;
  5910. b.input = "what do ya want for nothing?";
  5911. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  5912. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  5913. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  5914. "\xb2\x16\x49";
  5915. b.inLen = XSTRLEN(b.input);
  5916. b.outLen = WC_SHA384_DIGEST_SIZE;
  5917. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5918. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5919. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5920. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5921. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  5922. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  5923. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  5924. "\xa3\x4f\x27";
  5925. c.inLen = XSTRLEN(c.input);
  5926. c.outLen = WC_SHA384_DIGEST_SIZE;
  5927. d.input = "Test Using Larger Than Block-Size Key - Hash Key First";
  5928. d.output = "\x4e\xce\x08\x44\x85\x81\x3e\x90\x88\xd2\xc6\x3a\x04\x1b\xc5"
  5929. "\xb4\x4f\x9e\xf1\x01\x2a\x2b\x58\x8f\x3c\xd1\x1f\x05\x03\x3a"
  5930. "\xc4\xc6\x0c\x2e\xf6\xab\x40\x30\xfe\x82\x96\x24\x8d\xf1\x63"
  5931. "\xf4\x49\x52";
  5932. d.inLen = XSTRLEN(d.input);
  5933. d.outLen = WC_SHA384_DIGEST_SIZE;
  5934. test_hmac[0] = a;
  5935. test_hmac[1] = b;
  5936. test_hmac[2] = c;
  5937. test_hmac[3] = d;
  5938. for (i = 0; i < times; ++i) {
  5939. #if defined(HAVE_FIPS)
  5940. if (i == 1)
  5941. continue; /* fips not allowed */
  5942. #endif
  5943. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5944. return WC_TEST_RET_ENC_EC(ret);
  5945. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[i],
  5946. (word32)XSTRLEN(keys[i]));
  5947. if (ret != 0)
  5948. return WC_TEST_RET_ENC_EC(ret);
  5949. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5950. (word32)test_hmac[i].inLen);
  5951. if (ret != 0)
  5952. return WC_TEST_RET_ENC_EC(ret);
  5953. ret = wc_HmacFinal(&hmac, hash);
  5954. if (ret != 0)
  5955. return WC_TEST_RET_ENC_EC(ret);
  5956. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  5957. return WC_TEST_RET_ENC_I(i);
  5958. wc_HmacFree(&hmac);
  5959. }
  5960. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  5961. if ((ret = wc_HmacSizeByType(WC_SHA384)) != WC_SHA384_DIGEST_SIZE)
  5962. return WC_TEST_RET_ENC_EC(ret);
  5963. #endif
  5964. return 0;
  5965. }
  5966. #endif
  5967. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA512)
  5968. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha512_test(void)
  5969. {
  5970. Hmac hmac;
  5971. byte hash[WC_SHA512_DIGEST_SIZE];
  5972. const char* keys[]=
  5973. {
  5974. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5975. "\x0b\x0b\x0b",
  5976. "Jefe",
  5977. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5978. "\xAA\xAA\xAA",
  5979. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5980. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5981. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5982. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5983. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5984. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5985. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5986. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5987. "\xAA\xAA\xAA"
  5988. };
  5989. testVector a, b, c, d;
  5990. testVector test_hmac[4];
  5991. wc_test_ret_t ret;
  5992. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5993. WOLFSSL_ENTER("hmac_sha512_test");
  5994. /* Following test vectors are from RFC 4231 section 4 */
  5995. a.input = "Hi There";
  5996. a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
  5997. "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
  5998. "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
  5999. "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
  6000. "\x3a\x12\x68\x54";
  6001. a.inLen = XSTRLEN(a.input);
  6002. a.outLen = WC_SHA512_DIGEST_SIZE;
  6003. b.input = "what do ya want for nothing?";
  6004. b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
  6005. "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
  6006. "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
  6007. "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
  6008. "\x38\xbc\xe7\x37";
  6009. b.inLen = XSTRLEN(b.input);
  6010. b.outLen = WC_SHA512_DIGEST_SIZE;
  6011. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  6012. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  6013. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  6014. "\xDD\xDD\xDD\xDD\xDD\xDD";
  6015. c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
  6016. "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
  6017. "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
  6018. "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
  6019. "\xe1\x32\x92\xfb";
  6020. c.inLen = XSTRLEN(c.input);
  6021. c.outLen = WC_SHA512_DIGEST_SIZE;
  6022. d.input = "Test Using Larger Than Block-Size Key - Hash Key First";
  6023. d.output = "\x80\xb2\x42\x63\xc7\xc1\xa3\xeb\xb7\x14\x93\xc1\xdd\x7b\xe8"
  6024. "\xb4\x9b\x46\xd1\xf4\x1b\x4a\xee\xc1\x12\x1b\x01\x37\x83\xf8"
  6025. "\xf3\x52\x6b\x56\xd0\x37\xe0\x5f\x25\x98\xbd\x0f\xd2\x21\x5d"
  6026. "\x6a\x1e\x52\x95\xe6\x4f\x73\xf6\x3f\x0a\xec\x8b\x91\x5a\x98"
  6027. "\x5d\x78\x65\x98";
  6028. d.inLen = XSTRLEN(d.input);
  6029. d.outLen = WC_SHA512_DIGEST_SIZE;
  6030. test_hmac[0] = a;
  6031. test_hmac[1] = b;
  6032. test_hmac[2] = c;
  6033. test_hmac[3] = d;
  6034. for (i = 0; i < times; ++i) {
  6035. #if defined(HAVE_FIPS)
  6036. if (i == 1)
  6037. continue; /* fips not allowed */
  6038. #endif
  6039. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  6040. return WC_TEST_RET_ENC_EC(ret);
  6041. ret = wc_HmacSetKey(&hmac, WC_SHA512, (byte*)keys[i],
  6042. (word32)XSTRLEN(keys[i]));
  6043. if (ret != 0)
  6044. return WC_TEST_RET_ENC_EC(ret);
  6045. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  6046. (word32)test_hmac[i].inLen);
  6047. if (ret != 0)
  6048. return WC_TEST_RET_ENC_EC(ret);
  6049. ret = wc_HmacFinal(&hmac, hash);
  6050. if (ret != 0)
  6051. return WC_TEST_RET_ENC_EC(ret);
  6052. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  6053. return WC_TEST_RET_ENC_I(i);
  6054. wc_HmacFree(&hmac);
  6055. }
  6056. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  6057. if ((ret = wc_HmacSizeByType(WC_SHA512)) != WC_SHA512_DIGEST_SIZE)
  6058. return WC_TEST_RET_ENC_EC(ret);
  6059. #endif
  6060. return 0;
  6061. }
  6062. #endif
  6063. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  6064. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  6065. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  6066. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha3_test(void)
  6067. {
  6068. Hmac hmac;
  6069. byte hash[WC_SHA3_512_DIGEST_SIZE];
  6070. const char* key[4] =
  6071. {
  6072. "Jefe",
  6073. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  6074. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  6075. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  6076. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  6077. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6078. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6079. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6080. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6081. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6082. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6083. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6084. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6085. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6086. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6087. };
  6088. const char* input[4] =
  6089. {
  6090. "what do ya want for nothing?",
  6091. "Hi There",
  6092. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  6093. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  6094. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  6095. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  6096. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
  6097. "Big Key Input"
  6098. };
  6099. const int hashType[4] =
  6100. {
  6101. WC_SHA3_224, WC_SHA3_256, WC_SHA3_384, WC_SHA3_512
  6102. };
  6103. const int hashSz[4] =
  6104. {
  6105. WC_SHA3_224_DIGEST_SIZE, WC_SHA3_256_DIGEST_SIZE,
  6106. WC_SHA3_384_DIGEST_SIZE, WC_SHA3_512_DIGEST_SIZE
  6107. };
  6108. const char* output[16] =
  6109. {
  6110. /* key = jefe, input = what do ya want for nothing? */
  6111. /* HMAC-SHA3-224 */
  6112. "\x7f\xdb\x8d\xd8\x8b\xd2\xf6\x0d\x1b\x79\x86\x34\xad\x38\x68\x11"
  6113. "\xc2\xcf\xc8\x5b\xfa\xf5\xd5\x2b\xba\xce\x5e\x66",
  6114. /* HMAC-SHA3-256 */
  6115. "\xc7\xd4\x07\x2e\x78\x88\x77\xae\x35\x96\xbb\xb0\xda\x73\xb8\x87"
  6116. "\xc9\x17\x1f\x93\x09\x5b\x29\x4a\xe8\x57\xfb\xe2\x64\x5e\x1b\xa5",
  6117. /* HMAC-SHA3-384 */
  6118. "\xf1\x10\x1f\x8c\xbf\x97\x66\xfd\x67\x64\xd2\xed\x61\x90\x3f\x21"
  6119. "\xca\x9b\x18\xf5\x7c\xf3\xe1\xa2\x3c\xa1\x35\x08\xa9\x32\x43\xce"
  6120. "\x48\xc0\x45\xdc\x00\x7f\x26\xa2\x1b\x3f\x5e\x0e\x9d\xf4\xc2\x0a",
  6121. /* HMAC-SHA3-512 */
  6122. "\x5a\x4b\xfe\xab\x61\x66\x42\x7c\x7a\x36\x47\xb7\x47\x29\x2b\x83"
  6123. "\x84\x53\x7c\xdb\x89\xaf\xb3\xbf\x56\x65\xe4\xc5\xe7\x09\x35\x0b"
  6124. "\x28\x7b\xae\xc9\x21\xfd\x7c\xa0\xee\x7a\x0c\x31\xd0\x22\xa9\x5e"
  6125. "\x1f\xc9\x2b\xa9\xd7\x7d\xf8\x83\x96\x02\x75\xbe\xb4\xe6\x20\x24",
  6126. /* key = 0b..., input = Hi There */
  6127. /* HMAC-SHA3-224 */
  6128. "\x3b\x16\x54\x6b\xbc\x7b\xe2\x70\x6a\x03\x1d\xca\xfd\x56\x37\x3d"
  6129. "\x98\x84\x36\x76\x41\xd8\xc5\x9a\xf3\xc8\x60\xf7",
  6130. /* HMAC-SHA3-256 */
  6131. "\xba\x85\x19\x23\x10\xdf\xfa\x96\xe2\xa3\xa4\x0e\x69\x77\x43\x51"
  6132. "\x14\x0b\xb7\x18\x5e\x12\x02\xcd\xcc\x91\x75\x89\xf9\x5e\x16\xbb",
  6133. /* HMAC-SHA3-384 */
  6134. "\x68\xd2\xdc\xf7\xfd\x4d\xdd\x0a\x22\x40\xc8\xa4\x37\x30\x5f\x61"
  6135. "\xfb\x73\x34\xcf\xb5\xd0\x22\x6e\x1b\xc2\x7d\xc1\x0a\x2e\x72\x3a"
  6136. "\x20\xd3\x70\xb4\x77\x43\x13\x0e\x26\xac\x7e\x3d\x53\x28\x86\xbd",
  6137. /* HMAC-SHA3-512 */
  6138. "\xeb\x3f\xbd\x4b\x2e\xaa\xb8\xf5\xc5\x04\xbd\x3a\x41\x46\x5a\xac"
  6139. "\xec\x15\x77\x0a\x7c\xab\xac\x53\x1e\x48\x2f\x86\x0b\x5e\xc7\xba"
  6140. "\x47\xcc\xb2\xc6\xf2\xaf\xce\x8f\x88\xd2\x2b\x6d\xc6\x13\x80\xf2"
  6141. "\x3a\x66\x8f\xd3\x88\x8b\xb8\x05\x37\xc0\xa0\xb8\x64\x07\x68\x9e",
  6142. /* key = aa..., output = dd... */
  6143. /* HMAC-SHA3-224 */
  6144. "\x67\x6c\xfc\x7d\x16\x15\x36\x38\x78\x03\x90\x69\x2b\xe1\x42\xd2"
  6145. "\xdf\x7c\xe9\x24\xb9\x09\xc0\xc0\x8d\xbf\xdc\x1a",
  6146. /* HMAC-SHA3-256 */
  6147. "\x84\xec\x79\x12\x4a\x27\x10\x78\x65\xce\xdd\x8b\xd8\x2d\xa9\x96"
  6148. "\x5e\x5e\xd8\xc3\x7b\x0a\xc9\x80\x05\xa7\xf3\x9e\xd5\x8a\x42\x07",
  6149. /* HMAC-SHA3-384 */
  6150. "\x27\x5c\xd0\xe6\x61\xbb\x8b\x15\x1c\x64\xd2\x88\xf1\xf7\x82\xfb"
  6151. "\x91\xa8\xab\xd5\x68\x58\xd7\x2b\xab\xb2\xd4\x76\xf0\x45\x83\x73"
  6152. "\xb4\x1b\x6a\xb5\xbf\x17\x4b\xec\x42\x2e\x53\xfc\x31\x35\xac\x6e",
  6153. /* HMAC-SHA3-512 */
  6154. "\x30\x9e\x99\xf9\xec\x07\x5e\xc6\xc6\xd4\x75\xed\xa1\x18\x06\x87"
  6155. "\xfc\xf1\x53\x11\x95\x80\x2a\x99\xb5\x67\x74\x49\xa8\x62\x51\x82"
  6156. "\x85\x1c\xb3\x32\xaf\xb6\xa8\x9c\x41\x13\x25\xfb\xcb\xcd\x42\xaf"
  6157. "\xcb\x7b\x6e\x5a\xab\x7e\xa4\x2c\x66\x0f\x97\xfd\x85\x84\xbf\x03",
  6158. /* key = big key, input = Big Key Input */
  6159. /* HMAC-SHA3-224 */
  6160. "\x29\xe0\x5e\x46\xc4\xa4\x5e\x46\x74\xbf\xd7\x2d\x1a\xd8\x66\xdb"
  6161. "\x2d\x0d\x10\x4e\x2b\xfa\xad\x53\x7d\x15\x69\x8b",
  6162. /* HMAC-SHA3-256 */
  6163. "\xb5\x5b\x8d\x64\xb6\x9c\x21\xd0\xbf\x20\x5c\xa2\xf7\xb9\xb1\x4e"
  6164. "\x88\x21\x61\x2c\x66\xc3\x91\xae\x6c\x95\x16\x85\x83\xe6\xf4\x9b",
  6165. /* HMAC-SHA3-384 */
  6166. "\xaa\x91\xb3\xa6\x2f\x56\xa1\xbe\x8c\x3e\x74\x38\xdb\x58\xd9\xd3"
  6167. "\x34\xde\xa0\x60\x6d\x8d\x46\xe0\xec\xa9\xf6\x06\x35\x14\xe6\xed"
  6168. "\x83\xe6\x7c\x77\x24\x6c\x11\xb5\x90\x82\xb5\x75\xda\x7b\x83\x2d",
  6169. /* HMAC-SHA3-512 */
  6170. "\x1c\xc3\xa9\x24\x4a\x4a\x3f\xbd\xc7\x20\x00\x16\x9b\x79\x47\x03"
  6171. "\x78\x75\x2c\xb5\xf1\x2e\x62\x7c\xbe\xef\x4e\x8f\x0b\x11\x2b\x32"
  6172. "\xa0\xee\xc9\xd0\x4d\x64\x64\x0b\x37\xf4\xdd\x66\xf7\x8b\xb3\xad"
  6173. "\x52\x52\x6b\x65\x12\xde\x0d\x7c\xc0\x8b\x60\x01\x6c\x37\xd7\xa8"
  6174. };
  6175. int i = 0, iMax = sizeof(input) / sizeof(input[0]),
  6176. j, jMax = sizeof(hashType) / sizeof(hashType[0]);
  6177. int ret;
  6178. WOLFSSL_ENTER("hmac_sha3_test");
  6179. #ifdef HAVE_FIPS
  6180. /* FIPS requires a minimum length for HMAC keys, and "Jefe" is too
  6181. * short. Skip it in FIPS builds. */
  6182. i = 1;
  6183. #endif
  6184. for (; i < iMax; i++) {
  6185. for (j = 0; j < jMax; j++) {
  6186. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  6187. return WC_TEST_RET_ENC_EC(ret);
  6188. ret = wc_HmacSetKey(&hmac, hashType[j], (byte*)key[i],
  6189. (word32)XSTRLEN(key[i]));
  6190. if (ret != 0)
  6191. return WC_TEST_RET_ENC_EC(ret);
  6192. ret = wc_HmacUpdate(&hmac, (byte*)input[i],
  6193. (word32)XSTRLEN(input[i]));
  6194. if (ret != 0)
  6195. return WC_TEST_RET_ENC_EC(ret);
  6196. ret = wc_HmacFinal(&hmac, hash);
  6197. if (ret != 0)
  6198. return WC_TEST_RET_ENC_EC(ret);
  6199. if (XMEMCMP(hash, output[(i*jMax) + j], (size_t)hashSz[j]) != 0)
  6200. return WC_TEST_RET_ENC_NC;
  6201. wc_HmacFree(&hmac);
  6202. if (i > 0)
  6203. continue;
  6204. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  6205. ret = wc_HmacSizeByType(hashType[j]);
  6206. if (ret != hashSz[j])
  6207. return WC_TEST_RET_ENC_EC(ret);
  6208. #endif
  6209. }
  6210. }
  6211. return 0;
  6212. }
  6213. #endif
  6214. #ifdef WC_RC2
  6215. typedef struct rc2TestVector {
  6216. const char* input;
  6217. const char* output;
  6218. const char* key; /* Key, variable up to 128 bytes */
  6219. const char* iv; /* IV, 8-bytes */
  6220. int inLen;
  6221. int outLen;
  6222. int keyLen;
  6223. int effectiveKeyBits; /* Up to 1024 bits supported */
  6224. } rc2TestVector;
  6225. static wc_test_ret_t rc2_ecb_test(void)
  6226. {
  6227. wc_test_ret_t ret = 0;
  6228. byte cipher[RC2_BLOCK_SIZE];
  6229. byte plain[RC2_BLOCK_SIZE];
  6230. rc2TestVector a, b, c, d, e, f, g, h;
  6231. rc2TestVector test_rc2[8];
  6232. int times = sizeof(test_rc2) / sizeof(rc2TestVector), i;
  6233. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6234. a.output = "\xeb\xb7\x73\xf9\x93\x27\x8e\xff";
  6235. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6236. a.inLen = RC2_BLOCK_SIZE;
  6237. a.outLen = RC2_BLOCK_SIZE;
  6238. a.keyLen = 8;
  6239. a.effectiveKeyBits = 63;
  6240. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff";
  6241. b.output = "\x27\x8b\x27\xe4\x2e\x2f\x0d\x49";
  6242. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  6243. b.inLen = RC2_BLOCK_SIZE;
  6244. b.outLen = RC2_BLOCK_SIZE;
  6245. b.keyLen = 8;
  6246. b.effectiveKeyBits = 64;
  6247. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01";
  6248. c.output = "\x30\x64\x9e\xdf\x9b\xe7\xd2\xc2";
  6249. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  6250. c.inLen = RC2_BLOCK_SIZE;
  6251. c.outLen = RC2_BLOCK_SIZE;
  6252. c.keyLen = 8;
  6253. c.effectiveKeyBits = 64;
  6254. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6255. d.output = "\x61\xa8\xa2\x44\xad\xac\xcc\xf0";
  6256. d.key = "\x88";
  6257. d.inLen = RC2_BLOCK_SIZE;
  6258. d.outLen = RC2_BLOCK_SIZE;
  6259. d.keyLen = 1;
  6260. d.effectiveKeyBits = 64;
  6261. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6262. e.output = "\x6c\xcf\x43\x08\x97\x4c\x26\x7f";
  6263. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  6264. e.inLen = RC2_BLOCK_SIZE;
  6265. e.outLen = RC2_BLOCK_SIZE;
  6266. e.keyLen = 7;
  6267. e.effectiveKeyBits = 64;
  6268. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6269. f.output = "\x1a\x80\x7d\x27\x2b\xbe\x5d\xb1";
  6270. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  6271. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  6272. f.inLen = RC2_BLOCK_SIZE;
  6273. f.outLen = RC2_BLOCK_SIZE;
  6274. f.keyLen = 16;
  6275. f.effectiveKeyBits = 64;
  6276. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6277. g.output = "\x22\x69\x55\x2a\xb0\xf8\x5c\xa6";
  6278. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  6279. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  6280. g.inLen = RC2_BLOCK_SIZE;
  6281. g.outLen = RC2_BLOCK_SIZE;
  6282. g.keyLen = 16;
  6283. g.effectiveKeyBits = 128;
  6284. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6285. h.output = "\x5b\x78\xd3\xa4\x3d\xff\xf1\xf1";
  6286. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  6287. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  6288. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  6289. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  6290. "\x1e";
  6291. h.inLen = RC2_BLOCK_SIZE;
  6292. h.outLen = RC2_BLOCK_SIZE;
  6293. h.keyLen = 33;
  6294. h.effectiveKeyBits = 129;
  6295. a.iv = b.iv = c.iv = d.iv = e.iv = f.iv = g.iv = h.iv = NULL;
  6296. test_rc2[0] = a;
  6297. test_rc2[1] = b;
  6298. test_rc2[2] = c;
  6299. test_rc2[3] = d;
  6300. test_rc2[4] = e;
  6301. test_rc2[5] = f;
  6302. test_rc2[6] = g;
  6303. test_rc2[7] = h;
  6304. for (i = 0; i < times; ++i) {
  6305. Rc2 enc;
  6306. XMEMSET(cipher, 0, RC2_BLOCK_SIZE);
  6307. XMEMSET(plain, 0, RC2_BLOCK_SIZE);
  6308. ret = wc_Rc2SetKey(&enc, (byte*)test_rc2[i].key, test_rc2[i].keyLen,
  6309. NULL, test_rc2[i].effectiveKeyBits);
  6310. if (ret != 0) {
  6311. return WC_TEST_RET_ENC_EC(ret);
  6312. }
  6313. /* ECB encrypt */
  6314. ret = wc_Rc2EcbEncrypt(&enc, cipher, (byte*)test_rc2[i].input,
  6315. (word32)test_rc2[i].outLen);
  6316. if (ret != 0) {
  6317. return WC_TEST_RET_ENC_EC(ret);
  6318. }
  6319. if (XMEMCMP(cipher, test_rc2[i].output, test_rc2[i].outLen)) {
  6320. return WC_TEST_RET_ENC_NC;
  6321. }
  6322. /* ECB decrypt */
  6323. ret = wc_Rc2EcbDecrypt(&enc, plain, cipher, RC2_BLOCK_SIZE);
  6324. if (ret != 0) {
  6325. return WC_TEST_RET_ENC_EC(ret);
  6326. }
  6327. if (XMEMCMP(plain, test_rc2[i].input, RC2_BLOCK_SIZE)) {
  6328. return WC_TEST_RET_ENC_NC;
  6329. }
  6330. }
  6331. return 0;
  6332. }
  6333. static wc_test_ret_t rc2_cbc_test(void)
  6334. {
  6335. wc_test_ret_t ret = 0;
  6336. byte cipher[128];
  6337. byte plain[128];
  6338. rc2TestVector a, b, c, d, e, f, g, h, i;
  6339. rc2TestVector test_rc2[9];
  6340. int times = sizeof(test_rc2) / sizeof(rc2TestVector), j;
  6341. /* key length = 7, effective key bits = 63 */
  6342. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  6343. "\x00\x00\x00\x00\x00\x00\x00\x00";
  6344. a.output = "\xEB\xB7\x73\xF9\x93\x27\x8E\xFF"
  6345. "\xF0\x51\x77\x8B\x65\xDB\x13\x57";
  6346. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6347. a.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6348. a.inLen = RC2_BLOCK_SIZE*2;
  6349. a.outLen = RC2_BLOCK_SIZE*2;
  6350. a.keyLen = 8;
  6351. a.effectiveKeyBits = 63;
  6352. /* key length = 8, effective key bits = 64, all 0xFF */
  6353. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff"
  6354. "\xff\xff\xff\xff\xff\xff\xff\xff";
  6355. b.output = "\xA3\xA1\x12\x65\x4F\x81\xC5\xCD"
  6356. "\xB6\x94\x3E\xEA\x3E\x8B\x9D\x1F";
  6357. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  6358. b.iv = "\xff\xff\xff\xff\xff\xff\xff\xff";
  6359. b.inLen = RC2_BLOCK_SIZE*2;
  6360. b.outLen = RC2_BLOCK_SIZE*2;
  6361. b.keyLen = 8;
  6362. b.effectiveKeyBits = 64;
  6363. /* key length = 8, effective key bits = 64 */
  6364. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01"
  6365. "\x10\x00\x00\x00\x00\x00\x00\x01";
  6366. c.output = "\xB5\x70\x14\xA2\x5F\x40\xE3\x6D"
  6367. "\x81\x99\x8D\xE0\xB5\xD5\x3A\x05";
  6368. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  6369. c.iv = "\x30\x00\x00\x00\x00\x00\x00\x00";
  6370. c.inLen = RC2_BLOCK_SIZE*2;
  6371. c.outLen = RC2_BLOCK_SIZE*2;
  6372. c.keyLen = 8;
  6373. c.effectiveKeyBits = 64;
  6374. /* key length = 1, effective key bits = 64 */
  6375. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  6376. "\x00\x00\x00\x00\x00\x00\x00\x00";
  6377. d.output = "\x61\xA8\xA2\x44\xAD\xAC\xCC\xF0"
  6378. "\x6D\x19\xE8\xF1\xFC\xE7\x38\x87";
  6379. d.key = "\x88";
  6380. d.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6381. d.inLen = RC2_BLOCK_SIZE*2;
  6382. d.outLen = RC2_BLOCK_SIZE*2;
  6383. d.keyLen = 1;
  6384. d.effectiveKeyBits = 64;
  6385. /* key length = 7, effective key bits = 64 */
  6386. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  6387. "\x00\x00\x00\x00\x00\x00\x00\x00";
  6388. e.output = "\x6C\xCF\x43\x08\x97\x4C\x26\x7F"
  6389. "\xCC\x3C\x53\x57\x7C\xA1\xA4\x4B";
  6390. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  6391. e.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6392. e.inLen = RC2_BLOCK_SIZE*2;
  6393. e.outLen = RC2_BLOCK_SIZE*2;
  6394. e.keyLen = 7;
  6395. e.effectiveKeyBits = 64;
  6396. /* key length = 16, effective key bits = 64 */
  6397. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  6398. "\x00\x00\x00\x00\x00\x00\x00\x00";
  6399. f.output = "\x1A\x80\x7D\x27\x2B\xBE\x5D\xB1"
  6400. "\x64\xEF\xE1\xC3\xB8\xAD\xFB\xBA";
  6401. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  6402. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  6403. f.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6404. f.inLen = RC2_BLOCK_SIZE*2;
  6405. f.outLen = RC2_BLOCK_SIZE*2;
  6406. f.keyLen = 16;
  6407. f.effectiveKeyBits = 64;
  6408. /* key length = 16, effective bits = 128 */
  6409. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  6410. "\x00\x00\x00\x00\x00\x00\x00\x00";
  6411. g.output = "\x22\x69\x55\x2A\xB0\xF8\x5C\xA6"
  6412. "\x53\x6E\xFD\x2D\x89\xE1\x2A\x73";
  6413. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  6414. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  6415. g.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6416. g.inLen = RC2_BLOCK_SIZE*2;
  6417. g.outLen = RC2_BLOCK_SIZE*2;
  6418. g.keyLen = 16;
  6419. g.effectiveKeyBits = 128;
  6420. /* key length = 33, effective bits = 129 */
  6421. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  6422. "\x00\x00\x00\x00\x00\x00\x00\x00";
  6423. h.output = "\x5B\x78\xD3\xA4\x3D\xFF\xF1\xF1"
  6424. "\x45\x30\xA8\xD5\xC7\x7C\x46\x19";
  6425. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  6426. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  6427. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  6428. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  6429. "\x1e";
  6430. h.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6431. h.inLen = RC2_BLOCK_SIZE*2;
  6432. h.outLen = RC2_BLOCK_SIZE*2;
  6433. h.keyLen = 33;
  6434. h.effectiveKeyBits = 129;
  6435. /* key length = 10, effective bits = 40 */
  6436. i.input = "\x11\x22\x33\x44\x55\x66\x77\x88"
  6437. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00"
  6438. "\x11\x22\x33\x44\x55\x66\x77\x88"
  6439. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00";
  6440. i.output = "\x71\x2D\x11\x99\xC9\xA0\x78\x4F"
  6441. "\xCD\xF1\x1E\x3D\xFD\x21\x7E\xDB"
  6442. "\xB2\x6E\x0D\xA4\x72\xBC\x31\x51"
  6443. "\x48\xEF\x4E\x68\x3B\xDC\xCD\x7D";
  6444. i.key = "\x26\x1E\x57\x8E\xC9\x62\xBF\xB8"
  6445. "\x3E\x96";
  6446. i.iv = "\x01\x02\x03\x04\x05\x06\x07\x08";
  6447. i.inLen = RC2_BLOCK_SIZE*4;
  6448. i.outLen = RC2_BLOCK_SIZE*4;
  6449. i.keyLen = 10;
  6450. i.effectiveKeyBits = 40;
  6451. test_rc2[0] = a;
  6452. test_rc2[1] = b;
  6453. test_rc2[2] = c;
  6454. test_rc2[3] = d;
  6455. test_rc2[4] = e;
  6456. test_rc2[5] = f;
  6457. test_rc2[6] = g;
  6458. test_rc2[7] = h;
  6459. test_rc2[8] = i;
  6460. for (j = 0; j < times; ++j) {
  6461. Rc2 rc2;
  6462. XMEMSET(cipher, 0, sizeof(cipher));
  6463. XMEMSET(plain, 0, sizeof(plain));
  6464. ret = wc_Rc2SetKey(&rc2, (byte*)test_rc2[j].key, test_rc2[j].keyLen,
  6465. (byte*)test_rc2[j].iv, test_rc2[j].effectiveKeyBits);
  6466. if (ret != 0) {
  6467. return WC_TEST_RET_ENC_EC(ret);
  6468. }
  6469. ret = wc_Rc2CbcEncrypt(&rc2, cipher, (byte*)test_rc2[j].input,
  6470. test_rc2[j].inLen);
  6471. if (ret != 0) {
  6472. return WC_TEST_RET_ENC_EC(ret);
  6473. }
  6474. if (XMEMCMP(cipher, (byte*)test_rc2[j].output, test_rc2[j].outLen)) {
  6475. return WC_TEST_RET_ENC_NC;
  6476. }
  6477. /* reset IV for decrypt, since overridden by encrypt operation */
  6478. ret = wc_Rc2SetIV(&rc2, (byte*)test_rc2[j].iv);
  6479. if (ret != 0) {
  6480. return WC_TEST_RET_ENC_EC(ret);
  6481. }
  6482. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, test_rc2[j].outLen);
  6483. if (ret != 0) {
  6484. return WC_TEST_RET_ENC_EC(ret);
  6485. }
  6486. if (XMEMCMP(plain, (byte*)test_rc2[j].input, test_rc2[j].inLen)) {
  6487. return WC_TEST_RET_ENC_NC;
  6488. }
  6489. }
  6490. return 0;
  6491. }
  6492. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rc2_test(void)
  6493. {
  6494. wc_test_ret_t ret = 0;
  6495. WOLFSSL_ENTER("rc2_test");
  6496. ret = rc2_ecb_test();
  6497. if (ret != 0) {
  6498. return ret;
  6499. }
  6500. return rc2_cbc_test();
  6501. }
  6502. #endif
  6503. #ifndef NO_RC4
  6504. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t arc4_test(void)
  6505. {
  6506. byte cipher[16];
  6507. byte plain[16];
  6508. wc_test_ret_t ret;
  6509. const char* keys[] =
  6510. {
  6511. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  6512. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  6513. "\x00\x00\x00\x00\x00\x00\x00\x00",
  6514. "\xef\x01\x23\x45"
  6515. };
  6516. testVector a, b, c, d;
  6517. testVector test_arc4[4];
  6518. int times = sizeof(test_arc4) / sizeof(testVector), i;
  6519. WOLFSSL_ENTER("arc4_test");
  6520. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  6521. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  6522. a.inLen = 8;
  6523. a.outLen = 8;
  6524. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6525. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  6526. b.inLen = 8;
  6527. b.outLen = 8;
  6528. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6529. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  6530. c.inLen = 8;
  6531. c.outLen = 8;
  6532. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  6533. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  6534. d.inLen = 10;
  6535. d.outLen = 10;
  6536. test_arc4[0] = a;
  6537. test_arc4[1] = b;
  6538. test_arc4[2] = c;
  6539. test_arc4[3] = d;
  6540. for (i = 0; i < times; ++i) {
  6541. Arc4 enc;
  6542. Arc4 dec;
  6543. int keylen = 8; /* XSTRLEN with key 0x00 not good */
  6544. if (i == 3)
  6545. keylen = 4;
  6546. ret = wc_Arc4Init(&enc, HEAP_HINT, devId);
  6547. if (ret != 0)
  6548. return WC_TEST_RET_ENC_EC(ret);
  6549. ret = wc_Arc4Init(&dec, HEAP_HINT, devId);
  6550. if (ret != 0)
  6551. return WC_TEST_RET_ENC_EC(ret);
  6552. ret = wc_Arc4SetKey(&enc, (byte*)keys[i], (word32)keylen);
  6553. if (ret != 0)
  6554. return WC_TEST_RET_ENC_EC(ret);
  6555. ret = wc_Arc4SetKey(&dec, (byte*)keys[i], (word32)keylen);
  6556. if (ret != 0)
  6557. return WC_TEST_RET_ENC_EC(ret);
  6558. ret = wc_Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  6559. (word32)test_arc4[i].outLen);
  6560. if (ret != 0)
  6561. return WC_TEST_RET_ENC_EC(ret);
  6562. ret = wc_Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  6563. if (ret != 0)
  6564. return WC_TEST_RET_ENC_EC(ret);
  6565. if (XMEMCMP(plain, test_arc4[i].input, test_arc4[i].outLen))
  6566. return WC_TEST_RET_ENC_I(i);
  6567. if (XMEMCMP(cipher, test_arc4[i].output, test_arc4[i].outLen))
  6568. return WC_TEST_RET_ENC_I(i);
  6569. wc_Arc4Free(&enc);
  6570. wc_Arc4Free(&dec);
  6571. }
  6572. return 0;
  6573. }
  6574. #endif
  6575. #ifdef HAVE_CHACHA
  6576. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha_test(void)
  6577. {
  6578. ChaCha enc;
  6579. ChaCha dec;
  6580. byte cipher[128];
  6581. byte plain[128];
  6582. byte sliver[64];
  6583. byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  6584. word32 keySz = 32;
  6585. wc_test_ret_t ret = 0;
  6586. int i;
  6587. int times = 4;
  6588. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6589. {
  6590. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6591. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6592. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6593. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6594. };
  6595. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6596. {
  6597. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6598. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6599. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6600. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  6601. };
  6602. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6603. {
  6604. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6605. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6606. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6607. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6608. };
  6609. /* 128 bit key */
  6610. WOLFSSL_SMALL_STACK_STATIC const byte key4[] =
  6611. {
  6612. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6613. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6614. };
  6615. const byte* keys[] = {key1, key2, key3, key4};
  6616. WOLFSSL_SMALL_STACK_STATIC const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  6617. WOLFSSL_SMALL_STACK_STATIC const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  6618. WOLFSSL_SMALL_STACK_STATIC const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};
  6619. WOLFSSL_SMALL_STACK_STATIC const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  6620. const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4};
  6621. #ifndef BENCH_EMBEDDED
  6622. WOLFSSL_SMALL_STACK_STATIC const byte cipher_big_result[] = {
  6623. 0x06, 0xa6, 0x5d, 0x31, 0x21, 0x6c, 0xdb, 0x37, 0x48, 0x7c, 0x01, 0x9d,
  6624. 0x72, 0xdf, 0x0a, 0x5b, 0x64, 0x74, 0x20, 0xba, 0x9e, 0xe0, 0x26, 0x7a,
  6625. 0xbf, 0xdf, 0x83, 0x34, 0x3b, 0x4f, 0x94, 0x3f, 0x37, 0x89, 0xaf, 0x00,
  6626. 0xdf, 0x0f, 0x2e, 0x75, 0x16, 0x41, 0xf6, 0x7a, 0x86, 0x94, 0x9d, 0x32,
  6627. 0x56, 0xf0, 0x79, 0x71, 0x68, 0x6f, 0xa6, 0x6b, 0xc6, 0x59, 0x49, 0xf6,
  6628. 0x10, 0x34, 0x03, 0x03, 0x16, 0x53, 0x9a, 0x98, 0x2a, 0x46, 0xde, 0x17,
  6629. 0x06, 0x65, 0x70, 0xca, 0x0a, 0x1f, 0xab, 0x80, 0x26, 0x96, 0x3f, 0x3e,
  6630. 0x7a, 0x3c, 0xa8, 0x87, 0xbb, 0x65, 0xdd, 0x5e, 0x07, 0x7b, 0x34, 0xe0,
  6631. 0x56, 0xda, 0x32, 0x13, 0x30, 0xc9, 0x0c, 0xd7, 0xba, 0xe4, 0x1f, 0xa6,
  6632. 0x91, 0x4f, 0x72, 0x9f, 0xd9, 0x5c, 0x62, 0x7d, 0xa6, 0xc2, 0xbc, 0x87,
  6633. 0xae, 0x64, 0x11, 0x94, 0x3b, 0xbc, 0x6c, 0x23, 0xbd, 0x7d, 0x00, 0xb4,
  6634. 0x99, 0xf2, 0x68, 0xb5, 0x59, 0x70, 0x93, 0xad, 0x69, 0xd0, 0xb1, 0x28,
  6635. 0x70, 0x92, 0xeb, 0xec, 0x39, 0x80, 0x82, 0xde, 0x44, 0xe2, 0x8a, 0x26,
  6636. 0xb3, 0xe9, 0x45, 0xcf, 0x83, 0x76, 0x9f, 0x6a, 0xa0, 0x46, 0x4a, 0x3d,
  6637. 0x26, 0x56, 0xaf, 0x49, 0x41, 0x26, 0x1b, 0x6a, 0x41, 0x37, 0x65, 0x91,
  6638. 0x72, 0xc4, 0xe7, 0x3c, 0x17, 0x31, 0xae, 0x2e, 0x2b, 0x31, 0x45, 0xe4,
  6639. 0x93, 0xd3, 0x10, 0xaa, 0xc5, 0x62, 0xd5, 0x11, 0x4b, 0x57, 0x1d, 0xad,
  6640. 0x48, 0x06, 0xd0, 0x0d, 0x98, 0xa5, 0xc6, 0x5b, 0xd0, 0x9e, 0x22, 0xc0,
  6641. 0x00, 0x32, 0x5a, 0xf5, 0x1c, 0x89, 0x6d, 0x54, 0x97, 0x55, 0x6b, 0x46,
  6642. 0xc5, 0xc7, 0xc4, 0x48, 0x9c, 0xbf, 0x47, 0xdc, 0x03, 0xc4, 0x1b, 0xcb,
  6643. 0x65, 0xa6, 0x91, 0x9d, 0x6d, 0xf1, 0xb0, 0x7a, 0x4d, 0x3b, 0x03, 0x95,
  6644. 0xf4, 0x8b, 0x0b, 0xae, 0x39, 0xff, 0x3f, 0xf6, 0xc0, 0x14, 0x18, 0x8a,
  6645. 0xe5, 0x19, 0xbd, 0xc1, 0xb4, 0x05, 0x4e, 0x29, 0x2f, 0x0b, 0x33, 0x76,
  6646. 0x28, 0x16, 0xa4, 0xa6, 0x93, 0x04, 0xb5, 0x55, 0x6b, 0x89, 0x3d, 0xa5,
  6647. 0x0f, 0xd3, 0xad, 0xfa, 0xd9, 0xfd, 0x05, 0x5d, 0x48, 0x94, 0x25, 0x5a,
  6648. 0x2c, 0x9a, 0x94, 0x80, 0xb0, 0xe7, 0xcb, 0x4d, 0x77, 0xbf, 0xca, 0xd8,
  6649. 0x55, 0x48, 0xbd, 0x66, 0xb1, 0x85, 0x81, 0xb1, 0x37, 0x79, 0xab, 0x52,
  6650. 0x08, 0x14, 0x12, 0xac, 0xcd, 0x45, 0x4d, 0x53, 0x6b, 0xca, 0x96, 0xc7,
  6651. 0x3b, 0x2f, 0x73, 0xb1, 0x5a, 0x23, 0xbd, 0x65, 0xd5, 0xea, 0x17, 0xb3,
  6652. 0xdc, 0xa1, 0x17, 0x1b, 0x2d, 0xb3, 0x9c, 0xd0, 0xdb, 0x41, 0x77, 0xef,
  6653. 0x93, 0x20, 0x52, 0x3e, 0x9d, 0xf5, 0xbf, 0x33, 0xf7, 0x52, 0xc1, 0x90,
  6654. 0xa0, 0x15, 0x17, 0xce, 0xf7, 0xf7, 0xd0, 0x3a, 0x3b, 0xd1, 0x72, 0x56,
  6655. 0x31, 0x81, 0xae, 0x60, 0xab, 0x40, 0xc1, 0xd1, 0x28, 0x77, 0x53, 0xac,
  6656. 0x9f, 0x11, 0x0a, 0x88, 0x36, 0x4b, 0xda, 0x57, 0xa7, 0x28, 0x5c, 0x85,
  6657. 0xd3, 0x85, 0x9b, 0x79, 0xad, 0x05, 0x1c, 0x37, 0x14, 0x5e, 0x0d, 0xd0,
  6658. 0x23, 0x03, 0x42, 0x1d, 0x48, 0x5d, 0xc5, 0x3c, 0x5a, 0x08, 0xa9, 0x0d,
  6659. 0x6e, 0x82, 0x7c, 0x2e, 0x3c, 0x41, 0xcc, 0x96, 0x8e, 0xad, 0xee, 0x2a,
  6660. 0x61, 0x0b, 0x16, 0x0f, 0xa9, 0x24, 0x40, 0x85, 0xbc, 0x9f, 0x28, 0x8d,
  6661. 0xe6, 0x68, 0x4d, 0x8f, 0x30, 0x48, 0xd9, 0x73, 0x73, 0x6c, 0x9a, 0x7f,
  6662. 0x67, 0xf7, 0xde, 0x4c, 0x0a, 0x8b, 0xe4, 0xb3, 0x08, 0x2a, 0x52, 0xda,
  6663. 0x54, 0xee, 0xcd, 0xb5, 0x62, 0x4a, 0x26, 0x20, 0xfb, 0x40, 0xbb, 0x39,
  6664. 0x3a, 0x0f, 0x09, 0xe8, 0x00, 0xd1, 0x24, 0x97, 0x60, 0xe9, 0x83, 0x83,
  6665. 0xfe, 0x9f, 0x9c, 0x15, 0xcf, 0x69, 0x03, 0x9f, 0x03, 0xe1, 0xe8, 0x6e,
  6666. 0xbd, 0x87, 0x58, 0x68, 0xee, 0xec, 0xd8, 0x29, 0x46, 0x23, 0x49, 0x92,
  6667. 0x72, 0x95, 0x5b, 0x49, 0xca, 0xe0, 0x45, 0x59, 0xb2, 0xca, 0xf4, 0xfc,
  6668. 0xb7, 0x59, 0x37, 0x49, 0x28, 0xbc, 0xf3, 0xd7, 0x61, 0xbc, 0x4b, 0xf3,
  6669. 0xa9, 0x4b, 0x2f, 0x05, 0xa8, 0x01, 0xa5, 0xdc, 0x00, 0x6e, 0x01, 0xb6,
  6670. 0x45, 0x3c, 0xd5, 0x49, 0x7d, 0x5c, 0x25, 0xe8, 0x31, 0x87, 0xb2, 0xb9,
  6671. 0xbf, 0xb3, 0x01, 0x62, 0x0c, 0xd0, 0x48, 0x77, 0xa2, 0x34, 0x0f, 0x16,
  6672. 0x22, 0x28, 0xee, 0x54, 0x08, 0x93, 0x3b, 0xe4, 0xde, 0x7e, 0x63, 0xf7,
  6673. 0x97, 0x16, 0x5d, 0x71, 0x58, 0xc2, 0x2e, 0xf2, 0x36, 0xa6, 0x12, 0x65,
  6674. 0x94, 0x17, 0xac, 0x66, 0x23, 0x7e, 0xc6, 0x72, 0x79, 0x24, 0xce, 0x8f,
  6675. 0x55, 0x19, 0x97, 0x44, 0xfc, 0x55, 0xec, 0x85, 0x26, 0x27, 0xdb, 0x38,
  6676. 0xb1, 0x42, 0x0a, 0xdd, 0x05, 0x99, 0x28, 0xeb, 0x03, 0x6c, 0x9a, 0xe9,
  6677. 0x17, 0xf6, 0x2c, 0xb0, 0xfe, 0xe7, 0xa4, 0xa7, 0x31, 0xda, 0x4d, 0xb0,
  6678. 0x29, 0xdb, 0xdd, 0x8d, 0x12, 0x13, 0x9c, 0xb4, 0xcc, 0x83, 0x97, 0xfb,
  6679. 0x1a, 0xdc, 0x08, 0xd6, 0x30, 0x62, 0xe8, 0xeb, 0x8b, 0x61, 0xcb, 0x1d,
  6680. 0x06, 0xe3, 0xa5, 0x4d, 0x35, 0xdb, 0x59, 0xa8, 0x2d, 0x87, 0x27, 0x44,
  6681. 0x6f, 0xc0, 0x38, 0x97, 0xe4, 0x85, 0x00, 0x02, 0x09, 0xf6, 0x69, 0x3a,
  6682. 0xcf, 0x08, 0x1b, 0x21, 0xbb, 0x79, 0xb1, 0xa1, 0x34, 0x09, 0xe0, 0x80,
  6683. 0xca, 0xb0, 0x78, 0x8a, 0x11, 0x97, 0xd4, 0x07, 0xbe, 0x1b, 0x6a, 0x5d,
  6684. 0xdb, 0xd6, 0x1f, 0x76, 0x6b, 0x16, 0xf0, 0x58, 0x84, 0x5f, 0x59, 0xce,
  6685. 0x62, 0x34, 0xc3, 0xdf, 0x94, 0xb8, 0x2f, 0x84, 0x68, 0xf0, 0xb8, 0x51,
  6686. 0xd9, 0x6d, 0x8e, 0x4a, 0x1d, 0xe6, 0x5c, 0xd8, 0x86, 0x25, 0xe3, 0x24,
  6687. 0xfd, 0x21, 0x61, 0x13, 0x48, 0x3e, 0xf6, 0x7d, 0xa6, 0x71, 0x9b, 0xd2,
  6688. 0x6e, 0xe6, 0xd2, 0x08, 0x94, 0x62, 0x6c, 0x98, 0xfe, 0x2f, 0x9c, 0x88,
  6689. 0x7e, 0x78, 0x15, 0x02, 0x00, 0xf0, 0xba, 0x24, 0x91, 0xf2, 0xdc, 0x47,
  6690. 0x51, 0x4d, 0x15, 0x5e, 0x91, 0x5f, 0x57, 0x5b, 0x1d, 0x35, 0x24, 0x45,
  6691. 0x75, 0x9b, 0x88, 0x75, 0xf1, 0x2f, 0x85, 0xe7, 0x89, 0xd1, 0x01, 0xb4,
  6692. 0xc8, 0x18, 0xb7, 0x97, 0xef, 0x4b, 0x90, 0xf4, 0xbf, 0x10, 0x27, 0x3c,
  6693. 0x60, 0xff, 0xc4, 0x94, 0x20, 0x2f, 0x93, 0x4b, 0x4d, 0xe3, 0x80, 0xf7,
  6694. 0x2c, 0x71, 0xd9, 0xe3, 0x68, 0xb4, 0x77, 0x2b, 0xc7, 0x0d, 0x39, 0x92,
  6695. 0xef, 0x91, 0x0d, 0xb2, 0x11, 0x50, 0x0e, 0xe8, 0xad, 0x3b, 0xf6, 0xb5,
  6696. 0xc6, 0x14, 0x4d, 0x33, 0x53, 0xa7, 0x60, 0x15, 0xc7, 0x27, 0x51, 0xdc,
  6697. 0x54, 0x29, 0xa7, 0x0d, 0x6a, 0x7b, 0x72, 0x13, 0xad, 0x7d, 0x41, 0x19,
  6698. 0x4e, 0x42, 0x49, 0xcc, 0x42, 0xe4, 0xbd, 0x99, 0x13, 0xd9, 0x7f, 0xf3,
  6699. 0x38, 0xa4, 0xb6, 0x33, 0xed, 0x07, 0x48, 0x7e, 0x8e, 0x82, 0xfe, 0x3a,
  6700. 0x9d, 0x75, 0x93, 0xba, 0x25, 0x4e, 0x37, 0x3c, 0x0c, 0xd5, 0x69, 0xa9,
  6701. 0x2d, 0x9e, 0xfd, 0xe8, 0xbb, 0xf5, 0x0c, 0xe2, 0x86, 0xb9, 0x5e, 0x6f,
  6702. 0x28, 0xe4, 0x19, 0xb3, 0x0b, 0xa4, 0x86, 0xd7, 0x24, 0xd0, 0xb8, 0x89,
  6703. 0x7b, 0x76, 0xec, 0x05, 0x10, 0x5b, 0x68, 0xe9, 0x58, 0x66, 0xa3, 0xc5,
  6704. 0xb6, 0x63, 0x20, 0x0e, 0x0e, 0xea, 0x3d, 0x61, 0x5e, 0xda, 0x3d, 0x3c,
  6705. 0xf9, 0xfd, 0xed, 0xa9, 0xdb, 0x52, 0x94, 0x8a, 0x00, 0xca, 0x3c, 0x8d,
  6706. 0x66, 0x8f, 0xb0, 0xf0, 0x5a, 0xca, 0x3f, 0x63, 0x71, 0xbf, 0xca, 0x99,
  6707. 0x37, 0x9b, 0x75, 0x97, 0x89, 0x10, 0x6e, 0xcf, 0xf2, 0xf5, 0xe3, 0xd5,
  6708. 0x45, 0x9b, 0xad, 0x10, 0x71, 0x6c, 0x5f, 0x6f, 0x7f, 0x22, 0x77, 0x18,
  6709. 0x2f, 0xf9, 0x99, 0xc5, 0x69, 0x58, 0x03, 0x12, 0x86, 0x82, 0x3e, 0xbf,
  6710. 0xc2, 0x12, 0x35, 0x43, 0xa3, 0xd9, 0x18, 0x4f, 0x41, 0x11, 0x6b, 0xf3,
  6711. 0x67, 0xaf, 0x3d, 0x78, 0xe4, 0x22, 0x2d, 0xb3, 0x48, 0x43, 0x31, 0x1d,
  6712. 0xef, 0xa8, 0xba, 0x49, 0x8e, 0xa9, 0xa7, 0xb6, 0x18, 0x77, 0x84, 0xca,
  6713. 0xbd, 0xa2, 0x02, 0x1b, 0x6a, 0xf8, 0x5f, 0xda, 0xff, 0xcf, 0x01, 0x6a,
  6714. 0x86, 0x69, 0xa9, 0xe9, 0xcb, 0x60, 0x1e, 0x15, 0xdc, 0x8f, 0x5d, 0x39,
  6715. 0xb5, 0xce, 0x55, 0x5f, 0x47, 0x97, 0xb1, 0x19, 0x6e, 0x21, 0xd6, 0x13,
  6716. 0x39, 0xb2, 0x24, 0xe0, 0x62, 0x82, 0x9f, 0xed, 0x12, 0x81, 0xed, 0xee,
  6717. 0xab, 0xd0, 0x2f, 0x19, 0x89, 0x3f, 0x57, 0x2e, 0xc2, 0xe2, 0x67, 0xe8,
  6718. 0xae, 0x03, 0x56, 0xba, 0xd4, 0xd0, 0xa4, 0x89, 0x03, 0x06, 0x5b, 0xcc,
  6719. 0xf2, 0x22, 0xb8, 0x0e, 0x76, 0x79, 0x4a, 0x42, 0x1d, 0x37, 0x51, 0x5a,
  6720. 0xaa, 0x46, 0x6c, 0x2a, 0xdd, 0x66, 0xfe, 0xc6, 0x68, 0xc3, 0x38, 0xa2,
  6721. 0xae, 0x5b, 0x98, 0x24, 0x5d, 0x43, 0x05, 0x82, 0x38, 0x12, 0xd3, 0xd1,
  6722. 0x75, 0x2d, 0x4f, 0x61, 0xbd, 0xb9, 0x10, 0x87, 0x44, 0x2a, 0x78, 0x07,
  6723. 0xff, 0xf4, 0x0f, 0xa1, 0xf3, 0x68, 0x9f, 0xbe, 0xae, 0xa2, 0x91, 0xf0,
  6724. 0xc7, 0x55, 0x7a, 0x52, 0xd5, 0xa3, 0x8d, 0x6f, 0xe4, 0x90, 0x5c, 0xf3,
  6725. 0x5f, 0xce, 0x3d, 0x23, 0xf9, 0x8e, 0xae, 0x14, 0xfb, 0x82, 0x9a, 0xa3,
  6726. 0x04, 0x5f, 0xbf, 0xad, 0x3e, 0xf2, 0x97, 0x0a, 0x60, 0x40, 0x70, 0x19,
  6727. 0x72, 0xad, 0x66, 0xfb, 0x78, 0x1b, 0x84, 0x6c, 0x98, 0xbc, 0x8c, 0xf8,
  6728. 0x4f, 0xcb, 0xb5, 0xf6, 0xaf, 0x7a, 0xb7, 0x93, 0xef, 0x67, 0x48, 0x02,
  6729. 0x2c, 0xcb, 0xe6, 0x77, 0x0f, 0x7b, 0xc1, 0xee, 0xc5, 0xb6, 0x2d, 0x7e,
  6730. 0x62, 0xa0, 0xc0, 0xa7, 0xa5, 0x80, 0x31, 0x92, 0x50, 0xa1, 0x28, 0x22,
  6731. 0x95, 0x03, 0x17, 0xd1, 0x0f, 0xf6, 0x08, 0xe5, 0xec
  6732. };
  6733. #define CHACHA_BIG_TEST_SIZE 1305
  6734. #if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC)
  6735. byte cipher_big[CHACHA_BIG_TEST_SIZE] = {0};
  6736. byte plain_big[CHACHA_BIG_TEST_SIZE] = {0};
  6737. byte input_big[CHACHA_BIG_TEST_SIZE] = {0};
  6738. #else
  6739. byte* cipher_big;
  6740. byte* plain_big;
  6741. byte* input_big;
  6742. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  6743. int block_size;
  6744. #endif /* BENCH_EMBEDDED */
  6745. byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90};
  6746. byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96};
  6747. byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5};
  6748. byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd};
  6749. byte* test_chacha[4];
  6750. test_chacha[0] = a;
  6751. test_chacha[1] = b;
  6752. test_chacha[2] = c;
  6753. test_chacha[3] = d;
  6754. WOLFSSL_ENTER("chacha_test");
  6755. #ifndef BENCH_EMBEDDED
  6756. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6757. cipher_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  6758. DYNAMIC_TYPE_TMP_BUFFER);
  6759. if (cipher_big == NULL) {
  6760. return MEMORY_E;
  6761. }
  6762. plain_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  6763. DYNAMIC_TYPE_TMP_BUFFER);
  6764. if (plain_big == NULL) {
  6765. return MEMORY_E;
  6766. }
  6767. input_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  6768. DYNAMIC_TYPE_TMP_BUFFER);
  6769. if (input_big == NULL) {
  6770. return MEMORY_E;
  6771. }
  6772. XMEMSET(cipher_big, 0, CHACHA_BIG_TEST_SIZE);
  6773. XMEMSET(plain_big, 0, CHACHA_BIG_TEST_SIZE);
  6774. XMEMSET(input_big, 0, CHACHA_BIG_TEST_SIZE);
  6775. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  6776. #endif /* BENCH_EMBEDDED */
  6777. for (i = 0; i < times; ++i) {
  6778. if (i < 3) {
  6779. keySz = 32;
  6780. }
  6781. else {
  6782. keySz = 16;
  6783. }
  6784. XMEMCPY(plain, keys[i], keySz);
  6785. XMEMSET(cipher, 0, 32);
  6786. XMEMCPY(cipher + 4, ivs[i], 8);
  6787. ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
  6788. ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
  6789. if (ret != 0)
  6790. return ret;
  6791. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  6792. ret |= wc_Chacha_SetIV(&dec, cipher, 0);
  6793. if (ret != 0)
  6794. return ret;
  6795. XMEMCPY(plain, input, 8);
  6796. ret |= wc_Chacha_Process(&enc, cipher, plain, (word32)8);
  6797. ret |= wc_Chacha_Process(&dec, plain, cipher, (word32)8);
  6798. if (ret != 0)
  6799. return ret;
  6800. if (XMEMCMP(test_chacha[i], cipher, 8))
  6801. return WC_TEST_RET_ENC_I(i);
  6802. if (XMEMCMP(plain, input, 8))
  6803. return WC_TEST_RET_ENC_I(i);
  6804. }
  6805. /* test of starting at a different counter
  6806. encrypts all of the information and decrypts starting at 2nd chunk */
  6807. XMEMSET(plain, 0, sizeof(plain));
  6808. XMEMSET(sliver, 1, sizeof(sliver)); /* set as 1's to not match plain */
  6809. XMEMSET(cipher, 0, sizeof(cipher));
  6810. XMEMCPY(cipher + 4, ivs[0], 8);
  6811. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  6812. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  6813. if (ret != 0)
  6814. return ret;
  6815. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  6816. ret |= wc_Chacha_SetIV(&dec, cipher, 1);
  6817. if (ret != 0)
  6818. return ret;
  6819. ret |= wc_Chacha_Process(&enc, cipher, plain, sizeof(plain));
  6820. ret |= wc_Chacha_Process(&dec, sliver, cipher + 64, sizeof(sliver));
  6821. if (ret != 0)
  6822. return ret;
  6823. if (XMEMCMP(plain + 64, sliver, 64))
  6824. return WC_TEST_RET_ENC_NC;
  6825. #ifndef BENCH_EMBEDDED
  6826. /* test of encrypting more data */
  6827. keySz = 32;
  6828. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  6829. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  6830. if (ret != 0)
  6831. return ret;
  6832. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  6833. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  6834. if (ret != 0)
  6835. return ret;
  6836. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, CHACHA_BIG_TEST_SIZE);
  6837. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big,
  6838. CHACHA_BIG_TEST_SIZE);
  6839. if (ret != 0)
  6840. return ret;
  6841. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  6842. return WC_TEST_RET_ENC_NC;
  6843. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  6844. return WC_TEST_RET_ENC_NC;
  6845. for (i = 0; i < 18; ++i) {
  6846. /* this will test all paths
  6847. * block sizes: 1 3 7 15 31 63 127 255 511 (i = 0- 8)
  6848. * 2 4 8 16 32 64 128 256 512 (i = 9-17)
  6849. */
  6850. block_size = (2 << (i%9)) - (i<9?1:0);
  6851. keySz = 32;
  6852. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  6853. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  6854. if (ret != 0)
  6855. return ret;
  6856. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  6857. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  6858. if (ret != 0)
  6859. return ret;
  6860. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big , (word32)block_size);
  6861. ret |= wc_Chacha_Process(&dec, plain_big , cipher_big, (word32)block_size);
  6862. if (ret != 0)
  6863. return ret;
  6864. if (XMEMCMP(plain_big, input_big, block_size))
  6865. return WC_TEST_RET_ENC_I(i);
  6866. if (XMEMCMP(cipher_big, cipher_big_result, block_size))
  6867. return WC_TEST_RET_ENC_I(i);
  6868. }
  6869. /* Streaming test */
  6870. for (i = 1; i <= (int)CHACHA_CHUNK_BYTES + 1; i++) {
  6871. int j, rem;
  6872. ret = wc_Chacha_SetKey(&enc, keys[0], keySz);
  6873. if (ret != 0)
  6874. return WC_TEST_RET_ENC_EC(ret);
  6875. ret = wc_Chacha_SetKey(&dec, keys[0], keySz);
  6876. if (ret != 0)
  6877. return WC_TEST_RET_ENC_EC(ret);
  6878. ret = wc_Chacha_SetIV(&enc, ivs[2], 0);
  6879. if (ret != 0)
  6880. return WC_TEST_RET_ENC_EC(ret);
  6881. ret = wc_Chacha_SetIV(&dec, ivs[2], 0);
  6882. if (ret != 0)
  6883. return WC_TEST_RET_ENC_EC(ret);
  6884. for (j = 0; j < CHACHA_BIG_TEST_SIZE - i; j+= i) {
  6885. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, (word32)i);
  6886. if (ret != 0)
  6887. return WC_TEST_RET_ENC_EC(ret);
  6888. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, (word32)i);
  6889. if (ret != 0)
  6890. return WC_TEST_RET_ENC_EC(ret);
  6891. }
  6892. rem = CHACHA_BIG_TEST_SIZE - j;
  6893. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, (word32)rem);
  6894. if (ret != 0)
  6895. return WC_TEST_RET_ENC_EC(ret);
  6896. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, (word32)rem);
  6897. if (ret != 0)
  6898. return WC_TEST_RET_ENC_EC(ret);
  6899. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  6900. return WC_TEST_RET_ENC_NC;
  6901. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  6902. return WC_TEST_RET_ENC_NC;
  6903. }
  6904. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6905. XFREE(cipher_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6906. XFREE(plain_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6907. XFREE(input_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6908. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  6909. #endif /* BENCH_EMBEDDED */
  6910. return 0;
  6911. }
  6912. #endif /* HAVE_CHACHA */
  6913. #ifdef HAVE_POLY1305
  6914. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t poly1305_test(void)
  6915. {
  6916. byte tag[16];
  6917. Poly1305 enc;
  6918. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6919. {
  6920. 0x43,0x72,0x79,0x70,0x74,0x6f,0x67,0x72,
  6921. 0x61,0x70,0x68,0x69,0x63,0x20,0x46,0x6f,
  6922. 0x72,0x75,0x6d,0x20,0x52,0x65,0x73,0x65,
  6923. 0x61,0x72,0x63,0x68,0x20,0x47,0x72,0x6f,
  6924. 0x75,0x70
  6925. };
  6926. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6927. {
  6928. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,
  6929. 0x6c,0x64,0x21
  6930. };
  6931. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6932. {
  6933. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6934. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6935. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6936. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6937. };
  6938. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] =
  6939. {
  6940. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  6941. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  6942. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  6943. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  6944. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  6945. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  6946. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  6947. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  6948. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  6949. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  6950. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6951. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  6952. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  6953. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  6954. 0x61,0x16
  6955. };
  6956. WOLFSSL_SMALL_STACK_STATIC const byte msg5[] =
  6957. {
  6958. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  6959. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  6960. };
  6961. WOLFSSL_SMALL_STACK_STATIC const byte msg6[] =
  6962. {
  6963. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  6964. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  6965. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  6966. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  6967. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  6968. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  6969. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6970. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  6971. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  6972. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  6973. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6974. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  6975. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  6976. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  6977. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  6978. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6979. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  6980. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  6981. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  6982. 0x61,0x16
  6983. };
  6984. byte additional[] =
  6985. {
  6986. 0x50,0x51,0x52,0x53,0xc0,0xc1,0xc2,0xc3,
  6987. 0xc4,0xc5,0xc6,0xc7
  6988. };
  6989. WOLFSSL_SMALL_STACK_STATIC const byte correct0[] =
  6990. {
  6991. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  6992. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  6993. };
  6994. WOLFSSL_SMALL_STACK_STATIC const byte correct1[] =
  6995. {
  6996. 0xa8,0x06,0x1d,0xc1,0x30,0x51,0x36,0xc6,
  6997. 0xc2,0x2b,0x8b,0xaf,0x0c,0x01,0x27,0xa9
  6998. };
  6999. WOLFSSL_SMALL_STACK_STATIC const byte correct2[] =
  7000. {
  7001. 0xa6,0xf7,0x45,0x00,0x8f,0x81,0xc9,0x16,
  7002. 0xa2,0x0d,0xcc,0x74,0xee,0xf2,0xb2,0xf0
  7003. };
  7004. WOLFSSL_SMALL_STACK_STATIC const byte correct3[] =
  7005. {
  7006. 0x49,0xec,0x78,0x09,0x0e,0x48,0x1e,0xc6,
  7007. 0xc2,0x6b,0x33,0xb9,0x1c,0xcc,0x03,0x07
  7008. };
  7009. WOLFSSL_SMALL_STACK_STATIC const byte correct4[] =
  7010. {
  7011. 0x1a,0xe1,0x0b,0x59,0x4f,0x09,0xe2,0x6a,
  7012. 0x7e,0x90,0x2e,0xcb,0xd0,0x60,0x06,0x91
  7013. };
  7014. WOLFSSL_SMALL_STACK_STATIC const byte correct5[] =
  7015. {
  7016. 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7017. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7018. };
  7019. WOLFSSL_SMALL_STACK_STATIC const byte correct6[] =
  7020. {
  7021. 0xea,0x11,0x5c,0x4f,0xd0,0xc0,0x10,0xae,
  7022. 0xf7,0xdf,0xda,0x77,0xa2,0xe9,0xaf,0xca
  7023. };
  7024. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  7025. 0x85,0xd6,0xbe,0x78,0x57,0x55,0x6d,0x33,
  7026. 0x7f,0x44,0x52,0xfe,0x42,0xd5,0x06,0xa8,
  7027. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  7028. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  7029. };
  7030. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  7031. 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,
  7032. 0x33,0x32,0x2d,0x62,0x79,0x74,0x65,0x20,
  7033. 0x6b,0x65,0x79,0x20,0x66,0x6f,0x72,0x20,
  7034. 0x50,0x6f,0x6c,0x79,0x31,0x33,0x30,0x35
  7035. };
  7036. WOLFSSL_SMALL_STACK_STATIC const byte key4[] = {
  7037. 0x7b,0xac,0x2b,0x25,0x2d,0xb4,0x47,0xaf,
  7038. 0x09,0xb6,0x7a,0x55,0xa4,0xe9,0x55,0x84,
  7039. 0x0a,0xe1,0xd6,0x73,0x10,0x75,0xd9,0xeb,
  7040. 0x2a,0x93,0x75,0x78,0x3e,0xd5,0x53,0xff
  7041. };
  7042. WOLFSSL_SMALL_STACK_STATIC const byte key5[] = {
  7043. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7044. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7045. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7046. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7047. };
  7048. const byte* msgs[] = {NULL, msg1, msg2, msg3, msg5, msg6};
  7049. word32 szm[] = {0, sizeof(msg1), sizeof(msg2),
  7050. sizeof(msg3), sizeof(msg5), sizeof(msg6)};
  7051. const byte* keys[] = {key, key, key2, key2, key5, key};
  7052. const byte* tests[] = {correct0, correct1, correct2, correct3, correct5,
  7053. correct6};
  7054. int i;
  7055. wc_test_ret_t ret = 0;
  7056. WOLFSSL_ENTER("poly1305_test");
  7057. for (i = 0; i < 6; i++) {
  7058. ret = wc_Poly1305SetKey(&enc, keys[i], 32);
  7059. if (ret != 0)
  7060. return WC_TEST_RET_ENC_I(i);
  7061. ret = wc_Poly1305Update(&enc, msgs[i], szm[i]);
  7062. if (ret != 0)
  7063. return WC_TEST_RET_ENC_I(i);
  7064. ret = wc_Poly1305Final(&enc, tag);
  7065. if (ret != 0)
  7066. return WC_TEST_RET_ENC_I(i);
  7067. if (XMEMCMP(tag, tests[i], sizeof(tag)))
  7068. return WC_TEST_RET_ENC_I(i);
  7069. }
  7070. /* Check TLS MAC function from 2.8.2 https://tools.ietf.org/html/rfc7539 */
  7071. XMEMSET(tag, 0, sizeof(tag));
  7072. ret = wc_Poly1305SetKey(&enc, key4, sizeof(key4));
  7073. if (ret != 0)
  7074. return WC_TEST_RET_ENC_EC(ret);
  7075. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  7076. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  7077. if (ret != 0)
  7078. return WC_TEST_RET_ENC_EC(ret);
  7079. if (XMEMCMP(tag, correct4, sizeof(tag)))
  7080. return WC_TEST_RET_ENC_NC;
  7081. /* Check fail of TLS MAC function if altering additional data */
  7082. XMEMSET(tag, 0, sizeof(tag));
  7083. additional[0]++;
  7084. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  7085. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  7086. if (ret != 0)
  7087. return WC_TEST_RET_ENC_EC(ret);
  7088. if (XMEMCMP(tag, correct4, sizeof(tag)) == 0)
  7089. return WC_TEST_RET_ENC_NC;
  7090. return 0;
  7091. }
  7092. #endif /* HAVE_POLY1305 */
  7093. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  7094. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha20_poly1305_aead_test(void)
  7095. {
  7096. /* Test #1 from Section 2.8.2 of draft-irtf-cfrg-chacha20-poly1305-10 */
  7097. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  7098. WOLFSSL_SMALL_STACK_STATIC const byte key1[] = {
  7099. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  7100. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  7101. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  7102. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  7103. };
  7104. WOLFSSL_SMALL_STACK_STATIC const byte plaintext1[] = {
  7105. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  7106. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  7107. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  7108. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  7109. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  7110. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  7111. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  7112. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  7113. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  7114. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  7115. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  7116. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  7117. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  7118. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  7119. 0x74, 0x2e
  7120. };
  7121. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] = {
  7122. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  7123. 0x44, 0x45, 0x46, 0x47
  7124. };
  7125. WOLFSSL_SMALL_STACK_STATIC const byte aad1[] = { /* additional data */
  7126. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  7127. 0xc4, 0xc5, 0xc6, 0xc7
  7128. };
  7129. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] = { /* expected output from operation */
  7130. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  7131. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  7132. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  7133. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  7134. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  7135. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  7136. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  7137. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  7138. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  7139. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  7140. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  7141. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  7142. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  7143. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  7144. 0x61, 0x16
  7145. };
  7146. WOLFSSL_SMALL_STACK_STATIC const byte authTag1[] = { /* expected output from operation */
  7147. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  7148. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  7149. };
  7150. /* Test #2 from Appendix A.2 in draft-irtf-cfrg-chacha20-poly1305-10 */
  7151. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  7152. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  7153. 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
  7154. 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
  7155. 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
  7156. 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
  7157. };
  7158. WOLFSSL_SMALL_STACK_STATIC const byte plaintext2[] = {
  7159. 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
  7160. 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
  7161. 0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
  7162. 0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  7163. 0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69,
  7164. 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20,
  7165. 0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20,
  7166. 0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d,
  7167. 0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e,
  7168. 0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65,
  7169. 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
  7170. 0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63,
  7171. 0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f,
  7172. 0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64,
  7173. 0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65,
  7174. 0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  7175. 0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
  7176. 0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e,
  7177. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
  7178. 0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72,
  7179. 0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20,
  7180. 0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65,
  7181. 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61,
  7182. 0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
  7183. 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65,
  7184. 0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
  7185. 0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20,
  7186. 0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
  7187. 0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
  7188. 0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20,
  7189. 0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b,
  7190. 0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67,
  7191. 0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80,
  7192. 0x9d
  7193. };
  7194. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  7195. 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  7196. 0x05, 0x06, 0x07, 0x08
  7197. };
  7198. WOLFSSL_SMALL_STACK_STATIC const byte aad2[] = { /* additional data */
  7199. 0xf3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
  7200. 0x00, 0x00, 0x4e, 0x91
  7201. };
  7202. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] = { /* expected output from operation */
  7203. 0x64, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4,
  7204. 0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd,
  7205. 0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89,
  7206. 0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2,
  7207. 0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee,
  7208. 0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0,
  7209. 0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00,
  7210. 0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf,
  7211. 0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce,
  7212. 0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81,
  7213. 0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd,
  7214. 0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55,
  7215. 0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61,
  7216. 0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38,
  7217. 0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0,
  7218. 0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4,
  7219. 0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46,
  7220. 0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9,
  7221. 0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e,
  7222. 0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e,
  7223. 0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15,
  7224. 0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a,
  7225. 0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea,
  7226. 0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a,
  7227. 0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99,
  7228. 0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e,
  7229. 0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10,
  7230. 0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10,
  7231. 0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94,
  7232. 0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30,
  7233. 0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf,
  7234. 0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29,
  7235. 0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70,
  7236. 0x9b
  7237. };
  7238. WOLFSSL_SMALL_STACK_STATIC const byte authTag2[] = { /* expected output from operation */
  7239. 0xee, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22,
  7240. 0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38
  7241. };
  7242. byte generatedCiphertext[265]; /* max plaintext2/cipher2 */
  7243. byte generatedPlaintext[265]; /* max plaintext2/cipher2 */
  7244. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  7245. ChaChaPoly_Aead aead;
  7246. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  7247. #define TEST_SMALL_CHACHA_CHUNKS 32
  7248. #else
  7249. #define TEST_SMALL_CHACHA_CHUNKS 64
  7250. #endif
  7251. #ifdef TEST_SMALL_CHACHA_CHUNKS
  7252. word32 testLen;
  7253. #endif
  7254. wc_test_ret_t err;
  7255. WOLFSSL_ENTER("chacha20_poly1305_aead_test");
  7256. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  7257. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  7258. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  7259. /* Parameter Validation testing */
  7260. /* Encrypt */
  7261. err = wc_ChaCha20Poly1305_Encrypt(NULL, iv1, aad1, sizeof(aad1), plaintext1,
  7262. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  7263. if (err != BAD_FUNC_ARG)
  7264. return WC_TEST_RET_ENC_EC(err);
  7265. err = wc_ChaCha20Poly1305_Encrypt(key1, NULL, aad1, sizeof(aad1),
  7266. plaintext1, sizeof(plaintext1), generatedCiphertext,
  7267. generatedAuthTag);
  7268. if (err != BAD_FUNC_ARG)
  7269. return WC_TEST_RET_ENC_EC(err);
  7270. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  7271. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  7272. if (err != BAD_FUNC_ARG)
  7273. return WC_TEST_RET_ENC_EC(err);
  7274. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  7275. sizeof(plaintext1), NULL, generatedAuthTag);
  7276. if (err != BAD_FUNC_ARG)
  7277. return WC_TEST_RET_ENC_EC(err);
  7278. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  7279. sizeof(plaintext1), generatedCiphertext, NULL);
  7280. if (err != BAD_FUNC_ARG)
  7281. return WC_TEST_RET_ENC_EC(err);
  7282. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  7283. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  7284. if (err != BAD_FUNC_ARG)
  7285. return WC_TEST_RET_ENC_EC(err);
  7286. /* Decrypt */
  7287. err = wc_ChaCha20Poly1305_Decrypt(NULL, iv2, aad2, sizeof(aad2), cipher2,
  7288. sizeof(cipher2), authTag2, generatedPlaintext);
  7289. if (err != BAD_FUNC_ARG)
  7290. return WC_TEST_RET_ENC_EC(err);
  7291. err = wc_ChaCha20Poly1305_Decrypt(key2, NULL, aad2, sizeof(aad2), cipher2,
  7292. sizeof(cipher2), authTag2, generatedPlaintext);
  7293. if (err != BAD_FUNC_ARG)
  7294. return WC_TEST_RET_ENC_EC(err);
  7295. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  7296. sizeof(cipher2), authTag2, generatedPlaintext);
  7297. if (err != BAD_FUNC_ARG)
  7298. return WC_TEST_RET_ENC_EC(err);
  7299. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  7300. sizeof(cipher2), NULL, generatedPlaintext);
  7301. if (err != BAD_FUNC_ARG)
  7302. return WC_TEST_RET_ENC_EC(err);
  7303. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  7304. sizeof(cipher2), authTag2, NULL);
  7305. if (err != BAD_FUNC_ARG)
  7306. return WC_TEST_RET_ENC_EC(err);
  7307. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  7308. sizeof(cipher2), authTag2, generatedPlaintext);
  7309. if (err != BAD_FUNC_ARG)
  7310. return WC_TEST_RET_ENC_EC(err);
  7311. /* Test #1 */
  7312. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1,
  7313. aad1, sizeof(aad1),
  7314. plaintext1, sizeof(plaintext1),
  7315. generatedCiphertext, generatedAuthTag);
  7316. if (err) {
  7317. return WC_TEST_RET_ENC_EC(err);
  7318. }
  7319. /* -- Check the ciphertext and authtag */
  7320. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  7321. return WC_TEST_RET_ENC_NC;
  7322. }
  7323. if (XMEMCMP(generatedAuthTag, authTag1, sizeof(authTag1))) {
  7324. return WC_TEST_RET_ENC_NC;
  7325. }
  7326. /* -- Verify decryption works */
  7327. err = wc_ChaCha20Poly1305_Decrypt(key1, iv1,
  7328. aad1, sizeof(aad1),
  7329. cipher1, sizeof(cipher1),
  7330. authTag1, generatedPlaintext);
  7331. if (err) {
  7332. return err;
  7333. }
  7334. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  7335. return WC_TEST_RET_ENC_NC;
  7336. }
  7337. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  7338. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  7339. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  7340. /* Test #2 */
  7341. err = wc_ChaCha20Poly1305_Encrypt(key2, iv2,
  7342. aad2, sizeof(aad2),
  7343. plaintext2, sizeof(plaintext2),
  7344. generatedCiphertext, generatedAuthTag);
  7345. if (err) {
  7346. return err;
  7347. }
  7348. /* -- Check the ciphertext and authtag */
  7349. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  7350. return WC_TEST_RET_ENC_NC;
  7351. }
  7352. if (XMEMCMP(generatedAuthTag, authTag2, sizeof(authTag2))) {
  7353. return WC_TEST_RET_ENC_NC;
  7354. }
  7355. /* -- Verify decryption works */
  7356. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2,
  7357. aad2, sizeof(aad2),
  7358. cipher2, sizeof(cipher2),
  7359. authTag2, generatedPlaintext);
  7360. if (err) {
  7361. return err;
  7362. }
  7363. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  7364. return WC_TEST_RET_ENC_NC;
  7365. }
  7366. /* AEAD init/update/final - bad argument tests */
  7367. err = wc_ChaCha20Poly1305_Init(NULL, key1, iv1,
  7368. CHACHA20_POLY1305_AEAD_DECRYPT);
  7369. if (err != BAD_FUNC_ARG)
  7370. return WC_TEST_RET_ENC_EC(err);
  7371. err = wc_ChaCha20Poly1305_Init(&aead, NULL, iv1,
  7372. CHACHA20_POLY1305_AEAD_DECRYPT);
  7373. if (err != BAD_FUNC_ARG)
  7374. return WC_TEST_RET_ENC_EC(err);
  7375. err = wc_ChaCha20Poly1305_Init(&aead, key1, NULL,
  7376. CHACHA20_POLY1305_AEAD_DECRYPT);
  7377. if (err != BAD_FUNC_ARG)
  7378. return WC_TEST_RET_ENC_EC(err);
  7379. err = wc_ChaCha20Poly1305_UpdateAad(NULL, aad1, sizeof(aad1));
  7380. if (err != BAD_FUNC_ARG)
  7381. return WC_TEST_RET_ENC_EC(err);
  7382. err = wc_ChaCha20Poly1305_UpdateAad(&aead, NULL, sizeof(aad1));
  7383. if (err != BAD_FUNC_ARG)
  7384. return WC_TEST_RET_ENC_EC(err);
  7385. err = wc_ChaCha20Poly1305_UpdateData(NULL, generatedPlaintext,
  7386. generatedPlaintext, sizeof(plaintext1));
  7387. if (err != BAD_FUNC_ARG)
  7388. return WC_TEST_RET_ENC_EC(err);
  7389. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext, NULL,
  7390. sizeof(plaintext1));
  7391. if (err != BAD_FUNC_ARG)
  7392. return WC_TEST_RET_ENC_EC(err);
  7393. err = wc_ChaCha20Poly1305_UpdateData(&aead, NULL, generatedPlaintext,
  7394. sizeof(plaintext1));
  7395. if (err != BAD_FUNC_ARG)
  7396. return WC_TEST_RET_ENC_EC(err);
  7397. err = wc_ChaCha20Poly1305_Final(NULL, generatedAuthTag);
  7398. if (err != BAD_FUNC_ARG)
  7399. return WC_TEST_RET_ENC_EC(err);
  7400. err = wc_ChaCha20Poly1305_Final(&aead, NULL);
  7401. if (err != BAD_FUNC_ARG)
  7402. return WC_TEST_RET_ENC_EC(err);
  7403. /* AEAD init/update/final - bad state tests */
  7404. /* clear struct - make valgrind happy to resolve
  7405. "Conditional jump or move depends on uninitialised value(s)".
  7406. The enum is "int" size and aead.state is "byte" */
  7407. /* The wc_ChaCha20Poly1305_Init function does this normally */
  7408. XMEMSET(&aead, 0, sizeof(aead));
  7409. aead.state = CHACHA20_POLY1305_STATE_INIT;
  7410. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  7411. if (err != BAD_STATE_E)
  7412. return WC_TEST_RET_ENC_EC(err);
  7413. aead.state = CHACHA20_POLY1305_STATE_DATA;
  7414. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  7415. if (err != BAD_STATE_E)
  7416. return WC_TEST_RET_ENC_EC(err);
  7417. aead.state = CHACHA20_POLY1305_STATE_INIT;
  7418. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext,
  7419. generatedPlaintext, sizeof(plaintext1));
  7420. if (err != BAD_STATE_E)
  7421. return WC_TEST_RET_ENC_EC(err);
  7422. aead.state = CHACHA20_POLY1305_STATE_INIT;
  7423. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7424. if (err != BAD_STATE_E)
  7425. return WC_TEST_RET_ENC_EC(err);
  7426. aead.state = CHACHA20_POLY1305_STATE_READY;
  7427. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7428. if (err != BAD_STATE_E)
  7429. return WC_TEST_RET_ENC_EC(err);
  7430. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  7431. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  7432. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  7433. /* Test 1 - Encrypt */
  7434. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  7435. CHACHA20_POLY1305_AEAD_ENCRYPT);
  7436. if (err != 0)
  7437. return WC_TEST_RET_ENC_EC(err);
  7438. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  7439. if (err != 0)
  7440. return WC_TEST_RET_ENC_EC(err);
  7441. #ifdef TEST_SMALL_CHACHA_CHUNKS
  7442. /* test doing data in smaller chunks */
  7443. for (testLen=0; testLen<sizeof(plaintext1); ) {
  7444. word32 dataLen = sizeof(plaintext1) - testLen;
  7445. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  7446. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  7447. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext1[testLen],
  7448. &generatedCiphertext[testLen], dataLen);
  7449. if (err != 0)
  7450. return WC_TEST_RET_ENC_EC(err);
  7451. testLen += dataLen;
  7452. }
  7453. #else
  7454. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext1,
  7455. generatedCiphertext, sizeof(plaintext1));
  7456. #endif
  7457. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7458. if (err != 0)
  7459. return WC_TEST_RET_ENC_EC(err);
  7460. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  7461. if (err != 0)
  7462. return WC_TEST_RET_ENC_EC(err);
  7463. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  7464. return WC_TEST_RET_ENC_NC;
  7465. }
  7466. /* Test 1 - Decrypt */
  7467. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  7468. CHACHA20_POLY1305_AEAD_DECRYPT);
  7469. if (err != 0)
  7470. return WC_TEST_RET_ENC_EC(err);
  7471. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  7472. if (err != 0)
  7473. return WC_TEST_RET_ENC_EC(err);
  7474. #ifdef TEST_SMALL_CHACHA_CHUNKS
  7475. /* test doing data in smaller chunks */
  7476. for (testLen=0; testLen<sizeof(plaintext1); ) {
  7477. word32 dataLen = sizeof(plaintext1) - testLen;
  7478. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  7479. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  7480. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  7481. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  7482. dataLen);
  7483. if (err != 0)
  7484. return WC_TEST_RET_ENC_EC(err);
  7485. testLen += dataLen;
  7486. }
  7487. #else
  7488. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  7489. generatedPlaintext, sizeof(cipher1));
  7490. #endif
  7491. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7492. if (err != 0)
  7493. return WC_TEST_RET_ENC_EC(err);
  7494. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  7495. if (err != 0)
  7496. return WC_TEST_RET_ENC_EC(err);
  7497. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  7498. return WC_TEST_RET_ENC_NC;
  7499. }
  7500. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  7501. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  7502. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  7503. /* Test 2 - Encrypt */
  7504. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  7505. CHACHA20_POLY1305_AEAD_ENCRYPT);
  7506. if (err != 0)
  7507. return WC_TEST_RET_ENC_EC(err);
  7508. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  7509. if (err != 0)
  7510. return WC_TEST_RET_ENC_EC(err);
  7511. #ifdef TEST_SMALL_CHACHA_CHUNKS
  7512. /* test doing data in smaller chunks */
  7513. for (testLen=0; testLen<sizeof(plaintext2); ) {
  7514. word32 dataLen = sizeof(plaintext2) - testLen;
  7515. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  7516. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  7517. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext2[testLen],
  7518. &generatedCiphertext[testLen], dataLen);
  7519. if (err != 0)
  7520. return WC_TEST_RET_ENC_EC(err);
  7521. testLen += dataLen;
  7522. }
  7523. #else
  7524. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext2, generatedCiphertext,
  7525. sizeof(plaintext2));
  7526. #endif
  7527. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7528. if (err != 0)
  7529. return WC_TEST_RET_ENC_EC(err);
  7530. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  7531. if (err != 0)
  7532. return WC_TEST_RET_ENC_EC(err);
  7533. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  7534. return WC_TEST_RET_ENC_NC;
  7535. }
  7536. /* Test 2 - Decrypt */
  7537. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  7538. CHACHA20_POLY1305_AEAD_DECRYPT);
  7539. if (err != 0)
  7540. return WC_TEST_RET_ENC_EC(err);
  7541. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  7542. if (err != 0)
  7543. return WC_TEST_RET_ENC_EC(err);
  7544. #ifdef TEST_SMALL_CHACHA_CHUNKS
  7545. /* test doing data in smaller chunks */
  7546. for (testLen=0; testLen<sizeof(plaintext2); ) {
  7547. word32 dataLen = sizeof(plaintext2) - testLen;
  7548. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  7549. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  7550. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  7551. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  7552. dataLen);
  7553. if (err != 0)
  7554. return WC_TEST_RET_ENC_EC(err);
  7555. testLen += dataLen;
  7556. }
  7557. #else
  7558. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  7559. generatedPlaintext, sizeof(cipher2));
  7560. #endif
  7561. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7562. if (err != 0)
  7563. return WC_TEST_RET_ENC_EC(err);
  7564. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  7565. if (err != 0)
  7566. return WC_TEST_RET_ENC_EC(err);
  7567. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  7568. return WC_TEST_RET_ENC_NC;
  7569. }
  7570. return err;
  7571. }
  7572. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  7573. #ifndef NO_DES3
  7574. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des_test(void)
  7575. {
  7576. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  7577. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7578. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7579. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7580. };
  7581. byte plain[24];
  7582. byte cipher[24];
  7583. Des enc;
  7584. Des dec;
  7585. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  7586. {
  7587. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  7588. };
  7589. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  7590. {
  7591. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  7592. };
  7593. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  7594. {
  7595. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  7596. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  7597. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  7598. };
  7599. wc_test_ret_t ret;
  7600. WOLFSSL_ENTER("des_test");
  7601. ret = wc_Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  7602. if (ret != 0)
  7603. return WC_TEST_RET_ENC_EC(ret);
  7604. ret = wc_Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  7605. if (ret != 0)
  7606. return WC_TEST_RET_ENC_EC(ret);
  7607. ret = wc_Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  7608. if (ret != 0)
  7609. return WC_TEST_RET_ENC_EC(ret);
  7610. ret = wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  7611. if (ret != 0)
  7612. return WC_TEST_RET_ENC_EC(ret);
  7613. if (XMEMCMP(plain, vector, sizeof(plain)))
  7614. return WC_TEST_RET_ENC_NC;
  7615. if (XMEMCMP(cipher, verify, sizeof(cipher)))
  7616. return WC_TEST_RET_ENC_NC;
  7617. ret = wc_Des_CbcEncryptWithKey(cipher, vector, sizeof(vector), key, iv);
  7618. if (ret != 0)
  7619. return WC_TEST_RET_ENC_EC(ret);
  7620. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  7621. {
  7622. EncryptedInfo info;
  7623. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  7624. XMEMCPY(info.iv, iv, sizeof(iv));
  7625. info.ivSz = sizeof(iv);
  7626. info.keySz = sizeof(key);
  7627. info.cipherType = WC_CIPHER_DES;
  7628. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  7629. sizeof(key), WC_HASH_TYPE_SHA);
  7630. if (ret != 0)
  7631. return WC_TEST_RET_ENC_EC(ret);
  7632. /* Test invalid info ptr */
  7633. ret = wc_BufferKeyEncrypt(NULL, cipher, sizeof(cipher), key,
  7634. sizeof(key), WC_HASH_TYPE_SHA);
  7635. if (ret != BAD_FUNC_ARG)
  7636. return WC_TEST_RET_ENC_EC(ret);
  7637. #ifndef NO_PWDBASED
  7638. /* Test invalid hash type - only applies to wc_PBKDF1 call */
  7639. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  7640. sizeof(key), WC_HASH_TYPE_NONE);
  7641. if (ret == 0)
  7642. return WC_TEST_RET_ENC_EC(ret);
  7643. #endif /* !NO_PWDBASED */
  7644. }
  7645. #endif
  7646. return 0;
  7647. }
  7648. #endif /* !NO_DES3 */
  7649. #ifndef NO_DES3
  7650. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des3_test(void)
  7651. {
  7652. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  7653. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7654. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7655. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7656. };
  7657. byte plain[24];
  7658. byte cipher[24];
  7659. Des3 enc;
  7660. Des3 dec;
  7661. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  7662. {
  7663. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  7664. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  7665. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  7666. };
  7667. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  7668. {
  7669. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  7670. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  7671. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  7672. };
  7673. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] =
  7674. {
  7675. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  7676. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  7677. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  7678. };
  7679. wc_test_ret_t ret;
  7680. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7681. size_t i;
  7682. #endif
  7683. WOLFSSL_ENTER("des3_test");
  7684. ret = wc_Des3Init(&enc, HEAP_HINT, devId);
  7685. if (ret != 0)
  7686. return WC_TEST_RET_ENC_EC(ret);
  7687. ret = wc_Des3Init(&dec, HEAP_HINT, devId);
  7688. if (ret != 0)
  7689. return WC_TEST_RET_ENC_EC(ret);
  7690. ret = wc_Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  7691. if (ret != 0)
  7692. return WC_TEST_RET_ENC_EC(ret);
  7693. ret = wc_Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  7694. if (ret != 0)
  7695. return WC_TEST_RET_ENC_EC(ret);
  7696. ret = wc_Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  7697. #if defined(WOLFSSL_ASYNC_CRYPT)
  7698. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  7699. #endif
  7700. if (ret != 0)
  7701. return WC_TEST_RET_ENC_EC(ret);
  7702. ret = wc_Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  7703. #if defined(WOLFSSL_ASYNC_CRYPT)
  7704. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  7705. #endif
  7706. if (ret != 0)
  7707. return WC_TEST_RET_ENC_EC(ret);
  7708. if (XMEMCMP(plain, vector, sizeof(plain)))
  7709. return WC_TEST_RET_ENC_NC;
  7710. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  7711. return WC_TEST_RET_ENC_NC;
  7712. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7713. /* test the same vectors with using compatibility layer */
  7714. for (i = 0; i < sizeof(vector); i += DES_BLOCK_SIZE){
  7715. DES_key_schedule ks1;
  7716. DES_key_schedule ks2;
  7717. DES_key_schedule ks3;
  7718. DES_cblock iv4;
  7719. byte tmp[sizeof(vector)];
  7720. XMEMCPY(ks1, key3, sizeof(DES_key_schedule));
  7721. XMEMCPY(ks2, key3 + 8, sizeof(DES_key_schedule));
  7722. XMEMCPY(ks3, key3 + 16, sizeof(DES_key_schedule));
  7723. XMEMSET(plain, 0, sizeof(plain));
  7724. XMEMSET(cipher, 0, sizeof(cipher));
  7725. /* Test in-place encrypt/decrypt */
  7726. XMEMCPY(tmp, vector, sizeof(vector));
  7727. /* Use i as the splitter */
  7728. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  7729. DES_ede3_cbc_encrypt(tmp, tmp, (long)i, &ks1, &ks2, &ks3,
  7730. &iv4, DES_ENCRYPT);
  7731. DES_ede3_cbc_encrypt(tmp + i, tmp + i, (long)(sizeof(vector) - i),
  7732. &ks1, &ks2, &ks3, &iv4, DES_ENCRYPT);
  7733. XMEMCPY(cipher, tmp, sizeof(cipher));
  7734. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  7735. DES_ede3_cbc_encrypt(tmp, tmp, (long)i, &ks1, &ks2, &ks3,
  7736. &iv4, DES_DECRYPT);
  7737. DES_ede3_cbc_encrypt(tmp + i, tmp + i, (long)(sizeof(cipher) - i),
  7738. &ks1, &ks2, &ks3, &iv4, DES_DECRYPT);
  7739. XMEMCPY(plain, tmp, sizeof(plain));
  7740. if (XMEMCMP(plain, vector, sizeof(plain)))
  7741. return WC_TEST_RET_ENC_NC;
  7742. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  7743. return WC_TEST_RET_ENC_NC;
  7744. }
  7745. #endif /* OPENSSL_EXTRA */
  7746. wc_Des3Free(&enc);
  7747. wc_Des3Free(&dec);
  7748. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  7749. {
  7750. EncryptedInfo info;
  7751. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  7752. XMEMCPY(info.iv, iv3, sizeof(iv3));
  7753. info.ivSz = sizeof(iv3);
  7754. info.keySz = sizeof(key3);
  7755. info.cipherType = WC_CIPHER_DES3;
  7756. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key3,
  7757. sizeof(key3), WC_HASH_TYPE_SHA);
  7758. if (ret != 0)
  7759. return WC_TEST_RET_ENC_EC(ret);
  7760. }
  7761. #endif
  7762. return 0;
  7763. }
  7764. #endif /* NO_DES3 */
  7765. static const int fiducial1 = WC_TEST_RET_LN; /* source code reference point --
  7766. * see print_fiducials() below.
  7767. */
  7768. #ifndef NO_AES
  7769. #if defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_CFB) || \
  7770. defined(WOLFSSL_AES_XTS)
  7771. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7772. && !defined(HAVE_SELFTEST)
  7773. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  7774. /* pass in the function, key, iv, plain text and expected and this function
  7775. * tests that the encryption and decryption is successful */
  7776. static wc_test_ret_t EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key,
  7777. const byte* iv, const byte* plain, int plainSz,
  7778. const byte* expected, int expectedSz)
  7779. {
  7780. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7781. EVP_CIPHER_CTX *ctx = NULL;
  7782. #else
  7783. EVP_CIPHER_CTX ctx[1];
  7784. #endif
  7785. int ctx_inited = 0;
  7786. int idx, cipherSz;
  7787. wc_test_ret_t ret = 0;
  7788. byte* cipher;
  7789. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7790. if ((ctx = wolfSSL_EVP_CIPHER_CTX_new()) == NULL)
  7791. return MEMORY_E;
  7792. #endif
  7793. cipher = (byte*)XMALLOC(plainSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7794. if (cipher == NULL) {
  7795. ret = WC_TEST_RET_ENC_ERRNO;
  7796. goto EVP_TEST_END;
  7797. }
  7798. /* test encrypt */
  7799. EVP_CIPHER_CTX_init(ctx);
  7800. ctx_inited = 1;
  7801. if (EVP_CipherInit(ctx, type, key, iv, 1) == 0) {
  7802. ret = WC_TEST_RET_ENC_NC;
  7803. goto EVP_TEST_END;
  7804. }
  7805. if (EVP_CipherUpdate(ctx, cipher, &idx, plain, expectedSz) == 0) {
  7806. ret = WC_TEST_RET_ENC_NC;
  7807. goto EVP_TEST_END;
  7808. }
  7809. cipherSz = idx;
  7810. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  7811. ret = WC_TEST_RET_ENC_NC;
  7812. goto EVP_TEST_END;
  7813. }
  7814. cipherSz += idx;
  7815. if (XMEMCMP(cipher, expected, plainSz)) {
  7816. ret = WC_TEST_RET_ENC_NC;
  7817. goto EVP_TEST_END;
  7818. }
  7819. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(ctx);
  7820. ctx_inited = 0;
  7821. if (ret == WOLFSSL_SUCCESS)
  7822. ret = 0;
  7823. else {
  7824. ret = WC_TEST_RET_ENC_NC;
  7825. goto EVP_TEST_END;
  7826. }
  7827. /* test decrypt */
  7828. EVP_CIPHER_CTX_init(ctx);
  7829. ctx_inited = 1;
  7830. if (EVP_CipherInit(ctx, type, key, iv, 0) == 0) {
  7831. ret = WC_TEST_RET_ENC_NC;
  7832. goto EVP_TEST_END;
  7833. }
  7834. if (EVP_CipherUpdate(ctx, cipher, &idx, cipher, expectedSz) == 0) {
  7835. ret = WC_TEST_RET_ENC_NC;
  7836. goto EVP_TEST_END;
  7837. }
  7838. cipherSz = idx;
  7839. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  7840. ret = WC_TEST_RET_ENC_NC;
  7841. goto EVP_TEST_END;
  7842. }
  7843. cipherSz += idx;
  7844. if ((expectedSz != cipherSz) || XMEMCMP(plain, cipher, plainSz)) {
  7845. ret = WC_TEST_RET_ENC_NC;
  7846. goto EVP_TEST_END;
  7847. }
  7848. EVP_TEST_END:
  7849. if (cipher)
  7850. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7851. (void)cipherSz;
  7852. if (ctx_inited) {
  7853. int cleanup_ret = wolfSSL_EVP_CIPHER_CTX_cleanup(ctx);
  7854. if (cleanup_ret != WOLFSSL_SUCCESS)
  7855. ret = WC_TEST_RET_ENC_NC;
  7856. }
  7857. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7858. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  7859. #endif
  7860. return ret;
  7861. }
  7862. #endif /* !HAVE_FIPS || FIPS_VERSION3_GE(6,0,0) */
  7863. #endif /* OPENSSL_EXTRA && !WOLFCRYPT_ONLY && !HAVE_SELFTEST */
  7864. #endif /* WOLFSSL_AES_OFB || WOLFSSL_AES_CFB */
  7865. #ifdef WOLFSSL_AES_OFB
  7866. /* test vector from https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/Block-Ciphers */
  7867. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesofb_test(void)
  7868. {
  7869. #ifdef WOLFSSL_AES_256
  7870. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  7871. {
  7872. 0xc4,0xc7,0xfa,0xd6,0x53,0x5c,0xb8,0x71,
  7873. 0x4a,0x5c,0x40,0x77,0x9a,0x8b,0xa1,0xd2,
  7874. 0x53,0x3e,0x23,0xb4,0xb2,0x58,0x73,0x2a,
  7875. 0x5b,0x78,0x01,0xf4,0xe3,0x71,0xa7,0x94
  7876. };
  7877. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  7878. {
  7879. 0x5e,0xb9,0x33,0x13,0xb8,0x71,0xff,0x16,
  7880. 0xb9,0x8a,0x9b,0xcb,0x43,0x33,0x0d,0x6f
  7881. };
  7882. WOLFSSL_SMALL_STACK_STATIC const byte plain1[] =
  7883. {
  7884. 0x6d,0x0b,0xb0,0x79,0x63,0x84,0x71,0xe9,
  7885. 0x39,0xd4,0x53,0x14,0x86,0xc1,0x4c,0x25,
  7886. 0x9a,0xee,0xc6,0xf3,0xc0,0x0d,0xfd,0xd6,
  7887. 0xc0,0x50,0xa8,0xba,0xa8,0x20,0xdb,0x71,
  7888. 0xcc,0x12,0x2c,0x4e,0x0c,0x17,0x15,0xef,
  7889. 0x55,0xf3,0x99,0x5a,0x6b,0xf0,0x2a,0x4c
  7890. };
  7891. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  7892. {
  7893. 0x0f,0x54,0x61,0x71,0x59,0xd0,0x3f,0xfc,
  7894. 0x1b,0xfa,0xfb,0x60,0x29,0x30,0xd7,0x00,
  7895. 0xf4,0xa4,0xa8,0xe6,0xdd,0x93,0x94,0x46,
  7896. 0x64,0xd2,0x19,0xc4,0xc5,0x4d,0xde,0x1b,
  7897. 0x04,0x53,0xe1,0x73,0xf5,0x18,0x74,0xae,
  7898. 0xfd,0x64,0xa2,0xe1,0xe2,0x76,0x13,0xb0
  7899. };
  7900. #endif /* WOLFSSL_AES_256 */
  7901. #ifdef WOLFSSL_AES_128
  7902. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  7903. {
  7904. 0x10,0xa5,0x88,0x69,0xd7,0x4b,0xe5,0xa3,
  7905. 0x74,0xcf,0x86,0x7c,0xfb,0x47,0x38,0x59
  7906. };
  7907. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  7908. {
  7909. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7910. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7911. };
  7912. WOLFSSL_SMALL_STACK_STATIC const byte plain2[] =
  7913. {
  7914. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7915. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7916. };
  7917. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  7918. {
  7919. 0x6d,0x25,0x1e,0x69,0x44,0xb0,0x51,0xe0,
  7920. 0x4e,0xaa,0x6f,0xb4,0xdb,0xf7,0x84,0x65
  7921. };
  7922. #endif /* WOLFSSL_AES_128 */
  7923. #ifdef WOLFSSL_AES_192
  7924. WOLFSSL_SMALL_STACK_STATIC const byte key3[] = {
  7925. 0xd0,0x77,0xa0,0x3b,0xd8,0xa3,0x89,0x73,
  7926. 0x92,0x8c,0xca,0xfe,0x4a,0x9d,0x2f,0x45,
  7927. 0x51,0x30,0xbd,0x0a,0xf5,0xae,0x46,0xa9
  7928. };
  7929. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  7930. {
  7931. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7932. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7933. };
  7934. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  7935. {
  7936. 0xab,0xc7,0x86,0xfb,0x1e,0xdb,0x50,0x45,
  7937. 0x80,0xc4,0xd8,0x82,0xef,0x29,0xa0,0xc7
  7938. };
  7939. WOLFSSL_SMALL_STACK_STATIC const byte plain3[] =
  7940. {
  7941. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7942. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7943. };
  7944. #endif /* WOLFSSL_AES_192 */
  7945. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7946. Aes *enc = NULL;
  7947. #else
  7948. Aes enc[1];
  7949. #endif
  7950. byte cipher[AES_BLOCK_SIZE * 4];
  7951. #ifdef HAVE_AES_DECRYPT
  7952. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7953. Aes *dec = NULL;
  7954. #else
  7955. Aes dec[1];
  7956. #endif
  7957. byte plain [AES_BLOCK_SIZE * 4];
  7958. #endif
  7959. wc_test_ret_t ret = 0;
  7960. WOLFSSL_ENTER("aesofb_test");
  7961. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7962. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7963. ERROR_OUT(-1, out);
  7964. #ifdef HAVE_AES_DECRYPT
  7965. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7966. ERROR_OUT(-1, out);
  7967. #endif
  7968. #endif
  7969. XMEMSET(enc, 0, sizeof *enc);
  7970. #ifdef HAVE_AES_DECRYPT
  7971. XMEMSET(dec, 0, sizeof *dec);
  7972. #endif
  7973. #ifdef WOLFSSL_AES_128
  7974. /* 128 key size test */
  7975. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7976. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7977. ret = EVP_test(EVP_aes_128_ofb(), key2, iv2, plain2, sizeof(plain2),
  7978. cipher2, sizeof(cipher2));
  7979. if (ret != 0) {
  7980. goto out;
  7981. }
  7982. #endif
  7983. ret = wc_AesInit(enc, HEAP_HINT, INVALID_DEVID);
  7984. if (ret != 0)
  7985. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7986. #ifdef HAVE_AES_DECRYPT
  7987. ret = wc_AesInit(dec, HEAP_HINT, INVALID_DEVID);
  7988. if (ret != 0)
  7989. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7990. #endif
  7991. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7992. if (ret != 0)
  7993. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7994. #ifdef HAVE_AES_DECRYPT
  7995. /* decrypt uses AES_ENCRYPTION */
  7996. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7997. if (ret != 0)
  7998. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7999. #endif
  8000. XMEMSET(cipher, 0, sizeof(cipher));
  8001. ret = wc_AesOfbEncrypt(enc, cipher, plain2, AES_BLOCK_SIZE);
  8002. if (ret != 0)
  8003. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8004. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE))
  8005. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8006. #ifdef HAVE_AES_DECRYPT
  8007. ret = wc_AesOfbDecrypt(dec, plain, cipher2, AES_BLOCK_SIZE);
  8008. if (ret != 0)
  8009. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8010. if (XMEMCMP(plain, plain2, AES_BLOCK_SIZE))
  8011. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8012. #endif /* HAVE_AES_DECRYPT */
  8013. #endif /* WOLFSSL_AES_128 */
  8014. #ifdef WOLFSSL_AES_192
  8015. /* 192 key size test */
  8016. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8017. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8018. ret = EVP_test(EVP_aes_192_ofb(), key3, iv3, plain3, sizeof(plain3),
  8019. cipher3, sizeof(cipher3));
  8020. if (ret != 0) {
  8021. goto out;
  8022. }
  8023. #endif
  8024. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  8025. if (ret != 0)
  8026. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8027. #ifdef HAVE_AES_DECRYPT
  8028. /* decrypt uses AES_ENCRYPTION */
  8029. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  8030. if (ret != 0)
  8031. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8032. #endif
  8033. XMEMSET(cipher, 0, sizeof(cipher));
  8034. ret = wc_AesOfbEncrypt(enc, cipher, plain3, AES_BLOCK_SIZE);
  8035. if (ret != 0)
  8036. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8037. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE))
  8038. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8039. #ifdef HAVE_AES_DECRYPT
  8040. ret = wc_AesOfbDecrypt(dec, plain, cipher3, AES_BLOCK_SIZE);
  8041. if (ret != 0)
  8042. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8043. if (XMEMCMP(plain, plain3, AES_BLOCK_SIZE))
  8044. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8045. #endif /* HAVE_AES_DECRYPT */
  8046. #endif /* WOLFSSL_AES_192 */
  8047. #ifdef WOLFSSL_AES_256
  8048. /* 256 key size test */
  8049. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8050. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8051. ret = EVP_test(EVP_aes_256_ofb(), key1, iv1, plain1, sizeof(plain1),
  8052. cipher1, sizeof(cipher1));
  8053. if (ret != 0) {
  8054. goto out;
  8055. }
  8056. #endif
  8057. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8058. if (ret != 0)
  8059. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8060. #ifdef HAVE_AES_DECRYPT
  8061. /* decrypt uses AES_ENCRYPTION */
  8062. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8063. if (ret != 0)
  8064. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8065. #endif
  8066. XMEMSET(cipher, 0, sizeof(cipher));
  8067. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE);
  8068. if (ret != 0)
  8069. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8070. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE))
  8071. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8072. ret = wc_AesOfbEncrypt(enc, cipher + AES_BLOCK_SIZE,
  8073. plain1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  8074. if (ret != 0)
  8075. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8076. if (XMEMCMP(cipher + AES_BLOCK_SIZE, cipher1 + AES_BLOCK_SIZE,
  8077. AES_BLOCK_SIZE))
  8078. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8079. #ifdef HAVE_AES_DECRYPT
  8080. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE);
  8081. if (ret != 0)
  8082. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8083. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE))
  8084. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8085. ret = wc_AesOfbDecrypt(dec, plain + AES_BLOCK_SIZE,
  8086. cipher1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  8087. if (ret != 0)
  8088. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8089. if (XMEMCMP(plain + AES_BLOCK_SIZE, plain1 + AES_BLOCK_SIZE,
  8090. AES_BLOCK_SIZE))
  8091. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8092. #endif /* HAVE_AES_DECRYPT */
  8093. /* multiple blocks at once */
  8094. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8095. if (ret != 0)
  8096. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8097. #ifdef HAVE_AES_DECRYPT
  8098. /* decrypt uses AES_ENCRYPTION */
  8099. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8100. if (ret != 0)
  8101. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8102. #endif
  8103. XMEMSET(cipher, 0, sizeof(cipher));
  8104. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE * 3);
  8105. if (ret != 0)
  8106. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8107. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 3))
  8108. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8109. #ifdef HAVE_AES_DECRYPT
  8110. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE * 3);
  8111. if (ret != 0)
  8112. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8113. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE * 3))
  8114. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8115. #endif /* HAVE_AES_DECRYPT */
  8116. /* inline decrypt/encrypt*/
  8117. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8118. if (ret != 0)
  8119. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8120. #ifdef HAVE_AES_DECRYPT
  8121. /* decrypt uses AES_ENCRYPTION */
  8122. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8123. if (ret != 0)
  8124. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8125. #endif
  8126. XMEMCPY(cipher, plain1, AES_BLOCK_SIZE * 2);
  8127. ret = wc_AesOfbEncrypt(enc, cipher, cipher, AES_BLOCK_SIZE * 2);
  8128. if (ret != 0)
  8129. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8130. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  8131. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8132. #ifdef HAVE_AES_DECRYPT
  8133. ret = wc_AesOfbDecrypt(dec, cipher, cipher, AES_BLOCK_SIZE * 2);
  8134. if (ret != 0)
  8135. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8136. if (XMEMCMP(cipher, plain1, AES_BLOCK_SIZE * 2))
  8137. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8138. #endif /* HAVE_AES_DECRYPT */
  8139. /* 256 key size test leftover support */
  8140. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8141. if (ret != 0)
  8142. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8143. #ifdef HAVE_AES_DECRYPT
  8144. /* decrypt uses AES_ENCRYPTION */
  8145. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8146. if (ret != 0)
  8147. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8148. #endif
  8149. XMEMSET(cipher, 0, sizeof(cipher));
  8150. ret = wc_AesOfbEncrypt(enc, cipher, plain1, 3);
  8151. if (ret != 0)
  8152. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8153. if (XMEMCMP(cipher, cipher1, 3))
  8154. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8155. ret = wc_AesOfbEncrypt(enc, cipher + 3, plain1 + 3, AES_BLOCK_SIZE);
  8156. if (ret != 0)
  8157. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8158. if (XMEMCMP(cipher + 3, cipher1 + 3, AES_BLOCK_SIZE))
  8159. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8160. #ifdef HAVE_AES_DECRYPT
  8161. ret = wc_AesOfbDecrypt(dec, plain, cipher1, 6);
  8162. if (ret != 0)
  8163. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8164. if (XMEMCMP(plain, plain1, 6))
  8165. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8166. ret = wc_AesOfbDecrypt(dec, plain + 6, cipher1 + 6, AES_BLOCK_SIZE);
  8167. if (ret != 0)
  8168. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8169. if (XMEMCMP(plain + 6, plain1 + 6, AES_BLOCK_SIZE))
  8170. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8171. #endif /* HAVE_AES_DECRYPT */
  8172. out:
  8173. wc_AesFree(enc);
  8174. #ifdef HAVE_AES_DECRYPT
  8175. wc_AesFree(dec);
  8176. #endif
  8177. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8178. if (enc)
  8179. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8180. #ifdef HAVE_AES_DECRYPT
  8181. if (dec)
  8182. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8183. #endif
  8184. #endif
  8185. #endif /* WOLFSSL_AES_256 */
  8186. return ret;
  8187. }
  8188. #endif /* WOLFSSL_AES_OFB */
  8189. #if defined(WOLFSSL_AES_CFB)
  8190. /* Test cases from NIST SP 800-38A, Recommendation for Block Cipher Modes of
  8191. * Operation Methods and Techniques
  8192. */
  8193. static wc_test_ret_t aescfb_test_0(void)
  8194. {
  8195. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8196. Aes *enc = NULL;
  8197. #else
  8198. Aes enc[1];
  8199. #endif
  8200. int enc_inited = 0;
  8201. byte cipher[AES_BLOCK_SIZE * 4];
  8202. #ifdef HAVE_AES_DECRYPT
  8203. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8204. Aes *dec = NULL;
  8205. #else
  8206. Aes dec[1];
  8207. #endif
  8208. int dec_inited = 0;
  8209. byte plain [AES_BLOCK_SIZE * 4];
  8210. #endif
  8211. wc_test_ret_t ret = 0;
  8212. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  8213. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  8214. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  8215. };
  8216. #ifdef WOLFSSL_AES_128
  8217. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  8218. {
  8219. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  8220. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  8221. };
  8222. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  8223. {
  8224. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  8225. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  8226. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  8227. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b,
  8228. 0x26,0x75,0x1f,0x67,0xa3,0xcb,0xb1,0x40,
  8229. 0xb1,0x80,0x8c,0xf1,0x87,0xa4,0xf4,0xdf
  8230. };
  8231. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  8232. {
  8233. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8234. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  8235. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  8236. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  8237. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  8238. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef
  8239. };
  8240. #endif /* WOLFSSL_AES_128 */
  8241. #ifdef WOLFSSL_AES_192
  8242. /* 192 size key test */
  8243. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  8244. {
  8245. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  8246. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  8247. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  8248. };
  8249. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  8250. {
  8251. 0xcd,0xc8,0x0d,0x6f,0xdd,0xf1,0x8c,0xab,
  8252. 0x34,0xc2,0x59,0x09,0xc9,0x9a,0x41,0x74,
  8253. 0x67,0xce,0x7f,0x7f,0x81,0x17,0x36,0x21,
  8254. 0x96,0x1a,0x2b,0x70,0x17,0x1d,0x3d,0x7a,
  8255. 0x2e,0x1e,0x8a,0x1d,0xd5,0x9b,0x88,0xb1,
  8256. 0xc8,0xe6,0x0f,0xed,0x1e,0xfa,0xc4,0xc9,
  8257. 0xc0,0x5f,0x9f,0x9c,0xa9,0x83,0x4f,0xa0,
  8258. 0x42,0xae,0x8f,0xba,0x58,0x4b,0x09,0xff
  8259. };
  8260. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  8261. {
  8262. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8263. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  8264. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  8265. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  8266. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  8267. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  8268. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  8269. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  8270. };
  8271. #endif /* WOLFSSL_AES_192 */
  8272. #ifdef WOLFSSL_AES_256
  8273. /* 256 size key simple test */
  8274. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  8275. {
  8276. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8277. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8278. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8279. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8280. };
  8281. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  8282. {
  8283. 0xdc,0x7e,0x84,0xbf,0xda,0x79,0x16,0x4b,
  8284. 0x7e,0xcd,0x84,0x86,0x98,0x5d,0x38,0x60,
  8285. 0x39,0xff,0xed,0x14,0x3b,0x28,0xb1,0xc8,
  8286. 0x32,0x11,0x3c,0x63,0x31,0xe5,0x40,0x7b,
  8287. 0xdf,0x10,0x13,0x24,0x15,0xe5,0x4b,0x92,
  8288. 0xa1,0x3e,0xd0,0xa8,0x26,0x7a,0xe2,0xf9,
  8289. 0x75,0xa3,0x85,0x74,0x1a,0xb9,0xce,0xf8,
  8290. 0x20,0x31,0x62,0x3d,0x55,0xb1,0xe4,0x71
  8291. };
  8292. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  8293. {
  8294. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8295. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  8296. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  8297. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  8298. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  8299. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  8300. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  8301. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  8302. };
  8303. #endif /* WOLFSSL_AES_256 */
  8304. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8305. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8306. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8307. #ifdef HAVE_AES_DECRYPT
  8308. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8309. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8310. #endif
  8311. #endif
  8312. ret = wc_AesInit(enc, HEAP_HINT, devId);
  8313. if (ret != 0)
  8314. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8315. else
  8316. enc_inited = 1;
  8317. #ifdef HAVE_AES_DECRYPT
  8318. ret = wc_AesInit(dec, HEAP_HINT, devId);
  8319. if (ret != 0)
  8320. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8321. else
  8322. dec_inited = 1;
  8323. #endif
  8324. #ifdef WOLFSSL_AES_128
  8325. /* 128 key tests */
  8326. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8327. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8328. ret = EVP_test(EVP_aes_128_cfb128(), key1, iv, msg1, sizeof(msg1),
  8329. cipher1, sizeof(cipher1));
  8330. if (ret != 0) {
  8331. return ret;
  8332. }
  8333. #endif
  8334. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8335. if (ret != 0)
  8336. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8337. #ifdef HAVE_AES_DECRYPT
  8338. /* decrypt uses AES_ENCRYPTION */
  8339. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8340. if (ret != 0)
  8341. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8342. #endif
  8343. XMEMSET(cipher, 0, sizeof(cipher));
  8344. ret = wc_AesCfbEncrypt(enc, cipher, msg1, AES_BLOCK_SIZE * 2);
  8345. if (ret != 0)
  8346. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8347. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  8348. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8349. /* test restarting encryption process */
  8350. ret = wc_AesCfbEncrypt(enc, cipher + (AES_BLOCK_SIZE * 2),
  8351. msg1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE);
  8352. if (ret != 0)
  8353. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8354. if (XMEMCMP(cipher + (AES_BLOCK_SIZE * 2),
  8355. cipher1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE))
  8356. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8357. #ifdef HAVE_AES_DECRYPT
  8358. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 3);
  8359. if (ret != 0)
  8360. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8361. if (XMEMCMP(plain, msg1, AES_BLOCK_SIZE * 3))
  8362. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8363. #endif /* HAVE_AES_DECRYPT */
  8364. #endif /* WOLFSSL_AES_128 */
  8365. #ifdef WOLFSSL_AES_192
  8366. /* 192 key size test */
  8367. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8368. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8369. ret = EVP_test(EVP_aes_192_cfb128(), key2, iv, msg2, sizeof(msg2),
  8370. cipher2, sizeof(cipher2));
  8371. if (ret != 0) {
  8372. return ret;
  8373. }
  8374. #endif
  8375. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv, AES_ENCRYPTION);
  8376. if (ret != 0)
  8377. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8378. #ifdef HAVE_AES_DECRYPT
  8379. /* decrypt uses AES_ENCRYPTION */
  8380. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv, AES_ENCRYPTION);
  8381. if (ret != 0)
  8382. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8383. #endif
  8384. XMEMSET(cipher, 0, sizeof(cipher));
  8385. ret = wc_AesCfbEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE * 4);
  8386. if (ret != 0)
  8387. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8388. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE * 4))
  8389. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8390. #ifdef HAVE_AES_DECRYPT
  8391. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 4);
  8392. if (ret != 0)
  8393. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8394. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE * 4))
  8395. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8396. #endif /* HAVE_AES_DECRYPT */
  8397. #endif /* WOLFSSL_AES_192 */
  8398. #ifdef WOLFSSL_AES_256
  8399. /* 256 key size test */
  8400. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8401. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8402. ret = EVP_test(EVP_aes_256_cfb128(), key3, iv, msg3, sizeof(msg3),
  8403. cipher3, sizeof(cipher3));
  8404. if (ret != 0) {
  8405. return ret;
  8406. }
  8407. #endif
  8408. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv, AES_ENCRYPTION);
  8409. if (ret != 0)
  8410. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8411. #ifdef HAVE_AES_DECRYPT
  8412. /* decrypt uses AES_ENCRYPTION */
  8413. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv, AES_ENCRYPTION);
  8414. if (ret != 0)
  8415. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8416. #endif
  8417. /* test with data left overs, magic lengths are checking near edges */
  8418. XMEMSET(cipher, 0, sizeof(cipher));
  8419. ret = wc_AesCfbEncrypt(enc, cipher, msg3, 4);
  8420. if (ret != 0)
  8421. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8422. if (XMEMCMP(cipher, cipher3, 4))
  8423. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8424. ret = wc_AesCfbEncrypt(enc, cipher + 4, msg3 + 4, 27);
  8425. if (ret != 0)
  8426. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8427. if (XMEMCMP(cipher + 4, cipher3 + 4, 27))
  8428. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8429. ret = wc_AesCfbEncrypt(enc, cipher + 31, msg3 + 31,
  8430. (AES_BLOCK_SIZE * 4) - 31);
  8431. if (ret != 0)
  8432. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8433. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE * 4))
  8434. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8435. #ifdef HAVE_AES_DECRYPT
  8436. ret = wc_AesCfbDecrypt(dec, plain, cipher, 4);
  8437. if (ret != 0)
  8438. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8439. if (XMEMCMP(plain, msg3, 4))
  8440. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8441. ret = wc_AesCfbDecrypt(dec, plain + 4, cipher + 4, 4);
  8442. if (ret != 0)
  8443. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8444. ret = wc_AesCfbDecrypt(dec, plain + 8, cipher + 8, 23);
  8445. if (ret != 0)
  8446. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8447. if (XMEMCMP(plain + 4, msg3 + 4, 27))
  8448. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8449. ret = wc_AesCfbDecrypt(dec, plain + 31, cipher + 31,
  8450. (AES_BLOCK_SIZE * 4) - 31);
  8451. if (ret != 0)
  8452. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8453. if (XMEMCMP(plain, msg3, AES_BLOCK_SIZE * 4))
  8454. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8455. #endif /* HAVE_AES_DECRYPT */
  8456. #endif /* WOLFSSL_AES_256 */
  8457. out:
  8458. if (enc_inited)
  8459. wc_AesFree(enc);
  8460. #ifdef HAVE_AES_DECRYPT
  8461. if (dec_inited)
  8462. wc_AesFree(dec);
  8463. #endif
  8464. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8465. if (enc)
  8466. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8467. #ifdef HAVE_AES_DECRYPT
  8468. if (dec)
  8469. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8470. #endif
  8471. #endif
  8472. return ret;
  8473. }
  8474. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0))
  8475. static wc_test_ret_t aescfb1_test(void)
  8476. {
  8477. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8478. Aes *enc = NULL;
  8479. #else
  8480. Aes enc[1];
  8481. #endif
  8482. int enc_inited = 0;
  8483. byte cipher[AES_BLOCK_SIZE];
  8484. #ifdef HAVE_AES_DECRYPT
  8485. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8486. Aes *dec = NULL;
  8487. #else
  8488. Aes dec[1];
  8489. #endif
  8490. int dec_inited = 0;
  8491. byte plain [AES_BLOCK_SIZE];
  8492. #endif
  8493. wc_test_ret_t ret = 0;
  8494. #ifdef WOLFSSL_AES_128
  8495. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  8496. 0x4d,0xbb,0xdc,0xaa,0x59,0xf3,0x63,0xc9,
  8497. 0x2a,0x3b,0x98,0x43,0xad,0x20,0xe2,0xb7
  8498. };
  8499. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  8500. {
  8501. 0xcd,0xef,0x9d,0x06,0x61,0xba,0xe4,0x73,
  8502. 0x8d,0x1a,0x58,0xa2,0xa6,0x22,0x8b,0x66
  8503. };
  8504. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  8505. {
  8506. 0x00
  8507. };
  8508. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  8509. {
  8510. 0xC0
  8511. };
  8512. WOLFSSL_SMALL_STACK_STATIC const byte cipher1_7bit[] =
  8513. {
  8514. 0x1C
  8515. };
  8516. #endif /* WOLFSSL_AES_128 */
  8517. #ifdef WOLFSSL_AES_192
  8518. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  8519. 0x57,0xc6,0x89,0x7c,0x99,0x52,0x28,0x13,
  8520. 0xbf,0x67,0x9c,0xe1,0x13,0x70,0xaf,0x5e
  8521. };
  8522. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  8523. {
  8524. 0xba,0xa1,0x58,0xa1,0x6b,0x50,0x4a,0x10,
  8525. 0x8e,0xd4,0x33,0x2e,0xe7,0xf2,0x9b,0xf6,
  8526. 0xd1,0xac,0x46,0xa8,0xde,0x5a,0xfe,0x7a
  8527. };
  8528. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  8529. {
  8530. 0x30
  8531. };
  8532. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  8533. {
  8534. 0x80
  8535. };
  8536. #endif /* WOLFSSL_AES_192 */
  8537. #ifdef WOLFSSL_AES_256
  8538. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  8539. 0x63,0x2e,0x9f,0x83,0x1f,0xa3,0x80,0x5e,
  8540. 0x52,0x02,0xbc,0xe0,0x6d,0x04,0xf9,0xa0
  8541. };
  8542. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  8543. {
  8544. 0xf6,0xfa,0xe4,0xf1,0x5d,0x91,0xfc,0x50,
  8545. 0x88,0x78,0x4f,0x84,0xa5,0x37,0x12,0x7e,
  8546. 0x32,0x63,0x55,0x9c,0x62,0x73,0x88,0x20,
  8547. 0xc2,0xcf,0x3d,0xe1,0x1c,0x2a,0x30,0x40
  8548. };
  8549. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  8550. {
  8551. 0xF7, 0x00
  8552. };
  8553. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  8554. {
  8555. 0x41, 0xC0
  8556. };
  8557. #endif /* WOLFSSL_AES_256 */
  8558. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8559. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8560. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8561. #ifdef HAVE_AES_DECRYPT
  8562. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8563. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8564. #endif
  8565. #endif
  8566. ret = wc_AesInit(enc, HEAP_HINT, devId);
  8567. if (ret != 0)
  8568. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8569. else
  8570. enc_inited = 1;
  8571. #ifdef HAVE_AES_DECRYPT
  8572. ret = wc_AesInit(dec, HEAP_HINT, devId);
  8573. if (ret != 0)
  8574. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8575. else
  8576. dec_inited = 1;
  8577. #endif
  8578. #ifdef WOLFSSL_AES_128
  8579. /* 128 key tests */
  8580. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8581. if (ret != 0)
  8582. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8583. #ifdef HAVE_AES_DECRYPT
  8584. /* decrypt uses AES_ENCRYPTION */
  8585. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8586. if (ret != 0)
  8587. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8588. #endif
  8589. XMEMSET(cipher, 0, sizeof(cipher));
  8590. ret = wc_AesCfb1Encrypt(enc, cipher, msg1, 2);
  8591. if (ret != 0)
  8592. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8593. if (cipher[0] != cipher1[0])
  8594. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8595. #ifdef HAVE_AES_DECRYPT
  8596. ret = wc_AesCfb1Decrypt(dec, plain, cipher, 2);
  8597. if (ret != 0)
  8598. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8599. if (plain[0] != msg1[0])
  8600. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8601. #endif /* HAVE_AES_DECRYPT */
  8602. XMEMSET(cipher, 0, sizeof(cipher));
  8603. ret = wc_AesCfb1Encrypt(enc, cipher, msg1, 7);
  8604. if (ret != 0)
  8605. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8606. if (cipher[0] != cipher1_7bit[0])
  8607. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8608. #ifdef OPENSSL_EXTRA
  8609. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8610. if (ret != 0)
  8611. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8612. XMEMSET(cipher, 0, sizeof(cipher));
  8613. ret = wc_AesCfb1Encrypt(enc, cipher, msg1,
  8614. sizeof(msg1) * WOLFSSL_BIT_SIZE);
  8615. if (ret != 0)
  8616. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8617. #ifndef WOLFCRYPT_ONLY
  8618. ret = EVP_test(EVP_aes_128_cfb1(), key1, iv, msg1, sizeof(msg1),
  8619. cipher, sizeof(msg1));
  8620. if (ret != 0) {
  8621. goto out;
  8622. }
  8623. #endif
  8624. #endif
  8625. #endif /* WOLFSSL_AES_128 */
  8626. #ifdef WOLFSSL_AES_192
  8627. /* 192 key tests */
  8628. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8629. if (ret != 0)
  8630. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8631. XMEMSET(cipher, 0, sizeof(cipher));
  8632. ret = wc_AesCfb1Encrypt(enc, cipher, msg2, 4);
  8633. if (ret != 0)
  8634. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8635. if (XMEMCMP(cipher, cipher2, sizeof(cipher2)) != 0)
  8636. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8637. #ifdef OPENSSL_EXTRA
  8638. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8639. if (ret != 0)
  8640. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8641. XMEMSET(cipher, 0, sizeof(cipher));
  8642. ret = wc_AesCfb1Encrypt(enc, cipher, msg2,
  8643. sizeof(msg2) * WOLFSSL_BIT_SIZE);
  8644. if (ret != 0)
  8645. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8646. #ifndef WOLFCRYPT_ONLY
  8647. ret = EVP_test(EVP_aes_192_cfb1(), key2, iv2, msg2, sizeof(msg2),
  8648. cipher, sizeof(msg2));
  8649. if (ret != 0) {
  8650. goto out;
  8651. }
  8652. #endif
  8653. #endif
  8654. #endif /* WOLFSSL_AES_192 */
  8655. #ifdef WOLFSSL_AES_256
  8656. /* 256 key tests */
  8657. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  8658. if (ret != 0)
  8659. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8660. XMEMSET(cipher, 0, sizeof(cipher));
  8661. ret = wc_AesCfb1Encrypt(enc, cipher, msg3, 10);
  8662. if (ret != 0)
  8663. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8664. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  8665. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8666. #ifdef OPENSSL_EXTRA
  8667. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  8668. if (ret != 0)
  8669. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8670. XMEMSET(cipher, 0, sizeof(cipher));
  8671. ret = wc_AesCfb1Encrypt(enc, cipher, msg3,
  8672. sizeof(msg3) * WOLFSSL_BIT_SIZE);
  8673. if (ret != 0)
  8674. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8675. #ifndef WOLFCRYPT_ONLY
  8676. ret = EVP_test(EVP_aes_256_cfb1(), key3, iv3, msg3, sizeof(msg3),
  8677. cipher, sizeof(msg3));
  8678. if (ret != 0) {
  8679. goto out;
  8680. }
  8681. #endif
  8682. #endif
  8683. #endif /* WOLFSSL_AES_256 */
  8684. out:
  8685. if (enc_inited)
  8686. wc_AesFree(enc);
  8687. #ifdef HAVE_AES_DECRYPT
  8688. if (dec_inited)
  8689. wc_AesFree(dec);
  8690. #endif
  8691. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8692. if (enc)
  8693. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8694. #ifdef HAVE_AES_DECRYPT
  8695. if (dec)
  8696. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8697. #endif
  8698. #endif
  8699. return ret;
  8700. }
  8701. static wc_test_ret_t aescfb8_test(void)
  8702. {
  8703. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8704. Aes *enc = NULL;
  8705. #else
  8706. Aes enc[1];
  8707. #endif
  8708. int enc_inited = 0;
  8709. byte cipher[AES_BLOCK_SIZE];
  8710. #ifdef HAVE_AES_DECRYPT
  8711. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8712. Aes *dec = NULL;
  8713. #else
  8714. Aes dec[1];
  8715. #endif
  8716. int dec_inited = 0;
  8717. byte plain [AES_BLOCK_SIZE];
  8718. #endif
  8719. wc_test_ret_t ret = 0;
  8720. #ifdef WOLFSSL_AES_128
  8721. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  8722. 0xf4,0x75,0xc6,0x49,0x91,0xb2,0x0e,0xae,
  8723. 0xe1,0x83,0xa2,0x26,0x29,0xe2,0x1e,0x22
  8724. };
  8725. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  8726. {
  8727. 0xc8,0xfe,0x9b,0xf7,0x7b,0x93,0x0f,0x46,
  8728. 0xd2,0x07,0x8b,0x8c,0x0e,0x65,0x7c,0xd4
  8729. };
  8730. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  8731. {
  8732. 0xd2,0x76,0x91
  8733. };
  8734. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  8735. {
  8736. 0xc9,0x06,0x35
  8737. };
  8738. #endif /* WOLFSSL_AES_128 */
  8739. #ifdef WOLFSSL_AES_192
  8740. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  8741. 0x0a,0x02,0x84,0x6b,0x62,0xab,0xb6,0x93,
  8742. 0xef,0x31,0xd7,0x54,0x84,0x2e,0xed,0x29
  8743. };
  8744. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  8745. {
  8746. 0xba,0xf0,0x8b,0x76,0x31,0x7a,0x65,0xc5,
  8747. 0xf0,0x7a,0xe6,0xf5,0x7e,0xb0,0xe6,0x54,
  8748. 0x88,0x65,0x93,0x24,0xd2,0x97,0x09,0xe3
  8749. };
  8750. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  8751. {
  8752. 0x72,0x9c,0x0b,0x6d,0xeb,0x75,0xfa,0x6e,
  8753. 0xb5,0xe8
  8754. };
  8755. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  8756. {
  8757. 0x98,0x95,0x93,0x24,0x02,0x39,0x3d,0xc3,
  8758. 0x3a,0x60
  8759. };
  8760. #endif
  8761. #ifdef WOLFSSL_AES_256
  8762. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  8763. 0x33,0x8c,0x55,0x2f,0xf1,0xec,0xa1,0x44,
  8764. 0x08,0xe0,0x5d,0x8c,0xf9,0xf3,0xb3,0x1b
  8765. };
  8766. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  8767. {
  8768. 0x06,0x48,0x74,0x09,0x2f,0x7a,0x13,0xcc,
  8769. 0x44,0x62,0x24,0x7a,0xd4,0x23,0xd0,0xe9,
  8770. 0x6e,0xdf,0x42,0xe8,0xb6,0x7a,0x5a,0x23,
  8771. 0xb7,0xa0,0xa6,0x47,0x7b,0x09,0x8e,0x66
  8772. };
  8773. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  8774. {
  8775. 0x1c,0xff,0x95
  8776. };
  8777. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  8778. {
  8779. 0xb9,0x74,0xfa
  8780. };
  8781. #endif
  8782. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8783. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8784. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8785. #ifdef HAVE_AES_DECRYPT
  8786. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8787. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8788. #endif
  8789. #endif
  8790. ret = wc_AesInit(enc, HEAP_HINT, devId);
  8791. if (ret != 0)
  8792. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8793. else
  8794. enc_inited = 1;
  8795. #ifdef HAVE_AES_DECRYPT
  8796. ret = wc_AesInit(dec, HEAP_HINT, devId);
  8797. if (ret != 0)
  8798. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8799. else
  8800. dec_inited = 1;
  8801. #endif
  8802. #ifdef WOLFSSL_AES_128
  8803. /* 128 key tests */
  8804. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  8805. ret = EVP_test(EVP_aes_128_cfb8(), key1, iv, msg1, sizeof(msg1),
  8806. cipher1, sizeof(cipher1));
  8807. if (ret != 0) {
  8808. return ret;
  8809. }
  8810. #endif
  8811. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8812. if (ret != 0)
  8813. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8814. #ifdef HAVE_AES_DECRYPT
  8815. /* decrypt uses AES_ENCRYPTION */
  8816. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8817. if (ret != 0)
  8818. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8819. #endif
  8820. XMEMSET(cipher, 0, sizeof(cipher));
  8821. ret = wc_AesCfb8Encrypt(enc, cipher, msg1, sizeof(msg1));
  8822. if (ret != 0)
  8823. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8824. if (XMEMCMP(cipher, cipher1, sizeof(cipher1)) != 0)
  8825. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8826. #ifdef HAVE_AES_DECRYPT
  8827. ret = wc_AesCfb8Decrypt(dec, plain, cipher, sizeof(msg1));
  8828. if (ret != 0)
  8829. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8830. if (XMEMCMP(plain, msg1, sizeof(msg1)) != 0)
  8831. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8832. #endif /* HAVE_AES_DECRYPT */
  8833. #endif /* WOLFSSL_AES_128 */
  8834. #ifdef WOLFSSL_AES_192
  8835. /* 192 key tests */
  8836. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8837. if (ret != 0)
  8838. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8839. XMEMSET(cipher, 0, sizeof(cipher));
  8840. ret = wc_AesCfb8Encrypt(enc, cipher, msg2, sizeof(msg2));
  8841. if (ret != 0)
  8842. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8843. if (XMEMCMP(cipher, cipher2, sizeof(msg2)) != 0)
  8844. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8845. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  8846. ret = EVP_test(EVP_aes_192_cfb8(), key2, iv2, msg2, sizeof(msg2),
  8847. cipher2, sizeof(msg2));
  8848. if (ret != 0) {
  8849. return ret;
  8850. }
  8851. #endif
  8852. #endif /* WOLFSSL_AES_192 */
  8853. #ifdef WOLFSSL_AES_256
  8854. /* 256 key tests */
  8855. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  8856. if (ret != 0)
  8857. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8858. XMEMSET(cipher, 0, sizeof(cipher));
  8859. ret = wc_AesCfb8Encrypt(enc, cipher, msg3, sizeof(msg3));
  8860. if (ret != 0)
  8861. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8862. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  8863. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8864. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  8865. ret = EVP_test(EVP_aes_256_cfb8(), key3, iv3, msg3, sizeof(msg3),
  8866. cipher3, sizeof(msg3));
  8867. if (ret != 0) {
  8868. goto out;
  8869. }
  8870. #endif
  8871. #endif /* WOLFSSL_AES_256 */
  8872. out:
  8873. if (enc_inited)
  8874. wc_AesFree(enc);
  8875. #ifdef HAVE_AES_DECRYPT
  8876. if (dec_inited)
  8877. wc_AesFree(dec);
  8878. #endif
  8879. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8880. if (enc)
  8881. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8882. #ifdef HAVE_AES_DECRYPT
  8883. if (dec)
  8884. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8885. #endif
  8886. #endif
  8887. return ret;
  8888. }
  8889. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  8890. #endif /* WOLFSSL_AES_CFB */
  8891. static wc_test_ret_t aes_key_size_test(void)
  8892. {
  8893. wc_test_ret_t ret;
  8894. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8895. Aes *aes;
  8896. #else
  8897. Aes aes[1];
  8898. #endif
  8899. byte key16[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8900. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  8901. #ifndef WOLFSSL_CRYPTOCELL
  8902. byte key24[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8903. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  8904. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
  8905. #endif
  8906. byte key32[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8907. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  8908. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8909. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  8910. byte iv[] = "1234567890abcdef";
  8911. #ifndef HAVE_FIPS
  8912. word32 keySize;
  8913. #endif
  8914. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8915. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8916. return WC_TEST_RET_ENC_ERRNO;
  8917. #endif
  8918. #if !defined(HAVE_FIPS) || \
  8919. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  8920. /* w/ FIPS v1 (cert 2425) wc_AesInit just returns 0 always as it's not
  8921. * supported with that FIPS version */
  8922. ret = wc_AesInit(NULL, HEAP_HINT, devId);
  8923. if (ret != BAD_FUNC_ARG)
  8924. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8925. #endif
  8926. ret = wc_AesInit(aes, HEAP_HINT, devId);
  8927. /* 0 check OK for FIPSv1 */
  8928. if (ret != 0)
  8929. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8930. #ifndef HAVE_FIPS
  8931. /* Parameter Validation testing. */
  8932. ret = wc_AesGetKeySize(NULL, NULL);
  8933. if (ret != BAD_FUNC_ARG)
  8934. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8935. ret = wc_AesGetKeySize(aes, NULL);
  8936. if (ret != BAD_FUNC_ARG)
  8937. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8938. ret = wc_AesGetKeySize(NULL, &keySize);
  8939. if (ret != BAD_FUNC_ARG)
  8940. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8941. /* Crashes in FIPS */
  8942. ret = wc_AesSetKey(NULL, key16, sizeof(key16), iv, AES_ENCRYPTION);
  8943. if (ret != BAD_FUNC_ARG)
  8944. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8945. #endif
  8946. /* NULL IV indicates to use all zeros IV. */
  8947. ret = wc_AesSetKey(aes, key16, sizeof(key16), NULL, AES_ENCRYPTION);
  8948. #ifdef WOLFSSL_AES_128
  8949. if (ret != 0)
  8950. #else
  8951. if (ret != BAD_FUNC_ARG)
  8952. #endif
  8953. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8954. ret = wc_AesSetKey(aes, key32, sizeof(key32) - 1, iv, AES_ENCRYPTION);
  8955. if (ret != BAD_FUNC_ARG)
  8956. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8957. /* CryptoCell handles rounds internally */
  8958. #if !defined(HAVE_FIPS) && !defined(WOLFSSL_CRYPTOCELL)
  8959. /* PSA don't use aes->rounds */
  8960. #if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_AES)
  8961. /* Force invalid rounds */
  8962. aes->rounds = 16;
  8963. ret = wc_AesGetKeySize(aes, &keySize);
  8964. if (ret != BAD_FUNC_ARG)
  8965. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8966. #endif
  8967. #endif
  8968. ret = wc_AesSetKey(aes, key16, sizeof(key16), iv, AES_ENCRYPTION);
  8969. #ifdef WOLFSSL_AES_128
  8970. if (ret != 0)
  8971. #else
  8972. if (ret != BAD_FUNC_ARG)
  8973. #endif
  8974. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8975. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_128)
  8976. ret = wc_AesGetKeySize(aes, &keySize);
  8977. if (ret != 0 || keySize != sizeof(key16))
  8978. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8979. #endif
  8980. #ifndef WOLFSSL_CRYPTOCELL
  8981. /* Cryptocell only supports AES-128 key size */
  8982. ret = wc_AesSetKey(aes, key24, sizeof(key24), iv, AES_ENCRYPTION);
  8983. #ifdef WOLFSSL_AES_192
  8984. if (ret != 0)
  8985. #else
  8986. if (ret != BAD_FUNC_ARG)
  8987. #endif
  8988. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8989. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_192)
  8990. ret = wc_AesGetKeySize(aes, &keySize);
  8991. if (ret != 0 || keySize != sizeof(key24))
  8992. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8993. #endif
  8994. ret = wc_AesSetKey(aes, key32, sizeof(key32), iv, AES_ENCRYPTION);
  8995. #ifdef WOLFSSL_AES_256
  8996. if (ret != 0)
  8997. #else
  8998. if (ret != BAD_FUNC_ARG)
  8999. #endif
  9000. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9001. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_256)
  9002. ret = wc_AesGetKeySize(aes, &keySize);
  9003. if (ret != 0 || keySize != sizeof(key32))
  9004. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9005. #endif
  9006. #endif /* !WOLFSSL_CRYPTOCELL */
  9007. ret = 0; /* success */
  9008. out:
  9009. wc_AesFree(aes);
  9010. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9011. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  9012. #endif
  9013. return ret;
  9014. }
  9015. #if defined(WOLFSSL_AES_XTS) && (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3))
  9016. /* test vectors from http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html */
  9017. #ifdef WOLFSSL_AES_128
  9018. static wc_test_ret_t aes_xts_128_test(void)
  9019. {
  9020. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9021. XtsAes *aes = NULL;
  9022. #else
  9023. XtsAes aes[1];
  9024. #endif
  9025. int aes_inited = 0;
  9026. wc_test_ret_t ret = 0;
  9027. unsigned char buf[AES_BLOCK_SIZE * 2 + 8];
  9028. unsigned char cipher[AES_BLOCK_SIZE * 2 + 8];
  9029. #ifdef WOLFSSL_AESXTS_STREAM
  9030. struct XtsAesStreamData stream;
  9031. #endif
  9032. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  9033. !defined(WOLFSSL_AFALG)
  9034. #define LARGE_XTS_SZ 1024
  9035. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9036. byte* large_input = NULL;
  9037. #else
  9038. byte large_input[LARGE_XTS_SZ];
  9039. #endif
  9040. #endif
  9041. /* 128 key tests */
  9042. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  9043. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  9044. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  9045. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  9046. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  9047. };
  9048. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  9049. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  9050. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  9051. };
  9052. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  9053. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  9054. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  9055. };
  9056. /* plain text test of partial block is not from NIST test vector list */
  9057. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  9058. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  9059. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  9060. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  9061. };
  9062. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  9063. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a,
  9064. 0x82, 0x50, 0x81, 0xd5, 0xbe, 0x47, 0x1c, 0x63
  9065. };
  9066. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  9067. 0x39, 0x25, 0x79, 0x05, 0xdf, 0xcc, 0x77, 0x76,
  9068. 0x6c, 0x87, 0x0a, 0x80, 0x6a, 0x60, 0xe3, 0xc0,
  9069. 0x93, 0xd1, 0x2a, 0xcf, 0xcb, 0x51, 0x42, 0xfa,
  9070. 0x09, 0x69, 0x89, 0x62, 0x5b, 0x60, 0xdb, 0x16
  9071. };
  9072. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  9073. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  9074. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  9075. };
  9076. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  9077. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  9078. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  9079. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  9080. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  9081. };
  9082. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  9083. 0xd6, 0xbe, 0x04, 0x6d, 0x41, 0xf2, 0x3b, 0x5e,
  9084. 0xd7, 0x0b, 0x6b, 0x3d, 0x5c, 0x8e, 0x66, 0x23,
  9085. 0x2b, 0xe6, 0xb8, 0x07, 0xd4, 0xdc, 0xc6, 0x0e,
  9086. 0xff, 0x8d, 0xbc, 0x1d, 0x9f, 0x7f, 0xc8, 0x22
  9087. };
  9088. WOLFSSL_SMALL_STACK_STATIC unsigned char cp2[] = {
  9089. 0x2b, 0xf7, 0x2c, 0xf3, 0xeb, 0x85, 0xef, 0x7b,
  9090. 0x0b, 0x76, 0xa0, 0xaa, 0xf3, 0x3f, 0x25, 0x8b,
  9091. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a
  9092. };
  9093. #ifndef HAVE_FIPS /* FIPS requires different keys for main and tweak. */
  9094. WOLFSSL_SMALL_STACK_STATIC unsigned char k3[] = {
  9095. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9096. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9097. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9098. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9099. };
  9100. WOLFSSL_SMALL_STACK_STATIC unsigned char i3[] = {
  9101. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9102. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9103. };
  9104. WOLFSSL_SMALL_STACK_STATIC unsigned char p3[] = {
  9105. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9106. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9107. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9108. 0x20, 0xff, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9109. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
  9110. };
  9111. WOLFSSL_SMALL_STACK_STATIC unsigned char c3[] = {
  9112. 0xA2, 0x07, 0x47, 0x76, 0x3F, 0xEC, 0x0C, 0x23,
  9113. 0x1B, 0xD0, 0xBD, 0x46, 0x9A, 0x27, 0x38, 0x12,
  9114. 0x95, 0x02, 0x3D, 0x5D, 0xC6, 0x94, 0x51, 0x36,
  9115. 0xA0, 0x85, 0xD2, 0x69, 0x6E, 0x87, 0x0A, 0xBF,
  9116. 0xB5, 0x5A, 0xDD, 0xCB, 0x80, 0xE0, 0xFC, 0xCD
  9117. };
  9118. #endif /* HAVE_FIPS */
  9119. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9120. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9121. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9122. #endif
  9123. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  9124. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  9125. ret = EVP_test(EVP_aes_128_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  9126. if (ret != 0) {
  9127. printf("EVP_aes_128_xts failed!\n");
  9128. goto out;
  9129. }
  9130. #endif
  9131. XMEMSET(buf, 0, sizeof(buf));
  9132. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  9133. if (ret != 0)
  9134. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9135. else
  9136. aes_inited = 1;
  9137. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_ENCRYPTION);
  9138. if (ret != 0)
  9139. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9140. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  9141. #if defined(WOLFSSL_ASYNC_CRYPT)
  9142. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9143. #endif
  9144. if (ret != 0)
  9145. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9146. if (XMEMCMP(c2, buf, sizeof(c2)))
  9147. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9148. #ifdef WOLFSSL_AESXTS_STREAM
  9149. ret = wc_AesXtsEncryptInit(aes, i2, sizeof(i2), &stream);
  9150. #if defined(WOLFSSL_ASYNC_CRYPT)
  9151. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9152. #endif
  9153. if (ret != 0)
  9154. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9155. ret = wc_AesXtsEncryptUpdate(aes, buf, p2, AES_BLOCK_SIZE, &stream);
  9156. #if defined(WOLFSSL_ASYNC_CRYPT)
  9157. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9158. #endif
  9159. if (ret != 0)
  9160. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9161. ret = wc_AesXtsEncryptFinal(aes, buf + AES_BLOCK_SIZE, p2 + AES_BLOCK_SIZE, sizeof(p2) - AES_BLOCK_SIZE, &stream);
  9162. #if defined(WOLFSSL_ASYNC_CRYPT)
  9163. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9164. #endif
  9165. if (ret != 0)
  9166. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9167. if (XMEMCMP(c2, buf, sizeof(c2)))
  9168. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9169. #endif /* WOLFSSL_AESXTS_STREAM */
  9170. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9171. defined(WC_C_DYNAMIC_FALLBACK)
  9172. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9173. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  9174. #if defined(WOLFSSL_ASYNC_CRYPT)
  9175. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9176. #endif
  9177. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9178. if (ret != 0)
  9179. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9180. if (XMEMCMP(c2, buf, sizeof(c2)))
  9181. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9182. #endif
  9183. XMEMSET(buf, 0, sizeof(buf));
  9184. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  9185. if (ret != 0)
  9186. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9187. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  9188. #if defined(WOLFSSL_ASYNC_CRYPT)
  9189. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9190. #endif
  9191. if (ret != 0)
  9192. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9193. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  9194. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9195. #ifdef WOLFSSL_AESXTS_STREAM
  9196. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i2), &stream);
  9197. #if defined(WOLFSSL_ASYNC_CRYPT)
  9198. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9199. #endif
  9200. if (ret != 0)
  9201. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9202. ret = wc_AesXtsEncryptUpdate(aes, buf, p1, sizeof(p1), &stream);
  9203. #if defined(WOLFSSL_ASYNC_CRYPT)
  9204. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9205. #endif
  9206. if (ret != 0)
  9207. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9208. ret = wc_AesXtsEncryptFinal(aes, buf, NULL, 0, &stream);
  9209. #if defined(WOLFSSL_ASYNC_CRYPT)
  9210. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9211. #endif
  9212. if (ret != 0)
  9213. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9214. if (XMEMCMP(c1, buf, sizeof(c1)))
  9215. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9216. #endif /* WOLFSSL_AESXTS_STREAM */
  9217. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9218. defined(WC_C_DYNAMIC_FALLBACK)
  9219. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9220. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  9221. #if defined(WOLFSSL_ASYNC_CRYPT)
  9222. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9223. #endif
  9224. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9225. if (ret != 0)
  9226. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9227. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  9228. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9229. #endif
  9230. /* partial block encryption test */
  9231. XMEMSET(cipher, 0, sizeof(cipher));
  9232. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  9233. #if defined(WOLFSSL_ASYNC_CRYPT)
  9234. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9235. #endif
  9236. if (ret != 0)
  9237. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9238. if (XMEMCMP(cp2, cipher, sizeof(cp2)))
  9239. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9240. #ifdef WOLFSSL_AESXTS_STREAM
  9241. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &stream);
  9242. #if defined(WOLFSSL_ASYNC_CRYPT)
  9243. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9244. #endif
  9245. if (ret != 0)
  9246. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9247. ret = wc_AesXtsEncryptFinal(aes, buf, pp, sizeof(pp), &stream);
  9248. #if defined(WOLFSSL_ASYNC_CRYPT)
  9249. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9250. #endif
  9251. if (ret != 0)
  9252. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9253. ret = wc_AesXtsEncryptFinal(aes, buf, NULL, 0, &stream);
  9254. #if defined(WOLFSSL_ASYNC_CRYPT)
  9255. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9256. #endif
  9257. if (ret != 0)
  9258. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9259. if (XMEMCMP(cp2, buf, sizeof(cp2)))
  9260. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9261. #endif /* WOLFSSL_AESXTS_STREAM */
  9262. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9263. defined(WC_C_DYNAMIC_FALLBACK)
  9264. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9265. XMEMSET(cipher, 0, sizeof(cipher));
  9266. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  9267. #if defined(WOLFSSL_ASYNC_CRYPT)
  9268. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9269. #endif
  9270. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9271. if (ret != 0)
  9272. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9273. if (XMEMCMP(cp2, cipher, sizeof(cp2)))
  9274. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9275. #endif
  9276. /* partial block decrypt test */
  9277. XMEMSET(buf, 0, sizeof(buf));
  9278. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  9279. if (ret != 0)
  9280. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9281. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  9282. #if defined(WOLFSSL_ASYNC_CRYPT)
  9283. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9284. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9285. #else
  9286. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9287. #endif
  9288. #endif
  9289. if (ret != 0)
  9290. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9291. if (XMEMCMP(pp, buf, sizeof(pp)))
  9292. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9293. #ifdef WOLFSSL_AESXTS_STREAM
  9294. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  9295. #if defined(WOLFSSL_ASYNC_CRYPT)
  9296. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9297. #endif
  9298. if (ret != 0)
  9299. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9300. ret = wc_AesXtsDecryptFinal(aes, buf, cipher, sizeof(pp), &stream);
  9301. #if defined(WOLFSSL_ASYNC_CRYPT)
  9302. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9303. #endif
  9304. if (ret != 0)
  9305. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9306. if (XMEMCMP(pp, buf, sizeof(pp)))
  9307. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9308. #endif /* WOLFSSL_AESXTS_STREAM */
  9309. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9310. defined(WC_C_DYNAMIC_FALLBACK)
  9311. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9312. XMEMSET(buf, 0, sizeof(buf));
  9313. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  9314. #if defined(WOLFSSL_ASYNC_CRYPT)
  9315. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9316. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9317. #else
  9318. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9319. #endif
  9320. #endif
  9321. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9322. if (ret != 0)
  9323. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9324. if (XMEMCMP(pp, buf, sizeof(pp)))
  9325. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9326. #endif
  9327. /* NIST decrypt test vector */
  9328. XMEMSET(buf, 0, sizeof(buf));
  9329. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  9330. #if defined(WOLFSSL_ASYNC_CRYPT)
  9331. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9332. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9333. #else
  9334. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9335. #endif
  9336. #endif
  9337. if (ret != 0)
  9338. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9339. if (XMEMCMP(p1, buf, sizeof(p1)))
  9340. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9341. #ifdef WOLFSSL_AESXTS_STREAM
  9342. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  9343. #if defined(WOLFSSL_ASYNC_CRYPT)
  9344. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9345. #endif
  9346. if (ret != 0)
  9347. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9348. ret = wc_AesXtsDecryptFinal(aes, buf, c1, sizeof(c1), &stream);
  9349. #if defined(WOLFSSL_ASYNC_CRYPT)
  9350. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9351. #endif
  9352. if (ret != 0)
  9353. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9354. if (XMEMCMP(p1, buf, sizeof(p1)))
  9355. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9356. #endif /* WOLFSSL_AESXTS_STREAM */
  9357. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9358. defined(WC_C_DYNAMIC_FALLBACK)
  9359. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9360. XMEMSET(buf, 0, sizeof(buf));
  9361. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  9362. #if defined(WOLFSSL_ASYNC_CRYPT)
  9363. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9364. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9365. #else
  9366. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9367. #endif
  9368. #endif
  9369. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9370. if (ret != 0)
  9371. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9372. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  9373. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9374. #endif
  9375. /* fail case with decrypting using wrong key */
  9376. XMEMSET(buf, 0, sizeof(buf));
  9377. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  9378. #if defined(WOLFSSL_ASYNC_CRYPT)
  9379. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9380. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9381. #else
  9382. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9383. #endif
  9384. #endif
  9385. if (ret != 0)
  9386. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9387. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  9388. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9389. /* set correct key and retest */
  9390. XMEMSET(buf, 0, sizeof(buf));
  9391. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_DECRYPTION);
  9392. if (ret != 0)
  9393. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9394. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  9395. #if defined(WOLFSSL_ASYNC_CRYPT)
  9396. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9397. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9398. #else
  9399. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9400. #endif
  9401. #endif
  9402. if (ret != 0)
  9403. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9404. if (XMEMCMP(p2, buf, sizeof(p2)))
  9405. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9406. #ifndef HAVE_FIPS
  9407. /* Test ciphertext stealing in-place. */
  9408. XMEMCPY(buf, p3, sizeof(p3));
  9409. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_ENCRYPTION);
  9410. if (ret != 0)
  9411. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9412. ret = wc_AesXtsEncrypt(aes, buf, buf, sizeof(p3), i3, sizeof(i3));
  9413. #if defined(WOLFSSL_ASYNC_CRYPT)
  9414. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9415. #endif
  9416. if (ret != 0)
  9417. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9418. if (XMEMCMP(c3, buf, sizeof(c3)))
  9419. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9420. #ifdef WOLFSSL_AESXTS_STREAM
  9421. ret = wc_AesXtsEncryptInit(aes, i3, sizeof(i3), &stream);
  9422. #if defined(WOLFSSL_ASYNC_CRYPT)
  9423. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9424. #endif
  9425. if (ret != 0)
  9426. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9427. ret = wc_AesXtsEncryptUpdate(aes, buf, p3, AES_BLOCK_SIZE, &stream);
  9428. #if defined(WOLFSSL_ASYNC_CRYPT)
  9429. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9430. #endif
  9431. if (ret != 0)
  9432. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9433. ret = wc_AesXtsEncryptFinal(aes, buf + AES_BLOCK_SIZE, p3 + AES_BLOCK_SIZE, sizeof(p3) - AES_BLOCK_SIZE, &stream);
  9434. #if defined(WOLFSSL_ASYNC_CRYPT)
  9435. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9436. #endif
  9437. if (ret != 0)
  9438. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9439. if (XMEMCMP(c3, buf, sizeof(c3)))
  9440. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9441. #endif /* WOLFSSL_AESXTS_STREAM */
  9442. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_DECRYPTION);
  9443. if (ret != 0)
  9444. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9445. ret = wc_AesXtsDecrypt(aes, buf, buf, sizeof(c3), i3, sizeof(i3));
  9446. #if defined(WOLFSSL_ASYNC_CRYPT)
  9447. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9448. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9449. #else
  9450. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9451. #endif
  9452. #endif
  9453. if (ret != 0)
  9454. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9455. if (XMEMCMP(p3, buf, sizeof(p3)))
  9456. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9457. #ifdef WOLFSSL_AESXTS_STREAM
  9458. ret = wc_AesXtsDecryptInit(aes, i3, sizeof(i3), &stream);
  9459. #if defined(WOLFSSL_ASYNC_CRYPT)
  9460. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9461. #endif
  9462. if (ret != 0)
  9463. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9464. ret = wc_AesXtsDecryptUpdate(aes, buf, c3, AES_BLOCK_SIZE, &stream);
  9465. #if defined(WOLFSSL_ASYNC_CRYPT)
  9466. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9467. #endif
  9468. if (ret != 0)
  9469. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9470. ret = wc_AesXtsDecryptFinal(aes, buf + AES_BLOCK_SIZE, c3 + AES_BLOCK_SIZE, sizeof(c3) - AES_BLOCK_SIZE, &stream);
  9471. #if defined(WOLFSSL_ASYNC_CRYPT)
  9472. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9473. #endif
  9474. if (ret != 0)
  9475. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9476. if (XMEMCMP(p3, buf, sizeof(p3)))
  9477. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9478. #endif /* WOLFSSL_AESXTS_STREAM */
  9479. #endif /* !HAVE_FIPS */
  9480. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  9481. !defined(WOLFSSL_AFALG)
  9482. {
  9483. int i;
  9484. int j;
  9485. #ifdef WOLFSSL_AESXTS_STREAM
  9486. int k;
  9487. #endif
  9488. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9489. large_input = (byte *)XMALLOC(LARGE_XTS_SZ, HEAP_HINT,
  9490. DYNAMIC_TYPE_TMP_BUFFER);
  9491. if (large_input == NULL)
  9492. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), out);
  9493. #endif
  9494. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  9495. large_input[i] = (byte)i;
  9496. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  9497. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  9498. if (ret != 0)
  9499. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9500. ret = wc_AesXtsEncrypt(aes, large_input, large_input, (word32)j, i1,
  9501. sizeof(i1));
  9502. #if defined(WOLFSSL_ASYNC_CRYPT)
  9503. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9504. #endif
  9505. if (ret != 0)
  9506. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9507. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  9508. if (ret != 0)
  9509. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9510. ret = wc_AesXtsDecrypt(aes, large_input, large_input, (word32)j, i1,
  9511. sizeof(i1));
  9512. #if defined(WOLFSSL_ASYNC_CRYPT)
  9513. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9514. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  9515. WC_ASYNC_FLAG_NONE);
  9516. #else
  9517. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9518. #endif
  9519. #endif
  9520. if (ret != 0)
  9521. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9522. for (i = 0; i < j; i++) {
  9523. if (large_input[i] != (byte)i) {
  9524. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9525. }
  9526. }
  9527. }
  9528. #ifdef WOLFSSL_AESXTS_STREAM
  9529. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  9530. large_input[i] = (byte)i;
  9531. /* first, encrypt block by block then decrypt with a one-shot call. */
  9532. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  9533. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  9534. if (ret != 0)
  9535. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9536. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &stream);
  9537. #if defined(WOLFSSL_ASYNC_CRYPT)
  9538. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9539. #endif
  9540. if (ret != 0)
  9541. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9542. for (k = 0; k < j; k += AES_BLOCK_SIZE) {
  9543. if ((j - k) < AES_BLOCK_SIZE*2)
  9544. ret = wc_AesXtsEncryptFinal(aes, large_input + k, large_input + k, j - k, &stream);
  9545. else
  9546. ret = wc_AesXtsEncryptUpdate(aes, large_input + k, large_input + k, AES_BLOCK_SIZE, &stream);
  9547. #if defined(WOLFSSL_ASYNC_CRYPT)
  9548. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9549. #endif
  9550. if (ret != 0)
  9551. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9552. if ((j - k) < AES_BLOCK_SIZE*2)
  9553. break;
  9554. }
  9555. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  9556. if (ret != 0)
  9557. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9558. ret = wc_AesXtsDecrypt(aes, large_input, large_input, (word32)j, i1,
  9559. sizeof(i1));
  9560. #if defined(WOLFSSL_ASYNC_CRYPT)
  9561. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9562. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  9563. WC_ASYNC_FLAG_NONE);
  9564. #else
  9565. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9566. #endif
  9567. #endif
  9568. if (ret != 0)
  9569. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9570. for (i = 0; i < j; i++) {
  9571. if (large_input[i] != (byte)i) {
  9572. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9573. }
  9574. }
  9575. }
  9576. /* second, encrypt with a one-shot call then decrypt block by block. */
  9577. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  9578. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  9579. if (ret != 0)
  9580. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9581. ret = wc_AesXtsEncrypt(aes, large_input, large_input, (word32)j, i1,
  9582. sizeof(i1));
  9583. #if defined(WOLFSSL_ASYNC_CRYPT)
  9584. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9585. #endif
  9586. if (ret != 0)
  9587. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9588. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  9589. if (ret != 0)
  9590. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9591. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  9592. #if defined(WOLFSSL_ASYNC_CRYPT)
  9593. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9594. #endif
  9595. if (ret != 0)
  9596. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9597. for (k = 0; k < j; k += AES_BLOCK_SIZE) {
  9598. if ((j - k) < AES_BLOCK_SIZE*2)
  9599. ret = wc_AesXtsDecryptFinal(aes, large_input + k, large_input + k, j - k, &stream);
  9600. else
  9601. ret = wc_AesXtsDecryptUpdate(aes, large_input + k, large_input + k, AES_BLOCK_SIZE, &stream);
  9602. #if defined(WOLFSSL_ASYNC_CRYPT)
  9603. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9604. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  9605. WC_ASYNC_FLAG_NONE);
  9606. #else
  9607. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9608. #endif
  9609. #endif
  9610. if (ret != 0)
  9611. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9612. if ((j - k) < AES_BLOCK_SIZE*2)
  9613. break;
  9614. }
  9615. for (i = 0; i < j; i++) {
  9616. if (large_input[i] != (byte)i) {
  9617. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9618. }
  9619. }
  9620. }
  9621. #endif /* WOLFSSL_AESXTS_STREAM */
  9622. }
  9623. #endif /* !BENCH_EMBEDDED && !HAVE_CAVIUM &&
  9624. * !WOLFSSL_AFALG
  9625. */
  9626. out:
  9627. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  9628. !defined(WOLFSSL_AFALG) && defined(WOLFSSL_SMALL_STACK) && \
  9629. !defined(WOLFSSL_NO_MALLOC)
  9630. if (large_input)
  9631. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9632. #endif
  9633. if (aes_inited)
  9634. wc_AesXtsFree(aes);
  9635. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9636. if (aes)
  9637. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  9638. #endif
  9639. return ret;
  9640. }
  9641. #endif /* WOLFSSL_AES_128 */
  9642. #ifdef WOLFSSL_AES_192
  9643. static wc_test_ret_t aes_xts_192_test(void)
  9644. {
  9645. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9646. XtsAes *aes = NULL;
  9647. #else
  9648. XtsAes aes[1];
  9649. #endif
  9650. int aes_inited = 0;
  9651. wc_test_ret_t ret = 0;
  9652. unsigned char buf[AES_BLOCK_SIZE * 2 + 8];
  9653. unsigned char cipher[AES_BLOCK_SIZE * 2 + 8];
  9654. #ifdef WOLFSSL_AESXTS_STREAM
  9655. struct XtsAesStreamData stream;
  9656. #endif
  9657. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  9658. !defined(WOLFSSL_AFALG)
  9659. #define LARGE_XTS_SZ 1024
  9660. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9661. byte* large_input = NULL;
  9662. #else
  9663. byte large_input[LARGE_XTS_SZ];
  9664. #endif
  9665. #endif
  9666. /* 192 bit key tests */
  9667. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  9668. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  9669. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  9670. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  9671. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  9672. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  9673. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3
  9674. };
  9675. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  9676. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  9677. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  9678. };
  9679. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  9680. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  9681. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  9682. };
  9683. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  9684. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  9685. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  9686. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  9687. };
  9688. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  9689. 0x65, 0x37, 0x15, 0x53, 0xf1, 0x98, 0xab, 0xb4,
  9690. 0xdb, 0x4e, 0xd3, 0x69, 0xdf, 0x8e, 0x3a, 0xe0
  9691. };
  9692. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  9693. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  9694. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  9695. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  9696. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  9697. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  9698. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a
  9699. };
  9700. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  9701. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  9702. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  9703. };
  9704. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  9705. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  9706. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  9707. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  9708. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  9709. };
  9710. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  9711. 0x6c, 0xa6, 0xb5, 0x73, 0x48, 0xf1, 0x89, 0xfa,
  9712. 0xdd, 0x80, 0x72, 0x1f, 0xb8, 0x56, 0x0c, 0xa2,
  9713. 0x35, 0xd4, 0x08, 0xbf, 0x24, 0xcb, 0xec, 0xdb,
  9714. 0x81, 0xe0, 0xe6, 0x4f, 0x3d, 0x1c, 0x5c, 0x46
  9715. };
  9716. WOLFSSL_SMALL_STACK_STATIC unsigned char cp2[] = {
  9717. 0xe9, 0x58, 0xfe, 0xab, 0x66, 0xb4, 0xf1, 0x79,
  9718. 0x91, 0x3f, 0x91, 0xdc, 0x6f, 0xdf, 0xd6, 0xac,
  9719. 0x65, 0x37, 0x15, 0x53, 0xf1, 0x98, 0xab, 0xb4
  9720. };
  9721. #ifndef HAVE_FIPS /* FIPS requires different keys for main and tweak. */
  9722. WOLFSSL_SMALL_STACK_STATIC unsigned char k3[] = {
  9723. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9724. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9725. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9726. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9727. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9728. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
  9729. };
  9730. WOLFSSL_SMALL_STACK_STATIC unsigned char i3[] = {
  9731. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9732. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9733. };
  9734. WOLFSSL_SMALL_STACK_STATIC unsigned char p3[] = {
  9735. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9736. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9737. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9738. 0x20, 0xff, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9739. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
  9740. };
  9741. WOLFSSL_SMALL_STACK_STATIC unsigned char c3[] = {
  9742. 0xa4, 0xf2, 0x71, 0x5d, 0x80, 0x60, 0x68, 0xa0,
  9743. 0x80, 0x61, 0xd7, 0xc1, 0x55, 0xc8, 0x3a, 0x2e,
  9744. 0xd7, 0xf4, 0x62, 0xaf, 0xbd, 0x2d, 0xf9, 0x5f,
  9745. 0xe8, 0xc5, 0x99, 0x3d, 0x58, 0x3c, 0xeb, 0xba,
  9746. 0x86, 0xea, 0x2c, 0x7e, 0x1f, 0xba, 0x81, 0xde
  9747. };
  9748. #endif /* HAVE_FIPS */
  9749. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9750. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9751. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9752. #endif
  9753. XMEMSET(buf, 0, sizeof(buf));
  9754. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  9755. if (ret != 0)
  9756. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9757. else
  9758. aes_inited = 1;
  9759. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_ENCRYPTION);
  9760. if (ret != 0)
  9761. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9762. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  9763. #if defined(WOLFSSL_ASYNC_CRYPT)
  9764. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9765. #endif
  9766. if (ret != 0)
  9767. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9768. if (XMEMCMP(c2, buf, sizeof(c2)))
  9769. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9770. #ifdef WOLFSSL_AESXTS_STREAM
  9771. ret = wc_AesXtsEncryptInit(aes, i2, sizeof(i2), &stream);
  9772. #if defined(WOLFSSL_ASYNC_CRYPT)
  9773. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9774. #endif
  9775. if (ret != 0)
  9776. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9777. ret = wc_AesXtsEncryptUpdate(aes, buf, p2, AES_BLOCK_SIZE, &stream);
  9778. #if defined(WOLFSSL_ASYNC_CRYPT)
  9779. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9780. #endif
  9781. if (ret != 0)
  9782. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9783. ret = wc_AesXtsEncryptFinal(aes, buf + AES_BLOCK_SIZE, p2 + AES_BLOCK_SIZE, sizeof(p2) - AES_BLOCK_SIZE, &stream);
  9784. #if defined(WOLFSSL_ASYNC_CRYPT)
  9785. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9786. #endif
  9787. if (ret != 0)
  9788. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9789. if (XMEMCMP(c2, buf, sizeof(c2)))
  9790. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9791. #endif /* WOLFSSL_AESXTS_STREAM */
  9792. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9793. defined(WC_C_DYNAMIC_FALLBACK)
  9794. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9795. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  9796. #if defined(WOLFSSL_ASYNC_CRYPT)
  9797. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9798. #endif
  9799. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9800. if (ret != 0)
  9801. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9802. if (XMEMCMP(c2, buf, sizeof(c2)))
  9803. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9804. #endif
  9805. XMEMSET(buf, 0, sizeof(buf));
  9806. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  9807. if (ret != 0)
  9808. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9809. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  9810. #if defined(WOLFSSL_ASYNC_CRYPT)
  9811. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9812. #endif
  9813. if (ret != 0)
  9814. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9815. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  9816. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9817. #ifdef WOLFSSL_AESXTS_STREAM
  9818. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &stream);
  9819. #if defined(WOLFSSL_ASYNC_CRYPT)
  9820. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9821. #endif
  9822. if (ret != 0)
  9823. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9824. ret = wc_AesXtsEncryptUpdate(aes, buf, p1, sizeof(p1), &stream);
  9825. #if defined(WOLFSSL_ASYNC_CRYPT)
  9826. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9827. #endif
  9828. if (ret != 0)
  9829. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9830. ret = wc_AesXtsEncryptFinal(aes, buf, NULL, 0, &stream);
  9831. #if defined(WOLFSSL_ASYNC_CRYPT)
  9832. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9833. #endif
  9834. if (ret != 0)
  9835. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9836. if (XMEMCMP(c1, buf, sizeof(c1)))
  9837. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9838. #endif /* WOLFSSL_AESXTS_STREAM */
  9839. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9840. defined(WC_C_DYNAMIC_FALLBACK)
  9841. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9842. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  9843. #if defined(WOLFSSL_ASYNC_CRYPT)
  9844. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9845. #endif
  9846. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9847. if (ret != 0)
  9848. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9849. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  9850. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9851. #endif
  9852. /* partial block encryption test */
  9853. XMEMSET(cipher, 0, sizeof(cipher));
  9854. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  9855. #if defined(WOLFSSL_ASYNC_CRYPT)
  9856. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9857. #endif
  9858. if (ret != 0)
  9859. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9860. if (XMEMCMP(cp2, cipher, sizeof(cp2)))
  9861. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9862. #ifdef WOLFSSL_AESXTS_STREAM
  9863. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &stream);
  9864. #if defined(WOLFSSL_ASYNC_CRYPT)
  9865. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9866. #endif
  9867. if (ret != 0)
  9868. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9869. ret = wc_AesXtsEncryptFinal(aes, buf, pp, sizeof(pp), &stream);
  9870. #if defined(WOLFSSL_ASYNC_CRYPT)
  9871. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9872. #endif
  9873. if (ret != 0)
  9874. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9875. ret = wc_AesXtsEncryptFinal(aes, buf, NULL, 0, &stream);
  9876. #if defined(WOLFSSL_ASYNC_CRYPT)
  9877. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9878. #endif
  9879. if (ret != 0)
  9880. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9881. if (XMEMCMP(cp2, buf, sizeof(cp2)))
  9882. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9883. #endif /* WOLFSSL_AESXTS_STREAM */
  9884. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9885. defined(WC_C_DYNAMIC_FALLBACK)
  9886. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9887. XMEMSET(cipher, 0, sizeof(cipher));
  9888. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  9889. #if defined(WOLFSSL_ASYNC_CRYPT)
  9890. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9891. #endif
  9892. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9893. if (ret != 0)
  9894. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9895. if (XMEMCMP(cp2, cipher, sizeof(cp2)))
  9896. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9897. #endif
  9898. /* partial block decrypt test */
  9899. XMEMSET(buf, 0, sizeof(buf));
  9900. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  9901. if (ret != 0)
  9902. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9903. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  9904. #if defined(WOLFSSL_ASYNC_CRYPT)
  9905. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9906. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9907. #else
  9908. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9909. #endif
  9910. #endif
  9911. if (ret != 0)
  9912. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9913. if (XMEMCMP(pp, buf, sizeof(pp)))
  9914. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9915. #ifdef WOLFSSL_AESXTS_STREAM
  9916. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  9917. #if defined(WOLFSSL_ASYNC_CRYPT)
  9918. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9919. #endif
  9920. if (ret != 0)
  9921. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9922. ret = wc_AesXtsDecryptFinal(aes, buf, cipher, sizeof(pp), &stream);
  9923. #if defined(WOLFSSL_ASYNC_CRYPT)
  9924. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9925. #endif
  9926. if (ret != 0)
  9927. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9928. if (XMEMCMP(pp, buf, sizeof(pp)))
  9929. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9930. #endif /* WOLFSSL_AESXTS_STREAM */
  9931. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9932. defined(WC_C_DYNAMIC_FALLBACK)
  9933. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9934. XMEMSET(buf, 0, sizeof(buf));
  9935. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  9936. #if defined(WOLFSSL_ASYNC_CRYPT)
  9937. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9938. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9939. #else
  9940. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9941. #endif
  9942. #endif
  9943. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9944. if (ret != 0)
  9945. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9946. if (XMEMCMP(pp, buf, sizeof(pp)))
  9947. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9948. #endif
  9949. /* NIST decrypt test vector */
  9950. XMEMSET(buf, 0, sizeof(buf));
  9951. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  9952. #if defined(WOLFSSL_ASYNC_CRYPT)
  9953. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9954. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9955. #else
  9956. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9957. #endif
  9958. #endif
  9959. if (ret != 0)
  9960. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9961. if (XMEMCMP(p1, buf, sizeof(p1)))
  9962. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9963. #ifdef WOLFSSL_AESXTS_STREAM
  9964. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  9965. #if defined(WOLFSSL_ASYNC_CRYPT)
  9966. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9967. #endif
  9968. if (ret != 0)
  9969. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9970. ret = wc_AesXtsDecryptFinal(aes, buf, c1, sizeof(c1), &stream);
  9971. #if defined(WOLFSSL_ASYNC_CRYPT)
  9972. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9973. #endif
  9974. if (ret != 0)
  9975. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9976. if (XMEMCMP(p1, buf, sizeof(p1)))
  9977. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9978. #endif /* WOLFSSL_AESXTS_STREAM */
  9979. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9980. defined(WC_C_DYNAMIC_FALLBACK)
  9981. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9982. XMEMSET(buf, 0, sizeof(buf));
  9983. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  9984. #if defined(WOLFSSL_ASYNC_CRYPT)
  9985. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9986. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9987. #else
  9988. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9989. #endif
  9990. #endif
  9991. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9992. if (ret != 0)
  9993. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9994. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  9995. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9996. #endif
  9997. /* fail case with decrypting using wrong key */
  9998. XMEMSET(buf, 0, sizeof(buf));
  9999. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  10000. #if defined(WOLFSSL_ASYNC_CRYPT)
  10001. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10002. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10003. #else
  10004. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10005. #endif
  10006. #endif
  10007. if (ret != 0)
  10008. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10009. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  10010. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10011. /* set correct key and retest */
  10012. XMEMSET(buf, 0, sizeof(buf));
  10013. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_DECRYPTION);
  10014. if (ret != 0)
  10015. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10016. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  10017. #if defined(WOLFSSL_ASYNC_CRYPT)
  10018. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10019. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10020. #else
  10021. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10022. #endif
  10023. #endif
  10024. if (ret != 0)
  10025. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10026. if (XMEMCMP(p2, buf, sizeof(p2)))
  10027. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10028. #ifndef HAVE_FIPS
  10029. /* Test ciphertext stealing in-place. */
  10030. XMEMCPY(buf, p3, sizeof(p3));
  10031. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_ENCRYPTION);
  10032. if (ret != 0)
  10033. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10034. ret = wc_AesXtsEncrypt(aes, buf, buf, sizeof(p3), i3, sizeof(i3));
  10035. #if defined(WOLFSSL_ASYNC_CRYPT)
  10036. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10037. #endif
  10038. if (ret != 0)
  10039. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10040. if (XMEMCMP(c3, buf, sizeof(c3)))
  10041. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10042. #ifdef WOLFSSL_AESXTS_STREAM
  10043. ret = wc_AesXtsEncryptInit(aes, i3, sizeof(i3), &stream);
  10044. #if defined(WOLFSSL_ASYNC_CRYPT)
  10045. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10046. #endif
  10047. if (ret != 0)
  10048. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10049. ret = wc_AesXtsEncryptUpdate(aes, buf, p3, AES_BLOCK_SIZE, &stream);
  10050. #if defined(WOLFSSL_ASYNC_CRYPT)
  10051. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10052. #endif
  10053. if (ret != 0)
  10054. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10055. ret = wc_AesXtsEncryptFinal(aes, buf + AES_BLOCK_SIZE, p3 + AES_BLOCK_SIZE, sizeof(p3) - AES_BLOCK_SIZE, &stream);
  10056. #if defined(WOLFSSL_ASYNC_CRYPT)
  10057. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10058. #endif
  10059. if (ret != 0)
  10060. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10061. if (XMEMCMP(c3, buf, sizeof(c3)))
  10062. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10063. #endif /* WOLFSSL_AESXTS_STREAM */
  10064. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_DECRYPTION);
  10065. if (ret != 0)
  10066. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10067. ret = wc_AesXtsDecrypt(aes, buf, buf, sizeof(c3), i3, sizeof(i3));
  10068. #if defined(WOLFSSL_ASYNC_CRYPT)
  10069. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10070. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10071. #else
  10072. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10073. #endif
  10074. #endif
  10075. if (ret != 0)
  10076. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10077. if (XMEMCMP(p3, buf, sizeof(p3)))
  10078. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10079. #ifdef WOLFSSL_AESXTS_STREAM
  10080. ret = wc_AesXtsDecryptInit(aes, i3, sizeof(i3), &stream);
  10081. #if defined(WOLFSSL_ASYNC_CRYPT)
  10082. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10083. #endif
  10084. if (ret != 0)
  10085. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10086. ret = wc_AesXtsDecryptUpdate(aes, buf, c3, AES_BLOCK_SIZE, &stream);
  10087. #if defined(WOLFSSL_ASYNC_CRYPT)
  10088. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10089. #endif
  10090. if (ret != 0)
  10091. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10092. ret = wc_AesXtsDecryptFinal(aes, buf + AES_BLOCK_SIZE, c3 + AES_BLOCK_SIZE, sizeof(c3) - AES_BLOCK_SIZE, &stream);
  10093. #if defined(WOLFSSL_ASYNC_CRYPT)
  10094. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10095. #endif
  10096. if (ret != 0)
  10097. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10098. if (XMEMCMP(p3, buf, sizeof(p3)))
  10099. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10100. #endif /* WOLFSSL_AESXTS_STREAM */
  10101. #endif /* !HAVE_FIPS */
  10102. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10103. !defined(WOLFSSL_AFALG)
  10104. {
  10105. int i;
  10106. int j;
  10107. #ifdef WOLFSSL_AESXTS_STREAM
  10108. int k;
  10109. #endif
  10110. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10111. large_input = (byte *)XMALLOC(LARGE_XTS_SZ, HEAP_HINT,
  10112. DYNAMIC_TYPE_TMP_BUFFER);
  10113. if (large_input == NULL)
  10114. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), out);
  10115. #endif
  10116. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  10117. large_input[i] = (byte)i;
  10118. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  10119. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10120. if (ret != 0)
  10121. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10122. ret = wc_AesXtsEncrypt(aes, large_input, large_input, j, i1,
  10123. sizeof(i1));
  10124. #if defined(WOLFSSL_ASYNC_CRYPT)
  10125. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10126. #endif
  10127. if (ret != 0)
  10128. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10129. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10130. if (ret != 0)
  10131. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10132. ret = wc_AesXtsDecrypt(aes, large_input, large_input, j, i1,
  10133. sizeof(i1));
  10134. #if defined(WOLFSSL_ASYNC_CRYPT)
  10135. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10136. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  10137. WC_ASYNC_FLAG_NONE);
  10138. #else
  10139. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10140. #endif
  10141. #endif
  10142. if (ret != 0)
  10143. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10144. for (i = 0; i < j; i++) {
  10145. if (large_input[i] != (byte)i) {
  10146. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10147. }
  10148. }
  10149. }
  10150. #ifdef WOLFSSL_AESXTS_STREAM
  10151. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  10152. large_input[i] = (byte)i;
  10153. /* first, encrypt block by block then decrypt with a one-shot call. */
  10154. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  10155. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10156. if (ret != 0)
  10157. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10158. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &stream);
  10159. #if defined(WOLFSSL_ASYNC_CRYPT)
  10160. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10161. #endif
  10162. if (ret != 0)
  10163. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10164. for (k = 0; k < j; k += AES_BLOCK_SIZE) {
  10165. if ((j - k) < AES_BLOCK_SIZE*2)
  10166. ret = wc_AesXtsEncryptFinal(aes, large_input + k, large_input + k, j - k, &stream);
  10167. else
  10168. ret = wc_AesXtsEncryptUpdate(aes, large_input + k, large_input + k, AES_BLOCK_SIZE, &stream);
  10169. #if defined(WOLFSSL_ASYNC_CRYPT)
  10170. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10171. #endif
  10172. if (ret != 0)
  10173. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10174. if ((j - k) < AES_BLOCK_SIZE*2)
  10175. break;
  10176. }
  10177. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10178. if (ret != 0)
  10179. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10180. ret = wc_AesXtsDecrypt(aes, large_input, large_input, (word32)j, i1,
  10181. sizeof(i1));
  10182. #if defined(WOLFSSL_ASYNC_CRYPT)
  10183. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10184. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  10185. WC_ASYNC_FLAG_NONE);
  10186. #else
  10187. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10188. #endif
  10189. #endif
  10190. if (ret != 0)
  10191. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10192. for (i = 0; i < j; i++) {
  10193. if (large_input[i] != (byte)i) {
  10194. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10195. }
  10196. }
  10197. }
  10198. /* second, encrypt with a one-shot call then decrypt block by block. */
  10199. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  10200. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10201. if (ret != 0)
  10202. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10203. ret = wc_AesXtsEncrypt(aes, large_input, large_input, (word32)j, i1,
  10204. sizeof(i1));
  10205. #if defined(WOLFSSL_ASYNC_CRYPT)
  10206. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10207. #endif
  10208. if (ret != 0)
  10209. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10210. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10211. if (ret != 0)
  10212. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10213. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  10214. #if defined(WOLFSSL_ASYNC_CRYPT)
  10215. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10216. #endif
  10217. if (ret != 0)
  10218. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10219. for (k = 0; k < j; k += AES_BLOCK_SIZE) {
  10220. if ((j - k) < AES_BLOCK_SIZE*2)
  10221. ret = wc_AesXtsDecryptFinal(aes, large_input + k, large_input + k, j - k, &stream);
  10222. else
  10223. ret = wc_AesXtsDecryptUpdate(aes, large_input + k, large_input + k, AES_BLOCK_SIZE, &stream);
  10224. #if defined(WOLFSSL_ASYNC_CRYPT)
  10225. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10226. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  10227. WC_ASYNC_FLAG_NONE);
  10228. #else
  10229. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10230. #endif
  10231. #endif
  10232. if (ret != 0)
  10233. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10234. if ((j - k) < AES_BLOCK_SIZE*2)
  10235. break;
  10236. }
  10237. for (i = 0; i < j; i++) {
  10238. if (large_input[i] != (byte)i) {
  10239. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10240. }
  10241. }
  10242. }
  10243. #endif /* WOLFSSL_AESXTS_STREAM */
  10244. }
  10245. #endif /* !BENCH_EMBEDDED && !HAVE_CAVIUM &&
  10246. * !WOLFSSL_AFALG
  10247. */
  10248. out:
  10249. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10250. !defined(WOLFSSL_AFALG) && defined(WOLFSSL_SMALL_STACK) && \
  10251. !defined(WOLFSSL_NO_MALLOC)
  10252. if (large_input)
  10253. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10254. #endif
  10255. if (aes_inited)
  10256. wc_AesXtsFree(aes);
  10257. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10258. if (aes)
  10259. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  10260. #endif
  10261. return ret;
  10262. }
  10263. #endif /* WOLFSSL_AES_192 */
  10264. #ifdef WOLFSSL_AES_256
  10265. static wc_test_ret_t aes_xts_256_test(void)
  10266. {
  10267. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10268. XtsAes *aes = NULL;
  10269. #else
  10270. XtsAes aes[1];
  10271. #endif
  10272. int aes_inited = 0;
  10273. wc_test_ret_t ret = 0;
  10274. unsigned char buf[AES_BLOCK_SIZE * 3];
  10275. unsigned char cipher[AES_BLOCK_SIZE * 3];
  10276. #ifdef WOLFSSL_AESXTS_STREAM
  10277. struct XtsAesStreamData stream;
  10278. #endif
  10279. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10280. !defined(WOLFSSL_AFALG)
  10281. #define LARGE_XTS_SZ 1024
  10282. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10283. byte* large_input = NULL;
  10284. #else
  10285. byte large_input[LARGE_XTS_SZ];
  10286. #endif
  10287. #endif
  10288. /* 256 key tests */
  10289. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  10290. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  10291. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  10292. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  10293. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  10294. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  10295. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3,
  10296. 0xd6, 0x19, 0xb7, 0xe2, 0xb9, 0x14, 0x1e, 0x58,
  10297. 0x31, 0x8e, 0xea, 0x39, 0x2c, 0xf4, 0x1b, 0x08
  10298. };
  10299. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  10300. 0xad, 0xf8, 0xd9, 0x26, 0x27, 0x46, 0x4a, 0xd2,
  10301. 0xf0, 0x42, 0x8e, 0x84, 0xa9, 0xf8, 0x75, 0x64
  10302. };
  10303. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  10304. 0x2e, 0xed, 0xea, 0x52, 0xcd, 0x82, 0x15, 0xe1,
  10305. 0xac, 0xc6, 0x47, 0xe8, 0x10, 0xbb, 0xc3, 0x64,
  10306. 0x2e, 0x87, 0x28, 0x7f, 0x8d, 0x2e, 0x57, 0xe3,
  10307. 0x6c, 0x0a, 0x24, 0xfb, 0xc1, 0x2a, 0x20, 0x2e
  10308. };
  10309. /* plain text test of partial block is not from NIST test vector list */
  10310. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  10311. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  10312. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  10313. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  10314. };
  10315. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  10316. 0xcb, 0xaa, 0xd0, 0xe2, 0xf6, 0xce, 0xa3, 0xf5,
  10317. 0x0b, 0x37, 0xf9, 0x34, 0xd4, 0x6a, 0x9b, 0x13,
  10318. 0x0b, 0x9d, 0x54, 0xf0, 0x7e, 0x34, 0xf3, 0x6a,
  10319. 0xf7, 0x93, 0xe8, 0x6f, 0x73, 0xc6, 0xd7, 0xdb
  10320. };
  10321. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  10322. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  10323. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  10324. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  10325. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  10326. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  10327. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a,
  10328. 0xb5, 0xb0, 0xc7, 0xe5, 0x2d, 0xb5, 0x06, 0x12,
  10329. 0xd2, 0xb5, 0x3a, 0xcb, 0x47, 0x8a, 0x53, 0xb4
  10330. };
  10331. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  10332. 0xe6, 0x42, 0x19, 0xed, 0xe0, 0xe1, 0xc2, 0xa0,
  10333. 0x0e, 0xf5, 0x58, 0x6a, 0xc4, 0x9b, 0xeb, 0x6f
  10334. };
  10335. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  10336. 0x24, 0xcb, 0x76, 0x22, 0x55, 0xb5, 0xa8, 0x00,
  10337. 0xf4, 0x6e, 0x80, 0x60, 0x56, 0x9e, 0x05, 0x53,
  10338. 0xbc, 0xfe, 0x86, 0x55, 0x3b, 0xca, 0xd5, 0x89,
  10339. 0xc7, 0x54, 0x1a, 0x73, 0xac, 0xc3, 0x9a, 0xbd,
  10340. 0x53, 0xc4, 0x07, 0x76, 0xd8, 0xe8, 0x22, 0x61,
  10341. 0x9e, 0xa9, 0xad, 0x77, 0xa0, 0x13, 0x4c, 0xfc
  10342. };
  10343. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  10344. 0xa3, 0xc6, 0xf3, 0xf3, 0x82, 0x79, 0x5b, 0x10,
  10345. 0x87, 0xd7, 0x02, 0x50, 0xdb, 0x2c, 0xd3, 0xb1,
  10346. 0xa1, 0x62, 0xa8, 0xb6, 0xdc, 0x12, 0x60, 0x61,
  10347. 0xc1, 0x0a, 0x84, 0xa5, 0x85, 0x3f, 0x3a, 0x89,
  10348. 0xe6, 0x6c, 0xdb, 0xb7, 0x9a, 0xb4, 0x28, 0x9b,
  10349. 0xc3, 0xea, 0xd8, 0x10, 0xe9, 0xc0, 0xaf, 0x92
  10350. };
  10351. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10352. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10353. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10354. #endif
  10355. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  10356. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  10357. ret = EVP_test(EVP_aes_256_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  10358. if (ret != 0) {
  10359. printf("EVP_aes_256_xts failed\n");
  10360. goto out;
  10361. }
  10362. #endif
  10363. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  10364. if (ret != 0)
  10365. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10366. else
  10367. aes_inited = 1;
  10368. XMEMSET(buf, 0, sizeof(buf));
  10369. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_ENCRYPTION);
  10370. if (ret != 0)
  10371. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10372. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  10373. #if defined(WOLFSSL_ASYNC_CRYPT)
  10374. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10375. #endif
  10376. if (ret != 0)
  10377. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10378. if (XMEMCMP(c2, buf, sizeof(c2)))
  10379. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10380. #ifdef WOLFSSL_AESXTS_STREAM
  10381. ret = wc_AesXtsEncryptInit(aes, i2, sizeof(i2), &stream);
  10382. #if defined(WOLFSSL_ASYNC_CRYPT)
  10383. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10384. #endif
  10385. if (ret != 0)
  10386. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10387. ret = wc_AesXtsEncryptUpdate(aes, buf, p2, AES_BLOCK_SIZE, &stream);
  10388. #if defined(WOLFSSL_ASYNC_CRYPT)
  10389. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10390. #endif
  10391. if (ret != 0)
  10392. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10393. ret = wc_AesXtsEncryptFinal(aes, buf + AES_BLOCK_SIZE, p2 + AES_BLOCK_SIZE, sizeof(p2) - AES_BLOCK_SIZE, &stream);
  10394. #if defined(WOLFSSL_ASYNC_CRYPT)
  10395. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10396. #endif
  10397. if (ret != 0)
  10398. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10399. if (XMEMCMP(c2, buf, sizeof(c2)))
  10400. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10401. #endif /* WOLFSSL_AESXTS_STREAM */
  10402. XMEMSET(buf, 0, sizeof(buf));
  10403. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10404. if (ret != 0)
  10405. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10406. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  10407. #if defined(WOLFSSL_ASYNC_CRYPT)
  10408. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10409. #endif
  10410. if (ret != 0)
  10411. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10412. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  10413. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10414. #ifdef WOLFSSL_AESXTS_STREAM
  10415. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &stream);
  10416. #if defined(WOLFSSL_ASYNC_CRYPT)
  10417. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10418. #endif
  10419. if (ret != 0)
  10420. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10421. ret = wc_AesXtsEncryptUpdate(aes, buf, p1, sizeof(p1), &stream);
  10422. #if defined(WOLFSSL_ASYNC_CRYPT)
  10423. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10424. #endif
  10425. if (ret != 0)
  10426. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10427. ret = wc_AesXtsEncryptFinal(aes, buf, NULL, 0, &stream);
  10428. #if defined(WOLFSSL_ASYNC_CRYPT)
  10429. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10430. #endif
  10431. if (ret != 0)
  10432. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10433. if (XMEMCMP(c1, buf, sizeof(c1)))
  10434. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10435. #endif /* WOLFSSL_AESXTS_STREAM */
  10436. /* partial block encryption test */
  10437. XMEMSET(cipher, 0, sizeof(cipher));
  10438. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  10439. #if defined(WOLFSSL_ASYNC_CRYPT)
  10440. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10441. #endif
  10442. if (ret != 0)
  10443. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10444. /* partial block decrypt test */
  10445. XMEMSET(buf, 0, sizeof(buf));
  10446. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10447. if (ret != 0)
  10448. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10449. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  10450. #if defined(WOLFSSL_ASYNC_CRYPT)
  10451. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10452. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10453. #else
  10454. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10455. #endif
  10456. #endif
  10457. if (ret != 0)
  10458. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10459. if (XMEMCMP(pp, buf, sizeof(pp)))
  10460. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10461. /* NIST decrypt test vector */
  10462. XMEMSET(buf, 0, sizeof(buf));
  10463. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  10464. #if defined(WOLFSSL_ASYNC_CRYPT)
  10465. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10466. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10467. #else
  10468. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10469. #endif
  10470. #endif
  10471. if (ret != 0)
  10472. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10473. if (XMEMCMP(p1, buf, sizeof(p1)))
  10474. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10475. #ifdef WOLFSSL_AESXTS_STREAM
  10476. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  10477. #if defined(WOLFSSL_ASYNC_CRYPT)
  10478. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10479. #endif
  10480. if (ret != 0)
  10481. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10482. ret = wc_AesXtsDecryptFinal(aes, buf, c1, sizeof(c1), &stream);
  10483. #if defined(WOLFSSL_ASYNC_CRYPT)
  10484. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10485. #endif
  10486. if (ret != 0)
  10487. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10488. if (XMEMCMP(p1, buf, sizeof(p1)))
  10489. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10490. #endif /* WOLFSSL_AESXTS_STREAM */
  10491. XMEMSET(buf, 0, sizeof(buf));
  10492. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_DECRYPTION);
  10493. if (ret != 0)
  10494. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10495. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  10496. #if defined(WOLFSSL_ASYNC_CRYPT)
  10497. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10498. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10499. #else
  10500. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10501. #endif
  10502. #endif
  10503. if (ret != 0)
  10504. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10505. if (XMEMCMP(p2, buf, sizeof(p2)))
  10506. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10507. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10508. !defined(WOLFSSL_AFALG)
  10509. {
  10510. int i;
  10511. int j;
  10512. #ifdef WOLFSSL_AESXTS_STREAM
  10513. int k;
  10514. #endif
  10515. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10516. large_input = (byte *)XMALLOC(LARGE_XTS_SZ, HEAP_HINT,
  10517. DYNAMIC_TYPE_TMP_BUFFER);
  10518. if (large_input == NULL)
  10519. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), out);
  10520. #endif
  10521. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  10522. large_input[i] = (byte)i;
  10523. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  10524. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10525. if (ret != 0)
  10526. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10527. ret = wc_AesXtsEncrypt(aes, large_input, large_input, j, i1,
  10528. sizeof(i1));
  10529. #if defined(WOLFSSL_ASYNC_CRYPT)
  10530. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10531. #endif
  10532. if (ret != 0)
  10533. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10534. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10535. if (ret != 0)
  10536. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10537. ret = wc_AesXtsDecrypt(aes, large_input, large_input, j, i1,
  10538. sizeof(i1));
  10539. #if defined(WOLFSSL_ASYNC_CRYPT)
  10540. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10541. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  10542. WC_ASYNC_FLAG_NONE);
  10543. #else
  10544. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10545. #endif
  10546. #endif
  10547. if (ret != 0)
  10548. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10549. for (i = 0; i < j; i++) {
  10550. if (large_input[i] != (byte)i) {
  10551. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10552. }
  10553. }
  10554. }
  10555. #ifdef WOLFSSL_AESXTS_STREAM
  10556. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  10557. large_input[i] = (byte)i;
  10558. /* first, encrypt block by block then decrypt with a one-shot call. */
  10559. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  10560. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10561. if (ret != 0)
  10562. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10563. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &stream);
  10564. #if defined(WOLFSSL_ASYNC_CRYPT)
  10565. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10566. #endif
  10567. if (ret != 0)
  10568. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10569. for (k = 0; k < j; k += AES_BLOCK_SIZE) {
  10570. if ((j - k) < AES_BLOCK_SIZE*2)
  10571. ret = wc_AesXtsEncryptFinal(aes, large_input + k, large_input + k, j - k, &stream);
  10572. else
  10573. ret = wc_AesXtsEncryptUpdate(aes, large_input + k, large_input + k, AES_BLOCK_SIZE, &stream);
  10574. #if defined(WOLFSSL_ASYNC_CRYPT)
  10575. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10576. #endif
  10577. if (ret != 0)
  10578. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10579. if ((j - k) < AES_BLOCK_SIZE*2)
  10580. break;
  10581. }
  10582. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10583. if (ret != 0)
  10584. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10585. ret = wc_AesXtsDecrypt(aes, large_input, large_input, (word32)j, i1,
  10586. sizeof(i1));
  10587. #if defined(WOLFSSL_ASYNC_CRYPT)
  10588. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10589. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  10590. WC_ASYNC_FLAG_NONE);
  10591. #else
  10592. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10593. #endif
  10594. #endif
  10595. if (ret != 0)
  10596. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10597. for (i = 0; i < j; i++) {
  10598. if (large_input[i] != (byte)i) {
  10599. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10600. }
  10601. }
  10602. }
  10603. /* second, encrypt with a one-shot call then decrypt block by block. */
  10604. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  10605. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10606. if (ret != 0)
  10607. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10608. ret = wc_AesXtsEncrypt(aes, large_input, large_input, (word32)j, i1,
  10609. sizeof(i1));
  10610. #if defined(WOLFSSL_ASYNC_CRYPT)
  10611. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10612. #endif
  10613. if (ret != 0)
  10614. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10615. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10616. if (ret != 0)
  10617. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10618. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  10619. #if defined(WOLFSSL_ASYNC_CRYPT)
  10620. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10621. #endif
  10622. if (ret != 0)
  10623. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10624. for (k = 0; k < j; k += AES_BLOCK_SIZE) {
  10625. if ((j - k) < AES_BLOCK_SIZE*2)
  10626. ret = wc_AesXtsDecryptFinal(aes, large_input + k, large_input + k, j - k, &stream);
  10627. else
  10628. ret = wc_AesXtsDecryptUpdate(aes, large_input + k, large_input + k, AES_BLOCK_SIZE, &stream);
  10629. #if defined(WOLFSSL_ASYNC_CRYPT)
  10630. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10631. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  10632. WC_ASYNC_FLAG_NONE);
  10633. #else
  10634. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10635. #endif
  10636. #endif
  10637. if (ret != 0)
  10638. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10639. if ((j - k) < AES_BLOCK_SIZE*2)
  10640. break;
  10641. }
  10642. for (i = 0; i < j; i++) {
  10643. if (large_input[i] != (byte)i) {
  10644. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10645. }
  10646. }
  10647. }
  10648. #endif /* WOLFSSL_AESXTS_STREAM */
  10649. }
  10650. #endif /* !BENCH_EMBEDDED && !HAVE_CAVIUM &&
  10651. * !WOLFSSL_AFALG
  10652. */
  10653. out:
  10654. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10655. !defined(WOLFSSL_AFALG) && defined(WOLFSSL_SMALL_STACK) && \
  10656. !defined(WOLFSSL_NO_MALLOC)
  10657. if (large_input)
  10658. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10659. #endif
  10660. if (aes_inited)
  10661. wc_AesXtsFree(aes);
  10662. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10663. if (aes)
  10664. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  10665. #endif
  10666. return ret;
  10667. }
  10668. #endif /* WOLFSSL_AES_256 */
  10669. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  10670. /* both 128 and 256 bit key test */
  10671. static wc_test_ret_t aes_xts_sector_test(void)
  10672. {
  10673. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10674. XtsAes *aes = NULL;
  10675. #else
  10676. XtsAes aes[1];
  10677. #endif
  10678. int aes_inited = 0;
  10679. wc_test_ret_t ret = 0;
  10680. unsigned char buf[AES_BLOCK_SIZE * 2];
  10681. /* 128 key tests */
  10682. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  10683. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  10684. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  10685. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  10686. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  10687. };
  10688. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  10689. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  10690. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  10691. };
  10692. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  10693. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  10694. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  10695. };
  10696. word64 s1 = 141;
  10697. /* 256 key tests */
  10698. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  10699. 0xef, 0x01, 0x0c, 0xa1, 0xa3, 0x66, 0x3e, 0x32,
  10700. 0x53, 0x43, 0x49, 0xbc, 0x0b, 0xae, 0x62, 0x23,
  10701. 0x2a, 0x15, 0x73, 0x34, 0x85, 0x68, 0xfb, 0x9e,
  10702. 0xf4, 0x17, 0x68, 0xa7, 0x67, 0x4f, 0x50, 0x7a,
  10703. 0x72, 0x7f, 0x98, 0x75, 0x53, 0x97, 0xd0, 0xe0,
  10704. 0xaa, 0x32, 0xf8, 0x30, 0x33, 0x8c, 0xc7, 0xa9,
  10705. 0x26, 0xc7, 0x73, 0xf0, 0x9e, 0x57, 0xb3, 0x57,
  10706. 0xcd, 0x15, 0x6a, 0xfb, 0xca, 0x46, 0xe1, 0xa0
  10707. };
  10708. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  10709. 0xed, 0x98, 0xe0, 0x17, 0x70, 0xa8, 0x53, 0xb4,
  10710. 0x9d, 0xb9, 0xe6, 0xaa, 0xf8, 0x8f, 0x0a, 0x41,
  10711. 0xb9, 0xb5, 0x6e, 0x91, 0xa5, 0xa2, 0xb1, 0x1d,
  10712. 0x40, 0x52, 0x92, 0x54, 0xf5, 0x52, 0x3e, 0x75
  10713. };
  10714. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  10715. 0xca, 0x20, 0xc5, 0x5e, 0x8d, 0xc1, 0x49, 0x68,
  10716. 0x7d, 0x25, 0x41, 0xde, 0x39, 0xc3, 0xdf, 0x63,
  10717. 0x00, 0xbb, 0x5a, 0x16, 0x3c, 0x10, 0xce, 0xd3,
  10718. 0x66, 0x6b, 0x13, 0x57, 0xdb, 0x8b, 0xd3, 0x9d
  10719. };
  10720. word64 s2 = 187;
  10721. #if !defined(BENCH_EMBEDDED) && \
  10722. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  10723. /* Sector size for encrypt/decrypt consecutive sectors testcase */
  10724. word32 sectorSz = 512;
  10725. unsigned char data[550];
  10726. WOLFSSL_SMALL_STACK_STATIC unsigned char k3[] = {
  10727. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  10728. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  10729. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  10730. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22
  10731. };
  10732. WOLFSSL_SMALL_STACK_STATIC unsigned char p3[] = {
  10733. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  10734. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10735. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
  10736. 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  10737. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
  10738. 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  10739. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53,
  10740. 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  10741. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
  10742. 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  10743. 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
  10744. 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  10745. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
  10746. 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  10747. 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
  10748. 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  10749. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb,
  10750. 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  10751. 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
  10752. 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  10753. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
  10754. 0xfc, 0xfd, 0xfe, 0xff,
  10755. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  10756. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10757. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
  10758. 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  10759. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
  10760. 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  10761. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53,
  10762. 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  10763. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
  10764. 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  10765. 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
  10766. 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  10767. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
  10768. 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  10769. 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
  10770. 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  10771. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb,
  10772. 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  10773. 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
  10774. 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  10775. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
  10776. 0xfc, 0xfd, 0xfe, 0xff,
  10777. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  10778. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10779. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  10780. };
  10781. WOLFSSL_SMALL_STACK_STATIC unsigned char c3[] = {
  10782. 0xb9, 0x6b, 0x2b, 0xfd, 0x61, 0x87, 0x84, 0xd5, 0x26, 0xd2, 0x8c, 0x62,
  10783. 0x63, 0x01, 0xca, 0x46, 0xb1, 0x82, 0xfa, 0xdc, 0xbc, 0x32, 0x18, 0xe9,
  10784. 0xda, 0xe6, 0xda, 0xd1, 0x1a, 0x52, 0x77, 0xca, 0xdb, 0x0e, 0xbe, 0x37,
  10785. 0x88, 0x36, 0x1c, 0x87, 0x16, 0x60, 0xfe, 0xa8, 0x9e, 0xf6, 0x48, 0x64,
  10786. 0x94, 0x34, 0x64, 0xed, 0xf6, 0x9a, 0xc5, 0x28, 0xc9, 0xed, 0x64, 0x80,
  10787. 0x85, 0xd8, 0x93, 0xa7, 0x50, 0xb1, 0x9d, 0x2f, 0x1e, 0x34, 0xcc, 0xb4,
  10788. 0x03, 0xfb, 0x6b, 0x43, 0x21, 0xa8, 0x5b, 0xc6, 0x59, 0x13, 0xd2, 0xb5,
  10789. 0xf5, 0x7b, 0xf6, 0xb2, 0xa4, 0x7a, 0xd2, 0x50, 0x26, 0xcb, 0xa4, 0x83,
  10790. 0xc3, 0x56, 0xb0, 0xb1, 0x14, 0x34, 0x12, 0x1b, 0xea, 0x26, 0x97, 0x24,
  10791. 0x54, 0xcc, 0x32, 0x4c, 0xa4, 0xc2, 0xa3, 0x07, 0xfa, 0x30, 0xa9, 0xf0,
  10792. 0x91, 0x17, 0x60, 0x68, 0x88, 0x7f, 0x34, 0x7e, 0xbd, 0x20, 0x33, 0x95,
  10793. 0x6e, 0xc0, 0xb6, 0x2b, 0xff, 0x7e, 0x61, 0x35, 0x9a, 0x88, 0xff, 0xd9,
  10794. 0x69, 0x21, 0xe7, 0x8f, 0x45, 0x02, 0xf9, 0xd7, 0xeb, 0xa6, 0x53, 0xf1,
  10795. 0x73, 0x04, 0xf1, 0x0b, 0x85, 0xc6, 0x1f, 0x4a, 0x51, 0x2f, 0x95, 0x87,
  10796. 0x5a, 0x67, 0x37, 0xb2, 0x87, 0xf7, 0xbe, 0x2a, 0x17, 0x57, 0xca, 0xfc,
  10797. 0xdd, 0x5f, 0x37, 0x48, 0x78, 0xbd, 0xfa, 0x75, 0xc9, 0xfa, 0x86, 0x7e,
  10798. 0xc4, 0x0f, 0x60, 0x85, 0xce, 0x12, 0x44, 0x7c, 0xd9, 0xb2, 0x50, 0xd9,
  10799. 0x57, 0x85, 0xa5, 0xd7, 0x68, 0x59, 0x03, 0x09, 0x97, 0x2e, 0x8e, 0xa5,
  10800. 0xe3, 0x98, 0xac, 0x16, 0xfb, 0x6d, 0x54, 0xc5, 0x5d, 0x7a, 0x33, 0x44,
  10801. 0x0a, 0x39, 0x91, 0xcc, 0x9f, 0x67, 0xf9, 0x89, 0xbb, 0x62, 0x02, 0xc4,
  10802. 0x22, 0xec, 0xcf, 0x97, 0x69, 0x81, 0x3d, 0x00, 0xfd, 0xeb, 0x55, 0x08,
  10803. 0xa2, 0xff, 0x97, 0xaa, 0x79, 0xde, 0x3c, 0x8a, 0x78, 0x71, 0x73, 0xa2,
  10804. 0x98, 0x2f, 0xd8, 0x5c, 0x62, 0x1c, 0x5c, 0x23, 0x0a, 0xd1, 0xf1, 0x81,
  10805. 0x8a, 0x12, 0xe7, 0x4d, 0xdd, 0x4f, 0xd4, 0xf1, 0xe8, 0x0f, 0x25, 0x79,
  10806. 0x45, 0x4a, 0x49, 0x49, 0x7e, 0x56, 0x91, 0x4e, 0xaa, 0xba, 0x18, 0xe1,
  10807. 0xe4, 0xbe, 0x21, 0xdc, 0x58, 0x60, 0x6f, 0x6a, 0x7f, 0xdc, 0x5e, 0x74,
  10808. 0x47, 0xbf, 0xeb, 0x84, 0xc4, 0x1e, 0x5a, 0x61, 0x64, 0xc8, 0x63, 0x68,
  10809. 0xfa, 0x17, 0x9c, 0xac, 0x60, 0x1c, 0xa5, 0x6e, 0x00, 0x21, 0x93, 0x3c,
  10810. 0xd7, 0xbb, 0x73, 0x45, 0xf7, 0x34, 0x81, 0x6c, 0xfa, 0xf2, 0x33, 0xfd,
  10811. 0xb1, 0x40, 0x30, 0x6b, 0x30, 0xd1, 0x83, 0x5e, 0x2e, 0x7a, 0xce, 0xa6,
  10812. 0x12, 0x2a, 0x15, 0x03, 0x78, 0x29, 0xb9, 0x07, 0xae, 0xe7, 0xc2, 0x78,
  10813. 0x74, 0x72, 0xa5, 0x0e, 0x6b, 0x1f, 0x78, 0xf2, 0x5a, 0x69, 0xb6, 0x2b,
  10814. 0x99, 0x94, 0x1f, 0x89, 0xd1, 0x21, 0x14, 0x4a, 0x54, 0xab, 0x5a, 0x9f,
  10815. 0xaa, 0xa7, 0x96, 0x0a, 0x21, 0xce, 0x30, 0xb6, 0x70, 0x81, 0xe9, 0xd3,
  10816. 0x71, 0xc0, 0xf1, 0x15, 0xe2, 0xf6, 0xd3, 0xcc, 0x41, 0x15, 0x9d, 0xd5,
  10817. 0xa3, 0xa4, 0xe0, 0xf8, 0x62, 0xc4, 0x76, 0x65, 0x63, 0x89, 0xa7, 0xe2,
  10818. 0xfb, 0xf5, 0xc9, 0x80, 0x15, 0x5b, 0xc1, 0x59, 0xb2, 0xd0, 0x01, 0x3a,
  10819. 0xf9, 0xab, 0x5b, 0x79, 0x54, 0xed, 0x6b, 0xf9, 0x1d, 0x9d, 0x87, 0x63,
  10820. 0x80, 0x4f, 0xec, 0x9c, 0x4f, 0xad, 0x97, 0x04, 0xff, 0x62, 0x4a, 0x17,
  10821. 0xc0, 0x09, 0x2a, 0x2c, 0x23, 0x4b, 0xc3, 0xb6, 0x6d, 0xed, 0xdb, 0x1a,
  10822. 0x6f, 0x56, 0x2b, 0x78, 0x92, 0x3a, 0x5c, 0x7f, 0xb2, 0x63, 0xd3, 0xd5,
  10823. 0x1a, 0xbe, 0xc2, 0x34, 0xc8, 0xad, 0x36, 0xb7, 0x12, 0xb8, 0xe1, 0xb7,
  10824. 0x52, 0x7f, 0x16, 0x84, 0x2c, 0x47, 0x7e, 0xf2, 0xa5, 0x36, 0x2e, 0xad,
  10825. 0xe7, 0xbb, 0xc0, 0x6f, 0x27, 0x8e, 0x41, 0x08, 0x75, 0xe5, 0xff, 0xde,
  10826. 0x08, 0x9f, 0x8c, 0x91, 0xba, 0xc9, 0x9d, 0x9f, 0x27, 0x90, 0x50, 0x44,
  10827. 0x24, 0xe7, 0x3d, 0x6f
  10828. };
  10829. word64 s3 = 0x000000ffffffffff;
  10830. #endif
  10831. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10832. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10833. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10834. #endif
  10835. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  10836. if (ret != 0)
  10837. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10838. else
  10839. aes_inited = 1;
  10840. XMEMSET(buf, 0, sizeof(buf));
  10841. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10842. if (ret != 0)
  10843. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10844. ret = wc_AesXtsEncryptSector(aes, buf, p1, sizeof(p1), s1);
  10845. #if defined(WOLFSSL_ASYNC_CRYPT)
  10846. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10847. #endif
  10848. if (ret != 0)
  10849. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10850. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  10851. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10852. /* decrypt test */
  10853. XMEMSET(buf, 0, sizeof(buf));
  10854. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10855. if (ret != 0)
  10856. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10857. ret = wc_AesXtsDecryptSector(aes, buf, c1, sizeof(c1), s1);
  10858. #if defined(WOLFSSL_ASYNC_CRYPT)
  10859. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10860. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10861. #else
  10862. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10863. #endif
  10864. #endif
  10865. if (ret != 0)
  10866. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10867. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  10868. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10869. /* 256 bit key tests */
  10870. XMEMSET(buf, 0, sizeof(buf));
  10871. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_ENCRYPTION);
  10872. if (ret != 0)
  10873. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10874. ret = wc_AesXtsEncryptSector(aes, buf, p2, sizeof(p2), s2);
  10875. #if defined(WOLFSSL_ASYNC_CRYPT)
  10876. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10877. #endif
  10878. if (ret != 0)
  10879. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10880. if (XMEMCMP(c2, buf, sizeof(c2)))
  10881. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10882. /* decrypt test */
  10883. XMEMSET(buf, 0, sizeof(buf));
  10884. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_DECRYPTION);
  10885. if (ret != 0)
  10886. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10887. ret = wc_AesXtsDecryptSector(aes, buf, c2, sizeof(c2), s2);
  10888. #if defined(WOLFSSL_ASYNC_CRYPT)
  10889. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10890. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10891. #else
  10892. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10893. #endif
  10894. #endif
  10895. if (ret != 0)
  10896. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10897. if (XMEMCMP(p2, buf, sizeof(p2)))
  10898. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10899. #if !defined(BENCH_EMBEDDED) && \
  10900. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  10901. /* encrypt consecutive sectors test */
  10902. XMEMSET(data, 0, sizeof(buf));
  10903. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_ENCRYPTION);
  10904. if (ret != 0)
  10905. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10906. ret = wc_AesXtsEncryptConsecutiveSectors(aes, data, p3,
  10907. sizeof(p3), s3, sectorSz);
  10908. #if defined(WOLFSSL_ASYNC_CRYPT)
  10909. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10910. #endif
  10911. if (ret != 0)
  10912. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10913. if (XMEMCMP(c3, data, sizeof(c3)))
  10914. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10915. /* decrypt consecutive sectors test */
  10916. XMEMSET(data, 0, sizeof(buf));
  10917. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_DECRYPTION);
  10918. if (ret != 0)
  10919. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10920. ret = wc_AesXtsDecryptConsecutiveSectors(aes, data, c3,
  10921. sizeof(c3), s3, sectorSz);
  10922. #if defined(WOLFSSL_ASYNC_CRYPT)
  10923. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10924. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10925. #else
  10926. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10927. #endif
  10928. #endif
  10929. if (ret != 0)
  10930. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10931. if (XMEMCMP(p3, data, sizeof(p3)))
  10932. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10933. #endif /* !BENCH_EMBEDDED && (!HAVE_FIPS || FIPS_VERSION_GE(5, 3)) */
  10934. out:
  10935. if (aes_inited)
  10936. wc_AesXtsFree(aes);
  10937. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10938. if (aes)
  10939. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  10940. #endif
  10941. return ret;
  10942. }
  10943. #endif /* WOLFSSL_AES_128 && WOLFSSL_AES_256 */
  10944. #ifdef WOLFSSL_AES_128
  10945. /* testing of bad arguments */
  10946. static wc_test_ret_t aes_xts_args_test(void)
  10947. {
  10948. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10949. XtsAes *aes = NULL;
  10950. #else
  10951. XtsAes aes[1];
  10952. #endif
  10953. int aes_inited = 0;
  10954. wc_test_ret_t ret;
  10955. unsigned char buf[AES_BLOCK_SIZE * 2];
  10956. /* 128 key tests */
  10957. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  10958. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  10959. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  10960. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  10961. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  10962. };
  10963. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  10964. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  10965. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  10966. };
  10967. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  10968. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  10969. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  10970. };
  10971. word64 s1 = 141;
  10972. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10973. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10974. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10975. #endif
  10976. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  10977. if (ret != 0)
  10978. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10979. else
  10980. aes_inited = 1;
  10981. if (wc_AesXtsSetKeyNoInit(NULL, k1, sizeof(k1), AES_ENCRYPTION) == 0)
  10982. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10983. if (wc_AesXtsSetKeyNoInit(aes, NULL, sizeof(k1), AES_ENCRYPTION) == 0)
  10984. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10985. /* encryption operations */
  10986. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10987. if (ret != 0)
  10988. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10989. ret = wc_AesXtsEncryptSector(NULL, buf, p1, sizeof(p1), s1);
  10990. #if defined(WOLFSSL_ASYNC_CRYPT)
  10991. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10992. #endif
  10993. if (ret == 0)
  10994. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10995. ret = wc_AesXtsEncryptSector(aes, NULL, p1, sizeof(p1), s1);
  10996. #if defined(WOLFSSL_ASYNC_CRYPT)
  10997. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10998. #endif
  10999. if (ret == 0)
  11000. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11001. /* decryption operations */
  11002. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  11003. if (ret != 0)
  11004. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11005. ret = wc_AesXtsDecryptSector(NULL, buf, c1, sizeof(c1), s1);
  11006. #if defined(WOLFSSL_ASYNC_CRYPT)
  11007. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  11008. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  11009. #else
  11010. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  11011. #endif
  11012. #endif
  11013. if (ret == 0)
  11014. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11015. ret = wc_AesXtsDecryptSector(aes, NULL, c1, sizeof(c1), s1);
  11016. #if defined(WOLFSSL_ASYNC_CRYPT)
  11017. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  11018. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  11019. #else
  11020. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  11021. #endif
  11022. #endif
  11023. if (ret == 0)
  11024. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11025. ret = 0;
  11026. out:
  11027. if (aes_inited)
  11028. wc_AesXtsFree(aes);
  11029. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11030. if (aes)
  11031. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  11032. #endif
  11033. return ret;
  11034. }
  11035. #endif /* WOLFSSL_AES_128 */
  11036. #endif /* WOLFSSL_AES_XTS && (!HAVE_FIPS || FIPS_VERSION_GE(5,3)) */
  11037. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  11038. static wc_test_ret_t aes_cbc_test(void)
  11039. {
  11040. byte cipher[AES_BLOCK_SIZE];
  11041. byte plain[AES_BLOCK_SIZE];
  11042. wc_test_ret_t ret;
  11043. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  11044. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  11045. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  11046. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  11047. };
  11048. byte key[] = "0123456789abcdef "; /* align */
  11049. byte iv[] = "1234567890abcdef "; /* align */
  11050. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  11051. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  11052. /* Parameter Validation testing. */
  11053. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key, 17, NULL);
  11054. if (ret != BAD_FUNC_ARG)
  11055. return WC_TEST_RET_ENC_EC(ret);
  11056. #ifdef HAVE_AES_DECRYPT
  11057. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key, 17, NULL);
  11058. if (ret != BAD_FUNC_ARG)
  11059. return WC_TEST_RET_ENC_EC(ret);
  11060. #endif
  11061. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key,
  11062. AES_BLOCK_SIZE, iv);
  11063. if (ret != 0)
  11064. return WC_TEST_RET_ENC_EC(ret);
  11065. #ifdef HAVE_AES_DECRYPT
  11066. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key,
  11067. AES_BLOCK_SIZE, iv);
  11068. if (ret != 0)
  11069. return WC_TEST_RET_ENC_EC(ret);
  11070. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE) != 0)
  11071. return WC_TEST_RET_ENC_NC;
  11072. #endif /* HAVE_AES_DECRYPT */
  11073. (void)plain;
  11074. return 0;
  11075. }
  11076. #endif
  11077. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11078. static wc_test_ret_t aesecb_test(void)
  11079. {
  11080. wc_test_ret_t ret = 0;
  11081. #if defined(WOLFSSL_AES_256)
  11082. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11083. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  11084. #else
  11085. Aes enc[1];
  11086. #endif
  11087. int enc_inited = 0;
  11088. byte cipher[AES_BLOCK_SIZE * 4];
  11089. #ifdef HAVE_AES_DECRYPT
  11090. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11091. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  11092. #else
  11093. Aes dec[1];
  11094. #endif
  11095. int dec_inited = 0;
  11096. byte plain[AES_BLOCK_SIZE * 4];
  11097. #endif /* HAVE_AES_DECRYPT */
  11098. {
  11099. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  11100. {
  11101. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  11102. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  11103. };
  11104. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  11105. {
  11106. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  11107. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  11108. };
  11109. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  11110. {
  11111. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  11112. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  11113. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  11114. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  11115. };
  11116. ret = wc_AesInit(enc, HEAP_HINT, devId);
  11117. if (ret != 0)
  11118. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11119. enc_inited = 1;
  11120. #if defined(HAVE_AES_DECRYPT)
  11121. ret = wc_AesInit(dec, HEAP_HINT, devId);
  11122. if (ret != 0)
  11123. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11124. dec_inited = 1;
  11125. #endif
  11126. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  11127. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  11128. if (ret != 0)
  11129. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11130. if (wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE) != 0)
  11131. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11132. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  11133. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11134. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  11135. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  11136. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11137. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  11138. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11139. if (ret != 0)
  11140. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11141. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11142. ret = wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE);
  11143. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11144. if (ret != 0)
  11145. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11146. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  11147. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11148. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  11149. ret = wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE);
  11150. if (ret != 0)
  11151. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11152. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  11153. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11154. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  11155. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  11156. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11157. ret = wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE);
  11158. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11159. if (ret != 0)
  11160. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11161. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  11162. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11163. #endif
  11164. #ifdef HAVE_AES_DECRYPT
  11165. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  11166. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  11167. if (ret != 0)
  11168. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11169. if (wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE) != 0)
  11170. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11171. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  11172. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11173. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  11174. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  11175. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11176. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  11177. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11178. if (ret != 0)
  11179. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11180. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11181. ret = wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  11182. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11183. if (ret != 0)
  11184. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11185. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  11186. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11187. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  11188. ret = wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  11189. if (ret != 0)
  11190. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11191. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  11192. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11193. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  11194. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  11195. if (ret != 0)
  11196. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11197. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11198. ret = wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  11199. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11200. if (ret != 0)
  11201. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11202. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  11203. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11204. #endif
  11205. #endif /* HAVE_AES_DECRYPT */
  11206. }
  11207. out:
  11208. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11209. if (enc) {
  11210. if (enc_inited)
  11211. wc_AesFree(enc);
  11212. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  11213. }
  11214. #ifdef HAVE_AES_DECRYPT
  11215. if (dec) {
  11216. if (dec_inited)
  11217. wc_AesFree(dec);
  11218. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  11219. }
  11220. #endif
  11221. #else
  11222. if (enc_inited)
  11223. wc_AesFree(enc);
  11224. #ifdef HAVE_AES_DECRYPT
  11225. if (dec_inited)
  11226. wc_AesFree(dec);
  11227. #endif
  11228. #endif
  11229. #endif /* WOLFSSL_AES_256 */
  11230. return ret;
  11231. }
  11232. #endif /* HAVE_AES_ECB */
  11233. #ifdef WOLFSSL_AES_COUNTER
  11234. static wc_test_ret_t aesctr_test(Aes* enc, Aes* dec, byte* cipher, byte* plain)
  11235. {
  11236. wc_test_ret_t ret = 0;
  11237. /* test vectors from "Recommendation for Block Cipher Modes of
  11238. * Operation" NIST Special Publication 800-38A */
  11239. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  11240. {
  11241. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  11242. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  11243. };
  11244. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  11245. {
  11246. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  11247. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  11248. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  11249. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  11250. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  11251. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  11252. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  11253. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  11254. };
  11255. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11256. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32[] =
  11257. {
  11258. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,
  11259. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  11260. };
  11261. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32_2[] =
  11262. {
  11263. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  11264. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xfe
  11265. };
  11266. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64[] =
  11267. {
  11268. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  11269. 0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  11270. };
  11271. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64_2[] =
  11272. {
  11273. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0,
  11274. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  11275. };
  11276. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96[] =
  11277. {
  11278. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  11279. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff
  11280. };
  11281. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96_2[] =
  11282. {
  11283. 0xff,0xff,0xff,0xf0,0xff,0xff,0xff,0xff,
  11284. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  11285. };
  11286. #endif
  11287. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128[] =
  11288. {
  11289. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  11290. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  11291. };
  11292. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11293. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128_2[] =
  11294. {
  11295. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  11296. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  11297. };
  11298. #endif
  11299. #ifdef WOLFSSL_AES_128
  11300. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  11301. {
  11302. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  11303. 0xc2
  11304. };
  11305. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Key[] =
  11306. {
  11307. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  11308. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  11309. };
  11310. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Cipher[] =
  11311. {
  11312. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  11313. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  11314. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  11315. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  11316. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  11317. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  11318. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  11319. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  11320. };
  11321. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11322. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32Cipher[] =
  11323. {
  11324. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  11325. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  11326. 0x7e,0xad,0xea,0x5c,0xd1
  11327. };
  11328. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32CipherLong[] =
  11329. {
  11330. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  11331. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  11332. 0x7e,0xad,0xea,0x5c,0xd1,0xb7,0x98,0xf0,
  11333. 0x22,0x20,0xfe,0x67,0xb0,0x02,0x23,0x50
  11334. };
  11335. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32_2CipherLong[] =
  11336. {
  11337. 0x6e,0xa1,0x27,0x4d,0xea,0x20,0x5f,0x39,
  11338. 0x68,0xc8,0xb6,0x78,0xde,0xfc,0x53,0x5c,
  11339. 0x90,0xc8,0xf6,0xc6,0xfa,0xe0,0x7b,0x09,
  11340. 0x7c,0xf8,0x9c,0x6a,0x5a,0xa5,0x17,0x7f,
  11341. 0x03,0x92,0x5f,0x4e,0x85,0xea,0x26,0xc9,
  11342. 0x5a,0xc2,0x74,0xe2,0xbf,0xe4,0x1b,0xd4
  11343. };
  11344. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64Cipher[] =
  11345. {
  11346. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  11347. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  11348. 0xae,0x74,0x94,0x72,0x8e
  11349. };
  11350. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64CipherLong[] =
  11351. {
  11352. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  11353. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  11354. 0xae,0x74,0x94,0x72,0x8e,0xd0,0x71,0xc0,
  11355. 0x89,0x8a,0xa1,0xb0,0x29,0xa0,0x10,0x9e
  11356. };
  11357. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64_2CipherLong[] =
  11358. {
  11359. 0x3f,0xe7,0xd5,0xf3,0xfa,0x09,0xfe,0x40,
  11360. 0xa6,0xa1,0x32,0x8b,0x57,0x12,0xb9,0xfa,
  11361. 0xf2,0x2d,0xe4,0x3c,0x66,0x1d,0x0a,0x8e,
  11362. 0x46,0xf8,0x2e,0x33,0xce,0x8d,0x4e,0x3b,
  11363. 0x17,0x67,0x9e,0x9f,0x76,0x9e,0xc2,0x99,
  11364. 0xd5,0xd4,0x71,0xed,0xb4,0x33,0xb2,0xcd
  11365. };
  11366. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96Cipher[] =
  11367. {
  11368. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  11369. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  11370. 0x9d,0x77,0xc9,0x5f,0x38
  11371. };
  11372. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96CipherLong[] =
  11373. {
  11374. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  11375. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  11376. 0x9d,0x77,0xc9,0x5f,0x38,0xb5,0x6e,0x44,
  11377. 0x21,0x8e,0xda,0x57,0xe0,0x41,0xc7,0x6a
  11378. };
  11379. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96_2CipherLong[] =
  11380. {
  11381. 0xc8,0x81,0x1a,0xbe,0xc7,0x5b,0x93,0x6f,
  11382. 0xe6,0x52,0xe4,0xb1,0x2d,0x1c,0x39,0xbc,
  11383. 0xeb,0x82,0x27,0x0a,0x7e,0xa5,0x0e,0x2d,
  11384. 0x32,0xda,0xbe,0x10,0x7a,0x10,0xcc,0xd3,
  11385. 0x6f,0xc6,0x83,0x28,0x05,0x57,0x8a,0x24,
  11386. 0x44,0x76,0x17,0x81,0xb9,0x5c,0x94,0x81
  11387. };
  11388. #endif
  11389. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128Cipher[] =
  11390. {
  11391. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  11392. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  11393. 0xd3,0xda,0xe1,0x5b,0x04
  11394. };
  11395. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128CipherLong[] =
  11396. {
  11397. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  11398. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  11399. 0xd3,0xda,0xe1,0x5b,0x04,0xbb,0x35,0x2f,
  11400. 0xa0,0xf5,0x9f,0xeb,0xfc,0xb4,0xda,0x3e
  11401. };
  11402. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11403. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128_2CipherLong[] =
  11404. {
  11405. 0xba,0x76,0xaa,0x54,0xd5,0xb5,0x60,0x67,
  11406. 0xc1,0xa7,0x90,0x3b,0x3f,0xdd,0xfa,0x89,
  11407. 0x24,0xdf,0x0c,0x56,0x5c,0xf4,0x2a,0x68,
  11408. 0x97,0x87,0x13,0xb6,0x7a,0xd1,0x24,0xfd,
  11409. 0x4d,0x3f,0x77,0x4a,0xb9,0xe4,0x7d,0xa2,
  11410. 0xdb,0xb9,0x31,0x5e,0xa3,0x11,0x06,0x80
  11411. };
  11412. #endif
  11413. #endif /* WOLFSSL_AES_128 */
  11414. #ifdef WOLFSSL_AES_192
  11415. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  11416. {
  11417. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  11418. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  11419. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  11420. };
  11421. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  11422. {
  11423. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  11424. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b,
  11425. 0x09,0x03,0x39,0xec,0x0a,0xa6,0xfa,0xef,
  11426. 0xd5,0xcc,0xc2,0xc6,0xf4,0xce,0x8e,0x94,
  11427. 0x1e,0x36,0xb2,0x6b,0xd1,0xeb,0xc6,0x70,
  11428. 0xd1,0xbd,0x1d,0x66,0x56,0x20,0xab,0xf7,
  11429. 0x4f,0x78,0xa7,0xf6,0xd2,0x98,0x09,0x58,
  11430. 0x5a,0x97,0xda,0xec,0x58,0xc6,0xb0,0x50
  11431. };
  11432. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11433. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32Cipher[] =
  11434. {
  11435. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  11436. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  11437. 0x0c,0x33,0x97,0x06,0xc0
  11438. };
  11439. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32CipherLong[] =
  11440. {
  11441. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  11442. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  11443. 0x0c,0x33,0x97,0x06,0xc0,0xbe,0x83,0x87,
  11444. 0xdd,0xd3,0xff,0xd8,0xe4,0x6a,0x5b,0x84
  11445. };
  11446. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32_2CipherLong[] =
  11447. {
  11448. 0xf5,0x00,0xa2,0x91,0x54,0xa3,0x76,0xa2,
  11449. 0xdd,0xad,0x16,0x89,0xe5,0xf0,0x1d,0x40,
  11450. 0x84,0xcd,0x74,0x84,0xcb,0x8b,0x9e,0x29,
  11451. 0xa9,0x8a,0x12,0x65,0xa0,0x79,0x5e,0xce,
  11452. 0xd9,0x50,0x65,0x21,0x86,0xb0,0x85,0x0d,
  11453. 0x98,0x2d,0x9a,0x5a,0x11,0xbe,0xa5,0x7f
  11454. };
  11455. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64Cipher[] =
  11456. {
  11457. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  11458. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  11459. 0x3d,0xf7,0xb4,0xfd,0x8c
  11460. };
  11461. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64CipherLong[] =
  11462. {
  11463. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  11464. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  11465. 0x3d,0xf7,0xb4,0xfd,0x8c,0x55,0x47,0x10,
  11466. 0xd5,0x91,0x35,0xbe,0xd8,0x0d,0xa5,0x9e
  11467. };
  11468. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64_2CipherLong[] =
  11469. {
  11470. 0x59,0xf1,0xed,0x70,0x62,0x42,0xa8,0x06,
  11471. 0x07,0x36,0xe1,0xc5,0x04,0x79,0xc3,0x9b,
  11472. 0xd1,0x14,0x5c,0xcc,0x6f,0x81,0x5f,0x2f,
  11473. 0xa0,0xde,0xcf,0x61,0x55,0x18,0x7a,0xac,
  11474. 0xb0,0x59,0x37,0x90,0x53,0xb3,0x00,0x88,
  11475. 0xb4,0x49,0x90,0x7b,0x96,0xcd,0xcc,0xc3
  11476. };
  11477. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96Cipher[] =
  11478. {
  11479. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  11480. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  11481. 0x47,0xb5,0xf3,0x30,0x3b
  11482. };
  11483. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96CipherLong[] =
  11484. {
  11485. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  11486. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  11487. 0x47,0xb5,0xf3,0x30,0x3b,0xef,0xcd,0x80,
  11488. 0xe3,0x61,0x34,0xef,0x4e,0x1b,0x79,0xc1
  11489. };
  11490. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96_2CipherLong[] =
  11491. {
  11492. 0x3c,0xb2,0xff,0xc0,0x24,0xe1,0xf5,0xc4,
  11493. 0x0f,0xd1,0x0a,0x1b,0xbe,0x1f,0x23,0xa1,
  11494. 0x8e,0xbf,0x2b,0x96,0xb6,0x37,0xc8,0x25,
  11495. 0x06,0x90,0xe2,0xca,0x71,0x24,0x52,0x95,
  11496. 0xaa,0x8c,0x80,0xdf,0xb7,0xd7,0x30,0xb0,
  11497. 0xcc,0x06,0x4f,0x28,0xa2,0x74,0x27,0xf8
  11498. };
  11499. #endif
  11500. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128Cipher[] =
  11501. {
  11502. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  11503. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  11504. 0x8c,0x68,0xa7,0xd9,0x57
  11505. };
  11506. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128CipherLong[] =
  11507. {
  11508. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  11509. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  11510. 0x8c,0x68,0xa7,0xd9,0x57,0xab,0x09,0x0f,
  11511. 0x01,0xc4,0x4e,0x62,0xaf,0xc2,0xdf,0x1a
  11512. };
  11513. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11514. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128_2CipherLong[] =
  11515. {
  11516. 0x88,0x0a,0x26,0x4e,0xa8,0x26,0x21,0xe0,
  11517. 0xfc,0xbc,0x63,0xdc,0xd9,0x60,0x52,0xb2,
  11518. 0x99,0x2f,0xbb,0x1e,0x00,0xf5,0x9f,0x6d,
  11519. 0xab,0x48,0x0f,0xc6,0x37,0x12,0x56,0xe3,
  11520. 0x12,0x8d,0x31,0xc8,0xea,0xf4,0x41,0x82,
  11521. 0x7a,0x88,0xe0,0xd7,0xf0,0x67,0x03,0xa4
  11522. };
  11523. #endif
  11524. #endif
  11525. #ifdef WOLFSSL_AES_256
  11526. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  11527. {
  11528. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  11529. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  11530. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  11531. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  11532. };
  11533. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  11534. {
  11535. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  11536. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,
  11537. 0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,
  11538. 0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5,
  11539. 0x2b,0x09,0x30,0xda,0xa2,0x3d,0xe9,0x4c,
  11540. 0xe8,0x70,0x17,0xba,0x2d,0x84,0x98,0x8d,
  11541. 0xdf,0xc9,0xc5,0x8d,0xb6,0x7a,0xad,0xa6,
  11542. 0x13,0xc2,0xdd,0x08,0x45,0x79,0x41,0xa6
  11543. };
  11544. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11545. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32Cipher[] =
  11546. {
  11547. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  11548. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  11549. 0xa2,0x13,0x47,0x74,0xda
  11550. };
  11551. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32CipherLong[] =
  11552. {
  11553. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  11554. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  11555. 0xa2,0x13,0x47,0x74,0xda,0x89,0x77,0x40,
  11556. 0x28,0x9c,0xe8,0x19,0x26,0x32,0xd8,0x1f
  11557. };
  11558. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32_2CipherLong[] =
  11559. {
  11560. 0xf6,0xd9,0x22,0xc6,0x80,0x29,0xaf,0x14,
  11561. 0x54,0x6c,0x0a,0xce,0x42,0xea,0x3c,0xa1,
  11562. 0x7c,0xeb,0x36,0x0d,0x8e,0xd7,0x8c,0x59,
  11563. 0xa8,0x09,0x9f,0x9e,0xba,0x5b,0x95,0xfa,
  11564. 0x26,0x8c,0x37,0x59,0xf8,0xae,0x8e,0xaa,
  11565. 0x4d,0xe4,0x1c,0xfe,0x51,0xc7,0xb7,0xcc
  11566. };
  11567. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64Cipher[] =
  11568. {
  11569. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  11570. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  11571. 0xd4,0x94,0x4c,0xcd,0x4d
  11572. };
  11573. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64CipherLong[] =
  11574. {
  11575. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  11576. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  11577. 0xd4,0x94,0x4c,0xcd,0x4d,0x47,0x5a,0x92,
  11578. 0x26,0x49,0x81,0x7a,0xda,0x36,0x27,0x01
  11579. };
  11580. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64_2CipherLong[] =
  11581. {
  11582. 0xf7,0x9c,0xbf,0xf6,0xa2,0xaa,0x8a,0x0a,
  11583. 0x63,0x8a,0x20,0x2f,0x12,0xf1,0x8e,0x49,
  11584. 0x30,0xc0,0x8d,0x5c,0x5f,0x8b,0xbc,0x16,
  11585. 0xdd,0x71,0xee,0x13,0x14,0x7b,0xe1,0x25,
  11586. 0xcb,0x87,0x8a,0xc6,0xdc,0x1d,0x54,0x7a,
  11587. 0xe1,0xe4,0x6f,0x0d,0x95,0x1b,0xd1,0x8b
  11588. };
  11589. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96Cipher[] =
  11590. {
  11591. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  11592. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  11593. 0xb8,0x69,0xa1,0x48,0x45
  11594. };
  11595. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96CipherLong[] =
  11596. {
  11597. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  11598. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  11599. 0xb8,0x69,0xa1,0x48,0x45,0xf1,0xc6,0x27,
  11600. 0x36,0xa8,0xb2,0x4b,0x0e,0x62,0x6b,0x72
  11601. };
  11602. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96_2CipherLong[] =
  11603. {
  11604. 0xd5,0x56,0x73,0xaa,0xb8,0xe4,0x06,0xf6,
  11605. 0x83,0x45,0x3a,0xb4,0xb9,0x63,0xec,0xad,
  11606. 0x73,0xc5,0xab,0x78,0xb1,0x21,0xab,0xef,
  11607. 0x69,0x15,0xb7,0x0c,0xe9,0xb4,0x3a,0xe7,
  11608. 0xbc,0xc4,0x22,0xbd,0x93,0xba,0x52,0xe0,
  11609. 0x91,0x2f,0x5e,0x8d,0x6d,0x59,0xf7,0xc2
  11610. };
  11611. #endif
  11612. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128Cipher[] =
  11613. {
  11614. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  11615. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  11616. 0x4b,0x45,0x7c,0xd6,0x8a
  11617. };
  11618. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128CipherLong[] =
  11619. {
  11620. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  11621. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  11622. 0x4b,0x45,0x7c,0xd6,0x8a,0xcc,0xda,0x4a,
  11623. 0x89,0xfa,0x23,0x6c,0x06,0xbf,0x26,0x05
  11624. };
  11625. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11626. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128_2CipherLong[] =
  11627. {
  11628. 0x24,0x5c,0x09,0xa0,0x3b,0x1a,0x5a,0x94,
  11629. 0x2b,0x93,0x56,0x13,0x48,0xa0,0x21,0xce,
  11630. 0x95,0x11,0xa3,0x76,0xd6,0x59,0x88,0x42,
  11631. 0x04,0x71,0x69,0x62,0x28,0xb2,0xee,0x9d,
  11632. 0xd5,0xa0,0xea,0xc7,0x37,0x93,0x92,0xc7,
  11633. 0xf2,0xb6,0x8d,0xd9,0x59,0x1a,0xfa,0xbb
  11634. };
  11635. #endif
  11636. #endif
  11637. int i;
  11638. struct {
  11639. const byte* key;
  11640. int keySz;
  11641. const byte* iv;
  11642. const byte* plain;
  11643. int len;
  11644. const byte* cipher;
  11645. } testVec[] = {
  11646. #ifdef WOLFSSL_AES_128
  11647. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  11648. ctrPlain, (int)sizeof(ctrPlain), ctr128Cipher },
  11649. /* let's try with just 9 bytes, non block size test */
  11650. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  11651. ctrPlain, (int)sizeof(oddCipher), ctr128Cipher },
  11652. /* and an additional 9 bytes to reuse tmp left buffer */
  11653. { NULL, 0, NULL, ctrPlain, (int)sizeof(oddCipher), oddCipher },
  11654. /* Counter wrapping */
  11655. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  11656. ctrPlain, (int)sizeof(ctr128Wrap128Cipher), ctr128Wrap128Cipher },
  11657. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  11658. ctrPlain, (int)sizeof(ctr128Wrap128CipherLong),
  11659. ctr128Wrap128CipherLong },
  11660. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11661. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128_2,
  11662. ctrPlain, (int)sizeof(ctr128Wrap128_2CipherLong),
  11663. ctr128Wrap128_2CipherLong },
  11664. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  11665. ctrPlain, (int)sizeof(ctr128Wrap96Cipher), ctr128Wrap96Cipher },
  11666. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  11667. ctrPlain, (int)sizeof(ctr128Wrap96CipherLong),
  11668. ctr128Wrap96CipherLong },
  11669. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96_2,
  11670. ctrPlain, (int)sizeof(ctr128Wrap96_2CipherLong),
  11671. ctr128Wrap96_2CipherLong },
  11672. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  11673. ctrPlain, (int)sizeof(ctr128Wrap64Cipher), ctr128Wrap64Cipher },
  11674. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  11675. ctrPlain, (int)sizeof(ctr128Wrap64CipherLong),
  11676. ctr128Wrap64CipherLong },
  11677. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64_2,
  11678. ctrPlain, (int)sizeof(ctr128Wrap64_2CipherLong),
  11679. ctr128Wrap64_2CipherLong },
  11680. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  11681. ctrPlain, (int)sizeof(ctr128Wrap32Cipher), ctr128Wrap32Cipher },
  11682. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  11683. ctrPlain, (int)sizeof(ctr128Wrap32CipherLong),
  11684. ctr128Wrap32CipherLong },
  11685. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32_2,
  11686. ctrPlain, (int)sizeof(ctr128Wrap32_2CipherLong),
  11687. ctr128Wrap32_2CipherLong },
  11688. #endif
  11689. #endif
  11690. #ifdef WOLFSSL_AES_192
  11691. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  11692. ctrPlain, (int)sizeof(ctrPlain), ctr192Cipher },
  11693. /* let's try with just 9 bytes, non block size test */
  11694. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  11695. ctrPlain, (int)sizeof(oddCipher), ctr192Cipher },
  11696. /* Counter wrapping */
  11697. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  11698. ctrPlain, (int)sizeof(ctr192Wrap128Cipher), ctr192Wrap128Cipher },
  11699. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  11700. ctrPlain, (int)sizeof(ctr192Wrap128CipherLong),
  11701. ctr192Wrap128CipherLong },
  11702. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11703. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128_2,
  11704. ctrPlain, (int)sizeof(ctr192Wrap128_2CipherLong),
  11705. ctr192Wrap128_2CipherLong },
  11706. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  11707. ctrPlain, (int)sizeof(ctr192Wrap96Cipher), ctr192Wrap96Cipher },
  11708. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  11709. ctrPlain, (int)sizeof(ctr192Wrap96CipherLong),
  11710. ctr192Wrap96CipherLong },
  11711. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96_2,
  11712. ctrPlain, (int)sizeof(ctr192Wrap96_2CipherLong),
  11713. ctr192Wrap96_2CipherLong },
  11714. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  11715. ctrPlain, (int)sizeof(ctr192Wrap64Cipher), ctr192Wrap64Cipher },
  11716. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  11717. ctrPlain, (int)sizeof(ctr192Wrap64CipherLong),
  11718. ctr192Wrap64CipherLong },
  11719. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64_2,
  11720. ctrPlain, (int)sizeof(ctr192Wrap64_2CipherLong),
  11721. ctr192Wrap64_2CipherLong },
  11722. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  11723. ctrPlain, (int)sizeof(ctr192Wrap32Cipher), ctr192Wrap32Cipher },
  11724. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  11725. ctrPlain, (int)sizeof(ctr192Wrap32CipherLong),
  11726. ctr192Wrap32CipherLong },
  11727. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32_2,
  11728. ctrPlain, (int)sizeof(ctr192Wrap32_2CipherLong),
  11729. ctr192Wrap32_2CipherLong },
  11730. #endif
  11731. #endif
  11732. #ifdef WOLFSSL_AES_256
  11733. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  11734. ctrPlain, (int)sizeof(ctrPlain), ctr256Cipher },
  11735. /* let's try with just 9 bytes, non block size test */
  11736. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  11737. ctrPlain, (int)sizeof(oddCipher), ctr256Cipher },
  11738. /* Counter wrapping */
  11739. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  11740. ctrPlain, (int)sizeof(ctr256Wrap128Cipher), ctr256Wrap128Cipher },
  11741. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  11742. ctrPlain, (int)sizeof(ctr256Wrap128CipherLong),
  11743. ctr256Wrap128CipherLong },
  11744. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11745. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128_2,
  11746. ctrPlain, (int)sizeof(ctr256Wrap128_2CipherLong),
  11747. ctr256Wrap128_2CipherLong },
  11748. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  11749. ctrPlain, (int)sizeof(ctr256Wrap96Cipher), ctr256Wrap96Cipher },
  11750. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  11751. ctrPlain, (int)sizeof(ctr256Wrap96CipherLong),
  11752. ctr256Wrap96CipherLong },
  11753. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96_2,
  11754. ctrPlain, (int)sizeof(ctr256Wrap96_2CipherLong),
  11755. ctr256Wrap96_2CipherLong },
  11756. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  11757. ctrPlain, (int)sizeof(ctr256Wrap64Cipher), ctr256Wrap64Cipher },
  11758. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  11759. ctrPlain, (int)sizeof(ctr256Wrap64CipherLong),
  11760. ctr256Wrap64CipherLong },
  11761. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64_2,
  11762. ctrPlain, (int)sizeof(ctr256Wrap64_2CipherLong),
  11763. ctr256Wrap64_2CipherLong },
  11764. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  11765. ctrPlain, (int)sizeof(ctr256Wrap32Cipher), ctr256Wrap32Cipher },
  11766. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  11767. ctrPlain, (int)sizeof(ctr256Wrap32CipherLong),
  11768. ctr256Wrap32CipherLong },
  11769. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32_2,
  11770. ctrPlain, (int)sizeof(ctr256Wrap32_2CipherLong),
  11771. ctr256Wrap32_2CipherLong },
  11772. #endif
  11773. #endif
  11774. };
  11775. #define AES_CTR_TEST_LEN (int)(sizeof(testVec) / sizeof(*testVec))
  11776. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  11777. if (testVec[i].key != NULL) {
  11778. ret = wc_AesSetKeyDirect(enc, testVec[i].key, (word32)testVec[i].keySz,
  11779. testVec[i].iv, AES_ENCRYPTION);
  11780. if (ret != 0) {
  11781. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11782. }
  11783. /* Ctr only uses encrypt, even on key setup */
  11784. ret = wc_AesSetKeyDirect(dec, testVec[i].key, (word32)testVec[i].keySz,
  11785. testVec[i].iv, AES_ENCRYPTION);
  11786. if (ret != 0) {
  11787. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11788. }
  11789. }
  11790. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, (word32)testVec[i].len);
  11791. if (ret != 0) {
  11792. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11793. }
  11794. ret = wc_AesCtrEncrypt(dec, plain, cipher, (word32)testVec[i].len);
  11795. if (ret != 0) {
  11796. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11797. }
  11798. if (XMEMCMP(plain, ctrPlain, (size_t)testVec[i].len)) {
  11799. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11800. }
  11801. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  11802. if (XMEMCMP(cipher, testVec[i].cipher, (size_t)testVec[i].len)) {
  11803. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11804. }
  11805. #endif
  11806. }
  11807. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  11808. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  11809. if (testVec[i].key != NULL) {
  11810. ret = wc_AesSetKeyDirect(enc, testVec[i].key, testVec[i].keySz,
  11811. testVec[i].iv, AES_ENCRYPTION);
  11812. if (ret != 0) {
  11813. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11814. }
  11815. /* Ctr only uses encrypt, even on key setup */
  11816. ret = wc_AesSetKeyDirect(dec, testVec[i].key, testVec[i].keySz,
  11817. testVec[i].iv, AES_ENCRYPTION);
  11818. if (ret != 0) {
  11819. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11820. }
  11821. }
  11822. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11823. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, testVec[i].len);
  11824. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11825. if (ret != 0) {
  11826. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11827. }
  11828. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11829. ret = wc_AesCtrEncrypt(dec, plain, cipher, testVec[i].len);
  11830. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11831. if (ret != 0) {
  11832. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11833. }
  11834. if (XMEMCMP(plain, ctrPlain, testVec[i].len)) {
  11835. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11836. }
  11837. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  11838. if (XMEMCMP(cipher, testVec[i].cipher, testVec[i].len)) {
  11839. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11840. }
  11841. #endif
  11842. }
  11843. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  11844. if (testVec[i].key != NULL) {
  11845. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11846. ret = wc_AesSetKeyDirect(enc, testVec[i].key, testVec[i].keySz,
  11847. testVec[i].iv, AES_ENCRYPTION);
  11848. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11849. if (ret != 0) {
  11850. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11851. }
  11852. /* Ctr only uses encrypt, even on key setup */
  11853. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11854. ret = wc_AesSetKeyDirect(dec, testVec[i].key, testVec[i].keySz,
  11855. testVec[i].iv, AES_ENCRYPTION);
  11856. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11857. if (ret != 0) {
  11858. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11859. }
  11860. }
  11861. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, testVec[i].len);
  11862. if (ret != 0) {
  11863. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11864. }
  11865. ret = wc_AesCtrEncrypt(dec, plain, cipher, testVec[i].len);
  11866. if (ret != 0) {
  11867. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11868. }
  11869. if (XMEMCMP(plain, ctrPlain, testVec[i].len)) {
  11870. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11871. }
  11872. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  11873. if (XMEMCMP(cipher, testVec[i].cipher, testVec[i].len)) {
  11874. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11875. }
  11876. #endif
  11877. }
  11878. #endif /* DEBUG_VECTOR_REGISTER_ACCESS && WC_C_DYNAMIC_FALLBACK */
  11879. out:
  11880. return ret;
  11881. }
  11882. #endif /* WOLFSSL_AES_COUNTER */
  11883. #ifdef HAVE_AES_ECB
  11884. static wc_test_ret_t aes_ecb_test(Aes* enc, Aes* dec, byte* cipher, byte* plain)
  11885. {
  11886. wc_test_ret_t ret = 0;
  11887. WOLFSSL_SMALL_STACK_STATIC const byte key_128[] = "0123456789abcdef ";
  11888. WOLFSSL_SMALL_STACK_STATIC const byte key_192[] =
  11889. "0123456789abcdef01234567 ";
  11890. WOLFSSL_SMALL_STACK_STATIC const byte key_256[] =
  11891. "0123456789abcdef0123456789abcdef ";
  11892. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = "1234567890abcdef ";
  11893. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  11894. 0x6e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
  11895. 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20
  11896. };
  11897. WOLFSSL_SMALL_STACK_STATIC const byte verify_ecb_128[AES_BLOCK_SIZE] = {
  11898. 0xd0, 0xc9, 0xd9, 0xc9, 0x40, 0xe8, 0x97, 0xb6,
  11899. 0xc8, 0x8c, 0x33, 0x3b, 0xb5, 0x8f, 0x85, 0xd1
  11900. };
  11901. WOLFSSL_SMALL_STACK_STATIC const byte verify_ecb_192[AES_BLOCK_SIZE] = {
  11902. 0x06, 0x57, 0xee, 0x78, 0x3f, 0x96, 0x00, 0xb1,
  11903. 0xec, 0x76, 0x94, 0x30, 0x29, 0xbe, 0x15, 0xab
  11904. };
  11905. WOLFSSL_SMALL_STACK_STATIC const byte verify_ecb_256[AES_BLOCK_SIZE] = {
  11906. 0xcd, 0xf2, 0x81, 0x3e, 0x73, 0x3e, 0xf7, 0x33,
  11907. 0x3d, 0x18, 0xfd, 0x41, 0x85, 0x37, 0x04, 0x82
  11908. };
  11909. int i;
  11910. struct {
  11911. const byte* key;
  11912. int keySz;
  11913. const byte* iv;
  11914. const byte* verify;
  11915. } testVec[] = {
  11916. { key_128, 16, iv, verify_ecb_128 },
  11917. { key_192, 24, iv, verify_ecb_192 },
  11918. { key_256, 32, iv, verify_ecb_256 },
  11919. };
  11920. #define AES_ECB_TEST_LEN (int)(sizeof(testVec) / sizeof(*testVec))
  11921. for (i = 0; i < AES_ECB_TEST_LEN; i++) {
  11922. ret = wc_AesSetKey(enc, testVec[i].key, testVec[i].keySz, testVec[i].iv,
  11923. AES_ENCRYPTION);
  11924. if (ret != 0)
  11925. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11926. #ifdef HAVE_AES_DECRYPT
  11927. ret = wc_AesSetKey(dec, testVec[i].key, testVec[i].keySz, testVec[i].iv,
  11928. AES_DECRYPTION);
  11929. if (ret != 0)
  11930. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11931. #endif
  11932. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  11933. ret = wc_AesEcbEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  11934. #if defined(WOLFSSL_ASYNC_CRYPT)
  11935. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11936. #endif
  11937. if (ret != 0)
  11938. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11939. if (XMEMCMP(cipher, testVec[i].verify, AES_BLOCK_SIZE)) {
  11940. WOLFSSL_MSG("aes_test cipher vs verify_ecb mismatch!");
  11941. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11942. }
  11943. #ifdef HAVE_AES_DECRYPT
  11944. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  11945. ret = wc_AesEcbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  11946. #if defined(WOLFSSL_ASYNC_CRYPT)
  11947. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11948. #endif
  11949. if (ret != 0)
  11950. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11951. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  11952. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11953. #endif /* HAVE_AES_DECRYPT */
  11954. (void)dec;
  11955. (void)plain;
  11956. }
  11957. out:
  11958. return ret;
  11959. }
  11960. #endif
  11961. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_test(void)
  11962. {
  11963. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  11964. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11965. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  11966. #else
  11967. Aes enc[1];
  11968. #endif
  11969. int enc_inited = 0;
  11970. byte cipher[AES_BLOCK_SIZE * 4];
  11971. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  11972. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11973. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  11974. #else
  11975. Aes dec[1];
  11976. #endif
  11977. int dec_inited = 0;
  11978. byte plain [AES_BLOCK_SIZE * 4];
  11979. #endif /* HAVE_AES_DECRYPT || WOLFSSL_AES_COUNTER */
  11980. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  11981. wc_test_ret_t ret = 0;
  11982. #ifdef HAVE_AES_CBC
  11983. #ifdef WOLFSSL_AES_128
  11984. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  11985. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  11986. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  11987. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  11988. };
  11989. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  11990. {
  11991. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  11992. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  11993. };
  11994. #ifdef HAVE_RENESAS_SYNC
  11995. const byte *key =
  11996. (byte*)guser_PKCbInfo.wrapped_key_aes128;
  11997. #else
  11998. WOLFSSL_SMALL_STACK_STATIC const
  11999. byte key[] = "0123456789abcdef "; /* align */
  12000. #endif
  12001. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = "1234567890abcdef "; /* align */
  12002. WOLFSSL_ENTER("aes_test");
  12003. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12004. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || \
  12005. defined(WOLFSSL_AES_DIRECT)
  12006. if (enc == NULL)
  12007. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12008. #endif
  12009. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  12010. if (dec == NULL)
  12011. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12012. #endif
  12013. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  12014. ret = wc_AesInit(enc, HEAP_HINT, devId);
  12015. if (ret != 0)
  12016. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12017. enc_inited = 1;
  12018. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  12019. ret = wc_AesInit(dec, HEAP_HINT, devId);
  12020. if (ret != 0)
  12021. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12022. dec_inited = 1;
  12023. #endif
  12024. #ifdef HAVE_AES_ECB
  12025. ret = aes_ecb_test(enc, dec, cipher, plain);
  12026. if (ret != 0)
  12027. return ret;
  12028. #endif
  12029. ret = wc_AesSetKey(enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  12030. if (ret != 0)
  12031. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12032. #ifdef HAVE_AES_DECRYPT
  12033. ret = wc_AesSetKey(dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  12034. if (ret != 0)
  12035. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12036. #endif
  12037. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  12038. ret = wc_AesCbcEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  12039. #if defined(WOLFSSL_ASYNC_CRYPT)
  12040. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12041. #endif
  12042. if (ret != 0)
  12043. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12044. #ifdef HAVE_AES_DECRYPT
  12045. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  12046. ret = wc_AesCbcDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  12047. #if defined(WOLFSSL_ASYNC_CRYPT)
  12048. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12049. #endif
  12050. if (ret != 0) {
  12051. WOLFSSL_MSG("failed wc_AesCbcDecrypt");
  12052. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12053. }
  12054. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE)) {
  12055. WOLFSSL_MSG("wc_AesCbcDecrypt failed plain compare");
  12056. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12057. }
  12058. #endif /* HAVE_AES_DECRYPT */
  12059. /* skipped because wrapped key use in case of renesas sm */
  12060. #ifndef HAVE_RENESAS_SYNC
  12061. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE)) {
  12062. WOLFSSL_MSG("wc_AesCbcDecrypt failed cipher-verify compare");
  12063. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12064. }
  12065. #endif
  12066. #endif /* WOLFSSL_AES_128 */
  12067. #if defined(WOLFSSL_AESNI) && defined(HAVE_AES_DECRYPT)
  12068. {
  12069. WOLFSSL_SMALL_STACK_STATIC const byte bigMsg[] = {
  12070. /* "All work and no play makes Jack a dull boy. " */
  12071. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  12072. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  12073. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  12074. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  12075. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  12076. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  12077. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  12078. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  12079. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  12080. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  12081. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  12082. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  12083. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  12084. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  12085. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  12086. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  12087. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  12088. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  12089. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  12090. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  12091. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  12092. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  12093. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  12094. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  12095. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  12096. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  12097. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  12098. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  12099. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  12100. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  12101. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  12102. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  12103. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  12104. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  12105. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  12106. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  12107. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  12108. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  12109. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  12110. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  12111. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  12112. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  12113. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  12114. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  12115. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  12116. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  12117. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  12118. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20
  12119. };
  12120. WOLFSSL_SMALL_STACK_STATIC const byte bigKey[] = "0123456789abcdeffedcba9876543210";
  12121. word32 keySz, msgSz;
  12122. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12123. byte *bigCipher = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12124. byte *bigPlain = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12125. if ((bigCipher == NULL) ||
  12126. (bigPlain == NULL)) {
  12127. if (bigCipher != NULL)
  12128. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12129. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12130. }
  12131. #else
  12132. byte bigCipher[sizeof(bigMsg)];
  12133. byte bigPlain[sizeof(bigMsg)];
  12134. #endif
  12135. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  12136. * message by AES_BLOCK_SIZE for each size of AES key. */
  12137. for (keySz = 16; keySz <= 32; keySz += 8) {
  12138. for (msgSz = AES_BLOCK_SIZE;
  12139. msgSz <= sizeof(bigMsg);
  12140. msgSz += AES_BLOCK_SIZE) {
  12141. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  12142. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  12143. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  12144. if (ret != 0) {
  12145. ret = WC_TEST_RET_ENC_EC(ret);
  12146. break;
  12147. }
  12148. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  12149. if (ret != 0) {
  12150. ret = WC_TEST_RET_ENC_EC(ret);
  12151. break;
  12152. }
  12153. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  12154. #if defined(WOLFSSL_ASYNC_CRYPT)
  12155. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12156. #endif
  12157. if (ret != 0) {
  12158. ret = WC_TEST_RET_ENC_EC(ret);
  12159. break;
  12160. }
  12161. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  12162. #if defined(WOLFSSL_ASYNC_CRYPT)
  12163. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12164. #endif
  12165. if (ret != 0) {
  12166. ret = WC_TEST_RET_ENC_EC(ret);
  12167. break;
  12168. }
  12169. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  12170. ret = WC_TEST_RET_ENC_NC;
  12171. break;
  12172. }
  12173. }
  12174. if (ret != 0)
  12175. break;
  12176. }
  12177. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  12178. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  12179. * message by AES_BLOCK_SIZE for each size of AES key. */
  12180. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12181. for (keySz = 16; keySz <= 32; keySz += 8) {
  12182. for (msgSz = AES_BLOCK_SIZE;
  12183. msgSz <= sizeof(bigMsg);
  12184. msgSz += AES_BLOCK_SIZE) {
  12185. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  12186. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  12187. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  12188. if (ret != 0) {
  12189. ret = WC_TEST_RET_ENC_EC(ret);
  12190. break;
  12191. }
  12192. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  12193. if (ret != 0) {
  12194. ret = WC_TEST_RET_ENC_EC(ret);
  12195. break;
  12196. }
  12197. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  12198. #if defined(WOLFSSL_ASYNC_CRYPT)
  12199. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12200. #endif
  12201. if (ret != 0) {
  12202. ret = WC_TEST_RET_ENC_EC(ret);
  12203. break;
  12204. }
  12205. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  12206. #if defined(WOLFSSL_ASYNC_CRYPT)
  12207. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12208. #endif
  12209. if (ret != 0) {
  12210. ret = WC_TEST_RET_ENC_EC(ret);
  12211. break;
  12212. }
  12213. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  12214. ret = WC_TEST_RET_ENC_NC;
  12215. break;
  12216. }
  12217. }
  12218. if (ret != 0)
  12219. break;
  12220. }
  12221. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12222. #endif /* DEBUG_VECTOR_REGISTER_ACCESS && WC_C_DYNAMIC_FALLBACK */
  12223. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12224. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12225. XFREE(bigPlain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12226. #endif
  12227. if (ret != 0)
  12228. goto out;
  12229. }
  12230. #endif /* WOLFSSL_AESNI && HAVE_AES_DECRYPT */
  12231. /* Test of AES IV state with encrypt/decrypt */
  12232. #if defined(WOLFSSL_AES_128) && !defined(HAVE_RENESAS_SYNC)
  12233. {
  12234. /* Test Vector from "NIST Special Publication 800-38A, 2001 Edition"
  12235. * https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38a.pdf
  12236. */
  12237. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  12238. {
  12239. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  12240. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  12241. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  12242. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
  12243. };
  12244. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] =
  12245. {
  12246. 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
  12247. 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  12248. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
  12249. 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2
  12250. };
  12251. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  12252. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  12253. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  12254. };
  12255. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  12256. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12257. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  12258. };
  12259. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  12260. if (ret != 0)
  12261. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12262. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 2);
  12263. ret = wc_AesCbcEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE);
  12264. #if defined(WOLFSSL_ASYNC_CRYPT)
  12265. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12266. #endif
  12267. if (ret != 0)
  12268. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12269. #ifndef HAVE_RENESAS_SYNC
  12270. if (XMEMCMP(cipher, verify2, AES_BLOCK_SIZE))
  12271. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12272. #endif
  12273. ret = wc_AesCbcEncrypt(enc, cipher + AES_BLOCK_SIZE,
  12274. msg2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  12275. #if defined(WOLFSSL_ASYNC_CRYPT)
  12276. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12277. #endif
  12278. if (ret != 0)
  12279. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12280. if (XMEMCMP(cipher + AES_BLOCK_SIZE, verify2 + AES_BLOCK_SIZE,
  12281. AES_BLOCK_SIZE))
  12282. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12283. #if defined(HAVE_AES_DECRYPT)
  12284. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_DECRYPTION);
  12285. if (ret != 0)
  12286. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12287. XMEMSET(plain, 0, AES_BLOCK_SIZE * 2);
  12288. ret = wc_AesCbcDecrypt(dec, plain, verify2, AES_BLOCK_SIZE);
  12289. #if defined(WOLFSSL_ASYNC_CRYPT)
  12290. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12291. #endif
  12292. if (ret != 0)
  12293. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12294. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE))
  12295. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12296. ret = wc_AesCbcDecrypt(dec, plain + AES_BLOCK_SIZE,
  12297. verify2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  12298. #if defined(WOLFSSL_ASYNC_CRYPT)
  12299. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12300. #endif
  12301. if (ret != 0)
  12302. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12303. if (XMEMCMP(plain + AES_BLOCK_SIZE, msg2 + AES_BLOCK_SIZE,
  12304. AES_BLOCK_SIZE)) {
  12305. WOLFSSL_MSG("wc_AesCbcDecrypt failed plain-msg2 compare");
  12306. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12307. }
  12308. #endif /* HAVE_AES_DECRYPT */
  12309. }
  12310. #endif /* WOLFSSL_AES_128 && !HAVE_RENESAS_SYNC */
  12311. #endif /* HAVE_AES_CBC */
  12312. #ifdef WOLFSSL_AES_COUNTER
  12313. ret = aesctr_test(enc, dec, cipher, plain);
  12314. if (ret != 0)
  12315. return ret;
  12316. #endif
  12317. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  12318. {
  12319. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  12320. {
  12321. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  12322. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  12323. };
  12324. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  12325. {
  12326. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  12327. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  12328. };
  12329. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  12330. {
  12331. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  12332. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  12333. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  12334. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  12335. };
  12336. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  12337. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  12338. if (ret != 0)
  12339. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12340. #if !defined(HAVE_SELFTEST) && \
  12341. (defined(WOLFSSL_LINUXKM) || \
  12342. !defined(HAVE_FIPS) || \
  12343. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  12344. ret = wc_AesEncryptDirect(enc, cipher, niPlain);
  12345. if (ret != 0)
  12346. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12347. #else
  12348. wc_AesEncryptDirect(enc, cipher, niPlain);
  12349. #endif
  12350. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  12351. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12352. #ifdef HAVE_AES_DECRYPT
  12353. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  12354. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  12355. if (ret != 0)
  12356. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12357. #if !defined(HAVE_SELFTEST) && \
  12358. (defined(WOLFSSL_LINUXKM) || \
  12359. !defined(HAVE_FIPS) || \
  12360. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  12361. ret = wc_AesDecryptDirect(dec, plain, niCipher);
  12362. if (ret != 0)
  12363. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12364. #else
  12365. wc_AesDecryptDirect(dec, plain, niCipher);
  12366. #endif
  12367. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  12368. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12369. #endif /* HAVE_AES_DECRYPT */
  12370. }
  12371. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  12372. #ifndef HAVE_RENESAS_SYNC
  12373. ret = aes_key_size_test();
  12374. if (ret != 0)
  12375. goto out;
  12376. #endif
  12377. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) && \
  12378. !defined(HAVE_RENESAS_SYNC)
  12379. ret = aes_cbc_test();
  12380. if (ret != 0)
  12381. goto out;
  12382. #endif
  12383. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12384. ret = aesecb_test();
  12385. if (ret != 0)
  12386. goto out;
  12387. #endif
  12388. out:
  12389. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  12390. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12391. if (enc) {
  12392. if (enc_inited)
  12393. wc_AesFree(enc);
  12394. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  12395. }
  12396. #else
  12397. if (enc_inited)
  12398. wc_AesFree(enc);
  12399. #endif
  12400. (void)cipher;
  12401. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  12402. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12403. if (dec) {
  12404. if (dec_inited)
  12405. wc_AesFree(dec);
  12406. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  12407. }
  12408. #else
  12409. if (dec_inited)
  12410. wc_AesFree(dec);
  12411. #endif
  12412. (void)plain;
  12413. #endif /* HAVE_AES_DECRYPT || WOLFSSL_AES_COUNTER */
  12414. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  12415. return ret;
  12416. }
  12417. #if defined(WOLFSSL_AES_CFB)
  12418. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_cfb_test(void)
  12419. {
  12420. int ret;
  12421. WOLFSSL_ENTER("aes_cfb_test");
  12422. ret = aescfb_test_0();
  12423. if (ret != 0)
  12424. return ret;
  12425. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  12426. ret = aescfb1_test();
  12427. if (ret != 0)
  12428. return ret;
  12429. ret = aescfb8_test();
  12430. if (ret != 0)
  12431. return ret;
  12432. #endif
  12433. return 0;
  12434. }
  12435. #endif
  12436. #if defined(WOLFSSL_AES_XTS)
  12437. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_xts_test(void)
  12438. {
  12439. int ret = 0;
  12440. WOLFSSL_ENTER("aes_xts_test");
  12441. #ifdef WOLFSSL_AES_128
  12442. ret = aes_xts_128_test();
  12443. if (ret != 0)
  12444. return ret;
  12445. #endif
  12446. #ifdef WOLFSSL_AES_192
  12447. ret = aes_xts_192_test();
  12448. if (ret != 0)
  12449. return ret;
  12450. #endif
  12451. #ifdef WOLFSSL_AES_256
  12452. ret = aes_xts_256_test();
  12453. if (ret != 0)
  12454. return ret;
  12455. #endif
  12456. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  12457. ret = aes_xts_sector_test();
  12458. if (ret != 0)
  12459. return ret;
  12460. #endif
  12461. #ifdef WOLFSSL_AES_128
  12462. ret = aes_xts_args_test();
  12463. if (ret != 0)
  12464. return ret;
  12465. #endif
  12466. return 0;
  12467. }
  12468. #endif
  12469. #ifdef WOLFSSL_AES_192
  12470. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes192_test(void)
  12471. {
  12472. wc_test_ret_t ret = 0;
  12473. #ifdef HAVE_AES_CBC
  12474. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12475. Aes *enc = NULL;
  12476. #else
  12477. Aes enc[1];
  12478. #endif
  12479. int enc_inited = 0;
  12480. byte cipher[AES_BLOCK_SIZE];
  12481. #ifdef HAVE_AES_DECRYPT
  12482. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12483. Aes *dec = NULL;
  12484. #else
  12485. Aes dec[1];
  12486. #endif
  12487. byte plain[AES_BLOCK_SIZE];
  12488. #endif
  12489. #ifdef HAVE_AES_DECRYPT
  12490. int dec_inited = 0;
  12491. #endif
  12492. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  12493. * Appendix F.2.3 */
  12494. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  12495. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  12496. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  12497. };
  12498. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  12499. {
  12500. 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  12501. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8
  12502. };
  12503. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  12504. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  12505. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  12506. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  12507. };
  12508. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  12509. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  12510. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  12511. };
  12512. WOLFSSL_ENTER("aes192_test");
  12513. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12514. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12515. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12516. #ifdef HAVE_AES_DECRYPT
  12517. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12518. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12519. #endif
  12520. #endif
  12521. ret = wc_AesInit(enc, HEAP_HINT, devId);
  12522. if (ret != 0)
  12523. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12524. enc_inited = 1;
  12525. #ifdef HAVE_AES_DECRYPT
  12526. ret = wc_AesInit(dec, HEAP_HINT, devId);
  12527. if (ret != 0)
  12528. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12529. dec_inited = 1;
  12530. #endif
  12531. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  12532. if (ret != 0)
  12533. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12534. #ifdef HAVE_AES_DECRYPT
  12535. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  12536. if (ret != 0)
  12537. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12538. #endif
  12539. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  12540. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  12541. #if defined(WOLFSSL_ASYNC_CRYPT)
  12542. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12543. #endif
  12544. if (ret != 0)
  12545. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12546. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  12547. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12548. #ifdef HAVE_AES_DECRYPT
  12549. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  12550. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  12551. #if defined(WOLFSSL_ASYNC_CRYPT)
  12552. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12553. #endif
  12554. if (ret != 0)
  12555. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12556. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  12557. WOLFSSL_MSG("failed wc_AesCbcDecrypt plain-msg compare");
  12558. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12559. }
  12560. #endif
  12561. out:
  12562. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12563. if (enc) {
  12564. if (enc_inited)
  12565. wc_AesFree(enc);
  12566. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  12567. }
  12568. #ifdef HAVE_AES_DECRYPT
  12569. if (dec) {
  12570. if (dec_inited)
  12571. wc_AesFree(dec);
  12572. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  12573. }
  12574. #endif
  12575. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  12576. if (enc_inited)
  12577. wc_AesFree(enc);
  12578. #ifdef HAVE_AES_DECRYPT
  12579. if (dec_inited)
  12580. wc_AesFree(dec);
  12581. #endif
  12582. #endif
  12583. #endif /* HAVE_AES_CBC */
  12584. return ret;
  12585. }
  12586. #endif /* WOLFSSL_AES_192 */
  12587. #ifdef WOLFSSL_AES_256
  12588. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes256_test(void)
  12589. {
  12590. #ifdef HAVE_AES_CBC
  12591. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12592. Aes *enc = NULL;
  12593. #else
  12594. Aes enc[1];
  12595. #endif
  12596. int enc_inited = 0;
  12597. byte cipher[AES_BLOCK_SIZE];
  12598. #ifdef HAVE_AES_DECRYPT
  12599. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12600. Aes *dec = NULL;
  12601. #else
  12602. Aes dec[1];
  12603. #endif
  12604. int dec_inited = 0;
  12605. byte plain[AES_BLOCK_SIZE];
  12606. #endif
  12607. #endif /* HAVE_AES_CBC */
  12608. wc_test_ret_t ret = 0;
  12609. #ifdef HAVE_AES_CBC
  12610. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  12611. * Appendix F.2.5 */
  12612. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  12613. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  12614. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  12615. };
  12616. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  12617. {
  12618. 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  12619. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6
  12620. };
  12621. #ifdef HAVE_RENESAS_SYNC
  12622. byte *key =
  12623. (byte*)guser_PKCbInfo.wrapped_key_aes256;
  12624. int keySz = (256/8);
  12625. #else
  12626. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  12627. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  12628. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  12629. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  12630. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  12631. };
  12632. int keySz = (int)sizeof(key);
  12633. #endif
  12634. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  12635. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  12636. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  12637. };
  12638. WOLFSSL_ENTER("aes256_test");
  12639. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12640. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12641. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12642. #ifdef HAVE_AES_DECRYPT
  12643. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12644. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12645. #endif
  12646. #endif
  12647. ret = wc_AesInit(enc, HEAP_HINT, devId);
  12648. if (ret != 0)
  12649. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12650. enc_inited = 1;
  12651. #ifdef HAVE_AES_DECRYPT
  12652. ret = wc_AesInit(dec, HEAP_HINT, devId);
  12653. if (ret != 0)
  12654. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12655. dec_inited = 1;
  12656. #endif
  12657. ret = wc_AesSetKey(enc, key, (word32)keySz, iv, AES_ENCRYPTION);
  12658. if (ret != 0)
  12659. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12660. #ifdef HAVE_AES_DECRYPT
  12661. ret = wc_AesSetKey(dec, key, (word32)keySz, iv, AES_DECRYPTION);
  12662. if (ret != 0)
  12663. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12664. #endif
  12665. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  12666. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  12667. #if defined(WOLFSSL_ASYNC_CRYPT)
  12668. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12669. #endif
  12670. if (ret != 0)
  12671. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12672. #ifdef HAVE_AES_DECRYPT
  12673. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  12674. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  12675. #if defined(WOLFSSL_ASYNC_CRYPT)
  12676. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12677. #endif
  12678. if (ret != 0)
  12679. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12680. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  12681. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12682. }
  12683. #endif
  12684. #ifndef HAVE_RENESAS_SYNC
  12685. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  12686. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12687. #endif
  12688. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  12689. ret = wc_AesSetKey(enc, key, keySz, iv, AES_ENCRYPTION);
  12690. if (ret != 0)
  12691. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12692. #ifdef HAVE_AES_DECRYPT
  12693. ret = wc_AesSetKey(dec, key, keySz, iv, AES_DECRYPTION);
  12694. if (ret != 0)
  12695. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12696. #endif
  12697. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  12698. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12699. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  12700. #if defined(WOLFSSL_ASYNC_CRYPT)
  12701. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12702. #endif
  12703. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12704. if (ret != 0)
  12705. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12706. #ifdef HAVE_AES_DECRYPT
  12707. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  12708. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12709. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  12710. #if defined(WOLFSSL_ASYNC_CRYPT)
  12711. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12712. #endif
  12713. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12714. if (ret != 0)
  12715. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12716. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  12717. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12718. }
  12719. #endif
  12720. #ifndef HAVE_RENESAS_SYNC
  12721. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  12722. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12723. #endif
  12724. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12725. ret = wc_AesSetKey(enc, key, keySz, iv, AES_ENCRYPTION);
  12726. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12727. if (ret != 0)
  12728. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12729. #ifdef HAVE_AES_DECRYPT
  12730. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12731. ret = wc_AesSetKey(dec, key, keySz, iv, AES_DECRYPTION);
  12732. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12733. if (ret != 0)
  12734. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12735. #endif
  12736. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  12737. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  12738. #if defined(WOLFSSL_ASYNC_CRYPT)
  12739. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12740. #endif
  12741. if (ret != 0)
  12742. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12743. #ifdef HAVE_AES_DECRYPT
  12744. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  12745. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  12746. #if defined(WOLFSSL_ASYNC_CRYPT)
  12747. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12748. #endif
  12749. if (ret != 0)
  12750. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12751. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  12752. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12753. }
  12754. #endif
  12755. #ifndef HAVE_RENESAS_SYNC
  12756. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  12757. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12758. #endif
  12759. #endif /* DEBUG_VECTOR_REGISTER_ACCESS && WC_C_DYNAMIC_FALLBACK */
  12760. out:
  12761. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12762. if (enc) {
  12763. if (enc_inited)
  12764. wc_AesFree(enc);
  12765. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  12766. }
  12767. #ifdef HAVE_AES_DECRYPT
  12768. if (dec) {
  12769. if (dec_inited)
  12770. wc_AesFree(dec);
  12771. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  12772. }
  12773. #endif
  12774. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  12775. if (enc_inited)
  12776. wc_AesFree(enc);
  12777. #ifdef HAVE_AES_DECRYPT
  12778. if (dec_inited)
  12779. wc_AesFree(dec);
  12780. #endif /* HAVE_AES_DECRYPT */
  12781. #endif /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  12782. #endif /* HAVE_AES_CBC */
  12783. return ret;
  12784. }
  12785. #endif /* WOLFSSL_AES_256 */
  12786. #ifdef HAVE_AESGCM
  12787. #ifdef WOLFSSL_AES_128
  12788. static wc_test_ret_t aesgcm_default_test_helper(byte* key, int keySz, byte* iv, int ivSz,
  12789. byte* plain, int plainSz, byte* cipher, int cipherSz,
  12790. byte* aad, int aadSz, byte* tag, int tagSz)
  12791. {
  12792. wc_test_ret_t ret;
  12793. int enc_inited = 0, dec_inited = 0;
  12794. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12795. Aes *enc = NULL;
  12796. Aes *dec = NULL;
  12797. #else
  12798. Aes enc[1];
  12799. Aes dec[1];
  12800. #endif
  12801. byte resultT[AES_BLOCK_SIZE];
  12802. byte resultP[AES_BLOCK_SIZE * 3];
  12803. byte resultC[AES_BLOCK_SIZE * 3];
  12804. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12805. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12806. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12807. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12808. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12809. #endif
  12810. XMEMSET(resultT, 0, sizeof(resultT));
  12811. XMEMSET(resultC, 0, sizeof(resultC));
  12812. XMEMSET(resultP, 0, sizeof(resultP));
  12813. ret = wc_AesInit(enc, HEAP_HINT, devId);
  12814. if (ret != 0)
  12815. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12816. else
  12817. enc_inited = 1;
  12818. ret = wc_AesInit(dec, HEAP_HINT, devId);
  12819. if (ret != 0)
  12820. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12821. else
  12822. dec_inited = 1;
  12823. ret = wc_AesGcmSetKey(enc, key, (word32)keySz);
  12824. if (ret != 0)
  12825. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12826. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  12827. ret = wc_AesGcmEncrypt(enc, resultC, plain, (word32)plainSz, iv, ivSz,
  12828. resultT, (word32)tagSz, aad, aadSz);
  12829. #if defined(WOLFSSL_ASYNC_CRYPT)
  12830. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12831. #endif
  12832. if (ret != 0)
  12833. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12834. if (cipher != NULL) {
  12835. if (XMEMCMP(cipher, resultC, cipherSz))
  12836. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12837. }
  12838. if (XMEMCMP(tag, resultT, tagSz))
  12839. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12840. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  12841. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12842. ret = wc_AesGcmEncrypt(enc, resultC, plain, plainSz, iv, ivSz,
  12843. resultT, tagSz, aad, aadSz);
  12844. #if defined(WOLFSSL_ASYNC_CRYPT)
  12845. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12846. #endif
  12847. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12848. if (ret != 0)
  12849. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12850. if (cipher != NULL) {
  12851. if (XMEMCMP(cipher, resultC, cipherSz))
  12852. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12853. }
  12854. if (XMEMCMP(tag, resultT, tagSz))
  12855. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12856. #endif
  12857. #ifdef HAVE_AES_DECRYPT
  12858. ret = wc_AesGcmSetKey(dec, key, (word32)keySz);
  12859. if (ret != 0)
  12860. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12861. ret = wc_AesGcmDecrypt(dec, resultP, resultC, (word32)cipherSz,
  12862. iv, (word32)ivSz, resultT, tagSz, aad, aadSz);
  12863. #if defined(WOLFSSL_ASYNC_CRYPT)
  12864. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12865. #endif
  12866. if (ret != 0)
  12867. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12868. if (plain != NULL) {
  12869. if (XMEMCMP(plain, resultP, plainSz))
  12870. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12871. }
  12872. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  12873. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12874. ret = wc_AesGcmDecrypt(dec, resultP, resultC, cipherSz,
  12875. iv, ivSz, resultT, tagSz, aad, aadSz);
  12876. #if defined(WOLFSSL_ASYNC_CRYPT)
  12877. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12878. #endif
  12879. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12880. if (ret != 0)
  12881. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12882. if (plain != NULL) {
  12883. if (XMEMCMP(plain, resultP, plainSz))
  12884. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12885. }
  12886. #endif
  12887. #endif /* HAVE_AES_DECRYPT */
  12888. ret = 0;
  12889. out:
  12890. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12891. if (enc) {
  12892. if (enc_inited)
  12893. wc_AesFree(enc);
  12894. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  12895. }
  12896. if (dec) {
  12897. if (dec_inited)
  12898. wc_AesFree(dec);
  12899. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  12900. }
  12901. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  12902. if (enc_inited)
  12903. wc_AesFree(enc);
  12904. if (dec_inited)
  12905. wc_AesFree(dec);
  12906. #endif
  12907. return ret;
  12908. }
  12909. #endif
  12910. /* tests that only use 12 byte IV and 16 or less byte AAD
  12911. * test vectors are from NIST SP 800-38D
  12912. * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/CAVP-TESTING-BLOCK-CIPHER-MODES*/
  12913. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_default_test(void)
  12914. {
  12915. #ifdef WOLFSSL_AES_128
  12916. byte key1[] = {
  12917. 0x29, 0x8e, 0xfa, 0x1c, 0xcf, 0x29, 0xcf, 0x62,
  12918. 0xae, 0x68, 0x24, 0xbf, 0xc1, 0x95, 0x57, 0xfc
  12919. };
  12920. byte iv1[] = {
  12921. 0x6f, 0x58, 0xa9, 0x3f, 0xe1, 0xd2, 0x07, 0xfa,
  12922. 0xe4, 0xed, 0x2f, 0x6d
  12923. };
  12924. ALIGN64 byte plain1[] = {
  12925. 0xcc, 0x38, 0xbc, 0xcd, 0x6b, 0xc5, 0x36, 0xad,
  12926. 0x91, 0x9b, 0x13, 0x95, 0xf5, 0xd6, 0x38, 0x01,
  12927. 0xf9, 0x9f, 0x80, 0x68, 0xd6, 0x5c, 0xa5, 0xac,
  12928. 0x63, 0x87, 0x2d, 0xaf, 0x16, 0xb9, 0x39, 0x01
  12929. };
  12930. byte aad1[] = {
  12931. 0x02, 0x1f, 0xaf, 0xd2, 0x38, 0x46, 0x39, 0x73,
  12932. 0xff, 0xe8, 0x02, 0x56, 0xe5, 0xb1, 0xc6, 0xb1
  12933. };
  12934. ALIGN64 byte cipher1[] = {
  12935. 0xdf, 0xce, 0x4e, 0x9c, 0xd2, 0x91, 0x10, 0x3d,
  12936. 0x7f, 0xe4, 0xe6, 0x33, 0x51, 0xd9, 0xe7, 0x9d,
  12937. 0x3d, 0xfd, 0x39, 0x1e, 0x32, 0x67, 0x10, 0x46,
  12938. 0x58, 0x21, 0x2d, 0xa9, 0x65, 0x21, 0xb7, 0xdb
  12939. };
  12940. byte tag1[] = {
  12941. 0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
  12942. 0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
  12943. };
  12944. byte key2[] = {
  12945. 0x01, 0x6d, 0xbb, 0x38, 0xda, 0xa7, 0x6d, 0xfe,
  12946. 0x7d, 0xa3, 0x84, 0xeb, 0xf1, 0x24, 0x03, 0x64
  12947. };
  12948. byte iv2[] = {
  12949. 0x07, 0x93, 0xef, 0x3a, 0xda, 0x78, 0x2f, 0x78,
  12950. 0xc9, 0x8a, 0xff, 0xe3
  12951. };
  12952. ALIGN64 byte plain2[] = {
  12953. 0x4b, 0x34, 0xa9, 0xec, 0x57, 0x63, 0x52, 0x4b,
  12954. 0x19, 0x1d, 0x56, 0x16, 0xc5, 0x47, 0xf6, 0xb7
  12955. };
  12956. ALIGN64 byte cipher2[] = {
  12957. 0x60, 0x9a, 0xa3, 0xf4, 0x54, 0x1b, 0xc0, 0xfe,
  12958. 0x99, 0x31, 0xda, 0xad, 0x2e, 0xe1, 0x5d, 0x0c
  12959. };
  12960. byte tag2[] = {
  12961. 0x33, 0xaf, 0xec, 0x59, 0xc4, 0x5b, 0xaf, 0x68,
  12962. 0x9a, 0x5e, 0x1b, 0x13, 0xae, 0x42, 0x36, 0x19
  12963. };
  12964. byte key3[] = {
  12965. 0xb0, 0x1e, 0x45, 0xcc, 0x30, 0x88, 0xaa, 0xba,
  12966. 0x9f, 0xa4, 0x3d, 0x81, 0xd4, 0x81, 0x82, 0x3f
  12967. };
  12968. byte iv3[] = {
  12969. 0x5a, 0x2c, 0x4a, 0x66, 0x46, 0x87, 0x13, 0x45,
  12970. 0x6a, 0x4b, 0xd5, 0xe1
  12971. };
  12972. byte tag3[] = {
  12973. 0x01, 0x42, 0x80, 0xf9, 0x44, 0xf5, 0x3c, 0x68,
  12974. 0x11, 0x64, 0xb2, 0xff
  12975. };
  12976. wc_test_ret_t ret;
  12977. WOLFSSL_ENTER("aesgcm_default_test");
  12978. ret = aesgcm_default_test_helper(key1, sizeof(key1), iv1, sizeof(iv1),
  12979. plain1, sizeof(plain1), cipher1, sizeof(cipher1),
  12980. aad1, sizeof(aad1), tag1, sizeof(tag1));
  12981. if (ret != 0) {
  12982. return ret;
  12983. }
  12984. ret = aesgcm_default_test_helper(key2, sizeof(key2), iv2, sizeof(iv2),
  12985. plain2, sizeof(plain2), cipher2, sizeof(cipher2),
  12986. NULL, 0, tag2, sizeof(tag2));
  12987. if (ret != 0) {
  12988. return ret;
  12989. }
  12990. ret = aesgcm_default_test_helper(key3, sizeof(key3), iv3, sizeof(iv3),
  12991. NULL, 0, NULL, 0,
  12992. NULL, 0, tag3, sizeof(tag3));
  12993. if (ret != 0) {
  12994. return ret;
  12995. }
  12996. #endif
  12997. return 0;
  12998. }
  12999. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_test(void)
  13000. {
  13001. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13002. Aes *enc = NULL;
  13003. Aes *dec = NULL;
  13004. #else
  13005. Aes enc[1];
  13006. Aes dec[1];
  13007. #endif
  13008. /*
  13009. * This is Test Case 16 from the document Galois/
  13010. * Counter Mode of Operation (GCM) by McGrew and
  13011. * Viega.
  13012. */
  13013. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  13014. {
  13015. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  13016. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  13017. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  13018. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  13019. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  13020. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  13021. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  13022. 0xba, 0x63, 0x7b, 0x39
  13023. };
  13024. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  13025. {
  13026. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  13027. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  13028. 0xab, 0xad, 0xda, 0xd2
  13029. };
  13030. #ifdef WOLFSSL_AES_256
  13031. #ifdef HAVE_RENESAS_SYNC
  13032. const byte *k1 = (byte*)guser_PKCbInfo.wrapped_key_aes256;
  13033. int k1Sz = (int)(256/8);
  13034. #else
  13035. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  13036. {
  13037. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  13038. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  13039. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  13040. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  13041. };
  13042. int k1Sz = (int)sizeof(k1);
  13043. #endif
  13044. #endif /* WOLFSSL_AES_256 */
  13045. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  13046. {
  13047. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  13048. 0xde, 0xca, 0xf8, 0x88
  13049. };
  13050. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  13051. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  13052. {
  13053. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  13054. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  13055. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  13056. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  13057. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  13058. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  13059. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  13060. 0xbc, 0xc9, 0xf6, 0x62
  13061. };
  13062. #endif /* WOLFSSL_AES_256 || WOLFSSL_AES_192 */
  13063. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  13064. {
  13065. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  13066. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  13067. };
  13068. /* FIPS, QAT and PIC32MZ HW Crypto only support 12-byte IV */
  13069. #if !defined(HAVE_FIPS) && \
  13070. !defined(WOLFSSL_PIC32MZ_CRYPT) && \
  13071. !defined(FREESCALE_LTC) && !defined(FREESCALE_MMCAU) && \
  13072. !defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  13073. !defined(WOLFSSL_SILABS_SE_ACCEL) && !defined(WOLFSSL_KCAPI_AES) && \
  13074. !(defined(WOLF_CRYPTO_CB) && \
  13075. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  13076. #define ENABLE_NON_12BYTE_IV_TEST
  13077. #ifdef WOLFSSL_AES_192
  13078. /* Test Case 12, uses same plaintext and AAD data. */
  13079. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  13080. {
  13081. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  13082. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  13083. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c
  13084. };
  13085. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  13086. {
  13087. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  13088. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  13089. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  13090. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  13091. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  13092. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  13093. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  13094. 0xa6, 0x37, 0xb3, 0x9b
  13095. };
  13096. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  13097. {
  13098. 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  13099. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  13100. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  13101. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  13102. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  13103. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  13104. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  13105. 0xe9, 0xb7, 0x37, 0x3b
  13106. };
  13107. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  13108. {
  13109. 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  13110. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9
  13111. };
  13112. #endif /* WOLFSSL_AES_192 */
  13113. #ifdef WOLFSSL_AES_128
  13114. /* The following is an interesting test case from the example
  13115. * FIPS test vectors for AES-GCM. IVlen = 1 byte */
  13116. WOLFSSL_SMALL_STACK_STATIC const byte p3[] =
  13117. {
  13118. 0x57, 0xce, 0x45, 0x1f, 0xa5, 0xe2, 0x35, 0xa5,
  13119. 0x8e, 0x1a, 0xa2, 0x3b, 0x77, 0xcb, 0xaf, 0xe2
  13120. };
  13121. #ifdef HAVE_RENESAS_SYNC
  13122. const byte *k3 =
  13123. (byte*)guser_PKCbInfo.wrapped_key_aes128;
  13124. int k3Sz = (int)(128/8);
  13125. #else
  13126. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  13127. {
  13128. 0xbb, 0x01, 0xd7, 0x03, 0x81, 0x1c, 0x10, 0x1a,
  13129. 0x35, 0xe0, 0xff, 0xd2, 0x91, 0xba, 0xf2, 0x4b
  13130. };
  13131. int k3Sz = (int)sizeof(k3);
  13132. #endif
  13133. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  13134. {
  13135. 0xca
  13136. };
  13137. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  13138. {
  13139. 0x6b, 0x5f, 0xb3, 0x9d, 0xc1, 0xc5, 0x7a, 0x4f,
  13140. 0xf3, 0x51, 0x4d, 0xc2, 0xd5, 0xf0, 0xd0, 0x07
  13141. };
  13142. WOLFSSL_SMALL_STACK_STATIC const byte a3[] =
  13143. {
  13144. 0x40, 0xfc, 0xdc, 0xd7, 0x4a, 0xd7, 0x8b, 0xf1,
  13145. 0x3e, 0x7c, 0x60, 0x55, 0x50, 0x51, 0xdd, 0x54
  13146. };
  13147. WOLFSSL_SMALL_STACK_STATIC const byte t3[] =
  13148. {
  13149. 0x06, 0x90, 0xed, 0x01, 0x34, 0xdd, 0xc6, 0x95,
  13150. 0x31, 0x2e, 0x2a, 0xf9, 0x57, 0x7a, 0x1e, 0xa6
  13151. };
  13152. #endif /* WOLFSSL_AES_128 */
  13153. #ifdef WOLFSSL_AES_256
  13154. int ivlen;
  13155. #endif
  13156. #endif
  13157. byte resultT[sizeof(t1) + AES_BLOCK_SIZE];
  13158. byte resultP[sizeof(p) + AES_BLOCK_SIZE];
  13159. byte resultC[sizeof(p) + AES_BLOCK_SIZE];
  13160. wc_test_ret_t ret = 0;
  13161. int alen;
  13162. int plen;
  13163. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  13164. byte buf[sizeof(p) + AES_BLOCK_SIZE];
  13165. byte bufA[sizeof(a) + 1];
  13166. byte *large_aad = (byte*)XMALLOC((size_t)1024 + 16, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13167. #endif
  13168. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM)
  13169. #if !defined(BENCH_AESGCM_LARGE)
  13170. #define BENCH_AESGCM_LARGE 1024
  13171. #endif
  13172. #ifndef WOLFSSL_NO_MALLOC
  13173. byte *large_input = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13174. byte *large_output = (byte *)XMALLOC(BENCH_AESGCM_LARGE + AES_BLOCK_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13175. byte *large_outdec = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13176. (void)alen;
  13177. (void)plen;
  13178. if ((! large_input) || (! large_output) || (! large_outdec))
  13179. ERROR_OUT(MEMORY_E, out);
  13180. #else
  13181. byte large_input[BENCH_AESGCM_LARGE];
  13182. byte large_output[BENCH_AESGCM_LARGE + AES_BLOCK_SIZE];
  13183. byte large_outdec[BENCH_AESGCM_LARGE];
  13184. #endif
  13185. XMEMSET(large_input, 0, BENCH_AESGCM_LARGE);
  13186. XMEMSET(large_output, 0, BENCH_AESGCM_LARGE + AES_BLOCK_SIZE);
  13187. XMEMSET(large_outdec, 0, BENCH_AESGCM_LARGE);
  13188. #endif
  13189. WOLFSSL_ENTER("aesgcm_test");
  13190. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13191. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  13192. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  13193. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  13194. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  13195. #endif
  13196. XMEMSET(resultT, 0, sizeof(resultT));
  13197. XMEMSET(resultC, 0, sizeof(resultC));
  13198. XMEMSET(resultP, 0, sizeof(resultP));
  13199. ret = wc_AesInit(enc, HEAP_HINT, devId);
  13200. if (ret != 0)
  13201. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13202. ret = wc_AesInit(dec, HEAP_HINT, devId);
  13203. if (ret != 0)
  13204. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13205. #ifdef WOLFSSL_AES_256
  13206. ret = wc_AesGcmSetKey(enc, k1, (word32)k1Sz);
  13207. if (ret != 0)
  13208. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13209. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13210. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  13211. resultT, sizeof(t1), a, sizeof(a));
  13212. #if defined(WOLFSSL_ASYNC_CRYPT)
  13213. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13214. #endif
  13215. if (ret != 0)
  13216. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13217. #ifndef HAVE_RENESAS_SYNC
  13218. if (XMEMCMP(c1, resultC, sizeof(c1)))
  13219. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13220. if (XMEMCMP(t1, resultT, sizeof(t1)))
  13221. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13222. #endif
  13223. #ifdef HAVE_AES_DECRYPT
  13224. ret = wc_AesGcmSetKey(dec, k1, (word32)k1Sz);
  13225. if (ret != 0)
  13226. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13227. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1),
  13228. iv1, sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  13229. #if defined(WOLFSSL_ASYNC_CRYPT)
  13230. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13231. #endif
  13232. if (ret != 0)
  13233. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13234. if (XMEMCMP(p, resultP, sizeof(p)))
  13235. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13236. #endif /* HAVE_AES_DECRYPT */
  13237. /* Large buffer test */
  13238. #ifdef BENCH_AESGCM_LARGE
  13239. /* setup test buffer */
  13240. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  13241. large_input[alen] = (byte)alen;
  13242. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13243. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  13244. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  13245. resultT, sizeof(t1), a, sizeof(a));
  13246. #if defined(WOLFSSL_ASYNC_CRYPT)
  13247. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13248. #endif
  13249. if (ret != 0)
  13250. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13251. #ifdef HAVE_AES_DECRYPT
  13252. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  13253. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  13254. sizeof(t1), a, sizeof(a));
  13255. #if defined(WOLFSSL_ASYNC_CRYPT)
  13256. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13257. #endif
  13258. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  13259. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13260. if (ret != 0)
  13261. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13262. #endif /* HAVE_AES_DECRYPT */
  13263. #endif /* BENCH_AESGCM_LARGE */
  13264. #if defined(ENABLE_NON_12BYTE_IV_TEST) && defined(WOLFSSL_AES_256)
  13265. /* Variable IV length test */
  13266. for (ivlen=1; ivlen<k1Sz; ivlen++) {
  13267. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13268. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), k1,
  13269. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  13270. #if defined(WOLFSSL_ASYNC_CRYPT)
  13271. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13272. #endif
  13273. if (ret != 0)
  13274. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13275. #ifdef HAVE_AES_DECRYPT
  13276. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), k1,
  13277. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  13278. #if defined(WOLFSSL_ASYNC_CRYPT)
  13279. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13280. #endif
  13281. if (ret != 0)
  13282. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13283. #endif /* HAVE_AES_DECRYPT */
  13284. }
  13285. #endif
  13286. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  13287. /* Variable authenticated data length test */
  13288. for (alen=0; alen<(int)sizeof(p); alen++) {
  13289. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13290. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  13291. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  13292. #if defined(WOLFSSL_ASYNC_CRYPT)
  13293. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13294. #endif
  13295. if (ret != 0)
  13296. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13297. #ifdef HAVE_AES_DECRYPT
  13298. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  13299. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  13300. #if defined(WOLFSSL_ASYNC_CRYPT)
  13301. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13302. #endif
  13303. if (ret != 0)
  13304. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13305. #endif /* HAVE_AES_DECRYPT */
  13306. }
  13307. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  13308. if (! large_aad)
  13309. ERROR_OUT(MEMORY_E, out);
  13310. XMEMSET(large_aad, 0, 1024+16);
  13311. /* Variable authenticated data length test */
  13312. for (alen=0; alen<=1024; alen+=16) {
  13313. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13314. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  13315. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  13316. if (ret != 0)
  13317. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13318. #ifdef HAVE_AES_DECRYPT
  13319. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  13320. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  13321. if (ret != 0)
  13322. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13323. #endif /* HAVE_AES_DECRYPT */
  13324. }
  13325. /* Test unaligned memory of all potential arguments */
  13326. ret = wc_AesGcmSetKey(enc, k1, k1Sz);
  13327. if (ret != 0)
  13328. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13329. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13330. XMEMCPY(&buf[1], p, sizeof(p));
  13331. XMEMCPY(&bufA[1], a, sizeof(a));
  13332. ret = wc_AesGcmEncrypt(enc, &resultC[1], &buf[1], sizeof(p), iv1, sizeof(iv1),
  13333. &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  13334. if (ret != 0)
  13335. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13336. if (XMEMCMP(c1, &resultC[1], sizeof(c1)))
  13337. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13338. if (XMEMCMP(t1, &resultT[1], sizeof(t1)))
  13339. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13340. #ifdef HAVE_AES_DECRYPT
  13341. ret = wc_AesGcmSetKey(dec, k1, k1Sz);
  13342. if (ret != 0)
  13343. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13344. ret = wc_AesGcmDecrypt(dec, &resultP[1], &resultC[1], sizeof(c1),
  13345. iv1, sizeof(iv1), &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  13346. if (ret != 0)
  13347. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13348. if (XMEMCMP(p, &resultP[1], sizeof(p)))
  13349. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13350. #endif /* HAVE_AES_DECRYPT */
  13351. #endif /* Xilinx Versal */
  13352. #endif
  13353. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  13354. #ifdef BENCH_AESGCM_LARGE
  13355. /* Variable plain text length test */
  13356. for (plen=1; plen<BENCH_AESGCM_LARGE; plen++) {
  13357. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13358. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  13359. (word32)plen, iv1, sizeof(iv1), resultT,
  13360. sizeof(t1), a, sizeof(a));
  13361. #if defined(WOLFSSL_ASYNC_CRYPT)
  13362. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13363. #endif
  13364. if (ret != 0)
  13365. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13366. #ifdef HAVE_AES_DECRYPT
  13367. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  13368. (word32)plen, iv1, sizeof(iv1), resultT,
  13369. sizeof(t1), a, sizeof(a));
  13370. #if defined(WOLFSSL_ASYNC_CRYPT)
  13371. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13372. #endif
  13373. if (ret != 0)
  13374. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13375. #endif /* HAVE_AES_DECRYPT */
  13376. }
  13377. #else /* BENCH_AESGCM_LARGE */
  13378. /* Variable plain text length test */
  13379. for (plen=1; plen<(int)sizeof(p); plen++) {
  13380. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13381. ret = wc_AesGcmEncrypt(enc, resultC, p, (word32)plen, iv1,
  13382. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  13383. #if defined(WOLFSSL_ASYNC_CRYPT)
  13384. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13385. #endif
  13386. if (ret != 0)
  13387. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13388. #ifdef HAVE_AES_DECRYPT
  13389. ret = wc_AesGcmDecrypt(dec, resultP, resultC, (word32)plen, iv1,
  13390. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  13391. #if defined(WOLFSSL_ASYNC_CRYPT)
  13392. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13393. #endif
  13394. if (ret != 0)
  13395. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13396. #endif /* HAVE_AES_DECRYPT */
  13397. }
  13398. #endif /* BENCH_AESGCM_LARGE */
  13399. #endif
  13400. #endif /* WOLFSSL_AES_256 */
  13401. /* test with IV != 12 bytes */
  13402. #ifdef ENABLE_NON_12BYTE_IV_TEST
  13403. XMEMSET(resultT, 0, sizeof(resultT));
  13404. XMEMSET(resultC, 0, sizeof(resultC));
  13405. XMEMSET(resultP, 0, sizeof(resultP));
  13406. #ifdef WOLFSSL_AES_192
  13407. wc_AesGcmSetKey(enc, k2, sizeof(k2));
  13408. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13409. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv2, sizeof(iv2),
  13410. resultT, sizeof(t1), a, sizeof(a));
  13411. #if defined(WOLFSSL_ASYNC_CRYPT)
  13412. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13413. #endif
  13414. if (ret != 0)
  13415. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13416. if (XMEMCMP(c2, resultC, sizeof(c2)))
  13417. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13418. if (XMEMCMP(t2, resultT, sizeof(t1)))
  13419. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13420. #ifdef HAVE_AES_DECRYPT
  13421. ret = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c1),
  13422. iv2, sizeof(iv2), resultT, sizeof(t1), a, sizeof(a));
  13423. #if defined(WOLFSSL_ASYNC_CRYPT)
  13424. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13425. #endif
  13426. if (ret != 0)
  13427. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13428. if (XMEMCMP(p, resultP, sizeof(p)))
  13429. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13430. #endif /* HAVE_AES_DECRYPT */
  13431. /* Large buffer test */
  13432. #ifdef BENCH_AESGCM_LARGE
  13433. wc_AesGcmSetKey(enc, k2, (word32)sizeof(k2));
  13434. wc_AesGcmSetKey(dec, k2, (word32)sizeof(k2));
  13435. /* setup test buffer */
  13436. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  13437. large_input[alen] = (byte)alen;
  13438. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13439. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  13440. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  13441. resultT, sizeof(t1), a, sizeof(a));
  13442. #if defined(WOLFSSL_ASYNC_CRYPT)
  13443. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13444. #endif
  13445. if (ret != 0)
  13446. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13447. #ifdef HAVE_AES_DECRYPT
  13448. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  13449. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  13450. sizeof(t1), a, sizeof(a));
  13451. #if defined(WOLFSSL_ASYNC_CRYPT)
  13452. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13453. #endif
  13454. if (ret != 0)
  13455. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13456. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  13457. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13458. #endif /* HAVE_AES_DECRYPT */
  13459. #endif /* BENCH_AESGCM_LARGE */
  13460. XMEMSET(resultT, 0, sizeof(resultT));
  13461. XMEMSET(resultC, 0, sizeof(resultC));
  13462. XMEMSET(resultP, 0, sizeof(resultP));
  13463. #endif /* WOLFSSL_AES_192 */
  13464. #ifdef WOLFSSL_AES_128
  13465. wc_AesGcmSetKey(enc, k3, (word32)k3Sz);
  13466. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13467. ret = wc_AesGcmEncrypt(enc, resultC, p3, sizeof(p3), iv3, sizeof(iv3),
  13468. resultT, sizeof(t3), a3, sizeof(a3));
  13469. #if defined(WOLFSSL_ASYNC_CRYPT)
  13470. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13471. #endif
  13472. if (ret != 0)
  13473. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13474. #ifndef HAVE_RENESAS_SYNC
  13475. if (XMEMCMP(c3, resultC, sizeof(c3)))
  13476. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13477. if (XMEMCMP(t3, resultT, sizeof(t3)))
  13478. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13479. #endif
  13480. #ifdef HAVE_AES_DECRYPT
  13481. ret = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c3),
  13482. iv3, sizeof(iv3), resultT, sizeof(t3), a3, sizeof(a3));
  13483. #if defined(WOLFSSL_ASYNC_CRYPT)
  13484. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13485. #endif
  13486. if (ret != 0)
  13487. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13488. if (XMEMCMP(p3, resultP, sizeof(p3)))
  13489. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13490. #endif /* HAVE_AES_DECRYPT */
  13491. /* Large buffer test */
  13492. #ifdef BENCH_AESGCM_LARGE
  13493. wc_AesGcmSetKey(enc, k3, (word32)k3Sz);
  13494. wc_AesGcmSetKey(dec, k3, (word32)k3Sz);
  13495. /* setup test buffer */
  13496. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  13497. large_input[alen] = (byte)alen;
  13498. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13499. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  13500. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  13501. resultT, sizeof(t1), a, sizeof(a));
  13502. #if defined(WOLFSSL_ASYNC_CRYPT)
  13503. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13504. #endif
  13505. if (ret != 0)
  13506. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13507. #ifdef HAVE_AES_DECRYPT
  13508. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  13509. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  13510. sizeof(t1), a, sizeof(a));
  13511. #if defined(WOLFSSL_ASYNC_CRYPT)
  13512. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13513. #endif
  13514. if (ret != 0)
  13515. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13516. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  13517. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13518. #endif /* HAVE_AES_DECRYPT */
  13519. #endif /* BENCH_AESGCM_LARGE */
  13520. #endif /* WOLFSSL_AES_128 */
  13521. #endif /* ENABLE_NON_12BYTE_IV_TEST */
  13522. #if defined(WOLFSSL_AES_256) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  13523. !defined(WOLFSSL_XILINX_CRYPT) && \
  13524. !(defined(WOLF_CRYPTO_CB) && \
  13525. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC))
  13526. XMEMSET(resultT, 0, sizeof(resultT));
  13527. XMEMSET(resultC, 0, sizeof(resultC));
  13528. XMEMSET(resultP, 0, sizeof(resultP));
  13529. wc_AesGcmSetKey(enc, k1, (word32)k1Sz);
  13530. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13531. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  13532. resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  13533. #if defined(WOLFSSL_ASYNC_CRYPT)
  13534. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13535. #endif
  13536. if (ret != 0)
  13537. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13538. #ifndef HAVE_RENESAS_SYNC
  13539. if (XMEMCMP(c1, resultC, sizeof(c1)))
  13540. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13541. if (XMEMCMP(t1, resultT + 1, sizeof(t1) - 1))
  13542. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13543. #endif
  13544. #ifdef HAVE_AES_DECRYPT
  13545. ret = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(p),
  13546. iv1, sizeof(iv1), resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  13547. #if defined(WOLFSSL_ASYNC_CRYPT)
  13548. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13549. #endif
  13550. if (ret != 0)
  13551. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13552. if (XMEMCMP(p, resultP, sizeof(p)))
  13553. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13554. #endif /* HAVE_AES_DECRYPT */
  13555. #endif /* WOLFSSL_AES_256 */
  13556. #if !defined(HAVE_FIPS) || \
  13557. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  13558. /* Test encrypt with internally generated IV */
  13559. #if defined(WOLFSSL_AES_256) && !(defined(WC_NO_RNG) || defined(HAVE_SELFTEST)) \
  13560. && !(defined(WOLF_CRYPTO_CB) && defined(HAVE_CAVIUM_OCTEON_SYNC))
  13561. {
  13562. WC_RNG rng;
  13563. byte randIV[12];
  13564. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  13565. if (ret != 0)
  13566. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13567. XMEMSET(randIV, 0, sizeof(randIV));
  13568. XMEMSET(resultT, 0, sizeof(resultT));
  13569. XMEMSET(resultC, 0, sizeof(resultC));
  13570. XMEMSET(resultP, 0, sizeof(resultP));
  13571. wc_AesGcmSetKey(enc, k1, (word32)k1Sz);
  13572. ret = wc_AesGcmSetIV(enc, sizeof(randIV), NULL, 0, &rng);
  13573. if (ret != 0)
  13574. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13575. ret = wc_AesGcmEncrypt_ex(enc,
  13576. resultC, p, sizeof(p),
  13577. randIV, sizeof(randIV),
  13578. resultT, sizeof(t1),
  13579. a, sizeof(a));
  13580. #if defined(WOLFSSL_ASYNC_CRYPT)
  13581. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13582. #endif
  13583. if (ret != 0)
  13584. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13585. /* Check the IV has been set. */
  13586. {
  13587. word32 i, ivSum = 0;
  13588. for (i = 0; i < sizeof(randIV); i++)
  13589. ivSum += randIV[i];
  13590. if (ivSum == 0)
  13591. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13592. }
  13593. #ifdef HAVE_AES_DECRYPT
  13594. wc_AesGcmSetKey(dec, k1, (word32)k1Sz);
  13595. ret = wc_AesGcmSetIV(dec, sizeof(randIV), NULL, 0, &rng);
  13596. if (ret != 0)
  13597. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13598. ret = wc_AesGcmDecrypt(dec,
  13599. resultP, resultC, sizeof(c1),
  13600. randIV, sizeof(randIV),
  13601. resultT, sizeof(t1),
  13602. a, sizeof(a));
  13603. #if defined(WOLFSSL_ASYNC_CRYPT)
  13604. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13605. #endif
  13606. if (ret != 0)
  13607. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13608. if (XMEMCMP(p, resultP, sizeof(p)))
  13609. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13610. #endif /* HAVE_AES_DECRYPT */
  13611. wc_FreeRng(&rng);
  13612. }
  13613. #endif /* WOLFSSL_AES_256 && !(WC_NO_RNG || HAVE_SELFTEST) */
  13614. #endif /* HAVE_FIPS_VERSION >= 2 */
  13615. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  13616. #ifdef WOLFSSL_AES_256
  13617. #ifdef WOLFSSL_AESGCM_STREAM
  13618. ret = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  13619. if (ret != 0)
  13620. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13621. ret = wc_AesGcmEncryptUpdate(enc, resultC, p, sizeof(p), a, sizeof(a));
  13622. if (ret != 0)
  13623. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13624. ret = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  13625. if (ret != 0)
  13626. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13627. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  13628. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13629. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  13630. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13631. #ifdef HAVE_AES_DECRYPT
  13632. ret = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  13633. if (ret != 0)
  13634. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13635. ret = wc_AesGcmDecryptUpdate(enc, resultP, c1, sizeof(c1), a, sizeof(a));
  13636. if (ret != 0)
  13637. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13638. ret = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  13639. if (ret != 0)
  13640. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13641. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  13642. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13643. #endif
  13644. /* alen is the size to pass in with each update. */
  13645. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  13646. ret = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  13647. if (ret != 0)
  13648. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13649. /* plen is the offset into AAD to update with. */
  13650. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  13651. int len = sizeof(a) - plen;
  13652. if (len > alen) len = alen;
  13653. ret = wc_AesGcmEncryptUpdate(enc, NULL, NULL, 0, a + plen, (word32)len);
  13654. if (ret != 0)
  13655. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13656. }
  13657. /* plen is the offset into plaintext to update with. */
  13658. for (plen = 0; plen < (int)sizeof(p); plen += alen) {
  13659. int len = sizeof(p) - plen;
  13660. if (len > alen) len = alen;
  13661. ret = wc_AesGcmEncryptUpdate(enc, resultC + plen, p + plen, (word32)len,
  13662. NULL, 0);
  13663. if (ret != 0)
  13664. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13665. }
  13666. ret = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  13667. if (ret != 0)
  13668. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13669. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  13670. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13671. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  13672. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13673. }
  13674. #ifdef HAVE_AES_DECRYPT
  13675. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  13676. ret = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  13677. if (ret != 0)
  13678. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13679. /* plen is the offset into AAD to update with. */
  13680. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  13681. int len = sizeof(a) - plen;
  13682. if (len > alen) len = alen;
  13683. ret = wc_AesGcmDecryptUpdate(enc, NULL, NULL, 0, a + plen, (word32)len);
  13684. if (ret != 0)
  13685. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13686. }
  13687. /* plen is the offset into cipher text to update with. */
  13688. for (plen = 0; plen < (int)sizeof(c1); plen += alen) {
  13689. int len = sizeof(c1) - plen;
  13690. if (len > alen) len = alen;
  13691. ret = wc_AesGcmDecryptUpdate(enc, resultP + plen, c1 + plen, (word32)len,
  13692. NULL, 0);
  13693. if (ret != 0)
  13694. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13695. }
  13696. ret = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  13697. if (ret != 0)
  13698. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13699. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  13700. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13701. }
  13702. #endif /* HAVE_AES_DECRYPT */
  13703. #ifdef BENCH_AESGCM_LARGE
  13704. /* setup test buffer */
  13705. ret = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  13706. if (ret != 0)
  13707. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13708. ret = wc_AesGcmEncryptUpdate(enc, large_output, large_input,
  13709. BENCH_AESGCM_LARGE, a, sizeof(a));
  13710. if (ret != 0)
  13711. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13712. ret = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  13713. if (ret != 0)
  13714. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13715. #ifdef HAVE_AES_DECRYPT
  13716. ret = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  13717. if (ret != 0)
  13718. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13719. ret = wc_AesGcmDecryptUpdate(enc, large_outdec, large_output,
  13720. BENCH_AESGCM_LARGE, a, sizeof(a));
  13721. if (ret != 0)
  13722. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13723. ret = wc_AesGcmDecryptFinal(enc, resultT, sizeof(t1));
  13724. if (ret != 0)
  13725. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13726. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  13727. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13728. #endif /* HAVE_AES_DECRYPT */
  13729. #endif /* BENCH_AESGCM_LARGE */
  13730. #endif /* WOLFSSL_AESGCM_STREAM */
  13731. #endif /* WOLFSSL_AES_256 */
  13732. #endif /* !WOLFSSL_AFALG_XILINX_AES && !WOLFSSL_XILINX_CRYPT */
  13733. wc_AesFree(enc);
  13734. wc_AesFree(dec);
  13735. ret = 0;
  13736. out:
  13737. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  13738. !defined(WOLFSSL_NO_MALLOC)
  13739. if (large_input)
  13740. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13741. if (large_output)
  13742. XFREE(large_output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13743. if (large_outdec)
  13744. XFREE(large_outdec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13745. #endif
  13746. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13747. if (enc)
  13748. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  13749. if (dec)
  13750. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  13751. #endif
  13752. return ret;
  13753. }
  13754. #ifdef WOLFSSL_AES_128
  13755. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t gmac_test(void)
  13756. {
  13757. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13758. Gmac *gmac;
  13759. #else
  13760. Gmac gmac[1];
  13761. #endif
  13762. wc_test_ret_t ret;
  13763. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  13764. {
  13765. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  13766. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  13767. };
  13768. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  13769. {
  13770. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  13771. 0xe2, 0x8c, 0x8f, 0x16
  13772. };
  13773. WOLFSSL_SMALL_STACK_STATIC const byte a1[] =
  13774. {
  13775. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  13776. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  13777. };
  13778. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  13779. {
  13780. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  13781. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  13782. };
  13783. #if (!defined(HAVE_FIPS) || \
  13784. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  13785. /* FIPS builds only allow 16-byte auth tags. */
  13786. /* This sample uses a 15-byte auth tag. */
  13787. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  13788. {
  13789. 0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
  13790. 0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
  13791. };
  13792. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  13793. {
  13794. 0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
  13795. 0x1a, 0x60, 0x24, 0xa7
  13796. };
  13797. WOLFSSL_SMALL_STACK_STATIC const byte a2[] =
  13798. {
  13799. 0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
  13800. 0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
  13801. };
  13802. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  13803. {
  13804. 0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
  13805. 0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
  13806. };
  13807. #endif
  13808. byte tag[16];
  13809. WOLFSSL_ENTER("gmac_test");
  13810. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13811. if ((gmac = (Gmac *)XMALLOC(sizeof *gmac, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  13812. return WC_TEST_RET_ENC_ERRNO;
  13813. #endif
  13814. XMEMSET(gmac, 0, sizeof *gmac); /* clear context */
  13815. (void)wc_AesInit(&gmac->aes, HEAP_HINT, INVALID_DEVID); /* Make sure devId updated */
  13816. XMEMSET(tag, 0, sizeof(tag));
  13817. wc_GmacSetKey(gmac, k1, sizeof(k1));
  13818. wc_GmacUpdate(gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
  13819. if (XMEMCMP(t1, tag, sizeof(t1)) != 0)
  13820. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13821. #if (!defined(HAVE_FIPS) || \
  13822. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) )
  13823. XMEMSET(tag, 0, sizeof(tag));
  13824. wc_GmacSetKey(gmac, k2, sizeof(k2));
  13825. wc_GmacUpdate(gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
  13826. if (XMEMCMP(t2, tag, sizeof(t2)) != 0)
  13827. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13828. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && !defined(NO_AES_DECRYPT)
  13829. {
  13830. WOLFSSL_SMALL_STACK_STATIC const byte badT[] =
  13831. {
  13832. 0xde, 0xad, 0xbe, 0xef, 0x17, 0x2e, 0xd0, 0x43,
  13833. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  13834. };
  13835. WC_RNG rng;
  13836. byte iv[12];
  13837. #ifndef HAVE_FIPS
  13838. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  13839. if (ret != 0)
  13840. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13841. #else
  13842. ret = wc_InitRng(&rng);
  13843. if (ret != 0)
  13844. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13845. #endif
  13846. ret = wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  13847. t1, sizeof(t1));
  13848. if (ret != 0)
  13849. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13850. ret = wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  13851. badT, sizeof(badT));
  13852. if (ret != AES_GCM_AUTH_E)
  13853. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13854. ret = wc_GmacVerify(k2, sizeof(k2), iv2, sizeof(iv2), a2, sizeof(a2),
  13855. t2, sizeof(t2));
  13856. if (ret != 0)
  13857. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13858. XMEMSET(tag, 0, sizeof(tag));
  13859. XMEMSET(iv, 0, sizeof(iv));
  13860. ret = wc_Gmac(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  13861. tag, sizeof(tag), &rng);
  13862. if (ret != 0)
  13863. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13864. ret = wc_GmacVerify(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  13865. tag, sizeof(tag));
  13866. if (ret != 0)
  13867. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13868. wc_FreeRng(&rng);
  13869. }
  13870. #endif /* !WC_NO_RNG && !HAVE_SELFTEST && !NO_AES_DECRYPT */
  13871. #endif /* HAVE_FIPS */
  13872. ret = 0;
  13873. out:
  13874. wc_AesFree(&gmac->aes);
  13875. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13876. XFREE(gmac, HEAP_HINT, DYNAMIC_TYPE_AES);
  13877. #endif
  13878. return ret;
  13879. }
  13880. #endif /* WOLFSSL_AES_128 */
  13881. #endif /* HAVE_AESGCM */
  13882. #if defined(HAVE_AESCCM)
  13883. #if defined(WOLFSSL_AES_256)
  13884. static wc_test_ret_t aesccm_256_test(void)
  13885. {
  13886. wc_test_ret_t ret;
  13887. /* Test vectors from NIST AES CCM 256-bit CAST Example #1 */
  13888. WOLFSSL_SMALL_STACK_STATIC const byte in_key[32] = {
  13889. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  13890. 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
  13891. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  13892. 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F
  13893. };
  13894. WOLFSSL_SMALL_STACK_STATIC const byte in_nonce[7] = {
  13895. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
  13896. WOLFSSL_SMALL_STACK_STATIC const byte in_auth[8] = {
  13897. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  13898. WOLFSSL_SMALL_STACK_STATIC const byte in_plaintext[4] = {
  13899. 0x20, 0x21, 0x22, 0x23};
  13900. WOLFSSL_SMALL_STACK_STATIC const byte exp_ciphertext[4] = {
  13901. 0x8A, 0xB1, 0xA8, 0x74};
  13902. WOLFSSL_SMALL_STACK_STATIC const byte exp_tag[4] = {
  13903. 0x95, 0xFC, 0x08, 0x20};
  13904. byte output[sizeof(in_plaintext)];
  13905. byte atag[sizeof(exp_tag)];
  13906. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13907. Aes* aes = (Aes*)XMALLOC(sizeof(Aes), HEAP_HINT, DYNAMIC_TYPE_AES);
  13908. if (aes == NULL) {
  13909. return MEMORY_E;
  13910. }
  13911. #else
  13912. Aes aes[1];
  13913. #endif
  13914. ret = wc_AesInit(aes, HEAP_HINT, devId);
  13915. if (ret == 0) {
  13916. ret = wc_AesCcmSetKey(aes, in_key, sizeof(in_key));
  13917. }
  13918. if (ret == 0) {
  13919. ret = wc_AesCcmEncrypt(aes, output, in_plaintext, sizeof(in_plaintext),
  13920. in_nonce, sizeof(in_nonce),
  13921. atag, sizeof(atag),
  13922. in_auth, sizeof(in_auth));
  13923. }
  13924. /* Verify we produce the proper ciphertext and tag */
  13925. if (ret == 0 &&
  13926. (XMEMCMP(output, exp_ciphertext, sizeof(output)) ||
  13927. XMEMCMP(atag, exp_tag, sizeof(atag)))) {
  13928. ret = WC_TEST_RET_ENC_NC;
  13929. }
  13930. #ifdef HAVE_AES_DECRYPT
  13931. if (ret == 0) {
  13932. /* decrypt inline */
  13933. ret = wc_AesCcmDecrypt(aes, output, output, sizeof(output),
  13934. in_nonce, sizeof(in_nonce),
  13935. atag, sizeof(atag),
  13936. in_auth, sizeof(in_auth));
  13937. }
  13938. /* Verify decryption was successful */
  13939. if (ret == 0 &&
  13940. XMEMCMP(output, in_plaintext, sizeof(output))) {
  13941. ret = WC_TEST_RET_ENC_NC;
  13942. }
  13943. #endif
  13944. wc_AesFree(aes);
  13945. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13946. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  13947. #endif
  13948. return ret;
  13949. }
  13950. #endif /* WOLFSSL_AES_256 */
  13951. #if defined(WOLFSSL_AES_128)
  13952. static wc_test_ret_t aesccm_128_test(void)
  13953. {
  13954. wc_test_ret_t ret;
  13955. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13956. Aes *enc;
  13957. #else
  13958. Aes enc[1];
  13959. #endif
  13960. /* key */
  13961. WOLFSSL_SMALL_STACK_STATIC const byte k[] =
  13962. {
  13963. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  13964. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  13965. };
  13966. /* nonce */
  13967. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  13968. {
  13969. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  13970. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  13971. };
  13972. /* plaintext */
  13973. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  13974. {
  13975. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  13976. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  13977. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  13978. };
  13979. /* plaintext - long */
  13980. WOLFSSL_SMALL_STACK_STATIC const byte pl[] =
  13981. {
  13982. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  13983. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  13984. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  13985. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  13986. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  13987. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  13988. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  13989. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  13990. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  13991. 0x50
  13992. };
  13993. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  13994. {
  13995. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  13996. };
  13997. /* ciphertext */
  13998. WOLFSSL_SMALL_STACK_STATIC const byte c[] =
  13999. {
  14000. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  14001. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  14002. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  14003. };
  14004. /* tag - authentication */
  14005. WOLFSSL_SMALL_STACK_STATIC const byte t[] =
  14006. {
  14007. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  14008. };
  14009. /* ciphertext - long */
  14010. WOLFSSL_SMALL_STACK_STATIC const byte cl[] =
  14011. {
  14012. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  14013. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  14014. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0xe0,
  14015. 0x44, 0x2d, 0xbe, 0x25, 0xfa, 0x48, 0x2b, 0xa8,
  14016. 0x36, 0x0b, 0xbf, 0x01, 0xc0, 0x12, 0x45, 0xa4,
  14017. 0x82, 0x9f, 0x20, 0x6c, 0xc3, 0xd6, 0xae, 0x5b,
  14018. 0x54, 0x8d, 0xd0, 0xb1, 0x69, 0x2c, 0xec, 0x5e,
  14019. 0x95, 0xa5, 0x6b, 0x48, 0xc3, 0xc6, 0xc8, 0x9e,
  14020. 0xc7, 0x92, 0x98, 0x9d, 0x26, 0x7d, 0x2a, 0x10,
  14021. 0x0b
  14022. };
  14023. /* tag - authentication - long */
  14024. WOLFSSL_SMALL_STACK_STATIC const byte tl[] =
  14025. {
  14026. 0x89, 0xd8, 0xd2, 0x02, 0xc5, 0xcf, 0xae, 0xf4
  14027. };
  14028. /* tag - authentication - empty plaintext */
  14029. WOLFSSL_SMALL_STACK_STATIC const byte t_empty[] =
  14030. {
  14031. 0xe4, 0x28, 0x8a, 0xc3, 0x78, 0x00, 0x0f, 0xf5
  14032. };
  14033. byte t2[sizeof(t)];
  14034. byte p2[sizeof(p)];
  14035. byte c2[sizeof(c)];
  14036. byte iv2[sizeof(iv)];
  14037. byte pl2[sizeof(pl)];
  14038. byte cl2[sizeof(cl)];
  14039. byte tl2[sizeof(tl)];
  14040. byte t_empty2[sizeof(t_empty)];
  14041. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14042. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  14043. return WC_TEST_RET_ENC_ERRNO;
  14044. #endif
  14045. XMEMSET(enc, 0, sizeof *enc); /* clear context */
  14046. XMEMSET(t2, 0, sizeof(t2));
  14047. XMEMSET(c2, 0, sizeof(c2));
  14048. XMEMSET(p2, 0, sizeof(p2));
  14049. ret = wc_AesInit(enc, HEAP_HINT, devId);
  14050. if (ret != 0)
  14051. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14052. ret = wc_AesCcmSetKey(enc, k, sizeof(k));
  14053. if (ret != 0)
  14054. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14055. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  14056. ret = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  14057. t2, sizeof(t2), a, sizeof(a));
  14058. if (ret != 0)
  14059. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14060. if (XMEMCMP(c, c2, sizeof(c2)))
  14061. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14062. if (XMEMCMP(t, t2, sizeof(t2)))
  14063. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14064. #ifdef HAVE_AES_DECRYPT
  14065. ret = wc_AesCcmDecrypt(enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  14066. t2, sizeof(t2), a, sizeof(a));
  14067. if (ret != 0)
  14068. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14069. if (XMEMCMP(p, p2, sizeof(p2)))
  14070. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14071. /* Test the authentication failure */
  14072. t2[0]++; /* Corrupt the authentication tag. */
  14073. ret = wc_AesCcmDecrypt(enc, p2, c, sizeof(p2), iv, sizeof(iv),
  14074. t2, sizeof(t2), a, sizeof(a));
  14075. if (ret == 0)
  14076. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14077. /* Clear c2 to compare against p2. p2 should be set to zero in case of
  14078. * authentication fail. */
  14079. XMEMSET(c2, 0, sizeof(c2));
  14080. if (XMEMCMP(p2, c2, sizeof(p2)))
  14081. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14082. #endif
  14083. wc_AesFree(enc);
  14084. XMEMSET(enc, 0, sizeof(Aes)); /* clear context */
  14085. XMEMSET(t2, 0, sizeof(t2));
  14086. XMEMSET(c2, 0, sizeof(c2));
  14087. XMEMSET(p2, 0, sizeof(p2));
  14088. XMEMSET(iv2, 0, sizeof(iv2));
  14089. ret = wc_AesInit(enc, HEAP_HINT, devId);
  14090. if (ret != 0)
  14091. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14092. #ifndef HAVE_SELFTEST
  14093. /* selftest build does not have wc_AesCcmSetNonce() or
  14094. * wc_AesCcmEncrypt_ex() */
  14095. ret = wc_AesCcmSetKey(enc, k, sizeof(k));
  14096. if (ret != 0)
  14097. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14098. ret = wc_AesCcmSetNonce(enc, iv, sizeof(iv));
  14099. if (ret != 0)
  14100. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14101. ret = wc_AesCcmEncrypt_ex(enc, c2, p, sizeof(c2), iv2, sizeof(iv2),
  14102. t2, sizeof(t2), a, sizeof(a));
  14103. if (ret != 0)
  14104. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14105. if (XMEMCMP(iv, iv2, sizeof(iv2)))
  14106. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14107. if (XMEMCMP(c, c2, sizeof(c2)))
  14108. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14109. if (XMEMCMP(t, t2, sizeof(t2)))
  14110. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14111. #endif
  14112. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  14113. /* test fail on invalid IV sizes */
  14114. ret = wc_AesCcmSetKey(enc, k, sizeof(k));
  14115. if (ret != 0)
  14116. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14117. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  14118. ret = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  14119. t2, 1, a, sizeof(a));
  14120. if (ret == 0) {
  14121. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14122. }
  14123. #endif
  14124. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  14125. ret = wc_AesCcmEncrypt(enc, cl2, pl, sizeof(cl2), iv, sizeof(iv),
  14126. tl2, sizeof(tl2), a, sizeof(a));
  14127. if (ret != 0)
  14128. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14129. if (XMEMCMP(cl, cl2, sizeof(cl2)))
  14130. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14131. if (XMEMCMP(tl, tl2, sizeof(tl2)))
  14132. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14133. #ifdef HAVE_AES_DECRYPT
  14134. ret = wc_AesCcmDecrypt(enc, pl2, cl2, sizeof(pl2), iv, sizeof(iv),
  14135. tl2, sizeof(tl2), a, sizeof(a));
  14136. if (ret != 0)
  14137. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14138. if (XMEMCMP(pl, pl2, sizeof(pl2)))
  14139. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14140. #endif
  14141. /* test empty message as null input or output with nonzero inSz. */
  14142. ret = wc_AesCcmEncrypt(enc, pl2 /* out */, NULL /* in */, 1 /* inSz */,
  14143. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  14144. a, sizeof(a));
  14145. if (ret != BAD_FUNC_ARG)
  14146. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14147. ret = wc_AesCcmEncrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  14148. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  14149. a, sizeof(a));
  14150. if (ret != BAD_FUNC_ARG)
  14151. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14152. #ifdef HAVE_AES_DECRYPT
  14153. ret = wc_AesCcmDecrypt(enc, pl2, NULL /* in */, 1 /* inSz */,
  14154. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  14155. sizeof(a));
  14156. if (ret != BAD_FUNC_ARG)
  14157. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14158. ret = wc_AesCcmDecrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  14159. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  14160. sizeof(a));
  14161. if (ret != BAD_FUNC_ARG)
  14162. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14163. #endif
  14164. /* test empty message as null input and output with zero inSz --
  14165. * must either succeed, or fail early with BAD_FUNC_ARG.
  14166. */
  14167. ret = wc_AesCcmEncrypt(enc, NULL /* out */, NULL /* in */, 0 /* inSz */,
  14168. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  14169. a, sizeof(a));
  14170. if (ret != BAD_FUNC_ARG) {
  14171. if (ret != 0)
  14172. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14173. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  14174. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14175. #ifdef HAVE_AES_DECRYPT
  14176. ret = wc_AesCcmDecrypt(enc, NULL /* out */, NULL /* in */,
  14177. 0 /* inSz */, iv, sizeof(iv), t_empty2,
  14178. sizeof(t_empty2), a, sizeof(a));
  14179. if (ret != 0)
  14180. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14181. #endif
  14182. }
  14183. /* test empty message as zero-length string -- must work. */
  14184. ret = wc_AesCcmEncrypt(enc, pl2, (const byte *)"", 0 /* inSz */, iv,
  14185. sizeof(iv), t_empty2, sizeof(t_empty2), a,
  14186. sizeof(a));
  14187. if (ret != 0)
  14188. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14189. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  14190. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14191. #ifdef HAVE_AES_DECRYPT
  14192. ret = wc_AesCcmDecrypt(enc, pl2, (const byte *)"", 0 /* inSz */,
  14193. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  14194. sizeof(a));
  14195. if (ret != 0)
  14196. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14197. #endif
  14198. wc_AesFree(enc);
  14199. ret = 0;
  14200. out:
  14201. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14202. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  14203. #endif
  14204. return ret;
  14205. }
  14206. #endif /* WOLFSSL_AES_128 */
  14207. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesccm_test(void)
  14208. {
  14209. wc_test_ret_t ret = 0;
  14210. WOLFSSL_ENTER("aesccm_test");
  14211. #ifdef WOLFSSL_AES_128
  14212. if (ret == 0)
  14213. ret = aesccm_128_test();
  14214. #endif
  14215. #ifdef WOLFSSL_AES_256
  14216. if (ret == 0)
  14217. ret = aesccm_256_test();
  14218. #endif
  14219. return ret;
  14220. }
  14221. #endif /* HAVE_AESCCM */
  14222. #if defined(WOLFSSL_AES_EAX) && \
  14223. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  14224. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_eax_test(void)
  14225. {
  14226. typedef struct {
  14227. byte key[AES_256_KEY_SIZE];
  14228. int key_length;
  14229. byte iv[AES_BLOCK_SIZE];
  14230. int iv_length;
  14231. byte aad[AES_BLOCK_SIZE * 2];
  14232. int aad_length;
  14233. byte msg[AES_BLOCK_SIZE * 2];
  14234. int msg_length;
  14235. byte ct[AES_BLOCK_SIZE * 2];
  14236. int ct_length;
  14237. byte tag[AES_BLOCK_SIZE];
  14238. int tag_length;
  14239. int valid;
  14240. } AadVector;
  14241. /* A small selection of Google wycheproof vectors that use vectors
  14242. * from the original paper: eprint.iacr.org/2003/069
  14243. * https://github.com/google/wycheproof/blob/master/testvectors/aes_eax_test.json
  14244. */
  14245. WOLFSSL_SMALL_STACK_STATIC const AadVector vectors[] = {
  14246. /* Vector from paper - empty message with auth data */
  14247. {
  14248. /* key, key length */
  14249. {0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f,
  14250. 0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78}, 16,
  14251. /* iv, iv length */
  14252. {0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07,
  14253. 0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3}, 16,
  14254. /* aad, aad length */
  14255. {0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b}, 8,
  14256. /* msg, msg length */
  14257. {0}, 0,
  14258. /* ct, ct length */
  14259. {0}, 0,
  14260. /* tag, tag length */
  14261. {0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2,
  14262. 0x7b, 0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01}, 16,
  14263. /* valid */
  14264. 1,
  14265. },
  14266. /* Vector from paper - no auth data, valid auth tag */
  14267. {
  14268. /* key, key length */
  14269. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14270. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  14271. /* iv , iv length */
  14272. {0x3c, 0x8c, 0xc2, 0x97, 0x0a, 0x00, 0x8f, 0x75,
  14273. 0xcc, 0x5b, 0xea, 0xe2, 0x84, 0x72, 0x58, 0xc2}, 16,
  14274. /* aad, aad length */
  14275. {0}, 0,
  14276. /* msg, msg length */
  14277. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  14278. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  14279. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  14280. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  14281. /* ct, ct length */
  14282. {0x3c, 0x44, 0x1f, 0x32, 0xce, 0x07, 0x82, 0x23,
  14283. 0x64, 0xd7, 0xa2, 0x99, 0x0e, 0x50, 0xbb, 0x13,
  14284. 0xd7, 0xb0, 0x2a, 0x26, 0x96, 0x9e, 0x4a, 0x93,
  14285. 0x7e, 0x5e, 0x90, 0x73, 0xb0, 0xd9, 0xc9, 0x68}, 32,
  14286. /* tag, tag length */
  14287. {0xdb, 0x90, 0xbd, 0xb3, 0xda, 0x3d, 0x00, 0xaf,
  14288. 0xd0, 0xfc, 0x6a, 0x83, 0x55, 0x1d, 0xa9, 0x5e}, 16,
  14289. /* valid */
  14290. 1,
  14291. },
  14292. /* Vector from paper - no auth data with invalid auth tag */
  14293. {
  14294. /* key, key length */
  14295. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14296. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  14297. /* iv, iv length */
  14298. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  14299. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  14300. /* aad, aad length */
  14301. {0}, 0,
  14302. /* msg, msg length */
  14303. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  14304. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  14305. /* ct , ct length */
  14306. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  14307. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  14308. /* tag, tag length */
  14309. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  14310. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  14311. /* valid */
  14312. 0,
  14313. },
  14314. };
  14315. WOLFSSL_SMALL_STACK_STATIC byte ciphertext[sizeof(vectors[0].ct)];
  14316. WOLFSSL_SMALL_STACK_STATIC byte authtag[sizeof(vectors[0].tag)];
  14317. int i;
  14318. int len;
  14319. wc_test_ret_t ret;
  14320. WOLFSSL_ENTER("aes_eax_test");
  14321. for (i = 0; i < (int)(sizeof(vectors)/sizeof(vectors[0])); i++) {
  14322. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  14323. len = sizeof(authtag);
  14324. ret = wc_AesEaxEncryptAuth(vectors[i].key, (word32)vectors[i].key_length,
  14325. ciphertext,
  14326. vectors[i].msg, (word32)vectors[i].msg_length,
  14327. vectors[i].iv, (word32)vectors[i].iv_length,
  14328. authtag, (word32)len,
  14329. vectors[i].aad, (word32)vectors[i].aad_length);
  14330. if (ret != 0) {
  14331. return WC_TEST_RET_ENC_EC(ret);
  14332. }
  14333. /* check ciphertext matches vector */
  14334. if (XMEMCMP(ciphertext, vectors[i].ct, (size_t)vectors[i].ct_length)) {
  14335. return WC_TEST_RET_ENC_NC;
  14336. }
  14337. /* check that tag matches vector only for vectors marked as valid */
  14338. ret = XMEMCMP(authtag, vectors[i].tag, len);
  14339. if (vectors[i].valid == 1 && ret != 0 ) {
  14340. return WC_TEST_RET_ENC_NC;
  14341. }
  14342. else if (vectors[i].valid == 0 && ret == 0) {
  14343. return WC_TEST_RET_ENC_NC;
  14344. }
  14345. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  14346. ret = wc_AesEaxDecryptAuth(vectors[i].key, (word32)vectors[i].key_length,
  14347. ciphertext,
  14348. vectors[i].ct, (word32)vectors[i].ct_length,
  14349. vectors[i].iv, (word32)vectors[i].iv_length,
  14350. authtag, (word32)len,
  14351. vectors[i].aad, (word32)vectors[i].aad_length);
  14352. if (ret != 0) {
  14353. return WC_TEST_RET_ENC_EC(ret);
  14354. }
  14355. /* check decrypted ciphertext matches vector plaintext */
  14356. if (XMEMCMP(ciphertext, vectors[i].msg, (size_t)vectors[i].msg_length)) {
  14357. return WC_TEST_RET_ENC_NC;
  14358. }
  14359. }
  14360. return 0;
  14361. }
  14362. #endif /* WOLFSSL_AES_EAX */
  14363. #ifdef HAVE_AES_KEYWRAP
  14364. #define MAX_KEYWRAP_TEST_OUTLEN 40
  14365. #define MAX_KEYWRAP_TEST_PLAINLEN 32
  14366. typedef struct keywrapVector {
  14367. const byte* kek;
  14368. const byte* data;
  14369. const byte* verify;
  14370. word32 kekLen;
  14371. word32 dataLen;
  14372. word32 verifyLen;
  14373. } keywrapVector;
  14374. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aeskeywrap_test(void)
  14375. {
  14376. int wrapSz, plainSz, testSz, i;
  14377. /* test vectors from RFC 3394 (kek, data, verify) */
  14378. #ifdef WOLFSSL_AES_128
  14379. /* Wrap 128 bits of Key Data with a 128-bit KEK */
  14380. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  14381. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14382. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  14383. };
  14384. WOLFSSL_SMALL_STACK_STATIC const byte d1[] = {
  14385. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14386. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  14387. };
  14388. WOLFSSL_SMALL_STACK_STATIC const byte v1[] = {
  14389. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  14390. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  14391. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  14392. };
  14393. #endif /* WOLFSSL_AES_128 */
  14394. #ifdef WOLFSSL_AES_192
  14395. /* Wrap 128 bits of Key Data with a 192-bit KEK */
  14396. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  14397. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14398. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  14399. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  14400. };
  14401. WOLFSSL_SMALL_STACK_STATIC const byte d2[] = {
  14402. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14403. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  14404. };
  14405. WOLFSSL_SMALL_STACK_STATIC const byte v2[] = {
  14406. 0x96, 0x77, 0x8B, 0x25, 0xAE, 0x6C, 0xA4, 0x35,
  14407. 0xF9, 0x2B, 0x5B, 0x97, 0xC0, 0x50, 0xAE, 0xD2,
  14408. 0x46, 0x8A, 0xB8, 0xA1, 0x7A, 0xD8, 0x4E, 0x5D
  14409. };
  14410. #endif
  14411. #ifdef WOLFSSL_AES_256
  14412. /* Wrap 128 bits of Key Data with a 256-bit KEK */
  14413. WOLFSSL_SMALL_STACK_STATIC const byte k3[] = {
  14414. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14415. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  14416. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  14417. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  14418. };
  14419. WOLFSSL_SMALL_STACK_STATIC const byte d3[] = {
  14420. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14421. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  14422. };
  14423. WOLFSSL_SMALL_STACK_STATIC const byte v3[] = {
  14424. 0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2,
  14425. 0x63, 0xE9, 0x77, 0x79, 0x05, 0x81, 0x8A, 0x2A,
  14426. 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7
  14427. };
  14428. #endif
  14429. #ifdef WOLFSSL_AES_192
  14430. /* Wrap 192 bits of Key Data with a 192-bit KEK */
  14431. WOLFSSL_SMALL_STACK_STATIC const byte k4[] = {
  14432. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14433. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  14434. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  14435. };
  14436. WOLFSSL_SMALL_STACK_STATIC const byte d4[] = {
  14437. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14438. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  14439. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  14440. };
  14441. WOLFSSL_SMALL_STACK_STATIC const byte v4[] = {
  14442. 0x03, 0x1D, 0x33, 0x26, 0x4E, 0x15, 0xD3, 0x32,
  14443. 0x68, 0xF2, 0x4E, 0xC2, 0x60, 0x74, 0x3E, 0xDC,
  14444. 0xE1, 0xC6, 0xC7, 0xDD, 0xEE, 0x72, 0x5A, 0x93,
  14445. 0x6B, 0xA8, 0x14, 0x91, 0x5C, 0x67, 0x62, 0xD2
  14446. };
  14447. #endif
  14448. #ifdef WOLFSSL_AES_256
  14449. /* Wrap 192 bits of Key Data with a 256-bit KEK */
  14450. WOLFSSL_SMALL_STACK_STATIC const byte k5[] = {
  14451. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14452. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  14453. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  14454. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  14455. };
  14456. WOLFSSL_SMALL_STACK_STATIC const byte d5[] = {
  14457. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14458. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  14459. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  14460. };
  14461. WOLFSSL_SMALL_STACK_STATIC const byte v5[] = {
  14462. 0xA8, 0xF9, 0xBC, 0x16, 0x12, 0xC6, 0x8B, 0x3F,
  14463. 0xF6, 0xE6, 0xF4, 0xFB, 0xE3, 0x0E, 0x71, 0xE4,
  14464. 0x76, 0x9C, 0x8B, 0x80, 0xA3, 0x2C, 0xB8, 0x95,
  14465. 0x8C, 0xD5, 0xD1, 0x7D, 0x6B, 0x25, 0x4D, 0xA1
  14466. };
  14467. /* Wrap 256 bits of Key Data with a 256-bit KEK */
  14468. WOLFSSL_SMALL_STACK_STATIC const byte k6[] = {
  14469. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14470. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  14471. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  14472. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  14473. };
  14474. WOLFSSL_SMALL_STACK_STATIC const byte d6[] = {
  14475. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14476. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  14477. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14478. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  14479. };
  14480. WOLFSSL_SMALL_STACK_STATIC const byte v6[] = {
  14481. 0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4,
  14482. 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, 0xF8, 0x26,
  14483. 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26,
  14484. 0xCB, 0xC7, 0xF0, 0xE7, 0x1A, 0x99, 0xF4, 0x3B,
  14485. 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21
  14486. };
  14487. #endif /* WOLFSSL_AES_256 */
  14488. byte output[MAX_KEYWRAP_TEST_OUTLEN];
  14489. byte plain [MAX_KEYWRAP_TEST_PLAINLEN];
  14490. const keywrapVector test_wrap[] =
  14491. {
  14492. #ifdef WOLFSSL_AES_128
  14493. {k1, d1, v1, sizeof(k1), sizeof(d1), sizeof(v1)},
  14494. #endif
  14495. #ifdef WOLFSSL_AES_192
  14496. {k2, d2, v2, sizeof(k2), sizeof(d2), sizeof(v2)},
  14497. #endif
  14498. #ifdef WOLFSSL_AES_256
  14499. {k3, d3, v3, sizeof(k3), sizeof(d3), sizeof(v3)},
  14500. #endif
  14501. #ifdef WOLFSSL_AES_192
  14502. {k4, d4, v4, sizeof(k4), sizeof(d4), sizeof(v4)},
  14503. #endif
  14504. #ifdef WOLFSSL_AES_256
  14505. {k5, d5, v5, sizeof(k5), sizeof(d5), sizeof(v5)},
  14506. {k6, d6, v6, sizeof(k6), sizeof(d6), sizeof(v6)}
  14507. #endif
  14508. };
  14509. WOLFSSL_ENTER("aeskeywrap_test");
  14510. testSz = sizeof(test_wrap) / sizeof(keywrapVector);
  14511. XMEMSET(output, 0, sizeof(output));
  14512. XMEMSET(plain, 0, sizeof(plain));
  14513. for (i = 0; i < testSz; i++) {
  14514. wrapSz = wc_AesKeyWrap(test_wrap[i].kek, test_wrap[i].kekLen,
  14515. test_wrap[i].data, test_wrap[i].dataLen,
  14516. output, sizeof(output), NULL);
  14517. if ( (wrapSz < 0) || (wrapSz != (int)test_wrap[i].verifyLen) )
  14518. return WC_TEST_RET_ENC_NC;
  14519. if (XMEMCMP(output, test_wrap[i].verify, test_wrap[i].verifyLen) != 0)
  14520. return WC_TEST_RET_ENC_NC;
  14521. plainSz = wc_AesKeyUnWrap((byte*)test_wrap[i].kek, test_wrap[i].kekLen,
  14522. output, (word32)wrapSz,
  14523. plain, sizeof(plain), NULL);
  14524. if ( (plainSz < 0) || (plainSz != (int)test_wrap[i].dataLen) )
  14525. return WC_TEST_RET_ENC_NC;
  14526. if (XMEMCMP(plain, test_wrap[i].data, test_wrap[i].dataLen) != 0)
  14527. return WC_TEST_RET_ENC_I(i);
  14528. }
  14529. return 0;
  14530. }
  14531. #endif /* HAVE_AES_KEYWRAP */
  14532. #endif /* NO_AES */
  14533. #ifdef HAVE_ARIA
  14534. void printOutput(const char *strName, unsigned char *data, unsigned int dataSz)
  14535. {
  14536. #ifndef DEBUG_WOLFSSL
  14537. (void)strName;
  14538. (void)data;
  14539. (void)dataSz;
  14540. #else
  14541. WOLFSSL_MSG_EX("%s (%d):", strName,dataSz);
  14542. WOLFSSL_BUFFER(data,dataSz);
  14543. #endif
  14544. }
  14545. WOLFSSL_TEST_SUBROUTINE int ariagcm_test(MC_ALGID algo)
  14546. {
  14547. byte data[] = TEST_STRING;
  14548. word32 dataSz = TEST_STRING_SZ;
  14549. /* Arbitrarily random long key that we will truncate to the right size */
  14550. byte key[] = { 0x1E, 0xCC, 0x95, 0xCB, 0xD3, 0x74, 0x58, 0x4F,
  14551. 0x6F, 0x8A, 0x70, 0x26, 0xF7, 0x3C, 0x8D, 0xB6,
  14552. 0xDC, 0x32, 0x76, 0x20, 0xCF, 0x05, 0x4A, 0xCF,
  14553. 0x11, 0x86, 0xCD, 0x23, 0x5E, 0xC1, 0x6E, 0x2B };
  14554. byte cipher[2*TEST_STRING_SZ], plain[TEST_STRING_SZ], ad[256], authTag[AES_BLOCK_SIZE];
  14555. word32 keySz, adSz = 256, authTagSz = sizeof(authTag);
  14556. wc_Aria aria;
  14557. int ret = 0;
  14558. WOLFSSL_ENTER("ariagcm_test");
  14559. XMEMSET((void *)&aria, 0, sizeof(aria));
  14560. ret = wc_AriaInitCrypt(&aria, algo);
  14561. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  14562. ret = wc_AriaSetKey(&aria, key);
  14563. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  14564. MC_GetObjectValue(aria.hSession, aria.hKey, key, &keySz);
  14565. printOutput("Key", key, keySz);
  14566. WC_RNG rng;
  14567. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14568. if (ret != 0)
  14569. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14570. ret = wc_AriaGcmSetIV(&aria, GCM_NONCE_MID_SZ, NULL, 0, &rng);
  14571. if (ret != 0)
  14572. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14573. wc_FreeRng(&rng);
  14574. printOutput("Plaintext", data, sizeof(data));
  14575. XMEMSET(cipher, 0, sizeof(cipher));
  14576. ret = wc_AriaEncrypt(&aria, cipher, data, dataSz,
  14577. (byte *)aria.nonce, aria.nonceSz, ad, adSz,
  14578. authTag, authTagSz);
  14579. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  14580. printOutput("Ciphertext", cipher, sizeof(cipher));
  14581. printOutput("AuthTag", authTag, sizeof(authTag));
  14582. XMEMSET(plain, 0, sizeof(plain));
  14583. ret = wc_AriaDecrypt(&aria, plain, cipher, dataSz,
  14584. (byte *)aria.nonce, aria.nonceSz, ad, adSz,
  14585. authTag, authTagSz);
  14586. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  14587. printOutput("Plaintext", plain, sizeof(plain));
  14588. if (XMEMCMP(plain, data, dataSz) != 0)
  14589. ERROR_OUT(WC_TEST_RET_ENC_NC,out);
  14590. out:
  14591. if (ret != 0) { wc_AriaFreeCrypt(&aria); }
  14592. else { ret = wc_AriaFreeCrypt(&aria); }
  14593. return ret;
  14594. }
  14595. #endif /* HAVE_ARIA */
  14596. #ifdef HAVE_CAMELLIA
  14597. enum {
  14598. CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
  14599. };
  14600. typedef struct {
  14601. int type;
  14602. const byte* plaintext;
  14603. const byte* iv;
  14604. const byte* ciphertext;
  14605. const byte* key;
  14606. word32 keySz;
  14607. int errorCode;
  14608. } test_vector_t;
  14609. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t camellia_test(void)
  14610. {
  14611. /* Camellia ECB Test Plaintext */
  14612. WOLFSSL_SMALL_STACK_STATIC const byte pte[] =
  14613. {
  14614. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  14615. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  14616. };
  14617. /* Camellia ECB Test Initialization Vector */
  14618. WOLFSSL_SMALL_STACK_STATIC const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  14619. /* Test 1: Camellia ECB 128-bit key */
  14620. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  14621. {
  14622. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  14623. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  14624. };
  14625. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  14626. {
  14627. 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  14628. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
  14629. };
  14630. /* Test 2: Camellia ECB 192-bit key */
  14631. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  14632. {
  14633. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  14634. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  14635. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  14636. };
  14637. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  14638. {
  14639. 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  14640. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
  14641. };
  14642. /* Test 3: Camellia ECB 256-bit key */
  14643. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  14644. {
  14645. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  14646. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  14647. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14648. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  14649. };
  14650. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  14651. {
  14652. 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  14653. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
  14654. };
  14655. /* Camellia CBC Test Plaintext */
  14656. WOLFSSL_SMALL_STACK_STATIC const byte ptc[] =
  14657. {
  14658. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  14659. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  14660. };
  14661. /* Camellia CBC Test Initialization Vector */
  14662. WOLFSSL_SMALL_STACK_STATIC const byte ivc[] =
  14663. {
  14664. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14665. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  14666. };
  14667. /* Test 4: Camellia-CBC 128-bit key */
  14668. WOLFSSL_SMALL_STACK_STATIC const byte k4[] =
  14669. {
  14670. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  14671. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
  14672. };
  14673. WOLFSSL_SMALL_STACK_STATIC const byte c4[] =
  14674. {
  14675. 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  14676. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
  14677. };
  14678. /* Test 5: Camellia-CBC 192-bit key */
  14679. WOLFSSL_SMALL_STACK_STATIC const byte k5[] =
  14680. {
  14681. 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  14682. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  14683. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
  14684. };
  14685. WOLFSSL_SMALL_STACK_STATIC const byte c5[] =
  14686. {
  14687. 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  14688. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
  14689. };
  14690. /* Test 6: CBC 256-bit key */
  14691. WOLFSSL_SMALL_STACK_STATIC const byte k6[] =
  14692. {
  14693. 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  14694. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  14695. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  14696. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
  14697. };
  14698. WOLFSSL_SMALL_STACK_STATIC const byte c6[] =
  14699. {
  14700. 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  14701. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
  14702. };
  14703. byte out[CAMELLIA_BLOCK_SIZE];
  14704. Camellia cam;
  14705. WOLFSSL_SMALL_STACK_STATIC const test_vector_t testVectors[] =
  14706. {
  14707. {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
  14708. {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
  14709. {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
  14710. {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
  14711. {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
  14712. {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
  14713. {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
  14714. {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
  14715. {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
  14716. {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
  14717. {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
  14718. {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
  14719. };
  14720. int i, testsSz;
  14721. int ret;
  14722. WOLFSSL_ENTER("camellia_test");
  14723. testsSz = sizeof(testVectors)/sizeof(test_vector_t);
  14724. for (i = 0; i < testsSz; i++) {
  14725. if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
  14726. testVectors[i].iv) != 0)
  14727. return testVectors[i].errorCode;
  14728. switch (testVectors[i].type) {
  14729. case CAM_ECB_ENC:
  14730. ret = wc_CamelliaEncryptDirect(&cam, out,
  14731. testVectors[i].plaintext);
  14732. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  14733. CAMELLIA_BLOCK_SIZE))
  14734. return testVectors[i].errorCode;
  14735. break;
  14736. case CAM_ECB_DEC:
  14737. ret = wc_CamelliaDecryptDirect(&cam, out,
  14738. testVectors[i].ciphertext);
  14739. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  14740. CAMELLIA_BLOCK_SIZE))
  14741. return testVectors[i].errorCode;
  14742. break;
  14743. case CAM_CBC_ENC:
  14744. ret = wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
  14745. CAMELLIA_BLOCK_SIZE);
  14746. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  14747. CAMELLIA_BLOCK_SIZE))
  14748. return testVectors[i].errorCode;
  14749. break;
  14750. case CAM_CBC_DEC:
  14751. ret = wc_CamelliaCbcDecrypt(&cam, out,
  14752. testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE);
  14753. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  14754. CAMELLIA_BLOCK_SIZE))
  14755. return testVectors[i].errorCode;
  14756. break;
  14757. default:
  14758. break;
  14759. }
  14760. }
  14761. /* Setting the IV and checking it was actually set. */
  14762. ret = wc_CamelliaSetIV(&cam, ivc);
  14763. if (ret != 0)
  14764. return WC_TEST_RET_ENC_EC(ret);
  14765. if (XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE) != 0)
  14766. return WC_TEST_RET_ENC_NC;
  14767. /* Setting the IV to NULL should be same as all zeros IV */
  14768. ret = wc_CamelliaSetIV(&cam, NULL);
  14769. if (ret != 0)
  14770. return WC_TEST_RET_ENC_EC(ret);
  14771. if (XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE) != 0)
  14772. return WC_TEST_RET_ENC_NC;
  14773. /* First parameter should never be null */
  14774. if (wc_CamelliaSetIV(NULL, NULL) == 0)
  14775. return WC_TEST_RET_ENC_NC;
  14776. /* First parameter should never be null, check it fails */
  14777. if (wc_CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
  14778. return WC_TEST_RET_ENC_NC;
  14779. /* Key should have a size of 16, 24, or 32 */
  14780. if (wc_CamelliaSetKey(&cam, k1, 0, NULL) == 0)
  14781. return WC_TEST_RET_ENC_NC;
  14782. return 0;
  14783. }
  14784. #endif /* HAVE_CAMELLIA */
  14785. #ifdef WOLFSSL_SM4
  14786. #ifdef WOLFSSL_SM4_ECB
  14787. static int sm4_ecb_test(void)
  14788. {
  14789. /* draft-ribose-cfrg-sm4-10 A.2.1.1 */
  14790. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  14791. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  14792. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  14793. };
  14794. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  14795. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  14796. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  14797. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  14798. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  14799. };
  14800. WOLFSSL_SMALL_STACK_STATIC const byte c1_ecb[] = {
  14801. 0x5E, 0xC8, 0x14, 0x3D, 0xE5, 0x09, 0xCF, 0xF7,
  14802. 0xB5, 0x17, 0x9F, 0x8F, 0x47, 0x4B, 0x86, 0x19,
  14803. 0x2F, 0x1D, 0x30, 0x5A, 0x7F, 0xB1, 0x7D, 0xF9,
  14804. 0x85, 0xF8, 0x1C, 0x84, 0x82, 0x19, 0x23, 0x04
  14805. };
  14806. wc_Sm4 sm4;
  14807. byte enc[SM4_BLOCK_SIZE * 4];
  14808. byte dec[SM4_BLOCK_SIZE * 4];
  14809. int ret;
  14810. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  14811. if (ret != 0)
  14812. return WC_TEST_RET_ENC_EC(ret);
  14813. /* Encrypt and decrypt with ECB. */
  14814. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  14815. if (ret != 0)
  14816. return WC_TEST_RET_ENC_EC(ret);
  14817. ret = wc_Sm4EcbEncrypt(&sm4, enc, p1, sizeof(p1));
  14818. if (ret != 0)
  14819. return WC_TEST_RET_ENC_EC(ret);
  14820. if (XMEMCMP(enc, c1_ecb, sizeof(c1_ecb)) != 0)
  14821. return WC_TEST_RET_ENC_NC;
  14822. ret = wc_Sm4EcbDecrypt(&sm4, dec, enc, sizeof(c1_ecb));
  14823. if (ret != 0)
  14824. return WC_TEST_RET_ENC_EC(ret);
  14825. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  14826. return WC_TEST_RET_ENC_NC;
  14827. wc_Sm4Free(&sm4);
  14828. return 0;
  14829. }
  14830. #endif
  14831. #ifdef WOLFSSL_SM4_CBC
  14832. static int sm4_cbc_test(void)
  14833. {
  14834. /* draft-ribose-cfrg-sm4-10 A.2.2.1 */
  14835. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  14836. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  14837. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  14838. };
  14839. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  14840. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  14841. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  14842. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  14843. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  14844. };
  14845. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  14846. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14847. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  14848. };
  14849. WOLFSSL_SMALL_STACK_STATIC const byte c1_cbc[] = {
  14850. 0x78, 0xEB, 0xB1, 0x1C, 0xC4, 0x0B, 0x0A, 0x48,
  14851. 0x31, 0x2A, 0xAE, 0xB2, 0x04, 0x02, 0x44, 0xCB,
  14852. 0x4C, 0xB7, 0x01, 0x69, 0x51, 0x90, 0x92, 0x26,
  14853. 0x97, 0x9B, 0x0D, 0x15, 0xDC, 0x6A, 0x8F, 0x6D
  14854. };
  14855. wc_Sm4 sm4;
  14856. byte enc[SM4_BLOCK_SIZE * 4];
  14857. byte dec[SM4_BLOCK_SIZE * 4];
  14858. int ret;
  14859. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  14860. if (ret != 0)
  14861. return WC_TEST_RET_ENC_EC(ret);
  14862. /* Encrypt and decrypt with CBC. */
  14863. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  14864. if (ret != 0)
  14865. return WC_TEST_RET_ENC_EC(ret);
  14866. ret = wc_Sm4SetIV(&sm4, i1);
  14867. if (ret != 0)
  14868. return WC_TEST_RET_ENC_EC(ret);
  14869. ret = wc_Sm4CbcEncrypt(&sm4, enc, p1, sizeof(p1));
  14870. if (ret != 0)
  14871. return WC_TEST_RET_ENC_EC(ret);
  14872. if (XMEMCMP(enc, c1_cbc, sizeof(c1_cbc)) != 0)
  14873. return WC_TEST_RET_ENC_NC;
  14874. ret = wc_Sm4SetIV(&sm4, i1);
  14875. if (ret != 0)
  14876. return WC_TEST_RET_ENC_EC(ret);
  14877. ret = wc_Sm4CbcDecrypt(&sm4, dec, enc, sizeof(c1_cbc));
  14878. if (ret != 0)
  14879. return WC_TEST_RET_ENC_EC(ret);
  14880. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  14881. return WC_TEST_RET_ENC_NC;
  14882. /* Encrypt and decrypt in-place with CBC. */
  14883. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  14884. if (ret != 0)
  14885. return WC_TEST_RET_ENC_EC(ret);
  14886. ret = wc_Sm4SetIV(&sm4, i1);
  14887. if (ret != 0)
  14888. return WC_TEST_RET_ENC_EC(ret);
  14889. XMEMCPY(enc, p1, sizeof(p1));
  14890. ret = wc_Sm4CbcEncrypt(&sm4, enc, enc, sizeof(p1));
  14891. if (ret != 0)
  14892. return WC_TEST_RET_ENC_EC(ret);
  14893. if (XMEMCMP(enc, c1_cbc, sizeof(c1_cbc)) != 0)
  14894. return WC_TEST_RET_ENC_NC;
  14895. ret = wc_Sm4SetIV(&sm4, i1);
  14896. if (ret != 0)
  14897. return WC_TEST_RET_ENC_EC(ret);
  14898. ret = wc_Sm4CbcDecrypt(&sm4, enc, enc, sizeof(c1_cbc));
  14899. if (ret != 0)
  14900. return WC_TEST_RET_ENC_EC(ret);
  14901. if (XMEMCMP(enc, p1, sizeof(p1)) != 0)
  14902. return WC_TEST_RET_ENC_NC;
  14903. wc_Sm4Free(&sm4);
  14904. return 0;
  14905. }
  14906. #endif
  14907. #ifdef WOLFSSL_SM4_CTR
  14908. static int sm4_ctr_test(void)
  14909. {
  14910. /* draft-ribose-cfrg-sm4-10 A.2.5.1 */
  14911. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  14912. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  14913. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  14914. };
  14915. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  14916. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14917. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  14918. };
  14919. WOLFSSL_SMALL_STACK_STATIC const byte p2[] = {
  14920. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  14921. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  14922. 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
  14923. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  14924. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  14925. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  14926. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  14927. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB
  14928. };
  14929. WOLFSSL_SMALL_STACK_STATIC const byte c2_ctr[] = {
  14930. 0xAC, 0x32, 0x36, 0xCB, 0x97, 0x0C, 0xC2, 0x07,
  14931. 0x91, 0x36, 0x4C, 0x39, 0x5A, 0x13, 0x42, 0xD1,
  14932. 0xA3, 0xCB, 0xC1, 0x87, 0x8C, 0x6F, 0x30, 0xCD,
  14933. 0x07, 0x4C, 0xCE, 0x38, 0x5C, 0xDD, 0x70, 0xC7,
  14934. 0xF2, 0x34, 0xBC, 0x0E, 0x24, 0xC1, 0x19, 0x80,
  14935. 0xFD, 0x12, 0x86, 0x31, 0x0C, 0xE3, 0x7B, 0x92,
  14936. 0x6E, 0x02, 0xFC, 0xD0, 0xFA, 0xA0, 0xBA, 0xF3,
  14937. 0x8B, 0x29, 0x33, 0x85, 0x1D, 0x82, 0x45, 0x14
  14938. };
  14939. wc_Sm4 sm4;
  14940. byte enc[SM4_BLOCK_SIZE * 4];
  14941. byte dec[SM4_BLOCK_SIZE * 4];
  14942. int chunk;
  14943. int i;
  14944. int ret;
  14945. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  14946. if (ret != 0)
  14947. return WC_TEST_RET_ENC_EC(ret);
  14948. /* Encrypt and decrypt using encrypt with CTR. */
  14949. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  14950. if (ret != 0)
  14951. return WC_TEST_RET_ENC_EC(ret);
  14952. ret = wc_Sm4SetIV(&sm4, i1);
  14953. if (ret != 0)
  14954. return WC_TEST_RET_ENC_EC(ret);
  14955. ret = wc_Sm4CtrEncrypt(&sm4, enc, p2, sizeof(p2));
  14956. if (ret != 0)
  14957. return WC_TEST_RET_ENC_EC(ret);
  14958. if (XMEMCMP(enc, c2_ctr, sizeof(c2_ctr)) != 0)
  14959. return WC_TEST_RET_ENC_NC;
  14960. ret = wc_Sm4SetIV(&sm4, i1);
  14961. if (ret != 0)
  14962. return WC_TEST_RET_ENC_EC(ret);
  14963. ret = wc_Sm4CtrEncrypt(&sm4, dec, enc, sizeof(c2_ctr));
  14964. if (ret != 0)
  14965. return WC_TEST_RET_ENC_EC(ret);
  14966. if (XMEMCMP(dec, p2, sizeof(p2)) != 0)
  14967. return WC_TEST_RET_ENC_NC;
  14968. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  14969. ret = wc_Sm4SetIV(&sm4, i1);
  14970. if (ret != 0)
  14971. return WC_TEST_RET_ENC_I(chunk);
  14972. XMEMSET(enc, 0, sizeof(enc));
  14973. for (i = 0; i + chunk <= (int)sizeof(p2); i += chunk) {
  14974. ret = wc_Sm4CtrEncrypt(&sm4, enc + i, p2 + i, chunk);
  14975. if (ret != 0)
  14976. return WC_TEST_RET_ENC_I(i);
  14977. }
  14978. if (i < (int)sizeof(p2)) {
  14979. ret = wc_Sm4CtrEncrypt(&sm4, enc + i, p2 + i, sizeof(p2) - i);
  14980. if (ret != 0)
  14981. return WC_TEST_RET_ENC_I(chunk);
  14982. }
  14983. if (XMEMCMP(enc, c2_ctr, sizeof(c2_ctr)) != 0)
  14984. return WC_TEST_RET_ENC_I(chunk);
  14985. }
  14986. wc_Sm4Free(&sm4);
  14987. return 0;
  14988. }
  14989. #endif
  14990. #ifdef WOLFSSL_SM4_GCM
  14991. static int sm4_gcm_test(void)
  14992. {
  14993. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  14994. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  14995. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  14996. };
  14997. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  14998. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  14999. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  15000. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  15001. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  15002. };
  15003. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  15004. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15005. 0x08, 0x09, 0x0A, 0x0B
  15006. };
  15007. WOLFSSL_SMALL_STACK_STATIC const byte a1[] = {
  15008. 0xFF, 0xEE, 0xDD
  15009. };
  15010. WOLFSSL_SMALL_STACK_STATIC const byte tag1[] = {
  15011. 0x83, 0xb2, 0x91, 0xcf, 0x22, 0xc9, 0x5f, 0x89,
  15012. 0xde, 0x3d, 0x52, 0x8d, 0xd7, 0x13, 0x50, 0x89
  15013. };
  15014. WOLFSSL_SMALL_STACK_STATIC const byte c1[] = {
  15015. 0xff, 0x8b, 0xb2, 0x3b, 0x0a, 0x0a, 0x12, 0xa4,
  15016. 0xa8, 0x4c, 0x4f, 0x67, 0x06, 0x81, 0xbb, 0x88,
  15017. 0x66, 0x17, 0xc7, 0x43, 0xbf, 0xae, 0x41, 0x40,
  15018. 0xec, 0x1e, 0x03, 0x85, 0x2b, 0x56, 0xa8, 0xc0
  15019. };
  15020. /* RFC8998 A.1. */
  15021. WOLFSSL_SMALL_STACK_STATIC const byte i2[] = {
  15022. 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, 0x00,
  15023. 0x00, 0x00, 0xAB, 0xCD
  15024. };
  15025. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  15026. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  15027. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  15028. };
  15029. WOLFSSL_SMALL_STACK_STATIC const byte p2[] = {
  15030. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  15031. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  15032. 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
  15033. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  15034. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  15035. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  15036. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  15037. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA
  15038. };
  15039. WOLFSSL_SMALL_STACK_STATIC const byte a2[] = {
  15040. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  15041. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  15042. 0xAB, 0xAD, 0xDA, 0xD2
  15043. };
  15044. WOLFSSL_SMALL_STACK_STATIC const byte c2[] = {
  15045. 0x17, 0xF3, 0x99, 0xF0, 0x8C, 0x67, 0xD5, 0xEE,
  15046. 0x19, 0xD0, 0xDC, 0x99, 0x69, 0xC4, 0xBB, 0x7D,
  15047. 0x5F, 0xD4, 0x6F, 0xD3, 0x75, 0x64, 0x89, 0x06,
  15048. 0x91, 0x57, 0xB2, 0x82, 0xBB, 0x20, 0x07, 0x35,
  15049. 0xD8, 0x27, 0x10, 0xCA, 0x5C, 0x22, 0xF0, 0xCC,
  15050. 0xFA, 0x7C, 0xBF, 0x93, 0xD4, 0x96, 0xAC, 0x15,
  15051. 0xA5, 0x68, 0x34, 0xCB, 0xCF, 0x98, 0xC3, 0x97,
  15052. 0xB4, 0x02, 0x4A, 0x26, 0x91, 0x23, 0x3B, 0x8D
  15053. };
  15054. WOLFSSL_SMALL_STACK_STATIC const byte tag2[] = {
  15055. 0x83, 0xDE, 0x35, 0x41, 0xE4, 0xC2, 0xB5, 0x81,
  15056. 0x77, 0xE0, 0x65, 0xA9, 0xBF, 0x7B, 0x62, 0xEC
  15057. };
  15058. wc_Sm4 sm4;
  15059. byte enc[SM4_BLOCK_SIZE * 4];
  15060. byte dec[SM4_BLOCK_SIZE * 4];
  15061. byte tag[SM4_BLOCK_SIZE];
  15062. int ret;
  15063. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  15064. if (ret != 0)
  15065. return WC_TEST_RET_ENC_EC(ret);
  15066. /* Encrypt and decrypt using encrypt with GCM. */
  15067. ret = wc_Sm4GcmSetKey(&sm4, k1, sizeof(k1));
  15068. if (ret != 0)
  15069. return WC_TEST_RET_ENC_EC(ret);
  15070. ret = wc_Sm4GcmEncrypt(&sm4, enc, p1, sizeof(p1), i1, sizeof(i1), tag,
  15071. sizeof(tag), a1, sizeof(a1));
  15072. if (ret != 0)
  15073. return WC_TEST_RET_ENC_EC(ret);
  15074. if (XMEMCMP(enc, c1, sizeof(c1)) != 0)
  15075. return WC_TEST_RET_ENC_NC;
  15076. if (XMEMCMP(tag, tag1, sizeof(tag1)) != 0)
  15077. return WC_TEST_RET_ENC_NC;
  15078. ret = wc_Sm4GcmDecrypt(&sm4, dec, enc, sizeof(c1), i1, sizeof(i1), tag,
  15079. sizeof(tag), a1, sizeof(a1));
  15080. if (ret != 0)
  15081. return WC_TEST_RET_ENC_EC(ret);
  15082. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  15083. return WC_TEST_RET_ENC_NC;
  15084. /* RFC8998 test vector. */
  15085. ret = wc_Sm4GcmSetKey(&sm4, k2, sizeof(k2));
  15086. if (ret != 0)
  15087. return WC_TEST_RET_ENC_EC(ret);
  15088. ret = wc_Sm4GcmEncrypt(&sm4, enc, p2, sizeof(p2), i2, sizeof(i2), tag,
  15089. sizeof(tag), a2, sizeof(a2));
  15090. if (ret != 0)
  15091. return WC_TEST_RET_ENC_EC(ret);
  15092. if (XMEMCMP(enc, c2, sizeof(c2)) != 0)
  15093. return WC_TEST_RET_ENC_NC;
  15094. if (XMEMCMP(tag, tag2, sizeof(tag2)) != 0)
  15095. return WC_TEST_RET_ENC_NC;
  15096. ret = wc_Sm4GcmDecrypt(&sm4, dec, enc, sizeof(c2), i2, sizeof(i2), tag,
  15097. sizeof(tag), a2, sizeof(a2));
  15098. if (ret != 0)
  15099. return WC_TEST_RET_ENC_EC(ret);
  15100. if (XMEMCMP(dec, p2, sizeof(p2)) != 0)
  15101. return WC_TEST_RET_ENC_NC;
  15102. wc_Sm4Free(&sm4);
  15103. return 0;
  15104. }
  15105. #endif
  15106. #ifdef WOLFSSL_SM4_CCM
  15107. static int sm4_ccm_test(void)
  15108. {
  15109. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  15110. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  15111. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  15112. };
  15113. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  15114. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  15115. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  15116. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  15117. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  15118. };
  15119. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  15120. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15121. 0x08, 0x09, 0x0A, 0x0B
  15122. };
  15123. WOLFSSL_SMALL_STACK_STATIC const byte a1[] = {
  15124. 0xFF, 0xEE, 0xDD
  15125. };
  15126. WOLFSSL_SMALL_STACK_STATIC const byte tag1[] = {
  15127. 0x9a, 0x98, 0x04, 0xb6, 0x0f, 0x19, 0x4a, 0x46,
  15128. 0xba, 0xed, 0xe6, 0x89, 0x69, 0x34, 0xad, 0x61
  15129. };
  15130. WOLFSSL_SMALL_STACK_STATIC const byte c1[] = {
  15131. 0xbd, 0xc0, 0x72, 0x60, 0xda, 0x2d, 0x11, 0xdc,
  15132. 0x66, 0x33, 0xcc, 0xec, 0xb2, 0xf4, 0x53, 0x59,
  15133. 0x9e, 0xb1, 0xb3, 0x6b, 0x1f, 0x1c, 0xfb, 0x29,
  15134. 0xf5, 0x37, 0xfc, 0x00, 0xf2, 0x4e, 0x70, 0x6f
  15135. };
  15136. /* RFC8998 A.1. */
  15137. WOLFSSL_SMALL_STACK_STATIC const byte i2[] = {
  15138. 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, 0x00,
  15139. 0x00, 0x00, 0xAB, 0xCD
  15140. };
  15141. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  15142. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  15143. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  15144. };
  15145. WOLFSSL_SMALL_STACK_STATIC const byte p2[] = {
  15146. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  15147. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  15148. 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
  15149. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  15150. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  15151. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  15152. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  15153. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA
  15154. };
  15155. WOLFSSL_SMALL_STACK_STATIC const byte a2[] = {
  15156. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  15157. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  15158. 0xAB, 0xAD, 0xDA, 0xD2
  15159. };
  15160. WOLFSSL_SMALL_STACK_STATIC const byte c2[] = {
  15161. 0x48, 0xAF, 0x93, 0x50, 0x1F, 0xA6, 0x2A, 0xDB,
  15162. 0xCD, 0x41, 0x4C, 0xCE, 0x60, 0x34, 0xD8, 0x95,
  15163. 0xDD, 0xA1, 0xBF, 0x8F, 0x13, 0x2F, 0x04, 0x20,
  15164. 0x98, 0x66, 0x15, 0x72, 0xE7, 0x48, 0x30, 0x94,
  15165. 0xFD, 0x12, 0xE5, 0x18, 0xCE, 0x06, 0x2C, 0x98,
  15166. 0xAC, 0xEE, 0x28, 0xD9, 0x5D, 0xF4, 0x41, 0x6B,
  15167. 0xED, 0x31, 0xA2, 0xF0, 0x44, 0x76, 0xC1, 0x8B,
  15168. 0xB4, 0x0C, 0x84, 0xA7, 0x4B, 0x97, 0xDC, 0x5B
  15169. };
  15170. WOLFSSL_SMALL_STACK_STATIC const byte tag2[] = {
  15171. 0x16, 0x84, 0x2D, 0x4F, 0xA1, 0x86, 0xF5, 0x6A,
  15172. 0xB3, 0x32, 0x56, 0x97, 0x1F, 0xA1, 0x10, 0xF4
  15173. };
  15174. wc_Sm4 sm4;
  15175. byte enc[SM4_BLOCK_SIZE * 4];
  15176. byte dec[SM4_BLOCK_SIZE * 4];
  15177. byte tag[SM4_BLOCK_SIZE];
  15178. int ret;
  15179. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  15180. if (ret != 0)
  15181. return WC_TEST_RET_ENC_EC(ret);
  15182. /* Encrypt and decrypt using encrypt with CCM. */
  15183. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  15184. if (ret != 0)
  15185. return WC_TEST_RET_ENC_EC(ret);
  15186. ret = wc_Sm4CcmEncrypt(&sm4, enc, p1, sizeof(p1), i1, sizeof(i1), tag,
  15187. sizeof(tag), a1, sizeof(a1));
  15188. if (ret != 0)
  15189. return WC_TEST_RET_ENC_EC(ret);
  15190. if (XMEMCMP(enc, c1, sizeof(c1)) != 0)
  15191. return WC_TEST_RET_ENC_NC;
  15192. if (XMEMCMP(tag, tag1, sizeof(tag1)) != 0)
  15193. return WC_TEST_RET_ENC_NC;
  15194. ret = wc_Sm4CcmDecrypt(&sm4, dec, enc, sizeof(c1), i1, sizeof(i1), tag,
  15195. sizeof(tag), a1, sizeof(a1));
  15196. if (ret != 0)
  15197. return WC_TEST_RET_ENC_EC(ret);
  15198. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  15199. return WC_TEST_RET_ENC_NC;
  15200. /* RFC8998 test vector. */
  15201. ret = wc_Sm4SetKey(&sm4, k2, sizeof(k2));
  15202. if (ret != 0)
  15203. return WC_TEST_RET_ENC_EC(ret);
  15204. ret = wc_Sm4CcmEncrypt(&sm4, enc, p2, sizeof(p2), i2, sizeof(i2), tag,
  15205. sizeof(tag), a2, sizeof(a2));
  15206. if (ret != 0)
  15207. return WC_TEST_RET_ENC_EC(ret);
  15208. if (XMEMCMP(enc, c2, sizeof(c2)) != 0)
  15209. return WC_TEST_RET_ENC_NC;
  15210. if (XMEMCMP(tag, tag2, sizeof(tag2)) != 0)
  15211. return WC_TEST_RET_ENC_NC;
  15212. ret = wc_Sm4CcmDecrypt(&sm4, dec, enc, sizeof(c2), i2, sizeof(i2), tag,
  15213. sizeof(tag), a2, sizeof(a2));
  15214. if (ret != 0)
  15215. return WC_TEST_RET_ENC_EC(ret);
  15216. if (XMEMCMP(dec, p2, sizeof(p2)) != 0)
  15217. return WC_TEST_RET_ENC_NC;
  15218. wc_Sm4Free(&sm4);
  15219. return 0;
  15220. }
  15221. #endif
  15222. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm4_test(void)
  15223. {
  15224. wc_test_ret_t ret;
  15225. WOLFSSL_ENTER("sm4_test");
  15226. #ifdef WOLFSSL_SM4_ECB
  15227. ret = sm4_ecb_test();
  15228. if (ret != 0)
  15229. return ret;
  15230. #endif
  15231. #ifdef WOLFSSL_SM4_CBC
  15232. ret = sm4_cbc_test();
  15233. if (ret != 0)
  15234. return ret;
  15235. #endif
  15236. #ifdef WOLFSSL_SM4_CTR
  15237. ret = sm4_ctr_test();
  15238. if (ret != 0)
  15239. return ret;
  15240. #endif
  15241. #ifdef WOLFSSL_SM4_GCM
  15242. ret = sm4_gcm_test();
  15243. if (ret != 0)
  15244. return ret;
  15245. #endif
  15246. #ifdef WOLFSSL_SM4_CCM
  15247. ret = sm4_ccm_test();
  15248. if (ret != 0)
  15249. return ret;
  15250. #endif
  15251. return 0;
  15252. }
  15253. #endif
  15254. #ifdef HAVE_XCHACHA
  15255. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha_test(void) {
  15256. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  15257. 0x54, 0x68, 0x65, 0x20, 0x64, 0x68, 0x6f, 0x6c, 0x65, 0x20, 0x28, 0x70, 0x72, 0x6f, 0x6e, 0x6f, /* The dhole (prono */
  15258. 0x75, 0x6e, 0x63, 0x65, 0x64, 0x20, 0x22, 0x64, 0x6f, 0x6c, 0x65, 0x22, 0x29, 0x20, 0x69, 0x73, /* unced "dole") is */
  15259. 0x20, 0x61, 0x6c, 0x73, 0x6f, 0x20, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x20, 0x61, 0x73, 0x20, 0x74, /* also known as t */
  15260. 0x68, 0x65, 0x20, 0x41, 0x73, 0x69, 0x61, 0x74, 0x69, 0x63, 0x20, 0x77, 0x69, 0x6c, 0x64, 0x20, /* he Asiatic wild */
  15261. 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x72, 0x65, 0x64, 0x20, 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x61, 0x6e, /* dog, red dog, an */
  15262. 0x64, 0x20, 0x77, 0x68, 0x69, 0x73, 0x74, 0x6c, 0x69, 0x6e, 0x67, 0x20, 0x64, 0x6f, 0x67, 0x2e, /* d whistling dog. */
  15263. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x62, 0x6f, 0x75, 0x74, 0x20, 0x74, 0x68, 0x65, /* It is about the */
  15264. 0x20, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x61, 0x20, 0x47, 0x65, 0x72, 0x6d, 0x61, /* size of a Germa */
  15265. 0x6e, 0x20, 0x73, 0x68, 0x65, 0x70, 0x68, 0x65, 0x72, 0x64, 0x20, 0x62, 0x75, 0x74, 0x20, 0x6c, /* n shepherd but l */
  15266. 0x6f, 0x6f, 0x6b, 0x73, 0x20, 0x6d, 0x6f, 0x72, 0x65, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x61, /* ooks more like a */
  15267. 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x2d, 0x6c, 0x65, 0x67, 0x67, 0x65, 0x64, 0x20, 0x66, 0x6f, 0x78, /* long-legged fox */
  15268. 0x2e, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x68, 0x69, 0x67, 0x68, 0x6c, 0x79, 0x20, 0x65, 0x6c, /* . This highly el */
  15269. 0x75, 0x73, 0x69, 0x76, 0x65, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x6b, 0x69, 0x6c, 0x6c, 0x65, /* usive and skille */
  15270. 0x64, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x72, 0x20, 0x69, 0x73, 0x20, 0x63, 0x6c, 0x61, 0x73, /* d jumper is clas */
  15271. 0x73, 0x69, 0x66, 0x69, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x77, 0x6f, 0x6c, 0x76, /* sified with wolv */
  15272. 0x65, 0x73, 0x2c, 0x20, 0x63, 0x6f, 0x79, 0x6f, 0x74, 0x65, 0x73, 0x2c, 0x20, 0x6a, 0x61, 0x63, /* es, coyotes, jac */
  15273. 0x6b, 0x61, 0x6c, 0x73, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x66, 0x6f, 0x78, 0x65, 0x73, 0x20, /* kals, and foxes */
  15274. 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 0x61, 0x78, 0x6f, 0x6e, 0x6f, 0x6d, 0x69, 0x63, /* in the taxonomic */
  15275. 0x20, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x20, 0x43, 0x61, 0x6e, 0x69, 0x64, 0x61, 0x65, 0x2e /* family Canidae. */
  15276. };
  15277. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  15278. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  15279. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  15280. };
  15281. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  15282. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  15283. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x58 }; /* PQRSTUVW */
  15284. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  15285. 0x45, 0x59, 0xab, 0xba, 0x4e, 0x48, 0xc1, 0x61, 0x02, 0xe8, 0xbb, 0x2c, 0x05, 0xe6, 0x94, 0x7f,
  15286. 0x50, 0xa7, 0x86, 0xde, 0x16, 0x2f, 0x9b, 0x0b, 0x7e, 0x59, 0x2a, 0x9b, 0x53, 0xd0, 0xd4, 0xe9,
  15287. 0x8d, 0x8d, 0x64, 0x10, 0xd5, 0x40, 0xa1, 0xa6, 0x37, 0x5b, 0x26, 0xd8, 0x0d, 0xac, 0xe4, 0xfa,
  15288. 0xb5, 0x23, 0x84, 0xc7, 0x31, 0xac, 0xbf, 0x16, 0xa5, 0x92, 0x3c, 0x0c, 0x48, 0xd3, 0x57, 0x5d,
  15289. 0x4d, 0x0d, 0x2c, 0x67, 0x3b, 0x66, 0x6f, 0xaa, 0x73, 0x10, 0x61, 0x27, 0x77, 0x01, 0x09, 0x3a,
  15290. 0x6b, 0xf7, 0xa1, 0x58, 0xa8, 0x86, 0x42, 0x92, 0xa4, 0x1c, 0x48, 0xe3, 0xa9, 0xb4, 0xc0, 0xda,
  15291. 0xec, 0xe0, 0xf8, 0xd9, 0x8d, 0x0d, 0x7e, 0x05, 0xb3, 0x7a, 0x30, 0x7b, 0xbb, 0x66, 0x33, 0x31,
  15292. 0x64, 0xec, 0x9e, 0x1b, 0x24, 0xea, 0x0d, 0x6c, 0x3f, 0xfd, 0xdc, 0xec, 0x4f, 0x68, 0xe7, 0x44,
  15293. 0x30, 0x56, 0x19, 0x3a, 0x03, 0xc8, 0x10, 0xe1, 0x13, 0x44, 0xca, 0x06, 0xd8, 0xed, 0x8a, 0x2b,
  15294. 0xfb, 0x1e, 0x8d, 0x48, 0xcf, 0xa6, 0xbc, 0x0e, 0xb4, 0xe2, 0x46, 0x4b, 0x74, 0x81, 0x42, 0x40,
  15295. 0x7c, 0x9f, 0x43, 0x1a, 0xee, 0x76, 0x99, 0x60, 0xe1, 0x5b, 0xa8, 0xb9, 0x68, 0x90, 0x46, 0x6e,
  15296. 0xf2, 0x45, 0x75, 0x99, 0x85, 0x23, 0x85, 0xc6, 0x61, 0xf7, 0x52, 0xce, 0x20, 0xf9, 0xda, 0x0c,
  15297. 0x09, 0xab, 0x6b, 0x19, 0xdf, 0x74, 0xe7, 0x6a, 0x95, 0x96, 0x74, 0x46, 0xf8, 0xd0, 0xfd, 0x41,
  15298. 0x5e, 0x7b, 0xee, 0x2a, 0x12, 0xa1, 0x14, 0xc2, 0x0e, 0xb5, 0x29, 0x2a, 0xe7, 0xa3, 0x49, 0xae,
  15299. 0x57, 0x78, 0x20, 0xd5, 0x52, 0x0a, 0x1f, 0x3f, 0xb6, 0x2a, 0x17, 0xce, 0x6a, 0x7e, 0x68, 0xfa,
  15300. 0x7c, 0x79, 0x11, 0x1d, 0x88, 0x60, 0x92, 0x0b, 0xc0, 0x48, 0xef, 0x43, 0xfe, 0x84, 0x48, 0x6c,
  15301. 0xcb, 0x87, 0xc2, 0x5f, 0x0a, 0xe0, 0x45, 0xf0, 0xcc, 0xe1, 0xe7, 0x98, 0x9a, 0x9a, 0xa2, 0x20,
  15302. 0xa2, 0x8b, 0xdd, 0x48, 0x27, 0xe7, 0x51, 0xa2, 0x4a, 0x6d, 0x5c, 0x62, 0xd7, 0x90, 0xa6, 0x63,
  15303. 0x93, 0xb9, 0x31, 0x11, 0xc1, 0xa5, 0x5d, 0xd7, 0x42, 0x1a, 0x10, 0x18, 0x49, 0x74, 0xc7, 0xc5
  15304. };
  15305. wc_test_ret_t ret;
  15306. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15307. struct ChaCha *chacha = (struct ChaCha *)XMALLOC(sizeof *chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  15308. byte *buf1 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15309. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15310. WOLFSSL_ENTER("XChaCha_test");
  15311. if ((chacha == NULL) || (buf1 == NULL) || (buf2 == NULL))
  15312. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15313. #else
  15314. struct ChaCha chacha[1];
  15315. byte buf1[sizeof Plaintext];
  15316. byte buf2[sizeof Plaintext];
  15317. WOLFSSL_ENTER("XChaCha_test");
  15318. #endif
  15319. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  15320. if (ret < 0)
  15321. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15322. ret = wc_Chacha_Process(chacha, buf1, Plaintext, sizeof Plaintext);
  15323. if (ret < 0)
  15324. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15325. if (XMEMCMP(buf1, Ciphertext, sizeof Plaintext))
  15326. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15327. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  15328. if (ret < 0)
  15329. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15330. ret = wc_Chacha_Process(chacha, buf2, buf1, sizeof Plaintext);
  15331. if (ret < 0)
  15332. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15333. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  15334. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15335. out:
  15336. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15337. if (chacha)
  15338. XFREE(chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  15339. if (buf1)
  15340. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15341. if (buf2)
  15342. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15343. #endif
  15344. return ret;
  15345. }
  15346. #endif /* HAVE_XCHACHA */
  15347. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  15348. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha20Poly1305_test(void) {
  15349. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  15350. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c, /* Ladies and Gentl */
  15351. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73, /* emen of the clas */
  15352. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63, /* s of '99: If I c */
  15353. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f, /* ould offer you o */
  15354. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20, /* nly one tip for */
  15355. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73, /* the future, suns */
  15356. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69, /* creen would be i */
  15357. 0x74, 0x2e }; /* t. */
  15358. WOLFSSL_SMALL_STACK_STATIC const byte AAD[] = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 }; /* PQRS........ */
  15359. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  15360. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  15361. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  15362. };
  15363. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  15364. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  15365. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 }; /* PQRSTUVW */
  15366. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  15367. 0xbd, 0x6d, 0x17, 0x9d, 0x3e, 0x83, 0xd4, 0x3b, 0x95, 0x76, 0x57, 0x94, 0x93, 0xc0, 0xe9, 0x39,
  15368. 0x57, 0x2a, 0x17, 0x00, 0x25, 0x2b, 0xfa, 0xcc, 0xbe, 0xd2, 0x90, 0x2c, 0x21, 0x39, 0x6c, 0xbb,
  15369. 0x73, 0x1c, 0x7f, 0x1b, 0x0b, 0x4a, 0xa6, 0x44, 0x0b, 0xf3, 0xa8, 0x2f, 0x4e, 0xda, 0x7e, 0x39,
  15370. 0xae, 0x64, 0xc6, 0x70, 0x8c, 0x54, 0xc2, 0x16, 0xcb, 0x96, 0xb7, 0x2e, 0x12, 0x13, 0xb4, 0x52,
  15371. 0x2f, 0x8c, 0x9b, 0xa4, 0x0d, 0xb5, 0xd9, 0x45, 0xb1, 0x1b, 0x69, 0xb9, 0x82, 0xc1, 0xbb, 0x9e,
  15372. 0x3f, 0x3f, 0xac, 0x2b, 0xc3, 0x69, 0x48, 0x8f, 0x76, 0xb2, 0x38, 0x35, 0x65, 0xd3, 0xff, 0xf9,
  15373. 0x21, 0xf9, 0x66, 0x4c, 0x97, 0x63, 0x7d, 0xa9, 0x76, 0x88, 0x12, 0xf6, 0x15, 0xc6, 0x8b, 0x13,
  15374. 0xb5, 0x2e };
  15375. WOLFSSL_SMALL_STACK_STATIC const byte Tag[] = {
  15376. 0xc0, 0x87, 0x59, 0x24, 0xc1, 0xc7, 0x98, 0x79, 0x47, 0xde, 0xaf, 0xd8, 0x78, 0x0a, 0xcf, 0x49
  15377. };
  15378. wc_test_ret_t ret;
  15379. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15380. byte *buf1 = (byte *)XMALLOC(sizeof Ciphertext + sizeof Tag, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15381. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15382. WOLFSSL_ENTER("XChaCha20Poly1305_test");
  15383. if ((buf1 == NULL) || (buf2 == NULL))
  15384. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15385. #else
  15386. byte buf1[sizeof Ciphertext + sizeof Tag];
  15387. byte buf2[sizeof Plaintext];
  15388. WOLFSSL_ENTER("XChaCha20Poly1305_test");
  15389. #endif
  15390. ret = wc_XChaCha20Poly1305_Encrypt(buf1, sizeof Ciphertext + sizeof Tag,
  15391. Plaintext, sizeof Plaintext,
  15392. AAD, sizeof AAD,
  15393. IV, sizeof IV,
  15394. Key, sizeof Key);
  15395. if (ret < 0)
  15396. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15397. if (XMEMCMP(buf1, Ciphertext, sizeof Ciphertext))
  15398. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15399. if (XMEMCMP(buf1 + sizeof Ciphertext, Tag, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE))
  15400. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15401. ret = wc_XChaCha20Poly1305_Decrypt(buf2, sizeof Plaintext,
  15402. buf1, sizeof Ciphertext + sizeof Tag,
  15403. AAD, sizeof AAD,
  15404. IV, sizeof IV,
  15405. Key, sizeof Key);
  15406. if (ret < 0)
  15407. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15408. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  15409. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15410. out:
  15411. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15412. if (buf1 != NULL)
  15413. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15414. if (buf2 != NULL)
  15415. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15416. #endif
  15417. return ret;
  15418. }
  15419. #endif /* defined(HAVE_XCHACHA) && defined(HAVE_POLY1305) */
  15420. #ifndef WC_NO_RNG
  15421. static wc_test_ret_t _rng_test(WC_RNG* rng, int errorOffset)
  15422. {
  15423. byte block[32];
  15424. wc_test_ret_t ret;
  15425. int i;
  15426. XMEMSET(block, 0, sizeof(block));
  15427. ret = wc_RNG_GenerateBlock(rng, block, sizeof(block));
  15428. if (ret != 0) {
  15429. ret = 1;
  15430. goto exit;
  15431. }
  15432. /* Check for 0's */
  15433. for (i=0; i<(int)sizeof(block); i++) {
  15434. if (block[i] == 0) {
  15435. ret++;
  15436. }
  15437. }
  15438. /* All zeros count check */
  15439. if (ret >= (int)sizeof(block)) {
  15440. ret = 2;
  15441. goto exit;
  15442. }
  15443. ret = wc_RNG_GenerateByte(rng, block);
  15444. if (ret != 0) {
  15445. ret = 3;
  15446. goto exit;
  15447. }
  15448. /* Parameter validation testing. */
  15449. ret = wc_RNG_GenerateBlock(NULL, block, sizeof(block));
  15450. if (ret != BAD_FUNC_ARG) {
  15451. ret = 4;
  15452. goto exit;
  15453. }
  15454. ret = wc_RNG_GenerateBlock(rng, NULL, sizeof(block));
  15455. if (ret != BAD_FUNC_ARG) {
  15456. ret = 5;
  15457. goto exit;
  15458. }
  15459. ret = wc_RNG_GenerateByte(NULL, block);
  15460. if (ret != BAD_FUNC_ARG) {
  15461. ret = 6;
  15462. goto exit;
  15463. }
  15464. ret = wc_RNG_GenerateByte(rng, NULL);
  15465. if (ret != BAD_FUNC_ARG) {
  15466. ret = 7;
  15467. goto exit;
  15468. }
  15469. ret = 0;
  15470. exit:
  15471. if (ret != 0)
  15472. ret = errorOffset - (ret * 1000000);
  15473. return ret;
  15474. }
  15475. static wc_test_ret_t random_rng_test(void)
  15476. {
  15477. WC_RNG localRng;
  15478. WC_RNG* rng;
  15479. wc_test_ret_t ret;
  15480. rng = &localRng;
  15481. /* Test stack based RNG. */
  15482. #ifndef HAVE_FIPS
  15483. ret = wc_InitRng_ex(rng, HEAP_HINT, devId);
  15484. #else
  15485. ret = wc_InitRng(rng);
  15486. #endif
  15487. if (ret != 0)
  15488. return WC_TEST_RET_ENC_EC(ret);
  15489. ret = _rng_test(rng, WC_TEST_RET_ENC_NC);
  15490. /* Make sure and free RNG */
  15491. wc_FreeRng(rng);
  15492. if (ret != 0)
  15493. return ret;
  15494. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  15495. {
  15496. byte nonce[8] = { 0 };
  15497. /* Test dynamic RNG */
  15498. rng = wc_rng_new(nonce, (word32)sizeof(nonce), HEAP_HINT);
  15499. if (rng == NULL)
  15500. return WC_TEST_RET_ENC_ERRNO;
  15501. ret = _rng_test(rng, WC_TEST_RET_ENC_NC);
  15502. wc_rng_free(rng);
  15503. rng = NULL;
  15504. if (ret != 0)
  15505. return ret;
  15506. /* Test dynamic RNG using extended API */
  15507. ret = wc_rng_new_ex(&rng, nonce, (word32)sizeof(nonce),
  15508. HEAP_HINT, devId);
  15509. if (ret != 0)
  15510. return WC_TEST_RET_ENC_EC(ret);
  15511. ret = _rng_test(rng, WC_TEST_RET_ENC_NC);
  15512. wc_rng_free(rng);
  15513. if (ret != 0)
  15514. return ret;
  15515. }
  15516. #endif
  15517. return ret;
  15518. }
  15519. #if defined(HAVE_HASHDRBG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  15520. #ifdef WC_RNG_SEED_CB
  15521. static int seed_cb(OS_Seed* os, byte* output, word32 sz)
  15522. {
  15523. word32 i;
  15524. (void)os;
  15525. /* Known answer test. Set the seed to the same value every time. */
  15526. for (i = 0; i < sz; i++)
  15527. output[i] = (byte)i;
  15528. return 0;
  15529. }
  15530. static wc_test_ret_t rng_seed_test(void)
  15531. {
  15532. #ifndef HAVE_FIPS
  15533. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  15534. {
  15535. 0x83, 0x46, 0x65, 0x2f, 0x5c, 0x44, 0x16, 0x5f,
  15536. 0xb3, 0x89, 0x26, 0xde, 0x0b, 0x6b, 0xa2, 0x06,
  15537. 0x7e, 0xa7, 0x9a, 0x55, 0x22, 0x01, 0xb0, 0x22,
  15538. 0xf4, 0x7e, 0xa2, 0x66, 0xc4, 0x08, 0x6f, 0xba
  15539. };
  15540. #else
  15541. /* FIPS uses a longer seed, so different check value. */
  15542. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  15543. {
  15544. 0xaf, 0x31, 0xcc, 0xef, 0xa9, 0x29, 0x4c, 0x24,
  15545. 0xbd, 0xa5, 0xa3, 0x52, 0x69, 0xf3, 0xb9, 0xb2,
  15546. 0x1e, 0xd4, 0x52, 0x3b, 0x9a, 0x96, 0x06, 0x20,
  15547. 0xc0, 0x5f, 0x44, 0x06, 0x1f, 0x80, 0xdf, 0xe0
  15548. };
  15549. #endif
  15550. byte output[WC_SHA256_DIGEST_SIZE];
  15551. WC_RNG rng;
  15552. wc_test_ret_t ret;
  15553. ret = wc_SetSeed_Cb(seed_cb);
  15554. if (ret != 0) {
  15555. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15556. }
  15557. ret = wc_InitRng(&rng);
  15558. if (ret != 0) {
  15559. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15560. }
  15561. ret = wc_RNG_GenerateBlock(&rng, output, sizeof(output));
  15562. if (ret != 0) {
  15563. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15564. }
  15565. ret = XMEMCMP(output, check, sizeof(output));
  15566. if (ret != 0) {
  15567. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15568. }
  15569. ret = wc_FreeRng(&rng);
  15570. if (ret != 0) {
  15571. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15572. }
  15573. ret = wc_SetSeed_Cb(wc_GenerateSeed);
  15574. if (ret != 0) {
  15575. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15576. }
  15577. out:
  15578. return ret;
  15579. }
  15580. #endif
  15581. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_test(void)
  15582. {
  15583. WOLFSSL_SMALL_STACK_STATIC const byte test1Entropy[] =
  15584. {
  15585. 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
  15586. 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
  15587. 0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, 0x85, 0x81, 0xf9, 0x31,
  15588. 0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d, 0xdb, 0xcb, 0xcc, 0x2e
  15589. };
  15590. WOLFSSL_SMALL_STACK_STATIC const byte test1Output[] =
  15591. {
  15592. 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
  15593. 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
  15594. 0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
  15595. 0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
  15596. 0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
  15597. 0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
  15598. 0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
  15599. 0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
  15600. 0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
  15601. 0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
  15602. 0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
  15603. };
  15604. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyA[] =
  15605. {
  15606. 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
  15607. 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
  15608. 0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
  15609. 0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
  15610. };
  15611. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyB[] =
  15612. {
  15613. 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
  15614. 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
  15615. 0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
  15616. };
  15617. WOLFSSL_SMALL_STACK_STATIC const byte test2Output[] =
  15618. {
  15619. 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
  15620. 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
  15621. 0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
  15622. 0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
  15623. 0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
  15624. 0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
  15625. 0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
  15626. 0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
  15627. 0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
  15628. 0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
  15629. 0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
  15630. };
  15631. byte output[WC_SHA256_DIGEST_SIZE * 4];
  15632. wc_test_ret_t ret;
  15633. WOLFSSL_ENTER("random_test");
  15634. ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
  15635. output, sizeof(output));
  15636. if (ret != 0)
  15637. return WC_TEST_RET_ENC_EC(ret);
  15638. if (XMEMCMP(test1Output, output, sizeof(output)) != 0)
  15639. return WC_TEST_RET_ENC_NC;
  15640. ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
  15641. test2EntropyB, sizeof(test2EntropyB),
  15642. output, sizeof(output));
  15643. if (ret != 0)
  15644. return WC_TEST_RET_ENC_EC(ret);
  15645. if (XMEMCMP(test2Output, output, sizeof(output)) != 0)
  15646. return WC_TEST_RET_ENC_NC;
  15647. /* Basic RNG generate block test */
  15648. if ((ret = random_rng_test()) != 0)
  15649. return ret;
  15650. /* Test the seed check function. */
  15651. #if !(defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) || \
  15652. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  15653. {
  15654. word32 i, outputSz;
  15655. /* Repeat the same byte over and over. Should fail. */
  15656. outputSz = sizeof(output);
  15657. XMEMSET(output, 1, outputSz);
  15658. ret = wc_RNG_TestSeed(output, outputSz);
  15659. if (ret == 0)
  15660. return WC_TEST_RET_ENC_NC;
  15661. /* Every byte of the entropy scratch is different,
  15662. * entropy is a single byte that shouldn't match. */
  15663. outputSz = (sizeof(output) / 2) + 1;
  15664. for (i = 0; i < outputSz; i++)
  15665. output[i] = (byte)i;
  15666. ret = wc_RNG_TestSeed(output, outputSz);
  15667. if (ret != 0)
  15668. return WC_TEST_RET_ENC_EC(ret);
  15669. outputSz = sizeof(output);
  15670. for (i = 0; i < outputSz; i++)
  15671. output[i] = (byte)i;
  15672. ret = wc_RNG_TestSeed(output, outputSz);
  15673. if (ret != 0)
  15674. return WC_TEST_RET_ENC_EC(ret);
  15675. }
  15676. #endif
  15677. /* Test the seed callback. */
  15678. #ifdef WC_RNG_SEED_CB
  15679. if ((ret = rng_seed_test()) != 0)
  15680. return ret;
  15681. #endif
  15682. return 0;
  15683. }
  15684. #else
  15685. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_test(void)
  15686. {
  15687. WOLFSSL_ENTER("random_test");
  15688. /* Basic RNG generate block test */
  15689. return random_rng_test();
  15690. }
  15691. #endif /* HAVE_HASHDRBG && !CUSTOM_RAND_GENERATE_BLOCK */
  15692. #endif /* WC_NO_RNG */
  15693. #ifndef MEM_TEST_SZ
  15694. #define MEM_TEST_SZ 1024
  15695. #endif
  15696. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  15697. static int simple_mem_test(int sz)
  15698. {
  15699. int ret = 0;
  15700. byte* b;
  15701. int i;
  15702. b = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15703. if (b == NULL) {
  15704. return WC_TEST_RET_ENC_NC;
  15705. }
  15706. /* utilize memory */
  15707. for (i = 0; i < sz; i++) {
  15708. b[i] = (byte)i;
  15709. }
  15710. /* read back and verify */
  15711. for (i = 0; i < sz; i++) {
  15712. if (b[i] != (byte)i) {
  15713. ret = WC_TEST_RET_ENC_NC;
  15714. break;
  15715. }
  15716. }
  15717. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15718. return ret;
  15719. }
  15720. #endif
  15721. /* If successful, returns the first letter of the byte array `in`.
  15722. **
  15723. ** This is a deceptively simple test of a read-only embedded Linux file system.
  15724. ** (e.g CFLAGS `-mfdpic` and `-mforce-l32`) for Xtensa Linux ESP32. When the
  15725. ** `-mforce-l32` is missing, access to `in` will fail with Illegal Instruction.
  15726. ** Const is on read-only memory-mapped file system, *not* loaded in app memory.
  15727. **
  15728. ** Edit with caution. See PR #6523. */
  15729. static wc_test_ret_t const_byte_ptr_test(const byte* in, word32 *outJ)
  15730. {
  15731. wc_test_ret_t ret = 0;
  15732. volatile word32 j = (word32)-1; /* must be volatile to properly detect error */
  15733. ret = (wc_test_ret_t)*in; /* accessed *in value. */
  15734. (void)ret;
  15735. j = *outJ; /* Found index to use in const array. */
  15736. if (j == 0) {
  15737. #ifdef WOLFSSL_DEBUG
  15738. printf("Testing const byte ptr reference...\n");
  15739. #endif
  15740. /* although j is zero, in[0] does not detect the Illegal instruction */
  15741. ret = in[j]; /* The big test: can we actually access the `in` data? */
  15742. }
  15743. else {
  15744. ret = WC_TEST_RET_ENC_I(j);
  15745. }
  15746. return ret;
  15747. }
  15748. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memory_test(void)
  15749. {
  15750. wc_test_ret_t ret = 0;
  15751. word32 j = 0; /* used in embedded const pointer test */
  15752. WOLFSSL_ENTER("memory_test");
  15753. #if defined(COMPLEX_MEM_TEST) || defined(WOLFSSL_STATIC_MEMORY)
  15754. int i;
  15755. #endif
  15756. #ifdef WOLFSSL_STATIC_MEMORY
  15757. word32 size[] = { WOLFMEM_BUCKETS };
  15758. word32 dist[] = { WOLFMEM_DIST };
  15759. byte buffer[30000]; /* make large enough to involve many bucket sizes */
  15760. int pad = -(int)((wc_ptr_t)buffer) & (WOLFSSL_STATIC_ALIGN - 1);
  15761. /* pad to account for if head of buffer is not at set memory
  15762. * alignment when tests are ran */
  15763. #endif
  15764. #ifdef WOLFSSL_STATIC_MEMORY
  15765. /* check macro settings */
  15766. if (sizeof(size)/sizeof(word32) != WOLFMEM_DEF_BUCKETS) {
  15767. return WC_TEST_RET_ENC_NC;
  15768. }
  15769. if (sizeof(dist)/sizeof(word32) != WOLFMEM_DEF_BUCKETS) {
  15770. return WC_TEST_RET_ENC_NC;
  15771. }
  15772. if (WOLFMEM_DEF_BUCKETS > WOLFMEM_MAX_BUCKETS) {
  15773. return WC_TEST_RET_ENC_NC;
  15774. }
  15775. for (i = 0; i < WOLFMEM_DEF_BUCKETS; i++) {
  15776. if ((size[i] % WOLFSSL_STATIC_ALIGN) != 0) {
  15777. /* each element in array should be divisible by alignment size */
  15778. return WC_TEST_RET_ENC_NC;
  15779. }
  15780. }
  15781. for (i = 1; i < WOLFMEM_DEF_BUCKETS; i++) {
  15782. if (size[i - 1] >= size[i]) {
  15783. return WC_TEST_RET_ENC_NC; /* sizes should be in increasing order */
  15784. }
  15785. }
  15786. #ifndef WOLFSSL_STATIC_MEMORY_LEAN
  15787. /* check that padding size returned is possible */
  15788. if (wolfSSL_MemoryPaddingSz() < WOLFSSL_STATIC_ALIGN) {
  15789. return WC_TEST_RET_ENC_NC; /* no room for wc_Memory struct */
  15790. }
  15791. ret = wolfSSL_MemoryPaddingSz();
  15792. if (ret < 0) {
  15793. return WC_TEST_RET_ENC_EC(ret);
  15794. }
  15795. if (wolfSSL_MemoryPaddingSz() % WOLFSSL_STATIC_ALIGN != 0) {
  15796. return WC_TEST_RET_ENC_NC; /* not aligned! */
  15797. }
  15798. /* check function to return optimum buffer size (rounded down) */
  15799. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_GENERAL);
  15800. if (ret < 0) {
  15801. return WC_TEST_RET_ENC_EC(ret);
  15802. }
  15803. if ((ret - pad) % WOLFSSL_STATIC_ALIGN != 0) {
  15804. return WC_TEST_RET_ENC_NC; /* not aligned! */
  15805. }
  15806. if ((unsigned int)ret > sizeof(buffer)) {
  15807. return WC_TEST_RET_ENC_NC; /* did not round down as expected */
  15808. }
  15809. if (ret != wolfSSL_StaticBufferSz(buffer, ret, WOLFMEM_GENERAL)) {
  15810. return WC_TEST_RET_ENC_NC; /* return value changed when using suggested
  15811. * value
  15812. */
  15813. }
  15814. ret = wolfSSL_MemoryPaddingSz();
  15815. ret += pad; /* add space that is going to be needed if buffer not aligned */
  15816. if (wolfSSL_StaticBufferSz(buffer, size[0] + ret + 1, WOLFMEM_GENERAL) !=
  15817. (ret + (int)size[0])) {
  15818. return WC_TEST_RET_ENC_NC; /* did not round down to nearest bucket
  15819. * value
  15820. */
  15821. }
  15822. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_IO_POOL);
  15823. if ((ret - pad) < 0) {
  15824. return WC_TEST_RET_ENC_NC;
  15825. }
  15826. if (((ret - pad) % (WOLFMEM_IO_SZ + wolfSSL_MemoryPaddingSz())) != 0) {
  15827. return WC_TEST_RET_ENC_NC; /* not even chunks of memory for IO size */
  15828. }
  15829. if (((ret - pad) % WOLFSSL_STATIC_ALIGN) != 0) {
  15830. return WC_TEST_RET_ENC_NC; /* memory not aligned */
  15831. }
  15832. /* check for passing bad or unknown arguments to functions */
  15833. if (wolfSSL_StaticBufferSz(NULL, 1, WOLFMEM_GENERAL) > 0) {
  15834. return WC_TEST_RET_ENC_NC;
  15835. }
  15836. if (wolfSSL_StaticBufferSz(buffer, 1, WOLFMEM_GENERAL) != 0) {
  15837. return WC_TEST_RET_ENC_NC; /* should round to 0
  15838. since struct + bucket will not fit */
  15839. }
  15840. #endif
  15841. (void)pad;
  15842. (void)dist; /* avoid static analysis warning of variable not used */
  15843. #endif
  15844. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  15845. /* simple test */
  15846. ret = simple_mem_test(MEM_TEST_SZ);
  15847. if (ret != 0)
  15848. return ret;
  15849. #endif
  15850. #ifdef COMPLEX_MEM_TEST
  15851. /* test various size blocks */
  15852. for (i = 1; i < MEM_TEST_SZ; i*=2) {
  15853. ret = simple_mem_test(i);
  15854. if (ret != 0)
  15855. return ret;
  15856. }
  15857. #endif
  15858. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC) && defined(XREALLOC)
  15859. /* realloc test */
  15860. {
  15861. byte *b = (byte*)XMALLOC(MEM_TEST_SZ, HEAP_HINT,
  15862. DYNAMIC_TYPE_TMP_BUFFER);
  15863. #ifndef WOLFSSL_NO_REALLOC
  15864. byte *c = NULL;
  15865. if (b) {
  15866. c = (byte*)XREALLOC(b, MEM_TEST_SZ+sizeof(word32), HEAP_HINT,
  15867. DYNAMIC_TYPE_TMP_BUFFER);
  15868. if (c)
  15869. b = c;
  15870. }
  15871. #endif
  15872. if (b)
  15873. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15874. if ((b == NULL)
  15875. #ifndef WOLFSSL_NO_REALLOC
  15876. || (c == NULL)
  15877. #endif
  15878. ) {
  15879. return WC_TEST_RET_ENC_ERRNO;
  15880. }
  15881. }
  15882. #endif
  15883. if (ret == 0) {
  15884. /* This test is only interesting on embedded R/O Flash systems */
  15885. if (const_byte_ptr_test(const_byte_array, &j) != CBPTR_EXPECTED) {
  15886. ret = 1;
  15887. }
  15888. }
  15889. return ret;
  15890. }
  15891. #ifndef NO_FILESYSTEM
  15892. /* Cert Paths */
  15893. #ifdef FREESCALE_MQX
  15894. #define CERT_PREFIX "a:\\"
  15895. #define CERT_PATH_SEP "\\"
  15896. #elif defined(WOLFSSL_uTKERNEL2)
  15897. #define CERT_PREFIX "/uda/"
  15898. #define CERT_PATH_SEP "/"
  15899. #elif defined(_WIN32_WCE)
  15900. #define CERT_PREFIX "\\windows\\"
  15901. #define CERT_PATH_SEP "\\"
  15902. #endif
  15903. #ifndef CERT_PREFIX
  15904. #define CERT_PREFIX "./"
  15905. #endif
  15906. #ifndef CERT_PATH_SEP
  15907. #define CERT_PATH_SEP "/"
  15908. #endif
  15909. #ifndef CERT_WRITE_TEMP_DIR
  15910. #define CERT_WRITE_TEMP_DIR CERT_PREFIX
  15911. #endif
  15912. #define CERT_ROOT CERT_PREFIX "certs" CERT_PATH_SEP
  15913. /* Generated Test Certs */
  15914. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  15915. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  15916. #if !defined(NO_RSA) && !defined(NO_ASN)
  15917. static const char* clientKey = CERT_ROOT "client-key.der";
  15918. static const char* clientCert = CERT_ROOT "client-cert.der";
  15919. #ifdef WOLFSSL_CERT_EXT
  15920. static const char* clientKeyPub = CERT_ROOT "client-keyPub.der";
  15921. #endif
  15922. #endif /* !NO_RSA && !NO_ASN */
  15923. #endif
  15924. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  15925. #if !defined(NO_RSA) && !defined(NO_ASN)
  15926. #if defined(WOLFSSL_CERT_GEN) || defined(HAVE_PKCS7)
  15927. static const char* rsaCaKeyFile = CERT_ROOT "ca-key.der";
  15928. #ifdef WOLFSSL_CERT_GEN
  15929. static const char* rsaCaCertFile = CERT_ROOT "ca-cert.pem";
  15930. #endif
  15931. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  15932. static const char* rsaCaCertDerFile = CERT_ROOT "ca-cert.der";
  15933. #endif
  15934. #ifdef HAVE_PKCS7
  15935. static const char* rsaServerCertDerFile =
  15936. CERT_ROOT "server-cert.der";
  15937. static const char* rsaServerKeyDerFile =
  15938. CERT_ROOT "server-key.der";
  15939. #endif
  15940. #endif
  15941. #endif /* !NO_RSA && !NO_ASN */
  15942. #endif /* !USE_CERT_BUFFER_* */
  15943. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  15944. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  15945. #if !defined(NO_ASN) && !defined(NO_DH)
  15946. static const char* dhParamsFile = CERT_ROOT "dh2048.der";
  15947. #endif
  15948. #endif
  15949. #if !defined(NO_ASN) && !defined(NO_DH)
  15950. #if defined(WOLFSSL_DH_EXTRA) && (!defined(HAVE_FIPS) || \
  15951. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  15952. #if !defined(USE_CERT_BUFFERS_2048)
  15953. static const char* dhKeyFile = CERT_ROOT "statickeys/dh-ffdhe2048.der";
  15954. static const char* dhKeyPubFile = CERT_ROOT "statickeys/dh-ffdhe2048-pub.der";
  15955. #endif
  15956. #endif
  15957. #endif
  15958. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  15959. #ifndef NO_DSA
  15960. static const char* dsaKey = CERT_ROOT "dsa2048.der";
  15961. #endif
  15962. #endif /* !USE_CERT_BUFFER_* */
  15963. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ECC256)
  15964. #ifdef HAVE_ECC
  15965. /* cert files to be used in rsa cert gen test, check if RSA enabled */
  15966. #ifdef HAVE_ECC_KEY_IMPORT
  15967. static const char* eccKeyDerFile = CERT_ROOT "ecc-key.der";
  15968. #endif
  15969. #endif
  15970. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ASN)
  15971. #if defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN) && \
  15972. !defined(NO_ECC_SECP)
  15973. #ifndef NO_RSA
  15974. static const char* eccKeyPubFileDer = CERT_ROOT "ecc-keyPub.der";
  15975. #endif
  15976. #ifndef NO_ASN_TIME
  15977. static const char* eccCaKeyFile = CERT_ROOT "ca-ecc-key.der";
  15978. static const char* eccCaCertFile = CERT_ROOT "ca-ecc-cert.pem";
  15979. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  15980. static const char* eccCaKey384File =
  15981. CERT_ROOT "ca-ecc384-key.der";
  15982. static const char* eccCaCert384File =
  15983. CERT_ROOT "ca-ecc384-cert.pem";
  15984. #endif
  15985. #endif
  15986. #endif
  15987. #if defined(HAVE_PKCS7) && defined(HAVE_ECC)
  15988. static const char* eccClientKey = CERT_ROOT "ecc-client-key.der";
  15989. static const char* eccClientCert = CERT_ROOT "client-ecc-cert.der";
  15990. #endif
  15991. #endif /* HAVE_ECC */
  15992. #ifdef HAVE_ED25519
  15993. #ifdef WOLFSSL_TEST_CERT
  15994. static const char* serverEd25519Cert =
  15995. CERT_ROOT "ed25519/server-ed25519.der";
  15996. static const char* caEd25519Cert =
  15997. CERT_ROOT "ed25519/ca-ed25519.der";
  15998. #endif
  15999. #endif
  16000. #ifdef HAVE_ED448
  16001. #ifdef WOLFSSL_TEST_CERT
  16002. static const char* serverEd448Cert =
  16003. CERT_ROOT "ed448/server-ed448.der";
  16004. static const char* caEd448Cert = CERT_ROOT "ed448/ca-ed448.der";
  16005. #endif
  16006. #endif
  16007. #endif /* !USE_CERT_BUFFER_* */
  16008. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  16009. !defined(NO_FILESYSTEM)
  16010. static const char* certExtNc =
  16011. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nc.der";
  16012. static const char* certExtIa =
  16013. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-ia.der";
  16014. static const char* certExtNct =
  16015. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nct.der";
  16016. #ifndef WOLFSSL_ASN_INT_LEAD_0_ANY
  16017. static const char* certBadNegInt =
  16018. CERT_ROOT "test" CERT_PATH_SEP "cert-bad-neg-int.der";
  16019. #endif
  16020. static const char* certBadOid =
  16021. CERT_ROOT "test" CERT_PATH_SEP "cert-bad-oid.der";
  16022. #ifndef WOLFSSL_NO_ASN_STRICT
  16023. static const char* certBadUtf8 =
  16024. CERT_ROOT "test" CERT_PATH_SEP "cert-bad-utf8.der";
  16025. #endif
  16026. #endif
  16027. #ifndef NO_WRITE_TEMP_FILES
  16028. #ifdef HAVE_ECC
  16029. #ifndef NO_ECC_SECP
  16030. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  16031. static const char* certEccPemFile = CERT_WRITE_TEMP_DIR "certecc.pem";
  16032. static const char* certEccDerFile = CERT_WRITE_TEMP_DIR "certecc.der";
  16033. #endif
  16034. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  16035. static const char* certEccRsaPemFile = CERT_WRITE_TEMP_DIR "certeccrsa.pem";
  16036. static const char* certEccRsaDerFile = CERT_WRITE_TEMP_DIR "certeccrsa.der";
  16037. #endif
  16038. #endif
  16039. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  16040. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ASN_CRYPT)
  16041. static const char* eccCaKeyPemFile = CERT_WRITE_TEMP_DIR "ecc-key.pem";
  16042. static const char* eccPubKeyDerFile = CERT_WRITE_TEMP_DIR "ecc-public-key.der";
  16043. static const char* eccCaKeyTempFile = CERT_WRITE_TEMP_DIR "ecc-key.der";
  16044. #if defined(HAVE_PKCS8) && !defined(WC_NO_RNG) && \
  16045. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  16046. static const char* eccPkcs8KeyDerFile = CERT_WRITE_TEMP_DIR "ecc-key-pkcs8.der";
  16047. #endif
  16048. #endif /* HAVE_ECC_KEY_EXPORT */
  16049. #endif /* HAVE_ECC */
  16050. #ifndef NO_RSA
  16051. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  16052. static const char* otherCertDerFile = CERT_WRITE_TEMP_DIR "othercert.der";
  16053. static const char* certDerFile = CERT_WRITE_TEMP_DIR "cert.der";
  16054. static const char* otherCertPemFile = CERT_WRITE_TEMP_DIR "othercert.pem";
  16055. static const char* certPemFile = CERT_WRITE_TEMP_DIR "cert.pem";
  16056. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  16057. static const char* certReqDerFile = CERT_WRITE_TEMP_DIR "certreq.der";
  16058. static const char* certReqPemFile = CERT_WRITE_TEMP_DIR "certreq.pem";
  16059. #endif
  16060. #endif
  16061. #endif /* !NO_RSA */
  16062. #if !defined(NO_RSA) || !defined(NO_DSA)
  16063. #ifdef WOLFSSL_KEY_GEN
  16064. static const char* keyDerFile = CERT_WRITE_TEMP_DIR "key.der";
  16065. static const char* keyPemFile = CERT_WRITE_TEMP_DIR "key.pem";
  16066. #endif
  16067. #endif
  16068. #endif /* !NO_WRITE_TEMP_FILES */
  16069. #endif /* !NO_FILESYSTEM */
  16070. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  16071. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  16072. static CertName certDefaultName;
  16073. static void initDefaultName(void)
  16074. {
  16075. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  16076. NameAttrib* n;
  16077. #endif
  16078. XMEMCPY(certDefaultName.country, "US", sizeof("US"));
  16079. certDefaultName.countryEnc = CTC_PRINTABLE;
  16080. XMEMCPY(certDefaultName.state, "Oregon", sizeof("Oregon"));
  16081. certDefaultName.stateEnc = CTC_UTF8;
  16082. XMEMCPY(certDefaultName.street, "Main St", sizeof("Main St"));
  16083. certDefaultName.streetEnc = CTC_UTF8;
  16084. XMEMCPY(certDefaultName.locality, "Portland", sizeof("Portland"));
  16085. certDefaultName.localityEnc = CTC_UTF8;
  16086. XMEMCPY(certDefaultName.sur, "Test", sizeof("Test"));
  16087. certDefaultName.surEnc = CTC_UTF8;
  16088. XMEMCPY(certDefaultName.org, "wolfSSL", sizeof("wolfSSL"));
  16089. certDefaultName.orgEnc = CTC_UTF8;
  16090. XMEMCPY(certDefaultName.unit, "Development", sizeof("Development"));
  16091. certDefaultName.unitEnc = CTC_UTF8;
  16092. XMEMCPY(certDefaultName.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  16093. certDefaultName.commonNameEnc = CTC_UTF8;
  16094. XMEMCPY(certDefaultName.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  16095. certDefaultName.serialDevEnc = CTC_PRINTABLE;
  16096. XMEMCPY(certDefaultName.postalCode, "12-456", sizeof("12-456"));
  16097. certDefaultName.postalCodeEnc = CTC_PRINTABLE;
  16098. #ifdef WOLFSSL_CERT_EXT
  16099. XMEMCPY(certDefaultName.busCat, "Private Organization", sizeof("Private Organization"));
  16100. certDefaultName.busCatEnc = CTC_UTF8;
  16101. XMEMCPY(certDefaultName.joiSt, "US", sizeof("US"));
  16102. certDefaultName.joiStEnc = CTC_PRINTABLE;
  16103. XMEMCPY(certDefaultName.joiC, "Oregon", sizeof("Oregon"));
  16104. certDefaultName.joiCEnc = CTC_PRINTABLE;
  16105. #endif
  16106. XMEMCPY(certDefaultName.email, "info@wolfssl.com", sizeof("info@wolfssl.com"));
  16107. XMEMCPY(certDefaultName.userId, "TestUserID", sizeof("TestUserID"));
  16108. certDefaultName.userIdEnc = CTC_PRINTABLE;
  16109. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  16110. /* test having additional OUs and setting DC */
  16111. n = &certDefaultName.name[0];
  16112. n->id = ASN_ORGUNIT_NAME;
  16113. n->type = CTC_UTF8;
  16114. n->sz = XSTRLEN("Development-2");
  16115. XMEMCPY(n->value, "Development-2", sizeof("Development-2"));
  16116. #if CTC_MAX_ATTRIB > 3
  16117. n = &certDefaultName.name[1];
  16118. n->id = ASN_DOMAIN_COMPONENT;
  16119. n->type = CTC_UTF8;
  16120. n->sz = XSTRLEN("com");
  16121. XMEMCPY(n->value, "com", sizeof("com"));
  16122. n = &certDefaultName.name[2];
  16123. n->id = ASN_DOMAIN_COMPONENT;
  16124. n->type = CTC_UTF8;
  16125. n->sz = XSTRLEN("wolfssl");
  16126. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  16127. #endif
  16128. #endif /* WOLFSSL_MULTI_ATTRIB && WOLFSSL_TEST_CERT */
  16129. #ifdef WOLFSSL_CUSTOM_OID
  16130. /* TODO: Add test case for custom OID's */
  16131. #endif
  16132. }
  16133. #ifdef WOLFSSL_CERT_EXT
  16134. #if ((defined(HAVE_ED25519) || defined(HAVE_ED448)) && \
  16135. defined(WOLFSSL_TEST_CERT)) || defined(HAVE_ECC)
  16136. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage[] =
  16137. "digitalSignature,nonRepudiation";
  16138. #endif
  16139. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_ASN_TIME)
  16140. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage2[] =
  16141. "digitalSignature,nonRepudiation,keyEncipherment,keyAgreement";
  16142. #endif
  16143. #endif /* WOLFSSL_CERT_EXT */
  16144. #endif /* WOLFSSL_CERT_GEN */
  16145. #ifndef NO_RSA
  16146. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  16147. !defined(NO_FILESYSTEM)
  16148. static const byte minSerial[] = { 0x02, 0x01, 0x01 };
  16149. static const byte minName[] = { 0x30, 0x00 };
  16150. static const byte nameBad[] = {
  16151. 0x30, 0x08,
  16152. 0x31, 0x06,
  16153. 0x30, 0x04,
  16154. 0x06, 0x02,
  16155. 0x55, 0x04,
  16156. };
  16157. static const byte minDates[] = {
  16158. 0x30, 0x1e,
  16159. 0x17, 0x0d,
  16160. 0x31, 0x38, 0x30, 0x34, 0x31, 0x33, 0x31, 0x35,
  16161. 0x32, 0x33, 0x31, 0x30, 0x5a,
  16162. 0x17, 0x0d,
  16163. 0x32, 0x31, 0x30, 0x31, 0x30, 0x37, 0x31, 0x35,
  16164. 0x32, 0x33, 0x31, 0x30, 0x5a
  16165. };
  16166. static const byte minPubKey[] = {
  16167. 0x30, 0x1c,
  16168. 0x30, 0x0d,
  16169. 0x06, 0x09,
  16170. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  16171. 0x01,
  16172. 0x05, 0x00,
  16173. 0x03, 0x0b,
  16174. 0x00, 0x30, 0x08,
  16175. 0x02, 0x01,
  16176. 0x03,
  16177. 0x02, 0x03,
  16178. 0x01, 0x00, 0x01
  16179. };
  16180. static const byte minSigAlg[] = {
  16181. 0x30, 0x0d,
  16182. 0x06, 0x09,
  16183. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  16184. 0x0b,
  16185. 0x05, 0x00
  16186. };
  16187. static const byte minSig[] = {
  16188. 0x03, 0x01,
  16189. 0x00
  16190. };
  16191. static int add_seq(byte* certData, int offset, byte* data, byte length)
  16192. {
  16193. XMEMMOVE(certData + offset + 2, data, length);
  16194. certData[offset++] = 0x30;
  16195. certData[offset++] = length;
  16196. return offset + length;
  16197. }
  16198. static int add_data(byte* certData, int offset, const byte* data, byte length)
  16199. {
  16200. XMEMCPY(certData + offset, data, length);
  16201. return offset + length;
  16202. }
  16203. static wc_test_ret_t cert_asn1_test(void)
  16204. {
  16205. wc_test_ret_t ret;
  16206. int len[3];
  16207. DecodedCert cert;
  16208. byte certData[114];
  16209. byte* badCert = NULL;
  16210. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  16211. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  16212. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  16213. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  16214. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  16215. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  16216. len[1] = add_seq(certData, 0, certData, len[2]);
  16217. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  16218. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  16219. len[0] = add_seq(certData, 0, certData, len[1]);
  16220. /* Minimal good certificate */
  16221. InitDecodedCert(&cert, certData, len[0], 0);
  16222. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  16223. FreeDecodedCert(&cert);
  16224. if (ret != 0)
  16225. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16226. /* Bad issuer name */
  16227. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  16228. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  16229. len[2] = add_data(certData, len[2], nameBad, (byte)sizeof(nameBad));
  16230. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  16231. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  16232. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  16233. len[1] = add_seq(certData, 0, certData, len[2]);
  16234. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  16235. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  16236. len[0] = add_seq(certData, 0, certData, len[1]);
  16237. /* Put data into allocated buffer to allow access error checking. */
  16238. badCert = (byte*)XMALLOC(len[0], HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16239. XMEMCPY(badCert, certData, len[0]);
  16240. InitDecodedCert(&cert, badCert, len[0], 0);
  16241. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  16242. FreeDecodedCert(&cert);
  16243. if (ret != ASN_PARSE_E) {
  16244. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16245. }
  16246. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16247. badCert = NULL;
  16248. ret = 0;
  16249. done:
  16250. if (badCert != NULL)
  16251. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16252. return ret;
  16253. }
  16254. static wc_test_ret_t cert_load_bad(const char* fname, byte* tmp, int err)
  16255. {
  16256. wc_test_ret_t ret;
  16257. DecodedCert cert;
  16258. XFILE file;
  16259. size_t bytes;
  16260. if ((fname == NULL) || (tmp == NULL)) {
  16261. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  16262. }
  16263. file = XFOPEN(fname, "rb");
  16264. if (!file) {
  16265. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16266. }
  16267. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16268. XFCLOSE(file);
  16269. if (bytes == 0) {
  16270. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16271. }
  16272. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16273. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  16274. FreeDecodedCert(&cert);
  16275. if (ret != err) {
  16276. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16277. }
  16278. ret = 0;
  16279. done:
  16280. return ret;
  16281. }
  16282. static wc_test_ret_t cert_bad_asn1_test(void)
  16283. {
  16284. wc_test_ret_t ret = 0;
  16285. byte* tmp;
  16286. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16287. if (tmp == NULL) {
  16288. ret = WC_TEST_RET_ENC_ERRNO;
  16289. }
  16290. #ifndef WOLFSSL_ASN_INT_LEAD_0_ANY
  16291. if (ret == 0) {
  16292. /* Serial number: 0xff 0xa8. 0xff and top bit set on next byte invalid.
  16293. */
  16294. ret = cert_load_bad(certBadNegInt, tmp, ASN_EXPECT_0_E);
  16295. }
  16296. #endif
  16297. if (ret == 0) {
  16298. /* Subject name OID: 55 04 f4. Last byte with top bit set invalid. */
  16299. ret = cert_load_bad(certBadOid, tmp, ASN_PARSE_E);
  16300. }
  16301. #ifndef WOLFSSL_NO_ASN_STRICT
  16302. if (ret == 0) {
  16303. /* Issuer name UTF8STRING: df 52 4e 44. Top bit of second byte not set.
  16304. */
  16305. ret = cert_load_bad(certBadUtf8, tmp, ASN_PARSE_E);
  16306. }
  16307. #endif
  16308. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16309. return ret;
  16310. }
  16311. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cert_test(void)
  16312. {
  16313. #if !defined(NO_FILESYSTEM)
  16314. DecodedCert cert;
  16315. byte* tmp;
  16316. size_t bytes;
  16317. XFILE file;
  16318. wc_test_ret_t ret;
  16319. WOLFSSL_ENTER("cert_test");
  16320. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16321. if (tmp == NULL)
  16322. return WC_TEST_RET_ENC_ERRNO;
  16323. /* Certificate with Name Constraints extension. */
  16324. file = XFOPEN(certExtNc, "rb");
  16325. if (!file) {
  16326. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16327. }
  16328. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16329. XFCLOSE(file);
  16330. if (bytes == 0)
  16331. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16332. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16333. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  16334. if (ret != 0)
  16335. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16336. FreeDecodedCert(&cert);
  16337. /* Certificate with Inhibit Any Policy extension. */
  16338. file = XFOPEN(certExtIa, "rb");
  16339. if (!file) {
  16340. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16341. }
  16342. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16343. XFCLOSE(file);
  16344. if (bytes == 0)
  16345. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16346. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16347. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  16348. if (ret != 0)
  16349. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16350. FreeDecodedCert(&cert);
  16351. /* Certificate with Netscape Certificate Type extension. */
  16352. file = XFOPEN(certExtNct, "rb");
  16353. if (!file) {
  16354. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16355. }
  16356. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16357. XFCLOSE(file);
  16358. if (bytes == 0)
  16359. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16360. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16361. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  16362. #ifndef IGNORE_NETSCAPE_CERT_TYPE
  16363. if (ret != 0)
  16364. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16365. #else
  16366. if (ret != ASN_CRIT_EXT_E) {
  16367. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16368. }
  16369. ret = 0;
  16370. #endif
  16371. done:
  16372. FreeDecodedCert(&cert);
  16373. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16374. #endif /* !NO_FILESYSTEM */
  16375. if (ret == 0)
  16376. ret = cert_asn1_test();
  16377. if (ret == 0)
  16378. ret = cert_bad_asn1_test();
  16379. return ret;
  16380. }
  16381. #endif /* WOLFSSL_TEST_CERT */
  16382. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  16383. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  16384. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certext_test(void)
  16385. {
  16386. DecodedCert cert;
  16387. byte* tmp;
  16388. size_t bytes;
  16389. XFILE file;
  16390. wc_test_ret_t ret;
  16391. /* created from rsa_test : othercert.der */
  16392. byte skid_rsa[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  16393. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  16394. /* created from rsa_test : othercert.der */
  16395. byte akid_rsa[] = "\x27\x8E\x67\x11\x74\xC3\x26\x1D\x3F\xED"
  16396. "\x33\x63\xB3\xA4\xD8\x1D\x30\xE5\xE8\xD5";
  16397. #ifdef HAVE_ECC
  16398. /* created from ecc_test_cert_gen : certecc.der */
  16399. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  16400. /* Authority key id from ./certs/ca-ecc384-cert.pem */
  16401. byte akid_ecc[] = "\xAB\xE0\xC3\x26\x4C\x18\xD4\x72\xBB\xD2"
  16402. "\x84\x8C\x9C\x0A\x05\x92\x80\x12\x53\x52";
  16403. #else
  16404. /* Authority key id from ./certs/ca-ecc-cert.pem */
  16405. byte akid_ecc[] = "\x56\x8E\x9A\xC3\xF0\x42\xDE\x18\xB9\x45"
  16406. "\x55\x6E\xF9\x93\xCF\xEA\xC3\xF3\xA5\x21";
  16407. #endif
  16408. #endif /* HAVE_ECC */
  16409. /* created from rsa_test : cert.der */
  16410. byte kid_ca[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  16411. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  16412. WOLFSSL_ENTER("certext_test");
  16413. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16414. if (tmp == NULL)
  16415. return WC_TEST_RET_ENC_ERRNO;
  16416. /* load othercert.der (Cert signed by an authority) */
  16417. file = XFOPEN(otherCertDerFile, "rb");
  16418. if (!file) {
  16419. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16420. return WC_TEST_RET_ENC_ERRNO;
  16421. }
  16422. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16423. XFCLOSE(file);
  16424. if (bytes == 0)
  16425. return WC_TEST_RET_ENC_ERRNO;
  16426. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16427. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  16428. if (ret != 0)
  16429. return WC_TEST_RET_ENC_EC(ret);
  16430. /* check the SKID from a RSA certificate */
  16431. if (XMEMCMP(skid_rsa, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  16432. return WC_TEST_RET_ENC_NC;
  16433. /* check the AKID from an RSA certificate */
  16434. if (XMEMCMP(akid_rsa, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  16435. return WC_TEST_RET_ENC_NC;
  16436. /* check the Key Usage from an RSA certificate */
  16437. if (!cert.extKeyUsageSet)
  16438. return WC_TEST_RET_ENC_NC;
  16439. if (cert.extKeyUsage != (KEYUSE_KEY_ENCIPHER|KEYUSE_KEY_AGREE))
  16440. return WC_TEST_RET_ENC_NC;
  16441. /* check the CA Basic Constraints from an RSA certificate */
  16442. if (cert.isCA)
  16443. return WC_TEST_RET_ENC_NC;
  16444. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  16445. /* check the Certificate Policies Id */
  16446. if (cert.extCertPoliciesNb != 1)
  16447. return WC_TEST_RET_ENC_NC;
  16448. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  16449. return WC_TEST_RET_ENC_NC;
  16450. #endif
  16451. FreeDecodedCert(&cert);
  16452. #ifdef HAVE_ECC
  16453. /* load certecc.der (Cert signed by our ECC CA test in ecc_test_cert_gen) */
  16454. file = XFOPEN(certEccDerFile, "rb");
  16455. if (!file) {
  16456. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16457. return WC_TEST_RET_ENC_ERRNO;
  16458. }
  16459. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16460. XFCLOSE(file);
  16461. if (bytes == 0)
  16462. return WC_TEST_RET_ENC_ERRNO;
  16463. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16464. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  16465. if (ret != 0)
  16466. return WC_TEST_RET_ENC_EC(ret);
  16467. /* check the SKID from a ECC certificate - generated dynamically */
  16468. /* check the AKID from an ECC certificate */
  16469. if (XMEMCMP(akid_ecc, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  16470. return WC_TEST_RET_ENC_NC;
  16471. /* check the Key Usage from an ECC certificate */
  16472. if (!cert.extKeyUsageSet)
  16473. return WC_TEST_RET_ENC_NC;
  16474. if (cert.extKeyUsage != (KEYUSE_DIGITAL_SIG|KEYUSE_CONTENT_COMMIT))
  16475. return WC_TEST_RET_ENC_NC;
  16476. /* check the CA Basic Constraints from an ECC certificate */
  16477. if (cert.isCA)
  16478. return WC_TEST_RET_ENC_NC;
  16479. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  16480. /* check the Certificate Policies Id */
  16481. if (cert.extCertPoliciesNb != 2)
  16482. return WC_TEST_RET_ENC_NC;
  16483. if (strncmp(cert.extCertPolicies[0], "2.4.589440.587.101.2.1.9632587.1", 32))
  16484. return WC_TEST_RET_ENC_NC;
  16485. if (strncmp(cert.extCertPolicies[1], "1.2.13025.489.1.113549", 22))
  16486. return WC_TEST_RET_ENC_NC;
  16487. #endif
  16488. FreeDecodedCert(&cert);
  16489. #endif /* HAVE_ECC */
  16490. /* load cert.der (self signed certificate) */
  16491. file = XFOPEN(certDerFile, "rb");
  16492. if (!file) {
  16493. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16494. return WC_TEST_RET_ENC_ERRNO;
  16495. }
  16496. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16497. XFCLOSE(file);
  16498. if (bytes == 0)
  16499. return WC_TEST_RET_ENC_ERRNO;
  16500. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16501. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  16502. if (ret != 0)
  16503. return WC_TEST_RET_ENC_EC(ret);
  16504. /* check the SKID from a CA certificate */
  16505. if (XMEMCMP(kid_ca, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  16506. return WC_TEST_RET_ENC_NC;
  16507. /* check the AKID from an CA certificate */
  16508. if (XMEMCMP(kid_ca, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  16509. return WC_TEST_RET_ENC_NC;
  16510. /* check the Key Usage from CA certificate */
  16511. if (!cert.extKeyUsageSet)
  16512. return WC_TEST_RET_ENC_NC;
  16513. if (cert.extKeyUsage != (KEYUSE_KEY_CERT_SIGN|KEYUSE_CRL_SIGN))
  16514. return WC_TEST_RET_ENC_NC;
  16515. /* check the CA Basic Constraints CA certificate */
  16516. if (!cert.isCA)
  16517. return WC_TEST_RET_ENC_NC;
  16518. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  16519. /* check the Certificate Policies Id */
  16520. if (cert.extCertPoliciesNb != 2)
  16521. return WC_TEST_RET_ENC_NC;
  16522. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  16523. return WC_TEST_RET_ENC_NC;
  16524. if (strncmp(cert.extCertPolicies[1], "1.2.840.113549.1.9.16.6.5", 25))
  16525. return WC_TEST_RET_ENC_NC;
  16526. #endif
  16527. FreeDecodedCert(&cert);
  16528. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16529. return 0;
  16530. }
  16531. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_TEST_CERT &&
  16532. !NO_FILESYSTEM && WOLFSSL_CERT_GEN */
  16533. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  16534. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  16535. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t decodedCertCache_test(void)
  16536. {
  16537. wc_test_ret_t ret = 0;
  16538. Cert cert;
  16539. FILE* file;
  16540. byte* der;
  16541. word32 derSz;
  16542. WOLFSSL_ENTER("decodedCertCache_test");
  16543. derSz = FOURK_BUF;
  16544. der = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16545. if (der == NULL)
  16546. ret = WC_TEST_RET_ENC_NC;
  16547. if (ret == 0) {
  16548. /* load cert.der */
  16549. file = XFOPEN(certDerFile, "rb");
  16550. if (file != NULL) {
  16551. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  16552. XFCLOSE(file);
  16553. if (derSz == 0)
  16554. ret = WC_TEST_RET_ENC_ERRNO;
  16555. }
  16556. else
  16557. ret = WC_TEST_RET_ENC_ERRNO;
  16558. }
  16559. if (ret == 0) {
  16560. ret = wc_InitCert_ex(&cert, HEAP_HINT, devId);
  16561. if (ret != 0)
  16562. ret = WC_TEST_RET_ENC_EC(ret);
  16563. }
  16564. if (ret == 0) {
  16565. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  16566. if (ret != 0)
  16567. ret = WC_TEST_RET_ENC_EC(ret);
  16568. }
  16569. if (ret == 0) {
  16570. ret = wc_SetSubjectBuffer(NULL, der, derSz);
  16571. if (ret == BAD_FUNC_ARG)
  16572. ret = 0;
  16573. else
  16574. ret = WC_TEST_RET_ENC_EC(ret);
  16575. }
  16576. if (ret == 0) {
  16577. ret = wc_SetSubjectRaw(&cert, der, derSz);
  16578. if (ret != 0)
  16579. ret = WC_TEST_RET_ENC_EC(ret);
  16580. }
  16581. if (ret == 0) {
  16582. ret = wc_SetSubjectRaw(NULL, der, derSz);
  16583. if (ret == BAD_FUNC_ARG)
  16584. ret = 0;
  16585. else
  16586. ret = WC_TEST_RET_ENC_EC(ret);
  16587. }
  16588. if (ret == 0) {
  16589. ret = wc_SetIssuerBuffer(&cert, der, derSz);
  16590. if (ret != 0)
  16591. ret = WC_TEST_RET_ENC_EC(ret);
  16592. }
  16593. if (ret == 0) {
  16594. ret = wc_SetIssuerBuffer(NULL, der, derSz);
  16595. if (ret == BAD_FUNC_ARG)
  16596. ret = 0;
  16597. else
  16598. ret = WC_TEST_RET_ENC_EC(ret);
  16599. }
  16600. if (ret == 0) {
  16601. ret = wc_SetIssuerRaw(&cert, der, derSz);
  16602. if (ret != 0)
  16603. ret = WC_TEST_RET_ENC_EC(ret);
  16604. }
  16605. if (ret == 0) {
  16606. ret = wc_SetIssuerRaw(NULL, der, derSz);
  16607. if (ret == BAD_FUNC_ARG)
  16608. ret = 0;
  16609. else
  16610. ret = WC_TEST_RET_ENC_EC(ret);
  16611. }
  16612. #ifdef WOLFSSL_ALT_NAMES
  16613. if (ret == 0) {
  16614. ret = wc_SetAltNamesBuffer(&cert, der, derSz);
  16615. if (ret != 0)
  16616. ret = WC_TEST_RET_ENC_EC(ret);
  16617. }
  16618. if (ret == 0) {
  16619. ret = wc_SetAltNamesBuffer(NULL, der, derSz);
  16620. if (ret == BAD_FUNC_ARG)
  16621. ret = 0;
  16622. else
  16623. ret = WC_TEST_RET_ENC_EC(ret);
  16624. }
  16625. if (ret == 0) {
  16626. ret = wc_SetDatesBuffer(&cert, der, derSz);
  16627. if (ret != 0)
  16628. ret = WC_TEST_RET_ENC_EC(ret);
  16629. }
  16630. if (ret == 0) {
  16631. ret = wc_SetDatesBuffer(NULL, der, derSz);
  16632. if (ret == BAD_FUNC_ARG)
  16633. ret = 0;
  16634. else
  16635. ret = WC_TEST_RET_ENC_EC(ret);
  16636. }
  16637. #endif
  16638. if (ret == 0) {
  16639. ret = wc_SetAuthKeyIdFromCert(&cert, der, derSz);
  16640. if (ret != 0)
  16641. ret = WC_TEST_RET_ENC_EC(ret);
  16642. }
  16643. if (ret == 0) {
  16644. ret = wc_SetAuthKeyIdFromCert(NULL, der, derSz);
  16645. if (ret == BAD_FUNC_ARG)
  16646. ret = 0;
  16647. else
  16648. ret = WC_TEST_RET_ENC_NC;
  16649. }
  16650. wc_SetCert_Free(&cert);
  16651. if (ret == 0) {
  16652. if(cert.decodedCert != NULL)
  16653. ret = WC_TEST_RET_ENC_NC;
  16654. }
  16655. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16656. return ret;
  16657. }
  16658. #endif /* defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) &&
  16659. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) */
  16660. #define RSA_TEST_BYTES 512 /* up to 4096-bit key */
  16661. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  16662. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  16663. static wc_test_ret_t rsa_flatten_test(RsaKey* key)
  16664. {
  16665. wc_test_ret_t ret;
  16666. byte e[RSA_TEST_BYTES];
  16667. byte n[RSA_TEST_BYTES];
  16668. word32 eSz = sizeof(e);
  16669. word32 nSz = sizeof(n);
  16670. /* Parameter Validation testing. */
  16671. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  16672. if (ret != BAD_FUNC_ARG)
  16673. return WC_TEST_RET_ENC_EC(ret);
  16674. ret = wc_RsaFlattenPublicKey(key, NULL, &eSz, n, &nSz);
  16675. if (ret != BAD_FUNC_ARG)
  16676. return WC_TEST_RET_ENC_EC(ret);
  16677. ret = wc_RsaFlattenPublicKey(key, e, NULL, n, &nSz);
  16678. if (ret != BAD_FUNC_ARG)
  16679. return WC_TEST_RET_ENC_EC(ret);
  16680. ret = wc_RsaFlattenPublicKey(key, e, &eSz, NULL, &nSz);
  16681. if (ret != BAD_FUNC_ARG)
  16682. return WC_TEST_RET_ENC_EC(ret);
  16683. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, NULL);
  16684. if (ret != BAD_FUNC_ARG)
  16685. return WC_TEST_RET_ENC_EC(ret);
  16686. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  16687. if (ret != 0)
  16688. return WC_TEST_RET_ENC_EC(ret);
  16689. eSz = 0;
  16690. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  16691. if (ret != RSA_BUFFER_E)
  16692. return WC_TEST_RET_ENC_EC(ret);
  16693. eSz = sizeof(e);
  16694. nSz = 0;
  16695. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  16696. if (ret != RSA_BUFFER_E)
  16697. return WC_TEST_RET_ENC_EC(ret);
  16698. return 0;
  16699. }
  16700. #endif /* NO_ASN */
  16701. #if !defined(HAVE_FIPS) && !defined(NO_ASN) \
  16702. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  16703. static wc_test_ret_t rsa_export_key_test(RsaKey* key)
  16704. {
  16705. wc_test_ret_t ret;
  16706. byte e[3];
  16707. word32 eSz = sizeof(e);
  16708. byte n[RSA_TEST_BYTES];
  16709. word32 nSz = sizeof(n);
  16710. byte d[RSA_TEST_BYTES];
  16711. word32 dSz = sizeof(d);
  16712. byte p[RSA_TEST_BYTES/2];
  16713. word32 pSz = sizeof(p);
  16714. byte q[RSA_TEST_BYTES/2];
  16715. word32 qSz = sizeof(q);
  16716. word32 zero = 0;
  16717. ret = wc_RsaExportKey(NULL, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  16718. if (ret != BAD_FUNC_ARG)
  16719. return WC_TEST_RET_ENC_EC(ret);
  16720. ret = wc_RsaExportKey(key, NULL, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  16721. if (ret != BAD_FUNC_ARG)
  16722. return WC_TEST_RET_ENC_EC(ret);
  16723. ret = wc_RsaExportKey(key, e, NULL, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  16724. if (ret != BAD_FUNC_ARG)
  16725. return WC_TEST_RET_ENC_EC(ret);
  16726. ret = wc_RsaExportKey(key, e, &eSz, NULL, &nSz, d, &dSz, p, &pSz, q, &qSz);
  16727. if (ret != BAD_FUNC_ARG)
  16728. return WC_TEST_RET_ENC_EC(ret);
  16729. ret = wc_RsaExportKey(key, e, &eSz, n, NULL, d, &dSz, p, &pSz, q, &qSz);
  16730. if (ret != BAD_FUNC_ARG)
  16731. return WC_TEST_RET_ENC_EC(ret);
  16732. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, NULL, &dSz, p, &pSz, q, &qSz);
  16733. if (ret != BAD_FUNC_ARG)
  16734. return WC_TEST_RET_ENC_EC(ret);
  16735. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, NULL, p, &pSz, q, &qSz);
  16736. if (ret != BAD_FUNC_ARG)
  16737. return WC_TEST_RET_ENC_EC(ret);
  16738. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, NULL, &pSz, q, &qSz);
  16739. if (ret != BAD_FUNC_ARG)
  16740. return WC_TEST_RET_ENC_EC(ret);
  16741. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, NULL, q, &qSz);
  16742. if (ret != BAD_FUNC_ARG)
  16743. return WC_TEST_RET_ENC_EC(ret);
  16744. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, NULL, &qSz);
  16745. if (ret != BAD_FUNC_ARG)
  16746. return WC_TEST_RET_ENC_EC(ret);
  16747. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, NULL);
  16748. if (ret != BAD_FUNC_ARG)
  16749. return WC_TEST_RET_ENC_EC(ret);
  16750. ret = wc_RsaExportKey(key, e, &zero, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  16751. if (ret != RSA_BUFFER_E)
  16752. return WC_TEST_RET_ENC_EC(ret);
  16753. ret = wc_RsaExportKey(key, e, &eSz, n, &zero, d, &dSz, p, &pSz, q, &qSz);
  16754. if (ret != RSA_BUFFER_E)
  16755. return WC_TEST_RET_ENC_EC(ret);
  16756. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16757. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &zero, p, &pSz, q, &qSz);
  16758. if (ret != RSA_BUFFER_E)
  16759. return WC_TEST_RET_ENC_EC(ret);
  16760. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &zero, q, &qSz);
  16761. if (ret != RSA_BUFFER_E)
  16762. return WC_TEST_RET_ENC_EC(ret);
  16763. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &zero);
  16764. if (ret != RSA_BUFFER_E)
  16765. return WC_TEST_RET_ENC_EC(ret);
  16766. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  16767. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  16768. if (ret != 0)
  16769. return WC_TEST_RET_ENC_EC(ret);
  16770. return 0;
  16771. }
  16772. #endif /* !HAVE_FIPS && !NO_ASN && !WOLFSSL_RSA_VERIFY_ONLY */
  16773. #ifndef NO_SIG_WRAPPER
  16774. static wc_test_ret_t rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng)
  16775. {
  16776. wc_test_ret_t ret;
  16777. word32 sigSz;
  16778. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  16779. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  16780. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  16781. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  16782. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  16783. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  16784. };
  16785. WOLFSSL_SMALL_STACK_STATIC const byte hashEnc[] = {
  16786. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  16787. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  16788. 0x00, 0x04, 0x20,
  16789. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  16790. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  16791. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  16792. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  16793. };
  16794. word32 inLen = (word32)XSTRLEN((char*)in);
  16795. byte out[RSA_TEST_BYTES];
  16796. /* Parameter Validation testing. */
  16797. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_NONE, key, keyLen);
  16798. if (ret != BAD_FUNC_ARG)
  16799. return WC_TEST_RET_ENC_EC(ret);
  16800. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, 0);
  16801. if (ret != BAD_FUNC_ARG)
  16802. return WC_TEST_RET_ENC_EC(ret);
  16803. sigSz = (word32)modLen;
  16804. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  16805. inLen, out, &sigSz, key, keyLen, rng);
  16806. if (ret != BAD_FUNC_ARG)
  16807. return WC_TEST_RET_ENC_EC(ret);
  16808. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16809. 0, out, &sigSz, key, keyLen, rng);
  16810. if (ret != BAD_FUNC_ARG)
  16811. return WC_TEST_RET_ENC_EC(ret);
  16812. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16813. inLen, NULL, &sigSz, key, keyLen, rng);
  16814. if (ret != BAD_FUNC_ARG)
  16815. return WC_TEST_RET_ENC_EC(ret);
  16816. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16817. inLen, out, NULL, key, keyLen, rng);
  16818. if (ret != BAD_FUNC_ARG)
  16819. return WC_TEST_RET_ENC_EC(ret);
  16820. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16821. inLen, out, &sigSz, NULL, keyLen, rng);
  16822. if (ret != BAD_FUNC_ARG)
  16823. return WC_TEST_RET_ENC_EC(ret);
  16824. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16825. inLen, out, &sigSz, key, 0, rng);
  16826. if (ret != BAD_FUNC_ARG)
  16827. return WC_TEST_RET_ENC_EC(ret);
  16828. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16829. inLen, out, &sigSz, key, keyLen, NULL);
  16830. #if defined(WOLFSSL_AFALG_XILINX_RSA) || defined(WOLFSSL_XILINX_CRYPT)
  16831. /* blinding / rng handled with hardware acceleration */
  16832. if (ret != 0)
  16833. #elif defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  16834. /* async may not require RNG */
  16835. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  16836. if (ret != NO_VALID_DEVID)
  16837. #else
  16838. if (ret != 0 && ret != MISSING_RNG_E)
  16839. #endif
  16840. #elif defined(HAVE_FIPS) || !defined(WC_RSA_BLINDING)
  16841. /* FIPS140 implementation does not do blinding */
  16842. if (ret != 0)
  16843. #elif defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_RSA_VERIFY_ONLY)
  16844. if (ret != SIG_TYPE_E)
  16845. #elif defined(WOLFSSL_CRYPTOCELL) || defined(WOLFSSL_SE050)
  16846. /* RNG is handled by hardware */
  16847. if (ret != 0)
  16848. #else
  16849. if (ret != MISSING_RNG_E)
  16850. #endif
  16851. return WC_TEST_RET_ENC_EC(ret);
  16852. sigSz = 0;
  16853. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16854. inLen, out, &sigSz, key, keyLen, rng);
  16855. if (ret != BAD_FUNC_ARG)
  16856. return WC_TEST_RET_ENC_EC(ret);
  16857. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  16858. inLen, out, (word32)modLen, key, keyLen);
  16859. if (ret != BAD_FUNC_ARG)
  16860. return WC_TEST_RET_ENC_EC(ret);
  16861. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16862. 0, out, (word32)modLen, key, keyLen);
  16863. if (ret != BAD_FUNC_ARG)
  16864. return WC_TEST_RET_ENC_EC(ret);
  16865. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16866. inLen, NULL, (word32)modLen, key, keyLen);
  16867. if (ret != BAD_FUNC_ARG)
  16868. return WC_TEST_RET_ENC_EC(ret);
  16869. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16870. inLen, out, 0, key, keyLen);
  16871. if (ret != BAD_FUNC_ARG)
  16872. return WC_TEST_RET_ENC_EC(ret);
  16873. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16874. inLen, out, (word32)modLen, NULL, keyLen);
  16875. if (ret != BAD_FUNC_ARG)
  16876. return WC_TEST_RET_ENC_EC(ret);
  16877. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16878. inLen, out, (word32)modLen, key, 0);
  16879. if (ret != BAD_FUNC_ARG)
  16880. return WC_TEST_RET_ENC_EC(ret);
  16881. #ifndef HAVE_ECC
  16882. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, keyLen);
  16883. if (ret != SIG_TYPE_E)
  16884. return WC_TEST_RET_ENC_EC(ret);
  16885. #endif
  16886. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  16887. return 0;
  16888. #endif
  16889. /* Use APIs. */
  16890. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, keyLen);
  16891. if (ret != modLen)
  16892. return WC_TEST_RET_ENC_EC(ret);
  16893. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA_W_ENC, key, keyLen);
  16894. if (ret != modLen)
  16895. return WC_TEST_RET_ENC_EC(ret);
  16896. sigSz = (word32)ret;
  16897. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  16898. XMEMSET(out, 0, sizeof(out));
  16899. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16900. inLen, out, &sigSz, key, keyLen, rng);
  16901. if (ret != 0)
  16902. return WC_TEST_RET_ENC_EC(ret);
  16903. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16904. inLen, out, (word32)modLen, key, keyLen);
  16905. if (ret != 0)
  16906. return WC_TEST_RET_ENC_EC(ret);
  16907. sigSz = (word32)sizeof(out);
  16908. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  16909. in, inLen, out, &sigSz, key, keyLen, rng);
  16910. if (ret != 0)
  16911. return WC_TEST_RET_ENC_EC(ret);
  16912. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  16913. in, inLen, out, (word32)modLen, key, keyLen);
  16914. if (ret != 0)
  16915. return WC_TEST_RET_ENC_EC(ret);
  16916. /* Wrong signature type. */
  16917. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16918. inLen, out, (word32)modLen, key, keyLen);
  16919. if (ret == 0)
  16920. return WC_TEST_RET_ENC_EC(ret);
  16921. /* check hash functions */
  16922. sigSz = (word32)sizeof(out);
  16923. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  16924. hash, (int)sizeof(hash), out, &sigSz, key, keyLen, rng);
  16925. if (ret != 0)
  16926. return WC_TEST_RET_ENC_EC(ret);
  16927. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  16928. hash, (int)sizeof(hash), out, (word32)modLen, key, keyLen);
  16929. if (ret != 0)
  16930. return WC_TEST_RET_ENC_EC(ret);
  16931. sigSz = (word32)sizeof(out);
  16932. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  16933. hashEnc, (int)sizeof(hashEnc), out, &sigSz, key, keyLen, rng);
  16934. if (ret != 0)
  16935. return WC_TEST_RET_ENC_EC(ret);
  16936. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  16937. hashEnc, (int)sizeof(hashEnc), out, (word32)modLen, key, keyLen);
  16938. if (ret != 0)
  16939. return WC_TEST_RET_ENC_EC(ret);
  16940. #else
  16941. (void)hash;
  16942. (void)hashEnc;
  16943. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */
  16944. return 0;
  16945. }
  16946. #endif /* !NO_SIG_WRAPPER */
  16947. #ifdef WC_RSA_NONBLOCK
  16948. static wc_test_ret_t rsa_nb_test(RsaKey* key, const byte* in, word32 inLen, byte* out,
  16949. word32 outSz, byte* plain, word32 plainSz, WC_RNG* rng)
  16950. {
  16951. wc_test_ret_t ret = 0;
  16952. int count;
  16953. int signSz = 0;
  16954. RsaNb nb;
  16955. byte* inlinePlain = NULL;
  16956. /* Enable non-blocking RSA mode - provide context */
  16957. ret = wc_RsaSetNonBlock(key, &nb);
  16958. if (ret != 0)
  16959. return ret;
  16960. #ifdef WC_RSA_NONBLOCK_TIME
  16961. /* Enable time based RSA blocking. 8 microseconds max (3.1GHz) */
  16962. ret = wc_RsaSetNonBlockTime(key, 8, 3100);
  16963. if (ret != 0)
  16964. return ret;
  16965. #endif
  16966. count = 0;
  16967. do {
  16968. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, rng);
  16969. count++; /* track number of would blocks */
  16970. if (ret == FP_WOULDBLOCK) {
  16971. /* do "other" work here */
  16972. }
  16973. } while (ret == FP_WOULDBLOCK);
  16974. if (ret < 0) {
  16975. return ret;
  16976. }
  16977. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  16978. printf("RSA non-block sign: %d times\n", count);
  16979. #endif
  16980. signSz = ret;
  16981. /* Test non-blocking verify */
  16982. XMEMSET(plain, 0, plainSz);
  16983. count = 0;
  16984. do {
  16985. ret = wc_RsaSSL_Verify(out, (word32)signSz, plain, plainSz, key);
  16986. count++; /* track number of would blocks */
  16987. if (ret == FP_WOULDBLOCK) {
  16988. /* do "other" work here */
  16989. }
  16990. } while (ret == FP_WOULDBLOCK);
  16991. if (ret < 0) {
  16992. return ret;
  16993. }
  16994. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  16995. printf("RSA non-block verify: %d times\n", count);
  16996. #endif
  16997. if (signSz == ret && XMEMCMP(plain, in, (size_t)ret)) {
  16998. return SIG_VERIFY_E;
  16999. }
  17000. /* Test inline non-blocking verify */
  17001. count = 0;
  17002. do {
  17003. ret = wc_RsaSSL_VerifyInline(out, (word32)signSz, &inlinePlain, key);
  17004. count++; /* track number of would blocks */
  17005. if (ret == FP_WOULDBLOCK) {
  17006. /* do "other" work here */
  17007. }
  17008. } while (ret == FP_WOULDBLOCK);
  17009. if (ret < 0) {
  17010. return ret;
  17011. }
  17012. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  17013. printf("RSA non-block inline verify: %d times\n", count);
  17014. #endif
  17015. if (signSz == ret && XMEMCMP(inlinePlain, in, (size_t)ret)) {
  17016. return SIG_VERIFY_E;
  17017. }
  17018. /* Disabling non-block RSA mode */
  17019. ret = wc_RsaSetNonBlock(key, NULL);
  17020. (void)count;
  17021. return 0;
  17022. }
  17023. #endif
  17024. #if !defined(NO_ASN)
  17025. static wc_test_ret_t rsa_decode_test(RsaKey* keyPub)
  17026. {
  17027. wc_test_ret_t ret;
  17028. word32 inSz;
  17029. word32 inOutIdx;
  17030. WOLFSSL_SMALL_STACK_STATIC const byte n[2] = { 0x00, 0x23 };
  17031. WOLFSSL_SMALL_STACK_STATIC const byte e[2] = { 0x00, 0x03 };
  17032. WOLFSSL_SMALL_STACK_STATIC const byte good[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1,
  17033. 0x03 };
  17034. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgId[] = {
  17035. 0x30, 0x18, 0x30, 0x16,
  17036. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17037. 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17038. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgIdNull[] = {
  17039. 0x30, 0x1a, 0x30, 0x18,
  17040. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17041. 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  17042. 0x02, 0x1, 0x03 };
  17043. WOLFSSL_SMALL_STACK_STATIC const byte badAlgIdNull[] = {
  17044. 0x30, 0x1b, 0x30, 0x19,
  17045. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17046. 0x05, 0x01, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  17047. 0x02, 0x1, 0x03 };
  17048. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitString[] = {
  17049. 0x30, 0x18, 0x30, 0x16,
  17050. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17051. 0x04, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17052. WOLFSSL_SMALL_STACK_STATIC const byte badBitStringLen[] = {
  17053. 0x30, 0x18, 0x30, 0x16,
  17054. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17055. 0x03, 0x0a, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17056. WOLFSSL_SMALL_STACK_STATIC const byte badNoSeq[] = {
  17057. 0x30, 0x16, 0x30, 0x14,
  17058. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17059. 0x07, 0x00, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17060. WOLFSSL_SMALL_STACK_STATIC const byte badNoObj[] = {
  17061. 0x30, 0x0f, 0x30, 0x0d, 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06,
  17062. 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17063. WOLFSSL_SMALL_STACK_STATIC const byte badIntN[] = {
  17064. 0x30, 0x06, 0x02, 0x05, 0x23, 0x02, 0x1, 0x03 };
  17065. WOLFSSL_SMALL_STACK_STATIC const byte badNotIntE[] = {
  17066. 0x30, 0x06, 0x02, 0x01, 0x23, 0x04, 0x1, 0x03 };
  17067. WOLFSSL_SMALL_STACK_STATIC const byte badLength[] = {
  17068. 0x30, 0x04, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17069. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrNoZero[] = {
  17070. 0x30, 0x17, 0x30, 0x15,
  17071. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17072. 0x03, 0x08, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17073. ret = wc_InitRsaKey(keyPub, NULL);
  17074. if (ret != 0)
  17075. return WC_TEST_RET_ENC_EC(ret);
  17076. /* Parameter Validation testing. */
  17077. ret = wc_RsaPublicKeyDecodeRaw(NULL, sizeof(n), e, sizeof(e), keyPub);
  17078. if (ret != BAD_FUNC_ARG) {
  17079. ret = WC_TEST_RET_ENC_EC(ret);
  17080. goto done;
  17081. }
  17082. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), NULL, sizeof(e), keyPub);
  17083. if (ret != BAD_FUNC_ARG) {
  17084. ret = WC_TEST_RET_ENC_EC(ret);
  17085. goto done;
  17086. }
  17087. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), NULL);
  17088. if (ret != BAD_FUNC_ARG) {
  17089. ret = WC_TEST_RET_ENC_EC(ret);
  17090. goto done;
  17091. }
  17092. ret = wc_RsaPublicKeyDecodeRaw(n, (word32)-1, e, sizeof(e), keyPub);
  17093. #if defined(USE_INTEGER_HEAP_MATH)
  17094. if (ret != 0)
  17095. #else
  17096. if (ret != ASN_GETINT_E)
  17097. #endif
  17098. {
  17099. ret = WC_TEST_RET_ENC_EC(ret);
  17100. goto done;
  17101. }
  17102. wc_FreeRsaKey(keyPub);
  17103. ret = wc_InitRsaKey(keyPub, NULL);
  17104. if (ret != 0)
  17105. return WC_TEST_RET_ENC_EC(ret);
  17106. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, (word32)-1, keyPub);
  17107. #if defined(USE_INTEGER_HEAP_MATH)
  17108. if (ret != 0)
  17109. #else
  17110. if (ret != ASN_GETINT_E)
  17111. #endif
  17112. {
  17113. ret = WC_TEST_RET_ENC_EC(ret);
  17114. goto done;
  17115. }
  17116. wc_FreeRsaKey(keyPub);
  17117. ret = wc_InitRsaKey(keyPub, NULL);
  17118. if (ret != 0)
  17119. return WC_TEST_RET_ENC_EC(ret);
  17120. /* Use API. */
  17121. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), keyPub);
  17122. if (ret != 0) {
  17123. ret = WC_TEST_RET_ENC_EC(ret);
  17124. goto done;
  17125. }
  17126. wc_FreeRsaKey(keyPub);
  17127. ret = wc_InitRsaKey(keyPub, NULL);
  17128. if (ret != 0)
  17129. return WC_TEST_RET_ENC_EC(ret);
  17130. /* Parameter Validation testing. */
  17131. inSz = sizeof(good);
  17132. ret = wc_RsaPublicKeyDecode(NULL, &inOutIdx, keyPub, inSz);
  17133. if (ret != BAD_FUNC_ARG) {
  17134. ret = WC_TEST_RET_ENC_EC(ret);
  17135. goto done;
  17136. }
  17137. ret = wc_RsaPublicKeyDecode(good, NULL, keyPub, inSz);
  17138. if (ret != BAD_FUNC_ARG) {
  17139. ret = WC_TEST_RET_ENC_EC(ret);
  17140. goto done;
  17141. }
  17142. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  17143. if (ret != BAD_FUNC_ARG) {
  17144. ret = WC_TEST_RET_ENC_EC(ret);
  17145. goto done;
  17146. }
  17147. /* Use good data and offset to bad data. */
  17148. inOutIdx = 2;
  17149. inSz = sizeof(good) - inOutIdx;
  17150. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  17151. if (ret != ASN_PARSE_E) {
  17152. ret = WC_TEST_RET_ENC_EC(ret);
  17153. goto done;
  17154. }
  17155. inOutIdx = 2;
  17156. inSz = sizeof(goodAlgId) - inOutIdx;
  17157. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  17158. if (ret != ASN_PARSE_E) {
  17159. ret = WC_TEST_RET_ENC_EC(ret);
  17160. goto done;
  17161. }
  17162. inOutIdx = 2;
  17163. inSz = sizeof(goodAlgId);
  17164. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  17165. #ifndef WOLFSSL_NO_DECODE_EXTRA
  17166. if (ret != ASN_PARSE_E)
  17167. #else
  17168. if (ret != ASN_RSA_KEY_E)
  17169. #endif
  17170. {
  17171. ret = WC_TEST_RET_ENC_EC(ret);
  17172. goto done;
  17173. }
  17174. /* Try different bad data. */
  17175. inSz = sizeof(badAlgIdNull);
  17176. inOutIdx = 0;
  17177. ret = wc_RsaPublicKeyDecode(badAlgIdNull, &inOutIdx, keyPub, inSz);
  17178. if (ret != ASN_EXPECT_0_E) {
  17179. ret = WC_TEST_RET_ENC_EC(ret);
  17180. goto done;
  17181. }
  17182. inSz = sizeof(badNotBitString);
  17183. inOutIdx = 0;
  17184. ret = wc_RsaPublicKeyDecode(badNotBitString, &inOutIdx, keyPub, inSz);
  17185. if (ret != ASN_BITSTR_E) {
  17186. ret = WC_TEST_RET_ENC_EC(ret);
  17187. goto done;
  17188. }
  17189. inSz = sizeof(badBitStringLen);
  17190. inOutIdx = 0;
  17191. ret = wc_RsaPublicKeyDecode(badBitStringLen, &inOutIdx, keyPub, inSz);
  17192. if (ret != ASN_PARSE_E) {
  17193. ret = WC_TEST_RET_ENC_EC(ret);
  17194. goto done;
  17195. }
  17196. inSz = sizeof(badNoSeq);
  17197. inOutIdx = 0;
  17198. ret = wc_RsaPublicKeyDecode(badNoSeq, &inOutIdx, keyPub, inSz);
  17199. if (ret != ASN_PARSE_E) {
  17200. ret = WC_TEST_RET_ENC_EC(ret);
  17201. goto done;
  17202. }
  17203. inSz = sizeof(badNoObj);
  17204. inOutIdx = 0;
  17205. ret = wc_RsaPublicKeyDecode(badNoObj, &inOutIdx, keyPub, inSz);
  17206. if (ret != ASN_PARSE_E && ret != ASN_OBJECT_ID_E) {
  17207. ret = WC_TEST_RET_ENC_EC(ret);
  17208. goto done;
  17209. }
  17210. inSz = sizeof(badIntN);
  17211. inOutIdx = 0;
  17212. ret = wc_RsaPublicKeyDecode(badIntN, &inOutIdx, keyPub, inSz);
  17213. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  17214. ret = WC_TEST_RET_ENC_EC(ret);
  17215. goto done;
  17216. }
  17217. inSz = sizeof(badNotIntE);
  17218. inOutIdx = 0;
  17219. ret = wc_RsaPublicKeyDecode(badNotIntE, &inOutIdx, keyPub, inSz);
  17220. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  17221. ret = WC_TEST_RET_ENC_EC(ret);
  17222. goto done;
  17223. }
  17224. /* TODO: Shouldn't pass as the sequence length is too small. */
  17225. inSz = sizeof(badLength);
  17226. inOutIdx = 0;
  17227. ret = wc_RsaPublicKeyDecode(badLength, &inOutIdx, keyPub, inSz);
  17228. #ifndef WOLFSSL_ASN_TEMPLATE
  17229. if (ret != 0)
  17230. #else
  17231. if (ret != ASN_PARSE_E)
  17232. #endif
  17233. {
  17234. ret = WC_TEST_RET_ENC_EC(ret);
  17235. goto done;
  17236. }
  17237. /* TODO: Shouldn't ignore object id's data. */
  17238. wc_FreeRsaKey(keyPub);
  17239. ret = wc_InitRsaKey(keyPub, NULL);
  17240. if (ret != 0)
  17241. return WC_TEST_RET_ENC_EC(ret);
  17242. inSz = sizeof(badBitStrNoZero);
  17243. inOutIdx = 0;
  17244. ret = wc_RsaPublicKeyDecode(badBitStrNoZero, &inOutIdx, keyPub, inSz);
  17245. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  17246. ret = WC_TEST_RET_ENC_EC(ret);
  17247. goto done;
  17248. }
  17249. wc_FreeRsaKey(keyPub);
  17250. ret = wc_InitRsaKey(keyPub, NULL);
  17251. if (ret != 0)
  17252. return WC_TEST_RET_ENC_EC(ret);
  17253. /* Valid data cases. */
  17254. inSz = sizeof(good);
  17255. inOutIdx = 0;
  17256. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  17257. if (ret != 0) {
  17258. ret = WC_TEST_RET_ENC_EC(ret);
  17259. goto done;
  17260. }
  17261. if (inOutIdx != inSz) {
  17262. ret = WC_TEST_RET_ENC_NC;
  17263. goto done;
  17264. }
  17265. wc_FreeRsaKey(keyPub);
  17266. ret = wc_InitRsaKey(keyPub, NULL);
  17267. if (ret != 0)
  17268. return WC_TEST_RET_ENC_EC(ret);
  17269. inSz = sizeof(goodAlgId);
  17270. inOutIdx = 0;
  17271. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  17272. if (ret != 0) {
  17273. ret = WC_TEST_RET_ENC_EC(ret);
  17274. goto done;
  17275. }
  17276. if (inOutIdx != inSz) {
  17277. ret = WC_TEST_RET_ENC_NC;
  17278. goto done;
  17279. }
  17280. wc_FreeRsaKey(keyPub);
  17281. ret = wc_InitRsaKey(keyPub, NULL);
  17282. if (ret != 0)
  17283. return WC_TEST_RET_ENC_EC(ret);
  17284. inSz = sizeof(goodAlgIdNull);
  17285. inOutIdx = 0;
  17286. ret = wc_RsaPublicKeyDecode(goodAlgIdNull, &inOutIdx, keyPub, inSz);
  17287. if (ret != 0) {
  17288. ret = WC_TEST_RET_ENC_EC(ret);
  17289. goto done;
  17290. }
  17291. if (inOutIdx != inSz) {
  17292. ret = WC_TEST_RET_ENC_NC;
  17293. goto done;
  17294. }
  17295. done:
  17296. wc_FreeRsaKey(keyPub);
  17297. return ret;
  17298. }
  17299. #endif
  17300. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  17301. /* Need to create known good signatures to test with this. */
  17302. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  17303. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  17304. static wc_test_ret_t rsa_pss_test(WC_RNG* rng, RsaKey* key)
  17305. {
  17306. byte digest[WC_MAX_DIGEST_SIZE];
  17307. wc_test_ret_t ret = 0;
  17308. const char inStr[] = TEST_STRING;
  17309. word32 inLen = (word32)TEST_STRING_SZ;
  17310. word32 outSz;
  17311. word32 plainSz;
  17312. word32 digestSz;
  17313. int i, j;
  17314. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  17315. int k, l;
  17316. #endif
  17317. #ifndef WOLFSSL_SE050
  17318. int len;
  17319. #endif
  17320. byte* plain;
  17321. int mgf[] = {
  17322. #ifndef NO_SHA
  17323. WC_MGF1SHA1,
  17324. #endif
  17325. #ifdef WOLFSSL_SHA224
  17326. WC_MGF1SHA224,
  17327. #endif
  17328. WC_MGF1SHA256,
  17329. #ifdef WOLFSSL_SHA384
  17330. WC_MGF1SHA384,
  17331. #endif
  17332. #ifdef WOLFSSL_SHA512
  17333. WC_MGF1SHA512
  17334. #endif
  17335. };
  17336. enum wc_HashType hash[] = {
  17337. #ifndef NO_SHA
  17338. WC_HASH_TYPE_SHA,
  17339. #endif
  17340. #ifdef WOLFSSL_SHA224
  17341. WC_HASH_TYPE_SHA224,
  17342. #endif
  17343. WC_HASH_TYPE_SHA256,
  17344. #ifdef WOLFSSL_SHA384
  17345. WC_HASH_TYPE_SHA384,
  17346. #endif
  17347. #ifdef WOLFSSL_SHA512
  17348. WC_HASH_TYPE_SHA512,
  17349. #endif
  17350. };
  17351. WC_DECLARE_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
  17352. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17353. WC_DECLARE_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  17354. WC_ALLOC_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
  17355. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17356. WC_ALLOC_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  17357. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17358. if (in == NULL || out == NULL || sig == NULL)
  17359. ERROR_OUT(MEMORY_E, exit_rsa_pss);
  17360. #endif
  17361. XMEMCPY(in, inStr, inLen);
  17362. /* Test all combinations of hash and MGF. */
  17363. for (j = 0; j < (int)(sizeof(hash)/sizeof(*hash)); j++) {
  17364. /* Calculate hash of message. */
  17365. ret = wc_Hash(hash[j], in, inLen, digest, sizeof(digest));
  17366. if (ret != 0)
  17367. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17368. digestSz = (word32)wc_HashGetDigestSize(hash[j]);
  17369. #ifdef WOLFSSL_SE050
  17370. /* SE050 only supports MGF matched to same hash type */
  17371. i = j;
  17372. #else
  17373. for (i = 0; i < (int)(sizeof(mgf)/sizeof(*mgf)); i++) {
  17374. #endif
  17375. outSz = RSA_TEST_BYTES;
  17376. do {
  17377. #if defined(WOLFSSL_ASYNC_CRYPT)
  17378. ret = wc_AsyncWait(ret, &key->asyncDev,
  17379. WC_ASYNC_FLAG_CALL_AGAIN);
  17380. #endif
  17381. if (ret >= 0) {
  17382. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz,
  17383. hash[j], mgf[i], -1, key, rng);
  17384. }
  17385. } while (ret == WC_PENDING_E);
  17386. if (ret <= 0)
  17387. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17388. outSz = (word32)ret;
  17389. XMEMCPY(sig, out, outSz);
  17390. plain = NULL;
  17391. TEST_SLEEP();
  17392. do {
  17393. #if defined(WOLFSSL_ASYNC_CRYPT)
  17394. ret = wc_AsyncWait(ret, &key->asyncDev,
  17395. WC_ASYNC_FLAG_CALL_AGAIN);
  17396. #endif
  17397. if (ret >= 0) {
  17398. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[j],
  17399. mgf[i], -1, key);
  17400. }
  17401. } while (ret == WC_PENDING_E);
  17402. if (ret <= 0)
  17403. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17404. plainSz = (word32)ret;
  17405. TEST_SLEEP();
  17406. #if defined(HAVE_SELFTEST) && \
  17407. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  17408. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17409. hash[j], -1);
  17410. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  17411. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17412. hash[j], -1, 0);
  17413. #else
  17414. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz,
  17415. hash[j], -1, wc_RsaEncryptSize(key)*8, HEAP_HINT);
  17416. #endif
  17417. if (ret != 0)
  17418. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17419. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  17420. for (k = 0; k < (int)(sizeof(mgf)/sizeof(*mgf)); k++) {
  17421. for (l = 0; l < (int)(sizeof(hash)/sizeof(*hash)); l++) {
  17422. if (i == k && j == l)
  17423. continue;
  17424. XMEMCPY(sig, out, outSz);
  17425. do {
  17426. #if defined(WOLFSSL_ASYNC_CRYPT)
  17427. ret = wc_AsyncWait(ret, &key->asyncDev,
  17428. WC_ASYNC_FLAG_CALL_AGAIN);
  17429. #endif
  17430. if (ret >= 0) {
  17431. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz,
  17432. (byte**)&plain, hash[l], mgf[k], -1, key);
  17433. }
  17434. } while (ret == WC_PENDING_E);
  17435. if (ret >= 0)
  17436. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17437. }
  17438. }
  17439. #endif
  17440. #ifndef WOLFSSL_SE050
  17441. } /* end mgf for loop */
  17442. #endif
  17443. }
  17444. /* SE050 generates salts internally only of hash length */
  17445. #ifndef WOLFSSL_SE050
  17446. /* Test that a salt length of zero works. */
  17447. digestSz = (word32)wc_HashGetDigestSize(hash[0]);
  17448. outSz = RSA_TEST_BYTES;
  17449. do {
  17450. #if defined(WOLFSSL_ASYNC_CRYPT)
  17451. ret = wc_AsyncWait(ret, &key->asyncDev,
  17452. WC_ASYNC_FLAG_CALL_AGAIN);
  17453. #endif
  17454. if (ret >= 0) {
  17455. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  17456. mgf[0], 0, key, rng);
  17457. }
  17458. } while (ret == WC_PENDING_E);
  17459. if (ret <= 0)
  17460. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17461. outSz = (word32)ret;
  17462. TEST_SLEEP();
  17463. do {
  17464. #if defined(WOLFSSL_ASYNC_CRYPT)
  17465. ret = wc_AsyncWait(ret, &key->asyncDev,
  17466. WC_ASYNC_FLAG_CALL_AGAIN);
  17467. #endif
  17468. if (ret >= 0) {
  17469. ret = wc_RsaPSS_Verify_ex(out, outSz, sig, outSz, hash[0], mgf[0],
  17470. 0, key);
  17471. }
  17472. } while (ret == WC_PENDING_E);
  17473. if (ret <= 0)
  17474. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17475. plainSz = (word32)ret;
  17476. TEST_SLEEP();
  17477. do {
  17478. #if defined(WOLFSSL_ASYNC_CRYPT)
  17479. ret = wc_AsyncWait(ret, &key->asyncDev,
  17480. WC_ASYNC_FLAG_CALL_AGAIN);
  17481. #endif
  17482. if (ret >= 0) {
  17483. #if defined(HAVE_SELFTEST) && \
  17484. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  17485. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  17486. hash[0], 0);
  17487. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  17488. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  17489. hash[0], 0, 0);
  17490. #else
  17491. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, sig, plainSz,
  17492. hash[0], 0, 0, HEAP_HINT);
  17493. #endif
  17494. }
  17495. } while (ret == WC_PENDING_E);
  17496. if (ret != 0)
  17497. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17498. XMEMCPY(sig, out, outSz);
  17499. plain = NULL;
  17500. do {
  17501. #if defined(WOLFSSL_ASYNC_CRYPT)
  17502. ret = wc_AsyncWait(ret, &key->asyncDev,
  17503. WC_ASYNC_FLAG_CALL_AGAIN);
  17504. #endif
  17505. if (ret >= 0) {
  17506. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  17507. 0, key);
  17508. }
  17509. } while (ret == WC_PENDING_E);
  17510. if (ret <= 0)
  17511. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17512. plainSz = (word32)ret;
  17513. TEST_SLEEP();
  17514. #if defined(HAVE_SELFTEST) && \
  17515. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  17516. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17517. hash[0], 0);
  17518. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  17519. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17520. hash[0], 0, 0);
  17521. #else
  17522. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  17523. 0, 0, HEAP_HINT);
  17524. #endif
  17525. if (ret != 0)
  17526. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17527. /* Test bad salt lengths in various APIs. */
  17528. digestSz = (word32)wc_HashGetDigestSize(hash[0]);
  17529. outSz = RSA_TEST_BYTES;
  17530. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  17531. len = -2;
  17532. #else
  17533. len = -3;
  17534. #endif
  17535. do {
  17536. #if defined(WOLFSSL_ASYNC_CRYPT)
  17537. ret = wc_AsyncWait(ret, &key->asyncDev,
  17538. WC_ASYNC_FLAG_CALL_AGAIN);
  17539. #endif
  17540. if (ret >= 0) {
  17541. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  17542. mgf[0], len, key, rng);
  17543. }
  17544. } while (ret == WC_PENDING_E);
  17545. if (ret != PSS_SALTLEN_E)
  17546. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17547. do {
  17548. #if defined(WOLFSSL_ASYNC_CRYPT)
  17549. ret = wc_AsyncWait(ret, &key->asyncDev,
  17550. WC_ASYNC_FLAG_CALL_AGAIN);
  17551. #endif
  17552. if (ret >= 0) {
  17553. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  17554. mgf[0], digestSz + 1, key, rng);
  17555. }
  17556. } while (ret == WC_PENDING_E);
  17557. if (ret != PSS_SALTLEN_E)
  17558. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17559. TEST_SLEEP();
  17560. do {
  17561. #if defined(WOLFSSL_ASYNC_CRYPT)
  17562. ret = wc_AsyncWait(ret, &key->asyncDev,
  17563. WC_ASYNC_FLAG_CALL_AGAIN);
  17564. #endif
  17565. if (ret >= 0) {
  17566. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0],
  17567. mgf[0], -2, key);
  17568. }
  17569. } while (ret == WC_PENDING_E);
  17570. if (ret != PSS_SALTLEN_E)
  17571. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17572. TEST_SLEEP();
  17573. do {
  17574. #if defined(WOLFSSL_ASYNC_CRYPT)
  17575. ret = wc_AsyncWait(ret, &key->asyncDev,
  17576. WC_ASYNC_FLAG_CALL_AGAIN);
  17577. #endif
  17578. if (ret >= 0) {
  17579. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  17580. digestSz + 1, key);
  17581. }
  17582. } while (ret == WC_PENDING_E);
  17583. if (ret != PSS_SALTLEN_E)
  17584. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17585. TEST_SLEEP();
  17586. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  17587. len = -2;
  17588. #else
  17589. len = -3;
  17590. #endif
  17591. #if defined(HAVE_SELFTEST) && \
  17592. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  17593. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17594. hash[0], len);
  17595. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  17596. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17597. hash[0], len, 0);
  17598. #else
  17599. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  17600. len, 0, HEAP_HINT);
  17601. #endif
  17602. if (ret != PSS_SALTLEN_E)
  17603. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17604. #ifndef WOLFSSL_PSS_LONG_SALT
  17605. len = (int)(digestSz + 1);
  17606. #else
  17607. len = (int)(plainSz - digestSz - 1);
  17608. #endif
  17609. #if defined(HAVE_SELFTEST) && \
  17610. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  17611. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17612. hash[0], len);
  17613. if (ret != PSS_SALTLEN_E)
  17614. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17615. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  17616. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17617. hash[0], len, 0);
  17618. if (ret != BAD_PADDING_E)
  17619. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17620. #else
  17621. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  17622. len, 0, HEAP_HINT);
  17623. if (ret != PSS_SALTLEN_E)
  17624. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17625. #endif
  17626. ret = 0;
  17627. #endif /* WOLFSSL_SE050 */
  17628. exit_rsa_pss:
  17629. WC_FREE_VAR(sig, HEAP_HINT);
  17630. WC_FREE_VAR(in, HEAP_HINT);
  17631. WC_FREE_VAR(out, HEAP_HINT);
  17632. return ret;
  17633. }
  17634. #endif /* !WOLFSSL_RSA_VERIFY_ONLY && !WOLFSSL_RSA_PUBLIC_ONLY */
  17635. #endif
  17636. #ifdef WC_RSA_NO_PADDING
  17637. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_no_pad_test(void)
  17638. {
  17639. WC_RNG rng;
  17640. byte* tmp = NULL;
  17641. size_t bytes;
  17642. wc_test_ret_t ret;
  17643. word32 inLen = 0;
  17644. word32 idx = 0;
  17645. word32 outSz = RSA_TEST_BYTES;
  17646. word32 plainSz = RSA_TEST_BYTES;
  17647. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  17648. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  17649. !defined(NO_FILESYSTEM)
  17650. XFILE file;
  17651. #endif
  17652. WC_DECLARE_VAR(key, RsaKey, 1, HEAP_HINT);
  17653. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17654. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  17655. WC_ALLOC_VAR(key, RsaKey, 1, HEAP_HINT);
  17656. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17657. WC_ALLOC_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  17658. WOLFSSL_ENTER("rsa_no_pad_test");
  17659. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17660. if (key == NULL || out == NULL || plain == NULL)
  17661. ERROR_OUT(MEMORY_E, exit_rsa_nopadding);
  17662. #endif
  17663. /* initialize stack structures */
  17664. XMEMSET(&rng, 0, sizeof(rng));
  17665. XMEMSET(key, 0, sizeof(RsaKey));
  17666. #ifdef USE_CERT_BUFFERS_1024
  17667. bytes = (size_t)sizeof_client_key_der_1024;
  17668. if (bytes < (size_t)sizeof_client_cert_der_1024)
  17669. bytes = (size_t)sizeof_client_cert_der_1024;
  17670. #elif defined(USE_CERT_BUFFERS_2048)
  17671. bytes = (size_t)sizeof_client_key_der_2048;
  17672. if (bytes < (size_t)sizeof_client_cert_der_2048)
  17673. bytes = (size_t)sizeof_client_cert_der_2048;
  17674. #else
  17675. bytes = FOURK_BUF;
  17676. #endif
  17677. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17678. if (tmp == NULL
  17679. #ifdef WOLFSSL_ASYNC_CRYPT
  17680. || out == NULL || plain == NULL
  17681. #endif
  17682. ) {
  17683. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  17684. }
  17685. #ifdef USE_CERT_BUFFERS_1024
  17686. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  17687. #elif defined(USE_CERT_BUFFERS_2048)
  17688. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  17689. #elif defined(USE_CERT_BUFFERS_3072)
  17690. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  17691. #elif defined(USE_CERT_BUFFERS_4096)
  17692. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  17693. #elif !defined(NO_FILESYSTEM)
  17694. file = XFOPEN(clientKey, "rb");
  17695. if (!file) {
  17696. err_sys("can't open clientKey, Please run from wolfSSL home dir",
  17697. WC_TEST_RET_ENC_ERRNO);
  17698. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_nopadding);
  17699. }
  17700. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  17701. XFCLOSE(file);
  17702. if (bytes == 0)
  17703. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_nopadding);
  17704. #else
  17705. /* No key to use. */
  17706. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  17707. #endif /* USE_CERT_BUFFERS */
  17708. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  17709. if (ret != 0) {
  17710. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17711. }
  17712. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  17713. if (ret != 0) {
  17714. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17715. }
  17716. /* after loading in key use tmp as the test buffer */
  17717. #ifndef HAVE_FIPS
  17718. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  17719. #else
  17720. ret = wc_InitRng(&rng);
  17721. #endif
  17722. if (ret != 0) {
  17723. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17724. }
  17725. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  17726. inLen = (word32)wc_RsaEncryptSize(key);
  17727. outSz = inLen;
  17728. plainSz = inLen;
  17729. XMEMSET(tmp, 7, inLen);
  17730. do {
  17731. #if defined(WOLFSSL_ASYNC_CRYPT)
  17732. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17733. #endif
  17734. if (ret >= 0) {
  17735. ret = wc_RsaDirect(tmp, inLen, out, &outSz, key,
  17736. RSA_PRIVATE_ENCRYPT, &rng);
  17737. }
  17738. } while (ret == WC_PENDING_E);
  17739. if (ret <= 0) {
  17740. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17741. }
  17742. /* encrypted result should not be the same as input */
  17743. if (XMEMCMP(out, tmp, inLen) == 0) {
  17744. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  17745. }
  17746. TEST_SLEEP();
  17747. /* decrypt with public key and compare result */
  17748. do {
  17749. #if defined(WOLFSSL_ASYNC_CRYPT)
  17750. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17751. #endif
  17752. if (ret >= 0) {
  17753. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key,
  17754. RSA_PUBLIC_DECRYPT, &rng);
  17755. }
  17756. } while (ret == WC_PENDING_E);
  17757. if (ret <= 0) {
  17758. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17759. }
  17760. if (XMEMCMP(plain, tmp, inLen) != 0) {
  17761. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  17762. }
  17763. TEST_SLEEP();
  17764. #endif
  17765. #ifdef WC_RSA_BLINDING
  17766. ret = wc_RsaSetRNG(NULL, &rng);
  17767. if (ret != BAD_FUNC_ARG) {
  17768. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17769. }
  17770. ret = wc_RsaSetRNG(key, &rng);
  17771. if (ret < 0) {
  17772. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17773. }
  17774. #endif
  17775. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  17776. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  17777. do {
  17778. #if defined(WOLFSSL_ASYNC_CRYPT)
  17779. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17780. #endif
  17781. if (ret >= 0) {
  17782. ret = wc_RsaPublicEncrypt_ex(tmp, inLen, out, outSz, key, &rng,
  17783. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  17784. }
  17785. } while (ret == WC_PENDING_E);
  17786. if (ret < 0) {
  17787. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17788. }
  17789. TEST_SLEEP();
  17790. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  17791. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17792. do {
  17793. #if defined(WOLFSSL_ASYNC_CRYPT)
  17794. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17795. #endif
  17796. if (ret >= 0) {
  17797. ret = wc_RsaPrivateDecrypt_ex(out, outSz, plain, plainSz, key,
  17798. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  17799. }
  17800. } while (ret == WC_PENDING_E);
  17801. if (ret < 0) {
  17802. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17803. }
  17804. if (XMEMCMP(plain, tmp, inLen) != 0) {
  17805. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  17806. }
  17807. TEST_SLEEP();
  17808. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  17809. /* test some bad arguments */
  17810. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key, -1,
  17811. &rng);
  17812. if (ret != BAD_FUNC_ARG) {
  17813. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17814. }
  17815. ret = wc_RsaDirect(out, outSz, plain, &plainSz, NULL, RSA_PUBLIC_DECRYPT,
  17816. &rng);
  17817. if (ret != BAD_FUNC_ARG) {
  17818. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17819. }
  17820. ret = wc_RsaDirect(out, outSz, NULL, &plainSz, key, RSA_PUBLIC_DECRYPT,
  17821. &rng);
  17822. if (ret != LENGTH_ONLY_E || plainSz != inLen) {
  17823. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17824. }
  17825. ret = wc_RsaDirect(out, outSz - 10, plain, &plainSz, key,
  17826. RSA_PUBLIC_DECRYPT, &rng);
  17827. if (ret != BAD_FUNC_ARG) {
  17828. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17829. }
  17830. /* if making it to this point of code without hitting an ERROR_OUT then
  17831. * all tests have passed */
  17832. ret = 0;
  17833. exit_rsa_nopadding:
  17834. wc_FreeRsaKey(key);
  17835. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17836. WC_FREE_VAR(key, HEAP_HINT);
  17837. WC_FREE_VAR(out, HEAP_HINT);
  17838. WC_FREE_VAR(plain, HEAP_HINT);
  17839. wc_FreeRng(&rng);
  17840. return ret;
  17841. }
  17842. #endif /* WC_RSA_NO_PADDING */
  17843. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  17844. static wc_test_ret_t rsa_even_mod_test(WC_RNG* rng, RsaKey* key)
  17845. {
  17846. byte* tmp = NULL;
  17847. size_t bytes;
  17848. wc_test_ret_t ret;
  17849. word32 inLen = 0;
  17850. #ifndef NO_ASN
  17851. word32 idx = 0;
  17852. #endif
  17853. word32 outSz = RSA_TEST_BYTES;
  17854. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17855. word32 plainSz = RSA_TEST_BYTES;
  17856. #endif
  17857. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_3072) && \
  17858. !defined(USE_CERT_BUFFERS_4096) && !defined(NO_FILESYSTEM)
  17859. XFILE file;
  17860. #endif
  17861. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17862. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17863. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  17864. #endif
  17865. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17866. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17867. WC_ALLOC_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  17868. #endif
  17869. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17870. if (out == NULL
  17871. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17872. || plain == NULL
  17873. #endif
  17874. ) {
  17875. ERROR_OUT(MEMORY_E, exit_rsa_even_mod);
  17876. }
  17877. #endif
  17878. #if defined(USE_CERT_BUFFERS_2048)
  17879. bytes = (size_t)sizeof_client_key_der_2048;
  17880. if (bytes < (size_t)sizeof_client_cert_der_2048)
  17881. bytes = (size_t)sizeof_client_cert_der_2048;
  17882. #else
  17883. bytes = FOURK_BUF;
  17884. #endif
  17885. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17886. if (tmp == NULL
  17887. #ifdef WOLFSSL_ASYNC_CRYPT
  17888. || out == NULL || plain == NULL
  17889. #endif
  17890. ) {
  17891. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_even_mod);
  17892. }
  17893. #if defined(USE_CERT_BUFFERS_2048)
  17894. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  17895. #elif defined(USE_CERT_BUFFERS_3072)
  17896. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  17897. #elif defined(USE_CERT_BUFFERS_4096)
  17898. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  17899. #elif !defined(NO_FILESYSTEM)
  17900. file = XFOPEN(clientKey, "rb");
  17901. if (!file) {
  17902. err_sys("can't open ./certs/client-key.der, "
  17903. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  17904. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_even_mod);
  17905. }
  17906. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  17907. XFCLOSE(file);
  17908. if (bytes == 0)
  17909. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_even_mod);
  17910. #else
  17911. /* No key to use. */
  17912. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_even_mod);
  17913. #endif /* USE_CERT_BUFFERS */
  17914. #ifndef NO_ASN
  17915. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  17916. if (ret != 0) {
  17917. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17918. }
  17919. #else
  17920. #ifdef USE_CERT_BUFFERS_2048
  17921. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  17922. if (ret != 0) {
  17923. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17924. }
  17925. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  17926. if (ret != 0) {
  17927. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17928. }
  17929. #ifndef NO_SIG_WRAPPER
  17930. modLen = 2048;
  17931. #endif
  17932. #else
  17933. #error Not supported yet!
  17934. #endif
  17935. #endif
  17936. key->n.dp[0] &= (mp_digit)-2;
  17937. if (ret != 0) {
  17938. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17939. }
  17940. /* after loading in key use tmp as the test buffer */
  17941. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  17942. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM))) && \
  17943. !defined(WOLFSSL_XILINX_CRYPT)
  17944. /* The ARM64_ASM code that was FIPS validated did not return these expected
  17945. * failure codes. These tests cases were added after the assembly was
  17946. * in-lined in the module and validated, these tests will be available in
  17947. * the 140-3 module */
  17948. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  17949. inLen = 32;
  17950. outSz = wc_RsaEncryptSize(key);
  17951. XMEMSET(tmp, 7, plainSz);
  17952. ret = wc_RsaSSL_Sign(tmp, inLen, out, outSz, key, rng);
  17953. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  17954. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17955. }
  17956. ret = wc_RsaSSL_Verify(out, outSz, tmp, inLen, key);
  17957. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  17958. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17959. }
  17960. #endif
  17961. #ifdef WC_RSA_BLINDING
  17962. ret = wc_RsaSetRNG(key, rng);
  17963. if (ret < 0) {
  17964. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17965. }
  17966. #endif
  17967. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  17968. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  17969. ret = wc_RsaPublicEncrypt(tmp, inLen, out, (int)outSz, key, rng);
  17970. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  17971. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17972. }
  17973. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  17974. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17975. ret = wc_RsaPrivateDecrypt(out, outSz, plain, (int)plainSz, key);
  17976. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  17977. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17978. }
  17979. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  17980. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  17981. /* if making it to this point of code without hitting an ERROR_OUT then
  17982. * all tests have passed */
  17983. ret = 0;
  17984. exit_rsa_even_mod:
  17985. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17986. WC_FREE_VAR(out, HEAP_HINT);
  17987. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17988. WC_FREE_VAR(plain, HEAP_HINT);
  17989. #endif
  17990. (void)out;
  17991. (void)outSz;
  17992. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17993. (void)plain;
  17994. (void)plainSz;
  17995. #endif
  17996. (void)inLen;
  17997. (void)rng;
  17998. return ret;
  17999. }
  18000. #endif /* WOLFSSL_HAVE_SP_RSA */
  18001. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  18002. static wc_test_ret_t rsa_certgen_test(RsaKey* key, RsaKey* keypub, WC_RNG* rng, byte* tmp)
  18003. {
  18004. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18005. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18006. #ifdef WOLFSSL_TEST_CERT
  18007. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18008. #endif
  18009. #else
  18010. RsaKey caKey[1];
  18011. #ifdef WOLFSSL_TEST_CERT
  18012. DecodedCert decode[1];
  18013. #endif
  18014. #endif
  18015. byte* der = NULL;
  18016. wc_test_ret_t ret;
  18017. Cert* myCert = NULL;
  18018. int certSz;
  18019. size_t bytes3;
  18020. word32 idx3 = 0;
  18021. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  18022. XFILE file3;
  18023. #endif
  18024. #if defined(WOLFSSL_ALT_NAMES) && !defined(NO_ASN_TIME)
  18025. struct tm beforeTime;
  18026. struct tm afterTime;
  18027. #endif
  18028. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  18029. (void)keypub;
  18030. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18031. if (caKey == NULL)
  18032. ERROR_OUT(MEMORY_E, exit_rsa);
  18033. #ifdef WOLFSSL_TEST_CERT
  18034. if (decode == NULL)
  18035. ERROR_OUT(MEMORY_E, exit_rsa);
  18036. #endif
  18037. #endif
  18038. XMEMSET(caKey, 0, sizeof *caKey);
  18039. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18040. if (der == NULL) {
  18041. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18042. }
  18043. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18044. if (myCert == NULL) {
  18045. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18046. }
  18047. /* self signed */
  18048. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  18049. if (ret != 0) {
  18050. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18051. }
  18052. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  18053. XMEMCPY(myCert->serial, mySerial, sizeof(mySerial));
  18054. myCert->serialSz = (int)sizeof(mySerial);
  18055. myCert->isCA = 1;
  18056. #ifndef NO_SHA256
  18057. myCert->sigType = CTC_SHA256wRSA;
  18058. #else
  18059. myCert->sigType = CTC_SHAwRSA;
  18060. #endif
  18061. #ifdef WOLFSSL_CERT_EXT
  18062. /* add Policies */
  18063. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  18064. CTC_MAX_CERTPOL_SZ);
  18065. XSTRNCPY(myCert->certPolicies[1], "1.2.840.113549.1.9.16.6.5",
  18066. CTC_MAX_CERTPOL_SZ);
  18067. myCert->certPoliciesNb = 2;
  18068. /* add SKID from the Public Key */
  18069. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, keypub, NULL);
  18070. if (ret != 0) {
  18071. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18072. }
  18073. /* add AKID from the Public Key */
  18074. ret = wc_SetAuthKeyIdFromPublicKey(myCert, keypub, NULL);
  18075. if (ret != 0) {
  18076. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18077. }
  18078. /* add Key Usage */
  18079. ret = wc_SetKeyUsage(myCert,"cRLSign,keyCertSign");
  18080. if (ret != 0) {
  18081. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18082. }
  18083. #ifdef WOLFSSL_EKU_OID
  18084. {
  18085. const char unique[] = "2.16.840.1.111111.100.1.10.1";
  18086. ret = wc_SetExtKeyUsageOID(myCert, unique, sizeof(unique), 0,
  18087. HEAP_HINT);
  18088. if (ret != 0) {
  18089. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18090. }
  18091. }
  18092. #endif /* WOLFSSL_EKU_OID */
  18093. #endif /* WOLFSSL_CERT_EXT */
  18094. do {
  18095. #if defined(WOLFSSL_ASYNC_CRYPT)
  18096. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18097. #endif
  18098. if (ret >= 0) {
  18099. ret = wc_MakeSelfCert(myCert, der, FOURK_BUF, key, rng);
  18100. }
  18101. } while (ret == WC_PENDING_E);
  18102. if (ret < 0) {
  18103. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18104. }
  18105. certSz = (int)ret;
  18106. #ifdef WOLFSSL_TEST_CERT
  18107. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  18108. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  18109. if (ret != 0) {
  18110. FreeDecodedCert(decode);
  18111. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18112. }
  18113. FreeDecodedCert(decode);
  18114. #endif
  18115. ret = SaveDerAndPem(der, certSz, certDerFile, certPemFile,
  18116. CERT_TYPE);
  18117. if (ret != 0) {
  18118. goto exit_rsa;
  18119. }
  18120. /* Setup Certificate */
  18121. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  18122. if (ret < 0) {
  18123. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18124. }
  18125. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  18126. /* Get CA Cert for testing */
  18127. #ifdef USE_CERT_BUFFERS_1024
  18128. XMEMCPY(tmp, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  18129. bytes3 = sizeof_ca_cert_der_1024;
  18130. #elif defined(USE_CERT_BUFFERS_2048)
  18131. XMEMCPY(tmp, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  18132. bytes3 = sizeof_ca_cert_der_2048;
  18133. #else
  18134. file3 = XFOPEN(rsaCaCertDerFile, "rb");
  18135. if (!file3) {
  18136. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18137. }
  18138. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  18139. XFCLOSE(file3);
  18140. if (bytes3 == 0)
  18141. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18142. #endif /* USE_CERT_BUFFERS */
  18143. #if defined(WOLFSSL_ALT_NAMES)
  18144. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_1024) && \
  18145. !defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  18146. ret = wc_SetAltNames(myCert, rsaCaCertFile);
  18147. if (ret != 0)
  18148. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18149. #endif
  18150. /* get alt names from der */
  18151. ret = wc_SetAltNamesBuffer(myCert, tmp, (int)bytes3);
  18152. if (ret != 0)
  18153. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18154. /* get dates from der */
  18155. ret = wc_SetDatesBuffer(myCert, tmp, (int)bytes3);
  18156. if (ret != 0)
  18157. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18158. #ifndef NO_ASN_TIME
  18159. ret = wc_GetCertDates(myCert, &beforeTime, &afterTime);
  18160. if (ret < 0)
  18161. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18162. #endif
  18163. #endif /* WOLFSSL_ALT_NAMES */
  18164. #endif /* WOLFSSL_ALT_NAMES || HAVE_PKCS7 */
  18165. /* Get CA Key */
  18166. #ifdef USE_CERT_BUFFERS_1024
  18167. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  18168. bytes3 = sizeof_ca_key_der_1024;
  18169. #elif defined(USE_CERT_BUFFERS_2048)
  18170. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  18171. bytes3 = sizeof_ca_key_der_2048;
  18172. #else
  18173. file3 = XFOPEN(rsaCaKeyFile, "rb");
  18174. if (!file3) {
  18175. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18176. }
  18177. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  18178. XFCLOSE(file3);
  18179. if (bytes3 == 0)
  18180. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18181. #endif /* USE_CERT_BUFFERS */
  18182. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  18183. if (ret != 0)
  18184. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18185. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  18186. if (ret != 0)
  18187. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18188. #ifndef NO_SHA256
  18189. myCert->sigType = CTC_SHA256wRSA;
  18190. #else
  18191. myCert->sigType = CTC_SHAwRSA;
  18192. #endif
  18193. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  18194. #ifdef WOLFSSL_CERT_EXT
  18195. /* add Policies */
  18196. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  18197. CTC_MAX_CERTPOL_SZ);
  18198. myCert->certPoliciesNb =1;
  18199. /* add SKID from the Public Key */
  18200. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, key, NULL);
  18201. if (ret != 0)
  18202. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18203. /* add AKID from the CA certificate */
  18204. #if defined(USE_CERT_BUFFERS_2048)
  18205. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  18206. sizeof_ca_cert_der_2048);
  18207. #elif defined(USE_CERT_BUFFERS_1024)
  18208. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  18209. sizeof_ca_cert_der_1024);
  18210. #else
  18211. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  18212. #endif
  18213. if (ret != 0)
  18214. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18215. /* add Key Usage */
  18216. ret = wc_SetKeyUsage(myCert,"keyEncipherment,keyAgreement");
  18217. if (ret != 0)
  18218. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18219. #endif /* WOLFSSL_CERT_EXT */
  18220. #if defined(USE_CERT_BUFFERS_2048)
  18221. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  18222. sizeof_ca_cert_der_2048);
  18223. #elif defined(USE_CERT_BUFFERS_1024)
  18224. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  18225. sizeof_ca_cert_der_1024);
  18226. #else
  18227. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  18228. #endif
  18229. if (ret < 0)
  18230. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18231. certSz = wc_MakeCert(myCert, der, FOURK_BUF, key, NULL, rng);
  18232. if (certSz < 0) {
  18233. ERROR_OUT(WC_TEST_RET_ENC_EC(certSz), exit_rsa);
  18234. }
  18235. ret = 0;
  18236. do {
  18237. #if defined(WOLFSSL_ASYNC_CRYPT)
  18238. ret = wc_AsyncWait(ret, &caKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18239. #endif
  18240. if (ret >= 0) {
  18241. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der, FOURK_BUF,
  18242. caKey, NULL, rng);
  18243. }
  18244. } while (ret == WC_PENDING_E);
  18245. if (ret < 0)
  18246. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18247. certSz = (int)ret;
  18248. #ifdef WOLFSSL_TEST_CERT
  18249. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  18250. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  18251. if (ret != 0) {
  18252. FreeDecodedCert(decode);
  18253. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18254. }
  18255. FreeDecodedCert(decode);
  18256. #endif
  18257. ret = SaveDerAndPem(der, certSz, otherCertDerFile, otherCertPemFile,
  18258. CERT_TYPE);
  18259. if (ret != 0) {
  18260. goto exit_rsa;
  18261. }
  18262. exit_rsa:
  18263. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18264. if (caKey != NULL) {
  18265. wc_FreeRsaKey(caKey);
  18266. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18267. }
  18268. #ifdef WOLFSSL_TEST_CERT
  18269. if (decode != NULL)
  18270. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18271. #endif
  18272. #else
  18273. wc_FreeRsaKey(caKey);
  18274. #endif
  18275. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18276. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18277. return ret;
  18278. }
  18279. #endif
  18280. #if !defined(NO_RSA) && defined(HAVE_ECC) && !defined(NO_ECC_SECP) && \
  18281. defined(WOLFSSL_CERT_GEN)
  18282. /* Make Cert / Sign example for ECC cert and RSA CA */
  18283. static wc_test_ret_t rsa_ecc_certgen_test(WC_RNG* rng, byte* tmp)
  18284. {
  18285. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18286. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18287. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18288. ecc_key *caEccKeyPub = (ecc_key *)XMALLOC(sizeof *caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18289. #ifdef WOLFSSL_TEST_CERT
  18290. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18291. #endif
  18292. #else
  18293. RsaKey caKey[1];
  18294. ecc_key caEccKey[1];
  18295. ecc_key caEccKeyPub[1];
  18296. #ifdef WOLFSSL_TEST_CERT
  18297. DecodedCert decode[1];
  18298. #endif
  18299. #endif
  18300. byte* der = NULL;
  18301. Cert* myCert = NULL;
  18302. int certSz;
  18303. size_t bytes3;
  18304. word32 idx3 = 0;
  18305. #if (!defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)) \
  18306. || !defined(USE_CERT_BUFFERS_256)
  18307. #ifndef NO_FILESYSTEM
  18308. XFILE file3;
  18309. #endif
  18310. #endif
  18311. wc_test_ret_t ret;
  18312. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18313. if ((caKey == NULL) || (caEccKey == NULL) || (caEccKeyPub == NULL)
  18314. #ifdef WOLFSSL_TEST_CERT
  18315. || (decode == NULL)
  18316. #endif
  18317. )
  18318. ERROR_OUT(MEMORY_E, exit_rsa);
  18319. #endif
  18320. XMEMSET(caKey, 0, sizeof *caKey);
  18321. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  18322. XMEMSET(caEccKeyPub, 0, sizeof *caEccKeyPub);
  18323. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18324. if (der == NULL) {
  18325. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18326. }
  18327. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18328. if (myCert == NULL) {
  18329. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18330. }
  18331. /* Get CA Key */
  18332. #ifdef USE_CERT_BUFFERS_1024
  18333. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  18334. bytes3 = sizeof_ca_key_der_1024;
  18335. #elif defined(USE_CERT_BUFFERS_2048)
  18336. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  18337. bytes3 = sizeof_ca_key_der_2048;
  18338. #else
  18339. file3 = XFOPEN(rsaCaKeyFile, "rb");
  18340. if (!file3) {
  18341. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18342. }
  18343. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  18344. XFCLOSE(file3);
  18345. if (bytes3 == 0)
  18346. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18347. #endif /* USE_CERT_BUFFERS */
  18348. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  18349. if (ret != 0)
  18350. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18351. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  18352. if (ret != 0)
  18353. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18354. /* Get Cert Key */
  18355. #if defined(USE_CERT_BUFFERS_256)
  18356. {
  18357. XMEMCPY(tmp, ecc_key_pub_der_256, sizeof_ecc_key_pub_der_256);
  18358. bytes3 = sizeof_ecc_key_pub_der_256;
  18359. }
  18360. #elif !defined(NO_FILESYSTEM)
  18361. {
  18362. file3 = XFOPEN(eccKeyPubFileDer, "rb");
  18363. if (!file3) {
  18364. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18365. }
  18366. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  18367. XFCLOSE(file3);
  18368. if (bytes3 == 0)
  18369. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18370. }
  18371. #else
  18372. {
  18373. WOLFSSL_MSG("No file system and USE_CERT_BUFFERS_256 not defined.(1)");
  18374. ERROR_OUT(ASN_PARSE_E, exit_rsa);
  18375. }
  18376. #endif
  18377. ret = wc_ecc_init_ex(caEccKeyPub, HEAP_HINT, devId);
  18378. if (ret != 0)
  18379. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18380. idx3 = 0;
  18381. ret = wc_EccPublicKeyDecode(tmp, &idx3, caEccKeyPub, (word32)bytes3);
  18382. if (ret != 0)
  18383. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18384. /* Setup Certificate */
  18385. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  18386. if (ret != 0)
  18387. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18388. #ifndef NO_SHA256
  18389. myCert->sigType = CTC_SHA256wRSA;
  18390. #else
  18391. myCert->sigType = CTC_SHAwRSA;
  18392. #endif
  18393. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  18394. #ifdef WOLFSSL_CERT_EXT
  18395. /* add Policies */
  18396. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  18397. CTC_MAX_CERTPOL_SZ);
  18398. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  18399. CTC_MAX_CERTPOL_SZ);
  18400. myCert->certPoliciesNb = 2;
  18401. /* add SKID from the Public Key */
  18402. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, caEccKeyPub);
  18403. if (ret != 0)
  18404. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18405. /* add AKID from the CA certificate */
  18406. #if defined(USE_CERT_BUFFERS_2048)
  18407. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  18408. sizeof_ca_cert_der_2048);
  18409. #elif defined(USE_CERT_BUFFERS_1024)
  18410. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  18411. sizeof_ca_cert_der_1024);
  18412. #else
  18413. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  18414. #endif
  18415. if (ret != 0)
  18416. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18417. /* add Key Usage */
  18418. ret = wc_SetKeyUsage(myCert, certKeyUsage);
  18419. if (ret != 0)
  18420. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18421. #endif /* WOLFSSL_CERT_EXT */
  18422. #if defined(USE_CERT_BUFFERS_2048)
  18423. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  18424. sizeof_ca_cert_der_2048);
  18425. #elif defined(USE_CERT_BUFFERS_1024)
  18426. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  18427. sizeof_ca_cert_der_1024);
  18428. #else
  18429. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  18430. #endif
  18431. if (ret < 0)
  18432. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18433. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, caEccKeyPub, rng);
  18434. if (certSz < 0) {
  18435. ERROR_OUT(WC_TEST_RET_ENC_EC(certSz), exit_rsa);
  18436. }
  18437. ret = 0;
  18438. do {
  18439. #if defined(WOLFSSL_ASYNC_CRYPT)
  18440. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18441. #endif
  18442. if (ret >= 0) {
  18443. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  18444. FOURK_BUF, caKey, NULL, rng);
  18445. }
  18446. } while (ret == WC_PENDING_E);
  18447. if (ret < 0)
  18448. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18449. certSz = (int)ret;
  18450. #ifdef WOLFSSL_TEST_CERT
  18451. InitDecodedCert(decode, der, certSz, 0);
  18452. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  18453. if (ret != 0) {
  18454. FreeDecodedCert(decode);
  18455. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18456. }
  18457. FreeDecodedCert(decode);
  18458. #endif
  18459. ret = SaveDerAndPem(der, certSz, certEccRsaDerFile, certEccRsaPemFile,
  18460. CERT_TYPE);
  18461. if (ret != 0) {
  18462. goto exit_rsa;
  18463. }
  18464. exit_rsa:
  18465. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18466. if (caKey != NULL) {
  18467. wc_FreeRsaKey(caKey);
  18468. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18469. }
  18470. if (caEccKey != NULL) {
  18471. wc_ecc_free(caEccKey);
  18472. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18473. }
  18474. if (caEccKeyPub != NULL) {
  18475. wc_ecc_free(caEccKeyPub);
  18476. XFREE(caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18477. }
  18478. #ifdef WOLFSSL_TEST_CERT
  18479. if (decode != NULL)
  18480. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18481. #endif
  18482. #else
  18483. wc_FreeRsaKey(caKey);
  18484. wc_ecc_free(caEccKey);
  18485. wc_ecc_free(caEccKeyPub);
  18486. #endif
  18487. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18488. myCert = NULL;
  18489. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18490. der = NULL;
  18491. if (ret >= 0)
  18492. ret = 0;
  18493. return ret;
  18494. }
  18495. #endif /* !NO_RSA && HAVE_ECC && WOLFSSL_CERT_GEN */
  18496. #ifdef WOLFSSL_KEY_GEN
  18497. static wc_test_ret_t rsa_keygen_test(WC_RNG* rng)
  18498. {
  18499. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18500. RsaKey *genKey = (RsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18501. #else
  18502. RsaKey genKey[1];
  18503. #endif
  18504. wc_test_ret_t ret;
  18505. #ifndef WOLFSSL_NO_MALLOC
  18506. byte* der = NULL;
  18507. #else
  18508. byte der[1024];
  18509. #endif
  18510. #ifndef WOLFSSL_CRYPTOCELL
  18511. word32 idx = 0;
  18512. #endif
  18513. int derSz = 0;
  18514. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FIPS)
  18515. int keySz = 1024;
  18516. #else
  18517. int keySz = 2048;
  18518. #endif
  18519. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18520. if (! genKey)
  18521. ERROR_OUT(MEMORY_E, exit_rsa);
  18522. #endif
  18523. XMEMSET(genKey, 0, sizeof *genKey);
  18524. ret = wc_InitRsaKey_ex(genKey, HEAP_HINT, devId);
  18525. if (ret != 0)
  18526. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18527. #ifdef HAVE_FIPS
  18528. for (;;) {
  18529. #endif
  18530. ret = wc_MakeRsaKey(genKey, keySz, WC_RSA_EXPONENT, rng);
  18531. #if defined(WOLFSSL_ASYNC_CRYPT)
  18532. ret = wc_AsyncWait(ret, &genKey->asyncDev, WC_ASYNC_FLAG_NONE);
  18533. #endif
  18534. #ifdef HAVE_FIPS
  18535. if (ret == PRIME_GEN_E)
  18536. continue;
  18537. break;
  18538. }
  18539. #endif
  18540. if (ret != 0)
  18541. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18542. TEST_SLEEP();
  18543. #ifdef WOLFSSL_RSA_KEY_CHECK
  18544. ret = wc_CheckRsaKey(genKey);
  18545. if (ret != 0)
  18546. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18547. #endif
  18548. #ifndef WOLFSSL_NO_MALLOC
  18549. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18550. if (der == NULL) {
  18551. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18552. }
  18553. #endif
  18554. derSz = wc_RsaKeyToDer(genKey, der, FOURK_BUF);
  18555. if (derSz < 0) {
  18556. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  18557. }
  18558. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  18559. PRIVATEKEY_TYPE);
  18560. if (ret != 0) {
  18561. goto exit_rsa;
  18562. }
  18563. wc_FreeRsaKey(genKey);
  18564. ret = wc_InitRsaKey(genKey, HEAP_HINT);
  18565. if (ret != 0)
  18566. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18567. #ifndef WOLFSSL_CRYPTOCELL
  18568. idx = 0;
  18569. /* The private key part of the key gen pairs from cryptocell can't be exported */
  18570. ret = wc_RsaPrivateKeyDecode(der, &idx, genKey, (word32)derSz);
  18571. if (ret != 0)
  18572. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18573. #endif /* WOLFSSL_CRYPTOCELL */
  18574. exit_rsa:
  18575. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18576. if (genKey) {
  18577. wc_FreeRsaKey(genKey);
  18578. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18579. }
  18580. #else
  18581. wc_FreeRsaKey(genKey);
  18582. #endif
  18583. #ifndef WOLFSSL_NO_MALLOC
  18584. if (der != NULL) {
  18585. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18586. der = NULL;
  18587. }
  18588. #endif
  18589. return ret;
  18590. }
  18591. #endif
  18592. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  18593. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG) && \
  18594. (!defined(HAVE_FIPS) || \
  18595. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  18596. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  18597. static wc_test_ret_t rsa_oaep_padding_test(RsaKey* key, WC_RNG* rng)
  18598. {
  18599. wc_test_ret_t ret = 0;
  18600. word32 idx = 0;
  18601. const char inStr[] = TEST_STRING;
  18602. const word32 inLen = (word32)TEST_STRING_SZ;
  18603. const word32 outSz = RSA_TEST_BYTES;
  18604. const word32 plainSz = RSA_TEST_BYTES;
  18605. byte* res = NULL;
  18606. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  18607. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  18608. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  18609. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  18610. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  18611. WC_ALLOC_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  18612. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  18613. if (in == NULL || out == NULL || plain == NULL)
  18614. ERROR_OUT(MEMORY_E, exit_rsa);
  18615. #endif
  18616. XMEMCPY(in, inStr, inLen);
  18617. #ifndef NO_SHA
  18618. do {
  18619. #if defined(WOLFSSL_ASYNC_CRYPT)
  18620. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18621. #endif
  18622. if (ret >= 0) {
  18623. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18624. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  18625. }
  18626. } while (ret == WC_PENDING_E);
  18627. if (ret < 0)
  18628. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18629. TEST_SLEEP();
  18630. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18631. idx = (word32)ret;
  18632. do {
  18633. #if defined(WOLFSSL_ASYNC_CRYPT)
  18634. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18635. #endif
  18636. if (ret >= 0) {
  18637. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18638. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  18639. }
  18640. } while (ret == WC_PENDING_E);
  18641. if (ret < 0)
  18642. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18643. if (XMEMCMP(plain, in, inLen)) {
  18644. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18645. }
  18646. TEST_SLEEP();
  18647. #endif /* NO_SHA */
  18648. #endif
  18649. #ifndef NO_SHA256
  18650. XMEMSET(plain, 0, plainSz);
  18651. do {
  18652. #if defined(WOLFSSL_ASYNC_CRYPT)
  18653. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18654. #endif
  18655. if (ret >= 0) {
  18656. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18657. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  18658. }
  18659. } while (ret == WC_PENDING_E);
  18660. if (ret < 0)
  18661. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18662. TEST_SLEEP();
  18663. idx = (word32)ret;
  18664. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18665. do {
  18666. #if defined(WOLFSSL_ASYNC_CRYPT)
  18667. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18668. #endif
  18669. if (ret >= 0) {
  18670. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18671. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  18672. }
  18673. } while (ret == WC_PENDING_E);
  18674. if (ret < 0)
  18675. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18676. if (XMEMCMP(plain, in, inLen)) {
  18677. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18678. }
  18679. TEST_SLEEP();
  18680. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  18681. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18682. do {
  18683. #if defined(WOLFSSL_ASYNC_CRYPT)
  18684. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18685. #endif
  18686. if (ret >= 0) {
  18687. ret = wc_RsaPrivateDecryptInline_ex(out, idx, &res, key,
  18688. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  18689. }
  18690. } while (ret == WC_PENDING_E);
  18691. if (ret < 0)
  18692. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18693. if (ret != (int)inLen) {
  18694. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18695. }
  18696. if (XMEMCMP(res, in, inLen)) {
  18697. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18698. }
  18699. TEST_SLEEP();
  18700. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  18701. /* check fails if not using the same optional label */
  18702. XMEMSET(plain, 0, plainSz);
  18703. do {
  18704. #if defined(WOLFSSL_ASYNC_CRYPT)
  18705. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18706. #endif
  18707. if (ret >= 0) {
  18708. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18709. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  18710. }
  18711. } while (ret == WC_PENDING_E);
  18712. if (ret < 0)
  18713. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18714. TEST_SLEEP();
  18715. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  18716. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  18717. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  18718. /* label is unused in cryptocell and SE050 so it won't detect decrypt error
  18719. * due to label */
  18720. idx = (word32)ret;
  18721. do {
  18722. #if defined(WOLFSSL_ASYNC_CRYPT)
  18723. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18724. #endif
  18725. if (ret >= 0) {
  18726. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18727. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  18728. }
  18729. } while (ret == WC_PENDING_E);
  18730. if (ret > 0) { /* in this case decrypt should fail */
  18731. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18732. }
  18733. ret = 0;
  18734. TEST_SLEEP();
  18735. #endif /* !HAVE_CAVIUM */
  18736. /* check using optional label with encrypt/decrypt */
  18737. XMEMSET(plain, 0, plainSz);
  18738. do {
  18739. #if defined(WOLFSSL_ASYNC_CRYPT)
  18740. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18741. #endif
  18742. if (ret >= 0) {
  18743. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18744. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  18745. }
  18746. } while (ret == WC_PENDING_E);
  18747. if (ret < 0)
  18748. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18749. TEST_SLEEP();
  18750. idx = (word32)ret;
  18751. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18752. do {
  18753. #if defined(WOLFSSL_ASYNC_CRYPT)
  18754. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18755. #endif
  18756. if (ret >= 0) {
  18757. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18758. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  18759. }
  18760. } while (ret == WC_PENDING_E);
  18761. if (ret < 0)
  18762. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18763. if (XMEMCMP(plain, in, inLen)) {
  18764. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18765. }
  18766. TEST_SLEEP();
  18767. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  18768. #ifndef NO_SHA
  18769. /* check fail using mismatch hash algorithms */
  18770. XMEMSET(plain, 0, plainSz);
  18771. do {
  18772. #if defined(WOLFSSL_ASYNC_CRYPT)
  18773. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18774. #endif
  18775. if (ret >= 0) {
  18776. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18777. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, in, inLen);
  18778. }
  18779. } while (ret == WC_PENDING_E);
  18780. if (ret < 0)
  18781. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18782. TEST_SLEEP();
  18783. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  18784. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  18785. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  18786. idx = (word32)ret;
  18787. do {
  18788. #if defined(WOLFSSL_ASYNC_CRYPT)
  18789. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18790. #endif
  18791. if (ret >= 0) {
  18792. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18793. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256,
  18794. in, inLen);
  18795. }
  18796. } while (ret == WC_PENDING_E);
  18797. if (ret > 0) { /* should fail */
  18798. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18799. }
  18800. ret = 0;
  18801. TEST_SLEEP();
  18802. #endif /* !HAVE_CAVIUM */
  18803. #endif /* NO_SHA */
  18804. #endif /* NO_SHA256 */
  18805. #ifdef WOLFSSL_SHA512
  18806. /* Check valid RSA key size is used while using hash length of SHA512
  18807. If key size is less than (hash length * 2) + 2 then is invalid use
  18808. and test, since OAEP padding requires this.
  18809. BAD_FUNC_ARG is returned when this case is not met */
  18810. if (wc_RsaEncryptSize(key) > ((int)WC_SHA512_DIGEST_SIZE * 2) + 2) {
  18811. XMEMSET(plain, 0, plainSz);
  18812. do {
  18813. #if defined(WOLFSSL_ASYNC_CRYPT)
  18814. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18815. #endif
  18816. if (ret >= 0) {
  18817. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18818. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  18819. }
  18820. } while (ret == WC_PENDING_E);
  18821. if (ret < 0)
  18822. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18823. TEST_SLEEP();
  18824. idx = (word32)ret;
  18825. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18826. do {
  18827. #if defined(WOLFSSL_ASYNC_CRYPT)
  18828. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18829. #endif
  18830. if (ret >= 0) {
  18831. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18832. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  18833. }
  18834. } while (ret == WC_PENDING_E);
  18835. if (ret < 0)
  18836. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18837. if (XMEMCMP(plain, in, inLen)) {
  18838. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18839. }
  18840. TEST_SLEEP();
  18841. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  18842. }
  18843. #endif /* WOLFSSL_SHA512 */
  18844. /* check using pkcsv15 padding with _ex API */
  18845. XMEMSET(plain, 0, plainSz);
  18846. do {
  18847. #if defined(WOLFSSL_ASYNC_CRYPT)
  18848. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18849. #endif
  18850. if (ret >= 0) {
  18851. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18852. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  18853. }
  18854. } while (ret == WC_PENDING_E);
  18855. if (ret < 0)
  18856. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18857. TEST_SLEEP();
  18858. idx = (word32)ret;
  18859. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18860. do {
  18861. #if defined(WOLFSSL_ASYNC_CRYPT)
  18862. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18863. #endif
  18864. if (ret >= 0) {
  18865. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18866. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  18867. }
  18868. } while (ret == WC_PENDING_E);
  18869. if (ret < 0)
  18870. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18871. if (XMEMCMP(plain, in, inLen)) {
  18872. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18873. }
  18874. TEST_SLEEP();
  18875. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  18876. exit_rsa:
  18877. WC_FREE_VAR(in, HEAP_HINT);
  18878. WC_FREE_VAR(out, HEAP_HINT);
  18879. WC_FREE_VAR(plain, HEAP_HINT);
  18880. (void)idx;
  18881. (void)inStr;
  18882. (void)res;
  18883. if (ret >= 0)
  18884. ret = 0;
  18885. return ret;
  18886. }
  18887. #endif
  18888. #endif
  18889. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void)
  18890. {
  18891. wc_test_ret_t ret;
  18892. size_t bytes;
  18893. WC_RNG rng;
  18894. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18895. byte* tmp = NULL;
  18896. byte* der = NULL;
  18897. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18898. #else
  18899. RsaKey key[1];
  18900. byte tmp[FOURK_BUF];
  18901. #endif
  18902. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  18903. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18904. RsaKey *keypub = (RsaKey *)XMALLOC(sizeof *keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18905. #else
  18906. RsaKey keypub[1];
  18907. #endif
  18908. #endif
  18909. word32 idx = 0;
  18910. const char inStr[] = TEST_STRING;
  18911. const word32 inLen = (word32)TEST_STRING_SZ;
  18912. const word32 outSz = RSA_TEST_BYTES;
  18913. const word32 plainSz = RSA_TEST_BYTES;
  18914. byte* res = NULL;
  18915. #ifndef NO_SIG_WRAPPER
  18916. int modLen;
  18917. #endif
  18918. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  18919. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  18920. !defined(NO_FILESYSTEM)
  18921. XFILE file;
  18922. #ifdef WOLFSSL_TEST_CERT
  18923. XFILE file2;
  18924. #endif
  18925. #endif
  18926. #ifdef WOLFSSL_TEST_CERT
  18927. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18928. DecodedCert *cert = (DecodedCert *)XMALLOC(sizeof *cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18929. #else
  18930. DecodedCert cert[1];
  18931. #endif
  18932. #ifndef NO_ASN_TIME
  18933. struct tm timearg;
  18934. const byte* date;
  18935. byte dateFormat;
  18936. int dateLength;
  18937. #endif
  18938. #endif
  18939. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  18940. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  18941. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  18942. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  18943. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  18944. WC_ALLOC_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  18945. WOLFSSL_ENTER("rsa_test");
  18946. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  18947. if (in == NULL || out == NULL || plain == NULL)
  18948. ERROR_OUT(MEMORY_E, exit_rsa);
  18949. #endif
  18950. XMEMCPY(in, inStr, inLen);
  18951. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18952. if (key == NULL)
  18953. ERROR_OUT(MEMORY_E, exit_rsa);
  18954. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  18955. if (keypub == NULL)
  18956. ERROR_OUT(MEMORY_E, exit_rsa);
  18957. #endif
  18958. #ifdef WOLFSSL_TEST_CERT
  18959. if (cert == NULL)
  18960. ERROR_OUT(MEMORY_E, exit_rsa);
  18961. #endif
  18962. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  18963. /* initialize stack structures */
  18964. XMEMSET(&rng, 0, sizeof(rng));
  18965. XMEMSET(key, 0, sizeof *key);
  18966. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  18967. XMEMSET(keypub, 0, sizeof *keypub);
  18968. #endif
  18969. #if !defined(NO_ASN)
  18970. ret = rsa_decode_test(key);
  18971. if (ret != 0)
  18972. ERROR_OUT(ret, exit_rsa);
  18973. #endif
  18974. #ifdef USE_CERT_BUFFERS_1024
  18975. bytes = (size_t)sizeof_client_key_der_1024;
  18976. if (bytes < (size_t)sizeof_client_cert_der_1024)
  18977. bytes = (size_t)sizeof_client_cert_der_1024;
  18978. #elif defined(USE_CERT_BUFFERS_2048)
  18979. bytes = (size_t)sizeof_client_key_der_2048;
  18980. if (bytes < (size_t)sizeof_client_cert_der_2048)
  18981. bytes = (size_t)sizeof_client_cert_der_2048;
  18982. #elif defined(USE_CERT_BUFFERS_3072)
  18983. bytes = (size_t)sizeof_client_key_der_3072;
  18984. if (bytes < (size_t)sizeof_client_cert_der_3072)
  18985. bytes = (size_t)sizeof_client_cert_der_3072;
  18986. #elif defined(USE_CERT_BUFFERS_4096)
  18987. bytes = (size_t)sizeof_client_key_der_4096;
  18988. if (bytes < (size_t)sizeof_client_cert_der_4096)
  18989. bytes = (size_t)sizeof_client_cert_der_4096;
  18990. #else
  18991. bytes = FOURK_BUF;
  18992. #endif
  18993. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18994. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18995. if (tmp == NULL)
  18996. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18997. #endif
  18998. #ifdef USE_CERT_BUFFERS_1024
  18999. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  19000. #elif defined(USE_CERT_BUFFERS_2048)
  19001. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  19002. #elif defined(USE_CERT_BUFFERS_3072)
  19003. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  19004. #elif defined(USE_CERT_BUFFERS_4096)
  19005. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  19006. #elif !defined(NO_FILESYSTEM)
  19007. file = XFOPEN(clientKey, "rb");
  19008. if (!file) {
  19009. err_sys("can't open ./certs/client-key.der, "
  19010. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  19011. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19012. }
  19013. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  19014. XFCLOSE(file);
  19015. if (bytes == 0)
  19016. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19017. #else
  19018. /* No key to use. */
  19019. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  19020. #endif /* USE_CERT_BUFFERS */
  19021. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  19022. if (ret != 0)
  19023. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19024. #ifndef NO_ASN
  19025. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  19026. if (ret != 0)
  19027. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19028. #ifndef NO_SIG_WRAPPER
  19029. modLen = wc_RsaEncryptSize(key);
  19030. #endif
  19031. #else
  19032. #ifdef USE_CERT_BUFFERS_2048
  19033. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  19034. if (ret != 0)
  19035. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19036. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  19037. if (ret != 0)
  19038. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19039. #ifndef NO_SIG_WRAPPER
  19040. modLen = 2048;
  19041. #endif
  19042. #else
  19043. #error Not supported yet!
  19044. #endif
  19045. #endif
  19046. #ifndef WC_NO_RNG
  19047. #ifndef HAVE_FIPS
  19048. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  19049. #else
  19050. ret = wc_InitRng(&rng);
  19051. #endif
  19052. if (ret != 0)
  19053. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19054. #endif
  19055. #ifndef NO_SIG_WRAPPER
  19056. ret = rsa_sig_test(key, sizeof *key, modLen, &rng);
  19057. if (ret != 0)
  19058. goto exit_rsa;
  19059. #endif
  19060. #ifdef WC_RSA_NONBLOCK
  19061. ret = rsa_nb_test(key, in, inLen, out, outSz, plain, plainSz, &rng);
  19062. if (ret != 0)
  19063. goto exit_rsa;
  19064. #endif
  19065. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  19066. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  19067. do {
  19068. #if defined(WOLFSSL_ASYNC_CRYPT)
  19069. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19070. #endif
  19071. if (ret >= 0) {
  19072. ret = wc_RsaPublicEncrypt(in, inLen, out, outSz, key, &rng);
  19073. }
  19074. } while (ret == WC_PENDING_E);
  19075. if (ret < 0)
  19076. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19077. TEST_SLEEP();
  19078. #ifdef WC_RSA_BLINDING
  19079. {
  19080. wc_test_ret_t tmpret = ret;
  19081. ret = wc_RsaSetRNG(key, &rng);
  19082. if (ret < 0)
  19083. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19084. ret = tmpret;
  19085. }
  19086. #endif
  19087. idx = (word32)ret; /* save off encrypted length */
  19088. do {
  19089. #if defined(WOLFSSL_ASYNC_CRYPT)
  19090. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19091. #endif
  19092. if (ret >= 0) {
  19093. ret = wc_RsaPrivateDecrypt(out, idx, plain, plainSz, key);
  19094. }
  19095. } while (ret == WC_PENDING_E);
  19096. if (ret < 0)
  19097. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19098. if (XMEMCMP(plain, in, inLen)) {
  19099. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  19100. }
  19101. TEST_SLEEP();
  19102. do {
  19103. #if defined(WOLFSSL_ASYNC_CRYPT)
  19104. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19105. #endif
  19106. if (ret >= 0) {
  19107. ret = wc_RsaPrivateDecryptInline(out, idx, &res, key);
  19108. }
  19109. } while (ret == WC_PENDING_E);
  19110. if (ret < 0)
  19111. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19112. if (ret != (int)inLen) {
  19113. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  19114. }
  19115. if (XMEMCMP(res, in, inLen)) {
  19116. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  19117. }
  19118. TEST_SLEEP();
  19119. do {
  19120. #if defined(WOLFSSL_ASYNC_CRYPT)
  19121. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19122. #endif
  19123. if (ret >= 0) {
  19124. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, &rng);
  19125. }
  19126. } while (ret == WC_PENDING_E);
  19127. if (ret < 0)
  19128. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19129. TEST_SLEEP();
  19130. #elif defined(WOLFSSL_PUBLIC_MP)
  19131. {
  19132. static const byte signature_2048[] = {
  19133. 0x07, 0x6f, 0xc9, 0x85, 0x73, 0x9e, 0x21, 0x79,
  19134. 0x47, 0xf1, 0xa3, 0xd7, 0xf4, 0x27, 0x29, 0xbe,
  19135. 0x99, 0x5d, 0xac, 0xb2, 0x10, 0x3f, 0x95, 0xda,
  19136. 0x89, 0x23, 0xb8, 0x96, 0x13, 0x57, 0x72, 0x30,
  19137. 0xa1, 0xfe, 0x5a, 0x68, 0x9c, 0x99, 0x9d, 0x1e,
  19138. 0x05, 0xa4, 0x80, 0xb0, 0xbb, 0xd9, 0xd9, 0xa1,
  19139. 0x69, 0x97, 0x74, 0xb3, 0x41, 0x21, 0x3b, 0x47,
  19140. 0xf5, 0x51, 0xb1, 0xfb, 0xc7, 0xaa, 0xcc, 0xdc,
  19141. 0xcd, 0x76, 0xa0, 0x28, 0x4d, 0x27, 0x14, 0xa4,
  19142. 0xb9, 0x41, 0x68, 0x7c, 0xb3, 0x66, 0xe6, 0x6f,
  19143. 0x40, 0x76, 0xe4, 0x12, 0xfd, 0xae, 0x29, 0xb5,
  19144. 0x63, 0x60, 0x87, 0xce, 0x49, 0x6b, 0xf3, 0x05,
  19145. 0x9a, 0x14, 0xb5, 0xcc, 0xcd, 0xf7, 0x30, 0x95,
  19146. 0xd2, 0x72, 0x52, 0x1d, 0x5b, 0x7e, 0xef, 0x4a,
  19147. 0x02, 0x96, 0x21, 0x6c, 0x55, 0xa5, 0x15, 0xb1,
  19148. 0x57, 0x63, 0x2c, 0xa3, 0x8e, 0x9d, 0x3d, 0x45,
  19149. 0xcc, 0xb8, 0xe6, 0xa1, 0xc8, 0x59, 0xcd, 0xf5,
  19150. 0xdc, 0x0a, 0x51, 0xb6, 0x9d, 0xfb, 0xf4, 0x6b,
  19151. 0xfd, 0x32, 0x71, 0x6e, 0xcf, 0xcb, 0xb3, 0xd9,
  19152. 0xe0, 0x4a, 0x77, 0x34, 0xd6, 0x61, 0xf5, 0x7c,
  19153. 0xf9, 0xa9, 0xa4, 0xb0, 0x8e, 0x3b, 0xd6, 0x04,
  19154. 0xe0, 0xde, 0x2b, 0x5b, 0x5a, 0xbf, 0xd9, 0xef,
  19155. 0x8d, 0xa3, 0xf5, 0xb1, 0x67, 0xf3, 0xb9, 0x72,
  19156. 0x0a, 0x37, 0x12, 0x35, 0x6c, 0x8e, 0x10, 0x8b,
  19157. 0x38, 0x06, 0x16, 0x4b, 0x20, 0x20, 0x13, 0x00,
  19158. 0x2e, 0x6d, 0xc2, 0x59, 0x23, 0x67, 0x4a, 0x6d,
  19159. 0xa1, 0x46, 0x8b, 0xee, 0xcf, 0x44, 0xb4, 0x3e,
  19160. 0x56, 0x75, 0x00, 0x68, 0xb5, 0x7d, 0x0f, 0x20,
  19161. 0x79, 0x5d, 0x7f, 0x12, 0x15, 0x32, 0x89, 0x61,
  19162. 0x6b, 0x29, 0xb7, 0x52, 0xf5, 0x25, 0xd8, 0x98,
  19163. 0xe8, 0x6f, 0xf9, 0x22, 0xb4, 0xbb, 0xe5, 0xff,
  19164. 0xd0, 0x92, 0x86, 0x9a, 0x88, 0xa2, 0xaf, 0x6b
  19165. };
  19166. ret = sizeof(signature_2048);
  19167. XMEMCPY(out, signature_2048, ret);
  19168. }
  19169. #endif
  19170. #if !defined(WC_NO_RNG) && !defined(WC_NO_RSA_OAEP) && \
  19171. ((!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || \
  19172. defined(WOLFSSL_PUBLIC_MP)) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  19173. idx = (word32)ret;
  19174. XMEMSET(plain, 0, plainSz);
  19175. do {
  19176. #if defined(WOLFSSL_ASYNC_CRYPT)
  19177. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19178. #endif
  19179. if (ret >= 0) {
  19180. #ifndef WOLFSSL_RSA_VERIFY_INLINE
  19181. #if defined(WOLFSSL_CRYPTOCELL)
  19182. /*
  19183. Cryptocell requires the input data and signature byte array to verify.
  19184. first argument must be the input data
  19185. second argument must be the length of input data
  19186. third argument must be the signature byte array or the output from
  19187. wc_RsaSSL_Sign()
  19188. fourth argument must be the length of the signature byte array
  19189. */
  19190. ret = wc_RsaSSL_Verify(in, inLen, out, outSz, key);
  19191. #else
  19192. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, key);
  19193. #endif /* WOLFSSL_CRYPTOCELL */
  19194. #else
  19195. byte* dec = NULL;
  19196. ret = wc_RsaSSL_VerifyInline(out, idx, &dec, key);
  19197. if (ret > 0) {
  19198. XMEMCPY(plain, dec, ret);
  19199. }
  19200. #endif
  19201. }
  19202. } while (ret == WC_PENDING_E);
  19203. if (ret < 0)
  19204. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19205. if (XMEMCMP(plain, in, (size_t)ret)) {
  19206. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  19207. }
  19208. TEST_SLEEP();
  19209. #endif
  19210. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  19211. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  19212. #if (!defined(HAVE_FIPS) || \
  19213. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  19214. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  19215. ret = rsa_oaep_padding_test(key, &rng);
  19216. if (ret != 0)
  19217. return ret;
  19218. #endif /* !HAVE_FIPS */
  19219. #endif /* WC_NO_RSA_OAEP && !WC_NO_RNG */
  19220. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  19221. #if !defined(HAVE_FIPS) && !defined(NO_ASN) \
  19222. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  19223. ret = rsa_export_key_test(key);
  19224. if (ret != 0)
  19225. return ret;
  19226. #endif
  19227. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  19228. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  19229. ret = rsa_flatten_test(key);
  19230. if (ret != 0)
  19231. return ret;
  19232. #endif
  19233. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_ASN) && \
  19234. !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  19235. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  19236. (void)clientCert;
  19237. #endif
  19238. #ifdef WOLFSSL_TEST_CERT
  19239. #if defined(WOLFSSL_MDK_ARM)
  19240. #define sizeof(s) XSTRLEN((char *)(s))
  19241. #endif
  19242. #ifdef USE_CERT_BUFFERS_1024
  19243. XMEMCPY(tmp, client_cert_der_1024, (size_t)sizeof_client_cert_der_1024);
  19244. bytes = (size_t)sizeof_client_cert_der_1024;
  19245. #elif defined(USE_CERT_BUFFERS_2048)
  19246. XMEMCPY(tmp, client_cert_der_2048, (size_t)sizeof_client_cert_der_2048);
  19247. bytes = (size_t)sizeof_client_cert_der_2048;
  19248. #elif defined(USE_CERT_BUFFERS_3072)
  19249. XMEMCPY(tmp, client_cert_der_3072, (size_t)sizeof_client_cert_der_3072);
  19250. bytes = (size_t)sizeof_client_cert_der_3072;
  19251. #elif defined(USE_CERT_BUFFERS_4096)
  19252. XMEMCPY(tmp, client_cert_der_4096, (size_t)sizeof_client_cert_der_4096);
  19253. bytes = (size_t)sizeof_client_cert_der_4096;
  19254. #elif !defined(NO_FILESYSTEM)
  19255. file2 = XFOPEN(clientCert, "rb");
  19256. if (!file2) {
  19257. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19258. }
  19259. bytes = XFREAD(tmp, 1, FOURK_BUF, file2);
  19260. XFCLOSE(file2);
  19261. if (bytes == 0)
  19262. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19263. #else
  19264. /* No certificate to use. */
  19265. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  19266. #endif
  19267. #ifdef sizeof
  19268. #undef sizeof
  19269. #endif
  19270. InitDecodedCert(cert, tmp, (word32)bytes, NULL);
  19271. ret = ParseCert(cert, CERT_TYPE, NO_VERIFY, NULL);
  19272. if (ret != 0) {
  19273. FreeDecodedCert(cert);
  19274. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19275. }
  19276. #ifndef NO_ASN_TIME
  19277. ret = wc_GetDateInfo(cert->afterDate, cert->afterDateLen, &date,
  19278. &dateFormat, &dateLength);
  19279. if (ret != 0) {
  19280. FreeDecodedCert(cert);
  19281. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19282. }
  19283. ret = wc_GetDateAsCalendarTime(date, dateLength, dateFormat, &timearg);
  19284. if (ret != 0) {
  19285. FreeDecodedCert(cert);
  19286. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19287. }
  19288. #endif
  19289. FreeDecodedCert(cert);
  19290. #endif /* WOLFSSL_TEST_CERT */
  19291. #ifdef WOLFSSL_CERT_EXT
  19292. #ifdef USE_CERT_BUFFERS_1024
  19293. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  19294. bytes = sizeof_client_keypub_der_1024;
  19295. #elif defined(USE_CERT_BUFFERS_2048)
  19296. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  19297. bytes = sizeof_client_keypub_der_2048;
  19298. #elif defined(USE_CERT_BUFFERS_3072)
  19299. XMEMCPY(tmp, client_keypub_der_3072, sizeof_client_keypub_der_3072);
  19300. bytes = sizeof_client_keypub_der_3072;
  19301. #elif defined(USE_CERT_BUFFERS_4096)
  19302. XMEMCPY(tmp, client_keypub_der_4096, sizeof_client_keypub_der_4096);
  19303. bytes = sizeof_client_keypub_der_4096;
  19304. #else
  19305. file = XFOPEN(clientKeyPub, "rb");
  19306. if (!file) {
  19307. err_sys("can't open ./certs/client-keyPub.der, "
  19308. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  19309. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19310. }
  19311. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  19312. XFCLOSE(file);
  19313. if (bytes == 0)
  19314. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19315. #endif /* USE_CERT_BUFFERS */
  19316. ret = wc_InitRsaKey(keypub, HEAP_HINT);
  19317. if (ret != 0)
  19318. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19319. idx = 0;
  19320. ret = wc_RsaPublicKeyDecode(tmp, &idx, keypub, (word32)bytes);
  19321. if (ret != 0)
  19322. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19323. #endif /* WOLFSSL_CERT_EXT */
  19324. #ifdef WOLFSSL_KEY_GEN
  19325. ret = rsa_keygen_test(&rng);
  19326. if (ret != 0)
  19327. goto exit_rsa;
  19328. #endif
  19329. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  19330. /* Make Cert / Sign example for RSA cert and RSA CA */
  19331. ret = rsa_certgen_test(key, keypub, &rng, tmp);
  19332. if (ret != 0)
  19333. goto exit_rsa;
  19334. #if !defined(NO_RSA) && defined(HAVE_ECC) && !defined(NO_ECC_SECP)
  19335. ret = rsa_ecc_certgen_test(&rng, tmp);
  19336. if (ret != 0)
  19337. goto exit_rsa;
  19338. #endif
  19339. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  19340. {
  19341. Cert *req;
  19342. int derSz;
  19343. #ifndef WOLFSSL_SMALL_STACK
  19344. byte* der = NULL;
  19345. #endif
  19346. req = (Cert *)XMALLOC(sizeof *req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19347. if (! req)
  19348. ERROR_OUT(MEMORY_E, exit_rsa);
  19349. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  19350. if (der == NULL) {
  19351. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19352. }
  19353. ret = wc_InitCert_ex(req, HEAP_HINT, devId);
  19354. if (ret != 0)
  19355. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19356. req->version = 0;
  19357. req->isCA = 1;
  19358. XSTRNCPY(req->challengePw, "wolf123", CTC_NAME_SIZE);
  19359. XMEMCPY(&req->subject, &certDefaultName, sizeof(CertName));
  19360. #ifndef NO_SHA256
  19361. req->sigType = CTC_SHA256wRSA;
  19362. #else
  19363. req->sigType = CTC_SHAwRSA;
  19364. #endif
  19365. #ifdef WOLFSSL_CERT_EXT
  19366. /* add SKID from the Public Key */
  19367. ret = wc_SetSubjectKeyIdFromPublicKey(req, keypub, NULL);
  19368. if (ret != 0)
  19369. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19370. /* add Key Usage */
  19371. ret = wc_SetKeyUsage(req, certKeyUsage2);
  19372. if (ret != 0)
  19373. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19374. /* add Extended Key Usage */
  19375. ret = wc_SetExtKeyUsage(req,
  19376. "serverAuth,clientAuth,codeSigning,"
  19377. "emailProtection,timeStamping,OCSPSigning");
  19378. if (ret != 0)
  19379. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19380. #ifdef WOLFSSL_EKU_OID
  19381. {
  19382. WOLFSSL_SMALL_STACK_STATIC const char unique[] =
  19383. "2.16.840.1.111111.100.1.10.1";
  19384. ret = wc_SetExtKeyUsageOID(req, unique, sizeof(unique), 0,
  19385. HEAP_HINT);
  19386. if (ret != 0)
  19387. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19388. }
  19389. #endif /* WOLFSSL_EKU_OID */
  19390. #endif /* WOLFSSL_CERT_EXT */
  19391. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  19392. if (derSz < 0) {
  19393. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  19394. }
  19395. #ifdef WOLFSSL_CERT_EXT
  19396. /* Try again with "any" flag set, will override all others */
  19397. ret = wc_SetExtKeyUsage(req, "any");
  19398. if (ret != 0)
  19399. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19400. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  19401. if (derSz < 0) {
  19402. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  19403. }
  19404. #endif /* WOLFSSL_CERT_EXT */
  19405. ret = 0;
  19406. do {
  19407. #if defined(WOLFSSL_ASYNC_CRYPT)
  19408. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19409. #endif
  19410. if (ret >= 0) {
  19411. ret = wc_SignCert(req->bodySz, req->sigType, der, FOURK_BUF,
  19412. key, NULL, &rng);
  19413. }
  19414. } while (ret == WC_PENDING_E);
  19415. if (ret < 0)
  19416. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19417. derSz = (int)ret;
  19418. ret = SaveDerAndPem(der, derSz, certReqDerFile, certReqPemFile,
  19419. CERTREQ_TYPE);
  19420. if (ret != 0) {
  19421. goto exit_rsa;
  19422. }
  19423. derSz = wc_MakeCertReq_ex(req, der, FOURK_BUF, RSA_TYPE, key);
  19424. if (derSz < 0) {
  19425. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  19426. }
  19427. /* Test getting the size of the buffer without providing the buffer.
  19428. * derSz is set to the "largest buffer" we are willing to allocate. */
  19429. derSz = wc_MakeCertReq(req, NULL, 10000, key, NULL);
  19430. if (derSz < 0) {
  19431. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  19432. }
  19433. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19434. XFREE(req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19435. der = NULL;
  19436. }
  19437. #endif /* WOLFSSL_CERT_REQ */
  19438. #endif /* WOLFSSL_CERT_GEN */
  19439. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  19440. /* Need to create known good signatures to test with this. */
  19441. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  19442. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  19443. ret = rsa_pss_test(&rng, key);
  19444. if (ret != 0)
  19445. goto exit_rsa;
  19446. #endif
  19447. #endif
  19448. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  19449. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19450. /* New key to be loaded in rsa_even_mod_test(). */
  19451. if (key != NULL)
  19452. #endif
  19453. wc_FreeRsaKey(key);
  19454. /* New key to be loaded in rsa_even_mod_test(). */
  19455. ret = rsa_even_mod_test(&rng, key);
  19456. #endif
  19457. exit_rsa:
  19458. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19459. if (key != NULL) {
  19460. wc_FreeRsaKey(key);
  19461. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19462. }
  19463. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  19464. if (keypub != NULL) {
  19465. wc_FreeRsaKey(keypub);
  19466. XFREE(keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19467. }
  19468. #endif
  19469. #ifdef WOLFSSL_TEST_CERT
  19470. if (cert != NULL)
  19471. XFREE(cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19472. #endif
  19473. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19474. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19475. #else
  19476. wc_FreeRsaKey(key);
  19477. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  19478. wc_FreeRsaKey(keypub);
  19479. #endif
  19480. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  19481. wc_FreeRng(&rng);
  19482. WC_FREE_VAR(in, HEAP_HINT);
  19483. WC_FREE_VAR(out, HEAP_HINT);
  19484. WC_FREE_VAR(plain, HEAP_HINT);
  19485. (void)res;
  19486. (void)bytes;
  19487. (void)idx;
  19488. (void)in;
  19489. (void)out;
  19490. (void)plain;
  19491. (void)idx;
  19492. (void)inStr;
  19493. (void)inLen;
  19494. (void)outSz;
  19495. (void)plainSz;
  19496. /* ret can be greater then 0 with certgen but all negative values should
  19497. * be returned and treated as an error */
  19498. if (ret >= 0) {
  19499. return 0;
  19500. }
  19501. else {
  19502. return ret;
  19503. }
  19504. }
  19505. #endif /* !NO_RSA */
  19506. #ifndef NO_DH
  19507. static wc_test_ret_t dh_fips_generate_test(WC_RNG *rng)
  19508. {
  19509. wc_test_ret_t ret = 0;
  19510. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19511. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19512. #else
  19513. DhKey key[1];
  19514. #endif
  19515. WOLFSSL_SMALL_STACK_STATIC const byte p[] = {
  19516. 0xc5, 0x7c, 0xa2, 0x4f, 0x4b, 0xd6, 0x8c, 0x3c,
  19517. 0xda, 0xc7, 0xba, 0xaa, 0xea, 0x2e, 0x5c, 0x1e,
  19518. 0x18, 0xb2, 0x7b, 0x8c, 0x55, 0x65, 0x9f, 0xea,
  19519. 0xe0, 0xa1, 0x36, 0x53, 0x2b, 0x36, 0xe0, 0x4e,
  19520. 0x3e, 0x64, 0xa9, 0xe4, 0xfc, 0x8f, 0x32, 0x62,
  19521. 0x97, 0xe4, 0xbe, 0xf7, 0xc1, 0xde, 0x07, 0x5a,
  19522. 0x89, 0x28, 0xf3, 0xfe, 0x4f, 0xfe, 0x68, 0xbc,
  19523. 0xfb, 0x0a, 0x7c, 0xa4, 0xb3, 0x14, 0x48, 0x89,
  19524. 0x9f, 0xaf, 0xb8, 0x43, 0xe2, 0xa0, 0x62, 0x5c,
  19525. 0xb4, 0x88, 0x3f, 0x06, 0x50, 0x11, 0xfe, 0x65,
  19526. 0x8d, 0x49, 0xd2, 0xf5, 0x4b, 0x74, 0x79, 0xdb,
  19527. 0x06, 0x62, 0x92, 0x89, 0xed, 0xda, 0xcb, 0x87,
  19528. 0x37, 0x16, 0xd2, 0xa1, 0x7a, 0xe8, 0xde, 0x92,
  19529. 0xee, 0x3e, 0x41, 0x4a, 0x91, 0x5e, 0xed, 0xf3,
  19530. 0x6c, 0x6b, 0x7e, 0xfd, 0x15, 0x92, 0x18, 0xfc,
  19531. 0xa7, 0xac, 0x42, 0x85, 0x57, 0xe9, 0xdc, 0xda,
  19532. 0x55, 0xc9, 0x8b, 0x28, 0x9e, 0xc1, 0xc4, 0x46,
  19533. 0x4d, 0x88, 0xed, 0x62, 0x8e, 0xdb, 0x3f, 0xb9,
  19534. 0xd7, 0xc8, 0xe3, 0xcf, 0xb8, 0x34, 0x2c, 0xd2,
  19535. 0x6f, 0x28, 0x06, 0x41, 0xe3, 0x66, 0x8c, 0xfc,
  19536. 0x72, 0xff, 0x26, 0x3b, 0x6b, 0x6c, 0x6f, 0x73,
  19537. 0xde, 0xf2, 0x90, 0x29, 0xe0, 0x61, 0x32, 0xc4,
  19538. 0x12, 0x74, 0x09, 0x52, 0xec, 0xf3, 0x1b, 0xa6,
  19539. 0x45, 0x98, 0xac, 0xf9, 0x1c, 0x65, 0x8e, 0x3a,
  19540. 0x91, 0x84, 0x4b, 0x23, 0x8a, 0xb2, 0x3c, 0xc9,
  19541. 0xfa, 0xea, 0xf1, 0x38, 0xce, 0xd8, 0x05, 0xe0,
  19542. 0xfa, 0x44, 0x68, 0x1f, 0xeb, 0xd9, 0x57, 0xb8,
  19543. 0x4a, 0x97, 0x5b, 0x88, 0xc5, 0xf1, 0xbb, 0xb0,
  19544. 0x49, 0xc3, 0x91, 0x7c, 0xd3, 0x13, 0xb9, 0x47,
  19545. 0xbb, 0x91, 0x8f, 0xe5, 0x26, 0x07, 0xab, 0xa9,
  19546. 0xc5, 0xd0, 0x3d, 0x95, 0x41, 0x26, 0x92, 0x9d,
  19547. 0x13, 0x67, 0xf2, 0x7e, 0x11, 0x88, 0xdc, 0x2d
  19548. };
  19549. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  19550. 0x4a, 0x1a, 0xf3, 0xa4, 0x92, 0xe9, 0xee, 0x74,
  19551. 0x6e, 0x57, 0xd5, 0x8c, 0x2c, 0x5b, 0x41, 0x41,
  19552. 0x5e, 0xd4, 0x55, 0x19, 0xdc, 0xd9, 0x32, 0x91,
  19553. 0xf7, 0xfd, 0xc2, 0x57, 0xff, 0x03, 0x14, 0xdb,
  19554. 0xf1, 0xb7, 0x60, 0x0c, 0x43, 0x59, 0x3f, 0xff,
  19555. 0xac, 0xf1, 0x80, 0x9a, 0x15, 0x6f, 0xd8, 0x6e,
  19556. 0xb7, 0x85, 0x18, 0xc8, 0xec, 0x4e, 0x59, 0x4a,
  19557. 0xe2, 0x91, 0x43, 0x4c, 0xeb, 0x95, 0xb6, 0x2e,
  19558. 0x9a, 0xea, 0x53, 0x68, 0x80, 0x64, 0x69, 0x40,
  19559. 0xf9, 0xec, 0xbd, 0x85, 0x89, 0x26, 0x97, 0x67,
  19560. 0xaf, 0xb0, 0xad, 0x00, 0x1b, 0xd4, 0xfd, 0x94,
  19561. 0xd3, 0xe9, 0x92, 0xb1, 0xb4, 0xbc, 0x5a, 0xaa,
  19562. 0x92, 0x80, 0x89, 0x3b, 0x39, 0x05, 0x6c, 0x22,
  19563. 0x26, 0xfe, 0x5a, 0x28, 0x6c, 0x37, 0x50, 0x5a,
  19564. 0x38, 0x99, 0xcf, 0xf3, 0xc1, 0x96, 0x45, 0xdc,
  19565. 0x01, 0xcb, 0x20, 0x87, 0xa5, 0x00, 0x8c, 0xf5,
  19566. 0x4d, 0xc2, 0xef, 0xb8, 0x9b, 0xd1, 0x87, 0xbe,
  19567. 0xed, 0xd5, 0x0a, 0x29, 0x15, 0x34, 0x59, 0x4c,
  19568. 0x3a, 0x05, 0x22, 0x05, 0x44, 0x4f, 0x9f, 0xc8,
  19569. 0x47, 0x12, 0x24, 0x8e, 0xa8, 0x79, 0xe4, 0x67,
  19570. 0xba, 0x4d, 0x5b, 0x75, 0x56, 0x95, 0xeb, 0xe8,
  19571. 0x8a, 0xfa, 0x8e, 0x01, 0x8c, 0x1b, 0x74, 0x63,
  19572. 0xd9, 0x2f, 0xf7, 0xd3, 0x44, 0x8f, 0xa8, 0xf5,
  19573. 0xaf, 0x6c, 0x4f, 0xdb, 0xe7, 0xc9, 0x6c, 0x71,
  19574. 0x22, 0xa3, 0x1d, 0xf1, 0x40, 0xb2, 0xe0, 0x9a,
  19575. 0xb6, 0x72, 0xc9, 0xc0, 0x13, 0x16, 0xa2, 0x4a,
  19576. 0xe1, 0x92, 0xc7, 0x54, 0x23, 0xab, 0x9d, 0xa1,
  19577. 0xa1, 0xe5, 0x0b, 0xed, 0xba, 0xe8, 0x84, 0x37,
  19578. 0xb2, 0xe7, 0xfe, 0x32, 0x8d, 0xfa, 0x1c, 0x53,
  19579. 0x77, 0x97, 0xc7, 0xf3, 0x48, 0xc9, 0xdb, 0x2d,
  19580. 0x75, 0x52, 0x9d, 0x42, 0x51, 0x78, 0x62, 0x68,
  19581. 0x05, 0x45, 0x15, 0xf8, 0xa2, 0x4e, 0xf3, 0x0b
  19582. };
  19583. WOLFSSL_SMALL_STACK_STATIC const byte q[] = {
  19584. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  19585. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  19586. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  19587. 0x40, 0x52, 0xed, 0x41
  19588. };
  19589. WOLFSSL_SMALL_STACK_STATIC const byte q0[] = {
  19590. 0x00,
  19591. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  19592. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  19593. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  19594. 0x40, 0x52, 0xed, 0x41
  19595. };
  19596. byte priv[256];
  19597. byte pub[256];
  19598. word32 privSz = sizeof(priv);
  19599. word32 pubSz = sizeof(pub);
  19600. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19601. if (key == NULL)
  19602. ERROR_OUT(MEMORY_E, exit_gen_test);
  19603. #endif
  19604. /* Parameter Validation testing. */
  19605. ret = wc_DhGenerateKeyPair(NULL, rng, priv, &privSz, pub, &pubSz);
  19606. if (ret != BAD_FUNC_ARG)
  19607. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19608. ret = wc_DhGenerateKeyPair(key, NULL, priv, &privSz, pub, &pubSz);
  19609. if (ret != BAD_FUNC_ARG)
  19610. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19611. ret = wc_DhGenerateKeyPair(key, rng, NULL, &privSz, pub, &pubSz);
  19612. if (ret != BAD_FUNC_ARG)
  19613. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19614. ret = wc_DhGenerateKeyPair(key, rng, priv, NULL, pub, &pubSz);
  19615. if (ret != BAD_FUNC_ARG)
  19616. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19617. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, NULL, &pubSz);
  19618. if (ret != BAD_FUNC_ARG)
  19619. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19620. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, NULL);
  19621. if (ret != BAD_FUNC_ARG)
  19622. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19623. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  19624. if (ret != 0)
  19625. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19626. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q0, sizeof(q0));
  19627. if (ret != 0)
  19628. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19629. wc_FreeDhKey(key);
  19630. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  19631. if (ret != 0)
  19632. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19633. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q, sizeof(q));
  19634. if (ret != 0)
  19635. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19636. /* Use API. */
  19637. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  19638. #if defined(WOLFSSL_ASYNC_CRYPT)
  19639. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  19640. #endif
  19641. if (ret != 0)
  19642. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19643. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q0, sizeof(q0));
  19644. if (ret != 0)
  19645. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19646. wc_FreeDhKey(key);
  19647. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  19648. if (ret != 0)
  19649. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19650. ret = wc_DhSetKey(key, p, sizeof(p), g, sizeof(g));
  19651. if (ret != 0)
  19652. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19653. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q, sizeof(q));
  19654. if (ret != 0)
  19655. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19656. #ifndef HAVE_SELFTEST
  19657. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  19658. if (ret != 0)
  19659. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19660. /* Taint the public key so the check fails. */
  19661. pub[0]++;
  19662. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  19663. if (ret != MP_CMP_E) {
  19664. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19665. }
  19666. #ifdef WOLFSSL_KEY_GEN
  19667. wc_FreeDhKey(key);
  19668. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  19669. if (ret != 0)
  19670. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19671. ret = wc_DhGenerateParams(rng, 2048, key);
  19672. if (ret != 0)
  19673. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19674. privSz = sizeof(priv);
  19675. pubSz = sizeof(pub);
  19676. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  19677. #if defined(WOLFSSL_ASYNC_CRYPT)
  19678. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  19679. #endif
  19680. if (ret != 0)
  19681. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19682. #endif /* WOLFSSL_KEY_GEN */
  19683. #endif /* HAVE_SELFTEST */
  19684. ret = 0;
  19685. exit_gen_test:
  19686. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19687. if (key) {
  19688. wc_FreeDhKey(key);
  19689. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19690. }
  19691. #else
  19692. wc_FreeDhKey(key);
  19693. #endif
  19694. return ret;
  19695. }
  19696. static wc_test_ret_t dh_generate_test(WC_RNG *rng)
  19697. {
  19698. wc_test_ret_t ret = 0;
  19699. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19700. DhKey *smallKey = NULL;
  19701. #else
  19702. DhKey smallKey[1];
  19703. #endif
  19704. byte p[2] = { 1, 7 }; /* 263 in decimal */
  19705. byte g[2] = { 0, 2 };
  19706. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  19707. #ifdef WOLFSSL_DH_CONST
  19708. /* the table for constant DH lookup will round to the lowest byte size 21 */
  19709. byte priv[21];
  19710. byte pub[21];
  19711. #else
  19712. byte priv[2];
  19713. byte pub[2];
  19714. #endif
  19715. word32 privSz = sizeof(priv);
  19716. word32 pubSz = sizeof(pub);
  19717. #endif
  19718. int smallKey_inited = 0;
  19719. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19720. if ((smallKey = (DhKey *)XMALLOC(sizeof(*smallKey), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  19721. return WC_TEST_RET_ENC_ERRNO;
  19722. #endif
  19723. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  19724. if (ret != 0)
  19725. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19726. smallKey_inited = 1;
  19727. /* Parameter Validation testing. */
  19728. ret = wc_InitDhKey_ex(NULL, HEAP_HINT, devId);
  19729. if (ret != BAD_FUNC_ARG)
  19730. return WC_TEST_RET_ENC_EC(ret);
  19731. wc_FreeDhKey(NULL);
  19732. ret = wc_DhSetKey(NULL, p, sizeof(p), g, sizeof(g));
  19733. if (ret != BAD_FUNC_ARG) {
  19734. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19735. }
  19736. ret = wc_DhSetKey(smallKey, NULL, sizeof(p), g, sizeof(g));
  19737. if (ret != BAD_FUNC_ARG) {
  19738. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19739. }
  19740. ret = wc_DhSetKey(smallKey, p, 0, g, sizeof(g));
  19741. if (ret != BAD_FUNC_ARG) {
  19742. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19743. }
  19744. ret = wc_DhSetKey(smallKey, p, sizeof(p), NULL, sizeof(g));
  19745. if (ret != BAD_FUNC_ARG) {
  19746. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19747. }
  19748. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, 0);
  19749. if (ret != BAD_FUNC_ARG) {
  19750. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19751. }
  19752. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, sizeof(g));
  19753. if (ret != 0)
  19754. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19755. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  19756. /* Use API. */
  19757. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  19758. #if defined(WOLFSSL_ASYNC_CRYPT)
  19759. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  19760. #endif
  19761. if (ret != 0) {
  19762. ret = WC_TEST_RET_ENC_EC(ret);
  19763. }
  19764. #else
  19765. (void)rng;
  19766. #if defined(HAVE_FIPS) || !defined(WOLFSSL_NO_DH186)
  19767. ret = 0;
  19768. #endif
  19769. #endif
  19770. #if !defined(HAVE_FIPS) && defined(WOLFSSL_NO_DH186)
  19771. {
  19772. byte priv[260];
  19773. byte pub[260];
  19774. word32 privSz = sizeof(priv);
  19775. word32 pubSz = sizeof(pub);
  19776. /* test odd ball param generation with DH */
  19777. wc_FreeDhKey(smallKey);
  19778. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  19779. if (ret != 0)
  19780. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19781. ret = wc_DhGenerateParams(rng, 2056, smallKey);
  19782. if (ret != 0)
  19783. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19784. privSz = sizeof(priv);
  19785. pubSz = sizeof(pub);
  19786. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  19787. #if defined(WOLFSSL_ASYNC_CRYPT)
  19788. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  19789. #endif
  19790. if (ret != 0)
  19791. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19792. }
  19793. #endif /* !HAVE_FIPS and WOLFSSL_NO_DH186 */
  19794. exit_gen_test:
  19795. if (smallKey_inited)
  19796. wc_FreeDhKey(smallKey);
  19797. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19798. if (smallKey != NULL)
  19799. XFREE(smallKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19800. #endif
  19801. return ret;
  19802. }
  19803. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  19804. typedef struct dh_pubvalue_test {
  19805. const byte* data;
  19806. word32 len;
  19807. } dh_pubvalue_test;
  19808. static wc_test_ret_t dh_test_check_pubvalue(void)
  19809. {
  19810. wc_test_ret_t ret;
  19811. word32 i;
  19812. WOLFSSL_SMALL_STACK_STATIC const byte prime[] = {0x01, 0x00, 0x01};
  19813. WOLFSSL_SMALL_STACK_STATIC const byte pubValZero[] = { 0x00 };
  19814. WOLFSSL_SMALL_STACK_STATIC const byte pubValZeroLong[] = { 0x00, 0x00, 0x00 };
  19815. WOLFSSL_SMALL_STACK_STATIC const byte pubValOne[] = { 0x01 };
  19816. WOLFSSL_SMALL_STACK_STATIC const byte pubValOneLong[] = { 0x00, 0x00, 0x01 };
  19817. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeMinusOne[] = { 0x01, 0x00, 0x00 };
  19818. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeLong[] = {0x00, 0x01, 0x00, 0x01};
  19819. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimePlusOne[] = { 0x01, 0x00, 0x02 };
  19820. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig0[] = { 0x02, 0x00, 0x01 };
  19821. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig1[] = { 0x01, 0x01, 0x01 };
  19822. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooLong[] = { 0x01, 0x00, 0x00, 0x01 };
  19823. const dh_pubvalue_test dh_pubval_fail[] = {
  19824. { prime, sizeof(prime) },
  19825. { pubValZero, sizeof(pubValZero) },
  19826. { pubValZeroLong, sizeof(pubValZeroLong) },
  19827. { pubValOne, sizeof(pubValOne) },
  19828. { pubValOneLong, sizeof(pubValOneLong) },
  19829. { pubValPrimeMinusOne, sizeof(pubValPrimeMinusOne) },
  19830. { pubValPrimeLong, sizeof(pubValPrimeLong) },
  19831. { pubValPrimePlusOne, sizeof(pubValPrimePlusOne) },
  19832. { pubValTooBig0, sizeof(pubValTooBig0) },
  19833. { pubValTooBig1, sizeof(pubValTooBig1) },
  19834. { pubValTooLong, sizeof(pubValTooLong) },
  19835. };
  19836. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwo[] = { 0x02 };
  19837. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwoLong[] = { 0x00, 0x00, 0x02 };
  19838. WOLFSSL_SMALL_STACK_STATIC const byte pubValGood[] = { 0x12, 0x34 };
  19839. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLen[] = { 0x00, 0x12, 0x34 };
  19840. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLong[] = { 0x00, 0x00, 0x12, 0x34 };
  19841. const dh_pubvalue_test dh_pubval_pass[] = {
  19842. { pubValTwo, sizeof(pubValTwo) },
  19843. { pubValTwoLong, sizeof(pubValTwoLong) },
  19844. { pubValGood, sizeof(pubValGood) },
  19845. { pubValGoodLen, sizeof(pubValGoodLen) },
  19846. { pubValGoodLong, sizeof(pubValGoodLong) },
  19847. };
  19848. for (i = 0; i < sizeof(dh_pubval_fail) / sizeof(*dh_pubval_fail); i++) {
  19849. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_fail[i].data,
  19850. dh_pubval_fail[i].len);
  19851. if (ret != MP_VAL)
  19852. return WC_TEST_RET_ENC_I(i);
  19853. }
  19854. for (i = 0; i < sizeof(dh_pubval_pass) / sizeof(*dh_pubval_pass); i++) {
  19855. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_pass[i].data,
  19856. dh_pubval_pass[i].len);
  19857. if (ret != 0)
  19858. return WC_TEST_RET_ENC_I(i);
  19859. }
  19860. return 0;
  19861. }
  19862. #endif
  19863. #if defined(HAVE_FFDHE)
  19864. #if defined(HAVE_FFDHE_4096)
  19865. #define MAX_DH_PRIV_SZ 39
  19866. #define MAX_DH_KEY_SZ 512
  19867. #elif defined(HAVE_FFDHE_3072)
  19868. #define MAX_DH_PRIV_SZ 34
  19869. #define MAX_DH_KEY_SZ 384
  19870. #else
  19871. #define MAX_DH_PRIV_SZ 29
  19872. #define MAX_DH_KEY_SZ 256
  19873. #endif
  19874. #ifndef WC_NO_RNG
  19875. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  19876. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  19877. #ifdef HAVE_PUBLIC_FFDHE
  19878. static wc_test_ret_t dh_ffdhe_test(WC_RNG *rng, const DhParams* params)
  19879. #else
  19880. static wc_test_ret_t dh_ffdhe_test(WC_RNG *rng, int name)
  19881. #endif
  19882. {
  19883. wc_test_ret_t ret;
  19884. word32 privSz, pubSz, privSz2, pubSz2;
  19885. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19886. byte *priv = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19887. byte *pub = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19888. byte *priv2 = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19889. byte *pub2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19890. byte *agree = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19891. byte *agree2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19892. DhKey *key = (DhKey*)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19893. DhKey *key2 = (DhKey*)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19894. #else
  19895. byte priv[MAX_DH_PRIV_SZ];
  19896. byte pub[MAX_DH_KEY_SZ];
  19897. byte priv2[MAX_DH_PRIV_SZ];
  19898. byte pub2[MAX_DH_KEY_SZ];
  19899. byte agree[MAX_DH_KEY_SZ];
  19900. byte agree2[MAX_DH_KEY_SZ];
  19901. DhKey key[1];
  19902. DhKey key2[1];
  19903. #endif
  19904. word32 agreeSz = MAX_DH_KEY_SZ;
  19905. word32 agreeSz2 = MAX_DH_KEY_SZ;
  19906. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19907. if ((priv == NULL) ||
  19908. (pub == NULL) ||
  19909. (priv2 == NULL) ||
  19910. (pub2 == NULL) ||
  19911. (agree == NULL) ||
  19912. (agree2 == NULL) ||
  19913. (key == NULL) ||
  19914. (key2 == NULL))
  19915. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  19916. #endif
  19917. pubSz = MAX_DH_KEY_SZ;
  19918. pubSz2 = MAX_DH_KEY_SZ;
  19919. #ifdef HAVE_PUBLIC_FFDHE
  19920. privSz = MAX_DH_PRIV_SZ;
  19921. privSz2 = MAX_DH_PRIV_SZ;
  19922. #else
  19923. privSz = wc_DhGetNamedKeyMinSize(name);
  19924. privSz2 = privSz;
  19925. #endif
  19926. XMEMSET(key, 0, sizeof(*key));
  19927. XMEMSET(key2, 0, sizeof(*key2));
  19928. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  19929. if (ret != 0)
  19930. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19931. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  19932. if (ret != 0)
  19933. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19934. #ifdef HAVE_PUBLIC_FFDHE
  19935. ret = wc_DhSetKey(key, params->p, params->p_len, params->g, params->g_len);
  19936. #else
  19937. ret = wc_DhSetNamedKey(key, name);
  19938. #endif
  19939. if (ret != 0)
  19940. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19941. #ifdef HAVE_PUBLIC_FFDHE
  19942. ret = wc_DhSetKey(key2, params->p, params->p_len, params->g,
  19943. params->g_len);
  19944. #else
  19945. ret = wc_DhSetNamedKey(key2, name);
  19946. #endif
  19947. if (ret != 0)
  19948. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19949. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  19950. #if defined(WOLFSSL_ASYNC_CRYPT)
  19951. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  19952. #endif
  19953. if (ret != 0)
  19954. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19955. ret = wc_DhGenerateKeyPair(key2, rng, priv2, &privSz2, pub2, &pubSz2);
  19956. #if defined(WOLFSSL_ASYNC_CRYPT)
  19957. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  19958. #endif
  19959. if (ret != 0)
  19960. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19961. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  19962. #if defined(WOLFSSL_ASYNC_CRYPT)
  19963. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  19964. #endif
  19965. if (ret != 0)
  19966. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19967. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  19968. #if defined(WOLFSSL_ASYNC_CRYPT)
  19969. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  19970. #endif
  19971. if (ret != 0)
  19972. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19973. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  19974. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  19975. }
  19976. #if (defined(WOLFSSL_HAVE_SP_DH) || defined(USE_FAST_MATH)) && \
  19977. !defined(HAVE_INTEL_QA)
  19978. /* Make p even */
  19979. key->p.dp[0] &= (mp_digit)-2;
  19980. if (ret != 0)
  19981. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19982. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  19983. #if defined(WOLFSSL_ASYNC_CRYPT)
  19984. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  19985. #endif
  19986. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  19987. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19988. }
  19989. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  19990. #if defined(WOLFSSL_ASYNC_CRYPT)
  19991. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  19992. #endif
  19993. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != ASYNC_OP_E) {
  19994. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19995. }
  19996. #ifndef HAVE_SELFTEST
  19997. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  19998. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_CMP_E &&
  19999. ret != ASYNC_OP_E) {
  20000. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20001. }
  20002. #endif
  20003. /* Getting here means success - set ret to 0. */
  20004. ret = 0;
  20005. #endif /* (SP DH or Fast Math) and not Intel QuickAssist */
  20006. done:
  20007. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) && \
  20008. !defined(WC_NO_RNG)
  20009. if (priv)
  20010. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20011. if (pub)
  20012. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20013. if (priv2)
  20014. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20015. if (pub2)
  20016. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20017. if (agree)
  20018. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20019. if (agree2)
  20020. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20021. if (key) {
  20022. wc_FreeDhKey(key);
  20023. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20024. }
  20025. if (key2) {
  20026. wc_FreeDhKey(key2);
  20027. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20028. }
  20029. #else
  20030. wc_FreeDhKey(key);
  20031. wc_FreeDhKey(key2);
  20032. #endif
  20033. return ret;
  20034. }
  20035. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  20036. #endif /* !WC_NO_RNG */
  20037. #endif /* HAVE_FFDHE */
  20038. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dh_test(void)
  20039. {
  20040. wc_test_ret_t ret;
  20041. word32 bytes;
  20042. word32 idx = 0, privSz, pubSz, privSz2, pubSz2;
  20043. #ifndef WC_NO_RNG
  20044. WC_RNG rng;
  20045. int rngInit = 0;
  20046. #endif
  20047. int keyInit = 0, key2Init = 0;
  20048. #define DH_TEST_TMP_SIZE 1024
  20049. #if !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  20050. #define DH_TEST_BUF_SIZE 256
  20051. #else
  20052. #define DH_TEST_BUF_SIZE 512
  20053. #endif
  20054. #ifndef WC_NO_RNG
  20055. word32 agreeSz = DH_TEST_BUF_SIZE;
  20056. word32 agreeSz2 = DH_TEST_BUF_SIZE;
  20057. #endif
  20058. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20059. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20060. DhKey *key2 = (DhKey *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20061. byte *tmp = (byte *)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20062. #else
  20063. DhKey key[1];
  20064. DhKey key2[1];
  20065. byte tmp[DH_TEST_TMP_SIZE];
  20066. #endif
  20067. #ifndef WC_NO_RNG
  20068. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20069. byte *priv = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20070. byte *pub = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20071. byte *priv2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20072. byte *pub2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20073. byte *agree = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20074. byte *agree2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20075. if (priv == NULL || pub == NULL || priv2 == NULL || pub2 == NULL ||
  20076. agree == NULL || agree2 == NULL) {
  20077. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20078. }
  20079. #else
  20080. byte priv[DH_TEST_BUF_SIZE];
  20081. byte pub[DH_TEST_BUF_SIZE];
  20082. byte priv2[DH_TEST_BUF_SIZE];
  20083. byte pub2[DH_TEST_BUF_SIZE];
  20084. byte agree[DH_TEST_BUF_SIZE];
  20085. byte agree2[DH_TEST_BUF_SIZE];
  20086. #endif
  20087. #endif /* !WC_NO_RNG */
  20088. WOLFSSL_ENTER("dh_test");
  20089. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20090. if (key == NULL || key2 == NULL || tmp == NULL) {
  20091. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20092. }
  20093. #endif
  20094. #ifdef USE_CERT_BUFFERS_1024
  20095. XMEMCPY(tmp, dh_key_der_1024, (size_t)sizeof_dh_key_der_1024);
  20096. bytes = (size_t)sizeof_dh_key_der_1024;
  20097. #elif defined(USE_CERT_BUFFERS_2048)
  20098. XMEMCPY(tmp, dh_key_der_2048, (size_t)sizeof_dh_key_der_2048);
  20099. bytes = (size_t)sizeof_dh_key_der_2048;
  20100. #elif defined(USE_CERT_BUFFERS_3072)
  20101. XMEMCPY(tmp, dh_key_der_3072, (size_t)sizeof_dh_key_der_3072);
  20102. bytes = (size_t)sizeof_dh_key_der_3072;
  20103. #elif defined(USE_CERT_BUFFERS_4096)
  20104. XMEMCPY(tmp, dh_key_der_4096, (size_t)sizeof_dh_key_der_4096);
  20105. bytes = (size_t)sizeof_dh_key_der_4096;
  20106. #elif defined(NO_ASN)
  20107. /* don't use file, no DER parsing */
  20108. #elif !defined(NO_FILESYSTEM)
  20109. {
  20110. XFILE file = XFOPEN(dhParamsFile, "rb");
  20111. if (! file)
  20112. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20113. bytes = (word32) XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  20114. XFCLOSE(file);
  20115. if (bytes == 0)
  20116. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20117. }
  20118. #else
  20119. /* No DH key to use. */
  20120. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  20121. #endif /* USE_CERT_BUFFERS */
  20122. (void)idx;
  20123. (void)tmp;
  20124. (void)bytes;
  20125. pubSz = DH_TEST_BUF_SIZE;
  20126. pubSz2 = DH_TEST_BUF_SIZE;
  20127. privSz = DH_TEST_BUF_SIZE;
  20128. privSz2 = DH_TEST_BUF_SIZE;
  20129. #ifndef WC_NO_RNG
  20130. XMEMSET(&rng, 0, sizeof(rng));
  20131. #endif
  20132. /* Use API for coverage. */
  20133. ret = wc_InitDhKey(key);
  20134. if (ret != 0)
  20135. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20136. wc_FreeDhKey(key);
  20137. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  20138. if (ret != 0)
  20139. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20140. keyInit = 1;
  20141. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  20142. if (ret != 0)
  20143. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20144. key2Init = 1;
  20145. #ifdef NO_ASN
  20146. #ifndef WOLFSSL_SP_MATH
  20147. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  20148. if (ret != 0)
  20149. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20150. ret = wc_DhSetKey(key2, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  20151. if (ret != 0)
  20152. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20153. #else
  20154. ret = wc_DhSetKey(key, dh2048_p, sizeof(dh2048_p), dh2048_g,
  20155. sizeof(dh2048_g));
  20156. if (ret != 0)
  20157. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20158. ret = wc_DhSetKey(key2, dh2048_p, sizeof(dh2048_p), dh2048_g,
  20159. sizeof(dh2048_g));
  20160. if (ret != 0)
  20161. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20162. #endif
  20163. #else
  20164. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  20165. if (ret != 0)
  20166. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20167. idx = 0;
  20168. ret = wc_DhKeyDecode(tmp, &idx, key2, bytes);
  20169. if (ret != 0)
  20170. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20171. #endif
  20172. #ifndef WC_NO_RNG
  20173. #ifndef HAVE_FIPS
  20174. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  20175. #else
  20176. ret = wc_InitRng(&rng);
  20177. #endif
  20178. if (ret != 0)
  20179. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20180. rngInit = 1;
  20181. ret = wc_DhGenerateKeyPair(key, &rng, priv, &privSz, pub, &pubSz);
  20182. #if defined(WOLFSSL_ASYNC_CRYPT)
  20183. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  20184. #endif
  20185. if (ret != 0)
  20186. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20187. ret = wc_DhGenerateKeyPair(key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  20188. #if defined(WOLFSSL_ASYNC_CRYPT)
  20189. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  20190. #endif
  20191. if (ret != 0)
  20192. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20193. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  20194. #if defined(WOLFSSL_ASYNC_CRYPT)
  20195. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  20196. #endif
  20197. if (ret != 0)
  20198. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20199. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  20200. #if defined(WOLFSSL_ASYNC_CRYPT)
  20201. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  20202. #endif
  20203. if (ret != 0)
  20204. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20205. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  20206. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  20207. }
  20208. #endif /* !WC_NO_RNG */
  20209. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  20210. ret = wc_DhCheckPrivKey(NULL, NULL, 0);
  20211. if (ret != BAD_FUNC_ARG)
  20212. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20213. ret = wc_DhCheckPrivKey(key, priv, privSz);
  20214. if (ret != 0)
  20215. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20216. ret = wc_DhExportParamsRaw(NULL, NULL, NULL, NULL, NULL, NULL, NULL);
  20217. if (ret != BAD_FUNC_ARG)
  20218. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20219. {
  20220. word32 pSz, qSz, gSz;
  20221. ret = wc_DhExportParamsRaw(key, NULL, &pSz, NULL, &qSz, NULL, &gSz);
  20222. if (ret != LENGTH_ONLY_E)
  20223. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20224. }
  20225. #endif
  20226. /* Test DH key import / export */
  20227. #if defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM) && \
  20228. (!defined(HAVE_FIPS) || \
  20229. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  20230. wc_FreeDhKey(key);
  20231. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  20232. if (ret != 0)
  20233. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20234. #ifndef NO_ASN
  20235. {
  20236. /* DH Private - Key Export / Import */
  20237. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20238. byte *tmp2;
  20239. #else
  20240. byte tmp2[DH_TEST_TMP_SIZE];
  20241. #endif
  20242. #if defined(USE_CERT_BUFFERS_2048)
  20243. XMEMCPY(tmp, dh_ffdhe_statickey_der_2048, sizeof_dh_ffdhe_statickey_der_2048);
  20244. bytes = sizeof_dh_ffdhe_statickey_der_2048;
  20245. #else
  20246. XFILE file = XFOPEN(dhKeyFile, "rb");
  20247. if (!file)
  20248. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20249. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  20250. XFCLOSE(file);
  20251. if (bytes == 0)
  20252. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20253. #endif
  20254. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20255. tmp2 = (byte*)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20256. if (tmp2 == NULL)
  20257. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20258. #endif
  20259. idx = 0;
  20260. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  20261. /* Import DH Private key as DER */
  20262. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  20263. if (ret == 0) {
  20264. /* Export as DER */
  20265. idx = DH_TEST_TMP_SIZE;
  20266. ret = wc_DhPrivKeyToDer(key, tmp2, &idx);
  20267. }
  20268. /* Verify export matches original */
  20269. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  20270. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  20271. }
  20272. /* DH Public Key - Export / Import */
  20273. #if defined(USE_CERT_BUFFERS_2048)
  20274. XMEMCPY(tmp, dh_ffdhe_pub_statickey_der_2048, sizeof_dh_ffdhe_pub_statickey_der_2048);
  20275. bytes = sizeof_dh_ffdhe_pub_statickey_der_2048;
  20276. #else
  20277. file = XFOPEN(dhKeyPubFile, "rb");
  20278. if (!file)
  20279. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20280. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  20281. XFCLOSE(file);
  20282. if (bytes == 0)
  20283. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20284. #endif
  20285. /* for HAVE_WOLF_BIGINT prevent leak */
  20286. wc_FreeDhKey(key);
  20287. (void)wc_InitDhKey_ex(key, HEAP_HINT, devId);
  20288. idx = 0;
  20289. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  20290. /* Import DH Public key as DER */
  20291. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  20292. if (ret == 0) {
  20293. /* Export as DER */
  20294. idx = DH_TEST_TMP_SIZE;
  20295. ret = wc_DhPubKeyToDer(key, tmp2, &idx);
  20296. }
  20297. /* Verify export matches original */
  20298. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  20299. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  20300. }
  20301. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20302. XFREE(tmp2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20303. #endif
  20304. }
  20305. #else
  20306. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  20307. if (ret != 0)
  20308. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20309. #endif /* !NO_ASN */
  20310. privSz = DH_TEST_BUF_SIZE;
  20311. pubSz = DH_TEST_BUF_SIZE;
  20312. ret = wc_DhExportKeyPair(key, priv, &privSz, pub, &pubSz);
  20313. if (ret != 0)
  20314. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20315. ret = wc_DhImportKeyPair(key2, priv, privSz, pub, pubSz);
  20316. if (ret != 0)
  20317. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20318. #endif /* WOLFSSL_DH_EXTRA && !NO_FILESYSTEM && !FIPS <= 2 */
  20319. #ifndef WC_NO_RNG
  20320. ret = dh_generate_test(&rng);
  20321. if (ret != 0)
  20322. ERROR_OUT(ret, done);
  20323. ret = dh_fips_generate_test(&rng);
  20324. if (ret != 0)
  20325. ERROR_OUT(ret, done);
  20326. #endif /* !WC_NO_RNG */
  20327. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  20328. ret = dh_test_check_pubvalue();
  20329. if (ret != 0)
  20330. ERROR_OUT(ret, done);
  20331. #endif
  20332. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  20333. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  20334. /* RNG with DH and SP_ASM code not supported in the in-lined FIPS ASM code,
  20335. * this will be available for testing in the 140-3 module */
  20336. #ifndef WC_NO_RNG
  20337. /* Specialized code for key gen when using FFDHE-2048, FFDHE-3072 and FFDHE-4096 */
  20338. #ifdef HAVE_FFDHE_2048
  20339. #ifdef HAVE_PUBLIC_FFDHE
  20340. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe2048_Get());
  20341. #else
  20342. ret = dh_ffdhe_test(&rng, WC_FFDHE_2048);
  20343. #endif
  20344. if (ret != 0)
  20345. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20346. #endif
  20347. #ifdef HAVE_FFDHE_3072
  20348. #ifdef HAVE_PUBLIC_FFDHE
  20349. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe3072_Get());
  20350. #else
  20351. ret = dh_ffdhe_test(&rng, WC_FFDHE_3072);
  20352. #endif
  20353. if (ret != 0)
  20354. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20355. #endif
  20356. #ifdef HAVE_FFDHE_4096
  20357. #ifdef HAVE_PUBLIC_FFDHE
  20358. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe4096_Get());
  20359. #else
  20360. ret = dh_ffdhe_test(&rng, WC_FFDHE_4096);
  20361. #endif
  20362. if (ret != 0)
  20363. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20364. #endif
  20365. #endif /* !WC_NO_RNG */
  20366. #endif /* HAVE_FIPS_VERSION == 2 && !WOLFSSL_SP_ARM64_ASM */
  20367. wc_FreeDhKey(key);
  20368. keyInit = 0;
  20369. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  20370. !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(WC_NO_RNG)
  20371. /* Test Check Key */
  20372. ret = wc_DhSetCheckKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g),
  20373. NULL, 0, 0, &rng);
  20374. if (ret != 0)
  20375. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20376. keyInit = 1; /* DhSetCheckKey also initializes the key, free it */
  20377. #endif
  20378. done:
  20379. #ifndef WC_NO_RNG
  20380. if (rngInit)
  20381. wc_FreeRng(&rng);
  20382. #endif
  20383. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20384. if (key) {
  20385. if (keyInit)
  20386. wc_FreeDhKey(key);
  20387. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20388. }
  20389. if (key2) {
  20390. if (key2Init)
  20391. wc_FreeDhKey(key2);
  20392. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20393. }
  20394. if (tmp)
  20395. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20396. if (priv)
  20397. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20398. if (pub)
  20399. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20400. if (priv2)
  20401. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20402. if (pub2)
  20403. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20404. if (agree)
  20405. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20406. if (agree2)
  20407. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20408. #else
  20409. if (keyInit)
  20410. wc_FreeDhKey(key);
  20411. if (key2Init)
  20412. wc_FreeDhKey(key2);
  20413. #endif
  20414. (void)privSz;
  20415. (void)pubSz;
  20416. (void)pubSz2;
  20417. (void)privSz2;
  20418. return ret;
  20419. #undef DH_TEST_BUF_SIZE
  20420. #undef DH_TEST_TMP_SIZE
  20421. }
  20422. #endif /* NO_DH */
  20423. #ifndef NO_DSA
  20424. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dsa_test(void)
  20425. {
  20426. wc_test_ret_t ret = 0;
  20427. int answer;
  20428. word32 bytes;
  20429. word32 idx = 0;
  20430. WC_RNG rng;
  20431. int rng_inited = 0;
  20432. wc_Sha sha;
  20433. byte hash[WC_SHA_DIGEST_SIZE];
  20434. byte signature[40];
  20435. int key_inited = 0;
  20436. #ifdef WOLFSSL_KEY_GEN
  20437. byte* der = 0;
  20438. int derIn_inited = 0;
  20439. int genKey_inited = 0;
  20440. #endif
  20441. #define DSA_TEST_TMP_SIZE 1024
  20442. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20443. byte *tmp = (byte *)XMALLOC(DSA_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20444. DsaKey *key = (DsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20445. #ifdef WOLFSSL_KEY_GEN
  20446. DsaKey *derIn = (DsaKey *)XMALLOC(sizeof *derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20447. DsaKey *genKey = (DsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20448. #endif
  20449. WOLFSSL_ENTER("dsa_test");
  20450. if ((tmp == NULL) ||
  20451. (key == NULL)
  20452. #ifdef WOLFSSL_KEY_GEN
  20453. || (derIn == NULL)
  20454. || (genKey == NULL)
  20455. #endif
  20456. ) {
  20457. ret = WC_TEST_RET_ENC_NC;
  20458. goto out;
  20459. }
  20460. #else
  20461. byte tmp[1024];
  20462. DsaKey key[1];
  20463. #ifdef WOLFSSL_KEY_GEN
  20464. DsaKey derIn[1];
  20465. DsaKey genKey[1];
  20466. #endif
  20467. #endif
  20468. #ifdef USE_CERT_BUFFERS_1024
  20469. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  20470. bytes = sizeof_dsa_key_der_1024;
  20471. #elif defined(USE_CERT_BUFFERS_2048)
  20472. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  20473. bytes = sizeof_dsa_key_der_2048;
  20474. #else
  20475. {
  20476. XFILE file = XFOPEN(dsaKey, "rb");
  20477. if (!file)
  20478. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  20479. bytes = (word32) XFREAD(tmp, 1, DSA_TEST_TMP_SIZE, file);
  20480. XFCLOSE(file);
  20481. if (bytes == 0)
  20482. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  20483. }
  20484. #endif /* USE_CERT_BUFFERS */
  20485. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  20486. if (ret != 0)
  20487. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20488. wc_ShaUpdate(&sha, tmp, bytes);
  20489. wc_ShaFinal(&sha, hash);
  20490. wc_ShaFree(&sha);
  20491. ret = wc_InitDsaKey(key);
  20492. if (ret != 0)
  20493. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20494. key_inited = 1;
  20495. ret = wc_DsaPrivateKeyDecode(tmp, &idx, key, bytes);
  20496. if (ret != 0)
  20497. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20498. #ifndef HAVE_FIPS
  20499. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  20500. #else
  20501. ret = wc_InitRng(&rng);
  20502. #endif
  20503. if (ret != 0)
  20504. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20505. rng_inited = 1;
  20506. ret = wc_DsaSign(hash, signature, key, &rng);
  20507. if (ret != 0)
  20508. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20509. ret = wc_DsaVerify(hash, signature, key, &answer);
  20510. if (ret != 0)
  20511. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20512. if (answer != 1)
  20513. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  20514. wc_FreeDsaKey(key);
  20515. key_inited = 0;
  20516. ret = wc_InitDsaKey_h(key, NULL);
  20517. if (ret != 0)
  20518. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20519. key_inited = 1;
  20520. #ifdef WOLFSSL_KEY_GEN
  20521. {
  20522. int derSz = 0;
  20523. ret = wc_InitDsaKey(genKey);
  20524. if (ret != 0)
  20525. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20526. genKey_inited = 1;
  20527. ret = wc_MakeDsaParameters(&rng, 1024, genKey);
  20528. if (ret != 0)
  20529. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20530. ret = wc_MakeDsaKey(&rng, genKey);
  20531. if (ret != 0)
  20532. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20533. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20534. if (der == NULL)
  20535. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  20536. derSz = wc_DsaKeyToDer(genKey, der, FOURK_BUF);
  20537. if (derSz < 0)
  20538. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), out);
  20539. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  20540. DSA_PRIVATEKEY_TYPE);
  20541. if (ret != 0)
  20542. goto out;
  20543. ret = wc_InitDsaKey(derIn);
  20544. if (ret != 0)
  20545. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20546. derIn_inited = 1;
  20547. idx = 0;
  20548. ret = wc_DsaPrivateKeyDecode(der, &idx, derIn, (word32)derSz);
  20549. if (ret != 0)
  20550. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20551. }
  20552. #endif /* WOLFSSL_KEY_GEN */
  20553. out:
  20554. #ifdef WOLFSSL_KEY_GEN
  20555. if (der)
  20556. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20557. #endif
  20558. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20559. if (tmp)
  20560. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20561. if (key) {
  20562. if (key_inited)
  20563. wc_FreeDsaKey(key);
  20564. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20565. }
  20566. #ifdef WOLFSSL_KEY_GEN
  20567. if (derIn) {
  20568. if (derIn_inited)
  20569. wc_FreeDsaKey(derIn);
  20570. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20571. }
  20572. if (genKey) {
  20573. if (genKey_inited)
  20574. wc_FreeDsaKey(genKey);
  20575. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20576. }
  20577. #endif
  20578. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  20579. if (key_inited)
  20580. wc_FreeDsaKey(key);
  20581. #ifdef WOLFSSL_KEY_GEN
  20582. if (derIn_inited)
  20583. wc_FreeDsaKey(derIn);
  20584. if (genKey_inited)
  20585. wc_FreeDsaKey(genKey);
  20586. #endif
  20587. #endif
  20588. if (rng_inited)
  20589. wc_FreeRng(&rng);
  20590. return ret;
  20591. }
  20592. #endif /* NO_DSA */
  20593. #ifdef WOLFCRYPT_HAVE_SRP
  20594. static wc_test_ret_t generate_random_salt(byte *buf, word32 size)
  20595. {
  20596. wc_test_ret_t ret = WC_TEST_RET_ENC_NC;
  20597. WC_RNG rng;
  20598. if(NULL == buf || !size)
  20599. return WC_TEST_RET_ENC_NC;
  20600. if (buf && size && wc_InitRng_ex(&rng, HEAP_HINT, devId) == 0) {
  20601. ret = wc_RNG_GenerateBlock(&rng, (byte *)buf, size);
  20602. wc_FreeRng(&rng);
  20603. }
  20604. return ret;
  20605. }
  20606. static wc_test_ret_t srp_test_digest(SrpType dgstType)
  20607. {
  20608. wc_test_ret_t r;
  20609. byte clientPubKey[192]; /* A */
  20610. byte serverPubKey[192]; /* B */
  20611. word32 clientPubKeySz = 192;
  20612. word32 serverPubKeySz = 192;
  20613. byte username[] = "user";
  20614. word32 usernameSz = 4;
  20615. byte password[] = "password";
  20616. word32 passwordSz = 8;
  20617. WOLFSSL_SMALL_STACK_STATIC const byte N[] = {
  20618. 0xfc, 0x58, 0x7a, 0x8a, 0x70, 0xfb, 0x5a, 0x9a,
  20619. 0x5d, 0x39, 0x48, 0xbf, 0x1c, 0x46, 0xd8, 0x3b,
  20620. 0x7a, 0xe9, 0x1f, 0x85, 0x36, 0x18, 0xc4, 0x35,
  20621. 0x3f, 0xf8, 0x8a, 0x8f, 0x8c, 0x10, 0x2e, 0x01,
  20622. 0x58, 0x1d, 0x41, 0xcb, 0xc4, 0x47, 0xa8, 0xaf,
  20623. 0x9a, 0x6f, 0x58, 0x14, 0xa4, 0x68, 0xf0, 0x9c,
  20624. 0xa6, 0xe7, 0xbf, 0x0d, 0xe9, 0x62, 0x0b, 0xd7,
  20625. 0x26, 0x46, 0x5b, 0x27, 0xcb, 0x4c, 0xf9, 0x7e,
  20626. 0x1e, 0x8b, 0xe6, 0xdd, 0x29, 0xb7, 0xb7, 0x15,
  20627. 0x2e, 0xcf, 0x23, 0xa6, 0x4b, 0x97, 0x9f, 0x89,
  20628. 0xd4, 0x86, 0xc4, 0x90, 0x63, 0x92, 0xf4, 0x30,
  20629. 0x26, 0x69, 0x48, 0x9d, 0x7a, 0x4f, 0xad, 0xb5,
  20630. 0x6a, 0x51, 0xad, 0xeb, 0xf9, 0x90, 0x31, 0x77,
  20631. 0x53, 0x30, 0x2a, 0x85, 0xf7, 0x11, 0x21, 0x0c,
  20632. 0xb8, 0x4b, 0x56, 0x03, 0x5e, 0xbb, 0x25, 0x33,
  20633. 0x7c, 0xd9, 0x5a, 0xd1, 0x5c, 0xb2, 0xd4, 0x53,
  20634. 0xc5, 0x16, 0x68, 0xf0, 0xdf, 0x48, 0x55, 0x3e,
  20635. 0xd4, 0x59, 0x87, 0x64, 0x59, 0xaa, 0x39, 0x01,
  20636. 0x45, 0x89, 0x9c, 0x72, 0xff, 0xdd, 0x8f, 0x6d,
  20637. 0xa0, 0x42, 0xbc, 0x6f, 0x6e, 0x62, 0x18, 0x2d,
  20638. 0x50, 0xe8, 0x18, 0x97, 0x87, 0xfc, 0xef, 0x1f,
  20639. 0xf5, 0x53, 0x68, 0xe8, 0x49, 0xd1, 0xa2, 0xe8,
  20640. 0xb9, 0x26, 0x03, 0xba, 0xb5, 0x58, 0x6f, 0x6c,
  20641. 0x8b, 0x08, 0xa1, 0x7b, 0x6f, 0x42, 0xc9, 0x53
  20642. };
  20643. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  20644. 0x02
  20645. };
  20646. byte salt[10];
  20647. byte verifier[192];
  20648. word32 v_size = sizeof(verifier);
  20649. word32 clientProofSz = SRP_MAX_DIGEST_SIZE;
  20650. word32 serverProofSz = SRP_MAX_DIGEST_SIZE;
  20651. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20652. Srp *cli = (Srp *)XMALLOC(sizeof *cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20653. Srp *srv = (Srp *)XMALLOC(sizeof *srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20654. byte *clientProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  20655. DYNAMIC_TYPE_TMP_BUFFER); /* M1 */
  20656. byte *serverProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  20657. DYNAMIC_TYPE_TMP_BUFFER); /* M2 */
  20658. if ((cli == NULL) ||
  20659. (srv == NULL) ||
  20660. (clientProof == NULL) ||
  20661. (serverProof == NULL)) {
  20662. r = WC_TEST_RET_ENC_NC;
  20663. goto out;
  20664. }
  20665. #else
  20666. Srp cli[1], srv[1];
  20667. byte clientProof[SRP_MAX_DIGEST_SIZE]; /* M1 */
  20668. byte serverProof[SRP_MAX_DIGEST_SIZE]; /* M2 */
  20669. #endif
  20670. /* set as 0's so if second init on srv not called SrpTerm is not on
  20671. * garbage values */
  20672. XMEMSET(srv, 0, sizeof *srv);
  20673. XMEMSET(cli, 0, sizeof *cli);
  20674. /* generating random salt */
  20675. r = generate_random_salt(salt, sizeof(salt));
  20676. /* client knows username and password. */
  20677. /* server knows N, g, salt and verifier. */
  20678. if (!r) r = wc_SrpInit_ex(cli, dgstType, SRP_CLIENT_SIDE, HEAP_HINT, devId);
  20679. if (!r) r = wc_SrpSetUsername(cli, username, usernameSz);
  20680. /* loading N, g and salt in advance to generate the verifier. */
  20681. if (!r) r = wc_SrpSetParams(cli, N, sizeof(N),
  20682. g, sizeof(g),
  20683. salt, sizeof(salt));
  20684. if (!r) r = wc_SrpSetPassword(cli, password, passwordSz);
  20685. if (!r) r = wc_SrpGetVerifier(cli, verifier, &v_size);
  20686. /* client sends username to server */
  20687. if (!r) r = wc_SrpInit_ex(srv, dgstType, SRP_SERVER_SIDE, HEAP_HINT, devId);
  20688. if (!r) r = wc_SrpSetUsername(srv, username, usernameSz);
  20689. if (!r) r = wc_SrpSetParams(srv, N, sizeof(N),
  20690. g, sizeof(g),
  20691. salt, sizeof(salt));
  20692. if (!r) r = wc_SrpSetVerifier(srv, verifier, v_size);
  20693. if (!r) r = wc_SrpGetPublic(srv, serverPubKey, &serverPubKeySz);
  20694. /* server sends N, g, salt and B to client */
  20695. if (!r) r = wc_SrpGetPublic(cli, clientPubKey, &clientPubKeySz);
  20696. if (!r) r = wc_SrpComputeKey(cli, clientPubKey, clientPubKeySz,
  20697. serverPubKey, serverPubKeySz);
  20698. if (!r) r = wc_SrpGetProof(cli, clientProof, &clientProofSz);
  20699. /* client sends A and M1 to server */
  20700. if (!r) r = wc_SrpComputeKey(srv, clientPubKey, clientPubKeySz,
  20701. serverPubKey, serverPubKeySz);
  20702. if (!r) r = wc_SrpVerifyPeersProof(srv, clientProof, clientProofSz);
  20703. if (!r) r = wc_SrpGetProof(srv, serverProof, &serverProofSz);
  20704. /* server sends M2 to client */
  20705. if (!r) r = wc_SrpVerifyPeersProof(cli, serverProof, serverProofSz);
  20706. wc_SrpTerm(cli);
  20707. wc_SrpTerm(srv);
  20708. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20709. out:
  20710. if (cli)
  20711. XFREE(cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20712. if (srv)
  20713. XFREE(srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20714. if (clientProof)
  20715. XFREE(clientProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20716. if (serverProof)
  20717. XFREE(serverProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20718. #endif
  20719. return r;
  20720. }
  20721. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srp_test(void)
  20722. {
  20723. wc_test_ret_t ret;
  20724. WOLFSSL_ENTER("srp_test");
  20725. #ifndef NO_SHA
  20726. ret = srp_test_digest(SRP_TYPE_SHA);
  20727. if (ret != 0)
  20728. return ret;
  20729. #endif
  20730. #ifndef NO_SHA256
  20731. ret = srp_test_digest(SRP_TYPE_SHA256);
  20732. if (ret != 0)
  20733. return ret;
  20734. #endif
  20735. #ifdef WOLFSSL_SHA384
  20736. ret = srp_test_digest(SRP_TYPE_SHA384);
  20737. if (ret != 0)
  20738. return ret;
  20739. #endif
  20740. #ifdef WOLFSSL_SHA512
  20741. ret = srp_test_digest(SRP_TYPE_SHA512);
  20742. if (ret != 0)
  20743. return ret;
  20744. #endif
  20745. return ret;
  20746. }
  20747. #endif /* WOLFCRYPT_HAVE_SRP */
  20748. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  20749. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  20750. static wc_test_ret_t openssl_aes_test(void)
  20751. {
  20752. #ifdef HAVE_AES_CBC
  20753. #ifdef WOLFSSL_AES_128
  20754. {
  20755. /* EVP_CipherUpdate test */
  20756. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  20757. {
  20758. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  20759. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  20760. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  20761. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  20762. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  20763. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  20764. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  20765. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  20766. };
  20767. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  20768. "0123456789abcdef "; /* align */
  20769. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  20770. "1234567890abcdef "; /* align */
  20771. byte cipher[AES_BLOCK_SIZE * 4];
  20772. byte plain [AES_BLOCK_SIZE * 4];
  20773. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20774. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  20775. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  20776. #else
  20777. EVP_CIPHER_CTX en[1];
  20778. EVP_CIPHER_CTX de[1];
  20779. #endif
  20780. int outlen ;
  20781. int total = 0;
  20782. int i;
  20783. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20784. if ((en == NULL) || (de == NULL))
  20785. return MEMORY_E;
  20786. #endif
  20787. EVP_CIPHER_CTX_init(en);
  20788. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  20789. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  20790. return WC_TEST_RET_ENC_NC;
  20791. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  20792. (byte*)cbcPlain, 9) == 0)
  20793. return WC_TEST_RET_ENC_NC;
  20794. if (outlen != 0)
  20795. return WC_TEST_RET_ENC_NC;
  20796. total += outlen;
  20797. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  20798. (byte*)&cbcPlain[9] , 9) == 0)
  20799. return WC_TEST_RET_ENC_NC;
  20800. if (outlen != 16)
  20801. return WC_TEST_RET_ENC_NC;
  20802. total += outlen;
  20803. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  20804. return WC_TEST_RET_ENC_NC;
  20805. if (outlen != 16)
  20806. return WC_TEST_RET_ENC_NC;
  20807. total += outlen;
  20808. if (total != 32)
  20809. return 3408;
  20810. EVP_CIPHER_CTX_cleanup(en);
  20811. total = 0;
  20812. EVP_CIPHER_CTX_init(de);
  20813. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  20814. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  20815. return WC_TEST_RET_ENC_NC;
  20816. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  20817. return WC_TEST_RET_ENC_NC;
  20818. if (outlen != 0)
  20819. return WC_TEST_RET_ENC_NC;
  20820. total += outlen;
  20821. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  20822. (byte*)&cipher[6], 12) == 0)
  20823. return WC_TEST_RET_ENC_NC;
  20824. if (outlen != 0)
  20825. total += outlen;
  20826. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  20827. (byte*)&cipher[6+12], 14) == 0)
  20828. return WC_TEST_RET_ENC_NC;
  20829. if (outlen != 16)
  20830. return WC_TEST_RET_ENC_NC;
  20831. total += outlen;
  20832. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  20833. return WC_TEST_RET_ENC_NC;
  20834. if (outlen != 2)
  20835. return WC_TEST_RET_ENC_NC;
  20836. total += outlen;
  20837. if (total != 18)
  20838. return 3427;
  20839. if (XMEMCMP(plain, cbcPlain, 18))
  20840. return WC_TEST_RET_ENC_NC;
  20841. EVP_CIPHER_CTX_cleanup(de);
  20842. /* test with encrypting/decrypting more than 16 bytes at once */
  20843. total = 0;
  20844. EVP_CIPHER_CTX_init(en);
  20845. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  20846. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  20847. return WC_TEST_RET_ENC_NC;
  20848. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  20849. (byte*)cbcPlain, 17) == 0)
  20850. return WC_TEST_RET_ENC_NC;
  20851. if (outlen != 16)
  20852. return WC_TEST_RET_ENC_NC;
  20853. total += outlen;
  20854. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  20855. (byte*)&cbcPlain[17] , 1) == 0)
  20856. return WC_TEST_RET_ENC_NC;
  20857. if (outlen != 0)
  20858. return WC_TEST_RET_ENC_NC;
  20859. total += outlen;
  20860. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  20861. return WC_TEST_RET_ENC_NC;
  20862. if (outlen != 16)
  20863. return WC_TEST_RET_ENC_NC;
  20864. total += outlen;
  20865. if (total != 32)
  20866. return WC_TEST_RET_ENC_NC;
  20867. EVP_CIPHER_CTX_cleanup(en);
  20868. total = 0;
  20869. EVP_CIPHER_CTX_init(de);
  20870. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  20871. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  20872. return WC_TEST_RET_ENC_NC;
  20873. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 17) == 0)
  20874. return WC_TEST_RET_ENC_NC;
  20875. if (outlen != 16)
  20876. return WC_TEST_RET_ENC_NC;
  20877. total += outlen;
  20878. /* final call on non block size should fail */
  20879. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  20880. return WC_TEST_RET_ENC_NC;
  20881. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  20882. (byte*)&cipher[17], 1) == 0)
  20883. return WC_TEST_RET_ENC_NC;
  20884. if (outlen != 0)
  20885. total += outlen;
  20886. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  20887. (byte*)&cipher[17+1], 14) == 0)
  20888. return WC_TEST_RET_ENC_NC;
  20889. if (outlen != 0)
  20890. return WC_TEST_RET_ENC_NC;
  20891. total += outlen;
  20892. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  20893. return WC_TEST_RET_ENC_NC;
  20894. if (outlen != 2)
  20895. return WC_TEST_RET_ENC_NC;
  20896. total += outlen;
  20897. if (total != 18)
  20898. return WC_TEST_RET_ENC_NC;
  20899. if (XMEMCMP(plain, cbcPlain, 18))
  20900. return WC_TEST_RET_ENC_NC;
  20901. /* test byte by byte decrypt */
  20902. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  20903. plain[i] = i;
  20904. }
  20905. EVP_CIPHER_CTX_cleanup(de);
  20906. total = 0;
  20907. EVP_CIPHER_CTX_init(en);
  20908. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  20909. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  20910. return WC_TEST_RET_ENC_NC;
  20911. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  20912. (byte*)plain, AES_BLOCK_SIZE * 3) == 0)
  20913. return WC_TEST_RET_ENC_NC;
  20914. if (outlen != AES_BLOCK_SIZE * 3)
  20915. return WC_TEST_RET_ENC_NC;
  20916. total += outlen;
  20917. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  20918. return WC_TEST_RET_ENC_NC;
  20919. if (outlen != AES_BLOCK_SIZE)
  20920. return WC_TEST_RET_ENC_NC;
  20921. total += outlen;
  20922. if (total != sizeof(plain))
  20923. return WC_TEST_RET_ENC_NC;
  20924. EVP_CIPHER_CTX_cleanup(en);
  20925. total = 0;
  20926. EVP_CIPHER_CTX_init(de);
  20927. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  20928. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  20929. return WC_TEST_RET_ENC_NC;
  20930. for (i = 0; i < AES_BLOCK_SIZE * 4; i++) {
  20931. if (EVP_CipherUpdate(de, (byte*)plain + total, &outlen,
  20932. (byte*)cipher + i, 1) == 0)
  20933. return WC_TEST_RET_ENC_NC;
  20934. if (outlen > 0) {
  20935. int j;
  20936. total += outlen;
  20937. for (j = 0; j < total; j++) {
  20938. if (plain[j] != j) {
  20939. return WC_TEST_RET_ENC_NC;
  20940. }
  20941. }
  20942. }
  20943. }
  20944. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  20945. return WC_TEST_RET_ENC_NC;
  20946. total += outlen;
  20947. if (total != AES_BLOCK_SIZE * 3) {
  20948. return WC_TEST_RET_ENC_NC;
  20949. }
  20950. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  20951. if (plain[i] != i) {
  20952. return WC_TEST_RET_ENC_NC;
  20953. }
  20954. }
  20955. EVP_CIPHER_CTX_cleanup(de);
  20956. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20957. wolfSSL_EVP_CIPHER_CTX_free(en);
  20958. wolfSSL_EVP_CIPHER_CTX_free(de);
  20959. #endif
  20960. }
  20961. /* set buffers to be exact size to catch potential over read/write */
  20962. {
  20963. /* EVP_CipherUpdate test */
  20964. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  20965. {
  20966. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  20967. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  20968. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  20969. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  20970. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  20971. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  20972. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  20973. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  20974. };
  20975. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  20976. "0123456789abcdef "; /* align */
  20977. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  20978. "1234567890abcdef "; /* align */
  20979. #define EVP_TEST_BUF_SZ 18
  20980. #define EVP_TEST_BUF_PAD 32
  20981. byte cipher[EVP_TEST_BUF_SZ];
  20982. byte plain [EVP_TEST_BUF_SZ];
  20983. byte padded[EVP_TEST_BUF_PAD];
  20984. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20985. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  20986. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  20987. #else
  20988. EVP_CIPHER_CTX en[1];
  20989. EVP_CIPHER_CTX de[1];
  20990. #endif
  20991. int outlen ;
  20992. int total = 0;
  20993. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20994. if ((en == NULL) || (de == NULL))
  20995. return MEMORY_E;
  20996. #endif
  20997. EVP_CIPHER_CTX_init(en);
  20998. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  20999. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  21000. return WC_TEST_RET_ENC_NC;
  21001. if (EVP_CIPHER_CTX_set_padding(en, 0) != 1)
  21002. return WC_TEST_RET_ENC_NC;
  21003. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  21004. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  21005. return WC_TEST_RET_ENC_NC;
  21006. if (outlen != 16)
  21007. return WC_TEST_RET_ENC_NC;
  21008. total += outlen;
  21009. /* should fail here */
  21010. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) != 0)
  21011. return WC_TEST_RET_ENC_NC;
  21012. EVP_CIPHER_CTX_cleanup(en);
  21013. /* turn padding back on and do successful encrypt */
  21014. total = 0;
  21015. EVP_CIPHER_CTX_init(en);
  21016. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  21017. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  21018. return WC_TEST_RET_ENC_NC;
  21019. if (EVP_CIPHER_CTX_set_padding(en, 1) != 1)
  21020. return WC_TEST_RET_ENC_NC;
  21021. if (EVP_CipherUpdate(en, (byte*)padded, &outlen,
  21022. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  21023. return WC_TEST_RET_ENC_NC;
  21024. if (outlen != 16)
  21025. return WC_TEST_RET_ENC_NC;
  21026. total += outlen;
  21027. if (EVP_CipherFinal(en, (byte*)&padded[total], &outlen) == 0)
  21028. return WC_TEST_RET_ENC_NC;
  21029. total += outlen;
  21030. if (total != 32)
  21031. return WC_TEST_RET_ENC_NC;
  21032. XMEMCPY(cipher, padded, EVP_TEST_BUF_SZ);
  21033. EVP_CIPHER_CTX_cleanup(en);
  21034. /* test out of bounds read on buffers w/o padding during decryption */
  21035. total = 0;
  21036. EVP_CIPHER_CTX_init(de);
  21037. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  21038. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  21039. return WC_TEST_RET_ENC_NC;
  21040. if (EVP_CIPHER_CTX_set_padding(de, 0) != 1)
  21041. return WC_TEST_RET_ENC_NC;
  21042. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher,
  21043. EVP_TEST_BUF_SZ) == 0)
  21044. return WC_TEST_RET_ENC_NC;
  21045. if (outlen != 16)
  21046. return WC_TEST_RET_ENC_NC;
  21047. total += outlen;
  21048. /* should fail since not using padding */
  21049. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  21050. return WC_TEST_RET_ENC_NC;
  21051. EVP_CIPHER_CTX_cleanup(de);
  21052. total = 0;
  21053. EVP_CIPHER_CTX_init(de);
  21054. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  21055. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  21056. return WC_TEST_RET_ENC_NC;
  21057. if (EVP_CIPHER_CTX_set_padding(de, 1) != 1)
  21058. return WC_TEST_RET_ENC_NC;
  21059. if (EVP_CipherUpdate(de, (byte*)padded, &outlen, (byte*)padded,
  21060. EVP_TEST_BUF_PAD) == 0)
  21061. return WC_TEST_RET_ENC_NC;
  21062. if (outlen != 16)
  21063. return WC_TEST_RET_ENC_NC;
  21064. total += outlen;
  21065. if (EVP_CipherFinal(de, (byte*)&padded[total], &outlen) == 0)
  21066. return WC_TEST_RET_ENC_NC;
  21067. if (XMEMCMP(padded, cbcPlain, EVP_TEST_BUF_SZ))
  21068. return WC_TEST_RET_ENC_NC;
  21069. EVP_CIPHER_CTX_cleanup(de);
  21070. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21071. wolfSSL_EVP_CIPHER_CTX_free(en);
  21072. wolfSSL_EVP_CIPHER_CTX_free(de);
  21073. #endif
  21074. }
  21075. { /* evp_cipher test: EVP_aes_128_cbc */
  21076. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21077. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  21078. #else
  21079. EVP_CIPHER_CTX ctx[1];
  21080. #endif
  21081. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  21082. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  21083. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  21084. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  21085. };
  21086. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  21087. {
  21088. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  21089. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  21090. };
  21091. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  21092. "0123456789abcdef "; /* align */
  21093. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  21094. "1234567890abcdef "; /* align */
  21095. byte cipher[AES_BLOCK_SIZE * 4];
  21096. byte plain [AES_BLOCK_SIZE * 4];
  21097. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21098. if (ctx == NULL)
  21099. return MEMORY_E;
  21100. #endif
  21101. EVP_CIPHER_CTX_init(ctx);
  21102. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  21103. return WC_TEST_RET_ENC_NC;
  21104. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  21105. return WC_TEST_RET_ENC_NC;
  21106. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  21107. return WC_TEST_RET_ENC_NC;
  21108. EVP_CIPHER_CTX_cleanup(ctx);
  21109. EVP_CIPHER_CTX_init(ctx);
  21110. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  21111. return WC_TEST_RET_ENC_NC;
  21112. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  21113. return WC_TEST_RET_ENC_NC;
  21114. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  21115. return WC_TEST_RET_ENC_NC;
  21116. EVP_CIPHER_CTX_cleanup(ctx);
  21117. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21118. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  21119. #endif
  21120. } /* end evp_cipher test: EVP_aes_128_cbc*/
  21121. #endif /* WOLFSSL_AES_128 */
  21122. #endif /* HAVE_AES_CBC */
  21123. #ifndef WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API
  21124. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  21125. { /* evp_cipher test: EVP_aes_256_ecb*/
  21126. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21127. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  21128. #else
  21129. EVP_CIPHER_CTX ctx[1];
  21130. #endif
  21131. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  21132. {
  21133. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21134. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  21135. };
  21136. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  21137. {
  21138. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  21139. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  21140. };
  21141. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  21142. {
  21143. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  21144. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  21145. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  21146. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  21147. };
  21148. byte cipher[AES_BLOCK_SIZE * 4];
  21149. byte plain [AES_BLOCK_SIZE * 4];
  21150. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21151. if (ctx == NULL)
  21152. return MEMORY_E;
  21153. #endif
  21154. EVP_CIPHER_CTX_init(ctx);
  21155. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1) == 0)
  21156. return WC_TEST_RET_ENC_NC;
  21157. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  21158. return WC_TEST_RET_ENC_NC;
  21159. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  21160. return WC_TEST_RET_ENC_NC;
  21161. EVP_CIPHER_CTX_init(ctx);
  21162. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0) == 0)
  21163. return WC_TEST_RET_ENC_NC;
  21164. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  21165. return WC_TEST_RET_ENC_NC;
  21166. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  21167. return WC_TEST_RET_ENC_NC;
  21168. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21169. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  21170. #endif
  21171. } /* end evp_cipher test */
  21172. #endif /* HAVE_AES_ECB && WOLFSSL_AES_256 */
  21173. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  21174. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  21175. {
  21176. /* Test: AES_encrypt/decrypt/set Key */
  21177. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21178. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  21179. #ifdef HAVE_AES_DECRYPT
  21180. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  21181. #endif
  21182. #else
  21183. AES_KEY enc[1];
  21184. #ifdef HAVE_AES_DECRYPT
  21185. AES_KEY dec[1];
  21186. #endif
  21187. #endif
  21188. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  21189. {
  21190. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21191. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  21192. };
  21193. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  21194. {
  21195. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  21196. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  21197. };
  21198. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  21199. {
  21200. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  21201. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  21202. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  21203. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  21204. };
  21205. byte plain[sizeof(msg)];
  21206. byte cipher[sizeof(msg)];
  21207. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21208. if (enc == NULL)
  21209. return MEMORY_E;
  21210. #ifdef HAVE_AES_DECRYPT
  21211. if (dec == NULL)
  21212. return MEMORY_E;
  21213. #endif
  21214. #endif
  21215. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  21216. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  21217. AES_encrypt(msg, cipher, enc);
  21218. #ifdef HAVE_AES_DECRYPT
  21219. AES_decrypt(cipher, plain, dec);
  21220. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  21221. return WC_TEST_RET_ENC_NC;
  21222. #endif /* HAVE_AES_DECRYPT */
  21223. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  21224. return WC_TEST_RET_ENC_NC;
  21225. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21226. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  21227. #ifdef HAVE_AES_DECRYPT
  21228. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  21229. #endif
  21230. #endif
  21231. }
  21232. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  21233. #endif /* !WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API */
  21234. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  21235. #ifdef WOLFSSL_AES_COUNTER
  21236. {
  21237. byte plainBuff [64];
  21238. byte cipherBuff[64];
  21239. #ifdef WOLFSSL_AES_128
  21240. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  21241. {
  21242. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  21243. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  21244. };
  21245. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  21246. {
  21247. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  21248. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  21249. };
  21250. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  21251. {
  21252. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21253. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  21254. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  21255. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  21256. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  21257. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  21258. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  21259. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  21260. };
  21261. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  21262. {
  21263. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  21264. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  21265. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  21266. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  21267. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  21268. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  21269. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  21270. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  21271. };
  21272. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  21273. {
  21274. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  21275. 0xc2
  21276. };
  21277. #endif
  21278. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  21279. * NIST Special Publication 800-38A */
  21280. #ifdef WOLFSSL_AES_192
  21281. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  21282. {
  21283. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  21284. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  21285. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  21286. };
  21287. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  21288. {
  21289. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  21290. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  21291. };
  21292. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  21293. {
  21294. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21295. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  21296. };
  21297. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  21298. {
  21299. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  21300. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  21301. };
  21302. #endif /* WOLFSSL_AES_192 */
  21303. #ifdef WOLFSSL_AES_256
  21304. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  21305. * NIST Special Publication 800-38A */
  21306. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  21307. {
  21308. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  21309. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  21310. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  21311. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  21312. };
  21313. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  21314. {
  21315. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  21316. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  21317. };
  21318. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  21319. {
  21320. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21321. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  21322. };
  21323. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  21324. {
  21325. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  21326. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  21327. };
  21328. #endif /* WOLFSSL_AES_256 */
  21329. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21330. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  21331. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  21332. #else
  21333. EVP_CIPHER_CTX en[1];
  21334. EVP_CIPHER_CTX de[1];
  21335. #endif
  21336. #ifdef WOLFSSL_AES_128
  21337. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21338. EVP_CIPHER_CTX *p_en;
  21339. EVP_CIPHER_CTX *p_de;
  21340. #endif
  21341. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21342. if ((en == NULL) || (de == NULL))
  21343. return MEMORY_E;
  21344. #endif
  21345. EVP_CIPHER_CTX_init(en);
  21346. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  21347. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  21348. return WC_TEST_RET_ENC_NC;
  21349. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  21350. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  21351. return WC_TEST_RET_ENC_NC;
  21352. EVP_CIPHER_CTX_init(de);
  21353. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  21354. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  21355. return WC_TEST_RET_ENC_NC;
  21356. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  21357. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  21358. return WC_TEST_RET_ENC_NC;
  21359. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  21360. return WC_TEST_RET_ENC_NC;
  21361. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  21362. return WC_TEST_RET_ENC_NC;
  21363. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  21364. return WC_TEST_RET_ENC_NC;
  21365. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  21366. return WC_TEST_RET_ENC_NC;
  21367. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21368. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  21369. if (p_en == NULL)
  21370. return WC_TEST_RET_ENC_ERRNO;
  21371. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  21372. if (p_de == NULL)
  21373. return WC_TEST_RET_ENC_ERRNO;
  21374. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  21375. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  21376. return WC_TEST_RET_ENC_NC;
  21377. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  21378. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  21379. return WC_TEST_RET_ENC_NC;
  21380. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  21381. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  21382. return WC_TEST_RET_ENC_NC;
  21383. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  21384. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  21385. return WC_TEST_RET_ENC_NC;
  21386. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  21387. return WC_TEST_RET_ENC_NC;
  21388. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  21389. return WC_TEST_RET_ENC_NC;
  21390. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  21391. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  21392. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  21393. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  21394. return WC_TEST_RET_ENC_NC;
  21395. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  21396. return WC_TEST_RET_ENC_NC;
  21397. EVP_CIPHER_CTX_init(en);
  21398. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  21399. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  21400. return WC_TEST_RET_ENC_NC;
  21401. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  21402. return WC_TEST_RET_ENC_NC;
  21403. EVP_CIPHER_CTX_init(de);
  21404. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  21405. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  21406. return WC_TEST_RET_ENC_NC;
  21407. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  21408. return WC_TEST_RET_ENC_NC;
  21409. if (XMEMCMP(plainBuff, ctrPlain, 9))
  21410. return WC_TEST_RET_ENC_NC;
  21411. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  21412. return WC_TEST_RET_ENC_NC;
  21413. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  21414. return WC_TEST_RET_ENC_NC;
  21415. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  21416. return WC_TEST_RET_ENC_NC;
  21417. if (XMEMCMP(plainBuff, ctrPlain, 9))
  21418. return WC_TEST_RET_ENC_NC;
  21419. if (XMEMCMP(cipherBuff, oddCipher, 9))
  21420. return WC_TEST_RET_ENC_NC;
  21421. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  21422. return WC_TEST_RET_ENC_NC;
  21423. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  21424. return WC_TEST_RET_ENC_NC;
  21425. #endif /* WOLFSSL_AES_128 */
  21426. #ifdef WOLFSSL_AES_192
  21427. EVP_CIPHER_CTX_init(en);
  21428. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  21429. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  21430. return WC_TEST_RET_ENC_NC;
  21431. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  21432. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  21433. return WC_TEST_RET_ENC_NC;
  21434. EVP_CIPHER_CTX_init(de);
  21435. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  21436. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  21437. return WC_TEST_RET_ENC_NC;
  21438. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  21439. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  21440. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  21441. return WC_TEST_RET_ENC_NC;
  21442. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  21443. return WC_TEST_RET_ENC_NC;
  21444. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  21445. return WC_TEST_RET_ENC_NC;
  21446. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  21447. return WC_TEST_RET_ENC_NC;
  21448. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  21449. return WC_TEST_RET_ENC_NC;
  21450. #endif /* WOLFSSL_AES_192 */
  21451. #ifdef WOLFSSL_AES_256
  21452. EVP_CIPHER_CTX_init(en);
  21453. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  21454. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  21455. return WC_TEST_RET_ENC_NC;
  21456. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  21457. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  21458. return WC_TEST_RET_ENC_NC;
  21459. EVP_CIPHER_CTX_init(de);
  21460. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  21461. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  21462. return WC_TEST_RET_ENC_NC;
  21463. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  21464. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  21465. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  21466. return WC_TEST_RET_ENC_NC;
  21467. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  21468. return WC_TEST_RET_ENC_NC;
  21469. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  21470. return WC_TEST_RET_ENC_NC;
  21471. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  21472. return WC_TEST_RET_ENC_NC;
  21473. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  21474. return WC_TEST_RET_ENC_NC;
  21475. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21476. wolfSSL_EVP_CIPHER_CTX_free(en);
  21477. wolfSSL_EVP_CIPHER_CTX_free(de);
  21478. #endif
  21479. #endif /* WOLFSSL_AES_256 */
  21480. }
  21481. #endif /* HAVE_AES_COUNTER */
  21482. #ifndef WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API
  21483. #if defined(WOLFSSL_AES_CFB) && defined(WOLFSSL_AES_128)
  21484. {
  21485. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21486. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  21487. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  21488. #else
  21489. AES_KEY enc[1];
  21490. AES_KEY dec[1];
  21491. #endif
  21492. WOLFSSL_SMALL_STACK_STATIC const byte setIv[] = {
  21493. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  21494. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  21495. };
  21496. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  21497. {
  21498. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  21499. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  21500. };
  21501. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  21502. {
  21503. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  21504. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  21505. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  21506. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b
  21507. };
  21508. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  21509. {
  21510. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21511. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  21512. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  21513. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51
  21514. };
  21515. byte cipher[AES_BLOCK_SIZE * 2];
  21516. byte iv[AES_BLOCK_SIZE]; /* iv buffer is updeated by API */
  21517. int num = 0;
  21518. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21519. if ((enc == NULL) || (dec == NULL))
  21520. return MEMORY_E;
  21521. #endif
  21522. XMEMCPY(iv, setIv, sizeof(setIv));
  21523. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, enc);
  21524. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, dec);
  21525. wolfSSL_AES_cfb128_encrypt(msg, cipher, AES_BLOCK_SIZE - 1, enc, iv,
  21526. &num, AES_ENCRYPT);
  21527. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE - 1))
  21528. return WC_TEST_RET_ENC_NC;
  21529. if (num != 15) /* should have used 15 of the 16 bytes */
  21530. return WC_TEST_RET_ENC_NC;
  21531. wolfSSL_AES_cfb128_encrypt(msg + AES_BLOCK_SIZE - 1,
  21532. cipher + AES_BLOCK_SIZE - 1, AES_BLOCK_SIZE + 1, enc, iv,
  21533. &num, AES_ENCRYPT);
  21534. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  21535. return WC_TEST_RET_ENC_NC;
  21536. if (num != 0)
  21537. return WC_TEST_RET_ENC_NC;
  21538. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21539. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  21540. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  21541. #endif
  21542. }
  21543. #endif /* WOLFSSL_AES_CFB && WOLFSSL_AES_128 */
  21544. #endif /* !WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API */
  21545. return 0;
  21546. }
  21547. #endif /* !defined(NO_AES) && !defined(WOLFCRYPT_ONLY) */
  21548. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_test(void)
  21549. {
  21550. wc_test_ret_t ret;
  21551. #ifdef WOLFSSL_SMALL_STACK
  21552. EVP_MD_CTX *md_ctx = (EVP_MD_CTX *)XMALLOC(sizeof(EVP_MD_CTX), NULL, DYNAMIC_TYPE_OPENSSL);
  21553. #else
  21554. EVP_MD_CTX md_ctx[1];
  21555. #endif
  21556. testVector a, b, c, d, e, f;
  21557. byte hash[WC_SHA256_DIGEST_SIZE*2]; /* max size */
  21558. #ifdef WOLFSSL_SMALL_STACK
  21559. if (md_ctx == NULL)
  21560. return WC_TEST_RET_ENC_EC(MEMORY_E);
  21561. #endif
  21562. WOLFSSL_ENTER("openssl_test");
  21563. a.inLen = 0;
  21564. b.inLen = c.inLen = d.inLen = e.inLen = f.inLen = a.inLen;
  21565. (void)a;
  21566. (void)b;
  21567. (void)c;
  21568. (void)d;
  21569. (void)e;
  21570. (void)f;
  21571. /* test malloc / free , 10 is an arbitrary amount of memory chosen */
  21572. {
  21573. byte* p;
  21574. p = (byte*)CRYPTO_malloc(10, "", 0);
  21575. if (p == NULL) {
  21576. return WC_TEST_RET_ENC_NC;
  21577. }
  21578. XMEMSET(p, 0, 10);
  21579. CRYPTO_free(p, "", 0);
  21580. }
  21581. #ifndef NO_MD5
  21582. a.input = "1234567890123456789012345678901234567890123456789012345678"
  21583. "9012345678901234567890";
  21584. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  21585. "\x7a";
  21586. a.inLen = XSTRLEN(a.input);
  21587. a.outLen = WC_MD5_DIGEST_SIZE;
  21588. EVP_MD_CTX_init(md_ctx);
  21589. ret = EVP_DigestInit(md_ctx, EVP_md5());
  21590. if (ret == WOLFSSL_SUCCESS) {
  21591. ret = EVP_DigestUpdate(md_ctx, a.input, (unsigned long)a.inLen);
  21592. }
  21593. if (ret == WOLFSSL_SUCCESS) {
  21594. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21595. }
  21596. EVP_MD_CTX_cleanup(md_ctx);
  21597. if (ret != WOLFSSL_SUCCESS)
  21598. return WC_TEST_RET_ENC_NC;
  21599. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0)
  21600. return WC_TEST_RET_ENC_NC;
  21601. #endif /* NO_MD5 */
  21602. #ifndef NO_SHA
  21603. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  21604. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  21605. "aaaaaaaaaa";
  21606. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  21607. "\x53\x99\x5E\x26\xA0";
  21608. b.inLen = XSTRLEN(b.input);
  21609. b.outLen = WC_SHA_DIGEST_SIZE;
  21610. EVP_MD_CTX_init(md_ctx);
  21611. ret = EVP_DigestInit(md_ctx, EVP_sha1());
  21612. if (ret == WOLFSSL_SUCCESS) {
  21613. ret = EVP_DigestUpdate(md_ctx, b.input, (unsigned long)b.inLen);
  21614. if (ret == WOLFSSL_SUCCESS)
  21615. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21616. }
  21617. EVP_MD_CTX_cleanup(md_ctx);
  21618. if (ret != WOLFSSL_SUCCESS)
  21619. return WC_TEST_RET_ENC_NC;
  21620. if (XMEMCMP(hash, b.output, b.outLen) != 0)
  21621. return WC_TEST_RET_ENC_NC;
  21622. #endif /* NO_SHA */
  21623. #ifdef WOLFSSL_SHA224
  21624. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21625. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21626. e.output = "\xc9\x7c\xa9\xa5\x59\x85\x0c\xe9\x7a\x04\xa9\x6d\xef\x6d\x99"
  21627. "\xa9\xe0\xe0\xe2\xab\x14\xe6\xb8\xdf\x26\x5f\xc0\xb3";
  21628. e.inLen = XSTRLEN(e.input);
  21629. e.outLen = WC_SHA224_DIGEST_SIZE;
  21630. EVP_MD_CTX_init(md_ctx);
  21631. ret = EVP_DigestInit(md_ctx, EVP_sha224());
  21632. if (ret == WOLFSSL_SUCCESS) {
  21633. ret = EVP_DigestUpdate(md_ctx, e.input, (unsigned long)e.inLen);
  21634. if (ret == WOLFSSL_SUCCESS)
  21635. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21636. }
  21637. EVP_MD_CTX_cleanup(md_ctx);
  21638. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  21639. return WC_TEST_RET_ENC_NC;
  21640. }
  21641. #endif /* WOLFSSL_SHA224 */
  21642. #ifndef NO_SHA256
  21643. d.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  21644. d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  21645. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  21646. "\x06\xC1";
  21647. d.inLen = XSTRLEN(d.input);
  21648. d.outLen = WC_SHA256_DIGEST_SIZE;
  21649. EVP_MD_CTX_init(md_ctx);
  21650. ret = EVP_DigestInit(md_ctx, EVP_sha256());
  21651. if (ret == WOLFSSL_SUCCESS) {
  21652. ret = EVP_DigestUpdate(md_ctx, d.input, (unsigned long)d.inLen);
  21653. if (ret == WOLFSSL_SUCCESS)
  21654. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21655. }
  21656. EVP_MD_CTX_cleanup(md_ctx);
  21657. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, d.output, d.outLen) != 0) {
  21658. return WC_TEST_RET_ENC_NC;
  21659. }
  21660. #endif /* !NO_SHA256 */
  21661. #ifdef WOLFSSL_SHA384
  21662. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21663. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21664. e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  21665. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  21666. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  21667. "\x74\x60\x39";
  21668. e.inLen = XSTRLEN(e.input);
  21669. e.outLen = WC_SHA384_DIGEST_SIZE;
  21670. EVP_MD_CTX_init(md_ctx);
  21671. ret = EVP_DigestInit(md_ctx, EVP_sha384());
  21672. if (ret == WOLFSSL_SUCCESS) {
  21673. ret = EVP_DigestUpdate(md_ctx, e.input, (unsigned long)e.inLen);
  21674. if (ret == WOLFSSL_SUCCESS)
  21675. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21676. }
  21677. EVP_MD_CTX_cleanup(md_ctx);
  21678. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  21679. return WC_TEST_RET_ENC_NC;
  21680. }
  21681. #endif /* WOLFSSL_SHA384 */
  21682. #ifdef WOLFSSL_SHA512
  21683. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21684. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21685. f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  21686. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  21687. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  21688. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  21689. "\x87\x4b\xe9\x09";
  21690. f.inLen = XSTRLEN(f.input);
  21691. f.outLen = WC_SHA512_DIGEST_SIZE;
  21692. EVP_MD_CTX_init(md_ctx);
  21693. ret = EVP_DigestInit(md_ctx, EVP_sha512());
  21694. if (ret == WOLFSSL_SUCCESS) {
  21695. ret = EVP_DigestUpdate(md_ctx, f.input, (unsigned long)f.inLen);
  21696. if (ret == WOLFSSL_SUCCESS)
  21697. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21698. }
  21699. EVP_MD_CTX_cleanup(md_ctx);
  21700. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, f.output, f.outLen) != 0) {
  21701. return WC_TEST_RET_ENC_NC;
  21702. }
  21703. #if !defined(WOLFSSL_NOSHA512_224) && \
  21704. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  21705. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21706. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21707. f.output = "\x23\xfe\xc5\xbb\x94\xd6\x0b\x23\x30\x81\x92\x64\x0b\x0c\x45"
  21708. "\x33\x35\xd6\x64\x73\x4f\xe4\x0e\x72\x68\x67\x4a\xf9";
  21709. f.inLen = XSTRLEN(f.input);
  21710. f.outLen = WC_SHA512_224_DIGEST_SIZE;
  21711. EVP_MD_CTX_init(md_ctx);
  21712. ret = EVP_DigestInit(md_ctx, EVP_sha512_224());
  21713. if (ret == WOLFSSL_SUCCESS) {
  21714. ret = EVP_DigestUpdate(md_ctx, f.input, (unsigned long)f.inLen);
  21715. if (ret == WOLFSSL_SUCCESS)
  21716. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21717. }
  21718. EVP_MD_CTX_cleanup(md_ctx);
  21719. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, f.output, f.outLen) != 0) {
  21720. return WC_TEST_RET_ENC_NC;
  21721. }
  21722. #endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
  21723. #if !defined(WOLFSSL_NOSHA512_256) && \
  21724. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  21725. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21726. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21727. f.output = "\x39\x28\xe1\x84\xfb\x86\x90\xf8\x40\xda\x39\x88\x12\x1d\x31"
  21728. "\xbe\x65\xcb\x9d\x3e\xf8\x3e\xe6\x14\x6f\xea\xc8\x61\xe1\x9b"
  21729. "\x56\x3a";
  21730. f.inLen = XSTRLEN(f.input);
  21731. f.outLen = WC_SHA512_256_DIGEST_SIZE;
  21732. EVP_MD_CTX_init(md_ctx);
  21733. ret = EVP_DigestInit(md_ctx, EVP_sha512_256());
  21734. if (ret == WOLFSSL_SUCCESS) {
  21735. ret = EVP_DigestUpdate(md_ctx, f.input, (unsigned long)f.inLen);
  21736. if (ret == WOLFSSL_SUCCESS)
  21737. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21738. }
  21739. EVP_MD_CTX_cleanup(md_ctx);
  21740. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, f.output, f.outLen) != 0) {
  21741. return WC_TEST_RET_ENC_NC;
  21742. }
  21743. #endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
  21744. #endif /* WOLFSSL_SHA512 */
  21745. #ifdef WOLFSSL_SHA3
  21746. #ifndef WOLFSSL_NOSHA3_224
  21747. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21748. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21749. e.output = "\x54\x3e\x68\x68\xe1\x66\x6c\x1a\x64\x36\x30\xdf\x77\x36\x7a"
  21750. "\xe5\xa6\x2a\x85\x07\x0a\x51\xc1\x4c\xbf\x66\x5c\xbc";
  21751. e.inLen = XSTRLEN(e.input);
  21752. e.outLen = WC_SHA3_224_DIGEST_SIZE;
  21753. EVP_MD_CTX_init(md_ctx);
  21754. ret = EVP_DigestInit(md_ctx, EVP_sha3_224());
  21755. if (ret == WOLFSSL_SUCCESS) {
  21756. ret = EVP_DigestUpdate(md_ctx, e.input, (unsigned long)e.inLen);
  21757. if (ret == WOLFSSL_SUCCESS)
  21758. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21759. }
  21760. EVP_MD_CTX_cleanup(md_ctx);
  21761. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  21762. return WC_TEST_RET_ENC_NC;
  21763. }
  21764. #endif /* WOLFSSL_NOSHA3_224 */
  21765. #ifndef WOLFSSL_NOSHA3_256
  21766. d.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21767. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21768. d.output = "\x91\x6f\x60\x61\xfe\x87\x97\x41\xca\x64\x69\xb4\x39\x71\xdf"
  21769. "\xdb\x28\xb1\xa3\x2d\xc3\x6c\xb3\x25\x4e\x81\x2b\xe2\x7a\xad"
  21770. "\x1d\x18";
  21771. d.inLen = XSTRLEN(d.input);
  21772. d.outLen = WC_SHA3_256_DIGEST_SIZE;
  21773. EVP_MD_CTX_init(md_ctx);
  21774. ret = EVP_DigestInit(md_ctx, EVP_sha3_256());
  21775. if (ret == WOLFSSL_SUCCESS) {
  21776. ret = EVP_DigestUpdate(md_ctx, d.input, (unsigned long)d.inLen);
  21777. if (ret == WOLFSSL_SUCCESS)
  21778. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21779. }
  21780. EVP_MD_CTX_cleanup(md_ctx);
  21781. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, d.output, d.outLen) != 0) {
  21782. return WC_TEST_RET_ENC_NC;
  21783. }
  21784. #endif /* WOLFSSL_NOSHA3_256 */
  21785. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21786. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21787. e.output = "\x79\x40\x7d\x3b\x59\x16\xb5\x9c\x3e\x30\xb0\x98\x22\x97\x47"
  21788. "\x91\xc3\x13\xfb\x9e\xcc\x84\x9e\x40\x6f\x23\x59\x2d\x04\xf6"
  21789. "\x25\xdc\x8c\x70\x9b\x98\xb4\x3b\x38\x52\xb3\x37\x21\x61\x79"
  21790. "\xaa\x7f\xc7";
  21791. e.inLen = XSTRLEN(e.input);
  21792. e.outLen = WC_SHA3_384_DIGEST_SIZE;
  21793. EVP_MD_CTX_init(md_ctx);
  21794. ret = EVP_DigestInit(md_ctx, EVP_sha3_384());
  21795. if (ret == WOLFSSL_SUCCESS) {
  21796. ret = EVP_DigestUpdate(md_ctx, e.input, (unsigned long)e.inLen);
  21797. if (ret == WOLFSSL_SUCCESS)
  21798. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21799. }
  21800. EVP_MD_CTX_cleanup(md_ctx);
  21801. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  21802. return WC_TEST_RET_ENC_NC;
  21803. }
  21804. #ifndef WOLFSSL_NOSHA3_512
  21805. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21806. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21807. f.output = "\xaf\xeb\xb2\xef\x54\x2e\x65\x79\xc5\x0c\xad\x06\xd2\xe5\x78"
  21808. "\xf9\xf8\xdd\x68\x81\xd7\xdc\x82\x4d\x26\x36\x0f\xee\xbf\x18"
  21809. "\xa4\xfa\x73\xe3\x26\x11\x22\x94\x8e\xfc\xfd\x49\x2e\x74\xe8"
  21810. "\x2e\x21\x89\xed\x0f\xb4\x40\xd1\x87\xf3\x82\x27\x0c\xb4\x55"
  21811. "\xf2\x1d\xd1\x85";
  21812. f.inLen = XSTRLEN(f.input);
  21813. f.outLen = WC_SHA3_512_DIGEST_SIZE;
  21814. EVP_MD_CTX_init(md_ctx);
  21815. ret = EVP_DigestInit(md_ctx, EVP_sha3_512());
  21816. if (ret == WOLFSSL_SUCCESS) {
  21817. ret = EVP_DigestUpdate(md_ctx, f.input, (unsigned long)f.inLen);
  21818. if (ret == WOLFSSL_SUCCESS)
  21819. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21820. }
  21821. EVP_MD_CTX_cleanup(md_ctx);
  21822. if (ret != WOLFSSL_SUCCESS ||
  21823. XMEMCMP(hash, f.output, f.outLen) != 0) {
  21824. return WC_TEST_RET_ENC_NC;
  21825. }
  21826. #endif /* WOLFSSL_NOSHA3_512 */
  21827. #endif /* WOLFSSL_SHA3 */
  21828. #ifdef WOLFSSL_SMALL_STACK
  21829. XFREE(md_ctx, NULL, DYNAMIC_TYPE_OPENSSL);
  21830. md_ctx = NULL;
  21831. #endif
  21832. #ifndef WC_NO_RNG
  21833. if (RAND_bytes(hash, sizeof(hash)) != WOLFSSL_SUCCESS)
  21834. return WC_TEST_RET_ENC_NC;
  21835. #endif
  21836. #ifndef NO_MD5
  21837. c.input = "what do ya want for nothing?";
  21838. c.output = "\x55\x78\xe8\x48\x4b\xcc\x93\x80\x93\xec\x53\xaf\x22\xd6\x14"
  21839. "\x76";
  21840. c.inLen = XSTRLEN(c.input);
  21841. c.outLen = WC_MD5_DIGEST_SIZE;
  21842. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)
  21843. /* Expect failure with MD5 + HMAC when using FIPS 140-3. */
  21844. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  21845. hash, 0) != NULL)
  21846. #else
  21847. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  21848. hash, 0) == NULL ||
  21849. XMEMCMP(hash, c.output, c.outLen) != 0)
  21850. #endif
  21851. {
  21852. return WC_TEST_RET_ENC_NC;
  21853. }
  21854. #endif /* NO_MD5 */
  21855. #ifndef NO_DES3
  21856. { /* des test */
  21857. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  21858. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  21859. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  21860. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  21861. };
  21862. byte plain[24];
  21863. byte cipher[24];
  21864. const_DES_cblock key = {
  21865. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  21866. };
  21867. DES_cblock iv = {
  21868. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  21869. };
  21870. DES_key_schedule sched;
  21871. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  21872. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  21873. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  21874. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  21875. };
  21876. DES_key_sched(&key, &sched);
  21877. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  21878. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  21879. if (XMEMCMP(plain, vector, sizeof(vector)) != 0)
  21880. return WC_TEST_RET_ENC_NC;
  21881. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  21882. return WC_TEST_RET_ENC_NC;
  21883. /* test changing iv */
  21884. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  21885. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  21886. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  21887. return WC_TEST_RET_ENC_NC;
  21888. } /* end des test */
  21889. #endif /* NO_DES3 */
  21890. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  21891. if ((ret = openssl_aes_test()) != 0) {
  21892. return ret;
  21893. }
  21894. #if defined(WOLFSSL_AES_128) && defined(HAVE_AES_CBC)
  21895. { /* evp_cipher test: EVP_aes_128_cbc */
  21896. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21897. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  21898. #else
  21899. EVP_CIPHER_CTX ctx[1];
  21900. #endif
  21901. int idx, cipherSz, plainSz;
  21902. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  21903. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  21904. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  21905. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  21906. };
  21907. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  21908. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  21909. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  21910. 0x3b,0x5d,0x41,0x97,0x94,0x25,0xa4,0xb4,
  21911. 0xae,0x7b,0x34,0xd0,0x3f,0x0c,0xbc,0x06
  21912. };
  21913. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  21914. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  21915. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  21916. 0x7d,0x37,0x7b,0x0b,0x44,0xaa,0xb5,0xf0,
  21917. 0x5f,0x34,0xb4,0xde,0xb5,0xbd,0x2a,0xbb
  21918. };
  21919. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  21920. "0123456789abcdef "; /* align */
  21921. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  21922. "1234567890abcdef "; /* align */
  21923. byte cipher[AES_BLOCK_SIZE * 4];
  21924. byte plain [AES_BLOCK_SIZE * 4];
  21925. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21926. if (ctx == NULL)
  21927. return MEMORY_E;
  21928. #endif
  21929. cipherSz = 0;
  21930. EVP_CIPHER_CTX_init(ctx);
  21931. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  21932. if (ret == WOLFSSL_SUCCESS) {
  21933. ret = EVP_CipherUpdate(ctx, cipher, &idx, (byte*)msg, sizeof(msg));
  21934. if (ret == WOLFSSL_SUCCESS)
  21935. cipherSz += idx;
  21936. }
  21937. if (ret == WOLFSSL_SUCCESS) {
  21938. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  21939. if (ret == WOLFSSL_SUCCESS)
  21940. cipherSz += idx;
  21941. }
  21942. EVP_CIPHER_CTX_cleanup(ctx);
  21943. if (ret != WOLFSSL_SUCCESS)
  21944. return WC_TEST_RET_ENC_NC;
  21945. if (cipherSz != (int)sizeof(verify) || XMEMCMP(cipher, verify, cipherSz))
  21946. return WC_TEST_RET_ENC_NC;
  21947. /* check partial decrypt (not enough padding for full block) */
  21948. plainSz = 0;
  21949. EVP_CIPHER_CTX_init(ctx);
  21950. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  21951. if (ret == WOLFSSL_SUCCESS) {
  21952. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, 1);
  21953. if (ret == WOLFSSL_SUCCESS)
  21954. plainSz += idx;
  21955. }
  21956. if (ret == WOLFSSL_SUCCESS) {
  21957. /* this test should fail... not enough padding for full block */
  21958. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  21959. if (plainSz == 0 && ret != WOLFSSL_SUCCESS)
  21960. ret = WOLFSSL_SUCCESS;
  21961. else
  21962. ret = WC_TEST_RET_ENC_NC;
  21963. }
  21964. else
  21965. ret = WC_TEST_RET_ENC_NC;
  21966. EVP_CIPHER_CTX_cleanup(ctx);
  21967. if (ret != WOLFSSL_SUCCESS)
  21968. return ret;
  21969. plainSz = 0;
  21970. EVP_CIPHER_CTX_init(ctx);
  21971. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  21972. if (ret == WOLFSSL_SUCCESS) {
  21973. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, cipherSz);
  21974. if (ret == WOLFSSL_SUCCESS)
  21975. plainSz += idx;
  21976. }
  21977. if (ret == WOLFSSL_SUCCESS) {
  21978. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  21979. if (ret == WOLFSSL_SUCCESS)
  21980. plainSz += idx;
  21981. }
  21982. EVP_CIPHER_CTX_cleanup(ctx);
  21983. if (ret != WOLFSSL_SUCCESS)
  21984. return WC_TEST_RET_ENC_NC;
  21985. if (plainSz != (int)sizeof(msg) || XMEMCMP(plain, msg, sizeof(msg)))
  21986. return WC_TEST_RET_ENC_NC;
  21987. cipherSz = 0;
  21988. EVP_CIPHER_CTX_init(ctx);
  21989. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  21990. if (ret == WOLFSSL_SUCCESS) {
  21991. ret = EVP_CipherUpdate(ctx, cipher, &idx, msg, AES_BLOCK_SIZE);
  21992. if (ret == WOLFSSL_SUCCESS)
  21993. cipherSz += idx;
  21994. }
  21995. if (ret == WOLFSSL_SUCCESS) {
  21996. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  21997. if (ret == WOLFSSL_SUCCESS)
  21998. cipherSz += idx;
  21999. }
  22000. EVP_CIPHER_CTX_cleanup(ctx);
  22001. if (ret != WOLFSSL_SUCCESS)
  22002. return WC_TEST_RET_ENC_NC;
  22003. if (cipherSz != (int)sizeof(verify2) || XMEMCMP(cipher, verify2, cipherSz))
  22004. return WC_TEST_RET_ENC_NC;
  22005. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22006. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  22007. #endif
  22008. } /* end evp_cipher test: EVP_aes_128_cbc*/
  22009. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  22010. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  22011. { /* evp_cipher test: EVP_aes_256_ecb*/
  22012. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22013. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  22014. #else
  22015. EVP_CIPHER_CTX ctx[1];
  22016. #endif
  22017. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  22018. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  22019. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  22020. };
  22021. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  22022. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  22023. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  22024. };
  22025. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  22026. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  22027. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  22028. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  22029. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  22030. };
  22031. byte cipher[AES_BLOCK_SIZE * 4];
  22032. byte plain [AES_BLOCK_SIZE * 4];
  22033. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22034. if (ctx == NULL)
  22035. return MEMORY_E;
  22036. #endif
  22037. EVP_CIPHER_CTX_init(ctx);
  22038. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1);
  22039. if (ret == WOLFSSL_SUCCESS)
  22040. ret = EVP_Cipher(ctx, cipher, (byte*)msg, 16);
  22041. EVP_CIPHER_CTX_cleanup(ctx);
  22042. if (ret != 16)
  22043. return WC_TEST_RET_ENC_NC;
  22044. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  22045. return WC_TEST_RET_ENC_NC;
  22046. EVP_CIPHER_CTX_init(ctx);
  22047. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0);
  22048. if (ret == WOLFSSL_SUCCESS)
  22049. ret = EVP_Cipher(ctx, plain, cipher, 16);
  22050. EVP_CIPHER_CTX_cleanup(ctx);
  22051. if (ret != 16)
  22052. return WC_TEST_RET_ENC_NC;
  22053. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  22054. return WC_TEST_RET_ENC_NC;
  22055. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22056. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  22057. #endif
  22058. } /* end evp_cipher test */
  22059. #endif /* HAVE_AES_ECB && WOLFSSL_AES_128 */
  22060. #ifndef WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API
  22061. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  22062. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  22063. {
  22064. /* Test: AES_encrypt/decrypt/set Key */
  22065. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22066. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  22067. #ifdef HAVE_AES_DECRYPT
  22068. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  22069. #endif
  22070. #else
  22071. AES_KEY enc[1];
  22072. #ifdef HAVE_AES_DECRYPT
  22073. AES_KEY dec[1];
  22074. #endif
  22075. #endif
  22076. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  22077. {
  22078. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  22079. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  22080. };
  22081. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  22082. {
  22083. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  22084. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  22085. };
  22086. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  22087. {
  22088. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  22089. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  22090. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  22091. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  22092. };
  22093. byte plain[sizeof(msg)];
  22094. byte cipher[sizeof(msg)];
  22095. printf("openSSL extra test\n") ;
  22096. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22097. if (enc == NULL)
  22098. return MEMORY_E;
  22099. #ifdef HAVE_AES_DECRYPT
  22100. if (dec == NULL)
  22101. return MEMORY_E;
  22102. #endif
  22103. #endif
  22104. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  22105. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  22106. AES_encrypt(msg, cipher, enc);
  22107. #ifdef HAVE_AES_DECRYPT
  22108. AES_decrypt(cipher, plain, dec);
  22109. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  22110. return WC_TEST_RET_ENC_NC;
  22111. #endif /* HAVE_AES_DECRYPT */
  22112. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  22113. return WC_TEST_RET_ENC_NC;
  22114. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22115. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  22116. #ifdef HAVE_AES_DECRYPT
  22117. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  22118. #endif
  22119. #endif
  22120. }
  22121. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  22122. #endif /* !WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API */
  22123. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  22124. #ifdef WOLFSSL_AES_COUNTER
  22125. {
  22126. byte plainBuff [64];
  22127. byte cipherBuff[64];
  22128. #ifdef WOLFSSL_AES_128
  22129. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  22130. {
  22131. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  22132. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  22133. };
  22134. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  22135. {
  22136. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  22137. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  22138. };
  22139. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  22140. {
  22141. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  22142. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  22143. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  22144. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  22145. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  22146. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  22147. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  22148. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  22149. };
  22150. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  22151. {
  22152. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  22153. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  22154. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  22155. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  22156. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  22157. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  22158. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  22159. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  22160. };
  22161. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  22162. {
  22163. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  22164. 0xc2
  22165. };
  22166. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22167. EVP_CIPHER_CTX *p_en;
  22168. EVP_CIPHER_CTX *p_de;
  22169. #endif
  22170. #endif /* WOLFSSL_AES_128 */
  22171. #ifdef WOLFSSL_AES_192
  22172. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  22173. * NIST Special Publication 800-38A */
  22174. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  22175. {
  22176. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  22177. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  22178. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  22179. };
  22180. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  22181. {
  22182. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  22183. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  22184. };
  22185. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  22186. {
  22187. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  22188. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  22189. };
  22190. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  22191. {
  22192. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  22193. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  22194. };
  22195. #endif /* WOLFSSL_AES_192 */
  22196. #ifdef WOLFSSL_AES_256
  22197. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  22198. * NIST Special Publication 800-38A */
  22199. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  22200. {
  22201. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  22202. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  22203. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  22204. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  22205. };
  22206. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  22207. {
  22208. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  22209. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  22210. };
  22211. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  22212. {
  22213. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  22214. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  22215. };
  22216. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  22217. {
  22218. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  22219. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  22220. };
  22221. #endif /* WOLFSSL_AES_256 */
  22222. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22223. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  22224. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  22225. if ((en == NULL) || (de == NULL))
  22226. return MEMORY_E;
  22227. #else
  22228. EVP_CIPHER_CTX en[1];
  22229. EVP_CIPHER_CTX de[1];
  22230. #endif
  22231. #ifdef WOLFSSL_AES_128
  22232. EVP_CIPHER_CTX_init(en);
  22233. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  22234. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  22235. return WC_TEST_RET_ENC_NC;
  22236. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  22237. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  22238. return WC_TEST_RET_ENC_NC;
  22239. EVP_CIPHER_CTX_init(de);
  22240. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  22241. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  22242. return WC_TEST_RET_ENC_NC;
  22243. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  22244. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  22245. return WC_TEST_RET_ENC_NC;
  22246. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  22247. return WC_TEST_RET_ENC_NC;
  22248. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  22249. return WC_TEST_RET_ENC_NC;
  22250. EVP_CIPHER_CTX_cleanup(en);
  22251. EVP_CIPHER_CTX_cleanup(de);
  22252. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22253. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  22254. if (p_en == NULL)
  22255. return WC_TEST_RET_ENC_ERRNO;
  22256. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  22257. if (p_de == NULL)
  22258. return WC_TEST_RET_ENC_ERRNO;
  22259. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  22260. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  22261. return WC_TEST_RET_ENC_NC;
  22262. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  22263. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  22264. return WC_TEST_RET_ENC_NC;
  22265. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  22266. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  22267. return WC_TEST_RET_ENC_NC;
  22268. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  22269. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  22270. return WC_TEST_RET_ENC_NC;
  22271. EVP_CIPHER_CTX_cleanup(p_en);
  22272. EVP_CIPHER_CTX_cleanup(p_de);
  22273. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  22274. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  22275. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  22276. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  22277. return WC_TEST_RET_ENC_NC;
  22278. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  22279. return WC_TEST_RET_ENC_NC;
  22280. EVP_CIPHER_CTX_init(en);
  22281. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  22282. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  22283. return WC_TEST_RET_ENC_NC;
  22284. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  22285. return WC_TEST_RET_ENC_NC;
  22286. EVP_CIPHER_CTX_init(de);
  22287. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  22288. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  22289. return WC_TEST_RET_ENC_NC;
  22290. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  22291. return WC_TEST_RET_ENC_NC;
  22292. if (XMEMCMP(plainBuff, ctrPlain, 9))
  22293. return WC_TEST_RET_ENC_NC;
  22294. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  22295. return WC_TEST_RET_ENC_NC;
  22296. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  22297. return WC_TEST_RET_ENC_NC;
  22298. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  22299. return WC_TEST_RET_ENC_NC;
  22300. if (XMEMCMP(plainBuff, ctrPlain, 9))
  22301. return WC_TEST_RET_ENC_NC;
  22302. if (XMEMCMP(cipherBuff, oddCipher, 9))
  22303. return WC_TEST_RET_ENC_NC;
  22304. EVP_CIPHER_CTX_cleanup(en);
  22305. EVP_CIPHER_CTX_cleanup(de);
  22306. #endif /* WOLFSSL_AES_128 */
  22307. #ifdef WOLFSSL_AES_192
  22308. EVP_CIPHER_CTX_init(en);
  22309. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  22310. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  22311. return WC_TEST_RET_ENC_NC;
  22312. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  22313. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  22314. return WC_TEST_RET_ENC_NC;
  22315. EVP_CIPHER_CTX_init(de);
  22316. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  22317. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  22318. return WC_TEST_RET_ENC_NC;
  22319. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  22320. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  22321. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  22322. return WC_TEST_RET_ENC_NC;
  22323. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  22324. return WC_TEST_RET_ENC_NC;
  22325. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  22326. return WC_TEST_RET_ENC_NC;
  22327. EVP_CIPHER_CTX_cleanup(en);
  22328. EVP_CIPHER_CTX_cleanup(de);
  22329. #endif /* WOLFSSL_AES_192 */
  22330. #ifdef WOLFSSL_AES_256
  22331. EVP_CIPHER_CTX_init(en);
  22332. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  22333. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  22334. return WC_TEST_RET_ENC_NC;
  22335. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  22336. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  22337. return WC_TEST_RET_ENC_NC;
  22338. EVP_CIPHER_CTX_init(de);
  22339. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  22340. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  22341. return WC_TEST_RET_ENC_NC;
  22342. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  22343. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  22344. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  22345. return WC_TEST_RET_ENC_NC;
  22346. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  22347. return WC_TEST_RET_ENC_NC;
  22348. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  22349. return WC_TEST_RET_ENC_NC;
  22350. EVP_CIPHER_CTX_cleanup(en);
  22351. EVP_CIPHER_CTX_cleanup(de);
  22352. #endif /* WOLFSSL_AES_256 */
  22353. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22354. wolfSSL_EVP_CIPHER_CTX_free(en);
  22355. wolfSSL_EVP_CIPHER_CTX_free(de);
  22356. #endif
  22357. }
  22358. #endif /* HAVE_AES_COUNTER */
  22359. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  22360. {
  22361. /* EVP_CipherUpdate test */
  22362. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  22363. {
  22364. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  22365. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  22366. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  22367. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  22368. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  22369. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  22370. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  22371. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  22372. };
  22373. byte key[] = "0123456789abcdef "; /* align */
  22374. byte iv[] = "1234567890abcdef "; /* align */
  22375. byte cipher[AES_BLOCK_SIZE * 4];
  22376. byte plain [AES_BLOCK_SIZE * 4];
  22377. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22378. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  22379. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  22380. #else
  22381. EVP_CIPHER_CTX en[1];
  22382. EVP_CIPHER_CTX de[1];
  22383. #endif
  22384. int outlen ;
  22385. int total = 0;
  22386. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22387. if ((en == NULL) || (de == NULL))
  22388. return MEMORY_E;
  22389. #endif
  22390. EVP_CIPHER_CTX_init(en);
  22391. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  22392. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  22393. return WC_TEST_RET_ENC_NC;
  22394. /* openSSL compatibility, if(inlen == 0)return 1; */
  22395. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  22396. (byte*)cbcPlain, 0) != 1)
  22397. return WC_TEST_RET_ENC_NC;
  22398. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  22399. return WC_TEST_RET_ENC_NC;
  22400. EVP_CIPHER_CTX_init(en);
  22401. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  22402. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  22403. return WC_TEST_RET_ENC_NC;
  22404. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  22405. (byte*)cbcPlain, 9) == 0)
  22406. return WC_TEST_RET_ENC_NC;
  22407. if(outlen != 0)
  22408. return WC_TEST_RET_ENC_NC;
  22409. total += outlen;
  22410. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  22411. (byte*)&cbcPlain[9] , 9) == 0)
  22412. return WC_TEST_RET_ENC_NC;
  22413. if(outlen != 16)
  22414. return WC_TEST_RET_ENC_NC;
  22415. total += outlen;
  22416. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  22417. return WC_TEST_RET_ENC_NC;
  22418. if(outlen != 16)
  22419. return WC_TEST_RET_ENC_NC;
  22420. total += outlen;
  22421. if(total != 32)
  22422. return WC_TEST_RET_ENC_NC;
  22423. total = 0;
  22424. EVP_CIPHER_CTX_init(de);
  22425. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  22426. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  22427. return WC_TEST_RET_ENC_NC;
  22428. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  22429. return WC_TEST_RET_ENC_NC;
  22430. if(outlen != 0)
  22431. return WC_TEST_RET_ENC_NC;
  22432. total += outlen;
  22433. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  22434. (byte*)&cipher[6], 12) == 0)
  22435. return WC_TEST_RET_ENC_NC;
  22436. if(outlen != 0)
  22437. total += outlen;
  22438. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  22439. (byte*)&cipher[6+12], 14) == 0)
  22440. return WC_TEST_RET_ENC_NC;
  22441. if(outlen != 16)
  22442. return WC_TEST_RET_ENC_NC;
  22443. total += outlen;
  22444. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  22445. return WC_TEST_RET_ENC_NC;
  22446. if(outlen != 2)
  22447. return WC_TEST_RET_ENC_NC;
  22448. total += outlen;
  22449. if(total != 18)
  22450. return WC_TEST_RET_ENC_NC;
  22451. if (XMEMCMP(plain, cbcPlain, 18))
  22452. return WC_TEST_RET_ENC_NC;
  22453. total = 0;
  22454. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  22455. return WC_TEST_RET_ENC_NC;
  22456. EVP_CIPHER_CTX_init(en);
  22457. if (EVP_EncryptInit(en, EVP_aes_128_cbc(),
  22458. (unsigned char*)key, (unsigned char*)iv) == 0)
  22459. return WC_TEST_RET_ENC_NC;
  22460. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen, (byte*)cbcPlain, 9) == 0)
  22461. return WC_TEST_RET_ENC_NC;
  22462. if(outlen != 0)
  22463. return WC_TEST_RET_ENC_NC;
  22464. total += outlen;
  22465. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen, (byte*)&cbcPlain[9] , 9) == 0)
  22466. return WC_TEST_RET_ENC_NC;
  22467. if(outlen != 16)
  22468. return WC_TEST_RET_ENC_NC;
  22469. total += outlen;
  22470. if (EVP_EncryptFinal(en, (byte*)&cipher[total], &outlen) == 0)
  22471. return WC_TEST_RET_ENC_NC;
  22472. if(outlen != 16)
  22473. return WC_TEST_RET_ENC_NC;
  22474. total += outlen;
  22475. if(total != 32)
  22476. return 3438;
  22477. total = 0;
  22478. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  22479. return WC_TEST_RET_ENC_NC;
  22480. EVP_CIPHER_CTX_init(de);
  22481. if (EVP_DecryptInit(de, EVP_aes_128_cbc(),
  22482. (unsigned char*)key, (unsigned char*)iv) == 0)
  22483. return WC_TEST_RET_ENC_NC;
  22484. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  22485. return WC_TEST_RET_ENC_NC;
  22486. if(outlen != 0)
  22487. return WC_TEST_RET_ENC_NC;
  22488. total += outlen;
  22489. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6], 12) == 0)
  22490. return WC_TEST_RET_ENC_NC;
  22491. if(outlen != 0)
  22492. total += outlen;
  22493. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6+12], 14) == 0)
  22494. return WC_TEST_RET_ENC_NC;
  22495. if(outlen != 16)
  22496. return WC_TEST_RET_ENC_NC;
  22497. total += outlen;
  22498. if (EVP_DecryptFinal(de, (byte*)&plain[total], &outlen) == 0)
  22499. return WC_TEST_RET_ENC_NC;
  22500. if(outlen != 2)
  22501. return WC_TEST_RET_ENC_NC;
  22502. total += outlen;
  22503. if(total != 18)
  22504. return 3447;
  22505. if (XMEMCMP(plain, cbcPlain, 18))
  22506. return WC_TEST_RET_ENC_NC;
  22507. if (EVP_CIPHER_key_length(NULL) != 0)
  22508. return WC_TEST_RET_ENC_NC;
  22509. if (EVP_CIPHER_key_length(EVP_aes_128_cbc()) != 16)
  22510. return WC_TEST_RET_ENC_NC;
  22511. if (EVP_CIPHER_CTX_mode(NULL) != 0)
  22512. return WC_TEST_RET_ENC_NC;
  22513. if (EVP_CIPHER_CTX_mode(en) != (en->flags & WOLFSSL_EVP_CIPH_MODE))
  22514. return WC_TEST_RET_ENC_NC;
  22515. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  22516. return WC_TEST_RET_ENC_NC;
  22517. EVP_CIPHER_CTX_init(en);
  22518. if (EVP_CipherInit_ex(en, EVP_aes_128_cbc(), NULL,
  22519. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  22520. return WC_TEST_RET_ENC_NC;
  22521. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  22522. return WC_TEST_RET_ENC_NC;
  22523. EVP_CIPHER_CTX_init(en);
  22524. if (EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  22525. (unsigned char*)key, (unsigned char*)iv) == 0)
  22526. return WC_TEST_RET_ENC_NC;
  22527. if (wolfSSL_EVP_EncryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  22528. return WC_TEST_RET_ENC_NC;
  22529. if (wolfSSL_EVP_EncryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  22530. return WC_TEST_RET_ENC_NC;
  22531. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  22532. return WC_TEST_RET_ENC_NC;
  22533. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  22534. return WC_TEST_RET_ENC_NC;
  22535. EVP_CIPHER_CTX_init(de);
  22536. if (EVP_DecryptInit_ex(de, EVP_aes_128_cbc(), NULL,
  22537. (unsigned char*)key, (unsigned char*)iv) == 0)
  22538. return WC_TEST_RET_ENC_NC;
  22539. if (wolfSSL_EVP_DecryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  22540. return WC_TEST_RET_ENC_NC;
  22541. if (wolfSSL_EVP_DecryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  22542. return WC_TEST_RET_ENC_NC;
  22543. if (EVP_CIPHER_CTX_block_size(NULL) != BAD_FUNC_ARG)
  22544. return WC_TEST_RET_ENC_NC;
  22545. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  22546. return WC_TEST_RET_ENC_NC;
  22547. EVP_CIPHER_CTX_init(en);
  22548. EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  22549. (unsigned char*)key, (unsigned char*)iv);
  22550. if (EVP_CIPHER_CTX_block_size(en) != en->block_size)
  22551. return WC_TEST_RET_ENC_NC;
  22552. if (EVP_CIPHER_block_size(NULL) != BAD_FUNC_ARG)
  22553. return WC_TEST_RET_ENC_NC;
  22554. if (EVP_CIPHER_block_size(EVP_aes_128_cbc()) != AES_BLOCK_SIZE)
  22555. return WC_TEST_RET_ENC_NC;
  22556. if (WOLFSSL_EVP_CIPHER_mode(NULL) != 0)
  22557. return WC_TEST_RET_ENC_NC;
  22558. if (EVP_CIPHER_flags(EVP_aes_128_cbc()) != WOLFSSL_EVP_CIPH_CBC_MODE)
  22559. return WC_TEST_RET_ENC_NC;
  22560. EVP_CIPHER_CTX_clear_flags(en, 0xFFFFFFFF);
  22561. EVP_CIPHER_CTX_set_flags(en, 42);
  22562. if (en->flags != 42)
  22563. return WC_TEST_RET_ENC_NC;
  22564. if (EVP_CIPHER_CTX_set_padding(NULL, 0) != BAD_FUNC_ARG)
  22565. return WC_TEST_RET_ENC_NC;
  22566. if (EVP_CIPHER_CTX_set_padding(en, 0) != WOLFSSL_SUCCESS)
  22567. return WC_TEST_RET_ENC_NC;
  22568. if (EVP_CIPHER_CTX_set_padding(en, 1) != WOLFSSL_SUCCESS)
  22569. return WC_TEST_RET_ENC_NC;
  22570. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  22571. return WC_TEST_RET_ENC_NC;
  22572. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  22573. return WC_TEST_RET_ENC_NC;
  22574. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22575. wolfSSL_EVP_CIPHER_CTX_free(en);
  22576. wolfSSL_EVP_CIPHER_CTX_free(de);
  22577. #endif
  22578. }
  22579. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  22580. #endif /* ifndef NO_AES */
  22581. return 0;
  22582. }
  22583. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openSSL_evpMD_test(void)
  22584. {
  22585. wc_test_ret_t ret = 0;
  22586. #if !defined(NO_SHA256) && !defined(NO_SHA)
  22587. WOLFSSL_EVP_MD_CTX* ctx;
  22588. WOLFSSL_EVP_MD_CTX* ctx2;
  22589. WOLFSSL_ENTER("openSSL_evpMD_test");
  22590. ctx = EVP_MD_CTX_create();
  22591. ctx2 = EVP_MD_CTX_create();
  22592. ret = EVP_DigestInit(ctx, EVP_sha256());
  22593. if (ret != SSL_SUCCESS) {
  22594. ret = WC_TEST_RET_ENC_NC;
  22595. goto openSSL_evpMD_test_done;
  22596. }
  22597. ret = EVP_MD_CTX_copy(ctx2, ctx);
  22598. if (ret != SSL_SUCCESS) {
  22599. ret = WC_TEST_RET_ENC_NC;
  22600. goto openSSL_evpMD_test_done;
  22601. }
  22602. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  22603. ret = WC_TEST_RET_ENC_NC;
  22604. goto openSSL_evpMD_test_done;
  22605. }
  22606. ret = EVP_DigestInit(ctx, EVP_sha1());
  22607. if (ret != SSL_SUCCESS) {
  22608. ret = WC_TEST_RET_ENC_NC;
  22609. goto openSSL_evpMD_test_done;
  22610. }
  22611. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  22612. ret = WC_TEST_RET_ENC_NC;
  22613. goto openSSL_evpMD_test_done;
  22614. }
  22615. ret = EVP_MD_CTX_copy_ex(ctx2, ctx);
  22616. if (ret != SSL_SUCCESS) {
  22617. ret = WC_TEST_RET_ENC_NC;
  22618. goto openSSL_evpMD_test_done;
  22619. }
  22620. if (EVP_MD_type(EVP_sha256()) == EVP_MD_CTX_type(ctx2)) {
  22621. ret = WC_TEST_RET_ENC_NC;
  22622. goto openSSL_evpMD_test_done;
  22623. }
  22624. if (EVP_MD_type(EVP_sha1()) != EVP_MD_CTX_type(ctx2)) {
  22625. ret = WC_TEST_RET_ENC_NC;
  22626. goto openSSL_evpMD_test_done;
  22627. }
  22628. if (EVP_DigestInit_ex(ctx, EVP_sha1(), NULL) != SSL_SUCCESS) {
  22629. ret = WC_TEST_RET_ENC_NC;
  22630. goto openSSL_evpMD_test_done;
  22631. }
  22632. if (EVP_add_digest(NULL) != 0) {
  22633. ret = WC_TEST_RET_ENC_NC;
  22634. goto openSSL_evpMD_test_done;
  22635. }
  22636. if (wolfSSL_EVP_add_cipher(NULL) != 0) {
  22637. ret = WC_TEST_RET_ENC_NC;
  22638. goto openSSL_evpMD_test_done;
  22639. }
  22640. ret = 0; /* got to success state without jumping to end with a fail */
  22641. openSSL_evpMD_test_done:
  22642. EVP_MD_CTX_destroy(ctx);
  22643. EVP_MD_CTX_destroy(ctx2);
  22644. #endif /* NO_SHA256 */
  22645. return ret;
  22646. }
  22647. #ifdef DEBUG_SIGN
  22648. static void show(const char *title, const char *p, unsigned int s) {
  22649. char* i;
  22650. printf("%s: ", title);
  22651. for (i = p;
  22652. i < p + s;
  22653. printf("%c", *i), i++);
  22654. printf("\n");
  22655. }
  22656. #else
  22657. #define show(a,b,c) WC_DO_NOTHING
  22658. #endif
  22659. #define FOURK_BUFF 4096
  22660. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey0_test(void)
  22661. {
  22662. wc_test_ret_t ret = 0;
  22663. #if !defined(NO_RSA) && !defined(NO_SHA)
  22664. byte* prvTmp;
  22665. byte* pubTmp;
  22666. int prvBytes;
  22667. int pubBytes;
  22668. RSA *prvRsa = NULL;
  22669. RSA *pubRsa = NULL;
  22670. EVP_PKEY *prvPkey = NULL;
  22671. EVP_PKEY *pubPkey = NULL;
  22672. EVP_PKEY_CTX *enc = NULL;
  22673. EVP_PKEY_CTX *dec = NULL;
  22674. byte in[] = TEST_STRING;
  22675. byte out[256];
  22676. size_t outlen;
  22677. size_t keySz;
  22678. byte plain[256];
  22679. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  22680. XFILE keyFile;
  22681. XFILE keypubFile;
  22682. char cliKey[] = "./certs/client-key.der";
  22683. char cliKeypub[] = "./certs/client-keyPub.der";
  22684. #endif
  22685. WOLFSSL_ENTER("openssl_pkey0_test");
  22686. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22687. if (prvTmp == NULL)
  22688. return WC_TEST_RET_ENC_ERRNO;
  22689. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22690. if (pubTmp == NULL) {
  22691. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22692. return WC_TEST_RET_ENC_NC;
  22693. }
  22694. #ifdef USE_CERT_BUFFERS_1024
  22695. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  22696. prvBytes = sizeof_client_key_der_1024;
  22697. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  22698. pubBytes = sizeof_client_keypub_der_1024;
  22699. #elif defined(USE_CERT_BUFFERS_2048)
  22700. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  22701. prvBytes = sizeof_client_key_der_2048;
  22702. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  22703. pubBytes = sizeof_client_keypub_der_2048;
  22704. #else
  22705. keyFile = XFOPEN(cliKey, "rb");
  22706. if (!keyFile) {
  22707. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22708. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22709. err_sys("can't open ./certs/client-key.der, "
  22710. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  22711. return WC_TEST_RET_ENC_ERRNO;
  22712. }
  22713. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  22714. XFCLOSE(keyFile);
  22715. if (prvBytes == 0) {
  22716. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22717. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22718. return WC_TEST_RET_ENC_ERRNO;
  22719. }
  22720. keypubFile = XFOPEN(cliKeypub, "rb");
  22721. if (!keypubFile) {
  22722. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22723. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22724. err_sys("can't open ./certs/client-cert.der, "
  22725. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  22726. return WC_TEST_RET_ENC_ERRNO;
  22727. }
  22728. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  22729. XFCLOSE(keypubFile);
  22730. if (pubBytes == 0) {
  22731. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22732. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22733. return WC_TEST_RET_ENC_ERRNO;
  22734. }
  22735. #endif /* USE_CERT_BUFFERS */
  22736. prvRsa = wolfSSL_RSA_new();
  22737. pubRsa = wolfSSL_RSA_new();
  22738. if((prvRsa == NULL) || (pubRsa == NULL)){
  22739. printf("error with RSA_new\n");
  22740. ret = WC_TEST_RET_ENC_ERRNO;
  22741. goto openssl_pkey0_test_done;
  22742. }
  22743. ret = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  22744. if(ret != SSL_SUCCESS){
  22745. printf("error with RSA_LoadDer_ex\n");
  22746. ret = WC_TEST_RET_ENC_EC(ret);
  22747. goto openssl_pkey0_test_done;
  22748. }
  22749. ret = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  22750. if(ret != SSL_SUCCESS){
  22751. printf("error with RSA_LoadDer_ex\n");
  22752. ret = WC_TEST_RET_ENC_EC(ret);
  22753. goto openssl_pkey0_test_done;
  22754. }
  22755. keySz = (size_t)RSA_size(pubRsa);
  22756. prvPkey = wolfSSL_EVP_PKEY_new();
  22757. pubPkey = wolfSSL_EVP_PKEY_new();
  22758. if((prvPkey == NULL) || (pubPkey == NULL)){
  22759. printf("error with PKEY_new\n");
  22760. ret = WC_TEST_RET_ENC_NC;
  22761. goto openssl_pkey0_test_done;
  22762. }
  22763. ret = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  22764. ret += wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  22765. if(ret != 2){
  22766. printf("error with PKEY_set1_RSA\n");
  22767. ret = WC_TEST_RET_ENC_I(ret);
  22768. goto openssl_pkey0_test_done;
  22769. }
  22770. dec = EVP_PKEY_CTX_new(prvPkey, NULL);
  22771. enc = EVP_PKEY_CTX_new(pubPkey, NULL);
  22772. if((dec == NULL)||(enc==NULL)){
  22773. printf("error with EVP_PKEY_CTX_new\n");
  22774. ret = WC_TEST_RET_ENC_NC;
  22775. goto openssl_pkey0_test_done;
  22776. }
  22777. ret = EVP_PKEY_decrypt_init(dec);
  22778. if (ret != 1) {
  22779. printf("error with decrypt init\n");
  22780. ret = WC_TEST_RET_ENC_NC;
  22781. goto openssl_pkey0_test_done;
  22782. }
  22783. ret = EVP_PKEY_encrypt_init(enc);
  22784. if (ret != 1) {
  22785. printf("error with encrypt init\n");
  22786. ret = WC_TEST_RET_ENC_NC;
  22787. goto openssl_pkey0_test_done;
  22788. }
  22789. XMEMSET(out, 0, sizeof(out));
  22790. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  22791. if (ret != 1) {
  22792. printf("error encrypting msg\n");
  22793. ret = WC_TEST_RET_ENC_NC;
  22794. goto openssl_pkey0_test_done;
  22795. }
  22796. show("encrypted msg", out, outlen);
  22797. XMEMSET(plain, 0, sizeof(plain));
  22798. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  22799. if (ret != 1) {
  22800. printf("error decrypting msg\n");
  22801. ret = WC_TEST_RET_ENC_NC;
  22802. goto openssl_pkey0_test_done;
  22803. }
  22804. show("decrypted msg", plain, outlen);
  22805. /* RSA_PKCS1_OAEP_PADDING test */
  22806. ret = EVP_PKEY_decrypt_init(dec);
  22807. if (ret != 1) {
  22808. printf("error with decrypt init\n");
  22809. ret = WC_TEST_RET_ENC_NC;
  22810. goto openssl_pkey0_test_done;
  22811. }
  22812. ret = EVP_PKEY_encrypt_init(enc);
  22813. if (ret != 1) {
  22814. printf("error with encrypt init\n");
  22815. ret = WC_TEST_RET_ENC_NC;
  22816. goto openssl_pkey0_test_done;
  22817. }
  22818. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  22819. printf("first set rsa padding error\n");
  22820. ret = WC_TEST_RET_ENC_EC(ret);
  22821. goto openssl_pkey0_test_done;
  22822. }
  22823. #ifndef HAVE_FIPS
  22824. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  22825. printf("second set rsa padding error\n");
  22826. ret = WC_TEST_RET_ENC_EC(ret);
  22827. goto openssl_pkey0_test_done;
  22828. }
  22829. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  22830. printf("third set rsa padding error\n");
  22831. ret = WC_TEST_RET_ENC_EC(ret);
  22832. goto openssl_pkey0_test_done;
  22833. }
  22834. #endif
  22835. XMEMSET(out, 0, sizeof(out));
  22836. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  22837. if (ret != 1) {
  22838. printf("error encrypting msg\n");
  22839. ret = WC_TEST_RET_ENC_NC;
  22840. goto openssl_pkey0_test_done;
  22841. }
  22842. show("encrypted msg", out, outlen);
  22843. XMEMSET(plain, 0, sizeof(plain));
  22844. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  22845. if (ret != 1) {
  22846. printf("error decrypting msg\n");
  22847. ret = WC_TEST_RET_ENC_NC;
  22848. goto openssl_pkey0_test_done;
  22849. }
  22850. show("decrypted msg", plain, outlen);
  22851. ret = 0; /* made it to this point without error then set success */
  22852. openssl_pkey0_test_done:
  22853. wolfSSL_RSA_free(prvRsa);
  22854. wolfSSL_RSA_free(pubRsa);
  22855. EVP_PKEY_free(pubPkey);
  22856. EVP_PKEY_free(prvPkey);
  22857. EVP_PKEY_CTX_free(dec);
  22858. EVP_PKEY_CTX_free(enc);
  22859. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22860. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22861. #endif /* NO_RSA */
  22862. return ret;
  22863. }
  22864. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey1_test(void)
  22865. {
  22866. wc_test_ret_t ret = 0;
  22867. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_SHA)
  22868. EVP_PKEY_CTX* dec = NULL;
  22869. EVP_PKEY_CTX* enc = NULL;
  22870. EVP_PKEY* pubKey = NULL;
  22871. EVP_PKEY* prvKey = NULL;
  22872. X509* x509 = NULL;
  22873. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sugar slapped";
  22874. const unsigned char* clikey;
  22875. long cliKeySz;
  22876. size_t outlen;
  22877. int keyLenBits = 2048;
  22878. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22879. unsigned char *tmp = (unsigned char *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22880. unsigned char *cipher = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22881. unsigned char *plain = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22882. if ((tmp == NULL) ||
  22883. (cipher == NULL) ||
  22884. (plain == NULL)) {
  22885. ret = WC_TEST_RET_ENC_NC;
  22886. goto openssl_pkey1_test_done;
  22887. }
  22888. #else
  22889. unsigned char tmp[FOURK_BUF];
  22890. unsigned char cipher[RSA_TEST_BYTES];
  22891. unsigned char plain[RSA_TEST_BYTES];
  22892. #endif
  22893. WOLFSSL_ENTER("openssl_pkey1_test");
  22894. #if defined(USE_CERT_BUFFERS_1024)
  22895. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  22896. cliKeySz = (long)sizeof_client_key_der_1024;
  22897. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_1024,
  22898. sizeof_client_cert_der_1024, SSL_FILETYPE_ASN1);
  22899. keyLenBits = 1024;
  22900. #elif defined(USE_CERT_BUFFERS_2048)
  22901. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  22902. cliKeySz = (long)sizeof_client_key_der_2048;
  22903. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_2048,
  22904. sizeof_client_cert_der_2048, SSL_FILETYPE_ASN1);
  22905. #elif defined(USE_CERT_BUFFERS_3072)
  22906. XMEMCPY(tmp, client_key_der_3072, sizeof_client_key_der_3072);
  22907. cliKeySz = (long)sizeof_client_key_der_3072;
  22908. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_3072,
  22909. sizeof_client_cert_der_3072, SSL_FILETYPE_ASN1);
  22910. keyLenBits = 3072;
  22911. #elif defined(USE_CERT_BUFFERS_4096)
  22912. XMEMCPY(tmp, client_key_der_4096, sizeof_client_key_der_4096);
  22913. cliKeySz = (long)sizeof_client_key_der_4096;
  22914. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_4096,
  22915. sizeof_client_cert_der_4096, SSL_FILETYPE_ASN1);
  22916. keyLenBits = 4096;
  22917. #else
  22918. {
  22919. XFILE f;
  22920. f = XFOPEN(clientKey, "rb");
  22921. if (!f) {
  22922. err_sys("can't open ./certs/client-key.der, "
  22923. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  22924. ret = WC_TEST_RET_ENC_ERRNO;
  22925. goto openssl_pkey1_test_done;
  22926. }
  22927. cliKeySz = (long)XFREAD(tmp, 1, FOURK_BUF, f);
  22928. XFCLOSE(f);
  22929. if (cliKeySz == 0)
  22930. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, openssl_pkey1_test_done);
  22931. }
  22932. /* using existing wolfSSL api to get public and private key */
  22933. x509 = wolfSSL_X509_load_certificate_file(clientCert, SSL_FILETYPE_ASN1);
  22934. #endif /* USE_CERT_BUFFERS */
  22935. clikey = tmp;
  22936. if ((prvKey = EVP_PKEY_new()) == NULL) {
  22937. ret = WC_TEST_RET_ENC_ERRNO;
  22938. goto openssl_pkey1_test_done;
  22939. }
  22940. EVP_PKEY_free(prvKey);
  22941. prvKey = NULL;
  22942. if (x509 == NULL) {
  22943. ret = WC_TEST_RET_ENC_NC;
  22944. goto openssl_pkey1_test_done;
  22945. }
  22946. pubKey = X509_get_pubkey(x509);
  22947. if (pubKey == NULL) {
  22948. ret = WC_TEST_RET_ENC_NC;
  22949. goto openssl_pkey1_test_done;
  22950. }
  22951. prvKey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &clikey, cliKeySz);
  22952. if (prvKey == NULL) {
  22953. ret = WC_TEST_RET_ENC_NC;
  22954. goto openssl_pkey1_test_done;
  22955. }
  22956. /* phase 2 API to create EVP_PKEY_CTX and encrypt/decrypt */
  22957. if (EVP_PKEY_bits(prvKey) != keyLenBits) {
  22958. ret = WC_TEST_RET_ENC_NC;
  22959. goto openssl_pkey1_test_done;
  22960. }
  22961. if (EVP_PKEY_size(prvKey) != keyLenBits/8) {
  22962. ret = WC_TEST_RET_ENC_NC;
  22963. goto openssl_pkey1_test_done;
  22964. }
  22965. dec = EVP_PKEY_CTX_new(prvKey, NULL);
  22966. enc = EVP_PKEY_CTX_new(pubKey, NULL);
  22967. if (dec == NULL || enc == NULL) {
  22968. ret = WC_TEST_RET_ENC_ERRNO;
  22969. goto openssl_pkey1_test_done;
  22970. }
  22971. if (EVP_PKEY_decrypt_init(dec) != 1) {
  22972. ret = WC_TEST_RET_ENC_NC;
  22973. goto openssl_pkey1_test_done;
  22974. }
  22975. if (EVP_PKEY_encrypt_init(enc) != 1) {
  22976. ret = WC_TEST_RET_ENC_NC;
  22977. goto openssl_pkey1_test_done;
  22978. }
  22979. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  22980. ret = WC_TEST_RET_ENC_NC;
  22981. goto openssl_pkey1_test_done;
  22982. }
  22983. #ifndef HAVE_FIPS
  22984. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  22985. ret = WC_TEST_RET_ENC_EC(ret);
  22986. goto openssl_pkey1_test_done;
  22987. }
  22988. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  22989. ret = WC_TEST_RET_ENC_EC(ret);
  22990. goto openssl_pkey1_test_done;
  22991. }
  22992. #endif
  22993. XMEMSET(cipher, 0, RSA_TEST_BYTES);
  22994. outlen = (size_t)(keyLenBits/8);
  22995. if (EVP_PKEY_encrypt(enc, cipher, &outlen, msg, sizeof(msg)) < 0) {
  22996. ret = WC_TEST_RET_ENC_EC(ret);
  22997. goto openssl_pkey1_test_done;
  22998. }
  22999. XMEMSET(plain, 0, RSA_TEST_BYTES);
  23000. if (EVP_PKEY_decrypt(dec, plain, &outlen, cipher, outlen) != 1) {
  23001. ret = WC_TEST_RET_ENC_NC;
  23002. goto openssl_pkey1_test_done;
  23003. }
  23004. openssl_pkey1_test_done:
  23005. if (pubKey != NULL) {
  23006. EVP_PKEY_free(pubKey);
  23007. }
  23008. if (prvKey != NULL) {
  23009. EVP_PKEY_free(prvKey);
  23010. }
  23011. if (dec != NULL) {
  23012. EVP_PKEY_CTX_free(dec);
  23013. }
  23014. if (enc != NULL) {
  23015. EVP_PKEY_CTX_free(enc);
  23016. }
  23017. if (x509 != NULL) {
  23018. X509_free(x509);
  23019. }
  23020. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23021. if (tmp != NULL)
  23022. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23023. if (cipher != NULL)
  23024. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23025. if (plain != NULL)
  23026. XFREE(plain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23027. #endif
  23028. #endif
  23029. return ret;
  23030. }
  23031. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_evpSig_test(void)
  23032. {
  23033. #if !defined(NO_RSA) && !defined(NO_SHA)
  23034. byte* prvTmp;
  23035. byte* pubTmp;
  23036. int prvBytes;
  23037. int pubBytes;
  23038. RSA *prvRsa;
  23039. RSA *pubRsa;
  23040. EVP_PKEY *prvPkey;
  23041. EVP_PKEY *pubPkey;
  23042. EVP_MD_CTX* sign;
  23043. EVP_MD_CTX* verf;
  23044. char msg[] = "see spot run";
  23045. unsigned char sig[256];
  23046. unsigned int sigSz;
  23047. const void* pt;
  23048. unsigned int count;
  23049. wc_test_ret_t ret, ret1, ret2;
  23050. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  23051. XFILE keyFile;
  23052. XFILE keypubFile;
  23053. char cliKey[] = "./certs/client-key.der";
  23054. char cliKeypub[] = "./certs/client-keyPub.der";
  23055. #endif
  23056. WOLFSSL_ENTER("openssl_evpSig_test");
  23057. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23058. if (prvTmp == NULL)
  23059. return WC_TEST_RET_ENC_ERRNO;
  23060. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23061. if (pubTmp == NULL) {
  23062. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23063. return WC_TEST_RET_ENC_NC;
  23064. }
  23065. #ifdef USE_CERT_BUFFERS_1024
  23066. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  23067. prvBytes = sizeof_client_key_der_1024;
  23068. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  23069. pubBytes = sizeof_client_keypub_der_1024;
  23070. #elif defined(USE_CERT_BUFFERS_2048)
  23071. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  23072. prvBytes = sizeof_client_key_der_2048;
  23073. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  23074. pubBytes = sizeof_client_keypub_der_2048;
  23075. #else
  23076. keyFile = XFOPEN(cliKey, "rb");
  23077. if (!keyFile) {
  23078. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23079. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23080. err_sys("can't open ./certs/client-key.der, "
  23081. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  23082. return WC_TEST_RET_ENC_ERRNO;
  23083. }
  23084. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  23085. XFCLOSE(keyFile);
  23086. if (prvBytes == 0) {
  23087. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23088. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23089. return WC_TEST_RET_ENC_ERRNO;
  23090. }
  23091. keypubFile = XFOPEN(cliKeypub, "rb");
  23092. if (!keypubFile) {
  23093. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23094. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23095. err_sys("can't open ./certs/client-cert.der, "
  23096. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  23097. return WC_TEST_RET_ENC_ERRNO;
  23098. }
  23099. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  23100. XFCLOSE(keypubFile);
  23101. if (pubBytes == 0) {
  23102. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23103. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23104. return WC_TEST_RET_ENC_ERRNO;
  23105. }
  23106. #endif /* USE_CERT_BUFFERS */
  23107. prvRsa = wolfSSL_RSA_new();
  23108. pubRsa = wolfSSL_RSA_new();
  23109. if((prvRsa == NULL) || (pubRsa == NULL)){
  23110. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23111. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23112. err_sys("ERROR with RSA_new", WC_TEST_RET_ENC_NC);
  23113. return WC_TEST_RET_ENC_NC;
  23114. }
  23115. ret1 = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  23116. ret2 = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  23117. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  23118. printf("error with RSA_LoadDer_ex\n");
  23119. return WC_TEST_RET_ENC_NC;
  23120. }
  23121. prvPkey = wolfSSL_EVP_PKEY_new();
  23122. pubPkey = wolfSSL_EVP_PKEY_new();
  23123. if((prvPkey == NULL) || (pubPkey == NULL)){
  23124. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23125. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23126. printf("error with KEY_new\n");
  23127. return WC_TEST_RET_ENC_NC;
  23128. }
  23129. ret1 = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  23130. ret2 = wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  23131. if((ret1 != 1) || (ret2 != 1)){
  23132. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23133. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23134. printf("error with EVP_PKEY_set1_RSA\n");
  23135. return WC_TEST_RET_ENC_NC;
  23136. }
  23137. /****************** sign and verify *******************/
  23138. sign = EVP_MD_CTX_create();
  23139. verf = EVP_MD_CTX_create();
  23140. if((sign == NULL)||(verf == NULL)){
  23141. printf("error with EVP_MD_CTX_create\n");
  23142. EVP_MD_CTX_destroy(sign);
  23143. EVP_MD_CTX_destroy(verf);
  23144. return WC_TEST_RET_ENC_NC;
  23145. }
  23146. ret = EVP_SignInit(sign, EVP_sha1());
  23147. if (ret != SSL_SUCCESS){
  23148. printf("error with EVP_SignInit\n");
  23149. EVP_MD_CTX_destroy(sign);
  23150. EVP_MD_CTX_destroy(verf);
  23151. return WC_TEST_RET_ENC_NC;
  23152. }
  23153. count = sizeof(msg);
  23154. show("message = ", (char *)msg, count);
  23155. /* sign */
  23156. XMEMSET(sig, 0, sizeof(sig));
  23157. pt = (const void*)msg;
  23158. ret1 = EVP_SignUpdate(sign, pt, count);
  23159. ret2 = EVP_SignFinal(sign, sig, &sigSz, prvPkey);
  23160. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  23161. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23162. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23163. EVP_MD_CTX_destroy(sign);
  23164. EVP_MD_CTX_destroy(verf);
  23165. printf("error with EVP_MD_CTX_create\n");
  23166. return WC_TEST_RET_ENC_NC;
  23167. }
  23168. show("signature = ", (char *)sig, sigSz);
  23169. /* verify */
  23170. pt = (const void*)msg;
  23171. ret1 = EVP_VerifyInit(verf, EVP_sha1());
  23172. ret2 = EVP_VerifyUpdate(verf, pt, count);
  23173. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  23174. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23175. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23176. EVP_MD_CTX_destroy(sign);
  23177. EVP_MD_CTX_destroy(verf);
  23178. printf("error with EVP_Verify\n");
  23179. return WC_TEST_RET_ENC_NC;
  23180. }
  23181. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) != 1) {
  23182. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23183. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23184. EVP_MD_CTX_destroy(sign);
  23185. EVP_MD_CTX_destroy(verf);
  23186. printf("error with EVP_VerifyFinal\n");
  23187. return WC_TEST_RET_ENC_NC;
  23188. }
  23189. /* expect fail without update */
  23190. EVP_VerifyInit(verf, EVP_sha1());
  23191. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) == 1) {
  23192. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23193. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23194. EVP_MD_CTX_destroy(sign);
  23195. EVP_MD_CTX_destroy(verf);
  23196. printf("EVP_VerifyInit without update not detected\n");
  23197. return WC_TEST_RET_ENC_NC;
  23198. }
  23199. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23200. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23201. EVP_MD_CTX_destroy(sign);
  23202. EVP_MD_CTX_destroy(verf);
  23203. wolfSSL_RSA_free(prvRsa);
  23204. wolfSSL_RSA_free(pubRsa);
  23205. EVP_PKEY_free(pubPkey);
  23206. EVP_PKEY_free(prvPkey);
  23207. #endif /* NO_RSA */
  23208. return 0;
  23209. }
  23210. #endif /* OPENSSL_EXTRA */
  23211. #ifndef NO_PWDBASED
  23212. #ifdef HAVE_SCRYPT
  23213. /* Test vectors taken from RFC 7914: scrypt PBKDF - Section 12. */
  23214. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t scrypt_test(void)
  23215. {
  23216. #ifdef HAVE_FIPS
  23217. /* RFC 7914 test vector keys are too short for FIPS. */
  23218. #else
  23219. wc_test_ret_t ret;
  23220. byte derived[64];
  23221. WOLFSSL_SMALL_STACK_STATIC const byte verify1[] = {
  23222. 0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20,
  23223. 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04, 0x97,
  23224. 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8,
  23225. 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2, 0x14, 0x42,
  23226. 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8,
  23227. 0x32, 0x6a, 0x75, 0x3a, 0x0f, 0xc8, 0x1f, 0x17,
  23228. 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28,
  23229. 0xcf, 0x35, 0xe2, 0x0c, 0x38, 0xd1, 0x89, 0x06
  23230. };
  23231. #if !defined(BENCH_EMBEDDED)
  23232. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  23233. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  23234. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  23235. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  23236. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  23237. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  23238. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  23239. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  23240. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  23241. };
  23242. #endif
  23243. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  23244. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  23245. 0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48,
  23246. 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb,
  23247. 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e,
  23248. 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2,
  23249. 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf,
  23250. 0x62, 0xd4, 0x97, 0x05, 0x24, 0x2a, 0x9a, 0xf9,
  23251. 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40,
  23252. 0xdf, 0xcf, 0x01, 0x7b, 0x45, 0x57, 0x58, 0x87
  23253. };
  23254. #endif
  23255. #ifdef SCRYPT_TEST_ALL
  23256. /* Test case is very slow.
  23257. * Use for confirmation after code change or new platform.
  23258. */
  23259. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  23260. 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae,
  23261. 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81,
  23262. 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d,
  23263. 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad, 0xaa, 0x47,
  23264. 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f,
  23265. 0xfa, 0x1c, 0x6d, 0x92, 0x7c, 0x40, 0xf4, 0xc3,
  23266. 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb,
  23267. 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41, 0xa4
  23268. };
  23269. #endif
  23270. WOLFSSL_ENTER("scrypt_test");
  23271. ret = wc_scrypt(derived, NULL, 0, NULL, 0, 4, 1, 1, sizeof(verify1));
  23272. if (ret != 0)
  23273. return WC_TEST_RET_ENC_EC(ret);
  23274. if (XMEMCMP(derived, verify1, sizeof(verify1)) != 0)
  23275. return WC_TEST_RET_ENC_NC;
  23276. #if !defined(BENCH_EMBEDDED)
  23277. ret = wc_scrypt(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 10, 8, 16,
  23278. sizeof(verify2));
  23279. if (ret != 0)
  23280. return WC_TEST_RET_ENC_EC(ret);
  23281. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  23282. return WC_TEST_RET_ENC_NC;
  23283. #endif
  23284. /* Test case with parallel overflowing */
  23285. ret = wc_scrypt(derived, (byte*)"password", 16, (byte*)"NaCl", 16, 2, 4, 8388608,
  23286. sizeof(verify1));
  23287. if (ret != BAD_FUNC_ARG)
  23288. return WC_TEST_RET_ENC_EC(ret);
  23289. /* Don't run these test on embedded, since they use large mallocs */
  23290. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  23291. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  23292. (byte*)"SodiumChloride", 14, 14, 8, 1, sizeof(verify3));
  23293. if (ret != 0)
  23294. return WC_TEST_RET_ENC_EC(ret);
  23295. if (XMEMCMP(derived, verify3, sizeof(verify3)) != 0)
  23296. return WC_TEST_RET_ENC_NC;
  23297. #ifdef SCRYPT_TEST_ALL
  23298. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  23299. (byte*)"SodiumChloride", 14, 20, 8, 1, sizeof(verify4));
  23300. if (ret != 0)
  23301. return WC_TEST_RET_ENC_EC(ret);
  23302. if (XMEMCMP(derived, verify4, sizeof(verify4)) != 0)
  23303. return WC_TEST_RET_ENC_NC;
  23304. #endif
  23305. #else
  23306. #ifdef SCRYPT_TEST_ALL
  23307. (void)verify4;
  23308. #endif
  23309. #endif /* !BENCH_EMBEDDED && !defined(WOLFSSL_LINUXKM) && !HAVE_INTEL_QA */
  23310. #if !defined(BENCH_EMBEDDED)
  23311. ret = wc_scrypt_ex(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 1<<10,
  23312. 8, 16, sizeof(verify2));
  23313. if (ret != 0)
  23314. return WC_TEST_RET_ENC_EC(ret);
  23315. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  23316. return WC_TEST_RET_ENC_NC;
  23317. #endif
  23318. #endif /* !HAVE_FIPS */
  23319. return 0;
  23320. }
  23321. #endif
  23322. #ifdef HAVE_PKCS12
  23323. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_pbkdf_test(void)
  23324. {
  23325. WOLFSSL_SMALL_STACK_STATIC const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
  23326. 0x00, 0x00 };
  23327. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
  23328. WOLFSSL_SMALL_STACK_STATIC const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
  23329. 0x00, 0x67, 0x00, 0x00 };
  23330. WOLFSSL_SMALL_STACK_STATIC const byte salt2[] = { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
  23331. byte derived[64];
  23332. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  23333. 0x27, 0xE9, 0x0D, 0x7E, 0xD5, 0xA1, 0xC4, 0x11,
  23334. 0xBA, 0x87, 0x8B, 0xC0, 0x90, 0xF5, 0xCE, 0xBE,
  23335. 0x5E, 0x9D, 0x5F, 0xE3, 0xD6, 0x2B, 0x73, 0xAA
  23336. };
  23337. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  23338. 0x90, 0x1B, 0x49, 0x70, 0xF0, 0x94, 0xF0, 0xF8,
  23339. 0x45, 0xC0, 0xF3, 0xF3, 0x13, 0x59, 0x18, 0x6A,
  23340. 0x35, 0xE3, 0x67, 0xFE, 0xD3, 0x21, 0xFD, 0x7C
  23341. };
  23342. int id = 1;
  23343. int kLen = 24;
  23344. int iterations = 1;
  23345. wc_test_ret_t ret;
  23346. WOLFSSL_ENTER("pkcs12_pbkdf_test");
  23347. ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
  23348. iterations, kLen, WC_SHA256, id);
  23349. if (ret < 0)
  23350. return WC_TEST_RET_ENC_EC(ret);
  23351. if (XMEMCMP(derived, verify, kLen) != 0)
  23352. return WC_TEST_RET_ENC_NC;
  23353. iterations = 1000;
  23354. ret = wc_PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8,
  23355. iterations, kLen, WC_SHA256, id);
  23356. if (ret < 0)
  23357. return WC_TEST_RET_ENC_EC(ret);
  23358. ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8,
  23359. iterations, kLen, WC_SHA256, id, HEAP_HINT);
  23360. if (ret < 0)
  23361. return WC_TEST_RET_ENC_EC(ret);
  23362. if (XMEMCMP(derived, verify2, 24) != 0)
  23363. return WC_TEST_RET_ENC_NC;
  23364. return 0;
  23365. }
  23366. #endif /* HAVE_PKCS12 */
  23367. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  23368. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf2_test(void)
  23369. {
  23370. char passwd[] = "passwordpassword";
  23371. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a,
  23372. 0x5d, 0x63, 0xcb, 0x06 };
  23373. int iterations = 2048;
  23374. int kLen = 24;
  23375. byte derived[64];
  23376. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  23377. 0x43, 0x6d, 0xb5, 0xe8, 0xd0, 0xfb, 0x3f, 0x35, 0x42, 0x48, 0x39, 0xbc,
  23378. 0x2d, 0xd4, 0xf9, 0x37, 0xd4, 0x95, 0x16, 0xa7, 0x2a, 0x9a, 0x21, 0xd1
  23379. };
  23380. wc_test_ret_t ret;
  23381. WOLFSSL_ENTER("pbkdf2_test");
  23382. ret = wc_PBKDF2_ex(derived, (byte*)passwd, (int)XSTRLEN(passwd),
  23383. salt, (int)sizeof(salt), iterations,
  23384. kLen, WC_SHA256, HEAP_HINT, devId);
  23385. if (ret != 0)
  23386. return WC_TEST_RET_ENC_EC(ret);
  23387. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  23388. return WC_TEST_RET_ENC_NC;
  23389. return 0;
  23390. }
  23391. #endif /* HAVE_PBKDF2 && !NO_SHA256 && !NO_HMAC */
  23392. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  23393. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf1_test(void)
  23394. {
  23395. char passwd[] = "password";
  23396. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  23397. int iterations = 1000;
  23398. int kLen = 16;
  23399. byte derived[16];
  23400. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  23401. 0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F,
  23402. 0xAF, 0x10, 0xEB, 0xFB, 0x4A, 0x3D, 0x2A, 0x20
  23403. };
  23404. wc_test_ret_t ret;
  23405. WOLFSSL_ENTER("pbkdf1_test");
  23406. ret = wc_PBKDF1_ex(derived, kLen, NULL, 0, (byte*)passwd,
  23407. (int)XSTRLEN(passwd), salt, (int)sizeof(salt), iterations, WC_SHA,
  23408. HEAP_HINT);
  23409. if (ret != 0)
  23410. return ret;
  23411. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  23412. return WC_TEST_RET_ENC_NC;
  23413. return 0;
  23414. }
  23415. #endif /* HAVE_PBKDF2 && !NO_SHA */
  23416. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pwdbased_test(void)
  23417. {
  23418. wc_test_ret_t ret = 0;
  23419. WOLFSSL_ENTER("pwdbased_test");
  23420. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  23421. ret = pbkdf1_test();
  23422. if (ret != 0)
  23423. return ret;
  23424. #endif
  23425. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  23426. ret = pbkdf2_test();
  23427. if (ret != 0)
  23428. return ret;
  23429. #endif
  23430. #ifdef HAVE_PKCS12
  23431. ret = pkcs12_pbkdf_test();
  23432. if (ret != 0)
  23433. return ret;
  23434. #endif
  23435. #ifdef HAVE_SCRYPT
  23436. ret = scrypt_test();
  23437. if (ret != 0)
  23438. return ret;
  23439. #endif
  23440. return ret;
  23441. }
  23442. #endif /* NO_PWDBASED */
  23443. #if defined(USE_CERT_BUFFERS_2048) && \
  23444. defined(HAVE_PKCS12) && \
  23445. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_HMAC) && \
  23446. !defined(NO_CERTS) && !defined(NO_DES3)
  23447. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_test(void)
  23448. {
  23449. wc_test_ret_t ret = 0;
  23450. WC_PKCS12* pkcs12 = NULL;
  23451. /* Gen vars */
  23452. byte* pkcs12der = NULL;
  23453. int pkcs12derSz = 0;
  23454. WC_DerCertList derCaList = {
  23455. (byte*)ca_cert_der_2048, sizeof_ca_cert_der_2048, NULL
  23456. };
  23457. char* pass = (char*)"wolfSSL test";
  23458. /* Parsing vars */
  23459. WC_DerCertList* derCaListOut = NULL;
  23460. byte* keyDer = NULL;
  23461. byte* certDer = NULL;
  23462. word32 keySz;
  23463. word32 certSz;
  23464. WOLFSSL_ENTER("pkcs12_test");
  23465. pkcs12 = wc_PKCS12_create(pass, (word32)XSTRLEN(pass),
  23466. (char*)"friendlyName" /* not used currently */,
  23467. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  23468. (byte*)server_cert_der_2048, sizeof_server_cert_der_2048,
  23469. &derCaList, PBE_SHA1_DES3, PBE_SHA1_DES3, 100, 100,
  23470. 0 /* not used currently */, HEAP_HINT);
  23471. if (pkcs12 == NULL) {
  23472. ret = WC_TEST_RET_ENC_EC(MEMORY_E);
  23473. goto out;
  23474. }
  23475. ret = wc_i2d_PKCS12(pkcs12, NULL, &pkcs12derSz);
  23476. if (ret != LENGTH_ONLY_E) {
  23477. if (ret == 0)
  23478. ret = WC_TEST_RET_ENC_NC;
  23479. else
  23480. ret = WC_TEST_RET_ENC_I(ret);
  23481. goto out;
  23482. }
  23483. pkcs12der = (byte*)XMALLOC(pkcs12derSz, HEAP_HINT, DYNAMIC_TYPE_PKCS);
  23484. if (pkcs12der == NULL) {
  23485. ret = WC_TEST_RET_ENC_EC(MEMORY_E);
  23486. goto out;
  23487. }
  23488. {
  23489. /* Use tmp pointer to avoid advancing pkcs12der */
  23490. byte* tmp = pkcs12der;
  23491. ret = wc_i2d_PKCS12(pkcs12, &tmp, &pkcs12derSz);
  23492. if (ret <= 0) {
  23493. if (ret == 0)
  23494. ret = WC_TEST_RET_ENC_NC;
  23495. else
  23496. ret = WC_TEST_RET_ENC_I(ret);
  23497. goto out;
  23498. }
  23499. }
  23500. wc_PKCS12_free(pkcs12);
  23501. pkcs12 = wc_PKCS12_new_ex(HEAP_HINT);
  23502. if (pkcs12 == NULL) {
  23503. ret = WC_TEST_RET_ENC_EC(MEMORY_E);
  23504. goto out;
  23505. }
  23506. /* convert the DER file into an internal structure */
  23507. ret = wc_d2i_PKCS12(pkcs12der, pkcs12derSz, pkcs12);
  23508. if (ret != 0) {
  23509. ret = WC_TEST_RET_ENC_EC(ret);
  23510. goto out;
  23511. }
  23512. /* parse the internal structure into its parts */
  23513. ret = wc_PKCS12_parse(pkcs12, "wolfSSL test", &keyDer, &keySz,
  23514. &certDer, &certSz, &derCaListOut);
  23515. if (ret != 0) {
  23516. ret = WC_TEST_RET_ENC_EC(ret);
  23517. goto out;
  23518. }
  23519. if (keyDer == NULL || certDer == NULL || derCaListOut == NULL) {
  23520. ret = WC_TEST_RET_ENC_NC;
  23521. goto out;
  23522. }
  23523. out:
  23524. if (derCaListOut)
  23525. wc_FreeCertList(derCaListOut, HEAP_HINT);
  23526. XFREE(keyDer, HEAP_HINT, DYNAMIC_TYPE_PKCS);
  23527. XFREE(certDer, HEAP_HINT, DYNAMIC_TYPE_PKCS);
  23528. if (pkcs12)
  23529. wc_PKCS12_free(pkcs12);
  23530. XFREE(pkcs12der, HEAP_HINT, DYNAMIC_TYPE_PKCS);
  23531. return ret;
  23532. }
  23533. #endif
  23534. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  23535. #if defined(WOLFSSL_AFALG_XILINX) || defined(WOLFSSL_AFALG_XILINX_AES) || \
  23536. defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_AFALG_HASH_KEEP) || \
  23537. defined(WOLFSSL_AFALG_XILINX_RSA)
  23538. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  23539. static wc_test_ret_t hkdf_test(void)
  23540. #else
  23541. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hkdf_test(void)
  23542. #endif
  23543. {
  23544. wc_test_ret_t ret = 0;
  23545. #if !defined(NO_SHA) || !defined(NO_SHA256)
  23546. int L;
  23547. byte okm1[42];
  23548. byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  23549. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  23550. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  23551. #ifndef HAVE_FIPS
  23552. byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23553. 0x08, 0x09, 0x0a, 0x0b, 0x0c };
  23554. byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  23555. 0xf8, 0xf9 };
  23556. #endif
  23557. #ifndef NO_SHA
  23558. byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
  23559. 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
  23560. 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
  23561. 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
  23562. 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
  23563. 0x49, 0x18 };
  23564. #ifndef HAVE_FIPS
  23565. byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
  23566. 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
  23567. 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
  23568. 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
  23569. 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
  23570. 0xf8, 0x96 };
  23571. #endif
  23572. #endif /* !NO_SHA */
  23573. #ifndef NO_SHA256
  23574. byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  23575. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  23576. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  23577. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  23578. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  23579. 0x96, 0xc8 };
  23580. #ifndef HAVE_FIPS
  23581. byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
  23582. 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  23583. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  23584. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  23585. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
  23586. 0x58, 0x65 };
  23587. #endif
  23588. #endif /* !NO_SHA256 */
  23589. WOLFSSL_ENTER("hkdf_test");
  23590. XMEMSET(okm1, 0, sizeof(okm1));
  23591. L = (int)sizeof(okm1);
  23592. #ifndef NO_SHA
  23593. ret = wc_HKDF(WC_SHA, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  23594. okm1, (word32)L);
  23595. if (ret != 0)
  23596. return WC_TEST_RET_ENC_EC(ret);
  23597. if (XMEMCMP(okm1, res1, L) != 0)
  23598. return WC_TEST_RET_ENC_NC;
  23599. #ifndef HAVE_FIPS
  23600. /* fips can't have key size under 14 bytes, salt is key too */
  23601. L = (int)sizeof(okm1);
  23602. ret = wc_HKDF(WC_SHA, ikm1, 11, salt1, (word32)sizeof(salt1),
  23603. info1, (word32)sizeof(info1), okm1, (word32)L);
  23604. if (ret != 0)
  23605. return WC_TEST_RET_ENC_EC(ret);
  23606. if (XMEMCMP(okm1, res2, L) != 0)
  23607. return WC_TEST_RET_ENC_NC;
  23608. #endif /* HAVE_FIPS */
  23609. #endif /* !NO_SHA */
  23610. #ifndef NO_SHA256
  23611. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  23612. okm1, (word32)L);
  23613. if (ret != 0)
  23614. return WC_TEST_RET_ENC_EC(ret);
  23615. if (XMEMCMP(okm1, res3, L) != 0)
  23616. return WC_TEST_RET_ENC_NC;
  23617. #ifndef HAVE_FIPS
  23618. /* fips can't have key size under 14 bytes, salt is key too */
  23619. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1),
  23620. salt1, (word32)sizeof(salt1), info1, (word32)sizeof(info1), okm1, (word32)L);
  23621. if (ret != 0)
  23622. return WC_TEST_RET_ENC_EC(ret);
  23623. if (XMEMCMP(okm1, res4, L) != 0)
  23624. return WC_TEST_RET_ENC_NC;
  23625. #endif /* HAVE_FIPS */
  23626. #endif /* !NO_SHA256 */
  23627. #endif /* !NO_SHA || !NO_SHA256 */
  23628. return ret;
  23629. }
  23630. #endif /* HAVE_HKDF */
  23631. #ifdef WOLFSSL_WOLFSSH
  23632. typedef struct {
  23633. byte hashId;
  23634. byte keyId;
  23635. const byte* k;
  23636. word32 kSz;
  23637. const byte* h;
  23638. word32 hSz;
  23639. const byte* sessionId;
  23640. word32 sessionIdSz;
  23641. const byte* expectedKey;
  23642. word32 expectedKeySz;
  23643. } SshKdfTestVector;
  23644. /** Test Vector Set #3: SHA-256 **/
  23645. static const byte sshKdfTvSet3k[] = {
  23646. 0x6A, 0xC3, 0x82, 0xEA, 0xAC, 0xA0, 0x93, 0xE1,
  23647. 0x25, 0xE2, 0x5C, 0x24, 0xBE, 0xBC, 0x84, 0x64,
  23648. 0x0C, 0x11, 0x98, 0x75, 0x07, 0x34, 0x4B, 0x5C,
  23649. 0x73, 0x9C, 0xEB, 0x84, 0xA9, 0xE0, 0xB2, 0x22,
  23650. 0xB9, 0xA8, 0xB5, 0x1C, 0x83, 0x9E, 0x5E, 0xBE,
  23651. 0x49, 0xCF, 0xAD, 0xBF, 0xB3, 0x95, 0x99, 0x76,
  23652. 0x4E, 0xD5, 0x22, 0x09, 0x9D, 0xC9, 0x12, 0x75,
  23653. 0x19, 0x50, 0xDC, 0x7D, 0xC9, 0x7F, 0xBD, 0xC0,
  23654. 0x63, 0x28, 0xB6, 0x8F, 0x22, 0x78, 0x1F, 0xD3,
  23655. 0x15, 0xAF, 0x56, 0x80, 0x09, 0xA5, 0x50, 0x9E,
  23656. 0x5B, 0x87, 0xA1, 0x1B, 0xF5, 0x27, 0xC0, 0x56,
  23657. 0xDA, 0xFF, 0xD8, 0x2A, 0xB6, 0xCB, 0xC2, 0x5C,
  23658. 0xCA, 0x37, 0x14, 0x34, 0x59, 0xE7, 0xBC, 0x63,
  23659. 0xBC, 0xDE, 0x52, 0x75, 0x7A, 0xDE, 0xB7, 0xDF,
  23660. 0x01, 0xCF, 0x12, 0x17, 0x3F, 0x1F, 0xEF, 0x81,
  23661. 0x02, 0xEC, 0x5A, 0xB1, 0x42, 0xC2, 0x13, 0xDD,
  23662. 0x9D, 0x30, 0x69, 0x62, 0x78, 0xA8, 0xD8, 0xBC,
  23663. 0x32, 0xDD, 0xE9, 0x59, 0x2D, 0x28, 0xC0, 0x78,
  23664. 0xC6, 0xD9, 0x2B, 0x94, 0x7D, 0x82, 0x5A, 0xCA,
  23665. 0xAB, 0x64, 0x94, 0x84, 0x6A, 0x49, 0xDE, 0x24,
  23666. 0xB9, 0x62, 0x3F, 0x48, 0x89, 0xE8, 0xAD, 0xC3,
  23667. 0x8E, 0x8C, 0x66, 0x9E, 0xFF, 0xEF, 0x17, 0x60,
  23668. 0x40, 0xAD, 0x94, 0x5E, 0x90, 0xA7, 0xD3, 0xEE,
  23669. 0xC1, 0x5E, 0xFE, 0xEE, 0x78, 0xAE, 0x71, 0x04,
  23670. 0x3C, 0x96, 0x51, 0x11, 0x03, 0xA1, 0x6B, 0xA7,
  23671. 0xCA, 0xF0, 0xAC, 0xD0, 0x64, 0x2E, 0xFD, 0xBE,
  23672. 0x80, 0x99, 0x34, 0xFA, 0xA1, 0xA5, 0xF1, 0xBD,
  23673. 0x11, 0x04, 0x36, 0x49, 0xB2, 0x5C, 0xCD, 0x1F,
  23674. 0xEE, 0x2E, 0x38, 0x81, 0x5D, 0x4D, 0x5F, 0x5F,
  23675. 0xC6, 0xB4, 0x10, 0x29, 0x69, 0xF2, 0x1C, 0x22,
  23676. 0xAE, 0x1B, 0x0E, 0x7D, 0x36, 0x03, 0xA5, 0x56,
  23677. 0xA1, 0x32, 0x62, 0xFF, 0x62, 0x8D, 0xE2, 0x22
  23678. };
  23679. static const byte sshKdfTvSet3h[] = {
  23680. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  23681. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  23682. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  23683. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  23684. };
  23685. static const byte sshKdfTvSet3sid[] = {
  23686. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  23687. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  23688. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  23689. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  23690. };
  23691. static const byte sshKdfTvSet3a[] = {
  23692. 0x81, 0xF0, 0x33, 0x0E, 0xF6, 0xF0, 0x53, 0x61,
  23693. 0xB3, 0x82, 0x3B, 0xFD, 0xED, 0x6E, 0x1D, 0xE9
  23694. };
  23695. static const byte sshKdfTvSet3b[] = {
  23696. 0x3F, 0x6F, 0xD2, 0x06, 0x5E, 0xEB, 0x2B, 0x0B,
  23697. 0x1D, 0x93, 0x19, 0x5A, 0x1F, 0xED, 0x48, 0xA5
  23698. };
  23699. static const byte sshKdfTvSet3c[] = {
  23700. 0xC3, 0x54, 0x71, 0x03, 0x4E, 0x6F, 0xD6, 0x54,
  23701. 0x76, 0x13, 0x17, 0x8E, 0x23, 0x43, 0x5F, 0x21
  23702. };
  23703. static const byte sshKdfTvSet3d[] = {
  23704. 0x7E, 0x9D, 0x79, 0x03, 0x20, 0x90, 0xD9, 0x9F,
  23705. 0x98, 0xB0, 0x15, 0x63, 0x4D, 0xD9, 0xF4, 0x62
  23706. };
  23707. static const byte sshKdfTvSet3e[] = {
  23708. 0x24, 0xEE, 0x55, 0x9A, 0xD7, 0xCE, 0x71, 0x2B,
  23709. 0x68, 0x5D, 0x0B, 0x22, 0x71, 0xE4, 0x43, 0xC1,
  23710. 0x7A, 0xB1, 0xD1, 0xDC, 0xEB, 0x5A, 0x36, 0x05,
  23711. 0x69, 0xD2, 0x5D, 0x5D, 0xC2, 0x43, 0x00, 0x2F
  23712. };
  23713. static const byte sshKdfTvSet3f[] = {
  23714. 0xC3, 0x41, 0x9C, 0x2B, 0x96, 0x62, 0x35, 0x86,
  23715. 0x9D, 0x71, 0x4B, 0xA5, 0xAC, 0x48, 0xDD, 0xB7,
  23716. 0xD9, 0xE3, 0x5C, 0x8C, 0x19, 0xAA, 0xC7, 0x34,
  23717. 0x22, 0x33, 0x7A, 0x37, 0x34, 0x53, 0x60, 0x7E
  23718. };
  23719. static const SshKdfTestVector sshKdfTestVectors[] = {
  23720. {WC_HASH_TYPE_SHA256, 'A',
  23721. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  23722. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  23723. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  23724. sshKdfTvSet3a, sizeof(sshKdfTvSet3a)},
  23725. {WC_HASH_TYPE_SHA256, 'B',
  23726. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  23727. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  23728. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  23729. sshKdfTvSet3b, sizeof(sshKdfTvSet3b)},
  23730. {WC_HASH_TYPE_SHA256, 'C',
  23731. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  23732. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  23733. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  23734. sshKdfTvSet3c, sizeof(sshKdfTvSet3c)},
  23735. {WC_HASH_TYPE_SHA256, 'D',
  23736. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  23737. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  23738. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  23739. sshKdfTvSet3d, sizeof(sshKdfTvSet3d)},
  23740. {WC_HASH_TYPE_SHA256, 'E',
  23741. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  23742. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  23743. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  23744. sshKdfTvSet3e, sizeof(sshKdfTvSet3e)},
  23745. {WC_HASH_TYPE_SHA256, 'F',
  23746. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  23747. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  23748. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  23749. sshKdfTvSet3f, sizeof(sshKdfTvSet3f)},
  23750. };
  23751. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sshkdf_test(void)
  23752. {
  23753. byte cKey[32]; /* Greater of SHA256_DIGEST_SIZE and AES_BLOCK_SIZE */
  23754. word32 i;
  23755. word32 tc = sizeof(sshKdfTestVectors)/sizeof(SshKdfTestVector);
  23756. const SshKdfTestVector* tv = NULL;
  23757. wc_test_ret_t result = 0;
  23758. WOLFSSL_ENTER("sshkdf_test");
  23759. /* sId - Session ID, eKey - Expected Key, cKey - Calculated Key */
  23760. for (i = 0, tv = sshKdfTestVectors; i < tc; i++, tv++) {
  23761. result = wc_SSH_KDF(tv->hashId, tv->keyId,
  23762. cKey, tv->expectedKeySz,
  23763. tv->k, tv->kSz, tv->h, tv->hSz,
  23764. tv->sessionId, tv->sessionIdSz);
  23765. if (result != 0) {
  23766. printf("KDF: Could not derive key.\n");
  23767. result = WC_TEST_RET_ENC_EC(result);
  23768. }
  23769. else {
  23770. if (XMEMCMP(cKey, tv->expectedKey, tv->expectedKeySz) != 0) {
  23771. printf("KDF: Calculated Key does not match Expected Key.\n");
  23772. result = WC_TEST_RET_ENC_EC(result);
  23773. }
  23774. }
  23775. if (result != 0) break;
  23776. }
  23777. return result;
  23778. }
  23779. #endif /* WOLFSSL_WOLFSSH */
  23780. #if defined(WOLFSSL_HAVE_PRF) && !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  23781. #define DIGL 12
  23782. #define SECL 48
  23783. #define LBSL 63
  23784. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t prf_test(void)
  23785. {
  23786. int ret;
  23787. byte dig[DIGL] = {0};
  23788. byte secret[SECL] = {
  23789. 0x10, 0xbc, 0xb4, 0xa2, 0xe8, 0xdc, 0xf1, 0x9b, 0x4c,
  23790. 0x51, 0x9c, 0xed, 0x31, 0x1b, 0x51, 0x57, 0x02, 0x3f,
  23791. 0xa1, 0x7d, 0xfb, 0x0e, 0xf3, 0x4e, 0x8f, 0x6f, 0x71,
  23792. 0xa3, 0x67, 0x76, 0x6b, 0xfa, 0x5d, 0x46, 0x4a, 0xe8,
  23793. 0x61, 0x18, 0x81, 0xc4, 0x66, 0xcc, 0x6f, 0x09, 0x99,
  23794. 0x9d, 0xfc, 0x47
  23795. };
  23796. byte lablSd[LBSL] = {
  23797. 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x66, 0x69,
  23798. 0x6e, 0x69, 0x73, 0x68, 0x65, 0x64, 0x0b, 0x46, 0xba,
  23799. 0x56, 0xbf, 0x1f, 0x5d, 0x99, 0xff, 0xe9, 0xbb, 0x43,
  23800. 0x01, 0xe7, 0xca, 0x2c, 0x00, 0xdf, 0x9a, 0x39, 0x6e,
  23801. 0xcf, 0x6d, 0x15, 0x27, 0x4d, 0xf2, 0x93, 0x96, 0x4a,
  23802. 0x91, 0xde, 0x5c, 0xc0, 0x47, 0x7c, 0xa8, 0xae, 0xcf,
  23803. 0x5d, 0x93, 0x5f, 0x4c, 0x92, 0xcc, 0x98, 0x5b, 0x43
  23804. };
  23805. byte expected[DIGL] = {
  23806. 0xee, 0xcb, 0xb1, 0x30, 0xf2, 0xcd, 0xb3, 0x4a,
  23807. 0xbe, 0xda, 0xc1, 0xf6
  23808. };
  23809. int digL = DIGL;
  23810. int secL = SECL;
  23811. int lblsdL = LBSL;
  23812. int hash_type = sha384_mac;
  23813. ret = wc_PRF(dig, (word32)digL, secret, secL, lablSd, lblsdL, hash_type,
  23814. HEAP_HINT, INVALID_DEVID);
  23815. if (ret != 0) {
  23816. printf("Failed w/ code: %d\n", ret);
  23817. return WC_TEST_RET_ENC_EC(ret);
  23818. }
  23819. if (XMEMCMP(expected, dig, DIGL) != 0) {
  23820. printf("Got unexpected digest\n");
  23821. return WC_TEST_RET_ENC_NC;
  23822. }
  23823. return 0;
  23824. }
  23825. #endif /* WOLFSSL_HAVE_PRF && !NO_HMAC */
  23826. #ifdef WOLFSSL_HAVE_PRF
  23827. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  23828. #ifdef WOLFSSL_BASE16
  23829. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t tls12_kdf_test(void)
  23830. {
  23831. const char* preMasterSecret = "D06F9C19BFF49B1E91E4EFE97345D089"
  23832. "4E6C2E6C34A165B24540E2970875D641"
  23833. "2AA6515871B389B4C199BB8389C71CED";
  23834. const char* helloRandom = "162B81EDFBEAE4F25240320B87E7651C"
  23835. "865564191DD782DB0B9ECA275FBA1BB9"
  23836. "5A1DA3DF436D68DA86C5E7B4B4A36E46"
  23837. "B977C61767983A31BE270D74517BD0F6";
  23838. const char* masterSecret = "EB38B8D89B98B1C266DE44BB3CA14E83"
  23839. "C32F009F9955B1D994E61D3C51EE8760"
  23840. "90B4EF89CC7AF42F46E72201BFCC7977";
  23841. const char* label = "master secret";
  23842. byte pms[48] = {0};
  23843. byte seed[64] = {0};
  23844. byte ms[48] = {0};
  23845. byte result[48] = {0};
  23846. word32 pmsSz = (word32)sizeof(pms);
  23847. word32 seedSz = (word32)sizeof(seed);
  23848. word32 msSz = (word32)sizeof(ms);
  23849. int ret;
  23850. ret = Base16_Decode((const byte*)preMasterSecret,
  23851. (word32)XSTRLEN(preMasterSecret), pms, &pmsSz);
  23852. if (ret != 0)
  23853. return ret;
  23854. ret = Base16_Decode((const byte*)helloRandom,
  23855. (word32)XSTRLEN(helloRandom), seed, &seedSz);
  23856. if (ret != 0)
  23857. return ret;
  23858. ret = Base16_Decode((const byte*)masterSecret,
  23859. (word32)XSTRLEN(masterSecret), ms, &msSz);
  23860. if (ret != 0)
  23861. return ret;
  23862. ret = wc_PRF_TLS(result, msSz, pms, pmsSz,
  23863. (const byte*)label, (word32)XSTRLEN(label), seed, seedSz,
  23864. 1, sha256_mac, NULL, INVALID_DEVID);
  23865. if (ret != 0) {
  23866. if (ret == FIPS_PRIVATE_KEY_LOCKED_E) {
  23867. printf(" wc_PRF_TLSv12: Private key locked.\n");
  23868. }
  23869. return WC_TEST_RET_ENC_NC;
  23870. }
  23871. if (XMEMCMP(result, ms, msSz) != 0)
  23872. return WC_TEST_RET_ENC_NC;
  23873. return 0;
  23874. }
  23875. #endif /* WOLFSSL_BASE16 */
  23876. #endif /* WOLFSSL_HAVE_HKDF && !NO_HMAC */
  23877. #endif /* WOLFSSL_HAVE_PRF */
  23878. #ifdef WOLFSSL_TLS13
  23879. #define TLSV13_PSK_DHE_SZ 40
  23880. typedef struct {
  23881. enum wc_HashType hashAlg;
  23882. word32 pskSz;
  23883. word32 dheSz;
  23884. byte psk[TLSV13_PSK_DHE_SZ];
  23885. byte dhe[TLSV13_PSK_DHE_SZ];
  23886. byte hashHello1[WC_MAX_DIGEST_SIZE];
  23887. byte hashHello2[WC_MAX_DIGEST_SIZE];
  23888. byte hashFinished1[WC_MAX_DIGEST_SIZE];
  23889. byte hashFinished2[WC_MAX_DIGEST_SIZE];
  23890. /* Expected */
  23891. byte clientEarlyTrafficSecret[WC_MAX_DIGEST_SIZE];
  23892. byte earlyExporterMasterSecret[WC_MAX_DIGEST_SIZE];
  23893. byte clientHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  23894. byte serverHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  23895. byte clientApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  23896. byte serverApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  23897. byte exporterMasterSecret[WC_MAX_DIGEST_SIZE];
  23898. byte resumptionMasterSecret[WC_MAX_DIGEST_SIZE];
  23899. } Tls13KdfTestVector;
  23900. /* The following tests come from the CAVP test vectors we used for
  23901. * our FIPS validation. The hash values used are the components from
  23902. * the test hashed together. hashHello1 is the hash of the
  23903. * clientHelloRandom value of the test vector. hashHello2 is the hash
  23904. * of the clientHelloRandom and serverHelloRandom values from the test
  23905. * vector. hashFinished1 is clientHelloRandom, serverHelloRandom, and
  23906. * serverFinishedRandom. hashFinished2 is clientHelloRandom,
  23907. * serverHelloRandom, serverFinishedRandom, and clietnFinishedRandom
  23908. * hashed together. */
  23909. static const Tls13KdfTestVector tls13KdfTestVectors[] = {
  23910. { /* 1 */
  23911. WC_HASH_TYPE_SHA256, 35, 35,
  23912. { /* PSK */
  23913. 0x7b, 0xf1, 0x05, 0x31, 0x36, 0xfa, 0x03, 0xdc,
  23914. 0x31, 0x97, 0x88, 0x04, 0x9c, 0xbc, 0xee, 0xf7,
  23915. 0x8d, 0x84, 0x95, 0x26, 0xaf, 0x1d, 0x68, 0xb0,
  23916. 0x60, 0x7a, 0xcc, 0x4f, 0xc1, 0xd3, 0xa1, 0x68,
  23917. 0x7f, 0x6d, 0xbe
  23918. },
  23919. { /* DHE */
  23920. 0x6e, 0xa1, 0x77, 0xab, 0x2f, 0x43, 0xd2, 0x4b,
  23921. 0xe5, 0xa1, 0x09, 0xe0, 0x7a, 0xd0, 0x01, 0x35,
  23922. 0x8d, 0xf8, 0xf2, 0x5c, 0x91, 0x02, 0xb0, 0x6c,
  23923. 0x3f, 0xeb, 0xee, 0xa4, 0x42, 0x19, 0xce, 0xdc,
  23924. 0x81, 0x26, 0x40
  23925. },
  23926. { /* Hello 1 */
  23927. 0xd9, 0x4b, 0xe4, 0x17, 0xef, 0x58, 0x73, 0x7d,
  23928. 0x28, 0x3d, 0xf0, 0xcc, 0x05, 0x03, 0xaf, 0xac,
  23929. 0x3d, 0x92, 0x79, 0x48, 0xe8, 0x8c, 0xdb, 0xce,
  23930. 0x95, 0x82, 0x21, 0x31, 0x7b, 0x61, 0xd7, 0xc6
  23931. },
  23932. { /* Hello 2 */
  23933. 0xb7, 0x7f, 0x29, 0x91, 0xa4, 0x8b, 0x34, 0xdb,
  23934. 0xbd, 0xc7, 0x54, 0x1c, 0x3b, 0x86, 0xa3, 0x69,
  23935. 0xfe, 0x26, 0xe4, 0x7b, 0xac, 0x57, 0x71, 0xb3,
  23936. 0x32, 0x97, 0xed, 0xd2, 0x0e, 0x95, 0xb8, 0x63
  23937. },
  23938. { /* Finished 1 */
  23939. 0x65, 0xdb, 0x6d, 0x71, 0x71, 0xd0, 0xd8, 0x49,
  23940. 0xd0, 0x3c, 0x8e, 0x2b, 0x24, 0xdf, 0xc2, 0xe9,
  23941. 0xd6, 0xfd, 0xea, 0x04, 0x95, 0x7c, 0xf0, 0x7e,
  23942. 0x57, 0x74, 0x7c, 0xdd, 0xa3, 0x0b, 0x2b, 0x36
  23943. },
  23944. { /* Finished 2 */
  23945. 0x28, 0xf2, 0xf2, 0x79, 0xcf, 0x20, 0x52, 0x90,
  23946. 0x1d, 0x91, 0x05, 0xad, 0x44, 0x26, 0x23, 0x96,
  23947. 0x32, 0xce, 0xec, 0x61, 0xd1, 0xbf, 0x00, 0x48,
  23948. 0x4a, 0xa5, 0x60, 0xcc, 0x28, 0xb5, 0x8d, 0x98
  23949. },
  23950. { /* Client Early Traffic Secret */
  23951. 0x07, 0x14, 0x6a, 0x26, 0x5b, 0x6c, 0x7f, 0x4d, 0x6b, 0x47, 0x3f, 0xd5,
  23952. 0x03, 0x1d, 0xd2, 0x23, 0x3d, 0x89, 0x3e, 0xc6, 0x51, 0xd1, 0xac, 0xf8,
  23953. 0x28, 0xae, 0x4b, 0x76, 0xc8, 0x10, 0x7e, 0xdd
  23954. },
  23955. { /* Early Exporter Master Secret */
  23956. 0xb8, 0xd3, 0x25, 0x7e, 0x2d, 0x41, 0x7b, 0xcb, 0x5e, 0x82, 0x49, 0xf5,
  23957. 0x51, 0x3d, 0xb7, 0x59, 0x32, 0xb3, 0xdf, 0x99, 0x4e, 0x04, 0x69, 0xc6,
  23958. 0x96, 0x8e, 0xe6, 0x3d, 0x91, 0xe4, 0x81, 0x11
  23959. },
  23960. { /* Client Handshake Traffic Secret */
  23961. 0xd9, 0x3b, 0x54, 0xe2, 0xb0, 0xd1, 0x85, 0xf0, 0xfd, 0xf3, 0x48, 0x4a,
  23962. 0xf8, 0x0b, 0xa5, 0xdc, 0x4c, 0x37, 0xcb, 0xd4, 0x20, 0xaf, 0x60, 0xc7,
  23963. 0xd5, 0x50, 0x5d, 0x0c, 0x77, 0x3b, 0x6f, 0xd2
  23964. },
  23965. { /* Server Handshake Traffic Secret */
  23966. 0x4d, 0x40, 0x2b, 0xd2, 0x8c, 0x33, 0x90, 0x39, 0x67, 0x67, 0x05, 0xf7,
  23967. 0x5d, 0x37, 0x1e, 0xdc, 0x4a, 0x70, 0x6b, 0x9e, 0xf8, 0x06, 0x61, 0x89,
  23968. 0x70, 0xe1, 0x3d, 0x36, 0xad, 0x88, 0x7e, 0x5b
  23969. },
  23970. { /* Client Application Traffic Secret */
  23971. 0x74, 0x6e, 0xa0, 0x13, 0x18, 0x34, 0x48, 0x4d, 0x23, 0x31, 0xf1, 0xf9,
  23972. 0xee, 0x44, 0x6d, 0xad, 0xc1, 0xad, 0x92, 0x73, 0xca, 0x27, 0x16, 0x91,
  23973. 0xa2, 0x50, 0x9a, 0xfc, 0xec, 0xf0, 0x6b, 0x24
  23974. },
  23975. { /* Server Application Traffic Secret */
  23976. 0x89, 0x18, 0x7e, 0x34, 0x8d, 0xfc, 0x14, 0xb1, 0x4f, 0x21, 0xd8, 0x29,
  23977. 0xdb, 0x9b, 0xfb, 0x55, 0xcf, 0xa1, 0x4f, 0x95, 0xf8, 0xe0, 0xb0, 0x83,
  23978. 0xd5, 0x34, 0x9e, 0x0b, 0x83, 0x37, 0x42, 0x93
  23979. },
  23980. { /* Exporter Master Secret */
  23981. 0x7d, 0xc8, 0x88, 0x46, 0xd5, 0x57, 0x15, 0xb6, 0x24, 0x25, 0x92, 0x61,
  23982. 0xb1, 0x18, 0x86, 0x2a, 0x6d, 0xa5, 0x84, 0xeb, 0x59, 0xdf, 0x13, 0xbd,
  23983. 0x73, 0xaa, 0x5d, 0x65, 0xab, 0xd9, 0xb4, 0x56
  23984. },
  23985. { /* Resumption Master Secret */
  23986. 0x20, 0xb7, 0xd0, 0xe3, 0x82, 0x01, 0xa1, 0x04, 0xb8, 0x13, 0x29, 0xed,
  23987. 0x35, 0xe4, 0x2f, 0xbf, 0x58, 0x23, 0x7f, 0x21, 0xdb, 0x9f, 0xf8, 0xe0,
  23988. 0xe8, 0xe4, 0xab, 0xc4, 0xa1, 0x61, 0xb9, 0xbb
  23989. }
  23990. },
  23991. { /* 6 */
  23992. WC_HASH_TYPE_SHA256, 0, 33,
  23993. { 0 }, /* PSK */
  23994. { /* DHE */
  23995. 0x7a, 0x46, 0x8c, 0x5a, 0xd1, 0x8e, 0x95, 0xba,
  23996. 0x61, 0xe6, 0x6f, 0xe6, 0x76, 0x0c, 0x20, 0x43,
  23997. 0x16, 0x82, 0x15, 0xfe, 0x54, 0xa3, 0xc7, 0xfd,
  23998. 0x3b, 0x2c, 0x88, 0xb4, 0xd3, 0x42, 0x70, 0x12,
  23999. 0x18
  24000. },
  24001. { /* Hello 1 */
  24002. 0x63, 0x83, 0x58, 0xab, 0x36, 0xcd, 0x0c, 0xf3,
  24003. 0x26, 0x07, 0xb5, 0x5f, 0x0b, 0x8b, 0x45, 0xd6,
  24004. 0x7d, 0x5b, 0x42, 0xdc, 0xa8, 0xaa, 0x06, 0xfb,
  24005. 0x20, 0xa5, 0xbb, 0x85, 0xdb, 0x54, 0xd8, 0x8b
  24006. },
  24007. { /* Hello 2 */
  24008. 0xea, 0xfe, 0x9e, 0x8e, 0xff, 0x1f, 0x6f, 0x43,
  24009. 0xf9, 0x5d, 0xfd, 0xbf, 0xe2, 0x5f, 0x02, 0x2f,
  24010. 0x6d, 0x47, 0x60, 0x9a, 0x48, 0x9a, 0x75, 0xfb,
  24011. 0xb5, 0x4a, 0xbf, 0x9c, 0x4e, 0xff, 0xbf, 0x0b
  24012. },
  24013. { /* Finished 1 */
  24014. 0xca, 0x25, 0xb3, 0x53, 0x8e, 0x6d, 0xc3, 0x36,
  24015. 0x17, 0x30, 0x07, 0xdf, 0x0d, 0xd7, 0x79, 0xb0,
  24016. 0x7f, 0xcb, 0xbe, 0x7a, 0xbc, 0x2d, 0x9f, 0x2d,
  24017. 0x94, 0x44, 0x94, 0xe6, 0xa4, 0xf3, 0xe8, 0x53
  24018. },
  24019. { /* Finished 2 */
  24020. 0x2e, 0xa6, 0x5a, 0xaf, 0xb5, 0xba, 0x9f, 0x2f,
  24021. 0x74, 0x83, 0x5d, 0xbf, 0x86, 0xa4, 0xa6, 0xf6,
  24022. 0xb9, 0x89, 0xdf, 0x17, 0xe1, 0xa8, 0x14, 0xc0,
  24023. 0xe1, 0x50, 0xfa, 0xec, 0xfa, 0xae, 0x8b, 0x7b
  24024. },
  24025. {
  24026. 0x20, 0x18, 0x72, 0x7c, 0xde, 0x3a, 0x85, 0x17, 0x72, 0xdc, 0xd7, 0x72,
  24027. 0xb0, 0xfc, 0x45, 0xd0, 0x62, 0xb9, 0xbb, 0x38, 0x69, 0x05, 0x7b, 0xb4,
  24028. 0x5e, 0x58, 0x5d, 0xed, 0xcd, 0x0b, 0x96, 0xd3
  24029. },
  24030. {
  24031. 0x68, 0x10, 0x20, 0xd1, 0x5e, 0xfc, 0x0c, 0x53, 0x85, 0xbb, 0xdb, 0x18,
  24032. 0xa8, 0x78, 0xf1, 0x2b, 0x13, 0xba, 0x64, 0x1d, 0xe7, 0x09, 0xbe, 0x13,
  24033. 0x49, 0x26, 0xf9, 0x98, 0x56, 0xf1, 0x43, 0xfb
  24034. },
  24035. {
  24036. 0x24, 0x35, 0x3e, 0x10, 0x6f, 0x39, 0x50, 0xd6, 0xa2, 0x12, 0x99, 0xf2,
  24037. 0xd5, 0xf5, 0x19, 0xf5, 0x84, 0xed, 0xee, 0x78, 0x2a, 0xa6, 0xfa, 0x3d,
  24038. 0x06, 0xa8, 0xa7, 0x5d, 0x97, 0x78, 0xd6, 0x58
  24039. },
  24040. {
  24041. 0xf4, 0x57, 0xac, 0x24, 0x7a, 0xfb, 0x7c, 0x3b, 0xb6, 0x39, 0x17, 0x14,
  24042. 0xd9, 0xd4, 0x58, 0x4d, 0x46, 0xd5, 0x1b, 0xde, 0xf7, 0x9d, 0x06, 0xee,
  24043. 0x8d, 0x1a, 0x2c, 0x25, 0x6d, 0x64, 0xde, 0x89
  24044. },
  24045. {
  24046. 0xb6, 0x00, 0xce, 0x63, 0xed, 0x65, 0x8b, 0x66, 0x66, 0x42, 0xc6, 0xbd,
  24047. 0x89, 0xc4, 0x71, 0x6f, 0xce, 0x28, 0xb2, 0xac, 0x97, 0x07, 0x5b, 0xea,
  24048. 0xb8, 0x1d, 0x4c, 0xeb, 0x9e, 0x71, 0x07, 0x8f
  24049. },
  24050. {
  24051. 0xf8, 0x92, 0xc8, 0xba, 0xe7, 0x83, 0xfe, 0x68, 0xe4, 0xd6, 0x5e, 0xcb,
  24052. 0xb3, 0xef, 0x49, 0xd0, 0xe7, 0xb1, 0xac, 0xcb, 0x39, 0x19, 0xfd, 0xa7,
  24053. 0xf7, 0xca, 0xab, 0x1e, 0x42, 0x14, 0xd8, 0xe7
  24054. },
  24055. {
  24056. 0x32, 0x4a, 0x1a, 0xad, 0xe2, 0xbb, 0x55, 0x8a, 0xdd, 0xe9, 0xa5, 0x2a,
  24057. 0x46, 0x5e, 0x6c, 0x83, 0x66, 0x27, 0x27, 0x94, 0xdd, 0x68, 0x59, 0xa0,
  24058. 0xbb, 0xe8, 0x31, 0x7c, 0x39, 0xd7, 0xfd, 0x6d
  24059. },
  24060. {
  24061. 0x58, 0xbc, 0x6c, 0x5b, 0x24, 0xad, 0x82, 0xb3, 0xcc, 0xc7, 0xd1, 0xa1,
  24062. 0xaa, 0x2b, 0x98, 0x9f, 0x2f, 0x7e, 0xa9, 0x63, 0xc2, 0x8e, 0xb6, 0x06,
  24063. 0xc2, 0x2b, 0x74, 0x4b, 0x79, 0x19, 0x7e, 0x2e
  24064. }
  24065. },
  24066. { /* 11 */
  24067. WC_HASH_TYPE_SHA256, 33, 0,
  24068. { /* PSK */
  24069. 0x3d, 0x39, 0x49, 0x36, 0x98, 0xc5, 0xfd, 0xcd,
  24070. 0xa0, 0x17, 0xbd, 0x65, 0x0a, 0xdb, 0xd4, 0x07,
  24071. 0x56, 0xa2, 0x7b, 0xb8, 0x2a, 0x7e, 0xfb, 0x26,
  24072. 0x74, 0xe1, 0xbc, 0x08, 0x4b, 0xf0, 0x30, 0x14,
  24073. 0x12
  24074. },
  24075. { 0 }, /* DHE */
  24076. { /* Hello 1 */
  24077. 0xb7, 0x44, 0x74, 0x6c, 0x57, 0x1f, 0xf3, 0x84,
  24078. 0x8f, 0x63, 0xfb, 0x8c, 0x94, 0x6c, 0x16, 0x68,
  24079. 0x4b, 0xe1, 0xb5, 0xb5, 0x2a, 0x4e, 0x5f, 0xdf,
  24080. 0x4b, 0x53, 0xb2, 0x35, 0xfc, 0x30, 0xf1, 0x36
  24081. },
  24082. { /* Hello 2 */
  24083. 0xe6, 0x4f, 0x3a, 0x4f, 0xd7, 0xe0, 0x64, 0xd4,
  24084. 0x69, 0x50, 0xe4, 0x8b, 0xba, 0xbc, 0x47, 0x74,
  24085. 0xa7, 0x9b, 0x40, 0x91, 0x8f, 0xa8, 0x72, 0x22,
  24086. 0x97, 0xad, 0x43, 0xa7, 0x11, 0x86, 0xb5, 0x72
  24087. },
  24088. { /* Finished 1 */
  24089. 0x5f, 0xa6, 0x10, 0xe2, 0xa3, 0x99, 0x0b, 0x5e,
  24090. 0x57, 0xee, 0xc3, 0x3a, 0x8e, 0x04, 0xf3, 0x0e,
  24091. 0x58, 0x02, 0x09, 0xb2, 0x7e, 0x2d, 0xc6, 0xd2,
  24092. 0x08, 0xae, 0x68, 0x0a, 0x55, 0xa5, 0xda, 0x51
  24093. },
  24094. { /* Finished 2 */
  24095. 0xfc, 0x5b, 0xc0, 0x7e, 0x1b, 0xaa, 0xc0, 0xb4,
  24096. 0x34, 0x85, 0x49, 0x8e, 0x16, 0x31, 0x98, 0xdf,
  24097. 0x10, 0x54, 0x22, 0xda, 0x1e, 0x6b, 0x51, 0xf6,
  24098. 0x97, 0x57, 0xa0, 0x7a, 0x92, 0xe7, 0x47, 0x52
  24099. },
  24100. {
  24101. 0x80, 0xfa, 0x36, 0x30, 0xb8, 0x65, 0xb3, 0x2a, 0x1d, 0x68, 0x91, 0x06,
  24102. 0x98, 0xa0, 0x17, 0x8f, 0xee, 0xb7, 0x9e, 0x3d, 0xd8, 0x84, 0x99, 0x30,
  24103. 0xb9, 0xd6, 0x09, 0x25, 0x5e, 0xfb, 0x8f, 0xd3 },
  24104. {
  24105. 0xa9, 0x89, 0x29, 0x70, 0xe4, 0x55, 0xec, 0x97, 0xfb, 0x24, 0x5b, 0xf9,
  24106. 0xf1, 0xa3, 0x19, 0x3d, 0xf1, 0x31, 0x14, 0xcd, 0x2a, 0xed, 0x21, 0xc8,
  24107. 0xb1, 0x53, 0xad, 0x11, 0x0b, 0x9e, 0x5a, 0xee },
  24108. {
  24109. 0x72, 0xad, 0x8d, 0x7f, 0xfc, 0xb7, 0x68, 0xda, 0x27, 0x60, 0x37, 0xa3,
  24110. 0x4a, 0x63, 0xe8, 0xa5, 0xc8, 0xcd, 0x36, 0x6a, 0x77, 0x99, 0x0d, 0xa9,
  24111. 0xb1, 0x5b, 0x2f, 0x47, 0x2e, 0x22, 0xa7, 0x5e },
  24112. {
  24113. 0x95, 0x6e, 0x85, 0x09, 0xe5, 0x04, 0x88, 0x14, 0x28, 0x8d, 0xdf, 0xe6,
  24114. 0x0d, 0x0f, 0x0d, 0x6b, 0x4e, 0x66, 0x1c, 0x03, 0xb9, 0xaa, 0x2d, 0x45,
  24115. 0x56, 0x67, 0x5c, 0x55, 0x29, 0xd6, 0x89, 0xd0 },
  24116. {
  24117. 0xe8, 0xf2, 0x14, 0xf9, 0x9b, 0x2b, 0x9f, 0x24, 0x2b, 0x37, 0xbe, 0x86,
  24118. 0xdb, 0x23, 0x4b, 0xbe, 0x39, 0x57, 0xe8, 0xa9, 0xa5, 0xee, 0x08, 0xf2,
  24119. 0x75, 0x58, 0xdb, 0xd9, 0x51, 0xc1, 0x46, 0x02 },
  24120. {
  24121. 0x3d, 0x19, 0xaf, 0xa3, 0x0b, 0x21, 0xf7, 0x3d, 0xe7, 0x37, 0x6e, 0x32,
  24122. 0x13, 0x48, 0x9d, 0xea, 0xe0, 0x90, 0xbf, 0x64, 0x48, 0xf7, 0x1e, 0xcc,
  24123. 0xf0, 0xbc, 0x92, 0xd7, 0x8a, 0x4a, 0xa8, 0xc1 },
  24124. {
  24125. 0x16, 0x35, 0xb1, 0x66, 0x28, 0xa3, 0x3e, 0x19, 0xf5, 0x2d, 0x92, 0x22,
  24126. 0x95, 0x48, 0xe8, 0x34, 0x7b, 0x30, 0x50, 0xa2, 0xa0, 0xd9, 0xc2, 0x59,
  24127. 0x39, 0xf9, 0x8c, 0x69, 0xf2, 0x2a, 0xb9, 0xff },
  24128. {
  24129. 0x32, 0x71, 0xa6, 0x87, 0x0c, 0x97, 0x42, 0x07, 0xdd, 0x5f, 0xc9, 0x44,
  24130. 0xa5, 0x7c, 0x50, 0x14, 0xfd, 0xe7, 0x5f, 0x8b, 0xd3, 0x2f, 0xdc, 0x9b,
  24131. 0xa9, 0x93, 0x22, 0x19, 0xe6, 0xf2, 0x0c, 0xd8 }
  24132. },
  24133. #ifdef WOLFSSL_SHA384
  24134. { /* 26 */
  24135. WC_HASH_TYPE_SHA384, 35, 35,
  24136. { /* PSK */
  24137. 0x62, 0x83, 0x25, 0xc7, 0xcc, 0x08, 0x5e, 0x63,
  24138. 0x64, 0x56, 0xf0, 0xc6, 0x88, 0x27, 0x5a, 0x5b,
  24139. 0x68, 0x59, 0x0b, 0x14, 0x55, 0x13, 0x2e, 0xfd,
  24140. 0x8f, 0x28, 0x5b, 0x3d, 0xe3, 0xad, 0x67, 0xe4,
  24141. 0x68, 0xba, 0xf9
  24142. },
  24143. { /* DHE */
  24144. 0xa8, 0xb1, 0xab, 0xd8, 0xc8, 0x5b, 0x52, 0xdf,
  24145. 0x7f, 0x49, 0x10, 0xf4, 0xa1, 0x31, 0xd1, 0x91,
  24146. 0x36, 0xc1, 0x87, 0x5d, 0x42, 0x2a, 0xe7, 0x1d,
  24147. 0x2c, 0x29, 0x3d, 0x40, 0x64, 0x61, 0x63, 0x76,
  24148. 0xd8, 0x66, 0xac
  24149. },
  24150. { /* Hello 1 */
  24151. 0x6f, 0xc6, 0x4c, 0xe1, 0xc6, 0x68, 0x34, 0x8c,
  24152. 0x0a, 0xe1, 0xf8, 0xb8, 0x3e, 0xd4, 0xf8, 0x0b,
  24153. 0x54, 0x50, 0xe4, 0xc5, 0x4a, 0x33, 0x7d, 0xbd,
  24154. 0x90, 0xd2, 0xa2, 0xb9, 0xb7, 0x92, 0xed, 0xab,
  24155. 0x14, 0xf1, 0xe4, 0x86, 0x22, 0x67, 0xd7, 0x44,
  24156. 0x03, 0x21, 0xdc, 0x51, 0x52, 0x7f, 0x35, 0x80
  24157. },
  24158. { /* Hello 2 */
  24159. 0x3e, 0xcf, 0x2f, 0xc3, 0x87, 0xba, 0xc5, 0xbd,
  24160. 0x7c, 0xe8, 0x35, 0x5b, 0x95, 0x51, 0x30, 0x3b,
  24161. 0x08, 0xcc, 0x2a, 0x7d, 0xb5, 0x74, 0x7c, 0x16,
  24162. 0xb3, 0x0b, 0xe7, 0x61, 0xa3, 0x7c, 0x6c, 0xbd,
  24163. 0x39, 0x74, 0xfd, 0x1e, 0x4c, 0xff, 0xc8, 0xcc,
  24164. 0xa0, 0xef, 0x29, 0x4d, 0x94, 0xaa, 0x55, 0x6f,
  24165. },
  24166. { /* Finished 1 */
  24167. 0x06, 0xc1, 0x47, 0x78, 0x66, 0x53, 0x6f, 0x24,
  24168. 0x94, 0x61, 0x69, 0xec, 0xd8, 0x60, 0x31, 0x2f,
  24169. 0xbf, 0xd6, 0x8a, 0x29, 0x17, 0xff, 0xa3, 0x88,
  24170. 0x13, 0x09, 0x8c, 0x9d, 0x6c, 0x64, 0x84, 0x48,
  24171. 0x44, 0xdd, 0x2d, 0x29, 0x4d, 0xe6, 0x98, 0x2b,
  24172. 0x45, 0x3b, 0x84, 0x33, 0x79, 0xb2, 0x75, 0x68
  24173. },
  24174. { /* Finished 2 */
  24175. 0x28, 0x1e, 0x18, 0xf7, 0x9c, 0x32, 0xa9, 0xbf,
  24176. 0x0c, 0x24, 0x58, 0x21, 0xce, 0xbc, 0xf2, 0x44,
  24177. 0xb1, 0x18, 0xaf, 0x9d, 0xd9, 0x20, 0xf9, 0xf4,
  24178. 0xed, 0xcc, 0x53, 0x82, 0x66, 0x5c, 0x46, 0x94,
  24179. 0x8c, 0x36, 0x5e, 0xca, 0x9f, 0xd8, 0x9a, 0xd3,
  24180. 0xf0, 0xe1, 0x53, 0x71, 0xdd, 0x19, 0x1e, 0x59
  24181. },
  24182. {
  24183. 0xd0, 0xef, 0xa8, 0xcb, 0x5b, 0x14, 0x0f, 0x0a, 0x62, 0xba, 0x5a, 0xb1,
  24184. 0xc5, 0xb5, 0x3f, 0x11, 0xda, 0xa1, 0x0c, 0x9c, 0xb4, 0x32, 0x48, 0x4e,
  24185. 0xfa, 0x84, 0x4f, 0xe4, 0xe7, 0x91, 0x8f, 0x42, 0x3f, 0xc7, 0x4e, 0xd3,
  24186. 0x83, 0x3d, 0x7f, 0x70, 0x12, 0xee, 0x9a, 0x37, 0x01, 0xbb, 0x14, 0xd3
  24187. },
  24188. {
  24189. 0x48, 0x6f, 0x77, 0x1d, 0x39, 0x1b, 0xa5, 0x9a, 0x76, 0xd9, 0x1d, 0x7d,
  24190. 0xb3, 0xd9, 0xb9, 0x78, 0x35, 0x0f, 0xd0, 0xe1, 0x07, 0x1f, 0x8d, 0xe5,
  24191. 0x75, 0x00, 0xda, 0xc0, 0x19, 0x01, 0xfb, 0x08, 0x35, 0xe7, 0x18, 0x8f,
  24192. 0xf0, 0x19, 0xfb, 0x46, 0xf6, 0xa5, 0x77, 0x0e, 0x90, 0x38, 0x8b, 0x15
  24193. },
  24194. {
  24195. 0x80, 0x8c, 0xa7, 0x24, 0x97, 0xf9, 0xd3, 0x52, 0xb0, 0x69, 0x9d, 0x4b,
  24196. 0xa4, 0x19, 0x4a, 0xb1, 0x46, 0x53, 0x3a, 0xc8, 0xe4, 0x02, 0x69, 0xf2,
  24197. 0xe7, 0xb6, 0x1d, 0x33, 0x51, 0xcc, 0x14, 0x40, 0x4a, 0xb0, 0xe7, 0x58,
  24198. 0x84, 0xba, 0xc2, 0x14, 0x58, 0x6b, 0xb9, 0xdc, 0x50, 0x98, 0x67, 0x01
  24199. },
  24200. {
  24201. 0xb1, 0xa8, 0xc0, 0x06, 0xb3, 0x2e, 0xa7, 0x8a, 0x6a, 0x12, 0x88, 0x00,
  24202. 0x65, 0x88, 0x9c, 0x5d, 0x35, 0xee, 0xe5, 0x51, 0x0b, 0x62, 0xf8, 0x67,
  24203. 0xe5, 0xef, 0x15, 0x1f, 0x23, 0x02, 0x74, 0x08, 0x9c, 0xc8, 0xba, 0x27,
  24204. 0x5d, 0x32, 0x19, 0x6f, 0x6d, 0x5d, 0x72, 0x5e, 0x15, 0xde, 0x30, 0xc3
  24205. },
  24206. {
  24207. 0xfd, 0xce, 0xf5, 0x65, 0x45, 0x84, 0xfb, 0x8c, 0x79, 0xa4, 0x6c, 0x1b,
  24208. 0x0e, 0x1b, 0xfd, 0x26, 0xa2, 0x53, 0xf4, 0x4e, 0x00, 0x4d, 0x4b, 0x0b,
  24209. 0x24, 0x6d, 0x35, 0x35, 0xd9, 0x97, 0x70, 0xc5, 0xf4, 0xee, 0xe3, 0xba,
  24210. 0x31, 0x1e, 0x2a, 0x42, 0xcb, 0xdf, 0x40, 0xb1, 0x14, 0xb8, 0x53, 0xce
  24211. },
  24212. {
  24213. 0xbb, 0xb3, 0x26, 0x7c, 0x22, 0x21, 0x9b, 0x72, 0x32, 0xa1, 0x97, 0xfb,
  24214. 0x78, 0x8c, 0xbe, 0x3d, 0x71, 0x45, 0xb8, 0xf5, 0x24, 0x8f, 0x0f, 0xac,
  24215. 0x42, 0x5b, 0x81, 0xe8, 0xd0, 0x71, 0x4a, 0xcb, 0x32, 0x3f, 0x03, 0xfb,
  24216. 0xec, 0x6a, 0x1f, 0x76, 0x80, 0x65, 0x01, 0x7a, 0x3d, 0xce, 0xc4, 0xdf
  24217. },
  24218. {
  24219. 0x3f, 0xcf, 0x2f, 0x63, 0x94, 0x94, 0x99, 0xfd, 0x04, 0x3a, 0x89, 0x83,
  24220. 0xcf, 0x06, 0x05, 0xec, 0x20, 0x3e, 0x5f, 0x51, 0x9d, 0x6e, 0x4a, 0xc6,
  24221. 0xf1, 0x2b, 0x37, 0x17, 0x34, 0x72, 0x6e, 0x1d, 0x2a, 0xfd, 0xc7, 0x73,
  24222. 0xb5, 0x07, 0x22, 0x81, 0x32, 0x2e, 0x21, 0x85, 0xaf, 0x10, 0xb2, 0x73
  24223. },
  24224. {
  24225. 0x52, 0x0c, 0x3d, 0x2e, 0x2d, 0x4a, 0x11, 0xae, 0x96, 0x78, 0xe9, 0x5b,
  24226. 0xd8, 0x0f, 0x6c, 0xf4, 0xbd, 0x96, 0x13, 0x55, 0x88, 0xdd, 0xa3, 0x67,
  24227. 0x36, 0x86, 0x1e, 0x0b, 0x36, 0x41, 0xec, 0xf6, 0x04, 0xb2, 0xc4, 0x16,
  24228. 0xbc, 0x2c, 0xdb, 0x30, 0x02, 0x94, 0xd4, 0x42, 0xbf, 0x38, 0xee, 0x9d
  24229. }
  24230. },
  24231. { /* 36 */
  24232. WC_HASH_TYPE_SHA384, 0, 33,
  24233. { 0 }, /* PSK */
  24234. { /* DHE */
  24235. 0xd3, 0x00, 0x72, 0x9a, 0xa8, 0xc5, 0xf3, 0xc4,
  24236. 0xf1, 0xa0, 0x26, 0x89, 0x65, 0x70, 0xc7, 0x0b,
  24237. 0x77, 0xbb, 0xe1, 0x4b, 0x2b, 0xa8, 0x4f, 0xa6,
  24238. 0x09, 0x4b, 0xba, 0x45, 0x36, 0x15, 0xee, 0x68,
  24239. 0xfd
  24240. },
  24241. { /* Hello 1 */
  24242. 0x10, 0x9d, 0x8b, 0xa2, 0x93, 0xe7, 0xd3, 0xb9,
  24243. 0xb4, 0x0f, 0xeb, 0x6a, 0xb9, 0x69, 0xcb, 0x39,
  24244. 0x16, 0x29, 0xcc, 0xd3, 0xcc, 0x1a, 0x4c, 0x1b,
  24245. 0x53, 0x7c, 0x33, 0x88, 0x06, 0xbc, 0x0a, 0x02,
  24246. 0xa0, 0xbe, 0x62, 0xc0, 0xe6, 0x5e, 0x97, 0x5b,
  24247. 0x6a, 0xa1, 0x98, 0xf3, 0xd2, 0x1e, 0xcd, 0xc5
  24248. },
  24249. { /* Hello 2 */
  24250. 0x74, 0xc0, 0x07, 0x2c, 0xc1, 0x63, 0xcc, 0x11,
  24251. 0xad, 0x1a, 0x55, 0x63, 0xbc, 0x20, 0x77, 0x96,
  24252. 0x30, 0x1c, 0x68, 0x45, 0x1e, 0x9b, 0xa7, 0xb4,
  24253. 0xf3, 0x04, 0x45, 0x16, 0x76, 0x55, 0xf9, 0xdf,
  24254. 0x4b, 0x2f, 0x1a, 0xdf, 0x5a, 0xb0, 0x93, 0xc9,
  24255. 0xab, 0xf5, 0x32, 0x47, 0x79, 0x9c, 0x01, 0xeb
  24256. },
  24257. { /* Finished 1 */
  24258. 0x27, 0x08, 0x8e, 0xa5, 0xf1, 0x30, 0xe1, 0xd6,
  24259. 0x4f, 0xa2, 0x9e, 0x3b, 0x03, 0x2d, 0x2e, 0xa3,
  24260. 0x84, 0x75, 0x51, 0x3a, 0xc3, 0xf6, 0xee, 0x2e,
  24261. 0x37, 0x0c, 0xe3, 0x28, 0x46, 0xa5, 0x2d, 0xc7,
  24262. 0xf0, 0x64, 0x78, 0x53, 0x66, 0x43, 0x02, 0xa4,
  24263. 0x7a, 0x43, 0x66, 0x4b, 0xa7, 0xcb, 0x97, 0x16
  24264. },
  24265. { /* Finished 2 */
  24266. 0x1d, 0x0d, 0xf8, 0xe1, 0x81, 0xa5, 0xbd, 0xa8,
  24267. 0x6f, 0x9d, 0x01, 0xa4, 0x9a, 0x92, 0xe2, 0xef,
  24268. 0x08, 0xab, 0xef, 0x3e, 0x2d, 0xd4, 0x82, 0xac,
  24269. 0x68, 0x9d, 0xe0, 0x54, 0x17, 0xde, 0x1a, 0xed,
  24270. 0x57, 0xcb, 0xd9, 0x2d, 0xc8, 0xbc, 0x93, 0xe6,
  24271. 0xa3, 0xec, 0xde, 0xee, 0xa1, 0x1c, 0x41, 0x85
  24272. },
  24273. {
  24274. 0x7f, 0x1f, 0xe6, 0x7b, 0xd8, 0xf5, 0x2b, 0x37, 0xbe, 0xb7, 0xd0, 0x37,
  24275. 0xce, 0x46, 0xad, 0x04, 0x2f, 0xc7, 0xdb, 0xc9, 0x9a, 0xb6, 0x00, 0x3f,
  24276. 0xc1, 0x97, 0xe9, 0x5c, 0x5e, 0x14, 0xd1, 0x38, 0x4d, 0x55, 0xe1, 0x07,
  24277. 0xb5, 0x85, 0x6d, 0xfa, 0xa7, 0x66, 0xad, 0xfa, 0xb6, 0xad, 0x29, 0x44
  24278. },
  24279. {
  24280. 0x4e, 0x6b, 0x20, 0x99, 0x55, 0x1b, 0x21, 0x89, 0xb6, 0x70, 0xdb, 0xe8,
  24281. 0xa7, 0x16, 0x55, 0xf2, 0x93, 0x13, 0x90, 0x7d, 0xfa, 0x62, 0x65, 0x53,
  24282. 0xa0, 0x97, 0xe9, 0xb4, 0xc0, 0xf1, 0xc9, 0x1a, 0x67, 0xdd, 0xca, 0x57,
  24283. 0xbc, 0xca, 0x39, 0xe6, 0x39, 0x6b, 0x63, 0x47, 0x25, 0x08, 0x3a, 0xd7
  24284. },
  24285. {
  24286. 0x35, 0x0d, 0xac, 0xd8, 0x10, 0x6a, 0x46, 0x50, 0x66, 0xae, 0x02, 0xc9,
  24287. 0xde, 0x13, 0x48, 0xce, 0x53, 0xd4, 0x92, 0x62, 0xc5, 0x65, 0x10, 0x08,
  24288. 0xc2, 0xc2, 0x82, 0xed, 0x9d, 0xc9, 0x6f, 0xa8, 0xc3, 0xc1, 0x0b, 0x7c,
  24289. 0xe1, 0x97, 0x85, 0xd6, 0x46, 0x29, 0x0e, 0x42, 0x51, 0xc1, 0x35, 0xcf
  24290. },
  24291. {
  24292. 0x3d, 0x5d, 0x84, 0xbd, 0x16, 0x46, 0x34, 0xb3, 0xf6, 0x31, 0x49, 0x3e,
  24293. 0x8d, 0xdc, 0xcb, 0x8c, 0x6a, 0x42, 0xf4, 0x88, 0xfc, 0x19, 0xfa, 0xa2,
  24294. 0x25, 0xc7, 0xa0, 0xa4, 0xca, 0xf0, 0xea, 0x2d, 0xe8, 0xc4, 0x02, 0x14,
  24295. 0x63, 0xfb, 0xd3, 0x7b, 0x51, 0x1c, 0xce, 0xca, 0xa3, 0xc3, 0xe4, 0xa5
  24296. },
  24297. {
  24298. 0x7c, 0x3a, 0x55, 0x92, 0x2e, 0xdd, 0x75, 0xdd, 0x76, 0x54, 0x4a, 0x9f,
  24299. 0xd0, 0xa2, 0x88, 0x83, 0xe9, 0x27, 0xda, 0x30, 0xe9, 0x96, 0x58, 0xc5,
  24300. 0xb7, 0x56, 0xfc, 0x4b, 0xb8, 0x5d, 0xee, 0x46, 0x70, 0x4e, 0x1b, 0x06,
  24301. 0x86, 0xaf, 0x48, 0x5c, 0x17, 0x35, 0xfa, 0x69, 0xc2, 0x4d, 0xfb, 0x09
  24302. },
  24303. {
  24304. 0x00, 0x0e, 0x28, 0x51, 0xc1, 0x7f, 0x41, 0x89, 0x6f, 0x9a, 0xca, 0x15,
  24305. 0xee, 0xed, 0x43, 0xca, 0x6d, 0x65, 0x6f, 0x51, 0x18, 0x6c, 0x08, 0x4b,
  24306. 0x77, 0xca, 0x75, 0xc4, 0xc3, 0xde, 0x29, 0x41, 0x8b, 0xaf, 0xa7, 0x1c,
  24307. 0x28, 0x37, 0xa0, 0xa0, 0x74, 0x8e, 0x09, 0x42, 0x7a, 0x1b, 0x68, 0xdb
  24308. },
  24309. {
  24310. 0x14, 0x8f, 0xab, 0x28, 0x64, 0xea, 0x45, 0x88, 0xdb, 0xc1, 0xc6, 0xa0,
  24311. 0x48, 0xdf, 0x15, 0xd0, 0x28, 0x07, 0x2d, 0x6c, 0xb8, 0x42, 0xbb, 0x60,
  24312. 0x02, 0x08, 0x9e, 0x29, 0x9b, 0x8d, 0xd6, 0x1c, 0xaf, 0xf2, 0x1a, 0xdc,
  24313. 0xf0, 0x78, 0x0b, 0x4d, 0x90, 0xa1, 0x0c, 0xb3, 0x13, 0xde, 0xca, 0x5a
  24314. },
  24315. {
  24316. 0x4d, 0x80, 0x7d, 0x0b, 0xb9, 0x00, 0x6f, 0x65, 0x51, 0x65, 0x23, 0xde,
  24317. 0x72, 0xdc, 0x4f, 0x04, 0xa5, 0xa2, 0x90, 0x45, 0x51, 0x9e, 0xd0, 0x3a,
  24318. 0xe4, 0xd7, 0x78, 0xa3, 0x0f, 0x2d, 0x65, 0x12, 0xad, 0xc8, 0x92, 0x30,
  24319. 0x79, 0x9d, 0x9d, 0x08, 0x7a, 0x9c, 0x9f, 0x83, 0xb1, 0xca, 0x59, 0x56
  24320. }
  24321. },
  24322. { /* 41 */
  24323. WC_HASH_TYPE_SHA384, 33, 0,
  24324. { /* PSK */
  24325. 0xa4, 0x8b, 0x1b, 0x5f, 0xd0, 0xea, 0x75, 0x62,
  24326. 0x06, 0x4d, 0x68, 0x40, 0x85, 0x20, 0x45, 0x95,
  24327. 0x4a, 0x00, 0xca, 0x05, 0xeb, 0xd4, 0x1d, 0x48,
  24328. 0x81, 0x89, 0xe8, 0x86, 0x43, 0xfa, 0x28, 0x17,
  24329. 0x12
  24330. },
  24331. { 0 }, /* DHE */
  24332. { /* Hello 1 */
  24333. 0x03, 0x7c, 0x33, 0x75, 0xdc, 0xc5, 0x46, 0x3a,
  24334. 0x0d, 0x56, 0xc6, 0xfb, 0xab, 0x1e, 0x1d, 0xda,
  24335. 0x59, 0xc2, 0xb2, 0xb1, 0x7c, 0x48, 0x9b, 0x06,
  24336. 0x0a, 0x5a, 0xbb, 0xf8, 0x98, 0x53, 0x78, 0x2d,
  24337. 0xd2, 0xcc, 0x87, 0x68, 0x25, 0xdd, 0x88, 0x22,
  24338. 0xcd, 0xb7, 0x74, 0x55, 0x21, 0xf9, 0x34, 0x98
  24339. },
  24340. { /* Hello 2 */
  24341. 0x03, 0xb4, 0xfb, 0xcc, 0x28, 0x2c, 0xc1, 0x70,
  24342. 0x42, 0x73, 0x57, 0xac, 0xdb, 0x47, 0x71, 0xf6,
  24343. 0x2e, 0x11, 0x8a, 0x5b, 0x47, 0x2f, 0x02, 0x54,
  24344. 0x95, 0x34, 0xed, 0x5f, 0x19, 0xc1, 0x75, 0xe0,
  24345. 0x76, 0xad, 0xb0, 0x90, 0x57, 0xcd, 0xfd, 0xd7,
  24346. 0x58, 0x1f, 0x0d, 0x6b, 0x9e, 0x51, 0x3c, 0x08
  24347. },
  24348. { /* Finished 1 */
  24349. 0x2b, 0x50, 0xd9, 0xa7, 0x43, 0x24, 0xda, 0x2c,
  24350. 0x7a, 0xaa, 0x0e, 0x37, 0xd7, 0x6b, 0x2c, 0xab,
  24351. 0x8e, 0xb2, 0xfe, 0x31, 0x1b, 0xa8, 0x12, 0x59,
  24352. 0x5b, 0x7b, 0xdc, 0x3e, 0xa7, 0x86, 0xa5, 0x48,
  24353. 0xe4, 0x46, 0x2b, 0x4c, 0xc1, 0x66, 0x4b, 0xf3,
  24354. 0x2a, 0x99, 0x93, 0x08, 0xbc, 0x3d, 0x08, 0x76
  24355. },
  24356. { /* Finished 2 */
  24357. 0x7c, 0x34, 0xc8, 0x56, 0x17, 0xf1, 0x62, 0x1c,
  24358. 0x9f, 0x0b, 0xeb, 0xfd, 0x69, 0x72, 0x51, 0xc5,
  24359. 0xfa, 0x74, 0x87, 0xc9, 0xbd, 0x50, 0xe9, 0x48,
  24360. 0xa7, 0x3c, 0x94, 0x3e, 0x06, 0x7d, 0xe8, 0x8e,
  24361. 0xc1, 0xd1, 0x08, 0x1f, 0x5d, 0x48, 0x8a, 0x25,
  24362. 0xfc, 0xea, 0xe7, 0xd9, 0xd4, 0xd0, 0xf9, 0xad
  24363. },
  24364. {
  24365. 0x4b, 0x0b, 0xed, 0xb9, 0xc8, 0xb8, 0xa8, 0x1e, 0xb0, 0x81, 0x76, 0xd5,
  24366. 0x33, 0x22, 0x71, 0x33, 0x3a, 0x85, 0x19, 0x67, 0x7e, 0x91, 0x37, 0xf2,
  24367. 0xa6, 0x11, 0x22, 0xdf, 0x41, 0x04, 0x3d, 0xa9, 0x13, 0xb9, 0xb2, 0xb1,
  24368. 0xbb, 0xd8, 0xef, 0x23, 0x7c, 0xc2, 0xab, 0x70, 0x1b, 0x51, 0x9f, 0xc9
  24369. },
  24370. {
  24371. 0xeb, 0x96, 0x10, 0x8c, 0x7d, 0x92, 0xea, 0x80, 0x86, 0xb2, 0xf8, 0x27,
  24372. 0xf2, 0x9a, 0x09, 0xc1, 0x7c, 0x09, 0x43, 0xbc, 0xfe, 0xc8, 0x75, 0xe0,
  24373. 0x97, 0xe7, 0x6d, 0xd5, 0xb2, 0x3c, 0xed, 0x12, 0xb7, 0x74, 0x0e, 0xe3,
  24374. 0xb6, 0xe0, 0xba, 0xe1, 0x8d, 0x89, 0xcf, 0x4f, 0x57, 0xf6, 0x6d, 0x90
  24375. },
  24376. {
  24377. 0x22, 0xb0, 0x39, 0x34, 0xb6, 0x6c, 0x2d, 0x7a, 0x97, 0x1c, 0x5d, 0xcc,
  24378. 0x78, 0x84, 0x71, 0xbb, 0xc6, 0x7b, 0xb6, 0xbc, 0xcc, 0x0b, 0xf8, 0xac,
  24379. 0x8e, 0xd7, 0x20, 0xbd, 0xbe, 0x32, 0xf0, 0xd6, 0xe9, 0x69, 0x13, 0xf2,
  24380. 0x9a, 0xce, 0xfe, 0x86, 0xd3, 0xee, 0xba, 0x69, 0x51, 0xb6, 0x77, 0x56
  24381. },
  24382. {
  24383. 0x16, 0xfd, 0xda, 0xf3, 0x5e, 0xb9, 0xa6, 0x17, 0x24, 0xb2, 0x16, 0x9f,
  24384. 0xb6, 0x59, 0x13, 0x0f, 0x25, 0x5a, 0xf1, 0x5b, 0x5f, 0xe4, 0x54, 0x2a,
  24385. 0xa7, 0xbf, 0x29, 0xaf, 0x5a, 0x77, 0xf4, 0x4f, 0x25, 0xba, 0x94, 0xad,
  24386. 0x6b, 0x91, 0x3b, 0xe7, 0xd5, 0x73, 0x0d, 0xff, 0xaa, 0xe3, 0x72, 0x2c
  24387. },
  24388. {
  24389. 0x22, 0xb4, 0x94, 0xc0, 0x53, 0xd7, 0x82, 0x06, 0x38, 0x9d, 0x4a, 0xa0,
  24390. 0x3f, 0xf1, 0x5f, 0x6e, 0x23, 0x8d, 0x09, 0x62, 0xbf, 0x6f, 0x7c, 0x84,
  24391. 0xc6, 0x3e, 0x15, 0xad, 0x18, 0x37, 0x76, 0x29, 0xc7, 0xd6, 0x68, 0x0c,
  24392. 0x1e, 0xc6, 0x93, 0x31, 0xef, 0x85, 0x69, 0x30, 0x68, 0xf0, 0x1e, 0x37
  24393. },
  24394. {
  24395. 0x6d, 0x4d, 0x20, 0xaf, 0x47, 0xe8, 0x1b, 0xfa, 0xd0, 0xb6, 0xc8, 0x97,
  24396. 0xd1, 0x03, 0xfc, 0x9d, 0x59, 0xa0, 0x68, 0x9d, 0xe9, 0x17, 0x8b, 0xce,
  24397. 0x48, 0x2c, 0x77, 0x8a, 0x22, 0x4b, 0x5c, 0x54, 0x22, 0xa1, 0x15, 0x12,
  24398. 0xe1, 0x07, 0x8e, 0x15, 0xd8, 0x7b, 0x16, 0x65, 0x99, 0x6b, 0xcb, 0x71
  24399. },
  24400. {
  24401. 0x79, 0x64, 0x79, 0xdd, 0x75, 0x5c, 0x6f, 0x98, 0xac, 0x03, 0xe0, 0xcd,
  24402. 0x92, 0xba, 0x0e, 0x2d, 0xb4, 0xd1, 0x8b, 0x97, 0xd0, 0x85, 0xbb, 0x2e,
  24403. 0x4f, 0x26, 0x93, 0xf5, 0x1d, 0xf3, 0xd2, 0x43, 0x4f, 0xd2, 0x47, 0xaa,
  24404. 0x91, 0x1e, 0xf3, 0x67, 0x10, 0x18, 0x2c, 0xb9, 0x01, 0xba, 0x10, 0x9f
  24405. },
  24406. {
  24407. 0x79, 0xb6, 0x9c, 0xbe, 0xf1, 0x6a, 0xb0, 0x92, 0xa0, 0x29, 0x52, 0x61,
  24408. 0xf1, 0xcd, 0x3a, 0x67, 0xe1, 0x6b, 0xb8, 0x9d, 0x0d, 0x95, 0xb6, 0x03,
  24409. 0x80, 0x1f, 0xd5, 0x75, 0xb6, 0x1d, 0x79, 0x02, 0x93, 0x43, 0x77, 0xa7,
  24410. 0x9d, 0x2f, 0xc3, 0x84, 0xc6, 0x83, 0x76, 0x16, 0x06, 0x98, 0x7b, 0x79
  24411. }
  24412. },
  24413. #endif /* WOLFSSL_SHA384 */
  24414. };
  24415. static const char protocolLabel[] = "tls13 ";
  24416. static const char ceTrafficLabel[] = "c e traffic";
  24417. static const char eExpMasterLabel[] = "e exp master";
  24418. static const char cHsTrafficLabel[] = "c hs traffic";
  24419. static const char sHsTrafficLabel[] = "s hs traffic";
  24420. static const char cAppTrafficLabel[] = "c ap traffic";
  24421. static const char sAppTrafficLabel[] = "s ap traffic";
  24422. static const char expMasterLabel[] = "exp master";
  24423. static const char resMasterLabel[] = "res master";
  24424. static const char derivedLabel[] = "derived";
  24425. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t tls13_kdf_test(void)
  24426. {
  24427. wc_test_ret_t ret = 0;
  24428. word32 i;
  24429. word32 tc = sizeof(tls13KdfTestVectors)/sizeof(Tls13KdfTestVector);
  24430. const Tls13KdfTestVector* tv = NULL;
  24431. WOLFSSL_ENTER("tls13_kdf_test");
  24432. for (i = 0, tv = tls13KdfTestVectors; i < tc; i++, tv++) {
  24433. byte output[WC_MAX_DIGEST_SIZE];
  24434. byte secret[WC_MAX_DIGEST_SIZE];
  24435. byte salt[WC_MAX_DIGEST_SIZE];
  24436. byte zeroes[WC_MAX_DIGEST_SIZE];
  24437. byte hashZero[WC_MAX_DIGEST_SIZE];
  24438. int hashAlgSz;
  24439. XMEMSET(zeroes, 0, sizeof zeroes);
  24440. hashAlgSz = wc_HashGetDigestSize(tv->hashAlg);
  24441. if (hashAlgSz == BAD_FUNC_ARG) break;
  24442. ret = wc_Hash(tv->hashAlg, NULL, 0, hashZero, (word32)hashAlgSz);
  24443. if (ret != 0) break;
  24444. ret = wc_Tls13_HKDF_Extract(secret, NULL, 0,
  24445. (tv->pskSz == 0) ? zeroes : (byte*)tv->psk,
  24446. tv->pskSz, tv->hashAlg);
  24447. if (ret != 0) break;
  24448. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24449. secret, (word32)hashAlgSz,
  24450. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24451. (byte*)ceTrafficLabel, (word32)XSTRLEN(ceTrafficLabel),
  24452. tv->hashHello1, (word32)hashAlgSz, tv->hashAlg);
  24453. if (ret != 0) break;
  24454. ret = XMEMCMP(tv->clientEarlyTrafficSecret, output, hashAlgSz);
  24455. if (ret != 0) break;
  24456. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24457. secret, (word32)hashAlgSz,
  24458. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24459. (byte*)eExpMasterLabel, (word32)XSTRLEN(eExpMasterLabel),
  24460. tv->hashHello1, (word32)hashAlgSz, tv->hashAlg);
  24461. if (ret != 0) break;
  24462. ret = XMEMCMP(tv->earlyExporterMasterSecret, output, hashAlgSz);
  24463. if (ret != 0) break;
  24464. ret = wc_Tls13_HKDF_Expand_Label(salt, (word32)hashAlgSz,
  24465. secret, (word32)hashAlgSz,
  24466. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24467. (byte*)derivedLabel, (word32)XSTRLEN(derivedLabel),
  24468. hashZero, (word32)hashAlgSz, tv->hashAlg);
  24469. if (ret != 0) break;
  24470. ret = wc_Tls13_HKDF_Extract(secret, salt, (word32)(word32)hashAlgSz,
  24471. (tv->dheSz == 0) ? zeroes : (byte*)tv->dhe,
  24472. tv->dheSz, tv->hashAlg);
  24473. if (ret != 0) break;
  24474. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24475. secret, (word32)hashAlgSz,
  24476. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24477. (byte*)cHsTrafficLabel, (word32)XSTRLEN(cHsTrafficLabel),
  24478. tv->hashHello2, (word32)hashAlgSz, tv->hashAlg);
  24479. if (ret != 0) break;
  24480. ret = XMEMCMP(tv->clientHandshakeTrafficSecret,
  24481. output, hashAlgSz);
  24482. if (ret != 0) break;
  24483. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24484. secret, (word32)hashAlgSz,
  24485. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24486. (byte*)sHsTrafficLabel, (word32)XSTRLEN(sHsTrafficLabel),
  24487. tv->hashHello2, (word32)hashAlgSz, tv->hashAlg);
  24488. if (ret != 0) break;
  24489. ret = XMEMCMP(tv->serverHandshakeTrafficSecret, output, hashAlgSz);
  24490. if (ret != 0) break;
  24491. ret = wc_Tls13_HKDF_Expand_Label(salt, (word32)hashAlgSz,
  24492. secret, (word32)hashAlgSz,
  24493. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24494. (byte*)derivedLabel, (word32)XSTRLEN(derivedLabel),
  24495. hashZero, (word32)hashAlgSz, tv->hashAlg);
  24496. if (ret != 0) break;
  24497. ret = wc_Tls13_HKDF_Extract(secret, salt, (word32)(word32)hashAlgSz,
  24498. zeroes, (word32)(word32)hashAlgSz, tv->hashAlg);
  24499. if (ret != 0) break;
  24500. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24501. secret, (word32)hashAlgSz,
  24502. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24503. (byte*)cAppTrafficLabel, (word32)XSTRLEN(cAppTrafficLabel),
  24504. tv->hashFinished1, (word32)hashAlgSz, tv->hashAlg);
  24505. if (ret != 0) break;
  24506. ret = XMEMCMP(tv->clientApplicationTrafficSecret, output, hashAlgSz);
  24507. if (ret != 0) break;
  24508. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24509. secret, (word32)hashAlgSz,
  24510. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24511. (byte*)sAppTrafficLabel, (word32)XSTRLEN(sAppTrafficLabel),
  24512. tv->hashFinished1, (word32)hashAlgSz, tv->hashAlg);
  24513. if (ret != 0) break;
  24514. ret = XMEMCMP(tv->serverApplicationTrafficSecret, output, hashAlgSz);
  24515. if (ret != 0) break;
  24516. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24517. secret, (word32)hashAlgSz,
  24518. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24519. (byte*)expMasterLabel, (word32)XSTRLEN(expMasterLabel),
  24520. tv->hashFinished1, (word32)hashAlgSz, tv->hashAlg);
  24521. if (ret != 0) break;
  24522. ret = XMEMCMP(tv->exporterMasterSecret, output, hashAlgSz);
  24523. if (ret != 0) break;
  24524. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24525. secret, (word32)hashAlgSz,
  24526. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24527. (byte*)resMasterLabel, (word32)XSTRLEN(resMasterLabel),
  24528. tv->hashFinished2, (word32)hashAlgSz, tv->hashAlg);
  24529. if (ret != 0) break;
  24530. ret = XMEMCMP(tv->resumptionMasterSecret, output, hashAlgSz);
  24531. if (ret != 0) break;
  24532. }
  24533. return ret;
  24534. }
  24535. #endif /* WOLFSSL_TLS13 */
  24536. static const int fiducial2 = WC_TEST_RET_LN; /* source code reference point --
  24537. * see print_fiducials() below.
  24538. */
  24539. #if defined(HAVE_ECC) && defined(HAVE_X963_KDF)
  24540. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t x963kdf_test(void)
  24541. {
  24542. wc_test_ret_t ret;
  24543. byte kek[128];
  24544. #ifndef NO_SHA
  24545. /* SHA-1, COUNT = 0
  24546. * shared secret length: 192
  24547. * SharedInfo length: 0
  24548. * key data length: 128
  24549. */
  24550. WOLFSSL_SMALL_STACK_STATIC const byte Z[] = {
  24551. 0x1c, 0x7d, 0x7b, 0x5f, 0x05, 0x97, 0xb0, 0x3d,
  24552. 0x06, 0xa0, 0x18, 0x46, 0x6e, 0xd1, 0xa9, 0x3e,
  24553. 0x30, 0xed, 0x4b, 0x04, 0xdc, 0x64, 0xcc, 0xdd
  24554. };
  24555. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  24556. 0xbf, 0x71, 0xdf, 0xfd, 0x8f, 0x4d, 0x99, 0x22,
  24557. 0x39, 0x36, 0xbe, 0xb4, 0x6f, 0xee, 0x8c, 0xcc
  24558. };
  24559. #endif
  24560. #ifndef NO_SHA256
  24561. /* SHA-256, COUNT = 3
  24562. * shared secret length: 192
  24563. * SharedInfo length: 0
  24564. * key data length: 128
  24565. */
  24566. WOLFSSL_SMALL_STACK_STATIC const byte Z2[] = {
  24567. 0xd3, 0x8b, 0xdb, 0xe5, 0xc4, 0xfc, 0x16, 0x4c,
  24568. 0xdd, 0x96, 0x7f, 0x63, 0xc0, 0x4f, 0xe0, 0x7b,
  24569. 0x60, 0xcd, 0xe8, 0x81, 0xc2, 0x46, 0x43, 0x8c
  24570. };
  24571. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  24572. 0x5e, 0x67, 0x4d, 0xb9, 0x71, 0xba, 0xc2, 0x0a,
  24573. 0x80, 0xba, 0xd0, 0xd4, 0x51, 0x4d, 0xc4, 0x84
  24574. };
  24575. #endif
  24576. #ifdef WOLFSSL_SHA512
  24577. /* SHA-512, COUNT = 0
  24578. * shared secret length: 192
  24579. * SharedInfo length: 0
  24580. * key data length: 128
  24581. */
  24582. WOLFSSL_SMALL_STACK_STATIC const byte Z3[] = {
  24583. 0x87, 0xfc, 0x0d, 0x8c, 0x44, 0x77, 0x48, 0x5b,
  24584. 0xb5, 0x74, 0xf5, 0xfc, 0xea, 0x26, 0x4b, 0x30,
  24585. 0x88, 0x5d, 0xc8, 0xd9, 0x0a, 0xd8, 0x27, 0x82
  24586. };
  24587. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  24588. 0x94, 0x76, 0x65, 0xfb, 0xb9, 0x15, 0x21, 0x53,
  24589. 0xef, 0x46, 0x02, 0x38, 0x50, 0x6a, 0x02, 0x45
  24590. };
  24591. /* SHA-512, COUNT = 0
  24592. * shared secret length: 521
  24593. * SharedInfo length: 128
  24594. * key data length: 1024
  24595. */
  24596. WOLFSSL_SMALL_STACK_STATIC const byte Z4[] = {
  24597. 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89,
  24598. 0xfa, 0x58, 0xce, 0xad, 0xc0, 0x47, 0x19, 0x7f,
  24599. 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
  24600. 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8,
  24601. 0x15, 0x07, 0x39, 0x2f, 0x1a, 0x86, 0xdd, 0xfd,
  24602. 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
  24603. 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47,
  24604. 0x47, 0xa9, 0xe2, 0xe7, 0x53, 0xf5, 0x5e, 0xf0,
  24605. 0x5a, 0x2d
  24606. };
  24607. WOLFSSL_SMALL_STACK_STATIC const byte info4[] = {
  24608. 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d,
  24609. 0x2b, 0x3a, 0x2f, 0x99, 0x37, 0x89, 0x5d, 0x31
  24610. };
  24611. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  24612. 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76,
  24613. 0x9b, 0x52, 0x26, 0x4b, 0x01, 0x12, 0xb5, 0x85,
  24614. 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
  24615. 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d,
  24616. 0x6e, 0x4d, 0xd2, 0xa5, 0x99, 0xac, 0xce, 0xb3,
  24617. 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
  24618. 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50,
  24619. 0xa5, 0xa8, 0xee, 0xee, 0x20, 0x80, 0x02, 0x26,
  24620. 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
  24621. 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d,
  24622. 0xea, 0x9c, 0x4f, 0xf7, 0x45, 0xa2, 0x5a, 0xc2,
  24623. 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
  24624. 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc,
  24625. 0x0a, 0xb2, 0xaa, 0x8e, 0x2e, 0xfa, 0x7b, 0x17,
  24626. 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
  24627. 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
  24628. };
  24629. #endif
  24630. WOLFSSL_ENTER("x963kdf_test");
  24631. #ifndef NO_SHA
  24632. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, Z, sizeof(Z), NULL, 0,
  24633. kek, sizeof(verify));
  24634. if (ret != 0)
  24635. return WC_TEST_RET_ENC_EC(ret);
  24636. if (XMEMCMP(verify, kek, sizeof(verify)) != 0)
  24637. return WC_TEST_RET_ENC_NC;
  24638. #endif
  24639. #ifndef NO_SHA256
  24640. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, Z2, sizeof(Z2), NULL, 0,
  24641. kek, sizeof(verify2));
  24642. if (ret != 0)
  24643. return WC_TEST_RET_ENC_EC(ret);
  24644. if (XMEMCMP(verify2, kek, sizeof(verify2)) != 0)
  24645. return WC_TEST_RET_ENC_NC;
  24646. #endif
  24647. #ifdef WOLFSSL_SHA512
  24648. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z3, sizeof(Z3), NULL, 0,
  24649. kek, sizeof(verify3));
  24650. if (ret != 0)
  24651. return WC_TEST_RET_ENC_EC(ret);
  24652. if (XMEMCMP(verify3, kek, sizeof(verify3)) != 0)
  24653. return WC_TEST_RET_ENC_NC;
  24654. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z4, sizeof(Z4), info4,
  24655. sizeof(info4), kek, sizeof(verify4));
  24656. if (ret != 0)
  24657. return WC_TEST_RET_ENC_EC(ret);
  24658. if (XMEMCMP(verify4, kek, sizeof(verify4)) != 0)
  24659. return WC_TEST_RET_ENC_NC;
  24660. #endif
  24661. return 0;
  24662. }
  24663. #endif /* HAVE_X963_KDF */
  24664. #if defined(HAVE_HPKE) && \
  24665. (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)) && \
  24666. defined(HAVE_AESGCM)
  24667. static wc_test_ret_t hpke_test_single(Hpke* hpke)
  24668. {
  24669. wc_test_ret_t ret = 0;
  24670. int rngRet = 0;
  24671. WC_RNG rng[1];
  24672. const char* start_text = "this is a test";
  24673. const char* info_text = "info";
  24674. const char* aad_text = "aad";
  24675. byte ciphertext[MAX_HPKE_LABEL_SZ];
  24676. byte plaintext[MAX_HPKE_LABEL_SZ];
  24677. void* receiverKey = NULL;
  24678. void* ephemeralKey = NULL;
  24679. #ifdef WOLFSSL_SMALL_STACK
  24680. byte *pubKey = NULL; /* public key */
  24681. word16 pubKeySz = (word16)HPKE_Npk_MAX;
  24682. #else
  24683. byte pubKey[HPKE_Npk_MAX]; /* public key */
  24684. word16 pubKeySz = (word16)sizeof(pubKey);
  24685. #endif
  24686. rngRet = ret = wc_InitRng(rng);
  24687. if (ret != 0)
  24688. return WC_TEST_RET_ENC_EC(ret);
  24689. #ifdef WOLFSSL_SMALL_STACK
  24690. if (ret == 0) {
  24691. pubKey = (byte *)XMALLOC(pubKeySz, HEAP_HINT,
  24692. DYNAMIC_TYPE_TMP_BUFFER);
  24693. if (pubKey == NULL)
  24694. ret = WC_TEST_RET_ENC_EC(MEMORY_E);
  24695. }
  24696. #endif
  24697. /* generate the keys */
  24698. if (ret == 0) {
  24699. ret = wc_HpkeGenerateKeyPair(hpke, &ephemeralKey, rng);
  24700. if (ret != 0)
  24701. ret = WC_TEST_RET_ENC_EC(ret);
  24702. }
  24703. if (ret == 0) {
  24704. ret = wc_HpkeGenerateKeyPair(hpke, &receiverKey, rng);
  24705. if (ret != 0)
  24706. ret = WC_TEST_RET_ENC_EC(ret);
  24707. }
  24708. /* seal */
  24709. if (ret == 0) {
  24710. ret = wc_HpkeSealBase(hpke, ephemeralKey, receiverKey,
  24711. (byte*)info_text, (word32)XSTRLEN(info_text),
  24712. (byte*)aad_text, (word32)XSTRLEN(aad_text),
  24713. (byte*)start_text, (word32)XSTRLEN(start_text),
  24714. ciphertext);
  24715. if (ret != 0)
  24716. ret = WC_TEST_RET_ENC_EC(ret);
  24717. }
  24718. /* export ephemeral key */
  24719. if (ret == 0) {
  24720. ret = wc_HpkeSerializePublicKey(hpke, ephemeralKey, pubKey, &pubKeySz);
  24721. if (ret != 0)
  24722. ret = WC_TEST_RET_ENC_EC(ret);
  24723. }
  24724. /* open with exported ephemeral key */
  24725. if (ret == 0) {
  24726. ret = wc_HpkeOpenBase(hpke, receiverKey, pubKey, pubKeySz,
  24727. (byte*)info_text, (word32)XSTRLEN(info_text),
  24728. (byte*)aad_text, (word32)XSTRLEN(aad_text),
  24729. ciphertext, (word32)XSTRLEN(start_text),
  24730. plaintext);
  24731. if (ret != 0)
  24732. ret = WC_TEST_RET_ENC_EC(ret);
  24733. }
  24734. if (ret == 0) {
  24735. ret = XMEMCMP(plaintext, start_text, XSTRLEN(start_text));
  24736. if (ret != 0)
  24737. ret = WC_TEST_RET_ENC_NC;
  24738. }
  24739. /* Negative test case with NULL argument */
  24740. if (ret == 0) {
  24741. ret = wc_HpkeGenerateKeyPair(NULL, &receiverKey, rng);
  24742. if (ret != BAD_FUNC_ARG)
  24743. ret = WC_TEST_RET_ENC_EC(ret);
  24744. else
  24745. ret = 0;
  24746. }
  24747. if (ret == 0) {
  24748. ret = wc_HpkeGenerateKeyPair(hpke, NULL, rng);
  24749. if (ret != BAD_FUNC_ARG)
  24750. ret = WC_TEST_RET_ENC_EC(ret);
  24751. else
  24752. ret = 0;
  24753. }
  24754. if (ret == 0) {
  24755. ret = wc_HpkeGenerateKeyPair(hpke, &receiverKey, NULL);
  24756. if (ret != BAD_FUNC_ARG)
  24757. ret = WC_TEST_RET_ENC_EC(ret);
  24758. else
  24759. ret = 0;
  24760. }
  24761. if (ephemeralKey != NULL)
  24762. wc_HpkeFreeKey(hpke, hpke->kem, ephemeralKey, hpke->heap);
  24763. if (receiverKey != NULL)
  24764. wc_HpkeFreeKey(hpke, hpke->kem, receiverKey, hpke->heap);
  24765. #ifdef WOLFSSL_SMALL_STACK
  24766. if (pubKey != NULL)
  24767. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24768. #endif
  24769. if (rngRet == 0)
  24770. wc_FreeRng(rng);
  24771. return ret;
  24772. }
  24773. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hpke_test(void)
  24774. {
  24775. wc_test_ret_t ret = 0;
  24776. Hpke hpke[1];
  24777. WOLFSSL_ENTER("hpke_test");
  24778. #if defined(HAVE_ECC)
  24779. #if defined(WOLFSSL_SHA224) || !defined(NO_SHA256)
  24780. /* p256 */
  24781. ret = wc_HpkeInit(hpke, DHKEM_P256_HKDF_SHA256, HKDF_SHA256,
  24782. HPKE_AES_128_GCM, NULL);
  24783. if (ret != 0)
  24784. return WC_TEST_RET_ENC_EC(ret);
  24785. ret = hpke_test_single(hpke);
  24786. if (ret != 0)
  24787. return ret;
  24788. #endif
  24789. #if defined(WOLFSSL_SHA384) && \
  24790. (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  24791. /* p384 */
  24792. ret = wc_HpkeInit(hpke, DHKEM_P384_HKDF_SHA384, HKDF_SHA384,
  24793. HPKE_AES_128_GCM, NULL);
  24794. if (ret != 0)
  24795. return WC_TEST_RET_ENC_EC(ret);
  24796. ret = hpke_test_single(hpke);
  24797. if (ret != 0)
  24798. return ret;
  24799. #endif
  24800. #if (defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)) && \
  24801. (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  24802. /* p521 */
  24803. ret = wc_HpkeInit(hpke, DHKEM_P521_HKDF_SHA512, HKDF_SHA512,
  24804. HPKE_AES_128_GCM, NULL);
  24805. if (ret != 0)
  24806. return WC_TEST_RET_ENC_EC(ret);
  24807. ret = hpke_test_single(hpke);
  24808. if (ret != 0)
  24809. return ret;
  24810. #endif
  24811. #endif
  24812. #if defined(HAVE_CURVE25519)
  24813. /* test with curve25519 and aes256 */
  24814. ret = wc_HpkeInit(hpke, DHKEM_X25519_HKDF_SHA256, HKDF_SHA256,
  24815. HPKE_AES_256_GCM, NULL);
  24816. if (ret != 0)
  24817. return WC_TEST_RET_ENC_EC(ret);
  24818. ret = hpke_test_single(hpke);
  24819. if (ret != 0)
  24820. return ret;
  24821. #endif
  24822. #if defined(HAVE_CURVE448) && \
  24823. (defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512))
  24824. /* test with curve448 and aes256 */
  24825. ret = wc_HpkeInit(hpke, DHKEM_X448_HKDF_SHA512, HKDF_SHA512,
  24826. HPKE_AES_256_GCM, NULL);
  24827. /* HPKE does not support X448 yet, so expect failure */
  24828. if (ret != BAD_FUNC_ARG)
  24829. return WC_TEST_RET_ENC_EC(ret);
  24830. ret = hpke_test_single(hpke);
  24831. /* HPKE does not support X448 yet, so expect failure */
  24832. if (WC_TEST_RET_DEC_EC(ret) != BAD_FUNC_ARG)
  24833. return ret;
  24834. ret = 0; /* reset error code */
  24835. #endif
  24836. /* TODO: HPKE chacha20 is not implemented */
  24837. return ret;
  24838. }
  24839. #endif /* HAVE_HPKE && HAVE_ECC && HAVE_AESGCM */
  24840. #if defined(WC_SRTP_KDF)
  24841. typedef struct Srtp_Kdf_Tv {
  24842. const unsigned char* key;
  24843. word32 keySz;
  24844. const unsigned char* salt;
  24845. word32 saltSz;
  24846. int kdfIdx;
  24847. const unsigned char* index;
  24848. const unsigned char* ke;
  24849. const unsigned char* ka;
  24850. const unsigned char* ks;
  24851. const unsigned char* index_c;
  24852. const unsigned char* ke_c;
  24853. const unsigned char* ka_c;
  24854. const unsigned char* ks_c;
  24855. word32 keSz;
  24856. word32 kaSz;
  24857. word32 ksSz;
  24858. } Srtp_Kdf_Tv;
  24859. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srtpkdf_test(void)
  24860. {
  24861. wc_test_ret_t ret = 0;
  24862. /* 128-bit key, kdrIdx = -1 */
  24863. WOLFSSL_SMALL_STACK_STATIC const byte key_0[] = {
  24864. 0xc4, 0x80, 0x9f, 0x6d, 0x36, 0x98, 0x88, 0x72,
  24865. 0x8e, 0x26, 0xad, 0xb5, 0x32, 0x12, 0x98, 0x90
  24866. };
  24867. WOLFSSL_SMALL_STACK_STATIC const byte salt_0[] = {
  24868. 0x0e, 0x23, 0x00, 0x6c, 0x6c, 0x04, 0x4f, 0x56,
  24869. 0x62, 0x40, 0x0e, 0x9d, 0x1b, 0xd6
  24870. };
  24871. WOLFSSL_SMALL_STACK_STATIC const byte index_0[] = {
  24872. 0x48, 0x71, 0x65, 0x64, 0x9c, 0xca
  24873. };
  24874. WOLFSSL_SMALL_STACK_STATIC const byte ke_0[] = {
  24875. 0xdc, 0x38, 0x21, 0x92, 0xab, 0x65, 0x10, 0x8a,
  24876. 0x86, 0xb2, 0x59, 0xb6, 0x1b, 0x3a, 0xf4, 0x6f
  24877. };
  24878. WOLFSSL_SMALL_STACK_STATIC const byte ka_0[] = {
  24879. 0xb8, 0x39, 0x37, 0xfb, 0x32, 0x17, 0x92, 0xee,
  24880. 0x87, 0xb7, 0x88, 0x19, 0x3b, 0xe5, 0xa4, 0xe3,
  24881. 0xbd, 0x32, 0x6e, 0xe4
  24882. };
  24883. WOLFSSL_SMALL_STACK_STATIC const byte ks_0[] = {
  24884. 0xf1, 0xc0, 0x35, 0xc0, 0x0b, 0x5a, 0x54, 0xa6,
  24885. 0x16, 0x92, 0xc0, 0x16, 0x27, 0x6c
  24886. };
  24887. WOLFSSL_SMALL_STACK_STATIC const byte index_c_0[] = {
  24888. 0x56, 0xf3, 0xf1, 0x97
  24889. };
  24890. WOLFSSL_SMALL_STACK_STATIC const byte ke_c_0[] = {
  24891. 0xab, 0x5b, 0xe0, 0xb4, 0x56, 0x23, 0x5d, 0xcf,
  24892. 0x77, 0xd5, 0x08, 0x69, 0x29, 0xba, 0xfb, 0x38
  24893. };
  24894. WOLFSSL_SMALL_STACK_STATIC const byte ka_c_0[] = {
  24895. 0xc5, 0x2f, 0xde, 0x0b, 0x80, 0xb0, 0xf0, 0xba,
  24896. 0xd8, 0xd1, 0x56, 0x45, 0xcb, 0x86, 0xe7, 0xc7,
  24897. 0xc3, 0xd8, 0x77, 0x0e
  24898. };
  24899. WOLFSSL_SMALL_STACK_STATIC const byte ks_c_0[] = {
  24900. 0xde, 0xb5, 0xf8, 0x5f, 0x81, 0x33, 0x6a, 0x96,
  24901. 0x5e, 0xd3, 0x2b, 0xb7, 0xed, 0xe8
  24902. };
  24903. /* 192-bit key, kdrIdx = 0 */
  24904. WOLFSSL_SMALL_STACK_STATIC const byte key_1[] = {
  24905. 0xbb, 0x04, 0x5b, 0x1f, 0x53, 0xc6, 0x93, 0x2c,
  24906. 0x2b, 0xa6, 0x88, 0xf5, 0xe3, 0xf2, 0x24, 0x70,
  24907. 0xe1, 0x7d, 0x7d, 0xec, 0x8a, 0x93, 0x4d, 0xf2
  24908. };
  24909. WOLFSSL_SMALL_STACK_STATIC const byte salt_1[] = {
  24910. 0xe7, 0x22, 0xab, 0x92, 0xfc, 0x7c, 0x89, 0xb6,
  24911. 0x53, 0x8a, 0xf9, 0x3c, 0xb9, 0x52
  24912. };
  24913. WOLFSSL_SMALL_STACK_STATIC const byte index_1[] = {
  24914. 0xd7, 0x87, 0x8f, 0x33, 0xb1, 0x76
  24915. };
  24916. WOLFSSL_SMALL_STACK_STATIC const byte ke_1[] = {
  24917. 0x2c, 0xc8, 0x3e, 0x54, 0xb2, 0x33, 0x89, 0xb3,
  24918. 0x71, 0x65, 0x0f, 0x51, 0x61, 0x65, 0xe4, 0x93,
  24919. 0x07, 0x4e, 0xb3, 0x47, 0xba, 0x2d, 0x60, 0x60
  24920. };
  24921. WOLFSSL_SMALL_STACK_STATIC const byte ka_1[] = {
  24922. 0x2e, 0x80, 0xe4, 0x82, 0x55, 0xa2, 0xbe, 0x6d,
  24923. 0xe0, 0x46, 0xcc, 0xc1, 0x75, 0x78, 0x6e, 0x78,
  24924. 0xd1, 0xd1, 0x47, 0x08
  24925. };
  24926. WOLFSSL_SMALL_STACK_STATIC const byte ks_1[] = {
  24927. 0xe0, 0xc1, 0xe6, 0xaf, 0x1e, 0x8d, 0x8c, 0xfe,
  24928. 0xe5, 0x60, 0x70, 0xb5, 0xe6, 0xea
  24929. };
  24930. WOLFSSL_SMALL_STACK_STATIC const byte index_c_1[] = {
  24931. 0x40, 0xbf, 0xd4, 0xa9
  24932. };
  24933. WOLFSSL_SMALL_STACK_STATIC const byte ke_c_1[] = {
  24934. 0x94, 0x0f, 0x55, 0xce, 0x58, 0xd8, 0x16, 0x65,
  24935. 0xf0, 0xfa, 0x46, 0x40, 0x0c, 0xda, 0xb1, 0x11,
  24936. 0x9e, 0x69, 0xa0, 0x93, 0x4e, 0xd7, 0xf2, 0x84
  24937. };
  24938. WOLFSSL_SMALL_STACK_STATIC const byte ka_c_1[] = {
  24939. 0xf5, 0x41, 0x6f, 0xc2, 0x65, 0xc5, 0xb3, 0xef,
  24940. 0xbb, 0x22, 0xc8, 0xfc, 0x6b, 0x00, 0x14, 0xb2,
  24941. 0xf3, 0x3b, 0x8e, 0x29
  24942. };
  24943. WOLFSSL_SMALL_STACK_STATIC const byte ks_c_1[] = {
  24944. 0x35, 0xb7, 0x42, 0x43, 0xf0, 0x01, 0x01, 0xb4,
  24945. 0x68, 0xa1, 0x28, 0x80, 0x37, 0xf0
  24946. };
  24947. /* 256-bit key, kdrIdx = 1 */
  24948. WOLFSSL_SMALL_STACK_STATIC const byte key_2[] = {
  24949. 0x10, 0x38, 0x0a, 0xcd, 0xd6, 0x47, 0xab, 0xee,
  24950. 0xc0, 0xd4, 0x44, 0xf4, 0x7e, 0x51, 0x36, 0x02,
  24951. 0x79, 0xa8, 0x94, 0x80, 0x35, 0x40, 0xed, 0x50,
  24952. 0xf4, 0x45, 0x30, 0x3d, 0xb5, 0xf0, 0x2b, 0xbb
  24953. };
  24954. WOLFSSL_SMALL_STACK_STATIC const byte salt_2[] = {
  24955. 0xc7, 0x31, 0xf2, 0xc8, 0x40, 0x43, 0xb8, 0x74,
  24956. 0x8a, 0x61, 0x84, 0x7a, 0x25, 0x8a
  24957. };
  24958. WOLFSSL_SMALL_STACK_STATIC const byte index_2[] = {
  24959. 0x82, 0xf1, 0x84, 0x8c, 0xac, 0x42
  24960. };
  24961. WOLFSSL_SMALL_STACK_STATIC const byte ke_2[] = {
  24962. 0xb2, 0x26, 0x60, 0xaf, 0x08, 0x23, 0x14, 0x98,
  24963. 0x91, 0xde, 0x5d, 0x87, 0x95, 0x61, 0xca, 0x8f,
  24964. 0x0e, 0xce, 0xfb, 0x68, 0x4d, 0xd6, 0x28, 0xcb,
  24965. 0x28, 0xe2, 0x27, 0x20, 0x2d, 0xff, 0x64, 0xbb
  24966. };
  24967. WOLFSSL_SMALL_STACK_STATIC const byte ka_2[] = {
  24968. 0x12, 0x6f, 0x52, 0xe8, 0x07, 0x7f, 0x07, 0x84,
  24969. 0xa0, 0x61, 0x96, 0xf8, 0xee, 0x4d, 0x05, 0x57,
  24970. 0x65, 0xc7, 0x50, 0xc1
  24971. };
  24972. WOLFSSL_SMALL_STACK_STATIC const byte ks_2[] = {
  24973. 0x18, 0x5a, 0x59, 0xe5, 0x91, 0x4d, 0xc9, 0x6c,
  24974. 0xfa, 0x5b, 0x36, 0x06, 0x8c, 0x9a
  24975. };
  24976. WOLFSSL_SMALL_STACK_STATIC const byte index_c_2[] = {
  24977. 0x31, 0x2d, 0x58, 0x15
  24978. };
  24979. WOLFSSL_SMALL_STACK_STATIC const byte ke_c_2[] = {
  24980. 0x14, 0xf2, 0xc8, 0x25, 0x02, 0x79, 0x22, 0xa1,
  24981. 0x96, 0xb6, 0xf7, 0x07, 0x76, 0xa6, 0xa3, 0xc4,
  24982. 0x37, 0xdf, 0xa0, 0xf8, 0x78, 0x93, 0x2c, 0xfa,
  24983. 0xea, 0x35, 0xf0, 0xf3, 0x3f, 0x32, 0x6e, 0xfd
  24984. };
  24985. WOLFSSL_SMALL_STACK_STATIC const byte ka_c_2[] = {
  24986. 0x6e, 0x3d, 0x4a, 0x99, 0xea, 0x2f, 0x9d, 0x13,
  24987. 0x4a, 0x1e, 0x71, 0x2e, 0x15, 0xc0, 0xca, 0xb6,
  24988. 0x35, 0x78, 0xdf, 0xa4
  24989. };
  24990. WOLFSSL_SMALL_STACK_STATIC const byte ks_c_2[] = {
  24991. 0xae, 0xe4, 0xec, 0x18, 0x31, 0x70, 0x5d, 0x3f,
  24992. 0xdc, 0x97, 0x89, 0x88, 0xfd, 0xff
  24993. };
  24994. /* 128-bit key, kdrIdx = 8 */
  24995. WOLFSSL_SMALL_STACK_STATIC const byte key_3[] = {
  24996. 0x36, 0xb4, 0xde, 0xcb, 0x2e, 0x51, 0x23, 0x76,
  24997. 0xe0, 0x27, 0x7e, 0x3e, 0xc8, 0xf6, 0x54, 0x04
  24998. };
  24999. WOLFSSL_SMALL_STACK_STATIC const byte salt_3[] = {
  25000. 0x73, 0x26, 0xf4, 0x3f, 0xc0, 0xd9, 0xc6, 0xe3,
  25001. 0x2f, 0x92, 0x7d, 0x46, 0x12, 0x76
  25002. };
  25003. WOLFSSL_SMALL_STACK_STATIC const byte index_3[] = {
  25004. 0x44, 0x73, 0xb2, 0x2d, 0xb2, 0x60
  25005. };
  25006. WOLFSSL_SMALL_STACK_STATIC const byte ke_3[] = {
  25007. 0x79, 0x91, 0x3d, 0x7b, 0x20, 0x5d, 0xea, 0xe2,
  25008. 0xeb, 0x46, 0x89, 0x68, 0x5a, 0x06, 0x73, 0x74
  25009. };
  25010. WOLFSSL_SMALL_STACK_STATIC const byte ka_3[] = {
  25011. 0x2d, 0x2e, 0x97, 0x4e, 0x76, 0x8c, 0x62, 0xa6,
  25012. 0x57, 0x80, 0x13, 0x42, 0x0b, 0x51, 0xa7, 0x66,
  25013. 0xea, 0x31, 0x24, 0xe6
  25014. };
  25015. WOLFSSL_SMALL_STACK_STATIC const byte ks_3[] = {
  25016. 0xcc, 0xd7, 0x31, 0xf6, 0x3b, 0xf3, 0x89, 0x8a,
  25017. 0x5b, 0x7b, 0xb5, 0x8b, 0x4c, 0x3f
  25018. };
  25019. WOLFSSL_SMALL_STACK_STATIC const byte index_c_3[] = {
  25020. 0x4a, 0x7d, 0xaa, 0x85
  25021. };
  25022. WOLFSSL_SMALL_STACK_STATIC const byte ke_c_3[] = {
  25023. 0x34, 0x99, 0x71, 0xfe, 0x12, 0x93, 0xae, 0x8c,
  25024. 0x4a, 0xe9, 0x84, 0xe4, 0x93, 0x53, 0x63, 0x88
  25025. };
  25026. WOLFSSL_SMALL_STACK_STATIC const byte ka_c_3[] = {
  25027. 0xa4, 0x53, 0x5e, 0x0a, 0x9c, 0xf2, 0xce, 0x13,
  25028. 0xef, 0x7a, 0x13, 0xee, 0x0a, 0xef, 0xba, 0x17,
  25029. 0x05, 0x18, 0xe3, 0xed
  25030. };
  25031. WOLFSSL_SMALL_STACK_STATIC const byte ks_c_3[] = {
  25032. 0xe1, 0x29, 0x4f, 0x61, 0x30, 0x3c, 0x4d, 0x46,
  25033. 0x5f, 0x5c, 0x81, 0x3c, 0x38, 0xb6
  25034. };
  25035. /* SRTCP w/ 48-bit idx - KDR 0 (-1) */
  25036. WOLFSSL_SMALL_STACK_STATIC const byte mk48_1[] = {
  25037. 0xFF, 0xB6, 0xCB, 0x09, 0x71, 0x3F, 0x63, 0x4D,
  25038. 0x7F, 0x42, 0xED, 0xA8, 0x12, 0x81, 0x50, 0xE6
  25039. };
  25040. WOLFSSL_SMALL_STACK_STATIC const byte ms48_1[] = {
  25041. 0x1F, 0x04, 0x76, 0xC8, 0x7F, 0x58, 0x23, 0xEF,
  25042. 0xD3, 0x57, 0xB2, 0xBD, 0xF1, 0x32
  25043. };
  25044. WOLFSSL_SMALL_STACK_STATIC const byte srtcp48idx_1[] = {
  25045. 0x00, 0x00, 0x08, 0x56, 0xBC, 0x39
  25046. };
  25047. WOLFSSL_SMALL_STACK_STATIC const byte srtcpKe_48_1[] = {
  25048. 0xD2, 0xC3, 0xF3, 0x49, 0x00, 0x1A, 0x18, 0x0F,
  25049. 0xB6, 0x05, 0x5A, 0x5A, 0x67, 0x8E, 0xE5, 0xB2
  25050. };
  25051. WOLFSSL_SMALL_STACK_STATIC const byte srtcpKa_48_1[] = {
  25052. 0x8D, 0x54, 0xBE, 0xB5, 0x7B, 0x7F, 0x7A, 0xAB,
  25053. 0xF5, 0x46, 0xCE, 0x5B, 0x45, 0x69, 0x4A, 0x75,
  25054. 0x81, 0x2A, 0xE2, 0xCB
  25055. };
  25056. WOLFSSL_SMALL_STACK_STATIC const byte srtcpKs_48_1[] = {
  25057. 0x76, 0x3C, 0x97, 0x6A, 0x45, 0x31, 0xA7, 0x79,
  25058. 0x3C, 0x28, 0x4A, 0xA6, 0x82, 0x03
  25059. };
  25060. /* SRTCP w/ 48-bit idx - KDR 19 */
  25061. WOLFSSL_SMALL_STACK_STATIC const byte mk48_2[] = {
  25062. 0xBD, 0x1D, 0x71, 0x6B, 0xDA, 0x28, 0xE3, 0xFC,
  25063. 0xA5, 0xA0, 0x66, 0x3F, 0x2E, 0x34, 0xA8, 0x58
  25064. };
  25065. WOLFSSL_SMALL_STACK_STATIC const byte ms48_2[] = {
  25066. 0x79, 0x06, 0xE5, 0xAB, 0x5C, 0x2B, 0x1B, 0x69,
  25067. 0xFA, 0xEE, 0xD2, 0x29, 0x57, 0x3C
  25068. };
  25069. WOLFSSL_SMALL_STACK_STATIC const byte srtcp48idx_2[] = {
  25070. 0x00, 0x00, 0x59, 0xD0, 0xC2, 0xE8
  25071. };
  25072. WOLFSSL_SMALL_STACK_STATIC const byte srtcpKe_48_2[] = {
  25073. 0xB9, 0xD7, 0xAD, 0xD8, 0x90, 0x94, 0xC2, 0x92,
  25074. 0xA5, 0x04, 0x87, 0xC4, 0x8C, 0xEF, 0xE2, 0xA3
  25075. };
  25076. WOLFSSL_SMALL_STACK_STATIC const byte srtcpKa_48_2[] = {
  25077. 0x07, 0xD5, 0xC4, 0xD2, 0x06, 0xFB, 0x63, 0x15,
  25078. 0xC2, 0x9C, 0x7F, 0x55, 0xD1, 0x16, 0x5C, 0xB5,
  25079. 0xB7, 0x44, 0x54, 0xBD
  25080. };
  25081. WOLFSSL_SMALL_STACK_STATIC const byte srtcpKs_48_2[] = {
  25082. 0x0C, 0x5E, 0x53, 0xC1, 0xD0, 0x75, 0xAD, 0x65,
  25083. 0xBF, 0x51, 0x74, 0x50, 0x89, 0xD7
  25084. };
  25085. int kdr_48_1 = -1;
  25086. int kdr_48_2 = 19;
  25087. #define SRTP_TV_CNT 4
  25088. Srtp_Kdf_Tv tv[SRTP_TV_CNT] = {
  25089. { key_0, (word32)sizeof(key_0), salt_0, (word32)sizeof(salt_0), -1,
  25090. index_0, ke_0, ka_0, ks_0, index_c_0, ke_c_0, ka_c_0, ks_c_0,
  25091. 16, 20, 14 },
  25092. { key_1, (word32)sizeof(key_1), salt_1, (word32)sizeof(salt_1), 0,
  25093. index_1, ke_1, ka_1, ks_1, index_c_1, ke_c_1, ka_c_1, ks_c_1,
  25094. 24, 20, 14 },
  25095. { key_2, (word32)sizeof(key_2), salt_2, (word32)sizeof(salt_2), 1,
  25096. index_2, ke_2, ka_2, ks_2, index_c_2, ke_c_2, ka_c_2, ks_c_2,
  25097. 32, 20, 14 },
  25098. { key_3, (word32)sizeof(key_3), salt_3, (word32)sizeof(salt_3), 8,
  25099. index_3, ke_3, ka_3, ks_3, index_c_3, ke_c_3, ka_c_3, ks_c_3,
  25100. 16, 20, 14 },
  25101. };
  25102. int i;
  25103. int idx;
  25104. unsigned char keyE[32];
  25105. unsigned char keyA[20];
  25106. unsigned char keyS[14];
  25107. WOLFSSL_ENTER("srtpkdf_test");
  25108. for (i = 0; (ret == 0) && (i < SRTP_TV_CNT); i++) {
  25109. #ifndef WOLFSSL_AES_128
  25110. if (tv[i].keySz == AES_128_KEY_SIZE) {
  25111. continue;
  25112. }
  25113. #endif
  25114. #ifndef WOLFSSL_AES_192
  25115. if (tv[i].keySz == AES_192_KEY_SIZE) {
  25116. continue;
  25117. }
  25118. #endif
  25119. #ifndef WOLFSSL_AES_256
  25120. if (tv[i].keySz == AES_256_KEY_SIZE) {
  25121. continue;
  25122. }
  25123. #endif
  25124. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25125. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25126. keyS, tv[i].ksSz);
  25127. if (ret != 0)
  25128. return WC_TEST_RET_ENC_EC(ret);
  25129. if (XMEMCMP(keyE, tv[i].ke, tv[i].keSz) != 0)
  25130. return WC_TEST_RET_ENC_NC;
  25131. if (XMEMCMP(keyA, tv[i].ka, tv[i].kaSz) != 0)
  25132. return WC_TEST_RET_ENC_NC;
  25133. if (XMEMCMP(keyS, tv[i].ks, tv[i].ksSz) != 0)
  25134. return WC_TEST_RET_ENC_NC;
  25135. ret = wc_SRTP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  25136. tv[i].saltSz, tv[i].kdfIdx, tv[i].index, WC_SRTP_LABEL_ENCRYPTION,
  25137. keyE, tv[i].keSz);
  25138. if (ret != 0)
  25139. return WC_TEST_RET_ENC_EC(ret);
  25140. if (XMEMCMP(keyE, tv[i].ke, tv[i].keSz) != 0)
  25141. return WC_TEST_RET_ENC_NC;
  25142. ret = wc_SRTP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  25143. tv[i].saltSz, tv[i].kdfIdx, tv[i].index, WC_SRTP_LABEL_MSG_AUTH,
  25144. keyA, tv[i].kaSz);
  25145. if (ret != 0)
  25146. return WC_TEST_RET_ENC_EC(ret);
  25147. if (XMEMCMP(keyA, tv[i].ka, tv[i].kaSz) != 0)
  25148. return WC_TEST_RET_ENC_NC;
  25149. ret = wc_SRTP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  25150. tv[i].saltSz, tv[i].kdfIdx, tv[i].index, WC_SRTP_LABEL_SALT, keyS,
  25151. tv[i].ksSz);
  25152. if (ret != 0)
  25153. return WC_TEST_RET_ENC_EC(ret);
  25154. if (XMEMCMP(keyS, tv[i].ks, tv[i].ksSz) != 0)
  25155. return WC_TEST_RET_ENC_NC;
  25156. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25157. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25158. keyS, tv[i].ksSz);
  25159. if (ret != 0)
  25160. return WC_TEST_RET_ENC_EC(ret);
  25161. if (XMEMCMP(keyE, tv[i].ke_c, tv[i].keSz) != 0)
  25162. return WC_TEST_RET_ENC_NC;
  25163. if (XMEMCMP(keyA, tv[i].ka_c, tv[i].kaSz) != 0)
  25164. return WC_TEST_RET_ENC_NC;
  25165. if (XMEMCMP(keyS, tv[i].ks_c, tv[i].ksSz) != 0)
  25166. return WC_TEST_RET_ENC_NC;
  25167. ret = wc_SRTCP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  25168. tv[i].saltSz, tv[i].kdfIdx, tv[i].index_c,
  25169. WC_SRTCP_LABEL_ENCRYPTION, keyE, tv[i].keSz);
  25170. if (ret != 0)
  25171. return WC_TEST_RET_ENC_EC(ret);
  25172. if (XMEMCMP(keyE, tv[i].ke_c, tv[i].keSz) != 0)
  25173. return WC_TEST_RET_ENC_NC;
  25174. ret = wc_SRTCP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  25175. tv[i].saltSz, tv[i].kdfIdx, tv[i].index_c, WC_SRTCP_LABEL_MSG_AUTH,
  25176. keyA, tv[i].kaSz);
  25177. if (ret != 0)
  25178. return WC_TEST_RET_ENC_EC(ret);
  25179. if (XMEMCMP(keyA, tv[i].ka_c, tv[i].kaSz) != 0)
  25180. return WC_TEST_RET_ENC_NC;
  25181. ret = wc_SRTCP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  25182. tv[i].saltSz, tv[i].kdfIdx, tv[i].index_c, WC_SRTCP_LABEL_SALT,
  25183. keyS, tv[i].ksSz);
  25184. if (ret != 0)
  25185. return WC_TEST_RET_ENC_EC(ret);
  25186. if (XMEMCMP(keyS, tv[i].ks_c, tv[i].ksSz) != 0)
  25187. return WC_TEST_RET_ENC_NC;
  25188. }
  25189. #ifdef WOLFSSL_AES_128
  25190. i = 0;
  25191. #elif defined(WOLFSSL_AES_192)
  25192. i = 1;
  25193. #else
  25194. i = 2;
  25195. #endif
  25196. ret = wc_SRTP_KDF(tv[i].key, 33, tv[i].salt, tv[i].saltSz,
  25197. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25198. keyS, tv[i].ksSz);
  25199. if (ret != BAD_FUNC_ARG)
  25200. return WC_TEST_RET_ENC_EC(ret);
  25201. ret = wc_SRTCP_KDF(tv[i].key, 33, tv[i].salt, tv[i].saltSz,
  25202. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25203. keyS, tv[i].ksSz);
  25204. if (ret != BAD_FUNC_ARG)
  25205. return WC_TEST_RET_ENC_EC(ret);
  25206. ret = wc_SRTP_KDF(tv[i].key, 15, tv[i].salt, tv[i].saltSz,
  25207. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25208. keyS, tv[i].ksSz);
  25209. if (ret != BAD_FUNC_ARG)
  25210. return WC_TEST_RET_ENC_EC(ret);
  25211. ret = wc_SRTCP_KDF(tv[i].key, 15, tv[i].salt, tv[i].saltSz,
  25212. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25213. keyS, tv[i].ksSz);
  25214. if (ret != BAD_FUNC_ARG)
  25215. return WC_TEST_RET_ENC_EC(ret);
  25216. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, 15,
  25217. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25218. keyS, tv[i].ksSz);
  25219. if (ret != BAD_FUNC_ARG)
  25220. return WC_TEST_RET_ENC_EC(ret);
  25221. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, 15,
  25222. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25223. keyS, tv[i].ksSz);
  25224. if (ret != BAD_FUNC_ARG)
  25225. return WC_TEST_RET_ENC_EC(ret);
  25226. ret = wc_SRTP_KDF(NULL, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25227. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25228. keyS, tv[i].ksSz);
  25229. if (ret != BAD_FUNC_ARG)
  25230. return WC_TEST_RET_ENC_EC(ret);
  25231. ret = wc_SRTCP_KDF(NULL, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25232. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25233. keyS, tv[i].ksSz);
  25234. if (ret != BAD_FUNC_ARG)
  25235. return WC_TEST_RET_ENC_EC(ret);
  25236. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, NULL, tv[i].saltSz,
  25237. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25238. keyS, tv[i].ksSz);
  25239. if (ret != BAD_FUNC_ARG)
  25240. return WC_TEST_RET_ENC_EC(ret);
  25241. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, NULL, tv[i].saltSz,
  25242. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25243. keyS, tv[i].ksSz);
  25244. if (ret != BAD_FUNC_ARG)
  25245. return WC_TEST_RET_ENC_EC(ret);
  25246. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25247. 25, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25248. keyS, tv[i].ksSz);
  25249. if (ret != BAD_FUNC_ARG)
  25250. return WC_TEST_RET_ENC_EC(ret);
  25251. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25252. 25, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25253. keyS, tv[i].ksSz);
  25254. if (ret != BAD_FUNC_ARG)
  25255. return WC_TEST_RET_ENC_EC(ret);
  25256. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25257. -2, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25258. keyS, tv[i].ksSz);
  25259. if (ret != BAD_FUNC_ARG)
  25260. return WC_TEST_RET_ENC_EC(ret);
  25261. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25262. -2, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25263. keyS, tv[i].ksSz);
  25264. if (ret != BAD_FUNC_ARG)
  25265. return WC_TEST_RET_ENC_EC(ret);
  25266. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25267. tv[i].kdfIdx, tv[i].index, NULL, tv[i].keSz, keyA, tv[i].kaSz,
  25268. keyS, tv[i].ksSz);
  25269. if (ret != 0)
  25270. return WC_TEST_RET_ENC_EC(ret);
  25271. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25272. tv[i].kdfIdx, tv[i].index_c, NULL, tv[i].keSz, keyA, tv[i].kaSz,
  25273. keyS, tv[i].ksSz);
  25274. if (ret != 0)
  25275. return WC_TEST_RET_ENC_EC(ret);
  25276. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25277. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, NULL, tv[i].kaSz,
  25278. keyS, tv[i].ksSz);
  25279. if (ret != 0)
  25280. return WC_TEST_RET_ENC_EC(ret);
  25281. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25282. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, NULL, tv[i].kaSz,
  25283. keyS, tv[i].ksSz);
  25284. if (ret != 0)
  25285. return WC_TEST_RET_ENC_EC(ret);
  25286. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25287. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25288. NULL, tv[i].ksSz);
  25289. if (ret != 0)
  25290. return WC_TEST_RET_ENC_EC(ret);
  25291. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25292. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25293. NULL, tv[i].ksSz);
  25294. if (ret != 0)
  25295. return WC_TEST_RET_ENC_EC(ret);
  25296. idx = wc_SRTP_KDF_kdr_to_idx(0);
  25297. if (idx != -1)
  25298. return WC_TEST_RET_ENC_NC;
  25299. for (i = 0; i < 32; i++) {
  25300. word32 kdr = 1U << i;
  25301. idx = wc_SRTP_KDF_kdr_to_idx(kdr);
  25302. if (idx != i)
  25303. return WC_TEST_RET_ENC_NC;
  25304. }
  25305. /* SRTCP w/ 48-bit IDX, 128-bit key test */
  25306. if (i == 0) {
  25307. ret = wc_SRTCP_KDF_ex(mk48_1, (word32)sizeof(mk48_1),
  25308. ms48_1, (word32)sizeof(ms48_1),
  25309. kdr_48_1, srtcp48idx_1, keyE, tv[i].keSz,
  25310. keyA, tv[i].kaSz, keyS, tv[i].ksSz,
  25311. WC_SRTCP_48BIT_IDX);
  25312. if (ret != 0)
  25313. return WC_TEST_RET_ENC_EC(ret);
  25314. if (XMEMCMP(keyE, srtcpKe_48_1, tv[i].keSz) != 0)
  25315. return WC_TEST_RET_ENC_NC;
  25316. if (XMEMCMP(keyA, srtcpKa_48_1, tv[i].kaSz) != 0)
  25317. return WC_TEST_RET_ENC_NC;
  25318. if (XMEMCMP(keyS, srtcpKs_48_1, tv[i].ksSz) != 0)
  25319. return WC_TEST_RET_ENC_NC;
  25320. ret = wc_SRTCP_KDF_ex(mk48_2, (word32)sizeof(mk48_2),
  25321. ms48_2, (word32)sizeof(ms48_2),
  25322. kdr_48_2, srtcp48idx_2, keyE, tv[i].keSz,
  25323. keyA, tv[i].kaSz, keyS, tv[i].ksSz,
  25324. WC_SRTCP_48BIT_IDX);
  25325. if (ret != 0)
  25326. return WC_TEST_RET_ENC_EC(ret);
  25327. if (XMEMCMP(keyE, srtcpKe_48_2, tv[i].keSz) != 0)
  25328. return WC_TEST_RET_ENC_NC;
  25329. if (XMEMCMP(keyA, srtcpKa_48_2, tv[i].kaSz) != 0)
  25330. return WC_TEST_RET_ENC_NC;
  25331. if (XMEMCMP(keyS, srtcpKs_48_2, tv[i].ksSz) != 0)
  25332. return WC_TEST_RET_ENC_NC;
  25333. }
  25334. return 0;
  25335. }
  25336. #endif
  25337. #ifdef HAVE_ECC
  25338. /* size to use for ECC key gen tests */
  25339. #ifndef ECC_KEYGEN_SIZE
  25340. #if !defined(NO_ECC256) || defined(WOLFSSL_SM2)
  25341. #define ECC_KEYGEN_SIZE 32
  25342. #elif defined(HAVE_ECC384)
  25343. #define ECC_KEYGEN_SIZE 48
  25344. #elif defined(HAVE_ECC224)
  25345. #define ECC_KEYGEN_SIZE 28
  25346. #elif defined(HAVE_ECC521)
  25347. #define ECC_KEYGEN_SIZE 66
  25348. #else
  25349. #error No ECC keygen size defined for test
  25350. #endif
  25351. #endif
  25352. #ifdef BENCH_EMBEDDED
  25353. #define ECC_SHARED_SIZE 128
  25354. #else
  25355. #define ECC_SHARED_SIZE MAX_ECC_BYTES
  25356. #endif
  25357. #if defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  25358. #define HAVE_ECC_DETERMINISTIC_K
  25359. #define ECC_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
  25360. #else
  25361. #define ECC_DIGEST_SIZE MAX_ECC_BYTES
  25362. #endif
  25363. #define ECC_SIG_SIZE ECC_MAX_SIG_SIZE
  25364. #ifdef NO_ECC_SECP
  25365. #define NO_ECC_VECTOR_TEST
  25366. #endif
  25367. #ifndef NO_ECC_VECTOR_TEST
  25368. #if (defined(HAVE_ECC192) || defined(HAVE_ECC224) ||\
  25369. !defined(NO_ECC256) || defined(HAVE_ECC384) ||\
  25370. defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  25371. #define HAVE_ECC_VECTOR_TEST
  25372. #endif
  25373. #endif
  25374. #ifdef HAVE_ECC_VECTOR_TEST
  25375. typedef struct eccVector {
  25376. const char* msg; /* SHA-1 Encoded Message */
  25377. const char* Qx;
  25378. const char* Qy;
  25379. const char* d; /* Private Key */
  25380. const char* R;
  25381. const char* S;
  25382. const char* curveName;
  25383. word32 msgLen;
  25384. word32 keySize;
  25385. #ifndef NO_ASN
  25386. const byte* r;
  25387. word32 rSz;
  25388. const byte* s;
  25389. word32 sSz;
  25390. #endif
  25391. } eccVector;
  25392. #if !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  25393. static wc_test_ret_t ecc_test_vector_item(const eccVector* vector)
  25394. {
  25395. wc_test_ret_t ret = 0;
  25396. int verify = 0;
  25397. word32 sigSz;
  25398. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25399. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25400. #else
  25401. ecc_key userA[1];
  25402. #endif
  25403. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  25404. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  25405. word32 sigRawSz, rSz = MAX_ECC_BYTES, sSz = MAX_ECC_BYTES;
  25406. WC_DECLARE_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  25407. WC_DECLARE_VAR(r, byte, MAX_ECC_BYTES, HEAP_HINT);
  25408. WC_DECLARE_VAR(s, byte, MAX_ECC_BYTES, HEAP_HINT);
  25409. #endif
  25410. WC_ALLOC_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  25411. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  25412. WC_ALLOC_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  25413. WC_ALLOC_VAR(r, byte, MAX_ECC_BYTES, HEAP_HINT);
  25414. WC_ALLOC_VAR(s, byte, MAX_ECC_BYTES, HEAP_HINT);
  25415. #endif
  25416. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  25417. if (sig == NULL)
  25418. ERROR_OUT(MEMORY_E, done);
  25419. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  25420. if (sigRaw == NULL || r == NULL || s == NULL)
  25421. ERROR_OUT(MEMORY_E, done);
  25422. #endif
  25423. #endif
  25424. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25425. if (userA == NULL)
  25426. ERROR_OUT(MEMORY_E, done);
  25427. #endif
  25428. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  25429. if (ret != 0)
  25430. goto done;
  25431. ret = wc_ecc_import_raw(userA, vector->Qx, vector->Qy,
  25432. vector->d, vector->curveName);
  25433. if (ret != 0)
  25434. goto done;
  25435. XMEMSET(sig, 0, ECC_SIG_SIZE);
  25436. sigSz = ECC_SIG_SIZE;
  25437. ret = wc_ecc_rs_to_sig(vector->R, vector->S, sig, &sigSz);
  25438. if (ret != 0)
  25439. goto done;
  25440. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  25441. XMEMSET(sigRaw, 0, ECC_SIG_SIZE);
  25442. sigRawSz = ECC_SIG_SIZE;
  25443. ret = wc_ecc_rs_raw_to_sig(vector->r, vector->rSz, vector->s, vector->sSz,
  25444. sigRaw, &sigRawSz);
  25445. if (ret != 0)
  25446. goto done;
  25447. if (sigSz != sigRawSz || XMEMCMP(sig, sigRaw, sigSz) != 0) {
  25448. ret = WC_TEST_RET_ENC_NC;
  25449. goto done;
  25450. }
  25451. ret = wc_ecc_sig_to_rs(sig, sigSz, r, &rSz, s, &sSz);
  25452. if (ret != 0)
  25453. goto done;
  25454. if (rSz != vector->rSz || XMEMCMP(r, vector->r, rSz) != 0 ||
  25455. sSz != vector->sSz || XMEMCMP(s, vector->s, sSz) != 0) {
  25456. ret = WC_TEST_RET_ENC_NC;
  25457. goto done;
  25458. }
  25459. #endif
  25460. #ifdef HAVE_ECC_VERIFY
  25461. do {
  25462. #if defined(WOLFSSL_ASYNC_CRYPT)
  25463. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  25464. #endif
  25465. if (ret == 0)
  25466. ret = wc_ecc_verify_hash(sig, sigSz, (byte*)vector->msg,
  25467. vector->msgLen, &verify, userA);
  25468. } while (ret == WC_PENDING_E);
  25469. if (ret != 0)
  25470. goto done;
  25471. TEST_SLEEP();
  25472. if (verify != 1)
  25473. ret = WC_TEST_RET_ENC_NC;
  25474. #endif
  25475. done:
  25476. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25477. if (userA != NULL) {
  25478. wc_ecc_free(userA);
  25479. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25480. }
  25481. #else
  25482. wc_ecc_free(userA);
  25483. #endif
  25484. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  25485. WC_FREE_VAR(sigRaw, HEAP_HINT);
  25486. WC_FREE_VAR(r, HEAP_HINT);
  25487. WC_FREE_VAR(s, HEAP_HINT);
  25488. #endif
  25489. WC_FREE_VAR(sig, HEAP_HINT);
  25490. return ret;
  25491. }
  25492. static wc_test_ret_t ecc_test_vector(int keySize)
  25493. {
  25494. wc_test_ret_t ret;
  25495. eccVector vec;
  25496. XMEMSET(&vec, 0, sizeof(vec));
  25497. vec.keySize = (word32)keySize;
  25498. switch(keySize) {
  25499. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  25500. case 14:
  25501. return 0;
  25502. #endif /* HAVE_ECC112 */
  25503. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  25504. case 16:
  25505. return 0;
  25506. #endif /* HAVE_ECC128 */
  25507. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  25508. case 20:
  25509. return 0;
  25510. #endif /* HAVE_ECC160 */
  25511. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  25512. case 24:
  25513. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  25514. #if 1
  25515. vec.msg = "\x60\x80\x79\x42\x3f\x12\x42\x1d\xe6\x16\xb7\x49\x3e\xbe\x55\x1c\xf4\xd6\x5b\x92";
  25516. vec.msgLen = 20;
  25517. #else
  25518. /* This is the raw message prior to SHA-1 */
  25519. vec.msg =
  25520. "\xeb\xf7\x48\xd7\x48\xeb\xbc\xa7\xd2\x9f\xb4\x73\x69\x8a\x6e\x6b"
  25521. "\x4f\xb1\x0c\x86\x5d\x4a\xf0\x24\xcc\x39\xae\x3d\xf3\x46\x4b\xa4"
  25522. "\xf1\xd6\xd4\x0f\x32\xbf\x96\x18\xa9\x1b\xb5\x98\x6f\xa1\xa2\xaf"
  25523. "\x04\x8a\x0e\x14\xdc\x51\xe5\x26\x7e\xb0\x5e\x12\x7d\x68\x9d\x0a"
  25524. "\xc6\xf1\xa7\xf1\x56\xce\x06\x63\x16\xb9\x71\xcc\x7a\x11\xd0\xfd"
  25525. "\x7a\x20\x93\xe2\x7c\xf2\xd0\x87\x27\xa4\xe6\x74\x8c\xc3\x2f\xd5"
  25526. "\x9c\x78\x10\xc5\xb9\x01\x9d\xf2\x1c\xdc\xc0\xbc\xa4\x32\xc0\xa3"
  25527. "\xee\xd0\x78\x53\x87\x50\x88\x77\x11\x43\x59\xce\xe4\xa0\x71\xcf";
  25528. vec.msgLen = 128;
  25529. #endif
  25530. vec.Qx = "07008ea40b08dbe76432096e80a2494c94982d2d5bcf98e6";
  25531. vec.Qy = "76fab681d00b414ea636ba215de26d98c41bd7f2e4d65477";
  25532. vec.d = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3";
  25533. vec.R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  25534. vec.S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  25535. vec.curveName = "SECP192R1";
  25536. #ifndef NO_ASN
  25537. vec.r = (byte*)"\x69\x94\xd9\x62\xbd\xd0\xd7\x93\xff\xdd\xf8\x55"
  25538. "\xec\x5b\xf2\xf9\x1a\x96\x98\xb4\x62\x58\xa6\x3e";
  25539. vec.rSz = 24;
  25540. vec.s = (byte*)"\x02\xba\x64\x65\xa2\x34\x90\x37\x44\xab\x02\xbc"
  25541. "\x85\x21\x40\x5b\x73\xcf\x5f\xc0\x0e\x1a\x9f\x41";
  25542. vec.sSz = 24;
  25543. #endif
  25544. break;
  25545. #endif /* HAVE_ECC192 */
  25546. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  25547. case 28:
  25548. /* first [P-224,SHA-1] vector from FIPS 186-3 NIST vectors */
  25549. #if 1
  25550. vec.msg = "\xb9\xa3\xb8\x6d\xb0\xba\x99\xfd\xc6\xd2\x94\x6b\xfe\xbe\x9c\xe8\x3f\x10\x74\xfc";
  25551. vec.msgLen = 20;
  25552. #else
  25553. /* This is the raw message prior to SHA-1 */
  25554. vec.msg =
  25555. "\x36\xc8\xb2\x29\x86\x48\x7f\x67\x7c\x18\xd0\x97\x2a\x9e\x20\x47"
  25556. "\xb3\xaf\xa5\x9e\xc1\x62\x76\x4e\xc3\x0b\x5b\x69\xe0\x63\x0f\x99"
  25557. "\x0d\x4e\x05\xc2\x73\xb0\xe5\xa9\xd4\x28\x27\xb6\x95\xfc\x2d\x64"
  25558. "\xd9\x13\x8b\x1c\xf4\xc1\x21\x55\x89\x4c\x42\x13\x21\xa7\xbb\x97"
  25559. "\x0b\xdc\xe0\xfb\xf0\xd2\xae\x85\x61\xaa\xd8\x71\x7f\x2e\x46\xdf"
  25560. "\xe3\xff\x8d\xea\xb4\xd7\x93\x23\x56\x03\x2c\x15\x13\x0d\x59\x9e"
  25561. "\x26\xc1\x0f\x2f\xec\x96\x30\x31\xac\x69\x38\xa1\x8d\x66\x45\x38"
  25562. "\xb9\x4d\xac\x55\x34\xef\x7b\x59\x94\x24\xd6\x9b\xe1\xf7\x1c\x20";
  25563. vec.msgLen = 128;
  25564. #endif
  25565. vec.Qx = "8a4dca35136c4b70e588e23554637ae251077d1365a6ba5db9585de7";
  25566. vec.Qy = "ad3dee06de0be8279d4af435d7245f14f3b4f82eb578e519ee0057b1";
  25567. vec.d = "97c4b796e1639dd1035b708fc00dc7ba1682cec44a1002a1a820619f";
  25568. vec.R = "147b33758321e722a0360a4719738af848449e2c1d08defebc1671a7";
  25569. vec.S = "24fc7ed7f1352ca3872aa0916191289e2e04d454935d50fe6af3ad5b";
  25570. vec.curveName = "SECP224R1";
  25571. #ifndef NO_ASN
  25572. vec.r = (byte*)"\x14\x7b\x33\x75\x83\x21\xe7\x22\xa0\x36\x0a\x47"
  25573. "\x19\x73\x8a\xf8\x48\x44\x9e\x2c\x1d\x08\xde\xfe"
  25574. "\xbc\x16\x71\xa7";
  25575. vec.rSz = 28;
  25576. vec.s = (byte*)"\x24\xfc\x7e\xd7\xf1\x35\x2c\xa3\x87\x2a\xa0\x91"
  25577. "\x61\x91\x28\x9e\x2e\x04\xd4\x54\x93\x5d\x50\xfe"
  25578. "\x6a\xf3\xad\x5b";
  25579. vec.sSz = 28;
  25580. #endif
  25581. break;
  25582. #endif /* HAVE_ECC224 */
  25583. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  25584. case 30:
  25585. return 0;
  25586. #endif /* HAVE_ECC239 */
  25587. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  25588. case 32:
  25589. /* first [P-256,SHA-1] vector from FIPS 186-3 NIST vectors */
  25590. #if 1
  25591. vec.msg = "\xa3\xf9\x1a\xe2\x1b\xa6\xb3\x03\x98\x64\x47\x2f\x18\x41\x44\xc6\xaf\x62\xcd\x0e";
  25592. vec.msgLen = 20;
  25593. #else
  25594. /* This is the raw message prior to SHA-1 */
  25595. vec.msg =
  25596. "\xa2\x4b\x21\x76\x2e\x6e\xdb\x15\x3c\xc1\x14\x38\xdb\x0e\x92\xcd"
  25597. "\xf5\x2b\x86\xb0\x6c\xa9\x70\x16\x06\x27\x59\xc7\x0d\x36\xd1\x56"
  25598. "\x2c\xc9\x63\x0d\x7f\xc7\xc7\x74\xb2\x8b\x54\xe3\x1e\xf5\x58\x72"
  25599. "\xb2\xa6\x5d\xf1\xd7\xec\x26\xde\xbb\x33\xe7\xd9\x27\xef\xcc\xf4"
  25600. "\x6b\x63\xde\x52\xa4\xf4\x31\xea\xca\x59\xb0\x5d\x2e\xde\xc4\x84"
  25601. "\x5f\xff\xc0\xee\x15\x03\x94\xd6\x1f\x3d\xfe\xcb\xcd\xbf\x6f\x5a"
  25602. "\x73\x38\xd0\xbe\x3f\x2a\x77\x34\x51\x98\x3e\xba\xeb\x48\xf6\x73"
  25603. "\x8f\xc8\x95\xdf\x35\x7e\x1a\x48\xa6\x53\xbb\x35\x5a\x31\xa1\xb4"
  25604. vec.msgLen = 128;
  25605. #endif
  25606. vec.Qx = "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  25607. vec.Qy = "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  25608. vec.d = "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  25609. vec.R = "2b826f5d44e2d0b6de531ad96b51e8f0c56fdfead3c236892e4d84eacfc3b75c";
  25610. vec.S = "a2248b62c03db35a7cd63e8a120a3521a89d3d2f61ff99035a2148ae32e3a248";
  25611. #ifndef NO_ASN
  25612. vec.r = (byte*)"\x2b\x82\x6f\x5d\x44\xe2\xd0\xb6\xde\x53\x1a\xd9"
  25613. "\x6b\x51\xe8\xf0\xc5\x6f\xdf\xea\xd3\xc2\x36\x89"
  25614. "\x2e\x4d\x84\xea\xcf\xc3\xb7\x5c";
  25615. vec.rSz = 32;
  25616. vec.s = (byte*)"\xa2\x24\x8b\x62\xc0\x3d\xb3\x5a\x7c\xd6\x3e\x8a"
  25617. "\x12\x0a\x35\x21\xa8\x9d\x3d\x2f\x61\xff\x99\x03"
  25618. "\x5a\x21\x48\xae\x32\xe3\xa2\x48";
  25619. vec.sSz = 32;
  25620. #endif
  25621. vec.curveName = "SECP256R1";
  25622. break;
  25623. #endif /* !NO_ECC256 */
  25624. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  25625. case 40:
  25626. return 0;
  25627. #endif /* HAVE_ECC320 */
  25628. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  25629. case 48:
  25630. /* first [P-384,SHA-1] vector from FIPS 186-3 NIST vectors */
  25631. #if 1
  25632. vec.msg = "\x9b\x9f\x8c\x95\x35\xa5\xca\x26\x60\x5d\xb7\xf2\xfa\x57\x3b\xdf\xc3\x2e\xab\x8b";
  25633. vec.msgLen = 20;
  25634. #else
  25635. /* This is the raw message prior to SHA-1 */
  25636. vec.msg =
  25637. "\xab\xe1\x0a\xce\x13\xe7\xe1\xd9\x18\x6c\x48\xf7\x88\x9d\x51\x47"
  25638. "\x3d\x3a\x09\x61\x98\x4b\xc8\x72\xdf\x70\x8e\xcc\x3e\xd3\xb8\x16"
  25639. "\x9d\x01\xe3\xd9\x6f\xc4\xf1\xd5\xea\x00\xa0\x36\x92\xbc\xc5\xcf"
  25640. "\xfd\x53\x78\x7c\x88\xb9\x34\xaf\x40\x4c\x03\x9d\x32\x89\xb5\xba"
  25641. "\xc5\xae\x7d\xb1\x49\x68\x75\xb5\xdc\x73\xc3\x09\xf9\x25\xc1\x3d"
  25642. "\x1c\x01\xab\xda\xaf\xeb\xcd\xac\x2c\xee\x43\x39\x39\xce\x8d\x4a"
  25643. "\x0a\x5d\x57\xbb\x70\x5f\x3b\xf6\xec\x08\x47\x95\x11\xd4\xb4\xa3"
  25644. "\x21\x1f\x61\x64\x9a\xd6\x27\x43\x14\xbf\x0d\x43\x8a\x81\xe0\x60"
  25645. vec.msgLen = 128;
  25646. #endif
  25647. vec.Qx = "e55fee6c49d8d523f5ce7bf9c0425ce4ff650708b7de5cfb095901523979a7f042602db30854735369813b5c3f5ef868";
  25648. vec.Qy = "28f59cc5dc509892a988d38a8e2519de3d0c4fd0fbdb0993e38f18506c17606c5e24249246f1ce94983a5361c5be983e";
  25649. vec.d = "a492ce8fa90084c227e1a32f7974d39e9ff67a7e8705ec3419b35fb607582bebd461e0b1520ac76ec2dd4e9b63ebae71";
  25650. vec.R = "6820b8585204648aed63bdff47f6d9acebdea62944774a7d14f0e14aa0b9a5b99545b2daee6b3c74ebf606667a3f39b7";
  25651. vec.S = "491af1d0cccd56ddd520b233775d0bc6b40a6255cc55207d8e9356741f23c96c14714221078dbd5c17f4fdd89b32a907";
  25652. vec.curveName = "SECP384R1";
  25653. #ifndef NO_ASN
  25654. vec.r = (byte*)"\x68\x20\xb8\x58\x52\x04\x64\x8a\xed\x63\xbd\xff"
  25655. "\x47\xf6\xd9\xac\xeb\xde\xa6\x29\x44\x77\x4a\x7d"
  25656. "\x14\xf0\xe1\x4a\xa0\xb9\xa5\xb9\x95\x45\xb2\xda"
  25657. "\xee\x6b\x3c\x74\xeb\xf6\x06\x66\x7a\x3f\x39\xb7";
  25658. vec.rSz = 48;
  25659. vec.s = (byte*)"\x49\x1a\xf1\xd0\xcc\xcd\x56\xdd\xd5\x20\xb2\x33"
  25660. "\x77\x5d\x0b\xc6\xb4\x0a\x62\x55\xcc\x55\x20\x7d"
  25661. "\x8e\x93\x56\x74\x1f\x23\xc9\x6c\x14\x71\x42\x21"
  25662. "\x07\x8d\xbd\x5c\x17\xf4\xfd\xd8\x9b\x32\xa9\x07";
  25663. vec.sSz = 48;
  25664. #endif
  25665. break;
  25666. #endif /* HAVE_ECC384 */
  25667. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  25668. case 64:
  25669. return 0;
  25670. #endif /* HAVE_ECC512 */
  25671. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  25672. case 66:
  25673. /* first [P-521,SHA-1] vector from FIPS 186-3 NIST vectors */
  25674. #if 1
  25675. vec.msg = "\x1b\xf7\x03\x9c\xca\x23\x94\x27\x3f\x11\xa1\xd4\x8d\xcc\xb4\x46\x6f\x31\x61\xdf";
  25676. vec.msgLen = 20;
  25677. #else
  25678. /* This is the raw message prior to SHA-1 */
  25679. vec.msg =
  25680. "\x50\x3f\x79\x39\x34\x0a\xc7\x23\xcd\x4a\x2f\x4e\x6c\xcc\x27\x33"
  25681. "\x38\x3a\xca\x2f\xba\x90\x02\x19\x9d\x9e\x1f\x94\x8b\xe0\x41\x21"
  25682. "\x07\xa3\xfd\xd5\x14\xd9\x0c\xd4\xf3\x7c\xc3\xac\x62\xef\x00\x3a"
  25683. "\x2d\xb1\xd9\x65\x7a\xb7\x7f\xe7\x55\xbf\x71\xfa\x59\xe4\xd9\x6e"
  25684. "\xa7\x2a\xe7\xbf\x9d\xe8\x7d\x79\x34\x3b\xc1\xa4\xbb\x14\x4d\x16"
  25685. "\x28\xd1\xe9\xe9\xc8\xed\x80\x8b\x96\x2c\x54\xe5\xf9\x6d\x53\xda"
  25686. "\x14\x7a\x96\x38\xf9\x4a\x91\x75\xd8\xed\x61\x05\x5f\x0b\xa5\x73"
  25687. "\xa8\x2b\xb7\xe0\x18\xee\xda\xc4\xea\x7b\x36\x2e\xc8\x9c\x38\x2b"
  25688. vec.msgLen = 128;
  25689. #endif
  25690. vec.Qx = "12fbcaeffa6a51f3ee4d3d2b51c5dec6d7c726ca353fc014ea2bf7cfbb9b910d32cbfa6a00fe39b6cdb8946f22775398b2e233c0cf144d78c8a7742b5c7a3bb5d23";
  25691. vec.Qy = "09cdef823dd7bf9a79e8cceacd2e4527c231d0ae5967af0958e931d7ddccf2805a3e618dc3039fec9febbd33052fe4c0fee98f033106064982d88f4e03549d4a64d";
  25692. vec.d = "1bd56bd106118eda246155bd43b42b8e13f0a6e25dd3bb376026fab4dc92b6157bc6dfec2d15dd3d0cf2a39aa68494042af48ba9601118da82c6f2108a3a203ad74";
  25693. vec.R = "0bd117b4807710898f9dd7778056485777668f0e78e6ddf5b000356121eb7a220e9493c7f9a57c077947f89ac45d5acb6661bbcd17abb3faea149ba0aa3bb1521be";
  25694. vec.S = "019cd2c5c3f9870ecdeb9b323abdf3a98cd5e231d85c6ddc5b71ab190739f7f226e6b134ba1d5889ddeb2751dabd97911dff90c34684cdbe7bb669b6c3d22f2480c";
  25695. vec.curveName = "SECP521R1";
  25696. #ifndef NO_ASN
  25697. vec.r = (byte*)"\xbd\x11\x7b\x48\x07\x71\x08\x98\xf9\xdd\x77\x78"
  25698. "\x05\x64\x85\x77\x76\x68\xf0\xe7\x8e\x6d\xdf\x5b"
  25699. "\x00\x03\x56\x12\x1e\xb7\xa2\x20\xe9\x49\x3c\x7f"
  25700. "\x9a\x57\xc0\x77\x94\x7f\x89\xac\x45\xd5\xac\xb6"
  25701. "\x66\x1b\xbc\xd1\x7a\xbb\x3f\xae\xa1\x49\xba\x0a"
  25702. "\xa3\xbb\x15\x21\xbe";
  25703. vec.rSz = 65;
  25704. vec.s = (byte*)"\x19\xcd\x2c\x5c\x3f\x98\x70\xec\xde\xb9\xb3\x23"
  25705. "\xab\xdf\x3a\x98\xcd\x5e\x23\x1d\x85\xc6\xdd\xc5"
  25706. "\xb7\x1a\xb1\x90\x73\x9f\x7f\x22\x6e\x6b\x13\x4b"
  25707. "\xa1\xd5\x88\x9d\xde\xb2\x75\x1d\xab\xd9\x79\x11"
  25708. "\xdf\xf9\x0c\x34\x68\x4c\xdb\xe7\xbb\x66\x9b\x6c"
  25709. "\x3d\x22\xf2\x48\x0c";
  25710. vec.sSz = 65;
  25711. #endif
  25712. break;
  25713. #endif /* HAVE_ECC521 */
  25714. default:
  25715. return NOT_COMPILED_IN; /* Invalid key size / Not supported */
  25716. }; /* Switch */
  25717. ret = ecc_test_vector_item(&vec);
  25718. if (ret < 0) {
  25719. return ret;
  25720. }
  25721. return 0;
  25722. }
  25723. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  25724. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  25725. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  25726. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  25727. #if defined(HAVE_ECC256)
  25728. static wc_test_ret_t ecc_test_deterministic_k(WC_RNG* rng)
  25729. {
  25730. wc_test_ret_t ret;
  25731. #ifdef WOLFSSL_SMALL_STACK
  25732. ecc_key *key = NULL;
  25733. #else
  25734. ecc_key key[1];
  25735. #endif
  25736. int key_inited = 0;
  25737. byte sig[72];
  25738. word32 sigSz;
  25739. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  25740. unsigned char hash[32];
  25741. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  25742. "C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721";
  25743. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  25744. "60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6";
  25745. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  25746. "7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299";
  25747. WOLFSSL_SMALL_STACK_STATIC const byte expSig[] = {
  25748. 0x30, 0x46, 0x02, 0x21, 0x00, 0xEF, 0xD4, 0x8B,
  25749. 0x2A, 0xAC, 0xB6, 0xA8, 0xFD, 0x11, 0x40, 0xDD,
  25750. 0x9C, 0xD4, 0x5E, 0x81, 0xD6, 0x9D, 0x2C, 0x87,
  25751. 0x7B, 0x56, 0xAA, 0xF9, 0x91, 0xC3, 0x4D, 0x0E,
  25752. 0xA8, 0x4E, 0xAF, 0x37, 0x16, 0x02, 0x21, 0x00,
  25753. 0xF7, 0xCB, 0x1C, 0x94, 0x2D, 0x65, 0x7C, 0x41,
  25754. 0xD4, 0x36, 0xC7, 0xA1, 0xB6, 0xE2, 0x9F, 0x65,
  25755. 0xF3, 0xE9, 0x00, 0xDB, 0xB9, 0xAF, 0xF4, 0x06,
  25756. 0x4D, 0xC4, 0xAB, 0x2F, 0x84, 0x3A, 0xCD, 0xA8
  25757. };
  25758. #ifdef WOLFSSL_SMALL_STACK
  25759. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25760. if (key == NULL)
  25761. return MEMORY_E;
  25762. #endif
  25763. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  25764. if (ret != 0) {
  25765. goto done;
  25766. }
  25767. key_inited = 1;
  25768. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  25769. if (ret != 0) {
  25770. goto done;
  25771. }
  25772. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  25773. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  25774. if (ret != 0) {
  25775. goto done;
  25776. }
  25777. ret = wc_ecc_set_deterministic(key, 1);
  25778. if (ret != 0) {
  25779. goto done;
  25780. }
  25781. sigSz = sizeof(sig);
  25782. do {
  25783. #if defined(WOLFSSL_ASYNC_CRYPT)
  25784. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  25785. #endif
  25786. if (ret == 0)
  25787. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  25788. } while (ret == WC_PENDING_E);
  25789. if (ret != 0) {
  25790. goto done;
  25791. }
  25792. TEST_SLEEP();
  25793. if (sigSz != sizeof(expSig)) {
  25794. ret = WC_TEST_RET_ENC_NC;
  25795. goto done;
  25796. }
  25797. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  25798. ret = WC_TEST_RET_ENC_NC;
  25799. goto done;
  25800. }
  25801. sigSz = sizeof(sig);
  25802. do {
  25803. #if defined(WOLFSSL_ASYNC_CRYPT)
  25804. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  25805. #endif
  25806. if (ret == 0)
  25807. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  25808. } while (ret == WC_PENDING_E);
  25809. if (ret != 0) {
  25810. goto done;
  25811. }
  25812. TEST_SLEEP();
  25813. done:
  25814. if (key_inited)
  25815. wc_ecc_free(key);
  25816. #ifdef WOLFSSL_SMALL_STACK
  25817. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25818. #endif
  25819. return ret;
  25820. }
  25821. #endif
  25822. #ifdef WOLFSSL_PUBLIC_MP
  25823. #if defined(HAVE_ECC384)
  25824. /* KAT from RFC6979 */
  25825. static wc_test_ret_t ecc384_test_deterministic_k(WC_RNG* rng)
  25826. {
  25827. wc_test_ret_t ret;
  25828. #ifdef WOLFSSL_SMALL_STACK
  25829. ecc_key *key;
  25830. mp_int *r, *s, *expR, *expS;
  25831. #else
  25832. ecc_key key[1];
  25833. mp_int r[1], s[1], expR[1], expS[1];
  25834. #endif
  25835. int key_inited = 0;
  25836. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  25837. unsigned char hash[32];
  25838. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  25839. "6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D8"
  25840. "96D5724E4C70A825F872C9EA60D2EDF5";
  25841. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  25842. "EC3A4E415B4E19A4568618029F427FA5DA9A8BC4AE92E02E06AAE5286B300C64"
  25843. "DEF8F0EA9055866064A254515480BC13";
  25844. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  25845. "8015D9B72D7D57244EA8EF9AC0C621896708A59367F9DFB9F54CA84B3F1C9DB1"
  25846. "288B231C3AE0D4FE7344FD2533264720";
  25847. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  25848. "21B13D1E013C7FA1392D03C5F99AF8B30C570C6F98D4EA8E354B63A21D3DAA33"
  25849. "BDE1E888E63355D92FA2B3C36D8FB2CD";
  25850. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  25851. "F3AA443FB107745BF4BD77CB3891674632068A10CA67E3D45DB2266FA7D1FEEB"
  25852. "EFDC63ECCD1AC42EC0CB8668A4FA0AB0";
  25853. #ifdef WOLFSSL_SMALL_STACK
  25854. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25855. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25856. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25857. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25858. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25859. if ((key == NULL) ||
  25860. (r == NULL) ||
  25861. (s == NULL) ||
  25862. (expR == NULL) ||
  25863. (expS == NULL))
  25864. {
  25865. ret = MEMORY_E;
  25866. goto done;
  25867. }
  25868. #endif
  25869. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  25870. if (ret != MP_OKAY) {
  25871. goto done;
  25872. }
  25873. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  25874. if (ret != 0) {
  25875. goto done;
  25876. }
  25877. key_inited = 1;
  25878. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP384R1");
  25879. if (ret != 0) {
  25880. goto done;
  25881. }
  25882. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  25883. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  25884. if (ret != 0) {
  25885. goto done;
  25886. }
  25887. ret = wc_ecc_set_deterministic(key, 1);
  25888. if (ret != 0) {
  25889. goto done;
  25890. }
  25891. do {
  25892. #if defined(WOLFSSL_ASYNC_CRYPT)
  25893. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  25894. #endif
  25895. if (ret == 0)
  25896. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  25897. } while (ret == WC_PENDING_E);
  25898. if (ret != 0) {
  25899. goto done;
  25900. }
  25901. TEST_SLEEP();
  25902. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  25903. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  25904. if (mp_cmp(r, expR) != MP_EQ) {
  25905. ret = WC_TEST_RET_ENC_NC;
  25906. }
  25907. done:
  25908. if (key_inited)
  25909. wc_ecc_free(key);
  25910. #ifdef WOLFSSL_SMALL_STACK
  25911. if (key != NULL)
  25912. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25913. if (r != NULL)
  25914. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25915. if (s != NULL)
  25916. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25917. if (expR != NULL)
  25918. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25919. if (expS != NULL)
  25920. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25921. #endif
  25922. return ret;
  25923. }
  25924. #endif /* HAVE_ECC384 */
  25925. #if defined(HAVE_ECC521)
  25926. /* KAT from RFC6979 */
  25927. static wc_test_ret_t ecc521_test_deterministic_k(WC_RNG* rng)
  25928. {
  25929. wc_test_ret_t ret;
  25930. #ifdef WOLFSSL_SMALL_STACK
  25931. ecc_key *key;
  25932. mp_int *r, *s, *expR, *expS;
  25933. #else
  25934. ecc_key key[1];
  25935. mp_int r[1], s[1], expR[1], expS[1];
  25936. #endif
  25937. int key_inited = 0;
  25938. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  25939. unsigned char hash[32];
  25940. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  25941. "0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75C"
  25942. "AA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83"
  25943. "538";
  25944. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  25945. "1894550D0785932E00EAA23B694F213F8C3121F86DC97A04E5A7167DB4E5BCD3"
  25946. "71123D46E45DB6B5D5370A7F20FB633155D38FFA16D2BD761DCAC474B9A2F502"
  25947. "3A4";
  25948. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  25949. "0493101C962CD4D2FDDF782285E64584139C2F91B47F87FF82354D6630F746A2"
  25950. "8A0DB25741B5B34A828008B22ACC23F924FAAFBD4D33F81EA66956DFEAA2BFDF"
  25951. "CF5";
  25952. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  25953. "1511BB4D675114FE266FC4372B87682BAECC01D3CC62CF2303C92B3526012659"
  25954. "D16876E25C7C1E57648F23B73564D67F61C6F14D527D54972810421E7D87589E"
  25955. "1A7";
  25956. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  25957. "04A171143A83163D6DF460AAF61522695F207A58B95C0644D87E52AA1A347916"
  25958. "E4F7A72930B1BC06DBE22CE3F58264AFD23704CBB63B29B931F7DE6C9D949A7E"
  25959. "CFC";
  25960. #ifdef WOLFSSL_SMALL_STACK
  25961. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25962. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25963. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25964. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25965. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25966. if ((key == NULL) ||
  25967. (r == NULL) ||
  25968. (s == NULL) ||
  25969. (expR == NULL) ||
  25970. (expS == NULL))
  25971. {
  25972. ret = MEMORY_E;
  25973. goto done;
  25974. }
  25975. #endif
  25976. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  25977. if (ret != MP_OKAY) {
  25978. goto done;
  25979. }
  25980. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  25981. if (ret != 0) {
  25982. return WC_TEST_RET_ENC_EC(ret);
  25983. }
  25984. key_inited = 1;
  25985. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP521R1");
  25986. if (ret != 0) {
  25987. goto done;
  25988. }
  25989. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  25990. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  25991. if (ret != 0) {
  25992. goto done;
  25993. }
  25994. ret = wc_ecc_set_deterministic(key, 1);
  25995. if (ret != 0) {
  25996. goto done;
  25997. }
  25998. do {
  25999. #if defined(WOLFSSL_ASYNC_CRYPT)
  26000. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26001. #endif
  26002. if (ret == 0)
  26003. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  26004. } while (ret == WC_PENDING_E);
  26005. if (ret != 0) {
  26006. goto done;
  26007. }
  26008. TEST_SLEEP();
  26009. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  26010. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  26011. if (mp_cmp(r, expR) != MP_EQ) {
  26012. ret = WC_TEST_RET_ENC_NC;
  26013. }
  26014. done:
  26015. if (key_inited)
  26016. wc_ecc_free(key);
  26017. #ifdef WOLFSSL_SMALL_STACK
  26018. if (key != NULL)
  26019. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26020. if (r != NULL)
  26021. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26022. if (s != NULL)
  26023. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26024. if (expR != NULL)
  26025. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26026. if (expS != NULL)
  26027. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26028. #endif
  26029. return ret;
  26030. }
  26031. #endif /* HAVE_ECC521 */
  26032. #endif /* WOLFSSL_PUBLIC_MP */
  26033. #endif /* HAVE_ECC_SIGN && (WOLFSSL_ECDSA_DETERMINISTIC_K ||
  26034. WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  26035. && (!FIPS_VERSION_GE || FIPS_VERSION_GE(5,3)) */
  26036. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  26037. !defined(WOLFSSL_KCAPI_ECC)
  26038. static wc_test_ret_t ecc_test_sign_vectors(WC_RNG* rng)
  26039. {
  26040. wc_test_ret_t ret;
  26041. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26042. ecc_key *key = NULL;
  26043. #else
  26044. ecc_key key[1];
  26045. #endif
  26046. int key_inited = 0;
  26047. byte sig[72];
  26048. word32 sigSz;
  26049. WOLFSSL_SMALL_STACK_STATIC const unsigned char hash[32] = "test wolfSSL deterministic sign";
  26050. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  26051. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  26052. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  26053. WOLFSSL_SMALL_STACK_STATIC const byte k[1] = { 0x02 };
  26054. WOLFSSL_SMALL_STACK_STATIC const byte expSig[71] = {
  26055. 0x30, 0x45, 0x02, 0x20, 0x7c, 0xf2, 0x7b, 0x18,
  26056. 0x8d, 0x03, 0x4f, 0x7e, 0x8a, 0x52, 0x38, 0x03,
  26057. 0x04, 0xb5, 0x1a, 0xc3, 0xc0, 0x89, 0x69, 0xe2,
  26058. 0x77, 0xf2, 0x1b, 0x35, 0xa6, 0x0b, 0x48, 0xfc,
  26059. 0x47, 0x66, 0x99, 0x78, 0x02, 0x21, 0x00, 0xa8,
  26060. 0x43, 0xa0, 0xce, 0x6c, 0x5e, 0x17, 0x8a, 0x53,
  26061. 0x4d, 0xaf, 0xd2, 0x95, 0x78, 0x9f, 0x84, 0x4f,
  26062. 0x94, 0xb8, 0x75, 0xa3, 0x19, 0xa5, 0xd4, 0xdf,
  26063. 0xe1, 0xd4, 0x5e, 0x9d, 0x97, 0xfe, 0x81
  26064. };
  26065. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26066. if ((key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  26067. return MEMORY_E;
  26068. #endif
  26069. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  26070. if (ret != 0) {
  26071. goto done;
  26072. }
  26073. key_inited = 1;
  26074. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  26075. if (ret != 0) {
  26076. goto done;
  26077. }
  26078. #if (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) \
  26079. && (HAVE_FIPS_VERSION > 2)))
  26080. wc_ecc_set_flags(key, WC_ECC_FLAG_DEC_SIGN);
  26081. #endif
  26082. ret = wc_ecc_sign_set_k(k, sizeof(k), key);
  26083. if (ret != 0) {
  26084. goto done;
  26085. }
  26086. sigSz = sizeof(sig);
  26087. do {
  26088. #if defined(WOLFSSL_ASYNC_CRYPT)
  26089. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26090. #endif
  26091. if (ret == 0)
  26092. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  26093. } while (ret == WC_PENDING_E);
  26094. if (ret != 0) {
  26095. goto done;
  26096. }
  26097. TEST_SLEEP();
  26098. if (sigSz != sizeof(expSig)) {
  26099. ret = WC_TEST_RET_ENC_NC;
  26100. goto done;
  26101. }
  26102. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  26103. ret = WC_TEST_RET_ENC_NC;
  26104. goto done;
  26105. }
  26106. sigSz = sizeof(sig);
  26107. do {
  26108. #if defined(WOLFSSL_ASYNC_CRYPT)
  26109. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26110. #endif
  26111. if (ret == 0)
  26112. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  26113. } while (ret == WC_PENDING_E);
  26114. if (ret != 0) {
  26115. goto done;
  26116. }
  26117. TEST_SLEEP();
  26118. done:
  26119. if (key_inited)
  26120. wc_ecc_free(key);
  26121. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26122. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26123. #endif
  26124. return ret;
  26125. }
  26126. #endif
  26127. #if defined(HAVE_ECC_CDH) && defined(HAVE_ECC_DHE)
  26128. static wc_test_ret_t ecc_test_cdh_vectors(WC_RNG* rng)
  26129. {
  26130. wc_test_ret_t ret;
  26131. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26132. ecc_key *pub_key = (ecc_key *)XMALLOC(sizeof *pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26133. ecc_key *priv_key = (ecc_key *)XMALLOC(sizeof *priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26134. #else
  26135. ecc_key pub_key[1], priv_key[1];
  26136. #endif
  26137. byte sharedA[32] = {0}, sharedB[32] = {0};
  26138. word32 x, z;
  26139. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSx = "700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287";
  26140. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSy = "db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac";
  26141. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  26142. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  26143. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  26144. WOLFSSL_SMALL_STACK_STATIC const char* ZIUT = "46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b";
  26145. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26146. if ((pub_key == NULL) ||
  26147. (priv_key == NULL)) {
  26148. ret = MEMORY_E;
  26149. goto done;
  26150. }
  26151. #endif
  26152. XMEMSET(pub_key, 0, sizeof *pub_key);
  26153. XMEMSET(priv_key, 0, sizeof *priv_key);
  26154. /* setup private and public keys */
  26155. ret = wc_ecc_init_ex(pub_key, HEAP_HINT, devId);
  26156. if (ret != 0)
  26157. goto done;
  26158. ret = wc_ecc_init_ex(priv_key, HEAP_HINT, devId);
  26159. if (ret != 0)
  26160. goto done;
  26161. wc_ecc_set_flags(pub_key, WC_ECC_FLAG_COFACTOR);
  26162. wc_ecc_set_flags(priv_key, WC_ECC_FLAG_COFACTOR);
  26163. ret = wc_ecc_import_raw(pub_key, QCAVSx, QCAVSy, NULL, "SECP256R1");
  26164. if (ret != 0)
  26165. goto done;
  26166. ret = wc_ecc_import_raw(priv_key, QIUTx, QIUTy, dIUT, "SECP256R1");
  26167. if (ret != 0)
  26168. goto done;
  26169. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  26170. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  26171. !defined(HAVE_SELFTEST)
  26172. ret = wc_ecc_set_rng(priv_key, rng);
  26173. if (ret != 0)
  26174. goto done;
  26175. #else
  26176. (void)rng;
  26177. #endif
  26178. /* compute ECC Cofactor shared secret */
  26179. x = sizeof(sharedA);
  26180. do {
  26181. #if defined(WOLFSSL_ASYNC_CRYPT)
  26182. ret = wc_AsyncWait(ret, &priv_key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26183. #endif
  26184. if (ret == 0)
  26185. ret = wc_ecc_shared_secret(priv_key, pub_key, sharedA, &x);
  26186. } while (ret == WC_PENDING_E);
  26187. if (ret != 0) {
  26188. goto done;
  26189. }
  26190. TEST_SLEEP();
  26191. /* read in expected Z */
  26192. z = sizeof(sharedB);
  26193. ret = Base16_Decode((const byte*)ZIUT, (word32)XSTRLEN(ZIUT), sharedB, &z);
  26194. if (ret != 0)
  26195. goto done;
  26196. /* compare results */
  26197. if (x != z || XMEMCMP(sharedA, sharedB, x)) {
  26198. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26199. }
  26200. done:
  26201. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26202. if (priv_key) {
  26203. wc_ecc_free(priv_key);
  26204. XFREE(priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26205. }
  26206. if (pub_key) {
  26207. wc_ecc_free(pub_key);
  26208. XFREE(pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26209. }
  26210. #else
  26211. wc_ecc_free(priv_key);
  26212. wc_ecc_free(pub_key);
  26213. #endif
  26214. return ret;
  26215. }
  26216. #endif /* HAVE_ECC_CDH && HAVE_ECC_DHE */
  26217. #endif /* HAVE_ECC_VECTOR_TEST */
  26218. #ifdef HAVE_ECC_KEY_IMPORT
  26219. /* returns 0 on success */
  26220. static wc_test_ret_t ecc_test_make_pub(WC_RNG* rng)
  26221. {
  26222. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26223. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26224. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  26225. ecc_key *pub = (ecc_key *)XMALLOC(sizeof *pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26226. #endif
  26227. byte *exportBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26228. byte *tmp = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26229. #else
  26230. ecc_key key[1];
  26231. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  26232. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  26233. ecc_key pub[1];
  26234. #endif
  26235. byte exportBuf[ECC_BUFSIZE];
  26236. byte tmp[ECC_BUFSIZE];
  26237. #endif
  26238. const byte* msg = (const byte*)"test wolfSSL ECC public gen";
  26239. word32 x;
  26240. word32 tmpSz;
  26241. wc_test_ret_t ret = 0;
  26242. ecc_point* pubPoint = NULL;
  26243. #ifdef HAVE_ECC_VERIFY
  26244. int verify = 0;
  26245. #endif
  26246. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26247. if ((key == NULL) ||
  26248. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  26249. (pub == NULL) ||
  26250. #endif
  26251. (exportBuf == NULL) ||
  26252. (tmp == NULL))
  26253. ERROR_OUT(MEMORY_E, done);
  26254. #endif
  26255. (void)msg;
  26256. (void)verify;
  26257. (void)exportBuf;
  26258. (void)rng;
  26259. wc_ecc_init_ex(key, HEAP_HINT, devId);
  26260. #ifndef NO_ECC256
  26261. #if defined(USE_CERT_BUFFERS_256)
  26262. {
  26263. XMEMCPY(tmp, ecc_key_der_256, (size_t)sizeof_ecc_key_der_256);
  26264. tmpSz = (size_t)sizeof_ecc_key_der_256;
  26265. }
  26266. #elif !defined(NO_FILESYSTEM)
  26267. {
  26268. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  26269. if (!file) {
  26270. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26271. }
  26272. tmpSz = (word32)XFREAD(tmp, 1, ECC_BUFSIZE, file);
  26273. XFCLOSE(file);
  26274. if (tmpSz == 0)
  26275. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26276. }
  26277. #else
  26278. {
  26279. WOLFSSL_MSG("No file system and USE_CERT_BUFFERS_256 not defined.(2)");
  26280. ERROR_OUT(ASN_PARSE_E, done);
  26281. }
  26282. #endif /* USE_CERT_BUFFERS_256 */
  26283. /* import private only then test with */
  26284. ret = wc_ecc_import_private_key(tmp, tmpSz, NULL, 0, NULL);
  26285. if (ret == 0) {
  26286. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26287. }
  26288. ret = wc_ecc_import_private_key(NULL, tmpSz, NULL, 0, key);
  26289. if (ret == 0) {
  26290. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26291. }
  26292. x = 0;
  26293. ret = wc_EccPrivateKeyDecode(tmp, &x, key, tmpSz);
  26294. if (ret != 0)
  26295. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26296. #ifdef HAVE_ECC_KEY_EXPORT
  26297. x = ECC_BUFSIZE;
  26298. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  26299. if (ret != 0)
  26300. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26301. /* make private only key */
  26302. wc_ecc_free(key);
  26303. wc_ecc_init_ex(key, HEAP_HINT, devId);
  26304. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  26305. if (ret != 0)
  26306. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26307. x = ECC_BUFSIZE;
  26308. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  26309. if (ret == 0) {
  26310. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26311. }
  26312. #endif /* HAVE_ECC_KEY_EXPORT */
  26313. ret = wc_ecc_make_pub(NULL, NULL);
  26314. if (ret == 0) {
  26315. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26316. }
  26317. TEST_SLEEP();
  26318. #ifndef WOLFSSL_NO_MALLOC
  26319. pubPoint = wc_ecc_new_point_h(HEAP_HINT);
  26320. if (pubPoint == NULL) {
  26321. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26322. }
  26323. #if !defined(WOLFSSL_CRYPTOCELL)
  26324. ret = wc_ecc_make_pub(key, pubPoint);
  26325. #if defined(WOLFSSL_ASYNC_CRYPT)
  26326. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  26327. #endif
  26328. if (ret != 0)
  26329. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26330. #endif
  26331. TEST_SLEEP();
  26332. #ifdef HAVE_ECC_KEY_EXPORT
  26333. /* export should still fail, is private only key */
  26334. x = ECC_BUFSIZE;
  26335. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  26336. if (ret == 0) {
  26337. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26338. }
  26339. #endif /* HAVE_ECC_KEY_EXPORT */
  26340. #endif /* !WOLFSSL_NO_MALLOC */
  26341. #endif /* !NO_ECC256 */
  26342. /* create a new key since above test for loading key is not supported */
  26343. #if defined(WOLFSSL_CRYPTOCELL) || defined(NO_ECC256) || \
  26344. defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_SE050) || \
  26345. defined(WOLFSSL_SECO_CAAM) || defined(WOLFSSL_IMXRT1170_CAAM)
  26346. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  26347. if (ret != 0)
  26348. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26349. #endif
  26350. #if defined(HAVE_ECC_SIGN) && (!defined(ECC_TIMING_RESISTANT) || \
  26351. (defined(ECC_TIMING_RESISTANT) && !defined(WC_NO_RNG))) && \
  26352. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(HAVE_ECC_DETERMINISTIC_K)
  26353. tmpSz = ECC_BUFSIZE;
  26354. ret = 0;
  26355. do {
  26356. #if defined(WOLFSSL_ASYNC_CRYPT)
  26357. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26358. #endif
  26359. if (ret == 0) {
  26360. ret = wc_ecc_sign_hash(msg, (word32)XSTRLEN((const char* )msg), tmp,
  26361. &tmpSz, rng, key);
  26362. }
  26363. } while (ret == WC_PENDING_E);
  26364. if (ret != 0)
  26365. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26366. TEST_SLEEP();
  26367. #ifdef HAVE_ECC_VERIFY
  26368. /* try verify with private only key */
  26369. ret = 0;
  26370. do {
  26371. #if defined(WOLFSSL_ASYNC_CRYPT)
  26372. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26373. #endif
  26374. if (ret == 0) {
  26375. ret = wc_ecc_verify_hash(tmp, tmpSz, msg,
  26376. (word32)XSTRLEN((const char*)msg), &verify, key);
  26377. }
  26378. } while (ret == WC_PENDING_E);
  26379. if (ret != 0)
  26380. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26381. if (verify != 1) {
  26382. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26383. }
  26384. TEST_SLEEP();
  26385. #ifdef HAVE_ECC_KEY_EXPORT
  26386. /* exporting the public part should now work */
  26387. x = ECC_BUFSIZE;
  26388. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  26389. if (ret != 0)
  26390. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26391. #endif /* HAVE_ECC_KEY_EXPORT */
  26392. #endif /* HAVE_ECC_VERIFY */
  26393. #endif /* HAVE_ECC_SIGN */
  26394. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  26395. /* now test private only key with creating a shared secret */
  26396. x = ECC_BUFSIZE;
  26397. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  26398. if (ret != 0)
  26399. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26400. #if !defined(WOLFSSL_QNX_CAAM) && !defined(WOLFSSL_SE050)
  26401. /* make private only key */
  26402. wc_ecc_free(key);
  26403. wc_ecc_init_ex(key, HEAP_HINT, devId);
  26404. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  26405. if (ret != 0)
  26406. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26407. /* check that public export fails with private only key */
  26408. x = ECC_BUFSIZE;
  26409. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  26410. if (ret == 0) {
  26411. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26412. }
  26413. #endif /* WOLFSSL_QNX_CAAM */
  26414. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  26415. /* make public key for shared secret */
  26416. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  26417. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, pub);
  26418. #ifdef HAVE_ECC_CDH
  26419. wc_ecc_set_flags(key, WC_ECC_FLAG_COFACTOR);
  26420. #endif
  26421. #if defined(WOLFSSL_ASYNC_CRYPT)
  26422. ret = wc_AsyncWait(ret, &pub->asyncDev, WC_ASYNC_FLAG_NONE);
  26423. #endif
  26424. if (ret != 0)
  26425. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26426. TEST_SLEEP();
  26427. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  26428. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  26429. !defined(HAVE_SELFTEST)
  26430. ret = wc_ecc_set_rng(key, rng);
  26431. if (ret != 0)
  26432. goto done;
  26433. #endif
  26434. x = ECC_BUFSIZE;
  26435. do {
  26436. #if defined(WOLFSSL_ASYNC_CRYPT)
  26437. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26438. #endif
  26439. if (ret == 0) {
  26440. ret = wc_ecc_shared_secret(key, pub, exportBuf, &x);
  26441. }
  26442. } while (ret == WC_PENDING_E);
  26443. wc_ecc_free(pub);
  26444. if (ret != 0)
  26445. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26446. TEST_SLEEP();
  26447. #endif /* HAVE_ECC_DHE && HAVE_ECC_KEY_EXPORT && !WC_NO_RNG */
  26448. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  26449. ret = 0;
  26450. done:
  26451. wc_ecc_del_point_h(pubPoint, HEAP_HINT);
  26452. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26453. if (key != NULL) {
  26454. wc_ecc_free(key);
  26455. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26456. }
  26457. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  26458. if (pub != NULL)
  26459. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26460. #endif
  26461. if (exportBuf != NULL)
  26462. XFREE(exportBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26463. if (tmp != NULL)
  26464. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26465. #else
  26466. wc_ecc_free(key);
  26467. #endif
  26468. return ret;
  26469. }
  26470. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  26471. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  26472. static wc_test_ret_t ecc_test_key_decode(WC_RNG* rng, int keySize)
  26473. {
  26474. wc_test_ret_t ret;
  26475. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26476. ecc_key *eccKey = (ecc_key *)XMALLOC(sizeof *eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26477. byte *tmpBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26478. #else
  26479. ecc_key eccKey[1];
  26480. byte tmpBuf[ECC_BUFSIZE];
  26481. #endif
  26482. word32 tmpSz;
  26483. word32 idx;
  26484. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26485. if ((eccKey == NULL) || (tmpBuf == NULL))
  26486. ERROR_OUT(MEMORY_E, done);
  26487. #endif
  26488. ret = wc_ecc_init(eccKey);
  26489. if (ret != 0) {
  26490. goto done;
  26491. }
  26492. ret = wc_ecc_make_key(rng, keySize, eccKey);
  26493. #if defined(WOLFSSL_ASYNC_CRYPT)
  26494. ret = wc_AsyncWait(ret, &eccKey->asyncDev, WC_ASYNC_FLAG_NONE);
  26495. #endif
  26496. if (ret != 0) {
  26497. goto done;
  26498. }
  26499. tmpSz = ECC_BUFSIZE;
  26500. ret = wc_EccKeyToDer(eccKey, tmpBuf, tmpSz);
  26501. wc_ecc_free(eccKey);
  26502. if (ret < 0) {
  26503. goto done;
  26504. }
  26505. tmpSz = (word32)ret;
  26506. ret = wc_ecc_init(eccKey);
  26507. if (ret != 0) {
  26508. goto done;
  26509. }
  26510. idx = 0;
  26511. ret = wc_EccPrivateKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  26512. if (ret != 0) {
  26513. goto done;
  26514. }
  26515. wc_ecc_free(eccKey);
  26516. ret = wc_ecc_init(eccKey);
  26517. if (ret != 0) {
  26518. goto done;
  26519. }
  26520. idx = 0;
  26521. ret = wc_EccPublicKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  26522. if (ret != 0) {
  26523. goto done;
  26524. }
  26525. ret = 0;
  26526. done:
  26527. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26528. if (eccKey != NULL) {
  26529. wc_ecc_free(eccKey);
  26530. XFREE(eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26531. }
  26532. if (tmpBuf != NULL)
  26533. XFREE(tmpBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26534. #else
  26535. wc_ecc_free(eccKey);
  26536. #endif
  26537. return ret;
  26538. }
  26539. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  26540. #endif /* HAVE_ECC_KEY_IMPORT */
  26541. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  26542. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  26543. static wc_test_ret_t ecc_test_key_gen(WC_RNG* rng, int keySize)
  26544. {
  26545. wc_test_ret_t ret = 0;
  26546. int derSz;
  26547. #ifdef HAVE_PKCS8
  26548. word32 pkcs8Sz;
  26549. #endif
  26550. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26551. byte *der = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26552. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26553. #else
  26554. byte der[ECC_BUFSIZE];
  26555. ecc_key userA[1];
  26556. #endif
  26557. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26558. if ((der == NULL) || (userA == NULL))
  26559. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), done);
  26560. #endif
  26561. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  26562. if (ret != 0)
  26563. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26564. ret = wc_ecc_make_key(rng, keySize, userA);
  26565. #if defined(WOLFSSL_ASYNC_CRYPT)
  26566. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  26567. #endif
  26568. if (ret != 0)
  26569. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26570. TEST_SLEEP();
  26571. ret = wc_ecc_check_key(userA);
  26572. if (ret != 0)
  26573. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26574. TEST_SLEEP();
  26575. derSz = wc_EccKeyToDer(userA, der, ECC_BUFSIZE);
  26576. if (derSz < 0) {
  26577. ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
  26578. }
  26579. ret = SaveDerAndPem(der, derSz, eccCaKeyTempFile, eccCaKeyPemFile,
  26580. ECC_PRIVATEKEY_TYPE);
  26581. if (ret != 0)
  26582. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26583. /* test export of public key */
  26584. derSz = wc_EccPublicKeyToDer(userA, der, ECC_BUFSIZE, 1);
  26585. if (derSz < 0) {
  26586. ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
  26587. }
  26588. if (derSz == 0) {
  26589. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26590. }
  26591. #ifdef HAVE_COMP_KEY
  26592. /* test export of compressed public key */
  26593. derSz = wc_EccPublicKeyToDer_ex(userA, der, ECC_BUFSIZE, 1, 1);
  26594. if (derSz < 0) {
  26595. ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
  26596. }
  26597. if (derSz == 0) {
  26598. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26599. }
  26600. #endif
  26601. ret = SaveDerAndPem(der, derSz, eccPubKeyDerFile, NULL, 0);
  26602. if (ret != 0)
  26603. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26604. #ifdef HAVE_PKCS8
  26605. /* test export of PKCS#8 unencrypted private key */
  26606. pkcs8Sz = FOURK_BUF;
  26607. derSz = wc_EccPrivateKeyToPKCS8(userA, der, &pkcs8Sz);
  26608. if (derSz < 0) {
  26609. ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
  26610. }
  26611. if (derSz == 0) {
  26612. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26613. }
  26614. ret = SaveDerAndPem(der, derSz, eccPkcs8KeyDerFile, NULL, 0);
  26615. if (ret != 0) {
  26616. goto done;
  26617. }
  26618. #endif /* HAVE_PKCS8 */
  26619. done:
  26620. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26621. if (der != NULL)
  26622. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26623. if (userA != NULL) {
  26624. wc_ecc_free(userA);
  26625. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26626. }
  26627. #else
  26628. wc_ecc_free(userA);
  26629. #endif
  26630. return ret;
  26631. }
  26632. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  26633. static wc_test_ret_t ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount,
  26634. int curve_id, const ecc_set_type* dp)
  26635. {
  26636. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  26637. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  26638. WC_DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  26639. WC_DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  26640. word32 y;
  26641. #endif
  26642. #ifdef HAVE_ECC_KEY_EXPORT
  26643. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  26644. WC_DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  26645. #endif
  26646. word32 x = 0;
  26647. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  26648. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC)) && \
  26649. defined(HAVE_ECC_SIGN)
  26650. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  26651. WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  26652. int i;
  26653. #ifdef HAVE_ECC_VERIFY
  26654. int verify;
  26655. #endif /* HAVE_ECC_VERIFY */
  26656. #endif /* HAVE_ECC_SIGN */
  26657. wc_test_ret_t ret;
  26658. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26659. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26660. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26661. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26662. #else
  26663. ecc_key userA[1];
  26664. ecc_key userB[1];
  26665. ecc_key pubKey[1];
  26666. #endif
  26667. #ifndef WC_NO_RNG
  26668. int curveSize;
  26669. #endif
  26670. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  26671. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  26672. WC_ALLOC_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  26673. WC_ALLOC_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  26674. #endif
  26675. #ifdef HAVE_ECC_KEY_EXPORT
  26676. WC_ALLOC_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  26677. #endif
  26678. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  26679. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC)) && \
  26680. defined(HAVE_ECC_SIGN)
  26681. WC_ALLOC_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  26682. WC_ALLOC_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  26683. #endif
  26684. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  26685. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  26686. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  26687. if (sharedA == NULL || sharedB == NULL)
  26688. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26689. #endif
  26690. #ifdef HAVE_ECC_KEY_EXPORT
  26691. if (exportBuf == NULL)
  26692. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26693. #endif
  26694. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  26695. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC)) && \
  26696. defined(HAVE_ECC_SIGN)
  26697. if (sig == NULL || digest == NULL)
  26698. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26699. #endif
  26700. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  26701. (void)testVerifyCount;
  26702. (void)dp;
  26703. (void)x;
  26704. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26705. if ((userA == NULL) ||
  26706. (userB == NULL) ||
  26707. (pubKey == NULL))
  26708. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26709. #endif
  26710. XMEMSET(userA, 0, sizeof *userA);
  26711. XMEMSET(userB, 0, sizeof *userB);
  26712. XMEMSET(pubKey, 0, sizeof *pubKey);
  26713. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  26714. if (ret != 0)
  26715. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26716. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  26717. if (ret != 0)
  26718. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26719. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  26720. if (ret != 0)
  26721. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26722. #ifdef WOLFSSL_CUSTOM_CURVES
  26723. if (dp != NULL) {
  26724. ret = wc_ecc_set_custom_curve(userA, dp);
  26725. if (ret != 0)
  26726. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26727. ret = wc_ecc_set_custom_curve(userB, dp);
  26728. if (ret != 0)
  26729. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26730. }
  26731. #endif
  26732. #ifndef WC_NO_RNG
  26733. ret = wc_ecc_make_key_ex(rng, keySize, userA, curve_id);
  26734. #if defined(WOLFSSL_ASYNC_CRYPT)
  26735. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  26736. #endif
  26737. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  26738. if (ret == NO_VALID_DEVID) {
  26739. ret = 0;
  26740. goto done; /* no software case */
  26741. }
  26742. #endif
  26743. if (ret == ECC_CURVE_OID_E)
  26744. goto done; /* catch case, where curve is not supported */
  26745. if (ret != 0)
  26746. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26747. TEST_SLEEP();
  26748. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  26749. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  26750. if (curveSize != userA->dp->size)
  26751. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26752. }
  26753. ret = wc_ecc_check_key(userA);
  26754. if (ret != 0)
  26755. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26756. TEST_SLEEP();
  26757. /* ATECC508/608 configuration may not support more than one ECDH key */
  26758. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  26759. ret = wc_ecc_make_key_ex(rng, keySize, userB, curve_id);
  26760. #if defined(WOLFSSL_ASYNC_CRYPT)
  26761. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  26762. #endif
  26763. if (ret != 0)
  26764. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26765. TEST_SLEEP();
  26766. /* only perform the below tests if the key size matches */
  26767. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  26768. /* Not an error, just not a key size match */
  26769. WARNING_OUT(ECC_CURVE_OID_E, done);
  26770. #ifdef HAVE_ECC_DHE
  26771. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  26772. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  26773. !defined(HAVE_SELFTEST)
  26774. ret = wc_ecc_set_rng(userA, rng);
  26775. if (ret != 0)
  26776. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26777. ret = wc_ecc_set_rng(userB, rng);
  26778. if (ret != 0)
  26779. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26780. #endif
  26781. x = ECC_SHARED_SIZE;
  26782. do {
  26783. #if defined(WOLFSSL_ASYNC_CRYPT)
  26784. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26785. #endif
  26786. if (ret == 0)
  26787. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  26788. } while (ret == WC_PENDING_E);
  26789. if (ret != 0)
  26790. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26791. TEST_SLEEP();
  26792. y = ECC_SHARED_SIZE;
  26793. do {
  26794. #if defined(WOLFSSL_ASYNC_CRYPT)
  26795. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26796. #endif
  26797. if (ret == 0)
  26798. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  26799. } while (ret == WC_PENDING_E);
  26800. if (ret != 0)
  26801. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26802. if (y != x)
  26803. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26804. if (XMEMCMP(sharedA, sharedB, x))
  26805. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26806. TEST_SLEEP();
  26807. #ifdef HAVE_ECC_CDH
  26808. /* add cofactor flag */
  26809. wc_ecc_set_flags(userA, WC_ECC_FLAG_COFACTOR);
  26810. wc_ecc_set_flags(userB, WC_ECC_FLAG_COFACTOR);
  26811. x = ECC_SHARED_SIZE;
  26812. do {
  26813. #if defined(WOLFSSL_ASYNC_CRYPT)
  26814. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26815. #endif
  26816. if (ret == 0)
  26817. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  26818. } while (ret == WC_PENDING_E);
  26819. if (ret != 0)
  26820. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26821. TEST_SLEEP();
  26822. y = ECC_SHARED_SIZE;
  26823. do {
  26824. #if defined(WOLFSSL_ASYNC_CRYPT)
  26825. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26826. #endif
  26827. if (ret == 0)
  26828. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  26829. } while (ret == WC_PENDING_E);
  26830. if (ret != 0)
  26831. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26832. if (y != x)
  26833. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26834. if (XMEMCMP(sharedA, sharedB, x))
  26835. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26836. TEST_SLEEP();
  26837. /* remove cofactor flag */
  26838. wc_ecc_set_flags(userA, 0);
  26839. wc_ecc_set_flags(userB, 0);
  26840. #endif /* HAVE_ECC_CDH */
  26841. #endif /* HAVE_ECC_DHE */
  26842. #endif /* !WOLFSSL_ATECC508A && WOLFSSL_ATECC608A */
  26843. #ifdef HAVE_ECC_KEY_EXPORT
  26844. x = ECC_KEY_EXPORT_BUF_SIZE;
  26845. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  26846. if (ret != 0)
  26847. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26848. #ifdef HAVE_ECC_KEY_IMPORT
  26849. #ifdef WOLFSSL_CUSTOM_CURVES
  26850. if (dp != NULL) {
  26851. ret = wc_ecc_set_custom_curve(pubKey, dp);
  26852. if (ret != 0)
  26853. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26854. }
  26855. #endif
  26856. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  26857. if (ret != 0)
  26858. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26859. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  26860. #ifdef HAVE_ECC_DHE
  26861. y = ECC_SHARED_SIZE;
  26862. do {
  26863. #if defined(WOLFSSL_ASYNC_CRYPT)
  26864. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26865. #endif
  26866. if (ret == 0)
  26867. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  26868. } while (ret == WC_PENDING_E);
  26869. if (ret != 0)
  26870. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26871. if (XMEMCMP(sharedA, sharedB, y))
  26872. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26873. TEST_SLEEP();
  26874. #endif /* HAVE_ECC_DHE */
  26875. #ifdef HAVE_COMP_KEY
  26876. /* try compressed export / import too */
  26877. x = ECC_KEY_EXPORT_BUF_SIZE;
  26878. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  26879. if (ret != 0)
  26880. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26881. wc_ecc_free(pubKey);
  26882. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  26883. if (ret != 0)
  26884. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26885. #ifdef WOLFSSL_CUSTOM_CURVES
  26886. if (dp != NULL) {
  26887. ret = wc_ecc_set_custom_curve(pubKey, dp);
  26888. if (ret != 0)
  26889. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26890. }
  26891. #endif
  26892. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  26893. if (ret != 0)
  26894. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26895. #ifdef HAVE_ECC_DHE
  26896. y = ECC_SHARED_SIZE;
  26897. do {
  26898. #if defined(WOLFSSL_ASYNC_CRYPT)
  26899. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26900. #endif
  26901. if (ret == 0)
  26902. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  26903. } while (ret == WC_PENDING_E);
  26904. if (ret != 0)
  26905. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26906. if (XMEMCMP(sharedA, sharedB, y))
  26907. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26908. TEST_SLEEP();
  26909. #endif /* HAVE_ECC_DHE */
  26910. #endif /* HAVE_COMP_KEY */
  26911. #endif /* !WOLFSSL_ATECC508A && !WOLFSSL_ATECC608A */
  26912. #endif /* !WC_NO_RNG */
  26913. #endif /* HAVE_ECC_KEY_IMPORT */
  26914. #endif /* HAVE_ECC_KEY_EXPORT */
  26915. /* For KCAPI cannot sign using generated ECDH key */
  26916. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  26917. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC))
  26918. #ifdef HAVE_ECC_SIGN
  26919. /* ECC w/out Shamir has issue with all 0 digest */
  26920. /* WC_BIGINT doesn't have 0 len well on hardware */
  26921. /* Cryptocell has issues with all 0 digest */
  26922. #if defined(ECC_SHAMIR) && !defined(WOLFSSL_ASYNC_CRYPT) && \
  26923. !defined(WOLFSSL_CRYPTOCELL)
  26924. /* test DSA sign hash with zeros */
  26925. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  26926. digest[i] = 0;
  26927. }
  26928. x = ECC_SIG_SIZE;
  26929. do {
  26930. #if defined(WOLFSSL_ASYNC_CRYPT)
  26931. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26932. #endif
  26933. if (ret == 0)
  26934. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  26935. userA);
  26936. } while (ret == WC_PENDING_E);
  26937. if (ret != 0)
  26938. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26939. TEST_SLEEP();
  26940. #ifdef HAVE_ECC_VERIFY
  26941. for (i=0; i<testVerifyCount; i++) {
  26942. verify = 0;
  26943. do {
  26944. #if defined(WOLFSSL_ASYNC_CRYPT)
  26945. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26946. #endif
  26947. if (ret == 0)
  26948. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  26949. &verify, userA);
  26950. } while (ret == WC_PENDING_E);
  26951. if (ret != 0)
  26952. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26953. if (verify != 1)
  26954. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26955. TEST_SLEEP();
  26956. }
  26957. #endif /* HAVE_ECC_VERIFY */
  26958. #endif /* ECC_SHAMIR && !WOLFSSL_ASYNC_CRYPT && !WOLFSSL_CRYPTOCELL */
  26959. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  26960. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  26961. digest[i] = (byte)i;
  26962. }
  26963. x = ECC_SIG_SIZE;
  26964. do {
  26965. #if defined(WOLFSSL_ASYNC_CRYPT)
  26966. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26967. #endif
  26968. if (ret == 0)
  26969. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  26970. } while (ret == WC_PENDING_E);
  26971. if (ret != 0)
  26972. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26973. TEST_SLEEP();
  26974. #ifdef HAVE_ECC_VERIFY
  26975. for (i=0; i<testVerifyCount; i++) {
  26976. verify = 0;
  26977. do {
  26978. #if defined(WOLFSSL_ASYNC_CRYPT)
  26979. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26980. #endif
  26981. if (ret == 0)
  26982. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify, userA);
  26983. } while (ret == WC_PENDING_E);
  26984. if (ret != 0)
  26985. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26986. if (verify != 1)
  26987. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26988. TEST_SLEEP();
  26989. }
  26990. #endif /* HAVE_ECC_VERIFY */
  26991. #endif /* HAVE_ECC_SIGN */
  26992. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT &&
  26993. * !WC_NO_RNG && !WOLFSSL_KCAPI_ECC) */
  26994. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  26995. !defined(WOLFSSL_ATECC508) && !defined(WOLFSSL_ATECC608A) && \
  26996. !defined(WOLFSSL_KCAPI_ECC)
  26997. x = ECC_KEY_EXPORT_BUF_SIZE;
  26998. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  26999. if (ret != 0)
  27000. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27001. #elif defined(HAVE_ECC_KEY_EXPORT)
  27002. (void)exportBuf;
  27003. #endif /* HAVE_ECC_KEY_EXPORT */
  27004. done:
  27005. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27006. if (userA != NULL) {
  27007. wc_ecc_free(userA);
  27008. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27009. }
  27010. if (userB != NULL) {
  27011. wc_ecc_free(userB);
  27012. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27013. }
  27014. if (pubKey != NULL) {
  27015. wc_ecc_free(pubKey);
  27016. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27017. }
  27018. #else
  27019. wc_ecc_free(pubKey);
  27020. wc_ecc_free(userB);
  27021. wc_ecc_free(userA);
  27022. #endif
  27023. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  27024. WC_FREE_VAR(sharedA, HEAP_HINT);
  27025. WC_FREE_VAR(sharedB, HEAP_HINT);
  27026. #endif
  27027. #ifdef HAVE_ECC_KEY_EXPORT
  27028. WC_FREE_VAR(exportBuf, HEAP_HINT);
  27029. #endif
  27030. #ifdef HAVE_ECC_SIGN
  27031. WC_FREE_VAR(sig, HEAP_HINT);
  27032. WC_FREE_VAR(digest, HEAP_HINT);
  27033. #endif
  27034. (void)keySize;
  27035. (void)curve_id;
  27036. (void)rng;
  27037. return ret;
  27038. }
  27039. #undef ECC_TEST_VERIFY_COUNT
  27040. #define ECC_TEST_VERIFY_COUNT 2
  27041. static wc_test_ret_t ecc_test_curve(WC_RNG* rng, int keySize, int curve_id)
  27042. {
  27043. wc_test_ret_t ret;
  27044. WOLFSSL_MSG_EX("ecc_test_curve keySize = %d", keySize);
  27045. #if FIPS_VERSION3_GE(6,0,0)
  27046. #ifdef DEBUG_WOLFSSL
  27047. printf("keySize is %d\n", keySize);
  27048. #endif
  27049. if (keySize < WC_ECC_FIPS_GEN_MIN) {
  27050. goto skip_A;
  27051. }
  27052. #endif
  27053. ret = ecc_test_curve_size(rng, keySize, ECC_TEST_VERIFY_COUNT, curve_id,
  27054. NULL);
  27055. if (ret < 0) {
  27056. if (ret == ECC_CURVE_OID_E) {
  27057. /* ignore error for curves not found */
  27058. /* some curve sizes are only available with:
  27059. HAVE_ECC_SECPR2, HAVE_ECC_SECPR3, HAVE_ECC_BRAINPOOL
  27060. and HAVE_ECC_KOBLITZ */
  27061. }
  27062. else {
  27063. printf("ecc_test_curve_size %d failed!\n", keySize);
  27064. return ret;
  27065. }
  27066. }
  27067. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  27068. #if FIPS_VERSION3_GE(6,0,0)
  27069. skip_A:
  27070. #endif
  27071. #ifdef HAVE_ECC_VECTOR_TEST
  27072. ret = ecc_test_vector(keySize);
  27073. if (ret < 0) {
  27074. printf("ecc_test_vector %d failed!\n", keySize);
  27075. return ret;
  27076. }
  27077. #endif
  27078. #if FIPS_VERSION3_GE(6,0,0)
  27079. if (keySize < WC_ECC_FIPS_GEN_MIN) {
  27080. goto skip_B;
  27081. }
  27082. #endif
  27083. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  27084. !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  27085. ret = ecc_test_key_decode(rng, keySize);
  27086. if (ret < 0) {
  27087. if (ret == ECC_CURVE_OID_E) {
  27088. /* ignore error for curves not found */
  27089. }
  27090. else {
  27091. printf("ecc_test_key_decode %d failed!\n", keySize);
  27092. return ret;
  27093. }
  27094. }
  27095. #endif
  27096. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  27097. ret = ecc_test_key_gen(rng, keySize);
  27098. if (ret < 0) {
  27099. if (ret == ECC_CURVE_OID_E) {
  27100. /* ignore error for curves not found */
  27101. }
  27102. else {
  27103. printf("ecc_test_key_gen %d failed!\n", keySize);
  27104. return ret;
  27105. }
  27106. }
  27107. #endif
  27108. #if FIPS_VERSION3_GE(6,0,0)
  27109. skip_B:
  27110. #endif
  27111. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  27112. return 0;
  27113. }
  27114. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  27115. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  27116. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  27117. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  27118. static wc_test_ret_t ecc_point_test(void)
  27119. {
  27120. wc_test_ret_t ret;
  27121. ecc_point* point;
  27122. ecc_point* point2;
  27123. #ifdef HAVE_COMP_KEY
  27124. ecc_point* point3;
  27125. ecc_point* point4;
  27126. #endif
  27127. word32 outLen;
  27128. byte out[65];
  27129. byte der[] = { 0x04, /* = Uncompressed */
  27130. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27131. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27132. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27133. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27134. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27135. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27136. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27137. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  27138. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  27139. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  27140. byte derComp0[] = { 0x02, /* = Compressed, y even */
  27141. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27142. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27143. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27144. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  27145. byte derComp1[] = { 0x03, /* = Compressed, y odd */
  27146. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27147. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27148. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27149. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  27150. #endif
  27151. byte altDer[] = { 0x04, /* = Uncompressed */
  27152. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27153. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27154. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27155. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27156. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27157. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27158. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27159. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  27160. int curve_idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
  27161. /* if curve P256 is not enabled then test should not fail */
  27162. if (curve_idx == ECC_CURVE_INVALID)
  27163. return 0;
  27164. outLen = sizeof(out);
  27165. point = wc_ecc_new_point();
  27166. if (point == NULL)
  27167. return WC_TEST_RET_ENC_ERRNO;
  27168. point2 = wc_ecc_new_point();
  27169. if (point2 == NULL) {
  27170. wc_ecc_del_point(point);
  27171. return WC_TEST_RET_ENC_NC;
  27172. }
  27173. #ifdef HAVE_COMP_KEY
  27174. point3 = wc_ecc_new_point();
  27175. if (point3 == NULL) {
  27176. wc_ecc_del_point(point2);
  27177. wc_ecc_del_point(point);
  27178. return WC_TEST_RET_ENC_NC;
  27179. }
  27180. point4 = wc_ecc_new_point();
  27181. if (point4 == NULL) {
  27182. wc_ecc_del_point(point3);
  27183. wc_ecc_del_point(point2);
  27184. wc_ecc_del_point(point);
  27185. return WC_TEST_RET_ENC_NC;
  27186. }
  27187. #endif
  27188. /* Parameter Validation testing. */
  27189. wc_ecc_del_point(NULL);
  27190. ret = wc_ecc_import_point_der(NULL, sizeof(der), curve_idx, point);
  27191. if (ret != ECC_BAD_ARG_E) {
  27192. ret = WC_TEST_RET_ENC_EC(ret);
  27193. goto done;
  27194. }
  27195. ret = wc_ecc_import_point_der(der, sizeof(der), ECC_CURVE_INVALID, point);
  27196. if (ret != ECC_BAD_ARG_E) {
  27197. ret = WC_TEST_RET_ENC_EC(ret);
  27198. goto done;
  27199. }
  27200. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, NULL);
  27201. if (ret != ECC_BAD_ARG_E) {
  27202. ret = WC_TEST_RET_ENC_EC(ret);
  27203. goto done;
  27204. }
  27205. ret = wc_ecc_export_point_der(-1, point, out, &outLen);
  27206. if (ret != ECC_BAD_ARG_E) {
  27207. ret = WC_TEST_RET_ENC_EC(ret);
  27208. goto done;
  27209. }
  27210. ret = wc_ecc_export_point_der(curve_idx, NULL, out, &outLen);
  27211. if (ret != ECC_BAD_ARG_E) {
  27212. ret = WC_TEST_RET_ENC_EC(ret);
  27213. goto done;
  27214. }
  27215. ret = wc_ecc_export_point_der(curve_idx, point, NULL, &outLen);
  27216. if (ret != LENGTH_ONLY_E || outLen != sizeof(out)) {
  27217. ret = WC_TEST_RET_ENC_EC(ret);
  27218. goto done;
  27219. }
  27220. ret = wc_ecc_export_point_der(curve_idx, point, out, NULL);
  27221. if (ret != ECC_BAD_ARG_E) {
  27222. ret = WC_TEST_RET_ENC_EC(ret);
  27223. goto done;
  27224. }
  27225. outLen = 0;
  27226. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  27227. if (ret != BUFFER_E) {
  27228. ret = WC_TEST_RET_ENC_EC(ret);
  27229. goto done;
  27230. }
  27231. ret = wc_ecc_copy_point(NULL, NULL);
  27232. if (ret != ECC_BAD_ARG_E) {
  27233. ret = WC_TEST_RET_ENC_EC(ret);
  27234. goto done;
  27235. }
  27236. ret = wc_ecc_copy_point(NULL, point2);
  27237. if (ret != ECC_BAD_ARG_E) {
  27238. ret = WC_TEST_RET_ENC_EC(ret);
  27239. goto done;
  27240. }
  27241. ret = wc_ecc_copy_point(point, NULL);
  27242. if (ret != ECC_BAD_ARG_E) {
  27243. ret = WC_TEST_RET_ENC_EC(ret);
  27244. goto done;
  27245. }
  27246. ret = wc_ecc_cmp_point(NULL, NULL);
  27247. if (ret != BAD_FUNC_ARG) {
  27248. ret = WC_TEST_RET_ENC_EC(ret);
  27249. goto done;
  27250. }
  27251. ret = wc_ecc_cmp_point(NULL, point2);
  27252. if (ret != BAD_FUNC_ARG) {
  27253. ret = WC_TEST_RET_ENC_EC(ret);
  27254. goto done;
  27255. }
  27256. ret = wc_ecc_cmp_point(point, NULL);
  27257. if (ret != BAD_FUNC_ARG) {
  27258. ret = WC_TEST_RET_ENC_EC(ret);
  27259. goto done;
  27260. }
  27261. /* Use API. */
  27262. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, point);
  27263. if (ret != 0) {
  27264. ret = WC_TEST_RET_ENC_EC(ret);
  27265. goto done;
  27266. }
  27267. outLen = sizeof(out);
  27268. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  27269. if (ret != 0) {
  27270. ret = WC_TEST_RET_ENC_EC(ret);
  27271. goto done;
  27272. }
  27273. if (outLen != sizeof(der)) {
  27274. ret = WC_TEST_RET_ENC_NC;
  27275. goto done;
  27276. }
  27277. if (XMEMCMP(out, der, outLen) != 0) {
  27278. ret = WC_TEST_RET_ENC_NC;
  27279. goto done;
  27280. }
  27281. ret = wc_ecc_copy_point(point2, point);
  27282. if (ret != MP_OKAY) {
  27283. ret = WC_TEST_RET_ENC_EC(ret);
  27284. goto done;
  27285. }
  27286. ret = wc_ecc_cmp_point(point2, point);
  27287. if (ret != MP_EQ) {
  27288. ret = WC_TEST_RET_ENC_EC(ret);
  27289. goto done;
  27290. }
  27291. ret = wc_ecc_import_point_der(altDer, sizeof(altDer), curve_idx, point2);
  27292. if (ret != 0) {
  27293. ret = WC_TEST_RET_ENC_EC(ret);
  27294. goto done;
  27295. }
  27296. ret = wc_ecc_cmp_point(point2, point);
  27297. if (ret != MP_GT) {
  27298. ret = WC_TEST_RET_ENC_EC(ret);
  27299. goto done;
  27300. }
  27301. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  27302. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  27303. ret = wc_ecc_import_point_der(derComp0, sizeof(derComp0)*2-1, curve_idx, point3);
  27304. if (ret != 0) {
  27305. ret = WC_TEST_RET_ENC_EC(ret);
  27306. goto done;
  27307. }
  27308. ret = wc_ecc_import_point_der_ex(derComp0, sizeof(derComp0), curve_idx, point4, 0);
  27309. if (ret != 0) {
  27310. ret = WC_TEST_RET_ENC_EC(ret);
  27311. goto done;
  27312. }
  27313. ret = wc_ecc_cmp_point(point3, point4);
  27314. if (ret != MP_EQ) {
  27315. ret = WC_TEST_RET_ENC_EC(ret);
  27316. goto done;
  27317. }
  27318. ret = wc_ecc_import_point_der(derComp1, sizeof(derComp1)*2-1, curve_idx, point3);
  27319. if (ret != 0) {
  27320. ret = WC_TEST_RET_ENC_EC(ret);
  27321. goto done;
  27322. }
  27323. ret = wc_ecc_import_point_der_ex(derComp1, sizeof(derComp1), curve_idx, point4, 0);
  27324. if (ret != 0) {
  27325. ret = WC_TEST_RET_ENC_EC(ret);
  27326. goto done;
  27327. }
  27328. ret = wc_ecc_cmp_point(point3, point4);
  27329. if (ret != MP_EQ) {
  27330. ret = WC_TEST_RET_ENC_EC(ret);
  27331. goto done;
  27332. }
  27333. #endif
  27334. done:
  27335. #ifdef HAVE_COMP_KEY
  27336. wc_ecc_del_point(point4);
  27337. wc_ecc_del_point(point3);
  27338. #endif
  27339. wc_ecc_del_point(point2);
  27340. wc_ecc_del_point(point);
  27341. return ret;
  27342. }
  27343. #endif /* !WOLFSSL_ATECC508A && HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  27344. #if !defined(NO_SIG_WRAPPER) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  27345. static wc_test_ret_t ecc_sig_test(WC_RNG* rng, ecc_key* key)
  27346. {
  27347. wc_test_ret_t ret;
  27348. word32 sigSz;
  27349. int size;
  27350. byte out[ECC_MAX_SIG_SIZE];
  27351. byte in[] = TEST_STRING;
  27352. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  27353. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  27354. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  27355. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  27356. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  27357. };
  27358. word32 inLen = (word32)XSTRLEN((char*)in);
  27359. size = wc_ecc_sig_size(key);
  27360. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, sizeof(*key));
  27361. if (ret != size)
  27362. return WC_TEST_RET_ENC_NC;
  27363. sigSz = (word32)ret;
  27364. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  27365. inLen, out, &sigSz, key, sizeof(*key), rng);
  27366. if (ret != 0)
  27367. return WC_TEST_RET_ENC_EC(ret);
  27368. TEST_SLEEP();
  27369. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  27370. inLen, out, sigSz, key, sizeof(*key));
  27371. if (ret != 0)
  27372. return WC_TEST_RET_ENC_EC(ret);
  27373. TEST_SLEEP();
  27374. sigSz = (word32)sizeof(out);
  27375. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  27376. hash, (int)sizeof(hash), out, &sigSz, key, sizeof(*key), rng);
  27377. if (ret != 0)
  27378. return WC_TEST_RET_ENC_EC(ret);
  27379. TEST_SLEEP();
  27380. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  27381. hash, (int)sizeof(hash), out, sigSz, key, sizeof(*key));
  27382. if (ret != 0)
  27383. return WC_TEST_RET_ENC_EC(ret);
  27384. TEST_SLEEP();
  27385. return 0;
  27386. }
  27387. #endif
  27388. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  27389. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  27390. static wc_test_ret_t ecc_exp_imp_test(ecc_key* key)
  27391. {
  27392. wc_test_ret_t ret;
  27393. int curve_id;
  27394. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27395. ecc_key *keyImp = (ecc_key *)XMALLOC(sizeof *keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27396. #else
  27397. ecc_key keyImp[1];
  27398. #endif
  27399. byte priv[32];
  27400. word32 privLen;
  27401. byte pub[65*2];
  27402. word32 pubLen, pubLenX, pubLenY;
  27403. const char qx[] = "7a4e287890a1a47ad3457e52f2f76a83"
  27404. "ce46cbc947616d0cbaa82323818a793d";
  27405. const char qy[] = "eec4084f5b29ebf29c44cce3b3059610"
  27406. "922f8b30ea6e8811742ac7238fe87308";
  27407. const char d[] = "8c14b793cb19137e323a6d2e2a870bca"
  27408. "2e7a493ec1153b3a95feb8a4873f8d08";
  27409. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27410. if (keyImp == NULL)
  27411. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  27412. #endif
  27413. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  27414. privLen = sizeof(priv);
  27415. ret = wc_ecc_export_private_only(key, priv, &privLen);
  27416. if (ret != 0) {
  27417. ret = WC_TEST_RET_ENC_EC(ret);
  27418. goto done;
  27419. }
  27420. pubLen = sizeof(pub);
  27421. ret = wc_ecc_export_point_der(key->idx, &key->pubkey, pub, &pubLen);
  27422. if (ret != 0) {
  27423. ret = WC_TEST_RET_ENC_EC(ret);
  27424. goto done;
  27425. }
  27426. ret = wc_ecc_import_private_key(priv, privLen, pub, pubLen, keyImp);
  27427. if (ret != 0) {
  27428. ret = WC_TEST_RET_ENC_EC(ret);
  27429. goto done;
  27430. }
  27431. wc_ecc_free(keyImp);
  27432. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  27433. ret = wc_ecc_import_raw_ex(keyImp, qx, qy, d, ECC_SECP256R1);
  27434. if (ret != 0) {
  27435. ret = WC_TEST_RET_ENC_EC(ret);
  27436. goto done;
  27437. }
  27438. wc_ecc_free(keyImp);
  27439. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  27440. curve_id = wc_ecc_get_curve_id(key->idx);
  27441. if (curve_id < 0) {
  27442. ret = WC_TEST_RET_ENC_EC(curve_id);
  27443. goto done;
  27444. }
  27445. /* test import private only */
  27446. ret = wc_ecc_import_private_key_ex(priv, privLen, NULL, 0, keyImp,
  27447. curve_id);
  27448. if (ret != 0) {
  27449. ret = WC_TEST_RET_ENC_EC(ret);
  27450. goto done;
  27451. }
  27452. wc_ecc_free(keyImp);
  27453. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  27454. /* test export public raw */
  27455. pubLenX = pubLenY = 32;
  27456. ret = wc_ecc_export_public_raw(key, pub, &pubLenX, &pub[32], &pubLenY);
  27457. if (ret != 0) {
  27458. ret = WC_TEST_RET_ENC_EC(ret);
  27459. goto done;
  27460. }
  27461. #ifndef HAVE_SELFTEST
  27462. /* test import of public */
  27463. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], NULL, ECC_SECP256R1);
  27464. if (ret != 0) {
  27465. ret = WC_TEST_RET_ENC_EC(ret);
  27466. goto done;
  27467. }
  27468. #endif
  27469. wc_ecc_free(keyImp);
  27470. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  27471. /* test export private and public raw */
  27472. pubLenX = pubLenY = privLen = 32;
  27473. ret = wc_ecc_export_private_raw(key, pub, &pubLenX, &pub[32], &pubLenY,
  27474. priv, &privLen);
  27475. if (ret != 0) {
  27476. ret = WC_TEST_RET_ENC_EC(ret);
  27477. goto done;
  27478. }
  27479. #ifndef HAVE_SELFTEST
  27480. /* test import of private and public */
  27481. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], priv, ECC_SECP256R1);
  27482. if (ret != 0) {
  27483. ret = WC_TEST_RET_ENC_EC(ret);
  27484. goto done;
  27485. }
  27486. #endif
  27487. done:
  27488. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27489. if (keyImp != NULL) {
  27490. wc_ecc_free(keyImp);
  27491. XFREE(keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27492. }
  27493. #else
  27494. wc_ecc_free(keyImp);
  27495. #endif
  27496. return ret;
  27497. }
  27498. #endif /* HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  27499. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  27500. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  27501. static wc_test_ret_t ecc_mulmod_test(ecc_key* key1)
  27502. {
  27503. wc_test_ret_t ret;
  27504. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27505. ecc_key *key2 = (ecc_key *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27506. ecc_key *key3 = (ecc_key *)XMALLOC(sizeof *key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27507. #else
  27508. ecc_key key2[1];
  27509. ecc_key key3[1];
  27510. #endif
  27511. #ifdef WOLFSSL_PUBLIC_MP
  27512. mp_int* priv;
  27513. #endif
  27514. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27515. if ((key2 == NULL) || (key3 == NULL))
  27516. ERROR_OUT(MEMORY_E, done);
  27517. #endif
  27518. wc_ecc_init_ex(key2, HEAP_HINT, devId);
  27519. wc_ecc_init_ex(key3, HEAP_HINT, devId);
  27520. /* TODO: Use test data, test with WOLFSSL_VALIDATE_ECC_IMPORT. */
  27521. /* Need base point (Gx,Gy) and parameter A - load them as the public and
  27522. * private key in key2.
  27523. */
  27524. ret = wc_ecc_import_raw_ex(key2, key1->dp->Gx, key1->dp->Gy, key1->dp->Af,
  27525. ECC_SECP256R1);
  27526. if (ret != 0)
  27527. goto done;
  27528. /* Need a point (Gx,Gy) and prime - load them as the public and private key
  27529. * in key3.
  27530. */
  27531. ret = wc_ecc_import_raw_ex(key3, key1->dp->Gx, key1->dp->Gy,
  27532. key1->dp->prime, ECC_SECP256R1);
  27533. if (ret != 0)
  27534. goto done;
  27535. ret = wc_ecc_mulmod(wc_ecc_key_get_priv(key1), &key2->pubkey, &key3->pubkey,
  27536. wc_ecc_key_get_priv(key2), wc_ecc_key_get_priv(key3),
  27537. 1);
  27538. if (ret != 0) {
  27539. ret = WC_TEST_RET_ENC_EC(ret);
  27540. goto done;
  27541. }
  27542. #ifdef WOLFSSL_PUBLIC_MP
  27543. priv = wc_ecc_key_get_priv(key1);
  27544. mp_zero(priv);
  27545. ret = wc_ecc_mulmod(wc_ecc_key_get_priv(key1), &key2->pubkey, &key3->pubkey,
  27546. wc_ecc_key_get_priv(key2), wc_ecc_key_get_priv(key3),
  27547. 1);
  27548. if (ret != 0) {
  27549. ret = WC_TEST_RET_ENC_EC(ret);
  27550. goto done;
  27551. }
  27552. if (!wc_ecc_point_is_at_infinity(&key3->pubkey)) {
  27553. ret = WC_TEST_RET_ENC_EC(ret);
  27554. goto done;
  27555. }
  27556. #endif
  27557. done:
  27558. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27559. if (key2 != NULL) {
  27560. wc_ecc_free(key2);
  27561. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27562. }
  27563. if (key3 != NULL) {
  27564. wc_ecc_free(key3);
  27565. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27566. }
  27567. #else
  27568. wc_ecc_free(key3);
  27569. wc_ecc_free(key2);
  27570. #endif
  27571. return ret;
  27572. }
  27573. #endif
  27574. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  27575. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(WOLFSSL_ATECC508A) && \
  27576. !defined(WOLFSSL_ATECC608A) && !defined(PLUTON_CRYPTO_ECC) && \
  27577. !defined(WOLFSSL_CRYPTOCELL)
  27578. static wc_test_ret_t ecc_ssh_test(ecc_key* key, WC_RNG* rng)
  27579. {
  27580. wc_test_ret_t ret;
  27581. byte out[128];
  27582. word32 outLen = sizeof(out);
  27583. /* Parameter Validation testing. */
  27584. ret = wc_ecc_shared_secret_ssh(NULL, &key->pubkey, out, &outLen);
  27585. if (ret != BAD_FUNC_ARG)
  27586. return WC_TEST_RET_ENC_EC(ret);
  27587. ret = wc_ecc_shared_secret_ssh(key, NULL, out, &outLen);
  27588. if (ret != BAD_FUNC_ARG)
  27589. return WC_TEST_RET_ENC_EC(ret);
  27590. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, NULL, &outLen);
  27591. if (ret != BAD_FUNC_ARG)
  27592. return WC_TEST_RET_ENC_EC(ret);
  27593. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, NULL);
  27594. if (ret != BAD_FUNC_ARG)
  27595. return WC_TEST_RET_ENC_EC(ret);
  27596. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  27597. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  27598. !defined(HAVE_SELFTEST)
  27599. ret = wc_ecc_set_rng(key, rng);
  27600. if (ret != 0)
  27601. return WC_TEST_RET_ENC_EC(ret);
  27602. #else
  27603. (void)rng;
  27604. #endif
  27605. /* Use API. */
  27606. ret = 0;
  27607. do {
  27608. #if defined(WOLFSSL_ASYNC_CRYPT)
  27609. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  27610. #endif
  27611. if (ret == 0)
  27612. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, &outLen);
  27613. } while (ret == WC_PENDING_E);
  27614. if (ret != 0)
  27615. return WC_TEST_RET_ENC_EC(ret);
  27616. TEST_SLEEP();
  27617. return 0;
  27618. }
  27619. #endif /* HAVE_ECC_DHE && !WC_NO_RNG */
  27620. static wc_test_ret_t ecc_def_curve_test(WC_RNG *rng)
  27621. {
  27622. wc_test_ret_t ret;
  27623. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27624. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27625. #else
  27626. ecc_key key[1];
  27627. #endif
  27628. #if !defined(NO_ECC_SECP) && \
  27629. ((defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  27630. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)))
  27631. word32 idx = 0;
  27632. #endif
  27633. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27634. if (key == NULL)
  27635. ERROR_OUT(MEMORY_E, done);
  27636. #endif
  27637. wc_ecc_init_ex(key, HEAP_HINT, devId);
  27638. /* Use API */
  27639. ret = wc_ecc_set_flags(NULL, 0);
  27640. if (ret != BAD_FUNC_ARG) {
  27641. ret = WC_TEST_RET_ENC_EC(ret);
  27642. goto done;
  27643. }
  27644. ret = wc_ecc_set_flags(key, 0);
  27645. if (ret != 0) {
  27646. ret = WC_TEST_RET_ENC_EC(ret);
  27647. goto done;
  27648. }
  27649. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  27650. #ifndef WC_NO_RNG
  27651. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  27652. #if defined(WOLFSSL_ASYNC_CRYPT)
  27653. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  27654. #endif
  27655. if (ret != 0) {
  27656. goto done;
  27657. }
  27658. #ifndef NO_SIG_WRAPPER
  27659. ret = ecc_sig_test(rng, key);
  27660. if (ret < 0)
  27661. goto done;
  27662. #endif
  27663. TEST_SLEEP();
  27664. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_CRYPTOCELL) && \
  27665. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  27666. ret = ecc_ssh_test(key, rng);
  27667. if (ret < 0)
  27668. goto done;
  27669. #endif
  27670. wc_ecc_free(key);
  27671. #else
  27672. (void)rng;
  27673. #endif /* !WC_NO_RNG */
  27674. #if !defined(NO_ECC_SECP) && \
  27675. ((defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  27676. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)))
  27677. /* Use test ECC key - ensure real private "d" exists */
  27678. #if defined(USE_CERT_BUFFERS_256)
  27679. {
  27680. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  27681. sizeof_ecc_key_der_256);
  27682. }
  27683. #elif !defined(NO_FILESYSTEM)
  27684. {
  27685. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  27686. byte der[128];
  27687. word32 derSz;
  27688. if (!file) {
  27689. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  27690. }
  27691. derSz = (word32)XFREAD(der, 1, sizeof(der), file);
  27692. XFCLOSE(file);
  27693. if (derSz == 0)
  27694. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  27695. ret = wc_EccPrivateKeyDecode(der, &idx, key, derSz);
  27696. }
  27697. #else
  27698. {
  27699. (void)idx;
  27700. WOLFSSL_MSG("No file system and USE_CERT_BUFFERS_256 not defined.(3)");
  27701. ERROR_OUT(ASN_PARSE_E, done);
  27702. }
  27703. #endif
  27704. if (ret != 0) {
  27705. goto done;
  27706. }
  27707. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)
  27708. ret = ecc_exp_imp_test(key);
  27709. if (ret < 0)
  27710. goto done;
  27711. #endif
  27712. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  27713. !defined(WOLFSSL_CRYPTOCELL)
  27714. ret = ecc_mulmod_test(key);
  27715. if (ret < 0)
  27716. goto done;
  27717. #endif
  27718. #endif
  27719. #else
  27720. (void)rng;
  27721. (void)idx;
  27722. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  27723. done:
  27724. wc_ecc_free(key);
  27725. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27726. if (key != NULL) {
  27727. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27728. }
  27729. #endif
  27730. return ret;
  27731. }
  27732. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  27733. #if defined(WOLFSSL_CERT_EXT) && \
  27734. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  27735. static wc_test_ret_t ecc_decode_test(void)
  27736. {
  27737. wc_test_ret_t ret;
  27738. word32 inSz;
  27739. word32 inOutIdx;
  27740. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27741. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27742. #else
  27743. ecc_key key[1];
  27744. #endif
  27745. /* SECP256R1 OID: 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 */
  27746. /* This is ecc_clikeypub_der_256. */
  27747. WOLFSSL_SMALL_STACK_STATIC const byte good[] = {
  27748. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
  27749. 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  27750. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xbf, 0xf4,
  27751. 0x0f, 0x44, 0x50, 0x9a, 0x3d, 0xce, 0x9b, 0xb7, 0xf0, 0xc5,
  27752. 0x4d, 0xf5, 0x70, 0x7b, 0xd4, 0xec, 0x24, 0x8e, 0x19, 0x80,
  27753. 0xec, 0x5a, 0x4c, 0xa2, 0x24, 0x03, 0x62, 0x2c, 0x9b, 0xda,
  27754. 0xef, 0xa2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xc6, 0x56,
  27755. 0x95, 0x06, 0xcc, 0x01, 0xa9, 0xbd, 0xf6, 0x75, 0x1a, 0x42,
  27756. 0xf7, 0xbd, 0xa9, 0xb2, 0x36, 0x22, 0x5f, 0xc7, 0x5d, 0x7f,
  27757. 0xb4 };
  27758. WOLFSSL_SMALL_STACK_STATIC const byte badNoObjId[] = { 0x30, 0x08, 0x30, 0x06, 0x03, 0x04,
  27759. 0x00, 0x04, 0x01, 0x01 };
  27760. WOLFSSL_SMALL_STACK_STATIC const byte badOneObjId[] = { 0x30, 0x0a, 0x30, 0x08, 0x06, 0x00,
  27761. 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  27762. WOLFSSL_SMALL_STACK_STATIC const byte badObjId1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x09,
  27763. 0x06, 0x00, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  27764. WOLFSSL_SMALL_STACK_STATIC const byte badObj2d1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00,
  27765. 0x06, 0x07, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  27766. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitStr[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  27767. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  27768. 0x04, 0x04, 0x00, 0x04, 0x01, 0x01 };
  27769. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrLen[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  27770. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  27771. 0x03, 0x05, 0x00, 0x04, 0x01, 0x01 };
  27772. WOLFSSL_SMALL_STACK_STATIC const byte badNoBitStrZero[] = { 0x30, 0x13, 0x30, 0x0a, 0x06, 0x00,
  27773. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  27774. 0x03, 0x03, 0x04, 0x01, 0x01 };
  27775. WOLFSSL_SMALL_STACK_STATIC const byte badPoint[] = { 0x30, 0x12, 0x30, 0x09, 0x06, 0x00,
  27776. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  27777. 0x03, 0x03, 0x00, 0x04, 0x01 };
  27778. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27779. if (key == NULL)
  27780. ERROR_OUT(MEMORY_E, done);
  27781. #endif
  27782. XMEMSET(key, 0, sizeof *key);
  27783. wc_ecc_init_ex(key, HEAP_HINT, devId);
  27784. inSz = sizeof(good);
  27785. ret = wc_EccPublicKeyDecode(NULL, &inOutIdx, key, inSz);
  27786. if (ret != BAD_FUNC_ARG) {
  27787. ret = WC_TEST_RET_ENC_EC(ret);
  27788. goto done;
  27789. }
  27790. ret = wc_EccPublicKeyDecode(good, NULL, key, inSz);
  27791. if (ret != BAD_FUNC_ARG) {
  27792. ret = WC_TEST_RET_ENC_EC(ret);
  27793. goto done;
  27794. }
  27795. ret = wc_EccPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  27796. if (ret != BAD_FUNC_ARG) {
  27797. ret = WC_TEST_RET_ENC_EC(ret);
  27798. goto done;
  27799. }
  27800. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, 0);
  27801. if (ret != BAD_FUNC_ARG) {
  27802. ret = WC_TEST_RET_ENC_EC(ret);
  27803. goto done;
  27804. }
  27805. /* Change offset to produce bad input data. */
  27806. inOutIdx = 2;
  27807. inSz = sizeof(good) - inOutIdx;
  27808. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  27809. if (ret != ASN_PARSE_E) {
  27810. ret = WC_TEST_RET_ENC_EC(ret);
  27811. goto done;
  27812. }
  27813. inOutIdx = 4;
  27814. inSz = sizeof(good) - inOutIdx;
  27815. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  27816. if (ret != ASN_PARSE_E) {
  27817. ret = WC_TEST_RET_ENC_EC(ret);
  27818. goto done;
  27819. }
  27820. /* Bad data. */
  27821. inSz = sizeof(badNoObjId);
  27822. inOutIdx = 0;
  27823. ret = wc_EccPublicKeyDecode(badNoObjId, &inOutIdx, key, inSz);
  27824. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  27825. ret = WC_TEST_RET_ENC_EC(ret);
  27826. goto done;
  27827. }
  27828. inSz = sizeof(badOneObjId);
  27829. inOutIdx = 0;
  27830. ret = wc_EccPublicKeyDecode(badOneObjId, &inOutIdx, key, inSz);
  27831. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  27832. ret = WC_TEST_RET_ENC_EC(ret);
  27833. goto done;
  27834. }
  27835. inSz = sizeof(badObjId1Len);
  27836. inOutIdx = 0;
  27837. ret = wc_EccPublicKeyDecode(badObjId1Len, &inOutIdx, key, inSz);
  27838. if (ret != ASN_PARSE_E) {
  27839. ret = WC_TEST_RET_ENC_EC(ret);
  27840. goto done;
  27841. }
  27842. inSz = sizeof(badObj2d1Len);
  27843. inOutIdx = 0;
  27844. ret = wc_EccPublicKeyDecode(badObj2d1Len, &inOutIdx, key, inSz);
  27845. if (ret != ASN_PARSE_E) {
  27846. ret = WC_TEST_RET_ENC_EC(ret);
  27847. goto done;
  27848. }
  27849. inSz = sizeof(badNotBitStr);
  27850. inOutIdx = 0;
  27851. ret = wc_EccPublicKeyDecode(badNotBitStr, &inOutIdx, key, inSz);
  27852. if (ret != ASN_BITSTR_E && ret != ASN_PARSE_E) {
  27853. ret = WC_TEST_RET_ENC_EC(ret);
  27854. goto done;
  27855. }
  27856. inSz = sizeof(badBitStrLen);
  27857. inOutIdx = 0;
  27858. ret = wc_EccPublicKeyDecode(badBitStrLen, &inOutIdx, key, inSz);
  27859. if (ret != ASN_PARSE_E) {
  27860. ret = WC_TEST_RET_ENC_EC(ret);
  27861. goto done;
  27862. }
  27863. inSz = sizeof(badNoBitStrZero);
  27864. inOutIdx = 0;
  27865. ret = wc_EccPublicKeyDecode(badNoBitStrZero, &inOutIdx, key, inSz);
  27866. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  27867. ret = WC_TEST_RET_ENC_EC(ret);
  27868. goto done;
  27869. }
  27870. inSz = sizeof(badPoint);
  27871. inOutIdx = 0;
  27872. ret = wc_EccPublicKeyDecode(badPoint, &inOutIdx, key, inSz);
  27873. if (ret != ASN_ECC_KEY_E && ret != ASN_PARSE_E) {
  27874. ret = WC_TEST_RET_ENC_EC(ret);
  27875. goto done;
  27876. }
  27877. inSz = sizeof(good);
  27878. inOutIdx = 0;
  27879. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  27880. if (ret != 0) {
  27881. ret = WC_TEST_RET_ENC_EC(ret);
  27882. goto done;
  27883. }
  27884. done:
  27885. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27886. if (key != NULL) {
  27887. wc_ecc_free(key);
  27888. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27889. }
  27890. #else
  27891. wc_ecc_free(key);
  27892. #endif
  27893. return ret;
  27894. }
  27895. #endif /* WOLFSSL_CERT_EXT */
  27896. #ifdef WOLFSSL_CUSTOM_CURVES
  27897. static const byte eccKeyExplicitCurve[] = {
  27898. 0x30, 0x81, 0xf5, 0x30, 0x81, 0xae, 0x06, 0x07,
  27899. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30,
  27900. 0x81, 0xa2, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06,
  27901. 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01,
  27902. 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  27903. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  27904. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  27905. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
  27906. 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00,
  27907. 0x04, 0x01, 0x07, 0x04, 0x41, 0x04, 0x79, 0xbe,
  27908. 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
  27909. 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b,
  27910. 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2,
  27911. 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a,
  27912. 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4,
  27913. 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17,
  27914. 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47,
  27915. 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8, 0x02, 0x21,
  27916. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  27917. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  27918. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  27919. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  27920. 0x41, 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04,
  27921. 0x3c, 0x4c, 0xc9, 0x5e, 0x2e, 0xa2, 0x3d, 0x49,
  27922. 0xcc, 0x5b, 0xff, 0x4f, 0xc9, 0x2e, 0x1d, 0x4a,
  27923. 0xc6, 0x21, 0xf6, 0xf3, 0xe6, 0x0b, 0x4f, 0xa9,
  27924. 0x9d, 0x74, 0x99, 0xdd, 0x97, 0xc7, 0x6e, 0xbe,
  27925. 0x14, 0x2b, 0x39, 0x9d, 0x63, 0xc7, 0x97, 0x0d,
  27926. 0x45, 0x25, 0x40, 0x30, 0x77, 0x05, 0x76, 0x88,
  27927. 0x38, 0x96, 0x29, 0x7d, 0x9c, 0xe1, 0x50, 0xbe,
  27928. 0xac, 0xf0, 0x1d, 0x86, 0xf4, 0x2f, 0x65, 0x0b
  27929. };
  27930. static wc_test_ret_t ecc_test_custom_curves(WC_RNG* rng)
  27931. {
  27932. wc_test_ret_t ret;
  27933. word32 inOutIdx;
  27934. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27935. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27936. #else
  27937. ecc_key key[1];
  27938. #endif
  27939. /* test use of custom curve - using BRAINPOOLP256R1 for test */
  27940. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  27941. #ifndef WOLFSSL_ECC_CURVE_STATIC
  27942. WOLFSSL_SMALL_STACK_STATIC const ecc_oid_t ecc_oid_brainpoolp256r1[] = {
  27943. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07
  27944. };
  27945. #define ecc_oid_brainpoolp256r1_sz \
  27946. (sizeof(ecc_oid_brainpoolp256r1) / sizeof(ecc_oid_t))
  27947. #else
  27948. #define ecc_oid_brainpoolp256r1 { \
  27949. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07 \
  27950. }
  27951. #define ecc_oid_brainpoolp256r1_sz 9
  27952. #endif
  27953. #define ecc_oid_brainpoolp256r1_sum 104
  27954. WOLFSSL_SMALL_STACK_STATIC const ecc_set_type ecc_dp_brainpool256r1 = {
  27955. 32, /* size/bytes */
  27956. ECC_CURVE_CUSTOM, /* ID */
  27957. "BRAINPOOLP256R1", /* curve name */
  27958. "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", /* prime */
  27959. "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", /* A */
  27960. "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", /* B */
  27961. "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", /* order */
  27962. "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", /* Gx */
  27963. "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", /* Gy */
  27964. ecc_oid_brainpoolp256r1, /* oid/oidSz */
  27965. ecc_oid_brainpoolp256r1_sz,
  27966. ecc_oid_brainpoolp256r1_sum, /* oid sum */
  27967. 1, /* cofactor */
  27968. };
  27969. #endif /* HAVE_ECC_BRAINPOOL */
  27970. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27971. if (! key) {
  27972. ret = MEMORY_E;
  27973. goto done;
  27974. }
  27975. #endif
  27976. XMEMSET(key, 0, sizeof *key);
  27977. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  27978. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, ECC_CURVE_DEF,
  27979. &ecc_dp_brainpool256r1);
  27980. if (ret != 0) {
  27981. printf("ECC test for custom curve failed!\n");
  27982. goto done;
  27983. }
  27984. #endif
  27985. #if defined(HAVE_ECC_BRAINPOOL) || defined(HAVE_ECC_KOBLITZ)
  27986. {
  27987. int curve_id;
  27988. #ifdef HAVE_ECC_BRAINPOOL
  27989. curve_id = ECC_BRAINPOOLP256R1;
  27990. #else
  27991. curve_id = ECC_SECP256K1;
  27992. #endif
  27993. /* Test and demonstrate use of non-SECP curve */
  27994. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, curve_id, NULL);
  27995. if (ret < 0) {
  27996. printf("ECC test for curve_id %d failed!\n", curve_id);
  27997. goto done;
  27998. }
  27999. }
  28000. #endif
  28001. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  28002. if (ret != 0) {
  28003. ret = WC_TEST_RET_ENC_EC(ret);
  28004. goto done;
  28005. }
  28006. inOutIdx = 0;
  28007. ret = wc_EccPublicKeyDecode(eccKeyExplicitCurve, &inOutIdx, key,
  28008. sizeof(eccKeyExplicitCurve));
  28009. if (ret != 0)
  28010. ret = WC_TEST_RET_ENC_EC(ret);
  28011. done:
  28012. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28013. if (key) {
  28014. wc_ecc_free(key);
  28015. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28016. }
  28017. #else
  28018. wc_ecc_free(key);
  28019. #endif
  28020. (void)rng;
  28021. return ret;
  28022. }
  28023. #endif /* WOLFSSL_CUSTOM_CURVES */
  28024. #ifdef WOLFSSL_SM2
  28025. #ifdef HAVE_ECC_VERIFY
  28026. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_CUSTOM_CURVES)
  28027. #ifdef HAVE_OID_ENCODING
  28028. #define CODED_SM2P256V1 {1,2,156,10197,1,301}
  28029. #define CODED_SM2P256V1_SZ 6
  28030. #else
  28031. #define CODED_SM2P256V1 {0x06,0x08,0x2A,0x81,0x1C,0xCF,0x55,0x01,0x82,0x2D}
  28032. #define CODED_SM2P256V1_SZ 10
  28033. #endif
  28034. #ifndef WOLFSSL_ECC_CURVE_STATIC
  28035. static const ecc_oid_t ecc_oid_sm2p256v1[] = CODED_SM2P256V1;
  28036. #else
  28037. #define ecc_oid_sm2p256v1 CODED_SM2P256V1
  28038. #endif
  28039. #define ecc_oid_sm2p256v1_sz CODED_SM2P256V1_SZ
  28040. #define ECC_SM2P256V1_TEST 102
  28041. static int test_sm2_verify_caseA2(void)
  28042. {
  28043. ecc_key key;
  28044. int ret, res;
  28045. mp_int r,s;
  28046. /* test key values */
  28047. const char qx[] = "0AE4C7798AA0F119471BEE11825BE46202BB79E2A5844495E97C04FF4DF2548A";
  28048. const char qy[] = "7C0240F88F1CD4E16352A73C17B7F16F07353E53A176D684A9FE0C6BB798E857";
  28049. const char d[] = "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263";
  28050. const ecc_set_type ecc_sm2_A2 = {
  28051. 32, /* size/bytes */
  28052. ECC_SM2P256V1_TEST, /* ID */
  28053. "SM2P256V1_TEST", /* curve name */
  28054. /* from test case A.2 in draft-shen-sm2-ecdsa-02 */
  28055. "8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3", /* prime */
  28056. "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498", /* A */
  28057. "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A", /* B */
  28058. "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7", /* order n */
  28059. "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D", /* Gx */
  28060. "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2", /* Gy */
  28061. ecc_oid_sm2p256v1, /* oid/oidSz */
  28062. ecc_oid_sm2p256v1_sz,
  28063. ECC_SM2P256V1_OID, /* oid sum */
  28064. 1, /* cofactor */
  28065. };
  28066. /* use canned hash value hash = H(ZA||M) */
  28067. const byte hash[] = {
  28068. 0xB5,0x24,0xF5,0x52,0xCD,0x82,0xB8,0xB0,
  28069. 0x28,0x47,0x6E,0x00,0x5C,0x37,0x7F,0xB1,
  28070. 0x9A,0x87,0xE6,0xFC,0x68,0x2D,0x48,0xBB,
  28071. 0x5D,0x42,0xE3,0xD9,0xB9,0xEF,0xFE,0x76
  28072. };
  28073. /* canned r and s */
  28074. const byte rCan[] = {
  28075. 0x40,0xF1,0xEC,0x59,0xF7,0x93,0xD9,0xF4,
  28076. 0x9E,0x09,0xDC,0xEF,0x49,0x13,0x0D,0x41,
  28077. 0x94,0xF7,0x9F,0xB1,0xEE,0xD2,0xCA,0xA5,
  28078. 0x5B,0xAC,0xDB,0x49,0xC4,0xE7,0x55,0xD1
  28079. };
  28080. const byte sCan[] = {
  28081. 0x6F,0xC6,0xDA,0xC3,0x2C,0x5D,0x5C,0xF1,
  28082. 0x0C,0x77,0xDF,0xB2,0x0F,0x7C,0x2E,0xB6,
  28083. 0x67,0xA4,0x57,0x87,0x2F,0xB0,0x9E,0xC5,
  28084. 0x63,0x27,0xA6,0x7E,0xC7,0xDE,0xEB,0xE7
  28085. };
  28086. mp_init(&r);
  28087. mp_init(&s);
  28088. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  28089. if (ret != 0)
  28090. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28091. ret = wc_ecc_set_custom_curve(&key, &ecc_sm2_A2);
  28092. if (ret != 0)
  28093. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28094. ret = wc_ecc_import_raw_ex(&key, qx, qy, d, ECC_SM2P256V1_TEST);
  28095. if (ret != 0)
  28096. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28097. mp_read_unsigned_bin(&r, rCan, sizeof(rCan));
  28098. mp_read_unsigned_bin(&s, sCan, sizeof(sCan));
  28099. ret = wc_ecc_sm2_verify_hash_ex(&r, &s, hash, sizeof(hash), &res, &key);
  28100. if (ret != 0)
  28101. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28102. if (res != 1)
  28103. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28104. done:
  28105. mp_free(&r);
  28106. mp_free(&s);
  28107. wc_ecc_free(&key);
  28108. return ret;
  28109. }
  28110. #endif /* WOLFSSL_PUBLIC_MP && WOLFSSL_CUSTOM_CURVES */
  28111. static int test_sm2_verify_case(void)
  28112. {
  28113. ecc_key key;
  28114. int ret, res;
  28115. /* test key values */
  28116. const char qx[] = "637F1B135036C933DC3F7A8EBB1B7B2FD1DFBD268D4F894B5AD47DBDBECD558F";
  28117. const char qy[] = "E88101D08048E36CCBF61CA38DDF7ABA542B4486E99E49F3A7470A857A096433";
  28118. /* use canned hash value hash = H(ZA||M) */
  28119. const byte hash[] = {
  28120. 0x3B,0xFA,0x5F,0xFB,0xC4,0x27,0x8C,0x9D,
  28121. 0x02,0x3A,0x19,0xCB,0x1E,0xAA,0xD2,0xF1,
  28122. 0x50,0x69,0x5B,0x20
  28123. };
  28124. const byte sig[] = {
  28125. 0x30,0x45,0x02,0x21,0x00,0xD2,0xFC,0xA3,
  28126. 0x88,0xE3,0xDF,0xA3,0x00,0x73,0x9B,0x3C,
  28127. 0x2A,0x0D,0xAD,0x44,0xA2,0xFC,0x62,0xD5,
  28128. 0x6B,0x84,0x54,0xD8,0x40,0x22,0x62,0x3D,
  28129. 0x5C,0xA6,0x61,0x9B,0xE7,0x02,0x20,0x1D,
  28130. 0xB5,0xB5,0xD9,0xD8,0xF1,0x20,0xDD,0x97,
  28131. 0x92,0xBF,0x7E,0x9B,0x3F,0xE6,0x3C,0x4B,
  28132. 0x03,0xD8,0x80,0xBD,0xB7,0x27,0x7E,0x6A,
  28133. 0x84,0x23,0xDE,0x61,0x7C,0x8D,0xDC
  28134. };
  28135. const byte badSig[] = {
  28136. 0x30,0x45,0x02,0x21,0x00,0xD2,0xFC,0xA3,
  28137. 0x88,0xE3,0xDF,0xA3,0x00,0x73,0x9B,0x3C,
  28138. 0x2A,0x0D,0xAD,0x44,0xA2,0xFC,0x62,0xD5,
  28139. 0x6B,0x84,0x54,0xD8,0x40,0x22,0x62,0x3D,
  28140. 0x5C,0xA6,0x61,0x9B,0xE7,0x02,0x20,0x1D,
  28141. 0xB5,0xB5,0xE9,0xD8,0xF1,0x20,0xDD,0x97,
  28142. 0x92,0xBF,0x7E,0x9B,0x3F,0xE6,0x3C,0x4B,
  28143. 0x03,0xD8,0x80,0xBD,0xB7,0x27,0x7E,0x6A,
  28144. 0x84,0x23,0xDE,0x61,0x7C,0x8D,0xDC
  28145. };
  28146. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  28147. if (ret != 0)
  28148. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28149. ret = wc_ecc_import_raw(&key, qx, qy, NULL, "SM2P256V1");
  28150. if (ret != 0)
  28151. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28152. ret = wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash), &res,
  28153. &key);
  28154. if (ret != 0)
  28155. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28156. if (res != 1)
  28157. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28158. /* now test a case that should fail */
  28159. ret = wc_ecc_sm2_verify_hash(badSig, sizeof(badSig), hash, sizeof(hash),
  28160. &res, &key);
  28161. if (ret != 0)
  28162. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28163. if (res == 1)
  28164. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28165. done:
  28166. wc_ecc_free(&key);
  28167. return ret;
  28168. }
  28169. static int ecc_sm2_test_curve(WC_RNG* rng, int testVerifyCount)
  28170. {
  28171. const ecc_set_type* dp = wc_ecc_get_curve_params(
  28172. wc_ecc_get_curve_idx(ECC_SM2P256V1));
  28173. int keySize = 32;
  28174. int curve_id = ECC_SM2P256V1;
  28175. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  28176. WC_DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  28177. WC_DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  28178. #endif
  28179. #ifdef HAVE_ECC_KEY_EXPORT
  28180. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  28181. WC_DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  28182. #endif
  28183. word32 x = 0;
  28184. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  28185. word32 y;
  28186. #endif
  28187. #ifdef HAVE_ECC_SIGN
  28188. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  28189. WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  28190. int i;
  28191. int verify;
  28192. #endif /* HAVE_ECC_SIGN */
  28193. int ret;
  28194. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28195. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28196. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28197. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28198. #else
  28199. ecc_key userA[1];
  28200. ecc_key userB[1];
  28201. ecc_key pubKey[1];
  28202. #endif
  28203. #ifndef WC_NO_RNG
  28204. int curveSize;
  28205. #endif
  28206. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  28207. WC_ALLOC_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  28208. WC_ALLOC_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  28209. #endif
  28210. #ifdef HAVE_ECC_KEY_EXPORT
  28211. WC_ALLOC_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  28212. #endif
  28213. #ifdef HAVE_ECC_SIGN
  28214. WC_ALLOC_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  28215. WC_ALLOC_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  28216. #endif
  28217. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  28218. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  28219. if (sharedA == NULL || sharedB == NULL)
  28220. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28221. #endif
  28222. #ifdef HAVE_ECC_KEY_EXPORT
  28223. if (exportBuf == NULL)
  28224. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28225. #endif
  28226. #ifdef HAVE_ECC_SIGN
  28227. if (sig == NULL || digest == NULL)
  28228. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28229. #endif
  28230. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  28231. (void)testVerifyCount;
  28232. (void)dp;
  28233. (void)x;
  28234. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28235. if ((userA == NULL) ||
  28236. (userB == NULL) ||
  28237. (pubKey == NULL))
  28238. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28239. #endif
  28240. XMEMSET(userA, 0, sizeof *userA);
  28241. XMEMSET(userB, 0, sizeof *userB);
  28242. XMEMSET(pubKey, 0, sizeof *pubKey);
  28243. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  28244. if (ret != 0)
  28245. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28246. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  28247. if (ret != 0)
  28248. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28249. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  28250. if (ret != 0)
  28251. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28252. #ifndef WC_NO_RNG
  28253. ret = wc_ecc_sm2_make_key(rng, userA, WC_ECC_FLAG_NONE);
  28254. if (ret == ECC_CURVE_OID_E)
  28255. goto done; /* catch case, where curve is not supported */
  28256. if (ret != 0)
  28257. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28258. TEST_SLEEP();
  28259. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  28260. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  28261. if (curveSize != userA->dp->size) {
  28262. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28263. }
  28264. }
  28265. ret = wc_ecc_check_key(userA);
  28266. if (ret != 0)
  28267. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28268. TEST_SLEEP();
  28269. ret = wc_ecc_sm2_make_key(rng, userB, WC_ECC_FLAG_NONE);
  28270. if (ret != 0)
  28271. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28272. /* only perform the below tests if the key size matches */
  28273. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  28274. if (ret != 0) {
  28275. ret = ECC_CURVE_OID_E;
  28276. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28277. }
  28278. #ifdef HAVE_ECC_DHE
  28279. #if defined(ECC_TIMING_RESISTANT)
  28280. ret = wc_ecc_set_rng(userA, rng);
  28281. if (ret != 0)
  28282. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28283. ret = wc_ecc_set_rng(userB, rng);
  28284. if (ret != 0)
  28285. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28286. #endif
  28287. x = ECC_SHARED_SIZE;
  28288. ret = wc_ecc_sm2_shared_secret(userA, userB, sharedA, &x);
  28289. if (ret != 0)
  28290. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28291. y = ECC_SHARED_SIZE;
  28292. ret = wc_ecc_sm2_shared_secret(userB, userA, sharedB, &y);
  28293. if (ret != 0)
  28294. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28295. if (y != x)
  28296. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28297. if (XMEMCMP(sharedA, sharedB, x))
  28298. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28299. #endif /* HAVE_ECC_DHE */
  28300. #ifdef HAVE_ECC_KEY_EXPORT
  28301. x = ECC_KEY_EXPORT_BUF_SIZE;
  28302. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  28303. if (ret != 0)
  28304. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28305. #ifdef HAVE_ECC_KEY_IMPORT
  28306. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  28307. if (ret != 0)
  28308. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28309. #ifdef HAVE_ECC_DHE
  28310. y = ECC_SHARED_SIZE;
  28311. ret = wc_ecc_sm2_shared_secret(userB, pubKey, sharedB, &y);
  28312. if (ret != 0)
  28313. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28314. if (XMEMCMP(sharedA, sharedB, y))
  28315. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28316. #endif /* HAVE_ECC_DHE */
  28317. #ifdef HAVE_COMP_KEY
  28318. /* try compressed export / import too */
  28319. x = ECC_KEY_EXPORT_BUF_SIZE;
  28320. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  28321. if (ret != 0)
  28322. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28323. wc_ecc_free(pubKey);
  28324. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  28325. if (ret != 0)
  28326. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28327. #endif
  28328. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  28329. if (ret != 0)
  28330. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28331. #ifdef HAVE_ECC_DHE
  28332. y = ECC_SHARED_SIZE;
  28333. ret = wc_ecc_sm2_shared_secret(userB, pubKey, sharedB, &y);
  28334. if (ret != 0)
  28335. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28336. if (XMEMCMP(sharedA, sharedB, y))
  28337. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28338. #endif /* HAVE_ECC_DHE */
  28339. #endif /* HAVE_ECC_KEY_IMPORT */
  28340. #endif /* HAVE_ECC_KEY_EXPORT */
  28341. #endif /* !WC_NO_RNG */
  28342. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  28343. !defined(WC_NO_RNG))
  28344. #ifdef HAVE_ECC_SIGN
  28345. /* ECC w/out Shamir has issue with all 0 digest */
  28346. /* WC_BIGINT doesn't have 0 len well on hardware */
  28347. /* Cryptocell has issues with all 0 digest */
  28348. #if defined(ECC_SHAMIR)
  28349. /* test DSA sign hash with zeros */
  28350. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  28351. digest[i] = 0;
  28352. }
  28353. x = ECC_SIG_SIZE;
  28354. ret = wc_ecc_sm2_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  28355. if (ret != 0)
  28356. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28357. for (i = 0; i < testVerifyCount; i++) {
  28358. verify = 0;
  28359. ret = wc_ecc_sm2_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify,
  28360. userA);
  28361. if (ret != 0)
  28362. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28363. if (verify != 1)
  28364. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28365. }
  28366. #endif /* ECC_SHAMIR */
  28367. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  28368. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  28369. digest[i] = (byte)i;
  28370. }
  28371. x = ECC_SIG_SIZE;
  28372. ret = wc_ecc_sm2_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  28373. if (ret != 0)
  28374. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28375. for (i = 0; i < testVerifyCount; i++) {
  28376. verify = 0;
  28377. ret = wc_ecc_sm2_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify,
  28378. userA);
  28379. if (ret != 0)
  28380. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28381. if (verify != 1)
  28382. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28383. }
  28384. #endif /* HAVE_ECC_SIGN */
  28385. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT && !WC_NO_RNG) */
  28386. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  28387. x = ECC_KEY_EXPORT_BUF_SIZE;
  28388. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  28389. if (ret != 0)
  28390. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28391. #elif defined(HAVE_ECC_KEY_EXPORT)
  28392. (void)exportBuf;
  28393. #endif /* HAVE_ECC_KEY_EXPORT */
  28394. done:
  28395. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28396. if (userA != NULL) {
  28397. wc_ecc_free(userA);
  28398. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28399. }
  28400. if (userB != NULL) {
  28401. wc_ecc_free(userB);
  28402. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28403. }
  28404. if (pubKey != NULL) {
  28405. wc_ecc_free(pubKey);
  28406. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28407. }
  28408. #else
  28409. wc_ecc_free(pubKey);
  28410. wc_ecc_free(userB);
  28411. wc_ecc_free(userA);
  28412. #endif
  28413. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  28414. WC_FREE_VAR(sharedA, HEAP_HINT);
  28415. WC_FREE_VAR(sharedB, HEAP_HINT);
  28416. #endif
  28417. #ifdef HAVE_ECC_KEY_EXPORT
  28418. WC_FREE_VAR(exportBuf, HEAP_HINT);
  28419. #endif
  28420. #ifdef HAVE_ECC_SIGN
  28421. WC_FREE_VAR(sig, HEAP_HINT);
  28422. WC_FREE_VAR(digest, HEAP_HINT);
  28423. #endif
  28424. (void)keySize;
  28425. (void)curve_id;
  28426. (void)rng;
  28427. return ret;
  28428. }
  28429. #endif /* HAVE_ECC_VERIFY */
  28430. static int test_sm2_create_digest(void)
  28431. {
  28432. const byte msg[] = "message to sign";
  28433. const byte id[] = "0123456789";
  28434. const byte badId[] = "0123556789";
  28435. byte expected[] = {
  28436. 0xdd, 0x4d, 0x65, 0x49, 0xa3, 0x64, 0x76, 0xc0,
  28437. 0x73, 0x05, 0xdc, 0x05, 0x16, 0xb5, 0xee, 0x9f,
  28438. 0x82, 0xf9, 0xe9, 0x7d, 0x01, 0x1a, 0xdc, 0x88,
  28439. 0x5a, 0x59, 0x9c, 0x44, 0xcc, 0x47, 0xa4, 0x78
  28440. };
  28441. ecc_key key;
  28442. int ret;
  28443. /* test key values */
  28444. const char qx[] =
  28445. "af178b7b8740cc9d5b493fbd22049c12621bc27dcc5802e75ff4d045a4158baf";
  28446. const char qy[] =
  28447. "89933faf7a4798f48c5b9b4cd3a7693d54c9e05449946eb489c0dd50a5294805";
  28448. const char d[] =
  28449. "b3e66c2dbfb50c6ff6830c1fac4b51293a2562f9e667052b03df2d4b43c1f34a";
  28450. byte digest[WC_SHA256_DIGEST_SIZE];
  28451. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  28452. if (ret != 0)
  28453. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28454. ret = wc_ecc_import_raw(&key, qx, qy, d, "SM2P256V1");
  28455. if (ret != 0)
  28456. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28457. ret = wc_ecc_sm2_create_digest(id, (int)XSTRLEN((const char*)id),
  28458. msg, (int)XSTRLEN((const char*)msg), WC_HASH_TYPE_SHA256, digest,
  28459. WC_SHA256_DIGEST_SIZE, &key);
  28460. if (ret != 0)
  28461. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28462. if (XMEMCMP(digest, expected, WC_SHA256_DIGEST_SIZE) != 0)
  28463. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28464. ret = wc_ecc_sm2_create_digest(badId, (int)XSTRLEN((const char*)badId),
  28465. msg, (int)XSTRLEN((const char*)msg), WC_HASH_TYPE_SHA256, digest,
  28466. WC_SHA256_DIGEST_SIZE, &key);
  28467. if (ret != 0)
  28468. goto done;
  28469. /* should be different than the previous ID used */
  28470. if (XMEMCMP(digest, expected, WC_SHA256_DIGEST_SIZE) == 0)
  28471. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28472. done:
  28473. wc_ecc_free(&key);
  28474. return ret;
  28475. }
  28476. static int test_sm2_verify(void)
  28477. {
  28478. int ret = 0;
  28479. #ifdef HAVE_ECC_VERIFY
  28480. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_CUSTOM_CURVES)
  28481. ret = test_sm2_verify_caseA2();
  28482. if (ret != 0)
  28483. return ret;
  28484. #endif
  28485. ret = test_sm2_verify_case();
  28486. if (ret != 0)
  28487. return ret;
  28488. #endif /* HAVE_ECC_VERIFY */
  28489. ret = test_sm2_create_digest();
  28490. return ret;
  28491. }
  28492. #endif /* WOLFSSL_SM2 */
  28493. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ECC_SECP) && !defined(NO_ASN_TIME)
  28494. /* Make Cert / Sign example for ECC cert and ECC CA */
  28495. static wc_test_ret_t ecc_test_cert_gen(WC_RNG* rng)
  28496. {
  28497. wc_test_ret_t ret;
  28498. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28499. Cert *myCert = (Cert *)XMALLOC(sizeof *myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28500. #ifdef WOLFSSL_TEST_CERT
  28501. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28502. #endif
  28503. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28504. ecc_key *certPubKey = (ecc_key *)XMALLOC(sizeof *certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28505. #else
  28506. Cert myCert[1];
  28507. #ifdef WOLFSSL_TEST_CERT
  28508. DecodedCert decode[1];
  28509. #endif
  28510. ecc_key caEccKey[1];
  28511. ecc_key certPubKey[1];
  28512. #endif
  28513. int certSz;
  28514. size_t bytes = 0;
  28515. word32 idx = 0;
  28516. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_FILESYSTEM)
  28517. XFILE file;
  28518. #endif
  28519. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28520. byte* der = NULL;
  28521. #else
  28522. byte der[FOURK_BUF];
  28523. #endif
  28524. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28525. if ((myCert == NULL)
  28526. #ifdef WOLFSSL_TEST_CERT
  28527. || (decode == NULL)
  28528. #endif
  28529. || (caEccKey == NULL) || (certPubKey == NULL))
  28530. ERROR_OUT(MEMORY_E, exit);
  28531. #endif
  28532. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  28533. XMEMSET(certPubKey, 0, sizeof *certPubKey);
  28534. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28535. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28536. if (der == NULL) {
  28537. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  28538. }
  28539. #endif
  28540. /* Get cert private key */
  28541. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  28542. /* Get Cert Key 384 */
  28543. #ifdef USE_CERT_BUFFERS_256
  28544. XMEMCPY(der, ca_ecc_key_der_384, sizeof_ca_ecc_key_der_384);
  28545. bytes = sizeof_ca_ecc_key_der_384;
  28546. #elif !defined(NO_FILESYSTEM)
  28547. file = XFOPEN(eccCaKey384File, "rb");
  28548. if (!file) {
  28549. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  28550. }
  28551. bytes = XFREAD(der, 1, FOURK_BUF, file);
  28552. XFCLOSE(file);
  28553. if (bytes == 0)
  28554. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  28555. (void)eccCaKeyFile;
  28556. #else
  28557. WOLFSSL_MSG("No file system and USE_CERT_BUFFERS_256 not defined.(4)");
  28558. ERROR_OUT(ASN_PARSE_E, exit);
  28559. #endif /* USE_CERT_BUFFERS_256 */
  28560. /* end if ENABLE_ECC384_CERT_GEN_TEST */
  28561. #else
  28562. /* !ENABLE_ECC384_CERT_GEN_TEST */
  28563. #ifdef USE_CERT_BUFFERS_256
  28564. XMEMCPY(der, ca_ecc_key_der_256, sizeof_ca_ecc_key_der_256);
  28565. bytes = sizeof_ca_ecc_key_der_256;
  28566. #else
  28567. file = XFOPEN(eccCaKeyFile, "rb");
  28568. if (!file) {
  28569. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  28570. }
  28571. bytes = XFREAD(der, 1, FOURK_BUF, file);
  28572. XFCLOSE(file);
  28573. if (bytes == 0)
  28574. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  28575. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  28576. (void)eccCaKey384File;
  28577. #endif
  28578. #endif /* USE_CERT_BUFFERS_256 */
  28579. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  28580. /* Get CA Key */
  28581. ret = wc_ecc_init_ex(caEccKey, HEAP_HINT, devId);
  28582. if (ret != 0)
  28583. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28584. ret = wc_EccPrivateKeyDecode(der, &idx, caEccKey, (word32)bytes);
  28585. if (ret != 0)
  28586. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28587. /* Make a public key */
  28588. ret = wc_ecc_init_ex(certPubKey, HEAP_HINT, devId);
  28589. if (ret != 0)
  28590. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28591. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, certPubKey);
  28592. #if defined(WOLFSSL_ASYNC_CRYPT)
  28593. ret = wc_AsyncWait(ret, &certPubKey->asyncDev, WC_ASYNC_FLAG_NONE);
  28594. #endif
  28595. if (ret != 0)
  28596. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28597. TEST_SLEEP();
  28598. /* Setup Certificate */
  28599. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  28600. if (ret != 0)
  28601. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28602. #ifndef NO_SHA256
  28603. myCert->sigType = CTC_SHA256wECDSA;
  28604. #else
  28605. myCert->sigType = CTC_SHAwECDSA;
  28606. #endif
  28607. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  28608. #ifdef WOLFSSL_CERT_EXT
  28609. /* add Policies */
  28610. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  28611. CTC_MAX_CERTPOL_SZ);
  28612. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  28613. CTC_MAX_CERTPOL_SZ);
  28614. myCert->certPoliciesNb = 2;
  28615. /* add SKID from the Public Key */
  28616. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, certPubKey);
  28617. if (ret != 0)
  28618. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28619. /* add AKID from the Public Key */
  28620. ret = wc_SetAuthKeyIdFromPublicKey(myCert, NULL, caEccKey);
  28621. if (ret != 0)
  28622. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28623. /* add Key Usage */
  28624. ret = wc_SetKeyUsage(myCert, certKeyUsage);
  28625. if (ret != 0)
  28626. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28627. #endif /* WOLFSSL_CERT_EXT */
  28628. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  28629. #if defined(USE_CERT_BUFFERS_256)
  28630. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_384,
  28631. sizeof_ca_ecc_cert_der_384);
  28632. #elif !defined(NO_FILESYSTEM)
  28633. ret = wc_SetIssuer(myCert, eccCaCert384File);
  28634. (void)eccCaCertFile;
  28635. #else
  28636. /* not testing with embedded, no file system target */
  28637. ERROR_OUT(ASN_PARSE_E, exit);
  28638. #endif /* USE_CERT_BUFFERS_256 */
  28639. #else
  28640. /* not ENABLE_ECC384_CERT_GEN_TEST */
  28641. #if defined(USE_CERT_BUFFERS_256)
  28642. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_256,
  28643. sizeof_ca_ecc_cert_der_256);
  28644. #else
  28645. ret = wc_SetIssuer(myCert, eccCaCertFile);
  28646. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  28647. (void)eccCaCert384File;
  28648. #endif
  28649. #endif
  28650. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  28651. if (ret < 0)
  28652. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28653. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, certPubKey, rng);
  28654. if (certSz < 0) {
  28655. ERROR_OUT(WC_TEST_RET_ENC_EC(certSz), exit);
  28656. }
  28657. ret = 0;
  28658. do {
  28659. #if defined(WOLFSSL_ASYNC_CRYPT)
  28660. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  28661. #endif
  28662. if (ret >= 0) {
  28663. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  28664. FOURK_BUF, NULL, caEccKey, rng);
  28665. }
  28666. } while (ret == WC_PENDING_E);
  28667. if (ret < 0)
  28668. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28669. certSz = (int)ret;
  28670. TEST_SLEEP();
  28671. #ifdef WOLFSSL_TEST_CERT
  28672. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  28673. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  28674. if (ret != 0) {
  28675. FreeDecodedCert(decode);
  28676. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28677. }
  28678. FreeDecodedCert(decode);
  28679. #endif
  28680. ret = SaveDerAndPem(der, certSz, certEccDerFile, certEccPemFile,
  28681. CERT_TYPE);
  28682. if (ret != 0) {
  28683. goto exit;
  28684. }
  28685. exit:
  28686. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28687. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28688. #endif
  28689. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28690. if (myCert != NULL)
  28691. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28692. #ifdef WOLFSSL_TEST_CERT
  28693. if (decode != NULL)
  28694. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28695. #endif
  28696. if (caEccKey != NULL) {
  28697. wc_ecc_free(caEccKey);
  28698. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28699. }
  28700. if (certPubKey != NULL) {
  28701. wc_ecc_free(certPubKey);
  28702. XFREE(certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28703. }
  28704. #else
  28705. wc_ecc_free(certPubKey);
  28706. wc_ecc_free(caEccKey);
  28707. #endif
  28708. return ret;
  28709. }
  28710. #endif /* WOLFSSL_CERT_GEN */
  28711. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  28712. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC) && \
  28713. (!defined(NO_ECC_SECP) || defined(WOLFSSL_CUSTOM_CURVES))
  28714. /* Test for the wc_ecc_key_new() and wc_ecc_key_free() functions. */
  28715. static wc_test_ret_t ecc_test_allocator(WC_RNG* rng)
  28716. {
  28717. wc_test_ret_t ret = 0;
  28718. ecc_key* key;
  28719. #ifdef WC_NO_RNG
  28720. word32 idx = 0;
  28721. #endif
  28722. key = wc_ecc_key_new(HEAP_HINT);
  28723. if (key == NULL) {
  28724. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  28725. }
  28726. #ifndef WC_NO_RNG
  28727. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  28728. #if defined(WOLFSSL_ASYNC_CRYPT)
  28729. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  28730. #endif
  28731. if (ret != 0)
  28732. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28733. #else
  28734. /* use test ECC key */
  28735. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  28736. (word32)sizeof_ecc_key_der_256);
  28737. (void)rng;
  28738. #endif
  28739. exit:
  28740. wc_ecc_key_free(key);
  28741. return ret;
  28742. }
  28743. #endif
  28744. /* ECC Non-blocking tests for Sign and Verify */
  28745. /* Requires SP math and supports P384 or P256 */
  28746. /* ./configure --enable-ecc=nonblock --enable-sp=yes,nonblock CFLAGS="-DWOLFSSL_PUBLIC_MP" */
  28747. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_HAVE_SP_ECC) && \
  28748. defined(WOLFSSL_PUBLIC_MP)
  28749. /* ECC Private Key "d" */
  28750. static const byte p256PrivKey[] = {
  28751. /* SECP256R1 */
  28752. /* d */
  28753. 0x1e, 0xe7, 0x70, 0x07, 0xd3, 0x30, 0x94, 0x39,
  28754. 0x28, 0x90, 0xdf, 0x23, 0x88, 0x2c, 0x4a, 0x34,
  28755. 0x15, 0xdb, 0x4c, 0x43, 0xcd, 0xfa, 0xe5, 0x1f,
  28756. 0x3d, 0x4c, 0x37, 0xfe, 0x59, 0x3b, 0x96, 0xd8
  28757. };
  28758. #ifdef HAVE_ECC384
  28759. static const byte p384PrivKey[] = {
  28760. /* SECP384R1 */
  28761. /* d */
  28762. 0xa4, 0xe5, 0x06, 0xe8, 0x06, 0x16, 0x3e, 0xab,
  28763. 0x89, 0xf8, 0x60, 0x43, 0xc0, 0x60, 0x25, 0xdb,
  28764. 0xba, 0x7b, 0xfe, 0x19, 0x35, 0x08, 0x55, 0x65,
  28765. 0x76, 0xe2, 0xdc, 0xe0, 0x01, 0x8b, 0x6b, 0x68,
  28766. 0xdf, 0xcf, 0x6f, 0x80, 0x12, 0xce, 0x79, 0x37,
  28767. 0xeb, 0x2b, 0x9c, 0x7b, 0xc4, 0x68, 0x1c, 0x74
  28768. };
  28769. #endif /* HAVE_ECC384 */
  28770. #ifdef HAVE_ECC521
  28771. static const byte p521PrivKey[] = {
  28772. /* SECP521R1 */
  28773. /* d */
  28774. 0x01, 0x68, 0x91, 0x33, 0x53, 0xe2, 0x90, 0x68,
  28775. 0x11, 0x8f, 0xaa, 0xa8, 0x76, 0x0c, 0xf7, 0x2a,
  28776. 0x07, 0x1b, 0x92, 0x2a, 0xa7, 0x82, 0x3d, 0xfa,
  28777. 0x83, 0xce, 0x70, 0xc8, 0xc2, 0x60, 0x82, 0xfe,
  28778. 0x18, 0x88, 0x68, 0xda, 0x6a, 0x83, 0x46, 0x78,
  28779. 0xe4, 0xe9, 0xe9, 0xcc, 0x51, 0x7f, 0xed, 0x81,
  28780. 0x02, 0x32, 0xee, 0x26, 0x87, 0xcc, 0xed, 0x63,
  28781. 0x3f, 0x39, 0x27, 0xf0, 0xd7, 0x17, 0x77, 0xa1,
  28782. 0xa4, 0x36
  28783. };
  28784. #endif /* HAVE_ECC521 */
  28785. /* ECC public key Qx/Qy */
  28786. static const byte p256PubKey[] = {
  28787. /* SECP256R1 */
  28788. /* Qx */
  28789. 0x96, 0x93, 0x1c, 0x53, 0x0b, 0x43, 0x6c, 0x42,
  28790. 0x0c, 0x52, 0x90, 0xe4, 0xa7, 0xec, 0x98, 0xb1,
  28791. 0xaf, 0xd4, 0x14, 0x49, 0xd8, 0xc1, 0x42, 0x82,
  28792. 0x04, 0x78, 0xd1, 0x90, 0xae, 0xa0, 0x6c, 0x07,
  28793. /* Qy */
  28794. 0xf2, 0x3a, 0xb5, 0x10, 0x32, 0x8d, 0xce, 0x9e,
  28795. 0x76, 0xa0, 0xd2, 0x8c, 0xf3, 0xfc, 0xa9, 0x94,
  28796. 0x43, 0x24, 0xe6, 0x82, 0x00, 0x40, 0xc6, 0xdb,
  28797. 0x1c, 0x2f, 0xcd, 0x38, 0x4b, 0x60, 0xdd, 0x61
  28798. };
  28799. #ifdef HAVE_ECC384
  28800. static const byte p384PubKey[] = {
  28801. /* SECP384R1 */
  28802. /* Qx */
  28803. 0xea, 0xcf, 0x93, 0x4f, 0x2c, 0x09, 0xbb, 0x39,
  28804. 0x14, 0x0f, 0x56, 0x64, 0xc3, 0x40, 0xb4, 0xdf,
  28805. 0x0e, 0x63, 0xae, 0xe5, 0x71, 0x4b, 0x00, 0xcc,
  28806. 0x04, 0x97, 0xff, 0xe1, 0xe9, 0x38, 0x96, 0xbb,
  28807. 0x5f, 0x91, 0xb2, 0x6a, 0xcc, 0xb5, 0x39, 0x5f,
  28808. 0x8f, 0x70, 0x59, 0xf1, 0x01, 0xf6, 0x5a, 0x2b,
  28809. /* Qy */
  28810. 0x01, 0x6c, 0x68, 0x0b, 0xcf, 0x55, 0x25, 0xaf,
  28811. 0x6d, 0x98, 0x48, 0x0a, 0xa8, 0x74, 0xc9, 0xa9,
  28812. 0x17, 0xa0, 0x0c, 0xc3, 0xfb, 0xd3, 0x23, 0x68,
  28813. 0xfe, 0x04, 0x3c, 0x63, 0x50, 0x88, 0x3b, 0xb9,
  28814. 0x4f, 0x7c, 0x67, 0x34, 0xf7, 0x3b, 0xa9, 0x73,
  28815. 0xe7, 0x1b, 0xc3, 0x51, 0x5e, 0x22, 0x18, 0xec
  28816. };
  28817. #endif
  28818. #ifdef HAVE_ECC521
  28819. static const byte p521PubKey[] = {
  28820. /* SECP521R1 */
  28821. /* Qx */
  28822. 0x01, 0x62, 0x6e, 0xf1, 0x00, 0xec, 0xd8, 0x99,
  28823. 0x58, 0x9b, 0x80, 0x6b, 0xfe, 0x2c, 0xf1, 0xb2,
  28824. 0xf0, 0xc8, 0x48, 0xdf, 0xac, 0xd2, 0x3b, 0x71,
  28825. 0x29, 0xab, 0xf0, 0x66, 0x63, 0xd8, 0x8e, 0xb5,
  28826. 0xc8, 0xc2, 0xfc, 0x99, 0x44, 0xe2, 0x45, 0xb1,
  28827. 0x5a, 0x7b, 0xb9, 0x73, 0x01, 0xda, 0x79, 0xec,
  28828. 0x9c, 0x26, 0x27, 0x34, 0x45, 0x26, 0xd5, 0x89,
  28829. 0x4b, 0x44, 0xfe, 0x69, 0x4e, 0x72, 0x14, 0xe3,
  28830. 0x8b, 0xbc,
  28831. /* Qy */
  28832. 0x00, 0x0f, 0x09, 0xa2, 0x03, 0xc3, 0x5a, 0xdc,
  28833. 0x95, 0x82, 0xf6, 0xf9, 0xf6, 0x9c, 0xff, 0xb5,
  28834. 0x6b, 0x75, 0x95, 0x4b, 0xa4, 0x28, 0x5d, 0x9e,
  28835. 0x90, 0x04, 0xd1, 0xc0, 0x1e, 0xd5, 0xfd, 0x43,
  28836. 0x9e, 0x1e, 0x83, 0xc0, 0x11, 0x2b, 0x2b, 0x07,
  28837. 0x6d, 0xa9, 0x7a, 0x10, 0xd7, 0x67, 0xe7, 0x51,
  28838. 0x37, 0x24, 0xd8, 0xbf, 0x03, 0x0d, 0x8b, 0xb5,
  28839. 0x40, 0x5c, 0x4f, 0xd6, 0x13, 0x73, 0x42, 0xbc,
  28840. 0x91, 0xd9
  28841. };
  28842. /* perform verify of signature and hash using public key */
  28843. /* key is public Qx + public Qy */
  28844. /* sig is r + s */
  28845. static wc_test_ret_t crypto_ecc_verify(const byte *key, uint32_t keySz,
  28846. const byte *hash, uint32_t hashSz, const byte *sig, uint32_t sigSz,
  28847. uint32_t curveSz, int curveId)
  28848. {
  28849. wc_test_ret_t ret;
  28850. int verify_res = 0, count = 0;
  28851. mp_int r, s;
  28852. ecc_key ecc;
  28853. ecc_nb_ctx_t nb_ctx;
  28854. /* validate arguments */
  28855. if (key == NULL || hash == NULL || sig == NULL || curveSz == 0 ||
  28856. hashSz == 0 || keySz < (curveSz*2) || sigSz < (curveSz*2))
  28857. {
  28858. return WC_TEST_RET_ENC_NC;
  28859. }
  28860. /* Setup the ECC key */
  28861. ret = wc_ecc_init(&ecc);
  28862. if (ret < 0) {
  28863. return WC_TEST_RET_ENC_EC(ret);
  28864. }
  28865. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  28866. if (ret != MP_OKAY) {
  28867. wc_ecc_free(&ecc);
  28868. return WC_TEST_RET_ENC_EC(ret);
  28869. }
  28870. /* Setup the signature r/s variables */
  28871. ret = mp_init(&r);
  28872. if (ret != MP_OKAY) {
  28873. wc_ecc_free(&ecc);
  28874. return WC_TEST_RET_ENC_EC(ret);
  28875. }
  28876. ret = mp_init(&s);
  28877. if (ret != MP_OKAY) {
  28878. mp_clear(&r);
  28879. wc_ecc_free(&ecc);
  28880. return WC_TEST_RET_ENC_EC(ret);
  28881. }
  28882. /* Import public key x/y */
  28883. ret = wc_ecc_import_unsigned(
  28884. &ecc,
  28885. (byte*)key, /* Public "x" Coordinate */
  28886. (byte*)(key + curveSz), /* Public "y" Coordinate */
  28887. NULL, /* Private "d" (optional) */
  28888. curveId /* ECC Curve Id */
  28889. );
  28890. /* Make sure it was a public key imported */
  28891. if (ret == 0 && ecc.type != ECC_PUBLICKEY) {
  28892. ret = WC_TEST_RET_ENC_NC; /* ECC_BAD_ARG_E */
  28893. }
  28894. /* Import signature r/s */
  28895. if (ret == 0) {
  28896. ret = mp_read_unsigned_bin(&r, sig, curveSz);
  28897. if (ret < 0)
  28898. ret = WC_TEST_RET_ENC_EC(ret);
  28899. }
  28900. if (ret == 0) {
  28901. ret = mp_read_unsigned_bin(&s, sig + curveSz, curveSz);
  28902. if (ret < 0)
  28903. ret = WC_TEST_RET_ENC_EC(ret);
  28904. }
  28905. /* Verify ECC Signature */
  28906. if (ret == 0) {
  28907. do {
  28908. ret = wc_ecc_verify_hash_ex(
  28909. &r, &s, /* r/s as mp_int */
  28910. hash, hashSz, /* computed hash digest */
  28911. &verify_res, /* verification result 1=success */
  28912. &ecc
  28913. );
  28914. count++;
  28915. /* This is where real-time work could be called */
  28916. } while (ret == FP_WOULDBLOCK);
  28917. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  28918. printf("ECC non-block verify: %d times\n", count);
  28919. #endif
  28920. if (ret < 0)
  28921. ret = WC_TEST_RET_ENC_EC(ret);
  28922. }
  28923. /* check verify result */
  28924. if (ret == 0 && verify_res == 0) {
  28925. ret = WC_TEST_RET_ENC_NC /* SIG_VERIFY_E */;
  28926. }
  28927. mp_clear(&r);
  28928. mp_clear(&s);
  28929. wc_ecc_free(&ecc);
  28930. (void)count;
  28931. return ret;
  28932. }
  28933. /* perform signature operation against hash using private key */
  28934. static wc_test_ret_t crypto_ecc_sign(const byte *key, uint32_t keySz,
  28935. const byte *hash, uint32_t hashSz, byte *sig, uint32_t* sigSz,
  28936. uint32_t curveSz, int curveId, WC_RNG* rng)
  28937. {
  28938. wc_test_ret_t ret;
  28939. int count = 0;
  28940. mp_int r, s;
  28941. ecc_key ecc;
  28942. ecc_nb_ctx_t nb_ctx;
  28943. /* validate arguments */
  28944. if (key == NULL || hash == NULL || sig == NULL || sigSz == NULL ||
  28945. curveSz == 0 || hashSz == 0 || keySz < curveSz || *sigSz < (curveSz*2))
  28946. {
  28947. return WC_TEST_RET_ENC_NC /* BAD_FUNC_ARG */;
  28948. }
  28949. /* Initialize signature result */
  28950. memset(sig, 0, curveSz*2);
  28951. /* Setup the ECC key */
  28952. ret = wc_ecc_init(&ecc);
  28953. if (ret < 0) {
  28954. return WC_TEST_RET_ENC_EC(ret);
  28955. }
  28956. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  28957. if (ret != MP_OKAY) {
  28958. wc_ecc_free(&ecc);
  28959. return WC_TEST_RET_ENC_EC(ret);
  28960. }
  28961. /* Setup the signature r/s variables */
  28962. ret = mp_init(&r);
  28963. if (ret != MP_OKAY) {
  28964. wc_ecc_free(&ecc);
  28965. return WC_TEST_RET_ENC_EC(ret);
  28966. }
  28967. ret = mp_init(&s);
  28968. if (ret != MP_OKAY) {
  28969. mp_clear(&r);
  28970. wc_ecc_free(&ecc);
  28971. return WC_TEST_RET_ENC_EC(ret);
  28972. }
  28973. /* Import private key "k" */
  28974. ret = wc_ecc_import_private_key_ex(
  28975. key, keySz, /* private key "d" */
  28976. NULL, 0, /* public (optional) */
  28977. &ecc,
  28978. curveId /* ECC Curve Id */
  28979. );
  28980. if (ret < 0)
  28981. ret = WC_TEST_RET_ENC_EC(ret);
  28982. if (ret == 0) {
  28983. do {
  28984. /* Verify ECC Signature */
  28985. ret = wc_ecc_sign_hash_ex(
  28986. hash, hashSz, /* computed hash digest */
  28987. rng, &ecc, /* random and key context */
  28988. &r, &s /* r/s as mp_int */
  28989. );
  28990. count++;
  28991. /* This is where real-time work could be called */
  28992. } while (ret == FP_WOULDBLOCK);
  28993. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  28994. printf("ECC non-block sign: %d times\n", count);
  28995. #endif
  28996. if (ret < 0)
  28997. ret = WC_TEST_RET_ENC_EC(ret);
  28998. }
  28999. if (ret == 0) {
  29000. /* export r/s */
  29001. mp_to_unsigned_bin_len(&r, sig, curveSz);
  29002. mp_to_unsigned_bin_len(&s, sig + curveSz, curveSz);
  29003. }
  29004. mp_clear(&r);
  29005. mp_clear(&s);
  29006. wc_ecc_free(&ecc);
  29007. (void)count;
  29008. return ret;
  29009. }
  29010. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  29011. /*
  29012. * This test doesn't work with WOLFSSL_VALIDATE_ECC_KEYGEN defined because we
  29013. * don't have non-blocking versions of the key checking functions, yet.
  29014. */
  29015. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  29016. static wc_test_ret_t ecc_test_nonblock_dhe(int curveId, word32 curveSz,
  29017. const byte* privKey, const byte* pubKey, WC_RNG* rng)
  29018. {
  29019. wc_test_ret_t ret;
  29020. ecc_key keyA;
  29021. ecc_key keyB;
  29022. ecc_nb_ctx_t nbCtxA;
  29023. ecc_nb_ctx_t nbCtxB;
  29024. byte secretA[ECC_SHARED_SIZE];
  29025. byte secretB[ECC_SHARED_SIZE];
  29026. word32 secretSzA = ECC_SHARED_SIZE;
  29027. word32 secretSzB = ECC_SHARED_SIZE;
  29028. int count = 0;
  29029. ret = wc_ecc_init(&keyA);
  29030. if (ret == 0) {
  29031. ret = wc_ecc_init(&keyB);
  29032. if (ret < 0)
  29033. ret = WC_TEST_RET_ENC_EC(ret);
  29034. }
  29035. if (ret == 0) {
  29036. ret = wc_ecc_set_nonblock(&keyA, &nbCtxA);
  29037. if (ret < 0)
  29038. ret = WC_TEST_RET_ENC_EC(ret);
  29039. }
  29040. if (ret == 0) {
  29041. ret = wc_ecc_set_nonblock(&keyB, &nbCtxB);
  29042. if (ret < 0)
  29043. ret = WC_TEST_RET_ENC_EC(ret);
  29044. }
  29045. if (ret == 0) {
  29046. do {
  29047. ret = wc_ecc_make_key_ex(rng, curveSz, &keyA, curveId);
  29048. count++;
  29049. } while (ret == FP_WOULDBLOCK);
  29050. if (ret < 0)
  29051. ret = WC_TEST_RET_ENC_EC(ret);
  29052. }
  29053. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  29054. fprintf(stderr, "ECC non-block key gen: %d times\n", count);
  29055. #endif
  29056. if (ret == 0) {
  29057. ret = wc_ecc_check_key(&keyA);
  29058. if (ret < 0)
  29059. ret = WC_TEST_RET_ENC_EC(ret);
  29060. }
  29061. if (ret == 0) {
  29062. ret = wc_ecc_import_unsigned(&keyB, pubKey, pubKey + curveSz,
  29063. privKey, curveId);
  29064. if (ret < 0)
  29065. ret = WC_TEST_RET_ENC_EC(ret);
  29066. }
  29067. count = 0;
  29068. if (ret == 0) {
  29069. do {
  29070. ret = wc_ecc_shared_secret(&keyA, &keyB, secretA, &secretSzA);
  29071. count++;
  29072. } while (ret == FP_WOULDBLOCK);
  29073. if (ret < 0)
  29074. ret = WC_TEST_RET_ENC_EC(ret);
  29075. }
  29076. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  29077. fprintf(stderr, "ECC non-block shared secret: %d times\n", count);
  29078. #endif
  29079. if (ret == 0) {
  29080. do {
  29081. ret = wc_ecc_shared_secret(&keyB, &keyA, secretB, &secretSzB);
  29082. } while (ret == FP_WOULDBLOCK);
  29083. if (ret < 0)
  29084. ret = WC_TEST_RET_ENC_EC(ret);
  29085. }
  29086. if (ret == 0) {
  29087. if (secretSzA != secretSzB ||
  29088. XMEMCMP(secretA, secretB, secretSzA) != 0) {
  29089. ret = WC_TEST_RET_ENC_NC;
  29090. }
  29091. }
  29092. wc_ecc_free(&keyA);
  29093. wc_ecc_free(&keyB);
  29094. return ret;
  29095. }
  29096. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  29097. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  29098. static wc_test_ret_t ecc_test_nonblock_ecdsa(int curveId, word32 curveSz,
  29099. const byte* privKey, word32 privKeySz, const byte* pubKey, word32 pubKeySz,
  29100. WC_RNG* rng)
  29101. {
  29102. wc_test_ret_t ret = 0;
  29103. byte* sig = NULL;
  29104. word32 sigSz = curveSz * 2;
  29105. static const byte hash[] = {
  29106. 0x8d, 0x28, 0xa3, 0x8b, 0x0b, 0xa9, 0xfe, 0xd4, 0x0e, 0x54, 0xc4, 0x17,
  29107. 0x3d, 0x54, 0x66, 0x34, 0xbf, 0x5d, 0x6f, 0x46, 0xc2, 0x20, 0xcb, 0xc3,
  29108. 0x22, 0xe9, 0xb0, 0xdf, 0xe7, 0x64, 0x3f, 0xd9
  29109. };
  29110. sig = (byte*)XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  29111. if (sig == NULL) {
  29112. ret = WC_TEST_RET_ENC_ERRNO;
  29113. }
  29114. if (ret == 0) {
  29115. /* Sign hash using private key */
  29116. /* Note: result of an ECC sign varies for each call even with same
  29117. private key and hash. This is because a new random public key is
  29118. used for each operation. */
  29119. ret = crypto_ecc_sign(privKey, privKeySz, hash, sizeof(hash), sig,
  29120. &sigSz, curveSz, curveId, rng);
  29121. }
  29122. if (ret == 0) {
  29123. /* Verify generated signature is valid */
  29124. ret = crypto_ecc_verify(pubKey, pubKeySz, hash, sizeof(hash), sig,
  29125. sigSz, curveSz, curveId);
  29126. }
  29127. if (sig != NULL) {
  29128. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  29129. }
  29130. return ret;
  29131. }
  29132. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  29133. static wc_test_ret_t ecc_test_nonblock(WC_RNG* rng)
  29134. {
  29135. wc_test_ret_t ret = 0;
  29136. word32 i;
  29137. int curveIds[3] = {0, 0, 0};
  29138. word32 curveSzs[3] = {0, 0, 0};
  29139. const byte* privKeys[3] = {NULL, NULL, NULL};
  29140. word32 privKeySzs[3] = {0, 0, 0};
  29141. const byte* pubKeys[3] = {NULL, NULL, NULL};
  29142. word32 pubKeySzs[3] = {0, 0, 0};
  29143. curveIds[0] = ECC_SECP256R1;
  29144. curveSzs[0] = 32;
  29145. privKeys[0] = p256PrivKey;
  29146. privKeySzs[0] = sizeof(p256PrivKey);
  29147. pubKeys[0] = p256PubKey;
  29148. pubKeySzs[0] = sizeof(p256PubKey);
  29149. #ifdef HAVE_ECC384
  29150. curveIds[1] = ECC_SECP384R1;
  29151. curveSzs[1] = 48;
  29152. privKeys[1] = p384PrivKey;
  29153. privKeySzs[1] = sizeof(p384PrivKey);
  29154. pubKeys[1] = p384PubKey;
  29155. pubKeySzs[1] = sizeof(p384PubKey);
  29156. #endif
  29157. #ifdef HAVE_ECC521
  29158. curveIds[2] = ECC_SECP521R1;
  29159. curveSzs[2] = 66;
  29160. privKeys[2] = p521PrivKey;
  29161. privKeySzs[2] = sizeof(p521PrivKey);
  29162. pubKeys[2] = p521PubKey;
  29163. pubKeySzs[2] = sizeof(p521PubKey);
  29164. #endif
  29165. for (i = 0; ret == 0 && i < sizeof(curveIds) / sizeof(curveIds[0]); ++i) {
  29166. if (curveIds[i] == 0) {
  29167. continue;
  29168. }
  29169. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  29170. ret = ecc_test_nonblock_ecdsa(curveIds[i], curveSzs[i], privKeys[i],
  29171. privKeySzs[i], pubKeys[i], pubKeySzs[i], rng);
  29172. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  29173. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  29174. if (ret == 0) {
  29175. ret = ecc_test_nonblock_dhe(curveIds[i], curveSzs[i], privKeys[i],
  29176. pubKeys[i], rng);
  29177. }
  29178. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  29179. }
  29180. return ret;
  29181. }
  29182. #endif /* WC_ECC_NONBLOCK && WOLFSSL_HAVE_SP_ECC && WOLFSSL_PUBLIC_MP */
  29183. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST) && \
  29184. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  29185. (HAVE_FIPS_VERSION > 2)))
  29186. static int ecc_test_raw_enc_dec(void)
  29187. {
  29188. int ret;
  29189. unsigned char r[1];
  29190. word32 rSz;
  29191. unsigned char s[1];
  29192. word32 sSz;
  29193. unsigned char rZero[] = { 0, 0, 0, 0 };
  29194. unsigned char sOne[] = { 0, 0, 1 };
  29195. unsigned char sigRaw[32];
  29196. word32 sigRawSz;
  29197. unsigned char expSig[] = { 0x30, 0x06, 0x02, 0x01, 0x00, 0x02, 0x01, 0x01 };
  29198. sigRawSz = sizeof(sigRaw);
  29199. ret = wc_ecc_rs_raw_to_sig(rZero, sizeof(rZero), sOne, sizeof(sOne),
  29200. sigRaw, &sigRawSz);
  29201. if (ret != 0) {
  29202. return WC_TEST_RET_ENC_EC(ret);
  29203. }
  29204. if (sigRawSz != sizeof(expSig)) {
  29205. return WC_TEST_RET_ENC_EC((int)sigRawSz);
  29206. }
  29207. if (XMEMCMP(sigRaw, expSig, sizeof(expSig)) != 0) {
  29208. return WC_TEST_RET_ENC_NC;
  29209. }
  29210. rSz = sizeof(r);
  29211. sSz = sizeof(s);
  29212. ret = wc_ecc_sig_to_rs(sigRaw, sigRawSz, r, &rSz, s, &sSz);
  29213. if (ret != 0) {
  29214. return WC_TEST_RET_ENC_EC(ret);
  29215. }
  29216. if (rSz != 1) {
  29217. return WC_TEST_RET_ENC_EC((int)rSz);
  29218. }
  29219. if (sSz != 1) {
  29220. return WC_TEST_RET_ENC_EC((int)sSz);
  29221. }
  29222. if (r[0] != 0) {
  29223. return WC_TEST_RET_ENC_EC(r[0]);
  29224. }
  29225. if (s[0] != 1) {
  29226. return WC_TEST_RET_ENC_EC(s[0]);
  29227. }
  29228. return ret;
  29229. }
  29230. #endif
  29231. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test(void)
  29232. {
  29233. wc_test_ret_t ret;
  29234. WC_RNG rng;
  29235. WOLFSSL_ENTER("ecc_test");
  29236. #if defined(ECC_MIN_KEY_SZ)
  29237. WOLFSSL_MSG_EX("ecc_test ECC_MIN_KEY_SZ = %d\n", ECC_MIN_KEY_SZ);
  29238. #else
  29239. WOLFSSL_MSG("ecc_test ECC_MIN_KEY_SZ not defined.");
  29240. #endif
  29241. #if defined(WOLFSSL_CERT_EXT) && \
  29242. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  29243. ret = ecc_decode_test();
  29244. if (ret < 0)
  29245. return ret;
  29246. #endif
  29247. #ifndef HAVE_FIPS
  29248. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  29249. #else
  29250. ret = wc_InitRng(&rng);
  29251. #endif
  29252. #ifndef WC_NO_RNG
  29253. if (ret != 0)
  29254. return WC_TEST_RET_ENC_EC(ret);
  29255. #else
  29256. (void)ret;
  29257. #endif
  29258. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  29259. ret = ecc_test_curve(&rng, 14, ECC_CURVE_DEF);
  29260. if (ret < 0) {
  29261. printf("keySize=14, Default\n");
  29262. goto done;
  29263. }
  29264. #endif /* HAVE_ECC112 */
  29265. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  29266. ret = ecc_test_curve(&rng, 16, ECC_CURVE_DEF);
  29267. if (ret < 0) {
  29268. printf("keySize=16, Default\n");
  29269. goto done;
  29270. }
  29271. #endif /* HAVE_ECC128 */
  29272. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  29273. ret = ecc_test_curve(&rng, 20, ECC_CURVE_DEF);
  29274. if (ret < 0) {
  29275. printf("keySize=20, Default\n");
  29276. goto done;
  29277. }
  29278. #endif /* HAVE_ECC160 */
  29279. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  29280. #if !FIPS_VERSION3_GE(6,0,0)
  29281. ret = ecc_test_curve(&rng, 24, ECC_CURVE_DEF);
  29282. if (ret < 0) {
  29283. printf("keySize=24, Default\n");
  29284. goto done;
  29285. }
  29286. #endif
  29287. #endif /* HAVE_ECC192 */
  29288. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  29289. ret = ecc_test_curve(&rng, 28, ECC_CURVE_DEF);
  29290. if (ret < 0) {
  29291. printf("keySize=28, Default\n");
  29292. goto done;
  29293. }
  29294. #endif /* HAVE_ECC224 */
  29295. #if (defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 239
  29296. ret = ecc_test_curve(&rng, 30, ECC_CURVE_DEF);
  29297. if (ret < 0) {
  29298. printf("keySize=30, Default\n");
  29299. goto done;
  29300. }
  29301. #endif /* HAVE_ECC239 */
  29302. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  29303. ret = ecc_test_curve(&rng, 32, ECC_CURVE_DEF);
  29304. if (ret < 0) {
  29305. printf("keySize=32, Default\n");
  29306. goto done;
  29307. }
  29308. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  29309. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  29310. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  29311. ret = ecc_point_test();
  29312. if (ret < 0) {
  29313. goto done;
  29314. }
  29315. #endif
  29316. #if !defined(NO_ECC_SECP) || defined(WOLFSSL_CUSTOM_CURVES)
  29317. ret = ecc_def_curve_test(&rng);
  29318. if (ret < 0) {
  29319. printf("Default\n");
  29320. goto done;
  29321. }
  29322. #endif
  29323. #endif /* !NO_ECC256 */
  29324. #if (defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 320
  29325. ret = ecc_test_curve(&rng, 40, ECC_CURVE_DEF);
  29326. if (ret < 0) {
  29327. printf("keySize=40, Default\n");
  29328. goto done;
  29329. }
  29330. #endif /* HAVE_ECC320 */
  29331. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  29332. ret = ecc_test_curve(&rng, 48, ECC_CURVE_DEF);
  29333. if (ret < 0) {
  29334. printf("keySize=48, Default\n");
  29335. goto done;
  29336. }
  29337. #endif /* HAVE_ECC384 */
  29338. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  29339. ret = ecc_test_curve(&rng, 64, ECC_CURVE_DEF);
  29340. if (ret < 0) {
  29341. printf("keySize=64, Default\n");
  29342. goto done;
  29343. }
  29344. #endif /* HAVE_ECC512 */
  29345. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  29346. ret = ecc_test_curve(&rng, 66, ECC_CURVE_DEF);
  29347. if (ret < 0) {
  29348. printf("keySize=66, Default\n");
  29349. goto done;
  29350. }
  29351. #endif /* HAVE_ECC521 */
  29352. #ifdef WOLFSSL_SM2
  29353. ret = ecc_test_curve(&rng, 32, ECC_SM2P256V1);
  29354. if (ret < 0) {
  29355. printf("SM2\n");
  29356. goto done;
  29357. }
  29358. #endif
  29359. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST) && \
  29360. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  29361. (HAVE_FIPS_VERSION > 2)))
  29362. ret = ecc_test_raw_enc_dec();
  29363. if (ret != 0) {
  29364. printf("raw sig encode/decode\n");
  29365. goto done;
  29366. }
  29367. #endif
  29368. #if defined(WOLFSSL_CUSTOM_CURVES)
  29369. ret = ecc_test_custom_curves(&rng);
  29370. if (ret != 0) {
  29371. printf("Custom\n");
  29372. goto done;
  29373. }
  29374. #endif
  29375. #if defined(WOLFSSL_SM2)
  29376. ret = test_sm2_verify();
  29377. if (ret != 0) {
  29378. printf("SM2 Verify\n");
  29379. goto done;
  29380. }
  29381. ret = ecc_sm2_test_curve(&rng, ECC_TEST_VERIFY_COUNT);
  29382. if (ret != 0) {
  29383. printf("SM2 test\n");
  29384. goto done;
  29385. }
  29386. #endif
  29387. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  29388. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  29389. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  29390. #ifdef HAVE_ECC256
  29391. ret = ecc_test_deterministic_k(&rng);
  29392. if (ret != 0) {
  29393. printf("ecc_test_deterministic_k failed!\n");
  29394. goto done;
  29395. }
  29396. #endif
  29397. #ifdef WOLFSSL_PUBLIC_MP
  29398. #if defined(HAVE_ECC384)
  29399. ret = ecc384_test_deterministic_k(&rng);
  29400. if (ret != 0) {
  29401. printf("ecc384_test_deterministic_k failed!\n");
  29402. goto done;
  29403. }
  29404. #endif
  29405. #if defined(HAVE_ECC521)
  29406. ret = ecc521_test_deterministic_k(&rng);
  29407. if (ret != 0) {
  29408. printf("ecc512_test_deterministic_k failed!\n");
  29409. goto done;
  29410. }
  29411. #endif
  29412. #endif
  29413. #endif
  29414. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  29415. !defined(WOLFSSL_KCAPI_ECC)
  29416. ret = ecc_test_sign_vectors(&rng);
  29417. if (ret != 0) {
  29418. printf("ecc_test_sign_vectors failed!\n");
  29419. goto done;
  29420. }
  29421. #endif
  29422. #if defined(HAVE_ECC_VECTOR_TEST) && defined(HAVE_ECC_CDH) && \
  29423. defined(HAVE_ECC_DHE)
  29424. ret = ecc_test_cdh_vectors(&rng);
  29425. if (ret != 0) {
  29426. printf("ecc_test_cdh_vectors failed!\n");
  29427. goto done;
  29428. }
  29429. #endif
  29430. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  29431. !defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
  29432. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  29433. ret = ecc_test_make_pub(&rng);
  29434. if (ret != 0) {
  29435. printf("ecc_test_make_pub failed!\n");
  29436. goto done;
  29437. }
  29438. #elif defined(HAVE_ECC_KEY_IMPORT)
  29439. (void)ecc_test_make_pub; /* for compiler warning */
  29440. #endif
  29441. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ECC_SECP) && !defined(NO_ASN_TIME)
  29442. ret = ecc_test_cert_gen(&rng);
  29443. if (ret != 0) {
  29444. printf("ecc_test_cert_gen failed!\n");
  29445. goto done;
  29446. }
  29447. #endif
  29448. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC) && \
  29449. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && (!defined(NO_ECC_SECP) || \
  29450. defined(WOLFSSL_CUSTOM_CURVES))
  29451. ret = ecc_test_allocator(&rng);
  29452. if (ret != 0) {
  29453. printf("ecc_test_allocator failed!\n");
  29454. goto done;
  29455. }
  29456. #endif
  29457. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  29458. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  29459. ret = ecc_test_nonblock(&rng);
  29460. if (ret != 0) {
  29461. printf("ecc_test_nonblock failed!\n");
  29462. goto done;
  29463. }
  29464. #endif
  29465. done:
  29466. wc_FreeRng(&rng);
  29467. return ret;
  29468. }
  29469. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  29470. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  29471. #if ((! defined(HAVE_FIPS)) || FIPS_VERSION_GE(5,3))
  29472. static wc_test_ret_t ecc_ctx_kdf_salt_test(WC_RNG* rng, ecc_key* a, ecc_key* b)
  29473. {
  29474. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29475. byte* plaintext;
  29476. byte* encrypted;
  29477. byte* decrypted;
  29478. #else
  29479. byte plaintext[128];
  29480. byte encrypted[128];
  29481. byte decrypted[128];
  29482. #endif
  29483. ecEncCtx* aCtx = NULL;
  29484. ecEncCtx* bCtx = NULL;
  29485. static const byte salt[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  29486. 14, 15};
  29487. wc_test_ret_t ret = 0;
  29488. static const char message[] = "Hello wolfSSL!";
  29489. word32 plaintextLen;
  29490. word32 encryptLen = 128;
  29491. word32 decryptLen = 128;
  29492. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29493. plaintext = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29494. encrypted = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29495. decrypted = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29496. #endif
  29497. wc_ecc_free(a);
  29498. wc_ecc_free(b);
  29499. ret = wc_ecc_init(a);
  29500. if (ret != 0)
  29501. ret = WC_TEST_RET_ENC_EC(ret);
  29502. if (ret == 0) {
  29503. ret = wc_ecc_init(b);
  29504. if (ret != 0)
  29505. ret = WC_TEST_RET_ENC_EC(ret);
  29506. }
  29507. if (ret == 0)
  29508. ret = wc_ecc_make_key(rng, 32, a);
  29509. if (ret == 0)
  29510. ret = wc_ecc_make_key(rng, 32, b);
  29511. /* create context */
  29512. if (ret == 0) {
  29513. aCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  29514. if (aCtx == NULL)
  29515. ret = WC_TEST_RET_ENC_NC;
  29516. }
  29517. if (ret == 0) {
  29518. bCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  29519. if (bCtx == NULL)
  29520. ret = WC_TEST_RET_ENC_NC;
  29521. }
  29522. /* set salt */
  29523. if (ret == 0) {
  29524. ret = wc_ecc_ctx_set_kdf_salt(aCtx, salt, sizeof(salt));
  29525. if (ret != 0)
  29526. ret = 10472;
  29527. }
  29528. if (ret == 0) {
  29529. ret = wc_ecc_ctx_set_kdf_salt(bCtx, salt, sizeof(salt));
  29530. if (ret != 0)
  29531. ret = 10473;
  29532. }
  29533. XMEMSET(plaintext, 0, 128);
  29534. XSTRLCPY((char *)plaintext, message, sizeof plaintext);
  29535. plaintextLen = (((word32)XSTRLEN(message) + AES_BLOCK_SIZE - 1) /
  29536. AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
  29537. /* encrypt */
  29538. if (ret == 0) {
  29539. ret = wc_ecc_encrypt(a, b, plaintext, plaintextLen, encrypted,
  29540. &encryptLen, aCtx);
  29541. if (ret != 0)
  29542. ret = WC_TEST_RET_ENC_EC(ret);
  29543. }
  29544. /* decrypt */
  29545. if (ret == 0) {
  29546. ret = wc_ecc_decrypt(b, a, encrypted, encryptLen, decrypted,
  29547. &decryptLen, bCtx);
  29548. if (ret != 0)
  29549. ret = WC_TEST_RET_ENC_EC(ret);
  29550. }
  29551. /* compare */
  29552. if (ret == 0 && XMEMCMP(decrypted, plaintext, plaintextLen) != 0)
  29553. ret = WC_TEST_RET_ENC_NC;
  29554. wc_ecc_free(a);
  29555. wc_ecc_free(b);
  29556. wc_ecc_ctx_free(aCtx);
  29557. wc_ecc_ctx_free(bCtx);
  29558. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29559. XFREE(plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29560. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29561. XFREE(decrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29562. #endif
  29563. return ret;
  29564. }
  29565. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  29566. /* ecc_encrypt_e2e_test() uses wc_ecc_ctx_set_algo(), which was added in
  29567. * wolfFIPS 5.3.
  29568. * ecc_encrypt_kat() is used only by ecc_encrypt_e2e_test().
  29569. */
  29570. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  29571. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  29572. ECC_MIN_KEY_SZ <= 256 && defined(WOLFSSL_AES_128)
  29573. static wc_test_ret_t ecc_encrypt_kat(WC_RNG *rng)
  29574. {
  29575. wc_test_ret_t ret = 0;
  29576. #ifdef WOLFSSL_ECIES_OLD
  29577. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29578. ecc_key* userA = NULL;
  29579. #else
  29580. ecc_key userA[1];
  29581. #endif
  29582. int userAInit = 0;
  29583. #endif
  29584. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29585. ecc_key* userB = NULL;
  29586. #else
  29587. ecc_key userB[1];
  29588. #endif
  29589. int userBInit = 0;
  29590. ecc_key* tmpKey;
  29591. byte plain[48];
  29592. word32 plainSz = sizeof(plain);
  29593. WOLFSSL_SMALL_STACK_STATIC const byte privKey[] = {
  29594. 0x04, 0x80, 0xef, 0x1d, 0xbe, 0x02, 0x0c, 0x20,
  29595. 0x5b, 0xab, 0x80, 0x35, 0x5b, 0x2a, 0x0f, 0x6d,
  29596. 0xd3, 0xb0, 0x7f, 0x7e, 0x7f, 0x86, 0x8a, 0x49,
  29597. 0xee, 0xb4, 0xaa, 0x09, 0x2d, 0x1e, 0x1d, 0x02
  29598. };
  29599. #if defined(WOLFSSL_ECIES_OLD) || defined(WOLFSSL_QNX_CAAM)
  29600. WOLFSSL_SMALL_STACK_STATIC const byte pubKey[] = {
  29601. 0x04,
  29602. /* X */
  29603. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  29604. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  29605. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  29606. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  29607. /* X */
  29608. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  29609. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  29610. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  29611. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28
  29612. };
  29613. #endif
  29614. WOLFSSL_SMALL_STACK_STATIC const byte enc_msg[] = {
  29615. #ifdef WOLFSSL_ECIES_OLD
  29616. 0x42, 0x70, 0xbf, 0xf9, 0xf4, 0x7e, 0x4b, 0x9b,
  29617. 0xb5, 0x4c, 0xcc, 0xc5, 0x94, 0xa7, 0xef, 0xaa,
  29618. 0xc3, 0x7c, 0x85, 0xa6, 0x51, 0x6e, 0xd3, 0xfa,
  29619. 0x56, 0xc9, 0x10, 0x4d, 0x14, 0x32, 0x61, 0xb8,
  29620. 0xbb, 0x66, 0x7a, 0xb5, 0xbc, 0x95, 0xf8, 0xca,
  29621. 0xd1, 0x2a, 0x19, 0x51, 0x44, 0xd8, 0x0e, 0x57,
  29622. 0x34, 0xed, 0x45, 0x89, 0x2e, 0x57, 0xbe, 0xd5,
  29623. 0x06, 0x22, 0xd7, 0x13, 0x0a, 0x0e, 0x40, 0x36,
  29624. 0x0d, 0x05, 0x0d, 0xb6, 0xae, 0x61, 0x37, 0x18,
  29625. 0x83, 0x90, 0x0a, 0x27, 0x95, 0x41, 0x8c, 0x45
  29626. #elif defined(WOLFSSL_ECIES_ISO18033)
  29627. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  29628. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  29629. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  29630. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  29631. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  29632. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  29633. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  29634. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  29635. 0x28, 0xbb, 0x9f, 0xa8, 0x2d, 0xe1, 0xf1, 0x67,
  29636. 0x45, 0x02, 0x19, 0xdc, 0xc8, 0x24, 0x8b, 0x20,
  29637. 0x02, 0xa0, 0x8f, 0x95, 0x12, 0x55, 0x51, 0xf8,
  29638. 0x03, 0xc4, 0x54, 0x13, 0x98, 0x2d, 0xf0, 0x31,
  29639. 0x51, 0x80, 0x45, 0x24, 0xcb, 0x8b, 0x48, 0xa6,
  29640. 0x8b, 0x8e, 0x97, 0x9c, 0x56, 0x4d, 0x70, 0x00,
  29641. 0x53, 0xd3, 0x47, 0x00, 0x5a, 0x23, 0x8c, 0xf9,
  29642. 0xfd, 0xd2, 0x33, 0x2c, 0x43, 0x6e, 0x9e, 0xb2,
  29643. 0xf4, 0x95, 0xd4, 0xcf, 0x30, 0xd6, 0xa2, 0xc5,
  29644. 0x35, 0x96, 0x6a, 0xd4, 0x36, 0x15, 0xa9, 0xbd,
  29645. 0x7f
  29646. #elif defined(WOLFSSL_ECIES_GEN_IV)
  29647. /* EC P-256 point */
  29648. 0x04,
  29649. /* X */
  29650. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  29651. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  29652. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  29653. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  29654. /* Y */
  29655. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  29656. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  29657. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  29658. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28,
  29659. /* IV */
  29660. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29661. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29662. /* Encrypted Msg */
  29663. 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d, 0x50,
  29664. 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c, 0x1f,
  29665. 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc, 0x44,
  29666. 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca, 0x65,
  29667. 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61, 0xbd,
  29668. 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1, 0xde,
  29669. /* HMAC */
  29670. 0x5a, 0x22, 0xc1, 0x5d, 0x99, 0x66, 0x3f, 0x24,
  29671. 0x35, 0x96, 0xac, 0xf7, 0xf6, 0x28, 0x45, 0x16,
  29672. 0x52, 0x19, 0x0d, 0xe4, 0xb2, 0xca, 0x5b, 0x28,
  29673. 0x4e, 0xbb, 0xf3, 0x98, 0x57, 0xd7, 0x3b, 0xe2
  29674. #else
  29675. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  29676. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  29677. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  29678. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  29679. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  29680. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  29681. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  29682. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  29683. 0x28, 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d,
  29684. 0x50, 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c,
  29685. 0x1f, 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc,
  29686. 0x44, 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca,
  29687. 0x65, 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61,
  29688. 0xbd, 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1,
  29689. 0xde, 0xc7, 0x3f, 0x6f, 0xce, 0xbe, 0x49, 0x61,
  29690. 0x48, 0x01, 0x77, 0x41, 0xd0, 0xd8, 0x5b, 0x48,
  29691. 0xca, 0x4e, 0x47, 0x3e, 0x47, 0xbf, 0x1d, 0x28,
  29692. 0x4c, 0x18, 0x1a, 0xfb, 0x96, 0x95, 0xda, 0xde,
  29693. 0x55
  29694. #endif
  29695. };
  29696. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  29697. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  29698. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  29699. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  29700. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  29701. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  29702. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  29703. };
  29704. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29705. userB = (ecc_key *)XMALLOC(sizeof(*userB), HEAP_HINT,
  29706. DYNAMIC_TYPE_TMP_BUFFER);
  29707. if (userB == NULL) {
  29708. ret = WC_TEST_RET_ENC_ERRNO;
  29709. }
  29710. #ifdef WOLFSSL_ECIES_OLD
  29711. if (ret == 0) {
  29712. userA = (ecc_key *)XMALLOC(sizeof(*userA), HEAP_HINT,
  29713. DYNAMIC_TYPE_TMP_BUFFER);
  29714. if (userA == NULL) {
  29715. ret = WC_TEST_RET_ENC_ERRNO;
  29716. }
  29717. }
  29718. #endif
  29719. #endif
  29720. if (ret == 0) {
  29721. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  29722. if (ret != 0)
  29723. ret = WC_TEST_RET_ENC_EC(ret);
  29724. }
  29725. if (ret == 0) {
  29726. userBInit = 1;
  29727. #ifdef WOLFSSL_ECIES_OLD
  29728. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  29729. if (ret != 0)
  29730. ret = WC_TEST_RET_ENC_EC(ret);
  29731. }
  29732. if (ret == 0) {
  29733. userAInit = 1;
  29734. tmpKey = userA;
  29735. #else
  29736. tmpKey = NULL;
  29737. #endif
  29738. }
  29739. if (ret == 0) {
  29740. #ifdef WOLFSSL_QNX_CAAM
  29741. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), pubKey,
  29742. sizeof(pubKey), userB, ECC_SECP256R1);
  29743. #else
  29744. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), NULL, 0,
  29745. userB, ECC_SECP256R1);
  29746. #endif
  29747. if (ret != 0)
  29748. ret = WC_TEST_RET_ENC_EC(ret);
  29749. }
  29750. #ifdef WOLFSSL_ECIES_OLD
  29751. if (ret == 0) {
  29752. ret = wc_ecc_import_x963_ex(pubKey, sizeof(pubKey), userA,
  29753. ECC_SECP256R1);
  29754. if (ret != 0)
  29755. ret = WC_TEST_RET_ENC_EC(ret);
  29756. }
  29757. #endif
  29758. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  29759. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  29760. !defined(HAVE_SELFTEST)
  29761. if (ret == 0) {
  29762. ret = wc_ecc_set_rng(userB, rng);
  29763. if (ret != 0) {
  29764. ret = WC_TEST_RET_ENC_EC(ret);
  29765. }
  29766. }
  29767. #else
  29768. (void)rng;
  29769. #endif
  29770. if (ret == 0) {
  29771. ret = wc_ecc_decrypt(userB, tmpKey, enc_msg, sizeof(enc_msg), plain,
  29772. &plainSz, NULL);
  29773. if (ret != 0)
  29774. ret = WC_TEST_RET_ENC_EC(ret);
  29775. }
  29776. if (ret == 0) {
  29777. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  29778. ret = WC_TEST_RET_ENC_NC;
  29779. }
  29780. }
  29781. if (userBInit)
  29782. wc_ecc_free(userB);
  29783. #ifdef WOLFSSL_ECIES_OLD
  29784. if (userAInit)
  29785. wc_ecc_free(userA);
  29786. #endif
  29787. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29788. if (userB != NULL) {
  29789. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29790. }
  29791. #ifdef WOLFSSL_ECIES_OLD
  29792. if (userA != NULL) {
  29793. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29794. }
  29795. #endif
  29796. #endif
  29797. return ret;
  29798. }
  29799. #endif
  29800. static wc_test_ret_t ecc_encrypt_e2e_test(WC_RNG* rng, ecc_key* userA, ecc_key* userB,
  29801. byte encAlgo, byte kdfAlgo, byte macAlgo)
  29802. {
  29803. wc_test_ret_t ret = 0;
  29804. byte msg[48];
  29805. byte plain[48];
  29806. #ifdef WOLFSSL_ECIES_OLD
  29807. byte out[80];
  29808. #elif defined(WOLFSSL_ECIES_GEN_IV)
  29809. byte out[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  29810. #else
  29811. byte out[1 + ECC_KEYGEN_SIZE * 2 + 80];
  29812. #endif
  29813. word32 outSz = sizeof(out);
  29814. word32 plainSz = sizeof(plain);
  29815. int i;
  29816. ecEncCtx* cliCtx = NULL;
  29817. ecEncCtx* srvCtx = NULL;
  29818. byte cliSalt[EXCHANGE_SALT_SZ];
  29819. byte srvSalt[EXCHANGE_SALT_SZ];
  29820. const byte* tmpSalt;
  29821. byte msg2[48];
  29822. byte plain2[48];
  29823. #ifdef WOLFSSL_ECIES_OLD
  29824. byte out2[80];
  29825. #elif defined(WOLFSSL_ECIES_GEN_IV)
  29826. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  29827. #else
  29828. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 80];
  29829. #endif
  29830. word32 outSz2 = sizeof(out2);
  29831. word32 plainSz2 = sizeof(plain2);
  29832. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29833. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof(ecc_key), HEAP_HINT,
  29834. DYNAMIC_TYPE_TMP_BUFFER);
  29835. #else
  29836. ecc_key tmpKey[1];
  29837. #endif
  29838. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29839. if (tmpKey == NULL) {
  29840. ERROR_OUT(MEMORY_E, done);
  29841. }
  29842. #endif
  29843. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  29844. if (ret != 0)
  29845. goto done;
  29846. /* set message to incrementing 0,1,2,etc... */
  29847. for (i = 0; i < (int)sizeof(msg); i++)
  29848. msg[i] = i;
  29849. /* encrypt msg to B */
  29850. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz, NULL);
  29851. if (ret != 0) {
  29852. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  29853. }
  29854. #ifdef WOLFSSL_ECIES_OLD
  29855. tmpKey->dp = userA->dp;
  29856. ret = wc_ecc_copy_point(&userA->pubkey, &tmpKey->pubkey);
  29857. if (ret != 0) {
  29858. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  29859. }
  29860. #endif
  29861. /* decrypt msg from A */
  29862. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, NULL);
  29863. if (ret != 0) {
  29864. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  29865. }
  29866. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  29867. ret = WC_TEST_RET_ENC_NC; goto done;
  29868. }
  29869. #ifndef WOLFSSL_ECIES_OLD
  29870. /* A decrypts msg (response) from B */
  29871. ret = wc_ecc_decrypt(userB, NULL, out, outSz, plain2, &plainSz2, NULL);
  29872. if (ret != 0)
  29873. goto done;
  29874. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  29875. ret = WC_TEST_RET_ENC_NC; goto done;
  29876. }
  29877. #endif
  29878. /* let's verify message exchange works, A is client, B is server */
  29879. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  29880. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  29881. if (cliCtx == NULL || srvCtx == NULL) {
  29882. ret = WC_TEST_RET_ENC_ERRNO; goto done;
  29883. }
  29884. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  29885. if (ret != 0)
  29886. goto done;
  29887. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  29888. if (ret != 0)
  29889. goto done;
  29890. /* get salt to send to peer */
  29891. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  29892. if (tmpSalt == NULL) {
  29893. ret = WC_TEST_RET_ENC_NC; goto done;
  29894. }
  29895. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  29896. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  29897. if (tmpSalt == NULL) {
  29898. ret = WC_TEST_RET_ENC_NC; goto done;
  29899. }
  29900. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  29901. /* in actual use, we'd get the peer's salt over the transport */
  29902. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  29903. if (ret != 0)
  29904. goto done;
  29905. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  29906. if (ret != 0)
  29907. goto done;
  29908. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11);
  29909. if (ret != 0)
  29910. goto done;
  29911. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11);
  29912. if (ret != 0)
  29913. goto done;
  29914. /* get encrypted msg (request) to send to B */
  29915. outSz = sizeof(out);
  29916. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz,cliCtx);
  29917. if (ret != 0)
  29918. goto done;
  29919. #ifndef WOLFSSL_ECIES_OLD
  29920. wc_ecc_free(tmpKey);
  29921. #endif
  29922. /* B decrypts msg (request) from A */
  29923. plainSz = sizeof(plain);
  29924. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  29925. if (ret != 0)
  29926. goto done;
  29927. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  29928. ret = WC_TEST_RET_ENC_NC; goto done;
  29929. }
  29930. /* msg2 (response) from B to A */
  29931. for (i = 0; i < (int)sizeof(msg2); i++)
  29932. msg2[i] = i + sizeof(msg2);
  29933. /* get encrypted msg (response) to send to B */
  29934. ret = wc_ecc_encrypt(userB, userA, msg2, sizeof(msg2), out2,
  29935. &outSz2, srvCtx);
  29936. if (ret != 0)
  29937. goto done;
  29938. #ifdef WOLFSSL_ECIES_OLD
  29939. tmpKey->dp = userB->dp;
  29940. ret = wc_ecc_copy_point(&userB->pubkey, &tmpKey->pubkey);
  29941. if (ret != 0) {
  29942. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  29943. }
  29944. #else
  29945. wc_ecc_free(tmpKey);
  29946. #endif
  29947. /* A decrypts msg (response) from B */
  29948. ret = wc_ecc_decrypt(userA, tmpKey, out2, outSz2, plain2, &plainSz2,
  29949. cliCtx);
  29950. if (ret != 0)
  29951. goto done;
  29952. if (XMEMCMP(plain2, msg2, sizeof(msg2)) != 0) {
  29953. ret = WC_TEST_RET_ENC_NC; goto done;
  29954. }
  29955. #if defined(HAVE_COMP_KEY) && \
  29956. (! defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  29957. /* Create new client and server contexts. */
  29958. wc_ecc_ctx_free(srvCtx);
  29959. wc_ecc_ctx_free(cliCtx);
  29960. /* let's verify message exchange works, A is client, B is server */
  29961. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  29962. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  29963. if (cliCtx == NULL || srvCtx == NULL) {
  29964. ret = WC_TEST_RET_ENC_ERRNO; goto done;
  29965. }
  29966. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  29967. if (ret != 0)
  29968. goto done;
  29969. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  29970. if (ret != 0)
  29971. goto done;
  29972. /* get salt to send to peer */
  29973. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  29974. if (tmpSalt == NULL) {
  29975. ret = WC_TEST_RET_ENC_NC; goto done;
  29976. }
  29977. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  29978. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  29979. if (tmpSalt == NULL) {
  29980. ret = WC_TEST_RET_ENC_NC; goto done;
  29981. }
  29982. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  29983. /* in actual use, we'd get the peer's salt over the transport */
  29984. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  29985. if (ret != 0)
  29986. goto done;
  29987. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  29988. if (ret != 0)
  29989. goto done;
  29990. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 12);
  29991. if (ret != 0)
  29992. goto done;
  29993. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 12);
  29994. if (ret != 0)
  29995. goto done;
  29996. /* get encrypted msg (request) to send to B - compressed public key */
  29997. outSz = sizeof(out);
  29998. ret = wc_ecc_encrypt_ex(userA, userB, msg, sizeof(msg), out, &outSz, cliCtx,
  29999. 1);
  30000. if (ret != 0)
  30001. goto done;
  30002. #ifndef WOLFSSL_ECIES_OLD
  30003. wc_ecc_free(tmpKey);
  30004. #endif
  30005. /* B decrypts msg (request) from A - out has a compressed public key */
  30006. plainSz = sizeof(plain);
  30007. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  30008. if (ret != 0)
  30009. goto done;
  30010. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  30011. ret = WC_TEST_RET_ENC_NC; goto done;
  30012. }
  30013. #endif /* HAVE_COMP_KEY && (!FIPS || FIPS>=5.3) */
  30014. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  30015. (ECC_MIN_KEY_SZ <= 256) && defined(WOLFSSL_AES_128)
  30016. ret = ecc_encrypt_kat(rng);
  30017. #endif
  30018. done:
  30019. /* cleanup */
  30020. wc_ecc_ctx_free(srvCtx);
  30021. wc_ecc_ctx_free(cliCtx);
  30022. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30023. if (tmpKey != NULL) {
  30024. wc_ecc_free(tmpKey);
  30025. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30026. }
  30027. #else
  30028. wc_ecc_free(tmpKey);
  30029. #endif
  30030. return ret;
  30031. }
  30032. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  30033. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_encrypt_test(void)
  30034. {
  30035. WC_RNG rng;
  30036. wc_test_ret_t ret;
  30037. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30038. ecc_key *userA;
  30039. ecc_key *userB;
  30040. #else
  30041. ecc_key userA[1];
  30042. ecc_key userB[1];
  30043. #endif
  30044. WOLFSSL_ENTER("ecc_encrypt_test");
  30045. #ifndef HAVE_FIPS
  30046. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  30047. #else
  30048. ret = wc_InitRng(&rng);
  30049. #endif
  30050. if (ret != 0)
  30051. return WC_TEST_RET_ENC_EC(ret);
  30052. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30053. userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT,
  30054. DYNAMIC_TYPE_TMP_BUFFER);
  30055. userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT,
  30056. DYNAMIC_TYPE_TMP_BUFFER);
  30057. if ((userA == NULL) || (userB == NULL)) {
  30058. ERROR_OUT(MEMORY_E, done);
  30059. }
  30060. #endif
  30061. XMEMSET(userA, 0, sizeof *userA);
  30062. XMEMSET(userB, 0, sizeof *userB);
  30063. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  30064. if (ret != 0)
  30065. goto done;
  30066. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  30067. if (ret != 0)
  30068. goto done;
  30069. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userA);
  30070. #if defined(WOLFSSL_ASYNC_CRYPT)
  30071. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  30072. #endif
  30073. if (ret != 0){
  30074. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  30075. }
  30076. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userB);
  30077. #if defined(WOLFSSL_ASYNC_CRYPT)
  30078. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  30079. #endif
  30080. if (ret != 0){
  30081. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  30082. }
  30083. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  30084. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  30085. !defined(HAVE_SELFTEST)
  30086. ret = wc_ecc_set_rng(userA, &rng);
  30087. if (ret != 0) {
  30088. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  30089. }
  30090. ret = wc_ecc_set_rng(userB, &rng);
  30091. if (ret != 0) {
  30092. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  30093. }
  30094. #endif
  30095. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  30096. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  30097. #ifdef WOLFSSL_AES_128
  30098. if (ret == 0) {
  30099. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  30100. ecHKDF_SHA256, ecHMAC_SHA256);
  30101. if (ret != 0) {
  30102. printf("ECIES: AES_128_CBC, HKDF_SHA256, HMAC_SHA256\n");
  30103. }
  30104. }
  30105. #ifdef HAVE_X963_KDF
  30106. if (ret == 0) {
  30107. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  30108. ecKDF_X963_SHA256, ecHMAC_SHA256);
  30109. if (ret != 0) {
  30110. printf("ECIES: AES_128_CBC, KDF_X963_SHA256, HMAC_SHA256\n");
  30111. }
  30112. }
  30113. if (ret == 0) {
  30114. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  30115. ecKDF_SHA256, ecHMAC_SHA256);
  30116. if (ret != 0) {
  30117. printf("ECIES: AES_128_CBC, KDF_SHA256, HMAC_SHA256\n");
  30118. }
  30119. }
  30120. #endif
  30121. #endif
  30122. #ifdef WOLFSSL_AES_256
  30123. if (ret == 0) {
  30124. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CBC,
  30125. ecHKDF_SHA256, ecHMAC_SHA256);
  30126. if (ret != 0) {
  30127. printf("ECIES: AES_256_CBC, HKDF_SHA256, HMAC_SHA256\n");
  30128. }
  30129. }
  30130. #endif
  30131. #endif
  30132. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER)
  30133. #ifdef WOLFSSL_AES_128
  30134. if (ret == 0) {
  30135. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CTR,
  30136. ecHKDF_SHA256, ecHMAC_SHA256);
  30137. if (ret != 0) {
  30138. printf("ECIES: AES_128_CTR, HKDF_SHA256, HMAC_SHA256\n");
  30139. }
  30140. }
  30141. #endif
  30142. #ifdef WOLFSSL_AES_256
  30143. if (ret == 0) {
  30144. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CTR,
  30145. ecHKDF_SHA256, ecHMAC_SHA256);
  30146. if (ret != 0) {
  30147. printf("ECIES: AES_256_CTR, HKDF_SHA256, HMAC_SHA256\n");
  30148. }
  30149. }
  30150. #endif
  30151. #endif /* !NO_AES && WOLFSSL_AES_COUNTER */
  30152. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  30153. if (ret == 0) {
  30154. ret = ecc_ctx_kdf_salt_test(&rng, userA, userB);
  30155. }
  30156. #endif
  30157. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  30158. done:
  30159. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30160. if (userA != NULL) {
  30161. wc_ecc_free(userA);
  30162. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30163. }
  30164. if (userB != NULL) {
  30165. wc_ecc_free(userB);
  30166. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30167. }
  30168. #else
  30169. wc_ecc_free(userB);
  30170. wc_ecc_free(userA);
  30171. #endif
  30172. wc_FreeRng(&rng);
  30173. return ret;
  30174. }
  30175. #endif /* HAVE_ECC_ENCRYPT && HAVE_AES_CBC && WOLFSSL_AES_128 */
  30176. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  30177. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  30178. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  30179. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  30180. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test_buffers(void)
  30181. {
  30182. size_t bytes;
  30183. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30184. ecc_key *cliKey = (ecc_key *)XMALLOC(sizeof *cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30185. ecc_key *servKey = (ecc_key *)XMALLOC(sizeof *servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30186. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof *tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30187. #else
  30188. ecc_key cliKey[1];
  30189. ecc_key servKey[1];
  30190. ecc_key tmpKey[1];
  30191. #endif
  30192. WC_RNG rng;
  30193. word32 idx = 0;
  30194. wc_test_ret_t ret;
  30195. /* pad our test message to 32 bytes so evenly divisible by AES_BLOCK_SZ */
  30196. byte in[] = "Everyone gets Friday off. ecc p";
  30197. word32 inLen = (word32)XSTRLEN((char*)in);
  30198. byte out[256];
  30199. byte plain[256];
  30200. int verify = 0;
  30201. word32 x;
  30202. WOLFSSL_ENTER("ecc_test_buffers");
  30203. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30204. if ((cliKey == NULL) || (servKey == NULL) || (tmpKey == NULL))
  30205. ERROR_OUT(MEMORY_E, done);
  30206. #endif
  30207. ret = wc_ecc_init_ex(cliKey, HEAP_HINT, devId);
  30208. if (ret != 0)
  30209. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30210. ret = wc_ecc_init_ex(servKey, HEAP_HINT, devId);
  30211. if (ret != 0)
  30212. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30213. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  30214. if (ret != 0)
  30215. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30216. bytes = (size_t)sizeof_ecc_clikey_der_256;
  30217. /* place client key into ecc_key struct cliKey */
  30218. ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, cliKey,
  30219. (word32)bytes);
  30220. if (ret != 0)
  30221. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30222. idx = 0;
  30223. bytes = (size_t)sizeof_ecc_key_der_256;
  30224. /* place server key into ecc_key struct servKey */
  30225. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, servKey,
  30226. (word32)bytes);
  30227. if (ret != 0)
  30228. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30229. #ifndef WC_NO_RNG
  30230. #ifndef HAVE_FIPS
  30231. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  30232. #else
  30233. ret = wc_InitRng(&rng);
  30234. #endif
  30235. if (ret != 0)
  30236. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30237. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  30238. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  30239. !defined(HAVE_SELFTEST)
  30240. ret = wc_ecc_set_rng(cliKey, &rng);
  30241. if (ret != 0)
  30242. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30243. ret = wc_ecc_set_rng(servKey, &rng);
  30244. if (ret != 0)
  30245. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30246. #endif
  30247. #endif /* !WC_NO_RNG */
  30248. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_HKDF) && \
  30249. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  30250. {
  30251. word32 y;
  30252. /* test encrypt and decrypt if they're available */
  30253. x = sizeof(out);
  30254. ret = wc_ecc_encrypt(cliKey, servKey, in, sizeof(in), out, &x, NULL);
  30255. if (ret < 0)
  30256. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30257. #ifdef WOLFSSL_ECIES_OLD
  30258. tmpKey->dp = cliKey->dp;
  30259. ret = wc_ecc_copy_point(&cliKey->pubkey, &tmpKey->pubkey);
  30260. if (ret != 0) {
  30261. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  30262. }
  30263. #endif
  30264. y = sizeof(plain);
  30265. ret = wc_ecc_decrypt(servKey, tmpKey, out, x, plain, &y, NULL);
  30266. if (ret < 0)
  30267. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30268. if (XMEMCMP(plain, in, inLen))
  30269. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  30270. }
  30271. #endif
  30272. x = sizeof(out);
  30273. do {
  30274. #if defined(WOLFSSL_ASYNC_CRYPT)
  30275. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  30276. #endif
  30277. if (ret == 0)
  30278. ret = wc_ecc_sign_hash(in, inLen, out, &x, &rng, cliKey);
  30279. } while (ret == WC_PENDING_E);
  30280. if (ret < 0)
  30281. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30282. TEST_SLEEP();
  30283. XMEMSET(plain, 0, sizeof(plain));
  30284. do {
  30285. #if defined(WOLFSSL_ASYNC_CRYPT)
  30286. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  30287. #endif
  30288. if (ret == 0)
  30289. ret = wc_ecc_verify_hash(out, x, in, inLen, &verify,
  30290. cliKey);
  30291. } while (ret == WC_PENDING_E);
  30292. if (ret < 0)
  30293. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30294. if (verify != 1)
  30295. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  30296. TEST_SLEEP();
  30297. #ifdef WOLFSSL_CERT_EXT
  30298. idx = 0;
  30299. bytes = sizeof_ecc_clikeypub_der_256;
  30300. ret = wc_EccPublicKeyDecode(ecc_clikeypub_der_256, &idx, cliKey,
  30301. (word32) bytes);
  30302. if (ret != 0)
  30303. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30304. #endif
  30305. ret = 0;
  30306. done:
  30307. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30308. if (cliKey != NULL) {
  30309. wc_ecc_free(cliKey);
  30310. XFREE(cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30311. }
  30312. if (servKey != NULL) {
  30313. wc_ecc_free(servKey);
  30314. XFREE(servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30315. }
  30316. if (tmpKey != NULL) {
  30317. wc_ecc_free(tmpKey);
  30318. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30319. }
  30320. #else
  30321. wc_ecc_free(cliKey);
  30322. wc_ecc_free(servKey);
  30323. wc_ecc_free(tmpKey);
  30324. #endif
  30325. wc_FreeRng(&rng);
  30326. return ret;
  30327. }
  30328. #endif /* USE_CERT_BUFFERS_256 && !WOLFSSL_ATECCX08A && !NO_ECC256 */
  30329. #endif /* HAVE_ECC */
  30330. #ifdef HAVE_CURVE25519
  30331. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  30332. defined(HAVE_CURVE25519_KEY_IMPORT)
  30333. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  30334. #define X25519_TEST_CNT 5
  30335. #else
  30336. #define X25519_TEST_CNT 1
  30337. #endif
  30338. static wc_test_ret_t curve25519_overflow_test(void)
  30339. {
  30340. /* secret key for party a */
  30341. byte sa[X25519_TEST_CNT][32] = {
  30342. {
  30343. 0x8d,0xaf,0x6e,0x7a,0xc1,0xeb,0x8d,0x30,
  30344. 0x99,0x86,0xd3,0x90,0x47,0x96,0x21,0x3c,
  30345. 0x3a,0x75,0xc0,0x7b,0x75,0x01,0x75,0xa3,
  30346. 0x81,0x4b,0xff,0x5a,0xbc,0x96,0x87,0x28
  30347. },
  30348. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  30349. {
  30350. 0x9d,0x63,0x5f,0xce,0xe2,0xe8,0xd7,0xfb,
  30351. 0x68,0x77,0x0e,0x44,0xd1,0xad,0x87,0x2b,
  30352. 0xf4,0x65,0x06,0xb7,0xbb,0xdb,0xbe,0x6e,
  30353. 0x02,0x43,0x24,0xc7,0x3d,0x7b,0x88,0x60
  30354. },
  30355. {
  30356. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  30357. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  30358. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  30359. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  30360. },
  30361. {
  30362. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  30363. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  30364. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  30365. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  30366. },
  30367. {
  30368. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  30369. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  30370. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  30371. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  30372. }
  30373. #endif
  30374. };
  30375. /* public key for party b */
  30376. byte pb[X25519_TEST_CNT][32] = {
  30377. {
  30378. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30379. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30380. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30381. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  30382. },
  30383. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  30384. {
  30385. /* 0xff first byte in original - invalid! */
  30386. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30387. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30388. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30389. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  30390. },
  30391. {
  30392. 0x36,0x1a,0x74,0x87,0x28,0x59,0xe0,0xb6,
  30393. 0xe4,0x2b,0x17,0x9b,0x16,0xb0,0x3b,0xf8,
  30394. 0xb8,0x9f,0x2a,0x8f,0xc5,0x33,0x68,0x4f,
  30395. 0xde,0x4d,0xd8,0x80,0x63,0xe7,0xb4,0x0a
  30396. },
  30397. {
  30398. 0x00,0x80,0x38,0x59,0x19,0x3a,0x66,0x12,
  30399. 0xfd,0xa1,0xec,0x1c,0x40,0x84,0x40,0xbd,
  30400. 0x64,0x10,0x8b,0x53,0x81,0x21,0x03,0x2d,
  30401. 0x7d,0x33,0xb4,0x01,0x57,0x0d,0xe1,0x89
  30402. },
  30403. {
  30404. 0x1d,0xf8,0xf8,0x33,0x89,0x6c,0xb7,0xba,
  30405. 0x94,0x73,0xfa,0xc2,0x36,0xac,0xbe,0x49,
  30406. 0xaf,0x85,0x3e,0x93,0x5f,0xae,0xb2,0xc0,
  30407. 0xc8,0x80,0x8f,0x4a,0xaa,0xd3,0x55,0x2b
  30408. }
  30409. #endif
  30410. };
  30411. /* expected shared key */
  30412. byte ss[X25519_TEST_CNT][32] = {
  30413. {
  30414. 0x5c,0x4c,0x85,0x5f,0xfb,0x20,0x38,0xcc,
  30415. 0x55,0x16,0x5b,0x8a,0xa7,0xed,0x57,0x6e,
  30416. 0x35,0xaa,0x71,0x67,0x85,0x1f,0xb6,0x28,
  30417. 0x17,0x07,0x7b,0xda,0x76,0xdd,0xe0,0xb4
  30418. },
  30419. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  30420. {
  30421. 0x33,0xf6,0xc1,0x34,0x62,0x92,0x06,0x02,
  30422. 0x95,0xdb,0x91,0x4c,0x5d,0x52,0x54,0xc7,
  30423. 0xd2,0x5b,0x24,0xb5,0x4f,0x33,0x59,0x79,
  30424. 0x9f,0x6d,0x7e,0x4a,0x4c,0x30,0xd6,0x38
  30425. },
  30426. {
  30427. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30428. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30429. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30430. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02
  30431. },
  30432. {
  30433. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30434. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30435. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30436. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09
  30437. },
  30438. {
  30439. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30440. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30441. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30442. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10
  30443. }
  30444. #endif
  30445. };
  30446. wc_test_ret_t ret = 0;
  30447. int i;
  30448. word32 y;
  30449. byte shared[32];
  30450. curve25519_key userA;
  30451. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  30452. for (i = 0; i < X25519_TEST_CNT; i++) {
  30453. if (wc_curve25519_import_private_raw(sa[i], sizeof(sa[i]), pb[i],
  30454. sizeof(pb[i]), &userA) != 0) {
  30455. ret = WC_TEST_RET_ENC_I(i); break;
  30456. }
  30457. /* test against known test vector */
  30458. XMEMSET(shared, 0, sizeof(shared));
  30459. y = sizeof(shared);
  30460. if (wc_curve25519_shared_secret(&userA, &userA, shared, &y) != 0) {
  30461. ret = WC_TEST_RET_ENC_I(i); break;
  30462. }
  30463. if (XMEMCMP(ss[i], shared, y)) {
  30464. ret = WC_TEST_RET_ENC_I(i); break;
  30465. }
  30466. }
  30467. wc_curve25519_free(&userA);
  30468. return ret;
  30469. }
  30470. /* Test the wc_curve25519_check_public API.
  30471. *
  30472. * returns 0 on success and -ve on failure.
  30473. */
  30474. static wc_test_ret_t curve25519_check_public_test(void)
  30475. {
  30476. wc_test_ret_t ret;
  30477. /* Little-endian values that will fail */
  30478. byte fail_le[][CURVE25519_KEYSIZE] = {
  30479. {
  30480. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30481. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30482. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30483. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  30484. },
  30485. {
  30486. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30487. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30488. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30489. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  30490. },
  30491. {
  30492. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30493. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30494. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30495. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x81
  30496. },
  30497. };
  30498. /* Big-endian values that will fail */
  30499. byte fail_be[][CURVE25519_KEYSIZE] = {
  30500. {
  30501. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30502. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30503. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30504. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  30505. },
  30506. {
  30507. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30508. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30509. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30510. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  30511. },
  30512. {
  30513. 0x81,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30514. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30515. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30516. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  30517. },
  30518. };
  30519. /* Good or valid public value */
  30520. byte good[CURVE25519_KEYSIZE] = {
  30521. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30522. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30523. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30524. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  30525. };
  30526. int i;
  30527. /* Parameter checks */
  30528. /* NULL pointer */
  30529. ret = wc_curve25519_check_public(NULL, 0, EC25519_LITTLE_ENDIAN);
  30530. if (ret != BAD_FUNC_ARG) {
  30531. return WC_TEST_RET_ENC_EC(ret);
  30532. }
  30533. ret = wc_curve25519_check_public(NULL, 0, EC25519_BIG_ENDIAN);
  30534. if (ret != BAD_FUNC_ARG) {
  30535. return WC_TEST_RET_ENC_EC(ret);
  30536. }
  30537. /* Length of 0 treated differently to other invalid lengths for TLS */
  30538. ret = wc_curve25519_check_public(good, 0, EC25519_LITTLE_ENDIAN);
  30539. if (ret != BUFFER_E)
  30540. return WC_TEST_RET_ENC_EC(ret);
  30541. ret = wc_curve25519_check_public(good, 0, EC25519_BIG_ENDIAN);
  30542. if (ret != BUFFER_E)
  30543. return WC_TEST_RET_ENC_EC(ret);
  30544. /* Length not CURVE25519_KEYSIZE */
  30545. for (i = 1; i < CURVE25519_KEYSIZE + 2; i++) {
  30546. if (i == CURVE25519_KEYSIZE)
  30547. continue;
  30548. if (wc_curve25519_check_public(good, (word32)i, EC25519_LITTLE_ENDIAN) !=
  30549. ECC_BAD_ARG_E) {
  30550. return WC_TEST_RET_ENC_I(i);
  30551. }
  30552. if (wc_curve25519_check_public(good, (word32)i, EC25519_BIG_ENDIAN) !=
  30553. ECC_BAD_ARG_E) {
  30554. return WC_TEST_RET_ENC_I(i);
  30555. }
  30556. }
  30557. /* Little-endian fail cases */
  30558. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  30559. if (wc_curve25519_check_public(fail_le[i], CURVE25519_KEYSIZE,
  30560. EC25519_LITTLE_ENDIAN) == 0) {
  30561. return WC_TEST_RET_ENC_I(i);
  30562. }
  30563. }
  30564. /* Big-endian fail cases */
  30565. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  30566. if (wc_curve25519_check_public(fail_be[i], CURVE25519_KEYSIZE,
  30567. EC25519_BIG_ENDIAN) == 0) {
  30568. return WC_TEST_RET_ENC_I(i);
  30569. }
  30570. }
  30571. /* Check a valid public value works! */
  30572. ret = wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  30573. EC25519_LITTLE_ENDIAN);
  30574. if (ret != 0) {
  30575. return WC_TEST_RET_ENC_EC(ret);
  30576. }
  30577. ret = wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  30578. EC25519_BIG_ENDIAN);
  30579. if (ret != 0) {
  30580. return WC_TEST_RET_ENC_EC(ret);
  30581. }
  30582. return 0;
  30583. }
  30584. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  30585. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  30586. defined(HAVE_CURVE25519_KEY_IMPORT)
  30587. static wc_test_ret_t curve255519_der_test(void)
  30588. {
  30589. wc_test_ret_t ret = 0;
  30590. /* certs/statickeys/x25519.der */
  30591. const byte kCurve25519PrivDer[] = {
  30592. 0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E,
  30593. 0x04, 0x22, 0x04, 0x20, 0x78, 0x8E, 0x31, 0x5C, 0x33, 0xA9, 0x19, 0xC0,
  30594. 0x5E, 0x36, 0x70, 0x1B, 0xA4, 0xE8, 0xEF, 0xC1, 0x89, 0x8C, 0xB3, 0x15,
  30595. 0xC6, 0x79, 0xD3, 0xAC, 0x22, 0x00, 0xAE, 0xFA, 0xB3, 0xB7, 0x0F, 0x78
  30596. };
  30597. /* certs/statickeys/x25519-pub.der */
  30598. const byte kCurve25519PubDer[] = {
  30599. 0x30, 0x2A, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E, 0x03, 0x21, 0x00,
  30600. 0x09, 0xBC, 0x8C, 0xC7, 0x45, 0x0D, 0xC1, 0xC2, 0x02, 0x57, 0x9A, 0x68,
  30601. 0x3A, 0xFD, 0x7A, 0xA8, 0xA5, 0x2F, 0xF0, 0x99, 0x39, 0x98, 0xEA, 0x26,
  30602. 0xA2, 0x5B, 0x38, 0xFD, 0x96, 0xDB, 0x2A, 0x26
  30603. };
  30604. curve25519_key key;
  30605. byte output[128];
  30606. word32 outputSz = 128;
  30607. word32 idx;
  30608. ret = wc_curve25519_init_ex(&key, HEAP_HINT, devId);
  30609. if (ret != 0)
  30610. return WC_TEST_RET_ENC_EC(ret);
  30611. /* Test decode / encode of Curve25519 private key only */
  30612. if (ret == 0) {
  30613. idx = 0;
  30614. ret = wc_Curve25519PrivateKeyDecode(kCurve25519PrivDer, &idx, &key,
  30615. (word32)sizeof(kCurve25519PrivDer));
  30616. if (ret < 0)
  30617. ret = WC_TEST_RET_ENC_EC(ret);
  30618. }
  30619. if (ret == 0) {
  30620. outputSz = (word32)sizeof(output);
  30621. ret = wc_Curve25519PrivateKeyToDer(&key, output, outputSz);
  30622. if (ret >= 0) {
  30623. outputSz = (word32)ret;
  30624. ret = 0;
  30625. }
  30626. else {
  30627. ret = WC_TEST_RET_ENC_EC(ret);
  30628. }
  30629. }
  30630. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PrivDer) ||
  30631. XMEMCMP(output, kCurve25519PrivDer, outputSz) != 0)) {
  30632. ret = WC_TEST_RET_ENC_NC;
  30633. }
  30634. /* Test decode / encode of Curve25519 public key only */
  30635. if (ret == 0) {
  30636. idx = 0;
  30637. ret = wc_Curve25519PublicKeyDecode(kCurve25519PubDer, &idx, &key,
  30638. (word32)sizeof(kCurve25519PubDer));
  30639. if (ret < 0)
  30640. ret = WC_TEST_RET_ENC_EC(ret);
  30641. }
  30642. if (ret == 0) {
  30643. outputSz = (word32)sizeof(output);
  30644. ret = wc_Curve25519PublicKeyToDer(&key, output, outputSz, 1);
  30645. if (ret >= 0) {
  30646. outputSz = (word32)ret;
  30647. ret = 0;
  30648. }
  30649. else {
  30650. ret = WC_TEST_RET_ENC_EC(ret);
  30651. }
  30652. }
  30653. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PubDer) ||
  30654. XMEMCMP(output, kCurve25519PubDer, outputSz) != 0)) {
  30655. ret = WC_TEST_RET_ENC_NC;
  30656. }
  30657. wc_curve25519_free(&key);
  30658. return ret;
  30659. }
  30660. #endif /* !NO_ASN && HAVE_CURVE25519_KEY_EXPORT && HAVE_CURVE25519_KEY_IMPORT */
  30661. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve25519_test(void)
  30662. {
  30663. WC_RNG rng;
  30664. wc_test_ret_t ret;
  30665. #ifdef HAVE_CURVE25519_SHARED_SECRET
  30666. byte sharedA[32];
  30667. byte sharedB[32];
  30668. word32 y;
  30669. #endif
  30670. #ifdef HAVE_CURVE25519_KEY_EXPORT
  30671. byte exportBuf[32];
  30672. #endif
  30673. word32 x = 0;
  30674. curve25519_key userA, userB, pubKey;
  30675. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  30676. defined(HAVE_CURVE25519_KEY_IMPORT)
  30677. /* test vectors from
  30678. https://tools.ietf.org/html/draft-josefsson-tls-curve25519-03
  30679. */
  30680. /* secret key for party a */
  30681. byte sa[] = {
  30682. 0x5A,0xC9,0x9F,0x33,0x63,0x2E,0x5A,0x76,
  30683. 0x8D,0xE7,0xE8,0x1B,0xF8,0x54,0xC2,0x7C,
  30684. 0x46,0xE3,0xFB,0xF2,0xAB,0xBA,0xCD,0x29,
  30685. 0xEC,0x4A,0xFF,0x51,0x73,0x69,0xC6,0x60
  30686. };
  30687. /* public key for party a */
  30688. byte pa[] = {
  30689. 0x05,0x7E,0x23,0xEA,0x9F,0x1C,0xBE,0x8A,
  30690. 0x27,0x16,0x8F,0x6E,0x69,0x6A,0x79,0x1D,
  30691. 0xE6,0x1D,0xD3,0xAF,0x7A,0xCD,0x4E,0xEA,
  30692. 0xCC,0x6E,0x7B,0xA5,0x14,0xFD,0xA8,0x63
  30693. };
  30694. /* secret key for party b */
  30695. byte sb[] = {
  30696. 0x47,0xDC,0x3D,0x21,0x41,0x74,0x82,0x0E,
  30697. 0x11,0x54,0xB4,0x9B,0xC6,0xCD,0xB2,0xAB,
  30698. 0xD4,0x5E,0xE9,0x58,0x17,0x05,0x5D,0x25,
  30699. 0x5A,0xA3,0x58,0x31,0xB7,0x0D,0x32,0x60
  30700. };
  30701. /* public key for party b */
  30702. byte pb[] = {
  30703. 0x6E,0xB8,0x9D,0xA9,0x19,0x89,0xAE,0x37,
  30704. 0xC7,0xEA,0xC7,0x61,0x8D,0x9E,0x5C,0x49,
  30705. 0x51,0xDB,0xA1,0xD7,0x3C,0x28,0x5A,0xE1,
  30706. 0xCD,0x26,0xA8,0x55,0x02,0x0E,0xEF,0x04
  30707. };
  30708. /* expected shared key */
  30709. byte ss[] = {
  30710. 0x61,0x45,0x0C,0xD9,0x8E,0x36,0x01,0x6B,
  30711. 0x58,0x77,0x6A,0x89,0x7A,0x9F,0x0A,0xEF,
  30712. 0x73,0x8B,0x99,0xF0,0x94,0x68,0xB8,0xD6,
  30713. 0xB8,0x51,0x11,0x84,0xD5,0x34,0x94,0xAB
  30714. };
  30715. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  30716. (void)x;
  30717. WOLFSSL_ENTER("curve25519_test");
  30718. #ifndef HAVE_FIPS
  30719. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  30720. #else
  30721. ret = wc_InitRng(&rng);
  30722. #endif
  30723. if (ret != 0)
  30724. return WC_TEST_RET_ENC_EC(ret);
  30725. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  30726. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  30727. wc_curve25519_init_ex(&pubKey, HEAP_HINT, devId);
  30728. /* make curve25519 keys */
  30729. ret = wc_curve25519_make_key(&rng, 32, &userA);
  30730. if (ret != 0)
  30731. return WC_TEST_RET_ENC_EC(ret);
  30732. ret = wc_curve25519_make_key(&rng, 32, &userB);
  30733. if (ret != 0)
  30734. return WC_TEST_RET_ENC_EC(ret);
  30735. #ifdef HAVE_CURVE25519_SHARED_SECRET
  30736. /* find shared secret key */
  30737. x = sizeof(sharedA);
  30738. if ((ret = wc_curve25519_shared_secret(&userA, &userB, sharedA, &x)) != 0) {
  30739. printf("wc_curve25519_shared_secret 1 failed\n");
  30740. return WC_TEST_RET_ENC_EC(ret);
  30741. }
  30742. y = sizeof(sharedB);
  30743. if ((ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y)) != 0) {
  30744. printf("wc_curve25519_shared_secret 2 failed\n");
  30745. return WC_TEST_RET_ENC_EC(ret);
  30746. }
  30747. /* compare shared secret keys to test they are the same */
  30748. if (y != x)
  30749. return WC_TEST_RET_ENC_NC;
  30750. if (XMEMCMP(sharedA, sharedB, x))
  30751. return WC_TEST_RET_ENC_NC;
  30752. #endif
  30753. #ifdef HAVE_CURVE25519_KEY_EXPORT
  30754. /* export a public key and import it for another user */
  30755. x = sizeof(exportBuf);
  30756. ret = wc_curve25519_export_public(&userA, exportBuf, &x);
  30757. if (ret != 0)
  30758. return WC_TEST_RET_ENC_EC(ret);
  30759. #ifdef HAVE_CURVE25519_KEY_IMPORT
  30760. ret = wc_curve25519_import_public(exportBuf, x, &pubKey);
  30761. if (ret != 0)
  30762. return WC_TEST_RET_ENC_EC(ret);
  30763. #endif
  30764. #endif
  30765. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  30766. defined(HAVE_CURVE25519_KEY_IMPORT)
  30767. /* test shared key after importing a public key */
  30768. XMEMSET(sharedB, 0, sizeof(sharedB));
  30769. y = sizeof(sharedB);
  30770. if (wc_curve25519_shared_secret(&userB, &pubKey, sharedB, &y) != 0) {
  30771. return WC_TEST_RET_ENC_NC;
  30772. }
  30773. if (XMEMCMP(sharedA, sharedB, y))
  30774. return WC_TEST_RET_ENC_NC;
  30775. /* import RFC test vectors and compare shared key */
  30776. ret = wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa),
  30777. &userA);
  30778. if (ret != 0)
  30779. return WC_TEST_RET_ENC_EC(ret);
  30780. ret = wc_curve25519_import_private_raw(sb, sizeof(sb), pb, sizeof(pb),
  30781. &userB);
  30782. if (ret != 0)
  30783. return WC_TEST_RET_ENC_EC(ret);
  30784. /* test against known test vector */
  30785. XMEMSET(sharedB, 0, sizeof(sharedB));
  30786. y = sizeof(sharedB);
  30787. ret = wc_curve25519_shared_secret(&userA, &userB, sharedB, &y);
  30788. if (ret != 0)
  30789. return WC_TEST_RET_ENC_EC(ret);
  30790. if (XMEMCMP(ss, sharedB, y))
  30791. return WC_TEST_RET_ENC_NC;
  30792. /* test swapping roles of keys and generating same shared key */
  30793. XMEMSET(sharedB, 0, sizeof(sharedB));
  30794. y = sizeof(sharedB);
  30795. ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y);
  30796. if (ret != 0)
  30797. return WC_TEST_RET_ENC_EC(ret);
  30798. if (XMEMCMP(ss, sharedB, y))
  30799. return WC_TEST_RET_ENC_NC;
  30800. /* test with 1 generated key and 1 from known test vector */
  30801. ret = wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa),
  30802. &userA);
  30803. if (ret != 0)
  30804. return WC_TEST_RET_ENC_EC(ret);
  30805. wc_curve25519_free(&userB);
  30806. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  30807. ret = wc_curve25519_make_key(&rng, 32, &userB);
  30808. if (ret != 0)
  30809. return WC_TEST_RET_ENC_EC(ret);
  30810. x = sizeof(sharedA);
  30811. ret = wc_curve25519_shared_secret(&userA, &userB, sharedA, &x);
  30812. if (ret != 0)
  30813. return WC_TEST_RET_ENC_EC(ret);
  30814. y = sizeof(sharedB);
  30815. ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y);
  30816. if (ret != 0)
  30817. return WC_TEST_RET_ENC_EC(ret);
  30818. /* compare shared secret keys to test they are the same */
  30819. if (y != x)
  30820. return WC_TEST_RET_ENC_NC;
  30821. if (XMEMCMP(sharedA, sharedB, x))
  30822. return WC_TEST_RET_ENC_NC;
  30823. ret = curve25519_overflow_test();
  30824. if (ret != 0)
  30825. return ret;
  30826. ret = curve25519_check_public_test();
  30827. if (ret != 0)
  30828. return ret;
  30829. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  30830. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  30831. defined(HAVE_CURVE25519_KEY_IMPORT)
  30832. ret = curve255519_der_test();
  30833. if (ret != 0)
  30834. return ret;
  30835. #endif
  30836. /* clean up keys when done */
  30837. wc_curve25519_free(&pubKey);
  30838. wc_curve25519_free(&userB);
  30839. wc_curve25519_free(&userA);
  30840. wc_FreeRng(&rng);
  30841. return 0;
  30842. }
  30843. #endif /* HAVE_CURVE25519 */
  30844. #ifdef HAVE_ED25519
  30845. #ifdef WOLFSSL_TEST_CERT
  30846. static wc_test_ret_t ed25519_test_cert(void)
  30847. {
  30848. DecodedCert cert[2];
  30849. DecodedCert* serverCert = NULL;
  30850. DecodedCert* caCert = NULL;
  30851. #ifdef HAVE_ED25519_VERIFY
  30852. ed25519_key key;
  30853. ed25519_key* pubKey = NULL;
  30854. int verify;
  30855. #endif /* HAVE_ED25519_VERIFY */
  30856. wc_test_ret_t ret;
  30857. byte* tmp;
  30858. size_t bytes;
  30859. XFILE file;
  30860. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30861. if (tmp == NULL) {
  30862. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  30863. }
  30864. #ifdef USE_CERT_BUFFERS_256
  30865. XMEMCPY(tmp, ca_ed25519_cert, sizeof_ca_ed25519_cert);
  30866. bytes = sizeof_ca_ed25519_cert;
  30867. #elif !defined(NO_FILESYSTEM)
  30868. file = XFOPEN(caEd25519Cert, "rb");
  30869. if (file == NULL) {
  30870. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  30871. }
  30872. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  30873. XFCLOSE(file);
  30874. if (bytes == 0)
  30875. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  30876. #else
  30877. /* No certificate to use. */
  30878. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  30879. #endif
  30880. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  30881. caCert = &cert[0];
  30882. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  30883. if (ret != 0)
  30884. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30885. #ifdef USE_CERT_BUFFERS_256
  30886. XMEMCPY(tmp, server_ed25519_cert, sizeof_server_ed25519_cert);
  30887. bytes = sizeof_server_ed25519_cert;
  30888. #elif !defined(NO_FILESYSTEM)
  30889. file = XFOPEN(serverEd25519Cert, "rb");
  30890. if (file == NULL) {
  30891. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  30892. }
  30893. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  30894. XFCLOSE(file);
  30895. if (bytes == 0)
  30896. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  30897. #else
  30898. /* No certificate to use. */
  30899. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  30900. #endif
  30901. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  30902. serverCert = &cert[1];
  30903. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  30904. if (ret != 0)
  30905. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30906. #ifdef HAVE_ED25519_VERIFY
  30907. ret = wc_ed25519_init(&key);
  30908. if (ret < 0)
  30909. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30910. pubKey = &key;
  30911. ret = wc_ed25519_import_public(caCert->publicKey, caCert->pubKeySize,
  30912. pubKey);
  30913. if (ret < 0)
  30914. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30915. ret = wc_ed25519_verify_msg(serverCert->signature, serverCert->sigLength,
  30916. serverCert->source + serverCert->certBegin,
  30917. serverCert->sigIndex - serverCert->certBegin,
  30918. &verify, pubKey);
  30919. if (ret < 0 || verify != 1)
  30920. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30921. #endif /* HAVE_ED25519_VERIFY */
  30922. done:
  30923. if (tmp != NULL)
  30924. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30925. #ifdef HAVE_ED25519_VERIFY
  30926. wc_ed25519_free(pubKey);
  30927. #endif /* HAVE_ED25519_VERIFY */
  30928. if (caCert != NULL)
  30929. FreeDecodedCert(caCert);
  30930. if (serverCert != NULL)
  30931. FreeDecodedCert(serverCert);
  30932. return ret;
  30933. }
  30934. static wc_test_ret_t ed25519_test_make_cert(void)
  30935. {
  30936. WC_RNG rng;
  30937. Cert cert;
  30938. DecodedCert decode;
  30939. ed25519_key key;
  30940. ed25519_key* privKey = NULL;
  30941. wc_test_ret_t ret = 0;
  30942. byte* tmp = NULL;
  30943. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  30944. #ifndef HAVE_FIPS
  30945. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  30946. #else
  30947. ret = wc_InitRng(&rng);
  30948. #endif
  30949. if (ret != 0)
  30950. return WC_TEST_RET_ENC_EC(ret);
  30951. wc_ed25519_init(&key);
  30952. privKey = &key;
  30953. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, privKey);
  30954. cert.daysValid = 365 * 2;
  30955. cert.selfSigned = 1;
  30956. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  30957. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  30958. cert.isCA = 0;
  30959. #ifdef WOLFSSL_CERT_EXT
  30960. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  30961. if (ret < 0)
  30962. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30963. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  30964. if (ret < 0)
  30965. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30966. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  30967. if (ret < 0)
  30968. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30969. #endif
  30970. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30971. if (tmp == NULL) {
  30972. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  30973. }
  30974. cert.sigType = CTC_ED25519;
  30975. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED25519_TYPE, privKey, &rng);
  30976. if (ret < 0)
  30977. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30978. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF,
  30979. ED25519_TYPE, privKey, &rng);
  30980. if (ret < 0)
  30981. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30982. InitDecodedCert(&decode, tmp, (word32)ret, HEAP_HINT);
  30983. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  30984. FreeDecodedCert(&decode);
  30985. if (ret != 0)
  30986. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30987. done:
  30988. if (tmp != NULL)
  30989. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30990. wc_ed25519_free(privKey);
  30991. wc_FreeRng(&rng);
  30992. return ret;
  30993. }
  30994. #endif /* WOLFSSL_TEST_CERT */
  30995. #if defined(HAVE_ED25519_KEY_IMPORT)
  30996. static wc_test_ret_t ed25519_test_check_key(void)
  30997. {
  30998. /* Fails to find x-ordinate from this y-ordinate. */
  30999. WOLFSSL_SMALL_STACK_STATIC const byte key_bad_y[] = {
  31000. 0x40,
  31001. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31002. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31003. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31004. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31005. };
  31006. /* Y-ordinate value larger than prime. */
  31007. WOLFSSL_SMALL_STACK_STATIC const byte key_bad_y_max[] = {
  31008. 0x40,
  31009. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31010. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31011. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31012. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  31013. };
  31014. /* Y-ordinate value equal to prime. */
  31015. WOLFSSL_SMALL_STACK_STATIC const byte key_bad_y_is_p[] = {
  31016. 0x40,
  31017. 0xed,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31018. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31019. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31020. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  31021. };
  31022. /* Y-ordinate value equal to prime - 1. */
  31023. WOLFSSL_SMALL_STACK_STATIC const byte key_y_is_p_minus_1[] = {
  31024. 0x40,
  31025. 0xec,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31026. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31027. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31028. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  31029. };
  31030. ed25519_key key;
  31031. int ret;
  31032. int res = 0;
  31033. /* Initialize key for use. */
  31034. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  31035. if (ret != 0) {
  31036. return WC_TEST_RET_ENC_NC;
  31037. }
  31038. /* Load bad public key only and perform checks. */
  31039. ret = wc_ed25519_import_public(key_bad_y, ED25519_PUB_KEY_SIZE + 1, &key);
  31040. if (ret != PUBLIC_KEY_E) {
  31041. res = WC_TEST_RET_ENC_NC;
  31042. }
  31043. if (res == 0) {
  31044. /* Load bad public key only and perform checks. */
  31045. ret = wc_ed25519_import_public(key_bad_y_max, ED25519_PUB_KEY_SIZE + 1,
  31046. &key);
  31047. if (ret != PUBLIC_KEY_E) {
  31048. res = WC_TEST_RET_ENC_NC;
  31049. }
  31050. }
  31051. if (res == 0) {
  31052. /* Load bad public key only and perform checks. */
  31053. ret = wc_ed25519_import_public(key_bad_y_is_p, ED25519_PUB_KEY_SIZE + 1,
  31054. &key);
  31055. if (ret != PUBLIC_KEY_E) {
  31056. res = WC_TEST_RET_ENC_NC;
  31057. }
  31058. }
  31059. if (res == 0) {
  31060. /* Load good public key only and perform checks. */
  31061. ret = wc_ed25519_import_public(key_y_is_p_minus_1,
  31062. ED25519_PUB_KEY_SIZE + 1, &key);
  31063. if (ret != 0) {
  31064. res = WC_TEST_RET_ENC_NC;
  31065. }
  31066. }
  31067. /* Dispose of key. */
  31068. wc_ed25519_free(&key);
  31069. return res;
  31070. }
  31071. #endif
  31072. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  31073. defined(HAVE_ED25519_KEY_IMPORT)
  31074. static wc_test_ret_t ed25519ctx_test(void)
  31075. {
  31076. wc_test_ret_t ret;
  31077. byte out[ED25519_SIG_SIZE];
  31078. word32 outlen;
  31079. #ifdef HAVE_ED25519_VERIFY
  31080. int verify = 0;
  31081. #endif /* HAVE_ED25519_VERIFY */
  31082. ed25519_key key;
  31083. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  31084. 0x03,0x05,0x33,0x4e,0x38,0x1a,0xf7,0x8f,
  31085. 0x14,0x1c,0xb6,0x66,0xf6,0x19,0x9f,0x57,
  31086. 0xbc,0x34,0x95,0x33,0x5a,0x25,0x6a,0x95,
  31087. 0xbd,0x2a,0x55,0xbf,0x54,0x66,0x63,0xf6
  31088. };
  31089. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  31090. 0xdf,0xc9,0x42,0x5e,0x4f,0x96,0x8f,0x7f,
  31091. 0x0c,0x29,0xf0,0x25,0x9c,0xf5,0xf9,0xae,
  31092. 0xd6,0x85,0x1c,0x2b,0xb4,0xad,0x8b,0xfb,
  31093. 0x86,0x0c,0xfe,0xe0,0xab,0x24,0x82,0x92
  31094. };
  31095. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx1[] = {
  31096. 0x55,0xa4,0xcc,0x2f,0x70,0xa5,0x4e,0x04,
  31097. 0x28,0x8c,0x5f,0x4c,0xd1,0xe4,0x5a,0x7b,
  31098. 0xb5,0x20,0xb3,0x62,0x92,0x91,0x18,0x76,
  31099. 0xca,0xda,0x73,0x23,0x19,0x8d,0xd8,0x7a,
  31100. 0x8b,0x36,0x95,0x0b,0x95,0x13,0x00,0x22,
  31101. 0x90,0x7a,0x7f,0xb7,0xc4,0xe9,0xb2,0xd5,
  31102. 0xf6,0xcc,0xa6,0x85,0xa5,0x87,0xb4,0xb2,
  31103. 0x1f,0x4b,0x88,0x8e,0x4e,0x7e,0xdb,0x0d
  31104. };
  31105. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx2[] = {
  31106. 0xcc,0x5e,0x63,0xa2,0x7e,0x94,0xaf,0xd3,
  31107. 0x41,0x83,0x38,0xd2,0x48,0x6f,0xa9,0x2a,
  31108. 0xf9,0x91,0x7c,0x2d,0x98,0x9e,0x06,0xe5,
  31109. 0x02,0x77,0x72,0x1c,0x34,0x38,0x18,0xb4,
  31110. 0x21,0x96,0xbc,0x29,0x2e,0x68,0xf3,0x4d,
  31111. 0x85,0x9b,0xbe,0xad,0x17,0x9f,0x54,0x54,
  31112. 0x2d,0x4b,0x04,0xdc,0xfb,0xfa,0x4a,0x68,
  31113. 0x4e,0x39,0x50,0xfb,0x1c,0xcd,0x8d,0x0d
  31114. };
  31115. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  31116. 0xf7,0x26,0x93,0x6d,0x19,0xc8,0x00,0x49,
  31117. 0x4e,0x3f,0xda,0xff,0x20,0xb2,0x76,0xa8
  31118. };
  31119. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  31120. 0x66,0x6f,0x6f
  31121. };
  31122. outlen = sizeof(out);
  31123. XMEMSET(out, 0, sizeof(out));
  31124. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  31125. if (ret != 0)
  31126. return 10800;
  31127. ret = wc_ed25519_import_private_key(sKeyCtx, ED25519_KEY_SIZE, pKeyCtx,
  31128. sizeof(pKeyCtx), &key);
  31129. if (ret == 0)
  31130. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  31131. contextCtx, sizeof(contextCtx));
  31132. if (ret == 0 && XMEMCMP(out, sigCtx1, 64) != 0)
  31133. ret = WC_TEST_RET_ENC_NC;
  31134. #if defined(HAVE_ED25519_VERIFY)
  31135. /* test verify on good msg */
  31136. if (ret == 0)
  31137. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  31138. &verify, &key, contextCtx, sizeof(contextCtx));
  31139. if (ret == 0 && verify != 1)
  31140. ret = WC_TEST_RET_ENC_NC;
  31141. #endif
  31142. if (ret == 0)
  31143. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  31144. NULL, 0);
  31145. if (ret == 0 && XMEMCMP(out, sigCtx2, 64) != 0)
  31146. ret = WC_TEST_RET_ENC_NC;
  31147. #if defined(HAVE_ED25519_VERIFY)
  31148. /* test verify on good msg */
  31149. if (ret == 0)
  31150. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  31151. &verify, &key, NULL, 0);
  31152. if (ret == 0 && verify != 1)
  31153. ret = WC_TEST_RET_ENC_NC;
  31154. #endif
  31155. wc_ed25519_free(&key);
  31156. return ret;
  31157. }
  31158. static wc_test_ret_t ed25519ph_test(void)
  31159. {
  31160. wc_test_ret_t ret = 0;
  31161. byte out[ED25519_SIG_SIZE];
  31162. word32 outlen;
  31163. #ifdef HAVE_ED25519_VERIFY
  31164. int verify = 0;
  31165. #endif /* HAVE_ED25519_VERIFY */
  31166. ed25519_key key;
  31167. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  31168. 0x83,0x3f,0xe6,0x24,0x09,0x23,0x7b,0x9d,
  31169. 0x62,0xec,0x77,0x58,0x75,0x20,0x91,0x1e,
  31170. 0x9a,0x75,0x9c,0xec,0x1d,0x19,0x75,0x5b,
  31171. 0x7d,0xa9,0x01,0xb9,0x6d,0xca,0x3d,0x42
  31172. };
  31173. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  31174. 0xec,0x17,0x2b,0x93,0xad,0x5e,0x56,0x3b,
  31175. 0xf4,0x93,0x2c,0x70,0xe1,0x24,0x50,0x34,
  31176. 0xc3,0x54,0x67,0xef,0x2e,0xfd,0x4d,0x64,
  31177. 0xeb,0xf8,0x19,0x68,0x34,0x67,0xe2,0xbf
  31178. };
  31179. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  31180. 0x98,0xa7,0x02,0x22,0xf0,0xb8,0x12,0x1a,
  31181. 0xa9,0xd3,0x0f,0x81,0x3d,0x68,0x3f,0x80,
  31182. 0x9e,0x46,0x2b,0x46,0x9c,0x7f,0xf8,0x76,
  31183. 0x39,0x49,0x9b,0xb9,0x4e,0x6d,0xae,0x41,
  31184. 0x31,0xf8,0x50,0x42,0x46,0x3c,0x2a,0x35,
  31185. 0x5a,0x20,0x03,0xd0,0x62,0xad,0xf5,0xaa,
  31186. 0xa1,0x0b,0x8c,0x61,0xe6,0x36,0x06,0x2a,
  31187. 0xaa,0xd1,0x1c,0x2a,0x26,0x08,0x34,0x06
  31188. };
  31189. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  31190. 0xe0,0x39,0x70,0x2b,0x4c,0x25,0x95,0xa6,
  31191. 0xa5,0x41,0xac,0x85,0x09,0x23,0x6e,0x29,
  31192. 0x90,0x47,0x47,0x95,0x33,0x0c,0x9b,0x34,
  31193. 0xa7,0x5f,0x58,0xa6,0x60,0x12,0x9e,0x08,
  31194. 0xfd,0x73,0x69,0x43,0xfb,0x19,0x43,0xa5,
  31195. 0x57,0x20,0xb9,0xe0,0x95,0x7b,0x1e,0xd6,
  31196. 0x73,0x48,0x16,0x61,0x9f,0x13,0x88,0xf4,
  31197. 0x3f,0x73,0xe6,0xe3,0xba,0xa8,0x1c,0x0e
  31198. };
  31199. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  31200. 0x61,0x62,0x63
  31201. };
  31202. /* SHA-512 hash of msgPh */
  31203. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  31204. 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
  31205. 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
  31206. 0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,
  31207. 0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
  31208. 0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,
  31209. 0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
  31210. 0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,
  31211. 0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f
  31212. };
  31213. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  31214. 0x66,0x6f,0x6f
  31215. };
  31216. outlen = sizeof(out);
  31217. XMEMSET(out, 0, sizeof(out));
  31218. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  31219. if (ret != 0)
  31220. return WC_TEST_RET_ENC_EC(ret);
  31221. ret = wc_ed25519_import_private_key(sKeyPh, ED25519_KEY_SIZE, pKeyPh,
  31222. sizeof(pKeyPh), &key);
  31223. if (ret == 0)
  31224. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  31225. NULL, 0);
  31226. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  31227. ret = WC_TEST_RET_ENC_NC;
  31228. #if defined(HAVE_ED25519_VERIFY)
  31229. /* test verify on good msg */
  31230. if (ret == 0)
  31231. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh),
  31232. &verify, &key, NULL, 0);
  31233. if (ret == 0 && verify != 1)
  31234. ret = WC_TEST_RET_ENC_NC;
  31235. #endif
  31236. if (ret == 0)
  31237. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  31238. contextPh2, sizeof(contextPh2));
  31239. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  31240. ret = WC_TEST_RET_ENC_NC;
  31241. #if defined(HAVE_ED25519_VERIFY)
  31242. /* test verify on good msg */
  31243. if (ret == 0)
  31244. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify,
  31245. &key, contextPh2, sizeof(contextPh2));
  31246. if (ret == 0 && verify != 1)
  31247. ret = WC_TEST_RET_ENC_NC;
  31248. #endif
  31249. if (ret == 0)
  31250. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  31251. NULL, 0);
  31252. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  31253. ret = WC_TEST_RET_ENC_NC;
  31254. #if defined(HAVE_ED25519_VERIFY)
  31255. if (ret == 0)
  31256. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh),
  31257. &verify, &key, NULL, 0);
  31258. if (ret == 0 && verify != 1)
  31259. ret = WC_TEST_RET_ENC_NC;
  31260. #endif
  31261. if (ret == 0)
  31262. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  31263. contextPh2, sizeof(contextPh2));
  31264. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  31265. ret = WC_TEST_RET_ENC_NC;
  31266. #if defined(HAVE_ED25519_VERIFY)
  31267. if (ret == 0)
  31268. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  31269. &key, contextPh2, sizeof(contextPh2));
  31270. if (ret == 0 && verify != 1)
  31271. ret = WC_TEST_RET_ENC_NC;
  31272. #endif
  31273. wc_ed25519_free(&key);
  31274. return ret;
  31275. }
  31276. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  31277. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed25519_test(void)
  31278. {
  31279. wc_test_ret_t ret;
  31280. WC_RNG rng;
  31281. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  31282. defined(HAVE_ED25519_KEY_IMPORT)
  31283. byte out[ED25519_SIG_SIZE];
  31284. byte exportPKey[ED25519_KEY_SIZE];
  31285. byte exportSKey[ED25519_KEY_SIZE];
  31286. word32 exportPSz;
  31287. word32 exportSSz;
  31288. int i;
  31289. word32 outlen;
  31290. #ifdef HAVE_ED25519_VERIFY
  31291. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  31292. int j;
  31293. #endif
  31294. int verify;
  31295. #endif /* HAVE_ED25519_VERIFY */
  31296. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  31297. word32 keySz, sigSz;
  31298. ed25519_key key;
  31299. ed25519_key key2;
  31300. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  31301. defined(HAVE_ED25519_KEY_IMPORT)
  31302. /* test vectors from
  31303. https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-02
  31304. */
  31305. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  31306. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  31307. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  31308. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  31309. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  31310. };
  31311. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  31312. 0x4c,0xcd,0x08,0x9b,0x28,0xff,0x96,0xda,
  31313. 0x9d,0xb6,0xc3,0x46,0xec,0x11,0x4e,0x0f,
  31314. 0x5b,0x8a,0x31,0x9f,0x35,0xab,0xa6,0x24,
  31315. 0xda,0x8c,0xf6,0xed,0x4f,0xb8,0xa6,0xfb
  31316. };
  31317. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  31318. 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
  31319. 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
  31320. 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
  31321. 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
  31322. };
  31323. /* uncompressed test */
  31324. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  31325. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  31326. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  31327. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  31328. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  31329. };
  31330. /* compressed prefix test */
  31331. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  31332. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  31333. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  31334. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  31335. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  31336. };
  31337. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  31338. 0xf5,0xe5,0x76,0x7c,0xf1,0x53,0x31,0x95,
  31339. 0x17,0x63,0x0f,0x22,0x68,0x76,0xb8,0x6c,
  31340. 0x81,0x60,0xcc,0x58,0x3b,0xc0,0x13,0x74,
  31341. 0x4c,0x6b,0xf2,0x55,0xf5,0xcc,0x0e,0xe5
  31342. };
  31343. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  31344. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  31345. 0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  31346. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  31347. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  31348. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  31349. };
  31350. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  31351. 0x3d,0x40,0x17,0xc3,0xe8,0x43,0x89,0x5a,
  31352. 0x92,0xb7,0x0a,0xa7,0x4d,0x1b,0x7e,0xbc,
  31353. 0x9c,0x98,0x2c,0xcf,0x2e,0xc4,0x96,0x8c,
  31354. 0xc0,0xcd,0x55,0xf1,0x2a,0xf4,0x66,0x0c
  31355. };
  31356. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  31357. 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
  31358. 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
  31359. 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
  31360. 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
  31361. };
  31362. /* uncompressed test */
  31363. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  31364. 0x04,0x55,0xd0,0xe0,0x9a,0x2b,0x9d,0x34,
  31365. 0x29,0x22,0x97,0xe0,0x8d,0x60,0xd0,0xf6,
  31366. 0x20,0xc5,0x13,0xd4,0x72,0x53,0x18,0x7c,
  31367. 0x24,0xb1,0x27,0x86,0xbd,0x77,0x76,0x45,
  31368. 0xce,0x1a,0x51,0x07,0xf7,0x68,0x1a,0x02,
  31369. 0xaf,0x25,0x23,0xa6,0xda,0xf3,0x72,0xe1,
  31370. 0x0e,0x3a,0x07,0x64,0xc9,0xd3,0xfe,0x4b,
  31371. 0xd5,0xb7,0x0a,0xb1,0x82,0x01,0x98,0x5a,
  31372. 0xd7
  31373. };
  31374. /* compressed prefix */
  31375. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  31376. 0x40,0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  31377. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  31378. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  31379. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  31380. };
  31381. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  31382. 0x27,0x81,0x17,0xfc,0x14,0x4c,0x72,0x34,
  31383. 0x0f,0x67,0xd0,0xf2,0x31,0x6e,0x83,0x86,
  31384. 0xce,0xff,0xbf,0x2b,0x24,0x28,0xc9,0xc5,
  31385. 0x1f,0xef,0x7c,0x59,0x7f,0x1d,0x42,0x6e
  31386. };
  31387. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  31388. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  31389. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  31390. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  31391. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  31392. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  31393. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  31394. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  31395. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  31396. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  31397. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  31398. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  31399. };
  31400. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  31401. 0x92,0xa0,0x09,0xa9,0xf0,0xd4,0xca,0xb8,
  31402. 0x72,0x0e,0x82,0x0b,0x5f,0x64,0x25,0x40,
  31403. 0xa2,0xb2,0x7b,0x54,0x16,0x50,0x3f,0x8f,
  31404. 0xb3,0x76,0x22,0x23,0xeb,0xdb,0x69,0xda,
  31405. 0x08,0x5a,0xc1,0xe4,0x3e,0x15,0x99,0x6e,
  31406. 0x45,0x8f,0x36,0x13,0xd0,0xf1,0x1d,0x8c,
  31407. 0x38,0x7b,0x2e,0xae,0xb4,0x30,0x2a,0xee,
  31408. 0xb0,0x0d,0x29,0x16,0x12,0xbb,0x0c,0x00
  31409. };
  31410. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  31411. 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
  31412. 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
  31413. 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
  31414. 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
  31415. 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
  31416. 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
  31417. 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
  31418. 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
  31419. };
  31420. /* uncompressed test */
  31421. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  31422. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  31423. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  31424. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  31425. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  31426. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  31427. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  31428. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  31429. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  31430. };
  31431. /* compressed prefix */
  31432. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  31433. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  31434. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  31435. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  31436. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  31437. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  31438. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  31439. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  31440. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  31441. };
  31442. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  31443. 0x0a,0xab,0x4c,0x90,0x05,0x01,0xb3,0xe2,
  31444. 0x4d,0x7c,0xdf,0x46,0x63,0x32,0x6a,0x3a,
  31445. 0x87,0xdf,0x5e,0x48,0x43,0xb2,0xcb,0xdb,
  31446. 0x67,0xcb,0xf6,0xe4,0x60,0xfe,0xc3,0x50,
  31447. 0xaa,0x53,0x71,0xb1,0x50,0x8f,0x9f,0x45,
  31448. 0x28,0xec,0xea,0x23,0xc4,0x36,0xd9,0x4b,
  31449. 0x5e,0x8f,0xcd,0x4f,0x68,0x1e,0x30,0xa6,
  31450. 0xac,0x00,0xa9,0x70,0x4a,0x18,0x8a,0x03
  31451. };
  31452. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  31453. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = {0x0 };
  31454. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = {0x72};
  31455. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = {0xAF,0x82};
  31456. /* test of a 1024 byte long message */
  31457. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  31458. 0x08,0xb8,0xb2,0xb7,0x33,0x42,0x42,0x43,
  31459. 0x76,0x0f,0xe4,0x26,0xa4,0xb5,0x49,0x08,
  31460. 0x63,0x21,0x10,0xa6,0x6c,0x2f,0x65,0x91,
  31461. 0xea,0xbd,0x33,0x45,0xe3,0xe4,0xeb,0x98,
  31462. 0xfa,0x6e,0x26,0x4b,0xf0,0x9e,0xfe,0x12,
  31463. 0xee,0x50,0xf8,0xf5,0x4e,0x9f,0x77,0xb1,
  31464. 0xe3,0x55,0xf6,0xc5,0x05,0x44,0xe2,0x3f,
  31465. 0xb1,0x43,0x3d,0xdf,0x73,0xbe,0x84,0xd8,
  31466. 0x79,0xde,0x7c,0x00,0x46,0xdc,0x49,0x96,
  31467. 0xd9,0xe7,0x73,0xf4,0xbc,0x9e,0xfe,0x57,
  31468. 0x38,0x82,0x9a,0xdb,0x26,0xc8,0x1b,0x37,
  31469. 0xc9,0x3a,0x1b,0x27,0x0b,0x20,0x32,0x9d,
  31470. 0x65,0x86,0x75,0xfc,0x6e,0xa5,0x34,0xe0,
  31471. 0x81,0x0a,0x44,0x32,0x82,0x6b,0xf5,0x8c,
  31472. 0x94,0x1e,0xfb,0x65,0xd5,0x7a,0x33,0x8b,
  31473. 0xbd,0x2e,0x26,0x64,0x0f,0x89,0xff,0xbc,
  31474. 0x1a,0x85,0x8e,0xfc,0xb8,0x55,0x0e,0xe3,
  31475. 0xa5,0xe1,0x99,0x8b,0xd1,0x77,0xe9,0x3a,
  31476. 0x73,0x63,0xc3,0x44,0xfe,0x6b,0x19,0x9e,
  31477. 0xe5,0xd0,0x2e,0x82,0xd5,0x22,0xc4,0xfe,
  31478. 0xba,0x15,0x45,0x2f,0x80,0x28,0x8a,0x82,
  31479. 0x1a,0x57,0x91,0x16,0xec,0x6d,0xad,0x2b,
  31480. 0x3b,0x31,0x0d,0xa9,0x03,0x40,0x1a,0xa6,
  31481. 0x21,0x00,0xab,0x5d,0x1a,0x36,0x55,0x3e,
  31482. 0x06,0x20,0x3b,0x33,0x89,0x0c,0xc9,0xb8,
  31483. 0x32,0xf7,0x9e,0xf8,0x05,0x60,0xcc,0xb9,
  31484. 0xa3,0x9c,0xe7,0x67,0x96,0x7e,0xd6,0x28,
  31485. 0xc6,0xad,0x57,0x3c,0xb1,0x16,0xdb,0xef,
  31486. 0xef,0xd7,0x54,0x99,0xda,0x96,0xbd,0x68,
  31487. 0xa8,0xa9,0x7b,0x92,0x8a,0x8b,0xbc,0x10,
  31488. 0x3b,0x66,0x21,0xfc,0xde,0x2b,0xec,0xa1,
  31489. 0x23,0x1d,0x20,0x6b,0xe6,0xcd,0x9e,0xc7,
  31490. 0xaf,0xf6,0xf6,0xc9,0x4f,0xcd,0x72,0x04,
  31491. 0xed,0x34,0x55,0xc6,0x8c,0x83,0xf4,0xa4,
  31492. 0x1d,0xa4,0xaf,0x2b,0x74,0xef,0x5c,0x53,
  31493. 0xf1,0xd8,0xac,0x70,0xbd,0xcb,0x7e,0xd1,
  31494. 0x85,0xce,0x81,0xbd,0x84,0x35,0x9d,0x44,
  31495. 0x25,0x4d,0x95,0x62,0x9e,0x98,0x55,0xa9,
  31496. 0x4a,0x7c,0x19,0x58,0xd1,0xf8,0xad,0xa5,
  31497. 0xd0,0x53,0x2e,0xd8,0xa5,0xaa,0x3f,0xb2,
  31498. 0xd1,0x7b,0xa7,0x0e,0xb6,0x24,0x8e,0x59,
  31499. 0x4e,0x1a,0x22,0x97,0xac,0xbb,0xb3,0x9d,
  31500. 0x50,0x2f,0x1a,0x8c,0x6e,0xb6,0xf1,0xce,
  31501. 0x22,0xb3,0xde,0x1a,0x1f,0x40,0xcc,0x24,
  31502. 0x55,0x41,0x19,0xa8,0x31,0xa9,0xaa,0xd6,
  31503. 0x07,0x9c,0xad,0x88,0x42,0x5d,0xe6,0xbd,
  31504. 0xe1,0xa9,0x18,0x7e,0xbb,0x60,0x92,0xcf,
  31505. 0x67,0xbf,0x2b,0x13,0xfd,0x65,0xf2,0x70,
  31506. 0x88,0xd7,0x8b,0x7e,0x88,0x3c,0x87,0x59,
  31507. 0xd2,0xc4,0xf5,0xc6,0x5a,0xdb,0x75,0x53,
  31508. 0x87,0x8a,0xd5,0x75,0xf9,0xfa,0xd8,0x78,
  31509. 0xe8,0x0a,0x0c,0x9b,0xa6,0x3b,0xcb,0xcc,
  31510. 0x27,0x32,0xe6,0x94,0x85,0xbb,0xc9,0xc9,
  31511. 0x0b,0xfb,0xd6,0x24,0x81,0xd9,0x08,0x9b,
  31512. 0xec,0xcf,0x80,0xcf,0xe2,0xdf,0x16,0xa2,
  31513. 0xcf,0x65,0xbd,0x92,0xdd,0x59,0x7b,0x07,
  31514. 0x07,0xe0,0x91,0x7a,0xf4,0x8b,0xbb,0x75,
  31515. 0xfe,0xd4,0x13,0xd2,0x38,0xf5,0x55,0x5a,
  31516. 0x7a,0x56,0x9d,0x80,0xc3,0x41,0x4a,0x8d,
  31517. 0x08,0x59,0xdc,0x65,0xa4,0x61,0x28,0xba,
  31518. 0xb2,0x7a,0xf8,0x7a,0x71,0x31,0x4f,0x31,
  31519. 0x8c,0x78,0x2b,0x23,0xeb,0xfe,0x80,0x8b,
  31520. 0x82,0xb0,0xce,0x26,0x40,0x1d,0x2e,0x22,
  31521. 0xf0,0x4d,0x83,0xd1,0x25,0x5d,0xc5,0x1a,
  31522. 0xdd,0xd3,0xb7,0x5a,0x2b,0x1a,0xe0,0x78,
  31523. 0x45,0x04,0xdf,0x54,0x3a,0xf8,0x96,0x9b,
  31524. 0xe3,0xea,0x70,0x82,0xff,0x7f,0xc9,0x88,
  31525. 0x8c,0x14,0x4d,0xa2,0xaf,0x58,0x42,0x9e,
  31526. 0xc9,0x60,0x31,0xdb,0xca,0xd3,0xda,0xd9,
  31527. 0xaf,0x0d,0xcb,0xaa,0xaf,0x26,0x8c,0xb8,
  31528. 0xfc,0xff,0xea,0xd9,0x4f,0x3c,0x7c,0xa4,
  31529. 0x95,0xe0,0x56,0xa9,0xb4,0x7a,0xcd,0xb7,
  31530. 0x51,0xfb,0x73,0xe6,0x66,0xc6,0xc6,0x55,
  31531. 0xad,0xe8,0x29,0x72,0x97,0xd0,0x7a,0xd1,
  31532. 0xba,0x5e,0x43,0xf1,0xbc,0xa3,0x23,0x01,
  31533. 0x65,0x13,0x39,0xe2,0x29,0x04,0xcc,0x8c,
  31534. 0x42,0xf5,0x8c,0x30,0xc0,0x4a,0xaf,0xdb,
  31535. 0x03,0x8d,0xda,0x08,0x47,0xdd,0x98,0x8d,
  31536. 0xcd,0xa6,0xf3,0xbf,0xd1,0x5c,0x4b,0x4c,
  31537. 0x45,0x25,0x00,0x4a,0xa0,0x6e,0xef,0xf8,
  31538. 0xca,0x61,0x78,0x3a,0xac,0xec,0x57,0xfb,
  31539. 0x3d,0x1f,0x92,0xb0,0xfe,0x2f,0xd1,0xa8,
  31540. 0x5f,0x67,0x24,0x51,0x7b,0x65,0xe6,0x14,
  31541. 0xad,0x68,0x08,0xd6,0xf6,0xee,0x34,0xdf,
  31542. 0xf7,0x31,0x0f,0xdc,0x82,0xae,0xbf,0xd9,
  31543. 0x04,0xb0,0x1e,0x1d,0xc5,0x4b,0x29,0x27,
  31544. 0x09,0x4b,0x2d,0xb6,0x8d,0x6f,0x90,0x3b,
  31545. 0x68,0x40,0x1a,0xde,0xbf,0x5a,0x7e,0x08,
  31546. 0xd7,0x8f,0xf4,0xef,0x5d,0x63,0x65,0x3a,
  31547. 0x65,0x04,0x0c,0xf9,0xbf,0xd4,0xac,0xa7,
  31548. 0x98,0x4a,0x74,0xd3,0x71,0x45,0x98,0x67,
  31549. 0x80,0xfc,0x0b,0x16,0xac,0x45,0x16,0x49,
  31550. 0xde,0x61,0x88,0xa7,0xdb,0xdf,0x19,0x1f,
  31551. 0x64,0xb5,0xfc,0x5e,0x2a,0xb4,0x7b,0x57,
  31552. 0xf7,0xf7,0x27,0x6c,0xd4,0x19,0xc1,0x7a,
  31553. 0x3c,0xa8,0xe1,0xb9,0x39,0xae,0x49,0xe4,
  31554. 0x88,0xac,0xba,0x6b,0x96,0x56,0x10,0xb5,
  31555. 0x48,0x01,0x09,0xc8,0xb1,0x7b,0x80,0xe1,
  31556. 0xb7,0xb7,0x50,0xdf,0xc7,0x59,0x8d,0x5d,
  31557. 0x50,0x11,0xfd,0x2d,0xcc,0x56,0x00,0xa3,
  31558. 0x2e,0xf5,0xb5,0x2a,0x1e,0xcc,0x82,0x0e,
  31559. 0x30,0x8a,0xa3,0x42,0x72,0x1a,0xac,0x09,
  31560. 0x43,0xbf,0x66,0x86,0xb6,0x4b,0x25,0x79,
  31561. 0x37,0x65,0x04,0xcc,0xc4,0x93,0xd9,0x7e,
  31562. 0x6a,0xed,0x3f,0xb0,0xf9,0xcd,0x71,0xa4,
  31563. 0x3d,0xd4,0x97,0xf0,0x1f,0x17,0xc0,0xe2,
  31564. 0xcb,0x37,0x97,0xaa,0x2a,0x2f,0x25,0x66,
  31565. 0x56,0x16,0x8e,0x6c,0x49,0x6a,0xfc,0x5f,
  31566. 0xb9,0x32,0x46,0xf6,0xb1,0x11,0x63,0x98,
  31567. 0xa3,0x46,0xf1,0xa6,0x41,0xf3,0xb0,0x41,
  31568. 0xe9,0x89,0xf7,0x91,0x4f,0x90,0xcc,0x2c,
  31569. 0x7f,0xff,0x35,0x78,0x76,0xe5,0x06,0xb5,
  31570. 0x0d,0x33,0x4b,0xa7,0x7c,0x22,0x5b,0xc3,
  31571. 0x07,0xba,0x53,0x71,0x52,0xf3,0xf1,0x61,
  31572. 0x0e,0x4e,0xaf,0xe5,0x95,0xf6,0xd9,0xd9,
  31573. 0x0d,0x11,0xfa,0xa9,0x33,0xa1,0x5e,0xf1,
  31574. 0x36,0x95,0x46,0x86,0x8a,0x7f,0x3a,0x45,
  31575. 0xa9,0x67,0x68,0xd4,0x0f,0xd9,0xd0,0x34,
  31576. 0x12,0xc0,0x91,0xc6,0x31,0x5c,0xf4,0xfd,
  31577. 0xe7,0xcb,0x68,0x60,0x69,0x37,0x38,0x0d,
  31578. 0xb2,0xea,0xaa,0x70,0x7b,0x4c,0x41,0x85,
  31579. 0xc3,0x2e,0xdd,0xcd,0xd3,0x06,0x70,0x5e,
  31580. 0x4d,0xc1,0xff,0xc8,0x72,0xee,0xee,0x47,
  31581. 0x5a,0x64,0xdf,0xac,0x86,0xab,0xa4,0x1c,
  31582. 0x06,0x18,0x98,0x3f,0x87,0x41,0xc5,0xef,
  31583. 0x68,0xd3,0xa1,0x01,0xe8,0xa3,0xb8,0xca,
  31584. 0xc6,0x0c,0x90,0x5c,0x15,0xfc,0x91,0x08,
  31585. 0x40,0xb9,0x4c,0x00,0xa0,0xb9,0xd0
  31586. };
  31587. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  31588. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  31589. sizeof(msg2),
  31590. sizeof(msg3),
  31591. 0 /*sizeof(msg1)*/,
  31592. 0 /*sizeof(msg1)*/,
  31593. sizeof(msg4)
  31594. };
  31595. #ifndef NO_ASN
  31596. static const byte privateEd25519[] = {
  31597. 0x30,0x2e,0x02,0x01,0x00,0x30,0x05,0x06,
  31598. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  31599. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  31600. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  31601. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  31602. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  31603. };
  31604. static const byte badPrivateEd25519[] = {
  31605. 0x30,0x52,0x02,0x01,0x00,0x30,0x05,0x06,
  31606. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  31607. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  31608. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  31609. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  31610. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  31611. 0xa1,0x22,0x04,0x21,0xd7,0x5a,0x98,0x01, /* octet len 0x20 -> 0x21 */
  31612. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  31613. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  31614. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  31615. 0xf7,0x07,0x51,0x1a,
  31616. 0x00 /* add additional bytes to make the pubkey bigger */
  31617. };
  31618. static const byte publicEd25519[] = {
  31619. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  31620. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  31621. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  31622. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  31623. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  31624. 0xf7,0x07,0x51,0x1a
  31625. };
  31626. /* size has been altered to catch if sanity check is done */
  31627. static const byte badPublicEd25519[] = {
  31628. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  31629. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  31630. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  31631. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  31632. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  31633. 0xf7,0x07,0x51,0x1a,
  31634. 0x00 /* add an additional byte to make the pubkey appear bigger */
  31635. };
  31636. static const byte privPubEd25519[] = {
  31637. 0x30,0x50,0x02,0x01,0x00,0x30,0x05,0x06,
  31638. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  31639. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  31640. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  31641. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  31642. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  31643. 0x81,0x20,0xd7,0x5a,0x98,0x01,0x82,0xb1,
  31644. 0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,0xc9,0x64,
  31645. 0x07,0x3a,0x0e,0xe1,0x72,0xf3,0xda,0xa6,
  31646. 0x23,0x25,0xaf,0x02,0x1a,0x68,0xf7,0x07,
  31647. 0x51,0x1a
  31648. };
  31649. word32 idx;
  31650. #endif /* NO_ASN */
  31651. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  31652. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  31653. ed25519_key key3;
  31654. #endif
  31655. WOLFSSL_ENTER("ed25519_test");
  31656. /* create ed25519 keys */
  31657. #ifndef HAVE_FIPS
  31658. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  31659. #else
  31660. ret = wc_InitRng(&rng);
  31661. #endif
  31662. if (ret != 0)
  31663. return WC_TEST_RET_ENC_EC(ret);
  31664. wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  31665. wc_ed25519_init_ex(&key2, HEAP_HINT, devId);
  31666. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  31667. wc_ed25519_init_ex(&key3, HEAP_HINT, devId);
  31668. #endif
  31669. #ifdef HAVE_ED25519_MAKE_KEY
  31670. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  31671. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key2);
  31672. #endif
  31673. /* helper functions for signature and key size */
  31674. keySz = (word32)wc_ed25519_size(&key);
  31675. sigSz = (word32)wc_ed25519_sig_size(&key);
  31676. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  31677. defined(HAVE_ED25519_KEY_IMPORT)
  31678. for (i = 0; i < 6; i++) {
  31679. outlen = sizeof(out);
  31680. XMEMSET(out, 0, sizeof(out));
  31681. if (wc_ed25519_import_private_key(sKeys[i], ED25519_KEY_SIZE, pKeys[i],
  31682. pKeySz[i], &key) != 0)
  31683. return WC_TEST_RET_ENC_I(i);
  31684. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key) != 0)
  31685. return WC_TEST_RET_ENC_I(i);
  31686. if (XMEMCMP(out, sigs[i], 64))
  31687. return WC_TEST_RET_ENC_I(i);
  31688. #if defined(HAVE_ED25519_VERIFY)
  31689. /* test verify on good msg */
  31690. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  31691. &key) != 0 || verify != 1)
  31692. return WC_TEST_RET_ENC_I(i);
  31693. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  31694. /* test verify on good msg using streaming interface directly */
  31695. if (wc_ed25519_verify_msg_init(out, outlen,
  31696. &key, (byte)Ed25519, NULL, 0) != 0)
  31697. return WC_TEST_RET_ENC_I(i);
  31698. for (j = 0; j < msgSz[i]; j += i) {
  31699. if (wc_ed25519_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), &key) != 0)
  31700. return WC_TEST_RET_ENC_I(i);
  31701. }
  31702. if (wc_ed25519_verify_msg_final(out, outlen, &verify,
  31703. &key) != 0 || verify != 1)
  31704. return WC_TEST_RET_ENC_I(i);
  31705. #endif /* WOLFSSL_ED25519_STREAMING_VERIFY */
  31706. /* test verify on bad msg */
  31707. out[outlen-1] = out[outlen-1] + 1;
  31708. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  31709. &key) == 0 || verify == 1)
  31710. return WC_TEST_RET_ENC_I(i);
  31711. #endif /* HAVE_ED25519_VERIFY */
  31712. /* test api for import/exporting keys */
  31713. exportPSz = sizeof(exportPKey);
  31714. exportSSz = sizeof(exportSKey);
  31715. if (wc_ed25519_export_public(&key, exportPKey, &exportPSz) != 0)
  31716. return WC_TEST_RET_ENC_I(i);
  31717. if (wc_ed25519_import_public_ex(exportPKey, exportPSz, &key2, 1) != 0)
  31718. return WC_TEST_RET_ENC_I(i);
  31719. if (wc_ed25519_export_private_only(&key, exportSKey, &exportSSz) != 0)
  31720. return WC_TEST_RET_ENC_I(i);
  31721. if (wc_ed25519_import_private_key(exportSKey, exportSSz,
  31722. exportPKey, exportPSz, &key2) != 0)
  31723. return WC_TEST_RET_ENC_I(i);
  31724. /* clear "out" buffer and test sign with imported keys */
  31725. outlen = sizeof(out);
  31726. XMEMSET(out, 0, sizeof(out));
  31727. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2) != 0)
  31728. return WC_TEST_RET_ENC_I(i);
  31729. #if defined(HAVE_ED25519_VERIFY)
  31730. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  31731. &key2) != 0 || verify != 1)
  31732. return WC_TEST_RET_ENC_I(i);
  31733. if (XMEMCMP(out, sigs[i], 64))
  31734. return WC_TEST_RET_ENC_I(i);
  31735. #endif /* HAVE_ED25519_VERIFY */
  31736. }
  31737. {
  31738. /* Run tests for some rare code paths */
  31739. /* sig is exactly equal to the order */
  31740. static const byte rareEd1[] = {
  31741. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31742. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31743. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31744. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31745. 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
  31746. 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
  31747. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31748. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10
  31749. };
  31750. /* sig is larger than the order before we get to the low part */
  31751. static const byte rareEd2[] = {
  31752. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31753. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31754. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31755. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31756. 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
  31757. 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
  31758. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31759. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x10
  31760. };
  31761. /* sig is larger than the order in the low part */
  31762. static const byte rareEd3[] = {
  31763. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31764. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31765. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31766. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31767. 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
  31768. 0xd6, 0x9c, 0xf9, 0xa2, 0xde, 0xf9, 0xde, 0x14,
  31769. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31770. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10
  31771. };
  31772. /* sig is smaller than the order */
  31773. static const byte rareEd4[] = {
  31774. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31775. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31776. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31777. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31778. 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
  31779. 0xd6, 0x9c, 0xf1, 0xa2, 0xde, 0xf9, 0xde, 0x14,
  31780. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31781. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10
  31782. };
  31783. ret = wc_ed25519_import_private_key(sKeys[0], ED25519_KEY_SIZE,
  31784. pKeys[0], pKeySz[0], &key);
  31785. if (ret != 0)
  31786. return ret;
  31787. ret = wc_ed25519_verify_msg(rareEd1, sizeof(rareEd1), msgs[0], msgSz[0],
  31788. &verify, &key);
  31789. if (ret != BAD_FUNC_ARG)
  31790. return ret;
  31791. ret = wc_ed25519_verify_msg(rareEd2, sizeof(rareEd2), msgs[0], msgSz[0],
  31792. &verify, &key);
  31793. if (ret != BAD_FUNC_ARG)
  31794. return ret;
  31795. ret = wc_ed25519_verify_msg(rareEd3, sizeof(rareEd3), msgs[0], msgSz[0],
  31796. &verify, &key);
  31797. if (ret != BAD_FUNC_ARG)
  31798. return ret;
  31799. ret = wc_ed25519_verify_msg(rareEd4, sizeof(rareEd4), msgs[0], msgSz[0],
  31800. &verify, &key);
  31801. if (ret != SIG_VERIFY_E)
  31802. return ret;
  31803. }
  31804. ret = ed25519ctx_test();
  31805. if (ret != 0)
  31806. return ret;
  31807. ret = ed25519ph_test();
  31808. if (ret != 0)
  31809. return ret;
  31810. #ifndef NO_ASN
  31811. /* Try ASN.1 encoded private-only key and public key. */
  31812. idx = 0;
  31813. ret = wc_Ed25519PrivateKeyDecode(privateEd25519, &idx, &key3,
  31814. sizeof(privateEd25519));
  31815. if (ret != 0)
  31816. return WC_TEST_RET_ENC_EC(ret);
  31817. idx = 0;
  31818. if (wc_Ed25519PrivateKeyDecode(badPrivateEd25519, &idx, &key3,
  31819. sizeof(badPrivateEd25519)) == 0)
  31820. return WC_TEST_RET_ENC_NC;
  31821. ret = wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3);
  31822. if (ret != BAD_FUNC_ARG)
  31823. return WC_TEST_RET_ENC_EC(ret);
  31824. /* try with a buffer size that is too large */
  31825. idx = 0;
  31826. if (wc_Ed25519PublicKeyDecode(badPublicEd25519, &idx, &key3,
  31827. sizeof(badPublicEd25519)) == 0)
  31828. return WC_TEST_RET_ENC_NC;
  31829. idx = 0;
  31830. ret = wc_Ed25519PublicKeyDecode(publicEd25519, &idx, &key3,
  31831. sizeof(publicEd25519));
  31832. if (ret != 0)
  31833. return WC_TEST_RET_ENC_EC(ret);
  31834. ret = wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3);
  31835. if (ret != 0)
  31836. return WC_TEST_RET_ENC_EC(ret);
  31837. if (XMEMCMP(out, sigs[0], 64))
  31838. return WC_TEST_RET_ENC_NC;
  31839. #if defined(HAVE_ED25519_VERIFY)
  31840. /* test verify on good msg */
  31841. ret = wc_ed25519_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3);
  31842. if (ret != 0 || verify != 1)
  31843. return WC_TEST_RET_ENC_EC(ret);
  31844. #endif /* HAVE_ED25519_VERIFY */
  31845. wc_ed25519_free(&key3);
  31846. wc_ed25519_init(&key3);
  31847. idx = 0;
  31848. ret = wc_Ed25519PrivateKeyDecode(privPubEd25519, &idx, &key3,
  31849. sizeof(privPubEd25519));
  31850. if (ret != 0)
  31851. return WC_TEST_RET_ENC_EC(ret);
  31852. ret = wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3);
  31853. if (ret != 0)
  31854. return WC_TEST_RET_ENC_EC(ret);
  31855. if (XMEMCMP(out, sigs[0], 64))
  31856. return WC_TEST_RET_ENC_NC;
  31857. wc_ed25519_free(&key3);
  31858. #endif /* NO_ASN */
  31859. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  31860. /* clean up keys when done */
  31861. wc_ed25519_free(&key);
  31862. wc_ed25519_free(&key2);
  31863. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  31864. wc_FreeRng(&rng);
  31865. #endif
  31866. /* hush warnings of unused keySz and sigSz */
  31867. (void)keySz;
  31868. (void)sigSz;
  31869. ret = ed25519_test_check_key();
  31870. if (ret < 0)
  31871. return ret;
  31872. #ifdef WOLFSSL_TEST_CERT
  31873. ret = ed25519_test_cert();
  31874. if (ret < 0)
  31875. return ret;
  31876. #if defined(WOLFSSL_CERT_GEN) && defined(HAVE_ED25519_MAKE_KEY)
  31877. ret = ed25519_test_make_cert();
  31878. if (ret < 0)
  31879. return ret;
  31880. #endif /* WOLFSSL_CERT_GEN */
  31881. #endif /* WOLFSSL_TEST_CERT */
  31882. return 0;
  31883. }
  31884. #endif /* HAVE_ED25519 */
  31885. #ifdef HAVE_CURVE448
  31886. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  31887. defined(HAVE_CURVE448_KEY_IMPORT)
  31888. /* Test the wc_curve448_check_public API.
  31889. *
  31890. * returns 0 on success and -ve on failure.
  31891. */
  31892. static wc_test_ret_t curve448_check_public_test(void)
  31893. {
  31894. /* Little-endian values that will fail */
  31895. byte fail_le[][CURVE448_KEY_SIZE] = {
  31896. {
  31897. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31898. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31899. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31900. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31901. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31902. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31903. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  31904. },
  31905. {
  31906. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31907. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31908. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31909. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31910. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31911. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31912. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  31913. },
  31914. };
  31915. /* Big-endian values that will fail */
  31916. byte fail_be[][CURVE448_KEY_SIZE] = {
  31917. {
  31918. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31919. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31920. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31921. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31922. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31923. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31924. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  31925. },
  31926. {
  31927. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31928. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31929. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31930. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31931. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31932. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31933. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  31934. },
  31935. };
  31936. /* Good or valid public value */
  31937. byte good[CURVE448_KEY_SIZE] = {
  31938. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31939. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31940. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31941. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31942. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31943. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31944. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  31945. };
  31946. int i;
  31947. wc_test_ret_t ret;
  31948. /* Parameter checks */
  31949. /* NULL pointer */
  31950. ret = wc_curve448_check_public(NULL, 0, EC448_LITTLE_ENDIAN);
  31951. if (ret != BAD_FUNC_ARG)
  31952. return WC_TEST_RET_ENC_EC(ret);
  31953. ret = wc_curve448_check_public(NULL, 0, EC448_BIG_ENDIAN);
  31954. if (ret != BAD_FUNC_ARG)
  31955. return WC_TEST_RET_ENC_EC(ret);
  31956. /* Length of 0 treated differently to other invalid lengths for TLS */
  31957. ret = wc_curve448_check_public(good, 0, EC448_LITTLE_ENDIAN);
  31958. if (ret != BUFFER_E)
  31959. return WC_TEST_RET_ENC_EC(ret);
  31960. ret = wc_curve448_check_public(good, 0, EC448_BIG_ENDIAN);
  31961. if (ret != BUFFER_E)
  31962. return WC_TEST_RET_ENC_EC(ret);
  31963. /* Length not CURVE448_KEY_SIZE */
  31964. for (i = 1; i < CURVE448_KEY_SIZE + 2; i++) {
  31965. if (i == CURVE448_KEY_SIZE)
  31966. continue;
  31967. if (wc_curve448_check_public(good, (word32)i, EC448_LITTLE_ENDIAN) !=
  31968. ECC_BAD_ARG_E) {
  31969. return WC_TEST_RET_ENC_I(i);
  31970. }
  31971. if (wc_curve448_check_public(good, (word32)i, EC448_BIG_ENDIAN) !=
  31972. ECC_BAD_ARG_E) {
  31973. return WC_TEST_RET_ENC_I(i);
  31974. }
  31975. }
  31976. /* Little-endian fail cases */
  31977. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  31978. if (wc_curve448_check_public(fail_le[i], CURVE448_KEY_SIZE,
  31979. EC448_LITTLE_ENDIAN) == 0) {
  31980. return WC_TEST_RET_ENC_I(i);
  31981. }
  31982. }
  31983. /* Big-endian fail cases */
  31984. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  31985. if (wc_curve448_check_public(fail_be[i], CURVE448_KEY_SIZE,
  31986. EC448_BIG_ENDIAN) == 0) {
  31987. return WC_TEST_RET_ENC_I(i);
  31988. }
  31989. }
  31990. /* Check a valid public value works! */
  31991. ret = wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  31992. EC448_LITTLE_ENDIAN);
  31993. if (ret != 0)
  31994. return WC_TEST_RET_ENC_EC(ret);
  31995. ret = wc_curve448_check_public(good, CURVE448_KEY_SIZE, EC448_BIG_ENDIAN);
  31996. if (ret != 0)
  31997. return WC_TEST_RET_ENC_EC(ret);
  31998. return 0;
  31999. }
  32000. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  32001. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve448_test(void)
  32002. {
  32003. WC_RNG rng;
  32004. wc_test_ret_t ret;
  32005. #ifdef HAVE_CURVE448_SHARED_SECRET
  32006. byte sharedA[CURVE448_KEY_SIZE];
  32007. byte sharedB[CURVE448_KEY_SIZE];
  32008. word32 y;
  32009. #endif
  32010. #ifdef HAVE_CURVE448_KEY_EXPORT
  32011. byte exportBuf[CURVE448_KEY_SIZE];
  32012. #endif
  32013. word32 x = 0;
  32014. curve448_key userA, userB, pubKey;
  32015. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  32016. defined(HAVE_CURVE448_KEY_IMPORT)
  32017. /* test vectors from
  32018. https://www.rfc-editor.org/rfc/rfc7748.html
  32019. */
  32020. /* secret key for party a */
  32021. byte sa[] = {
  32022. 0x6b, 0x72, 0x98, 0xa5, 0xc0, 0xd8, 0xc2, 0x9a,
  32023. 0x1d, 0xab, 0x27, 0xf1, 0xa6, 0x82, 0x63, 0x00,
  32024. 0x91, 0x73, 0x89, 0x44, 0x97, 0x41, 0xa9, 0x74,
  32025. 0xf5, 0xba, 0xc9, 0xd9, 0x8d, 0xc2, 0x98, 0xd4,
  32026. 0x65, 0x55, 0xbc, 0xe8, 0xba, 0xe8, 0x9e, 0xee,
  32027. 0xd4, 0x00, 0x58, 0x4b, 0xb0, 0x46, 0xcf, 0x75,
  32028. 0x57, 0x9f, 0x51, 0xd1, 0x25, 0x49, 0x8f, 0x9a,
  32029. };
  32030. /* public key for party a */
  32031. byte pa[] = {
  32032. 0xa0, 0x1f, 0xc4, 0x32, 0xe5, 0x80, 0x7f, 0x17,
  32033. 0x53, 0x0d, 0x12, 0x88, 0xda, 0x12, 0x5b, 0x0c,
  32034. 0xd4, 0x53, 0xd9, 0x41, 0x72, 0x64, 0x36, 0xc8,
  32035. 0xbb, 0xd9, 0xc5, 0x22, 0x2c, 0x3d, 0xa7, 0xfa,
  32036. 0x63, 0x9c, 0xe0, 0x3d, 0xb8, 0xd2, 0x3b, 0x27,
  32037. 0x4a, 0x07, 0x21, 0xa1, 0xae, 0xd5, 0x22, 0x7d,
  32038. 0xe6, 0xe3, 0xb7, 0x31, 0xcc, 0xf7, 0x08, 0x9b,
  32039. };
  32040. /* secret key for party b */
  32041. byte sb[] = {
  32042. 0x2d, 0x99, 0x73, 0x51, 0xb6, 0x10, 0x6f, 0x36,
  32043. 0xb0, 0xd1, 0x09, 0x1b, 0x92, 0x9c, 0x4c, 0x37,
  32044. 0x21, 0x3e, 0x0d, 0x2b, 0x97, 0xe8, 0x5e, 0xbb,
  32045. 0x20, 0xc1, 0x27, 0x69, 0x1d, 0x0d, 0xad, 0x8f,
  32046. 0x1d, 0x81, 0x75, 0xb0, 0x72, 0x37, 0x45, 0xe6,
  32047. 0x39, 0xa3, 0xcb, 0x70, 0x44, 0x29, 0x0b, 0x99,
  32048. 0xe0, 0xe2, 0xa0, 0xc2, 0x7a, 0x6a, 0x30, 0x1c,
  32049. };
  32050. /* public key for party b */
  32051. byte pb[] = {
  32052. 0x09, 0x36, 0xf3, 0x7b, 0xc6, 0xc1, 0xbd, 0x07,
  32053. 0xae, 0x3d, 0xec, 0x7a, 0xb5, 0xdc, 0x06, 0xa7,
  32054. 0x3c, 0xa1, 0x32, 0x42, 0xfb, 0x34, 0x3e, 0xfc,
  32055. 0x72, 0xb9, 0xd8, 0x27, 0x30, 0xb4, 0x45, 0xf3,
  32056. 0xd4, 0xb0, 0xbd, 0x07, 0x71, 0x62, 0xa4, 0x6d,
  32057. 0xcf, 0xec, 0x6f, 0x9b, 0x59, 0x0b, 0xfc, 0xbc,
  32058. 0xf5, 0x20, 0xcd, 0xb0, 0x29, 0xa8, 0xb7, 0x3e,
  32059. };
  32060. /* expected shared key */
  32061. byte ss[] = {
  32062. 0x9d, 0x87, 0x4a, 0x51, 0x37, 0x50, 0x9a, 0x44,
  32063. 0x9a, 0xd5, 0x85, 0x30, 0x40, 0x24, 0x1c, 0x52,
  32064. 0x36, 0x39, 0x54, 0x35, 0xc3, 0x64, 0x24, 0xfd,
  32065. 0x56, 0x0b, 0x0c, 0xb6, 0x2b, 0x28, 0x1d, 0x28,
  32066. 0x52, 0x75, 0xa7, 0x40, 0xce, 0x32, 0xa2, 0x2d,
  32067. 0xd1, 0x74, 0x0f, 0x4a, 0xa9, 0x16, 0x1c, 0xec,
  32068. 0x95, 0xcc, 0xc6, 0x1a, 0x18, 0xf4, 0xff, 0x07,
  32069. };
  32070. #endif /* HAVE_CURVE448_SHARED_SECRET */
  32071. (void)x;
  32072. WOLFSSL_ENTER("curve448_test");
  32073. #ifndef HAVE_FIPS
  32074. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  32075. #else
  32076. ret = wc_InitRng(&rng);
  32077. #endif
  32078. if (ret != 0)
  32079. return WC_TEST_RET_ENC_EC(ret);
  32080. wc_curve448_init(&userA);
  32081. wc_curve448_init(&userB);
  32082. wc_curve448_init(&pubKey);
  32083. /* make curve448 keys */
  32084. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userA);
  32085. if (ret != 0)
  32086. return WC_TEST_RET_ENC_EC(ret);
  32087. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userB);
  32088. if (ret != 0)
  32089. return WC_TEST_RET_ENC_EC(ret);
  32090. #ifdef HAVE_CURVE448_SHARED_SECRET
  32091. /* find shared secret key */
  32092. x = sizeof(sharedA);
  32093. ret = wc_curve448_shared_secret(&userA, &userB, sharedA, &x);
  32094. if (ret != 0)
  32095. return WC_TEST_RET_ENC_EC(ret);
  32096. y = sizeof(sharedB);
  32097. ret = wc_curve448_shared_secret(&userB, &userA, sharedB, &y);
  32098. if (ret != 0)
  32099. return WC_TEST_RET_ENC_EC(ret);
  32100. /* compare shared secret keys to test they are the same */
  32101. if (y != x)
  32102. return WC_TEST_RET_ENC_NC;
  32103. if (XMEMCMP(sharedA, sharedB, x))
  32104. return WC_TEST_RET_ENC_NC;
  32105. #endif
  32106. #ifdef HAVE_CURVE448_KEY_EXPORT
  32107. /* export a public key and import it for another user */
  32108. x = sizeof(exportBuf);
  32109. ret = wc_curve448_export_public(&userA, exportBuf, &x);
  32110. if (ret != 0)
  32111. return WC_TEST_RET_ENC_EC(ret);
  32112. #ifdef HAVE_CURVE448_KEY_IMPORT
  32113. ret = wc_curve448_import_public(exportBuf, x, &pubKey);
  32114. if (ret != 0)
  32115. return WC_TEST_RET_ENC_EC(ret);
  32116. #endif
  32117. #endif
  32118. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  32119. defined(HAVE_CURVE448_KEY_IMPORT)
  32120. /* test shared key after importing a public key */
  32121. XMEMSET(sharedB, 0, sizeof(sharedB));
  32122. y = sizeof(sharedB);
  32123. ret = wc_curve448_shared_secret(&userB, &pubKey, sharedB, &y);
  32124. if (ret != 0)
  32125. return WC_TEST_RET_ENC_EC(ret);
  32126. if (XMEMCMP(sharedA, sharedB, y))
  32127. return WC_TEST_RET_ENC_NC;
  32128. /* import RFC test vectors and compare shared key */
  32129. ret = wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA);
  32130. if (ret != 0)
  32131. return WC_TEST_RET_ENC_EC(ret);
  32132. ret = wc_curve448_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB);
  32133. if (ret != 0)
  32134. return WC_TEST_RET_ENC_EC(ret);
  32135. /* test against known test vector */
  32136. XMEMSET(sharedB, 0, sizeof(sharedB));
  32137. y = sizeof(sharedB);
  32138. ret = wc_curve448_shared_secret(&userA, &userB, sharedB, &y);
  32139. if (ret != 0)
  32140. return WC_TEST_RET_ENC_EC(ret);
  32141. if (XMEMCMP(ss, sharedB, y))
  32142. return WC_TEST_RET_ENC_NC;
  32143. /* test swapping roles of keys and generating same shared key */
  32144. XMEMSET(sharedB, 0, sizeof(sharedB));
  32145. y = sizeof(sharedB);
  32146. ret = wc_curve448_shared_secret(&userB, &userA, sharedB, &y);
  32147. if (ret != 0)
  32148. return WC_TEST_RET_ENC_EC(ret);
  32149. if (XMEMCMP(ss, sharedB, y))
  32150. return WC_TEST_RET_ENC_NC;
  32151. /* test with 1 generated key and 1 from known test vector */
  32152. ret = wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA);
  32153. if (ret != 0)
  32154. return WC_TEST_RET_ENC_EC(ret);
  32155. ret = wc_curve448_make_key(&rng, 56, &userB);
  32156. if (ret != 0)
  32157. return WC_TEST_RET_ENC_EC(ret);
  32158. x = sizeof(sharedA);
  32159. ret = wc_curve448_shared_secret(&userA, &userB, sharedA, &x);
  32160. if (ret != 0)
  32161. return WC_TEST_RET_ENC_EC(ret);
  32162. y = sizeof(sharedB);
  32163. ret = wc_curve448_shared_secret(&userB, &userA, sharedB, &y);
  32164. if (ret != 0)
  32165. return WC_TEST_RET_ENC_EC(ret);
  32166. /* compare shared secret keys to test they are the same */
  32167. if (y != x)
  32168. return WC_TEST_RET_ENC_NC;
  32169. if (XMEMCMP(sharedA, sharedB, x))
  32170. return WC_TEST_RET_ENC_NC;
  32171. ret = curve448_check_public_test();
  32172. if (ret != 0)
  32173. return ret;
  32174. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  32175. /* clean up keys when done */
  32176. wc_curve448_free(&pubKey);
  32177. wc_curve448_free(&userB);
  32178. wc_curve448_free(&userA);
  32179. wc_FreeRng(&rng);
  32180. return 0;
  32181. }
  32182. #endif /* HAVE_CURVE448 */
  32183. #ifdef HAVE_ED448
  32184. #ifdef WOLFSSL_TEST_CERT
  32185. static wc_test_ret_t ed448_test_cert(void)
  32186. {
  32187. DecodedCert cert[2];
  32188. DecodedCert* serverCert = NULL;
  32189. DecodedCert* caCert = NULL;
  32190. #ifdef HAVE_ED448_VERIFY
  32191. ed448_key key;
  32192. ed448_key* pubKey = NULL;
  32193. int verify;
  32194. #endif /* HAVE_ED448_VERIFY */
  32195. wc_test_ret_t ret;
  32196. byte* tmp;
  32197. size_t bytes;
  32198. XFILE file;
  32199. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32200. if (tmp == NULL) {
  32201. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  32202. }
  32203. #ifdef USE_CERT_BUFFERS_256
  32204. XMEMCPY(tmp, ca_ed448_cert, sizeof_ca_ed448_cert);
  32205. bytes = sizeof_ca_ed448_cert;
  32206. #elif !defined(NO_FILESYSTEM)
  32207. file = XFOPEN(caEd448Cert, "rb");
  32208. if (file == NULL) {
  32209. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  32210. }
  32211. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  32212. XFCLOSE(file);
  32213. if (bytes == 0)
  32214. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  32215. #else
  32216. /* No certificate to use. */
  32217. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  32218. #endif
  32219. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  32220. caCert = &cert[0];
  32221. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  32222. if (ret != 0)
  32223. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32224. #ifdef USE_CERT_BUFFERS_256
  32225. XMEMCPY(tmp, server_ed448_cert, sizeof_server_ed448_cert);
  32226. bytes = sizeof_server_ed448_cert;
  32227. #elif !defined(NO_FILESYSTEM)
  32228. file = XFOPEN(serverEd448Cert, "rb");
  32229. if (file == NULL) {
  32230. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  32231. }
  32232. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  32233. XFCLOSE(file);
  32234. if (bytes == 0)
  32235. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  32236. #else
  32237. /* No certificate to use. */
  32238. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  32239. #endif
  32240. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  32241. serverCert = &cert[1];
  32242. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  32243. if (ret != 0)
  32244. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32245. #ifdef HAVE_ED448_VERIFY
  32246. ret = wc_ed448_init(&key);
  32247. if (ret < 0)
  32248. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32249. pubKey = &key;
  32250. ret = wc_ed448_import_public(caCert->publicKey, caCert->pubKeySize, pubKey);
  32251. if (ret < 0)
  32252. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32253. ret = wc_ed448_verify_msg(serverCert->signature, serverCert->sigLength,
  32254. serverCert->source + serverCert->certBegin,
  32255. serverCert->sigIndex - serverCert->certBegin,
  32256. &verify, pubKey, NULL, 0);
  32257. if (ret < 0 || verify != 1) {
  32258. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32259. }
  32260. #endif /* HAVE_ED448_VERIFY */
  32261. done:
  32262. if (tmp != NULL)
  32263. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32264. #ifdef HAVE_ED448_VERIFY
  32265. wc_ed448_free(pubKey);
  32266. #endif /* HAVE_ED448_VERIFY */
  32267. if (caCert != NULL)
  32268. FreeDecodedCert(caCert);
  32269. if (serverCert != NULL)
  32270. FreeDecodedCert(serverCert);
  32271. return ret;
  32272. }
  32273. static wc_test_ret_t ed448_test_make_cert(void)
  32274. {
  32275. WC_RNG rng;
  32276. Cert cert;
  32277. DecodedCert decode;
  32278. ed448_key key;
  32279. ed448_key* privKey = NULL;
  32280. wc_test_ret_t ret = 0;
  32281. byte* tmp = NULL;
  32282. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  32283. #ifndef HAVE_FIPS
  32284. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  32285. #else
  32286. ret = wc_InitRng(&rng);
  32287. #endif
  32288. if (ret != 0)
  32289. return WC_TEST_RET_ENC_EC(ret);
  32290. wc_ed448_init(&key);
  32291. privKey = &key;
  32292. wc_ed448_make_key(&rng, ED448_KEY_SIZE, privKey);
  32293. cert.daysValid = 365 * 2;
  32294. cert.selfSigned = 1;
  32295. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  32296. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  32297. cert.isCA = 0;
  32298. #ifdef WOLFSSL_CERT_EXT
  32299. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  32300. if (ret < 0)
  32301. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32302. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  32303. if (ret < 0)
  32304. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32305. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  32306. if (ret < 0)
  32307. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32308. #endif
  32309. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32310. if (tmp == NULL) {
  32311. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  32312. }
  32313. cert.sigType = CTC_ED448;
  32314. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED448_TYPE, privKey, &rng);
  32315. if (ret < 0)
  32316. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32317. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF, ED448_TYPE,
  32318. privKey, &rng);
  32319. if (ret < 0)
  32320. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32321. InitDecodedCert(&decode, tmp, (word32)ret, HEAP_HINT);
  32322. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  32323. FreeDecodedCert(&decode);
  32324. if (ret != 0)
  32325. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32326. done:
  32327. if (tmp != NULL)
  32328. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32329. wc_ed448_free(privKey);
  32330. wc_FreeRng(&rng);
  32331. return ret;
  32332. }
  32333. #endif /* WOLFSSL_TEST_CERT */
  32334. #if defined(HAVE_ED448_KEY_IMPORT)
  32335. static wc_test_ret_t ed448_test_check_key(void)
  32336. {
  32337. /* Fails to find x-ordinate from this y-ordinate. */
  32338. WOLFSSL_SMALL_STACK_STATIC const byte key_bad_y[] = {
  32339. 0x40,
  32340. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32341. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32342. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32343. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32344. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32345. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32346. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32347. 0x00
  32348. };
  32349. /* Y-ordinate value larger than prime. */
  32350. WOLFSSL_SMALL_STACK_STATIC const byte key_bad_y_max[] = {
  32351. 0x40,
  32352. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32353. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32354. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32355. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32356. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32357. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32358. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32359. 0xff
  32360. };
  32361. /* Y-ordinate value equal to prime. */
  32362. WOLFSSL_SMALL_STACK_STATIC const byte key_bad_y_is_p[] = {
  32363. 0x40,
  32364. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32365. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32366. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32367. 0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,
  32368. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32369. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32370. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32371. 0xff
  32372. };
  32373. /* Y-ordinate value equal to prime - 1. */
  32374. WOLFSSL_SMALL_STACK_STATIC const byte key_y_is_p_minus_1[] = {
  32375. 0x40,
  32376. 0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32377. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32378. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32379. 0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,
  32380. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32381. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32382. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32383. 0xff
  32384. };
  32385. ed448_key key;
  32386. int ret;
  32387. int res = 0;
  32388. /* Initialize key for use. */
  32389. ret = wc_ed448_init_ex(&key, HEAP_HINT, devId);
  32390. if (ret != 0) {
  32391. return WC_TEST_RET_ENC_NC;
  32392. }
  32393. /* Load bad public key only and perform checks. */
  32394. ret = wc_ed448_import_public(key_bad_y, ED448_PUB_KEY_SIZE + 1, &key);
  32395. if (ret != PUBLIC_KEY_E) {
  32396. res = WC_TEST_RET_ENC_NC;
  32397. }
  32398. if (ret == 0) {
  32399. /* Load bad public key only and perform checks. */
  32400. ret = wc_ed448_import_public(key_bad_y_max, ED448_PUB_KEY_SIZE + 1,
  32401. &key);
  32402. if (ret != PUBLIC_KEY_E) {
  32403. res = WC_TEST_RET_ENC_NC;
  32404. }
  32405. }
  32406. if (res == 0) {
  32407. /* Load bad public key only and perform checks. */
  32408. ret = wc_ed448_import_public(key_bad_y_is_p, ED448_PUB_KEY_SIZE + 1,
  32409. &key);
  32410. if (ret != PUBLIC_KEY_E) {
  32411. res = WC_TEST_RET_ENC_NC;
  32412. }
  32413. }
  32414. if (res == 0) {
  32415. /* Load good public key only and perform checks. */
  32416. ret = wc_ed448_import_public(key_y_is_p_minus_1, ED448_PUB_KEY_SIZE + 1,
  32417. &key);
  32418. if (ret != 0) {
  32419. res = WC_TEST_RET_ENC_NC;
  32420. }
  32421. }
  32422. /* Dispose of key. */
  32423. wc_ed448_free(&key);
  32424. return res;
  32425. }
  32426. #endif
  32427. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  32428. defined(HAVE_ED448_KEY_IMPORT)
  32429. static wc_test_ret_t ed448_ctx_test(void)
  32430. {
  32431. wc_test_ret_t ret;
  32432. byte out[ED448_SIG_SIZE];
  32433. word32 outlen;
  32434. #ifdef HAVE_ED448_VERIFY
  32435. int verify;
  32436. #endif /* HAVE_ED448_VERIFY */
  32437. ed448_key key;
  32438. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  32439. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  32440. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  32441. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  32442. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  32443. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  32444. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  32445. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  32446. 0x4e
  32447. };
  32448. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  32449. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  32450. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  32451. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  32452. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  32453. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  32454. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  32455. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  32456. 0x80
  32457. };
  32458. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx[] = {
  32459. 0xd4, 0xf8, 0xf6, 0x13, 0x17, 0x70, 0xdd, 0x46,
  32460. 0xf4, 0x08, 0x67, 0xd6, 0xfd, 0x5d, 0x50, 0x55,
  32461. 0xde, 0x43, 0x54, 0x1f, 0x8c, 0x5e, 0x35, 0xab,
  32462. 0xbc, 0xd0, 0x01, 0xb3, 0x2a, 0x89, 0xf7, 0xd2,
  32463. 0x15, 0x1f, 0x76, 0x47, 0xf1, 0x1d, 0x8c, 0xa2,
  32464. 0xae, 0x27, 0x9f, 0xb8, 0x42, 0xd6, 0x07, 0x21,
  32465. 0x7f, 0xce, 0x6e, 0x04, 0x2f, 0x68, 0x15, 0xea,
  32466. 0x00, 0x0c, 0x85, 0x74, 0x1d, 0xe5, 0xc8, 0xda,
  32467. 0x11, 0x44, 0xa6, 0xa1, 0xab, 0xa7, 0xf9, 0x6d,
  32468. 0xe4, 0x25, 0x05, 0xd7, 0xa7, 0x29, 0x85, 0x24,
  32469. 0xfd, 0xa5, 0x38, 0xfc, 0xcb, 0xbb, 0x75, 0x4f,
  32470. 0x57, 0x8c, 0x1c, 0xad, 0x10, 0xd5, 0x4d, 0x0d,
  32471. 0x54, 0x28, 0x40, 0x7e, 0x85, 0xdc, 0xbc, 0x98,
  32472. 0xa4, 0x91, 0x55, 0xc1, 0x37, 0x64, 0xe6, 0x6c,
  32473. 0x3c, 0x00
  32474. };
  32475. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  32476. 0x03
  32477. };
  32478. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  32479. 0x66,0x6f,0x6f
  32480. };
  32481. outlen = sizeof(out);
  32482. XMEMSET(out, 0, sizeof(out));
  32483. ret = wc_ed448_init_ex(&key, HEAP_HINT, devId);
  32484. if (ret != 0)
  32485. return WC_TEST_RET_ENC_EC(ret);
  32486. ret = wc_ed448_import_private_key(sKeyCtx, ED448_KEY_SIZE, pKeyCtx,
  32487. sizeof(pKeyCtx), &key);
  32488. if (ret != 0)
  32489. return WC_TEST_RET_ENC_EC(ret);
  32490. ret = wc_ed448_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  32491. contextCtx, sizeof(contextCtx));
  32492. if (ret != 0)
  32493. return WC_TEST_RET_ENC_EC(ret);
  32494. if (XMEMCMP(out, sigCtx, sizeof(sigCtx)))
  32495. return WC_TEST_RET_ENC_NC;
  32496. #if defined(HAVE_ED448_VERIFY)
  32497. /* test verify on good msg */
  32498. ret = wc_ed448_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify, &key,
  32499. contextCtx, sizeof(contextCtx));
  32500. if (ret != 0 || verify != 1)
  32501. return WC_TEST_RET_ENC_EC(ret);
  32502. #endif
  32503. wc_ed448_free(&key);
  32504. return 0;
  32505. }
  32506. static wc_test_ret_t ed448ph_test(void)
  32507. {
  32508. wc_test_ret_t ret;
  32509. byte out[ED448_SIG_SIZE];
  32510. word32 outlen;
  32511. #ifdef HAVE_ED448_VERIFY
  32512. int verify;
  32513. #endif /* HAVE_ED448_VERIFY */
  32514. ed448_key key;
  32515. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  32516. 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d,
  32517. 0x62, 0xec, 0x77, 0x58, 0x75, 0x20, 0x91, 0x1e,
  32518. 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b,
  32519. 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42,
  32520. 0xef, 0x78, 0x22, 0xe0, 0xd5, 0x10, 0x41, 0x27,
  32521. 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3,
  32522. 0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c,
  32523. 0x49
  32524. };
  32525. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  32526. 0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77,
  32527. 0xa7, 0xab, 0xd2, 0x65, 0x24, 0xcb, 0xdb, 0x31,
  32528. 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde,
  32529. 0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43,
  32530. 0xc0, 0x94, 0x28, 0xa1, 0x31, 0xd6, 0xb1, 0xb5,
  32531. 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76,
  32532. 0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38,
  32533. 0x80
  32534. };
  32535. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  32536. 0x82, 0x2f, 0x69, 0x01, 0xf7, 0x48, 0x0f, 0x3d,
  32537. 0x5f, 0x56, 0x2c, 0x59, 0x29, 0x94, 0xd9, 0x69,
  32538. 0x36, 0x02, 0x87, 0x56, 0x14, 0x48, 0x32, 0x56,
  32539. 0x50, 0x56, 0x00, 0xbb, 0xc2, 0x81, 0xae, 0x38,
  32540. 0x1f, 0x54, 0xd6, 0xbc, 0xe2, 0xea, 0x91, 0x15,
  32541. 0x74, 0x93, 0x2f, 0x52, 0xa4, 0xe6, 0xca, 0xdd,
  32542. 0x78, 0x76, 0x93, 0x75, 0xec, 0x3f, 0xfd, 0x1b,
  32543. 0x80, 0x1a, 0x0d, 0x9b, 0x3f, 0x40, 0x30, 0xcd,
  32544. 0x43, 0x39, 0x64, 0xb6, 0x45, 0x7e, 0xa3, 0x94,
  32545. 0x76, 0x51, 0x12, 0x14, 0xf9, 0x74, 0x69, 0xb5,
  32546. 0x7d, 0xd3, 0x2d, 0xbc, 0x56, 0x0a, 0x9a, 0x94,
  32547. 0xd0, 0x0b, 0xff, 0x07, 0x62, 0x04, 0x64, 0xa3,
  32548. 0xad, 0x20, 0x3d, 0xf7, 0xdc, 0x7c, 0xe3, 0x60,
  32549. 0xc3, 0xcd, 0x36, 0x96, 0xd9, 0xd9, 0xfa, 0xb9,
  32550. 0x0f, 0x00
  32551. };
  32552. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  32553. 0xc3, 0x22, 0x99, 0xd4, 0x6e, 0xc8, 0xff, 0x02,
  32554. 0xb5, 0x45, 0x40, 0x98, 0x28, 0x14, 0xdc, 0xe9,
  32555. 0xa0, 0x58, 0x12, 0xf8, 0x19, 0x62, 0xb6, 0x49,
  32556. 0xd5, 0x28, 0x09, 0x59, 0x16, 0xa2, 0xaa, 0x48,
  32557. 0x10, 0x65, 0xb1, 0x58, 0x04, 0x23, 0xef, 0x92,
  32558. 0x7e, 0xcf, 0x0a, 0xf5, 0x88, 0x8f, 0x90, 0xda,
  32559. 0x0f, 0x6a, 0x9a, 0x85, 0xad, 0x5d, 0xc3, 0xf2,
  32560. 0x80, 0xd9, 0x12, 0x24, 0xba, 0x99, 0x11, 0xa3,
  32561. 0x65, 0x3d, 0x00, 0xe4, 0x84, 0xe2, 0xce, 0x23,
  32562. 0x25, 0x21, 0x48, 0x1c, 0x86, 0x58, 0xdf, 0x30,
  32563. 0x4b, 0xb7, 0x74, 0x5a, 0x73, 0x51, 0x4c, 0xdb,
  32564. 0x9b, 0xf3, 0xe1, 0x57, 0x84, 0xab, 0x71, 0x28,
  32565. 0x4f, 0x8d, 0x07, 0x04, 0xa6, 0x08, 0xc5, 0x4a,
  32566. 0x6b, 0x62, 0xd9, 0x7b, 0xeb, 0x51, 0x1d, 0x13,
  32567. 0x21, 0x00
  32568. };
  32569. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  32570. 0x61,0x62,0x63
  32571. };
  32572. /* SHA-512 hash of msgPh */
  32573. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  32574. 0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77,
  32575. 0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
  32576. 0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee,
  32577. 0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39,
  32578. 0xd5, 0xa1, 0x5b, 0xef, 0x18, 0x6a, 0x53, 0x86,
  32579. 0xc7, 0x57, 0x44, 0xc0, 0x52, 0x7e, 0x1f, 0xaa,
  32580. 0x9f, 0x87, 0x26, 0xe4, 0x62, 0xa1, 0x2a, 0x4f,
  32581. 0xeb, 0x06, 0xbd, 0x88, 0x01, 0xe7, 0x51, 0xe4
  32582. };
  32583. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  32584. 0x66,0x6f,0x6f
  32585. };
  32586. outlen = sizeof(out);
  32587. XMEMSET(out, 0, sizeof(out));
  32588. ret = wc_ed448_init_ex(&key, HEAP_HINT, devId);
  32589. if (ret != 0)
  32590. return WC_TEST_RET_ENC_EC(ret);
  32591. ret = wc_ed448_import_private_key(sKeyPh, ED448_KEY_SIZE, pKeyPh,
  32592. sizeof(pKeyPh), &key);
  32593. if (ret != 0)
  32594. return WC_TEST_RET_ENC_EC(ret);
  32595. ret = wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key, NULL,
  32596. 0);
  32597. if (ret != 0)
  32598. return WC_TEST_RET_ENC_EC(ret);
  32599. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  32600. return WC_TEST_RET_ENC_NC;
  32601. #if defined(HAVE_ED448_VERIFY)
  32602. /* test verify on good msg */
  32603. ret = wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  32604. NULL, 0);
  32605. if (ret != 0 || verify != 1) {
  32606. return WC_TEST_RET_ENC_EC(ret);
  32607. }
  32608. #endif
  32609. ret = wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  32610. contextPh2, sizeof(contextPh2));
  32611. if (ret != 0)
  32612. return WC_TEST_RET_ENC_EC(ret);
  32613. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  32614. return WC_TEST_RET_ENC_NC;
  32615. #if defined(HAVE_ED448_VERIFY)
  32616. /* test verify on good msg */
  32617. ret = wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  32618. contextPh2, sizeof(contextPh2));
  32619. if (ret != 0 || verify != 1) {
  32620. return WC_TEST_RET_ENC_EC(ret);
  32621. }
  32622. #endif
  32623. ret = wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key, NULL,
  32624. 0);
  32625. if (ret != 0)
  32626. return WC_TEST_RET_ENC_EC(ret);
  32627. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  32628. return WC_TEST_RET_ENC_NC;
  32629. #if defined(HAVE_ED448_VERIFY)
  32630. ret = wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  32631. &key, NULL, 0);
  32632. if (ret != 0 || verify != 1) {
  32633. return WC_TEST_RET_ENC_EC(ret);
  32634. }
  32635. #endif
  32636. ret = wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  32637. contextPh2, sizeof(contextPh2));
  32638. if (ret != 0)
  32639. return WC_TEST_RET_ENC_EC(ret);
  32640. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  32641. return WC_TEST_RET_ENC_NC;
  32642. #if defined(HAVE_ED448_VERIFY)
  32643. ret = wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  32644. &key, contextPh2, sizeof(contextPh2));
  32645. if (ret != 0 || verify != 1) {
  32646. return WC_TEST_RET_ENC_EC(ret);
  32647. }
  32648. #endif
  32649. wc_ed448_free(&key);
  32650. return 0;
  32651. }
  32652. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  32653. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed448_test(void)
  32654. {
  32655. wc_test_ret_t ret;
  32656. WC_RNG rng;
  32657. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  32658. defined(HAVE_ED448_KEY_IMPORT)
  32659. byte out[ED448_SIG_SIZE];
  32660. int i;
  32661. word32 outlen;
  32662. #ifdef HAVE_ED448_VERIFY
  32663. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  32664. int j;
  32665. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  32666. int verify;
  32667. #endif /* HAVE_ED448_VERIFY */
  32668. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  32669. word32 keySz, sigSz;
  32670. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  32671. ed448_key *key = NULL;
  32672. ed448_key *key2 = NULL;
  32673. #else
  32674. ed448_key key[1];
  32675. ed448_key key2[1];
  32676. #endif
  32677. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  32678. defined(HAVE_ED448_KEY_IMPORT)
  32679. /* test vectors from
  32680. https://tools.ietf.org/html/rfc8032
  32681. */
  32682. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  32683. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  32684. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  32685. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  32686. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  32687. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  32688. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  32689. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  32690. 0x5b
  32691. };
  32692. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  32693. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  32694. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  32695. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  32696. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  32697. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  32698. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  32699. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  32700. 0x4e
  32701. };
  32702. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  32703. 0x25, 0x8c, 0xdd, 0x4a, 0xda, 0x32, 0xed, 0x9c,
  32704. 0x9f, 0xf5, 0x4e, 0x63, 0x75, 0x6a, 0xe5, 0x82,
  32705. 0xfb, 0x8f, 0xab, 0x2a, 0xc7, 0x21, 0xf2, 0xc8,
  32706. 0xe6, 0x76, 0xa7, 0x27, 0x68, 0x51, 0x3d, 0x93,
  32707. 0x9f, 0x63, 0xdd, 0xdb, 0x55, 0x60, 0x91, 0x33,
  32708. 0xf2, 0x9a, 0xdf, 0x86, 0xec, 0x99, 0x29, 0xdc,
  32709. 0xcb, 0x52, 0xc1, 0xc5, 0xfd, 0x2f, 0xf7, 0xe2,
  32710. 0x1b
  32711. };
  32712. /* uncompressed test */
  32713. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  32714. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  32715. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  32716. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  32717. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  32718. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  32719. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  32720. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  32721. 0x5b
  32722. };
  32723. /* compressed prefix test */
  32724. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  32725. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  32726. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  32727. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  32728. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  32729. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  32730. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  32731. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  32732. 0x5b
  32733. };
  32734. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  32735. 0x87, 0x2d, 0x09, 0x37, 0x80, 0xf5, 0xd3, 0x73,
  32736. 0x0d, 0xf7, 0xc2, 0x12, 0x66, 0x4b, 0x37, 0xb8,
  32737. 0xa0, 0xf2, 0x4f, 0x56, 0x81, 0x0d, 0xaa, 0x83,
  32738. 0x82, 0xcd, 0x4f, 0xa3, 0xf7, 0x76, 0x34, 0xec,
  32739. 0x44, 0xdc, 0x54, 0xf1, 0xc2, 0xed, 0x9b, 0xea,
  32740. 0x86, 0xfa, 0xfb, 0x76, 0x32, 0xd8, 0xbe, 0x19,
  32741. 0x9e, 0xa1, 0x65, 0xf5, 0xad, 0x55, 0xdd, 0x9c,
  32742. 0xe8
  32743. };
  32744. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  32745. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  32746. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  32747. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  32748. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  32749. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  32750. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  32751. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  32752. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  32753. 0x80
  32754. };
  32755. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  32756. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  32757. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  32758. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  32759. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  32760. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  32761. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  32762. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  32763. 0x80
  32764. };
  32765. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  32766. 0x3b, 0xa1, 0x6d, 0xa0, 0xc6, 0xf2, 0xcc, 0x1f,
  32767. 0x30, 0x18, 0x77, 0x40, 0x75, 0x6f, 0x5e, 0x79,
  32768. 0x8d, 0x6b, 0xc5, 0xfc, 0x01, 0x5d, 0x7c, 0x63,
  32769. 0xcc, 0x95, 0x10, 0xee, 0x3f, 0xd4, 0x4a, 0xdc,
  32770. 0x24, 0xd8, 0xe9, 0x68, 0xb6, 0xe4, 0x6e, 0x6f,
  32771. 0x94, 0xd1, 0x9b, 0x94, 0x53, 0x61, 0x72, 0x6b,
  32772. 0xd7, 0x5e, 0x14, 0x9e, 0xf0, 0x98, 0x17, 0xf5,
  32773. 0x80
  32774. };
  32775. /* uncompressed test */
  32776. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  32777. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  32778. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  32779. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  32780. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  32781. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  32782. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  32783. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  32784. 0x80
  32785. };
  32786. /* compressed prefix */
  32787. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  32788. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  32789. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  32790. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  32791. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  32792. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  32793. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  32794. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  32795. 0x80
  32796. };
  32797. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  32798. 0xa8, 0x1b, 0x2e, 0x8a, 0x70, 0xa5, 0xac, 0x94,
  32799. 0xff, 0xdb, 0xcc, 0x9b, 0xad, 0xfc, 0x3f, 0xeb,
  32800. 0x08, 0x01, 0xf2, 0x58, 0x57, 0x8b, 0xb1, 0x14,
  32801. 0xad, 0x44, 0xec, 0xe1, 0xec, 0x0e, 0x79, 0x9d,
  32802. 0xa0, 0x8e, 0xff, 0xb8, 0x1c, 0x5d, 0x68, 0x5c,
  32803. 0x0c, 0x56, 0xf6, 0x4e, 0xec, 0xae, 0xf8, 0xcd,
  32804. 0xf1, 0x1c, 0xc3, 0x87, 0x37, 0x83, 0x8c, 0xf4,
  32805. 0x00
  32806. };
  32807. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  32808. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  32809. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  32810. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  32811. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  32812. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  32813. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  32814. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  32815. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  32816. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  32817. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  32818. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  32819. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  32820. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  32821. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  32822. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  32823. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  32824. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  32825. 0x26, 0x00
  32826. };
  32827. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  32828. 0x26, 0xb8, 0xf9, 0x17, 0x27, 0xbd, 0x62, 0x89,
  32829. 0x7a, 0xf1, 0x5e, 0x41, 0xeb, 0x43, 0xc3, 0x77,
  32830. 0xef, 0xb9, 0xc6, 0x10, 0xd4, 0x8f, 0x23, 0x35,
  32831. 0xcb, 0x0b, 0xd0, 0x08, 0x78, 0x10, 0xf4, 0x35,
  32832. 0x25, 0x41, 0xb1, 0x43, 0xc4, 0xb9, 0x81, 0xb7,
  32833. 0xe1, 0x8f, 0x62, 0xde, 0x8c, 0xcd, 0xf6, 0x33,
  32834. 0xfc, 0x1b, 0xf0, 0x37, 0xab, 0x7c, 0xd7, 0x79,
  32835. 0x80, 0x5e, 0x0d, 0xbc, 0xc0, 0xaa, 0xe1, 0xcb,
  32836. 0xce, 0xe1, 0xaf, 0xb2, 0xe0, 0x27, 0xdf, 0x36,
  32837. 0xbc, 0x04, 0xdc, 0xec, 0xbf, 0x15, 0x43, 0x36,
  32838. 0xc1, 0x9f, 0x0a, 0xf7, 0xe0, 0xa6, 0x47, 0x29,
  32839. 0x05, 0xe7, 0x99, 0xf1, 0x95, 0x3d, 0x2a, 0x0f,
  32840. 0xf3, 0x34, 0x8a, 0xb2, 0x1a, 0xa4, 0xad, 0xaf,
  32841. 0xd1, 0xd2, 0x34, 0x44, 0x1c, 0xf8, 0x07, 0xc0,
  32842. 0x3a, 0x00
  32843. };
  32844. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  32845. 0x7e, 0xee, 0xab, 0x7c, 0x4e, 0x50, 0xfb, 0x79,
  32846. 0x9b, 0x41, 0x8e, 0xe5, 0xe3, 0x19, 0x7f, 0xf6,
  32847. 0xbf, 0x15, 0xd4, 0x3a, 0x14, 0xc3, 0x43, 0x89,
  32848. 0xb5, 0x9d, 0xd1, 0xa7, 0xb1, 0xb8, 0x5b, 0x4a,
  32849. 0xe9, 0x04, 0x38, 0xac, 0xa6, 0x34, 0xbe, 0xa4,
  32850. 0x5e, 0x3a, 0x26, 0x95, 0xf1, 0x27, 0x0f, 0x07,
  32851. 0xfd, 0xcd, 0xf7, 0xc6, 0x2b, 0x8e, 0xfe, 0xaf,
  32852. 0x00, 0xb4, 0x5c, 0x2c, 0x96, 0xba, 0x45, 0x7e,
  32853. 0xb1, 0xa8, 0xbf, 0x07, 0x5a, 0x3d, 0xb2, 0x8e,
  32854. 0x5c, 0x24, 0xf6, 0xb9, 0x23, 0xed, 0x4a, 0xd7,
  32855. 0x47, 0xc3, 0xc9, 0xe0, 0x3c, 0x70, 0x79, 0xef,
  32856. 0xb8, 0x7c, 0xb1, 0x10, 0xd3, 0xa9, 0x98, 0x61,
  32857. 0xe7, 0x20, 0x03, 0xcb, 0xae, 0x6d, 0x6b, 0x8b,
  32858. 0x82, 0x7e, 0x4e, 0x6c, 0x14, 0x30, 0x64, 0xff,
  32859. 0x3c, 0x00
  32860. };
  32861. /* uncompressed test */
  32862. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  32863. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  32864. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  32865. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  32866. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  32867. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  32868. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  32869. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  32870. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  32871. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  32872. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  32873. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  32874. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  32875. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  32876. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  32877. 0x26, 0x00
  32878. };
  32879. /* compressed prefix */
  32880. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  32881. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  32882. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  32883. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  32884. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  32885. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  32886. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  32887. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  32888. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  32889. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  32890. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  32891. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  32892. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  32893. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  32894. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  32895. 0x26, 0x00
  32896. };
  32897. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  32898. 0xe3, 0x01, 0x34, 0x5a, 0x41, 0xa3, 0x9a, 0x4d,
  32899. 0x72, 0xff, 0xf8, 0xdf, 0x69, 0xc9, 0x80, 0x75,
  32900. 0xa0, 0xcc, 0x08, 0x2b, 0x80, 0x2f, 0xc9, 0xb2,
  32901. 0xb6, 0xbc, 0x50, 0x3f, 0x92, 0x6b, 0x65, 0xbd,
  32902. 0xdf, 0x7f, 0x4c, 0x8f, 0x1c, 0xb4, 0x9f, 0x63,
  32903. 0x96, 0xaf, 0xc8, 0xa7, 0x0a, 0xbe, 0x6d, 0x8a,
  32904. 0xef, 0x0d, 0xb4, 0x78, 0xd4, 0xc6, 0xb2, 0x97,
  32905. 0x00, 0x76, 0xc6, 0xa0, 0x48, 0x4f, 0xe7, 0x6d,
  32906. 0x76, 0xb3, 0xa9, 0x76, 0x25, 0xd7, 0x9f, 0x1c,
  32907. 0xe2, 0x40, 0xe7, 0xc5, 0x76, 0x75, 0x0d, 0x29,
  32908. 0x55, 0x28, 0x28, 0x6f, 0x71, 0x9b, 0x41, 0x3d,
  32909. 0xe9, 0xad, 0xa3, 0xe8, 0xeb, 0x78, 0xed, 0x57,
  32910. 0x36, 0x03, 0xce, 0x30, 0xd8, 0xbb, 0x76, 0x17,
  32911. 0x85, 0xdc, 0x30, 0xdb, 0xc3, 0x20, 0x86, 0x9e,
  32912. 0x1a, 0x00
  32913. };
  32914. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  32915. #define SIGSZ sizeof(sig1)
  32916. PEDANTIC_EXTENSION WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = { 0 };
  32917. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = { 0x03 };
  32918. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = { 0x64, 0xa6, 0x5f, 0x3c, 0xde, 0xdc, 0xdd,
  32919. 0x66, 0x81, 0x1e, 0x29, 0x15 };
  32920. /* test of a 1023 byte long message */
  32921. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  32922. 0x6d, 0xdf, 0x80, 0x2e, 0x1a, 0xae, 0x49, 0x86,
  32923. 0x93, 0x5f, 0x7f, 0x98, 0x1b, 0xa3, 0xf0, 0x35,
  32924. 0x1d, 0x62, 0x73, 0xc0, 0xa0, 0xc2, 0x2c, 0x9c,
  32925. 0x0e, 0x83, 0x39, 0x16, 0x8e, 0x67, 0x54, 0x12,
  32926. 0xa3, 0xde, 0xbf, 0xaf, 0x43, 0x5e, 0xd6, 0x51,
  32927. 0x55, 0x80, 0x07, 0xdb, 0x43, 0x84, 0xb6, 0x50,
  32928. 0xfc, 0xc0, 0x7e, 0x3b, 0x58, 0x6a, 0x27, 0xa4,
  32929. 0xf7, 0xa0, 0x0a, 0xc8, 0xa6, 0xfe, 0xc2, 0xcd,
  32930. 0x86, 0xae, 0x4b, 0xf1, 0x57, 0x0c, 0x41, 0xe6,
  32931. 0xa4, 0x0c, 0x93, 0x1d, 0xb2, 0x7b, 0x2f, 0xaa,
  32932. 0x15, 0xa8, 0xce, 0xdd, 0x52, 0xcf, 0xf7, 0x36,
  32933. 0x2c, 0x4e, 0x6e, 0x23, 0xda, 0xec, 0x0f, 0xbc,
  32934. 0x3a, 0x79, 0xb6, 0x80, 0x6e, 0x31, 0x6e, 0xfc,
  32935. 0xc7, 0xb6, 0x81, 0x19, 0xbf, 0x46, 0xbc, 0x76,
  32936. 0xa2, 0x60, 0x67, 0xa5, 0x3f, 0x29, 0x6d, 0xaf,
  32937. 0xdb, 0xdc, 0x11, 0xc7, 0x7f, 0x77, 0x77, 0xe9,
  32938. 0x72, 0x66, 0x0c, 0xf4, 0xb6, 0xa9, 0xb3, 0x69,
  32939. 0xa6, 0x66, 0x5f, 0x02, 0xe0, 0xcc, 0x9b, 0x6e,
  32940. 0xdf, 0xad, 0x13, 0x6b, 0x4f, 0xab, 0xe7, 0x23,
  32941. 0xd2, 0x81, 0x3d, 0xb3, 0x13, 0x6c, 0xfd, 0xe9,
  32942. 0xb6, 0xd0, 0x44, 0x32, 0x2f, 0xee, 0x29, 0x47,
  32943. 0x95, 0x2e, 0x03, 0x1b, 0x73, 0xab, 0x5c, 0x60,
  32944. 0x33, 0x49, 0xb3, 0x07, 0xbd, 0xc2, 0x7b, 0xc6,
  32945. 0xcb, 0x8b, 0x8b, 0xbd, 0x7b, 0xd3, 0x23, 0x21,
  32946. 0x9b, 0x80, 0x33, 0xa5, 0x81, 0xb5, 0x9e, 0xad,
  32947. 0xeb, 0xb0, 0x9b, 0x3c, 0x4f, 0x3d, 0x22, 0x77,
  32948. 0xd4, 0xf0, 0x34, 0x36, 0x24, 0xac, 0xc8, 0x17,
  32949. 0x80, 0x47, 0x28, 0xb2, 0x5a, 0xb7, 0x97, 0x17,
  32950. 0x2b, 0x4c, 0x5c, 0x21, 0xa2, 0x2f, 0x9c, 0x78,
  32951. 0x39, 0xd6, 0x43, 0x00, 0x23, 0x2e, 0xb6, 0x6e,
  32952. 0x53, 0xf3, 0x1c, 0x72, 0x3f, 0xa3, 0x7f, 0xe3,
  32953. 0x87, 0xc7, 0xd3, 0xe5, 0x0b, 0xdf, 0x98, 0x13,
  32954. 0xa3, 0x0e, 0x5b, 0xb1, 0x2c, 0xf4, 0xcd, 0x93,
  32955. 0x0c, 0x40, 0xcf, 0xb4, 0xe1, 0xfc, 0x62, 0x25,
  32956. 0x92, 0xa4, 0x95, 0x88, 0x79, 0x44, 0x94, 0xd5,
  32957. 0x6d, 0x24, 0xea, 0x4b, 0x40, 0xc8, 0x9f, 0xc0,
  32958. 0x59, 0x6c, 0xc9, 0xeb, 0xb9, 0x61, 0xc8, 0xcb,
  32959. 0x10, 0xad, 0xde, 0x97, 0x6a, 0x5d, 0x60, 0x2b,
  32960. 0x1c, 0x3f, 0x85, 0xb9, 0xb9, 0xa0, 0x01, 0xed,
  32961. 0x3c, 0x6a, 0x4d, 0x3b, 0x14, 0x37, 0xf5, 0x20,
  32962. 0x96, 0xcd, 0x19, 0x56, 0xd0, 0x42, 0xa5, 0x97,
  32963. 0xd5, 0x61, 0xa5, 0x96, 0xec, 0xd3, 0xd1, 0x73,
  32964. 0x5a, 0x8d, 0x57, 0x0e, 0xa0, 0xec, 0x27, 0x22,
  32965. 0x5a, 0x2c, 0x4a, 0xaf, 0xf2, 0x63, 0x06, 0xd1,
  32966. 0x52, 0x6c, 0x1a, 0xf3, 0xca, 0x6d, 0x9c, 0xf5,
  32967. 0xa2, 0xc9, 0x8f, 0x47, 0xe1, 0xc4, 0x6d, 0xb9,
  32968. 0xa3, 0x32, 0x34, 0xcf, 0xd4, 0xd8, 0x1f, 0x2c,
  32969. 0x98, 0x53, 0x8a, 0x09, 0xeb, 0xe7, 0x69, 0x98,
  32970. 0xd0, 0xd8, 0xfd, 0x25, 0x99, 0x7c, 0x7d, 0x25,
  32971. 0x5c, 0x6d, 0x66, 0xec, 0xe6, 0xfa, 0x56, 0xf1,
  32972. 0x11, 0x44, 0x95, 0x0f, 0x02, 0x77, 0x95, 0xe6,
  32973. 0x53, 0x00, 0x8f, 0x4b, 0xd7, 0xca, 0x2d, 0xee,
  32974. 0x85, 0xd8, 0xe9, 0x0f, 0x3d, 0xc3, 0x15, 0x13,
  32975. 0x0c, 0xe2, 0xa0, 0x03, 0x75, 0xa3, 0x18, 0xc7,
  32976. 0xc3, 0xd9, 0x7b, 0xe2, 0xc8, 0xce, 0x5b, 0x6d,
  32977. 0xb4, 0x1a, 0x62, 0x54, 0xff, 0x26, 0x4f, 0xa6,
  32978. 0x15, 0x5b, 0xae, 0xe3, 0xb0, 0x77, 0x3c, 0x0f,
  32979. 0x49, 0x7c, 0x57, 0x3f, 0x19, 0xbb, 0x4f, 0x42,
  32980. 0x40, 0x28, 0x1f, 0x0b, 0x1f, 0x4f, 0x7b, 0xe8,
  32981. 0x57, 0xa4, 0xe5, 0x9d, 0x41, 0x6c, 0x06, 0xb4,
  32982. 0xc5, 0x0f, 0xa0, 0x9e, 0x18, 0x10, 0xdd, 0xc6,
  32983. 0xb1, 0x46, 0x7b, 0xae, 0xac, 0x5a, 0x36, 0x68,
  32984. 0xd1, 0x1b, 0x6e, 0xca, 0xa9, 0x01, 0x44, 0x00,
  32985. 0x16, 0xf3, 0x89, 0xf8, 0x0a, 0xcc, 0x4d, 0xb9,
  32986. 0x77, 0x02, 0x5e, 0x7f, 0x59, 0x24, 0x38, 0x8c,
  32987. 0x7e, 0x34, 0x0a, 0x73, 0x2e, 0x55, 0x44, 0x40,
  32988. 0xe7, 0x65, 0x70, 0xf8, 0xdd, 0x71, 0xb7, 0xd6,
  32989. 0x40, 0xb3, 0x45, 0x0d, 0x1f, 0xd5, 0xf0, 0x41,
  32990. 0x0a, 0x18, 0xf9, 0xa3, 0x49, 0x4f, 0x70, 0x7c,
  32991. 0x71, 0x7b, 0x79, 0xb4, 0xbf, 0x75, 0xc9, 0x84,
  32992. 0x00, 0xb0, 0x96, 0xb2, 0x16, 0x53, 0xb5, 0xd2,
  32993. 0x17, 0xcf, 0x35, 0x65, 0xc9, 0x59, 0x74, 0x56,
  32994. 0xf7, 0x07, 0x03, 0x49, 0x7a, 0x07, 0x87, 0x63,
  32995. 0x82, 0x9b, 0xc0, 0x1b, 0xb1, 0xcb, 0xc8, 0xfa,
  32996. 0x04, 0xea, 0xdc, 0x9a, 0x6e, 0x3f, 0x66, 0x99,
  32997. 0x58, 0x7a, 0x9e, 0x75, 0xc9, 0x4e, 0x5b, 0xab,
  32998. 0x00, 0x36, 0xe0, 0xb2, 0xe7, 0x11, 0x39, 0x2c,
  32999. 0xff, 0x00, 0x47, 0xd0, 0xd6, 0xb0, 0x5b, 0xd2,
  33000. 0xa5, 0x88, 0xbc, 0x10, 0x97, 0x18, 0x95, 0x42,
  33001. 0x59, 0xf1, 0xd8, 0x66, 0x78, 0xa5, 0x79, 0xa3,
  33002. 0x12, 0x0f, 0x19, 0xcf, 0xb2, 0x96, 0x3f, 0x17,
  33003. 0x7a, 0xeb, 0x70, 0xf2, 0xd4, 0x84, 0x48, 0x26,
  33004. 0x26, 0x2e, 0x51, 0xb8, 0x02, 0x71, 0x27, 0x20,
  33005. 0x68, 0xef, 0x5b, 0x38, 0x56, 0xfa, 0x85, 0x35,
  33006. 0xaa, 0x2a, 0x88, 0xb2, 0xd4, 0x1f, 0x2a, 0x0e,
  33007. 0x2f, 0xda, 0x76, 0x24, 0xc2, 0x85, 0x02, 0x72,
  33008. 0xac, 0x4a, 0x2f, 0x56, 0x1f, 0x8f, 0x2f, 0x7a,
  33009. 0x31, 0x8b, 0xfd, 0x5c, 0xaf, 0x96, 0x96, 0x14,
  33010. 0x9e, 0x4a, 0xc8, 0x24, 0xad, 0x34, 0x60, 0x53,
  33011. 0x8f, 0xdc, 0x25, 0x42, 0x1b, 0xee, 0xc2, 0xcc,
  33012. 0x68, 0x18, 0x16, 0x2d, 0x06, 0xbb, 0xed, 0x0c,
  33013. 0x40, 0xa3, 0x87, 0x19, 0x23, 0x49, 0xdb, 0x67,
  33014. 0xa1, 0x18, 0xba, 0xda, 0x6c, 0xd5, 0xab, 0x01,
  33015. 0x40, 0xee, 0x27, 0x32, 0x04, 0xf6, 0x28, 0xaa,
  33016. 0xd1, 0xc1, 0x35, 0xf7, 0x70, 0x27, 0x9a, 0x65,
  33017. 0x1e, 0x24, 0xd8, 0xc1, 0x4d, 0x75, 0xa6, 0x05,
  33018. 0x9d, 0x76, 0xb9, 0x6a, 0x6f, 0xd8, 0x57, 0xde,
  33019. 0xf5, 0xe0, 0xb3, 0x54, 0xb2, 0x7a, 0xb9, 0x37,
  33020. 0xa5, 0x81, 0x5d, 0x16, 0xb5, 0xfa, 0xe4, 0x07,
  33021. 0xff, 0x18, 0x22, 0x2c, 0x6d, 0x1e, 0xd2, 0x63,
  33022. 0xbe, 0x68, 0xc9, 0x5f, 0x32, 0xd9, 0x08, 0xbd,
  33023. 0x89, 0x5c, 0xd7, 0x62, 0x07, 0xae, 0x72, 0x64,
  33024. 0x87, 0x56, 0x7f, 0x9a, 0x67, 0xda, 0xd7, 0x9a,
  33025. 0xbe, 0xc3, 0x16, 0xf6, 0x83, 0xb1, 0x7f, 0x2d,
  33026. 0x02, 0xbf, 0x07, 0xe0, 0xac, 0x8b, 0x5b, 0xc6,
  33027. 0x16, 0x2c, 0xf9, 0x46, 0x97, 0xb3, 0xc2, 0x7c,
  33028. 0xd1, 0xfe, 0xa4, 0x9b, 0x27, 0xf2, 0x3b, 0xa2,
  33029. 0x90, 0x18, 0x71, 0x96, 0x25, 0x06, 0x52, 0x0c,
  33030. 0x39, 0x2d, 0xa8, 0xb6, 0xad, 0x0d, 0x99, 0xf7,
  33031. 0x01, 0x3f, 0xbc, 0x06, 0xc2, 0xc1, 0x7a, 0x56,
  33032. 0x95, 0x00, 0xc8, 0xa7, 0x69, 0x64, 0x81, 0xc1,
  33033. 0xcd, 0x33, 0xe9, 0xb1, 0x4e, 0x40, 0xb8, 0x2e,
  33034. 0x79, 0xa5, 0xf5, 0xdb, 0x82, 0x57, 0x1b, 0xa9,
  33035. 0x7b, 0xae, 0x3a, 0xd3, 0xe0, 0x47, 0x95, 0x15,
  33036. 0xbb, 0x0e, 0x2b, 0x0f, 0x3b, 0xfc, 0xd1, 0xfd,
  33037. 0x33, 0x03, 0x4e, 0xfc, 0x62, 0x45, 0xed, 0xdd,
  33038. 0x7e, 0xe2, 0x08, 0x6d, 0xda, 0xe2, 0x60, 0x0d,
  33039. 0x8c, 0xa7, 0x3e, 0x21, 0x4e, 0x8c, 0x2b, 0x0b,
  33040. 0xdb, 0x2b, 0x04, 0x7c, 0x6a, 0x46, 0x4a, 0x56,
  33041. 0x2e, 0xd7, 0x7b, 0x73, 0xd2, 0xd8, 0x41, 0xc4,
  33042. 0xb3, 0x49, 0x73, 0x55, 0x12, 0x57, 0x71, 0x3b,
  33043. 0x75, 0x36, 0x32, 0xef, 0xba, 0x34, 0x81, 0x69,
  33044. 0xab, 0xc9, 0x0a, 0x68, 0xf4, 0x26, 0x11, 0xa4,
  33045. 0x01, 0x26, 0xd7, 0xcb, 0x21, 0xb5, 0x86, 0x95,
  33046. 0x56, 0x81, 0x86, 0xf7, 0xe5, 0x69, 0xd2, 0xff,
  33047. 0x0f, 0x9e, 0x74, 0x5d, 0x04, 0x87, 0xdd, 0x2e,
  33048. 0xb9, 0x97, 0xca, 0xfc, 0x5a, 0xbf, 0x9d, 0xd1,
  33049. 0x02, 0xe6, 0x2f, 0xf6, 0x6c, 0xba, 0x87
  33050. };
  33051. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  33052. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  33053. sizeof(msg2),
  33054. sizeof(msg3),
  33055. 0 /*sizeof(msg1)*/,
  33056. 0 /*sizeof(msg1)*/,
  33057. sizeof(msg4)
  33058. };
  33059. #ifndef NO_ASN
  33060. static const byte privateEd448[] = {
  33061. 0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
  33062. 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04, 0x39,
  33063. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  33064. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  33065. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  33066. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  33067. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  33068. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  33069. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  33070. 0x5b
  33071. };
  33072. static const byte publicEd448[] = {
  33073. 0x30, 0x43, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
  33074. 0x71, 0x03, 0x3a, 0x00, 0x5f, 0xd7, 0x44, 0x9b,
  33075. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  33076. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  33077. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  33078. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  33079. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  33080. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  33081. 0xaf, 0xe8, 0x25, 0x61, 0x80
  33082. };
  33083. static const byte privPubEd448[] = {
  33084. 0x30, 0x81, 0x82, 0x02, 0x01, 0x00, 0x30, 0x05,
  33085. 0x06, 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04,
  33086. 0x39, 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d,
  33087. 0x10, 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e,
  33088. 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c,
  33089. 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48,
  33090. 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04,
  33091. 0x4e, 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f,
  33092. 0x8f, 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98,
  33093. 0xf9, 0x5b, 0x81, 0x39, 0x5f, 0xd7, 0x44, 0x9b,
  33094. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  33095. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  33096. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  33097. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  33098. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  33099. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  33100. 0xaf, 0xe8, 0x25, 0x61, 0x80
  33101. };
  33102. word32 idx;
  33103. #endif /* NO_ASN */
  33104. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  33105. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  33106. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33107. ed448_key *key3 = NULL;
  33108. #else
  33109. ed448_key key3[1];
  33110. #endif
  33111. #endif
  33112. WOLFSSL_ENTER("ed448_test");
  33113. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33114. key = (ed448_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33115. key2 = (ed448_key *)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33116. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  33117. key3 = (ed448_key *)XMALLOC(sizeof(*key3), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33118. #endif
  33119. #endif
  33120. /* create ed448 keys */
  33121. #ifndef HAVE_FIPS
  33122. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  33123. #else
  33124. ret = wc_InitRng(&rng);
  33125. #endif
  33126. if (ret != 0) {
  33127. XMEMSET(&rng, 0, sizeof(rng));
  33128. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33129. }
  33130. ret = wc_ed448_init(key);
  33131. if (ret < 0)
  33132. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33133. ret = wc_ed448_init(key2);
  33134. if (ret < 0)
  33135. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33136. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  33137. ret = wc_ed448_init(key3);
  33138. if (ret < 0)
  33139. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33140. #endif
  33141. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, key);
  33142. if (ret < 0)
  33143. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33144. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, key2);
  33145. if (ret < 0)
  33146. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33147. /* helper functions for signature and key size */
  33148. keySz = (word32)wc_ed448_size(key);
  33149. sigSz = (word32)wc_ed448_sig_size(key);
  33150. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  33151. defined(HAVE_ED448_KEY_IMPORT)
  33152. for (i = 0; i < 6; i++) {
  33153. outlen = sizeof(out);
  33154. XMEMSET(out, 0, sizeof(out));
  33155. if (wc_ed448_import_private_key(sKeys[i], ED448_KEY_SIZE, pKeys[i],
  33156. pKeySz[i], key) != 0)
  33157. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33158. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key, NULL,
  33159. 0) != 0)
  33160. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33161. if (XMEMCMP(out, sigs[i], 114))
  33162. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33163. #if defined(HAVE_ED448_VERIFY)
  33164. /* test verify on good msg */
  33165. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  33166. NULL, 0) != 0 || verify != 1)
  33167. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33168. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  33169. /* test verify on good msg using streaming interface directly */
  33170. if (wc_ed448_verify_msg_init(out, outlen,
  33171. key, (byte)Ed448, NULL, 0) != 0)
  33172. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33173. for (j = 0; j < msgSz[i]; j += i) {
  33174. if (wc_ed448_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), key) != 0)
  33175. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33176. }
  33177. if (wc_ed448_verify_msg_final(out, outlen, &verify,
  33178. key) != 0 || verify != 1)
  33179. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33180. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  33181. /* test verify on bad msg */
  33182. out[outlen-2] = out[outlen-2] + 1;
  33183. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  33184. NULL, 0) == 0 || verify == 1)
  33185. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33186. #endif /* HAVE_ED448_VERIFY */
  33187. /* test api for import/exporting keys */
  33188. {
  33189. byte *exportPKey = NULL;
  33190. byte *exportSKey = NULL;
  33191. word32 exportPSz = ED448_KEY_SIZE;
  33192. word32 exportSSz = ED448_KEY_SIZE;
  33193. exportPKey = (byte *)XMALLOC(exportPSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33194. exportSKey = (byte *)XMALLOC(exportSSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33195. if ((exportPKey == NULL) || (exportSKey == NULL))
  33196. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33197. ret = 0;
  33198. do {
  33199. if (wc_ed448_export_public(key, exportPKey, &exportPSz) != 0) {
  33200. ret = WC_TEST_RET_ENC_I(i);
  33201. break;
  33202. }
  33203. if (wc_ed448_import_public_ex(exportPKey, exportPSz, key2, 1) != 0) {
  33204. ret = WC_TEST_RET_ENC_I(i);
  33205. break;
  33206. }
  33207. if (wc_ed448_export_private_only(key, exportSKey, &exportSSz) != 0) {
  33208. ret = WC_TEST_RET_ENC_I(i);
  33209. break;
  33210. }
  33211. if (wc_ed448_import_private_key(exportSKey, exportSSz,
  33212. exportPKey, exportPSz, key2) != 0) {
  33213. ret = WC_TEST_RET_ENC_I(i);
  33214. break;
  33215. }
  33216. /* clear "out" buffer and test sign with imported keys */
  33217. outlen = sizeof(out);
  33218. XMEMSET(out, 0, sizeof(out));
  33219. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key2, NULL,
  33220. 0) != 0) {
  33221. ret = WC_TEST_RET_ENC_I(i);
  33222. break;
  33223. }
  33224. } while(0);
  33225. XFREE(exportPKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33226. XFREE(exportSKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33227. if (ret != 0)
  33228. goto out;
  33229. }
  33230. #if defined(HAVE_ED448_VERIFY)
  33231. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key2,
  33232. NULL, 0) != 0 || verify != 1)
  33233. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33234. if (XMEMCMP(out, sigs[i], SIGSZ))
  33235. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33236. #endif /* HAVE_ED448_VERIFY */
  33237. }
  33238. ret = ed448_ctx_test();
  33239. if (ret != 0)
  33240. goto out;
  33241. ret = ed448ph_test();
  33242. if (ret != 0)
  33243. goto out;
  33244. #ifndef NO_ASN
  33245. /* Try ASN.1 encoded private-only key and public key. */
  33246. idx = 0;
  33247. ret = wc_Ed448PrivateKeyDecode(privateEd448, &idx, key3,
  33248. sizeof(privateEd448));
  33249. if (ret != 0)
  33250. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33251. ret = wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0);
  33252. if (ret != BAD_FUNC_ARG)
  33253. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33254. idx = 0;
  33255. ret = wc_Ed448PublicKeyDecode(publicEd448, &idx, key3, sizeof(publicEd448));
  33256. if (ret != 0)
  33257. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33258. ret = wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0);
  33259. if (ret != 0)
  33260. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33261. if (XMEMCMP(out, sigs[0], SIGSZ))
  33262. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33263. #if defined(HAVE_ED448_VERIFY)
  33264. /* test verify on good msg */
  33265. ret = wc_ed448_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, key3,
  33266. NULL, 0);
  33267. if (ret != 0 || verify != 1)
  33268. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33269. #endif /* HAVE_ED448_VERIFY */
  33270. wc_ed448_free(key3);
  33271. ret = wc_ed448_init(key3);
  33272. if (ret < 0)
  33273. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33274. idx = 0;
  33275. ret = wc_Ed448PrivateKeyDecode(privPubEd448, &idx, key3,
  33276. sizeof(privPubEd448));
  33277. if (ret != 0)
  33278. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33279. ret = wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0);
  33280. if (ret != 0)
  33281. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33282. if (XMEMCMP(out, sigs[0], SIGSZ))
  33283. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33284. #endif /* NO_ASN */
  33285. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  33286. ret = 0;
  33287. out:
  33288. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33289. if (key) {
  33290. wc_ed448_free(key);
  33291. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33292. }
  33293. if (key2) {
  33294. wc_ed448_free(key2);
  33295. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33296. }
  33297. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  33298. if (key3) {
  33299. wc_ed448_free(key3);
  33300. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33301. }
  33302. #endif
  33303. #else
  33304. wc_ed448_free(key);
  33305. wc_ed448_free(key2);
  33306. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  33307. wc_ed448_free(key3);
  33308. #endif
  33309. #endif
  33310. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  33311. wc_FreeRng(&rng);
  33312. #endif
  33313. if (ret < 0)
  33314. return ret;
  33315. /* hush warnings of unused keySz and sigSz */
  33316. (void)keySz;
  33317. (void)sigSz;
  33318. ret = ed448_test_check_key();
  33319. if (ret < 0)
  33320. return ret;
  33321. #ifdef WOLFSSL_TEST_CERT
  33322. ret = ed448_test_cert();
  33323. if (ret < 0)
  33324. return ret;
  33325. #ifdef WOLFSSL_CERT_GEN
  33326. ret = ed448_test_make_cert();
  33327. if (ret < 0)
  33328. return ret;
  33329. #endif /* WOLFSSL_CERT_GEN */
  33330. #endif /* WOLFSSL_TEST_CERT */
  33331. return 0;
  33332. }
  33333. #endif /* HAVE_ED448 */
  33334. #ifdef WOLFSSL_HAVE_KYBER
  33335. #ifdef WOLFSSL_WC_KYBER /* OQS and PQM4 do not support KATs */
  33336. #ifdef WOLFSSL_KYBER512
  33337. static wc_test_ret_t kyber512_kat(void)
  33338. {
  33339. wc_test_ret_t ret;
  33340. #ifdef WOLFSSL_SMALL_STACK
  33341. KyberKey *key = NULL;
  33342. byte *priv = NULL;
  33343. byte *pub = NULL;
  33344. byte *ct = NULL;
  33345. byte *ss = NULL;
  33346. byte *ss_dec = NULL;
  33347. #else
  33348. KyberKey key[1];
  33349. byte priv[KYBER512_PRIVATE_KEY_SIZE];
  33350. byte pub[KYBER512_PUBLIC_KEY_SIZE];
  33351. byte ct[KYBER512_CIPHER_TEXT_SIZE];
  33352. byte ss[KYBER_SS_SZ];
  33353. byte ss_dec[KYBER_SS_SZ];
  33354. #endif
  33355. int key_inited = 0;
  33356. WOLFSSL_SMALL_STACK_STATIC const byte kyber512_rand[] = {
  33357. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  33358. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  33359. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  33360. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  33361. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  33362. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  33363. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  33364. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  33365. };
  33366. WOLFSSL_SMALL_STACK_STATIC const byte kyber512enc_rand[] = {
  33367. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  33368. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  33369. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  33370. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  33371. };
  33372. WOLFSSL_SMALL_STACK_STATIC const byte kyber512_pk[] = {
  33373. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  33374. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  33375. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  33376. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  33377. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  33378. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  33379. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  33380. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  33381. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  33382. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  33383. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  33384. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  33385. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  33386. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  33387. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  33388. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  33389. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  33390. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  33391. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  33392. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  33393. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  33394. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  33395. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  33396. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  33397. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  33398. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  33399. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  33400. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  33401. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  33402. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  33403. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  33404. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  33405. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  33406. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  33407. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  33408. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  33409. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  33410. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  33411. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  33412. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  33413. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  33414. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  33415. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  33416. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  33417. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  33418. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  33419. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  33420. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  33421. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  33422. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  33423. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  33424. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  33425. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  33426. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  33427. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  33428. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  33429. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  33430. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  33431. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  33432. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  33433. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  33434. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  33435. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  33436. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  33437. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  33438. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  33439. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  33440. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  33441. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  33442. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  33443. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  33444. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  33445. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  33446. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  33447. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  33448. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  33449. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  33450. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  33451. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  33452. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  33453. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  33454. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  33455. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  33456. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  33457. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  33458. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  33459. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  33460. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  33461. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  33462. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  33463. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  33464. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  33465. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  33466. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  33467. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  33468. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  33469. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  33470. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  33471. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  33472. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  33473. };
  33474. WOLFSSL_SMALL_STACK_STATIC const byte kyber512_sk[] = {
  33475. 0x6C, 0x89, 0x2B, 0x02, 0x97, 0xA9, 0xC7, 0x64,
  33476. 0x14, 0x93, 0xF8, 0x7D, 0xAF, 0x35, 0x33, 0xEE,
  33477. 0xD6, 0x1F, 0x07, 0xF4, 0x65, 0x20, 0x66, 0x33,
  33478. 0x7E, 0xD7, 0x40, 0x46, 0xDC, 0xC7, 0x1B, 0xA0,
  33479. 0x3F, 0x30, 0x96, 0x01, 0x03, 0x16, 0x1F, 0x7D,
  33480. 0xEB, 0x53, 0xA7, 0x1B, 0x11, 0x61, 0x72, 0x63,
  33481. 0xFE, 0x2A, 0x80, 0x97, 0x69, 0xCE, 0x6D, 0x70,
  33482. 0xA8, 0x5F, 0xE6, 0x00, 0xEC, 0xE2, 0x9D, 0x7F,
  33483. 0x36, 0xA1, 0x6D, 0x33, 0x1B, 0x8B, 0x2A, 0x9E,
  33484. 0x1D, 0xB8, 0xC0, 0x90, 0x74, 0x2D, 0xF0, 0x73,
  33485. 0x9F, 0xF0, 0x60, 0xCE, 0xB4, 0xEC, 0xC5, 0xAB,
  33486. 0x1C, 0x5E, 0x55, 0xAC, 0x97, 0xBB, 0x66, 0xA7,
  33487. 0xF8, 0x95, 0x10, 0x5D, 0x57, 0x78, 0x2B, 0x22,
  33488. 0x95, 0x38, 0xE3, 0x42, 0x15, 0x44, 0xA3, 0x42,
  33489. 0x14, 0x08, 0xDB, 0xF4, 0x49, 0x10, 0x93, 0x4C,
  33490. 0xC4, 0x23, 0x77, 0x4F, 0x16, 0x76, 0xFF, 0x1C,
  33491. 0x30, 0x6F, 0x97, 0x55, 0x5F, 0x57, 0xB4, 0xAE,
  33492. 0xD7, 0xA6, 0xBA, 0xB9, 0x50, 0xA8, 0x16, 0x3C,
  33493. 0x8D, 0x31, 0x8D, 0xEA, 0x62, 0x75, 0x1B, 0xD6,
  33494. 0xAB, 0xC5, 0x06, 0x9C, 0x06, 0xC8, 0x8F, 0x33,
  33495. 0x00, 0x26, 0xA1, 0x98, 0x06, 0xA0, 0x3B, 0x97,
  33496. 0xA7, 0x69, 0x6B, 0x56, 0xDA, 0x21, 0x82, 0x7B,
  33497. 0xB4, 0xE8, 0xDC, 0x03, 0x11, 0x52, 0xB4, 0x1B,
  33498. 0x89, 0x2A, 0x9E, 0x99, 0xAD, 0xF6, 0xE1, 0x96,
  33499. 0x3E, 0x96, 0x57, 0x88, 0x28, 0x15, 0x4F, 0x46,
  33500. 0x70, 0x33, 0x84, 0x69, 0x20, 0xFB, 0xB4, 0xB8,
  33501. 0x05, 0x44, 0xE7, 0xE8, 0xA8, 0x1A, 0xE9, 0x63,
  33502. 0xCF, 0x36, 0x8C, 0x9B, 0xA0, 0x37, 0xA8, 0xC2,
  33503. 0xAD, 0x62, 0xE3, 0x2B, 0x6E, 0x61, 0xC9, 0x1D,
  33504. 0x75, 0xCE, 0x00, 0x5A, 0xB3, 0x0F, 0x80, 0x99,
  33505. 0xA1, 0xF2, 0x9D, 0x7B, 0x63, 0x05, 0xB4, 0xDC,
  33506. 0x06, 0xE2, 0x56, 0x80, 0xBB, 0x00, 0x99, 0x2F,
  33507. 0x71, 0x7F, 0xE6, 0xC1, 0x15, 0xA8, 0x08, 0x42,
  33508. 0x31, 0xCC, 0x79, 0xDD, 0x70, 0x0E, 0xA6, 0x91,
  33509. 0x2A, 0xC7, 0xFA, 0x0D, 0x93, 0x7B, 0xB6, 0xA7,
  33510. 0x56, 0x66, 0x22, 0x30, 0x47, 0x0C, 0x18, 0x9B,
  33511. 0x5A, 0xA1, 0x65, 0x3D, 0xEB, 0x93, 0x7D, 0x5A,
  33512. 0x9C, 0x25, 0xA2, 0x1D, 0x93, 0xB1, 0x90, 0x74,
  33513. 0xFC, 0x23, 0x9D, 0x81, 0x53, 0x53, 0x97, 0x97,
  33514. 0xC7, 0xD4, 0xAB, 0x62, 0x64, 0x9D, 0x76, 0xAA,
  33515. 0x55, 0x37, 0x36, 0xA9, 0x49, 0x02, 0x2C, 0x22,
  33516. 0xC5, 0x2B, 0xAE, 0xEC, 0x60, 0x5B, 0x32, 0xCE,
  33517. 0x9E, 0x5B, 0x93, 0x84, 0x90, 0x35, 0x58, 0xCA,
  33518. 0x9D, 0x6A, 0x3A, 0xBA, 0x90, 0x42, 0x3E, 0xED,
  33519. 0xA0, 0x1C, 0x94, 0x19, 0x8B, 0x19, 0x2A, 0x8B,
  33520. 0xA9, 0x06, 0x34, 0x97, 0xA0, 0xC5, 0x01, 0x33,
  33521. 0x07, 0xDD, 0xD8, 0x63, 0x52, 0x64, 0x71, 0xA4,
  33522. 0xD9, 0x95, 0x23, 0xEB, 0x41, 0x7F, 0x29, 0x1A,
  33523. 0xAC, 0x0C, 0x3A, 0x58, 0x1B, 0x6D, 0xA0, 0x07,
  33524. 0x32, 0xE5, 0xE8, 0x1B, 0x1F, 0x7C, 0x87, 0x9B,
  33525. 0x16, 0x93, 0xC1, 0x3B, 0x6F, 0x9F, 0x79, 0x31,
  33526. 0x62, 0x24, 0x29, 0xE5, 0x42, 0xAF, 0x40, 0x69,
  33527. 0x22, 0x2F, 0x04, 0x55, 0x44, 0xE0, 0xCC, 0x4F,
  33528. 0xB2, 0x4D, 0x44, 0x48, 0xCF, 0x2C, 0x65, 0x96,
  33529. 0xF5, 0xCB, 0x08, 0x62, 0x4B, 0x11, 0x85, 0x01,
  33530. 0x3B, 0x6B, 0x02, 0x08, 0x92, 0xF9, 0x6B, 0xDF,
  33531. 0xD4, 0xAD, 0xA9, 0x17, 0x9D, 0xE7, 0x27, 0xB8,
  33532. 0xD9, 0x42, 0x6E, 0x09, 0x96, 0xB5, 0xD3, 0x49,
  33533. 0x48, 0xCE, 0x02, 0xD0, 0xC3, 0x69, 0xB3, 0x7C,
  33534. 0xBB, 0x54, 0xD3, 0x47, 0x9E, 0xD8, 0xB5, 0x82,
  33535. 0xE9, 0xE7, 0x28, 0x92, 0x9B, 0x4C, 0x71, 0xC9,
  33536. 0xBE, 0x11, 0xD4, 0x5B, 0x20, 0xC4, 0xBD, 0xC3,
  33537. 0xC7, 0x43, 0x13, 0x22, 0x3F, 0x58, 0x27, 0x4E,
  33538. 0x8B, 0xA5, 0x24, 0x44, 0x47, 0xC4, 0x95, 0x95,
  33539. 0x0B, 0x84, 0xCB, 0x0C, 0x3C, 0x27, 0x36, 0x40,
  33540. 0x10, 0x8A, 0x33, 0x97, 0x94, 0x45, 0x73, 0x27,
  33541. 0x93, 0x28, 0x99, 0x6C, 0xDC, 0x0C, 0x91, 0x3C,
  33542. 0x95, 0x8A, 0xD6, 0x20, 0xBA, 0x8B, 0x5E, 0x5E,
  33543. 0xCB, 0xBB, 0x7E, 0x13, 0xCB, 0x9C, 0x70, 0xBD,
  33544. 0x5A, 0xB3, 0x0E, 0xB7, 0x48, 0x8C, 0x97, 0x00,
  33545. 0x1C, 0x20, 0x49, 0x8F, 0x1D, 0x7C, 0xC0, 0x6D,
  33546. 0xA7, 0x6B, 0xF5, 0x20, 0xC6, 0x58, 0xCC, 0xAD,
  33547. 0xFA, 0x29, 0x56, 0x42, 0x45, 0x57, 0xAB, 0xEA,
  33548. 0x8A, 0xB8, 0x92, 0x39, 0xC1, 0x78, 0x33, 0xDC,
  33549. 0x3A, 0x49, 0xB3, 0x6A, 0x9A, 0xE9, 0xA4, 0x86,
  33550. 0x94, 0x05, 0x40, 0xEB, 0x44, 0x4F, 0x97, 0x15,
  33551. 0x23, 0x57, 0xE0, 0x20, 0x35, 0x93, 0x9D, 0x75,
  33552. 0xA3, 0xC0, 0x25, 0xF4, 0x1A, 0x40, 0x08, 0x23,
  33553. 0x82, 0xA0, 0x73, 0x3C, 0x39, 0xB0, 0x62, 0x2B,
  33554. 0x74, 0x0E, 0x40, 0x75, 0x92, 0xC6, 0x2E, 0xCA,
  33555. 0xEB, 0x14, 0x32, 0xC4, 0x45, 0xB3, 0x70, 0x3A,
  33556. 0x86, 0xF6, 0x98, 0x1A, 0x27, 0x81, 0x57, 0xEA,
  33557. 0x95, 0xA6, 0xE9, 0x2D, 0x55, 0xE4, 0xB9, 0x72,
  33558. 0xF9, 0x36, 0xC2, 0xF0, 0xA6, 0x58, 0x28, 0x0E,
  33559. 0xA2, 0xB0, 0x7A, 0x48, 0x99, 0x2D, 0xF8, 0x93,
  33560. 0x7E, 0x0A, 0x2A, 0xC1, 0xDC, 0xC9, 0x74, 0xFE,
  33561. 0x00, 0xAA, 0xE1, 0xF5, 0x61, 0xFA, 0x25, 0x8E,
  33562. 0x2D, 0x25, 0x9C, 0x3E, 0x86, 0x1D, 0xCE, 0x23,
  33563. 0x60, 0x39, 0x12, 0x76, 0x06, 0xFC, 0x1C, 0xE0,
  33564. 0x09, 0x00, 0x3A, 0x7B, 0xAC, 0x94, 0x21, 0x01,
  33565. 0xDC, 0xB8, 0x22, 0xB1, 0xF3, 0xC1, 0x2B, 0xF7,
  33566. 0x32, 0x38, 0xF5, 0x46, 0xE0, 0x1C, 0x36, 0xB5,
  33567. 0xA6, 0x93, 0x61, 0x92, 0x99, 0x5C, 0xC6, 0x9C,
  33568. 0x63, 0x23, 0x74, 0x09, 0xCB, 0x53, 0xC2, 0xE3,
  33569. 0x5D, 0x74, 0x89, 0x0D, 0x18, 0x88, 0x53, 0x76,
  33570. 0xFA, 0x55, 0x03, 0xB1, 0x07, 0xA2, 0xA3, 0x92,
  33571. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  33572. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  33573. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  33574. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  33575. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  33576. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  33577. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  33578. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  33579. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  33580. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  33581. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  33582. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  33583. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  33584. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  33585. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  33586. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  33587. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  33588. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  33589. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  33590. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  33591. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  33592. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  33593. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  33594. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  33595. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  33596. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  33597. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  33598. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  33599. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  33600. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  33601. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  33602. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  33603. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  33604. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  33605. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  33606. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  33607. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  33608. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  33609. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  33610. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  33611. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  33612. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  33613. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  33614. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  33615. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  33616. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  33617. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  33618. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  33619. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  33620. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  33621. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  33622. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  33623. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  33624. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  33625. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  33626. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  33627. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  33628. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  33629. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  33630. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  33631. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  33632. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  33633. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  33634. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  33635. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  33636. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  33637. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  33638. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  33639. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  33640. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  33641. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  33642. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  33643. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  33644. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  33645. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  33646. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  33647. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  33648. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  33649. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  33650. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  33651. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  33652. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  33653. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  33654. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  33655. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  33656. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  33657. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  33658. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  33659. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  33660. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  33661. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  33662. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  33663. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  33664. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  33665. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  33666. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  33667. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  33668. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  33669. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  33670. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  33671. 0x7F, 0xFA, 0xD1, 0xBC, 0x8A, 0xF7, 0x3B, 0x7E,
  33672. 0x87, 0x49, 0x56, 0xB8, 0x1C, 0x2A, 0x2E, 0xF0,
  33673. 0xBF, 0xAB, 0xE8, 0xDC, 0x93, 0xD7, 0x7B, 0x2F,
  33674. 0xBC, 0x9E, 0x0C, 0x64, 0xEF, 0xA0, 0x1E, 0x84,
  33675. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  33676. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  33677. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  33678. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  33679. };
  33680. WOLFSSL_SMALL_STACK_STATIC const byte kyber512_ct[] = {
  33681. 0xED, 0xF2, 0x41, 0x45, 0xE4, 0x3B, 0x4F, 0x6D,
  33682. 0xC6, 0xBF, 0x83, 0x32, 0xF5, 0x4E, 0x02, 0xCA,
  33683. 0xB0, 0x2D, 0xBF, 0x3B, 0x56, 0x05, 0xDD, 0xC9,
  33684. 0x0A, 0x15, 0xC8, 0x86, 0xAD, 0x3E, 0xD4, 0x89,
  33685. 0x46, 0x26, 0x99, 0xE4, 0xAB, 0xED, 0x44, 0x35,
  33686. 0x0B, 0xC3, 0x75, 0x7E, 0x26, 0x96, 0xFB, 0xFB,
  33687. 0x25, 0x34, 0x41, 0x2E, 0x8D, 0xD2, 0x01, 0xF1,
  33688. 0xE4, 0x54, 0x0A, 0x39, 0x70, 0xB0, 0x55, 0xFE,
  33689. 0x3B, 0x0B, 0xEC, 0x3A, 0x71, 0xF9, 0xE1, 0x15,
  33690. 0xB3, 0xF9, 0xF3, 0x91, 0x02, 0x06, 0x5B, 0x1C,
  33691. 0xCA, 0x83, 0x14, 0xDC, 0xC7, 0x95, 0xE3, 0xC0,
  33692. 0xE8, 0xFA, 0x98, 0xEE, 0x83, 0xCA, 0x66, 0x28,
  33693. 0x45, 0x70, 0x28, 0xA4, 0xD0, 0x9E, 0x83, 0x9E,
  33694. 0x55, 0x48, 0x62, 0xCF, 0x0B, 0x7B, 0xF5, 0x6C,
  33695. 0x5C, 0x0A, 0x82, 0x9E, 0x86, 0x57, 0x94, 0x79,
  33696. 0x45, 0xFE, 0x9C, 0x22, 0x56, 0x4F, 0xBA, 0xEB,
  33697. 0xC1, 0xB3, 0xAF, 0x35, 0x0D, 0x79, 0x55, 0x50,
  33698. 0x8A, 0x26, 0xD8, 0xA8, 0xEB, 0x54, 0x7B, 0x8B,
  33699. 0x1A, 0x2C, 0xF0, 0x3C, 0xCA, 0x1A, 0xAB, 0xCE,
  33700. 0x6C, 0x34, 0x97, 0x78, 0x3B, 0x64, 0x65, 0xBA,
  33701. 0x0B, 0x6E, 0x7A, 0xCB, 0xA8, 0x21, 0x19, 0x51,
  33702. 0x24, 0xAE, 0xF0, 0x9E, 0x62, 0x83, 0x82, 0xA1,
  33703. 0xF9, 0x14, 0x04, 0x3B, 0xE7, 0x09, 0x6E, 0x95,
  33704. 0x2C, 0xBC, 0x4F, 0xB4, 0xAF, 0xED, 0x13, 0x60,
  33705. 0x90, 0x46, 0x11, 0x7C, 0x01, 0x1F, 0xD7, 0x41,
  33706. 0xEE, 0x28, 0x6C, 0x83, 0x77, 0x16, 0x90, 0xF0,
  33707. 0xAE, 0xB5, 0x0D, 0xA0, 0xD7, 0x12, 0x85, 0xA1,
  33708. 0x79, 0xB2, 0x15, 0xC6, 0x03, 0x6D, 0xEB, 0x78,
  33709. 0x0F, 0x4D, 0x16, 0x76, 0x9F, 0x72, 0xDE, 0x16,
  33710. 0xFD, 0xAD, 0xAC, 0x73, 0xBE, 0xFA, 0x5B, 0xEF,
  33711. 0x89, 0x43, 0x19, 0x7F, 0x44, 0xC5, 0x95, 0x89,
  33712. 0xDC, 0x9F, 0x49, 0x73, 0xDE, 0x14, 0x50, 0xBA,
  33713. 0x1D, 0x0C, 0x32, 0x90, 0xD6, 0xB1, 0xD6, 0x83,
  33714. 0xF2, 0x94, 0xE7, 0x59, 0xC9, 0x54, 0xAB, 0xE8,
  33715. 0xA7, 0xDA, 0x5B, 0x10, 0x54, 0xFD, 0x6D, 0x21,
  33716. 0x32, 0x9B, 0x8E, 0x73, 0xD3, 0x75, 0x6A, 0xFD,
  33717. 0xA0, 0xDC, 0xB1, 0xFC, 0x8B, 0x15, 0x82, 0xD1,
  33718. 0xF9, 0x0C, 0xF2, 0x75, 0xA1, 0x02, 0xAB, 0xC6,
  33719. 0xAC, 0x69, 0x9D, 0xF0, 0xC5, 0x87, 0x0E, 0x50,
  33720. 0xA1, 0xF9, 0x89, 0xE4, 0xE6, 0x24, 0x1B, 0x60,
  33721. 0xAA, 0xA2, 0xEC, 0xF9, 0xE8, 0xE3, 0x3E, 0x0F,
  33722. 0xFC, 0xF4, 0x0F, 0xE8, 0x31, 0xE8, 0xFD, 0xC2,
  33723. 0xE8, 0x3B, 0x52, 0xCA, 0x7A, 0xB6, 0xD9, 0x3F,
  33724. 0x14, 0x6D, 0x29, 0xDC, 0xA5, 0x3C, 0x7D, 0xA1,
  33725. 0xDB, 0x4A, 0xC4, 0xF2, 0xDB, 0x39, 0xEA, 0x12,
  33726. 0x0D, 0x90, 0xFA, 0x60, 0xF4, 0xD4, 0x37, 0xC6,
  33727. 0xD0, 0x0E, 0xF4, 0x83, 0xBC, 0x94, 0xA3, 0x17,
  33728. 0x5C, 0xDA, 0x16, 0x3F, 0xC1, 0xC2, 0x82, 0x8B,
  33729. 0xE4, 0xDB, 0xD6, 0x43, 0x05, 0x07, 0xB5, 0x84,
  33730. 0xBB, 0x51, 0x77, 0xE1, 0x71, 0xB8, 0xDD, 0xA9,
  33731. 0xA4, 0x29, 0x3C, 0x32, 0x00, 0x29, 0x5C, 0x80,
  33732. 0x3A, 0x86, 0x5D, 0x6D, 0x21, 0x66, 0xF6, 0x6B,
  33733. 0xA5, 0x40, 0x1F, 0xB7, 0xA0, 0xE8, 0x53, 0x16,
  33734. 0x86, 0x00, 0xA2, 0x94, 0x84, 0x37, 0xE0, 0x36,
  33735. 0xE3, 0xBF, 0x19, 0xE1, 0x2F, 0xD3, 0xF2, 0xA2,
  33736. 0xB8, 0xB3, 0x43, 0xF7, 0x84, 0x24, 0x8E, 0x8D,
  33737. 0x68, 0x5E, 0xB0, 0xAF, 0xDE, 0x63, 0x15, 0x33,
  33738. 0x87, 0x30, 0xE7, 0xA1, 0x00, 0x1C, 0x27, 0xD8,
  33739. 0xD2, 0xA7, 0x6F, 0xA6, 0x9D, 0x15, 0x7B, 0xA1,
  33740. 0xAC, 0x7A, 0xD5, 0x6D, 0xA5, 0xA8, 0xC7, 0x0F,
  33741. 0xE4, 0xB5, 0xB8, 0xD7, 0x86, 0xDC, 0x6F, 0xC0,
  33742. 0x56, 0x6B, 0xA8, 0xE1, 0xB8, 0x81, 0x63, 0x34,
  33743. 0xD3, 0x2A, 0x3F, 0xB1, 0xCE, 0x7D, 0x4D, 0x5E,
  33744. 0x4C, 0x33, 0x2A, 0xF7, 0xB0, 0x03, 0xD0, 0x91,
  33745. 0x74, 0x1A, 0x3D, 0x5C, 0x96, 0x52, 0x92, 0x25,
  33746. 0x5D, 0xFF, 0x8E, 0xD2, 0xBB, 0xF1, 0xF9, 0x11,
  33747. 0x6B, 0xE5, 0x0C, 0x17, 0xB8, 0xE5, 0x48, 0x74,
  33748. 0x8A, 0xD4, 0xB2, 0xE9, 0x57, 0xBB, 0xD1, 0x95,
  33749. 0x34, 0x82, 0xA2, 0xE1, 0x71, 0x8C, 0xEC, 0x66,
  33750. 0xCD, 0x2C, 0x81, 0xF5, 0x72, 0xD5, 0x52, 0xB7,
  33751. 0x18, 0x78, 0x85, 0xE6, 0xB8, 0x94, 0x3D, 0x64,
  33752. 0x31, 0x41, 0x3C, 0x59, 0xEB, 0xB7, 0xE0, 0x36,
  33753. 0x04, 0x84, 0x90, 0xBE, 0x52, 0x89, 0xE9, 0x5B,
  33754. 0x20, 0xA8, 0x9E, 0x8B, 0x15, 0x9F, 0x61, 0xA9,
  33755. 0xA9, 0x88, 0x6E, 0x14, 0x75, 0x68, 0xF4, 0xC9,
  33756. 0x02, 0x1F, 0x36, 0x2F, 0x02, 0x68, 0x8A, 0x1C,
  33757. 0x8C, 0x3B, 0xB0, 0xD2, 0x40, 0x86, 0x88, 0x0E,
  33758. 0x55, 0xB6, 0xED, 0xB4, 0x3F, 0x37, 0x45, 0xD2,
  33759. 0xC1, 0x66, 0xDC, 0x1C, 0xB7, 0x43, 0xC7, 0x6F,
  33760. 0xE6, 0xBE, 0x52, 0x3A, 0x89, 0x3C, 0xC7, 0x64,
  33761. 0xD1, 0x64, 0x35, 0xC3, 0x78, 0x51, 0x25, 0x2A,
  33762. 0x81, 0xE2, 0xFF, 0xBA, 0x0F, 0x18, 0x97, 0x1A,
  33763. 0x3D, 0xEE, 0x37, 0xD4, 0x87, 0x7C, 0xB9, 0x28,
  33764. 0xE3, 0x6E, 0x52, 0x35, 0x03, 0x7A, 0x6B, 0x20,
  33765. 0x57, 0x89, 0x7D, 0x51, 0x8A, 0x5F, 0x0E, 0x34,
  33766. 0x8E, 0x3A, 0xB6, 0xD5, 0xB5, 0x2D, 0xFC, 0x60,
  33767. 0x75, 0x7F, 0x3B, 0x41, 0xA4, 0xFE, 0xC7, 0x82,
  33768. 0x8F, 0x1D, 0xEE, 0xAF, 0x45, 0x87, 0xCC, 0xC8,
  33769. 0xEA, 0xDF, 0x64, 0x7F, 0x4D, 0x20, 0x3B, 0x2F,
  33770. 0xAA, 0x05, 0xA6, 0x49, 0xB5, 0x82, 0x34, 0x0C,
  33771. 0xB4, 0xCA, 0xCE, 0x57, 0xA3, 0x07, 0x11, 0xBE,
  33772. 0x75, 0x2F, 0xAC, 0xF0, 0x22, 0x7D, 0x0A, 0x80,
  33773. 0xC4, 0x12, 0x84, 0x42, 0xDD, 0xC5, 0x44, 0xBE,
  33774. 0x80, 0x5B, 0x9C, 0xFE, 0x8F, 0xE9, 0xB1, 0x23,
  33775. 0x7C, 0x80, 0xF9, 0x67, 0x87, 0xCD, 0x92, 0x81,
  33776. 0xCC, 0xF2, 0x70, 0xC1, 0xAF, 0xC0, 0x67, 0x0D
  33777. };
  33778. WOLFSSL_SMALL_STACK_STATIC const byte kyber512_ss[] = {
  33779. 0x0A, 0x69, 0x25, 0x67, 0x6F, 0x24, 0xB2, 0x2C,
  33780. 0x28, 0x6F, 0x4C, 0x81, 0xA4, 0x22, 0x4C, 0xEC,
  33781. 0x50, 0x6C, 0x9B, 0x25, 0x7D, 0x48, 0x0E, 0x02,
  33782. 0xE3, 0xB4, 0x9F, 0x44, 0xCA, 0xA3, 0x23, 0x7F
  33783. };
  33784. #ifdef WOLFSSL_SMALL_STACK
  33785. key = (KyberKey *)XMALLOC(sizeof(KyberKey), HEAP_HINT,
  33786. DYNAMIC_TYPE_TMP_BUFFER);
  33787. priv = (byte *)XMALLOC(KYBER512_PRIVATE_KEY_SIZE, HEAP_HINT,
  33788. DYNAMIC_TYPE_TMP_BUFFER);
  33789. pub = (byte *)XMALLOC(KYBER512_PUBLIC_KEY_SIZE, HEAP_HINT,
  33790. DYNAMIC_TYPE_TMP_BUFFER);
  33791. ct = (byte *)XMALLOC(KYBER512_CIPHER_TEXT_SIZE, HEAP_HINT,
  33792. DYNAMIC_TYPE_TMP_BUFFER);
  33793. ss = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  33794. DYNAMIC_TYPE_TMP_BUFFER);
  33795. ss_dec = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  33796. DYNAMIC_TYPE_TMP_BUFFER);
  33797. if (! (key && priv && pub && ct && ss && ss_dec))
  33798. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  33799. #endif
  33800. ret = wc_KyberKey_Init(KYBER512, key, HEAP_HINT, INVALID_DEVID);
  33801. if (ret != 0)
  33802. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33803. else
  33804. key_inited = 1;
  33805. ret = wc_KyberKey_MakeKeyWithRandom(key, kyber512_rand,
  33806. sizeof(kyber512_rand));
  33807. if (ret != 0)
  33808. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33809. ret = wc_KyberKey_EncodePublicKey(key, pub, KYBER512_PUBLIC_KEY_SIZE);
  33810. if (ret != 0)
  33811. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33812. ret = wc_KyberKey_EncodePrivateKey(key, priv, KYBER512_PRIVATE_KEY_SIZE);
  33813. if (ret != 0)
  33814. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33815. if (XMEMCMP(pub, kyber512_pk, sizeof(kyber512_pk)) != 0)
  33816. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33817. if (XMEMCMP(priv, kyber512_sk, sizeof(kyber512_sk)) != 0)
  33818. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33819. ret = wc_KyberKey_EncapsulateWithRandom(key, ct, ss, kyber512enc_rand,
  33820. sizeof(kyber512enc_rand));
  33821. if (ret != 0)
  33822. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33823. if (XMEMCMP(ct, kyber512_ct, sizeof(kyber512_ct)) != 0)
  33824. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33825. if (XMEMCMP(ss, kyber512_ss, sizeof(kyber512_ss)) != 0)
  33826. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33827. ret = wc_KyberKey_Decapsulate(key, ss_dec, ct, sizeof(kyber512_ct));
  33828. if (ret != 0)
  33829. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33830. if (XMEMCMP(ss_dec, kyber512_ss, sizeof(kyber512_ss)) != 0)
  33831. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33832. out:
  33833. if (key_inited)
  33834. wc_KyberKey_Free(key);
  33835. #ifdef WOLFSSL_SMALL_STACK
  33836. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33837. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33838. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33839. XFREE(ct, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33840. XFREE(ss, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33841. XFREE(ss_dec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33842. #endif
  33843. return ret;
  33844. }
  33845. #endif /* WOLFSSL_KYBER512 */
  33846. #ifdef WOLFSSL_KYBER768
  33847. static wc_test_ret_t kyber768_kat(void)
  33848. {
  33849. wc_test_ret_t ret;
  33850. #ifdef WOLFSSL_SMALL_STACK
  33851. KyberKey *key = NULL;
  33852. byte *priv = NULL;
  33853. byte *pub = NULL;
  33854. byte *ct = NULL;
  33855. byte *ss = NULL;
  33856. byte *ss_dec = NULL;
  33857. #else
  33858. KyberKey key[1];
  33859. byte priv[KYBER768_PRIVATE_KEY_SIZE];
  33860. byte pub[KYBER768_PUBLIC_KEY_SIZE];
  33861. byte ct[KYBER768_CIPHER_TEXT_SIZE];
  33862. byte ss[KYBER_SS_SZ];
  33863. byte ss_dec[KYBER_SS_SZ];
  33864. #endif
  33865. int key_inited = 0;
  33866. WOLFSSL_SMALL_STACK_STATIC const byte kyber768_rand[] = {
  33867. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  33868. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  33869. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  33870. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  33871. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  33872. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  33873. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  33874. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  33875. };
  33876. WOLFSSL_SMALL_STACK_STATIC const byte kyber768enc_rand[] = {
  33877. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  33878. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  33879. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  33880. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  33881. };
  33882. WOLFSSL_SMALL_STACK_STATIC const byte kyber768_pk[] = {
  33883. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  33884. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  33885. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  33886. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  33887. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  33888. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  33889. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  33890. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  33891. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  33892. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  33893. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  33894. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  33895. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  33896. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  33897. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  33898. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  33899. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  33900. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  33901. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  33902. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  33903. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  33904. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  33905. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  33906. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  33907. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  33908. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  33909. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  33910. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  33911. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  33912. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  33913. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  33914. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  33915. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  33916. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  33917. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  33918. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  33919. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  33920. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  33921. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  33922. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  33923. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  33924. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  33925. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  33926. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  33927. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  33928. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  33929. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  33930. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  33931. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  33932. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  33933. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  33934. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  33935. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  33936. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  33937. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  33938. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  33939. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  33940. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  33941. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  33942. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  33943. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  33944. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  33945. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  33946. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  33947. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  33948. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  33949. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  33950. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  33951. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  33952. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  33953. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  33954. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  33955. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  33956. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  33957. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  33958. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  33959. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  33960. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  33961. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  33962. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  33963. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  33964. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  33965. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  33966. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  33967. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  33968. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  33969. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  33970. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  33971. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  33972. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  33973. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  33974. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  33975. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  33976. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  33977. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  33978. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  33979. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  33980. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  33981. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  33982. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  33983. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  33984. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  33985. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  33986. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  33987. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  33988. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  33989. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  33990. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  33991. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  33992. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  33993. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  33994. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  33995. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  33996. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  33997. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  33998. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  33999. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  34000. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  34001. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  34002. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  34003. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  34004. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  34005. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  34006. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  34007. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  34008. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  34009. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  34010. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  34011. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  34012. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  34013. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  34014. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  34015. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  34016. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  34017. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  34018. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  34019. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  34020. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  34021. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  34022. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  34023. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  34024. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  34025. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  34026. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  34027. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  34028. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  34029. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  34030. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  34031. };
  34032. WOLFSSL_SMALL_STACK_STATIC const byte kyber768_sk[] = {
  34033. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  34034. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  34035. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  34036. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  34037. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  34038. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  34039. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  34040. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  34041. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  34042. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  34043. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  34044. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  34045. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  34046. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  34047. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  34048. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  34049. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  34050. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  34051. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  34052. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  34053. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  34054. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  34055. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  34056. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  34057. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  34058. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  34059. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  34060. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  34061. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  34062. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  34063. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  34064. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  34065. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  34066. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  34067. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  34068. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  34069. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  34070. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  34071. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  34072. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  34073. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  34074. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  34075. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  34076. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  34077. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  34078. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  34079. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  34080. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  34081. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  34082. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  34083. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  34084. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  34085. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  34086. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  34087. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  34088. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  34089. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  34090. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  34091. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  34092. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  34093. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  34094. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  34095. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  34096. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  34097. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  34098. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  34099. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  34100. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  34101. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  34102. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  34103. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  34104. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  34105. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  34106. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  34107. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  34108. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  34109. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  34110. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  34111. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  34112. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  34113. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  34114. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  34115. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  34116. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  34117. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  34118. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  34119. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  34120. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  34121. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  34122. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  34123. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  34124. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  34125. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  34126. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  34127. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  34128. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  34129. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  34130. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  34131. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  34132. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  34133. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  34134. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  34135. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  34136. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  34137. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  34138. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  34139. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  34140. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  34141. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  34142. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  34143. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  34144. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  34145. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  34146. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  34147. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  34148. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  34149. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  34150. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  34151. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  34152. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  34153. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  34154. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  34155. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  34156. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  34157. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  34158. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  34159. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  34160. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  34161. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  34162. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  34163. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  34164. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  34165. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  34166. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  34167. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  34168. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  34169. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  34170. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  34171. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  34172. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  34173. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  34174. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  34175. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  34176. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  34177. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  34178. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  34179. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  34180. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  34181. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  34182. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  34183. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  34184. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  34185. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  34186. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  34187. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  34188. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  34189. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  34190. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  34191. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  34192. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  34193. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  34194. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  34195. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  34196. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  34197. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  34198. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  34199. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  34200. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  34201. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  34202. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  34203. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  34204. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  34205. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  34206. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  34207. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  34208. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  34209. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  34210. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  34211. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  34212. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  34213. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  34214. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  34215. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  34216. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  34217. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  34218. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  34219. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  34220. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  34221. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  34222. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  34223. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  34224. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  34225. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  34226. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  34227. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  34228. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  34229. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  34230. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  34231. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  34232. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  34233. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  34234. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  34235. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  34236. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  34237. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  34238. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  34239. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  34240. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  34241. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  34242. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  34243. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  34244. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  34245. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  34246. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  34247. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  34248. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  34249. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  34250. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  34251. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  34252. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  34253. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  34254. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  34255. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  34256. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  34257. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  34258. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  34259. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  34260. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  34261. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  34262. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  34263. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  34264. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  34265. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  34266. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  34267. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  34268. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  34269. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  34270. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  34271. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  34272. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  34273. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  34274. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  34275. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  34276. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  34277. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  34278. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  34279. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  34280. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  34281. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  34282. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  34283. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  34284. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  34285. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  34286. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  34287. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  34288. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  34289. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  34290. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  34291. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  34292. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  34293. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  34294. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  34295. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  34296. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  34297. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  34298. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  34299. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  34300. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  34301. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  34302. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  34303. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  34304. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  34305. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  34306. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  34307. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  34308. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  34309. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  34310. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  34311. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  34312. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  34313. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  34314. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  34315. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  34316. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  34317. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  34318. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  34319. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  34320. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  34321. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  34322. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  34323. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  34324. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  34325. 0xD4, 0xEC, 0x14, 0x3B, 0x50, 0xF0, 0x14, 0x23,
  34326. 0xB1, 0x77, 0x89, 0x5E, 0xDE, 0xE2, 0x2B, 0xB7,
  34327. 0x39, 0xF6, 0x47, 0xEC, 0xF8, 0x5F, 0x50, 0xBC,
  34328. 0x25, 0xEF, 0x7B, 0x5A, 0x72, 0x5D, 0xEE, 0x86,
  34329. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  34330. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  34331. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  34332. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  34333. };
  34334. WOLFSSL_SMALL_STACK_STATIC const byte kyber768_ct[] = {
  34335. 0xB5, 0x2C, 0x56, 0xB9, 0x2A, 0x4B, 0x7C, 0xE9,
  34336. 0xE4, 0xCB, 0x7C, 0x5B, 0x1B, 0x16, 0x31, 0x67,
  34337. 0xA8, 0xA1, 0x67, 0x5B, 0x2F, 0xDE, 0xF8, 0x4A,
  34338. 0x5B, 0x67, 0xCA, 0x15, 0xDB, 0x69, 0x4C, 0x9F,
  34339. 0x11, 0xBD, 0x02, 0x7C, 0x30, 0xAE, 0x22, 0xEC,
  34340. 0x92, 0x1A, 0x1D, 0x91, 0x15, 0x99, 0xAF, 0x05,
  34341. 0x85, 0xE4, 0x8D, 0x20, 0xDA, 0x70, 0xDF, 0x9F,
  34342. 0x39, 0xE3, 0x2E, 0xF9, 0x5D, 0x4C, 0x8F, 0x44,
  34343. 0xBF, 0xEF, 0xDA, 0xA5, 0xDA, 0x64, 0xF1, 0x05,
  34344. 0x46, 0x31, 0xD0, 0x4D, 0x6D, 0x3C, 0xFD, 0x0A,
  34345. 0x54, 0x0D, 0xD7, 0xBA, 0x38, 0x86, 0xE4, 0xB5,
  34346. 0xF1, 0x3E, 0x87, 0x87, 0x88, 0x60, 0x4C, 0x95,
  34347. 0xC0, 0x96, 0xEA, 0xB3, 0x91, 0x9F, 0x42, 0x75,
  34348. 0x21, 0x41, 0x9A, 0x94, 0x6C, 0x26, 0xCC, 0x04,
  34349. 0x14, 0x75, 0xD7, 0x12, 0x4C, 0xDC, 0x01, 0xD0,
  34350. 0x37, 0x3E, 0x5B, 0x09, 0xC7, 0xA7, 0x06, 0x03,
  34351. 0xCF, 0xDB, 0x4F, 0xB3, 0x40, 0x50, 0x23, 0xF2,
  34352. 0x26, 0x4D, 0xC3, 0xF9, 0x83, 0xC4, 0xFC, 0x02,
  34353. 0xA2, 0xD1, 0xB2, 0x68, 0xF2, 0x20, 0x8A, 0x1F,
  34354. 0x6E, 0x2A, 0x62, 0x09, 0xBF, 0xF1, 0x2F, 0x6F,
  34355. 0x46, 0x5F, 0x0B, 0x06, 0x9C, 0x3A, 0x7F, 0x84,
  34356. 0xF6, 0x06, 0xD8, 0xA9, 0x40, 0x64, 0x00, 0x3D,
  34357. 0x6E, 0xC1, 0x14, 0xC8, 0xE8, 0x08, 0xD3, 0x05,
  34358. 0x38, 0x84, 0xC1, 0xD5, 0xA1, 0x42, 0xFB, 0xF2,
  34359. 0x01, 0x12, 0xEB, 0x36, 0x0F, 0xDA, 0x3F, 0x0F,
  34360. 0x28, 0xB1, 0x72, 0xAE, 0x50, 0xF5, 0xE7, 0xD8,
  34361. 0x38, 0x01, 0xFB, 0x3F, 0x00, 0x64, 0xB6, 0x87,
  34362. 0x18, 0x70, 0x74, 0xBD, 0x7F, 0xE3, 0x0E, 0xDD,
  34363. 0xAA, 0x33, 0x4C, 0xF8, 0xFC, 0x04, 0xFA, 0x8C,
  34364. 0xED, 0x89, 0x9C, 0xEA, 0xDE, 0x4B, 0x4F, 0x28,
  34365. 0xB6, 0x83, 0x72, 0xBA, 0xF9, 0x8F, 0xF4, 0x82,
  34366. 0xA4, 0x15, 0xB7, 0x31, 0x15, 0x5B, 0x75, 0xCE,
  34367. 0xB9, 0x76, 0xBE, 0x0E, 0xA0, 0x28, 0x5B, 0xA0,
  34368. 0x1A, 0x27, 0xF1, 0x85, 0x7A, 0x8F, 0xB3, 0x77,
  34369. 0xA3, 0xAE, 0x0C, 0x23, 0xB2, 0xAA, 0x9A, 0x07,
  34370. 0x9B, 0xFA, 0xBF, 0xF0, 0xD5, 0xB2, 0xF1, 0xCD,
  34371. 0x9B, 0x71, 0x8B, 0xEA, 0x03, 0xC4, 0x2F, 0x34,
  34372. 0x3A, 0x39, 0xB4, 0xF1, 0x42, 0xD0, 0x1A, 0xD8,
  34373. 0xAC, 0xBB, 0x50, 0xE3, 0x88, 0x53, 0xCF, 0x9A,
  34374. 0x50, 0xC8, 0xB4, 0x4C, 0x3C, 0xF6, 0x71, 0xA4,
  34375. 0xA9, 0x04, 0x3B, 0x26, 0xDD, 0xBB, 0x24, 0x95,
  34376. 0x9A, 0xD6, 0x71, 0x5C, 0x08, 0x52, 0x18, 0x55,
  34377. 0xC7, 0x9A, 0x23, 0xB9, 0xC3, 0xD6, 0x47, 0x17,
  34378. 0x49, 0xC4, 0x07, 0x25, 0xBD, 0xD5, 0xC2, 0x77,
  34379. 0x6D, 0x43, 0xAE, 0xD2, 0x02, 0x04, 0xBA, 0xA1,
  34380. 0x41, 0xEF, 0xB3, 0x30, 0x49, 0x17, 0x47, 0x4B,
  34381. 0x7F, 0x9F, 0x7A, 0x4B, 0x08, 0xB1, 0xA9, 0x3D,
  34382. 0xAE, 0xD9, 0x8C, 0x67, 0x49, 0x53, 0x59, 0xD3,
  34383. 0x7D, 0x67, 0xF7, 0x43, 0x8B, 0xEE, 0x5E, 0x43,
  34384. 0x58, 0x56, 0x34, 0xB2, 0x6C, 0x6B, 0x38, 0x10,
  34385. 0xD7, 0xCD, 0xCB, 0xC0, 0xF6, 0xEB, 0x87, 0x7A,
  34386. 0x60, 0x87, 0xE6, 0x8A, 0xCB, 0x84, 0x80, 0xD3,
  34387. 0xA8, 0xCF, 0x69, 0x00, 0x44, 0x7E, 0x49, 0xB4,
  34388. 0x17, 0xF1, 0x5A, 0x53, 0xB6, 0x07, 0xA0, 0xE2,
  34389. 0x16, 0xB8, 0x55, 0x97, 0x0D, 0x37, 0x40, 0x68,
  34390. 0x70, 0xB4, 0x56, 0x87, 0x22, 0xDA, 0x77, 0xA4,
  34391. 0x08, 0x47, 0x03, 0x81, 0x67, 0x84, 0xE2, 0xF1,
  34392. 0x6B, 0xED, 0x18, 0x99, 0x65, 0x32, 0xC5, 0xD8,
  34393. 0xB7, 0xF5, 0xD2, 0x14, 0x46, 0x4E, 0x5F, 0x3F,
  34394. 0x6E, 0x90, 0x58, 0x67, 0xB0, 0xCE, 0x11, 0x9E,
  34395. 0x25, 0x2A, 0x66, 0x71, 0x32, 0x53, 0x54, 0x46,
  34396. 0x85, 0xD2, 0x08, 0xE1, 0x72, 0x39, 0x08, 0xA0,
  34397. 0xCE, 0x97, 0x83, 0x46, 0x52, 0xE0, 0x8A, 0xE7,
  34398. 0xBD, 0xC8, 0x81, 0xA1, 0x31, 0xB7, 0x3C, 0x71,
  34399. 0xE8, 0x4D, 0x20, 0xD6, 0x8F, 0xDE, 0xFF, 0x4F,
  34400. 0x5D, 0x70, 0xCD, 0x1A, 0xF5, 0x7B, 0x78, 0xE3,
  34401. 0x49, 0x1A, 0x98, 0x65, 0x94, 0x23, 0x21, 0x80,
  34402. 0x0A, 0x20, 0x3C, 0x05, 0xED, 0x1F, 0xEE, 0xB5,
  34403. 0xA2, 0x8E, 0x58, 0x4E, 0x19, 0xF6, 0x53, 0x5E,
  34404. 0x7F, 0x84, 0xE4, 0xA2, 0x4F, 0x84, 0xA7, 0x2D,
  34405. 0xCA, 0xF5, 0x64, 0x8B, 0x4A, 0x42, 0x35, 0xDD,
  34406. 0x66, 0x44, 0x64, 0x48, 0x2F, 0x03, 0x17, 0x6E,
  34407. 0x88, 0x8C, 0x28, 0xBF, 0xC6, 0xC1, 0xCB, 0x23,
  34408. 0x8C, 0xFF, 0xA3, 0x5A, 0x32, 0x1E, 0x71, 0x79,
  34409. 0x1D, 0x9E, 0xA8, 0xED, 0x08, 0x78, 0xC6, 0x11,
  34410. 0x21, 0xBF, 0x8D, 0x2A, 0x4A, 0xB2, 0xC1, 0xA5,
  34411. 0xE1, 0x20, 0xBC, 0x40, 0xAB, 0xB1, 0x89, 0x2D,
  34412. 0x17, 0x15, 0x09, 0x0A, 0x0E, 0xE4, 0x82, 0x52,
  34413. 0xCA, 0x29, 0x7A, 0x99, 0xAA, 0x0E, 0x51, 0x0C,
  34414. 0xF2, 0x6B, 0x1A, 0xDD, 0x06, 0xCA, 0x54, 0x3E,
  34415. 0x1C, 0x5D, 0x6B, 0xDC, 0xD3, 0xB9, 0xC5, 0x85,
  34416. 0xC8, 0x53, 0x80, 0x45, 0xDB, 0x5C, 0x25, 0x2E,
  34417. 0xC3, 0xC8, 0xC3, 0xC9, 0x54, 0xD9, 0xBE, 0x59,
  34418. 0x07, 0x09, 0x4A, 0x89, 0x4E, 0x60, 0xEA, 0xB4,
  34419. 0x35, 0x38, 0xCF, 0xEE, 0x82, 0xE8, 0xFF, 0xC0,
  34420. 0x79, 0x1B, 0x0D, 0x0F, 0x43, 0xAC, 0x16, 0x27,
  34421. 0x83, 0x0A, 0x61, 0xD5, 0x6D, 0xAD, 0x96, 0xC6,
  34422. 0x29, 0x58, 0xB0, 0xDE, 0x78, 0x0B, 0x78, 0xBD,
  34423. 0x47, 0xA6, 0x04, 0x55, 0x0D, 0xAB, 0x83, 0xFF,
  34424. 0xF2, 0x27, 0xC3, 0x24, 0x04, 0x94, 0x71, 0xF3,
  34425. 0x52, 0x48, 0xCF, 0xB8, 0x49, 0xB2, 0x57, 0x24,
  34426. 0xFF, 0x70, 0x4D, 0x52, 0x77, 0xAA, 0x35, 0x2D,
  34427. 0x55, 0x09, 0x58, 0xBE, 0x3B, 0x23, 0x7D, 0xFF,
  34428. 0x47, 0x3E, 0xC2, 0xAD, 0xBA, 0xEA, 0x48, 0xCA,
  34429. 0x26, 0x58, 0xAE, 0xFC, 0xC7, 0x7B, 0xBD, 0x42,
  34430. 0x64, 0xAB, 0x37, 0x4D, 0x70, 0xEA, 0xE5, 0xB9,
  34431. 0x64, 0x41, 0x6C, 0xE8, 0x22, 0x6A, 0x7E, 0x32,
  34432. 0x55, 0xA0, 0xF8, 0xD7, 0xE2, 0xAD, 0xCA, 0x06,
  34433. 0x2B, 0xCD, 0x6D, 0x78, 0xD6, 0x0D, 0x1B, 0x32,
  34434. 0xE1, 0x14, 0x05, 0xBE, 0x54, 0xB6, 0x6E, 0xF0,
  34435. 0xFD, 0xDD, 0x56, 0x77, 0x02, 0xA3, 0xBC, 0xCF,
  34436. 0xED, 0xE3, 0xC5, 0x84, 0x70, 0x12, 0x69, 0xED,
  34437. 0x14, 0x80, 0x9F, 0x06, 0xF8, 0x96, 0x83, 0x56,
  34438. 0xBB, 0x92, 0x67, 0xFE, 0x86, 0xE5, 0x14, 0x25,
  34439. 0x2E, 0x88, 0xBB, 0x5C, 0x30, 0xA7, 0xEC, 0xB3,
  34440. 0xD0, 0xE6, 0x21, 0x02, 0x1E, 0xE0, 0xFB, 0xF7,
  34441. 0x87, 0x1B, 0x09, 0x34, 0x2B, 0xF8, 0x4F, 0x55,
  34442. 0xC9, 0x7E, 0xAF, 0x86, 0xC4, 0x81, 0x89, 0xC7,
  34443. 0xFF, 0x4D, 0xF3, 0x89, 0xF0, 0x77, 0xE2, 0x80,
  34444. 0x6E, 0x5F, 0xA7, 0x3B, 0x3E, 0x94, 0x58, 0xA1,
  34445. 0x6C, 0x7E, 0x27, 0x5F, 0x4F, 0x60, 0x22, 0x75,
  34446. 0x58, 0x0E, 0xB7, 0xB7, 0x13, 0x5F, 0xB5, 0x37,
  34447. 0xFA, 0x0C, 0xD9, 0x5D, 0x6E, 0xA5, 0x8C, 0x10,
  34448. 0x8C, 0xD8, 0x94, 0x3D, 0x70, 0xC1, 0x64, 0x31,
  34449. 0x11, 0xF4, 0xF0, 0x1C, 0xA8, 0xA8, 0x27, 0x6A,
  34450. 0x90, 0x26, 0x66, 0xED, 0x81, 0xB7, 0x8D, 0x16,
  34451. 0x8B, 0x00, 0x6F, 0x16, 0xAA, 0xA3, 0xD8, 0xE4,
  34452. 0xCE, 0x4F, 0x4D, 0x0F, 0xB0, 0x99, 0x7E, 0x41,
  34453. 0xAE, 0xFF, 0xB5, 0xB3, 0xDA, 0xA8, 0x38, 0x73,
  34454. 0x2F, 0x35, 0x73, 0x49, 0x44, 0x7F, 0x38, 0x77,
  34455. 0x76, 0xC7, 0x93, 0xC0, 0x47, 0x9D, 0xE9, 0xE9,
  34456. 0x94, 0x98, 0xCC, 0x35, 0x6F, 0xDB, 0x00, 0x75,
  34457. 0xA7, 0x03, 0xF2, 0x3C, 0x55, 0xD4, 0x7B, 0x55,
  34458. 0x0E, 0xC8, 0x9B, 0x02, 0xAD, 0xE8, 0x93, 0x29,
  34459. 0x08, 0x6A, 0x50, 0x84, 0x34, 0x56, 0xFE, 0xDC,
  34460. 0x37, 0x88, 0xAC, 0x8D, 0x97, 0x23, 0x3C, 0x54,
  34461. 0x56, 0x04, 0x67, 0xEE, 0x1D, 0x0F, 0x02, 0x4B,
  34462. 0x18, 0x42, 0x8F, 0x0D, 0x73, 0xB3, 0x0E, 0x19,
  34463. 0xF5, 0xC6, 0x3B, 0x9A, 0xBF, 0x11, 0x41, 0x5B,
  34464. 0xEA, 0x4D, 0x01, 0x70, 0x13, 0x0B, 0xAA, 0xBD,
  34465. 0x33, 0xC0, 0x5E, 0x65, 0x24, 0xE5, 0xFB, 0x55,
  34466. 0x81, 0xB2, 0x2B, 0x04, 0x33, 0x34, 0x22, 0x48,
  34467. 0x26, 0x6D, 0x0F, 0x10, 0x53, 0xB2, 0x45, 0xCC,
  34468. 0x24, 0x62, 0xDC, 0x44, 0xD3, 0x49, 0x65, 0x10,
  34469. 0x24, 0x82, 0xA8, 0xED, 0x9E, 0x4E, 0x96, 0x4D,
  34470. 0x56, 0x83, 0xE5, 0xD4, 0x5D, 0x0C, 0x82, 0x69
  34471. };
  34472. WOLFSSL_SMALL_STACK_STATIC const byte kyber768_ss[] = {
  34473. 0x91, 0x4C, 0xB6, 0x7F, 0xE5, 0xC3, 0x8E, 0x73,
  34474. 0xBF, 0x74, 0x18, 0x1C, 0x0A, 0xC5, 0x04, 0x28,
  34475. 0xDE, 0xDF, 0x77, 0x50, 0xA9, 0x80, 0x58, 0xF7,
  34476. 0xD5, 0x36, 0x70, 0x87, 0x74, 0x53, 0x5B, 0x29
  34477. };
  34478. #ifdef WOLFSSL_SMALL_STACK
  34479. key = (KyberKey *)XMALLOC(sizeof(KyberKey), HEAP_HINT,
  34480. DYNAMIC_TYPE_TMP_BUFFER);
  34481. priv = (byte *)XMALLOC(KYBER768_PRIVATE_KEY_SIZE, HEAP_HINT,
  34482. DYNAMIC_TYPE_TMP_BUFFER);
  34483. pub = (byte *)XMALLOC(KYBER768_PUBLIC_KEY_SIZE, HEAP_HINT,
  34484. DYNAMIC_TYPE_TMP_BUFFER);
  34485. ct = (byte *)XMALLOC(KYBER768_CIPHER_TEXT_SIZE, HEAP_HINT,
  34486. DYNAMIC_TYPE_TMP_BUFFER);
  34487. ss = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  34488. DYNAMIC_TYPE_TMP_BUFFER);
  34489. ss_dec = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  34490. DYNAMIC_TYPE_TMP_BUFFER);
  34491. if (! (key && priv && pub && ct && ss && ss_dec))
  34492. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  34493. #endif
  34494. ret = wc_KyberKey_Init(KYBER768, key, HEAP_HINT, INVALID_DEVID);
  34495. if (ret != 0)
  34496. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34497. else
  34498. key_inited = 1;
  34499. ret = wc_KyberKey_MakeKeyWithRandom(key, kyber768_rand,
  34500. sizeof(kyber768_rand));
  34501. if (ret != 0)
  34502. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34503. ret = wc_KyberKey_EncodePublicKey(key, pub, KYBER768_PUBLIC_KEY_SIZE);
  34504. if (ret != 0)
  34505. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34506. ret = wc_KyberKey_EncodePrivateKey(key, priv, KYBER768_PRIVATE_KEY_SIZE);
  34507. if (ret != 0)
  34508. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34509. if (XMEMCMP(pub, kyber768_pk, sizeof(kyber768_pk)) != 0)
  34510. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34511. if (XMEMCMP(priv, kyber768_sk, sizeof(kyber768_sk)) != 0)
  34512. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34513. ret = wc_KyberKey_EncapsulateWithRandom(key, ct, ss, kyber768enc_rand,
  34514. sizeof(kyber768enc_rand));
  34515. if (ret != 0)
  34516. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34517. if (XMEMCMP(ct, kyber768_ct, sizeof(kyber768_ct)) != 0)
  34518. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34519. if (XMEMCMP(ss, kyber768_ss, sizeof(kyber768_ss)) != 0)
  34520. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34521. ret = wc_KyberKey_Decapsulate(key, ss_dec, ct, sizeof(kyber768_ct));
  34522. if (ret != 0)
  34523. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34524. if (XMEMCMP(ss_dec, kyber768_ss, sizeof(kyber768_ss)) != 0)
  34525. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34526. out:
  34527. if (key_inited)
  34528. wc_KyberKey_Free(key);
  34529. #ifdef WOLFSSL_SMALL_STACK
  34530. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34531. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34532. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34533. XFREE(ct, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34534. XFREE(ss, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34535. XFREE(ss_dec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34536. #endif
  34537. return ret;
  34538. }
  34539. #endif /* WOLFSSL_KYBER768 */
  34540. #ifdef WOLFSSL_KYBER1024
  34541. static wc_test_ret_t kyber1024_kat(void)
  34542. {
  34543. wc_test_ret_t ret;
  34544. #ifdef WOLFSSL_SMALL_STACK
  34545. KyberKey *key = NULL;
  34546. byte *priv = NULL;
  34547. byte *pub = NULL;
  34548. byte *ct = NULL;
  34549. byte *ss = NULL;
  34550. byte *ss_dec = NULL;
  34551. #else
  34552. KyberKey key[1];
  34553. byte priv[KYBER1024_PRIVATE_KEY_SIZE];
  34554. byte pub[KYBER1024_PUBLIC_KEY_SIZE];
  34555. byte ct[KYBER1024_CIPHER_TEXT_SIZE];
  34556. byte ss[KYBER_SS_SZ];
  34557. byte ss_dec[KYBER_SS_SZ];
  34558. #endif
  34559. int key_inited = 0;
  34560. WOLFSSL_SMALL_STACK_STATIC const byte kyber1024_rand[] = {
  34561. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  34562. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  34563. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  34564. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  34565. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  34566. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  34567. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  34568. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  34569. };
  34570. WOLFSSL_SMALL_STACK_STATIC const byte kyber1024enc_rand[] = {
  34571. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  34572. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  34573. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  34574. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  34575. };
  34576. WOLFSSL_SMALL_STACK_STATIC const byte kyber1024_pk[] = {
  34577. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  34578. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  34579. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  34580. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  34581. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  34582. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  34583. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  34584. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  34585. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  34586. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  34587. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  34588. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  34589. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  34590. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  34591. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  34592. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  34593. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  34594. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  34595. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  34596. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  34597. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  34598. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  34599. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  34600. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  34601. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  34602. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  34603. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  34604. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  34605. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  34606. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  34607. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  34608. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  34609. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  34610. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  34611. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  34612. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  34613. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  34614. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  34615. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  34616. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  34617. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  34618. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  34619. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  34620. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  34621. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  34622. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  34623. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  34624. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  34625. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  34626. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  34627. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  34628. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  34629. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  34630. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  34631. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  34632. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  34633. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  34634. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  34635. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  34636. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  34637. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  34638. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  34639. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  34640. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  34641. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  34642. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  34643. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  34644. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  34645. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  34646. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  34647. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  34648. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  34649. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  34650. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  34651. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  34652. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  34653. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  34654. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  34655. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  34656. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  34657. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  34658. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  34659. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  34660. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  34661. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  34662. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  34663. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  34664. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  34665. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  34666. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  34667. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  34668. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  34669. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  34670. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  34671. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  34672. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  34673. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  34674. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  34675. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  34676. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  34677. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  34678. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  34679. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  34680. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  34681. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  34682. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  34683. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  34684. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  34685. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  34686. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  34687. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  34688. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  34689. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  34690. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  34691. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  34692. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  34693. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  34694. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  34695. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  34696. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  34697. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  34698. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  34699. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  34700. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  34701. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  34702. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  34703. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  34704. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  34705. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  34706. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  34707. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  34708. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  34709. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  34710. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  34711. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  34712. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  34713. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  34714. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  34715. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  34716. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  34717. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  34718. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  34719. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  34720. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  34721. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  34722. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  34723. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  34724. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  34725. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  34726. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  34727. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  34728. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  34729. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  34730. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  34731. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  34732. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  34733. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  34734. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  34735. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  34736. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  34737. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  34738. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  34739. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  34740. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  34741. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  34742. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  34743. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  34744. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  34745. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  34746. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  34747. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  34748. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  34749. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  34750. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  34751. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  34752. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  34753. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  34754. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  34755. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  34756. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  34757. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  34758. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  34759. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  34760. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  34761. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  34762. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  34763. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  34764. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  34765. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  34766. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  34767. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  34768. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  34769. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  34770. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  34771. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  34772. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  34773. };
  34774. WOLFSSL_SMALL_STACK_STATIC const byte kyber1024_sk[] = {
  34775. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  34776. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  34777. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  34778. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  34779. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  34780. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  34781. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  34782. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  34783. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  34784. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  34785. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  34786. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  34787. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  34788. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  34789. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  34790. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  34791. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  34792. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  34793. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  34794. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  34795. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  34796. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  34797. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  34798. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  34799. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  34800. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  34801. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  34802. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  34803. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  34804. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  34805. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  34806. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  34807. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  34808. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  34809. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  34810. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  34811. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  34812. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  34813. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  34814. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  34815. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  34816. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  34817. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  34818. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  34819. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  34820. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  34821. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  34822. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  34823. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  34824. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  34825. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  34826. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  34827. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  34828. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  34829. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  34830. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  34831. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  34832. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  34833. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  34834. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  34835. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  34836. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  34837. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  34838. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  34839. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  34840. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  34841. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  34842. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  34843. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  34844. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  34845. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  34846. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  34847. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  34848. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  34849. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  34850. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  34851. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  34852. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  34853. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  34854. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  34855. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  34856. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  34857. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  34858. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  34859. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  34860. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  34861. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  34862. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  34863. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  34864. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  34865. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  34866. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  34867. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  34868. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  34869. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  34870. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  34871. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  34872. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  34873. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  34874. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  34875. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  34876. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  34877. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  34878. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  34879. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  34880. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  34881. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  34882. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  34883. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  34884. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  34885. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  34886. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  34887. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  34888. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  34889. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  34890. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  34891. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  34892. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  34893. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  34894. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  34895. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  34896. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  34897. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  34898. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  34899. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  34900. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  34901. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  34902. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  34903. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  34904. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  34905. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  34906. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  34907. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  34908. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  34909. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  34910. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  34911. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  34912. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  34913. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  34914. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  34915. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  34916. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  34917. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  34918. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  34919. 0x85, 0xA4, 0xC1, 0xA1, 0xAB, 0x9B, 0x6A, 0xEB,
  34920. 0x49, 0xCC, 0xE1, 0xC2, 0xF8, 0xA9, 0x7C, 0x35,
  34921. 0x16, 0xC7, 0x2A, 0x00, 0xA4, 0x62, 0x63, 0xBA,
  34922. 0xA6, 0x96, 0xBF, 0x25, 0x72, 0x77, 0x19, 0xC3,
  34923. 0x21, 0x64, 0x23, 0x61, 0x8F, 0xF3, 0x33, 0x80,
  34924. 0x93, 0x4A, 0x6C, 0x10, 0x54, 0x5C, 0x4C, 0x5C,
  34925. 0x51, 0x55, 0xB1, 0x24, 0x86, 0x18, 0x1F, 0xC7,
  34926. 0xA2, 0x31, 0x98, 0x73, 0x97, 0x8B, 0x6A, 0x2A,
  34927. 0x67, 0x49, 0x0F, 0x82, 0x56, 0xBD, 0x21, 0x96,
  34928. 0xFE, 0x17, 0x92, 0xA4, 0xC0, 0x00, 0x77, 0xB8,
  34929. 0x12, 0xEA, 0xE8, 0xBE, 0xD3, 0x57, 0x24, 0x99,
  34930. 0x68, 0x4A, 0xB3, 0x37, 0x18, 0x76, 0x76, 0x1E,
  34931. 0x45, 0x0C, 0x9F, 0x9D, 0x27, 0x68, 0xA3, 0x68,
  34932. 0x06, 0xD7, 0xAB, 0x20, 0x46, 0xC9, 0x1F, 0x17,
  34933. 0x59, 0x9E, 0x9A, 0xC5, 0x92, 0x99, 0x08, 0x08,
  34934. 0xDC, 0xD7, 0xB4, 0xD0, 0x91, 0x90, 0x72, 0xF1,
  34935. 0x4E, 0xC3, 0x61, 0x77, 0x3B, 0x72, 0x52, 0x44,
  34936. 0x4C, 0x32, 0x3C, 0x30, 0x83, 0x26, 0xF4, 0xA3,
  34937. 0x0F, 0x86, 0x80, 0xD2, 0xF7, 0x48, 0xF5, 0x6A,
  34938. 0x13, 0x2B, 0x82, 0x67, 0x4E, 0xD0, 0x18, 0x46,
  34939. 0x20, 0xB8, 0x2A, 0xD2, 0xCB, 0x18, 0x2C, 0x97,
  34940. 0xB4, 0x81, 0x62, 0x66, 0x47, 0x49, 0x12, 0x90,
  34941. 0xA0, 0x11, 0xCC, 0x73, 0x82, 0x86, 0x85, 0xA8,
  34942. 0xC3, 0x67, 0xA5, 0xB9, 0xCF, 0x8D, 0x62, 0x1B,
  34943. 0x0D, 0x5C, 0x1E, 0xFF, 0x03, 0x17, 0x27, 0x58,
  34944. 0xBD, 0x00, 0x49, 0x78, 0xC2, 0x51, 0xCD, 0x51,
  34945. 0x34, 0x22, 0x28, 0x98, 0x9C, 0xAE, 0x63, 0x32,
  34946. 0xAC, 0x48, 0x64, 0x37, 0xCB, 0x5C, 0x57, 0xD4,
  34947. 0x30, 0x74, 0x62, 0x86, 0x52, 0x53, 0xBE, 0x21,
  34948. 0x7B, 0x35, 0x15, 0xC7, 0x3D, 0xF4, 0x05, 0xB7,
  34949. 0xF2, 0x82, 0x17, 0xAD, 0x0B, 0x8C, 0xF6, 0x0C,
  34950. 0x2F, 0xFF, 0xAA, 0x0A, 0x00, 0x48, 0xB1, 0xFB,
  34951. 0x4A, 0xCD, 0xCD, 0xC3, 0x8B, 0x52, 0x50, 0xCF,
  34952. 0xEC, 0x35, 0x6A, 0x6D, 0xE2, 0x6C, 0xFA, 0x7A,
  34953. 0x58, 0x8F, 0xDC, 0x86, 0xF9, 0x8C, 0x85, 0x4A,
  34954. 0xC6, 0x4C, 0x7B, 0xFA, 0xA9, 0x6F, 0x5A, 0x32,
  34955. 0xCC, 0x06, 0x10, 0x93, 0x4B, 0xAA, 0x6A, 0x58,
  34956. 0x6B, 0x9A, 0x20, 0x54, 0xF1, 0x3B, 0xA2, 0x74,
  34957. 0x17, 0x4A, 0xA0, 0xD2, 0xB3, 0xA8, 0x1B, 0x96,
  34958. 0xA9, 0x40, 0x66, 0x6F, 0x78, 0x9B, 0x5A, 0x6B,
  34959. 0xCD, 0xC0, 0xA6, 0xA0, 0x17, 0x8A, 0x0C, 0x9A,
  34960. 0x02, 0x57, 0x8A, 0x49, 0x3F, 0x6E, 0xEA, 0x0D,
  34961. 0x2E, 0x6C, 0x13, 0x95, 0x1C, 0x9F, 0x24, 0x9A,
  34962. 0x5E, 0x8D, 0xD7, 0x1D, 0xD4, 0x9A, 0x74, 0x2D,
  34963. 0x45, 0x1F, 0x1A, 0xBB, 0xA1, 0x9A, 0xF8, 0xC5,
  34964. 0x47, 0x85, 0x5E, 0x0A, 0xFC, 0x72, 0x8E, 0x90,
  34965. 0xAB, 0xB4, 0x99, 0xC9, 0xBE, 0xEB, 0x76, 0x6F,
  34966. 0x47, 0x29, 0xCD, 0xA2, 0x22, 0x63, 0xE3, 0x24,
  34967. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  34968. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  34969. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  34970. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  34971. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  34972. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  34973. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  34974. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  34975. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  34976. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  34977. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  34978. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  34979. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  34980. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  34981. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  34982. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  34983. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  34984. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  34985. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  34986. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  34987. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  34988. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  34989. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  34990. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  34991. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  34992. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  34993. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  34994. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  34995. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  34996. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  34997. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  34998. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  34999. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  35000. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  35001. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  35002. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  35003. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  35004. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  35005. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  35006. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  35007. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  35008. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  35009. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  35010. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  35011. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  35012. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  35013. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  35014. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  35015. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  35016. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  35017. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  35018. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  35019. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  35020. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  35021. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  35022. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  35023. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  35024. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  35025. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  35026. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  35027. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  35028. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  35029. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  35030. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  35031. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  35032. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  35033. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  35034. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  35035. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  35036. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  35037. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  35038. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  35039. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  35040. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  35041. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  35042. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  35043. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  35044. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  35045. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  35046. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  35047. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  35048. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  35049. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  35050. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  35051. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  35052. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  35053. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  35054. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  35055. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  35056. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  35057. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  35058. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  35059. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  35060. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  35061. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  35062. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  35063. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  35064. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  35065. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  35066. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  35067. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  35068. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  35069. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  35070. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  35071. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  35072. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  35073. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  35074. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  35075. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  35076. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  35077. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  35078. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  35079. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  35080. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  35081. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  35082. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  35083. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  35084. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  35085. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  35086. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  35087. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  35088. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  35089. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  35090. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  35091. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  35092. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  35093. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  35094. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  35095. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  35096. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  35097. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  35098. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  35099. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  35100. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  35101. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  35102. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  35103. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  35104. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  35105. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  35106. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  35107. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  35108. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  35109. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  35110. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  35111. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  35112. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  35113. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  35114. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  35115. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  35116. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  35117. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  35118. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  35119. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  35120. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  35121. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  35122. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  35123. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  35124. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  35125. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  35126. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  35127. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  35128. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  35129. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  35130. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  35131. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  35132. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  35133. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  35134. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  35135. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  35136. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  35137. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  35138. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  35139. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  35140. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  35141. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  35142. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  35143. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  35144. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  35145. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  35146. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  35147. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  35148. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  35149. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  35150. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  35151. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  35152. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  35153. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  35154. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  35155. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  35156. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  35157. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  35158. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  35159. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  35160. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  35161. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  35162. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  35163. 0x8A, 0x39, 0xE8, 0x7D, 0x53, 0x1F, 0x35, 0x27,
  35164. 0xC2, 0x07, 0xED, 0xCC, 0x1D, 0xB7, 0xFA, 0xDD,
  35165. 0xCF, 0x96, 0x28, 0x39, 0x18, 0x79, 0xB3, 0x35,
  35166. 0xC7, 0x07, 0x83, 0x9A, 0x0D, 0xB0, 0x51, 0xA8,
  35167. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  35168. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  35169. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  35170. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  35171. };
  35172. WOLFSSL_SMALL_STACK_STATIC const byte kyber1024_ct[] = {
  35173. 0xA6, 0xAF, 0x29, 0xD5, 0xF5, 0xB8, 0x0B, 0xD1,
  35174. 0x30, 0xF5, 0x18, 0xBA, 0xDD, 0xD6, 0xC8, 0xF1,
  35175. 0x75, 0x45, 0x41, 0x3D, 0x86, 0x0F, 0xB3, 0xDE,
  35176. 0x45, 0x19, 0x79, 0xEB, 0xFA, 0x5E, 0x4E, 0x31,
  35177. 0x12, 0xC7, 0xC0, 0xAD, 0xF9, 0x98, 0x24, 0xBB,
  35178. 0x52, 0x6F, 0x2C, 0x35, 0x50, 0x74, 0x8E, 0xD0,
  35179. 0xE1, 0x34, 0xF0, 0x45, 0x7A, 0x7C, 0x61, 0xF9,
  35180. 0xF5, 0x26, 0xF0, 0x02, 0xBA, 0xAD, 0xC0, 0x3F,
  35181. 0xC1, 0x3E, 0x38, 0x13, 0x12, 0x19, 0x51, 0x3C,
  35182. 0x3E, 0xDE, 0x06, 0x16, 0x61, 0xE7, 0x4F, 0x60,
  35183. 0x3C, 0x4F, 0xCF, 0x79, 0x51, 0xC8, 0xE5, 0x2C,
  35184. 0x9C, 0x21, 0x3B, 0x0D, 0x22, 0xD9, 0x29, 0x36,
  35185. 0x63, 0xD6, 0x69, 0xA6, 0xB5, 0x8E, 0xD8, 0xFC,
  35186. 0xEF, 0xCF, 0x82, 0x49, 0xD7, 0xBB, 0x52, 0x98,
  35187. 0xF5, 0x57, 0x61, 0x44, 0x5B, 0x2B, 0x83, 0xCE,
  35188. 0x7F, 0x00, 0x5C, 0xB0, 0x42, 0x48, 0xAE, 0xC8,
  35189. 0xBD, 0xA2, 0x2F, 0xD2, 0xD4, 0x2A, 0xA7, 0x66,
  35190. 0x32, 0x20, 0x14, 0xEA, 0x03, 0x8C, 0xC3, 0x2C,
  35191. 0x55, 0xC8, 0xE4, 0xB9, 0xE2, 0x8E, 0xC9, 0x11,
  35192. 0x9F, 0x52, 0x73, 0x41, 0xE4, 0xF6, 0x6A, 0x03,
  35193. 0x51, 0x21, 0x07, 0x3B, 0x85, 0xDE, 0x67, 0x06,
  35194. 0xDA, 0x19, 0xE0, 0x83, 0x8A, 0x9F, 0x33, 0xB7,
  35195. 0x19, 0xA6, 0x8F, 0x03, 0x9B, 0x66, 0x4D, 0xC0,
  35196. 0x02, 0x65, 0x9E, 0xAB, 0xFC, 0x39, 0x86, 0x79,
  35197. 0xAA, 0x70, 0x09, 0xCE, 0x0C, 0xD0, 0x1C, 0xDA,
  35198. 0xFB, 0x6C, 0xD2, 0xA2, 0x6F, 0xE4, 0x10, 0x16,
  35199. 0x72, 0xC9, 0x8F, 0xF5, 0x8F, 0x7C, 0x47, 0xD5,
  35200. 0xBD, 0xA2, 0x90, 0x66, 0x53, 0xB3, 0xA6, 0xF9,
  35201. 0x65, 0x1F, 0x7A, 0x12, 0x1E, 0xA7, 0x7E, 0xA7,
  35202. 0x47, 0x23, 0xFA, 0xE5, 0xB8, 0x73, 0xF9, 0xBB,
  35203. 0x7B, 0x66, 0x4F, 0x0C, 0x8A, 0x93, 0x83, 0x1E,
  35204. 0xF9, 0xD5, 0x1C, 0x7C, 0xC1, 0xEF, 0x44, 0xAC,
  35205. 0x0E, 0x55, 0xA5, 0x5C, 0xA7, 0x6D, 0x13, 0x7F,
  35206. 0xE9, 0xB7, 0x5F, 0x40, 0x50, 0x9C, 0xEF, 0x15,
  35207. 0x6E, 0x5A, 0xD1, 0x8F, 0x9F, 0xB9, 0x99, 0x68,
  35208. 0x00, 0x08, 0xE5, 0x47, 0xD5, 0x5E, 0xEC, 0xD5,
  35209. 0xB4, 0xD1, 0xCB, 0x1D, 0x9F, 0x07, 0x6C, 0xEC,
  35210. 0x21, 0x50, 0x1C, 0x74, 0x02, 0x50, 0x9E, 0xCB,
  35211. 0x77, 0xAF, 0xB2, 0xCB, 0x9A, 0x61, 0x34, 0x0A,
  35212. 0x8B, 0xD1, 0x51, 0x4C, 0x6E, 0x71, 0xB4, 0xAA,
  35213. 0x45, 0xE4, 0x7E, 0xC3, 0x75, 0x12, 0x27, 0x1B,
  35214. 0x91, 0x1F, 0x8F, 0xB4, 0x6C, 0x90, 0x82, 0xC9,
  35215. 0xDF, 0x07, 0x20, 0x4A, 0xBB, 0x5A, 0x50, 0xE6,
  35216. 0xE3, 0x64, 0x7A, 0x8A, 0xD4, 0xD8, 0xD5, 0xD7,
  35217. 0xBF, 0xF1, 0x9C, 0x8A, 0x50, 0x93, 0x08, 0xBC,
  35218. 0xFB, 0x89, 0x55, 0x36, 0xD0, 0x45, 0xCA, 0x2B,
  35219. 0x97, 0xCB, 0x16, 0xA2, 0x9B, 0xB7, 0x18, 0x1C,
  35220. 0xAD, 0x05, 0x09, 0xDD, 0xB9, 0x17, 0x35, 0x02,
  35221. 0x8E, 0xBA, 0x8C, 0x31, 0xD7, 0x4B, 0xD2, 0x75,
  35222. 0xEA, 0xA6, 0x5B, 0x53, 0x40, 0xB3, 0xA4, 0x3F,
  35223. 0xBF, 0xE0, 0xB3, 0x06, 0x1D, 0x6B, 0xAE, 0x7E,
  35224. 0x75, 0xB7, 0x09, 0x8C, 0xDA, 0xBE, 0x91, 0xD4,
  35225. 0xB3, 0x1E, 0x36, 0xC9, 0xAA, 0x7A, 0x82, 0x98,
  35226. 0x86, 0x2A, 0xD6, 0x3C, 0x8F, 0xD2, 0x82, 0xE0,
  35227. 0x3B, 0x46, 0x0B, 0x3A, 0xB4, 0x64, 0xCE, 0x0F,
  35228. 0x27, 0xB1, 0xC3, 0xD1, 0x11, 0x55, 0xAC, 0xAA,
  35229. 0x01, 0x1E, 0xB9, 0xE2, 0xAE, 0x3E, 0x6D, 0xDA,
  35230. 0x07, 0xD6, 0xF4, 0x91, 0x73, 0x7C, 0xBC, 0xE9,
  35231. 0xB0, 0x5F, 0x9B, 0xC5, 0x6B, 0xE2, 0x0E, 0x8D,
  35232. 0x32, 0x6B, 0xA1, 0x32, 0xC5, 0x7F, 0xB2, 0x35,
  35233. 0x16, 0x11, 0x44, 0x51, 0x9C, 0xDF, 0x40, 0x56,
  35234. 0x0F, 0xBE, 0x27, 0x9B, 0xDE, 0x41, 0x1E, 0x11,
  35235. 0x25, 0x31, 0xF8, 0x26, 0xD6, 0xAB, 0x10, 0xD4,
  35236. 0x54, 0x73, 0x50, 0xAD, 0xD2, 0xA9, 0xDE, 0x8D,
  35237. 0x62, 0xC2, 0xAC, 0x82, 0xCA, 0xBE, 0x68, 0x15,
  35238. 0x64, 0x6F, 0x4D, 0xC9, 0x74, 0x2B, 0xB0, 0xC2,
  35239. 0xA3, 0xF7, 0x7E, 0xC7, 0xB4, 0x6C, 0x6B, 0x53,
  35240. 0x76, 0x05, 0xFA, 0x31, 0x79, 0x8C, 0xD8, 0x92,
  35241. 0x81, 0x22, 0x1A, 0x33, 0xDF, 0xB9, 0x79, 0x6E,
  35242. 0x64, 0x43, 0x05, 0x63, 0x03, 0x32, 0xC2, 0xCB,
  35243. 0x93, 0x14, 0x08, 0xAB, 0x48, 0x1A, 0x16, 0xD9,
  35244. 0x53, 0xF6, 0xBE, 0xAE, 0x38, 0x91, 0xD6, 0xD9,
  35245. 0xAC, 0x1F, 0xAB, 0x38, 0x22, 0x2D, 0x92, 0x71,
  35246. 0x87, 0x2D, 0x9D, 0x0C, 0xAD, 0xB9, 0x1A, 0xBE,
  35247. 0x9B, 0x4E, 0x26, 0x5F, 0x75, 0xC6, 0xE5, 0xE8,
  35248. 0x29, 0xE1, 0x46, 0xC3, 0xD8, 0xCE, 0x1E, 0x9D,
  35249. 0x12, 0xE0, 0xD1, 0x29, 0x80, 0x19, 0x57, 0xF4,
  35250. 0x6B, 0x0D, 0x2D, 0xBE, 0x1F, 0x74, 0x9B, 0x1D,
  35251. 0x08, 0xE2, 0x34, 0x5F, 0x62, 0x39, 0xA7, 0x31,
  35252. 0x34, 0x2E, 0xB7, 0x5B, 0x0C, 0xF1, 0xBF, 0x41,
  35253. 0x17, 0x49, 0xBC, 0x2C, 0xAF, 0x28, 0x10, 0xB7,
  35254. 0x88, 0xC6, 0xB7, 0x23, 0x8B, 0x4D, 0x3D, 0xA2,
  35255. 0xD6, 0x31, 0x5C, 0xE9, 0x54, 0x2E, 0x24, 0x40,
  35256. 0x4F, 0x14, 0x57, 0x55, 0xA3, 0x0A, 0xB8, 0x51,
  35257. 0xE4, 0x44, 0x58, 0x41, 0xBD, 0x33, 0xF7, 0x16,
  35258. 0xA5, 0x86, 0x88, 0x48, 0x88, 0xEC, 0xC6, 0xBC,
  35259. 0x64, 0x98, 0xAA, 0x32, 0x91, 0x9A, 0xE8, 0x1D,
  35260. 0x20, 0xC2, 0x69, 0x73, 0xC2, 0xBD, 0x54, 0x58,
  35261. 0x2A, 0x0F, 0x6A, 0xD9, 0x8A, 0xBF, 0xD2, 0x62,
  35262. 0x7E, 0x15, 0x69, 0x0A, 0x72, 0x7E, 0x69, 0xF5,
  35263. 0x81, 0xDD, 0x2A, 0x71, 0x27, 0x98, 0x2A, 0x90,
  35264. 0xE3, 0x3E, 0x2D, 0x4A, 0x03, 0xFE, 0x33, 0x91,
  35265. 0x42, 0xC7, 0xE4, 0x4C, 0x32, 0x6A, 0xC4, 0x6E,
  35266. 0xD3, 0x95, 0xA2, 0x25, 0xD3, 0x03, 0x33, 0x89,
  35267. 0x91, 0x73, 0x28, 0xB4, 0x53, 0x16, 0xB1, 0x58,
  35268. 0x5A, 0x01, 0xB2, 0xC3, 0x04, 0xB2, 0x94, 0x4E,
  35269. 0x90, 0x3A, 0xBB, 0xB3, 0xEC, 0x56, 0x19, 0x44,
  35270. 0x1C, 0xFC, 0x89, 0x65, 0xA4, 0x46, 0xDF, 0x75,
  35271. 0xDE, 0xFA, 0x80, 0xC6, 0xE1, 0x5A, 0xDB, 0xD5,
  35272. 0x06, 0xB7, 0xAB, 0x2D, 0xE1, 0x2D, 0xDA, 0x9B,
  35273. 0xC8, 0x14, 0x41, 0xCF, 0xC8, 0x90, 0x52, 0xE2,
  35274. 0xE5, 0x80, 0x8F, 0x71, 0x26, 0xC6, 0xFD, 0x3A,
  35275. 0xC6, 0xAC, 0x80, 0x81, 0x25, 0x8A, 0x84, 0xA0,
  35276. 0x9A, 0xE5, 0x0F, 0x6C, 0xD7, 0xCC, 0x0F, 0x4A,
  35277. 0xF3, 0x36, 0xFD, 0x1D, 0x64, 0x3E, 0x99, 0x07,
  35278. 0x99, 0x96, 0x26, 0x8C, 0x2D, 0x32, 0xD9, 0x09,
  35279. 0xF2, 0x2E, 0x35, 0x04, 0xF0, 0x7F, 0xBB, 0x56,
  35280. 0x31, 0x96, 0xD4, 0x31, 0x2F, 0xDD, 0xB9, 0x33,
  35281. 0x5D, 0x5C, 0x1D, 0x36, 0xE8, 0xC5, 0xEE, 0xA2,
  35282. 0x27, 0x8D, 0xBA, 0x23, 0xB9, 0x4D, 0x19, 0x3C,
  35283. 0x94, 0x7C, 0xC4, 0x1C, 0xA9, 0x93, 0xDC, 0x7D,
  35284. 0xB1, 0x39, 0x63, 0x40, 0xAD, 0x9C, 0x4F, 0xE6,
  35285. 0x87, 0xDD, 0x7B, 0x8D, 0x0C, 0x7A, 0x51, 0x20,
  35286. 0xAE, 0x02, 0x04, 0xF2, 0xC6, 0x65, 0xBD, 0x5F,
  35287. 0x47, 0x3D, 0x64, 0x4C, 0x7F, 0xF2, 0x6B, 0xFF,
  35288. 0xBA, 0x7A, 0x36, 0x98, 0x08, 0x30, 0x70, 0x21,
  35289. 0x28, 0xA7, 0xE6, 0x61, 0xD6, 0x77, 0xA0, 0x92,
  35290. 0xA3, 0x6E, 0x74, 0x28, 0xA4, 0x13, 0x9F, 0xB2,
  35291. 0x9B, 0x00, 0x95, 0xCC, 0x11, 0x08, 0x6F, 0x44,
  35292. 0x7D, 0x2A, 0x9E, 0xF6, 0xC9, 0xB1, 0x61, 0xF1,
  35293. 0x89, 0xC6, 0x29, 0x9E, 0x08, 0x4C, 0xB7, 0xAA,
  35294. 0x00, 0xFA, 0xF7, 0x87, 0x79, 0x7B, 0xFB, 0x06,
  35295. 0x9F, 0xBC, 0x08, 0x7F, 0xDE, 0x26, 0x25, 0x2A,
  35296. 0x16, 0x64, 0xF1, 0x9C, 0x5A, 0x8A, 0x22, 0xEC,
  35297. 0x5E, 0xE1, 0xAE, 0xB0, 0x76, 0x35, 0x7B, 0x7D,
  35298. 0xC3, 0x7E, 0x6B, 0x0F, 0x15, 0x20, 0xF9, 0x58,
  35299. 0xF7, 0x85, 0x1B, 0xAC, 0xB9, 0x2C, 0x89, 0xFD,
  35300. 0x11, 0x4A, 0x72, 0xFE, 0xAC, 0x54, 0x65, 0x2D,
  35301. 0x45, 0xB0, 0x9E, 0x1A, 0xE7, 0x65, 0x1A, 0xBD,
  35302. 0x16, 0x4B, 0xCD, 0x53, 0x7D, 0x58, 0xFA, 0x39,
  35303. 0xD3, 0xEC, 0x8A, 0xCD, 0xCD, 0xF9, 0x84, 0x25,
  35304. 0x00, 0x58, 0x62, 0xFA, 0x59, 0x69, 0x2D, 0xE1,
  35305. 0x62, 0xB7, 0x7E, 0x62, 0x97, 0xC6, 0x62, 0x33,
  35306. 0x34, 0x84, 0x08, 0xA8, 0xAB, 0x69, 0x5C, 0xE2,
  35307. 0xF2, 0x72, 0x8D, 0xB9, 0xFB, 0xE2, 0x7E, 0x95,
  35308. 0x89, 0x67, 0xEC, 0x59, 0x74, 0x76, 0x7C, 0x5A,
  35309. 0x66, 0x02, 0x30, 0x74, 0xB4, 0xA7, 0x1A, 0xFD,
  35310. 0x26, 0x4A, 0xD2, 0x89, 0x0E, 0x97, 0x0A, 0x1F,
  35311. 0x31, 0xD6, 0xE3, 0x31, 0x1B, 0x73, 0x6F, 0x9F,
  35312. 0x94, 0x88, 0x79, 0x3D, 0xDC, 0x88, 0xF2, 0x34,
  35313. 0x58, 0x06, 0x42, 0x54, 0xC8, 0x2A, 0x1D, 0x9E,
  35314. 0x59, 0xEA, 0xD2, 0xFC, 0xEC, 0x40, 0xB4, 0x30,
  35315. 0x68, 0x7C, 0x4B, 0x7E, 0x28, 0x96, 0x09, 0x26,
  35316. 0xAF, 0xCA, 0xCC, 0x9B, 0xD7, 0x56, 0xA7, 0x10,
  35317. 0x88, 0xC7, 0x84, 0x50, 0xE2, 0x0A, 0x2E, 0x98,
  35318. 0x0A, 0xED, 0xE9, 0xEB, 0xED, 0xFE, 0x7F, 0xAB,
  35319. 0xD6, 0xAB, 0xFE, 0x96, 0xF9, 0x34, 0xC4, 0xB0,
  35320. 0x2C, 0x01, 0xCA, 0x19, 0x4D, 0x01, 0xB7, 0x3C,
  35321. 0x25, 0xD5, 0x99, 0x70, 0x39, 0xD3, 0xFC, 0xD0,
  35322. 0xF0, 0x99, 0x52, 0x1F, 0x70, 0xCA, 0xEE, 0x69,
  35323. 0x11, 0x0A, 0xC1, 0xFC, 0x5A, 0x99, 0x91, 0x7A,
  35324. 0xD7, 0x52, 0xFC, 0x96, 0xAD, 0xFA, 0xD7, 0x18,
  35325. 0x6D, 0x0A, 0x7C, 0x9C, 0xFE, 0x56, 0x01, 0xC0,
  35326. 0x75, 0x14, 0xEA, 0x64, 0x48, 0xD6, 0x61, 0xC5,
  35327. 0x7A, 0xA2, 0x02, 0x42, 0x10, 0x3C, 0x42, 0x76,
  35328. 0xA0, 0x70, 0xA4, 0x89, 0xA4, 0xCB, 0x6B, 0xCA,
  35329. 0x0F, 0x9E, 0xCC, 0x43, 0x79, 0xFB, 0x22, 0x02,
  35330. 0x15, 0xFD, 0x91, 0xF8, 0x10, 0x19, 0xD5, 0xB0,
  35331. 0xAE, 0x61, 0x93, 0x58, 0xB5, 0x24, 0x68, 0xF2,
  35332. 0x72, 0xC1, 0x78, 0xE3, 0xA7, 0x4C, 0xF6, 0x77,
  35333. 0x5A, 0xA9, 0x24, 0xFE, 0x32, 0x9C, 0x31, 0x75,
  35334. 0xD9, 0xE4, 0xC3, 0xE2, 0x1A, 0xB9, 0xEC, 0x83,
  35335. 0x6E, 0xDC, 0x3A, 0xCA, 0xB2, 0xE3, 0x89, 0x1E,
  35336. 0xE8, 0xDE, 0xDA, 0x51, 0x5D, 0x39, 0xAF, 0x9B,
  35337. 0x8D, 0xDD, 0x0E, 0xE7, 0xB0, 0x16, 0x4F, 0x80,
  35338. 0x5C, 0x38, 0x35, 0xF6, 0xD2, 0xBA, 0xBD, 0xB3,
  35339. 0x0E, 0xAB, 0x47, 0x56, 0xE7, 0xEC, 0x7F, 0x82,
  35340. 0x9E, 0xCE, 0x01, 0xE8, 0xEA, 0xDF, 0xBB, 0xED,
  35341. 0x12, 0xFC, 0x28, 0x3B, 0x3D, 0x4C, 0x69, 0xF5,
  35342. 0x75, 0xE7, 0xF8, 0x04, 0x17, 0x68, 0x9F, 0xDF,
  35343. 0xCF, 0xC7, 0xBE, 0x27, 0xEE, 0x3B, 0x8C, 0xDF,
  35344. 0x57, 0xAA, 0xEB, 0xEC, 0x4A, 0x95, 0xB7, 0xE5,
  35345. 0xBB, 0x58, 0x5B, 0x85, 0x22, 0x7F, 0x7C, 0x32,
  35346. 0xBE, 0x30, 0xDB, 0x3E, 0x65, 0xE4, 0x2E, 0x30,
  35347. 0xDC, 0xF5, 0xA5, 0xFA, 0x07, 0x3D, 0xBA, 0x39,
  35348. 0x9D, 0x94, 0x2F, 0x22, 0x22, 0xAD, 0xB9, 0xB9,
  35349. 0x89, 0x81, 0x02, 0xAF, 0xE5, 0x43, 0x2E, 0xDC,
  35350. 0x7F, 0x04, 0xAE, 0x34, 0xA8, 0xFE, 0xC2, 0xD8,
  35351. 0x1C, 0xB4, 0x9A, 0x9A, 0x9B, 0x43, 0x81, 0x4C,
  35352. 0xE7, 0x1D, 0x97, 0xF7, 0x26, 0xE2, 0xB1, 0xE8,
  35353. 0xF6, 0x4B, 0x50, 0xE6, 0x5D, 0xFB, 0x48, 0x16,
  35354. 0xE1, 0x2E, 0x82, 0xA3, 0x19, 0x74, 0x84, 0xA4,
  35355. 0xE9, 0xBB, 0xA4, 0xD2, 0xD6, 0x9E, 0x3F, 0x19,
  35356. 0xD0, 0xB7, 0x5C, 0x21, 0xE2, 0xBF, 0xFE, 0x9F,
  35357. 0xC0, 0xC9, 0x8C, 0xF4, 0x8A, 0x3A, 0xAF, 0x08,
  35358. 0xD4, 0x67, 0xF7, 0x26, 0x87, 0xDF, 0x01, 0x78,
  35359. 0x17, 0x4B, 0x78, 0x97, 0xF7, 0x34, 0x34, 0x9B,
  35360. 0x18, 0x1E, 0xCA, 0x86, 0xA5, 0x98, 0xA0, 0xC5,
  35361. 0xE8, 0xC2, 0x59, 0x46, 0xF2, 0x4D, 0xC5, 0x57,
  35362. 0x2B, 0xD3, 0x24, 0xA4, 0x04, 0x58, 0xA7, 0x88,
  35363. 0xE5, 0x13, 0x7F, 0x3C, 0x7A, 0x7C, 0x97, 0xFC,
  35364. 0x9F, 0x12, 0xA3, 0xC4, 0x63, 0xA8, 0xFE, 0x94,
  35365. 0x49, 0x10, 0x1C, 0xCE, 0x96, 0x6D, 0x7C, 0x00,
  35366. 0x93, 0x23, 0x93, 0x29, 0x98, 0xD5, 0x6E, 0xF4,
  35367. 0x30, 0xC7, 0x3B, 0xC2, 0x4F, 0x5D, 0x95, 0xF7,
  35368. 0x37, 0x85, 0x8D, 0xDC, 0x4F, 0x32, 0xC0, 0x13
  35369. };
  35370. WOLFSSL_SMALL_STACK_STATIC const byte kyber1024_ss[] = {
  35371. 0xB1, 0x0F, 0x73, 0x94, 0x92, 0x6A, 0xD3, 0xB4,
  35372. 0x9C, 0x5D, 0x62, 0xD5, 0xAE, 0xB5, 0x31, 0xD5,
  35373. 0x75, 0x75, 0x38, 0xBC, 0xC0, 0xDA, 0x9E, 0x55,
  35374. 0x0D, 0x43, 0x8F, 0x1B, 0x61, 0xBD, 0x74, 0x19
  35375. };
  35376. #ifdef WOLFSSL_SMALL_STACK
  35377. key = (KyberKey *)XMALLOC(sizeof(KyberKey), HEAP_HINT,
  35378. DYNAMIC_TYPE_TMP_BUFFER);
  35379. priv = (byte *)XMALLOC(KYBER1024_PRIVATE_KEY_SIZE, HEAP_HINT,
  35380. DYNAMIC_TYPE_TMP_BUFFER);
  35381. pub = (byte *)XMALLOC(KYBER1024_PUBLIC_KEY_SIZE, HEAP_HINT,
  35382. DYNAMIC_TYPE_TMP_BUFFER);
  35383. ct = (byte *)XMALLOC(KYBER1024_CIPHER_TEXT_SIZE, HEAP_HINT,
  35384. DYNAMIC_TYPE_TMP_BUFFER);
  35385. ss = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  35386. DYNAMIC_TYPE_TMP_BUFFER);
  35387. ss_dec = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  35388. DYNAMIC_TYPE_TMP_BUFFER);
  35389. if (! (key && priv && pub && ct && ss && ss_dec))
  35390. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  35391. #endif
  35392. ret = wc_KyberKey_Init(KYBER1024, key, HEAP_HINT, INVALID_DEVID);
  35393. if (ret != 0)
  35394. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35395. else
  35396. key_inited = 1;
  35397. ret = wc_KyberKey_MakeKeyWithRandom(key, kyber1024_rand,
  35398. sizeof(kyber1024_rand));
  35399. if (ret != 0)
  35400. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35401. ret = wc_KyberKey_EncodePublicKey(key, pub, KYBER_MAX_PUBLIC_KEY_SIZE);
  35402. if (ret != 0)
  35403. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35404. ret = wc_KyberKey_EncodePrivateKey(key, priv, KYBER_MAX_PRIVATE_KEY_SIZE);
  35405. if (ret != 0)
  35406. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35407. if (XMEMCMP(pub, kyber1024_pk, sizeof(kyber1024_pk)) != 0)
  35408. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35409. if (XMEMCMP(priv, kyber1024_sk, sizeof(kyber1024_sk)) != 0)
  35410. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35411. ret = wc_KyberKey_EncapsulateWithRandom(key, ct, ss, kyber1024enc_rand,
  35412. sizeof(kyber1024enc_rand));
  35413. if (ret != 0)
  35414. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35415. if (XMEMCMP(ct, kyber1024_ct, sizeof(kyber1024_ct)) != 0)
  35416. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35417. if (XMEMCMP(ss, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  35418. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35419. ret = wc_KyberKey_Decapsulate(key, ss_dec, ct, sizeof(kyber1024_ct));
  35420. if (ret != 0)
  35421. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35422. if (XMEMCMP(ss_dec, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  35423. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35424. out:
  35425. if (key_inited)
  35426. wc_KyberKey_Free(key);
  35427. #ifdef WOLFSSL_SMALL_STACK
  35428. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35429. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35430. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35431. XFREE(ct, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35432. XFREE(ss, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35433. XFREE(ss_dec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35434. #endif
  35435. return ret;
  35436. }
  35437. #endif /* WOLFSSL_KYBER1024 */
  35438. #endif /* WOLFSSL_WC_KYBER */
  35439. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t kyber_test(void)
  35440. {
  35441. wc_test_ret_t ret;
  35442. WC_RNG rng;
  35443. int i;
  35444. #ifdef WOLFSSL_SMALL_STACK
  35445. KyberKey *key = NULL;
  35446. byte *priv = NULL;
  35447. byte *pub = NULL;
  35448. byte *priv2 = NULL;
  35449. byte *pub2 = NULL;
  35450. byte *ct = NULL;
  35451. byte *ss = NULL;
  35452. byte *ss_dec = NULL;
  35453. #else
  35454. KyberKey key[1];
  35455. byte priv[KYBER_MAX_PRIVATE_KEY_SIZE];
  35456. byte pub[KYBER_MAX_PUBLIC_KEY_SIZE];
  35457. byte priv2[KYBER_MAX_PRIVATE_KEY_SIZE];
  35458. byte pub2[KYBER_MAX_PUBLIC_KEY_SIZE];
  35459. byte ct[KYBER_MAX_CIPHER_TEXT_SIZE];
  35460. byte ss[KYBER_SS_SZ];
  35461. byte ss_dec[KYBER_SS_SZ];
  35462. #endif
  35463. int key_inited = 0;
  35464. static const int testData[][4] = {
  35465. #ifdef WOLFSSL_KYBER512
  35466. { KYBER512, KYBER512_PRIVATE_KEY_SIZE, KYBER512_PUBLIC_KEY_SIZE,
  35467. KYBER512_CIPHER_TEXT_SIZE },
  35468. #endif
  35469. #ifdef WOLFSSL_KYBER768
  35470. { KYBER768, KYBER768_PRIVATE_KEY_SIZE, KYBER768_PUBLIC_KEY_SIZE,
  35471. KYBER768_CIPHER_TEXT_SIZE },
  35472. #endif
  35473. #ifdef WOLFSSL_KYBER1024
  35474. { KYBER1024, KYBER1024_PRIVATE_KEY_SIZE, KYBER1024_PUBLIC_KEY_SIZE,
  35475. KYBER1024_CIPHER_TEXT_SIZE },
  35476. #endif
  35477. };
  35478. WOLFSSL_ENTER("kyber_test");
  35479. #ifdef WOLFSSL_SMALL_STACK
  35480. key = (KyberKey *)XMALLOC(sizeof(KyberKey), HEAP_HINT,
  35481. DYNAMIC_TYPE_TMP_BUFFER);
  35482. priv = (byte *)XMALLOC(KYBER_MAX_PRIVATE_KEY_SIZE, HEAP_HINT,
  35483. DYNAMIC_TYPE_TMP_BUFFER);
  35484. pub = (byte *)XMALLOC(KYBER_MAX_PUBLIC_KEY_SIZE, HEAP_HINT,
  35485. DYNAMIC_TYPE_TMP_BUFFER);
  35486. priv2 = (byte *)XMALLOC(KYBER_MAX_PRIVATE_KEY_SIZE, HEAP_HINT,
  35487. DYNAMIC_TYPE_TMP_BUFFER);
  35488. pub2 = (byte *)XMALLOC(KYBER_MAX_PUBLIC_KEY_SIZE, HEAP_HINT,
  35489. DYNAMIC_TYPE_TMP_BUFFER);
  35490. ct = (byte *)XMALLOC(KYBER_MAX_CIPHER_TEXT_SIZE, HEAP_HINT,
  35491. DYNAMIC_TYPE_TMP_BUFFER);
  35492. ss = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  35493. DYNAMIC_TYPE_TMP_BUFFER);
  35494. ss_dec = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  35495. DYNAMIC_TYPE_TMP_BUFFER);
  35496. if (! (key && priv && pub && priv2 && pub2 && ct && ss && ss_dec))
  35497. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  35498. #endif
  35499. #ifndef HAVE_FIPS
  35500. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  35501. #else
  35502. ret = wc_InitRng(&rng);
  35503. #endif
  35504. if (ret != 0)
  35505. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35506. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  35507. ret = wc_KyberKey_Init(testData[i][0], key, HEAP_HINT, INVALID_DEVID);
  35508. if (ret != 0)
  35509. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35510. else
  35511. key_inited = 1;
  35512. ret = wc_KyberKey_MakeKey(key, &rng);
  35513. if (ret != 0)
  35514. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35515. ret = wc_KyberKey_EncodePublicKey(key, pub, testData[i][2]);
  35516. if (ret != 0)
  35517. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35518. ret = wc_KyberKey_EncodePrivateKey(key, priv, testData[i][1]);
  35519. if (ret != 0)
  35520. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35521. ret = wc_KyberKey_Init(testData[i][0], key, HEAP_HINT, INVALID_DEVID);
  35522. if (ret != 0)
  35523. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35524. ret = wc_KyberKey_DecodePublicKey(key, pub, testData[i][2]);
  35525. if (ret != 0)
  35526. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35527. ret = wc_KyberKey_Encapsulate(key, ct, ss, &rng);
  35528. if (ret != 0)
  35529. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35530. ret = wc_KyberKey_EncodePublicKey(key, pub2, testData[i][2]);
  35531. if (ret != 0)
  35532. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35533. if (XMEMCMP(pub, pub2, testData[i][2]) != 0)
  35534. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35535. ret = wc_KyberKey_Init(testData[i][0], key, HEAP_HINT, INVALID_DEVID);
  35536. if (ret != 0)
  35537. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35538. ret = wc_KyberKey_DecodePrivateKey(key, priv, testData[i][1]);
  35539. if (ret != 0)
  35540. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35541. ret = wc_KyberKey_Decapsulate(key, ss_dec, ct, testData[i][3]);
  35542. if (ret != 0)
  35543. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35544. if (XMEMCMP(ss, ss_dec, KYBER_SS_SZ) != 0)
  35545. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35546. ret = wc_KyberKey_EncodePrivateKey(key, priv2, testData[i][1]);
  35547. if (ret != 0)
  35548. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35549. if (XMEMCMP(priv, priv2, testData[i][2]) != 0)
  35550. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35551. }
  35552. wc_FreeRng(&rng);
  35553. #ifdef WOLFSSL_WC_KYBER
  35554. #ifdef WOLFSSL_KYBER512
  35555. ret = kyber512_kat();
  35556. if (ret != 0)
  35557. goto out;
  35558. #endif
  35559. #ifdef WOLFSSL_KYBER768
  35560. ret = kyber768_kat();
  35561. if (ret != 0)
  35562. goto out;
  35563. #endif
  35564. #ifdef WOLFSSL_KYBER1024
  35565. ret = kyber1024_kat();
  35566. if (ret != 0)
  35567. goto out;
  35568. #endif
  35569. #endif /* WOLFSSL_WC_KYBER */
  35570. out:
  35571. if (key_inited)
  35572. wc_KyberKey_Free(key);
  35573. #ifdef WOLFSSL_SMALL_STACK
  35574. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35575. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35576. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35577. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35578. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35579. XFREE(ct, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35580. XFREE(ss, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35581. XFREE(ss_dec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35582. #endif
  35583. return ret;
  35584. }
  35585. #endif /* WOLFSSL_HAVE_KYBER */
  35586. #if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  35587. static enum wc_XmssRc xmss_write_key_mem(const byte * priv, word32 privSz,
  35588. void *context)
  35589. {
  35590. /* WARNING: THIS IS AN INSECURE WRITE CALLBACK THAT SHOULD ONLY
  35591. * BE USED FOR TESTING PURPOSES! Production applications should
  35592. * write only to non-volatile storage. */
  35593. XMEMCPY(context, priv, privSz);
  35594. return WC_XMSS_RC_SAVED_TO_NV_MEMORY;
  35595. }
  35596. static enum wc_XmssRc xmss_read_key_mem(byte * priv, word32 privSz,
  35597. void *context)
  35598. {
  35599. /* WARNING: THIS IS AN INSECURE READ CALLBACK THAT SHOULD ONLY
  35600. * BE USED FOR TESTING PURPOSES! */
  35601. XMEMCPY(priv, context, privSz);
  35602. return WC_XMSS_RC_READ_TO_MEMORY;
  35603. }
  35604. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t xmss_test(void)
  35605. {
  35606. int i = 0;
  35607. int j = 0;
  35608. XmssKey signingKey;
  35609. XmssKey verifyKey;
  35610. WC_RNG rng;
  35611. word32 pkSz = 0;
  35612. word32 skSz = 0;
  35613. word32 sigSz = 0;
  35614. word32 bufSz = 0;
  35615. unsigned char * sk = NULL;
  35616. unsigned char * old_sk = NULL;
  35617. const char * msg = "XMSS post quantum signature test";
  35618. word32 msgSz = (word32) XSTRLEN(msg);
  35619. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10
  35620. const char * param = "XMSS-SHA2_10_256";
  35621. #elif WOLFSSL_XMSS_MIN_HEIGHT <= 20
  35622. const char * param = "XMSSMT-SHA2_20/4_256";
  35623. #elif WOLFSSL_XMSS_MIN_HEIGHT <= 40
  35624. const char * param = "XMSSMT-SHA2_40/8_256";
  35625. #else
  35626. const char * param = "XMSSMT-SHA2_60/12_256";
  35627. #endif
  35628. byte * sig = NULL;
  35629. int ret2 = -1;
  35630. int ret = WC_TEST_RET_ENC_NC;
  35631. WOLFSSL_ENTER("xmss_test");
  35632. #ifndef HAVE_FIPS
  35633. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  35634. #else
  35635. ret = wc_InitRng(&rng);
  35636. #endif
  35637. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  35638. ret = wc_XmssKey_Init(&signingKey, NULL, INVALID_DEVID);
  35639. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  35640. ret = wc_XmssKey_Init(&verifyKey, NULL, INVALID_DEVID);
  35641. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  35642. /* Set the parameter string to the signing key, and
  35643. * get sizes for secret key, pub key, and signature. */
  35644. ret = wc_XmssKey_SetParamStr(&signingKey, param);
  35645. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  35646. ret = wc_XmssKey_GetPubLen(&signingKey, &pkSz);
  35647. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  35648. if (pkSz != XMSS_SHA256_PUBLEN) {
  35649. return WC_TEST_RET_ENC_EC(pkSz);
  35650. }
  35651. ret = wc_XmssKey_GetPrivLen(&signingKey, &skSz);
  35652. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  35653. ret = wc_XmssKey_GetSigLen(&signingKey, &sigSz);
  35654. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  35655. /* Allocate signature array. */
  35656. sig = (byte *)XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35657. if (sig == NULL) { return WC_TEST_RET_ENC_ERRNO; }
  35658. bufSz = sigSz;
  35659. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  35660. fprintf(stderr, "param: %s\n", param);
  35661. fprintf(stderr, "pkSz: %d\n", pkSz);
  35662. fprintf(stderr, "skSz: %d\n", skSz);
  35663. fprintf(stderr, "sigSz: %d\n", sigSz);
  35664. #endif
  35665. /* Allocate current and old secret keys.*/
  35666. sk = (unsigned char *)XMALLOC(skSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35667. if (sk == NULL) { return WC_TEST_RET_ENC_ERRNO; }
  35668. old_sk = (unsigned char *)XMALLOC(skSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35669. if (old_sk == NULL) { return WC_TEST_RET_ENC_ERRNO; }
  35670. XMEMSET(sk, 0, skSz);
  35671. XMEMSET(old_sk, 0, skSz);
  35672. XMEMSET(sig, 0, sigSz);
  35673. ret = wc_XmssKey_SetWriteCb(&signingKey, xmss_write_key_mem);
  35674. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  35675. ret = wc_XmssKey_SetReadCb(&signingKey, xmss_read_key_mem);
  35676. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  35677. ret = wc_XmssKey_SetContext(&signingKey, (void *) sk);
  35678. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  35679. ret = wc_XmssKey_MakeKey(&signingKey, &rng);
  35680. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  35681. /* Export the pub to a verify key. */
  35682. ret = wc_XmssKey_ExportPub(&verifyKey, &signingKey);
  35683. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  35684. /* Repeat a few times to check that:
  35685. * 1. The secret key is mutated on each sign.
  35686. * 2. We can verify each new signature.
  35687. * Only do a few times, because the full signature space
  35688. * for this parameter set is huge. */
  35689. for (i = 0; i < 10; ++i) {
  35690. XMEMCPY(old_sk, sk, skSz);
  35691. ret = wc_XmssKey_Sign(&signingKey, sig, &sigSz, (byte *) msg, msgSz);
  35692. if (ret != 0) { return WC_TEST_RET_ENC_I(i); }
  35693. if (sigSz != bufSz) { return WC_TEST_RET_ENC_I(i); }
  35694. /* Old secret key and current secret key should not match. */
  35695. ret = XMEMCMP(old_sk, sk, skSz);
  35696. if (ret == 0) { return WC_TEST_RET_ENC_I(i); }
  35697. ret = wc_XmssKey_Verify(&verifyKey, sig, sigSz, (byte *) msg, msgSz);
  35698. if (ret != 0) { return WC_TEST_RET_ENC_I(i); }
  35699. /* Flip bits in a few places throughout the signature, stepping in multiple
  35700. * of hash size. These should all fail with -1. */
  35701. for (j = 0; j < (int) sigSz; j+= 4 * 32) {
  35702. sig[j] ^= 1;
  35703. ret2 = wc_XmssKey_Verify(&verifyKey, sig, sigSz, (byte *) msg,
  35704. msgSz);
  35705. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  35706. /* Verify passed when it should have failed. */
  35707. return WC_TEST_RET_ENC_I(j);
  35708. }
  35709. /* Flip this spot back. */
  35710. sig[j] ^= 1;
  35711. }
  35712. }
  35713. /* Cleanup everything. */
  35714. if (sig != NULL) {
  35715. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35716. sig = NULL;
  35717. }
  35718. if (sk != NULL) {
  35719. XFREE(sk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35720. sk = NULL;
  35721. }
  35722. if (old_sk != NULL) {
  35723. XFREE(old_sk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35724. old_sk = NULL;
  35725. }
  35726. wc_XmssKey_Free(&signingKey);
  35727. wc_FreeRng(&rng);
  35728. return ret;
  35729. }
  35730. #endif /*if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY)*/
  35731. #if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_SMALL_STACK) && \
  35732. WOLFSSL_XMSS_MIN_HEIGHT <= 10
  35733. /* A simple xmss verify only test using:
  35734. * XMSS-SHA2_10_256
  35735. * pub len: 68
  35736. * msg len: 32
  35737. * sig len: 2500
  35738. *
  35739. * These were generated with the test xmss_fast, from the unpatched
  35740. * xmss-reference repository:
  35741. * https://github.com/XMSS/xmss-reference
  35742. * */
  35743. static const byte xmss_pub[XMSS_SHA256_PUBLEN] =
  35744. {
  35745. 0x00,0x00,0x00,0x01,0xA5,0x41,0x31,0x96,
  35746. 0x0A,0xF9,0xF3,0xB2,0x4B,0x2E,0x5B,0x3E,
  35747. 0xCA,0x74,0xAD,0x6C,0xA5,0x89,0xAD,0x2C,
  35748. 0x0E,0x96,0xB3,0x54,0xFB,0x5B,0x63,0x50,
  35749. 0x96,0x81,0xE2,0x59,0x72,0x10,0x09,0x54,
  35750. 0xBB,0x39,0xAC,0xEE,0x78,0xEF,0x95,0xEC,
  35751. 0x01,0x1D,0xF0,0x36,0x68,0xE2,0xC4,0xA5,
  35752. 0x2F,0x60,0x42,0x7E,0xD3,0x8E,0xAA,0x27,
  35753. 0xC9,0xB7,0x39,0x4E
  35754. };
  35755. static /* not const */ byte xmss_msg[32] =
  35756. {
  35757. 0x07,0x9F,0x80,0x86,0xDB,0x76,0x27,0xDF,
  35758. 0xED,0x5B,0x2A,0x81,0x60,0x60,0x7D,0xB4,
  35759. 0xE8,0x7A,0x69,0x45,0x20,0x6B,0xA2,0x96,
  35760. 0xC0,0x21,0xA5,0x46,0x29,0x63,0x9B,0x37
  35761. };
  35762. /* This was actually the 5th signature produced from
  35763. * xmss_fast test in xmss-reference. */
  35764. static /* not const */ byte xmss_sig[2500] =
  35765. {
  35766. 0x00,0x00,0x00,0x05,0xF0,0x15,0x34,0xBA,
  35767. 0x92,0x03,0x6A,0xB9,0xA5,0x23,0x86,0x11,
  35768. 0xAE,0x65,0x0A,0x5C,0x78,0x2C,0xC9,0xBE,
  35769. 0x7E,0xA6,0xDC,0xA2,0x8B,0xA9,0x9C,0x50,
  35770. 0xF6,0x61,0x8D,0x9D,0xD7,0xE9,0xC0,0xF8,
  35771. 0x67,0xCD,0x8A,0xC4,0x9B,0x74,0x96,0x07,
  35772. 0x5D,0xF2,0xC9,0xCC,0x28,0x05,0xB1,0xBE,
  35773. 0x5E,0xA4,0xBA,0xBE,0xAB,0xD8,0x21,0x6B,
  35774. 0x21,0x5F,0xAB,0xB7,0x6C,0xEC,0x2F,0xC8,
  35775. 0xC6,0x74,0x3E,0x97,0x1B,0xC3,0x45,0x57,
  35776. 0xAF,0xAA,0x1E,0xA8,0xF2,0x86,0xA8,0xAA,
  35777. 0x43,0x6D,0x66,0xE9,0x81,0x14,0xDE,0x09,
  35778. 0x39,0xD2,0xAF,0xD1,0x4C,0xE7,0x75,0x18,
  35779. 0x0D,0xAA,0x29,0xA1,0x92,0x53,0xCC,0xE9,
  35780. 0xF3,0x0B,0x1E,0x3B,0xE2,0xAE,0x80,0x0C,
  35781. 0xE7,0x7A,0x7C,0x13,0x8A,0x28,0xC6,0x5F,
  35782. 0x0A,0xA4,0xA3,0x73,0x0A,0x3A,0xC2,0xA6,
  35783. 0x3B,0xB4,0x30,0x67,0xC0,0x36,0x18,0xA1,
  35784. 0x58,0xCD,0xAD,0x54,0x36,0x64,0xCE,0xFD,
  35785. 0x52,0xFF,0x70,0x7E,0x09,0xFB,0x13,0xA2,
  35786. 0xEA,0xDF,0x67,0x8D,0x6C,0x42,0xB2,0x78,
  35787. 0xF5,0x7D,0x5C,0x4B,0xF7,0x8E,0xCF,0x3E,
  35788. 0xB7,0xC6,0xC1,0x23,0xFA,0x65,0xDE,0xD2,
  35789. 0xFA,0x40,0x51,0x97,0x0D,0x52,0x32,0x76,
  35790. 0x7E,0x82,0x8D,0xD0,0xB9,0x1E,0x62,0xD9,
  35791. 0x1E,0xC1,0xDB,0x40,0x43,0x37,0x4A,0x23,
  35792. 0x8A,0x1D,0x35,0xFA,0xF4,0x53,0x11,0x5A,
  35793. 0xB5,0x6D,0x1E,0x8B,0x22,0xC8,0x7D,0x2A,
  35794. 0xE4,0x94,0xAA,0x25,0x20,0x40,0x96,0xDB,
  35795. 0x82,0x62,0xBA,0x8F,0x8B,0x45,0xCB,0x4F,
  35796. 0x35,0x88,0x33,0xEB,0xEF,0xB3,0xBA,0xA7,
  35797. 0x09,0x72,0xB3,0x4C,0xEC,0xF2,0xC3,0xC7,
  35798. 0x5E,0x02,0x6C,0x41,0x93,0xCB,0x3C,0x89,
  35799. 0x12,0x09,0x68,0x54,0x8E,0xEC,0x6A,0x7E,
  35800. 0x20,0xE1,0x70,0x3D,0x8C,0xEB,0xB4,0x36,
  35801. 0xBE,0x91,0xBE,0x97,0xB5,0xA6,0x34,0x16,
  35802. 0x95,0x0F,0x10,0x26,0xA9,0x13,0x80,0x88,
  35803. 0x9C,0xAA,0x68,0xEC,0x34,0x70,0x4A,0x15,
  35804. 0x9B,0x5E,0x57,0x05,0x87,0x1C,0xF8,0x35,
  35805. 0x45,0x29,0xE9,0x6E,0xF2,0x70,0x13,0x42,
  35806. 0x89,0x4E,0x77,0xC0,0x18,0xC7,0x55,0x6D,
  35807. 0xE7,0xFA,0x0D,0x63,0x83,0x16,0x19,0x01,
  35808. 0x2D,0xFD,0x31,0x14,0x94,0xCA,0x3E,0x0E,
  35809. 0xD6,0x11,0x34,0x81,0x57,0x58,0xEC,0x24,
  35810. 0xA4,0x17,0x63,0xD3,0x25,0x00,0xBF,0x7D,
  35811. 0x78,0x5D,0xC5,0xD8,0xC6,0xC1,0xBD,0x8C,
  35812. 0xD0,0x94,0x0A,0xB1,0x33,0xA5,0x4B,0x31,
  35813. 0x25,0xF5,0xAF,0xE7,0x84,0x26,0xAA,0x05,
  35814. 0xBB,0xF3,0x9A,0xAF,0x58,0x36,0x40,0xEF,
  35815. 0x3D,0xA2,0xBD,0xCA,0xA1,0x8D,0x2F,0x6D,
  35816. 0x54,0xD2,0x62,0x33,0x09,0xAE,0xE6,0x73,
  35817. 0xD6,0x44,0xE8,0x7C,0x5C,0x39,0x2B,0x78,
  35818. 0x94,0x14,0xC7,0xC9,0xAF,0xEC,0x77,0x36,
  35819. 0xA1,0x61,0x61,0xF1,0xD0,0x09,0xA2,0xEE,
  35820. 0xE7,0x55,0xD7,0x35,0x89,0x89,0x9B,0xCF,
  35821. 0xFA,0xA6,0x09,0x1E,0x3B,0xBD,0x5D,0xD9,
  35822. 0x25,0xE7,0xED,0xDD,0x7C,0xF0,0x1C,0x57,
  35823. 0xE0,0x06,0xBB,0x08,0x39,0x59,0xDF,0xD7,
  35824. 0xAF,0x4B,0x88,0x0D,0x87,0x8F,0x4A,0xF3,
  35825. 0x1C,0xD4,0x4B,0xB3,0xE2,0xF3,0x1B,0x86,
  35826. 0x4F,0xCD,0x35,0x75,0xE2,0x03,0xF9,0x1D,
  35827. 0xBF,0x3E,0xD1,0x7B,0xC7,0x23,0x11,0x75,
  35828. 0x5F,0x92,0x0D,0x98,0xEE,0x14,0xE1,0xDA,
  35829. 0x7A,0x02,0x17,0x47,0x6B,0x41,0xEA,0x47,
  35830. 0xA1,0xAF,0x06,0x79,0x1A,0x52,0x6F,0x19,
  35831. 0x31,0x70,0x71,0xBD,0xC2,0x61,0x8D,0xB7,
  35832. 0xEE,0x6B,0x69,0x2A,0xE8,0x21,0x7A,0x95,
  35833. 0xBE,0x86,0x2A,0xA1,0xF4,0xE2,0x2F,0x17,
  35834. 0x02,0xFD,0xAD,0x17,0x9F,0x0A,0x0A,0x78,
  35835. 0xA9,0x92,0x30,0x21,0x72,0x2B,0x28,0xF8,
  35836. 0xF2,0x3E,0x05,0xD5,0xAC,0xC0,0x82,0xF8,
  35837. 0xD2,0xDA,0xD0,0xA3,0xBC,0x93,0xDB,0xA5,
  35838. 0x46,0xDE,0x14,0x1E,0xD4,0x3A,0x5D,0x79,
  35839. 0x3D,0x31,0x4B,0x06,0xCE,0x22,0x29,0x3C,
  35840. 0x98,0xB6,0x18,0x8A,0xAE,0xF7,0xBA,0x22,
  35841. 0x88,0xA1,0xEE,0xC0,0x14,0x4C,0x4A,0xA0,
  35842. 0x57,0x0A,0xD3,0x18,0xA2,0x3D,0xDD,0xC7,
  35843. 0x83,0x73,0xFC,0x38,0x9B,0x31,0xA3,0xE1,
  35844. 0x17,0x76,0xA1,0xA2,0x69,0xFC,0xAB,0x08,
  35845. 0x80,0x72,0x8D,0xF5,0xE4,0x14,0xB7,0x6B,
  35846. 0x03,0xFF,0xE8,0x11,0x4B,0x06,0x55,0x7E,
  35847. 0x36,0x21,0x2F,0xD7,0x54,0x82,0xC9,0x31,
  35848. 0xB4,0x85,0x68,0x41,0xEF,0x75,0xB0,0x3A,
  35849. 0xEA,0x4F,0xE0,0xEC,0x72,0xCC,0x33,0x96,
  35850. 0xCE,0x7D,0xAD,0xDD,0x0D,0x27,0x05,0x6E,
  35851. 0xA2,0xD4,0x11,0x07,0xD8,0x7D,0x27,0xD4,
  35852. 0x80,0x8F,0x00,0x22,0xE4,0xFC,0x2C,0x9D,
  35853. 0xD5,0xD8,0x18,0x7F,0x4E,0xF4,0xB9,0x7F,
  35854. 0xEF,0xD6,0x00,0x08,0x5C,0x05,0x04,0x1E,
  35855. 0x9A,0xC6,0x8D,0xCC,0x19,0xD9,0x0B,0x06,
  35856. 0xCC,0x6A,0x17,0xE2,0x03,0x23,0xDB,0x1C,
  35857. 0xBC,0xA2,0xB9,0xA2,0x95,0x3C,0x73,0xD8,
  35858. 0xFF,0xE6,0x0E,0xAE,0x04,0xB2,0xFC,0x91,
  35859. 0x4F,0xEF,0x8A,0x58,0xB7,0x31,0x68,0x4C,
  35860. 0x1E,0xD0,0x5B,0x85,0xCC,0x03,0xDC,0xF4,
  35861. 0xAC,0xDB,0x03,0x9B,0x35,0x33,0x08,0x71,
  35862. 0xD0,0x50,0x8D,0xDC,0xE3,0x3A,0x98,0x40,
  35863. 0x41,0x80,0xDD,0x35,0xE1,0xA2,0xAF,0x14,
  35864. 0x9A,0xDB,0xD3,0x68,0x14,0xE2,0x50,0x7A,
  35865. 0x76,0x3F,0xE4,0xA4,0x1B,0xAA,0xC1,0x06,
  35866. 0x87,0x9A,0x92,0xF9,0xBE,0x9E,0x86,0x8C,
  35867. 0x92,0x1D,0x74,0xB1,0x7F,0x27,0x43,0xC0,
  35868. 0xEE,0x2E,0xC2,0x6C,0x6D,0xAA,0x0C,0x0E,
  35869. 0x71,0xC9,0x56,0xD6,0x3A,0x56,0xCB,0x90,
  35870. 0xD1,0x7E,0x6E,0x1C,0x6A,0x00,0x2D,0x02,
  35871. 0x2C,0x96,0xF0,0x2A,0x37,0x37,0x18,0x07,
  35872. 0x0B,0xF4,0xB4,0x8C,0x30,0xF2,0xA4,0xAB,
  35873. 0x66,0xFB,0x8B,0x22,0xC0,0x00,0x7E,0x05,
  35874. 0xB6,0xF9,0x95,0x49,0x33,0xA1,0xDC,0x97,
  35875. 0x0C,0x5C,0x61,0x46,0xE2,0xD7,0x87,0x4B,
  35876. 0xC4,0xC7,0x5F,0x26,0x06,0x84,0xD7,0x47,
  35877. 0x05,0xF1,0x33,0xFF,0x85,0x85,0xB2,0xBD,
  35878. 0x1F,0x44,0xC6,0xC2,0x7D,0x51,0xBE,0x0E,
  35879. 0xB5,0xC4,0x44,0x2F,0xFE,0x73,0x5F,0xF4,
  35880. 0xA4,0xEF,0xE2,0xF1,0x73,0x0B,0xEF,0x3E,
  35881. 0x2B,0xD7,0xCC,0x9F,0xDA,0x1A,0x7E,0x92,
  35882. 0x39,0xA1,0x55,0xBF,0x60,0x0A,0xDB,0x23,
  35883. 0x74,0xFE,0xE7,0x05,0x63,0xA9,0x85,0x52,
  35884. 0x9F,0xCC,0xC3,0xFF,0xF6,0x6C,0x1B,0x4E,
  35885. 0x4F,0x01,0xBD,0xC3,0xEB,0x37,0xEC,0x29,
  35886. 0x21,0x3B,0x2C,0xC9,0x2E,0x93,0x20,0x3E,
  35887. 0x19,0xC0,0x8B,0xE8,0x33,0xCD,0xC6,0x6A,
  35888. 0x6E,0x72,0x13,0x15,0xA1,0x90,0x20,0x0C,
  35889. 0x14,0x66,0xED,0xCC,0xA4,0xDD,0x7F,0x58,
  35890. 0x53,0xBC,0x4A,0x68,0xFC,0x86,0x3E,0xAA,
  35891. 0xF1,0x17,0x0F,0x3E,0x20,0x54,0x93,0xF4,
  35892. 0x98,0xBF,0xB4,0x07,0x05,0xBD,0x70,0xE7,
  35893. 0xD7,0x34,0xFD,0xE3,0x69,0xDF,0xCD,0xF5,
  35894. 0x1A,0x73,0x6E,0xC9,0x2B,0x21,0xFB,0xB8,
  35895. 0x7E,0x44,0x10,0x83,0x56,0xCE,0xD5,0x15,
  35896. 0x9A,0x75,0xFC,0x91,0x8E,0x6B,0x9E,0x1A,
  35897. 0x3A,0x33,0x39,0x35,0xB4,0x0D,0x74,0xF4,
  35898. 0xFB,0x4C,0x0E,0x37,0xFE,0x82,0x95,0x46,
  35899. 0x6B,0xD2,0x6E,0xEE,0xCD,0x4D,0x38,0xAF,
  35900. 0x0A,0xAA,0xF1,0xD5,0xA4,0x7C,0x04,0xD8,
  35901. 0xB9,0xDB,0x11,0x68,0x88,0x35,0x41,0xDE,
  35902. 0x31,0x33,0x0C,0xDC,0x2D,0x4C,0xA8,0x20,
  35903. 0xCC,0x2C,0x4C,0x63,0xAB,0xBA,0xDF,0x48,
  35904. 0x84,0xD5,0x25,0xBC,0x70,0xE3,0x49,0xAA,
  35905. 0x43,0xCA,0x8B,0xE7,0x9F,0xDD,0x20,0x76,
  35906. 0x9B,0x38,0xF4,0xBA,0x4D,0x4E,0x34,0x4A,
  35907. 0xAF,0x81,0xE7,0x0B,0xEC,0xE9,0x59,0xC1,
  35908. 0x35,0x22,0x7F,0x69,0x46,0x62,0xD2,0x18,
  35909. 0x6E,0x1F,0x79,0xD1,0xAD,0xC3,0x84,0x95,
  35910. 0x96,0xB2,0x18,0x58,0x5E,0x7E,0x0C,0x25,
  35911. 0x0A,0x0F,0x69,0xA3,0x1D,0xEC,0x29,0xCB,
  35912. 0xDA,0xA2,0xD1,0x1A,0x10,0xA5,0x52,0xC3,
  35913. 0x62,0x1E,0xC5,0x83,0xFF,0xA3,0x56,0xC2,
  35914. 0xFD,0x87,0x3B,0x57,0x52,0x98,0x36,0x95,
  35915. 0x77,0x6B,0xE5,0x49,0x10,0x8E,0x39,0xDD,
  35916. 0xCA,0x4B,0xB3,0x9F,0x4C,0x0C,0x11,0x62,
  35917. 0xF3,0x22,0x78,0xDB,0x48,0xEB,0x68,0xFE,
  35918. 0xE4,0x2A,0xE9,0xAA,0x8F,0x7A,0x2F,0x69,
  35919. 0xA5,0xC5,0x03,0x2D,0xEF,0x62,0xA8,0x71,
  35920. 0x65,0x06,0x40,0x84,0x10,0x0F,0xF2,0xED,
  35921. 0xBC,0x70,0x71,0x69,0x24,0xA2,0xBF,0x83,
  35922. 0x39,0xDD,0xFA,0xA2,0x7B,0xE5,0xEC,0x3D,
  35923. 0xFE,0x3B,0x52,0x6E,0x3D,0x82,0xA6,0x2A,
  35924. 0x86,0x01,0x61,0x51,0x63,0xBF,0xF9,0x0A,
  35925. 0x06,0x72,0xF1,0xD5,0x39,0x0C,0xBA,0xC9,
  35926. 0x78,0xC6,0x77,0x22,0xE4,0x96,0x6E,0xB1,
  35927. 0x48,0x62,0x84,0x62,0x2D,0xEA,0x49,0x56,
  35928. 0x50,0x86,0x3F,0x90,0xC3,0x01,0x42,0x45,
  35929. 0xED,0xE6,0x9A,0x65,0x19,0x93,0x7F,0x48,
  35930. 0x16,0xF2,0x50,0xA7,0x70,0xB3,0xF5,0xDB,
  35931. 0x0E,0x5E,0x22,0x9E,0x64,0x04,0x26,0x69,
  35932. 0xC1,0x16,0xEE,0x65,0x08,0x82,0x27,0x65,
  35933. 0xEC,0x3D,0xDF,0x51,0x5E,0x2D,0xE8,0x76,
  35934. 0xF2,0xE3,0xE4,0x24,0x04,0x88,0x06,0x0F,
  35935. 0xB2,0x7B,0x9B,0x72,0x3D,0x4C,0x7D,0x6A,
  35936. 0x1F,0xB2,0xA2,0xD2,0x35,0xD6,0x40,0x25,
  35937. 0xC2,0x0B,0x25,0xF9,0xDF,0x26,0xE4,0xDC,
  35938. 0xFB,0xB1,0x84,0x84,0x77,0x1B,0x45,0x51,
  35939. 0x60,0xD5,0xF0,0xB6,0x09,0xE6,0xBC,0xE3,
  35940. 0x1C,0x70,0x96,0x2C,0xD3,0x9D,0x7D,0x7F,
  35941. 0xB1,0x70,0xDA,0x79,0xB8,0x74,0x99,0xBF,
  35942. 0x84,0x95,0xCC,0x93,0xD7,0x51,0xDD,0x66,
  35943. 0xD3,0x70,0x0C,0x75,0x86,0x09,0x06,0xFD,
  35944. 0x66,0x14,0x80,0xCD,0xF3,0x59,0xB4,0x92,
  35945. 0x5F,0xE4,0xEE,0x00,0xA8,0xB0,0x8B,0x5C,
  35946. 0x3E,0xDB,0x8A,0x9C,0x0B,0xB5,0x99,0xC2,
  35947. 0x0D,0x81,0x09,0x06,0x6C,0x28,0xC0,0x7E,
  35948. 0xA5,0x07,0x70,0x64,0xD7,0x41,0xF4,0xC3,
  35949. 0x66,0x61,0x1C,0xA8,0x51,0xF6,0x3C,0xBA,
  35950. 0xE0,0x94,0xA3,0x11,0x8C,0x2E,0xBA,0x13,
  35951. 0xB2,0x47,0x48,0x93,0xB4,0x1A,0x2C,0x9A,
  35952. 0x6E,0x8E,0x30,0x66,0x7B,0xD3,0xBB,0x3B,
  35953. 0x5D,0x97,0x0D,0xE4,0xEA,0x24,0x28,0x9E,
  35954. 0xB4,0x88,0xCE,0x1D,0x7D,0x6F,0x39,0xB3,
  35955. 0x87,0x21,0xE5,0x08,0x93,0xF0,0xD4,0x9D,
  35956. 0x2D,0x91,0xC9,0xFD,0x0C,0x74,0x34,0xB4,
  35957. 0x1F,0xFE,0xDA,0xDC,0x10,0x5B,0x8D,0x2B,
  35958. 0x87,0xD3,0x42,0xB4,0xAE,0x32,0x9C,0xAE,
  35959. 0x4C,0x99,0xD8,0xED,0x44,0x41,0x07,0xE0,
  35960. 0x8F,0xBD,0xA5,0x7C,0x5A,0xDF,0x91,0x29,
  35961. 0x00,0xB5,0x4B,0xC3,0x3A,0x40,0x6C,0x48,
  35962. 0xAB,0x2A,0xF3,0x02,0xCB,0xB3,0x69,0xDA,
  35963. 0x06,0x0C,0x4D,0x5C,0x45,0xC3,0x28,0xAC,
  35964. 0x7A,0x01,0xD4,0xF8,0xCB,0x07,0x63,0x89,
  35965. 0x09,0x34,0x78,0xA7,0x14,0x39,0xCF,0x2D,
  35966. 0x94,0x8D,0x7A,0x4E,0x4E,0xBD,0xC4,0x32,
  35967. 0xAB,0x21,0xC9,0xDA,0x3F,0x5F,0x04,0x6B,
  35968. 0x14,0x40,0x18,0x18,0x2F,0xF9,0x46,0x17,
  35969. 0x57,0x54,0x9B,0x28,0x7B,0xBD,0xF9,0xA2,
  35970. 0x13,0xAC,0x69,0x24,0xB1,0x31,0x39,0xBF,
  35971. 0x8D,0x75,0xC3,0xFD,0x03,0x54,0x5A,0xFD,
  35972. 0xD4,0x7A,0xB7,0x56,0x4F,0x66,0x43,0x57,
  35973. 0x1B,0xFB,0xF9,0x92,0x7A,0x83,0xE6,0xFF,
  35974. 0xB4,0xBA,0x83,0xD2,0x61,0x8E,0x4A,0x82,
  35975. 0x82,0xA8,0xF5,0x0C,0xD2,0x43,0x53,0xA8,
  35976. 0x85,0x0A,0xD4,0x69,0x7B,0x04,0x71,0x3B,
  35977. 0x80,0x49,0x27,0x47,0x12,0xB6,0xB0,0xEA,
  35978. 0x90,0x0A,0xFA,0xA8,0xC8,0x78,0x61,0xDE,
  35979. 0x30,0x12,0xBB,0xDC,0xA6,0x57,0x56,0x30,
  35980. 0x6E,0xF1,0xA8,0x3B,0xF6,0x09,0x07,0xEA,
  35981. 0x31,0xE2,0x08,0x23,0x31,0x0F,0xD4,0x34,
  35982. 0xE3,0x60,0xC2,0x2B,0xDB,0x5A,0x99,0xCF,
  35983. 0xD4,0x6B,0x4E,0x75,0x65,0x35,0xE8,0x8B,
  35984. 0x93,0x7D,0xCA,0x11,0x47,0xF0,0x3E,0x11,
  35985. 0x5C,0xD1,0xEE,0x4B,0x11,0xB4,0x65,0x2B,
  35986. 0x6B,0x79,0xC0,0x86,0x60,0xA4,0x4B,0x24,
  35987. 0xA0,0x5C,0x70,0x34,0xC3,0x7C,0xE7,0x4F,
  35988. 0x97,0x89,0x4D,0xFE,0x22,0x89,0x3A,0xE9,
  35989. 0x07,0xB9,0x1A,0x86,0xB8,0x7A,0x12,0x38,
  35990. 0xE1,0x24,0x46,0xBC,0x9B,0x21,0xCD,0xAC,
  35991. 0x30,0xAB,0x98,0x21,0x31,0xC5,0x17,0x3F,
  35992. 0x1E,0x56,0xC3,0x18,0xCE,0xF0,0xA1,0xCC,
  35993. 0xFF,0x9D,0xA8,0x53,0xAF,0x74,0x77,0x54,
  35994. 0x02,0x9A,0x8F,0xA4,0xD4,0xBD,0xB2,0x1A,
  35995. 0xBA,0x52,0x2E,0x19,0xBE,0x49,0x11,0x45,
  35996. 0x02,0x01,0x7A,0xBF,0x28,0xD6,0x18,0xED,
  35997. 0xBD,0xCE,0xE4,0xDE,0xB5,0xF1,0x53,0x5D,
  35998. 0x65,0xF9,0x5F,0x83,0x8F,0x2D,0xF2,0x82,
  35999. 0xA0,0x2D,0x28,0xD3,0x0A,0x9E,0x0F,0x7F,
  36000. 0xC7,0xC4,0x43,0x7F,0xC3,0x0E,0x06,0xEB,
  36001. 0x4E,0xB4,0x2D,0xFA,0xDD,0x48,0xAB,0xF4,
  36002. 0x7D,0x41,0x48,0x33,0x5A,0xE6,0x70,0x02,
  36003. 0xE7,0x71,0x8D,0xD9,0x6B,0x0C,0x5A,0x8F,
  36004. 0xA4,0xC1,0xB7,0x4E,0x96,0x83,0xD6,0xA7,
  36005. 0x1D,0xF1,0x88,0xB3,0x6E,0xF4,0x12,0xA9,
  36006. 0xF6,0x31,0x69,0x66,0xFE,0xFE,0x02,0xF2,
  36007. 0x86,0x6D,0xBB,0x57,0x51,0x8C,0x4C,0xE9,
  36008. 0x7C,0x92,0x3E,0x3A,0xD3,0x2D,0xA8,0x82,
  36009. 0x53,0x84,0x26,0x89,0xBB,0xCC,0x13,0x12,
  36010. 0x3D,0x94,0xBB,0xDF,0x3D,0x4C,0xDF,0x27,
  36011. 0x9B,0x1F,0xB8,0xB6,0xE4,0xEA,0xA2,0x07,
  36012. 0xF8,0x4D,0x42,0x8F,0x29,0x90,0xFE,0x21,
  36013. 0x20,0xE9,0x55,0x02,0xAD,0x90,0xA7,0x77,
  36014. 0x4E,0x29,0xB6,0xD9,0x14,0x94,0xB2,0x25,
  36015. 0xA4,0xB2,0x0E,0x96,0x31,0xAB,0x9E,0x93,
  36016. 0x49,0xAC,0xA9,0xCB,0x68,0x22,0xBA,0xB8,
  36017. 0x57,0x5C,0x9D,0x65,0xC1,0xF1,0xFC,0x99,
  36018. 0x7C,0x3C,0xE9,0xEA,0x4B,0x29,0x22,0x2F,
  36019. 0xDB,0x17,0x21,0x8D,0xB0,0x13,0xBF,0xEE,
  36020. 0x7D,0xE4,0x8B,0x6D,0x17,0xE0,0x53,0x92,
  36021. 0x0B,0x32,0x6B,0xB1,0x65,0x2E,0xA7,0x83,
  36022. 0xFD,0x62,0x62,0xE3,0xAA,0x81,0xE8,0xD6,
  36023. 0xF7,0xB1,0x30,0x65,0x80,0x9F,0x77,0x1E,
  36024. 0x4A,0xEA,0xE8,0x45,0x32,0x12,0x3A,0xFB,
  36025. 0x22,0xE9,0xA9,0xF6,0xCB,0xAB,0xA8,0x0C,
  36026. 0x20,0xA8,0x7C,0xF9,0xF7,0x53,0xC1,0xB4,
  36027. 0xC0,0x5D,0x06,0x45,0xDD,0x7E,0xA7,0x34,
  36028. 0xA1,0x21,0xC2,0x62,0xAB,0x22,0x45,0x3D,
  36029. 0x73,0x4C,0x26,0xD1,0x1A,0xB2,0xF0,0xB2,
  36030. 0x6D,0x11,0x70,0x58,0xAA,0xF5,0xA4,0xF5,
  36031. 0xF8,0x0B,0x3D,0xC1,0xF6,0x17,0x70,0x15,
  36032. 0xCD,0x72,0x02,0x7E,0x4E,0x94,0x96,0x0A,
  36033. 0x56,0xCC,0xA5,0xA3,0xB3,0x7E,0xDD,0x5A,
  36034. 0x72,0xD2,0xFB,0xAC,0x3D,0x0E,0x66,0x65,
  36035. 0xE9,0x08,0x6C,0xB0,0x1C,0xE2,0x1A,0x82,
  36036. 0xF6,0xF3,0x34,0x89,0x73,0x02,0x5B,0x42,
  36037. 0x6D,0x40,0x61,0xB6,0xE0,0xE6,0x53,0x32,
  36038. 0xA5,0x72,0x17,0x4F,0x3B,0x51,0x4F,0xBC,
  36039. 0x00,0xE0,0x69,0x26,0xA9,0xAE,0x83,0xE3,
  36040. 0x73,0x7F,0x71,0x97,0xE0,0xDC,0x7C,0x63,
  36041. 0x9C,0x85,0x5F,0xDF,0x7D,0xE4,0x6C,0xD8,
  36042. 0xA9,0x3A,0x6F,0x5E,0x4A,0x2E,0xB0,0xE7,
  36043. 0x8B,0x45,0xE2,0x90,0x05,0x37,0xE8,0xAB,
  36044. 0x49,0x48,0x4C,0xC0,0x59,0x1D,0x8C,0x46,
  36045. 0x5B,0x84,0xE0,0x83,0xCE,0xEA,0x4B,0xF9,
  36046. 0xD4,0xDC,0x63,0xDF,0x79,0xB7,0x5C,0x11,
  36047. 0x25,0x7F,0x90,0x2E,0x0A,0x38,0x03,0xEA,
  36048. 0xEA,0xA1,0x26,0x52,0x20,0x19,0xA3,0xBE,
  36049. 0xFC,0x9D,0xB7,0x6E,0xA6,0x58,0x8E,0x6D,
  36050. 0xC5,0x58,0xE9,0xED,0x2F,0x55,0x43,0x8B,
  36051. 0x03,0x8B,0xE6,0xA4,0xC2,0x25,0x4B,0x36,
  36052. 0xBA,0xD3,0x27,0x48,0x40,0x2E,0x87,0xA2,
  36053. 0xD4,0x12,0xC6,0x05,0x36,0x03,0x11,0x51,
  36054. 0xD1,0xF2,0xAC,0x71,0x2C,0xB6,0xC3,0xA5,
  36055. 0x57,0x0F,0xAF,0x4B,0xBD,0xCD,0x47,0x4C,
  36056. 0x3A,0x52,0x6F,0x47,0xE7,0x0B,0xB7,0xD5,
  36057. 0xF7,0xA6,0x39,0x63,0x82,0x08,0x4C,0x41,
  36058. 0x0E,0x2A,0x52,0x42,0x5A,0xEA,0x59,0xC7,
  36059. 0x94,0xFB,0xD0,0x88,0x47,0x27,0xF6,0x97,
  36060. 0x03,0x9E,0x29,0xB8,0x3A,0x67,0xE6,0xF3,
  36061. 0x95,0xA7,0x42,0xC1,0x96,0xD1,0x9A,0xA6,
  36062. 0xF0,0x09,0x0C,0xEA,0xE0,0xAB,0x0F,0x15,
  36063. 0xE9,0xC3,0xEB,0xA5,0x89,0x86,0x98,0x32,
  36064. 0x83,0xAB,0x30,0x33,0xAE,0x90,0x8D,0x2E,
  36065. 0xB3,0xAA,0x91,0xA6,0xD9,0xA4,0x4A,0x54,
  36066. 0xE0,0xD3,0x08,0xCC,0x79,0xCE,0xE4,0x15,
  36067. 0x31,0xA6,0xCE,0x61,0xCF,0x03,0x06,0xEE,
  36068. 0x8E,0xE2,0x64,0x29,0xD1,0x54,0x9B,0xD0,
  36069. 0x5F,0x09,0x2B,0x8B,0xD5,0xF8,0xD4,0x7D,
  36070. 0xF1,0x97,0x32,0xD9,0xEA,0x5A,0x0E,0x10,
  36071. 0x8C,0x4D,0xFB,0x55,0xE6,0x27,0x0C,0xBA,
  36072. 0xC1,0x73,0xC1,0x73,0xE3,0x1C,0x09,0xB3,
  36073. 0x6F,0xB4,0x12,0xFA,0xF3,0x29,0xDC,0x23,
  36074. 0x32,0xED,0x80,0x87,0x83,0xC2,0xF6,0x07,
  36075. 0xB5,0xA9,0x22,0xDE,0x66,0x1A,0xA7,0x4A,
  36076. 0x86,0xF1,0x39,0x9B,0xF4,0xE7,0x50,0x15,
  36077. 0x4A,0x55,0x3C,0x93,0xB9,0xF9,0xFD,0xDC,
  36078. 0xB3,0x5D,0x73,0x52
  36079. };
  36080. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t xmss_test_verify_only(void)
  36081. {
  36082. XmssKey verifyKey;
  36083. unsigned char pub_raw[XMSS_SHA256_PUBLEN];
  36084. word32 pub_len = sizeof(pub_raw);
  36085. word32 pkSz = 0;
  36086. word32 sigSz = 0;
  36087. const char * param = "XMSS-SHA2_10_256";
  36088. int j = 0;
  36089. int ret2 = WC_TEST_RET_ENC_NC;
  36090. int ret = WC_TEST_RET_ENC_NC;
  36091. int n_diff = 0;
  36092. WOLFSSL_ENTER("xmss_test_verify_only");
  36093. XMEMSET(pub_raw, 0, sizeof(pub_raw));
  36094. ret = wc_XmssKey_Init(&verifyKey, NULL, INVALID_DEVID);
  36095. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36096. ret = wc_XmssKey_SetParamStr(&verifyKey, param);
  36097. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36098. ret = wc_XmssKey_GetPubLen(&verifyKey, &pkSz);
  36099. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36100. if (pkSz != XMSS_SHA256_PUBLEN) {
  36101. return WC_TEST_RET_ENC_EC(pkSz);
  36102. }
  36103. ret = wc_XmssKey_GetSigLen(&verifyKey, &sigSz);
  36104. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36105. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  36106. fprintf(stderr, "param: %s\n", param);
  36107. fprintf(stderr, "pkSz: %d\n", pkSz);
  36108. fprintf(stderr, "sigSz: %d\n", sigSz);
  36109. #endif
  36110. if (sigSz != sizeof(xmss_sig)) {
  36111. return WC_TEST_RET_ENC_EC(sigSz);
  36112. }
  36113. ret = wc_XmssKey_ImportPubRaw(&verifyKey, xmss_pub, XMSS_SHA256_PUBLEN);
  36114. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36115. ret = wc_XmssKey_Verify(&verifyKey, xmss_sig, sizeof(xmss_sig),
  36116. (byte *) xmss_msg, sizeof(xmss_msg));
  36117. if (ret != 0) {
  36118. printf("error: wc_XmssKey_Verify returned %d, expected 0\n", ret);
  36119. return WC_TEST_RET_ENC_EC(ret);
  36120. }
  36121. /* Now test the ExportPubRaw API, verify we recover the original pub. */
  36122. ret = wc_XmssKey_ExportPubRaw(&verifyKey, pub_raw, &pub_len);
  36123. if (ret != 0) {
  36124. printf("error: wc_XmssKey_ExportPubRaw returned %d, expected 0\n", ret);
  36125. return WC_TEST_RET_ENC_EC(ret);
  36126. }
  36127. if (pub_len != XMSS_SHA256_PUBLEN) {
  36128. printf("error: xmss pub len %u, expected %d\n", pub_len,
  36129. XMSS_SHA256_PUBLEN);
  36130. return WC_TEST_RET_ENC_EC(pub_len);
  36131. }
  36132. n_diff = XMEMCMP(pub_raw, xmss_pub, sizeof(xmss_pub));
  36133. if (n_diff != 0) {
  36134. printf("error: exported and imported pub raw do not match: %d\n",
  36135. n_diff);
  36136. return WC_TEST_RET_ENC_EC(n_diff);
  36137. }
  36138. /* Flip bits in message. This should fail. */
  36139. xmss_msg[sizeof(xmss_msg) / 2] ^= 1;
  36140. ret2 = wc_XmssKey_Verify(&verifyKey, xmss_sig, sizeof(xmss_sig),
  36141. (byte *) xmss_msg, sizeof(xmss_msg));
  36142. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  36143. printf("error: wc_XmssKey_Verify returned %d, expected -1\n", ret2);
  36144. return WC_TEST_RET_ENC_EC(ret2);
  36145. }
  36146. /* Flip it back. This should pass again. */
  36147. xmss_msg[sizeof(xmss_msg) / 2] ^= 1;
  36148. ret = wc_XmssKey_Verify(&verifyKey, xmss_sig, sizeof(xmss_sig),
  36149. (byte *) xmss_msg, sizeof(xmss_msg));
  36150. if (ret != 0) {
  36151. printf("error: wc_XmssKey_Verify returned %d, expected 0\n", ret);
  36152. return WC_TEST_RET_ENC_EC(ret);
  36153. }
  36154. /* Flip bits in a few places throughout the signature, stepping in multiple
  36155. * of hash size. These should all fail with -1. */
  36156. for (j = 0; j < (int) sizeof(xmss_sig); j+= 4 * 32) {
  36157. xmss_sig[j] ^= 1;
  36158. ret2 = wc_XmssKey_Verify(&verifyKey, xmss_sig, sizeof(xmss_sig),
  36159. (byte *) xmss_msg, sizeof(xmss_msg));
  36160. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  36161. /* Verify passed when it should have failed. */
  36162. return WC_TEST_RET_ENC_I(j);
  36163. }
  36164. /* Flip this spot back. */
  36165. xmss_sig[j] ^= 1;
  36166. }
  36167. /* Cleanup everything. */
  36168. wc_XmssKey_Free(&verifyKey);
  36169. return ret;
  36170. }
  36171. #endif /* WOLFSSL_HAVE_XMSS && !WOLFSSL_SMALL_STACK &&
  36172. * WOLFSSL_XMSS_MIN_HEIGHT <= 10 */
  36173. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY)
  36174. static int lms_write_key_mem(const byte * priv, word32 privSz, void *context)
  36175. {
  36176. /* WARNING: THIS IS AN INSECURE WRITE CALLBACK THAT SHOULD ONLY
  36177. * BE USED FOR TESTING PURPOSES! Production applications should
  36178. * write only to non-volatile storage. */
  36179. XMEMCPY(context, priv, privSz);
  36180. return WC_LMS_RC_SAVED_TO_NV_MEMORY;
  36181. }
  36182. static int lms_read_key_mem(byte * priv, word32 privSz, void *context)
  36183. {
  36184. /* WARNING: THIS IS AN INSECURE READ CALLBACK THAT SHOULD ONLY
  36185. * BE USED FOR TESTING PURPOSES! */
  36186. XMEMCPY(priv, context, privSz);
  36187. return WC_LMS_RC_READ_TO_MEMORY;
  36188. }
  36189. /* LMS signature sizes are a function of their parameters. This
  36190. * test has a signature of 8688 bytes. */
  36191. #define WC_TEST_LMS_SIG_LEN (8688)
  36192. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t lms_test(void)
  36193. {
  36194. int i = 0;
  36195. int j = 0;
  36196. int ret = WC_TEST_RET_ENC_NC;
  36197. int ret2 = WC_TEST_RET_ENC_NC;
  36198. int sigsLeft = 0;
  36199. LmsKey signingKey;
  36200. LmsKey verifyKey;
  36201. WC_RNG rng;
  36202. word32 sigSz = 0;
  36203. const char * msg = "LMS HSS post quantum signature test";
  36204. word32 msgSz = (word32) XSTRLEN(msg);
  36205. unsigned char priv[HSS_MAX_PRIVATE_KEY_LEN];
  36206. unsigned char old_priv[HSS_MAX_PRIVATE_KEY_LEN];
  36207. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  36208. byte * sig = XMALLOC(WC_TEST_LMS_SIG_LEN, HEAP_HINT,
  36209. DYNAMIC_TYPE_TMP_BUFFER);
  36210. if (sig == NULL) {
  36211. return WC_TEST_RET_ENC_ERRNO;
  36212. }
  36213. #else
  36214. byte sig[WC_TEST_LMS_SIG_LEN];
  36215. #endif
  36216. WOLFSSL_ENTER("lms_test");
  36217. XMEMSET(priv, 0, sizeof(priv));
  36218. XMEMSET(old_priv, 0, sizeof(old_priv));
  36219. XMEMSET(sig, 0, WC_TEST_LMS_SIG_LEN);
  36220. #ifndef HAVE_FIPS
  36221. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  36222. #else
  36223. ret = wc_InitRng(&rng);
  36224. #endif
  36225. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36226. /* This test:
  36227. * levels: 1
  36228. * height: 5
  36229. * winternitz: 1
  36230. *
  36231. * max sigs: 2 ** (1 * 5) = 32
  36232. * signature length: 8688
  36233. */
  36234. ret = wc_LmsKey_Init(&signingKey, NULL, INVALID_DEVID);
  36235. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36236. ret = wc_LmsKey_Init(&verifyKey, NULL, INVALID_DEVID);
  36237. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36238. ret = wc_LmsKey_SetParameters(&signingKey, 1, 5, 1);
  36239. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36240. ret = wc_LmsKey_SetWriteCb(&signingKey, lms_write_key_mem);
  36241. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36242. ret = wc_LmsKey_SetReadCb(&signingKey, lms_read_key_mem);
  36243. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36244. ret = wc_LmsKey_SetContext(&signingKey, (void *) priv);
  36245. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36246. ret = wc_LmsKey_MakeKey(&signingKey, &rng);
  36247. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36248. XMEMCPY(old_priv, priv, sizeof(priv));
  36249. ret = wc_LmsKey_ExportPub(&verifyKey, &signingKey);
  36250. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36251. ret = wc_LmsKey_GetSigLen(&verifyKey, &sigSz);
  36252. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36253. if (sigSz != WC_TEST_LMS_SIG_LEN) {
  36254. printf("error: got %u, expected %d\n", sigSz, WC_TEST_LMS_SIG_LEN);
  36255. return WC_TEST_RET_ENC_EC(sigSz);
  36256. }
  36257. /* 2 ** 5 should be the max number of signatures */
  36258. for (i = 0; i < 32; ++i) {
  36259. /* We should have remaining signstures. */
  36260. sigsLeft = wc_LmsKey_SigsLeft(&signingKey);
  36261. if (sigsLeft == 0) {
  36262. return WC_TEST_RET_ENC_EC(sigsLeft);
  36263. }
  36264. /* Sign with key. The private key will be updated on every signature. */
  36265. ret = wc_LmsKey_Sign(&signingKey, sig, &sigSz, (byte *) msg, msgSz);
  36266. if (ret != 0) { return WC_TEST_RET_ENC_I(i); }
  36267. /* The updated private key should not match the old one. */
  36268. if (XMEMCMP(old_priv, priv, sizeof(priv)) == 0) {
  36269. printf("error: current priv key should not match old: %d\n", i);
  36270. return WC_TEST_RET_ENC_I(i);
  36271. }
  36272. XMEMCPY(old_priv, priv, sizeof(priv));
  36273. ret = wc_LmsKey_Verify(&verifyKey, sig, sigSz, (byte *) msg, msgSz);
  36274. if (ret != 0) { return WC_TEST_RET_ENC_I(i); }
  36275. /* Flip bits in a few places throughout the signature, stepping in multiple
  36276. * of hash size. These should all fail with -1. */
  36277. for (j = 0; j < (int) sigSz; j+= 4 * 32) {
  36278. sig[j] ^= 1;
  36279. ret2 = wc_LmsKey_Verify(&verifyKey, sig, sigSz, (byte *) msg,
  36280. msgSz);
  36281. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  36282. /* Verify passed when it should have failed. */
  36283. return WC_TEST_RET_ENC_I(j);
  36284. }
  36285. /* Flip this spot back. */
  36286. sig[j] ^= 1;
  36287. }
  36288. }
  36289. /* This should be the last signature. */
  36290. sigsLeft = wc_LmsKey_SigsLeft(&signingKey);
  36291. if (sigsLeft != 0) {
  36292. return WC_TEST_RET_ENC_EC(sigsLeft);
  36293. }
  36294. wc_LmsKey_Free(&signingKey);
  36295. wc_LmsKey_Free(&verifyKey);
  36296. wc_FreeRng(&rng);
  36297. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  36298. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36299. #endif
  36300. return ret;
  36301. }
  36302. #endif /* if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY) */
  36303. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_SMALL_STACK)
  36304. #if (defined(WOLFSSL_WC_LMS) && (LMS_MAX_HEIGHT >= 10)) || \
  36305. defined(HAVE_LIBLMS)
  36306. /* A simple LMS verify only test.
  36307. *
  36308. * Note: LMS signature sizes are a function of their parameters. This
  36309. * test has a signature of 1456 bytes:
  36310. * levels: 1
  36311. * height: 10
  36312. * winternitz: 8
  36313. * max sigs: 2 ** (1 * 10) = 1024
  36314. * signature length: 1456
  36315. * */
  36316. /* "wolfSSL LMS example message!" without null terminator. */
  36317. static byte lms_msg[28] =
  36318. {
  36319. 0x77,0x6F,0x6C,0x66,0x53,0x53,0x4C,0x20,
  36320. 0x4C,0x4D,0x53,0x20,0x65,0x78,0x61,0x6D,
  36321. 0x70,0x6C,0x65,0x20,0x6D,0x65,0x73,0x73,
  36322. 0x61,0x67,0x65,0x21
  36323. };
  36324. static const byte lms_L1H10W8_pub[HSS_MAX_PUBLIC_KEY_LEN] =
  36325. {
  36326. 0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x06,
  36327. 0x00,0x00,0x00,0x04,0xA1,0x26,0x76,0xF8,
  36328. 0xBB,0x0B,0xC0,0x82,0x21,0x71,0x0B,0x2E,
  36329. 0x8C,0xA6,0xEF,0x12,0xED,0x41,0x0E,0x8C,
  36330. 0xAF,0x11,0x93,0x34,0x7B,0x49,0x79,0xB7,
  36331. 0xDE,0x63,0x1C,0xFE,0x1F,0xD1,0x17,0x49,
  36332. 0xCD,0x5C,0xD4,0x26,0xA0,0x53,0x26,0x1A,
  36333. 0xC5,0xB4,0x8F,0x23
  36334. };
  36335. #define LMS_L1H10W8_SIGLEN (1456)
  36336. static byte lms_L1H10W8_sig[LMS_L1H10W8_SIGLEN] =
  36337. {
  36338. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
  36339. 0x00,0x00,0x00,0x04,0x18,0x70,0x09,0x2E,
  36340. 0x21,0xC9,0x6A,0xC9,0x5C,0xB6,0xB0,0xAA,
  36341. 0xC3,0xED,0x6E,0x66,0x2F,0xCC,0x45,0x81,
  36342. 0xBC,0xBA,0x44,0x96,0x1C,0xBF,0x4E,0xFB,
  36343. 0x7A,0x46,0xFB,0xBE,0x9A,0x0C,0xE4,0x50,
  36344. 0x90,0xC7,0x92,0xAC,0x53,0xAE,0x53,0x76,
  36345. 0x29,0xA6,0x65,0xF1,0x09,0xED,0x1A,0x8E,
  36346. 0x03,0x2E,0x5A,0x06,0x51,0xE3,0x1E,0xE6,
  36347. 0xF6,0xFE,0x3A,0x6E,0xD1,0x92,0x31,0x1D,
  36348. 0xA1,0x6A,0x5C,0x30,0x3A,0xC7,0xFD,0x5B,
  36349. 0xFE,0x71,0x2C,0x5C,0x2F,0x5B,0x5B,0xCF,
  36350. 0xBC,0x7F,0xBF,0x6C,0xAF,0x44,0x8A,0xAE,
  36351. 0x14,0x60,0xAB,0x88,0xED,0x0E,0x4F,0xF8,
  36352. 0xC7,0x1B,0x74,0x28,0x72,0xB3,0x96,0xA6,
  36353. 0xE6,0x46,0x22,0x82,0xCF,0x1F,0x4D,0xA6,
  36354. 0xEA,0x22,0x06,0x07,0x52,0xF5,0x26,0x16,
  36355. 0x0B,0x90,0xE3,0xFF,0x64,0xA9,0xE4,0x61,
  36356. 0x1E,0x9C,0x12,0x9C,0xF6,0xD4,0x63,0x29,
  36357. 0xEA,0x02,0xF7,0x18,0x52,0x79,0x6C,0x43,
  36358. 0xDC,0xCF,0x43,0x23,0xB9,0xCC,0x4A,0x25,
  36359. 0x9D,0x10,0xAF,0xA3,0xE6,0x47,0x5A,0x1C,
  36360. 0xFE,0x68,0x89,0xAF,0x1B,0x2D,0x88,0x3E,
  36361. 0xCA,0xDC,0x70,0xEA,0xAC,0x11,0x00,0x8A,
  36362. 0x6E,0xE0,0xC7,0xD0,0xD2,0x1A,0x36,0x18,
  36363. 0x97,0xB3,0x5F,0x0E,0x75,0x48,0x28,0xF8,
  36364. 0xA8,0xF5,0x90,0xD1,0xA1,0x84,0xFB,0xA4,
  36365. 0xAD,0x50,0xBE,0xE9,0x39,0x8C,0xC5,0xA1,
  36366. 0x67,0x51,0xA1,0x8C,0xD6,0x6B,0x97,0x1F,
  36367. 0x47,0x99,0xEE,0xE0,0x70,0x01,0xC7,0x07,
  36368. 0x50,0xF3,0x5E,0x3F,0xE7,0x06,0xD6,0x8D,
  36369. 0x26,0xD6,0x5A,0x59,0x18,0x72,0x6B,0x12,
  36370. 0xD2,0xAF,0x9B,0xB4,0x2B,0xD0,0xB2,0xF2,
  36371. 0x96,0x2F,0x40,0xEA,0xBE,0xE6,0xAC,0x1F,
  36372. 0xB8,0x33,0xC2,0x76,0xDC,0x8C,0xAC,0xC1,
  36373. 0x46,0x5E,0x04,0x84,0x1B,0xC8,0xB9,0x65,
  36374. 0x8D,0xAD,0x96,0xB5,0xB1,0xF6,0x17,0x4A,
  36375. 0x19,0x87,0xE7,0xBF,0x29,0xC7,0x9B,0xB9,
  36376. 0xD6,0x11,0x2C,0x92,0x2F,0xB7,0x24,0xD5,
  36377. 0x01,0x1D,0x80,0x37,0x54,0xED,0x33,0x32,
  36378. 0xAB,0x7A,0x12,0xD4,0x02,0x1D,0x27,0x52,
  36379. 0x89,0xDB,0x32,0xBF,0x61,0xD4,0xBB,0xB4,
  36380. 0x46,0x78,0x1B,0x64,0x17,0x84,0x4B,0x8A,
  36381. 0xBA,0xC6,0xC1,0xCF,0xC7,0x5D,0x8F,0x93,
  36382. 0xC5,0x9A,0x27,0x90,0xAC,0x17,0x98,0xFF,
  36383. 0xC8,0x22,0x59,0x55,0x90,0xB2,0x29,0x39,
  36384. 0xA0,0xBE,0x00,0x23,0x55,0x6B,0xDA,0x83,
  36385. 0xD8,0x5B,0x57,0x7C,0x67,0x1B,0xC3,0x6B,
  36386. 0x6D,0xC7,0x9B,0x2B,0x9E,0xB7,0x95,0xB3,
  36387. 0xF0,0x1B,0x89,0x5A,0xD7,0x4B,0x67,0xAF,
  36388. 0xDC,0x9E,0xCF,0x7E,0x1A,0xBA,0x1B,0xB9,
  36389. 0x3B,0x7A,0xDD,0x3F,0x0D,0xEE,0x4C,0x0B,
  36390. 0xD1,0x4F,0x34,0xF2,0x93,0xF7,0x21,0x64,
  36391. 0x2C,0x07,0x00,0x15,0x4F,0xE3,0x6A,0x9F,
  36392. 0x08,0x52,0xC2,0x65,0x47,0x1F,0x34,0x64,
  36393. 0x66,0x07,0xBC,0xEA,0xAF,0x9B,0xAA,0x39,
  36394. 0x15,0x8B,0x08,0x8C,0x24,0x41,0x9B,0x46,
  36395. 0x1B,0x5B,0x91,0x11,0xC4,0xFD,0xA9,0x88,
  36396. 0x35,0x0E,0x7D,0xAF,0xFD,0xB7,0x90,0x7E,
  36397. 0xD7,0x29,0x02,0x0A,0xDC,0xC8,0x3F,0xC0,
  36398. 0xFD,0x97,0xAF,0x50,0x49,0xA6,0x5E,0x12,
  36399. 0xC1,0xCD,0xEC,0x52,0xC5,0x51,0xF2,0x80,
  36400. 0x17,0x61,0xC7,0x7E,0xBE,0xD1,0x1B,0x65,
  36401. 0xA4,0xAB,0x92,0x8D,0x89,0xB2,0xC5,0x8F,
  36402. 0xFF,0xA5,0x6F,0xFA,0x62,0x75,0xE4,0xA1,
  36403. 0xD4,0x22,0xA8,0x9E,0x40,0x04,0x27,0x1F,
  36404. 0xCC,0x81,0xBA,0x28,0x67,0xA0,0x1C,0x80,
  36405. 0xEB,0xCA,0xB0,0x61,0xA5,0x48,0xD0,0x8A,
  36406. 0x25,0xEB,0x9E,0x67,0x8C,0x8E,0x9B,0xD1,
  36407. 0xAD,0xBB,0xC3,0xEA,0xD3,0xD4,0xC5,0x12,
  36408. 0x7B,0xDD,0x00,0x57,0x7F,0xF6,0xF7,0xF6,
  36409. 0x3C,0x05,0xCF,0xFC,0x12,0xE1,0x93,0x05,
  36410. 0xE5,0x9B,0x79,0x87,0x69,0xD8,0x82,0xD9,
  36411. 0xD7,0x1D,0x41,0x73,0xE4,0x52,0x1D,0x3E,
  36412. 0xE5,0x8C,0x8D,0x34,0xE1,0x75,0xA9,0xF1,
  36413. 0x9D,0x09,0xA2,0x5B,0xEF,0xDA,0x96,0x6E,
  36414. 0x76,0x3D,0xEA,0x50,0xD9,0xCF,0x4F,0xAC,
  36415. 0xAD,0x1D,0x35,0x72,0x1B,0x88,0x8B,0xCD,
  36416. 0x8C,0x8A,0x8A,0xE0,0x96,0x04,0xD8,0xBB,
  36417. 0x28,0x43,0x16,0x77,0x60,0x98,0x63,0xF9,
  36418. 0xB9,0x71,0x46,0xB7,0xE1,0xA7,0xA9,0x84,
  36419. 0xC3,0x65,0x82,0xE1,0x1B,0x67,0x04,0x2D,
  36420. 0x55,0x6B,0xF9,0xC0,0x79,0x09,0x09,0xE7,
  36421. 0xFD,0x06,0x4D,0x09,0x9B,0x1A,0xCE,0x35,
  36422. 0xFA,0x27,0x6F,0x2F,0x01,0x65,0x0D,0xA0,
  36423. 0x97,0x59,0x11,0xF0,0x48,0xD2,0xE7,0x46,
  36424. 0xBE,0xB4,0x0A,0xA3,0xE2,0x75,0x0E,0x09,
  36425. 0x94,0xD9,0x69,0x28,0xD4,0xDA,0x64,0xBA,
  36426. 0xFE,0xA4,0xB9,0xF0,0xBA,0xEB,0xBA,0xAC,
  36427. 0xA8,0xF9,0xD3,0x82,0x4C,0x36,0x80,0xFA,
  36428. 0xE5,0xF6,0x76,0xC3,0x80,0xFA,0x90,0x29,
  36429. 0xF4,0x85,0xA4,0xC6,0x25,0x22,0x79,0x7E,
  36430. 0x39,0x1E,0x30,0xB8,0x65,0x72,0xCF,0xE1,
  36431. 0x99,0xF0,0x75,0xE8,0x09,0xB4,0x92,0x96,
  36432. 0x1B,0x68,0x50,0x88,0xF1,0x2C,0x97,0xE3,
  36433. 0x2D,0x26,0x8F,0xC5,0x30,0xCF,0x24,0xCB,
  36434. 0xB2,0x60,0x77,0xDC,0x02,0x72,0x0D,0xD9,
  36435. 0x2E,0xF2,0x52,0xEA,0x00,0xF6,0x32,0x65,
  36436. 0xA5,0xC6,0x43,0x29,0x29,0x69,0xAB,0x27,
  36437. 0x0C,0x39,0xDF,0x76,0x3E,0x93,0x95,0xB1,
  36438. 0x2C,0xA2,0x0D,0x18,0xCE,0xA0,0x97,0x10,
  36439. 0x3C,0x90,0xC0,0xEF,0x0E,0x04,0xA6,0xC8,
  36440. 0xA0,0x21,0x3C,0x0B,0x22,0x77,0x7A,0x66,
  36441. 0xA5,0x90,0x25,0xA4,0x09,0x3E,0xD5,0x27,
  36442. 0x1F,0x6C,0x99,0x85,0x5C,0xA2,0x99,0x7A,
  36443. 0x25,0xEE,0x8D,0x32,0x3D,0xD3,0xDC,0xF5,
  36444. 0x00,0x5A,0x34,0x61,0xB6,0xCD,0x4E,0xBC,
  36445. 0x26,0x36,0xFB,0x44,0x97,0x35,0xBD,0x06,
  36446. 0x7D,0x2E,0x4A,0xA2,0xDC,0x24,0xFE,0x70,
  36447. 0x0A,0xF9,0x57,0xE3,0xEE,0xAB,0xD1,0x17,
  36448. 0xF3,0x7C,0xD6,0x37,0x26,0xFA,0x83,0x9F,
  36449. 0xDD,0xB2,0xE1,0xD7,0xF9,0xC7,0x0E,0x15,
  36450. 0x01,0xA6,0x58,0x32,0x98,0x04,0x32,0xD4,
  36451. 0xDE,0xB9,0xEF,0x09,0xFA,0xE4,0x5A,0xD7,
  36452. 0xDD,0x09,0x1C,0xC9,0xAC,0xB8,0x6A,0xF5,
  36453. 0x00,0x5D,0x6B,0x95,0x12,0x8C,0x2F,0xCC,
  36454. 0xD8,0xB9,0x50,0x3A,0xEB,0x74,0x86,0xD2,
  36455. 0x3F,0xA1,0x05,0x8F,0x6E,0xEF,0xF5,0xA4,
  36456. 0xD6,0x6E,0x53,0xFA,0x9E,0xFA,0xCE,0xDB,
  36457. 0x99,0x46,0xE7,0xC5,0xDA,0x92,0x51,0x4F,
  36458. 0x22,0x07,0xF3,0xA5,0x38,0x26,0xD3,0xEC,
  36459. 0xD6,0x01,0xDD,0x31,0x3A,0x48,0x93,0xF6,
  36460. 0x69,0x4F,0xD8,0xF6,0xC2,0x91,0xA5,0x7C,
  36461. 0xDF,0x51,0x64,0xF1,0x3B,0x79,0xBC,0x0A,
  36462. 0x2C,0xDC,0x33,0x5A,0x29,0xF6,0xB2,0x09,
  36463. 0x66,0xCA,0x24,0x9F,0x1A,0x18,0xF3,0x76,
  36464. 0x4C,0x5E,0x0B,0x81,0x7F,0x29,0x84,0xD8,
  36465. 0x7A,0xA8,0xD6,0x11,0xAC,0xEC,0xD9,0x07,
  36466. 0x91,0xEC,0xB6,0x6D,0xEC,0xDB,0xBE,0x6F,
  36467. 0x9F,0xC5,0x19,0x5E,0x56,0x87,0x20,0x80,
  36468. 0x75,0xD5,0x64,0xE9,0x80,0xBF,0x2D,0xD5,
  36469. 0x94,0x9F,0x8C,0xA4,0x54,0x41,0xAB,0xB1,
  36470. 0x8E,0xAD,0x51,0xE4,0x3C,0x24,0xF7,0x1D,
  36471. 0xFE,0x02,0x48,0x7C,0x6D,0xED,0xF1,0xAC,
  36472. 0xD9,0x79,0x42,0xE5,0x3A,0xCF,0x6A,0x4C,
  36473. 0x6D,0xE2,0x13,0xD2,0x2B,0x9D,0xAB,0x1F,
  36474. 0x70,0xD3,0xC0,0x6F,0x81,0xE9,0x9A,0x86,
  36475. 0x33,0x39,0x60,0xE7,0x6A,0x00,0x1F,0x97,
  36476. 0xEB,0xE5,0x1D,0x0D,0x66,0x15,0xC9,0xA2,
  36477. 0xB1,0xC0,0xF0,0x2E,0xF4,0x07,0xA2,0x2E,
  36478. 0x49,0x92,0x95,0x13,0xA3,0x18,0x46,0x25,
  36479. 0xB9,0x3C,0xA1,0x4B,0x00,0x00,0x00,0x06,
  36480. 0xAB,0xAA,0xF9,0x3F,0x7E,0x21,0xF4,0x0E,
  36481. 0xCE,0xFD,0xE0,0x44,0xAC,0xC7,0x1A,0x30,
  36482. 0x22,0x9D,0x0A,0xD7,0x96,0x2D,0x8F,0x9A,
  36483. 0x99,0x1F,0x40,0x75,0x7F,0x62,0xF9,0xC1,
  36484. 0x81,0x7B,0x4A,0x1B,0xFA,0xD6,0x87,0xB9,
  36485. 0xEF,0x58,0x48,0xE4,0x5C,0x79,0xE5,0xB1,
  36486. 0x2C,0x59,0xA4,0x42,0xDB,0xA6,0x53,0x70,
  36487. 0x80,0x61,0x17,0xD4,0xD3,0x77,0xBD,0x53,
  36488. 0x26,0x7C,0x0E,0x0E,0xFF,0x30,0x4B,0xD0,
  36489. 0x86,0xFC,0x02,0x20,0x24,0x46,0x5B,0xF5,
  36490. 0xE3,0x99,0x73,0x85,0x60,0x00,0x36,0x47,
  36491. 0x17,0xEE,0x0C,0xD2,0x80,0x71,0x46,0x0E,
  36492. 0x2B,0xB0,0xEF,0x7F,0xFE,0x3B,0xE5,0xE1,
  36493. 0x87,0xC2,0xAF,0x1A,0x6F,0x63,0xF4,0x5A,
  36494. 0xC4,0x16,0xF7,0xAD,0x07,0x70,0x71,0x85,
  36495. 0x7D,0x3D,0x67,0x08,0xB8,0xD8,0xE2,0xF0,
  36496. 0xA1,0xAC,0xD2,0x94,0x7D,0x93,0x03,0xDD,
  36497. 0x54,0xF9,0x64,0x19,0xB3,0xED,0x24,0x22,
  36498. 0x01,0xD7,0x12,0x5E,0xC1,0x2B,0x39,0x10,
  36499. 0x13,0xE2,0x56,0x1C,0xEE,0xF4,0x2A,0x49,
  36500. 0x7B,0xFB,0x36,0x8D,0xF8,0xAF,0x60,0xDF,
  36501. 0x10,0xF0,0x72,0xA2,0xED,0xB6,0x53,0x88,
  36502. 0xA9,0x0C,0xED,0x9C,0x18,0x33,0x7D,0x65,
  36503. 0x9B,0xB2,0x9C,0x3E,0xE9,0x1E,0x43,0x51,
  36504. 0x7E,0xBE,0x01,0x95,0xF6,0x60,0x65,0xBE,
  36505. 0xD1,0xF4,0xE2,0x83,0x6B,0xCA,0x7A,0x70,
  36506. 0x41,0x83,0x72,0xC0,0x23,0x51,0x13,0x11,
  36507. 0x2D,0xF9,0xC0,0x0D,0x7D,0x73,0x76,0xA5,
  36508. 0x30,0x83,0x68,0x10,0x35,0xA2,0x18,0x22,
  36509. 0x4E,0x21,0x93,0x27,0x6A,0x19,0x28,0x83,
  36510. 0x7F,0xDD,0xDD,0xFF,0xC3,0x8A,0x64,0x00,
  36511. 0x5F,0x1C,0x0D,0xF8,0xBB,0xD7,0x15,0xB9,
  36512. 0xEF,0xE0,0x07,0x62,0x05,0x9E,0xCF,0xFC,
  36513. 0x08,0x52,0x1E,0x65,0x41,0x56,0x6A,0xEB,
  36514. 0x81,0x53,0x30,0x7B,0xF2,0xFD,0x65,0xFF,
  36515. 0xA2,0x14,0xF5,0x62,0x1E,0x24,0x48,0x47,
  36516. 0xA5,0x41,0x80,0xB4,0xC5,0xDC,0xB2,0xB4,
  36517. 0x2D,0x17,0xE7,0xBE,0x49,0x53,0x7A,0x25,
  36518. 0xC5,0x0D,0x19,0x59,0xF4,0x88,0x59,0xED,
  36519. 0x92,0x13,0xEE,0x7A,0x4F,0x12,0x98,0x4C
  36520. };
  36521. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t lms_test_verify_only(void)
  36522. {
  36523. LmsKey verifyKey;
  36524. unsigned char pub_raw[HSS_MAX_PUBLIC_KEY_LEN];
  36525. word32 pub_len = sizeof(pub_raw);
  36526. word32 sigSz = 0;
  36527. word32 msgSz = sizeof(lms_msg);
  36528. word32 pubSz = 0;
  36529. int levels = 0;
  36530. int height = 0;
  36531. int winternitz = 0;
  36532. int ret = WC_TEST_RET_ENC_NC;
  36533. int ret2 = WC_TEST_RET_ENC_NC;
  36534. int j = 0;
  36535. int n_diff = 0;
  36536. WOLFSSL_ENTER("lms_test_verify_only");
  36537. XMEMSET(pub_raw, 0, sizeof(pub_raw));
  36538. ret = wc_LmsKey_Init(&verifyKey, NULL, INVALID_DEVID);
  36539. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36540. ret = wc_LmsKey_SetParameters(&verifyKey, 1, 10, 8);
  36541. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36542. ret = wc_LmsKey_ImportPubRaw(&verifyKey, lms_L1H10W8_pub,
  36543. HSS_MAX_PUBLIC_KEY_LEN);
  36544. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36545. /* Verify parameters, pub length, and sig length are correct. */
  36546. ret = wc_LmsKey_GetParameters(&verifyKey, &levels, &height, &winternitz);
  36547. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36548. if (levels != 1 || height != 10 || winternitz != 8) {
  36549. printf("error: invalid LMS parameters: L%d-H%d-W%d\n", levels, height,
  36550. winternitz);
  36551. return WC_TEST_RET_ENC_NC;
  36552. }
  36553. ret = wc_LmsKey_GetPubLen(&verifyKey, &pubSz);
  36554. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36555. if (pubSz != HSS_MAX_PUBLIC_KEY_LEN) {
  36556. printf("error: got %u, expected %d\n", pubSz, HSS_MAX_PUBLIC_KEY_LEN);
  36557. return WC_TEST_RET_ENC_EC(pubSz);
  36558. }
  36559. ret = wc_LmsKey_GetSigLen(&verifyKey, &sigSz);
  36560. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  36561. if (sigSz != LMS_L1H10W8_SIGLEN) {
  36562. printf("error: got %u, expected %d\n", sigSz, LMS_L1H10W8_SIGLEN);
  36563. return WC_TEST_RET_ENC_EC(sigSz);
  36564. }
  36565. ret = wc_LmsKey_Verify(&verifyKey, lms_L1H10W8_sig, LMS_L1H10W8_SIGLEN,
  36566. (byte *) lms_msg, msgSz);
  36567. if (ret != 0) {
  36568. printf("error: wc_LmsKey_Verify returned %d\n", ret);
  36569. return WC_TEST_RET_ENC_EC(ret);
  36570. }
  36571. /* Now test the ExportPubRaw API, verify we recover the original pub. */
  36572. ret = wc_LmsKey_ExportPubRaw(&verifyKey, pub_raw, &pub_len);
  36573. if (ret != 0) {
  36574. printf("error: wc_LmsKey_ExportPubRaw returned %d, expected 0\n", ret);
  36575. return WC_TEST_RET_ENC_EC(ret);
  36576. }
  36577. if (pub_len != HSS_MAX_PUBLIC_KEY_LEN) {
  36578. printf("error: LMS pub len %u, expected %d\n", pub_len,
  36579. HSS_MAX_PUBLIC_KEY_LEN);
  36580. return WC_TEST_RET_ENC_EC(pub_len);
  36581. }
  36582. n_diff = XMEMCMP(pub_raw, lms_L1H10W8_pub, sizeof(lms_L1H10W8_pub));
  36583. if (n_diff != 0) {
  36584. printf("error: exported and imported pub raw do not match: %d\n",
  36585. n_diff);
  36586. return WC_TEST_RET_ENC_EC(n_diff);
  36587. }
  36588. /* Flip bits in message. This should fail. */
  36589. lms_msg[msgSz / 2] ^= 1;
  36590. ret2 = wc_LmsKey_Verify(&verifyKey, lms_L1H10W8_sig, LMS_L1H10W8_SIGLEN,
  36591. (byte *) lms_msg, msgSz);
  36592. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  36593. printf("error: wc_LmsKey_Verify returned %d, expected -1\n", ret2);
  36594. return WC_TEST_RET_ENC_EC(ret);
  36595. }
  36596. /* Flip it back. This should pass again. */
  36597. lms_msg[msgSz / 2] ^= 1;
  36598. ret = wc_LmsKey_Verify(&verifyKey, lms_L1H10W8_sig, LMS_L1H10W8_SIGLEN,
  36599. (byte *) lms_msg, msgSz);
  36600. if (ret != 0) {
  36601. printf("error: wc_LmsKey_Verify returned %d, expected 0\n", ret);
  36602. return WC_TEST_RET_ENC_EC(ret);
  36603. }
  36604. /* Flip bits in a few places throughout the signature, stepping in multiple
  36605. * of hash size. These should all fail with -1. */
  36606. for (j = 0; j < (int) sigSz; j+= 4 * 32) {
  36607. lms_L1H10W8_sig[j] ^= 1;
  36608. ret2 = wc_LmsKey_Verify(&verifyKey, lms_L1H10W8_sig,
  36609. LMS_L1H10W8_SIGLEN,
  36610. (byte *) lms_msg, msgSz);
  36611. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  36612. /* Verify passed when it should have failed. */
  36613. return WC_TEST_RET_ENC_I(j);
  36614. }
  36615. /* Flip this spot back. */
  36616. lms_L1H10W8_sig[j] ^= 1;
  36617. }
  36618. wc_LmsKey_Free(&verifyKey);
  36619. return ret;
  36620. }
  36621. #endif
  36622. #endif /* if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_SMALL_STACK) */
  36623. static const int fiducial3 = WC_TEST_RET_LN; /* source code reference point --
  36624. * see print_fiducials() below.
  36625. */
  36626. #ifdef WOLFCRYPT_HAVE_ECCSI
  36627. static wc_test_ret_t eccsi_api_test(WC_RNG* rng, EccsiKey* key, mp_int* ssk,
  36628. ecc_point* pvt)
  36629. {
  36630. wc_test_ret_t ret;
  36631. byte id[1] = { 0x00 };
  36632. int valid;
  36633. word32 sz;
  36634. byte data[256];
  36635. byte hash[WC_MAX_DIGEST_SIZE];
  36636. byte hashSz;
  36637. byte sig[257];
  36638. word32 sigSz;
  36639. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  36640. if (ret != BAD_FUNC_ARG)
  36641. return WC_TEST_RET_ENC_EC(ret);
  36642. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  36643. if (ret != BAD_FUNC_ARG)
  36644. return WC_TEST_RET_ENC_EC(ret);
  36645. ret = wc_InitEccsiKey(NULL, NULL, INVALID_DEVID);
  36646. if (ret != BAD_FUNC_ARG)
  36647. return WC_TEST_RET_ENC_EC(ret);
  36648. ret = wc_InitEccsiKey(NULL, HEAP_HINT, INVALID_DEVID);
  36649. if (ret != BAD_FUNC_ARG)
  36650. return WC_TEST_RET_ENC_EC(ret);
  36651. wc_FreeEccsiKey(NULL);
  36652. /* Create a valid key. */
  36653. ret = wc_InitEccsiKey(key, NULL, INVALID_DEVID);
  36654. if (ret != 0)
  36655. return WC_TEST_RET_ENC_EC(ret);
  36656. ret = wc_MakeEccsiKey(NULL, NULL);
  36657. if (ret != BAD_FUNC_ARG)
  36658. return WC_TEST_RET_ENC_EC(ret);
  36659. ret = wc_MakeEccsiKey(key, NULL);
  36660. if (ret != BAD_FUNC_ARG)
  36661. return WC_TEST_RET_ENC_EC(ret);
  36662. ret = wc_MakeEccsiKey(NULL, rng);
  36663. if (ret != BAD_FUNC_ARG)
  36664. return WC_TEST_RET_ENC_EC(ret);
  36665. ret = wc_MakeEccsiPair(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL,
  36666. NULL);
  36667. if (ret != BAD_FUNC_ARG)
  36668. return WC_TEST_RET_ENC_EC(ret);
  36669. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL);
  36670. if (ret != BAD_FUNC_ARG)
  36671. return WC_TEST_RET_ENC_EC(ret);
  36672. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt);
  36673. if (ret != BAD_FUNC_ARG)
  36674. return WC_TEST_RET_ENC_EC(ret);
  36675. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt);
  36676. if (ret != BAD_FUNC_ARG)
  36677. return WC_TEST_RET_ENC_EC(ret);
  36678. ret = wc_MakeEccsiPair(key, NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  36679. if (ret != BAD_FUNC_ARG)
  36680. return WC_TEST_RET_ENC_EC(ret);
  36681. ret = wc_MakeEccsiPair(NULL, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  36682. if (ret != BAD_FUNC_ARG)
  36683. return WC_TEST_RET_ENC_EC(ret);
  36684. /* No key set */
  36685. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  36686. if (ret != BAD_STATE_E)
  36687. return WC_TEST_RET_ENC_EC(ret);
  36688. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL,
  36689. NULL);
  36690. if (ret != BAD_FUNC_ARG)
  36691. return WC_TEST_RET_ENC_EC(ret);
  36692. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  36693. NULL);
  36694. if (ret != BAD_FUNC_ARG)
  36695. return WC_TEST_RET_ENC_EC(ret);
  36696. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL,
  36697. &valid);
  36698. if (ret != BAD_FUNC_ARG)
  36699. return WC_TEST_RET_ENC_EC(ret);
  36700. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt,
  36701. &valid);
  36702. if (ret != BAD_FUNC_ARG)
  36703. return WC_TEST_RET_ENC_EC(ret);
  36704. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt,
  36705. &valid);
  36706. if (ret != BAD_FUNC_ARG)
  36707. return WC_TEST_RET_ENC_EC(ret);
  36708. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  36709. &valid);
  36710. if (ret != BAD_FUNC_ARG)
  36711. return WC_TEST_RET_ENC_EC(ret);
  36712. /* No key set */
  36713. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  36714. &valid);
  36715. if (ret != BAD_STATE_E)
  36716. return WC_TEST_RET_ENC_EC(ret);
  36717. ret = wc_ValidateEccsiPvt(NULL, NULL, NULL);
  36718. if (ret != BAD_FUNC_ARG)
  36719. return WC_TEST_RET_ENC_EC(ret);
  36720. ret = wc_ValidateEccsiPvt(key, NULL, NULL);
  36721. if (ret != BAD_FUNC_ARG)
  36722. return WC_TEST_RET_ENC_EC(ret);
  36723. ret = wc_ValidateEccsiPvt(NULL, pvt, NULL);
  36724. if (ret != BAD_FUNC_ARG)
  36725. return WC_TEST_RET_ENC_EC(ret);
  36726. ret = wc_ValidateEccsiPvt(NULL, NULL, &valid);
  36727. if (ret != BAD_FUNC_ARG)
  36728. return WC_TEST_RET_ENC_EC(ret);
  36729. ret = wc_ValidateEccsiPvt(key, pvt, NULL);
  36730. if (ret != BAD_FUNC_ARG)
  36731. return WC_TEST_RET_ENC_EC(ret);
  36732. ret = wc_ValidateEccsiPvt(key, NULL, &valid);
  36733. if (ret != BAD_FUNC_ARG)
  36734. return WC_TEST_RET_ENC_EC(ret);
  36735. ret = wc_ValidateEccsiPvt(NULL, pvt, &valid);
  36736. if (ret != BAD_FUNC_ARG)
  36737. return WC_TEST_RET_ENC_EC(ret);
  36738. ret = wc_EncodeEccsiPair(NULL, NULL, NULL, data, NULL);
  36739. if (ret != BAD_FUNC_ARG)
  36740. return WC_TEST_RET_ENC_EC(ret);
  36741. ret = wc_EncodeEccsiPair(key, ssk, pvt, data, NULL);
  36742. if (ret != BAD_FUNC_ARG)
  36743. return WC_TEST_RET_ENC_EC(ret);
  36744. ret = wc_EncodeEccsiPair(key, ssk, NULL, data, &sz);
  36745. if (ret != BAD_FUNC_ARG)
  36746. return WC_TEST_RET_ENC_EC(ret);
  36747. ret = wc_EncodeEccsiPair(key, NULL, pvt, data, &sz);
  36748. if (ret != BAD_FUNC_ARG)
  36749. return WC_TEST_RET_ENC_EC(ret);
  36750. ret = wc_EncodeEccsiPair(NULL, ssk, pvt, data, &sz);
  36751. if (ret != BAD_FUNC_ARG)
  36752. return WC_TEST_RET_ENC_EC(ret);
  36753. /* No key created so no curve information. */
  36754. ret = wc_EncodeEccsiPair(key, ssk, pvt, NULL, &sz);
  36755. if (ret != LENGTH_ONLY_E)
  36756. return WC_TEST_RET_ENC_EC(ret);
  36757. ret = wc_EncodeEccsiSsk(NULL, NULL, data, NULL);
  36758. if (ret != BAD_FUNC_ARG)
  36759. return WC_TEST_RET_ENC_EC(ret);
  36760. ret = wc_EncodeEccsiSsk(key, ssk, data, NULL);
  36761. if (ret != BAD_FUNC_ARG)
  36762. return WC_TEST_RET_ENC_EC(ret);
  36763. ret = wc_EncodeEccsiSsk(key, NULL, data, &sz);
  36764. if (ret != BAD_FUNC_ARG)
  36765. return WC_TEST_RET_ENC_EC(ret);
  36766. ret = wc_EncodeEccsiSsk(NULL, ssk, data, &sz);
  36767. if (ret != BAD_FUNC_ARG)
  36768. return WC_TEST_RET_ENC_EC(ret);
  36769. ret = wc_EncodeEccsiPvt(NULL, NULL, data, NULL, 1);
  36770. if (ret != BAD_FUNC_ARG)
  36771. return WC_TEST_RET_ENC_EC(ret);
  36772. ret = wc_EncodeEccsiPvt(key, pvt, data, NULL, 1);
  36773. if (ret != BAD_FUNC_ARG)
  36774. return WC_TEST_RET_ENC_EC(ret);
  36775. ret = wc_EncodeEccsiPvt(key, NULL, data, &sz, 1);
  36776. if (ret != BAD_FUNC_ARG)
  36777. return WC_TEST_RET_ENC_EC(ret);
  36778. ret = wc_EncodeEccsiPvt(NULL, pvt, data, &sz, 1);
  36779. if (ret != BAD_FUNC_ARG)
  36780. return WC_TEST_RET_ENC_EC(ret);
  36781. ret = wc_DecodeEccsiPair(NULL, NULL, 0, NULL, NULL);
  36782. if (ret != BAD_FUNC_ARG)
  36783. return WC_TEST_RET_ENC_EC(ret);
  36784. ret = wc_DecodeEccsiPair(key, data, 0, ssk, NULL);
  36785. if (ret != BAD_FUNC_ARG)
  36786. return WC_TEST_RET_ENC_EC(ret);
  36787. ret = wc_DecodeEccsiPair(key, data, 0, NULL, pvt);
  36788. if (ret != BAD_FUNC_ARG)
  36789. return WC_TEST_RET_ENC_EC(ret);
  36790. ret = wc_DecodeEccsiPair(key, NULL, 0, ssk, pvt);
  36791. if (ret != BAD_FUNC_ARG)
  36792. return WC_TEST_RET_ENC_EC(ret);
  36793. ret = wc_DecodeEccsiPair(NULL, data, 0, ssk, pvt);
  36794. if (ret != BAD_FUNC_ARG)
  36795. return WC_TEST_RET_ENC_EC(ret);
  36796. ret = wc_DecodeEccsiSsk(NULL, NULL, 0, NULL);
  36797. if (ret != BAD_FUNC_ARG)
  36798. return WC_TEST_RET_ENC_EC(ret);
  36799. ret = wc_DecodeEccsiSsk(key, data, 0, NULL);
  36800. if (ret != BAD_FUNC_ARG)
  36801. return WC_TEST_RET_ENC_EC(ret);
  36802. ret = wc_DecodeEccsiSsk(key, NULL, 0, ssk);
  36803. if (ret != BAD_FUNC_ARG)
  36804. return WC_TEST_RET_ENC_EC(ret);
  36805. ret = wc_DecodeEccsiSsk(NULL, data, 0, ssk);
  36806. if (ret != BAD_FUNC_ARG)
  36807. return WC_TEST_RET_ENC_EC(ret);
  36808. ret = wc_DecodeEccsiPvt(NULL, NULL, 0, NULL);
  36809. if (ret != BAD_FUNC_ARG)
  36810. return WC_TEST_RET_ENC_EC(ret);
  36811. ret = wc_DecodeEccsiPvt(key, data, 0, NULL);
  36812. if (ret != BAD_FUNC_ARG)
  36813. return WC_TEST_RET_ENC_EC(ret);
  36814. ret = wc_DecodeEccsiPvt(key, NULL, 0, pvt);
  36815. if (ret != BAD_FUNC_ARG)
  36816. return WC_TEST_RET_ENC_EC(ret);
  36817. ret = wc_DecodeEccsiPvt(NULL, data, 0, pvt);
  36818. if (ret != BAD_FUNC_ARG)
  36819. return WC_TEST_RET_ENC_EC(ret);
  36820. ret = wc_DecodeEccsiPvtFromSig(NULL, NULL, 0, NULL);
  36821. if (ret != BAD_FUNC_ARG)
  36822. return WC_TEST_RET_ENC_EC(ret);
  36823. ret = wc_DecodeEccsiPvtFromSig(key, data, 0, NULL);
  36824. if (ret != BAD_FUNC_ARG)
  36825. return WC_TEST_RET_ENC_EC(ret);
  36826. ret = wc_DecodeEccsiPvtFromSig(key, NULL, 0, pvt);
  36827. if (ret != BAD_FUNC_ARG)
  36828. return WC_TEST_RET_ENC_EC(ret);
  36829. ret = wc_DecodeEccsiPvtFromSig(NULL, data, 0, pvt);
  36830. if (ret != BAD_FUNC_ARG)
  36831. return WC_TEST_RET_ENC_EC(ret);
  36832. ret = wc_ExportEccsiKey(NULL, data, NULL);
  36833. if (ret != BAD_FUNC_ARG)
  36834. return WC_TEST_RET_ENC_EC(ret);
  36835. ret = wc_ExportEccsiKey(key, data, NULL);
  36836. if (ret != BAD_FUNC_ARG)
  36837. return WC_TEST_RET_ENC_EC(ret);
  36838. ret = wc_ExportEccsiKey(NULL, data, &sz);
  36839. if (ret != BAD_FUNC_ARG)
  36840. return WC_TEST_RET_ENC_EC(ret);
  36841. /* No key to export */
  36842. ret = wc_ExportEccsiKey(key, NULL, &sz);
  36843. if (ret != BAD_STATE_E)
  36844. return WC_TEST_RET_ENC_EC(ret);
  36845. ret = wc_ImportEccsiKey(NULL, NULL, 0);
  36846. if (ret != BAD_FUNC_ARG)
  36847. return WC_TEST_RET_ENC_EC(ret);
  36848. ret = wc_ImportEccsiKey(key, NULL, 0);
  36849. if (ret != BAD_FUNC_ARG)
  36850. return WC_TEST_RET_ENC_EC(ret);
  36851. ret = wc_ImportEccsiKey(NULL, data, 0);
  36852. if (ret != BAD_FUNC_ARG)
  36853. return WC_TEST_RET_ENC_EC(ret);
  36854. ret = wc_ExportEccsiPrivateKey(NULL, data, NULL);
  36855. if (ret != BAD_FUNC_ARG)
  36856. return WC_TEST_RET_ENC_EC(ret);
  36857. ret = wc_ExportEccsiPrivateKey(key, data, NULL);
  36858. if (ret != BAD_FUNC_ARG)
  36859. return WC_TEST_RET_ENC_EC(ret);
  36860. ret = wc_ExportEccsiPrivateKey(NULL, data, &sz);
  36861. if (ret != BAD_FUNC_ARG)
  36862. return WC_TEST_RET_ENC_EC(ret);
  36863. /* No key to export */
  36864. ret = wc_ExportEccsiPrivateKey(key, NULL, &sz);
  36865. if (ret != BAD_STATE_E)
  36866. return WC_TEST_RET_ENC_EC(ret);
  36867. ret = wc_ImportEccsiPrivateKey(NULL, NULL, 0);
  36868. if (ret != BAD_FUNC_ARG)
  36869. return WC_TEST_RET_ENC_EC(ret);
  36870. ret = wc_ImportEccsiPrivateKey(key, NULL, 0);
  36871. if (ret != BAD_FUNC_ARG)
  36872. return WC_TEST_RET_ENC_EC(ret);
  36873. ret = wc_ImportEccsiPrivateKey(NULL, data, 0);
  36874. if (ret != BAD_FUNC_ARG)
  36875. return WC_TEST_RET_ENC_EC(ret);
  36876. ret = wc_ExportEccsiPublicKey(NULL, data, NULL, 1);
  36877. if (ret != BAD_FUNC_ARG)
  36878. return WC_TEST_RET_ENC_EC(ret);
  36879. ret = wc_ExportEccsiPublicKey(key, data, NULL, 1);
  36880. if (ret != BAD_FUNC_ARG)
  36881. return WC_TEST_RET_ENC_EC(ret);
  36882. ret = wc_ExportEccsiPublicKey(NULL, data, &sz, 1);
  36883. if (ret != BAD_FUNC_ARG)
  36884. return WC_TEST_RET_ENC_EC(ret);
  36885. /* No key to export */
  36886. ret = wc_ExportEccsiPublicKey(key, data, &sz, 1);
  36887. if (ret != BAD_STATE_E)
  36888. return WC_TEST_RET_ENC_EC(ret);
  36889. ret = wc_ImportEccsiPublicKey(NULL, NULL, 0, 1);
  36890. if (ret != BAD_FUNC_ARG)
  36891. return WC_TEST_RET_ENC_EC(ret);
  36892. ret = wc_ImportEccsiPublicKey(key, NULL, 0, 1);
  36893. if (ret != BAD_FUNC_ARG)
  36894. return WC_TEST_RET_ENC_EC(ret);
  36895. ret = wc_ImportEccsiPublicKey(NULL, data, 0, 1);
  36896. if (ret != BAD_FUNC_ARG)
  36897. return WC_TEST_RET_ENC_EC(ret);
  36898. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL, NULL);
  36899. if (ret != BAD_FUNC_ARG)
  36900. return WC_TEST_RET_ENC_EC(ret);
  36901. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, NULL);
  36902. if (ret != BAD_FUNC_ARG)
  36903. return WC_TEST_RET_ENC_EC(ret);
  36904. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, NULL, &hashSz);
  36905. if (ret != BAD_FUNC_ARG)
  36906. return WC_TEST_RET_ENC_EC(ret);
  36907. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, NULL, hash, &hashSz);
  36908. if (ret != BAD_FUNC_ARG)
  36909. return WC_TEST_RET_ENC_EC(ret);
  36910. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, NULL, 1, pvt, hash,
  36911. &hashSz);
  36912. if (ret != BAD_FUNC_ARG)
  36913. return WC_TEST_RET_ENC_EC(ret);
  36914. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  36915. if (ret != BAD_FUNC_ARG)
  36916. return WC_TEST_RET_ENC_EC(ret);
  36917. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  36918. if (ret != BAD_STATE_E)
  36919. return WC_TEST_RET_ENC_EC(ret);
  36920. ret = wc_SetEccsiHash(NULL, NULL, 1);
  36921. if (ret != BAD_FUNC_ARG)
  36922. return WC_TEST_RET_ENC_EC(ret);
  36923. ret = wc_SetEccsiHash(key, NULL, 1);
  36924. if (ret != BAD_FUNC_ARG)
  36925. return WC_TEST_RET_ENC_EC(ret);
  36926. ret = wc_SetEccsiHash(NULL, hash, 1);
  36927. if (ret != BAD_FUNC_ARG)
  36928. return WC_TEST_RET_ENC_EC(ret);
  36929. ret = wc_SetEccsiPair(NULL, NULL, NULL);
  36930. if (ret != BAD_FUNC_ARG)
  36931. return WC_TEST_RET_ENC_EC(ret);
  36932. ret = wc_SetEccsiPair(key, NULL, NULL);
  36933. if (ret != BAD_FUNC_ARG)
  36934. return WC_TEST_RET_ENC_EC(ret);
  36935. ret = wc_SetEccsiPair(NULL, ssk, NULL);
  36936. if (ret != BAD_FUNC_ARG)
  36937. return WC_TEST_RET_ENC_EC(ret);
  36938. ret = wc_SetEccsiPair(NULL, NULL, pvt);
  36939. if (ret != BAD_FUNC_ARG)
  36940. return WC_TEST_RET_ENC_EC(ret);
  36941. ret = wc_SetEccsiPair(key, ssk, NULL);
  36942. if (ret != BAD_FUNC_ARG)
  36943. return WC_TEST_RET_ENC_EC(ret);
  36944. ret = wc_SetEccsiPair(key, NULL, pvt);
  36945. if (ret != BAD_FUNC_ARG)
  36946. return WC_TEST_RET_ENC_EC(ret);
  36947. ret = wc_SetEccsiPair(NULL, ssk, pvt);
  36948. if (ret != BAD_FUNC_ARG)
  36949. return WC_TEST_RET_ENC_EC(ret);
  36950. ret = wc_SignEccsiHash(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, NULL);
  36951. if (ret != BAD_FUNC_ARG)
  36952. return WC_TEST_RET_ENC_EC(ret);
  36953. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, sig, NULL);
  36954. if (ret != BAD_FUNC_ARG)
  36955. return WC_TEST_RET_ENC_EC(ret);
  36956. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, NULL, 0, sig,
  36957. &sigSz);
  36958. if (ret != BAD_FUNC_ARG)
  36959. return WC_TEST_RET_ENC_EC(ret);
  36960. ret = wc_SignEccsiHash(key, NULL, WC_HASH_TYPE_SHA256, data, 0, sig,
  36961. &sigSz);
  36962. if (ret != BAD_FUNC_ARG)
  36963. return WC_TEST_RET_ENC_EC(ret);
  36964. ret = wc_SignEccsiHash(NULL, rng, WC_HASH_TYPE_SHA256, data, 0, sig,
  36965. &sigSz);
  36966. if (ret != BAD_FUNC_ARG)
  36967. return WC_TEST_RET_ENC_EC(ret);
  36968. /* Key not set. */
  36969. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  36970. &sigSz);
  36971. if (ret != BAD_STATE_E)
  36972. return WC_TEST_RET_ENC_EC(ret);
  36973. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  36974. if (ret != BAD_FUNC_ARG)
  36975. return WC_TEST_RET_ENC_EC(ret);
  36976. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  36977. if (ret != BAD_FUNC_ARG)
  36978. return WC_TEST_RET_ENC_EC(ret);
  36979. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, NULL, 0, NULL);
  36980. if (ret != BAD_FUNC_ARG)
  36981. return WC_TEST_RET_ENC_EC(ret);
  36982. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0, NULL);
  36983. if (ret != BAD_FUNC_ARG)
  36984. return WC_TEST_RET_ENC_EC(ret);
  36985. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0,
  36986. &valid);
  36987. if (ret != BAD_FUNC_ARG)
  36988. return WC_TEST_RET_ENC_EC(ret);
  36989. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0, NULL);
  36990. if (ret != BAD_FUNC_ARG)
  36991. return WC_TEST_RET_ENC_EC(ret);
  36992. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, NULL, 0,
  36993. &valid);
  36994. if (ret != BAD_FUNC_ARG)
  36995. return WC_TEST_RET_ENC_EC(ret);
  36996. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0,
  36997. &valid);
  36998. if (ret != BAD_FUNC_ARG)
  36999. return WC_TEST_RET_ENC_EC(ret);
  37000. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  37001. &valid);
  37002. if (ret != BAD_FUNC_ARG)
  37003. return WC_TEST_RET_ENC_EC(ret);
  37004. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  37005. &valid);
  37006. if (ret != BAD_STATE_E)
  37007. return WC_TEST_RET_ENC_EC(ret);
  37008. ret = wc_SetEccsiPair(key, ssk, pvt);
  37009. if (ret != 0)
  37010. return WC_TEST_RET_ENC_EC(ret);
  37011. /* Identity hash not set. */
  37012. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  37013. &sigSz);
  37014. if (ret != BAD_STATE_E)
  37015. return WC_TEST_RET_ENC_EC(ret);
  37016. wc_FreeEccsiKey(key);
  37017. return 0;
  37018. }
  37019. /* RFC 6507: Appendix A */
  37020. static wc_test_ret_t eccsi_kat_verify_test(EccsiKey* key, ecc_point* pvt)
  37021. {
  37022. wc_test_ret_t ret;
  37023. int verified;
  37024. const byte msg[] = { 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x00 };
  37025. word32 msgSz = sizeof(msg);
  37026. byte hash[WC_SHA256_DIGEST_SIZE];
  37027. byte hashSz = WC_SHA256_DIGEST_SIZE;
  37028. static const byte id[] = {
  37029. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  37030. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  37031. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  37032. 0x33, 0x00
  37033. };
  37034. word32 idSz = sizeof(id);
  37035. static const byte sig[] = {
  37036. 0x26, 0x9D, 0x4C, 0x8F, 0xDE, 0xB6, 0x6A, 0x74,
  37037. 0xE4, 0xEF, 0x8C, 0x0D, 0x5D, 0xCC, 0x59, 0x7D,
  37038. 0xDF, 0xE6, 0x02, 0x9C, 0x2A, 0xFF, 0xC4, 0x93,
  37039. 0x60, 0x08, 0xCD, 0x2C, 0xC1, 0x04, 0x5D, 0x81,
  37040. 0xE0, 0x9B, 0x52, 0x8D, 0x0E, 0xF8, 0xD6, 0xDF,
  37041. 0x1A, 0xA3, 0xEC, 0xBF, 0x80, 0x11, 0x0C, 0xFC,
  37042. 0xEC, 0x9F, 0xC6, 0x82, 0x52, 0xCE, 0xBB, 0x67,
  37043. 0x9F, 0x41, 0x34, 0x84, 0x69, 0x40, 0xCC, 0xFD,
  37044. 0x04,
  37045. 0x75, 0x8A, 0x14, 0x27, 0x79, 0xBE, 0x89, 0xE8,
  37046. 0x29, 0xE7, 0x19, 0x84, 0xCB, 0x40, 0xEF, 0x75,
  37047. 0x8C, 0xC4, 0xAD, 0x77, 0x5F, 0xC5, 0xB9, 0xA3,
  37048. 0xE1, 0xC8, 0xED, 0x52, 0xF6, 0xFA, 0x36, 0xD9,
  37049. 0xA7, 0x9D, 0x24, 0x76, 0x92, 0xF4, 0xED, 0xA3,
  37050. 0xA6, 0xBD, 0xAB, 0x77, 0xD6, 0xAA, 0x64, 0x74,
  37051. 0xA4, 0x64, 0xAE, 0x49, 0x34, 0x66, 0x3C, 0x52,
  37052. 0x65, 0xBA, 0x70, 0x18, 0xBA, 0x09, 0x1F, 0x79
  37053. };
  37054. word32 sigSz = sizeof(sig);
  37055. static const byte pubData[] = {
  37056. 0x50, 0xD4, 0x67, 0x0B, 0xDE, 0x75, 0x24, 0x4F,
  37057. 0x28, 0xD2, 0x83, 0x8A, 0x0D, 0x25, 0x55, 0x8A,
  37058. 0x7A, 0x72, 0x68, 0x6D, 0x45, 0x22, 0xD4, 0xC8,
  37059. 0x27, 0x3F, 0xB6, 0x44, 0x2A, 0xEB, 0xFA, 0x93,
  37060. 0xDB, 0xDD, 0x37, 0x55, 0x1A, 0xFD, 0x26, 0x3B,
  37061. 0x5D, 0xFD, 0x61, 0x7F, 0x39, 0x60, 0xC6, 0x5A,
  37062. 0x8C, 0x29, 0x88, 0x50, 0xFF, 0x99, 0xF2, 0x03,
  37063. 0x66, 0xDC, 0xE7, 0xD4, 0x36, 0x72, 0x17, 0xF4
  37064. };
  37065. static const byte expHash[] = {
  37066. 0x49, 0x0f, 0x3f, 0xeb, 0xbc, 0x1c, 0x90, 0x2f,
  37067. 0x62, 0x89, 0x72, 0x3d, 0x7f, 0x8c, 0xbf, 0x79,
  37068. 0xdb, 0x88, 0x93, 0x08, 0x49, 0xd1, 0x9f, 0x38,
  37069. 0xf0, 0x29, 0x5b, 0x5c, 0x27, 0x6c, 0x14, 0xd1
  37070. };
  37071. ret = wc_ImportEccsiPublicKey(key, pubData, sizeof(pubData), 0);
  37072. if (ret != 0)
  37073. return WC_TEST_RET_ENC_EC(ret);
  37074. ret = wc_DecodeEccsiPvtFromSig(key, sig, sigSz, pvt);
  37075. if (ret != 0)
  37076. return WC_TEST_RET_ENC_EC(ret);
  37077. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, idSz, pvt, hash,
  37078. &hashSz);
  37079. if (ret != 0)
  37080. return WC_TEST_RET_ENC_EC(ret);
  37081. if (hashSz != sizeof(expHash))
  37082. return WC_TEST_RET_ENC_NC;
  37083. if (XMEMCMP(hash, expHash, hashSz) != 0)
  37084. return WC_TEST_RET_ENC_NC;
  37085. ret = wc_SetEccsiHash(key, hash, hashSz);
  37086. if (ret != 0)
  37087. return WC_TEST_RET_ENC_EC(ret);
  37088. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  37089. &verified);
  37090. if (ret != 0)
  37091. return WC_TEST_RET_ENC_EC(ret);
  37092. if (!verified)
  37093. return WC_TEST_RET_ENC_NC;
  37094. return 0;
  37095. }
  37096. static wc_test_ret_t eccsi_enc_dec_pair_test(EccsiKey* priv, mp_int* ssk, ecc_point* pvt)
  37097. {
  37098. wc_test_ret_t ret;
  37099. byte data[32 * 3];
  37100. word32 sz;
  37101. ecc_point* decPvt = NULL;
  37102. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37103. mp_int *decSsk = (mp_int *)XMALLOC(sizeof(*decSsk), HEAP_HINT,
  37104. DYNAMIC_TYPE_TMP_BUFFER);
  37105. if (decSsk == NULL)
  37106. return WC_TEST_RET_ENC_ERRNO;
  37107. #else
  37108. mp_int decSsk[1];
  37109. #endif
  37110. ret = mp_init(decSsk);
  37111. if (ret != 0)
  37112. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37113. decPvt = wc_ecc_new_point();
  37114. if (decPvt == NULL)
  37115. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  37116. ret = wc_EncodeEccsiPair(priv, ssk, pvt, NULL, &sz);
  37117. if (ret != LENGTH_ONLY_E)
  37118. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37119. if (sz != 32 * 3)
  37120. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37121. ret = wc_EncodeEccsiPair(priv, ssk, pvt, data, &sz);
  37122. if (ret != 0)
  37123. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37124. if (sz != 32* 3)
  37125. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37126. ret = wc_DecodeEccsiPair(priv, data, sz, decSsk, decPvt);
  37127. if (ret != 0)
  37128. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37129. ret = mp_cmp(ssk, decSsk);
  37130. if (ret != MP_EQ)
  37131. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37132. ret = wc_ecc_cmp_point(pvt, decPvt);
  37133. if (ret != MP_EQ)
  37134. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37135. ret = wc_EncodeEccsiSsk(priv, ssk, NULL, &sz);
  37136. if (ret != LENGTH_ONLY_E)
  37137. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37138. if (sz != 32)
  37139. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37140. ret = wc_EncodeEccsiSsk(priv, ssk, data, &sz);
  37141. if (ret != 0)
  37142. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37143. if (sz != 32)
  37144. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37145. ret = wc_DecodeEccsiSsk(priv, data, sz, decSsk);
  37146. if (ret != 0)
  37147. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37148. ret = mp_cmp(ssk, decSsk);
  37149. if (ret != MP_EQ)
  37150. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37151. ret = wc_EncodeEccsiPvt(priv, pvt, NULL, &sz, 1);
  37152. if (ret != LENGTH_ONLY_E)
  37153. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37154. if (sz != 32 * 2)
  37155. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37156. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 1);
  37157. if (ret != 0)
  37158. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37159. if (sz != 32 * 2)
  37160. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37161. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  37162. if (ret != 0)
  37163. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37164. ret = wc_ecc_cmp_point(pvt, decPvt);
  37165. if (ret != MP_EQ)
  37166. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37167. sz = sizeof(data);
  37168. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 0);
  37169. if (ret != 0)
  37170. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37171. if (sz != 32 * 2 + 1)
  37172. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37173. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  37174. if (ret != 0)
  37175. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37176. ret = wc_ecc_cmp_point(pvt, decPvt);
  37177. if (ret != MP_EQ)
  37178. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37179. wc_ecc_del_point(decPvt);
  37180. out:
  37181. mp_free(decSsk);
  37182. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37183. XFREE(decSsk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37184. #endif
  37185. return ret;
  37186. }
  37187. static wc_test_ret_t eccsi_imp_exp_key_test(EccsiKey* priv)
  37188. {
  37189. wc_test_ret_t ret;
  37190. byte data[32 * 3];
  37191. byte out[32 * 3];
  37192. word32 sz;
  37193. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  37194. if (ret != LENGTH_ONLY_E)
  37195. return WC_TEST_RET_ENC_EC(ret);
  37196. if (sz != 32 * 3)
  37197. return WC_TEST_RET_ENC_NC;
  37198. ret = wc_ExportEccsiKey(priv, data, &sz);
  37199. if (ret != 0)
  37200. return WC_TEST_RET_ENC_EC(ret);
  37201. ret = wc_ImportEccsiKey(priv, data, sz);
  37202. if (ret != 0)
  37203. return WC_TEST_RET_ENC_EC(ret);
  37204. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  37205. if (ret != LENGTH_ONLY_E)
  37206. return WC_TEST_RET_ENC_EC(ret);
  37207. if (sz != 32 * 3)
  37208. return WC_TEST_RET_ENC_NC;
  37209. ret = wc_ExportEccsiKey(priv, out, &sz);
  37210. if (ret != 0)
  37211. return WC_TEST_RET_ENC_EC(ret);
  37212. if (sz != 32 * 3)
  37213. return WC_TEST_RET_ENC_NC;
  37214. if (XMEMCMP(data, out, sz) != 0)
  37215. return WC_TEST_RET_ENC_NC;
  37216. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  37217. if (ret != LENGTH_ONLY_E)
  37218. return WC_TEST_RET_ENC_EC(ret);
  37219. if (sz != 32)
  37220. return WC_TEST_RET_ENC_NC;
  37221. ret = wc_ExportEccsiPrivateKey(priv, data, &sz);
  37222. if (ret != 0)
  37223. return WC_TEST_RET_ENC_EC(ret);
  37224. ret = wc_ImportEccsiPrivateKey(priv, data, sz);
  37225. if (ret != 0)
  37226. return WC_TEST_RET_ENC_EC(ret);
  37227. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  37228. if (ret != LENGTH_ONLY_E)
  37229. return WC_TEST_RET_ENC_EC(ret);
  37230. if (sz != 32)
  37231. return WC_TEST_RET_ENC_NC;
  37232. ret = wc_ExportEccsiPrivateKey(priv, out, &sz);
  37233. if (ret != 0)
  37234. return WC_TEST_RET_ENC_EC(ret);
  37235. if (sz != 32)
  37236. return WC_TEST_RET_ENC_NC;
  37237. if (XMEMCMP(data, out, sz) != 0)
  37238. return WC_TEST_RET_ENC_NC;
  37239. return 0;
  37240. }
  37241. static wc_test_ret_t eccsi_imp_exp_pubkey_test(EccsiKey* key1, EccsiKey* key2)
  37242. {
  37243. wc_test_ret_t ret;
  37244. byte data[32 * 2 + 1];
  37245. byte pubData[32 * 2 + 1];
  37246. word32 sz;
  37247. ret = wc_ExportEccsiPublicKey(key1, NULL, &sz, 1);
  37248. if (ret != LENGTH_ONLY_E)
  37249. return WC_TEST_RET_ENC_EC(ret);
  37250. if (sz != 32 * 2)
  37251. return WC_TEST_RET_ENC_NC;
  37252. ret = wc_ExportEccsiPublicKey(key1, data, &sz, 1);
  37253. if (ret != 0)
  37254. return WC_TEST_RET_ENC_EC(ret);
  37255. ret = wc_ImportEccsiPublicKey(key2, data, sz, 1);
  37256. if (ret != 0)
  37257. return WC_TEST_RET_ENC_EC(ret);
  37258. sz = sizeof(pubData);
  37259. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 1);
  37260. if (ret != 0)
  37261. return WC_TEST_RET_ENC_EC(ret);
  37262. if (sz != 32 * 2)
  37263. return WC_TEST_RET_ENC_NC;
  37264. if (XMEMCMP(data, pubData, sz) != 0)
  37265. return WC_TEST_RET_ENC_NC;
  37266. sz = sizeof(pubData);
  37267. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 0);
  37268. if (ret != 0)
  37269. return WC_TEST_RET_ENC_EC(ret);
  37270. if (sz != 32 * 2 + 1)
  37271. return WC_TEST_RET_ENC_NC;
  37272. if (pubData[0] != 0x04)
  37273. return WC_TEST_RET_ENC_NC;
  37274. if (XMEMCMP(pubData + 1, data, sz - 1) != 0)
  37275. return WC_TEST_RET_ENC_NC;
  37276. ret = wc_ImportEccsiPublicKey(key2, pubData, sz, 0);
  37277. if (ret != 0)
  37278. return WC_TEST_RET_ENC_EC(ret);
  37279. return 0;
  37280. }
  37281. static wc_test_ret_t eccsi_make_key_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  37282. mp_int* ssk, ecc_point* pvt)
  37283. {
  37284. wc_test_ret_t ret;
  37285. char mail[] = "test@wolfssl.com";
  37286. byte* id = (byte*)mail;
  37287. word32 idSz = (word32) XSTRLEN(mail);
  37288. int valid;
  37289. ret = wc_MakeEccsiKey(priv, rng);
  37290. if (ret != 0)
  37291. return WC_TEST_RET_ENC_EC(ret);
  37292. ret = eccsi_imp_exp_key_test(priv);
  37293. if (ret < 0)
  37294. return ret;
  37295. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  37296. if (ret < 0)
  37297. return ret;
  37298. ret = wc_MakeEccsiPair(priv, rng, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt);
  37299. if (ret != 0)
  37300. return WC_TEST_RET_ENC_EC(ret);
  37301. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  37302. &valid);
  37303. if (ret != 0)
  37304. return WC_TEST_RET_ENC_EC(ret);
  37305. if (!valid)
  37306. return WC_TEST_RET_ENC_NC;
  37307. ret = eccsi_enc_dec_pair_test(priv, ssk, pvt);
  37308. if (ret != 0)
  37309. return ret;
  37310. return 0;
  37311. }
  37312. static wc_test_ret_t eccsi_sign_verify_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  37313. mp_int* ssk, ecc_point* pvt)
  37314. {
  37315. wc_test_ret_t ret;
  37316. byte hashPriv[WC_MAX_DIGEST_SIZE];
  37317. byte hashPub[WC_MAX_DIGEST_SIZE];
  37318. byte hashSz;
  37319. byte sig[144];
  37320. word32 sigSz;
  37321. int verified, valid;
  37322. char mail[] = "test@wolfssl.com";
  37323. byte* id = (byte*)mail;
  37324. word32 idSz = (word32) XSTRLEN(mail);
  37325. byte msg[] = { 0x00 };
  37326. word32 msgSz = sizeof(msg);
  37327. #ifdef WOLFSSL_SHA384
  37328. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA384, id, idSz, pvt, hashPriv,
  37329. &hashSz);
  37330. if (ret != BAD_FUNC_ARG)
  37331. return WC_TEST_RET_ENC_EC(ret);
  37332. #endif
  37333. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPriv,
  37334. &hashSz);
  37335. if (ret != 0)
  37336. return WC_TEST_RET_ENC_EC(ret);
  37337. if (hashSz != 32)
  37338. return WC_TEST_RET_ENC_NC;
  37339. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPub,
  37340. &hashSz);
  37341. if (ret != 0)
  37342. return WC_TEST_RET_ENC_EC(ret);
  37343. if (hashSz != 32)
  37344. return WC_TEST_RET_ENC_NC;
  37345. if (XMEMCMP(hashPriv, hashPub, hashSz) != 0)
  37346. return WC_TEST_RET_ENC_NC;
  37347. ret = wc_SetEccsiHash(priv, hashPriv, hashSz);
  37348. if (ret != 0)
  37349. return WC_TEST_RET_ENC_EC(ret);
  37350. ret = wc_SetEccsiPair(priv, ssk, pvt);
  37351. if (ret != 0)
  37352. return WC_TEST_RET_ENC_EC(ret);
  37353. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, NULL,
  37354. &sigSz);
  37355. if (ret != LENGTH_ONLY_E)
  37356. return WC_TEST_RET_ENC_EC(ret);
  37357. if (sigSz != 129)
  37358. return WC_TEST_RET_ENC_NC;
  37359. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, sig,
  37360. &sigSz);
  37361. if (ret != 0)
  37362. return WC_TEST_RET_ENC_EC(ret);
  37363. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  37364. if (ret != 0)
  37365. return WC_TEST_RET_ENC_EC(ret);
  37366. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  37367. &verified);
  37368. if (ret != 0)
  37369. return WC_TEST_RET_ENC_EC(ret);
  37370. if (!verified)
  37371. return WC_TEST_RET_ENC_NC;
  37372. /* Check that changing HS results in verification failure. */
  37373. hashPub[0] ^= 0x80;
  37374. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  37375. if (ret != 0)
  37376. return WC_TEST_RET_ENC_EC(ret);
  37377. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  37378. &verified);
  37379. if (ret != 0)
  37380. return WC_TEST_RET_ENC_EC(ret);
  37381. if (verified)
  37382. return WC_TEST_RET_ENC_NC;
  37383. hashPub[0] ^= 0x80;
  37384. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  37385. if (ret != 0)
  37386. return WC_TEST_RET_ENC_EC(ret);
  37387. /* Check that changing msg results in verification failure. */
  37388. msg[0] ^= 0x80;
  37389. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  37390. &verified);
  37391. if (ret != 0)
  37392. return WC_TEST_RET_ENC_EC(ret);
  37393. if (verified)
  37394. return WC_TEST_RET_ENC_NC;
  37395. msg[0] ^= 0x80;
  37396. /* Check that changing signature results in verification failure. */
  37397. sig[0] ^= 0x80;
  37398. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  37399. &verified);
  37400. if (ret != 0)
  37401. return WC_TEST_RET_ENC_EC(ret);
  37402. if (verified)
  37403. return WC_TEST_RET_ENC_NC;
  37404. sig[0] ^= 0x80;
  37405. /* Check that key state hasn't been invalidated. */
  37406. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  37407. &verified);
  37408. if (ret != 0)
  37409. return WC_TEST_RET_ENC_EC(ret);
  37410. if (!verified)
  37411. return WC_TEST_RET_ENC_NC;
  37412. /* Check that verifying with the private key works. */
  37413. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  37414. &verified);
  37415. if (ret != 0)
  37416. return WC_TEST_RET_ENC_EC(ret);
  37417. if (!verified)
  37418. return WC_TEST_RET_ENC_NC;
  37419. /* Check that the KPAK is converted from montgomery form. */
  37420. ret = eccsi_imp_exp_key_test(priv);
  37421. if (ret != 0)
  37422. return ret;
  37423. /* Check that KPAK can converted to Montgomery form again. */
  37424. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  37425. &verified);
  37426. if (ret != 0)
  37427. return WC_TEST_RET_ENC_EC(ret);
  37428. if (!verified)
  37429. return WC_TEST_RET_ENC_NC;
  37430. /* Check that the KPAK is converted from montgomery form. */
  37431. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  37432. &valid);
  37433. if (ret != 0)
  37434. return WC_TEST_RET_ENC_EC(ret);
  37435. if (!valid)
  37436. return WC_TEST_RET_ENC_NC;
  37437. /* Check that KPAK can converted to Montgomery form again. */
  37438. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  37439. &verified);
  37440. if (ret != 0)
  37441. return WC_TEST_RET_ENC_EC(ret);
  37442. if (!verified)
  37443. return WC_TEST_RET_ENC_NC;
  37444. /* Check that the KPAK is converted from montgomery form. */
  37445. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  37446. if (ret != 0)
  37447. return ret;
  37448. return 0;
  37449. }
  37450. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t eccsi_test(void)
  37451. {
  37452. wc_test_ret_t ret = 0;
  37453. WC_RNG rng;
  37454. int rng_inited = 0;
  37455. EccsiKey* priv = NULL;
  37456. EccsiKey* pub = NULL;
  37457. mp_int* ssk = NULL;
  37458. ecc_point* pvt = NULL;
  37459. WOLFSSL_ENTER("eccsi_test");
  37460. priv = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  37461. DYNAMIC_TYPE_TMP_BUFFER);
  37462. if (priv == NULL)
  37463. ret = WC_TEST_RET_ENC_NC;
  37464. else
  37465. XMEMSET(priv, 0, sizeof(*priv));
  37466. if (ret == 0) {
  37467. pub = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  37468. DYNAMIC_TYPE_TMP_BUFFER);
  37469. if (pub == NULL)
  37470. ret = WC_TEST_RET_ENC_NC;
  37471. else
  37472. XMEMSET(pub, 0, sizeof(*pub));
  37473. }
  37474. if (ret == 0) {
  37475. ssk = (mp_int*)XMALLOC(sizeof(mp_int), HEAP_HINT,
  37476. DYNAMIC_TYPE_TMP_BUFFER);
  37477. if (ssk == NULL)
  37478. ret = WC_TEST_RET_ENC_NC;
  37479. else
  37480. XMEMSET(ssk, 0, sizeof(*ssk));
  37481. }
  37482. if (ret == 0) {
  37483. #ifndef HAVE_FIPS
  37484. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  37485. #else
  37486. ret = wc_InitRng(&rng);
  37487. #endif
  37488. if (ret != 0)
  37489. ret = WC_TEST_RET_ENC_EC(ret);
  37490. else
  37491. rng_inited = 1;
  37492. }
  37493. if (ret == 0) {
  37494. pvt = wc_ecc_new_point();
  37495. if (pvt == NULL)
  37496. ret = WC_TEST_RET_ENC_NC;
  37497. }
  37498. if (ret == 0) {
  37499. ret = mp_init(ssk);
  37500. if (ret != 0)
  37501. ret = WC_TEST_RET_ENC_EC(ret);
  37502. }
  37503. if (ret == 0) {
  37504. ret = eccsi_api_test(&rng, priv, ssk, pvt);
  37505. }
  37506. if (ret == 0) {
  37507. ret = wc_InitEccsiKey(pub, HEAP_HINT, INVALID_DEVID);
  37508. if (ret != 0)
  37509. ret = WC_TEST_RET_ENC_EC(ret);
  37510. }
  37511. if (ret == 0) {
  37512. ret = wc_InitEccsiKey(priv, HEAP_HINT, INVALID_DEVID);
  37513. if (ret != 0)
  37514. ret = WC_TEST_RET_ENC_EC(ret);
  37515. }
  37516. if (ret == 0) {
  37517. ret = eccsi_kat_verify_test(pub, pvt);
  37518. }
  37519. if (ret == 0) {
  37520. ret = eccsi_make_key_test(priv, pub, &rng, ssk, pvt);
  37521. }
  37522. if (ret == 0) {
  37523. ret = eccsi_sign_verify_test(priv, pub, &rng, ssk, pvt);
  37524. }
  37525. if (pvt != NULL)
  37526. wc_ecc_del_point(pvt);
  37527. if (rng_inited)
  37528. wc_FreeRng(&rng);
  37529. if (ssk != NULL) {
  37530. mp_free(ssk);
  37531. XFREE(ssk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37532. }
  37533. if (pub != NULL) {
  37534. wc_FreeEccsiKey(pub);
  37535. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37536. }
  37537. if (priv != NULL) {
  37538. wc_FreeEccsiKey(priv);
  37539. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37540. }
  37541. return ret;
  37542. }
  37543. #endif /* WOLFCRYPT_HAVE_ECCSI */
  37544. #ifdef WOLFCRYPT_HAVE_SAKKE
  37545. static wc_test_ret_t sakke_api_test(WC_RNG* rng, SakkeKey* key, ecc_point* rsk)
  37546. {
  37547. wc_test_ret_t ret;
  37548. byte id[1] = { 0x00 };
  37549. int valid;
  37550. byte data[256];
  37551. word32 sz;
  37552. byte auth[257];
  37553. word16 authSz;
  37554. byte ssv[256];
  37555. word16 ssvSz;
  37556. word32 len;
  37557. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  37558. if (ret != BAD_FUNC_ARG)
  37559. return WC_TEST_RET_ENC_EC(ret);
  37560. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  37561. if (ret != BAD_FUNC_ARG)
  37562. return WC_TEST_RET_ENC_EC(ret);
  37563. wc_FreeSakkeKey(NULL);
  37564. XMEMSET(key, 0, sizeof(*key));
  37565. wc_FreeSakkeKey(key);
  37566. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  37567. if (ret != 0)
  37568. return WC_TEST_RET_ENC_EC(ret);
  37569. ret = wc_MakeSakkeKey(NULL, NULL);
  37570. if (ret != BAD_FUNC_ARG)
  37571. return WC_TEST_RET_ENC_EC(ret);
  37572. ret = wc_MakeSakkeKey(key, NULL);
  37573. if (ret != BAD_FUNC_ARG)
  37574. return WC_TEST_RET_ENC_EC(ret);
  37575. ret = wc_MakeSakkeKey(NULL, rng);
  37576. if (ret != BAD_FUNC_ARG)
  37577. return WC_TEST_RET_ENC_EC(ret);
  37578. ret = wc_MakeSakkePublicKey(NULL, NULL);
  37579. if (ret != BAD_FUNC_ARG)
  37580. return WC_TEST_RET_ENC_EC(ret);
  37581. ret = wc_MakeSakkePublicKey(key, NULL);
  37582. if (ret != BAD_FUNC_ARG)
  37583. return WC_TEST_RET_ENC_EC(ret);
  37584. ret = wc_MakeSakkePublicKey(NULL, rsk);
  37585. if (ret != BAD_FUNC_ARG)
  37586. return WC_TEST_RET_ENC_EC(ret);
  37587. ret = wc_MakeSakkeRsk(NULL, NULL, 1, NULL);
  37588. if (ret != BAD_FUNC_ARG)
  37589. return WC_TEST_RET_ENC_EC(ret);
  37590. ret = wc_MakeSakkeRsk(key, id, 1, NULL);
  37591. if (ret != BAD_FUNC_ARG)
  37592. return WC_TEST_RET_ENC_EC(ret);
  37593. ret = wc_MakeSakkeRsk(key, NULL, 1, rsk);
  37594. if (ret != BAD_FUNC_ARG)
  37595. return WC_TEST_RET_ENC_EC(ret);
  37596. ret = wc_MakeSakkeRsk(NULL, id, 1, rsk);
  37597. if (ret != BAD_FUNC_ARG)
  37598. return WC_TEST_RET_ENC_EC(ret);
  37599. ret = wc_ValidateSakkeRsk(NULL, NULL, 1, NULL, NULL);
  37600. if (ret != BAD_FUNC_ARG)
  37601. return WC_TEST_RET_ENC_EC(ret);
  37602. ret = wc_ValidateSakkeRsk(key, id, 1, rsk, NULL);
  37603. if (ret != BAD_FUNC_ARG)
  37604. return WC_TEST_RET_ENC_EC(ret);
  37605. ret = wc_ValidateSakkeRsk(NULL, id, 1, rsk, &valid);
  37606. if (ret != BAD_FUNC_ARG)
  37607. return WC_TEST_RET_ENC_EC(ret);
  37608. ret = wc_ExportSakkeKey(NULL, NULL, NULL);
  37609. if (ret != BAD_FUNC_ARG)
  37610. return WC_TEST_RET_ENC_EC(ret);
  37611. ret = wc_ExportSakkeKey(key, data, NULL);
  37612. if (ret != BAD_FUNC_ARG)
  37613. return WC_TEST_RET_ENC_EC(ret);
  37614. ret = wc_ExportSakkeKey(NULL, data, &sz);
  37615. if (ret != BAD_FUNC_ARG)
  37616. return WC_TEST_RET_ENC_EC(ret);
  37617. ret = wc_ImportSakkeKey(NULL, NULL, 1);
  37618. if (ret != BAD_FUNC_ARG)
  37619. return WC_TEST_RET_ENC_EC(ret);
  37620. ret = wc_ImportSakkeKey(key, NULL, 1);
  37621. if (ret != BAD_FUNC_ARG)
  37622. return WC_TEST_RET_ENC_EC(ret);
  37623. ret = wc_ImportSakkeKey(NULL, data, 1);
  37624. if (ret != BAD_FUNC_ARG)
  37625. return WC_TEST_RET_ENC_EC(ret);
  37626. ret = wc_ExportSakkePrivateKey(NULL, NULL, NULL);
  37627. if (ret != BAD_FUNC_ARG)
  37628. return WC_TEST_RET_ENC_EC(ret);
  37629. ret = wc_ExportSakkePrivateKey(key, data, NULL);
  37630. if (ret != BAD_FUNC_ARG)
  37631. return WC_TEST_RET_ENC_EC(ret);
  37632. ret = wc_ExportSakkePrivateKey(NULL, data, &sz);
  37633. if (ret != BAD_FUNC_ARG)
  37634. return WC_TEST_RET_ENC_EC(ret);
  37635. ret = wc_ImportSakkePrivateKey(NULL, NULL, 1);
  37636. if (ret != BAD_FUNC_ARG)
  37637. return WC_TEST_RET_ENC_EC(ret);
  37638. ret = wc_ImportSakkePrivateKey(key, NULL, 1);
  37639. if (ret != BAD_FUNC_ARG)
  37640. return WC_TEST_RET_ENC_EC(ret);
  37641. ret = wc_ImportSakkePrivateKey(NULL, data, 1);
  37642. if (ret != BAD_FUNC_ARG)
  37643. return WC_TEST_RET_ENC_EC(ret);
  37644. sz = sizeof(data);
  37645. ret = wc_EncodeSakkeRsk(NULL, NULL, data, NULL, 1);
  37646. if (ret != BAD_FUNC_ARG)
  37647. return WC_TEST_RET_ENC_EC(ret);
  37648. ret = wc_EncodeSakkeRsk(key, rsk, data, NULL, 1);
  37649. if (ret != BAD_FUNC_ARG)
  37650. return WC_TEST_RET_ENC_EC(ret);
  37651. ret = wc_EncodeSakkeRsk(key, NULL, data, &sz, 1);
  37652. if (ret != BAD_FUNC_ARG)
  37653. return WC_TEST_RET_ENC_EC(ret);
  37654. ret = wc_EncodeSakkeRsk(NULL, rsk, data, &sz, 1);
  37655. if (ret != BAD_FUNC_ARG)
  37656. return WC_TEST_RET_ENC_EC(ret);
  37657. ret = wc_DecodeSakkeRsk(NULL, NULL, sz, NULL);
  37658. if (ret != BAD_FUNC_ARG)
  37659. return WC_TEST_RET_ENC_EC(ret);
  37660. ret = wc_DecodeSakkeRsk(key, data, sz, NULL);
  37661. if (ret != BAD_FUNC_ARG)
  37662. return WC_TEST_RET_ENC_EC(ret);
  37663. ret = wc_DecodeSakkeRsk(key, NULL, sz, rsk);
  37664. if (ret != BAD_FUNC_ARG)
  37665. return WC_TEST_RET_ENC_EC(ret);
  37666. ret = wc_DecodeSakkeRsk(NULL, data, sz, rsk);
  37667. if (ret != BAD_FUNC_ARG)
  37668. return WC_TEST_RET_ENC_EC(ret);
  37669. ret = wc_ImportSakkeRsk(NULL, NULL, sz);
  37670. if (ret != BAD_FUNC_ARG)
  37671. return WC_TEST_RET_ENC_EC(ret);
  37672. ret = wc_ImportSakkeRsk(key, NULL, sz);
  37673. if (ret != BAD_FUNC_ARG)
  37674. return WC_TEST_RET_ENC_EC(ret);
  37675. ret = wc_ImportSakkeRsk(NULL, data, sz);
  37676. if (ret != BAD_FUNC_ARG)
  37677. return WC_TEST_RET_ENC_EC(ret);
  37678. ret = wc_ImportSakkeRsk(key, data, 1);
  37679. if (ret != BUFFER_E)
  37680. return WC_TEST_RET_ENC_EC(ret);
  37681. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, NULL);
  37682. if (ret != BAD_FUNC_ARG)
  37683. return WC_TEST_RET_ENC_EC(ret);
  37684. ret = wc_GenerateSakkeRskTable(key, NULL, data, NULL);
  37685. if (ret != BAD_FUNC_ARG)
  37686. return WC_TEST_RET_ENC_EC(ret);
  37687. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, NULL);
  37688. if (ret != BAD_FUNC_ARG)
  37689. return WC_TEST_RET_ENC_EC(ret);
  37690. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, &len);
  37691. if (ret != BAD_FUNC_ARG)
  37692. return WC_TEST_RET_ENC_EC(ret);
  37693. ret = wc_GenerateSakkeRskTable(key, rsk, data, NULL);
  37694. if (ret != BAD_FUNC_ARG)
  37695. return WC_TEST_RET_ENC_EC(ret);
  37696. ret = wc_GenerateSakkeRskTable(key, NULL, data, &len);
  37697. if (ret != BAD_FUNC_ARG)
  37698. return WC_TEST_RET_ENC_EC(ret);
  37699. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, &len);
  37700. if (ret != BAD_FUNC_ARG)
  37701. return WC_TEST_RET_ENC_EC(ret);
  37702. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  37703. if (ret != LENGTH_ONLY_E)
  37704. return WC_TEST_RET_ENC_EC(ret);
  37705. len--;
  37706. ret = wc_GenerateSakkeRskTable(key, rsk, data, &len);
  37707. if (ret != BUFFER_E)
  37708. return WC_TEST_RET_ENC_EC(ret);
  37709. ret = wc_ExportSakkePublicKey(NULL, data, NULL, 1);
  37710. if (ret != BAD_FUNC_ARG)
  37711. return WC_TEST_RET_ENC_EC(ret);
  37712. ret = wc_ExportSakkePublicKey(key, data, NULL, 1);
  37713. if (ret != BAD_FUNC_ARG)
  37714. return WC_TEST_RET_ENC_EC(ret);
  37715. ret = wc_ExportSakkePublicKey(NULL, data, &sz, 1);
  37716. if (ret != BAD_FUNC_ARG)
  37717. return WC_TEST_RET_ENC_EC(ret);
  37718. ret = wc_ImportSakkePublicKey(NULL, NULL, sz, 1);
  37719. if (ret != BAD_FUNC_ARG)
  37720. return WC_TEST_RET_ENC_EC(ret);
  37721. ret = wc_ImportSakkePublicKey(key, NULL, sz, 1);
  37722. if (ret != BAD_FUNC_ARG)
  37723. return WC_TEST_RET_ENC_EC(ret);
  37724. ret = wc_ImportSakkePublicKey(NULL, data, sz, 1);
  37725. if (ret != BAD_FUNC_ARG)
  37726. return WC_TEST_RET_ENC_EC(ret);
  37727. ret = wc_GetSakkeAuthSize(NULL, NULL);
  37728. if (ret != BAD_FUNC_ARG)
  37729. return WC_TEST_RET_ENC_EC(ret);
  37730. ret = wc_GetSakkeAuthSize(key, NULL);
  37731. if (ret != BAD_FUNC_ARG)
  37732. return WC_TEST_RET_ENC_EC(ret);
  37733. ret = wc_GetSakkeAuthSize(NULL, &authSz);
  37734. if (ret != BAD_FUNC_ARG)
  37735. return WC_TEST_RET_ENC_EC(ret);
  37736. ret = wc_MakeSakkePointI(NULL, NULL, SAKKE_ID_MAX_SIZE + 1);
  37737. if (ret != BAD_FUNC_ARG)
  37738. return WC_TEST_RET_ENC_EC(ret);
  37739. ret = wc_MakeSakkePointI(key, NULL, SAKKE_ID_MAX_SIZE + 1);
  37740. if (ret != BAD_FUNC_ARG)
  37741. return WC_TEST_RET_ENC_EC(ret);
  37742. ret = wc_MakeSakkePointI(NULL, id, 1);
  37743. if (ret != BAD_FUNC_ARG)
  37744. return WC_TEST_RET_ENC_EC(ret);
  37745. ret = wc_MakeSakkePointI(NULL, NULL, 1);
  37746. if (ret != BAD_FUNC_ARG)
  37747. return WC_TEST_RET_ENC_EC(ret);
  37748. ret = wc_MakeSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1);
  37749. if (ret != BAD_FUNC_ARG)
  37750. return WC_TEST_RET_ENC_EC(ret);
  37751. ret = wc_MakeSakkePointI(key, NULL, 1);
  37752. if (ret != BAD_FUNC_ARG)
  37753. return WC_TEST_RET_ENC_EC(ret);
  37754. ret = wc_MakeSakkePointI(NULL, id, 1);
  37755. if (ret != BAD_FUNC_ARG)
  37756. return WC_TEST_RET_ENC_EC(ret);
  37757. ret = wc_GenerateSakkePointITable(NULL, data, NULL);
  37758. if (ret != BAD_FUNC_ARG)
  37759. return WC_TEST_RET_ENC_EC(ret);
  37760. ret = wc_GenerateSakkePointITable(key, data, NULL);
  37761. if (ret != BAD_FUNC_ARG)
  37762. return WC_TEST_RET_ENC_EC(ret);
  37763. ret = wc_GenerateSakkePointITable(NULL, data, &len);
  37764. if (ret != BAD_FUNC_ARG)
  37765. return WC_TEST_RET_ENC_EC(ret);
  37766. ret = wc_GenerateSakkePointITable(key, NULL, &len);
  37767. if (ret != LENGTH_ONLY_E)
  37768. return WC_TEST_RET_ENC_EC(ret);
  37769. len--;
  37770. ret = wc_GenerateSakkePointITable(key, data, &len);
  37771. if (ret != BUFFER_E)
  37772. return WC_TEST_RET_ENC_EC(ret);
  37773. ret = wc_SetSakkePointITable(NULL, NULL, 1);
  37774. if (ret != BAD_FUNC_ARG)
  37775. return WC_TEST_RET_ENC_EC(ret);
  37776. ret = wc_SetSakkePointITable(key, NULL, 1);
  37777. if (ret != BAD_FUNC_ARG)
  37778. return WC_TEST_RET_ENC_EC(ret);
  37779. ret = wc_SetSakkePointITable(NULL, data, 1);
  37780. if (ret != BAD_FUNC_ARG)
  37781. return WC_TEST_RET_ENC_EC(ret);
  37782. ret = wc_SetSakkePointITable(key, data, 1);
  37783. if (ret != BUFFER_E)
  37784. return WC_TEST_RET_ENC_EC(ret);
  37785. ret = wc_ClearSakkePointITable(NULL);
  37786. if (ret != BAD_FUNC_ARG)
  37787. return WC_TEST_RET_ENC_EC(ret);
  37788. ret = wc_GetSakkePointI(NULL, data, NULL);
  37789. if (ret != BAD_FUNC_ARG)
  37790. return WC_TEST_RET_ENC_EC(ret);
  37791. ret = wc_GetSakkePointI(key, data, NULL);
  37792. if (ret != BAD_FUNC_ARG)
  37793. return WC_TEST_RET_ENC_EC(ret);
  37794. ret = wc_GetSakkePointI(NULL, data, &sz);
  37795. if (ret != BAD_FUNC_ARG)
  37796. return WC_TEST_RET_ENC_EC(ret);
  37797. sz = 1;
  37798. ret = wc_GetSakkePointI(key, data, &sz);
  37799. if (ret != BUFFER_E)
  37800. return WC_TEST_RET_ENC_EC(ret);
  37801. sz = 256;
  37802. ret = wc_SetSakkePointI(NULL, NULL, 1, NULL, sz);
  37803. if (ret != BAD_FUNC_ARG)
  37804. return WC_TEST_RET_ENC_EC(ret);
  37805. ret = wc_SetSakkePointI(key, NULL, 1, NULL, sz);
  37806. if (ret != BAD_FUNC_ARG)
  37807. return WC_TEST_RET_ENC_EC(ret);
  37808. ret = wc_SetSakkePointI(NULL, id, 1, NULL, sz);
  37809. if (ret != BAD_FUNC_ARG)
  37810. return WC_TEST_RET_ENC_EC(ret);
  37811. ret = wc_SetSakkePointI(NULL, NULL, 1, data, sz);
  37812. if (ret != BAD_FUNC_ARG)
  37813. return WC_TEST_RET_ENC_EC(ret);
  37814. ret = wc_SetSakkePointI(key, id, 1, NULL, sz);
  37815. if (ret != BAD_FUNC_ARG)
  37816. return WC_TEST_RET_ENC_EC(ret);
  37817. ret = wc_SetSakkePointI(key, NULL, 1, data, sz);
  37818. if (ret != BAD_FUNC_ARG)
  37819. return WC_TEST_RET_ENC_EC(ret);
  37820. ret = wc_SetSakkePointI(NULL, id, 1, data, sz);
  37821. if (ret != BAD_FUNC_ARG)
  37822. return WC_TEST_RET_ENC_EC(ret);
  37823. ret = wc_SetSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1, data, sz);
  37824. if (ret != BUFFER_E)
  37825. return WC_TEST_RET_ENC_EC(ret);
  37826. ret = wc_SetSakkePointI(key, id, 1, data, sz - 1);
  37827. if (ret != BUFFER_E)
  37828. return WC_TEST_RET_ENC_EC(ret);
  37829. ret = wc_SetSakkeIdentity(NULL, NULL, 1);
  37830. if (ret != BAD_FUNC_ARG)
  37831. return WC_TEST_RET_ENC_EC(ret);
  37832. ret = wc_SetSakkeIdentity(key, NULL, 1);
  37833. if (ret != BAD_FUNC_ARG)
  37834. return WC_TEST_RET_ENC_EC(ret);
  37835. ret = wc_SetSakkeIdentity(NULL, id, 1);
  37836. if (ret != BAD_FUNC_ARG)
  37837. return WC_TEST_RET_ENC_EC(ret);
  37838. ssvSz = sizeof(ssv);
  37839. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  37840. auth, NULL);
  37841. if (ret != BAD_FUNC_ARG)
  37842. return WC_TEST_RET_ENC_EC(ret);
  37843. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  37844. auth, NULL);
  37845. if (ret != BAD_FUNC_ARG)
  37846. return WC_TEST_RET_ENC_EC(ret);
  37847. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  37848. auth, NULL);
  37849. if (ret != BAD_FUNC_ARG)
  37850. return WC_TEST_RET_ENC_EC(ret);
  37851. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  37852. auth, &authSz);
  37853. if (ret != BAD_FUNC_ARG)
  37854. return WC_TEST_RET_ENC_EC(ret);
  37855. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  37856. auth, NULL);
  37857. if (ret != BAD_FUNC_ARG)
  37858. return WC_TEST_RET_ENC_EC(ret);
  37859. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  37860. auth, &authSz);
  37861. if (ret != BAD_FUNC_ARG)
  37862. return WC_TEST_RET_ENC_EC(ret);
  37863. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  37864. auth, &authSz);
  37865. if (ret != BAD_FUNC_ARG)
  37866. return WC_TEST_RET_ENC_EC(ret);
  37867. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  37868. auth, &authSz);
  37869. if (ret != BAD_STATE_E)
  37870. return WC_TEST_RET_ENC_EC(ret);
  37871. ret = wc_GenerateSakkeSSV(NULL, NULL, data, NULL);
  37872. if (ret != BAD_FUNC_ARG)
  37873. return WC_TEST_RET_ENC_EC(ret);
  37874. ret = wc_GenerateSakkeSSV(key, rng, data, NULL);
  37875. if (ret != BAD_FUNC_ARG)
  37876. return WC_TEST_RET_ENC_EC(ret);
  37877. ret = wc_GenerateSakkeSSV(key, NULL, data, &ssvSz);
  37878. if (ret != BAD_FUNC_ARG)
  37879. return WC_TEST_RET_ENC_EC(ret);
  37880. ret = wc_GenerateSakkeSSV(NULL, rng, data, &ssvSz);
  37881. if (ret != BAD_FUNC_ARG)
  37882. return WC_TEST_RET_ENC_EC(ret);
  37883. ret = wc_SetSakkeRsk(NULL, NULL, data, 1);
  37884. if (ret != BAD_FUNC_ARG)
  37885. return WC_TEST_RET_ENC_EC(ret);
  37886. ret = wc_SetSakkeRsk(key, NULL, data, 1);
  37887. if (ret != BAD_FUNC_ARG)
  37888. return WC_TEST_RET_ENC_EC(ret);
  37889. ret = wc_SetSakkeRsk(NULL, rsk, data, 1);
  37890. if (ret != BAD_FUNC_ARG)
  37891. return WC_TEST_RET_ENC_EC(ret);
  37892. ssvSz = sizeof(ssv);
  37893. authSz = sizeof(auth);
  37894. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  37895. authSz);
  37896. if (ret != BAD_FUNC_ARG)
  37897. return WC_TEST_RET_ENC_EC(ret);
  37898. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  37899. authSz);
  37900. if (ret != BAD_FUNC_ARG)
  37901. return WC_TEST_RET_ENC_EC(ret);
  37902. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  37903. authSz);
  37904. if (ret != BAD_FUNC_ARG)
  37905. return WC_TEST_RET_ENC_EC(ret);
  37906. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  37907. authSz);
  37908. if (ret != BAD_FUNC_ARG)
  37909. return WC_TEST_RET_ENC_EC(ret);
  37910. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  37911. authSz);
  37912. if (ret != BAD_FUNC_ARG)
  37913. return WC_TEST_RET_ENC_EC(ret);
  37914. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  37915. authSz);
  37916. if (ret != BAD_FUNC_ARG)
  37917. return WC_TEST_RET_ENC_EC(ret);
  37918. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  37919. authSz);
  37920. if (ret != BAD_FUNC_ARG)
  37921. return WC_TEST_RET_ENC_EC(ret);
  37922. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  37923. authSz);
  37924. if (ret != BAD_STATE_E)
  37925. return WC_TEST_RET_ENC_EC(ret);
  37926. ret = wc_SetSakkeIdentity(key, id, 1);
  37927. if (ret != 0)
  37928. return WC_TEST_RET_ENC_EC(ret);
  37929. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  37930. authSz);
  37931. if (ret != BAD_STATE_E)
  37932. return WC_TEST_RET_ENC_EC(ret);
  37933. ret = wc_SetSakkeIdentity(key, id, 0);
  37934. if (ret != 0)
  37935. return WC_TEST_RET_ENC_EC(ret);
  37936. ret = wc_SetSakkeRsk(key, rsk, data, 1);
  37937. if (ret != 0)
  37938. return WC_TEST_RET_ENC_EC(ret);
  37939. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  37940. authSz);
  37941. if (ret != BAD_STATE_E)
  37942. return WC_TEST_RET_ENC_EC(ret);
  37943. wc_FreeSakkeKey(key);
  37944. return 0;
  37945. }
  37946. static wc_test_ret_t sakke_kat_derive_test(SakkeKey* key, ecc_point* rsk)
  37947. {
  37948. WOLFSSL_SMALL_STACK_STATIC const byte pubData[] = {
  37949. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  37950. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  37951. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  37952. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  37953. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  37954. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  37955. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  37956. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  37957. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  37958. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  37959. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  37960. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  37961. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  37962. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  37963. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  37964. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  37965. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  37966. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  37967. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  37968. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  37969. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  37970. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  37971. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  37972. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  37973. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  37974. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  37975. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  37976. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  37977. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  37978. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  37979. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  37980. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  37981. };
  37982. WOLFSSL_SMALL_STACK_STATIC const byte rskData[] = {
  37983. 0x93, 0xAF, 0x67, 0xE5, 0x00, 0x7B, 0xA6, 0xE6,
  37984. 0xA8, 0x0D, 0xA7, 0x93, 0xDA, 0x30, 0x0F, 0xA4,
  37985. 0xB5, 0x2D, 0x0A, 0x74, 0xE2, 0x5E, 0x6E, 0x7B,
  37986. 0x2B, 0x3D, 0x6E, 0xE9, 0xD1, 0x8A, 0x9B, 0x5C,
  37987. 0x50, 0x23, 0x59, 0x7B, 0xD8, 0x2D, 0x80, 0x62,
  37988. 0xD3, 0x40, 0x19, 0x56, 0x3B, 0xA1, 0xD2, 0x5C,
  37989. 0x0D, 0xC5, 0x6B, 0x7B, 0x97, 0x9D, 0x74, 0xAA,
  37990. 0x50, 0xF2, 0x9F, 0xBF, 0x11, 0xCC, 0x2C, 0x93,
  37991. 0xF5, 0xDF, 0xCA, 0x61, 0x5E, 0x60, 0x92, 0x79,
  37992. 0xF6, 0x17, 0x5C, 0xEA, 0xDB, 0x00, 0xB5, 0x8C,
  37993. 0x6B, 0xEE, 0x1E, 0x7A, 0x2A, 0x47, 0xC4, 0xF0,
  37994. 0xC4, 0x56, 0xF0, 0x52, 0x59, 0xA6, 0xFA, 0x94,
  37995. 0xA6, 0x34, 0xA4, 0x0D, 0xAE, 0x1D, 0xF5, 0x93,
  37996. 0xD4, 0xFE, 0xCF, 0x68, 0x8D, 0x5F, 0xC6, 0x78,
  37997. 0xBE, 0x7E, 0xFC, 0x6D, 0xF3, 0xD6, 0x83, 0x53,
  37998. 0x25, 0xB8, 0x3B, 0x2C, 0x6E, 0x69, 0x03, 0x6B,
  37999. 0x15, 0x5F, 0x0A, 0x27, 0x24, 0x10, 0x94, 0xB0,
  38000. 0x4B, 0xFB, 0x0B, 0xDF, 0xAC, 0x6C, 0x67, 0x0A,
  38001. 0x65, 0xC3, 0x25, 0xD3, 0x9A, 0x06, 0x9F, 0x03,
  38002. 0x65, 0x9D, 0x44, 0xCA, 0x27, 0xD3, 0xBE, 0x8D,
  38003. 0xF3, 0x11, 0x17, 0x2B, 0x55, 0x41, 0x60, 0x18,
  38004. 0x1C, 0xBE, 0x94, 0xA2, 0xA7, 0x83, 0x32, 0x0C,
  38005. 0xED, 0x59, 0x0B, 0xC4, 0x26, 0x44, 0x70, 0x2C,
  38006. 0xF3, 0x71, 0x27, 0x1E, 0x49, 0x6B, 0xF2, 0x0F,
  38007. 0x58, 0x8B, 0x78, 0xA1, 0xBC, 0x01, 0xEC, 0xBB,
  38008. 0x65, 0x59, 0x93, 0x4B, 0xDD, 0x2F, 0xB6, 0x5D,
  38009. 0x28, 0x84, 0x31, 0x8A, 0x33, 0xD1, 0xA4, 0x2A,
  38010. 0xDF, 0x5E, 0x33, 0xCC, 0x58, 0x00, 0x28, 0x0B,
  38011. 0x28, 0x35, 0x64, 0x97, 0xF8, 0x71, 0x35, 0xBA,
  38012. 0xB9, 0x61, 0x2A, 0x17, 0x26, 0x04, 0x24, 0x40,
  38013. 0x9A, 0xC1, 0x5F, 0xEE, 0x99, 0x6B, 0x74, 0x4C,
  38014. 0x33, 0x21, 0x51, 0x23, 0x5D, 0xEC, 0xB0, 0xF5
  38015. };
  38016. WOLFSSL_SMALL_STACK_STATIC const byte id[] = {
  38017. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  38018. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  38019. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  38020. 0x33, 0x00
  38021. };
  38022. WOLFSSL_SMALL_STACK_STATIC const byte ssv[] = {
  38023. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  38024. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  38025. };
  38026. WOLFSSL_SMALL_STACK_STATIC const byte auth[] = {
  38027. 0x04,
  38028. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  38029. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  38030. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  38031. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  38032. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  38033. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  38034. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  38035. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  38036. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  38037. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  38038. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  38039. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  38040. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  38041. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  38042. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  38043. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  38044. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  38045. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  38046. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  38047. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  38048. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  38049. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  38050. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  38051. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  38052. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  38053. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  38054. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  38055. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  38056. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  38057. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  38058. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  38059. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  38060. };
  38061. WOLFSSL_SMALL_STACK_STATIC const byte encSsv[] = {
  38062. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  38063. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  38064. };
  38065. wc_test_ret_t ret;
  38066. int valid;
  38067. byte pubKey[sizeof(pubData) + 1];
  38068. word32 sz = sizeof(pubKey);
  38069. byte tmpSsv[sizeof(encSsv)];
  38070. byte* iTable = NULL;
  38071. word32 iTableLen;
  38072. byte* table = NULL;
  38073. word32 len;
  38074. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  38075. if (ret != 0)
  38076. return WC_TEST_RET_ENC_EC(ret);
  38077. ret = wc_DecodeSakkeRsk(key, rskData, sizeof(rskData), rsk);
  38078. if (ret != 0)
  38079. return WC_TEST_RET_ENC_EC(ret);
  38080. ret = wc_ValidateSakkeRsk(key, id, sizeof(id), rsk, &valid);
  38081. if (ret != 0)
  38082. return WC_TEST_RET_ENC_EC(ret);
  38083. if (valid != 1)
  38084. return WC_TEST_RET_ENC_NC;
  38085. ret = wc_SetSakkeRsk(key, rsk, NULL, 0);
  38086. if (ret != 0)
  38087. return WC_TEST_RET_ENC_EC(ret);
  38088. ret = wc_SetSakkeIdentity(key, id, sizeof(id));
  38089. if (ret != 0)
  38090. return WC_TEST_RET_ENC_EC(ret);
  38091. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  38092. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  38093. auth, sizeof(auth));
  38094. if (ret != 0)
  38095. return WC_TEST_RET_ENC_EC(ret);
  38096. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  38097. return WC_TEST_RET_ENC_NC;
  38098. ret = wc_MakeSakkePointI(key, id, sizeof(id));
  38099. if (ret != 0)
  38100. return WC_TEST_RET_ENC_EC(ret);
  38101. iTableLen = 0;
  38102. ret = wc_GenerateSakkePointITable(key, NULL, &iTableLen);
  38103. if (ret != LENGTH_ONLY_E)
  38104. return WC_TEST_RET_ENC_EC(ret);
  38105. if (iTableLen != 0) {
  38106. iTable = (byte*)XMALLOC(iTableLen, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38107. if (iTable == NULL)
  38108. return WC_TEST_RET_ENC_ERRNO;
  38109. ret = wc_GenerateSakkePointITable(key, iTable, &iTableLen);
  38110. if (ret != 0)
  38111. return WC_TEST_RET_ENC_EC(ret);
  38112. }
  38113. len = 0;
  38114. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  38115. if (ret != LENGTH_ONLY_E)
  38116. return WC_TEST_RET_ENC_EC(ret);
  38117. if (len > 0) {
  38118. table = (byte*)XMALLOC(len, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38119. if (table == NULL)
  38120. return WC_TEST_RET_ENC_ERRNO;
  38121. ret = wc_GenerateSakkeRskTable(key, rsk, table, &len);
  38122. if (ret != 0)
  38123. return WC_TEST_RET_ENC_EC(ret);
  38124. }
  38125. ret = wc_SetSakkeRsk(key, rsk, table, len);
  38126. if (ret != 0)
  38127. return WC_TEST_RET_ENC_EC(ret);
  38128. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  38129. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  38130. auth, sizeof(auth));
  38131. if (ret != 0)
  38132. return WC_TEST_RET_ENC_EC(ret);
  38133. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  38134. return WC_TEST_RET_ENC_NC;
  38135. /* Don't reference table that is about to be freed. */
  38136. ret = wc_ClearSakkePointITable(key);
  38137. if (ret != 0)
  38138. return WC_TEST_RET_ENC_EC(ret);
  38139. /* Dispose of tables */
  38140. if (iTable != NULL)
  38141. XFREE(iTable, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38142. if (table != NULL)
  38143. XFREE(table, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38144. /* Make sure the key public key is exportable - convert to Montgomery form
  38145. * in Validation.
  38146. */
  38147. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 1);
  38148. if (ret != 0)
  38149. return WC_TEST_RET_ENC_EC(ret);
  38150. if (sz != sizeof(pubData))
  38151. return WC_TEST_RET_ENC_NC;
  38152. if (XMEMCMP(pubKey, pubData, sizeof(pubData)) != 0)
  38153. return WC_TEST_RET_ENC_NC;
  38154. sz = sizeof(pubData) + 1;
  38155. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 0);
  38156. if (ret != 0)
  38157. return WC_TEST_RET_ENC_EC(ret);
  38158. if (sz != sizeof(pubData) + 1)
  38159. return WC_TEST_RET_ENC_NC;
  38160. if (pubKey[0] != 0x04)
  38161. return WC_TEST_RET_ENC_NC;
  38162. if (XMEMCMP(pubKey + 1, pubData, sizeof(pubData)) != 0)
  38163. return WC_TEST_RET_ENC_NC;
  38164. return 0;
  38165. }
  38166. static wc_test_ret_t sakke_kat_encapsulate_test(SakkeKey* key)
  38167. {
  38168. static const byte pubData[] = {
  38169. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  38170. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  38171. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  38172. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  38173. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  38174. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  38175. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  38176. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  38177. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  38178. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  38179. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  38180. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  38181. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  38182. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  38183. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  38184. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  38185. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  38186. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  38187. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  38188. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  38189. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  38190. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  38191. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  38192. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  38193. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  38194. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  38195. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  38196. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  38197. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  38198. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  38199. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  38200. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  38201. };
  38202. static const byte id[] = {
  38203. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  38204. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  38205. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  38206. 0x33, 0x00
  38207. };
  38208. static const word32 idSz = sizeof(id);
  38209. byte ssv[] = {
  38210. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  38211. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  38212. };
  38213. static const word16 ssvSz = sizeof(ssv);
  38214. static const byte expAuth[] = {
  38215. 0x04,
  38216. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  38217. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  38218. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  38219. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  38220. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  38221. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  38222. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  38223. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  38224. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  38225. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  38226. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  38227. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  38228. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  38229. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  38230. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  38231. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  38232. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  38233. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  38234. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  38235. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  38236. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  38237. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  38238. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  38239. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  38240. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  38241. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  38242. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  38243. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  38244. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  38245. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  38246. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  38247. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  38248. };
  38249. static const byte encSsv[] = {
  38250. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  38251. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  38252. };
  38253. wc_test_ret_t ret;
  38254. byte auth[257];
  38255. word16 authSz = sizeof(auth);
  38256. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  38257. if (ret != 0)
  38258. return WC_TEST_RET_ENC_EC(ret);
  38259. ret = wc_SetSakkeIdentity(key, id, idSz);
  38260. if (ret != 0)
  38261. return WC_TEST_RET_ENC_EC(ret);
  38262. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  38263. auth, &authSz);
  38264. if (ret != 0)
  38265. return WC_TEST_RET_ENC_EC(ret);
  38266. if (authSz != 257)
  38267. return WC_TEST_RET_ENC_NC;
  38268. if (XMEMCMP(ssv, encSsv, ssvSz) != 0)
  38269. return WC_TEST_RET_ENC_NC;
  38270. if (XMEMCMP(auth, expAuth, authSz) != 0)
  38271. return WC_TEST_RET_ENC_NC;
  38272. return 0;
  38273. }
  38274. static wc_test_ret_t sakke_make_key_test(SakkeKey* priv, SakkeKey* pub, SakkeKey* key,
  38275. WC_RNG* rng, ecc_point* rsk)
  38276. {
  38277. wc_test_ret_t ret;
  38278. byte data[440];
  38279. byte pubData[257];
  38280. word32 sz;
  38281. char mail[] = "test@wolfssl.com";
  38282. byte* id = (byte*)mail;
  38283. word32 idSz = (word32)XSTRLEN(mail);
  38284. int valid;
  38285. ecc_point* pubKey = rsk;
  38286. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  38287. if (ret != 0)
  38288. return WC_TEST_RET_ENC_EC(ret);
  38289. ret = wc_MakeSakkeKey(priv, rng);
  38290. if (ret != 0)
  38291. return WC_TEST_RET_ENC_EC(ret);
  38292. ret = wc_ExportSakkeKey(priv, NULL, &sz);
  38293. if (ret != LENGTH_ONLY_E)
  38294. return WC_TEST_RET_ENC_EC(ret);
  38295. if (sz != 384)
  38296. return WC_TEST_RET_ENC_NC;
  38297. sz--;
  38298. ret = wc_ExportSakkeKey(priv, data, &sz);
  38299. if (ret == 0)
  38300. return WC_TEST_RET_ENC_NC;
  38301. sz++;
  38302. ret = wc_ExportSakkeKey(priv, data, &sz);
  38303. if (ret != 0)
  38304. return WC_TEST_RET_ENC_EC(ret);
  38305. if (sz != 384)
  38306. return WC_TEST_RET_ENC_NC;
  38307. ret = wc_ImportSakkeKey(key, data, sz - 1);
  38308. if (ret == 0)
  38309. return WC_TEST_RET_ENC_NC;
  38310. ret = wc_ImportSakkeKey(key, data, sz);
  38311. if (ret != 0)
  38312. return WC_TEST_RET_ENC_EC(ret);
  38313. wc_FreeSakkeKey(key);
  38314. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  38315. if (ret != 0)
  38316. return WC_TEST_RET_ENC_EC(ret);
  38317. ret = wc_ExportSakkePrivateKey(priv, NULL, &sz);
  38318. if (ret != LENGTH_ONLY_E)
  38319. return WC_TEST_RET_ENC_EC(ret);
  38320. if (sz != 128)
  38321. return WC_TEST_RET_ENC_NC;
  38322. sz--;
  38323. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  38324. if (ret == 0)
  38325. return WC_TEST_RET_ENC_NC;
  38326. sz++;
  38327. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  38328. if (ret != 0)
  38329. return WC_TEST_RET_ENC_EC(ret);
  38330. if (sz != 128)
  38331. return WC_TEST_RET_ENC_NC;
  38332. ret = wc_ImportSakkePrivateKey(key, data, sz - 1);
  38333. if (ret == 0)
  38334. return WC_TEST_RET_ENC_NC;
  38335. ret = wc_ImportSakkePrivateKey(key, data, sz);
  38336. if (ret != 0)
  38337. return WC_TEST_RET_ENC_EC(ret);
  38338. ret = wc_MakeSakkePublicKey(key, pubKey);
  38339. if (ret != 0)
  38340. return WC_TEST_RET_ENC_EC(ret);
  38341. ret = wc_ExportSakkePublicKey(priv, NULL, &sz, 1);
  38342. if (ret != LENGTH_ONLY_E)
  38343. return WC_TEST_RET_ENC_EC(ret);
  38344. if (sz != 256)
  38345. return WC_TEST_RET_ENC_NC;
  38346. sz--;
  38347. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  38348. if (ret == 0)
  38349. return WC_TEST_RET_ENC_NC;
  38350. sz++;
  38351. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  38352. if (ret != 0)
  38353. return WC_TEST_RET_ENC_EC(ret);
  38354. if (sz != 256)
  38355. return WC_TEST_RET_ENC_NC;
  38356. ret = wc_ImportSakkePublicKey(pub, data, sz - 1, 1);
  38357. if (ret == 0)
  38358. return WC_TEST_RET_ENC_NC;
  38359. ret = wc_ImportSakkePublicKey(pub, data, sz, 1);
  38360. if (ret != 0)
  38361. return WC_TEST_RET_ENC_EC(ret);
  38362. ret = wc_ExportSakkePublicKey(pub, pubData, &sz, 1);
  38363. if (ret != 0)
  38364. return WC_TEST_RET_ENC_EC(ret);
  38365. if (sz != 256)
  38366. return WC_TEST_RET_ENC_NC;
  38367. if (XMEMCMP(data, pubData, sz) != 0)
  38368. return WC_TEST_RET_ENC_NC;
  38369. ret = wc_MakeSakkeRsk(priv, id, idSz, rsk);
  38370. if (ret != 0)
  38371. return WC_TEST_RET_ENC_EC(ret);
  38372. ret = wc_ValidateSakkeRsk(priv, id, idSz, rsk, &valid);
  38373. if (ret != 0)
  38374. return WC_TEST_RET_ENC_EC(ret);
  38375. if (valid != 1)
  38376. return WC_TEST_RET_ENC_NC;
  38377. ret = wc_ValidateSakkeRsk(pub, id, idSz, rsk, &valid);
  38378. if (ret != 0)
  38379. return WC_TEST_RET_ENC_EC(ret);
  38380. if (valid != 1)
  38381. return WC_TEST_RET_ENC_NC;
  38382. sz = sizeof(data);
  38383. ret = wc_EncodeSakkeRsk(priv, rsk, data, &sz, 1);
  38384. if (ret != 0)
  38385. return WC_TEST_RET_ENC_EC(ret);
  38386. if (sz != 256)
  38387. return WC_TEST_RET_ENC_NC;
  38388. ret = wc_DecodeSakkeRsk(priv, data, sz, rsk);
  38389. if (ret != 0)
  38390. return WC_TEST_RET_ENC_EC(ret);
  38391. sz = sizeof(pubData);
  38392. ret = wc_EncodeSakkeRsk(priv, rsk, pubData, &sz, 0);
  38393. if (ret != 0)
  38394. return WC_TEST_RET_ENC_EC(ret);
  38395. if (sz != sizeof(pubData))
  38396. return WC_TEST_RET_ENC_NC;
  38397. ret = wc_DecodeSakkeRsk(priv, pubData, sz, rsk);
  38398. if (ret != 0)
  38399. return WC_TEST_RET_ENC_EC(ret);
  38400. wc_FreeSakkeKey(key);
  38401. return 0;
  38402. }
  38403. static wc_test_ret_t sakke_op_test(SakkeKey* priv, SakkeKey* pub, WC_RNG* rng,
  38404. ecc_point* rsk)
  38405. {
  38406. wc_test_ret_t ret;
  38407. byte ssv[16];
  38408. word16 ssvSz;
  38409. byte auth[257];
  38410. word16 authSz;
  38411. char mail[] = "test@wolfssl.com";
  38412. byte* id = (byte*)mail;
  38413. word32 idSz = (word32)XSTRLEN(mail);
  38414. byte pointI[256];
  38415. word32 sz;
  38416. ret = wc_GenerateSakkeSSV(pub, rng, NULL, &ssvSz);
  38417. if (ret != LENGTH_ONLY_E)
  38418. return WC_TEST_RET_ENC_EC(ret);
  38419. if (ssvSz != 16)
  38420. return WC_TEST_RET_ENC_NC;
  38421. ssvSz += 128;
  38422. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  38423. if (ret == 0)
  38424. return WC_TEST_RET_ENC_NC;
  38425. ssvSz -= 128;
  38426. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  38427. if (ret != 0)
  38428. return WC_TEST_RET_ENC_EC(ret);
  38429. if (ssvSz != 16)
  38430. return WC_TEST_RET_ENC_NC;
  38431. ret = wc_GetSakkeAuthSize(pub, &authSz);
  38432. if (ret != 0)
  38433. return WC_TEST_RET_ENC_EC(ret);
  38434. ret = wc_SetSakkeIdentity(pub, id, idSz);
  38435. if (ret != 0)
  38436. return WC_TEST_RET_ENC_EC(ret);
  38437. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  38438. NULL, &authSz);
  38439. if (ret != LENGTH_ONLY_E)
  38440. return WC_TEST_RET_ENC_EC(ret);
  38441. if (authSz != 257)
  38442. return WC_TEST_RET_ENC_NC;
  38443. authSz--;
  38444. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  38445. auth, &authSz);
  38446. if (ret == 0)
  38447. return WC_TEST_RET_ENC_NC;
  38448. authSz++;
  38449. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  38450. auth, &authSz);
  38451. if (ret != 0)
  38452. return WC_TEST_RET_ENC_EC(ret);
  38453. if (authSz != 257)
  38454. return WC_TEST_RET_ENC_NC;
  38455. ret = wc_GetSakkePointI(pub, NULL, &sz);
  38456. if (ret != LENGTH_ONLY_E)
  38457. return WC_TEST_RET_ENC_EC(ret);
  38458. if (sz != 256)
  38459. return WC_TEST_RET_ENC_NC;
  38460. ret = wc_GetSakkePointI(pub, pointI, &sz);
  38461. if (ret != 0)
  38462. return WC_TEST_RET_ENC_EC(ret);
  38463. if (sz != 256)
  38464. return WC_TEST_RET_ENC_NC;
  38465. /* Bogus identity - make it check and regenerate I. */
  38466. ret = wc_MakeSakkePointI(pub, ssv, ssvSz);
  38467. if (ret != 0)
  38468. return WC_TEST_RET_ENC_EC(ret);
  38469. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  38470. auth, &authSz);
  38471. if (ret != 0)
  38472. return WC_TEST_RET_ENC_EC(ret);
  38473. if (authSz != 257)
  38474. return WC_TEST_RET_ENC_NC;
  38475. ret = wc_SetSakkeRsk(priv, rsk, NULL, 0);
  38476. if (ret != 0)
  38477. return WC_TEST_RET_ENC_EC(ret);
  38478. ret = wc_SetSakkeIdentity(priv, id, idSz);
  38479. if (ret != 0)
  38480. return WC_TEST_RET_ENC_EC(ret);
  38481. authSz--;
  38482. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  38483. authSz);
  38484. if (ret == 0)
  38485. return WC_TEST_RET_ENC_NC;
  38486. authSz++;
  38487. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  38488. authSz);
  38489. if (ret != 0)
  38490. return WC_TEST_RET_ENC_EC(ret);
  38491. ssv[0] ^= 0x80;
  38492. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  38493. authSz);
  38494. if (ret != SAKKE_VERIFY_FAIL_E)
  38495. return WC_TEST_RET_ENC_EC(ret);
  38496. ssv[0] ^= 0x80;
  38497. /* Bogus identity - make it check and regenerate I. */
  38498. ret = wc_MakeSakkePointI(pub, ssv, idSz);
  38499. if (ret != 0)
  38500. return WC_TEST_RET_ENC_EC(ret);
  38501. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  38502. authSz);
  38503. if (ret != 0)
  38504. return WC_TEST_RET_ENC_EC(ret);
  38505. return 0;
  38506. }
  38507. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sakke_test(void)
  38508. {
  38509. wc_test_ret_t ret = 0;
  38510. WC_RNG rng;
  38511. int rng_inited = 0;
  38512. SakkeKey* priv = NULL;
  38513. SakkeKey* pub = NULL;
  38514. SakkeKey* key = NULL;
  38515. ecc_point* rsk = NULL;
  38516. WOLFSSL_ENTER("sakke_test");
  38517. priv = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  38518. DYNAMIC_TYPE_TMP_BUFFER);
  38519. if (priv == NULL)
  38520. ret = WC_TEST_RET_ENC_NC;
  38521. else
  38522. XMEMSET(priv, 0, sizeof(*priv));
  38523. if (ret == 0) {
  38524. pub = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  38525. DYNAMIC_TYPE_TMP_BUFFER);
  38526. if (pub == NULL)
  38527. ret = WC_TEST_RET_ENC_NC;
  38528. else
  38529. XMEMSET(pub, 0, sizeof(*pub));
  38530. }
  38531. if (ret == 0) {
  38532. key = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  38533. DYNAMIC_TYPE_TMP_BUFFER);
  38534. if (key == NULL)
  38535. ret = WC_TEST_RET_ENC_NC;
  38536. else
  38537. XMEMSET(key, 0, sizeof(*key));
  38538. }
  38539. if (ret == 0) {
  38540. #ifndef HAVE_FIPS
  38541. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  38542. #else
  38543. ret = wc_InitRng(&rng);
  38544. #endif
  38545. if (ret == 0)
  38546. rng_inited = 1;
  38547. else
  38548. ret = WC_TEST_RET_ENC_EC(ret);
  38549. }
  38550. if (ret == 0) {
  38551. rsk = wc_ecc_new_point();
  38552. if (rsk == NULL)
  38553. ret = WC_TEST_RET_ENC_NC;
  38554. }
  38555. if (ret == 0) {
  38556. ret = wc_InitSakkeKey(pub, HEAP_HINT, INVALID_DEVID);
  38557. if (ret != 0)
  38558. ret = WC_TEST_RET_ENC_EC(ret);
  38559. }
  38560. if (ret == 0) {
  38561. ret = wc_InitSakkeKey(priv, HEAP_HINT, INVALID_DEVID);
  38562. if (ret != 0)
  38563. ret = WC_TEST_RET_ENC_EC(ret);
  38564. }
  38565. if (ret == 0) {
  38566. ret = sakke_api_test(&rng, key, rsk);
  38567. }
  38568. if (ret == 0) {
  38569. ret = sakke_kat_derive_test(pub, rsk);
  38570. }
  38571. if (ret == 0) {
  38572. ret = sakke_kat_encapsulate_test(pub);
  38573. }
  38574. if (ret == 0) {
  38575. ret = sakke_make_key_test(priv, pub, key, &rng, rsk);
  38576. }
  38577. if (ret == 0) {
  38578. ret = sakke_op_test(priv, pub, &rng, rsk);
  38579. }
  38580. if (rsk != NULL) {
  38581. wc_ecc_forcezero_point(rsk);
  38582. wc_ecc_del_point(rsk);
  38583. }
  38584. if (rng_inited)
  38585. wc_FreeRng(&rng);
  38586. if (key != NULL)
  38587. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38588. if (pub != NULL) {
  38589. wc_FreeSakkeKey(pub);
  38590. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38591. }
  38592. if (priv != NULL) {
  38593. wc_FreeSakkeKey(priv);
  38594. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38595. }
  38596. return ret;
  38597. }
  38598. #endif /* WOLFCRYPT_HAVE_SAKKE */
  38599. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  38600. typedef struct CMAC_Test_Case {
  38601. int type;
  38602. int partial;
  38603. const byte* m;
  38604. word32 mSz;
  38605. const byte* k;
  38606. word32 kSz;
  38607. const byte* t;
  38608. word32 tSz;
  38609. } CMAC_Test_Case;
  38610. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cmac_test(void)
  38611. {
  38612. #ifdef WOLFSSL_AES_128
  38613. WOLFSSL_SMALL_STACK_STATIC const byte k128[] =
  38614. {
  38615. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  38616. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  38617. };
  38618. #define KLEN_128 (sizeof(k128))
  38619. #endif
  38620. #ifdef WOLFSSL_AES_192
  38621. WOLFSSL_SMALL_STACK_STATIC const byte k192[] =
  38622. {
  38623. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  38624. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  38625. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  38626. };
  38627. #define KLEN_192 (sizeof(k192))
  38628. #endif
  38629. #ifdef WOLFSSL_AES_256
  38630. WOLFSSL_SMALL_STACK_STATIC const byte k256[] =
  38631. {
  38632. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  38633. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  38634. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  38635. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  38636. };
  38637. #define KLEN_256 (sizeof(k256))
  38638. #endif
  38639. WOLFSSL_SMALL_STACK_STATIC const byte m[] =
  38640. {
  38641. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  38642. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  38643. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  38644. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  38645. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  38646. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  38647. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  38648. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  38649. };
  38650. #define MLEN_0 (0)
  38651. #define MLEN_128 (128/8)
  38652. #define MLEN_320 (320/8)
  38653. #define MLEN_319 (MLEN_320 - 1)
  38654. #define MLEN_512 (512/8)
  38655. #ifdef WOLFSSL_AES_128
  38656. WOLFSSL_SMALL_STACK_STATIC const byte t128_0[] =
  38657. {
  38658. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  38659. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  38660. };
  38661. WOLFSSL_SMALL_STACK_STATIC const byte t128_128[] =
  38662. {
  38663. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  38664. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  38665. };
  38666. WOLFSSL_SMALL_STACK_STATIC const byte t128_319[] =
  38667. {
  38668. 0x2c, 0x17, 0x84, 0x4c, 0x93, 0x1c, 0x07, 0x95,
  38669. 0x15, 0x92, 0x73, 0x0a, 0x34, 0xd0, 0xd9, 0xd2
  38670. };
  38671. WOLFSSL_SMALL_STACK_STATIC const byte t128_320[] =
  38672. {
  38673. 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  38674. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27
  38675. };
  38676. WOLFSSL_SMALL_STACK_STATIC const byte t128_512[] =
  38677. {
  38678. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  38679. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  38680. };
  38681. #endif
  38682. #ifdef WOLFSSL_AES_192
  38683. WOLFSSL_SMALL_STACK_STATIC const byte t192_0[] =
  38684. {
  38685. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  38686. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  38687. };
  38688. WOLFSSL_SMALL_STACK_STATIC const byte t192_128[] =
  38689. {
  38690. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  38691. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  38692. };
  38693. WOLFSSL_SMALL_STACK_STATIC const byte t192_320[] =
  38694. {
  38695. 0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad,
  38696. 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e
  38697. };
  38698. WOLFSSL_SMALL_STACK_STATIC const byte t192_512[] =
  38699. {
  38700. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  38701. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  38702. };
  38703. #endif
  38704. #ifdef WOLFSSL_AES_256
  38705. WOLFSSL_SMALL_STACK_STATIC const byte t256_0[] =
  38706. {
  38707. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  38708. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  38709. };
  38710. WOLFSSL_SMALL_STACK_STATIC const byte t256_128[] =
  38711. {
  38712. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  38713. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  38714. };
  38715. WOLFSSL_SMALL_STACK_STATIC const byte t256_320[] =
  38716. {
  38717. 0xaa, 0xf3, 0xd8, 0xf1, 0xde, 0x56, 0x40, 0xc2,
  38718. 0x32, 0xf5, 0xb1, 0x69, 0xb9, 0xc9, 0x11, 0xe6
  38719. };
  38720. WOLFSSL_SMALL_STACK_STATIC const byte t256_512[] =
  38721. {
  38722. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  38723. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  38724. };
  38725. #endif
  38726. const CMAC_Test_Case testCases[] =
  38727. {
  38728. #ifdef WOLFSSL_AES_128
  38729. {WC_CMAC_AES, 0, m, MLEN_0, k128, KLEN_128, t128_0, AES_BLOCK_SIZE},
  38730. {WC_CMAC_AES, 0, m, MLEN_128, k128, KLEN_128, t128_128, AES_BLOCK_SIZE},
  38731. {WC_CMAC_AES, 0, m, MLEN_320, k128, KLEN_128, t128_320, AES_BLOCK_SIZE},
  38732. {WC_CMAC_AES, 0, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  38733. {WC_CMAC_AES, 5, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  38734. #endif
  38735. #ifdef WOLFSSL_AES_192
  38736. {WC_CMAC_AES, 0, m, MLEN_0, k192, KLEN_192, t192_0, AES_BLOCK_SIZE},
  38737. {WC_CMAC_AES, 0, m, MLEN_128, k192, KLEN_192, t192_128, AES_BLOCK_SIZE},
  38738. {WC_CMAC_AES, 0, m, MLEN_320, k192, KLEN_192, t192_320, AES_BLOCK_SIZE},
  38739. {WC_CMAC_AES, 0, m, MLEN_512, k192, KLEN_192, t192_512, AES_BLOCK_SIZE},
  38740. #endif
  38741. #ifdef WOLFSSL_AES_256
  38742. {WC_CMAC_AES, 0, m, MLEN_0, k256, KLEN_256, t256_0, AES_BLOCK_SIZE},
  38743. {WC_CMAC_AES, 0, m, MLEN_128, k256, KLEN_256, t256_128, AES_BLOCK_SIZE},
  38744. {WC_CMAC_AES, 0, m, MLEN_320, k256, KLEN_256, t256_320, AES_BLOCK_SIZE},
  38745. {WC_CMAC_AES, 0, m, MLEN_512, k256, KLEN_256, t256_512, AES_BLOCK_SIZE},
  38746. #endif
  38747. #ifdef WOLFSSL_AES_128
  38748. {WC_CMAC_AES, 0, m, MLEN_319, k128, KLEN_128, t128_319, AES_BLOCK_SIZE}
  38749. #endif
  38750. };
  38751. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38752. Cmac *cmac;
  38753. #else
  38754. Cmac cmac[1];
  38755. #endif
  38756. byte tag[AES_BLOCK_SIZE];
  38757. const CMAC_Test_Case* tc;
  38758. word32 i, tagSz;
  38759. wc_test_ret_t ret;
  38760. WOLFSSL_ENTER("cmac_test");
  38761. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38762. if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC)) == NULL)
  38763. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  38764. #endif
  38765. for (i = 0, tc = testCases;
  38766. i < sizeof(testCases)/sizeof(CMAC_Test_Case);
  38767. i++, tc++) {
  38768. XMEMSET(tag, 0, sizeof(tag));
  38769. tagSz = AES_BLOCK_SIZE;
  38770. #if !defined(HAVE_FIPS) || \
  38771. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)
  38772. ret = wc_InitCmac_ex(cmac, tc->k, tc->kSz, tc->type, NULL, HEAP_HINT, devId);
  38773. #else
  38774. ret = wc_InitCmac(cmac, tc->k, tc->kSz, tc->type, NULL);
  38775. #endif
  38776. if (ret != 0)
  38777. {
  38778. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38779. }
  38780. if (tc->partial) {
  38781. ret = wc_CmacUpdate(cmac, tc->m,
  38782. tc->mSz/2 - tc->partial);
  38783. if (ret != 0)
  38784. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38785. ret = wc_CmacUpdate(cmac, tc->m + tc->mSz/2 - tc->partial,
  38786. tc->mSz/2 + tc->partial);
  38787. if (ret != 0)
  38788. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38789. }
  38790. else {
  38791. ret = wc_CmacUpdate(cmac, tc->m, tc->mSz);
  38792. if (ret != 0)
  38793. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38794. }
  38795. ret = wc_CmacFinal(cmac, tag, &tagSz);
  38796. if (ret != 0)
  38797. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38798. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  38799. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  38800. XMEMSET(tag, 0, sizeof(tag));
  38801. tagSz = sizeof(tag);
  38802. #if !defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)
  38803. ret = wc_AesCmacGenerate_ex(cmac, tag, &tagSz, tc->m, tc->mSz,
  38804. tc->k, tc->kSz, NULL, devId);
  38805. #else
  38806. ret = wc_AesCmacGenerate(tag, &tagSz, tc->m, tc->mSz,
  38807. tc->k, tc->kSz);
  38808. #endif
  38809. if (ret != 0)
  38810. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38811. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  38812. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  38813. #if !defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)
  38814. ret = wc_AesCmacVerify_ex(cmac, tc->t, tc->tSz, tc->m, tc->mSz,
  38815. tc->k, tc->kSz, HEAP_HINT, devId);
  38816. #else
  38817. ret = wc_AesCmacVerify(tc->t, tc->tSz, tc->m, tc->mSz,
  38818. tc->k, tc->kSz);
  38819. #endif
  38820. if (ret != 0)
  38821. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38822. #if !defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)
  38823. /* Test that keyless generate with init is the same */
  38824. XMEMSET(tag, 0, sizeof(tag));
  38825. tagSz = sizeof(tag);
  38826. ret = wc_InitCmac_ex(cmac, tc->k, tc->kSz, tc->type, NULL, HEAP_HINT, devId);
  38827. if (ret != 0) {
  38828. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38829. }
  38830. ret = wc_AesCmacGenerate_ex(cmac, tag, &tagSz, tc->m, tc->mSz,
  38831. NULL, 0, HEAP_HINT, devId);
  38832. if (ret != 0) {
  38833. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38834. }
  38835. #endif
  38836. }
  38837. ret = 0;
  38838. out:
  38839. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38840. if (cmac)
  38841. XFREE(cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC);
  38842. #endif
  38843. return ret;
  38844. }
  38845. #endif /* NO_AES && WOLFSSL_CMAC */
  38846. #if defined(WOLFSSL_SIPHASH)
  38847. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  38848. /* Test vectors from:
  38849. * https://github.com/veorq/SipHash/blob/master/vectors.h
  38850. */
  38851. static const unsigned char siphash_key[SIPHASH_KEY_SIZE] = {
  38852. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  38853. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  38854. };
  38855. static const unsigned char siphash_msg[64] = {
  38856. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  38857. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  38858. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  38859. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  38860. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  38861. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  38862. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  38863. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
  38864. };
  38865. static const unsigned char siphash_r8[64][SIPHASH_MAC_SIZE_8] = {
  38866. { 0x31, 0x0e, 0x0e, 0xdd, 0x47, 0xdb, 0x6f, 0x72, },
  38867. { 0xfd, 0x67, 0xdc, 0x93, 0xc5, 0x39, 0xf8, 0x74, },
  38868. { 0x5a, 0x4f, 0xa9, 0xd9, 0x09, 0x80, 0x6c, 0x0d, },
  38869. { 0x2d, 0x7e, 0xfb, 0xd7, 0x96, 0x66, 0x67, 0x85, },
  38870. { 0xb7, 0x87, 0x71, 0x27, 0xe0, 0x94, 0x27, 0xcf, },
  38871. { 0x8d, 0xa6, 0x99, 0xcd, 0x64, 0x55, 0x76, 0x18, },
  38872. { 0xce, 0xe3, 0xfe, 0x58, 0x6e, 0x46, 0xc9, 0xcb, },
  38873. { 0x37, 0xd1, 0x01, 0x8b, 0xf5, 0x00, 0x02, 0xab, },
  38874. { 0x62, 0x24, 0x93, 0x9a, 0x79, 0xf5, 0xf5, 0x93, },
  38875. { 0xb0, 0xe4, 0xa9, 0x0b, 0xdf, 0x82, 0x00, 0x9e, },
  38876. { 0xf3, 0xb9, 0xdd, 0x94, 0xc5, 0xbb, 0x5d, 0x7a, },
  38877. { 0xa7, 0xad, 0x6b, 0x22, 0x46, 0x2f, 0xb3, 0xf4, },
  38878. { 0xfb, 0xe5, 0x0e, 0x86, 0xbc, 0x8f, 0x1e, 0x75, },
  38879. { 0x90, 0x3d, 0x84, 0xc0, 0x27, 0x56, 0xea, 0x14, },
  38880. { 0xee, 0xf2, 0x7a, 0x8e, 0x90, 0xca, 0x23, 0xf7, },
  38881. { 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1, },
  38882. { 0xdb, 0x9b, 0xc2, 0x57, 0x7f, 0xcc, 0x2a, 0x3f, },
  38883. { 0x94, 0x47, 0xbe, 0x2c, 0xf5, 0xe9, 0x9a, 0x69, },
  38884. { 0x9c, 0xd3, 0x8d, 0x96, 0xf0, 0xb3, 0xc1, 0x4b, },
  38885. { 0xbd, 0x61, 0x79, 0xa7, 0x1d, 0xc9, 0x6d, 0xbb, },
  38886. { 0x98, 0xee, 0xa2, 0x1a, 0xf2, 0x5c, 0xd6, 0xbe, },
  38887. { 0xc7, 0x67, 0x3b, 0x2e, 0xb0, 0xcb, 0xf2, 0xd0, },
  38888. { 0x88, 0x3e, 0xa3, 0xe3, 0x95, 0x67, 0x53, 0x93, },
  38889. { 0xc8, 0xce, 0x5c, 0xcd, 0x8c, 0x03, 0x0c, 0xa8, },
  38890. { 0x94, 0xaf, 0x49, 0xf6, 0xc6, 0x50, 0xad, 0xb8, },
  38891. { 0xea, 0xb8, 0x85, 0x8a, 0xde, 0x92, 0xe1, 0xbc, },
  38892. { 0xf3, 0x15, 0xbb, 0x5b, 0xb8, 0x35, 0xd8, 0x17, },
  38893. { 0xad, 0xcf, 0x6b, 0x07, 0x63, 0x61, 0x2e, 0x2f, },
  38894. { 0xa5, 0xc9, 0x1d, 0xa7, 0xac, 0xaa, 0x4d, 0xde, },
  38895. { 0x71, 0x65, 0x95, 0x87, 0x66, 0x50, 0xa2, 0xa6, },
  38896. { 0x28, 0xef, 0x49, 0x5c, 0x53, 0xa3, 0x87, 0xad, },
  38897. { 0x42, 0xc3, 0x41, 0xd8, 0xfa, 0x92, 0xd8, 0x32, },
  38898. { 0xce, 0x7c, 0xf2, 0x72, 0x2f, 0x51, 0x27, 0x71, },
  38899. { 0xe3, 0x78, 0x59, 0xf9, 0x46, 0x23, 0xf3, 0xa7, },
  38900. { 0x38, 0x12, 0x05, 0xbb, 0x1a, 0xb0, 0xe0, 0x12, },
  38901. { 0xae, 0x97, 0xa1, 0x0f, 0xd4, 0x34, 0xe0, 0x15, },
  38902. { 0xb4, 0xa3, 0x15, 0x08, 0xbe, 0xff, 0x4d, 0x31, },
  38903. { 0x81, 0x39, 0x62, 0x29, 0xf0, 0x90, 0x79, 0x02, },
  38904. { 0x4d, 0x0c, 0xf4, 0x9e, 0xe5, 0xd4, 0xdc, 0xca, },
  38905. { 0x5c, 0x73, 0x33, 0x6a, 0x76, 0xd8, 0xbf, 0x9a, },
  38906. { 0xd0, 0xa7, 0x04, 0x53, 0x6b, 0xa9, 0x3e, 0x0e, },
  38907. { 0x92, 0x59, 0x58, 0xfc, 0xd6, 0x42, 0x0c, 0xad, },
  38908. { 0xa9, 0x15, 0xc2, 0x9b, 0xc8, 0x06, 0x73, 0x18, },
  38909. { 0x95, 0x2b, 0x79, 0xf3, 0xbc, 0x0a, 0xa6, 0xd4, },
  38910. { 0xf2, 0x1d, 0xf2, 0xe4, 0x1d, 0x45, 0x35, 0xf9, },
  38911. { 0x87, 0x57, 0x75, 0x19, 0x04, 0x8f, 0x53, 0xa9, },
  38912. { 0x10, 0xa5, 0x6c, 0xf5, 0xdf, 0xcd, 0x9a, 0xdb, },
  38913. { 0xeb, 0x75, 0x09, 0x5c, 0xcd, 0x98, 0x6c, 0xd0, },
  38914. { 0x51, 0xa9, 0xcb, 0x9e, 0xcb, 0xa3, 0x12, 0xe6, },
  38915. { 0x96, 0xaf, 0xad, 0xfc, 0x2c, 0xe6, 0x66, 0xc7, },
  38916. { 0x72, 0xfe, 0x52, 0x97, 0x5a, 0x43, 0x64, 0xee, },
  38917. { 0x5a, 0x16, 0x45, 0xb2, 0x76, 0xd5, 0x92, 0xa1, },
  38918. { 0xb2, 0x74, 0xcb, 0x8e, 0xbf, 0x87, 0x87, 0x0a, },
  38919. { 0x6f, 0x9b, 0xb4, 0x20, 0x3d, 0xe7, 0xb3, 0x81, },
  38920. { 0xea, 0xec, 0xb2, 0xa3, 0x0b, 0x22, 0xa8, 0x7f, },
  38921. { 0x99, 0x24, 0xa4, 0x3c, 0xc1, 0x31, 0x57, 0x24, },
  38922. { 0xbd, 0x83, 0x8d, 0x3a, 0xaf, 0xbf, 0x8d, 0xb7, },
  38923. { 0x0b, 0x1a, 0x2a, 0x32, 0x65, 0xd5, 0x1a, 0xea, },
  38924. { 0x13, 0x50, 0x79, 0xa3, 0x23, 0x1c, 0xe6, 0x60, },
  38925. { 0x93, 0x2b, 0x28, 0x46, 0xe4, 0xd7, 0x06, 0x66, },
  38926. { 0xe1, 0x91, 0x5f, 0x5c, 0xb1, 0xec, 0xa4, 0x6c, },
  38927. { 0xf3, 0x25, 0x96, 0x5c, 0xa1, 0x6d, 0x62, 0x9f, },
  38928. { 0x57, 0x5f, 0xf2, 0x8e, 0x60, 0x38, 0x1b, 0xe5, },
  38929. { 0x72, 0x45, 0x06, 0xeb, 0x4c, 0x32, 0x8a, 0x95, },
  38930. };
  38931. static const unsigned char siphash_r16[64][SIPHASH_MAC_SIZE_16] = {
  38932. { 0xa3, 0x81, 0x7f, 0x04, 0xba, 0x25, 0xa8, 0xe6,
  38933. 0x6d, 0xf6, 0x72, 0x14, 0xc7, 0x55, 0x02, 0x93, },
  38934. { 0xda, 0x87, 0xc1, 0xd8, 0x6b, 0x99, 0xaf, 0x44,
  38935. 0x34, 0x76, 0x59, 0x11, 0x9b, 0x22, 0xfc, 0x45, },
  38936. { 0x81, 0x77, 0x22, 0x8d, 0xa4, 0xa4, 0x5d, 0xc7,
  38937. 0xfc, 0xa3, 0x8b, 0xde, 0xf6, 0x0a, 0xff, 0xe4, },
  38938. { 0x9c, 0x70, 0xb6, 0x0c, 0x52, 0x67, 0xa9, 0x4e,
  38939. 0x5f, 0x33, 0xb6, 0xb0, 0x29, 0x85, 0xed, 0x51, },
  38940. { 0xf8, 0x81, 0x64, 0xc1, 0x2d, 0x9c, 0x8f, 0xaf,
  38941. 0x7d, 0x0f, 0x6e, 0x7c, 0x7b, 0xcd, 0x55, 0x79, },
  38942. { 0x13, 0x68, 0x87, 0x59, 0x80, 0x77, 0x6f, 0x88,
  38943. 0x54, 0x52, 0x7a, 0x07, 0x69, 0x0e, 0x96, 0x27, },
  38944. { 0x14, 0xee, 0xca, 0x33, 0x8b, 0x20, 0x86, 0x13,
  38945. 0x48, 0x5e, 0xa0, 0x30, 0x8f, 0xd7, 0xa1, 0x5e, },
  38946. { 0xa1, 0xf1, 0xeb, 0xbe, 0xd8, 0xdb, 0xc1, 0x53,
  38947. 0xc0, 0xb8, 0x4a, 0xa6, 0x1f, 0xf0, 0x82, 0x39, },
  38948. { 0x3b, 0x62, 0xa9, 0xba, 0x62, 0x58, 0xf5, 0x61,
  38949. 0x0f, 0x83, 0xe2, 0x64, 0xf3, 0x14, 0x97, 0xb4, },
  38950. { 0x26, 0x44, 0x99, 0x06, 0x0a, 0xd9, 0xba, 0xab,
  38951. 0xc4, 0x7f, 0x8b, 0x02, 0xbb, 0x6d, 0x71, 0xed, },
  38952. { 0x00, 0x11, 0x0d, 0xc3, 0x78, 0x14, 0x69, 0x56,
  38953. 0xc9, 0x54, 0x47, 0xd3, 0xf3, 0xd0, 0xfb, 0xba, },
  38954. { 0x01, 0x51, 0xc5, 0x68, 0x38, 0x6b, 0x66, 0x77,
  38955. 0xa2, 0xb4, 0xdc, 0x6f, 0x81, 0xe5, 0xdc, 0x18, },
  38956. { 0xd6, 0x26, 0xb2, 0x66, 0x90, 0x5e, 0xf3, 0x58,
  38957. 0x82, 0x63, 0x4d, 0xf6, 0x85, 0x32, 0xc1, 0x25, },
  38958. { 0x98, 0x69, 0xe2, 0x47, 0xe9, 0xc0, 0x8b, 0x10,
  38959. 0xd0, 0x29, 0x93, 0x4f, 0xc4, 0xb9, 0x52, 0xf7, },
  38960. { 0x31, 0xfc, 0xef, 0xac, 0x66, 0xd7, 0xde, 0x9c,
  38961. 0x7e, 0xc7, 0x48, 0x5f, 0xe4, 0x49, 0x49, 0x02, },
  38962. { 0x54, 0x93, 0xe9, 0x99, 0x33, 0xb0, 0xa8, 0x11,
  38963. 0x7e, 0x08, 0xec, 0x0f, 0x97, 0xcf, 0xc3, 0xd9, },
  38964. { 0x6e, 0xe2, 0xa4, 0xca, 0x67, 0xb0, 0x54, 0xbb,
  38965. 0xfd, 0x33, 0x15, 0xbf, 0x85, 0x23, 0x05, 0x77, },
  38966. { 0x47, 0x3d, 0x06, 0xe8, 0x73, 0x8d, 0xb8, 0x98,
  38967. 0x54, 0xc0, 0x66, 0xc4, 0x7a, 0xe4, 0x77, 0x40, },
  38968. { 0xa4, 0x26, 0xe5, 0xe4, 0x23, 0xbf, 0x48, 0x85,
  38969. 0x29, 0x4d, 0xa4, 0x81, 0xfe, 0xae, 0xf7, 0x23, },
  38970. { 0x78, 0x01, 0x77, 0x31, 0xcf, 0x65, 0xfa, 0xb0,
  38971. 0x74, 0xd5, 0x20, 0x89, 0x52, 0x51, 0x2e, 0xb1, },
  38972. { 0x9e, 0x25, 0xfc, 0x83, 0x3f, 0x22, 0x90, 0x73,
  38973. 0x3e, 0x93, 0x44, 0xa5, 0xe8, 0x38, 0x39, 0xeb, },
  38974. { 0x56, 0x8e, 0x49, 0x5a, 0xbe, 0x52, 0x5a, 0x21,
  38975. 0x8a, 0x22, 0x14, 0xcd, 0x3e, 0x07, 0x1d, 0x12, },
  38976. { 0x4a, 0x29, 0xb5, 0x45, 0x52, 0xd1, 0x6b, 0x9a,
  38977. 0x46, 0x9c, 0x10, 0x52, 0x8e, 0xff, 0x0a, 0xae, },
  38978. { 0xc9, 0xd1, 0x84, 0xdd, 0xd5, 0xa9, 0xf5, 0xe0,
  38979. 0xcf, 0x8c, 0xe2, 0x9a, 0x9a, 0xbf, 0x69, 0x1c, },
  38980. { 0x2d, 0xb4, 0x79, 0xae, 0x78, 0xbd, 0x50, 0xd8,
  38981. 0x88, 0x2a, 0x8a, 0x17, 0x8a, 0x61, 0x32, 0xad, },
  38982. { 0x8e, 0xce, 0x5f, 0x04, 0x2d, 0x5e, 0x44, 0x7b,
  38983. 0x50, 0x51, 0xb9, 0xea, 0xcb, 0x8d, 0x8f, 0x6f, },
  38984. { 0x9c, 0x0b, 0x53, 0xb4, 0xb3, 0xc3, 0x07, 0xe8,
  38985. 0x7e, 0xae, 0xe0, 0x86, 0x78, 0x14, 0x1f, 0x66, },
  38986. { 0xab, 0xf2, 0x48, 0xaf, 0x69, 0xa6, 0xea, 0xe4,
  38987. 0xbf, 0xd3, 0xeb, 0x2f, 0x12, 0x9e, 0xeb, 0x94, },
  38988. { 0x06, 0x64, 0xda, 0x16, 0x68, 0x57, 0x4b, 0x88,
  38989. 0xb9, 0x35, 0xf3, 0x02, 0x73, 0x58, 0xae, 0xf4, },
  38990. { 0xaa, 0x4b, 0x9d, 0xc4, 0xbf, 0x33, 0x7d, 0xe9,
  38991. 0x0c, 0xd4, 0xfd, 0x3c, 0x46, 0x7c, 0x6a, 0xb7, },
  38992. { 0xea, 0x5c, 0x7f, 0x47, 0x1f, 0xaf, 0x6b, 0xde,
  38993. 0x2b, 0x1a, 0xd7, 0xd4, 0x68, 0x6d, 0x22, 0x87, },
  38994. { 0x29, 0x39, 0xb0, 0x18, 0x32, 0x23, 0xfa, 0xfc,
  38995. 0x17, 0x23, 0xde, 0x4f, 0x52, 0xc4, 0x3d, 0x35, },
  38996. { 0x7c, 0x39, 0x56, 0xca, 0x5e, 0xea, 0xfc, 0x3e,
  38997. 0x36, 0x3e, 0x9d, 0x55, 0x65, 0x46, 0xeb, 0x68, },
  38998. { 0x77, 0xc6, 0x07, 0x71, 0x46, 0xf0, 0x1c, 0x32,
  38999. 0xb6, 0xb6, 0x9d, 0x5f, 0x4e, 0xa9, 0xff, 0xcf, },
  39000. { 0x37, 0xa6, 0x98, 0x6c, 0xb8, 0x84, 0x7e, 0xdf,
  39001. 0x09, 0x25, 0xf0, 0xf1, 0x30, 0x9b, 0x54, 0xde, },
  39002. { 0xa7, 0x05, 0xf0, 0xe6, 0x9d, 0xa9, 0xa8, 0xf9,
  39003. 0x07, 0x24, 0x1a, 0x2e, 0x92, 0x3c, 0x8c, 0xc8, },
  39004. { 0x3d, 0xc4, 0x7d, 0x1f, 0x29, 0xc4, 0x48, 0x46,
  39005. 0x1e, 0x9e, 0x76, 0xed, 0x90, 0x4f, 0x67, 0x11, },
  39006. { 0x0d, 0x62, 0xbf, 0x01, 0xe6, 0xfc, 0x0e, 0x1a,
  39007. 0x0d, 0x3c, 0x47, 0x51, 0xc5, 0xd3, 0x69, 0x2b, },
  39008. { 0x8c, 0x03, 0x46, 0x8b, 0xca, 0x7c, 0x66, 0x9e,
  39009. 0xe4, 0xfd, 0x5e, 0x08, 0x4b, 0xbe, 0xe7, 0xb5, },
  39010. { 0x52, 0x8a, 0x5b, 0xb9, 0x3b, 0xaf, 0x2c, 0x9c,
  39011. 0x44, 0x73, 0xcc, 0xe5, 0xd0, 0xd2, 0x2b, 0xd9, },
  39012. { 0xdf, 0x6a, 0x30, 0x1e, 0x95, 0xc9, 0x5d, 0xad,
  39013. 0x97, 0xae, 0x0c, 0xc8, 0xc6, 0x91, 0x3b, 0xd8, },
  39014. { 0x80, 0x11, 0x89, 0x90, 0x2c, 0x85, 0x7f, 0x39,
  39015. 0xe7, 0x35, 0x91, 0x28, 0x5e, 0x70, 0xb6, 0xdb, },
  39016. { 0xe6, 0x17, 0x34, 0x6a, 0xc9, 0xc2, 0x31, 0xbb,
  39017. 0x36, 0x50, 0xae, 0x34, 0xcc, 0xca, 0x0c, 0x5b, },
  39018. { 0x27, 0xd9, 0x34, 0x37, 0xef, 0xb7, 0x21, 0xaa,
  39019. 0x40, 0x18, 0x21, 0xdc, 0xec, 0x5a, 0xdf, 0x89, },
  39020. { 0x89, 0x23, 0x7d, 0x9d, 0xed, 0x9c, 0x5e, 0x78,
  39021. 0xd8, 0xb1, 0xc9, 0xb1, 0x66, 0xcc, 0x73, 0x42, },
  39022. { 0x4a, 0x6d, 0x80, 0x91, 0xbf, 0x5e, 0x7d, 0x65,
  39023. 0x11, 0x89, 0xfa, 0x94, 0xa2, 0x50, 0xb1, 0x4c, },
  39024. { 0x0e, 0x33, 0xf9, 0x60, 0x55, 0xe7, 0xae, 0x89,
  39025. 0x3f, 0xfc, 0x0e, 0x3d, 0xcf, 0x49, 0x29, 0x02, },
  39026. { 0xe6, 0x1c, 0x43, 0x2b, 0x72, 0x0b, 0x19, 0xd1,
  39027. 0x8e, 0xc8, 0xd8, 0x4b, 0xdc, 0x63, 0x15, 0x1b, },
  39028. { 0xf7, 0xe5, 0xae, 0xf5, 0x49, 0xf7, 0x82, 0xcf,
  39029. 0x37, 0x90, 0x55, 0xa6, 0x08, 0x26, 0x9b, 0x16, },
  39030. { 0x43, 0x8d, 0x03, 0x0f, 0xd0, 0xb7, 0xa5, 0x4f,
  39031. 0xa8, 0x37, 0xf2, 0xad, 0x20, 0x1a, 0x64, 0x03, },
  39032. { 0xa5, 0x90, 0xd3, 0xee, 0x4f, 0xbf, 0x04, 0xe3,
  39033. 0x24, 0x7e, 0x0d, 0x27, 0xf2, 0x86, 0x42, 0x3f, },
  39034. { 0x5f, 0xe2, 0xc1, 0xa1, 0x72, 0xfe, 0x93, 0xc4,
  39035. 0xb1, 0x5c, 0xd3, 0x7c, 0xae, 0xf9, 0xf5, 0x38, },
  39036. { 0x2c, 0x97, 0x32, 0x5c, 0xbd, 0x06, 0xb3, 0x6e,
  39037. 0xb2, 0x13, 0x3d, 0xd0, 0x8b, 0x3a, 0x01, 0x7c, },
  39038. { 0x92, 0xc8, 0x14, 0x22, 0x7a, 0x6b, 0xca, 0x94,
  39039. 0x9f, 0xf0, 0x65, 0x9f, 0x00, 0x2a, 0xd3, 0x9e, },
  39040. { 0xdc, 0xe8, 0x50, 0x11, 0x0b, 0xd8, 0x32, 0x8c,
  39041. 0xfb, 0xd5, 0x08, 0x41, 0xd6, 0x91, 0x1d, 0x87, },
  39042. { 0x67, 0xf1, 0x49, 0x84, 0xc7, 0xda, 0x79, 0x12,
  39043. 0x48, 0xe3, 0x2b, 0xb5, 0x92, 0x25, 0x83, 0xda, },
  39044. { 0x19, 0x38, 0xf2, 0xcf, 0x72, 0xd5, 0x4e, 0xe9,
  39045. 0x7e, 0x94, 0x16, 0x6f, 0xa9, 0x1d, 0x2a, 0x36, },
  39046. { 0x74, 0x48, 0x1e, 0x96, 0x46, 0xed, 0x49, 0xfe,
  39047. 0x0f, 0x62, 0x24, 0x30, 0x16, 0x04, 0x69, 0x8e, },
  39048. { 0x57, 0xfc, 0xa5, 0xde, 0x98, 0xa9, 0xd6, 0xd8,
  39049. 0x00, 0x64, 0x38, 0xd0, 0x58, 0x3d, 0x8a, 0x1d, },
  39050. { 0x9f, 0xec, 0xde, 0x1c, 0xef, 0xdc, 0x1c, 0xbe,
  39051. 0xd4, 0x76, 0x36, 0x74, 0xd9, 0x57, 0x53, 0x59, },
  39052. { 0xe3, 0x04, 0x0c, 0x00, 0xeb, 0x28, 0xf1, 0x53,
  39053. 0x66, 0xca, 0x73, 0xcb, 0xd8, 0x72, 0xe7, 0x40, },
  39054. { 0x76, 0x97, 0x00, 0x9a, 0x6a, 0x83, 0x1d, 0xfe,
  39055. 0xcc, 0xa9, 0x1c, 0x59, 0x93, 0x67, 0x0f, 0x7a, },
  39056. { 0x58, 0x53, 0x54, 0x23, 0x21, 0xf5, 0x67, 0xa0,
  39057. 0x05, 0xd5, 0x47, 0xa4, 0xf0, 0x47, 0x59, 0xbd, },
  39058. { 0x51, 0x50, 0xd1, 0x77, 0x2f, 0x50, 0x83, 0x4a,
  39059. 0x50, 0x3e, 0x06, 0x9a, 0x97, 0x3f, 0xbd, 0x7c, },
  39060. };
  39061. #endif
  39062. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t siphash_test(void)
  39063. {
  39064. wc_test_ret_t ret = 0;
  39065. int i;
  39066. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  39067. unsigned char res[SIPHASH_MAC_SIZE_16];
  39068. unsigned char tmp[SIPHASH_MAC_SIZE_8];
  39069. SipHash siphash;
  39070. WOLFSSL_ENTER("siphash_test (1)");
  39071. for (i = 0; i < 64; i++) {
  39072. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  39073. if (ret != 0)
  39074. return WC_TEST_RET_ENC_I(i);
  39075. ret = wc_SipHashUpdate(&siphash, siphash_msg, (word32)i);
  39076. if (ret != 0)
  39077. return WC_TEST_RET_ENC_I(i);
  39078. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  39079. if (ret != 0)
  39080. return WC_TEST_RET_ENC_I(i);
  39081. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  39082. return WC_TEST_RET_ENC_I(i);
  39083. ret = wc_SipHash(siphash_key, siphash_msg, (word32)i, res, SIPHASH_MAC_SIZE_8);
  39084. if (ret != 0)
  39085. return WC_TEST_RET_ENC_I(i);
  39086. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  39087. return WC_TEST_RET_ENC_I(i);
  39088. }
  39089. for (i = 0; i < 64; i++) {
  39090. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_16);
  39091. if (ret != 0)
  39092. return WC_TEST_RET_ENC_I(i);
  39093. ret = wc_SipHashUpdate(&siphash, siphash_msg, (word32)i);
  39094. if (ret != 0)
  39095. return WC_TEST_RET_ENC_I(i);
  39096. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  39097. if (ret != 0)
  39098. return WC_TEST_RET_ENC_I(i);
  39099. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  39100. return WC_TEST_RET_ENC_I(i);
  39101. ret = wc_SipHash(siphash_key, siphash_msg, (word32)i, res, SIPHASH_MAC_SIZE_16);
  39102. if (ret != 0)
  39103. return WC_TEST_RET_ENC_I(i);
  39104. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  39105. return WC_TEST_RET_ENC_I(i);
  39106. }
  39107. #else
  39108. WOLFSSL_ENTER("siphash_test (1)");
  39109. #endif
  39110. /* Testing bad parameters. */
  39111. ret = wc_InitSipHash(NULL, NULL, SIPHASH_MAC_SIZE_8);
  39112. if (ret != BAD_FUNC_ARG)
  39113. return WC_TEST_RET_ENC_EC(ret);
  39114. ret = wc_InitSipHash(NULL, siphash_key, SIPHASH_MAC_SIZE_8);
  39115. if (ret != BAD_FUNC_ARG)
  39116. return WC_TEST_RET_ENC_EC(ret);
  39117. ret = wc_InitSipHash(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  39118. if (ret != BAD_FUNC_ARG)
  39119. return WC_TEST_RET_ENC_EC(ret);
  39120. ret = wc_InitSipHash(&siphash, siphash_key, 7);
  39121. if (ret != BAD_FUNC_ARG)
  39122. return WC_TEST_RET_ENC_EC(ret);
  39123. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  39124. if (ret != 0)
  39125. return WC_TEST_RET_ENC_EC(ret);
  39126. ret = wc_SipHashUpdate(NULL, NULL, 0);
  39127. if (ret != BAD_FUNC_ARG)
  39128. return WC_TEST_RET_ENC_EC(ret);
  39129. ret = wc_SipHashUpdate(&siphash, NULL, 1);
  39130. if (ret != BAD_FUNC_ARG)
  39131. return WC_TEST_RET_ENC_EC(ret);
  39132. ret = wc_SipHashFinal(NULL, NULL, SIPHASH_MAC_SIZE_8);
  39133. if (ret != BAD_FUNC_ARG)
  39134. return WC_TEST_RET_ENC_EC(ret);
  39135. ret = wc_SipHashFinal(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  39136. if (ret != BAD_FUNC_ARG)
  39137. return WC_TEST_RET_ENC_EC(ret);
  39138. ret = wc_SipHashFinal(NULL, res, SIPHASH_MAC_SIZE_8);
  39139. if (ret != BAD_FUNC_ARG)
  39140. return WC_TEST_RET_ENC_EC(ret);
  39141. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  39142. if (ret != BAD_FUNC_ARG)
  39143. return WC_TEST_RET_ENC_EC(ret);
  39144. ret = wc_SipHash(NULL, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  39145. if (ret != BAD_FUNC_ARG)
  39146. return WC_TEST_RET_ENC_EC(ret);
  39147. ret = wc_SipHash(siphash_key, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  39148. if (ret != BAD_FUNC_ARG)
  39149. return WC_TEST_RET_ENC_EC(ret);
  39150. ret = wc_SipHash(NULL, NULL, 0, res, SIPHASH_MAC_SIZE_16);
  39151. if (ret != BAD_FUNC_ARG)
  39152. return WC_TEST_RET_ENC_EC(ret);
  39153. ret = wc_SipHash(siphash_key, NULL, 0, res, 15);
  39154. if (ret != BAD_FUNC_ARG)
  39155. return WC_TEST_RET_ENC_EC(ret);
  39156. ret = wc_SipHash(siphash_key, NULL, 1, res, SIPHASH_MAC_SIZE_16);
  39157. if (ret != BAD_FUNC_ARG)
  39158. return WC_TEST_RET_ENC_EC(ret);
  39159. /* Test cache with multiple non blocksize bytes */
  39160. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  39161. if (ret != 0)
  39162. return WC_TEST_RET_ENC_EC(ret);
  39163. ret = wc_SipHashUpdate(&siphash, siphash_msg, 5);
  39164. if (ret != 0)
  39165. return WC_TEST_RET_ENC_EC(ret);
  39166. ret = wc_SipHashUpdate(&siphash, siphash_msg + 5, 4);
  39167. if (ret != 0)
  39168. return WC_TEST_RET_ENC_EC(ret);
  39169. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  39170. if (ret != 0)
  39171. return WC_TEST_RET_ENC_EC(ret);
  39172. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  39173. if (ret != 0)
  39174. return WC_TEST_RET_ENC_EC(ret);
  39175. ret = wc_SipHashUpdate(&siphash, siphash_msg, 9);
  39176. if (ret != 0)
  39177. return WC_TEST_RET_ENC_EC(ret);
  39178. ret = wc_SipHashFinal(&siphash, tmp, SIPHASH_MAC_SIZE_8);
  39179. if (ret != 0)
  39180. return WC_TEST_RET_ENC_EC(ret);
  39181. if (XMEMCMP(res, tmp, SIPHASH_MAC_SIZE_8) != 0)
  39182. return WC_TEST_RET_ENC_NC;
  39183. return 0;
  39184. }
  39185. #endif /* WOLFSSL_SIPHASH */
  39186. #ifdef HAVE_LIBZ
  39187. static const byte sample_text[] =
  39188. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  39189. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  39190. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  39191. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  39192. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  39193. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  39194. "\n"
  39195. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  39196. "four loko you probably haven't heard of them high life. Messenger bag\n"
  39197. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  39198. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  39199. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  39200. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  39201. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  39202. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  39203. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  39204. "four loko whatever street art yr farm-to-table.\n"
  39205. "\n"
  39206. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  39207. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  39208. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  39209. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  39210. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  39211. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  39212. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  39213. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  39214. "locavore.\n"
  39215. "\n"
  39216. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  39217. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  39218. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  39219. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  39220. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  39221. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  39222. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  39223. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  39224. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  39225. "neutra PBR selvage.\n"
  39226. "\n"
  39227. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  39228. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  39229. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  39230. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  39231. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  39232. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  39233. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  39234. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  39235. "mollit magna, sriracha sartorial helvetica.\n"
  39236. "\n"
  39237. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  39238. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  39239. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  39240. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  39241. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  39242. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  39243. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  39244. "\n"
  39245. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  39246. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  39247. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  39248. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  39249. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  39250. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  39251. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  39252. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  39253. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  39254. "\n"
  39255. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  39256. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  39257. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  39258. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  39259. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  39260. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  39261. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  39262. "bag dolor terry richardson sapiente.\n";
  39263. static const byte sample_text_gz[] = {
  39264. 0x1F, 0x8B, 0x08, 0x08, 0xC5, 0x49, 0xB5, 0x5B, 0x00, 0x03, 0x63, 0x69, 0x70,
  39265. 0x68, 0x65, 0x72, 0x74, 0x65, 0x78, 0x74, 0x2E, 0x74, 0x78, 0x74, 0x00, 0x8D,
  39266. 0x58, 0xCB, 0x92, 0xE4, 0xB6, 0x11, 0xBC, 0xE3, 0x2B, 0xEA, 0xA6, 0x83, 0xD9,
  39267. 0x1D, 0x72, 0xF8, 0x22, 0x1F, 0xB5, 0x96, 0xA5, 0xDD, 0x90, 0xBC, 0xAB, 0xD0,
  39268. 0x28, 0x36, 0x42, 0x47, 0x90, 0x2C, 0x36, 0xA1, 0x06, 0x09, 0x0A, 0x8F, 0xEE,
  39269. 0xE1, 0xDF, 0x3B, 0x0B, 0xE0, 0x73, 0x2C, 0x4B, 0xBA, 0xCD, 0xCE, 0x80, 0x78,
  39270. 0x64, 0x65, 0x65, 0x66, 0xED, 0x3B, 0xE3, 0x5A, 0xC3, 0x81, 0x2D, 0x35, 0x69,
  39271. 0x32, 0xAD, 0x8E, 0x3A, 0xD2, 0xA0, 0x7D, 0xA7, 0x2B, 0x6A, 0xAC, 0x69, 0x7A,
  39272. 0x26, 0x9D, 0x22, 0xD3, 0x94, 0x22, 0x69, 0xAA, 0x8D, 0x6F, 0xC9, 0x8D, 0x64,
  39273. 0x22, 0x99, 0xB1, 0x31, 0xAD, 0x69, 0xD3, 0x18, 0x89, 0xAD, 0x89, 0x6A, 0x72,
  39274. 0x56, 0x7B, 0x67, 0xDA, 0x2B, 0xBD, 0xC8, 0xEF, 0xB0, 0x4D, 0x74, 0x8E, 0x5B,
  39275. 0xAA, 0x39, 0x4C, 0xEE, 0xCE, 0xE4, 0x79, 0xF2, 0xDC, 0xF3, 0xD8, 0xB2, 0x37,
  39276. 0x11, 0x8B, 0x8C, 0x2C, 0x7A, 0x32, 0x93, 0xF3, 0x37, 0x3D, 0x9A, 0x86, 0x4C,
  39277. 0xAB, 0xF2, 0xB9, 0x57, 0xFA, 0x97, 0x1B, 0x06, 0xD7, 0x3A, 0x7A, 0xF0, 0x68,
  39278. 0xF4, 0x40, 0xBA, 0x25, 0x0E, 0x81, 0xE9, 0xA6, 0x43, 0xF4, 0x6E, 0x4A, 0xF5,
  39279. 0x95, 0xFE, 0x41, 0x4F, 0x67, 0x3B, 0x1A, 0x1C, 0xEE, 0x12, 0xB4, 0x8F, 0xCE,
  39280. 0x1B, 0x6D, 0xB1, 0xDE, 0xBB, 0x4A, 0x4D, 0x56, 0x9B, 0x96, 0x5A, 0xB6, 0xDC,
  39281. 0xC4, 0x14, 0x70, 0xE5, 0xF5, 0x7D, 0xE1, 0xB7, 0x84, 0x3F, 0xFC, 0xED, 0xEF,
  39282. 0xF4, 0x30, 0x0D, 0x5F, 0xE9, 0x47, 0x17, 0xE2, 0xC5, 0x78, 0x27, 0x67, 0xDF,
  39283. 0xB9, 0xEB, 0xCC, 0xCC, 0x3D, 0x59, 0xBE, 0xDD, 0xCC, 0x78, 0x0B, 0x0A, 0x1F,
  39284. 0x74, 0xF8, 0x8C, 0x1A, 0xAF, 0x67, 0xEA, 0xF4, 0x44, 0xBD, 0x93, 0x7D, 0x2A,
  39285. 0xEA, 0x9C, 0xD7, 0x37, 0x80, 0x32, 0x9A, 0x01, 0x37, 0xD5, 0xDE, 0xCA, 0xA2,
  39286. 0x0D, 0xB9, 0xD0, 0x3B, 0xCF, 0xAD, 0x89, 0x4D, 0x5F, 0xD1, 0xE7, 0xF7, 0x2F,
  39287. 0x2A, 0x0C, 0xDA, 0x5A, 0xAA, 0x35, 0x7E, 0x41, 0xC3, 0xB2, 0x37, 0xDD, 0xDD,
  39288. 0xCD, 0x50, 0xEB, 0x2C, 0x96, 0x62, 0x3B, 0xD7, 0x52, 0xF4, 0xA9, 0xB9, 0x6F,
  39289. 0x48, 0xED, 0xEF, 0x54, 0xEA, 0x67, 0xF6, 0x7E, 0x26, 0x8F, 0x3A, 0x68, 0xDF,
  39290. 0x06, 0xBC, 0x56, 0xB7, 0x66, 0x32, 0xC1, 0x34, 0xD8, 0x88, 0x34, 0x1E, 0x88,
  39291. 0xED, 0x67, 0x8A, 0xF3, 0xC4, 0x4F, 0xC0, 0xCA, 0x9E, 0x62, 0x1A, 0x6A, 0xEB,
  39292. 0xAB, 0x02, 0xED, 0xB3, 0xD7, 0x91, 0x81, 0x8A, 0xEA, 0x5C, 0xF2, 0x64, 0xDD,
  39293. 0xDD, 0xD1, 0xEC, 0x12, 0x4D, 0xDE, 0xD5, 0xBA, 0xC6, 0x77, 0xBD, 0x06, 0xC4,
  39294. 0x5F, 0x44, 0xEA, 0x59, 0x4B, 0x5D, 0x3B, 0x8A, 0x3D, 0x0F, 0xD4, 0x9B, 0x1B,
  39295. 0x80, 0x30, 0x1D, 0x30, 0xFA, 0x8F, 0x00, 0x3F, 0xDE, 0xB0, 0x6F, 0xAD, 0x6F,
  39296. 0x6A, 0xDD, 0x6E, 0x2F, 0x6E, 0xCB, 0x3C, 0xD1, 0x83, 0x06, 0x7B, 0x0F, 0xFD,
  39297. 0xFD, 0x4A, 0xEF, 0xBC, 0x73, 0x77, 0x3B, 0x8F, 0x34, 0xA1, 0xBA, 0xEC, 0x39,
  39298. 0x80, 0x33, 0x21, 0xA4, 0x01, 0x55, 0xD7, 0xD4, 0xF4, 0xC6, 0xDA, 0x27, 0x4E,
  39299. 0x54, 0x1C, 0x2B, 0xEC, 0x37, 0xDE, 0xC3, 0x4C, 0xC9, 0x5A, 0x3D, 0x34, 0x0E,
  39300. 0xD8, 0x1C, 0x0E, 0xA2, 0x34, 0xE8, 0xC1, 0xD0, 0xA4, 0x51, 0xD5, 0x88, 0x8B,
  39301. 0xB7, 0xC6, 0xA3, 0x96, 0x40, 0x49, 0xB7, 0xBC, 0xE0, 0x7F, 0x55, 0x3F, 0xEF,
  39302. 0x6F, 0x6E, 0x92, 0x9D, 0x34, 0xFE, 0x3C, 0x5F, 0x04, 0xA5, 0x6A, 0xFF, 0x30,
  39303. 0x08, 0xC9, 0xEA, 0xF5, 0x52, 0x2B, 0xFE, 0x57, 0xFA, 0x8E, 0xC7, 0xE8, 0x4D,
  39304. 0x37, 0xAB, 0x03, 0xFA, 0x23, 0xBF, 0x46, 0x94, 0xFF, 0xC1, 0x16, 0xE0, 0xB9,
  39305. 0x14, 0x2C, 0x9E, 0x27, 0xEC, 0x98, 0x69, 0x14, 0x92, 0xF1, 0x60, 0x5C, 0x34,
  39306. 0x4D, 0xA0, 0x1F, 0xDF, 0xFD, 0x44, 0x1C, 0x7B, 0xD3, 0x80, 0x70, 0x42, 0x02,
  39307. 0x30, 0x84, 0x5B, 0xE5, 0x59, 0xB7, 0xF3, 0x80, 0xFB, 0x01, 0x33, 0xA9, 0x00,
  39308. 0x37, 0x52, 0xDC, 0xDA, 0xA7, 0x11, 0x85, 0xB7, 0x6E, 0x70, 0xE4, 0xDA, 0x96,
  39309. 0xBA, 0x84, 0x5B, 0x81, 0x43, 0x93, 0xF3, 0xD1, 0xEA, 0xB1, 0xDD, 0xB8, 0x1F,
  39310. 0xA5, 0xCC, 0xEA, 0x50, 0x66, 0x69, 0xA9, 0x8D, 0x8C, 0xA7, 0xA2, 0xF3, 0x38,
  39311. 0x26, 0x43, 0x5E, 0x3F, 0x01, 0xBE, 0x1C, 0x0F, 0x20, 0x7F, 0x75, 0xA8, 0x20,
  39312. 0x80, 0xC4, 0xC3, 0x5C, 0x8B, 0x0D, 0xD4, 0x60, 0x5E, 0xA3, 0x9E, 0xD0, 0xB4,
  39313. 0x4B, 0x4F, 0xE6, 0x13, 0x85, 0x60, 0x42, 0x96, 0xED, 0xAA, 0xDB, 0xE9, 0x99,
  39314. 0xE3, 0x07, 0x0E, 0x61, 0xB3, 0x07, 0xE3, 0xB1, 0xFA, 0xC0, 0x9B, 0xAD, 0xF6,
  39315. 0xE0, 0x26, 0x33, 0xEA, 0xEA, 0x23, 0xCD, 0x1E, 0x9D, 0xE1, 0x87, 0x4B, 0x74,
  39316. 0x97, 0x08, 0x3E, 0xA1, 0x28, 0xEA, 0xB3, 0x19, 0x67, 0x8B, 0x76, 0x9A, 0xA3,
  39317. 0xF6, 0xB9, 0xCF, 0x80, 0x65, 0x97, 0xAE, 0xF4, 0x83, 0x6B, 0xF4, 0x43, 0x20,
  39318. 0xF9, 0x0B, 0xFC, 0x9B, 0xD2, 0x4D, 0x4D, 0xA6, 0xB9, 0xA3, 0x02, 0x55, 0x79,
  39319. 0x18, 0x36, 0x19, 0x5F, 0xC9, 0xEA, 0x5A, 0x76, 0x40, 0xB9, 0xBA, 0x0E, 0x9A,
  39320. 0x44, 0xDF, 0x7C, 0xF8, 0x65, 0x61, 0x5E, 0x81, 0xAB, 0x71, 0xA1, 0x9E, 0x29,
  39321. 0x3C, 0x59, 0xCB, 0x23, 0xA4, 0xF6, 0x60, 0x1A, 0x0D, 0x5B, 0x39, 0xAE, 0xF4,
  39322. 0x6F, 0x59, 0x16, 0x9E, 0x60, 0xD8, 0x56, 0xCF, 0xEA, 0x2C, 0x4C, 0x79, 0xD3,
  39323. 0x5D, 0x51, 0x46, 0xA0, 0x4E, 0xE9, 0xD6, 0xAB, 0x91, 0x43, 0x63, 0x44, 0xD7,
  39324. 0x70, 0xB9, 0x23, 0x98, 0x4F, 0x3D, 0x03, 0x02, 0xF6, 0x81, 0x56, 0xC1, 0x58,
  39325. 0x85, 0x07, 0xA7, 0x2D, 0x2C, 0x29, 0xCA, 0x01, 0x45, 0x31, 0x51, 0x8F, 0xD4,
  39326. 0x19, 0xA1, 0x79, 0x88, 0x5A, 0xA4, 0xF5, 0xAE, 0x2D, 0x4B, 0x63, 0x4C, 0x58,
  39327. 0xFE, 0xBF, 0xAD, 0xEE, 0xA3, 0x09, 0xF8, 0xE2, 0x89, 0xBE, 0x81, 0x0E, 0x86,
  39328. 0x3A, 0xF9, 0x5B, 0xA5, 0xD8, 0xA4, 0x00, 0x75, 0x04, 0xF2, 0x23, 0xB8, 0x39,
  39329. 0x69, 0x50, 0xB7, 0xD0, 0x34, 0x63, 0x54, 0xD8, 0x61, 0xDD, 0xA5, 0x33, 0x47,
  39330. 0x85, 0x96, 0x22, 0xD0, 0x2F, 0x9F, 0x7E, 0xF8, 0x74, 0x24, 0xEA, 0x57, 0x97,
  39331. 0x5A, 0xE0, 0x00, 0xCF, 0xC1, 0x67, 0xE1, 0x41, 0xBD, 0x94, 0xA1, 0x03, 0xD3,
  39332. 0xB4, 0x08, 0x64, 0xF2, 0x17, 0x27, 0x35, 0x37, 0x53, 0xEF, 0x46, 0xCE, 0xD8,
  39333. 0xD4, 0x09, 0x52, 0xC6, 0x1E, 0xF7, 0x28, 0xDF, 0x08, 0x0F, 0xD0, 0x6F, 0x71,
  39334. 0xA6, 0xDF, 0xE4, 0x60, 0x8E, 0xC0, 0x1E, 0x78, 0x86, 0x50, 0xB0, 0x9B, 0x84,
  39335. 0x7E, 0xE8, 0x36, 0xFA, 0x95, 0xF1, 0x12, 0x51, 0xC7, 0x18, 0x96, 0xA2, 0x29,
  39336. 0xBB, 0x70, 0x02, 0xB4, 0xF9, 0xA8, 0x3D, 0x08, 0x66, 0xA9, 0xB3, 0xFC, 0x0A,
  39337. 0x94, 0x80, 0xFD, 0x78, 0xDC, 0xAB, 0x82, 0x5A, 0xD2, 0xCD, 0xC2, 0x87, 0xC6,
  39338. 0x4B, 0x07, 0xFA, 0xD1, 0xC3, 0xD9, 0x34, 0x41, 0x85, 0xF8, 0xD0, 0xB6, 0x0A,
  39339. 0x9D, 0x00, 0x91, 0x35, 0x05, 0x88, 0xC3, 0xE3, 0x9B, 0x22, 0xD2, 0xB8, 0xFD,
  39340. 0x95, 0x3E, 0x6D, 0x5D, 0x48, 0xA3, 0x68, 0xCF, 0x02, 0x42, 0x79, 0x79, 0x8A,
  39341. 0xAA, 0x01, 0xD6, 0x09, 0x14, 0x2C, 0xF4, 0x83, 0xA3, 0x80, 0x31, 0x55, 0x46,
  39342. 0x6E, 0xC5, 0xE5, 0x2F, 0x30, 0x58, 0x81, 0xA2, 0x90, 0xBE, 0x2E, 0xA1, 0xC3,
  39343. 0x0F, 0xA6, 0xF5, 0x51, 0x00, 0x39, 0xB6, 0xF2, 0x2A, 0xA3, 0x15, 0x7D, 0x8D,
  39344. 0xF5, 0x66, 0x5C, 0xD9, 0xFC, 0xCF, 0x2F, 0xBF, 0x08, 0x27, 0xE7, 0xD0, 0x03,
  39345. 0xB8, 0xD9, 0x00, 0x13, 0x3D, 0x01, 0x6B, 0xB6, 0xA8, 0xCD, 0x5B, 0x3B, 0x3E,
  39346. 0x93, 0xBF, 0xE6, 0x2E, 0xB7, 0x4A, 0xCF, 0xB3, 0x0A, 0xCE, 0x62, 0x11, 0xD6,
  39347. 0x1F, 0x68, 0x9B, 0x1D, 0x68, 0xD1, 0x8C, 0x97, 0xBD, 0xA1, 0x07, 0x67, 0x73,
  39348. 0x87, 0xE0, 0x36, 0xDA, 0x8C, 0xD2, 0xD2, 0xBB, 0x84, 0x28, 0xA9, 0xFE, 0x52,
  39349. 0x74, 0xD6, 0xB9, 0x0F, 0x0A, 0x6A, 0x2D, 0x28, 0x35, 0x34, 0x3A, 0xD3, 0xE2,
  39350. 0xCD, 0x35, 0x06, 0x7D, 0x1B, 0x35, 0x85, 0x86, 0xD1, 0x3E, 0xF2, 0x6F, 0xA1,
  39351. 0xC4, 0x55, 0xBD, 0x00, 0xD8, 0xC3, 0x5D, 0xC2, 0x1D, 0x6B, 0x6B, 0x27, 0x5B,
  39352. 0x95, 0xF3, 0xAB, 0xB5, 0xD3, 0x37, 0xF2, 0x2C, 0x9C, 0xC7, 0x5D, 0xBD, 0xF1,
  39353. 0x68, 0x1C, 0xAD, 0xF8, 0xB5, 0xE1, 0x29, 0x72, 0x7A, 0x73, 0x62, 0x55, 0x24,
  39354. 0xB9, 0x85, 0xDF, 0x7B, 0x29, 0x7D, 0xDE, 0x08, 0xF5, 0xE4, 0x44, 0xDA, 0x1A,
  39355. 0x30, 0x74, 0xDA, 0xB4, 0x9B, 0x23, 0x9A, 0x3A, 0xC1, 0x53, 0xB2, 0xA2, 0xA3,
  39356. 0x7B, 0x1F, 0xD9, 0x56, 0xD4, 0x4F, 0x9B, 0xB2, 0x1E, 0xEE, 0xB8, 0x6A, 0x4E,
  39357. 0xB5, 0xF4, 0x5A, 0xC9, 0x18, 0x27, 0x9C, 0xDE, 0x14, 0x44, 0xED, 0xC4, 0x3C,
  39358. 0x71, 0x9F, 0x5F, 0xD9, 0x37, 0xA0, 0x78, 0x34, 0x6E, 0xBC, 0xD2, 0x7B, 0x1D,
  39359. 0xFA, 0x08, 0x39, 0x5A, 0x04, 0x73, 0x15, 0xD9, 0x0A, 0x48, 0xC1, 0x2D, 0x15,
  39360. 0x4E, 0x84, 0x30, 0x45, 0x69, 0xB3, 0xE5, 0xF6, 0xAD, 0x09, 0x1E, 0xCC, 0x5F,
  39361. 0x1F, 0x06, 0xD5, 0x58, 0xAD, 0x78, 0xD7, 0x9F, 0xE5, 0xED, 0x3B, 0x09, 0xD5,
  39362. 0xA6, 0x52, 0x6F, 0x92, 0xD3, 0x3C, 0xC6, 0x1E, 0xF2, 0x93, 0x7C, 0xD3, 0x5F,
  39363. 0x70, 0x85, 0x5D, 0xF8, 0xAA, 0x9D, 0xB7, 0x7B, 0x24, 0x5A, 0xE9, 0x0A, 0x35,
  39364. 0x2F, 0xF5, 0xD9, 0x82, 0x02, 0x8A, 0x90, 0x13, 0x5B, 0xB5, 0x67, 0x9C, 0xDD,
  39365. 0xA0, 0x4E, 0x82, 0x27, 0xDA, 0x7E, 0xE8, 0x8E, 0xCD, 0xE1, 0x56, 0x71, 0x2C,
  39366. 0xE6, 0x4E, 0x1F, 0x91, 0xCD, 0x7C, 0x6A, 0xB7, 0x78, 0xD0, 0x26, 0xF3, 0x56,
  39367. 0xA9, 0xD5, 0xA1, 0xC3, 0x3B, 0x98, 0xE9, 0x28, 0x09, 0xEF, 0x50, 0x90, 0xCD,
  39368. 0xC4, 0x8E, 0x75, 0xCC, 0xAC, 0x2D, 0xC9, 0x03, 0x6D, 0xAC, 0xFE, 0xC4, 0x88,
  39369. 0x36, 0xD1, 0x3F, 0xBB, 0x1C, 0x7D, 0xB3, 0x14, 0x61, 0x2C, 0xB7, 0x54, 0x4B,
  39370. 0xDB, 0x64, 0xB6, 0x57, 0x14, 0x16, 0x8E, 0x1E, 0x6C, 0x64, 0xBB, 0x8B, 0x48,
  39371. 0x5D, 0x96, 0x9D, 0xDC, 0x80, 0xA7, 0xF7, 0x54, 0xC7, 0x46, 0x38, 0x3E, 0x44,
  39372. 0xDE, 0x7E, 0x92, 0x8D, 0x07, 0xF6, 0x07, 0x37, 0x4E, 0x16, 0x10, 0xB4, 0x7D,
  39373. 0x88, 0x66, 0x7F, 0xBB, 0xFF, 0xEA, 0x00, 0xF3, 0xFF, 0x97, 0x2C, 0xB5, 0xBE,
  39374. 0x35, 0x4B, 0x5C, 0x36, 0xEC, 0x4C, 0xBD, 0x2B, 0x7D, 0xBF, 0x46, 0xE2, 0x9C,
  39375. 0x0E, 0x8A, 0xA3, 0xEC, 0xB1, 0x0E, 0x9A, 0xDA, 0x9A, 0x9B, 0x28, 0x92, 0x10,
  39376. 0x53, 0x57, 0xEA, 0xEC, 0xA2, 0x32, 0x32, 0x20, 0x1D, 0x97, 0x5C, 0xB6, 0x84,
  39377. 0xA9, 0x93, 0x8D, 0x95, 0x11, 0xA3, 0x24, 0xA3, 0x2D, 0xC6, 0x4A, 0xEF, 0xAA,
  39378. 0x1D, 0x85, 0x2B, 0x7D, 0x28, 0xBE, 0x53, 0xCE, 0x10, 0x1F, 0xAE, 0x0E, 0x41,
  39379. 0x6C, 0x4B, 0x79, 0x12, 0xFB, 0xF7, 0x54, 0xA3, 0x96, 0x54, 0x83, 0x20, 0x96,
  39380. 0x8F, 0x28, 0xA9, 0x3F, 0x8B, 0x3D, 0xBA, 0x77, 0xDC, 0x24, 0xE1, 0xD4, 0x49,
  39381. 0x40, 0xD8, 0x78, 0x31, 0x85, 0x43, 0xF6, 0xFE, 0x5C, 0xA6, 0x8F, 0x90, 0x09,
  39382. 0xB0, 0xE7, 0xC4, 0x95, 0xB2, 0x55, 0x49, 0x97, 0x8F, 0x1C, 0x78, 0x30, 0x20,
  39383. 0xA0, 0xB4, 0xEF, 0x73, 0x56, 0x59, 0x82, 0xFD, 0xCE, 0xBA, 0x6A, 0x8F, 0x2C,
  39384. 0x8B, 0x15, 0xFD, 0xA1, 0x85, 0xA8, 0x5C, 0x0F, 0x11, 0xA5, 0x9D, 0xC2, 0x46,
  39385. 0xC6, 0x9C, 0xC9, 0x40, 0x0B, 0x58, 0x6A, 0x1C, 0x7A, 0x23, 0xF9, 0xE0, 0x95,
  39386. 0x05, 0x13, 0x58, 0x72, 0xE8, 0x9F, 0x30, 0xAC, 0xCD, 0x26, 0xD4, 0x66, 0x13,
  39387. 0xDF, 0x1E, 0x7B, 0x4F, 0x9C, 0xBE, 0x38, 0x79, 0x75, 0x92, 0xA4, 0xDA, 0x26,
  39388. 0x44, 0x55, 0x17, 0xA3, 0xE5, 0x62, 0xDA, 0xEB, 0x86, 0xEA, 0x68, 0xC7, 0xAB,
  39389. 0xFD, 0x2D, 0x43, 0x59, 0x51, 0xC0, 0x75, 0x64, 0x91, 0x01, 0x29, 0x33, 0x28,
  39390. 0xF3, 0x04, 0x83, 0x80, 0x75, 0x37, 0x75, 0x0C, 0x03, 0x7B, 0x0A, 0xAB, 0x8E,
  39391. 0x60, 0x62, 0x8B, 0x4C, 0xAF, 0x2D, 0xA3, 0x2F, 0xFE, 0xAB, 0x45, 0xCF, 0xDA,
  39392. 0xAB, 0xFA, 0xFA, 0x30, 0x3D, 0xE8, 0xA1, 0x96, 0xA5, 0x7B, 0xE2, 0x2A, 0xD0,
  39393. 0xAF, 0x59, 0xF7, 0xD0, 0x32, 0x57, 0x19, 0xBD, 0xCA, 0x9F, 0xD5, 0x1A, 0xC7,
  39394. 0xAA, 0x65, 0x4A, 0x38, 0xB2, 0x70, 0x33, 0xB7, 0x75, 0xD2, 0xCD, 0xD1, 0xF0,
  39395. 0xA8, 0x87, 0x59, 0x20, 0xA5, 0x57, 0x55, 0xB1, 0xB2, 0xC9, 0x4D, 0x97, 0x34,
  39396. 0x41, 0xF3, 0xF0, 0x30, 0xA1, 0x2C, 0x1C, 0x49, 0x3E, 0x89, 0x7D, 0x12, 0xE2,
  39397. 0xC3, 0x04, 0xC3, 0x92, 0xC0, 0xF6, 0x39, 0x10, 0x80, 0x81, 0x8F, 0x08, 0xB4,
  39398. 0xF8, 0xB9, 0x13, 0x4E, 0x2C, 0xAE, 0xB3, 0x71, 0x82, 0x63, 0x98, 0xAB, 0x5C,
  39399. 0x1C, 0x10, 0xEA, 0x66, 0xF9, 0x02, 0x3A, 0x82, 0x61, 0xD0, 0xD4, 0xAE, 0x43,
  39400. 0xD4, 0x01, 0x3E, 0x9D, 0x04, 0x14, 0xF6, 0x60, 0xD8, 0xA7, 0xD6, 0xB8, 0x53,
  39401. 0xC8, 0xDA, 0x80, 0x93, 0xA0, 0x02, 0xDD, 0xCC, 0xE2, 0xF2, 0xBB, 0xFB, 0xE0,
  39402. 0x27, 0xD7, 0x34, 0x9A, 0x71, 0x49, 0xB5, 0x4F, 0x42, 0x1F, 0xB2, 0x9D, 0x6D,
  39403. 0xAA, 0x9D, 0xD3, 0x50, 0xB5, 0x8F, 0x6A, 0x4B, 0xDF, 0x1F, 0xD5, 0x27, 0x8F,
  39404. 0x3B, 0x27, 0xCF, 0x2F, 0x8C, 0xF8, 0x9D, 0x4C, 0x52, 0xBC, 0x32, 0x0F, 0x73,
  39405. 0xD5, 0x51, 0x8E, 0x36, 0x7E, 0xAD, 0x09, 0xF0, 0x94, 0x83, 0x5F, 0x36, 0xFD,
  39406. 0x7C, 0x03, 0xED, 0xF1, 0x5E, 0x4B, 0xF7, 0xAA, 0x55, 0x5C, 0x4A, 0x14, 0x59,
  39407. 0x85, 0x38, 0x2D, 0x8C, 0xDF, 0xEC, 0x65, 0x1B, 0xB8, 0x76, 0x57, 0x96, 0x3C,
  39408. 0x86, 0xED, 0xF2, 0x7F, 0x2D, 0x28, 0x48, 0xDA, 0x49, 0x7F, 0xF7, 0x54, 0x2B,
  39409. 0xD5, 0x39, 0xD5, 0x57, 0x0A, 0x75, 0x7A, 0x3E, 0x5E, 0x5D, 0xBA, 0x4A, 0x15,
  39410. 0xFA, 0xB8, 0x31, 0x80, 0x71, 0x2C, 0xCA, 0xC4, 0x51, 0x10, 0x16, 0x5D, 0x39,
  39411. 0xEC, 0x9D, 0x07, 0xB6, 0x6A, 0x89, 0x9F, 0x9B, 0x5B, 0x6F, 0x03, 0xB0, 0x92,
  39412. 0x01, 0x38, 0x6B, 0x48, 0x99, 0x0A, 0x8F, 0x13, 0xC1, 0xA6, 0x01, 0xEA, 0xBF,
  39413. 0x6F, 0x86, 0x43, 0x51, 0xB6, 0x11, 0x00, 0x00
  39414. };
  39415. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t compress_test(void)
  39416. {
  39417. wc_test_ret_t ret = 0;
  39418. word32 dSz = sizeof(sample_text);
  39419. word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
  39420. byte *c;
  39421. byte *d;
  39422. WOLFSSL_ENTER("compress_test");
  39423. c = (byte *)XMALLOC(cSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39424. d = (byte *)XMALLOC(dSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39425. if (c == NULL || d == NULL) {
  39426. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  39427. }
  39428. /* follow calloc and initialize to 0 */
  39429. XMEMSET(c, 0, cSz);
  39430. XMEMSET(d, 0, dSz);
  39431. if ((ret = wc_Compress(c, cSz, sample_text, dSz, 0)) < 0) {
  39432. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  39433. }
  39434. cSz = (word32)ret;
  39435. if ((ret = wc_DeCompress(d, dSz, c, cSz)) != (int)dSz) {
  39436. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  39437. }
  39438. dSz = (word32)ret;
  39439. if (XMEMCMP(d, sample_text, dSz) != 0) {
  39440. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  39441. }
  39442. /* GZIP tests */
  39443. cSz = (dSz + (word32)(dSz * 0.001) + 12); /* reset cSz */
  39444. XMEMSET(c, 0, cSz);
  39445. XMEMSET(d, 0, dSz);
  39446. ret = wc_Compress_ex(c, cSz, sample_text, dSz, 0, LIBZ_WINBITS_GZIP);
  39447. if (ret < 0)
  39448. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  39449. cSz = (word32)ret;
  39450. ret = wc_DeCompress_ex(d, dSz, c, cSz, LIBZ_WINBITS_GZIP);
  39451. if (ret < 0)
  39452. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  39453. if (XMEMCMP(d, sample_text, dSz) != 0) {
  39454. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  39455. }
  39456. /* Try with gzip generated output */
  39457. XMEMSET(d, 0, dSz);
  39458. ret = wc_DeCompress_ex(d, dSz, sample_text_gz, sizeof(sample_text_gz),
  39459. LIBZ_WINBITS_GZIP);
  39460. if (ret < 0)
  39461. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  39462. dSz = (word32)ret;
  39463. if (XMEMCMP(d, sample_text, dSz) != 0) {
  39464. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  39465. }
  39466. ret = 0; /* success */
  39467. exit:
  39468. if (c) XFREE(c, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39469. if (d) XFREE(d, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39470. return ret;
  39471. }
  39472. #endif /* HAVE_LIBZ */
  39473. #ifdef HAVE_PKCS7
  39474. /* External Debugging/Testing Note:
  39475. *
  39476. * PKCS#7 test functions can output generated PKCS#7/CMS bundles for
  39477. * additional testing. To dump bundles to files DER encoded files, please
  39478. * define:
  39479. *
  39480. * #define PKCS7_OUTPUT_TEST_BUNDLES
  39481. */
  39482. /* Loads certs and keys for use with PKCS7 tests, from either files
  39483. * or buffers.
  39484. *
  39485. * rsaClientCertBuf - output buffer for RSA client cert
  39486. * rsaClientCertBufSz - IN/OUT size of output buffer, size of RSA client cert
  39487. * rsaClientPrivKeyBuf - output buffer for RSA client private key
  39488. * rsaClientPrivKeyBufSz - IN/OUT size of output buffer, size of RSA client key
  39489. *
  39490. * rsaServerCertBuf - output buffer for RSA server cert
  39491. * rsaServerCertBufSz - IN/OUT size of output buffer, size of RSA server cert
  39492. * rsaServerPrivKeyBuf - output buffer for RSA server private key
  39493. * rsaServerPrivKeyBufSz - IN/OUT size of output buffer, size of RSA server key
  39494. *
  39495. * rsaCaCertBuf - output buffer for RSA CA cert
  39496. * rsaCaCertBufSz - IN/OUT size of output buffer, size of RSA ca cert
  39497. * rsaCaPrivKeyBuf - output buffer for RSA CA private key
  39498. * rsaCaPrivKeyBufSz - IN/OUT size of output buffer, size of RSA CA key
  39499. *
  39500. * eccClientCertBuf - output buffer for ECC cert
  39501. * eccClientCertBufSz - IN/OUT size of output buffer, size of ECC cert
  39502. * eccClientPrivKeyBuf - output buffer for ECC private key
  39503. * eccClientPrivKeyBufSz - IN/OUT size of output buffer, size of ECC private key
  39504. *
  39505. * Returns 0 on success, negative on error
  39506. */
  39507. static wc_test_ret_t pkcs7_load_certs_keys(
  39508. byte* rsaClientCertBuf, word32* rsaClientCertBufSz,
  39509. byte* rsaClientPrivKeyBuf, word32* rsaClientPrivKeyBufSz,
  39510. byte* rsaServerCertBuf, word32* rsaServerCertBufSz,
  39511. byte* rsaServerPrivKeyBuf, word32* rsaServerPrivKeyBufSz,
  39512. byte* rsaCaCertBuf, word32* rsaCaCertBufSz,
  39513. byte* rsaCaPrivKeyBuf, word32* rsaCaPrivKeyBufSz,
  39514. byte* eccClientCertBuf, word32* eccClientCertBufSz,
  39515. byte* eccClientPrivKeyBuf, word32* eccClientPrivKeyBufSz)
  39516. {
  39517. #ifndef NO_FILESYSTEM
  39518. XFILE certFile;
  39519. XFILE keyFile;
  39520. (void)certFile;
  39521. (void)keyFile;
  39522. #endif
  39523. #ifndef NO_RSA
  39524. if (rsaClientCertBuf == NULL || rsaClientCertBufSz == NULL ||
  39525. rsaClientPrivKeyBuf == NULL || rsaClientPrivKeyBufSz == NULL)
  39526. return BAD_FUNC_ARG;
  39527. #endif
  39528. #ifdef HAVE_ECC
  39529. if (eccClientCertBuf == NULL || eccClientCertBufSz == NULL ||
  39530. eccClientPrivKeyBuf == NULL || eccClientPrivKeyBufSz == NULL)
  39531. return BAD_FUNC_ARG;
  39532. #endif
  39533. /* RSA */
  39534. #ifndef NO_RSA
  39535. #ifdef USE_CERT_BUFFERS_1024
  39536. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_1024)
  39537. return WC_TEST_RET_ENC_NC;
  39538. XMEMCPY(rsaClientCertBuf, client_cert_der_1024,
  39539. sizeof_client_cert_der_1024);
  39540. *rsaClientCertBufSz = sizeof_client_cert_der_1024;
  39541. if (rsaServerCertBuf != NULL) {
  39542. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_1024)
  39543. return WC_TEST_RET_ENC_NC;
  39544. XMEMCPY(rsaServerCertBuf, server_cert_der_1024,
  39545. sizeof_server_cert_der_1024);
  39546. *rsaServerCertBufSz = sizeof_server_cert_der_1024;
  39547. }
  39548. if (rsaCaCertBuf != NULL) {
  39549. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_1024)
  39550. return WC_TEST_RET_ENC_NC;
  39551. XMEMCPY(rsaCaCertBuf, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  39552. *rsaCaCertBufSz = sizeof_ca_cert_der_1024;
  39553. }
  39554. #elif defined(USE_CERT_BUFFERS_2048)
  39555. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_2048)
  39556. return WC_TEST_RET_ENC_NC;
  39557. XMEMCPY(rsaClientCertBuf, client_cert_der_2048,
  39558. sizeof_client_cert_der_2048);
  39559. *rsaClientCertBufSz = sizeof_client_cert_der_2048;
  39560. if (rsaServerCertBuf != NULL) {
  39561. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_2048)
  39562. return WC_TEST_RET_ENC_NC;
  39563. XMEMCPY(rsaServerCertBuf, server_cert_der_2048,
  39564. sizeof_server_cert_der_2048);
  39565. *rsaServerCertBufSz = sizeof_server_cert_der_2048;
  39566. }
  39567. if (rsaCaCertBuf != NULL) {
  39568. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_2048)
  39569. return WC_TEST_RET_ENC_NC;
  39570. XMEMCPY(rsaCaCertBuf, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  39571. *rsaCaCertBufSz = sizeof_ca_cert_der_2048;
  39572. }
  39573. #else
  39574. certFile = XFOPEN(clientCert, "rb");
  39575. if (!certFile)
  39576. return WC_TEST_RET_ENC_ERRNO;
  39577. *rsaClientCertBufSz = (word32)XFREAD(rsaClientCertBuf, 1,
  39578. *rsaClientCertBufSz, certFile);
  39579. XFCLOSE(certFile);
  39580. if (*rsaClientCertBufSz == 0)
  39581. return WC_TEST_RET_ENC_ERRNO;
  39582. if (rsaServerCertBuf != NULL) {
  39583. certFile = XFOPEN(rsaServerCertDerFile, "rb");
  39584. if (!certFile)
  39585. return WC_TEST_RET_ENC_ERRNO;
  39586. *rsaServerCertBufSz = (word32)XFREAD(rsaServerCertBuf, 1,
  39587. *rsaServerCertBufSz, certFile);
  39588. XFCLOSE(certFile);
  39589. if (*rsaServerCertBufSz == 0)
  39590. return WC_TEST_RET_ENC_ERRNO;
  39591. }
  39592. if (rsaCaCertBuf != NULL) {
  39593. certFile = XFOPEN(rsaCaCertDerFile, "rb");
  39594. if (!certFile)
  39595. return WC_TEST_RET_ENC_ERRNO;
  39596. *rsaCaCertBufSz = (word32)XFREAD(rsaCaCertBuf, 1, *rsaCaCertBufSz,
  39597. certFile);
  39598. XFCLOSE(certFile);
  39599. if (*rsaCaCertBufSz == 0)
  39600. return WC_TEST_RET_ENC_ERRNO;
  39601. }
  39602. #endif
  39603. #ifdef USE_CERT_BUFFERS_1024
  39604. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_1024)
  39605. return WC_TEST_RET_ENC_NC;
  39606. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_1024,
  39607. sizeof_client_key_der_1024);
  39608. *rsaClientPrivKeyBufSz = sizeof_client_key_der_1024;
  39609. if (rsaServerPrivKeyBuf != NULL) {
  39610. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_1024)
  39611. return WC_TEST_RET_ENC_NC;
  39612. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_1024,
  39613. sizeof_server_key_der_1024);
  39614. *rsaServerPrivKeyBufSz = sizeof_server_key_der_1024;
  39615. }
  39616. if (rsaCaPrivKeyBuf != NULL) {
  39617. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_1024)
  39618. return WC_TEST_RET_ENC_NC;
  39619. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_1024, sizeof_ca_key_der_1024);
  39620. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_1024;
  39621. }
  39622. #elif defined(USE_CERT_BUFFERS_2048)
  39623. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_2048)
  39624. return WC_TEST_RET_ENC_NC;
  39625. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_2048,
  39626. sizeof_client_key_der_2048);
  39627. *rsaClientPrivKeyBufSz = sizeof_client_key_der_2048;
  39628. if (rsaServerPrivKeyBuf != NULL) {
  39629. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_2048)
  39630. return WC_TEST_RET_ENC_NC;
  39631. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_2048,
  39632. sizeof_server_key_der_2048);
  39633. *rsaServerPrivKeyBufSz = sizeof_server_key_der_2048;
  39634. }
  39635. if (rsaCaPrivKeyBuf != NULL) {
  39636. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_2048)
  39637. return WC_TEST_RET_ENC_NC;
  39638. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_2048, sizeof_ca_key_der_2048);
  39639. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_2048;
  39640. }
  39641. #else
  39642. keyFile = XFOPEN(clientKey, "rb");
  39643. if (!keyFile)
  39644. return WC_TEST_RET_ENC_ERRNO;
  39645. *rsaClientPrivKeyBufSz = (word32)XFREAD(rsaClientPrivKeyBuf, 1,
  39646. *rsaClientPrivKeyBufSz, keyFile);
  39647. XFCLOSE(keyFile);
  39648. if (*rsaClientPrivKeyBufSz == 0)
  39649. return WC_TEST_RET_ENC_ERRNO;
  39650. if (rsaServerPrivKeyBuf != NULL) {
  39651. keyFile = XFOPEN(rsaServerKeyDerFile, "rb");
  39652. if (!keyFile)
  39653. return WC_TEST_RET_ENC_ERRNO;
  39654. *rsaServerPrivKeyBufSz = (word32)XFREAD(rsaServerPrivKeyBuf, 1,
  39655. *rsaServerPrivKeyBufSz, keyFile);
  39656. XFCLOSE(keyFile);
  39657. if (*rsaServerPrivKeyBufSz == 0)
  39658. return WC_TEST_RET_ENC_ERRNO;
  39659. }
  39660. if (rsaCaPrivKeyBuf != NULL) {
  39661. keyFile = XFOPEN(rsaCaKeyFile, "rb");
  39662. if (!keyFile)
  39663. return WC_TEST_RET_ENC_ERRNO;
  39664. *rsaCaPrivKeyBufSz = (word32)XFREAD(rsaCaPrivKeyBuf, 1,
  39665. *rsaCaPrivKeyBufSz, keyFile);
  39666. XFCLOSE(keyFile);
  39667. if (*rsaCaPrivKeyBufSz == 0)
  39668. return WC_TEST_RET_ENC_ERRNO;
  39669. }
  39670. #endif /* USE_CERT_BUFFERS */
  39671. #endif /* NO_RSA */
  39672. /* ECC */
  39673. #ifdef HAVE_ECC
  39674. #ifdef USE_CERT_BUFFERS_256
  39675. if (*eccClientCertBufSz < (word32)sizeof_cliecc_cert_der_256)
  39676. return WC_TEST_RET_ENC_NC;
  39677. XMEMCPY(eccClientCertBuf, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  39678. *eccClientCertBufSz = sizeof_cliecc_cert_der_256;
  39679. #else
  39680. certFile = XFOPEN(eccClientCert, "rb");
  39681. if (!certFile)
  39682. return WC_TEST_RET_ENC_ERRNO;
  39683. *eccClientCertBufSz = (word32)XFREAD(eccClientCertBuf, 1,
  39684. *eccClientCertBufSz, certFile);
  39685. XFCLOSE(certFile);
  39686. if (*eccClientCertBufSz == 0)
  39687. return WC_TEST_RET_ENC_ERRNO;
  39688. #endif /* USE_CERT_BUFFERS_256 */
  39689. #ifdef USE_CERT_BUFFERS_256
  39690. if (*eccClientPrivKeyBufSz < (word32)sizeof_ecc_clikey_der_256)
  39691. return WC_TEST_RET_ENC_NC;
  39692. XMEMCPY(eccClientPrivKeyBuf, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  39693. *eccClientPrivKeyBufSz = sizeof_ecc_clikey_der_256;
  39694. #else
  39695. keyFile = XFOPEN(eccClientKey, "rb");
  39696. if (!keyFile)
  39697. return WC_TEST_RET_ENC_ERRNO;
  39698. *eccClientPrivKeyBufSz = (word32)XFREAD(eccClientPrivKeyBuf, 1,
  39699. *eccClientPrivKeyBufSz, keyFile);
  39700. XFCLOSE(keyFile);
  39701. if (*eccClientPrivKeyBufSz == 0)
  39702. return WC_TEST_RET_ENC_ERRNO;
  39703. #endif /* USE_CERT_BUFFERS_256 */
  39704. #endif /* HAVE_ECC */
  39705. #ifdef NO_RSA
  39706. (void)rsaClientCertBuf;
  39707. (void)rsaClientCertBufSz;
  39708. (void)rsaClientPrivKeyBuf;
  39709. (void)rsaClientPrivKeyBufSz;
  39710. (void)rsaServerCertBuf;
  39711. (void)rsaServerCertBufSz;
  39712. (void)rsaServerPrivKeyBuf;
  39713. (void)rsaServerPrivKeyBufSz;
  39714. (void)rsaCaCertBuf;
  39715. (void)rsaCaCertBufSz;
  39716. (void)rsaCaPrivKeyBuf;
  39717. (void)rsaCaPrivKeyBufSz;
  39718. #endif
  39719. #ifndef HAVE_ECC
  39720. (void)eccClientCertBuf;
  39721. (void)eccClientCertBufSz;
  39722. (void)eccClientPrivKeyBuf;
  39723. (void)eccClientPrivKeyBufSz;
  39724. #endif
  39725. #ifndef NO_FILESYSTEM
  39726. (void)certFile;
  39727. (void)keyFile;
  39728. #endif
  39729. return 0;
  39730. }
  39731. typedef struct {
  39732. const byte* content;
  39733. word32 contentSz;
  39734. int contentOID;
  39735. int encryptOID;
  39736. int keyWrapOID;
  39737. int keyAgreeOID;
  39738. byte* cert;
  39739. size_t certSz;
  39740. byte* privateKey;
  39741. word32 privateKeySz;
  39742. byte* optionalUkm;
  39743. word32 optionalUkmSz;
  39744. int ktriOptions; /* KTRI options flags */
  39745. int kariOptions; /* KARI options flags */
  39746. /* KEKRI specific */
  39747. const byte* secretKey; /* key, only for kekri RecipientInfo types */
  39748. word32 secretKeySz; /* size of secretKey, bytes */
  39749. const byte* secretKeyId; /* key identifier */
  39750. word32 secretKeyIdSz; /* size of key identifier, bytes */
  39751. void* timePtr; /* time_t pointer */
  39752. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  39753. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  39754. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  39755. word32 otherAttrSz; /* size of otherAttr, bytes */
  39756. int kekriOptions; /* KEKRI options flags */
  39757. /* PWRI specific */
  39758. const char* password;
  39759. word32 passwordSz;
  39760. const byte* salt;
  39761. word32 saltSz;
  39762. int kdfOID;
  39763. int hashOID;
  39764. int kdfIterations;
  39765. int pwriOptions; /* PWRI options flags */
  39766. /* ORI specific */
  39767. int isOri;
  39768. int oriOptions; /* ORI options flags */
  39769. const char* outFileName;
  39770. } pkcs7EnvelopedVector;
  39771. static const byte asnDataOid[] = {
  39772. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
  39773. };
  39774. /* ORI encrypt callback, responsible for encrypting content-encryption key (CEK)
  39775. * and giving wolfCrypt the value for oriOID and oriValue to place in
  39776. * OtherRecipientInfo.
  39777. *
  39778. * Returns 0 on success, negative upon error. */
  39779. static int myOriEncryptCb(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* oriType,
  39780. word32* oriTypeSz, byte* oriValue, word32* oriValueSz,
  39781. void* ctx)
  39782. {
  39783. int i;
  39784. /* make sure buffers are large enough */
  39785. if (*oriValueSz < (2 + cekSz))
  39786. return WC_TEST_RET_ENC_NC;
  39787. if (*oriTypeSz < sizeof(asnDataOid))
  39788. return WC_TEST_RET_ENC_NC;
  39789. /* our simple encryption algorithm will be take the bitwise complement */
  39790. oriValue[0] = 0x04; /*ASN OCTET STRING */
  39791. oriValue[1] = (byte)cekSz; /* length */
  39792. for (i = 0; i < (int)cekSz; i++) {
  39793. oriValue[2 + i] = ~cek[i];
  39794. }
  39795. *oriValueSz = 2 + cekSz;
  39796. /* set oriType to ASN.1 encoded data OID */
  39797. XMEMCPY(oriType, asnDataOid, sizeof(asnDataOid));
  39798. *oriTypeSz = sizeof(asnDataOid);
  39799. (void)pkcs7;
  39800. (void)ctx;
  39801. return 0;
  39802. }
  39803. /* ORI decrypt callback, responsible for providing a decrypted content
  39804. * encryption key (CEK) placed into decryptedKey and size placed into
  39805. * decryptedKeySz. oriOID and oriValue are given to the callback to help
  39806. * in decrypting the encrypted CEK.
  39807. *
  39808. * Returns 0 on success, negative upon error. */
  39809. static int myOriDecryptCb(PKCS7* pkcs7, byte* oriType, word32 oriTypeSz,
  39810. byte* oriValue, word32 oriValueSz, byte* decryptedKey,
  39811. word32* decryptedKeySz, void* ctx)
  39812. {
  39813. int i;
  39814. /* make sure oriType matches what we expect */
  39815. if (oriTypeSz != sizeof(asnDataOid))
  39816. return WC_TEST_RET_ENC_NC;
  39817. if (XMEMCMP(oriType, asnDataOid, sizeof(asnDataOid)) != 0)
  39818. return WC_TEST_RET_ENC_NC;
  39819. /* make sure decrypted buffer is large enough */
  39820. if (*decryptedKeySz < oriValueSz)
  39821. return WC_TEST_RET_ENC_NC;
  39822. /* decrypt encrypted CEK using simple bitwise complement,
  39823. only for example */
  39824. for (i = 0; i < (int)oriValueSz - 2; i++) {
  39825. decryptedKey[i] = ~oriValue[2 + i];
  39826. }
  39827. *decryptedKeySz = oriValueSz - 2;
  39828. (void)pkcs7;
  39829. (void)ctx;
  39830. return 0;
  39831. }
  39832. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  39833. /* returns 0 on success */
  39834. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  39835. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  39836. byte* in, int inSz, byte* out, void* usrCtx)
  39837. {
  39838. wc_test_ret_t ret;
  39839. int keyId = -1, keySz;
  39840. word32 keyIdSz = 8;
  39841. const byte* key;
  39842. byte keyIdRaw[8];
  39843. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39844. Aes *aes;
  39845. #else
  39846. Aes aes[1];
  39847. #endif
  39848. /* looking for KEY ID
  39849. * fwDecryptKeyID OID "1.2.840.113549.1.9.16.2.37
  39850. */
  39851. WOLFSSL_SMALL_STACK_STATIC const unsigned char OID[] = {
  39852. /* 0x06, 0x0B do not pass in tag and length */
  39853. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  39854. 0x01, 0x09, 0x10, 0x02, 0x25
  39855. };
  39856. WOLFSSL_SMALL_STACK_STATIC const byte defKey[] = {
  39857. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  39858. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  39859. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  39860. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  39861. };
  39862. WOLFSSL_SMALL_STACK_STATIC const byte altKey[] = {
  39863. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  39864. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  39865. };
  39866. /* test user context passed in */
  39867. if (usrCtx == NULL || *(int*)usrCtx != 1) {
  39868. return WC_TEST_RET_ENC_NC;
  39869. }
  39870. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39871. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  39872. return WC_TEST_RET_ENC_ERRNO;
  39873. #endif
  39874. /* if needing to find keyIdSz can call with NULL */
  39875. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), NULL,
  39876. &keyIdSz);
  39877. if (ret != LENGTH_ONLY_E) {
  39878. printf("Unexpected error %d when getting keyIdSz\n", ret);
  39879. printf("Possibly no KEY ID attribute set\n");
  39880. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39881. }
  39882. else {
  39883. XMEMSET(keyIdRaw, 0, sizeof(keyIdRaw));
  39884. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), keyIdRaw,
  39885. &keyIdSz);
  39886. if (ret < 0) {
  39887. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39888. }
  39889. if (keyIdSz < 3) {
  39890. printf("keyIdSz is smaller than expected\n");
  39891. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39892. }
  39893. if (keyIdSz > 2 + sizeof(int)) {
  39894. printf("example case was only expecting a keyId of int size\n");
  39895. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39896. }
  39897. /* keyIdRaw[0] OCTET TAG */
  39898. /* keyIdRaw[1] Length */
  39899. #ifdef BIG_ENDIAN_ORDER
  39900. if (keyIdRaw[1] == 0x01) {
  39901. keyId = 1;
  39902. }
  39903. #else
  39904. XMEMCPY(&keyId, keyIdRaw + 2, sizeof(keyId));
  39905. #endif
  39906. }
  39907. /* Use keyID here if found to select key and decrypt in HSM or in this
  39908. * example just select key and do software decryption */
  39909. if (keyId == 1) {
  39910. key = altKey;
  39911. keySz = sizeof(altKey);
  39912. }
  39913. else {
  39914. key = defKey;
  39915. keySz = sizeof(defKey);
  39916. }
  39917. switch (encryptOID) {
  39918. #ifdef WOLFSSL_AES_256
  39919. case AES256CBCb:
  39920. if ((keySz != 32 ) || (ivSz != AES_BLOCK_SIZE))
  39921. WARNING_OUT(BAD_FUNC_ARG, out);
  39922. break;
  39923. #endif
  39924. #ifdef WOLFSSL_AES_128
  39925. case AES128CBCb:
  39926. if ((keySz != 16 ) || (ivSz != AES_BLOCK_SIZE))
  39927. ERROR_OUT(BAD_FUNC_ARG, out);
  39928. break;
  39929. #endif
  39930. default:
  39931. printf("Unsupported content cipher type for example");
  39932. ERROR_OUT(ALGO_ID_E, out);
  39933. };
  39934. ret = wc_AesInit(aes, HEAP_HINT, INVALID_DEVID);
  39935. if (ret == 0) {
  39936. ret = wc_AesSetKey(aes, key, (word32)keySz, iv, AES_DECRYPTION);
  39937. if (ret == 0)
  39938. ret = wc_AesCbcDecrypt(aes, out, in, (word32)inSz);
  39939. wc_AesFree(aes);
  39940. }
  39941. out:
  39942. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39943. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39944. #endif
  39945. (void)aad;
  39946. (void)aadSz;
  39947. (void)authTag;
  39948. (void)authTagSz;
  39949. return (int)ret;
  39950. }
  39951. #endif /* !NO_AES && HAVE_AES_CBC */
  39952. #define PKCS7_BUF_SIZE 2048
  39953. static wc_test_ret_t pkcs7enveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  39954. byte* rsaPrivKey, word32 rsaPrivKeySz,
  39955. byte* eccCert, word32 eccCertSz,
  39956. byte* eccPrivKey, word32 eccPrivKeySz)
  39957. {
  39958. wc_test_ret_t ret = 0;
  39959. int testSz = 0, i;
  39960. int envelopedSz, decodedSz;
  39961. byte *enveloped = NULL;
  39962. byte *decoded = NULL;
  39963. PKCS7* pkcs7 = NULL;
  39964. #ifdef ECC_TIMING_RESISTANT
  39965. WC_RNG rng;
  39966. #endif
  39967. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  39968. XFILE pkcs7File;
  39969. #endif
  39970. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  39971. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  39972. 0x72,0x6c,0x64
  39973. };
  39974. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256) && \
  39975. defined(HAVE_ECC) && defined(WOLFSSL_SHA512)
  39976. byte optionalUkm[] = {
  39977. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  39978. };
  39979. #endif /* NO_AES */
  39980. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) && \
  39981. !defined(NO_SHA)
  39982. /* encryption key for kekri recipient types */
  39983. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  39984. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  39985. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  39986. };
  39987. /* encryption key identifier */
  39988. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  39989. 0x02,0x02,0x03,0x04
  39990. };
  39991. #endif
  39992. #if !defined(NO_PWDBASED) && !defined(NO_SHA) && \
  39993. !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  39994. #ifndef HAVE_FIPS
  39995. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password"; /* NOTE: Password is too short for FIPS */
  39996. #else
  39997. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  39998. #endif
  39999. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  40000. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  40001. };
  40002. #endif
  40003. #define MAX_TESTVECTORS_LEN 13
  40004. #define ADD_PKCS7ENVELOPEDVECTOR(...) { \
  40005. pkcs7EnvelopedVector _this_vector = { __VA_ARGS__ }; \
  40006. if (testSz == MAX_TESTVECTORS_LEN) { \
  40007. ret = WC_TEST_RET_ENC_NC; \
  40008. goto out; \
  40009. } \
  40010. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  40011. }
  40012. pkcs7EnvelopedVector *testVectors = NULL;
  40013. #ifdef ECC_TIMING_RESISTANT
  40014. XMEMSET(&rng, 0, sizeof(rng));
  40015. #endif
  40016. testVectors = (pkcs7EnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  40017. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40018. if (testVectors == NULL) {
  40019. ret = WC_TEST_RET_ENC_ERRNO;
  40020. goto out;
  40021. }
  40022. {
  40023. /* key transport key encryption technique */
  40024. #ifndef NO_RSA
  40025. #ifndef NO_DES3
  40026. ADD_PKCS7ENVELOPEDVECTOR(
  40027. data, (word32)sizeof(data), DATA, DES3b, 0, 0, rsaCert, rsaCertSz,
  40028. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  40029. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  40030. "pkcs7envelopedDataDES3.der");
  40031. #endif
  40032. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  40033. #ifdef WOLFSSL_AES_128
  40034. ADD_PKCS7ENVELOPEDVECTOR(
  40035. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, rsaCert, rsaCertSz,
  40036. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  40037. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  40038. "pkcs7envelopedDataAES128CBC.der");
  40039. #endif
  40040. #ifdef WOLFSSL_AES_192
  40041. ADD_PKCS7ENVELOPEDVECTOR(
  40042. data, (word32)sizeof(data), DATA, AES192CBCb, 0, 0, rsaCert, rsaCertSz,
  40043. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  40044. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  40045. "pkcs7envelopedDataAES192CBC.der");
  40046. #endif
  40047. #ifdef WOLFSSL_AES_256
  40048. ADD_PKCS7ENVELOPEDVECTOR(
  40049. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  40050. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  40051. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  40052. "pkcs7envelopedDataAES256CBC.der");
  40053. /* explicitly using SKID for SubjectKeyIdentifier */
  40054. ADD_PKCS7ENVELOPEDVECTOR(
  40055. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  40056. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_SKID, 0, NULL, 0, NULL, 0, NULL,
  40057. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  40058. "pkcs7envelopedDataAES256CBC_SKID.der");
  40059. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  40060. ADD_PKCS7ENVELOPEDVECTOR(
  40061. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  40062. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_ISSUER_AND_SERIAL_NUMBER, 0,
  40063. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  40064. 0, 0, 0, 0, "pkcs7envelopedDataAES256CBC_IANDS.der");
  40065. #endif
  40066. #endif /* !NO_AES && HAVE_AES_CBC */
  40067. #endif
  40068. /* key agreement key encryption technique*/
  40069. #ifdef HAVE_ECC
  40070. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  40071. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  40072. ADD_PKCS7ENVELOPEDVECTOR(
  40073. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP,
  40074. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  40075. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  40076. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  40077. "pkcs7envelopedDataAES128CBC_ECDH_SHA1KDF.der");
  40078. #endif
  40079. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  40080. ADD_PKCS7ENVELOPEDVECTOR(
  40081. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  40082. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  40083. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  40084. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  40085. "pkcs7envelopedDataAES256CBC_ECDH_SHA256KDF.der");
  40086. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  40087. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  40088. ADD_PKCS7ENVELOPEDVECTOR(
  40089. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  40090. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  40091. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  40092. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  40093. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF.der");
  40094. /* with optional user keying material (ukm) */
  40095. ADD_PKCS7ENVELOPEDVECTOR(
  40096. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  40097. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  40098. eccPrivKeySz, optionalUkm, sizeof(optionalUkm), 0, 0, NULL, 0,
  40099. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  40100. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF_ukm.der");
  40101. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  40102. #endif /* !NO_AES && HAVE_AES_CBC */
  40103. #endif
  40104. /* kekri (KEKRecipientInfo) recipient types */
  40105. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  40106. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  40107. ADD_PKCS7ENVELOPEDVECTOR(
  40108. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP, 0,
  40109. NULL, 0, NULL, 0, NULL, 0, 0, 0, secretKey, sizeof(secretKey),
  40110. secretKeyId, sizeof(secretKeyId), NULL, NULL, 0, NULL, 0,
  40111. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  40112. "pkcs7envelopedDataAES128CBC_KEKRI.der");
  40113. #endif
  40114. #endif /* !NO_AES && HAVE_AES_CBC */
  40115. /* pwri (PasswordRecipientInfo) recipient types */
  40116. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  40117. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  40118. ADD_PKCS7ENVELOPEDVECTOR(
  40119. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0,
  40120. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  40121. NULL, 0, NULL, NULL, 0, NULL, 0, 0, password,
  40122. (word32)XSTRLEN(password), salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  40123. 0, 0, 0, "pkcs7envelopedDataAES128CBC_PWRI.der");
  40124. #endif
  40125. #endif
  40126. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_128)
  40127. /* ori (OtherRecipientInfo) recipient types */
  40128. ADD_PKCS7ENVELOPEDVECTOR(
  40129. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, NULL, 0, NULL, 0,
  40130. NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0,
  40131. NULL, 0, 0, 0, 0, 0, 1, 0, "pkcs7envelopedDataAES128CBC_ORI.der");
  40132. #endif
  40133. };
  40134. #undef MAX_TESTVECTORS_LEN
  40135. #undef ADD_PKCS7ENVELOPEDVECTOR
  40136. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40137. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40138. if ((! enveloped) || (! decoded)) {
  40139. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40140. }
  40141. #ifdef ECC_TIMING_RESISTANT
  40142. #ifndef HAVE_FIPS
  40143. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  40144. #else
  40145. ret = wc_InitRng(&rng);
  40146. #endif
  40147. if (ret != 0)
  40148. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40149. #endif
  40150. for (i = 0; i < testSz; i++) {
  40151. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  40152. #ifdef WOLFSSL_ASYNC_CRYPT
  40153. INVALID_DEVID /* async PKCS7 is not supported */
  40154. #else
  40155. devId
  40156. #endif
  40157. );
  40158. if (pkcs7 == NULL) {
  40159. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  40160. }
  40161. if (testVectors[i].secretKey != NULL) {
  40162. /* KEKRI recipient type */
  40163. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  40164. if (ret != 0)
  40165. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40166. pkcs7->content = (byte*)testVectors[i].content;
  40167. pkcs7->contentSz = testVectors[i].contentSz;
  40168. pkcs7->contentOID = testVectors[i].contentOID;
  40169. pkcs7->encryptOID = testVectors[i].encryptOID;
  40170. pkcs7->ukm = testVectors[i].optionalUkm;
  40171. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  40172. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  40173. (byte *)testVectors[i].secretKey, testVectors[i].secretKeySz,
  40174. (byte *)testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  40175. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  40176. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  40177. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  40178. if (ret < 0) {
  40179. wc_PKCS7_Free(pkcs7);
  40180. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40181. }
  40182. /* set key, for decryption */
  40183. ret = wc_PKCS7_SetKey(pkcs7, (byte *)testVectors[i].secretKey,
  40184. testVectors[i].secretKeySz);
  40185. if (ret != 0) {
  40186. wc_PKCS7_Free(pkcs7);
  40187. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40188. }
  40189. } else if (testVectors[i].password != NULL) {
  40190. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  40191. /* PWRI recipient type */
  40192. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  40193. if (ret != 0)
  40194. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40195. pkcs7->content = (byte*)testVectors[i].content;
  40196. pkcs7->contentSz = testVectors[i].contentSz;
  40197. pkcs7->contentOID = testVectors[i].contentOID;
  40198. pkcs7->encryptOID = testVectors[i].encryptOID;
  40199. pkcs7->ukm = testVectors[i].optionalUkm;
  40200. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  40201. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  40202. (byte *)testVectors[i].password, testVectors[i].passwordSz,
  40203. (byte *)testVectors[i].salt, testVectors[i].saltSz,
  40204. testVectors[i].kdfOID,
  40205. testVectors[i].hashOID, testVectors[i].kdfIterations,
  40206. testVectors[i].encryptOID, testVectors[i].pwriOptions);
  40207. if (ret < 0) {
  40208. wc_PKCS7_Free(pkcs7);
  40209. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40210. }
  40211. /* set password, for decryption */
  40212. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  40213. testVectors[i].passwordSz);
  40214. if (ret < 0) {
  40215. wc_PKCS7_Free(pkcs7);
  40216. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40217. }
  40218. #endif /* ! NO_PWDBASED && ! NO_SHA */
  40219. } else if (testVectors[i].isOri == 1) {
  40220. /* ORI recipient type */
  40221. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  40222. if (ret != 0)
  40223. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40224. pkcs7->content = (byte*)testVectors[i].content;
  40225. pkcs7->contentSz = testVectors[i].contentSz;
  40226. pkcs7->contentOID = testVectors[i].contentOID;
  40227. pkcs7->encryptOID = testVectors[i].encryptOID;
  40228. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  40229. testVectors[i].oriOptions);
  40230. if (ret < 0) {
  40231. wc_PKCS7_Free(pkcs7);
  40232. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40233. }
  40234. /* set decrypt callback for decryption */
  40235. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  40236. if (ret < 0) {
  40237. wc_PKCS7_Free(pkcs7);
  40238. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40239. }
  40240. } else {
  40241. /* KTRI or KARI recipient types */
  40242. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  40243. if (ret != 0)
  40244. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40245. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  40246. (word32)testVectors[i].certSz);
  40247. if (ret != 0) {
  40248. wc_PKCS7_Free(pkcs7);
  40249. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40250. }
  40251. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  40252. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  40253. pkcs7->privateKey = testVectors[i].privateKey;
  40254. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  40255. pkcs7->content = (byte*)testVectors[i].content;
  40256. pkcs7->contentSz = testVectors[i].contentSz;
  40257. pkcs7->contentOID = testVectors[i].contentOID;
  40258. pkcs7->encryptOID = testVectors[i].encryptOID;
  40259. pkcs7->ukm = testVectors[i].optionalUkm;
  40260. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  40261. /* set SubjectIdentifier type for KTRI types */
  40262. if (testVectors[i].ktriOptions & CMS_SKID) {
  40263. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  40264. if (ret != 0) {
  40265. wc_PKCS7_Free(pkcs7);
  40266. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40267. }
  40268. } else if (testVectors[i].ktriOptions &
  40269. CMS_ISSUER_AND_SERIAL_NUMBER) {
  40270. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  40271. CMS_ISSUER_AND_SERIAL_NUMBER);
  40272. if (ret != 0) {
  40273. wc_PKCS7_Free(pkcs7);
  40274. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40275. }
  40276. }
  40277. }
  40278. #ifdef ECC_TIMING_RESISTANT
  40279. pkcs7->rng = &rng;
  40280. #endif
  40281. /* encode envelopedData */
  40282. envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, enveloped,
  40283. PKCS7_BUF_SIZE);
  40284. if (envelopedSz <= 0) {
  40285. wc_PKCS7_Free(pkcs7);
  40286. ERROR_OUT(WC_TEST_RET_ENC_EC(envelopedSz), out);
  40287. }
  40288. /* decode envelopedData */
  40289. pkcs7->contentOID = 0;
  40290. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped, (word32)envelopedSz,
  40291. decoded, PKCS7_BUF_SIZE);
  40292. if (pkcs7->contentOID != testVectors[i].contentOID ||
  40293. decodedSz <= 0) {
  40294. wc_PKCS7_Free(pkcs7);
  40295. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  40296. }
  40297. /* test decode result */
  40298. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  40299. wc_PKCS7_Free(pkcs7);
  40300. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40301. }
  40302. #ifndef NO_PKCS7_STREAM
  40303. { /* test reading byte by byte */
  40304. int z;
  40305. for (z = 0; z < envelopedSz; z++) {
  40306. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped + z, 1,
  40307. decoded, PKCS7_BUF_SIZE);
  40308. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  40309. printf("unexpected error %d\n", decodedSz);
  40310. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  40311. }
  40312. }
  40313. /* test decode result */
  40314. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  40315. printf("stream read compare failed\n");
  40316. wc_PKCS7_Free(pkcs7);
  40317. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40318. }
  40319. }
  40320. #endif
  40321. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  40322. /* output pkcs7 envelopedData for external testing */
  40323. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  40324. if (!pkcs7File) {
  40325. wc_PKCS7_Free(pkcs7);
  40326. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  40327. }
  40328. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  40329. XFCLOSE(pkcs7File);
  40330. if (ret != envelopedSz) {
  40331. wc_PKCS7_Free(pkcs7);
  40332. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  40333. } else {
  40334. /* reset ret to 0 for success */
  40335. ret = 0;
  40336. }
  40337. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  40338. wc_PKCS7_Free(pkcs7);
  40339. pkcs7 = NULL;
  40340. }
  40341. #ifdef ECC_TIMING_RESISTANT
  40342. wc_FreeRng(&rng);
  40343. #endif
  40344. (void)eccCert;
  40345. (void)eccCertSz;
  40346. (void)eccPrivKey;
  40347. (void)eccPrivKeySz;
  40348. (void)rsaCert;
  40349. (void)rsaCertSz;
  40350. (void)rsaPrivKey;
  40351. (void)rsaPrivKeySz;
  40352. out:
  40353. if (testVectors)
  40354. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40355. if (enveloped)
  40356. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40357. if (decoded)
  40358. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40359. return ret;
  40360. }
  40361. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7enveloped_test(void)
  40362. {
  40363. wc_test_ret_t ret = 0;
  40364. byte* rsaCert = NULL;
  40365. byte* rsaPrivKey = NULL;
  40366. word32 rsaCertSz = 0;
  40367. word32 rsaPrivKeySz = 0;
  40368. byte* eccCert = NULL;
  40369. byte* eccPrivKey = NULL;
  40370. word32 eccCertSz = 0;
  40371. word32 eccPrivKeySz = 0;
  40372. WOLFSSL_ENTER("pkcs7enveloped_test");
  40373. #ifndef NO_RSA
  40374. /* read client RSA cert and key in DER format */
  40375. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40376. if (rsaCert == NULL)
  40377. return WC_TEST_RET_ENC_ERRNO;
  40378. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40379. if (rsaPrivKey == NULL) {
  40380. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40381. return WC_TEST_RET_ENC_NC;
  40382. }
  40383. rsaCertSz = FOURK_BUF;
  40384. rsaPrivKeySz = FOURK_BUF;
  40385. #endif /* NO_RSA */
  40386. #ifdef HAVE_ECC
  40387. /* read client ECC cert and key in DER format */
  40388. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40389. if (eccCert == NULL) {
  40390. #ifndef NO_RSA
  40391. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40392. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40393. #endif
  40394. return WC_TEST_RET_ENC_NC;
  40395. }
  40396. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40397. if (eccPrivKey == NULL) {
  40398. #ifndef NO_RSA
  40399. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40400. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40401. #endif
  40402. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40403. return WC_TEST_RET_ENC_NC;
  40404. }
  40405. eccCertSz = FOURK_BUF;
  40406. eccPrivKeySz = FOURK_BUF;
  40407. #endif /* HAVE_ECC */
  40408. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  40409. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  40410. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  40411. eccPrivKey, &eccPrivKeySz);
  40412. if (ret < 0) {
  40413. #ifndef NO_RSA
  40414. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40415. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40416. #endif
  40417. #ifdef HAVE_ECC
  40418. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40419. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40420. #endif
  40421. return WC_TEST_RET_ENC_EC(ret);
  40422. }
  40423. ret = pkcs7enveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  40424. rsaPrivKey, (word32)rsaPrivKeySz,
  40425. eccCert, (word32)eccCertSz,
  40426. eccPrivKey, (word32)eccPrivKeySz);
  40427. #ifndef NO_RSA
  40428. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40429. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40430. #endif
  40431. #ifdef HAVE_ECC
  40432. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40433. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40434. #endif
  40435. return ret;
  40436. }
  40437. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  40438. typedef struct {
  40439. const byte* content;
  40440. word32 contentSz;
  40441. int contentOID;
  40442. int encryptOID;
  40443. int keyWrapOID;
  40444. int keyAgreeOID;
  40445. byte* cert;
  40446. size_t certSz;
  40447. byte* privateKey;
  40448. word32 privateKeySz;
  40449. PKCS7Attrib* authAttribs;
  40450. word32 authAttribsSz;
  40451. PKCS7Attrib* unauthAttribs;
  40452. word32 unauthAttribsSz;
  40453. /* KARI / KTRI specific */
  40454. byte* optionalUkm;
  40455. word32 optionalUkmSz;
  40456. int ktriOptions; /* KTRI options flags */
  40457. int kariOptions; /* KARI options flags */
  40458. /* KEKRI specific */
  40459. byte* secretKey; /* key, only for kekri RecipientInfo types */
  40460. word32 secretKeySz; /* size of secretKey, bytes */
  40461. byte* secretKeyId; /* key identifier */
  40462. word32 secretKeyIdSz; /* size of key identifier, bytes */
  40463. void* timePtr; /* time_t pointer */
  40464. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  40465. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  40466. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  40467. word32 otherAttrSz; /* size of otherAttr, bytes */
  40468. int kekriOptions; /* KEKRI options flags */
  40469. /* PWRI specific */
  40470. char* password; /* password */
  40471. word32 passwordSz; /* password size, bytes */
  40472. byte* salt; /* KDF salt */
  40473. word32 saltSz; /* KDF salt size, bytes */
  40474. int kdfOID; /* KDF OID */
  40475. int hashOID; /* KDF hash algorithm OID */
  40476. int kdfIterations; /* KDF iterations */
  40477. int kekEncryptOID; /* KEK encryption algorithm OID */
  40478. int pwriOptions; /* PWRI options flags */
  40479. /* ORI specific */
  40480. int isOri;
  40481. int oriOptions; /* ORI options flags */
  40482. const char* outFileName;
  40483. } pkcs7AuthEnvelopedVector;
  40484. static wc_test_ret_t pkcs7authenveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  40485. byte* rsaPrivKey, word32 rsaPrivKeySz,
  40486. byte* eccCert, word32 eccCertSz,
  40487. byte* eccPrivKey, word32 eccPrivKeySz)
  40488. {
  40489. wc_test_ret_t ret = 0;
  40490. int testSz = 0, i;
  40491. int envelopedSz, decodedSz;
  40492. byte *enveloped = NULL;
  40493. byte *decoded = NULL;
  40494. WC_RNG rng;
  40495. PKCS7* pkcs7;
  40496. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  40497. XFILE pkcs7File;
  40498. #endif
  40499. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  40500. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  40501. 0x72,0x6c,0x64
  40502. };
  40503. byte senderNonce[PKCS7_NONCE_SZ + 2];
  40504. #ifdef HAVE_ECC
  40505. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  40506. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  40507. WOLFSSL_SMALL_STACK_STATIC const byte senderNonceOid[] =
  40508. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  40509. 0x09, 0x05 };
  40510. PKCS7Attrib attribs[] =
  40511. {
  40512. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  40513. sizeof(senderNonce) }
  40514. };
  40515. #endif
  40516. #endif
  40517. #endif
  40518. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  40519. defined(WOLFSSL_SHA512) && defined(HAVE_AESGCM)
  40520. WOLFSSL_SMALL_STACK_STATIC const byte optionalUkm[] = {
  40521. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  40522. };
  40523. #endif /* NO_AES */
  40524. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  40525. /* encryption key for kekri recipient types */
  40526. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  40527. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  40528. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  40529. };
  40530. /* encryption key identifier */
  40531. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  40532. 0x02,0x02,0x03,0x04
  40533. };
  40534. #endif
  40535. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  40536. !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  40537. #ifndef HAVE_FIPS
  40538. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password";
  40539. #else
  40540. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  40541. #endif
  40542. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  40543. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  40544. };
  40545. #endif
  40546. #define MAX_TESTVECTORS_LEN 20
  40547. #define ADD_PKCS7AUTHENVELOPEDVECTOR(...) { \
  40548. pkcs7AuthEnvelopedVector _this_vector = { __VA_ARGS__ }; \
  40549. if (testSz == MAX_TESTVECTORS_LEN) { \
  40550. ret = WC_TEST_RET_ENC_NC; \
  40551. goto out; \
  40552. } \
  40553. XMEMCPY(&testVectors[testSz++], &_this_vector, \
  40554. sizeof _this_vector); \
  40555. }
  40556. pkcs7AuthEnvelopedVector *testVectors = NULL;
  40557. XMEMSET(&rng, 0, sizeof(rng));
  40558. testVectors = (pkcs7AuthEnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  40559. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40560. if (testVectors == NULL) {
  40561. ret = WC_TEST_RET_ENC_ERRNO;
  40562. goto out;
  40563. }
  40564. {
  40565. /* key transport key encryption technique */
  40566. #ifndef NO_RSA
  40567. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  40568. #ifdef WOLFSSL_AES_128
  40569. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40570. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, rsaCert, rsaCertSz,
  40571. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  40572. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  40573. 0, 0, "pkcs7authEnvelopedDataAES128GCM.der");
  40574. #endif
  40575. #ifdef WOLFSSL_AES_192
  40576. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40577. data, (word32)sizeof(data), DATA, AES192GCMb, 0, 0, rsaCert, rsaCertSz,
  40578. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  40579. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  40580. 0, 0, "pkcs7authEnvelopedDataAES192GCM.der");
  40581. #endif
  40582. #ifdef WOLFSSL_AES_256
  40583. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40584. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  40585. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  40586. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  40587. 0, 0, "pkcs7authEnvelopedDataAES256GCM.der");
  40588. /* test with contentType set to FirmwarePkgData */
  40589. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40590. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, 0, 0,
  40591. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL,
  40592. 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL,
  40593. 0, 0, 0, 0, 0, 0, 0, 0,
  40594. "pkcs7authEnvelopedDataAES256GCM_firmwarePkgData.der");
  40595. /* explicitly using SKID for SubjectKeyIdentifier */
  40596. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40597. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  40598. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, CMS_SKID, 0,
  40599. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  40600. 0, 0, 0, 0, 0, "pkcs7authEnvelopedDataAES256GCM_SKID.der");
  40601. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  40602. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40603. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  40604. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0,
  40605. CMS_ISSUER_AND_SERIAL_NUMBER, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  40606. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  40607. "pkcs7authEnvelopedDataAES256GCM_IANDS.der");
  40608. #endif
  40609. #else /* NO_AES || !HAVE_AESGCM */
  40610. (void)rsaCert;
  40611. (void)rsaCertSz;
  40612. (void)rsaPrivKey;
  40613. (void)rsaPrivKeySz;
  40614. #endif /* NO_AES || !HAVE_AESGCM */
  40615. #endif
  40616. /* key agreement key encryption technique*/
  40617. #ifdef HAVE_ECC
  40618. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  40619. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  40620. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40621. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP,
  40622. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  40623. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  40624. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  40625. "pkcs7authEnvelopedDataAES128GCM_ECDH_SHA1KDF.der");
  40626. #endif
  40627. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  40628. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40629. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  40630. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  40631. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  40632. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  40633. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF.der");
  40634. /* with authenticated attributes */
  40635. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40636. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  40637. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  40638. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  40639. NULL, 0, NULL, 0, 0, 0, NULL, 0,
  40640. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  40641. 0, 0, 0,
  40642. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_authAttribs.der");
  40643. /* with unauthenticated attributes */
  40644. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40645. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  40646. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  40647. eccPrivKeySz, NULL, 0, attribs,
  40648. (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0, NULL, 0,
  40649. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  40650. 0, 0, 0,
  40651. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_unauthAttribs.der");
  40652. /* with authenticated AND unauthenticated attributes */
  40653. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40654. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  40655. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  40656. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  40657. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  40658. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  40659. 0, 0, 0, 0, 0, 0,
  40660. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_bothAttribs.der");
  40661. /* with authenticated AND unauthenticated attributes AND
  40662. * contentType of FirmwarePkgData */
  40663. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40664. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, AES256_WRAP,
  40665. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  40666. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  40667. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  40668. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  40669. 0, 0, 0, 0, 0, 0,
  40670. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_fw_bothAttribs.der");
  40671. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  40672. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  40673. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40674. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  40675. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  40676. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL,
  40677. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  40678. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF.der");
  40679. /* with optional user keying material (ukm) */
  40680. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40681. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  40682. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  40683. eccPrivKeySz, NULL, 0, NULL, 0, (byte *)optionalUkm, sizeof(optionalUkm), 0,
  40684. 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  40685. 0, 0, 0, 0, 0, 0,
  40686. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF_ukm.der");
  40687. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  40688. #endif /* NO_AES */
  40689. #endif
  40690. /* kekri (KEKRecipientInfo) recipient types */
  40691. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  40692. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  40693. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40694. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP, 0,
  40695. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0,
  40696. (byte *)secretKey, sizeof(secretKey), (byte *)secretKeyId, sizeof(secretKeyId),
  40697. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  40698. "pkcs7authEnvelopedDataAES128GCM_KEKRI.der");
  40699. #endif
  40700. #endif
  40701. /* pwri (PasswordRecipientInfo) recipient types */
  40702. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM)
  40703. #if !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  40704. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40705. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0,
  40706. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  40707. NULL, 0, NULL, NULL, 0, NULL, 0, 0, (char *)password,
  40708. (word32)XSTRLEN(password), (byte *)salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  40709. AES128CBCb, 0, 0, 0, "pkcs7authEnvelopedDataAES128GCM_PWRI.der");
  40710. #endif
  40711. #endif
  40712. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  40713. #ifdef WOLFSSL_AES_128
  40714. /* ori (OtherRecipientInfo) recipient types */
  40715. ADD_PKCS7AUTHENVELOPEDVECTOR(
  40716. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, NULL, 0, NULL, 0,
  40717. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  40718. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 1, 0,
  40719. "pkcs7authEnvelopedDataAES128GCM_ORI.der");
  40720. #endif
  40721. #endif
  40722. }
  40723. #undef MAX_TESTVECTORS_LEN
  40724. #undef ADD_PKCS7AUTHENVELOPEDVECTOR
  40725. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40726. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40727. if ((! enveloped) || (! decoded)) {
  40728. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40729. }
  40730. /* generate senderNonce */
  40731. {
  40732. #ifndef HAVE_FIPS
  40733. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  40734. #else
  40735. ret = wc_InitRng(&rng);
  40736. #endif
  40737. if (ret != 0)
  40738. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40739. senderNonce[0] = 0x04;
  40740. senderNonce[1] = PKCS7_NONCE_SZ;
  40741. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  40742. if (ret != 0) {
  40743. wc_FreeRng(&rng);
  40744. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40745. }
  40746. }
  40747. for (i = 0; i < testSz; i++) {
  40748. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  40749. #ifdef WOLFSSL_ASYNC_CRYPT
  40750. INVALID_DEVID /* async PKCS7 is not supported */
  40751. #else
  40752. devId
  40753. #endif
  40754. );
  40755. if (pkcs7 == NULL) {
  40756. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  40757. }
  40758. if (testVectors[i].secretKey != NULL) {
  40759. /* KEKRI recipient type */
  40760. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  40761. if (ret != 0)
  40762. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40763. pkcs7->content = (byte*)testVectors[i].content;
  40764. pkcs7->contentSz = testVectors[i].contentSz;
  40765. pkcs7->contentOID = testVectors[i].contentOID;
  40766. pkcs7->encryptOID = testVectors[i].encryptOID;
  40767. pkcs7->ukm = testVectors[i].optionalUkm;
  40768. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  40769. pkcs7->authAttribs = testVectors[i].authAttribs;
  40770. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  40771. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  40772. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  40773. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  40774. testVectors[i].secretKey, testVectors[i].secretKeySz,
  40775. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  40776. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  40777. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  40778. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  40779. if (ret < 0) {
  40780. wc_PKCS7_Free(pkcs7);
  40781. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40782. }
  40783. /* set key, for decryption */
  40784. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  40785. testVectors[i].secretKeySz);
  40786. if (ret != 0) {
  40787. wc_PKCS7_Free(pkcs7);
  40788. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40789. }
  40790. } else if (testVectors[i].password != NULL) {
  40791. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  40792. /* PWRI recipient type */
  40793. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  40794. if (ret != 0)
  40795. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40796. pkcs7->content = (byte*)testVectors[i].content;
  40797. pkcs7->contentSz = testVectors[i].contentSz;
  40798. pkcs7->contentOID = testVectors[i].contentOID;
  40799. pkcs7->encryptOID = testVectors[i].encryptOID;
  40800. pkcs7->ukm = testVectors[i].optionalUkm;
  40801. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  40802. pkcs7->authAttribs = testVectors[i].authAttribs;
  40803. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  40804. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  40805. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  40806. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  40807. (byte*)testVectors[i].password,
  40808. testVectors[i].passwordSz, testVectors[i].salt,
  40809. testVectors[i].saltSz, testVectors[i].kdfOID,
  40810. testVectors[i].hashOID, testVectors[i].kdfIterations,
  40811. testVectors[i].kekEncryptOID, testVectors[i].pwriOptions);
  40812. if (ret < 0) {
  40813. wc_PKCS7_Free(pkcs7);
  40814. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40815. }
  40816. /* set password, for decryption */
  40817. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  40818. testVectors[i].passwordSz);
  40819. if (ret < 0) {
  40820. wc_PKCS7_Free(pkcs7);
  40821. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40822. }
  40823. #endif /* ! NO_PWDBASED && ! NO_SHA */
  40824. } else if (testVectors[i].isOri == 1) {
  40825. /* ORI recipient type */
  40826. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  40827. if (ret != 0)
  40828. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40829. pkcs7->content = (byte*)testVectors[i].content;
  40830. pkcs7->contentSz = testVectors[i].contentSz;
  40831. pkcs7->contentOID = testVectors[i].contentOID;
  40832. pkcs7->encryptOID = testVectors[i].encryptOID;
  40833. pkcs7->authAttribs = testVectors[i].authAttribs;
  40834. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  40835. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  40836. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  40837. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  40838. testVectors[i].oriOptions);
  40839. if (ret < 0) {
  40840. wc_PKCS7_Free(pkcs7);
  40841. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40842. }
  40843. /* set decrypt callback for decryption */
  40844. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  40845. if (ret < 0) {
  40846. wc_PKCS7_Free(pkcs7);
  40847. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40848. }
  40849. } else {
  40850. /* KTRI or KARI recipient types */
  40851. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  40852. (word32)testVectors[i].certSz);
  40853. if (ret != 0) {
  40854. wc_PKCS7_Free(pkcs7);
  40855. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40856. }
  40857. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  40858. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  40859. pkcs7->privateKey = testVectors[i].privateKey;
  40860. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  40861. pkcs7->content = (byte*)testVectors[i].content;
  40862. pkcs7->contentSz = testVectors[i].contentSz;
  40863. pkcs7->contentOID = testVectors[i].contentOID;
  40864. pkcs7->encryptOID = testVectors[i].encryptOID;
  40865. pkcs7->ukm = testVectors[i].optionalUkm;
  40866. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  40867. pkcs7->authAttribs = testVectors[i].authAttribs;
  40868. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  40869. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  40870. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  40871. /* set SubjectIdentifier type for KTRI types */
  40872. if (testVectors[i].ktriOptions & CMS_SKID) {
  40873. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  40874. if (ret != 0) {
  40875. wc_PKCS7_Free(pkcs7);
  40876. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40877. }
  40878. } else if (testVectors[i].ktriOptions &
  40879. CMS_ISSUER_AND_SERIAL_NUMBER) {
  40880. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  40881. CMS_ISSUER_AND_SERIAL_NUMBER);
  40882. if (ret != 0) {
  40883. wc_PKCS7_Free(pkcs7);
  40884. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40885. }
  40886. }
  40887. }
  40888. #ifdef ECC_TIMING_RESISTANT
  40889. pkcs7->rng = &rng;
  40890. #endif
  40891. /* encode envelopedData */
  40892. envelopedSz = wc_PKCS7_EncodeAuthEnvelopedData(pkcs7, enveloped,
  40893. PKCS7_BUF_SIZE);
  40894. if (envelopedSz <= 0) {
  40895. wc_PKCS7_Free(pkcs7);
  40896. ERROR_OUT(WC_TEST_RET_ENC_EC(envelopedSz), out);
  40897. }
  40898. #ifndef NO_PKCS7_STREAM
  40899. { /* test reading byte by byte */
  40900. int z;
  40901. for (z = 0; z < envelopedSz; z++) {
  40902. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7,
  40903. enveloped + z, 1, decoded, PKCS7_BUF_SIZE);
  40904. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  40905. printf("unexpected error %d\n", decodedSz);
  40906. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  40907. }
  40908. }
  40909. /* test decode result */
  40910. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  40911. printf("stream read compare failed\n");
  40912. wc_PKCS7_Free(pkcs7);
  40913. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40914. }
  40915. }
  40916. #endif
  40917. /* decode envelopedData */
  40918. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7, enveloped,
  40919. (word32)envelopedSz, decoded,
  40920. PKCS7_BUF_SIZE);
  40921. if (decodedSz <= 0) {
  40922. wc_PKCS7_Free(pkcs7);
  40923. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  40924. }
  40925. /* test decode result */
  40926. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  40927. wc_PKCS7_Free(pkcs7);
  40928. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40929. }
  40930. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  40931. /* output pkcs7 envelopedData for external testing */
  40932. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  40933. if (!pkcs7File) {
  40934. wc_PKCS7_Free(pkcs7);
  40935. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  40936. }
  40937. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  40938. XFCLOSE(pkcs7File);
  40939. if (ret != envelopedSz) {
  40940. wc_PKCS7_Free(pkcs7);
  40941. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  40942. } else {
  40943. /* reset ret to 0 for success */
  40944. ret = 0;
  40945. }
  40946. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  40947. wc_PKCS7_Free(pkcs7);
  40948. pkcs7 = NULL;
  40949. }
  40950. wc_FreeRng(&rng);
  40951. (void)eccCert;
  40952. (void)eccCertSz;
  40953. (void)eccPrivKey;
  40954. (void)eccPrivKeySz;
  40955. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  40956. (void)secretKey;
  40957. (void)secretKeyId;
  40958. #endif
  40959. #ifdef NO_RSA
  40960. (void)rsaCert;
  40961. (void)rsaCertSz;
  40962. (void)rsaPrivKey;
  40963. (void)rsaPrivKeySz;
  40964. #endif
  40965. out:
  40966. if (testVectors)
  40967. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40968. if (enveloped)
  40969. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40970. if (decoded)
  40971. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40972. return ret;
  40973. }
  40974. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7authenveloped_test(void)
  40975. {
  40976. wc_test_ret_t ret = 0;
  40977. byte* rsaCert = NULL;
  40978. byte* rsaPrivKey = NULL;
  40979. word32 rsaCertSz = 0;
  40980. word32 rsaPrivKeySz = 0;
  40981. byte* eccCert = NULL;
  40982. byte* eccPrivKey = NULL;
  40983. word32 eccCertSz = 0;
  40984. word32 eccPrivKeySz = 0;
  40985. WOLFSSL_ENTER("pkcs7authenveloped_test");
  40986. #ifndef NO_RSA
  40987. /* read client RSA cert and key in DER format */
  40988. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40989. if (rsaCert == NULL)
  40990. return WC_TEST_RET_ENC_ERRNO;
  40991. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40992. if (rsaPrivKey == NULL) {
  40993. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40994. return WC_TEST_RET_ENC_NC;
  40995. }
  40996. rsaCertSz = FOURK_BUF;
  40997. rsaPrivKeySz = FOURK_BUF;
  40998. #endif /* NO_RSA */
  40999. #ifdef HAVE_ECC
  41000. /* read client ECC cert and key in DER format */
  41001. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41002. if (eccCert == NULL) {
  41003. #ifndef NO_RSA
  41004. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41005. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41006. #endif
  41007. return WC_TEST_RET_ENC_NC;
  41008. }
  41009. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41010. if (eccPrivKey == NULL) {
  41011. #ifndef NO_RSA
  41012. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41013. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41014. #endif
  41015. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41016. return WC_TEST_RET_ENC_NC;
  41017. }
  41018. eccCertSz = FOURK_BUF;
  41019. eccPrivKeySz = FOURK_BUF;
  41020. #endif /* HAVE_ECC */
  41021. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  41022. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  41023. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  41024. eccPrivKey, &eccPrivKeySz);
  41025. if (ret < 0) {
  41026. #ifndef NO_RSA
  41027. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41028. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41029. #endif
  41030. #ifdef HAVE_ECC
  41031. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41032. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41033. #endif
  41034. return WC_TEST_RET_ENC_EC(ret);
  41035. }
  41036. ret = pkcs7authenveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  41037. rsaPrivKey, (word32)rsaPrivKeySz,
  41038. eccCert, (word32)eccCertSz,
  41039. eccPrivKey, (word32)eccPrivKeySz);
  41040. #ifndef NO_RSA
  41041. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41042. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41043. #endif
  41044. #ifdef HAVE_ECC
  41045. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41046. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41047. #endif
  41048. return ret;
  41049. }
  41050. #endif /* HAVE_AESGCM || HAVE_AESCCM */
  41051. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  41052. static const byte p7DefKey[] = {
  41053. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  41054. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  41055. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  41056. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  41057. };
  41058. static const byte p7AltKey[] = {
  41059. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  41060. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  41061. };
  41062. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  41063. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  41064. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  41065. {
  41066. wc_test_ret_t ret;
  41067. if (cek == NULL || out == NULL)
  41068. return BAD_FUNC_ARG;
  41069. /* test case sanity checks */
  41070. if (keyIdSz != 1) {
  41071. return WC_TEST_RET_ENC_NC;
  41072. }
  41073. if (keyId[0] != 0x00) {
  41074. return WC_TEST_RET_ENC_NC;
  41075. }
  41076. if (type != (int)PKCS7_KEKRI) {
  41077. return WC_TEST_RET_ENC_NC;
  41078. }
  41079. switch (keyWrapAlgo) {
  41080. case AES256_WRAP:
  41081. ret = wc_AesKeyUnWrap(p7DefKey, sizeof(p7DefKey), cek, cekSz,
  41082. out, outSz, NULL);
  41083. if (ret <= 0)
  41084. return (int)ret;
  41085. break;
  41086. default:
  41087. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  41088. return BAD_KEYWRAP_ALG_E;
  41089. };
  41090. (void)pkcs7;
  41091. (void)direction;
  41092. (void)orginKey; /* used with KAKRI */
  41093. (void)orginKeySz;
  41094. return (int)ret;
  41095. }
  41096. /* returns key size on success */
  41097. static wc_test_ret_t getFirmwareKey(PKCS7* pkcs7, byte* key, word32 keySz)
  41098. {
  41099. wc_test_ret_t ret;
  41100. word32 atrSz;
  41101. byte atr[256];
  41102. /* Additionally can look for fwWrappedFirmwareKey
  41103. * 1.2.840.113529.1.9.16.1.16 */
  41104. const unsigned char fwWrappedFirmwareKey[] = {
  41105. /* 0x06, 0x0B */
  41106. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  41107. 0x01, 0x09, 0x10, 0x02, 0x27
  41108. };
  41109. /* find keyID in fwWrappedFirmwareKey */
  41110. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  41111. sizeof(fwWrappedFirmwareKey), NULL, &atrSz);
  41112. if (ret == LENGTH_ONLY_E) {
  41113. XMEMSET(atr, 0, sizeof(atr));
  41114. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  41115. sizeof(fwWrappedFirmwareKey), atr, &atrSz);
  41116. /* keyIdRaw[0] OCTET TAG */
  41117. /* keyIdRaw[1] Length */
  41118. if (ret > 0) {
  41119. PKCS7* envPkcs7;
  41120. envPkcs7 = wc_PKCS7_New(NULL, 0);
  41121. if (envPkcs7 == NULL) {
  41122. return MEMORY_E;
  41123. }
  41124. wc_PKCS7_Init(envPkcs7, NULL, 0);
  41125. ret = wc_PKCS7_SetWrapCEKCb(envPkcs7, myCEKwrapFunc);
  41126. if (ret == 0) {
  41127. /* expecting FIRMWARE_PKG_DATA content */
  41128. envPkcs7->contentOID = FIRMWARE_PKG_DATA;
  41129. ret = wc_PKCS7_DecodeEnvelopedData(envPkcs7, atr, atrSz,
  41130. key, keySz);
  41131. if (envPkcs7->contentOID != FIRMWARE_PKG_DATA) {
  41132. /* the contentOID should have been set to the inner
  41133. * FIRMWARE_PKG_DATA content */
  41134. ret = BAD_STATE_E;
  41135. }
  41136. }
  41137. wc_PKCS7_Free(envPkcs7);
  41138. }
  41139. }
  41140. return ret;
  41141. }
  41142. /* create a KEKRI enveloped data
  41143. * return size on success */
  41144. static wc_test_ret_t envelopedData_encrypt(byte* in, word32 inSz, byte* out,
  41145. word32 outSz)
  41146. {
  41147. wc_test_ret_t ret;
  41148. PKCS7* pkcs7;
  41149. WOLFSSL_SMALL_STACK_STATIC const byte keyId[] = { 0x00 };
  41150. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  41151. if (pkcs7 == NULL)
  41152. return WC_TEST_RET_ENC_ERRNO;
  41153. pkcs7->content = in;
  41154. pkcs7->contentSz = inSz;
  41155. pkcs7->contentOID = FIRMWARE_PKG_DATA;
  41156. pkcs7->encryptOID = AES256CBCb;
  41157. pkcs7->ukm = NULL;
  41158. pkcs7->ukmSz = 0;
  41159. /* add recipient (KEKRI type) */
  41160. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP, (byte*)p7DefKey,
  41161. sizeof(p7DefKey), (byte*)keyId,
  41162. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0);
  41163. if (ret < 0) {
  41164. printf("wc_PKCS7_AddRecipient_KEKRI() failed\n");
  41165. wc_PKCS7_Free(pkcs7);
  41166. return WC_TEST_RET_ENC_EC(ret);
  41167. }
  41168. /* encode envelopedData, returns size */
  41169. ret = wc_PKCS7_EncodeEnvelopedData(pkcs7, out, outSz);
  41170. if (ret <= 0) {
  41171. printf("wc_PKCS7_EncodeEnvelopedData() failed\n");
  41172. wc_PKCS7_Free(pkcs7);
  41173. return WC_TEST_RET_ENC_EC(ret);
  41174. }
  41175. wc_PKCS7_Free(pkcs7);
  41176. return ret;
  41177. }
  41178. /*
  41179. * keyHint is the KeyID to be set in the fwDecryptKeyID attribute
  41180. * returns size of buffer output on success
  41181. */
  41182. static wc_test_ret_t generateBundle(byte* out, word32 *outSz, const byte* encryptKey,
  41183. word32 encryptKeySz, byte keyHint, byte* cert, word32 certSz,
  41184. byte* key, word32 keySz)
  41185. {
  41186. wc_test_ret_t ret;
  41187. int attribNum = 1;
  41188. PKCS7* pkcs7;
  41189. /* KEY ID
  41190. * fwDecryptKeyID OID 1.2.840.113549.1.9.16.2.37
  41191. */
  41192. const unsigned char fwDecryptKeyID[] = {
  41193. 0x06, 0x0B,
  41194. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  41195. 0x01, 0x09, 0x10, 0x02, 0x25
  41196. };
  41197. /* fwWrappedFirmwareKey 1.2.840.113529.1.9.16.1.16 */
  41198. const unsigned char fwWrappedFirmwareKey[] = {
  41199. 0x06, 0x0B, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  41200. 0x01, 0x09, 0x10, 0x02, 0x27
  41201. };
  41202. byte keyID[] = { 0x04, 0x01, 0x00 };
  41203. byte env[256];
  41204. char data[] = "Test of wolfSSL PKCS7 decrypt callback";
  41205. PKCS7Attrib attribs[] =
  41206. {
  41207. { fwDecryptKeyID, sizeof(fwDecryptKeyID), keyID, sizeof(keyID) },
  41208. { fwWrappedFirmwareKey, sizeof(fwWrappedFirmwareKey), env, 0 }
  41209. };
  41210. keyID[2] = keyHint;
  41211. /* If using keyHint 0 then create a bundle with fwWrappedFirmwareKey */
  41212. if (keyHint == 0) {
  41213. ret = envelopedData_encrypt((byte*)p7DefKey, sizeof(p7DefKey), env,
  41214. sizeof(env));
  41215. if (ret <= 0) {
  41216. return ret;
  41217. }
  41218. attribs[1].valueSz = (word32)ret;
  41219. attribNum++;
  41220. }
  41221. /* init PKCS7 */
  41222. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  41223. if (pkcs7 == NULL)
  41224. return WC_TEST_RET_ENC_ERRNO;
  41225. ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
  41226. if (ret != 0) {
  41227. printf("ERROR: wc_PKCS7_InitWithCert() failed, ret = %d\n", ret);
  41228. wc_PKCS7_Free(pkcs7);
  41229. return WC_TEST_RET_ENC_EC(ret);
  41230. }
  41231. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  41232. if (ret != 0) {
  41233. wc_PKCS7_Free(pkcs7);
  41234. return WC_TEST_RET_ENC_EC(ret);
  41235. }
  41236. /* encode Signed Encrypted FirmwarePkgData */
  41237. if (encryptKeySz == 16) {
  41238. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  41239. encryptKeySz, key, keySz, AES128CBCb, RSAk, SHA256h,
  41240. (byte*)data, sizeof(data), NULL, 0,
  41241. attribs, (word32)attribNum, out, *outSz);
  41242. }
  41243. else {
  41244. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  41245. encryptKeySz, key, keySz, AES256CBCb, RSAk, SHA256h,
  41246. (byte*)data, sizeof(data), NULL, 0,
  41247. attribs, (word32)attribNum, out, *outSz);
  41248. }
  41249. if (ret <= 0) {
  41250. printf("ERROR: wc_PKCS7_EncodeSignedEncryptedFPD() failed, "
  41251. "ret = %d\n", ret);
  41252. wc_PKCS7_Free(pkcs7);
  41253. return WC_TEST_RET_ENC_EC(ret);
  41254. } else {
  41255. *outSz = (word32)ret;
  41256. }
  41257. wc_PKCS7_Free(pkcs7);
  41258. return ret;
  41259. }
  41260. /* test verification and decryption of PKCS7 bundle
  41261. * return 0 on success
  41262. */
  41263. static wc_test_ret_t verifyBundle(byte* derBuf, word32 derSz, int keyHint)
  41264. {
  41265. wc_test_ret_t ret = 0;
  41266. int usrCtx = 1; /* test value to pass as user context to callback */
  41267. PKCS7* pkcs7 = NULL;
  41268. byte* sid = NULL;
  41269. word32 sidSz;
  41270. byte key[256];
  41271. word32 keySz = sizeof(key);
  41272. byte *decoded = NULL;
  41273. int decodedSz = FOURK_BUF/2;
  41274. WOLFSSL_SMALL_STACK_STATIC const byte expectedSid[] = {
  41275. #ifdef NO_SHA
  41276. #ifdef USE_CERT_BUFFERS_1024
  41277. 0x70, 0xe7, 0x79, 0x60, 0x8f, 0x41, 0xdc, 0xe9,
  41278. 0xad, 0x8b, 0x3d, 0x0c, 0x20, 0xf4, 0xc3, 0xf2,
  41279. 0x8e, 0x05, 0xe8, 0xa1, 0xb6, 0x68, 0x74, 0x06,
  41280. 0xbc, 0xe7, 0xc5, 0x3c, 0x13, 0x99, 0x79, 0xb9
  41281. #else
  41282. 0xce, 0x06, 0x07, 0xbe, 0xf1, 0xa6, 0x1e, 0x36,
  41283. 0xef, 0xfa, 0xbc, 0x89, 0x71, 0xf3, 0x23, 0x9e,
  41284. 0x34, 0x6d, 0xae, 0x86, 0xae, 0x2b, 0xdc, 0xf4,
  41285. 0x4a, 0x27, 0xd5, 0x63, 0x59, 0x4f, 0x4a, 0x71
  41286. #endif
  41287. #else /* !NO_SHA */
  41288. #ifdef USE_CERT_BUFFERS_1024
  41289. 0x81, 0x69, 0x0f, 0xf8, 0xdf, 0xdd, 0xcf, 0x34,
  41290. 0x29, 0xd5, 0x67, 0x75, 0x71, 0x85, 0xc7, 0x75,
  41291. 0x10, 0x69, 0x59, 0xec,
  41292. #else
  41293. 0x33, 0xD8, 0x45, 0x66, 0xD7, 0x68, 0x87, 0x18,
  41294. 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26,
  41295. 0xD7, 0x85, 0x65, 0xC0
  41296. #endif
  41297. #endif /* !NO_SHA */
  41298. };
  41299. decoded = (byte *)XMALLOC(decodedSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41300. if (decoded == NULL) {
  41301. ret = MEMORY_E;
  41302. goto out;
  41303. }
  41304. pkcs7 = wc_PKCS7_New(HEAP_HINT, INVALID_DEVID);
  41305. if (pkcs7 == NULL) {
  41306. ret = MEMORY_E;
  41307. goto out;
  41308. }
  41309. /* Test verify */
  41310. ret = wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID);
  41311. if (ret != 0)
  41312. goto out;
  41313. ret = wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  41314. if (ret != 0)
  41315. goto out;
  41316. ret = wc_PKCS7_VerifySignedData(pkcs7, derBuf, derSz);
  41317. if (ret != 0)
  41318. goto out;
  41319. /* Get size of SID and print it out */
  41320. ret = wc_PKCS7_GetSignerSID(pkcs7, NULL, &sidSz);
  41321. if (ret != LENGTH_ONLY_E)
  41322. goto out;
  41323. sid = (byte*)XMALLOC(sidSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41324. if (sid == NULL) {
  41325. ret = MEMORY_E;
  41326. goto out;
  41327. }
  41328. ret = wc_PKCS7_GetSignerSID(pkcs7, sid, &sidSz);
  41329. if (ret != 0)
  41330. goto out;
  41331. ret = XMEMCMP(sid, expectedSid, sidSz);
  41332. if (ret != 0) {
  41333. ret = PKCS7_NO_SIGNER_E; /* close enough */
  41334. goto out;
  41335. }
  41336. /* get expected fwWrappedFirmwareKey */
  41337. if (keyHint == 0) {
  41338. ret = getFirmwareKey(pkcs7, key, keySz);
  41339. if (ret < 0)
  41340. goto out;
  41341. pkcs7->encryptionKey = key;
  41342. pkcs7->encryptionKeySz = (word32)ret;
  41343. }
  41344. else {
  41345. decodedSz = PKCS7_BUF_SIZE;
  41346. ret = wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc);
  41347. if (ret != 0)
  41348. goto out;
  41349. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)&usrCtx);
  41350. if (ret != 0)
  41351. goto out;
  41352. }
  41353. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  41354. pkcs7->contentSz, decoded, (word32)decodedSz);
  41355. if (decodedSz < 0) {
  41356. ret = decodedSz;
  41357. goto out;
  41358. }
  41359. ret = 0;
  41360. out:
  41361. if (decoded)
  41362. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41363. if (pkcs7)
  41364. wc_PKCS7_Free(pkcs7);
  41365. if (sid)
  41366. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41367. return ret;
  41368. }
  41369. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7callback_test(byte* cert, word32 certSz, byte* key, word32 keySz)
  41370. {
  41371. wc_test_ret_t ret = 0;
  41372. word32 derSz;
  41373. byte *derBuf = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41374. WOLFSSL_ENTER("pkcs7callback_test");
  41375. if (! derBuf)
  41376. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  41377. /* Doing default generation and verify */
  41378. derSz = FOURK_BUF;
  41379. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 0, cert,
  41380. certSz, key, keySz);
  41381. if (ret <= 0) {
  41382. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  41383. }
  41384. ret = verifyBundle(derBuf, derSz, 0);
  41385. if (ret != 0)
  41386. ERROR_OUT(ret, out);
  41387. /* test choosing other key with keyID */
  41388. derSz = FOURK_BUF;
  41389. ret = generateBundle(derBuf, &derSz, p7AltKey, sizeof(p7AltKey), 1,
  41390. cert, certSz, key, keySz);
  41391. if (ret <= 0) {
  41392. ERROR_OUT(ret, out);
  41393. }
  41394. ret = verifyBundle(derBuf, derSz, 1);
  41395. if (ret != 0)
  41396. ERROR_OUT(ret, out);
  41397. /* test fail case with wrong keyID */
  41398. derSz = FOURK_BUF;
  41399. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 1,
  41400. cert, certSz, key, keySz);
  41401. if (ret <= 0) {
  41402. ERROR_OUT(ret, out);
  41403. }
  41404. ret = verifyBundle(derBuf, derSz, 1);
  41405. if (ret == 0) {
  41406. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  41407. }
  41408. ret = 0;
  41409. out:
  41410. if (derBuf)
  41411. XFREE(derBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41412. return ret;
  41413. }
  41414. #endif /* !NO_AES && HAVE_AES_CBC */
  41415. #ifndef NO_PKCS7_ENCRYPTED_DATA
  41416. typedef struct {
  41417. const byte* content;
  41418. word32 contentSz;
  41419. int contentOID;
  41420. int encryptOID;
  41421. byte* encryptionKey;
  41422. word32 encryptionKeySz;
  41423. PKCS7Attrib* attribs;
  41424. word32 attribsSz;
  41425. const char* outFileName;
  41426. } pkcs7EncryptedVector;
  41427. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7encrypted_test(void)
  41428. {
  41429. wc_test_ret_t ret = 0;
  41430. int i, testSz;
  41431. int encryptedSz, decodedSz, attribIdx;
  41432. PKCS7* pkcs7;
  41433. byte *encrypted;
  41434. byte *decoded;
  41435. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  41436. XFILE pkcs7File;
  41437. #endif
  41438. PKCS7Attrib* expectedAttrib;
  41439. PKCS7DecodedAttrib* decodedAttrib;
  41440. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  41441. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  41442. 0x72,0x6c,0x64
  41443. };
  41444. #ifndef NO_DES3
  41445. byte desKey[] = {
  41446. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  41447. };
  41448. byte des3Key[] = {
  41449. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  41450. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  41451. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  41452. };
  41453. #endif
  41454. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  41455. #ifdef WOLFSSL_AES_128
  41456. byte aes128Key[] = {
  41457. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  41458. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  41459. };
  41460. #endif
  41461. #ifdef WOLFSSL_AES_192
  41462. byte aes192Key[] = {
  41463. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  41464. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  41465. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  41466. };
  41467. #endif
  41468. #ifdef WOLFSSL_AES_256
  41469. byte aes256Key[] = {
  41470. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  41471. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  41472. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  41473. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  41474. };
  41475. #endif
  41476. #ifdef WOLFSSL_AES_256
  41477. /* Attribute example from RFC 4134, Section 7.2
  41478. * OID = 1.2.5555
  41479. * OCTET STRING = 'This is a test General ASN Attribute, number 1.' */
  41480. static const byte genAttrOid[] = { 0x06, 0x03, 0x2a, 0xab, 0x33 };
  41481. static const byte genAttr[] = { 0x04, 47,
  41482. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  41483. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  41484. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  41485. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  41486. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  41487. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x31, 0x2e };
  41488. static const byte genAttrOid2[] = { 0x06, 0x03, 0x2a, 0xab, 0x34 };
  41489. static const byte genAttr2[] = { 0x04, 47,
  41490. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  41491. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  41492. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  41493. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  41494. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  41495. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x32, 0x2e };
  41496. PKCS7Attrib attribs[] =
  41497. {
  41498. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) }
  41499. };
  41500. PKCS7Attrib multiAttribs[] =
  41501. {
  41502. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) },
  41503. { genAttrOid2, sizeof(genAttrOid2), genAttr2, sizeof(genAttr2) }
  41504. };
  41505. #endif
  41506. #endif /* NO_AES */
  41507. const pkcs7EncryptedVector testVectors[] =
  41508. {
  41509. #ifndef NO_DES3
  41510. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key),
  41511. NULL, 0, "pkcs7encryptedDataDES3.der"},
  41512. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey),
  41513. NULL, 0, "pkcs7encryptedDataDES.der"},
  41514. #endif /* NO_DES3 */
  41515. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  41516. #ifdef WOLFSSL_AES_128
  41517. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  41518. sizeof(aes128Key), NULL, 0, "pkcs7encryptedDataAES128CBC.der"},
  41519. #endif
  41520. #ifdef WOLFSSL_AES_192
  41521. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  41522. sizeof(aes192Key), NULL, 0, "pkcs7encryptedDataAES192CBC.der"},
  41523. #endif
  41524. #ifdef WOLFSSL_AES_256
  41525. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  41526. sizeof(aes256Key), NULL, 0, "pkcs7encryptedDataAES256CBC.der"},
  41527. /* test with optional unprotected attributes */
  41528. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  41529. sizeof(aes256Key), attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41530. "pkcs7encryptedDataAES256CBC_attribs.der"},
  41531. /* test with multiple optional unprotected attributes */
  41532. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  41533. sizeof(aes256Key), multiAttribs,
  41534. (sizeof(multiAttribs)/sizeof(PKCS7Attrib)),
  41535. "pkcs7encryptedDataAES256CBC_multi_attribs.der"},
  41536. /* test with contentType set to FirmwarePkgData */
  41537. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256CBCb, aes256Key,
  41538. sizeof(aes256Key), NULL, 0,
  41539. "pkcs7encryptedDataAES256CBC_firmwarePkgData.der"},
  41540. #endif
  41541. #endif /* !NO_AES && HAVE_AES_CBC */
  41542. };
  41543. WOLFSSL_ENTER("pkcs7encrypted_test");
  41544. encrypted = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41545. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41546. if ((! encrypted) || (! decoded)) {
  41547. ERROR_OUT(MEMORY_E, out);
  41548. }
  41549. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  41550. for (i = 0; i < testSz; i++) {
  41551. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  41552. if (pkcs7 == NULL) {
  41553. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  41554. }
  41555. pkcs7->content = (byte*)testVectors[i].content;
  41556. pkcs7->contentSz = testVectors[i].contentSz;
  41557. pkcs7->contentOID = testVectors[i].contentOID;
  41558. pkcs7->encryptOID = testVectors[i].encryptOID;
  41559. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  41560. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  41561. pkcs7->unprotectedAttribs = testVectors[i].attribs;
  41562. pkcs7->unprotectedAttribsSz = testVectors[i].attribsSz;
  41563. /* encode encryptedData */
  41564. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  41565. PKCS7_BUF_SIZE);
  41566. if (encryptedSz <= 0) {
  41567. wc_PKCS7_Free(pkcs7);
  41568. ERROR_OUT(WC_TEST_RET_ENC_EC(encryptedSz), out);
  41569. }
  41570. /* decode encryptedData */
  41571. #ifndef NO_PKCS7_STREAM
  41572. { /* test reading byte by byte */
  41573. int z;
  41574. for (z = 0; z < encryptedSz; z++) {
  41575. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted + z, 1,
  41576. decoded, PKCS7_BUF_SIZE);
  41577. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  41578. printf("unexpected error %d\n", decodedSz);
  41579. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  41580. }
  41581. }
  41582. /* test decode result */
  41583. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  41584. printf("stream read failed\n");
  41585. wc_PKCS7_Free(pkcs7);
  41586. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  41587. }
  41588. }
  41589. #endif
  41590. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  41591. decoded, PKCS7_BUF_SIZE);
  41592. if (decodedSz <= 0){
  41593. wc_PKCS7_Free(pkcs7);
  41594. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  41595. }
  41596. /* test decode result */
  41597. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  41598. wc_PKCS7_Free(pkcs7);
  41599. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  41600. }
  41601. /* verify decoded unprotected attributes */
  41602. if (pkcs7->decodedAttrib != NULL) {
  41603. decodedAttrib = pkcs7->decodedAttrib;
  41604. attribIdx = 1;
  41605. while (decodedAttrib != NULL) {
  41606. /* expected attribute, stored list is reversed */
  41607. expectedAttrib = &(pkcs7->unprotectedAttribs
  41608. [pkcs7->unprotectedAttribsSz - attribIdx]);
  41609. /* verify oid */
  41610. if (XMEMCMP(decodedAttrib->oid, expectedAttrib->oid,
  41611. decodedAttrib->oidSz) != 0) {
  41612. wc_PKCS7_Free(pkcs7);
  41613. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  41614. }
  41615. /* verify value */
  41616. if (XMEMCMP(decodedAttrib->value, expectedAttrib->value,
  41617. decodedAttrib->valueSz) != 0) {
  41618. wc_PKCS7_Free(pkcs7);
  41619. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  41620. }
  41621. decodedAttrib = decodedAttrib->next;
  41622. attribIdx++;
  41623. }
  41624. }
  41625. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  41626. /* output pkcs7 envelopedData for external testing */
  41627. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  41628. if (!pkcs7File) {
  41629. wc_PKCS7_Free(pkcs7);
  41630. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  41631. }
  41632. ret = (int)XFWRITE(encrypted, encryptedSz, 1, pkcs7File);
  41633. if (ret < 0)
  41634. ret = WC_TEST_RET_ENC_ERRNO;
  41635. else
  41636. ret = 0;
  41637. XFCLOSE(pkcs7File);
  41638. #endif
  41639. wc_PKCS7_Free(pkcs7);
  41640. }
  41641. out:
  41642. if (encrypted)
  41643. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41644. if (decoded)
  41645. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41646. return ret;
  41647. }
  41648. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  41649. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  41650. typedef struct {
  41651. const byte* content;
  41652. word32 contentSz;
  41653. int contentOID;
  41654. const char* outFileName;
  41655. } pkcs7CompressedVector;
  41656. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7compressed_test(void)
  41657. {
  41658. wc_test_ret_t ret = 0;
  41659. int i, testSz;
  41660. int compressedSz, decodedSz;
  41661. PKCS7* pkcs7;
  41662. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  41663. byte *compressed;
  41664. byte *decoded;
  41665. #else
  41666. byte compressed[PKCS7_BUF_SIZE];
  41667. byte decoded[PKCS7_BUF_SIZE];
  41668. #endif
  41669. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  41670. XFILE pkcs7File;
  41671. #endif
  41672. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  41673. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  41674. 0x72,0x6c,0x64
  41675. };
  41676. const pkcs7CompressedVector testVectors[] =
  41677. {
  41678. {data, (word32)sizeof(data), DATA,
  41679. "pkcs7compressedData_data_zlib.der"},
  41680. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA,
  41681. "pkcs7compressedData_firmwarePkgData_zlib.der"},
  41682. };
  41683. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  41684. compressed = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41685. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41686. if ((! compressed) || (! decoded)) {
  41687. ERROR_OUT(MEMORY_E, out);
  41688. }
  41689. #endif
  41690. WOLFSSL_ENTER("pkcs7compressed_test");
  41691. testSz = sizeof(testVectors) / sizeof(pkcs7CompressedVector);
  41692. for (i = 0; i < testSz; i++) {
  41693. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  41694. if (pkcs7 == NULL) {
  41695. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  41696. }
  41697. pkcs7->content = (byte*)testVectors[i].content;
  41698. pkcs7->contentSz = testVectors[i].contentSz;
  41699. pkcs7->contentOID = testVectors[i].contentOID;
  41700. /* encode compressedData */
  41701. compressedSz = wc_PKCS7_EncodeCompressedData(pkcs7, compressed,
  41702. PKCS7_BUF_SIZE);
  41703. if (compressedSz <= 0) {
  41704. wc_PKCS7_Free(pkcs7);
  41705. ERROR_OUT(WC_TEST_RET_ENC_EC(compressedSz), out);
  41706. }
  41707. /* decode compressedData */
  41708. decodedSz = wc_PKCS7_DecodeCompressedData(pkcs7, compressed,
  41709. compressedSz, decoded,
  41710. PKCS7_BUF_SIZE);
  41711. if (decodedSz <= 0){
  41712. wc_PKCS7_Free(pkcs7);
  41713. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  41714. }
  41715. /* test decode result */
  41716. if (XMEMCMP(decoded, testVectors[i].content,
  41717. testVectors[i].contentSz) != 0) {
  41718. wc_PKCS7_Free(pkcs7);
  41719. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  41720. }
  41721. /* make sure content type is the same */
  41722. if (testVectors[i].contentOID != pkcs7->contentOID) {
  41723. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  41724. }
  41725. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  41726. /* output pkcs7 compressedData for external testing */
  41727. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  41728. if (!pkcs7File) {
  41729. wc_PKCS7_Free(pkcs7);
  41730. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  41731. }
  41732. ret = (int)XFWRITE(compressed, compressedSz, 1, pkcs7File);
  41733. if (ret < 0)
  41734. ret = WC_TEST_RET_ENC_ERRNO;
  41735. else
  41736. ret = 0;
  41737. XFCLOSE(pkcs7File);
  41738. #endif
  41739. wc_PKCS7_Free(pkcs7);
  41740. }
  41741. out:
  41742. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  41743. if (compressed)
  41744. XFREE(compressed, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41745. if (decoded)
  41746. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41747. #endif
  41748. return ret;
  41749. } /* pkcs7compressed_test() */
  41750. #undef PKCS7_BUF_SIZE
  41751. #endif /* HAVE_LIBZ */
  41752. typedef struct {
  41753. const byte* content;
  41754. word32 contentSz;
  41755. int hashOID;
  41756. int signOID;
  41757. byte* privateKey;
  41758. word32 privateKeySz;
  41759. byte* cert;
  41760. size_t certSz;
  41761. byte* caCert;
  41762. size_t caCertSz;
  41763. PKCS7Attrib* signedAttribs;
  41764. word32 signedAttribsSz;
  41765. const char* outFileName;
  41766. int contentOID;
  41767. const byte* contentType;
  41768. word32 contentTypeSz;
  41769. int sidType;
  41770. int encryptOID; /* for single-shot encrypt alg OID */
  41771. int encCompFlag; /* for single-shot. 1 = enc, 2 = comp, 3 = both*/
  41772. const byte* encryptKey; /* for single-shot, encryptedData */
  41773. word32 encryptKeySz; /* for single-shot, encryptedData */
  41774. PKCS7Attrib* unprotectedAttribs; /* for single-shot, encryptedData */
  41775. word32 unprotectedAttribsSz; /* for single-shot, encryptedData */
  41776. word16 detachedSignature; /* generate detached signature (0:1) */
  41777. } pkcs7SignedVector;
  41778. static wc_test_ret_t pkcs7signed_run_vectors(
  41779. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  41780. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  41781. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  41782. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  41783. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  41784. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  41785. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  41786. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  41787. {
  41788. wc_test_ret_t ret;
  41789. int testSz = 0, i;
  41790. int encodedSz;
  41791. byte* out = NULL;
  41792. word32 outSz;
  41793. WC_RNG rng;
  41794. PKCS7* pkcs7 = NULL;
  41795. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  41796. XFILE file;
  41797. #endif
  41798. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  41799. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  41800. 0x72,0x6c,0x64
  41801. };
  41802. static const byte transIdOid[] =
  41803. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  41804. 0x09, 0x07 };
  41805. static const byte messageTypeOid[] =
  41806. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  41807. 0x09, 0x02 };
  41808. static const byte senderNonceOid[] =
  41809. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  41810. 0x09, 0x05 };
  41811. #ifndef NO_SHA
  41812. byte transId[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  41813. #else
  41814. byte transId[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  41815. #endif
  41816. static const byte messageType[] = { 0x13, 2, '1', '9' };
  41817. byte senderNonce[PKCS7_NONCE_SZ + 2];
  41818. PKCS7Attrib attribs[] =
  41819. {
  41820. { transIdOid, sizeof(transIdOid), transId,
  41821. sizeof(transId) - 1 }, /* take off the null */
  41822. { messageTypeOid, sizeof(messageTypeOid), messageType,
  41823. sizeof(messageType) },
  41824. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  41825. sizeof(senderNonce) }
  41826. };
  41827. /* for testing custom contentType, FirmwarePkgData */
  41828. static const byte customContentType[] = { 0x06, 0x0B, 0x2A, 0x86,
  41829. 0x48, 0x86, 0xF7, 0x0D,
  41830. 0x01, 0x09, 0x10, 0x01, 0x10 };
  41831. #define MAX_TESTVECTORS_LEN 20
  41832. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  41833. const pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  41834. if (testSz == MAX_TESTVECTORS_LEN) { \
  41835. ret = WC_TEST_RET_ENC_NC; \
  41836. goto out; \
  41837. } \
  41838. XMEMCPY(&testVectors[testSz++], &_this_vector, \
  41839. sizeof _this_vector); \
  41840. }
  41841. pkcs7SignedVector *testVectors = NULL;
  41842. XMEMSET(&rng, 0, sizeof(rng));
  41843. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  41844. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41845. if (testVectors == NULL) {
  41846. ret = WC_TEST_RET_ENC_ERRNO;
  41847. goto out;
  41848. }
  41849. {
  41850. #ifndef NO_RSA
  41851. #ifndef NO_SHA
  41852. /* RSA with SHA */
  41853. ADD_PKCS7SIGNEDVECTOR(
  41854. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  41855. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  41856. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41857. "pkcs7signedData_RSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0, NULL,
  41858. 0, 0);
  41859. /* RSA with SHA, no signed attributes */
  41860. ADD_PKCS7SIGNEDVECTOR(
  41861. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  41862. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz,
  41863. NULL, 0, NULL, 0,
  41864. "pkcs7signedData_RSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  41865. NULL, 0, 0);
  41866. #endif
  41867. #ifdef WOLFSSL_SHA224
  41868. /* RSA with SHA224 */
  41869. ADD_PKCS7SIGNEDVECTOR(
  41870. data, (word32)sizeof(data), SHA224h, RSAk, rsaClientPrivKeyBuf,
  41871. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  41872. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41873. "pkcs7signedData_RSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  41874. NULL, 0, 0);
  41875. #endif
  41876. #ifndef NO_SHA256
  41877. /* RSA with SHA256 */
  41878. ADD_PKCS7SIGNEDVECTOR(
  41879. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  41880. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  41881. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41882. "pkcs7signedData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  41883. NULL, 0, 0);
  41884. /* RSA with SHA256, detached signature */
  41885. ADD_PKCS7SIGNEDVECTOR(
  41886. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  41887. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  41888. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41889. "pkcs7signedData_RSA_SHA256_detachedSig.der", 0, NULL, 0, 0, 0, 0,
  41890. NULL, 0, NULL, 0, 1);
  41891. /* RSA with SHA256 and SubjectKeyIdentifier in SignerIdentifier */
  41892. ADD_PKCS7SIGNEDVECTOR(
  41893. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  41894. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  41895. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41896. "pkcs7signedData_RSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  41897. NULL, 0, NULL, 0, 0);
  41898. /* RSA with SHA256 and custom contentType */
  41899. ADD_PKCS7SIGNEDVECTOR(
  41900. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  41901. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  41902. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41903. "pkcs7signedData_RSA_SHA256_custom_contentType.der", 0,
  41904. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  41905. NULL, 0, 0);
  41906. /* RSA with SHA256 and FirmwarePkgData contentType */
  41907. ADD_PKCS7SIGNEDVECTOR(
  41908. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  41909. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  41910. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41911. "pkcs7signedData_RSA_SHA256_firmwarePkgData.der",
  41912. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  41913. /* RSA with SHA256 using server cert and ca cert */
  41914. ADD_PKCS7SIGNEDVECTOR(
  41915. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  41916. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  41917. rsaCaCertBuf, rsaCaCertBufSz,
  41918. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41919. "pkcs7signedData_RSA_SHA256_with_ca_cert.der", 0, NULL, 0, 0, 0, 0,
  41920. NULL, 0, NULL, 0, 0);
  41921. #endif
  41922. #if defined(WOLFSSL_SHA384)
  41923. /* RSA with SHA384 */
  41924. ADD_PKCS7SIGNEDVECTOR(
  41925. data, (word32)sizeof(data), SHA384h, RSAk, rsaClientPrivKeyBuf,
  41926. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  41927. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41928. "pkcs7signedData_RSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  41929. NULL, 0, 0);
  41930. #endif
  41931. #if defined(WOLFSSL_SHA512)
  41932. /* RSA with SHA512 */
  41933. ADD_PKCS7SIGNEDVECTOR(
  41934. data, (word32)sizeof(data), SHA512h, RSAk, rsaClientPrivKeyBuf,
  41935. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  41936. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41937. "pkcs7signedData_RSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  41938. NULL, 0, 0);
  41939. #endif
  41940. #endif /* NO_RSA */
  41941. #ifdef HAVE_ECC
  41942. #ifndef NO_SHA
  41943. /* ECDSA with SHA */
  41944. ADD_PKCS7SIGNEDVECTOR(
  41945. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  41946. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  41947. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41948. "pkcs7signedData_ECDSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  41949. NULL, 0, 0);
  41950. /* ECDSA with SHA, no signed attributes */
  41951. ADD_PKCS7SIGNEDVECTOR(
  41952. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  41953. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz,
  41954. NULL, 0, NULL, 0,
  41955. "pkcs7signedData_ECDSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  41956. NULL, 0, 0);
  41957. #endif
  41958. #ifdef WOLFSSL_SHA224
  41959. /* ECDSA with SHA224 */
  41960. ADD_PKCS7SIGNEDVECTOR(
  41961. data, (word32)sizeof(data), SHA224h, ECDSAk, eccClientPrivKeyBuf,
  41962. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  41963. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41964. "pkcs7signedData_ECDSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  41965. NULL, 0, 0);
  41966. #endif
  41967. #ifndef NO_SHA256
  41968. /* ECDSA with SHA256 */
  41969. ADD_PKCS7SIGNEDVECTOR(
  41970. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  41971. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  41972. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41973. "pkcs7signedData_ECDSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  41974. NULL, 0, 0);
  41975. /* ECDSA with SHA256 and SubjectKeyIdentifier in SigherIdentifier */
  41976. ADD_PKCS7SIGNEDVECTOR(
  41977. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  41978. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  41979. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41980. "pkcs7signedData_ECDSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  41981. NULL, 0, NULL, 0, 0);
  41982. /* ECDSA with SHA256 and custom contentType */
  41983. ADD_PKCS7SIGNEDVECTOR(
  41984. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  41985. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  41986. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41987. "pkcs7signedData_ECDSA_SHA256_custom_contentType.der", 0,
  41988. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  41989. NULL, 0, 0);
  41990. /* ECDSA with SHA256 and FirmwarePkgData contentType */
  41991. ADD_PKCS7SIGNEDVECTOR(
  41992. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  41993. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  41994. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  41995. "pkcs7signedData_ECDSA_SHA256_firmwarePkgData.der",
  41996. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  41997. #endif
  41998. #ifdef WOLFSSL_SHA384
  41999. /* ECDSA with SHA384 */
  42000. ADD_PKCS7SIGNEDVECTOR(
  42001. data, (word32)sizeof(data), SHA384h, ECDSAk, eccClientPrivKeyBuf,
  42002. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  42003. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  42004. "pkcs7signedData_ECDSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  42005. NULL, 0, 0);
  42006. #endif
  42007. #ifdef WOLFSSL_SHA512
  42008. /* ECDSA with SHA512 */
  42009. ADD_PKCS7SIGNEDVECTOR(
  42010. data, (word32)sizeof(data), SHA512h, ECDSAk, eccClientPrivKeyBuf,
  42011. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  42012. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  42013. "pkcs7signedData_ECDSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  42014. NULL, 0, 0);
  42015. #endif
  42016. #endif /* HAVE_ECC */
  42017. };
  42018. #undef MAX_TESTVECTORS_LEN
  42019. #undef ADD_PKCS7SIGNEDVECTOR
  42020. outSz = FOURK_BUF;
  42021. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42022. if (out == NULL)
  42023. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42024. XMEMSET(out, 0, outSz);
  42025. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  42026. if (ret < 0)
  42027. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42028. #ifndef HAVE_FIPS
  42029. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  42030. #else
  42031. ret = wc_InitRng(&rng);
  42032. #endif
  42033. if (ret != 0)
  42034. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42035. for (i = 0; i < testSz; i++) {
  42036. if (pkcs7)
  42037. wc_PKCS7_Free(pkcs7);
  42038. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  42039. if (pkcs7 == NULL)
  42040. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42041. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  42042. (word32)testVectors[i].certSz);
  42043. if (ret != 0)
  42044. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42045. /* load CA certificate, if present */
  42046. if (testVectors[i].caCert != NULL) {
  42047. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  42048. (word32)testVectors[i].caCertSz);
  42049. if (ret != 0)
  42050. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42051. }
  42052. pkcs7->rng = &rng;
  42053. pkcs7->content = (byte*)testVectors[i].content;
  42054. pkcs7->contentSz = testVectors[i].contentSz;
  42055. pkcs7->contentOID = testVectors[i].contentOID;
  42056. pkcs7->hashOID = testVectors[i].hashOID;
  42057. pkcs7->encryptOID = testVectors[i].signOID;
  42058. pkcs7->privateKey = testVectors[i].privateKey;
  42059. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  42060. pkcs7->signedAttribs = testVectors[i].signedAttribs;
  42061. pkcs7->signedAttribsSz = testVectors[i].signedAttribsSz;
  42062. /* optional custom contentType, default is DATA,
  42063. overrides contentOID if set */
  42064. if (testVectors[i].contentType != NULL) {
  42065. ret = wc_PKCS7_SetContentType(pkcs7,
  42066. (byte *)testVectors[i].contentType,
  42067. testVectors[i].contentTypeSz);
  42068. if (ret != 0)
  42069. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42070. }
  42071. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  42072. default is IssuerAndSerialNumber */
  42073. if (testVectors[i].sidType == CMS_SKID) {
  42074. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  42075. if (ret != 0)
  42076. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42077. }
  42078. /* generate senderNonce */
  42079. {
  42080. senderNonce[0] = 0x04;
  42081. senderNonce[1] = PKCS7_NONCE_SZ;
  42082. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  42083. if (ret != 0)
  42084. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42085. }
  42086. /* generate transactionID (used with SCEP) */
  42087. {
  42088. #ifndef NO_SHA
  42089. wc_Sha sha;
  42090. byte digest[WC_SHA_DIGEST_SIZE];
  42091. #else
  42092. wc_Sha256 sha;
  42093. byte digest[WC_SHA256_DIGEST_SIZE];
  42094. #endif
  42095. int j,k;
  42096. transId[0] = 0x13;
  42097. transId[1] = sizeof(digest) * 2;
  42098. #ifndef NO_SHA
  42099. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  42100. if (ret != 0)
  42101. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42102. wc_ShaUpdate(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  42103. wc_ShaFinal(&sha, digest);
  42104. wc_ShaFree(&sha);
  42105. #else
  42106. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  42107. if (ret != 0)
  42108. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42109. wc_Sha256Update(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  42110. wc_Sha256Final(&sha, digest);
  42111. wc_Sha256Free(&sha);
  42112. #endif
  42113. for (j = 0, k = 2; j < (int)sizeof(digest); j++, k += 2) {
  42114. #if defined(WOLF_C89)
  42115. XSPRINTF((char*)&transId[k], "%02x", digest[j]);
  42116. #else
  42117. (void)XSNPRINTF((char*)&transId[k], 3, "%02x", digest[j]);
  42118. #endif
  42119. }
  42120. }
  42121. /* enable detached signature generation, if set */
  42122. if (testVectors[i].detachedSignature == 1) {
  42123. ret = wc_PKCS7_SetDetached(pkcs7, 1);
  42124. if (ret != 0)
  42125. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42126. }
  42127. encodedSz = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
  42128. if (encodedSz < 0)
  42129. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  42130. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  42131. /* write PKCS#7 to output file for more testing */
  42132. file = XFOPEN(testVectors[i].outFileName, "wb");
  42133. if (!file) {
  42134. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42135. }
  42136. ret = (int)XFWRITE(out, 1, encodedSz, file);
  42137. XFCLOSE(file);
  42138. if (ret != (int)encodedSz)
  42139. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42140. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  42141. wc_PKCS7_Free(pkcs7);
  42142. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  42143. if (pkcs7 == NULL)
  42144. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42145. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  42146. if (testVectors[i].detachedSignature == 1) {
  42147. /* set content for verifying detached signatures */
  42148. pkcs7->content = (byte*)testVectors[i].content;
  42149. pkcs7->contentSz = testVectors[i].contentSz;
  42150. }
  42151. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  42152. if (ret < 0)
  42153. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42154. /* verify contentType extracted successfully for custom content types */
  42155. if (testVectors[i].contentTypeSz > 0) {
  42156. if (pkcs7->contentTypeSz != testVectors[i].contentTypeSz) {
  42157. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42158. } else if (XMEMCMP(pkcs7->contentType, testVectors[i].contentType,
  42159. pkcs7->contentTypeSz) != 0) {
  42160. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42161. }
  42162. }
  42163. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  42164. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42165. {
  42166. /* check getting signed attributes */
  42167. #ifndef NO_SHA
  42168. byte buf[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  42169. #else
  42170. byte buf[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  42171. #endif
  42172. const byte* oidPt = transIdOid + 2; /* skip object id tag and size */
  42173. int oidSz = (int)sizeof(transIdOid) - 2;
  42174. int bufSz = 0;
  42175. if (testVectors[i].signedAttribs != NULL) {
  42176. ret = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, (word32)oidSz,
  42177. NULL, (word32*)&bufSz);
  42178. if (ret != LENGTH_ONLY_E)
  42179. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42180. ret = 0;
  42181. }
  42182. if (bufSz > (int)sizeof(buf))
  42183. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42184. bufSz = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, (word32)oidSz,
  42185. buf, (word32*)&bufSz);
  42186. if ((testVectors[i].signedAttribs != NULL && bufSz < 0) ||
  42187. (testVectors[i].signedAttribs == NULL && bufSz > 0))
  42188. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42189. }
  42190. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  42191. file = XFOPEN("./pkcs7cert.der", "wb");
  42192. if (!file)
  42193. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42194. ret = (int)XFWRITE(pkcs7->singleCert, 1, pkcs7->singleCertSz, file);
  42195. if (ret < 0)
  42196. ret = WC_TEST_RET_ENC_ERRNO;
  42197. else
  42198. ret = 0;
  42199. XFCLOSE(file);
  42200. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  42201. }
  42202. out:
  42203. if (pkcs7 != NULL)
  42204. wc_PKCS7_Free(pkcs7);
  42205. if (out != NULL)
  42206. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42207. if (testVectors != NULL)
  42208. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42209. wc_FreeRng(&rng);
  42210. if (ret > 0)
  42211. return 0;
  42212. (void)rsaClientCertBuf;
  42213. (void)rsaClientCertBufSz;
  42214. (void)rsaClientPrivKeyBuf;
  42215. (void)rsaClientPrivKeyBufSz;
  42216. (void)rsaServerCertBuf;
  42217. (void)rsaServerCertBufSz;
  42218. (void)rsaServerPrivKeyBuf;
  42219. (void)rsaServerPrivKeyBufSz;
  42220. (void)rsaCaCertBuf;
  42221. (void)rsaCaCertBufSz;
  42222. (void)rsaCaPrivKeyBuf;
  42223. (void)rsaCaPrivKeyBufSz;
  42224. (void)eccClientCertBuf;
  42225. (void)eccClientCertBufSz;
  42226. (void)eccClientPrivKeyBuf;
  42227. (void)eccClientPrivKeyBufSz;
  42228. return ret;
  42229. }
  42230. static wc_test_ret_t pkcs7signed_run_SingleShotVectors(
  42231. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  42232. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  42233. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  42234. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  42235. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  42236. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  42237. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  42238. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  42239. {
  42240. wc_test_ret_t ret;
  42241. int testSz = 0, i;
  42242. int encodedSz;
  42243. byte* out = NULL;
  42244. word32 outSz;
  42245. WC_RNG rng;
  42246. PKCS7* pkcs7 = NULL;
  42247. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  42248. XFILE file;
  42249. #endif
  42250. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  42251. !defined(NO_PKCS7_ENCRYPTED_DATA)
  42252. byte* encryptedTmp = NULL;
  42253. int encryptedTmpSz;
  42254. #endif
  42255. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  42256. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  42257. 0x72,0x6c,0x64
  42258. };
  42259. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  42260. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  42261. static const byte aes256Key[] = {
  42262. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  42263. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  42264. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  42265. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  42266. };
  42267. #endif
  42268. static const byte messageTypeOid[] =
  42269. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  42270. 0x09, 0x02 };
  42271. static const byte messageType[] = { 0x13, 2, '1', '9' };
  42272. PKCS7Attrib attribs[] =
  42273. {
  42274. { messageTypeOid, sizeof(messageTypeOid), messageType,
  42275. sizeof(messageType) },
  42276. };
  42277. #define MAX_TESTVECTORS_LEN 19
  42278. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  42279. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  42280. if (testSz == MAX_TESTVECTORS_LEN) { \
  42281. ret = WC_TEST_RET_ENC_NC; \
  42282. goto out; \
  42283. } \
  42284. XMEMCPY(&testVectors[testSz++], &_this_vector, \
  42285. sizeof _this_vector); \
  42286. }
  42287. pkcs7SignedVector *testVectors = NULL;
  42288. XMEMSET(&rng, 0, sizeof(rng));
  42289. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  42290. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42291. if (testVectors == NULL) {
  42292. ret = WC_TEST_RET_ENC_ERRNO;
  42293. goto out;
  42294. }
  42295. {
  42296. #ifndef NO_RSA
  42297. #ifndef NO_SHA256
  42298. /* Signed FirmwarePkgData, RSA, SHA256, no attribs */
  42299. ADD_PKCS7SIGNEDVECTOR(
  42300. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  42301. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  42302. NULL, 0,
  42303. "pkcs7signedFirmwarePkgData_RSA_SHA256_noattr.der", 0, NULL, 0, 0,
  42304. 0, 0, NULL, 0, NULL, 0, 0);
  42305. /* Signed FirmwarePkgData, RSA, SHA256, attrs */
  42306. ADD_PKCS7SIGNEDVECTOR(
  42307. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  42308. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  42309. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  42310. "pkcs7signedFirmwarePkgData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0,
  42311. NULL, 0, NULL, 0, 0);
  42312. /* Signed FirmwarePkgData, RSA, SHA256, SubjectKeyIdentifier, attrs */
  42313. ADD_PKCS7SIGNEDVECTOR(
  42314. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  42315. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  42316. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  42317. "pkcs7signedFirmwarePkgData_RSA_SHA256_SKID.der", 0, NULL,
  42318. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  42319. /* Signed FirmwraePkgData, RSA, SHA256, server cert and ca cert, attr */
  42320. ADD_PKCS7SIGNEDVECTOR(
  42321. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  42322. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  42323. rsaCaCertBuf, rsaCaCertBufSz,
  42324. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  42325. "pkcs7signedFirmwarePkgData_RSA_SHA256_with_ca_cert.der", 0, NULL,
  42326. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  42327. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  42328. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  42329. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, no attribs */
  42330. ADD_PKCS7SIGNEDVECTOR(
  42331. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  42332. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  42333. NULL, 0,
  42334. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  42335. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  42336. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, attribs */
  42337. ADD_PKCS7SIGNEDVECTOR(
  42338. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  42339. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  42340. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  42341. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256.der", 0,
  42342. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  42343. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  42344. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  42345. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  42346. /* Signed Compressed FirmwarePkgData, RSA, SHA256, no attribs */
  42347. ADD_PKCS7SIGNEDVECTOR(
  42348. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  42349. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  42350. NULL, 0,
  42351. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  42352. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  42353. /* Signed Compressed FirmwarePkgData, RSA, SHA256, attribs */
  42354. ADD_PKCS7SIGNEDVECTOR(
  42355. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  42356. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  42357. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  42358. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256.der", 0,
  42359. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  42360. #ifndef NO_PKCS7_ENCRYPTED_DATA
  42361. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  42362. no attribs */
  42363. ADD_PKCS7SIGNEDVECTOR(
  42364. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  42365. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  42366. NULL, 0,
  42367. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256_noattr.der",
  42368. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  42369. 0, 0);
  42370. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  42371. attribs */
  42372. ADD_PKCS7SIGNEDVECTOR(
  42373. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  42374. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  42375. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  42376. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256.der",
  42377. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  42378. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  42379. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  42380. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  42381. #endif /* NO_SHA256 */
  42382. #endif /* NO_RSA */
  42383. #ifdef HAVE_ECC
  42384. #ifndef NO_SHA256
  42385. /* Signed FirmwarePkgData, ECDSA, SHA256, no attribs */
  42386. ADD_PKCS7SIGNEDVECTOR(
  42387. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  42388. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  42389. NULL, 0,
  42390. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  42391. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  42392. /* Signed FirmwarePkgData, ECDSA, SHA256, attribs */
  42393. ADD_PKCS7SIGNEDVECTOR(
  42394. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  42395. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  42396. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  42397. "pkcs7signedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  42398. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  42399. /* Signed FirmwarePkgData, ECDSA, SHA256, SubjectKeyIdentifier, attr */
  42400. ADD_PKCS7SIGNEDVECTOR(
  42401. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  42402. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  42403. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  42404. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_SKID.der", 0, NULL,
  42405. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  42406. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  42407. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  42408. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, no attribs */
  42409. ADD_PKCS7SIGNEDVECTOR(
  42410. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  42411. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  42412. NULL, 0,
  42413. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  42414. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  42415. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, attribs */
  42416. ADD_PKCS7SIGNEDVECTOR(
  42417. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  42418. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  42419. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  42420. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  42421. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  42422. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  42423. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  42424. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  42425. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, no attribs */
  42426. ADD_PKCS7SIGNEDVECTOR(
  42427. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  42428. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  42429. NULL, 0,
  42430. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  42431. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  42432. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, attrib */
  42433. ADD_PKCS7SIGNEDVECTOR(
  42434. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  42435. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  42436. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  42437. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  42438. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  42439. #ifndef NO_PKCS7_ENCRYPTED_DATA
  42440. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  42441. no attribs */
  42442. ADD_PKCS7SIGNEDVECTOR(
  42443. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  42444. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  42445. NULL, 0,
  42446. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der",
  42447. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  42448. 0, 0);
  42449. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  42450. attribs */
  42451. ADD_PKCS7SIGNEDVECTOR(
  42452. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  42453. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  42454. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  42455. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256.der",
  42456. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  42457. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  42458. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  42459. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  42460. #endif /* NO_SHA256 */
  42461. #endif /* HAVE_ECC */
  42462. };
  42463. #undef MAX_TESTVECTORS_LEN
  42464. #undef ADD_PKCS7SIGNEDVECTOR
  42465. outSz = FOURK_BUF;
  42466. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42467. if (out == NULL)
  42468. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42469. XMEMSET(out, 0, outSz);
  42470. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  42471. if (ret < 0)
  42472. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42473. #ifndef HAVE_FIPS
  42474. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  42475. #else
  42476. ret = wc_InitRng(&rng);
  42477. #endif
  42478. if (ret != 0)
  42479. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42480. for (i = 0; i < testSz; i++) {
  42481. if (pkcs7)
  42482. wc_PKCS7_Free(pkcs7);
  42483. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  42484. if (pkcs7 == NULL)
  42485. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42486. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  42487. (word32)testVectors[i].certSz);
  42488. if (ret != 0)
  42489. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42490. /* load CA certificate, if present */
  42491. if (testVectors[i].caCert != NULL) {
  42492. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  42493. (word32)testVectors[i].caCertSz);
  42494. if (ret != 0)
  42495. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42496. }
  42497. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  42498. default is IssuerAndSerialNumber */
  42499. if (testVectors[i].sidType == CMS_SKID) {
  42500. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  42501. if (ret != 0)
  42502. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42503. }
  42504. if (testVectors[i].encCompFlag == 0) {
  42505. /* encode Signed FirmwarePkgData */
  42506. encodedSz = wc_PKCS7_EncodeSignedFPD(pkcs7,
  42507. testVectors[i].privateKey, testVectors[i].privateKeySz,
  42508. testVectors[i].signOID, testVectors[i].hashOID,
  42509. (byte*)testVectors[i].content, testVectors[i].contentSz,
  42510. testVectors[i].signedAttribs,
  42511. testVectors[i].signedAttribsSz, out, outSz);
  42512. if (encodedSz < 0)
  42513. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  42514. #ifndef NO_PKCS7_ENCRYPTED_DATA
  42515. } else if (testVectors[i].encCompFlag == 1) {
  42516. /* encode Signed Encrypted FirmwarePkgData */
  42517. encodedSz = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7,
  42518. (byte *)testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  42519. testVectors[i].privateKey, testVectors[i].privateKeySz,
  42520. testVectors[i].encryptOID, testVectors[i].signOID,
  42521. testVectors[i].hashOID, (byte*)testVectors[i].content,
  42522. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  42523. testVectors[i].unprotectedAttribsSz,
  42524. testVectors[i].signedAttribs,
  42525. testVectors[i].signedAttribsSz, out, outSz);
  42526. if (encodedSz <= 0)
  42527. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  42528. #endif
  42529. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  42530. } else if (testVectors[i].encCompFlag == 2) {
  42531. /* encode Signed Compressed FirmwarePkgData */
  42532. encodedSz = wc_PKCS7_EncodeSignedCompressedFPD(pkcs7,
  42533. testVectors[i].privateKey, testVectors[i].privateKeySz,
  42534. testVectors[i].signOID, testVectors[i].hashOID,
  42535. (byte*)testVectors[i].content, testVectors[i].contentSz,
  42536. testVectors[i].signedAttribs,
  42537. testVectors[i].signedAttribsSz, out, outSz);
  42538. if (encodedSz <= 0)
  42539. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  42540. #ifndef NO_PKCS7_ENCRYPTED_DATA
  42541. } else if (testVectors[i].encCompFlag == 3) {
  42542. /* encode Signed Encrypted Compressed FirmwarePkgData */
  42543. encodedSz = wc_PKCS7_EncodeSignedEncryptedCompressedFPD(pkcs7,
  42544. (byte*)testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  42545. testVectors[i].privateKey, testVectors[i].privateKeySz,
  42546. testVectors[i].encryptOID, testVectors[i].signOID,
  42547. testVectors[i].hashOID, (byte*)testVectors[i].content,
  42548. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  42549. testVectors[i].unprotectedAttribsSz,
  42550. testVectors[i].signedAttribs,
  42551. testVectors[i].signedAttribsSz, out, outSz);
  42552. if (encodedSz <= 0)
  42553. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  42554. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  42555. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  42556. } else {
  42557. /* unsupported SignedData single-shot combination */
  42558. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42559. }
  42560. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  42561. /* write PKCS#7 to output file for more testing */
  42562. file = XFOPEN(testVectors[i].outFileName, "wb");
  42563. if (!file)
  42564. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42565. ret = (int)XFWRITE(out, 1, encodedSz, file);
  42566. XFCLOSE(file);
  42567. file = NULL;
  42568. if (ret != (int)encodedSz)
  42569. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42570. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  42571. wc_PKCS7_Free(pkcs7);
  42572. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  42573. if (pkcs7 == NULL)
  42574. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42575. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  42576. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  42577. if (ret < 0)
  42578. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42579. #ifndef NO_PKCS7_STREAM
  42580. {
  42581. word32 z;
  42582. for (z = 0; z < outSz && ret != 0; z++) {
  42583. ret = wc_PKCS7_VerifySignedData(pkcs7, out + z, 1);
  42584. if (ret < 0 && ret != WC_PKCS7_WANT_READ_E) {
  42585. printf("unexpected error %d\n", ret);
  42586. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42587. }
  42588. }
  42589. }
  42590. #endif
  42591. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  42592. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42593. if (testVectors[i].encCompFlag == 0) {
  42594. /* verify decoded content matches expected */
  42595. if ((pkcs7->contentSz != testVectors[i].contentSz) ||
  42596. XMEMCMP(pkcs7->content, testVectors[i].content,
  42597. pkcs7->contentSz)) {
  42598. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42599. }
  42600. }
  42601. #ifndef NO_PKCS7_ENCRYPTED_DATA
  42602. else if (testVectors[i].encCompFlag == 1) {
  42603. /* decrypt inner encryptedData */
  42604. pkcs7->encryptionKey = (byte *)testVectors[i].encryptKey;
  42605. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  42606. ret = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  42607. pkcs7->contentSz, out, outSz);
  42608. if (ret < 0)
  42609. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42610. /* compare decrypted to expected */
  42611. if (((word32)ret != testVectors[i].contentSz) ||
  42612. XMEMCMP(out, testVectors[i].content, ret))
  42613. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42614. }
  42615. #endif
  42616. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  42617. else if (testVectors[i].encCompFlag == 2) {
  42618. /* decompress inner compressedData */
  42619. ret = wc_PKCS7_DecodeCompressedData(pkcs7, pkcs7->content,
  42620. pkcs7->contentSz, out, outSz);
  42621. if (ret < 0)
  42622. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42623. /* compare decompressed to expected */
  42624. if (((word32)ret != testVectors[i].contentSz) ||
  42625. XMEMCMP(out, testVectors[i].content, ret))
  42626. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42627. }
  42628. #ifndef NO_PKCS7_ENCRYPTED_DATA
  42629. else if (testVectors[i].encCompFlag == 3) {
  42630. encryptedTmpSz = FOURK_BUF;
  42631. encryptedTmp = (byte*)XMALLOC(encryptedTmpSz, HEAP_HINT,
  42632. DYNAMIC_TYPE_TMP_BUFFER);
  42633. if (encryptedTmp == NULL)
  42634. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42635. XMEMSET(encryptedTmp, 0, encryptedTmpSz);
  42636. /* decrypt inner encryptedData */
  42637. pkcs7->encryptionKey = (byte*)testVectors[i].encryptKey;
  42638. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  42639. encryptedTmpSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  42640. pkcs7->contentSz, encryptedTmp,
  42641. encryptedTmpSz);
  42642. if (encryptedTmpSz < 0 || pkcs7->contentOID != COMPRESSED_DATA)
  42643. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42644. /* decompress inner compressedData */
  42645. ret = wc_PKCS7_DecodeCompressedData(pkcs7, encryptedTmp,
  42646. encryptedTmpSz, out, outSz);
  42647. if (ret < 0)
  42648. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42649. /* compare decompressed to expected */
  42650. if (((word32)ret != testVectors[i].contentSz) ||
  42651. XMEMCMP(out, testVectors[i].content, ret))
  42652. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42653. }
  42654. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  42655. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  42656. }
  42657. out:
  42658. if (pkcs7 != NULL)
  42659. wc_PKCS7_Free(pkcs7);
  42660. if (out != NULL)
  42661. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42662. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  42663. !defined(NO_PKCS7_ENCRYPTED_DATA)
  42664. if (encryptedTmp != NULL)
  42665. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42666. #endif
  42667. if (testVectors != NULL)
  42668. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42669. wc_FreeRng(&rng);
  42670. if (ret > 0)
  42671. return 0;
  42672. (void)eccClientCertBuf;
  42673. (void)eccClientCertBufSz;
  42674. (void)eccClientPrivKeyBuf;
  42675. (void)eccClientPrivKeyBufSz;
  42676. (void)rsaClientCertBuf;
  42677. (void)rsaClientCertBufSz;
  42678. (void)rsaClientPrivKeyBuf;
  42679. (void)rsaClientPrivKeyBufSz;
  42680. (void)rsaServerCertBuf;
  42681. (void)rsaServerCertBufSz;
  42682. (void)rsaServerPrivKeyBuf;
  42683. (void)rsaServerPrivKeyBufSz;
  42684. (void)rsaCaCertBuf;
  42685. (void)rsaCaCertBufSz;
  42686. (void)rsaCaPrivKeyBuf;
  42687. (void)rsaCaPrivKeyBufSz;
  42688. return ret;
  42689. }
  42690. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7signed_test(void)
  42691. {
  42692. wc_test_ret_t ret = 0;
  42693. byte* rsaClientCertBuf = NULL;
  42694. byte* rsaServerCertBuf = NULL;
  42695. byte* rsaCaCertBuf = NULL;
  42696. byte* eccClientCertBuf = NULL;
  42697. byte* rsaClientPrivKeyBuf = NULL;
  42698. byte* rsaServerPrivKeyBuf = NULL;
  42699. byte* rsaCaPrivKeyBuf = NULL;
  42700. byte* eccClientPrivKeyBuf = NULL;
  42701. word32 rsaClientCertBufSz = 0;
  42702. word32 rsaServerCertBufSz = 0;
  42703. word32 rsaCaCertBufSz = 0;
  42704. word32 eccClientCertBufSz = 0;
  42705. word32 rsaClientPrivKeyBufSz = 0;
  42706. word32 rsaServerPrivKeyBufSz = 0;
  42707. word32 rsaCaPrivKeyBufSz = 0;
  42708. word32 eccClientPrivKeyBufSz = 0;
  42709. WOLFSSL_ENTER("pkcs7signed_test");
  42710. #ifndef NO_RSA
  42711. /* read client RSA cert and key in DER format */
  42712. rsaClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  42713. DYNAMIC_TYPE_TMP_BUFFER);
  42714. if (rsaClientCertBuf == NULL)
  42715. ret = WC_TEST_RET_ENC_NC;
  42716. rsaClientPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  42717. DYNAMIC_TYPE_TMP_BUFFER);
  42718. if (ret == 0 && rsaClientPrivKeyBuf == NULL) {
  42719. ret = WC_TEST_RET_ENC_ERRNO;
  42720. }
  42721. rsaClientCertBufSz = FOURK_BUF;
  42722. rsaClientPrivKeyBufSz = FOURK_BUF;
  42723. /* read server RSA cert and key in DER format */
  42724. rsaServerCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  42725. DYNAMIC_TYPE_TMP_BUFFER);
  42726. if (ret == 0 && rsaServerCertBuf == NULL)
  42727. ret = WC_TEST_RET_ENC_NC;
  42728. rsaServerPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  42729. DYNAMIC_TYPE_TMP_BUFFER);
  42730. if (ret == 0 && rsaServerPrivKeyBuf == NULL) {
  42731. ret = WC_TEST_RET_ENC_ERRNO;
  42732. }
  42733. rsaServerCertBufSz = FOURK_BUF;
  42734. rsaServerPrivKeyBufSz = FOURK_BUF;
  42735. /* read CA RSA cert and key in DER format, for use with server cert */
  42736. rsaCaCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  42737. DYNAMIC_TYPE_TMP_BUFFER);
  42738. if (ret == 0 && rsaCaCertBuf == NULL)
  42739. ret = WC_TEST_RET_ENC_NC;
  42740. rsaCaPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  42741. DYNAMIC_TYPE_TMP_BUFFER);
  42742. if (ret == 0 && rsaCaPrivKeyBuf == NULL) {
  42743. ret = WC_TEST_RET_ENC_ERRNO;
  42744. }
  42745. rsaCaCertBufSz = FOURK_BUF;
  42746. rsaCaPrivKeyBufSz = FOURK_BUF;
  42747. #endif /* NO_RSA */
  42748. #ifdef HAVE_ECC
  42749. /* read client ECC cert and key in DER format */
  42750. eccClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  42751. DYNAMIC_TYPE_TMP_BUFFER);
  42752. if (ret == 0 && eccClientCertBuf == NULL) {
  42753. ret = WC_TEST_RET_ENC_ERRNO;
  42754. }
  42755. eccClientPrivKeyBuf =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  42756. DYNAMIC_TYPE_TMP_BUFFER);
  42757. if (ret == 0 && eccClientPrivKeyBuf == NULL) {
  42758. ret = WC_TEST_RET_ENC_ERRNO;
  42759. }
  42760. eccClientCertBufSz = FOURK_BUF;
  42761. eccClientPrivKeyBufSz = FOURK_BUF;
  42762. #endif /* HAVE_ECC */
  42763. if (ret >= 0)
  42764. ret = pkcs7_load_certs_keys(rsaClientCertBuf, &rsaClientCertBufSz,
  42765. rsaClientPrivKeyBuf, &rsaClientPrivKeyBufSz,
  42766. rsaServerCertBuf, &rsaServerCertBufSz,
  42767. rsaServerPrivKeyBuf, &rsaServerPrivKeyBufSz,
  42768. rsaCaCertBuf, &rsaCaCertBufSz,
  42769. rsaCaPrivKeyBuf, &rsaCaPrivKeyBufSz,
  42770. eccClientCertBuf, &eccClientCertBufSz,
  42771. eccClientPrivKeyBuf, &eccClientPrivKeyBufSz);
  42772. if (ret < 0) {
  42773. ret = WC_TEST_RET_ENC_EC(ret);
  42774. }
  42775. if (ret >= 0)
  42776. ret = pkcs7signed_run_vectors(rsaClientCertBuf, (word32)rsaClientCertBufSz,
  42777. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  42778. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  42779. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  42780. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  42781. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  42782. eccClientCertBuf, (word32)eccClientCertBufSz,
  42783. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  42784. if (ret >= 0)
  42785. ret = pkcs7signed_run_SingleShotVectors(
  42786. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  42787. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  42788. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  42789. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  42790. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  42791. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  42792. eccClientCertBuf, (word32)eccClientCertBufSz,
  42793. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  42794. #if !defined(NO_RSA) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  42795. if (ret >= 0)
  42796. ret = pkcs7callback_test(
  42797. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  42798. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz);
  42799. #endif
  42800. XFREE(rsaClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42801. XFREE(rsaClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42802. XFREE(rsaServerCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42803. XFREE(rsaServerPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42804. XFREE(rsaCaCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42805. XFREE(rsaCaPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42806. XFREE(eccClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42807. XFREE(eccClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42808. return ret;
  42809. }
  42810. #endif /* HAVE_PKCS7 */
  42811. #if defined(WOLFSSL_PUBLIC_MP) && \
  42812. ((defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  42813. defined(USE_FAST_MATH))
  42814. /* Maximum number of bytes in a number to test. */
  42815. #define MP_MAX_TEST_BYTE_LEN 32
  42816. static wc_test_ret_t randNum(mp_int* n, int len, WC_RNG* rng, void* heap)
  42817. {
  42818. byte d[MP_MAX_TEST_BYTE_LEN];
  42819. wc_test_ret_t ret;
  42820. (void)heap;
  42821. do {
  42822. ret = wc_RNG_GenerateBlock(rng, d, (word32)len);
  42823. if (ret != 0)
  42824. return ret;
  42825. ret = mp_read_unsigned_bin(n, d, (word32)len);
  42826. if (ret != 0)
  42827. return ret;
  42828. } while (mp_iszero(n));
  42829. return 0;
  42830. }
  42831. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  42832. static wc_test_ret_t mp_test_div_3(mp_int* a, mp_int* r, WC_RNG* rng)
  42833. {
  42834. int i, j;
  42835. mp_digit rem;
  42836. mp_digit rem2;
  42837. wc_test_ret_t ret;
  42838. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  42839. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  42840. for (i = 0; i < 10; i++) {
  42841. for (j = 1; j < 10; j++) {
  42842. ret = randNum(a, j, rng, NULL);
  42843. if (ret != 0)
  42844. return WC_TEST_RET_ENC_EC(ret);
  42845. ret = mp_div_3(a, r, &rem);
  42846. if (ret != 0)
  42847. return WC_TEST_RET_ENC_EC(ret);
  42848. ret = mp_mul_d(r, 3, r);
  42849. if (ret != 0)
  42850. return WC_TEST_RET_ENC_EC(ret);
  42851. ret = mp_add_d(r, rem, r);
  42852. if (ret != 0)
  42853. return WC_TEST_RET_ENC_EC(ret);
  42854. ret = mp_cmp(r, a);
  42855. if (ret != MP_EQ)
  42856. return WC_TEST_RET_ENC_NC;
  42857. }
  42858. }
  42859. ret = mp_div_3(a, r, &rem);
  42860. if (ret != 0)
  42861. return WC_TEST_RET_ENC_NC;
  42862. ret = mp_div_3(a, a, NULL);
  42863. if (ret != 0)
  42864. return WC_TEST_RET_ENC_NC;
  42865. ret = mp_cmp(r, a);
  42866. if (ret != MP_EQ)
  42867. return WC_TEST_RET_ENC_NC;
  42868. #endif
  42869. #if defined(WOLFSSL_SP_MATH_ALL)
  42870. ret = mp_div_d(a, 10, r, &rem);
  42871. if (ret != 0)
  42872. return WC_TEST_RET_ENC_EC(ret);
  42873. ret = mp_div_d(a, 10, a, NULL);
  42874. if (ret != 0)
  42875. return WC_TEST_RET_ENC_EC(ret);
  42876. ret = mp_cmp(r, a);
  42877. if (ret != MP_EQ)
  42878. return WC_TEST_RET_ENC_NC;
  42879. ret = mp_div_d(a, 12, r, &rem);
  42880. if (ret != 0)
  42881. return WC_TEST_RET_ENC_EC(ret);
  42882. ret = mp_div_d(a, 12, a, NULL);
  42883. if (ret != 0)
  42884. return WC_TEST_RET_ENC_EC(ret);
  42885. ret = mp_cmp(r, a);
  42886. if (ret != MP_EQ)
  42887. return WC_TEST_RET_ENC_NC;
  42888. ret = mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), r, &rem);
  42889. if (ret != 0)
  42890. return WC_TEST_RET_ENC_EC(ret);
  42891. ret = mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), NULL, &rem2);
  42892. if (ret != 0)
  42893. return WC_TEST_RET_ENC_EC(ret);
  42894. ret = mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), a, NULL);
  42895. if (ret != 0)
  42896. return WC_TEST_RET_ENC_EC(ret);
  42897. ret = mp_cmp(r, a);
  42898. if (ret != MP_EQ)
  42899. return WC_TEST_RET_ENC_NC;
  42900. if (rem != rem2)
  42901. return WC_TEST_RET_ENC_NC;
  42902. #endif
  42903. (void)a;
  42904. (void)r;
  42905. (void)rng;
  42906. (void)i;
  42907. (void)j;
  42908. (void)rem;
  42909. (void)rem2;
  42910. (void)ret;
  42911. return 0;
  42912. }
  42913. #endif /* WOLFSSL_SP_MATH || !USE_FAST_MATH */
  42914. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  42915. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  42916. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  42917. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  42918. static wc_test_ret_t mp_test_radix_10(mp_int* a, mp_int* r, WC_RNG* rng)
  42919. {
  42920. wc_test_ret_t ret;
  42921. int i, j;
  42922. int size;
  42923. char str[30];
  42924. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = "A";
  42925. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "a";
  42926. WOLFSSL_SMALL_STACK_STATIC const char* empty2 = " ";
  42927. WOLFSSL_SMALL_STACK_STATIC const char* zeros = "000";
  42928. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  42929. for (i = 0; i < 10; i++) {
  42930. for (j = 2; j < 12; j++) {
  42931. ret = randNum(a, j, rng, NULL);
  42932. if (ret != 0)
  42933. return WC_TEST_RET_ENC_EC(ret);
  42934. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  42935. if (ret != MP_OKAY)
  42936. return WC_TEST_RET_ENC_EC(ret);
  42937. ret = mp_toradix(a, str, MP_RADIX_DEC);
  42938. if (ret != MP_OKAY)
  42939. return WC_TEST_RET_ENC_EC(ret);
  42940. if ((int)XSTRLEN(str) != size - 1)
  42941. return WC_TEST_RET_ENC_NC;
  42942. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  42943. if (ret != MP_OKAY)
  42944. return WC_TEST_RET_ENC_EC(ret);
  42945. ret = mp_cmp(a, r);
  42946. if (ret != MP_EQ)
  42947. return WC_TEST_RET_ENC_NC;
  42948. }
  42949. }
  42950. ret = mp_read_radix(r, badStr1, MP_RADIX_DEC);
  42951. if (ret != MP_VAL)
  42952. return WC_TEST_RET_ENC_EC(ret);
  42953. ret = mp_read_radix(r, badStr2, MP_RADIX_DEC);
  42954. if (ret != MP_VAL)
  42955. return WC_TEST_RET_ENC_EC(ret);
  42956. ret = mp_read_radix(r, empty2, MP_RADIX_DEC);
  42957. if (ret != MP_OKAY)
  42958. return WC_TEST_RET_ENC_EC(ret);
  42959. ret = mp_read_radix(r, zeros, MP_RADIX_DEC);
  42960. if (ret != MP_OKAY)
  42961. return WC_TEST_RET_ENC_EC(ret);
  42962. if (!mp_iszero(r))
  42963. return WC_TEST_RET_ENC_NC;
  42964. mp_set(r, 1);
  42965. ret = mp_read_radix(r, empty, MP_RADIX_DEC);
  42966. if (ret != MP_OKAY)
  42967. return WC_TEST_RET_ENC_EC(ret);
  42968. if (!mp_iszero(r))
  42969. return WC_TEST_RET_ENC_NC;
  42970. mp_zero(a);
  42971. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  42972. if (ret != 0)
  42973. return WC_TEST_RET_ENC_EC(ret);
  42974. if (size != 2)
  42975. return WC_TEST_RET_ENC_NC;
  42976. ret = mp_toradix(a, str, MP_RADIX_DEC);
  42977. if (ret != 0)
  42978. return WC_TEST_RET_ENC_EC(ret);
  42979. if ((int)XSTRLEN(str) != size - 1)
  42980. return WC_TEST_RET_ENC_NC;
  42981. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  42982. if (ret != 0)
  42983. return WC_TEST_RET_ENC_EC(ret);
  42984. if (!mp_iszero(r))
  42985. return WC_TEST_RET_ENC_NC;
  42986. return 0;
  42987. }
  42988. #endif
  42989. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  42990. defined(HAVE_ECC))
  42991. static wc_test_ret_t mp_test_radix_16(mp_int* a, mp_int* r, WC_RNG* rng)
  42992. {
  42993. wc_test_ret_t ret;
  42994. int i, j;
  42995. int size;
  42996. char str[30];
  42997. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  42998. static char longStr[2 * sizeof(a->dp) + 2];
  42999. #endif
  43000. WOLFSSL_SMALL_STACK_STATIC const char* empty2 = " ";
  43001. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "}";
  43002. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  43003. for (i = 0; i < 10; i++) {
  43004. for (j = 2; j < 12; j++) {
  43005. ret = randNum(a, j, rng, NULL);
  43006. if (ret != 0)
  43007. return WC_TEST_RET_ENC_EC(ret);
  43008. mp_radix_size(a, MP_RADIX_HEX, &size);
  43009. mp_toradix(a, str, MP_RADIX_HEX);
  43010. if ((int)XSTRLEN(str) != size - 1)
  43011. return WC_TEST_RET_ENC_NC;
  43012. mp_read_radix(r, str, MP_RADIX_HEX);
  43013. ret = mp_cmp(a, r);
  43014. if (ret != MP_EQ)
  43015. return WC_TEST_RET_ENC_NC;
  43016. }
  43017. }
  43018. ret = mp_read_radix(r, empty2, MP_RADIX_HEX);
  43019. if (ret != MP_OKAY)
  43020. return WC_TEST_RET_ENC_EC(ret);
  43021. ret = mp_read_radix(r, badStr2, MP_RADIX_HEX);
  43022. if (ret != MP_VAL)
  43023. return WC_TEST_RET_ENC_EC(ret);
  43024. mp_set(r, 1);
  43025. ret = mp_read_radix(r, empty, MP_RADIX_HEX);
  43026. if (ret != MP_OKAY)
  43027. return WC_TEST_RET_ENC_EC(ret);
  43028. if (!mp_iszero(r))
  43029. return WC_TEST_RET_ENC_NC;
  43030. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  43031. /* Fixed MP data size - string can be too long. */
  43032. longStr[0] = '8';
  43033. XMEMSET(longStr+1, '0', sizeof(longStr) - 2);
  43034. longStr[sizeof(longStr)-1] = '\0';
  43035. ret = mp_read_radix(r, longStr, MP_RADIX_HEX);
  43036. if (ret != MP_VAL)
  43037. return WC_TEST_RET_ENC_EC(ret);
  43038. #endif
  43039. mp_zero(a);
  43040. ret = mp_radix_size(a, MP_RADIX_HEX, &size);
  43041. if (ret != 0)
  43042. return WC_TEST_RET_ENC_EC(ret);
  43043. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  43044. if (size != 3)
  43045. #else
  43046. if (size != 2)
  43047. #endif
  43048. return WC_TEST_RET_ENC_NC;
  43049. ret = mp_toradix(a, str, MP_RADIX_HEX);
  43050. if (ret != 0)
  43051. return WC_TEST_RET_ENC_EC(ret);
  43052. if ((int)XSTRLEN(str) != size - 1)
  43053. return WC_TEST_RET_ENC_NC;
  43054. ret = mp_read_radix(r, str, MP_RADIX_HEX);
  43055. if (ret != 0)
  43056. return WC_TEST_RET_ENC_EC(ret);
  43057. if (!mp_iszero(r))
  43058. return WC_TEST_RET_ENC_NC;
  43059. #ifdef WOLFSSL_SP_MATH
  43060. ret = mp_toradix(a, str, 8);
  43061. if (ret != MP_VAL)
  43062. return WC_TEST_RET_ENC_EC(ret);
  43063. ret = mp_radix_size(a, 8, &size);
  43064. if (ret != MP_VAL)
  43065. return WC_TEST_RET_ENC_EC(ret);
  43066. #endif
  43067. return 0;
  43068. }
  43069. #endif
  43070. static wc_test_ret_t mp_test_shift(mp_int* a, mp_int* r1, WC_RNG* rng)
  43071. {
  43072. int i;
  43073. wc_test_ret_t ret;
  43074. ret = randNum(a, 4, rng, NULL);
  43075. if (ret != 0)
  43076. return WC_TEST_RET_ENC_EC(ret);
  43077. for (i = 0; i < 4; i++) {
  43078. mp_copy(r1, a);
  43079. ret = mp_lshd(r1, i);
  43080. if (ret != MP_OKAY)
  43081. return WC_TEST_RET_ENC_EC(ret);
  43082. #ifndef WOLFSSL_SP_MATH
  43083. mp_rshd(r1, i);
  43084. #else
  43085. mp_rshb(r1, i * SP_WORD_SIZE);
  43086. #endif
  43087. ret = mp_cmp(a, r1);
  43088. if (ret != MP_EQ)
  43089. return WC_TEST_RET_ENC_NC;
  43090. }
  43091. #ifndef WOLFSSL_SP_MATH
  43092. for (i = 0; i < DIGIT_BIT+1; i++) {
  43093. ret = mp_mul_2d(a, i, r1);
  43094. if (ret != MP_OKAY)
  43095. return WC_TEST_RET_ENC_EC(ret);
  43096. mp_rshb(r1, i);
  43097. ret = mp_cmp(a, r1);
  43098. if (ret != MP_EQ)
  43099. return WC_TEST_RET_ENC_NC;
  43100. }
  43101. #endif
  43102. return 0;
  43103. }
  43104. static wc_test_ret_t mp_test_add_sub_d(mp_int* a, mp_int* r1)
  43105. {
  43106. int i, j;
  43107. wc_test_ret_t ret;
  43108. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  43109. mp_zero(a);
  43110. mp_set_bit(a, i);
  43111. if ((int)a->used != (i + DIGIT_BIT) / DIGIT_BIT)
  43112. return WC_TEST_RET_ENC_NC;
  43113. for (j = 0; j < i && j < DIGIT_BIT; j++) {
  43114. mp_zero(r1);
  43115. mp_set_bit(r1, i);
  43116. ret = mp_sub_d(r1, (mp_digit)1 << j, r1);
  43117. if (ret != MP_OKAY)
  43118. return WC_TEST_RET_ENC_EC(ret);
  43119. ret = mp_add_d(r1, (mp_digit)1 << j, r1);
  43120. if (ret != MP_OKAY)
  43121. return WC_TEST_RET_ENC_EC(ret);
  43122. ret = mp_cmp(a, r1);
  43123. if (ret != MP_EQ)
  43124. return WC_TEST_RET_ENC_NC;
  43125. }
  43126. }
  43127. mp_zero(r1);
  43128. ret = mp_add_d(r1, 1, r1);
  43129. if (ret != MP_OKAY)
  43130. return WC_TEST_RET_ENC_EC(ret);
  43131. if (r1->used != 1)
  43132. return WC_TEST_RET_ENC_NC;
  43133. ret = mp_sub_d(r1, 1, r1);
  43134. if (ret != MP_OKAY)
  43135. return WC_TEST_RET_ENC_EC(ret);
  43136. if (r1->used != 0)
  43137. return WC_TEST_RET_ENC_NC;
  43138. return 0;
  43139. }
  43140. static wc_test_ret_t mp_test_read_to_bin(mp_int* a)
  43141. {
  43142. WOLFSSL_SMALL_STACK_STATIC const byte in[16] = {
  43143. 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7, 0x08,
  43144. 0x93, 0xa4, 0xb4, 0xc5, 0xd6, 0xe7, 0xf8, 0x09
  43145. };
  43146. byte out[24];
  43147. int i, j, k;
  43148. const byte* p;
  43149. wc_test_ret_t ret;
  43150. for (i = 0; i < (int)sizeof(in); i++) {
  43151. p = in + sizeof(in) - i;
  43152. ret = mp_read_unsigned_bin(a, p, (word32)i);
  43153. if (ret != 0)
  43154. return WC_TEST_RET_ENC_EC(ret);
  43155. for (j = i; j < (int)sizeof(out); j++) {
  43156. XMEMSET(out, 0xff, sizeof(out));
  43157. ret = mp_to_unsigned_bin_len(a, out, j);
  43158. if (ret != 0)
  43159. return WC_TEST_RET_ENC_EC(ret);
  43160. for (k = 0; k < j - i; k++) {
  43161. if (out[k] != 0)
  43162. return WC_TEST_RET_ENC_NC;
  43163. }
  43164. for (; k < j; k++) {
  43165. if (out[k] != p[k - (j - i)])
  43166. return WC_TEST_RET_ENC_NC;
  43167. }
  43168. }
  43169. }
  43170. /* Length too small. */
  43171. ret = mp_to_unsigned_bin_len(a, out, 1);
  43172. if (ret != MP_VAL)
  43173. return WC_TEST_RET_ENC_EC(ret);
  43174. ret = mp_read_unsigned_bin(a, NULL, 0);
  43175. if (ret != 0)
  43176. return WC_TEST_RET_ENC_EC(ret);
  43177. if (!mp_iszero(a))
  43178. return WC_TEST_RET_ENC_NC;
  43179. return 0;
  43180. }
  43181. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  43182. static wc_test_ret_t mp_test_set_int(mp_int* a)
  43183. {
  43184. #if SP_ULONG_BITS == 64
  43185. unsigned long n = 0xfedcba9876543210UL;
  43186. byte exp[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
  43187. byte out[8] = { 0 };
  43188. #elif SP_ULONG_BITS == 32
  43189. unsigned long n = 0xfedcba98UL;
  43190. byte exp[4] = { 0xfe, 0xdc, 0xba, 0x98 };
  43191. byte out[4] = { 0 };
  43192. #elif SP_ULONG_BITS == 16
  43193. unsigned long n = 0xfedc;
  43194. byte exp[2] = { 0xfe, 0xdc };
  43195. byte out[2] = { 0 };
  43196. #elif SP_ULONG_BITS == 8
  43197. unsigned long n = 0xfe;
  43198. byte exp[1] = { 0xfe };
  43199. byte out[1] = { 0 };
  43200. #endif
  43201. wc_test_ret_t ret;
  43202. ret = mp_set_int(a, n);
  43203. if (ret != 0)
  43204. return WC_TEST_RET_ENC_EC(ret);
  43205. ret = mp_unsigned_bin_size(a);
  43206. if (ret != sizeof(exp))
  43207. return WC_TEST_RET_ENC_NC;
  43208. ret = mp_to_unsigned_bin(a, out);
  43209. if (ret != 0)
  43210. return WC_TEST_RET_ENC_EC(ret);
  43211. if (XMEMCMP(exp, out, sizeof(exp)) != 0)
  43212. return WC_TEST_RET_ENC_NC;
  43213. return 0;
  43214. }
  43215. #endif
  43216. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  43217. static wc_test_ret_t mp_test_param(mp_int* a, mp_int* b, mp_int* r, WC_RNG* rng)
  43218. {
  43219. byte buffer[16];
  43220. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  43221. char hexStr[] = "abcdef0123456789";
  43222. #ifndef WOLFSSL_SP_INT_NEGATIVE
  43223. char negStr[] = "-1234";
  43224. #endif
  43225. #endif
  43226. #if !defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_KEY_GEN) || \
  43227. defined(HAVE_COMP_KEY)
  43228. char decStr[] = "0987654321";
  43229. #endif
  43230. wc_test_ret_t ret;
  43231. #ifdef WOLFSSL_SP_MATH_ALL
  43232. mp_digit rho;
  43233. int size;
  43234. #endif
  43235. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  43236. int result;
  43237. #endif
  43238. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  43239. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  43240. mp_digit rd;
  43241. #endif
  43242. (void)rng;
  43243. (void)r;
  43244. ret = mp_init(NULL);
  43245. if (ret != MP_VAL)
  43246. return WC_TEST_RET_ENC_EC(ret);
  43247. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  43248. ret = mp_init_multi(NULL, NULL, NULL, NULL, NULL, NULL);
  43249. if (ret != MP_OKAY)
  43250. return WC_TEST_RET_ENC_EC(ret);
  43251. #endif
  43252. mp_free(NULL);
  43253. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || !defined(NO_DH) || defined(HAVE_ECC)
  43254. ret = mp_grow(NULL, 1);
  43255. if (ret != MP_VAL)
  43256. return WC_TEST_RET_ENC_EC(ret);
  43257. #ifdef WOLFSSL_SP_MATH
  43258. ret = mp_grow(a, SP_INT_DIGITS + 1);
  43259. if (ret != MP_MEM)
  43260. return WC_TEST_RET_ENC_EC(ret);
  43261. #endif
  43262. #endif
  43263. mp_clear(NULL);
  43264. ret = mp_abs(NULL, NULL);
  43265. if (ret != MP_VAL)
  43266. return WC_TEST_RET_ENC_EC(ret);
  43267. ret = mp_abs(a, NULL);
  43268. if (ret != MP_VAL)
  43269. return WC_TEST_RET_ENC_EC(ret);
  43270. ret = mp_abs(NULL, b);
  43271. if (ret != MP_VAL)
  43272. return WC_TEST_RET_ENC_EC(ret);
  43273. ret = mp_unsigned_bin_size(NULL);
  43274. if (ret != 0)
  43275. return WC_TEST_RET_ENC_EC(ret);
  43276. ret = mp_read_unsigned_bin(NULL, NULL, sizeof(buffer));
  43277. if (ret != MP_VAL)
  43278. return WC_TEST_RET_ENC_EC(ret);
  43279. ret = mp_read_unsigned_bin(NULL, buffer, sizeof(buffer));
  43280. if (ret != MP_VAL)
  43281. return WC_TEST_RET_ENC_EC(ret);
  43282. ret = mp_read_unsigned_bin(a, NULL, sizeof(buffer));
  43283. if (ret != MP_VAL)
  43284. return WC_TEST_RET_ENC_EC(ret);
  43285. ret = mp_read_unsigned_bin(a, buffer, SP_INT_DIGITS * SP_WORD_SIZEOF + 1);
  43286. if (ret != MP_VAL)
  43287. return WC_TEST_RET_ENC_EC(ret);
  43288. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  43289. ret = mp_read_radix(NULL, NULL, 16);
  43290. if (ret != MP_VAL)
  43291. return WC_TEST_RET_ENC_EC(ret);
  43292. ret = mp_read_radix(a, NULL, 16);
  43293. if (ret != MP_VAL)
  43294. return WC_TEST_RET_ENC_EC(ret);
  43295. ret = mp_read_radix(NULL, hexStr, 16);
  43296. if (ret != MP_VAL)
  43297. return WC_TEST_RET_ENC_EC(ret);
  43298. #ifndef WOLFSSL_SP_INT_NEGATIVE
  43299. ret = mp_read_radix(a, negStr, 16);
  43300. if (ret != MP_VAL)
  43301. return WC_TEST_RET_ENC_EC(ret);
  43302. #ifdef WOLFSSL_SP_MATH_ALL
  43303. ret = mp_read_radix(a, negStr, 10);
  43304. if (ret != MP_VAL)
  43305. return WC_TEST_RET_ENC_EC(ret);
  43306. #endif /* WOLFSSL_SP_MATH_ALL */
  43307. #endif /* WOLFSSL_SP_INT_NEGATIVE */
  43308. #endif
  43309. #ifndef WOLFSSL_SP_MATH_ALL
  43310. /* Radix 10 only supported with ALL. */
  43311. ret = mp_read_radix(a, decStr, 10);
  43312. if (ret != MP_VAL)
  43313. return WC_TEST_RET_ENC_EC(ret);
  43314. #endif
  43315. /* Radix 8 not supported SP_INT. */
  43316. ret = mp_read_radix(a, "0123", 8);
  43317. if (ret != MP_VAL)
  43318. return WC_TEST_RET_ENC_EC(ret);
  43319. ret = mp_count_bits(NULL);
  43320. if (ret != 0)
  43321. return WC_TEST_RET_ENC_EC(ret);
  43322. ret = mp_is_bit_set(NULL, 0);
  43323. if (ret != 0)
  43324. return WC_TEST_RET_ENC_EC(ret);
  43325. ret = mp_leading_bit(NULL);
  43326. if (ret != 0)
  43327. return WC_TEST_RET_ENC_EC(ret);
  43328. mp_zero(a);
  43329. ret = mp_leading_bit(a);
  43330. if (ret != 0)
  43331. return WC_TEST_RET_ENC_EC(ret);
  43332. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  43333. defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || \
  43334. !defined(NO_RSA)
  43335. ret = mp_set_bit(NULL, 1);
  43336. if (ret != MP_VAL)
  43337. return WC_TEST_RET_ENC_EC(ret);
  43338. #endif
  43339. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  43340. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  43341. ret = mp_to_unsigned_bin(NULL, NULL);
  43342. if (ret != MP_VAL)
  43343. return WC_TEST_RET_ENC_EC(ret);
  43344. ret = mp_to_unsigned_bin(a, NULL);
  43345. if (ret != MP_VAL)
  43346. return WC_TEST_RET_ENC_EC(ret);
  43347. ret = mp_to_unsigned_bin(NULL, buffer);
  43348. if (ret != MP_VAL)
  43349. return WC_TEST_RET_ENC_EC(ret);
  43350. #endif
  43351. ret = mp_to_unsigned_bin_len(NULL, NULL, 1);
  43352. if (ret != MP_VAL)
  43353. return WC_TEST_RET_ENC_EC(ret);
  43354. ret = mp_to_unsigned_bin_len(a, NULL, 1);
  43355. if (ret != MP_VAL)
  43356. return WC_TEST_RET_ENC_EC(ret);
  43357. ret = mp_to_unsigned_bin_len(NULL, buffer, 1);
  43358. if (ret != MP_VAL)
  43359. return WC_TEST_RET_ENC_EC(ret);
  43360. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  43361. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  43362. ret = mp_to_unsigned_bin_at_pos(0, NULL, NULL);
  43363. if (ret != MP_VAL)
  43364. return WC_TEST_RET_ENC_EC(ret);
  43365. ret = mp_to_unsigned_bin_at_pos(0, a, NULL);
  43366. if (ret != MP_VAL)
  43367. return WC_TEST_RET_ENC_EC(ret);
  43368. ret = mp_to_unsigned_bin_at_pos(0, NULL, buffer);
  43369. if (ret != MP_VAL)
  43370. return WC_TEST_RET_ENC_EC(ret);
  43371. ret = mp_to_unsigned_bin_at_pos(0, a, buffer);
  43372. if (ret != MP_OKAY)
  43373. return WC_TEST_RET_ENC_EC(ret);
  43374. #endif
  43375. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  43376. ret = mp_copy(NULL, NULL);
  43377. if (ret != MP_VAL)
  43378. return WC_TEST_RET_ENC_EC(ret);
  43379. ret = mp_copy(a, NULL);
  43380. if (ret != MP_VAL)
  43381. return WC_TEST_RET_ENC_EC(ret);
  43382. ret = mp_copy(NULL, b);
  43383. if (ret != MP_VAL)
  43384. return WC_TEST_RET_ENC_EC(ret);
  43385. #endif
  43386. #if defined(WOLFSSL_KEY_GEN) || !defined(NO_DH)
  43387. ret = sp_2expt(NULL, 1);
  43388. if (ret != MP_VAL)
  43389. return WC_TEST_RET_ENC_EC(ret);
  43390. #endif
  43391. ret = mp_set(NULL, 0);
  43392. if (ret != MP_VAL)
  43393. return WC_TEST_RET_ENC_EC(ret);
  43394. ret = mp_cmp_d(NULL, 0);
  43395. if (ret != MP_LT)
  43396. return WC_TEST_RET_ENC_EC(ret);
  43397. ret = mp_cmp(NULL, NULL);
  43398. if (ret != MP_EQ)
  43399. return WC_TEST_RET_ENC_NC;
  43400. ret = mp_cmp(a, NULL);
  43401. if (ret != MP_GT)
  43402. return WC_TEST_RET_ENC_NC;
  43403. ret = mp_cmp(NULL, b);
  43404. if (ret != MP_LT)
  43405. return WC_TEST_RET_ENC_NC;
  43406. #ifdef WOLFSSL_SP_MATH_ALL
  43407. mp_rshd(NULL, 1);
  43408. #endif
  43409. mp_zero(NULL);
  43410. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  43411. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  43412. ret = mp_lshd(NULL, 0);
  43413. if (ret != MP_VAL)
  43414. return WC_TEST_RET_ENC_EC(ret);
  43415. ret = mp_lshd(a, SP_INT_DIGITS + 1);
  43416. if (ret != MP_VAL)
  43417. return WC_TEST_RET_ENC_EC(ret);
  43418. #endif
  43419. #if defined(WOLFSSL_SP_MATH_ALL)
  43420. ret = mp_div(NULL, NULL, a, b);
  43421. if (ret != MP_VAL)
  43422. return WC_TEST_RET_ENC_EC(ret);
  43423. ret = mp_div(a, NULL, a, b);
  43424. if (ret != MP_VAL)
  43425. return WC_TEST_RET_ENC_EC(ret);
  43426. ret = mp_div(NULL, b, a, b);
  43427. if (ret != MP_VAL)
  43428. return WC_TEST_RET_ENC_EC(ret);
  43429. ret = mp_div(a, b, NULL, NULL);
  43430. if (ret != MP_VAL)
  43431. return WC_TEST_RET_ENC_EC(ret);
  43432. #endif
  43433. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  43434. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  43435. ret = mp_mod(NULL, NULL, NULL);
  43436. if (ret != MP_VAL)
  43437. return WC_TEST_RET_ENC_EC(ret);
  43438. ret = mp_mod(a, NULL, NULL);
  43439. if (ret != MP_VAL)
  43440. return WC_TEST_RET_ENC_EC(ret);
  43441. ret = mp_mod(NULL, b, NULL);
  43442. if (ret != MP_VAL)
  43443. return WC_TEST_RET_ENC_EC(ret);
  43444. ret = mp_mod(NULL, NULL, r);
  43445. if (ret != MP_VAL)
  43446. return WC_TEST_RET_ENC_EC(ret);
  43447. ret = mp_mod(a, b, NULL);
  43448. if (ret != MP_VAL)
  43449. return WC_TEST_RET_ENC_EC(ret);
  43450. ret = mp_mod(a, NULL, r);
  43451. if (ret != MP_VAL)
  43452. return WC_TEST_RET_ENC_EC(ret);
  43453. ret = mp_mod(NULL, b, r);
  43454. if (ret != MP_VAL)
  43455. return WC_TEST_RET_ENC_EC(ret);
  43456. #endif
  43457. #if !defined(NO_RSA) || defined(WOLFSSL_SP_MATH_ALL)
  43458. ret = mp_set_int(NULL, 0);
  43459. if (ret != MP_VAL)
  43460. return WC_TEST_RET_ENC_EC(ret);
  43461. #endif
  43462. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  43463. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  43464. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  43465. if (ret != MP_VAL)
  43466. return WC_TEST_RET_ENC_EC(ret);
  43467. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  43468. if (ret != MP_VAL)
  43469. return WC_TEST_RET_ENC_EC(ret);
  43470. ret = mp_exptmod_ex(NULL, a, 1, NULL, NULL);
  43471. if (ret != MP_VAL)
  43472. return WC_TEST_RET_ENC_EC(ret);
  43473. ret = mp_exptmod_ex(NULL, NULL, 1, a, NULL);
  43474. if (ret != MP_VAL)
  43475. return WC_TEST_RET_ENC_EC(ret);
  43476. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  43477. if (ret != MP_VAL)
  43478. return WC_TEST_RET_ENC_EC(ret);
  43479. ret = mp_exptmod_ex(a, a, 1, a, NULL);
  43480. if (ret != MP_VAL)
  43481. return WC_TEST_RET_ENC_EC(ret);
  43482. ret = mp_exptmod_ex(a, a, 1, NULL, a);
  43483. if (ret != MP_VAL)
  43484. return WC_TEST_RET_ENC_EC(ret);
  43485. ret = mp_exptmod_ex(a, NULL, 1, a, a);
  43486. if (ret != MP_VAL)
  43487. return WC_TEST_RET_ENC_EC(ret);
  43488. ret = mp_exptmod_ex(NULL, a, 1, a, a);
  43489. if (ret != MP_VAL)
  43490. return WC_TEST_RET_ENC_EC(ret);
  43491. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  43492. if (ret != MP_VAL)
  43493. return WC_TEST_RET_ENC_EC(ret);
  43494. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  43495. if (ret != MP_VAL)
  43496. return WC_TEST_RET_ENC_EC(ret);
  43497. ret = mp_exptmod_nct(NULL, a, NULL, NULL);
  43498. if (ret != MP_VAL)
  43499. return WC_TEST_RET_ENC_EC(ret);
  43500. ret = mp_exptmod_nct(NULL, NULL, a, NULL);
  43501. if (ret != MP_VAL)
  43502. return WC_TEST_RET_ENC_EC(ret);
  43503. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  43504. if (ret != MP_VAL)
  43505. return WC_TEST_RET_ENC_EC(ret);
  43506. ret = mp_exptmod_nct(a, a, a, NULL);
  43507. if (ret != MP_VAL)
  43508. return WC_TEST_RET_ENC_EC(ret);
  43509. ret = mp_exptmod_nct(a, a, NULL, a);
  43510. if (ret != MP_VAL)
  43511. return WC_TEST_RET_ENC_EC(ret);
  43512. ret = mp_exptmod_nct(a, NULL, a, a);
  43513. if (ret != MP_VAL)
  43514. return WC_TEST_RET_ENC_EC(ret);
  43515. ret = mp_exptmod_nct(NULL, a, a, a);
  43516. if (ret != MP_VAL)
  43517. return WC_TEST_RET_ENC_EC(ret);
  43518. #endif
  43519. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  43520. !defined(WC_NO_RNG)
  43521. ret = mp_rand_prime(NULL, 32, NULL, NULL);
  43522. if (ret != MP_VAL)
  43523. return WC_TEST_RET_ENC_EC(ret);
  43524. ret = mp_rand_prime(a, 32, NULL, NULL);
  43525. if (ret != MP_VAL)
  43526. return WC_TEST_RET_ENC_EC(ret);
  43527. ret = mp_rand_prime(NULL, 32, rng, NULL);
  43528. if (ret != MP_VAL)
  43529. return WC_TEST_RET_ENC_EC(ret);
  43530. ret = mp_rand_prime(a, 0, rng, NULL);
  43531. if (ret != MP_VAL)
  43532. return WC_TEST_RET_ENC_EC(ret);
  43533. #endif
  43534. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  43535. ret = mp_mul(NULL, NULL, NULL);
  43536. if (ret != MP_VAL)
  43537. return WC_TEST_RET_ENC_EC(ret);
  43538. ret = mp_mul(a, NULL, NULL);
  43539. if (ret != MP_VAL)
  43540. return WC_TEST_RET_ENC_EC(ret);
  43541. ret = mp_mul(NULL, b, NULL);
  43542. if (ret != MP_VAL)
  43543. return WC_TEST_RET_ENC_EC(ret);
  43544. ret = mp_mul(NULL, NULL, r);
  43545. if (ret != MP_VAL)
  43546. return WC_TEST_RET_ENC_EC(ret);
  43547. ret = mp_mul(a, b, NULL);
  43548. if (ret != MP_VAL)
  43549. return WC_TEST_RET_ENC_EC(ret);
  43550. ret = mp_mul(a, NULL, r);
  43551. if (ret != MP_VAL)
  43552. return WC_TEST_RET_ENC_EC(ret);
  43553. ret = mp_mul(NULL, b, r);
  43554. if (ret != MP_VAL)
  43555. return WC_TEST_RET_ENC_EC(ret);
  43556. #endif
  43557. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  43558. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  43559. ret = mp_sqr(NULL, NULL);
  43560. if (ret != MP_VAL)
  43561. return WC_TEST_RET_ENC_EC(ret);
  43562. ret = mp_sqr(a, NULL);
  43563. if (ret != MP_VAL)
  43564. return WC_TEST_RET_ENC_EC(ret);
  43565. ret = mp_sqr(NULL, r);
  43566. if (ret != MP_VAL)
  43567. return WC_TEST_RET_ENC_EC(ret);
  43568. #endif
  43569. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  43570. ret = mp_sqrmod(NULL, NULL, NULL);
  43571. if (ret != MP_VAL)
  43572. return WC_TEST_RET_ENC_EC(ret);
  43573. ret = mp_sqrmod(a, NULL, NULL);
  43574. if (ret != MP_VAL)
  43575. return WC_TEST_RET_ENC_EC(ret);
  43576. ret = mp_sqrmod(NULL, a, NULL);
  43577. if (ret != MP_VAL)
  43578. return WC_TEST_RET_ENC_EC(ret);
  43579. ret = mp_sqrmod(NULL, NULL, a);
  43580. if (ret != MP_VAL)
  43581. return WC_TEST_RET_ENC_EC(ret);
  43582. ret = mp_sqrmod(a, b, NULL);
  43583. if (ret != MP_VAL)
  43584. return WC_TEST_RET_ENC_EC(ret);
  43585. ret = mp_sqrmod(a, NULL, b);
  43586. if (ret != MP_VAL)
  43587. return WC_TEST_RET_ENC_EC(ret);
  43588. ret = mp_sqrmod(NULL, a, b);
  43589. if (ret != MP_VAL)
  43590. return WC_TEST_RET_ENC_EC(ret);
  43591. ret = mp_mulmod(NULL, NULL, NULL, NULL);
  43592. if (ret != MP_VAL)
  43593. return WC_TEST_RET_ENC_EC(ret);
  43594. ret = mp_mulmod(a, NULL, NULL, NULL);
  43595. if (ret != MP_VAL)
  43596. return WC_TEST_RET_ENC_EC(ret);
  43597. ret = mp_mulmod(NULL, a, NULL, NULL);
  43598. if (ret != MP_VAL)
  43599. return WC_TEST_RET_ENC_EC(ret);
  43600. ret = mp_mulmod(NULL, NULL, a, NULL);
  43601. if (ret != MP_VAL)
  43602. return WC_TEST_RET_ENC_EC(ret);
  43603. ret = mp_mulmod(NULL, NULL, NULL, a);
  43604. if (ret != MP_VAL)
  43605. return WC_TEST_RET_ENC_EC(ret);
  43606. ret = mp_mulmod(a, b, b, NULL);
  43607. if (ret != MP_VAL)
  43608. return WC_TEST_RET_ENC_EC(ret);
  43609. ret = mp_mulmod(a, b, NULL, a);
  43610. if (ret != MP_VAL)
  43611. return WC_TEST_RET_ENC_EC(ret);
  43612. ret = mp_mulmod(a, NULL, b, a);
  43613. if (ret != MP_VAL)
  43614. return WC_TEST_RET_ENC_EC(ret);
  43615. ret = mp_mulmod(NULL, b, b, a);
  43616. if (ret != MP_VAL)
  43617. return WC_TEST_RET_ENC_EC(ret);
  43618. #endif
  43619. #if !defined(NO_PWDBASED) || defined(WOLFSSL_KEY_GEN) || !defined(NO_DH) || \
  43620. !defined(NO_RSA) || !defined(NO_DSA)
  43621. ret = mp_add_d(NULL, 1, NULL);
  43622. if (ret != MP_VAL)
  43623. return WC_TEST_RET_ENC_EC(ret);
  43624. ret = mp_add_d(a, 1, NULL);
  43625. if (ret != MP_VAL)
  43626. return WC_TEST_RET_ENC_EC(ret);
  43627. ret = mp_add_d(NULL, 1, b);
  43628. if (ret != MP_VAL)
  43629. return WC_TEST_RET_ENC_EC(ret);
  43630. #endif
  43631. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  43632. !defined(NO_DH) || defined(HAVE_ECC) || !defined(NO_DSA)
  43633. ret = mp_sub_d(NULL, 1, NULL);
  43634. if (ret != MP_VAL)
  43635. return WC_TEST_RET_ENC_EC(ret);
  43636. ret = mp_sub_d(a, 1, NULL);
  43637. if (ret != MP_VAL)
  43638. return WC_TEST_RET_ENC_EC(ret);
  43639. ret = mp_sub_d(NULL, 1, b);
  43640. if (ret != MP_VAL)
  43641. return WC_TEST_RET_ENC_EC(ret);
  43642. #endif
  43643. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  43644. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  43645. ret = mp_div_d(NULL, 0, NULL, NULL);
  43646. if (ret != MP_VAL)
  43647. return WC_TEST_RET_ENC_EC(ret);
  43648. ret = mp_div_d(a, 0, NULL, NULL);
  43649. if (ret != MP_VAL)
  43650. return WC_TEST_RET_ENC_EC(ret);
  43651. ret = mp_div_d(NULL, 1, NULL, NULL);
  43652. if (ret != MP_VAL)
  43653. return WC_TEST_RET_ENC_EC(ret);
  43654. #endif
  43655. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  43656. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  43657. ret = mp_mod_d(NULL, 0, NULL);
  43658. if (ret != MP_VAL)
  43659. return WC_TEST_RET_ENC_EC(ret);
  43660. ret = mp_mod_d(a, 0, NULL);
  43661. if (ret != MP_VAL)
  43662. return WC_TEST_RET_ENC_EC(ret);
  43663. ret = mp_mod_d(NULL, 0, &rd);
  43664. if (ret != MP_VAL)
  43665. return WC_TEST_RET_ENC_EC(ret);
  43666. #endif
  43667. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  43668. ret = mp_gcd(NULL, NULL, NULL);
  43669. if (ret != MP_VAL)
  43670. return WC_TEST_RET_ENC_EC(ret);
  43671. ret = mp_gcd(a, NULL, NULL);
  43672. if (ret != MP_VAL)
  43673. return WC_TEST_RET_ENC_EC(ret);
  43674. ret = mp_gcd(NULL, a, NULL);
  43675. if (ret != MP_VAL)
  43676. return WC_TEST_RET_ENC_EC(ret);
  43677. ret = mp_gcd(NULL, NULL, a);
  43678. if (ret != MP_VAL)
  43679. return WC_TEST_RET_ENC_EC(ret);
  43680. ret = mp_gcd(a, b, NULL);
  43681. if (ret != MP_VAL)
  43682. return WC_TEST_RET_ENC_EC(ret);
  43683. ret = mp_gcd(a, NULL, b);
  43684. if (ret != MP_VAL)
  43685. return WC_TEST_RET_ENC_EC(ret);
  43686. ret = mp_gcd(NULL, a, b);
  43687. if (ret != MP_VAL)
  43688. return WC_TEST_RET_ENC_EC(ret);
  43689. #endif
  43690. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  43691. ret = mp_div_2_mod_ct(NULL, NULL, NULL);
  43692. if (ret != MP_VAL)
  43693. return WC_TEST_RET_ENC_EC(ret);
  43694. ret = mp_div_2_mod_ct(a, NULL, NULL);
  43695. if (ret != MP_VAL)
  43696. return WC_TEST_RET_ENC_EC(ret);
  43697. ret = mp_div_2_mod_ct(NULL, b, NULL);
  43698. if (ret != MP_VAL)
  43699. return WC_TEST_RET_ENC_EC(ret);
  43700. ret = mp_div_2_mod_ct(NULL, NULL, a);
  43701. if (ret != MP_VAL)
  43702. return WC_TEST_RET_ENC_EC(ret);
  43703. ret = mp_div_2_mod_ct(a, b, NULL);
  43704. if (ret != MP_VAL)
  43705. return WC_TEST_RET_ENC_EC(ret);
  43706. ret = mp_div_2_mod_ct(a, b, NULL);
  43707. if (ret != MP_VAL)
  43708. return WC_TEST_RET_ENC_EC(ret);
  43709. ret = mp_div_2_mod_ct(NULL, b, a);
  43710. if (ret != MP_VAL)
  43711. return WC_TEST_RET_ENC_EC(ret);
  43712. ret = mp_div_2(NULL, NULL);
  43713. if (ret != MP_VAL)
  43714. return WC_TEST_RET_ENC_EC(ret);
  43715. ret = mp_div_2(a, NULL);
  43716. if (ret != MP_VAL)
  43717. return WC_TEST_RET_ENC_EC(ret);
  43718. ret = mp_div_2(NULL, a);
  43719. if (ret != MP_VAL)
  43720. return WC_TEST_RET_ENC_EC(ret);
  43721. #endif
  43722. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  43723. defined(HAVE_ECC) || !defined(NO_DSA) || defined(OPENSSL_EXTRA)
  43724. ret = mp_invmod(NULL, NULL, NULL);
  43725. if (ret != MP_VAL)
  43726. return WC_TEST_RET_ENC_EC(ret);
  43727. ret = mp_invmod(a, NULL, NULL);
  43728. if (ret != MP_VAL)
  43729. return WC_TEST_RET_ENC_EC(ret);
  43730. ret = mp_invmod(NULL, b, NULL);
  43731. if (ret != MP_VAL)
  43732. return WC_TEST_RET_ENC_EC(ret);
  43733. ret = mp_invmod(NULL, NULL, a);
  43734. if (ret != MP_VAL)
  43735. return WC_TEST_RET_ENC_EC(ret);
  43736. ret = mp_invmod(a, b, NULL);
  43737. if (ret != MP_VAL)
  43738. return WC_TEST_RET_ENC_EC(ret);
  43739. ret = mp_invmod(a, NULL, a);
  43740. if (ret != MP_VAL)
  43741. return WC_TEST_RET_ENC_EC(ret);
  43742. ret = mp_invmod(NULL, b, a);
  43743. if (ret != MP_VAL)
  43744. return WC_TEST_RET_ENC_EC(ret);
  43745. #endif
  43746. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  43747. ret = mp_invmod_mont_ct(NULL, NULL, NULL, 1);
  43748. if (ret != MP_VAL)
  43749. return WC_TEST_RET_ENC_EC(ret);
  43750. ret = mp_invmod_mont_ct(a, NULL, NULL, 1);
  43751. if (ret != MP_VAL)
  43752. return WC_TEST_RET_ENC_EC(ret);
  43753. ret = mp_invmod_mont_ct(NULL, b, NULL, 1);
  43754. if (ret != MP_VAL)
  43755. return WC_TEST_RET_ENC_EC(ret);
  43756. ret = mp_invmod_mont_ct(NULL, NULL, a, 1);
  43757. if (ret != MP_VAL)
  43758. return WC_TEST_RET_ENC_EC(ret);
  43759. ret = mp_invmod_mont_ct(a, b, NULL, 1);
  43760. if (ret != MP_VAL)
  43761. return WC_TEST_RET_ENC_EC(ret);
  43762. ret = mp_invmod_mont_ct(a, NULL, a, 1);
  43763. if (ret != MP_VAL)
  43764. return WC_TEST_RET_ENC_EC(ret);
  43765. ret = mp_invmod_mont_ct(NULL, b, a, 1);
  43766. if (ret != MP_VAL)
  43767. return WC_TEST_RET_ENC_EC(ret);
  43768. #endif
  43769. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  43770. ret = mp_lcm(NULL, NULL, NULL);
  43771. if (ret != MP_VAL)
  43772. return WC_TEST_RET_ENC_EC(ret);
  43773. ret = mp_lcm(a, NULL, NULL);
  43774. if (ret != MP_VAL)
  43775. return WC_TEST_RET_ENC_EC(ret);
  43776. ret = mp_lcm(NULL, b, NULL);
  43777. if (ret != MP_VAL)
  43778. return WC_TEST_RET_ENC_EC(ret);
  43779. ret = mp_lcm(NULL, NULL, a);
  43780. if (ret != MP_VAL)
  43781. return WC_TEST_RET_ENC_EC(ret);
  43782. ret = mp_lcm(a, b, NULL);
  43783. if (ret != MP_VAL)
  43784. return WC_TEST_RET_ENC_EC(ret);
  43785. ret = mp_lcm(a, NULL, a);
  43786. if (ret != MP_VAL)
  43787. return WC_TEST_RET_ENC_EC(ret);
  43788. ret = mp_lcm(NULL, b, a);
  43789. if (ret != MP_VAL)
  43790. return WC_TEST_RET_ENC_EC(ret);
  43791. #endif
  43792. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  43793. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  43794. if (ret != MP_VAL)
  43795. return WC_TEST_RET_ENC_EC(ret);
  43796. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  43797. if (ret != MP_VAL)
  43798. return WC_TEST_RET_ENC_EC(ret);
  43799. ret = mp_exptmod_ex(NULL, b, 1, NULL, NULL);
  43800. if (ret != MP_VAL)
  43801. return WC_TEST_RET_ENC_EC(ret);
  43802. ret = mp_exptmod_ex(NULL, NULL, 1, b, NULL);
  43803. if (ret != MP_VAL)
  43804. return WC_TEST_RET_ENC_EC(ret);
  43805. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  43806. if (ret != MP_VAL)
  43807. return WC_TEST_RET_ENC_EC(ret);
  43808. ret = mp_exptmod_ex(a, b, 1, b, NULL);
  43809. if (ret != MP_VAL)
  43810. return WC_TEST_RET_ENC_EC(ret);
  43811. ret = mp_exptmod_ex(a, b, 1, NULL, a);
  43812. if (ret != MP_VAL)
  43813. return WC_TEST_RET_ENC_EC(ret);
  43814. ret = mp_exptmod_ex(a, NULL, 1, b, a);
  43815. if (ret != MP_VAL)
  43816. return WC_TEST_RET_ENC_EC(ret);
  43817. ret = mp_exptmod_ex(NULL, b, 1, b, a);
  43818. if (ret != MP_VAL)
  43819. return WC_TEST_RET_ENC_EC(ret);
  43820. ret = mp_exptmod(NULL, NULL, NULL, NULL);
  43821. if (ret != MP_VAL)
  43822. return WC_TEST_RET_ENC_EC(ret);
  43823. ret = mp_exptmod(a, NULL, NULL, NULL);
  43824. if (ret != MP_VAL)
  43825. return WC_TEST_RET_ENC_EC(ret);
  43826. ret = mp_exptmod(NULL, b, NULL, NULL);
  43827. if (ret != MP_VAL)
  43828. return WC_TEST_RET_ENC_EC(ret);
  43829. ret = mp_exptmod(NULL, NULL, b, NULL);
  43830. if (ret != MP_VAL)
  43831. return WC_TEST_RET_ENC_EC(ret);
  43832. ret = mp_exptmod(NULL, NULL, NULL, a);
  43833. if (ret != MP_VAL)
  43834. return WC_TEST_RET_ENC_EC(ret);
  43835. ret = mp_exptmod(a, b, b, NULL);
  43836. if (ret != MP_VAL)
  43837. return WC_TEST_RET_ENC_EC(ret);
  43838. ret = mp_exptmod(a, b, NULL, a);
  43839. if (ret != MP_VAL)
  43840. return WC_TEST_RET_ENC_EC(ret);
  43841. ret = mp_exptmod(a, NULL, b, a);
  43842. if (ret != MP_VAL)
  43843. return WC_TEST_RET_ENC_EC(ret);
  43844. ret = mp_exptmod(NULL, b, b, a);
  43845. if (ret != MP_VAL)
  43846. return WC_TEST_RET_ENC_EC(ret);
  43847. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  43848. if (ret != MP_VAL)
  43849. return WC_TEST_RET_ENC_EC(ret);
  43850. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  43851. if (ret != MP_VAL)
  43852. return WC_TEST_RET_ENC_EC(ret);
  43853. ret = mp_exptmod_nct(NULL, b, NULL, NULL);
  43854. if (ret != MP_VAL)
  43855. return WC_TEST_RET_ENC_EC(ret);
  43856. ret = mp_exptmod_nct(NULL, NULL, b, NULL);
  43857. if (ret != MP_VAL)
  43858. return WC_TEST_RET_ENC_EC(ret);
  43859. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  43860. if (ret != MP_VAL)
  43861. return WC_TEST_RET_ENC_EC(ret);
  43862. ret = mp_exptmod_nct(a, b, b, NULL);
  43863. if (ret != MP_VAL)
  43864. return WC_TEST_RET_ENC_EC(ret);
  43865. ret = mp_exptmod_nct(a, b, NULL, a);
  43866. if (ret != MP_VAL)
  43867. return WC_TEST_RET_ENC_EC(ret);
  43868. ret = mp_exptmod_nct(a, NULL, b, a);
  43869. if (ret != MP_VAL)
  43870. return WC_TEST_RET_ENC_EC(ret);
  43871. ret = mp_exptmod_nct(NULL, b, b, a);
  43872. if (ret != MP_VAL)
  43873. return WC_TEST_RET_ENC_EC(ret);
  43874. #endif
  43875. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  43876. ret = mp_cnt_lsb(NULL);
  43877. if (ret != 0)
  43878. return WC_TEST_RET_ENC_EC(ret);
  43879. #endif
  43880. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  43881. ret = mp_prime_is_prime(NULL, 1, NULL);
  43882. if (ret != MP_VAL)
  43883. return WC_TEST_RET_ENC_EC(ret);
  43884. ret = mp_prime_is_prime(a, 1, NULL);
  43885. if (ret != MP_VAL)
  43886. return WC_TEST_RET_ENC_EC(ret);
  43887. ret = mp_prime_is_prime(NULL, 1, &result);
  43888. if (ret != MP_VAL)
  43889. return WC_TEST_RET_ENC_EC(ret);
  43890. ret = mp_prime_is_prime(a, 0, &result);
  43891. if (ret != MP_VAL)
  43892. return WC_TEST_RET_ENC_EC(ret);
  43893. ret = mp_prime_is_prime(a, 1024, &result);
  43894. if (ret != MP_VAL)
  43895. return WC_TEST_RET_ENC_EC(ret);
  43896. ret = mp_prime_is_prime_ex(NULL, 1, NULL, NULL);
  43897. if (ret != MP_VAL)
  43898. return WC_TEST_RET_ENC_EC(ret);
  43899. ret = mp_prime_is_prime_ex(a, 1, NULL, NULL);
  43900. if (ret != MP_VAL)
  43901. return WC_TEST_RET_ENC_EC(ret);
  43902. ret = mp_prime_is_prime_ex(NULL, 1, &result, NULL);
  43903. if (ret != MP_VAL)
  43904. return WC_TEST_RET_ENC_EC(ret);
  43905. ret = mp_prime_is_prime_ex(NULL, 1, NULL, rng);
  43906. if (ret != MP_VAL)
  43907. return WC_TEST_RET_ENC_EC(ret);
  43908. ret = mp_prime_is_prime_ex(a, 1, &result, NULL);
  43909. if (ret != MP_VAL)
  43910. return WC_TEST_RET_ENC_EC(ret);
  43911. ret = mp_prime_is_prime_ex(a, 1, NULL, rng);
  43912. if (ret != MP_VAL)
  43913. return WC_TEST_RET_ENC_EC(ret);
  43914. ret = mp_prime_is_prime_ex(NULL, 1, &result, rng);
  43915. if (ret != MP_VAL)
  43916. return WC_TEST_RET_ENC_EC(ret);
  43917. #endif
  43918. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || !defined(NO_DSA)
  43919. ret = mp_exch(NULL, NULL);
  43920. if (ret != MP_VAL)
  43921. return WC_TEST_RET_ENC_EC(ret);
  43922. ret = mp_exch(a, NULL);
  43923. if (ret != MP_VAL)
  43924. return WC_TEST_RET_ENC_EC(ret);
  43925. ret = mp_exch(NULL, b);
  43926. if (ret != MP_VAL)
  43927. return WC_TEST_RET_ENC_EC(ret);
  43928. #endif
  43929. #if (defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)) || \
  43930. defined(WOLFSSL_SP_MATH_ALL)
  43931. ret = mp_mul_d(NULL, 1, NULL);
  43932. if (ret != MP_VAL)
  43933. return WC_TEST_RET_ENC_EC(ret);
  43934. ret = mp_mul_d(a, 1, NULL);
  43935. if (ret != MP_VAL)
  43936. return WC_TEST_RET_ENC_EC(ret);
  43937. ret = mp_mul_d(NULL, 1, b);
  43938. if (ret != MP_VAL)
  43939. return WC_TEST_RET_ENC_EC(ret);
  43940. #endif
  43941. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  43942. ret = mp_add(NULL, NULL, NULL);
  43943. if (ret != MP_VAL)
  43944. return WC_TEST_RET_ENC_EC(ret);
  43945. ret = mp_add(a, NULL, NULL);
  43946. if (ret != MP_VAL)
  43947. return WC_TEST_RET_ENC_EC(ret);
  43948. ret = mp_add(NULL, b, NULL);
  43949. if (ret != MP_VAL)
  43950. return WC_TEST_RET_ENC_EC(ret);
  43951. ret = mp_add(NULL, NULL, r);
  43952. if (ret != MP_VAL)
  43953. return WC_TEST_RET_ENC_EC(ret);
  43954. ret = mp_add(a, b, NULL);
  43955. if (ret != MP_VAL)
  43956. return WC_TEST_RET_ENC_EC(ret);
  43957. ret = mp_add(a, NULL, r);
  43958. if (ret != MP_VAL)
  43959. return WC_TEST_RET_ENC_EC(ret);
  43960. ret = mp_add(NULL, b, r);
  43961. if (ret != MP_VAL)
  43962. return WC_TEST_RET_ENC_EC(ret);
  43963. #endif
  43964. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  43965. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  43966. ret = mp_sub(NULL, NULL, NULL);
  43967. if (ret != MP_VAL)
  43968. return WC_TEST_RET_ENC_EC(ret);
  43969. ret = mp_sub(a, NULL, NULL);
  43970. if (ret != MP_VAL)
  43971. return WC_TEST_RET_ENC_EC(ret);
  43972. ret = mp_sub(NULL, b, NULL);
  43973. if (ret != MP_VAL)
  43974. return WC_TEST_RET_ENC_EC(ret);
  43975. ret = mp_sub(NULL, NULL, r);
  43976. if (ret != MP_VAL)
  43977. return WC_TEST_RET_ENC_EC(ret);
  43978. ret = mp_sub(a, b, NULL);
  43979. if (ret != MP_VAL)
  43980. return WC_TEST_RET_ENC_EC(ret);
  43981. ret = mp_sub(a, NULL, r);
  43982. if (ret != MP_VAL)
  43983. return WC_TEST_RET_ENC_EC(ret);
  43984. ret = mp_sub(NULL, b, r);
  43985. if (ret != MP_VAL)
  43986. return WC_TEST_RET_ENC_EC(ret);
  43987. #endif
  43988. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined(WOLFSSL_SP_MATH) && \
  43989. defined(WOLFSSL_CUSTOM_CURVES))
  43990. ret = mp_addmod(NULL, NULL, NULL, NULL);
  43991. if (ret != MP_VAL)
  43992. return WC_TEST_RET_ENC_EC(ret);
  43993. ret = mp_addmod(a, NULL, NULL, NULL);
  43994. if (ret != MP_VAL)
  43995. return WC_TEST_RET_ENC_EC(ret);
  43996. ret = mp_addmod(NULL, b, NULL, NULL);
  43997. if (ret != MP_VAL)
  43998. return WC_TEST_RET_ENC_EC(ret);
  43999. ret = mp_addmod(NULL, NULL, b, NULL);
  44000. if (ret != MP_VAL)
  44001. return WC_TEST_RET_ENC_EC(ret);
  44002. ret = mp_addmod(NULL, NULL, NULL, a);
  44003. if (ret != MP_VAL)
  44004. return WC_TEST_RET_ENC_EC(ret);
  44005. ret = mp_addmod(a, b, b, NULL);
  44006. if (ret != MP_VAL)
  44007. return WC_TEST_RET_ENC_EC(ret);
  44008. ret = mp_addmod(a, b, NULL, a);
  44009. if (ret != MP_VAL)
  44010. return WC_TEST_RET_ENC_EC(ret);
  44011. ret = mp_addmod(a, NULL, b, a);
  44012. if (ret != MP_VAL)
  44013. return WC_TEST_RET_ENC_EC(ret);
  44014. ret = mp_addmod(NULL, b, b, a);
  44015. if (ret != MP_VAL)
  44016. return WC_TEST_RET_ENC_EC(ret);
  44017. #endif
  44018. #ifdef WOLFSSL_SP_MATH_ALL
  44019. ret = mp_submod(NULL, NULL, NULL, NULL);
  44020. if (ret != MP_VAL)
  44021. return WC_TEST_RET_ENC_EC(ret);
  44022. ret = mp_submod(a, NULL, NULL, NULL);
  44023. if (ret != MP_VAL)
  44024. return WC_TEST_RET_ENC_EC(ret);
  44025. ret = mp_submod(NULL, b, NULL, NULL);
  44026. if (ret != MP_VAL)
  44027. return WC_TEST_RET_ENC_EC(ret);
  44028. ret = mp_submod(NULL, NULL, b, NULL);
  44029. if (ret != MP_VAL)
  44030. return WC_TEST_RET_ENC_EC(ret);
  44031. ret = mp_submod(NULL, NULL, NULL, a);
  44032. if (ret != MP_VAL)
  44033. return WC_TEST_RET_ENC_EC(ret);
  44034. ret = mp_submod(a, b, b, NULL);
  44035. if (ret != MP_VAL)
  44036. return WC_TEST_RET_ENC_EC(ret);
  44037. ret = mp_submod(a, b, NULL, a);
  44038. if (ret != MP_VAL)
  44039. return WC_TEST_RET_ENC_EC(ret);
  44040. ret = mp_submod(a, NULL, b, a);
  44041. if (ret != MP_VAL)
  44042. return WC_TEST_RET_ENC_EC(ret);
  44043. ret = mp_submod(NULL, b, b, a);
  44044. if (ret != MP_VAL)
  44045. return WC_TEST_RET_ENC_EC(ret);
  44046. #endif
  44047. #ifdef WOLFSSL_SP_MATH_ALL
  44048. ret = mp_div_2d(NULL, 1, a, b);
  44049. if (ret != MP_VAL)
  44050. return WC_TEST_RET_ENC_EC(ret);
  44051. ret = mp_mod_2d(NULL, 1, NULL);
  44052. if (ret != MP_VAL)
  44053. return WC_TEST_RET_ENC_EC(ret);
  44054. ret = mp_mod_2d(a, 1, NULL);
  44055. if (ret != MP_VAL)
  44056. return WC_TEST_RET_ENC_EC(ret);
  44057. ret = mp_mod_2d(NULL, 1, b);
  44058. if (ret != MP_VAL)
  44059. return WC_TEST_RET_ENC_EC(ret);
  44060. ret = mp_mul_2d(NULL, 1, NULL);
  44061. if (ret != MP_VAL)
  44062. return WC_TEST_RET_ENC_EC(ret);
  44063. ret = mp_mul_2d(a, 1, NULL);
  44064. if (ret != MP_VAL)
  44065. return WC_TEST_RET_ENC_EC(ret);
  44066. ret = mp_mul_2d(NULL, 1, b);
  44067. if (ret != MP_VAL)
  44068. return WC_TEST_RET_ENC_EC(ret);
  44069. #endif
  44070. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  44071. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  44072. ret = mp_montgomery_reduce(NULL, NULL, 1);
  44073. if (ret != MP_VAL)
  44074. return WC_TEST_RET_ENC_EC(ret);
  44075. ret = mp_montgomery_reduce(a, NULL, 1);
  44076. if (ret != MP_VAL)
  44077. return WC_TEST_RET_ENC_EC(ret);
  44078. ret = mp_montgomery_reduce(NULL, b, 1);
  44079. if (ret != MP_VAL)
  44080. return WC_TEST_RET_ENC_EC(ret);
  44081. mp_zero(b);
  44082. ret = mp_montgomery_reduce(a, b, 1);
  44083. if (ret != MP_VAL)
  44084. return WC_TEST_RET_ENC_EC(ret);
  44085. #endif
  44086. #ifdef WOLFSSL_SP_MATH_ALL
  44087. ret = mp_montgomery_setup(NULL, NULL);
  44088. if (ret != MP_VAL)
  44089. return WC_TEST_RET_ENC_EC(ret);
  44090. ret = mp_montgomery_setup(a, NULL);
  44091. if (ret != MP_VAL)
  44092. return WC_TEST_RET_ENC_EC(ret);
  44093. ret = mp_montgomery_setup(NULL, &rho);
  44094. if (ret != MP_VAL)
  44095. return WC_TEST_RET_ENC_EC(ret);
  44096. ret = mp_montgomery_calc_normalization(NULL, NULL);
  44097. if (ret != MP_VAL)
  44098. return WC_TEST_RET_ENC_EC(ret);
  44099. ret = mp_montgomery_calc_normalization(a, NULL);
  44100. if (ret != MP_VAL)
  44101. return WC_TEST_RET_ENC_EC(ret);
  44102. ret = mp_montgomery_calc_normalization(NULL, b);
  44103. if (ret != MP_VAL)
  44104. return WC_TEST_RET_ENC_EC(ret);
  44105. #endif
  44106. ret = mp_unsigned_bin_size(NULL);
  44107. if (ret != 0)
  44108. return WC_TEST_RET_ENC_EC(ret);
  44109. #if defined(WC_MP_TO_RADIX) || defined(WOLFSSL_SP_MATH_ALL)
  44110. ret = mp_tohex(NULL, NULL);
  44111. if (ret != MP_VAL)
  44112. return WC_TEST_RET_ENC_EC(ret);
  44113. ret = mp_tohex(a, NULL);
  44114. if (ret != MP_VAL)
  44115. return WC_TEST_RET_ENC_EC(ret);
  44116. ret = mp_tohex(NULL, hexStr);
  44117. if (ret != MP_VAL)
  44118. return WC_TEST_RET_ENC_EC(ret);
  44119. #endif
  44120. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  44121. ret = mp_todecimal(NULL, NULL);
  44122. if (ret != MP_VAL)
  44123. return WC_TEST_RET_ENC_EC(ret);
  44124. ret = mp_todecimal(a, NULL);
  44125. if (ret != MP_VAL)
  44126. return WC_TEST_RET_ENC_EC(ret);
  44127. ret = mp_todecimal(NULL, decStr);
  44128. if (ret != MP_VAL)
  44129. return WC_TEST_RET_ENC_EC(ret);
  44130. #endif
  44131. #ifdef WOLFSSL_SP_MATH_ALL
  44132. ret = mp_toradix(NULL, NULL, MP_RADIX_HEX);
  44133. if (ret != MP_VAL)
  44134. return WC_TEST_RET_ENC_EC(ret);
  44135. ret = mp_toradix(a, NULL, MP_RADIX_HEX);
  44136. if (ret != MP_VAL)
  44137. return WC_TEST_RET_ENC_EC(ret);
  44138. ret = mp_toradix(NULL, hexStr, MP_RADIX_HEX);
  44139. if (ret != MP_VAL)
  44140. return WC_TEST_RET_ENC_EC(ret);
  44141. ret = mp_toradix(a, hexStr, 3);
  44142. if (ret != MP_VAL)
  44143. return WC_TEST_RET_ENC_EC(ret);
  44144. ret = mp_radix_size(NULL, MP_RADIX_HEX, NULL);
  44145. if (ret != MP_VAL)
  44146. return WC_TEST_RET_ENC_EC(ret);
  44147. ret = mp_radix_size(a, MP_RADIX_HEX, NULL);
  44148. if (ret != MP_VAL)
  44149. return WC_TEST_RET_ENC_EC(ret);
  44150. ret = mp_radix_size(NULL, MP_RADIX_HEX, &size);
  44151. if (ret != MP_VAL)
  44152. return WC_TEST_RET_ENC_EC(ret);
  44153. ret = mp_radix_size(a, 3, &size);
  44154. if (ret != MP_VAL)
  44155. return WC_TEST_RET_ENC_EC(ret);
  44156. #endif
  44157. return 0;
  44158. }
  44159. #endif
  44160. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  44161. static wc_test_ret_t mp_test_set_is_bit(mp_int* a)
  44162. {
  44163. int i, j;
  44164. wc_test_ret_t ret;
  44165. mp_zero(a);
  44166. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  44167. if (mp_is_bit_set(a, i))
  44168. return WC_TEST_RET_ENC_NC;
  44169. for (j = 0; j < i; j++) {
  44170. if (!mp_is_bit_set(a, j))
  44171. return WC_TEST_RET_ENC_NC;
  44172. }
  44173. if (mp_set_bit(a, i) != 0)
  44174. return WC_TEST_RET_ENC_NC;
  44175. if (!mp_is_bit_set(a, i))
  44176. return WC_TEST_RET_ENC_NC;
  44177. }
  44178. mp_zero(a);
  44179. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  44180. if (mp_is_bit_set(a, i))
  44181. return WC_TEST_RET_ENC_NC;
  44182. }
  44183. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  44184. mp_zero(a);
  44185. if (mp_set_bit(a, i) != 0)
  44186. return WC_TEST_RET_ENC_NC;
  44187. for (j = 0; j < i; j++) {
  44188. if (mp_is_bit_set(a, j))
  44189. return WC_TEST_RET_ENC_NC;
  44190. }
  44191. if (!mp_is_bit_set(a, i))
  44192. return WC_TEST_RET_ENC_NC;
  44193. }
  44194. #ifdef WOLFSSL_KEY_GEN
  44195. for (i = 0; i < DIGIT_BIT * 2; i++) {
  44196. mp_set(a, 1);
  44197. ret = mp_2expt(a, i);
  44198. if (ret != 0)
  44199. return WC_TEST_RET_ENC_EC(ret);
  44200. for (j = 0; j < i; j++) {
  44201. if (mp_is_bit_set(a, j))
  44202. return WC_TEST_RET_ENC_NC;
  44203. }
  44204. if (!mp_is_bit_set(a, i))
  44205. return WC_TEST_RET_ENC_NC;
  44206. }
  44207. #endif
  44208. #ifdef WOLFSSL_SP_MATH
  44209. mp_zero(a);
  44210. for (j = 1; j <= 3; j++) {
  44211. i = SP_INT_MAX_BITS - j;
  44212. if (mp_is_bit_set(a, i))
  44213. return WC_TEST_RET_ENC_NC;
  44214. if (mp_set_bit(a, i) != 0)
  44215. return WC_TEST_RET_ENC_NC;
  44216. if (!mp_is_bit_set(a, i))
  44217. return WC_TEST_RET_ENC_NC;
  44218. #ifdef WOLFSSL_KEY_GEN
  44219. ret = mp_2expt(a, i);
  44220. if (ret != 0)
  44221. return WC_TEST_RET_ENC_EC(ret);
  44222. if (!mp_is_bit_set(a, i))
  44223. return WC_TEST_RET_ENC_NC;
  44224. #endif
  44225. }
  44226. mp_zero(a);
  44227. for (j = 0; j <= 3; j++) {
  44228. i = SP_INT_MAX_BITS + j;
  44229. if (mp_is_bit_set(a, i))
  44230. return WC_TEST_RET_ENC_NC;
  44231. if (mp_set_bit(a, i) != MP_VAL)
  44232. return WC_TEST_RET_ENC_NC;
  44233. #ifdef WOLFSSL_KEY_GEN
  44234. ret = mp_2expt(a, i);
  44235. if (ret != MP_VAL)
  44236. return WC_TEST_RET_ENC_EC(ret);
  44237. #endif
  44238. }
  44239. #endif
  44240. (void)ret;
  44241. return 0;
  44242. }
  44243. #endif /* !WOLFSSL_SP_MATH || WOLFSSL_SP_MATH_ALL */
  44244. static wc_test_ret_t mp_test_cmp(mp_int* a, mp_int* b)
  44245. {
  44246. wc_test_ret_t ret;
  44247. mp_zero(a);
  44248. mp_zero(b);
  44249. ret = mp_cmp_d(a, 0);
  44250. if (ret != MP_EQ)
  44251. return WC_TEST_RET_ENC_EC(ret);
  44252. ret = mp_cmp_d(a, 1);
  44253. if (ret != MP_LT)
  44254. return WC_TEST_RET_ENC_EC(ret);
  44255. ret = mp_cmp(a, b);
  44256. if (ret != MP_EQ)
  44257. return WC_TEST_RET_ENC_NC;
  44258. mp_set(a, 1);
  44259. ret = mp_cmp_d(a, 0);
  44260. if (ret != MP_GT)
  44261. return WC_TEST_RET_ENC_EC(ret);
  44262. ret = mp_cmp_d(a, 1);
  44263. if (ret != MP_EQ)
  44264. return WC_TEST_RET_ENC_EC(ret);
  44265. ret = mp_cmp_d(a, 2);
  44266. if (ret != MP_LT)
  44267. return WC_TEST_RET_ENC_EC(ret);
  44268. ret = mp_cmp(a, b);
  44269. if (ret != MP_GT)
  44270. return WC_TEST_RET_ENC_NC;
  44271. mp_read_radix(b, "1234567890123456789", MP_RADIX_HEX);
  44272. ret = mp_cmp_d(b, -1);
  44273. if (ret != MP_GT)
  44274. return WC_TEST_RET_ENC_EC(ret);
  44275. ret = mp_cmp(a, b);
  44276. if (ret != MP_LT)
  44277. return WC_TEST_RET_ENC_NC;
  44278. ret = mp_cmp(b, a);
  44279. if (ret != MP_GT)
  44280. return WC_TEST_RET_ENC_NC;
  44281. ret = mp_cmp(b, b);
  44282. if (ret != MP_EQ)
  44283. return WC_TEST_RET_ENC_NC;
  44284. #if (!defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)) || \
  44285. defined(WOLFSSL_SP_INT_NEGATIVE)
  44286. mp_read_radix(a, "-1", MP_RADIX_HEX);
  44287. mp_read_radix(a, "1", MP_RADIX_HEX);
  44288. ret = mp_cmp(a, b);
  44289. if (ret != MP_LT)
  44290. return WC_TEST_RET_ENC_NC;
  44291. ret = mp_cmp(b, a);
  44292. if (ret != MP_GT)
  44293. return WC_TEST_RET_ENC_NC;
  44294. mp_read_radix(b, "-2", MP_RADIX_HEX);
  44295. ret = mp_cmp(a, b);
  44296. if (ret != MP_GT)
  44297. return WC_TEST_RET_ENC_NC;
  44298. ret = mp_cmp(b, a);
  44299. if (ret != MP_LT)
  44300. return WC_TEST_RET_ENC_NC;
  44301. mp_read_radix(a, "-2", MP_RADIX_HEX);
  44302. ret = mp_cmp(a, b);
  44303. if (ret != MP_EQ)
  44304. return WC_TEST_RET_ENC_NC;
  44305. #endif
  44306. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  44307. defined(WOLFSSL_ECC_GEN_REJECT_SAMPLING)
  44308. mp_zero(a);
  44309. mp_zero(b);
  44310. ret = mp_cmp_ct(a, b, 1);
  44311. if (ret != MP_EQ)
  44312. return WC_TEST_RET_ENC_EC(ret);
  44313. ret = mp_cmp_ct(a, a, a->used);
  44314. if (ret != MP_EQ)
  44315. return WC_TEST_RET_ENC_EC(ret);
  44316. #ifdef WOLFSSL_SP_MATH_ALL
  44317. ret = mp_cmp_ct(a, NULL, a->used);
  44318. if (ret != MP_GT)
  44319. return WC_TEST_RET_ENC_EC(ret);
  44320. ret = mp_cmp_ct(NULL, a, a->used);
  44321. if (ret != MP_LT)
  44322. return WC_TEST_RET_ENC_EC(ret);
  44323. #endif
  44324. mp_read_radix(a, "1", MP_RADIX_HEX);
  44325. ret = mp_cmp_ct(a, b, 1);
  44326. if (ret != MP_GT)
  44327. return WC_TEST_RET_ENC_EC(ret);
  44328. ret = mp_cmp_ct(b, a, 1);
  44329. if (ret != MP_LT)
  44330. return WC_TEST_RET_ENC_EC(ret);
  44331. mp_read_radix(a, "0123456789abcdef0123456789abcdef", MP_RADIX_HEX);
  44332. ret = mp_cmp_ct(a, b, a->used);
  44333. if (ret != MP_GT)
  44334. return WC_TEST_RET_ENC_EC(ret);
  44335. ret = mp_cmp_ct(b, a, a->used);
  44336. if (ret != MP_LT)
  44337. return WC_TEST_RET_ENC_EC(ret);
  44338. mp_read_radix(b, "1123456789abcdef0123456789abcdef", MP_RADIX_HEX);
  44339. ret = mp_cmp_ct(b, a, a->used);
  44340. if (ret != MP_GT)
  44341. return WC_TEST_RET_ENC_EC(ret);
  44342. ret = mp_cmp_ct(a, b, a->used);
  44343. if (ret != MP_LT)
  44344. return WC_TEST_RET_ENC_EC(ret);
  44345. mp_read_radix(b, "0123456789abcdef0123456789abcdf0", MP_RADIX_HEX);
  44346. ret = mp_cmp_ct(b, a, a->used);
  44347. if (ret != MP_GT)
  44348. return WC_TEST_RET_ENC_EC(ret);
  44349. ret = mp_cmp_ct(a, b, a->used);
  44350. if (ret != MP_LT)
  44351. return WC_TEST_RET_ENC_EC(ret);
  44352. mp_read_radix(b, "0123456789abcdf0", MP_RADIX_HEX);
  44353. ret = mp_cmp_ct(a, b, a->used);
  44354. if (ret != MP_GT)
  44355. return WC_TEST_RET_ENC_EC(ret);
  44356. ret = mp_cmp_ct(b, a, a->used);
  44357. if (ret != MP_LT)
  44358. return WC_TEST_RET_ENC_EC(ret);
  44359. #endif
  44360. return 0;
  44361. }
  44362. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  44363. static wc_test_ret_t mp_test_shbd(mp_int* a, mp_int* b, WC_RNG* rng)
  44364. {
  44365. wc_test_ret_t ret;
  44366. int i, j, k;
  44367. #ifndef WOLFSSL_SP_MATH
  44368. for (i = 0; i < 10; i++) {
  44369. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  44370. ret = randNum(a, j, rng, NULL);
  44371. if (ret != MP_OKAY)
  44372. return WC_TEST_RET_ENC_EC(ret);
  44373. mp_copy(a, b);
  44374. for (k = 0; k <= DIGIT_BIT * 2; k++) {
  44375. ret = mp_mul_2d(a, k, a);
  44376. if (ret != MP_OKAY)
  44377. return WC_TEST_RET_ENC_EC(ret);
  44378. mp_rshb(a, k);
  44379. ret = mp_cmp(a, b);
  44380. if (ret != MP_EQ)
  44381. return WC_TEST_RET_ENC_NC;
  44382. }
  44383. }
  44384. }
  44385. #endif
  44386. for (i = 0; i < 10; i++) {
  44387. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  44388. ret = randNum(a, j, rng, NULL);
  44389. if (ret != MP_OKAY)
  44390. return WC_TEST_RET_ENC_EC(ret);
  44391. mp_copy(a, b);
  44392. for (k = 0; k < 10; k++) {
  44393. ret = mp_lshd(a, k);
  44394. if (ret != MP_OKAY)
  44395. return WC_TEST_RET_ENC_EC(ret);
  44396. #ifndef WOLFSSL_SP_MATH
  44397. mp_rshd(a, k);
  44398. #else
  44399. mp_rshb(a, k * SP_WORD_SIZE);
  44400. #endif
  44401. ret = mp_cmp(a, b);
  44402. if (ret != MP_EQ)
  44403. return WC_TEST_RET_ENC_NC;
  44404. }
  44405. }
  44406. }
  44407. #ifndef WOLFSSL_SP_MATH
  44408. mp_zero(a);
  44409. mp_rshd(a, 1);
  44410. if (!mp_iszero(a))
  44411. return WC_TEST_RET_ENC_NC;
  44412. mp_set(a, 1);
  44413. mp_rshd(a, 1);
  44414. if (!mp_iszero(a))
  44415. return WC_TEST_RET_ENC_NC;
  44416. mp_set(a, 1);
  44417. mp_rshd(a, 2);
  44418. if (!mp_iszero(a))
  44419. return WC_TEST_RET_ENC_NC;
  44420. #endif
  44421. return 0;
  44422. }
  44423. #endif
  44424. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  44425. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && \
  44426. !defined(WOLFSSL_RSA_PUBLIC_ONLY))
  44427. static wc_test_ret_t mp_test_div(mp_int* a, mp_int* d, mp_int* r, mp_int* rem,
  44428. WC_RNG* rng)
  44429. {
  44430. wc_test_ret_t ret;
  44431. int i, j, k;
  44432. mp_zero(a);
  44433. mp_zero(d);
  44434. ret = mp_div(a, d, r, rem);
  44435. if (ret != MP_VAL)
  44436. return WC_TEST_RET_ENC_EC(ret);
  44437. mp_set(d, 1);
  44438. ret = mp_div(a, d, r, rem);
  44439. if (ret != MP_OKAY)
  44440. return WC_TEST_RET_ENC_EC(ret);
  44441. if (!mp_iszero(r))
  44442. return WC_TEST_RET_ENC_EC(ret);
  44443. if (!mp_iszero(rem))
  44444. return WC_TEST_RET_ENC_EC(ret);
  44445. mp_set(a, 1);
  44446. ret = mp_div(a, d, r, rem);
  44447. if (ret != MP_OKAY)
  44448. return WC_TEST_RET_ENC_EC(ret);
  44449. if (!mp_isone(r))
  44450. return WC_TEST_RET_ENC_EC(ret);
  44451. if (!mp_iszero(rem))
  44452. return WC_TEST_RET_ENC_EC(ret);
  44453. for (i = 0; i < 100; i++) {
  44454. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 2; j++) {
  44455. ret = randNum(d, j, rng, NULL);
  44456. if (ret != MP_OKAY)
  44457. return WC_TEST_RET_ENC_EC(ret);
  44458. for (k = 1; k < (DIGIT_BIT + 7) / 8 * 2 + 1; k++) {
  44459. ret = randNum(a, k, rng, NULL);
  44460. if (ret != MP_OKAY)
  44461. return WC_TEST_RET_ENC_EC(ret);
  44462. ret = mp_div(a, d, NULL, rem);
  44463. if (ret != MP_OKAY)
  44464. return WC_TEST_RET_ENC_EC(ret);
  44465. ret = mp_div(a, d, r, NULL);
  44466. if (ret != MP_OKAY)
  44467. return WC_TEST_RET_ENC_EC(ret);
  44468. ret = mp_div(a, d, r, rem);
  44469. if (ret != MP_OKAY)
  44470. return WC_TEST_RET_ENC_EC(ret);
  44471. mp_mul(r, d, r);
  44472. mp_add(r, rem, r);
  44473. if (mp_cmp(r, a) != MP_EQ)
  44474. return WC_TEST_RET_ENC_NC;
  44475. }
  44476. }
  44477. }
  44478. ret = randNum(d, (DIGIT_BIT + 7) / 8 * 2, rng, NULL);
  44479. if (ret != MP_OKAY)
  44480. return WC_TEST_RET_ENC_EC(ret);
  44481. mp_add(d, d, a);
  44482. mp_set(rem, 1);
  44483. mp_div(a, d, NULL, rem);
  44484. if (ret != MP_OKAY)
  44485. return WC_TEST_RET_ENC_EC(ret);
  44486. if (!mp_iszero(rem))
  44487. return WC_TEST_RET_ENC_EC(ret);
  44488. mp_set(r, 1);
  44489. mp_div(a, d, r, NULL);
  44490. if (ret != MP_OKAY)
  44491. return WC_TEST_RET_ENC_EC(ret);
  44492. if (mp_cmp_d(r, 2) != MP_EQ)
  44493. return WC_TEST_RET_ENC_EC(ret);
  44494. mp_set(r, 1);
  44495. mp_set(rem, 1);
  44496. mp_div(a, d, r, rem);
  44497. if (ret != MP_OKAY)
  44498. return WC_TEST_RET_ENC_EC(ret);
  44499. if (mp_cmp_d(r, 2) != MP_EQ)
  44500. return WC_TEST_RET_ENC_EC(ret);
  44501. if (!mp_iszero(rem))
  44502. return WC_TEST_RET_ENC_EC(ret);
  44503. mp_set(a, 0xfe);
  44504. mp_lshd(a, 3);
  44505. mp_add_d(a, 0xff, a);
  44506. mp_set(d, 0xfe);
  44507. mp_lshd(d, 2);
  44508. ret = mp_div(a, d, r, rem);
  44509. if (ret != MP_OKAY)
  44510. return WC_TEST_RET_ENC_EC(ret);
  44511. mp_mul(r, d, d);
  44512. mp_add(rem, d, d);
  44513. if (mp_cmp(a, d) != MP_EQ)
  44514. return WC_TEST_RET_ENC_NC;
  44515. /* Force (hi | lo) / d to be (d | 0) / d which will would not fit in
  44516. * a digit. So mp_div must detect and handle.
  44517. * For example: 0x800000 / 0x8001, DIGIT_BIT = 8
  44518. */
  44519. mp_zero(a);
  44520. mp_set_bit(a, DIGIT_BIT * 3 - 1);
  44521. mp_zero(d);
  44522. mp_set_bit(d, DIGIT_BIT * 2 - 1);
  44523. mp_add_d(d, 1, d);
  44524. ret = mp_div(a, d, r, rem);
  44525. if (ret != MP_OKAY)
  44526. return WC_TEST_RET_ENC_EC(ret);
  44527. /* Make sure [d | d] / d is handled. */
  44528. mp_zero(a);
  44529. mp_set_bit(a, DIGIT_BIT * 2 - 1);
  44530. mp_set_bit(a, DIGIT_BIT * 1 - 1);
  44531. mp_zero(d);
  44532. mp_set_bit(d, DIGIT_BIT - 1);
  44533. ret = mp_div(a, d, r, rem);
  44534. if (ret != MP_OKAY)
  44535. return WC_TEST_RET_ENC_EC(ret);
  44536. mp_zero(a);
  44537. mp_set_bit(a, DIGIT_BIT);
  44538. mp_set_bit(a, 0);
  44539. mp_zero(d);
  44540. if (mp_cmp(r, a) != MP_EQ)
  44541. return WC_TEST_RET_ENC_NC;
  44542. if (mp_cmp(rem, d) != MP_EQ)
  44543. return WC_TEST_RET_ENC_NC;
  44544. return 0;
  44545. }
  44546. #endif
  44547. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  44548. !defined(WC_NO_RNG)
  44549. static wc_test_ret_t mp_test_prime(mp_int* a, WC_RNG* rng)
  44550. {
  44551. wc_test_ret_t ret;
  44552. int res;
  44553. ret = mp_rand_prime(a, 1, rng, NULL);
  44554. #if defined(WOLFSSL_SP_MATH_ALL)
  44555. if (ret != 0)
  44556. return WC_TEST_RET_ENC_EC(ret);
  44557. #else
  44558. if (ret != MP_VAL)
  44559. return WC_TEST_RET_ENC_NC;
  44560. #endif
  44561. #ifndef WOLFSSL_SP_MATH
  44562. ret = mp_rand_prime(a, -5, rng, NULL);
  44563. if (ret != 0 || (a->dp[0] & 3) != 3)
  44564. return WC_TEST_RET_ENC_NC;
  44565. #endif
  44566. ret = mp_prime_is_prime(a, 1, &res);
  44567. if (ret != MP_OKAY)
  44568. return WC_TEST_RET_ENC_EC(ret);
  44569. #ifndef WOLFSSL_SP_MATH
  44570. if (res != MP_YES)
  44571. return WC_TEST_RET_ENC_EC(res);
  44572. #else
  44573. if (res != MP_NO)
  44574. return WC_TEST_RET_ENC_EC(res);
  44575. #endif
  44576. ret = mp_prime_is_prime(a, 0, &res);
  44577. if (ret != MP_VAL)
  44578. return WC_TEST_RET_ENC_EC(ret);
  44579. ret = mp_prime_is_prime(a, -1, &res);
  44580. if (ret != MP_VAL)
  44581. return WC_TEST_RET_ENC_EC(ret);
  44582. ret = mp_prime_is_prime(a, 257, &res);
  44583. if (ret != MP_VAL)
  44584. return WC_TEST_RET_ENC_EC(ret);
  44585. mp_set(a, 1);
  44586. ret = mp_prime_is_prime(a, 1, &res);
  44587. if (ret != MP_OKAY)
  44588. return WC_TEST_RET_ENC_EC(ret);
  44589. if (res != MP_NO)
  44590. return WC_TEST_RET_ENC_EC(res);
  44591. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  44592. if (ret != MP_OKAY)
  44593. return WC_TEST_RET_ENC_EC(ret);
  44594. if (res != MP_NO)
  44595. return WC_TEST_RET_ENC_EC(res);
  44596. mp_set(a, 2);
  44597. ret = mp_prime_is_prime(a, 1, &res);
  44598. if (ret != MP_OKAY)
  44599. return WC_TEST_RET_ENC_EC(ret);
  44600. if (res != MP_YES)
  44601. return WC_TEST_RET_ENC_EC(res);
  44602. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  44603. if (ret != MP_OKAY)
  44604. return WC_TEST_RET_ENC_EC(ret);
  44605. if (res != MP_YES)
  44606. return WC_TEST_RET_ENC_EC(res);
  44607. mp_set(a, 0xfb);
  44608. ret = mp_prime_is_prime(a, 1, &res);
  44609. if (ret != MP_OKAY)
  44610. return WC_TEST_RET_ENC_EC(ret);
  44611. if (res != MP_YES)
  44612. return WC_TEST_RET_ENC_EC(res);
  44613. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  44614. if (ret != MP_OKAY)
  44615. return WC_TEST_RET_ENC_EC(ret);
  44616. if (res != MP_YES)
  44617. return WC_TEST_RET_ENC_EC(res);
  44618. mp_set(a, 0x6);
  44619. ret = mp_prime_is_prime(a, 1, &res);
  44620. if (ret != MP_OKAY)
  44621. return WC_TEST_RET_ENC_EC(ret);
  44622. if (res != MP_NO)
  44623. return WC_TEST_RET_ENC_EC(res);
  44624. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  44625. if (ret != MP_OKAY)
  44626. return WC_TEST_RET_ENC_EC(ret);
  44627. if (res != MP_NO)
  44628. return WC_TEST_RET_ENC_EC(res);
  44629. mp_set_int(a, 0x655 * 0x65b);
  44630. ret = mp_prime_is_prime(a, 10, &res);
  44631. if (ret != MP_OKAY)
  44632. return WC_TEST_RET_ENC_EC(ret);
  44633. if (res != MP_NO)
  44634. return WC_TEST_RET_ENC_EC(res);
  44635. ret = mp_prime_is_prime_ex(a, 10, &res, rng);
  44636. if (ret != MP_OKAY)
  44637. return WC_TEST_RET_ENC_EC(ret);
  44638. if (res != MP_NO)
  44639. return WC_TEST_RET_ENC_EC(res);
  44640. return 0;
  44641. }
  44642. #endif
  44643. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  44644. static wc_test_ret_t mp_test_lcm_gcd(mp_int* a, mp_int* b, mp_int* r, mp_int* exp,
  44645. WC_RNG* rng)
  44646. {
  44647. wc_test_ret_t ret;
  44648. int i;
  44649. WOLFSSL_SMALL_STACK_STATIC const int kat[][3] = {
  44650. { 1, 1, 1 }, { 2, 1, 2 }, { 1, 2, 2 }, { 2, 4, 4 }, { 4, 2, 4 },
  44651. { 12, 56, 168 }, { 56, 12, 168 }
  44652. };
  44653. (void)exp;
  44654. mp_set(a, 0);
  44655. mp_set(b, 1);
  44656. ret = mp_lcm(a, a, r);
  44657. if (ret != MP_VAL)
  44658. return WC_TEST_RET_ENC_EC(ret);
  44659. ret = mp_lcm(a, b, r);
  44660. if (ret != MP_VAL)
  44661. return WC_TEST_RET_ENC_EC(ret);
  44662. ret = mp_lcm(b, a, r);
  44663. if (ret != MP_VAL)
  44664. return WC_TEST_RET_ENC_EC(ret);
  44665. for (i = 0; i < (int)(sizeof(kat) / sizeof(*kat)); i++) {
  44666. mp_set(a, kat[i][0]);
  44667. mp_set(b, kat[i][1]);
  44668. ret = mp_lcm(a, b, r);
  44669. if (ret != MP_OKAY)
  44670. return WC_TEST_RET_ENC_EC(ret);
  44671. mp_set(exp, kat[i][2]);
  44672. ret = mp_cmp(r, exp);
  44673. if (ret != MP_EQ)
  44674. return WC_TEST_RET_ENC_NC;
  44675. }
  44676. (void)rng;
  44677. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  44678. !defined(WC_NO_RNG)
  44679. ret = mp_rand_prime(a, 20, rng, NULL);
  44680. if (ret != MP_OKAY)
  44681. return WC_TEST_RET_ENC_EC(ret);
  44682. ret = mp_rand_prime(b, 20, rng, NULL);
  44683. if (ret != MP_OKAY)
  44684. return WC_TEST_RET_ENC_EC(ret);
  44685. ret = mp_mul(a, b, exp);
  44686. if (ret != MP_OKAY)
  44687. return WC_TEST_RET_ENC_EC(ret);
  44688. ret = mp_lcm(a, b, r);
  44689. if (ret != MP_OKAY)
  44690. return WC_TEST_RET_ENC_EC(ret);
  44691. ret = mp_cmp(r, exp);
  44692. if (ret != MP_EQ)
  44693. return WC_TEST_RET_ENC_NC;
  44694. ret = mp_lcm(b, a, r);
  44695. if (ret != MP_OKAY)
  44696. return WC_TEST_RET_ENC_EC(ret);
  44697. ret = mp_cmp(r, exp);
  44698. if (ret != MP_EQ)
  44699. return WC_TEST_RET_ENC_NC;
  44700. #endif
  44701. mp_set(a, 11);
  44702. mp_zero(b);
  44703. ret = mp_gcd(a, b, r);
  44704. if (ret != MP_OKAY)
  44705. return WC_TEST_RET_ENC_EC(ret);
  44706. ret = mp_cmp_d(r, 11);
  44707. if (ret != MP_EQ)
  44708. return WC_TEST_RET_ENC_EC(ret);
  44709. ret = mp_gcd(b, a, r);
  44710. if (ret != MP_OKAY)
  44711. return WC_TEST_RET_ENC_EC(ret);
  44712. ret = mp_cmp_d(r, 11);
  44713. if (ret != MP_EQ)
  44714. return WC_TEST_RET_ENC_EC(ret);
  44715. ret = mp_gcd(b, b, r);
  44716. if (ret != MP_VAL)
  44717. return WC_TEST_RET_ENC_EC(ret);
  44718. return 0;
  44719. }
  44720. #endif
  44721. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  44722. defined(WOLFSSL_SP_MATH_ALL)
  44723. static wc_test_ret_t mp_test_mod_2d(mp_int* a, mp_int* r, mp_int* t, WC_RNG* rng)
  44724. {
  44725. wc_test_ret_t ret;
  44726. int i;
  44727. int j;
  44728. mp_set(a, 10);
  44729. ret = mp_mod_2d(a, 0, r);
  44730. if (ret != MP_OKAY)
  44731. return WC_TEST_RET_ENC_EC(ret);
  44732. if (!mp_iszero(r))
  44733. return WC_TEST_RET_ENC_NC;
  44734. ret = mp_mod_2d(a, 1, r);
  44735. if (ret != MP_OKAY)
  44736. return WC_TEST_RET_ENC_EC(ret);
  44737. if (!mp_iszero(r))
  44738. return WC_TEST_RET_ENC_NC;
  44739. ret = mp_mod_2d(a, 2, r);
  44740. if (ret != MP_OKAY)
  44741. return WC_TEST_RET_ENC_EC(ret);
  44742. ret = mp_cmp_d(r, 2);
  44743. if (ret != 0)
  44744. return WC_TEST_RET_ENC_EC(ret);
  44745. for (i = 2; i < 20; i++) {
  44746. ret = randNum(a, i, rng, NULL);
  44747. if (ret != 0)
  44748. return WC_TEST_RET_ENC_EC(ret);
  44749. for (j = 1; j <= mp_count_bits(a); j++) {
  44750. /* Get top part */
  44751. ret = mp_div_2d(a, j, t, NULL);
  44752. if (ret != 0)
  44753. return WC_TEST_RET_ENC_EC(ret);
  44754. ret = mp_mul_2d(t, j, t);
  44755. if (ret != 0)
  44756. return WC_TEST_RET_ENC_EC(ret);
  44757. /* Get bottom part */
  44758. ret = mp_mod_2d(a, j, r);
  44759. if (ret != 0)
  44760. return WC_TEST_RET_ENC_EC(ret);
  44761. /* Reassemble */
  44762. ret = mp_add(t, r, r);
  44763. if (ret != 0)
  44764. return WC_TEST_RET_ENC_EC(ret);
  44765. ret = mp_cmp(a, r);
  44766. if (ret != MP_EQ)
  44767. return WC_TEST_RET_ENC_NC;
  44768. }
  44769. }
  44770. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_SP_INT_NEGATIVE)
  44771. /* Test negative value being moded. */
  44772. for (j = 0; j < 20; j++) {
  44773. ret = randNum(a, 2, rng, NULL);
  44774. if (ret != 0)
  44775. return WC_TEST_RET_ENC_EC(ret);
  44776. a->sign = MP_NEG;
  44777. for (i = 1; i < DIGIT_BIT * 3 + 1; i++) {
  44778. ret = mp_mod_2d(a, i, r);
  44779. if (ret != 0)
  44780. return WC_TEST_RET_ENC_EC(ret);
  44781. mp_zero(t);
  44782. ret = mp_set_bit(t, i);
  44783. if (ret != 0)
  44784. return WC_TEST_RET_ENC_EC(ret);
  44785. ret = mp_mod(a, t, t);
  44786. if (ret != 0)
  44787. return WC_TEST_RET_ENC_EC(ret);
  44788. ret = mp_cmp(r, t);
  44789. if (ret != MP_EQ)
  44790. return WC_TEST_RET_ENC_NC;
  44791. }
  44792. }
  44793. #endif
  44794. return 0;
  44795. }
  44796. #endif
  44797. #if defined(WOLFSSL_SP_MATH_ALL) || defined(OPENSSL_EXTRA) || \
  44798. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY))
  44799. static wc_test_ret_t mp_test_mod_d(mp_int* a, WC_RNG* rng)
  44800. {
  44801. wc_test_ret_t ret;
  44802. mp_digit r;
  44803. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)
  44804. mp_digit rem;
  44805. int i;
  44806. #endif
  44807. (void)rng;
  44808. ret = mp_set(a, 1);
  44809. if (ret != MP_OKAY)
  44810. return WC_TEST_RET_ENC_EC(ret);
  44811. ret = mp_mod_d(a, 0, &r);
  44812. if (ret != MP_VAL)
  44813. return WC_TEST_RET_ENC_EC(ret);
  44814. mp_zero(a);
  44815. ret = mp_mod_d(a, 1, &r);
  44816. if (ret != MP_OKAY)
  44817. return WC_TEST_RET_ENC_EC(ret);
  44818. ret = mp_mod_d(a, 3, &r);
  44819. if (ret != MP_OKAY)
  44820. return WC_TEST_RET_ENC_EC(ret);
  44821. ret = mp_mod_d(a, 5, &r);
  44822. if (ret != MP_OKAY)
  44823. return WC_TEST_RET_ENC_EC(ret);
  44824. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)
  44825. for (i = MP_MAX_TEST_BYTE_LEN - 16; i <= MP_MAX_TEST_BYTE_LEN; i++) {
  44826. ret = randNum(a, i, rng, NULL);
  44827. if (ret != MP_OKAY)
  44828. return WC_TEST_RET_ENC_EC(ret);
  44829. ret = mp_mod_d(a, 3, &r);
  44830. if (ret != MP_OKAY)
  44831. return WC_TEST_RET_ENC_EC(ret);
  44832. ret = mp_div_d(a, 3, a, &rem);
  44833. if (ret != MP_OKAY)
  44834. return WC_TEST_RET_ENC_EC(ret);
  44835. if (r != rem)
  44836. return WC_TEST_RET_ENC_NC;
  44837. }
  44838. #endif
  44839. return 0;
  44840. }
  44841. #endif
  44842. static wc_test_ret_t mp_test_mul_sqr(mp_int* a, mp_int* b, mp_int* r1, mp_int* r2,
  44843. WC_RNG* rng)
  44844. {
  44845. wc_test_ret_t ret;
  44846. int i;
  44847. for (i = 1; i < 16; i++) {
  44848. ret = randNum(a, i, rng, NULL);
  44849. if (ret != 0)
  44850. return WC_TEST_RET_ENC_EC(ret);
  44851. ret = mp_mul(a, a, r1);
  44852. if (ret != 0)
  44853. return WC_TEST_RET_ENC_EC(ret);
  44854. ret = mp_sqr(a, r2);
  44855. if (ret != 0)
  44856. return WC_TEST_RET_ENC_EC(ret);
  44857. ret = mp_cmp(r1, r2);
  44858. if (ret != MP_EQ)
  44859. return WC_TEST_RET_ENC_NC;
  44860. }
  44861. ret = mp_set(b, 0);
  44862. if (ret != MP_OKAY)
  44863. return WC_TEST_RET_ENC_EC(ret);
  44864. ret = mp_mul(a, b, r1);
  44865. if (ret != MP_OKAY)
  44866. return WC_TEST_RET_ENC_EC(ret);
  44867. if (!mp_iszero(r1))
  44868. return WC_TEST_RET_ENC_EC(ret);
  44869. ret = mp_sqr(b, r1);
  44870. if (ret != MP_OKAY)
  44871. return WC_TEST_RET_ENC_EC(ret);
  44872. if (!mp_iszero(r1))
  44873. return WC_TEST_RET_ENC_NC;
  44874. #ifdef WOLFSSL_SP_MATH_ALL
  44875. ret = mp_set(a, 1);
  44876. if (ret != MP_OKAY)
  44877. return WC_TEST_RET_ENC_EC(ret);
  44878. i = (SP_INT_DIGITS / 2) + 1;
  44879. ret = mp_mul_2d(a, i * SP_WORD_SIZE - 1, a);
  44880. if (ret != MP_OKAY)
  44881. return WC_TEST_RET_ENC_EC(ret);
  44882. ret = mp_set(b, 1);
  44883. if (ret != MP_OKAY)
  44884. return WC_TEST_RET_ENC_EC(ret);
  44885. ret = mp_mul_2d(b, (SP_INT_DIGITS - 1 - i) * SP_WORD_SIZE - 1, b);
  44886. if (ret != MP_OKAY)
  44887. return WC_TEST_RET_ENC_EC(ret);
  44888. ret = mp_mul(a, b, r1);
  44889. if (ret != MP_OKAY)
  44890. return WC_TEST_RET_ENC_EC(ret);
  44891. ret = mp_mul(a, a, r1);
  44892. if (ret == MP_OKAY)
  44893. return WC_TEST_RET_ENC_NC;
  44894. ret = mp_sqr(a, r1);
  44895. if (ret == MP_OKAY)
  44896. return WC_TEST_RET_ENC_NC;
  44897. ret = mp_sqr(b, r1);
  44898. if (ret != MP_OKAY)
  44899. return WC_TEST_RET_ENC_EC(ret);
  44900. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  44901. (defined(HAVE_ECC) && defined(FP_ECC))
  44902. ret = mp_mulmod(a, b, b, r1);
  44903. if (ret != MP_OKAY)
  44904. return WC_TEST_RET_ENC_EC(ret);
  44905. ret = mp_mulmod(a, a, b, r1);
  44906. if (ret == MP_OKAY)
  44907. return WC_TEST_RET_ENC_NC;
  44908. #if defined(HAVE_ECC) && (defined(ECC_SHAMIR) || defined(FP_ECC))
  44909. ret = mp_sqrmod(a, b, r1);
  44910. if (ret == MP_OKAY)
  44911. return WC_TEST_RET_ENC_NC;
  44912. ret = mp_sqrmod(b, a, r1);
  44913. if (ret != MP_OKAY)
  44914. return WC_TEST_RET_ENC_EC(ret);
  44915. #endif /* HAVE_ECC && (ECC_SHAMIR || FP_ECC) */
  44916. #endif /* WOLFSSL_SP_MATH_ALL || WOLFSSL_HAVE_SP_DH || (HAVE_ECC && FP_ECC) */
  44917. #endif /* WOLFSSL_SP_MATH_ALL */
  44918. return 0;
  44919. }
  44920. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  44921. defined(OPENSSL_EXTRA)
  44922. static wc_test_ret_t mp_test_invmod(mp_int* a, mp_int* m, mp_int* r)
  44923. {
  44924. wc_test_ret_t ret;
  44925. mp_set(a, 0);
  44926. mp_set(m, 1);
  44927. ret = mp_invmod(a, m, r);
  44928. if (ret != MP_VAL)
  44929. return WC_TEST_RET_ENC_EC(ret);
  44930. ret = mp_invmod(m, a, r);
  44931. if (ret != MP_VAL)
  44932. return WC_TEST_RET_ENC_EC(ret);
  44933. mp_set(a, 2);
  44934. mp_set(m, 4);
  44935. ret = mp_invmod(a, m, r);
  44936. if (ret != MP_VAL)
  44937. return WC_TEST_RET_ENC_EC(ret);
  44938. mp_set(a, 3);
  44939. mp_set(m, 6);
  44940. ret = mp_invmod(a, m, r);
  44941. if (ret != MP_VAL)
  44942. return WC_TEST_RET_ENC_EC(ret);
  44943. mp_set(a, 5*9);
  44944. mp_set(m, 6*9);
  44945. ret = mp_invmod(a, m, r);
  44946. if (ret != MP_VAL)
  44947. return WC_TEST_RET_ENC_EC(ret);
  44948. mp_set(a, 1);
  44949. mp_set(m, 4);
  44950. ret = mp_invmod(a, m, r);
  44951. if (ret != MP_OKAY)
  44952. return WC_TEST_RET_ENC_EC(ret);
  44953. if (!mp_isone(r))
  44954. return WC_TEST_RET_ENC_NC;
  44955. mp_set(a, 3);
  44956. mp_set(m, 4);
  44957. ret = mp_invmod(a, m, r);
  44958. if (ret != MP_OKAY)
  44959. return WC_TEST_RET_ENC_EC(ret);
  44960. ret = mp_cmp_d(r, 3);
  44961. if (ret != 0)
  44962. return WC_TEST_RET_ENC_EC(ret);
  44963. mp_set(a, 3);
  44964. mp_set(m, 5);
  44965. ret = mp_invmod(a, m, r);
  44966. if (ret != MP_OKAY)
  44967. return WC_TEST_RET_ENC_EC(ret);
  44968. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  44969. /* Maximum 'a' */
  44970. mp_set(a, 0);
  44971. mp_set_bit(a, (r->size / 2)* SP_WORD_SIZE - 1);
  44972. mp_sub_d(a, 1, a);
  44973. /* Modulus too big. */
  44974. mp_set(m, 0);
  44975. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE);
  44976. ret = mp_invmod(a, m, r);
  44977. if (ret != MP_VAL)
  44978. return WC_TEST_RET_ENC_EC(ret);
  44979. /* Maximum modulus - even. */
  44980. mp_set(m, 0);
  44981. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE - 1);
  44982. ret = mp_invmod(a, m, r);
  44983. if (ret != MP_OKAY)
  44984. return WC_TEST_RET_ENC_EC(ret);
  44985. #endif
  44986. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_INT_NEGATIVE)
  44987. mp_read_radix(a, "-3", 16);
  44988. ret = mp_invmod(a, m, r);
  44989. if (ret != MP_OKAY)
  44990. return WC_TEST_RET_ENC_EC(ret);
  44991. #endif
  44992. #if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC)
  44993. mp_set(a, 0);
  44994. mp_set(m, 3);
  44995. ret = mp_invmod_mont_ct(a, m, r, 1);
  44996. if (ret != MP_VAL)
  44997. return WC_TEST_RET_ENC_EC(ret);
  44998. mp_set(a, 1);
  44999. mp_set(m, 0);
  45000. ret = mp_invmod_mont_ct(a, m, r, 1);
  45001. if (ret != MP_VAL)
  45002. return WC_TEST_RET_ENC_EC(ret);
  45003. mp_set(a, 1);
  45004. mp_set(m, 1);
  45005. ret = mp_invmod_mont_ct(a, m, r, 1);
  45006. if (ret != MP_VAL)
  45007. return WC_TEST_RET_ENC_EC(ret);
  45008. mp_set(a, 1);
  45009. mp_set(m, 2);
  45010. ret = mp_invmod_mont_ct(a, m, r, 1);
  45011. if (ret != MP_VAL)
  45012. return WC_TEST_RET_ENC_EC(ret);
  45013. mp_set(a, 1);
  45014. mp_set(m, 3);
  45015. ret = mp_invmod_mont_ct(a, m, r, 1);
  45016. if (ret != MP_OKAY)
  45017. return WC_TEST_RET_ENC_EC(ret);
  45018. #endif
  45019. return 0;
  45020. }
  45021. #endif /* !NO_RSA || HAVE_ECC || !NO_DSA || OPENSSL_EXTRA */
  45022. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  45023. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  45024. static wc_test_ret_t mp_test_exptmod(mp_int* b, mp_int* e, mp_int* m, mp_int* r)
  45025. {
  45026. wc_test_ret_t ret;
  45027. mp_set(b, 0x2);
  45028. mp_set(e, 0x3);
  45029. mp_set(m, 0x0);
  45030. ret = mp_exptmod_ex(b, e, 1, m, r);
  45031. if (ret != MP_VAL)
  45032. return WC_TEST_RET_ENC_EC(ret);
  45033. ret = mp_exptmod_nct(b, e, m, r);
  45034. if (ret != MP_VAL)
  45035. return WC_TEST_RET_ENC_EC(ret);
  45036. mp_set(b, 0x2);
  45037. mp_set(e, 0x3);
  45038. mp_set(m, 0x1);
  45039. ret = mp_exptmod_ex(b, e, 1, m, r);
  45040. if (ret != MP_OKAY)
  45041. return WC_TEST_RET_ENC_EC(ret);
  45042. if (!mp_iszero(r))
  45043. return WC_TEST_RET_ENC_NC;
  45044. ret = mp_exptmod_nct(b, e, m, r);
  45045. if (ret != MP_OKAY)
  45046. return WC_TEST_RET_ENC_EC(ret);
  45047. if (!mp_iszero(r))
  45048. return WC_TEST_RET_ENC_NC;
  45049. mp_set(b, 0x2);
  45050. mp_set(e, 0x0);
  45051. mp_set(m, 0x7);
  45052. ret = mp_exptmod_ex(b, e, 1, m, r);
  45053. if (ret != MP_OKAY)
  45054. return WC_TEST_RET_ENC_EC(ret);
  45055. if (!mp_isone(r))
  45056. return WC_TEST_RET_ENC_NC;
  45057. ret = mp_exptmod_nct(b, e, m, r);
  45058. if (ret != MP_OKAY)
  45059. return WC_TEST_RET_ENC_EC(ret);
  45060. if (!mp_isone(r))
  45061. return WC_TEST_RET_ENC_NC;
  45062. mp_set(b, 0x0);
  45063. mp_set(e, 0x3);
  45064. mp_set(m, 0x7);
  45065. ret = mp_exptmod_ex(b, e, 1, m, r);
  45066. if (ret != MP_OKAY)
  45067. return WC_TEST_RET_ENC_EC(ret);
  45068. if (!mp_iszero(r))
  45069. return WC_TEST_RET_ENC_NC;
  45070. ret = mp_exptmod_nct(b, e, m, r);
  45071. if (ret != MP_OKAY)
  45072. return WC_TEST_RET_ENC_EC(ret);
  45073. if (!mp_iszero(r))
  45074. return WC_TEST_RET_ENC_NC;
  45075. mp_set(b, 0x10);
  45076. mp_set(e, 0x3);
  45077. mp_set(m, 0x7);
  45078. ret = mp_exptmod_ex(b, e, 1, m, r);
  45079. if (ret != MP_OKAY)
  45080. return WC_TEST_RET_ENC_EC(ret);
  45081. ret = mp_exptmod_nct(b, e, m, r);
  45082. if (ret != MP_OKAY)
  45083. return WC_TEST_RET_ENC_EC(ret);
  45084. mp_set(b, 0x7);
  45085. mp_set(e, 0x3);
  45086. mp_set(m, 0x7);
  45087. ret = mp_exptmod_ex(b, e, 1, m, r);
  45088. if (ret != MP_OKAY)
  45089. return WC_TEST_RET_ENC_EC(ret);
  45090. if (!mp_iszero(r))
  45091. return WC_TEST_RET_ENC_NC;
  45092. ret = mp_exptmod_nct(b, e, m, r);
  45093. if (ret != MP_OKAY)
  45094. return WC_TEST_RET_ENC_EC(ret);
  45095. if (!mp_iszero(r))
  45096. return WC_TEST_RET_ENC_NC;
  45097. #ifndef WOLFSSL_SP_MATH
  45098. mp_set(b, 0x01);
  45099. mp_mul_2d(b, DIGIT_BIT, b);
  45100. mp_add_d(b, 1, b);
  45101. mp_set(e, 0x3);
  45102. mp_copy(b, m);
  45103. ret = mp_exptmod_ex(b, e, 1, m, r);
  45104. if (ret != MP_OKAY)
  45105. return WC_TEST_RET_ENC_EC(ret);
  45106. if (!mp_iszero(r))
  45107. return WC_TEST_RET_ENC_NC;
  45108. ret = mp_exptmod_nct(b, e, m, r);
  45109. if (ret != MP_OKAY)
  45110. return WC_TEST_RET_ENC_EC(ret);
  45111. if (!mp_iszero(r))
  45112. return WC_TEST_RET_ENC_NC;
  45113. #endif
  45114. mp_set(b, 0x2);
  45115. mp_set(e, 0x3);
  45116. mp_set(m, 0x7);
  45117. ret = mp_exptmod_ex(b, e, 1, m, r);
  45118. if (ret != MP_OKAY)
  45119. return WC_TEST_RET_ENC_EC(ret);
  45120. ret = mp_exptmod_nct(b, e, m, r);
  45121. if (ret != MP_OKAY)
  45122. return WC_TEST_RET_ENC_EC(ret);
  45123. #ifdef WOLFSSL_SP_MATH_ALL
  45124. mp_set(b, 0x2);
  45125. mp_set(e, 0x3);
  45126. mp_set(m, 0x01);
  45127. mp_mul_2d(m, SP_WORD_SIZE * SP_INT_DIGITS / 2, m);
  45128. mp_add_d(m, 0x01, m);
  45129. ret = mp_exptmod_ex(b, e, 1, m, r);
  45130. if (ret != MP_VAL)
  45131. return WC_TEST_RET_ENC_EC(ret);
  45132. ret = mp_exptmod_nct(b, e, m, r);
  45133. if (ret != MP_VAL)
  45134. return WC_TEST_RET_ENC_EC(ret);
  45135. #endif
  45136. return 0;
  45137. }
  45138. #endif /* !NO_RSA || !NO_DSA || !NO_DH || (HAVE_ECC && HAVE_COMP_KEY) ||
  45139. * OPENSSL_EXTRA */
  45140. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  45141. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  45142. static wc_test_ret_t mp_test_mont(mp_int* a, mp_int* m, mp_int* n, mp_int* r, WC_RNG* rng)
  45143. {
  45144. wc_test_ret_t ret;
  45145. mp_digit mp;
  45146. static int exp[] = { 7, 8, 16, 27, 32, 64,
  45147. 127, 128, 255, 256,
  45148. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  45149. 383, 384, 2033, 2048
  45150. #endif
  45151. };
  45152. static mp_digit sub[] = { 0x01, 0x05, 0x0f, 0x27, 0x05, 0x3b,
  45153. 0x01, 0x9f, 0x13, 0xbd,
  45154. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  45155. 0x1f, 0x13d, 0x45, 0x615
  45156. #endif
  45157. };
  45158. int bits[] = { 256, 384,
  45159. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 4096
  45160. 2048,
  45161. #endif
  45162. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 6144
  45163. 3072
  45164. #endif
  45165. };
  45166. int i;
  45167. int j;
  45168. for (i = 0; i < (int)(sizeof(exp) / sizeof(*exp)); i++) {
  45169. if (exp[i] >= DIGIT_BIT)
  45170. continue;
  45171. mp_zero(m);
  45172. ret = mp_set_bit(m, exp[i]);
  45173. if (ret != MP_OKAY)
  45174. return WC_TEST_RET_ENC_EC(ret);
  45175. ret = mp_sub_d(m, sub[i], m);
  45176. if (ret != MP_OKAY)
  45177. return WC_TEST_RET_ENC_EC(ret);
  45178. ret = mp_montgomery_setup(m, &mp);
  45179. if (ret != MP_OKAY)
  45180. return WC_TEST_RET_ENC_EC(ret);
  45181. ret = mp_montgomery_calc_normalization(n, m);
  45182. if (ret != MP_OKAY)
  45183. return WC_TEST_RET_ENC_EC(ret);
  45184. for (j = 0; j < 10; j++) {
  45185. ret = randNum(a, (exp[i] + DIGIT_BIT - 1) / DIGIT_BIT, rng, NULL);
  45186. if (ret != 0)
  45187. return WC_TEST_RET_ENC_EC(ret);
  45188. ret = mp_mod(a, m, a);
  45189. if (ret != 0)
  45190. return WC_TEST_RET_ENC_EC(ret);
  45191. /* r = a * a */
  45192. ret = mp_sqrmod(a, m, r);
  45193. if (ret != MP_OKAY)
  45194. return WC_TEST_RET_ENC_EC(ret);
  45195. /* Convert to Montgomery form = a*n */
  45196. ret = mp_mulmod(a, n, m, a);
  45197. if (ret != MP_OKAY)
  45198. return WC_TEST_RET_ENC_EC(ret);
  45199. /* a*a mod m == ((a*n) * (a*n)) / n / n */
  45200. ret = mp_sqr(a, a);
  45201. if (ret != MP_OKAY)
  45202. return WC_TEST_RET_ENC_EC(ret);
  45203. ret = mp_montgomery_reduce(a, m, mp);
  45204. if (ret != MP_OKAY)
  45205. return WC_TEST_RET_ENC_EC(ret);
  45206. ret = mp_montgomery_reduce(a, m, mp);
  45207. if (ret != MP_OKAY)
  45208. return WC_TEST_RET_ENC_EC(ret);
  45209. if (mp_cmp(a, r) != MP_EQ)
  45210. return WC_TEST_RET_ENC_NC;
  45211. }
  45212. }
  45213. /* Force carries. */
  45214. for (i = 0; i < (int)(sizeof(bits) / sizeof(*bits)); i++) {
  45215. /* a = 2^(bits*2) - 1 */
  45216. mp_zero(a);
  45217. mp_set_bit(a, bits[i] * 2);
  45218. mp_sub_d(a, 1, a);
  45219. /* m = 2^(bits) - 1 */
  45220. mp_zero(m);
  45221. mp_set_bit(m, bits[i]);
  45222. mp_sub_d(m, 1, m);
  45223. mp = 1;
  45224. /* result = r = 2^(bits) - 1 */
  45225. mp_zero(r);
  45226. mp_set_bit(r, bits[i]);
  45227. mp_sub_d(r, 1, r);
  45228. ret = mp_montgomery_reduce(a, m, mp);
  45229. if (ret != MP_OKAY)
  45230. return WC_TEST_RET_ENC_EC(ret);
  45231. /* Result is m or 0 if reduced to range of modulus. */
  45232. if (mp_cmp(a, r) != MP_EQ && mp_iszero(a) != MP_YES)
  45233. return WC_TEST_RET_ENC_NC;
  45234. }
  45235. return 0;
  45236. }
  45237. #endif
  45238. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mp_test(void)
  45239. {
  45240. WC_RNG rng;
  45241. int rng_inited = 0;
  45242. wc_test_ret_t ret;
  45243. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  45244. int i, j;
  45245. #ifndef WOLFSSL_SP_MATH
  45246. int k;
  45247. #endif
  45248. mp_digit d = 0;
  45249. #endif
  45250. #ifdef WOLFSSL_SMALL_STACK
  45251. mp_int *a = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  45252. DYNAMIC_TYPE_TMP_BUFFER),
  45253. *b = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  45254. DYNAMIC_TYPE_TMP_BUFFER),
  45255. *r1 = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  45256. DYNAMIC_TYPE_TMP_BUFFER),
  45257. *r2 = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  45258. DYNAMIC_TYPE_TMP_BUFFER),
  45259. *p = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  45260. DYNAMIC_TYPE_TMP_BUFFER);
  45261. if ((a == NULL) ||
  45262. (b == NULL) ||
  45263. (r1 == NULL) ||
  45264. (r2 == NULL) ||
  45265. (p == NULL))
  45266. {
  45267. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  45268. }
  45269. #else
  45270. mp_int a[1], b[1], r1[1], r2[1], p[1];
  45271. #endif
  45272. WOLFSSL_ENTER("mp_test");
  45273. ret = mp_init_multi(a, b, r1, r2, NULL, NULL);
  45274. if (ret != 0)
  45275. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45276. #ifdef WOLFSSL_SP_MATH_ALL
  45277. mp_init_copy(p, a);
  45278. #else
  45279. ret = mp_init(p);
  45280. if (ret != 0)
  45281. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45282. #endif
  45283. #ifndef HAVE_FIPS
  45284. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  45285. #else
  45286. ret = wc_InitRng(&rng);
  45287. #endif
  45288. if (ret != 0)
  45289. goto done;
  45290. rng_inited = 1;
  45291. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  45292. mp_set_int(a, 0);
  45293. if (a->used != 0 || a->dp[0] != 0)
  45294. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  45295. for (j = 1; j <= MP_MAX_TEST_BYTE_LEN; j++) {
  45296. for (i = 0; i < 4 * j; i++) {
  45297. /* New values to use. */
  45298. ret = randNum(p, j, &rng, NULL);
  45299. if (ret != 0)
  45300. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45301. ret = randNum(a, j, &rng, NULL);
  45302. if (ret != 0)
  45303. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45304. ret = randNum(b, j, &rng, NULL);
  45305. if (ret != 0)
  45306. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45307. ret = wc_RNG_GenerateBlock(&rng, (byte*)&d, sizeof(d));
  45308. if (ret != 0)
  45309. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45310. d &= MP_MASK;
  45311. #if !defined(WOLFSSL_SP_MATH) || (defined(HAVE_ECC) && \
  45312. (defined(ECC_SHAMIR) || defined(FP_ECC)))
  45313. /* Ensure sqrmod produce same result as mulmod. */
  45314. ret = mp_sqrmod(a, p, r1);
  45315. if (ret != 0)
  45316. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45317. ret = mp_mulmod(a, a, p, r2);
  45318. if (ret != 0)
  45319. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45320. if (mp_cmp(r1, r2) != 0) {
  45321. WOLFSSL_MSG("Fail: mp_mulmod result does not match mp_sqrmod!");
  45322. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  45323. }
  45324. #endif
  45325. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  45326. #if defined(WOLFSSL_SP_MATH) || (defined(WOLFSSL_SP_MATH_ALL) && \
  45327. !defined(WOLFSSL_SP_INT_NEGATIVE))
  45328. ret = mp_addmod(a, b, p, r1);
  45329. if (ret != 0)
  45330. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45331. ret = mp_submod(r1, b, p, r2);
  45332. if (ret != 0)
  45333. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45334. ret = mp_mod(a, p, r1);
  45335. if (ret != 0)
  45336. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45337. if (mp_cmp(r1, r2) != MP_EQ)
  45338. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  45339. #else
  45340. /* Ensure add with mod produce same result as sub with mod. */
  45341. ret = mp_addmod(a, b, p, r1);
  45342. if (ret != 0)
  45343. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45344. b->sign ^= 1;
  45345. ret = mp_submod(a, b, p, r2);
  45346. if (ret != 0)
  45347. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45348. if (mp_cmp(r1, r2) != 0)
  45349. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  45350. #endif
  45351. #endif
  45352. /* Ensure add digit produce same result as sub digit. */
  45353. ret = mp_add_d(a, d, r1);
  45354. if (ret != 0)
  45355. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45356. ret = mp_sub_d(r1, d, r2);
  45357. if (ret != 0)
  45358. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45359. if (mp_cmp(a, r2) != 0)
  45360. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  45361. /* Invert - if p is even it will use the slow impl.
  45362. * - if p and a are even it will fail.
  45363. */
  45364. ret = mp_invmod(a, p, r1);
  45365. if (ret != 0 && ret != MP_VAL)
  45366. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45367. #ifndef WOLFSSL_SP_MATH
  45368. /* Shift up and down number all bits in a digit. */
  45369. for (k = 0; k < DIGIT_BIT; k++) {
  45370. mp_mul_2d(a, k, r1);
  45371. mp_div_2d(r1, k, r2, p);
  45372. if (mp_cmp(a, r2) != 0)
  45373. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  45374. if (!mp_iszero(p))
  45375. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  45376. mp_rshb(r1, k);
  45377. if (mp_cmp(a, r1) != 0)
  45378. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  45379. }
  45380. #endif
  45381. }
  45382. }
  45383. /* Test adding and subtracting zero from zero. */
  45384. mp_zero(a);
  45385. ret = mp_add_d(a, 0, r1);
  45386. if (ret != 0)
  45387. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45388. if (!mp_iszero(r1)) {
  45389. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  45390. }
  45391. ret = mp_sub_d(a, 0, r2);
  45392. if (ret != 0)
  45393. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  45394. if (!mp_iszero(r2)) {
  45395. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  45396. }
  45397. #if DIGIT_BIT >= 32
  45398. /* Check that setting a 32-bit digit works. */
  45399. d &= 0xffffffffU;
  45400. mp_set_int(a, d);
  45401. if (a->used != 1 || a->dp[0] != d)
  45402. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  45403. #endif
  45404. /* Check setting a bit and testing a bit works. */
  45405. for (i = 0; i < MP_MAX_TEST_BYTE_LEN * 8; i++) {
  45406. mp_zero(a);
  45407. mp_set_bit(a, i);
  45408. if (!mp_is_bit_set(a, i))
  45409. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  45410. }
  45411. #endif
  45412. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  45413. mp_zero(a);
  45414. i = mp_cnt_lsb(a);
  45415. if (i != 0)
  45416. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  45417. mp_set(a, 1);
  45418. i = mp_cnt_lsb(a);
  45419. if (i != 0)
  45420. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  45421. mp_set(a, 32);
  45422. i = mp_cnt_lsb(a);
  45423. if (i != 5)
  45424. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  45425. mp_zero(a);
  45426. mp_set_bit(a, 129);
  45427. i = mp_cnt_lsb(a);
  45428. if (i != 129)
  45429. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  45430. #endif
  45431. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  45432. if ((ret = mp_test_param(a, b, r1, &rng)) != 0)
  45433. goto done;
  45434. #endif
  45435. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  45436. if ((ret = mp_test_div_3(a, r1, &rng)) != 0)
  45437. goto done;
  45438. #endif
  45439. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  45440. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  45441. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  45442. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  45443. if ((ret = mp_test_radix_10(a, r1, &rng)) != 0)
  45444. goto done;
  45445. #endif
  45446. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  45447. defined(HAVE_ECC))
  45448. if ((ret = mp_test_radix_16(a, r1, &rng)) != 0)
  45449. goto done;
  45450. #endif
  45451. if ((ret = mp_test_shift(a, r1, &rng)) != 0)
  45452. goto done;
  45453. if ((ret = mp_test_add_sub_d(a, r1)) != 0)
  45454. goto done;
  45455. if ((ret = mp_test_read_to_bin(a)) != 0)
  45456. goto done;
  45457. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  45458. if ((ret = mp_test_set_int(a)) != 0)
  45459. goto done;
  45460. #endif
  45461. if ((ret = mp_test_cmp(a, r1)) != 0)
  45462. goto done;
  45463. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  45464. if ((ret = mp_test_shbd(a, b, &rng)) != 0)
  45465. goto done;
  45466. #endif
  45467. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  45468. if ((ret = mp_test_set_is_bit(a)) != 0)
  45469. goto done;
  45470. #endif
  45471. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  45472. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && \
  45473. !defined(WOLFSSL_RSA_PUBLIC_ONLY))
  45474. if ((ret = mp_test_div(a, b, r1, r2, &rng)) != 0)
  45475. goto done;
  45476. #endif
  45477. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  45478. !defined(WC_NO_RNG)
  45479. if ((ret = mp_test_prime(a, &rng)) != 0)
  45480. goto done;
  45481. #endif
  45482. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  45483. if ((ret = mp_test_lcm_gcd(a, b, r1, r2, &rng)) != 0)
  45484. goto done;
  45485. #endif
  45486. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  45487. defined(WOLFSSL_SP_MATH_ALL)
  45488. if ((ret = mp_test_mod_2d(a, r1, p, &rng)) != 0)
  45489. goto done;
  45490. #endif
  45491. #if defined(WOLFSSL_SP_MATH_ALL) || defined(OPENSSL_EXTRA) || \
  45492. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY))
  45493. if ((ret = mp_test_mod_d(a, &rng)) != 0)
  45494. goto done;
  45495. #endif
  45496. if ((ret = mp_test_mul_sqr(a, b, r1, r2, &rng)) != 0)
  45497. goto done;
  45498. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  45499. defined(OPENSSL_EXTRA)
  45500. if ((ret = mp_test_invmod(a, b, r1)) != 0)
  45501. goto done;
  45502. #endif
  45503. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  45504. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  45505. if ((ret = mp_test_exptmod(a, b, r1, r2)) != 0)
  45506. goto done;
  45507. #endif
  45508. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  45509. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  45510. if ((ret = mp_test_mont(a, b, r1, r2, &rng)) != 0)
  45511. goto done;
  45512. #endif
  45513. done:
  45514. #ifdef WOLFSSL_SMALL_STACK
  45515. if (p) {
  45516. mp_clear(p);
  45517. XFREE(p, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45518. }
  45519. if (r2) {
  45520. mp_clear(r2);
  45521. XFREE(r2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45522. }
  45523. if (r1) {
  45524. mp_clear(r1);
  45525. XFREE(r1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45526. }
  45527. if (b) {
  45528. mp_clear(b);
  45529. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45530. }
  45531. if (a) {
  45532. mp_clear(a);
  45533. XFREE(a, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45534. }
  45535. #else
  45536. mp_clear(p);
  45537. mp_clear(r2);
  45538. mp_clear(r1);
  45539. mp_clear(b);
  45540. mp_clear(a);
  45541. #endif
  45542. if (rng_inited)
  45543. wc_FreeRng(&rng);
  45544. return ret;
  45545. }
  45546. #endif /* WOLFSSL_PUBLIC_MP && ((WOLFSSL_SP_MATH_ALL &&
  45547. * !WOLFSSL_RSA_VERIFY_ONLY) || USE_FAST_MATH) */
  45548. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  45549. typedef struct pairs_t {
  45550. const unsigned char* coeff;
  45551. int coeffSz;
  45552. int exp;
  45553. } pairs_t;
  45554. /*
  45555. n =p1p2p3, where pi = ki(p1-1)+1 with (k2,k3) = (173,293)
  45556. p1 = 2^192 * 0x000000000000e24fd4f6d6363200bf2323ec46285cac1d3a
  45557. + 2^0 * 0x0b2488b0c29d96c5e67f8bec15b54b189ae5636efe89b45b
  45558. */
  45559. static const unsigned char c192a[] =
  45560. {
  45561. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x4f,
  45562. 0xd4, 0xf6, 0xd6, 0x36, 0x32, 0x00, 0xbf, 0x23,
  45563. 0x23, 0xec, 0x46, 0x28, 0x5c, 0xac, 0x1d, 0x3a
  45564. };
  45565. static const unsigned char c0a[] =
  45566. {
  45567. 0x0b, 0x24, 0x88, 0xb0, 0xc2, 0x9d, 0x96, 0xc5,
  45568. 0xe6, 0x7f, 0x8b, 0xec, 0x15, 0xb5, 0x4b, 0x18,
  45569. 0x9a, 0xe5, 0x63, 0x6e, 0xfe, 0x89, 0xb4, 0x5b
  45570. };
  45571. static const pairs_t ecPairsA[] =
  45572. {
  45573. {c192a, sizeof(c192a), 192},
  45574. {c0a, sizeof(c0a), 0}
  45575. };
  45576. static const int kA[] = {173, 293};
  45577. static const unsigned char controlPrime[] = {
  45578. 0xe1, 0x76, 0x45, 0x80, 0x59, 0xb6, 0xd3, 0x49,
  45579. 0xdf, 0x0a, 0xef, 0x12, 0xd6, 0x0f, 0xf0, 0xb7,
  45580. 0xcb, 0x2a, 0x37, 0xbf, 0xa7, 0xf8, 0xb5, 0x4d,
  45581. 0xf5, 0x31, 0x35, 0xad, 0xe4, 0xa3, 0x94, 0xa1,
  45582. 0xdb, 0xf1, 0x96, 0xad, 0xb5, 0x05, 0x64, 0x85,
  45583. 0x83, 0xfc, 0x1b, 0x5b, 0x29, 0xaa, 0xbe, 0xf8,
  45584. 0x26, 0x3f, 0x76, 0x7e, 0xad, 0x1c, 0xf0, 0xcb,
  45585. 0xd7, 0x26, 0xb4, 0x1b, 0x05, 0x8e, 0x56, 0x86,
  45586. 0x7e, 0x08, 0x62, 0x21, 0xc1, 0x86, 0xd6, 0x47,
  45587. 0x79, 0x3e, 0xb7, 0x5d, 0xa4, 0xc6, 0x3a, 0xd7,
  45588. 0xb1, 0x74, 0x20, 0xf6, 0x50, 0x97, 0x41, 0x04,
  45589. 0x53, 0xed, 0x3f, 0x26, 0xd6, 0x6f, 0x91, 0xfa,
  45590. 0x68, 0x26, 0xec, 0x2a, 0xdc, 0x9a, 0xf1, 0xe7,
  45591. 0xdc, 0xfb, 0x73, 0xf0, 0x79, 0x43, 0x1b, 0x21,
  45592. 0xa3, 0x59, 0x04, 0x63, 0x52, 0x07, 0xc9, 0xd7,
  45593. 0xe6, 0xd1, 0x1b, 0x5d, 0x5e, 0x96, 0xfa, 0x53
  45594. };
  45595. static const unsigned char testOne[] = { 1 };
  45596. static wc_test_ret_t GenerateNextP(mp_int* p1, mp_int* p2, int k)
  45597. {
  45598. wc_test_ret_t ret;
  45599. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  45600. mp_int *ki = (mp_int *)XMALLOC(sizeof(*ki), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45601. if (ki == NULL)
  45602. return MEMORY_E;
  45603. #else
  45604. mp_int ki[1];
  45605. #endif
  45606. ret = mp_init(ki);
  45607. if (ret != 0)
  45608. ret = WC_TEST_RET_ENC_EC(ret);
  45609. if (ret == 0) {
  45610. ret = mp_set(ki, (mp_digit)k);
  45611. if (ret != 0)
  45612. ret = WC_TEST_RET_ENC_EC(ret);
  45613. }
  45614. if (ret == 0) {
  45615. ret = mp_sub_d(p1, 1, p2);
  45616. if (ret != 0)
  45617. ret = WC_TEST_RET_ENC_EC(ret);
  45618. }
  45619. if (ret == 0) {
  45620. ret = mp_mul(p2, ki, p2);
  45621. if (ret != 0)
  45622. ret = WC_TEST_RET_ENC_EC(ret);
  45623. }
  45624. if (ret == 0) {
  45625. ret = mp_add_d(p2, 1, p2);
  45626. if (ret != 0)
  45627. ret = WC_TEST_RET_ENC_EC(ret);
  45628. }
  45629. mp_clear(ki);
  45630. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  45631. XFREE(ki, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45632. #endif
  45633. return ret;
  45634. }
  45635. static wc_test_ret_t GenerateP(mp_int* p1, mp_int* p2, mp_int* p3,
  45636. const pairs_t* ecPairs, int ecPairsSz,
  45637. const int* k)
  45638. {
  45639. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  45640. mp_int *x = NULL, *y = NULL;
  45641. #else
  45642. mp_int x[1], y[1];
  45643. #endif
  45644. wc_test_ret_t ret;
  45645. int i;
  45646. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  45647. if (((x = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) ||
  45648. ((y = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) {
  45649. ret = MEMORY_E;
  45650. goto out;
  45651. }
  45652. #endif
  45653. ret = mp_init_multi(x, y, NULL, NULL, NULL, NULL);
  45654. if (ret != 0) {
  45655. ret = WC_TEST_RET_ENC_EC(ret);
  45656. goto out;
  45657. }
  45658. for (i = 0; ret == 0 && i < ecPairsSz; i++) {
  45659. ret = mp_read_unsigned_bin(x, ecPairs[i].coeff, (word32)ecPairs[i].coeffSz);
  45660. if (ret != 0) {
  45661. ret = WC_TEST_RET_ENC_EC(ret);
  45662. break;
  45663. }
  45664. /* p1 = 2^exp */
  45665. ret = mp_2expt(y, ecPairs[i].exp);
  45666. if (ret != 0) {
  45667. ret = WC_TEST_RET_ENC_EC(ret);
  45668. break;
  45669. }
  45670. /* p1 = p1 * m */
  45671. ret = mp_mul(x, y, x);
  45672. if (ret != 0) {
  45673. ret = WC_TEST_RET_ENC_EC(ret);
  45674. break;
  45675. }
  45676. /* p1 += */
  45677. ret = mp_add(p1, x, p1);
  45678. if (ret != 0) {
  45679. ret = WC_TEST_RET_ENC_EC(ret);
  45680. break;
  45681. }
  45682. mp_zero(x);
  45683. mp_zero(y);
  45684. }
  45685. if (ret == 0)
  45686. ret = GenerateNextP(p1, p2, k[0]);
  45687. if (ret == 0)
  45688. ret = GenerateNextP(p1, p3, k[1]);
  45689. out:
  45690. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  45691. if (x != NULL) {
  45692. mp_clear(x);
  45693. XFREE(x, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45694. }
  45695. if (y != NULL) {
  45696. mp_clear(y);
  45697. XFREE(y, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45698. }
  45699. #else
  45700. mp_clear(x);
  45701. mp_clear(y);
  45702. #endif
  45703. return ret;
  45704. }
  45705. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t prime_test(void)
  45706. {
  45707. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  45708. mp_int *n = (mp_int *)XMALLOC(sizeof *n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  45709. *p1 = (mp_int *)XMALLOC(sizeof *p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  45710. *p2 = (mp_int *)XMALLOC(sizeof *p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  45711. *p3 = (mp_int *)XMALLOC(sizeof *p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45712. #else
  45713. mp_int n[1],
  45714. p1[1],
  45715. p2[1],
  45716. p3[1];
  45717. #endif
  45718. wc_test_ret_t ret;
  45719. int isPrime = 0;
  45720. WC_RNG rng;
  45721. WOLFSSL_ENTER("prime_test");
  45722. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  45723. if ((n == NULL) ||
  45724. (p1 == NULL) ||
  45725. (p2 == NULL) ||
  45726. (p3 == NULL))
  45727. ERROR_OUT(MEMORY_E, out);
  45728. #endif
  45729. ret = wc_InitRng(&rng);
  45730. if (ret != 0)
  45731. ret = WC_TEST_RET_ENC_EC(ret);
  45732. if (ret == 0) {
  45733. ret = mp_init_multi(n, p1, p2, p3, NULL, NULL);
  45734. if (ret != 0)
  45735. ret = WC_TEST_RET_ENC_EC(ret);
  45736. }
  45737. if (ret == 0)
  45738. ret = GenerateP(p1, p2, p3,
  45739. ecPairsA, sizeof(ecPairsA) / sizeof(ecPairsA[0]), kA);
  45740. if (ret == 0) {
  45741. ret = mp_mul(p1, p2, n);
  45742. if (ret != 0)
  45743. ret = WC_TEST_RET_ENC_EC(ret);
  45744. }
  45745. if (ret == 0) {
  45746. ret = mp_mul(n, p3, n);
  45747. if (ret != 0)
  45748. ret = WC_TEST_RET_ENC_EC(ret);
  45749. }
  45750. if (ret != 0)
  45751. ERROR_OUT(ret, out);
  45752. /* Check the old prime test using the number that false positives.
  45753. * This test result should indicate as not prime. */
  45754. ret = mp_prime_is_prime(n, 40, &isPrime);
  45755. if (ret != 0)
  45756. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45757. if (isPrime)
  45758. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  45759. /* This test result should fail. It should indicate the value as prime. */
  45760. ret = mp_prime_is_prime(n, 8, &isPrime);
  45761. if (ret != 0)
  45762. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45763. if (!isPrime)
  45764. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  45765. /* This test result should indicate the value as not prime. */
  45766. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  45767. if (ret != 0)
  45768. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45769. if (isPrime)
  45770. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  45771. ret = mp_read_unsigned_bin(n, controlPrime, sizeof(controlPrime));
  45772. if (ret != 0)
  45773. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45774. /* This test result should indicate the value as prime. */
  45775. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  45776. if (ret != 0)
  45777. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45778. if (!isPrime)
  45779. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  45780. /* This test result should indicate the value as prime. */
  45781. isPrime = -1;
  45782. ret = mp_prime_is_prime(n, 8, &isPrime);
  45783. if (ret != 0)
  45784. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45785. if (!isPrime)
  45786. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  45787. ret = mp_read_unsigned_bin(n, testOne, sizeof(testOne));
  45788. if (ret != 0)
  45789. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45790. /* This test result should indicate the value as not prime. */
  45791. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  45792. if (ret != 0)
  45793. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45794. if (isPrime)
  45795. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  45796. ret = mp_prime_is_prime(n, 8, &isPrime);
  45797. if (ret != 0)
  45798. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45799. if (isPrime)
  45800. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  45801. ret = 0;
  45802. out:
  45803. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  45804. if (n != NULL) {
  45805. mp_clear(n);
  45806. XFREE(n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45807. }
  45808. if (p1 != NULL) {
  45809. mp_clear(p1);
  45810. XFREE(p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45811. }
  45812. if (p2 != NULL) {
  45813. mp_clear(p2);
  45814. XFREE(p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45815. }
  45816. if (p3 != NULL) {
  45817. mp_clear(p3);
  45818. XFREE(p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45819. }
  45820. #else
  45821. mp_clear(p3);
  45822. mp_clear(p2);
  45823. mp_clear(p1);
  45824. mp_clear(n);
  45825. #endif
  45826. wc_FreeRng(&rng);
  45827. return ret;
  45828. }
  45829. #endif /* WOLFSSL_PUBLIC_MP */
  45830. #if defined(ASN_BER_TO_DER) && \
  45831. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  45832. defined(OPENSSL_EXTRA_X509_SMALL))
  45833. /* wc_BerToDer is only public facing in the case of test cert or opensslextra */
  45834. typedef struct berDerTestData {
  45835. const byte *in;
  45836. word32 inSz;
  45837. const byte *out;
  45838. word32 outSz;
  45839. } berDerTestData;
  45840. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t berder_test(void)
  45841. {
  45842. wc_test_ret_t ret;
  45843. int i;
  45844. word32 len = 0, l;
  45845. byte out[32];
  45846. WOLFSSL_SMALL_STACK_STATIC const byte good1_in[] = { 0x30, 0x80, 0x00, 0x00 };
  45847. WOLFSSL_SMALL_STACK_STATIC const byte good1_out[] = { 0x30, 0x00 };
  45848. WOLFSSL_SMALL_STACK_STATIC const byte good2_in[] = { 0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00 };
  45849. WOLFSSL_SMALL_STACK_STATIC const byte good2_out[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  45850. WOLFSSL_SMALL_STACK_STATIC const byte good3_in[] = {
  45851. 0x24, 0x80, 0x04, 0x01, 0x01, 0x00, 0x00
  45852. };
  45853. WOLFSSL_SMALL_STACK_STATIC const byte good3_out[] = { 0x04, 0x1, 0x01 };
  45854. WOLFSSL_SMALL_STACK_STATIC const byte good4_in[] = {
  45855. 0x30, 0x80,
  45856. 0x02, 0x01, 0x01,
  45857. 0x30, 0x80,
  45858. 0x24, 0x80,
  45859. 0x04, 0x01, 0x01,
  45860. 0x04, 0x02, 0x02, 0x03,
  45861. 0x00, 0x00,
  45862. 0x06, 0x01, 0x01,
  45863. 0x00, 0x00,
  45864. 0x31, 0x80,
  45865. 0x06, 0x01, 0x01,
  45866. 0x00, 0x00,
  45867. 0x00, 0x00,
  45868. };
  45869. WOLFSSL_SMALL_STACK_STATIC const byte good4_out[] = {
  45870. 0x30, 0x12,
  45871. 0x02, 0x01, 0x01,
  45872. 0x30, 0x08,
  45873. 0x04, 0x03, 0x01, 0x02, 0x03,
  45874. 0x06, 0x01, 0x01,
  45875. 0x31, 0x03,
  45876. 0x06, 0x01, 0x01
  45877. };
  45878. WOLFSSL_SMALL_STACK_STATIC const byte good5_in[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  45879. berDerTestData testData[] = {
  45880. { good1_in, sizeof(good1_in), good1_out, sizeof(good1_out) },
  45881. { good2_in, sizeof(good2_in), good2_out, sizeof(good2_out) },
  45882. { good3_in, sizeof(good3_in), good3_out, sizeof(good3_out) },
  45883. { good4_in, sizeof(good4_in), good4_out, sizeof(good4_out) },
  45884. { good5_in, sizeof(good5_in), good5_in , sizeof(good5_in ) },
  45885. };
  45886. WOLFSSL_ENTER("berder_test");
  45887. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  45888. ret = wc_BerToDer(testData[i].in, testData[i].inSz, NULL, &len);
  45889. if (ret != LENGTH_ONLY_E)
  45890. return WC_TEST_RET_ENC_I(i);
  45891. if (len != testData[i].outSz)
  45892. return WC_TEST_RET_ENC_I(i);
  45893. len = testData[i].outSz;
  45894. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &len);
  45895. if (ret != 0)
  45896. return WC_TEST_RET_ENC_I(i);
  45897. if (XMEMCMP(out, testData[i].out, len) != 0)
  45898. return WC_TEST_RET_ENC_I(i);
  45899. for (l = 1; l < testData[i].inSz; l++) {
  45900. ret = wc_BerToDer(testData[i].in, l, NULL, &len);
  45901. if (ret != ASN_PARSE_E)
  45902. return WC_TEST_RET_ENC_EC(ret);
  45903. len = testData[i].outSz;
  45904. ret = wc_BerToDer(testData[i].in, l, out, &len);
  45905. if (ret != ASN_PARSE_E)
  45906. return WC_TEST_RET_ENC_EC(ret);
  45907. }
  45908. for (l = 0; l < testData[i].outSz-1; l++) {
  45909. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &l);
  45910. if (ret != BUFFER_E)
  45911. return WC_TEST_RET_ENC_EC(ret);
  45912. }
  45913. }
  45914. ret = wc_BerToDer(NULL, 4, NULL, NULL);
  45915. if (ret != BAD_FUNC_ARG)
  45916. return WC_TEST_RET_ENC_EC(ret);
  45917. ret = wc_BerToDer(out, 4, NULL, NULL);
  45918. if (ret != BAD_FUNC_ARG)
  45919. return WC_TEST_RET_ENC_EC(ret);
  45920. ret = wc_BerToDer(NULL, 4, NULL, &len);
  45921. if (ret != BAD_FUNC_ARG)
  45922. return WC_TEST_RET_ENC_EC(ret);
  45923. ret = wc_BerToDer(NULL, 4, out, NULL);
  45924. if (ret != BAD_FUNC_ARG)
  45925. return WC_TEST_RET_ENC_EC(ret);
  45926. ret = wc_BerToDer(out, 4, out, NULL);
  45927. if (ret != BAD_FUNC_ARG)
  45928. return WC_TEST_RET_ENC_EC(ret);
  45929. ret = wc_BerToDer(NULL, 4, out, &len);
  45930. if (ret != BAD_FUNC_ARG)
  45931. return WC_TEST_RET_ENC_EC(ret);
  45932. for (l = 1; l < sizeof(good4_out); l++) {
  45933. len = l;
  45934. ret = wc_BerToDer(good4_in, sizeof(good4_in), out, &len);
  45935. if (ret != BUFFER_E)
  45936. return WC_TEST_RET_ENC_EC(ret);
  45937. }
  45938. return 0;
  45939. }
  45940. #endif
  45941. #ifdef DEBUG_WOLFSSL
  45942. static THREAD_LS_T int log_cnt = 0;
  45943. static void my_Logging_cb(const int logLevel, const char *const logMessage)
  45944. {
  45945. (void)logLevel;
  45946. (void)logMessage;
  45947. log_cnt++;
  45948. }
  45949. #endif /* DEBUG_WOLFSSL */
  45950. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t logging_test(void)
  45951. {
  45952. wc_test_ret_t ret;
  45953. #ifdef DEBUG_WOLFSSL
  45954. const char* msg = "Testing, testing. 1, 2, 3, 4 ...";
  45955. byte a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  45956. byte b[256];
  45957. int i;
  45958. WOLFSSL_ENTER("logging_test (debug)");
  45959. for (i = 0; i < (int)sizeof(b); i++)
  45960. b[i] = i;
  45961. ret = wolfSSL_Debugging_ON();
  45962. if (ret != 0)
  45963. return WC_TEST_RET_ENC_EC(ret);
  45964. ret = wolfSSL_SetLoggingCb(my_Logging_cb);
  45965. if (ret != 0)
  45966. return WC_TEST_RET_ENC_EC(ret);
  45967. WOLFSSL_MSG(msg);
  45968. WOLFSSL_BUFFER(a, sizeof(a));
  45969. WOLFSSL_BUFFER(b, sizeof(b));
  45970. WOLFSSL_BUFFER(NULL, 0);
  45971. WOLFSSL_ERROR(MEMORY_E);
  45972. WOLFSSL_ERROR_MSG(msg);
  45973. /* turn off logs */
  45974. wolfSSL_Debugging_OFF();
  45975. /* capture log count */
  45976. i = log_cnt;
  45977. /* validate no logs are output when disabled */
  45978. WOLFSSL_MSG(msg);
  45979. WOLFSSL_BUFFER(a, sizeof(a));
  45980. WOLFSSL_BUFFER(b, sizeof(b));
  45981. WOLFSSL_BUFFER(NULL, 0);
  45982. WOLFSSL_ERROR(MEMORY_E);
  45983. WOLFSSL_ERROR_MSG(msg);
  45984. /* check the logs were disabled */
  45985. if (i != log_cnt)
  45986. return WC_TEST_RET_ENC_NC;
  45987. /* restore callback and leave logging enabled */
  45988. wolfSSL_SetLoggingCb(NULL);
  45989. wolfSSL_Debugging_ON();
  45990. /* suppress unused args */
  45991. (void)a;
  45992. (void)b;
  45993. #else
  45994. WOLFSSL_ENTER("logging_test");
  45995. ret = wolfSSL_Debugging_ON();
  45996. if (ret != NOT_COMPILED_IN)
  45997. return WC_TEST_RET_ENC_EC(ret);
  45998. wolfSSL_Debugging_OFF();
  45999. ret = wolfSSL_SetLoggingCb(NULL);
  46000. if (ret != NOT_COMPILED_IN)
  46001. return WC_TEST_RET_ENC_EC(ret);
  46002. #endif /* DEBUG_WOLFSSL */
  46003. return 0;
  46004. }
  46005. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  46006. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t wolfcrypt_mutex_test(void)
  46007. #else
  46008. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mutex_test(void)
  46009. #endif
  46010. {
  46011. #ifdef WOLFSSL_PTHREADS
  46012. wolfSSL_Mutex m;
  46013. #endif
  46014. #if defined(WOLFSSL_PTHREADS) || (!defined(WOLFSSL_NO_MALLOC) && \
  46015. !defined(WOLFSSL_USER_MUTEX) && defined(WOLFSSL_STATIC_MEMORY))
  46016. wc_test_ret_t ret;
  46017. #endif
  46018. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_USER_MUTEX)
  46019. #ifndef WOLFSSL_STATIC_MEMORY
  46020. wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
  46021. WOLFSSL_ENTER("[wolfcrypt_]mutex_test (1)");
  46022. #else
  46023. wolfSSL_Mutex *mm = (wolfSSL_Mutex*) XMALLOC(sizeof(wolfSSL_Mutex),
  46024. HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  46025. WOLFSSL_ENTER("[wolfcrypt_]mutex_test (2)");
  46026. if (mm != NULL) {
  46027. ret = wc_InitMutex(mm);
  46028. if (ret != 0) {
  46029. WOLFSSL_MSG("Init Mutex failed");
  46030. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  46031. return WC_TEST_RET_ENC_EC(ret);
  46032. }
  46033. }
  46034. #endif
  46035. if (mm == NULL)
  46036. return WC_TEST_RET_ENC_ERRNO;
  46037. wc_FreeMutex(mm);
  46038. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  46039. #endif
  46040. /* Can optionally enable advanced pthread tests using "ENABLE_PTHREAD_LOCKFREE_TESTS" */
  46041. #ifdef WOLFSSL_PTHREADS
  46042. ret = wc_InitMutex(&m);
  46043. if (ret != 0)
  46044. return WC_TEST_RET_ENC_EC(ret);
  46045. ret = wc_LockMutex(&m);
  46046. if (ret != 0)
  46047. return WC_TEST_RET_ENC_EC(ret);
  46048. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  46049. /* trying to free a locked mutex is not portable behavior with pthread */
  46050. /* Attempting to destroy a locked mutex results in undefined behavior */
  46051. ret = wc_FreeMutex(&m);
  46052. if (ret != BAD_MUTEX_E)
  46053. return WC_TEST_RET_ENC_EC(ret);
  46054. #endif
  46055. ret = wc_UnLockMutex(&m);
  46056. if (ret != 0)
  46057. return WC_TEST_RET_ENC_EC(ret);
  46058. ret = wc_FreeMutex(&m);
  46059. if (ret != 0)
  46060. return WC_TEST_RET_ENC_EC(ret);
  46061. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  46062. /* Trying to use a pthread after free'ing is not portable behavior */
  46063. ret = wc_LockMutex(&m);
  46064. if (ret != BAD_MUTEX_E)
  46065. return WC_TEST_RET_ENC_EC(ret);
  46066. ret = wc_UnLockMutex(&m);
  46067. if (ret != BAD_MUTEX_E)
  46068. return WC_TEST_RET_ENC_EC(ret);
  46069. #endif
  46070. #endif
  46071. return 0;
  46072. }
  46073. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  46074. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  46075. !defined(WOLFSSL_STATIC_MEMORY)
  46076. static wc_test_ret_t malloc_cnt = 0;
  46077. static wc_test_ret_t realloc_cnt = 0;
  46078. static wc_test_ret_t free_cnt = 0;
  46079. #ifdef WOLFSSL_DEBUG_MEMORY
  46080. static void *my_Malloc_cb(size_t size, const char* func, unsigned int line)
  46081. {
  46082. (void) func;
  46083. (void) line;
  46084. #else
  46085. static void *my_Malloc_cb(size_t size)
  46086. {
  46087. #endif
  46088. malloc_cnt++;
  46089. #ifndef WOLFSSL_NO_MALLOC
  46090. return malloc(size);
  46091. #else
  46092. WOLFSSL_MSG("No malloc available");
  46093. (void)size;
  46094. return NULL;
  46095. #endif
  46096. }
  46097. #ifdef WOLFSSL_DEBUG_MEMORY
  46098. static void my_Free_cb(void *ptr, const char* func, unsigned int line)
  46099. {
  46100. (void) func;
  46101. (void) line;
  46102. #else
  46103. static void my_Free_cb(void *ptr)
  46104. {
  46105. #endif
  46106. free_cnt++;
  46107. #ifndef WOLFSSL_NO_MALLOC
  46108. free(ptr);
  46109. #else
  46110. WOLFSSL_MSG("No free available");
  46111. (void)ptr;
  46112. #endif
  46113. }
  46114. #ifdef WOLFSSL_DEBUG_MEMORY
  46115. static void *my_Realloc_cb(void *ptr, size_t size, const char* func, unsigned int line)
  46116. {
  46117. (void) func;
  46118. (void) line;
  46119. #else
  46120. static void *my_Realloc_cb(void *ptr, size_t size)
  46121. {
  46122. #endif
  46123. realloc_cnt++;
  46124. #ifndef WOLFSSL_NO_MALLOC
  46125. return realloc(ptr, size);
  46126. #else
  46127. WOLFSSL_MSG("No realloc available");
  46128. (void)ptr;
  46129. (void)size;
  46130. return NULL;
  46131. #endif
  46132. }
  46133. #endif /* !WOLFSSL_NO_MALLOC */
  46134. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memcb_test(void)
  46135. {
  46136. wc_test_ret_t ret = 0;
  46137. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  46138. !defined(WOLFSSL_STATIC_MEMORY)
  46139. byte* b = NULL;
  46140. #endif
  46141. wolfSSL_Malloc_cb mc;
  46142. wolfSSL_Free_cb fc;
  46143. wolfSSL_Realloc_cb rc;
  46144. WOLFSSL_ENTER("memcb_test");
  46145. /* Save existing memory callbacks */
  46146. ret = wolfSSL_GetAllocators(&mc, &fc, &rc);
  46147. if (ret != 0)
  46148. return WC_TEST_RET_ENC_EC(ret);
  46149. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  46150. !defined(WOLFSSL_STATIC_MEMORY)
  46151. /* test realloc */
  46152. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46153. if (b == NULL) {
  46154. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_memcb);
  46155. }
  46156. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46157. b = NULL;
  46158. /* Use API. */
  46159. ret = wolfSSL_SetAllocators((wolfSSL_Malloc_cb)my_Malloc_cb,
  46160. (wolfSSL_Free_cb)my_Free_cb,
  46161. (wolfSSL_Realloc_cb)my_Realloc_cb);
  46162. if (ret != 0) {
  46163. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_memcb);
  46164. }
  46165. b = (byte*)XMALLOC(1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46166. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46167. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46168. #ifndef WOLFSSL_STATIC_MEMORY
  46169. #ifndef WOLFSSL_CHECK_MEM_ZERO
  46170. if (malloc_cnt != 1 || free_cnt != 1 || realloc_cnt != 1)
  46171. #else
  46172. /* Checking zeroized memory means realloc is a malloc and free. */
  46173. if (malloc_cnt != 2 || free_cnt != 2 || realloc_cnt != 0)
  46174. #endif
  46175. #else
  46176. if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
  46177. #endif
  46178. ret = WC_TEST_RET_ENC_NC;
  46179. #endif /* !WOLFSSL_NO_MALLOC */
  46180. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  46181. !defined(WOLFSSL_STATIC_MEMORY)
  46182. exit_memcb:
  46183. /* reset malloc/free/realloc counts */
  46184. malloc_cnt = 0;
  46185. free_cnt = 0;
  46186. realloc_cnt = 0;
  46187. #endif
  46188. /* restore memory callbacks */
  46189. wolfSSL_SetAllocators(mc, fc, rc);
  46190. return ret;
  46191. }
  46192. #endif /* USE_WOLFSSL_MEMORY && !WOLFSSL_NO_MALLOC */
  46193. #if defined(WOLFSSL_CAAM_BLOB)
  46194. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blob_test(void)
  46195. {
  46196. wc_test_ret_t ret = 0;
  46197. byte out[112];
  46198. byte blob[112];
  46199. word32 outSz;
  46200. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  46201. {
  46202. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  46203. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  46204. };
  46205. WOLFSSL_SMALL_STACK_STATIC const byte text[] =
  46206. {
  46207. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  46208. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  46209. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  46210. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  46211. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  46212. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  46213. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  46214. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  46215. };
  46216. WOLFSSL_ENTER("blob_test");
  46217. XMEMSET(blob, 0, sizeof(blob));
  46218. XMEMSET(out, 0, sizeof(out));
  46219. outSz = sizeof(blob);
  46220. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  46221. if (ret != 0)
  46222. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  46223. blob[outSz - 2] += 1;
  46224. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  46225. if (ret == 0) { /* should fail with altered blob */
  46226. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_blob);
  46227. }
  46228. XMEMSET(blob, 0, sizeof(blob));
  46229. outSz = sizeof(blob);
  46230. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  46231. if (ret != 0)
  46232. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  46233. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  46234. if (ret != 0)
  46235. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  46236. if (XMEMCMP(out, iv, sizeof(iv))) {
  46237. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_blob);
  46238. }
  46239. XMEMSET(blob, 0, sizeof(blob));
  46240. outSz = sizeof(blob);
  46241. ret = wc_caamCreateBlob((byte*)text, sizeof(text), blob, &outSz);
  46242. if (ret != 0)
  46243. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  46244. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  46245. if (ret != 0)
  46246. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  46247. if (XMEMCMP(out, text, sizeof(text))) {
  46248. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_blob);
  46249. }
  46250. exit_blob:
  46251. return ret;
  46252. }
  46253. #endif /* WOLFSSL_CAAM_BLOB */
  46254. #ifdef WOLF_CRYPTO_CB
  46255. /* Example custom context for crypto callback */
  46256. typedef struct {
  46257. int exampleVar; /* flag for testing if only crypt is enabled. */
  46258. } myCryptoDevCtx;
  46259. #ifdef WOLF_CRYPTO_CB_ONLY_RSA
  46260. /* Testing rsa cb when CB_ONLY_RSA is enabled
  46261. * When CB_ONLY_RSA is enabled, software imple. is not available.
  46262. *
  46263. * ctx callback ctx
  46264. * returen 0 on success, otherwise return negative
  46265. */
  46266. static wc_test_ret_t rsa_onlycb_test(myCryptoDevCtx *ctx)
  46267. {
  46268. wc_test_ret_t ret = 0;
  46269. #if !defined(NO_RSA)
  46270. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  46271. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key,
  46272. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46273. byte* tmp = NULL;
  46274. #else
  46275. RsaKey key[1];
  46276. byte tmp[FOURK_BUF];
  46277. #endif
  46278. size_t bytes;
  46279. const word32 inLen = (word32)TEST_STRING_SZ;
  46280. word32 idx = 0;
  46281. word32 sigSz;
  46282. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  46283. byte out[RSA_TEST_BYTES];
  46284. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  46285. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  46286. !defined(NO_FILESYSTEM)
  46287. XFILE file;
  46288. #endif
  46289. #ifdef WOLFSSL_KEY_GEN
  46290. WC_RNG rng;
  46291. #endif
  46292. #ifdef USE_CERT_BUFFERS_1024
  46293. bytes = (size_t)sizeof_client_key_der_1024;
  46294. if (bytes < (size_t)sizeof_client_cert_der_1024)
  46295. bytes = (size_t)sizeof_client_cert_der_1024;
  46296. #elif defined(USE_CERT_BUFFERS_2048)
  46297. bytes = (size_t)sizeof_client_key_der_2048;
  46298. if (bytes < (size_t)sizeof_client_cert_der_2048)
  46299. bytes = (size_t)sizeof_client_cert_der_2048;
  46300. #elif defined(USE_CERT_BUFFERS_3072)
  46301. bytes = (size_t)sizeof_client_key_der_3072;
  46302. if (bytes < (size_t)sizeof_client_cert_der_3072)
  46303. bytes = (size_t)sizeof_client_cert_der_3072;
  46304. #elif defined(USE_CERT_BUFFERS_4096)
  46305. bytes = (size_t)sizeof_client_key_der_4096;
  46306. if (bytes < (size_t)sizeof_client_cert_der_4096)
  46307. bytes = (size_t)sizeof_client_cert_der_4096;
  46308. #else
  46309. bytes = FOURK_BUF;
  46310. #endif
  46311. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  46312. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46313. if (tmp == NULL)
  46314. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_onlycb);
  46315. #endif
  46316. #ifdef USE_CERT_BUFFERS_1024
  46317. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  46318. #elif defined(USE_CERT_BUFFERS_2048)
  46319. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  46320. #elif defined(USE_CERT_BUFFERS_3072)
  46321. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  46322. #elif defined(USE_CERT_BUFFERS_4096)
  46323. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  46324. #elif !defined(NO_FILESYSTEM)
  46325. file = XFOPEN(clientKey, "rb");
  46326. if (!file) {
  46327. ret = WC_TEST_RET_ENC_ERRNO;
  46328. err_sys("can't open ./certs/client-key.der, "
  46329. "Please run from wolfSSL home dir", ret);
  46330. ERROR_OUT(ret, exit_onlycb);
  46331. }
  46332. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  46333. XFCLOSE(file);
  46334. if (bytes == 0)
  46335. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_onlycb);
  46336. #endif
  46337. #ifdef WOLFSSL_KEY_GEN
  46338. /* wc_CryptoCb_MakeRsaKey cb test, no actual making key
  46339. * wc_MakeRsaKey() -> rsa cb ->
  46340. * myCryptoDevCb -> wc_MakeRsaKey(CBONLY_TEST_DEVID)
  46341. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return 0(success)
  46342. */
  46343. ctx->exampleVar = 99;
  46344. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  46345. if (ret != 0)
  46346. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46347. /* wc_MakeRsaKey() -> rsa cb ->
  46348. * myCryptoDevCb -> wc_MakeRsaKey(INVALID_DEVID)
  46349. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return NO_VALID_DEVID(failure)
  46350. */
  46351. ctx->exampleVar = 1;
  46352. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  46353. if (ret != NO_VALID_DEVID) {
  46354. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46355. } else
  46356. /* reset return code */
  46357. ret = 0;
  46358. #endif
  46359. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  46360. if (ret != 0)
  46361. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46362. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  46363. if (ret != 0)
  46364. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46365. sigSz = (word32)wc_RsaEncryptSize(key);
  46366. /* wc_CryptoCb_Rsa cb test, no actual rsa operation */
  46367. if (ret == 0) {
  46368. /* wc_SignatureGenerate() -> rsa cb ->
  46369. * myCryptoDevCb -> wc_RsaFunction(CBONLY_TEST_DEVID)
  46370. * wc_RsaFunction(CBONLY_TEST_DEVID) expects to return 0(success)
  46371. */
  46372. ctx->exampleVar = 99;
  46373. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  46374. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  46375. if (ret != 0)
  46376. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46377. }
  46378. if (ret == 0) {
  46379. /* wc_SignatureGenerate() -> rsa cb ->
  46380. * myCryptoDevCb -> wc_RsaFunction(INVALID_DEVID)
  46381. * wc_SignatureGenerate(INVALID_DEVID) expects to
  46382. * return NO_VALID_DEVID(failure)
  46383. */
  46384. ctx->exampleVar = 1;
  46385. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  46386. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  46387. if (ret != NO_VALID_DEVID) {
  46388. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46389. } else
  46390. /* reset return code */
  46391. ret = 0;
  46392. }
  46393. exit_onlycb:
  46394. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  46395. if (key != NULL) {
  46396. wc_FreeRsaKey(key);
  46397. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46398. }
  46399. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46400. #else
  46401. wc_FreeRsaKey(key);
  46402. #endif
  46403. #endif
  46404. return ret;
  46405. }
  46406. #endif
  46407. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  46408. /* Testing rsa cb when CB_ONLY_ECC is enabled
  46409. * When CB_ONLY_ECC is enabled, software imple. is not available.
  46410. *
  46411. * ctx callback ctx
  46412. * returen 0 on success, otherwise return negative
  46413. */
  46414. static wc_test_ret_t ecc_onlycb_test(myCryptoDevCtx *ctx)
  46415. {
  46416. wc_test_ret_t ret = 0;
  46417. #if defined(HAVE_ECC)
  46418. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  46419. ecc_key* key = (ecc_key *)XMALLOC(sizeof *key,
  46420. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46421. ecc_key* pub = (ecc_key *)XMALLOC(sizeof *pub,
  46422. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46423. byte* out = (byte*)XMALLOC(sizeof(byte),
  46424. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46425. #ifdef OPENSSL_EXTRA
  46426. byte* check = (byte*)XMALLOC(sizeof(byte)*(256), HEAP_HINT,
  46427. DYNAMIC_TYPE_TMP_BUFFER);
  46428. #endif
  46429. #else
  46430. ecc_key key[1];
  46431. ecc_key pub[1];
  46432. byte out[256];
  46433. #ifdef OPENSSL_EXTRA
  46434. unsigned char check[256];
  46435. #endif
  46436. #endif
  46437. #ifdef OPENSSL_EXTRA
  46438. EVP_PKEY* privKey = NULL;
  46439. EVP_PKEY* pubKey = NULL;
  46440. #ifdef USE_CERT_BUFFERS_256
  46441. ecc_key* pkey;
  46442. const unsigned char* cp;
  46443. #endif
  46444. EVP_MD_CTX mdCtx;
  46445. const char testData[] = "Hi There";
  46446. size_t checkSz = -1;
  46447. const unsigned char* p;
  46448. const unsigned char check_v[256] = {
  46449. 0x30,0x45,0x02,0x20,0x1b,0x5c,0x2a,0xf0,0x18,0x09,
  46450. 0x74,0x65,0xa1,0x04,0x76,0x3a,0xce,0xcc,0xe5,0x34,
  46451. 0x5e,0x89,0xed,0x40,0x1e,0x5a,0xb1,0x53,0xb4,0xff,
  46452. 0xc7,0x18,0xfe,0x0f,0xc7,0xa6,0x02,0x21,0x00,0xe5,
  46453. 0x70,0x21,0xfc,0xf9,0x63,0x36,0xfd,0x16,0x18,0x08,
  46454. 0x9a,0x63,0x61,0x0f,0xe7,0x7c,0xa3,0xc9,0x14,0xa3,
  46455. 0x30,0x87,0xf7,0xf5,0x70,0x19,0xaf,0x56,0x96,0x9b,
  46456. 0xd8,0x64,0xcd,0xd9,0xff,0x7b,0x2a,0x55,0x52,0xca,
  46457. 0x41,0xb2,0xa6,0xa4,0x8a,0x3b,0x02,0x20,0x8c,0xc5,
  46458. 0xf9,0xc1,0x7d,0x2a,0x65,0x6c,0xe6,0x5a,0xe3,0x76,
  46459. 0x9b,0xab,0x0b,0x9f,0xaf,0x62,0x5d,0xb2,0x60,0xd7,
  46460. 0xeb,0xb4,0x1b,0x73,0xdc,0x01,0x7d,0x7b,0xab,0xc1,
  46461. 0x0c,0x74,0x96,0x41,0xe6,0x3f,0xc5,0x86,0xe6,0x7d,
  46462. 0x2b,0x9d,0x54,0x6b,0xcd,0x31,0x35,0x1f,0xdb,0x49,
  46463. 0x1f,0x32,0x34,0xf8,0x57,0x12,0x86,0x5c,0x0e,0x80,
  46464. 0x55,0x8d,0xff,0xd8,0xbd,0xdf,0x32,0x26,0x62,0x42,
  46465. 0x09,0xda,0xf7,0x74,0xf2,0x3f,0xe6,0xf1,0x77,0x82,
  46466. 0xce,0xe4,0xbb,0x61,0xa6,0xc0,0x17,0x0c,0x6c,0x47,
  46467. 0x2a,0x40,0x1c,0x2b,0xe0,0x98,0x3b,0xbf,0xc6,0xf8,
  46468. 0x6d,0xfd,0xd0,0xfa,0xc1,0x02,0xfb,0x5f,0xfb,0xb0,
  46469. 0xcb,0xd9,0xa3,0x59,0x94,0xe9,0x0f,0x74,0xbb,0x3f,
  46470. 0x64,0xa3,0x83,0xc4,0x2b,0xf7,0xd2,0x97,0xbf,0x3b,
  46471. 0xcf,0xbb,0x60,0x81,0x33,0x94,0xfa,0x0d,0x35,0xd2,
  46472. 0x3d,0xb9,0x99,0xe3,0x12,0xf8,0xf4,0xa3,0x74,0xf4,
  46473. 0x94,0x1d,0x7a,0x66,0xf8,0xd1,0x1d,0xcf,0xb0,0x48,
  46474. 0xef,0x8c,0x94,0x6f,0xdd,0x62,
  46475. };
  46476. #endif
  46477. WC_RNG rng;
  46478. EncryptedInfo encInfo;
  46479. int keyFormat = 0;
  46480. word32 keyIdx = 0;
  46481. byte in[] = "Everyone gets Friday off. ecc p";
  46482. word32 inLen = (word32)XSTRLEN((char*)in);
  46483. word32 outLen;
  46484. int verify;
  46485. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  46486. if (key == NULL || pub == NULL) {
  46487. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_onlycb);
  46488. }
  46489. #endif
  46490. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  46491. if (ret != 0)
  46492. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46493. /* wc_CryptoCb_MakeEccKey cb test, , no actual testing */
  46494. ctx->exampleVar = 99;
  46495. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  46496. if (ret != 0)
  46497. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46498. ctx->exampleVar = 1;
  46499. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  46500. if (ret != NO_VALID_DEVID) {
  46501. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46502. } else
  46503. /* reset return code */
  46504. ret = 0;
  46505. #ifdef USE_CERT_BUFFERS_256
  46506. if (ret == 0) {
  46507. /* load ECC private key and perform private transform */
  46508. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &keyIdx,
  46509. key, sizeof_ecc_key_der_256);
  46510. }
  46511. if (ret != 0)
  46512. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46513. /* wc_CryptoCb_EccSign cb test, no actual testing */
  46514. ctx->exampleVar = 99;
  46515. if (ret == 0) {
  46516. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  46517. }
  46518. if (ret != 0)
  46519. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46520. ctx->exampleVar = 1;
  46521. if (ret == 0) {
  46522. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  46523. }
  46524. if (ret != NO_VALID_DEVID) {
  46525. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46526. }
  46527. else
  46528. ret = 0;
  46529. /* wc_CryptoCb_EccVerify cb test, no actual testing */
  46530. ctx->exampleVar = 99;
  46531. if (ret == 0) {
  46532. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  46533. }
  46534. if (ret != 0)
  46535. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46536. ctx->exampleVar = 1;
  46537. if (ret == 0) {
  46538. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  46539. }
  46540. if (ret != NO_VALID_DEVID) {
  46541. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46542. }
  46543. else
  46544. ret = 0;
  46545. /* wc_CryptoCb_Ecdh cb test, no actual testing */
  46546. /* make public key for shared secret */
  46547. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  46548. ctx->exampleVar = 99;
  46549. if (ret == 0) {
  46550. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  46551. }
  46552. if (ret != 0)
  46553. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46554. ctx->exampleVar = 1;
  46555. if (ret == 0) {
  46556. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  46557. }
  46558. if (ret != NO_VALID_DEVID) {
  46559. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46560. }
  46561. else
  46562. ret = 0;
  46563. #ifdef OPENSSL_EXTRA
  46564. (void)pkey;
  46565. cp = ecc_clikey_der_256;
  46566. privKey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  46567. sizeof_ecc_clikey_der_256);
  46568. if (privKey == NULL) {
  46569. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  46570. }
  46571. pkey = (ecc_key*)privKey->ecc->internal;
  46572. pkey->devId = devId;
  46573. p = ecc_clikeypub_der_256;
  46574. pubKey = d2i_PUBKEY(NULL, &p, sizeof_ecc_clikeypub_der_256);
  46575. if (pubKey == NULL) {
  46576. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  46577. }
  46578. pkey = (ecc_key*)pubKey->ecc->internal;
  46579. pkey->devId = devId;
  46580. /* sign */
  46581. EVP_MD_CTX_init(&mdCtx);
  46582. ret = EVP_DigestSignInit(&mdCtx, NULL, EVP_sha256(), NULL, privKey);
  46583. if (ret != WOLFSSL_SUCCESS) {
  46584. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  46585. }
  46586. ret = EVP_DigestSignUpdate(&mdCtx, testData,
  46587. (unsigned int)XSTRLEN(testData));
  46588. if (ret != WOLFSSL_SUCCESS) {
  46589. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  46590. }
  46591. ret = EVP_DigestSignFinal(&mdCtx, NULL, &checkSz);
  46592. if (ret != WOLFSSL_SUCCESS) {
  46593. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  46594. }
  46595. ctx->exampleVar = 99;
  46596. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  46597. /* just called crypt callback as dummy
  46598. * EVP_DigestSignFinal returns 0 internally.
  46599. */
  46600. if (ret != 0)
  46601. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46602. ctx->exampleVar = 1;
  46603. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  46604. /* just called crypt callback as dummy
  46605. * EVP_DigestSignFinal returns 0 internally.
  46606. */
  46607. if (ret != 0)
  46608. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46609. /* restore checkSz for verify */
  46610. checkSz = 71;
  46611. ret = EVP_MD_CTX_cleanup(&mdCtx);
  46612. if (ret != SSL_SUCCESS) {
  46613. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  46614. }
  46615. /* verify */
  46616. EVP_MD_CTX_init(&mdCtx);
  46617. if (ret == SSL_SUCCESS) {
  46618. ret = EVP_DigestVerifyInit(&mdCtx, NULL, EVP_sha256(), NULL, pubKey);
  46619. }
  46620. if (ret != WOLFSSL_SUCCESS) {
  46621. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  46622. }
  46623. if (ret == WOLFSSL_SUCCESS) {
  46624. ret = EVP_DigestVerifyUpdate(&mdCtx, testData,
  46625. (unsigned int)XSTRLEN(testData));
  46626. }
  46627. if (ret != WOLFSSL_SUCCESS) {
  46628. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  46629. }
  46630. ctx->exampleVar = 99;
  46631. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  46632. /* just called crypt callback as dummy
  46633. * EVP_DigestSignFinal returns 0 internally.
  46634. */
  46635. if (ret != 0)
  46636. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  46637. ctx->exampleVar = 1;
  46638. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  46639. /* just called crypt callback as dummy
  46640. * EVP_DigestVerifyFinal returns -1 internally rather than NO_VALID_DEVID.
  46641. */
  46642. if (ret != -1) {
  46643. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  46644. }
  46645. ret = EVP_MD_CTX_cleanup(&mdCtx);
  46646. if (ret != SSL_SUCCESS) {
  46647. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  46648. } else
  46649. ret = 0;
  46650. #endif
  46651. #else
  46652. (void)verify;
  46653. (void)outLen;
  46654. (void)inLen;
  46655. (void)out;
  46656. (void)pub;
  46657. #ifdef OPENSSL_EXTRA
  46658. (void)privKey;
  46659. (void)pubKey;
  46660. (void)mdCtx;
  46661. (void)check;
  46662. (void)checkSz;
  46663. (void)p;
  46664. #endif
  46665. #endif
  46666. (void)keyFormat;
  46667. (void)encInfo;
  46668. exit_onlycb:
  46669. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  46670. if (key != NULL) {
  46671. wc_ecc_free(key);
  46672. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46673. }
  46674. if (pub != NULL) {
  46675. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46676. }
  46677. if (out != NULL) {
  46678. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46679. }
  46680. #ifdef OPENSSL_EXTRA
  46681. if (check) {
  46682. FREE(check, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46683. }
  46684. #endif
  46685. #else
  46686. wc_ecc_free(key);
  46687. #ifdef OPENSSL_EXTRA
  46688. if (privKey)
  46689. EVP_PKEY_free(privKey);
  46690. if (pubKey)
  46691. EVP_PKEY_free(pubKey);
  46692. #endif
  46693. #endif
  46694. #endif /* HAVE_ECC */
  46695. return ret;
  46696. }
  46697. #endif
  46698. /* Example crypto dev callback function that calls software version */
  46699. static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
  46700. {
  46701. int ret = NOT_COMPILED_IN; /* return this to bypass HW and use SW */
  46702. myCryptoDevCtx* myCtx = (myCryptoDevCtx*)ctx;
  46703. if (info == NULL)
  46704. return BAD_FUNC_ARG;
  46705. #ifdef DEBUG_WOLFSSL
  46706. WOLFSSL_MSG_EX("CryptoDevCb: Algo Type %d\n", info->algo_type);
  46707. #endif
  46708. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  46709. #if defined(WOLF_CRYPTO_CB) && !defined(HAVE_HASHDRBG) && \
  46710. !defined(WC_NO_RNG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  46711. /* if RNG only supports crypto callback, just use seed */
  46712. ret = wc_GenerateSeed(&info->rng.rng->seed,
  46713. info->rng.out, info->rng.sz);
  46714. #elif !defined(WC_NO_RNG)
  46715. /* set devId to invalid, so software is used */
  46716. info->rng.rng->devId = INVALID_DEVID;
  46717. ret = wc_RNG_GenerateBlock(info->rng.rng,
  46718. info->rng.out, info->rng.sz);
  46719. /* reset devId */
  46720. info->rng.rng->devId = devIdArg;
  46721. #endif
  46722. }
  46723. else if (info->algo_type == WC_ALGO_TYPE_SEED) {
  46724. #ifndef WC_NO_RNG
  46725. ALIGN32 static byte seed[sizeof(word32)] = { 0x00, 0x00, 0x00, 0x01 };
  46726. word32* seedWord32 = (word32*)seed;
  46727. word32 len;
  46728. /* wc_GenerateSeed is a local symbol so we need to fake the entropy. */
  46729. while (info->seed.sz > 0) {
  46730. len = (word32)sizeof(seed);
  46731. if (info->seed.sz < len)
  46732. len = info->seed.sz;
  46733. XMEMCPY(info->seed.seed, seed, sizeof(seed));
  46734. info->seed.seed += len;
  46735. info->seed.sz -= len;
  46736. (*seedWord32)++;
  46737. }
  46738. ret = 0;
  46739. #endif
  46740. }
  46741. else if (info->algo_type == WC_ALGO_TYPE_PK) {
  46742. #ifdef DEBUG_WOLFSSL
  46743. WOLFSSL_MSG_EX("CryptoDevCb: Pk Type %d\n", info->pk.type);
  46744. #endif
  46745. #ifndef NO_RSA
  46746. if (info->pk.type == WC_PK_TYPE_RSA) {
  46747. /* set devId to invalid, so software is used */
  46748. info->pk.rsa.key->devId = INVALID_DEVID;
  46749. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  46750. #ifdef DEBUG_WOLFSSL
  46751. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  46752. #endif
  46753. if (myCtx->exampleVar == 99) {
  46754. info->pk.rsa.key->devId = devIdArg;
  46755. return 0;
  46756. }
  46757. #endif
  46758. switch (info->pk.rsa.type) {
  46759. case RSA_PUBLIC_ENCRYPT:
  46760. case RSA_PUBLIC_DECRYPT:
  46761. /* perform software based RSA public op */
  46762. ret = wc_RsaFunction(
  46763. info->pk.rsa.in, info->pk.rsa.inLen,
  46764. info->pk.rsa.out, info->pk.rsa.outLen,
  46765. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  46766. break;
  46767. case RSA_PRIVATE_ENCRYPT:
  46768. case RSA_PRIVATE_DECRYPT:
  46769. /* perform software based RSA private op */
  46770. ret = wc_RsaFunction(
  46771. info->pk.rsa.in, info->pk.rsa.inLen,
  46772. info->pk.rsa.out, info->pk.rsa.outLen,
  46773. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  46774. break;
  46775. }
  46776. /* reset devId */
  46777. info->pk.rsa.key->devId = devIdArg;
  46778. }
  46779. #ifdef WOLFSSL_KEY_GEN
  46780. else if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
  46781. info->pk.rsakg.key->devId = INVALID_DEVID;
  46782. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  46783. #ifdef DEBUG_WOLFSSL
  46784. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  46785. #endif
  46786. if (myCtx->exampleVar == 99) {
  46787. info->pk.rsakg.key->devId = devIdArg;
  46788. return 0;
  46789. }
  46790. #endif
  46791. #ifdef HAVE_FIPS
  46792. for (;;) {
  46793. #endif
  46794. ret = wc_MakeRsaKey(info->pk.rsakg.key, info->pk.rsakg.size,
  46795. info->pk.rsakg.e, info->pk.rsakg.rng);
  46796. #ifdef HAVE_FIPS
  46797. if (ret == PRIME_GEN_E)
  46798. continue;
  46799. break;
  46800. }
  46801. #endif
  46802. /* reset devId */
  46803. info->pk.rsakg.key->devId = devIdArg;
  46804. }
  46805. #endif
  46806. #endif /* !NO_RSA */
  46807. #ifdef HAVE_ECC
  46808. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  46809. /* set devId to invalid, so software is used */
  46810. info->pk.eckg.key->devId = INVALID_DEVID;
  46811. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  46812. #ifdef DEBUG_WOLFSSL
  46813. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  46814. #endif
  46815. if (myCtx->exampleVar == 99) {
  46816. info->pk.eckg.key->devId = devIdArg;
  46817. return 0;
  46818. }
  46819. #endif
  46820. ret = wc_ecc_make_key_ex(info->pk.eckg.rng, info->pk.eckg.size,
  46821. info->pk.eckg.key, info->pk.eckg.curveId);
  46822. /* reset devId */
  46823. info->pk.eckg.key->devId = devIdArg;
  46824. }
  46825. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  46826. /* set devId to invalid, so software is used */
  46827. info->pk.eccsign.key->devId = INVALID_DEVID;
  46828. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  46829. #ifdef DEBUG_WOLFSSL
  46830. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  46831. #endif
  46832. if (myCtx->exampleVar == 99) {
  46833. info->pk.eccsign.key->devId = devIdArg;
  46834. return 0;
  46835. }
  46836. #endif
  46837. ret = wc_ecc_sign_hash(
  46838. info->pk.eccsign.in, info->pk.eccsign.inlen,
  46839. info->pk.eccsign.out, info->pk.eccsign.outlen,
  46840. info->pk.eccsign.rng, info->pk.eccsign.key);
  46841. /* reset devId */
  46842. info->pk.eccsign.key->devId = devIdArg;
  46843. }
  46844. else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
  46845. /* set devId to invalid, so software is used */
  46846. info->pk.eccverify.key->devId = INVALID_DEVID;
  46847. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  46848. #ifdef DEBUG_WOLFSSL
  46849. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  46850. #endif
  46851. if (myCtx->exampleVar == 99) {
  46852. info->pk.eccverify.key->devId = devIdArg;
  46853. return 0;
  46854. }
  46855. #endif
  46856. ret = wc_ecc_verify_hash(
  46857. info->pk.eccverify.sig, info->pk.eccverify.siglen,
  46858. info->pk.eccverify.hash, info->pk.eccverify.hashlen,
  46859. info->pk.eccverify.res, info->pk.eccverify.key);
  46860. /* reset devId */
  46861. info->pk.eccverify.key->devId = devIdArg;
  46862. }
  46863. else if (info->pk.type == WC_PK_TYPE_ECDH) {
  46864. /* set devId to invalid, so software is used */
  46865. info->pk.ecdh.private_key->devId = INVALID_DEVID;
  46866. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  46867. #ifdef DEBUG_WOLFSSL
  46868. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  46869. #endif
  46870. if (myCtx->exampleVar == 99) {
  46871. info->pk.ecdh.private_key->devId = devIdArg;
  46872. return 0;
  46873. }
  46874. #endif
  46875. ret = wc_ecc_shared_secret(
  46876. info->pk.ecdh.private_key, info->pk.ecdh.public_key,
  46877. info->pk.ecdh.out, info->pk.ecdh.outlen);
  46878. /* reset devId */
  46879. info->pk.ecdh.private_key->devId = devIdArg;
  46880. }
  46881. #endif /* HAVE_ECC */
  46882. #ifdef HAVE_CURVE25519
  46883. if (info->pk.type == WC_PK_TYPE_CURVE25519_KEYGEN) {
  46884. /* set devId to invalid, so software is used */
  46885. info->pk.curve25519kg.key->devId = INVALID_DEVID;
  46886. ret = wc_curve25519_make_key(info->pk.curve25519kg.rng,
  46887. info->pk.curve25519kg.size, info->pk.curve25519kg.key);
  46888. /* reset devId */
  46889. info->pk.curve25519kg.key->devId = devIdArg;
  46890. }
  46891. else if (info->pk.type == WC_PK_TYPE_CURVE25519) {
  46892. /* set devId to invalid, so software is used */
  46893. info->pk.curve25519.private_key->devId = INVALID_DEVID;
  46894. ret = wc_curve25519_shared_secret_ex(
  46895. info->pk.curve25519.private_key, info->pk.curve25519.public_key,
  46896. info->pk.curve25519.out, info->pk.curve25519.outlen,
  46897. info->pk.curve25519.endian);
  46898. /* reset devId */
  46899. info->pk.curve25519.private_key->devId = devIdArg;
  46900. }
  46901. #endif /* HAVE_CURVE25519 */
  46902. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_MAKE_KEY)
  46903. if (info->pk.type == WC_PK_TYPE_ED25519_KEYGEN) {
  46904. /* set devId to invalid, so software is used */
  46905. info->pk.ed25519kg.key->devId = INVALID_DEVID;
  46906. ret = wc_ed25519_make_key(info->pk.ed25519kg.rng,
  46907. info->pk.ed25519kg.size, info->pk.ed25519kg.key);
  46908. /* reset devId */
  46909. info->pk.ed25519kg.key->devId = devIdArg;
  46910. }
  46911. #ifdef HAVE_ED25519_SIGN
  46912. else if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  46913. /* set devId to invalid, so software is used */
  46914. info->pk.ed25519sign.key->devId = INVALID_DEVID;
  46915. ret = wc_ed25519_sign_msg_ex(
  46916. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  46917. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  46918. info->pk.ed25519sign.key, info->pk.ed25519sign.type,
  46919. info->pk.ed25519sign.context, info->pk.ed25519sign.contextLen);
  46920. /* reset devId */
  46921. info->pk.ed25519sign.key->devId = devIdArg;
  46922. }
  46923. #endif
  46924. #ifdef HAVE_ED25519_VERIFY
  46925. else if (info->pk.type == WC_PK_TYPE_ED25519_VERIFY) {
  46926. /* set devId to invalid, so software is used */
  46927. info->pk.ed25519verify.key->devId = INVALID_DEVID;
  46928. ret = wc_ed25519_verify_msg_ex(
  46929. info->pk.ed25519verify.sig, info->pk.ed25519verify.sigLen,
  46930. info->pk.ed25519verify.msg, info->pk.ed25519verify.msgLen,
  46931. info->pk.ed25519verify.res, info->pk.ed25519verify.key,
  46932. info->pk.ed25519verify.type, info->pk.ed25519verify.context,
  46933. info->pk.ed25519verify.contextLen);
  46934. /* reset devId */
  46935. info->pk.ed25519verify.key->devId = devIdArg;
  46936. }
  46937. #endif
  46938. #endif /* HAVE_ED25519 */
  46939. }
  46940. else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  46941. #if !defined(NO_AES) || !defined(NO_DES3)
  46942. #ifdef HAVE_AESGCM
  46943. if (info->cipher.type == WC_CIPHER_AES_GCM) {
  46944. if (info->cipher.enc) {
  46945. /* set devId to invalid, so software is used */
  46946. info->cipher.aesgcm_enc.aes->devId = INVALID_DEVID;
  46947. ret = wc_AesGcmEncrypt(
  46948. info->cipher.aesgcm_enc.aes,
  46949. info->cipher.aesgcm_enc.out,
  46950. info->cipher.aesgcm_enc.in,
  46951. info->cipher.aesgcm_enc.sz,
  46952. info->cipher.aesgcm_enc.iv,
  46953. info->cipher.aesgcm_enc.ivSz,
  46954. info->cipher.aesgcm_enc.authTag,
  46955. info->cipher.aesgcm_enc.authTagSz,
  46956. info->cipher.aesgcm_enc.authIn,
  46957. info->cipher.aesgcm_enc.authInSz);
  46958. /* reset devId */
  46959. info->cipher.aesgcm_enc.aes->devId = devIdArg;
  46960. }
  46961. else {
  46962. /* set devId to invalid, so software is used */
  46963. info->cipher.aesgcm_dec.aes->devId = INVALID_DEVID;
  46964. ret = wc_AesGcmDecrypt(
  46965. info->cipher.aesgcm_dec.aes,
  46966. info->cipher.aesgcm_dec.out,
  46967. info->cipher.aesgcm_dec.in,
  46968. info->cipher.aesgcm_dec.sz,
  46969. info->cipher.aesgcm_dec.iv,
  46970. info->cipher.aesgcm_dec.ivSz,
  46971. info->cipher.aesgcm_dec.authTag,
  46972. info->cipher.aesgcm_dec.authTagSz,
  46973. info->cipher.aesgcm_dec.authIn,
  46974. info->cipher.aesgcm_dec.authInSz);
  46975. /* reset devId */
  46976. info->cipher.aesgcm_dec.aes->devId = devIdArg;
  46977. }
  46978. }
  46979. #endif /* HAVE_AESGCM */
  46980. #ifdef HAVE_AES_CBC
  46981. if (info->cipher.type == WC_CIPHER_AES_CBC) {
  46982. if (info->cipher.enc) {
  46983. /* set devId to invalid, so software is used */
  46984. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  46985. ret = wc_AesCbcEncrypt(
  46986. info->cipher.aescbc.aes,
  46987. info->cipher.aescbc.out,
  46988. info->cipher.aescbc.in,
  46989. info->cipher.aescbc.sz);
  46990. /* reset devId */
  46991. info->cipher.aescbc.aes->devId = devIdArg;
  46992. }
  46993. else {
  46994. /* set devId to invalid, so software is used */
  46995. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  46996. ret = wc_AesCbcDecrypt(
  46997. info->cipher.aescbc.aes,
  46998. info->cipher.aescbc.out,
  46999. info->cipher.aescbc.in,
  47000. info->cipher.aescbc.sz);
  47001. /* reset devId */
  47002. info->cipher.aescbc.aes->devId = devIdArg;
  47003. }
  47004. }
  47005. #endif /* HAVE_AES_CBC */
  47006. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  47007. if (info->cipher.type == WC_CIPHER_AES_ECB) {
  47008. if (info->cipher.enc) {
  47009. /* set devId to invalid, so software is used */
  47010. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  47011. ret = wc_AesEcbEncrypt(
  47012. info->cipher.aesecb.aes,
  47013. info->cipher.aesecb.out,
  47014. info->cipher.aesecb.in,
  47015. info->cipher.aesecb.sz);
  47016. /* reset devId */
  47017. info->cipher.aesecb.aes->devId = devIdArg;
  47018. }
  47019. else {
  47020. /* set devId to invalid, so software is used */
  47021. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  47022. ret = wc_AesEcbDecrypt(
  47023. info->cipher.aesecb.aes,
  47024. info->cipher.aesecb.out,
  47025. info->cipher.aesecb.in,
  47026. info->cipher.aesecb.sz);
  47027. /* reset devId */
  47028. info->cipher.aesecb.aes->devId = devIdArg;
  47029. }
  47030. }
  47031. #endif /* HAVE_AES_ECB */
  47032. #if defined(WOLFSSL_AES_COUNTER) && !defined(HAVE_FIPS) && \
  47033. !defined(HAVE_SELFTEST)
  47034. if (info->cipher.type == WC_CIPHER_AES_CTR) {
  47035. /* set devId to invalid, so software is used */
  47036. info->cipher.aesctr.aes->devId = INVALID_DEVID;
  47037. ret = wc_AesCtrEncrypt(
  47038. info->cipher.aesctr.aes,
  47039. info->cipher.aesctr.out,
  47040. info->cipher.aesctr.in,
  47041. info->cipher.aesctr.sz);
  47042. /* reset devId */
  47043. info->cipher.aesctr.aes->devId = devIdArg;
  47044. }
  47045. #endif /* WOLFSSL_AES_COUNTER */
  47046. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  47047. if (info->cipher.type == WC_CIPHER_AES_CCM) {
  47048. if (info->cipher.enc) {
  47049. /* set devId to invalid, so software is used */
  47050. info->cipher.aesccm_enc.aes->devId = INVALID_DEVID;
  47051. ret = wc_AesCcmEncrypt(
  47052. info->cipher.aesccm_enc.aes,
  47053. info->cipher.aesccm_enc.out,
  47054. info->cipher.aesccm_enc.in,
  47055. info->cipher.aesccm_enc.sz,
  47056. info->cipher.aesccm_enc.nonce,
  47057. info->cipher.aesccm_enc.nonceSz,
  47058. info->cipher.aesccm_enc.authTag,
  47059. info->cipher.aesccm_enc.authTagSz,
  47060. info->cipher.aesccm_enc.authIn,
  47061. info->cipher.aesccm_enc.authInSz);
  47062. /* reset devId */
  47063. info->cipher.aesccm_enc.aes->devId = devIdArg;
  47064. }
  47065. else {
  47066. /* set devId to invalid, so software is used */
  47067. info->cipher.aesccm_dec.aes->devId = INVALID_DEVID;
  47068. ret = wc_AesCcmDecrypt(
  47069. info->cipher.aesccm_dec.aes,
  47070. info->cipher.aesccm_dec.out,
  47071. info->cipher.aesccm_dec.in,
  47072. info->cipher.aesccm_dec.sz,
  47073. info->cipher.aesccm_dec.nonce,
  47074. info->cipher.aesccm_dec.nonceSz,
  47075. info->cipher.aesccm_dec.authTag,
  47076. info->cipher.aesccm_dec.authTagSz,
  47077. info->cipher.aesccm_dec.authIn,
  47078. info->cipher.aesccm_dec.authInSz);
  47079. /* reset devId */
  47080. info->cipher.aesccm_dec.aes->devId = devIdArg;
  47081. }
  47082. }
  47083. #endif
  47084. #ifndef NO_DES3
  47085. if (info->cipher.type == WC_CIPHER_DES3) {
  47086. if (info->cipher.enc) {
  47087. /* set devId to invalid, so software is used */
  47088. info->cipher.des3.des->devId = INVALID_DEVID;
  47089. ret = wc_Des3_CbcEncrypt(
  47090. info->cipher.des3.des,
  47091. info->cipher.des3.out,
  47092. info->cipher.des3.in,
  47093. info->cipher.des3.sz);
  47094. /* reset devId */
  47095. info->cipher.des3.des->devId = devIdArg;
  47096. }
  47097. else {
  47098. /* set devId to invalid, so software is used */
  47099. info->cipher.des3.des->devId = INVALID_DEVID;
  47100. ret = wc_Des3_CbcDecrypt(
  47101. info->cipher.des3.des,
  47102. info->cipher.des3.out,
  47103. info->cipher.des3.in,
  47104. info->cipher.des3.sz);
  47105. /* reset devId */
  47106. info->cipher.des3.des->devId = devIdArg;
  47107. }
  47108. }
  47109. #endif /* !NO_DES3 */
  47110. #endif /* !NO_AES || !NO_DES3 */
  47111. }
  47112. #if !defined(NO_SHA) || !defined(NO_SHA256) || \
  47113. defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
  47114. else if (info->algo_type == WC_ALGO_TYPE_HASH) {
  47115. #if !defined(NO_SHA)
  47116. if (info->hash.type == WC_HASH_TYPE_SHA) {
  47117. if (info->hash.sha1 == NULL)
  47118. return NOT_COMPILED_IN;
  47119. /* set devId to invalid, so software is used */
  47120. info->hash.sha1->devId = INVALID_DEVID;
  47121. if (info->hash.in != NULL) {
  47122. ret = wc_ShaUpdate(
  47123. info->hash.sha1,
  47124. info->hash.in,
  47125. info->hash.inSz);
  47126. }
  47127. if (info->hash.digest != NULL) {
  47128. ret = wc_ShaFinal(
  47129. info->hash.sha1,
  47130. info->hash.digest);
  47131. }
  47132. /* reset devId */
  47133. info->hash.sha1->devId = devIdArg;
  47134. }
  47135. else
  47136. #endif
  47137. #if !defined(NO_SHA256)
  47138. if (info->hash.type == WC_HASH_TYPE_SHA256) {
  47139. if (info->hash.sha256 == NULL)
  47140. return NOT_COMPILED_IN;
  47141. /* set devId to invalid, so software is used */
  47142. info->hash.sha256->devId = INVALID_DEVID;
  47143. if (info->hash.in != NULL) {
  47144. ret = wc_Sha256Update(
  47145. info->hash.sha256,
  47146. info->hash.in,
  47147. info->hash.inSz);
  47148. }
  47149. if (info->hash.digest != NULL) {
  47150. ret = wc_Sha256Final(
  47151. info->hash.sha256,
  47152. info->hash.digest);
  47153. }
  47154. /* reset devId */
  47155. info->hash.sha256->devId = devIdArg;
  47156. }
  47157. else
  47158. #endif
  47159. #ifdef WOLFSSL_SHA384
  47160. if (info->hash.type == WC_HASH_TYPE_SHA384) {
  47161. if (info->hash.sha384 == NULL)
  47162. return NOT_COMPILED_IN;
  47163. #ifndef NO_SHA2_CRYPTO_CB
  47164. /* set devId to invalid, so software is used */
  47165. info->hash.sha384->devId = INVALID_DEVID;
  47166. #endif
  47167. if (info->hash.in != NULL) {
  47168. ret = wc_Sha384Update(
  47169. info->hash.sha384,
  47170. info->hash.in,
  47171. info->hash.inSz);
  47172. }
  47173. if (info->hash.digest != NULL) {
  47174. ret = wc_Sha384Final(
  47175. info->hash.sha384,
  47176. info->hash.digest);
  47177. }
  47178. #ifndef NO_SHA2_CRYPTO_CB
  47179. /* reset devId */
  47180. info->hash.sha384->devId = devIdArg;
  47181. #endif
  47182. }
  47183. else
  47184. #endif
  47185. #ifdef WOLFSSL_SHA512
  47186. if (info->hash.type == WC_HASH_TYPE_SHA512) {
  47187. if (info->hash.sha512 == NULL)
  47188. return NOT_COMPILED_IN;
  47189. #ifndef NO_SHA2_CRYPTO_CB
  47190. /* set devId to invalid, so software is used */
  47191. info->hash.sha512->devId = INVALID_DEVID;
  47192. #endif
  47193. if (info->hash.in != NULL) {
  47194. ret = wc_Sha512Update(
  47195. info->hash.sha512,
  47196. info->hash.in,
  47197. info->hash.inSz);
  47198. }
  47199. if (info->hash.digest != NULL) {
  47200. ret = wc_Sha512Final(
  47201. info->hash.sha512,
  47202. info->hash.digest);
  47203. }
  47204. #ifndef NO_SHA2_CRYPTO_CB
  47205. /* reset devId */
  47206. info->hash.sha512->devId = devIdArg;
  47207. #endif
  47208. }
  47209. else
  47210. #endif
  47211. {
  47212. }
  47213. }
  47214. #endif /* !NO_SHA || !NO_SHA256 */
  47215. #ifndef NO_HMAC
  47216. else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
  47217. if (info->hmac.hmac == NULL)
  47218. return NOT_COMPILED_IN;
  47219. /* set devId to invalid, so software is used */
  47220. info->hmac.hmac->devId = INVALID_DEVID;
  47221. if (info->hmac.in != NULL) {
  47222. ret = wc_HmacUpdate(
  47223. info->hmac.hmac,
  47224. info->hmac.in,
  47225. info->hmac.inSz);
  47226. }
  47227. else if (info->hmac.digest != NULL) {
  47228. ret = wc_HmacFinal(
  47229. info->hmac.hmac,
  47230. info->hmac.digest);
  47231. }
  47232. /* reset devId */
  47233. info->hmac.hmac->devId = devIdArg;
  47234. }
  47235. #endif
  47236. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  47237. else if (info->algo_type == WC_ALGO_TYPE_CMAC) {
  47238. if (info->cmac.cmac == NULL) {
  47239. return NOT_COMPILED_IN;
  47240. }
  47241. /* set devId to invalid so software is used */
  47242. info->cmac.cmac->devId = INVALID_DEVID;
  47243. /* Handle one-shot cases */
  47244. if (info->cmac.key != NULL && info->cmac.in != NULL
  47245. && info->cmac.out != NULL) {
  47246. ret = wc_AesCmacGenerate(info->cmac.out,
  47247. info->cmac.outSz,
  47248. info->cmac.in,
  47249. info->cmac.inSz,
  47250. info->cmac.key,
  47251. info->cmac.keySz);
  47252. /* Sequentially handle incremental cases */
  47253. } else {
  47254. if (info->cmac.key != NULL) {
  47255. ret = wc_InitCmac(info->cmac.cmac,
  47256. info->cmac.key,
  47257. info->cmac.keySz,
  47258. info->cmac.type,
  47259. NULL);
  47260. }
  47261. if ((ret == 0) && (info->cmac.in != NULL)) {
  47262. ret = wc_CmacUpdate(info->cmac.cmac,
  47263. info->cmac.in,
  47264. info->cmac.inSz);
  47265. }
  47266. if ((ret ==0) && (info->cmac.out != NULL)) {
  47267. ret = wc_CmacFinal(info->cmac.cmac,
  47268. info->cmac.out,
  47269. info->cmac.outSz);
  47270. }
  47271. }
  47272. /* reset devId */
  47273. info->cmac.cmac->devId = devIdArg;
  47274. }
  47275. #endif /* WOLFSSL_CMAC && !(NO_AES) && WOLFSSL_AES_DIRECT */
  47276. (void)devIdArg;
  47277. (void)myCtx;
  47278. return ret;
  47279. }
  47280. #ifdef WOLF_CRYPTO_CB_FIND
  47281. static int myCryptoCbFind(int currentId, int algoType)
  47282. {
  47283. /* can have algo specific overrides here
  47284. switch (algoType) {
  47285. i.e.
  47286. WC_ALGO_TYPE_CMAC
  47287. WC_ALGO_TYPE_SEED
  47288. WC_ALGO_TYPE_HMAC
  47289. WC_ALGO_TYPE_HASH
  47290. WC_ALGO_TYPE_CIPHER
  47291. WC_ALGO_TYPE_PK
  47292. }
  47293. */
  47294. (void)algoType;
  47295. if (currentId == INVALID_DEVID) {
  47296. /* can override invalid devid found with 1 */
  47297. }
  47298. return currentId;
  47299. }
  47300. #endif /* WOLF_CRYPTO_CB_FIND */
  47301. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cryptocb_test(void)
  47302. {
  47303. wc_test_ret_t ret = 0;
  47304. int origDevId = devId;
  47305. myCryptoDevCtx myCtx;
  47306. WOLFSSL_ENTER("cryptocb_test");
  47307. /* example data for callback */
  47308. myCtx.exampleVar = 1;
  47309. /* set devId to something other than INVALID_DEVID */
  47310. devId = 1;
  47311. ret = wc_CryptoCb_RegisterDevice(devId, myCryptoDevCb, &myCtx);
  47312. #ifdef WOLF_CRYPTO_CB_FIND
  47313. wc_CryptoCb_SetDeviceFindCb(myCryptoCbFind);
  47314. #endif /* WOLF_CRYPTO_CB_FIND */
  47315. #ifndef WC_NO_RNG
  47316. if (ret == 0)
  47317. ret = random_test();
  47318. #endif /* WC_NO_RNG */
  47319. #if !defined(NO_RSA)
  47320. PRIVATE_KEY_UNLOCK();
  47321. if (ret == 0)
  47322. ret = rsa_test();
  47323. PRIVATE_KEY_LOCK();
  47324. #endif
  47325. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  47326. PRIVATE_KEY_UNLOCK();
  47327. if (ret == 0)
  47328. ret = rsa_onlycb_test(&myCtx);
  47329. PRIVATE_KEY_LOCK();
  47330. #endif
  47331. #if defined(HAVE_ECC)
  47332. PRIVATE_KEY_UNLOCK();
  47333. if (ret == 0)
  47334. ret = ecc_test();
  47335. PRIVATE_KEY_LOCK();
  47336. #endif
  47337. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  47338. PRIVATE_KEY_UNLOCK();
  47339. if (ret == 0)
  47340. ret = ecc_onlycb_test(&myCtx);
  47341. PRIVATE_KEY_LOCK();
  47342. #endif
  47343. #ifdef HAVE_ED25519
  47344. PRIVATE_KEY_UNLOCK();
  47345. if (ret == 0)
  47346. ret = ed25519_test();
  47347. PRIVATE_KEY_LOCK();
  47348. #endif
  47349. #ifdef HAVE_CURVE25519
  47350. if (ret == 0)
  47351. ret = curve25519_test();
  47352. #endif
  47353. #ifndef NO_AES
  47354. #ifdef HAVE_AESGCM
  47355. if (ret == 0)
  47356. ret = aesgcm_test();
  47357. #endif
  47358. #ifdef HAVE_AES_CBC
  47359. if (ret == 0)
  47360. ret = aes_test();
  47361. #endif
  47362. #ifdef WOLFSSL_AES_XTS
  47363. if (ret == 0)
  47364. ret = aes_xts_test();
  47365. #endif
  47366. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  47367. if (ret == 0)
  47368. ret = aesccm_test();
  47369. #endif
  47370. #endif /* !NO_AES */
  47371. #ifndef NO_DES3
  47372. if (ret == 0)
  47373. ret = des3_test();
  47374. #endif /* !NO_DES3 */
  47375. #ifndef NO_SHA
  47376. if (ret == 0)
  47377. ret = sha_test();
  47378. #endif
  47379. #ifndef NO_SHA256
  47380. if (ret == 0)
  47381. ret = sha256_test();
  47382. #endif
  47383. #ifdef WOLFSSL_SHA384
  47384. if (ret == 0)
  47385. ret = sha384_test();
  47386. #endif
  47387. #ifdef WOLFSSL_SHA512
  47388. if (ret == 0)
  47389. ret = sha512_test();
  47390. #endif
  47391. #ifndef NO_HMAC
  47392. #ifndef NO_SHA
  47393. if (ret == 0)
  47394. ret = hmac_sha_test();
  47395. #endif
  47396. #ifndef NO_SHA256
  47397. if (ret == 0)
  47398. ret = hmac_sha256_test();
  47399. #endif
  47400. #endif
  47401. #ifndef NO_PWDBASED
  47402. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  47403. PRIVATE_KEY_UNLOCK();
  47404. if (ret == 0)
  47405. ret = pbkdf2_test();
  47406. PRIVATE_KEY_LOCK();
  47407. #endif
  47408. #endif
  47409. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  47410. if (ret == 0)
  47411. ret = cmac_test();
  47412. #endif
  47413. /* restore devId */
  47414. devId = origDevId;
  47415. return ret;
  47416. }
  47417. #endif /* WOLF_CRYPTO_CB */
  47418. #ifdef WOLFSSL_CERT_PIV
  47419. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certpiv_test(void)
  47420. {
  47421. wc_test_ret_t ret;
  47422. wc_CertPIV piv;
  47423. /* Template for Identiv PIV cert, nonce and signature */
  47424. WOLFSSL_SMALL_STACK_STATIC const byte pivCertIdentiv[] = {
  47425. 0x0A, 0x0B,
  47426. 0x53, 0x09, /* NIST PIV Cert */
  47427. 0x70, 0x02, /* Certificate */
  47428. 0x30, 0x00,
  47429. 0x71, 0x01, 0x05, /* Cert Info */
  47430. 0xFE, 0x00, /* Error Detection */
  47431. 0x0B, 0x01, 0x00, /* Nonce */
  47432. 0x0C, 0x01, 0x00, /* Signed Nonce */
  47433. };
  47434. /* PIV certificate data including certificate, info and error detection. */
  47435. WOLFSSL_SMALL_STACK_STATIC const byte pivCert[] = {
  47436. 0x53, 0x09, /* NIST PIV Cert */
  47437. 0x70, 0x02, /* Certificate */
  47438. 0x30, 0x00,
  47439. 0x71, 0x01, 0x04, /* Cert Info */
  47440. 0xFE, 0x00, /* Error Detection */
  47441. };
  47442. WOLFSSL_ENTER("certpiv_test");
  47443. XMEMSET(&piv, 0, sizeof(piv));
  47444. /* Test with Identiv 0x0A, 0x0B and 0x0C markers */
  47445. ret = wc_ParseCertPIV(&piv, pivCertIdentiv, sizeof(pivCertIdentiv));
  47446. if (ret != 0) {
  47447. return WC_TEST_RET_ENC_EC(ret);
  47448. }
  47449. if (!piv.isIdentiv) {
  47450. return WC_TEST_RET_ENC_NC;
  47451. }
  47452. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  47453. return WC_TEST_RET_ENC_NC;
  47454. }
  47455. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  47456. return WC_TEST_RET_ENC_NC;
  47457. }
  47458. if ((piv.compression != ASN_PIV_CERT_INFO_GZIP)) {
  47459. return WC_TEST_RET_ENC_NC;
  47460. }
  47461. if (!piv.isX509) {
  47462. return WC_TEST_RET_ENC_NC;
  47463. }
  47464. if ((piv.nonce == NULL) || (piv.nonceSz != 1)) {
  47465. return WC_TEST_RET_ENC_NC;
  47466. }
  47467. if ((piv.signedNonce == NULL) || (piv.signedNonceSz != 1)) {
  47468. return WC_TEST_RET_ENC_NC;
  47469. }
  47470. XMEMSET(&piv, 0, sizeof(piv));
  47471. /* Test with NIST PIV format */
  47472. ret = wc_ParseCertPIV(&piv, pivCert, sizeof(pivCert));
  47473. if (ret != 0) {
  47474. return WC_TEST_RET_ENC_EC(ret);
  47475. }
  47476. if (piv.isIdentiv) {
  47477. return WC_TEST_RET_ENC_NC;
  47478. }
  47479. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  47480. return WC_TEST_RET_ENC_NC;
  47481. }
  47482. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  47483. return WC_TEST_RET_ENC_NC;
  47484. }
  47485. if ((piv.compression != 0)) {
  47486. return WC_TEST_RET_ENC_NC;
  47487. }
  47488. if (!piv.isX509) {
  47489. return WC_TEST_RET_ENC_NC;
  47490. }
  47491. return ret;
  47492. }
  47493. #endif /* WOLFSSL_CERT_PIV */
  47494. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  47495. static time_t time_cb(time_t* t)
  47496. {
  47497. if (t != NULL) {
  47498. *t = 99;
  47499. }
  47500. return 99;
  47501. }
  47502. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t time_test(void)
  47503. {
  47504. time_t t;
  47505. wc_test_ret_t ret;
  47506. WOLFSSL_ENTER("time_test");
  47507. ret = wc_SetTimeCb(time_cb);
  47508. if (ret != 0)
  47509. return WC_TEST_RET_ENC_EC(ret);
  47510. t = wc_Time(NULL);
  47511. if (t != 99)
  47512. return WC_TEST_RET_ENC_NC;
  47513. ret = wc_GetTime(&t, sizeof(time_t));
  47514. if (ret != 0)
  47515. return WC_TEST_RET_ENC_EC(ret);
  47516. if (t != 99)
  47517. return WC_TEST_RET_ENC_NC;
  47518. ret = wc_SetTimeCb(NULL);
  47519. if (ret != 0)
  47520. return WC_TEST_RET_ENC_EC(ret);
  47521. return 0;
  47522. }
  47523. #endif
  47524. #ifdef WOLFSSL_AES_SIV
  47525. typedef struct {
  47526. const byte key[33];
  47527. word32 keySz;
  47528. const byte nonce[49];
  47529. word32 nonceSz;
  47530. const byte assoc[81];
  47531. word32 assocSz;
  47532. const byte plaintext[83];
  47533. word32 plaintextSz;
  47534. const byte siv[AES_BLOCK_SIZE+1];
  47535. const byte ciphertext[82];
  47536. word32 ciphertextSz;
  47537. } AesSivTestVector;
  47538. #define AES_SIV_TEST_VECTORS 7
  47539. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_siv_test(void)
  47540. {
  47541. /* These test vectors come from chrony 4.1's SIV unit tests. */
  47542. WOLFSSL_SMALL_STACK_STATIC const AesSivTestVector testVectors[AES_SIV_TEST_VECTORS] = {
  47543. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  47544. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  47545. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  47546. "", 0,
  47547. "", 0,
  47548. "\x22\x3e\xb5\x94\xe0\xe0\x25\x4b\x00\x25\x8e\x21\x9a\x1c\xa4\x21",
  47549. "", 0
  47550. },
  47551. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  47552. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  47553. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  47554. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  47555. "", 0,
  47556. "\xd7\x20\x19\x89\xc6\xdb\xc6\xd6\x61\xfc\x62\xbc\x86\x5e\xee\xef",
  47557. "", 0
  47558. },
  47559. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  47560. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  47561. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  47562. "", 0,
  47563. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  47564. "\xb6\xc1\x60\xe9\xc2\xfd\x2a\xe8\xde\xc5\x36\x8b\x2a\x33\xed\xe1",
  47565. "\x14\xff\xb3\x97\x34\x5c\xcb\xe4\x4a\xa4\xde\xac\xd9\x36\x90\x46", 16
  47566. },
  47567. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  47568. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  47569. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e", 15,
  47570. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c", 15,
  47571. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4", 15,
  47572. "\x03\x8c\x41\x51\xba\x7a\x8f\x77\x6e\x56\x31\x99\x42\x0b\xc7\x03",
  47573. "\xe7\x6c\x67\xc9\xda\xb7\x0d\x5b\x44\x06\x26\x5a\xd0\xd2\x3b", 15
  47574. },
  47575. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  47576. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  47577. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  47578. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  47579. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7", 16,
  47580. "\x5c\x05\x23\x65\xf4\x57\x0a\xa0\xfb\x38\x3e\xce\x9b\x75\x85\xeb",
  47581. "\x68\x85\x19\x36\x0c\x7c\x48\x11\x40\xcb\x9b\x57\x9a\x0e\x65\x32", 16
  47582. },
  47583. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  47584. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  47585. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
  47586. "\xd5", 17,
  47587. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b"
  47588. "\xa0", 17,
  47589. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7"
  47590. "\x08", 17,
  47591. "\xaf\x58\x4b\xe7\x82\x1e\x96\x19\x29\x91\x25\xe0\xdd\x80\x3b\x49",
  47592. "\xa5\x11\xcd\xb6\x08\xf3\x76\xa0\xb6\xfa\x15\x82\xf3\x95\xe1\xeb"
  47593. "\xbd", 17
  47594. },
  47595. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  47596. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  47597. "\xb0\x5a\x1b\xc7\x56\xe7\xb6\x2c\xb4\x85\xe5\x56\xa5\x28\xc0\x6c"
  47598. "\x2f\x3b\x0b\x9d\x1a\x0c\xdf\x69\x47\xe0\xcc\xc0\x87\xaa\x5c\x09"
  47599. "\x98\x48\x8d\x6a\x8e\x1e\x05\xd7\x8b\x68\x74\x83\xb5\x1d\xf1\x2c", 48,
  47600. "\xe5\x8b\xd2\x6a\x30\xc5\xc5\x61\xcc\xbd\x7c\x27\xbf\xfe\xf9\x06"
  47601. "\x00\x5b\xd7\xfc\x11\x0b\xcf\x16\x61\xef\xac\x05\xa7\xaf\xec\x27"
  47602. "\x41\xc8\x5e\x9e\x0d\xf9\x2f\xaf\x20\x79\x17\xe5\x17\x91\x2a\x27"
  47603. "\x34\x1c\xbc\xaf\xeb\xef\x7f\x52\xe7\x1e\x4c\x2a\xca\xbd\x2b\xbe"
  47604. "\x34\xd6\xfb\x69\xd3\x3e\x49\x59\x60\xb4\x26\xc9\xb8\xce\xba", 79,
  47605. "\x6c\xe7\xcf\x7e\xab\x7b\xa0\xe1\xa7\x22\xcb\x88\xde\x5e\x42\xd2"
  47606. "\xec\x79\xe0\xa2\xcf\x5f\x0f\x6f\x6b\x89\x57\xcd\xae\x17\xd4\xc2"
  47607. "\xf3\x1b\xa2\xa8\x13\x78\x23\x2f\x83\xa8\xd4\x0c\xc0\xd2\xf3\x99"
  47608. "\xae\x81\xa1\xca\x5b\x5f\x45\xa6\x6f\x0c\x8a\xf3\xd4\x67\x40\x81"
  47609. "\x26\xe2\x01\x86\xe8\x5a\xd5\xf8\x58\x80\x9f\x56\xaa\x76\x96\xbf"
  47610. "\x31", 81,
  47611. "\x9a\x06\x33\xe0\xee\x00\x6a\x9b\xc8\x20\xd5\xe2\xc2\xed\xb5\x75",
  47612. "\xfa\x9e\x42\x2a\x31\x6b\xda\xca\xaa\x7d\x31\x8b\x84\x7a\xb8\xd7"
  47613. "\x8a\x81\x25\x64\xed\x41\x9b\xa9\x77\x10\xbd\x05\x0c\x4e\xc5\x31"
  47614. "\x0c\xa2\x86\xec\x8a\x94\xc8\x24\x23\x3c\x13\xee\xa5\x51\xc9\xdf"
  47615. "\x48\xc9\x55\xc5\x2f\x40\x73\x3f\x98\xbb\x8d\x69\x78\x46\x64\x17"
  47616. "\x8d\x49\x2f\x14\x62\xa4\x7c\x2a\x57\x38\x87\xce\xc6\x72\xd3\x5c"
  47617. "\xa1", 81
  47618. }};
  47619. int i;
  47620. byte computedCiphertext[82];
  47621. byte computedPlaintext[82];
  47622. byte siv[AES_BLOCK_SIZE];
  47623. wc_test_ret_t ret = 0;
  47624. WOLFSSL_ENTER("aes_siv_test");
  47625. for (i = 0; i < AES_SIV_TEST_VECTORS; ++i) {
  47626. ret = wc_AesSivEncrypt(testVectors[i].key, testVectors[i].keySz,
  47627. testVectors[i].assoc, testVectors[i].assocSz,
  47628. testVectors[i].nonce, testVectors[i].nonceSz,
  47629. testVectors[i].plaintext,
  47630. testVectors[i].plaintextSz, siv,
  47631. computedCiphertext);
  47632. if (ret != 0) {
  47633. return WC_TEST_RET_ENC_EC(ret);
  47634. }
  47635. ret = XMEMCMP(siv, testVectors[i].siv, AES_BLOCK_SIZE);
  47636. if (ret != 0) {
  47637. return WC_TEST_RET_ENC_NC;
  47638. }
  47639. ret = XMEMCMP(computedCiphertext, testVectors[i].ciphertext,
  47640. testVectors[i].ciphertextSz);
  47641. if (ret != 0) {
  47642. return WC_TEST_RET_ENC_NC;
  47643. }
  47644. ret = wc_AesSivDecrypt(testVectors[i].key, testVectors[i].keySz,
  47645. testVectors[i].assoc, testVectors[i].assocSz,
  47646. testVectors[i].nonce, testVectors[i].nonceSz,
  47647. computedCiphertext, testVectors[i].plaintextSz,
  47648. siv, computedPlaintext);
  47649. if (ret != 0) {
  47650. return WC_TEST_RET_ENC_EC(ret);
  47651. }
  47652. ret = XMEMCMP(computedPlaintext, testVectors[i].plaintext,
  47653. testVectors[i].plaintextSz);
  47654. if (ret != 0) {
  47655. return WC_TEST_RET_ENC_NC;
  47656. }
  47657. }
  47658. return 0;
  47659. }
  47660. #endif
  47661. #undef ERROR_OUT
  47662. static const int fiducial4 = WC_TEST_RET_LN;
  47663. /* print the fiducial line numbers assigned above, allowing confirmation of
  47664. * source code version match when in doubt.
  47665. */
  47666. static void print_fiducials(void) {
  47667. printf(" [fiducial line numbers: %d %d %d %d]\n",
  47668. fiducial1, fiducial2, fiducial3, fiducial4);
  47669. }
  47670. #else
  47671. #ifndef NO_MAIN_DRIVER
  47672. int main(void) { return 0; }
  47673. #endif
  47674. #endif /* NO_CRYPT_TEST */