1
0

test.c 1.9 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267442684426944270442714427244273442744427544276442774427844279442804428144282442834428444285442864428744288442894429044291442924429344294442954429644297442984429944300443014430244303443044430544306443074430844309443104431144312443134431444315443164431744318443194432044321443224432344324443254432644327443284432944330443314433244333443344433544336443374433844339443404434144342443434434444345443464434744348443494435044351443524435344354443554435644357443584435944360443614436244363443644436544366443674436844369443704437144372443734437444375443764437744378443794438044381443824438344384443854438644387443884438944390443914439244393443944439544396443974439844399444004440144402444034440444405444064440744408444094441044411444124441344414444154441644417444184441944420444214442244423444244442544426444274442844429444304443144432444334443444435444364443744438444394444044441444424444344444444454444644447444484444944450444514445244453444544445544456444574445844459444604446144462444634446444465444664446744468444694447044471444724447344474444754447644477444784447944480444814448244483444844448544486444874448844489444904449144492444934449444495444964449744498444994450044501445024450344504445054450644507445084450944510445114451244513445144451544516445174451844519445204452144522445234452444525445264452744528445294453044531445324453344534445354453644537445384453944540445414454244543445444454544546445474454844549445504455144552445534455444555445564455744558445594456044561445624456344564445654456644567445684456944570445714457244573445744457544576445774457844579445804458144582445834458444585445864458744588445894459044591445924459344594445954459644597445984459944600446014460244603446044460544606446074460844609446104461144612446134461444615446164461744618446194462044621446224462344624446254462644627446284462944630446314463244633446344463544636446374463844639446404464144642446434464444645446464464744648446494465044651446524465344654446554465644657446584465944660446614466244663446644466544666446674466844669446704467144672446734467444675446764467744678446794468044681446824468344684446854468644687446884468944690446914469244693446944469544696446974469844699447004470144702447034470444705447064470744708447094471044711447124471344714447154471644717447184471944720447214472244723447244472544726447274472844729447304473144732447334473444735447364473744738447394474044741447424474344744447454474644747447484474944750447514475244753447544475544756447574475844759447604476144762447634476444765447664476744768447694477044771447724477344774447754477644777447784477944780447814478244783447844478544786447874478844789447904479144792447934479444795447964479744798447994480044801448024480344804448054480644807448084480944810448114481244813448144481544816448174481844819448204482144822448234482444825448264482744828448294483044831448324483344834448354483644837448384483944840448414484244843448444484544846448474484844849448504485144852448534485444855448564485744858448594486044861448624486344864448654486644867448684486944870448714487244873448744487544876448774487844879448804488144882448834488444885448864488744888448894489044891448924489344894448954489644897448984489944900449014490244903449044490544906449074490844909449104491144912449134491444915449164491744918449194492044921449224492344924449254492644927449284492944930449314493244933449344493544936449374493844939449404494144942449434494444945449464494744948449494495044951449524495344954449554495644957449584495944960449614496244963449644496544966449674496844969449704497144972449734497444975449764497744978449794498044981449824498344984449854498644987449884498944990449914499244993449944499544996449974499844999450004500145002450034500445005450064500745008450094501045011450124501345014450154501645017450184501945020450214502245023450244502545026450274502845029450304503145032450334503445035450364503745038450394504045041450424504345044450454504645047450484504945050450514505245053450544505545056450574505845059450604506145062450634506445065450664506745068450694507045071450724507345074450754507645077450784507945080450814508245083450844508545086450874508845089450904509145092450934509445095450964509745098450994510045101451024510345104451054510645107451084510945110451114511245113451144511545116451174511845119451204512145122451234512445125451264512745128451294513045131451324513345134451354513645137451384513945140451414514245143451444514545146451474514845149451504515145152451534515445155451564515745158451594516045161451624516345164451654516645167451684516945170451714517245173451744517545176451774517845179451804518145182451834518445185451864518745188451894519045191451924519345194451954519645197451984519945200452014520245203452044520545206452074520845209452104521145212452134521445215452164521745218452194522045221452224522345224452254522645227452284522945230452314523245233452344523545236452374523845239452404524145242452434524445245452464524745248452494525045251452524525345254452554525645257452584525945260452614526245263452644526545266452674526845269452704527145272452734527445275452764527745278452794528045281452824528345284452854528645287452884528945290452914529245293452944529545296452974529845299453004530145302453034530445305453064530745308453094531045311453124531345314453154531645317453184531945320453214532245323453244532545326453274532845329453304533145332453334533445335453364533745338453394534045341453424534345344453454534645347453484534945350453514535245353453544535545356453574535845359453604536145362453634536445365453664536745368453694537045371453724537345374453754537645377453784537945380453814538245383453844538545386453874538845389453904539145392453934539445395453964539745398453994540045401454024540345404454054540645407454084540945410454114541245413454144541545416454174541845419454204542145422454234542445425454264542745428454294543045431454324543345434454354543645437454384543945440454414544245443454444544545446454474544845449454504545145452454534545445455454564545745458454594546045461454624546345464454654546645467454684546945470454714547245473454744547545476454774547845479454804548145482454834548445485454864548745488454894549045491454924549345494454954549645497454984549945500455014550245503455044550545506455074550845509455104551145512455134551445515455164551745518455194552045521455224552345524455254552645527455284552945530455314553245533455344553545536455374553845539455404554145542455434554445545455464554745548455494555045551455524555345554455554555645557455584555945560455614556245563455644556545566455674556845569455704557145572455734557445575455764557745578455794558045581455824558345584455854558645587455884558945590455914559245593455944559545596455974559845599456004560145602456034560445605456064560745608456094561045611456124561345614456154561645617456184561945620456214562245623456244562545626456274562845629456304563145632456334563445635456364563745638456394564045641456424564345644456454564645647456484564945650456514565245653456544565545656456574565845659456604566145662456634566445665456664566745668456694567045671456724567345674456754567645677456784567945680456814568245683456844568545686456874568845689456904569145692456934569445695456964569745698456994570045701457024570345704457054570645707457084570945710457114571245713457144571545716457174571845719457204572145722457234572445725457264572745728457294573045731457324573345734457354573645737457384573945740457414574245743457444574545746457474574845749457504575145752457534575445755457564575745758457594576045761457624576345764457654576645767457684576945770457714577245773457744577545776457774577845779457804578145782457834578445785457864578745788457894579045791457924579345794457954579645797457984579945800458014580245803458044580545806458074580845809458104581145812458134581445815458164581745818458194582045821458224582345824458254582645827458284582945830458314583245833458344583545836458374583845839458404584145842458434584445845458464584745848458494585045851458524585345854458554585645857458584585945860458614586245863458644586545866458674586845869458704587145872458734587445875458764587745878458794588045881458824588345884458854588645887458884588945890458914589245893458944589545896458974589845899459004590145902459034590445905459064590745908459094591045911459124591345914459154591645917459184591945920459214592245923459244592545926459274592845929459304593145932459334593445935459364593745938459394594045941459424594345944459454594645947459484594945950459514595245953459544595545956459574595845959459604596145962459634596445965459664596745968459694597045971459724597345974459754597645977459784597945980459814598245983459844598545986459874598845989459904599145992459934599445995459964599745998459994600046001460024600346004460054600646007460084600946010460114601246013460144601546016460174601846019460204602146022460234602446025460264602746028460294603046031460324603346034460354603646037460384603946040460414604246043460444604546046460474604846049460504605146052460534605446055460564605746058460594606046061460624606346064460654606646067460684606946070460714607246073460744607546076460774607846079460804608146082460834608446085460864608746088460894609046091460924609346094460954609646097460984609946100461014610246103461044610546106461074610846109461104611146112461134611446115461164611746118461194612046121461224612346124461254612646127461284612946130461314613246133461344613546136461374613846139461404614146142461434614446145461464614746148461494615046151461524615346154461554615646157461584615946160461614616246163461644616546166461674616846169461704617146172461734617446175461764617746178461794618046181461824618346184461854618646187461884618946190461914619246193461944619546196461974619846199462004620146202462034620446205462064620746208462094621046211462124621346214462154621646217462184621946220462214622246223462244622546226462274622846229462304623146232462334623446235462364623746238462394624046241462424624346244462454624646247462484624946250462514625246253462544625546256462574625846259462604626146262462634626446265462664626746268462694627046271462724627346274462754627646277462784627946280462814628246283462844628546286462874628846289462904629146292462934629446295462964629746298462994630046301463024630346304463054630646307463084630946310463114631246313463144631546316463174631846319463204632146322463234632446325463264632746328463294633046331463324633346334463354633646337463384633946340463414634246343463444634546346463474634846349463504635146352463534635446355463564635746358463594636046361463624636346364463654636646367463684636946370463714637246373463744637546376463774637846379463804638146382463834638446385463864638746388463894639046391463924639346394463954639646397463984639946400464014640246403464044640546406464074640846409464104641146412464134641446415464164641746418464194642046421464224642346424464254642646427464284642946430464314643246433464344643546436464374643846439464404644146442464434644446445464464644746448464494645046451464524645346454464554645646457464584645946460464614646246463464644646546466464674646846469464704647146472464734647446475464764647746478464794648046481464824648346484464854648646487464884648946490464914649246493464944649546496464974649846499465004650146502465034650446505465064650746508465094651046511465124651346514465154651646517465184651946520465214652246523465244652546526465274652846529465304653146532465334653446535465364653746538465394654046541465424654346544465454654646547465484654946550465514655246553465544655546556465574655846559465604656146562465634656446565465664656746568465694657046571465724657346574465754657646577465784657946580465814658246583465844658546586465874658846589465904659146592465934659446595465964659746598465994660046601466024660346604466054660646607466084660946610466114661246613466144661546616466174661846619466204662146622466234662446625466264662746628466294663046631466324663346634466354663646637466384663946640466414664246643466444664546646466474664846649466504665146652466534665446655466564665746658466594666046661466624666346664466654666646667466684666946670466714667246673466744667546676466774667846679466804668146682466834668446685466864668746688466894669046691466924669346694466954669646697466984669946700467014670246703467044670546706467074670846709467104671146712467134671446715467164671746718467194672046721467224672346724467254672646727467284672946730467314673246733467344673546736467374673846739467404674146742467434674446745467464674746748467494675046751467524675346754467554675646757467584675946760467614676246763467644676546766467674676846769467704677146772467734677446775467764677746778467794678046781467824678346784467854678646787467884678946790467914679246793467944679546796467974679846799468004680146802468034680446805468064680746808468094681046811468124681346814468154681646817468184681946820468214682246823468244682546826468274682846829468304683146832468334683446835468364683746838468394684046841468424684346844468454684646847468484684946850468514685246853468544685546856468574685846859468604686146862468634686446865468664686746868468694687046871468724687346874468754687646877468784687946880468814688246883468844688546886468874688846889468904689146892468934689446895468964689746898468994690046901469024690346904469054690646907469084690946910469114691246913469144691546916469174691846919469204692146922469234692446925469264692746928469294693046931469324693346934469354693646937469384693946940469414694246943469444694546946469474694846949469504695146952469534695446955469564695746958469594696046961469624696346964469654696646967469684696946970469714697246973469744697546976469774697846979469804698146982469834698446985469864698746988469894699046991469924699346994469954699646997469984699947000470014700247003470044700547006470074700847009470104701147012470134701447015470164701747018470194702047021470224702347024470254702647027470284702947030470314703247033470344703547036470374703847039470404704147042470434704447045470464704747048470494705047051470524705347054470554705647057470584705947060470614706247063470644706547066470674706847069470704707147072470734707447075470764707747078470794708047081470824708347084470854708647087470884708947090470914709247093470944709547096470974709847099471004710147102471034710447105471064710747108471094711047111471124711347114471154711647117471184711947120471214712247123471244712547126471274712847129471304713147132471334713447135471364713747138471394714047141471424714347144471454714647147471484714947150471514715247153471544715547156471574715847159471604716147162471634716447165471664716747168471694717047171471724717347174471754717647177471784717947180471814718247183471844718547186471874718847189471904719147192471934719447195471964719747198471994720047201472024720347204472054720647207472084720947210472114721247213472144721547216472174721847219472204722147222472234722447225472264722747228472294723047231472324723347234472354723647237472384723947240472414724247243472444724547246472474724847249472504725147252472534725447255472564725747258472594726047261472624726347264472654726647267472684726947270472714727247273472744727547276472774727847279472804728147282472834728447285472864728747288472894729047291472924729347294472954729647297472984729947300473014730247303473044730547306473074730847309473104731147312473134731447315473164731747318473194732047321473224732347324473254732647327473284732947330473314733247333473344733547336473374733847339473404734147342473434734447345473464734747348473494735047351473524735347354473554735647357473584735947360473614736247363473644736547366473674736847369473704737147372473734737447375473764737747378473794738047381473824738347384473854738647387473884738947390473914739247393473944739547396473974739847399474004740147402474034740447405474064740747408474094741047411474124741347414474154741647417474184741947420474214742247423474244742547426474274742847429474304743147432474334743447435474364743747438474394744047441474424744347444474454744647447474484744947450474514745247453474544745547456474574745847459474604746147462474634746447465474664746747468474694747047471474724747347474474754747647477474784747947480474814748247483474844748547486474874748847489474904749147492474934749447495474964749747498474994750047501475024750347504475054750647507475084750947510475114751247513475144751547516475174751847519475204752147522475234752447525475264752747528475294753047531475324753347534475354753647537475384753947540475414754247543475444754547546475474754847549475504755147552475534755447555475564755747558475594756047561475624756347564475654756647567475684756947570475714757247573475744757547576475774757847579475804758147582475834758447585475864758747588475894759047591475924759347594475954759647597475984759947600476014760247603476044760547606476074760847609476104761147612476134761447615476164761747618476194762047621476224762347624476254762647627476284762947630476314763247633476344763547636476374763847639476404764147642476434764447645476464764747648476494765047651476524765347654476554765647657476584765947660476614766247663476644766547666476674766847669476704767147672476734767447675476764767747678476794768047681476824768347684476854768647687476884768947690476914769247693476944769547696476974769847699477004770147702477034770447705477064770747708477094771047711477124771347714477154771647717477184771947720477214772247723477244772547726477274772847729477304773147732477334773447735477364773747738477394774047741477424774347744477454774647747477484774947750477514775247753477544775547756477574775847759477604776147762477634776447765477664776747768477694777047771477724777347774477754777647777477784777947780477814778247783477844778547786477874778847789477904779147792477934779447795477964779747798477994780047801478024780347804478054780647807478084780947810478114781247813478144781547816478174781847819478204782147822478234782447825478264782747828478294783047831478324783347834478354783647837478384783947840478414784247843478444784547846478474784847849478504785147852478534785447855478564785747858478594786047861478624786347864478654786647867478684786947870478714787247873478744787547876478774787847879478804788147882478834788447885478864788747888478894789047891478924789347894478954789647897478984789947900479014790247903479044790547906479074790847909479104791147912479134791447915479164791747918479194792047921479224792347924479254792647927479284792947930479314793247933479344793547936479374793847939479404794147942479434794447945479464794747948479494795047951479524795347954479554795647957479584795947960479614796247963479644796547966479674796847969479704797147972479734797447975479764797747978479794798047981479824798347984479854798647987479884798947990479914799247993479944799547996479974799847999480004800148002480034800448005480064800748008480094801048011480124801348014480154801648017480184801948020480214802248023480244802548026480274802848029480304803148032480334803448035480364803748038480394804048041480424804348044480454804648047480484804948050480514805248053480544805548056480574805848059480604806148062480634806448065480664806748068480694807048071480724807348074480754807648077480784807948080480814808248083480844808548086480874808848089480904809148092480934809448095480964809748098480994810048101481024810348104481054810648107481084810948110481114811248113481144811548116481174811848119481204812148122481234812448125481264812748128481294813048131481324813348134481354813648137481384813948140481414814248143481444814548146481474814848149481504815148152481534815448155481564815748158481594816048161481624816348164481654816648167481684816948170481714817248173481744817548176481774817848179481804818148182481834818448185481864818748188481894819048191481924819348194481954819648197481984819948200482014820248203482044820548206482074820848209482104821148212482134821448215482164821748218482194822048221482224822348224482254822648227482284822948230482314823248233482344823548236482374823848239482404824148242482434824448245482464824748248482494825048251482524825348254482554825648257482584825948260482614826248263482644826548266482674826848269482704827148272482734827448275482764827748278482794828048281482824828348284482854828648287482884828948290482914829248293482944829548296482974829848299483004830148302483034830448305483064830748308483094831048311483124831348314483154831648317483184831948320483214832248323483244832548326483274832848329483304833148332483334833448335483364833748338483394834048341483424834348344483454834648347483484834948350483514835248353483544835548356483574835848359483604836148362483634836448365483664836748368483694837048371483724837348374483754837648377483784837948380483814838248383483844838548386483874838848389483904839148392483934839448395483964839748398483994840048401484024840348404484054840648407484084840948410484114841248413484144841548416484174841848419484204842148422484234842448425484264842748428484294843048431484324843348434484354843648437484384843948440484414844248443484444844548446484474844848449484504845148452484534845448455484564845748458484594846048461484624846348464484654846648467484684846948470484714847248473484744847548476484774847848479484804848148482484834848448485484864848748488484894849048491484924849348494484954849648497484984849948500485014850248503485044850548506485074850848509485104851148512485134851448515485164851748518485194852048521485224852348524485254852648527485284852948530485314853248533485344853548536485374853848539485404854148542485434854448545485464854748548485494855048551485524855348554485554855648557485584855948560485614856248563485644856548566485674856848569485704857148572485734857448575485764857748578485794858048581485824858348584485854858648587485884858948590485914859248593485944859548596485974859848599486004860148602486034860448605486064860748608486094861048611486124861348614486154861648617486184861948620486214862248623486244862548626486274862848629486304863148632486334863448635486364863748638486394864048641486424864348644486454864648647486484864948650486514865248653486544865548656486574865848659486604866148662486634866448665486664866748668486694867048671486724867348674486754867648677486784867948680486814868248683486844868548686486874868848689486904869148692486934869448695486964869748698486994870048701487024870348704487054870648707487084870948710487114871248713487144871548716487174871848719487204872148722487234872448725487264872748728487294873048731487324873348734487354873648737487384873948740487414874248743487444874548746487474874848749487504875148752487534875448755487564875748758487594876048761487624876348764487654876648767487684876948770487714877248773487744877548776487774877848779487804878148782487834878448785487864878748788487894879048791487924879348794487954879648797487984879948800488014880248803488044880548806488074880848809488104881148812488134881448815488164881748818488194882048821488224882348824488254882648827488284882948830488314883248833488344883548836488374883848839488404884148842488434884448845488464884748848488494885048851488524885348854488554885648857488584885948860488614886248863488644886548866488674886848869488704887148872488734887448875488764887748878488794888048881488824888348884488854888648887488884888948890488914889248893488944889548896488974889848899489004890148902489034890448905489064890748908489094891048911489124891348914489154891648917489184891948920489214892248923489244892548926489274892848929489304893148932489334893448935489364893748938489394894048941489424894348944489454894648947489484894948950489514895248953489544895548956489574895848959489604896148962489634896448965489664896748968489694897048971489724897348974489754897648977489784897948980489814898248983489844898548986489874898848989489904899148992489934899448995489964899748998489994900049001490024900349004490054900649007490084900949010490114901249013490144901549016490174901849019490204902149022490234902449025490264902749028490294903049031490324903349034490354903649037490384903949040490414904249043490444904549046490474904849049490504905149052490534905449055490564905749058490594906049061490624906349064490654906649067490684906949070490714907249073490744907549076490774907849079490804908149082490834908449085490864908749088490894909049091490924909349094490954909649097490984909949100491014910249103491044910549106491074910849109491104911149112491134911449115491164911749118491194912049121491224912349124491254912649127491284912949130491314913249133491344913549136491374913849139491404914149142491434914449145491464914749148491494915049151491524915349154491554915649157491584915949160491614916249163491644916549166491674916849169491704917149172491734917449175491764917749178491794918049181491824918349184491854918649187491884918949190491914919249193491944919549196491974919849199492004920149202492034920449205492064920749208492094921049211492124921349214492154921649217492184921949220492214922249223492244922549226492274922849229492304923149232492334923449235492364923749238492394924049241492424924349244492454924649247492484924949250492514925249253492544925549256492574925849259492604926149262492634926449265492664926749268492694927049271492724927349274492754927649277492784927949280492814928249283492844928549286492874928849289492904929149292492934929449295492964929749298492994930049301493024930349304493054930649307493084930949310493114931249313493144931549316493174931849319493204932149322493234932449325493264932749328493294933049331493324933349334493354933649337493384933949340493414934249343493444934549346493474934849349493504935149352493534935449355493564935749358493594936049361493624936349364493654936649367493684936949370493714937249373493744937549376493774937849379493804938149382493834938449385493864938749388493894939049391493924939349394493954939649397493984939949400494014940249403494044940549406494074940849409494104941149412494134941449415494164941749418494194942049421494224942349424494254942649427494284942949430494314943249433494344943549436494374943849439494404944149442494434944449445494464944749448494494945049451494524945349454494554945649457494584945949460494614946249463494644946549466494674946849469494704947149472494734947449475494764947749478494794948049481494824948349484494854948649487494884948949490494914949249493494944949549496494974949849499495004950149502495034950449505495064950749508495094951049511495124951349514495154951649517495184951949520495214952249523495244952549526495274952849529495304953149532495334953449535495364953749538495394954049541495424954349544495454954649547495484954949550495514955249553495544955549556495574955849559495604956149562495634956449565495664956749568495694957049571495724957349574495754957649577495784957949580495814958249583495844958549586495874958849589495904959149592495934959449595495964959749598495994960049601496024960349604496054960649607496084960949610496114961249613496144961549616496174961849619496204962149622496234962449625496264962749628496294963049631496324963349634496354963649637496384963949640496414964249643496444964549646496474964849649496504965149652496534965449655496564965749658496594966049661496624966349664496654966649667496684966949670496714967249673496744967549676496774967849679496804968149682496834968449685496864968749688496894969049691496924969349694496954969649697496984969949700497014970249703497044970549706497074970849709497104971149712497134971449715497164971749718497194972049721497224972349724497254972649727497284972949730497314973249733497344973549736497374973849739497404974149742497434974449745497464974749748497494975049751497524975349754497554975649757497584975949760497614976249763497644976549766497674976849769497704977149772497734977449775497764977749778497794978049781497824978349784497854978649787497884978949790497914979249793497944979549796497974979849799498004980149802498034980449805498064980749808498094981049811498124981349814498154981649817498184981949820498214982249823498244982549826498274982849829498304983149832498334983449835498364983749838498394984049841498424984349844498454984649847498484984949850498514985249853498544985549856498574985849859498604986149862498634986449865498664986749868498694987049871498724987349874498754987649877498784987949880498814988249883498844988549886498874988849889498904989149892498934989449895498964989749898498994990049901499024990349904499054990649907499084990949910499114991249913499144991549916499174991849919499204992149922499234992449925499264992749928499294993049931499324993349934499354993649937499384993949940499414994249943499444994549946499474994849949499504995149952499534995449955499564995749958499594996049961499624996349964499654996649967499684996949970499714997249973499744997549976499774997849979499804998149982499834998449985499864998749988499894999049991499924999349994499954999649997499984999950000500015000250003500045000550006500075000850009500105001150012500135001450015500165001750018500195002050021500225002350024500255002650027500285002950030500315003250033500345003550036500375003850039500405004150042500435004450045500465004750048500495005050051500525005350054500555005650057500585005950060500615006250063500645006550066500675006850069500705007150072500735007450075500765007750078500795008050081500825008350084500855008650087500885008950090500915009250093500945009550096500975009850099501005010150102501035010450105501065010750108501095011050111501125011350114501155011650117501185011950120501215012250123501245012550126501275012850129501305013150132501335013450135501365013750138501395014050141501425014350144501455014650147501485014950150501515015250153501545015550156501575015850159501605016150162501635016450165501665016750168501695017050171501725017350174501755017650177501785017950180501815018250183501845018550186501875018850189501905019150192501935019450195501965019750198501995020050201502025020350204502055020650207502085020950210502115021250213502145021550216502175021850219502205022150222502235022450225502265022750228502295023050231502325023350234502355023650237502385023950240502415024250243502445024550246502475024850249502505025150252502535025450255502565025750258502595026050261502625026350264502655026650267502685026950270502715027250273502745027550276502775027850279502805028150282502835028450285502865028750288502895029050291502925029350294502955029650297502985029950300503015030250303503045030550306503075030850309503105031150312503135031450315503165031750318503195032050321503225032350324503255032650327503285032950330503315033250333503345033550336503375033850339503405034150342503435034450345503465034750348503495035050351503525035350354503555035650357503585035950360503615036250363503645036550366503675036850369503705037150372503735037450375503765037750378503795038050381503825038350384503855038650387503885038950390503915039250393503945039550396503975039850399504005040150402504035040450405504065040750408504095041050411504125041350414504155041650417504185041950420504215042250423504245042550426504275042850429504305043150432504335043450435504365043750438504395044050441504425044350444504455044650447504485044950450504515045250453504545045550456504575045850459504605046150462504635046450465504665046750468504695047050471504725047350474504755047650477504785047950480504815048250483504845048550486504875048850489504905049150492504935049450495504965049750498504995050050501505025050350504505055050650507505085050950510505115051250513505145051550516505175051850519505205052150522505235052450525505265052750528505295053050531505325053350534505355053650537505385053950540505415054250543505445054550546505475054850549505505055150552505535055450555505565055750558505595056050561505625056350564505655056650567505685056950570505715057250573505745057550576505775057850579505805058150582505835058450585505865058750588505895059050591505925059350594505955059650597505985059950600506015060250603506045060550606506075060850609506105061150612506135061450615506165061750618506195062050621506225062350624506255062650627506285062950630506315063250633506345063550636506375063850639506405064150642506435064450645506465064750648506495065050651506525065350654506555065650657506585065950660506615066250663506645066550666506675066850669506705067150672506735067450675506765067750678506795068050681506825068350684506855068650687506885068950690506915069250693506945069550696506975069850699507005070150702507035070450705507065070750708507095071050711507125071350714507155071650717507185071950720507215072250723507245072550726507275072850729507305073150732507335073450735507365073750738507395074050741507425074350744507455074650747507485074950750507515075250753507545075550756507575075850759507605076150762507635076450765507665076750768507695077050771507725077350774507755077650777507785077950780507815078250783507845078550786507875078850789507905079150792507935079450795507965079750798507995080050801508025080350804508055080650807508085080950810508115081250813508145081550816508175081850819508205082150822508235082450825508265082750828508295083050831508325083350834508355083650837508385083950840508415084250843508445084550846508475084850849508505085150852508535085450855508565085750858508595086050861508625086350864508655086650867508685086950870508715087250873508745087550876508775087850879508805088150882508835088450885508865088750888508895089050891508925089350894508955089650897508985089950900509015090250903509045090550906509075090850909509105091150912509135091450915509165091750918509195092050921509225092350924509255092650927509285092950930509315093250933509345093550936509375093850939509405094150942509435094450945509465094750948509495095050951509525095350954509555095650957509585095950960509615096250963509645096550966509675096850969509705097150972509735097450975509765097750978509795098050981509825098350984509855098650987509885098950990509915099250993509945099550996509975099850999510005100151002510035100451005510065100751008510095101051011510125101351014510155101651017510185101951020510215102251023510245102551026510275102851029510305103151032510335103451035510365103751038510395104051041510425104351044510455104651047510485104951050510515105251053510545105551056510575105851059510605106151062510635106451065510665106751068510695107051071510725107351074510755107651077510785107951080510815108251083510845108551086510875108851089510905109151092510935109451095510965109751098510995110051101511025110351104511055110651107511085110951110511115111251113511145111551116511175111851119511205112151122511235112451125511265112751128511295113051131511325113351134511355113651137511385113951140511415114251143511445114551146511475114851149511505115151152511535115451155511565115751158511595116051161511625116351164511655116651167511685116951170511715117251173511745117551176511775117851179511805118151182511835118451185511865118751188511895119051191511925119351194511955119651197511985119951200512015120251203512045120551206512075120851209512105121151212512135121451215512165121751218512195122051221512225122351224512255122651227512285122951230512315123251233512345123551236512375123851239512405124151242512435124451245512465124751248512495125051251512525125351254512555125651257512585125951260512615126251263512645126551266512675126851269512705127151272512735127451275512765127751278512795128051281512825128351284512855128651287512885128951290512915129251293512945129551296512975129851299513005130151302513035130451305513065130751308513095131051311513125131351314513155131651317513185131951320513215132251323513245132551326513275132851329513305133151332513335133451335513365133751338513395134051341513425134351344513455134651347513485134951350513515135251353513545135551356513575135851359513605136151362513635136451365513665136751368513695137051371513725137351374513755137651377513785137951380513815138251383513845138551386513875138851389513905139151392513935139451395513965139751398513995140051401514025140351404514055140651407514085140951410514115141251413514145141551416514175141851419514205142151422514235142451425514265142751428514295143051431514325143351434514355143651437514385143951440514415144251443514445144551446514475144851449514505145151452514535145451455514565145751458514595146051461514625146351464514655146651467514685146951470514715147251473514745147551476514775147851479514805148151482514835148451485514865148751488514895149051491514925149351494514955149651497514985149951500515015150251503515045150551506515075150851509515105151151512515135151451515515165151751518515195152051521515225152351524515255152651527515285152951530515315153251533515345153551536515375153851539515405154151542515435154451545515465154751548515495155051551515525155351554515555155651557515585155951560515615156251563515645156551566515675156851569515705157151572515735157451575515765157751578515795158051581515825158351584515855158651587515885158951590515915159251593515945159551596515975159851599516005160151602516035160451605516065160751608516095161051611516125161351614516155161651617516185161951620516215162251623516245162551626516275162851629516305163151632516335163451635516365163751638516395164051641516425164351644516455164651647516485164951650516515165251653516545165551656516575165851659516605166151662516635166451665516665166751668516695167051671516725167351674516755167651677516785167951680516815168251683516845168551686516875168851689516905169151692516935169451695516965169751698516995170051701517025170351704517055170651707517085170951710517115171251713517145171551716517175171851719517205172151722517235172451725517265172751728517295173051731517325173351734517355173651737517385173951740517415174251743517445174551746517475174851749517505175151752517535175451755517565175751758517595176051761517625176351764517655176651767517685176951770517715177251773517745177551776517775177851779517805178151782517835178451785517865178751788517895179051791517925179351794517955179651797517985179951800518015180251803518045180551806518075180851809518105181151812518135181451815518165181751818518195182051821518225182351824518255182651827518285182951830518315183251833518345183551836518375183851839518405184151842518435184451845518465184751848518495185051851518525185351854518555185651857518585185951860518615186251863518645186551866518675186851869518705187151872518735187451875518765187751878518795188051881518825188351884518855188651887518885188951890518915189251893518945189551896518975189851899519005190151902519035190451905519065190751908519095191051911519125191351914519155191651917519185191951920519215192251923519245192551926519275192851929519305193151932519335193451935519365193751938519395194051941519425194351944519455194651947519485194951950519515195251953519545195551956519575195851959519605196151962519635196451965519665196751968519695197051971519725197351974519755197651977519785197951980519815198251983519845198551986519875198851989519905199151992519935199451995519965199751998519995200052001520025200352004520055200652007520085200952010520115201252013520145201552016520175201852019520205202152022520235202452025520265202752028520295203052031520325203352034520355203652037520385203952040520415204252043520445204552046520475204852049520505205152052520535205452055520565205752058520595206052061520625206352064520655206652067520685206952070520715207252073520745207552076520775207852079520805208152082520835208452085520865208752088520895209052091520925209352094520955209652097520985209952100521015210252103521045210552106521075210852109521105211152112521135211452115521165211752118521195212052121521225212352124521255212652127521285212952130521315213252133521345213552136521375213852139521405214152142521435214452145521465214752148521495215052151521525215352154521555215652157521585215952160521615216252163521645216552166521675216852169521705217152172521735217452175521765217752178521795218052181521825218352184521855218652187521885218952190521915219252193521945219552196521975219852199522005220152202522035220452205522065220752208522095221052211522125221352214522155221652217522185221952220522215222252223522245222552226522275222852229522305223152232522335223452235522365223752238522395224052241522425224352244522455224652247522485224952250522515225252253522545225552256522575225852259522605226152262522635226452265522665226752268522695227052271522725227352274522755227652277522785227952280522815228252283522845228552286522875228852289522905229152292522935229452295522965229752298522995230052301523025230352304523055230652307523085230952310523115231252313523145231552316523175231852319523205232152322523235232452325523265232752328523295233052331523325233352334523355233652337523385233952340523415234252343523445234552346523475234852349523505235152352523535235452355523565235752358523595236052361523625236352364523655236652367523685236952370523715237252373523745237552376523775237852379523805238152382523835238452385523865238752388523895239052391523925239352394523955239652397523985239952400524015240252403524045240552406524075240852409524105241152412524135241452415524165241752418524195242052421524225242352424524255242652427524285242952430524315243252433524345243552436524375243852439524405244152442524435244452445524465244752448524495245052451524525245352454524555245652457524585245952460524615246252463524645246552466524675246852469524705247152472524735247452475524765247752478524795248052481524825248352484524855248652487524885248952490524915249252493524945249552496524975249852499525005250152502525035250452505525065250752508525095251052511525125251352514525155251652517525185251952520525215252252523525245252552526525275252852529525305253152532525335253452535525365253752538525395254052541525425254352544525455254652547525485254952550525515255252553525545255552556525575255852559525605256152562525635256452565525665256752568525695257052571525725257352574525755257652577525785257952580525815258252583525845258552586525875258852589525905259152592525935259452595525965259752598525995260052601526025260352604526055260652607526085260952610526115261252613526145261552616526175261852619526205262152622526235262452625526265262752628526295263052631526325263352634526355263652637526385263952640526415264252643526445264552646526475264852649526505265152652526535265452655526565265752658526595266052661526625266352664526655266652667526685266952670526715267252673526745267552676526775267852679526805268152682526835268452685526865268752688526895269052691526925269352694526955269652697526985269952700527015270252703527045270552706527075270852709527105271152712527135271452715527165271752718527195272052721527225272352724527255272652727527285272952730527315273252733527345273552736527375273852739527405274152742527435274452745527465274752748527495275052751527525275352754527555275652757527585275952760527615276252763527645276552766527675276852769527705277152772527735277452775527765277752778527795278052781527825278352784527855278652787527885278952790527915279252793527945279552796527975279852799528005280152802528035280452805528065280752808528095281052811528125281352814528155281652817528185281952820528215282252823528245282552826528275282852829528305283152832528335283452835528365283752838528395284052841528425284352844528455284652847528485284952850528515285252853528545285552856528575285852859528605286152862528635286452865528665286752868528695287052871528725287352874528755287652877528785287952880528815288252883528845288552886528875288852889528905289152892528935289452895528965289752898528995290052901529025290352904529055290652907529085290952910529115291252913529145291552916529175291852919529205292152922529235292452925529265292752928529295293052931529325293352934529355293652937529385293952940529415294252943529445294552946529475294852949529505295152952529535295452955529565295752958529595296052961529625296352964529655296652967529685296952970529715297252973529745297552976529775297852979529805298152982529835298452985529865298752988529895299052991529925299352994529955299652997529985299953000530015300253003530045300553006530075300853009530105301153012530135301453015530165301753018530195302053021530225302353024530255302653027530285302953030530315303253033530345303553036530375303853039530405304153042530435304453045530465304753048530495305053051530525305353054530555305653057530585305953060530615306253063530645306553066530675306853069530705307153072530735307453075530765307753078530795308053081530825308353084530855308653087530885308953090530915309253093530945309553096530975309853099531005310153102531035310453105531065310753108531095311053111531125311353114531155311653117531185311953120531215312253123531245312553126531275312853129531305313153132531335313453135531365313753138531395314053141531425314353144531455314653147531485314953150531515315253153531545315553156531575315853159531605316153162531635316453165531665316753168531695317053171531725317353174531755317653177531785317953180531815318253183531845318553186531875318853189531905319153192531935319453195531965319753198531995320053201532025320353204532055320653207532085320953210532115321253213532145321553216532175321853219532205322153222532235322453225532265322753228532295323053231532325323353234532355323653237532385323953240532415324253243532445324553246532475324853249532505325153252532535325453255532565325753258532595326053261532625326353264532655326653267532685326953270532715327253273532745327553276532775327853279532805328153282532835328453285532865328753288532895329053291532925329353294532955329653297532985329953300533015330253303533045330553306533075330853309533105331153312533135331453315533165331753318533195332053321533225332353324533255332653327533285332953330533315333253333533345333553336533375333853339533405334153342533435334453345533465334753348533495335053351533525335353354533555335653357533585335953360533615336253363533645336553366533675336853369533705337153372533735337453375533765337753378533795338053381533825338353384533855338653387533885338953390533915339253393533945339553396533975339853399534005340153402534035340453405534065340753408534095341053411534125341353414534155341653417534185341953420534215342253423534245342553426534275342853429534305343153432534335343453435534365343753438534395344053441534425344353444534455344653447534485344953450534515345253453534545345553456534575345853459534605346153462534635346453465534665346753468534695347053471534725347353474534755347653477534785347953480534815348253483534845348553486534875348853489534905349153492534935349453495534965349753498534995350053501535025350353504535055350653507535085350953510535115351253513535145351553516535175351853519535205352153522535235352453525535265352753528535295353053531535325353353534535355353653537535385353953540535415354253543535445354553546535475354853549535505355153552535535355453555535565355753558535595356053561535625356353564535655356653567535685356953570535715357253573535745357553576535775357853579535805358153582535835358453585535865358753588535895359053591535925359353594535955359653597535985359953600536015360253603536045360553606536075360853609536105361153612536135361453615536165361753618536195362053621536225362353624536255362653627536285362953630536315363253633536345363553636536375363853639536405364153642536435364453645536465364753648536495365053651536525365353654536555365653657536585365953660536615366253663536645366553666536675366853669536705367153672536735367453675536765367753678536795368053681536825368353684536855368653687536885368953690536915369253693536945369553696536975369853699537005370153702537035370453705537065370753708537095371053711537125371353714537155371653717537185371953720537215372253723537245372553726537275372853729537305373153732537335373453735537365373753738537395374053741537425374353744537455374653747537485374953750537515375253753537545375553756537575375853759537605376153762537635376453765537665376753768537695377053771537725377353774537755377653777537785377953780537815378253783537845378553786537875378853789537905379153792537935379453795537965379753798537995380053801538025380353804538055380653807538085380953810538115381253813538145381553816538175381853819538205382153822538235382453825538265382753828538295383053831538325383353834538355383653837538385383953840538415384253843538445384553846538475384853849538505385153852538535385453855538565385753858538595386053861538625386353864538655386653867538685386953870538715387253873538745387553876538775387853879538805388153882538835388453885538865388753888538895389053891538925389353894538955389653897538985389953900539015390253903539045390553906539075390853909539105391153912539135391453915539165391753918539195392053921539225392353924539255392653927539285392953930539315393253933539345393553936539375393853939539405394153942539435394453945539465394753948539495395053951539525395353954539555395653957539585395953960539615396253963539645396553966539675396853969539705397153972539735397453975539765397753978539795398053981539825398353984539855398653987539885398953990539915399253993539945399553996539975399853999540005400154002540035400454005540065400754008540095401054011540125401354014540155401654017540185401954020540215402254023540245402554026540275402854029540305403154032540335403454035540365403754038540395404054041540425404354044540455404654047540485404954050540515405254053540545405554056540575405854059540605406154062540635406454065540665406754068540695407054071540725407354074540755407654077540785407954080540815408254083540845408554086540875408854089540905409154092540935409454095540965409754098540995410054101541025410354104541055410654107541085410954110541115411254113541145411554116541175411854119541205412154122541235412454125541265412754128541295413054131541325413354134541355413654137541385413954140541415414254143541445414554146541475414854149541505415154152541535415454155541565415754158541595416054161541625416354164541655416654167541685416954170541715417254173541745417554176541775417854179541805418154182541835418454185541865418754188541895419054191541925419354194541955419654197541985419954200542015420254203542045420554206542075420854209542105421154212542135421454215542165421754218542195422054221542225422354224542255422654227542285422954230542315423254233542345423554236542375423854239542405424154242542435424454245542465424754248542495425054251542525425354254542555425654257542585425954260542615426254263542645426554266542675426854269542705427154272542735427454275542765427754278542795428054281542825428354284542855428654287542885428954290542915429254293542945429554296542975429854299543005430154302543035430454305543065430754308543095431054311543125431354314543155431654317543185431954320543215432254323543245432554326543275432854329543305433154332543335433454335543365433754338543395434054341543425434354344543455434654347543485434954350543515435254353543545435554356543575435854359543605436154362543635436454365543665436754368543695437054371543725437354374543755437654377543785437954380543815438254383543845438554386543875438854389543905439154392543935439454395543965439754398543995440054401544025440354404544055440654407544085440954410544115441254413544145441554416544175441854419544205442154422544235442454425544265442754428544295443054431544325443354434544355443654437544385443954440544415444254443544445444554446544475444854449544505445154452544535445454455544565445754458544595446054461544625446354464544655446654467544685446954470544715447254473544745447554476544775447854479544805448154482544835448454485544865448754488544895449054491544925449354494544955449654497544985449954500545015450254503545045450554506545075450854509545105451154512545135451454515545165451754518545195452054521545225452354524545255452654527545285452954530545315453254533545345453554536545375453854539545405454154542545435454454545545465454754548545495455054551545525455354554545555455654557545585455954560545615456254563545645456554566545675456854569545705457154572545735457454575545765457754578545795458054581545825458354584545855458654587545885458954590545915459254593545945459554596545975459854599546005460154602546035460454605546065460754608546095461054611546125461354614546155461654617546185461954620546215462254623546245462554626546275462854629546305463154632546335463454635546365463754638546395464054641546425464354644546455464654647546485464954650546515465254653546545465554656546575465854659546605466154662546635466454665546665466754668546695467054671546725467354674546755467654677546785467954680546815468254683546845468554686546875468854689546905469154692546935469454695546965469754698546995470054701547025470354704547055470654707547085470954710547115471254713547145471554716547175471854719547205472154722547235472454725547265472754728547295473054731547325473354734547355473654737547385473954740547415474254743547445474554746547475474854749547505475154752547535475454755547565475754758547595476054761547625476354764547655476654767547685476954770547715477254773547745477554776547775477854779547805478154782547835478454785547865478754788547895479054791547925479354794547955479654797547985479954800548015480254803548045480554806548075480854809548105481154812548135481454815548165481754818548195482054821548225482354824548255482654827548285482954830548315483254833548345483554836548375483854839548405484154842548435484454845548465484754848548495485054851548525485354854548555485654857548585485954860548615486254863548645486554866548675486854869548705487154872548735487454875548765487754878548795488054881548825488354884548855488654887548885488954890548915489254893548945489554896548975489854899549005490154902549035490454905549065490754908549095491054911549125491354914549155491654917549185491954920549215492254923549245492554926549275492854929549305493154932549335493454935549365493754938549395494054941549425494354944549455494654947549485494954950549515495254953549545495554956549575495854959549605496154962549635496454965549665496754968549695497054971549725497354974549755497654977549785497954980549815498254983549845498554986549875498854989549905499154992549935499454995549965499754998549995500055001550025500355004550055500655007550085500955010550115501255013550145501555016550175501855019550205502155022550235502455025550265502755028550295503055031550325503355034550355503655037550385503955040550415504255043550445504555046550475504855049550505505155052550535505455055550565505755058550595506055061550625506355064550655506655067550685506955070550715507255073550745507555076550775507855079550805508155082550835508455085550865508755088550895509055091550925509355094550955509655097550985509955100551015510255103551045510555106551075510855109551105511155112551135511455115551165511755118551195512055121551225512355124551255512655127551285512955130551315513255133551345513555136551375513855139551405514155142551435514455145551465514755148551495515055151551525515355154551555515655157551585515955160551615516255163551645516555166551675516855169551705517155172551735517455175551765517755178551795518055181551825518355184551855518655187551885518955190551915519255193551945519555196551975519855199552005520155202552035520455205552065520755208552095521055211552125521355214552155521655217552185521955220552215522255223552245522555226552275522855229552305523155232552335523455235552365523755238552395524055241552425524355244552455524655247552485524955250552515525255253552545525555256552575525855259552605526155262552635526455265552665526755268552695527055271552725527355274552755527655277552785527955280552815528255283552845528555286552875528855289552905529155292552935529455295552965529755298552995530055301553025530355304553055530655307553085530955310553115531255313553145531555316553175531855319553205532155322553235532455325553265532755328553295533055331553325533355334553355533655337553385533955340553415534255343553445534555346553475534855349553505535155352553535535455355553565535755358553595536055361553625536355364553655536655367553685536955370553715537255373553745537555376553775537855379553805538155382553835538455385553865538755388553895539055391553925539355394553955539655397553985539955400554015540255403554045540555406
  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. #ifdef HAVE_DILITHIUM
  299. #include <wolfssl/wolfcrypt/dilithium.h>
  300. #endif
  301. #if defined(WOLFSSL_HAVE_XMSS)
  302. #include <wolfssl/wolfcrypt/xmss.h>
  303. #ifdef HAVE_LIBXMSS
  304. #include <wolfssl/wolfcrypt/ext_xmss.h>
  305. #else
  306. #include <wolfssl/wolfcrypt/wc_xmss.h>
  307. #endif
  308. #endif
  309. #if defined(WOLFSSL_HAVE_LMS)
  310. #include <wolfssl/wolfcrypt/lms.h>
  311. #ifdef HAVE_LIBLMS
  312. #include <wolfssl/wolfcrypt/ext_lms.h>
  313. #else
  314. #include <wolfssl/wolfcrypt/wc_lms.h>
  315. #endif
  316. #endif
  317. #ifdef WOLFCRYPT_HAVE_ECCSI
  318. #include <wolfssl/wolfcrypt/eccsi.h>
  319. #endif
  320. #ifdef WOLFCRYPT_HAVE_SAKKE
  321. #include <wolfssl/wolfcrypt/sakke.h>
  322. #endif
  323. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  324. #include <wolfssl/wolfcrypt/blake2.h>
  325. #endif
  326. #ifdef WOLFSSL_SHA3
  327. #include <wolfssl/wolfcrypt/sha3.h>
  328. #endif
  329. #ifdef WOLFSSL_SM3
  330. #include <wolfssl/wolfcrypt/sm3.h>
  331. #endif
  332. #ifdef WOLFSSL_SM4
  333. #include <wolfssl/wolfcrypt/sm4.h>
  334. #endif
  335. #ifdef HAVE_LIBZ
  336. #include <wolfssl/wolfcrypt/compress.h>
  337. #endif
  338. #ifdef HAVE_PKCS7
  339. #include <wolfssl/wolfcrypt/pkcs7.h>
  340. #endif
  341. #ifdef HAVE_PKCS12
  342. #include <wolfssl/wolfcrypt/pkcs12.h>
  343. #endif
  344. #ifdef HAVE_FIPS
  345. #include <wolfssl/wolfcrypt/fips_test.h>
  346. #endif
  347. #ifdef HAVE_SELFTEST
  348. #include <wolfssl/wolfcrypt/selftest.h>
  349. #endif
  350. #ifdef WOLFSSL_ASYNC_CRYPT
  351. #include <wolfssl/wolfcrypt/async.h>
  352. #endif
  353. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  354. #include <wolfssl/wolfcrypt/logging.h>
  355. #endif
  356. #ifdef WOLFSSL_CAAM
  357. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  358. #endif
  359. #ifdef WOLF_CRYPTO_CB
  360. #include <wolfssl/wolfcrypt/cryptocb.h>
  361. #ifdef HAVE_INTEL_QA_SYNC
  362. #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
  363. #endif
  364. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  365. #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
  366. #endif
  367. #ifdef HAVE_RENESAS_SYNC
  368. #include <wolfssl/wolfcrypt/port/renesas/renesas_sync.h>
  369. #endif
  370. #endif
  371. #ifdef _MSC_VER
  372. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  373. #pragma warning(disable: 4996)
  374. #endif
  375. #ifdef OPENSSL_EXTRA
  376. #ifndef WOLFCRYPT_ONLY
  377. #include <wolfssl/openssl/evp.h>
  378. #include <wolfssl/openssl/hmac.h>
  379. #endif
  380. #include <wolfssl/openssl/rand.h>
  381. #include <wolfssl/openssl/aes.h>
  382. #include <wolfssl/openssl/des.h>
  383. #endif
  384. #if defined(NO_FILESYSTEM) || defined(WC_NO_RNG)
  385. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  386. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  387. #define USE_CERT_BUFFERS_2048
  388. #endif
  389. #if !defined(USE_CERT_BUFFERS_256)
  390. #define USE_CERT_BUFFERS_256
  391. #endif
  392. #endif
  393. #if defined(WOLFSSL_CERT_GEN) && (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  394. #define ENABLE_ECC384_CERT_GEN_TEST
  395. #endif
  396. #include <wolfssl/certs_test.h>
  397. #ifdef DEVKITPRO
  398. #include <wiiuse/wpad.h>
  399. #endif
  400. #ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  401. /* FIPS build has replaced ecc.h. */
  402. #define wc_ecc_key_get_priv(key) (&((key)->k))
  403. #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  404. #endif
  405. #ifdef WOLFSSL_STATIC_MEMORY
  406. static WOLFSSL_HEAP_HINT* HEAP_HINT;
  407. #else
  408. #define HEAP_HINT NULL
  409. #endif /* WOLFSSL_STATIC_MEMORY */
  410. /* these cases do not have intermediate hashing support */
  411. #if (defined(WOLFSSL_AFALG_XILINX_SHA3) && !defined(WOLFSSL_AFALG_HASH_KEEP)) \
  412. && !defined(WOLFSSL_XILINX_CRYPT) || defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  413. #define NO_INTM_HASH_TEST
  414. #endif
  415. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  416. defined(WOLFSSL_RENESAS_SCEPROTECT_CRYPTONLY) || \
  417. defined(WOLFSSL_SECO_CAAM)
  418. #define HASH_SIZE_LIMIT
  419. #endif
  420. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  421. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  422. static void initDefaultName(void);
  423. #endif
  424. /* for async devices */
  425. #ifdef WOLFSSL_CAAM_DEVID
  426. static int devId = WOLFSSL_CAAM_DEVID;
  427. #else
  428. #ifdef WC_USE_DEVID
  429. static int devId = WC_USE_DEVID;
  430. #else
  431. static int devId = INVALID_DEVID;
  432. #endif
  433. #endif
  434. #ifdef HAVE_WNR
  435. const char* wnrConfigFile = "wnr-example.conf";
  436. #endif
  437. #define TEST_STRING "Everyone gets Friday off."
  438. #define TEST_STRING_SZ 25
  439. typedef struct testVector {
  440. const char* input;
  441. const char* output;
  442. size_t inLen;
  443. size_t outLen;
  444. } testVector;
  445. #ifndef WOLFSSL_TEST_SUBROUTINE
  446. #define WOLFSSL_TEST_SUBROUTINE
  447. #endif
  448. PRAGMA_GCC("GCC diagnostic ignored \"-Wunused-function\"")
  449. PRAGMA_CLANG("clang diagnostic ignored \"-Wunused-function\"")
  450. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t error_test(void);
  451. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base64_test(void);
  452. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base16_test(void);
  453. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t asn_test(void);
  454. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md2_test(void);
  455. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md5_test(void);
  456. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md4_test(void);
  457. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha_test(void);
  458. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha224_test(void);
  459. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha256_test(void);
  460. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_test(void);
  461. #if !defined(WOLFSSL_NOSHA512_224) && \
  462. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  463. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_224_test(void);
  464. #endif
  465. #if !defined(WOLFSSL_NOSHA512_256) && \
  466. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  467. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_256_test(void);
  468. #endif
  469. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha384_test(void);
  470. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha3_test(void);
  471. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake128_test(void);
  472. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake256_test(void);
  473. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm3_test(void);
  474. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hash_test(void);
  475. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_md5_test(void);
  476. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha_test(void);
  477. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha224_test(void);
  478. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha256_test(void);
  479. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha384_test(void);
  480. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha512_test(void);
  481. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha3_test(void);
  482. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  483. #if defined(WOLFSSL_AFALG_XILINX) || defined(WOLFSSL_AFALG_XILINX_AES) || \
  484. defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_AFALG_HASH_KEEP) || \
  485. defined(WOLFSSL_AFALG_XILINX_RSA)
  486. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  487. static wc_test_ret_t hkdf_test(void);
  488. #else
  489. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hkdf_test(void);
  490. #endif
  491. #endif /* HAVE_HKDF && ! NO_HMAC */
  492. #ifdef WOLFSSL_HAVE_PRF
  493. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  494. #ifdef WOLFSSL_BASE16
  495. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t tls12_kdf_test(void);
  496. #endif /* WOLFSSL_BASE16 */
  497. #endif /* WOLFSSL_HAVE_HKDF && !NO_HMAC */
  498. #endif /* WOLFSSL_HAVE_PRF */
  499. #if defined(WOLFSSL_HAVE_PRF) && !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  500. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t prf_test(void);
  501. #endif
  502. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sshkdf_test(void);
  503. #ifdef WOLFSSL_TLS13
  504. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t tls13_kdf_test(void);
  505. #endif
  506. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t x963kdf_test(void);
  507. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hpke_test(void);
  508. #ifdef WC_SRTP_KDF
  509. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srtpkdf_test(void);
  510. #endif
  511. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t arc4_test(void);
  512. #ifdef WC_RC2
  513. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rc2_test(void);
  514. #endif
  515. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha_test(void);
  516. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha_test(void);
  517. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha20_poly1305_aead_test(void);
  518. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha20Poly1305_test(void);
  519. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des_test(void);
  520. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des3_test(void);
  521. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_test(void);
  522. #if defined(WOLFSSL_AES_CFB)
  523. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_cfb_test(void);
  524. #endif
  525. #ifdef WOLFSSL_AES_XTS
  526. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_xts_test(void);
  527. #endif
  528. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes192_test(void);
  529. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes256_test(void);
  530. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesofb_test(void);
  531. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cmac_test(void);
  532. #if defined(WOLFSSL_SIPHASH)
  533. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t siphash_test(void);
  534. #endif
  535. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t poly1305_test(void);
  536. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_test(void);
  537. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_default_test(void);
  538. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t gmac_test(void);
  539. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesccm_test(void);
  540. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aeskeywrap_test(void);
  541. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t camellia_test(void);
  542. #ifdef WOLFSSL_SM4
  543. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm4_test(void);
  544. #endif
  545. #ifdef WC_RSA_NO_PADDING
  546. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_no_pad_test(void);
  547. #endif
  548. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void);
  549. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dh_test(void);
  550. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dsa_test(void);
  551. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srp_test(void);
  552. #ifndef WC_NO_RNG
  553. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_test(void);
  554. #endif /* WC_NO_RNG */
  555. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pwdbased_test(void);
  556. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_test(void);
  557. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ripemd_test(void);
  558. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  559. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_test(void); /* test mini api */
  560. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey0_test(void);
  561. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey1_test(void);
  562. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openSSL_evpMD_test(void);
  563. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_evpSig_test(void);
  564. #endif
  565. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf1_test(void);
  566. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_pbkdf_test(void);
  567. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  568. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf2_test(void);
  569. #endif
  570. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t scrypt_test(void);
  571. #ifdef HAVE_ECC
  572. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test(void);
  573. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  574. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  575. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_encrypt_test(void);
  576. #endif
  577. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  578. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  579. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  580. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  581. /* skip for ATECC508/608A, cannot import private key buffers */
  582. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test_buffers(void);
  583. #endif
  584. #endif
  585. #ifdef HAVE_CURVE25519
  586. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve25519_test(void);
  587. #endif
  588. #ifdef HAVE_ED25519
  589. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed25519_test(void);
  590. #endif
  591. #ifdef HAVE_CURVE448
  592. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve448_test(void);
  593. #endif
  594. #ifdef HAVE_ED448
  595. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed448_test(void);
  596. #endif
  597. #ifdef WOLFSSL_HAVE_KYBER
  598. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t kyber_test(void);
  599. #endif
  600. #ifdef HAVE_DILITHIUM
  601. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dilithium_test(void);
  602. #endif
  603. #if defined(WOLFSSL_HAVE_XMSS)
  604. #if !defined(WOLFSSL_SMALL_STACK) && WOLFSSL_XMSS_MIN_HEIGHT <= 10
  605. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t xmss_test_verify_only(void);
  606. #endif
  607. #if !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  608. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t xmss_test(void);
  609. #endif
  610. #endif
  611. #if defined(WOLFSSL_HAVE_LMS)
  612. #if !defined(WOLFSSL_SMALL_STACK)
  613. #if (defined(WOLFSSL_WC_LMS) && (LMS_MAX_HEIGHT >= 10)) || \
  614. defined(HAVE_LIBLMS)
  615. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t lms_test_verify_only(void);
  616. #endif
  617. #endif
  618. #if !defined(WOLFSSL_LMS_VERIFY_ONLY)
  619. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t lms_test(void);
  620. #endif
  621. #endif
  622. #ifdef WOLFCRYPT_HAVE_ECCSI
  623. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t eccsi_test(void);
  624. #endif
  625. #ifdef WOLFCRYPT_HAVE_SAKKE
  626. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sakke_test(void);
  627. #endif
  628. #ifdef HAVE_BLAKE2
  629. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2b_test(void);
  630. #endif
  631. #ifdef HAVE_BLAKE2S
  632. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2s_test(void);
  633. #endif
  634. #ifdef HAVE_LIBZ
  635. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t compress_test(void);
  636. #endif
  637. #ifdef HAVE_PKCS7
  638. #ifndef NO_PKCS7_ENCRYPTED_DATA
  639. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7encrypted_test(void);
  640. #endif
  641. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  642. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7compressed_test(void);
  643. #endif
  644. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7signed_test(void);
  645. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7enveloped_test(void);
  646. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  647. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7authenveloped_test(void);
  648. #endif
  649. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  650. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7callback_test(byte* cert, word32 certSz, byte* key,
  651. word32 keySz);
  652. #endif
  653. #endif
  654. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  655. !defined(NO_FILESYSTEM)
  656. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cert_test(void);
  657. #endif
  658. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  659. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  660. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certext_test(void);
  661. #endif
  662. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  663. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  664. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t decodedCertCache_test(void);
  665. #endif
  666. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memory_test(void);
  667. #if defined(WOLFSSL_PUBLIC_MP) && \
  668. ((defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  669. defined(USE_FAST_MATH))
  670. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mp_test(void);
  671. #endif
  672. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  673. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t prime_test(void);
  674. #endif
  675. #if defined(ASN_BER_TO_DER) && \
  676. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  677. defined(OPENSSL_EXTRA_X509_SMALL))
  678. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t berder_test(void);
  679. #endif
  680. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t logging_test(void);
  681. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  682. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t time_test(void);
  683. #endif
  684. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  685. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t wolfcrypt_mutex_test(void);
  686. #else
  687. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mutex_test(void);
  688. #endif
  689. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  690. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memcb_test(void);
  691. #endif
  692. #ifdef WOLFSSL_CAAM_BLOB
  693. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blob_test(void);
  694. #endif
  695. #ifdef HAVE_ARIA
  696. #include "wolfssl/wolfcrypt/port/aria/aria-crypt.h"
  697. void printOutput(const char *strName, unsigned char *data, unsigned int dataSz);
  698. WOLFSSL_TEST_SUBROUTINE int ariagcm_test(MC_ALGID);
  699. #endif
  700. #ifdef WOLF_CRYPTO_CB
  701. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cryptocb_test(void);
  702. #endif
  703. #ifdef WOLFSSL_CERT_PIV
  704. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certpiv_test(void);
  705. #endif
  706. #ifdef WOLFSSL_AES_SIV
  707. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_siv_test(void);
  708. #endif
  709. #if defined(WOLFSSL_AES_EAX) && \
  710. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  711. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_eax_test(void);
  712. #endif /* WOLFSSL_AES_EAX */
  713. /* General big buffer size for many tests. */
  714. #define FOURK_BUF 4096
  715. /* If not defined in user_settings, the ERROR_OUT pause is 120 seconds. */
  716. #ifndef WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION
  717. #define WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION 120
  718. #endif
  719. #if defined(WOLFSSL_ESPIDF_ERROR_PAUSE)
  720. #if defined(CONFIG_FREERTOS_HZ)
  721. #define WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION_TICKS \
  722. (WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION * CONFIG_FREERTOS_HZ)
  723. #else
  724. /* If not defined, assume RTOS is 1000 ticks per second. */
  725. #define WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION_TICKS \
  726. (WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION * 1000)
  727. #endif
  728. /* When defined, pause at error condition rather than exit with error. */
  729. #define ERROR_OUT(err, eLabel) \
  730. do { \
  731. ret = (err); \
  732. ESP_LOGE(ESPIDF_TAG, "Failed: Error = %d during %s, line %d", \
  733. err, __FUNCTION__, __LINE__); \
  734. ESP_LOGI(ESPIDF_TAG, "Extended system info:"); \
  735. esp_ShowExtendedSystemInfo(); \
  736. ESP_LOGW(ESPIDF_TAG, "Paused for %d seconds! " \
  737. "WOLFSSL_ESPIDF_ERROR_PAUSE is enabled.", \
  738. WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION); \
  739. vTaskDelay(WOLFSSL_ESPIDF_ERROR_PAUSE_DURATION_TICKS); \
  740. goto eLabel; \
  741. } while (0)
  742. #else
  743. #define ERROR_OUT(err, eLabel) do { ret = (err); goto eLabel; } while (0)
  744. #endif
  745. /* Not all unexpected conditions are actually errors .*/
  746. #define WARNING_OUT(err, eLabel) do { ret = (err); goto eLabel; } while (0)
  747. static void render_error_message(const char* msg, wc_test_ret_t es)
  748. {
  749. (void)msg;
  750. (void)es;
  751. #ifdef WOLFSSL_LINUXKM
  752. #define err_sys_printf lkm_printf
  753. #else
  754. #define err_sys_printf printf
  755. #endif
  756. switch (WC_TEST_RET_DEC_TAG(es)) {
  757. case WC_TEST_RET_TAG_NC:
  758. err_sys_printf("%s error L=%d\n", msg, WC_TEST_RET_DEC_LN(es));
  759. break;
  760. case WC_TEST_RET_TAG_EC:
  761. #ifdef NO_ERROR_STRINGS
  762. err_sys_printf("%s error L=%d code=%d\n", msg,
  763. WC_TEST_RET_DEC_LN(es), -WC_TEST_RET_DEC_I(es));
  764. #else
  765. err_sys_printf("%s error L=%d code=%d (%s)\n", msg,
  766. WC_TEST_RET_DEC_LN(es), -WC_TEST_RET_DEC_I(es),
  767. wc_GetErrorString(-WC_TEST_RET_DEC_I(es)));
  768. #endif
  769. break;
  770. case WC_TEST_RET_TAG_ERRNO:
  771. {
  772. /* strerror_r() comes in two mutually incompatible flavors, a native glibc
  773. * flavor that always returns a non-null char pointer that must be used
  774. * directly, and a POSIX flavor that returns an error int, and iff success,
  775. * stores an error string in the supplied buffer. this is all most
  776. * infelicitous...
  777. */
  778. #if !defined(STRING_USER) && !defined(NO_ERROR_STRINGS) && \
  779. (defined(__STDC_VERSION__) && (__STDC_VERSION__ > 199901L)) && \
  780. ((defined(__GLIBC__) && (__GLIBC__ >= 2)) || \
  781. (defined(__USE_XOPEN2K) && \
  782. defined(_POSIX_C_SOURCE) && \
  783. (_POSIX_C_SOURCE >= 200112L)))
  784. char errno_buf[64], *errno_string;
  785. /* precisely mirror the gate used in glibc string.h */
  786. #if defined __USE_XOPEN2K && !defined __USE_GNU
  787. if (strerror_r(WC_TEST_RET_DEC_I(es),
  788. errno_buf, sizeof(errno_buf)) != 0)
  789. XSTRLCPY(errno_buf, "?", sizeof(errno_buf));
  790. errno_string = errno_buf;
  791. #else
  792. errno_string = strerror_r(WC_TEST_RET_DEC_I(es),
  793. errno_buf, sizeof(errno_buf));
  794. #endif
  795. err_sys_printf("%s error L=%d errno=%d (%s)\n", msg,
  796. WC_TEST_RET_DEC_LN(es), WC_TEST_RET_DEC_I(es),
  797. errno_string);
  798. #else /* can't figure out how to strerror_r(), or don't want error strings */
  799. err_sys_printf("%s error L=%d errno=%d\n", msg,
  800. WC_TEST_RET_DEC_LN(es), WC_TEST_RET_DEC_I(es));
  801. #endif
  802. break;
  803. }
  804. case WC_TEST_RET_TAG_I:
  805. err_sys_printf("%s error L=%d i=%d\n", msg,
  806. WC_TEST_RET_DEC_LN(es), WC_TEST_RET_DEC_I(es));
  807. break;
  808. }
  809. #undef err_sys_printf
  810. }
  811. static void print_fiducials(void);
  812. #ifdef HAVE_STACK_SIZE
  813. static THREAD_RETURN err_sys(const char* msg, int es)
  814. #else
  815. static wc_test_ret_t err_sys(const char* msg, wc_test_ret_t es)
  816. #endif
  817. {
  818. render_error_message(msg, es);
  819. print_fiducials();
  820. #ifdef WOLFSSL_LINUXKM
  821. EXIT_TEST(es);
  822. #else
  823. EXIT_TEST(-1);
  824. #endif
  825. }
  826. #ifndef HAVE_WOLFCRYPT_TEST_OPTIONS
  827. /* func_args from test.h, so don't have to pull in other stuff */
  828. typedef struct func_args {
  829. int argc;
  830. char** argv;
  831. wc_test_ret_t return_code;
  832. } func_args;
  833. #endif /* !HAVE_WOLFCRYPT_TEST_OPTIONS */
  834. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  835. static void myFipsCb(int ok, int err, const char* hash)
  836. {
  837. printf("in my Fips callback, ok = %d, err = %d\n", ok, err);
  838. printf("message = %s\n", wc_GetErrorString(err));
  839. printf("hash = %s\n", hash);
  840. if (err == IN_CORE_FIPS_E) {
  841. printf("In core integrity hash check failure, copy above hash\n");
  842. printf("into verifyCore[] in fips_test.c and rebuild\n");
  843. }
  844. }
  845. #endif /* HAVE_FIPS && !WOLFSSL_LINUXKM */
  846. #ifdef WOLFSSL_STATIC_MEMORY
  847. #if defined(WOLFSSL_STATIC_MEMORY_TEST_SZ)
  848. static byte gTestMemory[WOLFSSL_STATIC_MEMORY_TEST_SZ];
  849. #elif defined(BENCH_EMBEDDED)
  850. static byte gTestMemory[14000];
  851. #elif defined(WOLFSSL_CERT_EXT)
  852. static byte gTestMemory[140000];
  853. #elif (defined(WOLFSSL_SP_MATH_ALL) || defined(USE_FAST_MATH)) && \
  854. !defined(ALT_ECC_SIZE)
  855. static byte gTestMemory[160000];
  856. #else
  857. static byte gTestMemory[80000];
  858. #endif
  859. #endif
  860. #ifdef WOLFSSL_PB
  861. static int wolfssl_pb_print(const char* msg, ...)
  862. {
  863. int ret;
  864. va_list args;
  865. char tmpBuf[80];
  866. va_start(args, msg);
  867. ret = vsprint(tmpBuf, msg, args);
  868. va_end(args);
  869. fnDumpStringToSystemLog(tmpBuf);
  870. return ret;
  871. }
  872. #endif /* WOLFSSL_PB */
  873. #if defined(WOLF_CRYPTO_CB) && !defined(HAVE_HASHDRBG) && \
  874. !defined(WC_NO_RNG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  875. /* Enable support for RNG with crypto callback */
  876. static int rng_crypto_cb(int thisDevId, wc_CryptoInfo* info, void* ctx)
  877. {
  878. int rc = CRYPTOCB_UNAVAILABLE;
  879. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  880. rc = wc_GenerateSeed(&info->rng.rng->seed, info->rng.out, info->rng.sz);
  881. }
  882. (void)ctx;
  883. (void)thisDevId;
  884. return rc;
  885. }
  886. #endif
  887. #if defined(WC_KDF_NIST_SP_800_56C)
  888. #define INIT_SP80056C_TEST_VECTOR(_z, _fixedInfo, _derivedKey, _hashType) \
  889. { \
  890. .z = (const byte*)_z, .zSz = sizeof(_z) - 1, \
  891. .fixedInfo = (const byte*)_fixedInfo, \
  892. .fixedInfoSz = sizeof(_fixedInfo) - 1, \
  893. .derivedKey = (const byte*)_derivedKey, \
  894. .derivedKeySz = sizeof(_derivedKey) - 1, .hashType = _hashType, \
  895. }
  896. #define SP800_56C_MAX_OUT 128
  897. static WOLFSSL_TEST_SUBROUTINE wc_test_ret_t nist_sp80056c_kdf_test(void)
  898. {
  899. struct sp800_56c_test_vector {
  900. const byte* z;
  901. word32 zSz;
  902. const byte* fixedInfo;
  903. word32 fixedInfoSz;
  904. const byte* derivedKey;
  905. word32 derivedKeySz;
  906. enum wc_HashType hashType;
  907. };
  908. struct sp800_56c_test_vector* v;
  909. byte output[SP800_56C_MAX_OUT];
  910. word32 i;
  911. int ret;
  912. /* vectors from
  913. * https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/example-values
  914. *
  915. * (KeyManagement) */
  916. struct sp800_56c_test_vector vctors[] = {
  917. #if !defined(NO_SHA)
  918. /* SHA-1 */
  919. INIT_SP80056C_TEST_VECTOR(
  920. "\xad\x42\x01\x82\x63\x3f\x85\x26\xbf\xe9\x54\xac\xda\x37\x6f\x05"
  921. "\xe5\xff\x4f\x83\x7f\x54\xfe\xbe\x0f\xb1\x2a\x1b\x3b\xeb\xf2\x63"
  922. "\xee\x21\x64\x13\xed\x06\xa8\x4a\x12\xeb\x51\x11\x59\xf1\x33\x7d",
  923. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  924. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  925. "\x6f\xef\x44\x2f\xc1\x7a\x7e\x2b\x0c\x9d\xec\xe0\xe4\x7a\x57\x48"
  926. "\xac\xb4\x6a\xf1\x98\xd7\x67\x47\x0f\x28\xa1\x04\xb5\x61\x30\xae"
  927. "\xb0\x10\x09\xa4\x56\x82\xa5\xe1",
  928. WC_HASH_TYPE_SHA),
  929. INIT_SP80056C_TEST_VECTOR(
  930. "\xae\x64\xab\x2b\x2b\x75\xa9\x4c\xf8\xef\x24\xda\x24\x56\xbd\x3a"
  931. "\xa3\x6d\xb6\x14\x29\xea\x55\x21",
  932. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  933. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  934. "\xcc\x96\x5a\x52\xd0\x5c\x94\x9e\x52\xc0\x35\xfd\x03\x53\x0d\xb7"
  935. "\xea\xa4\x08\x70\x2c\x9d\x35\x21\x1e\x67\x21\x54\x12\x45\x91\x51"
  936. "\xba\x22\x62\xbd\x1e\x28\xe5\x6b",
  937. WC_HASH_TYPE_SHA),
  938. INIT_SP80056C_TEST_VECTOR(
  939. "\x71\x5d\xc0\xea\x24\x6b\x46\x56\x63\xa8\x9c\xde\x04\x12\xc1\x92"
  940. "\xe1\x9e\x58\xd5\xb0\xb6\x36\x6d\xa7\x89\xad\xbf\x44\x9a\x38\xaa"
  941. "\x46\x69\xfe\x36\x30\xa2\x0f\x7f\xa3\x14\x9c\x9b\x4b\x0a\xb5\xcd"
  942. "\x3e\x14\x18\x2b\x75\x04\xd5\xd2\x75\x2b\xf6\x58\x7a\xab\xc9\xf4"
  943. "\xcb\x8f\xe5\x29\x23\x6a\xb8\x15\x36\xad\xd2\xbd\x25\xd6\xbf\x9d"
  944. "\x5f\x1d\xf5\x76\x16\x5a\xa5\x5c\x24\x99\x61\xd8\xf8\x75\x00\xed"
  945. "\x8d\xbf\xc5\xd2\x50\x53\x4c\x07\xd9\x9a\xc9\x17\xf9\x84\x60\x46"
  946. "\xac\x5c\xb8\xa2\x98\x74\x26\x22\xd3\xc9\x86\x18\x06\x92\x46\xe8"
  947. "\xad\x37\x11\x25\x57\xe0\xe6\x34\xb5\x81\x32\x7a\x4a\xd3\x2c\x7c"
  948. "\x76\x4b\xe8\xf8\x08\x0d\x37\x2c\x63\x20\x93\xa7\x67\xf1\x55\xbd"
  949. "\x22\xec\x00\x3c\xa6\x1c\x8b\x43\x32\x0f\x3a\xbe\xb5\xdd\xc4\xa3"
  950. "\xb1\x89\x82\xfd\xd6\x51\x0f\x88\x3c\x8d\xc1\xe0\xb1\x57\xff\xb9"
  951. "\xcf\xc0\xa5\x9c\xe2\xd4\x05\x5f\xfc\x73\xe7\x15\x2a\x6a\x95\x43"
  952. "\xb9\x19\xe7\x94\xe9\x49\x61\x33\xbe\x2a\x23\x18\xd9\x05\x6e\xfd"
  953. "\x74\x48\x2d\xc6\x3c\x0d\xb5\x8f\xe6\x42\x6b\x0f\xe7\x35\x45\x42"
  954. "\xc2\x19\x7a\xb6\xbd\x35\xf1\xa9\x2d\xce\x90\xb1\xc4\x6d\x32\xc1",
  955. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  956. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  957. "\x1b\x5f\xcc\x8d\x81\xa5\xd9\xa9\x36\x94\xe6\x47\x77\x88\xd8\x03"
  958. "\x15\x84\xc3\xf5\x2e\x9f\x11\x7a\xe8\x18\x4d\xba\x56\x47\x9d\x87"
  959. "\x66\x76\x92\xf1\x2d\x7b\xd3\x8b",
  960. WC_HASH_TYPE_SHA),
  961. INIT_SP80056C_TEST_VECTOR(
  962. "\x33\xe0\x50\xbd\x20\x9f\x2d\xf2\x77\x19\x78\xfc\xd1\xd4\xc8\x2e"
  963. "\x49\xd0\x1d\x65\xbb\x62\x03\x20\xd3\x0b\xfe\xa8\x7a\xa8\x69\xe1"
  964. "\x07\xa5\x17\xa4\xc8\x5b\x69\x28\x45\x21\xca\x54\xb7\x7f\x59\xe9"
  965. "\x4a\x85\x6d\xaa\x30\xa3\x85\xa5\x25\xd8\xa3\xf7\xe1\x5e\xe5\xe9"
  966. "\xaa\x12\x8d\x45\xef\x63\xf9\x0c\x10\xe0\x8f\xc5\x26\x36\x13\x77"
  967. "\x81\x54\x7a\x58\x9f\x97\x87\xf9\xd7\xdd\x61\x43\x41\x9a\x26\x16"
  968. "\x80\x16\x82\x40\xaa\xb2\x01\x3d\x80\x20\xdb\xe8\x4b\x7e\x2b\xed"
  969. "\xce\x67\x1b\x94\x03\xbd\x1f\x91\x71\xa2\x57\x90\xce\x66\x7d\xed",
  970. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  971. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  972. "\x35\x10\x85\x85\xfc\x55\x62\xb5\xf1\x8f\x20\x7b\xa8\x35\x63\xe4"
  973. "\x49\x28\x45\x6a\x5e\x53\x63\x7e\xae\x6b\xb5\xfc\x33\x88\xae\x02"
  974. "\x91\x79\x0a\x1d\xc8\x31\x4e\x28",
  975. WC_HASH_TYPE_SHA),
  976. INIT_SP80056C_TEST_VECTOR(
  977. "\x5c\x80\x4f\x45\x4d\x30\xd9\xc4\xdf\x85\x27\x1f\x93\x52\x8c\x91"
  978. "\xdf\x6b\x48\xab\x5f\x80\xb3\xb5\x9c\xaa\xc1\xb2\x8f\x8a\xcb\xa9"
  979. "\xcd\x3e\x39\xf3\xcb\x61\x45\x25\xd9\x52\x1d\x2e\x64\x4c\x53\xb8"
  980. "\x07\xb8\x10\xf3\x40\x06\x2f\x25\x7d\x7d\x6f\xbf\xe8\xd5\xe8\xf0"
  981. "\x72\xe9\xb6\xe9\xaf\xda\x94\x13\xea\xfb\x2e\x8b\x06\x99\xb1\xfb"
  982. "\x5a\x0c\xac\xed\xde\xae\xad\x7e\x9c\xfb\xb3\x6a\xe2\xb4\x20\x83"
  983. "\x5b\xd8\x3a\x19\xfb\x0b\x5e\x96\xbf\x8f\xa4\xd0\x9e\x34\x55\x25"
  984. "\x16\x7e\xcd\x91\x55\x41\x6f\x46\xf4\x08\xed\x31\xb6\x3c\x6e\x6d",
  985. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  986. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  987. "\xfa\xa0\x22\xce\x7f\xa9\xba\x95\xeb\xa3\x9f\x3f\x44\xf3\xee\x14"
  988. "\x96\x0a\x0b\x23\x9d\x01\x4b\x57\x70\xe4\x71\xd7\x5a\x99\xea\x87"
  989. "\x10\xe3\x8f\x0c\xef\x0f\xfc\x67",
  990. WC_HASH_TYPE_SHA),
  991. INIT_SP80056C_TEST_VECTOR(
  992. "\xad\x42\x01\x82\x63\x3f\x85\x26\xbf\xe9\x54\xac\xda\x37\x6f\x05"
  993. "\xe5\xff\x4f\x83\x7f\x54\xfe\xbe\x0f\xb1\x2a\x1b\x3b\xeb\xf2\x63"
  994. "\xee\x21\x64\x13\xed\x06\xa8\x4a\x12\xeb\x51\x11\x59\xf1\x33\x7d",
  995. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  996. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  997. "\x6f\xef\x44\x2f\xc1\x7a\x7e\x2b\x0c\x9d\xec\xe0\xe4\x7a\x57\x48"
  998. "\xac\xb4\x6a\xf1\x98\xd7\x67\x47\x0f\x28\xa1\x04\xb5\x61\x30\xae"
  999. "\xb0\x10\x09\xa4\x56\x82\xa5\xe1",
  1000. WC_HASH_TYPE_SHA),
  1001. #endif
  1002. #if defined(WOLFSSL_SHA224)
  1003. /* SHA-224*/
  1004. INIT_SP80056C_TEST_VECTOR(
  1005. "\x8c\x5d\x6e\x5d\x36\x06\x83\xba\x55\xb0\x9d\xb1\x69\x6d\x7c\x64"
  1006. "\x02\xff\x87\x88\x5f\xf5\x07\x70\xf2\x76\x7b\x75\x54\x60\x20\x7e"
  1007. "\xd5\xc7\x43\xfd\x27\xe7\xeb\x1d\x0c\xa5\x91\xf8\x56\x38\x93\x11"
  1008. "\x73\x07\x44\xf2\x04\xd2\xe5\x5b\x8b\xd4\x46\xce\xca\x03\x1f\x7b"
  1009. "\xac\xcf\xf1\xa7\x1b\x68\x34\x59\xcc\x54\xd5\x01\xda\xbf\x4a\x84"
  1010. "\xcd\xb8\x6d\xfa\xda\xff\x31\x0f\xbd\xba\xf7\x4d\xd5\x1b\xa1\xe1"
  1011. "\xe1\x19\x1a\xf1\x4c\x9b\xf8\x94\x43\xbf\x58\x8e\x9c\xe3\x30\x34"
  1012. "\xaf\x5e\x89\xbf\x6f\xfc\x47\xd7\xd9\xca\x4a\x5e\x8f\xf8\xa0\x50"
  1013. "\x20\xbb\x0f\x95\xbc\xde\x01\x56\xd8\x7f\xb8\x60\xbd\x40\x83\xfa"
  1014. "\x5b\x53\x1a\x08\xa4\xfb\x7e\xe0\x20\x1a\xe8\xb3\xcc\xff\xe9\x9f"
  1015. "\x27\x0b\xc3\x53\x4b\xaa\xcf\xc0\x01\xcd\xd8\x0a\xd8\x7c\xce\x71"
  1016. "\xf0\x91\xe7\x66\xca\x5c\xc2\x75\xcb\x49\x14\x5a\x5e\xe6\x16\x2e"
  1017. "\xcc\xf5\x58\xce\xc4\xd3\xee\x53\x1e\x91\xe9\xa5\x29\x69\x63\x4d"
  1018. "\x3a\xf8\xd2\x6f\x8d\x15\xdc\x0d\x6f\x6e\x0a\x97\x4b\xe4\x34\x1b"
  1019. "\x68\xa0\x19\x90\xdb\xb8\x64\x95\x89\x1a\xd3\xaf\xc1\xe4\xce\xdf"
  1020. "\x4c\x6a\xe1\xf1\xcd\x60\x81\xcd\xee\xd8\xe6\xb3\x26\x4e\xc3\xbe"
  1021. "\x24\x58\xd1\xc0\xca\x24\x43\x41\x0c\xf3\xb4\x7c\x0c\x25\x4c\x7d"
  1022. "\xc8\xec\xb4\x3e\x6c\x23\x64\xe1\xc0\x62\x19\xcc\x7e\xfb\xff\xbb"
  1023. "\x63\xd7\xff\xfc\x74\x58\x12\xfd\x24\x0c\x33\xd4\x96\xb9\x99\x2f"
  1024. "\x96\x80\xa6\x3c\x07\x96\x3c\x0c\x49\xf3\xc1\xba\xef\xec\xaf\x32"
  1025. "\xe2\xaa\x8a\x2f\x7c\xd3\x0d\x8f\x05\x1e\xe2\xf5\x0f\xbf\x05\xab"
  1026. "\x13\x96\xa4\xea\x87\x44\x7d\x7b\x98\x1b\x5e\x46\x14\x28\x18\x71"
  1027. "\xa6\xf0\xf6\xbf\x1f\xe0\x02\x2f\x7e\xa1\x32\xbe\x0a\xe9\x19\x26"
  1028. "\xab\x12\xaf\x6d\xc4\x50\x64\xaa\xd5\x6b\x84\xb9\x0c\x70\x08\x37"
  1029. "\x09\xcf\xf7\xe3\x1b\x54\x8f\xb7\xfb\x2c\xf7\x5a\xbf\x96\xe0\x1c"
  1030. "\xcd\x3e\x94\x2e\xed\x91\x48\x0d\x4c\x24\xc6\xb7\xf9\x79\xfb\xbe"
  1031. "\x5d\xa2\x39\xb3\x76\x16\x7d\x68\x57\x35\x24\xff\xcb\x50\x99\x54"
  1032. "\xcc\x80\xa0\xe1\xa7\x1c\x40\xc4\xda\x17\xb8\xd1\x57\x2b\x21\x58"
  1033. "\x7a\x8d\x66\xcc\x62\x1c\x7c\xd1\x0f\x49\xab\xd5\xef\x86\x31\x13"
  1034. "\xe6\x19\x21\x08\x6f\xac\x25\x31\x2b\x74\x1c\x11\xa8\xfb\xc1\xe3"
  1035. "\x3c\x34\xd9\xda\x14\xa8\x22\x47\x7f\xcc\x36\x66\x70\x25\xc4\xf1"
  1036. "\x30\xae\x10\x0e\x36\xf1\x5d\xa0\x03\x74\xce\x87\x41\x67\x9f\x61",
  1037. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  1038. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  1039. "\x4b\x99\x6a\x60\xc0\x4a\x35\xc5\xe6\xd4\x74\xb1\x0a\x25\x8d\x56"
  1040. "\x2e\xa6\xdc\x52\xf6\xc6\x9b\xf3\x9e\xf8\x8c\x89\xe3\xcc\x8a\x54"
  1041. "\xda\x2f\x3c\x0b\x56\x1b\x53\xfe\x76\x55\x13\x63\xd6\x9c\x3c\xef"
  1042. "\x74\xe3\x4f\xe8\x8e\xb3\xac\x51",
  1043. WC_HASH_TYPE_SHA224),
  1044. INIT_SP80056C_TEST_VECTOR(
  1045. "\x52\x27\x2f\x50\xf4\x6f\x4e\xdc\x91\x51\x56\x90\x92\xf4\x6d\xf2"
  1046. "\xd9\x6e\xcc\x3b\x6d\xc1\x71\x4a\x4e\xa9\x49\xfa\x9f\x18\xff\x54"
  1047. "\xf8\x87\x23\x07\x3f\x64\xa6\x95\x3d\x04\x91\x4f\x45\xa2\x3e\xee"
  1048. "\x7c\xfc\x46\x67\x08\x0a\xa0\xf9",
  1049. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  1050. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  1051. "\x77\x56\xbc\xfd\xef\x3e\xe6\x9f\x6a\xc2\x3c\xd2\xdc\x60\x7d\x01"
  1052. "\xfa\x8c\xe1\xb2\x4f\x5c\xaa\xaa\x48\xe0\x4b\x81\x63\xe1\x73\x3a"
  1053. "\xed\x7a\x04\x0e\x73\xf2\xb5\x42\x36\x8f\x00\x54\x8b\x16\x3c\x3d"
  1054. "\xc9\x6d\x70\x09\x99\x16\xf1\x6b",
  1055. WC_HASH_TYPE_SHA224),
  1056. #endif
  1057. #if !defined(NO_SHA256)
  1058. /* SHA-256 */
  1059. INIT_SP80056C_TEST_VECTOR(
  1060. "\xdd\x0f\x53\x96\x21\x9d\x1e\xa3\x93\x31\x04\x12\xd1\x9a\x08\xf1"
  1061. "\xf5\x81\x1e\x9d\xc8\xec\x8e\xea\x7f\x80\xd2\x1c\x82\x0c\x27\x88"
  1062. "\x22\x76\x84\xe7\x1f\x5c\x31\x3f\xad\xc9\x1e\x52\x98\x07\xe3\x14"
  1063. "\x7d\x53\x14\x5b\x15\xab\xd6\xed\x41\x6a\xd3\x5c\xd7\xe6\x83\x8f",
  1064. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  1065. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  1066. "\xc0\x8b\x3d\xe2\x4f\x1a\x38\x1e\x7a\x56\x75\xa2\xa6\x52\x3b\x08"
  1067. "\xf3\x54\x60\x5e\xee\x46\xb9\xf3\x9e\xad\xb1\xe9\x75\x34\x41\x6d"
  1068. "\x98\xb4\x3c\xae\x8a\xb0\x4a\xfd\x53\xde\xb3\x7f\x44\x02\x23\x52"
  1069. "\xc3\xfb\xde\x1e\x2f\x2c\xec\x53\x1c\xfc\x32\x4f\xdd\x0f\xcc\xa6",
  1070. WC_HASH_TYPE_SHA256),
  1071. INIT_SP80056C_TEST_VECTOR(
  1072. "\x44\xf4\x84\x09\xf3\x1b\xf3\x50\x94\x51\xdb\x4d\x30\x4b\xeb\xd8"
  1073. "\x3c\x2a\xd6\x50\x1c\x1b\x85\xe6\x32\xbc\x92\x58\x8e\x2d\x48\xb5"
  1074. "\xd2\xb8\x44\x44\x62\xad\x94\xe8\xa4\x44\x94\x1e\xd4\x97\x5c\x97"
  1075. "\x91\x17\xd7\x5a\x0a\x2b\xa8\x10\xdf\xa8\x80\x4a\x0f\xe9\x42\x6d"
  1076. "\xd7\xeb\x95\xf2\x9c\xa4\x30\xda\x37\xef\xa5\x2c\x42\xda\x1d\xe2"
  1077. "\x23\x76\x3b\xdd\xc9\x5e\x46\x6a\xa3\xb8\xd2\x06\xb8\x21\x8e\xdf"
  1078. "\x23\x97\x3d\x05\xf3\xc7\xc2\x2b\x22\x46\x53\xd4\xf9\x21\x85\x45"
  1079. "\x79\x83\x26\x2f\x27\xbc\x55\xa0\xa7\xae\xe5\x43\x54\x55\xd4\x3c"
  1080. "\x0e\x0c\x6d\x80\x67\xad\xaf\x90\x42\xb6\xb7\x77\x7e\x19\x8d\x67"
  1081. "\x60\x83\x0d\x96\xb2\x9a\x6a\xae\xf5\x74\xd7\x5e\x2d\x65\x43\x55"
  1082. "\x0f\x75\x55\xcf\x89\x82\xc8\x21\xc4\xec\x9b\x82\x66\x2b\x09\x36"
  1083. "\x69\xfd\x24\x6d\x4d\xcc\xbf\xf3\x1d\x98\x4e\xf6\x3c\x4f\x95\xd7"
  1084. "\x39\xc7\x7e\x66\xf6\x9b\x6d\xb0\xd9\xfc\x1a\x97\x3f\x52\x33\xd1"
  1085. "\x1f\xe7\x15\x4b\x8a\x6b\xe1\x50\x46\x7d\x92\xda\xf0\x91\x44\x34"
  1086. "\xf0\xcf\x17\x6e\x96\x1b\x31\x3f\xe3\xe2\x3b\xfe\x37\x8b\x87\xba"
  1087. "\xf2\x73\x93\x7f\xfa\xa6\x85\x65\x8b\x09\x78\x1d\x26\x97\x86\x4b"
  1088. "\x0d\x82\x48\x5c\xf7\xd6\x2f\xfc\xea\x34\x33\x88\xcf\xec\x15\x27"
  1089. "\x3a\x39\x4b\x84\x32\xee\xb6\x32\x1b\xe3\x1d\x4f\xcd\x16\x15\xc8"
  1090. "\x1c\x69\x37\xc0\x8d\x92\x41\x6e\xdb\xdd\x20\x10\xfa\x8b\x6e\x0a"
  1091. "\x8a\x60\x3d\xad\xe0\x10\xbc\x9c\xd7\x6b\x14\xe3\xe2\x19\x0e\x3b"
  1092. "\xa3\x00\xe7\x1a\xdd\xbf\x24\x4c\xf8\x06\x29\x49\x76\xea\xf0\x7d"
  1093. "\x02\x3b\xdb\x57\xfc\x5d\x19\x64\xc7\xd4\x20\x6e\x72\x06\x1b\xfe"
  1094. "\x1e\xe4\xee\xbc\x92\x00\xe1\x3a\x6c\xba\x32\x86\x9d\xbe\x80\x82"
  1095. "\xcd\xf3\x64\x5b\x5a\x72\x7d\xd2\x7c\xa5\x03\xf4\xed\xeb\x73\xe8"
  1096. "\x8a\x3a\x55\x2c\x7e\x00\xd4\xee\x72\x42\x13\x72\x36\xa0\x96\x5c"
  1097. "\x1e\xc3\xeb\xc0\xb4\x8c\x2b\x46\x7e\xb8\x42\x41\x5a\x28\x3f\x55"
  1098. "\xe2\x20\xff\xd1\x88\x19\x25\x6d\xa2\x47\x4d\x28\xfc\x3b\x04\xe0"
  1099. "\xc0\x7e\x4d\x25\xc1\x74\x93\x41\xd2\x22\x97\x01\x5c\xd8\x17\x8c"
  1100. "\x39\x18\xbe\x8c\x5c\xdf\x0f\xbf\xbb\x9a\x5a\xcc\xdd\x82\xaf\x07"
  1101. "\x83\xef\xe4\xdf\x64\xa8\xd8\x92\x82\x8f\x8d\xe5\x8c\x5d\x56\x9b"
  1102. "\x5b\x08\x45\x58\x96\xc4\xd3\xc3\x4f\xd3\xce\x93\xc4\x34\xc3\x8e"
  1103. "\xf5\x6c\xed\x30\x56\x1c\x37\x1a\xf9\xf2\xd8\x64\xfd\xc5\xb6\x2f",
  1104. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  1105. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  1106. "\x42\x35\xac\x89\xc9\xf3\x3e\x5d\xbb\x11\x60\x1a\x29\x83\xe3\x76"
  1107. "\xe2\x15\x39\x97\x87\xd8\x39\xa0\x38\x55\xfe\x04\x53\x3e\x1a\x67"
  1108. "\x66\x91\x5e\x67\xc2\x5e\xeb\x04\x08\x28\xe9\x6c\xd3\xd6\xef\x0a"
  1109. "\xb7\xd1\x7b\x43\x13\x43\xa7\xf3\xaa\xc6\x8f\x0c\x4a\x7e\x77\x9b",
  1110. WC_HASH_TYPE_SHA256),
  1111. /* slightly modified vector to test generation when derivedKeySz %
  1112. * hashOutSz != 0*/
  1113. INIT_SP80056C_TEST_VECTOR(
  1114. "\xdd\x0f\x53\x96\x21\x9d\x1e\xa3\x93\x31\x04\x12\xd1\x9a\x08\xf1"
  1115. "\xf5\x81\x1e\x9d\xc8\xec\x8e\xea\x7f\x80\xd2\x1c\x82\x0c\x27\x88"
  1116. "\x22\x76\x84\xe7\x1f\x5c\x31\x3f\xad\xc9\x1e\x52\x98\x07\xe3\x14"
  1117. "\x7d\x53\x14\x5b\x15\xab\xd6\xed\x41\x6a\xd3\x5c\xd7\xe6\x83\x8f",
  1118. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  1119. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  1120. "\xc0\x8b\x3d\xe2\x4f\x1a\x38\x1e\x7a\x56\x75\xa2\xa6\x52\x3b\x08"
  1121. "\xf3\x54\x60\x5e\xee\x46\xb9\xf3\x9e\xad\xb1\xe9\x75\x34\x41\x6d"
  1122. "\x98\xb4\x3c\xae\x8a\xb0\x4a\xfd\x53\xde\xb3\x7f\x44\x02\x23\x52"
  1123. "\xc3\xfb\xde\x1e\x2f\x2c\xec\x53\x1c\xfc\x32\x4f\xdd\x0f\xcc",
  1124. WC_HASH_TYPE_SHA256),
  1125. #endif
  1126. #if defined(WOLFSSL_SHA384)
  1127. /* SHA-384 */
  1128. INIT_SP80056C_TEST_VECTOR(
  1129. "\x5e\xa1\xfc\x4a\xf7\x25\x6d\x20\x55\x98\x1b\x11\x05\x75\xe0\xa8"
  1130. "\xca\xe5\x31\x60\x13\x7d\x90\x4c\x59\xd9\x26\xeb\x1b\x84\x56\xe4"
  1131. "\x27\xaa\x8a\x45\x40\x88\x4c\x37\xde\x15\x9a\x58\x02\x8a\xbc\x0e"
  1132. "\x88\x76\x9c\xb7\x2f\xc5\xac\x45\x7c\xd5\x8e\x89\x08\x9b\x19\x6a"
  1133. "\x70\xbf\x53\x3c\x6d\xc9\x1c\x9c\x7e\x17\x41\xdb\x5e\x7a\xb6\xb0"
  1134. "\x84\x9f\x01\xde\xa6\x5f\xed\xd0\x6c\x77\x18\x7c\xd8\x8e\xd0\x30",
  1135. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  1136. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  1137. "\x8e\x6e\x26\x5f\x20\x82\xf1\x4d\x34\xda\x23\xe1\x03\x2c\x90\x24"
  1138. "\x83\x4a\xf0\x15\x72\xb6\x64\x77\x82\x41\x1b\xdd\xcb\x84\xa5\xda"
  1139. "\xee\x11\x7b\xa6\xfb\xa6\xd0\xeb\x28\x08\xef\x8a\xb0\x70\x05\xee"
  1140. "\xab\xe5\x2d\x2e\xfd\x31\x12\x1c\x7b\xf9\xd5\xfa\xfc\x40\xe0\x0c"
  1141. "\x6d\x6d\xbf\x39\xef\x43\xfe\x97\x15\xc7\x20\x2c\xdc\x2d\xb7\xe8"
  1142. "\x2b\x88\xd7\x48\xeb\x84\x25\x8b\xf8\x4d\x85\x82\xf2\xbf\xd9\x40",
  1143. WC_HASH_TYPE_SHA384),
  1144. #endif
  1145. #if defined(WOLFSSL_SHA512)
  1146. /* SHA-512 */
  1147. INIT_SP80056C_TEST_VECTOR(
  1148. "\x00\xcd\xea\x89\x62\x1c\xfa\x46\xb1\x32\xf9\xe4\xcf\xe2\x26\x1c"
  1149. "\xde\x2d\x43\x68\xeb\x56\x56\x63\x4c\x7c\xc9\x8c\x7a\x00\xcd\xe5"
  1150. "\x4e\xd1\x86\x6a\x0d\xd3\xe6\x12\x6c\x9d\x2f\x84\x5d\xaf\xf8\x2c"
  1151. "\xeb\x1d\xa0\x8f\x5d\x87\x52\x1b\xb0\xeb\xec\xa7\x79\x11\x16\x9c"
  1152. "\x20\xcc\x01\x38\xa6\x72\xb6\x95\x8b\xd7\x84\xe5\xd7\xfa\x83\x73"
  1153. "\x8a\xc6\x8f\x9b\x34\x23\xb4\x83\xf9\xbf\x53\x9e\x71\x14\x1e\x45"
  1154. "\xdb\xfb\x7a\xfe\xd1\x8b\x11\xc0\x02\x8b\x13\xf1\xf8\x60\xef\x43"
  1155. "\xc4\x80\xf4\xda\xcd\xa2\x08\x10\x59\xd3\x97\x8c\x99\x9d\x5d\x1a"
  1156. "\xde\x34\x54\xe4",
  1157. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  1158. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  1159. "\x2d\x4a\x46\xa1\x70\x99\xba\xa8\x33\x0b\xc5\x9d\x4a\x1c\xf5\xae"
  1160. "\x3a\x30\x75\xb4\xc6\x2b\xb2\x6e\x7f\xc9\x89\x24\x72\x6d\x27\x4c"
  1161. "\x09\x64\x6f\x44\x08\xe6\x85\x8c\x43\xb4\x2d\xae\xd0\x15\xef\x26"
  1162. "\x17\x08\xd5\x5e\xf2\x4d\xaa\x7d\x3e\xa3\xd1\xc4\xa0\x8c\xfd\x24"
  1163. "\xdb\x60\x00\xa5\xb8\xa6\x7d\xe7\x46\xf3\xd3\xf4\xff\x34\x85\x15"
  1164. "\x8f\xd3\xb6\x91\x55\x79\x1d\xf4\x67\x47\xd4\xdb\xbe\x17\xc4\xb5"
  1165. "\x58\x46\x2e\x26\xbe\x5e\xd3\x5f\xe6\x80\xe2\x97\x14\x22\xc3\xb0"
  1166. "\x1b\x17\xe1\x67\xfc\x43\x7f\x84\x86\x9d\x85\x49\x53\x7b\x33\x38",
  1167. WC_HASH_TYPE_SHA512),
  1168. #endif
  1169. INIT_SP80056C_TEST_VECTOR(
  1170. "\x00\xcd\xea\x89\x62\x1c\xfa\x46\xb1\x32\xf9\xe4\xcf\xe2\x26\x1c"
  1171. "\xde\x2d\x43\x68\xeb\x56\x56\x63\x4c\x7c\xc9\x8c\x7a\x00\xcd\xe5"
  1172. "\x4e\xd1\x86\x6a\x0d\xd3\xe6\x12\x6c\x9d\x2f\x84\x5d\xaf\xf8\x2c"
  1173. "\xeb\x1d\xa0\x8f\x5d\x87\x52\x1b\xb0\xeb\xec\xa7\x79\x11\x16\x9c"
  1174. "\x20\xcc\x01\x38\xa6\x72\xb6\x95\x8b\xd7\x84\xe5\xd7\xfa\x83\x73"
  1175. "\x8a\xc6\x8f\x9b\x34\x23\xb4\x83\xf9\xbf\x53\x9e\x71\x14\x1e\x45"
  1176. "\xdb\xfb\x7a\xfe\xd1\x8b\x11\xc0\x02\x8b\x13\xf1\xf8\x60\xef\x43"
  1177. "\xc4\x80\xf4\xda\xcd\xa2\x08\x10\x59\xd3\x97\x8c\x99\x9d\x5d\x1a"
  1178. "\xde\x34\x54\xe4",
  1179. "\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
  1180. "\x42\x4f\x42\x42\x59\x34\x35\x36",
  1181. "\x2d\x4a",
  1182. WC_HASH_TYPE_SHA512),
  1183. };
  1184. for (i = 0; i < sizeof(vctors) / sizeof(vctors[0]); i++) {
  1185. v = &vctors[i];
  1186. ret = wc_KDA_KDF_onestep(v->z, v->zSz, v->fixedInfo, v->fixedInfoSz,
  1187. v->derivedKeySz, v->hashType, output,
  1188. /* use derivedKeySz to force the function to use a temporary buff
  1189. for the last block */
  1190. v->derivedKeySz);
  1191. if (ret != 0)
  1192. return WC_TEST_RET_ENC_EC(ret);
  1193. if (XMEMCMP(output, v->derivedKey, v->derivedKeySz) != 0)
  1194. return WC_TEST_RET_ENC_NC;
  1195. }
  1196. /* negative tests */
  1197. ret = wc_KDA_KDF_onestep(NULL, 0, (byte*)"fixed_info",
  1198. sizeof("fixed_info"), 16, WC_HASH_TYPE_SHA256, output, 16);
  1199. if (ret != BAD_FUNC_ARG)
  1200. return WC_TEST_RET_ENC_NC;
  1201. ret = wc_KDA_KDF_onestep((byte*)"secret", sizeof("secret"), NULL, 1, 16,
  1202. WC_HASH_TYPE_SHA256, output, 16);
  1203. if (ret != BAD_FUNC_ARG)
  1204. return WC_TEST_RET_ENC_NC;
  1205. /* allow empty FixedInfo */
  1206. ret = wc_KDA_KDF_onestep((byte*)"secret", sizeof("secret"), NULL, 0, 16,
  1207. WC_HASH_TYPE_SHA256, output, 16);
  1208. if (ret != 0)
  1209. return WC_TEST_RET_ENC_EC(ret);
  1210. return 0;
  1211. }
  1212. #endif /* WC_KDF_NIST_SP_800_56C */
  1213. /* optional macro to add sleep between tests */
  1214. #ifndef TEST_SLEEP
  1215. #define TEST_SLEEP() WC_DO_NOTHING
  1216. #else
  1217. #define TEST_PASS test_pass
  1218. #include <stdarg.h> /* for var args */
  1219. static WC_INLINE void test_pass(const char* fmt, ...)
  1220. {
  1221. va_list args;
  1222. va_start(args, fmt);
  1223. STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max_relative_stack, vprintf(fmt, args));
  1224. va_end(args);
  1225. PRINT_HEAP_CHECKPOINT();
  1226. TEST_SLEEP();
  1227. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1););
  1228. }
  1229. #endif
  1230. /* set test pass output to printf if not overridden */
  1231. #ifndef TEST_PASS
  1232. /* redirect to printf */
  1233. #define TEST_PASS(...) { \
  1234. if (STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK \
  1235. (max_relative_stack, printf(__VA_ARGS__)) < 0) { \
  1236. return err_sys("post-test check failed", WC_TEST_RET_ENC_NC);\
  1237. } \
  1238. PRINT_HEAP_CHECKPOINT(); \
  1239. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1);); \
  1240. }
  1241. #endif
  1242. #ifdef TEST_ALWAYS_RUN_TO_END
  1243. #define TEST_FAIL(msg, retval) do { last_failed_test_ret = (retval); render_error_message(msg, retval); } while (0)
  1244. #elif !defined(TEST_FAIL)
  1245. #define TEST_FAIL(msg, retval) return err_sys(msg, retval)
  1246. #endif
  1247. #ifdef HAVE_STACK_SIZE
  1248. THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args)
  1249. #else
  1250. wc_test_ret_t wolfcrypt_test(void* args)
  1251. #endif
  1252. {
  1253. wc_test_ret_t ret;
  1254. #if defined(WOLFSSL_TRACK_MEMORY_VERBOSE) && !defined(WOLFSSL_STATIC_MEMORY)
  1255. long heap_baselineAllocs, heap_baselineBytes;
  1256. #endif
  1257. #ifdef TEST_ALWAYS_RUN_TO_END
  1258. int last_failed_test_ret = 0;
  1259. #endif
  1260. STACK_SIZE_INIT();
  1261. #if defined(WOLFSSL_TRACK_MEMORY_VERBOSE) && !defined(WOLFSSL_STATIC_MEMORY)
  1262. (void)wolfCrypt_heap_peakAllocs_checkpoint();
  1263. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint();
  1264. (void)wolfCrypt_heap_peakBytes_checkpoint();
  1265. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint();
  1266. #endif
  1267. #ifdef WC_RNG_SEED_CB
  1268. wc_SetSeed_Cb(wc_GenerateSeed);
  1269. #endif
  1270. printf("------------------------------------------------------------------------------\n");
  1271. printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING);
  1272. #ifdef WOLF_CRYPTO_CB
  1273. if (devId != INVALID_DEVID)
  1274. printf(" CryptoCB with DevID:%X\n", devId);
  1275. #endif
  1276. printf("------------------------------------------------------------------------------\n");
  1277. if (args) {
  1278. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  1279. int ch;
  1280. #endif
  1281. ((func_args*)args)->return_code = -1; /* error state */
  1282. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  1283. while ((ch = mygetopt(((func_args*)args)->argc, ((func_args*)args)->argv, "s:m:a:h")) != -1) {
  1284. switch(ch) {
  1285. case 's':
  1286. #ifdef HAVE_STACK_SIZE_VERBOSE
  1287. max_relative_stack = (ssize_t)atoi(myoptarg);
  1288. break;
  1289. #else
  1290. return err_sys("-s (max relative stack bytes) requires HAVE_STACK_SIZE_VERBOSE (--enable-stacksize=verbose).", WC_TEST_RET_ENC_NC);
  1291. #endif
  1292. case 'm':
  1293. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  1294. max_relative_heap_bytes = (ssize_t)atoi(myoptarg);
  1295. break;
  1296. #else
  1297. return err_sys("-m (max relative heap memory bytes) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", WC_TEST_RET_ENC_NC);
  1298. #endif
  1299. case 'a':
  1300. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  1301. max_relative_heap_allocs = (ssize_t)atoi(myoptarg);
  1302. break;
  1303. #else
  1304. return err_sys("-a (max relative heap allocs) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", WC_TEST_RET_ENC_NC);
  1305. #endif
  1306. case 'h':
  1307. return err_sys("\
  1308. options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
  1309. [-a max_relative_heap_allocs] [-h]\n", 0);
  1310. default:
  1311. return err_sys("unknown test option. try -h.", WC_TEST_RET_ENC_NC);
  1312. }
  1313. }
  1314. #endif
  1315. }
  1316. #ifdef WOLFSSL_STATIC_MEMORY
  1317. if (wc_LoadStaticMemory(&HEAP_HINT, gTestMemory, sizeof(gTestMemory),
  1318. WOLFMEM_GENERAL, 1) != 0) {
  1319. printf("unable to load static memory.\n");
  1320. return(EXIT_FAILURE);
  1321. }
  1322. #ifndef OPENSSL_EXTRA
  1323. wolfSSL_SetGlobalHeapHint(HEAP_HINT);
  1324. #endif
  1325. #endif
  1326. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  1327. wolfSSL_Debugging_ON();
  1328. #endif
  1329. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  1330. wc_SetLoggingHeap(HEAP_HINT);
  1331. #endif
  1332. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  1333. wolfCrypt_SetCb_fips(myFipsCb);
  1334. #if FIPS_VERSION3_GE(6,0,0)
  1335. printf("FIPS module version in use: %s\n",
  1336. wolfCrypt_GetVersion_fips());
  1337. #endif
  1338. #endif
  1339. #if !defined(NO_BIG_INT)
  1340. if (CheckCtcSettings() != 1) {
  1341. printf("Sizeof mismatch (build) %x != (run) %lx\n",
  1342. CTC_SETTINGS, (unsigned long)CheckRunTimeSettings());
  1343. return err_sys("Build vs runtime math mismatch\n", WC_TEST_RET_ENC_NC);
  1344. }
  1345. #if defined(USE_FAST_MATH) && \
  1346. (!defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC))
  1347. if (CheckFastMathSettings() != 1)
  1348. return err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n",
  1349. WC_TEST_RET_ENC_NC);
  1350. #endif /* USE_FAST_MATH */
  1351. #endif /* !NO_BIG_INT */
  1352. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  1353. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  1354. initDefaultName();
  1355. #endif
  1356. #ifdef WOLFSSL_ASYNC_CRYPT
  1357. ret = wolfAsync_DevOpen(&devId);
  1358. if (ret < 0) {
  1359. printf("Async device open failed\nRunning without async\n");
  1360. }
  1361. #else
  1362. (void)devId;
  1363. #endif /* WOLFSSL_ASYNC_CRYPT */
  1364. #ifdef WOLF_CRYPTO_CB
  1365. #ifdef HAVE_INTEL_QA_SYNC
  1366. devId = wc_CryptoCb_InitIntelQa();
  1367. if (INVALID_DEVID == devId) {
  1368. printf("Couldn't init the Intel QA\n");
  1369. }
  1370. #endif
  1371. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  1372. devId = wc_CryptoCb_InitOcteon();
  1373. if (INVALID_DEVID == devId) {
  1374. printf("Couldn't init the Cavium Octeon\n");
  1375. }
  1376. #endif
  1377. #ifdef HAVE_RENESAS_SYNC
  1378. devId = wc_CryptoCb_CryptInitRenesasCmn(NULL, &guser_PKCbInfo);
  1379. if (devId == INVALID_DEVID) {
  1380. printf("Couldn't get the Renesas device ID\n");
  1381. }
  1382. #endif
  1383. #endif
  1384. #if defined(WOLF_CRYPTO_CB) && !defined(HAVE_HASHDRBG) && \
  1385. !defined(WC_NO_RNG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  1386. if (devId == INVALID_DEVID) {
  1387. /* for testing RNG with crypto callback register function */
  1388. devId = 100; /* any value beside -2 (INVALID_DEVID) */
  1389. wc_CryptoCb_RegisterDevice(devId, rng_crypto_cb, NULL);
  1390. }
  1391. #endif
  1392. #ifdef HAVE_SELFTEST
  1393. if ( (ret = wolfCrypt_SelfTest()) != 0)
  1394. TEST_FAIL("CAVP selftest failed!\n", ret);
  1395. else
  1396. TEST_PASS("CAVP selftest passed!\n");
  1397. #endif
  1398. if ( (ret = error_test()) != 0)
  1399. TEST_FAIL("error test failed!\n", ret);
  1400. else
  1401. TEST_PASS("error test passed!\n");
  1402. if ( (ret = memory_test()) != 0)
  1403. TEST_FAIL("MEMORY test failed!\n", ret);
  1404. else
  1405. TEST_PASS("MEMORY test passed!\n");
  1406. #ifndef NO_CODING
  1407. if ( (ret = base64_test()) != 0)
  1408. TEST_FAIL("base64 test failed!\n", ret);
  1409. else
  1410. TEST_PASS("base64 test passed!\n");
  1411. #ifdef WOLFSSL_BASE16
  1412. if ( (ret = base16_test()) != 0)
  1413. TEST_FAIL("base16 test failed!\n", ret);
  1414. else
  1415. TEST_PASS("base16 test passed!\n");
  1416. #endif
  1417. #endif /* !NO_CODING */
  1418. #ifndef NO_ASN
  1419. if ( (ret = asn_test()) != 0)
  1420. TEST_FAIL("asn test failed!\n", ret);
  1421. else
  1422. TEST_PASS("asn test passed!\n");
  1423. #endif
  1424. #ifndef WC_NO_RNG
  1425. if ( (ret = random_test()) != 0)
  1426. TEST_FAIL("RANDOM test failed!\n", ret);
  1427. else
  1428. TEST_PASS("RANDOM test passed!\n");
  1429. #endif /* WC_NO_RNG */
  1430. #ifndef NO_MD5
  1431. if ( (ret = md5_test()) != 0)
  1432. TEST_FAIL("MD5 test failed!\n", ret);
  1433. else
  1434. TEST_PASS("MD5 test passed!\n");
  1435. #endif
  1436. #ifdef WOLFSSL_MD2
  1437. if ( (ret = md2_test()) != 0)
  1438. TEST_FAIL("MD2 test failed!\n", ret);
  1439. else
  1440. TEST_PASS("MD2 test passed!\n");
  1441. #endif
  1442. #ifndef NO_MD4
  1443. if ( (ret = md4_test()) != 0)
  1444. TEST_FAIL("MD4 test failed!\n", ret);
  1445. else
  1446. TEST_PASS("MD4 test passed!\n");
  1447. #endif
  1448. #ifndef NO_SHA
  1449. if ( (ret = sha_test()) != 0)
  1450. TEST_FAIL("SHA test failed!\n", ret);
  1451. else
  1452. TEST_PASS("SHA test passed!\n");
  1453. #endif
  1454. #ifdef WOLFSSL_SHA224
  1455. if ( (ret = sha224_test()) != 0)
  1456. TEST_FAIL("SHA-224 test failed!\n", ret);
  1457. else
  1458. TEST_PASS("SHA-224 test passed!\n");
  1459. #endif
  1460. #ifndef NO_SHA256
  1461. if ( (ret = sha256_test()) != 0)
  1462. TEST_FAIL("SHA-256 test failed!\n", ret);
  1463. else
  1464. TEST_PASS("SHA-256 test passed!\n");
  1465. #endif
  1466. #ifdef WOLFSSL_SHA384
  1467. if ( (ret = sha384_test()) != 0)
  1468. TEST_FAIL("SHA-384 test failed!\n", ret);
  1469. else
  1470. TEST_PASS("SHA-384 test passed!\n");
  1471. #endif
  1472. #ifdef WOLFSSL_SHA512
  1473. if ((ret = sha512_test()) != 0) {
  1474. TEST_FAIL("SHA-512 test failed!\n", ret);
  1475. }
  1476. else {
  1477. TEST_PASS("SHA-512 test passed!\n");
  1478. }
  1479. #if !defined(WOLFSSL_NOSHA512_224) && \
  1480. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  1481. if ((ret = sha512_224_test()) != 0) {
  1482. TEST_FAIL("SHA-512/224 test failed!\n", ret);
  1483. }
  1484. else
  1485. TEST_PASS("SHA-512/224 test passed!\n");
  1486. #endif /* !defined(WOLFSSL_NOSHA512_224) && !FIPS ... */
  1487. #if !defined(WOLFSSL_NOSHA512_256) && \
  1488. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  1489. if ((ret = sha512_256_test()) != 0) {
  1490. TEST_FAIL("SHA-512/256 test failed!\n", ret);
  1491. }
  1492. else
  1493. TEST_PASS("SHA-512/256 test passed!\n");
  1494. #endif /* !defined(WOLFSSL_NOSHA512_256) & !FIPS ... */
  1495. #endif /* WOLFSSL_SHA512 */
  1496. #ifdef WOLFSSL_SHA3
  1497. if ( (ret = sha3_test()) != 0)
  1498. TEST_FAIL("SHA-3 test failed!\n", ret);
  1499. else
  1500. TEST_PASS("SHA-3 test passed!\n");
  1501. #endif
  1502. #ifdef WOLFSSL_SHAKE128
  1503. if ( (ret = shake128_test()) != 0)
  1504. TEST_FAIL("SHAKE128 test failed!\n", ret);
  1505. else
  1506. TEST_PASS("SHAKE128 test passed!\n");
  1507. #endif
  1508. #ifdef WOLFSSL_SHAKE256
  1509. if ( (ret = shake256_test()) != 0)
  1510. TEST_FAIL("SHAKE256 test failed!\n", ret);
  1511. else
  1512. TEST_PASS("SHAKE256 test passed!\n");
  1513. #endif
  1514. #ifdef WOLFSSL_SM3
  1515. if ( (ret = sm3_test()) != 0)
  1516. return err_sys("SM-3 test failed!\n", ret);
  1517. else
  1518. TEST_PASS("SM-3 test passed!\n");
  1519. #endif
  1520. #ifndef NO_HASH_WRAPPER
  1521. if ( (ret = hash_test()) != 0)
  1522. TEST_FAIL("Hash test failed!\n", ret);
  1523. else
  1524. TEST_PASS("Hash test passed!\n");
  1525. #endif
  1526. #ifdef WOLFSSL_RIPEMD
  1527. if ( (ret = ripemd_test()) != 0)
  1528. TEST_FAIL("RIPEMD test failed!\n", ret);
  1529. else
  1530. TEST_PASS("RIPEMD test passed!\n");
  1531. #endif
  1532. #ifdef HAVE_BLAKE2
  1533. if ( (ret = blake2b_test()) != 0)
  1534. TEST_FAIL("BLAKE2b test failed!\n", ret);
  1535. else
  1536. TEST_PASS("BLAKE2b test passed!\n");
  1537. #endif
  1538. #ifdef HAVE_BLAKE2S
  1539. if ( (ret = blake2s_test()) != 0)
  1540. TEST_FAIL("BLAKE2s test failed!\n", ret);
  1541. else
  1542. TEST_PASS("BLAKE2s test passed!\n");
  1543. #endif
  1544. #ifndef NO_HMAC
  1545. #if !defined(NO_MD5) && !(defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) \
  1546. && (HAVE_FIPS_VERSION >= 5))
  1547. if ( (ret = hmac_md5_test()) != 0)
  1548. TEST_FAIL("HMAC-MD5 test failed!\n", ret);
  1549. else
  1550. TEST_PASS("HMAC-MD5 test passed!\n");
  1551. #endif
  1552. #ifndef NO_SHA
  1553. if ( (ret = hmac_sha_test()) != 0)
  1554. TEST_FAIL("HMAC-SHA test failed!\n", ret);
  1555. else
  1556. TEST_PASS("HMAC-SHA test passed!\n");
  1557. #endif
  1558. #ifdef WOLFSSL_SHA224
  1559. if ( (ret = hmac_sha224_test()) != 0)
  1560. TEST_FAIL("HMAC-SHA224 test failed!\n", ret);
  1561. else
  1562. TEST_PASS("HMAC-SHA224 test passed!\n");
  1563. #endif
  1564. #ifndef NO_SHA256
  1565. if ( (ret = hmac_sha256_test()) != 0)
  1566. TEST_FAIL("HMAC-SHA256 test failed!\n", ret);
  1567. else
  1568. TEST_PASS("HMAC-SHA256 test passed!\n");
  1569. #endif
  1570. #ifdef WOLFSSL_SHA384
  1571. if ( (ret = hmac_sha384_test()) != 0)
  1572. TEST_FAIL("HMAC-SHA384 test failed!\n", ret);
  1573. else
  1574. TEST_PASS("HMAC-SHA384 test passed!\n");
  1575. #endif
  1576. #ifdef WOLFSSL_SHA512
  1577. if ( (ret = hmac_sha512_test()) != 0)
  1578. TEST_FAIL("HMAC-SHA512 test failed!\n", ret);
  1579. else
  1580. TEST_PASS("HMAC-SHA512 test passed!\n");
  1581. #endif
  1582. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  1583. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  1584. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  1585. if ( (ret = hmac_sha3_test()) != 0)
  1586. TEST_FAIL("HMAC-SHA3 test failed!\n", ret);
  1587. else
  1588. TEST_PASS("HMAC-SHA3 test passed!\n");
  1589. #endif
  1590. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  1591. PRIVATE_KEY_UNLOCK();
  1592. if ( (ret = hkdf_test()) != 0)
  1593. TEST_FAIL("HMAC-KDF test failed!\n", ret);
  1594. else
  1595. TEST_PASS("HMAC-KDF test passed!\n");
  1596. PRIVATE_KEY_LOCK();
  1597. #endif
  1598. #endif /* !NO_HMAC */
  1599. #ifdef WOLFSSL_WOLFSSH
  1600. PRIVATE_KEY_UNLOCK();
  1601. if ( (ret = sshkdf_test()) != 0)
  1602. TEST_FAIL("SSH-KDF test failed!\n", ret);
  1603. else
  1604. TEST_PASS("SSH-KDF test passed!\n");
  1605. PRIVATE_KEY_LOCK();
  1606. #endif /* WOLFSSL_WOLFSSH */
  1607. #if defined(WOLFSSL_HAVE_PRF) && !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  1608. PRIVATE_KEY_UNLOCK();
  1609. if ( (ret = prf_test()) != 0)
  1610. TEST_FAIL("PRF test failed!\n", ret);
  1611. else
  1612. TEST_PASS("PRF test passed!\n");
  1613. PRIVATE_KEY_LOCK();
  1614. #endif
  1615. #ifdef WOLFSSL_HAVE_PRF
  1616. #if defined (HAVE_HKDF) && !defined(NO_HMAC)
  1617. #ifdef WOLFSSL_BASE16
  1618. PRIVATE_KEY_UNLOCK();
  1619. if ( (ret = tls12_kdf_test()) != 0)
  1620. TEST_FAIL("TLSv1.2 KDF test failed!\n", ret);
  1621. else
  1622. TEST_PASS("TLSv1.2 KDF test passed!\n");
  1623. PRIVATE_KEY_LOCK();
  1624. #endif /* WOLFSSL_BASE16 */
  1625. #endif /* WOLFSSL_HAVE_HKDF && !NO_HMAC */
  1626. #endif /* WOLFSSL_HAVE_PRF */
  1627. #ifdef WOLFSSL_TLS13
  1628. PRIVATE_KEY_UNLOCK();
  1629. if ( (ret = tls13_kdf_test()) != 0)
  1630. TEST_FAIL("TLSv1.3 KDF test failed!\n", ret);
  1631. else
  1632. TEST_PASS("TLSv1.3 KDF test passed!\n");
  1633. PRIVATE_KEY_LOCK();
  1634. #endif /* WOLFSSL_TLS13 */
  1635. #if defined(HAVE_X963_KDF) && defined(HAVE_ECC)
  1636. if ( (ret = x963kdf_test()) != 0)
  1637. TEST_FAIL("X963-KDF test failed!\n", ret);
  1638. else
  1639. TEST_PASS("X963-KDF test passed!\n");
  1640. #endif
  1641. #if defined(HAVE_HPKE) && defined(HAVE_ECC) && defined(HAVE_AESGCM)
  1642. if ( (ret = hpke_test()) != 0)
  1643. TEST_FAIL("HPKE test failed!\n", ret);
  1644. else
  1645. TEST_PASS("HPKE test passed!\n");
  1646. #endif
  1647. #if defined(WC_SRTP_KDF)
  1648. PRIVATE_KEY_UNLOCK();
  1649. if ( (ret = srtpkdf_test()) != 0)
  1650. TEST_FAIL("SRTP KDF test failed!\n", ret);
  1651. else
  1652. TEST_PASS("SRTP KDF test passed!\n");
  1653. PRIVATE_KEY_LOCK();
  1654. #endif
  1655. #if defined(WC_KDF_NIST_SP_800_56C)
  1656. if ( (ret = nist_sp80056c_kdf_test()) != 0)
  1657. TEST_FAIL("NIST SP 800-56C KDF test failed!\n", ret);
  1658. else
  1659. TEST_PASS("NIST SP 800-56C KDF test passed!\n");
  1660. #endif
  1661. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128) && \
  1662. !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  1663. !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
  1664. if ( (ret = gmac_test()) != 0)
  1665. TEST_FAIL("GMAC test failed!\n", ret);
  1666. else
  1667. TEST_PASS("GMAC test passed!\n");
  1668. #endif
  1669. #ifdef WC_RC2
  1670. if ( (ret = rc2_test()) != 0)
  1671. TEST_FAIL("RC2 test failed!\n", ret);
  1672. else
  1673. TEST_PASS("RC2 test passed!\n");
  1674. #endif
  1675. #ifndef NO_RC4
  1676. if ( (ret = arc4_test()) != 0)
  1677. TEST_FAIL("ARC4 test failed!\n", ret);
  1678. else
  1679. TEST_PASS("ARC4 test passed!\n");
  1680. #endif
  1681. #ifdef HAVE_CHACHA
  1682. if ( (ret = chacha_test()) != 0)
  1683. TEST_FAIL("Chacha test failed!\n", ret);
  1684. else
  1685. TEST_PASS("Chacha test passed!\n");
  1686. #endif
  1687. #ifdef HAVE_XCHACHA
  1688. if ( (ret = XChaCha_test()) != 0)
  1689. TEST_FAIL("XChacha test failed!\n", ret);
  1690. else
  1691. TEST_PASS("XChacha test passed!\n");
  1692. #endif
  1693. #ifdef HAVE_POLY1305
  1694. if ( (ret = poly1305_test()) != 0)
  1695. TEST_FAIL("POLY1305 test failed!\n", ret);
  1696. else
  1697. TEST_PASS("POLY1305 test passed!\n");
  1698. #endif
  1699. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  1700. if ( (ret = chacha20_poly1305_aead_test()) != 0)
  1701. TEST_FAIL("ChaCha20-Poly1305 AEAD test failed!\n", ret);
  1702. else
  1703. TEST_PASS("ChaCha20-Poly1305 AEAD test passed!\n");
  1704. #endif
  1705. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  1706. if ( (ret = XChaCha20Poly1305_test()) != 0)
  1707. TEST_FAIL("XChaCha20-Poly1305 AEAD test failed!\n", ret);
  1708. else
  1709. TEST_PASS("XChaCha20-Poly1305 AEAD test passed!\n");
  1710. #endif
  1711. #ifndef NO_DES3
  1712. if ( (ret = des_test()) != 0)
  1713. TEST_FAIL("DES test failed!\n", ret);
  1714. else
  1715. TEST_PASS("DES test passed!\n");
  1716. #endif
  1717. #ifndef NO_DES3
  1718. if ( (ret = des3_test()) != 0)
  1719. TEST_FAIL("DES3 test failed!\n", ret);
  1720. else
  1721. TEST_PASS("DES3 test passed!\n");
  1722. #endif
  1723. #ifndef NO_AES
  1724. if ( (ret = aes_test()) != 0)
  1725. TEST_FAIL("AES test failed!\n", ret);
  1726. else
  1727. TEST_PASS("AES test passed!\n");
  1728. #if defined(WOLFSSL_AES_192) && \
  1729. !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
  1730. if ( (ret = aes192_test()) != 0)
  1731. TEST_FAIL("AES192 test failed!\n", ret);
  1732. else
  1733. TEST_PASS("AES192 test passed!\n");
  1734. #endif
  1735. #if defined(WOLFSSL_AES_256)
  1736. if ( (ret = aes256_test()) != 0)
  1737. TEST_FAIL("AES256 test failed!\n", ret);
  1738. else
  1739. TEST_PASS("AES256 test passed!\n");
  1740. #endif
  1741. #ifdef WOLFSSL_AES_OFB
  1742. if ( (ret = aesofb_test()) != 0)
  1743. TEST_FAIL("AES-OFB test failed!\n", ret);
  1744. else
  1745. TEST_PASS("AES-OFB test passed!\n");
  1746. #endif
  1747. #ifdef HAVE_AESGCM
  1748. #if !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO)
  1749. if ( (ret = aesgcm_test()) != 0)
  1750. TEST_FAIL("AES-GCM test failed!\n", ret);
  1751. #endif
  1752. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  1753. !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY) && \
  1754. !defined(WOLFSSL_KCAPI_AES) && !(defined(WOLF_CRYPTO_CB) && \
  1755. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  1756. if ((ret = aesgcm_default_test()) != 0) {
  1757. TEST_FAIL("AES-GCM test failed!\n", ret);
  1758. }
  1759. #endif
  1760. if (ret == 0) {
  1761. TEST_PASS("AES-GCM test passed!\n");
  1762. }
  1763. #endif
  1764. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  1765. if ( (ret = aesccm_test()) != 0)
  1766. TEST_FAIL("AES-CCM test failed!\n", ret);
  1767. else
  1768. TEST_PASS("AES-CCM test passed!\n");
  1769. #endif
  1770. #ifdef WOLFSSL_AES_CFB
  1771. if ( (ret = aes_cfb_test()) != 0)
  1772. TEST_FAIL("AES-CFB test failed!\n", ret);
  1773. else
  1774. TEST_PASS("AES-CFB test passed!\n");
  1775. #endif
  1776. #ifdef WOLFSSL_AES_XTS
  1777. if ( (ret = aes_xts_test()) != 0)
  1778. TEST_FAIL("AES-XTS test failed!\n", ret);
  1779. else
  1780. TEST_PASS("AES-XTS test passed!\n");
  1781. #endif
  1782. #ifdef HAVE_AES_KEYWRAP
  1783. if ( (ret = aeskeywrap_test()) != 0)
  1784. TEST_FAIL("AES Key Wrap test failed!\n", ret);
  1785. else
  1786. TEST_PASS("AES Key Wrap test passed!\n");
  1787. #endif
  1788. #ifdef WOLFSSL_AES_SIV
  1789. if ( (ret = aes_siv_test()) != 0)
  1790. TEST_FAIL("AES-SIV test failed!\n", ret);
  1791. else
  1792. TEST_PASS("AES-SIV test passed!\n");
  1793. #endif
  1794. #endif
  1795. #if defined(WOLFSSL_AES_EAX) && \
  1796. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  1797. if ( (ret = aes_eax_test()) != 0)
  1798. TEST_FAIL("AES-EAX test failed!\n", ret);
  1799. else
  1800. TEST_PASS("AES-EAX test passed!\n");
  1801. #endif /* WOLFSSL_AES_EAX */
  1802. #ifdef HAVE_ARIA
  1803. if ( (ret = ariagcm_test(MC_ALGID_ARIA_128BITKEY)) != 0)
  1804. TEST_FAIL("ARIA128 test failed!\n", ret);
  1805. else
  1806. TEST_PASS("ARIA128 test passed!\n");
  1807. if ( (ret = ariagcm_test(MC_ALGID_ARIA_192BITKEY)) != 0)
  1808. TEST_FAIL("ARIA192 test failed!\n", ret);
  1809. else
  1810. TEST_PASS("ARIA192 test passed!\n");
  1811. if ( (ret = ariagcm_test(MC_ALGID_ARIA_256BITKEY)) != 0)
  1812. TEST_FAIL("ARIA256 test failed!\n", ret);
  1813. else
  1814. TEST_PASS("ARIA256 test passed!\n");
  1815. #endif
  1816. #ifdef HAVE_CAMELLIA
  1817. if ( (ret = camellia_test()) != 0)
  1818. TEST_FAIL("CAMELLIA test failed!\n", ret);
  1819. else
  1820. TEST_PASS("CAMELLIA test passed!\n");
  1821. #endif
  1822. #ifdef WOLFSSL_SM4
  1823. if ( (ret = sm4_test()) != 0)
  1824. return err_sys("SM-4 test failed!\n", ret);
  1825. else
  1826. TEST_PASS("SM-4 test passed!\n");
  1827. #endif
  1828. #if !defined(NO_RSA) && !defined(HAVE_RENESAS_SYNC)
  1829. #ifdef WC_RSA_NO_PADDING
  1830. if ( (ret = rsa_no_pad_test()) != 0)
  1831. TEST_FAIL("RSA NOPAD test failed!\n", ret);
  1832. else
  1833. TEST_PASS("RSA NOPAD test passed!\n");
  1834. #endif
  1835. if ( (ret = rsa_test()) != 0)
  1836. TEST_FAIL("RSA test failed!\n", ret);
  1837. else
  1838. TEST_PASS("RSA test passed!\n");
  1839. #endif
  1840. #ifndef NO_DH
  1841. PRIVATE_KEY_UNLOCK();
  1842. if ( (ret = dh_test()) != 0)
  1843. TEST_FAIL("DH test failed!\n", ret);
  1844. else
  1845. TEST_PASS("DH test passed!\n");
  1846. PRIVATE_KEY_LOCK();
  1847. #endif
  1848. #ifndef NO_DSA
  1849. if ( (ret = dsa_test()) != 0)
  1850. TEST_FAIL("DSA test failed!\n", ret);
  1851. else
  1852. TEST_PASS("DSA test passed!\n");
  1853. #endif
  1854. #ifdef WOLFCRYPT_HAVE_SRP
  1855. if ( (ret = srp_test()) != 0)
  1856. TEST_FAIL("SRP test failed!\n", ret);
  1857. else
  1858. TEST_PASS("SRP test passed!\n");
  1859. #endif
  1860. #ifndef NO_PWDBASED
  1861. PRIVATE_KEY_UNLOCK();
  1862. if ( (ret = pwdbased_test()) != 0)
  1863. TEST_FAIL("PWDBASED test failed!\n", ret);
  1864. else
  1865. TEST_PASS("PWDBASED test passed!\n");
  1866. PRIVATE_KEY_LOCK();
  1867. #endif
  1868. #if defined(USE_CERT_BUFFERS_2048) && \
  1869. defined(HAVE_PKCS12) && \
  1870. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_HMAC) && \
  1871. !defined(NO_CERTS) && !defined(NO_DES3)
  1872. if ( (ret = pkcs12_test()) != 0)
  1873. TEST_FAIL("PKCS12 test failed!\n", ret);
  1874. else
  1875. TEST_PASS("PKCS12 test passed!\n");
  1876. #endif
  1877. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  1878. if ( (ret = openssl_test()) != 0)
  1879. TEST_FAIL("OPENSSL test failed!\n", ret);
  1880. else
  1881. TEST_PASS("OPENSSL test passed!\n");
  1882. if ( (ret = openSSL_evpMD_test()) != 0)
  1883. TEST_FAIL("OPENSSL (EVP MD) test failed!\n", ret);
  1884. else
  1885. TEST_PASS("OPENSSL (EVP MD) passed!\n");
  1886. if ( (ret = openssl_pkey0_test()) != 0)
  1887. TEST_FAIL("OPENSSL (PKEY0) test failed!\n", ret);
  1888. else
  1889. TEST_PASS("OPENSSL (PKEY0) passed!\n");
  1890. if ( (ret = openssl_pkey1_test()) != 0)
  1891. TEST_FAIL("OPENSSL (PKEY1) test failed!\n", ret);
  1892. else
  1893. TEST_PASS("OPENSSL (PKEY1) passed!\n");
  1894. #if !defined(WOLF_CRYPTO_CB_ONLY_RSA) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1895. if ( (ret = openssl_evpSig_test()) != 0)
  1896. TEST_FAIL("OPENSSL (EVP Sign/Verify) test failed!\n", ret);
  1897. else
  1898. TEST_PASS("OPENSSL (EVP Sign/Verify) passed!\n");
  1899. #endif
  1900. #endif
  1901. #if defined(HAVE_ECC)
  1902. PRIVATE_KEY_UNLOCK();
  1903. if ( (ret = ecc_test()) != 0)
  1904. TEST_FAIL("ECC test failed!\n", ret);
  1905. else
  1906. TEST_PASS("ECC test passed!\n");
  1907. PRIVATE_KEY_LOCK();
  1908. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  1909. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  1910. if ( (ret = ecc_encrypt_test()) != 0)
  1911. TEST_FAIL("ECC Enc test failed!\n", ret);
  1912. else
  1913. TEST_PASS("ECC Enc test passed!\n");
  1914. #endif
  1915. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  1916. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  1917. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  1918. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  1919. /* skip for ATECC508/608A, cannot import private key buffers */
  1920. if ( (ret = ecc_test_buffers()) != 0)
  1921. TEST_FAIL("ECC buffer test failed!\n", ret);
  1922. else
  1923. TEST_PASS("ECC buffer test passed!\n");
  1924. #endif
  1925. #endif
  1926. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  1927. !defined(NO_FILESYSTEM)
  1928. if ( (ret = cert_test()) != 0)
  1929. TEST_FAIL("CERT test failed!\n", ret);
  1930. else
  1931. TEST_PASS("CERT test passed!\n");
  1932. #endif
  1933. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  1934. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(WOLFSSL_GEN_CERT)
  1935. if ( (ret = certext_test()) != 0)
  1936. TEST_FAIL("CERT EXT test failed!\n", ret);
  1937. else
  1938. TEST_PASS("CERT EXT test passed!\n");
  1939. #endif
  1940. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  1941. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  1942. if ( (ret = decodedCertCache_test()) != 0)
  1943. TEST_FAIL("DECODED CERT CACHE test failed!\n", ret);
  1944. else
  1945. TEST_PASS("DECODED CERT CACHE test passed!\n");
  1946. #endif
  1947. #ifdef HAVE_CURVE25519
  1948. if ( (ret = curve25519_test()) != 0)
  1949. TEST_FAIL("CURVE25519 test failed!\n", ret);
  1950. else
  1951. TEST_PASS("CURVE25519 test passed!\n");
  1952. #endif
  1953. #ifdef HAVE_ED25519
  1954. PRIVATE_KEY_UNLOCK();
  1955. if ( (ret = ed25519_test()) != 0)
  1956. TEST_FAIL("ED25519 test failed!\n", ret);
  1957. else
  1958. TEST_PASS("ED25519 test passed!\n");
  1959. PRIVATE_KEY_LOCK();
  1960. #endif
  1961. #ifdef HAVE_CURVE448
  1962. if ( (ret = curve448_test()) != 0)
  1963. TEST_FAIL("CURVE448 test failed!\n", ret);
  1964. else
  1965. TEST_PASS("CURVE448 test passed!\n");
  1966. #endif
  1967. #ifdef HAVE_ED448
  1968. PRIVATE_KEY_UNLOCK();
  1969. if ( (ret = ed448_test()) != 0)
  1970. TEST_FAIL("ED448 test failed!\n", ret);
  1971. else
  1972. TEST_PASS("ED448 test passed!\n");
  1973. PRIVATE_KEY_LOCK();
  1974. #endif
  1975. #ifdef WOLFSSL_HAVE_KYBER
  1976. if ( (ret = kyber_test()) != 0)
  1977. TEST_FAIL("KYBER test failed!\n", ret);
  1978. else
  1979. TEST_PASS("KYBER test passed!\n");
  1980. #endif
  1981. #ifdef HAVE_DILITHIUM
  1982. if ( (ret = dilithium_test()) != 0)
  1983. TEST_FAIL("DILITHIUM test failed!\n", ret);
  1984. else
  1985. TEST_PASS("DILITHIUM test passed!\n");
  1986. #endif
  1987. #if defined(WOLFSSL_HAVE_XMSS)
  1988. #if !defined(WOLFSSL_SMALL_STACK) && WOLFSSL_XMSS_MIN_HEIGHT <= 10
  1989. if ( (ret = xmss_test_verify_only()) != 0)
  1990. TEST_FAIL("XMSS Vfy test failed!\n", ret);
  1991. else
  1992. TEST_PASS("XMSS Vfy test passed!\n");
  1993. #endif
  1994. #if !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  1995. if ( (ret = xmss_test()) != 0)
  1996. TEST_FAIL("XMSS test failed!\n", ret);
  1997. else
  1998. TEST_PASS("XMSS test passed!\n");
  1999. #endif
  2000. #endif /* if defined(WOLFSSL_HAVE_XMSS) */
  2001. #if defined(WOLFSSL_HAVE_LMS)
  2002. #if !defined(WOLFSSL_SMALL_STACK)
  2003. #if (defined(WOLFSSL_WC_LMS) && (LMS_MAX_HEIGHT >= 10)) || \
  2004. defined(HAVE_LIBLMS)
  2005. if ( (ret = lms_test_verify_only()) != 0)
  2006. TEST_FAIL("LMS Vfy test failed!\n", ret);
  2007. else
  2008. TEST_PASS("LMS Vfy test passed!\n");
  2009. #endif
  2010. #endif
  2011. #if !defined(WOLFSSL_LMS_VERIFY_ONLY)
  2012. if ( (ret = lms_test()) != 0)
  2013. TEST_FAIL("LMS test failed!\n", ret);
  2014. else
  2015. TEST_PASS("LMS test passed!\n");
  2016. #endif
  2017. #endif /* if defined(WOLFSSL_HAVE_LMS) */
  2018. #ifdef WOLFCRYPT_HAVE_ECCSI
  2019. if ( (ret = eccsi_test()) != 0)
  2020. TEST_FAIL("ECCSI test failed!\n", ret);
  2021. else
  2022. TEST_PASS("ECCSI test passed!\n");
  2023. #endif
  2024. #ifdef WOLFCRYPT_HAVE_SAKKE
  2025. if ( (ret = sakke_test()) != 0)
  2026. TEST_FAIL("SAKKE test failed!\n", ret);
  2027. else
  2028. TEST_PASS("SAKKE test passed!\n");
  2029. #endif
  2030. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  2031. if ( (ret = cmac_test()) != 0)
  2032. TEST_FAIL("CMAC test failed!\n", ret);
  2033. else
  2034. TEST_PASS("CMAC test passed!\n");
  2035. #endif
  2036. #if defined(WOLFSSL_SIPHASH)
  2037. if ( (ret = siphash_test()) != 0)
  2038. TEST_FAIL("SipHash test failed!\n", ret);
  2039. else
  2040. TEST_PASS("SipHash test passed!\n");
  2041. #endif
  2042. #ifdef HAVE_LIBZ
  2043. if ( (ret = compress_test()) != 0)
  2044. TEST_FAIL("COMPRESS test failed!\n", ret);
  2045. else
  2046. TEST_PASS("COMPRESS test passed!\n");
  2047. #endif
  2048. #ifdef HAVE_PKCS7
  2049. #ifndef NO_PKCS7_ENCRYPTED_DATA
  2050. if ( (ret = pkcs7encrypted_test()) != 0)
  2051. TEST_FAIL("PKCS7encrypted test failed!\n", ret);
  2052. else
  2053. TEST_PASS("PKCS7encrypted test passed!\n");
  2054. #endif
  2055. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  2056. if ( (ret = pkcs7compressed_test()) != 0)
  2057. TEST_FAIL("PKCS7compressed test failed!\n", ret);
  2058. else
  2059. TEST_PASS("PKCS7compressed test passed!\n");
  2060. #endif
  2061. if ( (ret = pkcs7signed_test()) != 0)
  2062. TEST_FAIL("PKCS7signed test failed!\n", ret);
  2063. else
  2064. TEST_PASS("PKCS7signed test passed!\n");
  2065. if ( (ret = pkcs7enveloped_test()) != 0)
  2066. TEST_FAIL("PKCS7enveloped test failed!\n", ret);
  2067. else
  2068. TEST_PASS("PKCS7enveloped test passed!\n");
  2069. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  2070. if ( (ret = pkcs7authenveloped_test()) != 0)
  2071. TEST_FAIL("PKCS7authenveloped test failed!\n", ret);
  2072. else
  2073. TEST_PASS("PKCS7authenveloped test passed!\n");
  2074. #endif
  2075. #endif
  2076. #if defined(WOLFSSL_PUBLIC_MP) && \
  2077. ((defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  2078. defined(USE_FAST_MATH))
  2079. if ( (ret = mp_test()) != 0)
  2080. TEST_FAIL("mp test failed!\n", ret);
  2081. else
  2082. TEST_PASS("mp test passed!\n");
  2083. #endif
  2084. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  2085. if ( (ret = prime_test()) != 0)
  2086. TEST_FAIL("prime test failed!\n", ret);
  2087. else
  2088. TEST_PASS("prime test passed!\n");
  2089. #endif
  2090. #if defined(ASN_BER_TO_DER) && \
  2091. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  2092. defined(OPENSSL_EXTRA_X509_SMALL))
  2093. if ( (ret = berder_test()) != 0)
  2094. TEST_FAIL("ber-der test failed!\n", ret);
  2095. else
  2096. TEST_PASS("ber-der test passed!\n");
  2097. #endif
  2098. if ( (ret = logging_test()) != 0)
  2099. TEST_FAIL("logging test failed!\n", ret);
  2100. else
  2101. TEST_PASS("logging test passed!\n");
  2102. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  2103. if ( (ret = time_test()) != 0)
  2104. TEST_FAIL("time test failed!\n", ret);
  2105. else
  2106. TEST_PASS("time test passed!\n");
  2107. #endif
  2108. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  2109. if ((ret = wolfcrypt_mutex_test()) != 0)
  2110. #else
  2111. if ((ret = mutex_test()) != 0)
  2112. #endif
  2113. TEST_FAIL("mutex test failed!\n", ret);
  2114. else
  2115. TEST_PASS("mutex test passed!\n");
  2116. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  2117. if ( (ret = memcb_test()) != 0)
  2118. TEST_FAIL("memcb test failed!\n", ret);
  2119. else
  2120. TEST_PASS("memcb test passed!\n");
  2121. #endif
  2122. #ifdef WOLFSSL_CAAM_BLOB
  2123. if ( (ret = blob_test()) != 0)
  2124. TEST_FAIL("blob test failed!\n", ret);
  2125. else
  2126. TEST_PASS("blob test passed!\n");
  2127. #endif
  2128. #if defined(WOLF_CRYPTO_CB) && \
  2129. !(defined(HAVE_INTEL_QAT_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC) || \
  2130. defined(WOLFSSL_QNX_CAAM) || defined(HAVE_RENESAS_SYNC))
  2131. if ( (ret = cryptocb_test()) != 0)
  2132. TEST_FAIL("crypto callback test failed!\n", ret);
  2133. else
  2134. TEST_PASS("crypto callback test passed!\n");
  2135. #endif
  2136. #ifdef WOLFSSL_CERT_PIV
  2137. if ( (ret = certpiv_test()) != 0)
  2138. TEST_FAIL("cert piv test failed!\n", ret);
  2139. else
  2140. TEST_PASS("cert piv test passed!\n");
  2141. #endif
  2142. #ifdef WOLF_CRYPTO_CB
  2143. #ifdef HAVE_INTEL_QA_SYNC
  2144. wc_CryptoCb_CleanupIntelQa(&devId);
  2145. #endif
  2146. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  2147. wc_CryptoCb_CleanupOcteon(&devId);
  2148. #endif
  2149. #endif
  2150. #ifdef WOLFSSL_ASYNC_CRYPT
  2151. wolfAsync_DevClose(&devId);
  2152. #endif
  2153. /* cleanup the thread if fixed point cache is enabled and have thread local */
  2154. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  2155. wc_ecc_fp_free();
  2156. #endif
  2157. #ifdef TEST_ALWAYS_RUN_TO_END
  2158. if (last_failed_test_ret != 0)
  2159. ret = last_failed_test_ret;
  2160. #endif
  2161. if (args)
  2162. ((func_args*)args)->return_code = ret;
  2163. /* If hardware acceleration and respective metrics tracked, show results: */
  2164. #ifdef WOLFSSL_HW_METRICS
  2165. #if defined(WOLFSSL_ESP32_CRYPT_RSA_PRI) && defined(WOLFSSL_HW_METRICS)
  2166. esp_hw_show_mp_metrics();
  2167. #endif
  2168. #endif
  2169. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(OPENSSL_EXTRA)
  2170. wolfSSL_SetGlobalHeapHint(NULL);
  2171. #endif
  2172. TEST_PASS("Test complete\n");
  2173. EXIT_TEST(ret);
  2174. } /* end of wolfcrypt_test() */
  2175. #ifndef NO_MAIN_DRIVER
  2176. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  2177. int myoptind = 0;
  2178. char* myoptarg = NULL;
  2179. #endif
  2180. /* so overall tests can pull in test function */
  2181. #if defined(WOLFSSL_ESPIDF) || defined(_WIN32_WCE)
  2182. wc_test_ret_t wolf_test_task(void)
  2183. #else
  2184. #ifndef NO_MAIN_FUNCTION
  2185. int main(int argc, char** argv)
  2186. {
  2187. return (int)wolfcrypt_test_main(argc, argv);
  2188. }
  2189. #endif
  2190. wc_test_ret_t wolfcrypt_test_main(int argc, char** argv)
  2191. #endif
  2192. {
  2193. wc_test_ret_t ret;
  2194. func_args args = { 0, 0, 0 };
  2195. #if defined(WOLFSSL_ESPIDF) || defined(WOLFSSL_SE050)
  2196. /* set dummy wallclock time. */
  2197. struct timeval utctime;
  2198. struct timezone tz;
  2199. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  2200. utctime.tv_usec = 0;
  2201. tz.tz_minuteswest = 0;
  2202. tz.tz_dsttime = 0;
  2203. settimeofday(&utctime, &tz);
  2204. #endif
  2205. #ifdef WOLFSSL_APACHE_MYNEWT
  2206. #ifdef ARCH_sim
  2207. mcu_sim_parse_args(argc, argv);
  2208. #endif
  2209. sysinit();
  2210. /* set dummy wallclock time. */
  2211. struct os_timeval utctime;
  2212. struct os_timezone tz;
  2213. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  2214. utctime.tv_usec = 0;
  2215. tz.tz_minuteswest = 0;
  2216. tz.tz_dsttime = 0;
  2217. os_settimeofday(&utctime, &tz);
  2218. #endif
  2219. #ifdef WOLFSSL_ZEPHYR
  2220. /* set dummy wallclock time. */
  2221. struct timespec utctime;
  2222. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  2223. utctime.tv_nsec = 0;
  2224. clock_settime(CLOCK_REALTIME, &utctime);
  2225. #endif
  2226. #ifdef DEVKITPRO
  2227. void *framebuffer;
  2228. GXRModeObj *rmode = NULL;
  2229. VIDEO_Init();
  2230. WPAD_Init();
  2231. rmode = VIDEO_GetPreferredMode(NULL);
  2232. #pragma GCC diagnostic ignored "-Wbad-function-cast"
  2233. framebuffer = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
  2234. #pragma GCC diagnostic pop
  2235. console_init(framebuffer,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
  2236. VIDEO_Configure(rmode);
  2237. VIDEO_SetNextFramebuffer(framebuffer);
  2238. VIDEO_SetBlack(FALSE);
  2239. VIDEO_Flush();
  2240. VIDEO_WaitVSync();
  2241. if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
  2242. #endif
  2243. #ifdef HAVE_WNR
  2244. if ((ret = wc_InitNetRandom(wnrConfigFile, NULL, 5000)) != 0) {
  2245. err_sys("Whitewood netRandom global config failed",
  2246. WC_TEST_RET_ENC_EC(ret));
  2247. return WC_TEST_RET_ENC_EC(ret);
  2248. }
  2249. #endif
  2250. #ifndef WOLFSSL_ESPIDF
  2251. args.argc = argc;
  2252. args.argv = argv;
  2253. #endif
  2254. if ((ret = wolfCrypt_Init()) != 0) {
  2255. printf("wolfCrypt_Init failed %d\n", (int)ret);
  2256. err_sys("Error with wolfCrypt_Init!\n", WC_TEST_RET_ENC_EC(ret));
  2257. }
  2258. #ifdef HAVE_WC_INTROSPECTION
  2259. printf("Math: %s\n", wc_GetMathInfo());
  2260. #endif
  2261. #ifdef HAVE_STACK_SIZE
  2262. StackSizeCheck(&args, wolfcrypt_test);
  2263. #else
  2264. wolfcrypt_test(&args);
  2265. #endif
  2266. if ((ret = wolfCrypt_Cleanup()) != 0) {
  2267. printf("wolfCrypt_Cleanup failed %d\n", (int)ret);
  2268. err_sys("Error with wolfCrypt_Cleanup!\n", WC_TEST_RET_ENC_EC(ret));
  2269. }
  2270. #ifdef HAVE_WNR
  2271. if ((ret = wc_FreeNetRandom()) < 0)
  2272. err_sys("Failed to free netRandom context",
  2273. WC_TEST_RET_ENC_EC(ret));
  2274. #endif /* HAVE_WNR */
  2275. #ifdef DOLPHIN_EMULATOR
  2276. /* Returning from main panics the emulator. Just hang
  2277. * and let the user force quit the emulator window. */
  2278. printf("args.return_code: %d\n", args.return_code);
  2279. printf("Testing complete. You may close the window now\n");
  2280. while (1);
  2281. #endif
  2282. #if defined(WOLFSSL_ESPIDF)
  2283. /* ESP_LOGI to print takes up a lot less memory than printf */
  2284. ESP_LOGI(ESPIDF_TAG, "Exiting main with return code: % d\n",
  2285. args.return_code);
  2286. #else
  2287. /* gate this for target platforms wishing to avoid printf reference */
  2288. printf("Exiting main with return code: %ld\n", (long int)args.return_code);
  2289. #endif
  2290. return args.return_code;
  2291. } /* wolfcrypt_test_main or wolf_test_task */
  2292. #endif /* NO_MAIN_DRIVER */
  2293. /* helper to save DER, convert to PEM and save PEM */
  2294. #if !defined(NO_ASN) && (defined(HAVE_ECC) || !defined(NO_DSA) || \
  2295. (!defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)))) \
  2296. && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  2297. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  2298. #define SaveDerAndPem(d, dSz, fD, fP, pT) _SaveDerAndPem(d, dSz, fD, fP, pT, WC_TEST_RET_LN)
  2299. #else
  2300. #define SaveDerAndPem(d, dSz, fD, fP, pT) _SaveDerAndPem(d, dSz, NULL, NULL, pT, WC_TEST_RET_LN)
  2301. #endif
  2302. static wc_test_ret_t _SaveDerAndPem(const byte* der, int derSz,
  2303. const char* fileDer, const char* filePem, int pemType, int calling_line)
  2304. {
  2305. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  2306. wc_test_ret_t ret;
  2307. XFILE derFile;
  2308. derFile = XFOPEN(fileDer, "wb");
  2309. if (!derFile) {
  2310. return WC_TEST_RET_ENC(calling_line, 0, WC_TEST_RET_TAG_I);
  2311. }
  2312. ret = (int)XFWRITE(der, 1, (size_t)derSz, derFile);
  2313. XFCLOSE(derFile);
  2314. if (ret != derSz) {
  2315. return WC_TEST_RET_ENC(calling_line, 1, WC_TEST_RET_TAG_I);
  2316. }
  2317. #endif
  2318. #ifdef WOLFSSL_DER_TO_PEM
  2319. if (filePem) {
  2320. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  2321. XFILE pemFile;
  2322. #endif
  2323. #ifndef WOLFSSL_NO_MALLOC
  2324. byte* pem;
  2325. #else
  2326. byte pem[1024];
  2327. #endif
  2328. int pemSz;
  2329. /* calculate PEM size */
  2330. pemSz = wc_DerToPem(der, (word32)derSz, NULL, 0, pemType);
  2331. if (pemSz < 0) {
  2332. return WC_TEST_RET_ENC(calling_line, 2, WC_TEST_RET_TAG_I);
  2333. }
  2334. #ifndef WOLFSSL_NO_MALLOC
  2335. pem = (byte*)XMALLOC(pemSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2336. if (pem == NULL) {
  2337. return WC_TEST_RET_ENC(calling_line, 3, WC_TEST_RET_TAG_I);
  2338. }
  2339. #else
  2340. if (pemSz > (int)sizeof(pem))
  2341. return BAD_FUNC_ARG;
  2342. #endif
  2343. /* Convert to PEM */
  2344. pemSz = wc_DerToPem(der, (word32)derSz, pem, pemSz, pemType);
  2345. if (pemSz < 0) {
  2346. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2347. return WC_TEST_RET_ENC(calling_line, 4, WC_TEST_RET_TAG_I);
  2348. }
  2349. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  2350. pemFile = XFOPEN(filePem, "wb");
  2351. if (!pemFile) {
  2352. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2353. return WC_TEST_RET_ENC(calling_line, 5, WC_TEST_RET_TAG_I);
  2354. }
  2355. ret = (int)XFWRITE(pem, 1, (size_t)pemSz, pemFile);
  2356. XFCLOSE(pemFile);
  2357. if (ret != pemSz) {
  2358. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2359. return WC_TEST_RET_ENC(calling_line, 6, WC_TEST_RET_TAG_I);
  2360. }
  2361. #endif
  2362. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2363. }
  2364. #endif /* WOLFSSL_DER_TO_PEM */
  2365. /* suppress unused variable warnings */
  2366. (void)der;
  2367. (void)derSz;
  2368. (void)filePem;
  2369. (void)fileDer;
  2370. (void)pemType;
  2371. (void)calling_line;
  2372. return 0;
  2373. }
  2374. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  2375. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t error_test(void)
  2376. {
  2377. const char* errStr;
  2378. char out[WOLFSSL_MAX_ERROR_SZ];
  2379. const char* unknownStr = wc_GetErrorString(0);
  2380. #ifdef NO_ERROR_STRINGS
  2381. /* Ensure a valid error code's string matches an invalid code's.
  2382. * The string is that error strings are not available.
  2383. */
  2384. WOLFSSL_ENTER("error_test NO_ERROR_STRINGS");
  2385. errStr = wc_GetErrorString(OPEN_RAN_E);
  2386. wc_ErrorString(OPEN_RAN_E, out);
  2387. if (XSTRCMP(errStr, unknownStr) != 0)
  2388. return WC_TEST_RET_ENC_NC;
  2389. if (XSTRCMP(out, unknownStr) != 0)
  2390. return WC_TEST_RET_ENC_NC;
  2391. #else
  2392. int i;
  2393. int j = 0;
  2394. /* Values that are not or no longer error codes. */
  2395. int missing[] = { -124, -166, -167, -168, -169, 0 };
  2396. /* Check that all errors have a string and it's the same through the two
  2397. * APIs. Check that the values that are not errors map to the unknown
  2398. * string.
  2399. */
  2400. for (i = MAX_CODE_E-1; i >= WC_LAST_E; i--) {
  2401. errStr = wc_GetErrorString(i);
  2402. wc_ErrorString(i, out);
  2403. if (i != missing[j]) {
  2404. if (XSTRCMP(errStr, unknownStr) == 0) {
  2405. WOLFSSL_MSG("errStr unknown");
  2406. return WC_TEST_RET_ENC_NC;
  2407. }
  2408. if (XSTRCMP(out, unknownStr) == 0) {
  2409. WOLFSSL_MSG("out unknown");
  2410. return WC_TEST_RET_ENC_NC;
  2411. }
  2412. if (XSTRCMP(errStr, out) != 0) {
  2413. WOLFSSL_MSG("errStr does not match output");
  2414. return WC_TEST_RET_ENC_NC;
  2415. }
  2416. if (XSTRLEN(errStr) >= WOLFSSL_MAX_ERROR_SZ) {
  2417. WOLFSSL_MSG("errStr too long");
  2418. return WC_TEST_RET_ENC_NC;
  2419. }
  2420. }
  2421. else {
  2422. j++;
  2423. if (XSTRCMP(errStr, unknownStr) != 0)
  2424. return WC_TEST_RET_ENC_NC;
  2425. if (XSTRCMP(out, unknownStr) != 0)
  2426. return WC_TEST_RET_ENC_NC;
  2427. }
  2428. }
  2429. /* Check if the next possible value has been given a string. */
  2430. errStr = wc_GetErrorString(i);
  2431. wc_ErrorString(i, out);
  2432. if (XSTRCMP(errStr, unknownStr) != 0)
  2433. return WC_TEST_RET_ENC_NC;
  2434. if (XSTRCMP(out, unknownStr) != 0)
  2435. return WC_TEST_RET_ENC_NC;
  2436. #endif
  2437. return 0;
  2438. }
  2439. #ifndef NO_CODING
  2440. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base64_test(void)
  2441. {
  2442. wc_test_ret_t ret;
  2443. WOLFSSL_SMALL_STACK_STATIC const byte good[] = "A+Gd\0\0\0";
  2444. WOLFSSL_SMALL_STACK_STATIC const byte goodEnd[] = "A+Gd \r\n";
  2445. WOLFSSL_SMALL_STACK_STATIC const byte good_spaces[] = " A + G d \0";
  2446. byte out[128];
  2447. word32 outLen;
  2448. #ifdef WOLFSSL_BASE64_ENCODE
  2449. byte data[3];
  2450. word32 dataLen;
  2451. byte longData[79] = { 0 };
  2452. WOLFSSL_SMALL_STACK_STATIC const byte symbols[] = "+/A=";
  2453. #endif
  2454. WOLFSSL_SMALL_STACK_STATIC const byte badSmall[] = "AAA!Gdj=";
  2455. WOLFSSL_SMALL_STACK_STATIC const byte badLarge[] = "AAA~Gdj=";
  2456. WOLFSSL_SMALL_STACK_STATIC const byte badEOL[] = "A+Gd!AA";
  2457. WOLFSSL_SMALL_STACK_STATIC const byte badPadding[] = "AA=A";
  2458. WOLFSSL_SMALL_STACK_STATIC const byte badChar[] = ",-.:;<=>?@[\\]^_`";
  2459. byte goodChar[] =
  2460. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  2461. "abcdefghijklmnopqrstuvwxyz"
  2462. "0123456789+/;";
  2463. byte charTest[] = "A+Gd\0\0\0";
  2464. int i;
  2465. WOLFSSL_ENTER("base64_test");
  2466. /* Good Base64 encodings. */
  2467. outLen = sizeof(out);
  2468. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  2469. if (ret != 0)
  2470. return WC_TEST_RET_ENC_EC(ret);
  2471. outLen = sizeof(out);
  2472. ret = Base64_Decode(goodEnd, sizeof(goodEnd), out, &outLen);
  2473. if (ret != 0)
  2474. return WC_TEST_RET_ENC_EC(ret);
  2475. outLen = sizeof(goodChar);
  2476. ret = Base64_Decode(goodChar, sizeof(goodChar), goodChar, &outLen);
  2477. if (ret != 0)
  2478. return WC_TEST_RET_ENC_EC(ret);
  2479. if (outLen != 64 / 4 * 3)
  2480. return WC_TEST_RET_ENC_NC;
  2481. outLen = sizeof(out);
  2482. ret = Base64_Decode(good_spaces, sizeof(good_spaces), out, &outLen);
  2483. if (ret != 0)
  2484. return WC_TEST_RET_ENC_EC(ret);
  2485. /* Bad parameters. */
  2486. outLen = 1;
  2487. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  2488. if (ret != BAD_FUNC_ARG)
  2489. return WC_TEST_RET_ENC_EC(ret);
  2490. outLen = sizeof(out);
  2491. ret = Base64_Decode(badEOL, sizeof(badEOL), out, &outLen);
  2492. if (ret != ASN_INPUT_E)
  2493. return WC_TEST_RET_ENC_EC(ret);
  2494. outLen = sizeof(out);
  2495. ret = Base64_Decode(badPadding, sizeof(badPadding), out, &outLen);
  2496. if (ret != ASN_INPUT_E)
  2497. return WC_TEST_RET_ENC_EC(ret);
  2498. /* Bad character at each offset 0-3. */
  2499. for (i = 0; i < 4; i++) {
  2500. outLen = sizeof(out);
  2501. ret = Base64_Decode(badSmall + i, 4, out, &outLen);
  2502. if (ret != ASN_INPUT_E)
  2503. return WC_TEST_RET_ENC_I(i);
  2504. ret = Base64_Decode(badLarge + i, 4, out, &outLen);
  2505. if (ret != ASN_INPUT_E)
  2506. return WC_TEST_RET_ENC_I(i);
  2507. }
  2508. /* Invalid character less than 0x2b */
  2509. for (i = 1; i < 0x2b; i++) {
  2510. outLen = sizeof(out);
  2511. charTest[0] = (byte)i;
  2512. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  2513. if (ret != ASN_INPUT_E)
  2514. return WC_TEST_RET_ENC_I(i);
  2515. }
  2516. /* Bad characters in range 0x2b - 0x7a. */
  2517. for (i = 0; i < (int)sizeof(badChar) - 1; i++) {
  2518. outLen = sizeof(out);
  2519. charTest[0] = badChar[i];
  2520. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  2521. if (ret != ASN_INPUT_E)
  2522. return WC_TEST_RET_ENC_I(i);
  2523. }
  2524. /* Invalid character greater than 0x7a */
  2525. for (i = 0x7b; i < 0x100; i++) {
  2526. outLen = sizeof(out);
  2527. charTest[0] = (byte)i;
  2528. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  2529. if (ret != ASN_INPUT_E)
  2530. return WC_TEST_RET_ENC_I(i);
  2531. }
  2532. #ifdef WOLFSSL_BASE64_ENCODE
  2533. /* Decode and encode all symbols - non-alphanumeric. */
  2534. dataLen = sizeof(data);
  2535. ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen);
  2536. if (ret != 0)
  2537. return WC_TEST_RET_ENC_EC(ret);
  2538. outLen = sizeof(out);
  2539. ret = Base64_Encode(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_Encode(data, dataLen, out, &outLen);
  2544. if (ret != 0)
  2545. return WC_TEST_RET_ENC_EC(ret);
  2546. outLen = 7;
  2547. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  2548. if (ret != BUFFER_E)
  2549. return WC_TEST_RET_ENC_EC(ret);
  2550. outLen = sizeof(out);
  2551. ret = Base64_EncodeEsc(data, dataLen, NULL, &outLen);
  2552. if (ret != LENGTH_ONLY_E)
  2553. return WC_TEST_RET_ENC_EC(ret);
  2554. outLen = sizeof(out);
  2555. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  2556. if (ret != 0)
  2557. return WC_TEST_RET_ENC_EC(ret);
  2558. outLen = sizeof(out);
  2559. ret = Base64_Encode_NoNl(data, dataLen, out, &outLen);
  2560. if (ret != 0)
  2561. return WC_TEST_RET_ENC_EC(ret);
  2562. /* Data that results in an encoding longer than one line. */
  2563. outLen = sizeof(out);
  2564. dataLen = sizeof(longData);
  2565. ret = Base64_Encode(longData, dataLen, out, &outLen);
  2566. if (ret != 0)
  2567. return WC_TEST_RET_ENC_EC(ret);
  2568. outLen = sizeof(out);
  2569. ret = Base64_EncodeEsc(longData, dataLen, out, &outLen);
  2570. if (ret != 0)
  2571. return WC_TEST_RET_ENC_EC(ret);
  2572. outLen = sizeof(out);
  2573. ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen);
  2574. if (ret != 0)
  2575. return WC_TEST_RET_ENC_EC(ret);
  2576. #endif
  2577. return 0;
  2578. }
  2579. #ifdef WOLFSSL_BASE16
  2580. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base16_test(void)
  2581. {
  2582. wc_test_ret_t ret;
  2583. WOLFSSL_SMALL_STACK_STATIC const byte testData[] = "SomeDataToEncode\n";
  2584. WOLFSSL_SMALL_STACK_STATIC const byte encodedTestData[] = "536F6D6544617461546F456E636F64650A00";
  2585. byte encoded[40];
  2586. word32 encodedLen;
  2587. byte plain[40];
  2588. word32 len;
  2589. WOLFSSL_ENTER("base16_test");
  2590. /* length returned includes null termination */
  2591. encodedLen = sizeof(encoded);
  2592. ret = Base16_Encode(testData, sizeof(testData), encoded, &encodedLen);
  2593. if (ret != 0)
  2594. return WC_TEST_RET_ENC_EC(ret);
  2595. len = (word32)XSTRLEN((char*)encoded);
  2596. if (len != encodedLen - 1)
  2597. return WC_TEST_RET_ENC_NC;
  2598. len = sizeof(plain);
  2599. ret = Base16_Decode(encoded, encodedLen - 1, plain, &len);
  2600. if (ret != 0)
  2601. return WC_TEST_RET_ENC_EC(ret);
  2602. if (len != sizeof(testData) || XMEMCMP(testData, plain, len) != 0)
  2603. return WC_TEST_RET_ENC_NC;
  2604. if (encodedLen != sizeof(encodedTestData) ||
  2605. XMEMCMP(encoded, encodedTestData, encodedLen) != 0) {
  2606. return WC_TEST_RET_ENC_NC;
  2607. }
  2608. return 0;
  2609. }
  2610. #endif /* WOLFSSL_BASE16 */
  2611. #endif /* !NO_CODING */
  2612. #ifndef NO_ASN
  2613. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t asn_test(void)
  2614. {
  2615. wc_test_ret_t ret;
  2616. /* ASN1 encoded date buffer */
  2617. WOLFSSL_SMALL_STACK_STATIC const byte dateBuf[] = {
  2618. 0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31,
  2619. 0x32, 0x30, 0x30, 0x37, 0x33, 0x37, 0x5a
  2620. };
  2621. byte format;
  2622. int length;
  2623. const byte* datePart;
  2624. #ifndef NO_ASN_TIME
  2625. struct tm timearg;
  2626. time_t now;
  2627. #endif
  2628. WOLFSSL_ENTER("asn_test");
  2629. ret = wc_GetDateInfo(dateBuf, (int)sizeof(dateBuf), &datePart, &format,
  2630. &length);
  2631. if (ret != 0)
  2632. return WC_TEST_RET_ENC_EC(ret);
  2633. #ifndef NO_ASN_TIME
  2634. /* Parameter Validation tests. */
  2635. if ((ret = wc_GetTime(NULL, sizeof(now))) != BAD_FUNC_ARG)
  2636. return WC_TEST_RET_ENC_EC(ret);
  2637. if ((ret = wc_GetTime(&now, 0)) != BUFFER_E)
  2638. return WC_TEST_RET_ENC_EC(ret);
  2639. now = 0;
  2640. if ((ret = wc_GetTime(&now, sizeof(now))) != 0) {
  2641. return WC_TEST_RET_ENC_EC(ret);
  2642. }
  2643. if (now == 0) {
  2644. printf("RTC/Time not set!\n");
  2645. return WC_TEST_RET_ENC_NC;
  2646. }
  2647. ret = wc_GetDateAsCalendarTime(datePart, length, format, &timearg);
  2648. if (ret != 0)
  2649. return WC_TEST_RET_ENC_EC(ret);
  2650. #endif /* !NO_ASN_TIME */
  2651. return 0;
  2652. }
  2653. #endif /* !NO_ASN */
  2654. #ifdef WOLFSSL_MD2
  2655. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md2_test(void)
  2656. {
  2657. wc_test_ret_t ret = 0;
  2658. Md2 md2;
  2659. byte hash[MD2_DIGEST_SIZE];
  2660. testVector a, b, c, d, e, f, g;
  2661. testVector test_md2[7];
  2662. int times = sizeof(test_md2) / sizeof(testVector), i;
  2663. WOLFSSL_ENTER("md2_test");
  2664. a.input = "";
  2665. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  2666. "\x27\x73";
  2667. a.inLen = XSTRLEN(a.input);
  2668. a.outLen = MD2_DIGEST_SIZE;
  2669. b.input = "a";
  2670. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  2671. "\xb5\xd1";
  2672. b.inLen = XSTRLEN(b.input);
  2673. b.outLen = MD2_DIGEST_SIZE;
  2674. c.input = "abc";
  2675. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  2676. "\xd6\xbb";
  2677. c.inLen = XSTRLEN(c.input);
  2678. c.outLen = MD2_DIGEST_SIZE;
  2679. d.input = "message digest";
  2680. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  2681. "\x06\xb0";
  2682. d.inLen = XSTRLEN(d.input);
  2683. d.outLen = MD2_DIGEST_SIZE;
  2684. e.input = "abcdefghijklmnopqrstuvwxyz";
  2685. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  2686. "\x94\x0b";
  2687. e.inLen = XSTRLEN(e.input);
  2688. e.outLen = MD2_DIGEST_SIZE;
  2689. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2690. "6789";
  2691. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  2692. "\x38\xcd";
  2693. f.inLen = XSTRLEN(f.input);
  2694. f.outLen = MD2_DIGEST_SIZE;
  2695. g.input = "1234567890123456789012345678901234567890123456789012345678"
  2696. "9012345678901234567890";
  2697. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  2698. "\xef\xd8";
  2699. g.inLen = XSTRLEN(g.input);
  2700. g.outLen = MD2_DIGEST_SIZE;
  2701. test_md2[0] = a;
  2702. test_md2[1] = b;
  2703. test_md2[2] = c;
  2704. test_md2[3] = d;
  2705. test_md2[4] = e;
  2706. test_md2[5] = f;
  2707. test_md2[6] = g;
  2708. wc_InitMd2(&md2);
  2709. for (i = 0; i < times; ++i) {
  2710. wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  2711. wc_Md2Final(&md2, hash);
  2712. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  2713. return WC_TEST_RET_ENC_I(i);
  2714. }
  2715. for (i = 0; i < times; ++i) {
  2716. ret = wc_Md2Hash((byte*)test_md2[i].input, (word32)test_md2[i].inLen, hash);
  2717. if (ret != 0) {
  2718. return WC_TEST_RET_ENC_I(i);
  2719. }
  2720. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0) {
  2721. return WC_TEST_RET_ENC_I(i);
  2722. }
  2723. }
  2724. return 0;
  2725. }
  2726. #endif
  2727. #ifndef NO_MD5
  2728. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md5_test(void)
  2729. {
  2730. wc_test_ret_t ret = 0;
  2731. wc_Md5 md5, md5Copy;
  2732. byte hash[WC_MD5_DIGEST_SIZE];
  2733. byte hashcopy[WC_MD5_DIGEST_SIZE];
  2734. testVector a, b, c, d, e, f;
  2735. testVector test_md5[6];
  2736. int times = sizeof(test_md5) / sizeof(testVector), i;
  2737. WOLFSSL_ENTER("md5_test");
  2738. a.input = "";
  2739. a.output = "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42"
  2740. "\x7e";
  2741. a.inLen = XSTRLEN(a.input);
  2742. a.outLen = WC_MD5_DIGEST_SIZE;
  2743. b.input = "abc";
  2744. b.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  2745. "\x72";
  2746. b.inLen = XSTRLEN(b.input);
  2747. b.outLen = WC_MD5_DIGEST_SIZE;
  2748. c.input = "message digest";
  2749. c.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  2750. "\xd0";
  2751. c.inLen = XSTRLEN(c.input);
  2752. c.outLen = WC_MD5_DIGEST_SIZE;
  2753. d.input = "abcdefghijklmnopqrstuvwxyz";
  2754. d.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  2755. "\x3b";
  2756. d.inLen = XSTRLEN(d.input);
  2757. d.outLen = WC_MD5_DIGEST_SIZE;
  2758. e.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2759. "6789";
  2760. e.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  2761. "\x9f";
  2762. e.inLen = XSTRLEN(e.input);
  2763. e.outLen = WC_MD5_DIGEST_SIZE;
  2764. f.input = "1234567890123456789012345678901234567890123456789012345678"
  2765. "9012345678901234567890";
  2766. f.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  2767. "\x7a";
  2768. f.inLen = XSTRLEN(f.input);
  2769. f.outLen = WC_MD5_DIGEST_SIZE;
  2770. test_md5[0] = a;
  2771. test_md5[1] = b;
  2772. test_md5[2] = c;
  2773. test_md5[3] = d;
  2774. test_md5[4] = e;
  2775. test_md5[5] = f;
  2776. ret = wc_InitMd5_ex(&md5, HEAP_HINT, devId);
  2777. if (ret != 0)
  2778. return WC_TEST_RET_ENC_EC(ret);
  2779. ret = wc_InitMd5_ex(&md5Copy, HEAP_HINT, devId);
  2780. if (ret != 0) {
  2781. wc_Md5Free(&md5);
  2782. return WC_TEST_RET_ENC_EC(ret);
  2783. }
  2784. for (i = 0; i < times; ++i) {
  2785. ret = wc_Md5Update(&md5, (byte*)test_md5[i].input,
  2786. (word32)test_md5[i].inLen);
  2787. if (ret != 0)
  2788. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2789. ret = wc_Md5GetHash(&md5, hashcopy);
  2790. if (ret != 0)
  2791. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2792. ret = wc_Md5Copy(&md5, &md5Copy);
  2793. if (ret != 0)
  2794. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2795. ret = wc_Md5Final(&md5, hash);
  2796. if (ret != 0)
  2797. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2798. wc_Md5Free(&md5Copy);
  2799. if (XMEMCMP(hash, test_md5[i].output, WC_MD5_DIGEST_SIZE) != 0)
  2800. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2801. if (XMEMCMP(hash, hashcopy, WC_MD5_DIGEST_SIZE) != 0)
  2802. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2803. }
  2804. #ifndef NO_LARGE_HASH_TEST
  2805. /* BEGIN LARGE HASH TEST */ {
  2806. byte large_input[1024];
  2807. const char* large_digest =
  2808. "\x44\xd0\x88\xce\xf1\x36\xd1\x78\xe9\xc8\xba\x84\xc3\xfd\xf6\xca";
  2809. for (i = 0; i < (int)sizeof(large_input); i++) {
  2810. large_input[i] = (byte)(i & 0xFF);
  2811. }
  2812. times = 100;
  2813. #ifdef WOLFSSL_PIC32MZ_HASH
  2814. wc_Md5SizeSet(&md5, times * sizeof(large_input));
  2815. #endif
  2816. for (i = 0; i < times; ++i) {
  2817. ret = wc_Md5Update(&md5, (byte*)large_input,
  2818. (word32)sizeof(large_input));
  2819. if (ret != 0)
  2820. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2821. }
  2822. ret = wc_Md5Final(&md5, hash);
  2823. if (ret != 0)
  2824. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2825. if (XMEMCMP(hash, large_digest, WC_MD5_DIGEST_SIZE) != 0)
  2826. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  2827. } /* END LARGE HASH TEST */
  2828. #endif /* NO_LARGE_HASH_TEST */
  2829. exit:
  2830. wc_Md5Free(&md5);
  2831. wc_Md5Free(&md5Copy);
  2832. return ret;
  2833. }
  2834. #endif /* NO_MD5 */
  2835. #ifndef NO_MD4
  2836. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md4_test(void)
  2837. {
  2838. Md4 md4;
  2839. byte hash[MD4_DIGEST_SIZE];
  2840. testVector a, b, c, d, e, f, g;
  2841. testVector test_md4[7];
  2842. int times = sizeof(test_md4) / sizeof(testVector), i;
  2843. WOLFSSL_ENTER("md4_test");
  2844. a.input = "";
  2845. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  2846. "\xc0";
  2847. a.inLen = XSTRLEN(a.input);
  2848. a.outLen = MD4_DIGEST_SIZE;
  2849. b.input = "a";
  2850. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  2851. "\x24";
  2852. b.inLen = XSTRLEN(b.input);
  2853. b.outLen = MD4_DIGEST_SIZE;
  2854. c.input = "abc";
  2855. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  2856. "\x9d";
  2857. c.inLen = XSTRLEN(c.input);
  2858. c.outLen = MD4_DIGEST_SIZE;
  2859. d.input = "message digest";
  2860. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  2861. "\x4b";
  2862. d.inLen = XSTRLEN(d.input);
  2863. d.outLen = MD4_DIGEST_SIZE;
  2864. e.input = "abcdefghijklmnopqrstuvwxyz";
  2865. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  2866. "\xa9";
  2867. e.inLen = XSTRLEN(e.input);
  2868. e.outLen = MD4_DIGEST_SIZE;
  2869. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2870. "6789";
  2871. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  2872. "\xe4";
  2873. f.inLen = XSTRLEN(f.input);
  2874. f.outLen = MD4_DIGEST_SIZE;
  2875. g.input = "1234567890123456789012345678901234567890123456789012345678"
  2876. "9012345678901234567890";
  2877. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  2878. "\x36";
  2879. g.inLen = XSTRLEN(g.input);
  2880. g.outLen = MD4_DIGEST_SIZE;
  2881. test_md4[0] = a;
  2882. test_md4[1] = b;
  2883. test_md4[2] = c;
  2884. test_md4[3] = d;
  2885. test_md4[4] = e;
  2886. test_md4[5] = f;
  2887. test_md4[6] = g;
  2888. wc_InitMd4(&md4);
  2889. for (i = 0; i < times; ++i) {
  2890. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  2891. wc_Md4Final(&md4, hash);
  2892. if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  2893. return WC_TEST_RET_ENC_I(i);
  2894. }
  2895. return 0;
  2896. }
  2897. #endif /* NO_MD4 */
  2898. #ifndef NO_SHA
  2899. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha_test(void)
  2900. {
  2901. wc_test_ret_t ret = 0;
  2902. wc_Sha sha, shaCopy;
  2903. byte hash[WC_SHA_DIGEST_SIZE];
  2904. byte hashcopy[WC_SHA_DIGEST_SIZE];
  2905. testVector a, b, c, d, e;
  2906. testVector test_sha[5];
  2907. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2908. WOLFSSL_ENTER("sha_test");
  2909. a.input = "";
  2910. a.output = "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18"
  2911. "\x90\xaf\xd8\x07\x09";
  2912. a.inLen = XSTRLEN(a.input);
  2913. a.outLen = WC_SHA_DIGEST_SIZE;
  2914. b.input = "abc";
  2915. b.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  2916. "\x6C\x9C\xD0\xD8\x9D";
  2917. b.inLen = XSTRLEN(b.input);
  2918. b.outLen = WC_SHA_DIGEST_SIZE;
  2919. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2920. c.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  2921. "\xE5\xE5\x46\x70\xF1";
  2922. c.inLen = XSTRLEN(c.input);
  2923. c.outLen = WC_SHA_DIGEST_SIZE;
  2924. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2925. "aaaaaa";
  2926. d.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  2927. "\x2A\x25\xEC\x64\x4D";
  2928. d.inLen = XSTRLEN(d.input);
  2929. d.outLen = WC_SHA_DIGEST_SIZE;
  2930. e.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2931. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2932. "aaaaaaaaaa";
  2933. e.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  2934. "\x53\x99\x5E\x26\xA0";
  2935. e.inLen = XSTRLEN(e.input);
  2936. e.outLen = WC_SHA_DIGEST_SIZE;
  2937. test_sha[0] = a;
  2938. test_sha[1] = b;
  2939. test_sha[2] = c;
  2940. test_sha[3] = d;
  2941. test_sha[4] = e;
  2942. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  2943. if (ret != 0)
  2944. return WC_TEST_RET_ENC_EC(ret);
  2945. ret = wc_InitSha_ex(&shaCopy, HEAP_HINT, devId);
  2946. if (ret != 0) {
  2947. wc_ShaFree(&sha);
  2948. return WC_TEST_RET_ENC_EC(ret);
  2949. }
  2950. for (i = 0; i < times; ++i) {
  2951. ret = wc_ShaUpdate(&sha, (byte*)test_sha[i].input,
  2952. (word32)test_sha[i].inLen);
  2953. if (ret != 0)
  2954. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2955. ret = wc_ShaGetHash(&sha, hashcopy);
  2956. if (ret != 0)
  2957. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2958. ret = wc_ShaCopy(&sha, &shaCopy);
  2959. if (ret != 0)
  2960. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2961. ret = wc_ShaFinal(&sha, hash);
  2962. if (ret != 0)
  2963. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2964. wc_ShaFree(&shaCopy);
  2965. if (XMEMCMP(hash, test_sha[i].output, WC_SHA_DIGEST_SIZE) != 0)
  2966. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2967. if (XMEMCMP(hash, hashcopy, WC_SHA_DIGEST_SIZE) != 0)
  2968. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2969. }
  2970. #ifndef NO_LARGE_HASH_TEST
  2971. /* BEGIN LARGE HASH TEST */ {
  2972. byte large_input[1024];
  2973. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2974. defined(HASH_SIZE_LIMIT)
  2975. const char* large_digest =
  2976. "\x1d\x6a\x5a\xf6\xe5\x7c\x86\xce\x7f\x7c\xaf\xd5\xdb\x08\xcd\x59"
  2977. "\x15\x8c\x6d\xb6";
  2978. #else
  2979. const char* large_digest =
  2980. "\x8b\x77\x02\x48\x39\xe8\xdb\xd3\x9a\xf4\x05\x24\x66\x12\x2d\x9e"
  2981. "\xc5\xd9\x0a\xac";
  2982. #endif
  2983. for (i = 0; i < (int)sizeof(large_input); i++) {
  2984. large_input[i] = (byte)(i & 0xFF);
  2985. }
  2986. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2987. defined(HASH_SIZE_LIMIT)
  2988. times = 20;
  2989. #else
  2990. times = 100;
  2991. #endif
  2992. #ifdef WOLFSSL_PIC32MZ_HASH
  2993. wc_ShaSizeSet(&sha, times * sizeof(large_input));
  2994. #endif
  2995. for (i = 0; i < times; ++i) {
  2996. ret = wc_ShaUpdate(&sha, (byte*)large_input,
  2997. (word32)sizeof(large_input));
  2998. if (ret != 0)
  2999. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3000. }
  3001. ret = wc_ShaFinal(&sha, hash);
  3002. if (ret != 0)
  3003. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3004. if (XMEMCMP(hash, large_digest, WC_SHA_DIGEST_SIZE) != 0)
  3005. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3006. } /* END LARGE HASH TEST */
  3007. #endif /* NO_LARGE_HASH_TEST */
  3008. exit:
  3009. wc_ShaFree(&sha);
  3010. wc_ShaFree(&shaCopy);
  3011. return ret;
  3012. }
  3013. #endif /* NO_SHA */
  3014. #ifdef WOLFSSL_RIPEMD
  3015. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ripemd_test(void)
  3016. {
  3017. RipeMd ripemd;
  3018. wc_test_ret_t ret;
  3019. byte hash[RIPEMD_DIGEST_SIZE];
  3020. testVector a, b, c, d;
  3021. testVector test_ripemd[4];
  3022. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  3023. WOLFSSL_ENTER("ripemd_test");
  3024. a.input = "abc";
  3025. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  3026. "\xb0\x87\xf1\x5a\x0b\xfc";
  3027. a.inLen = XSTRLEN(a.input);
  3028. a.outLen = RIPEMD_DIGEST_SIZE;
  3029. b.input = "message digest";
  3030. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  3031. "\x5f\xfa\x21\x59\x5f\x36";
  3032. b.inLen = XSTRLEN(b.input);
  3033. b.outLen = RIPEMD_DIGEST_SIZE;
  3034. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3035. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  3036. "\xf4\x9a\xda\x62\xeb\x2b";
  3037. c.inLen = XSTRLEN(c.input);
  3038. c.outLen = RIPEMD_DIGEST_SIZE;
  3039. d.input = "12345678901234567890123456789012345678901234567890123456"
  3040. "789012345678901234567890";
  3041. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  3042. "\x82\xbf\x63\x32\x6b\xfb";
  3043. d.inLen = XSTRLEN(d.input);
  3044. d.outLen = RIPEMD_DIGEST_SIZE;
  3045. test_ripemd[0] = a;
  3046. test_ripemd[1] = b;
  3047. test_ripemd[2] = c;
  3048. test_ripemd[3] = d;
  3049. ret = wc_InitRipeMd(&ripemd);
  3050. if (ret != 0) {
  3051. return WC_TEST_RET_ENC_EC(ret);
  3052. }
  3053. for (i = 0; i < times; ++i) {
  3054. ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  3055. (word32)test_ripemd[i].inLen);
  3056. if (ret != 0) {
  3057. return WC_TEST_RET_ENC_I(i);
  3058. }
  3059. ret = wc_RipeMdFinal(&ripemd, hash);
  3060. if (ret != 0) {
  3061. return WC_TEST_RET_ENC_I(i);
  3062. }
  3063. if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  3064. return WC_TEST_RET_ENC_I(i);
  3065. }
  3066. return 0;
  3067. }
  3068. #endif /* WOLFSSL_RIPEMD */
  3069. #ifdef HAVE_BLAKE2
  3070. #define BLAKE2B_TESTS 3
  3071. static const byte blake2b_vec[BLAKE2B_TESTS][BLAKE2B_OUTBYTES] =
  3072. {
  3073. {
  3074. 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
  3075. 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
  3076. 0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
  3077. 0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
  3078. 0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
  3079. 0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
  3080. 0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
  3081. 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
  3082. },
  3083. {
  3084. 0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
  3085. 0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
  3086. 0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
  3087. 0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
  3088. 0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
  3089. 0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
  3090. 0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
  3091. 0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
  3092. },
  3093. {
  3094. 0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
  3095. 0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
  3096. 0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
  3097. 0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
  3098. 0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
  3099. 0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
  3100. 0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
  3101. 0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
  3102. }
  3103. };
  3104. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2b_test(void)
  3105. {
  3106. Blake2b b2b;
  3107. byte digest[64];
  3108. byte input[64];
  3109. int i, ret;
  3110. WOLFSSL_ENTER("blake2b_test");
  3111. for (i = 0; i < (int)sizeof(input); i++)
  3112. input[i] = (byte)i;
  3113. for (i = 0; i < BLAKE2B_TESTS; i++) {
  3114. ret = wc_InitBlake2b(&b2b, 64);
  3115. if (ret != 0)
  3116. return WC_TEST_RET_ENC_I(i);
  3117. ret = wc_Blake2bUpdate(&b2b, input, (word32)i);
  3118. if (ret != 0)
  3119. return WC_TEST_RET_ENC_I(i);
  3120. ret = wc_Blake2bFinal(&b2b, digest, 64);
  3121. if (ret != 0)
  3122. return WC_TEST_RET_ENC_I(i);
  3123. if (XMEMCMP(digest, blake2b_vec[i], 64) != 0) {
  3124. return WC_TEST_RET_ENC_I(i);
  3125. }
  3126. }
  3127. return 0;
  3128. }
  3129. #endif /* HAVE_BLAKE2 */
  3130. #ifdef HAVE_BLAKE2S
  3131. #define BLAKE2S_TESTS 3
  3132. static const byte blake2s_vec[BLAKE2S_TESTS][BLAKE2S_OUTBYTES] =
  3133. {
  3134. {
  3135. 0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94,
  3136. 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
  3137. 0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e,
  3138. 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
  3139. },
  3140. {
  3141. 0xe3, 0x4d, 0x74, 0xdb, 0xaf, 0x4f, 0xf4, 0xc6,
  3142. 0xab, 0xd8, 0x71, 0xcc, 0x22, 0x04, 0x51, 0xd2,
  3143. 0xea, 0x26, 0x48, 0x84, 0x6c, 0x77, 0x57, 0xfb,
  3144. 0xaa, 0xc8, 0x2f, 0xe5, 0x1a, 0xd6, 0x4b, 0xea,
  3145. },
  3146. {
  3147. 0xdd, 0xad, 0x9a, 0xb1, 0x5d, 0xac, 0x45, 0x49,
  3148. 0xba, 0x42, 0xf4, 0x9d, 0x26, 0x24, 0x96, 0xbe,
  3149. 0xf6, 0xc0, 0xba, 0xe1, 0xdd, 0x34, 0x2a, 0x88,
  3150. 0x08, 0xf8, 0xea, 0x26, 0x7c, 0x6e, 0x21, 0x0c,
  3151. }
  3152. };
  3153. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2s_test(void)
  3154. {
  3155. Blake2s b2s;
  3156. byte digest[32];
  3157. byte input[64];
  3158. int i, ret;
  3159. WOLFSSL_ENTER("blake2s_test");
  3160. for (i = 0; i < (int)sizeof(input); i++)
  3161. input[i] = (byte)i;
  3162. for (i = 0; i < BLAKE2S_TESTS; i++) {
  3163. ret = wc_InitBlake2s(&b2s, 32);
  3164. if (ret != 0)
  3165. return WC_TEST_RET_ENC_I(i);
  3166. ret = wc_Blake2sUpdate(&b2s, input, (word32)i);
  3167. if (ret != 0)
  3168. return WC_TEST_RET_ENC_I(i);
  3169. ret = wc_Blake2sFinal(&b2s, digest, 32);
  3170. if (ret != 0)
  3171. return WC_TEST_RET_ENC_I(i);
  3172. if (XMEMCMP(digest, blake2s_vec[i], 32) != 0) {
  3173. return WC_TEST_RET_ENC_I(i);
  3174. }
  3175. }
  3176. return 0;
  3177. }
  3178. #endif /* HAVE_BLAKE2S */
  3179. #ifdef WOLFSSL_SHA224
  3180. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha224_test(void)
  3181. {
  3182. wc_Sha224 sha, shaCopy;
  3183. byte hash[WC_SHA224_DIGEST_SIZE];
  3184. byte hashcopy[WC_SHA224_DIGEST_SIZE];
  3185. wc_test_ret_t ret = 0;
  3186. testVector a, b, c;
  3187. testVector test_sha[3];
  3188. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3189. WOLFSSL_ENTER("sha224_test");
  3190. a.input = "";
  3191. a.output = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34"
  3192. "\xc4\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
  3193. a.inLen = XSTRLEN(a.input);
  3194. a.outLen = WC_SHA224_DIGEST_SIZE;
  3195. b.input = "abc";
  3196. b.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
  3197. "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  3198. b.inLen = XSTRLEN(b.input);
  3199. b.outLen = WC_SHA224_DIGEST_SIZE;
  3200. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3201. c.output = "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  3202. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  3203. c.inLen = XSTRLEN(c.input);
  3204. c.outLen = WC_SHA224_DIGEST_SIZE;
  3205. test_sha[0] = a;
  3206. test_sha[1] = b;
  3207. test_sha[2] = c;
  3208. ret = wc_InitSha224_ex(&sha, HEAP_HINT, devId);
  3209. if (ret != 0)
  3210. return WC_TEST_RET_ENC_EC(ret);
  3211. ret = wc_InitSha224_ex(&shaCopy, HEAP_HINT, devId);
  3212. if (ret != 0) {
  3213. wc_Sha224Free(&sha);
  3214. return WC_TEST_RET_ENC_EC(ret);
  3215. }
  3216. for (i = 0; i < times; ++i) {
  3217. ret = wc_Sha224Update(&sha, (byte*)test_sha[i].input,
  3218. (word32)test_sha[i].inLen);
  3219. if (ret != 0)
  3220. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3221. ret = wc_Sha224GetHash(&sha, hashcopy);
  3222. if (ret != 0)
  3223. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3224. ret = wc_Sha224Copy(&sha, &shaCopy);
  3225. if (ret != 0)
  3226. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3227. ret = wc_Sha224Final(&sha, hash);
  3228. if (ret != 0)
  3229. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3230. wc_Sha224Free(&shaCopy);
  3231. if (XMEMCMP(hash, test_sha[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  3232. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3233. if (XMEMCMP(hash, hashcopy, WC_SHA224_DIGEST_SIZE) != 0)
  3234. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3235. }
  3236. exit:
  3237. wc_Sha224Free(&sha);
  3238. wc_Sha224Free(&shaCopy);
  3239. return ret;
  3240. }
  3241. #endif
  3242. #ifndef NO_SHA256
  3243. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha256_test(void)
  3244. {
  3245. wc_Sha256 sha, shaCopy;
  3246. byte hash[WC_SHA256_DIGEST_SIZE];
  3247. byte hashcopy[WC_SHA256_DIGEST_SIZE];
  3248. wc_test_ret_t ret = 0;
  3249. testVector a, b, c, d;
  3250. testVector test_sha[4];
  3251. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3252. byte i_hash[WC_SHA256_DIGEST_SIZE];
  3253. byte i_hashcopy[WC_SHA256_DIGEST_SIZE];
  3254. testVector interleave_test_sha[4];
  3255. wc_Sha256 i_sha, i_shaCopy;
  3256. #endif
  3257. #ifndef NO_LARGE_HASH_TEST
  3258. #define LARGE_HASH_TEST_INPUT_SZ 1024
  3259. #ifdef WOLFSSL_SMALL_STACK
  3260. byte *large_input = NULL;
  3261. #else
  3262. byte large_input[LARGE_HASH_TEST_INPUT_SZ];
  3263. #endif
  3264. #endif
  3265. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3266. WOLFSSL_ENTER("sha256_test");
  3267. a.input = "";
  3268. a.output = "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9"
  3269. "\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52"
  3270. "\xb8\x55";
  3271. a.inLen = XSTRLEN(a.input);
  3272. a.outLen = WC_SHA256_DIGEST_SIZE;
  3273. b.input = "abc";
  3274. b.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  3275. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  3276. "\x15\xAD";
  3277. b.inLen = XSTRLEN(b.input);
  3278. b.outLen = WC_SHA256_DIGEST_SIZE;
  3279. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3280. c.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  3281. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  3282. "\x06\xC1";
  3283. c.inLen = XSTRLEN(c.input);
  3284. c.outLen = WC_SHA256_DIGEST_SIZE;
  3285. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  3286. "aaaaaa"; /* this is BLOCKSIZE length */
  3287. d.output = "\xFF\xE0\x54\xFE\x7A\xE0\xCB\x6D\xC6\x5C\x3A\xF9\xB6\x1D\x52"
  3288. "\x09\xF4\x39\x85\x1D\xB4\x3D\x0B\xA5\x99\x73\x37\xDF\x15\x46"
  3289. "\x68\xEB";
  3290. d.inLen = XSTRLEN(d.input);
  3291. d.outLen = WC_SHA256_DIGEST_SIZE;
  3292. test_sha[0] = a;
  3293. test_sha[1] = b;
  3294. test_sha[2] = c;
  3295. test_sha[3] = d;
  3296. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3297. interleave_test_sha[0] = a;
  3298. interleave_test_sha[1] = b;
  3299. interleave_test_sha[2] = c;
  3300. interleave_test_sha[3] = d;
  3301. #endif
  3302. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  3303. if (ret != 0)
  3304. return WC_TEST_RET_ENC_EC(ret);
  3305. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3306. ret = wc_InitSha256_ex(&i_sha, HEAP_HINT, devId);
  3307. if (ret != 0)
  3308. return WC_TEST_RET_ENC_EC(ret);
  3309. #endif
  3310. ret = wc_InitSha256_ex(&shaCopy, HEAP_HINT, devId);
  3311. if (ret != 0) {
  3312. wc_Sha256Free(&sha);
  3313. return WC_TEST_RET_ENC_EC(ret);
  3314. }
  3315. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3316. ret = wc_InitSha256_ex(&i_shaCopy, HEAP_HINT, devId);
  3317. if (ret != 0) {
  3318. wc_Sha256Free(&sha);
  3319. wc_Sha256Free(&i_sha);
  3320. return WC_TEST_RET_ENC_EC(ret);
  3321. }
  3322. #endif
  3323. for (i = 0; i < times; ++i) {
  3324. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,
  3325. (word32)test_sha[i].inLen);
  3326. if (ret != 0) {
  3327. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3328. }
  3329. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3330. ret = wc_Sha256Update(&i_sha, (byte*)interleave_test_sha[i].input,
  3331. (word32)interleave_test_sha[i].inLen);
  3332. if (ret != 0) {
  3333. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3334. }
  3335. #endif
  3336. ret = wc_Sha256GetHash(&sha, hashcopy);
  3337. if (ret != 0)
  3338. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3339. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3340. ret = wc_Sha256GetHash(&i_sha, i_hashcopy);
  3341. if (ret != 0)
  3342. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3343. #endif
  3344. ret = wc_Sha256Copy(&sha, &shaCopy);
  3345. if (ret != 0)
  3346. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3347. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3348. ret = wc_Sha256Copy(&i_sha, &i_shaCopy);
  3349. if (ret != 0)
  3350. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3351. #endif
  3352. ret = wc_Sha256Final(&sha, hash);
  3353. if (ret != 0)
  3354. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3355. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3356. ret = wc_Sha256Final(&i_sha, i_hash);
  3357. if (ret != 0)
  3358. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3359. #endif
  3360. wc_Sha256Free(&shaCopy);
  3361. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3362. wc_Sha256Free(&i_shaCopy);
  3363. #endif
  3364. if (XMEMCMP(hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0) {
  3365. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3366. }
  3367. if (XMEMCMP(hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0) {
  3368. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3369. }
  3370. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3371. if (XMEMCMP(i_hash, interleave_test_sha[i].output,
  3372. WC_SHA256_DIGEST_SIZE) != 0) {
  3373. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3374. }
  3375. if (XMEMCMP(i_hash, i_hashcopy, WC_SHA256_DIGEST_SIZE) != 0) {
  3376. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3377. }
  3378. if (XMEMCMP(i_hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0) {
  3379. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3380. }
  3381. if (XMEMCMP(i_hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0) {
  3382. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3383. }
  3384. #endif
  3385. }
  3386. #ifndef NO_LARGE_HASH_TEST
  3387. /* BEGIN LARGE HASH TEST */ {
  3388. #ifdef HASH_SIZE_LIMIT
  3389. WOLFSSL_SMALL_STACK_STATIC const char* large_digest =
  3390. "\xa4\x75\x9e\x7a\xa2\x03\x38\x32\x88\x66\xa2\xea\x17\xea\xf8\xc7"
  3391. "\xfe\x4e\xc6\xbb\xe3\xbb\x71\xce\xe7\xdf\x7c\x04\x61\xb3\xc2\x2f";
  3392. #else
  3393. WOLFSSL_SMALL_STACK_STATIC const char* large_digest =
  3394. "\x27\x78\x3e\x87\x96\x3a\x4e\xfb\x68\x29\xb5\x31\xc9\xba\x57\xb4"
  3395. "\x4f\x45\x79\x7f\x67\x70\xbd\x63\x7f\xbf\x0d\x80\x7c\xbd\xba\xe0";
  3396. #endif
  3397. #ifdef WOLFSSL_SMALL_STACK
  3398. large_input = (byte *)XMALLOC(LARGE_HASH_TEST_INPUT_SZ, HEAP_HINT,
  3399. DYNAMIC_TYPE_TMP_BUFFER);
  3400. if (large_input == NULL) {
  3401. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), exit);
  3402. }
  3403. #endif
  3404. for (i = 0; i < LARGE_HASH_TEST_INPUT_SZ; i++) {
  3405. large_input[i] = (byte)(i & 0xFF);
  3406. }
  3407. #ifdef HASH_SIZE_LIMIT
  3408. times = 20;
  3409. #else
  3410. times = 100;
  3411. #endif
  3412. #ifdef WOLFSSL_PIC32MZ_HASH
  3413. wc_Sha256SizeSet(&sha, times * LARGE_HASH_TEST_INPUT_SZ);
  3414. #endif
  3415. for (i = 0; i < times; ++i) {
  3416. ret = wc_Sha256Update(&sha, (byte*)large_input,
  3417. LARGE_HASH_TEST_INPUT_SZ);
  3418. if (ret != 0)
  3419. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3420. }
  3421. ret = wc_Sha256Final(&sha, hash);
  3422. if (ret != 0)
  3423. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3424. if (XMEMCMP(hash, large_digest, WC_SHA256_DIGEST_SIZE) != 0)
  3425. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3426. } /* END LARGE HASH TEST */
  3427. #undef LARGE_HASH_TEST_INPUT_SZ
  3428. #endif /* NO_LARGE_HASH_TEST */
  3429. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_FULL_HASH)
  3430. unsigned char data_hb[WC_SHA256_BLOCK_SIZE] = {
  3431. 0x61, 0x62, 0x63, 0x80, 0x00, 0x00, 0x00, 0x00,
  3432. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3433. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3434. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3435. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3436. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3437. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3438. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18
  3439. };
  3440. ret = wc_Sha256HashBlock(&sha, data_hb, hash);
  3441. if (ret != 0) {
  3442. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3443. }
  3444. if (XMEMCMP(hash, b.output, WC_SHA256_DIGEST_SIZE) != 0) {
  3445. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3446. }
  3447. #endif
  3448. exit:
  3449. #if !defined(NO_LARGE_HASH_TEST) && defined(WOLFSSL_SMALL_STACK)
  3450. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3451. #endif
  3452. wc_Sha256Free(&sha);
  3453. wc_Sha256Free(&shaCopy);
  3454. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3455. wc_Sha256Free(&i_sha);
  3456. wc_Sha256Free(&i_shaCopy);
  3457. #endif
  3458. return ret;
  3459. }
  3460. #endif
  3461. #ifdef WOLFSSL_SHA512
  3462. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_test(void)
  3463. {
  3464. /*
  3465. ** See https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512.pdf
  3466. */
  3467. wc_Sha512 sha, shaCopy;
  3468. byte hash[WC_SHA512_DIGEST_SIZE];
  3469. byte hashcopy[WC_SHA512_DIGEST_SIZE];
  3470. wc_test_ret_t ret = 0;
  3471. testVector a, b, c;
  3472. testVector test_sha[3];
  3473. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3474. wc_Sha512 i_sha, i_shaCopy;
  3475. byte i_hash[WC_SHA512_DIGEST_SIZE];
  3476. byte i_hashcopy[WC_SHA512_DIGEST_SIZE];
  3477. testVector interleave_test_sha[3];
  3478. #endif
  3479. #ifndef NO_LARGE_HASH_TEST
  3480. #define LARGE_HASH_TEST_INPUT_SZ 1024
  3481. #ifdef WOLFSSL_SMALL_STACK
  3482. byte *large_input = NULL;
  3483. #else
  3484. byte large_input[LARGE_HASH_TEST_INPUT_SZ];
  3485. #endif
  3486. #endif
  3487. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3488. WOLFSSL_ENTER("sha512_test");
  3489. a.input = "";
  3490. a.output = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80"
  3491. "\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c"
  3492. "\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87"
  3493. "\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a"
  3494. "\xf9\x27\xda\x3e";
  3495. a.inLen = XSTRLEN(a.input);
  3496. a.outLen = WC_SHA512_DIGEST_SIZE;
  3497. b.input = "abc";
  3498. b.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  3499. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  3500. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  3501. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  3502. "\xa5\x4c\xa4\x9f";
  3503. b.inLen = XSTRLEN(b.input);
  3504. b.outLen = WC_SHA512_DIGEST_SIZE;
  3505. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3506. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3507. c.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  3508. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  3509. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  3510. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  3511. "\x87\x4b\xe9\x09";
  3512. c.inLen = XSTRLEN(c.input);
  3513. c.outLen = WC_SHA512_DIGEST_SIZE;
  3514. test_sha[0] = a;
  3515. test_sha[1] = b;
  3516. test_sha[2] = c;
  3517. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3518. interleave_test_sha[0] = a;
  3519. interleave_test_sha[1] = b;
  3520. interleave_test_sha[2] = c;
  3521. #endif
  3522. ret = wc_InitSha512_ex(&sha, HEAP_HINT, devId);
  3523. if (ret != 0)
  3524. return WC_TEST_RET_ENC_EC(ret);
  3525. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3526. ret = wc_InitSha512_ex(&i_sha, HEAP_HINT, devId);
  3527. if (ret != 0)
  3528. return WC_TEST_RET_ENC_EC(ret);
  3529. #endif
  3530. ret = wc_InitSha512_ex(&shaCopy, HEAP_HINT, devId);
  3531. if (ret != 0) {
  3532. wc_Sha512Free(&sha);
  3533. return WC_TEST_RET_ENC_EC(ret);
  3534. }
  3535. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3536. ret = wc_InitSha512_ex(&i_shaCopy, HEAP_HINT, devId);
  3537. if (ret != 0) {
  3538. wc_Sha512Free(&sha);
  3539. wc_Sha512Free(&i_sha);
  3540. return WC_TEST_RET_ENC_EC(ret);
  3541. }
  3542. #endif
  3543. for (i = 0; i < times; ++i) {
  3544. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,
  3545. (word32)test_sha[i].inLen);
  3546. if (ret != 0)
  3547. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3548. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3549. ret = wc_Sha512Update(&i_sha, (byte*)interleave_test_sha[i].input,
  3550. (word32)interleave_test_sha[i].inLen);
  3551. if (ret != 0)
  3552. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3553. #endif
  3554. ret = wc_Sha512GetHash(&sha, hashcopy);
  3555. if (ret != 0)
  3556. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3557. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3558. ret = wc_Sha512GetHash(&i_sha, i_hashcopy);
  3559. if (ret != 0)
  3560. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3561. #endif
  3562. ret = wc_Sha512Copy(&sha, &shaCopy);
  3563. if (ret != 0)
  3564. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3565. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3566. ret = wc_Sha512Copy(&i_sha, &i_shaCopy);
  3567. if (ret != 0)
  3568. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3569. #endif
  3570. ret = wc_Sha512Final(&sha, hash);
  3571. if (ret != 0)
  3572. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3573. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3574. ret = wc_Sha512Final(&i_sha, i_hash);
  3575. if (ret != 0)
  3576. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3577. #endif
  3578. wc_Sha512Free(&shaCopy);
  3579. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3580. wc_Sha512Free(&i_shaCopy);
  3581. #endif
  3582. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  3583. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3584. if (XMEMCMP(hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0)
  3585. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3586. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3587. if (XMEMCMP(i_hash, interleave_test_sha[i].output,
  3588. WC_SHA512_DIGEST_SIZE) != 0) {
  3589. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3590. }
  3591. if (XMEMCMP(i_hash, i_hashcopy, WC_SHA512_DIGEST_SIZE) != 0) {
  3592. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3593. }
  3594. if (XMEMCMP(i_hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0) {
  3595. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3596. }
  3597. if (XMEMCMP(i_hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0) {
  3598. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3599. }
  3600. #endif
  3601. }
  3602. #ifndef NO_LARGE_HASH_TEST
  3603. /* BEGIN LARGE HASH TEST */ {
  3604. #ifdef HASH_SIZE_LIMIT
  3605. WOLFSSL_SMALL_STACK_STATIC const char* large_digest =
  3606. "\x30\x9B\x96\xA6\xE9\x43\x78\x30\xA3\x71\x51\x61\xC1\xEB\xE1\xBE"
  3607. "\xC8\xA5\xF9\x13\x5A\xD6\x6D\x9E\x46\x31\x31\x67\x8D\xE2\xC0\x0B"
  3608. "\x2A\x1A\x03\xE1\xF3\x48\xA7\x33\xBD\x49\xF8\xFF\xF1\xC2\xC2\x95"
  3609. "\xCB\xF0\xAF\x87\x61\x85\x58\x63\x6A\xCA\x70\x9C\x8B\x83\x3F\x5D";
  3610. #else
  3611. WOLFSSL_SMALL_STACK_STATIC const char* large_digest =
  3612. "\x5a\x1f\x73\x90\xbd\x8c\xe4\x63\x54\xce\xa0\x9b\xef\x32\x78\x2d"
  3613. "\x2e\xe7\x0d\x5e\x2f\x9d\x15\x1b\xdd\x2d\xde\x65\x0c\x7b\xfa\x83"
  3614. "\x5e\x80\x02\x13\x84\xb8\x3f\xff\x71\x62\xb5\x09\x89\x63\xe1\xdc"
  3615. "\xa5\xdc\xfc\xfa\x9d\x1a\x4d\xc0\xfa\x3a\x14\xf6\x01\x51\x90\xa4";
  3616. #endif
  3617. #ifdef WOLFSSL_SMALL_STACK
  3618. large_input = (byte *)XMALLOC(LARGE_HASH_TEST_INPUT_SZ, HEAP_HINT,
  3619. DYNAMIC_TYPE_TMP_BUFFER);
  3620. if (large_input == NULL) {
  3621. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), exit);
  3622. }
  3623. #endif
  3624. for (i = 0; i < LARGE_HASH_TEST_INPUT_SZ; i++) {
  3625. large_input[i] = (byte)(i & 0xFF);
  3626. }
  3627. #ifdef HASH_SIZE_LIMIT
  3628. times = 20;
  3629. #else
  3630. times = 100;
  3631. #endif
  3632. for (i = 0; i < times; ++i) {
  3633. ret = wc_Sha512Update(&sha, (byte*)large_input,
  3634. LARGE_HASH_TEST_INPUT_SZ);
  3635. if (ret != 0)
  3636. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3637. }
  3638. ret = wc_Sha512Final(&sha, hash);
  3639. if (ret != 0)
  3640. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3641. if (XMEMCMP(hash, large_digest, WC_SHA512_DIGEST_SIZE) != 0)
  3642. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3643. #ifndef NO_UNALIGNED_MEMORY_TEST
  3644. /* Unaligned memory access test */
  3645. for (i = 1; i < 16; i++) {
  3646. ret = wc_Sha512Update(&sha, (byte*)large_input + i,
  3647. LARGE_HASH_TEST_INPUT_SZ - i);
  3648. if (ret != 0)
  3649. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3650. ret = wc_Sha512Final(&sha, hash);
  3651. }
  3652. #endif
  3653. } /* END LARGE HASH TEST */
  3654. #undef LARGE_HASH_TEST_INPUT_SZ
  3655. #endif /* NO_LARGE_HASH_TEST */
  3656. exit:
  3657. #if !defined(NO_LARGE_HASH_TEST) && defined(WOLFSSL_SMALL_STACK)
  3658. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3659. #endif
  3660. wc_Sha512Free(&sha);
  3661. wc_Sha512Free(&shaCopy);
  3662. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3663. wc_Sha512Free(&i_sha);
  3664. wc_Sha512Free(&i_shaCopy);
  3665. #endif
  3666. return ret;
  3667. }
  3668. #if !defined(WOLFSSL_NOSHA512_224) && \
  3669. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  3670. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_224_test(void)
  3671. {
  3672. /*
  3673. ** See https://csrc.nist.gov/Projects/cryptographic-standards-and-guidelines/example-values
  3674. **
  3675. ** NIST SHA512/224 Document Example:
  3676. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512_224.pdf
  3677. */
  3678. wc_Sha512 sha, shaCopy;
  3679. byte hash[WC_SHA512_224_DIGEST_SIZE];
  3680. byte hashcopy[WC_SHA512_224_DIGEST_SIZE];
  3681. wc_test_ret_t ret = 0;
  3682. testVector a, b, c;
  3683. testVector test_sha[3];
  3684. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3685. WOLFSSL_ENTER("sha512_224_test");
  3686. a.input = "";
  3687. a.output = "\x6e\xd0\xdd\x02"
  3688. "\x80\x6f\xa8\x9e" "\x25\xde\x06\x0c"
  3689. "\x19\xd3\xac\x86" "\xca\xbb\x87\xd6"
  3690. "\xa0\xdd\xd0\x5c" "\x33\x3b\x84\xf4";
  3691. a.inLen = XSTRLEN(a.input);
  3692. a.outLen = WC_SHA512_224_DIGEST_SIZE;
  3693. /*
  3694. ** See page 1 in above document for the SHA512/224 "abc" test:
  3695. */
  3696. b.input = "abc";
  3697. /*
  3698. ** See page 1 in above document for the SHA512/224 "abc" test digest:
  3699. */
  3700. b.output = "\x46\x34\x27\x0f"
  3701. "\x70\x7b\x6a\x54" "\xda\xae\x75\x30"
  3702. "\x46\x08\x42\xe2" "\x0e\x37\xed\x26"
  3703. "\x5c\xee\xe9\xa4" "\x3e\x89\x24\xaa";
  3704. b.inLen = XSTRLEN(b.input);
  3705. b.outLen = WC_SHA512_224_DIGEST_SIZE;
  3706. /*
  3707. ** See page 4 in above for the 2-block test:
  3708. */
  3709. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3710. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3711. /*
  3712. ** see page 9 in above document for the 2-block test message digest:
  3713. */
  3714. c.output = "\x23\xfe\xc5\xbb"
  3715. "\x94\xd6\x0b\x23" "\x30\x81\x92\x64"
  3716. "\x0b\x0c\x45\x33" "\x35\xd6\x64\x73"
  3717. "\x4f\xe4\x0e\x72" "\x68\x67\x4a\xf9";
  3718. c.inLen = XSTRLEN(c.input);
  3719. c.outLen = WC_SHA512_224_DIGEST_SIZE;
  3720. test_sha[0] = a;
  3721. test_sha[1] = b;
  3722. test_sha[2] = c;
  3723. ret = wc_InitSha512_224_ex(&sha, HEAP_HINT, devId);
  3724. if (ret != 0)
  3725. return WC_TEST_RET_ENC_EC(ret);
  3726. ret = wc_InitSha512_224_ex(&shaCopy, HEAP_HINT, devId);
  3727. if (ret != 0) {
  3728. wc_Sha512_224Free(&sha);
  3729. return WC_TEST_RET_ENC_EC(ret);
  3730. }
  3731. for (i = 0; i < times; ++i) {
  3732. ret = wc_Sha512_224Update(&sha, (byte*)test_sha[i].input,
  3733. (word32)test_sha[i].inLen);
  3734. if (ret != 0)
  3735. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3736. ret = wc_Sha512_224GetHash(&sha, hashcopy);
  3737. if (ret != 0)
  3738. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3739. ret = wc_Sha512_224Copy(&sha, &shaCopy);
  3740. if (ret != 0)
  3741. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3742. ret = wc_Sha512_224Final(&sha, hash);
  3743. if (ret != 0)
  3744. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3745. wc_Sha512_224Free(&shaCopy);
  3746. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_224_DIGEST_SIZE) != 0)
  3747. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3748. if (XMEMCMP(hash, hashcopy, WC_SHA512_224_DIGEST_SIZE) != 0)
  3749. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3750. }
  3751. #ifndef NO_LARGE_HASH_TEST
  3752. /* BEGIN LARGE HASH TEST */ {
  3753. byte large_input[1024];
  3754. #ifdef HASH_SIZE_LIMIT
  3755. const char* large_digest =
  3756. "\x98\x68\xc3\xd9\xb9\xef\x17\x53"
  3757. "\x43\x66\x0e\x60\xdf\x29\xf8\xef"
  3758. "\x96\xe3\x93\x34\x8c\x6f\xc0\xeb"
  3759. "\x14\x6c\xcf\x6a";
  3760. #else
  3761. const char* large_digest =
  3762. "\x26\x5f\x98\xd1\x76\x49\x71\x4e"
  3763. "\x82\xb7\x9d\x52\x32\x67\x9d\x56"
  3764. "\x91\xf5\x88\xc3\x05\xbb\x3f\x90"
  3765. "\xe2\x4e\x85\x05";
  3766. #endif
  3767. for (i = 0; i < (int)sizeof(large_input); i++) {
  3768. large_input[i] = (byte)(i & 0xFF);
  3769. }
  3770. #ifdef HASH_SIZE_LIMIT
  3771. times = 20;
  3772. #else
  3773. times = 100;
  3774. #endif
  3775. for (i = 0; i < times; ++i) {
  3776. ret = wc_Sha512_224Update(&sha, (byte*)large_input,
  3777. (word32)sizeof(large_input));
  3778. if (ret != 0)
  3779. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3780. }
  3781. ret = wc_Sha512_224Final(&sha, hash);
  3782. if (ret != 0)
  3783. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3784. if (XMEMCMP(hash, large_digest, WC_SHA512_224_DIGEST_SIZE) != 0)
  3785. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3786. #ifndef NO_UNALIGNED_MEMORY_TEST
  3787. /* Unaligned memory access test */
  3788. for (i = 1; i < 16; i++) {
  3789. ret = wc_Sha512_224Update(&sha, (byte*)large_input + i,
  3790. (word32)sizeof(large_input) - i);
  3791. if (ret != 0)
  3792. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3793. ret = wc_Sha512_224Final(&sha, hash);
  3794. }
  3795. #endif
  3796. } /* END LARGE HASH TEST */
  3797. #endif /* NO_LARGE_HASH_TEST */
  3798. exit:
  3799. wc_Sha512_224Free(&sha);
  3800. wc_Sha512_224Free(&shaCopy);
  3801. return ret;
  3802. } /* sha512_224_test */
  3803. #endif /* !defined(WOLFSSL_NOSHA512_224) && !FIPS ... */
  3804. #if !defined(WOLFSSL_NOSHA512_256) && \
  3805. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  3806. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_256_test(void)
  3807. {
  3808. /*
  3809. ** See https://csrc.nist.gov/Projects/cryptographic-standards-and-guidelines/example-values
  3810. ** NIST SHA512/256 Document Example:
  3811. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512_256.pdf
  3812. */
  3813. wc_Sha512 sha, shaCopy;
  3814. byte hash[WC_SHA512_256_DIGEST_SIZE];
  3815. byte hashcopy[WC_SHA512_256_DIGEST_SIZE];
  3816. wc_test_ret_t ret = 0;
  3817. testVector a, b, c;
  3818. testVector test_sha[3];
  3819. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3820. WOLFSSL_ENTER("sha512_256_test");
  3821. a.input = "";
  3822. a.output = "\xc6\x72\xb8\xd1" "\xef\x56\xed\x28"
  3823. "\xab\x87\xc3\x62" "\x2c\x51\x14\x06"
  3824. "\x9b\xdd\x3a\xd7" "\xb8\xf9\x73\x74"
  3825. "\x98\xd0\xc0\x1e" "\xce\xf0\x96\x7a";
  3826. a.inLen = XSTRLEN(a.input);
  3827. a.outLen = WC_SHA512_256_DIGEST_SIZE;
  3828. /*
  3829. ** See page 1 of above document for "abc" example:
  3830. */
  3831. b.input = "abc";
  3832. /*
  3833. ** See page 4 of above document for "abc" example digest:
  3834. */
  3835. b.output = "\x53\x04\x8e\x26" "\x81\x94\x1e\xf9"
  3836. "\x9b\x2e\x29\xb7" "\x6b\x4c\x7d\xab"
  3837. "\xe4\xc2\xd0\xc6" "\x34\xfc\x6d\x46"
  3838. "\xe0\xe2\xf1\x31" "\x07\xe7\xaf\x23";
  3839. b.inLen = XSTRLEN(b.input);
  3840. b.outLen = WC_SHA512_256_DIGEST_SIZE;
  3841. /*
  3842. ** See page 4 of above document for Two Block Message Sample:
  3843. */
  3844. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3845. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3846. /*
  3847. ** See page 10 of above document for Two Block Message Sample digest:
  3848. */
  3849. c.output = "\x39\x28\xe1\x84" "\xfb\x86\x90\xf8"
  3850. "\x40\xda\x39\x88" "\x12\x1d\x31\xbe"
  3851. "\x65\xcb\x9d\x3e" "\xf8\x3e\xe6\x14"
  3852. "\x6f\xea\xc8\x61" "\xe1\x9b\x56\x3a";
  3853. c.inLen = XSTRLEN(c.input);
  3854. c.outLen = WC_SHA512_256_DIGEST_SIZE;
  3855. test_sha[0] = a;
  3856. test_sha[1] = b;
  3857. test_sha[2] = c;
  3858. ret = wc_InitSha512_256_ex(&sha, HEAP_HINT, devId);
  3859. if (ret != 0)
  3860. return WC_TEST_RET_ENC_EC(ret);
  3861. ret = wc_InitSha512_256_ex(&shaCopy, HEAP_HINT, devId);
  3862. if (ret != 0) {
  3863. wc_Sha512_256Free(&sha);
  3864. return WC_TEST_RET_ENC_EC(ret);
  3865. }
  3866. for (i = 0; i < times; ++i) {
  3867. ret = wc_Sha512_256Update(&sha, (byte*)test_sha[i].input,
  3868. (word32)test_sha[i].inLen);
  3869. if (ret != 0)
  3870. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3871. ret = wc_Sha512_256GetHash(&sha, hashcopy);
  3872. if (ret != 0)
  3873. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3874. ret = wc_Sha512_256Copy(&sha, &shaCopy);
  3875. if (ret != 0)
  3876. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3877. ret = wc_Sha512_256Final(&sha, hash);
  3878. if (ret != 0)
  3879. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3880. wc_Sha512_256Free(&shaCopy);
  3881. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_256_DIGEST_SIZE) != 0)
  3882. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3883. if (XMEMCMP(hash, hashcopy, WC_SHA512_256_DIGEST_SIZE) != 0)
  3884. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3885. }
  3886. #ifndef NO_LARGE_HASH_TEST
  3887. /* BEGIN LARGE HASH TEST */ {
  3888. byte large_input[1024];
  3889. #ifdef HASH_SIZE_LIMIT
  3890. const char* large_digest =
  3891. "\x49\xcc\xbc\x7a\x93\x0b\x02\xb8"
  3892. "\xad\x9a\x46\x51\x00\x1f\x13\x80"
  3893. "\x35\x84\x36\xf1\xf2\x3c\xeb\xd8"
  3894. "\x41\xd4\x06\x8b\x1d\x19\xad\x72";
  3895. #else
  3896. const char* large_digest =
  3897. "\x7a\xe3\x84\x05\xcb\x06\x22\x08"
  3898. "\x7e\x2c\x65\x89\x1f\x26\x45\xfd"
  3899. "\xad\xbc\x2e\x29\x83\x12\x84\x4b"
  3900. "\xf2\xa0\xde\xbe\x06\x11\xd7\x44";
  3901. #endif
  3902. for (i = 0; i < (int)sizeof(large_input); i++) {
  3903. large_input[i] = (byte)(i & 0xFF);
  3904. }
  3905. #ifdef HASH_SIZE_LIMIT
  3906. times = 20;
  3907. #else
  3908. times = 100;
  3909. #endif
  3910. for (i = 0; i < times; ++i) {
  3911. ret = wc_Sha512_256Update(&sha, (byte*)large_input,
  3912. (word32)sizeof(large_input));
  3913. if (ret != 0)
  3914. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3915. }
  3916. ret = wc_Sha512_256Final(&sha, hash);
  3917. if (ret != 0)
  3918. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3919. if (XMEMCMP(hash, large_digest, WC_SHA512_256_DIGEST_SIZE) != 0)
  3920. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3921. #ifndef NO_UNALIGNED_MEMORY_TEST
  3922. /* Unaligned memory access test */
  3923. for (i = 1; i < 16; i++) {
  3924. ret = wc_Sha512_256Update(&sha, (byte*)large_input + i,
  3925. (word32)sizeof(large_input) - i);
  3926. if (ret != 0)
  3927. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3928. ret = wc_Sha512_256Final(&sha, hash);
  3929. }
  3930. #endif
  3931. } /* END LARGE HASH TEST */
  3932. #endif /* NO_LARGE_HASH_TEST */
  3933. exit:
  3934. wc_Sha512_256Free(&sha);
  3935. wc_Sha512_256Free(&shaCopy);
  3936. return ret;
  3937. } /* sha512_256_test */
  3938. #endif /* !defined(WOLFSSL_NOSHA512_256) && !FIPS ... */
  3939. #endif /* WOLFSSL_SHA512 */
  3940. #ifdef WOLFSSL_SHA384
  3941. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha384_test(void)
  3942. {
  3943. wc_Sha384 sha, shaCopy;
  3944. byte hash[WC_SHA384_DIGEST_SIZE];
  3945. byte hashcopy[WC_SHA384_DIGEST_SIZE];
  3946. wc_test_ret_t ret = 0;
  3947. testVector a, b, c;
  3948. testVector test_sha[3];
  3949. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3950. WOLFSSL_ENTER("sha384_test");
  3951. a.input = "";
  3952. a.output = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e\xb1\xb1\xe3"
  3953. "\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c\xc7\xbf\x63\xf6"
  3954. "\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb\xd5\x1a\xd2\xf1\x48"
  3955. "\x98\xb9\x5b";
  3956. a.inLen = XSTRLEN(a.input);
  3957. a.outLen = WC_SHA384_DIGEST_SIZE;
  3958. b.input = "abc";
  3959. b.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  3960. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  3961. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  3962. "\xc8\x25\xa7";
  3963. b.inLen = XSTRLEN(b.input);
  3964. b.outLen = WC_SHA384_DIGEST_SIZE;
  3965. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3966. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3967. c.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  3968. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  3969. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  3970. "\x74\x60\x39";
  3971. c.inLen = XSTRLEN(c.input);
  3972. c.outLen = WC_SHA384_DIGEST_SIZE;
  3973. test_sha[0] = a;
  3974. test_sha[1] = b;
  3975. test_sha[2] = c;
  3976. ret = wc_InitSha384_ex(&sha, HEAP_HINT, devId);
  3977. if (ret != 0)
  3978. return WC_TEST_RET_ENC_EC(ret);
  3979. ret = wc_InitSha384_ex(&shaCopy, HEAP_HINT, devId);
  3980. if (ret != 0) {
  3981. wc_Sha384Free(&sha);
  3982. return WC_TEST_RET_ENC_EC(ret);
  3983. }
  3984. for (i = 0; i < times; ++i) {
  3985. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,
  3986. (word32)test_sha[i].inLen);
  3987. if (ret != 0)
  3988. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3989. ret = wc_Sha384GetHash(&sha, hashcopy);
  3990. if (ret != 0)
  3991. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3992. ret = wc_Sha384Copy(&sha, &shaCopy);
  3993. if (ret != 0)
  3994. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3995. ret = wc_Sha384Final(&sha, hash);
  3996. if (ret != 0)
  3997. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3998. wc_Sha384Free(&shaCopy);
  3999. if (XMEMCMP(hash, test_sha[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  4000. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4001. if (XMEMCMP(hash, hashcopy, WC_SHA384_DIGEST_SIZE) != 0)
  4002. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4003. }
  4004. #ifndef NO_LARGE_HASH_TEST
  4005. /* BEGIN LARGE HASH TEST */ {
  4006. byte large_input[1024];
  4007. #ifdef HASH_SIZE_LIMIT
  4008. const char* large_digest =
  4009. "\xB5\xAD\x66\x6F\xD9\x58\x5E\x68\xDD\x5E\x30\xD3\x95\x72\x33\xA4"
  4010. "\xE9\x4B\x99\x3A\xEF\xF8\xE1\xBF\x1F\x05\x32\xAA\x16\x00\x82\xEC"
  4011. "\x15\xDA\xF2\x75\xEE\xE9\x06\xAF\x52\x8A\x5C\xEF\x72\x81\x80\xD6";
  4012. #else
  4013. const char* large_digest =
  4014. "\x37\x01\xdb\xff\x1e\x40\x4f\xe1\xe2\xea\x0b\x40\xbb\x3b\x39\x9a"
  4015. "\xcc\xe8\x44\x8e\x7e\xe5\x64\xb5\x6b\x7f\x56\x64\xa7\x2b\x84\xe3"
  4016. "\xc5\xd7\x79\x03\x25\x90\xf7\xa4\x58\xcb\x97\xa8\x8b\xb1\xa4\x81";
  4017. #endif
  4018. for (i = 0; i < (int)sizeof(large_input); i++) {
  4019. large_input[i] = (byte)(i & 0xFF);
  4020. }
  4021. #ifdef HASH_SIZE_LIMIT
  4022. times = 20;
  4023. #else
  4024. times = 100;
  4025. #endif
  4026. for (i = 0; i < times; ++i) {
  4027. ret = wc_Sha384Update(&sha, (byte*)large_input,
  4028. (word32)sizeof(large_input));
  4029. if (ret != 0)
  4030. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4031. }
  4032. ret = wc_Sha384Final(&sha, hash);
  4033. if (ret != 0)
  4034. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4035. if (XMEMCMP(hash, large_digest, WC_SHA384_DIGEST_SIZE) != 0)
  4036. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4037. } /* END LARGE HASH TEST */
  4038. #endif /* NO_LARGE_HASH_TEST */
  4039. exit:
  4040. wc_Sha384Free(&sha);
  4041. wc_Sha384Free(&shaCopy);
  4042. return ret;
  4043. }
  4044. #endif /* WOLFSSL_SHA384 */
  4045. #ifdef WOLFSSL_SHA3
  4046. #ifndef WOLFSSL_NOSHA3_224
  4047. static wc_test_ret_t sha3_224_test(void)
  4048. {
  4049. wc_Sha3 sha;
  4050. byte hash[WC_SHA3_224_DIGEST_SIZE];
  4051. byte hashcopy[WC_SHA3_224_DIGEST_SIZE];
  4052. testVector a, b, c;
  4053. testVector test_sha[3];
  4054. wc_test_ret_t ret = 0;
  4055. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4056. a.input = "";
  4057. a.output = "\x6b\x4e\x03\x42\x36\x67\xdb\xb7\x3b\x6e\x15\x45\x4f\x0e\xb1"
  4058. "\xab\xd4\x59\x7f\x9a\x1b\x07\x8e\x3f\x5b\x5a\x6b\xc7";
  4059. a.inLen = XSTRLEN(a.input);
  4060. a.outLen = WC_SHA3_224_DIGEST_SIZE;
  4061. b.input = "abc";
  4062. b.output = "\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76"
  4063. "\x6f\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf";
  4064. b.inLen = XSTRLEN(b.input);
  4065. b.outLen = WC_SHA3_224_DIGEST_SIZE;
  4066. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4067. c.output = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55\x74\x49\x44\x79"
  4068. "\xba\x5c\x7e\x7a\xb7\x6e\xf2\x64\xea\xd0\xfc\xce\x33";
  4069. c.inLen = XSTRLEN(c.input);
  4070. c.outLen = WC_SHA3_224_DIGEST_SIZE;
  4071. test_sha[0] = a;
  4072. test_sha[1] = b;
  4073. test_sha[2] = c;
  4074. ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
  4075. if (ret != 0)
  4076. return WC_TEST_RET_ENC_EC(ret);
  4077. for (i = 0; i < times; ++i) {
  4078. ret = wc_Sha3_224_Update(&sha, (byte*)test_sha[i].input,
  4079. (word32)test_sha[i].inLen);
  4080. if (ret != 0)
  4081. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4082. ret = wc_Sha3_224_GetHash(&sha, hashcopy);
  4083. if (ret != 0)
  4084. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4085. ret = wc_Sha3_224_Final(&sha, hash);
  4086. if (ret != 0)
  4087. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4088. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_224_DIGEST_SIZE) != 0)
  4089. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4090. if (XMEMCMP(hash, hashcopy, WC_SHA3_224_DIGEST_SIZE) != 0)
  4091. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4092. }
  4093. #ifndef NO_LARGE_HASH_TEST
  4094. /* BEGIN LARGE HASH TEST */ {
  4095. byte large_input[1024];
  4096. const char* large_digest =
  4097. "\x13\xe5\xd3\x98\x7b\x94\xda\x41\x12\xc7\x1e\x92\x3a\x19"
  4098. "\x21\x20\x86\x6f\x24\xbf\x0a\x31\xbc\xfd\xd6\x70\x36\xf3";
  4099. for (i = 0; i < (int)sizeof(large_input); i++) {
  4100. large_input[i] = (byte)(i & 0xFF);
  4101. }
  4102. times = 100;
  4103. for (i = 0; i < times; ++i) {
  4104. ret = wc_Sha3_224_Update(&sha, (byte*)large_input,
  4105. (word32)sizeof(large_input));
  4106. if (ret != 0)
  4107. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4108. }
  4109. ret = wc_Sha3_224_Final(&sha, hash);
  4110. if (ret != 0)
  4111. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4112. if (XMEMCMP(hash, large_digest, WC_SHA3_224_DIGEST_SIZE) != 0)
  4113. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4114. } /* END LARGE HASH TEST */
  4115. #endif /* NO_LARGE_HASH_TEST */
  4116. exit:
  4117. wc_Sha3_224_Free(&sha);
  4118. return ret;
  4119. }
  4120. #endif /* WOLFSSL_NOSHA3_224 */
  4121. #ifndef WOLFSSL_NOSHA3_256
  4122. static wc_test_ret_t sha3_256_test(void)
  4123. {
  4124. wc_Sha3 sha;
  4125. byte hash[WC_SHA3_256_DIGEST_SIZE];
  4126. byte hashcopy[WC_SHA3_256_DIGEST_SIZE];
  4127. testVector a, b, c;
  4128. testVector test_sha[3];
  4129. wc_test_ret_t ret = 0;
  4130. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4131. byte large_input[1024];
  4132. const char* large_digest =
  4133. "\xdc\x90\xc0\xb1\x25\xdb\x2c\x34\x81\xa3\xff\xbc\x1e\x2e\x87\xeb"
  4134. "\x6d\x70\x85\x61\xe0\xe9\x63\x61\xff\xe5\x84\x4b\x1f\x68\x05\x15";
  4135. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  4136. /* test vector with hash of empty string */
  4137. const char* Keccak256EmptyOut =
  4138. "\xc5\xd2\x46\x01\x86\xf7\x23\x3c\x92\x7e\x7d\xb2\xdc\xc7\x03\xc0"
  4139. "\xe5\x00\xb6\x53\xca\x82\x27\x3b\x7b\xfa\xd8\x04\x5d\x85\xa4\x70";
  4140. #endif
  4141. /*
  4142. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-256_Msg0.pdf
  4143. */
  4144. a.input = "";
  4145. a.output = "\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6"
  4146. "\x62\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8"
  4147. "\x43\x4a";
  4148. a.inLen = XSTRLEN(a.input);
  4149. a.outLen = WC_SHA3_256_DIGEST_SIZE;
  4150. b.input = "abc";
  4151. b.output = "\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90"
  4152. "\xbd\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43"
  4153. "\x15\x32";
  4154. b.inLen = XSTRLEN(b.input);
  4155. b.outLen = WC_SHA3_256_DIGEST_SIZE;
  4156. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4157. c.output = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8\x23\x5e"
  4158. "\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32\xdd\x97\x49\x6d"
  4159. "\x33\x76";
  4160. c.inLen = XSTRLEN(c.input);
  4161. c.outLen = WC_SHA3_256_DIGEST_SIZE;
  4162. test_sha[0] = a;
  4163. test_sha[1] = b;
  4164. test_sha[2] = c;
  4165. ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
  4166. if (ret != 0)
  4167. return WC_TEST_RET_ENC_EC(ret);
  4168. for (i = 0; i < times; ++i) {
  4169. ret = wc_Sha3_256_Update(&sha, (byte*)test_sha[i].input,
  4170. (word32)test_sha[i].inLen);
  4171. if (ret != 0)
  4172. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4173. ret = wc_Sha3_256_GetHash(&sha, hashcopy);
  4174. if (ret != 0)
  4175. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4176. ret = wc_Sha3_256_Final(&sha, hash);
  4177. if (ret != 0)
  4178. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4179. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_256_DIGEST_SIZE) != 0)
  4180. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4181. if (XMEMCMP(hash, hashcopy, WC_SHA3_256_DIGEST_SIZE) != 0)
  4182. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4183. }
  4184. #ifndef NO_LARGE_HASH_TEST
  4185. /* BEGIN LARGE HASH TEST */ {
  4186. for (i = 0; i < (int)sizeof(large_input); i++) {
  4187. large_input[i] = (byte)(i & 0xFF);
  4188. }
  4189. times = 100;
  4190. for (i = 0; i < times; ++i) {
  4191. ret = wc_Sha3_256_Update(&sha, (byte*)large_input,
  4192. (word32)sizeof(large_input));
  4193. if (ret != 0)
  4194. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4195. }
  4196. ret = wc_Sha3_256_Final(&sha, hash);
  4197. if (ret != 0)
  4198. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4199. if (XMEMCMP(hash, large_digest, WC_SHA3_256_DIGEST_SIZE) != 0)
  4200. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4201. } /* END LARGE HASH TEST */
  4202. #endif /* NO_LARGE_HASH_TEST */
  4203. /* this is a software only variant of SHA3 not supported by external hardware devices */
  4204. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  4205. /* Test for Keccak256 */
  4206. ret = wc_Sha3_SetFlags(&sha, WC_HASH_SHA3_KECCAK256);
  4207. if (ret != 0) {
  4208. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4209. }
  4210. ret = wc_Sha3_256_Update(&sha, (byte*)"", 0);
  4211. if (ret != 0) {
  4212. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4213. }
  4214. ret = wc_Sha3_256_Final(&sha, hash);
  4215. if (ret != 0) {
  4216. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4217. }
  4218. if (XMEMCMP(hash, Keccak256EmptyOut, WC_SHA3_256_DIGEST_SIZE) != 0) {
  4219. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4220. }
  4221. #endif /* WOLFSSL_HASH_FLAGS && !WOLFSSL_ASYNC_CRYPT */
  4222. exit:
  4223. wc_Sha3_256_Free(&sha);
  4224. return ret;
  4225. }
  4226. #endif /* WOLFSSL_NOSHA3_256 */
  4227. #ifndef WOLFSSL_NOSHA3_384
  4228. static wc_test_ret_t sha3_384_test(void)
  4229. {
  4230. wc_Sha3 sha;
  4231. byte hash[WC_SHA3_384_DIGEST_SIZE];
  4232. byte buf[64];
  4233. #ifndef NO_INTM_HASH_TEST
  4234. byte hashcopy[WC_SHA3_384_DIGEST_SIZE];
  4235. #endif
  4236. testVector a, b, c;
  4237. testVector test_sha[3];
  4238. wc_test_ret_t ret;
  4239. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4240. /*
  4241. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-384_Msg0.pdf
  4242. */
  4243. a.input = "";
  4244. a.output = "\x0c\x63\xa7\x5b\x84\x5e\x4f\x7d\x01\x10\x7d\x85\x2e\x4c\x24"
  4245. "\x85\xc5\x1a\x50\xaa\xaa\x94\xfc\x61\x99\x5e\x71\xbb\xee\x98"
  4246. "\x3a\x2a\xc3\x71\x38\x31\x26\x4a\xdb\x47\xfb\x6b\xd1\xe0\x58"
  4247. "\xd5\xf0\x04";
  4248. a.inLen = XSTRLEN(a.input);
  4249. a.outLen = WC_SHA3_384_DIGEST_SIZE;
  4250. #if defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  4251. /* NIST test vector with a length that is a multiple of 4 */
  4252. b.input = "\x7d\x80\xb1\x60\xc4\xb5\x36\xa3\xbe\xb7\x99\x80\x59\x93\x44"
  4253. "\x04\x7c\x5f\x82\xa1\xdf\xc3\xee\xd4";
  4254. b.output = "\x04\x1c\xc5\x86\x1b\xa3\x34\x56\x3c\x61\xd4\xef\x97\x10\xd4"
  4255. "\x89\x6c\x31\x1c\x92\xed\xbe\x0d\x7c\xd5\x3e\x80\x3b\xf2\xf4"
  4256. "\xeb\x60\x57\x23\x55\x70\x77\x0c\xe8\x7c\x55\x20\xd7\xec\x14"
  4257. "\x19\x87\x22";
  4258. b.inLen = XSTRLEN(b.input);
  4259. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  4260. #else
  4261. b.input = "abc";
  4262. b.output = "\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad"
  4263. "\x8d\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b"
  4264. "\xe4\xb2\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28"
  4265. "\x37\x6d\x25";
  4266. b.inLen = XSTRLEN(b.input);
  4267. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  4268. #endif
  4269. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4270. c.output = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7\x8a\x49"
  4271. "\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd\xbc\x32\xb9\xd4"
  4272. "\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe\xa1\x9e\xef\x51\xac\xd0"
  4273. "\x65\x7c\x22";
  4274. c.inLen = XSTRLEN(c.input);
  4275. c.outLen = WC_SHA3_384_DIGEST_SIZE;
  4276. #if defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  4277. test_sha[0] = b; /* hardware acc. pre-Versal can not handle "" string */
  4278. #else
  4279. test_sha[0] = a;
  4280. #endif
  4281. test_sha[1] = b;
  4282. test_sha[2] = c;
  4283. ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
  4284. if (ret != 0)
  4285. return WC_TEST_RET_ENC_EC(ret);
  4286. for (i = 0; i < times; ++i) {
  4287. XMEMCPY(buf, test_sha[i].input, test_sha[i].inLen);
  4288. ret = wc_Sha3_384_Update(&sha, buf,
  4289. (word32)test_sha[i].inLen);
  4290. if (ret != 0)
  4291. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4292. #ifndef NO_INTM_HASH_TEST
  4293. ret = wc_Sha3_384_GetHash(&sha, hashcopy);
  4294. if (ret != 0)
  4295. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4296. #endif
  4297. ret = wc_Sha3_384_Final(&sha, hash);
  4298. if (ret != 0)
  4299. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4300. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_384_DIGEST_SIZE) != 0)
  4301. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4302. #ifndef NO_INTM_HASH_TEST
  4303. if (XMEMCMP(hash, hashcopy, WC_SHA3_384_DIGEST_SIZE) != 0)
  4304. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4305. #endif
  4306. }
  4307. #ifndef NO_LARGE_HASH_TEST
  4308. /* BEGIN LARGE HASH TEST */ {
  4309. byte large_input[1024];
  4310. const char* large_digest =
  4311. "\x30\x44\xec\x17\xef\x47\x9f\x55\x36\x11\xd6\x3f\x8a\x31\x5a\x71"
  4312. "\x8a\x71\xa7\x1d\x8e\x84\xe8\x6c\x24\x02\x2f\x7a\x08\x4e\xea\xd7"
  4313. "\x42\x36\x5d\xa8\xc2\xb7\x42\xad\xec\x19\xfb\xca\xc6\x64\xb3\xa4";
  4314. for (i = 0; i < (int)sizeof(large_input); i++) {
  4315. large_input[i] = (byte)(i & 0xFF);
  4316. }
  4317. times = 100;
  4318. for (i = 0; i < times; ++i) {
  4319. ret = wc_Sha3_384_Update(&sha, (byte*)large_input,
  4320. (word32)sizeof(large_input));
  4321. if (ret != 0)
  4322. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4323. }
  4324. ret = wc_Sha3_384_Final(&sha, hash);
  4325. if (ret != 0)
  4326. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4327. if (XMEMCMP(hash, large_digest, WC_SHA3_384_DIGEST_SIZE) != 0)
  4328. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4329. } /* END LARGE HASH TEST */
  4330. #endif /* NO_LARGE_HASH_TEST */
  4331. exit:
  4332. wc_Sha3_384_Free(&sha);
  4333. return ret;
  4334. }
  4335. #endif /* WOLFSSL_NOSHA3_384 */
  4336. #ifndef WOLFSSL_NOSHA3_512
  4337. static wc_test_ret_t sha3_512_test(void)
  4338. {
  4339. wc_Sha3 sha;
  4340. byte hash[WC_SHA3_512_DIGEST_SIZE];
  4341. byte hashcopy[WC_SHA3_512_DIGEST_SIZE];
  4342. testVector a, b, c;
  4343. testVector test_sha[3];
  4344. wc_test_ret_t ret;
  4345. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4346. /*
  4347. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-512_Msg0.pdf
  4348. */
  4349. a.input = "";
  4350. a.output = "\xa6\x9f\x73\xcc\xa2\x3a\x9a\xc5\xc8\xb5\x67\xdc\x18\x5a\x75"
  4351. "\x6e\x97\xc9\x82\x16\x4f\xe2\x58\x59\xe0\xd1\xdc\xc1\x47\x5c"
  4352. "\x80\xa6\x15\xb2\x12\x3a\xf1\xf5\xf9\x4c\x11\xe3\xe9\x40\x2c"
  4353. "\x3a\xc5\x58\xf5\x00\x19\x9d\x95\xb6\xd3\xe3\x01\x75\x85\x86"
  4354. "\x28\x1d\xcd\x26";
  4355. a.inLen = XSTRLEN(a.input);
  4356. a.outLen = WC_SHA3_512_DIGEST_SIZE;
  4357. b.input = "abc";
  4358. b.output = "\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09"
  4359. "\x6e\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2"
  4360. "\x71\x2e\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47"
  4361. "\xe3\x93\x40\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27"
  4362. "\x4e\xec\x53\xf0";
  4363. b.inLen = XSTRLEN(b.input);
  4364. b.outLen = WC_SHA3_512_DIGEST_SIZE;
  4365. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4366. c.output = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10\xfc\xa8"
  4367. "\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7\xec\x2f\x1e\x91"
  4368. "\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53\x02\xba\x1b\x0d\x8d\xc7"
  4369. "\x8c\x08\x63\x46\xb5\x33\xb4\x9c\x03\x0d\x99\xa2\x7d\xaf\x11"
  4370. "\x39\xd6\xe7\x5e";
  4371. c.inLen = XSTRLEN(c.input);
  4372. c.outLen = WC_SHA3_512_DIGEST_SIZE;
  4373. test_sha[0] = a;
  4374. test_sha[1] = b;
  4375. test_sha[2] = c;
  4376. ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
  4377. if (ret != 0)
  4378. return WC_TEST_RET_ENC_EC(ret);
  4379. for (i = 0; i < times; ++i) {
  4380. ret = wc_Sha3_512_Update(&sha, (byte*)test_sha[i].input,
  4381. (word32)test_sha[i].inLen);
  4382. if (ret != 0)
  4383. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4384. ret = wc_Sha3_512_GetHash(&sha, hashcopy);
  4385. if (ret != 0)
  4386. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4387. ret = wc_Sha3_512_Final(&sha, hash);
  4388. if (ret != 0)
  4389. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4390. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_512_DIGEST_SIZE) != 0)
  4391. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4392. if (XMEMCMP(hash, hashcopy, WC_SHA3_512_DIGEST_SIZE) != 0)
  4393. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4394. }
  4395. #ifndef NO_LARGE_HASH_TEST
  4396. /* BEGIN LARGE HASH TEST */ {
  4397. byte large_input[1024];
  4398. const char* large_digest =
  4399. "\x9c\x13\x26\xb6\x26\xb2\x94\x31\xbc\xf4\x34\xe9\x6f\xf2\xd6\x29"
  4400. "\x9a\xd0\x9b\x32\x63\x2f\x18\xa7\x5f\x23\xc9\x60\xc2\x32\x0c\xbc"
  4401. "\x57\x77\x33\xf1\x83\x81\x8a\xd3\x15\x7c\x93\xdc\x80\x9f\xed\x61"
  4402. "\x41\xa7\x5b\xfd\x32\x0e\x38\x15\xb0\x46\x3b\x7a\x4f\xfd\x44\x88";
  4403. for (i = 0; i < (int)sizeof(large_input); i++) {
  4404. large_input[i] = (byte)(i & 0xFF);
  4405. }
  4406. times = 100;
  4407. for (i = 0; i < times; ++i) {
  4408. ret = wc_Sha3_512_Update(&sha, (byte*)large_input,
  4409. (word32)sizeof(large_input));
  4410. if (ret != 0)
  4411. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4412. }
  4413. ret = wc_Sha3_512_Final(&sha, hash);
  4414. if (ret != 0)
  4415. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4416. if (XMEMCMP(hash, large_digest, WC_SHA3_512_DIGEST_SIZE) != 0)
  4417. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4418. } /* END LARGE HASH TEST */
  4419. #endif /* NO_LARGE_HASH_TEST */
  4420. exit:
  4421. wc_Sha3_512_Free(&sha);
  4422. return ret;
  4423. }
  4424. #endif /* WOLFSSL_NOSHA3_512 */
  4425. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha3_test(void)
  4426. {
  4427. wc_test_ret_t ret;
  4428. (void)ret;
  4429. WOLFSSL_ENTER("sha3_test");
  4430. #ifndef WOLFSSL_NOSHA3_224
  4431. if ((ret = sha3_224_test()) != 0)
  4432. return ret;
  4433. #endif
  4434. #ifndef WOLFSSL_NOSHA3_256
  4435. if ((ret = sha3_256_test()) != 0)
  4436. return ret;
  4437. #endif
  4438. #ifndef WOLFSSL_NOSHA3_384
  4439. if ((ret = sha3_384_test()) != 0)
  4440. return ret;
  4441. #endif
  4442. #ifndef WOLFSSL_NOSHA3_512
  4443. if ((ret = sha3_512_test()) != 0)
  4444. return ret;
  4445. #endif
  4446. return 0;
  4447. }
  4448. #endif /* WOLFSSL_SHA3 */
  4449. #ifdef WOLFSSL_SHAKE128
  4450. static wc_test_ret_t shake128_absorb_test(wc_Shake* sha, byte *large_input_buf,
  4451. size_t large_input_buf_size)
  4452. {
  4453. testVector a, b, c, d, e;
  4454. testVector test_sha[5];
  4455. wc_test_ret_t ret = 0;
  4456. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4457. static const char large_digest[] =
  4458. "\x2b\xd1\x69\x9f\xb3\x75\x40\x74\xb8\xb2\xd2\x0b\x92\x47\x9b\xfe"
  4459. "\xc9\x91\x48\xbe\xda\xa4\x09\xd7\x61\x35\x18\x05\x07\x71\xa5\x61"
  4460. "\x4d\xc4\x94\xad\xbe\x04\x7d\xad\x95\x2f\xeb\x2c\xc0\x10\x67\x43"
  4461. "\x40\xf1\x4a\x58\x1c\x54\xfa\x24\x1c\x1a\x4e\x8d\x9b\xbc\xea\xa7"
  4462. "\x32\xf2\x4c\xc7\x86\x05\x36\xdc\xb4\x42\xd8\x35\xd1\xb4\xa2\x79"
  4463. "\xa2\xe6\xee\x67\x4f\xbf\x2a\x93\x41\x88\x25\x56\x29\x90\x1a\x06"
  4464. "\xba\xfe\x9f\xa6\x1a\x74\xe8\x7e\x85\x4a\xc8\x58\x60\xb1\x7b\x18"
  4465. "\xdf\x77\x59\x46\x04\xc1\xff\x4b\x9b\xcb\xad\xfe\x91\x28\xf0\x01"
  4466. "\xc1\x33\xd0\x99\x99\x2e\x0c\x86\x84\x67\x4d\x37\xa4\x42\x45\x10"
  4467. "\xdc\x8f\xdb\x6f\xa6\x9b\xee\x8a\x60\xa5\x1f\x95\x3f\x8f\xf5\x31"
  4468. "\x4b\x1d\x48\x1e\x45\xff\x79\x5c\xbe\x72\xfc\x56\xed\x6d\x1a\x99"
  4469. "\x7f\x23\x7c\xd1\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b"
  4470. "\xa3\x60\x51\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41"
  4471. "\x77\x2c\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59"
  4472. "\x0d\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  4473. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d\xa8"
  4474. "\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb\x67\x86"
  4475. "\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b\x3d\xca\x95"
  4476. "\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47\xcf\x56\xba\x71"
  4477. "\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a\xff\xb4\xbe\x72\x26"
  4478. "\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e\xd9\xe9\xe6\xf9\xff\xa5";
  4479. byte hash[sizeof(large_digest) - 1];
  4480. /*
  4481. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE128_Msg0.pdf
  4482. */
  4483. a.input = "";
  4484. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  4485. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  4486. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  4487. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  4488. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  4489. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  4490. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  4491. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a\x04\x30\x2e\x10\xc8\xbc"
  4492. "\x1c\xbf\x1a\x0b\x3a\x51\x20\xea\x17\xcd\xa7\xcf\xad\x76\x5f"
  4493. "\x56\x23\x47\x4d\x36\x8c\xcc\xa8\xaf\x00\x07\xcd\x9f\x5e\x4c"
  4494. "\x84\x9f\x16\x7a\x58\x0b\x14\xaa\xbd\xef\xae\xe7\xee\xf4\x7c"
  4495. "\xb0\xfc\xa9";
  4496. a.inLen = XSTRLEN(a.input);
  4497. a.outLen = WC_SHA3_128_BLOCK_SIZE;
  4498. b.input = "abc";
  4499. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  4500. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  4501. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  4502. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  4503. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  4504. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  4505. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  4506. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3\x77\xcf\xb9\xcd\xe4\xc6"
  4507. "\x35\x99\xb2\x7f\x34\x62\xbb\xa4\xa0\xed\x29\x6c\x80\x1f\x9f"
  4508. "\xf7\xf5\x73\x02\xbb\x30\x76\xee\x14\x5f\x97\xa3\x2a\xe6\x8e"
  4509. "\x76\xab\x66\xc4\x8d\x51\x67\x5b\xd4\x9a\xcc\x29\x08\x2f\x56"
  4510. "\x47\x58\x4e";
  4511. b.inLen = XSTRLEN(b.input);
  4512. b.outLen = WC_SHA3_128_BLOCK_SIZE;
  4513. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4514. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  4515. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  4516. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  4517. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  4518. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  4519. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  4520. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  4521. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa\x54\x56\x54\xf7\x0a\xe5"
  4522. "\x86\xff\x10\x13\x14\x20\x77\x14\x83\xec\x92\xed\xab\x40\x8c"
  4523. "\x76\x7b\xf4\xc5\xb4\xff\xfa\xa8\x0c\x8c\xa2\x14\xd8\x4c\x4d"
  4524. "\xc7\x00\xd0\xc5\x06\x30\xb2\xff\xc3\x79\x3e\xa4\xd8\x72\x58"
  4525. "\xb4\xc9\x54";
  4526. c.inLen = XSTRLEN(c.input);
  4527. c.outLen = WC_SHA3_128_BLOCK_SIZE;
  4528. /* Taken from NIST CAVP test vectors - full rate output. */
  4529. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  4530. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  4531. "\x85\xe0";
  4532. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  4533. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  4534. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  4535. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  4536. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  4537. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  4538. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  4539. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  4540. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  4541. "\xa1\x41\x9b\xaf\x60\x52\xc0\xc1\xb4\x45\xf8\x35\x17\x57\xb0"
  4542. "\xd0\x22\x87\x21\x89\xe2\xc0\x27\x3f\x82\xd9\x69\x69\x66\x3e"
  4543. "\x55\x4d\x09";
  4544. d.inLen = 32;
  4545. d.outLen = WC_SHA3_128_BLOCK_SIZE;
  4546. /* Taken from NIST CAVP test vectors - more than one output block. */
  4547. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  4548. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  4549. "\x47\xe4";
  4550. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  4551. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  4552. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  4553. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  4554. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  4555. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  4556. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  4557. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  4558. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  4559. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  4560. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  4561. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  4562. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  4563. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  4564. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  4565. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  4566. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d"
  4567. "\xa8\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb"
  4568. "\x67\x86\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b"
  4569. "\x3d\xca\x95\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47"
  4570. "\xcf\x56\xba\x71\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a"
  4571. "\xff\xb4\xbe\x72\x26\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e"
  4572. "\xd9\xe9\xe6\xf9\xff\xa5";
  4573. e.inLen = 32;
  4574. e.outLen = 2 * WC_SHA3_128_BLOCK_SIZE;
  4575. test_sha[0] = a;
  4576. test_sha[1] = b;
  4577. test_sha[2] = c;
  4578. test_sha[3] = d;
  4579. test_sha[4] = e;
  4580. for (i = 0; i < times; ++i) {
  4581. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  4582. if (ret != 0)
  4583. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4584. ret = wc_Shake128_Absorb(sha, (byte*)test_sha[i].input,
  4585. (word32)test_sha[i].inLen);
  4586. if (ret != 0)
  4587. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4588. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  4589. (word32)test_sha[i].outLen / WC_SHA3_128_BLOCK_SIZE);
  4590. if (ret != 0)
  4591. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4592. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  4593. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4594. }
  4595. #ifndef NO_LARGE_HASH_TEST
  4596. /* BEGIN LARGE HASH TEST */ {
  4597. for (i = 0; i < (int)large_input_buf_size; i++) {
  4598. large_input_buf[i] = (byte)(i & 0xFF);
  4599. }
  4600. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  4601. if (ret != 0)
  4602. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4603. /* Absorb is non-incremental. */
  4604. ret = wc_Shake128_Absorb(sha, (byte*)large_input_buf,
  4605. (word32)large_input_buf_size);
  4606. if (ret != 0)
  4607. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4608. /* Able to squeeze out blocks incrementally. */
  4609. ret = wc_Shake128_SqueezeBlocks(sha, hash, 1);
  4610. if (ret != 0)
  4611. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4612. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  4613. ((word32)sizeof(hash) / WC_SHA3_128_BLOCK_SIZE) - 1);
  4614. if (ret != 0)
  4615. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4616. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  4617. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4618. } /* END LARGE HASH TEST */
  4619. #endif /* NO_LARGE_HASH_TEST */
  4620. exit:
  4621. return ret;
  4622. }
  4623. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake128_test(void)
  4624. {
  4625. wc_Shake sha;
  4626. byte hash[250];
  4627. testVector a, b, c, d, e;
  4628. testVector test_sha[5];
  4629. wc_test_ret_t ret = 0;
  4630. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4631. #define SHAKE128_LARGE_INPUT_BUFSIZ 1024
  4632. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4633. byte *large_input = NULL;
  4634. #else
  4635. byte large_input[SHAKE128_LARGE_INPUT_BUFSIZ];
  4636. #endif
  4637. static const char large_digest[] =
  4638. "\x88\xd7\x0e\x86\x46\x72\x6b\x3d\x7d\x22\xe1\xa9\x2d\x02\xdb\x35"
  4639. "\x92\x4f\x1b\x03\x90\xee\xa3\xce\xd1\x3a\x08\x3a\xd7\x4e\x10\xdf"
  4640. "\x09\x67\x33\x35\x4f\xdd\x38\x50\x5b\xcb\x75\xc7\xba\x65\xe5\xe8"
  4641. "\xb8\x76\xde\xc5\xee\xd7\xf1\x65\x93\x4e\x5e\xc4\xb1\xd7\x6b\xee"
  4642. "\x4b\x57\x48\xf5\x38\x49\x9e\x45\xa0\xf7\x32\xe9\x05\x26\x6a\x10"
  4643. "\x70\xd4\x7c\x19\x01\x1f\x6d\x37\xba\x7b\x74\xc2\xbc\xb6\xbc\x74"
  4644. "\xa3\x66\x6c\x9b\x11\x84\x9d\x4a\x36\xbc\x8a\x0d\x4c\xe3\x39\xfa"
  4645. "\xfa\x1b";
  4646. WOLFSSL_ENTER("shake128_test");
  4647. /*
  4648. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE128_Msg0.pdf
  4649. */
  4650. a.input = "";
  4651. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  4652. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  4653. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  4654. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  4655. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  4656. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  4657. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  4658. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a";
  4659. a.inLen = XSTRLEN(a.input);
  4660. a.outLen = 114;
  4661. b.input = "abc";
  4662. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  4663. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  4664. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  4665. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  4666. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  4667. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  4668. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  4669. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3";
  4670. b.inLen = XSTRLEN(b.input);
  4671. b.outLen = 114;
  4672. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4673. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  4674. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  4675. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  4676. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  4677. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  4678. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  4679. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  4680. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa";
  4681. c.inLen = XSTRLEN(c.input);
  4682. c.outLen = 114;
  4683. /* Taken from NIST CAVP test vectors - full rate output. */
  4684. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  4685. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  4686. "\x85\xe0";
  4687. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  4688. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  4689. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  4690. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  4691. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  4692. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  4693. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  4694. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  4695. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  4696. "\xa1";
  4697. d.inLen = 32;
  4698. d.outLen = 136;
  4699. /* Taken from NIST CAVP test vectors - more than one output block. */
  4700. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  4701. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  4702. "\x47\xe4";
  4703. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  4704. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  4705. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  4706. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  4707. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  4708. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  4709. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  4710. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  4711. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  4712. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  4713. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  4714. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  4715. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  4716. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  4717. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  4718. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  4719. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4";
  4720. e.inLen = 32;
  4721. e.outLen = 250;
  4722. test_sha[0] = a;
  4723. test_sha[1] = b;
  4724. test_sha[2] = c;
  4725. test_sha[3] = d;
  4726. test_sha[4] = e;
  4727. ret = wc_InitShake128(&sha, HEAP_HINT, devId);
  4728. if (ret != 0)
  4729. return WC_TEST_RET_ENC_EC(ret);
  4730. for (i = 0; i < times; ++i) {
  4731. ret = wc_Shake128_Update(&sha, (byte*)test_sha[i].input,
  4732. (word32)test_sha[i].inLen);
  4733. if (ret != 0)
  4734. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4735. ret = wc_Shake128_Final(&sha, hash, (word32)test_sha[i].outLen);
  4736. if (ret != 0)
  4737. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4738. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  4739. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4740. }
  4741. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4742. large_input = (byte *)XMALLOC(SHAKE128_LARGE_INPUT_BUFSIZ, NULL,
  4743. DYNAMIC_TYPE_TMP_BUFFER);
  4744. if (large_input == NULL)
  4745. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  4746. #endif
  4747. #ifndef NO_LARGE_HASH_TEST
  4748. /* BEGIN LARGE HASH TEST */ {
  4749. for (i = 0; i < SHAKE128_LARGE_INPUT_BUFSIZ; i++) {
  4750. large_input[i] = (byte)(i & 0xFF);
  4751. }
  4752. times = 100;
  4753. for (i = 0; i < times; ++i) {
  4754. ret = wc_Shake128_Update(&sha, (byte*)large_input,
  4755. SHAKE128_LARGE_INPUT_BUFSIZ);
  4756. if (ret != 0)
  4757. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4758. }
  4759. ret = wc_Shake128_Final(&sha, hash, (word32)sizeof(hash));
  4760. if (ret != 0)
  4761. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4762. if (XMEMCMP(hash, large_digest, sizeof(large_digest) - 1) != 0)
  4763. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4764. } /* END LARGE HASH TEST */
  4765. #endif /* NO_LARGE_HASH_TEST */
  4766. ret = shake128_absorb_test(&sha, large_input, SHAKE128_LARGE_INPUT_BUFSIZ);
  4767. exit:
  4768. wc_Shake128_Free(&sha);
  4769. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4770. if (large_input != NULL)
  4771. XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4772. #endif
  4773. return ret;
  4774. }
  4775. #endif
  4776. #ifdef WOLFSSL_SHAKE256
  4777. static wc_test_ret_t shake256_absorb_test(wc_Shake* sha, byte *large_input_buf,
  4778. size_t large_input_buf_size)
  4779. {
  4780. testVector a, b, c, d, e;
  4781. testVector test_sha[5];
  4782. wc_test_ret_t ret = 0;
  4783. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4784. static const char large_digest[] =
  4785. "\x21\x25\x8e\xae\x6e\x4f\xa7\xe1\xb9\x6d\xa7\xc9\x7d\x46\x03\x69"
  4786. "\x29\x0d\x81\x49\xba\x5d\xaf\x37\xfd\xeb\x25\x52\x1d\xd9\xbd\x65"
  4787. "\xfa\x99\xb9\xd1\x70\x6b\xeb\xd4\xc1\x2c\xea\x24\x20\x27\xa7\xcd"
  4788. "\xfa\xe1\x81\xd9\xd5\xc1\x1c\xc7\xe9\x70\xc3\xc7\x21\x6f\x32\x22"
  4789. "\xe3\x27\xdb\x58\x5e\xea\x18\x2d\x63\x4d\x14\x6c\x94\xcf\x2b\x7e"
  4790. "\x6e\x2a\x74\xf3\xe0\xac\xb3\xb2\xcc\xef\x38\xe9\xe7\x35\xb3\xc5"
  4791. "\x77\x9d\xff\xe3\x08\x8e\xf8\x2c\x89\xbb\x45\x22\x16\x99\x91\xc0"
  4792. "\xe7\x71\x57\x75\xc5\xb1\xc6\xaf\x27\xcb\x64\x8c\xc4\xee\x3d\x5f"
  4793. "\x4c\x35\xfb\x1c\xf3\xf8\x0e\xfd\x5e\xfc\x07\xd8\x4d\x55\x32\x49"
  4794. "\x45\x0d\xab\x4a\x49\xc4\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93"
  4795. "\x7a\xe6\x6b\xb4\x36\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43"
  4796. "\x2f\x3b\xfc\x09\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48"
  4797. "\x3d\x0e\xda\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08"
  4798. "\xd9\xdc\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a"
  4799. "\xb7\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  4800. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4\xe6"
  4801. "\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97\x68\x44";
  4802. byte hash[sizeof(large_digest) - 1];
  4803. /*
  4804. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE256_Msg0.pdf
  4805. */
  4806. a.input = "";
  4807. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  4808. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  4809. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  4810. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  4811. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  4812. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  4813. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  4814. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46\xc1\x85\xc1\x51\x11\xe5"
  4815. "\x95\x52\x2a\x6b\xcd\x16\xcf\x86\xf3\xd1\x22\x10\x9e\x3b\x1f"
  4816. "\xdd";
  4817. a.inLen = XSTRLEN(a.input);
  4818. a.outLen = WC_SHA3_256_BLOCK_SIZE;
  4819. b.input = "abc";
  4820. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  4821. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  4822. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  4823. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  4824. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  4825. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  4826. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  4827. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0\x9a\x3e\x6d\x51\xcb\xfc"
  4828. "\x62\x27\x20\xd7\xa7\x5c\x63\x34\xe8\xa2\xd7\xec\x71\xa7\xcc"
  4829. "\x29";
  4830. b.inLen = XSTRLEN(b.input);
  4831. b.outLen = WC_SHA3_256_BLOCK_SIZE;
  4832. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4833. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  4834. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  4835. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  4836. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  4837. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  4838. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  4839. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  4840. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4\x9d\xd3\xef\x7e\x18\x2b"
  4841. "\x15\x24\xdf\x82\xea\x1c\xef\xe1\xc6\xc3\x96\x61\x75\xf0\x22"
  4842. "\x8d";
  4843. c.inLen = XSTRLEN(c.input);
  4844. c.outLen = WC_SHA3_256_BLOCK_SIZE;
  4845. /* Taken from NIST CAVP test vectors - full rate output. */
  4846. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  4847. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  4848. "\x85\xe0";
  4849. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  4850. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  4851. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  4852. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  4853. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  4854. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  4855. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  4856. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  4857. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  4858. "\xc2";
  4859. d.inLen = 32;
  4860. d.outLen = WC_SHA3_256_BLOCK_SIZE;
  4861. /* Taken from NIST CAVP test vectors - more than one output block. */
  4862. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  4863. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  4864. "\x47\xe4";
  4865. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  4866. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  4867. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  4868. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  4869. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  4870. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  4871. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  4872. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  4873. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  4874. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  4875. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  4876. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  4877. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  4878. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  4879. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  4880. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  4881. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4"
  4882. "\xe6\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97"
  4883. "\x68\x44";
  4884. e.inLen = 32;
  4885. e.outLen = 2 * WC_SHA3_256_BLOCK_SIZE;
  4886. test_sha[0] = a;
  4887. test_sha[1] = b;
  4888. test_sha[2] = c;
  4889. test_sha[3] = d;
  4890. test_sha[4] = e;
  4891. for (i = 0; i < times; ++i) {
  4892. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  4893. if (ret != 0)
  4894. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4895. ret = wc_Shake256_Absorb(sha, (byte*)test_sha[i].input,
  4896. (word32)test_sha[i].inLen);
  4897. if (ret != 0)
  4898. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4899. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  4900. (word32)test_sha[i].outLen / WC_SHA3_256_BLOCK_SIZE);
  4901. if (ret != 0)
  4902. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4903. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  4904. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4905. }
  4906. #ifndef NO_LARGE_HASH_TEST
  4907. /* BEGIN LARGE HASH TEST */ {
  4908. for (i = 0; i < (int)large_input_buf_size; i++) {
  4909. large_input_buf[i] = (byte)(i & 0xFF);
  4910. }
  4911. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  4912. if (ret != 0)
  4913. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4914. /* Absorb is non-incremental. */
  4915. ret = wc_Shake256_Absorb(sha, large_input_buf,
  4916. (word32)large_input_buf_size);
  4917. if (ret != 0)
  4918. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4919. /* Able to squeeze out blocks incrementally. */
  4920. ret = wc_Shake256_SqueezeBlocks(sha, hash, 1);
  4921. if (ret != 0)
  4922. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4923. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  4924. ((word32)sizeof(hash) / WC_SHA3_256_BLOCK_SIZE) - 1);
  4925. if (ret != 0)
  4926. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4927. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  4928. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4929. } /* END LARGE HASH TEST */
  4930. #endif /* NO_LARGE_HASH_TEST */
  4931. exit:
  4932. return ret;
  4933. }
  4934. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake256_test(void)
  4935. {
  4936. wc_Shake sha;
  4937. byte hash[250];
  4938. testVector a, b, c, d, e;
  4939. testVector test_sha[5];
  4940. wc_test_ret_t ret = 0;
  4941. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4942. #define SHAKE256_LARGE_INPUT_BUFSIZ 1024
  4943. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4944. byte *large_input = NULL;
  4945. #else
  4946. byte large_input[SHAKE256_LARGE_INPUT_BUFSIZ];
  4947. #endif
  4948. const char* large_digest =
  4949. "\x90\x32\x4a\xcc\xd1\xdf\xb8\x0b\x79\x1f\xb8\xc8\x5b\x54\xc8\xe7"
  4950. "\x45\xf5\x60\x6b\x38\x26\xb2\x0a\xee\x38\x01\xf3\xd9\xfa\x96\x9f"
  4951. "\x6a\xd7\x15\xdf\xb6\xc2\xf4\x20\x33\x44\x55\xe8\x2a\x09\x2b\x68"
  4952. "\x2e\x18\x65\x5e\x65\x93\x28\xbc\xb1\x9e\xe2\xb1\x92\xea\x98\xac"
  4953. "\x21\xef\x4c\xe1\xb4\xb7\xbe\x81\x5c\x1d\xd3\xb7\x17\xe5\xbb\xc5"
  4954. "\x8c\x68\xb7\xfb\xac\x55\x8a\x9b\x4d\x91\xe4\x9f\x72\xbb\x6e\x38"
  4955. "\xaf\x21\x7d\x21\xaa\x98\x4e\x75\xc4\xb4\x1c\x7c\x50\x45\x54\xf9"
  4956. "\xea\x26";
  4957. WOLFSSL_ENTER("shake256_test");
  4958. /*
  4959. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE256_Msg0.pdf
  4960. */
  4961. a.input = "";
  4962. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  4963. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  4964. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  4965. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  4966. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  4967. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  4968. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  4969. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46";
  4970. a.inLen = XSTRLEN(a.input);
  4971. a.outLen = 114;
  4972. b.input = "abc";
  4973. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  4974. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  4975. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  4976. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  4977. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  4978. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  4979. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  4980. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0";
  4981. b.inLen = XSTRLEN(b.input);
  4982. b.outLen = 114;
  4983. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4984. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  4985. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  4986. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  4987. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  4988. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  4989. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  4990. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  4991. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  4992. c.inLen = XSTRLEN(c.input);
  4993. c.outLen = 114;
  4994. /* Taken from NIST CAVP test vectors - full rate output. */
  4995. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  4996. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  4997. "\x85\xe0";
  4998. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  4999. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  5000. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  5001. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  5002. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  5003. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  5004. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  5005. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  5006. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  5007. "\xc2";
  5008. d.inLen = 32;
  5009. d.outLen = 136;
  5010. /* Taken from NIST CAVP test vectors - more than one output block. */
  5011. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  5012. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  5013. "\x47\xe4";
  5014. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  5015. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  5016. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  5017. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  5018. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  5019. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  5020. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  5021. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  5022. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  5023. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  5024. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  5025. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  5026. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  5027. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  5028. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  5029. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  5030. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c";
  5031. e.inLen = 32;
  5032. e.outLen = 250;
  5033. test_sha[0] = a;
  5034. test_sha[1] = b;
  5035. test_sha[2] = c;
  5036. test_sha[3] = d;
  5037. test_sha[4] = e;
  5038. ret = wc_InitShake256(&sha, HEAP_HINT, devId);
  5039. if (ret != 0)
  5040. return WC_TEST_RET_ENC_EC(ret);
  5041. for (i = 0; i < times; ++i) {
  5042. ret = wc_Shake256_Update(&sha, (byte*)test_sha[i].input,
  5043. (word32)test_sha[i].inLen);
  5044. if (ret != 0)
  5045. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5046. ret = wc_Shake256_Final(&sha, hash, (word32)test_sha[i].outLen);
  5047. if (ret != 0)
  5048. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5049. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  5050. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5051. }
  5052. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  5053. large_input = (byte *)XMALLOC(SHAKE256_LARGE_INPUT_BUFSIZ, NULL,
  5054. DYNAMIC_TYPE_TMP_BUFFER);
  5055. if (large_input == NULL)
  5056. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  5057. #endif
  5058. #ifndef NO_LARGE_HASH_TEST
  5059. /* BEGIN LARGE HASH TEST */ {
  5060. for (i = 0; i < SHAKE256_LARGE_INPUT_BUFSIZ; i++) {
  5061. large_input[i] = (byte)(i & 0xFF);
  5062. }
  5063. times = 100;
  5064. for (i = 0; i < times; ++i) {
  5065. ret = wc_Shake256_Update(&sha, (byte*)large_input,
  5066. SHAKE256_LARGE_INPUT_BUFSIZ);
  5067. if (ret != 0)
  5068. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  5069. }
  5070. ret = wc_Shake256_Final(&sha, hash, (word32)sizeof(hash));
  5071. if (ret != 0)
  5072. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  5073. if (XMEMCMP(hash, large_digest, 114) != 0)
  5074. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  5075. } /* END LARGE HASH TEST */
  5076. #endif /* NO_LARGE_HASH_TEST */
  5077. ret = shake256_absorb_test(&sha, large_input, SHAKE256_LARGE_INPUT_BUFSIZ);
  5078. exit:
  5079. wc_Shake256_Free(&sha);
  5080. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  5081. if (large_input != NULL)
  5082. XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5083. #endif
  5084. return ret;
  5085. }
  5086. #endif
  5087. #ifdef WOLFSSL_SM3
  5088. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm3_test(void)
  5089. {
  5090. wc_Sm3 sm3, sm3Copy;
  5091. byte hash[WC_SM3_DIGEST_SIZE];
  5092. byte hashGet[WC_SM3_DIGEST_SIZE];
  5093. byte hashCopy[WC_SM3_DIGEST_SIZE];
  5094. wc_test_ret_t ret = 0;
  5095. WOLFSSL_ENTER("sm3_test");
  5096. testVector a, b, c;
  5097. testVector test_sm3[3];
  5098. int times = sizeof(test_sm3) / sizeof(struct testVector), i;
  5099. a.input = "";
  5100. a.output = "\x1a\xb2\x1d\x83\x55\xcf\xa1\x7f\x8e\x61\x19\x48\x31\xe8\x1a"
  5101. "\x8f\x22\xbe\xc8\xc7\x28\xfe\xfb\x74\x7e\xd0\x35\xeb\x50\x82"
  5102. "\xaa\x2b";
  5103. a.inLen = XSTRLEN(a.input);
  5104. a.outLen = WC_SM3_DIGEST_SIZE;
  5105. b.input = "abc";
  5106. b.output = "\x66\xc7\xf0\xf4\x62\xee\xed\xd9\xd1\xf2\xd4\x6b\xdc\x10\xe4"
  5107. "\xe2\x41\x67\xc4\x87\x5c\xf2\xf7\xa2\x29\x7d\xa0\x2b\x8f\x4b"
  5108. "\xa8\xe0";
  5109. b.inLen = XSTRLEN(b.input);
  5110. b.outLen = WC_SM3_DIGEST_SIZE;
  5111. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  5112. c.output = "\x63\x9b\x6c\xc5\xe6\x4d\x9e\x37\xa3\x90\xb1\x92\xdf\x4f\xa1"
  5113. "\xea\x07\x20\xab\x74\x7f\xf6\x92\xb9\xf3\x8c\x4e\x66\xad\x7b"
  5114. "\x8c\x05";
  5115. c.inLen = XSTRLEN(c.input);
  5116. c.outLen = WC_SM3_DIGEST_SIZE;
  5117. test_sm3[0] = a;
  5118. test_sm3[1] = b;
  5119. test_sm3[2] = c;
  5120. ret = wc_InitSm3(&sm3, HEAP_HINT, devId);
  5121. if (ret != 0)
  5122. return WC_TEST_RET_ENC_EC(ret);
  5123. ret = wc_InitSm3(&sm3Copy, HEAP_HINT, devId);
  5124. if (ret != 0) {
  5125. wc_Sm3Free(&sm3);
  5126. return WC_TEST_RET_ENC_EC(ret);
  5127. }
  5128. /* Test all the KATs. */
  5129. for (i = 0; i < times; ++i) {
  5130. ret = wc_Sm3Update(&sm3, (byte*)test_sm3[i].input,
  5131. (word32)test_sm3[i].inLen);
  5132. if (ret != 0) {
  5133. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5134. }
  5135. /* Get the final hash but leave ready for more updates. */
  5136. ret = wc_Sm3GetHash(&sm3, hashGet);
  5137. if (ret != 0)
  5138. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5139. /* Make a copy of the hash. */
  5140. ret = wc_Sm3Copy(&sm3, &sm3Copy);
  5141. if (ret != 0)
  5142. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5143. /* Get the final hash with original. */
  5144. ret = wc_Sm3Final(&sm3, hash);
  5145. if (ret != 0)
  5146. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5147. /* Get the final hash with copy. */
  5148. ret = wc_Sm3Final(&sm3Copy, hashCopy);
  5149. if (ret != 0)
  5150. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5151. /* Dispose of copy. */
  5152. wc_Sm3Free(&sm3Copy);
  5153. /* Check hashes match expected. */
  5154. if (XMEMCMP(hash, test_sm3[i].output, WC_SM3_DIGEST_SIZE) != 0)
  5155. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5156. if (XMEMCMP(hash, hashGet, WC_SM3_DIGEST_SIZE) != 0)
  5157. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5158. if (XMEMCMP(hash, hashCopy, WC_SM3_DIGEST_SIZE) != 0)
  5159. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5160. }
  5161. #ifndef NO_LARGE_HASH_TEST
  5162. {
  5163. word32 sz;
  5164. byte large_input[1024];
  5165. #ifdef HASH_SIZE_LIMIT
  5166. const char* large_digest =
  5167. "\x6c\x42\x57\x64\x8e\x45\xf3\xb6\xc0\x83\xd3\x41\x83\x66\x51\xb4"
  5168. "\x50\xfe\x06\xb5\xb7\x1e\xd5\x0d\x41\xfc\x1e\xe5\xc6\x57\x95\x0f";
  5169. times = 20;
  5170. #else
  5171. const char* large_digest =
  5172. "\x34\x51\x3c\xde\x7c\x30\xb7\xc5\xaa\x97\x3b\xed\xb3\x16\xb9\x76"
  5173. "\x35\x46\x14\x80\x2a\x57\xca\xd9\x48\xf9\x93\xcc\x1f\xdd\xab\x79";
  5174. times = 100;
  5175. #endif
  5176. /* Set large input to something. */
  5177. for (i = 0; i < (int)sizeof(large_input); i++) {
  5178. large_input[i] = (byte)(i & 0xFF);
  5179. }
  5180. /* Hash a large number of times. */
  5181. for (i = 0; i < times; ++i) {
  5182. ret = wc_Sm3Update(&sm3, (byte*)large_input,
  5183. (word32)sizeof(large_input));
  5184. if (ret != 0)
  5185. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5186. }
  5187. /* Calculate hash and compare to expected. */
  5188. ret = wc_Sm3Final(&sm3, hash);
  5189. if (ret != 0)
  5190. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  5191. if (XMEMCMP(hash, large_digest, WC_SM3_DIGEST_SIZE) != 0)
  5192. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  5193. /* Check updating with various sizes works. */
  5194. for (sz = 1; sz <= 64; sz++) {
  5195. /* Hash a large number of times. */
  5196. for (i = 0; i < times; ++i) {
  5197. word32 o;
  5198. /* Update sz bytes at a time from large input buffer. */
  5199. for (o = 0; o + sz <= (word32)sizeof(large_input); o += sz) {
  5200. ret = wc_Sm3Update(&sm3, (byte*)(large_input + o), sz);
  5201. if (ret != 0)
  5202. ERROR_OUT(WC_TEST_RET_ENC_I(o), exit);
  5203. }
  5204. /* Check for left-overs. */
  5205. if (o < (word32)sizeof(large_input)) {
  5206. ret = wc_Sm3Update(&sm3, (byte*)(large_input + o),
  5207. (word32)sizeof(large_input) - o);
  5208. if (ret != 0)
  5209. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5210. }
  5211. }
  5212. /* Calculate hash and compare to expected. */
  5213. ret = wc_Sm3Final(&sm3, hash);
  5214. if (ret != 0)
  5215. ERROR_OUT(WC_TEST_RET_ENC_I(sz), exit);
  5216. if (XMEMCMP(hash, large_digest, WC_SM3_DIGEST_SIZE) != 0)
  5217. ERROR_OUT(WC_TEST_RET_ENC_I(sz), exit);
  5218. }
  5219. }
  5220. #endif /* NO_LARGE_HASH_TEST */
  5221. exit:
  5222. wc_Sm3Free(&sm3);
  5223. wc_Sm3Free(&sm3Copy);
  5224. return ret;
  5225. }
  5226. #endif
  5227. #ifndef NO_HASH_WRAPPER
  5228. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hash_test(void)
  5229. {
  5230. wc_HashAlg hash;
  5231. int ret, exp_ret;
  5232. int i, j;
  5233. int digestSz;
  5234. byte data[] = "0123456789abcdef0123456789abcdef0123456";
  5235. byte out[WC_MAX_DIGEST_SIZE];
  5236. byte hashOut[WC_MAX_DIGEST_SIZE];
  5237. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  5238. enum wc_HashType hashType;
  5239. #endif
  5240. enum wc_HashType typesGood[] = { WC_HASH_TYPE_MD5, WC_HASH_TYPE_SHA,
  5241. WC_HASH_TYPE_SHA224, WC_HASH_TYPE_SHA256,
  5242. WC_HASH_TYPE_SHA384, WC_HASH_TYPE_SHA512,
  5243. WC_HASH_TYPE_SHA3_224,
  5244. WC_HASH_TYPE_SHA3_256,
  5245. WC_HASH_TYPE_SHA3_384,
  5246. WC_HASH_TYPE_SHA3_512 };
  5247. enum wc_HashType typesNoImpl[] = {
  5248. #ifdef NO_MD5
  5249. WC_HASH_TYPE_MD5,
  5250. #endif
  5251. #ifdef NO_SHA
  5252. WC_HASH_TYPE_SHA,
  5253. #endif
  5254. #ifndef WOLFSSL_SHA224
  5255. WC_HASH_TYPE_SHA224,
  5256. #endif
  5257. #ifdef NO_SHA256
  5258. WC_HASH_TYPE_SHA256,
  5259. #endif
  5260. #ifndef WOLFSSL_SHA384
  5261. WC_HASH_TYPE_SHA384,
  5262. #endif
  5263. #ifndef WOLFSSL_SHA512
  5264. WC_HASH_TYPE_SHA512,
  5265. #endif
  5266. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_224)
  5267. WC_HASH_TYPE_SHA3_224,
  5268. #endif
  5269. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_256)
  5270. WC_HASH_TYPE_SHA3_256,
  5271. #endif
  5272. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_384)
  5273. WC_HASH_TYPE_SHA3_384,
  5274. #endif
  5275. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_512)
  5276. WC_HASH_TYPE_SHA3_512,
  5277. #endif
  5278. WC_HASH_TYPE_NONE
  5279. };
  5280. enum wc_HashType typesBad[] = { WC_HASH_TYPE_NONE, WC_HASH_TYPE_MD5_SHA,
  5281. WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4 };
  5282. enum wc_HashType typesHashBad[] = { WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4,
  5283. WC_HASH_TYPE_BLAKE2B,
  5284. WC_HASH_TYPE_NONE };
  5285. WOLFSSL_ENTER("hash_test");
  5286. /* Parameter Validation testing. */
  5287. ret = wc_HashInit(NULL, WC_HASH_TYPE_SHA256);
  5288. if (ret != BAD_FUNC_ARG)
  5289. return WC_TEST_RET_ENC_EC(ret);
  5290. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  5291. if (ret != BAD_FUNC_ARG)
  5292. return WC_TEST_RET_ENC_EC(ret);
  5293. ret = wc_HashUpdate(&hash, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  5294. if (ret != BAD_FUNC_ARG)
  5295. return WC_TEST_RET_ENC_EC(ret);
  5296. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, data, sizeof(data));
  5297. if (ret != BAD_FUNC_ARG)
  5298. return WC_TEST_RET_ENC_EC(ret);
  5299. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, NULL);
  5300. if (ret != BAD_FUNC_ARG)
  5301. return WC_TEST_RET_ENC_EC(ret);
  5302. ret = wc_HashFinal(&hash, WC_HASH_TYPE_SHA256, NULL);
  5303. if (ret != BAD_FUNC_ARG)
  5304. return WC_TEST_RET_ENC_EC(ret);
  5305. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, out);
  5306. if (ret != BAD_FUNC_ARG)
  5307. return WC_TEST_RET_ENC_EC(ret);
  5308. /* Try invalid hash algorithms. */
  5309. for (i = 0; i < (int)(sizeof(typesBad)/sizeof(*typesBad)); i++) {
  5310. ret = wc_HashInit(&hash, typesBad[i]);
  5311. if (ret != BAD_FUNC_ARG)
  5312. return WC_TEST_RET_ENC_I(i);
  5313. ret = wc_HashUpdate(&hash, typesBad[i], data, sizeof(data));
  5314. if (ret != BAD_FUNC_ARG)
  5315. return WC_TEST_RET_ENC_I(i);
  5316. ret = wc_HashFinal(&hash, typesBad[i], out);
  5317. if (ret != BAD_FUNC_ARG)
  5318. return WC_TEST_RET_ENC_I(i);
  5319. wc_HashFree(&hash, typesBad[i]);
  5320. }
  5321. /* Try valid hash algorithms. */
  5322. for (i = 0, j = 0; i < (int)(sizeof(typesGood)/sizeof(*typesGood)); i++) {
  5323. exp_ret = 0;
  5324. if (typesGood[i] == typesNoImpl[j]) {
  5325. /* Recognized but no implementation compiled in. */
  5326. exp_ret = HASH_TYPE_E;
  5327. j++;
  5328. }
  5329. ret = wc_HashInit(&hash, typesGood[i]);
  5330. if (ret != exp_ret)
  5331. return WC_TEST_RET_ENC_I(i);
  5332. ret = wc_HashUpdate(&hash, typesGood[i], data, sizeof(data));
  5333. if (ret != exp_ret)
  5334. return WC_TEST_RET_ENC_I(i);
  5335. ret = wc_HashFinal(&hash, typesGood[i], out);
  5336. if (ret != exp_ret)
  5337. return WC_TEST_RET_ENC_I(i);
  5338. wc_HashFree(&hash, typesGood[i]);
  5339. digestSz = wc_HashGetDigestSize(typesGood[i]);
  5340. if (exp_ret < 0 && digestSz != exp_ret)
  5341. return WC_TEST_RET_ENC_I(i);
  5342. if (exp_ret == 0 && digestSz < 0)
  5343. return WC_TEST_RET_ENC_I(i);
  5344. if (exp_ret == 0) {
  5345. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut,
  5346. digestSz - 1);
  5347. if (ret != BUFFER_E)
  5348. return WC_TEST_RET_ENC_I(i);
  5349. }
  5350. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut, (word32)digestSz);
  5351. if (ret != exp_ret)
  5352. return WC_TEST_RET_ENC_I(i);
  5353. if (exp_ret == 0 && XMEMCMP(out, hashOut, digestSz) != 0)
  5354. return WC_TEST_RET_ENC_I(i);
  5355. ret = wc_HashGetBlockSize(typesGood[i]);
  5356. if (exp_ret < 0 && ret != exp_ret)
  5357. return WC_TEST_RET_ENC_I(i);
  5358. if (exp_ret == 0 && ret < 0)
  5359. return WC_TEST_RET_ENC_I(i);
  5360. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  5361. ret = wc_HashGetOID(typesGood[i]);
  5362. if (ret == BAD_FUNC_ARG ||
  5363. (exp_ret == 0 && ret == HASH_TYPE_E) ||
  5364. (exp_ret != 0 && ret != HASH_TYPE_E)) {
  5365. return WC_TEST_RET_ENC_I(i);
  5366. }
  5367. hashType = wc_OidGetHash(ret);
  5368. if (exp_ret == 0 && hashType != typesGood[i])
  5369. return WC_TEST_RET_ENC_I(i);
  5370. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  5371. }
  5372. for (i = 0; i < (int)(sizeof(typesHashBad)/sizeof(*typesHashBad)); i++) {
  5373. ret = wc_Hash(typesHashBad[i], data, sizeof(data), out, sizeof(out));
  5374. if ((ret != BAD_FUNC_ARG) && (ret != BUFFER_E) && (ret != HASH_TYPE_E))
  5375. return WC_TEST_RET_ENC_I(i);
  5376. }
  5377. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  5378. ret = wc_HashGetOID(WC_HASH_TYPE_MD2);
  5379. #ifdef WOLFSSL_MD2
  5380. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5381. return WC_TEST_RET_ENC_EC(ret);
  5382. #else
  5383. if (ret != HASH_TYPE_E)
  5384. return WC_TEST_RET_ENC_EC(ret);
  5385. #endif
  5386. hashType = wc_OidGetHash(646); /* Md2h */
  5387. #ifdef WOLFSSL_MD2
  5388. if (hashType != WC_HASH_TYPE_MD2)
  5389. return WC_TEST_RET_ENC_NC;
  5390. #else
  5391. if (hashType != WC_HASH_TYPE_NONE)
  5392. return WC_TEST_RET_ENC_NC;
  5393. #endif
  5394. ret = wc_HashGetOID(WC_HASH_TYPE_MD5_SHA);
  5395. #ifndef NO_MD5
  5396. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5397. return WC_TEST_RET_ENC_EC(ret);
  5398. #else
  5399. if (ret != HASH_TYPE_E)
  5400. return WC_TEST_RET_ENC_EC(ret);
  5401. #endif
  5402. ret = wc_HashGetOID(WC_HASH_TYPE_MD4);
  5403. if (ret != BAD_FUNC_ARG)
  5404. return WC_TEST_RET_ENC_EC(ret);
  5405. ret = wc_HashGetOID(WC_HASH_TYPE_NONE);
  5406. if (ret != BAD_FUNC_ARG)
  5407. return WC_TEST_RET_ENC_EC(ret);
  5408. hashType = wc_OidGetHash(0);
  5409. if (hashType != WC_HASH_TYPE_NONE)
  5410. return WC_TEST_RET_ENC_NC;
  5411. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  5412. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD2);
  5413. #ifdef WOLFSSL_MD2
  5414. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5415. return WC_TEST_RET_ENC_EC(ret);
  5416. #else
  5417. if (ret != HASH_TYPE_E)
  5418. return WC_TEST_RET_ENC_EC(ret);
  5419. #endif
  5420. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD2);
  5421. #ifdef WOLFSSL_MD2
  5422. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5423. return WC_TEST_RET_ENC_EC(ret);
  5424. #else
  5425. if (ret != HASH_TYPE_E)
  5426. return WC_TEST_RET_ENC_EC(ret);
  5427. #endif
  5428. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD4);
  5429. #ifndef NO_MD4
  5430. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5431. return WC_TEST_RET_ENC_EC(ret);
  5432. #else
  5433. if (ret != HASH_TYPE_E)
  5434. return WC_TEST_RET_ENC_EC(ret);
  5435. #endif
  5436. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD4);
  5437. #ifndef NO_MD4
  5438. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5439. return WC_TEST_RET_ENC_EC(ret);
  5440. #else
  5441. if (ret != HASH_TYPE_E)
  5442. return WC_TEST_RET_ENC_EC(ret);
  5443. #endif
  5444. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD5_SHA);
  5445. #if !defined(NO_MD5) && !defined(NO_SHA)
  5446. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5447. return WC_TEST_RET_ENC_EC(ret);
  5448. #else
  5449. if (ret != HASH_TYPE_E)
  5450. return WC_TEST_RET_ENC_EC(ret);
  5451. #endif
  5452. ret = wc_HashGetBlockSize(WC_HASH_TYPE_BLAKE2B);
  5453. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  5454. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5455. return WC_TEST_RET_ENC_EC(ret);
  5456. #else
  5457. if (ret != HASH_TYPE_E)
  5458. return WC_TEST_RET_ENC_EC(ret);
  5459. #endif
  5460. ret = wc_HashGetDigestSize(WC_HASH_TYPE_BLAKE2B);
  5461. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  5462. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5463. return WC_TEST_RET_ENC_EC(ret);
  5464. #else
  5465. if (ret != HASH_TYPE_E)
  5466. return WC_TEST_RET_ENC_EC(ret);
  5467. #endif
  5468. ret = wc_HashGetBlockSize(WC_HASH_TYPE_NONE);
  5469. if (ret != BAD_FUNC_ARG)
  5470. return WC_TEST_RET_ENC_EC(ret);
  5471. ret = wc_HashGetDigestSize(WC_HASH_TYPE_NONE);
  5472. if (ret != BAD_FUNC_ARG)
  5473. return WC_TEST_RET_ENC_EC(ret);
  5474. #if !defined(NO_CERTS) && !defined(NO_ASN)
  5475. #if defined(WOLFSSL_MD2) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5476. ret = wc_GetCTC_HashOID(MD2);
  5477. if (ret == 0)
  5478. return WC_TEST_RET_ENC_EC(ret);
  5479. #endif
  5480. #ifndef NO_MD5
  5481. ret = wc_GetCTC_HashOID(WC_MD5);
  5482. if (ret == 0)
  5483. return WC_TEST_RET_ENC_EC(ret);
  5484. #endif
  5485. #ifndef NO_SHA
  5486. ret = wc_GetCTC_HashOID(WC_SHA);
  5487. if (ret == 0)
  5488. return WC_TEST_RET_ENC_EC(ret);
  5489. #endif
  5490. #ifdef WOLFSSL_SHA224
  5491. ret = wc_GetCTC_HashOID(WC_SHA224);
  5492. if (ret == 0)
  5493. return WC_TEST_RET_ENC_EC(ret);
  5494. #endif
  5495. #ifndef NO_SHA256
  5496. ret = wc_GetCTC_HashOID(WC_SHA256);
  5497. if (ret == 0)
  5498. return WC_TEST_RET_ENC_EC(ret);
  5499. #endif
  5500. #ifdef WOLFSSL_SHA384
  5501. ret = wc_GetCTC_HashOID(WC_SHA384);
  5502. if (ret == 0)
  5503. return WC_TEST_RET_ENC_EC(ret);
  5504. #endif
  5505. #ifdef WOLFSSL_SHA512
  5506. ret = wc_GetCTC_HashOID(WC_SHA512);
  5507. if (ret == 0)
  5508. return WC_TEST_RET_ENC_EC(ret);
  5509. #endif
  5510. ret = wc_GetCTC_HashOID(-1);
  5511. if (ret != 0)
  5512. return WC_TEST_RET_ENC_EC(ret);
  5513. #endif
  5514. return 0;
  5515. }
  5516. #endif /* !NO_HASH_WRAPPER */
  5517. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS) && \
  5518. defined(HAVE_FIPS_VERSION) && \
  5519. (HAVE_FIPS_VERSION >= 5))
  5520. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_md5_test(void)
  5521. {
  5522. Hmac hmac;
  5523. byte hash[WC_MD5_DIGEST_SIZE];
  5524. const char* keys[]=
  5525. {
  5526. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  5527. "Jefe",
  5528. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA",
  5529. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5530. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5531. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5532. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5533. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5534. };
  5535. testVector a, b, c, d;
  5536. testVector test_hmac[4];
  5537. wc_test_ret_t ret;
  5538. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5539. WOLFSSL_ENTER("hmac_md5_test");
  5540. /* Following test vectors are from RFC 2202 section 2 */
  5541. a.input = "Hi There";
  5542. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  5543. "\x9d";
  5544. a.inLen = XSTRLEN(a.input);
  5545. a.outLen = WC_MD5_DIGEST_SIZE;
  5546. b.input = "what do ya want for nothing?";
  5547. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  5548. "\x38";
  5549. b.inLen = XSTRLEN(b.input);
  5550. b.outLen = WC_MD5_DIGEST_SIZE;
  5551. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5552. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5553. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5554. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5555. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  5556. "\xf6";
  5557. c.inLen = XSTRLEN(c.input);
  5558. c.outLen = WC_MD5_DIGEST_SIZE;
  5559. d.input = "Test Using Larger Than Block-Size Key - Hash Key First";
  5560. d.output = "\x6b\x1a\xb7\xfe\x4b\xd7\xbf\x8f\x0b\x62\xe6\xce\x61\xb9\xd0"
  5561. "\xcd";
  5562. d.inLen = XSTRLEN(d.input);
  5563. d.outLen = WC_MD5_DIGEST_SIZE;
  5564. test_hmac[0] = a;
  5565. test_hmac[1] = b;
  5566. test_hmac[2] = c;
  5567. test_hmac[3] = d;
  5568. for (i = 0; i < times; ++i) {
  5569. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  5570. if (i == 1) {
  5571. continue; /* cavium can't handle short keys, fips not allowed */
  5572. }
  5573. #endif
  5574. ret = wc_HmacInit(&hmac, HEAP_HINT, devId);
  5575. if (ret != 0)
  5576. return WC_TEST_RET_ENC_EC(ret);
  5577. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[i],
  5578. (word32)XSTRLEN(keys[i]));
  5579. if (ret != 0)
  5580. return WC_TEST_RET_ENC_EC(ret);
  5581. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5582. (word32)test_hmac[i].inLen);
  5583. if (ret != 0)
  5584. return WC_TEST_RET_ENC_EC(ret);
  5585. ret = wc_HmacFinal(&hmac, hash);
  5586. if (ret != 0)
  5587. return WC_TEST_RET_ENC_EC(ret);
  5588. if (XMEMCMP(hash, test_hmac[i].output, WC_MD5_DIGEST_SIZE) != 0)
  5589. return WC_TEST_RET_ENC_I(i);
  5590. wc_HmacFree(&hmac);
  5591. }
  5592. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  5593. if ((ret = wc_HmacSizeByType(WC_MD5)) != WC_MD5_DIGEST_SIZE)
  5594. return WC_TEST_RET_ENC_EC(ret);
  5595. #endif
  5596. return 0;
  5597. }
  5598. #endif /* !NO_HMAC && !NO_MD5 && (!HAVE_FIPS || (HAVE_FIPS_VERSION < 5)) */
  5599. #if !defined(NO_HMAC) && !defined(NO_SHA)
  5600. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha_test(void)
  5601. {
  5602. Hmac hmac;
  5603. byte hash[WC_SHA_DIGEST_SIZE];
  5604. const char* keys[]=
  5605. {
  5606. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5607. "\x0b\x0b\x0b",
  5608. "Jefe",
  5609. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5610. "\xAA\xAA\xAA",
  5611. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5612. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5613. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5614. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5615. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5616. };
  5617. testVector a, b, c, d;
  5618. testVector test_hmac[4];
  5619. wc_test_ret_t ret;
  5620. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5621. WOLFSSL_ENTER("hmac_sha_test");
  5622. /* Following test vectors are from RFC 2202 section 3 */
  5623. a.input = "Hi There";
  5624. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  5625. "\x8e\xf1\x46\xbe\x00";
  5626. a.inLen = XSTRLEN(a.input);
  5627. a.outLen = WC_SHA_DIGEST_SIZE;
  5628. b.input = "what do ya want for nothing?";
  5629. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  5630. "\x9c\x25\x9a\x7c\x79";
  5631. b.inLen = XSTRLEN(b.input);
  5632. b.outLen = WC_SHA_DIGEST_SIZE;
  5633. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5634. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5635. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5636. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5637. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  5638. "\x4f\x63\xf1\x75\xd3";
  5639. c.inLen = XSTRLEN(c.input);
  5640. c.outLen = WC_SHA_DIGEST_SIZE;
  5641. d.input = "Test Using Larger Than Block-Size Key - Hash Key First";
  5642. d.output = "\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b"
  5643. "\x55\xed\x40\x21\x12";
  5644. d.inLen = XSTRLEN(d.input);
  5645. d.outLen = WC_SHA_DIGEST_SIZE;
  5646. test_hmac[0] = a;
  5647. test_hmac[1] = b;
  5648. test_hmac[2] = c;
  5649. test_hmac[3] = d;
  5650. #if FIPS_VERSION3_GE(6,0,0)
  5651. int allowShortKeyWithFips = 1;
  5652. #endif
  5653. for (i = 0; i < times; ++i) {
  5654. #if defined(HAVE_CAVIUM) || (defined(HAVE_FIPS) && FIPS_VERSION3_LT(6,0,0))
  5655. if (i == 1)
  5656. continue; /* cavium can't handle short keys, fips not allowed */
  5657. #endif
  5658. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5659. return WC_TEST_RET_ENC_EC(ret);
  5660. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[i],
  5661. (word32)XSTRLEN(keys[i]));
  5662. #if FIPS_VERSION3_GE(6,0,0)
  5663. if (i == 1) {
  5664. if (ret != HMAC_MIN_KEYLEN_E)
  5665. return WC_TEST_RET_ENC_EC(ret);
  5666. /* Now use the ex and allow short keys with FIPS option */
  5667. ret = wc_HmacSetKey_ex(&hmac, WC_SHA, (byte*) keys[i],
  5668. (word32)XSTRLEN(keys[i]), allowShortKeyWithFips);
  5669. }
  5670. #endif
  5671. if (ret != 0)
  5672. return WC_TEST_RET_ENC_EC(ret);
  5673. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5674. (word32)test_hmac[i].inLen);
  5675. if (ret != 0)
  5676. return WC_TEST_RET_ENC_EC(ret);
  5677. ret = wc_HmacFinal(&hmac, hash);
  5678. if (ret != 0)
  5679. return WC_TEST_RET_ENC_EC(ret);
  5680. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA_DIGEST_SIZE) != 0)
  5681. return WC_TEST_RET_ENC_I(i);
  5682. wc_HmacFree(&hmac);
  5683. }
  5684. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  5685. if ((ret = wc_HmacSizeByType(WC_SHA)) != WC_SHA_DIGEST_SIZE)
  5686. return WC_TEST_RET_ENC_EC(ret);
  5687. #endif
  5688. return 0;
  5689. }
  5690. #endif
  5691. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  5692. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha224_test(void)
  5693. {
  5694. Hmac hmac;
  5695. byte hash[WC_SHA224_DIGEST_SIZE];
  5696. const char* keys[]=
  5697. {
  5698. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5699. "\x0b\x0b\x0b",
  5700. "Jefe",
  5701. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5702. "\xAA\xAA\xAA",
  5703. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5704. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5705. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5706. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5707. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5708. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5709. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5710. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5711. "\xAA\xAA\xAA"
  5712. };
  5713. testVector a, b, c, d;
  5714. testVector test_hmac[4];
  5715. wc_test_ret_t ret;
  5716. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5717. WOLFSSL_ENTER("hmac_sha224_test");
  5718. /* Following test vectors are from RFC 4231 section 4 */
  5719. a.input = "Hi There";
  5720. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  5721. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  5722. a.inLen = XSTRLEN(a.input);
  5723. a.outLen = WC_SHA224_DIGEST_SIZE;
  5724. b.input = "what do ya want for nothing?";
  5725. b.output = "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d"
  5726. "\x0f\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44";
  5727. b.inLen = XSTRLEN(b.input);
  5728. b.outLen = WC_SHA224_DIGEST_SIZE;
  5729. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5730. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5731. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5732. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5733. c.output = "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2"
  5734. "\x64\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea";
  5735. c.inLen = XSTRLEN(c.input);
  5736. c.outLen = WC_SHA224_DIGEST_SIZE;
  5737. d.input = "Test Using Larger Than Block-Size Key - Hash Key First";
  5738. 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";
  5739. d.inLen = XSTRLEN(d.input);
  5740. d.outLen = WC_SHA224_DIGEST_SIZE;
  5741. test_hmac[0] = a;
  5742. test_hmac[1] = b;
  5743. test_hmac[2] = c;
  5744. test_hmac[3] = d;
  5745. for (i = 0; i < times; ++i) {
  5746. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  5747. if (i == 1)
  5748. continue; /* cavium can't handle short keys, fips not allowed */
  5749. #endif
  5750. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5751. return WC_TEST_RET_ENC_EC(ret);
  5752. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[i],
  5753. (word32)XSTRLEN(keys[i]));
  5754. if (ret != 0)
  5755. return WC_TEST_RET_ENC_EC(ret);
  5756. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5757. (word32)test_hmac[i].inLen);
  5758. if (ret != 0)
  5759. return WC_TEST_RET_ENC_EC(ret);
  5760. ret = wc_HmacFinal(&hmac, hash);
  5761. if (ret != 0)
  5762. return WC_TEST_RET_ENC_EC(ret);
  5763. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  5764. return WC_TEST_RET_ENC_I(i);
  5765. wc_HmacFree(&hmac);
  5766. }
  5767. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  5768. if ((ret = wc_HmacSizeByType(WC_SHA224)) != WC_SHA224_DIGEST_SIZE)
  5769. return WC_TEST_RET_ENC_EC(ret);
  5770. #endif
  5771. return 0;
  5772. }
  5773. #endif
  5774. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  5775. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha256_test(void)
  5776. {
  5777. Hmac hmac;
  5778. byte hash[WC_SHA256_DIGEST_SIZE];
  5779. const char* keys[]=
  5780. {
  5781. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5782. "\x0b\x0b\x0b",
  5783. "Jefe",
  5784. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5785. "\xAA\xAA\xAA",
  5786. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5787. "\xAA\xAA\xAA",
  5788. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5789. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5790. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5791. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5792. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5793. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5794. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5795. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5796. "\xAA\xAA\xAA"
  5797. };
  5798. testVector a, b, c, d, e;
  5799. testVector test_hmac[5];
  5800. wc_test_ret_t ret;
  5801. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5802. WOLFSSL_ENTER("hmac_sha256_test");
  5803. /* Following test vectors are from RFC 4231 section 4 */
  5804. a.input = "Hi There";
  5805. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  5806. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  5807. "\xcf\xf7";
  5808. a.inLen = XSTRLEN(a.input);
  5809. a.outLen = WC_SHA256_DIGEST_SIZE;
  5810. b.input = "what do ya want for nothing?";
  5811. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  5812. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  5813. "\x38\x43";
  5814. b.inLen = XSTRLEN(b.input);
  5815. b.outLen = WC_SHA256_DIGEST_SIZE;
  5816. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5817. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5818. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5819. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5820. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  5821. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  5822. "\x65\xfe";
  5823. c.inLen = XSTRLEN(c.input);
  5824. c.outLen = WC_SHA256_DIGEST_SIZE;
  5825. d.input = 0;
  5826. d.output = "\x86\xe5\x4f\xd4\x48\x72\x5d\x7e\x5d\xcf\xe2\x23\x53\xc8\x28"
  5827. "\xaf\x48\x78\x1e\xb4\x8c\xae\x81\x06\xa7\xe1\xd4\x98\x94\x9f"
  5828. "\x3e\x46";
  5829. d.inLen = 0;
  5830. d.outLen = WC_SHA256_DIGEST_SIZE;
  5831. e.input = "Test Using Larger Than Block-Size Key - Hash Key First";
  5832. e.output = "\x60\xe4\x31\x59\x1e\xe0\xb6\x7f\x0d\x8a\x26\xaa\xcb\xf5\xb7"
  5833. "\x7f\x8e\x0b\xc6\x21\x37\x28\xc5\x14\x05\x46\x04\x0f\x0e\xe3"
  5834. "\x7f\x54";
  5835. e.inLen = XSTRLEN(e.input);;
  5836. e.outLen = WC_SHA256_DIGEST_SIZE;
  5837. test_hmac[0] = a;
  5838. test_hmac[1] = b;
  5839. test_hmac[2] = c;
  5840. test_hmac[3] = d;
  5841. test_hmac[4] = e;
  5842. for (i = 0; i < times; ++i) {
  5843. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  5844. if (i == 1)
  5845. continue; /* cavium can't handle short keys, fips not allowed */
  5846. #endif
  5847. #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
  5848. if (i == 3)
  5849. continue; /* QuickAssist can't handle empty HMAC */
  5850. #endif
  5851. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  5852. return WC_TEST_RET_ENC_I(i);
  5853. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[i],
  5854. (word32)XSTRLEN(keys[i]));
  5855. if (ret != 0)
  5856. return WC_TEST_RET_ENC_I(i);
  5857. if (test_hmac[i].input != NULL) {
  5858. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5859. (word32)test_hmac[i].inLen);
  5860. if (ret != 0)
  5861. return WC_TEST_RET_ENC_I(i);
  5862. }
  5863. ret = wc_HmacFinal(&hmac, hash);
  5864. if (ret != 0)
  5865. return WC_TEST_RET_ENC_I(i);
  5866. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  5867. return WC_TEST_RET_ENC_I(i);
  5868. wc_HmacFree(&hmac);
  5869. }
  5870. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  5871. if ((ret = wc_HmacSizeByType(WC_SHA256)) != WC_SHA256_DIGEST_SIZE)
  5872. return WC_TEST_RET_ENC_EC(ret);
  5873. #if FIPS_VERSION3_GE(6,0,0)
  5874. if ((ret = wc_HmacSizeByType(21)) != HMAC_KAT_FIPS_E)
  5875. #else
  5876. if ((ret = wc_HmacSizeByType(21)) != BAD_FUNC_ARG)
  5877. #endif
  5878. {
  5879. return WC_TEST_RET_ENC_EC(ret);
  5880. }
  5881. #endif
  5882. if ((ret = wolfSSL_GetHmacMaxSize()) != WC_MAX_DIGEST_SIZE)
  5883. return WC_TEST_RET_ENC_EC(ret);
  5884. return 0;
  5885. }
  5886. #endif
  5887. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  5888. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha384_test(void)
  5889. {
  5890. Hmac hmac;
  5891. byte hash[WC_SHA384_DIGEST_SIZE];
  5892. const char* keys[]=
  5893. {
  5894. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5895. "\x0b\x0b\x0b",
  5896. "Jefe",
  5897. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5898. "\xAA\xAA\xAA",
  5899. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5900. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5901. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5902. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5903. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5904. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5905. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5906. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5907. "\xAA\xAA\xAA"
  5908. };
  5909. testVector a, b, c, d;
  5910. testVector test_hmac[4];
  5911. wc_test_ret_t ret;
  5912. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5913. WOLFSSL_ENTER("hmac_sha384_test");
  5914. /* Following test vectors are from RFC 4231 section 4 */
  5915. a.input = "Hi There";
  5916. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  5917. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  5918. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  5919. "\xfa\x9c\xb6";
  5920. a.inLen = XSTRLEN(a.input);
  5921. a.outLen = WC_SHA384_DIGEST_SIZE;
  5922. b.input = "what do ya want for nothing?";
  5923. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  5924. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  5925. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  5926. "\xb2\x16\x49";
  5927. b.inLen = XSTRLEN(b.input);
  5928. b.outLen = WC_SHA384_DIGEST_SIZE;
  5929. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5930. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5931. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5932. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5933. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  5934. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  5935. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  5936. "\xa3\x4f\x27";
  5937. c.inLen = XSTRLEN(c.input);
  5938. c.outLen = WC_SHA384_DIGEST_SIZE;
  5939. d.input = "Test Using Larger Than Block-Size Key - Hash Key First";
  5940. d.output = "\x4e\xce\x08\x44\x85\x81\x3e\x90\x88\xd2\xc6\x3a\x04\x1b\xc5"
  5941. "\xb4\x4f\x9e\xf1\x01\x2a\x2b\x58\x8f\x3c\xd1\x1f\x05\x03\x3a"
  5942. "\xc4\xc6\x0c\x2e\xf6\xab\x40\x30\xfe\x82\x96\x24\x8d\xf1\x63"
  5943. "\xf4\x49\x52";
  5944. d.inLen = XSTRLEN(d.input);
  5945. d.outLen = WC_SHA384_DIGEST_SIZE;
  5946. test_hmac[0] = a;
  5947. test_hmac[1] = b;
  5948. test_hmac[2] = c;
  5949. test_hmac[3] = d;
  5950. for (i = 0; i < times; ++i) {
  5951. #if defined(HAVE_FIPS)
  5952. if (i == 1)
  5953. continue; /* fips not allowed */
  5954. #endif
  5955. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5956. return WC_TEST_RET_ENC_EC(ret);
  5957. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[i],
  5958. (word32)XSTRLEN(keys[i]));
  5959. if (ret != 0)
  5960. return WC_TEST_RET_ENC_EC(ret);
  5961. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5962. (word32)test_hmac[i].inLen);
  5963. if (ret != 0)
  5964. return WC_TEST_RET_ENC_EC(ret);
  5965. ret = wc_HmacFinal(&hmac, hash);
  5966. if (ret != 0)
  5967. return WC_TEST_RET_ENC_EC(ret);
  5968. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  5969. return WC_TEST_RET_ENC_I(i);
  5970. wc_HmacFree(&hmac);
  5971. }
  5972. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  5973. if ((ret = wc_HmacSizeByType(WC_SHA384)) != WC_SHA384_DIGEST_SIZE)
  5974. return WC_TEST_RET_ENC_EC(ret);
  5975. #endif
  5976. return 0;
  5977. }
  5978. #endif
  5979. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA512)
  5980. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha512_test(void)
  5981. {
  5982. Hmac hmac;
  5983. byte hash[WC_SHA512_DIGEST_SIZE];
  5984. const char* keys[]=
  5985. {
  5986. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5987. "\x0b\x0b\x0b",
  5988. "Jefe",
  5989. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5990. "\xAA\xAA\xAA",
  5991. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5992. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5993. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5994. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5995. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5996. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5997. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5998. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5999. "\xAA\xAA\xAA"
  6000. };
  6001. testVector a, b, c, d;
  6002. testVector test_hmac[4];
  6003. wc_test_ret_t ret;
  6004. int times = sizeof(test_hmac) / sizeof(testVector), i;
  6005. WOLFSSL_ENTER("hmac_sha512_test");
  6006. /* Following test vectors are from RFC 4231 section 4 */
  6007. a.input = "Hi There";
  6008. a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
  6009. "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
  6010. "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
  6011. "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
  6012. "\x3a\x12\x68\x54";
  6013. a.inLen = XSTRLEN(a.input);
  6014. a.outLen = WC_SHA512_DIGEST_SIZE;
  6015. b.input = "what do ya want for nothing?";
  6016. b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
  6017. "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
  6018. "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
  6019. "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
  6020. "\x38\xbc\xe7\x37";
  6021. b.inLen = XSTRLEN(b.input);
  6022. b.outLen = WC_SHA512_DIGEST_SIZE;
  6023. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  6024. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  6025. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  6026. "\xDD\xDD\xDD\xDD\xDD\xDD";
  6027. c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
  6028. "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
  6029. "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
  6030. "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
  6031. "\xe1\x32\x92\xfb";
  6032. c.inLen = XSTRLEN(c.input);
  6033. c.outLen = WC_SHA512_DIGEST_SIZE;
  6034. d.input = "Test Using Larger Than Block-Size Key - Hash Key First";
  6035. d.output = "\x80\xb2\x42\x63\xc7\xc1\xa3\xeb\xb7\x14\x93\xc1\xdd\x7b\xe8"
  6036. "\xb4\x9b\x46\xd1\xf4\x1b\x4a\xee\xc1\x12\x1b\x01\x37\x83\xf8"
  6037. "\xf3\x52\x6b\x56\xd0\x37\xe0\x5f\x25\x98\xbd\x0f\xd2\x21\x5d"
  6038. "\x6a\x1e\x52\x95\xe6\x4f\x73\xf6\x3f\x0a\xec\x8b\x91\x5a\x98"
  6039. "\x5d\x78\x65\x98";
  6040. d.inLen = XSTRLEN(d.input);
  6041. d.outLen = WC_SHA512_DIGEST_SIZE;
  6042. test_hmac[0] = a;
  6043. test_hmac[1] = b;
  6044. test_hmac[2] = c;
  6045. test_hmac[3] = d;
  6046. for (i = 0; i < times; ++i) {
  6047. #if defined(HAVE_FIPS)
  6048. if (i == 1)
  6049. continue; /* fips not allowed */
  6050. #endif
  6051. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  6052. return WC_TEST_RET_ENC_EC(ret);
  6053. ret = wc_HmacSetKey(&hmac, WC_SHA512, (byte*)keys[i],
  6054. (word32)XSTRLEN(keys[i]));
  6055. if (ret != 0)
  6056. return WC_TEST_RET_ENC_EC(ret);
  6057. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  6058. (word32)test_hmac[i].inLen);
  6059. if (ret != 0)
  6060. return WC_TEST_RET_ENC_EC(ret);
  6061. ret = wc_HmacFinal(&hmac, hash);
  6062. if (ret != 0)
  6063. return WC_TEST_RET_ENC_EC(ret);
  6064. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  6065. return WC_TEST_RET_ENC_I(i);
  6066. wc_HmacFree(&hmac);
  6067. }
  6068. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  6069. if ((ret = wc_HmacSizeByType(WC_SHA512)) != WC_SHA512_DIGEST_SIZE)
  6070. return WC_TEST_RET_ENC_EC(ret);
  6071. #endif
  6072. return 0;
  6073. }
  6074. #endif
  6075. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  6076. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  6077. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  6078. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha3_test(void)
  6079. {
  6080. Hmac hmac;
  6081. byte hash[WC_SHA3_512_DIGEST_SIZE];
  6082. const char* key[4] =
  6083. {
  6084. "Jefe",
  6085. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  6086. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  6087. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  6088. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  6089. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6090. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6091. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6092. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6093. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6094. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6095. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6096. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6097. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6098. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6099. };
  6100. const char* input[4] =
  6101. {
  6102. "what do ya want for nothing?",
  6103. "Hi There",
  6104. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  6105. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  6106. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  6107. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  6108. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
  6109. "Big Key Input"
  6110. };
  6111. const int hashType[4] =
  6112. {
  6113. WC_SHA3_224, WC_SHA3_256, WC_SHA3_384, WC_SHA3_512
  6114. };
  6115. const int hashSz[4] =
  6116. {
  6117. WC_SHA3_224_DIGEST_SIZE, WC_SHA3_256_DIGEST_SIZE,
  6118. WC_SHA3_384_DIGEST_SIZE, WC_SHA3_512_DIGEST_SIZE
  6119. };
  6120. const char* output[16] =
  6121. {
  6122. /* key = jefe, input = what do ya want for nothing? */
  6123. /* HMAC-SHA3-224 */
  6124. "\x7f\xdb\x8d\xd8\x8b\xd2\xf6\x0d\x1b\x79\x86\x34\xad\x38\x68\x11"
  6125. "\xc2\xcf\xc8\x5b\xfa\xf5\xd5\x2b\xba\xce\x5e\x66",
  6126. /* HMAC-SHA3-256 */
  6127. "\xc7\xd4\x07\x2e\x78\x88\x77\xae\x35\x96\xbb\xb0\xda\x73\xb8\x87"
  6128. "\xc9\x17\x1f\x93\x09\x5b\x29\x4a\xe8\x57\xfb\xe2\x64\x5e\x1b\xa5",
  6129. /* HMAC-SHA3-384 */
  6130. "\xf1\x10\x1f\x8c\xbf\x97\x66\xfd\x67\x64\xd2\xed\x61\x90\x3f\x21"
  6131. "\xca\x9b\x18\xf5\x7c\xf3\xe1\xa2\x3c\xa1\x35\x08\xa9\x32\x43\xce"
  6132. "\x48\xc0\x45\xdc\x00\x7f\x26\xa2\x1b\x3f\x5e\x0e\x9d\xf4\xc2\x0a",
  6133. /* HMAC-SHA3-512 */
  6134. "\x5a\x4b\xfe\xab\x61\x66\x42\x7c\x7a\x36\x47\xb7\x47\x29\x2b\x83"
  6135. "\x84\x53\x7c\xdb\x89\xaf\xb3\xbf\x56\x65\xe4\xc5\xe7\x09\x35\x0b"
  6136. "\x28\x7b\xae\xc9\x21\xfd\x7c\xa0\xee\x7a\x0c\x31\xd0\x22\xa9\x5e"
  6137. "\x1f\xc9\x2b\xa9\xd7\x7d\xf8\x83\x96\x02\x75\xbe\xb4\xe6\x20\x24",
  6138. /* key = 0b..., input = Hi There */
  6139. /* HMAC-SHA3-224 */
  6140. "\x3b\x16\x54\x6b\xbc\x7b\xe2\x70\x6a\x03\x1d\xca\xfd\x56\x37\x3d"
  6141. "\x98\x84\x36\x76\x41\xd8\xc5\x9a\xf3\xc8\x60\xf7",
  6142. /* HMAC-SHA3-256 */
  6143. "\xba\x85\x19\x23\x10\xdf\xfa\x96\xe2\xa3\xa4\x0e\x69\x77\x43\x51"
  6144. "\x14\x0b\xb7\x18\x5e\x12\x02\xcd\xcc\x91\x75\x89\xf9\x5e\x16\xbb",
  6145. /* HMAC-SHA3-384 */
  6146. "\x68\xd2\xdc\xf7\xfd\x4d\xdd\x0a\x22\x40\xc8\xa4\x37\x30\x5f\x61"
  6147. "\xfb\x73\x34\xcf\xb5\xd0\x22\x6e\x1b\xc2\x7d\xc1\x0a\x2e\x72\x3a"
  6148. "\x20\xd3\x70\xb4\x77\x43\x13\x0e\x26\xac\x7e\x3d\x53\x28\x86\xbd",
  6149. /* HMAC-SHA3-512 */
  6150. "\xeb\x3f\xbd\x4b\x2e\xaa\xb8\xf5\xc5\x04\xbd\x3a\x41\x46\x5a\xac"
  6151. "\xec\x15\x77\x0a\x7c\xab\xac\x53\x1e\x48\x2f\x86\x0b\x5e\xc7\xba"
  6152. "\x47\xcc\xb2\xc6\xf2\xaf\xce\x8f\x88\xd2\x2b\x6d\xc6\x13\x80\xf2"
  6153. "\x3a\x66\x8f\xd3\x88\x8b\xb8\x05\x37\xc0\xa0\xb8\x64\x07\x68\x9e",
  6154. /* key = aa..., output = dd... */
  6155. /* HMAC-SHA3-224 */
  6156. "\x67\x6c\xfc\x7d\x16\x15\x36\x38\x78\x03\x90\x69\x2b\xe1\x42\xd2"
  6157. "\xdf\x7c\xe9\x24\xb9\x09\xc0\xc0\x8d\xbf\xdc\x1a",
  6158. /* HMAC-SHA3-256 */
  6159. "\x84\xec\x79\x12\x4a\x27\x10\x78\x65\xce\xdd\x8b\xd8\x2d\xa9\x96"
  6160. "\x5e\x5e\xd8\xc3\x7b\x0a\xc9\x80\x05\xa7\xf3\x9e\xd5\x8a\x42\x07",
  6161. /* HMAC-SHA3-384 */
  6162. "\x27\x5c\xd0\xe6\x61\xbb\x8b\x15\x1c\x64\xd2\x88\xf1\xf7\x82\xfb"
  6163. "\x91\xa8\xab\xd5\x68\x58\xd7\x2b\xab\xb2\xd4\x76\xf0\x45\x83\x73"
  6164. "\xb4\x1b\x6a\xb5\xbf\x17\x4b\xec\x42\x2e\x53\xfc\x31\x35\xac\x6e",
  6165. /* HMAC-SHA3-512 */
  6166. "\x30\x9e\x99\xf9\xec\x07\x5e\xc6\xc6\xd4\x75\xed\xa1\x18\x06\x87"
  6167. "\xfc\xf1\x53\x11\x95\x80\x2a\x99\xb5\x67\x74\x49\xa8\x62\x51\x82"
  6168. "\x85\x1c\xb3\x32\xaf\xb6\xa8\x9c\x41\x13\x25\xfb\xcb\xcd\x42\xaf"
  6169. "\xcb\x7b\x6e\x5a\xab\x7e\xa4\x2c\x66\x0f\x97\xfd\x85\x84\xbf\x03",
  6170. /* key = big key, input = Big Key Input */
  6171. /* HMAC-SHA3-224 */
  6172. "\x29\xe0\x5e\x46\xc4\xa4\x5e\x46\x74\xbf\xd7\x2d\x1a\xd8\x66\xdb"
  6173. "\x2d\x0d\x10\x4e\x2b\xfa\xad\x53\x7d\x15\x69\x8b",
  6174. /* HMAC-SHA3-256 */
  6175. "\xb5\x5b\x8d\x64\xb6\x9c\x21\xd0\xbf\x20\x5c\xa2\xf7\xb9\xb1\x4e"
  6176. "\x88\x21\x61\x2c\x66\xc3\x91\xae\x6c\x95\x16\x85\x83\xe6\xf4\x9b",
  6177. /* HMAC-SHA3-384 */
  6178. "\xaa\x91\xb3\xa6\x2f\x56\xa1\xbe\x8c\x3e\x74\x38\xdb\x58\xd9\xd3"
  6179. "\x34\xde\xa0\x60\x6d\x8d\x46\xe0\xec\xa9\xf6\x06\x35\x14\xe6\xed"
  6180. "\x83\xe6\x7c\x77\x24\x6c\x11\xb5\x90\x82\xb5\x75\xda\x7b\x83\x2d",
  6181. /* HMAC-SHA3-512 */
  6182. "\x1c\xc3\xa9\x24\x4a\x4a\x3f\xbd\xc7\x20\x00\x16\x9b\x79\x47\x03"
  6183. "\x78\x75\x2c\xb5\xf1\x2e\x62\x7c\xbe\xef\x4e\x8f\x0b\x11\x2b\x32"
  6184. "\xa0\xee\xc9\xd0\x4d\x64\x64\x0b\x37\xf4\xdd\x66\xf7\x8b\xb3\xad"
  6185. "\x52\x52\x6b\x65\x12\xde\x0d\x7c\xc0\x8b\x60\x01\x6c\x37\xd7\xa8"
  6186. };
  6187. int i = 0, iMax = sizeof(input) / sizeof(input[0]),
  6188. j, jMax = sizeof(hashType) / sizeof(hashType[0]);
  6189. int ret;
  6190. WOLFSSL_ENTER("hmac_sha3_test");
  6191. #ifdef HAVE_FIPS
  6192. /* FIPS requires a minimum length for HMAC keys, and "Jefe" is too
  6193. * short. Skip it in FIPS builds. */
  6194. i = 1;
  6195. #endif
  6196. for (; i < iMax; i++) {
  6197. for (j = 0; j < jMax; j++) {
  6198. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  6199. return WC_TEST_RET_ENC_EC(ret);
  6200. ret = wc_HmacSetKey(&hmac, hashType[j], (byte*)key[i],
  6201. (word32)XSTRLEN(key[i]));
  6202. if (ret != 0)
  6203. return WC_TEST_RET_ENC_EC(ret);
  6204. ret = wc_HmacUpdate(&hmac, (byte*)input[i],
  6205. (word32)XSTRLEN(input[i]));
  6206. if (ret != 0)
  6207. return WC_TEST_RET_ENC_EC(ret);
  6208. ret = wc_HmacFinal(&hmac, hash);
  6209. if (ret != 0)
  6210. return WC_TEST_RET_ENC_EC(ret);
  6211. if (XMEMCMP(hash, output[(i*jMax) + j], (size_t)hashSz[j]) != 0)
  6212. return WC_TEST_RET_ENC_NC;
  6213. wc_HmacFree(&hmac);
  6214. if (i > 0)
  6215. continue;
  6216. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  6217. ret = wc_HmacSizeByType(hashType[j]);
  6218. if (ret != hashSz[j])
  6219. return WC_TEST_RET_ENC_EC(ret);
  6220. #endif
  6221. }
  6222. }
  6223. return 0;
  6224. }
  6225. #endif
  6226. #ifdef WC_RC2
  6227. typedef struct rc2TestVector {
  6228. const char* input;
  6229. const char* output;
  6230. const char* key; /* Key, variable up to 128 bytes */
  6231. const char* iv; /* IV, 8-bytes */
  6232. int inLen;
  6233. int outLen;
  6234. int keyLen;
  6235. int effectiveKeyBits; /* Up to 1024 bits supported */
  6236. } rc2TestVector;
  6237. static wc_test_ret_t rc2_ecb_test(void)
  6238. {
  6239. wc_test_ret_t ret = 0;
  6240. byte cipher[RC2_BLOCK_SIZE];
  6241. byte plain[RC2_BLOCK_SIZE];
  6242. rc2TestVector a, b, c, d, e, f, g, h;
  6243. rc2TestVector test_rc2[8];
  6244. int times = sizeof(test_rc2) / sizeof(rc2TestVector), i;
  6245. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6246. a.output = "\xeb\xb7\x73\xf9\x93\x27\x8e\xff";
  6247. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6248. a.inLen = RC2_BLOCK_SIZE;
  6249. a.outLen = RC2_BLOCK_SIZE;
  6250. a.keyLen = 8;
  6251. a.effectiveKeyBits = 63;
  6252. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff";
  6253. b.output = "\x27\x8b\x27\xe4\x2e\x2f\x0d\x49";
  6254. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  6255. b.inLen = RC2_BLOCK_SIZE;
  6256. b.outLen = RC2_BLOCK_SIZE;
  6257. b.keyLen = 8;
  6258. b.effectiveKeyBits = 64;
  6259. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01";
  6260. c.output = "\x30\x64\x9e\xdf\x9b\xe7\xd2\xc2";
  6261. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  6262. c.inLen = RC2_BLOCK_SIZE;
  6263. c.outLen = RC2_BLOCK_SIZE;
  6264. c.keyLen = 8;
  6265. c.effectiveKeyBits = 64;
  6266. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6267. d.output = "\x61\xa8\xa2\x44\xad\xac\xcc\xf0";
  6268. d.key = "\x88";
  6269. d.inLen = RC2_BLOCK_SIZE;
  6270. d.outLen = RC2_BLOCK_SIZE;
  6271. d.keyLen = 1;
  6272. d.effectiveKeyBits = 64;
  6273. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6274. e.output = "\x6c\xcf\x43\x08\x97\x4c\x26\x7f";
  6275. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  6276. e.inLen = RC2_BLOCK_SIZE;
  6277. e.outLen = RC2_BLOCK_SIZE;
  6278. e.keyLen = 7;
  6279. e.effectiveKeyBits = 64;
  6280. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6281. f.output = "\x1a\x80\x7d\x27\x2b\xbe\x5d\xb1";
  6282. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  6283. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  6284. f.inLen = RC2_BLOCK_SIZE;
  6285. f.outLen = RC2_BLOCK_SIZE;
  6286. f.keyLen = 16;
  6287. f.effectiveKeyBits = 64;
  6288. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6289. g.output = "\x22\x69\x55\x2a\xb0\xf8\x5c\xa6";
  6290. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  6291. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  6292. g.inLen = RC2_BLOCK_SIZE;
  6293. g.outLen = RC2_BLOCK_SIZE;
  6294. g.keyLen = 16;
  6295. g.effectiveKeyBits = 128;
  6296. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6297. h.output = "\x5b\x78\xd3\xa4\x3d\xff\xf1\xf1";
  6298. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  6299. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  6300. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  6301. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  6302. "\x1e";
  6303. h.inLen = RC2_BLOCK_SIZE;
  6304. h.outLen = RC2_BLOCK_SIZE;
  6305. h.keyLen = 33;
  6306. h.effectiveKeyBits = 129;
  6307. a.iv = b.iv = c.iv = d.iv = e.iv = f.iv = g.iv = h.iv = NULL;
  6308. test_rc2[0] = a;
  6309. test_rc2[1] = b;
  6310. test_rc2[2] = c;
  6311. test_rc2[3] = d;
  6312. test_rc2[4] = e;
  6313. test_rc2[5] = f;
  6314. test_rc2[6] = g;
  6315. test_rc2[7] = h;
  6316. for (i = 0; i < times; ++i) {
  6317. Rc2 enc;
  6318. XMEMSET(cipher, 0, RC2_BLOCK_SIZE);
  6319. XMEMSET(plain, 0, RC2_BLOCK_SIZE);
  6320. ret = wc_Rc2SetKey(&enc, (byte*)test_rc2[i].key, test_rc2[i].keyLen,
  6321. NULL, test_rc2[i].effectiveKeyBits);
  6322. if (ret != 0) {
  6323. return WC_TEST_RET_ENC_EC(ret);
  6324. }
  6325. /* ECB encrypt */
  6326. ret = wc_Rc2EcbEncrypt(&enc, cipher, (byte*)test_rc2[i].input,
  6327. (word32)test_rc2[i].outLen);
  6328. if (ret != 0) {
  6329. return WC_TEST_RET_ENC_EC(ret);
  6330. }
  6331. if (XMEMCMP(cipher, test_rc2[i].output, test_rc2[i].outLen)) {
  6332. return WC_TEST_RET_ENC_NC;
  6333. }
  6334. /* ECB decrypt */
  6335. ret = wc_Rc2EcbDecrypt(&enc, plain, cipher, RC2_BLOCK_SIZE);
  6336. if (ret != 0) {
  6337. return WC_TEST_RET_ENC_EC(ret);
  6338. }
  6339. if (XMEMCMP(plain, test_rc2[i].input, RC2_BLOCK_SIZE)) {
  6340. return WC_TEST_RET_ENC_NC;
  6341. }
  6342. }
  6343. return 0;
  6344. }
  6345. static wc_test_ret_t rc2_cbc_test(void)
  6346. {
  6347. wc_test_ret_t ret = 0;
  6348. byte cipher[128];
  6349. byte plain[128];
  6350. rc2TestVector a, b, c, d, e, f, g, h, i;
  6351. rc2TestVector test_rc2[9];
  6352. int times = sizeof(test_rc2) / sizeof(rc2TestVector), j;
  6353. /* key length = 7, effective key bits = 63 */
  6354. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  6355. "\x00\x00\x00\x00\x00\x00\x00\x00";
  6356. a.output = "\xEB\xB7\x73\xF9\x93\x27\x8E\xFF"
  6357. "\xF0\x51\x77\x8B\x65\xDB\x13\x57";
  6358. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6359. a.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6360. a.inLen = RC2_BLOCK_SIZE*2;
  6361. a.outLen = RC2_BLOCK_SIZE*2;
  6362. a.keyLen = 8;
  6363. a.effectiveKeyBits = 63;
  6364. /* key length = 8, effective key bits = 64, all 0xFF */
  6365. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff"
  6366. "\xff\xff\xff\xff\xff\xff\xff\xff";
  6367. b.output = "\xA3\xA1\x12\x65\x4F\x81\xC5\xCD"
  6368. "\xB6\x94\x3E\xEA\x3E\x8B\x9D\x1F";
  6369. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  6370. b.iv = "\xff\xff\xff\xff\xff\xff\xff\xff";
  6371. b.inLen = RC2_BLOCK_SIZE*2;
  6372. b.outLen = RC2_BLOCK_SIZE*2;
  6373. b.keyLen = 8;
  6374. b.effectiveKeyBits = 64;
  6375. /* key length = 8, effective key bits = 64 */
  6376. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01"
  6377. "\x10\x00\x00\x00\x00\x00\x00\x01";
  6378. c.output = "\xB5\x70\x14\xA2\x5F\x40\xE3\x6D"
  6379. "\x81\x99\x8D\xE0\xB5\xD5\x3A\x05";
  6380. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  6381. c.iv = "\x30\x00\x00\x00\x00\x00\x00\x00";
  6382. c.inLen = RC2_BLOCK_SIZE*2;
  6383. c.outLen = RC2_BLOCK_SIZE*2;
  6384. c.keyLen = 8;
  6385. c.effectiveKeyBits = 64;
  6386. /* key length = 1, effective key bits = 64 */
  6387. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  6388. "\x00\x00\x00\x00\x00\x00\x00\x00";
  6389. d.output = "\x61\xA8\xA2\x44\xAD\xAC\xCC\xF0"
  6390. "\x6D\x19\xE8\xF1\xFC\xE7\x38\x87";
  6391. d.key = "\x88";
  6392. d.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6393. d.inLen = RC2_BLOCK_SIZE*2;
  6394. d.outLen = RC2_BLOCK_SIZE*2;
  6395. d.keyLen = 1;
  6396. d.effectiveKeyBits = 64;
  6397. /* key length = 7, effective key bits = 64 */
  6398. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  6399. "\x00\x00\x00\x00\x00\x00\x00\x00";
  6400. e.output = "\x6C\xCF\x43\x08\x97\x4C\x26\x7F"
  6401. "\xCC\x3C\x53\x57\x7C\xA1\xA4\x4B";
  6402. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  6403. e.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6404. e.inLen = RC2_BLOCK_SIZE*2;
  6405. e.outLen = RC2_BLOCK_SIZE*2;
  6406. e.keyLen = 7;
  6407. e.effectiveKeyBits = 64;
  6408. /* key length = 16, effective key bits = 64 */
  6409. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  6410. "\x00\x00\x00\x00\x00\x00\x00\x00";
  6411. f.output = "\x1A\x80\x7D\x27\x2B\xBE\x5D\xB1"
  6412. "\x64\xEF\xE1\xC3\xB8\xAD\xFB\xBA";
  6413. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  6414. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  6415. f.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6416. f.inLen = RC2_BLOCK_SIZE*2;
  6417. f.outLen = RC2_BLOCK_SIZE*2;
  6418. f.keyLen = 16;
  6419. f.effectiveKeyBits = 64;
  6420. /* key length = 16, effective bits = 128 */
  6421. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  6422. "\x00\x00\x00\x00\x00\x00\x00\x00";
  6423. g.output = "\x22\x69\x55\x2A\xB0\xF8\x5C\xA6"
  6424. "\x53\x6E\xFD\x2D\x89\xE1\x2A\x73";
  6425. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  6426. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  6427. g.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6428. g.inLen = RC2_BLOCK_SIZE*2;
  6429. g.outLen = RC2_BLOCK_SIZE*2;
  6430. g.keyLen = 16;
  6431. g.effectiveKeyBits = 128;
  6432. /* key length = 33, effective bits = 129 */
  6433. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  6434. "\x00\x00\x00\x00\x00\x00\x00\x00";
  6435. h.output = "\x5B\x78\xD3\xA4\x3D\xFF\xF1\xF1"
  6436. "\x45\x30\xA8\xD5\xC7\x7C\x46\x19";
  6437. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  6438. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  6439. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  6440. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  6441. "\x1e";
  6442. h.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6443. h.inLen = RC2_BLOCK_SIZE*2;
  6444. h.outLen = RC2_BLOCK_SIZE*2;
  6445. h.keyLen = 33;
  6446. h.effectiveKeyBits = 129;
  6447. /* key length = 10, effective bits = 40 */
  6448. i.input = "\x11\x22\x33\x44\x55\x66\x77\x88"
  6449. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00"
  6450. "\x11\x22\x33\x44\x55\x66\x77\x88"
  6451. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00";
  6452. i.output = "\x71\x2D\x11\x99\xC9\xA0\x78\x4F"
  6453. "\xCD\xF1\x1E\x3D\xFD\x21\x7E\xDB"
  6454. "\xB2\x6E\x0D\xA4\x72\xBC\x31\x51"
  6455. "\x48\xEF\x4E\x68\x3B\xDC\xCD\x7D";
  6456. i.key = "\x26\x1E\x57\x8E\xC9\x62\xBF\xB8"
  6457. "\x3E\x96";
  6458. i.iv = "\x01\x02\x03\x04\x05\x06\x07\x08";
  6459. i.inLen = RC2_BLOCK_SIZE*4;
  6460. i.outLen = RC2_BLOCK_SIZE*4;
  6461. i.keyLen = 10;
  6462. i.effectiveKeyBits = 40;
  6463. test_rc2[0] = a;
  6464. test_rc2[1] = b;
  6465. test_rc2[2] = c;
  6466. test_rc2[3] = d;
  6467. test_rc2[4] = e;
  6468. test_rc2[5] = f;
  6469. test_rc2[6] = g;
  6470. test_rc2[7] = h;
  6471. test_rc2[8] = i;
  6472. for (j = 0; j < times; ++j) {
  6473. Rc2 rc2;
  6474. XMEMSET(cipher, 0, sizeof(cipher));
  6475. XMEMSET(plain, 0, sizeof(plain));
  6476. ret = wc_Rc2SetKey(&rc2, (byte*)test_rc2[j].key, test_rc2[j].keyLen,
  6477. (byte*)test_rc2[j].iv, test_rc2[j].effectiveKeyBits);
  6478. if (ret != 0) {
  6479. return WC_TEST_RET_ENC_EC(ret);
  6480. }
  6481. ret = wc_Rc2CbcEncrypt(&rc2, cipher, (byte*)test_rc2[j].input,
  6482. test_rc2[j].inLen);
  6483. if (ret != 0) {
  6484. return WC_TEST_RET_ENC_EC(ret);
  6485. }
  6486. if (XMEMCMP(cipher, (byte*)test_rc2[j].output, test_rc2[j].outLen)) {
  6487. return WC_TEST_RET_ENC_NC;
  6488. }
  6489. /* reset IV for decrypt, since overridden by encrypt operation */
  6490. ret = wc_Rc2SetIV(&rc2, (byte*)test_rc2[j].iv);
  6491. if (ret != 0) {
  6492. return WC_TEST_RET_ENC_EC(ret);
  6493. }
  6494. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, test_rc2[j].outLen);
  6495. if (ret != 0) {
  6496. return WC_TEST_RET_ENC_EC(ret);
  6497. }
  6498. if (XMEMCMP(plain, (byte*)test_rc2[j].input, test_rc2[j].inLen)) {
  6499. return WC_TEST_RET_ENC_NC;
  6500. }
  6501. }
  6502. return 0;
  6503. }
  6504. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rc2_test(void)
  6505. {
  6506. wc_test_ret_t ret = 0;
  6507. WOLFSSL_ENTER("rc2_test");
  6508. ret = rc2_ecb_test();
  6509. if (ret != 0) {
  6510. return ret;
  6511. }
  6512. return rc2_cbc_test();
  6513. }
  6514. #endif
  6515. #ifndef NO_RC4
  6516. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t arc4_test(void)
  6517. {
  6518. byte cipher[16];
  6519. byte plain[16];
  6520. wc_test_ret_t ret;
  6521. const char* keys[] =
  6522. {
  6523. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  6524. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  6525. "\x00\x00\x00\x00\x00\x00\x00\x00",
  6526. "\xef\x01\x23\x45"
  6527. };
  6528. testVector a, b, c, d;
  6529. testVector test_arc4[4];
  6530. int times = sizeof(test_arc4) / sizeof(testVector), i;
  6531. WOLFSSL_ENTER("arc4_test");
  6532. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  6533. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  6534. a.inLen = 8;
  6535. a.outLen = 8;
  6536. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6537. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  6538. b.inLen = 8;
  6539. b.outLen = 8;
  6540. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6541. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  6542. c.inLen = 8;
  6543. c.outLen = 8;
  6544. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  6545. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  6546. d.inLen = 10;
  6547. d.outLen = 10;
  6548. test_arc4[0] = a;
  6549. test_arc4[1] = b;
  6550. test_arc4[2] = c;
  6551. test_arc4[3] = d;
  6552. for (i = 0; i < times; ++i) {
  6553. Arc4 enc;
  6554. Arc4 dec;
  6555. int keylen = 8; /* XSTRLEN with key 0x00 not good */
  6556. if (i == 3)
  6557. keylen = 4;
  6558. ret = wc_Arc4Init(&enc, HEAP_HINT, devId);
  6559. if (ret != 0)
  6560. return WC_TEST_RET_ENC_EC(ret);
  6561. ret = wc_Arc4Init(&dec, HEAP_HINT, devId);
  6562. if (ret != 0)
  6563. return WC_TEST_RET_ENC_EC(ret);
  6564. ret = wc_Arc4SetKey(&enc, (byte*)keys[i], (word32)keylen);
  6565. if (ret != 0)
  6566. return WC_TEST_RET_ENC_EC(ret);
  6567. ret = wc_Arc4SetKey(&dec, (byte*)keys[i], (word32)keylen);
  6568. if (ret != 0)
  6569. return WC_TEST_RET_ENC_EC(ret);
  6570. ret = wc_Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  6571. (word32)test_arc4[i].outLen);
  6572. if (ret != 0)
  6573. return WC_TEST_RET_ENC_EC(ret);
  6574. ret = wc_Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  6575. if (ret != 0)
  6576. return WC_TEST_RET_ENC_EC(ret);
  6577. if (XMEMCMP(plain, test_arc4[i].input, test_arc4[i].outLen))
  6578. return WC_TEST_RET_ENC_I(i);
  6579. if (XMEMCMP(cipher, test_arc4[i].output, test_arc4[i].outLen))
  6580. return WC_TEST_RET_ENC_I(i);
  6581. wc_Arc4Free(&enc);
  6582. wc_Arc4Free(&dec);
  6583. }
  6584. return 0;
  6585. }
  6586. #endif
  6587. #ifdef HAVE_CHACHA
  6588. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha_test(void)
  6589. {
  6590. ChaCha enc;
  6591. ChaCha dec;
  6592. byte cipher[128];
  6593. byte plain[128];
  6594. byte sliver[64];
  6595. byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  6596. word32 keySz = 32;
  6597. wc_test_ret_t ret = 0;
  6598. int i;
  6599. int times = 4;
  6600. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6601. {
  6602. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6603. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6604. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6605. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6606. };
  6607. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6608. {
  6609. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6610. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6611. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6612. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  6613. };
  6614. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6615. {
  6616. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6617. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6618. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6619. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6620. };
  6621. /* 128 bit key */
  6622. WOLFSSL_SMALL_STACK_STATIC const byte key4[] =
  6623. {
  6624. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6625. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6626. };
  6627. const byte* keys[] = {key1, key2, key3, key4};
  6628. WOLFSSL_SMALL_STACK_STATIC const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  6629. WOLFSSL_SMALL_STACK_STATIC const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  6630. WOLFSSL_SMALL_STACK_STATIC const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};
  6631. WOLFSSL_SMALL_STACK_STATIC const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  6632. const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4};
  6633. #ifndef BENCH_EMBEDDED
  6634. WOLFSSL_SMALL_STACK_STATIC const byte cipher_big_result[] = {
  6635. 0x06, 0xa6, 0x5d, 0x31, 0x21, 0x6c, 0xdb, 0x37, 0x48, 0x7c, 0x01, 0x9d,
  6636. 0x72, 0xdf, 0x0a, 0x5b, 0x64, 0x74, 0x20, 0xba, 0x9e, 0xe0, 0x26, 0x7a,
  6637. 0xbf, 0xdf, 0x83, 0x34, 0x3b, 0x4f, 0x94, 0x3f, 0x37, 0x89, 0xaf, 0x00,
  6638. 0xdf, 0x0f, 0x2e, 0x75, 0x16, 0x41, 0xf6, 0x7a, 0x86, 0x94, 0x9d, 0x32,
  6639. 0x56, 0xf0, 0x79, 0x71, 0x68, 0x6f, 0xa6, 0x6b, 0xc6, 0x59, 0x49, 0xf6,
  6640. 0x10, 0x34, 0x03, 0x03, 0x16, 0x53, 0x9a, 0x98, 0x2a, 0x46, 0xde, 0x17,
  6641. 0x06, 0x65, 0x70, 0xca, 0x0a, 0x1f, 0xab, 0x80, 0x26, 0x96, 0x3f, 0x3e,
  6642. 0x7a, 0x3c, 0xa8, 0x87, 0xbb, 0x65, 0xdd, 0x5e, 0x07, 0x7b, 0x34, 0xe0,
  6643. 0x56, 0xda, 0x32, 0x13, 0x30, 0xc9, 0x0c, 0xd7, 0xba, 0xe4, 0x1f, 0xa6,
  6644. 0x91, 0x4f, 0x72, 0x9f, 0xd9, 0x5c, 0x62, 0x7d, 0xa6, 0xc2, 0xbc, 0x87,
  6645. 0xae, 0x64, 0x11, 0x94, 0x3b, 0xbc, 0x6c, 0x23, 0xbd, 0x7d, 0x00, 0xb4,
  6646. 0x99, 0xf2, 0x68, 0xb5, 0x59, 0x70, 0x93, 0xad, 0x69, 0xd0, 0xb1, 0x28,
  6647. 0x70, 0x92, 0xeb, 0xec, 0x39, 0x80, 0x82, 0xde, 0x44, 0xe2, 0x8a, 0x26,
  6648. 0xb3, 0xe9, 0x45, 0xcf, 0x83, 0x76, 0x9f, 0x6a, 0xa0, 0x46, 0x4a, 0x3d,
  6649. 0x26, 0x56, 0xaf, 0x49, 0x41, 0x26, 0x1b, 0x6a, 0x41, 0x37, 0x65, 0x91,
  6650. 0x72, 0xc4, 0xe7, 0x3c, 0x17, 0x31, 0xae, 0x2e, 0x2b, 0x31, 0x45, 0xe4,
  6651. 0x93, 0xd3, 0x10, 0xaa, 0xc5, 0x62, 0xd5, 0x11, 0x4b, 0x57, 0x1d, 0xad,
  6652. 0x48, 0x06, 0xd0, 0x0d, 0x98, 0xa5, 0xc6, 0x5b, 0xd0, 0x9e, 0x22, 0xc0,
  6653. 0x00, 0x32, 0x5a, 0xf5, 0x1c, 0x89, 0x6d, 0x54, 0x97, 0x55, 0x6b, 0x46,
  6654. 0xc5, 0xc7, 0xc4, 0x48, 0x9c, 0xbf, 0x47, 0xdc, 0x03, 0xc4, 0x1b, 0xcb,
  6655. 0x65, 0xa6, 0x91, 0x9d, 0x6d, 0xf1, 0xb0, 0x7a, 0x4d, 0x3b, 0x03, 0x95,
  6656. 0xf4, 0x8b, 0x0b, 0xae, 0x39, 0xff, 0x3f, 0xf6, 0xc0, 0x14, 0x18, 0x8a,
  6657. 0xe5, 0x19, 0xbd, 0xc1, 0xb4, 0x05, 0x4e, 0x29, 0x2f, 0x0b, 0x33, 0x76,
  6658. 0x28, 0x16, 0xa4, 0xa6, 0x93, 0x04, 0xb5, 0x55, 0x6b, 0x89, 0x3d, 0xa5,
  6659. 0x0f, 0xd3, 0xad, 0xfa, 0xd9, 0xfd, 0x05, 0x5d, 0x48, 0x94, 0x25, 0x5a,
  6660. 0x2c, 0x9a, 0x94, 0x80, 0xb0, 0xe7, 0xcb, 0x4d, 0x77, 0xbf, 0xca, 0xd8,
  6661. 0x55, 0x48, 0xbd, 0x66, 0xb1, 0x85, 0x81, 0xb1, 0x37, 0x79, 0xab, 0x52,
  6662. 0x08, 0x14, 0x12, 0xac, 0xcd, 0x45, 0x4d, 0x53, 0x6b, 0xca, 0x96, 0xc7,
  6663. 0x3b, 0x2f, 0x73, 0xb1, 0x5a, 0x23, 0xbd, 0x65, 0xd5, 0xea, 0x17, 0xb3,
  6664. 0xdc, 0xa1, 0x17, 0x1b, 0x2d, 0xb3, 0x9c, 0xd0, 0xdb, 0x41, 0x77, 0xef,
  6665. 0x93, 0x20, 0x52, 0x3e, 0x9d, 0xf5, 0xbf, 0x33, 0xf7, 0x52, 0xc1, 0x90,
  6666. 0xa0, 0x15, 0x17, 0xce, 0xf7, 0xf7, 0xd0, 0x3a, 0x3b, 0xd1, 0x72, 0x56,
  6667. 0x31, 0x81, 0xae, 0x60, 0xab, 0x40, 0xc1, 0xd1, 0x28, 0x77, 0x53, 0xac,
  6668. 0x9f, 0x11, 0x0a, 0x88, 0x36, 0x4b, 0xda, 0x57, 0xa7, 0x28, 0x5c, 0x85,
  6669. 0xd3, 0x85, 0x9b, 0x79, 0xad, 0x05, 0x1c, 0x37, 0x14, 0x5e, 0x0d, 0xd0,
  6670. 0x23, 0x03, 0x42, 0x1d, 0x48, 0x5d, 0xc5, 0x3c, 0x5a, 0x08, 0xa9, 0x0d,
  6671. 0x6e, 0x82, 0x7c, 0x2e, 0x3c, 0x41, 0xcc, 0x96, 0x8e, 0xad, 0xee, 0x2a,
  6672. 0x61, 0x0b, 0x16, 0x0f, 0xa9, 0x24, 0x40, 0x85, 0xbc, 0x9f, 0x28, 0x8d,
  6673. 0xe6, 0x68, 0x4d, 0x8f, 0x30, 0x48, 0xd9, 0x73, 0x73, 0x6c, 0x9a, 0x7f,
  6674. 0x67, 0xf7, 0xde, 0x4c, 0x0a, 0x8b, 0xe4, 0xb3, 0x08, 0x2a, 0x52, 0xda,
  6675. 0x54, 0xee, 0xcd, 0xb5, 0x62, 0x4a, 0x26, 0x20, 0xfb, 0x40, 0xbb, 0x39,
  6676. 0x3a, 0x0f, 0x09, 0xe8, 0x00, 0xd1, 0x24, 0x97, 0x60, 0xe9, 0x83, 0x83,
  6677. 0xfe, 0x9f, 0x9c, 0x15, 0xcf, 0x69, 0x03, 0x9f, 0x03, 0xe1, 0xe8, 0x6e,
  6678. 0xbd, 0x87, 0x58, 0x68, 0xee, 0xec, 0xd8, 0x29, 0x46, 0x23, 0x49, 0x92,
  6679. 0x72, 0x95, 0x5b, 0x49, 0xca, 0xe0, 0x45, 0x59, 0xb2, 0xca, 0xf4, 0xfc,
  6680. 0xb7, 0x59, 0x37, 0x49, 0x28, 0xbc, 0xf3, 0xd7, 0x61, 0xbc, 0x4b, 0xf3,
  6681. 0xa9, 0x4b, 0x2f, 0x05, 0xa8, 0x01, 0xa5, 0xdc, 0x00, 0x6e, 0x01, 0xb6,
  6682. 0x45, 0x3c, 0xd5, 0x49, 0x7d, 0x5c, 0x25, 0xe8, 0x31, 0x87, 0xb2, 0xb9,
  6683. 0xbf, 0xb3, 0x01, 0x62, 0x0c, 0xd0, 0x48, 0x77, 0xa2, 0x34, 0x0f, 0x16,
  6684. 0x22, 0x28, 0xee, 0x54, 0x08, 0x93, 0x3b, 0xe4, 0xde, 0x7e, 0x63, 0xf7,
  6685. 0x97, 0x16, 0x5d, 0x71, 0x58, 0xc2, 0x2e, 0xf2, 0x36, 0xa6, 0x12, 0x65,
  6686. 0x94, 0x17, 0xac, 0x66, 0x23, 0x7e, 0xc6, 0x72, 0x79, 0x24, 0xce, 0x8f,
  6687. 0x55, 0x19, 0x97, 0x44, 0xfc, 0x55, 0xec, 0x85, 0x26, 0x27, 0xdb, 0x38,
  6688. 0xb1, 0x42, 0x0a, 0xdd, 0x05, 0x99, 0x28, 0xeb, 0x03, 0x6c, 0x9a, 0xe9,
  6689. 0x17, 0xf6, 0x2c, 0xb0, 0xfe, 0xe7, 0xa4, 0xa7, 0x31, 0xda, 0x4d, 0xb0,
  6690. 0x29, 0xdb, 0xdd, 0x8d, 0x12, 0x13, 0x9c, 0xb4, 0xcc, 0x83, 0x97, 0xfb,
  6691. 0x1a, 0xdc, 0x08, 0xd6, 0x30, 0x62, 0xe8, 0xeb, 0x8b, 0x61, 0xcb, 0x1d,
  6692. 0x06, 0xe3, 0xa5, 0x4d, 0x35, 0xdb, 0x59, 0xa8, 0x2d, 0x87, 0x27, 0x44,
  6693. 0x6f, 0xc0, 0x38, 0x97, 0xe4, 0x85, 0x00, 0x02, 0x09, 0xf6, 0x69, 0x3a,
  6694. 0xcf, 0x08, 0x1b, 0x21, 0xbb, 0x79, 0xb1, 0xa1, 0x34, 0x09, 0xe0, 0x80,
  6695. 0xca, 0xb0, 0x78, 0x8a, 0x11, 0x97, 0xd4, 0x07, 0xbe, 0x1b, 0x6a, 0x5d,
  6696. 0xdb, 0xd6, 0x1f, 0x76, 0x6b, 0x16, 0xf0, 0x58, 0x84, 0x5f, 0x59, 0xce,
  6697. 0x62, 0x34, 0xc3, 0xdf, 0x94, 0xb8, 0x2f, 0x84, 0x68, 0xf0, 0xb8, 0x51,
  6698. 0xd9, 0x6d, 0x8e, 0x4a, 0x1d, 0xe6, 0x5c, 0xd8, 0x86, 0x25, 0xe3, 0x24,
  6699. 0xfd, 0x21, 0x61, 0x13, 0x48, 0x3e, 0xf6, 0x7d, 0xa6, 0x71, 0x9b, 0xd2,
  6700. 0x6e, 0xe6, 0xd2, 0x08, 0x94, 0x62, 0x6c, 0x98, 0xfe, 0x2f, 0x9c, 0x88,
  6701. 0x7e, 0x78, 0x15, 0x02, 0x00, 0xf0, 0xba, 0x24, 0x91, 0xf2, 0xdc, 0x47,
  6702. 0x51, 0x4d, 0x15, 0x5e, 0x91, 0x5f, 0x57, 0x5b, 0x1d, 0x35, 0x24, 0x45,
  6703. 0x75, 0x9b, 0x88, 0x75, 0xf1, 0x2f, 0x85, 0xe7, 0x89, 0xd1, 0x01, 0xb4,
  6704. 0xc8, 0x18, 0xb7, 0x97, 0xef, 0x4b, 0x90, 0xf4, 0xbf, 0x10, 0x27, 0x3c,
  6705. 0x60, 0xff, 0xc4, 0x94, 0x20, 0x2f, 0x93, 0x4b, 0x4d, 0xe3, 0x80, 0xf7,
  6706. 0x2c, 0x71, 0xd9, 0xe3, 0x68, 0xb4, 0x77, 0x2b, 0xc7, 0x0d, 0x39, 0x92,
  6707. 0xef, 0x91, 0x0d, 0xb2, 0x11, 0x50, 0x0e, 0xe8, 0xad, 0x3b, 0xf6, 0xb5,
  6708. 0xc6, 0x14, 0x4d, 0x33, 0x53, 0xa7, 0x60, 0x15, 0xc7, 0x27, 0x51, 0xdc,
  6709. 0x54, 0x29, 0xa7, 0x0d, 0x6a, 0x7b, 0x72, 0x13, 0xad, 0x7d, 0x41, 0x19,
  6710. 0x4e, 0x42, 0x49, 0xcc, 0x42, 0xe4, 0xbd, 0x99, 0x13, 0xd9, 0x7f, 0xf3,
  6711. 0x38, 0xa4, 0xb6, 0x33, 0xed, 0x07, 0x48, 0x7e, 0x8e, 0x82, 0xfe, 0x3a,
  6712. 0x9d, 0x75, 0x93, 0xba, 0x25, 0x4e, 0x37, 0x3c, 0x0c, 0xd5, 0x69, 0xa9,
  6713. 0x2d, 0x9e, 0xfd, 0xe8, 0xbb, 0xf5, 0x0c, 0xe2, 0x86, 0xb9, 0x5e, 0x6f,
  6714. 0x28, 0xe4, 0x19, 0xb3, 0x0b, 0xa4, 0x86, 0xd7, 0x24, 0xd0, 0xb8, 0x89,
  6715. 0x7b, 0x76, 0xec, 0x05, 0x10, 0x5b, 0x68, 0xe9, 0x58, 0x66, 0xa3, 0xc5,
  6716. 0xb6, 0x63, 0x20, 0x0e, 0x0e, 0xea, 0x3d, 0x61, 0x5e, 0xda, 0x3d, 0x3c,
  6717. 0xf9, 0xfd, 0xed, 0xa9, 0xdb, 0x52, 0x94, 0x8a, 0x00, 0xca, 0x3c, 0x8d,
  6718. 0x66, 0x8f, 0xb0, 0xf0, 0x5a, 0xca, 0x3f, 0x63, 0x71, 0xbf, 0xca, 0x99,
  6719. 0x37, 0x9b, 0x75, 0x97, 0x89, 0x10, 0x6e, 0xcf, 0xf2, 0xf5, 0xe3, 0xd5,
  6720. 0x45, 0x9b, 0xad, 0x10, 0x71, 0x6c, 0x5f, 0x6f, 0x7f, 0x22, 0x77, 0x18,
  6721. 0x2f, 0xf9, 0x99, 0xc5, 0x69, 0x58, 0x03, 0x12, 0x86, 0x82, 0x3e, 0xbf,
  6722. 0xc2, 0x12, 0x35, 0x43, 0xa3, 0xd9, 0x18, 0x4f, 0x41, 0x11, 0x6b, 0xf3,
  6723. 0x67, 0xaf, 0x3d, 0x78, 0xe4, 0x22, 0x2d, 0xb3, 0x48, 0x43, 0x31, 0x1d,
  6724. 0xef, 0xa8, 0xba, 0x49, 0x8e, 0xa9, 0xa7, 0xb6, 0x18, 0x77, 0x84, 0xca,
  6725. 0xbd, 0xa2, 0x02, 0x1b, 0x6a, 0xf8, 0x5f, 0xda, 0xff, 0xcf, 0x01, 0x6a,
  6726. 0x86, 0x69, 0xa9, 0xe9, 0xcb, 0x60, 0x1e, 0x15, 0xdc, 0x8f, 0x5d, 0x39,
  6727. 0xb5, 0xce, 0x55, 0x5f, 0x47, 0x97, 0xb1, 0x19, 0x6e, 0x21, 0xd6, 0x13,
  6728. 0x39, 0xb2, 0x24, 0xe0, 0x62, 0x82, 0x9f, 0xed, 0x12, 0x81, 0xed, 0xee,
  6729. 0xab, 0xd0, 0x2f, 0x19, 0x89, 0x3f, 0x57, 0x2e, 0xc2, 0xe2, 0x67, 0xe8,
  6730. 0xae, 0x03, 0x56, 0xba, 0xd4, 0xd0, 0xa4, 0x89, 0x03, 0x06, 0x5b, 0xcc,
  6731. 0xf2, 0x22, 0xb8, 0x0e, 0x76, 0x79, 0x4a, 0x42, 0x1d, 0x37, 0x51, 0x5a,
  6732. 0xaa, 0x46, 0x6c, 0x2a, 0xdd, 0x66, 0xfe, 0xc6, 0x68, 0xc3, 0x38, 0xa2,
  6733. 0xae, 0x5b, 0x98, 0x24, 0x5d, 0x43, 0x05, 0x82, 0x38, 0x12, 0xd3, 0xd1,
  6734. 0x75, 0x2d, 0x4f, 0x61, 0xbd, 0xb9, 0x10, 0x87, 0x44, 0x2a, 0x78, 0x07,
  6735. 0xff, 0xf4, 0x0f, 0xa1, 0xf3, 0x68, 0x9f, 0xbe, 0xae, 0xa2, 0x91, 0xf0,
  6736. 0xc7, 0x55, 0x7a, 0x52, 0xd5, 0xa3, 0x8d, 0x6f, 0xe4, 0x90, 0x5c, 0xf3,
  6737. 0x5f, 0xce, 0x3d, 0x23, 0xf9, 0x8e, 0xae, 0x14, 0xfb, 0x82, 0x9a, 0xa3,
  6738. 0x04, 0x5f, 0xbf, 0xad, 0x3e, 0xf2, 0x97, 0x0a, 0x60, 0x40, 0x70, 0x19,
  6739. 0x72, 0xad, 0x66, 0xfb, 0x78, 0x1b, 0x84, 0x6c, 0x98, 0xbc, 0x8c, 0xf8,
  6740. 0x4f, 0xcb, 0xb5, 0xf6, 0xaf, 0x7a, 0xb7, 0x93, 0xef, 0x67, 0x48, 0x02,
  6741. 0x2c, 0xcb, 0xe6, 0x77, 0x0f, 0x7b, 0xc1, 0xee, 0xc5, 0xb6, 0x2d, 0x7e,
  6742. 0x62, 0xa0, 0xc0, 0xa7, 0xa5, 0x80, 0x31, 0x92, 0x50, 0xa1, 0x28, 0x22,
  6743. 0x95, 0x03, 0x17, 0xd1, 0x0f, 0xf6, 0x08, 0xe5, 0xec
  6744. };
  6745. #define CHACHA_BIG_TEST_SIZE 1305
  6746. #if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC)
  6747. byte cipher_big[CHACHA_BIG_TEST_SIZE] = {0};
  6748. byte plain_big[CHACHA_BIG_TEST_SIZE] = {0};
  6749. byte input_big[CHACHA_BIG_TEST_SIZE] = {0};
  6750. #else
  6751. byte* cipher_big;
  6752. byte* plain_big;
  6753. byte* input_big;
  6754. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  6755. int block_size;
  6756. #endif /* BENCH_EMBEDDED */
  6757. byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90};
  6758. byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96};
  6759. byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5};
  6760. byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd};
  6761. byte* test_chacha[4];
  6762. test_chacha[0] = a;
  6763. test_chacha[1] = b;
  6764. test_chacha[2] = c;
  6765. test_chacha[3] = d;
  6766. WOLFSSL_ENTER("chacha_test");
  6767. #ifndef BENCH_EMBEDDED
  6768. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6769. cipher_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  6770. DYNAMIC_TYPE_TMP_BUFFER);
  6771. if (cipher_big == NULL) {
  6772. return MEMORY_E;
  6773. }
  6774. plain_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  6775. DYNAMIC_TYPE_TMP_BUFFER);
  6776. if (plain_big == NULL) {
  6777. return MEMORY_E;
  6778. }
  6779. input_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  6780. DYNAMIC_TYPE_TMP_BUFFER);
  6781. if (input_big == NULL) {
  6782. return MEMORY_E;
  6783. }
  6784. XMEMSET(cipher_big, 0, CHACHA_BIG_TEST_SIZE);
  6785. XMEMSET(plain_big, 0, CHACHA_BIG_TEST_SIZE);
  6786. XMEMSET(input_big, 0, CHACHA_BIG_TEST_SIZE);
  6787. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  6788. #endif /* BENCH_EMBEDDED */
  6789. for (i = 0; i < times; ++i) {
  6790. if (i < 3) {
  6791. keySz = 32;
  6792. }
  6793. else {
  6794. keySz = 16;
  6795. }
  6796. XMEMCPY(plain, keys[i], keySz);
  6797. XMEMSET(cipher, 0, 32);
  6798. XMEMCPY(cipher + 4, ivs[i], 8);
  6799. ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
  6800. ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
  6801. if (ret != 0)
  6802. return ret;
  6803. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  6804. ret |= wc_Chacha_SetIV(&dec, cipher, 0);
  6805. if (ret != 0)
  6806. return ret;
  6807. XMEMCPY(plain, input, 8);
  6808. ret |= wc_Chacha_Process(&enc, cipher, plain, (word32)8);
  6809. ret |= wc_Chacha_Process(&dec, plain, cipher, (word32)8);
  6810. if (ret != 0)
  6811. return ret;
  6812. if (XMEMCMP(test_chacha[i], cipher, 8))
  6813. return WC_TEST_RET_ENC_I(i);
  6814. if (XMEMCMP(plain, input, 8))
  6815. return WC_TEST_RET_ENC_I(i);
  6816. }
  6817. /* test of starting at a different counter
  6818. encrypts all of the information and decrypts starting at 2nd chunk */
  6819. XMEMSET(plain, 0, sizeof(plain));
  6820. XMEMSET(sliver, 1, sizeof(sliver)); /* set as 1's to not match plain */
  6821. XMEMSET(cipher, 0, sizeof(cipher));
  6822. XMEMCPY(cipher + 4, ivs[0], 8);
  6823. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  6824. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  6825. if (ret != 0)
  6826. return ret;
  6827. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  6828. ret |= wc_Chacha_SetIV(&dec, cipher, 1);
  6829. if (ret != 0)
  6830. return ret;
  6831. ret |= wc_Chacha_Process(&enc, cipher, plain, sizeof(plain));
  6832. ret |= wc_Chacha_Process(&dec, sliver, cipher + 64, sizeof(sliver));
  6833. if (ret != 0)
  6834. return ret;
  6835. if (XMEMCMP(plain + 64, sliver, 64))
  6836. return WC_TEST_RET_ENC_NC;
  6837. #ifndef BENCH_EMBEDDED
  6838. /* test of encrypting more data */
  6839. keySz = 32;
  6840. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  6841. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  6842. if (ret != 0)
  6843. return ret;
  6844. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  6845. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  6846. if (ret != 0)
  6847. return ret;
  6848. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, CHACHA_BIG_TEST_SIZE);
  6849. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big,
  6850. CHACHA_BIG_TEST_SIZE);
  6851. if (ret != 0)
  6852. return ret;
  6853. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  6854. return WC_TEST_RET_ENC_NC;
  6855. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  6856. return WC_TEST_RET_ENC_NC;
  6857. for (i = 0; i < 18; ++i) {
  6858. /* this will test all paths
  6859. * block sizes: 1 3 7 15 31 63 127 255 511 (i = 0- 8)
  6860. * 2 4 8 16 32 64 128 256 512 (i = 9-17)
  6861. */
  6862. block_size = (2 << (i%9)) - (i<9?1:0);
  6863. keySz = 32;
  6864. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  6865. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  6866. if (ret != 0)
  6867. return ret;
  6868. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  6869. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  6870. if (ret != 0)
  6871. return ret;
  6872. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big , (word32)block_size);
  6873. ret |= wc_Chacha_Process(&dec, plain_big , cipher_big, (word32)block_size);
  6874. if (ret != 0)
  6875. return ret;
  6876. if (XMEMCMP(plain_big, input_big, block_size))
  6877. return WC_TEST_RET_ENC_I(i);
  6878. if (XMEMCMP(cipher_big, cipher_big_result, block_size))
  6879. return WC_TEST_RET_ENC_I(i);
  6880. }
  6881. /* Streaming test */
  6882. for (i = 1; i <= (int)CHACHA_CHUNK_BYTES + 1; i++) {
  6883. int j, rem;
  6884. ret = wc_Chacha_SetKey(&enc, keys[0], keySz);
  6885. if (ret != 0)
  6886. return WC_TEST_RET_ENC_EC(ret);
  6887. ret = wc_Chacha_SetKey(&dec, keys[0], keySz);
  6888. if (ret != 0)
  6889. return WC_TEST_RET_ENC_EC(ret);
  6890. ret = wc_Chacha_SetIV(&enc, ivs[2], 0);
  6891. if (ret != 0)
  6892. return WC_TEST_RET_ENC_EC(ret);
  6893. ret = wc_Chacha_SetIV(&dec, ivs[2], 0);
  6894. if (ret != 0)
  6895. return WC_TEST_RET_ENC_EC(ret);
  6896. for (j = 0; j < CHACHA_BIG_TEST_SIZE - i; j+= i) {
  6897. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, (word32)i);
  6898. if (ret != 0)
  6899. return WC_TEST_RET_ENC_EC(ret);
  6900. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, (word32)i);
  6901. if (ret != 0)
  6902. return WC_TEST_RET_ENC_EC(ret);
  6903. }
  6904. rem = CHACHA_BIG_TEST_SIZE - j;
  6905. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, (word32)rem);
  6906. if (ret != 0)
  6907. return WC_TEST_RET_ENC_EC(ret);
  6908. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, (word32)rem);
  6909. if (ret != 0)
  6910. return WC_TEST_RET_ENC_EC(ret);
  6911. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  6912. return WC_TEST_RET_ENC_NC;
  6913. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  6914. return WC_TEST_RET_ENC_NC;
  6915. }
  6916. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6917. XFREE(cipher_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6918. XFREE(plain_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6919. XFREE(input_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6920. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  6921. #endif /* BENCH_EMBEDDED */
  6922. return 0;
  6923. }
  6924. #endif /* HAVE_CHACHA */
  6925. #ifdef HAVE_POLY1305
  6926. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t poly1305_test(void)
  6927. {
  6928. byte tag[16];
  6929. Poly1305 enc;
  6930. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6931. {
  6932. 0x43,0x72,0x79,0x70,0x74,0x6f,0x67,0x72,
  6933. 0x61,0x70,0x68,0x69,0x63,0x20,0x46,0x6f,
  6934. 0x72,0x75,0x6d,0x20,0x52,0x65,0x73,0x65,
  6935. 0x61,0x72,0x63,0x68,0x20,0x47,0x72,0x6f,
  6936. 0x75,0x70
  6937. };
  6938. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6939. {
  6940. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,
  6941. 0x6c,0x64,0x21
  6942. };
  6943. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6944. {
  6945. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6946. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6947. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6948. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6949. };
  6950. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] =
  6951. {
  6952. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  6953. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  6954. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  6955. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  6956. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  6957. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  6958. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  6959. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  6960. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  6961. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  6962. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6963. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  6964. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  6965. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  6966. 0x61,0x16
  6967. };
  6968. WOLFSSL_SMALL_STACK_STATIC const byte msg5[] =
  6969. {
  6970. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  6971. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  6972. };
  6973. WOLFSSL_SMALL_STACK_STATIC const byte msg6[] =
  6974. {
  6975. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  6976. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  6977. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  6978. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  6979. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  6980. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  6981. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6982. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  6983. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  6984. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  6985. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6986. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  6987. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  6988. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  6989. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  6990. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6991. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  6992. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  6993. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  6994. 0x61,0x16
  6995. };
  6996. byte additional[] =
  6997. {
  6998. 0x50,0x51,0x52,0x53,0xc0,0xc1,0xc2,0xc3,
  6999. 0xc4,0xc5,0xc6,0xc7
  7000. };
  7001. WOLFSSL_SMALL_STACK_STATIC const byte correct0[] =
  7002. {
  7003. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  7004. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  7005. };
  7006. WOLFSSL_SMALL_STACK_STATIC const byte correct1[] =
  7007. {
  7008. 0xa8,0x06,0x1d,0xc1,0x30,0x51,0x36,0xc6,
  7009. 0xc2,0x2b,0x8b,0xaf,0x0c,0x01,0x27,0xa9
  7010. };
  7011. WOLFSSL_SMALL_STACK_STATIC const byte correct2[] =
  7012. {
  7013. 0xa6,0xf7,0x45,0x00,0x8f,0x81,0xc9,0x16,
  7014. 0xa2,0x0d,0xcc,0x74,0xee,0xf2,0xb2,0xf0
  7015. };
  7016. WOLFSSL_SMALL_STACK_STATIC const byte correct3[] =
  7017. {
  7018. 0x49,0xec,0x78,0x09,0x0e,0x48,0x1e,0xc6,
  7019. 0xc2,0x6b,0x33,0xb9,0x1c,0xcc,0x03,0x07
  7020. };
  7021. WOLFSSL_SMALL_STACK_STATIC const byte correct4[] =
  7022. {
  7023. 0x1a,0xe1,0x0b,0x59,0x4f,0x09,0xe2,0x6a,
  7024. 0x7e,0x90,0x2e,0xcb,0xd0,0x60,0x06,0x91
  7025. };
  7026. WOLFSSL_SMALL_STACK_STATIC const byte correct5[] =
  7027. {
  7028. 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7029. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7030. };
  7031. WOLFSSL_SMALL_STACK_STATIC const byte correct6[] =
  7032. {
  7033. 0xea,0x11,0x5c,0x4f,0xd0,0xc0,0x10,0xae,
  7034. 0xf7,0xdf,0xda,0x77,0xa2,0xe9,0xaf,0xca
  7035. };
  7036. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  7037. 0x85,0xd6,0xbe,0x78,0x57,0x55,0x6d,0x33,
  7038. 0x7f,0x44,0x52,0xfe,0x42,0xd5,0x06,0xa8,
  7039. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  7040. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  7041. };
  7042. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  7043. 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,
  7044. 0x33,0x32,0x2d,0x62,0x79,0x74,0x65,0x20,
  7045. 0x6b,0x65,0x79,0x20,0x66,0x6f,0x72,0x20,
  7046. 0x50,0x6f,0x6c,0x79,0x31,0x33,0x30,0x35
  7047. };
  7048. WOLFSSL_SMALL_STACK_STATIC const byte key4[] = {
  7049. 0x7b,0xac,0x2b,0x25,0x2d,0xb4,0x47,0xaf,
  7050. 0x09,0xb6,0x7a,0x55,0xa4,0xe9,0x55,0x84,
  7051. 0x0a,0xe1,0xd6,0x73,0x10,0x75,0xd9,0xeb,
  7052. 0x2a,0x93,0x75,0x78,0x3e,0xd5,0x53,0xff
  7053. };
  7054. WOLFSSL_SMALL_STACK_STATIC const byte key5[] = {
  7055. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7056. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7057. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7058. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7059. };
  7060. const byte* msgs[] = {NULL, msg1, msg2, msg3, msg5, msg6};
  7061. word32 szm[] = {0, sizeof(msg1), sizeof(msg2),
  7062. sizeof(msg3), sizeof(msg5), sizeof(msg6)};
  7063. const byte* keys[] = {key, key, key2, key2, key5, key};
  7064. const byte* tests[] = {correct0, correct1, correct2, correct3, correct5,
  7065. correct6};
  7066. int i;
  7067. wc_test_ret_t ret = 0;
  7068. WOLFSSL_ENTER("poly1305_test");
  7069. for (i = 0; i < 6; i++) {
  7070. ret = wc_Poly1305SetKey(&enc, keys[i], 32);
  7071. if (ret != 0)
  7072. return WC_TEST_RET_ENC_I(i);
  7073. ret = wc_Poly1305Update(&enc, msgs[i], szm[i]);
  7074. if (ret != 0)
  7075. return WC_TEST_RET_ENC_I(i);
  7076. ret = wc_Poly1305Final(&enc, tag);
  7077. if (ret != 0)
  7078. return WC_TEST_RET_ENC_I(i);
  7079. if (XMEMCMP(tag, tests[i], sizeof(tag)))
  7080. return WC_TEST_RET_ENC_I(i);
  7081. }
  7082. /* Check TLS MAC function from 2.8.2 https://tools.ietf.org/html/rfc7539 */
  7083. XMEMSET(tag, 0, sizeof(tag));
  7084. ret = wc_Poly1305SetKey(&enc, key4, sizeof(key4));
  7085. if (ret != 0)
  7086. return WC_TEST_RET_ENC_EC(ret);
  7087. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  7088. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  7089. if (ret != 0)
  7090. return WC_TEST_RET_ENC_EC(ret);
  7091. if (XMEMCMP(tag, correct4, sizeof(tag)))
  7092. return WC_TEST_RET_ENC_NC;
  7093. /* Check fail of TLS MAC function if altering additional data */
  7094. XMEMSET(tag, 0, sizeof(tag));
  7095. additional[0]++;
  7096. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  7097. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  7098. if (ret != 0)
  7099. return WC_TEST_RET_ENC_EC(ret);
  7100. if (XMEMCMP(tag, correct4, sizeof(tag)) == 0)
  7101. return WC_TEST_RET_ENC_NC;
  7102. return 0;
  7103. }
  7104. #endif /* HAVE_POLY1305 */
  7105. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  7106. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha20_poly1305_aead_test(void)
  7107. {
  7108. /* Test #1 from Section 2.8.2 of draft-irtf-cfrg-chacha20-poly1305-10 */
  7109. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  7110. WOLFSSL_SMALL_STACK_STATIC const byte key1[] = {
  7111. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  7112. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  7113. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  7114. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  7115. };
  7116. WOLFSSL_SMALL_STACK_STATIC const byte plaintext1[] = {
  7117. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  7118. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  7119. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  7120. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  7121. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  7122. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  7123. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  7124. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  7125. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  7126. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  7127. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  7128. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  7129. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  7130. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  7131. 0x74, 0x2e
  7132. };
  7133. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] = {
  7134. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  7135. 0x44, 0x45, 0x46, 0x47
  7136. };
  7137. WOLFSSL_SMALL_STACK_STATIC const byte aad1[] = { /* additional data */
  7138. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  7139. 0xc4, 0xc5, 0xc6, 0xc7
  7140. };
  7141. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] = { /* expected output from operation */
  7142. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  7143. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  7144. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  7145. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  7146. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  7147. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  7148. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  7149. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  7150. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  7151. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  7152. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  7153. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  7154. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  7155. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  7156. 0x61, 0x16
  7157. };
  7158. WOLFSSL_SMALL_STACK_STATIC const byte authTag1[] = { /* expected output from operation */
  7159. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  7160. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  7161. };
  7162. /* Test #2 from Appendix A.2 in draft-irtf-cfrg-chacha20-poly1305-10 */
  7163. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  7164. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  7165. 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
  7166. 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
  7167. 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
  7168. 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
  7169. };
  7170. WOLFSSL_SMALL_STACK_STATIC const byte plaintext2[] = {
  7171. 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
  7172. 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
  7173. 0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
  7174. 0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  7175. 0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69,
  7176. 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20,
  7177. 0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20,
  7178. 0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d,
  7179. 0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e,
  7180. 0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65,
  7181. 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
  7182. 0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63,
  7183. 0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f,
  7184. 0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64,
  7185. 0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65,
  7186. 0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  7187. 0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
  7188. 0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e,
  7189. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
  7190. 0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72,
  7191. 0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20,
  7192. 0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65,
  7193. 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61,
  7194. 0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
  7195. 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65,
  7196. 0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
  7197. 0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20,
  7198. 0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
  7199. 0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
  7200. 0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20,
  7201. 0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b,
  7202. 0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67,
  7203. 0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80,
  7204. 0x9d
  7205. };
  7206. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  7207. 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  7208. 0x05, 0x06, 0x07, 0x08
  7209. };
  7210. WOLFSSL_SMALL_STACK_STATIC const byte aad2[] = { /* additional data */
  7211. 0xf3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
  7212. 0x00, 0x00, 0x4e, 0x91
  7213. };
  7214. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] = { /* expected output from operation */
  7215. 0x64, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4,
  7216. 0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd,
  7217. 0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89,
  7218. 0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2,
  7219. 0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee,
  7220. 0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0,
  7221. 0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00,
  7222. 0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf,
  7223. 0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce,
  7224. 0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81,
  7225. 0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd,
  7226. 0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55,
  7227. 0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61,
  7228. 0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38,
  7229. 0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0,
  7230. 0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4,
  7231. 0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46,
  7232. 0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9,
  7233. 0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e,
  7234. 0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e,
  7235. 0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15,
  7236. 0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a,
  7237. 0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea,
  7238. 0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a,
  7239. 0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99,
  7240. 0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e,
  7241. 0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10,
  7242. 0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10,
  7243. 0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94,
  7244. 0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30,
  7245. 0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf,
  7246. 0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29,
  7247. 0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70,
  7248. 0x9b
  7249. };
  7250. WOLFSSL_SMALL_STACK_STATIC const byte authTag2[] = { /* expected output from operation */
  7251. 0xee, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22,
  7252. 0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38
  7253. };
  7254. byte generatedCiphertext[265]; /* max plaintext2/cipher2 */
  7255. byte generatedPlaintext[265]; /* max plaintext2/cipher2 */
  7256. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  7257. ChaChaPoly_Aead aead;
  7258. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  7259. #define TEST_SMALL_CHACHA_CHUNKS 32
  7260. #else
  7261. #define TEST_SMALL_CHACHA_CHUNKS 64
  7262. #endif
  7263. #ifdef TEST_SMALL_CHACHA_CHUNKS
  7264. word32 testLen;
  7265. #endif
  7266. wc_test_ret_t err;
  7267. WOLFSSL_ENTER("chacha20_poly1305_aead_test");
  7268. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  7269. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  7270. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  7271. /* Parameter Validation testing */
  7272. /* Encrypt */
  7273. err = wc_ChaCha20Poly1305_Encrypt(NULL, iv1, aad1, sizeof(aad1), plaintext1,
  7274. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  7275. if (err != BAD_FUNC_ARG)
  7276. return WC_TEST_RET_ENC_EC(err);
  7277. err = wc_ChaCha20Poly1305_Encrypt(key1, NULL, aad1, sizeof(aad1),
  7278. plaintext1, sizeof(plaintext1), generatedCiphertext,
  7279. generatedAuthTag);
  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. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  7287. sizeof(plaintext1), NULL, generatedAuthTag);
  7288. if (err != BAD_FUNC_ARG)
  7289. return WC_TEST_RET_ENC_EC(err);
  7290. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  7291. sizeof(plaintext1), generatedCiphertext, NULL);
  7292. if (err != BAD_FUNC_ARG)
  7293. return WC_TEST_RET_ENC_EC(err);
  7294. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  7295. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  7296. if (err != BAD_FUNC_ARG)
  7297. return WC_TEST_RET_ENC_EC(err);
  7298. /* Decrypt */
  7299. err = wc_ChaCha20Poly1305_Decrypt(NULL, iv2, aad2, sizeof(aad2), cipher2,
  7300. sizeof(cipher2), authTag2, generatedPlaintext);
  7301. if (err != BAD_FUNC_ARG)
  7302. return WC_TEST_RET_ENC_EC(err);
  7303. err = wc_ChaCha20Poly1305_Decrypt(key2, NULL, aad2, sizeof(aad2), cipher2,
  7304. sizeof(cipher2), authTag2, generatedPlaintext);
  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. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  7312. sizeof(cipher2), NULL, generatedPlaintext);
  7313. if (err != BAD_FUNC_ARG)
  7314. return WC_TEST_RET_ENC_EC(err);
  7315. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  7316. sizeof(cipher2), authTag2, NULL);
  7317. if (err != BAD_FUNC_ARG)
  7318. return WC_TEST_RET_ENC_EC(err);
  7319. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  7320. sizeof(cipher2), authTag2, generatedPlaintext);
  7321. if (err != BAD_FUNC_ARG)
  7322. return WC_TEST_RET_ENC_EC(err);
  7323. /* Test #1 */
  7324. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1,
  7325. aad1, sizeof(aad1),
  7326. plaintext1, sizeof(plaintext1),
  7327. generatedCiphertext, generatedAuthTag);
  7328. if (err) {
  7329. return WC_TEST_RET_ENC_EC(err);
  7330. }
  7331. /* -- Check the ciphertext and authtag */
  7332. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  7333. return WC_TEST_RET_ENC_NC;
  7334. }
  7335. if (XMEMCMP(generatedAuthTag, authTag1, sizeof(authTag1))) {
  7336. return WC_TEST_RET_ENC_NC;
  7337. }
  7338. /* -- Verify decryption works */
  7339. err = wc_ChaCha20Poly1305_Decrypt(key1, iv1,
  7340. aad1, sizeof(aad1),
  7341. cipher1, sizeof(cipher1),
  7342. authTag1, generatedPlaintext);
  7343. if (err) {
  7344. return err;
  7345. }
  7346. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  7347. return WC_TEST_RET_ENC_NC;
  7348. }
  7349. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  7350. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  7351. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  7352. /* Test #2 */
  7353. err = wc_ChaCha20Poly1305_Encrypt(key2, iv2,
  7354. aad2, sizeof(aad2),
  7355. plaintext2, sizeof(plaintext2),
  7356. generatedCiphertext, generatedAuthTag);
  7357. if (err) {
  7358. return err;
  7359. }
  7360. /* -- Check the ciphertext and authtag */
  7361. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  7362. return WC_TEST_RET_ENC_NC;
  7363. }
  7364. if (XMEMCMP(generatedAuthTag, authTag2, sizeof(authTag2))) {
  7365. return WC_TEST_RET_ENC_NC;
  7366. }
  7367. /* -- Verify decryption works */
  7368. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2,
  7369. aad2, sizeof(aad2),
  7370. cipher2, sizeof(cipher2),
  7371. authTag2, generatedPlaintext);
  7372. if (err) {
  7373. return err;
  7374. }
  7375. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  7376. return WC_TEST_RET_ENC_NC;
  7377. }
  7378. /* AEAD init/update/final - bad argument tests */
  7379. err = wc_ChaCha20Poly1305_Init(NULL, key1, iv1,
  7380. CHACHA20_POLY1305_AEAD_DECRYPT);
  7381. if (err != BAD_FUNC_ARG)
  7382. return WC_TEST_RET_ENC_EC(err);
  7383. err = wc_ChaCha20Poly1305_Init(&aead, NULL, iv1,
  7384. CHACHA20_POLY1305_AEAD_DECRYPT);
  7385. if (err != BAD_FUNC_ARG)
  7386. return WC_TEST_RET_ENC_EC(err);
  7387. err = wc_ChaCha20Poly1305_Init(&aead, key1, NULL,
  7388. CHACHA20_POLY1305_AEAD_DECRYPT);
  7389. if (err != BAD_FUNC_ARG)
  7390. return WC_TEST_RET_ENC_EC(err);
  7391. err = wc_ChaCha20Poly1305_UpdateAad(NULL, aad1, sizeof(aad1));
  7392. if (err != BAD_FUNC_ARG)
  7393. return WC_TEST_RET_ENC_EC(err);
  7394. err = wc_ChaCha20Poly1305_UpdateAad(&aead, NULL, sizeof(aad1));
  7395. if (err != BAD_FUNC_ARG)
  7396. return WC_TEST_RET_ENC_EC(err);
  7397. err = wc_ChaCha20Poly1305_UpdateData(NULL, generatedPlaintext,
  7398. generatedPlaintext, sizeof(plaintext1));
  7399. if (err != BAD_FUNC_ARG)
  7400. return WC_TEST_RET_ENC_EC(err);
  7401. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext, NULL,
  7402. sizeof(plaintext1));
  7403. if (err != BAD_FUNC_ARG)
  7404. return WC_TEST_RET_ENC_EC(err);
  7405. err = wc_ChaCha20Poly1305_UpdateData(&aead, NULL, generatedPlaintext,
  7406. sizeof(plaintext1));
  7407. if (err != BAD_FUNC_ARG)
  7408. return WC_TEST_RET_ENC_EC(err);
  7409. err = wc_ChaCha20Poly1305_Final(NULL, generatedAuthTag);
  7410. if (err != BAD_FUNC_ARG)
  7411. return WC_TEST_RET_ENC_EC(err);
  7412. err = wc_ChaCha20Poly1305_Final(&aead, NULL);
  7413. if (err != BAD_FUNC_ARG)
  7414. return WC_TEST_RET_ENC_EC(err);
  7415. /* AEAD init/update/final - bad state tests */
  7416. /* clear struct - make valgrind happy to resolve
  7417. "Conditional jump or move depends on uninitialised value(s)".
  7418. The enum is "int" size and aead.state is "byte" */
  7419. /* The wc_ChaCha20Poly1305_Init function does this normally */
  7420. XMEMSET(&aead, 0, sizeof(aead));
  7421. aead.state = CHACHA20_POLY1305_STATE_INIT;
  7422. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  7423. if (err != BAD_STATE_E)
  7424. return WC_TEST_RET_ENC_EC(err);
  7425. aead.state = CHACHA20_POLY1305_STATE_DATA;
  7426. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  7427. if (err != BAD_STATE_E)
  7428. return WC_TEST_RET_ENC_EC(err);
  7429. aead.state = CHACHA20_POLY1305_STATE_INIT;
  7430. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext,
  7431. generatedPlaintext, sizeof(plaintext1));
  7432. if (err != BAD_STATE_E)
  7433. return WC_TEST_RET_ENC_EC(err);
  7434. aead.state = CHACHA20_POLY1305_STATE_INIT;
  7435. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7436. if (err != BAD_STATE_E)
  7437. return WC_TEST_RET_ENC_EC(err);
  7438. aead.state = CHACHA20_POLY1305_STATE_READY;
  7439. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7440. if (err != BAD_STATE_E)
  7441. return WC_TEST_RET_ENC_EC(err);
  7442. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  7443. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  7444. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  7445. /* Test 1 - Encrypt */
  7446. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  7447. CHACHA20_POLY1305_AEAD_ENCRYPT);
  7448. if (err != 0)
  7449. return WC_TEST_RET_ENC_EC(err);
  7450. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  7451. if (err != 0)
  7452. return WC_TEST_RET_ENC_EC(err);
  7453. #ifdef TEST_SMALL_CHACHA_CHUNKS
  7454. /* test doing data in smaller chunks */
  7455. for (testLen=0; testLen<sizeof(plaintext1); ) {
  7456. word32 dataLen = sizeof(plaintext1) - testLen;
  7457. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  7458. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  7459. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext1[testLen],
  7460. &generatedCiphertext[testLen], dataLen);
  7461. if (err != 0)
  7462. return WC_TEST_RET_ENC_EC(err);
  7463. testLen += dataLen;
  7464. }
  7465. #else
  7466. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext1,
  7467. generatedCiphertext, sizeof(plaintext1));
  7468. #endif
  7469. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7470. if (err != 0)
  7471. return WC_TEST_RET_ENC_EC(err);
  7472. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  7473. if (err != 0)
  7474. return WC_TEST_RET_ENC_EC(err);
  7475. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  7476. return WC_TEST_RET_ENC_NC;
  7477. }
  7478. /* Test 1 - Decrypt */
  7479. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  7480. CHACHA20_POLY1305_AEAD_DECRYPT);
  7481. if (err != 0)
  7482. return WC_TEST_RET_ENC_EC(err);
  7483. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  7484. if (err != 0)
  7485. return WC_TEST_RET_ENC_EC(err);
  7486. #ifdef TEST_SMALL_CHACHA_CHUNKS
  7487. /* test doing data in smaller chunks */
  7488. for (testLen=0; testLen<sizeof(plaintext1); ) {
  7489. word32 dataLen = sizeof(plaintext1) - testLen;
  7490. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  7491. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  7492. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  7493. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  7494. dataLen);
  7495. if (err != 0)
  7496. return WC_TEST_RET_ENC_EC(err);
  7497. testLen += dataLen;
  7498. }
  7499. #else
  7500. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  7501. generatedPlaintext, sizeof(cipher1));
  7502. #endif
  7503. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7504. if (err != 0)
  7505. return WC_TEST_RET_ENC_EC(err);
  7506. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  7507. if (err != 0)
  7508. return WC_TEST_RET_ENC_EC(err);
  7509. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  7510. return WC_TEST_RET_ENC_NC;
  7511. }
  7512. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  7513. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  7514. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  7515. /* Test 2 - Encrypt */
  7516. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  7517. CHACHA20_POLY1305_AEAD_ENCRYPT);
  7518. if (err != 0)
  7519. return WC_TEST_RET_ENC_EC(err);
  7520. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  7521. if (err != 0)
  7522. return WC_TEST_RET_ENC_EC(err);
  7523. #ifdef TEST_SMALL_CHACHA_CHUNKS
  7524. /* test doing data in smaller chunks */
  7525. for (testLen=0; testLen<sizeof(plaintext2); ) {
  7526. word32 dataLen = sizeof(plaintext2) - testLen;
  7527. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  7528. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  7529. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext2[testLen],
  7530. &generatedCiphertext[testLen], dataLen);
  7531. if (err != 0)
  7532. return WC_TEST_RET_ENC_EC(err);
  7533. testLen += dataLen;
  7534. }
  7535. #else
  7536. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext2, generatedCiphertext,
  7537. sizeof(plaintext2));
  7538. #endif
  7539. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7540. if (err != 0)
  7541. return WC_TEST_RET_ENC_EC(err);
  7542. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  7543. if (err != 0)
  7544. return WC_TEST_RET_ENC_EC(err);
  7545. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  7546. return WC_TEST_RET_ENC_NC;
  7547. }
  7548. /* Test 2 - Decrypt */
  7549. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  7550. CHACHA20_POLY1305_AEAD_DECRYPT);
  7551. if (err != 0)
  7552. return WC_TEST_RET_ENC_EC(err);
  7553. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  7554. if (err != 0)
  7555. return WC_TEST_RET_ENC_EC(err);
  7556. #ifdef TEST_SMALL_CHACHA_CHUNKS
  7557. /* test doing data in smaller chunks */
  7558. for (testLen=0; testLen<sizeof(plaintext2); ) {
  7559. word32 dataLen = sizeof(plaintext2) - testLen;
  7560. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  7561. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  7562. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  7563. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  7564. dataLen);
  7565. if (err != 0)
  7566. return WC_TEST_RET_ENC_EC(err);
  7567. testLen += dataLen;
  7568. }
  7569. #else
  7570. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  7571. generatedPlaintext, sizeof(cipher2));
  7572. #endif
  7573. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7574. if (err != 0)
  7575. return WC_TEST_RET_ENC_EC(err);
  7576. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  7577. if (err != 0)
  7578. return WC_TEST_RET_ENC_EC(err);
  7579. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  7580. return WC_TEST_RET_ENC_NC;
  7581. }
  7582. return err;
  7583. }
  7584. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  7585. #ifndef NO_DES3
  7586. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des_test(void)
  7587. {
  7588. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  7589. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7590. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7591. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7592. };
  7593. byte plain[24];
  7594. byte cipher[24];
  7595. Des enc;
  7596. Des dec;
  7597. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  7598. {
  7599. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  7600. };
  7601. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  7602. {
  7603. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  7604. };
  7605. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  7606. {
  7607. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  7608. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  7609. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  7610. };
  7611. wc_test_ret_t ret;
  7612. WOLFSSL_ENTER("des_test");
  7613. ret = wc_Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  7614. if (ret != 0)
  7615. return WC_TEST_RET_ENC_EC(ret);
  7616. ret = wc_Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  7617. if (ret != 0)
  7618. return WC_TEST_RET_ENC_EC(ret);
  7619. ret = wc_Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  7620. if (ret != 0)
  7621. return WC_TEST_RET_ENC_EC(ret);
  7622. ret = wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  7623. if (ret != 0)
  7624. return WC_TEST_RET_ENC_EC(ret);
  7625. if (XMEMCMP(plain, vector, sizeof(plain)))
  7626. return WC_TEST_RET_ENC_NC;
  7627. if (XMEMCMP(cipher, verify, sizeof(cipher)))
  7628. return WC_TEST_RET_ENC_NC;
  7629. ret = wc_Des_CbcEncryptWithKey(cipher, vector, sizeof(vector), key, iv);
  7630. if (ret != 0)
  7631. return WC_TEST_RET_ENC_EC(ret);
  7632. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  7633. {
  7634. EncryptedInfo info;
  7635. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  7636. XMEMCPY(info.iv, iv, sizeof(iv));
  7637. info.ivSz = sizeof(iv);
  7638. info.keySz = sizeof(key);
  7639. info.cipherType = WC_CIPHER_DES;
  7640. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  7641. sizeof(key), WC_HASH_TYPE_SHA);
  7642. if (ret != 0)
  7643. return WC_TEST_RET_ENC_EC(ret);
  7644. /* Test invalid info ptr */
  7645. ret = wc_BufferKeyEncrypt(NULL, cipher, sizeof(cipher), key,
  7646. sizeof(key), WC_HASH_TYPE_SHA);
  7647. if (ret != BAD_FUNC_ARG)
  7648. return WC_TEST_RET_ENC_EC(ret);
  7649. #ifndef NO_PWDBASED
  7650. /* Test invalid hash type - only applies to wc_PBKDF1 call */
  7651. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  7652. sizeof(key), WC_HASH_TYPE_NONE);
  7653. if (ret == 0)
  7654. return WC_TEST_RET_ENC_EC(ret);
  7655. #endif /* !NO_PWDBASED */
  7656. }
  7657. #endif
  7658. return 0;
  7659. }
  7660. #endif /* !NO_DES3 */
  7661. #ifndef NO_DES3
  7662. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des3_test(void)
  7663. {
  7664. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  7665. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7666. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7667. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7668. };
  7669. byte plain[24];
  7670. byte cipher[24];
  7671. Des3 enc;
  7672. Des3 dec;
  7673. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  7674. {
  7675. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  7676. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  7677. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  7678. };
  7679. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  7680. {
  7681. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  7682. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  7683. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  7684. };
  7685. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] =
  7686. {
  7687. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  7688. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  7689. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  7690. };
  7691. wc_test_ret_t ret;
  7692. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7693. size_t i;
  7694. #endif
  7695. WOLFSSL_ENTER("des3_test");
  7696. ret = wc_Des3Init(&enc, HEAP_HINT, devId);
  7697. if (ret != 0)
  7698. return WC_TEST_RET_ENC_EC(ret);
  7699. ret = wc_Des3Init(&dec, HEAP_HINT, devId);
  7700. if (ret != 0)
  7701. return WC_TEST_RET_ENC_EC(ret);
  7702. ret = wc_Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  7703. if (ret != 0)
  7704. return WC_TEST_RET_ENC_EC(ret);
  7705. ret = wc_Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  7706. if (ret != 0)
  7707. return WC_TEST_RET_ENC_EC(ret);
  7708. ret = wc_Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  7709. #if defined(WOLFSSL_ASYNC_CRYPT)
  7710. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  7711. #endif
  7712. if (ret != 0)
  7713. return WC_TEST_RET_ENC_EC(ret);
  7714. ret = wc_Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  7715. #if defined(WOLFSSL_ASYNC_CRYPT)
  7716. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  7717. #endif
  7718. if (ret != 0)
  7719. return WC_TEST_RET_ENC_EC(ret);
  7720. if (XMEMCMP(plain, vector, sizeof(plain)))
  7721. return WC_TEST_RET_ENC_NC;
  7722. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  7723. return WC_TEST_RET_ENC_NC;
  7724. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7725. /* test the same vectors with using compatibility layer */
  7726. for (i = 0; i < sizeof(vector); i += DES_BLOCK_SIZE){
  7727. DES_key_schedule ks1;
  7728. DES_key_schedule ks2;
  7729. DES_key_schedule ks3;
  7730. DES_cblock iv4;
  7731. byte tmp[sizeof(vector)];
  7732. XMEMCPY(ks1, key3, sizeof(DES_key_schedule));
  7733. XMEMCPY(ks2, key3 + 8, sizeof(DES_key_schedule));
  7734. XMEMCPY(ks3, key3 + 16, sizeof(DES_key_schedule));
  7735. XMEMSET(plain, 0, sizeof(plain));
  7736. XMEMSET(cipher, 0, sizeof(cipher));
  7737. /* Test in-place encrypt/decrypt */
  7738. XMEMCPY(tmp, vector, sizeof(vector));
  7739. /* Use i as the splitter */
  7740. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  7741. DES_ede3_cbc_encrypt(tmp, tmp, (long)i, &ks1, &ks2, &ks3,
  7742. &iv4, DES_ENCRYPT);
  7743. DES_ede3_cbc_encrypt(tmp + i, tmp + i, (long)(sizeof(vector) - i),
  7744. &ks1, &ks2, &ks3, &iv4, DES_ENCRYPT);
  7745. XMEMCPY(cipher, tmp, sizeof(cipher));
  7746. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  7747. DES_ede3_cbc_encrypt(tmp, tmp, (long)i, &ks1, &ks2, &ks3,
  7748. &iv4, DES_DECRYPT);
  7749. DES_ede3_cbc_encrypt(tmp + i, tmp + i, (long)(sizeof(cipher) - i),
  7750. &ks1, &ks2, &ks3, &iv4, DES_DECRYPT);
  7751. XMEMCPY(plain, tmp, sizeof(plain));
  7752. if (XMEMCMP(plain, vector, sizeof(plain)))
  7753. return WC_TEST_RET_ENC_NC;
  7754. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  7755. return WC_TEST_RET_ENC_NC;
  7756. }
  7757. #endif /* OPENSSL_EXTRA */
  7758. wc_Des3Free(&enc);
  7759. wc_Des3Free(&dec);
  7760. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  7761. {
  7762. EncryptedInfo info;
  7763. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  7764. XMEMCPY(info.iv, iv3, sizeof(iv3));
  7765. info.ivSz = sizeof(iv3);
  7766. info.keySz = sizeof(key3);
  7767. info.cipherType = WC_CIPHER_DES3;
  7768. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key3,
  7769. sizeof(key3), WC_HASH_TYPE_SHA);
  7770. if (ret != 0)
  7771. return WC_TEST_RET_ENC_EC(ret);
  7772. }
  7773. #endif
  7774. return 0;
  7775. }
  7776. #endif /* NO_DES3 */
  7777. static const int fiducial1 = WC_TEST_RET_LN; /* source code reference point --
  7778. * see print_fiducials() below.
  7779. */
  7780. #ifndef NO_AES
  7781. #if defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_CFB) || \
  7782. defined(WOLFSSL_AES_XTS)
  7783. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7784. && !defined(HAVE_SELFTEST)
  7785. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  7786. /* pass in the function, key, iv, plain text and expected and this function
  7787. * tests that the encryption and decryption is successful */
  7788. static wc_test_ret_t EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key,
  7789. const byte* iv, const byte* plain, int plainSz,
  7790. const byte* expected, int expectedSz)
  7791. {
  7792. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7793. EVP_CIPHER_CTX *ctx = NULL;
  7794. #else
  7795. EVP_CIPHER_CTX ctx[1];
  7796. #endif
  7797. int ctx_inited = 0;
  7798. int idx, cipherSz;
  7799. wc_test_ret_t ret = 0;
  7800. byte* cipher;
  7801. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7802. if ((ctx = wolfSSL_EVP_CIPHER_CTX_new()) == NULL)
  7803. return MEMORY_E;
  7804. #endif
  7805. cipher = (byte*)XMALLOC(plainSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7806. if (cipher == NULL) {
  7807. ret = WC_TEST_RET_ENC_ERRNO;
  7808. goto EVP_TEST_END;
  7809. }
  7810. /* test encrypt */
  7811. EVP_CIPHER_CTX_init(ctx);
  7812. ctx_inited = 1;
  7813. if (EVP_CipherInit(ctx, type, key, iv, 1) == 0) {
  7814. ret = WC_TEST_RET_ENC_NC;
  7815. goto EVP_TEST_END;
  7816. }
  7817. if (EVP_CipherUpdate(ctx, cipher, &idx, plain, expectedSz) == 0) {
  7818. ret = WC_TEST_RET_ENC_NC;
  7819. goto EVP_TEST_END;
  7820. }
  7821. cipherSz = idx;
  7822. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  7823. ret = WC_TEST_RET_ENC_NC;
  7824. goto EVP_TEST_END;
  7825. }
  7826. cipherSz += idx;
  7827. if (XMEMCMP(cipher, expected, plainSz)) {
  7828. ret = WC_TEST_RET_ENC_NC;
  7829. goto EVP_TEST_END;
  7830. }
  7831. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(ctx);
  7832. ctx_inited = 0;
  7833. if (ret == WOLFSSL_SUCCESS)
  7834. ret = 0;
  7835. else {
  7836. ret = WC_TEST_RET_ENC_NC;
  7837. goto EVP_TEST_END;
  7838. }
  7839. /* test decrypt */
  7840. EVP_CIPHER_CTX_init(ctx);
  7841. ctx_inited = 1;
  7842. if (EVP_CipherInit(ctx, type, key, iv, 0) == 0) {
  7843. ret = WC_TEST_RET_ENC_NC;
  7844. goto EVP_TEST_END;
  7845. }
  7846. if (EVP_CipherUpdate(ctx, cipher, &idx, cipher, expectedSz) == 0) {
  7847. ret = WC_TEST_RET_ENC_NC;
  7848. goto EVP_TEST_END;
  7849. }
  7850. cipherSz = idx;
  7851. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  7852. ret = WC_TEST_RET_ENC_NC;
  7853. goto EVP_TEST_END;
  7854. }
  7855. cipherSz += idx;
  7856. if ((expectedSz != cipherSz) || XMEMCMP(plain, cipher, plainSz)) {
  7857. ret = WC_TEST_RET_ENC_NC;
  7858. goto EVP_TEST_END;
  7859. }
  7860. EVP_TEST_END:
  7861. if (cipher)
  7862. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7863. (void)cipherSz;
  7864. if (ctx_inited) {
  7865. int cleanup_ret = wolfSSL_EVP_CIPHER_CTX_cleanup(ctx);
  7866. if (cleanup_ret != WOLFSSL_SUCCESS)
  7867. ret = WC_TEST_RET_ENC_NC;
  7868. }
  7869. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7870. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  7871. #endif
  7872. return ret;
  7873. }
  7874. #endif /* !HAVE_FIPS || FIPS_VERSION3_GE(6,0,0) */
  7875. #endif /* OPENSSL_EXTRA && !WOLFCRYPT_ONLY && !HAVE_SELFTEST */
  7876. #endif /* WOLFSSL_AES_OFB || WOLFSSL_AES_CFB */
  7877. #ifdef WOLFSSL_AES_OFB
  7878. /* test vector from https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/Block-Ciphers */
  7879. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesofb_test(void)
  7880. {
  7881. #ifdef WOLFSSL_AES_256
  7882. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  7883. {
  7884. 0xc4,0xc7,0xfa,0xd6,0x53,0x5c,0xb8,0x71,
  7885. 0x4a,0x5c,0x40,0x77,0x9a,0x8b,0xa1,0xd2,
  7886. 0x53,0x3e,0x23,0xb4,0xb2,0x58,0x73,0x2a,
  7887. 0x5b,0x78,0x01,0xf4,0xe3,0x71,0xa7,0x94
  7888. };
  7889. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  7890. {
  7891. 0x5e,0xb9,0x33,0x13,0xb8,0x71,0xff,0x16,
  7892. 0xb9,0x8a,0x9b,0xcb,0x43,0x33,0x0d,0x6f
  7893. };
  7894. WOLFSSL_SMALL_STACK_STATIC const byte plain1[] =
  7895. {
  7896. 0x6d,0x0b,0xb0,0x79,0x63,0x84,0x71,0xe9,
  7897. 0x39,0xd4,0x53,0x14,0x86,0xc1,0x4c,0x25,
  7898. 0x9a,0xee,0xc6,0xf3,0xc0,0x0d,0xfd,0xd6,
  7899. 0xc0,0x50,0xa8,0xba,0xa8,0x20,0xdb,0x71,
  7900. 0xcc,0x12,0x2c,0x4e,0x0c,0x17,0x15,0xef,
  7901. 0x55,0xf3,0x99,0x5a,0x6b,0xf0,0x2a,0x4c
  7902. };
  7903. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  7904. {
  7905. 0x0f,0x54,0x61,0x71,0x59,0xd0,0x3f,0xfc,
  7906. 0x1b,0xfa,0xfb,0x60,0x29,0x30,0xd7,0x00,
  7907. 0xf4,0xa4,0xa8,0xe6,0xdd,0x93,0x94,0x46,
  7908. 0x64,0xd2,0x19,0xc4,0xc5,0x4d,0xde,0x1b,
  7909. 0x04,0x53,0xe1,0x73,0xf5,0x18,0x74,0xae,
  7910. 0xfd,0x64,0xa2,0xe1,0xe2,0x76,0x13,0xb0
  7911. };
  7912. #endif /* WOLFSSL_AES_256 */
  7913. #ifdef WOLFSSL_AES_128
  7914. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  7915. {
  7916. 0x10,0xa5,0x88,0x69,0xd7,0x4b,0xe5,0xa3,
  7917. 0x74,0xcf,0x86,0x7c,0xfb,0x47,0x38,0x59
  7918. };
  7919. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  7920. {
  7921. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7922. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7923. };
  7924. WOLFSSL_SMALL_STACK_STATIC const byte plain2[] =
  7925. {
  7926. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7927. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7928. };
  7929. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  7930. {
  7931. 0x6d,0x25,0x1e,0x69,0x44,0xb0,0x51,0xe0,
  7932. 0x4e,0xaa,0x6f,0xb4,0xdb,0xf7,0x84,0x65
  7933. };
  7934. #endif /* WOLFSSL_AES_128 */
  7935. #ifdef WOLFSSL_AES_192
  7936. WOLFSSL_SMALL_STACK_STATIC const byte key3[] = {
  7937. 0xd0,0x77,0xa0,0x3b,0xd8,0xa3,0x89,0x73,
  7938. 0x92,0x8c,0xca,0xfe,0x4a,0x9d,0x2f,0x45,
  7939. 0x51,0x30,0xbd,0x0a,0xf5,0xae,0x46,0xa9
  7940. };
  7941. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  7942. {
  7943. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7944. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7945. };
  7946. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  7947. {
  7948. 0xab,0xc7,0x86,0xfb,0x1e,0xdb,0x50,0x45,
  7949. 0x80,0xc4,0xd8,0x82,0xef,0x29,0xa0,0xc7
  7950. };
  7951. WOLFSSL_SMALL_STACK_STATIC const byte plain3[] =
  7952. {
  7953. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7954. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7955. };
  7956. #endif /* WOLFSSL_AES_192 */
  7957. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7958. Aes *enc = NULL;
  7959. #else
  7960. Aes enc[1];
  7961. #endif
  7962. byte cipher[AES_BLOCK_SIZE * 4];
  7963. #ifdef HAVE_AES_DECRYPT
  7964. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7965. Aes *dec = NULL;
  7966. #else
  7967. Aes dec[1];
  7968. #endif
  7969. byte plain [AES_BLOCK_SIZE * 4];
  7970. #endif
  7971. wc_test_ret_t ret = 0;
  7972. WOLFSSL_ENTER("aesofb_test");
  7973. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7974. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7975. ERROR_OUT(-1, out);
  7976. #ifdef HAVE_AES_DECRYPT
  7977. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7978. ERROR_OUT(-1, out);
  7979. #endif
  7980. #endif
  7981. XMEMSET(enc, 0, sizeof *enc);
  7982. #ifdef HAVE_AES_DECRYPT
  7983. XMEMSET(dec, 0, sizeof *dec);
  7984. #endif
  7985. #ifdef WOLFSSL_AES_128
  7986. /* 128 key size test */
  7987. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7988. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7989. ret = EVP_test(EVP_aes_128_ofb(), key2, iv2, plain2, sizeof(plain2),
  7990. cipher2, sizeof(cipher2));
  7991. if (ret != 0) {
  7992. goto out;
  7993. }
  7994. #endif
  7995. ret = wc_AesInit(enc, HEAP_HINT, INVALID_DEVID);
  7996. if (ret != 0)
  7997. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7998. #ifdef HAVE_AES_DECRYPT
  7999. ret = wc_AesInit(dec, HEAP_HINT, INVALID_DEVID);
  8000. if (ret != 0)
  8001. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8002. #endif
  8003. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8004. if (ret != 0)
  8005. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8006. #ifdef HAVE_AES_DECRYPT
  8007. /* decrypt uses AES_ENCRYPTION */
  8008. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8009. if (ret != 0)
  8010. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8011. #endif
  8012. XMEMSET(cipher, 0, sizeof(cipher));
  8013. ret = wc_AesOfbEncrypt(enc, cipher, plain2, AES_BLOCK_SIZE);
  8014. if (ret != 0)
  8015. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8016. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE))
  8017. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8018. #ifdef HAVE_AES_DECRYPT
  8019. ret = wc_AesOfbDecrypt(dec, plain, cipher2, AES_BLOCK_SIZE);
  8020. if (ret != 0)
  8021. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8022. if (XMEMCMP(plain, plain2, AES_BLOCK_SIZE))
  8023. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8024. #endif /* HAVE_AES_DECRYPT */
  8025. #endif /* WOLFSSL_AES_128 */
  8026. #ifdef WOLFSSL_AES_192
  8027. /* 192 key size test */
  8028. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8029. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8030. ret = EVP_test(EVP_aes_192_ofb(), key3, iv3, plain3, sizeof(plain3),
  8031. cipher3, sizeof(cipher3));
  8032. if (ret != 0) {
  8033. goto out;
  8034. }
  8035. #endif
  8036. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  8037. if (ret != 0)
  8038. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8039. #ifdef HAVE_AES_DECRYPT
  8040. /* decrypt uses AES_ENCRYPTION */
  8041. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  8042. if (ret != 0)
  8043. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8044. #endif
  8045. XMEMSET(cipher, 0, sizeof(cipher));
  8046. ret = wc_AesOfbEncrypt(enc, cipher, plain3, AES_BLOCK_SIZE);
  8047. if (ret != 0)
  8048. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8049. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE))
  8050. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8051. #ifdef HAVE_AES_DECRYPT
  8052. ret = wc_AesOfbDecrypt(dec, plain, cipher3, AES_BLOCK_SIZE);
  8053. if (ret != 0)
  8054. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8055. if (XMEMCMP(plain, plain3, AES_BLOCK_SIZE))
  8056. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8057. #endif /* HAVE_AES_DECRYPT */
  8058. #endif /* WOLFSSL_AES_192 */
  8059. #ifdef WOLFSSL_AES_256
  8060. /* 256 key size test */
  8061. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8062. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8063. ret = EVP_test(EVP_aes_256_ofb(), key1, iv1, plain1, sizeof(plain1),
  8064. cipher1, sizeof(cipher1));
  8065. if (ret != 0) {
  8066. goto out;
  8067. }
  8068. #endif
  8069. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8070. if (ret != 0)
  8071. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8072. #ifdef HAVE_AES_DECRYPT
  8073. /* decrypt uses AES_ENCRYPTION */
  8074. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8075. if (ret != 0)
  8076. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8077. #endif
  8078. XMEMSET(cipher, 0, sizeof(cipher));
  8079. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE);
  8080. if (ret != 0)
  8081. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8082. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE))
  8083. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8084. ret = wc_AesOfbEncrypt(enc, cipher + AES_BLOCK_SIZE,
  8085. plain1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  8086. if (ret != 0)
  8087. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8088. if (XMEMCMP(cipher + AES_BLOCK_SIZE, cipher1 + AES_BLOCK_SIZE,
  8089. AES_BLOCK_SIZE))
  8090. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8091. #ifdef HAVE_AES_DECRYPT
  8092. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE);
  8093. if (ret != 0)
  8094. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8095. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE))
  8096. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8097. ret = wc_AesOfbDecrypt(dec, plain + AES_BLOCK_SIZE,
  8098. cipher1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  8099. if (ret != 0)
  8100. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8101. if (XMEMCMP(plain + AES_BLOCK_SIZE, plain1 + AES_BLOCK_SIZE,
  8102. AES_BLOCK_SIZE))
  8103. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8104. #endif /* HAVE_AES_DECRYPT */
  8105. /* multiple blocks at once */
  8106. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8107. if (ret != 0)
  8108. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8109. #ifdef HAVE_AES_DECRYPT
  8110. /* decrypt uses AES_ENCRYPTION */
  8111. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8112. if (ret != 0)
  8113. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8114. #endif
  8115. XMEMSET(cipher, 0, sizeof(cipher));
  8116. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE * 3);
  8117. if (ret != 0)
  8118. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8119. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 3))
  8120. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8121. #ifdef HAVE_AES_DECRYPT
  8122. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE * 3);
  8123. if (ret != 0)
  8124. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8125. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE * 3))
  8126. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8127. #endif /* HAVE_AES_DECRYPT */
  8128. /* inline decrypt/encrypt*/
  8129. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8130. if (ret != 0)
  8131. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8132. #ifdef HAVE_AES_DECRYPT
  8133. /* decrypt uses AES_ENCRYPTION */
  8134. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8135. if (ret != 0)
  8136. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8137. #endif
  8138. XMEMCPY(cipher, plain1, AES_BLOCK_SIZE * 2);
  8139. ret = wc_AesOfbEncrypt(enc, cipher, cipher, AES_BLOCK_SIZE * 2);
  8140. if (ret != 0)
  8141. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8142. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  8143. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8144. #ifdef HAVE_AES_DECRYPT
  8145. ret = wc_AesOfbDecrypt(dec, cipher, cipher, AES_BLOCK_SIZE * 2);
  8146. if (ret != 0)
  8147. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8148. if (XMEMCMP(cipher, plain1, AES_BLOCK_SIZE * 2))
  8149. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8150. #endif /* HAVE_AES_DECRYPT */
  8151. /* 256 key size test leftover support */
  8152. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8153. if (ret != 0)
  8154. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8155. #ifdef HAVE_AES_DECRYPT
  8156. /* decrypt uses AES_ENCRYPTION */
  8157. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8158. if (ret != 0)
  8159. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8160. #endif
  8161. XMEMSET(cipher, 0, sizeof(cipher));
  8162. ret = wc_AesOfbEncrypt(enc, cipher, plain1, 3);
  8163. if (ret != 0)
  8164. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8165. if (XMEMCMP(cipher, cipher1, 3))
  8166. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8167. ret = wc_AesOfbEncrypt(enc, cipher + 3, plain1 + 3, AES_BLOCK_SIZE);
  8168. if (ret != 0)
  8169. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8170. if (XMEMCMP(cipher + 3, cipher1 + 3, AES_BLOCK_SIZE))
  8171. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8172. #ifdef HAVE_AES_DECRYPT
  8173. ret = wc_AesOfbDecrypt(dec, plain, cipher1, 6);
  8174. if (ret != 0)
  8175. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8176. if (XMEMCMP(plain, plain1, 6))
  8177. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8178. ret = wc_AesOfbDecrypt(dec, plain + 6, cipher1 + 6, AES_BLOCK_SIZE);
  8179. if (ret != 0)
  8180. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8181. if (XMEMCMP(plain + 6, plain1 + 6, AES_BLOCK_SIZE))
  8182. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8183. #endif /* HAVE_AES_DECRYPT */
  8184. out:
  8185. wc_AesFree(enc);
  8186. #ifdef HAVE_AES_DECRYPT
  8187. wc_AesFree(dec);
  8188. #endif
  8189. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8190. if (enc)
  8191. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8192. #ifdef HAVE_AES_DECRYPT
  8193. if (dec)
  8194. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8195. #endif
  8196. #endif
  8197. #endif /* WOLFSSL_AES_256 */
  8198. return ret;
  8199. }
  8200. #endif /* WOLFSSL_AES_OFB */
  8201. #if defined(WOLFSSL_AES_CFB)
  8202. /* Test cases from NIST SP 800-38A, Recommendation for Block Cipher Modes of
  8203. * Operation Methods and Techniques
  8204. */
  8205. static wc_test_ret_t aescfb_test_0(void)
  8206. {
  8207. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8208. Aes *enc = NULL;
  8209. #else
  8210. Aes enc[1];
  8211. #endif
  8212. int enc_inited = 0;
  8213. byte cipher[AES_BLOCK_SIZE * 4];
  8214. #ifdef HAVE_AES_DECRYPT
  8215. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8216. Aes *dec = NULL;
  8217. #else
  8218. Aes dec[1];
  8219. #endif
  8220. int dec_inited = 0;
  8221. byte plain [AES_BLOCK_SIZE * 4];
  8222. #endif
  8223. wc_test_ret_t ret = 0;
  8224. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  8225. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  8226. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  8227. };
  8228. #ifdef WOLFSSL_AES_128
  8229. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  8230. {
  8231. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  8232. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  8233. };
  8234. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  8235. {
  8236. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  8237. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  8238. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  8239. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b,
  8240. 0x26,0x75,0x1f,0x67,0xa3,0xcb,0xb1,0x40,
  8241. 0xb1,0x80,0x8c,0xf1,0x87,0xa4,0xf4,0xdf
  8242. };
  8243. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  8244. {
  8245. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8246. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  8247. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  8248. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  8249. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  8250. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef
  8251. };
  8252. #endif /* WOLFSSL_AES_128 */
  8253. #ifdef WOLFSSL_AES_192
  8254. /* 192 size key test */
  8255. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  8256. {
  8257. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  8258. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  8259. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  8260. };
  8261. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  8262. {
  8263. 0xcd,0xc8,0x0d,0x6f,0xdd,0xf1,0x8c,0xab,
  8264. 0x34,0xc2,0x59,0x09,0xc9,0x9a,0x41,0x74,
  8265. 0x67,0xce,0x7f,0x7f,0x81,0x17,0x36,0x21,
  8266. 0x96,0x1a,0x2b,0x70,0x17,0x1d,0x3d,0x7a,
  8267. 0x2e,0x1e,0x8a,0x1d,0xd5,0x9b,0x88,0xb1,
  8268. 0xc8,0xe6,0x0f,0xed,0x1e,0xfa,0xc4,0xc9,
  8269. 0xc0,0x5f,0x9f,0x9c,0xa9,0x83,0x4f,0xa0,
  8270. 0x42,0xae,0x8f,0xba,0x58,0x4b,0x09,0xff
  8271. };
  8272. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  8273. {
  8274. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8275. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  8276. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  8277. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  8278. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  8279. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  8280. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  8281. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  8282. };
  8283. #endif /* WOLFSSL_AES_192 */
  8284. #ifdef WOLFSSL_AES_256
  8285. /* 256 size key simple test */
  8286. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  8287. {
  8288. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8289. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8290. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8291. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8292. };
  8293. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  8294. {
  8295. 0xdc,0x7e,0x84,0xbf,0xda,0x79,0x16,0x4b,
  8296. 0x7e,0xcd,0x84,0x86,0x98,0x5d,0x38,0x60,
  8297. 0x39,0xff,0xed,0x14,0x3b,0x28,0xb1,0xc8,
  8298. 0x32,0x11,0x3c,0x63,0x31,0xe5,0x40,0x7b,
  8299. 0xdf,0x10,0x13,0x24,0x15,0xe5,0x4b,0x92,
  8300. 0xa1,0x3e,0xd0,0xa8,0x26,0x7a,0xe2,0xf9,
  8301. 0x75,0xa3,0x85,0x74,0x1a,0xb9,0xce,0xf8,
  8302. 0x20,0x31,0x62,0x3d,0x55,0xb1,0xe4,0x71
  8303. };
  8304. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  8305. {
  8306. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8307. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  8308. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  8309. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  8310. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  8311. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  8312. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  8313. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  8314. };
  8315. #endif /* WOLFSSL_AES_256 */
  8316. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8317. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8318. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8319. #ifdef HAVE_AES_DECRYPT
  8320. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8321. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8322. #endif
  8323. #endif
  8324. ret = wc_AesInit(enc, HEAP_HINT, devId);
  8325. if (ret != 0)
  8326. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8327. else
  8328. enc_inited = 1;
  8329. #ifdef HAVE_AES_DECRYPT
  8330. ret = wc_AesInit(dec, HEAP_HINT, devId);
  8331. if (ret != 0)
  8332. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8333. else
  8334. dec_inited = 1;
  8335. #endif
  8336. #ifdef WOLFSSL_AES_128
  8337. /* 128 key tests */
  8338. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8339. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8340. ret = EVP_test(EVP_aes_128_cfb128(), key1, iv, msg1, sizeof(msg1),
  8341. cipher1, sizeof(cipher1));
  8342. if (ret != 0) {
  8343. return ret;
  8344. }
  8345. #endif
  8346. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8347. if (ret != 0)
  8348. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8349. #ifdef HAVE_AES_DECRYPT
  8350. /* decrypt uses AES_ENCRYPTION */
  8351. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8352. if (ret != 0)
  8353. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8354. #endif
  8355. XMEMSET(cipher, 0, sizeof(cipher));
  8356. ret = wc_AesCfbEncrypt(enc, cipher, msg1, AES_BLOCK_SIZE * 2);
  8357. if (ret != 0)
  8358. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8359. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  8360. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8361. /* test restarting encryption process */
  8362. ret = wc_AesCfbEncrypt(enc, cipher + (AES_BLOCK_SIZE * 2),
  8363. msg1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE);
  8364. if (ret != 0)
  8365. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8366. if (XMEMCMP(cipher + (AES_BLOCK_SIZE * 2),
  8367. cipher1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE))
  8368. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8369. #ifdef HAVE_AES_DECRYPT
  8370. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 3);
  8371. if (ret != 0)
  8372. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8373. if (XMEMCMP(plain, msg1, AES_BLOCK_SIZE * 3))
  8374. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8375. #endif /* HAVE_AES_DECRYPT */
  8376. #endif /* WOLFSSL_AES_128 */
  8377. #ifdef WOLFSSL_AES_192
  8378. /* 192 key size test */
  8379. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8380. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8381. ret = EVP_test(EVP_aes_192_cfb128(), key2, iv, msg2, sizeof(msg2),
  8382. cipher2, sizeof(cipher2));
  8383. if (ret != 0) {
  8384. return ret;
  8385. }
  8386. #endif
  8387. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv, AES_ENCRYPTION);
  8388. if (ret != 0)
  8389. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8390. #ifdef HAVE_AES_DECRYPT
  8391. /* decrypt uses AES_ENCRYPTION */
  8392. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv, AES_ENCRYPTION);
  8393. if (ret != 0)
  8394. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8395. #endif
  8396. XMEMSET(cipher, 0, sizeof(cipher));
  8397. ret = wc_AesCfbEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE * 4);
  8398. if (ret != 0)
  8399. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8400. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE * 4))
  8401. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8402. #ifdef HAVE_AES_DECRYPT
  8403. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 4);
  8404. if (ret != 0)
  8405. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8406. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE * 4))
  8407. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8408. #endif /* HAVE_AES_DECRYPT */
  8409. #endif /* WOLFSSL_AES_192 */
  8410. #ifdef WOLFSSL_AES_256
  8411. /* 256 key size test */
  8412. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8413. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8414. ret = EVP_test(EVP_aes_256_cfb128(), key3, iv, msg3, sizeof(msg3),
  8415. cipher3, sizeof(cipher3));
  8416. if (ret != 0) {
  8417. return ret;
  8418. }
  8419. #endif
  8420. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv, AES_ENCRYPTION);
  8421. if (ret != 0)
  8422. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8423. #ifdef HAVE_AES_DECRYPT
  8424. /* decrypt uses AES_ENCRYPTION */
  8425. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv, AES_ENCRYPTION);
  8426. if (ret != 0)
  8427. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8428. #endif
  8429. /* test with data left overs, magic lengths are checking near edges */
  8430. XMEMSET(cipher, 0, sizeof(cipher));
  8431. ret = wc_AesCfbEncrypt(enc, cipher, msg3, 4);
  8432. if (ret != 0)
  8433. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8434. if (XMEMCMP(cipher, cipher3, 4))
  8435. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8436. ret = wc_AesCfbEncrypt(enc, cipher + 4, msg3 + 4, 27);
  8437. if (ret != 0)
  8438. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8439. if (XMEMCMP(cipher + 4, cipher3 + 4, 27))
  8440. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8441. ret = wc_AesCfbEncrypt(enc, cipher + 31, msg3 + 31,
  8442. (AES_BLOCK_SIZE * 4) - 31);
  8443. if (ret != 0)
  8444. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8445. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE * 4))
  8446. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8447. #ifdef HAVE_AES_DECRYPT
  8448. ret = wc_AesCfbDecrypt(dec, plain, cipher, 4);
  8449. if (ret != 0)
  8450. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8451. if (XMEMCMP(plain, msg3, 4))
  8452. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8453. ret = wc_AesCfbDecrypt(dec, plain + 4, cipher + 4, 4);
  8454. if (ret != 0)
  8455. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8456. ret = wc_AesCfbDecrypt(dec, plain + 8, cipher + 8, 23);
  8457. if (ret != 0)
  8458. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8459. if (XMEMCMP(plain + 4, msg3 + 4, 27))
  8460. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8461. ret = wc_AesCfbDecrypt(dec, plain + 31, cipher + 31,
  8462. (AES_BLOCK_SIZE * 4) - 31);
  8463. if (ret != 0)
  8464. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8465. if (XMEMCMP(plain, msg3, AES_BLOCK_SIZE * 4))
  8466. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8467. #endif /* HAVE_AES_DECRYPT */
  8468. #endif /* WOLFSSL_AES_256 */
  8469. out:
  8470. if (enc_inited)
  8471. wc_AesFree(enc);
  8472. #ifdef HAVE_AES_DECRYPT
  8473. if (dec_inited)
  8474. wc_AesFree(dec);
  8475. #endif
  8476. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8477. if (enc)
  8478. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8479. #ifdef HAVE_AES_DECRYPT
  8480. if (dec)
  8481. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8482. #endif
  8483. #endif
  8484. return ret;
  8485. }
  8486. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0))
  8487. static wc_test_ret_t aescfb1_test(void)
  8488. {
  8489. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8490. Aes *enc = NULL;
  8491. #else
  8492. Aes enc[1];
  8493. #endif
  8494. int enc_inited = 0;
  8495. byte cipher[AES_BLOCK_SIZE];
  8496. #ifdef HAVE_AES_DECRYPT
  8497. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8498. Aes *dec = NULL;
  8499. #else
  8500. Aes dec[1];
  8501. #endif
  8502. int dec_inited = 0;
  8503. byte plain [AES_BLOCK_SIZE];
  8504. #endif
  8505. wc_test_ret_t ret = 0;
  8506. #ifdef WOLFSSL_AES_128
  8507. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  8508. 0x4d,0xbb,0xdc,0xaa,0x59,0xf3,0x63,0xc9,
  8509. 0x2a,0x3b,0x98,0x43,0xad,0x20,0xe2,0xb7
  8510. };
  8511. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  8512. {
  8513. 0xcd,0xef,0x9d,0x06,0x61,0xba,0xe4,0x73,
  8514. 0x8d,0x1a,0x58,0xa2,0xa6,0x22,0x8b,0x66
  8515. };
  8516. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  8517. {
  8518. 0x00
  8519. };
  8520. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  8521. {
  8522. 0xC0
  8523. };
  8524. WOLFSSL_SMALL_STACK_STATIC const byte cipher1_7bit[] =
  8525. {
  8526. 0x1C
  8527. };
  8528. #endif /* WOLFSSL_AES_128 */
  8529. #ifdef WOLFSSL_AES_192
  8530. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  8531. 0x57,0xc6,0x89,0x7c,0x99,0x52,0x28,0x13,
  8532. 0xbf,0x67,0x9c,0xe1,0x13,0x70,0xaf,0x5e
  8533. };
  8534. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  8535. {
  8536. 0xba,0xa1,0x58,0xa1,0x6b,0x50,0x4a,0x10,
  8537. 0x8e,0xd4,0x33,0x2e,0xe7,0xf2,0x9b,0xf6,
  8538. 0xd1,0xac,0x46,0xa8,0xde,0x5a,0xfe,0x7a
  8539. };
  8540. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  8541. {
  8542. 0x30
  8543. };
  8544. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  8545. {
  8546. 0x80
  8547. };
  8548. #endif /* WOLFSSL_AES_192 */
  8549. #ifdef WOLFSSL_AES_256
  8550. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  8551. 0x63,0x2e,0x9f,0x83,0x1f,0xa3,0x80,0x5e,
  8552. 0x52,0x02,0xbc,0xe0,0x6d,0x04,0xf9,0xa0
  8553. };
  8554. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  8555. {
  8556. 0xf6,0xfa,0xe4,0xf1,0x5d,0x91,0xfc,0x50,
  8557. 0x88,0x78,0x4f,0x84,0xa5,0x37,0x12,0x7e,
  8558. 0x32,0x63,0x55,0x9c,0x62,0x73,0x88,0x20,
  8559. 0xc2,0xcf,0x3d,0xe1,0x1c,0x2a,0x30,0x40
  8560. };
  8561. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  8562. {
  8563. 0xF7, 0x00
  8564. };
  8565. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  8566. {
  8567. 0x41, 0xC0
  8568. };
  8569. #endif /* WOLFSSL_AES_256 */
  8570. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8571. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8572. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8573. #ifdef HAVE_AES_DECRYPT
  8574. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8575. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8576. #endif
  8577. #endif
  8578. ret = wc_AesInit(enc, HEAP_HINT, devId);
  8579. if (ret != 0)
  8580. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8581. else
  8582. enc_inited = 1;
  8583. #ifdef HAVE_AES_DECRYPT
  8584. ret = wc_AesInit(dec, HEAP_HINT, devId);
  8585. if (ret != 0)
  8586. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8587. else
  8588. dec_inited = 1;
  8589. #endif
  8590. #ifdef WOLFSSL_AES_128
  8591. /* 128 key tests */
  8592. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8593. if (ret != 0)
  8594. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8595. #ifdef HAVE_AES_DECRYPT
  8596. /* decrypt uses AES_ENCRYPTION */
  8597. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8598. if (ret != 0)
  8599. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8600. #endif
  8601. XMEMSET(cipher, 0, sizeof(cipher));
  8602. ret = wc_AesCfb1Encrypt(enc, cipher, msg1, 2);
  8603. if (ret != 0)
  8604. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8605. if (cipher[0] != cipher1[0])
  8606. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8607. #ifdef HAVE_AES_DECRYPT
  8608. ret = wc_AesCfb1Decrypt(dec, plain, cipher, 2);
  8609. if (ret != 0)
  8610. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8611. if (plain[0] != msg1[0])
  8612. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8613. #endif /* HAVE_AES_DECRYPT */
  8614. XMEMSET(cipher, 0, sizeof(cipher));
  8615. ret = wc_AesCfb1Encrypt(enc, cipher, msg1, 7);
  8616. if (ret != 0)
  8617. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8618. if (cipher[0] != cipher1_7bit[0])
  8619. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8620. #ifdef OPENSSL_EXTRA
  8621. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8622. if (ret != 0)
  8623. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8624. XMEMSET(cipher, 0, sizeof(cipher));
  8625. ret = wc_AesCfb1Encrypt(enc, cipher, msg1,
  8626. sizeof(msg1) * WOLFSSL_BIT_SIZE);
  8627. if (ret != 0)
  8628. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8629. #ifndef WOLFCRYPT_ONLY
  8630. ret = EVP_test(EVP_aes_128_cfb1(), key1, iv, msg1, sizeof(msg1),
  8631. cipher, sizeof(msg1));
  8632. if (ret != 0) {
  8633. goto out;
  8634. }
  8635. #endif
  8636. #endif
  8637. #endif /* WOLFSSL_AES_128 */
  8638. #ifdef WOLFSSL_AES_192
  8639. /* 192 key tests */
  8640. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8641. if (ret != 0)
  8642. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8643. XMEMSET(cipher, 0, sizeof(cipher));
  8644. ret = wc_AesCfb1Encrypt(enc, cipher, msg2, 4);
  8645. if (ret != 0)
  8646. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8647. if (XMEMCMP(cipher, cipher2, sizeof(cipher2)) != 0)
  8648. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8649. #ifdef OPENSSL_EXTRA
  8650. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8651. if (ret != 0)
  8652. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8653. XMEMSET(cipher, 0, sizeof(cipher));
  8654. ret = wc_AesCfb1Encrypt(enc, cipher, msg2,
  8655. sizeof(msg2) * WOLFSSL_BIT_SIZE);
  8656. if (ret != 0)
  8657. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8658. #ifndef WOLFCRYPT_ONLY
  8659. ret = EVP_test(EVP_aes_192_cfb1(), key2, iv2, msg2, sizeof(msg2),
  8660. cipher, sizeof(msg2));
  8661. if (ret != 0) {
  8662. goto out;
  8663. }
  8664. #endif
  8665. #endif
  8666. #endif /* WOLFSSL_AES_192 */
  8667. #ifdef WOLFSSL_AES_256
  8668. /* 256 key tests */
  8669. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  8670. if (ret != 0)
  8671. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8672. XMEMSET(cipher, 0, sizeof(cipher));
  8673. ret = wc_AesCfb1Encrypt(enc, cipher, msg3, 10);
  8674. if (ret != 0)
  8675. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8676. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  8677. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8678. #ifdef OPENSSL_EXTRA
  8679. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  8680. if (ret != 0)
  8681. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8682. XMEMSET(cipher, 0, sizeof(cipher));
  8683. ret = wc_AesCfb1Encrypt(enc, cipher, msg3,
  8684. sizeof(msg3) * WOLFSSL_BIT_SIZE);
  8685. if (ret != 0)
  8686. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8687. #ifndef WOLFCRYPT_ONLY
  8688. ret = EVP_test(EVP_aes_256_cfb1(), key3, iv3, msg3, sizeof(msg3),
  8689. cipher, sizeof(msg3));
  8690. if (ret != 0) {
  8691. goto out;
  8692. }
  8693. #endif
  8694. #endif
  8695. #endif /* WOLFSSL_AES_256 */
  8696. out:
  8697. if (enc_inited)
  8698. wc_AesFree(enc);
  8699. #ifdef HAVE_AES_DECRYPT
  8700. if (dec_inited)
  8701. wc_AesFree(dec);
  8702. #endif
  8703. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8704. if (enc)
  8705. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8706. #ifdef HAVE_AES_DECRYPT
  8707. if (dec)
  8708. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8709. #endif
  8710. #endif
  8711. return ret;
  8712. }
  8713. static wc_test_ret_t aescfb8_test(void)
  8714. {
  8715. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8716. Aes *enc = NULL;
  8717. #else
  8718. Aes enc[1];
  8719. #endif
  8720. int enc_inited = 0;
  8721. byte cipher[AES_BLOCK_SIZE];
  8722. #ifdef HAVE_AES_DECRYPT
  8723. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8724. Aes *dec = NULL;
  8725. #else
  8726. Aes dec[1];
  8727. #endif
  8728. int dec_inited = 0;
  8729. byte plain [AES_BLOCK_SIZE];
  8730. #endif
  8731. wc_test_ret_t ret = 0;
  8732. #ifdef WOLFSSL_AES_128
  8733. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  8734. 0xf4,0x75,0xc6,0x49,0x91,0xb2,0x0e,0xae,
  8735. 0xe1,0x83,0xa2,0x26,0x29,0xe2,0x1e,0x22
  8736. };
  8737. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  8738. {
  8739. 0xc8,0xfe,0x9b,0xf7,0x7b,0x93,0x0f,0x46,
  8740. 0xd2,0x07,0x8b,0x8c,0x0e,0x65,0x7c,0xd4
  8741. };
  8742. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  8743. {
  8744. 0xd2,0x76,0x91
  8745. };
  8746. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  8747. {
  8748. 0xc9,0x06,0x35
  8749. };
  8750. #endif /* WOLFSSL_AES_128 */
  8751. #ifdef WOLFSSL_AES_192
  8752. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  8753. 0x0a,0x02,0x84,0x6b,0x62,0xab,0xb6,0x93,
  8754. 0xef,0x31,0xd7,0x54,0x84,0x2e,0xed,0x29
  8755. };
  8756. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  8757. {
  8758. 0xba,0xf0,0x8b,0x76,0x31,0x7a,0x65,0xc5,
  8759. 0xf0,0x7a,0xe6,0xf5,0x7e,0xb0,0xe6,0x54,
  8760. 0x88,0x65,0x93,0x24,0xd2,0x97,0x09,0xe3
  8761. };
  8762. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  8763. {
  8764. 0x72,0x9c,0x0b,0x6d,0xeb,0x75,0xfa,0x6e,
  8765. 0xb5,0xe8
  8766. };
  8767. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  8768. {
  8769. 0x98,0x95,0x93,0x24,0x02,0x39,0x3d,0xc3,
  8770. 0x3a,0x60
  8771. };
  8772. #endif
  8773. #ifdef WOLFSSL_AES_256
  8774. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  8775. 0x33,0x8c,0x55,0x2f,0xf1,0xec,0xa1,0x44,
  8776. 0x08,0xe0,0x5d,0x8c,0xf9,0xf3,0xb3,0x1b
  8777. };
  8778. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  8779. {
  8780. 0x06,0x48,0x74,0x09,0x2f,0x7a,0x13,0xcc,
  8781. 0x44,0x62,0x24,0x7a,0xd4,0x23,0xd0,0xe9,
  8782. 0x6e,0xdf,0x42,0xe8,0xb6,0x7a,0x5a,0x23,
  8783. 0xb7,0xa0,0xa6,0x47,0x7b,0x09,0x8e,0x66
  8784. };
  8785. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  8786. {
  8787. 0x1c,0xff,0x95
  8788. };
  8789. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  8790. {
  8791. 0xb9,0x74,0xfa
  8792. };
  8793. #endif
  8794. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8795. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8796. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8797. #ifdef HAVE_AES_DECRYPT
  8798. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8799. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8800. #endif
  8801. #endif
  8802. ret = wc_AesInit(enc, HEAP_HINT, devId);
  8803. if (ret != 0)
  8804. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8805. else
  8806. enc_inited = 1;
  8807. #ifdef HAVE_AES_DECRYPT
  8808. ret = wc_AesInit(dec, HEAP_HINT, devId);
  8809. if (ret != 0)
  8810. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8811. else
  8812. dec_inited = 1;
  8813. #endif
  8814. #ifdef WOLFSSL_AES_128
  8815. /* 128 key tests */
  8816. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  8817. ret = EVP_test(EVP_aes_128_cfb8(), key1, iv, msg1, sizeof(msg1),
  8818. cipher1, sizeof(cipher1));
  8819. if (ret != 0) {
  8820. return ret;
  8821. }
  8822. #endif
  8823. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8824. if (ret != 0)
  8825. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8826. #ifdef HAVE_AES_DECRYPT
  8827. /* decrypt uses AES_ENCRYPTION */
  8828. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8829. if (ret != 0)
  8830. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8831. #endif
  8832. XMEMSET(cipher, 0, sizeof(cipher));
  8833. ret = wc_AesCfb8Encrypt(enc, cipher, msg1, sizeof(msg1));
  8834. if (ret != 0)
  8835. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8836. if (XMEMCMP(cipher, cipher1, sizeof(cipher1)) != 0)
  8837. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8838. #ifdef HAVE_AES_DECRYPT
  8839. ret = wc_AesCfb8Decrypt(dec, plain, cipher, sizeof(msg1));
  8840. if (ret != 0)
  8841. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8842. if (XMEMCMP(plain, msg1, sizeof(msg1)) != 0)
  8843. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8844. #endif /* HAVE_AES_DECRYPT */
  8845. #endif /* WOLFSSL_AES_128 */
  8846. #ifdef WOLFSSL_AES_192
  8847. /* 192 key tests */
  8848. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8849. if (ret != 0)
  8850. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8851. XMEMSET(cipher, 0, sizeof(cipher));
  8852. ret = wc_AesCfb8Encrypt(enc, cipher, msg2, sizeof(msg2));
  8853. if (ret != 0)
  8854. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8855. if (XMEMCMP(cipher, cipher2, sizeof(msg2)) != 0)
  8856. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8857. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  8858. ret = EVP_test(EVP_aes_192_cfb8(), key2, iv2, msg2, sizeof(msg2),
  8859. cipher2, sizeof(msg2));
  8860. if (ret != 0) {
  8861. return ret;
  8862. }
  8863. #endif
  8864. #endif /* WOLFSSL_AES_192 */
  8865. #ifdef WOLFSSL_AES_256
  8866. /* 256 key tests */
  8867. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  8868. if (ret != 0)
  8869. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8870. XMEMSET(cipher, 0, sizeof(cipher));
  8871. ret = wc_AesCfb8Encrypt(enc, cipher, msg3, sizeof(msg3));
  8872. if (ret != 0)
  8873. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8874. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  8875. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8876. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  8877. ret = EVP_test(EVP_aes_256_cfb8(), key3, iv3, msg3, sizeof(msg3),
  8878. cipher3, sizeof(msg3));
  8879. if (ret != 0) {
  8880. goto out;
  8881. }
  8882. #endif
  8883. #endif /* WOLFSSL_AES_256 */
  8884. out:
  8885. if (enc_inited)
  8886. wc_AesFree(enc);
  8887. #ifdef HAVE_AES_DECRYPT
  8888. if (dec_inited)
  8889. wc_AesFree(dec);
  8890. #endif
  8891. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8892. if (enc)
  8893. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8894. #ifdef HAVE_AES_DECRYPT
  8895. if (dec)
  8896. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8897. #endif
  8898. #endif
  8899. return ret;
  8900. }
  8901. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  8902. #endif /* WOLFSSL_AES_CFB */
  8903. static wc_test_ret_t aes_key_size_test(void)
  8904. {
  8905. wc_test_ret_t ret;
  8906. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8907. Aes *aes;
  8908. #else
  8909. Aes aes[1];
  8910. #endif
  8911. byte key16[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8912. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  8913. #ifndef WOLFSSL_CRYPTOCELL
  8914. byte key24[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8915. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  8916. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
  8917. #endif
  8918. byte key32[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8919. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  8920. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8921. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  8922. byte iv[] = "1234567890abcdef";
  8923. #ifndef HAVE_FIPS
  8924. word32 keySize;
  8925. #endif
  8926. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8927. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8928. return WC_TEST_RET_ENC_ERRNO;
  8929. #endif
  8930. #if !defined(HAVE_FIPS) || \
  8931. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  8932. /* w/ FIPS v1 (cert 2425) wc_AesInit just returns 0 always as it's not
  8933. * supported with that FIPS version */
  8934. ret = wc_AesInit(NULL, HEAP_HINT, devId);
  8935. if (ret != BAD_FUNC_ARG)
  8936. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8937. #endif
  8938. ret = wc_AesInit(aes, HEAP_HINT, devId);
  8939. /* 0 check OK for FIPSv1 */
  8940. if (ret != 0)
  8941. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8942. #ifndef HAVE_FIPS
  8943. /* Parameter Validation testing. */
  8944. ret = wc_AesGetKeySize(NULL, NULL);
  8945. if (ret != BAD_FUNC_ARG)
  8946. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8947. ret = wc_AesGetKeySize(aes, NULL);
  8948. if (ret != BAD_FUNC_ARG)
  8949. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8950. ret = wc_AesGetKeySize(NULL, &keySize);
  8951. if (ret != BAD_FUNC_ARG)
  8952. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8953. /* Crashes in FIPS */
  8954. ret = wc_AesSetKey(NULL, key16, sizeof(key16), iv, AES_ENCRYPTION);
  8955. if (ret != BAD_FUNC_ARG)
  8956. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8957. #endif
  8958. /* NULL IV indicates to use all zeros IV. */
  8959. ret = wc_AesSetKey(aes, key16, sizeof(key16), NULL, AES_ENCRYPTION);
  8960. #ifdef WOLFSSL_AES_128
  8961. if (ret != 0)
  8962. #else
  8963. if (ret != BAD_FUNC_ARG)
  8964. #endif
  8965. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8966. ret = wc_AesSetKey(aes, key32, sizeof(key32) - 1, iv, AES_ENCRYPTION);
  8967. if (ret != BAD_FUNC_ARG)
  8968. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8969. /* CryptoCell handles rounds internally */
  8970. #if !defined(HAVE_FIPS) && !defined(WOLFSSL_CRYPTOCELL)
  8971. /* PSA don't use aes->rounds */
  8972. #if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_AES)
  8973. /* Force invalid rounds */
  8974. aes->rounds = 16;
  8975. ret = wc_AesGetKeySize(aes, &keySize);
  8976. if (ret != BAD_FUNC_ARG)
  8977. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8978. #endif
  8979. #endif
  8980. ret = wc_AesSetKey(aes, key16, sizeof(key16), iv, AES_ENCRYPTION);
  8981. #ifdef WOLFSSL_AES_128
  8982. if (ret != 0)
  8983. #else
  8984. if (ret != BAD_FUNC_ARG)
  8985. #endif
  8986. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8987. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_128)
  8988. ret = wc_AesGetKeySize(aes, &keySize);
  8989. if (ret != 0 || keySize != sizeof(key16))
  8990. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8991. #endif
  8992. #ifndef WOLFSSL_CRYPTOCELL
  8993. /* Cryptocell only supports AES-128 key size */
  8994. ret = wc_AesSetKey(aes, key24, sizeof(key24), iv, AES_ENCRYPTION);
  8995. #ifdef WOLFSSL_AES_192
  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_192)
  9002. ret = wc_AesGetKeySize(aes, &keySize);
  9003. if (ret != 0 || keySize != sizeof(key24))
  9004. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9005. #endif
  9006. ret = wc_AesSetKey(aes, key32, sizeof(key32), iv, AES_ENCRYPTION);
  9007. #ifdef WOLFSSL_AES_256
  9008. if (ret != 0)
  9009. #else
  9010. if (ret != BAD_FUNC_ARG)
  9011. #endif
  9012. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9013. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_256)
  9014. ret = wc_AesGetKeySize(aes, &keySize);
  9015. if (ret != 0 || keySize != sizeof(key32))
  9016. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9017. #endif
  9018. #endif /* !WOLFSSL_CRYPTOCELL */
  9019. ret = 0; /* success */
  9020. out:
  9021. wc_AesFree(aes);
  9022. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9023. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  9024. #endif
  9025. return ret;
  9026. }
  9027. #if defined(WOLFSSL_AES_XTS) && (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3))
  9028. /* test vectors from http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html */
  9029. #ifdef WOLFSSL_AES_128
  9030. static wc_test_ret_t aes_xts_128_test(void)
  9031. {
  9032. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9033. XtsAes *aes = NULL;
  9034. #else
  9035. XtsAes aes[1];
  9036. #endif
  9037. int aes_inited = 0;
  9038. wc_test_ret_t ret = 0;
  9039. unsigned char buf[AES_BLOCK_SIZE * 2 + 8];
  9040. unsigned char cipher[AES_BLOCK_SIZE * 2 + 8];
  9041. #ifdef WOLFSSL_AESXTS_STREAM
  9042. struct XtsAesStreamData stream;
  9043. #endif
  9044. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  9045. !defined(WOLFSSL_AFALG)
  9046. #define LARGE_XTS_SZ 1024
  9047. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9048. byte* large_input = NULL;
  9049. #else
  9050. byte large_input[LARGE_XTS_SZ];
  9051. #endif
  9052. #endif
  9053. /* 128 key tests */
  9054. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  9055. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  9056. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  9057. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  9058. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  9059. };
  9060. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  9061. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  9062. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  9063. };
  9064. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  9065. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  9066. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  9067. };
  9068. /* plain text test of partial block is not from NIST test vector list */
  9069. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  9070. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  9071. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  9072. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  9073. };
  9074. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  9075. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a,
  9076. 0x82, 0x50, 0x81, 0xd5, 0xbe, 0x47, 0x1c, 0x63
  9077. };
  9078. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  9079. 0x39, 0x25, 0x79, 0x05, 0xdf, 0xcc, 0x77, 0x76,
  9080. 0x6c, 0x87, 0x0a, 0x80, 0x6a, 0x60, 0xe3, 0xc0,
  9081. 0x93, 0xd1, 0x2a, 0xcf, 0xcb, 0x51, 0x42, 0xfa,
  9082. 0x09, 0x69, 0x89, 0x62, 0x5b, 0x60, 0xdb, 0x16
  9083. };
  9084. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  9085. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  9086. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  9087. };
  9088. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  9089. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  9090. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  9091. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  9092. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  9093. };
  9094. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  9095. 0xd6, 0xbe, 0x04, 0x6d, 0x41, 0xf2, 0x3b, 0x5e,
  9096. 0xd7, 0x0b, 0x6b, 0x3d, 0x5c, 0x8e, 0x66, 0x23,
  9097. 0x2b, 0xe6, 0xb8, 0x07, 0xd4, 0xdc, 0xc6, 0x0e,
  9098. 0xff, 0x8d, 0xbc, 0x1d, 0x9f, 0x7f, 0xc8, 0x22
  9099. };
  9100. WOLFSSL_SMALL_STACK_STATIC unsigned char cp2[] = {
  9101. 0x2b, 0xf7, 0x2c, 0xf3, 0xeb, 0x85, 0xef, 0x7b,
  9102. 0x0b, 0x76, 0xa0, 0xaa, 0xf3, 0x3f, 0x25, 0x8b,
  9103. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a
  9104. };
  9105. #ifndef HAVE_FIPS /* FIPS requires different keys for main and tweak. */
  9106. WOLFSSL_SMALL_STACK_STATIC unsigned char k3[] = {
  9107. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9108. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9109. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9110. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9111. };
  9112. WOLFSSL_SMALL_STACK_STATIC unsigned char i3[] = {
  9113. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9114. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9115. };
  9116. WOLFSSL_SMALL_STACK_STATIC unsigned char p3[] = {
  9117. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9118. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9119. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9120. 0x20, 0xff, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9121. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
  9122. };
  9123. WOLFSSL_SMALL_STACK_STATIC unsigned char c3[] = {
  9124. 0xA2, 0x07, 0x47, 0x76, 0x3F, 0xEC, 0x0C, 0x23,
  9125. 0x1B, 0xD0, 0xBD, 0x46, 0x9A, 0x27, 0x38, 0x12,
  9126. 0x95, 0x02, 0x3D, 0x5D, 0xC6, 0x94, 0x51, 0x36,
  9127. 0xA0, 0x85, 0xD2, 0x69, 0x6E, 0x87, 0x0A, 0xBF,
  9128. 0xB5, 0x5A, 0xDD, 0xCB, 0x80, 0xE0, 0xFC, 0xCD
  9129. };
  9130. #endif /* HAVE_FIPS */
  9131. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9132. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9133. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9134. #endif
  9135. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  9136. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  9137. ret = EVP_test(EVP_aes_128_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  9138. if (ret != 0) {
  9139. printf("EVP_aes_128_xts failed!\n");
  9140. goto out;
  9141. }
  9142. #endif
  9143. XMEMSET(buf, 0, sizeof(buf));
  9144. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  9145. if (ret != 0)
  9146. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9147. else
  9148. aes_inited = 1;
  9149. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_ENCRYPTION);
  9150. if (ret != 0)
  9151. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9152. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  9153. #if defined(WOLFSSL_ASYNC_CRYPT)
  9154. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9155. #endif
  9156. if (ret != 0)
  9157. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9158. if (XMEMCMP(c2, buf, sizeof(c2)))
  9159. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9160. #ifdef WOLFSSL_AESXTS_STREAM
  9161. ret = wc_AesXtsEncryptInit(aes, i2, sizeof(i2), &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. ret = wc_AesXtsEncryptUpdate(aes, buf, p2, AES_BLOCK_SIZE, &stream);
  9168. #if defined(WOLFSSL_ASYNC_CRYPT)
  9169. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9170. #endif
  9171. if (ret != 0)
  9172. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9173. ret = wc_AesXtsEncryptFinal(aes, buf + AES_BLOCK_SIZE, p2 + AES_BLOCK_SIZE, sizeof(p2) - AES_BLOCK_SIZE, &stream);
  9174. #if defined(WOLFSSL_ASYNC_CRYPT)
  9175. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9176. #endif
  9177. if (ret != 0)
  9178. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9179. if (XMEMCMP(c2, buf, sizeof(c2)))
  9180. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9181. #endif /* WOLFSSL_AESXTS_STREAM */
  9182. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9183. defined(WC_C_DYNAMIC_FALLBACK)
  9184. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9185. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  9186. #if defined(WOLFSSL_ASYNC_CRYPT)
  9187. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9188. #endif
  9189. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9190. if (ret != 0)
  9191. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9192. if (XMEMCMP(c2, buf, sizeof(c2)))
  9193. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9194. #endif
  9195. XMEMSET(buf, 0, sizeof(buf));
  9196. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  9197. if (ret != 0)
  9198. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9199. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  9200. #if defined(WOLFSSL_ASYNC_CRYPT)
  9201. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9202. #endif
  9203. if (ret != 0)
  9204. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9205. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  9206. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9207. #ifdef WOLFSSL_AESXTS_STREAM
  9208. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i2), &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. ret = wc_AesXtsEncryptUpdate(aes, buf, p1, sizeof(p1), &stream);
  9215. #if defined(WOLFSSL_ASYNC_CRYPT)
  9216. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9217. #endif
  9218. if (ret != 0)
  9219. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9220. ret = wc_AesXtsEncryptFinal(aes, buf, NULL, 0, &stream);
  9221. #if defined(WOLFSSL_ASYNC_CRYPT)
  9222. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9223. #endif
  9224. if (ret != 0)
  9225. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9226. if (XMEMCMP(c1, buf, sizeof(c1)))
  9227. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9228. #endif /* WOLFSSL_AESXTS_STREAM */
  9229. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9230. defined(WC_C_DYNAMIC_FALLBACK)
  9231. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9232. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  9233. #if defined(WOLFSSL_ASYNC_CRYPT)
  9234. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9235. #endif
  9236. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9237. if (ret != 0)
  9238. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9239. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  9240. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9241. #endif
  9242. /* partial block encryption test */
  9243. XMEMSET(cipher, 0, sizeof(cipher));
  9244. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  9245. #if defined(WOLFSSL_ASYNC_CRYPT)
  9246. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9247. #endif
  9248. if (ret != 0)
  9249. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9250. if (XMEMCMP(cp2, cipher, sizeof(cp2)))
  9251. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9252. #ifdef WOLFSSL_AESXTS_STREAM
  9253. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &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. ret = wc_AesXtsEncryptFinal(aes, buf, pp, sizeof(pp), &stream);
  9260. #if defined(WOLFSSL_ASYNC_CRYPT)
  9261. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9262. #endif
  9263. if (ret != 0)
  9264. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9265. ret = wc_AesXtsEncryptFinal(aes, buf, NULL, 0, &stream);
  9266. #if defined(WOLFSSL_ASYNC_CRYPT)
  9267. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9268. #endif
  9269. if (ret != 0)
  9270. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9271. if (XMEMCMP(cp2, buf, sizeof(cp2)))
  9272. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9273. #endif /* WOLFSSL_AESXTS_STREAM */
  9274. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9275. defined(WC_C_DYNAMIC_FALLBACK)
  9276. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9277. XMEMSET(cipher, 0, sizeof(cipher));
  9278. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  9279. #if defined(WOLFSSL_ASYNC_CRYPT)
  9280. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9281. #endif
  9282. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9283. if (ret != 0)
  9284. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9285. if (XMEMCMP(cp2, cipher, sizeof(cp2)))
  9286. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9287. #endif
  9288. /* partial block decrypt test */
  9289. XMEMSET(buf, 0, sizeof(buf));
  9290. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  9291. if (ret != 0)
  9292. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9293. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  9294. #if defined(WOLFSSL_ASYNC_CRYPT)
  9295. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9296. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9297. #else
  9298. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9299. #endif
  9300. #endif
  9301. if (ret != 0)
  9302. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9303. if (XMEMCMP(pp, buf, sizeof(pp)))
  9304. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9305. #ifdef WOLFSSL_AESXTS_STREAM
  9306. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  9307. #if defined(WOLFSSL_ASYNC_CRYPT)
  9308. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9309. #endif
  9310. if (ret != 0)
  9311. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9312. ret = wc_AesXtsDecryptFinal(aes, buf, cipher, sizeof(pp), &stream);
  9313. #if defined(WOLFSSL_ASYNC_CRYPT)
  9314. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9315. #endif
  9316. if (ret != 0)
  9317. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9318. if (XMEMCMP(pp, buf, sizeof(pp)))
  9319. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9320. #endif /* WOLFSSL_AESXTS_STREAM */
  9321. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9322. defined(WC_C_DYNAMIC_FALLBACK)
  9323. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9324. XMEMSET(buf, 0, sizeof(buf));
  9325. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  9326. #if defined(WOLFSSL_ASYNC_CRYPT)
  9327. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9328. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9329. #else
  9330. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9331. #endif
  9332. #endif
  9333. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9334. if (ret != 0)
  9335. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9336. if (XMEMCMP(pp, buf, sizeof(pp)))
  9337. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9338. #endif
  9339. /* NIST decrypt test vector */
  9340. XMEMSET(buf, 0, sizeof(buf));
  9341. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  9342. #if defined(WOLFSSL_ASYNC_CRYPT)
  9343. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9344. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9345. #else
  9346. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9347. #endif
  9348. #endif
  9349. if (ret != 0)
  9350. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9351. if (XMEMCMP(p1, buf, sizeof(p1)))
  9352. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9353. #ifdef WOLFSSL_AESXTS_STREAM
  9354. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  9355. #if defined(WOLFSSL_ASYNC_CRYPT)
  9356. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9357. #endif
  9358. if (ret != 0)
  9359. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9360. ret = wc_AesXtsDecryptFinal(aes, buf, c1, sizeof(c1), &stream);
  9361. #if defined(WOLFSSL_ASYNC_CRYPT)
  9362. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9363. #endif
  9364. if (ret != 0)
  9365. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9366. if (XMEMCMP(p1, buf, sizeof(p1)))
  9367. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9368. #endif /* WOLFSSL_AESXTS_STREAM */
  9369. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9370. defined(WC_C_DYNAMIC_FALLBACK)
  9371. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9372. XMEMSET(buf, 0, sizeof(buf));
  9373. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  9374. #if defined(WOLFSSL_ASYNC_CRYPT)
  9375. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9376. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9377. #else
  9378. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9379. #endif
  9380. #endif
  9381. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9382. if (ret != 0)
  9383. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9384. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  9385. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9386. #endif
  9387. /* fail case with decrypting using wrong key */
  9388. XMEMSET(buf, 0, sizeof(buf));
  9389. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  9390. #if defined(WOLFSSL_ASYNC_CRYPT)
  9391. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9392. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9393. #else
  9394. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9395. #endif
  9396. #endif
  9397. if (ret != 0)
  9398. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9399. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  9400. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9401. /* set correct key and retest */
  9402. XMEMSET(buf, 0, sizeof(buf));
  9403. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_DECRYPTION);
  9404. if (ret != 0)
  9405. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9406. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  9407. #if defined(WOLFSSL_ASYNC_CRYPT)
  9408. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9409. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9410. #else
  9411. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9412. #endif
  9413. #endif
  9414. if (ret != 0)
  9415. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9416. if (XMEMCMP(p2, buf, sizeof(p2)))
  9417. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9418. #ifndef HAVE_FIPS
  9419. /* Test ciphertext stealing in-place. */
  9420. XMEMCPY(buf, p3, sizeof(p3));
  9421. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_ENCRYPTION);
  9422. if (ret != 0)
  9423. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9424. ret = wc_AesXtsEncrypt(aes, buf, buf, sizeof(p3), i3, sizeof(i3));
  9425. #if defined(WOLFSSL_ASYNC_CRYPT)
  9426. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9427. #endif
  9428. if (ret != 0)
  9429. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9430. if (XMEMCMP(c3, buf, sizeof(c3)))
  9431. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9432. #ifdef WOLFSSL_AESXTS_STREAM
  9433. ret = wc_AesXtsEncryptInit(aes, i3, sizeof(i3), &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. ret = wc_AesXtsEncryptUpdate(aes, buf, p3, AES_BLOCK_SIZE, &stream);
  9440. #if defined(WOLFSSL_ASYNC_CRYPT)
  9441. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9442. #endif
  9443. if (ret != 0)
  9444. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9445. ret = wc_AesXtsEncryptFinal(aes, buf + AES_BLOCK_SIZE, p3 + AES_BLOCK_SIZE, sizeof(p3) - AES_BLOCK_SIZE, &stream);
  9446. #if defined(WOLFSSL_ASYNC_CRYPT)
  9447. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9448. #endif
  9449. if (ret != 0)
  9450. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9451. if (XMEMCMP(c3, buf, sizeof(c3)))
  9452. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9453. #endif /* WOLFSSL_AESXTS_STREAM */
  9454. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_DECRYPTION);
  9455. if (ret != 0)
  9456. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9457. ret = wc_AesXtsDecrypt(aes, buf, buf, sizeof(c3), i3, sizeof(i3));
  9458. #if defined(WOLFSSL_ASYNC_CRYPT)
  9459. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9460. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9461. #else
  9462. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9463. #endif
  9464. #endif
  9465. if (ret != 0)
  9466. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9467. if (XMEMCMP(p3, buf, sizeof(p3)))
  9468. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9469. #ifdef WOLFSSL_AESXTS_STREAM
  9470. ret = wc_AesXtsDecryptInit(aes, i3, sizeof(i3), &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. ret = wc_AesXtsDecryptUpdate(aes, buf, c3, AES_BLOCK_SIZE, &stream);
  9477. #if defined(WOLFSSL_ASYNC_CRYPT)
  9478. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9479. #endif
  9480. if (ret != 0)
  9481. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9482. ret = wc_AesXtsDecryptFinal(aes, buf + AES_BLOCK_SIZE, c3 + AES_BLOCK_SIZE, sizeof(c3) - AES_BLOCK_SIZE, &stream);
  9483. #if defined(WOLFSSL_ASYNC_CRYPT)
  9484. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9485. #endif
  9486. if (ret != 0)
  9487. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9488. if (XMEMCMP(p3, buf, sizeof(p3)))
  9489. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9490. #endif /* WOLFSSL_AESXTS_STREAM */
  9491. #endif /* !HAVE_FIPS */
  9492. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  9493. !defined(WOLFSSL_AFALG)
  9494. {
  9495. int i;
  9496. int j;
  9497. #ifdef WOLFSSL_AESXTS_STREAM
  9498. int k;
  9499. #endif
  9500. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9501. large_input = (byte *)XMALLOC(LARGE_XTS_SZ, HEAP_HINT,
  9502. DYNAMIC_TYPE_TMP_BUFFER);
  9503. if (large_input == NULL)
  9504. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), out);
  9505. #endif
  9506. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  9507. large_input[i] = (byte)i;
  9508. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  9509. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  9510. if (ret != 0)
  9511. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9512. ret = wc_AesXtsEncrypt(aes, large_input, large_input, (word32)j, i1,
  9513. sizeof(i1));
  9514. #if defined(WOLFSSL_ASYNC_CRYPT)
  9515. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9516. #endif
  9517. if (ret != 0)
  9518. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9519. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  9520. if (ret != 0)
  9521. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9522. ret = wc_AesXtsDecrypt(aes, large_input, large_input, (word32)j, i1,
  9523. sizeof(i1));
  9524. #if defined(WOLFSSL_ASYNC_CRYPT)
  9525. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9526. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  9527. WC_ASYNC_FLAG_NONE);
  9528. #else
  9529. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9530. #endif
  9531. #endif
  9532. if (ret != 0)
  9533. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9534. for (i = 0; i < j; i++) {
  9535. if (large_input[i] != (byte)i) {
  9536. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9537. }
  9538. }
  9539. }
  9540. #ifdef WOLFSSL_AESXTS_STREAM
  9541. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  9542. large_input[i] = (byte)i;
  9543. /* first, encrypt block by block then decrypt with a one-shot call. */
  9544. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  9545. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  9546. if (ret != 0)
  9547. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9548. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &stream);
  9549. #if defined(WOLFSSL_ASYNC_CRYPT)
  9550. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9551. #endif
  9552. if (ret != 0)
  9553. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9554. for (k = 0; k < j; k += AES_BLOCK_SIZE) {
  9555. if ((j - k) < AES_BLOCK_SIZE*2)
  9556. ret = wc_AesXtsEncryptFinal(aes, large_input + k, large_input + k, j - k, &stream);
  9557. else
  9558. ret = wc_AesXtsEncryptUpdate(aes, large_input + k, large_input + k, AES_BLOCK_SIZE, &stream);
  9559. #if defined(WOLFSSL_ASYNC_CRYPT)
  9560. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9561. #endif
  9562. if (ret != 0)
  9563. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9564. if ((j - k) < AES_BLOCK_SIZE*2)
  9565. break;
  9566. }
  9567. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  9568. if (ret != 0)
  9569. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9570. ret = wc_AesXtsDecrypt(aes, large_input, large_input, (word32)j, i1,
  9571. sizeof(i1));
  9572. #if defined(WOLFSSL_ASYNC_CRYPT)
  9573. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9574. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  9575. WC_ASYNC_FLAG_NONE);
  9576. #else
  9577. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9578. #endif
  9579. #endif
  9580. if (ret != 0)
  9581. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9582. for (i = 0; i < j; i++) {
  9583. if (large_input[i] != (byte)i) {
  9584. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9585. }
  9586. }
  9587. }
  9588. /* second, encrypt with a one-shot call then decrypt block by block. */
  9589. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  9590. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  9591. if (ret != 0)
  9592. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9593. ret = wc_AesXtsEncrypt(aes, large_input, large_input, (word32)j, i1,
  9594. sizeof(i1));
  9595. #if defined(WOLFSSL_ASYNC_CRYPT)
  9596. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9597. #endif
  9598. if (ret != 0)
  9599. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9600. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  9601. if (ret != 0)
  9602. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9603. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  9604. #if defined(WOLFSSL_ASYNC_CRYPT)
  9605. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9606. #endif
  9607. if (ret != 0)
  9608. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9609. for (k = 0; k < j; k += AES_BLOCK_SIZE) {
  9610. if ((j - k) < AES_BLOCK_SIZE*2)
  9611. ret = wc_AesXtsDecryptFinal(aes, large_input + k, large_input + k, j - k, &stream);
  9612. else
  9613. ret = wc_AesXtsDecryptUpdate(aes, large_input + k, large_input + k, AES_BLOCK_SIZE, &stream);
  9614. #if defined(WOLFSSL_ASYNC_CRYPT)
  9615. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9616. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  9617. WC_ASYNC_FLAG_NONE);
  9618. #else
  9619. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9620. #endif
  9621. #endif
  9622. if (ret != 0)
  9623. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9624. if ((j - k) < AES_BLOCK_SIZE*2)
  9625. break;
  9626. }
  9627. for (i = 0; i < j; i++) {
  9628. if (large_input[i] != (byte)i) {
  9629. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9630. }
  9631. }
  9632. }
  9633. #endif /* WOLFSSL_AESXTS_STREAM */
  9634. }
  9635. #endif /* !BENCH_EMBEDDED && !HAVE_CAVIUM &&
  9636. * !WOLFSSL_AFALG
  9637. */
  9638. out:
  9639. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  9640. !defined(WOLFSSL_AFALG) && defined(WOLFSSL_SMALL_STACK) && \
  9641. !defined(WOLFSSL_NO_MALLOC)
  9642. if (large_input)
  9643. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9644. #endif
  9645. if (aes_inited)
  9646. wc_AesXtsFree(aes);
  9647. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9648. if (aes)
  9649. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  9650. #endif
  9651. return ret;
  9652. }
  9653. #endif /* WOLFSSL_AES_128 */
  9654. #ifdef WOLFSSL_AES_192
  9655. static wc_test_ret_t aes_xts_192_test(void)
  9656. {
  9657. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9658. XtsAes *aes = NULL;
  9659. #else
  9660. XtsAes aes[1];
  9661. #endif
  9662. int aes_inited = 0;
  9663. wc_test_ret_t ret = 0;
  9664. unsigned char buf[AES_BLOCK_SIZE * 2 + 8];
  9665. unsigned char cipher[AES_BLOCK_SIZE * 2 + 8];
  9666. #ifdef WOLFSSL_AESXTS_STREAM
  9667. struct XtsAesStreamData stream;
  9668. #endif
  9669. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  9670. !defined(WOLFSSL_AFALG)
  9671. #define LARGE_XTS_SZ 1024
  9672. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9673. byte* large_input = NULL;
  9674. #else
  9675. byte large_input[LARGE_XTS_SZ];
  9676. #endif
  9677. #endif
  9678. /* 192 bit key tests */
  9679. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  9680. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  9681. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  9682. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  9683. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  9684. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  9685. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3
  9686. };
  9687. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  9688. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  9689. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  9690. };
  9691. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  9692. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  9693. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  9694. };
  9695. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  9696. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  9697. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  9698. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  9699. };
  9700. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  9701. 0x65, 0x37, 0x15, 0x53, 0xf1, 0x98, 0xab, 0xb4,
  9702. 0xdb, 0x4e, 0xd3, 0x69, 0xdf, 0x8e, 0x3a, 0xe0
  9703. };
  9704. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  9705. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  9706. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  9707. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  9708. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  9709. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  9710. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a
  9711. };
  9712. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  9713. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  9714. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  9715. };
  9716. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  9717. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  9718. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  9719. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  9720. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  9721. };
  9722. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  9723. 0x6c, 0xa6, 0xb5, 0x73, 0x48, 0xf1, 0x89, 0xfa,
  9724. 0xdd, 0x80, 0x72, 0x1f, 0xb8, 0x56, 0x0c, 0xa2,
  9725. 0x35, 0xd4, 0x08, 0xbf, 0x24, 0xcb, 0xec, 0xdb,
  9726. 0x81, 0xe0, 0xe6, 0x4f, 0x3d, 0x1c, 0x5c, 0x46
  9727. };
  9728. WOLFSSL_SMALL_STACK_STATIC unsigned char cp2[] = {
  9729. 0xe9, 0x58, 0xfe, 0xab, 0x66, 0xb4, 0xf1, 0x79,
  9730. 0x91, 0x3f, 0x91, 0xdc, 0x6f, 0xdf, 0xd6, 0xac,
  9731. 0x65, 0x37, 0x15, 0x53, 0xf1, 0x98, 0xab, 0xb4
  9732. };
  9733. #ifndef HAVE_FIPS /* FIPS requires different keys for main and tweak. */
  9734. WOLFSSL_SMALL_STACK_STATIC unsigned char k3[] = {
  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, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9739. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9740. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
  9741. };
  9742. WOLFSSL_SMALL_STACK_STATIC unsigned char i3[] = {
  9743. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9744. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9745. };
  9746. WOLFSSL_SMALL_STACK_STATIC unsigned char p3[] = {
  9747. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9748. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9749. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9750. 0x20, 0xff, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9751. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
  9752. };
  9753. WOLFSSL_SMALL_STACK_STATIC unsigned char c3[] = {
  9754. 0xa4, 0xf2, 0x71, 0x5d, 0x80, 0x60, 0x68, 0xa0,
  9755. 0x80, 0x61, 0xd7, 0xc1, 0x55, 0xc8, 0x3a, 0x2e,
  9756. 0xd7, 0xf4, 0x62, 0xaf, 0xbd, 0x2d, 0xf9, 0x5f,
  9757. 0xe8, 0xc5, 0x99, 0x3d, 0x58, 0x3c, 0xeb, 0xba,
  9758. 0x86, 0xea, 0x2c, 0x7e, 0x1f, 0xba, 0x81, 0xde
  9759. };
  9760. #endif /* HAVE_FIPS */
  9761. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9762. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9763. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9764. #endif
  9765. XMEMSET(buf, 0, sizeof(buf));
  9766. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  9767. if (ret != 0)
  9768. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9769. else
  9770. aes_inited = 1;
  9771. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_ENCRYPTION);
  9772. if (ret != 0)
  9773. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9774. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  9775. #if defined(WOLFSSL_ASYNC_CRYPT)
  9776. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9777. #endif
  9778. if (ret != 0)
  9779. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9780. if (XMEMCMP(c2, buf, sizeof(c2)))
  9781. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9782. #ifdef WOLFSSL_AESXTS_STREAM
  9783. ret = wc_AesXtsEncryptInit(aes, i2, sizeof(i2), &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. ret = wc_AesXtsEncryptUpdate(aes, buf, p2, AES_BLOCK_SIZE, &stream);
  9790. #if defined(WOLFSSL_ASYNC_CRYPT)
  9791. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9792. #endif
  9793. if (ret != 0)
  9794. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9795. ret = wc_AesXtsEncryptFinal(aes, buf + AES_BLOCK_SIZE, p2 + AES_BLOCK_SIZE, sizeof(p2) - AES_BLOCK_SIZE, &stream);
  9796. #if defined(WOLFSSL_ASYNC_CRYPT)
  9797. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9798. #endif
  9799. if (ret != 0)
  9800. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9801. if (XMEMCMP(c2, buf, sizeof(c2)))
  9802. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9803. #endif /* WOLFSSL_AESXTS_STREAM */
  9804. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9805. defined(WC_C_DYNAMIC_FALLBACK)
  9806. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9807. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  9808. #if defined(WOLFSSL_ASYNC_CRYPT)
  9809. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9810. #endif
  9811. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9812. if (ret != 0)
  9813. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9814. if (XMEMCMP(c2, buf, sizeof(c2)))
  9815. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9816. #endif
  9817. XMEMSET(buf, 0, sizeof(buf));
  9818. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  9819. if (ret != 0)
  9820. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9821. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  9822. #if defined(WOLFSSL_ASYNC_CRYPT)
  9823. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9824. #endif
  9825. if (ret != 0)
  9826. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9827. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  9828. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9829. #ifdef WOLFSSL_AESXTS_STREAM
  9830. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &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. ret = wc_AesXtsEncryptUpdate(aes, buf, p1, sizeof(p1), &stream);
  9837. #if defined(WOLFSSL_ASYNC_CRYPT)
  9838. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9839. #endif
  9840. if (ret != 0)
  9841. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9842. ret = wc_AesXtsEncryptFinal(aes, buf, NULL, 0, &stream);
  9843. #if defined(WOLFSSL_ASYNC_CRYPT)
  9844. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9845. #endif
  9846. if (ret != 0)
  9847. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9848. if (XMEMCMP(c1, buf, sizeof(c1)))
  9849. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9850. #endif /* WOLFSSL_AESXTS_STREAM */
  9851. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9852. defined(WC_C_DYNAMIC_FALLBACK)
  9853. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9854. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  9855. #if defined(WOLFSSL_ASYNC_CRYPT)
  9856. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9857. #endif
  9858. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9859. if (ret != 0)
  9860. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9861. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  9862. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9863. #endif
  9864. /* partial block encryption test */
  9865. XMEMSET(cipher, 0, sizeof(cipher));
  9866. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  9867. #if defined(WOLFSSL_ASYNC_CRYPT)
  9868. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9869. #endif
  9870. if (ret != 0)
  9871. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9872. if (XMEMCMP(cp2, cipher, sizeof(cp2)))
  9873. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9874. #ifdef WOLFSSL_AESXTS_STREAM
  9875. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &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. ret = wc_AesXtsEncryptFinal(aes, buf, pp, sizeof(pp), &stream);
  9882. #if defined(WOLFSSL_ASYNC_CRYPT)
  9883. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9884. #endif
  9885. if (ret != 0)
  9886. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9887. ret = wc_AesXtsEncryptFinal(aes, buf, NULL, 0, &stream);
  9888. #if defined(WOLFSSL_ASYNC_CRYPT)
  9889. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9890. #endif
  9891. if (ret != 0)
  9892. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9893. if (XMEMCMP(cp2, buf, sizeof(cp2)))
  9894. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9895. #endif /* WOLFSSL_AESXTS_STREAM */
  9896. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9897. defined(WC_C_DYNAMIC_FALLBACK)
  9898. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9899. XMEMSET(cipher, 0, sizeof(cipher));
  9900. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  9901. #if defined(WOLFSSL_ASYNC_CRYPT)
  9902. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9903. #endif
  9904. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9905. if (ret != 0)
  9906. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9907. if (XMEMCMP(cp2, cipher, sizeof(cp2)))
  9908. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9909. #endif
  9910. /* partial block decrypt test */
  9911. XMEMSET(buf, 0, sizeof(buf));
  9912. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  9913. if (ret != 0)
  9914. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9915. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  9916. #if defined(WOLFSSL_ASYNC_CRYPT)
  9917. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9918. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9919. #else
  9920. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9921. #endif
  9922. #endif
  9923. if (ret != 0)
  9924. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9925. if (XMEMCMP(pp, buf, sizeof(pp)))
  9926. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9927. #ifdef WOLFSSL_AESXTS_STREAM
  9928. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  9929. #if defined(WOLFSSL_ASYNC_CRYPT)
  9930. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9931. #endif
  9932. if (ret != 0)
  9933. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9934. ret = wc_AesXtsDecryptFinal(aes, buf, cipher, sizeof(pp), &stream);
  9935. #if defined(WOLFSSL_ASYNC_CRYPT)
  9936. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9937. #endif
  9938. if (ret != 0)
  9939. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9940. if (XMEMCMP(pp, buf, sizeof(pp)))
  9941. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9942. #endif /* WOLFSSL_AESXTS_STREAM */
  9943. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9944. defined(WC_C_DYNAMIC_FALLBACK)
  9945. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9946. XMEMSET(buf, 0, sizeof(buf));
  9947. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  9948. #if defined(WOLFSSL_ASYNC_CRYPT)
  9949. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9950. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9951. #else
  9952. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9953. #endif
  9954. #endif
  9955. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9956. if (ret != 0)
  9957. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9958. if (XMEMCMP(pp, buf, sizeof(pp)))
  9959. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9960. #endif
  9961. /* NIST decrypt test vector */
  9962. XMEMSET(buf, 0, sizeof(buf));
  9963. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  9964. #if defined(WOLFSSL_ASYNC_CRYPT)
  9965. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9966. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9967. #else
  9968. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9969. #endif
  9970. #endif
  9971. if (ret != 0)
  9972. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9973. if (XMEMCMP(p1, buf, sizeof(p1)))
  9974. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9975. #ifdef WOLFSSL_AESXTS_STREAM
  9976. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  9977. #if defined(WOLFSSL_ASYNC_CRYPT)
  9978. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9979. #endif
  9980. if (ret != 0)
  9981. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9982. ret = wc_AesXtsDecryptFinal(aes, buf, c1, sizeof(c1), &stream);
  9983. #if defined(WOLFSSL_ASYNC_CRYPT)
  9984. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9985. #endif
  9986. if (ret != 0)
  9987. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9988. if (XMEMCMP(p1, buf, sizeof(p1)))
  9989. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9990. #endif /* WOLFSSL_AESXTS_STREAM */
  9991. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9992. defined(WC_C_DYNAMIC_FALLBACK)
  9993. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9994. XMEMSET(buf, 0, sizeof(buf));
  9995. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  9996. #if defined(WOLFSSL_ASYNC_CRYPT)
  9997. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9998. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9999. #else
  10000. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10001. #endif
  10002. #endif
  10003. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  10004. if (ret != 0)
  10005. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10006. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  10007. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10008. #endif
  10009. /* fail case with decrypting using wrong key */
  10010. XMEMSET(buf, 0, sizeof(buf));
  10011. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  10012. #if defined(WOLFSSL_ASYNC_CRYPT)
  10013. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10014. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10015. #else
  10016. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10017. #endif
  10018. #endif
  10019. if (ret != 0)
  10020. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10021. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  10022. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10023. /* set correct key and retest */
  10024. XMEMSET(buf, 0, sizeof(buf));
  10025. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_DECRYPTION);
  10026. if (ret != 0)
  10027. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10028. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  10029. #if defined(WOLFSSL_ASYNC_CRYPT)
  10030. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10031. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10032. #else
  10033. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10034. #endif
  10035. #endif
  10036. if (ret != 0)
  10037. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10038. if (XMEMCMP(p2, buf, sizeof(p2)))
  10039. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10040. #ifndef HAVE_FIPS
  10041. /* Test ciphertext stealing in-place. */
  10042. XMEMCPY(buf, p3, sizeof(p3));
  10043. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_ENCRYPTION);
  10044. if (ret != 0)
  10045. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10046. ret = wc_AesXtsEncrypt(aes, buf, buf, sizeof(p3), i3, sizeof(i3));
  10047. #if defined(WOLFSSL_ASYNC_CRYPT)
  10048. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10049. #endif
  10050. if (ret != 0)
  10051. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10052. if (XMEMCMP(c3, buf, sizeof(c3)))
  10053. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10054. #ifdef WOLFSSL_AESXTS_STREAM
  10055. ret = wc_AesXtsEncryptInit(aes, i3, sizeof(i3), &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. ret = wc_AesXtsEncryptUpdate(aes, buf, p3, AES_BLOCK_SIZE, &stream);
  10062. #if defined(WOLFSSL_ASYNC_CRYPT)
  10063. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10064. #endif
  10065. if (ret != 0)
  10066. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10067. ret = wc_AesXtsEncryptFinal(aes, buf + AES_BLOCK_SIZE, p3 + AES_BLOCK_SIZE, sizeof(p3) - AES_BLOCK_SIZE, &stream);
  10068. #if defined(WOLFSSL_ASYNC_CRYPT)
  10069. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10070. #endif
  10071. if (ret != 0)
  10072. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10073. if (XMEMCMP(c3, buf, sizeof(c3)))
  10074. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10075. #endif /* WOLFSSL_AESXTS_STREAM */
  10076. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_DECRYPTION);
  10077. if (ret != 0)
  10078. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10079. ret = wc_AesXtsDecrypt(aes, buf, buf, sizeof(c3), i3, sizeof(i3));
  10080. #if defined(WOLFSSL_ASYNC_CRYPT)
  10081. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10082. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10083. #else
  10084. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10085. #endif
  10086. #endif
  10087. if (ret != 0)
  10088. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10089. if (XMEMCMP(p3, buf, sizeof(p3)))
  10090. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10091. #ifdef WOLFSSL_AESXTS_STREAM
  10092. ret = wc_AesXtsDecryptInit(aes, i3, sizeof(i3), &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. ret = wc_AesXtsDecryptUpdate(aes, buf, c3, AES_BLOCK_SIZE, &stream);
  10099. #if defined(WOLFSSL_ASYNC_CRYPT)
  10100. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10101. #endif
  10102. if (ret != 0)
  10103. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10104. ret = wc_AesXtsDecryptFinal(aes, buf + AES_BLOCK_SIZE, c3 + AES_BLOCK_SIZE, sizeof(c3) - AES_BLOCK_SIZE, &stream);
  10105. #if defined(WOLFSSL_ASYNC_CRYPT)
  10106. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10107. #endif
  10108. if (ret != 0)
  10109. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10110. if (XMEMCMP(p3, buf, sizeof(p3)))
  10111. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10112. #endif /* WOLFSSL_AESXTS_STREAM */
  10113. #endif /* !HAVE_FIPS */
  10114. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10115. !defined(WOLFSSL_AFALG)
  10116. {
  10117. int i;
  10118. int j;
  10119. #ifdef WOLFSSL_AESXTS_STREAM
  10120. int k;
  10121. #endif
  10122. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10123. large_input = (byte *)XMALLOC(LARGE_XTS_SZ, HEAP_HINT,
  10124. DYNAMIC_TYPE_TMP_BUFFER);
  10125. if (large_input == NULL)
  10126. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), out);
  10127. #endif
  10128. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  10129. large_input[i] = (byte)i;
  10130. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  10131. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10132. if (ret != 0)
  10133. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10134. ret = wc_AesXtsEncrypt(aes, large_input, large_input, j, i1,
  10135. sizeof(i1));
  10136. #if defined(WOLFSSL_ASYNC_CRYPT)
  10137. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10138. #endif
  10139. if (ret != 0)
  10140. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10141. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10142. if (ret != 0)
  10143. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10144. ret = wc_AesXtsDecrypt(aes, large_input, large_input, j, i1,
  10145. sizeof(i1));
  10146. #if defined(WOLFSSL_ASYNC_CRYPT)
  10147. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10148. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  10149. WC_ASYNC_FLAG_NONE);
  10150. #else
  10151. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10152. #endif
  10153. #endif
  10154. if (ret != 0)
  10155. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10156. for (i = 0; i < j; i++) {
  10157. if (large_input[i] != (byte)i) {
  10158. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10159. }
  10160. }
  10161. }
  10162. #ifdef WOLFSSL_AESXTS_STREAM
  10163. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  10164. large_input[i] = (byte)i;
  10165. /* first, encrypt block by block then decrypt with a one-shot call. */
  10166. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  10167. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10168. if (ret != 0)
  10169. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10170. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &stream);
  10171. #if defined(WOLFSSL_ASYNC_CRYPT)
  10172. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10173. #endif
  10174. if (ret != 0)
  10175. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10176. for (k = 0; k < j; k += AES_BLOCK_SIZE) {
  10177. if ((j - k) < AES_BLOCK_SIZE*2)
  10178. ret = wc_AesXtsEncryptFinal(aes, large_input + k, large_input + k, j - k, &stream);
  10179. else
  10180. ret = wc_AesXtsEncryptUpdate(aes, large_input + k, large_input + k, AES_BLOCK_SIZE, &stream);
  10181. #if defined(WOLFSSL_ASYNC_CRYPT)
  10182. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10183. #endif
  10184. if (ret != 0)
  10185. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10186. if ((j - k) < AES_BLOCK_SIZE*2)
  10187. break;
  10188. }
  10189. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10190. if (ret != 0)
  10191. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10192. ret = wc_AesXtsDecrypt(aes, large_input, large_input, (word32)j, i1,
  10193. sizeof(i1));
  10194. #if defined(WOLFSSL_ASYNC_CRYPT)
  10195. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10196. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  10197. WC_ASYNC_FLAG_NONE);
  10198. #else
  10199. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10200. #endif
  10201. #endif
  10202. if (ret != 0)
  10203. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10204. for (i = 0; i < j; i++) {
  10205. if (large_input[i] != (byte)i) {
  10206. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10207. }
  10208. }
  10209. }
  10210. /* second, encrypt with a one-shot call then decrypt block by block. */
  10211. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  10212. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10213. if (ret != 0)
  10214. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10215. ret = wc_AesXtsEncrypt(aes, large_input, large_input, (word32)j, i1,
  10216. sizeof(i1));
  10217. #if defined(WOLFSSL_ASYNC_CRYPT)
  10218. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10219. #endif
  10220. if (ret != 0)
  10221. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10222. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10223. if (ret != 0)
  10224. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10225. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  10226. #if defined(WOLFSSL_ASYNC_CRYPT)
  10227. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10228. #endif
  10229. if (ret != 0)
  10230. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10231. for (k = 0; k < j; k += AES_BLOCK_SIZE) {
  10232. if ((j - k) < AES_BLOCK_SIZE*2)
  10233. ret = wc_AesXtsDecryptFinal(aes, large_input + k, large_input + k, j - k, &stream);
  10234. else
  10235. ret = wc_AesXtsDecryptUpdate(aes, large_input + k, large_input + k, AES_BLOCK_SIZE, &stream);
  10236. #if defined(WOLFSSL_ASYNC_CRYPT)
  10237. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10238. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  10239. WC_ASYNC_FLAG_NONE);
  10240. #else
  10241. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10242. #endif
  10243. #endif
  10244. if (ret != 0)
  10245. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10246. if ((j - k) < AES_BLOCK_SIZE*2)
  10247. break;
  10248. }
  10249. for (i = 0; i < j; i++) {
  10250. if (large_input[i] != (byte)i) {
  10251. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10252. }
  10253. }
  10254. }
  10255. #endif /* WOLFSSL_AESXTS_STREAM */
  10256. }
  10257. #endif /* !BENCH_EMBEDDED && !HAVE_CAVIUM &&
  10258. * !WOLFSSL_AFALG
  10259. */
  10260. out:
  10261. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10262. !defined(WOLFSSL_AFALG) && defined(WOLFSSL_SMALL_STACK) && \
  10263. !defined(WOLFSSL_NO_MALLOC)
  10264. if (large_input)
  10265. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10266. #endif
  10267. if (aes_inited)
  10268. wc_AesXtsFree(aes);
  10269. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10270. if (aes)
  10271. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  10272. #endif
  10273. return ret;
  10274. }
  10275. #endif /* WOLFSSL_AES_192 */
  10276. #ifdef WOLFSSL_AES_256
  10277. static wc_test_ret_t aes_xts_256_test(void)
  10278. {
  10279. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10280. XtsAes *aes = NULL;
  10281. #else
  10282. XtsAes aes[1];
  10283. #endif
  10284. int aes_inited = 0;
  10285. wc_test_ret_t ret = 0;
  10286. unsigned char buf[AES_BLOCK_SIZE * 3];
  10287. unsigned char cipher[AES_BLOCK_SIZE * 3];
  10288. #ifdef WOLFSSL_AESXTS_STREAM
  10289. struct XtsAesStreamData stream;
  10290. #endif
  10291. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10292. !defined(WOLFSSL_AFALG)
  10293. #define LARGE_XTS_SZ 1024
  10294. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10295. byte* large_input = NULL;
  10296. #else
  10297. byte large_input[LARGE_XTS_SZ];
  10298. #endif
  10299. #endif
  10300. /* 256 key tests */
  10301. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  10302. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  10303. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  10304. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  10305. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  10306. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  10307. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3,
  10308. 0xd6, 0x19, 0xb7, 0xe2, 0xb9, 0x14, 0x1e, 0x58,
  10309. 0x31, 0x8e, 0xea, 0x39, 0x2c, 0xf4, 0x1b, 0x08
  10310. };
  10311. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  10312. 0xad, 0xf8, 0xd9, 0x26, 0x27, 0x46, 0x4a, 0xd2,
  10313. 0xf0, 0x42, 0x8e, 0x84, 0xa9, 0xf8, 0x75, 0x64
  10314. };
  10315. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  10316. 0x2e, 0xed, 0xea, 0x52, 0xcd, 0x82, 0x15, 0xe1,
  10317. 0xac, 0xc6, 0x47, 0xe8, 0x10, 0xbb, 0xc3, 0x64,
  10318. 0x2e, 0x87, 0x28, 0x7f, 0x8d, 0x2e, 0x57, 0xe3,
  10319. 0x6c, 0x0a, 0x24, 0xfb, 0xc1, 0x2a, 0x20, 0x2e
  10320. };
  10321. /* plain text test of partial block is not from NIST test vector list */
  10322. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  10323. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  10324. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  10325. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  10326. };
  10327. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  10328. 0xcb, 0xaa, 0xd0, 0xe2, 0xf6, 0xce, 0xa3, 0xf5,
  10329. 0x0b, 0x37, 0xf9, 0x34, 0xd4, 0x6a, 0x9b, 0x13,
  10330. 0x0b, 0x9d, 0x54, 0xf0, 0x7e, 0x34, 0xf3, 0x6a,
  10331. 0xf7, 0x93, 0xe8, 0x6f, 0x73, 0xc6, 0xd7, 0xdb
  10332. };
  10333. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  10334. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  10335. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  10336. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  10337. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  10338. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  10339. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a,
  10340. 0xb5, 0xb0, 0xc7, 0xe5, 0x2d, 0xb5, 0x06, 0x12,
  10341. 0xd2, 0xb5, 0x3a, 0xcb, 0x47, 0x8a, 0x53, 0xb4
  10342. };
  10343. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  10344. 0xe6, 0x42, 0x19, 0xed, 0xe0, 0xe1, 0xc2, 0xa0,
  10345. 0x0e, 0xf5, 0x58, 0x6a, 0xc4, 0x9b, 0xeb, 0x6f
  10346. };
  10347. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  10348. 0x24, 0xcb, 0x76, 0x22, 0x55, 0xb5, 0xa8, 0x00,
  10349. 0xf4, 0x6e, 0x80, 0x60, 0x56, 0x9e, 0x05, 0x53,
  10350. 0xbc, 0xfe, 0x86, 0x55, 0x3b, 0xca, 0xd5, 0x89,
  10351. 0xc7, 0x54, 0x1a, 0x73, 0xac, 0xc3, 0x9a, 0xbd,
  10352. 0x53, 0xc4, 0x07, 0x76, 0xd8, 0xe8, 0x22, 0x61,
  10353. 0x9e, 0xa9, 0xad, 0x77, 0xa0, 0x13, 0x4c, 0xfc
  10354. };
  10355. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  10356. 0xa3, 0xc6, 0xf3, 0xf3, 0x82, 0x79, 0x5b, 0x10,
  10357. 0x87, 0xd7, 0x02, 0x50, 0xdb, 0x2c, 0xd3, 0xb1,
  10358. 0xa1, 0x62, 0xa8, 0xb6, 0xdc, 0x12, 0x60, 0x61,
  10359. 0xc1, 0x0a, 0x84, 0xa5, 0x85, 0x3f, 0x3a, 0x89,
  10360. 0xe6, 0x6c, 0xdb, 0xb7, 0x9a, 0xb4, 0x28, 0x9b,
  10361. 0xc3, 0xea, 0xd8, 0x10, 0xe9, 0xc0, 0xaf, 0x92
  10362. };
  10363. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10364. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10365. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10366. #endif
  10367. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  10368. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  10369. ret = EVP_test(EVP_aes_256_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  10370. if (ret != 0) {
  10371. printf("EVP_aes_256_xts failed\n");
  10372. goto out;
  10373. }
  10374. #endif
  10375. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  10376. if (ret != 0)
  10377. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10378. else
  10379. aes_inited = 1;
  10380. XMEMSET(buf, 0, sizeof(buf));
  10381. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_ENCRYPTION);
  10382. if (ret != 0)
  10383. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10384. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  10385. #if defined(WOLFSSL_ASYNC_CRYPT)
  10386. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10387. #endif
  10388. if (ret != 0)
  10389. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10390. if (XMEMCMP(c2, buf, sizeof(c2)))
  10391. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10392. #ifdef WOLFSSL_AESXTS_STREAM
  10393. ret = wc_AesXtsEncryptInit(aes, i2, sizeof(i2), &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. ret = wc_AesXtsEncryptUpdate(aes, buf, p2, AES_BLOCK_SIZE, &stream);
  10400. #if defined(WOLFSSL_ASYNC_CRYPT)
  10401. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10402. #endif
  10403. if (ret != 0)
  10404. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10405. ret = wc_AesXtsEncryptFinal(aes, buf + AES_BLOCK_SIZE, p2 + AES_BLOCK_SIZE, sizeof(p2) - AES_BLOCK_SIZE, &stream);
  10406. #if defined(WOLFSSL_ASYNC_CRYPT)
  10407. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10408. #endif
  10409. if (ret != 0)
  10410. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10411. if (XMEMCMP(c2, buf, sizeof(c2)))
  10412. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10413. #endif /* WOLFSSL_AESXTS_STREAM */
  10414. XMEMSET(buf, 0, sizeof(buf));
  10415. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10416. if (ret != 0)
  10417. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10418. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  10419. #if defined(WOLFSSL_ASYNC_CRYPT)
  10420. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10421. #endif
  10422. if (ret != 0)
  10423. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10424. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  10425. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10426. #ifdef WOLFSSL_AESXTS_STREAM
  10427. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &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. ret = wc_AesXtsEncryptUpdate(aes, buf, p1, sizeof(p1), &stream);
  10434. #if defined(WOLFSSL_ASYNC_CRYPT)
  10435. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10436. #endif
  10437. if (ret != 0)
  10438. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10439. ret = wc_AesXtsEncryptFinal(aes, buf, NULL, 0, &stream);
  10440. #if defined(WOLFSSL_ASYNC_CRYPT)
  10441. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10442. #endif
  10443. if (ret != 0)
  10444. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10445. if (XMEMCMP(c1, buf, sizeof(c1)))
  10446. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10447. #endif /* WOLFSSL_AESXTS_STREAM */
  10448. /* partial block encryption test */
  10449. XMEMSET(cipher, 0, sizeof(cipher));
  10450. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  10451. #if defined(WOLFSSL_ASYNC_CRYPT)
  10452. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10453. #endif
  10454. if (ret != 0)
  10455. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10456. /* partial block decrypt test */
  10457. XMEMSET(buf, 0, sizeof(buf));
  10458. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10459. if (ret != 0)
  10460. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10461. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  10462. #if defined(WOLFSSL_ASYNC_CRYPT)
  10463. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10464. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10465. #else
  10466. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10467. #endif
  10468. #endif
  10469. if (ret != 0)
  10470. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10471. if (XMEMCMP(pp, buf, sizeof(pp)))
  10472. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10473. /* NIST decrypt test vector */
  10474. XMEMSET(buf, 0, sizeof(buf));
  10475. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  10476. #if defined(WOLFSSL_ASYNC_CRYPT)
  10477. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10478. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10479. #else
  10480. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10481. #endif
  10482. #endif
  10483. if (ret != 0)
  10484. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10485. if (XMEMCMP(p1, buf, sizeof(p1)))
  10486. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10487. #ifdef WOLFSSL_AESXTS_STREAM
  10488. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  10489. #if defined(WOLFSSL_ASYNC_CRYPT)
  10490. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10491. #endif
  10492. if (ret != 0)
  10493. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10494. ret = wc_AesXtsDecryptFinal(aes, buf, c1, sizeof(c1), &stream);
  10495. #if defined(WOLFSSL_ASYNC_CRYPT)
  10496. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10497. #endif
  10498. if (ret != 0)
  10499. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10500. if (XMEMCMP(p1, buf, sizeof(p1)))
  10501. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10502. #endif /* WOLFSSL_AESXTS_STREAM */
  10503. XMEMSET(buf, 0, sizeof(buf));
  10504. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_DECRYPTION);
  10505. if (ret != 0)
  10506. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10507. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  10508. #if defined(WOLFSSL_ASYNC_CRYPT)
  10509. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10510. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10511. #else
  10512. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10513. #endif
  10514. #endif
  10515. if (ret != 0)
  10516. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10517. if (XMEMCMP(p2, buf, sizeof(p2)))
  10518. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10519. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10520. !defined(WOLFSSL_AFALG)
  10521. {
  10522. int i;
  10523. int j;
  10524. #ifdef WOLFSSL_AESXTS_STREAM
  10525. int k;
  10526. #endif
  10527. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10528. large_input = (byte *)XMALLOC(LARGE_XTS_SZ, HEAP_HINT,
  10529. DYNAMIC_TYPE_TMP_BUFFER);
  10530. if (large_input == NULL)
  10531. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), out);
  10532. #endif
  10533. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  10534. large_input[i] = (byte)i;
  10535. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  10536. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10537. if (ret != 0)
  10538. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10539. ret = wc_AesXtsEncrypt(aes, large_input, large_input, j, i1,
  10540. sizeof(i1));
  10541. #if defined(WOLFSSL_ASYNC_CRYPT)
  10542. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10543. #endif
  10544. if (ret != 0)
  10545. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10546. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10547. if (ret != 0)
  10548. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10549. ret = wc_AesXtsDecrypt(aes, large_input, large_input, j, i1,
  10550. sizeof(i1));
  10551. #if defined(WOLFSSL_ASYNC_CRYPT)
  10552. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10553. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  10554. WC_ASYNC_FLAG_NONE);
  10555. #else
  10556. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10557. #endif
  10558. #endif
  10559. if (ret != 0)
  10560. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10561. for (i = 0; i < j; i++) {
  10562. if (large_input[i] != (byte)i) {
  10563. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10564. }
  10565. }
  10566. }
  10567. #ifdef WOLFSSL_AESXTS_STREAM
  10568. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  10569. large_input[i] = (byte)i;
  10570. /* first, encrypt block by block then decrypt with a one-shot call. */
  10571. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  10572. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10573. if (ret != 0)
  10574. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10575. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &stream);
  10576. #if defined(WOLFSSL_ASYNC_CRYPT)
  10577. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10578. #endif
  10579. if (ret != 0)
  10580. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10581. for (k = 0; k < j; k += AES_BLOCK_SIZE) {
  10582. if ((j - k) < AES_BLOCK_SIZE*2)
  10583. ret = wc_AesXtsEncryptFinal(aes, large_input + k, large_input + k, j - k, &stream);
  10584. else
  10585. ret = wc_AesXtsEncryptUpdate(aes, large_input + k, large_input + k, AES_BLOCK_SIZE, &stream);
  10586. #if defined(WOLFSSL_ASYNC_CRYPT)
  10587. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10588. #endif
  10589. if (ret != 0)
  10590. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10591. if ((j - k) < AES_BLOCK_SIZE*2)
  10592. break;
  10593. }
  10594. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10595. if (ret != 0)
  10596. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10597. ret = wc_AesXtsDecrypt(aes, large_input, large_input, (word32)j, i1,
  10598. sizeof(i1));
  10599. #if defined(WOLFSSL_ASYNC_CRYPT)
  10600. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10601. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  10602. WC_ASYNC_FLAG_NONE);
  10603. #else
  10604. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10605. #endif
  10606. #endif
  10607. if (ret != 0)
  10608. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10609. for (i = 0; i < j; i++) {
  10610. if (large_input[i] != (byte)i) {
  10611. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10612. }
  10613. }
  10614. }
  10615. /* second, encrypt with a one-shot call then decrypt block by block. */
  10616. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  10617. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10618. if (ret != 0)
  10619. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10620. ret = wc_AesXtsEncrypt(aes, large_input, large_input, (word32)j, i1,
  10621. sizeof(i1));
  10622. #if defined(WOLFSSL_ASYNC_CRYPT)
  10623. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10624. #endif
  10625. if (ret != 0)
  10626. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10627. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10628. if (ret != 0)
  10629. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10630. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  10631. #if defined(WOLFSSL_ASYNC_CRYPT)
  10632. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10633. #endif
  10634. if (ret != 0)
  10635. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10636. for (k = 0; k < j; k += AES_BLOCK_SIZE) {
  10637. if ((j - k) < AES_BLOCK_SIZE*2)
  10638. ret = wc_AesXtsDecryptFinal(aes, large_input + k, large_input + k, j - k, &stream);
  10639. else
  10640. ret = wc_AesXtsDecryptUpdate(aes, large_input + k, large_input + k, AES_BLOCK_SIZE, &stream);
  10641. #if defined(WOLFSSL_ASYNC_CRYPT)
  10642. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10643. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  10644. WC_ASYNC_FLAG_NONE);
  10645. #else
  10646. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10647. #endif
  10648. #endif
  10649. if (ret != 0)
  10650. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10651. if ((j - k) < AES_BLOCK_SIZE*2)
  10652. break;
  10653. }
  10654. for (i = 0; i < j; i++) {
  10655. if (large_input[i] != (byte)i) {
  10656. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10657. }
  10658. }
  10659. }
  10660. #endif /* WOLFSSL_AESXTS_STREAM */
  10661. }
  10662. #endif /* !BENCH_EMBEDDED && !HAVE_CAVIUM &&
  10663. * !WOLFSSL_AFALG
  10664. */
  10665. out:
  10666. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10667. !defined(WOLFSSL_AFALG) && defined(WOLFSSL_SMALL_STACK) && \
  10668. !defined(WOLFSSL_NO_MALLOC)
  10669. if (large_input)
  10670. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10671. #endif
  10672. if (aes_inited)
  10673. wc_AesXtsFree(aes);
  10674. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10675. if (aes)
  10676. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  10677. #endif
  10678. return ret;
  10679. }
  10680. #endif /* WOLFSSL_AES_256 */
  10681. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  10682. /* both 128 and 256 bit key test */
  10683. static wc_test_ret_t aes_xts_sector_test(void)
  10684. {
  10685. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10686. XtsAes *aes = NULL;
  10687. #else
  10688. XtsAes aes[1];
  10689. #endif
  10690. int aes_inited = 0;
  10691. wc_test_ret_t ret = 0;
  10692. unsigned char buf[AES_BLOCK_SIZE * 2];
  10693. /* 128 key tests */
  10694. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  10695. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  10696. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  10697. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  10698. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  10699. };
  10700. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  10701. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  10702. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  10703. };
  10704. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  10705. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  10706. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  10707. };
  10708. word64 s1 = 141;
  10709. /* 256 key tests */
  10710. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  10711. 0xef, 0x01, 0x0c, 0xa1, 0xa3, 0x66, 0x3e, 0x32,
  10712. 0x53, 0x43, 0x49, 0xbc, 0x0b, 0xae, 0x62, 0x23,
  10713. 0x2a, 0x15, 0x73, 0x34, 0x85, 0x68, 0xfb, 0x9e,
  10714. 0xf4, 0x17, 0x68, 0xa7, 0x67, 0x4f, 0x50, 0x7a,
  10715. 0x72, 0x7f, 0x98, 0x75, 0x53, 0x97, 0xd0, 0xe0,
  10716. 0xaa, 0x32, 0xf8, 0x30, 0x33, 0x8c, 0xc7, 0xa9,
  10717. 0x26, 0xc7, 0x73, 0xf0, 0x9e, 0x57, 0xb3, 0x57,
  10718. 0xcd, 0x15, 0x6a, 0xfb, 0xca, 0x46, 0xe1, 0xa0
  10719. };
  10720. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  10721. 0xed, 0x98, 0xe0, 0x17, 0x70, 0xa8, 0x53, 0xb4,
  10722. 0x9d, 0xb9, 0xe6, 0xaa, 0xf8, 0x8f, 0x0a, 0x41,
  10723. 0xb9, 0xb5, 0x6e, 0x91, 0xa5, 0xa2, 0xb1, 0x1d,
  10724. 0x40, 0x52, 0x92, 0x54, 0xf5, 0x52, 0x3e, 0x75
  10725. };
  10726. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  10727. 0xca, 0x20, 0xc5, 0x5e, 0x8d, 0xc1, 0x49, 0x68,
  10728. 0x7d, 0x25, 0x41, 0xde, 0x39, 0xc3, 0xdf, 0x63,
  10729. 0x00, 0xbb, 0x5a, 0x16, 0x3c, 0x10, 0xce, 0xd3,
  10730. 0x66, 0x6b, 0x13, 0x57, 0xdb, 0x8b, 0xd3, 0x9d
  10731. };
  10732. word64 s2 = 187;
  10733. #if !defined(BENCH_EMBEDDED) && \
  10734. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  10735. /* Sector size for encrypt/decrypt consecutive sectors testcase */
  10736. word32 sectorSz = 512;
  10737. unsigned char data[550];
  10738. WOLFSSL_SMALL_STACK_STATIC unsigned char k3[] = {
  10739. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  10740. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  10741. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  10742. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22
  10743. };
  10744. WOLFSSL_SMALL_STACK_STATIC unsigned char p3[] = {
  10745. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  10746. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10747. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
  10748. 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  10749. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
  10750. 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  10751. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53,
  10752. 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  10753. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
  10754. 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  10755. 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
  10756. 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  10757. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
  10758. 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  10759. 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
  10760. 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  10761. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb,
  10762. 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  10763. 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
  10764. 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  10765. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
  10766. 0xfc, 0xfd, 0xfe, 0xff,
  10767. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  10768. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10769. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
  10770. 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  10771. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
  10772. 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  10773. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53,
  10774. 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  10775. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
  10776. 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  10777. 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
  10778. 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  10779. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
  10780. 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  10781. 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
  10782. 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  10783. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb,
  10784. 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  10785. 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
  10786. 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  10787. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
  10788. 0xfc, 0xfd, 0xfe, 0xff,
  10789. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  10790. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10791. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  10792. };
  10793. WOLFSSL_SMALL_STACK_STATIC unsigned char c3[] = {
  10794. 0xb9, 0x6b, 0x2b, 0xfd, 0x61, 0x87, 0x84, 0xd5, 0x26, 0xd2, 0x8c, 0x62,
  10795. 0x63, 0x01, 0xca, 0x46, 0xb1, 0x82, 0xfa, 0xdc, 0xbc, 0x32, 0x18, 0xe9,
  10796. 0xda, 0xe6, 0xda, 0xd1, 0x1a, 0x52, 0x77, 0xca, 0xdb, 0x0e, 0xbe, 0x37,
  10797. 0x88, 0x36, 0x1c, 0x87, 0x16, 0x60, 0xfe, 0xa8, 0x9e, 0xf6, 0x48, 0x64,
  10798. 0x94, 0x34, 0x64, 0xed, 0xf6, 0x9a, 0xc5, 0x28, 0xc9, 0xed, 0x64, 0x80,
  10799. 0x85, 0xd8, 0x93, 0xa7, 0x50, 0xb1, 0x9d, 0x2f, 0x1e, 0x34, 0xcc, 0xb4,
  10800. 0x03, 0xfb, 0x6b, 0x43, 0x21, 0xa8, 0x5b, 0xc6, 0x59, 0x13, 0xd2, 0xb5,
  10801. 0xf5, 0x7b, 0xf6, 0xb2, 0xa4, 0x7a, 0xd2, 0x50, 0x26, 0xcb, 0xa4, 0x83,
  10802. 0xc3, 0x56, 0xb0, 0xb1, 0x14, 0x34, 0x12, 0x1b, 0xea, 0x26, 0x97, 0x24,
  10803. 0x54, 0xcc, 0x32, 0x4c, 0xa4, 0xc2, 0xa3, 0x07, 0xfa, 0x30, 0xa9, 0xf0,
  10804. 0x91, 0x17, 0x60, 0x68, 0x88, 0x7f, 0x34, 0x7e, 0xbd, 0x20, 0x33, 0x95,
  10805. 0x6e, 0xc0, 0xb6, 0x2b, 0xff, 0x7e, 0x61, 0x35, 0x9a, 0x88, 0xff, 0xd9,
  10806. 0x69, 0x21, 0xe7, 0x8f, 0x45, 0x02, 0xf9, 0xd7, 0xeb, 0xa6, 0x53, 0xf1,
  10807. 0x73, 0x04, 0xf1, 0x0b, 0x85, 0xc6, 0x1f, 0x4a, 0x51, 0x2f, 0x95, 0x87,
  10808. 0x5a, 0x67, 0x37, 0xb2, 0x87, 0xf7, 0xbe, 0x2a, 0x17, 0x57, 0xca, 0xfc,
  10809. 0xdd, 0x5f, 0x37, 0x48, 0x78, 0xbd, 0xfa, 0x75, 0xc9, 0xfa, 0x86, 0x7e,
  10810. 0xc4, 0x0f, 0x60, 0x85, 0xce, 0x12, 0x44, 0x7c, 0xd9, 0xb2, 0x50, 0xd9,
  10811. 0x57, 0x85, 0xa5, 0xd7, 0x68, 0x59, 0x03, 0x09, 0x97, 0x2e, 0x8e, 0xa5,
  10812. 0xe3, 0x98, 0xac, 0x16, 0xfb, 0x6d, 0x54, 0xc5, 0x5d, 0x7a, 0x33, 0x44,
  10813. 0x0a, 0x39, 0x91, 0xcc, 0x9f, 0x67, 0xf9, 0x89, 0xbb, 0x62, 0x02, 0xc4,
  10814. 0x22, 0xec, 0xcf, 0x97, 0x69, 0x81, 0x3d, 0x00, 0xfd, 0xeb, 0x55, 0x08,
  10815. 0xa2, 0xff, 0x97, 0xaa, 0x79, 0xde, 0x3c, 0x8a, 0x78, 0x71, 0x73, 0xa2,
  10816. 0x98, 0x2f, 0xd8, 0x5c, 0x62, 0x1c, 0x5c, 0x23, 0x0a, 0xd1, 0xf1, 0x81,
  10817. 0x8a, 0x12, 0xe7, 0x4d, 0xdd, 0x4f, 0xd4, 0xf1, 0xe8, 0x0f, 0x25, 0x79,
  10818. 0x45, 0x4a, 0x49, 0x49, 0x7e, 0x56, 0x91, 0x4e, 0xaa, 0xba, 0x18, 0xe1,
  10819. 0xe4, 0xbe, 0x21, 0xdc, 0x58, 0x60, 0x6f, 0x6a, 0x7f, 0xdc, 0x5e, 0x74,
  10820. 0x47, 0xbf, 0xeb, 0x84, 0xc4, 0x1e, 0x5a, 0x61, 0x64, 0xc8, 0x63, 0x68,
  10821. 0xfa, 0x17, 0x9c, 0xac, 0x60, 0x1c, 0xa5, 0x6e, 0x00, 0x21, 0x93, 0x3c,
  10822. 0xd7, 0xbb, 0x73, 0x45, 0xf7, 0x34, 0x81, 0x6c, 0xfa, 0xf2, 0x33, 0xfd,
  10823. 0xb1, 0x40, 0x30, 0x6b, 0x30, 0xd1, 0x83, 0x5e, 0x2e, 0x7a, 0xce, 0xa6,
  10824. 0x12, 0x2a, 0x15, 0x03, 0x78, 0x29, 0xb9, 0x07, 0xae, 0xe7, 0xc2, 0x78,
  10825. 0x74, 0x72, 0xa5, 0x0e, 0x6b, 0x1f, 0x78, 0xf2, 0x5a, 0x69, 0xb6, 0x2b,
  10826. 0x99, 0x94, 0x1f, 0x89, 0xd1, 0x21, 0x14, 0x4a, 0x54, 0xab, 0x5a, 0x9f,
  10827. 0xaa, 0xa7, 0x96, 0x0a, 0x21, 0xce, 0x30, 0xb6, 0x70, 0x81, 0xe9, 0xd3,
  10828. 0x71, 0xc0, 0xf1, 0x15, 0xe2, 0xf6, 0xd3, 0xcc, 0x41, 0x15, 0x9d, 0xd5,
  10829. 0xa3, 0xa4, 0xe0, 0xf8, 0x62, 0xc4, 0x76, 0x65, 0x63, 0x89, 0xa7, 0xe2,
  10830. 0xfb, 0xf5, 0xc9, 0x80, 0x15, 0x5b, 0xc1, 0x59, 0xb2, 0xd0, 0x01, 0x3a,
  10831. 0xf9, 0xab, 0x5b, 0x79, 0x54, 0xed, 0x6b, 0xf9, 0x1d, 0x9d, 0x87, 0x63,
  10832. 0x80, 0x4f, 0xec, 0x9c, 0x4f, 0xad, 0x97, 0x04, 0xff, 0x62, 0x4a, 0x17,
  10833. 0xc0, 0x09, 0x2a, 0x2c, 0x23, 0x4b, 0xc3, 0xb6, 0x6d, 0xed, 0xdb, 0x1a,
  10834. 0x6f, 0x56, 0x2b, 0x78, 0x92, 0x3a, 0x5c, 0x7f, 0xb2, 0x63, 0xd3, 0xd5,
  10835. 0x1a, 0xbe, 0xc2, 0x34, 0xc8, 0xad, 0x36, 0xb7, 0x12, 0xb8, 0xe1, 0xb7,
  10836. 0x52, 0x7f, 0x16, 0x84, 0x2c, 0x47, 0x7e, 0xf2, 0xa5, 0x36, 0x2e, 0xad,
  10837. 0xe7, 0xbb, 0xc0, 0x6f, 0x27, 0x8e, 0x41, 0x08, 0x75, 0xe5, 0xff, 0xde,
  10838. 0x08, 0x9f, 0x8c, 0x91, 0xba, 0xc9, 0x9d, 0x9f, 0x27, 0x90, 0x50, 0x44,
  10839. 0x24, 0xe7, 0x3d, 0x6f
  10840. };
  10841. word64 s3 = 0x000000ffffffffff;
  10842. #endif
  10843. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10844. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10845. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10846. #endif
  10847. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  10848. if (ret != 0)
  10849. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10850. else
  10851. aes_inited = 1;
  10852. XMEMSET(buf, 0, sizeof(buf));
  10853. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10854. if (ret != 0)
  10855. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10856. ret = wc_AesXtsEncryptSector(aes, buf, p1, sizeof(p1), s1);
  10857. #if defined(WOLFSSL_ASYNC_CRYPT)
  10858. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10859. #endif
  10860. if (ret != 0)
  10861. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10862. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  10863. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10864. /* decrypt test */
  10865. XMEMSET(buf, 0, sizeof(buf));
  10866. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10867. if (ret != 0)
  10868. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10869. ret = wc_AesXtsDecryptSector(aes, buf, c1, sizeof(c1), s1);
  10870. #if defined(WOLFSSL_ASYNC_CRYPT)
  10871. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10872. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10873. #else
  10874. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10875. #endif
  10876. #endif
  10877. if (ret != 0)
  10878. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10879. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  10880. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10881. /* 256 bit key tests */
  10882. XMEMSET(buf, 0, sizeof(buf));
  10883. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_ENCRYPTION);
  10884. if (ret != 0)
  10885. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10886. ret = wc_AesXtsEncryptSector(aes, buf, p2, sizeof(p2), s2);
  10887. #if defined(WOLFSSL_ASYNC_CRYPT)
  10888. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10889. #endif
  10890. if (ret != 0)
  10891. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10892. if (XMEMCMP(c2, buf, sizeof(c2)))
  10893. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10894. /* decrypt test */
  10895. XMEMSET(buf, 0, sizeof(buf));
  10896. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_DECRYPTION);
  10897. if (ret != 0)
  10898. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10899. ret = wc_AesXtsDecryptSector(aes, buf, c2, sizeof(c2), s2);
  10900. #if defined(WOLFSSL_ASYNC_CRYPT)
  10901. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10902. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10903. #else
  10904. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10905. #endif
  10906. #endif
  10907. if (ret != 0)
  10908. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10909. if (XMEMCMP(p2, buf, sizeof(p2)))
  10910. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10911. #if !defined(BENCH_EMBEDDED) && \
  10912. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  10913. /* encrypt consecutive sectors test */
  10914. XMEMSET(data, 0, sizeof(buf));
  10915. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_ENCRYPTION);
  10916. if (ret != 0)
  10917. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10918. ret = wc_AesXtsEncryptConsecutiveSectors(aes, data, p3,
  10919. sizeof(p3), s3, sectorSz);
  10920. #if defined(WOLFSSL_ASYNC_CRYPT)
  10921. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10922. #endif
  10923. if (ret != 0)
  10924. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10925. if (XMEMCMP(c3, data, sizeof(c3)))
  10926. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10927. /* decrypt consecutive sectors test */
  10928. XMEMSET(data, 0, sizeof(buf));
  10929. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_DECRYPTION);
  10930. if (ret != 0)
  10931. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10932. ret = wc_AesXtsDecryptConsecutiveSectors(aes, data, c3,
  10933. sizeof(c3), s3, sectorSz);
  10934. #if defined(WOLFSSL_ASYNC_CRYPT)
  10935. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10936. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10937. #else
  10938. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10939. #endif
  10940. #endif
  10941. if (ret != 0)
  10942. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10943. if (XMEMCMP(p3, data, sizeof(p3)))
  10944. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10945. #endif /* !BENCH_EMBEDDED && (!HAVE_FIPS || FIPS_VERSION_GE(5, 3)) */
  10946. out:
  10947. if (aes_inited)
  10948. wc_AesXtsFree(aes);
  10949. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10950. if (aes)
  10951. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  10952. #endif
  10953. return ret;
  10954. }
  10955. #endif /* WOLFSSL_AES_128 && WOLFSSL_AES_256 */
  10956. #ifdef WOLFSSL_AES_128
  10957. /* testing of bad arguments */
  10958. static wc_test_ret_t aes_xts_args_test(void)
  10959. {
  10960. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10961. XtsAes *aes = NULL;
  10962. #else
  10963. XtsAes aes[1];
  10964. #endif
  10965. int aes_inited = 0;
  10966. wc_test_ret_t ret;
  10967. unsigned char buf[AES_BLOCK_SIZE * 2];
  10968. /* 128 key tests */
  10969. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  10970. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  10971. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  10972. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  10973. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  10974. };
  10975. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  10976. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  10977. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  10978. };
  10979. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  10980. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  10981. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  10982. };
  10983. word64 s1 = 141;
  10984. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10985. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10986. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10987. #endif
  10988. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  10989. if (ret != 0)
  10990. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10991. else
  10992. aes_inited = 1;
  10993. if (wc_AesXtsSetKeyNoInit(NULL, k1, sizeof(k1), AES_ENCRYPTION) == 0)
  10994. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10995. if (wc_AesXtsSetKeyNoInit(aes, NULL, sizeof(k1), AES_ENCRYPTION) == 0)
  10996. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10997. /* encryption operations */
  10998. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10999. if (ret != 0)
  11000. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11001. ret = wc_AesXtsEncryptSector(NULL, buf, p1, sizeof(p1), s1);
  11002. #if defined(WOLFSSL_ASYNC_CRYPT)
  11003. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  11004. #endif
  11005. if (ret == 0)
  11006. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11007. ret = wc_AesXtsEncryptSector(aes, NULL, p1, sizeof(p1), s1);
  11008. #if defined(WOLFSSL_ASYNC_CRYPT)
  11009. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  11010. #endif
  11011. if (ret == 0)
  11012. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11013. /* decryption operations */
  11014. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  11015. if (ret != 0)
  11016. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11017. ret = wc_AesXtsDecryptSector(NULL, buf, c1, sizeof(c1), s1);
  11018. #if defined(WOLFSSL_ASYNC_CRYPT)
  11019. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  11020. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  11021. #else
  11022. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  11023. #endif
  11024. #endif
  11025. if (ret == 0)
  11026. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11027. ret = wc_AesXtsDecryptSector(aes, NULL, c1, sizeof(c1), s1);
  11028. #if defined(WOLFSSL_ASYNC_CRYPT)
  11029. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  11030. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  11031. #else
  11032. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  11033. #endif
  11034. #endif
  11035. if (ret == 0)
  11036. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11037. ret = 0;
  11038. out:
  11039. if (aes_inited)
  11040. wc_AesXtsFree(aes);
  11041. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11042. if (aes)
  11043. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  11044. #endif
  11045. return ret;
  11046. }
  11047. #endif /* WOLFSSL_AES_128 */
  11048. #endif /* WOLFSSL_AES_XTS && (!HAVE_FIPS || FIPS_VERSION_GE(5,3)) */
  11049. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  11050. static wc_test_ret_t aes_cbc_test(void)
  11051. {
  11052. byte cipher[AES_BLOCK_SIZE];
  11053. byte plain[AES_BLOCK_SIZE];
  11054. wc_test_ret_t ret;
  11055. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  11056. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  11057. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  11058. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  11059. };
  11060. byte key[] = "0123456789abcdef "; /* align */
  11061. byte iv[] = "1234567890abcdef "; /* align */
  11062. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  11063. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  11064. /* Parameter Validation testing. */
  11065. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key, 17, NULL);
  11066. if (ret != BAD_FUNC_ARG)
  11067. return WC_TEST_RET_ENC_EC(ret);
  11068. #ifdef HAVE_AES_DECRYPT
  11069. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key, 17, NULL);
  11070. if (ret != BAD_FUNC_ARG)
  11071. return WC_TEST_RET_ENC_EC(ret);
  11072. #endif
  11073. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key,
  11074. AES_BLOCK_SIZE, iv);
  11075. if (ret != 0)
  11076. return WC_TEST_RET_ENC_EC(ret);
  11077. #ifdef HAVE_AES_DECRYPT
  11078. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key,
  11079. AES_BLOCK_SIZE, iv);
  11080. if (ret != 0)
  11081. return WC_TEST_RET_ENC_EC(ret);
  11082. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE) != 0)
  11083. return WC_TEST_RET_ENC_NC;
  11084. #endif /* HAVE_AES_DECRYPT */
  11085. (void)plain;
  11086. return 0;
  11087. }
  11088. #endif
  11089. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11090. static wc_test_ret_t aesecb_test(void)
  11091. {
  11092. wc_test_ret_t ret = 0;
  11093. #if defined(WOLFSSL_AES_256)
  11094. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11095. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  11096. #else
  11097. Aes enc[1];
  11098. #endif
  11099. int enc_inited = 0;
  11100. byte cipher[AES_BLOCK_SIZE * 4];
  11101. #ifdef HAVE_AES_DECRYPT
  11102. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11103. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  11104. #else
  11105. Aes dec[1];
  11106. #endif
  11107. int dec_inited = 0;
  11108. byte plain[AES_BLOCK_SIZE * 4];
  11109. #endif /* HAVE_AES_DECRYPT */
  11110. {
  11111. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  11112. {
  11113. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  11114. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  11115. };
  11116. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  11117. {
  11118. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  11119. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  11120. };
  11121. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  11122. {
  11123. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  11124. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  11125. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  11126. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  11127. };
  11128. ret = wc_AesInit(enc, HEAP_HINT, devId);
  11129. if (ret != 0)
  11130. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11131. enc_inited = 1;
  11132. #if defined(HAVE_AES_DECRYPT)
  11133. ret = wc_AesInit(dec, HEAP_HINT, devId);
  11134. if (ret != 0)
  11135. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11136. dec_inited = 1;
  11137. #endif
  11138. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  11139. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  11140. if (ret != 0)
  11141. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11142. if (wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE) != 0)
  11143. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11144. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  11145. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11146. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  11147. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  11148. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11149. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  11150. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11151. if (ret != 0)
  11152. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11153. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11154. ret = wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE);
  11155. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11156. if (ret != 0)
  11157. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11158. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  11159. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11160. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  11161. ret = wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE);
  11162. if (ret != 0)
  11163. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11164. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  11165. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11166. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  11167. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  11168. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11169. ret = wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE);
  11170. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11171. if (ret != 0)
  11172. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11173. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  11174. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11175. #endif
  11176. #ifdef HAVE_AES_DECRYPT
  11177. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  11178. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  11179. if (ret != 0)
  11180. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11181. if (wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE) != 0)
  11182. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11183. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  11184. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11185. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  11186. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  11187. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11188. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  11189. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11190. if (ret != 0)
  11191. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11192. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11193. ret = wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  11194. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11195. if (ret != 0)
  11196. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11197. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  11198. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11199. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  11200. ret = wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  11201. if (ret != 0)
  11202. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11203. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  11204. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11205. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  11206. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  11207. if (ret != 0)
  11208. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11209. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11210. ret = wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  11211. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11212. if (ret != 0)
  11213. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11214. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  11215. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11216. #endif
  11217. #endif /* HAVE_AES_DECRYPT */
  11218. }
  11219. out:
  11220. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11221. if (enc) {
  11222. if (enc_inited)
  11223. wc_AesFree(enc);
  11224. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  11225. }
  11226. #ifdef HAVE_AES_DECRYPT
  11227. if (dec) {
  11228. if (dec_inited)
  11229. wc_AesFree(dec);
  11230. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  11231. }
  11232. #endif
  11233. #else
  11234. if (enc_inited)
  11235. wc_AesFree(enc);
  11236. #ifdef HAVE_AES_DECRYPT
  11237. if (dec_inited)
  11238. wc_AesFree(dec);
  11239. #endif
  11240. #endif
  11241. #endif /* WOLFSSL_AES_256 */
  11242. return ret;
  11243. }
  11244. #endif /* HAVE_AES_ECB */
  11245. #ifdef WOLFSSL_AES_COUNTER
  11246. static wc_test_ret_t aesctr_test(Aes* enc, Aes* dec, byte* cipher, byte* plain)
  11247. {
  11248. wc_test_ret_t ret = 0;
  11249. /* test vectors from "Recommendation for Block Cipher Modes of
  11250. * Operation" NIST Special Publication 800-38A */
  11251. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  11252. {
  11253. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  11254. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  11255. };
  11256. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  11257. {
  11258. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  11259. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  11260. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  11261. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  11262. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  11263. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  11264. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  11265. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  11266. };
  11267. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11268. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32[] =
  11269. {
  11270. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,
  11271. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  11272. };
  11273. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32_2[] =
  11274. {
  11275. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  11276. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xfe
  11277. };
  11278. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64[] =
  11279. {
  11280. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  11281. 0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  11282. };
  11283. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64_2[] =
  11284. {
  11285. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0,
  11286. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  11287. };
  11288. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96[] =
  11289. {
  11290. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  11291. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff
  11292. };
  11293. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96_2[] =
  11294. {
  11295. 0xff,0xff,0xff,0xf0,0xff,0xff,0xff,0xff,
  11296. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  11297. };
  11298. #endif
  11299. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128[] =
  11300. {
  11301. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  11302. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  11303. };
  11304. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11305. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128_2[] =
  11306. {
  11307. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  11308. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  11309. };
  11310. #endif
  11311. #ifdef WOLFSSL_AES_128
  11312. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  11313. {
  11314. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  11315. 0xc2
  11316. };
  11317. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Key[] =
  11318. {
  11319. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  11320. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  11321. };
  11322. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Cipher[] =
  11323. {
  11324. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  11325. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  11326. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  11327. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  11328. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  11329. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  11330. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  11331. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  11332. };
  11333. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11334. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32Cipher[] =
  11335. {
  11336. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  11337. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  11338. 0x7e,0xad,0xea,0x5c,0xd1
  11339. };
  11340. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32CipherLong[] =
  11341. {
  11342. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  11343. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  11344. 0x7e,0xad,0xea,0x5c,0xd1,0xb7,0x98,0xf0,
  11345. 0x22,0x20,0xfe,0x67,0xb0,0x02,0x23,0x50
  11346. };
  11347. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32_2CipherLong[] =
  11348. {
  11349. 0x6e,0xa1,0x27,0x4d,0xea,0x20,0x5f,0x39,
  11350. 0x68,0xc8,0xb6,0x78,0xde,0xfc,0x53,0x5c,
  11351. 0x90,0xc8,0xf6,0xc6,0xfa,0xe0,0x7b,0x09,
  11352. 0x7c,0xf8,0x9c,0x6a,0x5a,0xa5,0x17,0x7f,
  11353. 0x03,0x92,0x5f,0x4e,0x85,0xea,0x26,0xc9,
  11354. 0x5a,0xc2,0x74,0xe2,0xbf,0xe4,0x1b,0xd4
  11355. };
  11356. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64Cipher[] =
  11357. {
  11358. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  11359. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  11360. 0xae,0x74,0x94,0x72,0x8e
  11361. };
  11362. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64CipherLong[] =
  11363. {
  11364. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  11365. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  11366. 0xae,0x74,0x94,0x72,0x8e,0xd0,0x71,0xc0,
  11367. 0x89,0x8a,0xa1,0xb0,0x29,0xa0,0x10,0x9e
  11368. };
  11369. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64_2CipherLong[] =
  11370. {
  11371. 0x3f,0xe7,0xd5,0xf3,0xfa,0x09,0xfe,0x40,
  11372. 0xa6,0xa1,0x32,0x8b,0x57,0x12,0xb9,0xfa,
  11373. 0xf2,0x2d,0xe4,0x3c,0x66,0x1d,0x0a,0x8e,
  11374. 0x46,0xf8,0x2e,0x33,0xce,0x8d,0x4e,0x3b,
  11375. 0x17,0x67,0x9e,0x9f,0x76,0x9e,0xc2,0x99,
  11376. 0xd5,0xd4,0x71,0xed,0xb4,0x33,0xb2,0xcd
  11377. };
  11378. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96Cipher[] =
  11379. {
  11380. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  11381. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  11382. 0x9d,0x77,0xc9,0x5f,0x38
  11383. };
  11384. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96CipherLong[] =
  11385. {
  11386. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  11387. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  11388. 0x9d,0x77,0xc9,0x5f,0x38,0xb5,0x6e,0x44,
  11389. 0x21,0x8e,0xda,0x57,0xe0,0x41,0xc7,0x6a
  11390. };
  11391. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96_2CipherLong[] =
  11392. {
  11393. 0xc8,0x81,0x1a,0xbe,0xc7,0x5b,0x93,0x6f,
  11394. 0xe6,0x52,0xe4,0xb1,0x2d,0x1c,0x39,0xbc,
  11395. 0xeb,0x82,0x27,0x0a,0x7e,0xa5,0x0e,0x2d,
  11396. 0x32,0xda,0xbe,0x10,0x7a,0x10,0xcc,0xd3,
  11397. 0x6f,0xc6,0x83,0x28,0x05,0x57,0x8a,0x24,
  11398. 0x44,0x76,0x17,0x81,0xb9,0x5c,0x94,0x81
  11399. };
  11400. #endif
  11401. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128Cipher[] =
  11402. {
  11403. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  11404. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  11405. 0xd3,0xda,0xe1,0x5b,0x04
  11406. };
  11407. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128CipherLong[] =
  11408. {
  11409. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  11410. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  11411. 0xd3,0xda,0xe1,0x5b,0x04,0xbb,0x35,0x2f,
  11412. 0xa0,0xf5,0x9f,0xeb,0xfc,0xb4,0xda,0x3e
  11413. };
  11414. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11415. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128_2CipherLong[] =
  11416. {
  11417. 0xba,0x76,0xaa,0x54,0xd5,0xb5,0x60,0x67,
  11418. 0xc1,0xa7,0x90,0x3b,0x3f,0xdd,0xfa,0x89,
  11419. 0x24,0xdf,0x0c,0x56,0x5c,0xf4,0x2a,0x68,
  11420. 0x97,0x87,0x13,0xb6,0x7a,0xd1,0x24,0xfd,
  11421. 0x4d,0x3f,0x77,0x4a,0xb9,0xe4,0x7d,0xa2,
  11422. 0xdb,0xb9,0x31,0x5e,0xa3,0x11,0x06,0x80
  11423. };
  11424. #endif
  11425. #endif /* WOLFSSL_AES_128 */
  11426. #ifdef WOLFSSL_AES_192
  11427. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  11428. {
  11429. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  11430. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  11431. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  11432. };
  11433. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  11434. {
  11435. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  11436. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b,
  11437. 0x09,0x03,0x39,0xec,0x0a,0xa6,0xfa,0xef,
  11438. 0xd5,0xcc,0xc2,0xc6,0xf4,0xce,0x8e,0x94,
  11439. 0x1e,0x36,0xb2,0x6b,0xd1,0xeb,0xc6,0x70,
  11440. 0xd1,0xbd,0x1d,0x66,0x56,0x20,0xab,0xf7,
  11441. 0x4f,0x78,0xa7,0xf6,0xd2,0x98,0x09,0x58,
  11442. 0x5a,0x97,0xda,0xec,0x58,0xc6,0xb0,0x50
  11443. };
  11444. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11445. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32Cipher[] =
  11446. {
  11447. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  11448. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  11449. 0x0c,0x33,0x97,0x06,0xc0
  11450. };
  11451. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32CipherLong[] =
  11452. {
  11453. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  11454. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  11455. 0x0c,0x33,0x97,0x06,0xc0,0xbe,0x83,0x87,
  11456. 0xdd,0xd3,0xff,0xd8,0xe4,0x6a,0x5b,0x84
  11457. };
  11458. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32_2CipherLong[] =
  11459. {
  11460. 0xf5,0x00,0xa2,0x91,0x54,0xa3,0x76,0xa2,
  11461. 0xdd,0xad,0x16,0x89,0xe5,0xf0,0x1d,0x40,
  11462. 0x84,0xcd,0x74,0x84,0xcb,0x8b,0x9e,0x29,
  11463. 0xa9,0x8a,0x12,0x65,0xa0,0x79,0x5e,0xce,
  11464. 0xd9,0x50,0x65,0x21,0x86,0xb0,0x85,0x0d,
  11465. 0x98,0x2d,0x9a,0x5a,0x11,0xbe,0xa5,0x7f
  11466. };
  11467. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64Cipher[] =
  11468. {
  11469. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  11470. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  11471. 0x3d,0xf7,0xb4,0xfd,0x8c
  11472. };
  11473. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64CipherLong[] =
  11474. {
  11475. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  11476. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  11477. 0x3d,0xf7,0xb4,0xfd,0x8c,0x55,0x47,0x10,
  11478. 0xd5,0x91,0x35,0xbe,0xd8,0x0d,0xa5,0x9e
  11479. };
  11480. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64_2CipherLong[] =
  11481. {
  11482. 0x59,0xf1,0xed,0x70,0x62,0x42,0xa8,0x06,
  11483. 0x07,0x36,0xe1,0xc5,0x04,0x79,0xc3,0x9b,
  11484. 0xd1,0x14,0x5c,0xcc,0x6f,0x81,0x5f,0x2f,
  11485. 0xa0,0xde,0xcf,0x61,0x55,0x18,0x7a,0xac,
  11486. 0xb0,0x59,0x37,0x90,0x53,0xb3,0x00,0x88,
  11487. 0xb4,0x49,0x90,0x7b,0x96,0xcd,0xcc,0xc3
  11488. };
  11489. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96Cipher[] =
  11490. {
  11491. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  11492. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  11493. 0x47,0xb5,0xf3,0x30,0x3b
  11494. };
  11495. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96CipherLong[] =
  11496. {
  11497. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  11498. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  11499. 0x47,0xb5,0xf3,0x30,0x3b,0xef,0xcd,0x80,
  11500. 0xe3,0x61,0x34,0xef,0x4e,0x1b,0x79,0xc1
  11501. };
  11502. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96_2CipherLong[] =
  11503. {
  11504. 0x3c,0xb2,0xff,0xc0,0x24,0xe1,0xf5,0xc4,
  11505. 0x0f,0xd1,0x0a,0x1b,0xbe,0x1f,0x23,0xa1,
  11506. 0x8e,0xbf,0x2b,0x96,0xb6,0x37,0xc8,0x25,
  11507. 0x06,0x90,0xe2,0xca,0x71,0x24,0x52,0x95,
  11508. 0xaa,0x8c,0x80,0xdf,0xb7,0xd7,0x30,0xb0,
  11509. 0xcc,0x06,0x4f,0x28,0xa2,0x74,0x27,0xf8
  11510. };
  11511. #endif
  11512. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128Cipher[] =
  11513. {
  11514. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  11515. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  11516. 0x8c,0x68,0xa7,0xd9,0x57
  11517. };
  11518. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128CipherLong[] =
  11519. {
  11520. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  11521. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  11522. 0x8c,0x68,0xa7,0xd9,0x57,0xab,0x09,0x0f,
  11523. 0x01,0xc4,0x4e,0x62,0xaf,0xc2,0xdf,0x1a
  11524. };
  11525. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11526. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128_2CipherLong[] =
  11527. {
  11528. 0x88,0x0a,0x26,0x4e,0xa8,0x26,0x21,0xe0,
  11529. 0xfc,0xbc,0x63,0xdc,0xd9,0x60,0x52,0xb2,
  11530. 0x99,0x2f,0xbb,0x1e,0x00,0xf5,0x9f,0x6d,
  11531. 0xab,0x48,0x0f,0xc6,0x37,0x12,0x56,0xe3,
  11532. 0x12,0x8d,0x31,0xc8,0xea,0xf4,0x41,0x82,
  11533. 0x7a,0x88,0xe0,0xd7,0xf0,0x67,0x03,0xa4
  11534. };
  11535. #endif
  11536. #endif
  11537. #ifdef WOLFSSL_AES_256
  11538. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  11539. {
  11540. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  11541. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  11542. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  11543. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  11544. };
  11545. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  11546. {
  11547. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  11548. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,
  11549. 0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,
  11550. 0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5,
  11551. 0x2b,0x09,0x30,0xda,0xa2,0x3d,0xe9,0x4c,
  11552. 0xe8,0x70,0x17,0xba,0x2d,0x84,0x98,0x8d,
  11553. 0xdf,0xc9,0xc5,0x8d,0xb6,0x7a,0xad,0xa6,
  11554. 0x13,0xc2,0xdd,0x08,0x45,0x79,0x41,0xa6
  11555. };
  11556. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11557. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32Cipher[] =
  11558. {
  11559. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  11560. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  11561. 0xa2,0x13,0x47,0x74,0xda
  11562. };
  11563. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32CipherLong[] =
  11564. {
  11565. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  11566. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  11567. 0xa2,0x13,0x47,0x74,0xda,0x89,0x77,0x40,
  11568. 0x28,0x9c,0xe8,0x19,0x26,0x32,0xd8,0x1f
  11569. };
  11570. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32_2CipherLong[] =
  11571. {
  11572. 0xf6,0xd9,0x22,0xc6,0x80,0x29,0xaf,0x14,
  11573. 0x54,0x6c,0x0a,0xce,0x42,0xea,0x3c,0xa1,
  11574. 0x7c,0xeb,0x36,0x0d,0x8e,0xd7,0x8c,0x59,
  11575. 0xa8,0x09,0x9f,0x9e,0xba,0x5b,0x95,0xfa,
  11576. 0x26,0x8c,0x37,0x59,0xf8,0xae,0x8e,0xaa,
  11577. 0x4d,0xe4,0x1c,0xfe,0x51,0xc7,0xb7,0xcc
  11578. };
  11579. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64Cipher[] =
  11580. {
  11581. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  11582. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  11583. 0xd4,0x94,0x4c,0xcd,0x4d
  11584. };
  11585. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64CipherLong[] =
  11586. {
  11587. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  11588. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  11589. 0xd4,0x94,0x4c,0xcd,0x4d,0x47,0x5a,0x92,
  11590. 0x26,0x49,0x81,0x7a,0xda,0x36,0x27,0x01
  11591. };
  11592. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64_2CipherLong[] =
  11593. {
  11594. 0xf7,0x9c,0xbf,0xf6,0xa2,0xaa,0x8a,0x0a,
  11595. 0x63,0x8a,0x20,0x2f,0x12,0xf1,0x8e,0x49,
  11596. 0x30,0xc0,0x8d,0x5c,0x5f,0x8b,0xbc,0x16,
  11597. 0xdd,0x71,0xee,0x13,0x14,0x7b,0xe1,0x25,
  11598. 0xcb,0x87,0x8a,0xc6,0xdc,0x1d,0x54,0x7a,
  11599. 0xe1,0xe4,0x6f,0x0d,0x95,0x1b,0xd1,0x8b
  11600. };
  11601. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96Cipher[] =
  11602. {
  11603. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  11604. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  11605. 0xb8,0x69,0xa1,0x48,0x45
  11606. };
  11607. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96CipherLong[] =
  11608. {
  11609. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  11610. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  11611. 0xb8,0x69,0xa1,0x48,0x45,0xf1,0xc6,0x27,
  11612. 0x36,0xa8,0xb2,0x4b,0x0e,0x62,0x6b,0x72
  11613. };
  11614. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96_2CipherLong[] =
  11615. {
  11616. 0xd5,0x56,0x73,0xaa,0xb8,0xe4,0x06,0xf6,
  11617. 0x83,0x45,0x3a,0xb4,0xb9,0x63,0xec,0xad,
  11618. 0x73,0xc5,0xab,0x78,0xb1,0x21,0xab,0xef,
  11619. 0x69,0x15,0xb7,0x0c,0xe9,0xb4,0x3a,0xe7,
  11620. 0xbc,0xc4,0x22,0xbd,0x93,0xba,0x52,0xe0,
  11621. 0x91,0x2f,0x5e,0x8d,0x6d,0x59,0xf7,0xc2
  11622. };
  11623. #endif
  11624. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128Cipher[] =
  11625. {
  11626. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  11627. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  11628. 0x4b,0x45,0x7c,0xd6,0x8a
  11629. };
  11630. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128CipherLong[] =
  11631. {
  11632. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  11633. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  11634. 0x4b,0x45,0x7c,0xd6,0x8a,0xcc,0xda,0x4a,
  11635. 0x89,0xfa,0x23,0x6c,0x06,0xbf,0x26,0x05
  11636. };
  11637. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11638. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128_2CipherLong[] =
  11639. {
  11640. 0x24,0x5c,0x09,0xa0,0x3b,0x1a,0x5a,0x94,
  11641. 0x2b,0x93,0x56,0x13,0x48,0xa0,0x21,0xce,
  11642. 0x95,0x11,0xa3,0x76,0xd6,0x59,0x88,0x42,
  11643. 0x04,0x71,0x69,0x62,0x28,0xb2,0xee,0x9d,
  11644. 0xd5,0xa0,0xea,0xc7,0x37,0x93,0x92,0xc7,
  11645. 0xf2,0xb6,0x8d,0xd9,0x59,0x1a,0xfa,0xbb
  11646. };
  11647. #endif
  11648. #endif
  11649. int i;
  11650. struct {
  11651. const byte* key;
  11652. int keySz;
  11653. const byte* iv;
  11654. const byte* plain;
  11655. int len;
  11656. const byte* cipher;
  11657. } testVec[] = {
  11658. #ifdef WOLFSSL_AES_128
  11659. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  11660. ctrPlain, (int)sizeof(ctrPlain), ctr128Cipher },
  11661. /* let's try with just 9 bytes, non block size test */
  11662. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  11663. ctrPlain, (int)sizeof(oddCipher), ctr128Cipher },
  11664. /* and an additional 9 bytes to reuse tmp left buffer */
  11665. { NULL, 0, NULL, ctrPlain, (int)sizeof(oddCipher), oddCipher },
  11666. /* Counter wrapping */
  11667. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  11668. ctrPlain, (int)sizeof(ctr128Wrap128Cipher), ctr128Wrap128Cipher },
  11669. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  11670. ctrPlain, (int)sizeof(ctr128Wrap128CipherLong),
  11671. ctr128Wrap128CipherLong },
  11672. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11673. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128_2,
  11674. ctrPlain, (int)sizeof(ctr128Wrap128_2CipherLong),
  11675. ctr128Wrap128_2CipherLong },
  11676. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  11677. ctrPlain, (int)sizeof(ctr128Wrap96Cipher), ctr128Wrap96Cipher },
  11678. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  11679. ctrPlain, (int)sizeof(ctr128Wrap96CipherLong),
  11680. ctr128Wrap96CipherLong },
  11681. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96_2,
  11682. ctrPlain, (int)sizeof(ctr128Wrap96_2CipherLong),
  11683. ctr128Wrap96_2CipherLong },
  11684. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  11685. ctrPlain, (int)sizeof(ctr128Wrap64Cipher), ctr128Wrap64Cipher },
  11686. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  11687. ctrPlain, (int)sizeof(ctr128Wrap64CipherLong),
  11688. ctr128Wrap64CipherLong },
  11689. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64_2,
  11690. ctrPlain, (int)sizeof(ctr128Wrap64_2CipherLong),
  11691. ctr128Wrap64_2CipherLong },
  11692. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  11693. ctrPlain, (int)sizeof(ctr128Wrap32Cipher), ctr128Wrap32Cipher },
  11694. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  11695. ctrPlain, (int)sizeof(ctr128Wrap32CipherLong),
  11696. ctr128Wrap32CipherLong },
  11697. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32_2,
  11698. ctrPlain, (int)sizeof(ctr128Wrap32_2CipherLong),
  11699. ctr128Wrap32_2CipherLong },
  11700. #endif
  11701. #endif
  11702. #ifdef WOLFSSL_AES_192
  11703. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  11704. ctrPlain, (int)sizeof(ctrPlain), ctr192Cipher },
  11705. /* let's try with just 9 bytes, non block size test */
  11706. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  11707. ctrPlain, (int)sizeof(oddCipher), ctr192Cipher },
  11708. /* Counter wrapping */
  11709. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  11710. ctrPlain, (int)sizeof(ctr192Wrap128Cipher), ctr192Wrap128Cipher },
  11711. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  11712. ctrPlain, (int)sizeof(ctr192Wrap128CipherLong),
  11713. ctr192Wrap128CipherLong },
  11714. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11715. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128_2,
  11716. ctrPlain, (int)sizeof(ctr192Wrap128_2CipherLong),
  11717. ctr192Wrap128_2CipherLong },
  11718. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  11719. ctrPlain, (int)sizeof(ctr192Wrap96Cipher), ctr192Wrap96Cipher },
  11720. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  11721. ctrPlain, (int)sizeof(ctr192Wrap96CipherLong),
  11722. ctr192Wrap96CipherLong },
  11723. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96_2,
  11724. ctrPlain, (int)sizeof(ctr192Wrap96_2CipherLong),
  11725. ctr192Wrap96_2CipherLong },
  11726. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  11727. ctrPlain, (int)sizeof(ctr192Wrap64Cipher), ctr192Wrap64Cipher },
  11728. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  11729. ctrPlain, (int)sizeof(ctr192Wrap64CipherLong),
  11730. ctr192Wrap64CipherLong },
  11731. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64_2,
  11732. ctrPlain, (int)sizeof(ctr192Wrap64_2CipherLong),
  11733. ctr192Wrap64_2CipherLong },
  11734. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  11735. ctrPlain, (int)sizeof(ctr192Wrap32Cipher), ctr192Wrap32Cipher },
  11736. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  11737. ctrPlain, (int)sizeof(ctr192Wrap32CipherLong),
  11738. ctr192Wrap32CipherLong },
  11739. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32_2,
  11740. ctrPlain, (int)sizeof(ctr192Wrap32_2CipherLong),
  11741. ctr192Wrap32_2CipherLong },
  11742. #endif
  11743. #endif
  11744. #ifdef WOLFSSL_AES_256
  11745. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  11746. ctrPlain, (int)sizeof(ctrPlain), ctr256Cipher },
  11747. /* let's try with just 9 bytes, non block size test */
  11748. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  11749. ctrPlain, (int)sizeof(oddCipher), ctr256Cipher },
  11750. /* Counter wrapping */
  11751. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  11752. ctrPlain, (int)sizeof(ctr256Wrap128Cipher), ctr256Wrap128Cipher },
  11753. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  11754. ctrPlain, (int)sizeof(ctr256Wrap128CipherLong),
  11755. ctr256Wrap128CipherLong },
  11756. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11757. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128_2,
  11758. ctrPlain, (int)sizeof(ctr256Wrap128_2CipherLong),
  11759. ctr256Wrap128_2CipherLong },
  11760. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  11761. ctrPlain, (int)sizeof(ctr256Wrap96Cipher), ctr256Wrap96Cipher },
  11762. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  11763. ctrPlain, (int)sizeof(ctr256Wrap96CipherLong),
  11764. ctr256Wrap96CipherLong },
  11765. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96_2,
  11766. ctrPlain, (int)sizeof(ctr256Wrap96_2CipherLong),
  11767. ctr256Wrap96_2CipherLong },
  11768. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  11769. ctrPlain, (int)sizeof(ctr256Wrap64Cipher), ctr256Wrap64Cipher },
  11770. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  11771. ctrPlain, (int)sizeof(ctr256Wrap64CipherLong),
  11772. ctr256Wrap64CipherLong },
  11773. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64_2,
  11774. ctrPlain, (int)sizeof(ctr256Wrap64_2CipherLong),
  11775. ctr256Wrap64_2CipherLong },
  11776. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  11777. ctrPlain, (int)sizeof(ctr256Wrap32Cipher), ctr256Wrap32Cipher },
  11778. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  11779. ctrPlain, (int)sizeof(ctr256Wrap32CipherLong),
  11780. ctr256Wrap32CipherLong },
  11781. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32_2,
  11782. ctrPlain, (int)sizeof(ctr256Wrap32_2CipherLong),
  11783. ctr256Wrap32_2CipherLong },
  11784. #endif
  11785. #endif
  11786. };
  11787. #define AES_CTR_TEST_LEN (int)(sizeof(testVec) / sizeof(*testVec))
  11788. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  11789. if (testVec[i].key != NULL) {
  11790. ret = wc_AesSetKeyDirect(enc, testVec[i].key, (word32)testVec[i].keySz,
  11791. testVec[i].iv, AES_ENCRYPTION);
  11792. if (ret != 0) {
  11793. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11794. }
  11795. /* Ctr only uses encrypt, even on key setup */
  11796. ret = wc_AesSetKeyDirect(dec, testVec[i].key, (word32)testVec[i].keySz,
  11797. testVec[i].iv, AES_ENCRYPTION);
  11798. if (ret != 0) {
  11799. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11800. }
  11801. }
  11802. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, (word32)testVec[i].len);
  11803. if (ret != 0) {
  11804. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11805. }
  11806. ret = wc_AesCtrEncrypt(dec, plain, cipher, (word32)testVec[i].len);
  11807. if (ret != 0) {
  11808. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11809. }
  11810. if (XMEMCMP(plain, ctrPlain, (size_t)testVec[i].len)) {
  11811. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11812. }
  11813. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  11814. if (XMEMCMP(cipher, testVec[i].cipher, (size_t)testVec[i].len)) {
  11815. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11816. }
  11817. #endif
  11818. }
  11819. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  11820. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  11821. if (testVec[i].key != NULL) {
  11822. ret = wc_AesSetKeyDirect(enc, testVec[i].key, testVec[i].keySz,
  11823. testVec[i].iv, AES_ENCRYPTION);
  11824. if (ret != 0) {
  11825. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11826. }
  11827. /* Ctr only uses encrypt, even on key setup */
  11828. ret = wc_AesSetKeyDirect(dec, testVec[i].key, testVec[i].keySz,
  11829. testVec[i].iv, AES_ENCRYPTION);
  11830. if (ret != 0) {
  11831. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11832. }
  11833. }
  11834. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11835. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, testVec[i].len);
  11836. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11837. if (ret != 0) {
  11838. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11839. }
  11840. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11841. ret = wc_AesCtrEncrypt(dec, plain, cipher, testVec[i].len);
  11842. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11843. if (ret != 0) {
  11844. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11845. }
  11846. if (XMEMCMP(plain, ctrPlain, testVec[i].len)) {
  11847. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11848. }
  11849. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  11850. if (XMEMCMP(cipher, testVec[i].cipher, testVec[i].len)) {
  11851. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11852. }
  11853. #endif
  11854. }
  11855. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  11856. if (testVec[i].key != NULL) {
  11857. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11858. ret = wc_AesSetKeyDirect(enc, testVec[i].key, testVec[i].keySz,
  11859. testVec[i].iv, AES_ENCRYPTION);
  11860. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11861. if (ret != 0) {
  11862. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11863. }
  11864. /* Ctr only uses encrypt, even on key setup */
  11865. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11866. ret = wc_AesSetKeyDirect(dec, testVec[i].key, testVec[i].keySz,
  11867. testVec[i].iv, AES_ENCRYPTION);
  11868. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11869. if (ret != 0) {
  11870. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11871. }
  11872. }
  11873. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, testVec[i].len);
  11874. if (ret != 0) {
  11875. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11876. }
  11877. ret = wc_AesCtrEncrypt(dec, plain, cipher, testVec[i].len);
  11878. if (ret != 0) {
  11879. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11880. }
  11881. if (XMEMCMP(plain, ctrPlain, testVec[i].len)) {
  11882. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11883. }
  11884. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  11885. if (XMEMCMP(cipher, testVec[i].cipher, testVec[i].len)) {
  11886. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11887. }
  11888. #endif
  11889. }
  11890. #endif /* DEBUG_VECTOR_REGISTER_ACCESS && WC_C_DYNAMIC_FALLBACK */
  11891. out:
  11892. return ret;
  11893. }
  11894. #endif /* WOLFSSL_AES_COUNTER */
  11895. #ifdef HAVE_AES_ECB
  11896. static wc_test_ret_t aes_ecb_test(Aes* enc, Aes* dec, byte* cipher, byte* plain)
  11897. {
  11898. wc_test_ret_t ret = 0;
  11899. WOLFSSL_SMALL_STACK_STATIC const byte key_128[] = "0123456789abcdef ";
  11900. WOLFSSL_SMALL_STACK_STATIC const byte key_192[] =
  11901. "0123456789abcdef01234567 ";
  11902. WOLFSSL_SMALL_STACK_STATIC const byte key_256[] =
  11903. "0123456789abcdef0123456789abcdef ";
  11904. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = "1234567890abcdef ";
  11905. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  11906. 0x6e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
  11907. 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20
  11908. };
  11909. WOLFSSL_SMALL_STACK_STATIC const byte verify_ecb_128[AES_BLOCK_SIZE] = {
  11910. 0xd0, 0xc9, 0xd9, 0xc9, 0x40, 0xe8, 0x97, 0xb6,
  11911. 0xc8, 0x8c, 0x33, 0x3b, 0xb5, 0x8f, 0x85, 0xd1
  11912. };
  11913. WOLFSSL_SMALL_STACK_STATIC const byte verify_ecb_192[AES_BLOCK_SIZE] = {
  11914. 0x06, 0x57, 0xee, 0x78, 0x3f, 0x96, 0x00, 0xb1,
  11915. 0xec, 0x76, 0x94, 0x30, 0x29, 0xbe, 0x15, 0xab
  11916. };
  11917. WOLFSSL_SMALL_STACK_STATIC const byte verify_ecb_256[AES_BLOCK_SIZE] = {
  11918. 0xcd, 0xf2, 0x81, 0x3e, 0x73, 0x3e, 0xf7, 0x33,
  11919. 0x3d, 0x18, 0xfd, 0x41, 0x85, 0x37, 0x04, 0x82
  11920. };
  11921. int i;
  11922. struct {
  11923. const byte* key;
  11924. int keySz;
  11925. const byte* iv;
  11926. const byte* verify;
  11927. } testVec[] = {
  11928. { key_128, 16, iv, verify_ecb_128 },
  11929. { key_192, 24, iv, verify_ecb_192 },
  11930. { key_256, 32, iv, verify_ecb_256 },
  11931. };
  11932. #define AES_ECB_TEST_LEN (int)(sizeof(testVec) / sizeof(*testVec))
  11933. for (i = 0; i < AES_ECB_TEST_LEN; i++) {
  11934. ret = wc_AesSetKey(enc, testVec[i].key, testVec[i].keySz, testVec[i].iv,
  11935. AES_ENCRYPTION);
  11936. if (ret != 0)
  11937. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11938. #ifdef HAVE_AES_DECRYPT
  11939. ret = wc_AesSetKey(dec, testVec[i].key, testVec[i].keySz, testVec[i].iv,
  11940. AES_DECRYPTION);
  11941. if (ret != 0)
  11942. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11943. #endif
  11944. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  11945. ret = wc_AesEcbEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  11946. #if defined(WOLFSSL_ASYNC_CRYPT)
  11947. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11948. #endif
  11949. if (ret != 0)
  11950. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11951. if (XMEMCMP(cipher, testVec[i].verify, AES_BLOCK_SIZE)) {
  11952. WOLFSSL_MSG("aes_test cipher vs verify_ecb mismatch!");
  11953. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11954. }
  11955. #ifdef HAVE_AES_DECRYPT
  11956. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  11957. ret = wc_AesEcbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  11958. #if defined(WOLFSSL_ASYNC_CRYPT)
  11959. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11960. #endif
  11961. if (ret != 0)
  11962. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11963. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  11964. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11965. #endif /* HAVE_AES_DECRYPT */
  11966. (void)dec;
  11967. (void)plain;
  11968. }
  11969. out:
  11970. return ret;
  11971. }
  11972. #endif
  11973. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_test(void)
  11974. {
  11975. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  11976. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11977. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  11978. #else
  11979. Aes enc[1];
  11980. #endif
  11981. int enc_inited = 0;
  11982. byte cipher[AES_BLOCK_SIZE * 4];
  11983. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  11984. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11985. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  11986. #else
  11987. Aes dec[1];
  11988. #endif
  11989. int dec_inited = 0;
  11990. byte plain [AES_BLOCK_SIZE * 4];
  11991. #endif /* HAVE_AES_DECRYPT || WOLFSSL_AES_COUNTER */
  11992. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  11993. wc_test_ret_t ret = 0;
  11994. #ifdef HAVE_AES_CBC
  11995. #ifdef WOLFSSL_AES_128
  11996. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  11997. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  11998. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  11999. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  12000. };
  12001. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  12002. {
  12003. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  12004. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  12005. };
  12006. #ifdef HAVE_RENESAS_SYNC
  12007. const byte *key =
  12008. (byte*)guser_PKCbInfo.wrapped_key_aes128;
  12009. #else
  12010. WOLFSSL_SMALL_STACK_STATIC const
  12011. byte key[] = "0123456789abcdef "; /* align */
  12012. #endif
  12013. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = "1234567890abcdef "; /* align */
  12014. WOLFSSL_ENTER("aes_test");
  12015. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12016. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || \
  12017. defined(WOLFSSL_AES_DIRECT)
  12018. if (enc == NULL)
  12019. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12020. #endif
  12021. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  12022. if (dec == NULL)
  12023. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12024. #endif
  12025. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  12026. ret = wc_AesInit(enc, HEAP_HINT, devId);
  12027. if (ret != 0)
  12028. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12029. enc_inited = 1;
  12030. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  12031. ret = wc_AesInit(dec, HEAP_HINT, devId);
  12032. if (ret != 0)
  12033. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12034. dec_inited = 1;
  12035. #endif
  12036. #ifdef HAVE_AES_ECB
  12037. ret = aes_ecb_test(enc, dec, cipher, plain);
  12038. if (ret != 0)
  12039. return ret;
  12040. #endif
  12041. ret = wc_AesSetKey(enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  12042. if (ret != 0)
  12043. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12044. #ifdef HAVE_AES_DECRYPT
  12045. ret = wc_AesSetKey(dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  12046. if (ret != 0)
  12047. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12048. #endif
  12049. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  12050. ret = wc_AesCbcEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  12051. #if defined(WOLFSSL_ASYNC_CRYPT)
  12052. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12053. #endif
  12054. if (ret != 0)
  12055. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12056. #ifdef HAVE_AES_DECRYPT
  12057. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  12058. ret = wc_AesCbcDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  12059. #if defined(WOLFSSL_ASYNC_CRYPT)
  12060. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12061. #endif
  12062. if (ret != 0) {
  12063. WOLFSSL_MSG("failed wc_AesCbcDecrypt");
  12064. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12065. }
  12066. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE)) {
  12067. WOLFSSL_MSG("wc_AesCbcDecrypt failed plain compare");
  12068. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12069. }
  12070. #endif /* HAVE_AES_DECRYPT */
  12071. /* skipped because wrapped key use in case of renesas sm */
  12072. #ifndef HAVE_RENESAS_SYNC
  12073. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE)) {
  12074. WOLFSSL_MSG("wc_AesCbcDecrypt failed cipher-verify compare");
  12075. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12076. }
  12077. #endif
  12078. #endif /* WOLFSSL_AES_128 */
  12079. #if defined(WOLFSSL_AESNI) && defined(HAVE_AES_DECRYPT)
  12080. {
  12081. WOLFSSL_SMALL_STACK_STATIC const byte bigMsg[] = {
  12082. /* "All work and no play makes Jack a dull boy. " */
  12083. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  12084. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  12085. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  12086. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  12087. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  12088. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  12089. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  12090. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  12091. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  12092. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  12093. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  12094. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  12095. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  12096. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  12097. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  12098. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  12099. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  12100. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  12101. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  12102. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  12103. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  12104. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  12105. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  12106. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  12107. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  12108. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  12109. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  12110. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  12111. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  12112. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  12113. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  12114. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  12115. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  12116. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  12117. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  12118. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  12119. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  12120. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  12121. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  12122. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  12123. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  12124. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  12125. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  12126. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  12127. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  12128. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  12129. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  12130. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20
  12131. };
  12132. WOLFSSL_SMALL_STACK_STATIC const byte bigKey[] = "0123456789abcdeffedcba9876543210";
  12133. word32 keySz, msgSz;
  12134. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12135. byte *bigCipher = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12136. byte *bigPlain = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12137. if ((bigCipher == NULL) ||
  12138. (bigPlain == NULL)) {
  12139. if (bigCipher != NULL)
  12140. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12141. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12142. }
  12143. #else
  12144. byte bigCipher[sizeof(bigMsg)];
  12145. byte bigPlain[sizeof(bigMsg)];
  12146. #endif
  12147. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  12148. * message by AES_BLOCK_SIZE for each size of AES key. */
  12149. for (keySz = 16; keySz <= 32; keySz += 8) {
  12150. for (msgSz = AES_BLOCK_SIZE;
  12151. msgSz <= sizeof(bigMsg);
  12152. msgSz += AES_BLOCK_SIZE) {
  12153. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  12154. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  12155. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  12156. if (ret != 0) {
  12157. ret = WC_TEST_RET_ENC_EC(ret);
  12158. break;
  12159. }
  12160. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  12161. if (ret != 0) {
  12162. ret = WC_TEST_RET_ENC_EC(ret);
  12163. break;
  12164. }
  12165. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  12166. #if defined(WOLFSSL_ASYNC_CRYPT)
  12167. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12168. #endif
  12169. if (ret != 0) {
  12170. ret = WC_TEST_RET_ENC_EC(ret);
  12171. break;
  12172. }
  12173. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  12174. #if defined(WOLFSSL_ASYNC_CRYPT)
  12175. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12176. #endif
  12177. if (ret != 0) {
  12178. ret = WC_TEST_RET_ENC_EC(ret);
  12179. break;
  12180. }
  12181. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  12182. ret = WC_TEST_RET_ENC_NC;
  12183. break;
  12184. }
  12185. }
  12186. if (ret != 0)
  12187. break;
  12188. }
  12189. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  12190. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  12191. * message by AES_BLOCK_SIZE for each size of AES key. */
  12192. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12193. for (keySz = 16; keySz <= 32; keySz += 8) {
  12194. for (msgSz = AES_BLOCK_SIZE;
  12195. msgSz <= sizeof(bigMsg);
  12196. msgSz += AES_BLOCK_SIZE) {
  12197. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  12198. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  12199. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  12200. if (ret != 0) {
  12201. ret = WC_TEST_RET_ENC_EC(ret);
  12202. break;
  12203. }
  12204. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  12205. if (ret != 0) {
  12206. ret = WC_TEST_RET_ENC_EC(ret);
  12207. break;
  12208. }
  12209. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  12210. #if defined(WOLFSSL_ASYNC_CRYPT)
  12211. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12212. #endif
  12213. if (ret != 0) {
  12214. ret = WC_TEST_RET_ENC_EC(ret);
  12215. break;
  12216. }
  12217. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  12218. #if defined(WOLFSSL_ASYNC_CRYPT)
  12219. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12220. #endif
  12221. if (ret != 0) {
  12222. ret = WC_TEST_RET_ENC_EC(ret);
  12223. break;
  12224. }
  12225. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  12226. ret = WC_TEST_RET_ENC_NC;
  12227. break;
  12228. }
  12229. }
  12230. if (ret != 0)
  12231. break;
  12232. }
  12233. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12234. #endif /* DEBUG_VECTOR_REGISTER_ACCESS && WC_C_DYNAMIC_FALLBACK */
  12235. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12236. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12237. XFREE(bigPlain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12238. #endif
  12239. if (ret != 0)
  12240. goto out;
  12241. }
  12242. #endif /* WOLFSSL_AESNI && HAVE_AES_DECRYPT */
  12243. /* Test of AES IV state with encrypt/decrypt */
  12244. #if defined(WOLFSSL_AES_128) && !defined(HAVE_RENESAS_SYNC)
  12245. {
  12246. /* Test Vector from "NIST Special Publication 800-38A, 2001 Edition"
  12247. * https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38a.pdf
  12248. */
  12249. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  12250. {
  12251. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  12252. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  12253. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  12254. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
  12255. };
  12256. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] =
  12257. {
  12258. 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
  12259. 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  12260. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
  12261. 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2
  12262. };
  12263. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  12264. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  12265. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  12266. };
  12267. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  12268. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12269. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  12270. };
  12271. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  12272. if (ret != 0)
  12273. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12274. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 2);
  12275. ret = wc_AesCbcEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE);
  12276. #if defined(WOLFSSL_ASYNC_CRYPT)
  12277. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12278. #endif
  12279. if (ret != 0)
  12280. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12281. #ifndef HAVE_RENESAS_SYNC
  12282. if (XMEMCMP(cipher, verify2, AES_BLOCK_SIZE))
  12283. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12284. #endif
  12285. ret = wc_AesCbcEncrypt(enc, cipher + AES_BLOCK_SIZE,
  12286. msg2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  12287. #if defined(WOLFSSL_ASYNC_CRYPT)
  12288. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12289. #endif
  12290. if (ret != 0)
  12291. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12292. if (XMEMCMP(cipher + AES_BLOCK_SIZE, verify2 + AES_BLOCK_SIZE,
  12293. AES_BLOCK_SIZE))
  12294. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12295. #if defined(HAVE_AES_DECRYPT)
  12296. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_DECRYPTION);
  12297. if (ret != 0)
  12298. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12299. XMEMSET(plain, 0, AES_BLOCK_SIZE * 2);
  12300. ret = wc_AesCbcDecrypt(dec, plain, verify2, AES_BLOCK_SIZE);
  12301. #if defined(WOLFSSL_ASYNC_CRYPT)
  12302. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12303. #endif
  12304. if (ret != 0)
  12305. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12306. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE))
  12307. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12308. ret = wc_AesCbcDecrypt(dec, plain + AES_BLOCK_SIZE,
  12309. verify2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  12310. #if defined(WOLFSSL_ASYNC_CRYPT)
  12311. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12312. #endif
  12313. if (ret != 0)
  12314. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12315. if (XMEMCMP(plain + AES_BLOCK_SIZE, msg2 + AES_BLOCK_SIZE,
  12316. AES_BLOCK_SIZE)) {
  12317. WOLFSSL_MSG("wc_AesCbcDecrypt failed plain-msg2 compare");
  12318. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12319. }
  12320. #endif /* HAVE_AES_DECRYPT */
  12321. }
  12322. #endif /* WOLFSSL_AES_128 && !HAVE_RENESAS_SYNC */
  12323. #endif /* HAVE_AES_CBC */
  12324. #ifdef WOLFSSL_AES_COUNTER
  12325. ret = aesctr_test(enc, dec, cipher, plain);
  12326. if (ret != 0)
  12327. return ret;
  12328. #endif
  12329. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  12330. {
  12331. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  12332. {
  12333. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  12334. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  12335. };
  12336. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  12337. {
  12338. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  12339. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  12340. };
  12341. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  12342. {
  12343. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  12344. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  12345. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  12346. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  12347. };
  12348. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  12349. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  12350. if (ret != 0)
  12351. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12352. #if !defined(HAVE_SELFTEST) && \
  12353. (defined(WOLFSSL_LINUXKM) || \
  12354. !defined(HAVE_FIPS) || \
  12355. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  12356. ret = wc_AesEncryptDirect(enc, cipher, niPlain);
  12357. if (ret != 0)
  12358. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12359. #else
  12360. wc_AesEncryptDirect(enc, cipher, niPlain);
  12361. #endif
  12362. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  12363. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12364. #ifdef HAVE_AES_DECRYPT
  12365. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  12366. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  12367. if (ret != 0)
  12368. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12369. #if !defined(HAVE_SELFTEST) && \
  12370. (defined(WOLFSSL_LINUXKM) || \
  12371. !defined(HAVE_FIPS) || \
  12372. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  12373. ret = wc_AesDecryptDirect(dec, plain, niCipher);
  12374. if (ret != 0)
  12375. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12376. #else
  12377. wc_AesDecryptDirect(dec, plain, niCipher);
  12378. #endif
  12379. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  12380. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12381. #endif /* HAVE_AES_DECRYPT */
  12382. }
  12383. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  12384. #ifndef HAVE_RENESAS_SYNC
  12385. ret = aes_key_size_test();
  12386. if (ret != 0)
  12387. goto out;
  12388. #endif
  12389. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) && \
  12390. !defined(HAVE_RENESAS_SYNC)
  12391. ret = aes_cbc_test();
  12392. if (ret != 0)
  12393. goto out;
  12394. #endif
  12395. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12396. ret = aesecb_test();
  12397. if (ret != 0)
  12398. goto out;
  12399. #endif
  12400. out:
  12401. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  12402. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12403. if (enc) {
  12404. if (enc_inited)
  12405. wc_AesFree(enc);
  12406. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  12407. }
  12408. #else
  12409. if (enc_inited)
  12410. wc_AesFree(enc);
  12411. #endif
  12412. (void)cipher;
  12413. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  12414. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12415. if (dec) {
  12416. if (dec_inited)
  12417. wc_AesFree(dec);
  12418. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  12419. }
  12420. #else
  12421. if (dec_inited)
  12422. wc_AesFree(dec);
  12423. #endif
  12424. (void)plain;
  12425. #endif /* HAVE_AES_DECRYPT || WOLFSSL_AES_COUNTER */
  12426. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  12427. return ret;
  12428. }
  12429. #if defined(WOLFSSL_AES_CFB)
  12430. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_cfb_test(void)
  12431. {
  12432. int ret;
  12433. WOLFSSL_ENTER("aes_cfb_test");
  12434. ret = aescfb_test_0();
  12435. if (ret != 0)
  12436. return ret;
  12437. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  12438. ret = aescfb1_test();
  12439. if (ret != 0)
  12440. return ret;
  12441. ret = aescfb8_test();
  12442. if (ret != 0)
  12443. return ret;
  12444. #endif
  12445. return 0;
  12446. }
  12447. #endif
  12448. #if defined(WOLFSSL_AES_XTS)
  12449. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_xts_test(void)
  12450. {
  12451. int ret = 0;
  12452. WOLFSSL_ENTER("aes_xts_test");
  12453. #ifdef WOLFSSL_AES_128
  12454. ret = aes_xts_128_test();
  12455. if (ret != 0)
  12456. return ret;
  12457. #endif
  12458. #ifdef WOLFSSL_AES_192
  12459. ret = aes_xts_192_test();
  12460. if (ret != 0)
  12461. return ret;
  12462. #endif
  12463. #ifdef WOLFSSL_AES_256
  12464. ret = aes_xts_256_test();
  12465. if (ret != 0)
  12466. return ret;
  12467. #endif
  12468. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  12469. ret = aes_xts_sector_test();
  12470. if (ret != 0)
  12471. return ret;
  12472. #endif
  12473. #ifdef WOLFSSL_AES_128
  12474. ret = aes_xts_args_test();
  12475. if (ret != 0)
  12476. return ret;
  12477. #endif
  12478. return 0;
  12479. }
  12480. #endif
  12481. #ifdef WOLFSSL_AES_192
  12482. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes192_test(void)
  12483. {
  12484. wc_test_ret_t ret = 0;
  12485. #ifdef HAVE_AES_CBC
  12486. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12487. Aes *enc = NULL;
  12488. #else
  12489. Aes enc[1];
  12490. #endif
  12491. int enc_inited = 0;
  12492. byte cipher[AES_BLOCK_SIZE];
  12493. #ifdef HAVE_AES_DECRYPT
  12494. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12495. Aes *dec = NULL;
  12496. #else
  12497. Aes dec[1];
  12498. #endif
  12499. byte plain[AES_BLOCK_SIZE];
  12500. #endif
  12501. #ifdef HAVE_AES_DECRYPT
  12502. int dec_inited = 0;
  12503. #endif
  12504. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  12505. * Appendix F.2.3 */
  12506. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  12507. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  12508. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  12509. };
  12510. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  12511. {
  12512. 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  12513. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8
  12514. };
  12515. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  12516. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  12517. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  12518. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  12519. };
  12520. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  12521. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  12522. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  12523. };
  12524. WOLFSSL_ENTER("aes192_test");
  12525. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12526. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12527. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12528. #ifdef HAVE_AES_DECRYPT
  12529. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12530. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12531. #endif
  12532. #endif
  12533. ret = wc_AesInit(enc, HEAP_HINT, devId);
  12534. if (ret != 0)
  12535. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12536. enc_inited = 1;
  12537. #ifdef HAVE_AES_DECRYPT
  12538. ret = wc_AesInit(dec, HEAP_HINT, devId);
  12539. if (ret != 0)
  12540. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12541. dec_inited = 1;
  12542. #endif
  12543. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  12544. if (ret != 0)
  12545. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12546. #ifdef HAVE_AES_DECRYPT
  12547. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  12548. if (ret != 0)
  12549. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12550. #endif
  12551. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  12552. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  12553. #if defined(WOLFSSL_ASYNC_CRYPT)
  12554. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12555. #endif
  12556. if (ret != 0)
  12557. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12558. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  12559. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12560. #ifdef HAVE_AES_DECRYPT
  12561. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  12562. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  12563. #if defined(WOLFSSL_ASYNC_CRYPT)
  12564. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12565. #endif
  12566. if (ret != 0)
  12567. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12568. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  12569. WOLFSSL_MSG("failed wc_AesCbcDecrypt plain-msg compare");
  12570. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12571. }
  12572. #endif
  12573. out:
  12574. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12575. if (enc) {
  12576. if (enc_inited)
  12577. wc_AesFree(enc);
  12578. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  12579. }
  12580. #ifdef HAVE_AES_DECRYPT
  12581. if (dec) {
  12582. if (dec_inited)
  12583. wc_AesFree(dec);
  12584. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  12585. }
  12586. #endif
  12587. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  12588. if (enc_inited)
  12589. wc_AesFree(enc);
  12590. #ifdef HAVE_AES_DECRYPT
  12591. if (dec_inited)
  12592. wc_AesFree(dec);
  12593. #endif
  12594. #endif
  12595. #endif /* HAVE_AES_CBC */
  12596. return ret;
  12597. }
  12598. #endif /* WOLFSSL_AES_192 */
  12599. #ifdef WOLFSSL_AES_256
  12600. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes256_test(void)
  12601. {
  12602. #ifdef HAVE_AES_CBC
  12603. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12604. Aes *enc = NULL;
  12605. #else
  12606. Aes enc[1];
  12607. #endif
  12608. int enc_inited = 0;
  12609. byte cipher[AES_BLOCK_SIZE];
  12610. #ifdef HAVE_AES_DECRYPT
  12611. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12612. Aes *dec = NULL;
  12613. #else
  12614. Aes dec[1];
  12615. #endif
  12616. int dec_inited = 0;
  12617. byte plain[AES_BLOCK_SIZE];
  12618. #endif
  12619. #endif /* HAVE_AES_CBC */
  12620. wc_test_ret_t ret = 0;
  12621. #ifdef HAVE_AES_CBC
  12622. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  12623. * Appendix F.2.5 */
  12624. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  12625. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  12626. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  12627. };
  12628. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  12629. {
  12630. 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  12631. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6
  12632. };
  12633. #ifdef HAVE_RENESAS_SYNC
  12634. byte *key =
  12635. (byte*)guser_PKCbInfo.wrapped_key_aes256;
  12636. int keySz = (256/8);
  12637. #else
  12638. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  12639. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  12640. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  12641. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  12642. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  12643. };
  12644. int keySz = (int)sizeof(key);
  12645. #endif
  12646. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  12647. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  12648. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  12649. };
  12650. WOLFSSL_ENTER("aes256_test");
  12651. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12652. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12653. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12654. #ifdef HAVE_AES_DECRYPT
  12655. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12656. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12657. #endif
  12658. #endif
  12659. ret = wc_AesInit(enc, HEAP_HINT, devId);
  12660. if (ret != 0)
  12661. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12662. enc_inited = 1;
  12663. #ifdef HAVE_AES_DECRYPT
  12664. ret = wc_AesInit(dec, HEAP_HINT, devId);
  12665. if (ret != 0)
  12666. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12667. dec_inited = 1;
  12668. #endif
  12669. ret = wc_AesSetKey(enc, key, (word32)keySz, iv, AES_ENCRYPTION);
  12670. if (ret != 0)
  12671. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12672. #ifdef HAVE_AES_DECRYPT
  12673. ret = wc_AesSetKey(dec, key, (word32)keySz, iv, AES_DECRYPTION);
  12674. if (ret != 0)
  12675. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12676. #endif
  12677. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  12678. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  12679. #if defined(WOLFSSL_ASYNC_CRYPT)
  12680. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12681. #endif
  12682. if (ret != 0)
  12683. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12684. #ifdef HAVE_AES_DECRYPT
  12685. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  12686. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  12687. #if defined(WOLFSSL_ASYNC_CRYPT)
  12688. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12689. #endif
  12690. if (ret != 0)
  12691. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12692. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  12693. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12694. }
  12695. #endif
  12696. #ifndef HAVE_RENESAS_SYNC
  12697. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  12698. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12699. #endif
  12700. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  12701. ret = wc_AesSetKey(enc, key, keySz, iv, AES_ENCRYPTION);
  12702. if (ret != 0)
  12703. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12704. #ifdef HAVE_AES_DECRYPT
  12705. ret = wc_AesSetKey(dec, key, keySz, iv, AES_DECRYPTION);
  12706. if (ret != 0)
  12707. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12708. #endif
  12709. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  12710. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12711. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  12712. #if defined(WOLFSSL_ASYNC_CRYPT)
  12713. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12714. #endif
  12715. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12716. if (ret != 0)
  12717. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12718. #ifdef HAVE_AES_DECRYPT
  12719. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  12720. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12721. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  12722. #if defined(WOLFSSL_ASYNC_CRYPT)
  12723. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12724. #endif
  12725. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12726. if (ret != 0)
  12727. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12728. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  12729. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12730. }
  12731. #endif
  12732. #ifndef HAVE_RENESAS_SYNC
  12733. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  12734. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12735. #endif
  12736. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12737. ret = wc_AesSetKey(enc, key, keySz, iv, AES_ENCRYPTION);
  12738. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12739. if (ret != 0)
  12740. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12741. #ifdef HAVE_AES_DECRYPT
  12742. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12743. ret = wc_AesSetKey(dec, key, keySz, iv, AES_DECRYPTION);
  12744. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12745. if (ret != 0)
  12746. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12747. #endif
  12748. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  12749. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  12750. #if defined(WOLFSSL_ASYNC_CRYPT)
  12751. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12752. #endif
  12753. if (ret != 0)
  12754. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12755. #ifdef HAVE_AES_DECRYPT
  12756. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  12757. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  12758. #if defined(WOLFSSL_ASYNC_CRYPT)
  12759. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12760. #endif
  12761. if (ret != 0)
  12762. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12763. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  12764. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12765. }
  12766. #endif
  12767. #ifndef HAVE_RENESAS_SYNC
  12768. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  12769. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12770. #endif
  12771. #endif /* DEBUG_VECTOR_REGISTER_ACCESS && WC_C_DYNAMIC_FALLBACK */
  12772. out:
  12773. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12774. if (enc) {
  12775. if (enc_inited)
  12776. wc_AesFree(enc);
  12777. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  12778. }
  12779. #ifdef HAVE_AES_DECRYPT
  12780. if (dec) {
  12781. if (dec_inited)
  12782. wc_AesFree(dec);
  12783. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  12784. }
  12785. #endif
  12786. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  12787. if (enc_inited)
  12788. wc_AesFree(enc);
  12789. #ifdef HAVE_AES_DECRYPT
  12790. if (dec_inited)
  12791. wc_AesFree(dec);
  12792. #endif /* HAVE_AES_DECRYPT */
  12793. #endif /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  12794. #endif /* HAVE_AES_CBC */
  12795. return ret;
  12796. }
  12797. #endif /* WOLFSSL_AES_256 */
  12798. #ifdef HAVE_AESGCM
  12799. #ifdef WOLFSSL_AES_128
  12800. static wc_test_ret_t aesgcm_default_test_helper(byte* key, int keySz, byte* iv, int ivSz,
  12801. byte* plain, int plainSz, byte* cipher, int cipherSz,
  12802. byte* aad, int aadSz, byte* tag, int tagSz)
  12803. {
  12804. wc_test_ret_t ret;
  12805. int enc_inited = 0, dec_inited = 0;
  12806. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12807. Aes *enc = NULL;
  12808. Aes *dec = NULL;
  12809. #else
  12810. Aes enc[1];
  12811. Aes dec[1];
  12812. #endif
  12813. byte resultT[AES_BLOCK_SIZE];
  12814. byte resultP[AES_BLOCK_SIZE * 3];
  12815. byte resultC[AES_BLOCK_SIZE * 3];
  12816. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12817. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12818. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12819. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12820. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12821. #endif
  12822. XMEMSET(resultT, 0, sizeof(resultT));
  12823. XMEMSET(resultC, 0, sizeof(resultC));
  12824. XMEMSET(resultP, 0, sizeof(resultP));
  12825. ret = wc_AesInit(enc, HEAP_HINT, devId);
  12826. if (ret != 0)
  12827. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12828. else
  12829. enc_inited = 1;
  12830. ret = wc_AesInit(dec, HEAP_HINT, devId);
  12831. if (ret != 0)
  12832. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12833. else
  12834. dec_inited = 1;
  12835. ret = wc_AesGcmSetKey(enc, key, (word32)keySz);
  12836. if (ret != 0)
  12837. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12838. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  12839. ret = wc_AesGcmEncrypt(enc, resultC, plain, (word32)plainSz, iv, ivSz,
  12840. resultT, (word32)tagSz, aad, aadSz);
  12841. #if defined(WOLFSSL_ASYNC_CRYPT)
  12842. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12843. #endif
  12844. if (ret != 0)
  12845. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12846. if (cipher != NULL) {
  12847. if (XMEMCMP(cipher, resultC, cipherSz))
  12848. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12849. }
  12850. if (XMEMCMP(tag, resultT, tagSz))
  12851. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12852. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  12853. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12854. ret = wc_AesGcmEncrypt(enc, resultC, plain, plainSz, iv, ivSz,
  12855. resultT, tagSz, aad, aadSz);
  12856. #if defined(WOLFSSL_ASYNC_CRYPT)
  12857. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12858. #endif
  12859. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12860. if (ret != 0)
  12861. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12862. if (cipher != NULL) {
  12863. if (XMEMCMP(cipher, resultC, cipherSz))
  12864. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12865. }
  12866. if (XMEMCMP(tag, resultT, tagSz))
  12867. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12868. #endif
  12869. #ifdef HAVE_AES_DECRYPT
  12870. ret = wc_AesGcmSetKey(dec, key, (word32)keySz);
  12871. if (ret != 0)
  12872. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12873. ret = wc_AesGcmDecrypt(dec, resultP, resultC, (word32)cipherSz,
  12874. iv, (word32)ivSz, resultT, tagSz, aad, aadSz);
  12875. #if defined(WOLFSSL_ASYNC_CRYPT)
  12876. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12877. #endif
  12878. if (ret != 0)
  12879. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12880. if (plain != NULL) {
  12881. if (XMEMCMP(plain, resultP, plainSz))
  12882. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12883. }
  12884. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  12885. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12886. ret = wc_AesGcmDecrypt(dec, resultP, resultC, cipherSz,
  12887. iv, ivSz, resultT, tagSz, aad, aadSz);
  12888. #if defined(WOLFSSL_ASYNC_CRYPT)
  12889. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12890. #endif
  12891. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12892. if (ret != 0)
  12893. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12894. if (plain != NULL) {
  12895. if (XMEMCMP(plain, resultP, plainSz))
  12896. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12897. }
  12898. #endif
  12899. #endif /* HAVE_AES_DECRYPT */
  12900. ret = 0;
  12901. out:
  12902. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12903. if (enc) {
  12904. if (enc_inited)
  12905. wc_AesFree(enc);
  12906. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  12907. }
  12908. if (dec) {
  12909. if (dec_inited)
  12910. wc_AesFree(dec);
  12911. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  12912. }
  12913. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  12914. if (enc_inited)
  12915. wc_AesFree(enc);
  12916. if (dec_inited)
  12917. wc_AesFree(dec);
  12918. #endif
  12919. return ret;
  12920. }
  12921. #endif
  12922. /* tests that only use 12 byte IV and 16 or less byte AAD
  12923. * test vectors are from NIST SP 800-38D
  12924. * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/CAVP-TESTING-BLOCK-CIPHER-MODES*/
  12925. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_default_test(void)
  12926. {
  12927. #ifdef WOLFSSL_AES_128
  12928. byte key1[] = {
  12929. 0x29, 0x8e, 0xfa, 0x1c, 0xcf, 0x29, 0xcf, 0x62,
  12930. 0xae, 0x68, 0x24, 0xbf, 0xc1, 0x95, 0x57, 0xfc
  12931. };
  12932. byte iv1[] = {
  12933. 0x6f, 0x58, 0xa9, 0x3f, 0xe1, 0xd2, 0x07, 0xfa,
  12934. 0xe4, 0xed, 0x2f, 0x6d
  12935. };
  12936. ALIGN64 byte plain1[] = {
  12937. 0xcc, 0x38, 0xbc, 0xcd, 0x6b, 0xc5, 0x36, 0xad,
  12938. 0x91, 0x9b, 0x13, 0x95, 0xf5, 0xd6, 0x38, 0x01,
  12939. 0xf9, 0x9f, 0x80, 0x68, 0xd6, 0x5c, 0xa5, 0xac,
  12940. 0x63, 0x87, 0x2d, 0xaf, 0x16, 0xb9, 0x39, 0x01
  12941. };
  12942. byte aad1[] = {
  12943. 0x02, 0x1f, 0xaf, 0xd2, 0x38, 0x46, 0x39, 0x73,
  12944. 0xff, 0xe8, 0x02, 0x56, 0xe5, 0xb1, 0xc6, 0xb1
  12945. };
  12946. ALIGN64 byte cipher1[] = {
  12947. 0xdf, 0xce, 0x4e, 0x9c, 0xd2, 0x91, 0x10, 0x3d,
  12948. 0x7f, 0xe4, 0xe6, 0x33, 0x51, 0xd9, 0xe7, 0x9d,
  12949. 0x3d, 0xfd, 0x39, 0x1e, 0x32, 0x67, 0x10, 0x46,
  12950. 0x58, 0x21, 0x2d, 0xa9, 0x65, 0x21, 0xb7, 0xdb
  12951. };
  12952. byte tag1[] = {
  12953. 0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
  12954. 0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
  12955. };
  12956. byte key2[] = {
  12957. 0x01, 0x6d, 0xbb, 0x38, 0xda, 0xa7, 0x6d, 0xfe,
  12958. 0x7d, 0xa3, 0x84, 0xeb, 0xf1, 0x24, 0x03, 0x64
  12959. };
  12960. byte iv2[] = {
  12961. 0x07, 0x93, 0xef, 0x3a, 0xda, 0x78, 0x2f, 0x78,
  12962. 0xc9, 0x8a, 0xff, 0xe3
  12963. };
  12964. ALIGN64 byte plain2[] = {
  12965. 0x4b, 0x34, 0xa9, 0xec, 0x57, 0x63, 0x52, 0x4b,
  12966. 0x19, 0x1d, 0x56, 0x16, 0xc5, 0x47, 0xf6, 0xb7
  12967. };
  12968. ALIGN64 byte cipher2[] = {
  12969. 0x60, 0x9a, 0xa3, 0xf4, 0x54, 0x1b, 0xc0, 0xfe,
  12970. 0x99, 0x31, 0xda, 0xad, 0x2e, 0xe1, 0x5d, 0x0c
  12971. };
  12972. byte tag2[] = {
  12973. 0x33, 0xaf, 0xec, 0x59, 0xc4, 0x5b, 0xaf, 0x68,
  12974. 0x9a, 0x5e, 0x1b, 0x13, 0xae, 0x42, 0x36, 0x19
  12975. };
  12976. byte key3[] = {
  12977. 0xb0, 0x1e, 0x45, 0xcc, 0x30, 0x88, 0xaa, 0xba,
  12978. 0x9f, 0xa4, 0x3d, 0x81, 0xd4, 0x81, 0x82, 0x3f
  12979. };
  12980. byte iv3[] = {
  12981. 0x5a, 0x2c, 0x4a, 0x66, 0x46, 0x87, 0x13, 0x45,
  12982. 0x6a, 0x4b, 0xd5, 0xe1
  12983. };
  12984. byte tag3[] = {
  12985. 0x01, 0x42, 0x80, 0xf9, 0x44, 0xf5, 0x3c, 0x68,
  12986. 0x11, 0x64, 0xb2, 0xff
  12987. };
  12988. wc_test_ret_t ret;
  12989. WOLFSSL_ENTER("aesgcm_default_test");
  12990. ret = aesgcm_default_test_helper(key1, sizeof(key1), iv1, sizeof(iv1),
  12991. plain1, sizeof(plain1), cipher1, sizeof(cipher1),
  12992. aad1, sizeof(aad1), tag1, sizeof(tag1));
  12993. if (ret != 0) {
  12994. return ret;
  12995. }
  12996. ret = aesgcm_default_test_helper(key2, sizeof(key2), iv2, sizeof(iv2),
  12997. plain2, sizeof(plain2), cipher2, sizeof(cipher2),
  12998. NULL, 0, tag2, sizeof(tag2));
  12999. if (ret != 0) {
  13000. return ret;
  13001. }
  13002. ret = aesgcm_default_test_helper(key3, sizeof(key3), iv3, sizeof(iv3),
  13003. NULL, 0, NULL, 0,
  13004. NULL, 0, tag3, sizeof(tag3));
  13005. if (ret != 0) {
  13006. return ret;
  13007. }
  13008. #endif
  13009. return 0;
  13010. }
  13011. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_test(void)
  13012. {
  13013. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13014. Aes *enc = NULL;
  13015. Aes *dec = NULL;
  13016. #else
  13017. Aes enc[1];
  13018. Aes dec[1];
  13019. #endif
  13020. /*
  13021. * This is Test Case 16 from the document Galois/
  13022. * Counter Mode of Operation (GCM) by McGrew and
  13023. * Viega.
  13024. */
  13025. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  13026. {
  13027. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  13028. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  13029. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  13030. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  13031. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  13032. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  13033. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  13034. 0xba, 0x63, 0x7b, 0x39
  13035. };
  13036. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  13037. {
  13038. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  13039. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  13040. 0xab, 0xad, 0xda, 0xd2
  13041. };
  13042. #ifdef WOLFSSL_AES_256
  13043. #ifdef HAVE_RENESAS_SYNC
  13044. const byte *k1 = (byte*)guser_PKCbInfo.wrapped_key_aes256;
  13045. int k1Sz = (int)(256/8);
  13046. #else
  13047. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  13048. {
  13049. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  13050. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  13051. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  13052. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  13053. };
  13054. int k1Sz = (int)sizeof(k1);
  13055. #endif
  13056. #endif /* WOLFSSL_AES_256 */
  13057. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  13058. {
  13059. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  13060. 0xde, 0xca, 0xf8, 0x88
  13061. };
  13062. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  13063. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  13064. {
  13065. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  13066. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  13067. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  13068. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  13069. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  13070. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  13071. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  13072. 0xbc, 0xc9, 0xf6, 0x62
  13073. };
  13074. #endif /* WOLFSSL_AES_256 || WOLFSSL_AES_192 */
  13075. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  13076. {
  13077. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  13078. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  13079. };
  13080. /* FIPS, QAT and PIC32MZ HW Crypto only support 12-byte IV */
  13081. #if !defined(HAVE_FIPS) && \
  13082. !defined(WOLFSSL_PIC32MZ_CRYPT) && \
  13083. !defined(FREESCALE_LTC) && !defined(FREESCALE_MMCAU) && \
  13084. !defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  13085. !defined(WOLFSSL_SILABS_SE_ACCEL) && !defined(WOLFSSL_KCAPI_AES) && \
  13086. !(defined(WOLF_CRYPTO_CB) && \
  13087. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  13088. #define ENABLE_NON_12BYTE_IV_TEST
  13089. #ifdef WOLFSSL_AES_192
  13090. /* Test Case 12, uses same plaintext and AAD data. */
  13091. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  13092. {
  13093. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  13094. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  13095. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c
  13096. };
  13097. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  13098. {
  13099. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  13100. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  13101. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  13102. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  13103. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  13104. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  13105. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  13106. 0xa6, 0x37, 0xb3, 0x9b
  13107. };
  13108. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  13109. {
  13110. 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  13111. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  13112. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  13113. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  13114. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  13115. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  13116. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  13117. 0xe9, 0xb7, 0x37, 0x3b
  13118. };
  13119. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  13120. {
  13121. 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  13122. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9
  13123. };
  13124. #endif /* WOLFSSL_AES_192 */
  13125. #ifdef WOLFSSL_AES_128
  13126. /* The following is an interesting test case from the example
  13127. * FIPS test vectors for AES-GCM. IVlen = 1 byte */
  13128. WOLFSSL_SMALL_STACK_STATIC const byte p3[] =
  13129. {
  13130. 0x57, 0xce, 0x45, 0x1f, 0xa5, 0xe2, 0x35, 0xa5,
  13131. 0x8e, 0x1a, 0xa2, 0x3b, 0x77, 0xcb, 0xaf, 0xe2
  13132. };
  13133. #ifdef HAVE_RENESAS_SYNC
  13134. const byte *k3 =
  13135. (byte*)guser_PKCbInfo.wrapped_key_aes128;
  13136. int k3Sz = (int)(128/8);
  13137. #else
  13138. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  13139. {
  13140. 0xbb, 0x01, 0xd7, 0x03, 0x81, 0x1c, 0x10, 0x1a,
  13141. 0x35, 0xe0, 0xff, 0xd2, 0x91, 0xba, 0xf2, 0x4b
  13142. };
  13143. int k3Sz = (int)sizeof(k3);
  13144. #endif
  13145. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  13146. {
  13147. 0xca
  13148. };
  13149. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  13150. {
  13151. 0x6b, 0x5f, 0xb3, 0x9d, 0xc1, 0xc5, 0x7a, 0x4f,
  13152. 0xf3, 0x51, 0x4d, 0xc2, 0xd5, 0xf0, 0xd0, 0x07
  13153. };
  13154. WOLFSSL_SMALL_STACK_STATIC const byte a3[] =
  13155. {
  13156. 0x40, 0xfc, 0xdc, 0xd7, 0x4a, 0xd7, 0x8b, 0xf1,
  13157. 0x3e, 0x7c, 0x60, 0x55, 0x50, 0x51, 0xdd, 0x54
  13158. };
  13159. WOLFSSL_SMALL_STACK_STATIC const byte t3[] =
  13160. {
  13161. 0x06, 0x90, 0xed, 0x01, 0x34, 0xdd, 0xc6, 0x95,
  13162. 0x31, 0x2e, 0x2a, 0xf9, 0x57, 0x7a, 0x1e, 0xa6
  13163. };
  13164. #endif /* WOLFSSL_AES_128 */
  13165. #ifdef WOLFSSL_AES_256
  13166. int ivlen;
  13167. #endif
  13168. #endif
  13169. byte resultT[sizeof(t1) + AES_BLOCK_SIZE];
  13170. byte resultP[sizeof(p) + AES_BLOCK_SIZE];
  13171. byte resultC[sizeof(p) + AES_BLOCK_SIZE];
  13172. wc_test_ret_t ret = 0;
  13173. int alen;
  13174. int plen;
  13175. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  13176. byte buf[sizeof(p) + AES_BLOCK_SIZE];
  13177. byte bufA[sizeof(a) + 1];
  13178. byte *large_aad = (byte*)XMALLOC((size_t)1024 + 16, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13179. #endif
  13180. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM)
  13181. #if !defined(BENCH_AESGCM_LARGE)
  13182. #define BENCH_AESGCM_LARGE 1024
  13183. #endif
  13184. #ifndef WOLFSSL_NO_MALLOC
  13185. byte *large_input = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13186. byte *large_output = (byte *)XMALLOC(BENCH_AESGCM_LARGE + AES_BLOCK_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13187. byte *large_outdec = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13188. (void)alen;
  13189. (void)plen;
  13190. if ((! large_input) || (! large_output) || (! large_outdec))
  13191. ERROR_OUT(MEMORY_E, out);
  13192. #else
  13193. byte large_input[BENCH_AESGCM_LARGE];
  13194. byte large_output[BENCH_AESGCM_LARGE + AES_BLOCK_SIZE];
  13195. byte large_outdec[BENCH_AESGCM_LARGE];
  13196. #endif
  13197. XMEMSET(large_input, 0, BENCH_AESGCM_LARGE);
  13198. XMEMSET(large_output, 0, BENCH_AESGCM_LARGE + AES_BLOCK_SIZE);
  13199. XMEMSET(large_outdec, 0, BENCH_AESGCM_LARGE);
  13200. #endif
  13201. WOLFSSL_ENTER("aesgcm_test");
  13202. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13203. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  13204. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  13205. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  13206. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  13207. #endif
  13208. XMEMSET(resultT, 0, sizeof(resultT));
  13209. XMEMSET(resultC, 0, sizeof(resultC));
  13210. XMEMSET(resultP, 0, sizeof(resultP));
  13211. ret = wc_AesInit(enc, HEAP_HINT, devId);
  13212. if (ret != 0)
  13213. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13214. ret = wc_AesInit(dec, HEAP_HINT, devId);
  13215. if (ret != 0)
  13216. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13217. #ifdef WOLFSSL_AES_256
  13218. ret = wc_AesGcmSetKey(enc, k1, (word32)k1Sz);
  13219. if (ret != 0)
  13220. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13221. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13222. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  13223. resultT, sizeof(t1), a, sizeof(a));
  13224. #if defined(WOLFSSL_ASYNC_CRYPT)
  13225. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13226. #endif
  13227. if (ret != 0)
  13228. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13229. #ifndef HAVE_RENESAS_SYNC
  13230. if (XMEMCMP(c1, resultC, sizeof(c1)))
  13231. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13232. if (XMEMCMP(t1, resultT, sizeof(t1)))
  13233. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13234. #endif
  13235. #ifdef HAVE_AES_DECRYPT
  13236. ret = wc_AesGcmSetKey(dec, k1, (word32)k1Sz);
  13237. if (ret != 0)
  13238. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13239. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1),
  13240. iv1, sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  13241. #if defined(WOLFSSL_ASYNC_CRYPT)
  13242. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13243. #endif
  13244. if (ret != 0)
  13245. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13246. if (XMEMCMP(p, resultP, sizeof(p)))
  13247. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13248. #endif /* HAVE_AES_DECRYPT */
  13249. /* Large buffer test */
  13250. #ifdef BENCH_AESGCM_LARGE
  13251. /* setup test buffer */
  13252. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  13253. large_input[alen] = (byte)alen;
  13254. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13255. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  13256. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  13257. resultT, sizeof(t1), a, sizeof(a));
  13258. #if defined(WOLFSSL_ASYNC_CRYPT)
  13259. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13260. #endif
  13261. if (ret != 0)
  13262. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13263. #ifdef HAVE_AES_DECRYPT
  13264. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  13265. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  13266. sizeof(t1), a, sizeof(a));
  13267. #if defined(WOLFSSL_ASYNC_CRYPT)
  13268. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13269. #endif
  13270. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  13271. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13272. if (ret != 0)
  13273. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13274. #endif /* HAVE_AES_DECRYPT */
  13275. #endif /* BENCH_AESGCM_LARGE */
  13276. #if defined(ENABLE_NON_12BYTE_IV_TEST) && defined(WOLFSSL_AES_256)
  13277. /* Variable IV length test */
  13278. for (ivlen=1; ivlen<k1Sz; ivlen++) {
  13279. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13280. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), k1,
  13281. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  13282. #if defined(WOLFSSL_ASYNC_CRYPT)
  13283. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13284. #endif
  13285. if (ret != 0)
  13286. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13287. #ifdef HAVE_AES_DECRYPT
  13288. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), k1,
  13289. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  13290. #if defined(WOLFSSL_ASYNC_CRYPT)
  13291. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13292. #endif
  13293. if (ret != 0)
  13294. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13295. #endif /* HAVE_AES_DECRYPT */
  13296. }
  13297. #endif
  13298. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  13299. /* Variable authenticated data length test */
  13300. for (alen=0; alen<(int)sizeof(p); alen++) {
  13301. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13302. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  13303. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  13304. #if defined(WOLFSSL_ASYNC_CRYPT)
  13305. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13306. #endif
  13307. if (ret != 0)
  13308. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13309. #ifdef HAVE_AES_DECRYPT
  13310. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  13311. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  13312. #if defined(WOLFSSL_ASYNC_CRYPT)
  13313. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13314. #endif
  13315. if (ret != 0)
  13316. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13317. #endif /* HAVE_AES_DECRYPT */
  13318. }
  13319. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  13320. if (! large_aad)
  13321. ERROR_OUT(MEMORY_E, out);
  13322. XMEMSET(large_aad, 0, 1024+16);
  13323. /* Variable authenticated data length test */
  13324. for (alen=0; alen<=1024; alen+=16) {
  13325. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13326. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  13327. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  13328. if (ret != 0)
  13329. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13330. #ifdef HAVE_AES_DECRYPT
  13331. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  13332. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  13333. if (ret != 0)
  13334. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13335. #endif /* HAVE_AES_DECRYPT */
  13336. }
  13337. /* Test unaligned memory of all potential arguments */
  13338. ret = wc_AesGcmSetKey(enc, k1, k1Sz);
  13339. if (ret != 0)
  13340. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13341. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13342. XMEMCPY(&buf[1], p, sizeof(p));
  13343. XMEMCPY(&bufA[1], a, sizeof(a));
  13344. ret = wc_AesGcmEncrypt(enc, &resultC[1], &buf[1], sizeof(p), iv1, sizeof(iv1),
  13345. &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(c1, &resultC[1], sizeof(c1)))
  13349. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13350. if (XMEMCMP(t1, &resultT[1], sizeof(t1)))
  13351. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13352. #ifdef HAVE_AES_DECRYPT
  13353. ret = wc_AesGcmSetKey(dec, k1, k1Sz);
  13354. if (ret != 0)
  13355. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13356. ret = wc_AesGcmDecrypt(dec, &resultP[1], &resultC[1], sizeof(c1),
  13357. iv1, sizeof(iv1), &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  13358. if (ret != 0)
  13359. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13360. if (XMEMCMP(p, &resultP[1], sizeof(p)))
  13361. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13362. #endif /* HAVE_AES_DECRYPT */
  13363. #endif /* Xilinx Versal */
  13364. #endif
  13365. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  13366. #ifdef BENCH_AESGCM_LARGE
  13367. /* Variable plain text length test */
  13368. for (plen=1; plen<BENCH_AESGCM_LARGE; plen++) {
  13369. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13370. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  13371. (word32)plen, iv1, sizeof(iv1), resultT,
  13372. sizeof(t1), a, sizeof(a));
  13373. #if defined(WOLFSSL_ASYNC_CRYPT)
  13374. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13375. #endif
  13376. if (ret != 0)
  13377. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13378. #ifdef HAVE_AES_DECRYPT
  13379. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  13380. (word32)plen, iv1, sizeof(iv1), resultT,
  13381. sizeof(t1), a, sizeof(a));
  13382. #if defined(WOLFSSL_ASYNC_CRYPT)
  13383. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13384. #endif
  13385. if (ret != 0)
  13386. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13387. #endif /* HAVE_AES_DECRYPT */
  13388. }
  13389. #else /* BENCH_AESGCM_LARGE */
  13390. /* Variable plain text length test */
  13391. for (plen=1; plen<(int)sizeof(p); plen++) {
  13392. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13393. ret = wc_AesGcmEncrypt(enc, resultC, p, (word32)plen, iv1,
  13394. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  13395. #if defined(WOLFSSL_ASYNC_CRYPT)
  13396. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13397. #endif
  13398. if (ret != 0)
  13399. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13400. #ifdef HAVE_AES_DECRYPT
  13401. ret = wc_AesGcmDecrypt(dec, resultP, resultC, (word32)plen, iv1,
  13402. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  13403. #if defined(WOLFSSL_ASYNC_CRYPT)
  13404. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13405. #endif
  13406. if (ret != 0)
  13407. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13408. #endif /* HAVE_AES_DECRYPT */
  13409. }
  13410. #endif /* BENCH_AESGCM_LARGE */
  13411. #endif
  13412. #endif /* WOLFSSL_AES_256 */
  13413. /* test with IV != 12 bytes */
  13414. #ifdef ENABLE_NON_12BYTE_IV_TEST
  13415. XMEMSET(resultT, 0, sizeof(resultT));
  13416. XMEMSET(resultC, 0, sizeof(resultC));
  13417. XMEMSET(resultP, 0, sizeof(resultP));
  13418. #ifdef WOLFSSL_AES_192
  13419. wc_AesGcmSetKey(enc, k2, sizeof(k2));
  13420. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13421. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv2, sizeof(iv2),
  13422. 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(c2, resultC, sizeof(c2)))
  13429. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13430. if (XMEMCMP(t2, resultT, sizeof(t1)))
  13431. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13432. #ifdef HAVE_AES_DECRYPT
  13433. ret = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c1),
  13434. iv2, sizeof(iv2), resultT, sizeof(t1), a, sizeof(a));
  13435. #if defined(WOLFSSL_ASYNC_CRYPT)
  13436. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13437. #endif
  13438. if (ret != 0)
  13439. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13440. if (XMEMCMP(p, resultP, sizeof(p)))
  13441. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13442. #endif /* HAVE_AES_DECRYPT */
  13443. /* Large buffer test */
  13444. #ifdef BENCH_AESGCM_LARGE
  13445. wc_AesGcmSetKey(enc, k2, (word32)sizeof(k2));
  13446. wc_AesGcmSetKey(dec, k2, (word32)sizeof(k2));
  13447. /* setup test buffer */
  13448. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  13449. large_input[alen] = (byte)alen;
  13450. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13451. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  13452. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  13453. resultT, sizeof(t1), a, sizeof(a));
  13454. #if defined(WOLFSSL_ASYNC_CRYPT)
  13455. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13456. #endif
  13457. if (ret != 0)
  13458. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13459. #ifdef HAVE_AES_DECRYPT
  13460. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  13461. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  13462. sizeof(t1), a, sizeof(a));
  13463. #if defined(WOLFSSL_ASYNC_CRYPT)
  13464. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13465. #endif
  13466. if (ret != 0)
  13467. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13468. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  13469. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13470. #endif /* HAVE_AES_DECRYPT */
  13471. #endif /* BENCH_AESGCM_LARGE */
  13472. XMEMSET(resultT, 0, sizeof(resultT));
  13473. XMEMSET(resultC, 0, sizeof(resultC));
  13474. XMEMSET(resultP, 0, sizeof(resultP));
  13475. #endif /* WOLFSSL_AES_192 */
  13476. #ifdef WOLFSSL_AES_128
  13477. wc_AesGcmSetKey(enc, k3, (word32)k3Sz);
  13478. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13479. ret = wc_AesGcmEncrypt(enc, resultC, p3, sizeof(p3), iv3, sizeof(iv3),
  13480. resultT, sizeof(t3), a3, sizeof(a3));
  13481. #if defined(WOLFSSL_ASYNC_CRYPT)
  13482. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13483. #endif
  13484. if (ret != 0)
  13485. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13486. #ifndef HAVE_RENESAS_SYNC
  13487. if (XMEMCMP(c3, resultC, sizeof(c3)))
  13488. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13489. if (XMEMCMP(t3, resultT, sizeof(t3)))
  13490. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13491. #endif
  13492. #ifdef HAVE_AES_DECRYPT
  13493. ret = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c3),
  13494. iv3, sizeof(iv3), resultT, sizeof(t3), a3, sizeof(a3));
  13495. #if defined(WOLFSSL_ASYNC_CRYPT)
  13496. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13497. #endif
  13498. if (ret != 0)
  13499. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13500. if (XMEMCMP(p3, resultP, sizeof(p3)))
  13501. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13502. #endif /* HAVE_AES_DECRYPT */
  13503. /* Large buffer test */
  13504. #ifdef BENCH_AESGCM_LARGE
  13505. wc_AesGcmSetKey(enc, k3, (word32)k3Sz);
  13506. wc_AesGcmSetKey(dec, k3, (word32)k3Sz);
  13507. /* setup test buffer */
  13508. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  13509. large_input[alen] = (byte)alen;
  13510. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13511. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  13512. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  13513. resultT, sizeof(t1), a, sizeof(a));
  13514. #if defined(WOLFSSL_ASYNC_CRYPT)
  13515. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13516. #endif
  13517. if (ret != 0)
  13518. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13519. #ifdef HAVE_AES_DECRYPT
  13520. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  13521. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  13522. sizeof(t1), a, sizeof(a));
  13523. #if defined(WOLFSSL_ASYNC_CRYPT)
  13524. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13525. #endif
  13526. if (ret != 0)
  13527. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13528. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  13529. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13530. #endif /* HAVE_AES_DECRYPT */
  13531. #endif /* BENCH_AESGCM_LARGE */
  13532. #endif /* WOLFSSL_AES_128 */
  13533. #endif /* ENABLE_NON_12BYTE_IV_TEST */
  13534. #if defined(WOLFSSL_AES_256) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  13535. !defined(WOLFSSL_XILINX_CRYPT) && \
  13536. !(defined(WOLF_CRYPTO_CB) && \
  13537. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC))
  13538. XMEMSET(resultT, 0, sizeof(resultT));
  13539. XMEMSET(resultC, 0, sizeof(resultC));
  13540. XMEMSET(resultP, 0, sizeof(resultP));
  13541. wc_AesGcmSetKey(enc, k1, (word32)k1Sz);
  13542. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13543. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  13544. resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  13545. #if defined(WOLFSSL_ASYNC_CRYPT)
  13546. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13547. #endif
  13548. if (ret != 0)
  13549. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13550. #ifndef HAVE_RENESAS_SYNC
  13551. if (XMEMCMP(c1, resultC, sizeof(c1)))
  13552. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13553. if (XMEMCMP(t1, resultT + 1, sizeof(t1) - 1))
  13554. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13555. #endif
  13556. #ifdef HAVE_AES_DECRYPT
  13557. ret = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(p),
  13558. iv1, sizeof(iv1), resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  13559. #if defined(WOLFSSL_ASYNC_CRYPT)
  13560. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13561. #endif
  13562. if (ret != 0)
  13563. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13564. if (XMEMCMP(p, resultP, sizeof(p)))
  13565. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13566. #endif /* HAVE_AES_DECRYPT */
  13567. #endif /* WOLFSSL_AES_256 */
  13568. #if !defined(HAVE_FIPS) || \
  13569. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  13570. /* Test encrypt with internally generated IV */
  13571. #if defined(WOLFSSL_AES_256) && !(defined(WC_NO_RNG) || defined(HAVE_SELFTEST)) \
  13572. && !(defined(WOLF_CRYPTO_CB) && defined(HAVE_CAVIUM_OCTEON_SYNC))
  13573. {
  13574. WC_RNG rng;
  13575. byte randIV[12];
  13576. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  13577. if (ret != 0)
  13578. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13579. XMEMSET(randIV, 0, sizeof(randIV));
  13580. XMEMSET(resultT, 0, sizeof(resultT));
  13581. XMEMSET(resultC, 0, sizeof(resultC));
  13582. XMEMSET(resultP, 0, sizeof(resultP));
  13583. wc_AesGcmSetKey(enc, k1, (word32)k1Sz);
  13584. ret = wc_AesGcmSetIV(enc, sizeof(randIV), NULL, 0, &rng);
  13585. if (ret != 0)
  13586. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13587. ret = wc_AesGcmEncrypt_ex(enc,
  13588. resultC, p, sizeof(p),
  13589. randIV, sizeof(randIV),
  13590. resultT, sizeof(t1),
  13591. a, sizeof(a));
  13592. #if defined(WOLFSSL_ASYNC_CRYPT)
  13593. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13594. #endif
  13595. if (ret != 0)
  13596. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13597. /* Check the IV has been set. */
  13598. {
  13599. word32 i, ivSum = 0;
  13600. for (i = 0; i < sizeof(randIV); i++)
  13601. ivSum += randIV[i];
  13602. if (ivSum == 0)
  13603. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13604. }
  13605. #ifdef HAVE_AES_DECRYPT
  13606. wc_AesGcmSetKey(dec, k1, (word32)k1Sz);
  13607. ret = wc_AesGcmSetIV(dec, sizeof(randIV), NULL, 0, &rng);
  13608. if (ret != 0)
  13609. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13610. ret = wc_AesGcmDecrypt(dec,
  13611. resultP, resultC, sizeof(c1),
  13612. randIV, sizeof(randIV),
  13613. resultT, sizeof(t1),
  13614. a, sizeof(a));
  13615. #if defined(WOLFSSL_ASYNC_CRYPT)
  13616. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13617. #endif
  13618. if (ret != 0)
  13619. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13620. if (XMEMCMP(p, resultP, sizeof(p)))
  13621. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13622. #endif /* HAVE_AES_DECRYPT */
  13623. wc_FreeRng(&rng);
  13624. }
  13625. #endif /* WOLFSSL_AES_256 && !(WC_NO_RNG || HAVE_SELFTEST) */
  13626. #endif /* HAVE_FIPS_VERSION >= 2 */
  13627. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  13628. #ifdef WOLFSSL_AES_256
  13629. #ifdef WOLFSSL_AESGCM_STREAM
  13630. ret = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  13631. if (ret != 0)
  13632. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13633. ret = wc_AesGcmEncryptUpdate(enc, resultC, p, sizeof(p), a, sizeof(a));
  13634. if (ret != 0)
  13635. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13636. ret = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  13637. if (ret != 0)
  13638. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13639. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  13640. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13641. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  13642. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13643. #ifdef HAVE_AES_DECRYPT
  13644. ret = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  13645. if (ret != 0)
  13646. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13647. ret = wc_AesGcmDecryptUpdate(enc, resultP, c1, sizeof(c1), a, sizeof(a));
  13648. if (ret != 0)
  13649. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13650. ret = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  13651. if (ret != 0)
  13652. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13653. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  13654. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13655. #endif
  13656. /* alen is the size to pass in with each update. */
  13657. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  13658. ret = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  13659. if (ret != 0)
  13660. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13661. /* plen is the offset into AAD to update with. */
  13662. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  13663. int len = sizeof(a) - plen;
  13664. if (len > alen) len = alen;
  13665. ret = wc_AesGcmEncryptUpdate(enc, NULL, NULL, 0, a + plen, (word32)len);
  13666. if (ret != 0)
  13667. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13668. }
  13669. /* plen is the offset into plaintext to update with. */
  13670. for (plen = 0; plen < (int)sizeof(p); plen += alen) {
  13671. int len = sizeof(p) - plen;
  13672. if (len > alen) len = alen;
  13673. ret = wc_AesGcmEncryptUpdate(enc, resultC + plen, p + plen, (word32)len,
  13674. NULL, 0);
  13675. if (ret != 0)
  13676. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13677. }
  13678. ret = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  13679. if (ret != 0)
  13680. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13681. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  13682. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13683. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  13684. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13685. }
  13686. #ifdef HAVE_AES_DECRYPT
  13687. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  13688. ret = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  13689. if (ret != 0)
  13690. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13691. /* plen is the offset into AAD to update with. */
  13692. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  13693. int len = sizeof(a) - plen;
  13694. if (len > alen) len = alen;
  13695. ret = wc_AesGcmDecryptUpdate(enc, NULL, NULL, 0, a + plen, (word32)len);
  13696. if (ret != 0)
  13697. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13698. }
  13699. /* plen is the offset into cipher text to update with. */
  13700. for (plen = 0; plen < (int)sizeof(c1); plen += alen) {
  13701. int len = sizeof(c1) - plen;
  13702. if (len > alen) len = alen;
  13703. ret = wc_AesGcmDecryptUpdate(enc, resultP + plen, c1 + plen, (word32)len,
  13704. NULL, 0);
  13705. if (ret != 0)
  13706. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13707. }
  13708. ret = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  13709. if (ret != 0)
  13710. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13711. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  13712. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13713. }
  13714. #endif /* HAVE_AES_DECRYPT */
  13715. #ifdef BENCH_AESGCM_LARGE
  13716. /* setup test buffer */
  13717. ret = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  13718. if (ret != 0)
  13719. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13720. ret = wc_AesGcmEncryptUpdate(enc, large_output, large_input,
  13721. BENCH_AESGCM_LARGE, a, sizeof(a));
  13722. if (ret != 0)
  13723. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13724. ret = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  13725. if (ret != 0)
  13726. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13727. #ifdef HAVE_AES_DECRYPT
  13728. ret = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  13729. if (ret != 0)
  13730. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13731. ret = wc_AesGcmDecryptUpdate(enc, large_outdec, large_output,
  13732. BENCH_AESGCM_LARGE, a, sizeof(a));
  13733. if (ret != 0)
  13734. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13735. ret = wc_AesGcmDecryptFinal(enc, resultT, sizeof(t1));
  13736. if (ret != 0)
  13737. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13738. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  13739. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13740. #endif /* HAVE_AES_DECRYPT */
  13741. #endif /* BENCH_AESGCM_LARGE */
  13742. #endif /* WOLFSSL_AESGCM_STREAM */
  13743. #endif /* WOLFSSL_AES_256 */
  13744. #endif /* !WOLFSSL_AFALG_XILINX_AES && !WOLFSSL_XILINX_CRYPT */
  13745. wc_AesFree(enc);
  13746. wc_AesFree(dec);
  13747. ret = 0;
  13748. out:
  13749. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  13750. !defined(WOLFSSL_NO_MALLOC)
  13751. if (large_input)
  13752. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13753. if (large_output)
  13754. XFREE(large_output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13755. if (large_outdec)
  13756. XFREE(large_outdec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13757. #endif
  13758. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13759. if (enc)
  13760. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  13761. if (dec)
  13762. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  13763. #endif
  13764. return ret;
  13765. }
  13766. #ifdef WOLFSSL_AES_128
  13767. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t gmac_test(void)
  13768. {
  13769. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13770. Gmac *gmac;
  13771. #else
  13772. Gmac gmac[1];
  13773. #endif
  13774. wc_test_ret_t ret;
  13775. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  13776. {
  13777. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  13778. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  13779. };
  13780. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  13781. {
  13782. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  13783. 0xe2, 0x8c, 0x8f, 0x16
  13784. };
  13785. WOLFSSL_SMALL_STACK_STATIC const byte a1[] =
  13786. {
  13787. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  13788. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  13789. };
  13790. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  13791. {
  13792. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  13793. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  13794. };
  13795. #if (!defined(HAVE_FIPS) || \
  13796. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  13797. /* FIPS builds only allow 16-byte auth tags. */
  13798. /* This sample uses a 15-byte auth tag. */
  13799. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  13800. {
  13801. 0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
  13802. 0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
  13803. };
  13804. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  13805. {
  13806. 0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
  13807. 0x1a, 0x60, 0x24, 0xa7
  13808. };
  13809. WOLFSSL_SMALL_STACK_STATIC const byte a2[] =
  13810. {
  13811. 0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
  13812. 0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
  13813. };
  13814. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  13815. {
  13816. 0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
  13817. 0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
  13818. };
  13819. #endif
  13820. byte tag[16];
  13821. WOLFSSL_ENTER("gmac_test");
  13822. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13823. if ((gmac = (Gmac *)XMALLOC(sizeof *gmac, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  13824. return WC_TEST_RET_ENC_ERRNO;
  13825. #endif
  13826. XMEMSET(gmac, 0, sizeof *gmac); /* clear context */
  13827. (void)wc_AesInit(&gmac->aes, HEAP_HINT, INVALID_DEVID); /* Make sure devId updated */
  13828. XMEMSET(tag, 0, sizeof(tag));
  13829. wc_GmacSetKey(gmac, k1, sizeof(k1));
  13830. wc_GmacUpdate(gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
  13831. if (XMEMCMP(t1, tag, sizeof(t1)) != 0)
  13832. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13833. #if (!defined(HAVE_FIPS) || \
  13834. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) )
  13835. XMEMSET(tag, 0, sizeof(tag));
  13836. wc_GmacSetKey(gmac, k2, sizeof(k2));
  13837. wc_GmacUpdate(gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
  13838. if (XMEMCMP(t2, tag, sizeof(t2)) != 0)
  13839. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13840. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && !defined(NO_AES_DECRYPT)
  13841. {
  13842. WOLFSSL_SMALL_STACK_STATIC const byte badT[] =
  13843. {
  13844. 0xde, 0xad, 0xbe, 0xef, 0x17, 0x2e, 0xd0, 0x43,
  13845. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  13846. };
  13847. WC_RNG rng;
  13848. byte iv[12];
  13849. #ifndef HAVE_FIPS
  13850. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  13851. if (ret != 0)
  13852. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13853. #else
  13854. ret = wc_InitRng(&rng);
  13855. if (ret != 0)
  13856. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13857. #endif
  13858. ret = wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  13859. t1, sizeof(t1));
  13860. if (ret != 0)
  13861. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13862. ret = wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  13863. badT, sizeof(badT));
  13864. if (ret != AES_GCM_AUTH_E)
  13865. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13866. ret = wc_GmacVerify(k2, sizeof(k2), iv2, sizeof(iv2), a2, sizeof(a2),
  13867. t2, sizeof(t2));
  13868. if (ret != 0)
  13869. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13870. XMEMSET(tag, 0, sizeof(tag));
  13871. XMEMSET(iv, 0, sizeof(iv));
  13872. ret = wc_Gmac(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  13873. tag, sizeof(tag), &rng);
  13874. if (ret != 0)
  13875. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13876. ret = wc_GmacVerify(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  13877. tag, sizeof(tag));
  13878. if (ret != 0)
  13879. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13880. wc_FreeRng(&rng);
  13881. }
  13882. #endif /* !WC_NO_RNG && !HAVE_SELFTEST && !NO_AES_DECRYPT */
  13883. #endif /* HAVE_FIPS */
  13884. ret = 0;
  13885. out:
  13886. wc_AesFree(&gmac->aes);
  13887. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13888. XFREE(gmac, HEAP_HINT, DYNAMIC_TYPE_AES);
  13889. #endif
  13890. return ret;
  13891. }
  13892. #endif /* WOLFSSL_AES_128 */
  13893. #endif /* HAVE_AESGCM */
  13894. #if defined(HAVE_AESCCM)
  13895. #if defined(WOLFSSL_AES_256)
  13896. static wc_test_ret_t aesccm_256_test(void)
  13897. {
  13898. wc_test_ret_t ret;
  13899. /* Test vectors from NIST AES CCM 256-bit CAST Example #1 */
  13900. WOLFSSL_SMALL_STACK_STATIC const byte in_key[32] = {
  13901. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  13902. 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
  13903. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  13904. 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F
  13905. };
  13906. WOLFSSL_SMALL_STACK_STATIC const byte in_nonce[7] = {
  13907. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
  13908. WOLFSSL_SMALL_STACK_STATIC const byte in_auth[8] = {
  13909. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  13910. WOLFSSL_SMALL_STACK_STATIC const byte in_plaintext[4] = {
  13911. 0x20, 0x21, 0x22, 0x23};
  13912. WOLFSSL_SMALL_STACK_STATIC const byte exp_ciphertext[4] = {
  13913. 0x8A, 0xB1, 0xA8, 0x74};
  13914. WOLFSSL_SMALL_STACK_STATIC const byte exp_tag[4] = {
  13915. 0x95, 0xFC, 0x08, 0x20};
  13916. byte output[sizeof(in_plaintext)];
  13917. byte atag[sizeof(exp_tag)];
  13918. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13919. Aes* aes = (Aes*)XMALLOC(sizeof(Aes), HEAP_HINT, DYNAMIC_TYPE_AES);
  13920. if (aes == NULL) {
  13921. return MEMORY_E;
  13922. }
  13923. #else
  13924. Aes aes[1];
  13925. #endif
  13926. ret = wc_AesInit(aes, HEAP_HINT, devId);
  13927. if (ret == 0) {
  13928. ret = wc_AesCcmSetKey(aes, in_key, sizeof(in_key));
  13929. }
  13930. if (ret == 0) {
  13931. ret = wc_AesCcmEncrypt(aes, output, in_plaintext, sizeof(in_plaintext),
  13932. in_nonce, sizeof(in_nonce),
  13933. atag, sizeof(atag),
  13934. in_auth, sizeof(in_auth));
  13935. }
  13936. /* Verify we produce the proper ciphertext and tag */
  13937. if (ret == 0 &&
  13938. (XMEMCMP(output, exp_ciphertext, sizeof(output)) ||
  13939. XMEMCMP(atag, exp_tag, sizeof(atag)))) {
  13940. ret = WC_TEST_RET_ENC_NC;
  13941. }
  13942. #ifdef HAVE_AES_DECRYPT
  13943. if (ret == 0) {
  13944. /* decrypt inline */
  13945. ret = wc_AesCcmDecrypt(aes, output, output, sizeof(output),
  13946. in_nonce, sizeof(in_nonce),
  13947. atag, sizeof(atag),
  13948. in_auth, sizeof(in_auth));
  13949. }
  13950. /* Verify decryption was successful */
  13951. if (ret == 0 &&
  13952. XMEMCMP(output, in_plaintext, sizeof(output))) {
  13953. ret = WC_TEST_RET_ENC_NC;
  13954. }
  13955. #endif
  13956. wc_AesFree(aes);
  13957. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13958. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  13959. #endif
  13960. return ret;
  13961. }
  13962. #endif /* WOLFSSL_AES_256 */
  13963. #if defined(WOLFSSL_AES_128)
  13964. static wc_test_ret_t aesccm_128_test(void)
  13965. {
  13966. wc_test_ret_t ret;
  13967. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13968. Aes *enc;
  13969. #else
  13970. Aes enc[1];
  13971. #endif
  13972. /* key */
  13973. WOLFSSL_SMALL_STACK_STATIC const byte k[] =
  13974. {
  13975. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  13976. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  13977. };
  13978. /* nonce */
  13979. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  13980. {
  13981. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  13982. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  13983. };
  13984. /* plaintext */
  13985. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  13986. {
  13987. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  13988. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  13989. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  13990. };
  13991. /* plaintext - long */
  13992. WOLFSSL_SMALL_STACK_STATIC const byte pl[] =
  13993. {
  13994. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  13995. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  13996. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  13997. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  13998. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  13999. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14000. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  14001. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  14002. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  14003. 0x50
  14004. };
  14005. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  14006. {
  14007. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  14008. };
  14009. /* ciphertext */
  14010. WOLFSSL_SMALL_STACK_STATIC const byte c[] =
  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
  14015. };
  14016. /* tag - authentication */
  14017. WOLFSSL_SMALL_STACK_STATIC const byte t[] =
  14018. {
  14019. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  14020. };
  14021. /* ciphertext - long */
  14022. WOLFSSL_SMALL_STACK_STATIC const byte cl[] =
  14023. {
  14024. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  14025. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  14026. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0xe0,
  14027. 0x44, 0x2d, 0xbe, 0x25, 0xfa, 0x48, 0x2b, 0xa8,
  14028. 0x36, 0x0b, 0xbf, 0x01, 0xc0, 0x12, 0x45, 0xa4,
  14029. 0x82, 0x9f, 0x20, 0x6c, 0xc3, 0xd6, 0xae, 0x5b,
  14030. 0x54, 0x8d, 0xd0, 0xb1, 0x69, 0x2c, 0xec, 0x5e,
  14031. 0x95, 0xa5, 0x6b, 0x48, 0xc3, 0xc6, 0xc8, 0x9e,
  14032. 0xc7, 0x92, 0x98, 0x9d, 0x26, 0x7d, 0x2a, 0x10,
  14033. 0x0b
  14034. };
  14035. /* tag - authentication - long */
  14036. WOLFSSL_SMALL_STACK_STATIC const byte tl[] =
  14037. {
  14038. 0x89, 0xd8, 0xd2, 0x02, 0xc5, 0xcf, 0xae, 0xf4
  14039. };
  14040. /* tag - authentication - empty plaintext */
  14041. WOLFSSL_SMALL_STACK_STATIC const byte t_empty[] =
  14042. {
  14043. 0xe4, 0x28, 0x8a, 0xc3, 0x78, 0x00, 0x0f, 0xf5
  14044. };
  14045. byte t2[sizeof(t)];
  14046. byte p2[sizeof(p)];
  14047. byte c2[sizeof(c)];
  14048. byte iv2[sizeof(iv)];
  14049. byte pl2[sizeof(pl)];
  14050. byte cl2[sizeof(cl)];
  14051. byte tl2[sizeof(tl)];
  14052. byte t_empty2[sizeof(t_empty)];
  14053. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14054. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  14055. return WC_TEST_RET_ENC_ERRNO;
  14056. #endif
  14057. XMEMSET(enc, 0, sizeof *enc); /* clear context */
  14058. XMEMSET(t2, 0, sizeof(t2));
  14059. XMEMSET(c2, 0, sizeof(c2));
  14060. XMEMSET(p2, 0, sizeof(p2));
  14061. ret = wc_AesInit(enc, HEAP_HINT, devId);
  14062. if (ret != 0)
  14063. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14064. ret = wc_AesCcmSetKey(enc, k, sizeof(k));
  14065. if (ret != 0)
  14066. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14067. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  14068. ret = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  14069. t2, sizeof(t2), a, sizeof(a));
  14070. if (ret != 0)
  14071. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14072. if (XMEMCMP(c, c2, sizeof(c2)))
  14073. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14074. if (XMEMCMP(t, t2, sizeof(t2)))
  14075. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14076. #ifdef HAVE_AES_DECRYPT
  14077. ret = wc_AesCcmDecrypt(enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  14078. t2, sizeof(t2), a, sizeof(a));
  14079. if (ret != 0)
  14080. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14081. if (XMEMCMP(p, p2, sizeof(p2)))
  14082. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14083. /* Test the authentication failure */
  14084. t2[0]++; /* Corrupt the authentication tag. */
  14085. ret = wc_AesCcmDecrypt(enc, p2, c, sizeof(p2), iv, sizeof(iv),
  14086. t2, sizeof(t2), a, sizeof(a));
  14087. if (ret == 0)
  14088. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14089. /* Clear c2 to compare against p2. p2 should be set to zero in case of
  14090. * authentication fail. */
  14091. XMEMSET(c2, 0, sizeof(c2));
  14092. if (XMEMCMP(p2, c2, sizeof(p2)))
  14093. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14094. #endif
  14095. wc_AesFree(enc);
  14096. XMEMSET(enc, 0, sizeof(Aes)); /* clear context */
  14097. XMEMSET(t2, 0, sizeof(t2));
  14098. XMEMSET(c2, 0, sizeof(c2));
  14099. XMEMSET(p2, 0, sizeof(p2));
  14100. XMEMSET(iv2, 0, sizeof(iv2));
  14101. ret = wc_AesInit(enc, HEAP_HINT, devId);
  14102. if (ret != 0)
  14103. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14104. #ifndef HAVE_SELFTEST
  14105. /* selftest build does not have wc_AesCcmSetNonce() or
  14106. * wc_AesCcmEncrypt_ex() */
  14107. ret = wc_AesCcmSetKey(enc, k, sizeof(k));
  14108. if (ret != 0)
  14109. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14110. ret = wc_AesCcmSetNonce(enc, iv, sizeof(iv));
  14111. if (ret != 0)
  14112. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14113. ret = wc_AesCcmEncrypt_ex(enc, c2, p, sizeof(c2), iv2, sizeof(iv2),
  14114. t2, sizeof(t2), a, sizeof(a));
  14115. if (ret != 0)
  14116. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14117. if (XMEMCMP(iv, iv2, sizeof(iv2)))
  14118. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14119. if (XMEMCMP(c, c2, sizeof(c2)))
  14120. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14121. if (XMEMCMP(t, t2, sizeof(t2)))
  14122. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14123. #endif
  14124. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  14125. /* test fail on invalid IV sizes */
  14126. ret = wc_AesCcmSetKey(enc, k, sizeof(k));
  14127. if (ret != 0)
  14128. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14129. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  14130. ret = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  14131. t2, 1, a, sizeof(a));
  14132. if (ret == 0) {
  14133. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14134. }
  14135. #endif
  14136. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  14137. ret = wc_AesCcmEncrypt(enc, cl2, pl, sizeof(cl2), iv, sizeof(iv),
  14138. tl2, sizeof(tl2), a, sizeof(a));
  14139. if (ret != 0)
  14140. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14141. if (XMEMCMP(cl, cl2, sizeof(cl2)))
  14142. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14143. if (XMEMCMP(tl, tl2, sizeof(tl2)))
  14144. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14145. #ifdef HAVE_AES_DECRYPT
  14146. ret = wc_AesCcmDecrypt(enc, pl2, cl2, sizeof(pl2), iv, sizeof(iv),
  14147. tl2, sizeof(tl2), a, sizeof(a));
  14148. if (ret != 0)
  14149. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14150. if (XMEMCMP(pl, pl2, sizeof(pl2)))
  14151. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14152. #endif
  14153. /* test empty message as null input or output with nonzero inSz. */
  14154. ret = wc_AesCcmEncrypt(enc, pl2 /* out */, NULL /* in */, 1 /* inSz */,
  14155. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  14156. a, sizeof(a));
  14157. if (ret != BAD_FUNC_ARG)
  14158. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14159. ret = wc_AesCcmEncrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  14160. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  14161. a, sizeof(a));
  14162. if (ret != BAD_FUNC_ARG)
  14163. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14164. #ifdef HAVE_AES_DECRYPT
  14165. ret = wc_AesCcmDecrypt(enc, pl2, NULL /* in */, 1 /* inSz */,
  14166. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  14167. sizeof(a));
  14168. if (ret != BAD_FUNC_ARG)
  14169. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14170. ret = wc_AesCcmDecrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  14171. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  14172. sizeof(a));
  14173. if (ret != BAD_FUNC_ARG)
  14174. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14175. #endif
  14176. /* test empty message as null input and output with zero inSz --
  14177. * must either succeed, or fail early with BAD_FUNC_ARG.
  14178. */
  14179. ret = wc_AesCcmEncrypt(enc, NULL /* out */, NULL /* in */, 0 /* inSz */,
  14180. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  14181. a, sizeof(a));
  14182. if (ret != BAD_FUNC_ARG) {
  14183. if (ret != 0)
  14184. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14185. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  14186. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14187. #ifdef HAVE_AES_DECRYPT
  14188. ret = wc_AesCcmDecrypt(enc, NULL /* out */, NULL /* in */,
  14189. 0 /* inSz */, iv, sizeof(iv), t_empty2,
  14190. sizeof(t_empty2), a, sizeof(a));
  14191. if (ret != 0)
  14192. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14193. #endif
  14194. }
  14195. /* test empty message as zero-length string -- must work. */
  14196. ret = wc_AesCcmEncrypt(enc, pl2, (const byte *)"", 0 /* inSz */, iv,
  14197. sizeof(iv), t_empty2, sizeof(t_empty2), a,
  14198. sizeof(a));
  14199. if (ret != 0)
  14200. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14201. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  14202. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14203. #ifdef HAVE_AES_DECRYPT
  14204. ret = wc_AesCcmDecrypt(enc, pl2, (const byte *)"", 0 /* inSz */,
  14205. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  14206. sizeof(a));
  14207. if (ret != 0)
  14208. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14209. #endif
  14210. wc_AesFree(enc);
  14211. ret = 0;
  14212. out:
  14213. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14214. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  14215. #endif
  14216. return ret;
  14217. }
  14218. #endif /* WOLFSSL_AES_128 */
  14219. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesccm_test(void)
  14220. {
  14221. wc_test_ret_t ret = 0;
  14222. WOLFSSL_ENTER("aesccm_test");
  14223. #ifdef WOLFSSL_AES_128
  14224. if (ret == 0)
  14225. ret = aesccm_128_test();
  14226. #endif
  14227. #ifdef WOLFSSL_AES_256
  14228. if (ret == 0)
  14229. ret = aesccm_256_test();
  14230. #endif
  14231. return ret;
  14232. }
  14233. #endif /* HAVE_AESCCM */
  14234. #if defined(WOLFSSL_AES_EAX) && \
  14235. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  14236. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_eax_test(void)
  14237. {
  14238. typedef struct {
  14239. byte key[AES_256_KEY_SIZE];
  14240. int key_length;
  14241. byte iv[AES_BLOCK_SIZE];
  14242. int iv_length;
  14243. byte aad[AES_BLOCK_SIZE * 2];
  14244. int aad_length;
  14245. byte msg[AES_BLOCK_SIZE * 2];
  14246. int msg_length;
  14247. byte ct[AES_BLOCK_SIZE * 2];
  14248. int ct_length;
  14249. byte tag[AES_BLOCK_SIZE];
  14250. int tag_length;
  14251. int valid;
  14252. } AadVector;
  14253. /* A small selection of Google wycheproof vectors that use vectors
  14254. * from the original paper: eprint.iacr.org/2003/069
  14255. * https://github.com/google/wycheproof/blob/master/testvectors/aes_eax_test.json
  14256. */
  14257. WOLFSSL_SMALL_STACK_STATIC const AadVector vectors[] = {
  14258. /* Vector from paper - empty message with auth data */
  14259. {
  14260. /* key, key length */
  14261. {0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f,
  14262. 0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78}, 16,
  14263. /* iv, iv length */
  14264. {0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07,
  14265. 0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3}, 16,
  14266. /* aad, aad length */
  14267. {0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b}, 8,
  14268. /* msg, msg length */
  14269. {0}, 0,
  14270. /* ct, ct length */
  14271. {0}, 0,
  14272. /* tag, tag length */
  14273. {0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2,
  14274. 0x7b, 0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01}, 16,
  14275. /* valid */
  14276. 1,
  14277. },
  14278. /* Vector from paper - no auth data, valid auth tag */
  14279. {
  14280. /* key, key length */
  14281. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14282. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  14283. /* iv , iv length */
  14284. {0x3c, 0x8c, 0xc2, 0x97, 0x0a, 0x00, 0x8f, 0x75,
  14285. 0xcc, 0x5b, 0xea, 0xe2, 0x84, 0x72, 0x58, 0xc2}, 16,
  14286. /* aad, aad length */
  14287. {0}, 0,
  14288. /* msg, msg length */
  14289. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  14290. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  14291. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  14292. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  14293. /* ct, ct length */
  14294. {0x3c, 0x44, 0x1f, 0x32, 0xce, 0x07, 0x82, 0x23,
  14295. 0x64, 0xd7, 0xa2, 0x99, 0x0e, 0x50, 0xbb, 0x13,
  14296. 0xd7, 0xb0, 0x2a, 0x26, 0x96, 0x9e, 0x4a, 0x93,
  14297. 0x7e, 0x5e, 0x90, 0x73, 0xb0, 0xd9, 0xc9, 0x68}, 32,
  14298. /* tag, tag length */
  14299. {0xdb, 0x90, 0xbd, 0xb3, 0xda, 0x3d, 0x00, 0xaf,
  14300. 0xd0, 0xfc, 0x6a, 0x83, 0x55, 0x1d, 0xa9, 0x5e}, 16,
  14301. /* valid */
  14302. 1,
  14303. },
  14304. /* Vector from paper - no auth data with invalid auth tag */
  14305. {
  14306. /* key, key length */
  14307. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14308. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  14309. /* iv, iv length */
  14310. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  14311. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  14312. /* aad, aad length */
  14313. {0}, 0,
  14314. /* msg, msg length */
  14315. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  14316. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  14317. /* ct , ct length */
  14318. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  14319. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  14320. /* tag, tag length */
  14321. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  14322. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  14323. /* valid */
  14324. 0,
  14325. },
  14326. };
  14327. WOLFSSL_SMALL_STACK_STATIC byte ciphertext[sizeof(vectors[0].ct)];
  14328. WOLFSSL_SMALL_STACK_STATIC byte authtag[sizeof(vectors[0].tag)];
  14329. int i;
  14330. int len;
  14331. wc_test_ret_t ret;
  14332. WOLFSSL_ENTER("aes_eax_test");
  14333. for (i = 0; i < (int)(sizeof(vectors)/sizeof(vectors[0])); i++) {
  14334. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  14335. len = sizeof(authtag);
  14336. ret = wc_AesEaxEncryptAuth(vectors[i].key, (word32)vectors[i].key_length,
  14337. ciphertext,
  14338. vectors[i].msg, (word32)vectors[i].msg_length,
  14339. vectors[i].iv, (word32)vectors[i].iv_length,
  14340. authtag, (word32)len,
  14341. vectors[i].aad, (word32)vectors[i].aad_length);
  14342. if (ret != 0) {
  14343. return WC_TEST_RET_ENC_EC(ret);
  14344. }
  14345. /* check ciphertext matches vector */
  14346. if (XMEMCMP(ciphertext, vectors[i].ct, (size_t)vectors[i].ct_length)) {
  14347. return WC_TEST_RET_ENC_NC;
  14348. }
  14349. /* check that tag matches vector only for vectors marked as valid */
  14350. ret = XMEMCMP(authtag, vectors[i].tag, len);
  14351. if (vectors[i].valid == 1 && ret != 0 ) {
  14352. return WC_TEST_RET_ENC_NC;
  14353. }
  14354. else if (vectors[i].valid == 0 && ret == 0) {
  14355. return WC_TEST_RET_ENC_NC;
  14356. }
  14357. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  14358. ret = wc_AesEaxDecryptAuth(vectors[i].key, (word32)vectors[i].key_length,
  14359. ciphertext,
  14360. vectors[i].ct, (word32)vectors[i].ct_length,
  14361. vectors[i].iv, (word32)vectors[i].iv_length,
  14362. authtag, (word32)len,
  14363. vectors[i].aad, (word32)vectors[i].aad_length);
  14364. if (ret != 0) {
  14365. return WC_TEST_RET_ENC_EC(ret);
  14366. }
  14367. /* check decrypted ciphertext matches vector plaintext */
  14368. if (XMEMCMP(ciphertext, vectors[i].msg, (size_t)vectors[i].msg_length)) {
  14369. return WC_TEST_RET_ENC_NC;
  14370. }
  14371. }
  14372. return 0;
  14373. }
  14374. #endif /* WOLFSSL_AES_EAX */
  14375. #ifdef HAVE_AES_KEYWRAP
  14376. #define MAX_KEYWRAP_TEST_OUTLEN 40
  14377. #define MAX_KEYWRAP_TEST_PLAINLEN 32
  14378. typedef struct keywrapVector {
  14379. const byte* kek;
  14380. const byte* data;
  14381. const byte* verify;
  14382. word32 kekLen;
  14383. word32 dataLen;
  14384. word32 verifyLen;
  14385. } keywrapVector;
  14386. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aeskeywrap_test(void)
  14387. {
  14388. int wrapSz, plainSz, testSz, i;
  14389. /* test vectors from RFC 3394 (kek, data, verify) */
  14390. #ifdef WOLFSSL_AES_128
  14391. /* Wrap 128 bits of Key Data with a 128-bit KEK */
  14392. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  14393. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14394. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  14395. };
  14396. WOLFSSL_SMALL_STACK_STATIC const byte d1[] = {
  14397. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14398. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  14399. };
  14400. WOLFSSL_SMALL_STACK_STATIC const byte v1[] = {
  14401. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  14402. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  14403. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  14404. };
  14405. #endif /* WOLFSSL_AES_128 */
  14406. #ifdef WOLFSSL_AES_192
  14407. /* Wrap 128 bits of Key Data with a 192-bit KEK */
  14408. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  14409. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14410. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  14411. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  14412. };
  14413. WOLFSSL_SMALL_STACK_STATIC const byte d2[] = {
  14414. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14415. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  14416. };
  14417. WOLFSSL_SMALL_STACK_STATIC const byte v2[] = {
  14418. 0x96, 0x77, 0x8B, 0x25, 0xAE, 0x6C, 0xA4, 0x35,
  14419. 0xF9, 0x2B, 0x5B, 0x97, 0xC0, 0x50, 0xAE, 0xD2,
  14420. 0x46, 0x8A, 0xB8, 0xA1, 0x7A, 0xD8, 0x4E, 0x5D
  14421. };
  14422. #endif
  14423. #ifdef WOLFSSL_AES_256
  14424. /* Wrap 128 bits of Key Data with a 256-bit KEK */
  14425. WOLFSSL_SMALL_STACK_STATIC const byte k3[] = {
  14426. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14427. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  14428. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  14429. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  14430. };
  14431. WOLFSSL_SMALL_STACK_STATIC const byte d3[] = {
  14432. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14433. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  14434. };
  14435. WOLFSSL_SMALL_STACK_STATIC const byte v3[] = {
  14436. 0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2,
  14437. 0x63, 0xE9, 0x77, 0x79, 0x05, 0x81, 0x8A, 0x2A,
  14438. 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7
  14439. };
  14440. #endif
  14441. #ifdef WOLFSSL_AES_192
  14442. /* Wrap 192 bits of Key Data with a 192-bit KEK */
  14443. WOLFSSL_SMALL_STACK_STATIC const byte k4[] = {
  14444. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14445. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  14446. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  14447. };
  14448. WOLFSSL_SMALL_STACK_STATIC const byte d4[] = {
  14449. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14450. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  14451. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  14452. };
  14453. WOLFSSL_SMALL_STACK_STATIC const byte v4[] = {
  14454. 0x03, 0x1D, 0x33, 0x26, 0x4E, 0x15, 0xD3, 0x32,
  14455. 0x68, 0xF2, 0x4E, 0xC2, 0x60, 0x74, 0x3E, 0xDC,
  14456. 0xE1, 0xC6, 0xC7, 0xDD, 0xEE, 0x72, 0x5A, 0x93,
  14457. 0x6B, 0xA8, 0x14, 0x91, 0x5C, 0x67, 0x62, 0xD2
  14458. };
  14459. #endif
  14460. #ifdef WOLFSSL_AES_256
  14461. /* Wrap 192 bits of Key Data with a 256-bit KEK */
  14462. WOLFSSL_SMALL_STACK_STATIC const byte k5[] = {
  14463. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14464. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  14465. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  14466. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  14467. };
  14468. WOLFSSL_SMALL_STACK_STATIC const byte d5[] = {
  14469. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14470. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  14471. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  14472. };
  14473. WOLFSSL_SMALL_STACK_STATIC const byte v5[] = {
  14474. 0xA8, 0xF9, 0xBC, 0x16, 0x12, 0xC6, 0x8B, 0x3F,
  14475. 0xF6, 0xE6, 0xF4, 0xFB, 0xE3, 0x0E, 0x71, 0xE4,
  14476. 0x76, 0x9C, 0x8B, 0x80, 0xA3, 0x2C, 0xB8, 0x95,
  14477. 0x8C, 0xD5, 0xD1, 0x7D, 0x6B, 0x25, 0x4D, 0xA1
  14478. };
  14479. /* Wrap 256 bits of Key Data with a 256-bit KEK */
  14480. WOLFSSL_SMALL_STACK_STATIC const byte k6[] = {
  14481. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14482. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  14483. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  14484. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  14485. };
  14486. WOLFSSL_SMALL_STACK_STATIC const byte d6[] = {
  14487. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14488. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  14489. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14490. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  14491. };
  14492. WOLFSSL_SMALL_STACK_STATIC const byte v6[] = {
  14493. 0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4,
  14494. 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, 0xF8, 0x26,
  14495. 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26,
  14496. 0xCB, 0xC7, 0xF0, 0xE7, 0x1A, 0x99, 0xF4, 0x3B,
  14497. 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21
  14498. };
  14499. #endif /* WOLFSSL_AES_256 */
  14500. byte output[MAX_KEYWRAP_TEST_OUTLEN];
  14501. byte plain [MAX_KEYWRAP_TEST_PLAINLEN];
  14502. const keywrapVector test_wrap[] =
  14503. {
  14504. #ifdef WOLFSSL_AES_128
  14505. {k1, d1, v1, sizeof(k1), sizeof(d1), sizeof(v1)},
  14506. #endif
  14507. #ifdef WOLFSSL_AES_192
  14508. {k2, d2, v2, sizeof(k2), sizeof(d2), sizeof(v2)},
  14509. #endif
  14510. #ifdef WOLFSSL_AES_256
  14511. {k3, d3, v3, sizeof(k3), sizeof(d3), sizeof(v3)},
  14512. #endif
  14513. #ifdef WOLFSSL_AES_192
  14514. {k4, d4, v4, sizeof(k4), sizeof(d4), sizeof(v4)},
  14515. #endif
  14516. #ifdef WOLFSSL_AES_256
  14517. {k5, d5, v5, sizeof(k5), sizeof(d5), sizeof(v5)},
  14518. {k6, d6, v6, sizeof(k6), sizeof(d6), sizeof(v6)}
  14519. #endif
  14520. };
  14521. WOLFSSL_ENTER("aeskeywrap_test");
  14522. testSz = sizeof(test_wrap) / sizeof(keywrapVector);
  14523. XMEMSET(output, 0, sizeof(output));
  14524. XMEMSET(plain, 0, sizeof(plain));
  14525. for (i = 0; i < testSz; i++) {
  14526. wrapSz = wc_AesKeyWrap(test_wrap[i].kek, test_wrap[i].kekLen,
  14527. test_wrap[i].data, test_wrap[i].dataLen,
  14528. output, sizeof(output), NULL);
  14529. if ( (wrapSz < 0) || (wrapSz != (int)test_wrap[i].verifyLen) )
  14530. return WC_TEST_RET_ENC_NC;
  14531. if (XMEMCMP(output, test_wrap[i].verify, test_wrap[i].verifyLen) != 0)
  14532. return WC_TEST_RET_ENC_NC;
  14533. plainSz = wc_AesKeyUnWrap((byte*)test_wrap[i].kek, test_wrap[i].kekLen,
  14534. output, (word32)wrapSz,
  14535. plain, sizeof(plain), NULL);
  14536. if ( (plainSz < 0) || (plainSz != (int)test_wrap[i].dataLen) )
  14537. return WC_TEST_RET_ENC_NC;
  14538. if (XMEMCMP(plain, test_wrap[i].data, test_wrap[i].dataLen) != 0)
  14539. return WC_TEST_RET_ENC_I(i);
  14540. }
  14541. return 0;
  14542. }
  14543. #endif /* HAVE_AES_KEYWRAP */
  14544. #endif /* NO_AES */
  14545. #ifdef HAVE_ARIA
  14546. void printOutput(const char *strName, unsigned char *data, unsigned int dataSz)
  14547. {
  14548. #ifndef DEBUG_WOLFSSL
  14549. (void)strName;
  14550. (void)data;
  14551. (void)dataSz;
  14552. #else
  14553. WOLFSSL_MSG_EX("%s (%d):", strName,dataSz);
  14554. WOLFSSL_BUFFER(data,dataSz);
  14555. #endif
  14556. }
  14557. WOLFSSL_TEST_SUBROUTINE int ariagcm_test(MC_ALGID algo)
  14558. {
  14559. byte data[] = TEST_STRING;
  14560. word32 dataSz = TEST_STRING_SZ;
  14561. /* Arbitrarily random long key that we will truncate to the right size */
  14562. byte key[] = { 0x1E, 0xCC, 0x95, 0xCB, 0xD3, 0x74, 0x58, 0x4F,
  14563. 0x6F, 0x8A, 0x70, 0x26, 0xF7, 0x3C, 0x8D, 0xB6,
  14564. 0xDC, 0x32, 0x76, 0x20, 0xCF, 0x05, 0x4A, 0xCF,
  14565. 0x11, 0x86, 0xCD, 0x23, 0x5E, 0xC1, 0x6E, 0x2B };
  14566. byte cipher[2*TEST_STRING_SZ], plain[TEST_STRING_SZ], ad[256], authTag[AES_BLOCK_SIZE];
  14567. word32 keySz, adSz = 256, authTagSz = sizeof(authTag);
  14568. wc_Aria aria;
  14569. int ret = 0;
  14570. WOLFSSL_ENTER("ariagcm_test");
  14571. XMEMSET((void *)&aria, 0, sizeof(aria));
  14572. ret = wc_AriaInitCrypt(&aria, algo);
  14573. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  14574. ret = wc_AriaSetKey(&aria, key);
  14575. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  14576. MC_GetObjectValue(aria.hSession, aria.hKey, key, &keySz);
  14577. printOutput("Key", key, keySz);
  14578. WC_RNG rng;
  14579. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14580. if (ret != 0)
  14581. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14582. ret = wc_AriaGcmSetIV(&aria, GCM_NONCE_MID_SZ, NULL, 0, &rng);
  14583. if (ret != 0)
  14584. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14585. wc_FreeRng(&rng);
  14586. printOutput("Plaintext", data, sizeof(data));
  14587. XMEMSET(cipher, 0, sizeof(cipher));
  14588. ret = wc_AriaEncrypt(&aria, cipher, data, dataSz,
  14589. (byte *)aria.nonce, aria.nonceSz, ad, adSz,
  14590. authTag, authTagSz);
  14591. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  14592. printOutput("Ciphertext", cipher, sizeof(cipher));
  14593. printOutput("AuthTag", authTag, sizeof(authTag));
  14594. XMEMSET(plain, 0, sizeof(plain));
  14595. ret = wc_AriaDecrypt(&aria, plain, cipher, dataSz,
  14596. (byte *)aria.nonce, aria.nonceSz, ad, adSz,
  14597. authTag, authTagSz);
  14598. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  14599. printOutput("Plaintext", plain, sizeof(plain));
  14600. if (XMEMCMP(plain, data, dataSz) != 0)
  14601. ERROR_OUT(WC_TEST_RET_ENC_NC,out);
  14602. out:
  14603. if (ret != 0) { wc_AriaFreeCrypt(&aria); }
  14604. else { ret = wc_AriaFreeCrypt(&aria); }
  14605. return ret;
  14606. }
  14607. #endif /* HAVE_ARIA */
  14608. #ifdef HAVE_CAMELLIA
  14609. enum {
  14610. CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
  14611. };
  14612. typedef struct {
  14613. int type;
  14614. const byte* plaintext;
  14615. const byte* iv;
  14616. const byte* ciphertext;
  14617. const byte* key;
  14618. word32 keySz;
  14619. int errorCode;
  14620. } test_vector_t;
  14621. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t camellia_test(void)
  14622. {
  14623. /* Camellia ECB Test Plaintext */
  14624. WOLFSSL_SMALL_STACK_STATIC const byte pte[] =
  14625. {
  14626. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  14627. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  14628. };
  14629. /* Camellia ECB Test Initialization Vector */
  14630. WOLFSSL_SMALL_STACK_STATIC const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  14631. /* Test 1: Camellia ECB 128-bit key */
  14632. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  14633. {
  14634. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  14635. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  14636. };
  14637. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  14638. {
  14639. 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  14640. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
  14641. };
  14642. /* Test 2: Camellia ECB 192-bit key */
  14643. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  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. };
  14649. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  14650. {
  14651. 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  14652. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
  14653. };
  14654. /* Test 3: Camellia ECB 256-bit key */
  14655. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  14656. {
  14657. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  14658. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  14659. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14660. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  14661. };
  14662. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  14663. {
  14664. 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  14665. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
  14666. };
  14667. /* Camellia CBC Test Plaintext */
  14668. WOLFSSL_SMALL_STACK_STATIC const byte ptc[] =
  14669. {
  14670. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  14671. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  14672. };
  14673. /* Camellia CBC Test Initialization Vector */
  14674. WOLFSSL_SMALL_STACK_STATIC const byte ivc[] =
  14675. {
  14676. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14677. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  14678. };
  14679. /* Test 4: Camellia-CBC 128-bit key */
  14680. WOLFSSL_SMALL_STACK_STATIC const byte k4[] =
  14681. {
  14682. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  14683. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
  14684. };
  14685. WOLFSSL_SMALL_STACK_STATIC const byte c4[] =
  14686. {
  14687. 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  14688. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
  14689. };
  14690. /* Test 5: Camellia-CBC 192-bit key */
  14691. WOLFSSL_SMALL_STACK_STATIC const byte k5[] =
  14692. {
  14693. 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  14694. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  14695. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
  14696. };
  14697. WOLFSSL_SMALL_STACK_STATIC const byte c5[] =
  14698. {
  14699. 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  14700. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
  14701. };
  14702. /* Test 6: CBC 256-bit key */
  14703. WOLFSSL_SMALL_STACK_STATIC const byte k6[] =
  14704. {
  14705. 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  14706. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  14707. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  14708. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
  14709. };
  14710. WOLFSSL_SMALL_STACK_STATIC const byte c6[] =
  14711. {
  14712. 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  14713. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
  14714. };
  14715. byte out[CAMELLIA_BLOCK_SIZE];
  14716. Camellia cam;
  14717. WOLFSSL_SMALL_STACK_STATIC const test_vector_t testVectors[] =
  14718. {
  14719. {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
  14720. {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
  14721. {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
  14722. {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
  14723. {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
  14724. {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
  14725. {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
  14726. {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
  14727. {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
  14728. {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
  14729. {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
  14730. {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
  14731. };
  14732. int i, testsSz;
  14733. int ret;
  14734. WOLFSSL_ENTER("camellia_test");
  14735. testsSz = sizeof(testVectors)/sizeof(test_vector_t);
  14736. for (i = 0; i < testsSz; i++) {
  14737. if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
  14738. testVectors[i].iv) != 0)
  14739. return testVectors[i].errorCode;
  14740. switch (testVectors[i].type) {
  14741. case CAM_ECB_ENC:
  14742. ret = wc_CamelliaEncryptDirect(&cam, out,
  14743. testVectors[i].plaintext);
  14744. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  14745. CAMELLIA_BLOCK_SIZE))
  14746. return testVectors[i].errorCode;
  14747. break;
  14748. case CAM_ECB_DEC:
  14749. ret = wc_CamelliaDecryptDirect(&cam, out,
  14750. testVectors[i].ciphertext);
  14751. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  14752. CAMELLIA_BLOCK_SIZE))
  14753. return testVectors[i].errorCode;
  14754. break;
  14755. case CAM_CBC_ENC:
  14756. ret = wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
  14757. CAMELLIA_BLOCK_SIZE);
  14758. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  14759. CAMELLIA_BLOCK_SIZE))
  14760. return testVectors[i].errorCode;
  14761. break;
  14762. case CAM_CBC_DEC:
  14763. ret = wc_CamelliaCbcDecrypt(&cam, out,
  14764. testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE);
  14765. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  14766. CAMELLIA_BLOCK_SIZE))
  14767. return testVectors[i].errorCode;
  14768. break;
  14769. default:
  14770. break;
  14771. }
  14772. }
  14773. /* Setting the IV and checking it was actually set. */
  14774. ret = wc_CamelliaSetIV(&cam, ivc);
  14775. if (ret != 0)
  14776. return WC_TEST_RET_ENC_EC(ret);
  14777. if (XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE) != 0)
  14778. return WC_TEST_RET_ENC_NC;
  14779. /* Setting the IV to NULL should be same as all zeros IV */
  14780. ret = wc_CamelliaSetIV(&cam, NULL);
  14781. if (ret != 0)
  14782. return WC_TEST_RET_ENC_EC(ret);
  14783. if (XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE) != 0)
  14784. return WC_TEST_RET_ENC_NC;
  14785. /* First parameter should never be null */
  14786. if (wc_CamelliaSetIV(NULL, NULL) == 0)
  14787. return WC_TEST_RET_ENC_NC;
  14788. /* First parameter should never be null, check it fails */
  14789. if (wc_CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
  14790. return WC_TEST_RET_ENC_NC;
  14791. /* Key should have a size of 16, 24, or 32 */
  14792. if (wc_CamelliaSetKey(&cam, k1, 0, NULL) == 0)
  14793. return WC_TEST_RET_ENC_NC;
  14794. return 0;
  14795. }
  14796. #endif /* HAVE_CAMELLIA */
  14797. #ifdef WOLFSSL_SM4
  14798. #ifdef WOLFSSL_SM4_ECB
  14799. static int sm4_ecb_test(void)
  14800. {
  14801. /* draft-ribose-cfrg-sm4-10 A.2.1.1 */
  14802. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  14803. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  14804. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  14805. };
  14806. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  14807. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  14808. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  14809. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  14810. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  14811. };
  14812. WOLFSSL_SMALL_STACK_STATIC const byte c1_ecb[] = {
  14813. 0x5E, 0xC8, 0x14, 0x3D, 0xE5, 0x09, 0xCF, 0xF7,
  14814. 0xB5, 0x17, 0x9F, 0x8F, 0x47, 0x4B, 0x86, 0x19,
  14815. 0x2F, 0x1D, 0x30, 0x5A, 0x7F, 0xB1, 0x7D, 0xF9,
  14816. 0x85, 0xF8, 0x1C, 0x84, 0x82, 0x19, 0x23, 0x04
  14817. };
  14818. wc_Sm4 sm4;
  14819. byte enc[SM4_BLOCK_SIZE * 4];
  14820. byte dec[SM4_BLOCK_SIZE * 4];
  14821. int ret;
  14822. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  14823. if (ret != 0)
  14824. return WC_TEST_RET_ENC_EC(ret);
  14825. /* Encrypt and decrypt with ECB. */
  14826. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  14827. if (ret != 0)
  14828. return WC_TEST_RET_ENC_EC(ret);
  14829. ret = wc_Sm4EcbEncrypt(&sm4, enc, p1, sizeof(p1));
  14830. if (ret != 0)
  14831. return WC_TEST_RET_ENC_EC(ret);
  14832. if (XMEMCMP(enc, c1_ecb, sizeof(c1_ecb)) != 0)
  14833. return WC_TEST_RET_ENC_NC;
  14834. ret = wc_Sm4EcbDecrypt(&sm4, dec, enc, sizeof(c1_ecb));
  14835. if (ret != 0)
  14836. return WC_TEST_RET_ENC_EC(ret);
  14837. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  14838. return WC_TEST_RET_ENC_NC;
  14839. wc_Sm4Free(&sm4);
  14840. return 0;
  14841. }
  14842. #endif
  14843. #ifdef WOLFSSL_SM4_CBC
  14844. static int sm4_cbc_test(void)
  14845. {
  14846. /* draft-ribose-cfrg-sm4-10 A.2.2.1 */
  14847. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  14848. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  14849. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  14850. };
  14851. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  14852. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  14853. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  14854. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  14855. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  14856. };
  14857. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  14858. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14859. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  14860. };
  14861. WOLFSSL_SMALL_STACK_STATIC const byte c1_cbc[] = {
  14862. 0x78, 0xEB, 0xB1, 0x1C, 0xC4, 0x0B, 0x0A, 0x48,
  14863. 0x31, 0x2A, 0xAE, 0xB2, 0x04, 0x02, 0x44, 0xCB,
  14864. 0x4C, 0xB7, 0x01, 0x69, 0x51, 0x90, 0x92, 0x26,
  14865. 0x97, 0x9B, 0x0D, 0x15, 0xDC, 0x6A, 0x8F, 0x6D
  14866. };
  14867. wc_Sm4 sm4;
  14868. byte enc[SM4_BLOCK_SIZE * 4];
  14869. byte dec[SM4_BLOCK_SIZE * 4];
  14870. int ret;
  14871. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  14872. if (ret != 0)
  14873. return WC_TEST_RET_ENC_EC(ret);
  14874. /* Encrypt and decrypt with CBC. */
  14875. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  14876. if (ret != 0)
  14877. return WC_TEST_RET_ENC_EC(ret);
  14878. ret = wc_Sm4SetIV(&sm4, i1);
  14879. if (ret != 0)
  14880. return WC_TEST_RET_ENC_EC(ret);
  14881. ret = wc_Sm4CbcEncrypt(&sm4, enc, p1, sizeof(p1));
  14882. if (ret != 0)
  14883. return WC_TEST_RET_ENC_EC(ret);
  14884. if (XMEMCMP(enc, c1_cbc, sizeof(c1_cbc)) != 0)
  14885. return WC_TEST_RET_ENC_NC;
  14886. ret = wc_Sm4SetIV(&sm4, i1);
  14887. if (ret != 0)
  14888. return WC_TEST_RET_ENC_EC(ret);
  14889. ret = wc_Sm4CbcDecrypt(&sm4, dec, enc, sizeof(c1_cbc));
  14890. if (ret != 0)
  14891. return WC_TEST_RET_ENC_EC(ret);
  14892. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  14893. return WC_TEST_RET_ENC_NC;
  14894. /* Encrypt and decrypt in-place with CBC. */
  14895. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  14896. if (ret != 0)
  14897. return WC_TEST_RET_ENC_EC(ret);
  14898. ret = wc_Sm4SetIV(&sm4, i1);
  14899. if (ret != 0)
  14900. return WC_TEST_RET_ENC_EC(ret);
  14901. XMEMCPY(enc, p1, sizeof(p1));
  14902. ret = wc_Sm4CbcEncrypt(&sm4, enc, enc, sizeof(p1));
  14903. if (ret != 0)
  14904. return WC_TEST_RET_ENC_EC(ret);
  14905. if (XMEMCMP(enc, c1_cbc, sizeof(c1_cbc)) != 0)
  14906. return WC_TEST_RET_ENC_NC;
  14907. ret = wc_Sm4SetIV(&sm4, i1);
  14908. if (ret != 0)
  14909. return WC_TEST_RET_ENC_EC(ret);
  14910. ret = wc_Sm4CbcDecrypt(&sm4, enc, enc, sizeof(c1_cbc));
  14911. if (ret != 0)
  14912. return WC_TEST_RET_ENC_EC(ret);
  14913. if (XMEMCMP(enc, p1, sizeof(p1)) != 0)
  14914. return WC_TEST_RET_ENC_NC;
  14915. wc_Sm4Free(&sm4);
  14916. return 0;
  14917. }
  14918. #endif
  14919. #ifdef WOLFSSL_SM4_CTR
  14920. static int sm4_ctr_test(void)
  14921. {
  14922. /* draft-ribose-cfrg-sm4-10 A.2.5.1 */
  14923. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  14924. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  14925. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  14926. };
  14927. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  14928. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14929. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  14930. };
  14931. WOLFSSL_SMALL_STACK_STATIC const byte p2[] = {
  14932. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  14933. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  14934. 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
  14935. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  14936. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  14937. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  14938. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  14939. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB
  14940. };
  14941. WOLFSSL_SMALL_STACK_STATIC const byte c2_ctr[] = {
  14942. 0xAC, 0x32, 0x36, 0xCB, 0x97, 0x0C, 0xC2, 0x07,
  14943. 0x91, 0x36, 0x4C, 0x39, 0x5A, 0x13, 0x42, 0xD1,
  14944. 0xA3, 0xCB, 0xC1, 0x87, 0x8C, 0x6F, 0x30, 0xCD,
  14945. 0x07, 0x4C, 0xCE, 0x38, 0x5C, 0xDD, 0x70, 0xC7,
  14946. 0xF2, 0x34, 0xBC, 0x0E, 0x24, 0xC1, 0x19, 0x80,
  14947. 0xFD, 0x12, 0x86, 0x31, 0x0C, 0xE3, 0x7B, 0x92,
  14948. 0x6E, 0x02, 0xFC, 0xD0, 0xFA, 0xA0, 0xBA, 0xF3,
  14949. 0x8B, 0x29, 0x33, 0x85, 0x1D, 0x82, 0x45, 0x14
  14950. };
  14951. wc_Sm4 sm4;
  14952. byte enc[SM4_BLOCK_SIZE * 4];
  14953. byte dec[SM4_BLOCK_SIZE * 4];
  14954. int chunk;
  14955. int i;
  14956. int ret;
  14957. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  14958. if (ret != 0)
  14959. return WC_TEST_RET_ENC_EC(ret);
  14960. /* Encrypt and decrypt using encrypt with CTR. */
  14961. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  14962. if (ret != 0)
  14963. return WC_TEST_RET_ENC_EC(ret);
  14964. ret = wc_Sm4SetIV(&sm4, i1);
  14965. if (ret != 0)
  14966. return WC_TEST_RET_ENC_EC(ret);
  14967. ret = wc_Sm4CtrEncrypt(&sm4, enc, p2, sizeof(p2));
  14968. if (ret != 0)
  14969. return WC_TEST_RET_ENC_EC(ret);
  14970. if (XMEMCMP(enc, c2_ctr, sizeof(c2_ctr)) != 0)
  14971. return WC_TEST_RET_ENC_NC;
  14972. ret = wc_Sm4SetIV(&sm4, i1);
  14973. if (ret != 0)
  14974. return WC_TEST_RET_ENC_EC(ret);
  14975. ret = wc_Sm4CtrEncrypt(&sm4, dec, enc, sizeof(c2_ctr));
  14976. if (ret != 0)
  14977. return WC_TEST_RET_ENC_EC(ret);
  14978. if (XMEMCMP(dec, p2, sizeof(p2)) != 0)
  14979. return WC_TEST_RET_ENC_NC;
  14980. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  14981. ret = wc_Sm4SetIV(&sm4, i1);
  14982. if (ret != 0)
  14983. return WC_TEST_RET_ENC_I(chunk);
  14984. XMEMSET(enc, 0, sizeof(enc));
  14985. for (i = 0; i + chunk <= (int)sizeof(p2); i += chunk) {
  14986. ret = wc_Sm4CtrEncrypt(&sm4, enc + i, p2 + i, chunk);
  14987. if (ret != 0)
  14988. return WC_TEST_RET_ENC_I(i);
  14989. }
  14990. if (i < (int)sizeof(p2)) {
  14991. ret = wc_Sm4CtrEncrypt(&sm4, enc + i, p2 + i, sizeof(p2) - i);
  14992. if (ret != 0)
  14993. return WC_TEST_RET_ENC_I(chunk);
  14994. }
  14995. if (XMEMCMP(enc, c2_ctr, sizeof(c2_ctr)) != 0)
  14996. return WC_TEST_RET_ENC_I(chunk);
  14997. }
  14998. wc_Sm4Free(&sm4);
  14999. return 0;
  15000. }
  15001. #endif
  15002. #ifdef WOLFSSL_SM4_GCM
  15003. static int sm4_gcm_test(void)
  15004. {
  15005. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  15006. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  15007. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  15008. };
  15009. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  15010. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  15011. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  15012. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  15013. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  15014. };
  15015. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  15016. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15017. 0x08, 0x09, 0x0A, 0x0B
  15018. };
  15019. WOLFSSL_SMALL_STACK_STATIC const byte a1[] = {
  15020. 0xFF, 0xEE, 0xDD
  15021. };
  15022. WOLFSSL_SMALL_STACK_STATIC const byte tag1[] = {
  15023. 0x83, 0xb2, 0x91, 0xcf, 0x22, 0xc9, 0x5f, 0x89,
  15024. 0xde, 0x3d, 0x52, 0x8d, 0xd7, 0x13, 0x50, 0x89
  15025. };
  15026. WOLFSSL_SMALL_STACK_STATIC const byte c1[] = {
  15027. 0xff, 0x8b, 0xb2, 0x3b, 0x0a, 0x0a, 0x12, 0xa4,
  15028. 0xa8, 0x4c, 0x4f, 0x67, 0x06, 0x81, 0xbb, 0x88,
  15029. 0x66, 0x17, 0xc7, 0x43, 0xbf, 0xae, 0x41, 0x40,
  15030. 0xec, 0x1e, 0x03, 0x85, 0x2b, 0x56, 0xa8, 0xc0
  15031. };
  15032. /* RFC8998 A.1. */
  15033. WOLFSSL_SMALL_STACK_STATIC const byte i2[] = {
  15034. 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, 0x00,
  15035. 0x00, 0x00, 0xAB, 0xCD
  15036. };
  15037. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  15038. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  15039. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  15040. };
  15041. WOLFSSL_SMALL_STACK_STATIC const byte p2[] = {
  15042. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  15043. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  15044. 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
  15045. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  15046. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  15047. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  15048. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  15049. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA
  15050. };
  15051. WOLFSSL_SMALL_STACK_STATIC const byte a2[] = {
  15052. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  15053. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  15054. 0xAB, 0xAD, 0xDA, 0xD2
  15055. };
  15056. WOLFSSL_SMALL_STACK_STATIC const byte c2[] = {
  15057. 0x17, 0xF3, 0x99, 0xF0, 0x8C, 0x67, 0xD5, 0xEE,
  15058. 0x19, 0xD0, 0xDC, 0x99, 0x69, 0xC4, 0xBB, 0x7D,
  15059. 0x5F, 0xD4, 0x6F, 0xD3, 0x75, 0x64, 0x89, 0x06,
  15060. 0x91, 0x57, 0xB2, 0x82, 0xBB, 0x20, 0x07, 0x35,
  15061. 0xD8, 0x27, 0x10, 0xCA, 0x5C, 0x22, 0xF0, 0xCC,
  15062. 0xFA, 0x7C, 0xBF, 0x93, 0xD4, 0x96, 0xAC, 0x15,
  15063. 0xA5, 0x68, 0x34, 0xCB, 0xCF, 0x98, 0xC3, 0x97,
  15064. 0xB4, 0x02, 0x4A, 0x26, 0x91, 0x23, 0x3B, 0x8D
  15065. };
  15066. WOLFSSL_SMALL_STACK_STATIC const byte tag2[] = {
  15067. 0x83, 0xDE, 0x35, 0x41, 0xE4, 0xC2, 0xB5, 0x81,
  15068. 0x77, 0xE0, 0x65, 0xA9, 0xBF, 0x7B, 0x62, 0xEC
  15069. };
  15070. wc_Sm4 sm4;
  15071. byte enc[SM4_BLOCK_SIZE * 4];
  15072. byte dec[SM4_BLOCK_SIZE * 4];
  15073. byte tag[SM4_BLOCK_SIZE];
  15074. int ret;
  15075. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  15076. if (ret != 0)
  15077. return WC_TEST_RET_ENC_EC(ret);
  15078. /* Encrypt and decrypt using encrypt with GCM. */
  15079. ret = wc_Sm4GcmSetKey(&sm4, k1, sizeof(k1));
  15080. if (ret != 0)
  15081. return WC_TEST_RET_ENC_EC(ret);
  15082. ret = wc_Sm4GcmEncrypt(&sm4, enc, p1, sizeof(p1), i1, sizeof(i1), tag,
  15083. sizeof(tag), a1, sizeof(a1));
  15084. if (ret != 0)
  15085. return WC_TEST_RET_ENC_EC(ret);
  15086. if (XMEMCMP(enc, c1, sizeof(c1)) != 0)
  15087. return WC_TEST_RET_ENC_NC;
  15088. if (XMEMCMP(tag, tag1, sizeof(tag1)) != 0)
  15089. return WC_TEST_RET_ENC_NC;
  15090. ret = wc_Sm4GcmDecrypt(&sm4, dec, enc, sizeof(c1), i1, sizeof(i1), tag,
  15091. sizeof(tag), a1, sizeof(a1));
  15092. if (ret != 0)
  15093. return WC_TEST_RET_ENC_EC(ret);
  15094. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  15095. return WC_TEST_RET_ENC_NC;
  15096. /* RFC8998 test vector. */
  15097. ret = wc_Sm4GcmSetKey(&sm4, k2, sizeof(k2));
  15098. if (ret != 0)
  15099. return WC_TEST_RET_ENC_EC(ret);
  15100. ret = wc_Sm4GcmEncrypt(&sm4, enc, p2, sizeof(p2), i2, sizeof(i2), tag,
  15101. sizeof(tag), a2, sizeof(a2));
  15102. if (ret != 0)
  15103. return WC_TEST_RET_ENC_EC(ret);
  15104. if (XMEMCMP(enc, c2, sizeof(c2)) != 0)
  15105. return WC_TEST_RET_ENC_NC;
  15106. if (XMEMCMP(tag, tag2, sizeof(tag2)) != 0)
  15107. return WC_TEST_RET_ENC_NC;
  15108. ret = wc_Sm4GcmDecrypt(&sm4, dec, enc, sizeof(c2), i2, sizeof(i2), tag,
  15109. sizeof(tag), a2, sizeof(a2));
  15110. if (ret != 0)
  15111. return WC_TEST_RET_ENC_EC(ret);
  15112. if (XMEMCMP(dec, p2, sizeof(p2)) != 0)
  15113. return WC_TEST_RET_ENC_NC;
  15114. wc_Sm4Free(&sm4);
  15115. return 0;
  15116. }
  15117. #endif
  15118. #ifdef WOLFSSL_SM4_CCM
  15119. static int sm4_ccm_test(void)
  15120. {
  15121. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  15122. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  15123. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  15124. };
  15125. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  15126. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  15127. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  15128. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  15129. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  15130. };
  15131. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  15132. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15133. 0x08, 0x09, 0x0A, 0x0B
  15134. };
  15135. WOLFSSL_SMALL_STACK_STATIC const byte a1[] = {
  15136. 0xFF, 0xEE, 0xDD
  15137. };
  15138. WOLFSSL_SMALL_STACK_STATIC const byte tag1[] = {
  15139. 0x9a, 0x98, 0x04, 0xb6, 0x0f, 0x19, 0x4a, 0x46,
  15140. 0xba, 0xed, 0xe6, 0x89, 0x69, 0x34, 0xad, 0x61
  15141. };
  15142. WOLFSSL_SMALL_STACK_STATIC const byte c1[] = {
  15143. 0xbd, 0xc0, 0x72, 0x60, 0xda, 0x2d, 0x11, 0xdc,
  15144. 0x66, 0x33, 0xcc, 0xec, 0xb2, 0xf4, 0x53, 0x59,
  15145. 0x9e, 0xb1, 0xb3, 0x6b, 0x1f, 0x1c, 0xfb, 0x29,
  15146. 0xf5, 0x37, 0xfc, 0x00, 0xf2, 0x4e, 0x70, 0x6f
  15147. };
  15148. /* RFC8998 A.1. */
  15149. WOLFSSL_SMALL_STACK_STATIC const byte i2[] = {
  15150. 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, 0x00,
  15151. 0x00, 0x00, 0xAB, 0xCD
  15152. };
  15153. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  15154. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  15155. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  15156. };
  15157. WOLFSSL_SMALL_STACK_STATIC const byte p2[] = {
  15158. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  15159. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  15160. 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
  15161. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  15162. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  15163. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  15164. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  15165. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA
  15166. };
  15167. WOLFSSL_SMALL_STACK_STATIC const byte a2[] = {
  15168. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  15169. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  15170. 0xAB, 0xAD, 0xDA, 0xD2
  15171. };
  15172. WOLFSSL_SMALL_STACK_STATIC const byte c2[] = {
  15173. 0x48, 0xAF, 0x93, 0x50, 0x1F, 0xA6, 0x2A, 0xDB,
  15174. 0xCD, 0x41, 0x4C, 0xCE, 0x60, 0x34, 0xD8, 0x95,
  15175. 0xDD, 0xA1, 0xBF, 0x8F, 0x13, 0x2F, 0x04, 0x20,
  15176. 0x98, 0x66, 0x15, 0x72, 0xE7, 0x48, 0x30, 0x94,
  15177. 0xFD, 0x12, 0xE5, 0x18, 0xCE, 0x06, 0x2C, 0x98,
  15178. 0xAC, 0xEE, 0x28, 0xD9, 0x5D, 0xF4, 0x41, 0x6B,
  15179. 0xED, 0x31, 0xA2, 0xF0, 0x44, 0x76, 0xC1, 0x8B,
  15180. 0xB4, 0x0C, 0x84, 0xA7, 0x4B, 0x97, 0xDC, 0x5B
  15181. };
  15182. WOLFSSL_SMALL_STACK_STATIC const byte tag2[] = {
  15183. 0x16, 0x84, 0x2D, 0x4F, 0xA1, 0x86, 0xF5, 0x6A,
  15184. 0xB3, 0x32, 0x56, 0x97, 0x1F, 0xA1, 0x10, 0xF4
  15185. };
  15186. wc_Sm4 sm4;
  15187. byte enc[SM4_BLOCK_SIZE * 4];
  15188. byte dec[SM4_BLOCK_SIZE * 4];
  15189. byte tag[SM4_BLOCK_SIZE];
  15190. int ret;
  15191. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  15192. if (ret != 0)
  15193. return WC_TEST_RET_ENC_EC(ret);
  15194. /* Encrypt and decrypt using encrypt with CCM. */
  15195. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  15196. if (ret != 0)
  15197. return WC_TEST_RET_ENC_EC(ret);
  15198. ret = wc_Sm4CcmEncrypt(&sm4, enc, p1, sizeof(p1), i1, sizeof(i1), tag,
  15199. sizeof(tag), a1, sizeof(a1));
  15200. if (ret != 0)
  15201. return WC_TEST_RET_ENC_EC(ret);
  15202. if (XMEMCMP(enc, c1, sizeof(c1)) != 0)
  15203. return WC_TEST_RET_ENC_NC;
  15204. if (XMEMCMP(tag, tag1, sizeof(tag1)) != 0)
  15205. return WC_TEST_RET_ENC_NC;
  15206. ret = wc_Sm4CcmDecrypt(&sm4, dec, enc, sizeof(c1), i1, sizeof(i1), tag,
  15207. sizeof(tag), a1, sizeof(a1));
  15208. if (ret != 0)
  15209. return WC_TEST_RET_ENC_EC(ret);
  15210. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  15211. return WC_TEST_RET_ENC_NC;
  15212. /* RFC8998 test vector. */
  15213. ret = wc_Sm4SetKey(&sm4, k2, sizeof(k2));
  15214. if (ret != 0)
  15215. return WC_TEST_RET_ENC_EC(ret);
  15216. ret = wc_Sm4CcmEncrypt(&sm4, enc, p2, sizeof(p2), i2, sizeof(i2), tag,
  15217. sizeof(tag), a2, sizeof(a2));
  15218. if (ret != 0)
  15219. return WC_TEST_RET_ENC_EC(ret);
  15220. if (XMEMCMP(enc, c2, sizeof(c2)) != 0)
  15221. return WC_TEST_RET_ENC_NC;
  15222. if (XMEMCMP(tag, tag2, sizeof(tag2)) != 0)
  15223. return WC_TEST_RET_ENC_NC;
  15224. ret = wc_Sm4CcmDecrypt(&sm4, dec, enc, sizeof(c2), i2, sizeof(i2), tag,
  15225. sizeof(tag), a2, sizeof(a2));
  15226. if (ret != 0)
  15227. return WC_TEST_RET_ENC_EC(ret);
  15228. if (XMEMCMP(dec, p2, sizeof(p2)) != 0)
  15229. return WC_TEST_RET_ENC_NC;
  15230. wc_Sm4Free(&sm4);
  15231. return 0;
  15232. }
  15233. #endif
  15234. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm4_test(void)
  15235. {
  15236. wc_test_ret_t ret;
  15237. WOLFSSL_ENTER("sm4_test");
  15238. #ifdef WOLFSSL_SM4_ECB
  15239. ret = sm4_ecb_test();
  15240. if (ret != 0)
  15241. return ret;
  15242. #endif
  15243. #ifdef WOLFSSL_SM4_CBC
  15244. ret = sm4_cbc_test();
  15245. if (ret != 0)
  15246. return ret;
  15247. #endif
  15248. #ifdef WOLFSSL_SM4_CTR
  15249. ret = sm4_ctr_test();
  15250. if (ret != 0)
  15251. return ret;
  15252. #endif
  15253. #ifdef WOLFSSL_SM4_GCM
  15254. ret = sm4_gcm_test();
  15255. if (ret != 0)
  15256. return ret;
  15257. #endif
  15258. #ifdef WOLFSSL_SM4_CCM
  15259. ret = sm4_ccm_test();
  15260. if (ret != 0)
  15261. return ret;
  15262. #endif
  15263. return 0;
  15264. }
  15265. #endif
  15266. #ifdef HAVE_XCHACHA
  15267. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha_test(void) {
  15268. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  15269. 0x54, 0x68, 0x65, 0x20, 0x64, 0x68, 0x6f, 0x6c, 0x65, 0x20, 0x28, 0x70, 0x72, 0x6f, 0x6e, 0x6f, /* The dhole (prono */
  15270. 0x75, 0x6e, 0x63, 0x65, 0x64, 0x20, 0x22, 0x64, 0x6f, 0x6c, 0x65, 0x22, 0x29, 0x20, 0x69, 0x73, /* unced "dole") is */
  15271. 0x20, 0x61, 0x6c, 0x73, 0x6f, 0x20, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x20, 0x61, 0x73, 0x20, 0x74, /* also known as t */
  15272. 0x68, 0x65, 0x20, 0x41, 0x73, 0x69, 0x61, 0x74, 0x69, 0x63, 0x20, 0x77, 0x69, 0x6c, 0x64, 0x20, /* he Asiatic wild */
  15273. 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x72, 0x65, 0x64, 0x20, 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x61, 0x6e, /* dog, red dog, an */
  15274. 0x64, 0x20, 0x77, 0x68, 0x69, 0x73, 0x74, 0x6c, 0x69, 0x6e, 0x67, 0x20, 0x64, 0x6f, 0x67, 0x2e, /* d whistling dog. */
  15275. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x62, 0x6f, 0x75, 0x74, 0x20, 0x74, 0x68, 0x65, /* It is about the */
  15276. 0x20, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x61, 0x20, 0x47, 0x65, 0x72, 0x6d, 0x61, /* size of a Germa */
  15277. 0x6e, 0x20, 0x73, 0x68, 0x65, 0x70, 0x68, 0x65, 0x72, 0x64, 0x20, 0x62, 0x75, 0x74, 0x20, 0x6c, /* n shepherd but l */
  15278. 0x6f, 0x6f, 0x6b, 0x73, 0x20, 0x6d, 0x6f, 0x72, 0x65, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x61, /* ooks more like a */
  15279. 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x2d, 0x6c, 0x65, 0x67, 0x67, 0x65, 0x64, 0x20, 0x66, 0x6f, 0x78, /* long-legged fox */
  15280. 0x2e, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x68, 0x69, 0x67, 0x68, 0x6c, 0x79, 0x20, 0x65, 0x6c, /* . This highly el */
  15281. 0x75, 0x73, 0x69, 0x76, 0x65, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x6b, 0x69, 0x6c, 0x6c, 0x65, /* usive and skille */
  15282. 0x64, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x72, 0x20, 0x69, 0x73, 0x20, 0x63, 0x6c, 0x61, 0x73, /* d jumper is clas */
  15283. 0x73, 0x69, 0x66, 0x69, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x77, 0x6f, 0x6c, 0x76, /* sified with wolv */
  15284. 0x65, 0x73, 0x2c, 0x20, 0x63, 0x6f, 0x79, 0x6f, 0x74, 0x65, 0x73, 0x2c, 0x20, 0x6a, 0x61, 0x63, /* es, coyotes, jac */
  15285. 0x6b, 0x61, 0x6c, 0x73, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x66, 0x6f, 0x78, 0x65, 0x73, 0x20, /* kals, and foxes */
  15286. 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 0x61, 0x78, 0x6f, 0x6e, 0x6f, 0x6d, 0x69, 0x63, /* in the taxonomic */
  15287. 0x20, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x20, 0x43, 0x61, 0x6e, 0x69, 0x64, 0x61, 0x65, 0x2e /* family Canidae. */
  15288. };
  15289. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  15290. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  15291. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  15292. };
  15293. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  15294. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  15295. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x58 }; /* PQRSTUVW */
  15296. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  15297. 0x45, 0x59, 0xab, 0xba, 0x4e, 0x48, 0xc1, 0x61, 0x02, 0xe8, 0xbb, 0x2c, 0x05, 0xe6, 0x94, 0x7f,
  15298. 0x50, 0xa7, 0x86, 0xde, 0x16, 0x2f, 0x9b, 0x0b, 0x7e, 0x59, 0x2a, 0x9b, 0x53, 0xd0, 0xd4, 0xe9,
  15299. 0x8d, 0x8d, 0x64, 0x10, 0xd5, 0x40, 0xa1, 0xa6, 0x37, 0x5b, 0x26, 0xd8, 0x0d, 0xac, 0xe4, 0xfa,
  15300. 0xb5, 0x23, 0x84, 0xc7, 0x31, 0xac, 0xbf, 0x16, 0xa5, 0x92, 0x3c, 0x0c, 0x48, 0xd3, 0x57, 0x5d,
  15301. 0x4d, 0x0d, 0x2c, 0x67, 0x3b, 0x66, 0x6f, 0xaa, 0x73, 0x10, 0x61, 0x27, 0x77, 0x01, 0x09, 0x3a,
  15302. 0x6b, 0xf7, 0xa1, 0x58, 0xa8, 0x86, 0x42, 0x92, 0xa4, 0x1c, 0x48, 0xe3, 0xa9, 0xb4, 0xc0, 0xda,
  15303. 0xec, 0xe0, 0xf8, 0xd9, 0x8d, 0x0d, 0x7e, 0x05, 0xb3, 0x7a, 0x30, 0x7b, 0xbb, 0x66, 0x33, 0x31,
  15304. 0x64, 0xec, 0x9e, 0x1b, 0x24, 0xea, 0x0d, 0x6c, 0x3f, 0xfd, 0xdc, 0xec, 0x4f, 0x68, 0xe7, 0x44,
  15305. 0x30, 0x56, 0x19, 0x3a, 0x03, 0xc8, 0x10, 0xe1, 0x13, 0x44, 0xca, 0x06, 0xd8, 0xed, 0x8a, 0x2b,
  15306. 0xfb, 0x1e, 0x8d, 0x48, 0xcf, 0xa6, 0xbc, 0x0e, 0xb4, 0xe2, 0x46, 0x4b, 0x74, 0x81, 0x42, 0x40,
  15307. 0x7c, 0x9f, 0x43, 0x1a, 0xee, 0x76, 0x99, 0x60, 0xe1, 0x5b, 0xa8, 0xb9, 0x68, 0x90, 0x46, 0x6e,
  15308. 0xf2, 0x45, 0x75, 0x99, 0x85, 0x23, 0x85, 0xc6, 0x61, 0xf7, 0x52, 0xce, 0x20, 0xf9, 0xda, 0x0c,
  15309. 0x09, 0xab, 0x6b, 0x19, 0xdf, 0x74, 0xe7, 0x6a, 0x95, 0x96, 0x74, 0x46, 0xf8, 0xd0, 0xfd, 0x41,
  15310. 0x5e, 0x7b, 0xee, 0x2a, 0x12, 0xa1, 0x14, 0xc2, 0x0e, 0xb5, 0x29, 0x2a, 0xe7, 0xa3, 0x49, 0xae,
  15311. 0x57, 0x78, 0x20, 0xd5, 0x52, 0x0a, 0x1f, 0x3f, 0xb6, 0x2a, 0x17, 0xce, 0x6a, 0x7e, 0x68, 0xfa,
  15312. 0x7c, 0x79, 0x11, 0x1d, 0x88, 0x60, 0x92, 0x0b, 0xc0, 0x48, 0xef, 0x43, 0xfe, 0x84, 0x48, 0x6c,
  15313. 0xcb, 0x87, 0xc2, 0x5f, 0x0a, 0xe0, 0x45, 0xf0, 0xcc, 0xe1, 0xe7, 0x98, 0x9a, 0x9a, 0xa2, 0x20,
  15314. 0xa2, 0x8b, 0xdd, 0x48, 0x27, 0xe7, 0x51, 0xa2, 0x4a, 0x6d, 0x5c, 0x62, 0xd7, 0x90, 0xa6, 0x63,
  15315. 0x93, 0xb9, 0x31, 0x11, 0xc1, 0xa5, 0x5d, 0xd7, 0x42, 0x1a, 0x10, 0x18, 0x49, 0x74, 0xc7, 0xc5
  15316. };
  15317. wc_test_ret_t ret;
  15318. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15319. struct ChaCha *chacha = (struct ChaCha *)XMALLOC(sizeof *chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  15320. byte *buf1 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15321. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15322. WOLFSSL_ENTER("XChaCha_test");
  15323. if ((chacha == NULL) || (buf1 == NULL) || (buf2 == NULL))
  15324. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15325. #else
  15326. struct ChaCha chacha[1];
  15327. byte buf1[sizeof Plaintext];
  15328. byte buf2[sizeof Plaintext];
  15329. WOLFSSL_ENTER("XChaCha_test");
  15330. #endif
  15331. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  15332. if (ret < 0)
  15333. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15334. ret = wc_Chacha_Process(chacha, buf1, Plaintext, sizeof Plaintext);
  15335. if (ret < 0)
  15336. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15337. if (XMEMCMP(buf1, Ciphertext, sizeof Plaintext))
  15338. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15339. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  15340. if (ret < 0)
  15341. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15342. ret = wc_Chacha_Process(chacha, buf2, buf1, sizeof Plaintext);
  15343. if (ret < 0)
  15344. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15345. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  15346. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15347. out:
  15348. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15349. if (chacha)
  15350. XFREE(chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  15351. if (buf1)
  15352. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15353. if (buf2)
  15354. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15355. #endif
  15356. return ret;
  15357. }
  15358. #endif /* HAVE_XCHACHA */
  15359. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  15360. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha20Poly1305_test(void) {
  15361. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  15362. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c, /* Ladies and Gentl */
  15363. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73, /* emen of the clas */
  15364. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63, /* s of '99: If I c */
  15365. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f, /* ould offer you o */
  15366. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20, /* nly one tip for */
  15367. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73, /* the future, suns */
  15368. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69, /* creen would be i */
  15369. 0x74, 0x2e }; /* t. */
  15370. WOLFSSL_SMALL_STACK_STATIC const byte AAD[] = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 }; /* PQRS........ */
  15371. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  15372. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  15373. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  15374. };
  15375. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  15376. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  15377. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 }; /* PQRSTUVW */
  15378. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  15379. 0xbd, 0x6d, 0x17, 0x9d, 0x3e, 0x83, 0xd4, 0x3b, 0x95, 0x76, 0x57, 0x94, 0x93, 0xc0, 0xe9, 0x39,
  15380. 0x57, 0x2a, 0x17, 0x00, 0x25, 0x2b, 0xfa, 0xcc, 0xbe, 0xd2, 0x90, 0x2c, 0x21, 0x39, 0x6c, 0xbb,
  15381. 0x73, 0x1c, 0x7f, 0x1b, 0x0b, 0x4a, 0xa6, 0x44, 0x0b, 0xf3, 0xa8, 0x2f, 0x4e, 0xda, 0x7e, 0x39,
  15382. 0xae, 0x64, 0xc6, 0x70, 0x8c, 0x54, 0xc2, 0x16, 0xcb, 0x96, 0xb7, 0x2e, 0x12, 0x13, 0xb4, 0x52,
  15383. 0x2f, 0x8c, 0x9b, 0xa4, 0x0d, 0xb5, 0xd9, 0x45, 0xb1, 0x1b, 0x69, 0xb9, 0x82, 0xc1, 0xbb, 0x9e,
  15384. 0x3f, 0x3f, 0xac, 0x2b, 0xc3, 0x69, 0x48, 0x8f, 0x76, 0xb2, 0x38, 0x35, 0x65, 0xd3, 0xff, 0xf9,
  15385. 0x21, 0xf9, 0x66, 0x4c, 0x97, 0x63, 0x7d, 0xa9, 0x76, 0x88, 0x12, 0xf6, 0x15, 0xc6, 0x8b, 0x13,
  15386. 0xb5, 0x2e };
  15387. WOLFSSL_SMALL_STACK_STATIC const byte Tag[] = {
  15388. 0xc0, 0x87, 0x59, 0x24, 0xc1, 0xc7, 0x98, 0x79, 0x47, 0xde, 0xaf, 0xd8, 0x78, 0x0a, 0xcf, 0x49
  15389. };
  15390. wc_test_ret_t ret;
  15391. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15392. byte *buf1 = (byte *)XMALLOC(sizeof Ciphertext + sizeof Tag, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15393. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15394. WOLFSSL_ENTER("XChaCha20Poly1305_test");
  15395. if ((buf1 == NULL) || (buf2 == NULL))
  15396. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15397. #else
  15398. byte buf1[sizeof Ciphertext + sizeof Tag];
  15399. byte buf2[sizeof Plaintext];
  15400. WOLFSSL_ENTER("XChaCha20Poly1305_test");
  15401. #endif
  15402. ret = wc_XChaCha20Poly1305_Encrypt(buf1, sizeof Ciphertext + sizeof Tag,
  15403. Plaintext, sizeof Plaintext,
  15404. AAD, sizeof AAD,
  15405. IV, sizeof IV,
  15406. Key, sizeof Key);
  15407. if (ret < 0)
  15408. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15409. if (XMEMCMP(buf1, Ciphertext, sizeof Ciphertext))
  15410. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15411. if (XMEMCMP(buf1 + sizeof Ciphertext, Tag, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE))
  15412. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15413. ret = wc_XChaCha20Poly1305_Decrypt(buf2, sizeof Plaintext,
  15414. buf1, sizeof Ciphertext + sizeof Tag,
  15415. AAD, sizeof AAD,
  15416. IV, sizeof IV,
  15417. Key, sizeof Key);
  15418. if (ret < 0)
  15419. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15420. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  15421. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15422. out:
  15423. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15424. if (buf1 != NULL)
  15425. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15426. if (buf2 != NULL)
  15427. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15428. #endif
  15429. return ret;
  15430. }
  15431. #endif /* defined(HAVE_XCHACHA) && defined(HAVE_POLY1305) */
  15432. #ifndef WC_NO_RNG
  15433. static wc_test_ret_t _rng_test(WC_RNG* rng, int errorOffset)
  15434. {
  15435. byte block[32];
  15436. wc_test_ret_t ret;
  15437. int i;
  15438. XMEMSET(block, 0, sizeof(block));
  15439. ret = wc_RNG_GenerateBlock(rng, block, sizeof(block));
  15440. if (ret != 0) {
  15441. ret = 1;
  15442. goto exit;
  15443. }
  15444. /* Check for 0's */
  15445. for (i=0; i<(int)sizeof(block); i++) {
  15446. if (block[i] == 0) {
  15447. ret++;
  15448. }
  15449. }
  15450. /* All zeros count check */
  15451. if (ret >= (int)sizeof(block)) {
  15452. ret = 2;
  15453. goto exit;
  15454. }
  15455. ret = wc_RNG_GenerateByte(rng, block);
  15456. if (ret != 0) {
  15457. ret = 3;
  15458. goto exit;
  15459. }
  15460. /* Parameter validation testing. */
  15461. ret = wc_RNG_GenerateBlock(NULL, block, sizeof(block));
  15462. if (ret != BAD_FUNC_ARG) {
  15463. ret = 4;
  15464. goto exit;
  15465. }
  15466. ret = wc_RNG_GenerateBlock(rng, NULL, sizeof(block));
  15467. if (ret != BAD_FUNC_ARG) {
  15468. ret = 5;
  15469. goto exit;
  15470. }
  15471. ret = wc_RNG_GenerateByte(NULL, block);
  15472. if (ret != BAD_FUNC_ARG) {
  15473. ret = 6;
  15474. goto exit;
  15475. }
  15476. ret = wc_RNG_GenerateByte(rng, NULL);
  15477. if (ret != BAD_FUNC_ARG) {
  15478. ret = 7;
  15479. goto exit;
  15480. }
  15481. ret = 0;
  15482. exit:
  15483. if (ret != 0)
  15484. ret = errorOffset - (ret * 1000000);
  15485. return ret;
  15486. }
  15487. static wc_test_ret_t random_rng_test(void)
  15488. {
  15489. WC_RNG localRng;
  15490. WC_RNG* rng;
  15491. wc_test_ret_t ret;
  15492. rng = &localRng;
  15493. /* Test stack based RNG. */
  15494. #ifndef HAVE_FIPS
  15495. ret = wc_InitRng_ex(rng, HEAP_HINT, devId);
  15496. #else
  15497. ret = wc_InitRng(rng);
  15498. #endif
  15499. if (ret != 0)
  15500. return WC_TEST_RET_ENC_EC(ret);
  15501. ret = _rng_test(rng, WC_TEST_RET_ENC_NC);
  15502. /* Make sure and free RNG */
  15503. wc_FreeRng(rng);
  15504. if (ret != 0)
  15505. return ret;
  15506. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  15507. {
  15508. byte nonce[8] = { 0 };
  15509. /* Test dynamic RNG */
  15510. rng = wc_rng_new(nonce, (word32)sizeof(nonce), HEAP_HINT);
  15511. if (rng == NULL)
  15512. return WC_TEST_RET_ENC_ERRNO;
  15513. ret = _rng_test(rng, WC_TEST_RET_ENC_NC);
  15514. wc_rng_free(rng);
  15515. rng = NULL;
  15516. if (ret != 0)
  15517. return ret;
  15518. /* Test dynamic RNG using extended API */
  15519. ret = wc_rng_new_ex(&rng, nonce, (word32)sizeof(nonce),
  15520. HEAP_HINT, devId);
  15521. if (ret != 0)
  15522. return WC_TEST_RET_ENC_EC(ret);
  15523. ret = _rng_test(rng, WC_TEST_RET_ENC_NC);
  15524. wc_rng_free(rng);
  15525. if (ret != 0)
  15526. return ret;
  15527. }
  15528. #endif
  15529. return ret;
  15530. }
  15531. #if defined(HAVE_HASHDRBG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  15532. #ifdef WC_RNG_SEED_CB
  15533. static int seed_cb(OS_Seed* os, byte* output, word32 sz)
  15534. {
  15535. word32 i;
  15536. (void)os;
  15537. /* Known answer test. Set the seed to the same value every time. */
  15538. for (i = 0; i < sz; i++)
  15539. output[i] = (byte)i;
  15540. return 0;
  15541. }
  15542. static wc_test_ret_t rng_seed_test(void)
  15543. {
  15544. #ifndef HAVE_FIPS
  15545. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  15546. {
  15547. 0x83, 0x46, 0x65, 0x2f, 0x5c, 0x44, 0x16, 0x5f,
  15548. 0xb3, 0x89, 0x26, 0xde, 0x0b, 0x6b, 0xa2, 0x06,
  15549. 0x7e, 0xa7, 0x9a, 0x55, 0x22, 0x01, 0xb0, 0x22,
  15550. 0xf4, 0x7e, 0xa2, 0x66, 0xc4, 0x08, 0x6f, 0xba
  15551. };
  15552. #else
  15553. /* FIPS uses a longer seed, so different check value. */
  15554. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  15555. {
  15556. 0xaf, 0x31, 0xcc, 0xef, 0xa9, 0x29, 0x4c, 0x24,
  15557. 0xbd, 0xa5, 0xa3, 0x52, 0x69, 0xf3, 0xb9, 0xb2,
  15558. 0x1e, 0xd4, 0x52, 0x3b, 0x9a, 0x96, 0x06, 0x20,
  15559. 0xc0, 0x5f, 0x44, 0x06, 0x1f, 0x80, 0xdf, 0xe0
  15560. };
  15561. #endif
  15562. byte output[WC_SHA256_DIGEST_SIZE];
  15563. WC_RNG rng;
  15564. wc_test_ret_t ret;
  15565. ret = wc_SetSeed_Cb(seed_cb);
  15566. if (ret != 0) {
  15567. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15568. }
  15569. ret = wc_InitRng(&rng);
  15570. if (ret != 0) {
  15571. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15572. }
  15573. ret = wc_RNG_GenerateBlock(&rng, output, sizeof(output));
  15574. if (ret != 0) {
  15575. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15576. }
  15577. ret = XMEMCMP(output, check, sizeof(output));
  15578. if (ret != 0) {
  15579. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15580. }
  15581. ret = wc_FreeRng(&rng);
  15582. if (ret != 0) {
  15583. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15584. }
  15585. ret = wc_SetSeed_Cb(wc_GenerateSeed);
  15586. if (ret != 0) {
  15587. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15588. }
  15589. out:
  15590. return ret;
  15591. }
  15592. #endif
  15593. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_test(void)
  15594. {
  15595. WOLFSSL_SMALL_STACK_STATIC const byte test1Entropy[] =
  15596. {
  15597. 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
  15598. 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
  15599. 0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, 0x85, 0x81, 0xf9, 0x31,
  15600. 0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d, 0xdb, 0xcb, 0xcc, 0x2e
  15601. };
  15602. WOLFSSL_SMALL_STACK_STATIC const byte test1Output[] =
  15603. {
  15604. 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
  15605. 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
  15606. 0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
  15607. 0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
  15608. 0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
  15609. 0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
  15610. 0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
  15611. 0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
  15612. 0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
  15613. 0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
  15614. 0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
  15615. };
  15616. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyA[] =
  15617. {
  15618. 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
  15619. 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
  15620. 0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
  15621. 0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
  15622. };
  15623. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyB[] =
  15624. {
  15625. 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
  15626. 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
  15627. 0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
  15628. };
  15629. WOLFSSL_SMALL_STACK_STATIC const byte test2Output[] =
  15630. {
  15631. 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
  15632. 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
  15633. 0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
  15634. 0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
  15635. 0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
  15636. 0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
  15637. 0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
  15638. 0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
  15639. 0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
  15640. 0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
  15641. 0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
  15642. };
  15643. byte output[WC_SHA256_DIGEST_SIZE * 4];
  15644. wc_test_ret_t ret;
  15645. WOLFSSL_ENTER("random_test");
  15646. ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
  15647. output, sizeof(output));
  15648. if (ret != 0)
  15649. return WC_TEST_RET_ENC_EC(ret);
  15650. if (XMEMCMP(test1Output, output, sizeof(output)) != 0)
  15651. return WC_TEST_RET_ENC_NC;
  15652. ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
  15653. test2EntropyB, sizeof(test2EntropyB),
  15654. output, sizeof(output));
  15655. if (ret != 0)
  15656. return WC_TEST_RET_ENC_EC(ret);
  15657. if (XMEMCMP(test2Output, output, sizeof(output)) != 0)
  15658. return WC_TEST_RET_ENC_NC;
  15659. /* Basic RNG generate block test */
  15660. if ((ret = random_rng_test()) != 0)
  15661. return ret;
  15662. /* Test the seed check function. */
  15663. #if !(defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) || \
  15664. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  15665. {
  15666. word32 i, outputSz;
  15667. /* Repeat the same byte over and over. Should fail. */
  15668. outputSz = sizeof(output);
  15669. XMEMSET(output, 1, outputSz);
  15670. ret = wc_RNG_TestSeed(output, outputSz);
  15671. if (ret == 0)
  15672. return WC_TEST_RET_ENC_NC;
  15673. /* Every byte of the entropy scratch is different,
  15674. * entropy is a single byte that shouldn't match. */
  15675. outputSz = (sizeof(output) / 2) + 1;
  15676. for (i = 0; i < outputSz; i++)
  15677. output[i] = (byte)i;
  15678. ret = wc_RNG_TestSeed(output, outputSz);
  15679. if (ret != 0)
  15680. return WC_TEST_RET_ENC_EC(ret);
  15681. outputSz = sizeof(output);
  15682. for (i = 0; i < outputSz; i++)
  15683. output[i] = (byte)i;
  15684. ret = wc_RNG_TestSeed(output, outputSz);
  15685. if (ret != 0)
  15686. return WC_TEST_RET_ENC_EC(ret);
  15687. }
  15688. #endif
  15689. /* Test the seed callback. */
  15690. #ifdef WC_RNG_SEED_CB
  15691. if ((ret = rng_seed_test()) != 0)
  15692. return ret;
  15693. #endif
  15694. return 0;
  15695. }
  15696. #else
  15697. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_test(void)
  15698. {
  15699. WOLFSSL_ENTER("random_test");
  15700. /* Basic RNG generate block test */
  15701. return random_rng_test();
  15702. }
  15703. #endif /* HAVE_HASHDRBG && !CUSTOM_RAND_GENERATE_BLOCK */
  15704. #endif /* WC_NO_RNG */
  15705. #ifndef MEM_TEST_SZ
  15706. #define MEM_TEST_SZ 1024
  15707. #endif
  15708. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  15709. static int simple_mem_test(int sz)
  15710. {
  15711. int ret = 0;
  15712. byte* b;
  15713. int i;
  15714. b = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15715. if (b == NULL) {
  15716. return WC_TEST_RET_ENC_NC;
  15717. }
  15718. /* utilize memory */
  15719. for (i = 0; i < sz; i++) {
  15720. b[i] = (byte)i;
  15721. }
  15722. /* read back and verify */
  15723. for (i = 0; i < sz; i++) {
  15724. if (b[i] != (byte)i) {
  15725. ret = WC_TEST_RET_ENC_NC;
  15726. break;
  15727. }
  15728. }
  15729. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15730. return ret;
  15731. }
  15732. #endif
  15733. /* If successful, returns the first letter of the byte array `in`.
  15734. **
  15735. ** This is a deceptively simple test of a read-only embedded Linux file system.
  15736. ** (e.g CFLAGS `-mfdpic` and `-mforce-l32`) for Xtensa Linux ESP32. When the
  15737. ** `-mforce-l32` is missing, access to `in` will fail with Illegal Instruction.
  15738. ** Const is on read-only memory-mapped file system, *not* loaded in app memory.
  15739. **
  15740. ** Edit with caution. See PR #6523. */
  15741. static wc_test_ret_t const_byte_ptr_test(const byte* in, word32 *outJ)
  15742. {
  15743. wc_test_ret_t ret = 0;
  15744. volatile word32 j = (word32)-1; /* must be volatile to properly detect error */
  15745. ret = (wc_test_ret_t)*in; /* accessed *in value. */
  15746. (void)ret;
  15747. j = *outJ; /* Found index to use in const array. */
  15748. if (j == 0) {
  15749. #ifdef WOLFSSL_DEBUG
  15750. printf("Testing const byte ptr reference...\n");
  15751. #endif
  15752. /* although j is zero, in[0] does not detect the Illegal instruction */
  15753. ret = in[j]; /* The big test: can we actually access the `in` data? */
  15754. }
  15755. else {
  15756. ret = WC_TEST_RET_ENC_I(j);
  15757. }
  15758. return ret;
  15759. }
  15760. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memory_test(void)
  15761. {
  15762. wc_test_ret_t ret = 0;
  15763. word32 j = 0; /* used in embedded const pointer test */
  15764. WOLFSSL_ENTER("memory_test");
  15765. #if defined(COMPLEX_MEM_TEST) || defined(WOLFSSL_STATIC_MEMORY)
  15766. int i;
  15767. #endif
  15768. #ifdef WOLFSSL_STATIC_MEMORY
  15769. word32 size[] = { WOLFMEM_BUCKETS };
  15770. word32 dist[] = { WOLFMEM_DIST };
  15771. byte buffer[30000]; /* make large enough to involve many bucket sizes */
  15772. int pad = -(int)((wc_ptr_t)buffer) & (WOLFSSL_STATIC_ALIGN - 1);
  15773. /* pad to account for if head of buffer is not at set memory
  15774. * alignment when tests are ran */
  15775. #endif
  15776. #ifdef WOLFSSL_STATIC_MEMORY
  15777. /* check macro settings */
  15778. if (sizeof(size)/sizeof(word32) != WOLFMEM_DEF_BUCKETS) {
  15779. return WC_TEST_RET_ENC_NC;
  15780. }
  15781. if (sizeof(dist)/sizeof(word32) != WOLFMEM_DEF_BUCKETS) {
  15782. return WC_TEST_RET_ENC_NC;
  15783. }
  15784. if (WOLFMEM_DEF_BUCKETS > WOLFMEM_MAX_BUCKETS) {
  15785. return WC_TEST_RET_ENC_NC;
  15786. }
  15787. for (i = 0; i < WOLFMEM_DEF_BUCKETS; i++) {
  15788. if ((size[i] % WOLFSSL_STATIC_ALIGN) != 0) {
  15789. /* each element in array should be divisible by alignment size */
  15790. return WC_TEST_RET_ENC_NC;
  15791. }
  15792. }
  15793. for (i = 1; i < WOLFMEM_DEF_BUCKETS; i++) {
  15794. if (size[i - 1] >= size[i]) {
  15795. return WC_TEST_RET_ENC_NC; /* sizes should be in increasing order */
  15796. }
  15797. }
  15798. #ifndef WOLFSSL_STATIC_MEMORY_LEAN
  15799. /* check that padding size returned is possible */
  15800. if (wolfSSL_MemoryPaddingSz() < WOLFSSL_STATIC_ALIGN) {
  15801. return WC_TEST_RET_ENC_NC; /* no room for wc_Memory struct */
  15802. }
  15803. ret = wolfSSL_MemoryPaddingSz();
  15804. if (ret < 0) {
  15805. return WC_TEST_RET_ENC_EC(ret);
  15806. }
  15807. if (wolfSSL_MemoryPaddingSz() % WOLFSSL_STATIC_ALIGN != 0) {
  15808. return WC_TEST_RET_ENC_NC; /* not aligned! */
  15809. }
  15810. /* check function to return optimum buffer size (rounded down) */
  15811. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_GENERAL);
  15812. if (ret < 0) {
  15813. return WC_TEST_RET_ENC_EC(ret);
  15814. }
  15815. if ((ret - pad) % WOLFSSL_STATIC_ALIGN != 0) {
  15816. return WC_TEST_RET_ENC_NC; /* not aligned! */
  15817. }
  15818. if ((unsigned int)ret > sizeof(buffer)) {
  15819. return WC_TEST_RET_ENC_NC; /* did not round down as expected */
  15820. }
  15821. if (ret != wolfSSL_StaticBufferSz(buffer, ret, WOLFMEM_GENERAL)) {
  15822. return WC_TEST_RET_ENC_NC; /* return value changed when using suggested
  15823. * value
  15824. */
  15825. }
  15826. ret = wolfSSL_MemoryPaddingSz();
  15827. ret += pad; /* add space that is going to be needed if buffer not aligned */
  15828. if (wolfSSL_StaticBufferSz(buffer, size[0] + ret + 1, WOLFMEM_GENERAL) !=
  15829. (ret + (int)size[0])) {
  15830. return WC_TEST_RET_ENC_NC; /* did not round down to nearest bucket
  15831. * value
  15832. */
  15833. }
  15834. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_IO_POOL);
  15835. if ((ret - pad) < 0) {
  15836. return WC_TEST_RET_ENC_NC;
  15837. }
  15838. if (((ret - pad) % (WOLFMEM_IO_SZ + wolfSSL_MemoryPaddingSz())) != 0) {
  15839. return WC_TEST_RET_ENC_NC; /* not even chunks of memory for IO size */
  15840. }
  15841. if (((ret - pad) % WOLFSSL_STATIC_ALIGN) != 0) {
  15842. return WC_TEST_RET_ENC_NC; /* memory not aligned */
  15843. }
  15844. /* check for passing bad or unknown arguments to functions */
  15845. if (wolfSSL_StaticBufferSz(NULL, 1, WOLFMEM_GENERAL) > 0) {
  15846. return WC_TEST_RET_ENC_NC;
  15847. }
  15848. if (wolfSSL_StaticBufferSz(buffer, 1, WOLFMEM_GENERAL) != 0) {
  15849. return WC_TEST_RET_ENC_NC; /* should round to 0
  15850. since struct + bucket will not fit */
  15851. }
  15852. #endif
  15853. (void)pad;
  15854. (void)dist; /* avoid static analysis warning of variable not used */
  15855. #endif
  15856. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  15857. /* simple test */
  15858. ret = simple_mem_test(MEM_TEST_SZ);
  15859. if (ret != 0)
  15860. return ret;
  15861. #endif
  15862. #ifdef COMPLEX_MEM_TEST
  15863. /* test various size blocks */
  15864. for (i = 1; i < MEM_TEST_SZ; i*=2) {
  15865. ret = simple_mem_test(i);
  15866. if (ret != 0)
  15867. return ret;
  15868. }
  15869. #endif
  15870. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC) && defined(XREALLOC)
  15871. /* realloc test */
  15872. {
  15873. byte *b = (byte*)XMALLOC(MEM_TEST_SZ, HEAP_HINT,
  15874. DYNAMIC_TYPE_TMP_BUFFER);
  15875. #ifndef WOLFSSL_NO_REALLOC
  15876. byte *c = NULL;
  15877. if (b) {
  15878. c = (byte*)XREALLOC(b, MEM_TEST_SZ+sizeof(word32), HEAP_HINT,
  15879. DYNAMIC_TYPE_TMP_BUFFER);
  15880. if (c)
  15881. b = c;
  15882. }
  15883. #endif
  15884. if (b)
  15885. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15886. if ((b == NULL)
  15887. #ifndef WOLFSSL_NO_REALLOC
  15888. || (c == NULL)
  15889. #endif
  15890. ) {
  15891. return WC_TEST_RET_ENC_ERRNO;
  15892. }
  15893. }
  15894. #endif
  15895. if (ret == 0) {
  15896. /* This test is only interesting on embedded R/O Flash systems */
  15897. if (const_byte_ptr_test(const_byte_array, &j) != CBPTR_EXPECTED) {
  15898. ret = 1;
  15899. }
  15900. }
  15901. return ret;
  15902. }
  15903. #ifndef NO_FILESYSTEM
  15904. /* Cert Paths */
  15905. #ifdef FREESCALE_MQX
  15906. #define CERT_PREFIX "a:\\"
  15907. #define CERT_PATH_SEP "\\"
  15908. #elif defined(WOLFSSL_uTKERNEL2)
  15909. #define CERT_PREFIX "/uda/"
  15910. #define CERT_PATH_SEP "/"
  15911. #elif defined(_WIN32_WCE)
  15912. #define CERT_PREFIX "\\windows\\"
  15913. #define CERT_PATH_SEP "\\"
  15914. #endif
  15915. #ifndef CERT_PREFIX
  15916. #define CERT_PREFIX "./"
  15917. #endif
  15918. #ifndef CERT_PATH_SEP
  15919. #define CERT_PATH_SEP "/"
  15920. #endif
  15921. #ifndef CERT_WRITE_TEMP_DIR
  15922. #define CERT_WRITE_TEMP_DIR CERT_PREFIX
  15923. #endif
  15924. #define CERT_ROOT CERT_PREFIX "certs" CERT_PATH_SEP
  15925. /* Generated Test Certs */
  15926. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  15927. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  15928. #if !defined(NO_RSA) && !defined(NO_ASN)
  15929. static const char* clientKey = CERT_ROOT "client-key.der";
  15930. static const char* clientCert = CERT_ROOT "client-cert.der";
  15931. #ifdef WOLFSSL_CERT_EXT
  15932. static const char* clientKeyPub = CERT_ROOT "client-keyPub.der";
  15933. #endif
  15934. #endif /* !NO_RSA && !NO_ASN */
  15935. #endif
  15936. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  15937. #if !defined(NO_RSA) && !defined(NO_ASN)
  15938. #if defined(WOLFSSL_CERT_GEN) || defined(HAVE_PKCS7)
  15939. static const char* rsaCaKeyFile = CERT_ROOT "ca-key.der";
  15940. #ifdef WOLFSSL_CERT_GEN
  15941. static const char* rsaCaCertFile = CERT_ROOT "ca-cert.pem";
  15942. #endif
  15943. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  15944. static const char* rsaCaCertDerFile = CERT_ROOT "ca-cert.der";
  15945. #endif
  15946. #ifdef HAVE_PKCS7
  15947. static const char* rsaServerCertDerFile =
  15948. CERT_ROOT "server-cert.der";
  15949. static const char* rsaServerKeyDerFile =
  15950. CERT_ROOT "server-key.der";
  15951. #endif
  15952. #endif
  15953. #endif /* !NO_RSA && !NO_ASN */
  15954. #endif /* !USE_CERT_BUFFER_* */
  15955. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  15956. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  15957. #if !defined(NO_ASN) && !defined(NO_DH)
  15958. static const char* dhParamsFile = CERT_ROOT "dh2048.der";
  15959. #endif
  15960. #endif
  15961. #if !defined(NO_ASN) && !defined(NO_DH)
  15962. #if defined(WOLFSSL_DH_EXTRA) && (!defined(HAVE_FIPS) || \
  15963. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  15964. #if !defined(USE_CERT_BUFFERS_2048)
  15965. static const char* dhKeyFile = CERT_ROOT "statickeys/dh-ffdhe2048.der";
  15966. static const char* dhKeyPubFile = CERT_ROOT "statickeys/dh-ffdhe2048-pub.der";
  15967. #endif
  15968. #endif
  15969. #endif
  15970. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  15971. #ifndef NO_DSA
  15972. static const char* dsaKey = CERT_ROOT "dsa2048.der";
  15973. #endif
  15974. #endif /* !USE_CERT_BUFFER_* */
  15975. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ECC256)
  15976. #ifdef HAVE_ECC
  15977. /* cert files to be used in rsa cert gen test, check if RSA enabled */
  15978. #ifdef HAVE_ECC_KEY_IMPORT
  15979. static const char* eccKeyDerFile = CERT_ROOT "ecc-key.der";
  15980. #endif
  15981. #endif
  15982. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ASN)
  15983. #if defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN) && \
  15984. !defined(NO_ECC_SECP)
  15985. #ifndef NO_RSA
  15986. static const char* eccKeyPubFileDer = CERT_ROOT "ecc-keyPub.der";
  15987. #endif
  15988. #ifndef NO_ASN_TIME
  15989. static const char* eccCaKeyFile = CERT_ROOT "ca-ecc-key.der";
  15990. static const char* eccCaCertFile = CERT_ROOT "ca-ecc-cert.pem";
  15991. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  15992. static const char* eccCaKey384File =
  15993. CERT_ROOT "ca-ecc384-key.der";
  15994. static const char* eccCaCert384File =
  15995. CERT_ROOT "ca-ecc384-cert.pem";
  15996. #endif
  15997. #endif
  15998. #endif
  15999. #if defined(HAVE_PKCS7) && defined(HAVE_ECC)
  16000. static const char* eccClientKey = CERT_ROOT "ecc-client-key.der";
  16001. static const char* eccClientCert = CERT_ROOT "client-ecc-cert.der";
  16002. #endif
  16003. #endif /* HAVE_ECC */
  16004. #ifdef HAVE_ED25519
  16005. #ifdef WOLFSSL_TEST_CERT
  16006. static const char* serverEd25519Cert =
  16007. CERT_ROOT "ed25519/server-ed25519.der";
  16008. static const char* caEd25519Cert =
  16009. CERT_ROOT "ed25519/ca-ed25519.der";
  16010. #endif
  16011. #endif
  16012. #ifdef HAVE_ED448
  16013. #ifdef WOLFSSL_TEST_CERT
  16014. static const char* serverEd448Cert =
  16015. CERT_ROOT "ed448/server-ed448.der";
  16016. static const char* caEd448Cert = CERT_ROOT "ed448/ca-ed448.der";
  16017. #endif
  16018. #endif
  16019. #endif /* !USE_CERT_BUFFER_* */
  16020. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  16021. !defined(NO_FILESYSTEM)
  16022. static const char* certExtNc =
  16023. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nc.der";
  16024. static const char* certExtIa =
  16025. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-ia.der";
  16026. static const char* certExtNct =
  16027. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nct.der";
  16028. #ifndef WOLFSSL_ASN_INT_LEAD_0_ANY
  16029. static const char* certBadNegInt =
  16030. CERT_ROOT "test" CERT_PATH_SEP "cert-bad-neg-int.der";
  16031. #endif
  16032. static const char* certBadOid =
  16033. CERT_ROOT "test" CERT_PATH_SEP "cert-bad-oid.der";
  16034. #ifndef WOLFSSL_NO_ASN_STRICT
  16035. static const char* certBadUtf8 =
  16036. CERT_ROOT "test" CERT_PATH_SEP "cert-bad-utf8.der";
  16037. #endif
  16038. #endif
  16039. #ifndef NO_WRITE_TEMP_FILES
  16040. #ifdef HAVE_ECC
  16041. #ifndef NO_ECC_SECP
  16042. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  16043. static const char* certEccPemFile = CERT_WRITE_TEMP_DIR "certecc.pem";
  16044. static const char* certEccDerFile = CERT_WRITE_TEMP_DIR "certecc.der";
  16045. #endif
  16046. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  16047. static const char* certEccRsaPemFile = CERT_WRITE_TEMP_DIR "certeccrsa.pem";
  16048. static const char* certEccRsaDerFile = CERT_WRITE_TEMP_DIR "certeccrsa.der";
  16049. #endif
  16050. #endif
  16051. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  16052. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ASN_CRYPT)
  16053. static const char* eccCaKeyPemFile = CERT_WRITE_TEMP_DIR "ecc-key.pem";
  16054. static const char* eccPubKeyDerFile = CERT_WRITE_TEMP_DIR "ecc-public-key.der";
  16055. static const char* eccCaKeyTempFile = CERT_WRITE_TEMP_DIR "ecc-key.der";
  16056. #if defined(HAVE_PKCS8) && !defined(WC_NO_RNG) && \
  16057. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  16058. static const char* eccPkcs8KeyDerFile = CERT_WRITE_TEMP_DIR "ecc-key-pkcs8.der";
  16059. #endif
  16060. #endif /* HAVE_ECC_KEY_EXPORT */
  16061. #endif /* HAVE_ECC */
  16062. #ifndef NO_RSA
  16063. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  16064. static const char* otherCertDerFile = CERT_WRITE_TEMP_DIR "othercert.der";
  16065. static const char* certDerFile = CERT_WRITE_TEMP_DIR "cert.der";
  16066. static const char* otherCertPemFile = CERT_WRITE_TEMP_DIR "othercert.pem";
  16067. static const char* certPemFile = CERT_WRITE_TEMP_DIR "cert.pem";
  16068. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  16069. static const char* certReqDerFile = CERT_WRITE_TEMP_DIR "certreq.der";
  16070. static const char* certReqPemFile = CERT_WRITE_TEMP_DIR "certreq.pem";
  16071. #endif
  16072. #endif
  16073. #endif /* !NO_RSA */
  16074. #if !defined(NO_RSA) || !defined(NO_DSA)
  16075. #ifdef WOLFSSL_KEY_GEN
  16076. static const char* keyDerFile = CERT_WRITE_TEMP_DIR "key.der";
  16077. static const char* keyPemFile = CERT_WRITE_TEMP_DIR "key.pem";
  16078. #endif
  16079. #endif
  16080. #endif /* !NO_WRITE_TEMP_FILES */
  16081. #endif /* !NO_FILESYSTEM */
  16082. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  16083. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  16084. static CertName certDefaultName;
  16085. static void initDefaultName(void)
  16086. {
  16087. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  16088. NameAttrib* n;
  16089. #endif
  16090. XMEMCPY(certDefaultName.country, "US", sizeof("US"));
  16091. certDefaultName.countryEnc = CTC_PRINTABLE;
  16092. XMEMCPY(certDefaultName.state, "Oregon", sizeof("Oregon"));
  16093. certDefaultName.stateEnc = CTC_UTF8;
  16094. XMEMCPY(certDefaultName.street, "Main St", sizeof("Main St"));
  16095. certDefaultName.streetEnc = CTC_UTF8;
  16096. XMEMCPY(certDefaultName.locality, "Portland", sizeof("Portland"));
  16097. certDefaultName.localityEnc = CTC_UTF8;
  16098. XMEMCPY(certDefaultName.sur, "Test", sizeof("Test"));
  16099. certDefaultName.surEnc = CTC_UTF8;
  16100. XMEMCPY(certDefaultName.org, "wolfSSL", sizeof("wolfSSL"));
  16101. certDefaultName.orgEnc = CTC_UTF8;
  16102. XMEMCPY(certDefaultName.unit, "Development", sizeof("Development"));
  16103. certDefaultName.unitEnc = CTC_UTF8;
  16104. XMEMCPY(certDefaultName.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  16105. certDefaultName.commonNameEnc = CTC_UTF8;
  16106. XMEMCPY(certDefaultName.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  16107. certDefaultName.serialDevEnc = CTC_PRINTABLE;
  16108. XMEMCPY(certDefaultName.postalCode, "12-456", sizeof("12-456"));
  16109. certDefaultName.postalCodeEnc = CTC_PRINTABLE;
  16110. #ifdef WOLFSSL_CERT_EXT
  16111. XMEMCPY(certDefaultName.busCat, "Private Organization", sizeof("Private Organization"));
  16112. certDefaultName.busCatEnc = CTC_UTF8;
  16113. XMEMCPY(certDefaultName.joiSt, "US", sizeof("US"));
  16114. certDefaultName.joiStEnc = CTC_PRINTABLE;
  16115. XMEMCPY(certDefaultName.joiC, "Oregon", sizeof("Oregon"));
  16116. certDefaultName.joiCEnc = CTC_PRINTABLE;
  16117. #endif
  16118. XMEMCPY(certDefaultName.email, "info@wolfssl.com", sizeof("info@wolfssl.com"));
  16119. XMEMCPY(certDefaultName.userId, "TestUserID", sizeof("TestUserID"));
  16120. certDefaultName.userIdEnc = CTC_PRINTABLE;
  16121. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  16122. /* test having additional OUs and setting DC */
  16123. n = &certDefaultName.name[0];
  16124. n->id = ASN_ORGUNIT_NAME;
  16125. n->type = CTC_UTF8;
  16126. n->sz = XSTRLEN("Development-2");
  16127. XMEMCPY(n->value, "Development-2", sizeof("Development-2"));
  16128. #if CTC_MAX_ATTRIB > 3
  16129. n = &certDefaultName.name[1];
  16130. n->id = ASN_DOMAIN_COMPONENT;
  16131. n->type = CTC_UTF8;
  16132. n->sz = XSTRLEN("com");
  16133. XMEMCPY(n->value, "com", sizeof("com"));
  16134. n = &certDefaultName.name[2];
  16135. n->id = ASN_DOMAIN_COMPONENT;
  16136. n->type = CTC_UTF8;
  16137. n->sz = XSTRLEN("wolfssl");
  16138. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  16139. #endif
  16140. #endif /* WOLFSSL_MULTI_ATTRIB && WOLFSSL_TEST_CERT */
  16141. #ifdef WOLFSSL_CUSTOM_OID
  16142. /* TODO: Add test case for custom OID's */
  16143. #endif
  16144. }
  16145. #ifdef WOLFSSL_CERT_EXT
  16146. #if ((defined(HAVE_ED25519) || defined(HAVE_ED448)) && \
  16147. defined(WOLFSSL_TEST_CERT)) || defined(HAVE_ECC)
  16148. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage[] =
  16149. "digitalSignature,nonRepudiation";
  16150. #endif
  16151. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_ASN_TIME)
  16152. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage2[] =
  16153. "digitalSignature,nonRepudiation,keyEncipherment,keyAgreement";
  16154. #endif
  16155. #endif /* WOLFSSL_CERT_EXT */
  16156. #endif /* WOLFSSL_CERT_GEN */
  16157. #ifndef NO_RSA
  16158. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  16159. !defined(NO_FILESYSTEM)
  16160. static const byte minSerial[] = { 0x02, 0x01, 0x01 };
  16161. static const byte minName[] = { 0x30, 0x00 };
  16162. static const byte nameBad[] = {
  16163. 0x30, 0x08,
  16164. 0x31, 0x06,
  16165. 0x30, 0x04,
  16166. 0x06, 0x02,
  16167. 0x55, 0x04,
  16168. };
  16169. static const byte minDates[] = {
  16170. 0x30, 0x1e,
  16171. 0x17, 0x0d,
  16172. 0x31, 0x38, 0x30, 0x34, 0x31, 0x33, 0x31, 0x35,
  16173. 0x32, 0x33, 0x31, 0x30, 0x5a,
  16174. 0x17, 0x0d,
  16175. 0x32, 0x31, 0x30, 0x31, 0x30, 0x37, 0x31, 0x35,
  16176. 0x32, 0x33, 0x31, 0x30, 0x5a
  16177. };
  16178. static const byte minPubKey[] = {
  16179. 0x30, 0x1c,
  16180. 0x30, 0x0d,
  16181. 0x06, 0x09,
  16182. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  16183. 0x01,
  16184. 0x05, 0x00,
  16185. 0x03, 0x0b,
  16186. 0x00, 0x30, 0x08,
  16187. 0x02, 0x01,
  16188. 0x03,
  16189. 0x02, 0x03,
  16190. 0x01, 0x00, 0x01
  16191. };
  16192. static const byte minSigAlg[] = {
  16193. 0x30, 0x0d,
  16194. 0x06, 0x09,
  16195. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  16196. 0x0b,
  16197. 0x05, 0x00
  16198. };
  16199. static const byte minSig[] = {
  16200. 0x03, 0x01,
  16201. 0x00
  16202. };
  16203. static int add_seq(byte* certData, int offset, byte* data, byte length)
  16204. {
  16205. XMEMMOVE(certData + offset + 2, data, length);
  16206. certData[offset++] = 0x30;
  16207. certData[offset++] = length;
  16208. return offset + length;
  16209. }
  16210. static int add_data(byte* certData, int offset, const byte* data, byte length)
  16211. {
  16212. XMEMCPY(certData + offset, data, length);
  16213. return offset + length;
  16214. }
  16215. static wc_test_ret_t cert_asn1_test(void)
  16216. {
  16217. wc_test_ret_t ret;
  16218. int len[3];
  16219. DecodedCert cert;
  16220. byte certData[114];
  16221. byte* badCert = NULL;
  16222. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  16223. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  16224. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  16225. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  16226. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  16227. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  16228. len[1] = add_seq(certData, 0, certData, len[2]);
  16229. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  16230. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  16231. len[0] = add_seq(certData, 0, certData, len[1]);
  16232. /* Minimal good certificate */
  16233. InitDecodedCert(&cert, certData, len[0], 0);
  16234. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  16235. FreeDecodedCert(&cert);
  16236. if (ret != 0)
  16237. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16238. /* Bad issuer name */
  16239. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  16240. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  16241. len[2] = add_data(certData, len[2], nameBad, (byte)sizeof(nameBad));
  16242. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  16243. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  16244. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  16245. len[1] = add_seq(certData, 0, certData, len[2]);
  16246. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  16247. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  16248. len[0] = add_seq(certData, 0, certData, len[1]);
  16249. /* Put data into allocated buffer to allow access error checking. */
  16250. badCert = (byte*)XMALLOC(len[0], HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16251. XMEMCPY(badCert, certData, len[0]);
  16252. InitDecodedCert(&cert, badCert, len[0], 0);
  16253. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  16254. FreeDecodedCert(&cert);
  16255. if (ret != ASN_PARSE_E) {
  16256. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16257. }
  16258. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16259. badCert = NULL;
  16260. ret = 0;
  16261. done:
  16262. if (badCert != NULL)
  16263. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16264. return ret;
  16265. }
  16266. static wc_test_ret_t cert_load_bad(const char* fname, byte* tmp, int err)
  16267. {
  16268. wc_test_ret_t ret;
  16269. DecodedCert cert;
  16270. XFILE file;
  16271. size_t bytes;
  16272. if ((fname == NULL) || (tmp == NULL)) {
  16273. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  16274. }
  16275. file = XFOPEN(fname, "rb");
  16276. if (!file) {
  16277. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16278. }
  16279. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16280. XFCLOSE(file);
  16281. if (bytes == 0) {
  16282. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16283. }
  16284. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16285. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  16286. FreeDecodedCert(&cert);
  16287. if (ret != err) {
  16288. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16289. }
  16290. ret = 0;
  16291. done:
  16292. return ret;
  16293. }
  16294. static wc_test_ret_t cert_bad_asn1_test(void)
  16295. {
  16296. wc_test_ret_t ret = 0;
  16297. byte* tmp;
  16298. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16299. if (tmp == NULL) {
  16300. ret = WC_TEST_RET_ENC_ERRNO;
  16301. }
  16302. #ifndef WOLFSSL_ASN_INT_LEAD_0_ANY
  16303. if (ret == 0) {
  16304. /* Serial number: 0xff 0xa8. 0xff and top bit set on next byte invalid.
  16305. */
  16306. ret = cert_load_bad(certBadNegInt, tmp, ASN_EXPECT_0_E);
  16307. }
  16308. #endif
  16309. if (ret == 0) {
  16310. /* Subject name OID: 55 04 f4. Last byte with top bit set invalid. */
  16311. ret = cert_load_bad(certBadOid, tmp, ASN_PARSE_E);
  16312. }
  16313. #ifndef WOLFSSL_NO_ASN_STRICT
  16314. if (ret == 0) {
  16315. /* Issuer name UTF8STRING: df 52 4e 44. Top bit of second byte not set.
  16316. */
  16317. ret = cert_load_bad(certBadUtf8, tmp, ASN_PARSE_E);
  16318. }
  16319. #endif
  16320. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16321. return ret;
  16322. }
  16323. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cert_test(void)
  16324. {
  16325. #if !defined(NO_FILESYSTEM)
  16326. DecodedCert cert;
  16327. byte* tmp;
  16328. size_t bytes;
  16329. XFILE file;
  16330. wc_test_ret_t ret;
  16331. WOLFSSL_ENTER("cert_test");
  16332. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16333. if (tmp == NULL)
  16334. return WC_TEST_RET_ENC_ERRNO;
  16335. /* Certificate with Name Constraints extension. */
  16336. file = XFOPEN(certExtNc, "rb");
  16337. if (!file) {
  16338. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16339. }
  16340. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16341. XFCLOSE(file);
  16342. if (bytes == 0)
  16343. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16344. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16345. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  16346. if (ret != 0)
  16347. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16348. FreeDecodedCert(&cert);
  16349. /* Certificate with Inhibit Any Policy extension. */
  16350. file = XFOPEN(certExtIa, "rb");
  16351. if (!file) {
  16352. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16353. }
  16354. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16355. XFCLOSE(file);
  16356. if (bytes == 0)
  16357. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16358. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16359. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  16360. if (ret != 0)
  16361. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16362. FreeDecodedCert(&cert);
  16363. /* Certificate with Netscape Certificate Type extension. */
  16364. file = XFOPEN(certExtNct, "rb");
  16365. if (!file) {
  16366. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16367. }
  16368. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16369. XFCLOSE(file);
  16370. if (bytes == 0)
  16371. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16372. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16373. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  16374. #ifndef IGNORE_NETSCAPE_CERT_TYPE
  16375. if (ret != 0)
  16376. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16377. #else
  16378. if (ret != ASN_CRIT_EXT_E) {
  16379. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16380. }
  16381. ret = 0;
  16382. #endif
  16383. done:
  16384. FreeDecodedCert(&cert);
  16385. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16386. #endif /* !NO_FILESYSTEM */
  16387. if (ret == 0)
  16388. ret = cert_asn1_test();
  16389. if (ret == 0)
  16390. ret = cert_bad_asn1_test();
  16391. return ret;
  16392. }
  16393. #endif /* WOLFSSL_TEST_CERT */
  16394. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  16395. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  16396. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certext_test(void)
  16397. {
  16398. DecodedCert cert;
  16399. byte* tmp;
  16400. size_t bytes;
  16401. XFILE file;
  16402. wc_test_ret_t ret;
  16403. /* created from rsa_test : othercert.der */
  16404. byte skid_rsa[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  16405. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  16406. /* created from rsa_test : othercert.der */
  16407. byte akid_rsa[] = "\x27\x8E\x67\x11\x74\xC3\x26\x1D\x3F\xED"
  16408. "\x33\x63\xB3\xA4\xD8\x1D\x30\xE5\xE8\xD5";
  16409. #ifdef HAVE_ECC
  16410. /* created from ecc_test_cert_gen : certecc.der */
  16411. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  16412. /* Authority key id from ./certs/ca-ecc384-cert.pem */
  16413. byte akid_ecc[] = "\xAB\xE0\xC3\x26\x4C\x18\xD4\x72\xBB\xD2"
  16414. "\x84\x8C\x9C\x0A\x05\x92\x80\x12\x53\x52";
  16415. #else
  16416. /* Authority key id from ./certs/ca-ecc-cert.pem */
  16417. byte akid_ecc[] = "\x56\x8E\x9A\xC3\xF0\x42\xDE\x18\xB9\x45"
  16418. "\x55\x6E\xF9\x93\xCF\xEA\xC3\xF3\xA5\x21";
  16419. #endif
  16420. #endif /* HAVE_ECC */
  16421. /* created from rsa_test : cert.der */
  16422. byte kid_ca[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  16423. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  16424. WOLFSSL_ENTER("certext_test");
  16425. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16426. if (tmp == NULL)
  16427. return WC_TEST_RET_ENC_ERRNO;
  16428. /* load othercert.der (Cert signed by an authority) */
  16429. file = XFOPEN(otherCertDerFile, "rb");
  16430. if (!file) {
  16431. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16432. return WC_TEST_RET_ENC_ERRNO;
  16433. }
  16434. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16435. XFCLOSE(file);
  16436. if (bytes == 0)
  16437. return WC_TEST_RET_ENC_ERRNO;
  16438. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16439. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  16440. if (ret != 0)
  16441. return WC_TEST_RET_ENC_EC(ret);
  16442. /* check the SKID from a RSA certificate */
  16443. if (XMEMCMP(skid_rsa, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  16444. return WC_TEST_RET_ENC_NC;
  16445. /* check the AKID from an RSA certificate */
  16446. if (XMEMCMP(akid_rsa, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  16447. return WC_TEST_RET_ENC_NC;
  16448. /* check the Key Usage from an RSA certificate */
  16449. if (!cert.extKeyUsageSet)
  16450. return WC_TEST_RET_ENC_NC;
  16451. if (cert.extKeyUsage != (KEYUSE_KEY_ENCIPHER|KEYUSE_KEY_AGREE))
  16452. return WC_TEST_RET_ENC_NC;
  16453. /* check the CA Basic Constraints from an RSA certificate */
  16454. if (cert.isCA)
  16455. return WC_TEST_RET_ENC_NC;
  16456. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  16457. /* check the Certificate Policies Id */
  16458. if (cert.extCertPoliciesNb != 1)
  16459. return WC_TEST_RET_ENC_NC;
  16460. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  16461. return WC_TEST_RET_ENC_NC;
  16462. #endif
  16463. FreeDecodedCert(&cert);
  16464. #ifdef HAVE_ECC
  16465. /* load certecc.der (Cert signed by our ECC CA test in ecc_test_cert_gen) */
  16466. file = XFOPEN(certEccDerFile, "rb");
  16467. if (!file) {
  16468. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16469. return WC_TEST_RET_ENC_ERRNO;
  16470. }
  16471. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16472. XFCLOSE(file);
  16473. if (bytes == 0)
  16474. return WC_TEST_RET_ENC_ERRNO;
  16475. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16476. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  16477. if (ret != 0)
  16478. return WC_TEST_RET_ENC_EC(ret);
  16479. /* check the SKID from a ECC certificate - generated dynamically */
  16480. /* check the AKID from an ECC certificate */
  16481. if (XMEMCMP(akid_ecc, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  16482. return WC_TEST_RET_ENC_NC;
  16483. /* check the Key Usage from an ECC certificate */
  16484. if (!cert.extKeyUsageSet)
  16485. return WC_TEST_RET_ENC_NC;
  16486. if (cert.extKeyUsage != (KEYUSE_DIGITAL_SIG|KEYUSE_CONTENT_COMMIT))
  16487. return WC_TEST_RET_ENC_NC;
  16488. /* check the CA Basic Constraints from an ECC certificate */
  16489. if (cert.isCA)
  16490. return WC_TEST_RET_ENC_NC;
  16491. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  16492. /* check the Certificate Policies Id */
  16493. if (cert.extCertPoliciesNb != 2)
  16494. return WC_TEST_RET_ENC_NC;
  16495. if (strncmp(cert.extCertPolicies[0], "2.4.589440.587.101.2.1.9632587.1", 32))
  16496. return WC_TEST_RET_ENC_NC;
  16497. if (strncmp(cert.extCertPolicies[1], "1.2.13025.489.1.113549", 22))
  16498. return WC_TEST_RET_ENC_NC;
  16499. #endif
  16500. FreeDecodedCert(&cert);
  16501. #endif /* HAVE_ECC */
  16502. /* load cert.der (self signed certificate) */
  16503. file = XFOPEN(certDerFile, "rb");
  16504. if (!file) {
  16505. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16506. return WC_TEST_RET_ENC_ERRNO;
  16507. }
  16508. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16509. XFCLOSE(file);
  16510. if (bytes == 0)
  16511. return WC_TEST_RET_ENC_ERRNO;
  16512. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16513. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  16514. if (ret != 0)
  16515. return WC_TEST_RET_ENC_EC(ret);
  16516. /* check the SKID from a CA certificate */
  16517. if (XMEMCMP(kid_ca, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  16518. return WC_TEST_RET_ENC_NC;
  16519. /* check the AKID from an CA certificate */
  16520. if (XMEMCMP(kid_ca, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  16521. return WC_TEST_RET_ENC_NC;
  16522. /* check the Key Usage from CA certificate */
  16523. if (!cert.extKeyUsageSet)
  16524. return WC_TEST_RET_ENC_NC;
  16525. if (cert.extKeyUsage != (KEYUSE_KEY_CERT_SIGN|KEYUSE_CRL_SIGN))
  16526. return WC_TEST_RET_ENC_NC;
  16527. /* check the CA Basic Constraints CA certificate */
  16528. if (!cert.isCA)
  16529. return WC_TEST_RET_ENC_NC;
  16530. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  16531. /* check the Certificate Policies Id */
  16532. if (cert.extCertPoliciesNb != 2)
  16533. return WC_TEST_RET_ENC_NC;
  16534. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  16535. return WC_TEST_RET_ENC_NC;
  16536. if (strncmp(cert.extCertPolicies[1], "1.2.840.113549.1.9.16.6.5", 25))
  16537. return WC_TEST_RET_ENC_NC;
  16538. #endif
  16539. FreeDecodedCert(&cert);
  16540. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16541. return 0;
  16542. }
  16543. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_TEST_CERT &&
  16544. !NO_FILESYSTEM && WOLFSSL_CERT_GEN */
  16545. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  16546. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  16547. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t decodedCertCache_test(void)
  16548. {
  16549. wc_test_ret_t ret = 0;
  16550. Cert cert;
  16551. FILE* file;
  16552. byte* der;
  16553. word32 derSz;
  16554. WOLFSSL_ENTER("decodedCertCache_test");
  16555. derSz = FOURK_BUF;
  16556. der = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16557. if (der == NULL)
  16558. ret = WC_TEST_RET_ENC_NC;
  16559. if (ret == 0) {
  16560. /* load cert.der */
  16561. file = XFOPEN(certDerFile, "rb");
  16562. if (file != NULL) {
  16563. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  16564. XFCLOSE(file);
  16565. if (derSz == 0)
  16566. ret = WC_TEST_RET_ENC_ERRNO;
  16567. }
  16568. else
  16569. ret = WC_TEST_RET_ENC_ERRNO;
  16570. }
  16571. if (ret == 0) {
  16572. ret = wc_InitCert_ex(&cert, HEAP_HINT, devId);
  16573. if (ret != 0)
  16574. ret = WC_TEST_RET_ENC_EC(ret);
  16575. }
  16576. if (ret == 0) {
  16577. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  16578. if (ret != 0)
  16579. ret = WC_TEST_RET_ENC_EC(ret);
  16580. }
  16581. if (ret == 0) {
  16582. ret = wc_SetSubjectBuffer(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_SetSubjectRaw(&cert, der, derSz);
  16590. if (ret != 0)
  16591. ret = WC_TEST_RET_ENC_EC(ret);
  16592. }
  16593. if (ret == 0) {
  16594. ret = wc_SetSubjectRaw(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_SetIssuerBuffer(&cert, der, derSz);
  16602. if (ret != 0)
  16603. ret = WC_TEST_RET_ENC_EC(ret);
  16604. }
  16605. if (ret == 0) {
  16606. ret = wc_SetIssuerBuffer(NULL, der, derSz);
  16607. if (ret == BAD_FUNC_ARG)
  16608. ret = 0;
  16609. else
  16610. ret = WC_TEST_RET_ENC_EC(ret);
  16611. }
  16612. if (ret == 0) {
  16613. ret = wc_SetIssuerRaw(&cert, der, derSz);
  16614. if (ret != 0)
  16615. ret = WC_TEST_RET_ENC_EC(ret);
  16616. }
  16617. if (ret == 0) {
  16618. ret = wc_SetIssuerRaw(NULL, der, derSz);
  16619. if (ret == BAD_FUNC_ARG)
  16620. ret = 0;
  16621. else
  16622. ret = WC_TEST_RET_ENC_EC(ret);
  16623. }
  16624. #ifdef WOLFSSL_ALT_NAMES
  16625. if (ret == 0) {
  16626. ret = wc_SetAltNamesBuffer(&cert, der, derSz);
  16627. if (ret != 0)
  16628. ret = WC_TEST_RET_ENC_EC(ret);
  16629. }
  16630. if (ret == 0) {
  16631. ret = wc_SetAltNamesBuffer(NULL, der, derSz);
  16632. if (ret == BAD_FUNC_ARG)
  16633. ret = 0;
  16634. else
  16635. ret = WC_TEST_RET_ENC_EC(ret);
  16636. }
  16637. if (ret == 0) {
  16638. ret = wc_SetDatesBuffer(&cert, der, derSz);
  16639. if (ret != 0)
  16640. ret = WC_TEST_RET_ENC_EC(ret);
  16641. }
  16642. if (ret == 0) {
  16643. ret = wc_SetDatesBuffer(NULL, der, derSz);
  16644. if (ret == BAD_FUNC_ARG)
  16645. ret = 0;
  16646. else
  16647. ret = WC_TEST_RET_ENC_EC(ret);
  16648. }
  16649. #endif
  16650. if (ret == 0) {
  16651. ret = wc_SetAuthKeyIdFromCert(&cert, der, derSz);
  16652. if (ret != 0)
  16653. ret = WC_TEST_RET_ENC_EC(ret);
  16654. }
  16655. if (ret == 0) {
  16656. ret = wc_SetAuthKeyIdFromCert(NULL, der, derSz);
  16657. if (ret == BAD_FUNC_ARG)
  16658. ret = 0;
  16659. else
  16660. ret = WC_TEST_RET_ENC_NC;
  16661. }
  16662. wc_SetCert_Free(&cert);
  16663. if (ret == 0) {
  16664. if(cert.decodedCert != NULL)
  16665. ret = WC_TEST_RET_ENC_NC;
  16666. }
  16667. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16668. return ret;
  16669. }
  16670. #endif /* defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) &&
  16671. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) */
  16672. #define RSA_TEST_BYTES 512 /* up to 4096-bit key */
  16673. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  16674. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  16675. static wc_test_ret_t rsa_flatten_test(RsaKey* key)
  16676. {
  16677. wc_test_ret_t ret;
  16678. byte e[RSA_TEST_BYTES];
  16679. byte n[RSA_TEST_BYTES];
  16680. word32 eSz = sizeof(e);
  16681. word32 nSz = sizeof(n);
  16682. /* Parameter Validation testing. */
  16683. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  16684. if (ret != BAD_FUNC_ARG)
  16685. return WC_TEST_RET_ENC_EC(ret);
  16686. ret = wc_RsaFlattenPublicKey(key, NULL, &eSz, n, &nSz);
  16687. if (ret != BAD_FUNC_ARG)
  16688. return WC_TEST_RET_ENC_EC(ret);
  16689. ret = wc_RsaFlattenPublicKey(key, e, NULL, n, &nSz);
  16690. if (ret != BAD_FUNC_ARG)
  16691. return WC_TEST_RET_ENC_EC(ret);
  16692. ret = wc_RsaFlattenPublicKey(key, e, &eSz, NULL, &nSz);
  16693. if (ret != BAD_FUNC_ARG)
  16694. return WC_TEST_RET_ENC_EC(ret);
  16695. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, NULL);
  16696. if (ret != BAD_FUNC_ARG)
  16697. return WC_TEST_RET_ENC_EC(ret);
  16698. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  16699. if (ret != 0)
  16700. return WC_TEST_RET_ENC_EC(ret);
  16701. eSz = 0;
  16702. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  16703. if (ret != RSA_BUFFER_E)
  16704. return WC_TEST_RET_ENC_EC(ret);
  16705. eSz = sizeof(e);
  16706. nSz = 0;
  16707. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  16708. if (ret != RSA_BUFFER_E)
  16709. return WC_TEST_RET_ENC_EC(ret);
  16710. return 0;
  16711. }
  16712. #endif /* NO_ASN */
  16713. #if !defined(HAVE_FIPS) && !defined(NO_ASN) \
  16714. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  16715. static wc_test_ret_t rsa_export_key_test(RsaKey* key)
  16716. {
  16717. wc_test_ret_t ret;
  16718. byte e[3];
  16719. word32 eSz = sizeof(e);
  16720. byte n[RSA_TEST_BYTES];
  16721. word32 nSz = sizeof(n);
  16722. byte d[RSA_TEST_BYTES];
  16723. word32 dSz = sizeof(d);
  16724. byte p[RSA_TEST_BYTES/2];
  16725. word32 pSz = sizeof(p);
  16726. byte q[RSA_TEST_BYTES/2];
  16727. word32 qSz = sizeof(q);
  16728. word32 zero = 0;
  16729. ret = wc_RsaExportKey(NULL, e, &eSz, n, &nSz, 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, NULL, &eSz, n, &nSz, d, &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, NULL, n, &nSz, d, &dSz, 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, NULL, &nSz, d, &dSz, p, &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, NULL, d, &dSz, p, &pSz, 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, NULL, &dSz, p, &pSz, q, &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, NULL, p, &pSz, q, &qSz);
  16748. if (ret != BAD_FUNC_ARG)
  16749. return WC_TEST_RET_ENC_EC(ret);
  16750. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, NULL, &pSz, q, &qSz);
  16751. if (ret != BAD_FUNC_ARG)
  16752. return WC_TEST_RET_ENC_EC(ret);
  16753. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, NULL, q, &qSz);
  16754. if (ret != BAD_FUNC_ARG)
  16755. return WC_TEST_RET_ENC_EC(ret);
  16756. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, NULL, &qSz);
  16757. if (ret != BAD_FUNC_ARG)
  16758. return WC_TEST_RET_ENC_EC(ret);
  16759. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, NULL);
  16760. if (ret != BAD_FUNC_ARG)
  16761. return WC_TEST_RET_ENC_EC(ret);
  16762. ret = wc_RsaExportKey(key, e, &zero, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  16763. if (ret != RSA_BUFFER_E)
  16764. return WC_TEST_RET_ENC_EC(ret);
  16765. ret = wc_RsaExportKey(key, e, &eSz, n, &zero, d, &dSz, p, &pSz, q, &qSz);
  16766. if (ret != RSA_BUFFER_E)
  16767. return WC_TEST_RET_ENC_EC(ret);
  16768. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16769. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &zero, p, &pSz, q, &qSz);
  16770. if (ret != RSA_BUFFER_E)
  16771. return WC_TEST_RET_ENC_EC(ret);
  16772. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &zero, q, &qSz);
  16773. if (ret != RSA_BUFFER_E)
  16774. return WC_TEST_RET_ENC_EC(ret);
  16775. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &zero);
  16776. if (ret != RSA_BUFFER_E)
  16777. return WC_TEST_RET_ENC_EC(ret);
  16778. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  16779. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  16780. if (ret != 0)
  16781. return WC_TEST_RET_ENC_EC(ret);
  16782. return 0;
  16783. }
  16784. #endif /* !HAVE_FIPS && !NO_ASN && !WOLFSSL_RSA_VERIFY_ONLY */
  16785. #ifndef NO_SIG_WRAPPER
  16786. static wc_test_ret_t rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng)
  16787. {
  16788. wc_test_ret_t ret;
  16789. word32 sigSz;
  16790. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  16791. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  16792. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  16793. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  16794. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  16795. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  16796. };
  16797. WOLFSSL_SMALL_STACK_STATIC const byte hashEnc[] = {
  16798. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  16799. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  16800. 0x00, 0x04, 0x20,
  16801. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  16802. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  16803. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  16804. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  16805. };
  16806. word32 inLen = (word32)XSTRLEN((char*)in);
  16807. byte out[RSA_TEST_BYTES];
  16808. /* Parameter Validation testing. */
  16809. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_NONE, key, keyLen);
  16810. if (ret != BAD_FUNC_ARG)
  16811. return WC_TEST_RET_ENC_EC(ret);
  16812. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, 0);
  16813. if (ret != BAD_FUNC_ARG)
  16814. return WC_TEST_RET_ENC_EC(ret);
  16815. sigSz = (word32)modLen;
  16816. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  16817. inLen, out, &sigSz, 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. 0, out, &sigSz, key, 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, NULL, &sigSz, key, keyLen, 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, NULL, key, keyLen, rng);
  16830. if (ret != BAD_FUNC_ARG)
  16831. return WC_TEST_RET_ENC_EC(ret);
  16832. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16833. inLen, out, &sigSz, NULL, keyLen, rng);
  16834. if (ret != BAD_FUNC_ARG)
  16835. return WC_TEST_RET_ENC_EC(ret);
  16836. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16837. inLen, out, &sigSz, key, 0, rng);
  16838. if (ret != BAD_FUNC_ARG)
  16839. return WC_TEST_RET_ENC_EC(ret);
  16840. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16841. inLen, out, &sigSz, key, keyLen, NULL);
  16842. #if defined(WOLFSSL_AFALG_XILINX_RSA) || defined(WOLFSSL_XILINX_CRYPT)
  16843. /* blinding / rng handled with hardware acceleration */
  16844. if (ret != 0)
  16845. #elif defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  16846. /* async may not require RNG */
  16847. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  16848. if (ret != NO_VALID_DEVID)
  16849. #else
  16850. if (ret != 0 && ret != MISSING_RNG_E)
  16851. #endif
  16852. #elif defined(HAVE_FIPS) || !defined(WC_RSA_BLINDING)
  16853. /* FIPS140 implementation does not do blinding */
  16854. if (ret != 0)
  16855. #elif defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_RSA_VERIFY_ONLY)
  16856. if (ret != SIG_TYPE_E)
  16857. #elif defined(WOLFSSL_CRYPTOCELL) || defined(WOLFSSL_SE050)
  16858. /* RNG is handled by hardware */
  16859. if (ret != 0)
  16860. #else
  16861. if (ret != MISSING_RNG_E)
  16862. #endif
  16863. return WC_TEST_RET_ENC_EC(ret);
  16864. sigSz = 0;
  16865. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16866. inLen, out, &sigSz, key, keyLen, rng);
  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, NULL,
  16870. inLen, out, (word32)modLen, 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. 0, out, (word32)modLen, key, 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, NULL, (word32)modLen, key, keyLen);
  16879. if (ret != BAD_FUNC_ARG)
  16880. return WC_TEST_RET_ENC_EC(ret);
  16881. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16882. inLen, out, 0, key, keyLen);
  16883. if (ret != BAD_FUNC_ARG)
  16884. return WC_TEST_RET_ENC_EC(ret);
  16885. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16886. inLen, out, (word32)modLen, NULL, keyLen);
  16887. if (ret != BAD_FUNC_ARG)
  16888. return WC_TEST_RET_ENC_EC(ret);
  16889. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16890. inLen, out, (word32)modLen, key, 0);
  16891. if (ret != BAD_FUNC_ARG)
  16892. return WC_TEST_RET_ENC_EC(ret);
  16893. #ifndef HAVE_ECC
  16894. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, keyLen);
  16895. if (ret != SIG_TYPE_E)
  16896. return WC_TEST_RET_ENC_EC(ret);
  16897. #endif
  16898. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  16899. return 0;
  16900. #endif
  16901. /* Use APIs. */
  16902. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, keyLen);
  16903. if (ret != modLen)
  16904. return WC_TEST_RET_ENC_EC(ret);
  16905. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA_W_ENC, key, keyLen);
  16906. if (ret != modLen)
  16907. return WC_TEST_RET_ENC_EC(ret);
  16908. sigSz = (word32)ret;
  16909. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  16910. XMEMSET(out, 0, sizeof(out));
  16911. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16912. inLen, out, &sigSz, key, keyLen, rng);
  16913. if (ret != 0)
  16914. return WC_TEST_RET_ENC_EC(ret);
  16915. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16916. inLen, out, (word32)modLen, key, keyLen);
  16917. if (ret != 0)
  16918. return WC_TEST_RET_ENC_EC(ret);
  16919. sigSz = (word32)sizeof(out);
  16920. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  16921. in, inLen, out, &sigSz, key, keyLen, rng);
  16922. if (ret != 0)
  16923. return WC_TEST_RET_ENC_EC(ret);
  16924. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  16925. in, inLen, out, (word32)modLen, key, keyLen);
  16926. if (ret != 0)
  16927. return WC_TEST_RET_ENC_EC(ret);
  16928. /* Wrong signature type. */
  16929. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16930. inLen, out, (word32)modLen, key, keyLen);
  16931. if (ret == 0)
  16932. return WC_TEST_RET_ENC_EC(ret);
  16933. /* check hash functions */
  16934. sigSz = (word32)sizeof(out);
  16935. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  16936. hash, (int)sizeof(hash), out, &sigSz, key, keyLen, rng);
  16937. if (ret != 0)
  16938. return WC_TEST_RET_ENC_EC(ret);
  16939. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  16940. hash, (int)sizeof(hash), out, (word32)modLen, key, keyLen);
  16941. if (ret != 0)
  16942. return WC_TEST_RET_ENC_EC(ret);
  16943. sigSz = (word32)sizeof(out);
  16944. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  16945. hashEnc, (int)sizeof(hashEnc), out, &sigSz, key, keyLen, rng);
  16946. if (ret != 0)
  16947. return WC_TEST_RET_ENC_EC(ret);
  16948. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  16949. hashEnc, (int)sizeof(hashEnc), out, (word32)modLen, key, keyLen);
  16950. if (ret != 0)
  16951. return WC_TEST_RET_ENC_EC(ret);
  16952. #else
  16953. (void)hash;
  16954. (void)hashEnc;
  16955. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */
  16956. return 0;
  16957. }
  16958. #endif /* !NO_SIG_WRAPPER */
  16959. #ifdef WC_RSA_NONBLOCK
  16960. static wc_test_ret_t rsa_nb_test(RsaKey* key, const byte* in, word32 inLen, byte* out,
  16961. word32 outSz, byte* plain, word32 plainSz, WC_RNG* rng)
  16962. {
  16963. wc_test_ret_t ret = 0;
  16964. int count;
  16965. int signSz = 0;
  16966. RsaNb nb;
  16967. byte* inlinePlain = NULL;
  16968. /* Enable non-blocking RSA mode - provide context */
  16969. ret = wc_RsaSetNonBlock(key, &nb);
  16970. if (ret != 0)
  16971. return ret;
  16972. #ifdef WC_RSA_NONBLOCK_TIME
  16973. /* Enable time based RSA blocking. 8 microseconds max (3.1GHz) */
  16974. ret = wc_RsaSetNonBlockTime(key, 8, 3100);
  16975. if (ret != 0)
  16976. return ret;
  16977. #endif
  16978. count = 0;
  16979. do {
  16980. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, rng);
  16981. count++; /* track number of would blocks */
  16982. if (ret == FP_WOULDBLOCK) {
  16983. /* do "other" work here */
  16984. }
  16985. } while (ret == FP_WOULDBLOCK);
  16986. if (ret < 0) {
  16987. return ret;
  16988. }
  16989. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  16990. printf("RSA non-block sign: %d times\n", count);
  16991. #endif
  16992. signSz = ret;
  16993. /* Test non-blocking verify */
  16994. XMEMSET(plain, 0, plainSz);
  16995. count = 0;
  16996. do {
  16997. ret = wc_RsaSSL_Verify(out, (word32)signSz, plain, plainSz, key);
  16998. count++; /* track number of would blocks */
  16999. if (ret == FP_WOULDBLOCK) {
  17000. /* do "other" work here */
  17001. }
  17002. } while (ret == FP_WOULDBLOCK);
  17003. if (ret < 0) {
  17004. return ret;
  17005. }
  17006. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  17007. printf("RSA non-block verify: %d times\n", count);
  17008. #endif
  17009. if (signSz == ret && XMEMCMP(plain, in, (size_t)ret)) {
  17010. return SIG_VERIFY_E;
  17011. }
  17012. /* Test inline non-blocking verify */
  17013. count = 0;
  17014. do {
  17015. ret = wc_RsaSSL_VerifyInline(out, (word32)signSz, &inlinePlain, key);
  17016. count++; /* track number of would blocks */
  17017. if (ret == FP_WOULDBLOCK) {
  17018. /* do "other" work here */
  17019. }
  17020. } while (ret == FP_WOULDBLOCK);
  17021. if (ret < 0) {
  17022. return ret;
  17023. }
  17024. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  17025. printf("RSA non-block inline verify: %d times\n", count);
  17026. #endif
  17027. if (signSz == ret && XMEMCMP(inlinePlain, in, (size_t)ret)) {
  17028. return SIG_VERIFY_E;
  17029. }
  17030. /* Disabling non-block RSA mode */
  17031. ret = wc_RsaSetNonBlock(key, NULL);
  17032. (void)count;
  17033. return 0;
  17034. }
  17035. #endif
  17036. #if !defined(NO_ASN)
  17037. static wc_test_ret_t rsa_decode_test(RsaKey* keyPub)
  17038. {
  17039. wc_test_ret_t ret;
  17040. word32 inSz;
  17041. word32 inOutIdx;
  17042. WOLFSSL_SMALL_STACK_STATIC const byte n[2] = { 0x00, 0x23 };
  17043. WOLFSSL_SMALL_STACK_STATIC const byte e[2] = { 0x00, 0x03 };
  17044. WOLFSSL_SMALL_STACK_STATIC const byte good[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1,
  17045. 0x03 };
  17046. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgId[] = {
  17047. 0x30, 0x18, 0x30, 0x16,
  17048. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17049. 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17050. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgIdNull[] = {
  17051. 0x30, 0x1a, 0x30, 0x18,
  17052. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17053. 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  17054. 0x02, 0x1, 0x03 };
  17055. WOLFSSL_SMALL_STACK_STATIC const byte badAlgIdNull[] = {
  17056. 0x30, 0x1b, 0x30, 0x19,
  17057. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17058. 0x05, 0x01, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  17059. 0x02, 0x1, 0x03 };
  17060. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitString[] = {
  17061. 0x30, 0x18, 0x30, 0x16,
  17062. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17063. 0x04, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17064. WOLFSSL_SMALL_STACK_STATIC const byte badBitStringLen[] = {
  17065. 0x30, 0x18, 0x30, 0x16,
  17066. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17067. 0x03, 0x0a, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17068. WOLFSSL_SMALL_STACK_STATIC const byte badNoSeq[] = {
  17069. 0x30, 0x16, 0x30, 0x14,
  17070. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17071. 0x07, 0x00, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17072. WOLFSSL_SMALL_STACK_STATIC const byte badNoObj[] = {
  17073. 0x30, 0x0f, 0x30, 0x0d, 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06,
  17074. 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17075. WOLFSSL_SMALL_STACK_STATIC const byte badIntN[] = {
  17076. 0x30, 0x06, 0x02, 0x05, 0x23, 0x02, 0x1, 0x03 };
  17077. WOLFSSL_SMALL_STACK_STATIC const byte badNotIntE[] = {
  17078. 0x30, 0x06, 0x02, 0x01, 0x23, 0x04, 0x1, 0x03 };
  17079. WOLFSSL_SMALL_STACK_STATIC const byte badLength[] = {
  17080. 0x30, 0x04, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17081. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrNoZero[] = {
  17082. 0x30, 0x17, 0x30, 0x15,
  17083. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17084. 0x03, 0x08, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17085. ret = wc_InitRsaKey(keyPub, NULL);
  17086. if (ret != 0)
  17087. return WC_TEST_RET_ENC_EC(ret);
  17088. /* Parameter Validation testing. */
  17089. ret = wc_RsaPublicKeyDecodeRaw(NULL, sizeof(n), e, sizeof(e), keyPub);
  17090. if (ret != BAD_FUNC_ARG) {
  17091. ret = WC_TEST_RET_ENC_EC(ret);
  17092. goto done;
  17093. }
  17094. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), NULL, sizeof(e), keyPub);
  17095. if (ret != BAD_FUNC_ARG) {
  17096. ret = WC_TEST_RET_ENC_EC(ret);
  17097. goto done;
  17098. }
  17099. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), NULL);
  17100. if (ret != BAD_FUNC_ARG) {
  17101. ret = WC_TEST_RET_ENC_EC(ret);
  17102. goto done;
  17103. }
  17104. ret = wc_RsaPublicKeyDecodeRaw(n, (word32)-1, e, sizeof(e), keyPub);
  17105. #if defined(USE_INTEGER_HEAP_MATH)
  17106. if (ret != 0)
  17107. #else
  17108. if (ret != ASN_GETINT_E)
  17109. #endif
  17110. {
  17111. ret = WC_TEST_RET_ENC_EC(ret);
  17112. goto done;
  17113. }
  17114. wc_FreeRsaKey(keyPub);
  17115. ret = wc_InitRsaKey(keyPub, NULL);
  17116. if (ret != 0)
  17117. return WC_TEST_RET_ENC_EC(ret);
  17118. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, (word32)-1, keyPub);
  17119. #if defined(USE_INTEGER_HEAP_MATH)
  17120. if (ret != 0)
  17121. #else
  17122. if (ret != ASN_GETINT_E)
  17123. #endif
  17124. {
  17125. ret = WC_TEST_RET_ENC_EC(ret);
  17126. goto done;
  17127. }
  17128. wc_FreeRsaKey(keyPub);
  17129. ret = wc_InitRsaKey(keyPub, NULL);
  17130. if (ret != 0)
  17131. return WC_TEST_RET_ENC_EC(ret);
  17132. /* Use API. */
  17133. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), keyPub);
  17134. if (ret != 0) {
  17135. ret = WC_TEST_RET_ENC_EC(ret);
  17136. goto done;
  17137. }
  17138. wc_FreeRsaKey(keyPub);
  17139. ret = wc_InitRsaKey(keyPub, NULL);
  17140. if (ret != 0)
  17141. return WC_TEST_RET_ENC_EC(ret);
  17142. /* Parameter Validation testing. */
  17143. inSz = sizeof(good);
  17144. ret = wc_RsaPublicKeyDecode(NULL, &inOutIdx, keyPub, inSz);
  17145. if (ret != BAD_FUNC_ARG) {
  17146. ret = WC_TEST_RET_ENC_EC(ret);
  17147. goto done;
  17148. }
  17149. ret = wc_RsaPublicKeyDecode(good, NULL, keyPub, inSz);
  17150. if (ret != BAD_FUNC_ARG) {
  17151. ret = WC_TEST_RET_ENC_EC(ret);
  17152. goto done;
  17153. }
  17154. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  17155. if (ret != BAD_FUNC_ARG) {
  17156. ret = WC_TEST_RET_ENC_EC(ret);
  17157. goto done;
  17158. }
  17159. /* Use good data and offset to bad data. */
  17160. inOutIdx = 2;
  17161. inSz = sizeof(good) - inOutIdx;
  17162. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  17163. if (ret != ASN_PARSE_E) {
  17164. ret = WC_TEST_RET_ENC_EC(ret);
  17165. goto done;
  17166. }
  17167. inOutIdx = 2;
  17168. inSz = sizeof(goodAlgId) - inOutIdx;
  17169. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  17170. if (ret != ASN_PARSE_E) {
  17171. ret = WC_TEST_RET_ENC_EC(ret);
  17172. goto done;
  17173. }
  17174. inOutIdx = 2;
  17175. inSz = sizeof(goodAlgId);
  17176. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  17177. #ifndef WOLFSSL_NO_DECODE_EXTRA
  17178. if (ret != ASN_PARSE_E)
  17179. #else
  17180. if (ret != ASN_RSA_KEY_E)
  17181. #endif
  17182. {
  17183. ret = WC_TEST_RET_ENC_EC(ret);
  17184. goto done;
  17185. }
  17186. /* Try different bad data. */
  17187. inSz = sizeof(badAlgIdNull);
  17188. inOutIdx = 0;
  17189. ret = wc_RsaPublicKeyDecode(badAlgIdNull, &inOutIdx, keyPub, inSz);
  17190. if (ret != ASN_EXPECT_0_E) {
  17191. ret = WC_TEST_RET_ENC_EC(ret);
  17192. goto done;
  17193. }
  17194. inSz = sizeof(badNotBitString);
  17195. inOutIdx = 0;
  17196. ret = wc_RsaPublicKeyDecode(badNotBitString, &inOutIdx, keyPub, inSz);
  17197. if (ret != ASN_BITSTR_E) {
  17198. ret = WC_TEST_RET_ENC_EC(ret);
  17199. goto done;
  17200. }
  17201. inSz = sizeof(badBitStringLen);
  17202. inOutIdx = 0;
  17203. ret = wc_RsaPublicKeyDecode(badBitStringLen, &inOutIdx, keyPub, inSz);
  17204. if (ret != ASN_PARSE_E) {
  17205. ret = WC_TEST_RET_ENC_EC(ret);
  17206. goto done;
  17207. }
  17208. inSz = sizeof(badNoSeq);
  17209. inOutIdx = 0;
  17210. ret = wc_RsaPublicKeyDecode(badNoSeq, &inOutIdx, keyPub, inSz);
  17211. if (ret != ASN_PARSE_E) {
  17212. ret = WC_TEST_RET_ENC_EC(ret);
  17213. goto done;
  17214. }
  17215. inSz = sizeof(badNoObj);
  17216. inOutIdx = 0;
  17217. ret = wc_RsaPublicKeyDecode(badNoObj, &inOutIdx, keyPub, inSz);
  17218. if (ret != ASN_PARSE_E && ret != ASN_OBJECT_ID_E) {
  17219. ret = WC_TEST_RET_ENC_EC(ret);
  17220. goto done;
  17221. }
  17222. inSz = sizeof(badIntN);
  17223. inOutIdx = 0;
  17224. ret = wc_RsaPublicKeyDecode(badIntN, &inOutIdx, keyPub, inSz);
  17225. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  17226. ret = WC_TEST_RET_ENC_EC(ret);
  17227. goto done;
  17228. }
  17229. inSz = sizeof(badNotIntE);
  17230. inOutIdx = 0;
  17231. ret = wc_RsaPublicKeyDecode(badNotIntE, &inOutIdx, keyPub, inSz);
  17232. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  17233. ret = WC_TEST_RET_ENC_EC(ret);
  17234. goto done;
  17235. }
  17236. /* TODO: Shouldn't pass as the sequence length is too small. */
  17237. inSz = sizeof(badLength);
  17238. inOutIdx = 0;
  17239. ret = wc_RsaPublicKeyDecode(badLength, &inOutIdx, keyPub, inSz);
  17240. #ifndef WOLFSSL_ASN_TEMPLATE
  17241. if (ret != 0)
  17242. #else
  17243. if (ret != ASN_PARSE_E)
  17244. #endif
  17245. {
  17246. ret = WC_TEST_RET_ENC_EC(ret);
  17247. goto done;
  17248. }
  17249. /* TODO: Shouldn't ignore object id's data. */
  17250. wc_FreeRsaKey(keyPub);
  17251. ret = wc_InitRsaKey(keyPub, NULL);
  17252. if (ret != 0)
  17253. return WC_TEST_RET_ENC_EC(ret);
  17254. inSz = sizeof(badBitStrNoZero);
  17255. inOutIdx = 0;
  17256. ret = wc_RsaPublicKeyDecode(badBitStrNoZero, &inOutIdx, keyPub, inSz);
  17257. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  17258. ret = WC_TEST_RET_ENC_EC(ret);
  17259. goto done;
  17260. }
  17261. wc_FreeRsaKey(keyPub);
  17262. ret = wc_InitRsaKey(keyPub, NULL);
  17263. if (ret != 0)
  17264. return WC_TEST_RET_ENC_EC(ret);
  17265. /* Valid data cases. */
  17266. inSz = sizeof(good);
  17267. inOutIdx = 0;
  17268. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  17269. if (ret != 0) {
  17270. ret = WC_TEST_RET_ENC_EC(ret);
  17271. goto done;
  17272. }
  17273. if (inOutIdx != inSz) {
  17274. ret = WC_TEST_RET_ENC_NC;
  17275. goto done;
  17276. }
  17277. wc_FreeRsaKey(keyPub);
  17278. ret = wc_InitRsaKey(keyPub, NULL);
  17279. if (ret != 0)
  17280. return WC_TEST_RET_ENC_EC(ret);
  17281. inSz = sizeof(goodAlgId);
  17282. inOutIdx = 0;
  17283. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  17284. if (ret != 0) {
  17285. ret = WC_TEST_RET_ENC_EC(ret);
  17286. goto done;
  17287. }
  17288. if (inOutIdx != inSz) {
  17289. ret = WC_TEST_RET_ENC_NC;
  17290. goto done;
  17291. }
  17292. wc_FreeRsaKey(keyPub);
  17293. ret = wc_InitRsaKey(keyPub, NULL);
  17294. if (ret != 0)
  17295. return WC_TEST_RET_ENC_EC(ret);
  17296. inSz = sizeof(goodAlgIdNull);
  17297. inOutIdx = 0;
  17298. ret = wc_RsaPublicKeyDecode(goodAlgIdNull, &inOutIdx, keyPub, inSz);
  17299. if (ret != 0) {
  17300. ret = WC_TEST_RET_ENC_EC(ret);
  17301. goto done;
  17302. }
  17303. if (inOutIdx != inSz) {
  17304. ret = WC_TEST_RET_ENC_NC;
  17305. goto done;
  17306. }
  17307. done:
  17308. wc_FreeRsaKey(keyPub);
  17309. return ret;
  17310. }
  17311. #endif
  17312. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  17313. /* Need to create known good signatures to test with this. */
  17314. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  17315. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  17316. static wc_test_ret_t rsa_pss_test(WC_RNG* rng, RsaKey* key)
  17317. {
  17318. byte digest[WC_MAX_DIGEST_SIZE];
  17319. wc_test_ret_t ret = 0;
  17320. const char inStr[] = TEST_STRING;
  17321. word32 inLen = (word32)TEST_STRING_SZ;
  17322. word32 outSz;
  17323. word32 plainSz;
  17324. word32 digestSz;
  17325. int i, j;
  17326. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  17327. int k, l;
  17328. #endif
  17329. #ifndef WOLFSSL_SE050
  17330. int len;
  17331. #endif
  17332. byte* plain;
  17333. int mgf[] = {
  17334. #ifndef NO_SHA
  17335. WC_MGF1SHA1,
  17336. #endif
  17337. #ifdef WOLFSSL_SHA224
  17338. WC_MGF1SHA224,
  17339. #endif
  17340. WC_MGF1SHA256,
  17341. #ifdef WOLFSSL_SHA384
  17342. WC_MGF1SHA384,
  17343. #endif
  17344. #ifdef WOLFSSL_SHA512
  17345. WC_MGF1SHA512
  17346. #endif
  17347. };
  17348. enum wc_HashType hash[] = {
  17349. #ifndef NO_SHA
  17350. WC_HASH_TYPE_SHA,
  17351. #endif
  17352. #ifdef WOLFSSL_SHA224
  17353. WC_HASH_TYPE_SHA224,
  17354. #endif
  17355. WC_HASH_TYPE_SHA256,
  17356. #ifdef WOLFSSL_SHA384
  17357. WC_HASH_TYPE_SHA384,
  17358. #endif
  17359. #ifdef WOLFSSL_SHA512
  17360. WC_HASH_TYPE_SHA512,
  17361. #endif
  17362. };
  17363. WC_DECLARE_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
  17364. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17365. WC_DECLARE_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  17366. WC_ALLOC_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
  17367. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17368. WC_ALLOC_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  17369. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17370. if (in == NULL || out == NULL || sig == NULL)
  17371. ERROR_OUT(MEMORY_E, exit_rsa_pss);
  17372. #endif
  17373. XMEMCPY(in, inStr, inLen);
  17374. /* Test all combinations of hash and MGF. */
  17375. for (j = 0; j < (int)(sizeof(hash)/sizeof(*hash)); j++) {
  17376. /* Calculate hash of message. */
  17377. ret = wc_Hash(hash[j], in, inLen, digest, sizeof(digest));
  17378. if (ret != 0)
  17379. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17380. digestSz = (word32)wc_HashGetDigestSize(hash[j]);
  17381. #ifdef WOLFSSL_SE050
  17382. /* SE050 only supports MGF matched to same hash type */
  17383. i = j;
  17384. #else
  17385. for (i = 0; i < (int)(sizeof(mgf)/sizeof(*mgf)); i++) {
  17386. #endif
  17387. outSz = RSA_TEST_BYTES;
  17388. do {
  17389. #if defined(WOLFSSL_ASYNC_CRYPT)
  17390. ret = wc_AsyncWait(ret, &key->asyncDev,
  17391. WC_ASYNC_FLAG_CALL_AGAIN);
  17392. #endif
  17393. if (ret >= 0) {
  17394. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz,
  17395. hash[j], mgf[i], -1, key, rng);
  17396. }
  17397. } while (ret == WC_PENDING_E);
  17398. if (ret <= 0)
  17399. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17400. outSz = (word32)ret;
  17401. XMEMCPY(sig, out, outSz);
  17402. plain = NULL;
  17403. TEST_SLEEP();
  17404. do {
  17405. #if defined(WOLFSSL_ASYNC_CRYPT)
  17406. ret = wc_AsyncWait(ret, &key->asyncDev,
  17407. WC_ASYNC_FLAG_CALL_AGAIN);
  17408. #endif
  17409. if (ret >= 0) {
  17410. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[j],
  17411. mgf[i], -1, key);
  17412. }
  17413. } while (ret == WC_PENDING_E);
  17414. if (ret <= 0)
  17415. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17416. plainSz = (word32)ret;
  17417. TEST_SLEEP();
  17418. #if defined(HAVE_SELFTEST) && \
  17419. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  17420. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17421. hash[j], -1);
  17422. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  17423. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17424. hash[j], -1, 0);
  17425. #else
  17426. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz,
  17427. hash[j], -1, wc_RsaEncryptSize(key)*8, HEAP_HINT);
  17428. #endif
  17429. if (ret != 0)
  17430. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17431. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  17432. for (k = 0; k < (int)(sizeof(mgf)/sizeof(*mgf)); k++) {
  17433. for (l = 0; l < (int)(sizeof(hash)/sizeof(*hash)); l++) {
  17434. if (i == k && j == l)
  17435. continue;
  17436. XMEMCPY(sig, out, outSz);
  17437. do {
  17438. #if defined(WOLFSSL_ASYNC_CRYPT)
  17439. ret = wc_AsyncWait(ret, &key->asyncDev,
  17440. WC_ASYNC_FLAG_CALL_AGAIN);
  17441. #endif
  17442. if (ret >= 0) {
  17443. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz,
  17444. (byte**)&plain, hash[l], mgf[k], -1, key);
  17445. }
  17446. } while (ret == WC_PENDING_E);
  17447. if (ret >= 0)
  17448. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17449. }
  17450. }
  17451. #endif
  17452. #ifndef WOLFSSL_SE050
  17453. } /* end mgf for loop */
  17454. #endif
  17455. }
  17456. /* SE050 generates salts internally only of hash length */
  17457. #ifndef WOLFSSL_SE050
  17458. /* Test that a salt length of zero works. */
  17459. digestSz = (word32)wc_HashGetDigestSize(hash[0]);
  17460. outSz = RSA_TEST_BYTES;
  17461. do {
  17462. #if defined(WOLFSSL_ASYNC_CRYPT)
  17463. ret = wc_AsyncWait(ret, &key->asyncDev,
  17464. WC_ASYNC_FLAG_CALL_AGAIN);
  17465. #endif
  17466. if (ret >= 0) {
  17467. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  17468. mgf[0], 0, key, rng);
  17469. }
  17470. } while (ret == WC_PENDING_E);
  17471. if (ret <= 0)
  17472. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17473. outSz = (word32)ret;
  17474. TEST_SLEEP();
  17475. do {
  17476. #if defined(WOLFSSL_ASYNC_CRYPT)
  17477. ret = wc_AsyncWait(ret, &key->asyncDev,
  17478. WC_ASYNC_FLAG_CALL_AGAIN);
  17479. #endif
  17480. if (ret >= 0) {
  17481. ret = wc_RsaPSS_Verify_ex(out, outSz, sig, outSz, hash[0], mgf[0],
  17482. 0, key);
  17483. }
  17484. } while (ret == WC_PENDING_E);
  17485. if (ret <= 0)
  17486. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17487. plainSz = (word32)ret;
  17488. TEST_SLEEP();
  17489. do {
  17490. #if defined(WOLFSSL_ASYNC_CRYPT)
  17491. ret = wc_AsyncWait(ret, &key->asyncDev,
  17492. WC_ASYNC_FLAG_CALL_AGAIN);
  17493. #endif
  17494. if (ret >= 0) {
  17495. #if defined(HAVE_SELFTEST) && \
  17496. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  17497. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  17498. hash[0], 0);
  17499. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  17500. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  17501. hash[0], 0, 0);
  17502. #else
  17503. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, sig, plainSz,
  17504. hash[0], 0, 0, HEAP_HINT);
  17505. #endif
  17506. }
  17507. } while (ret == WC_PENDING_E);
  17508. if (ret != 0)
  17509. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17510. XMEMCPY(sig, out, outSz);
  17511. plain = NULL;
  17512. do {
  17513. #if defined(WOLFSSL_ASYNC_CRYPT)
  17514. ret = wc_AsyncWait(ret, &key->asyncDev,
  17515. WC_ASYNC_FLAG_CALL_AGAIN);
  17516. #endif
  17517. if (ret >= 0) {
  17518. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  17519. 0, key);
  17520. }
  17521. } while (ret == WC_PENDING_E);
  17522. if (ret <= 0)
  17523. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17524. plainSz = (word32)ret;
  17525. TEST_SLEEP();
  17526. #if defined(HAVE_SELFTEST) && \
  17527. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  17528. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17529. hash[0], 0);
  17530. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  17531. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17532. hash[0], 0, 0);
  17533. #else
  17534. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  17535. 0, 0, HEAP_HINT);
  17536. #endif
  17537. if (ret != 0)
  17538. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17539. /* Test bad salt lengths in various APIs. */
  17540. digestSz = (word32)wc_HashGetDigestSize(hash[0]);
  17541. outSz = RSA_TEST_BYTES;
  17542. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  17543. len = -2;
  17544. #else
  17545. len = -3;
  17546. #endif
  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], len, 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. do {
  17560. #if defined(WOLFSSL_ASYNC_CRYPT)
  17561. ret = wc_AsyncWait(ret, &key->asyncDev,
  17562. WC_ASYNC_FLAG_CALL_AGAIN);
  17563. #endif
  17564. if (ret >= 0) {
  17565. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  17566. mgf[0], digestSz + 1, key, rng);
  17567. }
  17568. } while (ret == WC_PENDING_E);
  17569. if (ret != PSS_SALTLEN_E)
  17570. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17571. TEST_SLEEP();
  17572. do {
  17573. #if defined(WOLFSSL_ASYNC_CRYPT)
  17574. ret = wc_AsyncWait(ret, &key->asyncDev,
  17575. WC_ASYNC_FLAG_CALL_AGAIN);
  17576. #endif
  17577. if (ret >= 0) {
  17578. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0],
  17579. mgf[0], -2, key);
  17580. }
  17581. } while (ret == WC_PENDING_E);
  17582. if (ret != PSS_SALTLEN_E)
  17583. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17584. TEST_SLEEP();
  17585. do {
  17586. #if defined(WOLFSSL_ASYNC_CRYPT)
  17587. ret = wc_AsyncWait(ret, &key->asyncDev,
  17588. WC_ASYNC_FLAG_CALL_AGAIN);
  17589. #endif
  17590. if (ret >= 0) {
  17591. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  17592. digestSz + 1, key);
  17593. }
  17594. } while (ret == WC_PENDING_E);
  17595. if (ret != PSS_SALTLEN_E)
  17596. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17597. TEST_SLEEP();
  17598. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  17599. len = -2;
  17600. #else
  17601. len = -3;
  17602. #endif
  17603. #if defined(HAVE_SELFTEST) && \
  17604. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  17605. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17606. hash[0], len);
  17607. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  17608. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17609. hash[0], len, 0);
  17610. #else
  17611. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  17612. len, 0, HEAP_HINT);
  17613. #endif
  17614. if (ret != PSS_SALTLEN_E)
  17615. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17616. #ifndef WOLFSSL_PSS_LONG_SALT
  17617. len = (int)(digestSz + 1);
  17618. #else
  17619. len = (int)(plainSz - digestSz - 1);
  17620. #endif
  17621. #if defined(HAVE_SELFTEST) && \
  17622. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  17623. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17624. hash[0], len);
  17625. if (ret != PSS_SALTLEN_E)
  17626. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17627. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  17628. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17629. hash[0], len, 0);
  17630. if (ret != BAD_PADDING_E)
  17631. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17632. #else
  17633. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  17634. len, 0, HEAP_HINT);
  17635. if (ret != PSS_SALTLEN_E)
  17636. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17637. #endif
  17638. ret = 0;
  17639. #endif /* WOLFSSL_SE050 */
  17640. exit_rsa_pss:
  17641. WC_FREE_VAR(sig, HEAP_HINT);
  17642. WC_FREE_VAR(in, HEAP_HINT);
  17643. WC_FREE_VAR(out, HEAP_HINT);
  17644. return ret;
  17645. }
  17646. #endif /* !WOLFSSL_RSA_VERIFY_ONLY && !WOLFSSL_RSA_PUBLIC_ONLY */
  17647. #endif
  17648. #ifdef WC_RSA_NO_PADDING
  17649. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_no_pad_test(void)
  17650. {
  17651. WC_RNG rng;
  17652. byte* tmp = NULL;
  17653. size_t bytes;
  17654. wc_test_ret_t ret;
  17655. word32 inLen = 0;
  17656. word32 idx = 0;
  17657. word32 outSz = RSA_TEST_BYTES;
  17658. word32 plainSz = RSA_TEST_BYTES;
  17659. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  17660. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  17661. !defined(NO_FILESYSTEM)
  17662. XFILE file;
  17663. #endif
  17664. WC_DECLARE_VAR(key, RsaKey, 1, HEAP_HINT);
  17665. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17666. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  17667. WC_ALLOC_VAR(key, RsaKey, 1, HEAP_HINT);
  17668. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17669. WC_ALLOC_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  17670. WOLFSSL_ENTER("rsa_no_pad_test");
  17671. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17672. if (key == NULL || out == NULL || plain == NULL)
  17673. ERROR_OUT(MEMORY_E, exit_rsa_nopadding);
  17674. #endif
  17675. /* initialize stack structures */
  17676. XMEMSET(&rng, 0, sizeof(rng));
  17677. XMEMSET(key, 0, sizeof(RsaKey));
  17678. #ifdef USE_CERT_BUFFERS_1024
  17679. bytes = (size_t)sizeof_client_key_der_1024;
  17680. if (bytes < (size_t)sizeof_client_cert_der_1024)
  17681. bytes = (size_t)sizeof_client_cert_der_1024;
  17682. #elif defined(USE_CERT_BUFFERS_2048)
  17683. bytes = (size_t)sizeof_client_key_der_2048;
  17684. if (bytes < (size_t)sizeof_client_cert_der_2048)
  17685. bytes = (size_t)sizeof_client_cert_der_2048;
  17686. #else
  17687. bytes = FOURK_BUF;
  17688. #endif
  17689. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17690. if (tmp == NULL
  17691. #ifdef WOLFSSL_ASYNC_CRYPT
  17692. || out == NULL || plain == NULL
  17693. #endif
  17694. ) {
  17695. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  17696. }
  17697. #ifdef USE_CERT_BUFFERS_1024
  17698. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  17699. #elif defined(USE_CERT_BUFFERS_2048)
  17700. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  17701. #elif defined(USE_CERT_BUFFERS_3072)
  17702. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  17703. #elif defined(USE_CERT_BUFFERS_4096)
  17704. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  17705. #elif !defined(NO_FILESYSTEM)
  17706. file = XFOPEN(clientKey, "rb");
  17707. if (!file) {
  17708. err_sys("can't open clientKey, Please run from wolfSSL home dir",
  17709. WC_TEST_RET_ENC_ERRNO);
  17710. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_nopadding);
  17711. }
  17712. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  17713. XFCLOSE(file);
  17714. if (bytes == 0)
  17715. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_nopadding);
  17716. #else
  17717. /* No key to use. */
  17718. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  17719. #endif /* USE_CERT_BUFFERS */
  17720. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  17721. if (ret != 0) {
  17722. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17723. }
  17724. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  17725. if (ret != 0) {
  17726. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17727. }
  17728. /* after loading in key use tmp as the test buffer */
  17729. #ifndef HAVE_FIPS
  17730. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  17731. #else
  17732. ret = wc_InitRng(&rng);
  17733. #endif
  17734. if (ret != 0) {
  17735. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17736. }
  17737. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  17738. inLen = (word32)wc_RsaEncryptSize(key);
  17739. outSz = inLen;
  17740. plainSz = inLen;
  17741. XMEMSET(tmp, 7, inLen);
  17742. do {
  17743. #if defined(WOLFSSL_ASYNC_CRYPT)
  17744. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17745. #endif
  17746. if (ret >= 0) {
  17747. ret = wc_RsaDirect(tmp, inLen, out, &outSz, key,
  17748. RSA_PRIVATE_ENCRYPT, &rng);
  17749. }
  17750. } while (ret == WC_PENDING_E);
  17751. if (ret <= 0) {
  17752. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17753. }
  17754. /* encrypted result should not be the same as input */
  17755. if (XMEMCMP(out, tmp, inLen) == 0) {
  17756. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  17757. }
  17758. TEST_SLEEP();
  17759. /* decrypt with public key and compare result */
  17760. do {
  17761. #if defined(WOLFSSL_ASYNC_CRYPT)
  17762. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17763. #endif
  17764. if (ret >= 0) {
  17765. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key,
  17766. RSA_PUBLIC_DECRYPT, &rng);
  17767. }
  17768. } while (ret == WC_PENDING_E);
  17769. if (ret <= 0) {
  17770. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17771. }
  17772. if (XMEMCMP(plain, tmp, inLen) != 0) {
  17773. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  17774. }
  17775. TEST_SLEEP();
  17776. #endif
  17777. #ifdef WC_RSA_BLINDING
  17778. ret = wc_RsaSetRNG(NULL, &rng);
  17779. if (ret != BAD_FUNC_ARG) {
  17780. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17781. }
  17782. ret = wc_RsaSetRNG(key, &rng);
  17783. if (ret < 0) {
  17784. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17785. }
  17786. #endif
  17787. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  17788. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  17789. do {
  17790. #if defined(WOLFSSL_ASYNC_CRYPT)
  17791. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17792. #endif
  17793. if (ret >= 0) {
  17794. ret = wc_RsaPublicEncrypt_ex(tmp, inLen, out, outSz, key, &rng,
  17795. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  17796. }
  17797. } while (ret == WC_PENDING_E);
  17798. if (ret < 0) {
  17799. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17800. }
  17801. TEST_SLEEP();
  17802. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  17803. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17804. do {
  17805. #if defined(WOLFSSL_ASYNC_CRYPT)
  17806. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17807. #endif
  17808. if (ret >= 0) {
  17809. ret = wc_RsaPrivateDecrypt_ex(out, outSz, plain, plainSz, key,
  17810. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  17811. }
  17812. } while (ret == WC_PENDING_E);
  17813. if (ret < 0) {
  17814. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17815. }
  17816. if (XMEMCMP(plain, tmp, inLen) != 0) {
  17817. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  17818. }
  17819. TEST_SLEEP();
  17820. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  17821. /* test some bad arguments */
  17822. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key, -1,
  17823. &rng);
  17824. if (ret != BAD_FUNC_ARG) {
  17825. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17826. }
  17827. ret = wc_RsaDirect(out, outSz, plain, &plainSz, NULL, RSA_PUBLIC_DECRYPT,
  17828. &rng);
  17829. if (ret != BAD_FUNC_ARG) {
  17830. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17831. }
  17832. ret = wc_RsaDirect(out, outSz, NULL, &plainSz, key, RSA_PUBLIC_DECRYPT,
  17833. &rng);
  17834. if (ret != LENGTH_ONLY_E || plainSz != inLen) {
  17835. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17836. }
  17837. ret = wc_RsaDirect(out, outSz - 10, plain, &plainSz, key,
  17838. RSA_PUBLIC_DECRYPT, &rng);
  17839. if (ret != BAD_FUNC_ARG) {
  17840. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17841. }
  17842. /* if making it to this point of code without hitting an ERROR_OUT then
  17843. * all tests have passed */
  17844. ret = 0;
  17845. exit_rsa_nopadding:
  17846. wc_FreeRsaKey(key);
  17847. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17848. WC_FREE_VAR(key, HEAP_HINT);
  17849. WC_FREE_VAR(out, HEAP_HINT);
  17850. WC_FREE_VAR(plain, HEAP_HINT);
  17851. wc_FreeRng(&rng);
  17852. return ret;
  17853. }
  17854. #endif /* WC_RSA_NO_PADDING */
  17855. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  17856. static wc_test_ret_t rsa_even_mod_test(WC_RNG* rng, RsaKey* key)
  17857. {
  17858. byte* tmp = NULL;
  17859. size_t bytes;
  17860. wc_test_ret_t ret;
  17861. word32 inLen = 0;
  17862. #ifndef NO_ASN
  17863. word32 idx = 0;
  17864. #endif
  17865. word32 outSz = RSA_TEST_BYTES;
  17866. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17867. word32 plainSz = RSA_TEST_BYTES;
  17868. #endif
  17869. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_3072) && \
  17870. !defined(USE_CERT_BUFFERS_4096) && !defined(NO_FILESYSTEM)
  17871. XFILE file;
  17872. #endif
  17873. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17874. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17875. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  17876. #endif
  17877. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17878. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17879. WC_ALLOC_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  17880. #endif
  17881. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17882. if (out == NULL
  17883. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17884. || plain == NULL
  17885. #endif
  17886. ) {
  17887. ERROR_OUT(MEMORY_E, exit_rsa_even_mod);
  17888. }
  17889. #endif
  17890. #if defined(USE_CERT_BUFFERS_2048)
  17891. bytes = (size_t)sizeof_client_key_der_2048;
  17892. if (bytes < (size_t)sizeof_client_cert_der_2048)
  17893. bytes = (size_t)sizeof_client_cert_der_2048;
  17894. #else
  17895. bytes = FOURK_BUF;
  17896. #endif
  17897. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17898. if (tmp == NULL
  17899. #ifdef WOLFSSL_ASYNC_CRYPT
  17900. || out == NULL || plain == NULL
  17901. #endif
  17902. ) {
  17903. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_even_mod);
  17904. }
  17905. #if defined(USE_CERT_BUFFERS_2048)
  17906. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  17907. #elif defined(USE_CERT_BUFFERS_3072)
  17908. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  17909. #elif defined(USE_CERT_BUFFERS_4096)
  17910. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  17911. #elif !defined(NO_FILESYSTEM)
  17912. file = XFOPEN(clientKey, "rb");
  17913. if (!file) {
  17914. err_sys("can't open ./certs/client-key.der, "
  17915. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  17916. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_even_mod);
  17917. }
  17918. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  17919. XFCLOSE(file);
  17920. if (bytes == 0)
  17921. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_even_mod);
  17922. #else
  17923. /* No key to use. */
  17924. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_even_mod);
  17925. #endif /* USE_CERT_BUFFERS */
  17926. #ifndef NO_ASN
  17927. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  17928. if (ret != 0) {
  17929. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17930. }
  17931. #else
  17932. #ifdef USE_CERT_BUFFERS_2048
  17933. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  17934. if (ret != 0) {
  17935. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17936. }
  17937. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  17938. if (ret != 0) {
  17939. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17940. }
  17941. #ifndef NO_SIG_WRAPPER
  17942. modLen = 2048;
  17943. #endif
  17944. #else
  17945. #error Not supported yet!
  17946. #endif
  17947. #endif
  17948. key->n.dp[0] &= (mp_digit)-2;
  17949. if (ret != 0) {
  17950. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17951. }
  17952. /* after loading in key use tmp as the test buffer */
  17953. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  17954. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM))) && \
  17955. !defined(WOLFSSL_XILINX_CRYPT)
  17956. /* The ARM64_ASM code that was FIPS validated did not return these expected
  17957. * failure codes. These tests cases were added after the assembly was
  17958. * in-lined in the module and validated, these tests will be available in
  17959. * the 140-3 module */
  17960. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  17961. inLen = 32;
  17962. outSz = wc_RsaEncryptSize(key);
  17963. XMEMSET(tmp, 7, plainSz);
  17964. ret = wc_RsaSSL_Sign(tmp, inLen, out, outSz, key, rng);
  17965. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  17966. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17967. }
  17968. ret = wc_RsaSSL_Verify(out, outSz, tmp, inLen, key);
  17969. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  17970. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17971. }
  17972. #endif
  17973. #ifdef WC_RSA_BLINDING
  17974. ret = wc_RsaSetRNG(key, rng);
  17975. if (ret < 0) {
  17976. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17977. }
  17978. #endif
  17979. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  17980. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  17981. ret = wc_RsaPublicEncrypt(tmp, inLen, out, (int)outSz, key, rng);
  17982. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  17983. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17984. }
  17985. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  17986. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17987. ret = wc_RsaPrivateDecrypt(out, outSz, plain, (int)plainSz, key);
  17988. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  17989. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17990. }
  17991. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  17992. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  17993. /* if making it to this point of code without hitting an ERROR_OUT then
  17994. * all tests have passed */
  17995. ret = 0;
  17996. exit_rsa_even_mod:
  17997. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17998. WC_FREE_VAR(out, HEAP_HINT);
  17999. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18000. WC_FREE_VAR(plain, HEAP_HINT);
  18001. #endif
  18002. (void)out;
  18003. (void)outSz;
  18004. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18005. (void)plain;
  18006. (void)plainSz;
  18007. #endif
  18008. (void)inLen;
  18009. (void)rng;
  18010. return ret;
  18011. }
  18012. #endif /* WOLFSSL_HAVE_SP_RSA */
  18013. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  18014. static wc_test_ret_t rsa_certgen_test(RsaKey* key, RsaKey* keypub, WC_RNG* rng, byte* tmp)
  18015. {
  18016. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18017. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18018. #ifdef WOLFSSL_TEST_CERT
  18019. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18020. #endif
  18021. #else
  18022. RsaKey caKey[1];
  18023. #ifdef WOLFSSL_TEST_CERT
  18024. DecodedCert decode[1];
  18025. #endif
  18026. #endif
  18027. byte* der = NULL;
  18028. wc_test_ret_t ret;
  18029. Cert* myCert = NULL;
  18030. int certSz;
  18031. size_t bytes3;
  18032. word32 idx3 = 0;
  18033. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  18034. XFILE file3;
  18035. #endif
  18036. #if defined(WOLFSSL_ALT_NAMES) && !defined(NO_ASN_TIME)
  18037. struct tm beforeTime;
  18038. struct tm afterTime;
  18039. #endif
  18040. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  18041. (void)keypub;
  18042. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18043. if (caKey == NULL)
  18044. ERROR_OUT(MEMORY_E, exit_rsa);
  18045. #ifdef WOLFSSL_TEST_CERT
  18046. if (decode == NULL)
  18047. ERROR_OUT(MEMORY_E, exit_rsa);
  18048. #endif
  18049. #endif
  18050. XMEMSET(caKey, 0, sizeof *caKey);
  18051. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18052. if (der == NULL) {
  18053. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18054. }
  18055. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18056. if (myCert == NULL) {
  18057. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18058. }
  18059. /* self signed */
  18060. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  18061. if (ret != 0) {
  18062. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18063. }
  18064. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  18065. XMEMCPY(myCert->serial, mySerial, sizeof(mySerial));
  18066. myCert->serialSz = (int)sizeof(mySerial);
  18067. myCert->isCA = 1;
  18068. #ifndef NO_SHA256
  18069. myCert->sigType = CTC_SHA256wRSA;
  18070. #else
  18071. myCert->sigType = CTC_SHAwRSA;
  18072. #endif
  18073. #ifdef WOLFSSL_CERT_EXT
  18074. /* add Policies */
  18075. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  18076. CTC_MAX_CERTPOL_SZ);
  18077. XSTRNCPY(myCert->certPolicies[1], "1.2.840.113549.1.9.16.6.5",
  18078. CTC_MAX_CERTPOL_SZ);
  18079. myCert->certPoliciesNb = 2;
  18080. /* add SKID from the Public Key */
  18081. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, keypub, NULL);
  18082. if (ret != 0) {
  18083. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18084. }
  18085. /* add AKID from the Public Key */
  18086. ret = wc_SetAuthKeyIdFromPublicKey(myCert, keypub, NULL);
  18087. if (ret != 0) {
  18088. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18089. }
  18090. /* add Key Usage */
  18091. ret = wc_SetKeyUsage(myCert,"cRLSign,keyCertSign");
  18092. if (ret != 0) {
  18093. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18094. }
  18095. #ifdef WOLFSSL_EKU_OID
  18096. {
  18097. const char unique[] = "2.16.840.1.111111.100.1.10.1";
  18098. ret = wc_SetExtKeyUsageOID(myCert, unique, sizeof(unique), 0,
  18099. HEAP_HINT);
  18100. if (ret != 0) {
  18101. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18102. }
  18103. }
  18104. #endif /* WOLFSSL_EKU_OID */
  18105. #endif /* WOLFSSL_CERT_EXT */
  18106. do {
  18107. #if defined(WOLFSSL_ASYNC_CRYPT)
  18108. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18109. #endif
  18110. if (ret >= 0) {
  18111. ret = wc_MakeSelfCert(myCert, der, FOURK_BUF, key, rng);
  18112. }
  18113. } while (ret == WC_PENDING_E);
  18114. if (ret < 0) {
  18115. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18116. }
  18117. certSz = (int)ret;
  18118. #ifdef WOLFSSL_TEST_CERT
  18119. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  18120. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  18121. if (ret != 0) {
  18122. FreeDecodedCert(decode);
  18123. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18124. }
  18125. FreeDecodedCert(decode);
  18126. #endif
  18127. ret = SaveDerAndPem(der, certSz, certDerFile, certPemFile,
  18128. CERT_TYPE);
  18129. if (ret != 0) {
  18130. goto exit_rsa;
  18131. }
  18132. /* Setup Certificate */
  18133. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  18134. if (ret < 0) {
  18135. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18136. }
  18137. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  18138. /* Get CA Cert for testing */
  18139. #ifdef USE_CERT_BUFFERS_1024
  18140. XMEMCPY(tmp, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  18141. bytes3 = sizeof_ca_cert_der_1024;
  18142. #elif defined(USE_CERT_BUFFERS_2048)
  18143. XMEMCPY(tmp, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  18144. bytes3 = sizeof_ca_cert_der_2048;
  18145. #else
  18146. file3 = XFOPEN(rsaCaCertDerFile, "rb");
  18147. if (!file3) {
  18148. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18149. }
  18150. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  18151. XFCLOSE(file3);
  18152. if (bytes3 == 0)
  18153. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18154. #endif /* USE_CERT_BUFFERS */
  18155. #if defined(WOLFSSL_ALT_NAMES)
  18156. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_1024) && \
  18157. !defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  18158. ret = wc_SetAltNames(myCert, rsaCaCertFile);
  18159. if (ret != 0)
  18160. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18161. #endif
  18162. /* get alt names from der */
  18163. ret = wc_SetAltNamesBuffer(myCert, tmp, (int)bytes3);
  18164. if (ret != 0)
  18165. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18166. /* get dates from der */
  18167. ret = wc_SetDatesBuffer(myCert, tmp, (int)bytes3);
  18168. if (ret != 0)
  18169. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18170. #ifndef NO_ASN_TIME
  18171. ret = wc_GetCertDates(myCert, &beforeTime, &afterTime);
  18172. if (ret < 0)
  18173. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18174. #endif
  18175. #endif /* WOLFSSL_ALT_NAMES */
  18176. #endif /* WOLFSSL_ALT_NAMES || HAVE_PKCS7 */
  18177. /* Get CA Key */
  18178. #ifdef USE_CERT_BUFFERS_1024
  18179. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  18180. bytes3 = sizeof_ca_key_der_1024;
  18181. #elif defined(USE_CERT_BUFFERS_2048)
  18182. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  18183. bytes3 = sizeof_ca_key_der_2048;
  18184. #else
  18185. file3 = XFOPEN(rsaCaKeyFile, "rb");
  18186. if (!file3) {
  18187. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18188. }
  18189. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  18190. XFCLOSE(file3);
  18191. if (bytes3 == 0)
  18192. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18193. #endif /* USE_CERT_BUFFERS */
  18194. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  18195. if (ret != 0)
  18196. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18197. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  18198. if (ret != 0)
  18199. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18200. #ifndef NO_SHA256
  18201. myCert->sigType = CTC_SHA256wRSA;
  18202. #else
  18203. myCert->sigType = CTC_SHAwRSA;
  18204. #endif
  18205. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  18206. #ifdef WOLFSSL_CERT_EXT
  18207. /* add Policies */
  18208. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  18209. CTC_MAX_CERTPOL_SZ);
  18210. myCert->certPoliciesNb =1;
  18211. /* add SKID from the Public Key */
  18212. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, key, NULL);
  18213. if (ret != 0)
  18214. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18215. /* add AKID from the CA certificate */
  18216. #if defined(USE_CERT_BUFFERS_2048)
  18217. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  18218. sizeof_ca_cert_der_2048);
  18219. #elif defined(USE_CERT_BUFFERS_1024)
  18220. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  18221. sizeof_ca_cert_der_1024);
  18222. #else
  18223. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  18224. #endif
  18225. if (ret != 0)
  18226. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18227. /* add Key Usage */
  18228. ret = wc_SetKeyUsage(myCert,"keyEncipherment,keyAgreement");
  18229. if (ret != 0)
  18230. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18231. #endif /* WOLFSSL_CERT_EXT */
  18232. #if defined(USE_CERT_BUFFERS_2048)
  18233. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  18234. sizeof_ca_cert_der_2048);
  18235. #elif defined(USE_CERT_BUFFERS_1024)
  18236. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  18237. sizeof_ca_cert_der_1024);
  18238. #else
  18239. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  18240. #endif
  18241. if (ret < 0)
  18242. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18243. certSz = wc_MakeCert(myCert, der, FOURK_BUF, key, NULL, rng);
  18244. if (certSz < 0) {
  18245. ERROR_OUT(WC_TEST_RET_ENC_EC(certSz), exit_rsa);
  18246. }
  18247. ret = 0;
  18248. do {
  18249. #if defined(WOLFSSL_ASYNC_CRYPT)
  18250. ret = wc_AsyncWait(ret, &caKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18251. #endif
  18252. if (ret >= 0) {
  18253. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der, FOURK_BUF,
  18254. caKey, NULL, rng);
  18255. }
  18256. } while (ret == WC_PENDING_E);
  18257. if (ret < 0)
  18258. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18259. certSz = (int)ret;
  18260. #ifdef WOLFSSL_TEST_CERT
  18261. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  18262. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  18263. if (ret != 0) {
  18264. FreeDecodedCert(decode);
  18265. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18266. }
  18267. FreeDecodedCert(decode);
  18268. #endif
  18269. ret = SaveDerAndPem(der, certSz, otherCertDerFile, otherCertPemFile,
  18270. CERT_TYPE);
  18271. if (ret != 0) {
  18272. goto exit_rsa;
  18273. }
  18274. exit_rsa:
  18275. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18276. if (caKey != NULL) {
  18277. wc_FreeRsaKey(caKey);
  18278. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18279. }
  18280. #ifdef WOLFSSL_TEST_CERT
  18281. if (decode != NULL)
  18282. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18283. #endif
  18284. #else
  18285. wc_FreeRsaKey(caKey);
  18286. #endif
  18287. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18288. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18289. return ret;
  18290. }
  18291. #endif
  18292. #if !defined(NO_RSA) && defined(HAVE_ECC) && !defined(NO_ECC_SECP) && \
  18293. defined(WOLFSSL_CERT_GEN)
  18294. /* Make Cert / Sign example for ECC cert and RSA CA */
  18295. static wc_test_ret_t rsa_ecc_certgen_test(WC_RNG* rng, byte* tmp)
  18296. {
  18297. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18298. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18299. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18300. ecc_key *caEccKeyPub = (ecc_key *)XMALLOC(sizeof *caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18301. #ifdef WOLFSSL_TEST_CERT
  18302. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18303. #endif
  18304. #else
  18305. RsaKey caKey[1];
  18306. ecc_key caEccKey[1];
  18307. ecc_key caEccKeyPub[1];
  18308. #ifdef WOLFSSL_TEST_CERT
  18309. DecodedCert decode[1];
  18310. #endif
  18311. #endif
  18312. byte* der = NULL;
  18313. Cert* myCert = NULL;
  18314. int certSz;
  18315. size_t bytes3;
  18316. word32 idx3 = 0;
  18317. #if (!defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)) \
  18318. || !defined(USE_CERT_BUFFERS_256)
  18319. #ifndef NO_FILESYSTEM
  18320. XFILE file3;
  18321. #endif
  18322. #endif
  18323. wc_test_ret_t ret;
  18324. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18325. if ((caKey == NULL) || (caEccKey == NULL) || (caEccKeyPub == NULL)
  18326. #ifdef WOLFSSL_TEST_CERT
  18327. || (decode == NULL)
  18328. #endif
  18329. )
  18330. ERROR_OUT(MEMORY_E, exit_rsa);
  18331. #endif
  18332. XMEMSET(caKey, 0, sizeof *caKey);
  18333. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  18334. XMEMSET(caEccKeyPub, 0, sizeof *caEccKeyPub);
  18335. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18336. if (der == NULL) {
  18337. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18338. }
  18339. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18340. if (myCert == NULL) {
  18341. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18342. }
  18343. /* Get CA Key */
  18344. #ifdef USE_CERT_BUFFERS_1024
  18345. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  18346. bytes3 = sizeof_ca_key_der_1024;
  18347. #elif defined(USE_CERT_BUFFERS_2048)
  18348. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  18349. bytes3 = sizeof_ca_key_der_2048;
  18350. #else
  18351. file3 = XFOPEN(rsaCaKeyFile, "rb");
  18352. if (!file3) {
  18353. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18354. }
  18355. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  18356. XFCLOSE(file3);
  18357. if (bytes3 == 0)
  18358. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18359. #endif /* USE_CERT_BUFFERS */
  18360. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  18361. if (ret != 0)
  18362. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18363. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  18364. if (ret != 0)
  18365. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18366. /* Get Cert Key */
  18367. #if defined(USE_CERT_BUFFERS_256)
  18368. {
  18369. XMEMCPY(tmp, ecc_key_pub_der_256, sizeof_ecc_key_pub_der_256);
  18370. bytes3 = sizeof_ecc_key_pub_der_256;
  18371. }
  18372. #elif !defined(NO_FILESYSTEM)
  18373. {
  18374. file3 = XFOPEN(eccKeyPubFileDer, "rb");
  18375. if (!file3) {
  18376. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18377. }
  18378. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  18379. XFCLOSE(file3);
  18380. if (bytes3 == 0)
  18381. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18382. }
  18383. #else
  18384. {
  18385. WOLFSSL_MSG("No file system and USE_CERT_BUFFERS_256 not defined.(1)");
  18386. ERROR_OUT(ASN_PARSE_E, exit_rsa);
  18387. }
  18388. #endif
  18389. ret = wc_ecc_init_ex(caEccKeyPub, HEAP_HINT, devId);
  18390. if (ret != 0)
  18391. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18392. idx3 = 0;
  18393. ret = wc_EccPublicKeyDecode(tmp, &idx3, caEccKeyPub, (word32)bytes3);
  18394. if (ret != 0)
  18395. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18396. /* Setup Certificate */
  18397. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  18398. if (ret != 0)
  18399. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18400. #ifndef NO_SHA256
  18401. myCert->sigType = CTC_SHA256wRSA;
  18402. #else
  18403. myCert->sigType = CTC_SHAwRSA;
  18404. #endif
  18405. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  18406. #ifdef WOLFSSL_CERT_EXT
  18407. /* add Policies */
  18408. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  18409. CTC_MAX_CERTPOL_SZ);
  18410. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  18411. CTC_MAX_CERTPOL_SZ);
  18412. myCert->certPoliciesNb = 2;
  18413. /* add SKID from the Public Key */
  18414. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, caEccKeyPub);
  18415. if (ret != 0)
  18416. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18417. /* add AKID from the CA certificate */
  18418. #if defined(USE_CERT_BUFFERS_2048)
  18419. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  18420. sizeof_ca_cert_der_2048);
  18421. #elif defined(USE_CERT_BUFFERS_1024)
  18422. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  18423. sizeof_ca_cert_der_1024);
  18424. #else
  18425. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  18426. #endif
  18427. if (ret != 0)
  18428. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18429. /* add Key Usage */
  18430. ret = wc_SetKeyUsage(myCert, certKeyUsage);
  18431. if (ret != 0)
  18432. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18433. #endif /* WOLFSSL_CERT_EXT */
  18434. #if defined(USE_CERT_BUFFERS_2048)
  18435. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  18436. sizeof_ca_cert_der_2048);
  18437. #elif defined(USE_CERT_BUFFERS_1024)
  18438. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  18439. sizeof_ca_cert_der_1024);
  18440. #else
  18441. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  18442. #endif
  18443. if (ret < 0)
  18444. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18445. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, caEccKeyPub, rng);
  18446. if (certSz < 0) {
  18447. ERROR_OUT(WC_TEST_RET_ENC_EC(certSz), exit_rsa);
  18448. }
  18449. ret = 0;
  18450. do {
  18451. #if defined(WOLFSSL_ASYNC_CRYPT)
  18452. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18453. #endif
  18454. if (ret >= 0) {
  18455. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  18456. FOURK_BUF, caKey, NULL, rng);
  18457. }
  18458. } while (ret == WC_PENDING_E);
  18459. if (ret < 0)
  18460. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18461. certSz = (int)ret;
  18462. #ifdef WOLFSSL_TEST_CERT
  18463. InitDecodedCert(decode, der, certSz, 0);
  18464. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  18465. if (ret != 0) {
  18466. FreeDecodedCert(decode);
  18467. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18468. }
  18469. FreeDecodedCert(decode);
  18470. #endif
  18471. ret = SaveDerAndPem(der, certSz, certEccRsaDerFile, certEccRsaPemFile,
  18472. CERT_TYPE);
  18473. if (ret != 0) {
  18474. goto exit_rsa;
  18475. }
  18476. exit_rsa:
  18477. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18478. if (caKey != NULL) {
  18479. wc_FreeRsaKey(caKey);
  18480. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18481. }
  18482. if (caEccKey != NULL) {
  18483. wc_ecc_free(caEccKey);
  18484. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18485. }
  18486. if (caEccKeyPub != NULL) {
  18487. wc_ecc_free(caEccKeyPub);
  18488. XFREE(caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18489. }
  18490. #ifdef WOLFSSL_TEST_CERT
  18491. if (decode != NULL)
  18492. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18493. #endif
  18494. #else
  18495. wc_FreeRsaKey(caKey);
  18496. wc_ecc_free(caEccKey);
  18497. wc_ecc_free(caEccKeyPub);
  18498. #endif
  18499. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18500. myCert = NULL;
  18501. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18502. der = NULL;
  18503. if (ret >= 0)
  18504. ret = 0;
  18505. return ret;
  18506. }
  18507. #endif /* !NO_RSA && HAVE_ECC && WOLFSSL_CERT_GEN */
  18508. #ifdef WOLFSSL_KEY_GEN
  18509. static wc_test_ret_t rsa_keygen_test(WC_RNG* rng)
  18510. {
  18511. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18512. RsaKey *genKey = (RsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18513. #else
  18514. RsaKey genKey[1];
  18515. #endif
  18516. wc_test_ret_t ret;
  18517. #ifndef WOLFSSL_NO_MALLOC
  18518. byte* der = NULL;
  18519. #else
  18520. byte der[1024];
  18521. #endif
  18522. #ifndef WOLFSSL_CRYPTOCELL
  18523. word32 idx = 0;
  18524. #endif
  18525. int derSz = 0;
  18526. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FIPS)
  18527. int keySz = 1024;
  18528. #else
  18529. int keySz = 2048;
  18530. #endif
  18531. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18532. if (! genKey)
  18533. ERROR_OUT(MEMORY_E, exit_rsa);
  18534. #endif
  18535. XMEMSET(genKey, 0, sizeof *genKey);
  18536. ret = wc_InitRsaKey_ex(genKey, HEAP_HINT, devId);
  18537. if (ret != 0)
  18538. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18539. #ifdef HAVE_FIPS
  18540. for (;;) {
  18541. #endif
  18542. ret = wc_MakeRsaKey(genKey, keySz, WC_RSA_EXPONENT, rng);
  18543. #if defined(WOLFSSL_ASYNC_CRYPT)
  18544. ret = wc_AsyncWait(ret, &genKey->asyncDev, WC_ASYNC_FLAG_NONE);
  18545. #endif
  18546. #ifdef HAVE_FIPS
  18547. if (ret == PRIME_GEN_E)
  18548. continue;
  18549. break;
  18550. }
  18551. #endif
  18552. if (ret != 0)
  18553. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18554. TEST_SLEEP();
  18555. #ifdef WOLFSSL_RSA_KEY_CHECK
  18556. ret = wc_CheckRsaKey(genKey);
  18557. if (ret != 0)
  18558. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18559. #endif
  18560. #ifndef WOLFSSL_NO_MALLOC
  18561. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18562. if (der == NULL) {
  18563. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18564. }
  18565. #endif
  18566. derSz = wc_RsaKeyToDer(genKey, der, FOURK_BUF);
  18567. if (derSz < 0) {
  18568. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  18569. }
  18570. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  18571. PRIVATEKEY_TYPE);
  18572. if (ret != 0) {
  18573. goto exit_rsa;
  18574. }
  18575. wc_FreeRsaKey(genKey);
  18576. ret = wc_InitRsaKey(genKey, HEAP_HINT);
  18577. if (ret != 0)
  18578. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18579. #ifndef WOLFSSL_CRYPTOCELL
  18580. idx = 0;
  18581. /* The private key part of the key gen pairs from cryptocell can't be exported */
  18582. ret = wc_RsaPrivateKeyDecode(der, &idx, genKey, (word32)derSz);
  18583. if (ret != 0)
  18584. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18585. #endif /* WOLFSSL_CRYPTOCELL */
  18586. exit_rsa:
  18587. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18588. if (genKey) {
  18589. wc_FreeRsaKey(genKey);
  18590. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18591. }
  18592. #else
  18593. wc_FreeRsaKey(genKey);
  18594. #endif
  18595. #ifndef WOLFSSL_NO_MALLOC
  18596. if (der != NULL) {
  18597. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18598. der = NULL;
  18599. }
  18600. #endif
  18601. return ret;
  18602. }
  18603. #endif
  18604. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  18605. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG) && \
  18606. (!defined(HAVE_FIPS) || \
  18607. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  18608. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  18609. static wc_test_ret_t rsa_oaep_padding_test(RsaKey* key, WC_RNG* rng)
  18610. {
  18611. wc_test_ret_t ret = 0;
  18612. word32 idx = 0;
  18613. const char inStr[] = TEST_STRING;
  18614. const word32 inLen = (word32)TEST_STRING_SZ;
  18615. const word32 outSz = RSA_TEST_BYTES;
  18616. const word32 plainSz = RSA_TEST_BYTES;
  18617. byte* res = NULL;
  18618. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  18619. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  18620. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  18621. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  18622. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  18623. WC_ALLOC_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  18624. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  18625. if (in == NULL || out == NULL || plain == NULL)
  18626. ERROR_OUT(MEMORY_E, exit_rsa);
  18627. #endif
  18628. XMEMCPY(in, inStr, inLen);
  18629. #ifndef NO_SHA
  18630. do {
  18631. #if defined(WOLFSSL_ASYNC_CRYPT)
  18632. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18633. #endif
  18634. if (ret >= 0) {
  18635. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18636. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  18637. }
  18638. } while (ret == WC_PENDING_E);
  18639. if (ret < 0)
  18640. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18641. TEST_SLEEP();
  18642. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18643. idx = (word32)ret;
  18644. do {
  18645. #if defined(WOLFSSL_ASYNC_CRYPT)
  18646. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18647. #endif
  18648. if (ret >= 0) {
  18649. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18650. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  18651. }
  18652. } while (ret == WC_PENDING_E);
  18653. if (ret < 0)
  18654. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18655. if (XMEMCMP(plain, in, inLen)) {
  18656. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18657. }
  18658. TEST_SLEEP();
  18659. #endif /* NO_SHA */
  18660. #endif
  18661. #ifndef NO_SHA256
  18662. XMEMSET(plain, 0, plainSz);
  18663. do {
  18664. #if defined(WOLFSSL_ASYNC_CRYPT)
  18665. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18666. #endif
  18667. if (ret >= 0) {
  18668. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18669. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  18670. }
  18671. } while (ret == WC_PENDING_E);
  18672. if (ret < 0)
  18673. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18674. TEST_SLEEP();
  18675. idx = (word32)ret;
  18676. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18677. do {
  18678. #if defined(WOLFSSL_ASYNC_CRYPT)
  18679. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18680. #endif
  18681. if (ret >= 0) {
  18682. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18683. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  18684. }
  18685. } while (ret == WC_PENDING_E);
  18686. if (ret < 0)
  18687. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18688. if (XMEMCMP(plain, in, inLen)) {
  18689. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18690. }
  18691. TEST_SLEEP();
  18692. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  18693. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18694. do {
  18695. #if defined(WOLFSSL_ASYNC_CRYPT)
  18696. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18697. #endif
  18698. if (ret >= 0) {
  18699. ret = wc_RsaPrivateDecryptInline_ex(out, idx, &res, key,
  18700. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  18701. }
  18702. } while (ret == WC_PENDING_E);
  18703. if (ret < 0)
  18704. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18705. if (ret != (int)inLen) {
  18706. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18707. }
  18708. if (XMEMCMP(res, in, inLen)) {
  18709. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18710. }
  18711. TEST_SLEEP();
  18712. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  18713. /* check fails if not using the same optional label */
  18714. XMEMSET(plain, 0, plainSz);
  18715. do {
  18716. #if defined(WOLFSSL_ASYNC_CRYPT)
  18717. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18718. #endif
  18719. if (ret >= 0) {
  18720. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18721. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  18722. }
  18723. } while (ret == WC_PENDING_E);
  18724. if (ret < 0)
  18725. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18726. TEST_SLEEP();
  18727. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  18728. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  18729. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  18730. /* label is unused in cryptocell and SE050 so it won't detect decrypt error
  18731. * due to label */
  18732. idx = (word32)ret;
  18733. do {
  18734. #if defined(WOLFSSL_ASYNC_CRYPT)
  18735. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18736. #endif
  18737. if (ret >= 0) {
  18738. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18739. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  18740. }
  18741. } while (ret == WC_PENDING_E);
  18742. if (ret > 0) { /* in this case decrypt should fail */
  18743. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18744. }
  18745. ret = 0;
  18746. TEST_SLEEP();
  18747. #endif /* !HAVE_CAVIUM */
  18748. /* check using optional label with encrypt/decrypt */
  18749. XMEMSET(plain, 0, plainSz);
  18750. do {
  18751. #if defined(WOLFSSL_ASYNC_CRYPT)
  18752. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18753. #endif
  18754. if (ret >= 0) {
  18755. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18756. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  18757. }
  18758. } while (ret == WC_PENDING_E);
  18759. if (ret < 0)
  18760. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18761. TEST_SLEEP();
  18762. idx = (word32)ret;
  18763. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18764. do {
  18765. #if defined(WOLFSSL_ASYNC_CRYPT)
  18766. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18767. #endif
  18768. if (ret >= 0) {
  18769. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18770. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  18771. }
  18772. } while (ret == WC_PENDING_E);
  18773. if (ret < 0)
  18774. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18775. if (XMEMCMP(plain, in, inLen)) {
  18776. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18777. }
  18778. TEST_SLEEP();
  18779. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  18780. #ifndef NO_SHA
  18781. /* check fail using mismatch hash algorithms */
  18782. XMEMSET(plain, 0, plainSz);
  18783. do {
  18784. #if defined(WOLFSSL_ASYNC_CRYPT)
  18785. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18786. #endif
  18787. if (ret >= 0) {
  18788. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18789. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, in, inLen);
  18790. }
  18791. } while (ret == WC_PENDING_E);
  18792. if (ret < 0)
  18793. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18794. TEST_SLEEP();
  18795. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  18796. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  18797. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  18798. idx = (word32)ret;
  18799. do {
  18800. #if defined(WOLFSSL_ASYNC_CRYPT)
  18801. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18802. #endif
  18803. if (ret >= 0) {
  18804. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18805. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256,
  18806. in, inLen);
  18807. }
  18808. } while (ret == WC_PENDING_E);
  18809. if (ret > 0) { /* should fail */
  18810. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18811. }
  18812. ret = 0;
  18813. TEST_SLEEP();
  18814. #endif /* !HAVE_CAVIUM */
  18815. #endif /* NO_SHA */
  18816. #endif /* NO_SHA256 */
  18817. #ifdef WOLFSSL_SHA512
  18818. /* Check valid RSA key size is used while using hash length of SHA512
  18819. If key size is less than (hash length * 2) + 2 then is invalid use
  18820. and test, since OAEP padding requires this.
  18821. BAD_FUNC_ARG is returned when this case is not met */
  18822. if (wc_RsaEncryptSize(key) > ((int)WC_SHA512_DIGEST_SIZE * 2) + 2) {
  18823. XMEMSET(plain, 0, plainSz);
  18824. do {
  18825. #if defined(WOLFSSL_ASYNC_CRYPT)
  18826. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18827. #endif
  18828. if (ret >= 0) {
  18829. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18830. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  18831. }
  18832. } while (ret == WC_PENDING_E);
  18833. if (ret < 0)
  18834. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18835. TEST_SLEEP();
  18836. idx = (word32)ret;
  18837. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18838. do {
  18839. #if defined(WOLFSSL_ASYNC_CRYPT)
  18840. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18841. #endif
  18842. if (ret >= 0) {
  18843. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18844. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  18845. }
  18846. } while (ret == WC_PENDING_E);
  18847. if (ret < 0)
  18848. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18849. if (XMEMCMP(plain, in, inLen)) {
  18850. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18851. }
  18852. TEST_SLEEP();
  18853. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  18854. }
  18855. #endif /* WOLFSSL_SHA512 */
  18856. /* check using pkcsv15 padding with _ex API */
  18857. XMEMSET(plain, 0, plainSz);
  18858. do {
  18859. #if defined(WOLFSSL_ASYNC_CRYPT)
  18860. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18861. #endif
  18862. if (ret >= 0) {
  18863. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18864. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  18865. }
  18866. } while (ret == WC_PENDING_E);
  18867. if (ret < 0)
  18868. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18869. TEST_SLEEP();
  18870. idx = (word32)ret;
  18871. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18872. do {
  18873. #if defined(WOLFSSL_ASYNC_CRYPT)
  18874. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18875. #endif
  18876. if (ret >= 0) {
  18877. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18878. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  18879. }
  18880. } while (ret == WC_PENDING_E);
  18881. if (ret < 0)
  18882. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18883. if (XMEMCMP(plain, in, inLen)) {
  18884. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18885. }
  18886. TEST_SLEEP();
  18887. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  18888. exit_rsa:
  18889. WC_FREE_VAR(in, HEAP_HINT);
  18890. WC_FREE_VAR(out, HEAP_HINT);
  18891. WC_FREE_VAR(plain, HEAP_HINT);
  18892. (void)idx;
  18893. (void)inStr;
  18894. (void)res;
  18895. if (ret >= 0)
  18896. ret = 0;
  18897. return ret;
  18898. }
  18899. #endif
  18900. #endif
  18901. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void)
  18902. {
  18903. wc_test_ret_t ret;
  18904. size_t bytes;
  18905. WC_RNG rng;
  18906. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18907. byte* tmp = NULL;
  18908. byte* der = NULL;
  18909. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18910. #else
  18911. RsaKey key[1];
  18912. byte tmp[FOURK_BUF];
  18913. #endif
  18914. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  18915. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18916. RsaKey *keypub = (RsaKey *)XMALLOC(sizeof *keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18917. #else
  18918. RsaKey keypub[1];
  18919. #endif
  18920. #endif
  18921. word32 idx = 0;
  18922. const char inStr[] = TEST_STRING;
  18923. const word32 inLen = (word32)TEST_STRING_SZ;
  18924. const word32 outSz = RSA_TEST_BYTES;
  18925. const word32 plainSz = RSA_TEST_BYTES;
  18926. byte* res = NULL;
  18927. #ifndef NO_SIG_WRAPPER
  18928. int modLen;
  18929. #endif
  18930. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  18931. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  18932. !defined(NO_FILESYSTEM)
  18933. XFILE file;
  18934. #ifdef WOLFSSL_TEST_CERT
  18935. XFILE file2;
  18936. #endif
  18937. #endif
  18938. #ifdef WOLFSSL_TEST_CERT
  18939. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18940. DecodedCert *cert = (DecodedCert *)XMALLOC(sizeof *cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18941. #else
  18942. DecodedCert cert[1];
  18943. #endif
  18944. #ifndef NO_ASN_TIME
  18945. struct tm timearg;
  18946. const byte* date;
  18947. byte dateFormat;
  18948. int dateLength;
  18949. #endif
  18950. #endif
  18951. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  18952. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  18953. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  18954. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  18955. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  18956. WC_ALLOC_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  18957. WOLFSSL_ENTER("rsa_test");
  18958. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  18959. if (in == NULL || out == NULL || plain == NULL)
  18960. ERROR_OUT(MEMORY_E, exit_rsa);
  18961. #endif
  18962. XMEMCPY(in, inStr, inLen);
  18963. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18964. if (key == NULL)
  18965. ERROR_OUT(MEMORY_E, exit_rsa);
  18966. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  18967. if (keypub == NULL)
  18968. ERROR_OUT(MEMORY_E, exit_rsa);
  18969. #endif
  18970. #ifdef WOLFSSL_TEST_CERT
  18971. if (cert == NULL)
  18972. ERROR_OUT(MEMORY_E, exit_rsa);
  18973. #endif
  18974. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  18975. /* initialize stack structures */
  18976. XMEMSET(&rng, 0, sizeof(rng));
  18977. XMEMSET(key, 0, sizeof *key);
  18978. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  18979. XMEMSET(keypub, 0, sizeof *keypub);
  18980. #endif
  18981. #if !defined(NO_ASN)
  18982. ret = rsa_decode_test(key);
  18983. if (ret != 0)
  18984. ERROR_OUT(ret, exit_rsa);
  18985. #endif
  18986. #ifdef USE_CERT_BUFFERS_1024
  18987. bytes = (size_t)sizeof_client_key_der_1024;
  18988. if (bytes < (size_t)sizeof_client_cert_der_1024)
  18989. bytes = (size_t)sizeof_client_cert_der_1024;
  18990. #elif defined(USE_CERT_BUFFERS_2048)
  18991. bytes = (size_t)sizeof_client_key_der_2048;
  18992. if (bytes < (size_t)sizeof_client_cert_der_2048)
  18993. bytes = (size_t)sizeof_client_cert_der_2048;
  18994. #elif defined(USE_CERT_BUFFERS_3072)
  18995. bytes = (size_t)sizeof_client_key_der_3072;
  18996. if (bytes < (size_t)sizeof_client_cert_der_3072)
  18997. bytes = (size_t)sizeof_client_cert_der_3072;
  18998. #elif defined(USE_CERT_BUFFERS_4096)
  18999. bytes = (size_t)sizeof_client_key_der_4096;
  19000. if (bytes < (size_t)sizeof_client_cert_der_4096)
  19001. bytes = (size_t)sizeof_client_cert_der_4096;
  19002. #else
  19003. bytes = FOURK_BUF;
  19004. #endif
  19005. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19006. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19007. if (tmp == NULL)
  19008. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19009. #endif
  19010. #ifdef USE_CERT_BUFFERS_1024
  19011. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  19012. #elif defined(USE_CERT_BUFFERS_2048)
  19013. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  19014. #elif defined(USE_CERT_BUFFERS_3072)
  19015. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  19016. #elif defined(USE_CERT_BUFFERS_4096)
  19017. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  19018. #elif !defined(NO_FILESYSTEM)
  19019. file = XFOPEN(clientKey, "rb");
  19020. if (!file) {
  19021. err_sys("can't open ./certs/client-key.der, "
  19022. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  19023. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19024. }
  19025. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  19026. XFCLOSE(file);
  19027. if (bytes == 0)
  19028. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19029. #else
  19030. /* No key to use. */
  19031. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  19032. #endif /* USE_CERT_BUFFERS */
  19033. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  19034. if (ret != 0)
  19035. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19036. #ifndef NO_ASN
  19037. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  19038. if (ret != 0)
  19039. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19040. #ifndef NO_SIG_WRAPPER
  19041. modLen = wc_RsaEncryptSize(key);
  19042. #endif
  19043. #else
  19044. #ifdef USE_CERT_BUFFERS_2048
  19045. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  19046. if (ret != 0)
  19047. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19048. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  19049. if (ret != 0)
  19050. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19051. #ifndef NO_SIG_WRAPPER
  19052. modLen = 2048;
  19053. #endif
  19054. #else
  19055. #error Not supported yet!
  19056. #endif
  19057. #endif
  19058. #ifndef WC_NO_RNG
  19059. #ifndef HAVE_FIPS
  19060. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  19061. #else
  19062. ret = wc_InitRng(&rng);
  19063. #endif
  19064. if (ret != 0)
  19065. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19066. #endif
  19067. #ifndef NO_SIG_WRAPPER
  19068. ret = rsa_sig_test(key, sizeof *key, modLen, &rng);
  19069. if (ret != 0)
  19070. goto exit_rsa;
  19071. #endif
  19072. #ifdef WC_RSA_NONBLOCK
  19073. ret = rsa_nb_test(key, in, inLen, out, outSz, plain, plainSz, &rng);
  19074. if (ret != 0)
  19075. goto exit_rsa;
  19076. #endif
  19077. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  19078. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  19079. do {
  19080. #if defined(WOLFSSL_ASYNC_CRYPT)
  19081. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19082. #endif
  19083. if (ret >= 0) {
  19084. ret = wc_RsaPublicEncrypt(in, inLen, out, outSz, key, &rng);
  19085. }
  19086. } while (ret == WC_PENDING_E);
  19087. if (ret < 0)
  19088. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19089. TEST_SLEEP();
  19090. #ifdef WC_RSA_BLINDING
  19091. {
  19092. wc_test_ret_t tmpret = ret;
  19093. ret = wc_RsaSetRNG(key, &rng);
  19094. if (ret < 0)
  19095. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19096. ret = tmpret;
  19097. }
  19098. #endif
  19099. idx = (word32)ret; /* save off encrypted length */
  19100. do {
  19101. #if defined(WOLFSSL_ASYNC_CRYPT)
  19102. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19103. #endif
  19104. if (ret >= 0) {
  19105. ret = wc_RsaPrivateDecrypt(out, idx, plain, plainSz, key);
  19106. }
  19107. } while (ret == WC_PENDING_E);
  19108. if (ret < 0)
  19109. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19110. if (XMEMCMP(plain, in, inLen)) {
  19111. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  19112. }
  19113. TEST_SLEEP();
  19114. do {
  19115. #if defined(WOLFSSL_ASYNC_CRYPT)
  19116. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19117. #endif
  19118. if (ret >= 0) {
  19119. ret = wc_RsaPrivateDecryptInline(out, idx, &res, key);
  19120. }
  19121. } while (ret == WC_PENDING_E);
  19122. if (ret < 0)
  19123. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19124. if (ret != (int)inLen) {
  19125. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  19126. }
  19127. if (XMEMCMP(res, in, inLen)) {
  19128. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  19129. }
  19130. TEST_SLEEP();
  19131. do {
  19132. #if defined(WOLFSSL_ASYNC_CRYPT)
  19133. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19134. #endif
  19135. if (ret >= 0) {
  19136. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, &rng);
  19137. }
  19138. } while (ret == WC_PENDING_E);
  19139. if (ret < 0)
  19140. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19141. TEST_SLEEP();
  19142. #elif defined(WOLFSSL_PUBLIC_MP)
  19143. {
  19144. static const byte signature_2048[] = {
  19145. 0x07, 0x6f, 0xc9, 0x85, 0x73, 0x9e, 0x21, 0x79,
  19146. 0x47, 0xf1, 0xa3, 0xd7, 0xf4, 0x27, 0x29, 0xbe,
  19147. 0x99, 0x5d, 0xac, 0xb2, 0x10, 0x3f, 0x95, 0xda,
  19148. 0x89, 0x23, 0xb8, 0x96, 0x13, 0x57, 0x72, 0x30,
  19149. 0xa1, 0xfe, 0x5a, 0x68, 0x9c, 0x99, 0x9d, 0x1e,
  19150. 0x05, 0xa4, 0x80, 0xb0, 0xbb, 0xd9, 0xd9, 0xa1,
  19151. 0x69, 0x97, 0x74, 0xb3, 0x41, 0x21, 0x3b, 0x47,
  19152. 0xf5, 0x51, 0xb1, 0xfb, 0xc7, 0xaa, 0xcc, 0xdc,
  19153. 0xcd, 0x76, 0xa0, 0x28, 0x4d, 0x27, 0x14, 0xa4,
  19154. 0xb9, 0x41, 0x68, 0x7c, 0xb3, 0x66, 0xe6, 0x6f,
  19155. 0x40, 0x76, 0xe4, 0x12, 0xfd, 0xae, 0x29, 0xb5,
  19156. 0x63, 0x60, 0x87, 0xce, 0x49, 0x6b, 0xf3, 0x05,
  19157. 0x9a, 0x14, 0xb5, 0xcc, 0xcd, 0xf7, 0x30, 0x95,
  19158. 0xd2, 0x72, 0x52, 0x1d, 0x5b, 0x7e, 0xef, 0x4a,
  19159. 0x02, 0x96, 0x21, 0x6c, 0x55, 0xa5, 0x15, 0xb1,
  19160. 0x57, 0x63, 0x2c, 0xa3, 0x8e, 0x9d, 0x3d, 0x45,
  19161. 0xcc, 0xb8, 0xe6, 0xa1, 0xc8, 0x59, 0xcd, 0xf5,
  19162. 0xdc, 0x0a, 0x51, 0xb6, 0x9d, 0xfb, 0xf4, 0x6b,
  19163. 0xfd, 0x32, 0x71, 0x6e, 0xcf, 0xcb, 0xb3, 0xd9,
  19164. 0xe0, 0x4a, 0x77, 0x34, 0xd6, 0x61, 0xf5, 0x7c,
  19165. 0xf9, 0xa9, 0xa4, 0xb0, 0x8e, 0x3b, 0xd6, 0x04,
  19166. 0xe0, 0xde, 0x2b, 0x5b, 0x5a, 0xbf, 0xd9, 0xef,
  19167. 0x8d, 0xa3, 0xf5, 0xb1, 0x67, 0xf3, 0xb9, 0x72,
  19168. 0x0a, 0x37, 0x12, 0x35, 0x6c, 0x8e, 0x10, 0x8b,
  19169. 0x38, 0x06, 0x16, 0x4b, 0x20, 0x20, 0x13, 0x00,
  19170. 0x2e, 0x6d, 0xc2, 0x59, 0x23, 0x67, 0x4a, 0x6d,
  19171. 0xa1, 0x46, 0x8b, 0xee, 0xcf, 0x44, 0xb4, 0x3e,
  19172. 0x56, 0x75, 0x00, 0x68, 0xb5, 0x7d, 0x0f, 0x20,
  19173. 0x79, 0x5d, 0x7f, 0x12, 0x15, 0x32, 0x89, 0x61,
  19174. 0x6b, 0x29, 0xb7, 0x52, 0xf5, 0x25, 0xd8, 0x98,
  19175. 0xe8, 0x6f, 0xf9, 0x22, 0xb4, 0xbb, 0xe5, 0xff,
  19176. 0xd0, 0x92, 0x86, 0x9a, 0x88, 0xa2, 0xaf, 0x6b
  19177. };
  19178. ret = sizeof(signature_2048);
  19179. XMEMCPY(out, signature_2048, ret);
  19180. }
  19181. #endif
  19182. #if !defined(WC_NO_RNG) && !defined(WC_NO_RSA_OAEP) && \
  19183. ((!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || \
  19184. defined(WOLFSSL_PUBLIC_MP)) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  19185. idx = (word32)ret;
  19186. XMEMSET(plain, 0, plainSz);
  19187. do {
  19188. #if defined(WOLFSSL_ASYNC_CRYPT)
  19189. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19190. #endif
  19191. if (ret >= 0) {
  19192. #ifndef WOLFSSL_RSA_VERIFY_INLINE
  19193. #if defined(WOLFSSL_CRYPTOCELL)
  19194. /*
  19195. Cryptocell requires the input data and signature byte array to verify.
  19196. first argument must be the input data
  19197. second argument must be the length of input data
  19198. third argument must be the signature byte array or the output from
  19199. wc_RsaSSL_Sign()
  19200. fourth argument must be the length of the signature byte array
  19201. */
  19202. ret = wc_RsaSSL_Verify(in, inLen, out, outSz, key);
  19203. #else
  19204. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, key);
  19205. #endif /* WOLFSSL_CRYPTOCELL */
  19206. #else
  19207. byte* dec = NULL;
  19208. ret = wc_RsaSSL_VerifyInline(out, idx, &dec, key);
  19209. if (ret > 0) {
  19210. XMEMCPY(plain, dec, ret);
  19211. }
  19212. #endif
  19213. }
  19214. } while (ret == WC_PENDING_E);
  19215. if (ret < 0)
  19216. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19217. if (XMEMCMP(plain, in, (size_t)ret)) {
  19218. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  19219. }
  19220. TEST_SLEEP();
  19221. #endif
  19222. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  19223. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  19224. #if (!defined(HAVE_FIPS) || \
  19225. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  19226. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  19227. ret = rsa_oaep_padding_test(key, &rng);
  19228. if (ret != 0)
  19229. return ret;
  19230. #endif /* !HAVE_FIPS */
  19231. #endif /* WC_NO_RSA_OAEP && !WC_NO_RNG */
  19232. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  19233. #if !defined(HAVE_FIPS) && !defined(NO_ASN) \
  19234. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  19235. ret = rsa_export_key_test(key);
  19236. if (ret != 0)
  19237. return ret;
  19238. #endif
  19239. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  19240. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  19241. ret = rsa_flatten_test(key);
  19242. if (ret != 0)
  19243. return ret;
  19244. #endif
  19245. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_ASN) && \
  19246. !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  19247. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  19248. (void)clientCert;
  19249. #endif
  19250. #ifdef WOLFSSL_TEST_CERT
  19251. #if defined(WOLFSSL_MDK_ARM)
  19252. #define sizeof(s) XSTRLEN((char *)(s))
  19253. #endif
  19254. #ifdef USE_CERT_BUFFERS_1024
  19255. XMEMCPY(tmp, client_cert_der_1024, (size_t)sizeof_client_cert_der_1024);
  19256. bytes = (size_t)sizeof_client_cert_der_1024;
  19257. #elif defined(USE_CERT_BUFFERS_2048)
  19258. XMEMCPY(tmp, client_cert_der_2048, (size_t)sizeof_client_cert_der_2048);
  19259. bytes = (size_t)sizeof_client_cert_der_2048;
  19260. #elif defined(USE_CERT_BUFFERS_3072)
  19261. XMEMCPY(tmp, client_cert_der_3072, (size_t)sizeof_client_cert_der_3072);
  19262. bytes = (size_t)sizeof_client_cert_der_3072;
  19263. #elif defined(USE_CERT_BUFFERS_4096)
  19264. XMEMCPY(tmp, client_cert_der_4096, (size_t)sizeof_client_cert_der_4096);
  19265. bytes = (size_t)sizeof_client_cert_der_4096;
  19266. #elif !defined(NO_FILESYSTEM)
  19267. file2 = XFOPEN(clientCert, "rb");
  19268. if (!file2) {
  19269. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19270. }
  19271. bytes = XFREAD(tmp, 1, FOURK_BUF, file2);
  19272. XFCLOSE(file2);
  19273. if (bytes == 0)
  19274. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19275. #else
  19276. /* No certificate to use. */
  19277. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  19278. #endif
  19279. #ifdef sizeof
  19280. #undef sizeof
  19281. #endif
  19282. InitDecodedCert(cert, tmp, (word32)bytes, NULL);
  19283. ret = ParseCert(cert, CERT_TYPE, NO_VERIFY, NULL);
  19284. if (ret != 0) {
  19285. FreeDecodedCert(cert);
  19286. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19287. }
  19288. #ifndef NO_ASN_TIME
  19289. ret = wc_GetDateInfo(cert->afterDate, cert->afterDateLen, &date,
  19290. &dateFormat, &dateLength);
  19291. if (ret != 0) {
  19292. FreeDecodedCert(cert);
  19293. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19294. }
  19295. ret = wc_GetDateAsCalendarTime(date, dateLength, dateFormat, &timearg);
  19296. if (ret != 0) {
  19297. FreeDecodedCert(cert);
  19298. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19299. }
  19300. #endif
  19301. FreeDecodedCert(cert);
  19302. #endif /* WOLFSSL_TEST_CERT */
  19303. #ifdef WOLFSSL_CERT_EXT
  19304. #ifdef USE_CERT_BUFFERS_1024
  19305. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  19306. bytes = sizeof_client_keypub_der_1024;
  19307. #elif defined(USE_CERT_BUFFERS_2048)
  19308. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  19309. bytes = sizeof_client_keypub_der_2048;
  19310. #elif defined(USE_CERT_BUFFERS_3072)
  19311. XMEMCPY(tmp, client_keypub_der_3072, sizeof_client_keypub_der_3072);
  19312. bytes = sizeof_client_keypub_der_3072;
  19313. #elif defined(USE_CERT_BUFFERS_4096)
  19314. XMEMCPY(tmp, client_keypub_der_4096, sizeof_client_keypub_der_4096);
  19315. bytes = sizeof_client_keypub_der_4096;
  19316. #else
  19317. file = XFOPEN(clientKeyPub, "rb");
  19318. if (!file) {
  19319. err_sys("can't open ./certs/client-keyPub.der, "
  19320. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  19321. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19322. }
  19323. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  19324. XFCLOSE(file);
  19325. if (bytes == 0)
  19326. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19327. #endif /* USE_CERT_BUFFERS */
  19328. ret = wc_InitRsaKey(keypub, HEAP_HINT);
  19329. if (ret != 0)
  19330. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19331. idx = 0;
  19332. ret = wc_RsaPublicKeyDecode(tmp, &idx, keypub, (word32)bytes);
  19333. if (ret != 0)
  19334. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19335. #endif /* WOLFSSL_CERT_EXT */
  19336. #ifdef WOLFSSL_KEY_GEN
  19337. ret = rsa_keygen_test(&rng);
  19338. if (ret != 0)
  19339. goto exit_rsa;
  19340. #endif
  19341. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  19342. /* Make Cert / Sign example for RSA cert and RSA CA */
  19343. ret = rsa_certgen_test(key, keypub, &rng, tmp);
  19344. if (ret != 0)
  19345. goto exit_rsa;
  19346. #if !defined(NO_RSA) && defined(HAVE_ECC) && !defined(NO_ECC_SECP)
  19347. ret = rsa_ecc_certgen_test(&rng, tmp);
  19348. if (ret != 0)
  19349. goto exit_rsa;
  19350. #endif
  19351. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  19352. {
  19353. Cert *req;
  19354. int derSz;
  19355. #ifndef WOLFSSL_SMALL_STACK
  19356. byte* der = NULL;
  19357. #endif
  19358. req = (Cert *)XMALLOC(sizeof *req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19359. if (! req)
  19360. ERROR_OUT(MEMORY_E, exit_rsa);
  19361. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  19362. if (der == NULL) {
  19363. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19364. }
  19365. ret = wc_InitCert_ex(req, HEAP_HINT, devId);
  19366. if (ret != 0)
  19367. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19368. req->version = 0;
  19369. req->isCA = 1;
  19370. XSTRNCPY(req->challengePw, "wolf123", CTC_NAME_SIZE);
  19371. XMEMCPY(&req->subject, &certDefaultName, sizeof(CertName));
  19372. #ifndef NO_SHA256
  19373. req->sigType = CTC_SHA256wRSA;
  19374. #else
  19375. req->sigType = CTC_SHAwRSA;
  19376. #endif
  19377. #ifdef WOLFSSL_CERT_EXT
  19378. /* add SKID from the Public Key */
  19379. ret = wc_SetSubjectKeyIdFromPublicKey(req, keypub, NULL);
  19380. if (ret != 0)
  19381. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19382. /* add Key Usage */
  19383. ret = wc_SetKeyUsage(req, certKeyUsage2);
  19384. if (ret != 0)
  19385. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19386. /* add Extended Key Usage */
  19387. ret = wc_SetExtKeyUsage(req,
  19388. "serverAuth,clientAuth,codeSigning,"
  19389. "emailProtection,timeStamping,OCSPSigning");
  19390. if (ret != 0)
  19391. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19392. #ifdef WOLFSSL_EKU_OID
  19393. {
  19394. WOLFSSL_SMALL_STACK_STATIC const char unique[] =
  19395. "2.16.840.1.111111.100.1.10.1";
  19396. ret = wc_SetExtKeyUsageOID(req, unique, sizeof(unique), 0,
  19397. HEAP_HINT);
  19398. if (ret != 0)
  19399. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19400. }
  19401. #endif /* WOLFSSL_EKU_OID */
  19402. #endif /* WOLFSSL_CERT_EXT */
  19403. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  19404. if (derSz < 0) {
  19405. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  19406. }
  19407. #ifdef WOLFSSL_CERT_EXT
  19408. /* Try again with "any" flag set, will override all others */
  19409. ret = wc_SetExtKeyUsage(req, "any");
  19410. if (ret != 0)
  19411. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19412. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  19413. if (derSz < 0) {
  19414. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  19415. }
  19416. #endif /* WOLFSSL_CERT_EXT */
  19417. ret = 0;
  19418. do {
  19419. #if defined(WOLFSSL_ASYNC_CRYPT)
  19420. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19421. #endif
  19422. if (ret >= 0) {
  19423. ret = wc_SignCert(req->bodySz, req->sigType, der, FOURK_BUF,
  19424. key, NULL, &rng);
  19425. }
  19426. } while (ret == WC_PENDING_E);
  19427. if (ret < 0)
  19428. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19429. derSz = (int)ret;
  19430. ret = SaveDerAndPem(der, derSz, certReqDerFile, certReqPemFile,
  19431. CERTREQ_TYPE);
  19432. if (ret != 0) {
  19433. goto exit_rsa;
  19434. }
  19435. derSz = wc_MakeCertReq_ex(req, der, FOURK_BUF, RSA_TYPE, key);
  19436. if (derSz < 0) {
  19437. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  19438. }
  19439. /* Test getting the size of the buffer without providing the buffer.
  19440. * derSz is set to the "largest buffer" we are willing to allocate. */
  19441. derSz = wc_MakeCertReq(req, NULL, 10000, key, NULL);
  19442. if (derSz < 0) {
  19443. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  19444. }
  19445. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19446. XFREE(req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19447. der = NULL;
  19448. }
  19449. #endif /* WOLFSSL_CERT_REQ */
  19450. #endif /* WOLFSSL_CERT_GEN */
  19451. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  19452. /* Need to create known good signatures to test with this. */
  19453. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  19454. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  19455. ret = rsa_pss_test(&rng, key);
  19456. if (ret != 0)
  19457. goto exit_rsa;
  19458. #endif
  19459. #endif
  19460. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  19461. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19462. /* New key to be loaded in rsa_even_mod_test(). */
  19463. if (key != NULL)
  19464. #endif
  19465. wc_FreeRsaKey(key);
  19466. /* New key to be loaded in rsa_even_mod_test(). */
  19467. ret = rsa_even_mod_test(&rng, key);
  19468. #endif
  19469. exit_rsa:
  19470. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19471. if (key != NULL) {
  19472. wc_FreeRsaKey(key);
  19473. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19474. }
  19475. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  19476. if (keypub != NULL) {
  19477. wc_FreeRsaKey(keypub);
  19478. XFREE(keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19479. }
  19480. #endif
  19481. #ifdef WOLFSSL_TEST_CERT
  19482. if (cert != NULL)
  19483. XFREE(cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19484. #endif
  19485. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19486. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19487. #else
  19488. wc_FreeRsaKey(key);
  19489. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  19490. wc_FreeRsaKey(keypub);
  19491. #endif
  19492. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  19493. wc_FreeRng(&rng);
  19494. WC_FREE_VAR(in, HEAP_HINT);
  19495. WC_FREE_VAR(out, HEAP_HINT);
  19496. WC_FREE_VAR(plain, HEAP_HINT);
  19497. (void)res;
  19498. (void)bytes;
  19499. (void)idx;
  19500. (void)in;
  19501. (void)out;
  19502. (void)plain;
  19503. (void)idx;
  19504. (void)inStr;
  19505. (void)inLen;
  19506. (void)outSz;
  19507. (void)plainSz;
  19508. /* ret can be greater then 0 with certgen but all negative values should
  19509. * be returned and treated as an error */
  19510. if (ret >= 0) {
  19511. return 0;
  19512. }
  19513. else {
  19514. return ret;
  19515. }
  19516. }
  19517. #endif /* !NO_RSA */
  19518. #ifndef NO_DH
  19519. static wc_test_ret_t dh_fips_generate_test(WC_RNG *rng)
  19520. {
  19521. wc_test_ret_t ret = 0;
  19522. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19523. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19524. #else
  19525. DhKey key[1];
  19526. #endif
  19527. WOLFSSL_SMALL_STACK_STATIC const byte p[] = {
  19528. 0xc5, 0x7c, 0xa2, 0x4f, 0x4b, 0xd6, 0x8c, 0x3c,
  19529. 0xda, 0xc7, 0xba, 0xaa, 0xea, 0x2e, 0x5c, 0x1e,
  19530. 0x18, 0xb2, 0x7b, 0x8c, 0x55, 0x65, 0x9f, 0xea,
  19531. 0xe0, 0xa1, 0x36, 0x53, 0x2b, 0x36, 0xe0, 0x4e,
  19532. 0x3e, 0x64, 0xa9, 0xe4, 0xfc, 0x8f, 0x32, 0x62,
  19533. 0x97, 0xe4, 0xbe, 0xf7, 0xc1, 0xde, 0x07, 0x5a,
  19534. 0x89, 0x28, 0xf3, 0xfe, 0x4f, 0xfe, 0x68, 0xbc,
  19535. 0xfb, 0x0a, 0x7c, 0xa4, 0xb3, 0x14, 0x48, 0x89,
  19536. 0x9f, 0xaf, 0xb8, 0x43, 0xe2, 0xa0, 0x62, 0x5c,
  19537. 0xb4, 0x88, 0x3f, 0x06, 0x50, 0x11, 0xfe, 0x65,
  19538. 0x8d, 0x49, 0xd2, 0xf5, 0x4b, 0x74, 0x79, 0xdb,
  19539. 0x06, 0x62, 0x92, 0x89, 0xed, 0xda, 0xcb, 0x87,
  19540. 0x37, 0x16, 0xd2, 0xa1, 0x7a, 0xe8, 0xde, 0x92,
  19541. 0xee, 0x3e, 0x41, 0x4a, 0x91, 0x5e, 0xed, 0xf3,
  19542. 0x6c, 0x6b, 0x7e, 0xfd, 0x15, 0x92, 0x18, 0xfc,
  19543. 0xa7, 0xac, 0x42, 0x85, 0x57, 0xe9, 0xdc, 0xda,
  19544. 0x55, 0xc9, 0x8b, 0x28, 0x9e, 0xc1, 0xc4, 0x46,
  19545. 0x4d, 0x88, 0xed, 0x62, 0x8e, 0xdb, 0x3f, 0xb9,
  19546. 0xd7, 0xc8, 0xe3, 0xcf, 0xb8, 0x34, 0x2c, 0xd2,
  19547. 0x6f, 0x28, 0x06, 0x41, 0xe3, 0x66, 0x8c, 0xfc,
  19548. 0x72, 0xff, 0x26, 0x3b, 0x6b, 0x6c, 0x6f, 0x73,
  19549. 0xde, 0xf2, 0x90, 0x29, 0xe0, 0x61, 0x32, 0xc4,
  19550. 0x12, 0x74, 0x09, 0x52, 0xec, 0xf3, 0x1b, 0xa6,
  19551. 0x45, 0x98, 0xac, 0xf9, 0x1c, 0x65, 0x8e, 0x3a,
  19552. 0x91, 0x84, 0x4b, 0x23, 0x8a, 0xb2, 0x3c, 0xc9,
  19553. 0xfa, 0xea, 0xf1, 0x38, 0xce, 0xd8, 0x05, 0xe0,
  19554. 0xfa, 0x44, 0x68, 0x1f, 0xeb, 0xd9, 0x57, 0xb8,
  19555. 0x4a, 0x97, 0x5b, 0x88, 0xc5, 0xf1, 0xbb, 0xb0,
  19556. 0x49, 0xc3, 0x91, 0x7c, 0xd3, 0x13, 0xb9, 0x47,
  19557. 0xbb, 0x91, 0x8f, 0xe5, 0x26, 0x07, 0xab, 0xa9,
  19558. 0xc5, 0xd0, 0x3d, 0x95, 0x41, 0x26, 0x92, 0x9d,
  19559. 0x13, 0x67, 0xf2, 0x7e, 0x11, 0x88, 0xdc, 0x2d
  19560. };
  19561. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  19562. 0x4a, 0x1a, 0xf3, 0xa4, 0x92, 0xe9, 0xee, 0x74,
  19563. 0x6e, 0x57, 0xd5, 0x8c, 0x2c, 0x5b, 0x41, 0x41,
  19564. 0x5e, 0xd4, 0x55, 0x19, 0xdc, 0xd9, 0x32, 0x91,
  19565. 0xf7, 0xfd, 0xc2, 0x57, 0xff, 0x03, 0x14, 0xdb,
  19566. 0xf1, 0xb7, 0x60, 0x0c, 0x43, 0x59, 0x3f, 0xff,
  19567. 0xac, 0xf1, 0x80, 0x9a, 0x15, 0x6f, 0xd8, 0x6e,
  19568. 0xb7, 0x85, 0x18, 0xc8, 0xec, 0x4e, 0x59, 0x4a,
  19569. 0xe2, 0x91, 0x43, 0x4c, 0xeb, 0x95, 0xb6, 0x2e,
  19570. 0x9a, 0xea, 0x53, 0x68, 0x80, 0x64, 0x69, 0x40,
  19571. 0xf9, 0xec, 0xbd, 0x85, 0x89, 0x26, 0x97, 0x67,
  19572. 0xaf, 0xb0, 0xad, 0x00, 0x1b, 0xd4, 0xfd, 0x94,
  19573. 0xd3, 0xe9, 0x92, 0xb1, 0xb4, 0xbc, 0x5a, 0xaa,
  19574. 0x92, 0x80, 0x89, 0x3b, 0x39, 0x05, 0x6c, 0x22,
  19575. 0x26, 0xfe, 0x5a, 0x28, 0x6c, 0x37, 0x50, 0x5a,
  19576. 0x38, 0x99, 0xcf, 0xf3, 0xc1, 0x96, 0x45, 0xdc,
  19577. 0x01, 0xcb, 0x20, 0x87, 0xa5, 0x00, 0x8c, 0xf5,
  19578. 0x4d, 0xc2, 0xef, 0xb8, 0x9b, 0xd1, 0x87, 0xbe,
  19579. 0xed, 0xd5, 0x0a, 0x29, 0x15, 0x34, 0x59, 0x4c,
  19580. 0x3a, 0x05, 0x22, 0x05, 0x44, 0x4f, 0x9f, 0xc8,
  19581. 0x47, 0x12, 0x24, 0x8e, 0xa8, 0x79, 0xe4, 0x67,
  19582. 0xba, 0x4d, 0x5b, 0x75, 0x56, 0x95, 0xeb, 0xe8,
  19583. 0x8a, 0xfa, 0x8e, 0x01, 0x8c, 0x1b, 0x74, 0x63,
  19584. 0xd9, 0x2f, 0xf7, 0xd3, 0x44, 0x8f, 0xa8, 0xf5,
  19585. 0xaf, 0x6c, 0x4f, 0xdb, 0xe7, 0xc9, 0x6c, 0x71,
  19586. 0x22, 0xa3, 0x1d, 0xf1, 0x40, 0xb2, 0xe0, 0x9a,
  19587. 0xb6, 0x72, 0xc9, 0xc0, 0x13, 0x16, 0xa2, 0x4a,
  19588. 0xe1, 0x92, 0xc7, 0x54, 0x23, 0xab, 0x9d, 0xa1,
  19589. 0xa1, 0xe5, 0x0b, 0xed, 0xba, 0xe8, 0x84, 0x37,
  19590. 0xb2, 0xe7, 0xfe, 0x32, 0x8d, 0xfa, 0x1c, 0x53,
  19591. 0x77, 0x97, 0xc7, 0xf3, 0x48, 0xc9, 0xdb, 0x2d,
  19592. 0x75, 0x52, 0x9d, 0x42, 0x51, 0x78, 0x62, 0x68,
  19593. 0x05, 0x45, 0x15, 0xf8, 0xa2, 0x4e, 0xf3, 0x0b
  19594. };
  19595. WOLFSSL_SMALL_STACK_STATIC const byte q[] = {
  19596. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  19597. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  19598. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  19599. 0x40, 0x52, 0xed, 0x41
  19600. };
  19601. WOLFSSL_SMALL_STACK_STATIC const byte q0[] = {
  19602. 0x00,
  19603. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  19604. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  19605. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  19606. 0x40, 0x52, 0xed, 0x41
  19607. };
  19608. byte priv[256];
  19609. byte pub[256];
  19610. word32 privSz = sizeof(priv);
  19611. word32 pubSz = sizeof(pub);
  19612. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19613. if (key == NULL)
  19614. ERROR_OUT(MEMORY_E, exit_gen_test);
  19615. #endif
  19616. /* Parameter Validation testing. */
  19617. ret = wc_DhGenerateKeyPair(NULL, rng, priv, &privSz, pub, &pubSz);
  19618. if (ret != BAD_FUNC_ARG)
  19619. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19620. ret = wc_DhGenerateKeyPair(key, NULL, priv, &privSz, pub, &pubSz);
  19621. if (ret != BAD_FUNC_ARG)
  19622. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19623. ret = wc_DhGenerateKeyPair(key, rng, NULL, &privSz, pub, &pubSz);
  19624. if (ret != BAD_FUNC_ARG)
  19625. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19626. ret = wc_DhGenerateKeyPair(key, rng, priv, NULL, pub, &pubSz);
  19627. if (ret != BAD_FUNC_ARG)
  19628. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19629. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, NULL, &pubSz);
  19630. if (ret != BAD_FUNC_ARG)
  19631. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19632. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, NULL);
  19633. if (ret != BAD_FUNC_ARG)
  19634. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19635. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  19636. if (ret != 0)
  19637. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19638. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q0, sizeof(q0));
  19639. if (ret != 0)
  19640. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19641. wc_FreeDhKey(key);
  19642. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  19643. if (ret != 0)
  19644. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19645. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q, sizeof(q));
  19646. if (ret != 0)
  19647. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19648. /* Use API. */
  19649. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  19650. #if defined(WOLFSSL_ASYNC_CRYPT)
  19651. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  19652. #endif
  19653. if (ret != 0)
  19654. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19655. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q0, sizeof(q0));
  19656. if (ret != 0)
  19657. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19658. wc_FreeDhKey(key);
  19659. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  19660. if (ret != 0)
  19661. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19662. ret = wc_DhSetKey(key, p, sizeof(p), g, sizeof(g));
  19663. if (ret != 0)
  19664. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19665. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q, sizeof(q));
  19666. if (ret != 0)
  19667. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19668. #ifndef HAVE_SELFTEST
  19669. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  19670. if (ret != 0)
  19671. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19672. /* Taint the public key so the check fails. */
  19673. pub[0]++;
  19674. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  19675. if (ret != MP_CMP_E) {
  19676. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19677. }
  19678. #ifdef WOLFSSL_KEY_GEN
  19679. wc_FreeDhKey(key);
  19680. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  19681. if (ret != 0)
  19682. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19683. ret = wc_DhGenerateParams(rng, 2048, key);
  19684. if (ret != 0)
  19685. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19686. privSz = sizeof(priv);
  19687. pubSz = sizeof(pub);
  19688. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  19689. #if defined(WOLFSSL_ASYNC_CRYPT)
  19690. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  19691. #endif
  19692. if (ret != 0)
  19693. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19694. #endif /* WOLFSSL_KEY_GEN */
  19695. #endif /* HAVE_SELFTEST */
  19696. ret = 0;
  19697. exit_gen_test:
  19698. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19699. if (key) {
  19700. wc_FreeDhKey(key);
  19701. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19702. }
  19703. #else
  19704. wc_FreeDhKey(key);
  19705. #endif
  19706. return ret;
  19707. }
  19708. static wc_test_ret_t dh_generate_test(WC_RNG *rng)
  19709. {
  19710. wc_test_ret_t ret = 0;
  19711. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19712. DhKey *smallKey = NULL;
  19713. #else
  19714. DhKey smallKey[1];
  19715. #endif
  19716. byte p[2] = { 1, 7 }; /* 263 in decimal */
  19717. byte g[2] = { 0, 2 };
  19718. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  19719. #ifdef WOLFSSL_DH_CONST
  19720. /* the table for constant DH lookup will round to the lowest byte size 21 */
  19721. byte priv[21];
  19722. byte pub[21];
  19723. #else
  19724. byte priv[2];
  19725. byte pub[2];
  19726. #endif
  19727. word32 privSz = sizeof(priv);
  19728. word32 pubSz = sizeof(pub);
  19729. #endif
  19730. int smallKey_inited = 0;
  19731. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19732. if ((smallKey = (DhKey *)XMALLOC(sizeof(*smallKey), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  19733. return WC_TEST_RET_ENC_ERRNO;
  19734. #endif
  19735. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  19736. if (ret != 0)
  19737. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19738. smallKey_inited = 1;
  19739. /* Parameter Validation testing. */
  19740. ret = wc_InitDhKey_ex(NULL, HEAP_HINT, devId);
  19741. if (ret != BAD_FUNC_ARG)
  19742. return WC_TEST_RET_ENC_EC(ret);
  19743. wc_FreeDhKey(NULL);
  19744. ret = wc_DhSetKey(NULL, p, sizeof(p), g, 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, NULL, sizeof(p), g, sizeof(g));
  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, 0, g, sizeof(g));
  19753. if (ret != BAD_FUNC_ARG) {
  19754. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19755. }
  19756. ret = wc_DhSetKey(smallKey, p, sizeof(p), NULL, sizeof(g));
  19757. if (ret != BAD_FUNC_ARG) {
  19758. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19759. }
  19760. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, 0);
  19761. if (ret != BAD_FUNC_ARG) {
  19762. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19763. }
  19764. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, sizeof(g));
  19765. if (ret != 0)
  19766. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19767. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  19768. /* Use API. */
  19769. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  19770. #if defined(WOLFSSL_ASYNC_CRYPT)
  19771. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  19772. #endif
  19773. if (ret != 0) {
  19774. ret = WC_TEST_RET_ENC_EC(ret);
  19775. }
  19776. #else
  19777. (void)rng;
  19778. #if defined(HAVE_FIPS) || !defined(WOLFSSL_NO_DH186)
  19779. ret = 0;
  19780. #endif
  19781. #endif
  19782. #if !defined(HAVE_FIPS) && defined(WOLFSSL_NO_DH186)
  19783. {
  19784. byte priv[260];
  19785. byte pub[260];
  19786. word32 privSz = sizeof(priv);
  19787. word32 pubSz = sizeof(pub);
  19788. /* test odd ball param generation with DH */
  19789. wc_FreeDhKey(smallKey);
  19790. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  19791. if (ret != 0)
  19792. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19793. ret = wc_DhGenerateParams(rng, 2056, smallKey);
  19794. if (ret != 0)
  19795. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19796. privSz = sizeof(priv);
  19797. pubSz = sizeof(pub);
  19798. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  19799. #if defined(WOLFSSL_ASYNC_CRYPT)
  19800. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  19801. #endif
  19802. if (ret != 0)
  19803. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19804. }
  19805. #endif /* !HAVE_FIPS and WOLFSSL_NO_DH186 */
  19806. exit_gen_test:
  19807. if (smallKey_inited)
  19808. wc_FreeDhKey(smallKey);
  19809. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19810. if (smallKey != NULL)
  19811. XFREE(smallKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19812. #endif
  19813. return ret;
  19814. }
  19815. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  19816. typedef struct dh_pubvalue_test {
  19817. const byte* data;
  19818. word32 len;
  19819. } dh_pubvalue_test;
  19820. static wc_test_ret_t dh_test_check_pubvalue(void)
  19821. {
  19822. wc_test_ret_t ret;
  19823. word32 i;
  19824. WOLFSSL_SMALL_STACK_STATIC const byte prime[] = {0x01, 0x00, 0x01};
  19825. WOLFSSL_SMALL_STACK_STATIC const byte pubValZero[] = { 0x00 };
  19826. WOLFSSL_SMALL_STACK_STATIC const byte pubValZeroLong[] = { 0x00, 0x00, 0x00 };
  19827. WOLFSSL_SMALL_STACK_STATIC const byte pubValOne[] = { 0x01 };
  19828. WOLFSSL_SMALL_STACK_STATIC const byte pubValOneLong[] = { 0x00, 0x00, 0x01 };
  19829. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeMinusOne[] = { 0x01, 0x00, 0x00 };
  19830. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeLong[] = {0x00, 0x01, 0x00, 0x01};
  19831. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimePlusOne[] = { 0x01, 0x00, 0x02 };
  19832. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig0[] = { 0x02, 0x00, 0x01 };
  19833. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig1[] = { 0x01, 0x01, 0x01 };
  19834. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooLong[] = { 0x01, 0x00, 0x00, 0x01 };
  19835. const dh_pubvalue_test dh_pubval_fail[] = {
  19836. { prime, sizeof(prime) },
  19837. { pubValZero, sizeof(pubValZero) },
  19838. { pubValZeroLong, sizeof(pubValZeroLong) },
  19839. { pubValOne, sizeof(pubValOne) },
  19840. { pubValOneLong, sizeof(pubValOneLong) },
  19841. { pubValPrimeMinusOne, sizeof(pubValPrimeMinusOne) },
  19842. { pubValPrimeLong, sizeof(pubValPrimeLong) },
  19843. { pubValPrimePlusOne, sizeof(pubValPrimePlusOne) },
  19844. { pubValTooBig0, sizeof(pubValTooBig0) },
  19845. { pubValTooBig1, sizeof(pubValTooBig1) },
  19846. { pubValTooLong, sizeof(pubValTooLong) },
  19847. };
  19848. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwo[] = { 0x02 };
  19849. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwoLong[] = { 0x00, 0x00, 0x02 };
  19850. WOLFSSL_SMALL_STACK_STATIC const byte pubValGood[] = { 0x12, 0x34 };
  19851. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLen[] = { 0x00, 0x12, 0x34 };
  19852. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLong[] = { 0x00, 0x00, 0x12, 0x34 };
  19853. const dh_pubvalue_test dh_pubval_pass[] = {
  19854. { pubValTwo, sizeof(pubValTwo) },
  19855. { pubValTwoLong, sizeof(pubValTwoLong) },
  19856. { pubValGood, sizeof(pubValGood) },
  19857. { pubValGoodLen, sizeof(pubValGoodLen) },
  19858. { pubValGoodLong, sizeof(pubValGoodLong) },
  19859. };
  19860. for (i = 0; i < sizeof(dh_pubval_fail) / sizeof(*dh_pubval_fail); i++) {
  19861. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_fail[i].data,
  19862. dh_pubval_fail[i].len);
  19863. if (ret != MP_VAL)
  19864. return WC_TEST_RET_ENC_I(i);
  19865. }
  19866. for (i = 0; i < sizeof(dh_pubval_pass) / sizeof(*dh_pubval_pass); i++) {
  19867. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_pass[i].data,
  19868. dh_pubval_pass[i].len);
  19869. if (ret != 0)
  19870. return WC_TEST_RET_ENC_I(i);
  19871. }
  19872. return 0;
  19873. }
  19874. #endif
  19875. #if defined(HAVE_FFDHE)
  19876. #if defined(HAVE_FFDHE_4096)
  19877. #define MAX_DH_PRIV_SZ 39
  19878. #define MAX_DH_KEY_SZ 512
  19879. #elif defined(HAVE_FFDHE_3072)
  19880. #define MAX_DH_PRIV_SZ 34
  19881. #define MAX_DH_KEY_SZ 384
  19882. #else
  19883. #define MAX_DH_PRIV_SZ 29
  19884. #define MAX_DH_KEY_SZ 256
  19885. #endif
  19886. #ifndef WC_NO_RNG
  19887. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  19888. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  19889. #ifdef HAVE_PUBLIC_FFDHE
  19890. static wc_test_ret_t dh_ffdhe_test(WC_RNG *rng, const DhParams* params)
  19891. #else
  19892. static wc_test_ret_t dh_ffdhe_test(WC_RNG *rng, int name)
  19893. #endif
  19894. {
  19895. wc_test_ret_t ret;
  19896. word32 privSz, pubSz, privSz2, pubSz2;
  19897. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19898. byte *priv = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19899. byte *pub = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19900. byte *priv2 = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19901. byte *pub2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19902. byte *agree = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19903. byte *agree2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19904. DhKey *key = (DhKey*)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19905. DhKey *key2 = (DhKey*)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19906. #else
  19907. byte priv[MAX_DH_PRIV_SZ];
  19908. byte pub[MAX_DH_KEY_SZ];
  19909. byte priv2[MAX_DH_PRIV_SZ];
  19910. byte pub2[MAX_DH_KEY_SZ];
  19911. byte agree[MAX_DH_KEY_SZ];
  19912. byte agree2[MAX_DH_KEY_SZ];
  19913. DhKey key[1];
  19914. DhKey key2[1];
  19915. #endif
  19916. word32 agreeSz = MAX_DH_KEY_SZ;
  19917. word32 agreeSz2 = MAX_DH_KEY_SZ;
  19918. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19919. if ((priv == NULL) ||
  19920. (pub == NULL) ||
  19921. (priv2 == NULL) ||
  19922. (pub2 == NULL) ||
  19923. (agree == NULL) ||
  19924. (agree2 == NULL) ||
  19925. (key == NULL) ||
  19926. (key2 == NULL))
  19927. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  19928. #endif
  19929. pubSz = MAX_DH_KEY_SZ;
  19930. pubSz2 = MAX_DH_KEY_SZ;
  19931. #ifdef HAVE_PUBLIC_FFDHE
  19932. privSz = MAX_DH_PRIV_SZ;
  19933. privSz2 = MAX_DH_PRIV_SZ;
  19934. #else
  19935. privSz = wc_DhGetNamedKeyMinSize(name);
  19936. privSz2 = privSz;
  19937. #endif
  19938. XMEMSET(key, 0, sizeof(*key));
  19939. XMEMSET(key2, 0, sizeof(*key2));
  19940. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  19941. if (ret != 0)
  19942. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19943. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  19944. if (ret != 0)
  19945. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19946. #ifdef HAVE_PUBLIC_FFDHE
  19947. ret = wc_DhSetKey(key, params->p, params->p_len, params->g, params->g_len);
  19948. #else
  19949. ret = wc_DhSetNamedKey(key, name);
  19950. #endif
  19951. if (ret != 0)
  19952. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19953. #ifdef HAVE_PUBLIC_FFDHE
  19954. ret = wc_DhSetKey(key2, params->p, params->p_len, params->g,
  19955. params->g_len);
  19956. #else
  19957. ret = wc_DhSetNamedKey(key2, name);
  19958. #endif
  19959. if (ret != 0)
  19960. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19961. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  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_DhGenerateKeyPair(key2, rng, priv2, &privSz2, pub2, &pubSz2);
  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. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  19974. #if defined(WOLFSSL_ASYNC_CRYPT)
  19975. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  19976. #endif
  19977. if (ret != 0)
  19978. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19979. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  19980. #if defined(WOLFSSL_ASYNC_CRYPT)
  19981. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  19982. #endif
  19983. if (ret != 0)
  19984. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19985. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  19986. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  19987. }
  19988. #if (defined(WOLFSSL_HAVE_SP_DH) || defined(USE_FAST_MATH)) && \
  19989. !defined(HAVE_INTEL_QA)
  19990. /* Make p even */
  19991. key->p.dp[0] &= (mp_digit)-2;
  19992. if (ret != 0)
  19993. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19994. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  19995. #if defined(WOLFSSL_ASYNC_CRYPT)
  19996. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  19997. #endif
  19998. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  19999. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20000. }
  20001. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  20002. #if defined(WOLFSSL_ASYNC_CRYPT)
  20003. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  20004. #endif
  20005. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != ASYNC_OP_E) {
  20006. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20007. }
  20008. #ifndef HAVE_SELFTEST
  20009. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  20010. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_CMP_E &&
  20011. ret != ASYNC_OP_E) {
  20012. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20013. }
  20014. #endif
  20015. /* Getting here means success - set ret to 0. */
  20016. ret = 0;
  20017. #endif /* (SP DH or Fast Math) and not Intel QuickAssist */
  20018. done:
  20019. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) && \
  20020. !defined(WC_NO_RNG)
  20021. if (priv)
  20022. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20023. if (pub)
  20024. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20025. if (priv2)
  20026. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20027. if (pub2)
  20028. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20029. if (agree)
  20030. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20031. if (agree2)
  20032. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20033. if (key) {
  20034. wc_FreeDhKey(key);
  20035. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20036. }
  20037. if (key2) {
  20038. wc_FreeDhKey(key2);
  20039. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20040. }
  20041. #else
  20042. wc_FreeDhKey(key);
  20043. wc_FreeDhKey(key2);
  20044. #endif
  20045. return ret;
  20046. }
  20047. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  20048. #endif /* !WC_NO_RNG */
  20049. #endif /* HAVE_FFDHE */
  20050. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dh_test(void)
  20051. {
  20052. wc_test_ret_t ret;
  20053. word32 bytes;
  20054. word32 idx = 0, privSz, pubSz, privSz2, pubSz2;
  20055. #ifndef WC_NO_RNG
  20056. WC_RNG rng;
  20057. int rngInit = 0;
  20058. #endif
  20059. int keyInit = 0, key2Init = 0;
  20060. #define DH_TEST_TMP_SIZE 1024
  20061. #if !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  20062. #define DH_TEST_BUF_SIZE 256
  20063. #else
  20064. #define DH_TEST_BUF_SIZE 512
  20065. #endif
  20066. #ifndef WC_NO_RNG
  20067. word32 agreeSz = DH_TEST_BUF_SIZE;
  20068. word32 agreeSz2 = DH_TEST_BUF_SIZE;
  20069. #endif
  20070. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20071. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20072. DhKey *key2 = (DhKey *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20073. byte *tmp = (byte *)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20074. #else
  20075. DhKey key[1];
  20076. DhKey key2[1];
  20077. byte tmp[DH_TEST_TMP_SIZE];
  20078. #endif
  20079. #ifndef WC_NO_RNG
  20080. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20081. byte *priv = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20082. byte *pub = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20083. byte *priv2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20084. byte *pub2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20085. byte *agree = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20086. byte *agree2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20087. if (priv == NULL || pub == NULL || priv2 == NULL || pub2 == NULL ||
  20088. agree == NULL || agree2 == NULL) {
  20089. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20090. }
  20091. #else
  20092. byte priv[DH_TEST_BUF_SIZE];
  20093. byte pub[DH_TEST_BUF_SIZE];
  20094. byte priv2[DH_TEST_BUF_SIZE];
  20095. byte pub2[DH_TEST_BUF_SIZE];
  20096. byte agree[DH_TEST_BUF_SIZE];
  20097. byte agree2[DH_TEST_BUF_SIZE];
  20098. #endif
  20099. #endif /* !WC_NO_RNG */
  20100. WOLFSSL_ENTER("dh_test");
  20101. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20102. if (key == NULL || key2 == NULL || tmp == NULL) {
  20103. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20104. }
  20105. #endif
  20106. #ifdef USE_CERT_BUFFERS_1024
  20107. XMEMCPY(tmp, dh_key_der_1024, (size_t)sizeof_dh_key_der_1024);
  20108. bytes = (size_t)sizeof_dh_key_der_1024;
  20109. #elif defined(USE_CERT_BUFFERS_2048)
  20110. XMEMCPY(tmp, dh_key_der_2048, (size_t)sizeof_dh_key_der_2048);
  20111. bytes = (size_t)sizeof_dh_key_der_2048;
  20112. #elif defined(USE_CERT_BUFFERS_3072)
  20113. XMEMCPY(tmp, dh_key_der_3072, (size_t)sizeof_dh_key_der_3072);
  20114. bytes = (size_t)sizeof_dh_key_der_3072;
  20115. #elif defined(USE_CERT_BUFFERS_4096)
  20116. XMEMCPY(tmp, dh_key_der_4096, (size_t)sizeof_dh_key_der_4096);
  20117. bytes = (size_t)sizeof_dh_key_der_4096;
  20118. #elif defined(NO_ASN)
  20119. /* don't use file, no DER parsing */
  20120. #elif !defined(NO_FILESYSTEM)
  20121. {
  20122. XFILE file = XFOPEN(dhParamsFile, "rb");
  20123. if (! file)
  20124. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20125. bytes = (word32) XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  20126. XFCLOSE(file);
  20127. if (bytes == 0)
  20128. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20129. }
  20130. #else
  20131. /* No DH key to use. */
  20132. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  20133. #endif /* USE_CERT_BUFFERS */
  20134. (void)idx;
  20135. (void)tmp;
  20136. (void)bytes;
  20137. pubSz = DH_TEST_BUF_SIZE;
  20138. pubSz2 = DH_TEST_BUF_SIZE;
  20139. privSz = DH_TEST_BUF_SIZE;
  20140. privSz2 = DH_TEST_BUF_SIZE;
  20141. #ifndef WC_NO_RNG
  20142. XMEMSET(&rng, 0, sizeof(rng));
  20143. #endif
  20144. /* Use API for coverage. */
  20145. ret = wc_InitDhKey(key);
  20146. if (ret != 0)
  20147. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20148. wc_FreeDhKey(key);
  20149. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  20150. if (ret != 0)
  20151. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20152. keyInit = 1;
  20153. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  20154. if (ret != 0)
  20155. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20156. key2Init = 1;
  20157. #ifdef NO_ASN
  20158. #ifndef WOLFSSL_SP_MATH
  20159. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  20160. if (ret != 0)
  20161. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20162. ret = wc_DhSetKey(key2, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  20163. if (ret != 0)
  20164. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20165. #else
  20166. ret = wc_DhSetKey(key, dh2048_p, sizeof(dh2048_p), dh2048_g,
  20167. sizeof(dh2048_g));
  20168. if (ret != 0)
  20169. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20170. ret = wc_DhSetKey(key2, dh2048_p, sizeof(dh2048_p), dh2048_g,
  20171. sizeof(dh2048_g));
  20172. if (ret != 0)
  20173. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20174. #endif
  20175. #else
  20176. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  20177. if (ret != 0)
  20178. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20179. idx = 0;
  20180. ret = wc_DhKeyDecode(tmp, &idx, key2, bytes);
  20181. if (ret != 0)
  20182. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20183. #endif
  20184. #ifndef WC_NO_RNG
  20185. #ifndef HAVE_FIPS
  20186. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  20187. #else
  20188. ret = wc_InitRng(&rng);
  20189. #endif
  20190. if (ret != 0)
  20191. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20192. rngInit = 1;
  20193. ret = wc_DhGenerateKeyPair(key, &rng, priv, &privSz, pub, &pubSz);
  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_DhGenerateKeyPair(key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  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. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  20206. #if defined(WOLFSSL_ASYNC_CRYPT)
  20207. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  20208. #endif
  20209. if (ret != 0)
  20210. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20211. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  20212. #if defined(WOLFSSL_ASYNC_CRYPT)
  20213. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  20214. #endif
  20215. if (ret != 0)
  20216. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20217. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  20218. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  20219. }
  20220. #endif /* !WC_NO_RNG */
  20221. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  20222. ret = wc_DhCheckPrivKey(NULL, NULL, 0);
  20223. if (ret != BAD_FUNC_ARG)
  20224. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20225. ret = wc_DhCheckPrivKey(key, priv, privSz);
  20226. if (ret != 0)
  20227. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20228. ret = wc_DhExportParamsRaw(NULL, NULL, NULL, NULL, NULL, NULL, NULL);
  20229. if (ret != BAD_FUNC_ARG)
  20230. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20231. {
  20232. word32 pSz, qSz, gSz;
  20233. ret = wc_DhExportParamsRaw(key, NULL, &pSz, NULL, &qSz, NULL, &gSz);
  20234. if (ret != LENGTH_ONLY_E)
  20235. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20236. }
  20237. #endif
  20238. /* Test DH key import / export */
  20239. #if defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM) && \
  20240. (!defined(HAVE_FIPS) || \
  20241. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  20242. wc_FreeDhKey(key);
  20243. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  20244. if (ret != 0)
  20245. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20246. #ifndef NO_ASN
  20247. {
  20248. /* DH Private - Key Export / Import */
  20249. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20250. byte *tmp2;
  20251. #else
  20252. byte tmp2[DH_TEST_TMP_SIZE];
  20253. #endif
  20254. #if defined(USE_CERT_BUFFERS_2048)
  20255. XMEMCPY(tmp, dh_ffdhe_statickey_der_2048, sizeof_dh_ffdhe_statickey_der_2048);
  20256. bytes = sizeof_dh_ffdhe_statickey_der_2048;
  20257. #else
  20258. XFILE file = XFOPEN(dhKeyFile, "rb");
  20259. if (!file)
  20260. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20261. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  20262. XFCLOSE(file);
  20263. if (bytes == 0)
  20264. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20265. #endif
  20266. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20267. tmp2 = (byte*)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20268. if (tmp2 == NULL)
  20269. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20270. #endif
  20271. idx = 0;
  20272. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  20273. /* Import DH Private key as DER */
  20274. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  20275. if (ret == 0) {
  20276. /* Export as DER */
  20277. idx = DH_TEST_TMP_SIZE;
  20278. ret = wc_DhPrivKeyToDer(key, tmp2, &idx);
  20279. }
  20280. /* Verify export matches original */
  20281. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  20282. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  20283. }
  20284. /* DH Public Key - Export / Import */
  20285. #if defined(USE_CERT_BUFFERS_2048)
  20286. XMEMCPY(tmp, dh_ffdhe_pub_statickey_der_2048, sizeof_dh_ffdhe_pub_statickey_der_2048);
  20287. bytes = sizeof_dh_ffdhe_pub_statickey_der_2048;
  20288. #else
  20289. file = XFOPEN(dhKeyPubFile, "rb");
  20290. if (!file)
  20291. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20292. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  20293. XFCLOSE(file);
  20294. if (bytes == 0)
  20295. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20296. #endif
  20297. /* for HAVE_WOLF_BIGINT prevent leak */
  20298. wc_FreeDhKey(key);
  20299. (void)wc_InitDhKey_ex(key, HEAP_HINT, devId);
  20300. idx = 0;
  20301. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  20302. /* Import DH Public key as DER */
  20303. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  20304. if (ret == 0) {
  20305. /* Export as DER */
  20306. idx = DH_TEST_TMP_SIZE;
  20307. ret = wc_DhPubKeyToDer(key, tmp2, &idx);
  20308. }
  20309. /* Verify export matches original */
  20310. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  20311. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  20312. }
  20313. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20314. XFREE(tmp2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20315. #endif
  20316. }
  20317. #else
  20318. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  20319. if (ret != 0)
  20320. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20321. #endif /* !NO_ASN */
  20322. privSz = DH_TEST_BUF_SIZE;
  20323. pubSz = DH_TEST_BUF_SIZE;
  20324. ret = wc_DhExportKeyPair(key, priv, &privSz, pub, &pubSz);
  20325. if (ret != 0)
  20326. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20327. ret = wc_DhImportKeyPair(key2, priv, privSz, pub, pubSz);
  20328. if (ret != 0)
  20329. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20330. #endif /* WOLFSSL_DH_EXTRA && !NO_FILESYSTEM && !FIPS <= 2 */
  20331. #ifndef WC_NO_RNG
  20332. ret = dh_generate_test(&rng);
  20333. if (ret != 0)
  20334. ERROR_OUT(ret, done);
  20335. ret = dh_fips_generate_test(&rng);
  20336. if (ret != 0)
  20337. ERROR_OUT(ret, done);
  20338. #endif /* !WC_NO_RNG */
  20339. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  20340. ret = dh_test_check_pubvalue();
  20341. if (ret != 0)
  20342. ERROR_OUT(ret, done);
  20343. #endif
  20344. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  20345. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  20346. /* RNG with DH and SP_ASM code not supported in the in-lined FIPS ASM code,
  20347. * this will be available for testing in the 140-3 module */
  20348. #ifndef WC_NO_RNG
  20349. /* Specialized code for key gen when using FFDHE-2048, FFDHE-3072 and FFDHE-4096 */
  20350. #ifdef HAVE_FFDHE_2048
  20351. #ifdef HAVE_PUBLIC_FFDHE
  20352. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe2048_Get());
  20353. #else
  20354. ret = dh_ffdhe_test(&rng, WC_FFDHE_2048);
  20355. #endif
  20356. if (ret != 0)
  20357. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20358. #endif
  20359. #ifdef HAVE_FFDHE_3072
  20360. #ifdef HAVE_PUBLIC_FFDHE
  20361. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe3072_Get());
  20362. #else
  20363. ret = dh_ffdhe_test(&rng, WC_FFDHE_3072);
  20364. #endif
  20365. if (ret != 0)
  20366. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20367. #endif
  20368. #ifdef HAVE_FFDHE_4096
  20369. #ifdef HAVE_PUBLIC_FFDHE
  20370. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe4096_Get());
  20371. #else
  20372. ret = dh_ffdhe_test(&rng, WC_FFDHE_4096);
  20373. #endif
  20374. if (ret != 0)
  20375. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20376. #endif
  20377. #endif /* !WC_NO_RNG */
  20378. #endif /* HAVE_FIPS_VERSION == 2 && !WOLFSSL_SP_ARM64_ASM */
  20379. wc_FreeDhKey(key);
  20380. keyInit = 0;
  20381. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  20382. !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(WC_NO_RNG)
  20383. /* Test Check Key */
  20384. ret = wc_DhSetCheckKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g),
  20385. NULL, 0, 0, &rng);
  20386. if (ret != 0)
  20387. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20388. keyInit = 1; /* DhSetCheckKey also initializes the key, free it */
  20389. #endif
  20390. done:
  20391. #ifndef WC_NO_RNG
  20392. if (rngInit)
  20393. wc_FreeRng(&rng);
  20394. #endif
  20395. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20396. if (key) {
  20397. if (keyInit)
  20398. wc_FreeDhKey(key);
  20399. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20400. }
  20401. if (key2) {
  20402. if (key2Init)
  20403. wc_FreeDhKey(key2);
  20404. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20405. }
  20406. if (tmp)
  20407. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20408. if (priv)
  20409. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20410. if (pub)
  20411. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20412. if (priv2)
  20413. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20414. if (pub2)
  20415. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20416. if (agree)
  20417. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20418. if (agree2)
  20419. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20420. #else
  20421. if (keyInit)
  20422. wc_FreeDhKey(key);
  20423. if (key2Init)
  20424. wc_FreeDhKey(key2);
  20425. #endif
  20426. (void)privSz;
  20427. (void)pubSz;
  20428. (void)pubSz2;
  20429. (void)privSz2;
  20430. return ret;
  20431. #undef DH_TEST_BUF_SIZE
  20432. #undef DH_TEST_TMP_SIZE
  20433. }
  20434. #endif /* NO_DH */
  20435. #ifndef NO_DSA
  20436. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dsa_test(void)
  20437. {
  20438. wc_test_ret_t ret = 0;
  20439. int answer;
  20440. word32 bytes;
  20441. word32 idx = 0;
  20442. WC_RNG rng;
  20443. int rng_inited = 0;
  20444. wc_Sha sha;
  20445. byte hash[WC_SHA_DIGEST_SIZE];
  20446. byte signature[40];
  20447. int key_inited = 0;
  20448. #ifdef WOLFSSL_KEY_GEN
  20449. byte* der = 0;
  20450. int derIn_inited = 0;
  20451. int genKey_inited = 0;
  20452. #endif
  20453. #define DSA_TEST_TMP_SIZE 1024
  20454. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20455. byte *tmp = (byte *)XMALLOC(DSA_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20456. DsaKey *key = (DsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20457. #ifdef WOLFSSL_KEY_GEN
  20458. DsaKey *derIn = (DsaKey *)XMALLOC(sizeof *derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20459. DsaKey *genKey = (DsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20460. #endif
  20461. WOLFSSL_ENTER("dsa_test");
  20462. if ((tmp == NULL) ||
  20463. (key == NULL)
  20464. #ifdef WOLFSSL_KEY_GEN
  20465. || (derIn == NULL)
  20466. || (genKey == NULL)
  20467. #endif
  20468. ) {
  20469. ret = WC_TEST_RET_ENC_NC;
  20470. goto out;
  20471. }
  20472. #else
  20473. byte tmp[1024];
  20474. DsaKey key[1];
  20475. #ifdef WOLFSSL_KEY_GEN
  20476. DsaKey derIn[1];
  20477. DsaKey genKey[1];
  20478. #endif
  20479. #endif
  20480. #ifdef USE_CERT_BUFFERS_1024
  20481. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  20482. bytes = sizeof_dsa_key_der_1024;
  20483. #elif defined(USE_CERT_BUFFERS_2048)
  20484. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  20485. bytes = sizeof_dsa_key_der_2048;
  20486. #else
  20487. {
  20488. XFILE file = XFOPEN(dsaKey, "rb");
  20489. if (!file)
  20490. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  20491. bytes = (word32) XFREAD(tmp, 1, DSA_TEST_TMP_SIZE, file);
  20492. XFCLOSE(file);
  20493. if (bytes == 0)
  20494. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  20495. }
  20496. #endif /* USE_CERT_BUFFERS */
  20497. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  20498. if (ret != 0)
  20499. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20500. wc_ShaUpdate(&sha, tmp, bytes);
  20501. wc_ShaFinal(&sha, hash);
  20502. wc_ShaFree(&sha);
  20503. ret = wc_InitDsaKey(key);
  20504. if (ret != 0)
  20505. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20506. key_inited = 1;
  20507. ret = wc_DsaPrivateKeyDecode(tmp, &idx, key, bytes);
  20508. if (ret != 0)
  20509. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20510. #ifndef HAVE_FIPS
  20511. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  20512. #else
  20513. ret = wc_InitRng(&rng);
  20514. #endif
  20515. if (ret != 0)
  20516. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20517. rng_inited = 1;
  20518. ret = wc_DsaSign(hash, signature, key, &rng);
  20519. if (ret != 0)
  20520. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20521. ret = wc_DsaVerify(hash, signature, key, &answer);
  20522. if (ret != 0)
  20523. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20524. if (answer != 1)
  20525. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  20526. wc_FreeDsaKey(key);
  20527. key_inited = 0;
  20528. ret = wc_InitDsaKey_h(key, NULL);
  20529. if (ret != 0)
  20530. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20531. key_inited = 1;
  20532. #ifdef WOLFSSL_KEY_GEN
  20533. {
  20534. int derSz = 0;
  20535. ret = wc_InitDsaKey(genKey);
  20536. if (ret != 0)
  20537. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20538. genKey_inited = 1;
  20539. ret = wc_MakeDsaParameters(&rng, 1024, genKey);
  20540. if (ret != 0)
  20541. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20542. ret = wc_MakeDsaKey(&rng, genKey);
  20543. if (ret != 0)
  20544. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20545. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20546. if (der == NULL)
  20547. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  20548. derSz = wc_DsaKeyToDer(genKey, der, FOURK_BUF);
  20549. if (derSz < 0)
  20550. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), out);
  20551. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  20552. DSA_PRIVATEKEY_TYPE);
  20553. if (ret != 0)
  20554. goto out;
  20555. ret = wc_InitDsaKey(derIn);
  20556. if (ret != 0)
  20557. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20558. derIn_inited = 1;
  20559. idx = 0;
  20560. ret = wc_DsaPrivateKeyDecode(der, &idx, derIn, (word32)derSz);
  20561. if (ret != 0)
  20562. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20563. }
  20564. #endif /* WOLFSSL_KEY_GEN */
  20565. out:
  20566. #ifdef WOLFSSL_KEY_GEN
  20567. if (der)
  20568. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20569. #endif
  20570. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20571. if (tmp)
  20572. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20573. if (key) {
  20574. if (key_inited)
  20575. wc_FreeDsaKey(key);
  20576. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20577. }
  20578. #ifdef WOLFSSL_KEY_GEN
  20579. if (derIn) {
  20580. if (derIn_inited)
  20581. wc_FreeDsaKey(derIn);
  20582. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20583. }
  20584. if (genKey) {
  20585. if (genKey_inited)
  20586. wc_FreeDsaKey(genKey);
  20587. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20588. }
  20589. #endif
  20590. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  20591. if (key_inited)
  20592. wc_FreeDsaKey(key);
  20593. #ifdef WOLFSSL_KEY_GEN
  20594. if (derIn_inited)
  20595. wc_FreeDsaKey(derIn);
  20596. if (genKey_inited)
  20597. wc_FreeDsaKey(genKey);
  20598. #endif
  20599. #endif
  20600. if (rng_inited)
  20601. wc_FreeRng(&rng);
  20602. return ret;
  20603. }
  20604. #endif /* NO_DSA */
  20605. #ifdef WOLFCRYPT_HAVE_SRP
  20606. static wc_test_ret_t generate_random_salt(byte *buf, word32 size)
  20607. {
  20608. wc_test_ret_t ret = WC_TEST_RET_ENC_NC;
  20609. WC_RNG rng;
  20610. if(NULL == buf || !size)
  20611. return WC_TEST_RET_ENC_NC;
  20612. if (buf && size && wc_InitRng_ex(&rng, HEAP_HINT, devId) == 0) {
  20613. ret = wc_RNG_GenerateBlock(&rng, (byte *)buf, size);
  20614. wc_FreeRng(&rng);
  20615. }
  20616. return ret;
  20617. }
  20618. static wc_test_ret_t srp_test_digest(SrpType dgstType)
  20619. {
  20620. wc_test_ret_t r;
  20621. byte clientPubKey[192]; /* A */
  20622. byte serverPubKey[192]; /* B */
  20623. word32 clientPubKeySz = 192;
  20624. word32 serverPubKeySz = 192;
  20625. byte username[] = "user";
  20626. word32 usernameSz = 4;
  20627. byte password[] = "password";
  20628. word32 passwordSz = 8;
  20629. WOLFSSL_SMALL_STACK_STATIC const byte N[] = {
  20630. 0xfc, 0x58, 0x7a, 0x8a, 0x70, 0xfb, 0x5a, 0x9a,
  20631. 0x5d, 0x39, 0x48, 0xbf, 0x1c, 0x46, 0xd8, 0x3b,
  20632. 0x7a, 0xe9, 0x1f, 0x85, 0x36, 0x18, 0xc4, 0x35,
  20633. 0x3f, 0xf8, 0x8a, 0x8f, 0x8c, 0x10, 0x2e, 0x01,
  20634. 0x58, 0x1d, 0x41, 0xcb, 0xc4, 0x47, 0xa8, 0xaf,
  20635. 0x9a, 0x6f, 0x58, 0x14, 0xa4, 0x68, 0xf0, 0x9c,
  20636. 0xa6, 0xe7, 0xbf, 0x0d, 0xe9, 0x62, 0x0b, 0xd7,
  20637. 0x26, 0x46, 0x5b, 0x27, 0xcb, 0x4c, 0xf9, 0x7e,
  20638. 0x1e, 0x8b, 0xe6, 0xdd, 0x29, 0xb7, 0xb7, 0x15,
  20639. 0x2e, 0xcf, 0x23, 0xa6, 0x4b, 0x97, 0x9f, 0x89,
  20640. 0xd4, 0x86, 0xc4, 0x90, 0x63, 0x92, 0xf4, 0x30,
  20641. 0x26, 0x69, 0x48, 0x9d, 0x7a, 0x4f, 0xad, 0xb5,
  20642. 0x6a, 0x51, 0xad, 0xeb, 0xf9, 0x90, 0x31, 0x77,
  20643. 0x53, 0x30, 0x2a, 0x85, 0xf7, 0x11, 0x21, 0x0c,
  20644. 0xb8, 0x4b, 0x56, 0x03, 0x5e, 0xbb, 0x25, 0x33,
  20645. 0x7c, 0xd9, 0x5a, 0xd1, 0x5c, 0xb2, 0xd4, 0x53,
  20646. 0xc5, 0x16, 0x68, 0xf0, 0xdf, 0x48, 0x55, 0x3e,
  20647. 0xd4, 0x59, 0x87, 0x64, 0x59, 0xaa, 0x39, 0x01,
  20648. 0x45, 0x89, 0x9c, 0x72, 0xff, 0xdd, 0x8f, 0x6d,
  20649. 0xa0, 0x42, 0xbc, 0x6f, 0x6e, 0x62, 0x18, 0x2d,
  20650. 0x50, 0xe8, 0x18, 0x97, 0x87, 0xfc, 0xef, 0x1f,
  20651. 0xf5, 0x53, 0x68, 0xe8, 0x49, 0xd1, 0xa2, 0xe8,
  20652. 0xb9, 0x26, 0x03, 0xba, 0xb5, 0x58, 0x6f, 0x6c,
  20653. 0x8b, 0x08, 0xa1, 0x7b, 0x6f, 0x42, 0xc9, 0x53
  20654. };
  20655. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  20656. 0x02
  20657. };
  20658. byte salt[10];
  20659. byte verifier[192];
  20660. word32 v_size = sizeof(verifier);
  20661. word32 clientProofSz = SRP_MAX_DIGEST_SIZE;
  20662. word32 serverProofSz = SRP_MAX_DIGEST_SIZE;
  20663. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20664. Srp *cli = (Srp *)XMALLOC(sizeof *cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20665. Srp *srv = (Srp *)XMALLOC(sizeof *srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20666. byte *clientProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  20667. DYNAMIC_TYPE_TMP_BUFFER); /* M1 */
  20668. byte *serverProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  20669. DYNAMIC_TYPE_TMP_BUFFER); /* M2 */
  20670. if ((cli == NULL) ||
  20671. (srv == NULL) ||
  20672. (clientProof == NULL) ||
  20673. (serverProof == NULL)) {
  20674. r = WC_TEST_RET_ENC_NC;
  20675. goto out;
  20676. }
  20677. #else
  20678. Srp cli[1], srv[1];
  20679. byte clientProof[SRP_MAX_DIGEST_SIZE]; /* M1 */
  20680. byte serverProof[SRP_MAX_DIGEST_SIZE]; /* M2 */
  20681. #endif
  20682. /* set as 0's so if second init on srv not called SrpTerm is not on
  20683. * garbage values */
  20684. XMEMSET(srv, 0, sizeof *srv);
  20685. XMEMSET(cli, 0, sizeof *cli);
  20686. /* generating random salt */
  20687. r = generate_random_salt(salt, sizeof(salt));
  20688. /* client knows username and password. */
  20689. /* server knows N, g, salt and verifier. */
  20690. if (!r) r = wc_SrpInit_ex(cli, dgstType, SRP_CLIENT_SIDE, HEAP_HINT, devId);
  20691. if (!r) r = wc_SrpSetUsername(cli, username, usernameSz);
  20692. /* loading N, g and salt in advance to generate the verifier. */
  20693. if (!r) r = wc_SrpSetParams(cli, N, sizeof(N),
  20694. g, sizeof(g),
  20695. salt, sizeof(salt));
  20696. if (!r) r = wc_SrpSetPassword(cli, password, passwordSz);
  20697. if (!r) r = wc_SrpGetVerifier(cli, verifier, &v_size);
  20698. /* client sends username to server */
  20699. if (!r) r = wc_SrpInit_ex(srv, dgstType, SRP_SERVER_SIDE, HEAP_HINT, devId);
  20700. if (!r) r = wc_SrpSetUsername(srv, username, usernameSz);
  20701. if (!r) r = wc_SrpSetParams(srv, N, sizeof(N),
  20702. g, sizeof(g),
  20703. salt, sizeof(salt));
  20704. if (!r) r = wc_SrpSetVerifier(srv, verifier, v_size);
  20705. if (!r) r = wc_SrpGetPublic(srv, serverPubKey, &serverPubKeySz);
  20706. /* server sends N, g, salt and B to client */
  20707. if (!r) r = wc_SrpGetPublic(cli, clientPubKey, &clientPubKeySz);
  20708. if (!r) r = wc_SrpComputeKey(cli, clientPubKey, clientPubKeySz,
  20709. serverPubKey, serverPubKeySz);
  20710. if (!r) r = wc_SrpGetProof(cli, clientProof, &clientProofSz);
  20711. /* client sends A and M1 to server */
  20712. if (!r) r = wc_SrpComputeKey(srv, clientPubKey, clientPubKeySz,
  20713. serverPubKey, serverPubKeySz);
  20714. if (!r) r = wc_SrpVerifyPeersProof(srv, clientProof, clientProofSz);
  20715. if (!r) r = wc_SrpGetProof(srv, serverProof, &serverProofSz);
  20716. /* server sends M2 to client */
  20717. if (!r) r = wc_SrpVerifyPeersProof(cli, serverProof, serverProofSz);
  20718. wc_SrpTerm(cli);
  20719. wc_SrpTerm(srv);
  20720. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20721. out:
  20722. if (cli)
  20723. XFREE(cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20724. if (srv)
  20725. XFREE(srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20726. if (clientProof)
  20727. XFREE(clientProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20728. if (serverProof)
  20729. XFREE(serverProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20730. #endif
  20731. return r;
  20732. }
  20733. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srp_test(void)
  20734. {
  20735. wc_test_ret_t ret;
  20736. WOLFSSL_ENTER("srp_test");
  20737. #ifndef NO_SHA
  20738. ret = srp_test_digest(SRP_TYPE_SHA);
  20739. if (ret != 0)
  20740. return ret;
  20741. #endif
  20742. #ifndef NO_SHA256
  20743. ret = srp_test_digest(SRP_TYPE_SHA256);
  20744. if (ret != 0)
  20745. return ret;
  20746. #endif
  20747. #ifdef WOLFSSL_SHA384
  20748. ret = srp_test_digest(SRP_TYPE_SHA384);
  20749. if (ret != 0)
  20750. return ret;
  20751. #endif
  20752. #ifdef WOLFSSL_SHA512
  20753. ret = srp_test_digest(SRP_TYPE_SHA512);
  20754. if (ret != 0)
  20755. return ret;
  20756. #endif
  20757. return ret;
  20758. }
  20759. #endif /* WOLFCRYPT_HAVE_SRP */
  20760. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  20761. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  20762. static wc_test_ret_t openssl_aes_test(void)
  20763. {
  20764. #ifdef HAVE_AES_CBC
  20765. #ifdef WOLFSSL_AES_128
  20766. {
  20767. /* EVP_CipherUpdate test */
  20768. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  20769. {
  20770. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  20771. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  20772. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  20773. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  20774. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  20775. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  20776. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  20777. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  20778. };
  20779. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  20780. "0123456789abcdef "; /* align */
  20781. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  20782. "1234567890abcdef "; /* align */
  20783. byte cipher[AES_BLOCK_SIZE * 4];
  20784. byte plain [AES_BLOCK_SIZE * 4];
  20785. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20786. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  20787. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  20788. #else
  20789. EVP_CIPHER_CTX en[1];
  20790. EVP_CIPHER_CTX de[1];
  20791. #endif
  20792. int outlen ;
  20793. int total = 0;
  20794. int i;
  20795. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20796. if ((en == NULL) || (de == NULL))
  20797. return MEMORY_E;
  20798. #endif
  20799. EVP_CIPHER_CTX_init(en);
  20800. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  20801. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  20802. return WC_TEST_RET_ENC_NC;
  20803. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  20804. (byte*)cbcPlain, 9) == 0)
  20805. return WC_TEST_RET_ENC_NC;
  20806. if (outlen != 0)
  20807. return WC_TEST_RET_ENC_NC;
  20808. total += outlen;
  20809. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  20810. (byte*)&cbcPlain[9] , 9) == 0)
  20811. return WC_TEST_RET_ENC_NC;
  20812. if (outlen != 16)
  20813. return WC_TEST_RET_ENC_NC;
  20814. total += outlen;
  20815. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  20816. return WC_TEST_RET_ENC_NC;
  20817. if (outlen != 16)
  20818. return WC_TEST_RET_ENC_NC;
  20819. total += outlen;
  20820. if (total != 32)
  20821. return 3408;
  20822. EVP_CIPHER_CTX_cleanup(en);
  20823. total = 0;
  20824. EVP_CIPHER_CTX_init(de);
  20825. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  20826. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  20827. return WC_TEST_RET_ENC_NC;
  20828. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  20829. return WC_TEST_RET_ENC_NC;
  20830. if (outlen != 0)
  20831. return WC_TEST_RET_ENC_NC;
  20832. total += outlen;
  20833. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  20834. (byte*)&cipher[6], 12) == 0)
  20835. return WC_TEST_RET_ENC_NC;
  20836. if (outlen != 0)
  20837. total += outlen;
  20838. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  20839. (byte*)&cipher[6+12], 14) == 0)
  20840. return WC_TEST_RET_ENC_NC;
  20841. if (outlen != 16)
  20842. return WC_TEST_RET_ENC_NC;
  20843. total += outlen;
  20844. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  20845. return WC_TEST_RET_ENC_NC;
  20846. if (outlen != 2)
  20847. return WC_TEST_RET_ENC_NC;
  20848. total += outlen;
  20849. if (total != 18)
  20850. return 3427;
  20851. if (XMEMCMP(plain, cbcPlain, 18))
  20852. return WC_TEST_RET_ENC_NC;
  20853. EVP_CIPHER_CTX_cleanup(de);
  20854. /* test with encrypting/decrypting more than 16 bytes at once */
  20855. total = 0;
  20856. EVP_CIPHER_CTX_init(en);
  20857. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  20858. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  20859. return WC_TEST_RET_ENC_NC;
  20860. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  20861. (byte*)cbcPlain, 17) == 0)
  20862. return WC_TEST_RET_ENC_NC;
  20863. if (outlen != 16)
  20864. return WC_TEST_RET_ENC_NC;
  20865. total += outlen;
  20866. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  20867. (byte*)&cbcPlain[17] , 1) == 0)
  20868. return WC_TEST_RET_ENC_NC;
  20869. if (outlen != 0)
  20870. return WC_TEST_RET_ENC_NC;
  20871. total += outlen;
  20872. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  20873. return WC_TEST_RET_ENC_NC;
  20874. if (outlen != 16)
  20875. return WC_TEST_RET_ENC_NC;
  20876. total += outlen;
  20877. if (total != 32)
  20878. return WC_TEST_RET_ENC_NC;
  20879. EVP_CIPHER_CTX_cleanup(en);
  20880. total = 0;
  20881. EVP_CIPHER_CTX_init(de);
  20882. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  20883. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  20884. return WC_TEST_RET_ENC_NC;
  20885. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 17) == 0)
  20886. return WC_TEST_RET_ENC_NC;
  20887. if (outlen != 16)
  20888. return WC_TEST_RET_ENC_NC;
  20889. total += outlen;
  20890. /* final call on non block size should fail */
  20891. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  20892. return WC_TEST_RET_ENC_NC;
  20893. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  20894. (byte*)&cipher[17], 1) == 0)
  20895. return WC_TEST_RET_ENC_NC;
  20896. if (outlen != 0)
  20897. total += outlen;
  20898. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  20899. (byte*)&cipher[17+1], 14) == 0)
  20900. return WC_TEST_RET_ENC_NC;
  20901. if (outlen != 0)
  20902. return WC_TEST_RET_ENC_NC;
  20903. total += outlen;
  20904. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  20905. return WC_TEST_RET_ENC_NC;
  20906. if (outlen != 2)
  20907. return WC_TEST_RET_ENC_NC;
  20908. total += outlen;
  20909. if (total != 18)
  20910. return WC_TEST_RET_ENC_NC;
  20911. if (XMEMCMP(plain, cbcPlain, 18))
  20912. return WC_TEST_RET_ENC_NC;
  20913. /* test byte by byte decrypt */
  20914. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  20915. plain[i] = i;
  20916. }
  20917. EVP_CIPHER_CTX_cleanup(de);
  20918. total = 0;
  20919. EVP_CIPHER_CTX_init(en);
  20920. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  20921. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  20922. return WC_TEST_RET_ENC_NC;
  20923. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  20924. (byte*)plain, AES_BLOCK_SIZE * 3) == 0)
  20925. return WC_TEST_RET_ENC_NC;
  20926. if (outlen != AES_BLOCK_SIZE * 3)
  20927. return WC_TEST_RET_ENC_NC;
  20928. total += outlen;
  20929. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  20930. return WC_TEST_RET_ENC_NC;
  20931. if (outlen != AES_BLOCK_SIZE)
  20932. return WC_TEST_RET_ENC_NC;
  20933. total += outlen;
  20934. if (total != sizeof(plain))
  20935. return WC_TEST_RET_ENC_NC;
  20936. EVP_CIPHER_CTX_cleanup(en);
  20937. total = 0;
  20938. EVP_CIPHER_CTX_init(de);
  20939. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  20940. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  20941. return WC_TEST_RET_ENC_NC;
  20942. for (i = 0; i < AES_BLOCK_SIZE * 4; i++) {
  20943. if (EVP_CipherUpdate(de, (byte*)plain + total, &outlen,
  20944. (byte*)cipher + i, 1) == 0)
  20945. return WC_TEST_RET_ENC_NC;
  20946. if (outlen > 0) {
  20947. int j;
  20948. total += outlen;
  20949. for (j = 0; j < total; j++) {
  20950. if (plain[j] != j) {
  20951. return WC_TEST_RET_ENC_NC;
  20952. }
  20953. }
  20954. }
  20955. }
  20956. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  20957. return WC_TEST_RET_ENC_NC;
  20958. total += outlen;
  20959. if (total != AES_BLOCK_SIZE * 3) {
  20960. return WC_TEST_RET_ENC_NC;
  20961. }
  20962. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  20963. if (plain[i] != i) {
  20964. return WC_TEST_RET_ENC_NC;
  20965. }
  20966. }
  20967. EVP_CIPHER_CTX_cleanup(de);
  20968. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20969. wolfSSL_EVP_CIPHER_CTX_free(en);
  20970. wolfSSL_EVP_CIPHER_CTX_free(de);
  20971. #endif
  20972. }
  20973. /* set buffers to be exact size to catch potential over read/write */
  20974. {
  20975. /* EVP_CipherUpdate test */
  20976. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  20977. {
  20978. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  20979. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  20980. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  20981. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  20982. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  20983. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  20984. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  20985. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  20986. };
  20987. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  20988. "0123456789abcdef "; /* align */
  20989. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  20990. "1234567890abcdef "; /* align */
  20991. #define EVP_TEST_BUF_SZ 18
  20992. #define EVP_TEST_BUF_PAD 32
  20993. byte cipher[EVP_TEST_BUF_SZ];
  20994. byte plain [EVP_TEST_BUF_SZ];
  20995. byte padded[EVP_TEST_BUF_PAD];
  20996. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20997. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  20998. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  20999. #else
  21000. EVP_CIPHER_CTX en[1];
  21001. EVP_CIPHER_CTX de[1];
  21002. #endif
  21003. int outlen ;
  21004. int total = 0;
  21005. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21006. if ((en == NULL) || (de == NULL))
  21007. return MEMORY_E;
  21008. #endif
  21009. EVP_CIPHER_CTX_init(en);
  21010. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  21011. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  21012. return WC_TEST_RET_ENC_NC;
  21013. if (EVP_CIPHER_CTX_set_padding(en, 0) != 1)
  21014. return WC_TEST_RET_ENC_NC;
  21015. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  21016. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  21017. return WC_TEST_RET_ENC_NC;
  21018. if (outlen != 16)
  21019. return WC_TEST_RET_ENC_NC;
  21020. total += outlen;
  21021. /* should fail here */
  21022. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) != 0)
  21023. return WC_TEST_RET_ENC_NC;
  21024. EVP_CIPHER_CTX_cleanup(en);
  21025. /* turn padding back on and do successful encrypt */
  21026. total = 0;
  21027. EVP_CIPHER_CTX_init(en);
  21028. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  21029. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  21030. return WC_TEST_RET_ENC_NC;
  21031. if (EVP_CIPHER_CTX_set_padding(en, 1) != 1)
  21032. return WC_TEST_RET_ENC_NC;
  21033. if (EVP_CipherUpdate(en, (byte*)padded, &outlen,
  21034. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  21035. return WC_TEST_RET_ENC_NC;
  21036. if (outlen != 16)
  21037. return WC_TEST_RET_ENC_NC;
  21038. total += outlen;
  21039. if (EVP_CipherFinal(en, (byte*)&padded[total], &outlen) == 0)
  21040. return WC_TEST_RET_ENC_NC;
  21041. total += outlen;
  21042. if (total != 32)
  21043. return WC_TEST_RET_ENC_NC;
  21044. XMEMCPY(cipher, padded, EVP_TEST_BUF_SZ);
  21045. EVP_CIPHER_CTX_cleanup(en);
  21046. /* test out of bounds read on buffers w/o padding during decryption */
  21047. total = 0;
  21048. EVP_CIPHER_CTX_init(de);
  21049. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  21050. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  21051. return WC_TEST_RET_ENC_NC;
  21052. if (EVP_CIPHER_CTX_set_padding(de, 0) != 1)
  21053. return WC_TEST_RET_ENC_NC;
  21054. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher,
  21055. EVP_TEST_BUF_SZ) == 0)
  21056. return WC_TEST_RET_ENC_NC;
  21057. if (outlen != 16)
  21058. return WC_TEST_RET_ENC_NC;
  21059. total += outlen;
  21060. /* should fail since not using padding */
  21061. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  21062. return WC_TEST_RET_ENC_NC;
  21063. EVP_CIPHER_CTX_cleanup(de);
  21064. total = 0;
  21065. EVP_CIPHER_CTX_init(de);
  21066. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  21067. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  21068. return WC_TEST_RET_ENC_NC;
  21069. if (EVP_CIPHER_CTX_set_padding(de, 1) != 1)
  21070. return WC_TEST_RET_ENC_NC;
  21071. if (EVP_CipherUpdate(de, (byte*)padded, &outlen, (byte*)padded,
  21072. EVP_TEST_BUF_PAD) == 0)
  21073. return WC_TEST_RET_ENC_NC;
  21074. if (outlen != 16)
  21075. return WC_TEST_RET_ENC_NC;
  21076. total += outlen;
  21077. if (EVP_CipherFinal(de, (byte*)&padded[total], &outlen) == 0)
  21078. return WC_TEST_RET_ENC_NC;
  21079. if (XMEMCMP(padded, cbcPlain, EVP_TEST_BUF_SZ))
  21080. return WC_TEST_RET_ENC_NC;
  21081. EVP_CIPHER_CTX_cleanup(de);
  21082. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21083. wolfSSL_EVP_CIPHER_CTX_free(en);
  21084. wolfSSL_EVP_CIPHER_CTX_free(de);
  21085. #endif
  21086. }
  21087. { /* evp_cipher test: EVP_aes_128_cbc */
  21088. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21089. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  21090. #else
  21091. EVP_CIPHER_CTX ctx[1];
  21092. #endif
  21093. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  21094. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  21095. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  21096. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  21097. };
  21098. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  21099. {
  21100. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  21101. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  21102. };
  21103. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  21104. "0123456789abcdef "; /* align */
  21105. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  21106. "1234567890abcdef "; /* align */
  21107. byte cipher[AES_BLOCK_SIZE * 4];
  21108. byte plain [AES_BLOCK_SIZE * 4];
  21109. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21110. if (ctx == NULL)
  21111. return MEMORY_E;
  21112. #endif
  21113. EVP_CIPHER_CTX_init(ctx);
  21114. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  21115. return WC_TEST_RET_ENC_NC;
  21116. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  21117. return WC_TEST_RET_ENC_NC;
  21118. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  21119. return WC_TEST_RET_ENC_NC;
  21120. EVP_CIPHER_CTX_cleanup(ctx);
  21121. EVP_CIPHER_CTX_init(ctx);
  21122. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  21123. return WC_TEST_RET_ENC_NC;
  21124. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  21125. return WC_TEST_RET_ENC_NC;
  21126. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  21127. return WC_TEST_RET_ENC_NC;
  21128. EVP_CIPHER_CTX_cleanup(ctx);
  21129. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21130. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  21131. #endif
  21132. } /* end evp_cipher test: EVP_aes_128_cbc*/
  21133. #endif /* WOLFSSL_AES_128 */
  21134. #endif /* HAVE_AES_CBC */
  21135. #ifndef WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API
  21136. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  21137. { /* evp_cipher test: EVP_aes_256_ecb*/
  21138. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21139. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  21140. #else
  21141. EVP_CIPHER_CTX ctx[1];
  21142. #endif
  21143. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  21144. {
  21145. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21146. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  21147. };
  21148. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  21149. {
  21150. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  21151. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  21152. };
  21153. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  21154. {
  21155. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  21156. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  21157. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  21158. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  21159. };
  21160. byte cipher[AES_BLOCK_SIZE * 4];
  21161. byte plain [AES_BLOCK_SIZE * 4];
  21162. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21163. if (ctx == NULL)
  21164. return MEMORY_E;
  21165. #endif
  21166. EVP_CIPHER_CTX_init(ctx);
  21167. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1) == 0)
  21168. return WC_TEST_RET_ENC_NC;
  21169. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  21170. return WC_TEST_RET_ENC_NC;
  21171. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  21172. return WC_TEST_RET_ENC_NC;
  21173. EVP_CIPHER_CTX_init(ctx);
  21174. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0) == 0)
  21175. return WC_TEST_RET_ENC_NC;
  21176. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  21177. return WC_TEST_RET_ENC_NC;
  21178. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  21179. return WC_TEST_RET_ENC_NC;
  21180. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21181. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  21182. #endif
  21183. } /* end evp_cipher test */
  21184. #endif /* HAVE_AES_ECB && WOLFSSL_AES_256 */
  21185. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  21186. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  21187. {
  21188. /* Test: AES_encrypt/decrypt/set Key */
  21189. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21190. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  21191. #ifdef HAVE_AES_DECRYPT
  21192. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  21193. #endif
  21194. #else
  21195. AES_KEY enc[1];
  21196. #ifdef HAVE_AES_DECRYPT
  21197. AES_KEY dec[1];
  21198. #endif
  21199. #endif
  21200. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  21201. {
  21202. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21203. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  21204. };
  21205. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  21206. {
  21207. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  21208. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  21209. };
  21210. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  21211. {
  21212. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  21213. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  21214. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  21215. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  21216. };
  21217. byte plain[sizeof(msg)];
  21218. byte cipher[sizeof(msg)];
  21219. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21220. if (enc == NULL)
  21221. return MEMORY_E;
  21222. #ifdef HAVE_AES_DECRYPT
  21223. if (dec == NULL)
  21224. return MEMORY_E;
  21225. #endif
  21226. #endif
  21227. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  21228. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  21229. AES_encrypt(msg, cipher, enc);
  21230. #ifdef HAVE_AES_DECRYPT
  21231. AES_decrypt(cipher, plain, dec);
  21232. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  21233. return WC_TEST_RET_ENC_NC;
  21234. #endif /* HAVE_AES_DECRYPT */
  21235. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  21236. return WC_TEST_RET_ENC_NC;
  21237. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21238. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  21239. #ifdef HAVE_AES_DECRYPT
  21240. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  21241. #endif
  21242. #endif
  21243. }
  21244. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  21245. #endif /* !WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API */
  21246. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  21247. #ifdef WOLFSSL_AES_COUNTER
  21248. {
  21249. byte plainBuff [64];
  21250. byte cipherBuff[64];
  21251. #ifdef WOLFSSL_AES_128
  21252. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  21253. {
  21254. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  21255. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  21256. };
  21257. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  21258. {
  21259. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  21260. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  21261. };
  21262. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  21263. {
  21264. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21265. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  21266. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  21267. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  21268. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  21269. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  21270. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  21271. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  21272. };
  21273. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  21274. {
  21275. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  21276. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  21277. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  21278. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  21279. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  21280. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  21281. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  21282. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  21283. };
  21284. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  21285. {
  21286. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  21287. 0xc2
  21288. };
  21289. #endif
  21290. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  21291. * NIST Special Publication 800-38A */
  21292. #ifdef WOLFSSL_AES_192
  21293. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  21294. {
  21295. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  21296. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  21297. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  21298. };
  21299. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  21300. {
  21301. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  21302. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  21303. };
  21304. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  21305. {
  21306. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21307. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  21308. };
  21309. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  21310. {
  21311. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  21312. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  21313. };
  21314. #endif /* WOLFSSL_AES_192 */
  21315. #ifdef WOLFSSL_AES_256
  21316. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  21317. * NIST Special Publication 800-38A */
  21318. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  21319. {
  21320. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  21321. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  21322. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  21323. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  21324. };
  21325. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  21326. {
  21327. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  21328. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  21329. };
  21330. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  21331. {
  21332. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21333. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  21334. };
  21335. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  21336. {
  21337. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  21338. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  21339. };
  21340. #endif /* WOLFSSL_AES_256 */
  21341. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21342. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  21343. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  21344. #else
  21345. EVP_CIPHER_CTX en[1];
  21346. EVP_CIPHER_CTX de[1];
  21347. #endif
  21348. #ifdef WOLFSSL_AES_128
  21349. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21350. EVP_CIPHER_CTX *p_en;
  21351. EVP_CIPHER_CTX *p_de;
  21352. #endif
  21353. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21354. if ((en == NULL) || (de == NULL))
  21355. return MEMORY_E;
  21356. #endif
  21357. EVP_CIPHER_CTX_init(en);
  21358. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  21359. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  21360. return WC_TEST_RET_ENC_NC;
  21361. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  21362. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  21363. return WC_TEST_RET_ENC_NC;
  21364. EVP_CIPHER_CTX_init(de);
  21365. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  21366. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  21367. return WC_TEST_RET_ENC_NC;
  21368. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  21369. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  21370. return WC_TEST_RET_ENC_NC;
  21371. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  21372. return WC_TEST_RET_ENC_NC;
  21373. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  21374. return WC_TEST_RET_ENC_NC;
  21375. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  21376. return WC_TEST_RET_ENC_NC;
  21377. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  21378. return WC_TEST_RET_ENC_NC;
  21379. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21380. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  21381. if (p_en == NULL)
  21382. return WC_TEST_RET_ENC_ERRNO;
  21383. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  21384. if (p_de == NULL)
  21385. return WC_TEST_RET_ENC_ERRNO;
  21386. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  21387. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  21388. return WC_TEST_RET_ENC_NC;
  21389. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  21390. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  21391. return WC_TEST_RET_ENC_NC;
  21392. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  21393. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  21394. return WC_TEST_RET_ENC_NC;
  21395. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  21396. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  21397. return WC_TEST_RET_ENC_NC;
  21398. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  21399. return WC_TEST_RET_ENC_NC;
  21400. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  21401. return WC_TEST_RET_ENC_NC;
  21402. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  21403. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  21404. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  21405. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  21406. return WC_TEST_RET_ENC_NC;
  21407. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  21408. return WC_TEST_RET_ENC_NC;
  21409. EVP_CIPHER_CTX_init(en);
  21410. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  21411. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  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. EVP_CIPHER_CTX_init(de);
  21416. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  21417. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  21418. return WC_TEST_RET_ENC_NC;
  21419. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  21420. return WC_TEST_RET_ENC_NC;
  21421. if (XMEMCMP(plainBuff, ctrPlain, 9))
  21422. return WC_TEST_RET_ENC_NC;
  21423. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  21424. return WC_TEST_RET_ENC_NC;
  21425. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  21426. return WC_TEST_RET_ENC_NC;
  21427. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  21428. return WC_TEST_RET_ENC_NC;
  21429. if (XMEMCMP(plainBuff, ctrPlain, 9))
  21430. return WC_TEST_RET_ENC_NC;
  21431. if (XMEMCMP(cipherBuff, oddCipher, 9))
  21432. return WC_TEST_RET_ENC_NC;
  21433. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  21434. return WC_TEST_RET_ENC_NC;
  21435. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  21436. return WC_TEST_RET_ENC_NC;
  21437. #endif /* WOLFSSL_AES_128 */
  21438. #ifdef WOLFSSL_AES_192
  21439. EVP_CIPHER_CTX_init(en);
  21440. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  21441. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  21442. return WC_TEST_RET_ENC_NC;
  21443. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  21444. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  21445. return WC_TEST_RET_ENC_NC;
  21446. EVP_CIPHER_CTX_init(de);
  21447. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  21448. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  21449. return WC_TEST_RET_ENC_NC;
  21450. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  21451. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  21452. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  21453. return WC_TEST_RET_ENC_NC;
  21454. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  21455. return WC_TEST_RET_ENC_NC;
  21456. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  21457. return WC_TEST_RET_ENC_NC;
  21458. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  21459. return WC_TEST_RET_ENC_NC;
  21460. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  21461. return WC_TEST_RET_ENC_NC;
  21462. #endif /* WOLFSSL_AES_192 */
  21463. #ifdef WOLFSSL_AES_256
  21464. EVP_CIPHER_CTX_init(en);
  21465. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  21466. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  21467. return WC_TEST_RET_ENC_NC;
  21468. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  21469. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  21470. return WC_TEST_RET_ENC_NC;
  21471. EVP_CIPHER_CTX_init(de);
  21472. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  21473. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  21474. return WC_TEST_RET_ENC_NC;
  21475. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  21476. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  21477. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  21478. return WC_TEST_RET_ENC_NC;
  21479. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  21480. return WC_TEST_RET_ENC_NC;
  21481. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  21482. return WC_TEST_RET_ENC_NC;
  21483. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  21484. return WC_TEST_RET_ENC_NC;
  21485. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  21486. return WC_TEST_RET_ENC_NC;
  21487. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21488. wolfSSL_EVP_CIPHER_CTX_free(en);
  21489. wolfSSL_EVP_CIPHER_CTX_free(de);
  21490. #endif
  21491. #endif /* WOLFSSL_AES_256 */
  21492. }
  21493. #endif /* HAVE_AES_COUNTER */
  21494. #ifndef WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API
  21495. #if defined(WOLFSSL_AES_CFB) && defined(WOLFSSL_AES_128)
  21496. {
  21497. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21498. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  21499. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  21500. #else
  21501. AES_KEY enc[1];
  21502. AES_KEY dec[1];
  21503. #endif
  21504. WOLFSSL_SMALL_STACK_STATIC const byte setIv[] = {
  21505. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  21506. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  21507. };
  21508. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  21509. {
  21510. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  21511. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  21512. };
  21513. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  21514. {
  21515. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  21516. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  21517. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  21518. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b
  21519. };
  21520. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  21521. {
  21522. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21523. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  21524. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  21525. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51
  21526. };
  21527. byte cipher[AES_BLOCK_SIZE * 2];
  21528. byte iv[AES_BLOCK_SIZE]; /* iv buffer is updeated by API */
  21529. int num = 0;
  21530. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21531. if ((enc == NULL) || (dec == NULL))
  21532. return MEMORY_E;
  21533. #endif
  21534. XMEMCPY(iv, setIv, sizeof(setIv));
  21535. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, enc);
  21536. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, dec);
  21537. wolfSSL_AES_cfb128_encrypt(msg, cipher, AES_BLOCK_SIZE - 1, enc, iv,
  21538. &num, AES_ENCRYPT);
  21539. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE - 1))
  21540. return WC_TEST_RET_ENC_NC;
  21541. if (num != 15) /* should have used 15 of the 16 bytes */
  21542. return WC_TEST_RET_ENC_NC;
  21543. wolfSSL_AES_cfb128_encrypt(msg + AES_BLOCK_SIZE - 1,
  21544. cipher + AES_BLOCK_SIZE - 1, AES_BLOCK_SIZE + 1, enc, iv,
  21545. &num, AES_ENCRYPT);
  21546. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  21547. return WC_TEST_RET_ENC_NC;
  21548. if (num != 0)
  21549. return WC_TEST_RET_ENC_NC;
  21550. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21551. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  21552. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  21553. #endif
  21554. }
  21555. #endif /* WOLFSSL_AES_CFB && WOLFSSL_AES_128 */
  21556. #endif /* !WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API */
  21557. return 0;
  21558. }
  21559. #endif /* !defined(NO_AES) && !defined(WOLFCRYPT_ONLY) */
  21560. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_test(void)
  21561. {
  21562. wc_test_ret_t ret;
  21563. #ifdef WOLFSSL_SMALL_STACK
  21564. EVP_MD_CTX *md_ctx = (EVP_MD_CTX *)XMALLOC(sizeof(EVP_MD_CTX), NULL, DYNAMIC_TYPE_OPENSSL);
  21565. #else
  21566. EVP_MD_CTX md_ctx[1];
  21567. #endif
  21568. testVector a, b, c, d, e, f;
  21569. byte hash[WC_SHA256_DIGEST_SIZE*2]; /* max size */
  21570. #ifdef WOLFSSL_SMALL_STACK
  21571. if (md_ctx == NULL)
  21572. return WC_TEST_RET_ENC_EC(MEMORY_E);
  21573. #endif
  21574. WOLFSSL_ENTER("openssl_test");
  21575. a.inLen = 0;
  21576. b.inLen = c.inLen = d.inLen = e.inLen = f.inLen = a.inLen;
  21577. (void)a;
  21578. (void)b;
  21579. (void)c;
  21580. (void)d;
  21581. (void)e;
  21582. (void)f;
  21583. /* test malloc / free , 10 is an arbitrary amount of memory chosen */
  21584. {
  21585. byte* p;
  21586. p = (byte*)CRYPTO_malloc(10, "", 0);
  21587. if (p == NULL) {
  21588. return WC_TEST_RET_ENC_NC;
  21589. }
  21590. XMEMSET(p, 0, 10);
  21591. CRYPTO_free(p, "", 0);
  21592. }
  21593. #ifndef NO_MD5
  21594. a.input = "1234567890123456789012345678901234567890123456789012345678"
  21595. "9012345678901234567890";
  21596. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  21597. "\x7a";
  21598. a.inLen = XSTRLEN(a.input);
  21599. a.outLen = WC_MD5_DIGEST_SIZE;
  21600. EVP_MD_CTX_init(md_ctx);
  21601. ret = EVP_DigestInit(md_ctx, EVP_md5());
  21602. if (ret == WOLFSSL_SUCCESS) {
  21603. ret = EVP_DigestUpdate(md_ctx, a.input, (unsigned long)a.inLen);
  21604. }
  21605. if (ret == WOLFSSL_SUCCESS) {
  21606. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21607. }
  21608. EVP_MD_CTX_cleanup(md_ctx);
  21609. if (ret != WOLFSSL_SUCCESS)
  21610. return WC_TEST_RET_ENC_NC;
  21611. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0)
  21612. return WC_TEST_RET_ENC_NC;
  21613. #endif /* NO_MD5 */
  21614. #ifndef NO_SHA
  21615. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  21616. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  21617. "aaaaaaaaaa";
  21618. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  21619. "\x53\x99\x5E\x26\xA0";
  21620. b.inLen = XSTRLEN(b.input);
  21621. b.outLen = WC_SHA_DIGEST_SIZE;
  21622. EVP_MD_CTX_init(md_ctx);
  21623. ret = EVP_DigestInit(md_ctx, EVP_sha1());
  21624. if (ret == WOLFSSL_SUCCESS) {
  21625. ret = EVP_DigestUpdate(md_ctx, b.input, (unsigned long)b.inLen);
  21626. if (ret == WOLFSSL_SUCCESS)
  21627. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21628. }
  21629. EVP_MD_CTX_cleanup(md_ctx);
  21630. if (ret != WOLFSSL_SUCCESS)
  21631. return WC_TEST_RET_ENC_NC;
  21632. if (XMEMCMP(hash, b.output, b.outLen) != 0)
  21633. return WC_TEST_RET_ENC_NC;
  21634. #endif /* NO_SHA */
  21635. #ifdef WOLFSSL_SHA224
  21636. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21637. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21638. e.output = "\xc9\x7c\xa9\xa5\x59\x85\x0c\xe9\x7a\x04\xa9\x6d\xef\x6d\x99"
  21639. "\xa9\xe0\xe0\xe2\xab\x14\xe6\xb8\xdf\x26\x5f\xc0\xb3";
  21640. e.inLen = XSTRLEN(e.input);
  21641. e.outLen = WC_SHA224_DIGEST_SIZE;
  21642. EVP_MD_CTX_init(md_ctx);
  21643. ret = EVP_DigestInit(md_ctx, EVP_sha224());
  21644. if (ret == WOLFSSL_SUCCESS) {
  21645. ret = EVP_DigestUpdate(md_ctx, e.input, (unsigned long)e.inLen);
  21646. if (ret == WOLFSSL_SUCCESS)
  21647. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21648. }
  21649. EVP_MD_CTX_cleanup(md_ctx);
  21650. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  21651. return WC_TEST_RET_ENC_NC;
  21652. }
  21653. #endif /* WOLFSSL_SHA224 */
  21654. #ifndef NO_SHA256
  21655. d.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  21656. d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  21657. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  21658. "\x06\xC1";
  21659. d.inLen = XSTRLEN(d.input);
  21660. d.outLen = WC_SHA256_DIGEST_SIZE;
  21661. EVP_MD_CTX_init(md_ctx);
  21662. ret = EVP_DigestInit(md_ctx, EVP_sha256());
  21663. if (ret == WOLFSSL_SUCCESS) {
  21664. ret = EVP_DigestUpdate(md_ctx, d.input, (unsigned long)d.inLen);
  21665. if (ret == WOLFSSL_SUCCESS)
  21666. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21667. }
  21668. EVP_MD_CTX_cleanup(md_ctx);
  21669. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, d.output, d.outLen) != 0) {
  21670. return WC_TEST_RET_ENC_NC;
  21671. }
  21672. #endif /* !NO_SHA256 */
  21673. #ifdef WOLFSSL_SHA384
  21674. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21675. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21676. e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  21677. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  21678. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  21679. "\x74\x60\x39";
  21680. e.inLen = XSTRLEN(e.input);
  21681. e.outLen = WC_SHA384_DIGEST_SIZE;
  21682. EVP_MD_CTX_init(md_ctx);
  21683. ret = EVP_DigestInit(md_ctx, EVP_sha384());
  21684. if (ret == WOLFSSL_SUCCESS) {
  21685. ret = EVP_DigestUpdate(md_ctx, e.input, (unsigned long)e.inLen);
  21686. if (ret == WOLFSSL_SUCCESS)
  21687. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21688. }
  21689. EVP_MD_CTX_cleanup(md_ctx);
  21690. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  21691. return WC_TEST_RET_ENC_NC;
  21692. }
  21693. #endif /* WOLFSSL_SHA384 */
  21694. #ifdef WOLFSSL_SHA512
  21695. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21696. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21697. f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  21698. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  21699. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  21700. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  21701. "\x87\x4b\xe9\x09";
  21702. f.inLen = XSTRLEN(f.input);
  21703. f.outLen = WC_SHA512_DIGEST_SIZE;
  21704. EVP_MD_CTX_init(md_ctx);
  21705. ret = EVP_DigestInit(md_ctx, EVP_sha512());
  21706. if (ret == WOLFSSL_SUCCESS) {
  21707. ret = EVP_DigestUpdate(md_ctx, f.input, (unsigned long)f.inLen);
  21708. if (ret == WOLFSSL_SUCCESS)
  21709. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21710. }
  21711. EVP_MD_CTX_cleanup(md_ctx);
  21712. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, f.output, f.outLen) != 0) {
  21713. return WC_TEST_RET_ENC_NC;
  21714. }
  21715. #if !defined(WOLFSSL_NOSHA512_224) && \
  21716. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  21717. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21718. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21719. f.output = "\x23\xfe\xc5\xbb\x94\xd6\x0b\x23\x30\x81\x92\x64\x0b\x0c\x45"
  21720. "\x33\x35\xd6\x64\x73\x4f\xe4\x0e\x72\x68\x67\x4a\xf9";
  21721. f.inLen = XSTRLEN(f.input);
  21722. f.outLen = WC_SHA512_224_DIGEST_SIZE;
  21723. EVP_MD_CTX_init(md_ctx);
  21724. ret = EVP_DigestInit(md_ctx, EVP_sha512_224());
  21725. if (ret == WOLFSSL_SUCCESS) {
  21726. ret = EVP_DigestUpdate(md_ctx, f.input, (unsigned long)f.inLen);
  21727. if (ret == WOLFSSL_SUCCESS)
  21728. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21729. }
  21730. EVP_MD_CTX_cleanup(md_ctx);
  21731. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, f.output, f.outLen) != 0) {
  21732. return WC_TEST_RET_ENC_NC;
  21733. }
  21734. #endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
  21735. #if !defined(WOLFSSL_NOSHA512_256) && \
  21736. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  21737. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21738. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21739. f.output = "\x39\x28\xe1\x84\xfb\x86\x90\xf8\x40\xda\x39\x88\x12\x1d\x31"
  21740. "\xbe\x65\xcb\x9d\x3e\xf8\x3e\xe6\x14\x6f\xea\xc8\x61\xe1\x9b"
  21741. "\x56\x3a";
  21742. f.inLen = XSTRLEN(f.input);
  21743. f.outLen = WC_SHA512_256_DIGEST_SIZE;
  21744. EVP_MD_CTX_init(md_ctx);
  21745. ret = EVP_DigestInit(md_ctx, EVP_sha512_256());
  21746. if (ret == WOLFSSL_SUCCESS) {
  21747. ret = EVP_DigestUpdate(md_ctx, f.input, (unsigned long)f.inLen);
  21748. if (ret == WOLFSSL_SUCCESS)
  21749. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21750. }
  21751. EVP_MD_CTX_cleanup(md_ctx);
  21752. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, f.output, f.outLen) != 0) {
  21753. return WC_TEST_RET_ENC_NC;
  21754. }
  21755. #endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
  21756. #endif /* WOLFSSL_SHA512 */
  21757. #ifdef WOLFSSL_SHA3
  21758. #ifndef WOLFSSL_NOSHA3_224
  21759. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21760. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21761. e.output = "\x54\x3e\x68\x68\xe1\x66\x6c\x1a\x64\x36\x30\xdf\x77\x36\x7a"
  21762. "\xe5\xa6\x2a\x85\x07\x0a\x51\xc1\x4c\xbf\x66\x5c\xbc";
  21763. e.inLen = XSTRLEN(e.input);
  21764. e.outLen = WC_SHA3_224_DIGEST_SIZE;
  21765. EVP_MD_CTX_init(md_ctx);
  21766. ret = EVP_DigestInit(md_ctx, EVP_sha3_224());
  21767. if (ret == WOLFSSL_SUCCESS) {
  21768. ret = EVP_DigestUpdate(md_ctx, e.input, (unsigned long)e.inLen);
  21769. if (ret == WOLFSSL_SUCCESS)
  21770. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21771. }
  21772. EVP_MD_CTX_cleanup(md_ctx);
  21773. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  21774. return WC_TEST_RET_ENC_NC;
  21775. }
  21776. #endif /* WOLFSSL_NOSHA3_224 */
  21777. #ifndef WOLFSSL_NOSHA3_256
  21778. d.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21779. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21780. d.output = "\x91\x6f\x60\x61\xfe\x87\x97\x41\xca\x64\x69\xb4\x39\x71\xdf"
  21781. "\xdb\x28\xb1\xa3\x2d\xc3\x6c\xb3\x25\x4e\x81\x2b\xe2\x7a\xad"
  21782. "\x1d\x18";
  21783. d.inLen = XSTRLEN(d.input);
  21784. d.outLen = WC_SHA3_256_DIGEST_SIZE;
  21785. EVP_MD_CTX_init(md_ctx);
  21786. ret = EVP_DigestInit(md_ctx, EVP_sha3_256());
  21787. if (ret == WOLFSSL_SUCCESS) {
  21788. ret = EVP_DigestUpdate(md_ctx, d.input, (unsigned long)d.inLen);
  21789. if (ret == WOLFSSL_SUCCESS)
  21790. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21791. }
  21792. EVP_MD_CTX_cleanup(md_ctx);
  21793. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, d.output, d.outLen) != 0) {
  21794. return WC_TEST_RET_ENC_NC;
  21795. }
  21796. #endif /* WOLFSSL_NOSHA3_256 */
  21797. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21798. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21799. e.output = "\x79\x40\x7d\x3b\x59\x16\xb5\x9c\x3e\x30\xb0\x98\x22\x97\x47"
  21800. "\x91\xc3\x13\xfb\x9e\xcc\x84\x9e\x40\x6f\x23\x59\x2d\x04\xf6"
  21801. "\x25\xdc\x8c\x70\x9b\x98\xb4\x3b\x38\x52\xb3\x37\x21\x61\x79"
  21802. "\xaa\x7f\xc7";
  21803. e.inLen = XSTRLEN(e.input);
  21804. e.outLen = WC_SHA3_384_DIGEST_SIZE;
  21805. EVP_MD_CTX_init(md_ctx);
  21806. ret = EVP_DigestInit(md_ctx, EVP_sha3_384());
  21807. if (ret == WOLFSSL_SUCCESS) {
  21808. ret = EVP_DigestUpdate(md_ctx, e.input, (unsigned long)e.inLen);
  21809. if (ret == WOLFSSL_SUCCESS)
  21810. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21811. }
  21812. EVP_MD_CTX_cleanup(md_ctx);
  21813. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  21814. return WC_TEST_RET_ENC_NC;
  21815. }
  21816. #ifndef WOLFSSL_NOSHA3_512
  21817. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21818. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21819. f.output = "\xaf\xeb\xb2\xef\x54\x2e\x65\x79\xc5\x0c\xad\x06\xd2\xe5\x78"
  21820. "\xf9\xf8\xdd\x68\x81\xd7\xdc\x82\x4d\x26\x36\x0f\xee\xbf\x18"
  21821. "\xa4\xfa\x73\xe3\x26\x11\x22\x94\x8e\xfc\xfd\x49\x2e\x74\xe8"
  21822. "\x2e\x21\x89\xed\x0f\xb4\x40\xd1\x87\xf3\x82\x27\x0c\xb4\x55"
  21823. "\xf2\x1d\xd1\x85";
  21824. f.inLen = XSTRLEN(f.input);
  21825. f.outLen = WC_SHA3_512_DIGEST_SIZE;
  21826. EVP_MD_CTX_init(md_ctx);
  21827. ret = EVP_DigestInit(md_ctx, EVP_sha3_512());
  21828. if (ret == WOLFSSL_SUCCESS) {
  21829. ret = EVP_DigestUpdate(md_ctx, f.input, (unsigned long)f.inLen);
  21830. if (ret == WOLFSSL_SUCCESS)
  21831. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21832. }
  21833. EVP_MD_CTX_cleanup(md_ctx);
  21834. if (ret != WOLFSSL_SUCCESS ||
  21835. XMEMCMP(hash, f.output, f.outLen) != 0) {
  21836. return WC_TEST_RET_ENC_NC;
  21837. }
  21838. #endif /* WOLFSSL_NOSHA3_512 */
  21839. #endif /* WOLFSSL_SHA3 */
  21840. #ifdef WOLFSSL_SMALL_STACK
  21841. XFREE(md_ctx, NULL, DYNAMIC_TYPE_OPENSSL);
  21842. md_ctx = NULL;
  21843. #endif
  21844. #ifndef WC_NO_RNG
  21845. if (RAND_bytes(hash, sizeof(hash)) != WOLFSSL_SUCCESS)
  21846. return WC_TEST_RET_ENC_NC;
  21847. #endif
  21848. #ifndef NO_MD5
  21849. c.input = "what do ya want for nothing?";
  21850. c.output = "\x55\x78\xe8\x48\x4b\xcc\x93\x80\x93\xec\x53\xaf\x22\xd6\x14"
  21851. "\x76";
  21852. c.inLen = XSTRLEN(c.input);
  21853. c.outLen = WC_MD5_DIGEST_SIZE;
  21854. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)
  21855. /* Expect failure with MD5 + HMAC when using FIPS 140-3. */
  21856. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  21857. hash, 0) != NULL)
  21858. #else
  21859. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  21860. hash, 0) == NULL ||
  21861. XMEMCMP(hash, c.output, c.outLen) != 0)
  21862. #endif
  21863. {
  21864. return WC_TEST_RET_ENC_NC;
  21865. }
  21866. #endif /* NO_MD5 */
  21867. #ifndef NO_DES3
  21868. { /* des test */
  21869. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  21870. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  21871. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  21872. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  21873. };
  21874. byte plain[24];
  21875. byte cipher[24];
  21876. const_DES_cblock key = {
  21877. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  21878. };
  21879. DES_cblock iv = {
  21880. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  21881. };
  21882. DES_key_schedule sched;
  21883. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  21884. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  21885. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  21886. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  21887. };
  21888. DES_key_sched(&key, &sched);
  21889. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  21890. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  21891. if (XMEMCMP(plain, vector, sizeof(vector)) != 0)
  21892. return WC_TEST_RET_ENC_NC;
  21893. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  21894. return WC_TEST_RET_ENC_NC;
  21895. /* test changing iv */
  21896. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  21897. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  21898. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  21899. return WC_TEST_RET_ENC_NC;
  21900. } /* end des test */
  21901. #endif /* NO_DES3 */
  21902. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  21903. if ((ret = openssl_aes_test()) != 0) {
  21904. return ret;
  21905. }
  21906. #if defined(WOLFSSL_AES_128) && defined(HAVE_AES_CBC)
  21907. { /* evp_cipher test: EVP_aes_128_cbc */
  21908. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21909. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  21910. #else
  21911. EVP_CIPHER_CTX ctx[1];
  21912. #endif
  21913. int idx, cipherSz, plainSz;
  21914. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  21915. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  21916. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  21917. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  21918. };
  21919. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  21920. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  21921. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  21922. 0x3b,0x5d,0x41,0x97,0x94,0x25,0xa4,0xb4,
  21923. 0xae,0x7b,0x34,0xd0,0x3f,0x0c,0xbc,0x06
  21924. };
  21925. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  21926. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  21927. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  21928. 0x7d,0x37,0x7b,0x0b,0x44,0xaa,0xb5,0xf0,
  21929. 0x5f,0x34,0xb4,0xde,0xb5,0xbd,0x2a,0xbb
  21930. };
  21931. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  21932. "0123456789abcdef "; /* align */
  21933. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  21934. "1234567890abcdef "; /* align */
  21935. byte cipher[AES_BLOCK_SIZE * 4];
  21936. byte plain [AES_BLOCK_SIZE * 4];
  21937. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21938. if (ctx == NULL)
  21939. return MEMORY_E;
  21940. #endif
  21941. cipherSz = 0;
  21942. EVP_CIPHER_CTX_init(ctx);
  21943. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  21944. if (ret == WOLFSSL_SUCCESS) {
  21945. ret = EVP_CipherUpdate(ctx, cipher, &idx, (byte*)msg, sizeof(msg));
  21946. if (ret == WOLFSSL_SUCCESS)
  21947. cipherSz += idx;
  21948. }
  21949. if (ret == WOLFSSL_SUCCESS) {
  21950. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  21951. if (ret == WOLFSSL_SUCCESS)
  21952. cipherSz += idx;
  21953. }
  21954. EVP_CIPHER_CTX_cleanup(ctx);
  21955. if (ret != WOLFSSL_SUCCESS)
  21956. return WC_TEST_RET_ENC_NC;
  21957. if (cipherSz != (int)sizeof(verify) || XMEMCMP(cipher, verify, cipherSz))
  21958. return WC_TEST_RET_ENC_NC;
  21959. /* check partial decrypt (not enough padding for full block) */
  21960. plainSz = 0;
  21961. EVP_CIPHER_CTX_init(ctx);
  21962. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  21963. if (ret == WOLFSSL_SUCCESS) {
  21964. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, 1);
  21965. if (ret == WOLFSSL_SUCCESS)
  21966. plainSz += idx;
  21967. }
  21968. if (ret == WOLFSSL_SUCCESS) {
  21969. /* this test should fail... not enough padding for full block */
  21970. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  21971. if (plainSz == 0 && ret != WOLFSSL_SUCCESS)
  21972. ret = WOLFSSL_SUCCESS;
  21973. else
  21974. ret = WC_TEST_RET_ENC_NC;
  21975. }
  21976. else
  21977. ret = WC_TEST_RET_ENC_NC;
  21978. EVP_CIPHER_CTX_cleanup(ctx);
  21979. if (ret != WOLFSSL_SUCCESS)
  21980. return ret;
  21981. plainSz = 0;
  21982. EVP_CIPHER_CTX_init(ctx);
  21983. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  21984. if (ret == WOLFSSL_SUCCESS) {
  21985. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, cipherSz);
  21986. if (ret == WOLFSSL_SUCCESS)
  21987. plainSz += idx;
  21988. }
  21989. if (ret == WOLFSSL_SUCCESS) {
  21990. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  21991. if (ret == WOLFSSL_SUCCESS)
  21992. plainSz += idx;
  21993. }
  21994. EVP_CIPHER_CTX_cleanup(ctx);
  21995. if (ret != WOLFSSL_SUCCESS)
  21996. return WC_TEST_RET_ENC_NC;
  21997. if (plainSz != (int)sizeof(msg) || XMEMCMP(plain, msg, sizeof(msg)))
  21998. return WC_TEST_RET_ENC_NC;
  21999. cipherSz = 0;
  22000. EVP_CIPHER_CTX_init(ctx);
  22001. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  22002. if (ret == WOLFSSL_SUCCESS) {
  22003. ret = EVP_CipherUpdate(ctx, cipher, &idx, msg, AES_BLOCK_SIZE);
  22004. if (ret == WOLFSSL_SUCCESS)
  22005. cipherSz += idx;
  22006. }
  22007. if (ret == WOLFSSL_SUCCESS) {
  22008. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  22009. if (ret == WOLFSSL_SUCCESS)
  22010. cipherSz += idx;
  22011. }
  22012. EVP_CIPHER_CTX_cleanup(ctx);
  22013. if (ret != WOLFSSL_SUCCESS)
  22014. return WC_TEST_RET_ENC_NC;
  22015. if (cipherSz != (int)sizeof(verify2) || XMEMCMP(cipher, verify2, cipherSz))
  22016. return WC_TEST_RET_ENC_NC;
  22017. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22018. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  22019. #endif
  22020. } /* end evp_cipher test: EVP_aes_128_cbc*/
  22021. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  22022. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  22023. { /* evp_cipher test: EVP_aes_256_ecb*/
  22024. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22025. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  22026. #else
  22027. EVP_CIPHER_CTX ctx[1];
  22028. #endif
  22029. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  22030. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  22031. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  22032. };
  22033. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  22034. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  22035. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  22036. };
  22037. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  22038. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  22039. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  22040. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  22041. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  22042. };
  22043. byte cipher[AES_BLOCK_SIZE * 4];
  22044. byte plain [AES_BLOCK_SIZE * 4];
  22045. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22046. if (ctx == NULL)
  22047. return MEMORY_E;
  22048. #endif
  22049. EVP_CIPHER_CTX_init(ctx);
  22050. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1);
  22051. if (ret == WOLFSSL_SUCCESS)
  22052. ret = EVP_Cipher(ctx, cipher, (byte*)msg, 16);
  22053. EVP_CIPHER_CTX_cleanup(ctx);
  22054. if (ret != 16)
  22055. return WC_TEST_RET_ENC_NC;
  22056. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  22057. return WC_TEST_RET_ENC_NC;
  22058. EVP_CIPHER_CTX_init(ctx);
  22059. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0);
  22060. if (ret == WOLFSSL_SUCCESS)
  22061. ret = EVP_Cipher(ctx, plain, cipher, 16);
  22062. EVP_CIPHER_CTX_cleanup(ctx);
  22063. if (ret != 16)
  22064. return WC_TEST_RET_ENC_NC;
  22065. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  22066. return WC_TEST_RET_ENC_NC;
  22067. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22068. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  22069. #endif
  22070. } /* end evp_cipher test */
  22071. #endif /* HAVE_AES_ECB && WOLFSSL_AES_128 */
  22072. #ifndef WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API
  22073. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  22074. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  22075. {
  22076. /* Test: AES_encrypt/decrypt/set Key */
  22077. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22078. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  22079. #ifdef HAVE_AES_DECRYPT
  22080. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  22081. #endif
  22082. #else
  22083. AES_KEY enc[1];
  22084. #ifdef HAVE_AES_DECRYPT
  22085. AES_KEY dec[1];
  22086. #endif
  22087. #endif
  22088. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  22089. {
  22090. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  22091. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  22092. };
  22093. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  22094. {
  22095. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  22096. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  22097. };
  22098. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  22099. {
  22100. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  22101. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  22102. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  22103. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  22104. };
  22105. byte plain[sizeof(msg)];
  22106. byte cipher[sizeof(msg)];
  22107. printf("openSSL extra test\n") ;
  22108. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22109. if (enc == NULL)
  22110. return MEMORY_E;
  22111. #ifdef HAVE_AES_DECRYPT
  22112. if (dec == NULL)
  22113. return MEMORY_E;
  22114. #endif
  22115. #endif
  22116. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  22117. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  22118. AES_encrypt(msg, cipher, enc);
  22119. #ifdef HAVE_AES_DECRYPT
  22120. AES_decrypt(cipher, plain, dec);
  22121. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  22122. return WC_TEST_RET_ENC_NC;
  22123. #endif /* HAVE_AES_DECRYPT */
  22124. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  22125. return WC_TEST_RET_ENC_NC;
  22126. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22127. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  22128. #ifdef HAVE_AES_DECRYPT
  22129. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  22130. #endif
  22131. #endif
  22132. }
  22133. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  22134. #endif /* !WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API */
  22135. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  22136. #ifdef WOLFSSL_AES_COUNTER
  22137. {
  22138. byte plainBuff [64];
  22139. byte cipherBuff[64];
  22140. #ifdef WOLFSSL_AES_128
  22141. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  22142. {
  22143. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  22144. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  22145. };
  22146. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  22147. {
  22148. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  22149. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  22150. };
  22151. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  22152. {
  22153. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  22154. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  22155. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  22156. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  22157. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  22158. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  22159. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  22160. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  22161. };
  22162. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  22163. {
  22164. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  22165. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  22166. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  22167. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  22168. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  22169. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  22170. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  22171. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  22172. };
  22173. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  22174. {
  22175. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  22176. 0xc2
  22177. };
  22178. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22179. EVP_CIPHER_CTX *p_en;
  22180. EVP_CIPHER_CTX *p_de;
  22181. #endif
  22182. #endif /* WOLFSSL_AES_128 */
  22183. #ifdef WOLFSSL_AES_192
  22184. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  22185. * NIST Special Publication 800-38A */
  22186. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  22187. {
  22188. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  22189. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  22190. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  22191. };
  22192. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  22193. {
  22194. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  22195. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  22196. };
  22197. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  22198. {
  22199. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  22200. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  22201. };
  22202. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  22203. {
  22204. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  22205. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  22206. };
  22207. #endif /* WOLFSSL_AES_192 */
  22208. #ifdef WOLFSSL_AES_256
  22209. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  22210. * NIST Special Publication 800-38A */
  22211. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  22212. {
  22213. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  22214. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  22215. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  22216. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  22217. };
  22218. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  22219. {
  22220. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  22221. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  22222. };
  22223. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  22224. {
  22225. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  22226. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  22227. };
  22228. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  22229. {
  22230. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  22231. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  22232. };
  22233. #endif /* WOLFSSL_AES_256 */
  22234. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22235. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  22236. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  22237. if ((en == NULL) || (de == NULL))
  22238. return MEMORY_E;
  22239. #else
  22240. EVP_CIPHER_CTX en[1];
  22241. EVP_CIPHER_CTX de[1];
  22242. #endif
  22243. #ifdef WOLFSSL_AES_128
  22244. EVP_CIPHER_CTX_init(en);
  22245. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  22246. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  22247. return WC_TEST_RET_ENC_NC;
  22248. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  22249. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  22250. return WC_TEST_RET_ENC_NC;
  22251. EVP_CIPHER_CTX_init(de);
  22252. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  22253. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  22254. return WC_TEST_RET_ENC_NC;
  22255. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  22256. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  22257. return WC_TEST_RET_ENC_NC;
  22258. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  22259. return WC_TEST_RET_ENC_NC;
  22260. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  22261. return WC_TEST_RET_ENC_NC;
  22262. EVP_CIPHER_CTX_cleanup(en);
  22263. EVP_CIPHER_CTX_cleanup(de);
  22264. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22265. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  22266. if (p_en == NULL)
  22267. return WC_TEST_RET_ENC_ERRNO;
  22268. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  22269. if (p_de == NULL)
  22270. return WC_TEST_RET_ENC_ERRNO;
  22271. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  22272. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  22273. return WC_TEST_RET_ENC_NC;
  22274. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  22275. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  22276. return WC_TEST_RET_ENC_NC;
  22277. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  22278. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  22279. return WC_TEST_RET_ENC_NC;
  22280. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  22281. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  22282. return WC_TEST_RET_ENC_NC;
  22283. EVP_CIPHER_CTX_cleanup(p_en);
  22284. EVP_CIPHER_CTX_cleanup(p_de);
  22285. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  22286. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  22287. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  22288. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  22289. return WC_TEST_RET_ENC_NC;
  22290. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  22291. return WC_TEST_RET_ENC_NC;
  22292. EVP_CIPHER_CTX_init(en);
  22293. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  22294. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  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. EVP_CIPHER_CTX_init(de);
  22299. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  22300. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  22301. return WC_TEST_RET_ENC_NC;
  22302. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  22303. return WC_TEST_RET_ENC_NC;
  22304. if (XMEMCMP(plainBuff, ctrPlain, 9))
  22305. return WC_TEST_RET_ENC_NC;
  22306. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  22307. return WC_TEST_RET_ENC_NC;
  22308. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  22309. return WC_TEST_RET_ENC_NC;
  22310. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  22311. return WC_TEST_RET_ENC_NC;
  22312. if (XMEMCMP(plainBuff, ctrPlain, 9))
  22313. return WC_TEST_RET_ENC_NC;
  22314. if (XMEMCMP(cipherBuff, oddCipher, 9))
  22315. return WC_TEST_RET_ENC_NC;
  22316. EVP_CIPHER_CTX_cleanup(en);
  22317. EVP_CIPHER_CTX_cleanup(de);
  22318. #endif /* WOLFSSL_AES_128 */
  22319. #ifdef WOLFSSL_AES_192
  22320. EVP_CIPHER_CTX_init(en);
  22321. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  22322. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  22323. return WC_TEST_RET_ENC_NC;
  22324. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  22325. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  22326. return WC_TEST_RET_ENC_NC;
  22327. EVP_CIPHER_CTX_init(de);
  22328. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  22329. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  22330. return WC_TEST_RET_ENC_NC;
  22331. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  22332. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  22333. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  22334. return WC_TEST_RET_ENC_NC;
  22335. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  22336. return WC_TEST_RET_ENC_NC;
  22337. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  22338. return WC_TEST_RET_ENC_NC;
  22339. EVP_CIPHER_CTX_cleanup(en);
  22340. EVP_CIPHER_CTX_cleanup(de);
  22341. #endif /* WOLFSSL_AES_192 */
  22342. #ifdef WOLFSSL_AES_256
  22343. EVP_CIPHER_CTX_init(en);
  22344. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  22345. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  22346. return WC_TEST_RET_ENC_NC;
  22347. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  22348. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  22349. return WC_TEST_RET_ENC_NC;
  22350. EVP_CIPHER_CTX_init(de);
  22351. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  22352. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  22353. return WC_TEST_RET_ENC_NC;
  22354. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  22355. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  22356. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  22357. return WC_TEST_RET_ENC_NC;
  22358. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  22359. return WC_TEST_RET_ENC_NC;
  22360. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  22361. return WC_TEST_RET_ENC_NC;
  22362. EVP_CIPHER_CTX_cleanup(en);
  22363. EVP_CIPHER_CTX_cleanup(de);
  22364. #endif /* WOLFSSL_AES_256 */
  22365. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22366. wolfSSL_EVP_CIPHER_CTX_free(en);
  22367. wolfSSL_EVP_CIPHER_CTX_free(de);
  22368. #endif
  22369. }
  22370. #endif /* HAVE_AES_COUNTER */
  22371. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  22372. {
  22373. /* EVP_CipherUpdate test */
  22374. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  22375. {
  22376. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  22377. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  22378. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  22379. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  22380. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  22381. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  22382. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  22383. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  22384. };
  22385. byte key[] = "0123456789abcdef "; /* align */
  22386. byte iv[] = "1234567890abcdef "; /* align */
  22387. byte cipher[AES_BLOCK_SIZE * 4];
  22388. byte plain [AES_BLOCK_SIZE * 4];
  22389. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22390. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  22391. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  22392. #else
  22393. EVP_CIPHER_CTX en[1];
  22394. EVP_CIPHER_CTX de[1];
  22395. #endif
  22396. int outlen ;
  22397. int total = 0;
  22398. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22399. if ((en == NULL) || (de == NULL))
  22400. return MEMORY_E;
  22401. #endif
  22402. EVP_CIPHER_CTX_init(en);
  22403. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  22404. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  22405. return WC_TEST_RET_ENC_NC;
  22406. /* openSSL compatibility, if(inlen == 0)return 1; */
  22407. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  22408. (byte*)cbcPlain, 0) != 1)
  22409. return WC_TEST_RET_ENC_NC;
  22410. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  22411. return WC_TEST_RET_ENC_NC;
  22412. EVP_CIPHER_CTX_init(en);
  22413. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  22414. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  22415. return WC_TEST_RET_ENC_NC;
  22416. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  22417. (byte*)cbcPlain, 9) == 0)
  22418. return WC_TEST_RET_ENC_NC;
  22419. if(outlen != 0)
  22420. return WC_TEST_RET_ENC_NC;
  22421. total += outlen;
  22422. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  22423. (byte*)&cbcPlain[9] , 9) == 0)
  22424. return WC_TEST_RET_ENC_NC;
  22425. if(outlen != 16)
  22426. return WC_TEST_RET_ENC_NC;
  22427. total += outlen;
  22428. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  22429. return WC_TEST_RET_ENC_NC;
  22430. if(outlen != 16)
  22431. return WC_TEST_RET_ENC_NC;
  22432. total += outlen;
  22433. if(total != 32)
  22434. return WC_TEST_RET_ENC_NC;
  22435. total = 0;
  22436. EVP_CIPHER_CTX_init(de);
  22437. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  22438. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  22439. return WC_TEST_RET_ENC_NC;
  22440. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  22441. return WC_TEST_RET_ENC_NC;
  22442. if(outlen != 0)
  22443. return WC_TEST_RET_ENC_NC;
  22444. total += outlen;
  22445. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  22446. (byte*)&cipher[6], 12) == 0)
  22447. return WC_TEST_RET_ENC_NC;
  22448. if(outlen != 0)
  22449. total += outlen;
  22450. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  22451. (byte*)&cipher[6+12], 14) == 0)
  22452. return WC_TEST_RET_ENC_NC;
  22453. if(outlen != 16)
  22454. return WC_TEST_RET_ENC_NC;
  22455. total += outlen;
  22456. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  22457. return WC_TEST_RET_ENC_NC;
  22458. if(outlen != 2)
  22459. return WC_TEST_RET_ENC_NC;
  22460. total += outlen;
  22461. if(total != 18)
  22462. return WC_TEST_RET_ENC_NC;
  22463. if (XMEMCMP(plain, cbcPlain, 18))
  22464. return WC_TEST_RET_ENC_NC;
  22465. total = 0;
  22466. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  22467. return WC_TEST_RET_ENC_NC;
  22468. EVP_CIPHER_CTX_init(en);
  22469. if (EVP_EncryptInit(en, EVP_aes_128_cbc(),
  22470. (unsigned char*)key, (unsigned char*)iv) == 0)
  22471. return WC_TEST_RET_ENC_NC;
  22472. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen, (byte*)cbcPlain, 9) == 0)
  22473. return WC_TEST_RET_ENC_NC;
  22474. if(outlen != 0)
  22475. return WC_TEST_RET_ENC_NC;
  22476. total += outlen;
  22477. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen, (byte*)&cbcPlain[9] , 9) == 0)
  22478. return WC_TEST_RET_ENC_NC;
  22479. if(outlen != 16)
  22480. return WC_TEST_RET_ENC_NC;
  22481. total += outlen;
  22482. if (EVP_EncryptFinal(en, (byte*)&cipher[total], &outlen) == 0)
  22483. return WC_TEST_RET_ENC_NC;
  22484. if(outlen != 16)
  22485. return WC_TEST_RET_ENC_NC;
  22486. total += outlen;
  22487. if(total != 32)
  22488. return 3438;
  22489. total = 0;
  22490. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  22491. return WC_TEST_RET_ENC_NC;
  22492. EVP_CIPHER_CTX_init(de);
  22493. if (EVP_DecryptInit(de, EVP_aes_128_cbc(),
  22494. (unsigned char*)key, (unsigned char*)iv) == 0)
  22495. return WC_TEST_RET_ENC_NC;
  22496. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  22497. return WC_TEST_RET_ENC_NC;
  22498. if(outlen != 0)
  22499. return WC_TEST_RET_ENC_NC;
  22500. total += outlen;
  22501. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6], 12) == 0)
  22502. return WC_TEST_RET_ENC_NC;
  22503. if(outlen != 0)
  22504. total += outlen;
  22505. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6+12], 14) == 0)
  22506. return WC_TEST_RET_ENC_NC;
  22507. if(outlen != 16)
  22508. return WC_TEST_RET_ENC_NC;
  22509. total += outlen;
  22510. if (EVP_DecryptFinal(de, (byte*)&plain[total], &outlen) == 0)
  22511. return WC_TEST_RET_ENC_NC;
  22512. if(outlen != 2)
  22513. return WC_TEST_RET_ENC_NC;
  22514. total += outlen;
  22515. if(total != 18)
  22516. return 3447;
  22517. if (XMEMCMP(plain, cbcPlain, 18))
  22518. return WC_TEST_RET_ENC_NC;
  22519. if (EVP_CIPHER_key_length(NULL) != 0)
  22520. return WC_TEST_RET_ENC_NC;
  22521. if (EVP_CIPHER_key_length(EVP_aes_128_cbc()) != 16)
  22522. return WC_TEST_RET_ENC_NC;
  22523. if (EVP_CIPHER_CTX_mode(NULL) != 0)
  22524. return WC_TEST_RET_ENC_NC;
  22525. if (EVP_CIPHER_CTX_mode(en) != (en->flags & WOLFSSL_EVP_CIPH_MODE))
  22526. return WC_TEST_RET_ENC_NC;
  22527. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  22528. return WC_TEST_RET_ENC_NC;
  22529. EVP_CIPHER_CTX_init(en);
  22530. if (EVP_CipherInit_ex(en, EVP_aes_128_cbc(), NULL,
  22531. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  22532. return WC_TEST_RET_ENC_NC;
  22533. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  22534. return WC_TEST_RET_ENC_NC;
  22535. EVP_CIPHER_CTX_init(en);
  22536. if (EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  22537. (unsigned char*)key, (unsigned char*)iv) == 0)
  22538. return WC_TEST_RET_ENC_NC;
  22539. if (wolfSSL_EVP_EncryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  22540. return WC_TEST_RET_ENC_NC;
  22541. if (wolfSSL_EVP_EncryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  22542. return WC_TEST_RET_ENC_NC;
  22543. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  22544. return WC_TEST_RET_ENC_NC;
  22545. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  22546. return WC_TEST_RET_ENC_NC;
  22547. EVP_CIPHER_CTX_init(de);
  22548. if (EVP_DecryptInit_ex(de, EVP_aes_128_cbc(), NULL,
  22549. (unsigned char*)key, (unsigned char*)iv) == 0)
  22550. return WC_TEST_RET_ENC_NC;
  22551. if (wolfSSL_EVP_DecryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  22552. return WC_TEST_RET_ENC_NC;
  22553. if (wolfSSL_EVP_DecryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  22554. return WC_TEST_RET_ENC_NC;
  22555. if (EVP_CIPHER_CTX_block_size(NULL) != BAD_FUNC_ARG)
  22556. return WC_TEST_RET_ENC_NC;
  22557. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  22558. return WC_TEST_RET_ENC_NC;
  22559. EVP_CIPHER_CTX_init(en);
  22560. EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  22561. (unsigned char*)key, (unsigned char*)iv);
  22562. if (EVP_CIPHER_CTX_block_size(en) != en->block_size)
  22563. return WC_TEST_RET_ENC_NC;
  22564. if (EVP_CIPHER_block_size(NULL) != BAD_FUNC_ARG)
  22565. return WC_TEST_RET_ENC_NC;
  22566. if (EVP_CIPHER_block_size(EVP_aes_128_cbc()) != AES_BLOCK_SIZE)
  22567. return WC_TEST_RET_ENC_NC;
  22568. if (WOLFSSL_EVP_CIPHER_mode(NULL) != 0)
  22569. return WC_TEST_RET_ENC_NC;
  22570. if (EVP_CIPHER_flags(EVP_aes_128_cbc()) != WOLFSSL_EVP_CIPH_CBC_MODE)
  22571. return WC_TEST_RET_ENC_NC;
  22572. EVP_CIPHER_CTX_clear_flags(en, 0xFFFFFFFF);
  22573. EVP_CIPHER_CTX_set_flags(en, 42);
  22574. if (en->flags != 42)
  22575. return WC_TEST_RET_ENC_NC;
  22576. if (EVP_CIPHER_CTX_set_padding(NULL, 0) != BAD_FUNC_ARG)
  22577. return WC_TEST_RET_ENC_NC;
  22578. if (EVP_CIPHER_CTX_set_padding(en, 0) != WOLFSSL_SUCCESS)
  22579. return WC_TEST_RET_ENC_NC;
  22580. if (EVP_CIPHER_CTX_set_padding(en, 1) != WOLFSSL_SUCCESS)
  22581. return WC_TEST_RET_ENC_NC;
  22582. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  22583. return WC_TEST_RET_ENC_NC;
  22584. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  22585. return WC_TEST_RET_ENC_NC;
  22586. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22587. wolfSSL_EVP_CIPHER_CTX_free(en);
  22588. wolfSSL_EVP_CIPHER_CTX_free(de);
  22589. #endif
  22590. }
  22591. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  22592. #endif /* ifndef NO_AES */
  22593. return 0;
  22594. }
  22595. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openSSL_evpMD_test(void)
  22596. {
  22597. wc_test_ret_t ret = 0;
  22598. #if !defined(NO_SHA256) && !defined(NO_SHA)
  22599. WOLFSSL_EVP_MD_CTX* ctx;
  22600. WOLFSSL_EVP_MD_CTX* ctx2;
  22601. WOLFSSL_ENTER("openSSL_evpMD_test");
  22602. ctx = EVP_MD_CTX_create();
  22603. ctx2 = EVP_MD_CTX_create();
  22604. ret = EVP_DigestInit(ctx, EVP_sha256());
  22605. if (ret != SSL_SUCCESS) {
  22606. ret = WC_TEST_RET_ENC_NC;
  22607. goto openSSL_evpMD_test_done;
  22608. }
  22609. ret = EVP_MD_CTX_copy(ctx2, ctx);
  22610. if (ret != SSL_SUCCESS) {
  22611. ret = WC_TEST_RET_ENC_NC;
  22612. goto openSSL_evpMD_test_done;
  22613. }
  22614. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  22615. ret = WC_TEST_RET_ENC_NC;
  22616. goto openSSL_evpMD_test_done;
  22617. }
  22618. ret = EVP_DigestInit(ctx, EVP_sha1());
  22619. if (ret != SSL_SUCCESS) {
  22620. ret = WC_TEST_RET_ENC_NC;
  22621. goto openSSL_evpMD_test_done;
  22622. }
  22623. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  22624. ret = WC_TEST_RET_ENC_NC;
  22625. goto openSSL_evpMD_test_done;
  22626. }
  22627. ret = EVP_MD_CTX_copy_ex(ctx2, ctx);
  22628. if (ret != SSL_SUCCESS) {
  22629. ret = WC_TEST_RET_ENC_NC;
  22630. goto openSSL_evpMD_test_done;
  22631. }
  22632. if (EVP_MD_type(EVP_sha256()) == EVP_MD_CTX_type(ctx2)) {
  22633. ret = WC_TEST_RET_ENC_NC;
  22634. goto openSSL_evpMD_test_done;
  22635. }
  22636. if (EVP_MD_type(EVP_sha1()) != EVP_MD_CTX_type(ctx2)) {
  22637. ret = WC_TEST_RET_ENC_NC;
  22638. goto openSSL_evpMD_test_done;
  22639. }
  22640. if (EVP_DigestInit_ex(ctx, EVP_sha1(), NULL) != SSL_SUCCESS) {
  22641. ret = WC_TEST_RET_ENC_NC;
  22642. goto openSSL_evpMD_test_done;
  22643. }
  22644. if (EVP_add_digest(NULL) != 0) {
  22645. ret = WC_TEST_RET_ENC_NC;
  22646. goto openSSL_evpMD_test_done;
  22647. }
  22648. if (wolfSSL_EVP_add_cipher(NULL) != 0) {
  22649. ret = WC_TEST_RET_ENC_NC;
  22650. goto openSSL_evpMD_test_done;
  22651. }
  22652. ret = 0; /* got to success state without jumping to end with a fail */
  22653. openSSL_evpMD_test_done:
  22654. EVP_MD_CTX_destroy(ctx);
  22655. EVP_MD_CTX_destroy(ctx2);
  22656. #endif /* NO_SHA256 */
  22657. return ret;
  22658. }
  22659. #ifdef DEBUG_SIGN
  22660. static void show(const char *title, const char *p, unsigned int s) {
  22661. char* i;
  22662. printf("%s: ", title);
  22663. for (i = p;
  22664. i < p + s;
  22665. printf("%c", *i), i++);
  22666. printf("\n");
  22667. }
  22668. #else
  22669. #define show(a,b,c) WC_DO_NOTHING
  22670. #endif
  22671. #define FOURK_BUFF 4096
  22672. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey0_test(void)
  22673. {
  22674. wc_test_ret_t ret = 0;
  22675. #if !defined(NO_RSA) && !defined(NO_SHA)
  22676. byte* prvTmp;
  22677. byte* pubTmp;
  22678. int prvBytes;
  22679. int pubBytes;
  22680. RSA *prvRsa = NULL;
  22681. RSA *pubRsa = NULL;
  22682. EVP_PKEY *prvPkey = NULL;
  22683. EVP_PKEY *pubPkey = NULL;
  22684. EVP_PKEY_CTX *enc = NULL;
  22685. EVP_PKEY_CTX *dec = NULL;
  22686. byte in[] = TEST_STRING;
  22687. byte out[256];
  22688. size_t outlen;
  22689. size_t keySz;
  22690. byte plain[256];
  22691. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  22692. XFILE keyFile;
  22693. XFILE keypubFile;
  22694. char cliKey[] = "./certs/client-key.der";
  22695. char cliKeypub[] = "./certs/client-keyPub.der";
  22696. #endif
  22697. WOLFSSL_ENTER("openssl_pkey0_test");
  22698. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22699. if (prvTmp == NULL)
  22700. return WC_TEST_RET_ENC_ERRNO;
  22701. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22702. if (pubTmp == NULL) {
  22703. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22704. return WC_TEST_RET_ENC_NC;
  22705. }
  22706. #ifdef USE_CERT_BUFFERS_1024
  22707. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  22708. prvBytes = sizeof_client_key_der_1024;
  22709. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  22710. pubBytes = sizeof_client_keypub_der_1024;
  22711. #elif defined(USE_CERT_BUFFERS_2048)
  22712. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  22713. prvBytes = sizeof_client_key_der_2048;
  22714. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  22715. pubBytes = sizeof_client_keypub_der_2048;
  22716. #else
  22717. keyFile = XFOPEN(cliKey, "rb");
  22718. if (!keyFile) {
  22719. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22720. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22721. err_sys("can't open ./certs/client-key.der, "
  22722. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  22723. return WC_TEST_RET_ENC_ERRNO;
  22724. }
  22725. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  22726. XFCLOSE(keyFile);
  22727. if (prvBytes == 0) {
  22728. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22729. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22730. return WC_TEST_RET_ENC_ERRNO;
  22731. }
  22732. keypubFile = XFOPEN(cliKeypub, "rb");
  22733. if (!keypubFile) {
  22734. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22735. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22736. err_sys("can't open ./certs/client-cert.der, "
  22737. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  22738. return WC_TEST_RET_ENC_ERRNO;
  22739. }
  22740. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  22741. XFCLOSE(keypubFile);
  22742. if (pubBytes == 0) {
  22743. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22744. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22745. return WC_TEST_RET_ENC_ERRNO;
  22746. }
  22747. #endif /* USE_CERT_BUFFERS */
  22748. prvRsa = wolfSSL_RSA_new();
  22749. pubRsa = wolfSSL_RSA_new();
  22750. if((prvRsa == NULL) || (pubRsa == NULL)){
  22751. printf("error with RSA_new\n");
  22752. ret = WC_TEST_RET_ENC_ERRNO;
  22753. goto openssl_pkey0_test_done;
  22754. }
  22755. ret = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  22756. if(ret != SSL_SUCCESS){
  22757. printf("error with RSA_LoadDer_ex\n");
  22758. ret = WC_TEST_RET_ENC_EC(ret);
  22759. goto openssl_pkey0_test_done;
  22760. }
  22761. ret = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  22762. if(ret != SSL_SUCCESS){
  22763. printf("error with RSA_LoadDer_ex\n");
  22764. ret = WC_TEST_RET_ENC_EC(ret);
  22765. goto openssl_pkey0_test_done;
  22766. }
  22767. keySz = (size_t)RSA_size(pubRsa);
  22768. prvPkey = wolfSSL_EVP_PKEY_new();
  22769. pubPkey = wolfSSL_EVP_PKEY_new();
  22770. if((prvPkey == NULL) || (pubPkey == NULL)){
  22771. printf("error with PKEY_new\n");
  22772. ret = WC_TEST_RET_ENC_NC;
  22773. goto openssl_pkey0_test_done;
  22774. }
  22775. ret = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  22776. ret += wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  22777. if(ret != 2){
  22778. printf("error with PKEY_set1_RSA\n");
  22779. ret = WC_TEST_RET_ENC_I(ret);
  22780. goto openssl_pkey0_test_done;
  22781. }
  22782. dec = EVP_PKEY_CTX_new(prvPkey, NULL);
  22783. enc = EVP_PKEY_CTX_new(pubPkey, NULL);
  22784. if((dec == NULL)||(enc==NULL)){
  22785. printf("error with EVP_PKEY_CTX_new\n");
  22786. ret = WC_TEST_RET_ENC_NC;
  22787. goto openssl_pkey0_test_done;
  22788. }
  22789. ret = EVP_PKEY_decrypt_init(dec);
  22790. if (ret != 1) {
  22791. printf("error with decrypt init\n");
  22792. ret = WC_TEST_RET_ENC_NC;
  22793. goto openssl_pkey0_test_done;
  22794. }
  22795. ret = EVP_PKEY_encrypt_init(enc);
  22796. if (ret != 1) {
  22797. printf("error with encrypt init\n");
  22798. ret = WC_TEST_RET_ENC_NC;
  22799. goto openssl_pkey0_test_done;
  22800. }
  22801. XMEMSET(out, 0, sizeof(out));
  22802. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  22803. if (ret != 1) {
  22804. printf("error encrypting msg\n");
  22805. ret = WC_TEST_RET_ENC_NC;
  22806. goto openssl_pkey0_test_done;
  22807. }
  22808. show("encrypted msg", out, outlen);
  22809. XMEMSET(plain, 0, sizeof(plain));
  22810. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  22811. if (ret != 1) {
  22812. printf("error decrypting msg\n");
  22813. ret = WC_TEST_RET_ENC_NC;
  22814. goto openssl_pkey0_test_done;
  22815. }
  22816. show("decrypted msg", plain, outlen);
  22817. /* RSA_PKCS1_OAEP_PADDING test */
  22818. ret = EVP_PKEY_decrypt_init(dec);
  22819. if (ret != 1) {
  22820. printf("error with decrypt init\n");
  22821. ret = WC_TEST_RET_ENC_NC;
  22822. goto openssl_pkey0_test_done;
  22823. }
  22824. ret = EVP_PKEY_encrypt_init(enc);
  22825. if (ret != 1) {
  22826. printf("error with encrypt init\n");
  22827. ret = WC_TEST_RET_ENC_NC;
  22828. goto openssl_pkey0_test_done;
  22829. }
  22830. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  22831. printf("first set rsa padding error\n");
  22832. ret = WC_TEST_RET_ENC_EC(ret);
  22833. goto openssl_pkey0_test_done;
  22834. }
  22835. #ifndef HAVE_FIPS
  22836. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  22837. printf("second set rsa padding error\n");
  22838. ret = WC_TEST_RET_ENC_EC(ret);
  22839. goto openssl_pkey0_test_done;
  22840. }
  22841. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  22842. printf("third set rsa padding error\n");
  22843. ret = WC_TEST_RET_ENC_EC(ret);
  22844. goto openssl_pkey0_test_done;
  22845. }
  22846. #endif
  22847. XMEMSET(out, 0, sizeof(out));
  22848. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  22849. if (ret != 1) {
  22850. printf("error encrypting msg\n");
  22851. ret = WC_TEST_RET_ENC_NC;
  22852. goto openssl_pkey0_test_done;
  22853. }
  22854. show("encrypted msg", out, outlen);
  22855. XMEMSET(plain, 0, sizeof(plain));
  22856. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  22857. if (ret != 1) {
  22858. printf("error decrypting msg\n");
  22859. ret = WC_TEST_RET_ENC_NC;
  22860. goto openssl_pkey0_test_done;
  22861. }
  22862. show("decrypted msg", plain, outlen);
  22863. ret = 0; /* made it to this point without error then set success */
  22864. openssl_pkey0_test_done:
  22865. wolfSSL_RSA_free(prvRsa);
  22866. wolfSSL_RSA_free(pubRsa);
  22867. EVP_PKEY_free(pubPkey);
  22868. EVP_PKEY_free(prvPkey);
  22869. EVP_PKEY_CTX_free(dec);
  22870. EVP_PKEY_CTX_free(enc);
  22871. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22872. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22873. #endif /* NO_RSA */
  22874. return ret;
  22875. }
  22876. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey1_test(void)
  22877. {
  22878. wc_test_ret_t ret = 0;
  22879. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_SHA)
  22880. EVP_PKEY_CTX* dec = NULL;
  22881. EVP_PKEY_CTX* enc = NULL;
  22882. EVP_PKEY* pubKey = NULL;
  22883. EVP_PKEY* prvKey = NULL;
  22884. X509* x509 = NULL;
  22885. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sugar slapped";
  22886. const unsigned char* clikey;
  22887. long cliKeySz;
  22888. size_t outlen;
  22889. int keyLenBits = 2048;
  22890. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22891. unsigned char *tmp = (unsigned char *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22892. unsigned char *cipher = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22893. unsigned char *plain = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22894. if ((tmp == NULL) ||
  22895. (cipher == NULL) ||
  22896. (plain == NULL)) {
  22897. ret = WC_TEST_RET_ENC_NC;
  22898. goto openssl_pkey1_test_done;
  22899. }
  22900. #else
  22901. unsigned char tmp[FOURK_BUF];
  22902. unsigned char cipher[RSA_TEST_BYTES];
  22903. unsigned char plain[RSA_TEST_BYTES];
  22904. #endif
  22905. WOLFSSL_ENTER("openssl_pkey1_test");
  22906. #if defined(USE_CERT_BUFFERS_1024)
  22907. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  22908. cliKeySz = (long)sizeof_client_key_der_1024;
  22909. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_1024,
  22910. sizeof_client_cert_der_1024, SSL_FILETYPE_ASN1);
  22911. keyLenBits = 1024;
  22912. #elif defined(USE_CERT_BUFFERS_2048)
  22913. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  22914. cliKeySz = (long)sizeof_client_key_der_2048;
  22915. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_2048,
  22916. sizeof_client_cert_der_2048, SSL_FILETYPE_ASN1);
  22917. #elif defined(USE_CERT_BUFFERS_3072)
  22918. XMEMCPY(tmp, client_key_der_3072, sizeof_client_key_der_3072);
  22919. cliKeySz = (long)sizeof_client_key_der_3072;
  22920. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_3072,
  22921. sizeof_client_cert_der_3072, SSL_FILETYPE_ASN1);
  22922. keyLenBits = 3072;
  22923. #elif defined(USE_CERT_BUFFERS_4096)
  22924. XMEMCPY(tmp, client_key_der_4096, sizeof_client_key_der_4096);
  22925. cliKeySz = (long)sizeof_client_key_der_4096;
  22926. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_4096,
  22927. sizeof_client_cert_der_4096, SSL_FILETYPE_ASN1);
  22928. keyLenBits = 4096;
  22929. #else
  22930. {
  22931. XFILE f;
  22932. f = XFOPEN(clientKey, "rb");
  22933. if (!f) {
  22934. err_sys("can't open ./certs/client-key.der, "
  22935. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  22936. ret = WC_TEST_RET_ENC_ERRNO;
  22937. goto openssl_pkey1_test_done;
  22938. }
  22939. cliKeySz = (long)XFREAD(tmp, 1, FOURK_BUF, f);
  22940. XFCLOSE(f);
  22941. if (cliKeySz == 0)
  22942. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, openssl_pkey1_test_done);
  22943. }
  22944. /* using existing wolfSSL api to get public and private key */
  22945. x509 = wolfSSL_X509_load_certificate_file(clientCert, SSL_FILETYPE_ASN1);
  22946. #endif /* USE_CERT_BUFFERS */
  22947. clikey = tmp;
  22948. if ((prvKey = EVP_PKEY_new()) == NULL) {
  22949. ret = WC_TEST_RET_ENC_ERRNO;
  22950. goto openssl_pkey1_test_done;
  22951. }
  22952. EVP_PKEY_free(prvKey);
  22953. prvKey = NULL;
  22954. if (x509 == NULL) {
  22955. ret = WC_TEST_RET_ENC_NC;
  22956. goto openssl_pkey1_test_done;
  22957. }
  22958. pubKey = X509_get_pubkey(x509);
  22959. if (pubKey == NULL) {
  22960. ret = WC_TEST_RET_ENC_NC;
  22961. goto openssl_pkey1_test_done;
  22962. }
  22963. prvKey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &clikey, cliKeySz);
  22964. if (prvKey == NULL) {
  22965. ret = WC_TEST_RET_ENC_NC;
  22966. goto openssl_pkey1_test_done;
  22967. }
  22968. /* phase 2 API to create EVP_PKEY_CTX and encrypt/decrypt */
  22969. if (EVP_PKEY_bits(prvKey) != keyLenBits) {
  22970. ret = WC_TEST_RET_ENC_NC;
  22971. goto openssl_pkey1_test_done;
  22972. }
  22973. if (EVP_PKEY_size(prvKey) != keyLenBits/8) {
  22974. ret = WC_TEST_RET_ENC_NC;
  22975. goto openssl_pkey1_test_done;
  22976. }
  22977. dec = EVP_PKEY_CTX_new(prvKey, NULL);
  22978. enc = EVP_PKEY_CTX_new(pubKey, NULL);
  22979. if (dec == NULL || enc == NULL) {
  22980. ret = WC_TEST_RET_ENC_ERRNO;
  22981. goto openssl_pkey1_test_done;
  22982. }
  22983. if (EVP_PKEY_decrypt_init(dec) != 1) {
  22984. ret = WC_TEST_RET_ENC_NC;
  22985. goto openssl_pkey1_test_done;
  22986. }
  22987. if (EVP_PKEY_encrypt_init(enc) != 1) {
  22988. ret = WC_TEST_RET_ENC_NC;
  22989. goto openssl_pkey1_test_done;
  22990. }
  22991. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  22992. ret = WC_TEST_RET_ENC_NC;
  22993. goto openssl_pkey1_test_done;
  22994. }
  22995. #ifndef HAVE_FIPS
  22996. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  22997. ret = WC_TEST_RET_ENC_EC(ret);
  22998. goto openssl_pkey1_test_done;
  22999. }
  23000. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  23001. ret = WC_TEST_RET_ENC_EC(ret);
  23002. goto openssl_pkey1_test_done;
  23003. }
  23004. #endif
  23005. XMEMSET(cipher, 0, RSA_TEST_BYTES);
  23006. outlen = (size_t)(keyLenBits/8);
  23007. if (EVP_PKEY_encrypt(enc, cipher, &outlen, msg, sizeof(msg)) < 0) {
  23008. ret = WC_TEST_RET_ENC_EC(ret);
  23009. goto openssl_pkey1_test_done;
  23010. }
  23011. XMEMSET(plain, 0, RSA_TEST_BYTES);
  23012. if (EVP_PKEY_decrypt(dec, plain, &outlen, cipher, outlen) != 1) {
  23013. ret = WC_TEST_RET_ENC_NC;
  23014. goto openssl_pkey1_test_done;
  23015. }
  23016. openssl_pkey1_test_done:
  23017. if (pubKey != NULL) {
  23018. EVP_PKEY_free(pubKey);
  23019. }
  23020. if (prvKey != NULL) {
  23021. EVP_PKEY_free(prvKey);
  23022. }
  23023. if (dec != NULL) {
  23024. EVP_PKEY_CTX_free(dec);
  23025. }
  23026. if (enc != NULL) {
  23027. EVP_PKEY_CTX_free(enc);
  23028. }
  23029. if (x509 != NULL) {
  23030. X509_free(x509);
  23031. }
  23032. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23033. if (tmp != NULL)
  23034. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23035. if (cipher != NULL)
  23036. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23037. if (plain != NULL)
  23038. XFREE(plain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23039. #endif
  23040. #endif
  23041. return ret;
  23042. }
  23043. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_evpSig_test(void)
  23044. {
  23045. #if !defined(NO_RSA) && !defined(NO_SHA)
  23046. byte* prvTmp;
  23047. byte* pubTmp;
  23048. int prvBytes;
  23049. int pubBytes;
  23050. RSA *prvRsa;
  23051. RSA *pubRsa;
  23052. EVP_PKEY *prvPkey;
  23053. EVP_PKEY *pubPkey;
  23054. EVP_MD_CTX* sign;
  23055. EVP_MD_CTX* verf;
  23056. char msg[] = "see spot run";
  23057. unsigned char sig[256];
  23058. unsigned int sigSz;
  23059. const void* pt;
  23060. unsigned int count;
  23061. wc_test_ret_t ret, ret1, ret2;
  23062. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  23063. XFILE keyFile;
  23064. XFILE keypubFile;
  23065. char cliKey[] = "./certs/client-key.der";
  23066. char cliKeypub[] = "./certs/client-keyPub.der";
  23067. #endif
  23068. WOLFSSL_ENTER("openssl_evpSig_test");
  23069. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23070. if (prvTmp == NULL)
  23071. return WC_TEST_RET_ENC_ERRNO;
  23072. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23073. if (pubTmp == NULL) {
  23074. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23075. return WC_TEST_RET_ENC_NC;
  23076. }
  23077. #ifdef USE_CERT_BUFFERS_1024
  23078. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  23079. prvBytes = sizeof_client_key_der_1024;
  23080. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  23081. pubBytes = sizeof_client_keypub_der_1024;
  23082. #elif defined(USE_CERT_BUFFERS_2048)
  23083. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  23084. prvBytes = sizeof_client_key_der_2048;
  23085. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  23086. pubBytes = sizeof_client_keypub_der_2048;
  23087. #else
  23088. keyFile = XFOPEN(cliKey, "rb");
  23089. if (!keyFile) {
  23090. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23091. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23092. err_sys("can't open ./certs/client-key.der, "
  23093. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  23094. return WC_TEST_RET_ENC_ERRNO;
  23095. }
  23096. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  23097. XFCLOSE(keyFile);
  23098. if (prvBytes == 0) {
  23099. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23100. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23101. return WC_TEST_RET_ENC_ERRNO;
  23102. }
  23103. keypubFile = XFOPEN(cliKeypub, "rb");
  23104. if (!keypubFile) {
  23105. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23106. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23107. err_sys("can't open ./certs/client-cert.der, "
  23108. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  23109. return WC_TEST_RET_ENC_ERRNO;
  23110. }
  23111. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  23112. XFCLOSE(keypubFile);
  23113. if (pubBytes == 0) {
  23114. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23115. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23116. return WC_TEST_RET_ENC_ERRNO;
  23117. }
  23118. #endif /* USE_CERT_BUFFERS */
  23119. prvRsa = wolfSSL_RSA_new();
  23120. pubRsa = wolfSSL_RSA_new();
  23121. if((prvRsa == NULL) || (pubRsa == NULL)){
  23122. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23123. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23124. err_sys("ERROR with RSA_new", WC_TEST_RET_ENC_NC);
  23125. return WC_TEST_RET_ENC_NC;
  23126. }
  23127. ret1 = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  23128. ret2 = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  23129. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  23130. printf("error with RSA_LoadDer_ex\n");
  23131. return WC_TEST_RET_ENC_NC;
  23132. }
  23133. prvPkey = wolfSSL_EVP_PKEY_new();
  23134. pubPkey = wolfSSL_EVP_PKEY_new();
  23135. if((prvPkey == NULL) || (pubPkey == NULL)){
  23136. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23137. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23138. printf("error with KEY_new\n");
  23139. return WC_TEST_RET_ENC_NC;
  23140. }
  23141. ret1 = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  23142. ret2 = wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  23143. if((ret1 != 1) || (ret2 != 1)){
  23144. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23145. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23146. printf("error with EVP_PKEY_set1_RSA\n");
  23147. return WC_TEST_RET_ENC_NC;
  23148. }
  23149. /****************** sign and verify *******************/
  23150. sign = EVP_MD_CTX_create();
  23151. verf = EVP_MD_CTX_create();
  23152. if((sign == NULL)||(verf == NULL)){
  23153. printf("error with EVP_MD_CTX_create\n");
  23154. EVP_MD_CTX_destroy(sign);
  23155. EVP_MD_CTX_destroy(verf);
  23156. return WC_TEST_RET_ENC_NC;
  23157. }
  23158. ret = EVP_SignInit(sign, EVP_sha1());
  23159. if (ret != SSL_SUCCESS){
  23160. printf("error with EVP_SignInit\n");
  23161. EVP_MD_CTX_destroy(sign);
  23162. EVP_MD_CTX_destroy(verf);
  23163. return WC_TEST_RET_ENC_NC;
  23164. }
  23165. count = sizeof(msg);
  23166. show("message = ", (char *)msg, count);
  23167. /* sign */
  23168. XMEMSET(sig, 0, sizeof(sig));
  23169. pt = (const void*)msg;
  23170. ret1 = EVP_SignUpdate(sign, pt, count);
  23171. ret2 = EVP_SignFinal(sign, sig, &sigSz, prvPkey);
  23172. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  23173. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23174. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23175. EVP_MD_CTX_destroy(sign);
  23176. EVP_MD_CTX_destroy(verf);
  23177. printf("error with EVP_MD_CTX_create\n");
  23178. return WC_TEST_RET_ENC_NC;
  23179. }
  23180. show("signature = ", (char *)sig, sigSz);
  23181. /* verify */
  23182. pt = (const void*)msg;
  23183. ret1 = EVP_VerifyInit(verf, EVP_sha1());
  23184. ret2 = EVP_VerifyUpdate(verf, pt, count);
  23185. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  23186. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23187. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23188. EVP_MD_CTX_destroy(sign);
  23189. EVP_MD_CTX_destroy(verf);
  23190. printf("error with EVP_Verify\n");
  23191. return WC_TEST_RET_ENC_NC;
  23192. }
  23193. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) != 1) {
  23194. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23195. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23196. EVP_MD_CTX_destroy(sign);
  23197. EVP_MD_CTX_destroy(verf);
  23198. printf("error with EVP_VerifyFinal\n");
  23199. return WC_TEST_RET_ENC_NC;
  23200. }
  23201. /* expect fail without update */
  23202. EVP_VerifyInit(verf, EVP_sha1());
  23203. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) == 1) {
  23204. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23205. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23206. EVP_MD_CTX_destroy(sign);
  23207. EVP_MD_CTX_destroy(verf);
  23208. printf("EVP_VerifyInit without update not detected\n");
  23209. return WC_TEST_RET_ENC_NC;
  23210. }
  23211. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23212. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23213. EVP_MD_CTX_destroy(sign);
  23214. EVP_MD_CTX_destroy(verf);
  23215. wolfSSL_RSA_free(prvRsa);
  23216. wolfSSL_RSA_free(pubRsa);
  23217. EVP_PKEY_free(pubPkey);
  23218. EVP_PKEY_free(prvPkey);
  23219. #endif /* NO_RSA */
  23220. return 0;
  23221. }
  23222. #endif /* OPENSSL_EXTRA */
  23223. #ifndef NO_PWDBASED
  23224. #ifdef HAVE_SCRYPT
  23225. /* Test vectors taken from RFC 7914: scrypt PBKDF - Section 12. */
  23226. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t scrypt_test(void)
  23227. {
  23228. #ifdef HAVE_FIPS
  23229. /* RFC 7914 test vector keys are too short for FIPS. */
  23230. #else
  23231. wc_test_ret_t ret;
  23232. byte derived[64];
  23233. WOLFSSL_SMALL_STACK_STATIC const byte verify1[] = {
  23234. 0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20,
  23235. 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04, 0x97,
  23236. 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8,
  23237. 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2, 0x14, 0x42,
  23238. 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8,
  23239. 0x32, 0x6a, 0x75, 0x3a, 0x0f, 0xc8, 0x1f, 0x17,
  23240. 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28,
  23241. 0xcf, 0x35, 0xe2, 0x0c, 0x38, 0xd1, 0x89, 0x06
  23242. };
  23243. #if !defined(BENCH_EMBEDDED)
  23244. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  23245. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  23246. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  23247. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  23248. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  23249. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  23250. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  23251. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  23252. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  23253. };
  23254. #endif
  23255. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  23256. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  23257. 0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48,
  23258. 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb,
  23259. 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e,
  23260. 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2,
  23261. 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf,
  23262. 0x62, 0xd4, 0x97, 0x05, 0x24, 0x2a, 0x9a, 0xf9,
  23263. 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40,
  23264. 0xdf, 0xcf, 0x01, 0x7b, 0x45, 0x57, 0x58, 0x87
  23265. };
  23266. #endif
  23267. #ifdef SCRYPT_TEST_ALL
  23268. /* Test case is very slow.
  23269. * Use for confirmation after code change or new platform.
  23270. */
  23271. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  23272. 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae,
  23273. 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81,
  23274. 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d,
  23275. 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad, 0xaa, 0x47,
  23276. 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f,
  23277. 0xfa, 0x1c, 0x6d, 0x92, 0x7c, 0x40, 0xf4, 0xc3,
  23278. 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb,
  23279. 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41, 0xa4
  23280. };
  23281. #endif
  23282. WOLFSSL_ENTER("scrypt_test");
  23283. ret = wc_scrypt(derived, NULL, 0, NULL, 0, 4, 1, 1, sizeof(verify1));
  23284. if (ret != 0)
  23285. return WC_TEST_RET_ENC_EC(ret);
  23286. if (XMEMCMP(derived, verify1, sizeof(verify1)) != 0)
  23287. return WC_TEST_RET_ENC_NC;
  23288. #if !defined(BENCH_EMBEDDED)
  23289. ret = wc_scrypt(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 10, 8, 16,
  23290. sizeof(verify2));
  23291. if (ret != 0)
  23292. return WC_TEST_RET_ENC_EC(ret);
  23293. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  23294. return WC_TEST_RET_ENC_NC;
  23295. #endif
  23296. /* Test case with parallel overflowing */
  23297. ret = wc_scrypt(derived, (byte*)"password", 16, (byte*)"NaCl", 16, 2, 4, 8388608,
  23298. sizeof(verify1));
  23299. if (ret != BAD_FUNC_ARG)
  23300. return WC_TEST_RET_ENC_EC(ret);
  23301. /* Don't run these test on embedded, since they use large mallocs */
  23302. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  23303. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  23304. (byte*)"SodiumChloride", 14, 14, 8, 1, sizeof(verify3));
  23305. if (ret != 0)
  23306. return WC_TEST_RET_ENC_EC(ret);
  23307. if (XMEMCMP(derived, verify3, sizeof(verify3)) != 0)
  23308. return WC_TEST_RET_ENC_NC;
  23309. #ifdef SCRYPT_TEST_ALL
  23310. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  23311. (byte*)"SodiumChloride", 14, 20, 8, 1, sizeof(verify4));
  23312. if (ret != 0)
  23313. return WC_TEST_RET_ENC_EC(ret);
  23314. if (XMEMCMP(derived, verify4, sizeof(verify4)) != 0)
  23315. return WC_TEST_RET_ENC_NC;
  23316. #endif
  23317. #else
  23318. #ifdef SCRYPT_TEST_ALL
  23319. (void)verify4;
  23320. #endif
  23321. #endif /* !BENCH_EMBEDDED && !defined(WOLFSSL_LINUXKM) && !HAVE_INTEL_QA */
  23322. #if !defined(BENCH_EMBEDDED)
  23323. ret = wc_scrypt_ex(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 1<<10,
  23324. 8, 16, sizeof(verify2));
  23325. if (ret != 0)
  23326. return WC_TEST_RET_ENC_EC(ret);
  23327. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  23328. return WC_TEST_RET_ENC_NC;
  23329. #endif
  23330. #endif /* !HAVE_FIPS */
  23331. return 0;
  23332. }
  23333. #endif
  23334. #ifdef HAVE_PKCS12
  23335. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_pbkdf_test(void)
  23336. {
  23337. WOLFSSL_SMALL_STACK_STATIC const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
  23338. 0x00, 0x00 };
  23339. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
  23340. WOLFSSL_SMALL_STACK_STATIC const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
  23341. 0x00, 0x67, 0x00, 0x00 };
  23342. WOLFSSL_SMALL_STACK_STATIC const byte salt2[] = { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
  23343. byte derived[64];
  23344. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  23345. 0x27, 0xE9, 0x0D, 0x7E, 0xD5, 0xA1, 0xC4, 0x11,
  23346. 0xBA, 0x87, 0x8B, 0xC0, 0x90, 0xF5, 0xCE, 0xBE,
  23347. 0x5E, 0x9D, 0x5F, 0xE3, 0xD6, 0x2B, 0x73, 0xAA
  23348. };
  23349. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  23350. 0x90, 0x1B, 0x49, 0x70, 0xF0, 0x94, 0xF0, 0xF8,
  23351. 0x45, 0xC0, 0xF3, 0xF3, 0x13, 0x59, 0x18, 0x6A,
  23352. 0x35, 0xE3, 0x67, 0xFE, 0xD3, 0x21, 0xFD, 0x7C
  23353. };
  23354. int id = 1;
  23355. int kLen = 24;
  23356. int iterations = 1;
  23357. wc_test_ret_t ret;
  23358. WOLFSSL_ENTER("pkcs12_pbkdf_test");
  23359. ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
  23360. iterations, kLen, WC_SHA256, id);
  23361. if (ret < 0)
  23362. return WC_TEST_RET_ENC_EC(ret);
  23363. if (XMEMCMP(derived, verify, kLen) != 0)
  23364. return WC_TEST_RET_ENC_NC;
  23365. iterations = 1000;
  23366. ret = wc_PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8,
  23367. iterations, kLen, WC_SHA256, id);
  23368. if (ret < 0)
  23369. return WC_TEST_RET_ENC_EC(ret);
  23370. ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8,
  23371. iterations, kLen, WC_SHA256, id, HEAP_HINT);
  23372. if (ret < 0)
  23373. return WC_TEST_RET_ENC_EC(ret);
  23374. if (XMEMCMP(derived, verify2, 24) != 0)
  23375. return WC_TEST_RET_ENC_NC;
  23376. return 0;
  23377. }
  23378. #endif /* HAVE_PKCS12 */
  23379. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  23380. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf2_test(void)
  23381. {
  23382. char passwd[] = "passwordpassword";
  23383. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a,
  23384. 0x5d, 0x63, 0xcb, 0x06 };
  23385. int iterations = 2048;
  23386. int kLen = 24;
  23387. byte derived[64];
  23388. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  23389. 0x43, 0x6d, 0xb5, 0xe8, 0xd0, 0xfb, 0x3f, 0x35, 0x42, 0x48, 0x39, 0xbc,
  23390. 0x2d, 0xd4, 0xf9, 0x37, 0xd4, 0x95, 0x16, 0xa7, 0x2a, 0x9a, 0x21, 0xd1
  23391. };
  23392. wc_test_ret_t ret;
  23393. WOLFSSL_ENTER("pbkdf2_test");
  23394. ret = wc_PBKDF2_ex(derived, (byte*)passwd, (int)XSTRLEN(passwd),
  23395. salt, (int)sizeof(salt), iterations,
  23396. kLen, WC_SHA256, HEAP_HINT, devId);
  23397. if (ret != 0)
  23398. return WC_TEST_RET_ENC_EC(ret);
  23399. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  23400. return WC_TEST_RET_ENC_NC;
  23401. return 0;
  23402. }
  23403. #endif /* HAVE_PBKDF2 && !NO_SHA256 && !NO_HMAC */
  23404. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  23405. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf1_test(void)
  23406. {
  23407. char passwd[] = "password";
  23408. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  23409. int iterations = 1000;
  23410. int kLen = 16;
  23411. byte derived[16];
  23412. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  23413. 0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F,
  23414. 0xAF, 0x10, 0xEB, 0xFB, 0x4A, 0x3D, 0x2A, 0x20
  23415. };
  23416. wc_test_ret_t ret;
  23417. WOLFSSL_ENTER("pbkdf1_test");
  23418. ret = wc_PBKDF1_ex(derived, kLen, NULL, 0, (byte*)passwd,
  23419. (int)XSTRLEN(passwd), salt, (int)sizeof(salt), iterations, WC_SHA,
  23420. HEAP_HINT);
  23421. if (ret != 0)
  23422. return ret;
  23423. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  23424. return WC_TEST_RET_ENC_NC;
  23425. return 0;
  23426. }
  23427. #endif /* HAVE_PBKDF2 && !NO_SHA */
  23428. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pwdbased_test(void)
  23429. {
  23430. wc_test_ret_t ret = 0;
  23431. WOLFSSL_ENTER("pwdbased_test");
  23432. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  23433. ret = pbkdf1_test();
  23434. if (ret != 0)
  23435. return ret;
  23436. #endif
  23437. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  23438. ret = pbkdf2_test();
  23439. if (ret != 0)
  23440. return ret;
  23441. #endif
  23442. #ifdef HAVE_PKCS12
  23443. ret = pkcs12_pbkdf_test();
  23444. if (ret != 0)
  23445. return ret;
  23446. #endif
  23447. #ifdef HAVE_SCRYPT
  23448. ret = scrypt_test();
  23449. if (ret != 0)
  23450. return ret;
  23451. #endif
  23452. return ret;
  23453. }
  23454. #endif /* NO_PWDBASED */
  23455. #if defined(USE_CERT_BUFFERS_2048) && \
  23456. defined(HAVE_PKCS12) && \
  23457. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_HMAC) && \
  23458. !defined(NO_CERTS) && !defined(NO_DES3)
  23459. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_test(void)
  23460. {
  23461. wc_test_ret_t ret = 0;
  23462. WC_PKCS12* pkcs12 = NULL;
  23463. /* Gen vars */
  23464. byte* pkcs12der = NULL;
  23465. int pkcs12derSz = 0;
  23466. WC_DerCertList derCaList = {
  23467. (byte*)ca_cert_der_2048, sizeof_ca_cert_der_2048, NULL
  23468. };
  23469. char* pass = (char*)"wolfSSL test";
  23470. /* Parsing vars */
  23471. WC_DerCertList* derCaListOut = NULL;
  23472. byte* keyDer = NULL;
  23473. byte* certDer = NULL;
  23474. word32 keySz;
  23475. word32 certSz;
  23476. WOLFSSL_ENTER("pkcs12_test");
  23477. pkcs12 = wc_PKCS12_create(pass, (word32)XSTRLEN(pass),
  23478. (char*)"friendlyName" /* not used currently */,
  23479. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  23480. (byte*)server_cert_der_2048, sizeof_server_cert_der_2048,
  23481. &derCaList, PBE_SHA1_DES3, PBE_SHA1_DES3, 100, 100,
  23482. 0 /* not used currently */, HEAP_HINT);
  23483. if (pkcs12 == NULL) {
  23484. ret = WC_TEST_RET_ENC_EC(MEMORY_E);
  23485. goto out;
  23486. }
  23487. ret = wc_i2d_PKCS12(pkcs12, NULL, &pkcs12derSz);
  23488. if (ret != LENGTH_ONLY_E) {
  23489. if (ret == 0)
  23490. ret = WC_TEST_RET_ENC_NC;
  23491. else
  23492. ret = WC_TEST_RET_ENC_I(ret);
  23493. goto out;
  23494. }
  23495. pkcs12der = (byte*)XMALLOC(pkcs12derSz, HEAP_HINT, DYNAMIC_TYPE_PKCS);
  23496. if (pkcs12der == NULL) {
  23497. ret = WC_TEST_RET_ENC_EC(MEMORY_E);
  23498. goto out;
  23499. }
  23500. {
  23501. /* Use tmp pointer to avoid advancing pkcs12der */
  23502. byte* tmp = pkcs12der;
  23503. ret = wc_i2d_PKCS12(pkcs12, &tmp, &pkcs12derSz);
  23504. if (ret <= 0) {
  23505. if (ret == 0)
  23506. ret = WC_TEST_RET_ENC_NC;
  23507. else
  23508. ret = WC_TEST_RET_ENC_I(ret);
  23509. goto out;
  23510. }
  23511. }
  23512. wc_PKCS12_free(pkcs12);
  23513. pkcs12 = wc_PKCS12_new_ex(HEAP_HINT);
  23514. if (pkcs12 == NULL) {
  23515. ret = WC_TEST_RET_ENC_EC(MEMORY_E);
  23516. goto out;
  23517. }
  23518. /* convert the DER file into an internal structure */
  23519. ret = wc_d2i_PKCS12(pkcs12der, pkcs12derSz, pkcs12);
  23520. if (ret != 0) {
  23521. ret = WC_TEST_RET_ENC_EC(ret);
  23522. goto out;
  23523. }
  23524. /* parse the internal structure into its parts */
  23525. ret = wc_PKCS12_parse(pkcs12, "wolfSSL test", &keyDer, &keySz,
  23526. &certDer, &certSz, &derCaListOut);
  23527. if (ret != 0) {
  23528. ret = WC_TEST_RET_ENC_EC(ret);
  23529. goto out;
  23530. }
  23531. if (keyDer == NULL || certDer == NULL || derCaListOut == NULL) {
  23532. ret = WC_TEST_RET_ENC_NC;
  23533. goto out;
  23534. }
  23535. out:
  23536. if (derCaListOut)
  23537. wc_FreeCertList(derCaListOut, HEAP_HINT);
  23538. XFREE(keyDer, HEAP_HINT, DYNAMIC_TYPE_PKCS);
  23539. XFREE(certDer, HEAP_HINT, DYNAMIC_TYPE_PKCS);
  23540. if (pkcs12)
  23541. wc_PKCS12_free(pkcs12);
  23542. XFREE(pkcs12der, HEAP_HINT, DYNAMIC_TYPE_PKCS);
  23543. return ret;
  23544. }
  23545. #endif
  23546. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  23547. #if defined(WOLFSSL_AFALG_XILINX) || defined(WOLFSSL_AFALG_XILINX_AES) || \
  23548. defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_AFALG_HASH_KEEP) || \
  23549. defined(WOLFSSL_AFALG_XILINX_RSA)
  23550. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  23551. static wc_test_ret_t hkdf_test(void)
  23552. #else
  23553. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hkdf_test(void)
  23554. #endif
  23555. {
  23556. wc_test_ret_t ret = 0;
  23557. #if !defined(NO_SHA) || !defined(NO_SHA256)
  23558. int L;
  23559. byte okm1[42];
  23560. byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  23561. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  23562. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  23563. #ifndef HAVE_FIPS
  23564. byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23565. 0x08, 0x09, 0x0a, 0x0b, 0x0c };
  23566. byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  23567. 0xf8, 0xf9 };
  23568. #endif
  23569. #ifndef NO_SHA
  23570. byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
  23571. 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
  23572. 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
  23573. 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
  23574. 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
  23575. 0x49, 0x18 };
  23576. #ifndef HAVE_FIPS
  23577. byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
  23578. 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
  23579. 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
  23580. 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
  23581. 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
  23582. 0xf8, 0x96 };
  23583. #endif
  23584. #endif /* !NO_SHA */
  23585. #ifndef NO_SHA256
  23586. byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  23587. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  23588. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  23589. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  23590. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  23591. 0x96, 0xc8 };
  23592. #ifndef HAVE_FIPS
  23593. byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
  23594. 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  23595. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  23596. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  23597. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
  23598. 0x58, 0x65 };
  23599. #endif
  23600. #endif /* !NO_SHA256 */
  23601. WOLFSSL_ENTER("hkdf_test");
  23602. XMEMSET(okm1, 0, sizeof(okm1));
  23603. L = (int)sizeof(okm1);
  23604. #ifndef NO_SHA
  23605. ret = wc_HKDF(WC_SHA, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  23606. okm1, (word32)L);
  23607. if (ret != 0)
  23608. return WC_TEST_RET_ENC_EC(ret);
  23609. if (XMEMCMP(okm1, res1, L) != 0)
  23610. return WC_TEST_RET_ENC_NC;
  23611. #ifndef HAVE_FIPS
  23612. /* fips can't have key size under 14 bytes, salt is key too */
  23613. L = (int)sizeof(okm1);
  23614. ret = wc_HKDF(WC_SHA, ikm1, 11, salt1, (word32)sizeof(salt1),
  23615. info1, (word32)sizeof(info1), okm1, (word32)L);
  23616. if (ret != 0)
  23617. return WC_TEST_RET_ENC_EC(ret);
  23618. if (XMEMCMP(okm1, res2, L) != 0)
  23619. return WC_TEST_RET_ENC_NC;
  23620. #endif /* HAVE_FIPS */
  23621. #endif /* !NO_SHA */
  23622. #ifndef NO_SHA256
  23623. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  23624. okm1, (word32)L);
  23625. if (ret != 0)
  23626. return WC_TEST_RET_ENC_EC(ret);
  23627. if (XMEMCMP(okm1, res3, L) != 0)
  23628. return WC_TEST_RET_ENC_NC;
  23629. #ifndef HAVE_FIPS
  23630. /* fips can't have key size under 14 bytes, salt is key too */
  23631. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1),
  23632. salt1, (word32)sizeof(salt1), info1, (word32)sizeof(info1), okm1, (word32)L);
  23633. if (ret != 0)
  23634. return WC_TEST_RET_ENC_EC(ret);
  23635. if (XMEMCMP(okm1, res4, L) != 0)
  23636. return WC_TEST_RET_ENC_NC;
  23637. #endif /* HAVE_FIPS */
  23638. #endif /* !NO_SHA256 */
  23639. #endif /* !NO_SHA || !NO_SHA256 */
  23640. return ret;
  23641. }
  23642. #endif /* HAVE_HKDF */
  23643. #ifdef WOLFSSL_WOLFSSH
  23644. typedef struct {
  23645. byte hashId;
  23646. byte keyId;
  23647. const byte* k;
  23648. word32 kSz;
  23649. const byte* h;
  23650. word32 hSz;
  23651. const byte* sessionId;
  23652. word32 sessionIdSz;
  23653. const byte* expectedKey;
  23654. word32 expectedKeySz;
  23655. } SshKdfTestVector;
  23656. /** Test Vector Set #3: SHA-256 **/
  23657. static const byte sshKdfTvSet3k[] = {
  23658. 0x6A, 0xC3, 0x82, 0xEA, 0xAC, 0xA0, 0x93, 0xE1,
  23659. 0x25, 0xE2, 0x5C, 0x24, 0xBE, 0xBC, 0x84, 0x64,
  23660. 0x0C, 0x11, 0x98, 0x75, 0x07, 0x34, 0x4B, 0x5C,
  23661. 0x73, 0x9C, 0xEB, 0x84, 0xA9, 0xE0, 0xB2, 0x22,
  23662. 0xB9, 0xA8, 0xB5, 0x1C, 0x83, 0x9E, 0x5E, 0xBE,
  23663. 0x49, 0xCF, 0xAD, 0xBF, 0xB3, 0x95, 0x99, 0x76,
  23664. 0x4E, 0xD5, 0x22, 0x09, 0x9D, 0xC9, 0x12, 0x75,
  23665. 0x19, 0x50, 0xDC, 0x7D, 0xC9, 0x7F, 0xBD, 0xC0,
  23666. 0x63, 0x28, 0xB6, 0x8F, 0x22, 0x78, 0x1F, 0xD3,
  23667. 0x15, 0xAF, 0x56, 0x80, 0x09, 0xA5, 0x50, 0x9E,
  23668. 0x5B, 0x87, 0xA1, 0x1B, 0xF5, 0x27, 0xC0, 0x56,
  23669. 0xDA, 0xFF, 0xD8, 0x2A, 0xB6, 0xCB, 0xC2, 0x5C,
  23670. 0xCA, 0x37, 0x14, 0x34, 0x59, 0xE7, 0xBC, 0x63,
  23671. 0xBC, 0xDE, 0x52, 0x75, 0x7A, 0xDE, 0xB7, 0xDF,
  23672. 0x01, 0xCF, 0x12, 0x17, 0x3F, 0x1F, 0xEF, 0x81,
  23673. 0x02, 0xEC, 0x5A, 0xB1, 0x42, 0xC2, 0x13, 0xDD,
  23674. 0x9D, 0x30, 0x69, 0x62, 0x78, 0xA8, 0xD8, 0xBC,
  23675. 0x32, 0xDD, 0xE9, 0x59, 0x2D, 0x28, 0xC0, 0x78,
  23676. 0xC6, 0xD9, 0x2B, 0x94, 0x7D, 0x82, 0x5A, 0xCA,
  23677. 0xAB, 0x64, 0x94, 0x84, 0x6A, 0x49, 0xDE, 0x24,
  23678. 0xB9, 0x62, 0x3F, 0x48, 0x89, 0xE8, 0xAD, 0xC3,
  23679. 0x8E, 0x8C, 0x66, 0x9E, 0xFF, 0xEF, 0x17, 0x60,
  23680. 0x40, 0xAD, 0x94, 0x5E, 0x90, 0xA7, 0xD3, 0xEE,
  23681. 0xC1, 0x5E, 0xFE, 0xEE, 0x78, 0xAE, 0x71, 0x04,
  23682. 0x3C, 0x96, 0x51, 0x11, 0x03, 0xA1, 0x6B, 0xA7,
  23683. 0xCA, 0xF0, 0xAC, 0xD0, 0x64, 0x2E, 0xFD, 0xBE,
  23684. 0x80, 0x99, 0x34, 0xFA, 0xA1, 0xA5, 0xF1, 0xBD,
  23685. 0x11, 0x04, 0x36, 0x49, 0xB2, 0x5C, 0xCD, 0x1F,
  23686. 0xEE, 0x2E, 0x38, 0x81, 0x5D, 0x4D, 0x5F, 0x5F,
  23687. 0xC6, 0xB4, 0x10, 0x29, 0x69, 0xF2, 0x1C, 0x22,
  23688. 0xAE, 0x1B, 0x0E, 0x7D, 0x36, 0x03, 0xA5, 0x56,
  23689. 0xA1, 0x32, 0x62, 0xFF, 0x62, 0x8D, 0xE2, 0x22
  23690. };
  23691. static const byte sshKdfTvSet3h[] = {
  23692. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  23693. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  23694. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  23695. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  23696. };
  23697. static const byte sshKdfTvSet3sid[] = {
  23698. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  23699. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  23700. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  23701. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  23702. };
  23703. static const byte sshKdfTvSet3a[] = {
  23704. 0x81, 0xF0, 0x33, 0x0E, 0xF6, 0xF0, 0x53, 0x61,
  23705. 0xB3, 0x82, 0x3B, 0xFD, 0xED, 0x6E, 0x1D, 0xE9
  23706. };
  23707. static const byte sshKdfTvSet3b[] = {
  23708. 0x3F, 0x6F, 0xD2, 0x06, 0x5E, 0xEB, 0x2B, 0x0B,
  23709. 0x1D, 0x93, 0x19, 0x5A, 0x1F, 0xED, 0x48, 0xA5
  23710. };
  23711. static const byte sshKdfTvSet3c[] = {
  23712. 0xC3, 0x54, 0x71, 0x03, 0x4E, 0x6F, 0xD6, 0x54,
  23713. 0x76, 0x13, 0x17, 0x8E, 0x23, 0x43, 0x5F, 0x21
  23714. };
  23715. static const byte sshKdfTvSet3d[] = {
  23716. 0x7E, 0x9D, 0x79, 0x03, 0x20, 0x90, 0xD9, 0x9F,
  23717. 0x98, 0xB0, 0x15, 0x63, 0x4D, 0xD9, 0xF4, 0x62
  23718. };
  23719. static const byte sshKdfTvSet3e[] = {
  23720. 0x24, 0xEE, 0x55, 0x9A, 0xD7, 0xCE, 0x71, 0x2B,
  23721. 0x68, 0x5D, 0x0B, 0x22, 0x71, 0xE4, 0x43, 0xC1,
  23722. 0x7A, 0xB1, 0xD1, 0xDC, 0xEB, 0x5A, 0x36, 0x05,
  23723. 0x69, 0xD2, 0x5D, 0x5D, 0xC2, 0x43, 0x00, 0x2F
  23724. };
  23725. static const byte sshKdfTvSet3f[] = {
  23726. 0xC3, 0x41, 0x9C, 0x2B, 0x96, 0x62, 0x35, 0x86,
  23727. 0x9D, 0x71, 0x4B, 0xA5, 0xAC, 0x48, 0xDD, 0xB7,
  23728. 0xD9, 0xE3, 0x5C, 0x8C, 0x19, 0xAA, 0xC7, 0x34,
  23729. 0x22, 0x33, 0x7A, 0x37, 0x34, 0x53, 0x60, 0x7E
  23730. };
  23731. static const SshKdfTestVector sshKdfTestVectors[] = {
  23732. {WC_HASH_TYPE_SHA256, 'A',
  23733. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  23734. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  23735. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  23736. sshKdfTvSet3a, sizeof(sshKdfTvSet3a)},
  23737. {WC_HASH_TYPE_SHA256, 'B',
  23738. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  23739. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  23740. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  23741. sshKdfTvSet3b, sizeof(sshKdfTvSet3b)},
  23742. {WC_HASH_TYPE_SHA256, 'C',
  23743. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  23744. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  23745. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  23746. sshKdfTvSet3c, sizeof(sshKdfTvSet3c)},
  23747. {WC_HASH_TYPE_SHA256, 'D',
  23748. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  23749. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  23750. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  23751. sshKdfTvSet3d, sizeof(sshKdfTvSet3d)},
  23752. {WC_HASH_TYPE_SHA256, 'E',
  23753. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  23754. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  23755. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  23756. sshKdfTvSet3e, sizeof(sshKdfTvSet3e)},
  23757. {WC_HASH_TYPE_SHA256, 'F',
  23758. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  23759. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  23760. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  23761. sshKdfTvSet3f, sizeof(sshKdfTvSet3f)},
  23762. };
  23763. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sshkdf_test(void)
  23764. {
  23765. byte cKey[32]; /* Greater of SHA256_DIGEST_SIZE and AES_BLOCK_SIZE */
  23766. word32 i;
  23767. word32 tc = sizeof(sshKdfTestVectors)/sizeof(SshKdfTestVector);
  23768. const SshKdfTestVector* tv = NULL;
  23769. wc_test_ret_t result = 0;
  23770. WOLFSSL_ENTER("sshkdf_test");
  23771. /* sId - Session ID, eKey - Expected Key, cKey - Calculated Key */
  23772. for (i = 0, tv = sshKdfTestVectors; i < tc; i++, tv++) {
  23773. result = wc_SSH_KDF(tv->hashId, tv->keyId,
  23774. cKey, tv->expectedKeySz,
  23775. tv->k, tv->kSz, tv->h, tv->hSz,
  23776. tv->sessionId, tv->sessionIdSz);
  23777. if (result != 0) {
  23778. printf("KDF: Could not derive key.\n");
  23779. result = WC_TEST_RET_ENC_EC(result);
  23780. }
  23781. else {
  23782. if (XMEMCMP(cKey, tv->expectedKey, tv->expectedKeySz) != 0) {
  23783. printf("KDF: Calculated Key does not match Expected Key.\n");
  23784. result = WC_TEST_RET_ENC_EC(result);
  23785. }
  23786. }
  23787. if (result != 0) break;
  23788. }
  23789. return result;
  23790. }
  23791. #endif /* WOLFSSL_WOLFSSH */
  23792. #if defined(WOLFSSL_HAVE_PRF) && !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  23793. #define DIGL 12
  23794. #define SECL 48
  23795. #define LBSL 63
  23796. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t prf_test(void)
  23797. {
  23798. int ret;
  23799. byte dig[DIGL] = {0};
  23800. byte secret[SECL] = {
  23801. 0x10, 0xbc, 0xb4, 0xa2, 0xe8, 0xdc, 0xf1, 0x9b, 0x4c,
  23802. 0x51, 0x9c, 0xed, 0x31, 0x1b, 0x51, 0x57, 0x02, 0x3f,
  23803. 0xa1, 0x7d, 0xfb, 0x0e, 0xf3, 0x4e, 0x8f, 0x6f, 0x71,
  23804. 0xa3, 0x67, 0x76, 0x6b, 0xfa, 0x5d, 0x46, 0x4a, 0xe8,
  23805. 0x61, 0x18, 0x81, 0xc4, 0x66, 0xcc, 0x6f, 0x09, 0x99,
  23806. 0x9d, 0xfc, 0x47
  23807. };
  23808. byte lablSd[LBSL] = {
  23809. 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x66, 0x69,
  23810. 0x6e, 0x69, 0x73, 0x68, 0x65, 0x64, 0x0b, 0x46, 0xba,
  23811. 0x56, 0xbf, 0x1f, 0x5d, 0x99, 0xff, 0xe9, 0xbb, 0x43,
  23812. 0x01, 0xe7, 0xca, 0x2c, 0x00, 0xdf, 0x9a, 0x39, 0x6e,
  23813. 0xcf, 0x6d, 0x15, 0x27, 0x4d, 0xf2, 0x93, 0x96, 0x4a,
  23814. 0x91, 0xde, 0x5c, 0xc0, 0x47, 0x7c, 0xa8, 0xae, 0xcf,
  23815. 0x5d, 0x93, 0x5f, 0x4c, 0x92, 0xcc, 0x98, 0x5b, 0x43
  23816. };
  23817. byte expected[DIGL] = {
  23818. 0xee, 0xcb, 0xb1, 0x30, 0xf2, 0xcd, 0xb3, 0x4a,
  23819. 0xbe, 0xda, 0xc1, 0xf6
  23820. };
  23821. int digL = DIGL;
  23822. int secL = SECL;
  23823. int lblsdL = LBSL;
  23824. int hash_type = sha384_mac;
  23825. ret = wc_PRF(dig, (word32)digL, secret, secL, lablSd, lblsdL, hash_type,
  23826. HEAP_HINT, INVALID_DEVID);
  23827. if (ret != 0) {
  23828. printf("Failed w/ code: %d\n", ret);
  23829. return WC_TEST_RET_ENC_EC(ret);
  23830. }
  23831. if (XMEMCMP(expected, dig, DIGL) != 0) {
  23832. printf("Got unexpected digest\n");
  23833. return WC_TEST_RET_ENC_NC;
  23834. }
  23835. return 0;
  23836. }
  23837. #endif /* WOLFSSL_HAVE_PRF && !NO_HMAC */
  23838. #ifdef WOLFSSL_HAVE_PRF
  23839. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  23840. #ifdef WOLFSSL_BASE16
  23841. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t tls12_kdf_test(void)
  23842. {
  23843. const char* preMasterSecret = "D06F9C19BFF49B1E91E4EFE97345D089"
  23844. "4E6C2E6C34A165B24540E2970875D641"
  23845. "2AA6515871B389B4C199BB8389C71CED";
  23846. const char* helloRandom = "162B81EDFBEAE4F25240320B87E7651C"
  23847. "865564191DD782DB0B9ECA275FBA1BB9"
  23848. "5A1DA3DF436D68DA86C5E7B4B4A36E46"
  23849. "B977C61767983A31BE270D74517BD0F6";
  23850. const char* masterSecret = "EB38B8D89B98B1C266DE44BB3CA14E83"
  23851. "C32F009F9955B1D994E61D3C51EE8760"
  23852. "90B4EF89CC7AF42F46E72201BFCC7977";
  23853. const char* label = "master secret";
  23854. byte pms[48] = {0};
  23855. byte seed[64] = {0};
  23856. byte ms[48] = {0};
  23857. byte result[48] = {0};
  23858. word32 pmsSz = (word32)sizeof(pms);
  23859. word32 seedSz = (word32)sizeof(seed);
  23860. word32 msSz = (word32)sizeof(ms);
  23861. int ret;
  23862. ret = Base16_Decode((const byte*)preMasterSecret,
  23863. (word32)XSTRLEN(preMasterSecret), pms, &pmsSz);
  23864. if (ret != 0)
  23865. return ret;
  23866. ret = Base16_Decode((const byte*)helloRandom,
  23867. (word32)XSTRLEN(helloRandom), seed, &seedSz);
  23868. if (ret != 0)
  23869. return ret;
  23870. ret = Base16_Decode((const byte*)masterSecret,
  23871. (word32)XSTRLEN(masterSecret), ms, &msSz);
  23872. if (ret != 0)
  23873. return ret;
  23874. ret = wc_PRF_TLS(result, msSz, pms, pmsSz,
  23875. (const byte*)label, (word32)XSTRLEN(label), seed, seedSz,
  23876. 1, sha256_mac, NULL, INVALID_DEVID);
  23877. if (ret != 0) {
  23878. if (ret == FIPS_PRIVATE_KEY_LOCKED_E) {
  23879. printf(" wc_PRF_TLSv12: Private key locked.\n");
  23880. }
  23881. return WC_TEST_RET_ENC_NC;
  23882. }
  23883. if (XMEMCMP(result, ms, msSz) != 0)
  23884. return WC_TEST_RET_ENC_NC;
  23885. return 0;
  23886. }
  23887. #endif /* WOLFSSL_BASE16 */
  23888. #endif /* WOLFSSL_HAVE_HKDF && !NO_HMAC */
  23889. #endif /* WOLFSSL_HAVE_PRF */
  23890. #ifdef WOLFSSL_TLS13
  23891. #define TLSV13_PSK_DHE_SZ 40
  23892. typedef struct {
  23893. enum wc_HashType hashAlg;
  23894. word32 pskSz;
  23895. word32 dheSz;
  23896. byte psk[TLSV13_PSK_DHE_SZ];
  23897. byte dhe[TLSV13_PSK_DHE_SZ];
  23898. byte hashHello1[WC_MAX_DIGEST_SIZE];
  23899. byte hashHello2[WC_MAX_DIGEST_SIZE];
  23900. byte hashFinished1[WC_MAX_DIGEST_SIZE];
  23901. byte hashFinished2[WC_MAX_DIGEST_SIZE];
  23902. /* Expected */
  23903. byte clientEarlyTrafficSecret[WC_MAX_DIGEST_SIZE];
  23904. byte earlyExporterMasterSecret[WC_MAX_DIGEST_SIZE];
  23905. byte clientHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  23906. byte serverHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  23907. byte clientApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  23908. byte serverApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  23909. byte exporterMasterSecret[WC_MAX_DIGEST_SIZE];
  23910. byte resumptionMasterSecret[WC_MAX_DIGEST_SIZE];
  23911. } Tls13KdfTestVector;
  23912. /* The following tests come from the CAVP test vectors we used for
  23913. * our FIPS validation. The hash values used are the components from
  23914. * the test hashed together. hashHello1 is the hash of the
  23915. * clientHelloRandom value of the test vector. hashHello2 is the hash
  23916. * of the clientHelloRandom and serverHelloRandom values from the test
  23917. * vector. hashFinished1 is clientHelloRandom, serverHelloRandom, and
  23918. * serverFinishedRandom. hashFinished2 is clientHelloRandom,
  23919. * serverHelloRandom, serverFinishedRandom, and clietnFinishedRandom
  23920. * hashed together. */
  23921. static const Tls13KdfTestVector tls13KdfTestVectors[] = {
  23922. { /* 1 */
  23923. WC_HASH_TYPE_SHA256, 35, 35,
  23924. { /* PSK */
  23925. 0x7b, 0xf1, 0x05, 0x31, 0x36, 0xfa, 0x03, 0xdc,
  23926. 0x31, 0x97, 0x88, 0x04, 0x9c, 0xbc, 0xee, 0xf7,
  23927. 0x8d, 0x84, 0x95, 0x26, 0xaf, 0x1d, 0x68, 0xb0,
  23928. 0x60, 0x7a, 0xcc, 0x4f, 0xc1, 0xd3, 0xa1, 0x68,
  23929. 0x7f, 0x6d, 0xbe
  23930. },
  23931. { /* DHE */
  23932. 0x6e, 0xa1, 0x77, 0xab, 0x2f, 0x43, 0xd2, 0x4b,
  23933. 0xe5, 0xa1, 0x09, 0xe0, 0x7a, 0xd0, 0x01, 0x35,
  23934. 0x8d, 0xf8, 0xf2, 0x5c, 0x91, 0x02, 0xb0, 0x6c,
  23935. 0x3f, 0xeb, 0xee, 0xa4, 0x42, 0x19, 0xce, 0xdc,
  23936. 0x81, 0x26, 0x40
  23937. },
  23938. { /* Hello 1 */
  23939. 0xd9, 0x4b, 0xe4, 0x17, 0xef, 0x58, 0x73, 0x7d,
  23940. 0x28, 0x3d, 0xf0, 0xcc, 0x05, 0x03, 0xaf, 0xac,
  23941. 0x3d, 0x92, 0x79, 0x48, 0xe8, 0x8c, 0xdb, 0xce,
  23942. 0x95, 0x82, 0x21, 0x31, 0x7b, 0x61, 0xd7, 0xc6
  23943. },
  23944. { /* Hello 2 */
  23945. 0xb7, 0x7f, 0x29, 0x91, 0xa4, 0x8b, 0x34, 0xdb,
  23946. 0xbd, 0xc7, 0x54, 0x1c, 0x3b, 0x86, 0xa3, 0x69,
  23947. 0xfe, 0x26, 0xe4, 0x7b, 0xac, 0x57, 0x71, 0xb3,
  23948. 0x32, 0x97, 0xed, 0xd2, 0x0e, 0x95, 0xb8, 0x63
  23949. },
  23950. { /* Finished 1 */
  23951. 0x65, 0xdb, 0x6d, 0x71, 0x71, 0xd0, 0xd8, 0x49,
  23952. 0xd0, 0x3c, 0x8e, 0x2b, 0x24, 0xdf, 0xc2, 0xe9,
  23953. 0xd6, 0xfd, 0xea, 0x04, 0x95, 0x7c, 0xf0, 0x7e,
  23954. 0x57, 0x74, 0x7c, 0xdd, 0xa3, 0x0b, 0x2b, 0x36
  23955. },
  23956. { /* Finished 2 */
  23957. 0x28, 0xf2, 0xf2, 0x79, 0xcf, 0x20, 0x52, 0x90,
  23958. 0x1d, 0x91, 0x05, 0xad, 0x44, 0x26, 0x23, 0x96,
  23959. 0x32, 0xce, 0xec, 0x61, 0xd1, 0xbf, 0x00, 0x48,
  23960. 0x4a, 0xa5, 0x60, 0xcc, 0x28, 0xb5, 0x8d, 0x98
  23961. },
  23962. { /* Client Early Traffic Secret */
  23963. 0x07, 0x14, 0x6a, 0x26, 0x5b, 0x6c, 0x7f, 0x4d, 0x6b, 0x47, 0x3f, 0xd5,
  23964. 0x03, 0x1d, 0xd2, 0x23, 0x3d, 0x89, 0x3e, 0xc6, 0x51, 0xd1, 0xac, 0xf8,
  23965. 0x28, 0xae, 0x4b, 0x76, 0xc8, 0x10, 0x7e, 0xdd
  23966. },
  23967. { /* Early Exporter Master Secret */
  23968. 0xb8, 0xd3, 0x25, 0x7e, 0x2d, 0x41, 0x7b, 0xcb, 0x5e, 0x82, 0x49, 0xf5,
  23969. 0x51, 0x3d, 0xb7, 0x59, 0x32, 0xb3, 0xdf, 0x99, 0x4e, 0x04, 0x69, 0xc6,
  23970. 0x96, 0x8e, 0xe6, 0x3d, 0x91, 0xe4, 0x81, 0x11
  23971. },
  23972. { /* Client Handshake Traffic Secret */
  23973. 0xd9, 0x3b, 0x54, 0xe2, 0xb0, 0xd1, 0x85, 0xf0, 0xfd, 0xf3, 0x48, 0x4a,
  23974. 0xf8, 0x0b, 0xa5, 0xdc, 0x4c, 0x37, 0xcb, 0xd4, 0x20, 0xaf, 0x60, 0xc7,
  23975. 0xd5, 0x50, 0x5d, 0x0c, 0x77, 0x3b, 0x6f, 0xd2
  23976. },
  23977. { /* Server Handshake Traffic Secret */
  23978. 0x4d, 0x40, 0x2b, 0xd2, 0x8c, 0x33, 0x90, 0x39, 0x67, 0x67, 0x05, 0xf7,
  23979. 0x5d, 0x37, 0x1e, 0xdc, 0x4a, 0x70, 0x6b, 0x9e, 0xf8, 0x06, 0x61, 0x89,
  23980. 0x70, 0xe1, 0x3d, 0x36, 0xad, 0x88, 0x7e, 0x5b
  23981. },
  23982. { /* Client Application Traffic Secret */
  23983. 0x74, 0x6e, 0xa0, 0x13, 0x18, 0x34, 0x48, 0x4d, 0x23, 0x31, 0xf1, 0xf9,
  23984. 0xee, 0x44, 0x6d, 0xad, 0xc1, 0xad, 0x92, 0x73, 0xca, 0x27, 0x16, 0x91,
  23985. 0xa2, 0x50, 0x9a, 0xfc, 0xec, 0xf0, 0x6b, 0x24
  23986. },
  23987. { /* Server Application Traffic Secret */
  23988. 0x89, 0x18, 0x7e, 0x34, 0x8d, 0xfc, 0x14, 0xb1, 0x4f, 0x21, 0xd8, 0x29,
  23989. 0xdb, 0x9b, 0xfb, 0x55, 0xcf, 0xa1, 0x4f, 0x95, 0xf8, 0xe0, 0xb0, 0x83,
  23990. 0xd5, 0x34, 0x9e, 0x0b, 0x83, 0x37, 0x42, 0x93
  23991. },
  23992. { /* Exporter Master Secret */
  23993. 0x7d, 0xc8, 0x88, 0x46, 0xd5, 0x57, 0x15, 0xb6, 0x24, 0x25, 0x92, 0x61,
  23994. 0xb1, 0x18, 0x86, 0x2a, 0x6d, 0xa5, 0x84, 0xeb, 0x59, 0xdf, 0x13, 0xbd,
  23995. 0x73, 0xaa, 0x5d, 0x65, 0xab, 0xd9, 0xb4, 0x56
  23996. },
  23997. { /* Resumption Master Secret */
  23998. 0x20, 0xb7, 0xd0, 0xe3, 0x82, 0x01, 0xa1, 0x04, 0xb8, 0x13, 0x29, 0xed,
  23999. 0x35, 0xe4, 0x2f, 0xbf, 0x58, 0x23, 0x7f, 0x21, 0xdb, 0x9f, 0xf8, 0xe0,
  24000. 0xe8, 0xe4, 0xab, 0xc4, 0xa1, 0x61, 0xb9, 0xbb
  24001. }
  24002. },
  24003. { /* 6 */
  24004. WC_HASH_TYPE_SHA256, 0, 33,
  24005. { 0 }, /* PSK */
  24006. { /* DHE */
  24007. 0x7a, 0x46, 0x8c, 0x5a, 0xd1, 0x8e, 0x95, 0xba,
  24008. 0x61, 0xe6, 0x6f, 0xe6, 0x76, 0x0c, 0x20, 0x43,
  24009. 0x16, 0x82, 0x15, 0xfe, 0x54, 0xa3, 0xc7, 0xfd,
  24010. 0x3b, 0x2c, 0x88, 0xb4, 0xd3, 0x42, 0x70, 0x12,
  24011. 0x18
  24012. },
  24013. { /* Hello 1 */
  24014. 0x63, 0x83, 0x58, 0xab, 0x36, 0xcd, 0x0c, 0xf3,
  24015. 0x26, 0x07, 0xb5, 0x5f, 0x0b, 0x8b, 0x45, 0xd6,
  24016. 0x7d, 0x5b, 0x42, 0xdc, 0xa8, 0xaa, 0x06, 0xfb,
  24017. 0x20, 0xa5, 0xbb, 0x85, 0xdb, 0x54, 0xd8, 0x8b
  24018. },
  24019. { /* Hello 2 */
  24020. 0xea, 0xfe, 0x9e, 0x8e, 0xff, 0x1f, 0x6f, 0x43,
  24021. 0xf9, 0x5d, 0xfd, 0xbf, 0xe2, 0x5f, 0x02, 0x2f,
  24022. 0x6d, 0x47, 0x60, 0x9a, 0x48, 0x9a, 0x75, 0xfb,
  24023. 0xb5, 0x4a, 0xbf, 0x9c, 0x4e, 0xff, 0xbf, 0x0b
  24024. },
  24025. { /* Finished 1 */
  24026. 0xca, 0x25, 0xb3, 0x53, 0x8e, 0x6d, 0xc3, 0x36,
  24027. 0x17, 0x30, 0x07, 0xdf, 0x0d, 0xd7, 0x79, 0xb0,
  24028. 0x7f, 0xcb, 0xbe, 0x7a, 0xbc, 0x2d, 0x9f, 0x2d,
  24029. 0x94, 0x44, 0x94, 0xe6, 0xa4, 0xf3, 0xe8, 0x53
  24030. },
  24031. { /* Finished 2 */
  24032. 0x2e, 0xa6, 0x5a, 0xaf, 0xb5, 0xba, 0x9f, 0x2f,
  24033. 0x74, 0x83, 0x5d, 0xbf, 0x86, 0xa4, 0xa6, 0xf6,
  24034. 0xb9, 0x89, 0xdf, 0x17, 0xe1, 0xa8, 0x14, 0xc0,
  24035. 0xe1, 0x50, 0xfa, 0xec, 0xfa, 0xae, 0x8b, 0x7b
  24036. },
  24037. {
  24038. 0x20, 0x18, 0x72, 0x7c, 0xde, 0x3a, 0x85, 0x17, 0x72, 0xdc, 0xd7, 0x72,
  24039. 0xb0, 0xfc, 0x45, 0xd0, 0x62, 0xb9, 0xbb, 0x38, 0x69, 0x05, 0x7b, 0xb4,
  24040. 0x5e, 0x58, 0x5d, 0xed, 0xcd, 0x0b, 0x96, 0xd3
  24041. },
  24042. {
  24043. 0x68, 0x10, 0x20, 0xd1, 0x5e, 0xfc, 0x0c, 0x53, 0x85, 0xbb, 0xdb, 0x18,
  24044. 0xa8, 0x78, 0xf1, 0x2b, 0x13, 0xba, 0x64, 0x1d, 0xe7, 0x09, 0xbe, 0x13,
  24045. 0x49, 0x26, 0xf9, 0x98, 0x56, 0xf1, 0x43, 0xfb
  24046. },
  24047. {
  24048. 0x24, 0x35, 0x3e, 0x10, 0x6f, 0x39, 0x50, 0xd6, 0xa2, 0x12, 0x99, 0xf2,
  24049. 0xd5, 0xf5, 0x19, 0xf5, 0x84, 0xed, 0xee, 0x78, 0x2a, 0xa6, 0xfa, 0x3d,
  24050. 0x06, 0xa8, 0xa7, 0x5d, 0x97, 0x78, 0xd6, 0x58
  24051. },
  24052. {
  24053. 0xf4, 0x57, 0xac, 0x24, 0x7a, 0xfb, 0x7c, 0x3b, 0xb6, 0x39, 0x17, 0x14,
  24054. 0xd9, 0xd4, 0x58, 0x4d, 0x46, 0xd5, 0x1b, 0xde, 0xf7, 0x9d, 0x06, 0xee,
  24055. 0x8d, 0x1a, 0x2c, 0x25, 0x6d, 0x64, 0xde, 0x89
  24056. },
  24057. {
  24058. 0xb6, 0x00, 0xce, 0x63, 0xed, 0x65, 0x8b, 0x66, 0x66, 0x42, 0xc6, 0xbd,
  24059. 0x89, 0xc4, 0x71, 0x6f, 0xce, 0x28, 0xb2, 0xac, 0x97, 0x07, 0x5b, 0xea,
  24060. 0xb8, 0x1d, 0x4c, 0xeb, 0x9e, 0x71, 0x07, 0x8f
  24061. },
  24062. {
  24063. 0xf8, 0x92, 0xc8, 0xba, 0xe7, 0x83, 0xfe, 0x68, 0xe4, 0xd6, 0x5e, 0xcb,
  24064. 0xb3, 0xef, 0x49, 0xd0, 0xe7, 0xb1, 0xac, 0xcb, 0x39, 0x19, 0xfd, 0xa7,
  24065. 0xf7, 0xca, 0xab, 0x1e, 0x42, 0x14, 0xd8, 0xe7
  24066. },
  24067. {
  24068. 0x32, 0x4a, 0x1a, 0xad, 0xe2, 0xbb, 0x55, 0x8a, 0xdd, 0xe9, 0xa5, 0x2a,
  24069. 0x46, 0x5e, 0x6c, 0x83, 0x66, 0x27, 0x27, 0x94, 0xdd, 0x68, 0x59, 0xa0,
  24070. 0xbb, 0xe8, 0x31, 0x7c, 0x39, 0xd7, 0xfd, 0x6d
  24071. },
  24072. {
  24073. 0x58, 0xbc, 0x6c, 0x5b, 0x24, 0xad, 0x82, 0xb3, 0xcc, 0xc7, 0xd1, 0xa1,
  24074. 0xaa, 0x2b, 0x98, 0x9f, 0x2f, 0x7e, 0xa9, 0x63, 0xc2, 0x8e, 0xb6, 0x06,
  24075. 0xc2, 0x2b, 0x74, 0x4b, 0x79, 0x19, 0x7e, 0x2e
  24076. }
  24077. },
  24078. { /* 11 */
  24079. WC_HASH_TYPE_SHA256, 33, 0,
  24080. { /* PSK */
  24081. 0x3d, 0x39, 0x49, 0x36, 0x98, 0xc5, 0xfd, 0xcd,
  24082. 0xa0, 0x17, 0xbd, 0x65, 0x0a, 0xdb, 0xd4, 0x07,
  24083. 0x56, 0xa2, 0x7b, 0xb8, 0x2a, 0x7e, 0xfb, 0x26,
  24084. 0x74, 0xe1, 0xbc, 0x08, 0x4b, 0xf0, 0x30, 0x14,
  24085. 0x12
  24086. },
  24087. { 0 }, /* DHE */
  24088. { /* Hello 1 */
  24089. 0xb7, 0x44, 0x74, 0x6c, 0x57, 0x1f, 0xf3, 0x84,
  24090. 0x8f, 0x63, 0xfb, 0x8c, 0x94, 0x6c, 0x16, 0x68,
  24091. 0x4b, 0xe1, 0xb5, 0xb5, 0x2a, 0x4e, 0x5f, 0xdf,
  24092. 0x4b, 0x53, 0xb2, 0x35, 0xfc, 0x30, 0xf1, 0x36
  24093. },
  24094. { /* Hello 2 */
  24095. 0xe6, 0x4f, 0x3a, 0x4f, 0xd7, 0xe0, 0x64, 0xd4,
  24096. 0x69, 0x50, 0xe4, 0x8b, 0xba, 0xbc, 0x47, 0x74,
  24097. 0xa7, 0x9b, 0x40, 0x91, 0x8f, 0xa8, 0x72, 0x22,
  24098. 0x97, 0xad, 0x43, 0xa7, 0x11, 0x86, 0xb5, 0x72
  24099. },
  24100. { /* Finished 1 */
  24101. 0x5f, 0xa6, 0x10, 0xe2, 0xa3, 0x99, 0x0b, 0x5e,
  24102. 0x57, 0xee, 0xc3, 0x3a, 0x8e, 0x04, 0xf3, 0x0e,
  24103. 0x58, 0x02, 0x09, 0xb2, 0x7e, 0x2d, 0xc6, 0xd2,
  24104. 0x08, 0xae, 0x68, 0x0a, 0x55, 0xa5, 0xda, 0x51
  24105. },
  24106. { /* Finished 2 */
  24107. 0xfc, 0x5b, 0xc0, 0x7e, 0x1b, 0xaa, 0xc0, 0xb4,
  24108. 0x34, 0x85, 0x49, 0x8e, 0x16, 0x31, 0x98, 0xdf,
  24109. 0x10, 0x54, 0x22, 0xda, 0x1e, 0x6b, 0x51, 0xf6,
  24110. 0x97, 0x57, 0xa0, 0x7a, 0x92, 0xe7, 0x47, 0x52
  24111. },
  24112. {
  24113. 0x80, 0xfa, 0x36, 0x30, 0xb8, 0x65, 0xb3, 0x2a, 0x1d, 0x68, 0x91, 0x06,
  24114. 0x98, 0xa0, 0x17, 0x8f, 0xee, 0xb7, 0x9e, 0x3d, 0xd8, 0x84, 0x99, 0x30,
  24115. 0xb9, 0xd6, 0x09, 0x25, 0x5e, 0xfb, 0x8f, 0xd3 },
  24116. {
  24117. 0xa9, 0x89, 0x29, 0x70, 0xe4, 0x55, 0xec, 0x97, 0xfb, 0x24, 0x5b, 0xf9,
  24118. 0xf1, 0xa3, 0x19, 0x3d, 0xf1, 0x31, 0x14, 0xcd, 0x2a, 0xed, 0x21, 0xc8,
  24119. 0xb1, 0x53, 0xad, 0x11, 0x0b, 0x9e, 0x5a, 0xee },
  24120. {
  24121. 0x72, 0xad, 0x8d, 0x7f, 0xfc, 0xb7, 0x68, 0xda, 0x27, 0x60, 0x37, 0xa3,
  24122. 0x4a, 0x63, 0xe8, 0xa5, 0xc8, 0xcd, 0x36, 0x6a, 0x77, 0x99, 0x0d, 0xa9,
  24123. 0xb1, 0x5b, 0x2f, 0x47, 0x2e, 0x22, 0xa7, 0x5e },
  24124. {
  24125. 0x95, 0x6e, 0x85, 0x09, 0xe5, 0x04, 0x88, 0x14, 0x28, 0x8d, 0xdf, 0xe6,
  24126. 0x0d, 0x0f, 0x0d, 0x6b, 0x4e, 0x66, 0x1c, 0x03, 0xb9, 0xaa, 0x2d, 0x45,
  24127. 0x56, 0x67, 0x5c, 0x55, 0x29, 0xd6, 0x89, 0xd0 },
  24128. {
  24129. 0xe8, 0xf2, 0x14, 0xf9, 0x9b, 0x2b, 0x9f, 0x24, 0x2b, 0x37, 0xbe, 0x86,
  24130. 0xdb, 0x23, 0x4b, 0xbe, 0x39, 0x57, 0xe8, 0xa9, 0xa5, 0xee, 0x08, 0xf2,
  24131. 0x75, 0x58, 0xdb, 0xd9, 0x51, 0xc1, 0x46, 0x02 },
  24132. {
  24133. 0x3d, 0x19, 0xaf, 0xa3, 0x0b, 0x21, 0xf7, 0x3d, 0xe7, 0x37, 0x6e, 0x32,
  24134. 0x13, 0x48, 0x9d, 0xea, 0xe0, 0x90, 0xbf, 0x64, 0x48, 0xf7, 0x1e, 0xcc,
  24135. 0xf0, 0xbc, 0x92, 0xd7, 0x8a, 0x4a, 0xa8, 0xc1 },
  24136. {
  24137. 0x16, 0x35, 0xb1, 0x66, 0x28, 0xa3, 0x3e, 0x19, 0xf5, 0x2d, 0x92, 0x22,
  24138. 0x95, 0x48, 0xe8, 0x34, 0x7b, 0x30, 0x50, 0xa2, 0xa0, 0xd9, 0xc2, 0x59,
  24139. 0x39, 0xf9, 0x8c, 0x69, 0xf2, 0x2a, 0xb9, 0xff },
  24140. {
  24141. 0x32, 0x71, 0xa6, 0x87, 0x0c, 0x97, 0x42, 0x07, 0xdd, 0x5f, 0xc9, 0x44,
  24142. 0xa5, 0x7c, 0x50, 0x14, 0xfd, 0xe7, 0x5f, 0x8b, 0xd3, 0x2f, 0xdc, 0x9b,
  24143. 0xa9, 0x93, 0x22, 0x19, 0xe6, 0xf2, 0x0c, 0xd8 }
  24144. },
  24145. #ifdef WOLFSSL_SHA384
  24146. { /* 26 */
  24147. WC_HASH_TYPE_SHA384, 35, 35,
  24148. { /* PSK */
  24149. 0x62, 0x83, 0x25, 0xc7, 0xcc, 0x08, 0x5e, 0x63,
  24150. 0x64, 0x56, 0xf0, 0xc6, 0x88, 0x27, 0x5a, 0x5b,
  24151. 0x68, 0x59, 0x0b, 0x14, 0x55, 0x13, 0x2e, 0xfd,
  24152. 0x8f, 0x28, 0x5b, 0x3d, 0xe3, 0xad, 0x67, 0xe4,
  24153. 0x68, 0xba, 0xf9
  24154. },
  24155. { /* DHE */
  24156. 0xa8, 0xb1, 0xab, 0xd8, 0xc8, 0x5b, 0x52, 0xdf,
  24157. 0x7f, 0x49, 0x10, 0xf4, 0xa1, 0x31, 0xd1, 0x91,
  24158. 0x36, 0xc1, 0x87, 0x5d, 0x42, 0x2a, 0xe7, 0x1d,
  24159. 0x2c, 0x29, 0x3d, 0x40, 0x64, 0x61, 0x63, 0x76,
  24160. 0xd8, 0x66, 0xac
  24161. },
  24162. { /* Hello 1 */
  24163. 0x6f, 0xc6, 0x4c, 0xe1, 0xc6, 0x68, 0x34, 0x8c,
  24164. 0x0a, 0xe1, 0xf8, 0xb8, 0x3e, 0xd4, 0xf8, 0x0b,
  24165. 0x54, 0x50, 0xe4, 0xc5, 0x4a, 0x33, 0x7d, 0xbd,
  24166. 0x90, 0xd2, 0xa2, 0xb9, 0xb7, 0x92, 0xed, 0xab,
  24167. 0x14, 0xf1, 0xe4, 0x86, 0x22, 0x67, 0xd7, 0x44,
  24168. 0x03, 0x21, 0xdc, 0x51, 0x52, 0x7f, 0x35, 0x80
  24169. },
  24170. { /* Hello 2 */
  24171. 0x3e, 0xcf, 0x2f, 0xc3, 0x87, 0xba, 0xc5, 0xbd,
  24172. 0x7c, 0xe8, 0x35, 0x5b, 0x95, 0x51, 0x30, 0x3b,
  24173. 0x08, 0xcc, 0x2a, 0x7d, 0xb5, 0x74, 0x7c, 0x16,
  24174. 0xb3, 0x0b, 0xe7, 0x61, 0xa3, 0x7c, 0x6c, 0xbd,
  24175. 0x39, 0x74, 0xfd, 0x1e, 0x4c, 0xff, 0xc8, 0xcc,
  24176. 0xa0, 0xef, 0x29, 0x4d, 0x94, 0xaa, 0x55, 0x6f,
  24177. },
  24178. { /* Finished 1 */
  24179. 0x06, 0xc1, 0x47, 0x78, 0x66, 0x53, 0x6f, 0x24,
  24180. 0x94, 0x61, 0x69, 0xec, 0xd8, 0x60, 0x31, 0x2f,
  24181. 0xbf, 0xd6, 0x8a, 0x29, 0x17, 0xff, 0xa3, 0x88,
  24182. 0x13, 0x09, 0x8c, 0x9d, 0x6c, 0x64, 0x84, 0x48,
  24183. 0x44, 0xdd, 0x2d, 0x29, 0x4d, 0xe6, 0x98, 0x2b,
  24184. 0x45, 0x3b, 0x84, 0x33, 0x79, 0xb2, 0x75, 0x68
  24185. },
  24186. { /* Finished 2 */
  24187. 0x28, 0x1e, 0x18, 0xf7, 0x9c, 0x32, 0xa9, 0xbf,
  24188. 0x0c, 0x24, 0x58, 0x21, 0xce, 0xbc, 0xf2, 0x44,
  24189. 0xb1, 0x18, 0xaf, 0x9d, 0xd9, 0x20, 0xf9, 0xf4,
  24190. 0xed, 0xcc, 0x53, 0x82, 0x66, 0x5c, 0x46, 0x94,
  24191. 0x8c, 0x36, 0x5e, 0xca, 0x9f, 0xd8, 0x9a, 0xd3,
  24192. 0xf0, 0xe1, 0x53, 0x71, 0xdd, 0x19, 0x1e, 0x59
  24193. },
  24194. {
  24195. 0xd0, 0xef, 0xa8, 0xcb, 0x5b, 0x14, 0x0f, 0x0a, 0x62, 0xba, 0x5a, 0xb1,
  24196. 0xc5, 0xb5, 0x3f, 0x11, 0xda, 0xa1, 0x0c, 0x9c, 0xb4, 0x32, 0x48, 0x4e,
  24197. 0xfa, 0x84, 0x4f, 0xe4, 0xe7, 0x91, 0x8f, 0x42, 0x3f, 0xc7, 0x4e, 0xd3,
  24198. 0x83, 0x3d, 0x7f, 0x70, 0x12, 0xee, 0x9a, 0x37, 0x01, 0xbb, 0x14, 0xd3
  24199. },
  24200. {
  24201. 0x48, 0x6f, 0x77, 0x1d, 0x39, 0x1b, 0xa5, 0x9a, 0x76, 0xd9, 0x1d, 0x7d,
  24202. 0xb3, 0xd9, 0xb9, 0x78, 0x35, 0x0f, 0xd0, 0xe1, 0x07, 0x1f, 0x8d, 0xe5,
  24203. 0x75, 0x00, 0xda, 0xc0, 0x19, 0x01, 0xfb, 0x08, 0x35, 0xe7, 0x18, 0x8f,
  24204. 0xf0, 0x19, 0xfb, 0x46, 0xf6, 0xa5, 0x77, 0x0e, 0x90, 0x38, 0x8b, 0x15
  24205. },
  24206. {
  24207. 0x80, 0x8c, 0xa7, 0x24, 0x97, 0xf9, 0xd3, 0x52, 0xb0, 0x69, 0x9d, 0x4b,
  24208. 0xa4, 0x19, 0x4a, 0xb1, 0x46, 0x53, 0x3a, 0xc8, 0xe4, 0x02, 0x69, 0xf2,
  24209. 0xe7, 0xb6, 0x1d, 0x33, 0x51, 0xcc, 0x14, 0x40, 0x4a, 0xb0, 0xe7, 0x58,
  24210. 0x84, 0xba, 0xc2, 0x14, 0x58, 0x6b, 0xb9, 0xdc, 0x50, 0x98, 0x67, 0x01
  24211. },
  24212. {
  24213. 0xb1, 0xa8, 0xc0, 0x06, 0xb3, 0x2e, 0xa7, 0x8a, 0x6a, 0x12, 0x88, 0x00,
  24214. 0x65, 0x88, 0x9c, 0x5d, 0x35, 0xee, 0xe5, 0x51, 0x0b, 0x62, 0xf8, 0x67,
  24215. 0xe5, 0xef, 0x15, 0x1f, 0x23, 0x02, 0x74, 0x08, 0x9c, 0xc8, 0xba, 0x27,
  24216. 0x5d, 0x32, 0x19, 0x6f, 0x6d, 0x5d, 0x72, 0x5e, 0x15, 0xde, 0x30, 0xc3
  24217. },
  24218. {
  24219. 0xfd, 0xce, 0xf5, 0x65, 0x45, 0x84, 0xfb, 0x8c, 0x79, 0xa4, 0x6c, 0x1b,
  24220. 0x0e, 0x1b, 0xfd, 0x26, 0xa2, 0x53, 0xf4, 0x4e, 0x00, 0x4d, 0x4b, 0x0b,
  24221. 0x24, 0x6d, 0x35, 0x35, 0xd9, 0x97, 0x70, 0xc5, 0xf4, 0xee, 0xe3, 0xba,
  24222. 0x31, 0x1e, 0x2a, 0x42, 0xcb, 0xdf, 0x40, 0xb1, 0x14, 0xb8, 0x53, 0xce
  24223. },
  24224. {
  24225. 0xbb, 0xb3, 0x26, 0x7c, 0x22, 0x21, 0x9b, 0x72, 0x32, 0xa1, 0x97, 0xfb,
  24226. 0x78, 0x8c, 0xbe, 0x3d, 0x71, 0x45, 0xb8, 0xf5, 0x24, 0x8f, 0x0f, 0xac,
  24227. 0x42, 0x5b, 0x81, 0xe8, 0xd0, 0x71, 0x4a, 0xcb, 0x32, 0x3f, 0x03, 0xfb,
  24228. 0xec, 0x6a, 0x1f, 0x76, 0x80, 0x65, 0x01, 0x7a, 0x3d, 0xce, 0xc4, 0xdf
  24229. },
  24230. {
  24231. 0x3f, 0xcf, 0x2f, 0x63, 0x94, 0x94, 0x99, 0xfd, 0x04, 0x3a, 0x89, 0x83,
  24232. 0xcf, 0x06, 0x05, 0xec, 0x20, 0x3e, 0x5f, 0x51, 0x9d, 0x6e, 0x4a, 0xc6,
  24233. 0xf1, 0x2b, 0x37, 0x17, 0x34, 0x72, 0x6e, 0x1d, 0x2a, 0xfd, 0xc7, 0x73,
  24234. 0xb5, 0x07, 0x22, 0x81, 0x32, 0x2e, 0x21, 0x85, 0xaf, 0x10, 0xb2, 0x73
  24235. },
  24236. {
  24237. 0x52, 0x0c, 0x3d, 0x2e, 0x2d, 0x4a, 0x11, 0xae, 0x96, 0x78, 0xe9, 0x5b,
  24238. 0xd8, 0x0f, 0x6c, 0xf4, 0xbd, 0x96, 0x13, 0x55, 0x88, 0xdd, 0xa3, 0x67,
  24239. 0x36, 0x86, 0x1e, 0x0b, 0x36, 0x41, 0xec, 0xf6, 0x04, 0xb2, 0xc4, 0x16,
  24240. 0xbc, 0x2c, 0xdb, 0x30, 0x02, 0x94, 0xd4, 0x42, 0xbf, 0x38, 0xee, 0x9d
  24241. }
  24242. },
  24243. { /* 36 */
  24244. WC_HASH_TYPE_SHA384, 0, 33,
  24245. { 0 }, /* PSK */
  24246. { /* DHE */
  24247. 0xd3, 0x00, 0x72, 0x9a, 0xa8, 0xc5, 0xf3, 0xc4,
  24248. 0xf1, 0xa0, 0x26, 0x89, 0x65, 0x70, 0xc7, 0x0b,
  24249. 0x77, 0xbb, 0xe1, 0x4b, 0x2b, 0xa8, 0x4f, 0xa6,
  24250. 0x09, 0x4b, 0xba, 0x45, 0x36, 0x15, 0xee, 0x68,
  24251. 0xfd
  24252. },
  24253. { /* Hello 1 */
  24254. 0x10, 0x9d, 0x8b, 0xa2, 0x93, 0xe7, 0xd3, 0xb9,
  24255. 0xb4, 0x0f, 0xeb, 0x6a, 0xb9, 0x69, 0xcb, 0x39,
  24256. 0x16, 0x29, 0xcc, 0xd3, 0xcc, 0x1a, 0x4c, 0x1b,
  24257. 0x53, 0x7c, 0x33, 0x88, 0x06, 0xbc, 0x0a, 0x02,
  24258. 0xa0, 0xbe, 0x62, 0xc0, 0xe6, 0x5e, 0x97, 0x5b,
  24259. 0x6a, 0xa1, 0x98, 0xf3, 0xd2, 0x1e, 0xcd, 0xc5
  24260. },
  24261. { /* Hello 2 */
  24262. 0x74, 0xc0, 0x07, 0x2c, 0xc1, 0x63, 0xcc, 0x11,
  24263. 0xad, 0x1a, 0x55, 0x63, 0xbc, 0x20, 0x77, 0x96,
  24264. 0x30, 0x1c, 0x68, 0x45, 0x1e, 0x9b, 0xa7, 0xb4,
  24265. 0xf3, 0x04, 0x45, 0x16, 0x76, 0x55, 0xf9, 0xdf,
  24266. 0x4b, 0x2f, 0x1a, 0xdf, 0x5a, 0xb0, 0x93, 0xc9,
  24267. 0xab, 0xf5, 0x32, 0x47, 0x79, 0x9c, 0x01, 0xeb
  24268. },
  24269. { /* Finished 1 */
  24270. 0x27, 0x08, 0x8e, 0xa5, 0xf1, 0x30, 0xe1, 0xd6,
  24271. 0x4f, 0xa2, 0x9e, 0x3b, 0x03, 0x2d, 0x2e, 0xa3,
  24272. 0x84, 0x75, 0x51, 0x3a, 0xc3, 0xf6, 0xee, 0x2e,
  24273. 0x37, 0x0c, 0xe3, 0x28, 0x46, 0xa5, 0x2d, 0xc7,
  24274. 0xf0, 0x64, 0x78, 0x53, 0x66, 0x43, 0x02, 0xa4,
  24275. 0x7a, 0x43, 0x66, 0x4b, 0xa7, 0xcb, 0x97, 0x16
  24276. },
  24277. { /* Finished 2 */
  24278. 0x1d, 0x0d, 0xf8, 0xe1, 0x81, 0xa5, 0xbd, 0xa8,
  24279. 0x6f, 0x9d, 0x01, 0xa4, 0x9a, 0x92, 0xe2, 0xef,
  24280. 0x08, 0xab, 0xef, 0x3e, 0x2d, 0xd4, 0x82, 0xac,
  24281. 0x68, 0x9d, 0xe0, 0x54, 0x17, 0xde, 0x1a, 0xed,
  24282. 0x57, 0xcb, 0xd9, 0x2d, 0xc8, 0xbc, 0x93, 0xe6,
  24283. 0xa3, 0xec, 0xde, 0xee, 0xa1, 0x1c, 0x41, 0x85
  24284. },
  24285. {
  24286. 0x7f, 0x1f, 0xe6, 0x7b, 0xd8, 0xf5, 0x2b, 0x37, 0xbe, 0xb7, 0xd0, 0x37,
  24287. 0xce, 0x46, 0xad, 0x04, 0x2f, 0xc7, 0xdb, 0xc9, 0x9a, 0xb6, 0x00, 0x3f,
  24288. 0xc1, 0x97, 0xe9, 0x5c, 0x5e, 0x14, 0xd1, 0x38, 0x4d, 0x55, 0xe1, 0x07,
  24289. 0xb5, 0x85, 0x6d, 0xfa, 0xa7, 0x66, 0xad, 0xfa, 0xb6, 0xad, 0x29, 0x44
  24290. },
  24291. {
  24292. 0x4e, 0x6b, 0x20, 0x99, 0x55, 0x1b, 0x21, 0x89, 0xb6, 0x70, 0xdb, 0xe8,
  24293. 0xa7, 0x16, 0x55, 0xf2, 0x93, 0x13, 0x90, 0x7d, 0xfa, 0x62, 0x65, 0x53,
  24294. 0xa0, 0x97, 0xe9, 0xb4, 0xc0, 0xf1, 0xc9, 0x1a, 0x67, 0xdd, 0xca, 0x57,
  24295. 0xbc, 0xca, 0x39, 0xe6, 0x39, 0x6b, 0x63, 0x47, 0x25, 0x08, 0x3a, 0xd7
  24296. },
  24297. {
  24298. 0x35, 0x0d, 0xac, 0xd8, 0x10, 0x6a, 0x46, 0x50, 0x66, 0xae, 0x02, 0xc9,
  24299. 0xde, 0x13, 0x48, 0xce, 0x53, 0xd4, 0x92, 0x62, 0xc5, 0x65, 0x10, 0x08,
  24300. 0xc2, 0xc2, 0x82, 0xed, 0x9d, 0xc9, 0x6f, 0xa8, 0xc3, 0xc1, 0x0b, 0x7c,
  24301. 0xe1, 0x97, 0x85, 0xd6, 0x46, 0x29, 0x0e, 0x42, 0x51, 0xc1, 0x35, 0xcf
  24302. },
  24303. {
  24304. 0x3d, 0x5d, 0x84, 0xbd, 0x16, 0x46, 0x34, 0xb3, 0xf6, 0x31, 0x49, 0x3e,
  24305. 0x8d, 0xdc, 0xcb, 0x8c, 0x6a, 0x42, 0xf4, 0x88, 0xfc, 0x19, 0xfa, 0xa2,
  24306. 0x25, 0xc7, 0xa0, 0xa4, 0xca, 0xf0, 0xea, 0x2d, 0xe8, 0xc4, 0x02, 0x14,
  24307. 0x63, 0xfb, 0xd3, 0x7b, 0x51, 0x1c, 0xce, 0xca, 0xa3, 0xc3, 0xe4, 0xa5
  24308. },
  24309. {
  24310. 0x7c, 0x3a, 0x55, 0x92, 0x2e, 0xdd, 0x75, 0xdd, 0x76, 0x54, 0x4a, 0x9f,
  24311. 0xd0, 0xa2, 0x88, 0x83, 0xe9, 0x27, 0xda, 0x30, 0xe9, 0x96, 0x58, 0xc5,
  24312. 0xb7, 0x56, 0xfc, 0x4b, 0xb8, 0x5d, 0xee, 0x46, 0x70, 0x4e, 0x1b, 0x06,
  24313. 0x86, 0xaf, 0x48, 0x5c, 0x17, 0x35, 0xfa, 0x69, 0xc2, 0x4d, 0xfb, 0x09
  24314. },
  24315. {
  24316. 0x00, 0x0e, 0x28, 0x51, 0xc1, 0x7f, 0x41, 0x89, 0x6f, 0x9a, 0xca, 0x15,
  24317. 0xee, 0xed, 0x43, 0xca, 0x6d, 0x65, 0x6f, 0x51, 0x18, 0x6c, 0x08, 0x4b,
  24318. 0x77, 0xca, 0x75, 0xc4, 0xc3, 0xde, 0x29, 0x41, 0x8b, 0xaf, 0xa7, 0x1c,
  24319. 0x28, 0x37, 0xa0, 0xa0, 0x74, 0x8e, 0x09, 0x42, 0x7a, 0x1b, 0x68, 0xdb
  24320. },
  24321. {
  24322. 0x14, 0x8f, 0xab, 0x28, 0x64, 0xea, 0x45, 0x88, 0xdb, 0xc1, 0xc6, 0xa0,
  24323. 0x48, 0xdf, 0x15, 0xd0, 0x28, 0x07, 0x2d, 0x6c, 0xb8, 0x42, 0xbb, 0x60,
  24324. 0x02, 0x08, 0x9e, 0x29, 0x9b, 0x8d, 0xd6, 0x1c, 0xaf, 0xf2, 0x1a, 0xdc,
  24325. 0xf0, 0x78, 0x0b, 0x4d, 0x90, 0xa1, 0x0c, 0xb3, 0x13, 0xde, 0xca, 0x5a
  24326. },
  24327. {
  24328. 0x4d, 0x80, 0x7d, 0x0b, 0xb9, 0x00, 0x6f, 0x65, 0x51, 0x65, 0x23, 0xde,
  24329. 0x72, 0xdc, 0x4f, 0x04, 0xa5, 0xa2, 0x90, 0x45, 0x51, 0x9e, 0xd0, 0x3a,
  24330. 0xe4, 0xd7, 0x78, 0xa3, 0x0f, 0x2d, 0x65, 0x12, 0xad, 0xc8, 0x92, 0x30,
  24331. 0x79, 0x9d, 0x9d, 0x08, 0x7a, 0x9c, 0x9f, 0x83, 0xb1, 0xca, 0x59, 0x56
  24332. }
  24333. },
  24334. { /* 41 */
  24335. WC_HASH_TYPE_SHA384, 33, 0,
  24336. { /* PSK */
  24337. 0xa4, 0x8b, 0x1b, 0x5f, 0xd0, 0xea, 0x75, 0x62,
  24338. 0x06, 0x4d, 0x68, 0x40, 0x85, 0x20, 0x45, 0x95,
  24339. 0x4a, 0x00, 0xca, 0x05, 0xeb, 0xd4, 0x1d, 0x48,
  24340. 0x81, 0x89, 0xe8, 0x86, 0x43, 0xfa, 0x28, 0x17,
  24341. 0x12
  24342. },
  24343. { 0 }, /* DHE */
  24344. { /* Hello 1 */
  24345. 0x03, 0x7c, 0x33, 0x75, 0xdc, 0xc5, 0x46, 0x3a,
  24346. 0x0d, 0x56, 0xc6, 0xfb, 0xab, 0x1e, 0x1d, 0xda,
  24347. 0x59, 0xc2, 0xb2, 0xb1, 0x7c, 0x48, 0x9b, 0x06,
  24348. 0x0a, 0x5a, 0xbb, 0xf8, 0x98, 0x53, 0x78, 0x2d,
  24349. 0xd2, 0xcc, 0x87, 0x68, 0x25, 0xdd, 0x88, 0x22,
  24350. 0xcd, 0xb7, 0x74, 0x55, 0x21, 0xf9, 0x34, 0x98
  24351. },
  24352. { /* Hello 2 */
  24353. 0x03, 0xb4, 0xfb, 0xcc, 0x28, 0x2c, 0xc1, 0x70,
  24354. 0x42, 0x73, 0x57, 0xac, 0xdb, 0x47, 0x71, 0xf6,
  24355. 0x2e, 0x11, 0x8a, 0x5b, 0x47, 0x2f, 0x02, 0x54,
  24356. 0x95, 0x34, 0xed, 0x5f, 0x19, 0xc1, 0x75, 0xe0,
  24357. 0x76, 0xad, 0xb0, 0x90, 0x57, 0xcd, 0xfd, 0xd7,
  24358. 0x58, 0x1f, 0x0d, 0x6b, 0x9e, 0x51, 0x3c, 0x08
  24359. },
  24360. { /* Finished 1 */
  24361. 0x2b, 0x50, 0xd9, 0xa7, 0x43, 0x24, 0xda, 0x2c,
  24362. 0x7a, 0xaa, 0x0e, 0x37, 0xd7, 0x6b, 0x2c, 0xab,
  24363. 0x8e, 0xb2, 0xfe, 0x31, 0x1b, 0xa8, 0x12, 0x59,
  24364. 0x5b, 0x7b, 0xdc, 0x3e, 0xa7, 0x86, 0xa5, 0x48,
  24365. 0xe4, 0x46, 0x2b, 0x4c, 0xc1, 0x66, 0x4b, 0xf3,
  24366. 0x2a, 0x99, 0x93, 0x08, 0xbc, 0x3d, 0x08, 0x76
  24367. },
  24368. { /* Finished 2 */
  24369. 0x7c, 0x34, 0xc8, 0x56, 0x17, 0xf1, 0x62, 0x1c,
  24370. 0x9f, 0x0b, 0xeb, 0xfd, 0x69, 0x72, 0x51, 0xc5,
  24371. 0xfa, 0x74, 0x87, 0xc9, 0xbd, 0x50, 0xe9, 0x48,
  24372. 0xa7, 0x3c, 0x94, 0x3e, 0x06, 0x7d, 0xe8, 0x8e,
  24373. 0xc1, 0xd1, 0x08, 0x1f, 0x5d, 0x48, 0x8a, 0x25,
  24374. 0xfc, 0xea, 0xe7, 0xd9, 0xd4, 0xd0, 0xf9, 0xad
  24375. },
  24376. {
  24377. 0x4b, 0x0b, 0xed, 0xb9, 0xc8, 0xb8, 0xa8, 0x1e, 0xb0, 0x81, 0x76, 0xd5,
  24378. 0x33, 0x22, 0x71, 0x33, 0x3a, 0x85, 0x19, 0x67, 0x7e, 0x91, 0x37, 0xf2,
  24379. 0xa6, 0x11, 0x22, 0xdf, 0x41, 0x04, 0x3d, 0xa9, 0x13, 0xb9, 0xb2, 0xb1,
  24380. 0xbb, 0xd8, 0xef, 0x23, 0x7c, 0xc2, 0xab, 0x70, 0x1b, 0x51, 0x9f, 0xc9
  24381. },
  24382. {
  24383. 0xeb, 0x96, 0x10, 0x8c, 0x7d, 0x92, 0xea, 0x80, 0x86, 0xb2, 0xf8, 0x27,
  24384. 0xf2, 0x9a, 0x09, 0xc1, 0x7c, 0x09, 0x43, 0xbc, 0xfe, 0xc8, 0x75, 0xe0,
  24385. 0x97, 0xe7, 0x6d, 0xd5, 0xb2, 0x3c, 0xed, 0x12, 0xb7, 0x74, 0x0e, 0xe3,
  24386. 0xb6, 0xe0, 0xba, 0xe1, 0x8d, 0x89, 0xcf, 0x4f, 0x57, 0xf6, 0x6d, 0x90
  24387. },
  24388. {
  24389. 0x22, 0xb0, 0x39, 0x34, 0xb6, 0x6c, 0x2d, 0x7a, 0x97, 0x1c, 0x5d, 0xcc,
  24390. 0x78, 0x84, 0x71, 0xbb, 0xc6, 0x7b, 0xb6, 0xbc, 0xcc, 0x0b, 0xf8, 0xac,
  24391. 0x8e, 0xd7, 0x20, 0xbd, 0xbe, 0x32, 0xf0, 0xd6, 0xe9, 0x69, 0x13, 0xf2,
  24392. 0x9a, 0xce, 0xfe, 0x86, 0xd3, 0xee, 0xba, 0x69, 0x51, 0xb6, 0x77, 0x56
  24393. },
  24394. {
  24395. 0x16, 0xfd, 0xda, 0xf3, 0x5e, 0xb9, 0xa6, 0x17, 0x24, 0xb2, 0x16, 0x9f,
  24396. 0xb6, 0x59, 0x13, 0x0f, 0x25, 0x5a, 0xf1, 0x5b, 0x5f, 0xe4, 0x54, 0x2a,
  24397. 0xa7, 0xbf, 0x29, 0xaf, 0x5a, 0x77, 0xf4, 0x4f, 0x25, 0xba, 0x94, 0xad,
  24398. 0x6b, 0x91, 0x3b, 0xe7, 0xd5, 0x73, 0x0d, 0xff, 0xaa, 0xe3, 0x72, 0x2c
  24399. },
  24400. {
  24401. 0x22, 0xb4, 0x94, 0xc0, 0x53, 0xd7, 0x82, 0x06, 0x38, 0x9d, 0x4a, 0xa0,
  24402. 0x3f, 0xf1, 0x5f, 0x6e, 0x23, 0x8d, 0x09, 0x62, 0xbf, 0x6f, 0x7c, 0x84,
  24403. 0xc6, 0x3e, 0x15, 0xad, 0x18, 0x37, 0x76, 0x29, 0xc7, 0xd6, 0x68, 0x0c,
  24404. 0x1e, 0xc6, 0x93, 0x31, 0xef, 0x85, 0x69, 0x30, 0x68, 0xf0, 0x1e, 0x37
  24405. },
  24406. {
  24407. 0x6d, 0x4d, 0x20, 0xaf, 0x47, 0xe8, 0x1b, 0xfa, 0xd0, 0xb6, 0xc8, 0x97,
  24408. 0xd1, 0x03, 0xfc, 0x9d, 0x59, 0xa0, 0x68, 0x9d, 0xe9, 0x17, 0x8b, 0xce,
  24409. 0x48, 0x2c, 0x77, 0x8a, 0x22, 0x4b, 0x5c, 0x54, 0x22, 0xa1, 0x15, 0x12,
  24410. 0xe1, 0x07, 0x8e, 0x15, 0xd8, 0x7b, 0x16, 0x65, 0x99, 0x6b, 0xcb, 0x71
  24411. },
  24412. {
  24413. 0x79, 0x64, 0x79, 0xdd, 0x75, 0x5c, 0x6f, 0x98, 0xac, 0x03, 0xe0, 0xcd,
  24414. 0x92, 0xba, 0x0e, 0x2d, 0xb4, 0xd1, 0x8b, 0x97, 0xd0, 0x85, 0xbb, 0x2e,
  24415. 0x4f, 0x26, 0x93, 0xf5, 0x1d, 0xf3, 0xd2, 0x43, 0x4f, 0xd2, 0x47, 0xaa,
  24416. 0x91, 0x1e, 0xf3, 0x67, 0x10, 0x18, 0x2c, 0xb9, 0x01, 0xba, 0x10, 0x9f
  24417. },
  24418. {
  24419. 0x79, 0xb6, 0x9c, 0xbe, 0xf1, 0x6a, 0xb0, 0x92, 0xa0, 0x29, 0x52, 0x61,
  24420. 0xf1, 0xcd, 0x3a, 0x67, 0xe1, 0x6b, 0xb8, 0x9d, 0x0d, 0x95, 0xb6, 0x03,
  24421. 0x80, 0x1f, 0xd5, 0x75, 0xb6, 0x1d, 0x79, 0x02, 0x93, 0x43, 0x77, 0xa7,
  24422. 0x9d, 0x2f, 0xc3, 0x84, 0xc6, 0x83, 0x76, 0x16, 0x06, 0x98, 0x7b, 0x79
  24423. }
  24424. },
  24425. #endif /* WOLFSSL_SHA384 */
  24426. };
  24427. static const char protocolLabel[] = "tls13 ";
  24428. static const char ceTrafficLabel[] = "c e traffic";
  24429. static const char eExpMasterLabel[] = "e exp master";
  24430. static const char cHsTrafficLabel[] = "c hs traffic";
  24431. static const char sHsTrafficLabel[] = "s hs traffic";
  24432. static const char cAppTrafficLabel[] = "c ap traffic";
  24433. static const char sAppTrafficLabel[] = "s ap traffic";
  24434. static const char expMasterLabel[] = "exp master";
  24435. static const char resMasterLabel[] = "res master";
  24436. static const char derivedLabel[] = "derived";
  24437. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t tls13_kdf_test(void)
  24438. {
  24439. wc_test_ret_t ret = 0;
  24440. word32 i;
  24441. word32 tc = sizeof(tls13KdfTestVectors)/sizeof(Tls13KdfTestVector);
  24442. const Tls13KdfTestVector* tv = NULL;
  24443. WOLFSSL_ENTER("tls13_kdf_test");
  24444. for (i = 0, tv = tls13KdfTestVectors; i < tc; i++, tv++) {
  24445. byte output[WC_MAX_DIGEST_SIZE];
  24446. byte secret[WC_MAX_DIGEST_SIZE];
  24447. byte salt[WC_MAX_DIGEST_SIZE];
  24448. byte zeroes[WC_MAX_DIGEST_SIZE];
  24449. byte hashZero[WC_MAX_DIGEST_SIZE];
  24450. int hashAlgSz;
  24451. XMEMSET(zeroes, 0, sizeof zeroes);
  24452. hashAlgSz = wc_HashGetDigestSize(tv->hashAlg);
  24453. if (hashAlgSz == BAD_FUNC_ARG) break;
  24454. ret = wc_Hash(tv->hashAlg, NULL, 0, hashZero, (word32)hashAlgSz);
  24455. if (ret != 0) break;
  24456. ret = wc_Tls13_HKDF_Extract(secret, NULL, 0,
  24457. (tv->pskSz == 0) ? zeroes : (byte*)tv->psk,
  24458. tv->pskSz, tv->hashAlg);
  24459. if (ret != 0) break;
  24460. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24461. secret, (word32)hashAlgSz,
  24462. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24463. (byte*)ceTrafficLabel, (word32)XSTRLEN(ceTrafficLabel),
  24464. tv->hashHello1, (word32)hashAlgSz, tv->hashAlg);
  24465. if (ret != 0) break;
  24466. ret = XMEMCMP(tv->clientEarlyTrafficSecret, output, hashAlgSz);
  24467. if (ret != 0) break;
  24468. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24469. secret, (word32)hashAlgSz,
  24470. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24471. (byte*)eExpMasterLabel, (word32)XSTRLEN(eExpMasterLabel),
  24472. tv->hashHello1, (word32)hashAlgSz, tv->hashAlg);
  24473. if (ret != 0) break;
  24474. ret = XMEMCMP(tv->earlyExporterMasterSecret, output, hashAlgSz);
  24475. if (ret != 0) break;
  24476. ret = wc_Tls13_HKDF_Expand_Label(salt, (word32)hashAlgSz,
  24477. secret, (word32)hashAlgSz,
  24478. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24479. (byte*)derivedLabel, (word32)XSTRLEN(derivedLabel),
  24480. hashZero, (word32)hashAlgSz, tv->hashAlg);
  24481. if (ret != 0) break;
  24482. ret = wc_Tls13_HKDF_Extract(secret, salt, (word32)(word32)hashAlgSz,
  24483. (tv->dheSz == 0) ? zeroes : (byte*)tv->dhe,
  24484. tv->dheSz, tv->hashAlg);
  24485. if (ret != 0) break;
  24486. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24487. secret, (word32)hashAlgSz,
  24488. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24489. (byte*)cHsTrafficLabel, (word32)XSTRLEN(cHsTrafficLabel),
  24490. tv->hashHello2, (word32)hashAlgSz, tv->hashAlg);
  24491. if (ret != 0) break;
  24492. ret = XMEMCMP(tv->clientHandshakeTrafficSecret,
  24493. output, hashAlgSz);
  24494. if (ret != 0) break;
  24495. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24496. secret, (word32)hashAlgSz,
  24497. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24498. (byte*)sHsTrafficLabel, (word32)XSTRLEN(sHsTrafficLabel),
  24499. tv->hashHello2, (word32)hashAlgSz, tv->hashAlg);
  24500. if (ret != 0) break;
  24501. ret = XMEMCMP(tv->serverHandshakeTrafficSecret, output, hashAlgSz);
  24502. if (ret != 0) break;
  24503. ret = wc_Tls13_HKDF_Expand_Label(salt, (word32)hashAlgSz,
  24504. secret, (word32)hashAlgSz,
  24505. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24506. (byte*)derivedLabel, (word32)XSTRLEN(derivedLabel),
  24507. hashZero, (word32)hashAlgSz, tv->hashAlg);
  24508. if (ret != 0) break;
  24509. ret = wc_Tls13_HKDF_Extract(secret, salt, (word32)(word32)hashAlgSz,
  24510. zeroes, (word32)(word32)hashAlgSz, tv->hashAlg);
  24511. if (ret != 0) break;
  24512. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24513. secret, (word32)hashAlgSz,
  24514. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24515. (byte*)cAppTrafficLabel, (word32)XSTRLEN(cAppTrafficLabel),
  24516. tv->hashFinished1, (word32)hashAlgSz, tv->hashAlg);
  24517. if (ret != 0) break;
  24518. ret = XMEMCMP(tv->clientApplicationTrafficSecret, output, hashAlgSz);
  24519. if (ret != 0) break;
  24520. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24521. secret, (word32)hashAlgSz,
  24522. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24523. (byte*)sAppTrafficLabel, (word32)XSTRLEN(sAppTrafficLabel),
  24524. tv->hashFinished1, (word32)hashAlgSz, tv->hashAlg);
  24525. if (ret != 0) break;
  24526. ret = XMEMCMP(tv->serverApplicationTrafficSecret, output, hashAlgSz);
  24527. if (ret != 0) break;
  24528. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24529. secret, (word32)hashAlgSz,
  24530. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24531. (byte*)expMasterLabel, (word32)XSTRLEN(expMasterLabel),
  24532. tv->hashFinished1, (word32)hashAlgSz, tv->hashAlg);
  24533. if (ret != 0) break;
  24534. ret = XMEMCMP(tv->exporterMasterSecret, output, hashAlgSz);
  24535. if (ret != 0) break;
  24536. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24537. secret, (word32)hashAlgSz,
  24538. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24539. (byte*)resMasterLabel, (word32)XSTRLEN(resMasterLabel),
  24540. tv->hashFinished2, (word32)hashAlgSz, tv->hashAlg);
  24541. if (ret != 0) break;
  24542. ret = XMEMCMP(tv->resumptionMasterSecret, output, hashAlgSz);
  24543. if (ret != 0) break;
  24544. }
  24545. return ret;
  24546. }
  24547. #endif /* WOLFSSL_TLS13 */
  24548. static const int fiducial2 = WC_TEST_RET_LN; /* source code reference point --
  24549. * see print_fiducials() below.
  24550. */
  24551. #if defined(HAVE_ECC) && defined(HAVE_X963_KDF)
  24552. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t x963kdf_test(void)
  24553. {
  24554. wc_test_ret_t ret;
  24555. byte kek[128];
  24556. #ifndef NO_SHA
  24557. /* SHA-1, COUNT = 0
  24558. * shared secret length: 192
  24559. * SharedInfo length: 0
  24560. * key data length: 128
  24561. */
  24562. WOLFSSL_SMALL_STACK_STATIC const byte Z[] = {
  24563. 0x1c, 0x7d, 0x7b, 0x5f, 0x05, 0x97, 0xb0, 0x3d,
  24564. 0x06, 0xa0, 0x18, 0x46, 0x6e, 0xd1, 0xa9, 0x3e,
  24565. 0x30, 0xed, 0x4b, 0x04, 0xdc, 0x64, 0xcc, 0xdd
  24566. };
  24567. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  24568. 0xbf, 0x71, 0xdf, 0xfd, 0x8f, 0x4d, 0x99, 0x22,
  24569. 0x39, 0x36, 0xbe, 0xb4, 0x6f, 0xee, 0x8c, 0xcc
  24570. };
  24571. #endif
  24572. #ifndef NO_SHA256
  24573. /* SHA-256, COUNT = 3
  24574. * shared secret length: 192
  24575. * SharedInfo length: 0
  24576. * key data length: 128
  24577. */
  24578. WOLFSSL_SMALL_STACK_STATIC const byte Z2[] = {
  24579. 0xd3, 0x8b, 0xdb, 0xe5, 0xc4, 0xfc, 0x16, 0x4c,
  24580. 0xdd, 0x96, 0x7f, 0x63, 0xc0, 0x4f, 0xe0, 0x7b,
  24581. 0x60, 0xcd, 0xe8, 0x81, 0xc2, 0x46, 0x43, 0x8c
  24582. };
  24583. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  24584. 0x5e, 0x67, 0x4d, 0xb9, 0x71, 0xba, 0xc2, 0x0a,
  24585. 0x80, 0xba, 0xd0, 0xd4, 0x51, 0x4d, 0xc4, 0x84
  24586. };
  24587. #endif
  24588. #ifdef WOLFSSL_SHA512
  24589. /* SHA-512, COUNT = 0
  24590. * shared secret length: 192
  24591. * SharedInfo length: 0
  24592. * key data length: 128
  24593. */
  24594. WOLFSSL_SMALL_STACK_STATIC const byte Z3[] = {
  24595. 0x87, 0xfc, 0x0d, 0x8c, 0x44, 0x77, 0x48, 0x5b,
  24596. 0xb5, 0x74, 0xf5, 0xfc, 0xea, 0x26, 0x4b, 0x30,
  24597. 0x88, 0x5d, 0xc8, 0xd9, 0x0a, 0xd8, 0x27, 0x82
  24598. };
  24599. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  24600. 0x94, 0x76, 0x65, 0xfb, 0xb9, 0x15, 0x21, 0x53,
  24601. 0xef, 0x46, 0x02, 0x38, 0x50, 0x6a, 0x02, 0x45
  24602. };
  24603. /* SHA-512, COUNT = 0
  24604. * shared secret length: 521
  24605. * SharedInfo length: 128
  24606. * key data length: 1024
  24607. */
  24608. WOLFSSL_SMALL_STACK_STATIC const byte Z4[] = {
  24609. 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89,
  24610. 0xfa, 0x58, 0xce, 0xad, 0xc0, 0x47, 0x19, 0x7f,
  24611. 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
  24612. 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8,
  24613. 0x15, 0x07, 0x39, 0x2f, 0x1a, 0x86, 0xdd, 0xfd,
  24614. 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
  24615. 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47,
  24616. 0x47, 0xa9, 0xe2, 0xe7, 0x53, 0xf5, 0x5e, 0xf0,
  24617. 0x5a, 0x2d
  24618. };
  24619. WOLFSSL_SMALL_STACK_STATIC const byte info4[] = {
  24620. 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d,
  24621. 0x2b, 0x3a, 0x2f, 0x99, 0x37, 0x89, 0x5d, 0x31
  24622. };
  24623. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  24624. 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76,
  24625. 0x9b, 0x52, 0x26, 0x4b, 0x01, 0x12, 0xb5, 0x85,
  24626. 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
  24627. 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d,
  24628. 0x6e, 0x4d, 0xd2, 0xa5, 0x99, 0xac, 0xce, 0xb3,
  24629. 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
  24630. 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50,
  24631. 0xa5, 0xa8, 0xee, 0xee, 0x20, 0x80, 0x02, 0x26,
  24632. 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
  24633. 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d,
  24634. 0xea, 0x9c, 0x4f, 0xf7, 0x45, 0xa2, 0x5a, 0xc2,
  24635. 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
  24636. 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc,
  24637. 0x0a, 0xb2, 0xaa, 0x8e, 0x2e, 0xfa, 0x7b, 0x17,
  24638. 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
  24639. 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
  24640. };
  24641. #endif
  24642. WOLFSSL_ENTER("x963kdf_test");
  24643. #ifndef NO_SHA
  24644. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, Z, sizeof(Z), NULL, 0,
  24645. kek, sizeof(verify));
  24646. if (ret != 0)
  24647. return WC_TEST_RET_ENC_EC(ret);
  24648. if (XMEMCMP(verify, kek, sizeof(verify)) != 0)
  24649. return WC_TEST_RET_ENC_NC;
  24650. #endif
  24651. #ifndef NO_SHA256
  24652. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, Z2, sizeof(Z2), NULL, 0,
  24653. kek, sizeof(verify2));
  24654. if (ret != 0)
  24655. return WC_TEST_RET_ENC_EC(ret);
  24656. if (XMEMCMP(verify2, kek, sizeof(verify2)) != 0)
  24657. return WC_TEST_RET_ENC_NC;
  24658. #endif
  24659. #ifdef WOLFSSL_SHA512
  24660. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z3, sizeof(Z3), NULL, 0,
  24661. kek, sizeof(verify3));
  24662. if (ret != 0)
  24663. return WC_TEST_RET_ENC_EC(ret);
  24664. if (XMEMCMP(verify3, kek, sizeof(verify3)) != 0)
  24665. return WC_TEST_RET_ENC_NC;
  24666. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z4, sizeof(Z4), info4,
  24667. sizeof(info4), kek, sizeof(verify4));
  24668. if (ret != 0)
  24669. return WC_TEST_RET_ENC_EC(ret);
  24670. if (XMEMCMP(verify4, kek, sizeof(verify4)) != 0)
  24671. return WC_TEST_RET_ENC_NC;
  24672. #endif
  24673. return 0;
  24674. }
  24675. #endif /* HAVE_X963_KDF */
  24676. #if defined(HAVE_HPKE) && \
  24677. (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)) && \
  24678. defined(HAVE_AESGCM)
  24679. static wc_test_ret_t hpke_test_single(Hpke* hpke)
  24680. {
  24681. wc_test_ret_t ret = 0;
  24682. int rngRet = 0;
  24683. WC_RNG rng[1];
  24684. const char* start_text = "this is a test";
  24685. const char* info_text = "info";
  24686. const char* aad_text = "aad";
  24687. byte ciphertext[MAX_HPKE_LABEL_SZ];
  24688. byte plaintext[MAX_HPKE_LABEL_SZ];
  24689. void* receiverKey = NULL;
  24690. void* ephemeralKey = NULL;
  24691. #ifdef WOLFSSL_SMALL_STACK
  24692. byte *pubKey = NULL; /* public key */
  24693. word16 pubKeySz = (word16)HPKE_Npk_MAX;
  24694. #else
  24695. byte pubKey[HPKE_Npk_MAX]; /* public key */
  24696. word16 pubKeySz = (word16)sizeof(pubKey);
  24697. #endif
  24698. rngRet = ret = wc_InitRng(rng);
  24699. if (ret != 0)
  24700. return WC_TEST_RET_ENC_EC(ret);
  24701. #ifdef WOLFSSL_SMALL_STACK
  24702. if (ret == 0) {
  24703. pubKey = (byte *)XMALLOC(pubKeySz, HEAP_HINT,
  24704. DYNAMIC_TYPE_TMP_BUFFER);
  24705. if (pubKey == NULL)
  24706. ret = WC_TEST_RET_ENC_EC(MEMORY_E);
  24707. }
  24708. #endif
  24709. /* generate the keys */
  24710. if (ret == 0) {
  24711. ret = wc_HpkeGenerateKeyPair(hpke, &ephemeralKey, rng);
  24712. if (ret != 0)
  24713. ret = WC_TEST_RET_ENC_EC(ret);
  24714. }
  24715. if (ret == 0) {
  24716. ret = wc_HpkeGenerateKeyPair(hpke, &receiverKey, rng);
  24717. if (ret != 0)
  24718. ret = WC_TEST_RET_ENC_EC(ret);
  24719. }
  24720. /* seal */
  24721. if (ret == 0) {
  24722. ret = wc_HpkeSealBase(hpke, ephemeralKey, receiverKey,
  24723. (byte*)info_text, (word32)XSTRLEN(info_text),
  24724. (byte*)aad_text, (word32)XSTRLEN(aad_text),
  24725. (byte*)start_text, (word32)XSTRLEN(start_text),
  24726. ciphertext);
  24727. if (ret != 0)
  24728. ret = WC_TEST_RET_ENC_EC(ret);
  24729. }
  24730. /* export ephemeral key */
  24731. if (ret == 0) {
  24732. ret = wc_HpkeSerializePublicKey(hpke, ephemeralKey, pubKey, &pubKeySz);
  24733. if (ret != 0)
  24734. ret = WC_TEST_RET_ENC_EC(ret);
  24735. }
  24736. /* open with exported ephemeral key */
  24737. if (ret == 0) {
  24738. ret = wc_HpkeOpenBase(hpke, receiverKey, pubKey, pubKeySz,
  24739. (byte*)info_text, (word32)XSTRLEN(info_text),
  24740. (byte*)aad_text, (word32)XSTRLEN(aad_text),
  24741. ciphertext, (word32)XSTRLEN(start_text),
  24742. plaintext);
  24743. if (ret != 0)
  24744. ret = WC_TEST_RET_ENC_EC(ret);
  24745. }
  24746. if (ret == 0) {
  24747. ret = XMEMCMP(plaintext, start_text, XSTRLEN(start_text));
  24748. if (ret != 0)
  24749. ret = WC_TEST_RET_ENC_NC;
  24750. }
  24751. /* Negative test case with NULL argument */
  24752. if (ret == 0) {
  24753. ret = wc_HpkeGenerateKeyPair(NULL, &receiverKey, rng);
  24754. if (ret != BAD_FUNC_ARG)
  24755. ret = WC_TEST_RET_ENC_EC(ret);
  24756. else
  24757. ret = 0;
  24758. }
  24759. if (ret == 0) {
  24760. ret = wc_HpkeGenerateKeyPair(hpke, NULL, rng);
  24761. if (ret != BAD_FUNC_ARG)
  24762. ret = WC_TEST_RET_ENC_EC(ret);
  24763. else
  24764. ret = 0;
  24765. }
  24766. if (ret == 0) {
  24767. ret = wc_HpkeGenerateKeyPair(hpke, &receiverKey, NULL);
  24768. if (ret != BAD_FUNC_ARG)
  24769. ret = WC_TEST_RET_ENC_EC(ret);
  24770. else
  24771. ret = 0;
  24772. }
  24773. if (ephemeralKey != NULL)
  24774. wc_HpkeFreeKey(hpke, hpke->kem, ephemeralKey, hpke->heap);
  24775. if (receiverKey != NULL)
  24776. wc_HpkeFreeKey(hpke, hpke->kem, receiverKey, hpke->heap);
  24777. #ifdef WOLFSSL_SMALL_STACK
  24778. if (pubKey != NULL)
  24779. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24780. #endif
  24781. if (rngRet == 0)
  24782. wc_FreeRng(rng);
  24783. return ret;
  24784. }
  24785. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hpke_test(void)
  24786. {
  24787. wc_test_ret_t ret = 0;
  24788. Hpke hpke[1];
  24789. WOLFSSL_ENTER("hpke_test");
  24790. #if defined(HAVE_ECC)
  24791. #if defined(WOLFSSL_SHA224) || !defined(NO_SHA256)
  24792. /* p256 */
  24793. ret = wc_HpkeInit(hpke, DHKEM_P256_HKDF_SHA256, HKDF_SHA256,
  24794. HPKE_AES_128_GCM, NULL);
  24795. if (ret != 0)
  24796. return WC_TEST_RET_ENC_EC(ret);
  24797. ret = hpke_test_single(hpke);
  24798. if (ret != 0)
  24799. return ret;
  24800. #endif
  24801. #if defined(WOLFSSL_SHA384) && \
  24802. (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  24803. /* p384 */
  24804. ret = wc_HpkeInit(hpke, DHKEM_P384_HKDF_SHA384, HKDF_SHA384,
  24805. HPKE_AES_128_GCM, NULL);
  24806. if (ret != 0)
  24807. return WC_TEST_RET_ENC_EC(ret);
  24808. ret = hpke_test_single(hpke);
  24809. if (ret != 0)
  24810. return ret;
  24811. #endif
  24812. #if (defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)) && \
  24813. (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  24814. /* p521 */
  24815. ret = wc_HpkeInit(hpke, DHKEM_P521_HKDF_SHA512, HKDF_SHA512,
  24816. HPKE_AES_128_GCM, NULL);
  24817. if (ret != 0)
  24818. return WC_TEST_RET_ENC_EC(ret);
  24819. ret = hpke_test_single(hpke);
  24820. if (ret != 0)
  24821. return ret;
  24822. #endif
  24823. #endif
  24824. #if defined(HAVE_CURVE25519)
  24825. /* test with curve25519 and aes256 */
  24826. ret = wc_HpkeInit(hpke, DHKEM_X25519_HKDF_SHA256, HKDF_SHA256,
  24827. HPKE_AES_256_GCM, NULL);
  24828. if (ret != 0)
  24829. return WC_TEST_RET_ENC_EC(ret);
  24830. ret = hpke_test_single(hpke);
  24831. if (ret != 0)
  24832. return ret;
  24833. #endif
  24834. #if defined(HAVE_CURVE448) && \
  24835. (defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512))
  24836. /* test with curve448 and aes256 */
  24837. ret = wc_HpkeInit(hpke, DHKEM_X448_HKDF_SHA512, HKDF_SHA512,
  24838. HPKE_AES_256_GCM, NULL);
  24839. /* HPKE does not support X448 yet, so expect failure */
  24840. if (ret != BAD_FUNC_ARG)
  24841. return WC_TEST_RET_ENC_EC(ret);
  24842. ret = hpke_test_single(hpke);
  24843. /* HPKE does not support X448 yet, so expect failure */
  24844. if (WC_TEST_RET_DEC_EC(ret) != BAD_FUNC_ARG)
  24845. return ret;
  24846. ret = 0; /* reset error code */
  24847. #endif
  24848. /* TODO: HPKE chacha20 is not implemented */
  24849. return ret;
  24850. }
  24851. #endif /* HAVE_HPKE && HAVE_ECC && HAVE_AESGCM */
  24852. #if defined(WC_SRTP_KDF)
  24853. typedef struct Srtp_Kdf_Tv {
  24854. const unsigned char* key;
  24855. word32 keySz;
  24856. const unsigned char* salt;
  24857. word32 saltSz;
  24858. int kdfIdx;
  24859. const unsigned char* index;
  24860. const unsigned char* ke;
  24861. const unsigned char* ka;
  24862. const unsigned char* ks;
  24863. const unsigned char* index_c;
  24864. const unsigned char* ke_c;
  24865. const unsigned char* ka_c;
  24866. const unsigned char* ks_c;
  24867. word32 keSz;
  24868. word32 kaSz;
  24869. word32 ksSz;
  24870. } Srtp_Kdf_Tv;
  24871. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srtpkdf_test(void)
  24872. {
  24873. wc_test_ret_t ret = 0;
  24874. /* 128-bit key, kdrIdx = -1 */
  24875. WOLFSSL_SMALL_STACK_STATIC const byte key_0[] = {
  24876. 0xc4, 0x80, 0x9f, 0x6d, 0x36, 0x98, 0x88, 0x72,
  24877. 0x8e, 0x26, 0xad, 0xb5, 0x32, 0x12, 0x98, 0x90
  24878. };
  24879. WOLFSSL_SMALL_STACK_STATIC const byte salt_0[] = {
  24880. 0x0e, 0x23, 0x00, 0x6c, 0x6c, 0x04, 0x4f, 0x56,
  24881. 0x62, 0x40, 0x0e, 0x9d, 0x1b, 0xd6
  24882. };
  24883. WOLFSSL_SMALL_STACK_STATIC const byte index_0[] = {
  24884. 0x48, 0x71, 0x65, 0x64, 0x9c, 0xca
  24885. };
  24886. WOLFSSL_SMALL_STACK_STATIC const byte ke_0[] = {
  24887. 0xdc, 0x38, 0x21, 0x92, 0xab, 0x65, 0x10, 0x8a,
  24888. 0x86, 0xb2, 0x59, 0xb6, 0x1b, 0x3a, 0xf4, 0x6f
  24889. };
  24890. WOLFSSL_SMALL_STACK_STATIC const byte ka_0[] = {
  24891. 0xb8, 0x39, 0x37, 0xfb, 0x32, 0x17, 0x92, 0xee,
  24892. 0x87, 0xb7, 0x88, 0x19, 0x3b, 0xe5, 0xa4, 0xe3,
  24893. 0xbd, 0x32, 0x6e, 0xe4
  24894. };
  24895. WOLFSSL_SMALL_STACK_STATIC const byte ks_0[] = {
  24896. 0xf1, 0xc0, 0x35, 0xc0, 0x0b, 0x5a, 0x54, 0xa6,
  24897. 0x16, 0x92, 0xc0, 0x16, 0x27, 0x6c
  24898. };
  24899. WOLFSSL_SMALL_STACK_STATIC const byte index_c_0[] = {
  24900. 0x56, 0xf3, 0xf1, 0x97
  24901. };
  24902. WOLFSSL_SMALL_STACK_STATIC const byte ke_c_0[] = {
  24903. 0xab, 0x5b, 0xe0, 0xb4, 0x56, 0x23, 0x5d, 0xcf,
  24904. 0x77, 0xd5, 0x08, 0x69, 0x29, 0xba, 0xfb, 0x38
  24905. };
  24906. WOLFSSL_SMALL_STACK_STATIC const byte ka_c_0[] = {
  24907. 0xc5, 0x2f, 0xde, 0x0b, 0x80, 0xb0, 0xf0, 0xba,
  24908. 0xd8, 0xd1, 0x56, 0x45, 0xcb, 0x86, 0xe7, 0xc7,
  24909. 0xc3, 0xd8, 0x77, 0x0e
  24910. };
  24911. WOLFSSL_SMALL_STACK_STATIC const byte ks_c_0[] = {
  24912. 0xde, 0xb5, 0xf8, 0x5f, 0x81, 0x33, 0x6a, 0x96,
  24913. 0x5e, 0xd3, 0x2b, 0xb7, 0xed, 0xe8
  24914. };
  24915. /* 192-bit key, kdrIdx = 0 */
  24916. WOLFSSL_SMALL_STACK_STATIC const byte key_1[] = {
  24917. 0xbb, 0x04, 0x5b, 0x1f, 0x53, 0xc6, 0x93, 0x2c,
  24918. 0x2b, 0xa6, 0x88, 0xf5, 0xe3, 0xf2, 0x24, 0x70,
  24919. 0xe1, 0x7d, 0x7d, 0xec, 0x8a, 0x93, 0x4d, 0xf2
  24920. };
  24921. WOLFSSL_SMALL_STACK_STATIC const byte salt_1[] = {
  24922. 0xe7, 0x22, 0xab, 0x92, 0xfc, 0x7c, 0x89, 0xb6,
  24923. 0x53, 0x8a, 0xf9, 0x3c, 0xb9, 0x52
  24924. };
  24925. WOLFSSL_SMALL_STACK_STATIC const byte index_1[] = {
  24926. 0xd7, 0x87, 0x8f, 0x33, 0xb1, 0x76
  24927. };
  24928. WOLFSSL_SMALL_STACK_STATIC const byte ke_1[] = {
  24929. 0x2c, 0xc8, 0x3e, 0x54, 0xb2, 0x33, 0x89, 0xb3,
  24930. 0x71, 0x65, 0x0f, 0x51, 0x61, 0x65, 0xe4, 0x93,
  24931. 0x07, 0x4e, 0xb3, 0x47, 0xba, 0x2d, 0x60, 0x60
  24932. };
  24933. WOLFSSL_SMALL_STACK_STATIC const byte ka_1[] = {
  24934. 0x2e, 0x80, 0xe4, 0x82, 0x55, 0xa2, 0xbe, 0x6d,
  24935. 0xe0, 0x46, 0xcc, 0xc1, 0x75, 0x78, 0x6e, 0x78,
  24936. 0xd1, 0xd1, 0x47, 0x08
  24937. };
  24938. WOLFSSL_SMALL_STACK_STATIC const byte ks_1[] = {
  24939. 0xe0, 0xc1, 0xe6, 0xaf, 0x1e, 0x8d, 0x8c, 0xfe,
  24940. 0xe5, 0x60, 0x70, 0xb5, 0xe6, 0xea
  24941. };
  24942. WOLFSSL_SMALL_STACK_STATIC const byte index_c_1[] = {
  24943. 0x40, 0xbf, 0xd4, 0xa9
  24944. };
  24945. WOLFSSL_SMALL_STACK_STATIC const byte ke_c_1[] = {
  24946. 0x94, 0x0f, 0x55, 0xce, 0x58, 0xd8, 0x16, 0x65,
  24947. 0xf0, 0xfa, 0x46, 0x40, 0x0c, 0xda, 0xb1, 0x11,
  24948. 0x9e, 0x69, 0xa0, 0x93, 0x4e, 0xd7, 0xf2, 0x84
  24949. };
  24950. WOLFSSL_SMALL_STACK_STATIC const byte ka_c_1[] = {
  24951. 0xf5, 0x41, 0x6f, 0xc2, 0x65, 0xc5, 0xb3, 0xef,
  24952. 0xbb, 0x22, 0xc8, 0xfc, 0x6b, 0x00, 0x14, 0xb2,
  24953. 0xf3, 0x3b, 0x8e, 0x29
  24954. };
  24955. WOLFSSL_SMALL_STACK_STATIC const byte ks_c_1[] = {
  24956. 0x35, 0xb7, 0x42, 0x43, 0xf0, 0x01, 0x01, 0xb4,
  24957. 0x68, 0xa1, 0x28, 0x80, 0x37, 0xf0
  24958. };
  24959. /* 256-bit key, kdrIdx = 1 */
  24960. WOLFSSL_SMALL_STACK_STATIC const byte key_2[] = {
  24961. 0x10, 0x38, 0x0a, 0xcd, 0xd6, 0x47, 0xab, 0xee,
  24962. 0xc0, 0xd4, 0x44, 0xf4, 0x7e, 0x51, 0x36, 0x02,
  24963. 0x79, 0xa8, 0x94, 0x80, 0x35, 0x40, 0xed, 0x50,
  24964. 0xf4, 0x45, 0x30, 0x3d, 0xb5, 0xf0, 0x2b, 0xbb
  24965. };
  24966. WOLFSSL_SMALL_STACK_STATIC const byte salt_2[] = {
  24967. 0xc7, 0x31, 0xf2, 0xc8, 0x40, 0x43, 0xb8, 0x74,
  24968. 0x8a, 0x61, 0x84, 0x7a, 0x25, 0x8a
  24969. };
  24970. WOLFSSL_SMALL_STACK_STATIC const byte index_2[] = {
  24971. 0x82, 0xf1, 0x84, 0x8c, 0xac, 0x42
  24972. };
  24973. WOLFSSL_SMALL_STACK_STATIC const byte ke_2[] = {
  24974. 0xb2, 0x26, 0x60, 0xaf, 0x08, 0x23, 0x14, 0x98,
  24975. 0x91, 0xde, 0x5d, 0x87, 0x95, 0x61, 0xca, 0x8f,
  24976. 0x0e, 0xce, 0xfb, 0x68, 0x4d, 0xd6, 0x28, 0xcb,
  24977. 0x28, 0xe2, 0x27, 0x20, 0x2d, 0xff, 0x64, 0xbb
  24978. };
  24979. WOLFSSL_SMALL_STACK_STATIC const byte ka_2[] = {
  24980. 0x12, 0x6f, 0x52, 0xe8, 0x07, 0x7f, 0x07, 0x84,
  24981. 0xa0, 0x61, 0x96, 0xf8, 0xee, 0x4d, 0x05, 0x57,
  24982. 0x65, 0xc7, 0x50, 0xc1
  24983. };
  24984. WOLFSSL_SMALL_STACK_STATIC const byte ks_2[] = {
  24985. 0x18, 0x5a, 0x59, 0xe5, 0x91, 0x4d, 0xc9, 0x6c,
  24986. 0xfa, 0x5b, 0x36, 0x06, 0x8c, 0x9a
  24987. };
  24988. WOLFSSL_SMALL_STACK_STATIC const byte index_c_2[] = {
  24989. 0x31, 0x2d, 0x58, 0x15
  24990. };
  24991. WOLFSSL_SMALL_STACK_STATIC const byte ke_c_2[] = {
  24992. 0x14, 0xf2, 0xc8, 0x25, 0x02, 0x79, 0x22, 0xa1,
  24993. 0x96, 0xb6, 0xf7, 0x07, 0x76, 0xa6, 0xa3, 0xc4,
  24994. 0x37, 0xdf, 0xa0, 0xf8, 0x78, 0x93, 0x2c, 0xfa,
  24995. 0xea, 0x35, 0xf0, 0xf3, 0x3f, 0x32, 0x6e, 0xfd
  24996. };
  24997. WOLFSSL_SMALL_STACK_STATIC const byte ka_c_2[] = {
  24998. 0x6e, 0x3d, 0x4a, 0x99, 0xea, 0x2f, 0x9d, 0x13,
  24999. 0x4a, 0x1e, 0x71, 0x2e, 0x15, 0xc0, 0xca, 0xb6,
  25000. 0x35, 0x78, 0xdf, 0xa4
  25001. };
  25002. WOLFSSL_SMALL_STACK_STATIC const byte ks_c_2[] = {
  25003. 0xae, 0xe4, 0xec, 0x18, 0x31, 0x70, 0x5d, 0x3f,
  25004. 0xdc, 0x97, 0x89, 0x88, 0xfd, 0xff
  25005. };
  25006. /* 128-bit key, kdrIdx = 8 */
  25007. WOLFSSL_SMALL_STACK_STATIC const byte key_3[] = {
  25008. 0x36, 0xb4, 0xde, 0xcb, 0x2e, 0x51, 0x23, 0x76,
  25009. 0xe0, 0x27, 0x7e, 0x3e, 0xc8, 0xf6, 0x54, 0x04
  25010. };
  25011. WOLFSSL_SMALL_STACK_STATIC const byte salt_3[] = {
  25012. 0x73, 0x26, 0xf4, 0x3f, 0xc0, 0xd9, 0xc6, 0xe3,
  25013. 0x2f, 0x92, 0x7d, 0x46, 0x12, 0x76
  25014. };
  25015. WOLFSSL_SMALL_STACK_STATIC const byte index_3[] = {
  25016. 0x44, 0x73, 0xb2, 0x2d, 0xb2, 0x60
  25017. };
  25018. WOLFSSL_SMALL_STACK_STATIC const byte ke_3[] = {
  25019. 0x79, 0x91, 0x3d, 0x7b, 0x20, 0x5d, 0xea, 0xe2,
  25020. 0xeb, 0x46, 0x89, 0x68, 0x5a, 0x06, 0x73, 0x74
  25021. };
  25022. WOLFSSL_SMALL_STACK_STATIC const byte ka_3[] = {
  25023. 0x2d, 0x2e, 0x97, 0x4e, 0x76, 0x8c, 0x62, 0xa6,
  25024. 0x57, 0x80, 0x13, 0x42, 0x0b, 0x51, 0xa7, 0x66,
  25025. 0xea, 0x31, 0x24, 0xe6
  25026. };
  25027. WOLFSSL_SMALL_STACK_STATIC const byte ks_3[] = {
  25028. 0xcc, 0xd7, 0x31, 0xf6, 0x3b, 0xf3, 0x89, 0x8a,
  25029. 0x5b, 0x7b, 0xb5, 0x8b, 0x4c, 0x3f
  25030. };
  25031. WOLFSSL_SMALL_STACK_STATIC const byte index_c_3[] = {
  25032. 0x4a, 0x7d, 0xaa, 0x85
  25033. };
  25034. WOLFSSL_SMALL_STACK_STATIC const byte ke_c_3[] = {
  25035. 0x34, 0x99, 0x71, 0xfe, 0x12, 0x93, 0xae, 0x8c,
  25036. 0x4a, 0xe9, 0x84, 0xe4, 0x93, 0x53, 0x63, 0x88
  25037. };
  25038. WOLFSSL_SMALL_STACK_STATIC const byte ka_c_3[] = {
  25039. 0xa4, 0x53, 0x5e, 0x0a, 0x9c, 0xf2, 0xce, 0x13,
  25040. 0xef, 0x7a, 0x13, 0xee, 0x0a, 0xef, 0xba, 0x17,
  25041. 0x05, 0x18, 0xe3, 0xed
  25042. };
  25043. WOLFSSL_SMALL_STACK_STATIC const byte ks_c_3[] = {
  25044. 0xe1, 0x29, 0x4f, 0x61, 0x30, 0x3c, 0x4d, 0x46,
  25045. 0x5f, 0x5c, 0x81, 0x3c, 0x38, 0xb6
  25046. };
  25047. /* SRTCP w/ 48-bit idx - KDR 0 (-1) */
  25048. WOLFSSL_SMALL_STACK_STATIC const byte mk48_1[] = {
  25049. 0xFF, 0xB6, 0xCB, 0x09, 0x71, 0x3F, 0x63, 0x4D,
  25050. 0x7F, 0x42, 0xED, 0xA8, 0x12, 0x81, 0x50, 0xE6
  25051. };
  25052. WOLFSSL_SMALL_STACK_STATIC const byte ms48_1[] = {
  25053. 0x1F, 0x04, 0x76, 0xC8, 0x7F, 0x58, 0x23, 0xEF,
  25054. 0xD3, 0x57, 0xB2, 0xBD, 0xF1, 0x32
  25055. };
  25056. WOLFSSL_SMALL_STACK_STATIC const byte srtcp48idx_1[] = {
  25057. 0x00, 0x00, 0x08, 0x56, 0xBC, 0x39
  25058. };
  25059. WOLFSSL_SMALL_STACK_STATIC const byte srtcpKe_48_1[] = {
  25060. 0xD2, 0xC3, 0xF3, 0x49, 0x00, 0x1A, 0x18, 0x0F,
  25061. 0xB6, 0x05, 0x5A, 0x5A, 0x67, 0x8E, 0xE5, 0xB2
  25062. };
  25063. WOLFSSL_SMALL_STACK_STATIC const byte srtcpKa_48_1[] = {
  25064. 0x8D, 0x54, 0xBE, 0xB5, 0x7B, 0x7F, 0x7A, 0xAB,
  25065. 0xF5, 0x46, 0xCE, 0x5B, 0x45, 0x69, 0x4A, 0x75,
  25066. 0x81, 0x2A, 0xE2, 0xCB
  25067. };
  25068. WOLFSSL_SMALL_STACK_STATIC const byte srtcpKs_48_1[] = {
  25069. 0x76, 0x3C, 0x97, 0x6A, 0x45, 0x31, 0xA7, 0x79,
  25070. 0x3C, 0x28, 0x4A, 0xA6, 0x82, 0x03
  25071. };
  25072. /* SRTCP w/ 48-bit idx - KDR 19 */
  25073. WOLFSSL_SMALL_STACK_STATIC const byte mk48_2[] = {
  25074. 0xBD, 0x1D, 0x71, 0x6B, 0xDA, 0x28, 0xE3, 0xFC,
  25075. 0xA5, 0xA0, 0x66, 0x3F, 0x2E, 0x34, 0xA8, 0x58
  25076. };
  25077. WOLFSSL_SMALL_STACK_STATIC const byte ms48_2[] = {
  25078. 0x79, 0x06, 0xE5, 0xAB, 0x5C, 0x2B, 0x1B, 0x69,
  25079. 0xFA, 0xEE, 0xD2, 0x29, 0x57, 0x3C
  25080. };
  25081. WOLFSSL_SMALL_STACK_STATIC const byte srtcp48idx_2[] = {
  25082. 0x00, 0x00, 0x59, 0xD0, 0xC2, 0xE8
  25083. };
  25084. WOLFSSL_SMALL_STACK_STATIC const byte srtcpKe_48_2[] = {
  25085. 0xB9, 0xD7, 0xAD, 0xD8, 0x90, 0x94, 0xC2, 0x92,
  25086. 0xA5, 0x04, 0x87, 0xC4, 0x8C, 0xEF, 0xE2, 0xA3
  25087. };
  25088. WOLFSSL_SMALL_STACK_STATIC const byte srtcpKa_48_2[] = {
  25089. 0x07, 0xD5, 0xC4, 0xD2, 0x06, 0xFB, 0x63, 0x15,
  25090. 0xC2, 0x9C, 0x7F, 0x55, 0xD1, 0x16, 0x5C, 0xB5,
  25091. 0xB7, 0x44, 0x54, 0xBD
  25092. };
  25093. WOLFSSL_SMALL_STACK_STATIC const byte srtcpKs_48_2[] = {
  25094. 0x0C, 0x5E, 0x53, 0xC1, 0xD0, 0x75, 0xAD, 0x65,
  25095. 0xBF, 0x51, 0x74, 0x50, 0x89, 0xD7
  25096. };
  25097. int kdr_48_1 = -1;
  25098. int kdr_48_2 = 19;
  25099. #define SRTP_TV_CNT 4
  25100. Srtp_Kdf_Tv tv[SRTP_TV_CNT] = {
  25101. { key_0, (word32)sizeof(key_0), salt_0, (word32)sizeof(salt_0), -1,
  25102. index_0, ke_0, ka_0, ks_0, index_c_0, ke_c_0, ka_c_0, ks_c_0,
  25103. 16, 20, 14 },
  25104. { key_1, (word32)sizeof(key_1), salt_1, (word32)sizeof(salt_1), 0,
  25105. index_1, ke_1, ka_1, ks_1, index_c_1, ke_c_1, ka_c_1, ks_c_1,
  25106. 24, 20, 14 },
  25107. { key_2, (word32)sizeof(key_2), salt_2, (word32)sizeof(salt_2), 1,
  25108. index_2, ke_2, ka_2, ks_2, index_c_2, ke_c_2, ka_c_2, ks_c_2,
  25109. 32, 20, 14 },
  25110. { key_3, (word32)sizeof(key_3), salt_3, (word32)sizeof(salt_3), 8,
  25111. index_3, ke_3, ka_3, ks_3, index_c_3, ke_c_3, ka_c_3, ks_c_3,
  25112. 16, 20, 14 },
  25113. };
  25114. int i;
  25115. int idx;
  25116. unsigned char keyE[32];
  25117. unsigned char keyA[20];
  25118. unsigned char keyS[14];
  25119. WOLFSSL_ENTER("srtpkdf_test");
  25120. for (i = 0; (ret == 0) && (i < SRTP_TV_CNT); i++) {
  25121. #ifndef WOLFSSL_AES_128
  25122. if (tv[i].keySz == AES_128_KEY_SIZE) {
  25123. continue;
  25124. }
  25125. #endif
  25126. #ifndef WOLFSSL_AES_192
  25127. if (tv[i].keySz == AES_192_KEY_SIZE) {
  25128. continue;
  25129. }
  25130. #endif
  25131. #ifndef WOLFSSL_AES_256
  25132. if (tv[i].keySz == AES_256_KEY_SIZE) {
  25133. continue;
  25134. }
  25135. #endif
  25136. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25137. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25138. keyS, tv[i].ksSz);
  25139. if (ret != 0)
  25140. return WC_TEST_RET_ENC_EC(ret);
  25141. if (XMEMCMP(keyE, tv[i].ke, tv[i].keSz) != 0)
  25142. return WC_TEST_RET_ENC_NC;
  25143. if (XMEMCMP(keyA, tv[i].ka, tv[i].kaSz) != 0)
  25144. return WC_TEST_RET_ENC_NC;
  25145. if (XMEMCMP(keyS, tv[i].ks, tv[i].ksSz) != 0)
  25146. return WC_TEST_RET_ENC_NC;
  25147. ret = wc_SRTP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  25148. tv[i].saltSz, tv[i].kdfIdx, tv[i].index, WC_SRTP_LABEL_ENCRYPTION,
  25149. keyE, tv[i].keSz);
  25150. if (ret != 0)
  25151. return WC_TEST_RET_ENC_EC(ret);
  25152. if (XMEMCMP(keyE, tv[i].ke, tv[i].keSz) != 0)
  25153. return WC_TEST_RET_ENC_NC;
  25154. ret = wc_SRTP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  25155. tv[i].saltSz, tv[i].kdfIdx, tv[i].index, WC_SRTP_LABEL_MSG_AUTH,
  25156. keyA, tv[i].kaSz);
  25157. if (ret != 0)
  25158. return WC_TEST_RET_ENC_EC(ret);
  25159. if (XMEMCMP(keyA, tv[i].ka, tv[i].kaSz) != 0)
  25160. return WC_TEST_RET_ENC_NC;
  25161. ret = wc_SRTP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  25162. tv[i].saltSz, tv[i].kdfIdx, tv[i].index, WC_SRTP_LABEL_SALT, keyS,
  25163. tv[i].ksSz);
  25164. if (ret != 0)
  25165. return WC_TEST_RET_ENC_EC(ret);
  25166. if (XMEMCMP(keyS, tv[i].ks, tv[i].ksSz) != 0)
  25167. return WC_TEST_RET_ENC_NC;
  25168. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25169. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25170. keyS, tv[i].ksSz);
  25171. if (ret != 0)
  25172. return WC_TEST_RET_ENC_EC(ret);
  25173. if (XMEMCMP(keyE, tv[i].ke_c, tv[i].keSz) != 0)
  25174. return WC_TEST_RET_ENC_NC;
  25175. if (XMEMCMP(keyA, tv[i].ka_c, tv[i].kaSz) != 0)
  25176. return WC_TEST_RET_ENC_NC;
  25177. if (XMEMCMP(keyS, tv[i].ks_c, tv[i].ksSz) != 0)
  25178. return WC_TEST_RET_ENC_NC;
  25179. ret = wc_SRTCP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  25180. tv[i].saltSz, tv[i].kdfIdx, tv[i].index_c,
  25181. WC_SRTCP_LABEL_ENCRYPTION, keyE, tv[i].keSz);
  25182. if (ret != 0)
  25183. return WC_TEST_RET_ENC_EC(ret);
  25184. if (XMEMCMP(keyE, tv[i].ke_c, tv[i].keSz) != 0)
  25185. return WC_TEST_RET_ENC_NC;
  25186. ret = wc_SRTCP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  25187. tv[i].saltSz, tv[i].kdfIdx, tv[i].index_c, WC_SRTCP_LABEL_MSG_AUTH,
  25188. keyA, tv[i].kaSz);
  25189. if (ret != 0)
  25190. return WC_TEST_RET_ENC_EC(ret);
  25191. if (XMEMCMP(keyA, tv[i].ka_c, tv[i].kaSz) != 0)
  25192. return WC_TEST_RET_ENC_NC;
  25193. ret = wc_SRTCP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  25194. tv[i].saltSz, tv[i].kdfIdx, tv[i].index_c, WC_SRTCP_LABEL_SALT,
  25195. keyS, tv[i].ksSz);
  25196. if (ret != 0)
  25197. return WC_TEST_RET_ENC_EC(ret);
  25198. if (XMEMCMP(keyS, tv[i].ks_c, tv[i].ksSz) != 0)
  25199. return WC_TEST_RET_ENC_NC;
  25200. }
  25201. #ifdef WOLFSSL_AES_128
  25202. i = 0;
  25203. #elif defined(WOLFSSL_AES_192)
  25204. i = 1;
  25205. #else
  25206. i = 2;
  25207. #endif
  25208. ret = wc_SRTP_KDF(tv[i].key, 33, tv[i].salt, tv[i].saltSz,
  25209. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25210. keyS, tv[i].ksSz);
  25211. if (ret != BAD_FUNC_ARG)
  25212. return WC_TEST_RET_ENC_EC(ret);
  25213. ret = wc_SRTCP_KDF(tv[i].key, 33, tv[i].salt, tv[i].saltSz,
  25214. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25215. keyS, tv[i].ksSz);
  25216. if (ret != BAD_FUNC_ARG)
  25217. return WC_TEST_RET_ENC_EC(ret);
  25218. ret = wc_SRTP_KDF(tv[i].key, 15, tv[i].salt, tv[i].saltSz,
  25219. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25220. keyS, tv[i].ksSz);
  25221. if (ret != BAD_FUNC_ARG)
  25222. return WC_TEST_RET_ENC_EC(ret);
  25223. ret = wc_SRTCP_KDF(tv[i].key, 15, tv[i].salt, tv[i].saltSz,
  25224. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25225. keyS, tv[i].ksSz);
  25226. if (ret != BAD_FUNC_ARG)
  25227. return WC_TEST_RET_ENC_EC(ret);
  25228. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, 15,
  25229. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25230. keyS, tv[i].ksSz);
  25231. if (ret != BAD_FUNC_ARG)
  25232. return WC_TEST_RET_ENC_EC(ret);
  25233. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, 15,
  25234. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25235. keyS, tv[i].ksSz);
  25236. if (ret != BAD_FUNC_ARG)
  25237. return WC_TEST_RET_ENC_EC(ret);
  25238. ret = wc_SRTP_KDF(NULL, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25239. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25240. keyS, tv[i].ksSz);
  25241. if (ret != BAD_FUNC_ARG)
  25242. return WC_TEST_RET_ENC_EC(ret);
  25243. ret = wc_SRTCP_KDF(NULL, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25244. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25245. keyS, tv[i].ksSz);
  25246. if (ret != BAD_FUNC_ARG)
  25247. return WC_TEST_RET_ENC_EC(ret);
  25248. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, NULL, tv[i].saltSz,
  25249. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25250. keyS, tv[i].ksSz);
  25251. if (ret != BAD_FUNC_ARG)
  25252. return WC_TEST_RET_ENC_EC(ret);
  25253. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, NULL, tv[i].saltSz,
  25254. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25255. keyS, tv[i].ksSz);
  25256. if (ret != BAD_FUNC_ARG)
  25257. return WC_TEST_RET_ENC_EC(ret);
  25258. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25259. 25, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25260. keyS, tv[i].ksSz);
  25261. if (ret != BAD_FUNC_ARG)
  25262. return WC_TEST_RET_ENC_EC(ret);
  25263. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25264. 25, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25265. keyS, tv[i].ksSz);
  25266. if (ret != BAD_FUNC_ARG)
  25267. return WC_TEST_RET_ENC_EC(ret);
  25268. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25269. -2, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25270. keyS, tv[i].ksSz);
  25271. if (ret != BAD_FUNC_ARG)
  25272. return WC_TEST_RET_ENC_EC(ret);
  25273. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25274. -2, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25275. keyS, tv[i].ksSz);
  25276. if (ret != BAD_FUNC_ARG)
  25277. return WC_TEST_RET_ENC_EC(ret);
  25278. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25279. tv[i].kdfIdx, tv[i].index, NULL, tv[i].keSz, keyA, tv[i].kaSz,
  25280. keyS, tv[i].ksSz);
  25281. if (ret != 0)
  25282. return WC_TEST_RET_ENC_EC(ret);
  25283. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25284. tv[i].kdfIdx, tv[i].index_c, NULL, tv[i].keSz, keyA, tv[i].kaSz,
  25285. keyS, tv[i].ksSz);
  25286. if (ret != 0)
  25287. return WC_TEST_RET_ENC_EC(ret);
  25288. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25289. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, NULL, tv[i].kaSz,
  25290. keyS, tv[i].ksSz);
  25291. if (ret != 0)
  25292. return WC_TEST_RET_ENC_EC(ret);
  25293. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25294. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, NULL, tv[i].kaSz,
  25295. keyS, tv[i].ksSz);
  25296. if (ret != 0)
  25297. return WC_TEST_RET_ENC_EC(ret);
  25298. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25299. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25300. NULL, tv[i].ksSz);
  25301. if (ret != 0)
  25302. return WC_TEST_RET_ENC_EC(ret);
  25303. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25304. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25305. NULL, tv[i].ksSz);
  25306. if (ret != 0)
  25307. return WC_TEST_RET_ENC_EC(ret);
  25308. idx = wc_SRTP_KDF_kdr_to_idx(0);
  25309. if (idx != -1)
  25310. return WC_TEST_RET_ENC_NC;
  25311. for (i = 0; i < 32; i++) {
  25312. word32 kdr = 1U << i;
  25313. idx = wc_SRTP_KDF_kdr_to_idx(kdr);
  25314. if (idx != i)
  25315. return WC_TEST_RET_ENC_NC;
  25316. }
  25317. /* SRTCP w/ 48-bit IDX, 128-bit key test */
  25318. if (i == 0) {
  25319. ret = wc_SRTCP_KDF_ex(mk48_1, (word32)sizeof(mk48_1),
  25320. ms48_1, (word32)sizeof(ms48_1),
  25321. kdr_48_1, srtcp48idx_1, keyE, tv[i].keSz,
  25322. keyA, tv[i].kaSz, keyS, tv[i].ksSz,
  25323. WC_SRTCP_48BIT_IDX);
  25324. if (ret != 0)
  25325. return WC_TEST_RET_ENC_EC(ret);
  25326. if (XMEMCMP(keyE, srtcpKe_48_1, tv[i].keSz) != 0)
  25327. return WC_TEST_RET_ENC_NC;
  25328. if (XMEMCMP(keyA, srtcpKa_48_1, tv[i].kaSz) != 0)
  25329. return WC_TEST_RET_ENC_NC;
  25330. if (XMEMCMP(keyS, srtcpKs_48_1, tv[i].ksSz) != 0)
  25331. return WC_TEST_RET_ENC_NC;
  25332. ret = wc_SRTCP_KDF_ex(mk48_2, (word32)sizeof(mk48_2),
  25333. ms48_2, (word32)sizeof(ms48_2),
  25334. kdr_48_2, srtcp48idx_2, keyE, tv[i].keSz,
  25335. keyA, tv[i].kaSz, keyS, tv[i].ksSz,
  25336. WC_SRTCP_48BIT_IDX);
  25337. if (ret != 0)
  25338. return WC_TEST_RET_ENC_EC(ret);
  25339. if (XMEMCMP(keyE, srtcpKe_48_2, tv[i].keSz) != 0)
  25340. return WC_TEST_RET_ENC_NC;
  25341. if (XMEMCMP(keyA, srtcpKa_48_2, tv[i].kaSz) != 0)
  25342. return WC_TEST_RET_ENC_NC;
  25343. if (XMEMCMP(keyS, srtcpKs_48_2, tv[i].ksSz) != 0)
  25344. return WC_TEST_RET_ENC_NC;
  25345. }
  25346. return 0;
  25347. }
  25348. #endif
  25349. #ifdef HAVE_ECC
  25350. /* size to use for ECC key gen tests */
  25351. #ifndef ECC_KEYGEN_SIZE
  25352. #if !defined(NO_ECC256) || defined(WOLFSSL_SM2)
  25353. #define ECC_KEYGEN_SIZE 32
  25354. #elif defined(HAVE_ECC384)
  25355. #define ECC_KEYGEN_SIZE 48
  25356. #elif defined(HAVE_ECC224)
  25357. #define ECC_KEYGEN_SIZE 28
  25358. #elif defined(HAVE_ECC521)
  25359. #define ECC_KEYGEN_SIZE 66
  25360. #else
  25361. #error No ECC keygen size defined for test
  25362. #endif
  25363. #endif
  25364. #ifdef BENCH_EMBEDDED
  25365. #define ECC_SHARED_SIZE 128
  25366. #else
  25367. #define ECC_SHARED_SIZE MAX_ECC_BYTES
  25368. #endif
  25369. #if defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  25370. #define HAVE_ECC_DETERMINISTIC_K
  25371. #define ECC_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
  25372. #else
  25373. #define ECC_DIGEST_SIZE MAX_ECC_BYTES
  25374. #endif
  25375. #define ECC_SIG_SIZE ECC_MAX_SIG_SIZE
  25376. #ifdef NO_ECC_SECP
  25377. #define NO_ECC_VECTOR_TEST
  25378. #endif
  25379. #ifndef NO_ECC_VECTOR_TEST
  25380. #if (defined(HAVE_ECC192) || defined(HAVE_ECC224) ||\
  25381. !defined(NO_ECC256) || defined(HAVE_ECC384) ||\
  25382. defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  25383. #define HAVE_ECC_VECTOR_TEST
  25384. #endif
  25385. #endif
  25386. #ifdef HAVE_ECC_VECTOR_TEST
  25387. typedef struct eccVector {
  25388. const char* msg; /* SHA-1 Encoded Message */
  25389. const char* Qx;
  25390. const char* Qy;
  25391. const char* d; /* Private Key */
  25392. const char* R;
  25393. const char* S;
  25394. const char* curveName;
  25395. word32 msgLen;
  25396. word32 keySize;
  25397. #ifndef NO_ASN
  25398. const byte* r;
  25399. word32 rSz;
  25400. const byte* s;
  25401. word32 sSz;
  25402. #endif
  25403. } eccVector;
  25404. #if !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  25405. static wc_test_ret_t ecc_test_vector_item(const eccVector* vector)
  25406. {
  25407. wc_test_ret_t ret = 0;
  25408. int verify = 0;
  25409. word32 sigSz;
  25410. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25411. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25412. #else
  25413. ecc_key userA[1];
  25414. #endif
  25415. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  25416. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  25417. word32 sigRawSz, rSz = MAX_ECC_BYTES, sSz = MAX_ECC_BYTES;
  25418. WC_DECLARE_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  25419. WC_DECLARE_VAR(r, byte, MAX_ECC_BYTES, HEAP_HINT);
  25420. WC_DECLARE_VAR(s, byte, MAX_ECC_BYTES, HEAP_HINT);
  25421. #endif
  25422. WC_ALLOC_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  25423. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  25424. WC_ALLOC_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  25425. WC_ALLOC_VAR(r, byte, MAX_ECC_BYTES, HEAP_HINT);
  25426. WC_ALLOC_VAR(s, byte, MAX_ECC_BYTES, HEAP_HINT);
  25427. #endif
  25428. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  25429. if (sig == NULL)
  25430. ERROR_OUT(MEMORY_E, done);
  25431. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  25432. if (sigRaw == NULL || r == NULL || s == NULL)
  25433. ERROR_OUT(MEMORY_E, done);
  25434. #endif
  25435. #endif
  25436. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25437. if (userA == NULL)
  25438. ERROR_OUT(MEMORY_E, done);
  25439. #endif
  25440. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  25441. if (ret != 0)
  25442. goto done;
  25443. ret = wc_ecc_import_raw(userA, vector->Qx, vector->Qy,
  25444. vector->d, vector->curveName);
  25445. if (ret != 0)
  25446. goto done;
  25447. XMEMSET(sig, 0, ECC_SIG_SIZE);
  25448. sigSz = ECC_SIG_SIZE;
  25449. ret = wc_ecc_rs_to_sig(vector->R, vector->S, sig, &sigSz);
  25450. if (ret != 0)
  25451. goto done;
  25452. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  25453. XMEMSET(sigRaw, 0, ECC_SIG_SIZE);
  25454. sigRawSz = ECC_SIG_SIZE;
  25455. ret = wc_ecc_rs_raw_to_sig(vector->r, vector->rSz, vector->s, vector->sSz,
  25456. sigRaw, &sigRawSz);
  25457. if (ret != 0)
  25458. goto done;
  25459. if (sigSz != sigRawSz || XMEMCMP(sig, sigRaw, sigSz) != 0) {
  25460. ret = WC_TEST_RET_ENC_NC;
  25461. goto done;
  25462. }
  25463. ret = wc_ecc_sig_to_rs(sig, sigSz, r, &rSz, s, &sSz);
  25464. if (ret != 0)
  25465. goto done;
  25466. if (rSz != vector->rSz || XMEMCMP(r, vector->r, rSz) != 0 ||
  25467. sSz != vector->sSz || XMEMCMP(s, vector->s, sSz) != 0) {
  25468. ret = WC_TEST_RET_ENC_NC;
  25469. goto done;
  25470. }
  25471. #endif
  25472. #ifdef HAVE_ECC_VERIFY
  25473. do {
  25474. #if defined(WOLFSSL_ASYNC_CRYPT)
  25475. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  25476. #endif
  25477. if (ret == 0)
  25478. ret = wc_ecc_verify_hash(sig, sigSz, (byte*)vector->msg,
  25479. vector->msgLen, &verify, userA);
  25480. } while (ret == WC_PENDING_E);
  25481. if (ret != 0)
  25482. goto done;
  25483. TEST_SLEEP();
  25484. if (verify != 1)
  25485. ret = WC_TEST_RET_ENC_NC;
  25486. #endif
  25487. done:
  25488. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25489. if (userA != NULL) {
  25490. wc_ecc_free(userA);
  25491. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25492. }
  25493. #else
  25494. wc_ecc_free(userA);
  25495. #endif
  25496. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  25497. WC_FREE_VAR(sigRaw, HEAP_HINT);
  25498. WC_FREE_VAR(r, HEAP_HINT);
  25499. WC_FREE_VAR(s, HEAP_HINT);
  25500. #endif
  25501. WC_FREE_VAR(sig, HEAP_HINT);
  25502. return ret;
  25503. }
  25504. static wc_test_ret_t ecc_test_vector(int keySize)
  25505. {
  25506. wc_test_ret_t ret;
  25507. eccVector vec;
  25508. XMEMSET(&vec, 0, sizeof(vec));
  25509. vec.keySize = (word32)keySize;
  25510. switch(keySize) {
  25511. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  25512. case 14:
  25513. return 0;
  25514. #endif /* HAVE_ECC112 */
  25515. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  25516. case 16:
  25517. return 0;
  25518. #endif /* HAVE_ECC128 */
  25519. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  25520. case 20:
  25521. return 0;
  25522. #endif /* HAVE_ECC160 */
  25523. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  25524. case 24:
  25525. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  25526. #if 1
  25527. vec.msg = "\x60\x80\x79\x42\x3f\x12\x42\x1d\xe6\x16\xb7\x49\x3e\xbe\x55\x1c\xf4\xd6\x5b\x92";
  25528. vec.msgLen = 20;
  25529. #else
  25530. /* This is the raw message prior to SHA-1 */
  25531. vec.msg =
  25532. "\xeb\xf7\x48\xd7\x48\xeb\xbc\xa7\xd2\x9f\xb4\x73\x69\x8a\x6e\x6b"
  25533. "\x4f\xb1\x0c\x86\x5d\x4a\xf0\x24\xcc\x39\xae\x3d\xf3\x46\x4b\xa4"
  25534. "\xf1\xd6\xd4\x0f\x32\xbf\x96\x18\xa9\x1b\xb5\x98\x6f\xa1\xa2\xaf"
  25535. "\x04\x8a\x0e\x14\xdc\x51\xe5\x26\x7e\xb0\x5e\x12\x7d\x68\x9d\x0a"
  25536. "\xc6\xf1\xa7\xf1\x56\xce\x06\x63\x16\xb9\x71\xcc\x7a\x11\xd0\xfd"
  25537. "\x7a\x20\x93\xe2\x7c\xf2\xd0\x87\x27\xa4\xe6\x74\x8c\xc3\x2f\xd5"
  25538. "\x9c\x78\x10\xc5\xb9\x01\x9d\xf2\x1c\xdc\xc0\xbc\xa4\x32\xc0\xa3"
  25539. "\xee\xd0\x78\x53\x87\x50\x88\x77\x11\x43\x59\xce\xe4\xa0\x71\xcf";
  25540. vec.msgLen = 128;
  25541. #endif
  25542. vec.Qx = "07008ea40b08dbe76432096e80a2494c94982d2d5bcf98e6";
  25543. vec.Qy = "76fab681d00b414ea636ba215de26d98c41bd7f2e4d65477";
  25544. vec.d = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3";
  25545. vec.R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  25546. vec.S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  25547. vec.curveName = "SECP192R1";
  25548. #ifndef NO_ASN
  25549. vec.r = (byte*)"\x69\x94\xd9\x62\xbd\xd0\xd7\x93\xff\xdd\xf8\x55"
  25550. "\xec\x5b\xf2\xf9\x1a\x96\x98\xb4\x62\x58\xa6\x3e";
  25551. vec.rSz = 24;
  25552. vec.s = (byte*)"\x02\xba\x64\x65\xa2\x34\x90\x37\x44\xab\x02\xbc"
  25553. "\x85\x21\x40\x5b\x73\xcf\x5f\xc0\x0e\x1a\x9f\x41";
  25554. vec.sSz = 24;
  25555. #endif
  25556. break;
  25557. #endif /* HAVE_ECC192 */
  25558. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  25559. case 28:
  25560. /* first [P-224,SHA-1] vector from FIPS 186-3 NIST vectors */
  25561. #if 1
  25562. vec.msg = "\xb9\xa3\xb8\x6d\xb0\xba\x99\xfd\xc6\xd2\x94\x6b\xfe\xbe\x9c\xe8\x3f\x10\x74\xfc";
  25563. vec.msgLen = 20;
  25564. #else
  25565. /* This is the raw message prior to SHA-1 */
  25566. vec.msg =
  25567. "\x36\xc8\xb2\x29\x86\x48\x7f\x67\x7c\x18\xd0\x97\x2a\x9e\x20\x47"
  25568. "\xb3\xaf\xa5\x9e\xc1\x62\x76\x4e\xc3\x0b\x5b\x69\xe0\x63\x0f\x99"
  25569. "\x0d\x4e\x05\xc2\x73\xb0\xe5\xa9\xd4\x28\x27\xb6\x95\xfc\x2d\x64"
  25570. "\xd9\x13\x8b\x1c\xf4\xc1\x21\x55\x89\x4c\x42\x13\x21\xa7\xbb\x97"
  25571. "\x0b\xdc\xe0\xfb\xf0\xd2\xae\x85\x61\xaa\xd8\x71\x7f\x2e\x46\xdf"
  25572. "\xe3\xff\x8d\xea\xb4\xd7\x93\x23\x56\x03\x2c\x15\x13\x0d\x59\x9e"
  25573. "\x26\xc1\x0f\x2f\xec\x96\x30\x31\xac\x69\x38\xa1\x8d\x66\x45\x38"
  25574. "\xb9\x4d\xac\x55\x34\xef\x7b\x59\x94\x24\xd6\x9b\xe1\xf7\x1c\x20";
  25575. vec.msgLen = 128;
  25576. #endif
  25577. vec.Qx = "8a4dca35136c4b70e588e23554637ae251077d1365a6ba5db9585de7";
  25578. vec.Qy = "ad3dee06de0be8279d4af435d7245f14f3b4f82eb578e519ee0057b1";
  25579. vec.d = "97c4b796e1639dd1035b708fc00dc7ba1682cec44a1002a1a820619f";
  25580. vec.R = "147b33758321e722a0360a4719738af848449e2c1d08defebc1671a7";
  25581. vec.S = "24fc7ed7f1352ca3872aa0916191289e2e04d454935d50fe6af3ad5b";
  25582. vec.curveName = "SECP224R1";
  25583. #ifndef NO_ASN
  25584. vec.r = (byte*)"\x14\x7b\x33\x75\x83\x21\xe7\x22\xa0\x36\x0a\x47"
  25585. "\x19\x73\x8a\xf8\x48\x44\x9e\x2c\x1d\x08\xde\xfe"
  25586. "\xbc\x16\x71\xa7";
  25587. vec.rSz = 28;
  25588. vec.s = (byte*)"\x24\xfc\x7e\xd7\xf1\x35\x2c\xa3\x87\x2a\xa0\x91"
  25589. "\x61\x91\x28\x9e\x2e\x04\xd4\x54\x93\x5d\x50\xfe"
  25590. "\x6a\xf3\xad\x5b";
  25591. vec.sSz = 28;
  25592. #endif
  25593. break;
  25594. #endif /* HAVE_ECC224 */
  25595. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  25596. case 30:
  25597. return 0;
  25598. #endif /* HAVE_ECC239 */
  25599. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  25600. case 32:
  25601. /* first [P-256,SHA-1] vector from FIPS 186-3 NIST vectors */
  25602. #if 1
  25603. vec.msg = "\xa3\xf9\x1a\xe2\x1b\xa6\xb3\x03\x98\x64\x47\x2f\x18\x41\x44\xc6\xaf\x62\xcd\x0e";
  25604. vec.msgLen = 20;
  25605. #else
  25606. /* This is the raw message prior to SHA-1 */
  25607. vec.msg =
  25608. "\xa2\x4b\x21\x76\x2e\x6e\xdb\x15\x3c\xc1\x14\x38\xdb\x0e\x92\xcd"
  25609. "\xf5\x2b\x86\xb0\x6c\xa9\x70\x16\x06\x27\x59\xc7\x0d\x36\xd1\x56"
  25610. "\x2c\xc9\x63\x0d\x7f\xc7\xc7\x74\xb2\x8b\x54\xe3\x1e\xf5\x58\x72"
  25611. "\xb2\xa6\x5d\xf1\xd7\xec\x26\xde\xbb\x33\xe7\xd9\x27\xef\xcc\xf4"
  25612. "\x6b\x63\xde\x52\xa4\xf4\x31\xea\xca\x59\xb0\x5d\x2e\xde\xc4\x84"
  25613. "\x5f\xff\xc0\xee\x15\x03\x94\xd6\x1f\x3d\xfe\xcb\xcd\xbf\x6f\x5a"
  25614. "\x73\x38\xd0\xbe\x3f\x2a\x77\x34\x51\x98\x3e\xba\xeb\x48\xf6\x73"
  25615. "\x8f\xc8\x95\xdf\x35\x7e\x1a\x48\xa6\x53\xbb\x35\x5a\x31\xa1\xb4"
  25616. vec.msgLen = 128;
  25617. #endif
  25618. vec.Qx = "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  25619. vec.Qy = "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  25620. vec.d = "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  25621. vec.R = "2b826f5d44e2d0b6de531ad96b51e8f0c56fdfead3c236892e4d84eacfc3b75c";
  25622. vec.S = "a2248b62c03db35a7cd63e8a120a3521a89d3d2f61ff99035a2148ae32e3a248";
  25623. #ifndef NO_ASN
  25624. vec.r = (byte*)"\x2b\x82\x6f\x5d\x44\xe2\xd0\xb6\xde\x53\x1a\xd9"
  25625. "\x6b\x51\xe8\xf0\xc5\x6f\xdf\xea\xd3\xc2\x36\x89"
  25626. "\x2e\x4d\x84\xea\xcf\xc3\xb7\x5c";
  25627. vec.rSz = 32;
  25628. vec.s = (byte*)"\xa2\x24\x8b\x62\xc0\x3d\xb3\x5a\x7c\xd6\x3e\x8a"
  25629. "\x12\x0a\x35\x21\xa8\x9d\x3d\x2f\x61\xff\x99\x03"
  25630. "\x5a\x21\x48\xae\x32\xe3\xa2\x48";
  25631. vec.sSz = 32;
  25632. #endif
  25633. vec.curveName = "SECP256R1";
  25634. break;
  25635. #endif /* !NO_ECC256 */
  25636. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  25637. case 40:
  25638. return 0;
  25639. #endif /* HAVE_ECC320 */
  25640. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  25641. case 48:
  25642. /* first [P-384,SHA-1] vector from FIPS 186-3 NIST vectors */
  25643. #if 1
  25644. vec.msg = "\x9b\x9f\x8c\x95\x35\xa5\xca\x26\x60\x5d\xb7\xf2\xfa\x57\x3b\xdf\xc3\x2e\xab\x8b";
  25645. vec.msgLen = 20;
  25646. #else
  25647. /* This is the raw message prior to SHA-1 */
  25648. vec.msg =
  25649. "\xab\xe1\x0a\xce\x13\xe7\xe1\xd9\x18\x6c\x48\xf7\x88\x9d\x51\x47"
  25650. "\x3d\x3a\x09\x61\x98\x4b\xc8\x72\xdf\x70\x8e\xcc\x3e\xd3\xb8\x16"
  25651. "\x9d\x01\xe3\xd9\x6f\xc4\xf1\xd5\xea\x00\xa0\x36\x92\xbc\xc5\xcf"
  25652. "\xfd\x53\x78\x7c\x88\xb9\x34\xaf\x40\x4c\x03\x9d\x32\x89\xb5\xba"
  25653. "\xc5\xae\x7d\xb1\x49\x68\x75\xb5\xdc\x73\xc3\x09\xf9\x25\xc1\x3d"
  25654. "\x1c\x01\xab\xda\xaf\xeb\xcd\xac\x2c\xee\x43\x39\x39\xce\x8d\x4a"
  25655. "\x0a\x5d\x57\xbb\x70\x5f\x3b\xf6\xec\x08\x47\x95\x11\xd4\xb4\xa3"
  25656. "\x21\x1f\x61\x64\x9a\xd6\x27\x43\x14\xbf\x0d\x43\x8a\x81\xe0\x60"
  25657. vec.msgLen = 128;
  25658. #endif
  25659. vec.Qx = "e55fee6c49d8d523f5ce7bf9c0425ce4ff650708b7de5cfb095901523979a7f042602db30854735369813b5c3f5ef868";
  25660. vec.Qy = "28f59cc5dc509892a988d38a8e2519de3d0c4fd0fbdb0993e38f18506c17606c5e24249246f1ce94983a5361c5be983e";
  25661. vec.d = "a492ce8fa90084c227e1a32f7974d39e9ff67a7e8705ec3419b35fb607582bebd461e0b1520ac76ec2dd4e9b63ebae71";
  25662. vec.R = "6820b8585204648aed63bdff47f6d9acebdea62944774a7d14f0e14aa0b9a5b99545b2daee6b3c74ebf606667a3f39b7";
  25663. vec.S = "491af1d0cccd56ddd520b233775d0bc6b40a6255cc55207d8e9356741f23c96c14714221078dbd5c17f4fdd89b32a907";
  25664. vec.curveName = "SECP384R1";
  25665. #ifndef NO_ASN
  25666. vec.r = (byte*)"\x68\x20\xb8\x58\x52\x04\x64\x8a\xed\x63\xbd\xff"
  25667. "\x47\xf6\xd9\xac\xeb\xde\xa6\x29\x44\x77\x4a\x7d"
  25668. "\x14\xf0\xe1\x4a\xa0\xb9\xa5\xb9\x95\x45\xb2\xda"
  25669. "\xee\x6b\x3c\x74\xeb\xf6\x06\x66\x7a\x3f\x39\xb7";
  25670. vec.rSz = 48;
  25671. vec.s = (byte*)"\x49\x1a\xf1\xd0\xcc\xcd\x56\xdd\xd5\x20\xb2\x33"
  25672. "\x77\x5d\x0b\xc6\xb4\x0a\x62\x55\xcc\x55\x20\x7d"
  25673. "\x8e\x93\x56\x74\x1f\x23\xc9\x6c\x14\x71\x42\x21"
  25674. "\x07\x8d\xbd\x5c\x17\xf4\xfd\xd8\x9b\x32\xa9\x07";
  25675. vec.sSz = 48;
  25676. #endif
  25677. break;
  25678. #endif /* HAVE_ECC384 */
  25679. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  25680. case 64:
  25681. return 0;
  25682. #endif /* HAVE_ECC512 */
  25683. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  25684. case 66:
  25685. /* first [P-521,SHA-1] vector from FIPS 186-3 NIST vectors */
  25686. #if 1
  25687. vec.msg = "\x1b\xf7\x03\x9c\xca\x23\x94\x27\x3f\x11\xa1\xd4\x8d\xcc\xb4\x46\x6f\x31\x61\xdf";
  25688. vec.msgLen = 20;
  25689. #else
  25690. /* This is the raw message prior to SHA-1 */
  25691. vec.msg =
  25692. "\x50\x3f\x79\x39\x34\x0a\xc7\x23\xcd\x4a\x2f\x4e\x6c\xcc\x27\x33"
  25693. "\x38\x3a\xca\x2f\xba\x90\x02\x19\x9d\x9e\x1f\x94\x8b\xe0\x41\x21"
  25694. "\x07\xa3\xfd\xd5\x14\xd9\x0c\xd4\xf3\x7c\xc3\xac\x62\xef\x00\x3a"
  25695. "\x2d\xb1\xd9\x65\x7a\xb7\x7f\xe7\x55\xbf\x71\xfa\x59\xe4\xd9\x6e"
  25696. "\xa7\x2a\xe7\xbf\x9d\xe8\x7d\x79\x34\x3b\xc1\xa4\xbb\x14\x4d\x16"
  25697. "\x28\xd1\xe9\xe9\xc8\xed\x80\x8b\x96\x2c\x54\xe5\xf9\x6d\x53\xda"
  25698. "\x14\x7a\x96\x38\xf9\x4a\x91\x75\xd8\xed\x61\x05\x5f\x0b\xa5\x73"
  25699. "\xa8\x2b\xb7\xe0\x18\xee\xda\xc4\xea\x7b\x36\x2e\xc8\x9c\x38\x2b"
  25700. vec.msgLen = 128;
  25701. #endif
  25702. vec.Qx = "12fbcaeffa6a51f3ee4d3d2b51c5dec6d7c726ca353fc014ea2bf7cfbb9b910d32cbfa6a00fe39b6cdb8946f22775398b2e233c0cf144d78c8a7742b5c7a3bb5d23";
  25703. vec.Qy = "09cdef823dd7bf9a79e8cceacd2e4527c231d0ae5967af0958e931d7ddccf2805a3e618dc3039fec9febbd33052fe4c0fee98f033106064982d88f4e03549d4a64d";
  25704. vec.d = "1bd56bd106118eda246155bd43b42b8e13f0a6e25dd3bb376026fab4dc92b6157bc6dfec2d15dd3d0cf2a39aa68494042af48ba9601118da82c6f2108a3a203ad74";
  25705. vec.R = "0bd117b4807710898f9dd7778056485777668f0e78e6ddf5b000356121eb7a220e9493c7f9a57c077947f89ac45d5acb6661bbcd17abb3faea149ba0aa3bb1521be";
  25706. vec.S = "019cd2c5c3f9870ecdeb9b323abdf3a98cd5e231d85c6ddc5b71ab190739f7f226e6b134ba1d5889ddeb2751dabd97911dff90c34684cdbe7bb669b6c3d22f2480c";
  25707. vec.curveName = "SECP521R1";
  25708. #ifndef NO_ASN
  25709. vec.r = (byte*)"\xbd\x11\x7b\x48\x07\x71\x08\x98\xf9\xdd\x77\x78"
  25710. "\x05\x64\x85\x77\x76\x68\xf0\xe7\x8e\x6d\xdf\x5b"
  25711. "\x00\x03\x56\x12\x1e\xb7\xa2\x20\xe9\x49\x3c\x7f"
  25712. "\x9a\x57\xc0\x77\x94\x7f\x89\xac\x45\xd5\xac\xb6"
  25713. "\x66\x1b\xbc\xd1\x7a\xbb\x3f\xae\xa1\x49\xba\x0a"
  25714. "\xa3\xbb\x15\x21\xbe";
  25715. vec.rSz = 65;
  25716. vec.s = (byte*)"\x19\xcd\x2c\x5c\x3f\x98\x70\xec\xde\xb9\xb3\x23"
  25717. "\xab\xdf\x3a\x98\xcd\x5e\x23\x1d\x85\xc6\xdd\xc5"
  25718. "\xb7\x1a\xb1\x90\x73\x9f\x7f\x22\x6e\x6b\x13\x4b"
  25719. "\xa1\xd5\x88\x9d\xde\xb2\x75\x1d\xab\xd9\x79\x11"
  25720. "\xdf\xf9\x0c\x34\x68\x4c\xdb\xe7\xbb\x66\x9b\x6c"
  25721. "\x3d\x22\xf2\x48\x0c";
  25722. vec.sSz = 65;
  25723. #endif
  25724. break;
  25725. #endif /* HAVE_ECC521 */
  25726. default:
  25727. return NOT_COMPILED_IN; /* Invalid key size / Not supported */
  25728. }; /* Switch */
  25729. ret = ecc_test_vector_item(&vec);
  25730. if (ret < 0) {
  25731. return ret;
  25732. }
  25733. return 0;
  25734. }
  25735. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  25736. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  25737. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  25738. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  25739. #if defined(HAVE_ECC256)
  25740. static wc_test_ret_t ecc_test_deterministic_k(WC_RNG* rng)
  25741. {
  25742. wc_test_ret_t ret;
  25743. #ifdef WOLFSSL_SMALL_STACK
  25744. ecc_key *key = NULL;
  25745. #else
  25746. ecc_key key[1];
  25747. #endif
  25748. int key_inited = 0;
  25749. byte sig[72];
  25750. word32 sigSz;
  25751. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  25752. unsigned char hash[32];
  25753. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  25754. "C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721";
  25755. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  25756. "60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6";
  25757. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  25758. "7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299";
  25759. WOLFSSL_SMALL_STACK_STATIC const byte expSig[] = {
  25760. 0x30, 0x46, 0x02, 0x21, 0x00, 0xEF, 0xD4, 0x8B,
  25761. 0x2A, 0xAC, 0xB6, 0xA8, 0xFD, 0x11, 0x40, 0xDD,
  25762. 0x9C, 0xD4, 0x5E, 0x81, 0xD6, 0x9D, 0x2C, 0x87,
  25763. 0x7B, 0x56, 0xAA, 0xF9, 0x91, 0xC3, 0x4D, 0x0E,
  25764. 0xA8, 0x4E, 0xAF, 0x37, 0x16, 0x02, 0x21, 0x00,
  25765. 0xF7, 0xCB, 0x1C, 0x94, 0x2D, 0x65, 0x7C, 0x41,
  25766. 0xD4, 0x36, 0xC7, 0xA1, 0xB6, 0xE2, 0x9F, 0x65,
  25767. 0xF3, 0xE9, 0x00, 0xDB, 0xB9, 0xAF, 0xF4, 0x06,
  25768. 0x4D, 0xC4, 0xAB, 0x2F, 0x84, 0x3A, 0xCD, 0xA8
  25769. };
  25770. #ifdef WOLFSSL_SMALL_STACK
  25771. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25772. if (key == NULL)
  25773. return MEMORY_E;
  25774. #endif
  25775. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  25776. if (ret != 0) {
  25777. goto done;
  25778. }
  25779. key_inited = 1;
  25780. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  25781. if (ret != 0) {
  25782. goto done;
  25783. }
  25784. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  25785. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  25786. if (ret != 0) {
  25787. goto done;
  25788. }
  25789. ret = wc_ecc_set_deterministic(key, 1);
  25790. if (ret != 0) {
  25791. goto done;
  25792. }
  25793. sigSz = sizeof(sig);
  25794. do {
  25795. #if defined(WOLFSSL_ASYNC_CRYPT)
  25796. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  25797. #endif
  25798. if (ret == 0)
  25799. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  25800. } while (ret == WC_PENDING_E);
  25801. if (ret != 0) {
  25802. goto done;
  25803. }
  25804. TEST_SLEEP();
  25805. if (sigSz != sizeof(expSig)) {
  25806. ret = WC_TEST_RET_ENC_NC;
  25807. goto done;
  25808. }
  25809. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  25810. ret = WC_TEST_RET_ENC_NC;
  25811. goto done;
  25812. }
  25813. sigSz = sizeof(sig);
  25814. do {
  25815. #if defined(WOLFSSL_ASYNC_CRYPT)
  25816. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  25817. #endif
  25818. if (ret == 0)
  25819. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  25820. } while (ret == WC_PENDING_E);
  25821. if (ret != 0) {
  25822. goto done;
  25823. }
  25824. TEST_SLEEP();
  25825. done:
  25826. if (key_inited)
  25827. wc_ecc_free(key);
  25828. #ifdef WOLFSSL_SMALL_STACK
  25829. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25830. #endif
  25831. return ret;
  25832. }
  25833. #endif
  25834. #ifdef WOLFSSL_PUBLIC_MP
  25835. #if defined(HAVE_ECC384)
  25836. /* KAT from RFC6979 */
  25837. static wc_test_ret_t ecc384_test_deterministic_k(WC_RNG* rng)
  25838. {
  25839. wc_test_ret_t ret;
  25840. #ifdef WOLFSSL_SMALL_STACK
  25841. ecc_key *key;
  25842. mp_int *r, *s, *expR, *expS;
  25843. #else
  25844. ecc_key key[1];
  25845. mp_int r[1], s[1], expR[1], expS[1];
  25846. #endif
  25847. int key_inited = 0;
  25848. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  25849. unsigned char hash[32];
  25850. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  25851. "6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D8"
  25852. "96D5724E4C70A825F872C9EA60D2EDF5";
  25853. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  25854. "EC3A4E415B4E19A4568618029F427FA5DA9A8BC4AE92E02E06AAE5286B300C64"
  25855. "DEF8F0EA9055866064A254515480BC13";
  25856. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  25857. "8015D9B72D7D57244EA8EF9AC0C621896708A59367F9DFB9F54CA84B3F1C9DB1"
  25858. "288B231C3AE0D4FE7344FD2533264720";
  25859. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  25860. "21B13D1E013C7FA1392D03C5F99AF8B30C570C6F98D4EA8E354B63A21D3DAA33"
  25861. "BDE1E888E63355D92FA2B3C36D8FB2CD";
  25862. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  25863. "F3AA443FB107745BF4BD77CB3891674632068A10CA67E3D45DB2266FA7D1FEEB"
  25864. "EFDC63ECCD1AC42EC0CB8668A4FA0AB0";
  25865. #ifdef WOLFSSL_SMALL_STACK
  25866. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25867. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25868. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25869. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25870. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25871. if ((key == NULL) ||
  25872. (r == NULL) ||
  25873. (s == NULL) ||
  25874. (expR == NULL) ||
  25875. (expS == NULL))
  25876. {
  25877. ret = MEMORY_E;
  25878. goto done;
  25879. }
  25880. #endif
  25881. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  25882. if (ret != MP_OKAY) {
  25883. goto done;
  25884. }
  25885. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  25886. if (ret != 0) {
  25887. goto done;
  25888. }
  25889. key_inited = 1;
  25890. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP384R1");
  25891. if (ret != 0) {
  25892. goto done;
  25893. }
  25894. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  25895. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  25896. if (ret != 0) {
  25897. goto done;
  25898. }
  25899. ret = wc_ecc_set_deterministic(key, 1);
  25900. if (ret != 0) {
  25901. goto done;
  25902. }
  25903. do {
  25904. #if defined(WOLFSSL_ASYNC_CRYPT)
  25905. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  25906. #endif
  25907. if (ret == 0)
  25908. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  25909. } while (ret == WC_PENDING_E);
  25910. if (ret != 0) {
  25911. goto done;
  25912. }
  25913. TEST_SLEEP();
  25914. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  25915. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  25916. if (mp_cmp(r, expR) != MP_EQ) {
  25917. ret = WC_TEST_RET_ENC_NC;
  25918. }
  25919. done:
  25920. if (key_inited)
  25921. wc_ecc_free(key);
  25922. #ifdef WOLFSSL_SMALL_STACK
  25923. if (key != NULL)
  25924. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25925. if (r != NULL)
  25926. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25927. if (s != NULL)
  25928. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25929. if (expR != NULL)
  25930. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25931. if (expS != NULL)
  25932. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25933. #endif
  25934. return ret;
  25935. }
  25936. #endif /* HAVE_ECC384 */
  25937. #if defined(HAVE_ECC521)
  25938. /* KAT from RFC6979 */
  25939. static wc_test_ret_t ecc521_test_deterministic_k(WC_RNG* rng)
  25940. {
  25941. wc_test_ret_t ret;
  25942. #ifdef WOLFSSL_SMALL_STACK
  25943. ecc_key *key;
  25944. mp_int *r, *s, *expR, *expS;
  25945. #else
  25946. ecc_key key[1];
  25947. mp_int r[1], s[1], expR[1], expS[1];
  25948. #endif
  25949. int key_inited = 0;
  25950. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  25951. unsigned char hash[32];
  25952. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  25953. "0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75C"
  25954. "AA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83"
  25955. "538";
  25956. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  25957. "1894550D0785932E00EAA23B694F213F8C3121F86DC97A04E5A7167DB4E5BCD3"
  25958. "71123D46E45DB6B5D5370A7F20FB633155D38FFA16D2BD761DCAC474B9A2F502"
  25959. "3A4";
  25960. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  25961. "0493101C962CD4D2FDDF782285E64584139C2F91B47F87FF82354D6630F746A2"
  25962. "8A0DB25741B5B34A828008B22ACC23F924FAAFBD4D33F81EA66956DFEAA2BFDF"
  25963. "CF5";
  25964. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  25965. "1511BB4D675114FE266FC4372B87682BAECC01D3CC62CF2303C92B3526012659"
  25966. "D16876E25C7C1E57648F23B73564D67F61C6F14D527D54972810421E7D87589E"
  25967. "1A7";
  25968. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  25969. "04A171143A83163D6DF460AAF61522695F207A58B95C0644D87E52AA1A347916"
  25970. "E4F7A72930B1BC06DBE22CE3F58264AFD23704CBB63B29B931F7DE6C9D949A7E"
  25971. "CFC";
  25972. #ifdef WOLFSSL_SMALL_STACK
  25973. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25974. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25975. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25976. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25977. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25978. if ((key == NULL) ||
  25979. (r == NULL) ||
  25980. (s == NULL) ||
  25981. (expR == NULL) ||
  25982. (expS == NULL))
  25983. {
  25984. ret = MEMORY_E;
  25985. goto done;
  25986. }
  25987. #endif
  25988. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  25989. if (ret != MP_OKAY) {
  25990. goto done;
  25991. }
  25992. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  25993. if (ret != 0) {
  25994. return WC_TEST_RET_ENC_EC(ret);
  25995. }
  25996. key_inited = 1;
  25997. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP521R1");
  25998. if (ret != 0) {
  25999. goto done;
  26000. }
  26001. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  26002. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  26003. if (ret != 0) {
  26004. goto done;
  26005. }
  26006. ret = wc_ecc_set_deterministic(key, 1);
  26007. if (ret != 0) {
  26008. goto done;
  26009. }
  26010. do {
  26011. #if defined(WOLFSSL_ASYNC_CRYPT)
  26012. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26013. #endif
  26014. if (ret == 0)
  26015. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  26016. } while (ret == WC_PENDING_E);
  26017. if (ret != 0) {
  26018. goto done;
  26019. }
  26020. TEST_SLEEP();
  26021. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  26022. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  26023. if (mp_cmp(r, expR) != MP_EQ) {
  26024. ret = WC_TEST_RET_ENC_NC;
  26025. }
  26026. done:
  26027. if (key_inited)
  26028. wc_ecc_free(key);
  26029. #ifdef WOLFSSL_SMALL_STACK
  26030. if (key != NULL)
  26031. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26032. if (r != NULL)
  26033. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26034. if (s != NULL)
  26035. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26036. if (expR != NULL)
  26037. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26038. if (expS != NULL)
  26039. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26040. #endif
  26041. return ret;
  26042. }
  26043. #endif /* HAVE_ECC521 */
  26044. #endif /* WOLFSSL_PUBLIC_MP */
  26045. #endif /* HAVE_ECC_SIGN && (WOLFSSL_ECDSA_DETERMINISTIC_K ||
  26046. WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  26047. && (!FIPS_VERSION_GE || FIPS_VERSION_GE(5,3)) */
  26048. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  26049. !defined(WOLFSSL_KCAPI_ECC)
  26050. static wc_test_ret_t ecc_test_sign_vectors(WC_RNG* rng)
  26051. {
  26052. wc_test_ret_t ret;
  26053. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26054. ecc_key *key = NULL;
  26055. #else
  26056. ecc_key key[1];
  26057. #endif
  26058. int key_inited = 0;
  26059. byte sig[72];
  26060. word32 sigSz;
  26061. WOLFSSL_SMALL_STACK_STATIC const unsigned char hash[32] = "test wolfSSL deterministic sign";
  26062. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  26063. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  26064. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  26065. WOLFSSL_SMALL_STACK_STATIC const byte k[1] = { 0x02 };
  26066. WOLFSSL_SMALL_STACK_STATIC const byte expSig[71] = {
  26067. 0x30, 0x45, 0x02, 0x20, 0x7c, 0xf2, 0x7b, 0x18,
  26068. 0x8d, 0x03, 0x4f, 0x7e, 0x8a, 0x52, 0x38, 0x03,
  26069. 0x04, 0xb5, 0x1a, 0xc3, 0xc0, 0x89, 0x69, 0xe2,
  26070. 0x77, 0xf2, 0x1b, 0x35, 0xa6, 0x0b, 0x48, 0xfc,
  26071. 0x47, 0x66, 0x99, 0x78, 0x02, 0x21, 0x00, 0xa8,
  26072. 0x43, 0xa0, 0xce, 0x6c, 0x5e, 0x17, 0x8a, 0x53,
  26073. 0x4d, 0xaf, 0xd2, 0x95, 0x78, 0x9f, 0x84, 0x4f,
  26074. 0x94, 0xb8, 0x75, 0xa3, 0x19, 0xa5, 0xd4, 0xdf,
  26075. 0xe1, 0xd4, 0x5e, 0x9d, 0x97, 0xfe, 0x81
  26076. };
  26077. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26078. if ((key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  26079. return MEMORY_E;
  26080. #endif
  26081. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  26082. if (ret != 0) {
  26083. goto done;
  26084. }
  26085. key_inited = 1;
  26086. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  26087. if (ret != 0) {
  26088. goto done;
  26089. }
  26090. #if (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) \
  26091. && (HAVE_FIPS_VERSION > 2)))
  26092. wc_ecc_set_flags(key, WC_ECC_FLAG_DEC_SIGN);
  26093. #endif
  26094. ret = wc_ecc_sign_set_k(k, sizeof(k), key);
  26095. if (ret != 0) {
  26096. goto done;
  26097. }
  26098. sigSz = sizeof(sig);
  26099. do {
  26100. #if defined(WOLFSSL_ASYNC_CRYPT)
  26101. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26102. #endif
  26103. if (ret == 0)
  26104. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  26105. } while (ret == WC_PENDING_E);
  26106. if (ret != 0) {
  26107. goto done;
  26108. }
  26109. TEST_SLEEP();
  26110. if (sigSz != sizeof(expSig)) {
  26111. ret = WC_TEST_RET_ENC_NC;
  26112. goto done;
  26113. }
  26114. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  26115. ret = WC_TEST_RET_ENC_NC;
  26116. goto done;
  26117. }
  26118. sigSz = sizeof(sig);
  26119. do {
  26120. #if defined(WOLFSSL_ASYNC_CRYPT)
  26121. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26122. #endif
  26123. if (ret == 0)
  26124. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  26125. } while (ret == WC_PENDING_E);
  26126. if (ret != 0) {
  26127. goto done;
  26128. }
  26129. TEST_SLEEP();
  26130. done:
  26131. if (key_inited)
  26132. wc_ecc_free(key);
  26133. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26134. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26135. #endif
  26136. return ret;
  26137. }
  26138. #endif
  26139. #if defined(HAVE_ECC_CDH) && defined(HAVE_ECC_DHE)
  26140. static wc_test_ret_t ecc_test_cdh_vectors(WC_RNG* rng)
  26141. {
  26142. wc_test_ret_t ret;
  26143. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26144. ecc_key *pub_key = (ecc_key *)XMALLOC(sizeof *pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26145. ecc_key *priv_key = (ecc_key *)XMALLOC(sizeof *priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26146. #else
  26147. ecc_key pub_key[1], priv_key[1];
  26148. #endif
  26149. byte sharedA[32] = {0}, sharedB[32] = {0};
  26150. word32 x, z;
  26151. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSx = "700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287";
  26152. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSy = "db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac";
  26153. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  26154. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  26155. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  26156. WOLFSSL_SMALL_STACK_STATIC const char* ZIUT = "46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b";
  26157. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26158. if ((pub_key == NULL) ||
  26159. (priv_key == NULL)) {
  26160. ret = MEMORY_E;
  26161. goto done;
  26162. }
  26163. #endif
  26164. XMEMSET(pub_key, 0, sizeof *pub_key);
  26165. XMEMSET(priv_key, 0, sizeof *priv_key);
  26166. /* setup private and public keys */
  26167. ret = wc_ecc_init_ex(pub_key, HEAP_HINT, devId);
  26168. if (ret != 0)
  26169. goto done;
  26170. ret = wc_ecc_init_ex(priv_key, HEAP_HINT, devId);
  26171. if (ret != 0)
  26172. goto done;
  26173. wc_ecc_set_flags(pub_key, WC_ECC_FLAG_COFACTOR);
  26174. wc_ecc_set_flags(priv_key, WC_ECC_FLAG_COFACTOR);
  26175. ret = wc_ecc_import_raw(pub_key, QCAVSx, QCAVSy, NULL, "SECP256R1");
  26176. if (ret != 0)
  26177. goto done;
  26178. ret = wc_ecc_import_raw(priv_key, QIUTx, QIUTy, dIUT, "SECP256R1");
  26179. if (ret != 0)
  26180. goto done;
  26181. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  26182. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  26183. !defined(HAVE_SELFTEST)
  26184. ret = wc_ecc_set_rng(priv_key, rng);
  26185. if (ret != 0)
  26186. goto done;
  26187. #else
  26188. (void)rng;
  26189. #endif
  26190. /* compute ECC Cofactor shared secret */
  26191. x = sizeof(sharedA);
  26192. do {
  26193. #if defined(WOLFSSL_ASYNC_CRYPT)
  26194. ret = wc_AsyncWait(ret, &priv_key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26195. #endif
  26196. if (ret == 0)
  26197. ret = wc_ecc_shared_secret(priv_key, pub_key, sharedA, &x);
  26198. } while (ret == WC_PENDING_E);
  26199. if (ret != 0) {
  26200. goto done;
  26201. }
  26202. TEST_SLEEP();
  26203. /* read in expected Z */
  26204. z = sizeof(sharedB);
  26205. ret = Base16_Decode((const byte*)ZIUT, (word32)XSTRLEN(ZIUT), sharedB, &z);
  26206. if (ret != 0)
  26207. goto done;
  26208. /* compare results */
  26209. if (x != z || XMEMCMP(sharedA, sharedB, x)) {
  26210. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26211. }
  26212. done:
  26213. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26214. if (priv_key) {
  26215. wc_ecc_free(priv_key);
  26216. XFREE(priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26217. }
  26218. if (pub_key) {
  26219. wc_ecc_free(pub_key);
  26220. XFREE(pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26221. }
  26222. #else
  26223. wc_ecc_free(priv_key);
  26224. wc_ecc_free(pub_key);
  26225. #endif
  26226. return ret;
  26227. }
  26228. #endif /* HAVE_ECC_CDH && HAVE_ECC_DHE */
  26229. #endif /* HAVE_ECC_VECTOR_TEST */
  26230. #ifdef HAVE_ECC_KEY_IMPORT
  26231. /* returns 0 on success */
  26232. static wc_test_ret_t ecc_test_make_pub(WC_RNG* rng)
  26233. {
  26234. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26235. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26236. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  26237. ecc_key *pub = (ecc_key *)XMALLOC(sizeof *pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26238. #endif
  26239. byte *exportBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26240. byte *tmp = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26241. #else
  26242. ecc_key key[1];
  26243. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  26244. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  26245. ecc_key pub[1];
  26246. #endif
  26247. byte exportBuf[ECC_BUFSIZE];
  26248. byte tmp[ECC_BUFSIZE];
  26249. #endif
  26250. const byte* msg = (const byte*)"test wolfSSL ECC public gen";
  26251. word32 x;
  26252. word32 tmpSz;
  26253. wc_test_ret_t ret = 0;
  26254. ecc_point* pubPoint = NULL;
  26255. #ifdef HAVE_ECC_VERIFY
  26256. int verify = 0;
  26257. #endif
  26258. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26259. if ((key == NULL) ||
  26260. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  26261. (pub == NULL) ||
  26262. #endif
  26263. (exportBuf == NULL) ||
  26264. (tmp == NULL))
  26265. ERROR_OUT(MEMORY_E, done);
  26266. #endif
  26267. (void)msg;
  26268. (void)verify;
  26269. (void)exportBuf;
  26270. (void)rng;
  26271. wc_ecc_init_ex(key, HEAP_HINT, devId);
  26272. #ifndef NO_ECC256
  26273. #if defined(USE_CERT_BUFFERS_256)
  26274. {
  26275. XMEMCPY(tmp, ecc_key_der_256, (size_t)sizeof_ecc_key_der_256);
  26276. tmpSz = (size_t)sizeof_ecc_key_der_256;
  26277. }
  26278. #elif !defined(NO_FILESYSTEM)
  26279. {
  26280. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  26281. if (!file) {
  26282. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26283. }
  26284. tmpSz = (word32)XFREAD(tmp, 1, ECC_BUFSIZE, file);
  26285. XFCLOSE(file);
  26286. if (tmpSz == 0)
  26287. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26288. }
  26289. #else
  26290. {
  26291. WOLFSSL_MSG("No file system and USE_CERT_BUFFERS_256 not defined.(2)");
  26292. ERROR_OUT(ASN_PARSE_E, done);
  26293. }
  26294. #endif /* USE_CERT_BUFFERS_256 */
  26295. /* import private only then test with */
  26296. ret = wc_ecc_import_private_key(tmp, tmpSz, NULL, 0, NULL);
  26297. if (ret == 0) {
  26298. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26299. }
  26300. ret = wc_ecc_import_private_key(NULL, tmpSz, NULL, 0, key);
  26301. if (ret == 0) {
  26302. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26303. }
  26304. x = 0;
  26305. ret = wc_EccPrivateKeyDecode(tmp, &x, key, tmpSz);
  26306. if (ret != 0)
  26307. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26308. #ifdef HAVE_ECC_KEY_EXPORT
  26309. x = ECC_BUFSIZE;
  26310. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  26311. if (ret != 0)
  26312. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26313. /* make private only key */
  26314. wc_ecc_free(key);
  26315. wc_ecc_init_ex(key, HEAP_HINT, devId);
  26316. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  26317. if (ret != 0)
  26318. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26319. x = ECC_BUFSIZE;
  26320. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  26321. if (ret == 0) {
  26322. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26323. }
  26324. #endif /* HAVE_ECC_KEY_EXPORT */
  26325. ret = wc_ecc_make_pub(NULL, NULL);
  26326. if (ret == 0) {
  26327. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26328. }
  26329. TEST_SLEEP();
  26330. #ifndef WOLFSSL_NO_MALLOC
  26331. pubPoint = wc_ecc_new_point_h(HEAP_HINT);
  26332. if (pubPoint == NULL) {
  26333. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26334. }
  26335. #if !defined(WOLFSSL_CRYPTOCELL)
  26336. ret = wc_ecc_make_pub(key, pubPoint);
  26337. #if defined(WOLFSSL_ASYNC_CRYPT)
  26338. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  26339. #endif
  26340. if (ret != 0)
  26341. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26342. #endif
  26343. TEST_SLEEP();
  26344. #ifdef HAVE_ECC_KEY_EXPORT
  26345. /* export should still fail, is private only key */
  26346. x = ECC_BUFSIZE;
  26347. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  26348. if (ret == 0) {
  26349. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26350. }
  26351. #endif /* HAVE_ECC_KEY_EXPORT */
  26352. #endif /* !WOLFSSL_NO_MALLOC */
  26353. #endif /* !NO_ECC256 */
  26354. /* create a new key since above test for loading key is not supported */
  26355. #if defined(WOLFSSL_CRYPTOCELL) || defined(NO_ECC256) || \
  26356. defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_SE050) || \
  26357. defined(WOLFSSL_SECO_CAAM) || defined(WOLFSSL_IMXRT1170_CAAM)
  26358. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  26359. if (ret != 0)
  26360. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26361. #endif
  26362. #if defined(HAVE_ECC_SIGN) && (!defined(ECC_TIMING_RESISTANT) || \
  26363. (defined(ECC_TIMING_RESISTANT) && !defined(WC_NO_RNG))) && \
  26364. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(HAVE_ECC_DETERMINISTIC_K)
  26365. tmpSz = ECC_BUFSIZE;
  26366. ret = 0;
  26367. do {
  26368. #if defined(WOLFSSL_ASYNC_CRYPT)
  26369. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26370. #endif
  26371. if (ret == 0) {
  26372. ret = wc_ecc_sign_hash(msg, (word32)XSTRLEN((const char* )msg), tmp,
  26373. &tmpSz, rng, key);
  26374. }
  26375. } while (ret == WC_PENDING_E);
  26376. if (ret != 0)
  26377. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26378. TEST_SLEEP();
  26379. #ifdef HAVE_ECC_VERIFY
  26380. /* try verify with private only key */
  26381. ret = 0;
  26382. do {
  26383. #if defined(WOLFSSL_ASYNC_CRYPT)
  26384. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26385. #endif
  26386. if (ret == 0) {
  26387. ret = wc_ecc_verify_hash(tmp, tmpSz, msg,
  26388. (word32)XSTRLEN((const char*)msg), &verify, key);
  26389. }
  26390. } while (ret == WC_PENDING_E);
  26391. if (ret != 0)
  26392. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26393. if (verify != 1) {
  26394. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26395. }
  26396. TEST_SLEEP();
  26397. #ifdef HAVE_ECC_KEY_EXPORT
  26398. /* exporting the public part should now work */
  26399. x = ECC_BUFSIZE;
  26400. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  26401. if (ret != 0)
  26402. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26403. #endif /* HAVE_ECC_KEY_EXPORT */
  26404. #endif /* HAVE_ECC_VERIFY */
  26405. #endif /* HAVE_ECC_SIGN */
  26406. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  26407. /* now test private only key with creating a shared secret */
  26408. x = ECC_BUFSIZE;
  26409. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  26410. if (ret != 0)
  26411. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26412. #if !defined(WOLFSSL_QNX_CAAM) && !defined(WOLFSSL_SE050)
  26413. /* make private only key */
  26414. wc_ecc_free(key);
  26415. wc_ecc_init_ex(key, HEAP_HINT, devId);
  26416. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  26417. if (ret != 0)
  26418. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26419. /* check that public export fails with private only key */
  26420. x = ECC_BUFSIZE;
  26421. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  26422. if (ret == 0) {
  26423. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26424. }
  26425. #endif /* WOLFSSL_QNX_CAAM */
  26426. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  26427. /* make public key for shared secret */
  26428. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  26429. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, pub);
  26430. #ifdef HAVE_ECC_CDH
  26431. wc_ecc_set_flags(key, WC_ECC_FLAG_COFACTOR);
  26432. #endif
  26433. #if defined(WOLFSSL_ASYNC_CRYPT)
  26434. ret = wc_AsyncWait(ret, &pub->asyncDev, WC_ASYNC_FLAG_NONE);
  26435. #endif
  26436. if (ret != 0)
  26437. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26438. TEST_SLEEP();
  26439. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  26440. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  26441. !defined(HAVE_SELFTEST)
  26442. ret = wc_ecc_set_rng(key, rng);
  26443. if (ret != 0)
  26444. goto done;
  26445. #endif
  26446. x = ECC_BUFSIZE;
  26447. do {
  26448. #if defined(WOLFSSL_ASYNC_CRYPT)
  26449. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26450. #endif
  26451. if (ret == 0) {
  26452. ret = wc_ecc_shared_secret(key, pub, exportBuf, &x);
  26453. }
  26454. } while (ret == WC_PENDING_E);
  26455. wc_ecc_free(pub);
  26456. if (ret != 0)
  26457. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26458. TEST_SLEEP();
  26459. #endif /* HAVE_ECC_DHE && HAVE_ECC_KEY_EXPORT && !WC_NO_RNG */
  26460. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  26461. ret = 0;
  26462. done:
  26463. wc_ecc_del_point_h(pubPoint, HEAP_HINT);
  26464. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26465. if (key != NULL) {
  26466. wc_ecc_free(key);
  26467. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26468. }
  26469. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  26470. if (pub != NULL)
  26471. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26472. #endif
  26473. if (exportBuf != NULL)
  26474. XFREE(exportBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26475. if (tmp != NULL)
  26476. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26477. #else
  26478. wc_ecc_free(key);
  26479. #endif
  26480. return ret;
  26481. }
  26482. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  26483. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  26484. static wc_test_ret_t ecc_test_key_decode(WC_RNG* rng, int keySize)
  26485. {
  26486. wc_test_ret_t ret;
  26487. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26488. ecc_key *eccKey = (ecc_key *)XMALLOC(sizeof *eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26489. byte *tmpBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26490. #else
  26491. ecc_key eccKey[1];
  26492. byte tmpBuf[ECC_BUFSIZE];
  26493. #endif
  26494. word32 tmpSz;
  26495. word32 idx;
  26496. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26497. if ((eccKey == NULL) || (tmpBuf == NULL))
  26498. ERROR_OUT(MEMORY_E, done);
  26499. #endif
  26500. ret = wc_ecc_init(eccKey);
  26501. if (ret != 0) {
  26502. goto done;
  26503. }
  26504. ret = wc_ecc_make_key(rng, keySize, eccKey);
  26505. #if defined(WOLFSSL_ASYNC_CRYPT)
  26506. ret = wc_AsyncWait(ret, &eccKey->asyncDev, WC_ASYNC_FLAG_NONE);
  26507. #endif
  26508. if (ret != 0) {
  26509. goto done;
  26510. }
  26511. tmpSz = ECC_BUFSIZE;
  26512. ret = wc_EccKeyToDer(eccKey, tmpBuf, tmpSz);
  26513. wc_ecc_free(eccKey);
  26514. if (ret < 0) {
  26515. goto done;
  26516. }
  26517. tmpSz = (word32)ret;
  26518. ret = wc_ecc_init(eccKey);
  26519. if (ret != 0) {
  26520. goto done;
  26521. }
  26522. idx = 0;
  26523. ret = wc_EccPrivateKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  26524. if (ret != 0) {
  26525. goto done;
  26526. }
  26527. wc_ecc_free(eccKey);
  26528. ret = wc_ecc_init(eccKey);
  26529. if (ret != 0) {
  26530. goto done;
  26531. }
  26532. idx = 0;
  26533. ret = wc_EccPublicKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  26534. if (ret != 0) {
  26535. goto done;
  26536. }
  26537. ret = 0;
  26538. done:
  26539. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26540. if (eccKey != NULL) {
  26541. wc_ecc_free(eccKey);
  26542. XFREE(eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26543. }
  26544. if (tmpBuf != NULL)
  26545. XFREE(tmpBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26546. #else
  26547. wc_ecc_free(eccKey);
  26548. #endif
  26549. return ret;
  26550. }
  26551. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  26552. #endif /* HAVE_ECC_KEY_IMPORT */
  26553. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  26554. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  26555. static wc_test_ret_t ecc_test_key_gen(WC_RNG* rng, int keySize)
  26556. {
  26557. wc_test_ret_t ret = 0;
  26558. int derSz;
  26559. #ifdef HAVE_PKCS8
  26560. word32 pkcs8Sz;
  26561. #endif
  26562. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26563. byte *der = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26564. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26565. #else
  26566. byte der[ECC_BUFSIZE];
  26567. ecc_key userA[1];
  26568. #endif
  26569. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26570. if ((der == NULL) || (userA == NULL))
  26571. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), done);
  26572. #endif
  26573. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  26574. if (ret != 0)
  26575. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26576. ret = wc_ecc_make_key(rng, keySize, userA);
  26577. #if defined(WOLFSSL_ASYNC_CRYPT)
  26578. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  26579. #endif
  26580. if (ret != 0)
  26581. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26582. TEST_SLEEP();
  26583. ret = wc_ecc_check_key(userA);
  26584. if (ret != 0)
  26585. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26586. TEST_SLEEP();
  26587. derSz = wc_EccKeyToDer(userA, der, ECC_BUFSIZE);
  26588. if (derSz < 0) {
  26589. ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
  26590. }
  26591. ret = SaveDerAndPem(der, derSz, eccCaKeyTempFile, eccCaKeyPemFile,
  26592. ECC_PRIVATEKEY_TYPE);
  26593. if (ret != 0)
  26594. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26595. /* test export of public key */
  26596. derSz = wc_EccPublicKeyToDer(userA, der, ECC_BUFSIZE, 1);
  26597. if (derSz < 0) {
  26598. ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
  26599. }
  26600. if (derSz == 0) {
  26601. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26602. }
  26603. #ifdef HAVE_COMP_KEY
  26604. /* test export of compressed public key */
  26605. derSz = wc_EccPublicKeyToDer_ex(userA, der, ECC_BUFSIZE, 1, 1);
  26606. if (derSz < 0) {
  26607. ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
  26608. }
  26609. if (derSz == 0) {
  26610. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26611. }
  26612. #endif
  26613. ret = SaveDerAndPem(der, derSz, eccPubKeyDerFile, NULL, 0);
  26614. if (ret != 0)
  26615. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26616. #ifdef HAVE_PKCS8
  26617. /* test export of PKCS#8 unencrypted private key */
  26618. pkcs8Sz = FOURK_BUF;
  26619. derSz = wc_EccPrivateKeyToPKCS8(userA, der, &pkcs8Sz);
  26620. if (derSz < 0) {
  26621. ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
  26622. }
  26623. if (derSz == 0) {
  26624. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26625. }
  26626. ret = SaveDerAndPem(der, derSz, eccPkcs8KeyDerFile, NULL, 0);
  26627. if (ret != 0) {
  26628. goto done;
  26629. }
  26630. #endif /* HAVE_PKCS8 */
  26631. done:
  26632. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26633. if (der != NULL)
  26634. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26635. if (userA != NULL) {
  26636. wc_ecc_free(userA);
  26637. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26638. }
  26639. #else
  26640. wc_ecc_free(userA);
  26641. #endif
  26642. return ret;
  26643. }
  26644. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  26645. static wc_test_ret_t ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount,
  26646. int curve_id, const ecc_set_type* dp)
  26647. {
  26648. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  26649. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  26650. WC_DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  26651. WC_DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  26652. word32 y;
  26653. #endif
  26654. #ifdef HAVE_ECC_KEY_EXPORT
  26655. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  26656. WC_DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  26657. #endif
  26658. word32 x = 0;
  26659. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  26660. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC)) && \
  26661. defined(HAVE_ECC_SIGN)
  26662. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  26663. WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  26664. int i;
  26665. #ifdef HAVE_ECC_VERIFY
  26666. int verify;
  26667. #endif /* HAVE_ECC_VERIFY */
  26668. #endif /* HAVE_ECC_SIGN */
  26669. wc_test_ret_t ret;
  26670. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26671. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26672. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26673. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26674. #else
  26675. ecc_key userA[1];
  26676. ecc_key userB[1];
  26677. ecc_key pubKey[1];
  26678. #endif
  26679. #ifndef WC_NO_RNG
  26680. int curveSize;
  26681. #endif
  26682. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  26683. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  26684. WC_ALLOC_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  26685. WC_ALLOC_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  26686. #endif
  26687. #ifdef HAVE_ECC_KEY_EXPORT
  26688. WC_ALLOC_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  26689. #endif
  26690. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  26691. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC)) && \
  26692. defined(HAVE_ECC_SIGN)
  26693. WC_ALLOC_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  26694. WC_ALLOC_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  26695. #endif
  26696. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  26697. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  26698. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  26699. if (sharedA == NULL || sharedB == NULL)
  26700. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26701. #endif
  26702. #ifdef HAVE_ECC_KEY_EXPORT
  26703. if (exportBuf == NULL)
  26704. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26705. #endif
  26706. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  26707. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC)) && \
  26708. defined(HAVE_ECC_SIGN)
  26709. if (sig == NULL || digest == NULL)
  26710. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26711. #endif
  26712. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  26713. (void)testVerifyCount;
  26714. (void)dp;
  26715. (void)x;
  26716. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26717. if ((userA == NULL) ||
  26718. (userB == NULL) ||
  26719. (pubKey == NULL))
  26720. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26721. #endif
  26722. XMEMSET(userA, 0, sizeof *userA);
  26723. XMEMSET(userB, 0, sizeof *userB);
  26724. XMEMSET(pubKey, 0, sizeof *pubKey);
  26725. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  26726. if (ret != 0)
  26727. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26728. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  26729. if (ret != 0)
  26730. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26731. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  26732. if (ret != 0)
  26733. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26734. #ifdef WOLFSSL_CUSTOM_CURVES
  26735. if (dp != NULL) {
  26736. ret = wc_ecc_set_custom_curve(userA, dp);
  26737. if (ret != 0)
  26738. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26739. ret = wc_ecc_set_custom_curve(userB, dp);
  26740. if (ret != 0)
  26741. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26742. }
  26743. #endif
  26744. #ifndef WC_NO_RNG
  26745. ret = wc_ecc_make_key_ex(rng, keySize, userA, curve_id);
  26746. #if defined(WOLFSSL_ASYNC_CRYPT)
  26747. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  26748. #endif
  26749. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  26750. if (ret == NO_VALID_DEVID) {
  26751. ret = 0;
  26752. goto done; /* no software case */
  26753. }
  26754. #endif
  26755. if (ret == ECC_CURVE_OID_E)
  26756. goto done; /* catch case, where curve is not supported */
  26757. if (ret != 0)
  26758. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26759. TEST_SLEEP();
  26760. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  26761. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  26762. if (curveSize != userA->dp->size)
  26763. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26764. }
  26765. ret = wc_ecc_check_key(userA);
  26766. if (ret != 0)
  26767. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26768. TEST_SLEEP();
  26769. /* ATECC508/608 configuration may not support more than one ECDH key */
  26770. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  26771. ret = wc_ecc_make_key_ex(rng, keySize, userB, curve_id);
  26772. #if defined(WOLFSSL_ASYNC_CRYPT)
  26773. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  26774. #endif
  26775. if (ret != 0)
  26776. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26777. TEST_SLEEP();
  26778. /* only perform the below tests if the key size matches */
  26779. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  26780. /* Not an error, just not a key size match */
  26781. WARNING_OUT(ECC_CURVE_OID_E, done);
  26782. #ifdef HAVE_ECC_DHE
  26783. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  26784. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  26785. !defined(HAVE_SELFTEST)
  26786. ret = wc_ecc_set_rng(userA, rng);
  26787. if (ret != 0)
  26788. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26789. ret = wc_ecc_set_rng(userB, rng);
  26790. if (ret != 0)
  26791. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26792. #endif
  26793. x = ECC_SHARED_SIZE;
  26794. do {
  26795. #if defined(WOLFSSL_ASYNC_CRYPT)
  26796. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26797. #endif
  26798. if (ret == 0)
  26799. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  26800. } while (ret == WC_PENDING_E);
  26801. if (ret != 0)
  26802. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26803. TEST_SLEEP();
  26804. y = ECC_SHARED_SIZE;
  26805. do {
  26806. #if defined(WOLFSSL_ASYNC_CRYPT)
  26807. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26808. #endif
  26809. if (ret == 0)
  26810. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  26811. } while (ret == WC_PENDING_E);
  26812. if (ret != 0)
  26813. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26814. if (y != x)
  26815. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26816. if (XMEMCMP(sharedA, sharedB, x))
  26817. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26818. TEST_SLEEP();
  26819. #ifdef HAVE_ECC_CDH
  26820. /* add cofactor flag */
  26821. wc_ecc_set_flags(userA, WC_ECC_FLAG_COFACTOR);
  26822. wc_ecc_set_flags(userB, WC_ECC_FLAG_COFACTOR);
  26823. x = ECC_SHARED_SIZE;
  26824. do {
  26825. #if defined(WOLFSSL_ASYNC_CRYPT)
  26826. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26827. #endif
  26828. if (ret == 0)
  26829. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  26830. } while (ret == WC_PENDING_E);
  26831. if (ret != 0)
  26832. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26833. TEST_SLEEP();
  26834. y = ECC_SHARED_SIZE;
  26835. do {
  26836. #if defined(WOLFSSL_ASYNC_CRYPT)
  26837. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26838. #endif
  26839. if (ret == 0)
  26840. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  26841. } while (ret == WC_PENDING_E);
  26842. if (ret != 0)
  26843. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26844. if (y != x)
  26845. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26846. if (XMEMCMP(sharedA, sharedB, x))
  26847. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26848. TEST_SLEEP();
  26849. /* remove cofactor flag */
  26850. wc_ecc_set_flags(userA, 0);
  26851. wc_ecc_set_flags(userB, 0);
  26852. #endif /* HAVE_ECC_CDH */
  26853. #endif /* HAVE_ECC_DHE */
  26854. #endif /* !WOLFSSL_ATECC508A && WOLFSSL_ATECC608A */
  26855. #ifdef HAVE_ECC_KEY_EXPORT
  26856. x = ECC_KEY_EXPORT_BUF_SIZE;
  26857. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  26858. if (ret != 0)
  26859. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26860. #ifdef HAVE_ECC_KEY_IMPORT
  26861. #ifdef WOLFSSL_CUSTOM_CURVES
  26862. if (dp != NULL) {
  26863. ret = wc_ecc_set_custom_curve(pubKey, dp);
  26864. if (ret != 0)
  26865. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26866. }
  26867. #endif
  26868. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  26869. if (ret != 0)
  26870. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26871. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  26872. #ifdef HAVE_ECC_DHE
  26873. y = ECC_SHARED_SIZE;
  26874. do {
  26875. #if defined(WOLFSSL_ASYNC_CRYPT)
  26876. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26877. #endif
  26878. if (ret == 0)
  26879. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  26880. } while (ret == WC_PENDING_E);
  26881. if (ret != 0)
  26882. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26883. if (XMEMCMP(sharedA, sharedB, y))
  26884. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26885. TEST_SLEEP();
  26886. #endif /* HAVE_ECC_DHE */
  26887. #ifdef HAVE_COMP_KEY
  26888. /* try compressed export / import too */
  26889. x = ECC_KEY_EXPORT_BUF_SIZE;
  26890. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  26891. if (ret != 0)
  26892. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26893. wc_ecc_free(pubKey);
  26894. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  26895. if (ret != 0)
  26896. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26897. #ifdef WOLFSSL_CUSTOM_CURVES
  26898. if (dp != NULL) {
  26899. ret = wc_ecc_set_custom_curve(pubKey, dp);
  26900. if (ret != 0)
  26901. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26902. }
  26903. #endif
  26904. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  26905. if (ret != 0)
  26906. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26907. #ifdef HAVE_ECC_DHE
  26908. y = ECC_SHARED_SIZE;
  26909. do {
  26910. #if defined(WOLFSSL_ASYNC_CRYPT)
  26911. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26912. #endif
  26913. if (ret == 0)
  26914. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  26915. } while (ret == WC_PENDING_E);
  26916. if (ret != 0)
  26917. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26918. if (XMEMCMP(sharedA, sharedB, y))
  26919. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26920. TEST_SLEEP();
  26921. #endif /* HAVE_ECC_DHE */
  26922. #endif /* HAVE_COMP_KEY */
  26923. #endif /* !WOLFSSL_ATECC508A && !WOLFSSL_ATECC608A */
  26924. #endif /* !WC_NO_RNG */
  26925. #endif /* HAVE_ECC_KEY_IMPORT */
  26926. #endif /* HAVE_ECC_KEY_EXPORT */
  26927. /* For KCAPI cannot sign using generated ECDH key */
  26928. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  26929. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC))
  26930. #ifdef HAVE_ECC_SIGN
  26931. /* ECC w/out Shamir has issue with all 0 digest */
  26932. /* WC_BIGINT doesn't have 0 len well on hardware */
  26933. /* Cryptocell has issues with all 0 digest */
  26934. #if defined(ECC_SHAMIR) && !defined(WOLFSSL_ASYNC_CRYPT) && \
  26935. !defined(WOLFSSL_CRYPTOCELL)
  26936. /* test DSA sign hash with zeros */
  26937. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  26938. digest[i] = 0;
  26939. }
  26940. x = ECC_SIG_SIZE;
  26941. do {
  26942. #if defined(WOLFSSL_ASYNC_CRYPT)
  26943. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26944. #endif
  26945. if (ret == 0)
  26946. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  26947. userA);
  26948. } while (ret == WC_PENDING_E);
  26949. if (ret != 0)
  26950. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26951. TEST_SLEEP();
  26952. #ifdef HAVE_ECC_VERIFY
  26953. for (i=0; i<testVerifyCount; i++) {
  26954. verify = 0;
  26955. do {
  26956. #if defined(WOLFSSL_ASYNC_CRYPT)
  26957. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26958. #endif
  26959. if (ret == 0)
  26960. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  26961. &verify, userA);
  26962. } while (ret == WC_PENDING_E);
  26963. if (ret != 0)
  26964. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26965. if (verify != 1)
  26966. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26967. TEST_SLEEP();
  26968. }
  26969. #endif /* HAVE_ECC_VERIFY */
  26970. #endif /* ECC_SHAMIR && !WOLFSSL_ASYNC_CRYPT && !WOLFSSL_CRYPTOCELL */
  26971. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  26972. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  26973. digest[i] = (byte)i;
  26974. }
  26975. x = ECC_SIG_SIZE;
  26976. do {
  26977. #if defined(WOLFSSL_ASYNC_CRYPT)
  26978. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26979. #endif
  26980. if (ret == 0)
  26981. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  26982. } while (ret == WC_PENDING_E);
  26983. if (ret != 0)
  26984. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26985. TEST_SLEEP();
  26986. #ifdef HAVE_ECC_VERIFY
  26987. for (i=0; i<testVerifyCount; i++) {
  26988. verify = 0;
  26989. do {
  26990. #if defined(WOLFSSL_ASYNC_CRYPT)
  26991. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26992. #endif
  26993. if (ret == 0)
  26994. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify, userA);
  26995. } while (ret == WC_PENDING_E);
  26996. if (ret != 0)
  26997. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26998. if (verify != 1)
  26999. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  27000. TEST_SLEEP();
  27001. }
  27002. #endif /* HAVE_ECC_VERIFY */
  27003. #endif /* HAVE_ECC_SIGN */
  27004. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT &&
  27005. * !WC_NO_RNG && !WOLFSSL_KCAPI_ECC) */
  27006. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  27007. !defined(WOLFSSL_ATECC508) && !defined(WOLFSSL_ATECC608A) && \
  27008. !defined(WOLFSSL_KCAPI_ECC)
  27009. x = ECC_KEY_EXPORT_BUF_SIZE;
  27010. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  27011. if (ret != 0)
  27012. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27013. #elif defined(HAVE_ECC_KEY_EXPORT)
  27014. (void)exportBuf;
  27015. #endif /* HAVE_ECC_KEY_EXPORT */
  27016. done:
  27017. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27018. if (userA != NULL) {
  27019. wc_ecc_free(userA);
  27020. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27021. }
  27022. if (userB != NULL) {
  27023. wc_ecc_free(userB);
  27024. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27025. }
  27026. if (pubKey != NULL) {
  27027. wc_ecc_free(pubKey);
  27028. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27029. }
  27030. #else
  27031. wc_ecc_free(pubKey);
  27032. wc_ecc_free(userB);
  27033. wc_ecc_free(userA);
  27034. #endif
  27035. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  27036. WC_FREE_VAR(sharedA, HEAP_HINT);
  27037. WC_FREE_VAR(sharedB, HEAP_HINT);
  27038. #endif
  27039. #ifdef HAVE_ECC_KEY_EXPORT
  27040. WC_FREE_VAR(exportBuf, HEAP_HINT);
  27041. #endif
  27042. #ifdef HAVE_ECC_SIGN
  27043. WC_FREE_VAR(sig, HEAP_HINT);
  27044. WC_FREE_VAR(digest, HEAP_HINT);
  27045. #endif
  27046. (void)keySize;
  27047. (void)curve_id;
  27048. (void)rng;
  27049. return ret;
  27050. }
  27051. #undef ECC_TEST_VERIFY_COUNT
  27052. #define ECC_TEST_VERIFY_COUNT 2
  27053. static wc_test_ret_t ecc_test_curve(WC_RNG* rng, int keySize, int curve_id)
  27054. {
  27055. wc_test_ret_t ret;
  27056. WOLFSSL_MSG_EX("ecc_test_curve keySize = %d", keySize);
  27057. #if FIPS_VERSION3_GE(6,0,0)
  27058. #ifdef DEBUG_WOLFSSL
  27059. printf("keySize is %d\n", keySize);
  27060. #endif
  27061. if (keySize < WC_ECC_FIPS_GEN_MIN) {
  27062. goto skip_A;
  27063. }
  27064. #endif
  27065. ret = ecc_test_curve_size(rng, keySize, ECC_TEST_VERIFY_COUNT, curve_id,
  27066. NULL);
  27067. if (ret < 0) {
  27068. if (ret == ECC_CURVE_OID_E) {
  27069. /* ignore error for curves not found */
  27070. /* some curve sizes are only available with:
  27071. HAVE_ECC_SECPR2, HAVE_ECC_SECPR3, HAVE_ECC_BRAINPOOL
  27072. and HAVE_ECC_KOBLITZ */
  27073. }
  27074. else {
  27075. printf("ecc_test_curve_size %d failed!\n", keySize);
  27076. return ret;
  27077. }
  27078. }
  27079. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  27080. #if FIPS_VERSION3_GE(6,0,0)
  27081. skip_A:
  27082. #endif
  27083. #ifdef HAVE_ECC_VECTOR_TEST
  27084. ret = ecc_test_vector(keySize);
  27085. if (ret < 0) {
  27086. printf("ecc_test_vector %d failed!\n", keySize);
  27087. return ret;
  27088. }
  27089. #endif
  27090. #if FIPS_VERSION3_GE(6,0,0)
  27091. if (keySize < WC_ECC_FIPS_GEN_MIN) {
  27092. goto skip_B;
  27093. }
  27094. #endif
  27095. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  27096. !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  27097. ret = ecc_test_key_decode(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_decode %d failed!\n", keySize);
  27104. return ret;
  27105. }
  27106. }
  27107. #endif
  27108. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  27109. ret = ecc_test_key_gen(rng, keySize);
  27110. if (ret < 0) {
  27111. if (ret == ECC_CURVE_OID_E) {
  27112. /* ignore error for curves not found */
  27113. }
  27114. else {
  27115. printf("ecc_test_key_gen %d failed!\n", keySize);
  27116. return ret;
  27117. }
  27118. }
  27119. #endif
  27120. #if FIPS_VERSION3_GE(6,0,0)
  27121. skip_B:
  27122. #endif
  27123. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  27124. return 0;
  27125. }
  27126. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  27127. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  27128. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  27129. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  27130. static wc_test_ret_t ecc_point_test(void)
  27131. {
  27132. wc_test_ret_t ret;
  27133. ecc_point* point;
  27134. ecc_point* point2;
  27135. #ifdef HAVE_COMP_KEY
  27136. ecc_point* point3;
  27137. ecc_point* point4;
  27138. #endif
  27139. word32 outLen;
  27140. byte out[65];
  27141. byte der[] = { 0x04, /* = Uncompressed */
  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. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  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. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  27151. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  27152. byte derComp0[] = { 0x02, /* = Compressed, y even */
  27153. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27154. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27155. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27156. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  27157. byte derComp1[] = { 0x03, /* = Compressed, y odd */
  27158. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27159. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27160. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27161. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  27162. #endif
  27163. byte altDer[] = { 0x04, /* = Uncompressed */
  27164. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27165. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27166. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27167. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27168. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27169. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27170. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27171. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  27172. int curve_idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
  27173. /* if curve P256 is not enabled then test should not fail */
  27174. if (curve_idx == ECC_CURVE_INVALID)
  27175. return 0;
  27176. outLen = sizeof(out);
  27177. point = wc_ecc_new_point();
  27178. if (point == NULL)
  27179. return WC_TEST_RET_ENC_ERRNO;
  27180. point2 = wc_ecc_new_point();
  27181. if (point2 == NULL) {
  27182. wc_ecc_del_point(point);
  27183. return WC_TEST_RET_ENC_NC;
  27184. }
  27185. #ifdef HAVE_COMP_KEY
  27186. point3 = wc_ecc_new_point();
  27187. if (point3 == NULL) {
  27188. wc_ecc_del_point(point2);
  27189. wc_ecc_del_point(point);
  27190. return WC_TEST_RET_ENC_NC;
  27191. }
  27192. point4 = wc_ecc_new_point();
  27193. if (point4 == NULL) {
  27194. wc_ecc_del_point(point3);
  27195. wc_ecc_del_point(point2);
  27196. wc_ecc_del_point(point);
  27197. return WC_TEST_RET_ENC_NC;
  27198. }
  27199. #endif
  27200. /* Parameter Validation testing. */
  27201. wc_ecc_del_point(NULL);
  27202. ret = wc_ecc_import_point_der(NULL, sizeof(der), curve_idx, point);
  27203. if (ret != ECC_BAD_ARG_E) {
  27204. ret = WC_TEST_RET_ENC_EC(ret);
  27205. goto done;
  27206. }
  27207. ret = wc_ecc_import_point_der(der, sizeof(der), ECC_CURVE_INVALID, point);
  27208. if (ret != ECC_BAD_ARG_E) {
  27209. ret = WC_TEST_RET_ENC_EC(ret);
  27210. goto done;
  27211. }
  27212. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, NULL);
  27213. if (ret != ECC_BAD_ARG_E) {
  27214. ret = WC_TEST_RET_ENC_EC(ret);
  27215. goto done;
  27216. }
  27217. ret = wc_ecc_export_point_der(-1, point, out, &outLen);
  27218. if (ret != ECC_BAD_ARG_E) {
  27219. ret = WC_TEST_RET_ENC_EC(ret);
  27220. goto done;
  27221. }
  27222. ret = wc_ecc_export_point_der(curve_idx, NULL, out, &outLen);
  27223. if (ret != ECC_BAD_ARG_E) {
  27224. ret = WC_TEST_RET_ENC_EC(ret);
  27225. goto done;
  27226. }
  27227. ret = wc_ecc_export_point_der(curve_idx, point, NULL, &outLen);
  27228. if (ret != LENGTH_ONLY_E || outLen != sizeof(out)) {
  27229. ret = WC_TEST_RET_ENC_EC(ret);
  27230. goto done;
  27231. }
  27232. ret = wc_ecc_export_point_der(curve_idx, point, out, NULL);
  27233. if (ret != ECC_BAD_ARG_E) {
  27234. ret = WC_TEST_RET_ENC_EC(ret);
  27235. goto done;
  27236. }
  27237. outLen = 0;
  27238. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  27239. if (ret != BUFFER_E) {
  27240. ret = WC_TEST_RET_ENC_EC(ret);
  27241. goto done;
  27242. }
  27243. ret = wc_ecc_copy_point(NULL, NULL);
  27244. if (ret != ECC_BAD_ARG_E) {
  27245. ret = WC_TEST_RET_ENC_EC(ret);
  27246. goto done;
  27247. }
  27248. ret = wc_ecc_copy_point(NULL, point2);
  27249. if (ret != ECC_BAD_ARG_E) {
  27250. ret = WC_TEST_RET_ENC_EC(ret);
  27251. goto done;
  27252. }
  27253. ret = wc_ecc_copy_point(point, NULL);
  27254. if (ret != ECC_BAD_ARG_E) {
  27255. ret = WC_TEST_RET_ENC_EC(ret);
  27256. goto done;
  27257. }
  27258. ret = wc_ecc_cmp_point(NULL, NULL);
  27259. if (ret != BAD_FUNC_ARG) {
  27260. ret = WC_TEST_RET_ENC_EC(ret);
  27261. goto done;
  27262. }
  27263. ret = wc_ecc_cmp_point(NULL, point2);
  27264. if (ret != BAD_FUNC_ARG) {
  27265. ret = WC_TEST_RET_ENC_EC(ret);
  27266. goto done;
  27267. }
  27268. ret = wc_ecc_cmp_point(point, NULL);
  27269. if (ret != BAD_FUNC_ARG) {
  27270. ret = WC_TEST_RET_ENC_EC(ret);
  27271. goto done;
  27272. }
  27273. /* Use API. */
  27274. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, point);
  27275. if (ret != 0) {
  27276. ret = WC_TEST_RET_ENC_EC(ret);
  27277. goto done;
  27278. }
  27279. outLen = sizeof(out);
  27280. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  27281. if (ret != 0) {
  27282. ret = WC_TEST_RET_ENC_EC(ret);
  27283. goto done;
  27284. }
  27285. if (outLen != sizeof(der)) {
  27286. ret = WC_TEST_RET_ENC_NC;
  27287. goto done;
  27288. }
  27289. if (XMEMCMP(out, der, outLen) != 0) {
  27290. ret = WC_TEST_RET_ENC_NC;
  27291. goto done;
  27292. }
  27293. ret = wc_ecc_copy_point(point2, point);
  27294. if (ret != MP_OKAY) {
  27295. ret = WC_TEST_RET_ENC_EC(ret);
  27296. goto done;
  27297. }
  27298. ret = wc_ecc_cmp_point(point2, point);
  27299. if (ret != MP_EQ) {
  27300. ret = WC_TEST_RET_ENC_EC(ret);
  27301. goto done;
  27302. }
  27303. ret = wc_ecc_import_point_der(altDer, sizeof(altDer), curve_idx, point2);
  27304. if (ret != 0) {
  27305. ret = WC_TEST_RET_ENC_EC(ret);
  27306. goto done;
  27307. }
  27308. ret = wc_ecc_cmp_point(point2, point);
  27309. if (ret != MP_GT) {
  27310. ret = WC_TEST_RET_ENC_EC(ret);
  27311. goto done;
  27312. }
  27313. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  27314. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  27315. ret = wc_ecc_import_point_der(derComp0, sizeof(derComp0)*2-1, curve_idx, point3);
  27316. if (ret != 0) {
  27317. ret = WC_TEST_RET_ENC_EC(ret);
  27318. goto done;
  27319. }
  27320. ret = wc_ecc_import_point_der_ex(derComp0, sizeof(derComp0), curve_idx, point4, 0);
  27321. if (ret != 0) {
  27322. ret = WC_TEST_RET_ENC_EC(ret);
  27323. goto done;
  27324. }
  27325. ret = wc_ecc_cmp_point(point3, point4);
  27326. if (ret != MP_EQ) {
  27327. ret = WC_TEST_RET_ENC_EC(ret);
  27328. goto done;
  27329. }
  27330. ret = wc_ecc_import_point_der(derComp1, sizeof(derComp1)*2-1, curve_idx, point3);
  27331. if (ret != 0) {
  27332. ret = WC_TEST_RET_ENC_EC(ret);
  27333. goto done;
  27334. }
  27335. ret = wc_ecc_import_point_der_ex(derComp1, sizeof(derComp1), curve_idx, point4, 0);
  27336. if (ret != 0) {
  27337. ret = WC_TEST_RET_ENC_EC(ret);
  27338. goto done;
  27339. }
  27340. ret = wc_ecc_cmp_point(point3, point4);
  27341. if (ret != MP_EQ) {
  27342. ret = WC_TEST_RET_ENC_EC(ret);
  27343. goto done;
  27344. }
  27345. #endif
  27346. done:
  27347. #ifdef HAVE_COMP_KEY
  27348. wc_ecc_del_point(point4);
  27349. wc_ecc_del_point(point3);
  27350. #endif
  27351. wc_ecc_del_point(point2);
  27352. wc_ecc_del_point(point);
  27353. return ret;
  27354. }
  27355. #endif /* !WOLFSSL_ATECC508A && HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  27356. #if !defined(NO_SIG_WRAPPER) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  27357. static wc_test_ret_t ecc_sig_test(WC_RNG* rng, ecc_key* key)
  27358. {
  27359. wc_test_ret_t ret;
  27360. word32 sigSz;
  27361. int size;
  27362. byte out[ECC_MAX_SIG_SIZE];
  27363. byte in[] = TEST_STRING;
  27364. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  27365. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  27366. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  27367. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  27368. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  27369. };
  27370. word32 inLen = (word32)XSTRLEN((char*)in);
  27371. size = wc_ecc_sig_size(key);
  27372. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, sizeof(*key));
  27373. if (ret != size)
  27374. return WC_TEST_RET_ENC_NC;
  27375. sigSz = (word32)ret;
  27376. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  27377. inLen, out, &sigSz, key, sizeof(*key), rng);
  27378. if (ret != 0)
  27379. return WC_TEST_RET_ENC_EC(ret);
  27380. TEST_SLEEP();
  27381. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  27382. inLen, out, sigSz, key, sizeof(*key));
  27383. if (ret != 0)
  27384. return WC_TEST_RET_ENC_EC(ret);
  27385. TEST_SLEEP();
  27386. sigSz = (word32)sizeof(out);
  27387. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  27388. hash, (int)sizeof(hash), out, &sigSz, key, sizeof(*key), rng);
  27389. if (ret != 0)
  27390. return WC_TEST_RET_ENC_EC(ret);
  27391. TEST_SLEEP();
  27392. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  27393. hash, (int)sizeof(hash), out, sigSz, key, sizeof(*key));
  27394. if (ret != 0)
  27395. return WC_TEST_RET_ENC_EC(ret);
  27396. TEST_SLEEP();
  27397. return 0;
  27398. }
  27399. #endif
  27400. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  27401. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  27402. static wc_test_ret_t ecc_exp_imp_test(ecc_key* key)
  27403. {
  27404. wc_test_ret_t ret;
  27405. int curve_id;
  27406. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27407. ecc_key *keyImp = (ecc_key *)XMALLOC(sizeof *keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27408. #else
  27409. ecc_key keyImp[1];
  27410. #endif
  27411. byte priv[32];
  27412. word32 privLen;
  27413. byte pub[65*2];
  27414. word32 pubLen, pubLenX, pubLenY;
  27415. const char qx[] = "7a4e287890a1a47ad3457e52f2f76a83"
  27416. "ce46cbc947616d0cbaa82323818a793d";
  27417. const char qy[] = "eec4084f5b29ebf29c44cce3b3059610"
  27418. "922f8b30ea6e8811742ac7238fe87308";
  27419. const char d[] = "8c14b793cb19137e323a6d2e2a870bca"
  27420. "2e7a493ec1153b3a95feb8a4873f8d08";
  27421. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27422. if (keyImp == NULL)
  27423. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  27424. #endif
  27425. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  27426. privLen = sizeof(priv);
  27427. ret = wc_ecc_export_private_only(key, priv, &privLen);
  27428. if (ret != 0) {
  27429. ret = WC_TEST_RET_ENC_EC(ret);
  27430. goto done;
  27431. }
  27432. pubLen = sizeof(pub);
  27433. ret = wc_ecc_export_point_der(key->idx, &key->pubkey, pub, &pubLen);
  27434. if (ret != 0) {
  27435. ret = WC_TEST_RET_ENC_EC(ret);
  27436. goto done;
  27437. }
  27438. ret = wc_ecc_import_private_key(priv, privLen, pub, pubLen, keyImp);
  27439. if (ret != 0) {
  27440. ret = WC_TEST_RET_ENC_EC(ret);
  27441. goto done;
  27442. }
  27443. wc_ecc_free(keyImp);
  27444. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  27445. ret = wc_ecc_import_raw_ex(keyImp, qx, qy, d, ECC_SECP256R1);
  27446. if (ret != 0) {
  27447. ret = WC_TEST_RET_ENC_EC(ret);
  27448. goto done;
  27449. }
  27450. wc_ecc_free(keyImp);
  27451. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  27452. curve_id = wc_ecc_get_curve_id(key->idx);
  27453. if (curve_id < 0) {
  27454. ret = WC_TEST_RET_ENC_EC(curve_id);
  27455. goto done;
  27456. }
  27457. /* test import private only */
  27458. ret = wc_ecc_import_private_key_ex(priv, privLen, NULL, 0, keyImp,
  27459. curve_id);
  27460. if (ret != 0) {
  27461. ret = WC_TEST_RET_ENC_EC(ret);
  27462. goto done;
  27463. }
  27464. wc_ecc_free(keyImp);
  27465. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  27466. /* test export public raw */
  27467. pubLenX = pubLenY = 32;
  27468. ret = wc_ecc_export_public_raw(key, pub, &pubLenX, &pub[32], &pubLenY);
  27469. if (ret != 0) {
  27470. ret = WC_TEST_RET_ENC_EC(ret);
  27471. goto done;
  27472. }
  27473. #ifndef HAVE_SELFTEST
  27474. /* test import of public */
  27475. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], NULL, ECC_SECP256R1);
  27476. if (ret != 0) {
  27477. ret = WC_TEST_RET_ENC_EC(ret);
  27478. goto done;
  27479. }
  27480. #endif
  27481. wc_ecc_free(keyImp);
  27482. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  27483. /* test export private and public raw */
  27484. pubLenX = pubLenY = privLen = 32;
  27485. ret = wc_ecc_export_private_raw(key, pub, &pubLenX, &pub[32], &pubLenY,
  27486. priv, &privLen);
  27487. if (ret != 0) {
  27488. ret = WC_TEST_RET_ENC_EC(ret);
  27489. goto done;
  27490. }
  27491. #ifndef HAVE_SELFTEST
  27492. /* test import of private and public */
  27493. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], priv, ECC_SECP256R1);
  27494. if (ret != 0) {
  27495. ret = WC_TEST_RET_ENC_EC(ret);
  27496. goto done;
  27497. }
  27498. #endif
  27499. done:
  27500. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27501. if (keyImp != NULL) {
  27502. wc_ecc_free(keyImp);
  27503. XFREE(keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27504. }
  27505. #else
  27506. wc_ecc_free(keyImp);
  27507. #endif
  27508. return ret;
  27509. }
  27510. #endif /* HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  27511. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  27512. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  27513. static wc_test_ret_t ecc_mulmod_test(ecc_key* key1)
  27514. {
  27515. wc_test_ret_t ret;
  27516. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27517. ecc_key *key2 = (ecc_key *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27518. ecc_key *key3 = (ecc_key *)XMALLOC(sizeof *key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27519. #else
  27520. ecc_key key2[1];
  27521. ecc_key key3[1];
  27522. #endif
  27523. #ifdef WOLFSSL_PUBLIC_MP
  27524. mp_int* priv;
  27525. #endif
  27526. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27527. if ((key2 == NULL) || (key3 == NULL))
  27528. ERROR_OUT(MEMORY_E, done);
  27529. #endif
  27530. wc_ecc_init_ex(key2, HEAP_HINT, devId);
  27531. wc_ecc_init_ex(key3, HEAP_HINT, devId);
  27532. /* TODO: Use test data, test with WOLFSSL_VALIDATE_ECC_IMPORT. */
  27533. /* Need base point (Gx,Gy) and parameter A - load them as the public and
  27534. * private key in key2.
  27535. */
  27536. ret = wc_ecc_import_raw_ex(key2, key1->dp->Gx, key1->dp->Gy, key1->dp->Af,
  27537. ECC_SECP256R1);
  27538. if (ret != 0)
  27539. goto done;
  27540. /* Need a point (Gx,Gy) and prime - load them as the public and private key
  27541. * in key3.
  27542. */
  27543. ret = wc_ecc_import_raw_ex(key3, key1->dp->Gx, key1->dp->Gy,
  27544. key1->dp->prime, ECC_SECP256R1);
  27545. if (ret != 0)
  27546. goto done;
  27547. ret = wc_ecc_mulmod(wc_ecc_key_get_priv(key1), &key2->pubkey, &key3->pubkey,
  27548. wc_ecc_key_get_priv(key2), wc_ecc_key_get_priv(key3),
  27549. 1);
  27550. if (ret != 0) {
  27551. ret = WC_TEST_RET_ENC_EC(ret);
  27552. goto done;
  27553. }
  27554. #ifdef WOLFSSL_PUBLIC_MP
  27555. priv = wc_ecc_key_get_priv(key1);
  27556. mp_zero(priv);
  27557. ret = wc_ecc_mulmod(wc_ecc_key_get_priv(key1), &key2->pubkey, &key3->pubkey,
  27558. wc_ecc_key_get_priv(key2), wc_ecc_key_get_priv(key3),
  27559. 1);
  27560. if (ret != 0) {
  27561. ret = WC_TEST_RET_ENC_EC(ret);
  27562. goto done;
  27563. }
  27564. if (!wc_ecc_point_is_at_infinity(&key3->pubkey)) {
  27565. ret = WC_TEST_RET_ENC_EC(ret);
  27566. goto done;
  27567. }
  27568. if (mp_cmp_d(key3->pubkey.z, 1) != MP_EQ) {
  27569. ret = WC_TEST_RET_ENC_EC(ret);
  27570. goto done;
  27571. }
  27572. #endif
  27573. done:
  27574. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27575. if (key2 != NULL) {
  27576. wc_ecc_free(key2);
  27577. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27578. }
  27579. if (key3 != NULL) {
  27580. wc_ecc_free(key3);
  27581. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27582. }
  27583. #else
  27584. wc_ecc_free(key3);
  27585. wc_ecc_free(key2);
  27586. #endif
  27587. return ret;
  27588. }
  27589. #endif
  27590. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  27591. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(WOLFSSL_ATECC508A) && \
  27592. !defined(WOLFSSL_ATECC608A) && !defined(PLUTON_CRYPTO_ECC) && \
  27593. !defined(WOLFSSL_CRYPTOCELL)
  27594. static wc_test_ret_t ecc_ssh_test(ecc_key* key, WC_RNG* rng)
  27595. {
  27596. wc_test_ret_t ret;
  27597. byte out[128];
  27598. word32 outLen = sizeof(out);
  27599. /* Parameter Validation testing. */
  27600. ret = wc_ecc_shared_secret_ssh(NULL, &key->pubkey, out, &outLen);
  27601. if (ret != BAD_FUNC_ARG)
  27602. return WC_TEST_RET_ENC_EC(ret);
  27603. ret = wc_ecc_shared_secret_ssh(key, NULL, out, &outLen);
  27604. if (ret != BAD_FUNC_ARG)
  27605. return WC_TEST_RET_ENC_EC(ret);
  27606. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, NULL, &outLen);
  27607. if (ret != BAD_FUNC_ARG)
  27608. return WC_TEST_RET_ENC_EC(ret);
  27609. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, NULL);
  27610. if (ret != BAD_FUNC_ARG)
  27611. return WC_TEST_RET_ENC_EC(ret);
  27612. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  27613. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  27614. !defined(HAVE_SELFTEST)
  27615. ret = wc_ecc_set_rng(key, rng);
  27616. if (ret != 0)
  27617. return WC_TEST_RET_ENC_EC(ret);
  27618. #else
  27619. (void)rng;
  27620. #endif
  27621. /* Use API. */
  27622. ret = 0;
  27623. do {
  27624. #if defined(WOLFSSL_ASYNC_CRYPT)
  27625. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  27626. #endif
  27627. if (ret == 0)
  27628. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, &outLen);
  27629. } while (ret == WC_PENDING_E);
  27630. if (ret != 0)
  27631. return WC_TEST_RET_ENC_EC(ret);
  27632. TEST_SLEEP();
  27633. return 0;
  27634. }
  27635. #endif /* HAVE_ECC_DHE && !WC_NO_RNG */
  27636. static wc_test_ret_t ecc_def_curve_test(WC_RNG *rng)
  27637. {
  27638. wc_test_ret_t ret;
  27639. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27640. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27641. #else
  27642. ecc_key key[1];
  27643. #endif
  27644. #if !defined(NO_ECC_SECP) && \
  27645. ((defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  27646. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)))
  27647. word32 idx = 0;
  27648. #endif
  27649. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27650. if (key == NULL)
  27651. ERROR_OUT(MEMORY_E, done);
  27652. #endif
  27653. wc_ecc_init_ex(key, HEAP_HINT, devId);
  27654. /* Use API */
  27655. ret = wc_ecc_set_flags(NULL, 0);
  27656. if (ret != BAD_FUNC_ARG) {
  27657. ret = WC_TEST_RET_ENC_EC(ret);
  27658. goto done;
  27659. }
  27660. ret = wc_ecc_set_flags(key, 0);
  27661. if (ret != 0) {
  27662. ret = WC_TEST_RET_ENC_EC(ret);
  27663. goto done;
  27664. }
  27665. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  27666. #ifndef WC_NO_RNG
  27667. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  27668. #if defined(WOLFSSL_ASYNC_CRYPT)
  27669. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  27670. #endif
  27671. if (ret != 0) {
  27672. goto done;
  27673. }
  27674. #ifndef NO_SIG_WRAPPER
  27675. ret = ecc_sig_test(rng, key);
  27676. if (ret < 0)
  27677. goto done;
  27678. #endif
  27679. TEST_SLEEP();
  27680. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_CRYPTOCELL) && \
  27681. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  27682. ret = ecc_ssh_test(key, rng);
  27683. if (ret < 0)
  27684. goto done;
  27685. #endif
  27686. wc_ecc_free(key);
  27687. #else
  27688. (void)rng;
  27689. #endif /* !WC_NO_RNG */
  27690. #if !defined(NO_ECC_SECP) && \
  27691. ((defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  27692. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)))
  27693. /* Use test ECC key - ensure real private "d" exists */
  27694. #if defined(USE_CERT_BUFFERS_256)
  27695. {
  27696. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  27697. sizeof_ecc_key_der_256);
  27698. }
  27699. #elif !defined(NO_FILESYSTEM)
  27700. {
  27701. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  27702. byte der[128];
  27703. word32 derSz;
  27704. if (!file) {
  27705. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  27706. }
  27707. derSz = (word32)XFREAD(der, 1, sizeof(der), file);
  27708. XFCLOSE(file);
  27709. if (derSz == 0)
  27710. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  27711. ret = wc_EccPrivateKeyDecode(der, &idx, key, derSz);
  27712. }
  27713. #else
  27714. {
  27715. (void)idx;
  27716. WOLFSSL_MSG("No file system and USE_CERT_BUFFERS_256 not defined.(3)");
  27717. ERROR_OUT(ASN_PARSE_E, done);
  27718. }
  27719. #endif
  27720. if (ret != 0) {
  27721. goto done;
  27722. }
  27723. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)
  27724. ret = ecc_exp_imp_test(key);
  27725. if (ret < 0)
  27726. goto done;
  27727. #endif
  27728. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  27729. !defined(WOLFSSL_CRYPTOCELL)
  27730. ret = ecc_mulmod_test(key);
  27731. if (ret < 0)
  27732. goto done;
  27733. #endif
  27734. #endif
  27735. #else
  27736. (void)rng;
  27737. (void)idx;
  27738. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  27739. done:
  27740. wc_ecc_free(key);
  27741. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27742. if (key != NULL) {
  27743. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27744. }
  27745. #endif
  27746. return ret;
  27747. }
  27748. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  27749. #if defined(WOLFSSL_CERT_EXT) && \
  27750. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  27751. static wc_test_ret_t ecc_decode_test(void)
  27752. {
  27753. wc_test_ret_t ret;
  27754. word32 inSz;
  27755. word32 inOutIdx;
  27756. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27757. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27758. #else
  27759. ecc_key key[1];
  27760. #endif
  27761. /* SECP256R1 OID: 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 */
  27762. /* This is ecc_clikeypub_der_256. */
  27763. WOLFSSL_SMALL_STACK_STATIC const byte good[] = {
  27764. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
  27765. 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  27766. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xbf, 0xf4,
  27767. 0x0f, 0x44, 0x50, 0x9a, 0x3d, 0xce, 0x9b, 0xb7, 0xf0, 0xc5,
  27768. 0x4d, 0xf5, 0x70, 0x7b, 0xd4, 0xec, 0x24, 0x8e, 0x19, 0x80,
  27769. 0xec, 0x5a, 0x4c, 0xa2, 0x24, 0x03, 0x62, 0x2c, 0x9b, 0xda,
  27770. 0xef, 0xa2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xc6, 0x56,
  27771. 0x95, 0x06, 0xcc, 0x01, 0xa9, 0xbd, 0xf6, 0x75, 0x1a, 0x42,
  27772. 0xf7, 0xbd, 0xa9, 0xb2, 0x36, 0x22, 0x5f, 0xc7, 0x5d, 0x7f,
  27773. 0xb4 };
  27774. WOLFSSL_SMALL_STACK_STATIC const byte badNoObjId[] = { 0x30, 0x08, 0x30, 0x06, 0x03, 0x04,
  27775. 0x00, 0x04, 0x01, 0x01 };
  27776. WOLFSSL_SMALL_STACK_STATIC const byte badOneObjId[] = { 0x30, 0x0a, 0x30, 0x08, 0x06, 0x00,
  27777. 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  27778. WOLFSSL_SMALL_STACK_STATIC const byte badObjId1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x09,
  27779. 0x06, 0x00, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  27780. WOLFSSL_SMALL_STACK_STATIC const byte badObj2d1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00,
  27781. 0x06, 0x07, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  27782. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitStr[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  27783. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  27784. 0x04, 0x04, 0x00, 0x04, 0x01, 0x01 };
  27785. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrLen[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  27786. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  27787. 0x03, 0x05, 0x00, 0x04, 0x01, 0x01 };
  27788. WOLFSSL_SMALL_STACK_STATIC const byte badNoBitStrZero[] = { 0x30, 0x13, 0x30, 0x0a, 0x06, 0x00,
  27789. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  27790. 0x03, 0x03, 0x04, 0x01, 0x01 };
  27791. WOLFSSL_SMALL_STACK_STATIC const byte badPoint[] = { 0x30, 0x12, 0x30, 0x09, 0x06, 0x00,
  27792. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  27793. 0x03, 0x03, 0x00, 0x04, 0x01 };
  27794. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27795. if (key == NULL)
  27796. ERROR_OUT(MEMORY_E, done);
  27797. #endif
  27798. XMEMSET(key, 0, sizeof *key);
  27799. wc_ecc_init_ex(key, HEAP_HINT, devId);
  27800. inSz = sizeof(good);
  27801. ret = wc_EccPublicKeyDecode(NULL, &inOutIdx, key, inSz);
  27802. if (ret != BAD_FUNC_ARG) {
  27803. ret = WC_TEST_RET_ENC_EC(ret);
  27804. goto done;
  27805. }
  27806. ret = wc_EccPublicKeyDecode(good, NULL, key, inSz);
  27807. if (ret != BAD_FUNC_ARG) {
  27808. ret = WC_TEST_RET_ENC_EC(ret);
  27809. goto done;
  27810. }
  27811. ret = wc_EccPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  27812. if (ret != BAD_FUNC_ARG) {
  27813. ret = WC_TEST_RET_ENC_EC(ret);
  27814. goto done;
  27815. }
  27816. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, 0);
  27817. if (ret != BAD_FUNC_ARG) {
  27818. ret = WC_TEST_RET_ENC_EC(ret);
  27819. goto done;
  27820. }
  27821. /* Change offset to produce bad input data. */
  27822. inOutIdx = 2;
  27823. inSz = sizeof(good) - inOutIdx;
  27824. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  27825. if (ret != ASN_PARSE_E) {
  27826. ret = WC_TEST_RET_ENC_EC(ret);
  27827. goto done;
  27828. }
  27829. inOutIdx = 4;
  27830. inSz = sizeof(good) - inOutIdx;
  27831. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  27832. if (ret != ASN_PARSE_E) {
  27833. ret = WC_TEST_RET_ENC_EC(ret);
  27834. goto done;
  27835. }
  27836. /* Bad data. */
  27837. inSz = sizeof(badNoObjId);
  27838. inOutIdx = 0;
  27839. ret = wc_EccPublicKeyDecode(badNoObjId, &inOutIdx, key, inSz);
  27840. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  27841. ret = WC_TEST_RET_ENC_EC(ret);
  27842. goto done;
  27843. }
  27844. inSz = sizeof(badOneObjId);
  27845. inOutIdx = 0;
  27846. ret = wc_EccPublicKeyDecode(badOneObjId, &inOutIdx, key, inSz);
  27847. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  27848. ret = WC_TEST_RET_ENC_EC(ret);
  27849. goto done;
  27850. }
  27851. inSz = sizeof(badObjId1Len);
  27852. inOutIdx = 0;
  27853. ret = wc_EccPublicKeyDecode(badObjId1Len, &inOutIdx, key, inSz);
  27854. if (ret != ASN_PARSE_E) {
  27855. ret = WC_TEST_RET_ENC_EC(ret);
  27856. goto done;
  27857. }
  27858. inSz = sizeof(badObj2d1Len);
  27859. inOutIdx = 0;
  27860. ret = wc_EccPublicKeyDecode(badObj2d1Len, &inOutIdx, key, inSz);
  27861. if (ret != ASN_PARSE_E) {
  27862. ret = WC_TEST_RET_ENC_EC(ret);
  27863. goto done;
  27864. }
  27865. inSz = sizeof(badNotBitStr);
  27866. inOutIdx = 0;
  27867. ret = wc_EccPublicKeyDecode(badNotBitStr, &inOutIdx, key, inSz);
  27868. if (ret != ASN_BITSTR_E && ret != ASN_PARSE_E) {
  27869. ret = WC_TEST_RET_ENC_EC(ret);
  27870. goto done;
  27871. }
  27872. inSz = sizeof(badBitStrLen);
  27873. inOutIdx = 0;
  27874. ret = wc_EccPublicKeyDecode(badBitStrLen, &inOutIdx, key, inSz);
  27875. if (ret != ASN_PARSE_E) {
  27876. ret = WC_TEST_RET_ENC_EC(ret);
  27877. goto done;
  27878. }
  27879. inSz = sizeof(badNoBitStrZero);
  27880. inOutIdx = 0;
  27881. ret = wc_EccPublicKeyDecode(badNoBitStrZero, &inOutIdx, key, inSz);
  27882. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  27883. ret = WC_TEST_RET_ENC_EC(ret);
  27884. goto done;
  27885. }
  27886. inSz = sizeof(badPoint);
  27887. inOutIdx = 0;
  27888. ret = wc_EccPublicKeyDecode(badPoint, &inOutIdx, key, inSz);
  27889. if (ret != ASN_ECC_KEY_E && ret != ASN_PARSE_E) {
  27890. ret = WC_TEST_RET_ENC_EC(ret);
  27891. goto done;
  27892. }
  27893. inSz = sizeof(good);
  27894. inOutIdx = 0;
  27895. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  27896. if (ret != 0) {
  27897. ret = WC_TEST_RET_ENC_EC(ret);
  27898. goto done;
  27899. }
  27900. done:
  27901. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27902. if (key != NULL) {
  27903. wc_ecc_free(key);
  27904. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27905. }
  27906. #else
  27907. wc_ecc_free(key);
  27908. #endif
  27909. return ret;
  27910. }
  27911. #endif /* WOLFSSL_CERT_EXT */
  27912. #ifdef WOLFSSL_CUSTOM_CURVES
  27913. static const byte eccKeyExplicitCurve[] = {
  27914. 0x30, 0x81, 0xf5, 0x30, 0x81, 0xae, 0x06, 0x07,
  27915. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30,
  27916. 0x81, 0xa2, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06,
  27917. 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01,
  27918. 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  27919. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  27920. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  27921. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
  27922. 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00,
  27923. 0x04, 0x01, 0x07, 0x04, 0x41, 0x04, 0x79, 0xbe,
  27924. 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
  27925. 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b,
  27926. 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2,
  27927. 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a,
  27928. 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4,
  27929. 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17,
  27930. 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47,
  27931. 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8, 0x02, 0x21,
  27932. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  27933. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  27934. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  27935. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  27936. 0x41, 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04,
  27937. 0x3c, 0x4c, 0xc9, 0x5e, 0x2e, 0xa2, 0x3d, 0x49,
  27938. 0xcc, 0x5b, 0xff, 0x4f, 0xc9, 0x2e, 0x1d, 0x4a,
  27939. 0xc6, 0x21, 0xf6, 0xf3, 0xe6, 0x0b, 0x4f, 0xa9,
  27940. 0x9d, 0x74, 0x99, 0xdd, 0x97, 0xc7, 0x6e, 0xbe,
  27941. 0x14, 0x2b, 0x39, 0x9d, 0x63, 0xc7, 0x97, 0x0d,
  27942. 0x45, 0x25, 0x40, 0x30, 0x77, 0x05, 0x76, 0x88,
  27943. 0x38, 0x96, 0x29, 0x7d, 0x9c, 0xe1, 0x50, 0xbe,
  27944. 0xac, 0xf0, 0x1d, 0x86, 0xf4, 0x2f, 0x65, 0x0b
  27945. };
  27946. static wc_test_ret_t ecc_test_custom_curves(WC_RNG* rng)
  27947. {
  27948. wc_test_ret_t ret;
  27949. word32 inOutIdx;
  27950. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27951. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27952. #else
  27953. ecc_key key[1];
  27954. #endif
  27955. /* test use of custom curve - using BRAINPOOLP256R1 for test */
  27956. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  27957. #ifndef WOLFSSL_ECC_CURVE_STATIC
  27958. WOLFSSL_SMALL_STACK_STATIC const ecc_oid_t ecc_oid_brainpoolp256r1[] = {
  27959. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07
  27960. };
  27961. #define ecc_oid_brainpoolp256r1_sz \
  27962. (sizeof(ecc_oid_brainpoolp256r1) / sizeof(ecc_oid_t))
  27963. #else
  27964. #define ecc_oid_brainpoolp256r1 { \
  27965. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07 \
  27966. }
  27967. #define ecc_oid_brainpoolp256r1_sz 9
  27968. #endif
  27969. #define ecc_oid_brainpoolp256r1_sum 104
  27970. WOLFSSL_SMALL_STACK_STATIC const ecc_set_type ecc_dp_brainpool256r1 = {
  27971. 32, /* size/bytes */
  27972. ECC_CURVE_CUSTOM, /* ID */
  27973. "BRAINPOOLP256R1", /* curve name */
  27974. "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", /* prime */
  27975. "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", /* A */
  27976. "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", /* B */
  27977. "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", /* order */
  27978. "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", /* Gx */
  27979. "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", /* Gy */
  27980. ecc_oid_brainpoolp256r1, /* oid/oidSz */
  27981. ecc_oid_brainpoolp256r1_sz,
  27982. ecc_oid_brainpoolp256r1_sum, /* oid sum */
  27983. 1, /* cofactor */
  27984. };
  27985. #endif /* HAVE_ECC_BRAINPOOL */
  27986. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27987. if (! key) {
  27988. ret = MEMORY_E;
  27989. goto done;
  27990. }
  27991. #endif
  27992. XMEMSET(key, 0, sizeof *key);
  27993. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  27994. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, ECC_CURVE_DEF,
  27995. &ecc_dp_brainpool256r1);
  27996. if (ret != 0) {
  27997. printf("ECC test for custom curve failed!\n");
  27998. goto done;
  27999. }
  28000. #endif
  28001. #if defined(HAVE_ECC_BRAINPOOL) || defined(HAVE_ECC_KOBLITZ)
  28002. {
  28003. int curve_id;
  28004. #ifdef HAVE_ECC_BRAINPOOL
  28005. curve_id = ECC_BRAINPOOLP256R1;
  28006. #else
  28007. curve_id = ECC_SECP256K1;
  28008. #endif
  28009. /* Test and demonstrate use of non-SECP curve */
  28010. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, curve_id, NULL);
  28011. if (ret < 0) {
  28012. printf("ECC test for curve_id %d failed!\n", curve_id);
  28013. goto done;
  28014. }
  28015. }
  28016. #endif
  28017. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  28018. if (ret != 0) {
  28019. ret = WC_TEST_RET_ENC_EC(ret);
  28020. goto done;
  28021. }
  28022. inOutIdx = 0;
  28023. ret = wc_EccPublicKeyDecode(eccKeyExplicitCurve, &inOutIdx, key,
  28024. sizeof(eccKeyExplicitCurve));
  28025. if (ret != 0)
  28026. ret = WC_TEST_RET_ENC_EC(ret);
  28027. done:
  28028. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28029. if (key) {
  28030. wc_ecc_free(key);
  28031. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28032. }
  28033. #else
  28034. wc_ecc_free(key);
  28035. #endif
  28036. (void)rng;
  28037. return ret;
  28038. }
  28039. #endif /* WOLFSSL_CUSTOM_CURVES */
  28040. #ifdef WOLFSSL_SM2
  28041. #ifdef HAVE_ECC_VERIFY
  28042. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_CUSTOM_CURVES)
  28043. #ifdef HAVE_OID_ENCODING
  28044. #define CODED_SM2P256V1 {1,2,156,10197,1,301}
  28045. #define CODED_SM2P256V1_SZ 6
  28046. #else
  28047. #define CODED_SM2P256V1 {0x06,0x08,0x2A,0x81,0x1C,0xCF,0x55,0x01,0x82,0x2D}
  28048. #define CODED_SM2P256V1_SZ 10
  28049. #endif
  28050. #ifndef WOLFSSL_ECC_CURVE_STATIC
  28051. static const ecc_oid_t ecc_oid_sm2p256v1[] = CODED_SM2P256V1;
  28052. #else
  28053. #define ecc_oid_sm2p256v1 CODED_SM2P256V1
  28054. #endif
  28055. #define ecc_oid_sm2p256v1_sz CODED_SM2P256V1_SZ
  28056. #define ECC_SM2P256V1_TEST 102
  28057. static int test_sm2_verify_caseA2(void)
  28058. {
  28059. ecc_key key;
  28060. int ret, res;
  28061. mp_int r,s;
  28062. /* test key values */
  28063. const char qx[] = "0AE4C7798AA0F119471BEE11825BE46202BB79E2A5844495E97C04FF4DF2548A";
  28064. const char qy[] = "7C0240F88F1CD4E16352A73C17B7F16F07353E53A176D684A9FE0C6BB798E857";
  28065. const char d[] = "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263";
  28066. const ecc_set_type ecc_sm2_A2 = {
  28067. 32, /* size/bytes */
  28068. ECC_SM2P256V1_TEST, /* ID */
  28069. "SM2P256V1_TEST", /* curve name */
  28070. /* from test case A.2 in draft-shen-sm2-ecdsa-02 */
  28071. "8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3", /* prime */
  28072. "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498", /* A */
  28073. "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A", /* B */
  28074. "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7", /* order n */
  28075. "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D", /* Gx */
  28076. "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2", /* Gy */
  28077. ecc_oid_sm2p256v1, /* oid/oidSz */
  28078. ecc_oid_sm2p256v1_sz,
  28079. ECC_SM2P256V1_OID, /* oid sum */
  28080. 1, /* cofactor */
  28081. };
  28082. /* use canned hash value hash = H(ZA||M) */
  28083. const byte hash[] = {
  28084. 0xB5,0x24,0xF5,0x52,0xCD,0x82,0xB8,0xB0,
  28085. 0x28,0x47,0x6E,0x00,0x5C,0x37,0x7F,0xB1,
  28086. 0x9A,0x87,0xE6,0xFC,0x68,0x2D,0x48,0xBB,
  28087. 0x5D,0x42,0xE3,0xD9,0xB9,0xEF,0xFE,0x76
  28088. };
  28089. /* canned r and s */
  28090. const byte rCan[] = {
  28091. 0x40,0xF1,0xEC,0x59,0xF7,0x93,0xD9,0xF4,
  28092. 0x9E,0x09,0xDC,0xEF,0x49,0x13,0x0D,0x41,
  28093. 0x94,0xF7,0x9F,0xB1,0xEE,0xD2,0xCA,0xA5,
  28094. 0x5B,0xAC,0xDB,0x49,0xC4,0xE7,0x55,0xD1
  28095. };
  28096. const byte sCan[] = {
  28097. 0x6F,0xC6,0xDA,0xC3,0x2C,0x5D,0x5C,0xF1,
  28098. 0x0C,0x77,0xDF,0xB2,0x0F,0x7C,0x2E,0xB6,
  28099. 0x67,0xA4,0x57,0x87,0x2F,0xB0,0x9E,0xC5,
  28100. 0x63,0x27,0xA6,0x7E,0xC7,0xDE,0xEB,0xE7
  28101. };
  28102. mp_init(&r);
  28103. mp_init(&s);
  28104. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  28105. if (ret != 0)
  28106. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28107. ret = wc_ecc_set_custom_curve(&key, &ecc_sm2_A2);
  28108. if (ret != 0)
  28109. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28110. ret = wc_ecc_import_raw_ex(&key, qx, qy, d, ECC_SM2P256V1_TEST);
  28111. if (ret != 0)
  28112. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28113. mp_read_unsigned_bin(&r, rCan, sizeof(rCan));
  28114. mp_read_unsigned_bin(&s, sCan, sizeof(sCan));
  28115. ret = wc_ecc_sm2_verify_hash_ex(&r, &s, hash, sizeof(hash), &res, &key);
  28116. if (ret != 0)
  28117. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28118. if (res != 1)
  28119. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28120. done:
  28121. mp_free(&r);
  28122. mp_free(&s);
  28123. wc_ecc_free(&key);
  28124. return ret;
  28125. }
  28126. #endif /* WOLFSSL_PUBLIC_MP && WOLFSSL_CUSTOM_CURVES */
  28127. static int test_sm2_verify_case(void)
  28128. {
  28129. ecc_key key;
  28130. int ret, res;
  28131. /* test key values */
  28132. const char qx[] = "637F1B135036C933DC3F7A8EBB1B7B2FD1DFBD268D4F894B5AD47DBDBECD558F";
  28133. const char qy[] = "E88101D08048E36CCBF61CA38DDF7ABA542B4486E99E49F3A7470A857A096433";
  28134. /* use canned hash value hash = H(ZA||M) */
  28135. const byte hash[] = {
  28136. 0x3B,0xFA,0x5F,0xFB,0xC4,0x27,0x8C,0x9D,
  28137. 0x02,0x3A,0x19,0xCB,0x1E,0xAA,0xD2,0xF1,
  28138. 0x50,0x69,0x5B,0x20
  28139. };
  28140. const byte sig[] = {
  28141. 0x30,0x45,0x02,0x21,0x00,0xD2,0xFC,0xA3,
  28142. 0x88,0xE3,0xDF,0xA3,0x00,0x73,0x9B,0x3C,
  28143. 0x2A,0x0D,0xAD,0x44,0xA2,0xFC,0x62,0xD5,
  28144. 0x6B,0x84,0x54,0xD8,0x40,0x22,0x62,0x3D,
  28145. 0x5C,0xA6,0x61,0x9B,0xE7,0x02,0x20,0x1D,
  28146. 0xB5,0xB5,0xD9,0xD8,0xF1,0x20,0xDD,0x97,
  28147. 0x92,0xBF,0x7E,0x9B,0x3F,0xE6,0x3C,0x4B,
  28148. 0x03,0xD8,0x80,0xBD,0xB7,0x27,0x7E,0x6A,
  28149. 0x84,0x23,0xDE,0x61,0x7C,0x8D,0xDC
  28150. };
  28151. const byte badSig[] = {
  28152. 0x30,0x45,0x02,0x21,0x00,0xD2,0xFC,0xA3,
  28153. 0x88,0xE3,0xDF,0xA3,0x00,0x73,0x9B,0x3C,
  28154. 0x2A,0x0D,0xAD,0x44,0xA2,0xFC,0x62,0xD5,
  28155. 0x6B,0x84,0x54,0xD8,0x40,0x22,0x62,0x3D,
  28156. 0x5C,0xA6,0x61,0x9B,0xE7,0x02,0x20,0x1D,
  28157. 0xB5,0xB5,0xE9,0xD8,0xF1,0x20,0xDD,0x97,
  28158. 0x92,0xBF,0x7E,0x9B,0x3F,0xE6,0x3C,0x4B,
  28159. 0x03,0xD8,0x80,0xBD,0xB7,0x27,0x7E,0x6A,
  28160. 0x84,0x23,0xDE,0x61,0x7C,0x8D,0xDC
  28161. };
  28162. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  28163. if (ret != 0)
  28164. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28165. ret = wc_ecc_import_raw(&key, qx, qy, NULL, "SM2P256V1");
  28166. if (ret != 0)
  28167. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28168. ret = wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash), &res,
  28169. &key);
  28170. if (ret != 0)
  28171. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28172. if (res != 1)
  28173. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28174. /* now test a case that should fail */
  28175. ret = wc_ecc_sm2_verify_hash(badSig, sizeof(badSig), hash, sizeof(hash),
  28176. &res, &key);
  28177. if (ret != 0)
  28178. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28179. if (res == 1)
  28180. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28181. done:
  28182. wc_ecc_free(&key);
  28183. return ret;
  28184. }
  28185. static int ecc_sm2_test_curve(WC_RNG* rng, int testVerifyCount)
  28186. {
  28187. const ecc_set_type* dp = wc_ecc_get_curve_params(
  28188. wc_ecc_get_curve_idx(ECC_SM2P256V1));
  28189. int keySize = 32;
  28190. int curve_id = ECC_SM2P256V1;
  28191. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  28192. WC_DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  28193. WC_DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  28194. #endif
  28195. #ifdef HAVE_ECC_KEY_EXPORT
  28196. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  28197. WC_DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  28198. #endif
  28199. word32 x = 0;
  28200. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  28201. word32 y;
  28202. #endif
  28203. #ifdef HAVE_ECC_SIGN
  28204. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  28205. WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  28206. int i;
  28207. int verify;
  28208. #endif /* HAVE_ECC_SIGN */
  28209. int ret;
  28210. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28211. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28212. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28213. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28214. #else
  28215. ecc_key userA[1];
  28216. ecc_key userB[1];
  28217. ecc_key pubKey[1];
  28218. #endif
  28219. #ifndef WC_NO_RNG
  28220. int curveSize;
  28221. #endif
  28222. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  28223. WC_ALLOC_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  28224. WC_ALLOC_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  28225. #endif
  28226. #ifdef HAVE_ECC_KEY_EXPORT
  28227. WC_ALLOC_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  28228. #endif
  28229. #ifdef HAVE_ECC_SIGN
  28230. WC_ALLOC_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  28231. WC_ALLOC_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  28232. #endif
  28233. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  28234. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  28235. if (sharedA == NULL || sharedB == NULL)
  28236. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28237. #endif
  28238. #ifdef HAVE_ECC_KEY_EXPORT
  28239. if (exportBuf == NULL)
  28240. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28241. #endif
  28242. #ifdef HAVE_ECC_SIGN
  28243. if (sig == NULL || digest == NULL)
  28244. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28245. #endif
  28246. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  28247. (void)testVerifyCount;
  28248. (void)dp;
  28249. (void)x;
  28250. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28251. if ((userA == NULL) ||
  28252. (userB == NULL) ||
  28253. (pubKey == NULL))
  28254. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28255. #endif
  28256. XMEMSET(userA, 0, sizeof *userA);
  28257. XMEMSET(userB, 0, sizeof *userB);
  28258. XMEMSET(pubKey, 0, sizeof *pubKey);
  28259. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  28260. if (ret != 0)
  28261. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28262. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  28263. if (ret != 0)
  28264. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28265. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  28266. if (ret != 0)
  28267. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28268. #ifndef WC_NO_RNG
  28269. ret = wc_ecc_sm2_make_key(rng, userA, WC_ECC_FLAG_NONE);
  28270. if (ret == ECC_CURVE_OID_E)
  28271. goto done; /* catch case, where curve is not supported */
  28272. if (ret != 0)
  28273. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28274. TEST_SLEEP();
  28275. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  28276. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  28277. if (curveSize != userA->dp->size) {
  28278. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28279. }
  28280. }
  28281. ret = wc_ecc_check_key(userA);
  28282. if (ret != 0)
  28283. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28284. TEST_SLEEP();
  28285. ret = wc_ecc_sm2_make_key(rng, userB, WC_ECC_FLAG_NONE);
  28286. if (ret != 0)
  28287. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28288. /* only perform the below tests if the key size matches */
  28289. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  28290. if (ret != 0) {
  28291. ret = ECC_CURVE_OID_E;
  28292. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28293. }
  28294. #ifdef HAVE_ECC_DHE
  28295. #if defined(ECC_TIMING_RESISTANT)
  28296. ret = wc_ecc_set_rng(userA, rng);
  28297. if (ret != 0)
  28298. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28299. ret = wc_ecc_set_rng(userB, rng);
  28300. if (ret != 0)
  28301. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28302. #endif
  28303. x = ECC_SHARED_SIZE;
  28304. ret = wc_ecc_sm2_shared_secret(userA, userB, sharedA, &x);
  28305. if (ret != 0)
  28306. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28307. y = ECC_SHARED_SIZE;
  28308. ret = wc_ecc_sm2_shared_secret(userB, userA, sharedB, &y);
  28309. if (ret != 0)
  28310. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28311. if (y != x)
  28312. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28313. if (XMEMCMP(sharedA, sharedB, x))
  28314. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28315. #endif /* HAVE_ECC_DHE */
  28316. #ifdef HAVE_ECC_KEY_EXPORT
  28317. x = ECC_KEY_EXPORT_BUF_SIZE;
  28318. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  28319. if (ret != 0)
  28320. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28321. #ifdef HAVE_ECC_KEY_IMPORT
  28322. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  28323. if (ret != 0)
  28324. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28325. #ifdef HAVE_ECC_DHE
  28326. y = ECC_SHARED_SIZE;
  28327. ret = wc_ecc_sm2_shared_secret(userB, pubKey, sharedB, &y);
  28328. if (ret != 0)
  28329. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28330. if (XMEMCMP(sharedA, sharedB, y))
  28331. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28332. #endif /* HAVE_ECC_DHE */
  28333. #ifdef HAVE_COMP_KEY
  28334. /* try compressed export / import too */
  28335. x = ECC_KEY_EXPORT_BUF_SIZE;
  28336. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  28337. if (ret != 0)
  28338. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28339. wc_ecc_free(pubKey);
  28340. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  28341. if (ret != 0)
  28342. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28343. #endif
  28344. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  28345. if (ret != 0)
  28346. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28347. #ifdef HAVE_ECC_DHE
  28348. y = ECC_SHARED_SIZE;
  28349. ret = wc_ecc_sm2_shared_secret(userB, pubKey, sharedB, &y);
  28350. if (ret != 0)
  28351. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28352. if (XMEMCMP(sharedA, sharedB, y))
  28353. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28354. #endif /* HAVE_ECC_DHE */
  28355. #endif /* HAVE_ECC_KEY_IMPORT */
  28356. #endif /* HAVE_ECC_KEY_EXPORT */
  28357. #endif /* !WC_NO_RNG */
  28358. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  28359. !defined(WC_NO_RNG))
  28360. #ifdef HAVE_ECC_SIGN
  28361. /* ECC w/out Shamir has issue with all 0 digest */
  28362. /* WC_BIGINT doesn't have 0 len well on hardware */
  28363. /* Cryptocell has issues with all 0 digest */
  28364. #if defined(ECC_SHAMIR)
  28365. /* test DSA sign hash with zeros */
  28366. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  28367. digest[i] = 0;
  28368. }
  28369. x = ECC_SIG_SIZE;
  28370. ret = wc_ecc_sm2_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  28371. if (ret != 0)
  28372. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28373. for (i = 0; i < testVerifyCount; i++) {
  28374. verify = 0;
  28375. ret = wc_ecc_sm2_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify,
  28376. userA);
  28377. if (ret != 0)
  28378. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28379. if (verify != 1)
  28380. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28381. }
  28382. #endif /* ECC_SHAMIR */
  28383. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  28384. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  28385. digest[i] = (byte)i;
  28386. }
  28387. x = ECC_SIG_SIZE;
  28388. ret = wc_ecc_sm2_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  28389. if (ret != 0)
  28390. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28391. for (i = 0; i < testVerifyCount; i++) {
  28392. verify = 0;
  28393. ret = wc_ecc_sm2_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify,
  28394. userA);
  28395. if (ret != 0)
  28396. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28397. if (verify != 1)
  28398. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28399. }
  28400. #endif /* HAVE_ECC_SIGN */
  28401. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT && !WC_NO_RNG) */
  28402. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  28403. x = ECC_KEY_EXPORT_BUF_SIZE;
  28404. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  28405. if (ret != 0)
  28406. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28407. #elif defined(HAVE_ECC_KEY_EXPORT)
  28408. (void)exportBuf;
  28409. #endif /* HAVE_ECC_KEY_EXPORT */
  28410. done:
  28411. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28412. if (userA != NULL) {
  28413. wc_ecc_free(userA);
  28414. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28415. }
  28416. if (userB != NULL) {
  28417. wc_ecc_free(userB);
  28418. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28419. }
  28420. if (pubKey != NULL) {
  28421. wc_ecc_free(pubKey);
  28422. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28423. }
  28424. #else
  28425. wc_ecc_free(pubKey);
  28426. wc_ecc_free(userB);
  28427. wc_ecc_free(userA);
  28428. #endif
  28429. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  28430. WC_FREE_VAR(sharedA, HEAP_HINT);
  28431. WC_FREE_VAR(sharedB, HEAP_HINT);
  28432. #endif
  28433. #ifdef HAVE_ECC_KEY_EXPORT
  28434. WC_FREE_VAR(exportBuf, HEAP_HINT);
  28435. #endif
  28436. #ifdef HAVE_ECC_SIGN
  28437. WC_FREE_VAR(sig, HEAP_HINT);
  28438. WC_FREE_VAR(digest, HEAP_HINT);
  28439. #endif
  28440. (void)keySize;
  28441. (void)curve_id;
  28442. (void)rng;
  28443. return ret;
  28444. }
  28445. #endif /* HAVE_ECC_VERIFY */
  28446. static int test_sm2_create_digest(void)
  28447. {
  28448. #ifndef WOLFSSL_SM3
  28449. const byte msg[] = "message to sign";
  28450. const byte id[] = "0123456789";
  28451. const byte badId[] = "0123556789";
  28452. byte expected[] = {
  28453. 0xdd, 0x4d, 0x65, 0x49, 0xa3, 0x64, 0x76, 0xc0,
  28454. 0x73, 0x05, 0xdc, 0x05, 0x16, 0xb5, 0xee, 0x9f,
  28455. 0x82, 0xf9, 0xe9, 0x7d, 0x01, 0x1a, 0xdc, 0x88,
  28456. 0x5a, 0x59, 0x9c, 0x44, 0xcc, 0x47, 0xa4, 0x78
  28457. };
  28458. ecc_key key;
  28459. int ret;
  28460. /* test key values */
  28461. const char qx[] =
  28462. "af178b7b8740cc9d5b493fbd22049c12621bc27dcc5802e75ff4d045a4158baf";
  28463. const char qy[] =
  28464. "89933faf7a4798f48c5b9b4cd3a7693d54c9e05449946eb489c0dd50a5294805";
  28465. const char d[] =
  28466. "b3e66c2dbfb50c6ff6830c1fac4b51293a2562f9e667052b03df2d4b43c1f34a";
  28467. int hash_type = WC_HASH_TYPE_SHA256;
  28468. byte digest[WC_SHA256_DIGEST_SIZE];
  28469. #else
  28470. ecc_key key;
  28471. int ret;
  28472. const byte msg[] = { 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20,
  28473. 0x64, 0x69, 0x67, 0x65, 0x73, 0x74, 0x00 };
  28474. const byte id[] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
  28475. 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
  28476. 0x00 };
  28477. const byte badId[] = "0123556789";
  28478. const char qx[] =
  28479. "09F9DF311E5421A150DD7D161E4BC5C672179FAD1833FC076BB08FF356F35020";
  28480. const char qy[] =
  28481. "CCEA490CE26775A52DC6EA718CC1AA600AED05FBF35E084A6632F6072DA9AD13";
  28482. const char d[] =
  28483. "3945208F7B2144B13F36E38AC6D39F95889393692860B51A42FB81EF4DF7C5B8";
  28484. byte expected[] = {
  28485. 0xf0, 0xb4, 0x3e, 0x94, 0xba, 0x45, 0xac, 0xca,
  28486. 0xac, 0xe6, 0x92, 0xed, 0x53, 0x43, 0x82, 0xeb,
  28487. 0x17, 0xe6, 0xab, 0x5a, 0x19, 0xce, 0x7b, 0x31,
  28488. 0xf4, 0x48, 0x6f, 0xdf, 0xc0, 0xd2, 0x86, 0x40
  28489. };
  28490. int hash_type = WC_HASH_TYPE_SM3;
  28491. byte digest[WC_SM3_DIGEST_SIZE];
  28492. #endif
  28493. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  28494. if (ret != 0)
  28495. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28496. ret = wc_ecc_import_raw(&key, qx, qy, d, "SM2P256V1");
  28497. if (ret != 0)
  28498. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28499. ret = wc_ecc_sm2_create_digest(id, (int)XSTRLEN((const char*)id),
  28500. msg, (int)XSTRLEN((const char*)msg), hash_type, digest, sizeof(digest),
  28501. &key);
  28502. if (ret != 0)
  28503. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28504. if (XMEMCMP(digest, expected, WC_SHA256_DIGEST_SIZE) != 0)
  28505. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28506. ret = wc_ecc_sm2_create_digest(badId, (int)XSTRLEN((const char*)badId),
  28507. msg, (int)XSTRLEN((const char*)msg), hash_type, digest, sizeof(digest),
  28508. &key);
  28509. if (ret != 0)
  28510. goto done;
  28511. /* should be different than the previous ID used */
  28512. if (XMEMCMP(digest, expected, WC_SHA256_DIGEST_SIZE) == 0)
  28513. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28514. done:
  28515. wc_ecc_free(&key);
  28516. return ret;
  28517. }
  28518. static int test_sm2_verify(void)
  28519. {
  28520. int ret = 0;
  28521. #ifdef HAVE_ECC_VERIFY
  28522. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_CUSTOM_CURVES)
  28523. ret = test_sm2_verify_caseA2();
  28524. if (ret != 0)
  28525. return ret;
  28526. #endif
  28527. ret = test_sm2_verify_case();
  28528. if (ret != 0)
  28529. return ret;
  28530. #endif /* HAVE_ECC_VERIFY */
  28531. ret = test_sm2_create_digest();
  28532. return ret;
  28533. }
  28534. #endif /* WOLFSSL_SM2 */
  28535. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ECC_SECP) && !defined(NO_ASN_TIME)
  28536. /* Make Cert / Sign example for ECC cert and ECC CA */
  28537. static wc_test_ret_t ecc_test_cert_gen(WC_RNG* rng)
  28538. {
  28539. wc_test_ret_t ret;
  28540. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28541. Cert *myCert = (Cert *)XMALLOC(sizeof *myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28542. #ifdef WOLFSSL_TEST_CERT
  28543. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28544. #endif
  28545. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28546. ecc_key *certPubKey = (ecc_key *)XMALLOC(sizeof *certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28547. #else
  28548. Cert myCert[1];
  28549. #ifdef WOLFSSL_TEST_CERT
  28550. DecodedCert decode[1];
  28551. #endif
  28552. ecc_key caEccKey[1];
  28553. ecc_key certPubKey[1];
  28554. #endif
  28555. int certSz;
  28556. size_t bytes = 0;
  28557. word32 idx = 0;
  28558. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_FILESYSTEM)
  28559. XFILE file;
  28560. #endif
  28561. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28562. byte* der = NULL;
  28563. #else
  28564. byte der[FOURK_BUF];
  28565. #endif
  28566. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28567. if ((myCert == NULL)
  28568. #ifdef WOLFSSL_TEST_CERT
  28569. || (decode == NULL)
  28570. #endif
  28571. || (caEccKey == NULL) || (certPubKey == NULL))
  28572. ERROR_OUT(MEMORY_E, exit);
  28573. #endif
  28574. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  28575. XMEMSET(certPubKey, 0, sizeof *certPubKey);
  28576. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28577. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28578. if (der == NULL) {
  28579. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  28580. }
  28581. #endif
  28582. /* Get cert private key */
  28583. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  28584. /* Get Cert Key 384 */
  28585. #ifdef USE_CERT_BUFFERS_256
  28586. XMEMCPY(der, ca_ecc_key_der_384, sizeof_ca_ecc_key_der_384);
  28587. bytes = sizeof_ca_ecc_key_der_384;
  28588. #elif !defined(NO_FILESYSTEM)
  28589. file = XFOPEN(eccCaKey384File, "rb");
  28590. if (!file) {
  28591. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  28592. }
  28593. bytes = XFREAD(der, 1, FOURK_BUF, file);
  28594. XFCLOSE(file);
  28595. if (bytes == 0)
  28596. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  28597. (void)eccCaKeyFile;
  28598. #else
  28599. WOLFSSL_MSG("No file system and USE_CERT_BUFFERS_256 not defined.(4)");
  28600. ERROR_OUT(ASN_PARSE_E, exit);
  28601. #endif /* USE_CERT_BUFFERS_256 */
  28602. /* end if ENABLE_ECC384_CERT_GEN_TEST */
  28603. #else
  28604. /* !ENABLE_ECC384_CERT_GEN_TEST */
  28605. #ifdef USE_CERT_BUFFERS_256
  28606. XMEMCPY(der, ca_ecc_key_der_256, sizeof_ca_ecc_key_der_256);
  28607. bytes = sizeof_ca_ecc_key_der_256;
  28608. #else
  28609. file = XFOPEN(eccCaKeyFile, "rb");
  28610. if (!file) {
  28611. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  28612. }
  28613. bytes = XFREAD(der, 1, FOURK_BUF, file);
  28614. XFCLOSE(file);
  28615. if (bytes == 0)
  28616. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  28617. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  28618. (void)eccCaKey384File;
  28619. #endif
  28620. #endif /* USE_CERT_BUFFERS_256 */
  28621. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  28622. /* Get CA Key */
  28623. ret = wc_ecc_init_ex(caEccKey, HEAP_HINT, devId);
  28624. if (ret != 0)
  28625. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28626. ret = wc_EccPrivateKeyDecode(der, &idx, caEccKey, (word32)bytes);
  28627. if (ret != 0)
  28628. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28629. /* Make a public key */
  28630. ret = wc_ecc_init_ex(certPubKey, HEAP_HINT, devId);
  28631. if (ret != 0)
  28632. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28633. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, certPubKey);
  28634. #if defined(WOLFSSL_ASYNC_CRYPT)
  28635. ret = wc_AsyncWait(ret, &certPubKey->asyncDev, WC_ASYNC_FLAG_NONE);
  28636. #endif
  28637. if (ret != 0)
  28638. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28639. TEST_SLEEP();
  28640. /* Setup Certificate */
  28641. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  28642. if (ret != 0)
  28643. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28644. #ifndef NO_SHA256
  28645. myCert->sigType = CTC_SHA256wECDSA;
  28646. #else
  28647. myCert->sigType = CTC_SHAwECDSA;
  28648. #endif
  28649. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  28650. #ifdef WOLFSSL_CERT_EXT
  28651. /* add Policies */
  28652. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  28653. CTC_MAX_CERTPOL_SZ);
  28654. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  28655. CTC_MAX_CERTPOL_SZ);
  28656. myCert->certPoliciesNb = 2;
  28657. /* add SKID from the Public Key */
  28658. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, certPubKey);
  28659. if (ret != 0)
  28660. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28661. /* add AKID from the Public Key */
  28662. ret = wc_SetAuthKeyIdFromPublicKey(myCert, NULL, caEccKey);
  28663. if (ret != 0)
  28664. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28665. /* add Key Usage */
  28666. ret = wc_SetKeyUsage(myCert, certKeyUsage);
  28667. if (ret != 0)
  28668. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28669. #endif /* WOLFSSL_CERT_EXT */
  28670. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  28671. #if defined(USE_CERT_BUFFERS_256)
  28672. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_384,
  28673. sizeof_ca_ecc_cert_der_384);
  28674. #elif !defined(NO_FILESYSTEM)
  28675. ret = wc_SetIssuer(myCert, eccCaCert384File);
  28676. (void)eccCaCertFile;
  28677. #else
  28678. /* not testing with embedded, no file system target */
  28679. ERROR_OUT(ASN_PARSE_E, exit);
  28680. #endif /* USE_CERT_BUFFERS_256 */
  28681. #else
  28682. /* not ENABLE_ECC384_CERT_GEN_TEST */
  28683. #if defined(USE_CERT_BUFFERS_256)
  28684. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_256,
  28685. sizeof_ca_ecc_cert_der_256);
  28686. #else
  28687. ret = wc_SetIssuer(myCert, eccCaCertFile);
  28688. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  28689. (void)eccCaCert384File;
  28690. #endif
  28691. #endif
  28692. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  28693. if (ret < 0)
  28694. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28695. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, certPubKey, rng);
  28696. if (certSz < 0) {
  28697. ERROR_OUT(WC_TEST_RET_ENC_EC(certSz), exit);
  28698. }
  28699. ret = 0;
  28700. do {
  28701. #if defined(WOLFSSL_ASYNC_CRYPT)
  28702. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  28703. #endif
  28704. if (ret >= 0) {
  28705. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  28706. FOURK_BUF, NULL, caEccKey, rng);
  28707. }
  28708. } while (ret == WC_PENDING_E);
  28709. if (ret < 0)
  28710. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28711. certSz = (int)ret;
  28712. TEST_SLEEP();
  28713. #ifdef WOLFSSL_TEST_CERT
  28714. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  28715. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  28716. if (ret != 0) {
  28717. FreeDecodedCert(decode);
  28718. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28719. }
  28720. FreeDecodedCert(decode);
  28721. #endif
  28722. ret = SaveDerAndPem(der, certSz, certEccDerFile, certEccPemFile,
  28723. CERT_TYPE);
  28724. if (ret != 0) {
  28725. goto exit;
  28726. }
  28727. exit:
  28728. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28729. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28730. #endif
  28731. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28732. if (myCert != NULL)
  28733. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28734. #ifdef WOLFSSL_TEST_CERT
  28735. if (decode != NULL)
  28736. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28737. #endif
  28738. if (caEccKey != NULL) {
  28739. wc_ecc_free(caEccKey);
  28740. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28741. }
  28742. if (certPubKey != NULL) {
  28743. wc_ecc_free(certPubKey);
  28744. XFREE(certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28745. }
  28746. #else
  28747. wc_ecc_free(certPubKey);
  28748. wc_ecc_free(caEccKey);
  28749. #endif
  28750. return ret;
  28751. }
  28752. #endif /* WOLFSSL_CERT_GEN */
  28753. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  28754. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC) && \
  28755. (!defined(NO_ECC_SECP) || defined(WOLFSSL_CUSTOM_CURVES))
  28756. /* Test for the wc_ecc_key_new() and wc_ecc_key_free() functions. */
  28757. static wc_test_ret_t ecc_test_allocator(WC_RNG* rng)
  28758. {
  28759. wc_test_ret_t ret = 0;
  28760. ecc_key* key;
  28761. #ifdef WC_NO_RNG
  28762. word32 idx = 0;
  28763. #endif
  28764. key = wc_ecc_key_new(HEAP_HINT);
  28765. if (key == NULL) {
  28766. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  28767. }
  28768. #ifndef WC_NO_RNG
  28769. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  28770. #if defined(WOLFSSL_ASYNC_CRYPT)
  28771. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  28772. #endif
  28773. if (ret != 0)
  28774. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28775. #else
  28776. /* use test ECC key */
  28777. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  28778. (word32)sizeof_ecc_key_der_256);
  28779. (void)rng;
  28780. #endif
  28781. exit:
  28782. wc_ecc_key_free(key);
  28783. return ret;
  28784. }
  28785. #endif
  28786. /* ECC Non-blocking tests for Sign and Verify */
  28787. /* Requires SP math and supports P384 or P256 */
  28788. /* ./configure --enable-ecc=nonblock --enable-sp=yes,nonblock CFLAGS="-DWOLFSSL_PUBLIC_MP" */
  28789. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_HAVE_SP_ECC) && \
  28790. defined(WOLFSSL_PUBLIC_MP)
  28791. /* ECC Private Key "d" */
  28792. static const byte p256PrivKey[] = {
  28793. /* SECP256R1 */
  28794. /* d */
  28795. 0x1e, 0xe7, 0x70, 0x07, 0xd3, 0x30, 0x94, 0x39,
  28796. 0x28, 0x90, 0xdf, 0x23, 0x88, 0x2c, 0x4a, 0x34,
  28797. 0x15, 0xdb, 0x4c, 0x43, 0xcd, 0xfa, 0xe5, 0x1f,
  28798. 0x3d, 0x4c, 0x37, 0xfe, 0x59, 0x3b, 0x96, 0xd8
  28799. };
  28800. #ifdef HAVE_ECC384
  28801. static const byte p384PrivKey[] = {
  28802. /* SECP384R1 */
  28803. /* d */
  28804. 0xa4, 0xe5, 0x06, 0xe8, 0x06, 0x16, 0x3e, 0xab,
  28805. 0x89, 0xf8, 0x60, 0x43, 0xc0, 0x60, 0x25, 0xdb,
  28806. 0xba, 0x7b, 0xfe, 0x19, 0x35, 0x08, 0x55, 0x65,
  28807. 0x76, 0xe2, 0xdc, 0xe0, 0x01, 0x8b, 0x6b, 0x68,
  28808. 0xdf, 0xcf, 0x6f, 0x80, 0x12, 0xce, 0x79, 0x37,
  28809. 0xeb, 0x2b, 0x9c, 0x7b, 0xc4, 0x68, 0x1c, 0x74
  28810. };
  28811. #endif /* HAVE_ECC384 */
  28812. #ifdef HAVE_ECC521
  28813. static const byte p521PrivKey[] = {
  28814. /* SECP521R1 */
  28815. /* d */
  28816. 0x01, 0x68, 0x91, 0x33, 0x53, 0xe2, 0x90, 0x68,
  28817. 0x11, 0x8f, 0xaa, 0xa8, 0x76, 0x0c, 0xf7, 0x2a,
  28818. 0x07, 0x1b, 0x92, 0x2a, 0xa7, 0x82, 0x3d, 0xfa,
  28819. 0x83, 0xce, 0x70, 0xc8, 0xc2, 0x60, 0x82, 0xfe,
  28820. 0x18, 0x88, 0x68, 0xda, 0x6a, 0x83, 0x46, 0x78,
  28821. 0xe4, 0xe9, 0xe9, 0xcc, 0x51, 0x7f, 0xed, 0x81,
  28822. 0x02, 0x32, 0xee, 0x26, 0x87, 0xcc, 0xed, 0x63,
  28823. 0x3f, 0x39, 0x27, 0xf0, 0xd7, 0x17, 0x77, 0xa1,
  28824. 0xa4, 0x36
  28825. };
  28826. #endif /* HAVE_ECC521 */
  28827. /* ECC public key Qx/Qy */
  28828. static const byte p256PubKey[] = {
  28829. /* SECP256R1 */
  28830. /* Qx */
  28831. 0x96, 0x93, 0x1c, 0x53, 0x0b, 0x43, 0x6c, 0x42,
  28832. 0x0c, 0x52, 0x90, 0xe4, 0xa7, 0xec, 0x98, 0xb1,
  28833. 0xaf, 0xd4, 0x14, 0x49, 0xd8, 0xc1, 0x42, 0x82,
  28834. 0x04, 0x78, 0xd1, 0x90, 0xae, 0xa0, 0x6c, 0x07,
  28835. /* Qy */
  28836. 0xf2, 0x3a, 0xb5, 0x10, 0x32, 0x8d, 0xce, 0x9e,
  28837. 0x76, 0xa0, 0xd2, 0x8c, 0xf3, 0xfc, 0xa9, 0x94,
  28838. 0x43, 0x24, 0xe6, 0x82, 0x00, 0x40, 0xc6, 0xdb,
  28839. 0x1c, 0x2f, 0xcd, 0x38, 0x4b, 0x60, 0xdd, 0x61
  28840. };
  28841. #ifdef HAVE_ECC384
  28842. static const byte p384PubKey[] = {
  28843. /* SECP384R1 */
  28844. /* Qx */
  28845. 0xea, 0xcf, 0x93, 0x4f, 0x2c, 0x09, 0xbb, 0x39,
  28846. 0x14, 0x0f, 0x56, 0x64, 0xc3, 0x40, 0xb4, 0xdf,
  28847. 0x0e, 0x63, 0xae, 0xe5, 0x71, 0x4b, 0x00, 0xcc,
  28848. 0x04, 0x97, 0xff, 0xe1, 0xe9, 0x38, 0x96, 0xbb,
  28849. 0x5f, 0x91, 0xb2, 0x6a, 0xcc, 0xb5, 0x39, 0x5f,
  28850. 0x8f, 0x70, 0x59, 0xf1, 0x01, 0xf6, 0x5a, 0x2b,
  28851. /* Qy */
  28852. 0x01, 0x6c, 0x68, 0x0b, 0xcf, 0x55, 0x25, 0xaf,
  28853. 0x6d, 0x98, 0x48, 0x0a, 0xa8, 0x74, 0xc9, 0xa9,
  28854. 0x17, 0xa0, 0x0c, 0xc3, 0xfb, 0xd3, 0x23, 0x68,
  28855. 0xfe, 0x04, 0x3c, 0x63, 0x50, 0x88, 0x3b, 0xb9,
  28856. 0x4f, 0x7c, 0x67, 0x34, 0xf7, 0x3b, 0xa9, 0x73,
  28857. 0xe7, 0x1b, 0xc3, 0x51, 0x5e, 0x22, 0x18, 0xec
  28858. };
  28859. #endif
  28860. #ifdef HAVE_ECC521
  28861. static const byte p521PubKey[] = {
  28862. /* SECP521R1 */
  28863. /* Qx */
  28864. 0x01, 0x62, 0x6e, 0xf1, 0x00, 0xec, 0xd8, 0x99,
  28865. 0x58, 0x9b, 0x80, 0x6b, 0xfe, 0x2c, 0xf1, 0xb2,
  28866. 0xf0, 0xc8, 0x48, 0xdf, 0xac, 0xd2, 0x3b, 0x71,
  28867. 0x29, 0xab, 0xf0, 0x66, 0x63, 0xd8, 0x8e, 0xb5,
  28868. 0xc8, 0xc2, 0xfc, 0x99, 0x44, 0xe2, 0x45, 0xb1,
  28869. 0x5a, 0x7b, 0xb9, 0x73, 0x01, 0xda, 0x79, 0xec,
  28870. 0x9c, 0x26, 0x27, 0x34, 0x45, 0x26, 0xd5, 0x89,
  28871. 0x4b, 0x44, 0xfe, 0x69, 0x4e, 0x72, 0x14, 0xe3,
  28872. 0x8b, 0xbc,
  28873. /* Qy */
  28874. 0x00, 0x0f, 0x09, 0xa2, 0x03, 0xc3, 0x5a, 0xdc,
  28875. 0x95, 0x82, 0xf6, 0xf9, 0xf6, 0x9c, 0xff, 0xb5,
  28876. 0x6b, 0x75, 0x95, 0x4b, 0xa4, 0x28, 0x5d, 0x9e,
  28877. 0x90, 0x04, 0xd1, 0xc0, 0x1e, 0xd5, 0xfd, 0x43,
  28878. 0x9e, 0x1e, 0x83, 0xc0, 0x11, 0x2b, 0x2b, 0x07,
  28879. 0x6d, 0xa9, 0x7a, 0x10, 0xd7, 0x67, 0xe7, 0x51,
  28880. 0x37, 0x24, 0xd8, 0xbf, 0x03, 0x0d, 0x8b, 0xb5,
  28881. 0x40, 0x5c, 0x4f, 0xd6, 0x13, 0x73, 0x42, 0xbc,
  28882. 0x91, 0xd9
  28883. };
  28884. /* perform verify of signature and hash using public key */
  28885. /* key is public Qx + public Qy */
  28886. /* sig is r + s */
  28887. static wc_test_ret_t crypto_ecc_verify(const byte *key, uint32_t keySz,
  28888. const byte *hash, uint32_t hashSz, const byte *sig, uint32_t sigSz,
  28889. uint32_t curveSz, int curveId)
  28890. {
  28891. wc_test_ret_t ret;
  28892. int verify_res = 0, count = 0;
  28893. mp_int r, s;
  28894. ecc_key ecc;
  28895. ecc_nb_ctx_t nb_ctx;
  28896. /* validate arguments */
  28897. if (key == NULL || hash == NULL || sig == NULL || curveSz == 0 ||
  28898. hashSz == 0 || keySz < (curveSz*2) || sigSz < (curveSz*2))
  28899. {
  28900. return WC_TEST_RET_ENC_NC;
  28901. }
  28902. /* Setup the ECC key */
  28903. ret = wc_ecc_init(&ecc);
  28904. if (ret < 0) {
  28905. return WC_TEST_RET_ENC_EC(ret);
  28906. }
  28907. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  28908. if (ret != MP_OKAY) {
  28909. wc_ecc_free(&ecc);
  28910. return WC_TEST_RET_ENC_EC(ret);
  28911. }
  28912. /* Setup the signature r/s variables */
  28913. ret = mp_init(&r);
  28914. if (ret != MP_OKAY) {
  28915. wc_ecc_free(&ecc);
  28916. return WC_TEST_RET_ENC_EC(ret);
  28917. }
  28918. ret = mp_init(&s);
  28919. if (ret != MP_OKAY) {
  28920. mp_clear(&r);
  28921. wc_ecc_free(&ecc);
  28922. return WC_TEST_RET_ENC_EC(ret);
  28923. }
  28924. /* Import public key x/y */
  28925. ret = wc_ecc_import_unsigned(
  28926. &ecc,
  28927. (byte*)key, /* Public "x" Coordinate */
  28928. (byte*)(key + curveSz), /* Public "y" Coordinate */
  28929. NULL, /* Private "d" (optional) */
  28930. curveId /* ECC Curve Id */
  28931. );
  28932. /* Make sure it was a public key imported */
  28933. if (ret == 0 && ecc.type != ECC_PUBLICKEY) {
  28934. ret = WC_TEST_RET_ENC_NC; /* ECC_BAD_ARG_E */
  28935. }
  28936. /* Import signature r/s */
  28937. if (ret == 0) {
  28938. ret = mp_read_unsigned_bin(&r, sig, curveSz);
  28939. if (ret < 0)
  28940. ret = WC_TEST_RET_ENC_EC(ret);
  28941. }
  28942. if (ret == 0) {
  28943. ret = mp_read_unsigned_bin(&s, sig + curveSz, curveSz);
  28944. if (ret < 0)
  28945. ret = WC_TEST_RET_ENC_EC(ret);
  28946. }
  28947. /* Verify ECC Signature */
  28948. if (ret == 0) {
  28949. do {
  28950. ret = wc_ecc_verify_hash_ex(
  28951. &r, &s, /* r/s as mp_int */
  28952. hash, hashSz, /* computed hash digest */
  28953. &verify_res, /* verification result 1=success */
  28954. &ecc
  28955. );
  28956. count++;
  28957. /* This is where real-time work could be called */
  28958. } while (ret == FP_WOULDBLOCK);
  28959. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  28960. printf("ECC non-block verify: %d times\n", count);
  28961. #endif
  28962. if (ret < 0)
  28963. ret = WC_TEST_RET_ENC_EC(ret);
  28964. }
  28965. /* check verify result */
  28966. if (ret == 0 && verify_res == 0) {
  28967. ret = WC_TEST_RET_ENC_NC /* SIG_VERIFY_E */;
  28968. }
  28969. mp_clear(&r);
  28970. mp_clear(&s);
  28971. wc_ecc_free(&ecc);
  28972. (void)count;
  28973. return ret;
  28974. }
  28975. /* perform signature operation against hash using private key */
  28976. static wc_test_ret_t crypto_ecc_sign(const byte *key, uint32_t keySz,
  28977. const byte *hash, uint32_t hashSz, byte *sig, uint32_t* sigSz,
  28978. uint32_t curveSz, int curveId, WC_RNG* rng)
  28979. {
  28980. wc_test_ret_t ret;
  28981. int count = 0;
  28982. mp_int r, s;
  28983. ecc_key ecc;
  28984. ecc_nb_ctx_t nb_ctx;
  28985. /* validate arguments */
  28986. if (key == NULL || hash == NULL || sig == NULL || sigSz == NULL ||
  28987. curveSz == 0 || hashSz == 0 || keySz < curveSz || *sigSz < (curveSz*2))
  28988. {
  28989. return WC_TEST_RET_ENC_NC /* BAD_FUNC_ARG */;
  28990. }
  28991. /* Initialize signature result */
  28992. memset(sig, 0, curveSz*2);
  28993. /* Setup the ECC key */
  28994. ret = wc_ecc_init(&ecc);
  28995. if (ret < 0) {
  28996. return WC_TEST_RET_ENC_EC(ret);
  28997. }
  28998. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  28999. if (ret != MP_OKAY) {
  29000. wc_ecc_free(&ecc);
  29001. return WC_TEST_RET_ENC_EC(ret);
  29002. }
  29003. /* Setup the signature r/s variables */
  29004. ret = mp_init(&r);
  29005. if (ret != MP_OKAY) {
  29006. wc_ecc_free(&ecc);
  29007. return WC_TEST_RET_ENC_EC(ret);
  29008. }
  29009. ret = mp_init(&s);
  29010. if (ret != MP_OKAY) {
  29011. mp_clear(&r);
  29012. wc_ecc_free(&ecc);
  29013. return WC_TEST_RET_ENC_EC(ret);
  29014. }
  29015. /* Import private key "k" */
  29016. ret = wc_ecc_import_private_key_ex(
  29017. key, keySz, /* private key "d" */
  29018. NULL, 0, /* public (optional) */
  29019. &ecc,
  29020. curveId /* ECC Curve Id */
  29021. );
  29022. if (ret < 0)
  29023. ret = WC_TEST_RET_ENC_EC(ret);
  29024. if (ret == 0) {
  29025. do {
  29026. /* Verify ECC Signature */
  29027. ret = wc_ecc_sign_hash_ex(
  29028. hash, hashSz, /* computed hash digest */
  29029. rng, &ecc, /* random and key context */
  29030. &r, &s /* r/s as mp_int */
  29031. );
  29032. count++;
  29033. /* This is where real-time work could be called */
  29034. } while (ret == FP_WOULDBLOCK);
  29035. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  29036. printf("ECC non-block sign: %d times\n", count);
  29037. #endif
  29038. if (ret < 0)
  29039. ret = WC_TEST_RET_ENC_EC(ret);
  29040. }
  29041. if (ret == 0) {
  29042. /* export r/s */
  29043. mp_to_unsigned_bin_len(&r, sig, curveSz);
  29044. mp_to_unsigned_bin_len(&s, sig + curveSz, curveSz);
  29045. }
  29046. mp_clear(&r);
  29047. mp_clear(&s);
  29048. wc_ecc_free(&ecc);
  29049. (void)count;
  29050. return ret;
  29051. }
  29052. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  29053. /*
  29054. * This test doesn't work with WOLFSSL_VALIDATE_ECC_KEYGEN defined because we
  29055. * don't have non-blocking versions of the key checking functions, yet.
  29056. */
  29057. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  29058. static wc_test_ret_t ecc_test_nonblock_dhe(int curveId, word32 curveSz,
  29059. const byte* privKey, const byte* pubKey, WC_RNG* rng)
  29060. {
  29061. wc_test_ret_t ret;
  29062. ecc_key keyA;
  29063. ecc_key keyB;
  29064. ecc_nb_ctx_t nbCtxA;
  29065. ecc_nb_ctx_t nbCtxB;
  29066. byte secretA[ECC_SHARED_SIZE];
  29067. byte secretB[ECC_SHARED_SIZE];
  29068. word32 secretSzA = ECC_SHARED_SIZE;
  29069. word32 secretSzB = ECC_SHARED_SIZE;
  29070. int count = 0;
  29071. ret = wc_ecc_init(&keyA);
  29072. if (ret == 0) {
  29073. ret = wc_ecc_init(&keyB);
  29074. if (ret < 0)
  29075. ret = WC_TEST_RET_ENC_EC(ret);
  29076. }
  29077. if (ret == 0) {
  29078. ret = wc_ecc_set_nonblock(&keyA, &nbCtxA);
  29079. if (ret < 0)
  29080. ret = WC_TEST_RET_ENC_EC(ret);
  29081. }
  29082. if (ret == 0) {
  29083. ret = wc_ecc_set_nonblock(&keyB, &nbCtxB);
  29084. if (ret < 0)
  29085. ret = WC_TEST_RET_ENC_EC(ret);
  29086. }
  29087. if (ret == 0) {
  29088. do {
  29089. ret = wc_ecc_make_key_ex(rng, curveSz, &keyA, curveId);
  29090. count++;
  29091. } while (ret == FP_WOULDBLOCK);
  29092. if (ret < 0)
  29093. ret = WC_TEST_RET_ENC_EC(ret);
  29094. }
  29095. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  29096. fprintf(stderr, "ECC non-block key gen: %d times\n", count);
  29097. #endif
  29098. if (ret == 0) {
  29099. ret = wc_ecc_check_key(&keyA);
  29100. if (ret < 0)
  29101. ret = WC_TEST_RET_ENC_EC(ret);
  29102. }
  29103. if (ret == 0) {
  29104. ret = wc_ecc_import_unsigned(&keyB, pubKey, pubKey + curveSz,
  29105. privKey, curveId);
  29106. if (ret < 0)
  29107. ret = WC_TEST_RET_ENC_EC(ret);
  29108. }
  29109. count = 0;
  29110. if (ret == 0) {
  29111. do {
  29112. ret = wc_ecc_shared_secret(&keyA, &keyB, secretA, &secretSzA);
  29113. count++;
  29114. } while (ret == FP_WOULDBLOCK);
  29115. if (ret < 0)
  29116. ret = WC_TEST_RET_ENC_EC(ret);
  29117. }
  29118. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  29119. fprintf(stderr, "ECC non-block shared secret: %d times\n", count);
  29120. #endif
  29121. if (ret == 0) {
  29122. do {
  29123. ret = wc_ecc_shared_secret(&keyB, &keyA, secretB, &secretSzB);
  29124. } while (ret == FP_WOULDBLOCK);
  29125. if (ret < 0)
  29126. ret = WC_TEST_RET_ENC_EC(ret);
  29127. }
  29128. if (ret == 0) {
  29129. if (secretSzA != secretSzB ||
  29130. XMEMCMP(secretA, secretB, secretSzA) != 0) {
  29131. ret = WC_TEST_RET_ENC_NC;
  29132. }
  29133. }
  29134. wc_ecc_free(&keyA);
  29135. wc_ecc_free(&keyB);
  29136. return ret;
  29137. }
  29138. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  29139. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  29140. static wc_test_ret_t ecc_test_nonblock_ecdsa(int curveId, word32 curveSz,
  29141. const byte* privKey, word32 privKeySz, const byte* pubKey, word32 pubKeySz,
  29142. WC_RNG* rng)
  29143. {
  29144. wc_test_ret_t ret = 0;
  29145. byte* sig = NULL;
  29146. word32 sigSz = curveSz * 2;
  29147. static const byte hash[] = {
  29148. 0x8d, 0x28, 0xa3, 0x8b, 0x0b, 0xa9, 0xfe, 0xd4, 0x0e, 0x54, 0xc4, 0x17,
  29149. 0x3d, 0x54, 0x66, 0x34, 0xbf, 0x5d, 0x6f, 0x46, 0xc2, 0x20, 0xcb, 0xc3,
  29150. 0x22, 0xe9, 0xb0, 0xdf, 0xe7, 0x64, 0x3f, 0xd9
  29151. };
  29152. sig = (byte*)XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  29153. if (sig == NULL) {
  29154. ret = WC_TEST_RET_ENC_ERRNO;
  29155. }
  29156. if (ret == 0) {
  29157. /* Sign hash using private key */
  29158. /* Note: result of an ECC sign varies for each call even with same
  29159. private key and hash. This is because a new random public key is
  29160. used for each operation. */
  29161. ret = crypto_ecc_sign(privKey, privKeySz, hash, sizeof(hash), sig,
  29162. &sigSz, curveSz, curveId, rng);
  29163. }
  29164. if (ret == 0) {
  29165. /* Verify generated signature is valid */
  29166. ret = crypto_ecc_verify(pubKey, pubKeySz, hash, sizeof(hash), sig,
  29167. sigSz, curveSz, curveId);
  29168. }
  29169. if (sig != NULL) {
  29170. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  29171. }
  29172. return ret;
  29173. }
  29174. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  29175. static wc_test_ret_t ecc_test_nonblock(WC_RNG* rng)
  29176. {
  29177. wc_test_ret_t ret = 0;
  29178. word32 i;
  29179. int curveIds[3] = {0, 0, 0};
  29180. word32 curveSzs[3] = {0, 0, 0};
  29181. const byte* privKeys[3] = {NULL, NULL, NULL};
  29182. word32 privKeySzs[3] = {0, 0, 0};
  29183. const byte* pubKeys[3] = {NULL, NULL, NULL};
  29184. word32 pubKeySzs[3] = {0, 0, 0};
  29185. curveIds[0] = ECC_SECP256R1;
  29186. curveSzs[0] = 32;
  29187. privKeys[0] = p256PrivKey;
  29188. privKeySzs[0] = sizeof(p256PrivKey);
  29189. pubKeys[0] = p256PubKey;
  29190. pubKeySzs[0] = sizeof(p256PubKey);
  29191. #ifdef HAVE_ECC384
  29192. curveIds[1] = ECC_SECP384R1;
  29193. curveSzs[1] = 48;
  29194. privKeys[1] = p384PrivKey;
  29195. privKeySzs[1] = sizeof(p384PrivKey);
  29196. pubKeys[1] = p384PubKey;
  29197. pubKeySzs[1] = sizeof(p384PubKey);
  29198. #endif
  29199. #ifdef HAVE_ECC521
  29200. curveIds[2] = ECC_SECP521R1;
  29201. curveSzs[2] = 66;
  29202. privKeys[2] = p521PrivKey;
  29203. privKeySzs[2] = sizeof(p521PrivKey);
  29204. pubKeys[2] = p521PubKey;
  29205. pubKeySzs[2] = sizeof(p521PubKey);
  29206. #endif
  29207. for (i = 0; ret == 0 && i < sizeof(curveIds) / sizeof(curveIds[0]); ++i) {
  29208. if (curveIds[i] == 0) {
  29209. continue;
  29210. }
  29211. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  29212. ret = ecc_test_nonblock_ecdsa(curveIds[i], curveSzs[i], privKeys[i],
  29213. privKeySzs[i], pubKeys[i], pubKeySzs[i], rng);
  29214. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  29215. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  29216. if (ret == 0) {
  29217. ret = ecc_test_nonblock_dhe(curveIds[i], curveSzs[i], privKeys[i],
  29218. pubKeys[i], rng);
  29219. }
  29220. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  29221. }
  29222. return ret;
  29223. }
  29224. #endif /* WC_ECC_NONBLOCK && WOLFSSL_HAVE_SP_ECC && WOLFSSL_PUBLIC_MP */
  29225. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST) && \
  29226. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  29227. (HAVE_FIPS_VERSION > 2)))
  29228. static int ecc_test_raw_enc_dec(void)
  29229. {
  29230. int ret;
  29231. unsigned char r[1];
  29232. word32 rSz;
  29233. unsigned char s[1];
  29234. word32 sSz;
  29235. unsigned char rZero[] = { 0, 0, 0, 0 };
  29236. unsigned char sOne[] = { 0, 0, 1 };
  29237. unsigned char sigRaw[32];
  29238. word32 sigRawSz;
  29239. unsigned char expSig[] = { 0x30, 0x06, 0x02, 0x01, 0x00, 0x02, 0x01, 0x01 };
  29240. sigRawSz = sizeof(sigRaw);
  29241. ret = wc_ecc_rs_raw_to_sig(rZero, sizeof(rZero), sOne, sizeof(sOne),
  29242. sigRaw, &sigRawSz);
  29243. if (ret != 0) {
  29244. return WC_TEST_RET_ENC_EC(ret);
  29245. }
  29246. if (sigRawSz != sizeof(expSig)) {
  29247. return WC_TEST_RET_ENC_EC((int)sigRawSz);
  29248. }
  29249. if (XMEMCMP(sigRaw, expSig, sizeof(expSig)) != 0) {
  29250. return WC_TEST_RET_ENC_NC;
  29251. }
  29252. rSz = sizeof(r);
  29253. sSz = sizeof(s);
  29254. ret = wc_ecc_sig_to_rs(sigRaw, sigRawSz, r, &rSz, s, &sSz);
  29255. if (ret != 0) {
  29256. return WC_TEST_RET_ENC_EC(ret);
  29257. }
  29258. if (rSz != 1) {
  29259. return WC_TEST_RET_ENC_EC((int)rSz);
  29260. }
  29261. if (sSz != 1) {
  29262. return WC_TEST_RET_ENC_EC((int)sSz);
  29263. }
  29264. if (r[0] != 0) {
  29265. return WC_TEST_RET_ENC_EC(r[0]);
  29266. }
  29267. if (s[0] != 1) {
  29268. return WC_TEST_RET_ENC_EC(s[0]);
  29269. }
  29270. return ret;
  29271. }
  29272. #endif
  29273. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test(void)
  29274. {
  29275. wc_test_ret_t ret;
  29276. WC_RNG rng;
  29277. WOLFSSL_ENTER("ecc_test");
  29278. #if defined(ECC_MIN_KEY_SZ)
  29279. WOLFSSL_MSG_EX("ecc_test ECC_MIN_KEY_SZ = %d\n", ECC_MIN_KEY_SZ);
  29280. #else
  29281. WOLFSSL_MSG("ecc_test ECC_MIN_KEY_SZ not defined.");
  29282. #endif
  29283. #if defined(WOLFSSL_CERT_EXT) && \
  29284. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  29285. ret = ecc_decode_test();
  29286. if (ret < 0)
  29287. return ret;
  29288. #endif
  29289. #ifndef HAVE_FIPS
  29290. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  29291. #else
  29292. ret = wc_InitRng(&rng);
  29293. #endif
  29294. #ifndef WC_NO_RNG
  29295. if (ret != 0)
  29296. return WC_TEST_RET_ENC_EC(ret);
  29297. #else
  29298. (void)ret;
  29299. #endif
  29300. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  29301. ret = ecc_test_curve(&rng, 14, ECC_CURVE_DEF);
  29302. if (ret < 0) {
  29303. printf("keySize=14, Default\n");
  29304. goto done;
  29305. }
  29306. #endif /* HAVE_ECC112 */
  29307. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  29308. ret = ecc_test_curve(&rng, 16, ECC_CURVE_DEF);
  29309. if (ret < 0) {
  29310. printf("keySize=16, Default\n");
  29311. goto done;
  29312. }
  29313. #endif /* HAVE_ECC128 */
  29314. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  29315. ret = ecc_test_curve(&rng, 20, ECC_CURVE_DEF);
  29316. if (ret < 0) {
  29317. printf("keySize=20, Default\n");
  29318. goto done;
  29319. }
  29320. #endif /* HAVE_ECC160 */
  29321. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  29322. #if !FIPS_VERSION3_GE(6,0,0)
  29323. ret = ecc_test_curve(&rng, 24, ECC_CURVE_DEF);
  29324. if (ret < 0) {
  29325. printf("keySize=24, Default\n");
  29326. goto done;
  29327. }
  29328. #endif
  29329. #endif /* HAVE_ECC192 */
  29330. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  29331. ret = ecc_test_curve(&rng, 28, ECC_CURVE_DEF);
  29332. if (ret < 0) {
  29333. printf("keySize=28, Default\n");
  29334. goto done;
  29335. }
  29336. #endif /* HAVE_ECC224 */
  29337. #if (defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 239
  29338. ret = ecc_test_curve(&rng, 30, ECC_CURVE_DEF);
  29339. if (ret < 0) {
  29340. printf("keySize=30, Default\n");
  29341. goto done;
  29342. }
  29343. #endif /* HAVE_ECC239 */
  29344. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  29345. ret = ecc_test_curve(&rng, 32, ECC_CURVE_DEF);
  29346. if (ret < 0) {
  29347. printf("keySize=32, Default\n");
  29348. goto done;
  29349. }
  29350. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  29351. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  29352. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  29353. ret = ecc_point_test();
  29354. if (ret < 0) {
  29355. goto done;
  29356. }
  29357. #endif
  29358. #if !defined(NO_ECC_SECP) || defined(WOLFSSL_CUSTOM_CURVES)
  29359. ret = ecc_def_curve_test(&rng);
  29360. if (ret < 0) {
  29361. printf("Default\n");
  29362. goto done;
  29363. }
  29364. #endif
  29365. #endif /* !NO_ECC256 */
  29366. #if (defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 320
  29367. ret = ecc_test_curve(&rng, 40, ECC_CURVE_DEF);
  29368. if (ret < 0) {
  29369. printf("keySize=40, Default\n");
  29370. goto done;
  29371. }
  29372. #endif /* HAVE_ECC320 */
  29373. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  29374. ret = ecc_test_curve(&rng, 48, ECC_CURVE_DEF);
  29375. if (ret < 0) {
  29376. printf("keySize=48, Default\n");
  29377. goto done;
  29378. }
  29379. #endif /* HAVE_ECC384 */
  29380. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  29381. ret = ecc_test_curve(&rng, 64, ECC_CURVE_DEF);
  29382. if (ret < 0) {
  29383. printf("keySize=64, Default\n");
  29384. goto done;
  29385. }
  29386. #endif /* HAVE_ECC512 */
  29387. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  29388. ret = ecc_test_curve(&rng, 66, ECC_CURVE_DEF);
  29389. if (ret < 0) {
  29390. printf("keySize=66, Default\n");
  29391. goto done;
  29392. }
  29393. #endif /* HAVE_ECC521 */
  29394. #ifdef WOLFSSL_SM2
  29395. ret = ecc_test_curve(&rng, 32, ECC_SM2P256V1);
  29396. if (ret < 0) {
  29397. printf("SM2\n");
  29398. goto done;
  29399. }
  29400. #endif
  29401. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST) && \
  29402. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  29403. (HAVE_FIPS_VERSION > 2)))
  29404. ret = ecc_test_raw_enc_dec();
  29405. if (ret != 0) {
  29406. printf("raw sig encode/decode\n");
  29407. goto done;
  29408. }
  29409. #endif
  29410. #if defined(WOLFSSL_CUSTOM_CURVES)
  29411. ret = ecc_test_custom_curves(&rng);
  29412. if (ret != 0) {
  29413. printf("Custom\n");
  29414. goto done;
  29415. }
  29416. #endif
  29417. #if defined(WOLFSSL_SM2)
  29418. ret = test_sm2_verify();
  29419. if (ret != 0) {
  29420. printf("SM2 Verify\n");
  29421. goto done;
  29422. }
  29423. ret = ecc_sm2_test_curve(&rng, ECC_TEST_VERIFY_COUNT);
  29424. if (ret != 0) {
  29425. printf("SM2 test\n");
  29426. goto done;
  29427. }
  29428. #endif
  29429. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  29430. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  29431. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  29432. #ifdef HAVE_ECC256
  29433. ret = ecc_test_deterministic_k(&rng);
  29434. if (ret != 0) {
  29435. printf("ecc_test_deterministic_k failed!\n");
  29436. goto done;
  29437. }
  29438. #endif
  29439. #ifdef WOLFSSL_PUBLIC_MP
  29440. #if defined(HAVE_ECC384)
  29441. ret = ecc384_test_deterministic_k(&rng);
  29442. if (ret != 0) {
  29443. printf("ecc384_test_deterministic_k failed!\n");
  29444. goto done;
  29445. }
  29446. #endif
  29447. #if defined(HAVE_ECC521)
  29448. ret = ecc521_test_deterministic_k(&rng);
  29449. if (ret != 0) {
  29450. printf("ecc512_test_deterministic_k failed!\n");
  29451. goto done;
  29452. }
  29453. #endif
  29454. #endif
  29455. #endif
  29456. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  29457. !defined(WOLFSSL_KCAPI_ECC)
  29458. ret = ecc_test_sign_vectors(&rng);
  29459. if (ret != 0) {
  29460. printf("ecc_test_sign_vectors failed!\n");
  29461. goto done;
  29462. }
  29463. #endif
  29464. #if defined(HAVE_ECC_VECTOR_TEST) && defined(HAVE_ECC_CDH) && \
  29465. defined(HAVE_ECC_DHE)
  29466. ret = ecc_test_cdh_vectors(&rng);
  29467. if (ret != 0) {
  29468. printf("ecc_test_cdh_vectors failed!\n");
  29469. goto done;
  29470. }
  29471. #endif
  29472. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  29473. !defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
  29474. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  29475. ret = ecc_test_make_pub(&rng);
  29476. if (ret != 0) {
  29477. printf("ecc_test_make_pub failed!\n");
  29478. goto done;
  29479. }
  29480. #elif defined(HAVE_ECC_KEY_IMPORT)
  29481. (void)ecc_test_make_pub; /* for compiler warning */
  29482. #endif
  29483. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ECC_SECP) && !defined(NO_ASN_TIME)
  29484. ret = ecc_test_cert_gen(&rng);
  29485. if (ret != 0) {
  29486. printf("ecc_test_cert_gen failed!\n");
  29487. goto done;
  29488. }
  29489. #endif
  29490. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC) && \
  29491. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && (!defined(NO_ECC_SECP) || \
  29492. defined(WOLFSSL_CUSTOM_CURVES))
  29493. ret = ecc_test_allocator(&rng);
  29494. if (ret != 0) {
  29495. printf("ecc_test_allocator failed!\n");
  29496. goto done;
  29497. }
  29498. #endif
  29499. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  29500. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  29501. ret = ecc_test_nonblock(&rng);
  29502. if (ret != 0) {
  29503. printf("ecc_test_nonblock failed!\n");
  29504. goto done;
  29505. }
  29506. #endif
  29507. done:
  29508. wc_FreeRng(&rng);
  29509. return ret;
  29510. }
  29511. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  29512. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  29513. #if ((! defined(HAVE_FIPS)) || FIPS_VERSION_GE(5,3))
  29514. static wc_test_ret_t ecc_ctx_kdf_salt_test(WC_RNG* rng, ecc_key* a, ecc_key* b)
  29515. {
  29516. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29517. byte* plaintext;
  29518. byte* encrypted;
  29519. byte* decrypted;
  29520. #else
  29521. byte plaintext[128];
  29522. byte encrypted[128];
  29523. byte decrypted[128];
  29524. #endif
  29525. ecEncCtx* aCtx = NULL;
  29526. ecEncCtx* bCtx = NULL;
  29527. static const byte salt[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  29528. 14, 15};
  29529. wc_test_ret_t ret = 0;
  29530. static const char message[] = "Hello wolfSSL!";
  29531. word32 plaintextLen;
  29532. word32 encryptLen = 128;
  29533. word32 decryptLen = 128;
  29534. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29535. plaintext = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29536. encrypted = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29537. decrypted = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29538. #endif
  29539. wc_ecc_free(a);
  29540. wc_ecc_free(b);
  29541. ret = wc_ecc_init(a);
  29542. if (ret != 0)
  29543. ret = WC_TEST_RET_ENC_EC(ret);
  29544. if (ret == 0) {
  29545. ret = wc_ecc_init(b);
  29546. if (ret != 0)
  29547. ret = WC_TEST_RET_ENC_EC(ret);
  29548. }
  29549. if (ret == 0)
  29550. ret = wc_ecc_make_key(rng, 32, a);
  29551. if (ret == 0)
  29552. ret = wc_ecc_make_key(rng, 32, b);
  29553. /* create context */
  29554. if (ret == 0) {
  29555. aCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  29556. if (aCtx == NULL)
  29557. ret = WC_TEST_RET_ENC_NC;
  29558. }
  29559. if (ret == 0) {
  29560. bCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  29561. if (bCtx == NULL)
  29562. ret = WC_TEST_RET_ENC_NC;
  29563. }
  29564. /* set salt */
  29565. if (ret == 0) {
  29566. ret = wc_ecc_ctx_set_kdf_salt(aCtx, salt, sizeof(salt));
  29567. if (ret != 0)
  29568. ret = 10472;
  29569. }
  29570. if (ret == 0) {
  29571. ret = wc_ecc_ctx_set_kdf_salt(bCtx, salt, sizeof(salt));
  29572. if (ret != 0)
  29573. ret = 10473;
  29574. }
  29575. XMEMSET(plaintext, 0, 128);
  29576. XSTRLCPY((char *)plaintext, message, sizeof plaintext);
  29577. plaintextLen = (((word32)XSTRLEN(message) + AES_BLOCK_SIZE - 1) /
  29578. AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
  29579. /* encrypt */
  29580. if (ret == 0) {
  29581. ret = wc_ecc_encrypt(a, b, plaintext, plaintextLen, encrypted,
  29582. &encryptLen, aCtx);
  29583. if (ret != 0)
  29584. ret = WC_TEST_RET_ENC_EC(ret);
  29585. }
  29586. /* decrypt */
  29587. if (ret == 0) {
  29588. ret = wc_ecc_decrypt(b, a, encrypted, encryptLen, decrypted,
  29589. &decryptLen, bCtx);
  29590. if (ret != 0)
  29591. ret = WC_TEST_RET_ENC_EC(ret);
  29592. }
  29593. /* compare */
  29594. if (ret == 0 && XMEMCMP(decrypted, plaintext, plaintextLen) != 0)
  29595. ret = WC_TEST_RET_ENC_NC;
  29596. wc_ecc_free(a);
  29597. wc_ecc_free(b);
  29598. wc_ecc_ctx_free(aCtx);
  29599. wc_ecc_ctx_free(bCtx);
  29600. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29601. XFREE(plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29602. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29603. XFREE(decrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29604. #endif
  29605. return ret;
  29606. }
  29607. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  29608. /* ecc_encrypt_e2e_test() uses wc_ecc_ctx_set_algo(), which was added in
  29609. * wolfFIPS 5.3.
  29610. * ecc_encrypt_kat() is used only by ecc_encrypt_e2e_test().
  29611. */
  29612. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  29613. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  29614. ECC_MIN_KEY_SZ <= 256 && defined(WOLFSSL_AES_128)
  29615. static wc_test_ret_t ecc_encrypt_kat(WC_RNG *rng)
  29616. {
  29617. wc_test_ret_t ret = 0;
  29618. #ifdef WOLFSSL_ECIES_OLD
  29619. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29620. ecc_key* userA = NULL;
  29621. #else
  29622. ecc_key userA[1];
  29623. #endif
  29624. int userAInit = 0;
  29625. #endif
  29626. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29627. ecc_key* userB = NULL;
  29628. #else
  29629. ecc_key userB[1];
  29630. #endif
  29631. int userBInit = 0;
  29632. ecc_key* tmpKey;
  29633. byte plain[48];
  29634. word32 plainSz = sizeof(plain);
  29635. WOLFSSL_SMALL_STACK_STATIC const byte privKey[] = {
  29636. 0x04, 0x80, 0xef, 0x1d, 0xbe, 0x02, 0x0c, 0x20,
  29637. 0x5b, 0xab, 0x80, 0x35, 0x5b, 0x2a, 0x0f, 0x6d,
  29638. 0xd3, 0xb0, 0x7f, 0x7e, 0x7f, 0x86, 0x8a, 0x49,
  29639. 0xee, 0xb4, 0xaa, 0x09, 0x2d, 0x1e, 0x1d, 0x02
  29640. };
  29641. #if defined(WOLFSSL_ECIES_OLD) || defined(WOLFSSL_QNX_CAAM)
  29642. WOLFSSL_SMALL_STACK_STATIC const byte pubKey[] = {
  29643. 0x04,
  29644. /* X */
  29645. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  29646. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  29647. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  29648. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  29649. /* X */
  29650. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  29651. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  29652. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  29653. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28
  29654. };
  29655. #endif
  29656. WOLFSSL_SMALL_STACK_STATIC const byte enc_msg[] = {
  29657. #ifdef WOLFSSL_ECIES_OLD
  29658. 0x42, 0x70, 0xbf, 0xf9, 0xf4, 0x7e, 0x4b, 0x9b,
  29659. 0xb5, 0x4c, 0xcc, 0xc5, 0x94, 0xa7, 0xef, 0xaa,
  29660. 0xc3, 0x7c, 0x85, 0xa6, 0x51, 0x6e, 0xd3, 0xfa,
  29661. 0x56, 0xc9, 0x10, 0x4d, 0x14, 0x32, 0x61, 0xb8,
  29662. 0xbb, 0x66, 0x7a, 0xb5, 0xbc, 0x95, 0xf8, 0xca,
  29663. 0xd1, 0x2a, 0x19, 0x51, 0x44, 0xd8, 0x0e, 0x57,
  29664. 0x34, 0xed, 0x45, 0x89, 0x2e, 0x57, 0xbe, 0xd5,
  29665. 0x06, 0x22, 0xd7, 0x13, 0x0a, 0x0e, 0x40, 0x36,
  29666. 0x0d, 0x05, 0x0d, 0xb6, 0xae, 0x61, 0x37, 0x18,
  29667. 0x83, 0x90, 0x0a, 0x27, 0x95, 0x41, 0x8c, 0x45
  29668. #elif defined(WOLFSSL_ECIES_ISO18033)
  29669. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  29670. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  29671. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  29672. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  29673. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  29674. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  29675. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  29676. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  29677. 0x28, 0xbb, 0x9f, 0xa8, 0x2d, 0xe1, 0xf1, 0x67,
  29678. 0x45, 0x02, 0x19, 0xdc, 0xc8, 0x24, 0x8b, 0x20,
  29679. 0x02, 0xa0, 0x8f, 0x95, 0x12, 0x55, 0x51, 0xf8,
  29680. 0x03, 0xc4, 0x54, 0x13, 0x98, 0x2d, 0xf0, 0x31,
  29681. 0x51, 0x80, 0x45, 0x24, 0xcb, 0x8b, 0x48, 0xa6,
  29682. 0x8b, 0x8e, 0x97, 0x9c, 0x56, 0x4d, 0x70, 0x00,
  29683. 0x53, 0xd3, 0x47, 0x00, 0x5a, 0x23, 0x8c, 0xf9,
  29684. 0xfd, 0xd2, 0x33, 0x2c, 0x43, 0x6e, 0x9e, 0xb2,
  29685. 0xf4, 0x95, 0xd4, 0xcf, 0x30, 0xd6, 0xa2, 0xc5,
  29686. 0x35, 0x96, 0x6a, 0xd4, 0x36, 0x15, 0xa9, 0xbd,
  29687. 0x7f
  29688. #elif defined(WOLFSSL_ECIES_GEN_IV)
  29689. /* EC P-256 point */
  29690. 0x04,
  29691. /* X */
  29692. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  29693. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  29694. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  29695. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  29696. /* Y */
  29697. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  29698. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  29699. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  29700. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28,
  29701. /* IV */
  29702. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29703. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29704. /* Encrypted Msg */
  29705. 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d, 0x50,
  29706. 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c, 0x1f,
  29707. 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc, 0x44,
  29708. 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca, 0x65,
  29709. 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61, 0xbd,
  29710. 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1, 0xde,
  29711. /* HMAC */
  29712. 0x5a, 0x22, 0xc1, 0x5d, 0x99, 0x66, 0x3f, 0x24,
  29713. 0x35, 0x96, 0xac, 0xf7, 0xf6, 0x28, 0x45, 0x16,
  29714. 0x52, 0x19, 0x0d, 0xe4, 0xb2, 0xca, 0x5b, 0x28,
  29715. 0x4e, 0xbb, 0xf3, 0x98, 0x57, 0xd7, 0x3b, 0xe2
  29716. #else
  29717. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  29718. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  29719. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  29720. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  29721. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  29722. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  29723. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  29724. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  29725. 0x28, 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d,
  29726. 0x50, 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c,
  29727. 0x1f, 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc,
  29728. 0x44, 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca,
  29729. 0x65, 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61,
  29730. 0xbd, 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1,
  29731. 0xde, 0xc7, 0x3f, 0x6f, 0xce, 0xbe, 0x49, 0x61,
  29732. 0x48, 0x01, 0x77, 0x41, 0xd0, 0xd8, 0x5b, 0x48,
  29733. 0xca, 0x4e, 0x47, 0x3e, 0x47, 0xbf, 0x1d, 0x28,
  29734. 0x4c, 0x18, 0x1a, 0xfb, 0x96, 0x95, 0xda, 0xde,
  29735. 0x55
  29736. #endif
  29737. };
  29738. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  29739. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  29740. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  29741. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  29742. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  29743. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  29744. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  29745. };
  29746. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29747. userB = (ecc_key *)XMALLOC(sizeof(*userB), HEAP_HINT,
  29748. DYNAMIC_TYPE_TMP_BUFFER);
  29749. if (userB == NULL) {
  29750. ret = WC_TEST_RET_ENC_ERRNO;
  29751. }
  29752. #ifdef WOLFSSL_ECIES_OLD
  29753. if (ret == 0) {
  29754. userA = (ecc_key *)XMALLOC(sizeof(*userA), HEAP_HINT,
  29755. DYNAMIC_TYPE_TMP_BUFFER);
  29756. if (userA == NULL) {
  29757. ret = WC_TEST_RET_ENC_ERRNO;
  29758. }
  29759. }
  29760. #endif
  29761. #endif
  29762. if (ret == 0) {
  29763. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  29764. if (ret != 0)
  29765. ret = WC_TEST_RET_ENC_EC(ret);
  29766. }
  29767. if (ret == 0) {
  29768. userBInit = 1;
  29769. #ifdef WOLFSSL_ECIES_OLD
  29770. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  29771. if (ret != 0)
  29772. ret = WC_TEST_RET_ENC_EC(ret);
  29773. }
  29774. if (ret == 0) {
  29775. userAInit = 1;
  29776. tmpKey = userA;
  29777. #else
  29778. tmpKey = NULL;
  29779. #endif
  29780. }
  29781. if (ret == 0) {
  29782. #ifdef WOLFSSL_QNX_CAAM
  29783. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), pubKey,
  29784. sizeof(pubKey), userB, ECC_SECP256R1);
  29785. #else
  29786. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), NULL, 0,
  29787. userB, ECC_SECP256R1);
  29788. #endif
  29789. if (ret != 0)
  29790. ret = WC_TEST_RET_ENC_EC(ret);
  29791. }
  29792. #ifdef WOLFSSL_ECIES_OLD
  29793. if (ret == 0) {
  29794. ret = wc_ecc_import_x963_ex(pubKey, sizeof(pubKey), userA,
  29795. ECC_SECP256R1);
  29796. if (ret != 0)
  29797. ret = WC_TEST_RET_ENC_EC(ret);
  29798. }
  29799. #endif
  29800. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  29801. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  29802. !defined(HAVE_SELFTEST)
  29803. if (ret == 0) {
  29804. ret = wc_ecc_set_rng(userB, rng);
  29805. if (ret != 0) {
  29806. ret = WC_TEST_RET_ENC_EC(ret);
  29807. }
  29808. }
  29809. #else
  29810. (void)rng;
  29811. #endif
  29812. if (ret == 0) {
  29813. ret = wc_ecc_decrypt(userB, tmpKey, enc_msg, sizeof(enc_msg), plain,
  29814. &plainSz, NULL);
  29815. if (ret != 0)
  29816. ret = WC_TEST_RET_ENC_EC(ret);
  29817. }
  29818. if (ret == 0) {
  29819. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  29820. ret = WC_TEST_RET_ENC_NC;
  29821. }
  29822. }
  29823. if (userBInit)
  29824. wc_ecc_free(userB);
  29825. #ifdef WOLFSSL_ECIES_OLD
  29826. if (userAInit)
  29827. wc_ecc_free(userA);
  29828. #endif
  29829. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29830. if (userB != NULL) {
  29831. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29832. }
  29833. #ifdef WOLFSSL_ECIES_OLD
  29834. if (userA != NULL) {
  29835. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29836. }
  29837. #endif
  29838. #endif
  29839. return ret;
  29840. }
  29841. #endif
  29842. static wc_test_ret_t ecc_encrypt_e2e_test(WC_RNG* rng, ecc_key* userA, ecc_key* userB,
  29843. byte encAlgo, byte kdfAlgo, byte macAlgo)
  29844. {
  29845. wc_test_ret_t ret = 0;
  29846. byte msg[48];
  29847. byte plain[48];
  29848. #ifdef WOLFSSL_ECIES_OLD
  29849. byte out[80];
  29850. #elif defined(WOLFSSL_ECIES_GEN_IV)
  29851. byte out[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  29852. #else
  29853. byte out[1 + ECC_KEYGEN_SIZE * 2 + 80];
  29854. #endif
  29855. word32 outSz = sizeof(out);
  29856. word32 plainSz = sizeof(plain);
  29857. int i;
  29858. ecEncCtx* cliCtx = NULL;
  29859. ecEncCtx* srvCtx = NULL;
  29860. byte cliSalt[EXCHANGE_SALT_SZ];
  29861. byte srvSalt[EXCHANGE_SALT_SZ];
  29862. const byte* tmpSalt;
  29863. byte msg2[48];
  29864. byte plain2[48];
  29865. #ifdef WOLFSSL_ECIES_OLD
  29866. byte out2[80];
  29867. #elif defined(WOLFSSL_ECIES_GEN_IV)
  29868. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  29869. #else
  29870. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 80];
  29871. #endif
  29872. word32 outSz2 = sizeof(out2);
  29873. word32 plainSz2 = sizeof(plain2);
  29874. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29875. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof(ecc_key), HEAP_HINT,
  29876. DYNAMIC_TYPE_TMP_BUFFER);
  29877. #else
  29878. ecc_key tmpKey[1];
  29879. #endif
  29880. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29881. if (tmpKey == NULL) {
  29882. ERROR_OUT(MEMORY_E, done);
  29883. }
  29884. #endif
  29885. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  29886. if (ret != 0)
  29887. goto done;
  29888. /* set message to incrementing 0,1,2,etc... */
  29889. for (i = 0; i < (int)sizeof(msg); i++)
  29890. msg[i] = i;
  29891. /* encrypt msg to B */
  29892. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz, NULL);
  29893. if (ret != 0) {
  29894. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  29895. }
  29896. #ifdef WOLFSSL_ECIES_OLD
  29897. tmpKey->dp = userA->dp;
  29898. ret = wc_ecc_copy_point(&userA->pubkey, &tmpKey->pubkey);
  29899. if (ret != 0) {
  29900. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  29901. }
  29902. #endif
  29903. /* decrypt msg from A */
  29904. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, NULL);
  29905. if (ret != 0) {
  29906. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  29907. }
  29908. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  29909. ret = WC_TEST_RET_ENC_NC; goto done;
  29910. }
  29911. #ifndef WOLFSSL_ECIES_OLD
  29912. /* A decrypts msg (response) from B */
  29913. ret = wc_ecc_decrypt(userB, NULL, out, outSz, plain2, &plainSz2, NULL);
  29914. if (ret != 0)
  29915. goto done;
  29916. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  29917. ret = WC_TEST_RET_ENC_NC; goto done;
  29918. }
  29919. #endif
  29920. /* let's verify message exchange works, A is client, B is server */
  29921. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  29922. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  29923. if (cliCtx == NULL || srvCtx == NULL) {
  29924. ret = WC_TEST_RET_ENC_ERRNO; goto done;
  29925. }
  29926. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  29927. if (ret != 0)
  29928. goto done;
  29929. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  29930. if (ret != 0)
  29931. goto done;
  29932. /* get salt to send to peer */
  29933. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  29934. if (tmpSalt == NULL) {
  29935. ret = WC_TEST_RET_ENC_NC; goto done;
  29936. }
  29937. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  29938. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  29939. if (tmpSalt == NULL) {
  29940. ret = WC_TEST_RET_ENC_NC; goto done;
  29941. }
  29942. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  29943. /* in actual use, we'd get the peer's salt over the transport */
  29944. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  29945. if (ret != 0)
  29946. goto done;
  29947. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  29948. if (ret != 0)
  29949. goto done;
  29950. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11);
  29951. if (ret != 0)
  29952. goto done;
  29953. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11);
  29954. if (ret != 0)
  29955. goto done;
  29956. /* get encrypted msg (request) to send to B */
  29957. outSz = sizeof(out);
  29958. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz,cliCtx);
  29959. if (ret != 0)
  29960. goto done;
  29961. #ifndef WOLFSSL_ECIES_OLD
  29962. wc_ecc_free(tmpKey);
  29963. #endif
  29964. /* B decrypts msg (request) from A */
  29965. plainSz = sizeof(plain);
  29966. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  29967. if (ret != 0)
  29968. goto done;
  29969. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  29970. ret = WC_TEST_RET_ENC_NC; goto done;
  29971. }
  29972. /* msg2 (response) from B to A */
  29973. for (i = 0; i < (int)sizeof(msg2); i++)
  29974. msg2[i] = i + sizeof(msg2);
  29975. /* get encrypted msg (response) to send to B */
  29976. ret = wc_ecc_encrypt(userB, userA, msg2, sizeof(msg2), out2,
  29977. &outSz2, srvCtx);
  29978. if (ret != 0)
  29979. goto done;
  29980. #ifdef WOLFSSL_ECIES_OLD
  29981. tmpKey->dp = userB->dp;
  29982. ret = wc_ecc_copy_point(&userB->pubkey, &tmpKey->pubkey);
  29983. if (ret != 0) {
  29984. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  29985. }
  29986. #else
  29987. wc_ecc_free(tmpKey);
  29988. #endif
  29989. /* A decrypts msg (response) from B */
  29990. ret = wc_ecc_decrypt(userA, tmpKey, out2, outSz2, plain2, &plainSz2,
  29991. cliCtx);
  29992. if (ret != 0)
  29993. goto done;
  29994. if (XMEMCMP(plain2, msg2, sizeof(msg2)) != 0) {
  29995. ret = WC_TEST_RET_ENC_NC; goto done;
  29996. }
  29997. #if defined(HAVE_COMP_KEY) && \
  29998. (! defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  29999. /* Create new client and server contexts. */
  30000. wc_ecc_ctx_free(srvCtx);
  30001. wc_ecc_ctx_free(cliCtx);
  30002. /* let's verify message exchange works, A is client, B is server */
  30003. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  30004. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  30005. if (cliCtx == NULL || srvCtx == NULL) {
  30006. ret = WC_TEST_RET_ENC_ERRNO; goto done;
  30007. }
  30008. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  30009. if (ret != 0)
  30010. goto done;
  30011. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  30012. if (ret != 0)
  30013. goto done;
  30014. /* get salt to send to peer */
  30015. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  30016. if (tmpSalt == NULL) {
  30017. ret = WC_TEST_RET_ENC_NC; goto done;
  30018. }
  30019. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  30020. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  30021. if (tmpSalt == NULL) {
  30022. ret = WC_TEST_RET_ENC_NC; goto done;
  30023. }
  30024. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  30025. /* in actual use, we'd get the peer's salt over the transport */
  30026. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  30027. if (ret != 0)
  30028. goto done;
  30029. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  30030. if (ret != 0)
  30031. goto done;
  30032. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 12);
  30033. if (ret != 0)
  30034. goto done;
  30035. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 12);
  30036. if (ret != 0)
  30037. goto done;
  30038. /* get encrypted msg (request) to send to B - compressed public key */
  30039. outSz = sizeof(out);
  30040. ret = wc_ecc_encrypt_ex(userA, userB, msg, sizeof(msg), out, &outSz, cliCtx,
  30041. 1);
  30042. if (ret != 0)
  30043. goto done;
  30044. #ifndef WOLFSSL_ECIES_OLD
  30045. wc_ecc_free(tmpKey);
  30046. #endif
  30047. /* B decrypts msg (request) from A - out has a compressed public key */
  30048. plainSz = sizeof(plain);
  30049. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  30050. if (ret != 0)
  30051. goto done;
  30052. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  30053. ret = WC_TEST_RET_ENC_NC; goto done;
  30054. }
  30055. #endif /* HAVE_COMP_KEY && (!FIPS || FIPS>=5.3) */
  30056. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  30057. (ECC_MIN_KEY_SZ <= 256) && defined(WOLFSSL_AES_128)
  30058. ret = ecc_encrypt_kat(rng);
  30059. #endif
  30060. done:
  30061. /* cleanup */
  30062. wc_ecc_ctx_free(srvCtx);
  30063. wc_ecc_ctx_free(cliCtx);
  30064. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30065. if (tmpKey != NULL) {
  30066. wc_ecc_free(tmpKey);
  30067. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30068. }
  30069. #else
  30070. wc_ecc_free(tmpKey);
  30071. #endif
  30072. return ret;
  30073. }
  30074. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  30075. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_encrypt_test(void)
  30076. {
  30077. WC_RNG rng;
  30078. wc_test_ret_t ret;
  30079. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30080. ecc_key *userA;
  30081. ecc_key *userB;
  30082. #else
  30083. ecc_key userA[1];
  30084. ecc_key userB[1];
  30085. #endif
  30086. WOLFSSL_ENTER("ecc_encrypt_test");
  30087. #ifndef HAVE_FIPS
  30088. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  30089. #else
  30090. ret = wc_InitRng(&rng);
  30091. #endif
  30092. if (ret != 0)
  30093. return WC_TEST_RET_ENC_EC(ret);
  30094. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30095. userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT,
  30096. DYNAMIC_TYPE_TMP_BUFFER);
  30097. userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT,
  30098. DYNAMIC_TYPE_TMP_BUFFER);
  30099. if ((userA == NULL) || (userB == NULL)) {
  30100. ERROR_OUT(MEMORY_E, done);
  30101. }
  30102. #endif
  30103. XMEMSET(userA, 0, sizeof *userA);
  30104. XMEMSET(userB, 0, sizeof *userB);
  30105. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  30106. if (ret != 0)
  30107. goto done;
  30108. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  30109. if (ret != 0)
  30110. goto done;
  30111. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userA);
  30112. #if defined(WOLFSSL_ASYNC_CRYPT)
  30113. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  30114. #endif
  30115. if (ret != 0){
  30116. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  30117. }
  30118. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userB);
  30119. #if defined(WOLFSSL_ASYNC_CRYPT)
  30120. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  30121. #endif
  30122. if (ret != 0){
  30123. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  30124. }
  30125. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  30126. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  30127. !defined(HAVE_SELFTEST)
  30128. ret = wc_ecc_set_rng(userA, &rng);
  30129. if (ret != 0) {
  30130. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  30131. }
  30132. ret = wc_ecc_set_rng(userB, &rng);
  30133. if (ret != 0) {
  30134. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  30135. }
  30136. #endif
  30137. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  30138. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  30139. #ifdef WOLFSSL_AES_128
  30140. if (ret == 0) {
  30141. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  30142. ecHKDF_SHA256, ecHMAC_SHA256);
  30143. if (ret != 0) {
  30144. printf("ECIES: AES_128_CBC, HKDF_SHA256, HMAC_SHA256\n");
  30145. }
  30146. }
  30147. #ifdef HAVE_X963_KDF
  30148. if (ret == 0) {
  30149. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  30150. ecKDF_X963_SHA256, ecHMAC_SHA256);
  30151. if (ret != 0) {
  30152. printf("ECIES: AES_128_CBC, KDF_X963_SHA256, HMAC_SHA256\n");
  30153. }
  30154. }
  30155. if (ret == 0) {
  30156. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  30157. ecKDF_SHA256, ecHMAC_SHA256);
  30158. if (ret != 0) {
  30159. printf("ECIES: AES_128_CBC, KDF_SHA256, HMAC_SHA256\n");
  30160. }
  30161. }
  30162. #endif
  30163. #endif
  30164. #ifdef WOLFSSL_AES_256
  30165. if (ret == 0) {
  30166. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CBC,
  30167. ecHKDF_SHA256, ecHMAC_SHA256);
  30168. if (ret != 0) {
  30169. printf("ECIES: AES_256_CBC, HKDF_SHA256, HMAC_SHA256\n");
  30170. }
  30171. }
  30172. #endif
  30173. #endif
  30174. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER)
  30175. #ifdef WOLFSSL_AES_128
  30176. if (ret == 0) {
  30177. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CTR,
  30178. ecHKDF_SHA256, ecHMAC_SHA256);
  30179. if (ret != 0) {
  30180. printf("ECIES: AES_128_CTR, HKDF_SHA256, HMAC_SHA256\n");
  30181. }
  30182. }
  30183. #endif
  30184. #ifdef WOLFSSL_AES_256
  30185. if (ret == 0) {
  30186. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CTR,
  30187. ecHKDF_SHA256, ecHMAC_SHA256);
  30188. if (ret != 0) {
  30189. printf("ECIES: AES_256_CTR, HKDF_SHA256, HMAC_SHA256\n");
  30190. }
  30191. }
  30192. #endif
  30193. #endif /* !NO_AES && WOLFSSL_AES_COUNTER */
  30194. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  30195. if (ret == 0) {
  30196. ret = ecc_ctx_kdf_salt_test(&rng, userA, userB);
  30197. }
  30198. #endif
  30199. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  30200. done:
  30201. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30202. if (userA != NULL) {
  30203. wc_ecc_free(userA);
  30204. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30205. }
  30206. if (userB != NULL) {
  30207. wc_ecc_free(userB);
  30208. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30209. }
  30210. #else
  30211. wc_ecc_free(userB);
  30212. wc_ecc_free(userA);
  30213. #endif
  30214. wc_FreeRng(&rng);
  30215. return ret;
  30216. }
  30217. #endif /* HAVE_ECC_ENCRYPT && HAVE_AES_CBC && WOLFSSL_AES_128 */
  30218. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  30219. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  30220. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  30221. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  30222. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test_buffers(void)
  30223. {
  30224. size_t bytes;
  30225. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30226. ecc_key *cliKey = (ecc_key *)XMALLOC(sizeof *cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30227. ecc_key *servKey = (ecc_key *)XMALLOC(sizeof *servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30228. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof *tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30229. #else
  30230. ecc_key cliKey[1];
  30231. ecc_key servKey[1];
  30232. ecc_key tmpKey[1];
  30233. #endif
  30234. WC_RNG rng;
  30235. word32 idx = 0;
  30236. wc_test_ret_t ret;
  30237. /* pad our test message to 32 bytes so evenly divisible by AES_BLOCK_SZ */
  30238. byte in[] = "Everyone gets Friday off. ecc p";
  30239. word32 inLen = (word32)XSTRLEN((char*)in);
  30240. byte out[256];
  30241. byte plain[256];
  30242. int verify = 0;
  30243. word32 x;
  30244. WOLFSSL_ENTER("ecc_test_buffers");
  30245. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30246. if ((cliKey == NULL) || (servKey == NULL) || (tmpKey == NULL))
  30247. ERROR_OUT(MEMORY_E, done);
  30248. #endif
  30249. ret = wc_ecc_init_ex(cliKey, HEAP_HINT, devId);
  30250. if (ret != 0)
  30251. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30252. ret = wc_ecc_init_ex(servKey, HEAP_HINT, devId);
  30253. if (ret != 0)
  30254. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30255. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  30256. if (ret != 0)
  30257. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30258. bytes = (size_t)sizeof_ecc_clikey_der_256;
  30259. /* place client key into ecc_key struct cliKey */
  30260. ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, cliKey,
  30261. (word32)bytes);
  30262. if (ret != 0)
  30263. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30264. idx = 0;
  30265. bytes = (size_t)sizeof_ecc_key_der_256;
  30266. /* place server key into ecc_key struct servKey */
  30267. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, servKey,
  30268. (word32)bytes);
  30269. if (ret != 0)
  30270. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30271. #ifndef WC_NO_RNG
  30272. #ifndef HAVE_FIPS
  30273. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  30274. #else
  30275. ret = wc_InitRng(&rng);
  30276. #endif
  30277. if (ret != 0)
  30278. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30279. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  30280. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  30281. !defined(HAVE_SELFTEST)
  30282. ret = wc_ecc_set_rng(cliKey, &rng);
  30283. if (ret != 0)
  30284. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30285. ret = wc_ecc_set_rng(servKey, &rng);
  30286. if (ret != 0)
  30287. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30288. #endif
  30289. #endif /* !WC_NO_RNG */
  30290. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_HKDF) && \
  30291. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  30292. {
  30293. word32 y;
  30294. /* test encrypt and decrypt if they're available */
  30295. x = sizeof(out);
  30296. ret = wc_ecc_encrypt(cliKey, servKey, in, sizeof(in), out, &x, NULL);
  30297. if (ret < 0)
  30298. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30299. #ifdef WOLFSSL_ECIES_OLD
  30300. tmpKey->dp = cliKey->dp;
  30301. ret = wc_ecc_copy_point(&cliKey->pubkey, &tmpKey->pubkey);
  30302. if (ret != 0) {
  30303. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  30304. }
  30305. #endif
  30306. y = sizeof(plain);
  30307. ret = wc_ecc_decrypt(servKey, tmpKey, out, x, plain, &y, NULL);
  30308. if (ret < 0)
  30309. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30310. if (XMEMCMP(plain, in, inLen))
  30311. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  30312. }
  30313. #endif
  30314. x = sizeof(out);
  30315. do {
  30316. #if defined(WOLFSSL_ASYNC_CRYPT)
  30317. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  30318. #endif
  30319. if (ret == 0)
  30320. ret = wc_ecc_sign_hash(in, inLen, out, &x, &rng, cliKey);
  30321. } while (ret == WC_PENDING_E);
  30322. if (ret < 0)
  30323. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30324. TEST_SLEEP();
  30325. XMEMSET(plain, 0, sizeof(plain));
  30326. do {
  30327. #if defined(WOLFSSL_ASYNC_CRYPT)
  30328. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  30329. #endif
  30330. if (ret == 0)
  30331. ret = wc_ecc_verify_hash(out, x, in, inLen, &verify,
  30332. cliKey);
  30333. } while (ret == WC_PENDING_E);
  30334. if (ret < 0)
  30335. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30336. if (verify != 1)
  30337. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  30338. TEST_SLEEP();
  30339. #ifdef WOLFSSL_CERT_EXT
  30340. idx = 0;
  30341. bytes = sizeof_ecc_clikeypub_der_256;
  30342. ret = wc_EccPublicKeyDecode(ecc_clikeypub_der_256, &idx, cliKey,
  30343. (word32) bytes);
  30344. if (ret != 0)
  30345. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30346. #endif
  30347. ret = 0;
  30348. done:
  30349. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30350. if (cliKey != NULL) {
  30351. wc_ecc_free(cliKey);
  30352. XFREE(cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30353. }
  30354. if (servKey != NULL) {
  30355. wc_ecc_free(servKey);
  30356. XFREE(servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30357. }
  30358. if (tmpKey != NULL) {
  30359. wc_ecc_free(tmpKey);
  30360. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30361. }
  30362. #else
  30363. wc_ecc_free(cliKey);
  30364. wc_ecc_free(servKey);
  30365. wc_ecc_free(tmpKey);
  30366. #endif
  30367. wc_FreeRng(&rng);
  30368. return ret;
  30369. }
  30370. #endif /* USE_CERT_BUFFERS_256 && !WOLFSSL_ATECCX08A && !NO_ECC256 */
  30371. #endif /* HAVE_ECC */
  30372. #ifdef HAVE_CURVE25519
  30373. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  30374. defined(HAVE_CURVE25519_KEY_IMPORT)
  30375. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  30376. #define X25519_TEST_CNT 5
  30377. #else
  30378. #define X25519_TEST_CNT 1
  30379. #endif
  30380. static wc_test_ret_t curve25519_overflow_test(void)
  30381. {
  30382. /* secret key for party a */
  30383. byte sa[X25519_TEST_CNT][32] = {
  30384. {
  30385. 0x8d,0xaf,0x6e,0x7a,0xc1,0xeb,0x8d,0x30,
  30386. 0x99,0x86,0xd3,0x90,0x47,0x96,0x21,0x3c,
  30387. 0x3a,0x75,0xc0,0x7b,0x75,0x01,0x75,0xa3,
  30388. 0x81,0x4b,0xff,0x5a,0xbc,0x96,0x87,0x28
  30389. },
  30390. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  30391. {
  30392. 0x9d,0x63,0x5f,0xce,0xe2,0xe8,0xd7,0xfb,
  30393. 0x68,0x77,0x0e,0x44,0xd1,0xad,0x87,0x2b,
  30394. 0xf4,0x65,0x06,0xb7,0xbb,0xdb,0xbe,0x6e,
  30395. 0x02,0x43,0x24,0xc7,0x3d,0x7b,0x88,0x60
  30396. },
  30397. {
  30398. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  30399. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  30400. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  30401. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  30402. },
  30403. {
  30404. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  30405. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  30406. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  30407. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  30408. },
  30409. {
  30410. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  30411. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  30412. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  30413. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  30414. }
  30415. #endif
  30416. };
  30417. /* public key for party b */
  30418. byte pb[X25519_TEST_CNT][32] = {
  30419. {
  30420. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30421. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30422. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30423. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  30424. },
  30425. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  30426. {
  30427. /* 0xff first byte in original - invalid! */
  30428. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30429. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30430. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30431. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  30432. },
  30433. {
  30434. 0x36,0x1a,0x74,0x87,0x28,0x59,0xe0,0xb6,
  30435. 0xe4,0x2b,0x17,0x9b,0x16,0xb0,0x3b,0xf8,
  30436. 0xb8,0x9f,0x2a,0x8f,0xc5,0x33,0x68,0x4f,
  30437. 0xde,0x4d,0xd8,0x80,0x63,0xe7,0xb4,0x0a
  30438. },
  30439. {
  30440. 0x00,0x80,0x38,0x59,0x19,0x3a,0x66,0x12,
  30441. 0xfd,0xa1,0xec,0x1c,0x40,0x84,0x40,0xbd,
  30442. 0x64,0x10,0x8b,0x53,0x81,0x21,0x03,0x2d,
  30443. 0x7d,0x33,0xb4,0x01,0x57,0x0d,0xe1,0x89
  30444. },
  30445. {
  30446. 0x1d,0xf8,0xf8,0x33,0x89,0x6c,0xb7,0xba,
  30447. 0x94,0x73,0xfa,0xc2,0x36,0xac,0xbe,0x49,
  30448. 0xaf,0x85,0x3e,0x93,0x5f,0xae,0xb2,0xc0,
  30449. 0xc8,0x80,0x8f,0x4a,0xaa,0xd3,0x55,0x2b
  30450. }
  30451. #endif
  30452. };
  30453. /* expected shared key */
  30454. byte ss[X25519_TEST_CNT][32] = {
  30455. {
  30456. 0x5c,0x4c,0x85,0x5f,0xfb,0x20,0x38,0xcc,
  30457. 0x55,0x16,0x5b,0x8a,0xa7,0xed,0x57,0x6e,
  30458. 0x35,0xaa,0x71,0x67,0x85,0x1f,0xb6,0x28,
  30459. 0x17,0x07,0x7b,0xda,0x76,0xdd,0xe0,0xb4
  30460. },
  30461. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  30462. {
  30463. 0x33,0xf6,0xc1,0x34,0x62,0x92,0x06,0x02,
  30464. 0x95,0xdb,0x91,0x4c,0x5d,0x52,0x54,0xc7,
  30465. 0xd2,0x5b,0x24,0xb5,0x4f,0x33,0x59,0x79,
  30466. 0x9f,0x6d,0x7e,0x4a,0x4c,0x30,0xd6,0x38
  30467. },
  30468. {
  30469. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30470. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30471. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30472. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02
  30473. },
  30474. {
  30475. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30476. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30477. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30478. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09
  30479. },
  30480. {
  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. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10
  30485. }
  30486. #endif
  30487. };
  30488. wc_test_ret_t ret = 0;
  30489. int i;
  30490. word32 y;
  30491. byte shared[32];
  30492. curve25519_key userA;
  30493. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  30494. for (i = 0; i < X25519_TEST_CNT; i++) {
  30495. if (wc_curve25519_import_private_raw(sa[i], sizeof(sa[i]), pb[i],
  30496. sizeof(pb[i]), &userA) != 0) {
  30497. ret = WC_TEST_RET_ENC_I(i); break;
  30498. }
  30499. /* test against known test vector */
  30500. XMEMSET(shared, 0, sizeof(shared));
  30501. y = sizeof(shared);
  30502. if (wc_curve25519_shared_secret(&userA, &userA, shared, &y) != 0) {
  30503. ret = WC_TEST_RET_ENC_I(i); break;
  30504. }
  30505. if (XMEMCMP(ss[i], shared, y)) {
  30506. ret = WC_TEST_RET_ENC_I(i); break;
  30507. }
  30508. }
  30509. wc_curve25519_free(&userA);
  30510. return ret;
  30511. }
  30512. /* Test the wc_curve25519_check_public API.
  30513. *
  30514. * returns 0 on success and -ve on failure.
  30515. */
  30516. static wc_test_ret_t curve25519_check_public_test(void)
  30517. {
  30518. wc_test_ret_t ret;
  30519. /* Little-endian values that will fail */
  30520. byte fail_le[][CURVE25519_KEYSIZE] = {
  30521. {
  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,0x00,
  30525. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  30526. },
  30527. {
  30528. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30529. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30530. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30531. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  30532. },
  30533. {
  30534. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30535. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30536. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30537. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x81
  30538. },
  30539. };
  30540. /* Big-endian values that will fail */
  30541. byte fail_be[][CURVE25519_KEYSIZE] = {
  30542. {
  30543. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30544. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30545. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30546. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  30547. },
  30548. {
  30549. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30550. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30551. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30552. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  30553. },
  30554. {
  30555. 0x81,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30556. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30557. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30558. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  30559. },
  30560. };
  30561. /* Good or valid public value */
  30562. byte good[CURVE25519_KEYSIZE] = {
  30563. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30564. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30565. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30566. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  30567. };
  30568. int i;
  30569. /* Parameter checks */
  30570. /* NULL pointer */
  30571. ret = wc_curve25519_check_public(NULL, 0, EC25519_LITTLE_ENDIAN);
  30572. if (ret != BAD_FUNC_ARG) {
  30573. return WC_TEST_RET_ENC_EC(ret);
  30574. }
  30575. ret = wc_curve25519_check_public(NULL, 0, EC25519_BIG_ENDIAN);
  30576. if (ret != BAD_FUNC_ARG) {
  30577. return WC_TEST_RET_ENC_EC(ret);
  30578. }
  30579. /* Length of 0 treated differently to other invalid lengths for TLS */
  30580. ret = wc_curve25519_check_public(good, 0, EC25519_LITTLE_ENDIAN);
  30581. if (ret != BUFFER_E)
  30582. return WC_TEST_RET_ENC_EC(ret);
  30583. ret = wc_curve25519_check_public(good, 0, EC25519_BIG_ENDIAN);
  30584. if (ret != BUFFER_E)
  30585. return WC_TEST_RET_ENC_EC(ret);
  30586. /* Length not CURVE25519_KEYSIZE */
  30587. for (i = 1; i < CURVE25519_KEYSIZE + 2; i++) {
  30588. if (i == CURVE25519_KEYSIZE)
  30589. continue;
  30590. if (wc_curve25519_check_public(good, (word32)i, EC25519_LITTLE_ENDIAN) !=
  30591. ECC_BAD_ARG_E) {
  30592. return WC_TEST_RET_ENC_I(i);
  30593. }
  30594. if (wc_curve25519_check_public(good, (word32)i, EC25519_BIG_ENDIAN) !=
  30595. ECC_BAD_ARG_E) {
  30596. return WC_TEST_RET_ENC_I(i);
  30597. }
  30598. }
  30599. /* Little-endian fail cases */
  30600. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  30601. if (wc_curve25519_check_public(fail_le[i], CURVE25519_KEYSIZE,
  30602. EC25519_LITTLE_ENDIAN) == 0) {
  30603. return WC_TEST_RET_ENC_I(i);
  30604. }
  30605. }
  30606. /* Big-endian fail cases */
  30607. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  30608. if (wc_curve25519_check_public(fail_be[i], CURVE25519_KEYSIZE,
  30609. EC25519_BIG_ENDIAN) == 0) {
  30610. return WC_TEST_RET_ENC_I(i);
  30611. }
  30612. }
  30613. /* Check a valid public value works! */
  30614. ret = wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  30615. EC25519_LITTLE_ENDIAN);
  30616. if (ret != 0) {
  30617. return WC_TEST_RET_ENC_EC(ret);
  30618. }
  30619. ret = wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  30620. EC25519_BIG_ENDIAN);
  30621. if (ret != 0) {
  30622. return WC_TEST_RET_ENC_EC(ret);
  30623. }
  30624. return 0;
  30625. }
  30626. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  30627. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  30628. defined(HAVE_CURVE25519_KEY_IMPORT)
  30629. static wc_test_ret_t curve255519_der_test(void)
  30630. {
  30631. wc_test_ret_t ret = 0;
  30632. /* certs/statickeys/x25519.der */
  30633. const byte kCurve25519PrivDer[] = {
  30634. 0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E,
  30635. 0x04, 0x22, 0x04, 0x20, 0x78, 0x8E, 0x31, 0x5C, 0x33, 0xA9, 0x19, 0xC0,
  30636. 0x5E, 0x36, 0x70, 0x1B, 0xA4, 0xE8, 0xEF, 0xC1, 0x89, 0x8C, 0xB3, 0x15,
  30637. 0xC6, 0x79, 0xD3, 0xAC, 0x22, 0x00, 0xAE, 0xFA, 0xB3, 0xB7, 0x0F, 0x78
  30638. };
  30639. /* certs/statickeys/x25519-pub.der */
  30640. const byte kCurve25519PubDer[] = {
  30641. 0x30, 0x2A, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E, 0x03, 0x21, 0x00,
  30642. 0x09, 0xBC, 0x8C, 0xC7, 0x45, 0x0D, 0xC1, 0xC2, 0x02, 0x57, 0x9A, 0x68,
  30643. 0x3A, 0xFD, 0x7A, 0xA8, 0xA5, 0x2F, 0xF0, 0x99, 0x39, 0x98, 0xEA, 0x26,
  30644. 0xA2, 0x5B, 0x38, 0xFD, 0x96, 0xDB, 0x2A, 0x26
  30645. };
  30646. curve25519_key key;
  30647. byte output[128];
  30648. word32 outputSz = 128;
  30649. word32 idx;
  30650. ret = wc_curve25519_init_ex(&key, HEAP_HINT, devId);
  30651. if (ret != 0)
  30652. return WC_TEST_RET_ENC_EC(ret);
  30653. /* Test decode / encode of Curve25519 private key only */
  30654. if (ret == 0) {
  30655. idx = 0;
  30656. ret = wc_Curve25519PrivateKeyDecode(kCurve25519PrivDer, &idx, &key,
  30657. (word32)sizeof(kCurve25519PrivDer));
  30658. if (ret < 0)
  30659. ret = WC_TEST_RET_ENC_EC(ret);
  30660. }
  30661. if (ret == 0) {
  30662. outputSz = (word32)sizeof(output);
  30663. ret = wc_Curve25519PrivateKeyToDer(&key, output, outputSz);
  30664. if (ret >= 0) {
  30665. outputSz = (word32)ret;
  30666. ret = 0;
  30667. }
  30668. else {
  30669. ret = WC_TEST_RET_ENC_EC(ret);
  30670. }
  30671. }
  30672. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PrivDer) ||
  30673. XMEMCMP(output, kCurve25519PrivDer, outputSz) != 0)) {
  30674. ret = WC_TEST_RET_ENC_NC;
  30675. }
  30676. /* Test decode / encode of Curve25519 public key only */
  30677. if (ret == 0) {
  30678. idx = 0;
  30679. ret = wc_Curve25519PublicKeyDecode(kCurve25519PubDer, &idx, &key,
  30680. (word32)sizeof(kCurve25519PubDer));
  30681. if (ret < 0)
  30682. ret = WC_TEST_RET_ENC_EC(ret);
  30683. }
  30684. if (ret == 0) {
  30685. outputSz = (word32)sizeof(output);
  30686. ret = wc_Curve25519PublicKeyToDer(&key, output, outputSz, 1);
  30687. if (ret >= 0) {
  30688. outputSz = (word32)ret;
  30689. ret = 0;
  30690. }
  30691. else {
  30692. ret = WC_TEST_RET_ENC_EC(ret);
  30693. }
  30694. }
  30695. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PubDer) ||
  30696. XMEMCMP(output, kCurve25519PubDer, outputSz) != 0)) {
  30697. ret = WC_TEST_RET_ENC_NC;
  30698. }
  30699. wc_curve25519_free(&key);
  30700. return ret;
  30701. }
  30702. #endif /* !NO_ASN && HAVE_CURVE25519_KEY_EXPORT && HAVE_CURVE25519_KEY_IMPORT */
  30703. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve25519_test(void)
  30704. {
  30705. WC_RNG rng;
  30706. wc_test_ret_t ret;
  30707. #ifdef HAVE_CURVE25519_SHARED_SECRET
  30708. byte sharedA[32];
  30709. byte sharedB[32];
  30710. word32 y;
  30711. #endif
  30712. #ifdef HAVE_CURVE25519_KEY_EXPORT
  30713. byte exportBuf[32];
  30714. #endif
  30715. word32 x = 0;
  30716. curve25519_key userA, userB, pubKey;
  30717. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  30718. defined(HAVE_CURVE25519_KEY_IMPORT)
  30719. /* test vectors from
  30720. https://tools.ietf.org/html/draft-josefsson-tls-curve25519-03
  30721. */
  30722. /* secret key for party a */
  30723. byte sa[] = {
  30724. 0x5A,0xC9,0x9F,0x33,0x63,0x2E,0x5A,0x76,
  30725. 0x8D,0xE7,0xE8,0x1B,0xF8,0x54,0xC2,0x7C,
  30726. 0x46,0xE3,0xFB,0xF2,0xAB,0xBA,0xCD,0x29,
  30727. 0xEC,0x4A,0xFF,0x51,0x73,0x69,0xC6,0x60
  30728. };
  30729. /* public key for party a */
  30730. byte pa[] = {
  30731. 0x05,0x7E,0x23,0xEA,0x9F,0x1C,0xBE,0x8A,
  30732. 0x27,0x16,0x8F,0x6E,0x69,0x6A,0x79,0x1D,
  30733. 0xE6,0x1D,0xD3,0xAF,0x7A,0xCD,0x4E,0xEA,
  30734. 0xCC,0x6E,0x7B,0xA5,0x14,0xFD,0xA8,0x63
  30735. };
  30736. /* secret key for party b */
  30737. byte sb[] = {
  30738. 0x47,0xDC,0x3D,0x21,0x41,0x74,0x82,0x0E,
  30739. 0x11,0x54,0xB4,0x9B,0xC6,0xCD,0xB2,0xAB,
  30740. 0xD4,0x5E,0xE9,0x58,0x17,0x05,0x5D,0x25,
  30741. 0x5A,0xA3,0x58,0x31,0xB7,0x0D,0x32,0x60
  30742. };
  30743. /* public key for party b */
  30744. byte pb[] = {
  30745. 0x6E,0xB8,0x9D,0xA9,0x19,0x89,0xAE,0x37,
  30746. 0xC7,0xEA,0xC7,0x61,0x8D,0x9E,0x5C,0x49,
  30747. 0x51,0xDB,0xA1,0xD7,0x3C,0x28,0x5A,0xE1,
  30748. 0xCD,0x26,0xA8,0x55,0x02,0x0E,0xEF,0x04
  30749. };
  30750. /* expected shared key */
  30751. byte ss[] = {
  30752. 0x61,0x45,0x0C,0xD9,0x8E,0x36,0x01,0x6B,
  30753. 0x58,0x77,0x6A,0x89,0x7A,0x9F,0x0A,0xEF,
  30754. 0x73,0x8B,0x99,0xF0,0x94,0x68,0xB8,0xD6,
  30755. 0xB8,0x51,0x11,0x84,0xD5,0x34,0x94,0xAB
  30756. };
  30757. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  30758. (void)x;
  30759. WOLFSSL_ENTER("curve25519_test");
  30760. #ifndef HAVE_FIPS
  30761. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  30762. #else
  30763. ret = wc_InitRng(&rng);
  30764. #endif
  30765. if (ret != 0)
  30766. return WC_TEST_RET_ENC_EC(ret);
  30767. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  30768. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  30769. wc_curve25519_init_ex(&pubKey, HEAP_HINT, devId);
  30770. /* make curve25519 keys */
  30771. ret = wc_curve25519_make_key(&rng, 32, &userA);
  30772. if (ret != 0)
  30773. return WC_TEST_RET_ENC_EC(ret);
  30774. ret = wc_curve25519_make_key(&rng, 32, &userB);
  30775. if (ret != 0)
  30776. return WC_TEST_RET_ENC_EC(ret);
  30777. #ifdef HAVE_CURVE25519_SHARED_SECRET
  30778. /* find shared secret key */
  30779. x = sizeof(sharedA);
  30780. if ((ret = wc_curve25519_shared_secret(&userA, &userB, sharedA, &x)) != 0) {
  30781. printf("wc_curve25519_shared_secret 1 failed\n");
  30782. return WC_TEST_RET_ENC_EC(ret);
  30783. }
  30784. y = sizeof(sharedB);
  30785. if ((ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y)) != 0) {
  30786. printf("wc_curve25519_shared_secret 2 failed\n");
  30787. return WC_TEST_RET_ENC_EC(ret);
  30788. }
  30789. /* compare shared secret keys to test they are the same */
  30790. if (y != x)
  30791. return WC_TEST_RET_ENC_NC;
  30792. if (XMEMCMP(sharedA, sharedB, x))
  30793. return WC_TEST_RET_ENC_NC;
  30794. #endif
  30795. #ifdef HAVE_CURVE25519_KEY_EXPORT
  30796. /* export a public key and import it for another user */
  30797. x = sizeof(exportBuf);
  30798. ret = wc_curve25519_export_public(&userA, exportBuf, &x);
  30799. if (ret != 0)
  30800. return WC_TEST_RET_ENC_EC(ret);
  30801. #ifdef HAVE_CURVE25519_KEY_IMPORT
  30802. ret = wc_curve25519_import_public(exportBuf, x, &pubKey);
  30803. if (ret != 0)
  30804. return WC_TEST_RET_ENC_EC(ret);
  30805. #endif
  30806. #endif
  30807. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  30808. defined(HAVE_CURVE25519_KEY_IMPORT)
  30809. /* test shared key after importing a public key */
  30810. XMEMSET(sharedB, 0, sizeof(sharedB));
  30811. y = sizeof(sharedB);
  30812. if (wc_curve25519_shared_secret(&userB, &pubKey, sharedB, &y) != 0) {
  30813. return WC_TEST_RET_ENC_NC;
  30814. }
  30815. if (XMEMCMP(sharedA, sharedB, y))
  30816. return WC_TEST_RET_ENC_NC;
  30817. /* import RFC test vectors and compare shared key */
  30818. ret = wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa),
  30819. &userA);
  30820. if (ret != 0)
  30821. return WC_TEST_RET_ENC_EC(ret);
  30822. ret = wc_curve25519_import_private_raw(sb, sizeof(sb), pb, sizeof(pb),
  30823. &userB);
  30824. if (ret != 0)
  30825. return WC_TEST_RET_ENC_EC(ret);
  30826. /* test against known test vector */
  30827. XMEMSET(sharedB, 0, sizeof(sharedB));
  30828. y = sizeof(sharedB);
  30829. ret = wc_curve25519_shared_secret(&userA, &userB, sharedB, &y);
  30830. if (ret != 0)
  30831. return WC_TEST_RET_ENC_EC(ret);
  30832. if (XMEMCMP(ss, sharedB, y))
  30833. return WC_TEST_RET_ENC_NC;
  30834. /* test swapping roles of keys and generating same shared key */
  30835. XMEMSET(sharedB, 0, sizeof(sharedB));
  30836. y = sizeof(sharedB);
  30837. ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y);
  30838. if (ret != 0)
  30839. return WC_TEST_RET_ENC_EC(ret);
  30840. if (XMEMCMP(ss, sharedB, y))
  30841. return WC_TEST_RET_ENC_NC;
  30842. /* test with 1 generated key and 1 from known test vector */
  30843. ret = wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa),
  30844. &userA);
  30845. if (ret != 0)
  30846. return WC_TEST_RET_ENC_EC(ret);
  30847. wc_curve25519_free(&userB);
  30848. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  30849. ret = wc_curve25519_make_key(&rng, 32, &userB);
  30850. if (ret != 0)
  30851. return WC_TEST_RET_ENC_EC(ret);
  30852. x = sizeof(sharedA);
  30853. ret = wc_curve25519_shared_secret(&userA, &userB, sharedA, &x);
  30854. if (ret != 0)
  30855. return WC_TEST_RET_ENC_EC(ret);
  30856. y = sizeof(sharedB);
  30857. ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y);
  30858. if (ret != 0)
  30859. return WC_TEST_RET_ENC_EC(ret);
  30860. /* compare shared secret keys to test they are the same */
  30861. if (y != x)
  30862. return WC_TEST_RET_ENC_NC;
  30863. if (XMEMCMP(sharedA, sharedB, x))
  30864. return WC_TEST_RET_ENC_NC;
  30865. ret = curve25519_overflow_test();
  30866. if (ret != 0)
  30867. return ret;
  30868. ret = curve25519_check_public_test();
  30869. if (ret != 0)
  30870. return ret;
  30871. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  30872. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  30873. defined(HAVE_CURVE25519_KEY_IMPORT)
  30874. ret = curve255519_der_test();
  30875. if (ret != 0)
  30876. return ret;
  30877. #endif
  30878. /* clean up keys when done */
  30879. wc_curve25519_free(&pubKey);
  30880. wc_curve25519_free(&userB);
  30881. wc_curve25519_free(&userA);
  30882. wc_FreeRng(&rng);
  30883. return 0;
  30884. }
  30885. #endif /* HAVE_CURVE25519 */
  30886. #ifdef HAVE_ED25519
  30887. #ifdef WOLFSSL_TEST_CERT
  30888. static wc_test_ret_t ed25519_test_cert(void)
  30889. {
  30890. DecodedCert cert[2];
  30891. DecodedCert* serverCert = NULL;
  30892. DecodedCert* caCert = NULL;
  30893. #ifdef HAVE_ED25519_VERIFY
  30894. ed25519_key key;
  30895. ed25519_key* pubKey = NULL;
  30896. int verify;
  30897. #endif /* HAVE_ED25519_VERIFY */
  30898. wc_test_ret_t ret;
  30899. byte* tmp;
  30900. size_t bytes;
  30901. XFILE file;
  30902. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30903. if (tmp == NULL) {
  30904. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  30905. }
  30906. #ifdef USE_CERT_BUFFERS_256
  30907. XMEMCPY(tmp, ca_ed25519_cert, sizeof_ca_ed25519_cert);
  30908. bytes = sizeof_ca_ed25519_cert;
  30909. #elif !defined(NO_FILESYSTEM)
  30910. file = XFOPEN(caEd25519Cert, "rb");
  30911. if (file == NULL) {
  30912. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  30913. }
  30914. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  30915. XFCLOSE(file);
  30916. if (bytes == 0)
  30917. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  30918. #else
  30919. /* No certificate to use. */
  30920. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  30921. #endif
  30922. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  30923. caCert = &cert[0];
  30924. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  30925. if (ret != 0)
  30926. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30927. #ifdef USE_CERT_BUFFERS_256
  30928. XMEMCPY(tmp, server_ed25519_cert, sizeof_server_ed25519_cert);
  30929. bytes = sizeof_server_ed25519_cert;
  30930. #elif !defined(NO_FILESYSTEM)
  30931. file = XFOPEN(serverEd25519Cert, "rb");
  30932. if (file == NULL) {
  30933. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  30934. }
  30935. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  30936. XFCLOSE(file);
  30937. if (bytes == 0)
  30938. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  30939. #else
  30940. /* No certificate to use. */
  30941. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  30942. #endif
  30943. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  30944. serverCert = &cert[1];
  30945. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  30946. if (ret != 0)
  30947. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30948. #ifdef HAVE_ED25519_VERIFY
  30949. ret = wc_ed25519_init(&key);
  30950. if (ret < 0)
  30951. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30952. pubKey = &key;
  30953. ret = wc_ed25519_import_public(caCert->publicKey, caCert->pubKeySize,
  30954. pubKey);
  30955. if (ret < 0)
  30956. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30957. ret = wc_ed25519_verify_msg(serverCert->signature, serverCert->sigLength,
  30958. serverCert->source + serverCert->certBegin,
  30959. serverCert->sigIndex - serverCert->certBegin,
  30960. &verify, pubKey);
  30961. if (ret < 0 || verify != 1)
  30962. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30963. #endif /* HAVE_ED25519_VERIFY */
  30964. done:
  30965. if (tmp != NULL)
  30966. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30967. #ifdef HAVE_ED25519_VERIFY
  30968. wc_ed25519_free(pubKey);
  30969. #endif /* HAVE_ED25519_VERIFY */
  30970. if (caCert != NULL)
  30971. FreeDecodedCert(caCert);
  30972. if (serverCert != NULL)
  30973. FreeDecodedCert(serverCert);
  30974. return ret;
  30975. }
  30976. static wc_test_ret_t ed25519_test_make_cert(void)
  30977. {
  30978. WC_RNG rng;
  30979. Cert cert;
  30980. DecodedCert decode;
  30981. ed25519_key key;
  30982. ed25519_key* privKey = NULL;
  30983. wc_test_ret_t ret = 0;
  30984. byte* tmp = NULL;
  30985. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  30986. #ifndef HAVE_FIPS
  30987. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  30988. #else
  30989. ret = wc_InitRng(&rng);
  30990. #endif
  30991. if (ret != 0)
  30992. return WC_TEST_RET_ENC_EC(ret);
  30993. wc_ed25519_init(&key);
  30994. privKey = &key;
  30995. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, privKey);
  30996. cert.daysValid = 365 * 2;
  30997. cert.selfSigned = 1;
  30998. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  30999. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  31000. cert.isCA = 0;
  31001. #ifdef WOLFSSL_CERT_EXT
  31002. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  31003. if (ret < 0)
  31004. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  31005. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  31006. if (ret < 0)
  31007. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  31008. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  31009. if (ret < 0)
  31010. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  31011. #endif
  31012. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31013. if (tmp == NULL) {
  31014. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  31015. }
  31016. cert.sigType = CTC_ED25519;
  31017. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED25519_TYPE, privKey, &rng);
  31018. if (ret < 0)
  31019. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  31020. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF,
  31021. ED25519_TYPE, privKey, &rng);
  31022. if (ret < 0)
  31023. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  31024. InitDecodedCert(&decode, tmp, (word32)ret, HEAP_HINT);
  31025. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  31026. FreeDecodedCert(&decode);
  31027. if (ret != 0)
  31028. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  31029. done:
  31030. if (tmp != NULL)
  31031. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31032. wc_ed25519_free(privKey);
  31033. wc_FreeRng(&rng);
  31034. return ret;
  31035. }
  31036. #endif /* WOLFSSL_TEST_CERT */
  31037. #if defined(HAVE_ED25519_KEY_IMPORT)
  31038. static wc_test_ret_t ed25519_test_check_key(void)
  31039. {
  31040. /* Fails to find x-ordinate from this y-ordinate. */
  31041. WOLFSSL_SMALL_STACK_STATIC const byte key_bad_y[] = {
  31042. 0x40,
  31043. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31044. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31045. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31046. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31047. };
  31048. /* Y-ordinate value larger than prime. */
  31049. WOLFSSL_SMALL_STACK_STATIC const byte key_bad_y_max[] = {
  31050. 0x40,
  31051. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31052. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31053. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31054. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  31055. };
  31056. /* Y-ordinate value equal to prime. */
  31057. WOLFSSL_SMALL_STACK_STATIC const byte key_bad_y_is_p[] = {
  31058. 0x40,
  31059. 0xed,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31060. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31061. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31062. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  31063. };
  31064. /* Y-ordinate value equal to prime - 1. */
  31065. WOLFSSL_SMALL_STACK_STATIC const byte key_y_is_p_minus_1[] = {
  31066. 0x40,
  31067. 0xec,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31068. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31069. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31070. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  31071. };
  31072. ed25519_key key;
  31073. int ret;
  31074. int res = 0;
  31075. /* Initialize key for use. */
  31076. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  31077. if (ret != 0) {
  31078. return WC_TEST_RET_ENC_NC;
  31079. }
  31080. /* Load bad public key only and perform checks. */
  31081. ret = wc_ed25519_import_public(key_bad_y, ED25519_PUB_KEY_SIZE + 1, &key);
  31082. if (ret != PUBLIC_KEY_E) {
  31083. res = WC_TEST_RET_ENC_NC;
  31084. }
  31085. if (res == 0) {
  31086. /* Load bad public key only and perform checks. */
  31087. ret = wc_ed25519_import_public(key_bad_y_max, ED25519_PUB_KEY_SIZE + 1,
  31088. &key);
  31089. if (ret != PUBLIC_KEY_E) {
  31090. res = WC_TEST_RET_ENC_NC;
  31091. }
  31092. }
  31093. if (res == 0) {
  31094. /* Load bad public key only and perform checks. */
  31095. ret = wc_ed25519_import_public(key_bad_y_is_p, ED25519_PUB_KEY_SIZE + 1,
  31096. &key);
  31097. if (ret != PUBLIC_KEY_E) {
  31098. res = WC_TEST_RET_ENC_NC;
  31099. }
  31100. }
  31101. if (res == 0) {
  31102. /* Load good public key only and perform checks. */
  31103. ret = wc_ed25519_import_public(key_y_is_p_minus_1,
  31104. ED25519_PUB_KEY_SIZE + 1, &key);
  31105. if (ret != 0) {
  31106. res = WC_TEST_RET_ENC_NC;
  31107. }
  31108. }
  31109. /* Dispose of key. */
  31110. wc_ed25519_free(&key);
  31111. return res;
  31112. }
  31113. #endif
  31114. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  31115. defined(HAVE_ED25519_KEY_IMPORT)
  31116. static wc_test_ret_t ed25519ctx_test(void)
  31117. {
  31118. wc_test_ret_t ret;
  31119. byte out[ED25519_SIG_SIZE];
  31120. word32 outlen;
  31121. #ifdef HAVE_ED25519_VERIFY
  31122. int verify = 0;
  31123. #endif /* HAVE_ED25519_VERIFY */
  31124. ed25519_key key;
  31125. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  31126. 0x03,0x05,0x33,0x4e,0x38,0x1a,0xf7,0x8f,
  31127. 0x14,0x1c,0xb6,0x66,0xf6,0x19,0x9f,0x57,
  31128. 0xbc,0x34,0x95,0x33,0x5a,0x25,0x6a,0x95,
  31129. 0xbd,0x2a,0x55,0xbf,0x54,0x66,0x63,0xf6
  31130. };
  31131. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  31132. 0xdf,0xc9,0x42,0x5e,0x4f,0x96,0x8f,0x7f,
  31133. 0x0c,0x29,0xf0,0x25,0x9c,0xf5,0xf9,0xae,
  31134. 0xd6,0x85,0x1c,0x2b,0xb4,0xad,0x8b,0xfb,
  31135. 0x86,0x0c,0xfe,0xe0,0xab,0x24,0x82,0x92
  31136. };
  31137. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx1[] = {
  31138. 0x55,0xa4,0xcc,0x2f,0x70,0xa5,0x4e,0x04,
  31139. 0x28,0x8c,0x5f,0x4c,0xd1,0xe4,0x5a,0x7b,
  31140. 0xb5,0x20,0xb3,0x62,0x92,0x91,0x18,0x76,
  31141. 0xca,0xda,0x73,0x23,0x19,0x8d,0xd8,0x7a,
  31142. 0x8b,0x36,0x95,0x0b,0x95,0x13,0x00,0x22,
  31143. 0x90,0x7a,0x7f,0xb7,0xc4,0xe9,0xb2,0xd5,
  31144. 0xf6,0xcc,0xa6,0x85,0xa5,0x87,0xb4,0xb2,
  31145. 0x1f,0x4b,0x88,0x8e,0x4e,0x7e,0xdb,0x0d
  31146. };
  31147. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx2[] = {
  31148. 0xcc,0x5e,0x63,0xa2,0x7e,0x94,0xaf,0xd3,
  31149. 0x41,0x83,0x38,0xd2,0x48,0x6f,0xa9,0x2a,
  31150. 0xf9,0x91,0x7c,0x2d,0x98,0x9e,0x06,0xe5,
  31151. 0x02,0x77,0x72,0x1c,0x34,0x38,0x18,0xb4,
  31152. 0x21,0x96,0xbc,0x29,0x2e,0x68,0xf3,0x4d,
  31153. 0x85,0x9b,0xbe,0xad,0x17,0x9f,0x54,0x54,
  31154. 0x2d,0x4b,0x04,0xdc,0xfb,0xfa,0x4a,0x68,
  31155. 0x4e,0x39,0x50,0xfb,0x1c,0xcd,0x8d,0x0d
  31156. };
  31157. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  31158. 0xf7,0x26,0x93,0x6d,0x19,0xc8,0x00,0x49,
  31159. 0x4e,0x3f,0xda,0xff,0x20,0xb2,0x76,0xa8
  31160. };
  31161. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  31162. 0x66,0x6f,0x6f
  31163. };
  31164. outlen = sizeof(out);
  31165. XMEMSET(out, 0, sizeof(out));
  31166. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  31167. if (ret != 0)
  31168. return 10800;
  31169. ret = wc_ed25519_import_private_key(sKeyCtx, ED25519_KEY_SIZE, pKeyCtx,
  31170. sizeof(pKeyCtx), &key);
  31171. if (ret == 0)
  31172. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  31173. contextCtx, sizeof(contextCtx));
  31174. if (ret == 0 && XMEMCMP(out, sigCtx1, 64) != 0)
  31175. ret = WC_TEST_RET_ENC_NC;
  31176. #if defined(HAVE_ED25519_VERIFY)
  31177. /* test verify on good msg */
  31178. if (ret == 0)
  31179. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  31180. &verify, &key, contextCtx, sizeof(contextCtx));
  31181. if (ret == 0 && verify != 1)
  31182. ret = WC_TEST_RET_ENC_NC;
  31183. #endif
  31184. if (ret == 0)
  31185. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  31186. NULL, 0);
  31187. if (ret == 0 && XMEMCMP(out, sigCtx2, 64) != 0)
  31188. ret = WC_TEST_RET_ENC_NC;
  31189. #if defined(HAVE_ED25519_VERIFY)
  31190. /* test verify on good msg */
  31191. if (ret == 0)
  31192. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  31193. &verify, &key, NULL, 0);
  31194. if (ret == 0 && verify != 1)
  31195. ret = WC_TEST_RET_ENC_NC;
  31196. #endif
  31197. wc_ed25519_free(&key);
  31198. return ret;
  31199. }
  31200. static wc_test_ret_t ed25519ph_test(void)
  31201. {
  31202. wc_test_ret_t ret = 0;
  31203. byte out[ED25519_SIG_SIZE];
  31204. word32 outlen;
  31205. #ifdef HAVE_ED25519_VERIFY
  31206. int verify = 0;
  31207. #endif /* HAVE_ED25519_VERIFY */
  31208. ed25519_key key;
  31209. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  31210. 0x83,0x3f,0xe6,0x24,0x09,0x23,0x7b,0x9d,
  31211. 0x62,0xec,0x77,0x58,0x75,0x20,0x91,0x1e,
  31212. 0x9a,0x75,0x9c,0xec,0x1d,0x19,0x75,0x5b,
  31213. 0x7d,0xa9,0x01,0xb9,0x6d,0xca,0x3d,0x42
  31214. };
  31215. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  31216. 0xec,0x17,0x2b,0x93,0xad,0x5e,0x56,0x3b,
  31217. 0xf4,0x93,0x2c,0x70,0xe1,0x24,0x50,0x34,
  31218. 0xc3,0x54,0x67,0xef,0x2e,0xfd,0x4d,0x64,
  31219. 0xeb,0xf8,0x19,0x68,0x34,0x67,0xe2,0xbf
  31220. };
  31221. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  31222. 0x98,0xa7,0x02,0x22,0xf0,0xb8,0x12,0x1a,
  31223. 0xa9,0xd3,0x0f,0x81,0x3d,0x68,0x3f,0x80,
  31224. 0x9e,0x46,0x2b,0x46,0x9c,0x7f,0xf8,0x76,
  31225. 0x39,0x49,0x9b,0xb9,0x4e,0x6d,0xae,0x41,
  31226. 0x31,0xf8,0x50,0x42,0x46,0x3c,0x2a,0x35,
  31227. 0x5a,0x20,0x03,0xd0,0x62,0xad,0xf5,0xaa,
  31228. 0xa1,0x0b,0x8c,0x61,0xe6,0x36,0x06,0x2a,
  31229. 0xaa,0xd1,0x1c,0x2a,0x26,0x08,0x34,0x06
  31230. };
  31231. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  31232. 0xe0,0x39,0x70,0x2b,0x4c,0x25,0x95,0xa6,
  31233. 0xa5,0x41,0xac,0x85,0x09,0x23,0x6e,0x29,
  31234. 0x90,0x47,0x47,0x95,0x33,0x0c,0x9b,0x34,
  31235. 0xa7,0x5f,0x58,0xa6,0x60,0x12,0x9e,0x08,
  31236. 0xfd,0x73,0x69,0x43,0xfb,0x19,0x43,0xa5,
  31237. 0x57,0x20,0xb9,0xe0,0x95,0x7b,0x1e,0xd6,
  31238. 0x73,0x48,0x16,0x61,0x9f,0x13,0x88,0xf4,
  31239. 0x3f,0x73,0xe6,0xe3,0xba,0xa8,0x1c,0x0e
  31240. };
  31241. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  31242. 0x61,0x62,0x63
  31243. };
  31244. /* SHA-512 hash of msgPh */
  31245. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  31246. 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
  31247. 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
  31248. 0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,
  31249. 0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
  31250. 0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,
  31251. 0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
  31252. 0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,
  31253. 0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f
  31254. };
  31255. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  31256. 0x66,0x6f,0x6f
  31257. };
  31258. outlen = sizeof(out);
  31259. XMEMSET(out, 0, sizeof(out));
  31260. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  31261. if (ret != 0)
  31262. return WC_TEST_RET_ENC_EC(ret);
  31263. ret = wc_ed25519_import_private_key(sKeyPh, ED25519_KEY_SIZE, pKeyPh,
  31264. sizeof(pKeyPh), &key);
  31265. if (ret == 0)
  31266. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  31267. NULL, 0);
  31268. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  31269. ret = WC_TEST_RET_ENC_NC;
  31270. #if defined(HAVE_ED25519_VERIFY)
  31271. /* test verify on good msg */
  31272. if (ret == 0)
  31273. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh),
  31274. &verify, &key, NULL, 0);
  31275. if (ret == 0 && verify != 1)
  31276. ret = WC_TEST_RET_ENC_NC;
  31277. #endif
  31278. if (ret == 0)
  31279. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  31280. contextPh2, sizeof(contextPh2));
  31281. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  31282. ret = WC_TEST_RET_ENC_NC;
  31283. #if defined(HAVE_ED25519_VERIFY)
  31284. /* test verify on good msg */
  31285. if (ret == 0)
  31286. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify,
  31287. &key, contextPh2, sizeof(contextPh2));
  31288. if (ret == 0 && verify != 1)
  31289. ret = WC_TEST_RET_ENC_NC;
  31290. #endif
  31291. if (ret == 0)
  31292. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  31293. NULL, 0);
  31294. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  31295. ret = WC_TEST_RET_ENC_NC;
  31296. #if defined(HAVE_ED25519_VERIFY)
  31297. if (ret == 0)
  31298. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh),
  31299. &verify, &key, NULL, 0);
  31300. if (ret == 0 && verify != 1)
  31301. ret = WC_TEST_RET_ENC_NC;
  31302. #endif
  31303. if (ret == 0)
  31304. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  31305. contextPh2, sizeof(contextPh2));
  31306. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  31307. ret = WC_TEST_RET_ENC_NC;
  31308. #if defined(HAVE_ED25519_VERIFY)
  31309. if (ret == 0)
  31310. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  31311. &key, contextPh2, sizeof(contextPh2));
  31312. if (ret == 0 && verify != 1)
  31313. ret = WC_TEST_RET_ENC_NC;
  31314. #endif
  31315. wc_ed25519_free(&key);
  31316. return ret;
  31317. }
  31318. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  31319. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed25519_test(void)
  31320. {
  31321. wc_test_ret_t ret;
  31322. WC_RNG rng;
  31323. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  31324. defined(HAVE_ED25519_KEY_IMPORT)
  31325. byte out[ED25519_SIG_SIZE];
  31326. byte exportPKey[ED25519_KEY_SIZE];
  31327. byte exportSKey[ED25519_KEY_SIZE];
  31328. word32 exportPSz;
  31329. word32 exportSSz;
  31330. int i;
  31331. word32 outlen;
  31332. #ifdef HAVE_ED25519_VERIFY
  31333. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  31334. int j;
  31335. #endif
  31336. int verify;
  31337. #endif /* HAVE_ED25519_VERIFY */
  31338. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  31339. word32 keySz, sigSz;
  31340. ed25519_key key;
  31341. ed25519_key key2;
  31342. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  31343. defined(HAVE_ED25519_KEY_IMPORT)
  31344. /* test vectors from
  31345. https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-02
  31346. */
  31347. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  31348. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  31349. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  31350. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  31351. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  31352. };
  31353. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  31354. 0x4c,0xcd,0x08,0x9b,0x28,0xff,0x96,0xda,
  31355. 0x9d,0xb6,0xc3,0x46,0xec,0x11,0x4e,0x0f,
  31356. 0x5b,0x8a,0x31,0x9f,0x35,0xab,0xa6,0x24,
  31357. 0xda,0x8c,0xf6,0xed,0x4f,0xb8,0xa6,0xfb
  31358. };
  31359. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  31360. 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
  31361. 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
  31362. 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
  31363. 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
  31364. };
  31365. /* uncompressed test */
  31366. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  31367. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  31368. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  31369. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  31370. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  31371. };
  31372. /* compressed prefix test */
  31373. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  31374. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  31375. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  31376. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  31377. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  31378. };
  31379. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  31380. 0xf5,0xe5,0x76,0x7c,0xf1,0x53,0x31,0x95,
  31381. 0x17,0x63,0x0f,0x22,0x68,0x76,0xb8,0x6c,
  31382. 0x81,0x60,0xcc,0x58,0x3b,0xc0,0x13,0x74,
  31383. 0x4c,0x6b,0xf2,0x55,0xf5,0xcc,0x0e,0xe5
  31384. };
  31385. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  31386. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  31387. 0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  31388. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  31389. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  31390. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  31391. };
  31392. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  31393. 0x3d,0x40,0x17,0xc3,0xe8,0x43,0x89,0x5a,
  31394. 0x92,0xb7,0x0a,0xa7,0x4d,0x1b,0x7e,0xbc,
  31395. 0x9c,0x98,0x2c,0xcf,0x2e,0xc4,0x96,0x8c,
  31396. 0xc0,0xcd,0x55,0xf1,0x2a,0xf4,0x66,0x0c
  31397. };
  31398. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  31399. 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
  31400. 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
  31401. 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
  31402. 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
  31403. };
  31404. /* uncompressed test */
  31405. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  31406. 0x04,0x55,0xd0,0xe0,0x9a,0x2b,0x9d,0x34,
  31407. 0x29,0x22,0x97,0xe0,0x8d,0x60,0xd0,0xf6,
  31408. 0x20,0xc5,0x13,0xd4,0x72,0x53,0x18,0x7c,
  31409. 0x24,0xb1,0x27,0x86,0xbd,0x77,0x76,0x45,
  31410. 0xce,0x1a,0x51,0x07,0xf7,0x68,0x1a,0x02,
  31411. 0xaf,0x25,0x23,0xa6,0xda,0xf3,0x72,0xe1,
  31412. 0x0e,0x3a,0x07,0x64,0xc9,0xd3,0xfe,0x4b,
  31413. 0xd5,0xb7,0x0a,0xb1,0x82,0x01,0x98,0x5a,
  31414. 0xd7
  31415. };
  31416. /* compressed prefix */
  31417. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  31418. 0x40,0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  31419. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  31420. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  31421. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  31422. };
  31423. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  31424. 0x27,0x81,0x17,0xfc,0x14,0x4c,0x72,0x34,
  31425. 0x0f,0x67,0xd0,0xf2,0x31,0x6e,0x83,0x86,
  31426. 0xce,0xff,0xbf,0x2b,0x24,0x28,0xc9,0xc5,
  31427. 0x1f,0xef,0x7c,0x59,0x7f,0x1d,0x42,0x6e
  31428. };
  31429. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  31430. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  31431. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  31432. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  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 sig2[] = {
  31443. 0x92,0xa0,0x09,0xa9,0xf0,0xd4,0xca,0xb8,
  31444. 0x72,0x0e,0x82,0x0b,0x5f,0x64,0x25,0x40,
  31445. 0xa2,0xb2,0x7b,0x54,0x16,0x50,0x3f,0x8f,
  31446. 0xb3,0x76,0x22,0x23,0xeb,0xdb,0x69,0xda,
  31447. 0x08,0x5a,0xc1,0xe4,0x3e,0x15,0x99,0x6e,
  31448. 0x45,0x8f,0x36,0x13,0xd0,0xf1,0x1d,0x8c,
  31449. 0x38,0x7b,0x2e,0xae,0xb4,0x30,0x2a,0xee,
  31450. 0xb0,0x0d,0x29,0x16,0x12,0xbb,0x0c,0x00
  31451. };
  31452. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  31453. 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
  31454. 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
  31455. 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
  31456. 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
  31457. 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
  31458. 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
  31459. 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
  31460. 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
  31461. };
  31462. /* uncompressed test */
  31463. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  31464. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  31465. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  31466. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  31467. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  31468. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  31469. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  31470. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  31471. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  31472. };
  31473. /* compressed prefix */
  31474. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  31475. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  31476. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  31477. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  31478. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  31479. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  31480. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  31481. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  31482. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  31483. };
  31484. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  31485. 0x0a,0xab,0x4c,0x90,0x05,0x01,0xb3,0xe2,
  31486. 0x4d,0x7c,0xdf,0x46,0x63,0x32,0x6a,0x3a,
  31487. 0x87,0xdf,0x5e,0x48,0x43,0xb2,0xcb,0xdb,
  31488. 0x67,0xcb,0xf6,0xe4,0x60,0xfe,0xc3,0x50,
  31489. 0xaa,0x53,0x71,0xb1,0x50,0x8f,0x9f,0x45,
  31490. 0x28,0xec,0xea,0x23,0xc4,0x36,0xd9,0x4b,
  31491. 0x5e,0x8f,0xcd,0x4f,0x68,0x1e,0x30,0xa6,
  31492. 0xac,0x00,0xa9,0x70,0x4a,0x18,0x8a,0x03
  31493. };
  31494. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  31495. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = {0x0 };
  31496. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = {0x72};
  31497. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = {0xAF,0x82};
  31498. /* test of a 1024 byte long message */
  31499. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  31500. 0x08,0xb8,0xb2,0xb7,0x33,0x42,0x42,0x43,
  31501. 0x76,0x0f,0xe4,0x26,0xa4,0xb5,0x49,0x08,
  31502. 0x63,0x21,0x10,0xa6,0x6c,0x2f,0x65,0x91,
  31503. 0xea,0xbd,0x33,0x45,0xe3,0xe4,0xeb,0x98,
  31504. 0xfa,0x6e,0x26,0x4b,0xf0,0x9e,0xfe,0x12,
  31505. 0xee,0x50,0xf8,0xf5,0x4e,0x9f,0x77,0xb1,
  31506. 0xe3,0x55,0xf6,0xc5,0x05,0x44,0xe2,0x3f,
  31507. 0xb1,0x43,0x3d,0xdf,0x73,0xbe,0x84,0xd8,
  31508. 0x79,0xde,0x7c,0x00,0x46,0xdc,0x49,0x96,
  31509. 0xd9,0xe7,0x73,0xf4,0xbc,0x9e,0xfe,0x57,
  31510. 0x38,0x82,0x9a,0xdb,0x26,0xc8,0x1b,0x37,
  31511. 0xc9,0x3a,0x1b,0x27,0x0b,0x20,0x32,0x9d,
  31512. 0x65,0x86,0x75,0xfc,0x6e,0xa5,0x34,0xe0,
  31513. 0x81,0x0a,0x44,0x32,0x82,0x6b,0xf5,0x8c,
  31514. 0x94,0x1e,0xfb,0x65,0xd5,0x7a,0x33,0x8b,
  31515. 0xbd,0x2e,0x26,0x64,0x0f,0x89,0xff,0xbc,
  31516. 0x1a,0x85,0x8e,0xfc,0xb8,0x55,0x0e,0xe3,
  31517. 0xa5,0xe1,0x99,0x8b,0xd1,0x77,0xe9,0x3a,
  31518. 0x73,0x63,0xc3,0x44,0xfe,0x6b,0x19,0x9e,
  31519. 0xe5,0xd0,0x2e,0x82,0xd5,0x22,0xc4,0xfe,
  31520. 0xba,0x15,0x45,0x2f,0x80,0x28,0x8a,0x82,
  31521. 0x1a,0x57,0x91,0x16,0xec,0x6d,0xad,0x2b,
  31522. 0x3b,0x31,0x0d,0xa9,0x03,0x40,0x1a,0xa6,
  31523. 0x21,0x00,0xab,0x5d,0x1a,0x36,0x55,0x3e,
  31524. 0x06,0x20,0x3b,0x33,0x89,0x0c,0xc9,0xb8,
  31525. 0x32,0xf7,0x9e,0xf8,0x05,0x60,0xcc,0xb9,
  31526. 0xa3,0x9c,0xe7,0x67,0x96,0x7e,0xd6,0x28,
  31527. 0xc6,0xad,0x57,0x3c,0xb1,0x16,0xdb,0xef,
  31528. 0xef,0xd7,0x54,0x99,0xda,0x96,0xbd,0x68,
  31529. 0xa8,0xa9,0x7b,0x92,0x8a,0x8b,0xbc,0x10,
  31530. 0x3b,0x66,0x21,0xfc,0xde,0x2b,0xec,0xa1,
  31531. 0x23,0x1d,0x20,0x6b,0xe6,0xcd,0x9e,0xc7,
  31532. 0xaf,0xf6,0xf6,0xc9,0x4f,0xcd,0x72,0x04,
  31533. 0xed,0x34,0x55,0xc6,0x8c,0x83,0xf4,0xa4,
  31534. 0x1d,0xa4,0xaf,0x2b,0x74,0xef,0x5c,0x53,
  31535. 0xf1,0xd8,0xac,0x70,0xbd,0xcb,0x7e,0xd1,
  31536. 0x85,0xce,0x81,0xbd,0x84,0x35,0x9d,0x44,
  31537. 0x25,0x4d,0x95,0x62,0x9e,0x98,0x55,0xa9,
  31538. 0x4a,0x7c,0x19,0x58,0xd1,0xf8,0xad,0xa5,
  31539. 0xd0,0x53,0x2e,0xd8,0xa5,0xaa,0x3f,0xb2,
  31540. 0xd1,0x7b,0xa7,0x0e,0xb6,0x24,0x8e,0x59,
  31541. 0x4e,0x1a,0x22,0x97,0xac,0xbb,0xb3,0x9d,
  31542. 0x50,0x2f,0x1a,0x8c,0x6e,0xb6,0xf1,0xce,
  31543. 0x22,0xb3,0xde,0x1a,0x1f,0x40,0xcc,0x24,
  31544. 0x55,0x41,0x19,0xa8,0x31,0xa9,0xaa,0xd6,
  31545. 0x07,0x9c,0xad,0x88,0x42,0x5d,0xe6,0xbd,
  31546. 0xe1,0xa9,0x18,0x7e,0xbb,0x60,0x92,0xcf,
  31547. 0x67,0xbf,0x2b,0x13,0xfd,0x65,0xf2,0x70,
  31548. 0x88,0xd7,0x8b,0x7e,0x88,0x3c,0x87,0x59,
  31549. 0xd2,0xc4,0xf5,0xc6,0x5a,0xdb,0x75,0x53,
  31550. 0x87,0x8a,0xd5,0x75,0xf9,0xfa,0xd8,0x78,
  31551. 0xe8,0x0a,0x0c,0x9b,0xa6,0x3b,0xcb,0xcc,
  31552. 0x27,0x32,0xe6,0x94,0x85,0xbb,0xc9,0xc9,
  31553. 0x0b,0xfb,0xd6,0x24,0x81,0xd9,0x08,0x9b,
  31554. 0xec,0xcf,0x80,0xcf,0xe2,0xdf,0x16,0xa2,
  31555. 0xcf,0x65,0xbd,0x92,0xdd,0x59,0x7b,0x07,
  31556. 0x07,0xe0,0x91,0x7a,0xf4,0x8b,0xbb,0x75,
  31557. 0xfe,0xd4,0x13,0xd2,0x38,0xf5,0x55,0x5a,
  31558. 0x7a,0x56,0x9d,0x80,0xc3,0x41,0x4a,0x8d,
  31559. 0x08,0x59,0xdc,0x65,0xa4,0x61,0x28,0xba,
  31560. 0xb2,0x7a,0xf8,0x7a,0x71,0x31,0x4f,0x31,
  31561. 0x8c,0x78,0x2b,0x23,0xeb,0xfe,0x80,0x8b,
  31562. 0x82,0xb0,0xce,0x26,0x40,0x1d,0x2e,0x22,
  31563. 0xf0,0x4d,0x83,0xd1,0x25,0x5d,0xc5,0x1a,
  31564. 0xdd,0xd3,0xb7,0x5a,0x2b,0x1a,0xe0,0x78,
  31565. 0x45,0x04,0xdf,0x54,0x3a,0xf8,0x96,0x9b,
  31566. 0xe3,0xea,0x70,0x82,0xff,0x7f,0xc9,0x88,
  31567. 0x8c,0x14,0x4d,0xa2,0xaf,0x58,0x42,0x9e,
  31568. 0xc9,0x60,0x31,0xdb,0xca,0xd3,0xda,0xd9,
  31569. 0xaf,0x0d,0xcb,0xaa,0xaf,0x26,0x8c,0xb8,
  31570. 0xfc,0xff,0xea,0xd9,0x4f,0x3c,0x7c,0xa4,
  31571. 0x95,0xe0,0x56,0xa9,0xb4,0x7a,0xcd,0xb7,
  31572. 0x51,0xfb,0x73,0xe6,0x66,0xc6,0xc6,0x55,
  31573. 0xad,0xe8,0x29,0x72,0x97,0xd0,0x7a,0xd1,
  31574. 0xba,0x5e,0x43,0xf1,0xbc,0xa3,0x23,0x01,
  31575. 0x65,0x13,0x39,0xe2,0x29,0x04,0xcc,0x8c,
  31576. 0x42,0xf5,0x8c,0x30,0xc0,0x4a,0xaf,0xdb,
  31577. 0x03,0x8d,0xda,0x08,0x47,0xdd,0x98,0x8d,
  31578. 0xcd,0xa6,0xf3,0xbf,0xd1,0x5c,0x4b,0x4c,
  31579. 0x45,0x25,0x00,0x4a,0xa0,0x6e,0xef,0xf8,
  31580. 0xca,0x61,0x78,0x3a,0xac,0xec,0x57,0xfb,
  31581. 0x3d,0x1f,0x92,0xb0,0xfe,0x2f,0xd1,0xa8,
  31582. 0x5f,0x67,0x24,0x51,0x7b,0x65,0xe6,0x14,
  31583. 0xad,0x68,0x08,0xd6,0xf6,0xee,0x34,0xdf,
  31584. 0xf7,0x31,0x0f,0xdc,0x82,0xae,0xbf,0xd9,
  31585. 0x04,0xb0,0x1e,0x1d,0xc5,0x4b,0x29,0x27,
  31586. 0x09,0x4b,0x2d,0xb6,0x8d,0x6f,0x90,0x3b,
  31587. 0x68,0x40,0x1a,0xde,0xbf,0x5a,0x7e,0x08,
  31588. 0xd7,0x8f,0xf4,0xef,0x5d,0x63,0x65,0x3a,
  31589. 0x65,0x04,0x0c,0xf9,0xbf,0xd4,0xac,0xa7,
  31590. 0x98,0x4a,0x74,0xd3,0x71,0x45,0x98,0x67,
  31591. 0x80,0xfc,0x0b,0x16,0xac,0x45,0x16,0x49,
  31592. 0xde,0x61,0x88,0xa7,0xdb,0xdf,0x19,0x1f,
  31593. 0x64,0xb5,0xfc,0x5e,0x2a,0xb4,0x7b,0x57,
  31594. 0xf7,0xf7,0x27,0x6c,0xd4,0x19,0xc1,0x7a,
  31595. 0x3c,0xa8,0xe1,0xb9,0x39,0xae,0x49,0xe4,
  31596. 0x88,0xac,0xba,0x6b,0x96,0x56,0x10,0xb5,
  31597. 0x48,0x01,0x09,0xc8,0xb1,0x7b,0x80,0xe1,
  31598. 0xb7,0xb7,0x50,0xdf,0xc7,0x59,0x8d,0x5d,
  31599. 0x50,0x11,0xfd,0x2d,0xcc,0x56,0x00,0xa3,
  31600. 0x2e,0xf5,0xb5,0x2a,0x1e,0xcc,0x82,0x0e,
  31601. 0x30,0x8a,0xa3,0x42,0x72,0x1a,0xac,0x09,
  31602. 0x43,0xbf,0x66,0x86,0xb6,0x4b,0x25,0x79,
  31603. 0x37,0x65,0x04,0xcc,0xc4,0x93,0xd9,0x7e,
  31604. 0x6a,0xed,0x3f,0xb0,0xf9,0xcd,0x71,0xa4,
  31605. 0x3d,0xd4,0x97,0xf0,0x1f,0x17,0xc0,0xe2,
  31606. 0xcb,0x37,0x97,0xaa,0x2a,0x2f,0x25,0x66,
  31607. 0x56,0x16,0x8e,0x6c,0x49,0x6a,0xfc,0x5f,
  31608. 0xb9,0x32,0x46,0xf6,0xb1,0x11,0x63,0x98,
  31609. 0xa3,0x46,0xf1,0xa6,0x41,0xf3,0xb0,0x41,
  31610. 0xe9,0x89,0xf7,0x91,0x4f,0x90,0xcc,0x2c,
  31611. 0x7f,0xff,0x35,0x78,0x76,0xe5,0x06,0xb5,
  31612. 0x0d,0x33,0x4b,0xa7,0x7c,0x22,0x5b,0xc3,
  31613. 0x07,0xba,0x53,0x71,0x52,0xf3,0xf1,0x61,
  31614. 0x0e,0x4e,0xaf,0xe5,0x95,0xf6,0xd9,0xd9,
  31615. 0x0d,0x11,0xfa,0xa9,0x33,0xa1,0x5e,0xf1,
  31616. 0x36,0x95,0x46,0x86,0x8a,0x7f,0x3a,0x45,
  31617. 0xa9,0x67,0x68,0xd4,0x0f,0xd9,0xd0,0x34,
  31618. 0x12,0xc0,0x91,0xc6,0x31,0x5c,0xf4,0xfd,
  31619. 0xe7,0xcb,0x68,0x60,0x69,0x37,0x38,0x0d,
  31620. 0xb2,0xea,0xaa,0x70,0x7b,0x4c,0x41,0x85,
  31621. 0xc3,0x2e,0xdd,0xcd,0xd3,0x06,0x70,0x5e,
  31622. 0x4d,0xc1,0xff,0xc8,0x72,0xee,0xee,0x47,
  31623. 0x5a,0x64,0xdf,0xac,0x86,0xab,0xa4,0x1c,
  31624. 0x06,0x18,0x98,0x3f,0x87,0x41,0xc5,0xef,
  31625. 0x68,0xd3,0xa1,0x01,0xe8,0xa3,0xb8,0xca,
  31626. 0xc6,0x0c,0x90,0x5c,0x15,0xfc,0x91,0x08,
  31627. 0x40,0xb9,0x4c,0x00,0xa0,0xb9,0xd0
  31628. };
  31629. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  31630. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  31631. sizeof(msg2),
  31632. sizeof(msg3),
  31633. 0 /*sizeof(msg1)*/,
  31634. 0 /*sizeof(msg1)*/,
  31635. sizeof(msg4)
  31636. };
  31637. #ifndef NO_ASN
  31638. static const byte privateEd25519[] = {
  31639. 0x30,0x2e,0x02,0x01,0x00,0x30,0x05,0x06,
  31640. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  31641. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  31642. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  31643. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  31644. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  31645. };
  31646. static const byte badPrivateEd25519[] = {
  31647. 0x30,0x52,0x02,0x01,0x00,0x30,0x05,0x06,
  31648. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  31649. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  31650. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  31651. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  31652. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  31653. 0xa1,0x22,0x04,0x21,0xd7,0x5a,0x98,0x01, /* octet len 0x20 -> 0x21 */
  31654. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  31655. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  31656. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  31657. 0xf7,0x07,0x51,0x1a,
  31658. 0x00 /* add additional bytes to make the pubkey bigger */
  31659. };
  31660. static const byte publicEd25519[] = {
  31661. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  31662. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  31663. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  31664. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  31665. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  31666. 0xf7,0x07,0x51,0x1a
  31667. };
  31668. /* size has been altered to catch if sanity check is done */
  31669. static const byte badPublicEd25519[] = {
  31670. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  31671. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  31672. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  31673. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  31674. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  31675. 0xf7,0x07,0x51,0x1a,
  31676. 0x00 /* add an additional byte to make the pubkey appear bigger */
  31677. };
  31678. static const byte privPubEd25519[] = {
  31679. 0x30,0x50,0x02,0x01,0x00,0x30,0x05,0x06,
  31680. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  31681. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  31682. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  31683. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  31684. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  31685. 0x81,0x20,0xd7,0x5a,0x98,0x01,0x82,0xb1,
  31686. 0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,0xc9,0x64,
  31687. 0x07,0x3a,0x0e,0xe1,0x72,0xf3,0xda,0xa6,
  31688. 0x23,0x25,0xaf,0x02,0x1a,0x68,0xf7,0x07,
  31689. 0x51,0x1a
  31690. };
  31691. word32 idx;
  31692. #endif /* NO_ASN */
  31693. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  31694. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  31695. ed25519_key key3;
  31696. #endif
  31697. WOLFSSL_ENTER("ed25519_test");
  31698. /* create ed25519 keys */
  31699. #ifndef HAVE_FIPS
  31700. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  31701. #else
  31702. ret = wc_InitRng(&rng);
  31703. #endif
  31704. if (ret != 0)
  31705. return WC_TEST_RET_ENC_EC(ret);
  31706. wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  31707. wc_ed25519_init_ex(&key2, HEAP_HINT, devId);
  31708. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  31709. wc_ed25519_init_ex(&key3, HEAP_HINT, devId);
  31710. #endif
  31711. #ifdef HAVE_ED25519_MAKE_KEY
  31712. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  31713. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key2);
  31714. #endif
  31715. /* helper functions for signature and key size */
  31716. keySz = (word32)wc_ed25519_size(&key);
  31717. sigSz = (word32)wc_ed25519_sig_size(&key);
  31718. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  31719. defined(HAVE_ED25519_KEY_IMPORT)
  31720. for (i = 0; i < 6; i++) {
  31721. outlen = sizeof(out);
  31722. XMEMSET(out, 0, sizeof(out));
  31723. if (wc_ed25519_import_private_key(sKeys[i], ED25519_KEY_SIZE, pKeys[i],
  31724. pKeySz[i], &key) != 0)
  31725. return WC_TEST_RET_ENC_I(i);
  31726. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key) != 0)
  31727. return WC_TEST_RET_ENC_I(i);
  31728. if (XMEMCMP(out, sigs[i], 64))
  31729. return WC_TEST_RET_ENC_I(i);
  31730. #if defined(HAVE_ED25519_VERIFY)
  31731. /* test verify on good msg */
  31732. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  31733. &key) != 0 || verify != 1)
  31734. return WC_TEST_RET_ENC_I(i);
  31735. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  31736. /* test verify on good msg using streaming interface directly */
  31737. if (wc_ed25519_verify_msg_init(out, outlen,
  31738. &key, (byte)Ed25519, NULL, 0) != 0)
  31739. return WC_TEST_RET_ENC_I(i);
  31740. for (j = 0; j < msgSz[i]; j += i) {
  31741. if (wc_ed25519_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), &key) != 0)
  31742. return WC_TEST_RET_ENC_I(i);
  31743. }
  31744. if (wc_ed25519_verify_msg_final(out, outlen, &verify,
  31745. &key) != 0 || verify != 1)
  31746. return WC_TEST_RET_ENC_I(i);
  31747. #endif /* WOLFSSL_ED25519_STREAMING_VERIFY */
  31748. /* test verify on bad msg */
  31749. out[outlen-1] = out[outlen-1] + 1;
  31750. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  31751. &key) == 0 || verify == 1)
  31752. return WC_TEST_RET_ENC_I(i);
  31753. #endif /* HAVE_ED25519_VERIFY */
  31754. /* test api for import/exporting keys */
  31755. exportPSz = sizeof(exportPKey);
  31756. exportSSz = sizeof(exportSKey);
  31757. if (wc_ed25519_export_public(&key, exportPKey, &exportPSz) != 0)
  31758. return WC_TEST_RET_ENC_I(i);
  31759. if (wc_ed25519_import_public_ex(exportPKey, exportPSz, &key2, 1) != 0)
  31760. return WC_TEST_RET_ENC_I(i);
  31761. if (wc_ed25519_export_private_only(&key, exportSKey, &exportSSz) != 0)
  31762. return WC_TEST_RET_ENC_I(i);
  31763. if (wc_ed25519_import_private_key(exportSKey, exportSSz,
  31764. exportPKey, exportPSz, &key2) != 0)
  31765. return WC_TEST_RET_ENC_I(i);
  31766. /* clear "out" buffer and test sign with imported keys */
  31767. outlen = sizeof(out);
  31768. XMEMSET(out, 0, sizeof(out));
  31769. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2) != 0)
  31770. return WC_TEST_RET_ENC_I(i);
  31771. #if defined(HAVE_ED25519_VERIFY)
  31772. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  31773. &key2) != 0 || verify != 1)
  31774. return WC_TEST_RET_ENC_I(i);
  31775. if (XMEMCMP(out, sigs[i], 64))
  31776. return WC_TEST_RET_ENC_I(i);
  31777. #endif /* HAVE_ED25519_VERIFY */
  31778. }
  31779. {
  31780. /* Run tests for some rare code paths */
  31781. /* sig is exactly equal to the order */
  31782. static const byte rareEd1[] = {
  31783. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31784. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31785. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31786. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31787. 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
  31788. 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
  31789. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31790. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10
  31791. };
  31792. /* sig is larger than the order before we get to the low part */
  31793. static const byte rareEd2[] = {
  31794. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31795. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31796. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31797. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31798. 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
  31799. 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
  31800. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31801. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x10
  31802. };
  31803. /* sig is larger than the order in the low part */
  31804. static const byte rareEd3[] = {
  31805. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31806. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31807. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31808. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31809. 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
  31810. 0xd6, 0x9c, 0xf9, 0xa2, 0xde, 0xf9, 0xde, 0x14,
  31811. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31812. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10
  31813. };
  31814. /* sig is smaller than the order */
  31815. static const byte rareEd4[] = {
  31816. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31817. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31818. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31819. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31820. 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
  31821. 0xd6, 0x9c, 0xf1, 0xa2, 0xde, 0xf9, 0xde, 0x14,
  31822. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31823. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10
  31824. };
  31825. ret = wc_ed25519_import_private_key(sKeys[0], ED25519_KEY_SIZE,
  31826. pKeys[0], pKeySz[0], &key);
  31827. if (ret != 0)
  31828. return ret;
  31829. ret = wc_ed25519_verify_msg(rareEd1, sizeof(rareEd1), msgs[0], msgSz[0],
  31830. &verify, &key);
  31831. if (ret != BAD_FUNC_ARG)
  31832. return ret;
  31833. ret = wc_ed25519_verify_msg(rareEd2, sizeof(rareEd2), msgs[0], msgSz[0],
  31834. &verify, &key);
  31835. if (ret != BAD_FUNC_ARG)
  31836. return ret;
  31837. ret = wc_ed25519_verify_msg(rareEd3, sizeof(rareEd3), msgs[0], msgSz[0],
  31838. &verify, &key);
  31839. if (ret != BAD_FUNC_ARG)
  31840. return ret;
  31841. ret = wc_ed25519_verify_msg(rareEd4, sizeof(rareEd4), msgs[0], msgSz[0],
  31842. &verify, &key);
  31843. if (ret != SIG_VERIFY_E)
  31844. return ret;
  31845. }
  31846. ret = ed25519ctx_test();
  31847. if (ret != 0)
  31848. return ret;
  31849. ret = ed25519ph_test();
  31850. if (ret != 0)
  31851. return ret;
  31852. #ifndef NO_ASN
  31853. /* Try ASN.1 encoded private-only key and public key. */
  31854. idx = 0;
  31855. ret = wc_Ed25519PrivateKeyDecode(privateEd25519, &idx, &key3,
  31856. sizeof(privateEd25519));
  31857. if (ret != 0)
  31858. return WC_TEST_RET_ENC_EC(ret);
  31859. idx = 0;
  31860. if (wc_Ed25519PrivateKeyDecode(badPrivateEd25519, &idx, &key3,
  31861. sizeof(badPrivateEd25519)) == 0)
  31862. return WC_TEST_RET_ENC_NC;
  31863. ret = wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3);
  31864. if (ret != BAD_FUNC_ARG)
  31865. return WC_TEST_RET_ENC_EC(ret);
  31866. /* try with a buffer size that is too large */
  31867. idx = 0;
  31868. if (wc_Ed25519PublicKeyDecode(badPublicEd25519, &idx, &key3,
  31869. sizeof(badPublicEd25519)) == 0)
  31870. return WC_TEST_RET_ENC_NC;
  31871. idx = 0;
  31872. ret = wc_Ed25519PublicKeyDecode(publicEd25519, &idx, &key3,
  31873. sizeof(publicEd25519));
  31874. if (ret != 0)
  31875. return WC_TEST_RET_ENC_EC(ret);
  31876. ret = wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3);
  31877. if (ret != 0)
  31878. return WC_TEST_RET_ENC_EC(ret);
  31879. if (XMEMCMP(out, sigs[0], 64))
  31880. return WC_TEST_RET_ENC_NC;
  31881. #if defined(HAVE_ED25519_VERIFY)
  31882. /* test verify on good msg */
  31883. ret = wc_ed25519_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3);
  31884. if (ret != 0 || verify != 1)
  31885. return WC_TEST_RET_ENC_EC(ret);
  31886. #endif /* HAVE_ED25519_VERIFY */
  31887. wc_ed25519_free(&key3);
  31888. wc_ed25519_init(&key3);
  31889. idx = 0;
  31890. ret = wc_Ed25519PrivateKeyDecode(privPubEd25519, &idx, &key3,
  31891. sizeof(privPubEd25519));
  31892. if (ret != 0)
  31893. return WC_TEST_RET_ENC_EC(ret);
  31894. ret = wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3);
  31895. if (ret != 0)
  31896. return WC_TEST_RET_ENC_EC(ret);
  31897. if (XMEMCMP(out, sigs[0], 64))
  31898. return WC_TEST_RET_ENC_NC;
  31899. wc_ed25519_free(&key3);
  31900. #endif /* NO_ASN */
  31901. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  31902. /* clean up keys when done */
  31903. wc_ed25519_free(&key);
  31904. wc_ed25519_free(&key2);
  31905. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  31906. wc_FreeRng(&rng);
  31907. #endif
  31908. /* hush warnings of unused keySz and sigSz */
  31909. (void)keySz;
  31910. (void)sigSz;
  31911. ret = ed25519_test_check_key();
  31912. if (ret < 0)
  31913. return ret;
  31914. #ifdef WOLFSSL_TEST_CERT
  31915. ret = ed25519_test_cert();
  31916. if (ret < 0)
  31917. return ret;
  31918. #if defined(WOLFSSL_CERT_GEN) && defined(HAVE_ED25519_MAKE_KEY)
  31919. ret = ed25519_test_make_cert();
  31920. if (ret < 0)
  31921. return ret;
  31922. #endif /* WOLFSSL_CERT_GEN */
  31923. #endif /* WOLFSSL_TEST_CERT */
  31924. return 0;
  31925. }
  31926. #endif /* HAVE_ED25519 */
  31927. #ifdef HAVE_CURVE448
  31928. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  31929. defined(HAVE_CURVE448_KEY_IMPORT)
  31930. /* Test the wc_curve448_check_public API.
  31931. *
  31932. * returns 0 on success and -ve on failure.
  31933. */
  31934. static wc_test_ret_t curve448_check_public_test(void)
  31935. {
  31936. /* Little-endian values that will fail */
  31937. byte fail_le[][CURVE448_KEY_SIZE] = {
  31938. {
  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,0x00,
  31945. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  31946. },
  31947. {
  31948. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31949. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31950. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31951. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31952. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31953. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31954. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  31955. },
  31956. };
  31957. /* Big-endian values that will fail */
  31958. byte fail_be[][CURVE448_KEY_SIZE] = {
  31959. {
  31960. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31961. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31962. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31963. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31964. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31965. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31966. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  31967. },
  31968. {
  31969. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31970. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31971. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31972. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31973. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31974. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31975. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  31976. },
  31977. };
  31978. /* Good or valid public value */
  31979. byte good[CURVE448_KEY_SIZE] = {
  31980. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31981. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31982. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31983. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31984. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31985. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31986. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  31987. };
  31988. int i;
  31989. wc_test_ret_t ret;
  31990. /* Parameter checks */
  31991. /* NULL pointer */
  31992. ret = wc_curve448_check_public(NULL, 0, EC448_LITTLE_ENDIAN);
  31993. if (ret != BAD_FUNC_ARG)
  31994. return WC_TEST_RET_ENC_EC(ret);
  31995. ret = wc_curve448_check_public(NULL, 0, EC448_BIG_ENDIAN);
  31996. if (ret != BAD_FUNC_ARG)
  31997. return WC_TEST_RET_ENC_EC(ret);
  31998. /* Length of 0 treated differently to other invalid lengths for TLS */
  31999. ret = wc_curve448_check_public(good, 0, EC448_LITTLE_ENDIAN);
  32000. if (ret != BUFFER_E)
  32001. return WC_TEST_RET_ENC_EC(ret);
  32002. ret = wc_curve448_check_public(good, 0, EC448_BIG_ENDIAN);
  32003. if (ret != BUFFER_E)
  32004. return WC_TEST_RET_ENC_EC(ret);
  32005. /* Length not CURVE448_KEY_SIZE */
  32006. for (i = 1; i < CURVE448_KEY_SIZE + 2; i++) {
  32007. if (i == CURVE448_KEY_SIZE)
  32008. continue;
  32009. if (wc_curve448_check_public(good, (word32)i, EC448_LITTLE_ENDIAN) !=
  32010. ECC_BAD_ARG_E) {
  32011. return WC_TEST_RET_ENC_I(i);
  32012. }
  32013. if (wc_curve448_check_public(good, (word32)i, EC448_BIG_ENDIAN) !=
  32014. ECC_BAD_ARG_E) {
  32015. return WC_TEST_RET_ENC_I(i);
  32016. }
  32017. }
  32018. /* Little-endian fail cases */
  32019. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  32020. if (wc_curve448_check_public(fail_le[i], CURVE448_KEY_SIZE,
  32021. EC448_LITTLE_ENDIAN) == 0) {
  32022. return WC_TEST_RET_ENC_I(i);
  32023. }
  32024. }
  32025. /* Big-endian fail cases */
  32026. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  32027. if (wc_curve448_check_public(fail_be[i], CURVE448_KEY_SIZE,
  32028. EC448_BIG_ENDIAN) == 0) {
  32029. return WC_TEST_RET_ENC_I(i);
  32030. }
  32031. }
  32032. /* Check a valid public value works! */
  32033. ret = wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  32034. EC448_LITTLE_ENDIAN);
  32035. if (ret != 0)
  32036. return WC_TEST_RET_ENC_EC(ret);
  32037. ret = wc_curve448_check_public(good, CURVE448_KEY_SIZE, EC448_BIG_ENDIAN);
  32038. if (ret != 0)
  32039. return WC_TEST_RET_ENC_EC(ret);
  32040. return 0;
  32041. }
  32042. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  32043. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve448_test(void)
  32044. {
  32045. WC_RNG rng;
  32046. wc_test_ret_t ret;
  32047. #ifdef HAVE_CURVE448_SHARED_SECRET
  32048. byte sharedA[CURVE448_KEY_SIZE];
  32049. byte sharedB[CURVE448_KEY_SIZE];
  32050. word32 y;
  32051. #endif
  32052. #ifdef HAVE_CURVE448_KEY_EXPORT
  32053. byte exportBuf[CURVE448_KEY_SIZE];
  32054. #endif
  32055. word32 x = 0;
  32056. curve448_key userA, userB, pubKey;
  32057. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  32058. defined(HAVE_CURVE448_KEY_IMPORT)
  32059. /* test vectors from
  32060. https://www.rfc-editor.org/rfc/rfc7748.html
  32061. */
  32062. /* secret key for party a */
  32063. byte sa[] = {
  32064. 0x6b, 0x72, 0x98, 0xa5, 0xc0, 0xd8, 0xc2, 0x9a,
  32065. 0x1d, 0xab, 0x27, 0xf1, 0xa6, 0x82, 0x63, 0x00,
  32066. 0x91, 0x73, 0x89, 0x44, 0x97, 0x41, 0xa9, 0x74,
  32067. 0xf5, 0xba, 0xc9, 0xd9, 0x8d, 0xc2, 0x98, 0xd4,
  32068. 0x65, 0x55, 0xbc, 0xe8, 0xba, 0xe8, 0x9e, 0xee,
  32069. 0xd4, 0x00, 0x58, 0x4b, 0xb0, 0x46, 0xcf, 0x75,
  32070. 0x57, 0x9f, 0x51, 0xd1, 0x25, 0x49, 0x8f, 0x9a,
  32071. };
  32072. /* public key for party a */
  32073. byte pa[] = {
  32074. 0xa0, 0x1f, 0xc4, 0x32, 0xe5, 0x80, 0x7f, 0x17,
  32075. 0x53, 0x0d, 0x12, 0x88, 0xda, 0x12, 0x5b, 0x0c,
  32076. 0xd4, 0x53, 0xd9, 0x41, 0x72, 0x64, 0x36, 0xc8,
  32077. 0xbb, 0xd9, 0xc5, 0x22, 0x2c, 0x3d, 0xa7, 0xfa,
  32078. 0x63, 0x9c, 0xe0, 0x3d, 0xb8, 0xd2, 0x3b, 0x27,
  32079. 0x4a, 0x07, 0x21, 0xa1, 0xae, 0xd5, 0x22, 0x7d,
  32080. 0xe6, 0xe3, 0xb7, 0x31, 0xcc, 0xf7, 0x08, 0x9b,
  32081. };
  32082. /* secret key for party b */
  32083. byte sb[] = {
  32084. 0x2d, 0x99, 0x73, 0x51, 0xb6, 0x10, 0x6f, 0x36,
  32085. 0xb0, 0xd1, 0x09, 0x1b, 0x92, 0x9c, 0x4c, 0x37,
  32086. 0x21, 0x3e, 0x0d, 0x2b, 0x97, 0xe8, 0x5e, 0xbb,
  32087. 0x20, 0xc1, 0x27, 0x69, 0x1d, 0x0d, 0xad, 0x8f,
  32088. 0x1d, 0x81, 0x75, 0xb0, 0x72, 0x37, 0x45, 0xe6,
  32089. 0x39, 0xa3, 0xcb, 0x70, 0x44, 0x29, 0x0b, 0x99,
  32090. 0xe0, 0xe2, 0xa0, 0xc2, 0x7a, 0x6a, 0x30, 0x1c,
  32091. };
  32092. /* public key for party b */
  32093. byte pb[] = {
  32094. 0x09, 0x36, 0xf3, 0x7b, 0xc6, 0xc1, 0xbd, 0x07,
  32095. 0xae, 0x3d, 0xec, 0x7a, 0xb5, 0xdc, 0x06, 0xa7,
  32096. 0x3c, 0xa1, 0x32, 0x42, 0xfb, 0x34, 0x3e, 0xfc,
  32097. 0x72, 0xb9, 0xd8, 0x27, 0x30, 0xb4, 0x45, 0xf3,
  32098. 0xd4, 0xb0, 0xbd, 0x07, 0x71, 0x62, 0xa4, 0x6d,
  32099. 0xcf, 0xec, 0x6f, 0x9b, 0x59, 0x0b, 0xfc, 0xbc,
  32100. 0xf5, 0x20, 0xcd, 0xb0, 0x29, 0xa8, 0xb7, 0x3e,
  32101. };
  32102. /* expected shared key */
  32103. byte ss[] = {
  32104. 0x9d, 0x87, 0x4a, 0x51, 0x37, 0x50, 0x9a, 0x44,
  32105. 0x9a, 0xd5, 0x85, 0x30, 0x40, 0x24, 0x1c, 0x52,
  32106. 0x36, 0x39, 0x54, 0x35, 0xc3, 0x64, 0x24, 0xfd,
  32107. 0x56, 0x0b, 0x0c, 0xb6, 0x2b, 0x28, 0x1d, 0x28,
  32108. 0x52, 0x75, 0xa7, 0x40, 0xce, 0x32, 0xa2, 0x2d,
  32109. 0xd1, 0x74, 0x0f, 0x4a, 0xa9, 0x16, 0x1c, 0xec,
  32110. 0x95, 0xcc, 0xc6, 0x1a, 0x18, 0xf4, 0xff, 0x07,
  32111. };
  32112. #endif /* HAVE_CURVE448_SHARED_SECRET */
  32113. (void)x;
  32114. WOLFSSL_ENTER("curve448_test");
  32115. #ifndef HAVE_FIPS
  32116. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  32117. #else
  32118. ret = wc_InitRng(&rng);
  32119. #endif
  32120. if (ret != 0)
  32121. return WC_TEST_RET_ENC_EC(ret);
  32122. wc_curve448_init(&userA);
  32123. wc_curve448_init(&userB);
  32124. wc_curve448_init(&pubKey);
  32125. /* make curve448 keys */
  32126. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userA);
  32127. if (ret != 0)
  32128. return WC_TEST_RET_ENC_EC(ret);
  32129. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userB);
  32130. if (ret != 0)
  32131. return WC_TEST_RET_ENC_EC(ret);
  32132. #ifdef HAVE_CURVE448_SHARED_SECRET
  32133. /* find shared secret key */
  32134. x = sizeof(sharedA);
  32135. ret = wc_curve448_shared_secret(&userA, &userB, sharedA, &x);
  32136. if (ret != 0)
  32137. return WC_TEST_RET_ENC_EC(ret);
  32138. y = sizeof(sharedB);
  32139. ret = wc_curve448_shared_secret(&userB, &userA, sharedB, &y);
  32140. if (ret != 0)
  32141. return WC_TEST_RET_ENC_EC(ret);
  32142. /* compare shared secret keys to test they are the same */
  32143. if (y != x)
  32144. return WC_TEST_RET_ENC_NC;
  32145. if (XMEMCMP(sharedA, sharedB, x))
  32146. return WC_TEST_RET_ENC_NC;
  32147. #endif
  32148. #ifdef HAVE_CURVE448_KEY_EXPORT
  32149. /* export a public key and import it for another user */
  32150. x = sizeof(exportBuf);
  32151. ret = wc_curve448_export_public(&userA, exportBuf, &x);
  32152. if (ret != 0)
  32153. return WC_TEST_RET_ENC_EC(ret);
  32154. #ifdef HAVE_CURVE448_KEY_IMPORT
  32155. ret = wc_curve448_import_public(exportBuf, x, &pubKey);
  32156. if (ret != 0)
  32157. return WC_TEST_RET_ENC_EC(ret);
  32158. #endif
  32159. #endif
  32160. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  32161. defined(HAVE_CURVE448_KEY_IMPORT)
  32162. /* test shared key after importing a public key */
  32163. XMEMSET(sharedB, 0, sizeof(sharedB));
  32164. y = sizeof(sharedB);
  32165. ret = wc_curve448_shared_secret(&userB, &pubKey, sharedB, &y);
  32166. if (ret != 0)
  32167. return WC_TEST_RET_ENC_EC(ret);
  32168. if (XMEMCMP(sharedA, sharedB, y))
  32169. return WC_TEST_RET_ENC_NC;
  32170. /* import RFC test vectors and compare shared key */
  32171. ret = wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA);
  32172. if (ret != 0)
  32173. return WC_TEST_RET_ENC_EC(ret);
  32174. ret = wc_curve448_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB);
  32175. if (ret != 0)
  32176. return WC_TEST_RET_ENC_EC(ret);
  32177. /* test against known test vector */
  32178. XMEMSET(sharedB, 0, sizeof(sharedB));
  32179. y = sizeof(sharedB);
  32180. ret = wc_curve448_shared_secret(&userA, &userB, sharedB, &y);
  32181. if (ret != 0)
  32182. return WC_TEST_RET_ENC_EC(ret);
  32183. if (XMEMCMP(ss, sharedB, y))
  32184. return WC_TEST_RET_ENC_NC;
  32185. /* test swapping roles of keys and generating same shared key */
  32186. XMEMSET(sharedB, 0, sizeof(sharedB));
  32187. y = sizeof(sharedB);
  32188. ret = wc_curve448_shared_secret(&userB, &userA, sharedB, &y);
  32189. if (ret != 0)
  32190. return WC_TEST_RET_ENC_EC(ret);
  32191. if (XMEMCMP(ss, sharedB, y))
  32192. return WC_TEST_RET_ENC_NC;
  32193. /* test with 1 generated key and 1 from known test vector */
  32194. ret = wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA);
  32195. if (ret != 0)
  32196. return WC_TEST_RET_ENC_EC(ret);
  32197. ret = wc_curve448_make_key(&rng, 56, &userB);
  32198. if (ret != 0)
  32199. return WC_TEST_RET_ENC_EC(ret);
  32200. x = sizeof(sharedA);
  32201. ret = wc_curve448_shared_secret(&userA, &userB, sharedA, &x);
  32202. if (ret != 0)
  32203. return WC_TEST_RET_ENC_EC(ret);
  32204. y = sizeof(sharedB);
  32205. ret = wc_curve448_shared_secret(&userB, &userA, sharedB, &y);
  32206. if (ret != 0)
  32207. return WC_TEST_RET_ENC_EC(ret);
  32208. /* compare shared secret keys to test they are the same */
  32209. if (y != x)
  32210. return WC_TEST_RET_ENC_NC;
  32211. if (XMEMCMP(sharedA, sharedB, x))
  32212. return WC_TEST_RET_ENC_NC;
  32213. ret = curve448_check_public_test();
  32214. if (ret != 0)
  32215. return ret;
  32216. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  32217. /* clean up keys when done */
  32218. wc_curve448_free(&pubKey);
  32219. wc_curve448_free(&userB);
  32220. wc_curve448_free(&userA);
  32221. wc_FreeRng(&rng);
  32222. return 0;
  32223. }
  32224. #endif /* HAVE_CURVE448 */
  32225. #ifdef HAVE_ED448
  32226. #ifdef WOLFSSL_TEST_CERT
  32227. static wc_test_ret_t ed448_test_cert(void)
  32228. {
  32229. DecodedCert cert[2];
  32230. DecodedCert* serverCert = NULL;
  32231. DecodedCert* caCert = NULL;
  32232. #ifdef HAVE_ED448_VERIFY
  32233. ed448_key key;
  32234. ed448_key* pubKey = NULL;
  32235. int verify;
  32236. #endif /* HAVE_ED448_VERIFY */
  32237. wc_test_ret_t ret;
  32238. byte* tmp;
  32239. size_t bytes;
  32240. XFILE file;
  32241. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32242. if (tmp == NULL) {
  32243. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  32244. }
  32245. #ifdef USE_CERT_BUFFERS_256
  32246. XMEMCPY(tmp, ca_ed448_cert, sizeof_ca_ed448_cert);
  32247. bytes = sizeof_ca_ed448_cert;
  32248. #elif !defined(NO_FILESYSTEM)
  32249. file = XFOPEN(caEd448Cert, "rb");
  32250. if (file == NULL) {
  32251. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  32252. }
  32253. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  32254. XFCLOSE(file);
  32255. if (bytes == 0)
  32256. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  32257. #else
  32258. /* No certificate to use. */
  32259. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  32260. #endif
  32261. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  32262. caCert = &cert[0];
  32263. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  32264. if (ret != 0)
  32265. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32266. #ifdef USE_CERT_BUFFERS_256
  32267. XMEMCPY(tmp, server_ed448_cert, sizeof_server_ed448_cert);
  32268. bytes = sizeof_server_ed448_cert;
  32269. #elif !defined(NO_FILESYSTEM)
  32270. file = XFOPEN(serverEd448Cert, "rb");
  32271. if (file == NULL) {
  32272. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  32273. }
  32274. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  32275. XFCLOSE(file);
  32276. if (bytes == 0)
  32277. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  32278. #else
  32279. /* No certificate to use. */
  32280. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  32281. #endif
  32282. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  32283. serverCert = &cert[1];
  32284. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  32285. if (ret != 0)
  32286. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32287. #ifdef HAVE_ED448_VERIFY
  32288. ret = wc_ed448_init(&key);
  32289. if (ret < 0)
  32290. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32291. pubKey = &key;
  32292. ret = wc_ed448_import_public(caCert->publicKey, caCert->pubKeySize, pubKey);
  32293. if (ret < 0)
  32294. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32295. ret = wc_ed448_verify_msg(serverCert->signature, serverCert->sigLength,
  32296. serverCert->source + serverCert->certBegin,
  32297. serverCert->sigIndex - serverCert->certBegin,
  32298. &verify, pubKey, NULL, 0);
  32299. if (ret < 0 || verify != 1) {
  32300. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32301. }
  32302. #endif /* HAVE_ED448_VERIFY */
  32303. done:
  32304. if (tmp != NULL)
  32305. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32306. #ifdef HAVE_ED448_VERIFY
  32307. wc_ed448_free(pubKey);
  32308. #endif /* HAVE_ED448_VERIFY */
  32309. if (caCert != NULL)
  32310. FreeDecodedCert(caCert);
  32311. if (serverCert != NULL)
  32312. FreeDecodedCert(serverCert);
  32313. return ret;
  32314. }
  32315. static wc_test_ret_t ed448_test_make_cert(void)
  32316. {
  32317. WC_RNG rng;
  32318. Cert cert;
  32319. DecodedCert decode;
  32320. ed448_key key;
  32321. ed448_key* privKey = NULL;
  32322. wc_test_ret_t ret = 0;
  32323. byte* tmp = NULL;
  32324. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  32325. #ifndef HAVE_FIPS
  32326. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  32327. #else
  32328. ret = wc_InitRng(&rng);
  32329. #endif
  32330. if (ret != 0)
  32331. return WC_TEST_RET_ENC_EC(ret);
  32332. wc_ed448_init(&key);
  32333. privKey = &key;
  32334. wc_ed448_make_key(&rng, ED448_KEY_SIZE, privKey);
  32335. cert.daysValid = 365 * 2;
  32336. cert.selfSigned = 1;
  32337. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  32338. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  32339. cert.isCA = 0;
  32340. #ifdef WOLFSSL_CERT_EXT
  32341. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  32342. if (ret < 0)
  32343. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32344. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  32345. if (ret < 0)
  32346. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32347. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  32348. if (ret < 0)
  32349. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32350. #endif
  32351. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32352. if (tmp == NULL) {
  32353. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  32354. }
  32355. cert.sigType = CTC_ED448;
  32356. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED448_TYPE, privKey, &rng);
  32357. if (ret < 0)
  32358. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32359. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF, ED448_TYPE,
  32360. privKey, &rng);
  32361. if (ret < 0)
  32362. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32363. InitDecodedCert(&decode, tmp, (word32)ret, HEAP_HINT);
  32364. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  32365. FreeDecodedCert(&decode);
  32366. if (ret != 0)
  32367. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32368. done:
  32369. if (tmp != NULL)
  32370. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32371. wc_ed448_free(privKey);
  32372. wc_FreeRng(&rng);
  32373. return ret;
  32374. }
  32375. #endif /* WOLFSSL_TEST_CERT */
  32376. #if defined(HAVE_ED448_KEY_IMPORT)
  32377. static wc_test_ret_t ed448_test_check_key(void)
  32378. {
  32379. /* Fails to find x-ordinate from this y-ordinate. */
  32380. WOLFSSL_SMALL_STACK_STATIC const byte key_bad_y[] = {
  32381. 0x40,
  32382. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32383. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32384. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32385. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32386. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32387. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32388. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32389. 0x00
  32390. };
  32391. /* Y-ordinate value larger than prime. */
  32392. WOLFSSL_SMALL_STACK_STATIC const byte key_bad_y_max[] = {
  32393. 0x40,
  32394. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32395. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32396. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32397. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32398. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32399. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32400. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32401. 0xff
  32402. };
  32403. /* Y-ordinate value equal to prime. */
  32404. WOLFSSL_SMALL_STACK_STATIC const byte key_bad_y_is_p[] = {
  32405. 0x40,
  32406. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32407. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32408. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32409. 0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,
  32410. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32411. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32412. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32413. 0xff
  32414. };
  32415. /* Y-ordinate value equal to prime - 1. */
  32416. WOLFSSL_SMALL_STACK_STATIC const byte key_y_is_p_minus_1[] = {
  32417. 0x40,
  32418. 0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32419. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32420. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32421. 0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,
  32422. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32423. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32424. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32425. 0xff
  32426. };
  32427. ed448_key key;
  32428. int ret;
  32429. int res = 0;
  32430. /* Initialize key for use. */
  32431. ret = wc_ed448_init_ex(&key, HEAP_HINT, devId);
  32432. if (ret != 0) {
  32433. return WC_TEST_RET_ENC_NC;
  32434. }
  32435. /* Load bad public key only and perform checks. */
  32436. ret = wc_ed448_import_public(key_bad_y, ED448_PUB_KEY_SIZE + 1, &key);
  32437. if (ret != PUBLIC_KEY_E) {
  32438. res = WC_TEST_RET_ENC_NC;
  32439. }
  32440. if (ret == 0) {
  32441. /* Load bad public key only and perform checks. */
  32442. ret = wc_ed448_import_public(key_bad_y_max, ED448_PUB_KEY_SIZE + 1,
  32443. &key);
  32444. if (ret != PUBLIC_KEY_E) {
  32445. res = WC_TEST_RET_ENC_NC;
  32446. }
  32447. }
  32448. if (res == 0) {
  32449. /* Load bad public key only and perform checks. */
  32450. ret = wc_ed448_import_public(key_bad_y_is_p, ED448_PUB_KEY_SIZE + 1,
  32451. &key);
  32452. if (ret != PUBLIC_KEY_E) {
  32453. res = WC_TEST_RET_ENC_NC;
  32454. }
  32455. }
  32456. if (res == 0) {
  32457. /* Load good public key only and perform checks. */
  32458. ret = wc_ed448_import_public(key_y_is_p_minus_1, ED448_PUB_KEY_SIZE + 1,
  32459. &key);
  32460. if (ret != 0) {
  32461. res = WC_TEST_RET_ENC_NC;
  32462. }
  32463. }
  32464. /* Dispose of key. */
  32465. wc_ed448_free(&key);
  32466. return res;
  32467. }
  32468. #endif
  32469. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  32470. defined(HAVE_ED448_KEY_IMPORT)
  32471. static wc_test_ret_t ed448_ctx_test(void)
  32472. {
  32473. wc_test_ret_t ret;
  32474. byte out[ED448_SIG_SIZE];
  32475. word32 outlen;
  32476. #ifdef HAVE_ED448_VERIFY
  32477. int verify;
  32478. #endif /* HAVE_ED448_VERIFY */
  32479. ed448_key key;
  32480. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  32481. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  32482. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  32483. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  32484. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  32485. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  32486. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  32487. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  32488. 0x4e
  32489. };
  32490. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  32491. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  32492. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  32493. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  32494. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  32495. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  32496. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  32497. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  32498. 0x80
  32499. };
  32500. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx[] = {
  32501. 0xd4, 0xf8, 0xf6, 0x13, 0x17, 0x70, 0xdd, 0x46,
  32502. 0xf4, 0x08, 0x67, 0xd6, 0xfd, 0x5d, 0x50, 0x55,
  32503. 0xde, 0x43, 0x54, 0x1f, 0x8c, 0x5e, 0x35, 0xab,
  32504. 0xbc, 0xd0, 0x01, 0xb3, 0x2a, 0x89, 0xf7, 0xd2,
  32505. 0x15, 0x1f, 0x76, 0x47, 0xf1, 0x1d, 0x8c, 0xa2,
  32506. 0xae, 0x27, 0x9f, 0xb8, 0x42, 0xd6, 0x07, 0x21,
  32507. 0x7f, 0xce, 0x6e, 0x04, 0x2f, 0x68, 0x15, 0xea,
  32508. 0x00, 0x0c, 0x85, 0x74, 0x1d, 0xe5, 0xc8, 0xda,
  32509. 0x11, 0x44, 0xa6, 0xa1, 0xab, 0xa7, 0xf9, 0x6d,
  32510. 0xe4, 0x25, 0x05, 0xd7, 0xa7, 0x29, 0x85, 0x24,
  32511. 0xfd, 0xa5, 0x38, 0xfc, 0xcb, 0xbb, 0x75, 0x4f,
  32512. 0x57, 0x8c, 0x1c, 0xad, 0x10, 0xd5, 0x4d, 0x0d,
  32513. 0x54, 0x28, 0x40, 0x7e, 0x85, 0xdc, 0xbc, 0x98,
  32514. 0xa4, 0x91, 0x55, 0xc1, 0x37, 0x64, 0xe6, 0x6c,
  32515. 0x3c, 0x00
  32516. };
  32517. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  32518. 0x03
  32519. };
  32520. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  32521. 0x66,0x6f,0x6f
  32522. };
  32523. outlen = sizeof(out);
  32524. XMEMSET(out, 0, sizeof(out));
  32525. ret = wc_ed448_init_ex(&key, HEAP_HINT, devId);
  32526. if (ret != 0)
  32527. return WC_TEST_RET_ENC_EC(ret);
  32528. ret = wc_ed448_import_private_key(sKeyCtx, ED448_KEY_SIZE, pKeyCtx,
  32529. sizeof(pKeyCtx), &key);
  32530. if (ret != 0)
  32531. return WC_TEST_RET_ENC_EC(ret);
  32532. ret = wc_ed448_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  32533. contextCtx, sizeof(contextCtx));
  32534. if (ret != 0)
  32535. return WC_TEST_RET_ENC_EC(ret);
  32536. if (XMEMCMP(out, sigCtx, sizeof(sigCtx)))
  32537. return WC_TEST_RET_ENC_NC;
  32538. #if defined(HAVE_ED448_VERIFY)
  32539. /* test verify on good msg */
  32540. ret = wc_ed448_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify, &key,
  32541. contextCtx, sizeof(contextCtx));
  32542. if (ret != 0 || verify != 1)
  32543. return WC_TEST_RET_ENC_EC(ret);
  32544. #endif
  32545. wc_ed448_free(&key);
  32546. return 0;
  32547. }
  32548. static wc_test_ret_t ed448ph_test(void)
  32549. {
  32550. wc_test_ret_t ret;
  32551. byte out[ED448_SIG_SIZE];
  32552. word32 outlen;
  32553. #ifdef HAVE_ED448_VERIFY
  32554. int verify;
  32555. #endif /* HAVE_ED448_VERIFY */
  32556. ed448_key key;
  32557. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  32558. 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d,
  32559. 0x62, 0xec, 0x77, 0x58, 0x75, 0x20, 0x91, 0x1e,
  32560. 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b,
  32561. 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42,
  32562. 0xef, 0x78, 0x22, 0xe0, 0xd5, 0x10, 0x41, 0x27,
  32563. 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3,
  32564. 0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c,
  32565. 0x49
  32566. };
  32567. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  32568. 0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77,
  32569. 0xa7, 0xab, 0xd2, 0x65, 0x24, 0xcb, 0xdb, 0x31,
  32570. 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde,
  32571. 0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43,
  32572. 0xc0, 0x94, 0x28, 0xa1, 0x31, 0xd6, 0xb1, 0xb5,
  32573. 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76,
  32574. 0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38,
  32575. 0x80
  32576. };
  32577. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  32578. 0x82, 0x2f, 0x69, 0x01, 0xf7, 0x48, 0x0f, 0x3d,
  32579. 0x5f, 0x56, 0x2c, 0x59, 0x29, 0x94, 0xd9, 0x69,
  32580. 0x36, 0x02, 0x87, 0x56, 0x14, 0x48, 0x32, 0x56,
  32581. 0x50, 0x56, 0x00, 0xbb, 0xc2, 0x81, 0xae, 0x38,
  32582. 0x1f, 0x54, 0xd6, 0xbc, 0xe2, 0xea, 0x91, 0x15,
  32583. 0x74, 0x93, 0x2f, 0x52, 0xa4, 0xe6, 0xca, 0xdd,
  32584. 0x78, 0x76, 0x93, 0x75, 0xec, 0x3f, 0xfd, 0x1b,
  32585. 0x80, 0x1a, 0x0d, 0x9b, 0x3f, 0x40, 0x30, 0xcd,
  32586. 0x43, 0x39, 0x64, 0xb6, 0x45, 0x7e, 0xa3, 0x94,
  32587. 0x76, 0x51, 0x12, 0x14, 0xf9, 0x74, 0x69, 0xb5,
  32588. 0x7d, 0xd3, 0x2d, 0xbc, 0x56, 0x0a, 0x9a, 0x94,
  32589. 0xd0, 0x0b, 0xff, 0x07, 0x62, 0x04, 0x64, 0xa3,
  32590. 0xad, 0x20, 0x3d, 0xf7, 0xdc, 0x7c, 0xe3, 0x60,
  32591. 0xc3, 0xcd, 0x36, 0x96, 0xd9, 0xd9, 0xfa, 0xb9,
  32592. 0x0f, 0x00
  32593. };
  32594. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  32595. 0xc3, 0x22, 0x99, 0xd4, 0x6e, 0xc8, 0xff, 0x02,
  32596. 0xb5, 0x45, 0x40, 0x98, 0x28, 0x14, 0xdc, 0xe9,
  32597. 0xa0, 0x58, 0x12, 0xf8, 0x19, 0x62, 0xb6, 0x49,
  32598. 0xd5, 0x28, 0x09, 0x59, 0x16, 0xa2, 0xaa, 0x48,
  32599. 0x10, 0x65, 0xb1, 0x58, 0x04, 0x23, 0xef, 0x92,
  32600. 0x7e, 0xcf, 0x0a, 0xf5, 0x88, 0x8f, 0x90, 0xda,
  32601. 0x0f, 0x6a, 0x9a, 0x85, 0xad, 0x5d, 0xc3, 0xf2,
  32602. 0x80, 0xd9, 0x12, 0x24, 0xba, 0x99, 0x11, 0xa3,
  32603. 0x65, 0x3d, 0x00, 0xe4, 0x84, 0xe2, 0xce, 0x23,
  32604. 0x25, 0x21, 0x48, 0x1c, 0x86, 0x58, 0xdf, 0x30,
  32605. 0x4b, 0xb7, 0x74, 0x5a, 0x73, 0x51, 0x4c, 0xdb,
  32606. 0x9b, 0xf3, 0xe1, 0x57, 0x84, 0xab, 0x71, 0x28,
  32607. 0x4f, 0x8d, 0x07, 0x04, 0xa6, 0x08, 0xc5, 0x4a,
  32608. 0x6b, 0x62, 0xd9, 0x7b, 0xeb, 0x51, 0x1d, 0x13,
  32609. 0x21, 0x00
  32610. };
  32611. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  32612. 0x61,0x62,0x63
  32613. };
  32614. /* SHA-512 hash of msgPh */
  32615. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  32616. 0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77,
  32617. 0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
  32618. 0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee,
  32619. 0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39,
  32620. 0xd5, 0xa1, 0x5b, 0xef, 0x18, 0x6a, 0x53, 0x86,
  32621. 0xc7, 0x57, 0x44, 0xc0, 0x52, 0x7e, 0x1f, 0xaa,
  32622. 0x9f, 0x87, 0x26, 0xe4, 0x62, 0xa1, 0x2a, 0x4f,
  32623. 0xeb, 0x06, 0xbd, 0x88, 0x01, 0xe7, 0x51, 0xe4
  32624. };
  32625. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  32626. 0x66,0x6f,0x6f
  32627. };
  32628. outlen = sizeof(out);
  32629. XMEMSET(out, 0, sizeof(out));
  32630. ret = wc_ed448_init_ex(&key, HEAP_HINT, devId);
  32631. if (ret != 0)
  32632. return WC_TEST_RET_ENC_EC(ret);
  32633. ret = wc_ed448_import_private_key(sKeyPh, ED448_KEY_SIZE, pKeyPh,
  32634. sizeof(pKeyPh), &key);
  32635. if (ret != 0)
  32636. return WC_TEST_RET_ENC_EC(ret);
  32637. ret = wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key, NULL,
  32638. 0);
  32639. if (ret != 0)
  32640. return WC_TEST_RET_ENC_EC(ret);
  32641. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  32642. return WC_TEST_RET_ENC_NC;
  32643. #if defined(HAVE_ED448_VERIFY)
  32644. /* test verify on good msg */
  32645. ret = wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  32646. NULL, 0);
  32647. if (ret != 0 || verify != 1) {
  32648. return WC_TEST_RET_ENC_EC(ret);
  32649. }
  32650. #endif
  32651. ret = wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  32652. contextPh2, sizeof(contextPh2));
  32653. if (ret != 0)
  32654. return WC_TEST_RET_ENC_EC(ret);
  32655. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  32656. return WC_TEST_RET_ENC_NC;
  32657. #if defined(HAVE_ED448_VERIFY)
  32658. /* test verify on good msg */
  32659. ret = wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  32660. contextPh2, sizeof(contextPh2));
  32661. if (ret != 0 || verify != 1) {
  32662. return WC_TEST_RET_ENC_EC(ret);
  32663. }
  32664. #endif
  32665. ret = wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key, NULL,
  32666. 0);
  32667. if (ret != 0)
  32668. return WC_TEST_RET_ENC_EC(ret);
  32669. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  32670. return WC_TEST_RET_ENC_NC;
  32671. #if defined(HAVE_ED448_VERIFY)
  32672. ret = wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  32673. &key, NULL, 0);
  32674. if (ret != 0 || verify != 1) {
  32675. return WC_TEST_RET_ENC_EC(ret);
  32676. }
  32677. #endif
  32678. ret = wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  32679. contextPh2, sizeof(contextPh2));
  32680. if (ret != 0)
  32681. return WC_TEST_RET_ENC_EC(ret);
  32682. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  32683. return WC_TEST_RET_ENC_NC;
  32684. #if defined(HAVE_ED448_VERIFY)
  32685. ret = wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  32686. &key, contextPh2, sizeof(contextPh2));
  32687. if (ret != 0 || verify != 1) {
  32688. return WC_TEST_RET_ENC_EC(ret);
  32689. }
  32690. #endif
  32691. wc_ed448_free(&key);
  32692. return 0;
  32693. }
  32694. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  32695. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed448_test(void)
  32696. {
  32697. wc_test_ret_t ret;
  32698. WC_RNG rng;
  32699. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  32700. defined(HAVE_ED448_KEY_IMPORT)
  32701. byte out[ED448_SIG_SIZE];
  32702. int i;
  32703. word32 outlen;
  32704. #ifdef HAVE_ED448_VERIFY
  32705. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  32706. int j;
  32707. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  32708. int verify;
  32709. #endif /* HAVE_ED448_VERIFY */
  32710. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  32711. word32 keySz, sigSz;
  32712. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  32713. ed448_key *key = NULL;
  32714. ed448_key *key2 = NULL;
  32715. #else
  32716. ed448_key key[1];
  32717. ed448_key key2[1];
  32718. #endif
  32719. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  32720. defined(HAVE_ED448_KEY_IMPORT)
  32721. /* test vectors from
  32722. https://tools.ietf.org/html/rfc8032
  32723. */
  32724. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  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 sKey2[] = {
  32735. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  32736. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  32737. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  32738. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  32739. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  32740. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  32741. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  32742. 0x4e
  32743. };
  32744. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  32745. 0x25, 0x8c, 0xdd, 0x4a, 0xda, 0x32, 0xed, 0x9c,
  32746. 0x9f, 0xf5, 0x4e, 0x63, 0x75, 0x6a, 0xe5, 0x82,
  32747. 0xfb, 0x8f, 0xab, 0x2a, 0xc7, 0x21, 0xf2, 0xc8,
  32748. 0xe6, 0x76, 0xa7, 0x27, 0x68, 0x51, 0x3d, 0x93,
  32749. 0x9f, 0x63, 0xdd, 0xdb, 0x55, 0x60, 0x91, 0x33,
  32750. 0xf2, 0x9a, 0xdf, 0x86, 0xec, 0x99, 0x29, 0xdc,
  32751. 0xcb, 0x52, 0xc1, 0xc5, 0xfd, 0x2f, 0xf7, 0xe2,
  32752. 0x1b
  32753. };
  32754. /* uncompressed test */
  32755. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  32756. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  32757. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  32758. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  32759. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  32760. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  32761. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  32762. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  32763. 0x5b
  32764. };
  32765. /* compressed prefix test */
  32766. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  32767. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  32768. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  32769. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  32770. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  32771. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  32772. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  32773. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  32774. 0x5b
  32775. };
  32776. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  32777. 0x87, 0x2d, 0x09, 0x37, 0x80, 0xf5, 0xd3, 0x73,
  32778. 0x0d, 0xf7, 0xc2, 0x12, 0x66, 0x4b, 0x37, 0xb8,
  32779. 0xa0, 0xf2, 0x4f, 0x56, 0x81, 0x0d, 0xaa, 0x83,
  32780. 0x82, 0xcd, 0x4f, 0xa3, 0xf7, 0x76, 0x34, 0xec,
  32781. 0x44, 0xdc, 0x54, 0xf1, 0xc2, 0xed, 0x9b, 0xea,
  32782. 0x86, 0xfa, 0xfb, 0x76, 0x32, 0xd8, 0xbe, 0x19,
  32783. 0x9e, 0xa1, 0x65, 0xf5, 0xad, 0x55, 0xdd, 0x9c,
  32784. 0xe8
  32785. };
  32786. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  32787. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  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 pKey2[] = {
  32798. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  32799. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  32800. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  32801. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  32802. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  32803. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  32804. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  32805. 0x80
  32806. };
  32807. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  32808. 0x3b, 0xa1, 0x6d, 0xa0, 0xc6, 0xf2, 0xcc, 0x1f,
  32809. 0x30, 0x18, 0x77, 0x40, 0x75, 0x6f, 0x5e, 0x79,
  32810. 0x8d, 0x6b, 0xc5, 0xfc, 0x01, 0x5d, 0x7c, 0x63,
  32811. 0xcc, 0x95, 0x10, 0xee, 0x3f, 0xd4, 0x4a, 0xdc,
  32812. 0x24, 0xd8, 0xe9, 0x68, 0xb6, 0xe4, 0x6e, 0x6f,
  32813. 0x94, 0xd1, 0x9b, 0x94, 0x53, 0x61, 0x72, 0x6b,
  32814. 0xd7, 0x5e, 0x14, 0x9e, 0xf0, 0x98, 0x17, 0xf5,
  32815. 0x80
  32816. };
  32817. /* uncompressed test */
  32818. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  32819. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  32820. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  32821. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  32822. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  32823. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  32824. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  32825. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  32826. 0x80
  32827. };
  32828. /* compressed prefix */
  32829. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  32830. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  32831. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  32832. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  32833. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  32834. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  32835. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  32836. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  32837. 0x80
  32838. };
  32839. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  32840. 0xa8, 0x1b, 0x2e, 0x8a, 0x70, 0xa5, 0xac, 0x94,
  32841. 0xff, 0xdb, 0xcc, 0x9b, 0xad, 0xfc, 0x3f, 0xeb,
  32842. 0x08, 0x01, 0xf2, 0x58, 0x57, 0x8b, 0xb1, 0x14,
  32843. 0xad, 0x44, 0xec, 0xe1, 0xec, 0x0e, 0x79, 0x9d,
  32844. 0xa0, 0x8e, 0xff, 0xb8, 0x1c, 0x5d, 0x68, 0x5c,
  32845. 0x0c, 0x56, 0xf6, 0x4e, 0xec, 0xae, 0xf8, 0xcd,
  32846. 0xf1, 0x1c, 0xc3, 0x87, 0x37, 0x83, 0x8c, 0xf4,
  32847. 0x00
  32848. };
  32849. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  32850. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  32851. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  32852. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  32853. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  32854. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  32855. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  32856. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  32857. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  32858. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  32859. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  32860. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  32861. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  32862. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  32863. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  32864. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  32865. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  32866. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  32867. 0x26, 0x00
  32868. };
  32869. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  32870. 0x26, 0xb8, 0xf9, 0x17, 0x27, 0xbd, 0x62, 0x89,
  32871. 0x7a, 0xf1, 0x5e, 0x41, 0xeb, 0x43, 0xc3, 0x77,
  32872. 0xef, 0xb9, 0xc6, 0x10, 0xd4, 0x8f, 0x23, 0x35,
  32873. 0xcb, 0x0b, 0xd0, 0x08, 0x78, 0x10, 0xf4, 0x35,
  32874. 0x25, 0x41, 0xb1, 0x43, 0xc4, 0xb9, 0x81, 0xb7,
  32875. 0xe1, 0x8f, 0x62, 0xde, 0x8c, 0xcd, 0xf6, 0x33,
  32876. 0xfc, 0x1b, 0xf0, 0x37, 0xab, 0x7c, 0xd7, 0x79,
  32877. 0x80, 0x5e, 0x0d, 0xbc, 0xc0, 0xaa, 0xe1, 0xcb,
  32878. 0xce, 0xe1, 0xaf, 0xb2, 0xe0, 0x27, 0xdf, 0x36,
  32879. 0xbc, 0x04, 0xdc, 0xec, 0xbf, 0x15, 0x43, 0x36,
  32880. 0xc1, 0x9f, 0x0a, 0xf7, 0xe0, 0xa6, 0x47, 0x29,
  32881. 0x05, 0xe7, 0x99, 0xf1, 0x95, 0x3d, 0x2a, 0x0f,
  32882. 0xf3, 0x34, 0x8a, 0xb2, 0x1a, 0xa4, 0xad, 0xaf,
  32883. 0xd1, 0xd2, 0x34, 0x44, 0x1c, 0xf8, 0x07, 0xc0,
  32884. 0x3a, 0x00
  32885. };
  32886. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  32887. 0x7e, 0xee, 0xab, 0x7c, 0x4e, 0x50, 0xfb, 0x79,
  32888. 0x9b, 0x41, 0x8e, 0xe5, 0xe3, 0x19, 0x7f, 0xf6,
  32889. 0xbf, 0x15, 0xd4, 0x3a, 0x14, 0xc3, 0x43, 0x89,
  32890. 0xb5, 0x9d, 0xd1, 0xa7, 0xb1, 0xb8, 0x5b, 0x4a,
  32891. 0xe9, 0x04, 0x38, 0xac, 0xa6, 0x34, 0xbe, 0xa4,
  32892. 0x5e, 0x3a, 0x26, 0x95, 0xf1, 0x27, 0x0f, 0x07,
  32893. 0xfd, 0xcd, 0xf7, 0xc6, 0x2b, 0x8e, 0xfe, 0xaf,
  32894. 0x00, 0xb4, 0x5c, 0x2c, 0x96, 0xba, 0x45, 0x7e,
  32895. 0xb1, 0xa8, 0xbf, 0x07, 0x5a, 0x3d, 0xb2, 0x8e,
  32896. 0x5c, 0x24, 0xf6, 0xb9, 0x23, 0xed, 0x4a, 0xd7,
  32897. 0x47, 0xc3, 0xc9, 0xe0, 0x3c, 0x70, 0x79, 0xef,
  32898. 0xb8, 0x7c, 0xb1, 0x10, 0xd3, 0xa9, 0x98, 0x61,
  32899. 0xe7, 0x20, 0x03, 0xcb, 0xae, 0x6d, 0x6b, 0x8b,
  32900. 0x82, 0x7e, 0x4e, 0x6c, 0x14, 0x30, 0x64, 0xff,
  32901. 0x3c, 0x00
  32902. };
  32903. /* uncompressed test */
  32904. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  32905. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  32906. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  32907. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  32908. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  32909. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  32910. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  32911. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  32912. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  32913. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  32914. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  32915. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  32916. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  32917. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  32918. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  32919. 0x26, 0x00
  32920. };
  32921. /* compressed prefix */
  32922. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  32923. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  32924. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  32925. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  32926. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  32927. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  32928. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  32929. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  32930. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  32931. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  32932. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  32933. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  32934. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  32935. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  32936. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  32937. 0x26, 0x00
  32938. };
  32939. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  32940. 0xe3, 0x01, 0x34, 0x5a, 0x41, 0xa3, 0x9a, 0x4d,
  32941. 0x72, 0xff, 0xf8, 0xdf, 0x69, 0xc9, 0x80, 0x75,
  32942. 0xa0, 0xcc, 0x08, 0x2b, 0x80, 0x2f, 0xc9, 0xb2,
  32943. 0xb6, 0xbc, 0x50, 0x3f, 0x92, 0x6b, 0x65, 0xbd,
  32944. 0xdf, 0x7f, 0x4c, 0x8f, 0x1c, 0xb4, 0x9f, 0x63,
  32945. 0x96, 0xaf, 0xc8, 0xa7, 0x0a, 0xbe, 0x6d, 0x8a,
  32946. 0xef, 0x0d, 0xb4, 0x78, 0xd4, 0xc6, 0xb2, 0x97,
  32947. 0x00, 0x76, 0xc6, 0xa0, 0x48, 0x4f, 0xe7, 0x6d,
  32948. 0x76, 0xb3, 0xa9, 0x76, 0x25, 0xd7, 0x9f, 0x1c,
  32949. 0xe2, 0x40, 0xe7, 0xc5, 0x76, 0x75, 0x0d, 0x29,
  32950. 0x55, 0x28, 0x28, 0x6f, 0x71, 0x9b, 0x41, 0x3d,
  32951. 0xe9, 0xad, 0xa3, 0xe8, 0xeb, 0x78, 0xed, 0x57,
  32952. 0x36, 0x03, 0xce, 0x30, 0xd8, 0xbb, 0x76, 0x17,
  32953. 0x85, 0xdc, 0x30, 0xdb, 0xc3, 0x20, 0x86, 0x9e,
  32954. 0x1a, 0x00
  32955. };
  32956. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  32957. #define SIGSZ sizeof(sig1)
  32958. PEDANTIC_EXTENSION WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = { 0 };
  32959. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = { 0x03 };
  32960. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = { 0x64, 0xa6, 0x5f, 0x3c, 0xde, 0xdc, 0xdd,
  32961. 0x66, 0x81, 0x1e, 0x29, 0x15 };
  32962. /* test of a 1023 byte long message */
  32963. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  32964. 0x6d, 0xdf, 0x80, 0x2e, 0x1a, 0xae, 0x49, 0x86,
  32965. 0x93, 0x5f, 0x7f, 0x98, 0x1b, 0xa3, 0xf0, 0x35,
  32966. 0x1d, 0x62, 0x73, 0xc0, 0xa0, 0xc2, 0x2c, 0x9c,
  32967. 0x0e, 0x83, 0x39, 0x16, 0x8e, 0x67, 0x54, 0x12,
  32968. 0xa3, 0xde, 0xbf, 0xaf, 0x43, 0x5e, 0xd6, 0x51,
  32969. 0x55, 0x80, 0x07, 0xdb, 0x43, 0x84, 0xb6, 0x50,
  32970. 0xfc, 0xc0, 0x7e, 0x3b, 0x58, 0x6a, 0x27, 0xa4,
  32971. 0xf7, 0xa0, 0x0a, 0xc8, 0xa6, 0xfe, 0xc2, 0xcd,
  32972. 0x86, 0xae, 0x4b, 0xf1, 0x57, 0x0c, 0x41, 0xe6,
  32973. 0xa4, 0x0c, 0x93, 0x1d, 0xb2, 0x7b, 0x2f, 0xaa,
  32974. 0x15, 0xa8, 0xce, 0xdd, 0x52, 0xcf, 0xf7, 0x36,
  32975. 0x2c, 0x4e, 0x6e, 0x23, 0xda, 0xec, 0x0f, 0xbc,
  32976. 0x3a, 0x79, 0xb6, 0x80, 0x6e, 0x31, 0x6e, 0xfc,
  32977. 0xc7, 0xb6, 0x81, 0x19, 0xbf, 0x46, 0xbc, 0x76,
  32978. 0xa2, 0x60, 0x67, 0xa5, 0x3f, 0x29, 0x6d, 0xaf,
  32979. 0xdb, 0xdc, 0x11, 0xc7, 0x7f, 0x77, 0x77, 0xe9,
  32980. 0x72, 0x66, 0x0c, 0xf4, 0xb6, 0xa9, 0xb3, 0x69,
  32981. 0xa6, 0x66, 0x5f, 0x02, 0xe0, 0xcc, 0x9b, 0x6e,
  32982. 0xdf, 0xad, 0x13, 0x6b, 0x4f, 0xab, 0xe7, 0x23,
  32983. 0xd2, 0x81, 0x3d, 0xb3, 0x13, 0x6c, 0xfd, 0xe9,
  32984. 0xb6, 0xd0, 0x44, 0x32, 0x2f, 0xee, 0x29, 0x47,
  32985. 0x95, 0x2e, 0x03, 0x1b, 0x73, 0xab, 0x5c, 0x60,
  32986. 0x33, 0x49, 0xb3, 0x07, 0xbd, 0xc2, 0x7b, 0xc6,
  32987. 0xcb, 0x8b, 0x8b, 0xbd, 0x7b, 0xd3, 0x23, 0x21,
  32988. 0x9b, 0x80, 0x33, 0xa5, 0x81, 0xb5, 0x9e, 0xad,
  32989. 0xeb, 0xb0, 0x9b, 0x3c, 0x4f, 0x3d, 0x22, 0x77,
  32990. 0xd4, 0xf0, 0x34, 0x36, 0x24, 0xac, 0xc8, 0x17,
  32991. 0x80, 0x47, 0x28, 0xb2, 0x5a, 0xb7, 0x97, 0x17,
  32992. 0x2b, 0x4c, 0x5c, 0x21, 0xa2, 0x2f, 0x9c, 0x78,
  32993. 0x39, 0xd6, 0x43, 0x00, 0x23, 0x2e, 0xb6, 0x6e,
  32994. 0x53, 0xf3, 0x1c, 0x72, 0x3f, 0xa3, 0x7f, 0xe3,
  32995. 0x87, 0xc7, 0xd3, 0xe5, 0x0b, 0xdf, 0x98, 0x13,
  32996. 0xa3, 0x0e, 0x5b, 0xb1, 0x2c, 0xf4, 0xcd, 0x93,
  32997. 0x0c, 0x40, 0xcf, 0xb4, 0xe1, 0xfc, 0x62, 0x25,
  32998. 0x92, 0xa4, 0x95, 0x88, 0x79, 0x44, 0x94, 0xd5,
  32999. 0x6d, 0x24, 0xea, 0x4b, 0x40, 0xc8, 0x9f, 0xc0,
  33000. 0x59, 0x6c, 0xc9, 0xeb, 0xb9, 0x61, 0xc8, 0xcb,
  33001. 0x10, 0xad, 0xde, 0x97, 0x6a, 0x5d, 0x60, 0x2b,
  33002. 0x1c, 0x3f, 0x85, 0xb9, 0xb9, 0xa0, 0x01, 0xed,
  33003. 0x3c, 0x6a, 0x4d, 0x3b, 0x14, 0x37, 0xf5, 0x20,
  33004. 0x96, 0xcd, 0x19, 0x56, 0xd0, 0x42, 0xa5, 0x97,
  33005. 0xd5, 0x61, 0xa5, 0x96, 0xec, 0xd3, 0xd1, 0x73,
  33006. 0x5a, 0x8d, 0x57, 0x0e, 0xa0, 0xec, 0x27, 0x22,
  33007. 0x5a, 0x2c, 0x4a, 0xaf, 0xf2, 0x63, 0x06, 0xd1,
  33008. 0x52, 0x6c, 0x1a, 0xf3, 0xca, 0x6d, 0x9c, 0xf5,
  33009. 0xa2, 0xc9, 0x8f, 0x47, 0xe1, 0xc4, 0x6d, 0xb9,
  33010. 0xa3, 0x32, 0x34, 0xcf, 0xd4, 0xd8, 0x1f, 0x2c,
  33011. 0x98, 0x53, 0x8a, 0x09, 0xeb, 0xe7, 0x69, 0x98,
  33012. 0xd0, 0xd8, 0xfd, 0x25, 0x99, 0x7c, 0x7d, 0x25,
  33013. 0x5c, 0x6d, 0x66, 0xec, 0xe6, 0xfa, 0x56, 0xf1,
  33014. 0x11, 0x44, 0x95, 0x0f, 0x02, 0x77, 0x95, 0xe6,
  33015. 0x53, 0x00, 0x8f, 0x4b, 0xd7, 0xca, 0x2d, 0xee,
  33016. 0x85, 0xd8, 0xe9, 0x0f, 0x3d, 0xc3, 0x15, 0x13,
  33017. 0x0c, 0xe2, 0xa0, 0x03, 0x75, 0xa3, 0x18, 0xc7,
  33018. 0xc3, 0xd9, 0x7b, 0xe2, 0xc8, 0xce, 0x5b, 0x6d,
  33019. 0xb4, 0x1a, 0x62, 0x54, 0xff, 0x26, 0x4f, 0xa6,
  33020. 0x15, 0x5b, 0xae, 0xe3, 0xb0, 0x77, 0x3c, 0x0f,
  33021. 0x49, 0x7c, 0x57, 0x3f, 0x19, 0xbb, 0x4f, 0x42,
  33022. 0x40, 0x28, 0x1f, 0x0b, 0x1f, 0x4f, 0x7b, 0xe8,
  33023. 0x57, 0xa4, 0xe5, 0x9d, 0x41, 0x6c, 0x06, 0xb4,
  33024. 0xc5, 0x0f, 0xa0, 0x9e, 0x18, 0x10, 0xdd, 0xc6,
  33025. 0xb1, 0x46, 0x7b, 0xae, 0xac, 0x5a, 0x36, 0x68,
  33026. 0xd1, 0x1b, 0x6e, 0xca, 0xa9, 0x01, 0x44, 0x00,
  33027. 0x16, 0xf3, 0x89, 0xf8, 0x0a, 0xcc, 0x4d, 0xb9,
  33028. 0x77, 0x02, 0x5e, 0x7f, 0x59, 0x24, 0x38, 0x8c,
  33029. 0x7e, 0x34, 0x0a, 0x73, 0x2e, 0x55, 0x44, 0x40,
  33030. 0xe7, 0x65, 0x70, 0xf8, 0xdd, 0x71, 0xb7, 0xd6,
  33031. 0x40, 0xb3, 0x45, 0x0d, 0x1f, 0xd5, 0xf0, 0x41,
  33032. 0x0a, 0x18, 0xf9, 0xa3, 0x49, 0x4f, 0x70, 0x7c,
  33033. 0x71, 0x7b, 0x79, 0xb4, 0xbf, 0x75, 0xc9, 0x84,
  33034. 0x00, 0xb0, 0x96, 0xb2, 0x16, 0x53, 0xb5, 0xd2,
  33035. 0x17, 0xcf, 0x35, 0x65, 0xc9, 0x59, 0x74, 0x56,
  33036. 0xf7, 0x07, 0x03, 0x49, 0x7a, 0x07, 0x87, 0x63,
  33037. 0x82, 0x9b, 0xc0, 0x1b, 0xb1, 0xcb, 0xc8, 0xfa,
  33038. 0x04, 0xea, 0xdc, 0x9a, 0x6e, 0x3f, 0x66, 0x99,
  33039. 0x58, 0x7a, 0x9e, 0x75, 0xc9, 0x4e, 0x5b, 0xab,
  33040. 0x00, 0x36, 0xe0, 0xb2, 0xe7, 0x11, 0x39, 0x2c,
  33041. 0xff, 0x00, 0x47, 0xd0, 0xd6, 0xb0, 0x5b, 0xd2,
  33042. 0xa5, 0x88, 0xbc, 0x10, 0x97, 0x18, 0x95, 0x42,
  33043. 0x59, 0xf1, 0xd8, 0x66, 0x78, 0xa5, 0x79, 0xa3,
  33044. 0x12, 0x0f, 0x19, 0xcf, 0xb2, 0x96, 0x3f, 0x17,
  33045. 0x7a, 0xeb, 0x70, 0xf2, 0xd4, 0x84, 0x48, 0x26,
  33046. 0x26, 0x2e, 0x51, 0xb8, 0x02, 0x71, 0x27, 0x20,
  33047. 0x68, 0xef, 0x5b, 0x38, 0x56, 0xfa, 0x85, 0x35,
  33048. 0xaa, 0x2a, 0x88, 0xb2, 0xd4, 0x1f, 0x2a, 0x0e,
  33049. 0x2f, 0xda, 0x76, 0x24, 0xc2, 0x85, 0x02, 0x72,
  33050. 0xac, 0x4a, 0x2f, 0x56, 0x1f, 0x8f, 0x2f, 0x7a,
  33051. 0x31, 0x8b, 0xfd, 0x5c, 0xaf, 0x96, 0x96, 0x14,
  33052. 0x9e, 0x4a, 0xc8, 0x24, 0xad, 0x34, 0x60, 0x53,
  33053. 0x8f, 0xdc, 0x25, 0x42, 0x1b, 0xee, 0xc2, 0xcc,
  33054. 0x68, 0x18, 0x16, 0x2d, 0x06, 0xbb, 0xed, 0x0c,
  33055. 0x40, 0xa3, 0x87, 0x19, 0x23, 0x49, 0xdb, 0x67,
  33056. 0xa1, 0x18, 0xba, 0xda, 0x6c, 0xd5, 0xab, 0x01,
  33057. 0x40, 0xee, 0x27, 0x32, 0x04, 0xf6, 0x28, 0xaa,
  33058. 0xd1, 0xc1, 0x35, 0xf7, 0x70, 0x27, 0x9a, 0x65,
  33059. 0x1e, 0x24, 0xd8, 0xc1, 0x4d, 0x75, 0xa6, 0x05,
  33060. 0x9d, 0x76, 0xb9, 0x6a, 0x6f, 0xd8, 0x57, 0xde,
  33061. 0xf5, 0xe0, 0xb3, 0x54, 0xb2, 0x7a, 0xb9, 0x37,
  33062. 0xa5, 0x81, 0x5d, 0x16, 0xb5, 0xfa, 0xe4, 0x07,
  33063. 0xff, 0x18, 0x22, 0x2c, 0x6d, 0x1e, 0xd2, 0x63,
  33064. 0xbe, 0x68, 0xc9, 0x5f, 0x32, 0xd9, 0x08, 0xbd,
  33065. 0x89, 0x5c, 0xd7, 0x62, 0x07, 0xae, 0x72, 0x64,
  33066. 0x87, 0x56, 0x7f, 0x9a, 0x67, 0xda, 0xd7, 0x9a,
  33067. 0xbe, 0xc3, 0x16, 0xf6, 0x83, 0xb1, 0x7f, 0x2d,
  33068. 0x02, 0xbf, 0x07, 0xe0, 0xac, 0x8b, 0x5b, 0xc6,
  33069. 0x16, 0x2c, 0xf9, 0x46, 0x97, 0xb3, 0xc2, 0x7c,
  33070. 0xd1, 0xfe, 0xa4, 0x9b, 0x27, 0xf2, 0x3b, 0xa2,
  33071. 0x90, 0x18, 0x71, 0x96, 0x25, 0x06, 0x52, 0x0c,
  33072. 0x39, 0x2d, 0xa8, 0xb6, 0xad, 0x0d, 0x99, 0xf7,
  33073. 0x01, 0x3f, 0xbc, 0x06, 0xc2, 0xc1, 0x7a, 0x56,
  33074. 0x95, 0x00, 0xc8, 0xa7, 0x69, 0x64, 0x81, 0xc1,
  33075. 0xcd, 0x33, 0xe9, 0xb1, 0x4e, 0x40, 0xb8, 0x2e,
  33076. 0x79, 0xa5, 0xf5, 0xdb, 0x82, 0x57, 0x1b, 0xa9,
  33077. 0x7b, 0xae, 0x3a, 0xd3, 0xe0, 0x47, 0x95, 0x15,
  33078. 0xbb, 0x0e, 0x2b, 0x0f, 0x3b, 0xfc, 0xd1, 0xfd,
  33079. 0x33, 0x03, 0x4e, 0xfc, 0x62, 0x45, 0xed, 0xdd,
  33080. 0x7e, 0xe2, 0x08, 0x6d, 0xda, 0xe2, 0x60, 0x0d,
  33081. 0x8c, 0xa7, 0x3e, 0x21, 0x4e, 0x8c, 0x2b, 0x0b,
  33082. 0xdb, 0x2b, 0x04, 0x7c, 0x6a, 0x46, 0x4a, 0x56,
  33083. 0x2e, 0xd7, 0x7b, 0x73, 0xd2, 0xd8, 0x41, 0xc4,
  33084. 0xb3, 0x49, 0x73, 0x55, 0x12, 0x57, 0x71, 0x3b,
  33085. 0x75, 0x36, 0x32, 0xef, 0xba, 0x34, 0x81, 0x69,
  33086. 0xab, 0xc9, 0x0a, 0x68, 0xf4, 0x26, 0x11, 0xa4,
  33087. 0x01, 0x26, 0xd7, 0xcb, 0x21, 0xb5, 0x86, 0x95,
  33088. 0x56, 0x81, 0x86, 0xf7, 0xe5, 0x69, 0xd2, 0xff,
  33089. 0x0f, 0x9e, 0x74, 0x5d, 0x04, 0x87, 0xdd, 0x2e,
  33090. 0xb9, 0x97, 0xca, 0xfc, 0x5a, 0xbf, 0x9d, 0xd1,
  33091. 0x02, 0xe6, 0x2f, 0xf6, 0x6c, 0xba, 0x87
  33092. };
  33093. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  33094. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  33095. sizeof(msg2),
  33096. sizeof(msg3),
  33097. 0 /*sizeof(msg1)*/,
  33098. 0 /*sizeof(msg1)*/,
  33099. sizeof(msg4)
  33100. };
  33101. #ifndef NO_ASN
  33102. static const byte privateEd448[] = {
  33103. 0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
  33104. 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04, 0x39,
  33105. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  33106. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  33107. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  33108. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  33109. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  33110. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  33111. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  33112. 0x5b
  33113. };
  33114. static const byte publicEd448[] = {
  33115. 0x30, 0x43, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
  33116. 0x71, 0x03, 0x3a, 0x00, 0x5f, 0xd7, 0x44, 0x9b,
  33117. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  33118. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  33119. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  33120. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  33121. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  33122. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  33123. 0xaf, 0xe8, 0x25, 0x61, 0x80
  33124. };
  33125. static const byte privPubEd448[] = {
  33126. 0x30, 0x81, 0x82, 0x02, 0x01, 0x00, 0x30, 0x05,
  33127. 0x06, 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04,
  33128. 0x39, 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d,
  33129. 0x10, 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e,
  33130. 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c,
  33131. 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48,
  33132. 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04,
  33133. 0x4e, 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f,
  33134. 0x8f, 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98,
  33135. 0xf9, 0x5b, 0x81, 0x39, 0x5f, 0xd7, 0x44, 0x9b,
  33136. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  33137. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  33138. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  33139. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  33140. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  33141. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  33142. 0xaf, 0xe8, 0x25, 0x61, 0x80
  33143. };
  33144. word32 idx;
  33145. #endif /* NO_ASN */
  33146. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  33147. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  33148. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33149. ed448_key *key3 = NULL;
  33150. #else
  33151. ed448_key key3[1];
  33152. #endif
  33153. #endif
  33154. WOLFSSL_ENTER("ed448_test");
  33155. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33156. key = (ed448_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33157. key2 = (ed448_key *)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33158. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  33159. key3 = (ed448_key *)XMALLOC(sizeof(*key3), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33160. #endif
  33161. #endif
  33162. /* create ed448 keys */
  33163. #ifndef HAVE_FIPS
  33164. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  33165. #else
  33166. ret = wc_InitRng(&rng);
  33167. #endif
  33168. if (ret != 0) {
  33169. XMEMSET(&rng, 0, sizeof(rng));
  33170. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33171. }
  33172. ret = wc_ed448_init(key);
  33173. if (ret < 0)
  33174. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33175. ret = wc_ed448_init(key2);
  33176. if (ret < 0)
  33177. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33178. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  33179. ret = wc_ed448_init(key3);
  33180. if (ret < 0)
  33181. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33182. #endif
  33183. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, key);
  33184. if (ret < 0)
  33185. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33186. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, key2);
  33187. if (ret < 0)
  33188. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33189. /* helper functions for signature and key size */
  33190. keySz = (word32)wc_ed448_size(key);
  33191. sigSz = (word32)wc_ed448_sig_size(key);
  33192. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  33193. defined(HAVE_ED448_KEY_IMPORT)
  33194. for (i = 0; i < 6; i++) {
  33195. outlen = sizeof(out);
  33196. XMEMSET(out, 0, sizeof(out));
  33197. if (wc_ed448_import_private_key(sKeys[i], ED448_KEY_SIZE, pKeys[i],
  33198. pKeySz[i], key) != 0)
  33199. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33200. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key, NULL,
  33201. 0) != 0)
  33202. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33203. if (XMEMCMP(out, sigs[i], 114))
  33204. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33205. #if defined(HAVE_ED448_VERIFY)
  33206. /* test verify on good msg */
  33207. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  33208. NULL, 0) != 0 || verify != 1)
  33209. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33210. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  33211. /* test verify on good msg using streaming interface directly */
  33212. if (wc_ed448_verify_msg_init(out, outlen,
  33213. key, (byte)Ed448, NULL, 0) != 0)
  33214. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33215. for (j = 0; j < msgSz[i]; j += i) {
  33216. if (wc_ed448_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), key) != 0)
  33217. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33218. }
  33219. if (wc_ed448_verify_msg_final(out, outlen, &verify,
  33220. key) != 0 || verify != 1)
  33221. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33222. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  33223. /* test verify on bad msg */
  33224. out[outlen-2] = out[outlen-2] + 1;
  33225. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  33226. NULL, 0) == 0 || verify == 1)
  33227. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33228. #endif /* HAVE_ED448_VERIFY */
  33229. /* test api for import/exporting keys */
  33230. {
  33231. byte *exportPKey = NULL;
  33232. byte *exportSKey = NULL;
  33233. word32 exportPSz = ED448_KEY_SIZE;
  33234. word32 exportSSz = ED448_KEY_SIZE;
  33235. exportPKey = (byte *)XMALLOC(exportPSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33236. exportSKey = (byte *)XMALLOC(exportSSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33237. if ((exportPKey == NULL) || (exportSKey == NULL))
  33238. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33239. ret = 0;
  33240. do {
  33241. if (wc_ed448_export_public(key, exportPKey, &exportPSz) != 0) {
  33242. ret = WC_TEST_RET_ENC_I(i);
  33243. break;
  33244. }
  33245. if (wc_ed448_import_public_ex(exportPKey, exportPSz, key2, 1) != 0) {
  33246. ret = WC_TEST_RET_ENC_I(i);
  33247. break;
  33248. }
  33249. if (wc_ed448_export_private_only(key, exportSKey, &exportSSz) != 0) {
  33250. ret = WC_TEST_RET_ENC_I(i);
  33251. break;
  33252. }
  33253. if (wc_ed448_import_private_key(exportSKey, exportSSz,
  33254. exportPKey, exportPSz, key2) != 0) {
  33255. ret = WC_TEST_RET_ENC_I(i);
  33256. break;
  33257. }
  33258. /* clear "out" buffer and test sign with imported keys */
  33259. outlen = sizeof(out);
  33260. XMEMSET(out, 0, sizeof(out));
  33261. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key2, NULL,
  33262. 0) != 0) {
  33263. ret = WC_TEST_RET_ENC_I(i);
  33264. break;
  33265. }
  33266. } while(0);
  33267. XFREE(exportPKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33268. XFREE(exportSKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33269. if (ret != 0)
  33270. goto out;
  33271. }
  33272. #if defined(HAVE_ED448_VERIFY)
  33273. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key2,
  33274. NULL, 0) != 0 || verify != 1)
  33275. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33276. if (XMEMCMP(out, sigs[i], SIGSZ))
  33277. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33278. #endif /* HAVE_ED448_VERIFY */
  33279. }
  33280. ret = ed448_ctx_test();
  33281. if (ret != 0)
  33282. goto out;
  33283. ret = ed448ph_test();
  33284. if (ret != 0)
  33285. goto out;
  33286. #ifndef NO_ASN
  33287. /* Try ASN.1 encoded private-only key and public key. */
  33288. idx = 0;
  33289. ret = wc_Ed448PrivateKeyDecode(privateEd448, &idx, key3,
  33290. sizeof(privateEd448));
  33291. if (ret != 0)
  33292. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33293. ret = wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0);
  33294. if (ret != BAD_FUNC_ARG)
  33295. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33296. idx = 0;
  33297. ret = wc_Ed448PublicKeyDecode(publicEd448, &idx, key3, sizeof(publicEd448));
  33298. if (ret != 0)
  33299. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33300. ret = wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0);
  33301. if (ret != 0)
  33302. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33303. if (XMEMCMP(out, sigs[0], SIGSZ))
  33304. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33305. #if defined(HAVE_ED448_VERIFY)
  33306. /* test verify on good msg */
  33307. ret = wc_ed448_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, key3,
  33308. NULL, 0);
  33309. if (ret != 0 || verify != 1)
  33310. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33311. #endif /* HAVE_ED448_VERIFY */
  33312. wc_ed448_free(key3);
  33313. ret = wc_ed448_init(key3);
  33314. if (ret < 0)
  33315. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33316. idx = 0;
  33317. ret = wc_Ed448PrivateKeyDecode(privPubEd448, &idx, key3,
  33318. sizeof(privPubEd448));
  33319. if (ret != 0)
  33320. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33321. ret = wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0);
  33322. if (ret != 0)
  33323. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33324. if (XMEMCMP(out, sigs[0], SIGSZ))
  33325. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33326. #endif /* NO_ASN */
  33327. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  33328. ret = 0;
  33329. out:
  33330. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33331. if (key) {
  33332. wc_ed448_free(key);
  33333. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33334. }
  33335. if (key2) {
  33336. wc_ed448_free(key2);
  33337. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33338. }
  33339. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  33340. if (key3) {
  33341. wc_ed448_free(key3);
  33342. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33343. }
  33344. #endif
  33345. #else
  33346. wc_ed448_free(key);
  33347. wc_ed448_free(key2);
  33348. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  33349. wc_ed448_free(key3);
  33350. #endif
  33351. #endif
  33352. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  33353. wc_FreeRng(&rng);
  33354. #endif
  33355. if (ret < 0)
  33356. return ret;
  33357. /* hush warnings of unused keySz and sigSz */
  33358. (void)keySz;
  33359. (void)sigSz;
  33360. ret = ed448_test_check_key();
  33361. if (ret < 0)
  33362. return ret;
  33363. #ifdef WOLFSSL_TEST_CERT
  33364. ret = ed448_test_cert();
  33365. if (ret < 0)
  33366. return ret;
  33367. #ifdef WOLFSSL_CERT_GEN
  33368. ret = ed448_test_make_cert();
  33369. if (ret < 0)
  33370. return ret;
  33371. #endif /* WOLFSSL_CERT_GEN */
  33372. #endif /* WOLFSSL_TEST_CERT */
  33373. return 0;
  33374. }
  33375. #endif /* HAVE_ED448 */
  33376. #ifdef WOLFSSL_HAVE_KYBER
  33377. #ifdef WOLFSSL_WC_KYBER /* OQS and PQM4 do not support KATs */
  33378. #ifdef WOLFSSL_KYBER512
  33379. static wc_test_ret_t kyber512_kat(void)
  33380. {
  33381. wc_test_ret_t ret;
  33382. #ifdef WOLFSSL_SMALL_STACK
  33383. KyberKey *key = NULL;
  33384. byte *priv = NULL;
  33385. byte *pub = NULL;
  33386. byte *ct = NULL;
  33387. byte *ss = NULL;
  33388. byte *ss_dec = NULL;
  33389. #else
  33390. KyberKey key[1];
  33391. byte priv[KYBER512_PRIVATE_KEY_SIZE];
  33392. byte pub[KYBER512_PUBLIC_KEY_SIZE];
  33393. byte ct[KYBER512_CIPHER_TEXT_SIZE];
  33394. byte ss[KYBER_SS_SZ];
  33395. byte ss_dec[KYBER_SS_SZ];
  33396. #endif
  33397. int key_inited = 0;
  33398. WOLFSSL_SMALL_STACK_STATIC const byte kyber512_rand[] = {
  33399. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  33400. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  33401. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  33402. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  33403. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  33404. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  33405. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  33406. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  33407. };
  33408. WOLFSSL_SMALL_STACK_STATIC const byte kyber512enc_rand[] = {
  33409. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  33410. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  33411. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  33412. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  33413. };
  33414. WOLFSSL_SMALL_STACK_STATIC const byte kyber512_pk[] = {
  33415. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  33416. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  33417. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  33418. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  33419. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  33420. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  33421. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  33422. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  33423. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  33424. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  33425. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  33426. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  33427. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  33428. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  33429. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  33430. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  33431. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  33432. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  33433. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  33434. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  33435. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  33436. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  33437. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  33438. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  33439. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  33440. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  33441. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  33442. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  33443. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  33444. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  33445. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  33446. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  33447. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  33448. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  33449. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  33450. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  33451. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  33452. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  33453. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  33454. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  33455. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  33456. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  33457. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  33458. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  33459. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  33460. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  33461. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  33462. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  33463. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  33464. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  33465. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  33466. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  33467. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  33468. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  33469. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  33470. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  33471. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  33472. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  33473. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  33474. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  33475. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  33476. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  33477. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  33478. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  33479. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  33480. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  33481. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  33482. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  33483. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  33484. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  33485. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  33486. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  33487. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  33488. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  33489. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  33490. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  33491. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  33492. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  33493. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  33494. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  33495. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  33496. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  33497. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  33498. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  33499. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  33500. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  33501. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  33502. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  33503. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  33504. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  33505. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  33506. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  33507. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  33508. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  33509. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  33510. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  33511. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  33512. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  33513. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  33514. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  33515. };
  33516. WOLFSSL_SMALL_STACK_STATIC const byte kyber512_sk[] = {
  33517. 0x6C, 0x89, 0x2B, 0x02, 0x97, 0xA9, 0xC7, 0x64,
  33518. 0x14, 0x93, 0xF8, 0x7D, 0xAF, 0x35, 0x33, 0xEE,
  33519. 0xD6, 0x1F, 0x07, 0xF4, 0x65, 0x20, 0x66, 0x33,
  33520. 0x7E, 0xD7, 0x40, 0x46, 0xDC, 0xC7, 0x1B, 0xA0,
  33521. 0x3F, 0x30, 0x96, 0x01, 0x03, 0x16, 0x1F, 0x7D,
  33522. 0xEB, 0x53, 0xA7, 0x1B, 0x11, 0x61, 0x72, 0x63,
  33523. 0xFE, 0x2A, 0x80, 0x97, 0x69, 0xCE, 0x6D, 0x70,
  33524. 0xA8, 0x5F, 0xE6, 0x00, 0xEC, 0xE2, 0x9D, 0x7F,
  33525. 0x36, 0xA1, 0x6D, 0x33, 0x1B, 0x8B, 0x2A, 0x9E,
  33526. 0x1D, 0xB8, 0xC0, 0x90, 0x74, 0x2D, 0xF0, 0x73,
  33527. 0x9F, 0xF0, 0x60, 0xCE, 0xB4, 0xEC, 0xC5, 0xAB,
  33528. 0x1C, 0x5E, 0x55, 0xAC, 0x97, 0xBB, 0x66, 0xA7,
  33529. 0xF8, 0x95, 0x10, 0x5D, 0x57, 0x78, 0x2B, 0x22,
  33530. 0x95, 0x38, 0xE3, 0x42, 0x15, 0x44, 0xA3, 0x42,
  33531. 0x14, 0x08, 0xDB, 0xF4, 0x49, 0x10, 0x93, 0x4C,
  33532. 0xC4, 0x23, 0x77, 0x4F, 0x16, 0x76, 0xFF, 0x1C,
  33533. 0x30, 0x6F, 0x97, 0x55, 0x5F, 0x57, 0xB4, 0xAE,
  33534. 0xD7, 0xA6, 0xBA, 0xB9, 0x50, 0xA8, 0x16, 0x3C,
  33535. 0x8D, 0x31, 0x8D, 0xEA, 0x62, 0x75, 0x1B, 0xD6,
  33536. 0xAB, 0xC5, 0x06, 0x9C, 0x06, 0xC8, 0x8F, 0x33,
  33537. 0x00, 0x26, 0xA1, 0x98, 0x06, 0xA0, 0x3B, 0x97,
  33538. 0xA7, 0x69, 0x6B, 0x56, 0xDA, 0x21, 0x82, 0x7B,
  33539. 0xB4, 0xE8, 0xDC, 0x03, 0x11, 0x52, 0xB4, 0x1B,
  33540. 0x89, 0x2A, 0x9E, 0x99, 0xAD, 0xF6, 0xE1, 0x96,
  33541. 0x3E, 0x96, 0x57, 0x88, 0x28, 0x15, 0x4F, 0x46,
  33542. 0x70, 0x33, 0x84, 0x69, 0x20, 0xFB, 0xB4, 0xB8,
  33543. 0x05, 0x44, 0xE7, 0xE8, 0xA8, 0x1A, 0xE9, 0x63,
  33544. 0xCF, 0x36, 0x8C, 0x9B, 0xA0, 0x37, 0xA8, 0xC2,
  33545. 0xAD, 0x62, 0xE3, 0x2B, 0x6E, 0x61, 0xC9, 0x1D,
  33546. 0x75, 0xCE, 0x00, 0x5A, 0xB3, 0x0F, 0x80, 0x99,
  33547. 0xA1, 0xF2, 0x9D, 0x7B, 0x63, 0x05, 0xB4, 0xDC,
  33548. 0x06, 0xE2, 0x56, 0x80, 0xBB, 0x00, 0x99, 0x2F,
  33549. 0x71, 0x7F, 0xE6, 0xC1, 0x15, 0xA8, 0x08, 0x42,
  33550. 0x31, 0xCC, 0x79, 0xDD, 0x70, 0x0E, 0xA6, 0x91,
  33551. 0x2A, 0xC7, 0xFA, 0x0D, 0x93, 0x7B, 0xB6, 0xA7,
  33552. 0x56, 0x66, 0x22, 0x30, 0x47, 0x0C, 0x18, 0x9B,
  33553. 0x5A, 0xA1, 0x65, 0x3D, 0xEB, 0x93, 0x7D, 0x5A,
  33554. 0x9C, 0x25, 0xA2, 0x1D, 0x93, 0xB1, 0x90, 0x74,
  33555. 0xFC, 0x23, 0x9D, 0x81, 0x53, 0x53, 0x97, 0x97,
  33556. 0xC7, 0xD4, 0xAB, 0x62, 0x64, 0x9D, 0x76, 0xAA,
  33557. 0x55, 0x37, 0x36, 0xA9, 0x49, 0x02, 0x2C, 0x22,
  33558. 0xC5, 0x2B, 0xAE, 0xEC, 0x60, 0x5B, 0x32, 0xCE,
  33559. 0x9E, 0x5B, 0x93, 0x84, 0x90, 0x35, 0x58, 0xCA,
  33560. 0x9D, 0x6A, 0x3A, 0xBA, 0x90, 0x42, 0x3E, 0xED,
  33561. 0xA0, 0x1C, 0x94, 0x19, 0x8B, 0x19, 0x2A, 0x8B,
  33562. 0xA9, 0x06, 0x34, 0x97, 0xA0, 0xC5, 0x01, 0x33,
  33563. 0x07, 0xDD, 0xD8, 0x63, 0x52, 0x64, 0x71, 0xA4,
  33564. 0xD9, 0x95, 0x23, 0xEB, 0x41, 0x7F, 0x29, 0x1A,
  33565. 0xAC, 0x0C, 0x3A, 0x58, 0x1B, 0x6D, 0xA0, 0x07,
  33566. 0x32, 0xE5, 0xE8, 0x1B, 0x1F, 0x7C, 0x87, 0x9B,
  33567. 0x16, 0x93, 0xC1, 0x3B, 0x6F, 0x9F, 0x79, 0x31,
  33568. 0x62, 0x24, 0x29, 0xE5, 0x42, 0xAF, 0x40, 0x69,
  33569. 0x22, 0x2F, 0x04, 0x55, 0x44, 0xE0, 0xCC, 0x4F,
  33570. 0xB2, 0x4D, 0x44, 0x48, 0xCF, 0x2C, 0x65, 0x96,
  33571. 0xF5, 0xCB, 0x08, 0x62, 0x4B, 0x11, 0x85, 0x01,
  33572. 0x3B, 0x6B, 0x02, 0x08, 0x92, 0xF9, 0x6B, 0xDF,
  33573. 0xD4, 0xAD, 0xA9, 0x17, 0x9D, 0xE7, 0x27, 0xB8,
  33574. 0xD9, 0x42, 0x6E, 0x09, 0x96, 0xB5, 0xD3, 0x49,
  33575. 0x48, 0xCE, 0x02, 0xD0, 0xC3, 0x69, 0xB3, 0x7C,
  33576. 0xBB, 0x54, 0xD3, 0x47, 0x9E, 0xD8, 0xB5, 0x82,
  33577. 0xE9, 0xE7, 0x28, 0x92, 0x9B, 0x4C, 0x71, 0xC9,
  33578. 0xBE, 0x11, 0xD4, 0x5B, 0x20, 0xC4, 0xBD, 0xC3,
  33579. 0xC7, 0x43, 0x13, 0x22, 0x3F, 0x58, 0x27, 0x4E,
  33580. 0x8B, 0xA5, 0x24, 0x44, 0x47, 0xC4, 0x95, 0x95,
  33581. 0x0B, 0x84, 0xCB, 0x0C, 0x3C, 0x27, 0x36, 0x40,
  33582. 0x10, 0x8A, 0x33, 0x97, 0x94, 0x45, 0x73, 0x27,
  33583. 0x93, 0x28, 0x99, 0x6C, 0xDC, 0x0C, 0x91, 0x3C,
  33584. 0x95, 0x8A, 0xD6, 0x20, 0xBA, 0x8B, 0x5E, 0x5E,
  33585. 0xCB, 0xBB, 0x7E, 0x13, 0xCB, 0x9C, 0x70, 0xBD,
  33586. 0x5A, 0xB3, 0x0E, 0xB7, 0x48, 0x8C, 0x97, 0x00,
  33587. 0x1C, 0x20, 0x49, 0x8F, 0x1D, 0x7C, 0xC0, 0x6D,
  33588. 0xA7, 0x6B, 0xF5, 0x20, 0xC6, 0x58, 0xCC, 0xAD,
  33589. 0xFA, 0x29, 0x56, 0x42, 0x45, 0x57, 0xAB, 0xEA,
  33590. 0x8A, 0xB8, 0x92, 0x39, 0xC1, 0x78, 0x33, 0xDC,
  33591. 0x3A, 0x49, 0xB3, 0x6A, 0x9A, 0xE9, 0xA4, 0x86,
  33592. 0x94, 0x05, 0x40, 0xEB, 0x44, 0x4F, 0x97, 0x15,
  33593. 0x23, 0x57, 0xE0, 0x20, 0x35, 0x93, 0x9D, 0x75,
  33594. 0xA3, 0xC0, 0x25, 0xF4, 0x1A, 0x40, 0x08, 0x23,
  33595. 0x82, 0xA0, 0x73, 0x3C, 0x39, 0xB0, 0x62, 0x2B,
  33596. 0x74, 0x0E, 0x40, 0x75, 0x92, 0xC6, 0x2E, 0xCA,
  33597. 0xEB, 0x14, 0x32, 0xC4, 0x45, 0xB3, 0x70, 0x3A,
  33598. 0x86, 0xF6, 0x98, 0x1A, 0x27, 0x81, 0x57, 0xEA,
  33599. 0x95, 0xA6, 0xE9, 0x2D, 0x55, 0xE4, 0xB9, 0x72,
  33600. 0xF9, 0x36, 0xC2, 0xF0, 0xA6, 0x58, 0x28, 0x0E,
  33601. 0xA2, 0xB0, 0x7A, 0x48, 0x99, 0x2D, 0xF8, 0x93,
  33602. 0x7E, 0x0A, 0x2A, 0xC1, 0xDC, 0xC9, 0x74, 0xFE,
  33603. 0x00, 0xAA, 0xE1, 0xF5, 0x61, 0xFA, 0x25, 0x8E,
  33604. 0x2D, 0x25, 0x9C, 0x3E, 0x86, 0x1D, 0xCE, 0x23,
  33605. 0x60, 0x39, 0x12, 0x76, 0x06, 0xFC, 0x1C, 0xE0,
  33606. 0x09, 0x00, 0x3A, 0x7B, 0xAC, 0x94, 0x21, 0x01,
  33607. 0xDC, 0xB8, 0x22, 0xB1, 0xF3, 0xC1, 0x2B, 0xF7,
  33608. 0x32, 0x38, 0xF5, 0x46, 0xE0, 0x1C, 0x36, 0xB5,
  33609. 0xA6, 0x93, 0x61, 0x92, 0x99, 0x5C, 0xC6, 0x9C,
  33610. 0x63, 0x23, 0x74, 0x09, 0xCB, 0x53, 0xC2, 0xE3,
  33611. 0x5D, 0x74, 0x89, 0x0D, 0x18, 0x88, 0x53, 0x76,
  33612. 0xFA, 0x55, 0x03, 0xB1, 0x07, 0xA2, 0xA3, 0x92,
  33613. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  33614. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  33615. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  33616. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  33617. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  33618. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  33619. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  33620. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  33621. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  33622. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  33623. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  33624. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  33625. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  33626. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  33627. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  33628. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  33629. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  33630. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  33631. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  33632. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  33633. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  33634. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  33635. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  33636. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  33637. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  33638. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  33639. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  33640. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  33641. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  33642. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  33643. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  33644. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  33645. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  33646. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  33647. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  33648. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  33649. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  33650. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  33651. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  33652. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  33653. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  33654. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  33655. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  33656. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  33657. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  33658. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  33659. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  33660. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  33661. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  33662. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  33663. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  33664. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  33665. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  33666. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  33667. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  33668. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  33669. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  33670. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  33671. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  33672. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  33673. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  33674. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  33675. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  33676. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  33677. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  33678. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  33679. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  33680. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  33681. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  33682. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  33683. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  33684. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  33685. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  33686. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  33687. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  33688. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  33689. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  33690. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  33691. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  33692. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  33693. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  33694. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  33695. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  33696. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  33697. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  33698. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  33699. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  33700. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  33701. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  33702. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  33703. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  33704. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  33705. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  33706. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  33707. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  33708. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  33709. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  33710. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  33711. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  33712. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  33713. 0x7F, 0xFA, 0xD1, 0xBC, 0x8A, 0xF7, 0x3B, 0x7E,
  33714. 0x87, 0x49, 0x56, 0xB8, 0x1C, 0x2A, 0x2E, 0xF0,
  33715. 0xBF, 0xAB, 0xE8, 0xDC, 0x93, 0xD7, 0x7B, 0x2F,
  33716. 0xBC, 0x9E, 0x0C, 0x64, 0xEF, 0xA0, 0x1E, 0x84,
  33717. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  33718. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  33719. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  33720. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  33721. };
  33722. WOLFSSL_SMALL_STACK_STATIC const byte kyber512_ct[] = {
  33723. 0xED, 0xF2, 0x41, 0x45, 0xE4, 0x3B, 0x4F, 0x6D,
  33724. 0xC6, 0xBF, 0x83, 0x32, 0xF5, 0x4E, 0x02, 0xCA,
  33725. 0xB0, 0x2D, 0xBF, 0x3B, 0x56, 0x05, 0xDD, 0xC9,
  33726. 0x0A, 0x15, 0xC8, 0x86, 0xAD, 0x3E, 0xD4, 0x89,
  33727. 0x46, 0x26, 0x99, 0xE4, 0xAB, 0xED, 0x44, 0x35,
  33728. 0x0B, 0xC3, 0x75, 0x7E, 0x26, 0x96, 0xFB, 0xFB,
  33729. 0x25, 0x34, 0x41, 0x2E, 0x8D, 0xD2, 0x01, 0xF1,
  33730. 0xE4, 0x54, 0x0A, 0x39, 0x70, 0xB0, 0x55, 0xFE,
  33731. 0x3B, 0x0B, 0xEC, 0x3A, 0x71, 0xF9, 0xE1, 0x15,
  33732. 0xB3, 0xF9, 0xF3, 0x91, 0x02, 0x06, 0x5B, 0x1C,
  33733. 0xCA, 0x83, 0x14, 0xDC, 0xC7, 0x95, 0xE3, 0xC0,
  33734. 0xE8, 0xFA, 0x98, 0xEE, 0x83, 0xCA, 0x66, 0x28,
  33735. 0x45, 0x70, 0x28, 0xA4, 0xD0, 0x9E, 0x83, 0x9E,
  33736. 0x55, 0x48, 0x62, 0xCF, 0x0B, 0x7B, 0xF5, 0x6C,
  33737. 0x5C, 0x0A, 0x82, 0x9E, 0x86, 0x57, 0x94, 0x79,
  33738. 0x45, 0xFE, 0x9C, 0x22, 0x56, 0x4F, 0xBA, 0xEB,
  33739. 0xC1, 0xB3, 0xAF, 0x35, 0x0D, 0x79, 0x55, 0x50,
  33740. 0x8A, 0x26, 0xD8, 0xA8, 0xEB, 0x54, 0x7B, 0x8B,
  33741. 0x1A, 0x2C, 0xF0, 0x3C, 0xCA, 0x1A, 0xAB, 0xCE,
  33742. 0x6C, 0x34, 0x97, 0x78, 0x3B, 0x64, 0x65, 0xBA,
  33743. 0x0B, 0x6E, 0x7A, 0xCB, 0xA8, 0x21, 0x19, 0x51,
  33744. 0x24, 0xAE, 0xF0, 0x9E, 0x62, 0x83, 0x82, 0xA1,
  33745. 0xF9, 0x14, 0x04, 0x3B, 0xE7, 0x09, 0x6E, 0x95,
  33746. 0x2C, 0xBC, 0x4F, 0xB4, 0xAF, 0xED, 0x13, 0x60,
  33747. 0x90, 0x46, 0x11, 0x7C, 0x01, 0x1F, 0xD7, 0x41,
  33748. 0xEE, 0x28, 0x6C, 0x83, 0x77, 0x16, 0x90, 0xF0,
  33749. 0xAE, 0xB5, 0x0D, 0xA0, 0xD7, 0x12, 0x85, 0xA1,
  33750. 0x79, 0xB2, 0x15, 0xC6, 0x03, 0x6D, 0xEB, 0x78,
  33751. 0x0F, 0x4D, 0x16, 0x76, 0x9F, 0x72, 0xDE, 0x16,
  33752. 0xFD, 0xAD, 0xAC, 0x73, 0xBE, 0xFA, 0x5B, 0xEF,
  33753. 0x89, 0x43, 0x19, 0x7F, 0x44, 0xC5, 0x95, 0x89,
  33754. 0xDC, 0x9F, 0x49, 0x73, 0xDE, 0x14, 0x50, 0xBA,
  33755. 0x1D, 0x0C, 0x32, 0x90, 0xD6, 0xB1, 0xD6, 0x83,
  33756. 0xF2, 0x94, 0xE7, 0x59, 0xC9, 0x54, 0xAB, 0xE8,
  33757. 0xA7, 0xDA, 0x5B, 0x10, 0x54, 0xFD, 0x6D, 0x21,
  33758. 0x32, 0x9B, 0x8E, 0x73, 0xD3, 0x75, 0x6A, 0xFD,
  33759. 0xA0, 0xDC, 0xB1, 0xFC, 0x8B, 0x15, 0x82, 0xD1,
  33760. 0xF9, 0x0C, 0xF2, 0x75, 0xA1, 0x02, 0xAB, 0xC6,
  33761. 0xAC, 0x69, 0x9D, 0xF0, 0xC5, 0x87, 0x0E, 0x50,
  33762. 0xA1, 0xF9, 0x89, 0xE4, 0xE6, 0x24, 0x1B, 0x60,
  33763. 0xAA, 0xA2, 0xEC, 0xF9, 0xE8, 0xE3, 0x3E, 0x0F,
  33764. 0xFC, 0xF4, 0x0F, 0xE8, 0x31, 0xE8, 0xFD, 0xC2,
  33765. 0xE8, 0x3B, 0x52, 0xCA, 0x7A, 0xB6, 0xD9, 0x3F,
  33766. 0x14, 0x6D, 0x29, 0xDC, 0xA5, 0x3C, 0x7D, 0xA1,
  33767. 0xDB, 0x4A, 0xC4, 0xF2, 0xDB, 0x39, 0xEA, 0x12,
  33768. 0x0D, 0x90, 0xFA, 0x60, 0xF4, 0xD4, 0x37, 0xC6,
  33769. 0xD0, 0x0E, 0xF4, 0x83, 0xBC, 0x94, 0xA3, 0x17,
  33770. 0x5C, 0xDA, 0x16, 0x3F, 0xC1, 0xC2, 0x82, 0x8B,
  33771. 0xE4, 0xDB, 0xD6, 0x43, 0x05, 0x07, 0xB5, 0x84,
  33772. 0xBB, 0x51, 0x77, 0xE1, 0x71, 0xB8, 0xDD, 0xA9,
  33773. 0xA4, 0x29, 0x3C, 0x32, 0x00, 0x29, 0x5C, 0x80,
  33774. 0x3A, 0x86, 0x5D, 0x6D, 0x21, 0x66, 0xF6, 0x6B,
  33775. 0xA5, 0x40, 0x1F, 0xB7, 0xA0, 0xE8, 0x53, 0x16,
  33776. 0x86, 0x00, 0xA2, 0x94, 0x84, 0x37, 0xE0, 0x36,
  33777. 0xE3, 0xBF, 0x19, 0xE1, 0x2F, 0xD3, 0xF2, 0xA2,
  33778. 0xB8, 0xB3, 0x43, 0xF7, 0x84, 0x24, 0x8E, 0x8D,
  33779. 0x68, 0x5E, 0xB0, 0xAF, 0xDE, 0x63, 0x15, 0x33,
  33780. 0x87, 0x30, 0xE7, 0xA1, 0x00, 0x1C, 0x27, 0xD8,
  33781. 0xD2, 0xA7, 0x6F, 0xA6, 0x9D, 0x15, 0x7B, 0xA1,
  33782. 0xAC, 0x7A, 0xD5, 0x6D, 0xA5, 0xA8, 0xC7, 0x0F,
  33783. 0xE4, 0xB5, 0xB8, 0xD7, 0x86, 0xDC, 0x6F, 0xC0,
  33784. 0x56, 0x6B, 0xA8, 0xE1, 0xB8, 0x81, 0x63, 0x34,
  33785. 0xD3, 0x2A, 0x3F, 0xB1, 0xCE, 0x7D, 0x4D, 0x5E,
  33786. 0x4C, 0x33, 0x2A, 0xF7, 0xB0, 0x03, 0xD0, 0x91,
  33787. 0x74, 0x1A, 0x3D, 0x5C, 0x96, 0x52, 0x92, 0x25,
  33788. 0x5D, 0xFF, 0x8E, 0xD2, 0xBB, 0xF1, 0xF9, 0x11,
  33789. 0x6B, 0xE5, 0x0C, 0x17, 0xB8, 0xE5, 0x48, 0x74,
  33790. 0x8A, 0xD4, 0xB2, 0xE9, 0x57, 0xBB, 0xD1, 0x95,
  33791. 0x34, 0x82, 0xA2, 0xE1, 0x71, 0x8C, 0xEC, 0x66,
  33792. 0xCD, 0x2C, 0x81, 0xF5, 0x72, 0xD5, 0x52, 0xB7,
  33793. 0x18, 0x78, 0x85, 0xE6, 0xB8, 0x94, 0x3D, 0x64,
  33794. 0x31, 0x41, 0x3C, 0x59, 0xEB, 0xB7, 0xE0, 0x36,
  33795. 0x04, 0x84, 0x90, 0xBE, 0x52, 0x89, 0xE9, 0x5B,
  33796. 0x20, 0xA8, 0x9E, 0x8B, 0x15, 0x9F, 0x61, 0xA9,
  33797. 0xA9, 0x88, 0x6E, 0x14, 0x75, 0x68, 0xF4, 0xC9,
  33798. 0x02, 0x1F, 0x36, 0x2F, 0x02, 0x68, 0x8A, 0x1C,
  33799. 0x8C, 0x3B, 0xB0, 0xD2, 0x40, 0x86, 0x88, 0x0E,
  33800. 0x55, 0xB6, 0xED, 0xB4, 0x3F, 0x37, 0x45, 0xD2,
  33801. 0xC1, 0x66, 0xDC, 0x1C, 0xB7, 0x43, 0xC7, 0x6F,
  33802. 0xE6, 0xBE, 0x52, 0x3A, 0x89, 0x3C, 0xC7, 0x64,
  33803. 0xD1, 0x64, 0x35, 0xC3, 0x78, 0x51, 0x25, 0x2A,
  33804. 0x81, 0xE2, 0xFF, 0xBA, 0x0F, 0x18, 0x97, 0x1A,
  33805. 0x3D, 0xEE, 0x37, 0xD4, 0x87, 0x7C, 0xB9, 0x28,
  33806. 0xE3, 0x6E, 0x52, 0x35, 0x03, 0x7A, 0x6B, 0x20,
  33807. 0x57, 0x89, 0x7D, 0x51, 0x8A, 0x5F, 0x0E, 0x34,
  33808. 0x8E, 0x3A, 0xB6, 0xD5, 0xB5, 0x2D, 0xFC, 0x60,
  33809. 0x75, 0x7F, 0x3B, 0x41, 0xA4, 0xFE, 0xC7, 0x82,
  33810. 0x8F, 0x1D, 0xEE, 0xAF, 0x45, 0x87, 0xCC, 0xC8,
  33811. 0xEA, 0xDF, 0x64, 0x7F, 0x4D, 0x20, 0x3B, 0x2F,
  33812. 0xAA, 0x05, 0xA6, 0x49, 0xB5, 0x82, 0x34, 0x0C,
  33813. 0xB4, 0xCA, 0xCE, 0x57, 0xA3, 0x07, 0x11, 0xBE,
  33814. 0x75, 0x2F, 0xAC, 0xF0, 0x22, 0x7D, 0x0A, 0x80,
  33815. 0xC4, 0x12, 0x84, 0x42, 0xDD, 0xC5, 0x44, 0xBE,
  33816. 0x80, 0x5B, 0x9C, 0xFE, 0x8F, 0xE9, 0xB1, 0x23,
  33817. 0x7C, 0x80, 0xF9, 0x67, 0x87, 0xCD, 0x92, 0x81,
  33818. 0xCC, 0xF2, 0x70, 0xC1, 0xAF, 0xC0, 0x67, 0x0D
  33819. };
  33820. WOLFSSL_SMALL_STACK_STATIC const byte kyber512_ss[] = {
  33821. 0x0A, 0x69, 0x25, 0x67, 0x6F, 0x24, 0xB2, 0x2C,
  33822. 0x28, 0x6F, 0x4C, 0x81, 0xA4, 0x22, 0x4C, 0xEC,
  33823. 0x50, 0x6C, 0x9B, 0x25, 0x7D, 0x48, 0x0E, 0x02,
  33824. 0xE3, 0xB4, 0x9F, 0x44, 0xCA, 0xA3, 0x23, 0x7F
  33825. };
  33826. #ifdef WOLFSSL_SMALL_STACK
  33827. key = (KyberKey *)XMALLOC(sizeof(KyberKey), HEAP_HINT,
  33828. DYNAMIC_TYPE_TMP_BUFFER);
  33829. priv = (byte *)XMALLOC(KYBER512_PRIVATE_KEY_SIZE, HEAP_HINT,
  33830. DYNAMIC_TYPE_TMP_BUFFER);
  33831. pub = (byte *)XMALLOC(KYBER512_PUBLIC_KEY_SIZE, HEAP_HINT,
  33832. DYNAMIC_TYPE_TMP_BUFFER);
  33833. ct = (byte *)XMALLOC(KYBER512_CIPHER_TEXT_SIZE, HEAP_HINT,
  33834. DYNAMIC_TYPE_TMP_BUFFER);
  33835. ss = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  33836. DYNAMIC_TYPE_TMP_BUFFER);
  33837. ss_dec = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  33838. DYNAMIC_TYPE_TMP_BUFFER);
  33839. if (! (key && priv && pub && ct && ss && ss_dec))
  33840. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  33841. #endif
  33842. ret = wc_KyberKey_Init(KYBER512, key, HEAP_HINT, INVALID_DEVID);
  33843. if (ret != 0)
  33844. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33845. else
  33846. key_inited = 1;
  33847. ret = wc_KyberKey_MakeKeyWithRandom(key, kyber512_rand,
  33848. sizeof(kyber512_rand));
  33849. if (ret != 0)
  33850. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33851. ret = wc_KyberKey_EncodePublicKey(key, pub, KYBER512_PUBLIC_KEY_SIZE);
  33852. if (ret != 0)
  33853. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33854. ret = wc_KyberKey_EncodePrivateKey(key, priv, KYBER512_PRIVATE_KEY_SIZE);
  33855. if (ret != 0)
  33856. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33857. if (XMEMCMP(pub, kyber512_pk, sizeof(kyber512_pk)) != 0)
  33858. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33859. if (XMEMCMP(priv, kyber512_sk, sizeof(kyber512_sk)) != 0)
  33860. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33861. ret = wc_KyberKey_EncapsulateWithRandom(key, ct, ss, kyber512enc_rand,
  33862. sizeof(kyber512enc_rand));
  33863. if (ret != 0)
  33864. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33865. if (XMEMCMP(ct, kyber512_ct, sizeof(kyber512_ct)) != 0)
  33866. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33867. if (XMEMCMP(ss, kyber512_ss, sizeof(kyber512_ss)) != 0)
  33868. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33869. ret = wc_KyberKey_Decapsulate(key, ss_dec, ct, sizeof(kyber512_ct));
  33870. if (ret != 0)
  33871. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33872. if (XMEMCMP(ss_dec, kyber512_ss, sizeof(kyber512_ss)) != 0)
  33873. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33874. out:
  33875. if (key_inited)
  33876. wc_KyberKey_Free(key);
  33877. #ifdef WOLFSSL_SMALL_STACK
  33878. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33879. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33880. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33881. XFREE(ct, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33882. XFREE(ss, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33883. XFREE(ss_dec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33884. #endif
  33885. return ret;
  33886. }
  33887. #endif /* WOLFSSL_KYBER512 */
  33888. #ifdef WOLFSSL_KYBER768
  33889. static wc_test_ret_t kyber768_kat(void)
  33890. {
  33891. wc_test_ret_t ret;
  33892. #ifdef WOLFSSL_SMALL_STACK
  33893. KyberKey *key = NULL;
  33894. byte *priv = NULL;
  33895. byte *pub = NULL;
  33896. byte *ct = NULL;
  33897. byte *ss = NULL;
  33898. byte *ss_dec = NULL;
  33899. #else
  33900. KyberKey key[1];
  33901. byte priv[KYBER768_PRIVATE_KEY_SIZE];
  33902. byte pub[KYBER768_PUBLIC_KEY_SIZE];
  33903. byte ct[KYBER768_CIPHER_TEXT_SIZE];
  33904. byte ss[KYBER_SS_SZ];
  33905. byte ss_dec[KYBER_SS_SZ];
  33906. #endif
  33907. int key_inited = 0;
  33908. WOLFSSL_SMALL_STACK_STATIC const byte kyber768_rand[] = {
  33909. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  33910. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  33911. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  33912. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  33913. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  33914. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  33915. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  33916. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  33917. };
  33918. WOLFSSL_SMALL_STACK_STATIC const byte kyber768enc_rand[] = {
  33919. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  33920. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  33921. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  33922. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  33923. };
  33924. WOLFSSL_SMALL_STACK_STATIC const byte kyber768_pk[] = {
  33925. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  33926. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  33927. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  33928. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  33929. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  33930. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  33931. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  33932. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  33933. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  33934. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  33935. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  33936. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  33937. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  33938. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  33939. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  33940. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  33941. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  33942. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  33943. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  33944. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  33945. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  33946. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  33947. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  33948. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  33949. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  33950. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  33951. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  33952. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  33953. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  33954. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  33955. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  33956. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  33957. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  33958. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  33959. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  33960. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  33961. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  33962. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  33963. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  33964. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  33965. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  33966. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  33967. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  33968. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  33969. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  33970. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  33971. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  33972. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  33973. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  33974. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  33975. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  33976. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  33977. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  33978. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  33979. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  33980. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  33981. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  33982. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  33983. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  33984. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  33985. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  33986. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  33987. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  33988. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  33989. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  33990. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  33991. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  33992. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  33993. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  33994. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  33995. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  33996. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  33997. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  33998. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  33999. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  34000. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  34001. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  34002. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  34003. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  34004. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  34005. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  34006. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  34007. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  34008. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  34009. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  34010. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  34011. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  34012. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  34013. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  34014. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  34015. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  34016. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  34017. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  34018. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  34019. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  34020. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  34021. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  34022. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  34023. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  34024. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  34025. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  34026. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  34027. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  34028. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  34029. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  34030. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  34031. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  34032. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  34033. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  34034. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  34035. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  34036. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  34037. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  34038. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  34039. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  34040. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  34041. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  34042. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  34043. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  34044. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  34045. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  34046. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  34047. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  34048. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  34049. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  34050. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  34051. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  34052. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  34053. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  34054. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  34055. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  34056. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  34057. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  34058. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  34059. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  34060. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  34061. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  34062. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  34063. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  34064. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  34065. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  34066. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  34067. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  34068. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  34069. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  34070. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  34071. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  34072. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  34073. };
  34074. WOLFSSL_SMALL_STACK_STATIC const byte kyber768_sk[] = {
  34075. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  34076. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  34077. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  34078. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  34079. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  34080. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  34081. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  34082. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  34083. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  34084. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  34085. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  34086. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  34087. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  34088. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  34089. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  34090. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  34091. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  34092. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  34093. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  34094. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  34095. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  34096. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  34097. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  34098. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  34099. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  34100. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  34101. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  34102. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  34103. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  34104. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  34105. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  34106. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  34107. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  34108. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  34109. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  34110. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  34111. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  34112. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  34113. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  34114. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  34115. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  34116. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  34117. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  34118. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  34119. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  34120. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  34121. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  34122. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  34123. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  34124. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  34125. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  34126. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  34127. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  34128. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  34129. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  34130. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  34131. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  34132. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  34133. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  34134. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  34135. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  34136. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  34137. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  34138. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  34139. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  34140. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  34141. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  34142. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  34143. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  34144. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  34145. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  34146. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  34147. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  34148. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  34149. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  34150. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  34151. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  34152. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  34153. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  34154. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  34155. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  34156. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  34157. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  34158. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  34159. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  34160. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  34161. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  34162. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  34163. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  34164. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  34165. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  34166. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  34167. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  34168. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  34169. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  34170. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  34171. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  34172. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  34173. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  34174. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  34175. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  34176. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  34177. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  34178. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  34179. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  34180. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  34181. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  34182. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  34183. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  34184. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  34185. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  34186. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  34187. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  34188. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  34189. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  34190. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  34191. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  34192. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  34193. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  34194. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  34195. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  34196. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  34197. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  34198. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  34199. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  34200. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  34201. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  34202. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  34203. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  34204. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  34205. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  34206. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  34207. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  34208. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  34209. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  34210. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  34211. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  34212. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  34213. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  34214. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  34215. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  34216. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  34217. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  34218. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  34219. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  34220. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  34221. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  34222. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  34223. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  34224. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  34225. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  34226. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  34227. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  34228. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  34229. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  34230. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  34231. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  34232. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  34233. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  34234. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  34235. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  34236. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  34237. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  34238. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  34239. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  34240. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  34241. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  34242. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  34243. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  34244. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  34245. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  34246. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  34247. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  34248. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  34249. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  34250. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  34251. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  34252. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  34253. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  34254. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  34255. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  34256. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  34257. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  34258. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  34259. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  34260. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  34261. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  34262. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  34263. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  34264. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  34265. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  34266. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  34267. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  34268. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  34269. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  34270. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  34271. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  34272. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  34273. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  34274. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  34275. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  34276. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  34277. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  34278. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  34279. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  34280. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  34281. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  34282. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  34283. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  34284. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  34285. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  34286. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  34287. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  34288. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  34289. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  34290. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  34291. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  34292. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  34293. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  34294. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  34295. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  34296. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  34297. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  34298. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  34299. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  34300. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  34301. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  34302. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  34303. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  34304. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  34305. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  34306. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  34307. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  34308. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  34309. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  34310. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  34311. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  34312. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  34313. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  34314. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  34315. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  34316. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  34317. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  34318. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  34319. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  34320. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  34321. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  34322. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  34323. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  34324. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  34325. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  34326. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  34327. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  34328. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  34329. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  34330. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  34331. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  34332. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  34333. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  34334. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  34335. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  34336. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  34337. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  34338. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  34339. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  34340. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  34341. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  34342. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  34343. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  34344. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  34345. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  34346. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  34347. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  34348. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  34349. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  34350. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  34351. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  34352. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  34353. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  34354. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  34355. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  34356. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  34357. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  34358. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  34359. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  34360. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  34361. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  34362. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  34363. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  34364. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  34365. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  34366. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  34367. 0xD4, 0xEC, 0x14, 0x3B, 0x50, 0xF0, 0x14, 0x23,
  34368. 0xB1, 0x77, 0x89, 0x5E, 0xDE, 0xE2, 0x2B, 0xB7,
  34369. 0x39, 0xF6, 0x47, 0xEC, 0xF8, 0x5F, 0x50, 0xBC,
  34370. 0x25, 0xEF, 0x7B, 0x5A, 0x72, 0x5D, 0xEE, 0x86,
  34371. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  34372. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  34373. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  34374. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  34375. };
  34376. WOLFSSL_SMALL_STACK_STATIC const byte kyber768_ct[] = {
  34377. 0xB5, 0x2C, 0x56, 0xB9, 0x2A, 0x4B, 0x7C, 0xE9,
  34378. 0xE4, 0xCB, 0x7C, 0x5B, 0x1B, 0x16, 0x31, 0x67,
  34379. 0xA8, 0xA1, 0x67, 0x5B, 0x2F, 0xDE, 0xF8, 0x4A,
  34380. 0x5B, 0x67, 0xCA, 0x15, 0xDB, 0x69, 0x4C, 0x9F,
  34381. 0x11, 0xBD, 0x02, 0x7C, 0x30, 0xAE, 0x22, 0xEC,
  34382. 0x92, 0x1A, 0x1D, 0x91, 0x15, 0x99, 0xAF, 0x05,
  34383. 0x85, 0xE4, 0x8D, 0x20, 0xDA, 0x70, 0xDF, 0x9F,
  34384. 0x39, 0xE3, 0x2E, 0xF9, 0x5D, 0x4C, 0x8F, 0x44,
  34385. 0xBF, 0xEF, 0xDA, 0xA5, 0xDA, 0x64, 0xF1, 0x05,
  34386. 0x46, 0x31, 0xD0, 0x4D, 0x6D, 0x3C, 0xFD, 0x0A,
  34387. 0x54, 0x0D, 0xD7, 0xBA, 0x38, 0x86, 0xE4, 0xB5,
  34388. 0xF1, 0x3E, 0x87, 0x87, 0x88, 0x60, 0x4C, 0x95,
  34389. 0xC0, 0x96, 0xEA, 0xB3, 0x91, 0x9F, 0x42, 0x75,
  34390. 0x21, 0x41, 0x9A, 0x94, 0x6C, 0x26, 0xCC, 0x04,
  34391. 0x14, 0x75, 0xD7, 0x12, 0x4C, 0xDC, 0x01, 0xD0,
  34392. 0x37, 0x3E, 0x5B, 0x09, 0xC7, 0xA7, 0x06, 0x03,
  34393. 0xCF, 0xDB, 0x4F, 0xB3, 0x40, 0x50, 0x23, 0xF2,
  34394. 0x26, 0x4D, 0xC3, 0xF9, 0x83, 0xC4, 0xFC, 0x02,
  34395. 0xA2, 0xD1, 0xB2, 0x68, 0xF2, 0x20, 0x8A, 0x1F,
  34396. 0x6E, 0x2A, 0x62, 0x09, 0xBF, 0xF1, 0x2F, 0x6F,
  34397. 0x46, 0x5F, 0x0B, 0x06, 0x9C, 0x3A, 0x7F, 0x84,
  34398. 0xF6, 0x06, 0xD8, 0xA9, 0x40, 0x64, 0x00, 0x3D,
  34399. 0x6E, 0xC1, 0x14, 0xC8, 0xE8, 0x08, 0xD3, 0x05,
  34400. 0x38, 0x84, 0xC1, 0xD5, 0xA1, 0x42, 0xFB, 0xF2,
  34401. 0x01, 0x12, 0xEB, 0x36, 0x0F, 0xDA, 0x3F, 0x0F,
  34402. 0x28, 0xB1, 0x72, 0xAE, 0x50, 0xF5, 0xE7, 0xD8,
  34403. 0x38, 0x01, 0xFB, 0x3F, 0x00, 0x64, 0xB6, 0x87,
  34404. 0x18, 0x70, 0x74, 0xBD, 0x7F, 0xE3, 0x0E, 0xDD,
  34405. 0xAA, 0x33, 0x4C, 0xF8, 0xFC, 0x04, 0xFA, 0x8C,
  34406. 0xED, 0x89, 0x9C, 0xEA, 0xDE, 0x4B, 0x4F, 0x28,
  34407. 0xB6, 0x83, 0x72, 0xBA, 0xF9, 0x8F, 0xF4, 0x82,
  34408. 0xA4, 0x15, 0xB7, 0x31, 0x15, 0x5B, 0x75, 0xCE,
  34409. 0xB9, 0x76, 0xBE, 0x0E, 0xA0, 0x28, 0x5B, 0xA0,
  34410. 0x1A, 0x27, 0xF1, 0x85, 0x7A, 0x8F, 0xB3, 0x77,
  34411. 0xA3, 0xAE, 0x0C, 0x23, 0xB2, 0xAA, 0x9A, 0x07,
  34412. 0x9B, 0xFA, 0xBF, 0xF0, 0xD5, 0xB2, 0xF1, 0xCD,
  34413. 0x9B, 0x71, 0x8B, 0xEA, 0x03, 0xC4, 0x2F, 0x34,
  34414. 0x3A, 0x39, 0xB4, 0xF1, 0x42, 0xD0, 0x1A, 0xD8,
  34415. 0xAC, 0xBB, 0x50, 0xE3, 0x88, 0x53, 0xCF, 0x9A,
  34416. 0x50, 0xC8, 0xB4, 0x4C, 0x3C, 0xF6, 0x71, 0xA4,
  34417. 0xA9, 0x04, 0x3B, 0x26, 0xDD, 0xBB, 0x24, 0x95,
  34418. 0x9A, 0xD6, 0x71, 0x5C, 0x08, 0x52, 0x18, 0x55,
  34419. 0xC7, 0x9A, 0x23, 0xB9, 0xC3, 0xD6, 0x47, 0x17,
  34420. 0x49, 0xC4, 0x07, 0x25, 0xBD, 0xD5, 0xC2, 0x77,
  34421. 0x6D, 0x43, 0xAE, 0xD2, 0x02, 0x04, 0xBA, 0xA1,
  34422. 0x41, 0xEF, 0xB3, 0x30, 0x49, 0x17, 0x47, 0x4B,
  34423. 0x7F, 0x9F, 0x7A, 0x4B, 0x08, 0xB1, 0xA9, 0x3D,
  34424. 0xAE, 0xD9, 0x8C, 0x67, 0x49, 0x53, 0x59, 0xD3,
  34425. 0x7D, 0x67, 0xF7, 0x43, 0x8B, 0xEE, 0x5E, 0x43,
  34426. 0x58, 0x56, 0x34, 0xB2, 0x6C, 0x6B, 0x38, 0x10,
  34427. 0xD7, 0xCD, 0xCB, 0xC0, 0xF6, 0xEB, 0x87, 0x7A,
  34428. 0x60, 0x87, 0xE6, 0x8A, 0xCB, 0x84, 0x80, 0xD3,
  34429. 0xA8, 0xCF, 0x69, 0x00, 0x44, 0x7E, 0x49, 0xB4,
  34430. 0x17, 0xF1, 0x5A, 0x53, 0xB6, 0x07, 0xA0, 0xE2,
  34431. 0x16, 0xB8, 0x55, 0x97, 0x0D, 0x37, 0x40, 0x68,
  34432. 0x70, 0xB4, 0x56, 0x87, 0x22, 0xDA, 0x77, 0xA4,
  34433. 0x08, 0x47, 0x03, 0x81, 0x67, 0x84, 0xE2, 0xF1,
  34434. 0x6B, 0xED, 0x18, 0x99, 0x65, 0x32, 0xC5, 0xD8,
  34435. 0xB7, 0xF5, 0xD2, 0x14, 0x46, 0x4E, 0x5F, 0x3F,
  34436. 0x6E, 0x90, 0x58, 0x67, 0xB0, 0xCE, 0x11, 0x9E,
  34437. 0x25, 0x2A, 0x66, 0x71, 0x32, 0x53, 0x54, 0x46,
  34438. 0x85, 0xD2, 0x08, 0xE1, 0x72, 0x39, 0x08, 0xA0,
  34439. 0xCE, 0x97, 0x83, 0x46, 0x52, 0xE0, 0x8A, 0xE7,
  34440. 0xBD, 0xC8, 0x81, 0xA1, 0x31, 0xB7, 0x3C, 0x71,
  34441. 0xE8, 0x4D, 0x20, 0xD6, 0x8F, 0xDE, 0xFF, 0x4F,
  34442. 0x5D, 0x70, 0xCD, 0x1A, 0xF5, 0x7B, 0x78, 0xE3,
  34443. 0x49, 0x1A, 0x98, 0x65, 0x94, 0x23, 0x21, 0x80,
  34444. 0x0A, 0x20, 0x3C, 0x05, 0xED, 0x1F, 0xEE, 0xB5,
  34445. 0xA2, 0x8E, 0x58, 0x4E, 0x19, 0xF6, 0x53, 0x5E,
  34446. 0x7F, 0x84, 0xE4, 0xA2, 0x4F, 0x84, 0xA7, 0x2D,
  34447. 0xCA, 0xF5, 0x64, 0x8B, 0x4A, 0x42, 0x35, 0xDD,
  34448. 0x66, 0x44, 0x64, 0x48, 0x2F, 0x03, 0x17, 0x6E,
  34449. 0x88, 0x8C, 0x28, 0xBF, 0xC6, 0xC1, 0xCB, 0x23,
  34450. 0x8C, 0xFF, 0xA3, 0x5A, 0x32, 0x1E, 0x71, 0x79,
  34451. 0x1D, 0x9E, 0xA8, 0xED, 0x08, 0x78, 0xC6, 0x11,
  34452. 0x21, 0xBF, 0x8D, 0x2A, 0x4A, 0xB2, 0xC1, 0xA5,
  34453. 0xE1, 0x20, 0xBC, 0x40, 0xAB, 0xB1, 0x89, 0x2D,
  34454. 0x17, 0x15, 0x09, 0x0A, 0x0E, 0xE4, 0x82, 0x52,
  34455. 0xCA, 0x29, 0x7A, 0x99, 0xAA, 0x0E, 0x51, 0x0C,
  34456. 0xF2, 0x6B, 0x1A, 0xDD, 0x06, 0xCA, 0x54, 0x3E,
  34457. 0x1C, 0x5D, 0x6B, 0xDC, 0xD3, 0xB9, 0xC5, 0x85,
  34458. 0xC8, 0x53, 0x80, 0x45, 0xDB, 0x5C, 0x25, 0x2E,
  34459. 0xC3, 0xC8, 0xC3, 0xC9, 0x54, 0xD9, 0xBE, 0x59,
  34460. 0x07, 0x09, 0x4A, 0x89, 0x4E, 0x60, 0xEA, 0xB4,
  34461. 0x35, 0x38, 0xCF, 0xEE, 0x82, 0xE8, 0xFF, 0xC0,
  34462. 0x79, 0x1B, 0x0D, 0x0F, 0x43, 0xAC, 0x16, 0x27,
  34463. 0x83, 0x0A, 0x61, 0xD5, 0x6D, 0xAD, 0x96, 0xC6,
  34464. 0x29, 0x58, 0xB0, 0xDE, 0x78, 0x0B, 0x78, 0xBD,
  34465. 0x47, 0xA6, 0x04, 0x55, 0x0D, 0xAB, 0x83, 0xFF,
  34466. 0xF2, 0x27, 0xC3, 0x24, 0x04, 0x94, 0x71, 0xF3,
  34467. 0x52, 0x48, 0xCF, 0xB8, 0x49, 0xB2, 0x57, 0x24,
  34468. 0xFF, 0x70, 0x4D, 0x52, 0x77, 0xAA, 0x35, 0x2D,
  34469. 0x55, 0x09, 0x58, 0xBE, 0x3B, 0x23, 0x7D, 0xFF,
  34470. 0x47, 0x3E, 0xC2, 0xAD, 0xBA, 0xEA, 0x48, 0xCA,
  34471. 0x26, 0x58, 0xAE, 0xFC, 0xC7, 0x7B, 0xBD, 0x42,
  34472. 0x64, 0xAB, 0x37, 0x4D, 0x70, 0xEA, 0xE5, 0xB9,
  34473. 0x64, 0x41, 0x6C, 0xE8, 0x22, 0x6A, 0x7E, 0x32,
  34474. 0x55, 0xA0, 0xF8, 0xD7, 0xE2, 0xAD, 0xCA, 0x06,
  34475. 0x2B, 0xCD, 0x6D, 0x78, 0xD6, 0x0D, 0x1B, 0x32,
  34476. 0xE1, 0x14, 0x05, 0xBE, 0x54, 0xB6, 0x6E, 0xF0,
  34477. 0xFD, 0xDD, 0x56, 0x77, 0x02, 0xA3, 0xBC, 0xCF,
  34478. 0xED, 0xE3, 0xC5, 0x84, 0x70, 0x12, 0x69, 0xED,
  34479. 0x14, 0x80, 0x9F, 0x06, 0xF8, 0x96, 0x83, 0x56,
  34480. 0xBB, 0x92, 0x67, 0xFE, 0x86, 0xE5, 0x14, 0x25,
  34481. 0x2E, 0x88, 0xBB, 0x5C, 0x30, 0xA7, 0xEC, 0xB3,
  34482. 0xD0, 0xE6, 0x21, 0x02, 0x1E, 0xE0, 0xFB, 0xF7,
  34483. 0x87, 0x1B, 0x09, 0x34, 0x2B, 0xF8, 0x4F, 0x55,
  34484. 0xC9, 0x7E, 0xAF, 0x86, 0xC4, 0x81, 0x89, 0xC7,
  34485. 0xFF, 0x4D, 0xF3, 0x89, 0xF0, 0x77, 0xE2, 0x80,
  34486. 0x6E, 0x5F, 0xA7, 0x3B, 0x3E, 0x94, 0x58, 0xA1,
  34487. 0x6C, 0x7E, 0x27, 0x5F, 0x4F, 0x60, 0x22, 0x75,
  34488. 0x58, 0x0E, 0xB7, 0xB7, 0x13, 0x5F, 0xB5, 0x37,
  34489. 0xFA, 0x0C, 0xD9, 0x5D, 0x6E, 0xA5, 0x8C, 0x10,
  34490. 0x8C, 0xD8, 0x94, 0x3D, 0x70, 0xC1, 0x64, 0x31,
  34491. 0x11, 0xF4, 0xF0, 0x1C, 0xA8, 0xA8, 0x27, 0x6A,
  34492. 0x90, 0x26, 0x66, 0xED, 0x81, 0xB7, 0x8D, 0x16,
  34493. 0x8B, 0x00, 0x6F, 0x16, 0xAA, 0xA3, 0xD8, 0xE4,
  34494. 0xCE, 0x4F, 0x4D, 0x0F, 0xB0, 0x99, 0x7E, 0x41,
  34495. 0xAE, 0xFF, 0xB5, 0xB3, 0xDA, 0xA8, 0x38, 0x73,
  34496. 0x2F, 0x35, 0x73, 0x49, 0x44, 0x7F, 0x38, 0x77,
  34497. 0x76, 0xC7, 0x93, 0xC0, 0x47, 0x9D, 0xE9, 0xE9,
  34498. 0x94, 0x98, 0xCC, 0x35, 0x6F, 0xDB, 0x00, 0x75,
  34499. 0xA7, 0x03, 0xF2, 0x3C, 0x55, 0xD4, 0x7B, 0x55,
  34500. 0x0E, 0xC8, 0x9B, 0x02, 0xAD, 0xE8, 0x93, 0x29,
  34501. 0x08, 0x6A, 0x50, 0x84, 0x34, 0x56, 0xFE, 0xDC,
  34502. 0x37, 0x88, 0xAC, 0x8D, 0x97, 0x23, 0x3C, 0x54,
  34503. 0x56, 0x04, 0x67, 0xEE, 0x1D, 0x0F, 0x02, 0x4B,
  34504. 0x18, 0x42, 0x8F, 0x0D, 0x73, 0xB3, 0x0E, 0x19,
  34505. 0xF5, 0xC6, 0x3B, 0x9A, 0xBF, 0x11, 0x41, 0x5B,
  34506. 0xEA, 0x4D, 0x01, 0x70, 0x13, 0x0B, 0xAA, 0xBD,
  34507. 0x33, 0xC0, 0x5E, 0x65, 0x24, 0xE5, 0xFB, 0x55,
  34508. 0x81, 0xB2, 0x2B, 0x04, 0x33, 0x34, 0x22, 0x48,
  34509. 0x26, 0x6D, 0x0F, 0x10, 0x53, 0xB2, 0x45, 0xCC,
  34510. 0x24, 0x62, 0xDC, 0x44, 0xD3, 0x49, 0x65, 0x10,
  34511. 0x24, 0x82, 0xA8, 0xED, 0x9E, 0x4E, 0x96, 0x4D,
  34512. 0x56, 0x83, 0xE5, 0xD4, 0x5D, 0x0C, 0x82, 0x69
  34513. };
  34514. WOLFSSL_SMALL_STACK_STATIC const byte kyber768_ss[] = {
  34515. 0x91, 0x4C, 0xB6, 0x7F, 0xE5, 0xC3, 0x8E, 0x73,
  34516. 0xBF, 0x74, 0x18, 0x1C, 0x0A, 0xC5, 0x04, 0x28,
  34517. 0xDE, 0xDF, 0x77, 0x50, 0xA9, 0x80, 0x58, 0xF7,
  34518. 0xD5, 0x36, 0x70, 0x87, 0x74, 0x53, 0x5B, 0x29
  34519. };
  34520. #ifdef WOLFSSL_SMALL_STACK
  34521. key = (KyberKey *)XMALLOC(sizeof(KyberKey), HEAP_HINT,
  34522. DYNAMIC_TYPE_TMP_BUFFER);
  34523. priv = (byte *)XMALLOC(KYBER768_PRIVATE_KEY_SIZE, HEAP_HINT,
  34524. DYNAMIC_TYPE_TMP_BUFFER);
  34525. pub = (byte *)XMALLOC(KYBER768_PUBLIC_KEY_SIZE, HEAP_HINT,
  34526. DYNAMIC_TYPE_TMP_BUFFER);
  34527. ct = (byte *)XMALLOC(KYBER768_CIPHER_TEXT_SIZE, HEAP_HINT,
  34528. DYNAMIC_TYPE_TMP_BUFFER);
  34529. ss = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  34530. DYNAMIC_TYPE_TMP_BUFFER);
  34531. ss_dec = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  34532. DYNAMIC_TYPE_TMP_BUFFER);
  34533. if (! (key && priv && pub && ct && ss && ss_dec))
  34534. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  34535. #endif
  34536. ret = wc_KyberKey_Init(KYBER768, key, HEAP_HINT, INVALID_DEVID);
  34537. if (ret != 0)
  34538. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34539. else
  34540. key_inited = 1;
  34541. ret = wc_KyberKey_MakeKeyWithRandom(key, kyber768_rand,
  34542. sizeof(kyber768_rand));
  34543. if (ret != 0)
  34544. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34545. ret = wc_KyberKey_EncodePublicKey(key, pub, KYBER768_PUBLIC_KEY_SIZE);
  34546. if (ret != 0)
  34547. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34548. ret = wc_KyberKey_EncodePrivateKey(key, priv, KYBER768_PRIVATE_KEY_SIZE);
  34549. if (ret != 0)
  34550. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34551. if (XMEMCMP(pub, kyber768_pk, sizeof(kyber768_pk)) != 0)
  34552. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34553. if (XMEMCMP(priv, kyber768_sk, sizeof(kyber768_sk)) != 0)
  34554. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34555. ret = wc_KyberKey_EncapsulateWithRandom(key, ct, ss, kyber768enc_rand,
  34556. sizeof(kyber768enc_rand));
  34557. if (ret != 0)
  34558. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34559. if (XMEMCMP(ct, kyber768_ct, sizeof(kyber768_ct)) != 0)
  34560. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34561. if (XMEMCMP(ss, kyber768_ss, sizeof(kyber768_ss)) != 0)
  34562. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34563. ret = wc_KyberKey_Decapsulate(key, ss_dec, ct, sizeof(kyber768_ct));
  34564. if (ret != 0)
  34565. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34566. if (XMEMCMP(ss_dec, kyber768_ss, sizeof(kyber768_ss)) != 0)
  34567. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34568. out:
  34569. if (key_inited)
  34570. wc_KyberKey_Free(key);
  34571. #ifdef WOLFSSL_SMALL_STACK
  34572. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34573. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34574. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34575. XFREE(ct, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34576. XFREE(ss, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34577. XFREE(ss_dec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34578. #endif
  34579. return ret;
  34580. }
  34581. #endif /* WOLFSSL_KYBER768 */
  34582. #ifdef WOLFSSL_KYBER1024
  34583. static wc_test_ret_t kyber1024_kat(void)
  34584. {
  34585. wc_test_ret_t ret;
  34586. #ifdef WOLFSSL_SMALL_STACK
  34587. KyberKey *key = NULL;
  34588. byte *priv = NULL;
  34589. byte *pub = NULL;
  34590. byte *ct = NULL;
  34591. byte *ss = NULL;
  34592. byte *ss_dec = NULL;
  34593. #else
  34594. KyberKey key[1];
  34595. byte priv[KYBER1024_PRIVATE_KEY_SIZE];
  34596. byte pub[KYBER1024_PUBLIC_KEY_SIZE];
  34597. byte ct[KYBER1024_CIPHER_TEXT_SIZE];
  34598. byte ss[KYBER_SS_SZ];
  34599. byte ss_dec[KYBER_SS_SZ];
  34600. #endif
  34601. int key_inited = 0;
  34602. WOLFSSL_SMALL_STACK_STATIC const byte kyber1024_rand[] = {
  34603. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  34604. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  34605. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  34606. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  34607. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  34608. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  34609. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  34610. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  34611. };
  34612. WOLFSSL_SMALL_STACK_STATIC const byte kyber1024enc_rand[] = {
  34613. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  34614. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  34615. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  34616. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  34617. };
  34618. WOLFSSL_SMALL_STACK_STATIC const byte kyber1024_pk[] = {
  34619. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  34620. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  34621. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  34622. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  34623. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  34624. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  34625. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  34626. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  34627. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  34628. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  34629. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  34630. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  34631. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  34632. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  34633. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  34634. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  34635. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  34636. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  34637. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  34638. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  34639. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  34640. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  34641. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  34642. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  34643. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  34644. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  34645. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  34646. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  34647. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  34648. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  34649. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  34650. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  34651. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  34652. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  34653. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  34654. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  34655. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  34656. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  34657. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  34658. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  34659. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  34660. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  34661. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  34662. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  34663. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  34664. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  34665. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  34666. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  34667. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  34668. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  34669. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  34670. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  34671. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  34672. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  34673. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  34674. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  34675. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  34676. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  34677. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  34678. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  34679. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  34680. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  34681. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  34682. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  34683. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  34684. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  34685. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  34686. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  34687. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  34688. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  34689. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  34690. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  34691. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  34692. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  34693. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  34694. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  34695. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  34696. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  34697. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  34698. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  34699. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  34700. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  34701. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  34702. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  34703. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  34704. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  34705. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  34706. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  34707. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  34708. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  34709. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  34710. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  34711. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  34712. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  34713. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  34714. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  34715. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  34716. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  34717. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  34718. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  34719. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  34720. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  34721. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  34722. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  34723. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  34724. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  34725. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  34726. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  34727. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  34728. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  34729. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  34730. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  34731. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  34732. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  34733. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  34734. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  34735. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  34736. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  34737. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  34738. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  34739. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  34740. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  34741. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  34742. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  34743. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  34744. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  34745. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  34746. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  34747. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  34748. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  34749. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  34750. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  34751. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  34752. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  34753. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  34754. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  34755. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  34756. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  34757. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  34758. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  34759. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  34760. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  34761. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  34762. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  34763. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  34764. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  34765. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  34766. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  34767. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  34768. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  34769. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  34770. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  34771. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  34772. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  34773. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  34774. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  34775. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  34776. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  34777. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  34778. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  34779. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  34780. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  34781. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  34782. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  34783. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  34784. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  34785. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  34786. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  34787. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  34788. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  34789. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  34790. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  34791. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  34792. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  34793. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  34794. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  34795. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  34796. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  34797. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  34798. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  34799. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  34800. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  34801. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  34802. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  34803. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  34804. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  34805. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  34806. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  34807. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  34808. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  34809. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  34810. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  34811. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  34812. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  34813. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  34814. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  34815. };
  34816. WOLFSSL_SMALL_STACK_STATIC const byte kyber1024_sk[] = {
  34817. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  34818. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  34819. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  34820. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  34821. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  34822. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  34823. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  34824. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  34825. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  34826. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  34827. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  34828. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  34829. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  34830. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  34831. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  34832. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  34833. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  34834. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  34835. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  34836. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  34837. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  34838. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  34839. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  34840. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  34841. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  34842. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  34843. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  34844. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  34845. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  34846. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  34847. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  34848. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  34849. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  34850. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  34851. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  34852. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  34853. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  34854. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  34855. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  34856. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  34857. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  34858. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  34859. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  34860. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  34861. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  34862. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  34863. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  34864. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  34865. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  34866. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  34867. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  34868. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  34869. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  34870. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  34871. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  34872. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  34873. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  34874. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  34875. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  34876. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  34877. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  34878. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  34879. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  34880. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  34881. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  34882. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  34883. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  34884. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  34885. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  34886. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  34887. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  34888. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  34889. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  34890. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  34891. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  34892. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  34893. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  34894. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  34895. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  34896. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  34897. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  34898. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  34899. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  34900. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  34901. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  34902. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  34903. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  34904. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  34905. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  34906. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  34907. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  34908. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  34909. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  34910. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  34911. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  34912. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  34913. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  34914. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  34915. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  34916. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  34917. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  34918. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  34919. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  34920. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  34921. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  34922. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  34923. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  34924. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  34925. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  34926. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  34927. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  34928. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  34929. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  34930. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  34931. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  34932. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  34933. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  34934. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  34935. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  34936. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  34937. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  34938. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  34939. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  34940. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  34941. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  34942. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  34943. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  34944. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  34945. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  34946. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  34947. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  34948. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  34949. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  34950. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  34951. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  34952. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  34953. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  34954. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  34955. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  34956. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  34957. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  34958. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  34959. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  34960. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  34961. 0x85, 0xA4, 0xC1, 0xA1, 0xAB, 0x9B, 0x6A, 0xEB,
  34962. 0x49, 0xCC, 0xE1, 0xC2, 0xF8, 0xA9, 0x7C, 0x35,
  34963. 0x16, 0xC7, 0x2A, 0x00, 0xA4, 0x62, 0x63, 0xBA,
  34964. 0xA6, 0x96, 0xBF, 0x25, 0x72, 0x77, 0x19, 0xC3,
  34965. 0x21, 0x64, 0x23, 0x61, 0x8F, 0xF3, 0x33, 0x80,
  34966. 0x93, 0x4A, 0x6C, 0x10, 0x54, 0x5C, 0x4C, 0x5C,
  34967. 0x51, 0x55, 0xB1, 0x24, 0x86, 0x18, 0x1F, 0xC7,
  34968. 0xA2, 0x31, 0x98, 0x73, 0x97, 0x8B, 0x6A, 0x2A,
  34969. 0x67, 0x49, 0x0F, 0x82, 0x56, 0xBD, 0x21, 0x96,
  34970. 0xFE, 0x17, 0x92, 0xA4, 0xC0, 0x00, 0x77, 0xB8,
  34971. 0x12, 0xEA, 0xE8, 0xBE, 0xD3, 0x57, 0x24, 0x99,
  34972. 0x68, 0x4A, 0xB3, 0x37, 0x18, 0x76, 0x76, 0x1E,
  34973. 0x45, 0x0C, 0x9F, 0x9D, 0x27, 0x68, 0xA3, 0x68,
  34974. 0x06, 0xD7, 0xAB, 0x20, 0x46, 0xC9, 0x1F, 0x17,
  34975. 0x59, 0x9E, 0x9A, 0xC5, 0x92, 0x99, 0x08, 0x08,
  34976. 0xDC, 0xD7, 0xB4, 0xD0, 0x91, 0x90, 0x72, 0xF1,
  34977. 0x4E, 0xC3, 0x61, 0x77, 0x3B, 0x72, 0x52, 0x44,
  34978. 0x4C, 0x32, 0x3C, 0x30, 0x83, 0x26, 0xF4, 0xA3,
  34979. 0x0F, 0x86, 0x80, 0xD2, 0xF7, 0x48, 0xF5, 0x6A,
  34980. 0x13, 0x2B, 0x82, 0x67, 0x4E, 0xD0, 0x18, 0x46,
  34981. 0x20, 0xB8, 0x2A, 0xD2, 0xCB, 0x18, 0x2C, 0x97,
  34982. 0xB4, 0x81, 0x62, 0x66, 0x47, 0x49, 0x12, 0x90,
  34983. 0xA0, 0x11, 0xCC, 0x73, 0x82, 0x86, 0x85, 0xA8,
  34984. 0xC3, 0x67, 0xA5, 0xB9, 0xCF, 0x8D, 0x62, 0x1B,
  34985. 0x0D, 0x5C, 0x1E, 0xFF, 0x03, 0x17, 0x27, 0x58,
  34986. 0xBD, 0x00, 0x49, 0x78, 0xC2, 0x51, 0xCD, 0x51,
  34987. 0x34, 0x22, 0x28, 0x98, 0x9C, 0xAE, 0x63, 0x32,
  34988. 0xAC, 0x48, 0x64, 0x37, 0xCB, 0x5C, 0x57, 0xD4,
  34989. 0x30, 0x74, 0x62, 0x86, 0x52, 0x53, 0xBE, 0x21,
  34990. 0x7B, 0x35, 0x15, 0xC7, 0x3D, 0xF4, 0x05, 0xB7,
  34991. 0xF2, 0x82, 0x17, 0xAD, 0x0B, 0x8C, 0xF6, 0x0C,
  34992. 0x2F, 0xFF, 0xAA, 0x0A, 0x00, 0x48, 0xB1, 0xFB,
  34993. 0x4A, 0xCD, 0xCD, 0xC3, 0x8B, 0x52, 0x50, 0xCF,
  34994. 0xEC, 0x35, 0x6A, 0x6D, 0xE2, 0x6C, 0xFA, 0x7A,
  34995. 0x58, 0x8F, 0xDC, 0x86, 0xF9, 0x8C, 0x85, 0x4A,
  34996. 0xC6, 0x4C, 0x7B, 0xFA, 0xA9, 0x6F, 0x5A, 0x32,
  34997. 0xCC, 0x06, 0x10, 0x93, 0x4B, 0xAA, 0x6A, 0x58,
  34998. 0x6B, 0x9A, 0x20, 0x54, 0xF1, 0x3B, 0xA2, 0x74,
  34999. 0x17, 0x4A, 0xA0, 0xD2, 0xB3, 0xA8, 0x1B, 0x96,
  35000. 0xA9, 0x40, 0x66, 0x6F, 0x78, 0x9B, 0x5A, 0x6B,
  35001. 0xCD, 0xC0, 0xA6, 0xA0, 0x17, 0x8A, 0x0C, 0x9A,
  35002. 0x02, 0x57, 0x8A, 0x49, 0x3F, 0x6E, 0xEA, 0x0D,
  35003. 0x2E, 0x6C, 0x13, 0x95, 0x1C, 0x9F, 0x24, 0x9A,
  35004. 0x5E, 0x8D, 0xD7, 0x1D, 0xD4, 0x9A, 0x74, 0x2D,
  35005. 0x45, 0x1F, 0x1A, 0xBB, 0xA1, 0x9A, 0xF8, 0xC5,
  35006. 0x47, 0x85, 0x5E, 0x0A, 0xFC, 0x72, 0x8E, 0x90,
  35007. 0xAB, 0xB4, 0x99, 0xC9, 0xBE, 0xEB, 0x76, 0x6F,
  35008. 0x47, 0x29, 0xCD, 0xA2, 0x22, 0x63, 0xE3, 0x24,
  35009. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  35010. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  35011. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  35012. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  35013. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  35014. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  35015. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  35016. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  35017. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  35018. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  35019. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  35020. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  35021. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  35022. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  35023. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  35024. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  35025. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  35026. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  35027. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  35028. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  35029. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  35030. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  35031. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  35032. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  35033. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  35034. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  35035. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  35036. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  35037. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  35038. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  35039. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  35040. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  35041. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  35042. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  35043. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  35044. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  35045. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  35046. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  35047. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  35048. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  35049. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  35050. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  35051. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  35052. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  35053. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  35054. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  35055. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  35056. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  35057. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  35058. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  35059. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  35060. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  35061. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  35062. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  35063. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  35064. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  35065. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  35066. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  35067. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  35068. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  35069. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  35070. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  35071. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  35072. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  35073. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  35074. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  35075. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  35076. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  35077. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  35078. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  35079. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  35080. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  35081. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  35082. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  35083. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  35084. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  35085. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  35086. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  35087. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  35088. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  35089. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  35090. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  35091. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  35092. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  35093. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  35094. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  35095. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  35096. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  35097. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  35098. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  35099. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  35100. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  35101. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  35102. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  35103. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  35104. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  35105. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  35106. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  35107. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  35108. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  35109. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  35110. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  35111. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  35112. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  35113. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  35114. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  35115. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  35116. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  35117. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  35118. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  35119. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  35120. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  35121. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  35122. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  35123. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  35124. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  35125. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  35126. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  35127. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  35128. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  35129. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  35130. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  35131. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  35132. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  35133. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  35134. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  35135. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  35136. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  35137. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  35138. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  35139. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  35140. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  35141. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  35142. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  35143. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  35144. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  35145. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  35146. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  35147. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  35148. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  35149. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  35150. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  35151. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  35152. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  35153. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  35154. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  35155. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  35156. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  35157. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  35158. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  35159. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  35160. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  35161. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  35162. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  35163. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  35164. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  35165. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  35166. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  35167. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  35168. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  35169. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  35170. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  35171. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  35172. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  35173. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  35174. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  35175. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  35176. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  35177. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  35178. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  35179. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  35180. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  35181. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  35182. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  35183. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  35184. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  35185. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  35186. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  35187. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  35188. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  35189. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  35190. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  35191. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  35192. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  35193. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  35194. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  35195. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  35196. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  35197. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  35198. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  35199. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  35200. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  35201. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  35202. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  35203. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  35204. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  35205. 0x8A, 0x39, 0xE8, 0x7D, 0x53, 0x1F, 0x35, 0x27,
  35206. 0xC2, 0x07, 0xED, 0xCC, 0x1D, 0xB7, 0xFA, 0xDD,
  35207. 0xCF, 0x96, 0x28, 0x39, 0x18, 0x79, 0xB3, 0x35,
  35208. 0xC7, 0x07, 0x83, 0x9A, 0x0D, 0xB0, 0x51, 0xA8,
  35209. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  35210. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  35211. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  35212. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  35213. };
  35214. WOLFSSL_SMALL_STACK_STATIC const byte kyber1024_ct[] = {
  35215. 0xA6, 0xAF, 0x29, 0xD5, 0xF5, 0xB8, 0x0B, 0xD1,
  35216. 0x30, 0xF5, 0x18, 0xBA, 0xDD, 0xD6, 0xC8, 0xF1,
  35217. 0x75, 0x45, 0x41, 0x3D, 0x86, 0x0F, 0xB3, 0xDE,
  35218. 0x45, 0x19, 0x79, 0xEB, 0xFA, 0x5E, 0x4E, 0x31,
  35219. 0x12, 0xC7, 0xC0, 0xAD, 0xF9, 0x98, 0x24, 0xBB,
  35220. 0x52, 0x6F, 0x2C, 0x35, 0x50, 0x74, 0x8E, 0xD0,
  35221. 0xE1, 0x34, 0xF0, 0x45, 0x7A, 0x7C, 0x61, 0xF9,
  35222. 0xF5, 0x26, 0xF0, 0x02, 0xBA, 0xAD, 0xC0, 0x3F,
  35223. 0xC1, 0x3E, 0x38, 0x13, 0x12, 0x19, 0x51, 0x3C,
  35224. 0x3E, 0xDE, 0x06, 0x16, 0x61, 0xE7, 0x4F, 0x60,
  35225. 0x3C, 0x4F, 0xCF, 0x79, 0x51, 0xC8, 0xE5, 0x2C,
  35226. 0x9C, 0x21, 0x3B, 0x0D, 0x22, 0xD9, 0x29, 0x36,
  35227. 0x63, 0xD6, 0x69, 0xA6, 0xB5, 0x8E, 0xD8, 0xFC,
  35228. 0xEF, 0xCF, 0x82, 0x49, 0xD7, 0xBB, 0x52, 0x98,
  35229. 0xF5, 0x57, 0x61, 0x44, 0x5B, 0x2B, 0x83, 0xCE,
  35230. 0x7F, 0x00, 0x5C, 0xB0, 0x42, 0x48, 0xAE, 0xC8,
  35231. 0xBD, 0xA2, 0x2F, 0xD2, 0xD4, 0x2A, 0xA7, 0x66,
  35232. 0x32, 0x20, 0x14, 0xEA, 0x03, 0x8C, 0xC3, 0x2C,
  35233. 0x55, 0xC8, 0xE4, 0xB9, 0xE2, 0x8E, 0xC9, 0x11,
  35234. 0x9F, 0x52, 0x73, 0x41, 0xE4, 0xF6, 0x6A, 0x03,
  35235. 0x51, 0x21, 0x07, 0x3B, 0x85, 0xDE, 0x67, 0x06,
  35236. 0xDA, 0x19, 0xE0, 0x83, 0x8A, 0x9F, 0x33, 0xB7,
  35237. 0x19, 0xA6, 0x8F, 0x03, 0x9B, 0x66, 0x4D, 0xC0,
  35238. 0x02, 0x65, 0x9E, 0xAB, 0xFC, 0x39, 0x86, 0x79,
  35239. 0xAA, 0x70, 0x09, 0xCE, 0x0C, 0xD0, 0x1C, 0xDA,
  35240. 0xFB, 0x6C, 0xD2, 0xA2, 0x6F, 0xE4, 0x10, 0x16,
  35241. 0x72, 0xC9, 0x8F, 0xF5, 0x8F, 0x7C, 0x47, 0xD5,
  35242. 0xBD, 0xA2, 0x90, 0x66, 0x53, 0xB3, 0xA6, 0xF9,
  35243. 0x65, 0x1F, 0x7A, 0x12, 0x1E, 0xA7, 0x7E, 0xA7,
  35244. 0x47, 0x23, 0xFA, 0xE5, 0xB8, 0x73, 0xF9, 0xBB,
  35245. 0x7B, 0x66, 0x4F, 0x0C, 0x8A, 0x93, 0x83, 0x1E,
  35246. 0xF9, 0xD5, 0x1C, 0x7C, 0xC1, 0xEF, 0x44, 0xAC,
  35247. 0x0E, 0x55, 0xA5, 0x5C, 0xA7, 0x6D, 0x13, 0x7F,
  35248. 0xE9, 0xB7, 0x5F, 0x40, 0x50, 0x9C, 0xEF, 0x15,
  35249. 0x6E, 0x5A, 0xD1, 0x8F, 0x9F, 0xB9, 0x99, 0x68,
  35250. 0x00, 0x08, 0xE5, 0x47, 0xD5, 0x5E, 0xEC, 0xD5,
  35251. 0xB4, 0xD1, 0xCB, 0x1D, 0x9F, 0x07, 0x6C, 0xEC,
  35252. 0x21, 0x50, 0x1C, 0x74, 0x02, 0x50, 0x9E, 0xCB,
  35253. 0x77, 0xAF, 0xB2, 0xCB, 0x9A, 0x61, 0x34, 0x0A,
  35254. 0x8B, 0xD1, 0x51, 0x4C, 0x6E, 0x71, 0xB4, 0xAA,
  35255. 0x45, 0xE4, 0x7E, 0xC3, 0x75, 0x12, 0x27, 0x1B,
  35256. 0x91, 0x1F, 0x8F, 0xB4, 0x6C, 0x90, 0x82, 0xC9,
  35257. 0xDF, 0x07, 0x20, 0x4A, 0xBB, 0x5A, 0x50, 0xE6,
  35258. 0xE3, 0x64, 0x7A, 0x8A, 0xD4, 0xD8, 0xD5, 0xD7,
  35259. 0xBF, 0xF1, 0x9C, 0x8A, 0x50, 0x93, 0x08, 0xBC,
  35260. 0xFB, 0x89, 0x55, 0x36, 0xD0, 0x45, 0xCA, 0x2B,
  35261. 0x97, 0xCB, 0x16, 0xA2, 0x9B, 0xB7, 0x18, 0x1C,
  35262. 0xAD, 0x05, 0x09, 0xDD, 0xB9, 0x17, 0x35, 0x02,
  35263. 0x8E, 0xBA, 0x8C, 0x31, 0xD7, 0x4B, 0xD2, 0x75,
  35264. 0xEA, 0xA6, 0x5B, 0x53, 0x40, 0xB3, 0xA4, 0x3F,
  35265. 0xBF, 0xE0, 0xB3, 0x06, 0x1D, 0x6B, 0xAE, 0x7E,
  35266. 0x75, 0xB7, 0x09, 0x8C, 0xDA, 0xBE, 0x91, 0xD4,
  35267. 0xB3, 0x1E, 0x36, 0xC9, 0xAA, 0x7A, 0x82, 0x98,
  35268. 0x86, 0x2A, 0xD6, 0x3C, 0x8F, 0xD2, 0x82, 0xE0,
  35269. 0x3B, 0x46, 0x0B, 0x3A, 0xB4, 0x64, 0xCE, 0x0F,
  35270. 0x27, 0xB1, 0xC3, 0xD1, 0x11, 0x55, 0xAC, 0xAA,
  35271. 0x01, 0x1E, 0xB9, 0xE2, 0xAE, 0x3E, 0x6D, 0xDA,
  35272. 0x07, 0xD6, 0xF4, 0x91, 0x73, 0x7C, 0xBC, 0xE9,
  35273. 0xB0, 0x5F, 0x9B, 0xC5, 0x6B, 0xE2, 0x0E, 0x8D,
  35274. 0x32, 0x6B, 0xA1, 0x32, 0xC5, 0x7F, 0xB2, 0x35,
  35275. 0x16, 0x11, 0x44, 0x51, 0x9C, 0xDF, 0x40, 0x56,
  35276. 0x0F, 0xBE, 0x27, 0x9B, 0xDE, 0x41, 0x1E, 0x11,
  35277. 0x25, 0x31, 0xF8, 0x26, 0xD6, 0xAB, 0x10, 0xD4,
  35278. 0x54, 0x73, 0x50, 0xAD, 0xD2, 0xA9, 0xDE, 0x8D,
  35279. 0x62, 0xC2, 0xAC, 0x82, 0xCA, 0xBE, 0x68, 0x15,
  35280. 0x64, 0x6F, 0x4D, 0xC9, 0x74, 0x2B, 0xB0, 0xC2,
  35281. 0xA3, 0xF7, 0x7E, 0xC7, 0xB4, 0x6C, 0x6B, 0x53,
  35282. 0x76, 0x05, 0xFA, 0x31, 0x79, 0x8C, 0xD8, 0x92,
  35283. 0x81, 0x22, 0x1A, 0x33, 0xDF, 0xB9, 0x79, 0x6E,
  35284. 0x64, 0x43, 0x05, 0x63, 0x03, 0x32, 0xC2, 0xCB,
  35285. 0x93, 0x14, 0x08, 0xAB, 0x48, 0x1A, 0x16, 0xD9,
  35286. 0x53, 0xF6, 0xBE, 0xAE, 0x38, 0x91, 0xD6, 0xD9,
  35287. 0xAC, 0x1F, 0xAB, 0x38, 0x22, 0x2D, 0x92, 0x71,
  35288. 0x87, 0x2D, 0x9D, 0x0C, 0xAD, 0xB9, 0x1A, 0xBE,
  35289. 0x9B, 0x4E, 0x26, 0x5F, 0x75, 0xC6, 0xE5, 0xE8,
  35290. 0x29, 0xE1, 0x46, 0xC3, 0xD8, 0xCE, 0x1E, 0x9D,
  35291. 0x12, 0xE0, 0xD1, 0x29, 0x80, 0x19, 0x57, 0xF4,
  35292. 0x6B, 0x0D, 0x2D, 0xBE, 0x1F, 0x74, 0x9B, 0x1D,
  35293. 0x08, 0xE2, 0x34, 0x5F, 0x62, 0x39, 0xA7, 0x31,
  35294. 0x34, 0x2E, 0xB7, 0x5B, 0x0C, 0xF1, 0xBF, 0x41,
  35295. 0x17, 0x49, 0xBC, 0x2C, 0xAF, 0x28, 0x10, 0xB7,
  35296. 0x88, 0xC6, 0xB7, 0x23, 0x8B, 0x4D, 0x3D, 0xA2,
  35297. 0xD6, 0x31, 0x5C, 0xE9, 0x54, 0x2E, 0x24, 0x40,
  35298. 0x4F, 0x14, 0x57, 0x55, 0xA3, 0x0A, 0xB8, 0x51,
  35299. 0xE4, 0x44, 0x58, 0x41, 0xBD, 0x33, 0xF7, 0x16,
  35300. 0xA5, 0x86, 0x88, 0x48, 0x88, 0xEC, 0xC6, 0xBC,
  35301. 0x64, 0x98, 0xAA, 0x32, 0x91, 0x9A, 0xE8, 0x1D,
  35302. 0x20, 0xC2, 0x69, 0x73, 0xC2, 0xBD, 0x54, 0x58,
  35303. 0x2A, 0x0F, 0x6A, 0xD9, 0x8A, 0xBF, 0xD2, 0x62,
  35304. 0x7E, 0x15, 0x69, 0x0A, 0x72, 0x7E, 0x69, 0xF5,
  35305. 0x81, 0xDD, 0x2A, 0x71, 0x27, 0x98, 0x2A, 0x90,
  35306. 0xE3, 0x3E, 0x2D, 0x4A, 0x03, 0xFE, 0x33, 0x91,
  35307. 0x42, 0xC7, 0xE4, 0x4C, 0x32, 0x6A, 0xC4, 0x6E,
  35308. 0xD3, 0x95, 0xA2, 0x25, 0xD3, 0x03, 0x33, 0x89,
  35309. 0x91, 0x73, 0x28, 0xB4, 0x53, 0x16, 0xB1, 0x58,
  35310. 0x5A, 0x01, 0xB2, 0xC3, 0x04, 0xB2, 0x94, 0x4E,
  35311. 0x90, 0x3A, 0xBB, 0xB3, 0xEC, 0x56, 0x19, 0x44,
  35312. 0x1C, 0xFC, 0x89, 0x65, 0xA4, 0x46, 0xDF, 0x75,
  35313. 0xDE, 0xFA, 0x80, 0xC6, 0xE1, 0x5A, 0xDB, 0xD5,
  35314. 0x06, 0xB7, 0xAB, 0x2D, 0xE1, 0x2D, 0xDA, 0x9B,
  35315. 0xC8, 0x14, 0x41, 0xCF, 0xC8, 0x90, 0x52, 0xE2,
  35316. 0xE5, 0x80, 0x8F, 0x71, 0x26, 0xC6, 0xFD, 0x3A,
  35317. 0xC6, 0xAC, 0x80, 0x81, 0x25, 0x8A, 0x84, 0xA0,
  35318. 0x9A, 0xE5, 0x0F, 0x6C, 0xD7, 0xCC, 0x0F, 0x4A,
  35319. 0xF3, 0x36, 0xFD, 0x1D, 0x64, 0x3E, 0x99, 0x07,
  35320. 0x99, 0x96, 0x26, 0x8C, 0x2D, 0x32, 0xD9, 0x09,
  35321. 0xF2, 0x2E, 0x35, 0x04, 0xF0, 0x7F, 0xBB, 0x56,
  35322. 0x31, 0x96, 0xD4, 0x31, 0x2F, 0xDD, 0xB9, 0x33,
  35323. 0x5D, 0x5C, 0x1D, 0x36, 0xE8, 0xC5, 0xEE, 0xA2,
  35324. 0x27, 0x8D, 0xBA, 0x23, 0xB9, 0x4D, 0x19, 0x3C,
  35325. 0x94, 0x7C, 0xC4, 0x1C, 0xA9, 0x93, 0xDC, 0x7D,
  35326. 0xB1, 0x39, 0x63, 0x40, 0xAD, 0x9C, 0x4F, 0xE6,
  35327. 0x87, 0xDD, 0x7B, 0x8D, 0x0C, 0x7A, 0x51, 0x20,
  35328. 0xAE, 0x02, 0x04, 0xF2, 0xC6, 0x65, 0xBD, 0x5F,
  35329. 0x47, 0x3D, 0x64, 0x4C, 0x7F, 0xF2, 0x6B, 0xFF,
  35330. 0xBA, 0x7A, 0x36, 0x98, 0x08, 0x30, 0x70, 0x21,
  35331. 0x28, 0xA7, 0xE6, 0x61, 0xD6, 0x77, 0xA0, 0x92,
  35332. 0xA3, 0x6E, 0x74, 0x28, 0xA4, 0x13, 0x9F, 0xB2,
  35333. 0x9B, 0x00, 0x95, 0xCC, 0x11, 0x08, 0x6F, 0x44,
  35334. 0x7D, 0x2A, 0x9E, 0xF6, 0xC9, 0xB1, 0x61, 0xF1,
  35335. 0x89, 0xC6, 0x29, 0x9E, 0x08, 0x4C, 0xB7, 0xAA,
  35336. 0x00, 0xFA, 0xF7, 0x87, 0x79, 0x7B, 0xFB, 0x06,
  35337. 0x9F, 0xBC, 0x08, 0x7F, 0xDE, 0x26, 0x25, 0x2A,
  35338. 0x16, 0x64, 0xF1, 0x9C, 0x5A, 0x8A, 0x22, 0xEC,
  35339. 0x5E, 0xE1, 0xAE, 0xB0, 0x76, 0x35, 0x7B, 0x7D,
  35340. 0xC3, 0x7E, 0x6B, 0x0F, 0x15, 0x20, 0xF9, 0x58,
  35341. 0xF7, 0x85, 0x1B, 0xAC, 0xB9, 0x2C, 0x89, 0xFD,
  35342. 0x11, 0x4A, 0x72, 0xFE, 0xAC, 0x54, 0x65, 0x2D,
  35343. 0x45, 0xB0, 0x9E, 0x1A, 0xE7, 0x65, 0x1A, 0xBD,
  35344. 0x16, 0x4B, 0xCD, 0x53, 0x7D, 0x58, 0xFA, 0x39,
  35345. 0xD3, 0xEC, 0x8A, 0xCD, 0xCD, 0xF9, 0x84, 0x25,
  35346. 0x00, 0x58, 0x62, 0xFA, 0x59, 0x69, 0x2D, 0xE1,
  35347. 0x62, 0xB7, 0x7E, 0x62, 0x97, 0xC6, 0x62, 0x33,
  35348. 0x34, 0x84, 0x08, 0xA8, 0xAB, 0x69, 0x5C, 0xE2,
  35349. 0xF2, 0x72, 0x8D, 0xB9, 0xFB, 0xE2, 0x7E, 0x95,
  35350. 0x89, 0x67, 0xEC, 0x59, 0x74, 0x76, 0x7C, 0x5A,
  35351. 0x66, 0x02, 0x30, 0x74, 0xB4, 0xA7, 0x1A, 0xFD,
  35352. 0x26, 0x4A, 0xD2, 0x89, 0x0E, 0x97, 0x0A, 0x1F,
  35353. 0x31, 0xD6, 0xE3, 0x31, 0x1B, 0x73, 0x6F, 0x9F,
  35354. 0x94, 0x88, 0x79, 0x3D, 0xDC, 0x88, 0xF2, 0x34,
  35355. 0x58, 0x06, 0x42, 0x54, 0xC8, 0x2A, 0x1D, 0x9E,
  35356. 0x59, 0xEA, 0xD2, 0xFC, 0xEC, 0x40, 0xB4, 0x30,
  35357. 0x68, 0x7C, 0x4B, 0x7E, 0x28, 0x96, 0x09, 0x26,
  35358. 0xAF, 0xCA, 0xCC, 0x9B, 0xD7, 0x56, 0xA7, 0x10,
  35359. 0x88, 0xC7, 0x84, 0x50, 0xE2, 0x0A, 0x2E, 0x98,
  35360. 0x0A, 0xED, 0xE9, 0xEB, 0xED, 0xFE, 0x7F, 0xAB,
  35361. 0xD6, 0xAB, 0xFE, 0x96, 0xF9, 0x34, 0xC4, 0xB0,
  35362. 0x2C, 0x01, 0xCA, 0x19, 0x4D, 0x01, 0xB7, 0x3C,
  35363. 0x25, 0xD5, 0x99, 0x70, 0x39, 0xD3, 0xFC, 0xD0,
  35364. 0xF0, 0x99, 0x52, 0x1F, 0x70, 0xCA, 0xEE, 0x69,
  35365. 0x11, 0x0A, 0xC1, 0xFC, 0x5A, 0x99, 0x91, 0x7A,
  35366. 0xD7, 0x52, 0xFC, 0x96, 0xAD, 0xFA, 0xD7, 0x18,
  35367. 0x6D, 0x0A, 0x7C, 0x9C, 0xFE, 0x56, 0x01, 0xC0,
  35368. 0x75, 0x14, 0xEA, 0x64, 0x48, 0xD6, 0x61, 0xC5,
  35369. 0x7A, 0xA2, 0x02, 0x42, 0x10, 0x3C, 0x42, 0x76,
  35370. 0xA0, 0x70, 0xA4, 0x89, 0xA4, 0xCB, 0x6B, 0xCA,
  35371. 0x0F, 0x9E, 0xCC, 0x43, 0x79, 0xFB, 0x22, 0x02,
  35372. 0x15, 0xFD, 0x91, 0xF8, 0x10, 0x19, 0xD5, 0xB0,
  35373. 0xAE, 0x61, 0x93, 0x58, 0xB5, 0x24, 0x68, 0xF2,
  35374. 0x72, 0xC1, 0x78, 0xE3, 0xA7, 0x4C, 0xF6, 0x77,
  35375. 0x5A, 0xA9, 0x24, 0xFE, 0x32, 0x9C, 0x31, 0x75,
  35376. 0xD9, 0xE4, 0xC3, 0xE2, 0x1A, 0xB9, 0xEC, 0x83,
  35377. 0x6E, 0xDC, 0x3A, 0xCA, 0xB2, 0xE3, 0x89, 0x1E,
  35378. 0xE8, 0xDE, 0xDA, 0x51, 0x5D, 0x39, 0xAF, 0x9B,
  35379. 0x8D, 0xDD, 0x0E, 0xE7, 0xB0, 0x16, 0x4F, 0x80,
  35380. 0x5C, 0x38, 0x35, 0xF6, 0xD2, 0xBA, 0xBD, 0xB3,
  35381. 0x0E, 0xAB, 0x47, 0x56, 0xE7, 0xEC, 0x7F, 0x82,
  35382. 0x9E, 0xCE, 0x01, 0xE8, 0xEA, 0xDF, 0xBB, 0xED,
  35383. 0x12, 0xFC, 0x28, 0x3B, 0x3D, 0x4C, 0x69, 0xF5,
  35384. 0x75, 0xE7, 0xF8, 0x04, 0x17, 0x68, 0x9F, 0xDF,
  35385. 0xCF, 0xC7, 0xBE, 0x27, 0xEE, 0x3B, 0x8C, 0xDF,
  35386. 0x57, 0xAA, 0xEB, 0xEC, 0x4A, 0x95, 0xB7, 0xE5,
  35387. 0xBB, 0x58, 0x5B, 0x85, 0x22, 0x7F, 0x7C, 0x32,
  35388. 0xBE, 0x30, 0xDB, 0x3E, 0x65, 0xE4, 0x2E, 0x30,
  35389. 0xDC, 0xF5, 0xA5, 0xFA, 0x07, 0x3D, 0xBA, 0x39,
  35390. 0x9D, 0x94, 0x2F, 0x22, 0x22, 0xAD, 0xB9, 0xB9,
  35391. 0x89, 0x81, 0x02, 0xAF, 0xE5, 0x43, 0x2E, 0xDC,
  35392. 0x7F, 0x04, 0xAE, 0x34, 0xA8, 0xFE, 0xC2, 0xD8,
  35393. 0x1C, 0xB4, 0x9A, 0x9A, 0x9B, 0x43, 0x81, 0x4C,
  35394. 0xE7, 0x1D, 0x97, 0xF7, 0x26, 0xE2, 0xB1, 0xE8,
  35395. 0xF6, 0x4B, 0x50, 0xE6, 0x5D, 0xFB, 0x48, 0x16,
  35396. 0xE1, 0x2E, 0x82, 0xA3, 0x19, 0x74, 0x84, 0xA4,
  35397. 0xE9, 0xBB, 0xA4, 0xD2, 0xD6, 0x9E, 0x3F, 0x19,
  35398. 0xD0, 0xB7, 0x5C, 0x21, 0xE2, 0xBF, 0xFE, 0x9F,
  35399. 0xC0, 0xC9, 0x8C, 0xF4, 0x8A, 0x3A, 0xAF, 0x08,
  35400. 0xD4, 0x67, 0xF7, 0x26, 0x87, 0xDF, 0x01, 0x78,
  35401. 0x17, 0x4B, 0x78, 0x97, 0xF7, 0x34, 0x34, 0x9B,
  35402. 0x18, 0x1E, 0xCA, 0x86, 0xA5, 0x98, 0xA0, 0xC5,
  35403. 0xE8, 0xC2, 0x59, 0x46, 0xF2, 0x4D, 0xC5, 0x57,
  35404. 0x2B, 0xD3, 0x24, 0xA4, 0x04, 0x58, 0xA7, 0x88,
  35405. 0xE5, 0x13, 0x7F, 0x3C, 0x7A, 0x7C, 0x97, 0xFC,
  35406. 0x9F, 0x12, 0xA3, 0xC4, 0x63, 0xA8, 0xFE, 0x94,
  35407. 0x49, 0x10, 0x1C, 0xCE, 0x96, 0x6D, 0x7C, 0x00,
  35408. 0x93, 0x23, 0x93, 0x29, 0x98, 0xD5, 0x6E, 0xF4,
  35409. 0x30, 0xC7, 0x3B, 0xC2, 0x4F, 0x5D, 0x95, 0xF7,
  35410. 0x37, 0x85, 0x8D, 0xDC, 0x4F, 0x32, 0xC0, 0x13
  35411. };
  35412. WOLFSSL_SMALL_STACK_STATIC const byte kyber1024_ss[] = {
  35413. 0xB1, 0x0F, 0x73, 0x94, 0x92, 0x6A, 0xD3, 0xB4,
  35414. 0x9C, 0x5D, 0x62, 0xD5, 0xAE, 0xB5, 0x31, 0xD5,
  35415. 0x75, 0x75, 0x38, 0xBC, 0xC0, 0xDA, 0x9E, 0x55,
  35416. 0x0D, 0x43, 0x8F, 0x1B, 0x61, 0xBD, 0x74, 0x19
  35417. };
  35418. #ifdef WOLFSSL_SMALL_STACK
  35419. key = (KyberKey *)XMALLOC(sizeof(KyberKey), HEAP_HINT,
  35420. DYNAMIC_TYPE_TMP_BUFFER);
  35421. priv = (byte *)XMALLOC(KYBER1024_PRIVATE_KEY_SIZE, HEAP_HINT,
  35422. DYNAMIC_TYPE_TMP_BUFFER);
  35423. pub = (byte *)XMALLOC(KYBER1024_PUBLIC_KEY_SIZE, HEAP_HINT,
  35424. DYNAMIC_TYPE_TMP_BUFFER);
  35425. ct = (byte *)XMALLOC(KYBER1024_CIPHER_TEXT_SIZE, HEAP_HINT,
  35426. DYNAMIC_TYPE_TMP_BUFFER);
  35427. ss = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  35428. DYNAMIC_TYPE_TMP_BUFFER);
  35429. ss_dec = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  35430. DYNAMIC_TYPE_TMP_BUFFER);
  35431. if (! (key && priv && pub && ct && ss && ss_dec))
  35432. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  35433. #endif
  35434. ret = wc_KyberKey_Init(KYBER1024, key, HEAP_HINT, INVALID_DEVID);
  35435. if (ret != 0)
  35436. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35437. else
  35438. key_inited = 1;
  35439. ret = wc_KyberKey_MakeKeyWithRandom(key, kyber1024_rand,
  35440. sizeof(kyber1024_rand));
  35441. if (ret != 0)
  35442. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35443. ret = wc_KyberKey_EncodePublicKey(key, pub, KYBER_MAX_PUBLIC_KEY_SIZE);
  35444. if (ret != 0)
  35445. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35446. ret = wc_KyberKey_EncodePrivateKey(key, priv, KYBER_MAX_PRIVATE_KEY_SIZE);
  35447. if (ret != 0)
  35448. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35449. if (XMEMCMP(pub, kyber1024_pk, sizeof(kyber1024_pk)) != 0)
  35450. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35451. if (XMEMCMP(priv, kyber1024_sk, sizeof(kyber1024_sk)) != 0)
  35452. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35453. ret = wc_KyberKey_EncapsulateWithRandom(key, ct, ss, kyber1024enc_rand,
  35454. sizeof(kyber1024enc_rand));
  35455. if (ret != 0)
  35456. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35457. if (XMEMCMP(ct, kyber1024_ct, sizeof(kyber1024_ct)) != 0)
  35458. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35459. if (XMEMCMP(ss, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  35460. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35461. ret = wc_KyberKey_Decapsulate(key, ss_dec, ct, sizeof(kyber1024_ct));
  35462. if (ret != 0)
  35463. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35464. if (XMEMCMP(ss_dec, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  35465. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35466. out:
  35467. if (key_inited)
  35468. wc_KyberKey_Free(key);
  35469. #ifdef WOLFSSL_SMALL_STACK
  35470. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35471. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35472. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35473. XFREE(ct, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35474. XFREE(ss, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35475. XFREE(ss_dec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35476. #endif
  35477. return ret;
  35478. }
  35479. #endif /* WOLFSSL_KYBER1024 */
  35480. #endif /* WOLFSSL_WC_KYBER */
  35481. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t kyber_test(void)
  35482. {
  35483. wc_test_ret_t ret;
  35484. WC_RNG rng;
  35485. int i;
  35486. #ifdef WOLFSSL_SMALL_STACK
  35487. KyberKey *key = NULL;
  35488. byte *priv = NULL;
  35489. byte *pub = NULL;
  35490. byte *priv2 = NULL;
  35491. byte *pub2 = NULL;
  35492. byte *ct = NULL;
  35493. byte *ss = NULL;
  35494. byte *ss_dec = NULL;
  35495. #else
  35496. KyberKey key[1];
  35497. byte priv[KYBER_MAX_PRIVATE_KEY_SIZE];
  35498. byte pub[KYBER_MAX_PUBLIC_KEY_SIZE];
  35499. byte priv2[KYBER_MAX_PRIVATE_KEY_SIZE];
  35500. byte pub2[KYBER_MAX_PUBLIC_KEY_SIZE];
  35501. byte ct[KYBER_MAX_CIPHER_TEXT_SIZE];
  35502. byte ss[KYBER_SS_SZ];
  35503. byte ss_dec[KYBER_SS_SZ];
  35504. #endif
  35505. int key_inited = 0;
  35506. static const int testData[][4] = {
  35507. #ifdef WOLFSSL_KYBER512
  35508. { KYBER512, KYBER512_PRIVATE_KEY_SIZE, KYBER512_PUBLIC_KEY_SIZE,
  35509. KYBER512_CIPHER_TEXT_SIZE },
  35510. #endif
  35511. #ifdef WOLFSSL_KYBER768
  35512. { KYBER768, KYBER768_PRIVATE_KEY_SIZE, KYBER768_PUBLIC_KEY_SIZE,
  35513. KYBER768_CIPHER_TEXT_SIZE },
  35514. #endif
  35515. #ifdef WOLFSSL_KYBER1024
  35516. { KYBER1024, KYBER1024_PRIVATE_KEY_SIZE, KYBER1024_PUBLIC_KEY_SIZE,
  35517. KYBER1024_CIPHER_TEXT_SIZE },
  35518. #endif
  35519. };
  35520. WOLFSSL_ENTER("kyber_test");
  35521. #ifdef WOLFSSL_SMALL_STACK
  35522. key = (KyberKey *)XMALLOC(sizeof(KyberKey), HEAP_HINT,
  35523. DYNAMIC_TYPE_TMP_BUFFER);
  35524. priv = (byte *)XMALLOC(KYBER_MAX_PRIVATE_KEY_SIZE, HEAP_HINT,
  35525. DYNAMIC_TYPE_TMP_BUFFER);
  35526. pub = (byte *)XMALLOC(KYBER_MAX_PUBLIC_KEY_SIZE, HEAP_HINT,
  35527. DYNAMIC_TYPE_TMP_BUFFER);
  35528. priv2 = (byte *)XMALLOC(KYBER_MAX_PRIVATE_KEY_SIZE, HEAP_HINT,
  35529. DYNAMIC_TYPE_TMP_BUFFER);
  35530. pub2 = (byte *)XMALLOC(KYBER_MAX_PUBLIC_KEY_SIZE, HEAP_HINT,
  35531. DYNAMIC_TYPE_TMP_BUFFER);
  35532. ct = (byte *)XMALLOC(KYBER_MAX_CIPHER_TEXT_SIZE, HEAP_HINT,
  35533. DYNAMIC_TYPE_TMP_BUFFER);
  35534. ss = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  35535. DYNAMIC_TYPE_TMP_BUFFER);
  35536. ss_dec = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  35537. DYNAMIC_TYPE_TMP_BUFFER);
  35538. if (! (key && priv && pub && priv2 && pub2 && ct && ss && ss_dec))
  35539. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  35540. #endif
  35541. #ifndef HAVE_FIPS
  35542. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  35543. #else
  35544. ret = wc_InitRng(&rng);
  35545. #endif
  35546. if (ret != 0)
  35547. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35548. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  35549. ret = wc_KyberKey_Init(testData[i][0], key, HEAP_HINT, INVALID_DEVID);
  35550. if (ret != 0)
  35551. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35552. else
  35553. key_inited = 1;
  35554. ret = wc_KyberKey_MakeKey(key, &rng);
  35555. if (ret != 0)
  35556. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35557. ret = wc_KyberKey_EncodePublicKey(key, pub, testData[i][2]);
  35558. if (ret != 0)
  35559. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35560. ret = wc_KyberKey_EncodePrivateKey(key, priv, testData[i][1]);
  35561. if (ret != 0)
  35562. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35563. ret = wc_KyberKey_Init(testData[i][0], key, HEAP_HINT, INVALID_DEVID);
  35564. if (ret != 0)
  35565. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35566. ret = wc_KyberKey_DecodePublicKey(key, pub, testData[i][2]);
  35567. if (ret != 0)
  35568. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35569. ret = wc_KyberKey_Encapsulate(key, ct, ss, &rng);
  35570. if (ret != 0)
  35571. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35572. ret = wc_KyberKey_EncodePublicKey(key, pub2, testData[i][2]);
  35573. if (ret != 0)
  35574. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35575. if (XMEMCMP(pub, pub2, testData[i][2]) != 0)
  35576. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35577. ret = wc_KyberKey_Init(testData[i][0], key, HEAP_HINT, INVALID_DEVID);
  35578. if (ret != 0)
  35579. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35580. ret = wc_KyberKey_DecodePrivateKey(key, priv, testData[i][1]);
  35581. if (ret != 0)
  35582. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35583. ret = wc_KyberKey_Decapsulate(key, ss_dec, ct, testData[i][3]);
  35584. if (ret != 0)
  35585. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35586. if (XMEMCMP(ss, ss_dec, KYBER_SS_SZ) != 0)
  35587. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35588. ret = wc_KyberKey_EncodePrivateKey(key, priv2, testData[i][1]);
  35589. if (ret != 0)
  35590. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35591. if (XMEMCMP(priv, priv2, testData[i][2]) != 0)
  35592. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  35593. }
  35594. wc_FreeRng(&rng);
  35595. #ifdef WOLFSSL_WC_KYBER
  35596. #ifdef WOLFSSL_KYBER512
  35597. ret = kyber512_kat();
  35598. if (ret != 0)
  35599. goto out;
  35600. #endif
  35601. #ifdef WOLFSSL_KYBER768
  35602. ret = kyber768_kat();
  35603. if (ret != 0)
  35604. goto out;
  35605. #endif
  35606. #ifdef WOLFSSL_KYBER1024
  35607. ret = kyber1024_kat();
  35608. if (ret != 0)
  35609. goto out;
  35610. #endif
  35611. #endif /* WOLFSSL_WC_KYBER */
  35612. out:
  35613. if (key_inited)
  35614. wc_KyberKey_Free(key);
  35615. #ifdef WOLFSSL_SMALL_STACK
  35616. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35617. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35618. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35619. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35620. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35621. XFREE(ct, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35622. XFREE(ss, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35623. XFREE(ss_dec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35624. #endif
  35625. return ret;
  35626. }
  35627. #endif /* WOLFSSL_HAVE_KYBER */
  35628. #ifdef HAVE_DILITHIUM
  35629. #ifndef WOLFSSL_DILITHIUM_NO_VERIFY
  35630. static wc_test_ret_t dilithium_param_vfy_test(int param, const byte* pubKey,
  35631. word32 pubKeyLen, const byte* sig, word32 sigLen)
  35632. {
  35633. byte msg[512];
  35634. dilithium_key* key;
  35635. wc_test_ret_t ret;
  35636. int i;
  35637. int res = 0;
  35638. key = (dilithium_key*)XMALLOC(sizeof(*key), HEAP_HINT,
  35639. DYNAMIC_TYPE_TMP_BUFFER);
  35640. if (key == NULL) {
  35641. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  35642. }
  35643. /* make dummy msg */
  35644. for (i = 0; i < (int)sizeof(msg); i++) {
  35645. msg[i] = (byte)i;
  35646. }
  35647. ret = wc_dilithium_init(key);
  35648. if (ret != 0) {
  35649. ret = WC_TEST_RET_ENC_EC(ret);
  35650. return ret;
  35651. }
  35652. ret = wc_dilithium_set_level(key, param);
  35653. if (ret != 0)
  35654. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35655. ret = wc_dilithium_import_public(pubKey, pubKeyLen, key);
  35656. if (ret != 0)
  35657. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35658. ret = wc_dilithium_verify_msg(sig, sigLen, msg, (word32)sizeof(msg), &res,
  35659. key);
  35660. if (ret != 0)
  35661. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35662. if (res != 1)
  35663. ERROR_OUT(WC_TEST_RET_ENC_EC(res), out);
  35664. out:
  35665. wc_dilithium_free(key);
  35666. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35667. return ret;
  35668. }
  35669. #ifndef WOLFSSL_NO_ML_DSA_44
  35670. static wc_test_ret_t dilithium_param_44_vfy_test(void)
  35671. {
  35672. WOLFSSL_SMALL_STACK_STATIC const byte ml_dsa_44_pub_key[] = {
  35673. 0xea, 0x05, 0x24, 0x0d, 0x80, 0x72, 0x25, 0x55, 0xf4, 0x5b,
  35674. 0xc2, 0x13, 0x8b, 0x87, 0x5d, 0x31, 0x99, 0x2f, 0x1d, 0xa9,
  35675. 0x41, 0x09, 0x05, 0x76, 0xa7, 0xb7, 0x5e, 0x8c, 0x44, 0xe2,
  35676. 0x64, 0x79, 0xa0, 0xec, 0x1f, 0x24, 0xb6, 0xc8, 0x05, 0x5b,
  35677. 0xc1, 0x18, 0xb0, 0xb7, 0xcf, 0x8c, 0x60, 0x67, 0x6b, 0x81,
  35678. 0x44, 0x27, 0xb6, 0x0e, 0xfd, 0x9b, 0xc3, 0xcb, 0x52, 0x31,
  35679. 0xfa, 0xc9, 0x34, 0x8d, 0x22, 0x1e, 0x07, 0x9d, 0x96, 0x6a,
  35680. 0x63, 0x83, 0x5c, 0xd7, 0x83, 0x2d, 0x7f, 0x48, 0x64, 0x79,
  35681. 0xca, 0xb4, 0x9f, 0xa2, 0x02, 0xb7, 0x86, 0x1d, 0x0e, 0xc7,
  35682. 0xf9, 0x6c, 0x07, 0xc0, 0x35, 0x6a, 0x34, 0x79, 0x7c, 0xb8,
  35683. 0x0f, 0xed, 0x98, 0x50, 0xfb, 0x51, 0xe0, 0x36, 0x44, 0x4c,
  35684. 0xc6, 0x35, 0xa2, 0xbb, 0x55, 0xb0, 0x5c, 0x39, 0x08, 0x02,
  35685. 0x20, 0x35, 0x5c, 0x56, 0x6d, 0x2e, 0xb9, 0xef, 0x21, 0x26,
  35686. 0x87, 0x87, 0x85, 0x8a, 0x32, 0xb5, 0xa7, 0x68, 0x70, 0x3a,
  35687. 0xfd, 0x0d, 0x21, 0x48, 0x91, 0xa3, 0x29, 0xc1, 0x2a, 0x38,
  35688. 0xe5, 0x26, 0x31, 0x1f, 0x42, 0xde, 0x0b, 0x25, 0xff, 0x1d,
  35689. 0x6b, 0xb4, 0xe0, 0x5d, 0x2d, 0xcf, 0x44, 0xd5, 0x7d, 0xc4,
  35690. 0xf6, 0x95, 0xf2, 0x06, 0x4f, 0x83, 0x88, 0x9d, 0x1e, 0xeb,
  35691. 0x1c, 0x09, 0x45, 0x62, 0x67, 0x3d, 0xff, 0x51, 0x47, 0xe8,
  35692. 0xbc, 0x9b, 0x03, 0x1f, 0xc7, 0x72, 0x65, 0xce, 0xa8, 0x8c,
  35693. 0xc2, 0xa0, 0xc2, 0xbd, 0x5b, 0x7c, 0x17, 0x16, 0x8b, 0x72,
  35694. 0xfa, 0xb1, 0xbd, 0xdf, 0x49, 0xd6, 0xa1, 0x00, 0x65, 0xbe,
  35695. 0x82, 0xe7, 0x68, 0xc7, 0xe7, 0xbc, 0xc2, 0xa4, 0xdb, 0xaa,
  35696. 0xcc, 0xea, 0x41, 0x52, 0x7f, 0x56, 0xb4, 0x68, 0x1f, 0x92,
  35697. 0x96, 0x0f, 0xce, 0xd4, 0xd0, 0x87, 0x4c, 0x4a, 0x73, 0xb5,
  35698. 0x6c, 0xd4, 0x69, 0x55, 0x15, 0x47, 0xdc, 0x94, 0x7f, 0xd2,
  35699. 0x54, 0x5e, 0xb2, 0x90, 0xc2, 0x47, 0xe4, 0xf5, 0xde, 0x8b,
  35700. 0x9b, 0xc6, 0x5d, 0x50, 0x95, 0x60, 0xe0, 0xf0, 0xa7, 0x4e,
  35701. 0xe0, 0xcd, 0x41, 0x09, 0xef, 0xb3, 0x3d, 0x90, 0x5c, 0x77,
  35702. 0x54, 0xec, 0x9e, 0x5d, 0x8a, 0xe7, 0x09, 0x5c, 0xc9, 0x58,
  35703. 0x0c, 0xd0, 0x42, 0x35, 0xd2, 0x14, 0x59, 0x38, 0x69, 0xad,
  35704. 0xf9, 0xb5, 0xbf, 0x8a, 0x8e, 0x33, 0xd8, 0x5e, 0x7a, 0x55,
  35705. 0xd0, 0x53, 0x15, 0x40, 0x4e, 0xc5, 0x86, 0xd7, 0x8f, 0x5f,
  35706. 0x2f, 0x55, 0x82, 0xc2, 0x4f, 0x16, 0xe5, 0xea, 0x1c, 0xbc,
  35707. 0xff, 0x5e, 0x1f, 0x39, 0x46, 0x70, 0x54, 0x7a, 0x3a, 0x27,
  35708. 0x16, 0x1a, 0x2b, 0x6c, 0xd2, 0xb7, 0x80, 0xd3, 0xd1, 0x9d,
  35709. 0x25, 0x59, 0xed, 0xe6, 0x51, 0xb1, 0xf2, 0xad, 0x7e, 0x51,
  35710. 0x78, 0x14, 0x2b, 0x19, 0xae, 0x64, 0x72, 0x0f, 0xd8, 0x18,
  35711. 0x79, 0x8e, 0x66, 0x88, 0xd3, 0xa4, 0xa3, 0xc3, 0x76, 0x21,
  35712. 0xcb, 0xe4, 0x79, 0x5e, 0x95, 0x74, 0xe3, 0x31, 0x18, 0x79,
  35713. 0xed, 0xc7, 0xe7, 0xfb, 0x86, 0x48, 0x1b, 0x7b, 0x75, 0x5b,
  35714. 0x7f, 0x7c, 0x82, 0xc5, 0xab, 0x11, 0xb4, 0x5d, 0x59, 0x6f,
  35715. 0x78, 0xb2, 0xa5, 0x39, 0xc6, 0x63, 0x38, 0x6c, 0xeb, 0x50,
  35716. 0x06, 0x14, 0x76, 0xf0, 0xe8, 0xfb, 0x11, 0x95, 0x1f, 0x9d,
  35717. 0x9c, 0xa6, 0xe1, 0xe2, 0x0d, 0xa3, 0x66, 0xfc, 0x20, 0x83,
  35718. 0x50, 0x0e, 0x53, 0x75, 0xb5, 0x12, 0xf4, 0xdf, 0x31, 0x46,
  35719. 0x83, 0xac, 0x5b, 0xf3, 0x99, 0xa6, 0xd1, 0x7b, 0x2b, 0xc5,
  35720. 0xdc, 0x71, 0x07, 0x27, 0x33, 0x35, 0x34, 0xf5, 0x30, 0x19,
  35721. 0xc1, 0x3b, 0xba, 0x8a, 0xaf, 0x7e, 0x49, 0x93, 0x48, 0x5b,
  35722. 0x38, 0xc0, 0xbc, 0x2e, 0xc7, 0x59, 0x1b, 0xd9, 0xf5, 0xcc,
  35723. 0x86, 0xf5, 0x7b, 0x4d, 0xd7, 0x39, 0xa7, 0xa2, 0x56, 0x20,
  35724. 0x48, 0x98, 0x7d, 0x4f, 0x75, 0x56, 0x9b, 0xb8, 0x95, 0x45,
  35725. 0x17, 0xf3, 0x86, 0x3d, 0x97, 0x0a, 0x49, 0x1b, 0xca, 0xff,
  35726. 0x20, 0xc0, 0x24, 0x2c, 0x51, 0xc2, 0x0a, 0x3c, 0xbf, 0x07,
  35727. 0x60, 0x1c, 0x88, 0x85, 0x9b, 0x85, 0x2d, 0x4a, 0xfe, 0x5a,
  35728. 0x1c, 0x90, 0xf5, 0x90, 0x12, 0xd3, 0x03, 0x3c, 0x8c, 0x2e,
  35729. 0x95, 0x4a, 0x47, 0x76, 0x0f, 0x1f, 0x5d, 0x9e, 0xed, 0xc5,
  35730. 0x64, 0xc4, 0x9b, 0xbf, 0x86, 0xc5, 0x63, 0x84, 0x33, 0x00,
  35731. 0xf1, 0x26, 0x18, 0x21, 0xf3, 0x88, 0x1a, 0x08, 0x18, 0x6d,
  35732. 0x2f, 0xef, 0xd5, 0xeb, 0x2f, 0x69, 0xc8, 0x6e, 0x92, 0x34,
  35733. 0xfc, 0x72, 0x3d, 0x9a, 0xa7, 0x9e, 0x51, 0xfb, 0x56, 0xe3,
  35734. 0xdc, 0xf4, 0x8f, 0x9b, 0x6d, 0x0d, 0x2a, 0xec, 0x66, 0x12,
  35735. 0x26, 0x35, 0xbd, 0x61, 0xc2, 0x67, 0x19, 0xf5, 0x7e, 0xa1,
  35736. 0x67, 0xa2, 0x9c, 0x3b, 0x67, 0xb0, 0xc2, 0x51, 0x6a, 0x37,
  35737. 0x7c, 0x48, 0xe9, 0x4b, 0xb9, 0xa3, 0x38, 0x2f, 0xfc, 0xde,
  35738. 0xb4, 0x7c, 0xda, 0x52, 0x84, 0x0b, 0xb0, 0xd9, 0x08, 0xe9,
  35739. 0x7a, 0x4a, 0x6f, 0x79, 0x29, 0x3d, 0xc4, 0x5c, 0x78, 0xee,
  35740. 0x63, 0xb6, 0x96, 0x68, 0xd9, 0x82, 0x4e, 0xc1, 0x1b, 0x6f,
  35741. 0x52, 0xf5, 0xb3, 0xfb, 0xe8, 0xc4, 0x2a, 0x07, 0xc6, 0x3b,
  35742. 0x85, 0x0d, 0xf4, 0xbf, 0xb0, 0x6b, 0xfb, 0xce, 0x1d, 0xb4,
  35743. 0xbf, 0x63, 0x0b, 0x91, 0x67, 0xc4, 0xa3, 0x06, 0xa4, 0xaf,
  35744. 0x6c, 0xd3, 0xe5, 0x8b, 0x87, 0x4e, 0x64, 0x9c, 0xb1, 0xf3,
  35745. 0x70, 0x7c, 0x68, 0x43, 0x46, 0x13, 0x46, 0xee, 0x27, 0x75,
  35746. 0x12, 0x45, 0x42, 0xde, 0xa5, 0x8d, 0xcf, 0xf7, 0x09, 0x87,
  35747. 0xa8, 0x80, 0x3d, 0xb6, 0x45, 0xee, 0x41, 0x2d, 0x7c, 0x45,
  35748. 0x01, 0x9d, 0xaa, 0x78, 0xa8, 0x10, 0xa4, 0xfd, 0xb5, 0x5f,
  35749. 0xee, 0x0f, 0x77, 0xba, 0x73, 0xff, 0x49, 0xdc, 0xfa, 0x39,
  35750. 0xd6, 0xa3, 0x6f, 0x25, 0xb9, 0x63, 0x2c, 0x92, 0xc5, 0xdf,
  35751. 0xfb, 0xba, 0x89, 0xf9, 0xfa, 0x94, 0x5b, 0x6f, 0x5a, 0x4d,
  35752. 0x1c, 0xe4, 0xc9, 0x10, 0xf9, 0xa0, 0xe8, 0xc4, 0xcb, 0x55,
  35753. 0x1a, 0xdb, 0x56, 0x5f, 0x8e, 0x91, 0x03, 0x23, 0xca, 0xb0,
  35754. 0x1f, 0xef, 0xb8, 0x6c, 0x13, 0x5a, 0x99, 0x25, 0xf0, 0x49,
  35755. 0xa9, 0x5a, 0x45, 0xf7, 0xfd, 0x1a, 0xc2, 0x71, 0x06, 0xe3,
  35756. 0x2d, 0x25, 0x64, 0xb0, 0x52, 0x12, 0x03, 0x62, 0xc7, 0xb6,
  35757. 0xf9, 0xdc, 0x1f, 0x78, 0xff, 0x8b, 0xfa, 0xde, 0x7f, 0x71,
  35758. 0xa6, 0x35, 0x3e, 0xac, 0x20, 0x54, 0x94, 0xa7, 0x2e, 0x9d,
  35759. 0x47, 0x17, 0x4b, 0xad, 0x92, 0xb3, 0x14, 0x26, 0x8c, 0x5a,
  35760. 0xd0, 0x16, 0x4b, 0x22, 0xe9, 0x0c, 0x79, 0x6b, 0x8e, 0xac,
  35761. 0x0d, 0x12, 0xf5, 0x66, 0x8e, 0x82, 0x1a, 0x44, 0xf3, 0xe9,
  35762. 0x56, 0x5a, 0xcd, 0x1c, 0x1b, 0x81, 0x7b, 0x63, 0x59, 0xfe,
  35763. 0xc8, 0xc0, 0xe3, 0xda, 0x16, 0x6b, 0x6f, 0x0d, 0xba, 0x0e,
  35764. 0x47, 0x12, 0x86, 0x9e, 0xf0, 0x3b, 0x4d, 0x87, 0x3b, 0xf2,
  35765. 0x75, 0x73, 0x2d, 0xdf, 0xca, 0x76, 0x0b, 0xbd, 0xe7, 0xb7,
  35766. 0x74, 0x24, 0xf3, 0xc6, 0xe6, 0x75, 0x3f, 0x8b, 0x6a, 0xd9,
  35767. 0xad, 0xed, 0xc0, 0x70, 0x04, 0x1e, 0x0b, 0x8e, 0x8b, 0x7f,
  35768. 0xea, 0xbc, 0x39, 0x6b, 0x8a, 0x44, 0xa6, 0x9a, 0x2d, 0x0d,
  35769. 0x8c, 0x21, 0x60, 0x09, 0xd2, 0x4a, 0xe0, 0x62, 0xcf, 0xfa,
  35770. 0xe8, 0x9b, 0x35, 0x6f, 0x23, 0x2f, 0xb5, 0x65, 0x08, 0x60,
  35771. 0x92, 0x15, 0xd0, 0x5b, 0x63, 0xcc, 0x65, 0x05, 0xd1, 0xef,
  35772. 0x0f, 0x7e, 0x1b, 0xb3, 0x8e, 0xc6, 0x12, 0x85, 0xc9, 0x82,
  35773. 0x53, 0x79, 0x2e, 0x80, 0x5f, 0x0c, 0x7b, 0xc7, 0x1c, 0x83,
  35774. 0x41, 0x06, 0xd8, 0x41, 0xc9, 0xe7, 0xb9, 0x4b, 0xa1, 0x61,
  35775. 0xc6, 0x86, 0x67, 0xf5, 0x10, 0xf7, 0x34, 0x0d, 0x39, 0x9e,
  35776. 0x2b, 0x5f, 0x19, 0x06, 0x02, 0xa5, 0x02, 0x23, 0x71, 0xc2,
  35777. 0x12, 0x65, 0xcc, 0x81, 0x06, 0xfd, 0x8d, 0x09, 0x68, 0x37,
  35778. 0x06, 0x3b, 0xff, 0xc4, 0x24, 0xb3, 0x1f, 0xd6, 0xe6, 0x8f,
  35779. 0x9c, 0x74, 0x2c, 0x5e, 0xc5, 0xf4, 0xe9, 0xeb, 0xca, 0xd3,
  35780. 0x04, 0x5b, 0x92, 0x9e, 0x5c, 0x1a, 0x1d, 0xa1, 0xa7, 0x34,
  35781. 0xd2, 0x05, 0xae, 0xdb, 0x3d, 0x71, 0x10, 0x6e, 0x30, 0xd9,
  35782. 0xa3, 0x44, 0xa0, 0xbd, 0x9e, 0x7b, 0xb5, 0x12, 0x8a, 0x12,
  35783. 0x07, 0x60, 0xd7, 0x1f, 0x92, 0xe6, 0xfe, 0x04, 0xa9, 0x3e,
  35784. 0x62, 0x64, 0x00, 0x5f, 0x7c, 0x7b, 0x34, 0x09, 0xeb, 0x4a,
  35785. 0x18, 0x9e, 0x77, 0x72, 0x3a, 0x31, 0x1a, 0x62, 0x2a, 0xb5,
  35786. 0xcb, 0x4e, 0x53, 0xce, 0xad, 0x8b, 0x5a, 0x20, 0x4f, 0xd7,
  35787. 0x3e, 0x16, 0xf8, 0x10, 0xe2, 0xae, 0xbd, 0x3f, 0x02, 0xa9,
  35788. 0x18, 0xa0, 0x01, 0x18, 0x84, 0x95, 0x22, 0x2e, 0x93, 0x76,
  35789. 0x44, 0x4e, 0x11, 0x7b, 0x03, 0x51, 0x50, 0x19, 0x79, 0xe7,
  35790. 0xbb, 0x5c, 0x7b, 0xca, 0x74, 0xb4, 0x25, 0x26, 0xdb, 0x66,
  35791. 0xaa, 0x0b, 0x21, 0x07, 0xfb, 0x7a, 0x96, 0x10, 0x7d, 0x99,
  35792. 0xa9, 0x16, 0xcb, 0x0e, 0xba, 0x63, 0xab, 0x95, 0xfc, 0x5a,
  35793. 0xbe, 0xa6, 0x7f, 0xd8, 0xb4, 0xcd, 0x7c, 0xc5, 0xd0, 0xb1,
  35794. 0x1b, 0x48, 0x40, 0xfb, 0xe6, 0x2f, 0x2b, 0x94, 0xfe, 0x68,
  35795. 0xa2, 0xc4, 0x36, 0xd9, 0xcd, 0xc1, 0x93, 0x6d, 0xef, 0x39,
  35796. 0x5e, 0x43, 0x30, 0x5a, 0x2e, 0x66, 0xb6, 0xf2, 0xed, 0x9a,
  35797. 0x8d, 0x12, 0xdf, 0x5c, 0xae, 0xad, 0x16, 0x12, 0x7e, 0x81,
  35798. 0x82, 0x91, 0x7d, 0x2b, 0x12, 0xe9, 0x96, 0xb8, 0xb7, 0x42,
  35799. 0xcb, 0x1f, 0xf8, 0xd1, 0xfd, 0x83, 0x7a, 0xe4, 0x36, 0x1d,
  35800. 0x04, 0x27, 0x4c, 0xe5, 0xbd, 0x75, 0x24, 0xf7, 0xbd, 0xb6,
  35801. 0x6a, 0x68, 0x4e, 0x2c, 0x1b, 0x56, 0x3e, 0x60, 0xa4, 0x42,
  35802. 0xca, 0x7a, 0x54, 0xe5, 0x06, 0xe3, 0xda, 0x05, 0xf7, 0x77,
  35803. 0x36, 0x8b, 0x81, 0x26, 0x99, 0x92, 0x42, 0xda, 0x45, 0xb1,
  35804. 0xfe, 0x4b,
  35805. };
  35806. WOLFSSL_SMALL_STACK_STATIC const byte ml_dsa_44_sig[] = {
  35807. 0x5e, 0xc1, 0xce, 0x0e, 0x31, 0xea, 0x10, 0x52, 0xa3, 0x7a,
  35808. 0xfe, 0x4d, 0xac, 0x07, 0x89, 0x5a, 0x45, 0xbd, 0x5a, 0xe5,
  35809. 0x22, 0xed, 0x98, 0x4d, 0x2f, 0xc8, 0x27, 0x00, 0x99, 0x40,
  35810. 0x00, 0x79, 0xcd, 0x93, 0x27, 0xd0, 0x40, 0x33, 0x79, 0x4f,
  35811. 0xe5, 0x16, 0x89, 0x9f, 0xbd, 0xa6, 0x3f, 0xdd, 0x68, 0x74,
  35812. 0x73, 0xc3, 0x97, 0x54, 0x11, 0x1d, 0xc8, 0xb8, 0xc8, 0xfd,
  35813. 0x3a, 0xbe, 0xca, 0x17, 0x0f, 0x10, 0x6d, 0x89, 0x6d, 0xe0,
  35814. 0xb2, 0xff, 0x3b, 0xe5, 0xa1, 0x75, 0xea, 0x35, 0x16, 0xa3,
  35815. 0x0c, 0x6e, 0x4a, 0x7b, 0xdb, 0x28, 0xc6, 0x2a, 0x76, 0x0e,
  35816. 0x78, 0x78, 0xa0, 0x4f, 0x4e, 0xf8, 0x99, 0xff, 0xe7, 0x47,
  35817. 0x7e, 0xc4, 0x62, 0xa7, 0xb4, 0xb9, 0x2b, 0xc1, 0xc7, 0xd0,
  35818. 0x00, 0xb6, 0xaa, 0xa7, 0x37, 0xd5, 0x1e, 0x19, 0xc4, 0xc4,
  35819. 0x59, 0x2f, 0xa5, 0x09, 0xa3, 0xda, 0x5d, 0xd4, 0x48, 0x64,
  35820. 0x16, 0x0e, 0x92, 0xdf, 0x61, 0xb7, 0x25, 0x3b, 0x90, 0x5a,
  35821. 0x08, 0xb5, 0x88, 0xe8, 0x64, 0x80, 0x63, 0xee, 0xbf, 0x59,
  35822. 0x0f, 0x4a, 0x48, 0x1e, 0x77, 0xa9, 0x46, 0xc6, 0x9c, 0x0b,
  35823. 0x83, 0xad, 0xb5, 0xbf, 0xb5, 0x5b, 0x99, 0xf3, 0x55, 0xe8,
  35824. 0xe5, 0xe7, 0x5c, 0x12, 0xac, 0x06, 0x06, 0xe0, 0xc0, 0x32,
  35825. 0x5d, 0xb6, 0x9f, 0x2b, 0x8e, 0x19, 0x5c, 0x2a, 0x58, 0xbb,
  35826. 0x37, 0xf1, 0x68, 0x56, 0x8b, 0x74, 0x94, 0x58, 0x48, 0x28,
  35827. 0xee, 0xf7, 0x0a, 0x8f, 0xad, 0x43, 0x67, 0xe1, 0xa3, 0x8c,
  35828. 0x3b, 0x35, 0x48, 0xcc, 0x52, 0x14, 0x36, 0x99, 0x18, 0x71,
  35829. 0x1c, 0xb2, 0xfc, 0x82, 0xda, 0xac, 0xd5, 0x55, 0x0a, 0x77,
  35830. 0x44, 0x6a, 0x48, 0xed, 0xfc, 0x5a, 0x68, 0xa6, 0x4d, 0x65,
  35831. 0xe7, 0x30, 0xaa, 0x23, 0x66, 0x84, 0xdf, 0x83, 0xf1, 0x17,
  35832. 0x5c, 0x46, 0xfe, 0x63, 0xcb, 0xc3, 0x6e, 0x4e, 0x47, 0x8d,
  35833. 0x30, 0x48, 0x06, 0xda, 0x97, 0x6b, 0x04, 0x5d, 0x44, 0xf3,
  35834. 0xb7, 0x2a, 0x6d, 0x2b, 0xbb, 0xcd, 0x97, 0x4e, 0x26, 0x8e,
  35835. 0xc9, 0x03, 0x0b, 0x5d, 0x68, 0xed, 0x81, 0xf7, 0x19, 0x61,
  35836. 0x81, 0xe9, 0xac, 0x3a, 0x35, 0xcd, 0xe8, 0xfd, 0x99, 0xdb,
  35837. 0x89, 0x83, 0x7d, 0x23, 0x6a, 0xc1, 0xc1, 0x10, 0xe9, 0xd3,
  35838. 0xfa, 0x9e, 0x5a, 0xcd, 0x73, 0xa3, 0x0a, 0x37, 0xa3, 0x12,
  35839. 0xef, 0x72, 0xa2, 0x28, 0xd4, 0x3d, 0x67, 0x53, 0x24, 0x0d,
  35840. 0x61, 0x98, 0xbb, 0x07, 0xf3, 0xa7, 0x79, 0x22, 0x74, 0x57,
  35841. 0x99, 0xe8, 0x7a, 0xbf, 0x90, 0x84, 0xa2, 0x6b, 0x29, 0x34,
  35842. 0xac, 0xc9, 0xff, 0x67, 0x82, 0xd0, 0xd2, 0x7d, 0x69, 0xc0,
  35843. 0xf3, 0xd7, 0x4b, 0x5c, 0xf2, 0xa8, 0x53, 0x8b, 0x78, 0x57,
  35844. 0xfc, 0x74, 0xf5, 0x81, 0x6e, 0xc2, 0x5b, 0x32, 0x52, 0x9e,
  35845. 0x58, 0x84, 0xa1, 0x71, 0xd5, 0x8c, 0xf5, 0x16, 0x36, 0x4d,
  35846. 0x11, 0xd4, 0xb5, 0xc2, 0x05, 0xc4, 0x03, 0xce, 0x83, 0xea,
  35847. 0x0b, 0x6a, 0x2e, 0xf6, 0x28, 0x5e, 0xb2, 0x40, 0x8c, 0xa3,
  35848. 0x6a, 0xc7, 0xee, 0x04, 0x54, 0x93, 0x0f, 0x3b, 0xf9, 0x57,
  35849. 0x92, 0x00, 0xf1, 0xc7, 0x1b, 0x48, 0x63, 0xcb, 0xd3, 0xdd,
  35850. 0x40, 0x90, 0x46, 0xb0, 0x87, 0x2a, 0xb8, 0xec, 0xbc, 0x07,
  35851. 0x09, 0x83, 0x25, 0xb1, 0x88, 0x2c, 0xa0, 0x0a, 0x40, 0x4f,
  35852. 0xfd, 0xec, 0xfd, 0xbe, 0x18, 0xae, 0xdd, 0x83, 0x89, 0x83,
  35853. 0x2d, 0x10, 0xb4, 0x14, 0x30, 0xac, 0x6c, 0xd9, 0xc9, 0xaa,
  35854. 0xbc, 0xdb, 0x5e, 0x14, 0xab, 0x19, 0x64, 0xaa, 0xb1, 0x9c,
  35855. 0xc3, 0xf5, 0xdc, 0x2b, 0xcd, 0x26, 0x0b, 0x81, 0x1a, 0x0e,
  35856. 0x0a, 0xd6, 0x39, 0x79, 0x10, 0x06, 0xbf, 0xe0, 0xc1, 0x8b,
  35857. 0x20, 0x24, 0x90, 0x8b, 0x0f, 0xa4, 0x2d, 0x2d, 0x46, 0x2a,
  35858. 0xd4, 0xf3, 0xa9, 0x58, 0x4b, 0xd9, 0xa6, 0x6c, 0x75, 0x3d,
  35859. 0xbc, 0x36, 0x76, 0x7f, 0xef, 0x1b, 0xa1, 0x41, 0xba, 0xd0,
  35860. 0xfe, 0x16, 0x19, 0xc3, 0x92, 0xe3, 0x59, 0x07, 0x3f, 0x48,
  35861. 0x11, 0x70, 0xe0, 0x8a, 0xff, 0x97, 0xbc, 0x71, 0xd5, 0xb9,
  35862. 0x4a, 0x9b, 0x4c, 0xb8, 0x4b, 0x50, 0xd6, 0x43, 0xe8, 0x84,
  35863. 0x0a, 0x95, 0xd0, 0x20, 0x28, 0xd3, 0x20, 0x4a, 0x0e, 0x1b,
  35864. 0xe6, 0x5d, 0x2f, 0x0c, 0xdb, 0x76, 0xab, 0xa3, 0xc2, 0xad,
  35865. 0xd5, 0x86, 0xae, 0xb9, 0x26, 0xb2, 0x5d, 0x72, 0x27, 0xbb,
  35866. 0xec, 0x23, 0x9f, 0x42, 0x90, 0x58, 0xe1, 0xf8, 0xe9, 0x63,
  35867. 0xdf, 0x1a, 0x46, 0x53, 0x65, 0x05, 0xfb, 0x20, 0x21, 0xa6,
  35868. 0x64, 0xc8, 0x5c, 0x67, 0x6b, 0x41, 0x6c, 0x04, 0x34, 0xeb,
  35869. 0x05, 0x71, 0xeb, 0xbe, 0xed, 0x6d, 0xa2, 0x96, 0x67, 0x45,
  35870. 0xe7, 0x47, 0x22, 0x64, 0xaf, 0x82, 0xf8, 0x78, 0x0e, 0xe6,
  35871. 0xa1, 0x4a, 0x2d, 0x82, 0x1e, 0xd0, 0xc2, 0x79, 0x4e, 0x29,
  35872. 0x89, 0xd9, 0xf3, 0x3f, 0xb6, 0xc4, 0xee, 0x69, 0xb2, 0x8f,
  35873. 0x8b, 0xd9, 0x13, 0xd9, 0x6e, 0x3a, 0xc5, 0x9f, 0xdf, 0x25,
  35874. 0xb7, 0xc3, 0x16, 0xb8, 0xa2, 0x85, 0x17, 0xae, 0xe9, 0x95,
  35875. 0x5d, 0xb8, 0x1d, 0x21, 0xbb, 0xd9, 0x38, 0x11, 0x8f, 0x44,
  35876. 0xea, 0xe8, 0x4c, 0x91, 0x82, 0xf5, 0x45, 0xee, 0x8f, 0xf5,
  35877. 0x6a, 0x0d, 0x08, 0xe7, 0x6b, 0xb0, 0x91, 0xd5, 0x42, 0x17,
  35878. 0x8c, 0x37, 0x6a, 0x5a, 0x0a, 0x87, 0x53, 0x76, 0xc3, 0x59,
  35879. 0x35, 0x13, 0x1c, 0xf1, 0x72, 0x2c, 0x2b, 0xb2, 0x9e, 0xda,
  35880. 0x10, 0x2a, 0xce, 0x38, 0xb4, 0x67, 0x8c, 0x4b, 0x08, 0xa1,
  35881. 0xb6, 0xa3, 0x08, 0x9c, 0xeb, 0xd8, 0x93, 0x1b, 0x29, 0x5a,
  35882. 0xa7, 0x03, 0x17, 0x7e, 0xec, 0x58, 0x6b, 0x5b, 0xc5, 0x46,
  35883. 0x03, 0x33, 0x7f, 0x0e, 0x93, 0x9a, 0xdd, 0xb5, 0x89, 0xb1,
  35884. 0x16, 0x4c, 0xa7, 0xd8, 0x0e, 0x73, 0xd8, 0xc3, 0xd2, 0x36,
  35885. 0x85, 0x66, 0xcb, 0x5b, 0x64, 0xf2, 0xdc, 0xba, 0x39, 0xcc,
  35886. 0xa5, 0xe0, 0x9b, 0xaa, 0x2a, 0x95, 0x6d, 0xdc, 0x49, 0xde,
  35887. 0x3b, 0x61, 0xa2, 0x3b, 0x1f, 0xed, 0x32, 0xfa, 0x10, 0xe4,
  35888. 0x88, 0x59, 0xca, 0x5a, 0xe4, 0xf9, 0x5e, 0xe2, 0xca, 0x21,
  35889. 0x5a, 0xdc, 0x02, 0x73, 0x7a, 0xc8, 0x90, 0x7a, 0x8e, 0x91,
  35890. 0x19, 0x04, 0x53, 0x3c, 0x50, 0x15, 0x8a, 0x84, 0x93, 0x8f,
  35891. 0xac, 0x99, 0x82, 0xdd, 0xc6, 0xce, 0xfb, 0x18, 0x84, 0x29,
  35892. 0x2a, 0x8d, 0xa2, 0xc5, 0x7f, 0x87, 0xce, 0x4c, 0xf5, 0xdf,
  35893. 0x73, 0xd2, 0xba, 0xc2, 0x4f, 0xe3, 0x74, 0xa5, 0x8f, 0xc3,
  35894. 0xf4, 0x99, 0xd1, 0xe8, 0x4e, 0xb8, 0xe0, 0x2e, 0xef, 0xd6,
  35895. 0x87, 0x70, 0xcf, 0x45, 0x3b, 0xff, 0x03, 0xfd, 0x59, 0x7f,
  35896. 0x7c, 0xd0, 0x4e, 0x49, 0xf7, 0xd5, 0x08, 0xd9, 0x06, 0x53,
  35897. 0x90, 0x0a, 0x5a, 0x1b, 0x2e, 0xf5, 0xb0, 0x85, 0xb6, 0xb6,
  35898. 0x61, 0xa5, 0x71, 0x47, 0xbf, 0x4a, 0xf6, 0xae, 0x9a, 0x19,
  35899. 0x6c, 0xd8, 0x2d, 0x9b, 0xb4, 0x40, 0x9e, 0x15, 0x77, 0x2e,
  35900. 0x7e, 0xe9, 0xb4, 0x3d, 0x0f, 0x1b, 0xb5, 0x1c, 0xc2, 0x58,
  35901. 0x4e, 0x4b, 0xf6, 0x53, 0x9e, 0x6f, 0x09, 0x55, 0xa0, 0xb8,
  35902. 0x73, 0x11, 0x64, 0x70, 0x54, 0xb4, 0xcb, 0xb7, 0x27, 0xe5,
  35903. 0xdf, 0x58, 0x67, 0x5b, 0xc0, 0xd6, 0xf5, 0x64, 0xa6, 0x66,
  35904. 0x6d, 0xdf, 0xd8, 0xf8, 0xd6, 0x85, 0xba, 0xba, 0x30, 0xa7,
  35905. 0xca, 0x34, 0xf4, 0x9a, 0xba, 0x0a, 0xfb, 0x0e, 0xa0, 0x65,
  35906. 0x98, 0x78, 0xee, 0xaa, 0x14, 0x6a, 0x99, 0x77, 0x67, 0xad,
  35907. 0x01, 0x95, 0x5e, 0x50, 0x22, 0xe9, 0x74, 0x95, 0xa7, 0x13,
  35908. 0x3f, 0xdd, 0xa6, 0x69, 0x64, 0xf6, 0x50, 0x06, 0x6d, 0xba,
  35909. 0x90, 0x5a, 0x8c, 0x81, 0xa0, 0xda, 0x55, 0xe9, 0x97, 0x0e,
  35910. 0xd7, 0x10, 0x8e, 0x1f, 0x23, 0x65, 0xd9, 0x14, 0xd4, 0xde,
  35911. 0xa5, 0xf9, 0xec, 0xb6, 0xad, 0x65, 0xce, 0x0b, 0x1b, 0x0a,
  35912. 0x4c, 0x7d, 0xb0, 0x97, 0xa6, 0xfe, 0x67, 0xfb, 0x4f, 0x8f,
  35913. 0x00, 0x92, 0xb6, 0x0d, 0x20, 0x78, 0x65, 0x1d, 0x9a, 0x56,
  35914. 0x57, 0xc6, 0x15, 0x88, 0xba, 0x55, 0x02, 0x7a, 0x9a, 0xac,
  35915. 0x50, 0x4c, 0xc7, 0x9e, 0x66, 0x8b, 0xfc, 0xf3, 0x67, 0x48,
  35916. 0x07, 0xbf, 0x84, 0x94, 0x9b, 0x22, 0x2a, 0xae, 0x1b, 0x25,
  35917. 0xe9, 0x94, 0x06, 0xa7, 0xe8, 0x61, 0x52, 0x89, 0xdc, 0x93,
  35918. 0x6e, 0x89, 0xdc, 0x30, 0x6e, 0xd9, 0xee, 0xcb, 0x12, 0x38,
  35919. 0x58, 0x9d, 0x8b, 0xc5, 0x05, 0x2c, 0x50, 0x4e, 0xc8, 0xc2,
  35920. 0xe0, 0x65, 0xb6, 0x49, 0xc4, 0xf0, 0x1e, 0x5c, 0x8e, 0x3c,
  35921. 0xe9, 0x77, 0xd2, 0x9e, 0xa8, 0xd5, 0xf5, 0xd9, 0xc5, 0xad,
  35922. 0x5b, 0x74, 0x48, 0x08, 0x3a, 0x30, 0x84, 0x57, 0x71, 0x1e,
  35923. 0x69, 0x45, 0x09, 0xdd, 0xea, 0x62, 0xec, 0x7c, 0xa3, 0xf9,
  35924. 0x92, 0xee, 0x16, 0xdc, 0xe5, 0x9d, 0xcf, 0xb7, 0x08, 0x51,
  35925. 0x8a, 0x76, 0x3a, 0x23, 0x94, 0x50, 0x8e, 0x4d, 0x3a, 0xea,
  35926. 0xf3, 0xc1, 0x53, 0x2c, 0x65, 0x9c, 0x36, 0x8c, 0x10, 0xe3,
  35927. 0x9c, 0x01, 0xa4, 0xe6, 0x45, 0x77, 0xa6, 0x5d, 0x7e, 0x37,
  35928. 0x31, 0x95, 0x2f, 0xec, 0x61, 0x92, 0x69, 0x65, 0x53, 0x54,
  35929. 0x6d, 0xbe, 0x9e, 0x5a, 0x68, 0x12, 0xc4, 0xe7, 0xe4, 0x06,
  35930. 0x51, 0x5a, 0xc0, 0x63, 0xb9, 0x69, 0xb8, 0x3c, 0xd8, 0xae,
  35931. 0x8b, 0xff, 0x96, 0x4d, 0x55, 0xce, 0x25, 0x2b, 0x8b, 0x89,
  35932. 0xc9, 0x3a, 0x16, 0x48, 0x2a, 0x73, 0xb2, 0x70, 0x8b, 0x62,
  35933. 0xd5, 0xb1, 0xa0, 0x30, 0xe5, 0x46, 0xab, 0x8b, 0xc3, 0xeb,
  35934. 0x37, 0x2f, 0xbd, 0xb8, 0x4e, 0x6c, 0x30, 0xdc, 0x6c, 0x8a,
  35935. 0xf1, 0x89, 0x06, 0xce, 0x64, 0x0a, 0x3e, 0xb2, 0x16, 0x31,
  35936. 0xa1, 0xe4, 0x4b, 0x98, 0xe7, 0xf1, 0x99, 0x76, 0x00, 0x5f,
  35937. 0xd2, 0xd3, 0x30, 0xf0, 0xbf, 0xa7, 0x4a, 0xf6, 0x9e, 0xa5,
  35938. 0x75, 0x74, 0x78, 0xfe, 0xec, 0x72, 0x7c, 0x89, 0xe9, 0xf6,
  35939. 0x0d, 0x7e, 0x15, 0xd6, 0xd8, 0x79, 0x85, 0x3c, 0xcf, 0xb0,
  35940. 0x21, 0xc8, 0x9c, 0x54, 0x87, 0x63, 0xb3, 0x05, 0xbb, 0x8a,
  35941. 0x02, 0xe4, 0x79, 0xdc, 0xa1, 0xa2, 0xd3, 0x19, 0xd8, 0x86,
  35942. 0xff, 0x8a, 0x0e, 0x82, 0x89, 0xaf, 0xaa, 0x62, 0x2e, 0xd4,
  35943. 0xb2, 0xd0, 0x5d, 0x0d, 0x4f, 0x2a, 0xda, 0x0e, 0x9f, 0x8a,
  35944. 0x2b, 0x32, 0xe9, 0x09, 0xf5, 0x55, 0x51, 0xe7, 0xd5, 0x69,
  35945. 0x12, 0xdd, 0x33, 0x6b, 0x3d, 0xd7, 0xe9, 0xfd, 0xb2, 0xa7,
  35946. 0xf5, 0x97, 0x2a, 0x6d, 0x89, 0x30, 0x65, 0x2a, 0x0d, 0xf2,
  35947. 0x00, 0x81, 0xbe, 0xfb, 0xd9, 0xd7, 0x1b, 0xc2, 0x48, 0x7a,
  35948. 0x22, 0x30, 0xae, 0x35, 0xf6, 0x32, 0x41, 0x9d, 0xd9, 0x12,
  35949. 0xb3, 0xa7, 0x6d, 0xba, 0x74, 0x93, 0x2d, 0x0d, 0xb2, 0xb6,
  35950. 0xdc, 0xa9, 0x98, 0x5b, 0x3b, 0xaa, 0x2b, 0x47, 0x06, 0xc4,
  35951. 0x36, 0xfd, 0x04, 0x10, 0x94, 0x61, 0x61, 0x47, 0x1c, 0x02,
  35952. 0x54, 0x85, 0x4a, 0xcb, 0x75, 0x6b, 0x75, 0xf5, 0xb4, 0x61,
  35953. 0x26, 0xb3, 0x12, 0x43, 0x31, 0x55, 0xb5, 0xda, 0x4b, 0xb5,
  35954. 0x11, 0xb4, 0xb8, 0xfb, 0x0a, 0xd9, 0xa7, 0x0e, 0x9f, 0x2a,
  35955. 0x74, 0x01, 0xf6, 0x1a, 0x33, 0x10, 0x9e, 0x66, 0xff, 0x82,
  35956. 0xfa, 0xa9, 0xa4, 0xa0, 0x9b, 0x25, 0x2d, 0x16, 0xbf, 0x60,
  35957. 0x0d, 0x87, 0xea, 0x94, 0xad, 0xdd, 0xc4, 0xd0, 0xa8, 0xdd,
  35958. 0x2d, 0xc7, 0xc8, 0xac, 0x39, 0x9e, 0x87, 0x69, 0xc4, 0x3a,
  35959. 0xbc, 0x28, 0x7e, 0x36, 0x69, 0xfd, 0x20, 0x25, 0xac, 0xa3,
  35960. 0xa7, 0x37, 0x96, 0xe9, 0x8a, 0x65, 0xe4, 0xb0, 0x2a, 0x61,
  35961. 0x23, 0x28, 0x64, 0xff, 0x17, 0x6c, 0x36, 0x9e, 0x0a, 0xba,
  35962. 0xe4, 0x4b, 0xeb, 0x84, 0x24, 0x20, 0x57, 0x0f, 0x34, 0x05,
  35963. 0x95, 0x56, 0xc3, 0x2f, 0x2b, 0xf0, 0x36, 0xef, 0xca, 0x68,
  35964. 0xfe, 0x78, 0xf8, 0x98, 0x09, 0x4a, 0x25, 0xcc, 0x17, 0xbe,
  35965. 0x05, 0x00, 0xff, 0xf9, 0xa5, 0x5b, 0xe6, 0xaa, 0x5b, 0x56,
  35966. 0xb6, 0x89, 0x64, 0x9c, 0x16, 0x48, 0xe1, 0xcd, 0x67, 0x87,
  35967. 0xdd, 0xba, 0xbd, 0x02, 0x0d, 0xd8, 0xb4, 0xc9, 0x7c, 0x37,
  35968. 0x92, 0xd0, 0x39, 0x46, 0xd2, 0xc4, 0x78, 0x13, 0xf0, 0x76,
  35969. 0x45, 0x5f, 0xeb, 0x52, 0xd2, 0x3f, 0x61, 0x87, 0x34, 0x09,
  35970. 0xb7, 0x24, 0x4e, 0x93, 0xf3, 0xc5, 0x10, 0x19, 0x66, 0x66,
  35971. 0x3f, 0x15, 0xe3, 0x05, 0x55, 0x43, 0xb7, 0xf4, 0x62, 0x57,
  35972. 0xb4, 0xd9, 0xef, 0x46, 0x47, 0xb5, 0xfb, 0x79, 0xc9, 0x67,
  35973. 0xc5, 0xc3, 0x18, 0x91, 0x73, 0x75, 0xec, 0xd5, 0x68, 0x2b,
  35974. 0xf6, 0x42, 0xb4, 0xff, 0xfb, 0x27, 0x61, 0x77, 0x28, 0x10,
  35975. 0x6b, 0xce, 0x19, 0xad, 0x87, 0xc3, 0x85, 0xe3, 0x78, 0x00,
  35976. 0xdb, 0x21, 0xee, 0xd8, 0xfa, 0x9c, 0x81, 0x11, 0x97, 0xac,
  35977. 0xd0, 0x50, 0x89, 0x45, 0x23, 0xf6, 0x85, 0x7d, 0x60, 0xb2,
  35978. 0xad, 0x0c, 0x5d, 0xd8, 0x9e, 0xe4, 0xe1, 0x25, 0xb2, 0x13,
  35979. 0x1a, 0x54, 0x54, 0xfd, 0x7b, 0xab, 0x85, 0x20, 0xe8, 0xda,
  35980. 0x52, 0x0f, 0xac, 0x49, 0x70, 0xf1, 0x4c, 0x66, 0x74, 0x8c,
  35981. 0x87, 0x6e, 0xca, 0xc1, 0x0d, 0x92, 0xc0, 0xa8, 0x08, 0xfd,
  35982. 0x0f, 0x60, 0x55, 0xaf, 0x24, 0xcb, 0x04, 0xb7, 0xff, 0xa9,
  35983. 0xc5, 0x07, 0x26, 0xf6, 0xe2, 0x1e, 0x2f, 0xd1, 0x99, 0x6d,
  35984. 0xef, 0xc0, 0xdb, 0x5b, 0xf7, 0x06, 0x80, 0x92, 0x5f, 0x56,
  35985. 0x54, 0xdb, 0x2e, 0xba, 0x93, 0xb2, 0x94, 0xf2, 0xad, 0xbc,
  35986. 0x91, 0x6e, 0x4e, 0xce, 0x21, 0xc4, 0x8b, 0x18, 0xc4, 0xfc,
  35987. 0xab, 0xb4, 0x4f, 0xd7, 0xa2, 0xef, 0x55, 0x00, 0x6d, 0x34,
  35988. 0x17, 0x59, 0x8d, 0x79, 0x75, 0x02, 0xa3, 0x7a, 0x52, 0x57,
  35989. 0x5c, 0x26, 0xb9, 0xae, 0xd6, 0x19, 0x2e, 0x31, 0x02, 0x98,
  35990. 0x98, 0xe5, 0x3d, 0xc2, 0xa5, 0x56, 0xb6, 0x02, 0xae, 0x0d,
  35991. 0x3b, 0x35, 0x97, 0xd2, 0x43, 0x38, 0x8a, 0x65, 0xfa, 0x86,
  35992. 0x20, 0xb7, 0xb5, 0xb0, 0xda, 0x19, 0x01, 0x2f, 0x13, 0xb5,
  35993. 0x6d, 0xbd, 0xb2, 0x34, 0xa7, 0xff, 0xae, 0x7e, 0x8f, 0x98,
  35994. 0x1b, 0xc4, 0x27, 0xbd, 0xa9, 0x64, 0xdc, 0xab, 0x2a, 0xd2,
  35995. 0xb4, 0x27, 0xd0, 0x25, 0xdd, 0xff, 0xdc, 0x0a, 0x96, 0xd3,
  35996. 0x85, 0x3e, 0xc5, 0x11, 0x34, 0x60, 0xa2, 0x33, 0x92, 0x90,
  35997. 0xbb, 0x4c, 0x86, 0xdd, 0xd6, 0x1e, 0xcb, 0x0a, 0x17, 0xc6,
  35998. 0x87, 0x4e, 0x3e, 0x7a, 0x4b, 0xab, 0xef, 0x0a, 0x00, 0x3d,
  35999. 0x94, 0x34, 0x8b, 0x63, 0x36, 0xd9, 0xaf, 0x5d, 0x63, 0x40,
  36000. 0xbb, 0x32, 0x4b, 0x64, 0xf0, 0x31, 0x48, 0xdb, 0x44, 0x2b,
  36001. 0x48, 0x60, 0x6a, 0xea, 0xa4, 0x8c, 0xdd, 0xaf, 0x81, 0x3f,
  36002. 0x86, 0x81, 0x99, 0x7a, 0x98, 0xe1, 0xff, 0x21, 0x7a, 0x28,
  36003. 0xbc, 0x33, 0xe6, 0x4e, 0xb0, 0x85, 0x6b, 0xec, 0x11, 0x37,
  36004. 0x81, 0x7f, 0xf9, 0xdc, 0xbf, 0x1a, 0xa6, 0x6d, 0x4d, 0x0f,
  36005. 0x5b, 0x99, 0x73, 0xb8, 0xd2, 0x6e, 0x37, 0xf0, 0x71, 0xf1,
  36006. 0x1a, 0xc3, 0x5c, 0xea, 0x12, 0x5f, 0x2e, 0x85, 0x3f, 0xfd,
  36007. 0xd5, 0x87, 0x67, 0x9f, 0x67, 0x9f, 0xd7, 0xef, 0x9f, 0x81,
  36008. 0xa4, 0xbc, 0x63, 0x1d, 0x00, 0x81, 0xf6, 0x20, 0x77, 0xae,
  36009. 0x0b, 0x90, 0xe5, 0x9c, 0xa9, 0x44, 0xb5, 0xd7, 0xb1, 0x61,
  36010. 0x33, 0x4f, 0x75, 0xa9, 0xb7, 0xf4, 0xa4, 0x72, 0x9e, 0x72,
  36011. 0xec, 0x7b, 0xcd, 0x83, 0xb3, 0xd6, 0x22, 0x50, 0x50, 0x97,
  36012. 0x0f, 0x63, 0x0f, 0xe1, 0x15, 0xb3, 0x07, 0xb6, 0xa3, 0xfa,
  36013. 0x2f, 0xb5, 0xf3, 0x5b, 0x5d, 0x7f, 0x90, 0x20, 0xcd, 0x5f,
  36014. 0x40, 0x48, 0x87, 0x43, 0xfd, 0xa3, 0x69, 0xdc, 0xf8, 0x51,
  36015. 0x08, 0x67, 0xc2, 0x2d, 0xff, 0xfe, 0xbf, 0x85, 0x3e, 0x80,
  36016. 0xff, 0x91, 0x62, 0xc5, 0x83, 0xe0, 0x80, 0xeb, 0xce, 0xdc,
  36017. 0xff, 0xb1, 0xdb, 0x02, 0xb7, 0x01, 0x1e, 0xa6, 0xf0, 0x32,
  36018. 0xfb, 0x95, 0x6a, 0x47, 0x44, 0x84, 0x42, 0x6e, 0x3a, 0xb1,
  36019. 0xcf, 0xf9, 0x28, 0xb4, 0x3a, 0x8e, 0xa7, 0x8d, 0x48, 0x81,
  36020. 0x1c, 0x7e, 0xf5, 0x0b, 0x46, 0x7e, 0x92, 0x4e, 0xb9, 0xa8,
  36021. 0x36, 0xb8, 0x81, 0x6d, 0x8c, 0x70, 0x59, 0x33, 0x12, 0x61,
  36022. 0xbb, 0xe6, 0x10, 0x8a, 0xe4, 0xc1, 0x2c, 0x50, 0x12, 0xbf,
  36023. 0xd3, 0xc6, 0x3c, 0x53, 0x91, 0x50, 0x07, 0xc8, 0x85, 0x32,
  36024. 0x3c, 0xe1, 0x67, 0x99, 0x68, 0xc1, 0xf4, 0x74, 0x86, 0x35,
  36025. 0x8a, 0x6c, 0x75, 0x1d, 0x8f, 0x8a, 0x60, 0xe1, 0xc7, 0x59,
  36026. 0x4e, 0xb0, 0xe0, 0x45, 0x5a, 0x11, 0x05, 0x24, 0xa7, 0x8d,
  36027. 0x39, 0x93, 0x60, 0x4c, 0xc5, 0x9e, 0x8a, 0x70, 0xcc, 0x44,
  36028. 0x96, 0x92, 0xc8, 0xf7, 0x23, 0x14, 0xc7, 0xf4, 0x82, 0x9d,
  36029. 0x5b, 0x1c, 0x26, 0xd0, 0x3c, 0x76, 0x36, 0xe9, 0x98, 0x8a,
  36030. 0xbb, 0xe6, 0xa0, 0xad, 0xed, 0xf7, 0xd9, 0x06, 0x50, 0x67,
  36031. 0x79, 0x50, 0x4e, 0xd5, 0x80, 0x4e, 0x59, 0x72, 0x5d, 0x8b,
  36032. 0xcb, 0x86, 0x3b, 0x57, 0xc4, 0xb2, 0x3d, 0xbc, 0x35, 0x6d,
  36033. 0xb1, 0x50, 0xf5, 0x8c, 0xf2, 0x89, 0x72, 0x20, 0xd0, 0x47,
  36034. 0x68, 0x13, 0x42, 0x25, 0x1a, 0xb6, 0xc5, 0x07, 0xdf, 0x45,
  36035. 0x11, 0xa9, 0x05, 0x5d, 0xad, 0xf0, 0x49, 0x9e, 0x70, 0x78,
  36036. 0xed, 0xe7, 0xf9, 0x00, 0x1f, 0x62, 0x76, 0x47, 0xb5, 0x48,
  36037. 0x4f, 0x2c, 0x2e, 0xe3, 0x78, 0x6a, 0x44, 0x46, 0x1e, 0x6b,
  36038. 0x00, 0x74, 0x54, 0xb9, 0xd1, 0x4f, 0x6d, 0x45, 0xc1, 0xa6,
  36039. 0x45, 0x2e, 0x1a, 0xaf, 0x94, 0x3f, 0xd0, 0x72, 0x67, 0x0d,
  36040. 0x2e, 0xa9, 0x8d, 0x16, 0xc4, 0x05, 0x01, 0x07, 0x13, 0x1b,
  36041. 0x1c, 0x3d, 0x43, 0x71, 0x91, 0x95, 0x9a, 0xae, 0xaf, 0xc4,
  36042. 0xe5, 0xe6, 0xe9, 0xff, 0x02, 0x0c, 0x0f, 0x3e, 0x62, 0x67,
  36043. 0x68, 0x81, 0xc7, 0xd0, 0xd8, 0xdd, 0xe0, 0xf5, 0x0b, 0x25,
  36044. 0x35, 0x45, 0x4a, 0x4b, 0x63, 0x74, 0x79, 0x7e, 0x82, 0xa2,
  36045. 0xaf, 0xc6, 0xc7, 0xcc, 0xd2, 0xfa, 0x2a, 0x2d, 0x2f, 0x32,
  36046. 0x35, 0x38, 0x3f, 0x4c, 0x7f, 0x80, 0x81, 0x8b, 0x9b, 0x9c,
  36047. 0x9d, 0xa7, 0xa9, 0xcb, 0xe9, 0xf0, 0x00, 0x00, 0x00, 0x00,
  36048. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x20, 0x32, 0x46,
  36049. };
  36050. return dilithium_param_vfy_test(WC_ML_DSA_44, ml_dsa_44_pub_key,
  36051. (word32)sizeof(ml_dsa_44_pub_key), ml_dsa_44_sig,
  36052. (word32)sizeof(ml_dsa_44_sig));
  36053. }
  36054. #endif
  36055. #ifndef WOLFSSL_NO_ML_DSA_65
  36056. static wc_test_ret_t dilithium_param_65_vfy_test(void)
  36057. {
  36058. WOLFSSL_SMALL_STACK_STATIC const byte ml_dsa_65_pub_key[] = {
  36059. 0x15, 0xc9, 0xe5, 0x53, 0x2f, 0xd8, 0x1f, 0xb4, 0xa3, 0x9f,
  36060. 0xae, 0xad, 0xb3, 0x10, 0xd0, 0x72, 0x69, 0xd3, 0x02, 0xf3,
  36061. 0xdf, 0x67, 0x5a, 0x31, 0x52, 0x19, 0xca, 0x39, 0x27, 0x77,
  36062. 0x61, 0x6d, 0x0f, 0xc1, 0x33, 0x26, 0x09, 0xf0, 0xf9, 0x4d,
  36063. 0x12, 0x7a, 0xef, 0xf7, 0x21, 0x26, 0x2c, 0xe0, 0xe2, 0x92,
  36064. 0x1f, 0x9d, 0xd1, 0xaa, 0xaf, 0x08, 0x14, 0xf2, 0xaa, 0x24,
  36065. 0x99, 0x0f, 0x20, 0x57, 0x35, 0x04, 0x32, 0x96, 0x8e, 0x6e,
  36066. 0x10, 0x64, 0xe3, 0xe3, 0x57, 0x26, 0x33, 0x32, 0x7b, 0xe4,
  36067. 0x18, 0x41, 0x77, 0xd3, 0x24, 0x63, 0x3d, 0x11, 0xea, 0xdc,
  36068. 0xbe, 0x59, 0xff, 0x8d, 0xc2, 0xe4, 0xc7, 0x04, 0xf3, 0xd4,
  36069. 0xe0, 0x1d, 0x5e, 0x09, 0x46, 0xbf, 0x02, 0x05, 0xc7, 0xa6,
  36070. 0xb7, 0x82, 0x40, 0x1f, 0x55, 0xe9, 0x77, 0x82, 0xc0, 0xcc,
  36071. 0x86, 0x99, 0x19, 0x99, 0xa2, 0xc9, 0x1b, 0x4f, 0xdd, 0x49,
  36072. 0x4c, 0x78, 0x0a, 0x58, 0xb8, 0xf0, 0x23, 0xac, 0x1a, 0x71,
  36073. 0x57, 0x6d, 0xd6, 0x3a, 0x3a, 0x6f, 0x93, 0xb3, 0x2b, 0x09,
  36074. 0xbe, 0xec, 0x7b, 0x5b, 0xf7, 0x3a, 0xed, 0xf9, 0xd0, 0xb1,
  36075. 0xfe, 0x9f, 0x9b, 0xec, 0x11, 0xb6, 0x6b, 0xd1, 0xb6, 0x00,
  36076. 0x72, 0x7f, 0x68, 0x9a, 0x61, 0xa5, 0xf5, 0x6e, 0xe9, 0x46,
  36077. 0xa4, 0x82, 0x08, 0x9f, 0x50, 0x4c, 0x75, 0xc3, 0x48, 0x85,
  36078. 0x76, 0x39, 0xea, 0x0c, 0xf2, 0xe8, 0x7e, 0x48, 0x69, 0xd9,
  36079. 0x6f, 0x9a, 0x89, 0x7d, 0x98, 0xc1, 0x16, 0xdc, 0x2f, 0xc7,
  36080. 0x0a, 0x11, 0xa8, 0xbb, 0xe7, 0x91, 0xb1, 0x0f, 0x0e, 0xf0,
  36081. 0xb4, 0xc8, 0x41, 0x7e, 0x62, 0x9e, 0x3c, 0x30, 0x4c, 0xbc,
  36082. 0x4c, 0xeb, 0x37, 0xaf, 0x48, 0x72, 0x59, 0x64, 0x8e, 0xfb,
  36083. 0x77, 0x11, 0x28, 0xdd, 0x30, 0x52, 0x8e, 0x69, 0x8c, 0x9f,
  36084. 0x3d, 0xec, 0xdf, 0xa7, 0x5f, 0x42, 0x18, 0xda, 0xba, 0x1a,
  36085. 0x96, 0x91, 0x7d, 0x62, 0xd5, 0x52, 0xff, 0x44, 0xc9, 0x1d,
  36086. 0x29, 0xa6, 0xb9, 0x03, 0x9a, 0x26, 0x26, 0xcf, 0x57, 0x40,
  36087. 0x70, 0x7e, 0x2b, 0xbd, 0xf0, 0x81, 0x71, 0x0f, 0x0b, 0x2e,
  36088. 0x9b, 0x03, 0xba, 0x31, 0x41, 0x68, 0x37, 0xc8, 0xff, 0xea,
  36089. 0xc4, 0x73, 0xa5, 0xf9, 0xc2, 0x92, 0x78, 0x0c, 0xe7, 0xfd,
  36090. 0x5d, 0xb2, 0x01, 0xb5, 0x8d, 0xeb, 0x64, 0xd4, 0x14, 0xea,
  36091. 0x7a, 0xd1, 0x42, 0xc8, 0x99, 0xe4, 0x7d, 0x5b, 0x7e, 0x3b,
  36092. 0x8f, 0xab, 0x82, 0x12, 0xdf, 0xbb, 0xa1, 0x45, 0x30, 0xc9,
  36093. 0x0f, 0xb9, 0xe5, 0xba, 0xe6, 0x8a, 0xf3, 0x78, 0x61, 0xcc,
  36094. 0x9f, 0xe1, 0x46, 0x2a, 0x9a, 0x18, 0x0e, 0x2a, 0x57, 0xf3,
  36095. 0xe5, 0x56, 0xd1, 0x42, 0x48, 0xe1, 0x5a, 0x8e, 0x33, 0xce,
  36096. 0x19, 0xe5, 0x3e, 0x7f, 0x00, 0x70, 0x9c, 0x4c, 0xd3, 0xe1,
  36097. 0x0c, 0xa1, 0x7e, 0xd4, 0xa9, 0x9e, 0x8b, 0xe2, 0xf0, 0xac,
  36098. 0xdb, 0xa6, 0x72, 0x75, 0x67, 0xa6, 0x57, 0xed, 0x79, 0x2e,
  36099. 0xca, 0x8d, 0xeb, 0x9b, 0x9e, 0xb7, 0xbf, 0x30, 0x02, 0x2b,
  36100. 0xb3, 0x43, 0x89, 0x9b, 0xa8, 0x88, 0xa5, 0xbb, 0x33, 0xd9,
  36101. 0x99, 0x30, 0x7c, 0xc7, 0xd4, 0x28, 0x5e, 0x5e, 0x3f, 0x9d,
  36102. 0x6d, 0x35, 0x75, 0x33, 0x8e, 0xff, 0x84, 0x2e, 0x2d, 0xda,
  36103. 0xf0, 0xff, 0x70, 0xe5, 0xb5, 0x62, 0x96, 0x33, 0x3a, 0xd9,
  36104. 0xb5, 0x82, 0x25, 0x81, 0x81, 0x40, 0x5d, 0x4f, 0x11, 0x86,
  36105. 0x63, 0x1a, 0x06, 0xc1, 0x67, 0xc7, 0x49, 0x03, 0xc7, 0xe4,
  36106. 0x6f, 0xb4, 0x13, 0x3e, 0x57, 0x62, 0xfd, 0x8a, 0xc6, 0x2b,
  36107. 0x65, 0x5b, 0xa4, 0x29, 0x57, 0x8d, 0xde, 0xa5, 0xee, 0x32,
  36108. 0xc2, 0x76, 0x03, 0xca, 0xce, 0xc1, 0x48, 0xec, 0x45, 0xcf,
  36109. 0x30, 0x21, 0x28, 0x7f, 0x10, 0x47, 0xd2, 0xdb, 0xee, 0xca,
  36110. 0x5b, 0x0f, 0xd5, 0x39, 0x3a, 0xc3, 0xa6, 0x78, 0xb2, 0x15,
  36111. 0xaf, 0x82, 0x3c, 0x2f, 0xc4, 0x51, 0x5c, 0x52, 0xad, 0xf2,
  36112. 0x89, 0x92, 0x8e, 0xf3, 0x50, 0x38, 0xed, 0xf8, 0xc9, 0x14,
  36113. 0x4c, 0xe4, 0xa3, 0x9a, 0xaf, 0xc4, 0x5c, 0xf3, 0x9f, 0xc3,
  36114. 0xa3, 0xc0, 0xbe, 0x45, 0x1b, 0x21, 0x63, 0xfa, 0xe0, 0xe0,
  36115. 0x91, 0x2b, 0x42, 0xca, 0x91, 0xfb, 0x5e, 0x97, 0x9a, 0x0a,
  36116. 0xd4, 0x88, 0xba, 0xb8, 0x22, 0xc6, 0xbf, 0x56, 0x58, 0x1e,
  36117. 0x92, 0xa9, 0x9d, 0xa7, 0xed, 0xc9, 0xab, 0x54, 0x4f, 0x75,
  36118. 0x8d, 0x42, 0xc1, 0xe1, 0x61, 0xd0, 0x91, 0x9a, 0x3a, 0x40,
  36119. 0x9a, 0xa3, 0xfb, 0x7b, 0x4e, 0xf0, 0x85, 0xf0, 0xdc, 0x40,
  36120. 0x72, 0x9f, 0x05, 0xa8, 0xbe, 0x95, 0x5a, 0x7f, 0xba, 0x75,
  36121. 0x00, 0x6e, 0x95, 0x76, 0xbd, 0xb2, 0x40, 0xf5, 0xb0, 0x64,
  36122. 0x0a, 0x2f, 0x06, 0x3d, 0x9f, 0xac, 0x6a, 0xa5, 0x46, 0x5a,
  36123. 0x85, 0xa4, 0x6f, 0xee, 0x27, 0xa0, 0xeb, 0x5f, 0x1f, 0x91,
  36124. 0xbd, 0x2b, 0x02, 0x16, 0xdf, 0x74, 0x97, 0x2c, 0xd0, 0xa8,
  36125. 0x9f, 0x3a, 0x7b, 0xdf, 0x3e, 0x98, 0x4a, 0x91, 0xdc, 0x19,
  36126. 0x96, 0x88, 0x75, 0x21, 0x1a, 0x6a, 0xa8, 0x4b, 0x1f, 0x35,
  36127. 0xd1, 0x92, 0xf5, 0x76, 0xf4, 0x72, 0x55, 0x13, 0xdb, 0x5d,
  36128. 0x07, 0x8d, 0xd9, 0x72, 0xe4, 0x75, 0xde, 0x80, 0xbc, 0xe9,
  36129. 0x9c, 0xf0, 0x5c, 0x6a, 0x8a, 0x0e, 0x34, 0xf6, 0x3f, 0x5c,
  36130. 0xef, 0x0e, 0xcc, 0x52, 0x38, 0x2d, 0x7b, 0xc2, 0x1b, 0x69,
  36131. 0x9f, 0xe5, 0xed, 0x14, 0xb0, 0x91, 0x0b, 0xe9, 0x4d, 0x34,
  36132. 0xd5, 0xaa, 0xd4, 0xd2, 0x46, 0x39, 0x45, 0x7e, 0x85, 0x2f,
  36133. 0xdb, 0x89, 0xf4, 0xff, 0x05, 0x74, 0x51, 0xba, 0xdd, 0xee,
  36134. 0xf6, 0xc2, 0xc1, 0x0a, 0x8f, 0xd9, 0xeb, 0xc7, 0x61, 0x30,
  36135. 0x8f, 0x86, 0x8b, 0x1f, 0x82, 0xc1, 0x22, 0xfd, 0x83, 0xf4,
  36136. 0x5d, 0xc5, 0x94, 0xf5, 0xd7, 0x17, 0xc7, 0x7b, 0x71, 0xf5,
  36137. 0x5e, 0x15, 0x49, 0x70, 0xb2, 0x57, 0xa0, 0xc0, 0x57, 0x63,
  36138. 0x53, 0x35, 0xb6, 0x52, 0x20, 0x7b, 0x83, 0xd4, 0x57, 0x63,
  36139. 0x25, 0x8e, 0x83, 0xb3, 0x8e, 0x26, 0x1f, 0x09, 0xde, 0x14,
  36140. 0xd6, 0xa6, 0xfc, 0xe5, 0x93, 0x3c, 0x88, 0x8e, 0xf5, 0x10,
  36141. 0x57, 0xb9, 0xc9, 0x9b, 0xff, 0x72, 0x9d, 0x3d, 0x3f, 0x97,
  36142. 0xd9, 0x3c, 0x20, 0xe2, 0x57, 0xfd, 0x2a, 0x5c, 0x17, 0x12,
  36143. 0xe6, 0x08, 0xaf, 0xe4, 0x26, 0x96, 0xb9, 0x6d, 0xc3, 0xac,
  36144. 0x22, 0xf3, 0x8b, 0x89, 0xde, 0xc7, 0x8a, 0x93, 0x06, 0xf7,
  36145. 0x1d, 0x08, 0x21, 0x36, 0x16, 0x74, 0x2b, 0x97, 0x23, 0xe4,
  36146. 0x79, 0x31, 0x08, 0x23, 0x62, 0x30, 0x67, 0xe2, 0xed, 0x30,
  36147. 0x9b, 0x0c, 0xf9, 0x08, 0x7a, 0x29, 0x73, 0xc6, 0x77, 0x8a,
  36148. 0xbb, 0x2a, 0x1c, 0x66, 0xd0, 0xdd, 0x9e, 0xa3, 0xe9, 0x62,
  36149. 0xcc, 0xb7, 0x88, 0x25, 0x4a, 0x5f, 0xbc, 0xaa, 0xe3, 0xe4,
  36150. 0x4f, 0xec, 0xa6, 0x8e, 0xa6, 0xa4, 0x1b, 0x22, 0x2b, 0x2c,
  36151. 0x8f, 0x57, 0x7f, 0xb7, 0x33, 0xfe, 0x16, 0x43, 0x85, 0xc5,
  36152. 0xd2, 0x95, 0xe6, 0xb9, 0x21, 0x68, 0x88, 0x98, 0x33, 0x8c,
  36153. 0x1d, 0x15, 0x9c, 0x4d, 0x62, 0x1f, 0x6b, 0xe8, 0x7a, 0x2d,
  36154. 0x6b, 0x0e, 0xc3, 0xde, 0x1a, 0xa8, 0xed, 0x67, 0xb3, 0xb3,
  36155. 0x36, 0x5b, 0x4b, 0xcb, 0xe8, 0xa8, 0x5c, 0x0b, 0x2f, 0xca,
  36156. 0xd7, 0x71, 0xe8, 0x85, 0xe7, 0x4d, 0xe5, 0x7b, 0x45, 0xed,
  36157. 0xb2, 0x4c, 0x69, 0x04, 0x7e, 0x4f, 0xc0, 0xef, 0x1a, 0xca,
  36158. 0x0d, 0xa6, 0xc4, 0x79, 0x15, 0x78, 0x9c, 0xd2, 0x91, 0x3c,
  36159. 0x32, 0x55, 0x40, 0xe7, 0xcb, 0x7e, 0xde, 0x07, 0xa6, 0x97,
  36160. 0x00, 0x2d, 0x70, 0xf6, 0x3d, 0x15, 0xdf, 0x29, 0x8e, 0xa3,
  36161. 0x96, 0x6d, 0xf2, 0xbb, 0xa5, 0x1b, 0x7b, 0x58, 0x30, 0xf6,
  36162. 0x17, 0xbd, 0xda, 0x13, 0xf7, 0x33, 0xc2, 0x62, 0x32, 0xd4,
  36163. 0x1c, 0x2e, 0x31, 0x74, 0x92, 0xad, 0x99, 0x8c, 0x0e, 0x7c,
  36164. 0x50, 0x21, 0xcd, 0xff, 0x41, 0xeb, 0xd1, 0xca, 0x14, 0xb7,
  36165. 0xb2, 0x31, 0x2f, 0xbe, 0x16, 0xce, 0x4f, 0x26, 0x16, 0x04,
  36166. 0xc2, 0xaf, 0xbe, 0x0d, 0x24, 0xab, 0x9a, 0x21, 0x37, 0x06,
  36167. 0xac, 0x50, 0x23, 0xf1, 0xbe, 0x5c, 0xbb, 0x64, 0xf3, 0xd3,
  36168. 0x66, 0xa3, 0xb8, 0xbe, 0x8b, 0x49, 0x8d, 0xf6, 0xc7, 0xb9,
  36169. 0x8f, 0x4e, 0x31, 0x06, 0x51, 0xe5, 0xf3, 0x0e, 0x56, 0xc4,
  36170. 0x24, 0x30, 0xf5, 0xe9, 0x36, 0x71, 0xbc, 0xc9, 0x70, 0x2c,
  36171. 0x6c, 0x4c, 0x15, 0x43, 0x44, 0xa4, 0xfc, 0xf1, 0xd2, 0x71,
  36172. 0x6c, 0x4c, 0xce, 0x30, 0x6c, 0x05, 0x7d, 0x2e, 0xb7, 0xbc,
  36173. 0xe4, 0x65, 0x76, 0x24, 0x75, 0x36, 0xdf, 0x28, 0xfc, 0xcd,
  36174. 0x9a, 0xba, 0xc2, 0xcd, 0xb0, 0x30, 0xdb, 0xe7, 0x2e, 0x3c,
  36175. 0x92, 0x63, 0x1d, 0x30, 0x23, 0x74, 0xb1, 0xb8, 0xcc, 0xd7,
  36176. 0xb6, 0x90, 0x65, 0x73, 0xa2, 0x2a, 0x6e, 0x49, 0x95, 0x0d,
  36177. 0xab, 0x24, 0xdf, 0x2d, 0xbf, 0x76, 0x46, 0x01, 0x44, 0xe4,
  36178. 0x18, 0x8e, 0xd5, 0x9a, 0x76, 0xc9, 0xc6, 0xbc, 0xdb, 0x7f,
  36179. 0x80, 0x52, 0xc6, 0x40, 0x41, 0x12, 0x36, 0x7c, 0x80, 0x69,
  36180. 0xce, 0x7b, 0xe1, 0xa0, 0x53, 0xa2, 0xd6, 0x8f, 0x3f, 0xf7,
  36181. 0xd7, 0x61, 0x09, 0x70, 0xa2, 0xa0, 0xc6, 0xaf, 0xa0, 0xd0,
  36182. 0xfa, 0x13, 0xbf, 0xc0, 0x69, 0x15, 0xce, 0x15, 0xec, 0x24,
  36183. 0x4b, 0x6b, 0xdc, 0x93, 0x51, 0xc6, 0x82, 0x19, 0x92, 0x84,
  36184. 0x5d, 0x99, 0xb0, 0x90, 0x2c, 0xcc, 0x2a, 0x81, 0x6b, 0x22,
  36185. 0x64, 0x0a, 0xcb, 0x51, 0x25, 0x82, 0x50, 0x02, 0x2d, 0x3e,
  36186. 0xd4, 0x72, 0xb3, 0x0c, 0x15, 0x77, 0xd2, 0xca, 0x98, 0x2f,
  36187. 0x41, 0x93, 0x14, 0xb2, 0x7f, 0xa1, 0x97, 0xa3, 0xb8, 0x8a,
  36188. 0x56, 0x24, 0x38, 0xa7, 0x36, 0xc5, 0x01, 0xc0, 0x9f, 0x3f,
  36189. 0x3e, 0x9a, 0xf6, 0xe9, 0x16, 0x82, 0x01, 0x58, 0x70, 0x0e,
  36190. 0x0d, 0xbc, 0xfa, 0x03, 0x57, 0x65, 0xa8, 0x5a, 0x3d, 0x57,
  36191. 0x81, 0x23, 0xbe, 0x6e, 0xa9, 0xe8, 0x22, 0xdf, 0x2f, 0x70,
  36192. 0xeb, 0x0a, 0x03, 0x96, 0x6b, 0xef, 0x20, 0x9f, 0xf2, 0x62,
  36193. 0xe7, 0xb2, 0x6e, 0x3a, 0x1e, 0x40, 0x1f, 0xd2, 0x97, 0x48,
  36194. 0xd1, 0x18, 0xf0, 0xeb, 0x52, 0x58, 0x02, 0x26, 0xce, 0x75,
  36195. 0xb1, 0x3a, 0x9d, 0x5b, 0x52, 0x94, 0xb2, 0x6e, 0x0e, 0x3f,
  36196. 0x39, 0xb6, 0xd9, 0x8a, 0x9d, 0xe8, 0x7c, 0x83, 0x32, 0xcc,
  36197. 0x43, 0x35, 0x9b, 0x7a, 0xed, 0xb2, 0x1e, 0x51, 0x37, 0x6c,
  36198. 0x14, 0xd8, 0xb8, 0x55, 0xb3, 0x91, 0xef, 0x0c, 0x3a, 0xe5,
  36199. 0x77, 0xd0, 0xbd, 0xb0, 0x7d, 0x38, 0x84, 0x2a, 0x47, 0xb2,
  36200. 0xb6, 0xda, 0xd7, 0x75, 0xd6, 0x2e, 0x60, 0xc7, 0x10, 0x52,
  36201. 0xf7, 0xdd, 0x09, 0x15, 0x6f, 0x04, 0x31, 0xc3, 0x5a, 0x6b,
  36202. 0x0c, 0x60, 0x10, 0xa8, 0x6e, 0x20, 0xa9, 0xdd, 0xb7, 0x72,
  36203. 0xc3, 0x9e, 0x85, 0xd2, 0x8f, 0x16, 0x7e, 0x3d, 0xe0, 0x63,
  36204. 0x81, 0x32, 0xfd, 0xca, 0xbc, 0x0f, 0xef, 0x3e, 0x74, 0x6a,
  36205. 0xb1, 0x60, 0xc1, 0x10, 0x50, 0x7c, 0x67, 0xa4, 0x19, 0xa7,
  36206. 0xb8, 0xed, 0xe6, 0xf5, 0x4e, 0x41, 0x53, 0xa6, 0x72, 0x1b,
  36207. 0x2c, 0x33, 0x6a, 0x37, 0xf1, 0xb5, 0x1c, 0x01, 0x7d, 0xa2,
  36208. 0x1f, 0x2c, 0x4e, 0x0a, 0xbf, 0xd4, 0x2c, 0x24, 0x91, 0x58,
  36209. 0x62, 0xfb, 0xf8, 0x63, 0xd9, 0xf8, 0x78, 0xf5, 0xc7, 0x78,
  36210. 0x32, 0xda, 0x99, 0xeb, 0x58, 0x20, 0x25, 0x19, 0xb1, 0x06,
  36211. 0x7f, 0x6a, 0x29, 0x20, 0xdb, 0xc8, 0x22, 0x48, 0xa9, 0x7f,
  36212. 0x24, 0x54, 0x8d, 0x7d, 0x8d, 0xb1, 0x69, 0xb2, 0xa3, 0x98,
  36213. 0x14, 0x0f, 0xba, 0xfa, 0xb6, 0x15, 0xe8, 0x28, 0x99, 0x3f,
  36214. 0x30, 0x04, 0x50, 0xab, 0x5a, 0x3c, 0xf1, 0x97, 0xe1, 0xc8,
  36215. 0x0f, 0x0e, 0xb4, 0x11, 0x63, 0x5a, 0x79, 0x08, 0x48, 0x75,
  36216. 0xaf, 0x9b, 0xca, 0xd9, 0x13, 0x18, 0xcc, 0xb1, 0xb3, 0xee,
  36217. 0xdd, 0x63, 0xdd, 0xf4, 0x21, 0x98, 0x76, 0xe2, 0x3e, 0xd5,
  36218. 0x86, 0x23, 0x33, 0x7e, 0xc7, 0xb4, 0x35, 0x4b, 0xc2, 0x2d,
  36219. 0xe1, 0xe2, 0xb0, 0x6c, 0x8b, 0x9b, 0x20, 0x3d, 0x48, 0x24,
  36220. 0x7c, 0xea, 0xa1, 0x75, 0x27, 0xe5, 0xf4, 0x70, 0xeb, 0x3b,
  36221. 0xc7, 0x26, 0x37, 0x04, 0xff, 0x8a, 0x7a, 0xd0, 0xc2, 0xb7,
  36222. 0x84, 0xb7, 0x29, 0xfb, 0x0e, 0xa3, 0xa8, 0x71, 0xcd, 0x58,
  36223. 0x06, 0x36, 0xe2, 0xf2, 0x77, 0xcc, 0x0f, 0x78, 0x08, 0x2b,
  36224. 0xbb, 0xe3, 0x53, 0x05, 0x71, 0xdc, 0x6c, 0x37, 0x32, 0x91,
  36225. 0x46, 0x42, 0x4f, 0x21, 0xe0, 0x34, 0xad, 0x3f, 0x30, 0x5a,
  36226. 0xc7, 0x0d, 0x17, 0x19, 0x39, 0x31, 0x58, 0x69, 0x3c, 0x8c,
  36227. 0xbe, 0xe7, 0xa6, 0x3b, 0xad, 0xfb, 0x46, 0x89, 0x06, 0xc1,
  36228. 0x8c, 0x16, 0x9a, 0x06, 0x3a, 0xd0, 0x7e, 0xd6, 0xb0, 0x7b,
  36229. 0x7d, 0xf8, 0x91, 0x7c, 0xfa, 0xd9, 0x66, 0x39, 0xfa, 0xbc,
  36230. 0x57, 0xa7, 0x78, 0x8b, 0x36, 0x78, 0xc0, 0x1c, 0x0e, 0x23,
  36231. 0x05, 0x0e, 0x04, 0x61, 0x16, 0x34, 0xf9, 0xc6, 0x63, 0x58,
  36232. 0xdf, 0xf4, 0x52, 0xce, 0xd0, 0x0f, 0x0c, 0xec, 0xb1, 0x82,
  36233. 0xf4, 0x72, 0x73, 0x72, 0x3f, 0x02, 0xbe, 0xe3, 0x9c, 0x63,
  36234. 0x73, 0xc8, 0x21, 0x65, 0xba, 0x57, 0x52, 0xa9, 0x19, 0xac,
  36235. 0x68, 0x50, 0xbd, 0x2d, 0x72, 0x5b, 0x93, 0x0f, 0x1c, 0x81,
  36236. 0x77, 0xd7, 0x2e, 0xc3, 0x93, 0x52, 0x6e, 0xdc, 0x79, 0x52,
  36237. 0x9f, 0xe3, 0xde, 0xe1, 0xba, 0x58, 0x55, 0xab, 0x8a, 0xf2,
  36238. 0x35, 0x6a, 0xcf, 0x94, 0x1f, 0x17, 0xa4, 0x23, 0x2e, 0x8e,
  36239. 0x18, 0x21, 0xbe, 0x14, 0xfa, 0xe7, 0x59, 0xc5, 0x44, 0x34,
  36240. 0xce, 0x03, 0xf4, 0xb7, 0x75, 0xd3, 0x51, 0x55, 0xdf, 0xff,
  36241. 0xcf, 0x4f, 0x44, 0xee, 0x13, 0x9b, 0xcb, 0x12, 0xae, 0xe5,
  36242. 0x5b, 0x44, 0x65, 0x28, 0xcb, 0x6a, 0x9c, 0x24, 0x1d, 0xea,
  36243. 0x2d, 0x5e, 0xa5, 0xc3, 0x78, 0xad, 0xed, 0x0c, 0x05, 0xa6,
  36244. 0xaf, 0x95, 0x04, 0xd2, 0xb5, 0x91, 0x0e, 0xa0, 0x06, 0x77,
  36245. 0xc5, 0x82, 0xf6, 0xdd, 0x72, 0x83, 0x04, 0xcc, 0xb0, 0xab,
  36246. 0x7a, 0xf0, 0xb4, 0x4d, 0x36, 0x71, 0x72, 0x1a, 0x9a, 0x0d,
  36247. 0xcd, 0xa3, 0x11, 0xa8, 0x0d, 0x7d, 0x49, 0xce, 0x9c, 0x09,
  36248. 0x1d, 0x08, 0xa4, 0x39, 0x2e, 0x03, 0xdf, 0x3a, 0xc8, 0xfe,
  36249. 0x6a, 0x2b, 0x0b, 0x07, 0x80, 0x55, 0x8a, 0xa8, 0xe6, 0x0e,
  36250. 0xc9, 0x7e, 0x83, 0xce, 0x3a, 0x98, 0x98, 0x4e, 0x3e, 0x08,
  36251. 0x20, 0x8f, 0x10, 0xfc, 0xc1, 0xc4, 0xcf, 0x37, 0x8d, 0x69,
  36252. 0xd8, 0x57, 0x9d, 0x48, 0x80, 0x6a, 0xef, 0x0c, 0xdd, 0x27,
  36253. 0x99, 0xf9, 0xe7, 0xd0, 0xd2, 0x36, 0xd8, 0xed, 0x41, 0x14,
  36254. 0x1b, 0x10,
  36255. };
  36256. WOLFSSL_SMALL_STACK_STATIC const byte ml_dsa_65_sig[] = {
  36257. 0x3e, 0xff, 0xf4, 0x48, 0x80, 0x2d, 0x88, 0x87, 0xf4, 0xcc,
  36258. 0xa4, 0x61, 0xe1, 0x27, 0x20, 0x55, 0x66, 0xc8, 0xfe, 0x3e,
  36259. 0xdd, 0xf5, 0x5c, 0x70, 0x6c, 0x54, 0xba, 0x50, 0x8a, 0xa2,
  36260. 0x4b, 0x88, 0xbc, 0xb8, 0x87, 0xf9, 0x4e, 0x50, 0x3a, 0x04,
  36261. 0x18, 0xb3, 0xf4, 0x5f, 0x77, 0x4a, 0x7e, 0xa8, 0xf5, 0xca,
  36262. 0x49, 0x00, 0xdc, 0x24, 0xaa, 0x05, 0x35, 0x0f, 0x34, 0xf7,
  36263. 0xbf, 0x09, 0xa6, 0xcf, 0x75, 0x37, 0x07, 0xcd, 0x07, 0x99,
  36264. 0x92, 0x1d, 0xc7, 0xc9, 0x17, 0x1c, 0xdd, 0x27, 0x8c, 0x66,
  36265. 0xf2, 0x8b, 0x75, 0xb0, 0x86, 0x2d, 0xbd, 0x51, 0x16, 0xc2,
  36266. 0x50, 0xe0, 0x7e, 0x0a, 0x21, 0x58, 0x93, 0x22, 0x06, 0xcb,
  36267. 0x85, 0x8b, 0xfd, 0x97, 0x61, 0xc0, 0xdb, 0xab, 0xfa, 0x4a,
  36268. 0x69, 0xef, 0x9c, 0xc1, 0x4e, 0xae, 0xb2, 0xb3, 0xa2, 0x74,
  36269. 0xa4, 0x94, 0x0a, 0xed, 0x39, 0x9e, 0xe8, 0x58, 0xeb, 0xfd,
  36270. 0x43, 0x05, 0x73, 0x38, 0xd6, 0xbb, 0xeb, 0xb9, 0x9d, 0x3b,
  36271. 0xf8, 0x85, 0xb4, 0x4b, 0x16, 0x5c, 0x9e, 0xfe, 0xb8, 0x13,
  36272. 0xf8, 0x68, 0x44, 0x90, 0x05, 0x61, 0xb3, 0xed, 0x6f, 0x47,
  36273. 0xc9, 0x50, 0xcf, 0x6c, 0xc0, 0xac, 0xdf, 0x4c, 0x4c, 0x1b,
  36274. 0x42, 0xce, 0x0a, 0x32, 0x69, 0xb0, 0xfd, 0x87, 0xef, 0xf3,
  36275. 0x9c, 0xcc, 0xba, 0x2f, 0x03, 0xd7, 0xdb, 0x76, 0xee, 0xa0,
  36276. 0x71, 0x4a, 0x80, 0xcb, 0x90, 0x9e, 0xbb, 0x8f, 0x00, 0x46,
  36277. 0x81, 0xe0, 0xde, 0xa6, 0x43, 0xb5, 0x37, 0x79, 0xf2, 0x35,
  36278. 0xce, 0x9e, 0xd2, 0xb1, 0x5b, 0xff, 0x91, 0xfb, 0x98, 0xc1,
  36279. 0xe1, 0x66, 0x2c, 0x00, 0x1b, 0x89, 0xf2, 0x57, 0x81, 0x73,
  36280. 0x7e, 0x9f, 0x8d, 0x50, 0xd0, 0xe0, 0xe3, 0x93, 0xf2, 0x87,
  36281. 0x41, 0x64, 0x6c, 0xb7, 0x09, 0x60, 0x91, 0x4e, 0x0b, 0xbe,
  36282. 0xbe, 0xd4, 0x98, 0xfa, 0x14, 0x8c, 0x46, 0x09, 0xfa, 0xaa,
  36283. 0x82, 0xd6, 0xdd, 0x65, 0x93, 0x39, 0x45, 0x50, 0x90, 0x10,
  36284. 0xae, 0x1b, 0xff, 0xab, 0x7e, 0x86, 0xda, 0xb9, 0x4d, 0xf1,
  36285. 0xc2, 0x00, 0x54, 0x66, 0xee, 0x40, 0xc0, 0x56, 0x2f, 0xe8,
  36286. 0x43, 0x89, 0xbb, 0xb8, 0x59, 0x24, 0x63, 0x45, 0x9a, 0xde,
  36287. 0x08, 0xf3, 0x16, 0x94, 0xd2, 0x8d, 0xee, 0xf9, 0xbe, 0x4f,
  36288. 0x29, 0xe1, 0x4b, 0x5e, 0x2b, 0x14, 0xef, 0x66, 0xe2, 0x12,
  36289. 0xf8, 0x87, 0x2e, 0xb1, 0x75, 0x8b, 0x21, 0xb5, 0x8f, 0x8e,
  36290. 0xc5, 0x0e, 0x60, 0x27, 0x15, 0xbd, 0x72, 0xe4, 0x26, 0x4e,
  36291. 0x62, 0x7d, 0x3a, 0x46, 0x49, 0x93, 0xa9, 0x52, 0x7f, 0xc2,
  36292. 0x27, 0xb9, 0x55, 0x6a, 0x45, 0x9f, 0x2c, 0x7a, 0x5a, 0xc9,
  36293. 0xf4, 0x55, 0xaf, 0x49, 0xb3, 0xd5, 0xc0, 0x84, 0xdb, 0x89,
  36294. 0x5f, 0x21, 0x04, 0xf5, 0x4c, 0x66, 0x1e, 0x2e, 0x69, 0xdf,
  36295. 0x5b, 0x14, 0x60, 0x89, 0x84, 0xf8, 0xa3, 0xaf, 0xdf, 0xb9,
  36296. 0x18, 0x5e, 0xbf, 0x81, 0x95, 0x9a, 0x5e, 0x4f, 0x24, 0x45,
  36297. 0xad, 0xab, 0xe2, 0x36, 0x7c, 0x19, 0xde, 0xc0, 0xf4, 0x1a,
  36298. 0x42, 0xb2, 0xc2, 0x58, 0x2f, 0x5f, 0xd0, 0x2e, 0x28, 0x33,
  36299. 0x59, 0x75, 0xc2, 0xde, 0x41, 0xe3, 0x9b, 0x85, 0x46, 0xad,
  36300. 0x6d, 0xf1, 0x06, 0xf0, 0x6a, 0xb9, 0xed, 0x71, 0x7b, 0xfd,
  36301. 0xf1, 0xc4, 0x56, 0xd8, 0xb3, 0x1a, 0x5f, 0x04, 0xae, 0xe8,
  36302. 0xce, 0xde, 0xa1, 0x6d, 0x46, 0x2a, 0x4f, 0x62, 0xee, 0x25,
  36303. 0xdf, 0x22, 0x21, 0xb2, 0x8f, 0x5f, 0x26, 0x33, 0x5a, 0xdd,
  36304. 0xbe, 0x08, 0xb3, 0x93, 0x16, 0x16, 0xad, 0x2e, 0x00, 0xb8,
  36305. 0x14, 0x0c, 0x10, 0xa3, 0x29, 0x89, 0x1f, 0xd7, 0x06, 0x7a,
  36306. 0x09, 0xf3, 0x84, 0xf9, 0x18, 0x04, 0x56, 0x2f, 0x7f, 0xbd,
  36307. 0x8e, 0x12, 0xdf, 0x4d, 0x58, 0x5c, 0x1d, 0x81, 0x0c, 0x7d,
  36308. 0x62, 0x02, 0xe0, 0xf9, 0x1b, 0x69, 0xe9, 0x38, 0x45, 0x84,
  36309. 0x2d, 0x9a, 0x4a, 0x3d, 0x7b, 0x48, 0xd5, 0x0d, 0x76, 0xba,
  36310. 0xff, 0x20, 0x00, 0xf8, 0x42, 0x7f, 0xd2, 0x25, 0x70, 0x90,
  36311. 0x88, 0xb3, 0x98, 0xac, 0xe9, 0xd9, 0xac, 0x58, 0xa6, 0x49,
  36312. 0xcc, 0x93, 0xa5, 0x04, 0x0c, 0x68, 0x53, 0x64, 0x72, 0x8c,
  36313. 0xfc, 0x8d, 0x61, 0xeb, 0x3f, 0x93, 0x8b, 0x85, 0x98, 0x05,
  36314. 0xce, 0x06, 0xd7, 0xbf, 0xbb, 0xa5, 0x22, 0xda, 0xe9, 0x8a,
  36315. 0x29, 0x30, 0x5e, 0x82, 0xe4, 0x46, 0x7c, 0x36, 0x5e, 0xf5,
  36316. 0xc7, 0xe3, 0x09, 0xdf, 0x20, 0x76, 0x73, 0x33, 0x31, 0x75,
  36317. 0xc2, 0x99, 0xe9, 0x74, 0x43, 0x82, 0xb1, 0xeb, 0x74, 0x6f,
  36318. 0xad, 0x59, 0x48, 0x12, 0xa0, 0x24, 0xe3, 0x38, 0x48, 0x61,
  36319. 0x0c, 0xf6, 0x38, 0x83, 0x3a, 0xcd, 0xd6, 0x45, 0x10, 0x0e,
  36320. 0x09, 0x79, 0x31, 0x30, 0x80, 0xfb, 0x34, 0x60, 0x1e, 0x72,
  36321. 0x98, 0xe9, 0x5c, 0xbf, 0xab, 0x21, 0x7f, 0xa3, 0x19, 0x7e,
  36322. 0x8c, 0xa9, 0xa7, 0xfc, 0x25, 0xe0, 0x8e, 0x6d, 0xa1, 0xb9,
  36323. 0x7b, 0x5b, 0x37, 0x33, 0x96, 0xd8, 0x6e, 0x7a, 0xce, 0xa6,
  36324. 0x1a, 0xbd, 0xe6, 0x6e, 0x62, 0xc4, 0x8c, 0x69, 0xfe, 0xe4,
  36325. 0xcb, 0x0a, 0xa1, 0x6c, 0x66, 0x0e, 0x1a, 0x5e, 0xb9, 0xd1,
  36326. 0x4a, 0xa3, 0x91, 0x39, 0xcf, 0x85, 0x07, 0x5b, 0xaf, 0x99,
  36327. 0x11, 0xca, 0xee, 0x6f, 0x2e, 0x33, 0xda, 0x60, 0xbf, 0xd6,
  36328. 0xa0, 0x7a, 0xdb, 0x91, 0x13, 0xb7, 0xa3, 0x5d, 0x0e, 0x1e,
  36329. 0x3b, 0xf9, 0x7a, 0x3e, 0x4f, 0x8d, 0xb3, 0x81, 0xe8, 0x0c,
  36330. 0x4d, 0x48, 0x61, 0x06, 0x14, 0x0f, 0x3e, 0x33, 0x9e, 0xea,
  36331. 0xa6, 0xd8, 0xd8, 0x4d, 0x9b, 0x00, 0x34, 0x0d, 0x31, 0x62,
  36332. 0x54, 0x93, 0x04, 0xd2, 0x02, 0x21, 0x38, 0x91, 0x58, 0xca,
  36333. 0x77, 0xd3, 0x6c, 0xd1, 0x94, 0x05, 0xfa, 0x30, 0x6a, 0x0b,
  36334. 0xf0, 0x52, 0x52, 0xb7, 0xdb, 0x34, 0xff, 0x18, 0x5c, 0x78,
  36335. 0x25, 0x44, 0x39, 0xe4, 0x54, 0x8a, 0xf1, 0x49, 0x04, 0xab,
  36336. 0x8a, 0x5f, 0x87, 0xe1, 0x6e, 0x1a, 0xf2, 0xba, 0x39, 0xb4,
  36337. 0x7c, 0x71, 0x5b, 0xbe, 0x8d, 0xbb, 0xed, 0x3b, 0xed, 0x20,
  36338. 0x95, 0xdf, 0xa7, 0x50, 0xb5, 0x66, 0xff, 0xd0, 0x3a, 0x92,
  36339. 0xde, 0xf2, 0xa3, 0xf2, 0xd6, 0x48, 0x6b, 0xd8, 0xef, 0x80,
  36340. 0x4d, 0xc2, 0x3c, 0xc7, 0xc6, 0x6e, 0xdf, 0xd1, 0x54, 0xfb,
  36341. 0x22, 0xac, 0x1a, 0x11, 0x81, 0x02, 0xc7, 0x66, 0xe0, 0xf3,
  36342. 0xad, 0x0b, 0xd0, 0xec, 0xae, 0x93, 0x53, 0xa5, 0xbf, 0xa5,
  36343. 0x17, 0x59, 0x14, 0x7d, 0x7e, 0x1e, 0x26, 0x15, 0x7a, 0x74,
  36344. 0xfb, 0xb1, 0x7a, 0x0e, 0xd3, 0xb5, 0x7c, 0x8c, 0x3a, 0xd7,
  36345. 0x45, 0x38, 0x55, 0xae, 0x4b, 0xe1, 0xfe, 0x5b, 0x57, 0x20,
  36346. 0x73, 0x38, 0xb9, 0x67, 0x34, 0xb1, 0xf3, 0x15, 0xb0, 0xb7,
  36347. 0x46, 0xa7, 0x1b, 0x19, 0x6d, 0xaf, 0x5e, 0x2c, 0x9c, 0x02,
  36348. 0x3f, 0x0f, 0xa3, 0x56, 0x2f, 0x9f, 0x1a, 0x82, 0x0e, 0xb4,
  36349. 0x46, 0xf5, 0x69, 0x89, 0x91, 0xf9, 0x2d, 0x99, 0x45, 0xa6,
  36350. 0x3c, 0x82, 0x74, 0xac, 0xeb, 0x58, 0x4a, 0xdd, 0x03, 0xaf,
  36351. 0xd1, 0x0a, 0xca, 0x4b, 0xe8, 0x4c, 0x63, 0xd4, 0x73, 0x94,
  36352. 0xbf, 0xd1, 0xc5, 0x8a, 0x3f, 0x6e, 0x58, 0xfc, 0x70, 0x76,
  36353. 0x69, 0x92, 0x05, 0xe0, 0xb9, 0xed, 0x5f, 0x19, 0xd7, 0x6f,
  36354. 0xd0, 0x35, 0xbb, 0x5a, 0x8d, 0x45, 0xac, 0x43, 0xcb, 0x74,
  36355. 0xcc, 0x92, 0xc3, 0x62, 0x56, 0x02, 0xb0, 0x0a, 0xb6, 0x88,
  36356. 0x40, 0x6f, 0x76, 0x1b, 0x89, 0xe4, 0x51, 0xeb, 0x7e, 0x08,
  36357. 0x8c, 0xce, 0x24, 0xc8, 0xd8, 0x58, 0xbd, 0x0e, 0x48, 0x57,
  36358. 0xc8, 0x9f, 0xad, 0x64, 0xcf, 0x69, 0x72, 0x35, 0xbf, 0x04,
  36359. 0x09, 0xfb, 0x0e, 0x62, 0x92, 0x76, 0x8b, 0x8d, 0xd5, 0x16,
  36360. 0xa2, 0x51, 0xdb, 0x71, 0xa9, 0x08, 0xb2, 0xf9, 0x1e, 0x07,
  36361. 0xe7, 0xf8, 0xf4, 0x79, 0x59, 0x2f, 0x8f, 0xf1, 0x5b, 0x45,
  36362. 0xe1, 0xb8, 0xb7, 0xef, 0x86, 0x69, 0x71, 0x51, 0x1c, 0xe5,
  36363. 0x61, 0xee, 0xb8, 0x1d, 0xa7, 0xdc, 0x48, 0xba, 0x51, 0xa5,
  36364. 0x70, 0x4d, 0xfd, 0x2c, 0x46, 0x21, 0x63, 0x0c, 0x9f, 0xb7,
  36365. 0x68, 0x58, 0x7b, 0xb3, 0x7d, 0x64, 0xfd, 0xaf, 0x87, 0x3d,
  36366. 0x86, 0x06, 0x36, 0x8a, 0x6d, 0xfe, 0xdf, 0xce, 0xa8, 0x16,
  36367. 0x42, 0x46, 0x15, 0xe5, 0xcf, 0x48, 0xa6, 0x4b, 0xe5, 0xc1,
  36368. 0xad, 0x14, 0x3a, 0x6d, 0xeb, 0xf9, 0xc9, 0x32, 0xd1, 0x82,
  36369. 0x60, 0x23, 0xf0, 0xff, 0xa7, 0xe6, 0x2e, 0xd6, 0x8d, 0x9d,
  36370. 0x4f, 0x6d, 0xb3, 0xc4, 0xad, 0xd9, 0xf0, 0xf5, 0x5c, 0x47,
  36371. 0x6c, 0x67, 0xf4, 0x0e, 0x18, 0x25, 0xbb, 0x67, 0xfa, 0x11,
  36372. 0x70, 0xd5, 0xbc, 0x3a, 0x34, 0xae, 0xa2, 0x76, 0x4b, 0x9f,
  36373. 0x59, 0x01, 0x18, 0x69, 0x44, 0xc4, 0x8a, 0xff, 0x00, 0xfc,
  36374. 0x2a, 0x45, 0xa9, 0x50, 0x8e, 0x37, 0x6b, 0x78, 0x14, 0x69,
  36375. 0xe7, 0x92, 0x3d, 0xf1, 0x34, 0xd5, 0x5c, 0x48, 0xc2, 0x50,
  36376. 0xb3, 0x0c, 0x7d, 0x54, 0x05, 0x31, 0x1e, 0xce, 0xaa, 0xc1,
  36377. 0x4c, 0xc9, 0x13, 0x33, 0x26, 0x1f, 0x56, 0x7e, 0x7e, 0x74,
  36378. 0xd3, 0x78, 0x3e, 0x00, 0x4a, 0xc8, 0xc6, 0x20, 0x5b, 0xb8,
  36379. 0x80, 0xb4, 0x13, 0x35, 0x23, 0xff, 0x50, 0xde, 0x25, 0x92,
  36380. 0x67, 0x08, 0xb8, 0xa3, 0xb6, 0x39, 0xd4, 0x30, 0xdc, 0xa5,
  36381. 0x88, 0x8a, 0x44, 0x08, 0x8b, 0x6d, 0x2e, 0xb8, 0xf3, 0x0d,
  36382. 0x23, 0xda, 0x35, 0x08, 0x5a, 0x92, 0xe1, 0x40, 0xac, 0xc7,
  36383. 0x15, 0x05, 0x8a, 0xdf, 0xe5, 0x71, 0xd8, 0xe0, 0xd7, 0x9f,
  36384. 0x58, 0x03, 0xf4, 0xec, 0x99, 0x3c, 0xb0, 0xe0, 0x07, 0x42,
  36385. 0x9b, 0xa0, 0x10, 0x7c, 0x24, 0x60, 0x19, 0xe8, 0x84, 0xd4,
  36386. 0xb1, 0x86, 0x19, 0x0a, 0x52, 0x70, 0x6e, 0xc2, 0x3c, 0xe2,
  36387. 0x73, 0x8d, 0xfe, 0xf8, 0x7e, 0xdf, 0x78, 0xe7, 0x92, 0x36,
  36388. 0x10, 0xf7, 0x2d, 0x76, 0x93, 0x8a, 0x0f, 0x20, 0xc8, 0x30,
  36389. 0x59, 0x81, 0xff, 0x3b, 0x70, 0x22, 0xce, 0x6e, 0x23, 0x68,
  36390. 0x35, 0x59, 0x0e, 0xcf, 0xf8, 0xf6, 0xcd, 0x45, 0xb6, 0x41,
  36391. 0xba, 0xda, 0xe6, 0x35, 0x0b, 0xd1, 0xef, 0xa5, 0x7c, 0xe0,
  36392. 0xb9, 0x6f, 0x5b, 0xa9, 0xab, 0x87, 0xe3, 0x3b, 0x92, 0xce,
  36393. 0xbe, 0xfe, 0xf7, 0xab, 0x82, 0xa3, 0xe6, 0xbd, 0xfe, 0xce,
  36394. 0xa6, 0x17, 0xcb, 0x4c, 0xb4, 0x4c, 0xd6, 0xfe, 0xbb, 0x1c,
  36395. 0x10, 0xde, 0x29, 0x3e, 0x92, 0x66, 0x20, 0xf8, 0xee, 0x83,
  36396. 0x86, 0x66, 0xe0, 0x66, 0x97, 0x85, 0xaf, 0x3a, 0x8f, 0xa9,
  36397. 0x97, 0x09, 0xde, 0x77, 0xda, 0xb7, 0x81, 0x41, 0x10, 0xca,
  36398. 0x66, 0x00, 0xec, 0xf8, 0x46, 0x73, 0xa6, 0x24, 0x36, 0xec,
  36399. 0x25, 0xbe, 0x93, 0x5e, 0x74, 0x9f, 0xbe, 0xf4, 0x84, 0x15,
  36400. 0x9c, 0xc5, 0x43, 0xd9, 0xea, 0x5a, 0xcc, 0x2c, 0x4e, 0x2e,
  36401. 0x4e, 0x32, 0xa6, 0x88, 0xb1, 0x25, 0x34, 0xf7, 0xba, 0xab,
  36402. 0xd3, 0xa0, 0xc2, 0x06, 0x70, 0xed, 0x66, 0x4d, 0x71, 0x34,
  36403. 0xaf, 0x10, 0x99, 0x10, 0x11, 0x4f, 0xe4, 0x7d, 0x42, 0x03,
  36404. 0x04, 0x02, 0xc2, 0x41, 0x85, 0x1e, 0xc4, 0xca, 0xae, 0xf0,
  36405. 0x83, 0x78, 0x34, 0x98, 0x55, 0x8b, 0x4c, 0xa0, 0x14, 0xea,
  36406. 0x15, 0x2c, 0xa1, 0x30, 0xd8, 0xcf, 0xac, 0xd4, 0xca, 0xf7,
  36407. 0xf4, 0xc4, 0x20, 0xca, 0xa1, 0xef, 0xce, 0x5d, 0x6b, 0x32,
  36408. 0xb6, 0xf0, 0x22, 0x08, 0x49, 0x21, 0x0c, 0x57, 0x0f, 0xf8,
  36409. 0xc0, 0xd2, 0xe3, 0xc0, 0xa6, 0x31, 0xc7, 0x87, 0x96, 0xa9,
  36410. 0xfe, 0x69, 0xa0, 0x7f, 0xf7, 0x8e, 0x31, 0x92, 0x37, 0xce,
  36411. 0xde, 0x36, 0x3f, 0xf5, 0x7d, 0x07, 0xaa, 0xa9, 0x43, 0xee,
  36412. 0x3c, 0x8c, 0xd3, 0x7d, 0x2c, 0xa6, 0xc3, 0x98, 0xab, 0xbe,
  36413. 0x90, 0x4c, 0xa5, 0x5a, 0x27, 0xeb, 0x0e, 0xed, 0xa1, 0x1e,
  36414. 0x3e, 0x44, 0xa3, 0x4b, 0x49, 0xad, 0xe4, 0x19, 0x90, 0xc8,
  36415. 0x9e, 0x6e, 0x5b, 0x68, 0xbc, 0x37, 0x54, 0xaf, 0xa6, 0xb7,
  36416. 0x71, 0x5c, 0x5d, 0x74, 0x83, 0xf4, 0xb9, 0x2f, 0xe5, 0x1a,
  36417. 0x0c, 0x73, 0x30, 0x56, 0x82, 0x04, 0xb3, 0x0e, 0x32, 0x98,
  36418. 0xfd, 0x27, 0xa0, 0xfe, 0xe0, 0xe0, 0xf5, 0xb7, 0xe0, 0x47,
  36419. 0x2a, 0xa6, 0x4a, 0xe0, 0xfc, 0xb5, 0xd8, 0xfd, 0x01, 0xfe,
  36420. 0x4e, 0x96, 0x17, 0x06, 0xcc, 0x92, 0x7c, 0xa1, 0x2f, 0xb5,
  36421. 0x04, 0x08, 0x76, 0xcc, 0x40, 0x75, 0x37, 0x4d, 0x2c, 0x74,
  36422. 0xcd, 0xc7, 0x62, 0xa6, 0xe6, 0xd8, 0x9e, 0x21, 0x7f, 0x2e,
  36423. 0xf5, 0x2c, 0xcf, 0x0b, 0x3f, 0xd7, 0xed, 0x17, 0xee, 0x92,
  36424. 0xaf, 0xf9, 0xa4, 0x71, 0x5d, 0x5f, 0x81, 0xb9, 0x2f, 0x12,
  36425. 0xe5, 0x57, 0x2d, 0x1e, 0xf1, 0x67, 0x47, 0x2a, 0xde, 0xab,
  36426. 0xf2, 0xea, 0xb7, 0xb5, 0x83, 0xdc, 0x46, 0xd4, 0xf3, 0x25,
  36427. 0x65, 0x15, 0x4d, 0x66, 0x34, 0x54, 0xab, 0x94, 0x89, 0x80,
  36428. 0x39, 0xd3, 0x39, 0xe3, 0xa2, 0xb1, 0x91, 0x2a, 0x5e, 0x55,
  36429. 0xe1, 0xa4, 0x0f, 0xc3, 0x4b, 0x5a, 0xa5, 0x4a, 0xb3, 0xc0,
  36430. 0x40, 0xea, 0x16, 0x0c, 0xd5, 0x2d, 0x83, 0x3e, 0x28, 0x20,
  36431. 0xac, 0x0a, 0x1b, 0x5b, 0x87, 0xcf, 0xf1, 0x51, 0xd6, 0xda,
  36432. 0xd1, 0xc9, 0xb1, 0x27, 0xf5, 0x62, 0x03, 0x10, 0xcf, 0x76,
  36433. 0x28, 0xa2, 0xea, 0x4b, 0x76, 0xaf, 0x9c, 0x3d, 0xf1, 0x1b,
  36434. 0x92, 0xff, 0xb0, 0xca, 0x16, 0xa2, 0x29, 0x94, 0x0e, 0x1e,
  36435. 0x51, 0xfb, 0xe1, 0x2b, 0x5a, 0x50, 0xfd, 0xaf, 0xab, 0xd7,
  36436. 0x32, 0xaa, 0x43, 0xa7, 0xcb, 0xd3, 0xd3, 0xe9, 0x1e, 0xb1,
  36437. 0x70, 0xd2, 0xbb, 0x15, 0x68, 0x49, 0xee, 0x6e, 0x1e, 0xc5,
  36438. 0x64, 0x4b, 0x26, 0x08, 0xe7, 0x32, 0x1c, 0x1d, 0x73, 0x8f,
  36439. 0x42, 0xfe, 0xeb, 0x67, 0x89, 0x42, 0x25, 0x40, 0xd6, 0x15,
  36440. 0x02, 0x55, 0x87, 0xe3, 0x87, 0xdd, 0x78, 0xc1, 0x01, 0x94,
  36441. 0xbc, 0x30, 0x5f, 0xbd, 0x89, 0xe1, 0xb0, 0x5c, 0xcd, 0xb7,
  36442. 0x68, 0xd5, 0xbb, 0xf4, 0xa0, 0x5d, 0x3d, 0xdd, 0x89, 0x12,
  36443. 0xc7, 0xb8, 0x5d, 0x51, 0x8a, 0xf4, 0xd5, 0x05, 0xc6, 0xdd,
  36444. 0x7b, 0x44, 0x38, 0xce, 0xb1, 0x24, 0x24, 0xe1, 0x9d, 0xc7,
  36445. 0x80, 0x86, 0x46, 0x2a, 0xd2, 0xa4, 0x0f, 0xec, 0xd3, 0x6b,
  36446. 0x31, 0xc0, 0x05, 0x31, 0xff, 0xf5, 0x1a, 0x33, 0x35, 0x68,
  36447. 0x2e, 0x68, 0x24, 0xbd, 0x62, 0xfc, 0x46, 0x79, 0x54, 0x5e,
  36448. 0x1e, 0x27, 0x93, 0x07, 0xed, 0x78, 0x94, 0x50, 0x42, 0x98,
  36449. 0x53, 0x88, 0xb7, 0x57, 0x04, 0x7d, 0xe2, 0xe1, 0xb5, 0x61,
  36450. 0x9e, 0x5a, 0x88, 0x31, 0x3e, 0x6c, 0x69, 0xbc, 0x8a, 0xe6,
  36451. 0xbc, 0x9d, 0x20, 0x7a, 0x86, 0xe5, 0x73, 0x93, 0x02, 0xc5,
  36452. 0xde, 0xdc, 0xcc, 0xbf, 0x89, 0x76, 0xdc, 0x4e, 0xa1, 0x89,
  36453. 0xe7, 0x95, 0x75, 0x01, 0xf7, 0x43, 0xaa, 0x3f, 0x1b, 0xb7,
  36454. 0x8c, 0x92, 0x66, 0x22, 0xbe, 0x34, 0xf1, 0x2f, 0xc3, 0xc7,
  36455. 0x21, 0xaf, 0x25, 0x57, 0x9a, 0x2c, 0x80, 0xf0, 0xb3, 0xdd,
  36456. 0xb3, 0xb2, 0x82, 0x97, 0x85, 0x73, 0xa9, 0x76, 0xe4, 0x37,
  36457. 0xa2, 0x65, 0xf9, 0xc1, 0x3d, 0x11, 0xbf, 0xcb, 0x3c, 0x8e,
  36458. 0xdd, 0xaf, 0x98, 0x57, 0x6a, 0xe1, 0x33, 0xe7, 0xf0, 0xff,
  36459. 0xed, 0x61, 0x53, 0xfe, 0x1e, 0x2d, 0x06, 0x2f, 0xb8, 0x9e,
  36460. 0xf9, 0xa5, 0x21, 0x06, 0xf3, 0x72, 0xf6, 0xa3, 0x77, 0xbb,
  36461. 0x63, 0x6e, 0x52, 0xb2, 0x42, 0x47, 0x9b, 0x92, 0x4c, 0xf8,
  36462. 0xd2, 0xe6, 0x02, 0xa5, 0x57, 0x2d, 0x6f, 0x30, 0x05, 0xe2,
  36463. 0xfd, 0x33, 0xe5, 0xb6, 0x23, 0x85, 0x89, 0x4a, 0x99, 0x20,
  36464. 0x33, 0xea, 0x2f, 0xcd, 0x28, 0x27, 0xff, 0xfd, 0x2e, 0x73,
  36465. 0x52, 0x29, 0x19, 0x7c, 0x65, 0xf5, 0x6a, 0xaa, 0x97, 0x6e,
  36466. 0xe9, 0x42, 0xa8, 0x55, 0x97, 0x56, 0x92, 0x9d, 0xd2, 0xd1,
  36467. 0xc4, 0x30, 0xaa, 0x95, 0x86, 0xba, 0x71, 0xdd, 0x2f, 0xf1,
  36468. 0xed, 0x66, 0x54, 0x78, 0x4b, 0x13, 0x31, 0xed, 0x9d, 0x2c,
  36469. 0xae, 0x0a, 0xc3, 0xca, 0xfb, 0x3f, 0x92, 0x92, 0x30, 0xa3,
  36470. 0x8e, 0xc8, 0x6d, 0x7b, 0x42, 0xd5, 0x5d, 0x99, 0x79, 0x42,
  36471. 0x28, 0x63, 0x9f, 0x97, 0x8e, 0x94, 0x6d, 0x1d, 0xb4, 0x21,
  36472. 0x39, 0xc7, 0x64, 0x48, 0x44, 0x5e, 0x15, 0x10, 0x45, 0x9f,
  36473. 0x8a, 0x01, 0x45, 0x20, 0x5c, 0xd1, 0x28, 0x0d, 0xe9, 0xfb,
  36474. 0xa9, 0x72, 0x68, 0x07, 0x31, 0x20, 0x75, 0x76, 0x82, 0x76,
  36475. 0x5d, 0x7c, 0xc1, 0x5d, 0x42, 0x40, 0xfd, 0x06, 0xa9, 0x66,
  36476. 0xb0, 0x36, 0x55, 0x86, 0x6c, 0x96, 0xbd, 0xb8, 0xf7, 0x36,
  36477. 0x87, 0xf2, 0xa1, 0x37, 0xd8, 0x2d, 0x83, 0xf5, 0xdc, 0xd8,
  36478. 0xde, 0x9e, 0x69, 0xd6, 0xe1, 0x0d, 0xd5, 0x93, 0xc5, 0xee,
  36479. 0xba, 0xd3, 0x40, 0x71, 0xbb, 0xc7, 0xbb, 0x50, 0x1a, 0x10,
  36480. 0x80, 0x99, 0x62, 0x1c, 0xe3, 0x1f, 0xa2, 0xcc, 0x98, 0xe1,
  36481. 0xaa, 0xff, 0xd9, 0x69, 0xe7, 0x87, 0x04, 0x87, 0x76, 0xec,
  36482. 0x55, 0x18, 0xaf, 0x82, 0x34, 0x4d, 0x4f, 0xf7, 0x57, 0x1f,
  36483. 0xa5, 0x43, 0xcc, 0xe9, 0x7a, 0x4a, 0xc8, 0xb4, 0x1f, 0x61,
  36484. 0x40, 0x5e, 0x1d, 0x11, 0xdd, 0xdc, 0xdc, 0xb4, 0x57, 0xf9,
  36485. 0x47, 0x96, 0xbc, 0x47, 0x29, 0xf8, 0xf2, 0x43, 0xc4, 0xa0,
  36486. 0x8c, 0x14, 0x5e, 0x73, 0x52, 0xac, 0xac, 0x39, 0x3b, 0x06,
  36487. 0x19, 0x1a, 0xca, 0x22, 0xc8, 0x96, 0x12, 0x2e, 0x4c, 0x7b,
  36488. 0xa0, 0x96, 0x53, 0x16, 0xce, 0x6d, 0x6e, 0xac, 0xb2, 0x07,
  36489. 0x17, 0x22, 0x07, 0x30, 0x20, 0x84, 0x9b, 0x0e, 0x92, 0x31,
  36490. 0x07, 0xe2, 0x77, 0xcd, 0x6a, 0x3e, 0x16, 0x4f, 0xd6, 0x12,
  36491. 0x88, 0x8a, 0x70, 0x5a, 0x87, 0xd8, 0xb9, 0xef, 0x76, 0xab,
  36492. 0x14, 0x65, 0x87, 0x3a, 0xef, 0xd8, 0x0e, 0x24, 0x40, 0x73,
  36493. 0x93, 0x2b, 0xbf, 0xac, 0xfe, 0x96, 0x8a, 0x9d, 0x12, 0xe6,
  36494. 0xc1, 0x5b, 0x00, 0x3b, 0x23, 0xee, 0xe2, 0x10, 0xb6, 0xbe,
  36495. 0x0e, 0x2f, 0xa2, 0x77, 0x16, 0x17, 0xfc, 0x4b, 0x2c, 0xd7,
  36496. 0x9c, 0xad, 0x66, 0xb4, 0xf2, 0xfd, 0xc1, 0xaf, 0x81, 0x12,
  36497. 0xd9, 0xed, 0x14, 0x32, 0xcf, 0x1b, 0xee, 0xc6, 0x63, 0xe8,
  36498. 0xe5, 0xe6, 0xb6, 0x91, 0x8d, 0x1b, 0x90, 0x75, 0x5d, 0x69,
  36499. 0x4c, 0x5d, 0xd6, 0xac, 0x79, 0xe8, 0xb6, 0xdf, 0xbf, 0x43,
  36500. 0x39, 0xd3, 0xb8, 0xf0, 0x39, 0xf4, 0x90, 0xaf, 0x73, 0x26,
  36501. 0xc7, 0x73, 0x6f, 0x93, 0xbb, 0xce, 0x6e, 0xdc, 0x1c, 0xd0,
  36502. 0x36, 0x23, 0x17, 0xb2, 0x39, 0x37, 0x15, 0xf5, 0x3a, 0x61,
  36503. 0xa9, 0x15, 0x52, 0x6e, 0xc5, 0x3a, 0x63, 0x79, 0x5d, 0x45,
  36504. 0xdc, 0x3a, 0xd5, 0x26, 0x01, 0x56, 0x97, 0x80, 0x7f, 0x83,
  36505. 0xf9, 0xec, 0xde, 0xa0, 0x2e, 0x7a, 0xb2, 0x4b, 0x04, 0x63,
  36506. 0x60, 0x05, 0xce, 0x96, 0xeb, 0xe0, 0x0a, 0x5f, 0xb0, 0x7e,
  36507. 0x6d, 0x0a, 0x24, 0x32, 0x47, 0x82, 0x7f, 0x0b, 0xd7, 0xe9,
  36508. 0xd5, 0x14, 0xa9, 0x6b, 0x10, 0x5d, 0x1e, 0x1f, 0x8a, 0xad,
  36509. 0x70, 0x91, 0xd4, 0x33, 0x1d, 0xc2, 0x3e, 0xf8, 0xc8, 0x52,
  36510. 0x9a, 0x27, 0x1f, 0x45, 0x2f, 0xb5, 0xc7, 0xb1, 0x8b, 0xf9,
  36511. 0xc6, 0x7b, 0xb5, 0x92, 0x7a, 0xdd, 0xeb, 0x07, 0x6c, 0x6f,
  36512. 0x11, 0xd7, 0x5b, 0x56, 0x56, 0xec, 0x88, 0x1c, 0xc9, 0xb4,
  36513. 0xe8, 0x43, 0xab, 0xdf, 0x0b, 0xc5, 0x28, 0xba, 0x70, 0x5d,
  36514. 0xd3, 0xb2, 0xe2, 0xcf, 0xa7, 0xbb, 0x53, 0x04, 0x6b, 0x73,
  36515. 0xdf, 0x27, 0xa6, 0x63, 0x58, 0xe1, 0x39, 0x26, 0x2a, 0x1a,
  36516. 0x21, 0xec, 0xbb, 0x5f, 0x46, 0x98, 0x3d, 0x48, 0x66, 0xfe,
  36517. 0xf3, 0xcb, 0xfc, 0x6e, 0x99, 0x82, 0x91, 0xce, 0x53, 0xfd,
  36518. 0x75, 0xc9, 0xb6, 0x08, 0xa8, 0xf3, 0xe4, 0xe0, 0xa0, 0x24,
  36519. 0x45, 0xb4, 0x69, 0x11, 0xac, 0x06, 0x1c, 0x39, 0x71, 0xcf,
  36520. 0x72, 0xfc, 0x77, 0x9b, 0x5f, 0xf4, 0x8b, 0x02, 0x31, 0xf3,
  36521. 0x67, 0xd1, 0x9b, 0xe0, 0x49, 0xa4, 0x69, 0x20, 0x99, 0x38,
  36522. 0xa7, 0xf5, 0x43, 0xd2, 0x45, 0x9f, 0x7a, 0xe7, 0xad, 0x7e,
  36523. 0x36, 0xee, 0xfd, 0x8c, 0xc5, 0x6a, 0x12, 0x58, 0x15, 0x3b,
  36524. 0x02, 0x81, 0x73, 0x8b, 0x10, 0xda, 0x21, 0xc7, 0x1d, 0x38,
  36525. 0xd8, 0x40, 0x7a, 0xa3, 0x59, 0x55, 0x35, 0x44, 0xa9, 0x9c,
  36526. 0xf5, 0xf4, 0xe4, 0x14, 0xc1, 0xc4, 0x15, 0x26, 0x01, 0xe3,
  36527. 0x31, 0xbf, 0xdc, 0xbc, 0x69, 0x0b, 0xcf, 0x71, 0x8c, 0xdb,
  36528. 0x16, 0xab, 0x36, 0x3e, 0xb3, 0xa4, 0x9f, 0xcc, 0xbf, 0xa2,
  36529. 0x93, 0x93, 0x9a, 0x3b, 0xaf, 0x72, 0x8d, 0x8b, 0x92, 0x44,
  36530. 0x5d, 0x6f, 0xc5, 0xf0, 0xdc, 0x65, 0x62, 0xea, 0xba, 0x33,
  36531. 0xe7, 0x6c, 0xa4, 0x35, 0xcf, 0xd9, 0xbc, 0x3c, 0xbf, 0x25,
  36532. 0x7b, 0x7c, 0x0b, 0x62, 0x92, 0x5a, 0x66, 0x63, 0xe1, 0x27,
  36533. 0x89, 0x12, 0xe2, 0xae, 0xb7, 0xf8, 0x04, 0x70, 0xda, 0x4a,
  36534. 0x3d, 0xa6, 0x67, 0x12, 0x14, 0x9e, 0x8e, 0xdc, 0xa2, 0xf2,
  36535. 0x3d, 0xc7, 0xd2, 0x8f, 0x18, 0x3a, 0x53, 0x8c, 0x83, 0x5d,
  36536. 0x66, 0xbb, 0x9f, 0x8c, 0xaf, 0xa8, 0x73, 0x08, 0x2e, 0x6d,
  36537. 0x30, 0xa0, 0xd0, 0x20, 0x94, 0x48, 0xad, 0x5e, 0x31, 0xfd,
  36538. 0x5e, 0xfd, 0xf9, 0xb5, 0xa2, 0x39, 0xa3, 0xb9, 0xdf, 0x4d,
  36539. 0xa4, 0xb1, 0x54, 0xcc, 0x92, 0x63, 0x2c, 0x66, 0x2d, 0x01,
  36540. 0x88, 0x8b, 0x7d, 0xc6, 0x5c, 0x9f, 0x18, 0x9a, 0x53, 0x91,
  36541. 0x59, 0x66, 0x70, 0xd7, 0x81, 0x0e, 0xa1, 0x3c, 0x7e, 0x86,
  36542. 0x85, 0x64, 0x38, 0x6f, 0xec, 0x76, 0x57, 0x80, 0x41, 0x9d,
  36543. 0xef, 0x61, 0xb8, 0xb2, 0x8a, 0xeb, 0xe9, 0x26, 0xbb, 0x69,
  36544. 0xb3, 0x8d, 0xd4, 0x6b, 0x05, 0xd8, 0x55, 0x1c, 0xbd, 0x9f,
  36545. 0x6b, 0x23, 0x46, 0x2b, 0xf7, 0xfb, 0x4d, 0x33, 0x3b, 0x21,
  36546. 0x6d, 0xea, 0x1b, 0x15, 0xaf, 0x0f, 0x8c, 0x98, 0xc8, 0xf4,
  36547. 0xd1, 0x3c, 0xdd, 0x21, 0xd0, 0x45, 0xdc, 0xaf, 0x89, 0x89,
  36548. 0xbf, 0xde, 0xbf, 0x46, 0x9e, 0x9e, 0x18, 0x56, 0x9d, 0x05,
  36549. 0x4d, 0x63, 0x5f, 0x1c, 0xd9, 0x15, 0xd1, 0x43, 0x17, 0x0c,
  36550. 0x48, 0x3d, 0x36, 0x8b, 0x14, 0x87, 0xc8, 0x10, 0x44, 0xdf,
  36551. 0x9c, 0xfd, 0x6e, 0x88, 0x88, 0xae, 0x7f, 0x7f, 0x67, 0xa3,
  36552. 0x33, 0x4d, 0xa3, 0x84, 0x8b, 0x58, 0x07, 0x17, 0xd8, 0x1d,
  36553. 0x9e, 0x43, 0xd6, 0x41, 0x9c, 0xff, 0xfa, 0x35, 0xa2, 0x42,
  36554. 0xa9, 0x5d, 0xa9, 0x4b, 0x95, 0x23, 0x6a, 0x6e, 0x42, 0xd7,
  36555. 0xa2, 0x0a, 0x70, 0x00, 0x61, 0x8b, 0x45, 0xbb, 0xac, 0x20,
  36556. 0x27, 0xcd, 0xfc, 0x61, 0x17, 0xfe, 0xab, 0x6b, 0xe8, 0xe0,
  36557. 0x51, 0xab, 0xa3, 0xbf, 0xe4, 0x85, 0x69, 0x8e, 0xd7, 0xa6,
  36558. 0x62, 0x33, 0x8f, 0x7c, 0xba, 0x48, 0xfa, 0x83, 0x94, 0xa5,
  36559. 0xdf, 0xa1, 0x76, 0xdc, 0xa9, 0x4b, 0x3c, 0x27, 0xff, 0xd9,
  36560. 0xbe, 0xf4, 0x80, 0x5a, 0xca, 0x33, 0xf3, 0x9a, 0x1d, 0xf8,
  36561. 0xf3, 0xe1, 0x83, 0x27, 0x0b, 0x59, 0x87, 0x31, 0x7d, 0x4f,
  36562. 0x5a, 0x5e, 0xe1, 0xbe, 0xa9, 0x68, 0xe9, 0x6f, 0x10, 0x0a,
  36563. 0xe2, 0x70, 0x05, 0xaa, 0xcb, 0xdd, 0x41, 0xd7, 0x49, 0x8a,
  36564. 0x98, 0xa0, 0x40, 0x2d, 0xc6, 0x56, 0x49, 0xca, 0x60, 0x16,
  36565. 0x9c, 0x38, 0xc9, 0xfe, 0x99, 0x15, 0xfb, 0x79, 0x01, 0x33,
  36566. 0xcd, 0x54, 0x2f, 0xf3, 0x70, 0x37, 0x82, 0x36, 0x32, 0x76,
  36567. 0x8f, 0x63, 0x00, 0xa2, 0x42, 0xce, 0x39, 0x90, 0xfc, 0xf8,
  36568. 0xff, 0x34, 0x38, 0x0a, 0x17, 0x5e, 0x9d, 0x34, 0x86, 0xde,
  36569. 0x33, 0x45, 0xac, 0xbf, 0x81, 0xdf, 0xd2, 0xbc, 0xc7, 0xd7,
  36570. 0xd1, 0xee, 0xde, 0x2b, 0x5b, 0x50, 0x56, 0xb5, 0x88, 0x00,
  36571. 0x92, 0x76, 0x5a, 0x34, 0x0c, 0xfe, 0x8f, 0xc5, 0xa0, 0x92,
  36572. 0xb0, 0xed, 0x43, 0xe7, 0x81, 0x39, 0x36, 0x6e, 0xb7, 0x4d,
  36573. 0x5b, 0xcf, 0xc7, 0xf0, 0x83, 0xe5, 0xdc, 0xb7, 0x74, 0xf4,
  36574. 0xf3, 0xbd, 0xa8, 0xa6, 0x7b, 0xe0, 0xc5, 0x50, 0xaa, 0xc7,
  36575. 0x83, 0x4d, 0xd9, 0xc5, 0x97, 0x03, 0x7c, 0x0c, 0x3b, 0x3a,
  36576. 0x18, 0xb2, 0x8c, 0xee, 0x67, 0x91, 0x38, 0x84, 0x8f, 0xef,
  36577. 0xb4, 0xf4, 0xe4, 0x7c, 0x1a, 0x3f, 0xa3, 0x0a, 0xd9, 0xba,
  36578. 0xff, 0x56, 0xd8, 0xe2, 0x82, 0xfc, 0x58, 0x8f, 0xf6, 0x12,
  36579. 0x10, 0x65, 0x6a, 0x68, 0x53, 0x2d, 0x9f, 0x2c, 0x77, 0xd1,
  36580. 0xb8, 0x21, 0x8a, 0xcb, 0xe9, 0xd4, 0x25, 0x18, 0x22, 0x46,
  36581. 0x3e, 0x72, 0x29, 0x2a, 0x68, 0x70, 0x73, 0xe2, 0x61, 0xa2,
  36582. 0xa8, 0x1f, 0x24, 0x48, 0x92, 0xa0, 0xd4, 0xdd, 0xde, 0xe5,
  36583. 0x02, 0x1b, 0x59, 0x5c, 0x7e, 0x92, 0x9c, 0xd8, 0xf4, 0x2d,
  36584. 0x6b, 0x79, 0x7b, 0xc7, 0xcd, 0xef, 0x21, 0x2a, 0x50, 0x7e,
  36585. 0xba, 0xdd, 0x02, 0x45, 0x7e, 0xc1, 0xdd, 0xeb, 0x00, 0x00,
  36586. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  36587. 0x00, 0x00, 0x00, 0x03, 0x0c, 0x15, 0x1c, 0x22, 0x28,
  36588. };
  36589. return dilithium_param_vfy_test(WC_ML_DSA_65, ml_dsa_65_pub_key,
  36590. (word32)sizeof(ml_dsa_65_pub_key), ml_dsa_65_sig,
  36591. (word32)sizeof(ml_dsa_65_sig));
  36592. }
  36593. #endif
  36594. #ifndef WOLFSSL_NO_ML_DSA_87
  36595. static wc_test_ret_t dilithium_param_87_vfy_test(void)
  36596. {
  36597. WOLFSSL_SMALL_STACK_STATIC const byte ml_dsa_87_pub_key[] = {
  36598. 0xef, 0x49, 0x79, 0x47, 0x15, 0xc4, 0x8a, 0xa9, 0x74, 0x2a,
  36599. 0xf0, 0x36, 0x94, 0x5c, 0x91, 0x1c, 0x5d, 0xff, 0x2c, 0x83,
  36600. 0xf2, 0x8b, 0x04, 0xfc, 0x5d, 0x64, 0xbd, 0x49, 0x73, 0xcd,
  36601. 0xcc, 0x99, 0x50, 0x5f, 0x2b, 0x16, 0x3a, 0xbb, 0x98, 0xc0,
  36602. 0xa7, 0x69, 0x0e, 0x95, 0x99, 0x0b, 0xa2, 0x6c, 0xfe, 0x6c,
  36603. 0xdb, 0xc8, 0xa7, 0x09, 0x46, 0x6c, 0x90, 0x50, 0xa4, 0x75,
  36604. 0x30, 0xf7, 0x90, 0xac, 0x31, 0xb6, 0xdd, 0x21, 0xaf, 0xc6,
  36605. 0xf9, 0xfe, 0xee, 0xc6, 0x5b, 0xa8, 0x8f, 0x0a, 0x2e, 0xd0,
  36606. 0x42, 0xab, 0xa8, 0x3c, 0x8d, 0xbf, 0xf7, 0x44, 0xbd, 0x0d,
  36607. 0xcf, 0xf4, 0x68, 0xfc, 0x16, 0x67, 0xf7, 0x39, 0x48, 0x5f,
  36608. 0x56, 0xd1, 0xe7, 0x1f, 0x49, 0x80, 0x50, 0xbe, 0x54, 0xd1,
  36609. 0xb7, 0xc9, 0xd2, 0x32, 0xc7, 0x08, 0x8c, 0xde, 0x2c, 0x31,
  36610. 0xf6, 0x1d, 0xc7, 0xac, 0xb3, 0x79, 0xd7, 0x4b, 0x1b, 0x23,
  36611. 0x89, 0x0a, 0xdc, 0x8e, 0x44, 0x41, 0x14, 0x28, 0x99, 0x13,
  36612. 0xb3, 0x26, 0xa6, 0x0e, 0x83, 0x60, 0xaa, 0x8d, 0x7c, 0x23,
  36613. 0x13, 0xba, 0x6c, 0x28, 0x90, 0x56, 0x84, 0xa1, 0x23, 0x8b,
  36614. 0x81, 0x20, 0x97, 0x7c, 0x66, 0x3f, 0xed, 0x5d, 0xd0, 0xe4,
  36615. 0x5d, 0xee, 0x46, 0xbc, 0x4b, 0x3c, 0x03, 0xb5, 0xbc, 0x4d,
  36616. 0x8d, 0x37, 0xa3, 0x56, 0x4b, 0x33, 0xad, 0xef, 0xd4, 0xb6,
  36617. 0xec, 0xdb, 0x04, 0x9a, 0x19, 0x58, 0x57, 0xd8, 0x00, 0x3a,
  36618. 0x92, 0x61, 0x0c, 0x0b, 0xc8, 0x52, 0xe5, 0x04, 0x02, 0x9a,
  36619. 0x00, 0x7e, 0xec, 0x7e, 0x94, 0xaa, 0xef, 0x2d, 0x7f, 0xb6,
  36620. 0x2e, 0x7c, 0xb0, 0x73, 0xa2, 0x20, 0xc0, 0x07, 0x30, 0x41,
  36621. 0x50, 0x20, 0x14, 0x18, 0x21, 0x5e, 0x2a, 0x6f, 0x70, 0x21,
  36622. 0xd6, 0x97, 0x13, 0xb9, 0xc1, 0x9e, 0x90, 0x67, 0xcc, 0x55,
  36623. 0x8a, 0xec, 0xec, 0x0a, 0x1e, 0x90, 0xdc, 0x3f, 0xb0, 0x4d,
  36624. 0xd1, 0x18, 0xea, 0x4f, 0xcb, 0x5d, 0x15, 0x4c, 0xb8, 0x35,
  36625. 0x9b, 0x34, 0x24, 0x30, 0x06, 0x53, 0x17, 0xf0, 0xbe, 0x27,
  36626. 0x36, 0xb3, 0x04, 0x6a, 0xbd, 0xbf, 0xa7, 0x39, 0xee, 0xa9,
  36627. 0x8f, 0x0e, 0x98, 0xc5, 0xf5, 0x9f, 0x46, 0x25, 0x93, 0xc9,
  36628. 0xf2, 0xf6, 0x2b, 0x8e, 0x92, 0x06, 0x01, 0x3d, 0x81, 0x18,
  36629. 0xf2, 0xec, 0xf1, 0x05, 0x4c, 0xad, 0x4b, 0xcb, 0x98, 0xa4,
  36630. 0xb5, 0x61, 0x20, 0xda, 0x81, 0xa1, 0xfb, 0x92, 0x4c, 0xaf,
  36631. 0x87, 0x6f, 0x6e, 0xd2, 0x57, 0xec, 0xcd, 0x94, 0xb3, 0x79,
  36632. 0xbf, 0x59, 0x88, 0x17, 0x81, 0xce, 0x8a, 0x57, 0xce, 0x57,
  36633. 0xae, 0x3e, 0x82, 0x81, 0x2f, 0x83, 0x61, 0xd8, 0xf9, 0x68,
  36634. 0x21, 0xe7, 0x72, 0x5b, 0xd6, 0x80, 0x55, 0x68, 0x5d, 0x67,
  36635. 0x15, 0x0c, 0x8b, 0xdc, 0x4f, 0xc3, 0x89, 0x36, 0x3c, 0xac,
  36636. 0xaf, 0x16, 0x5e, 0x1c, 0xfa, 0x68, 0x74, 0x6a, 0xab, 0x68,
  36637. 0xd8, 0x59, 0x96, 0x2d, 0x33, 0x62, 0xe4, 0xbd, 0xb3, 0xb7,
  36638. 0x4d, 0x88, 0x35, 0xb8, 0xed, 0xb2, 0x16, 0x85, 0x97, 0x08,
  36639. 0x71, 0x71, 0x39, 0x7e, 0x0c, 0x53, 0x16, 0xda, 0x38, 0xe5,
  36640. 0x28, 0x09, 0x9c, 0xd9, 0x46, 0xec, 0x68, 0xda, 0x8d, 0xd0,
  36641. 0xad, 0xb2, 0x79, 0x28, 0x3b, 0x1e, 0x12, 0xc9, 0xdf, 0xa9,
  36642. 0x6d, 0x3d, 0x29, 0x99, 0x2f, 0x53, 0xc2, 0xd0, 0xf9, 0x88,
  36643. 0x26, 0x94, 0x47, 0xaf, 0xf6, 0x96, 0xf3, 0xe1, 0x11, 0xa6,
  36644. 0x82, 0x3d, 0x43, 0x3f, 0x1f, 0xbc, 0xf6, 0x98, 0xbe, 0xff,
  36645. 0x06, 0x86, 0x61, 0x27, 0xdc, 0x91, 0x54, 0xd4, 0xfc, 0x68,
  36646. 0x83, 0xe8, 0x35, 0x3e, 0xee, 0x94, 0x59, 0x28, 0x2f, 0xde,
  36647. 0xdd, 0x03, 0x60, 0x66, 0xc1, 0x49, 0x57, 0xdd, 0xbc, 0xd5,
  36648. 0x0a, 0x67, 0x34, 0xf1, 0xa6, 0x0a, 0x57, 0x94, 0x65, 0x02,
  36649. 0x2c, 0x52, 0x43, 0x70, 0x3b, 0xc1, 0x9a, 0xff, 0xda, 0x6f,
  36650. 0xb9, 0x54, 0x47, 0x01, 0xda, 0x27, 0xe4, 0x48, 0x4a, 0x90,
  36651. 0x9f, 0xb5, 0xc3, 0xee, 0x0e, 0x09, 0x57, 0xfe, 0x48, 0x51,
  36652. 0x08, 0x34, 0x5e, 0x8f, 0x16, 0xc9, 0x0b, 0x74, 0xd9, 0x7d,
  36653. 0x22, 0x3f, 0xd6, 0xb7, 0x5d, 0xd6, 0x76, 0x00, 0x8d, 0x4e,
  36654. 0x78, 0x73, 0x86, 0xd6, 0xdb, 0x2a, 0x65, 0xab, 0xdf, 0xb0,
  36655. 0xea, 0x11, 0xad, 0xdf, 0xba, 0x43, 0xdb, 0xa8, 0x0a, 0xfb,
  36656. 0x04, 0x38, 0x81, 0x2b, 0xa3, 0x29, 0xfc, 0x95, 0x73, 0x9a,
  36657. 0x0c, 0x6c, 0x9e, 0xcd, 0xdc, 0xcf, 0x0a, 0x0c, 0x18, 0x41,
  36658. 0x6f, 0x1d, 0xa3, 0xf6, 0x12, 0x4c, 0x13, 0xf2, 0x02, 0xc6,
  36659. 0x50, 0x99, 0x86, 0x73, 0xa7, 0xf9, 0x7e, 0x84, 0x7f, 0x4c,
  36660. 0x00, 0xce, 0x2e, 0x21, 0x76, 0x8e, 0x17, 0x7a, 0x87, 0x6f,
  36661. 0x81, 0xe6, 0xc0, 0x52, 0xa5, 0xa0, 0x3c, 0x54, 0x3c, 0xec,
  36662. 0xb0, 0x9d, 0x1c, 0x3b, 0xec, 0xe5, 0x4e, 0x4a, 0x37, 0xe7,
  36663. 0xd5, 0xa9, 0x07, 0x87, 0x23, 0x28, 0x5d, 0x3d, 0x22, 0x02,
  36664. 0x79, 0x40, 0x3f, 0x2d, 0x40, 0xc9, 0xe5, 0xa6, 0x9b, 0xa8,
  36665. 0xb8, 0x76, 0xf6, 0x77, 0x5b, 0x8d, 0x72, 0x96, 0x3e, 0x13,
  36666. 0xbf, 0x76, 0xfa, 0x7b, 0xb7, 0x82, 0x5f, 0xe7, 0x9d, 0x54,
  36667. 0x0e, 0x05, 0x1a, 0x9f, 0xa4, 0x42, 0xa5, 0xb4, 0x93, 0x23,
  36668. 0x06, 0x59, 0x43, 0xa8, 0xe8, 0x5c, 0xfc, 0x18, 0x97, 0xdb,
  36669. 0xad, 0x9a, 0x80, 0x0a, 0xf2, 0x20, 0x50, 0xac, 0xc1, 0x13,
  36670. 0x3e, 0x98, 0x09, 0xde, 0xf2, 0x70, 0x9e, 0x14, 0xc2, 0x5c,
  36671. 0xec, 0x65, 0x07, 0x0b, 0xfa, 0x02, 0x5c, 0xf8, 0x71, 0xaa,
  36672. 0x9b, 0x45, 0x62, 0xe2, 0x27, 0xaf, 0x77, 0xf8, 0xe3, 0xeb,
  36673. 0x7b, 0x24, 0x7b, 0x3c, 0x67, 0xc2, 0x6d, 0x6e, 0x17, 0xae,
  36674. 0x6e, 0x86, 0x6f, 0x98, 0xc9, 0xac, 0x13, 0x9f, 0x87, 0x64,
  36675. 0x3d, 0x4d, 0x6f, 0xa0, 0xb3, 0x39, 0xc6, 0x68, 0x1b, 0xa7,
  36676. 0xeb, 0x3e, 0x0f, 0x6b, 0xc7, 0xa4, 0xe2, 0x20, 0x27, 0x75,
  36677. 0x3f, 0x09, 0x16, 0xff, 0x1a, 0xcc, 0xa7, 0xc4, 0x6d, 0xc2,
  36678. 0xfc, 0xc3, 0x0b, 0x37, 0x63, 0xff, 0x9b, 0x10, 0xe6, 0x00,
  36679. 0xf7, 0x18, 0x43, 0x9f, 0x07, 0x50, 0x31, 0x51, 0xd4, 0xfd,
  36680. 0xad, 0xa2, 0x0f, 0x77, 0xda, 0x41, 0xc1, 0x0a, 0x6f, 0x86,
  36681. 0xd7, 0xdc, 0x8a, 0x52, 0xd6, 0xa1, 0x27, 0xdb, 0x14, 0x67,
  36682. 0x26, 0x91, 0xb3, 0xcd, 0x01, 0x5f, 0x60, 0xa1, 0x7f, 0x43,
  36683. 0x15, 0x1a, 0x82, 0x0f, 0xd3, 0x66, 0x5f, 0x60, 0x57, 0x2f,
  36684. 0xb2, 0x8c, 0x27, 0x2a, 0x9d, 0x1b, 0xf9, 0xf2, 0x59, 0x20,
  36685. 0x39, 0xd9, 0xc5, 0xaf, 0xf2, 0x36, 0x8c, 0x58, 0x00, 0x1b,
  36686. 0xd0, 0xc5, 0x8e, 0x1a, 0x49, 0xa8, 0x60, 0xbe, 0xd1, 0xd7,
  36687. 0x2a, 0xb0, 0xc2, 0xab, 0x58, 0x8a, 0x7a, 0xa9, 0x41, 0x68,
  36688. 0x70, 0xbd, 0xea, 0x73, 0xa5, 0x03, 0x11, 0xb2, 0x27, 0xd9,
  36689. 0xcd, 0xf5, 0x09, 0xe8, 0x1c, 0xe2, 0x4f, 0x50, 0x6a, 0x84,
  36690. 0x34, 0x62, 0x2e, 0x36, 0xaa, 0x4c, 0xc1, 0x83, 0x78, 0x98,
  36691. 0x35, 0x7a, 0x27, 0x7e, 0xfe, 0xf1, 0x6f, 0x59, 0x27, 0x35,
  36692. 0x73, 0xce, 0x74, 0xaa, 0xb4, 0x72, 0x82, 0xa8, 0xe2, 0x81,
  36693. 0x7a, 0x6b, 0xca, 0x33, 0xa5, 0xda, 0xa2, 0x63, 0xca, 0x2e,
  36694. 0x90, 0x03, 0x32, 0xec, 0x63, 0xdb, 0x52, 0x7b, 0x16, 0xfc,
  36695. 0x01, 0x2d, 0x30, 0x12, 0x1e, 0xf9, 0xa3, 0x72, 0x21, 0x3c,
  36696. 0x75, 0x0c, 0x61, 0x9c, 0x7e, 0x73, 0x04, 0x71, 0x41, 0x45,
  36697. 0x5d, 0x7f, 0x49, 0x1c, 0x09, 0x08, 0xa4, 0xec, 0x2f, 0xfd,
  36698. 0xc4, 0xfb, 0x59, 0x6a, 0x27, 0x7a, 0xd4, 0xfc, 0x5f, 0x20,
  36699. 0x04, 0x34, 0x7d, 0x08, 0xed, 0x82, 0x5a, 0x90, 0xe1, 0xab,
  36700. 0xfd, 0x35, 0x3a, 0x8d, 0xbb, 0x0a, 0x9d, 0x73, 0xff, 0x69,
  36701. 0xe5, 0xe9, 0x09, 0x55, 0x14, 0xd9, 0x7b, 0x6f, 0x0d, 0x99,
  36702. 0xd2, 0x7e, 0x71, 0xf8, 0x4f, 0x72, 0x2f, 0xbb, 0xc6, 0xc4,
  36703. 0x36, 0xc9, 0x01, 0xd3, 0x9b, 0x94, 0xab, 0x41, 0x0f, 0x4a,
  36704. 0x61, 0x5c, 0x68, 0xe5, 0xd7, 0x0d, 0x94, 0xaa, 0xee, 0xba,
  36705. 0x95, 0xcb, 0x8c, 0x0e, 0x85, 0x3a, 0x02, 0x6b, 0x95, 0x50,
  36706. 0xfd, 0x02, 0xfd, 0xa4, 0x58, 0x29, 0x78, 0x4f, 0xd0, 0xae,
  36707. 0x66, 0xd6, 0x5c, 0xe7, 0x45, 0xfe, 0x98, 0xb0, 0xa3, 0xe2,
  36708. 0x87, 0xc0, 0xd2, 0x81, 0x08, 0xf1, 0xf1, 0xe7, 0xda, 0x62,
  36709. 0x9e, 0xa0, 0x34, 0x86, 0xeb, 0xa1, 0x6e, 0x4a, 0x26, 0x8e,
  36710. 0x39, 0x0c, 0x51, 0x10, 0x33, 0x11, 0x87, 0xf8, 0x79, 0x3c,
  36711. 0x49, 0x7a, 0x8b, 0xce, 0xc1, 0x0a, 0x0e, 0xe1, 0xd5, 0x2a,
  36712. 0xac, 0xf0, 0x3a, 0x1d, 0x6a, 0x6a, 0xe5, 0xe1, 0x81, 0x70,
  36713. 0xad, 0xaf, 0x15, 0x4c, 0x2a, 0x70, 0x2a, 0x6b, 0x22, 0x0d,
  36714. 0x30, 0xe7, 0x56, 0xed, 0x2d, 0x4b, 0x85, 0x17, 0x49, 0x72,
  36715. 0x3a, 0x1b, 0x6f, 0x57, 0x1c, 0xf7, 0x72, 0x9e, 0x20, 0xdb,
  36716. 0x57, 0x1c, 0xfb, 0x36, 0x50, 0x52, 0xec, 0x5b, 0xd6, 0x6a,
  36717. 0x1b, 0xf8, 0x74, 0xad, 0xe6, 0x00, 0x74, 0x04, 0xc5, 0x99,
  36718. 0x83, 0xe4, 0x5a, 0x0c, 0xc3, 0xe8, 0x6d, 0x3a, 0xd7, 0x3c,
  36719. 0x3c, 0xc0, 0x1a, 0x28, 0xb3, 0x29, 0x7a, 0x10, 0x9e, 0x39,
  36720. 0x66, 0x5b, 0xc1, 0x38, 0xac, 0x21, 0x4e, 0xcd, 0x01, 0xf2,
  36721. 0xf6, 0x30, 0x2c, 0x2b, 0xb6, 0xbf, 0xf5, 0xea, 0x61, 0xaf,
  36722. 0x0c, 0xa6, 0x01, 0x11, 0x15, 0x19, 0x09, 0x8c, 0x7e, 0x69,
  36723. 0xdf, 0x3b, 0xea, 0xd3, 0x0a, 0x3a, 0xd7, 0xbd, 0xe1, 0x17,
  36724. 0xaf, 0x92, 0x3c, 0xf5, 0xfe, 0x35, 0xd6, 0xcf, 0x07, 0xa6,
  36725. 0xf7, 0xe9, 0xc1, 0x99, 0xed, 0x80, 0xe3, 0x12, 0xd5, 0x4b,
  36726. 0xb9, 0xdf, 0xaf, 0x4e, 0x52, 0xad, 0x8e, 0x66, 0x87, 0xe5,
  36727. 0x2c, 0xd0, 0x45, 0x70, 0xd9, 0x78, 0x8f, 0x4b, 0xf4, 0xe1,
  36728. 0xf1, 0x22, 0xf2, 0xe3, 0xed, 0x1f, 0xeb, 0xe9, 0x70, 0x31,
  36729. 0x4c, 0x65, 0x5f, 0x55, 0xee, 0x5d, 0xaa, 0x83, 0x87, 0x76,
  36730. 0xbe, 0x11, 0xae, 0xd7, 0xf2, 0xfb, 0x43, 0xe7, 0x17, 0x81,
  36731. 0x33, 0x15, 0x47, 0xa0, 0xf3, 0x8e, 0x84, 0x57, 0xff, 0x35,
  36732. 0x9e, 0x4a, 0x8a, 0xab, 0x50, 0x3a, 0x45, 0xe0, 0xc3, 0x73,
  36733. 0xca, 0x77, 0x61, 0x68, 0x38, 0xd0, 0xa3, 0x5f, 0x03, 0x8d,
  36734. 0x41, 0xc2, 0xd3, 0x4a, 0x17, 0xe0, 0xa8, 0xaa, 0x00, 0xf3,
  36735. 0xf2, 0x5b, 0xa8, 0xe1, 0x06, 0xa6, 0x2b, 0xdb, 0xe1, 0x74,
  36736. 0xbd, 0xc4, 0xd2, 0x2b, 0x55, 0x9a, 0xb0, 0xf8, 0x35, 0xd8,
  36737. 0x6b, 0xec, 0xdb, 0xc5, 0xf4, 0x6c, 0x40, 0x90, 0x6a, 0x68,
  36738. 0xc9, 0xb5, 0xcb, 0xbb, 0xd0, 0xb0, 0xbc, 0x9f, 0xb9, 0xaa,
  36739. 0x50, 0x14, 0x93, 0x3b, 0x9f, 0x25, 0xcb, 0x40, 0xb8, 0x08,
  36740. 0xcc, 0x13, 0xe5, 0xdc, 0x3f, 0x84, 0x96, 0xe0, 0x73, 0x7b,
  36741. 0x7d, 0x9e, 0x41, 0x92, 0x5d, 0xcc, 0xa4, 0xea, 0x4f, 0x93,
  36742. 0x0c, 0x40, 0x2e, 0x42, 0x8a, 0xe9, 0xb9, 0x12, 0x74, 0xbb,
  36743. 0x79, 0x7c, 0xb0, 0x37, 0x20, 0xb6, 0xaf, 0x43, 0x3a, 0x88,
  36744. 0x59, 0x7c, 0x68, 0x28, 0x5f, 0x98, 0xc2, 0xf0, 0x2a, 0xbc,
  36745. 0xa1, 0x61, 0x88, 0x1f, 0x43, 0xbc, 0x42, 0x8f, 0x43, 0xf3,
  36746. 0x7e, 0x16, 0x96, 0xfa, 0x92, 0x70, 0xaf, 0x3c, 0x9f, 0x4b,
  36747. 0xd9, 0x60, 0xe9, 0xf6, 0x2e, 0x84, 0xda, 0x88, 0x31, 0x34,
  36748. 0xa6, 0x85, 0x10, 0x05, 0xef, 0x40, 0xa8, 0xa5, 0x4f, 0x92,
  36749. 0x59, 0xf7, 0xe0, 0xc4, 0x2b, 0x12, 0x17, 0x71, 0xbe, 0x8c,
  36750. 0x4a, 0x02, 0xfe, 0x12, 0xb6, 0x3b, 0x85, 0x75, 0x37, 0xf3,
  36751. 0x73, 0x2d, 0x9c, 0x00, 0x5d, 0x80, 0xad, 0x20, 0x2f, 0x5a,
  36752. 0x0b, 0x17, 0x7e, 0x67, 0x72, 0x24, 0x5a, 0xb9, 0xf3, 0xb1,
  36753. 0x33, 0xa4, 0x57, 0x1d, 0x49, 0x72, 0x2c, 0x7f, 0x47, 0x15,
  36754. 0x07, 0xe0, 0x45, 0x14, 0xdd, 0x77, 0x86, 0x6d, 0x03, 0xbe,
  36755. 0x57, 0xd0, 0xaa, 0x18, 0xa6, 0xdd, 0x94, 0x18, 0x3f, 0x8a,
  36756. 0xf3, 0xb5, 0xd7, 0x5a, 0xec, 0xc8, 0x79, 0x7f, 0x51, 0x61,
  36757. 0x3c, 0x9b, 0xb2, 0x9b, 0xf3, 0xb4, 0x35, 0xd1, 0x38, 0xbf,
  36758. 0x37, 0xce, 0x54, 0xd1, 0xf8, 0xb6, 0x45, 0xeb, 0x52, 0x0d,
  36759. 0x9a, 0x09, 0x58, 0x0d, 0x2c, 0x0b, 0xb1, 0xf2, 0x30, 0x3a,
  36760. 0x95, 0xc1, 0x13, 0x91, 0xd2, 0x9f, 0x8d, 0x8d, 0xd0, 0x38,
  36761. 0x3e, 0x4c, 0xae, 0x4a, 0x55, 0xa7, 0x42, 0x11, 0x83, 0xc4,
  36762. 0x70, 0xf0, 0x2b, 0x68, 0x9e, 0x07, 0xad, 0xb7, 0x83, 0xc6,
  36763. 0x53, 0x3c, 0xfb, 0x0a, 0x5d, 0x24, 0xdc, 0xe1, 0x55, 0x72,
  36764. 0xcf, 0xce, 0x3e, 0xc8, 0xd0, 0x57, 0x8a, 0x82, 0x5e, 0x78,
  36765. 0x2b, 0x80, 0xc5, 0xb9, 0x09, 0x46, 0xf8, 0x90, 0x39, 0x52,
  36766. 0xa9, 0xce, 0x3f, 0x3d, 0x41, 0x3b, 0x28, 0x45, 0xa3, 0xb3,
  36767. 0x21, 0xc2, 0xcd, 0x14, 0x49, 0x41, 0x6c, 0x38, 0xda, 0x1b,
  36768. 0x5f, 0x16, 0x49, 0xf9, 0x65, 0x00, 0x4e, 0xb4, 0x20, 0x55,
  36769. 0x70, 0xe8, 0x58, 0x1a, 0x18, 0xbf, 0x41, 0xef, 0x31, 0xb1,
  36770. 0xe7, 0x8d, 0x89, 0xc1, 0x48, 0xe8, 0xf5, 0x57, 0x35, 0xfa,
  36771. 0xc1, 0x79, 0xee, 0x2c, 0xe8, 0x7d, 0xb6, 0x03, 0xcc, 0x66,
  36772. 0x09, 0x6f, 0x52, 0x84, 0x0a, 0x34, 0x18, 0x2c, 0x01, 0x45,
  36773. 0x81, 0x00, 0xe5, 0x5e, 0x8d, 0xae, 0x1c, 0x96, 0x8b, 0x45,
  36774. 0x73, 0x00, 0x0a, 0xb5, 0xcf, 0x8d, 0x0e, 0x35, 0x5d, 0x1a,
  36775. 0x0e, 0xbf, 0x64, 0x9a, 0x52, 0x20, 0x48, 0xc6, 0xb9, 0x40,
  36776. 0xd3, 0x2c, 0x52, 0xca, 0x93, 0xcf, 0xbb, 0x94, 0x06, 0xf3,
  36777. 0x97, 0xee, 0xcc, 0x5d, 0xa3, 0xea, 0xf8, 0x5a, 0x39, 0x77,
  36778. 0x34, 0xd7, 0xf6, 0x4e, 0xbe, 0x8a, 0x07, 0x5f, 0x51, 0x53,
  36779. 0xc5, 0x1b, 0x8c, 0x47, 0x8f, 0x34, 0x0e, 0x60, 0x0a, 0x90,
  36780. 0xe2, 0xda, 0x7b, 0xef, 0xd6, 0xf5, 0x5d, 0xe5, 0x32, 0x37,
  36781. 0x75, 0x99, 0x81, 0x4a, 0x2a, 0x78, 0x71, 0xdc, 0xf4, 0xe5,
  36782. 0xca, 0xd8, 0x6b, 0x3b, 0x90, 0x68, 0x2e, 0x93, 0xc5, 0x10,
  36783. 0x42, 0x5d, 0x38, 0x90, 0x32, 0x46, 0xea, 0x87, 0xe0, 0xbc,
  36784. 0xb8, 0x9a, 0x18, 0x20, 0x68, 0x85, 0x6d, 0x9b, 0xc9, 0x8f,
  36785. 0x9b, 0xd2, 0xbe, 0x15, 0x12, 0x68, 0xd0, 0xb0, 0x16, 0x5f,
  36786. 0xe2, 0x69, 0x1d, 0x04, 0x00, 0xfc, 0x63, 0x33, 0xcd, 0x1f,
  36787. 0x89, 0xcd, 0x52, 0xff, 0xec, 0x19, 0x69, 0x74, 0xa3, 0xce,
  36788. 0x4d, 0xab, 0x93, 0xe4, 0xc6, 0x13, 0x56, 0x27, 0xc9, 0x25,
  36789. 0x5a, 0x01, 0xb2, 0x36, 0x8b, 0x61, 0xe5, 0x8b, 0x98, 0xac,
  36790. 0xe4, 0x2a, 0xb6, 0x40, 0x9f, 0x42, 0xe4, 0x1b, 0x52, 0xf7,
  36791. 0xfd, 0xd8, 0x30, 0x07, 0x33, 0xf9, 0x47, 0xcb, 0x3c, 0xad,
  36792. 0x12, 0xc1, 0xcc, 0x29, 0x62, 0x49, 0x04, 0x0c, 0x23, 0x97,
  36793. 0x5a, 0xa4, 0x84, 0x67, 0xde, 0x5a, 0xe5, 0x36, 0xd2, 0x88,
  36794. 0xf1, 0xd4, 0xeb, 0x13, 0x81, 0x54, 0x51, 0x11, 0xe3, 0xba,
  36795. 0xbc, 0xee, 0xdd, 0x6c, 0xcd, 0xe6, 0xb4, 0xa1, 0x8b, 0x0b,
  36796. 0x66, 0xfb, 0x8e, 0x50, 0xa0, 0xda, 0x69, 0x8d, 0xcc, 0x2d,
  36797. 0xe4, 0x2c, 0xc4, 0x37, 0xdf, 0x61, 0xc0, 0x03, 0xbd, 0x8b,
  36798. 0x28, 0xca, 0xd2, 0x8c, 0x1c, 0xf1, 0xa4, 0x26, 0x69, 0xe5,
  36799. 0xcf, 0x45, 0xdb, 0x5a, 0x47, 0x79, 0xed, 0x9f, 0xf7, 0xd2,
  36800. 0xdb, 0xba, 0x46, 0x53, 0x4f, 0xce, 0xa8, 0xbe, 0x8f, 0x4a,
  36801. 0xd6, 0xdf, 0x2e, 0x06, 0xe6, 0x4c, 0x9a, 0xc1, 0xb6, 0x49,
  36802. 0xed, 0xc4, 0xeb, 0xaa, 0xa4, 0x29, 0x6d, 0xd4, 0xcc, 0x8c,
  36803. 0xb6, 0x40, 0x11, 0x39, 0x69, 0xf7, 0x75, 0xcd, 0xb1, 0x99,
  36804. 0x46, 0x4e, 0xde, 0xcb, 0xf6, 0x9d, 0x32, 0xf3, 0xc9, 0x47,
  36805. 0x47, 0x7a, 0xcb, 0xfb, 0xa3, 0x0c, 0x3b, 0xdf, 0xb7, 0xde,
  36806. 0xec, 0x99, 0xde, 0xb0, 0x26, 0x04, 0x34, 0xae, 0x6b, 0xfc,
  36807. 0x99, 0xbc, 0xde, 0xd5, 0xbe, 0xe7, 0xeb, 0xf9, 0xe7, 0xa6,
  36808. 0x01, 0x9a, 0x0c, 0x5e, 0x66, 0xe6, 0x53, 0xe4, 0xd1, 0x58,
  36809. 0xac, 0xda, 0x69, 0x77, 0x7b, 0x68, 0xd6, 0x30, 0x2a, 0x9c,
  36810. 0x6b, 0xbe, 0x9f, 0x3d, 0x71, 0xd6, 0x54, 0xcd, 0x59, 0x4e,
  36811. 0x1f, 0xe3, 0x83, 0x4e, 0xd1, 0x8e, 0xaf, 0x97, 0xa8, 0xe5,
  36812. 0xb6, 0x59, 0x77, 0xa8, 0x02, 0x20, 0xe4, 0xeb, 0x44, 0x71,
  36813. 0xbc, 0x07, 0x14, 0x79, 0x4f, 0x0c, 0x27, 0x06, 0x39, 0xcf,
  36814. 0x7c, 0xef, 0x2b, 0x9b, 0x5e, 0xc4, 0x6d, 0x79, 0x13, 0x00,
  36815. 0x43, 0x6f, 0x51, 0x77, 0xb5, 0xc3, 0x72, 0xad, 0x13, 0xa9,
  36816. 0xe5, 0x9a, 0x5b, 0x1a, 0x99, 0x74, 0xc0, 0x7a, 0xf9, 0xc5,
  36817. 0xb0, 0x58, 0x35, 0x1c, 0xa5, 0x51, 0xdb, 0xa1, 0x14, 0xcd,
  36818. 0x26, 0x71, 0xb1, 0xe7, 0xaa, 0x14, 0xa7, 0x46, 0x93, 0xd3,
  36819. 0x5c, 0x8c, 0x1a, 0x91, 0x77, 0x46, 0x2e, 0x15, 0xaa, 0x9e,
  36820. 0xf7, 0x2b, 0x79, 0x41, 0x76, 0xf7, 0x22, 0x53, 0x7d, 0x51,
  36821. 0xdb, 0x98, 0x3d, 0x5b, 0x78, 0x5f, 0xc3, 0xc9, 0x29, 0xa3,
  36822. 0xff, 0x75, 0x82, 0x06, 0x9a, 0x16, 0x5e, 0xa4, 0x79, 0x0d,
  36823. 0xd1, 0x6d, 0x08, 0xff, 0x43, 0xef, 0x9c, 0xf3, 0x1b, 0x7a,
  36824. 0x3f, 0x34, 0xbe, 0x19, 0x15, 0x06, 0x33, 0xdb, 0xa5, 0x71,
  36825. 0xcb, 0x5f, 0x6b, 0x8d, 0xbd, 0x5b, 0x32, 0x91, 0xb2, 0x37,
  36826. 0x3d, 0xb4, 0x40, 0x9e, 0x02, 0x9b, 0xb7, 0x68, 0x20, 0x58,
  36827. 0x5c, 0xab, 0xcb, 0xc8, 0x23, 0x2d, 0x77, 0xcc, 0x0b, 0xf6,
  36828. 0x78, 0x6b, 0x80, 0x06, 0x91, 0xa9, 0xfd, 0x7e, 0xfa, 0x25,
  36829. 0x98, 0x9f, 0xcc, 0x79, 0x0a, 0x1a, 0x54, 0x83, 0xac, 0x64,
  36830. 0x16, 0x90, 0xe5, 0xd9, 0xa7, 0xd7, 0x1b, 0x86, 0x0d, 0xe6,
  36831. 0xe6, 0x22, 0x2b, 0x1f, 0x44, 0x49, 0x98, 0x9c, 0x51, 0x6f,
  36832. 0xcf, 0x58, 0x4a, 0xfa, 0xfa, 0x84, 0x12, 0xa5, 0x10, 0xf4,
  36833. 0xca, 0xf0, 0x98, 0x2b, 0xc9, 0x03, 0x71, 0x37, 0xe7, 0xdc,
  36834. 0xc2, 0xb1, 0x4e, 0x64, 0xde, 0x4f, 0x46, 0x0d, 0x6b, 0x25,
  36835. 0x88, 0x5d, 0xd6, 0xff, 0x23, 0x46, 0x57, 0x36, 0x14, 0x18,
  36836. 0xa7, 0xcb, 0xb8, 0xbd, 0xf0, 0xc5, 0x37, 0x36, 0xee, 0xe1,
  36837. 0xed, 0x9f, 0x4d, 0xd4, 0x39, 0xe5, 0x92, 0xcf, 0x95, 0x4d,
  36838. 0x66, 0x36, 0x5d, 0xd0, 0xcc, 0x07, 0xcf, 0x15, 0x5a, 0xce,
  36839. 0x14, 0xb8, 0xda, 0x0d, 0x3d, 0x1b, 0x45, 0xc5, 0x2e, 0x34,
  36840. 0x43, 0x25, 0x02, 0x3a, 0xcd, 0x14, 0x45, 0xfb, 0x3e, 0xf9,
  36841. 0x88, 0x5d, 0x0d, 0x29, 0x31, 0xb9, 0xa1, 0xe6, 0x31, 0x18,
  36842. 0x52, 0x46, 0x3f, 0x22, 0x4f, 0x9f, 0x7a, 0x65, 0x36, 0x88,
  36843. 0xa3, 0x1c, 0x3e, 0x6f, 0x50, 0x7a, 0x36, 0xbe, 0x56, 0x7e,
  36844. 0x50, 0xcb, 0x7a, 0x10, 0xa0, 0xec, 0xf6, 0x82, 0xd6, 0x30,
  36845. 0x1c, 0xe8, 0x4c, 0x50, 0xf9, 0x3e, 0xdb, 0xac, 0xbe, 0x4f,
  36846. 0x90, 0xb1, 0xd5, 0x1b, 0x12, 0x95, 0xfb, 0xe8, 0x08, 0x64,
  36847. 0x56, 0x7c, 0x96, 0xcc, 0x90, 0xb1, 0xbc, 0xa0, 0xf5, 0x32,
  36848. 0x69, 0xb3, 0x5f, 0x27, 0x0f, 0xbe, 0xc9, 0xbd, 0xeb, 0xfa,
  36849. 0x4b, 0x5c, 0xc5, 0x99, 0x9e, 0x5a, 0x04, 0xcc, 0xd0, 0x4d,
  36850. 0x29, 0xe8, 0x84, 0x55, 0x8c, 0xd7, 0xc4, 0x06, 0x13, 0x4d,
  36851. 0x92, 0xe5, 0x98, 0x9c, 0x4c, 0xc1, 0xf7, 0xaf, 0x7b, 0xd5,
  36852. 0x2b, 0x92, 0x68, 0x68, 0x19, 0x70, 0x4c, 0x9e, 0x46, 0xb8,
  36853. 0x34, 0xeb, 0x01, 0x47, 0xbe, 0x59, 0xab, 0x0b, 0x22, 0x25,
  36854. 0xe7, 0x56, 0xa8, 0xb4, 0x93, 0x3c, 0xd5, 0x98, 0x9f, 0x61,
  36855. 0x2e, 0xfa, 0xcb, 0x5f, 0x5b, 0xd8, 0x09, 0x83, 0xe9, 0x40,
  36856. 0xe9, 0x0e, 0x42, 0xdd, 0x17, 0xd7, 0x6e, 0x19, 0x8d, 0x95,
  36857. 0x0a, 0x93,
  36858. };
  36859. WOLFSSL_SMALL_STACK_STATIC const byte ml_dsa_87_sig[] = {
  36860. 0x78, 0xed, 0x1a, 0x3f, 0x41, 0xab, 0xf8, 0x93, 0x80, 0xf0,
  36861. 0xc6, 0xbf, 0x4a, 0xde, 0xaf, 0x29, 0x93, 0xe5, 0x9a, 0xbf,
  36862. 0x38, 0x08, 0x18, 0x33, 0xca, 0x7d, 0x5e, 0x65, 0xa4, 0xd2,
  36863. 0xd7, 0x45, 0xe3, 0xe7, 0x58, 0xfb, 0x05, 0xab, 0x65, 0x57,
  36864. 0xac, 0x6f, 0xf5, 0x43, 0x28, 0x5f, 0x9c, 0x9a, 0x3e, 0x35,
  36865. 0x84, 0xe4, 0xef, 0xa5, 0x57, 0x17, 0xad, 0x51, 0x44, 0x70,
  36866. 0x09, 0x00, 0x81, 0xbe, 0xfe, 0x14, 0x01, 0xfe, 0x0c, 0x94,
  36867. 0xbe, 0xa9, 0x89, 0xfd, 0x47, 0xfc, 0xb9, 0xd8, 0x17, 0x4d,
  36868. 0xd8, 0x73, 0xd5, 0x50, 0x9f, 0x13, 0x6c, 0x07, 0x71, 0x47,
  36869. 0xaa, 0x3c, 0xc0, 0x64, 0x00, 0x19, 0x2e, 0x74, 0x51, 0x0e,
  36870. 0x0f, 0x25, 0x30, 0x7f, 0x13, 0x96, 0xc6, 0xc5, 0xbf, 0xd4,
  36871. 0x82, 0xd3, 0x0d, 0xd3, 0x65, 0x4c, 0x72, 0x67, 0xe2, 0x37,
  36872. 0x6b, 0x3c, 0x8e, 0xa3, 0x36, 0x84, 0xe9, 0xaa, 0xac, 0x7d,
  36873. 0xf3, 0xac, 0xfc, 0x01, 0x50, 0x87, 0x88, 0xf6, 0xbf, 0x84,
  36874. 0xc3, 0xa0, 0x23, 0xe4, 0xe8, 0x01, 0x38, 0x39, 0x30, 0x8a,
  36875. 0xf3, 0xba, 0x92, 0x62, 0x37, 0xd7, 0x20, 0xd7, 0xf7, 0x41,
  36876. 0xff, 0xae, 0x81, 0x02, 0x29, 0x2a, 0x66, 0x8b, 0x20, 0xbe,
  36877. 0x61, 0x8d, 0xfb, 0x7c, 0x70, 0x14, 0xad, 0xf4, 0x94, 0x8c,
  36878. 0xee, 0x64, 0x3b, 0x9f, 0xe1, 0x6e, 0x68, 0x17, 0x07, 0xb8,
  36879. 0xfc, 0x99, 0xdc, 0xde, 0x69, 0x58, 0x8c, 0x97, 0x7d, 0xb3,
  36880. 0x2c, 0x9e, 0x90, 0x33, 0x2e, 0x7b, 0xbf, 0xf8, 0x6f, 0xf8,
  36881. 0x12, 0x64, 0xda, 0xc0, 0xfb, 0x30, 0xe6, 0xbf, 0x7b, 0x9a,
  36882. 0xde, 0xb5, 0xac, 0x9d, 0x6b, 0xcb, 0xe1, 0x0d, 0xf1, 0xbb,
  36883. 0xf3, 0x97, 0xc5, 0x08, 0xd3, 0x3e, 0xe3, 0xa4, 0xeb, 0x6f,
  36884. 0x6b, 0x62, 0x61, 0xc5, 0x0b, 0xa8, 0x02, 0xc2, 0xf1, 0xbe,
  36885. 0xbb, 0x93, 0x13, 0xa5, 0x8d, 0x7b, 0x5a, 0x6d, 0x1f, 0x28,
  36886. 0xbc, 0x35, 0xd8, 0xe8, 0xcf, 0x80, 0x8b, 0x4b, 0x02, 0x80,
  36887. 0x3b, 0xdc, 0x00, 0xce, 0x88, 0xb0, 0x62, 0x35, 0x7d, 0x51,
  36888. 0x7f, 0x5c, 0xb2, 0x23, 0x85, 0x47, 0x7e, 0x73, 0x88, 0x65,
  36889. 0xfd, 0x0d, 0x47, 0x33, 0xef, 0xb9, 0x75, 0x05, 0x86, 0x5d,
  36890. 0xd3, 0x98, 0xa6, 0x91, 0xe6, 0x8c, 0xe2, 0x71, 0x7a, 0x95,
  36891. 0xe0, 0x8c, 0x54, 0x4b, 0x68, 0x4d, 0x5a, 0xec, 0xad, 0xae,
  36892. 0x54, 0x4e, 0x3b, 0x0e, 0xcd, 0x70, 0xe6, 0x81, 0xbf, 0xf4,
  36893. 0x86, 0xab, 0xfe, 0xd8, 0xed, 0x69, 0xdd, 0x0f, 0x75, 0x8f,
  36894. 0x8e, 0xcd, 0x72, 0x40, 0x21, 0xee, 0x80, 0x6f, 0x9e, 0xa0,
  36895. 0x80, 0xf7, 0xf6, 0xa2, 0xf5, 0x04, 0x82, 0xea, 0xb6, 0xb1,
  36896. 0xa3, 0xfe, 0xa2, 0x2d, 0x83, 0xc7, 0x01, 0x4b, 0x27, 0x19,
  36897. 0x6a, 0x31, 0x04, 0x70, 0xce, 0x75, 0x22, 0x4b, 0x7a, 0x21,
  36898. 0x29, 0xfd, 0xe9, 0xcb, 0xbb, 0xca, 0x95, 0x0a, 0xd8, 0xcd,
  36899. 0x20, 0x2a, 0xb7, 0xbe, 0xdf, 0x2f, 0x0f, 0xfa, 0xf1, 0xc0,
  36900. 0x39, 0xf3, 0x74, 0x22, 0x05, 0x33, 0xca, 0x2a, 0x9c, 0x9f,
  36901. 0x06, 0x71, 0x90, 0x1e, 0x74, 0x4b, 0xbe, 0x9a, 0xc7, 0x1e,
  36902. 0x37, 0x9b, 0x96, 0x19, 0xfd, 0xa0, 0x61, 0x87, 0x93, 0xab,
  36903. 0x75, 0x79, 0xac, 0x2f, 0x83, 0xe1, 0x8c, 0x70, 0x54, 0x70,
  36904. 0x01, 0x93, 0xce, 0x76, 0x7a, 0x08, 0xe7, 0x75, 0xfb, 0x5e,
  36905. 0xa4, 0xcc, 0xd6, 0xeb, 0x90, 0xe2, 0x57, 0x07, 0x53, 0x88,
  36906. 0x8f, 0x7f, 0x29, 0x39, 0x80, 0xc4, 0x7f, 0x70, 0x6f, 0xff,
  36907. 0x44, 0x25, 0x2b, 0x9e, 0xa1, 0xbb, 0xda, 0x43, 0x53, 0x14,
  36908. 0xf8, 0x97, 0x08, 0xa4, 0xaf, 0xa0, 0xa5, 0x0c, 0xfa, 0xcc,
  36909. 0xba, 0xcd, 0x4f, 0xd3, 0x90, 0x28, 0x02, 0x25, 0xbe, 0xc6,
  36910. 0x35, 0x66, 0x99, 0xb0, 0x69, 0x46, 0xe5, 0xbf, 0x7e, 0x4f,
  36911. 0x53, 0x11, 0x1f, 0xa5, 0x2c, 0x9b, 0xd1, 0x70, 0x90, 0x34,
  36912. 0x66, 0xaa, 0x9f, 0xa8, 0x02, 0x3a, 0x05, 0x2b, 0x0a, 0xd0,
  36913. 0x72, 0x5d, 0x01, 0x7b, 0x02, 0xce, 0x18, 0xb9, 0x63, 0xd1,
  36914. 0x7d, 0xd2, 0x34, 0xa3, 0x2d, 0xaa, 0x78, 0xf0, 0x30, 0x6e,
  36915. 0x59, 0xe3, 0xf1, 0x1e, 0xf1, 0x33, 0x41, 0xde, 0xc4, 0x4e,
  36916. 0x88, 0x61, 0xc3, 0xb4, 0x6b, 0x21, 0x5d, 0xcc, 0x69, 0x44,
  36917. 0xf3, 0xb0, 0x84, 0x54, 0x2a, 0x23, 0x22, 0xa2, 0xc4, 0xba,
  36918. 0xad, 0x00, 0x57, 0x5b, 0xdf, 0xa0, 0xf7, 0x1c, 0x00, 0xc3,
  36919. 0x23, 0x93, 0xc0, 0x2f, 0x3b, 0x9d, 0x6e, 0x8c, 0x38, 0xa6,
  36920. 0x5e, 0xd8, 0x98, 0x7a, 0x6c, 0x90, 0xd5, 0x40, 0x3f, 0x8c,
  36921. 0xc3, 0xf0, 0x92, 0x66, 0xc4, 0xe5, 0xa8, 0x42, 0x25, 0x4c,
  36922. 0x56, 0x42, 0x37, 0x9a, 0xa4, 0x1d, 0xf5, 0xb0, 0xe3, 0x8a,
  36923. 0x9c, 0x57, 0x52, 0x63, 0xdc, 0xd9, 0xb0, 0xbf, 0xc3, 0xfc,
  36924. 0xfc, 0x6c, 0xab, 0x41, 0xae, 0xec, 0xc7, 0x40, 0x80, 0xb6,
  36925. 0x0b, 0x3c, 0xa9, 0xf5, 0x4f, 0x2d, 0xf6, 0x72, 0xe3, 0xba,
  36926. 0x13, 0x2c, 0x73, 0x61, 0x98, 0x66, 0x6f, 0x03, 0x88, 0x3b,
  36927. 0xe6, 0x95, 0x43, 0x33, 0x3b, 0xfe, 0xfd, 0x63, 0x8c, 0x00,
  36928. 0x8a, 0x67, 0x1c, 0x46, 0x0e, 0x0b, 0x51, 0x26, 0x79, 0x4f,
  36929. 0x7b, 0xb1, 0x36, 0x34, 0x52, 0x41, 0x7e, 0x74, 0xbb, 0x71,
  36930. 0x52, 0x8f, 0xcc, 0xf2, 0x99, 0x24, 0x3f, 0x18, 0xe6, 0xcf,
  36931. 0xdf, 0x6b, 0xfe, 0x77, 0xfa, 0xa8, 0x3f, 0xe3, 0x6b, 0xb7,
  36932. 0x32, 0x30, 0x8e, 0x16, 0x08, 0x59, 0x66, 0xdf, 0x95, 0x75,
  36933. 0x7d, 0xa3, 0x80, 0xf0, 0x0c, 0x1a, 0xa8, 0xe7, 0x87, 0x2f,
  36934. 0xe3, 0x39, 0x11, 0x82, 0x00, 0x3e, 0xe5, 0x71, 0x05, 0x7d,
  36935. 0x0c, 0x90, 0xae, 0xbc, 0xbf, 0xe0, 0x4b, 0x8f, 0x91, 0x85,
  36936. 0x1d, 0x0a, 0xa2, 0x36, 0x66, 0x18, 0x78, 0xd0, 0x0a, 0xa0,
  36937. 0xaf, 0x0f, 0x1c, 0x01, 0xdb, 0xb2, 0x21, 0x96, 0x25, 0xf7,
  36938. 0x9e, 0x3a, 0x9e, 0xc3, 0xe8, 0x92, 0x34, 0xaf, 0x7e, 0x3b,
  36939. 0x5f, 0xd9, 0x23, 0x97, 0x09, 0xf1, 0x87, 0x31, 0x3a, 0x94,
  36940. 0xc8, 0x9b, 0x52, 0xf4, 0x57, 0x54, 0x7b, 0x3e, 0x50, 0xd3,
  36941. 0x75, 0x2a, 0xba, 0x97, 0xd7, 0xec, 0x95, 0x6c, 0x35, 0x63,
  36942. 0xa4, 0xa1, 0x8f, 0xf5, 0xcc, 0xbe, 0x42, 0x65, 0x4e, 0x69,
  36943. 0x35, 0x55, 0xa5, 0x3e, 0xc4, 0xf0, 0xde, 0x60, 0x54, 0xdf,
  36944. 0xbb, 0x83, 0xad, 0xdf, 0xa5, 0x24, 0x8f, 0xbe, 0x0b, 0x16,
  36945. 0xfc, 0xf2, 0x64, 0xd5, 0x79, 0x68, 0xf3, 0x91, 0x81, 0x2a,
  36946. 0xd7, 0x1c, 0xc0, 0xdd, 0xe6, 0xb6, 0xb3, 0xa2, 0x4f, 0xc0,
  36947. 0x6d, 0x77, 0x02, 0xee, 0x43, 0xd6, 0x5e, 0x82, 0x66, 0x7f,
  36948. 0xb4, 0xe6, 0x5c, 0xff, 0x87, 0x1e, 0x1d, 0x6f, 0x1d, 0x96,
  36949. 0x6d, 0xbd, 0x90, 0x57, 0x65, 0xc2, 0x01, 0x35, 0xfa, 0x9a,
  36950. 0xc6, 0xe0, 0x4e, 0x2c, 0x4b, 0x16, 0xfa, 0x0d, 0x38, 0x87,
  36951. 0x39, 0x2c, 0x2b, 0x48, 0x14, 0x92, 0x3d, 0x83, 0x00, 0xa9,
  36952. 0x1a, 0x3d, 0x4d, 0x30, 0x23, 0x48, 0xcd, 0xd5, 0xcd, 0x01,
  36953. 0xb1, 0x45, 0x85, 0xcc, 0x66, 0x47, 0x1d, 0x63, 0x3d, 0x70,
  36954. 0xb8, 0x0c, 0xfd, 0xe3, 0xb2, 0x0f, 0x64, 0x6e, 0xb9, 0x2b,
  36955. 0xe5, 0xb0, 0x4d, 0x44, 0x4d, 0x66, 0x1a, 0xfa, 0x49, 0xbb,
  36956. 0xc3, 0xb8, 0xad, 0x64, 0x23, 0x7e, 0x71, 0x9f, 0x59, 0xec,
  36957. 0x25, 0xa8, 0x5e, 0x11, 0xd6, 0x6e, 0xc9, 0x09, 0xe7, 0xb9,
  36958. 0x6a, 0x63, 0x91, 0xaa, 0x5d, 0xd2, 0x8c, 0x91, 0xe8, 0x8d,
  36959. 0x35, 0x6d, 0x10, 0xf6, 0xfc, 0x6a, 0x3c, 0x77, 0x90, 0xf8,
  36960. 0x2a, 0x49, 0x13, 0x7f, 0xdb, 0xf5, 0x0c, 0xe9, 0xc8, 0x57,
  36961. 0xc6, 0xfd, 0x26, 0x8d, 0x79, 0xb5, 0xdd, 0x47, 0x74, 0x6e,
  36962. 0xe8, 0x8f, 0x50, 0xf5, 0xa7, 0x9e, 0xd1, 0x74, 0x10, 0xbb,
  36963. 0xf4, 0x8f, 0x8f, 0x0d, 0xcd, 0x1f, 0xf6, 0x59, 0xb8, 0x6c,
  36964. 0xd2, 0x37, 0x83, 0x28, 0xb2, 0x36, 0xc1, 0x39, 0x5b, 0xde,
  36965. 0x59, 0xee, 0x77, 0xa2, 0x6e, 0x67, 0xc6, 0xea, 0x1d, 0x2b,
  36966. 0x41, 0x8f, 0x6f, 0x96, 0x94, 0x1b, 0x5d, 0xab, 0x30, 0x53,
  36967. 0x1e, 0xf8, 0x17, 0x06, 0xea, 0xcc, 0x98, 0xa8, 0xdf, 0x81,
  36968. 0xe1, 0x80, 0xb7, 0xad, 0x69, 0xcb, 0x8f, 0x81, 0x1e, 0x76,
  36969. 0x75, 0x3c, 0x11, 0x9b, 0x38, 0x95, 0xa7, 0x87, 0x1f, 0xd9,
  36970. 0x76, 0x82, 0x21, 0x13, 0x25, 0x20, 0x42, 0xd3, 0x8c, 0xd9,
  36971. 0x1c, 0x64, 0xed, 0xe9, 0x55, 0xb5, 0x29, 0x98, 0x85, 0x7c,
  36972. 0x01, 0x94, 0xaa, 0xdd, 0x8c, 0x78, 0x08, 0x99, 0x99, 0x5a,
  36973. 0xf6, 0x61, 0x4c, 0xe0, 0x99, 0xf8, 0x15, 0x74, 0x2e, 0x0d,
  36974. 0x14, 0x89, 0x11, 0x84, 0xcd, 0x78, 0x0c, 0x6b, 0x48, 0xde,
  36975. 0xb4, 0xd6, 0x05, 0xbd, 0x99, 0x58, 0xb7, 0xe5, 0xc5, 0x7a,
  36976. 0x43, 0x18, 0x55, 0x33, 0x16, 0x2b, 0xfa, 0x27, 0xf5, 0xbb,
  36977. 0xaa, 0x52, 0xb5, 0x28, 0x5c, 0xfe, 0x61, 0x7f, 0x7a, 0x70,
  36978. 0xc2, 0x32, 0x4b, 0x05, 0x8d, 0x7b, 0x4d, 0x22, 0x57, 0x25,
  36979. 0x40, 0x46, 0x7c, 0xad, 0x2f, 0x8a, 0xc8, 0x16, 0xd6, 0xac,
  36980. 0x4e, 0xe3, 0xe3, 0x29, 0xe4, 0xe8, 0x00, 0x2b, 0xc9, 0xe3,
  36981. 0x3a, 0x6f, 0x66, 0xf1, 0x37, 0x37, 0x52, 0x88, 0x77, 0xf6,
  36982. 0xbd, 0x59, 0x5f, 0xf8, 0x11, 0x46, 0x7b, 0x12, 0x88, 0x2f,
  36983. 0x4b, 0x0d, 0x16, 0x89, 0x3e, 0x2a, 0x56, 0x58, 0xa8, 0x1c,
  36984. 0xee, 0x23, 0xd5, 0x66, 0x86, 0x5f, 0x59, 0x55, 0xac, 0x07,
  36985. 0xfd, 0xda, 0x6b, 0xf1, 0xc7, 0x01, 0x19, 0xdb, 0xff, 0x63,
  36986. 0x6f, 0x27, 0xdb, 0xa1, 0xc7, 0xe9, 0xe0, 0xdb, 0xe4, 0x9a,
  36987. 0xce, 0xf5, 0xac, 0x68, 0xab, 0x59, 0x0c, 0x83, 0xa3, 0x1c,
  36988. 0x2a, 0x86, 0x55, 0xe2, 0xaa, 0xa1, 0xb3, 0xed, 0xc2, 0x2d,
  36989. 0x43, 0xc5, 0x13, 0x68, 0xe4, 0x83, 0x3e, 0xd5, 0x7f, 0xf7,
  36990. 0xd5, 0xd0, 0x60, 0xd3, 0x70, 0x7f, 0x88, 0xaa, 0xca, 0x74,
  36991. 0xcc, 0x50, 0x8d, 0x55, 0x9c, 0xfe, 0x4a, 0xc6, 0xc9, 0x36,
  36992. 0xf7, 0x27, 0x26, 0x64, 0xd3, 0x6c, 0xdb, 0x16, 0x31, 0x81,
  36993. 0xe9, 0xce, 0x73, 0x60, 0x61, 0x9c, 0x0f, 0xb5, 0x6e, 0x68,
  36994. 0xbc, 0xb1, 0x9e, 0x9f, 0xcd, 0x6c, 0x27, 0x31, 0x2d, 0x40,
  36995. 0x36, 0xce, 0x91, 0xee, 0x47, 0xdc, 0xa0, 0x4f, 0xd7, 0x14,
  36996. 0x4f, 0x93, 0x00, 0xc4, 0x34, 0xca, 0xd4, 0x42, 0x21, 0x90,
  36997. 0xf6, 0x9d, 0xea, 0x45, 0x15, 0xfe, 0x2d, 0xd6, 0xab, 0xc2,
  36998. 0x36, 0x47, 0xc0, 0x5b, 0xd2, 0xae, 0x53, 0x33, 0xb0, 0x2d,
  36999. 0x29, 0xa3, 0x14, 0xda, 0xa4, 0x48, 0xc1, 0x57, 0x0c, 0xdc,
  37000. 0x72, 0x4a, 0xd0, 0xf5, 0x5b, 0x9a, 0x57, 0x1d, 0x06, 0xc8,
  37001. 0x0f, 0xc7, 0x5b, 0x70, 0xbb, 0x27, 0xf4, 0xe2, 0xf4, 0xf3,
  37002. 0x3c, 0xdc, 0xba, 0x43, 0xc4, 0x4e, 0xe2, 0x96, 0xd4, 0x6c,
  37003. 0x33, 0x3e, 0xbf, 0x85, 0xf7, 0x3c, 0x1d, 0x46, 0x59, 0x4e,
  37004. 0xa1, 0xa7, 0xa3, 0x76, 0x55, 0x8a, 0x72, 0x83, 0xd0, 0x45,
  37005. 0x86, 0x38, 0xa5, 0x4d, 0xc8, 0x62, 0xe4, 0x8a, 0xd5, 0x8e,
  37006. 0xb7, 0x4c, 0x6e, 0xaf, 0xa4, 0xbe, 0x88, 0x87, 0x77, 0xd1,
  37007. 0x7b, 0xb2, 0x1d, 0xe0, 0x1e, 0x53, 0x30, 0x31, 0x15, 0x6c,
  37008. 0x10, 0x81, 0x03, 0x55, 0xa7, 0x69, 0xb6, 0xa5, 0x48, 0xf4,
  37009. 0xb2, 0x3b, 0x76, 0x8b, 0x2e, 0x42, 0xa6, 0xaa, 0x7e, 0x66,
  37010. 0x57, 0xc2, 0x11, 0xc5, 0x2c, 0x7d, 0x96, 0xdf, 0xe3, 0x58,
  37011. 0x12, 0x98, 0x18, 0x0d, 0x87, 0xbd, 0x64, 0xbd, 0xfe, 0x6d,
  37012. 0xad, 0x6d, 0x1e, 0xf6, 0x34, 0x01, 0xb5, 0x56, 0xe8, 0x6a,
  37013. 0xb3, 0x8c, 0x70, 0x84, 0x36, 0x17, 0xd6, 0x4b, 0xaa, 0x57,
  37014. 0xab, 0xb3, 0x45, 0x30, 0x36, 0x10, 0xd4, 0xee, 0x8a, 0xc9,
  37015. 0x29, 0xd1, 0x92, 0x9b, 0xe2, 0x7c, 0x12, 0xd1, 0x29, 0x62,
  37016. 0x41, 0x69, 0xae, 0x3a, 0x50, 0xcc, 0x89, 0x50, 0x2e, 0xe6,
  37017. 0x07, 0xf8, 0x9c, 0x98, 0x80, 0xd5, 0xa3, 0xc8, 0x74, 0xfb,
  37018. 0xfc, 0x91, 0x16, 0x02, 0xdc, 0xf0, 0x42, 0x49, 0xbc, 0xc9,
  37019. 0x2f, 0x7f, 0x8d, 0x93, 0xf7, 0xf0, 0x74, 0xb7, 0xd1, 0x55,
  37020. 0xfc, 0x79, 0x03, 0x37, 0xfb, 0xf6, 0x7d, 0x2f, 0x2d, 0xf8,
  37021. 0x6b, 0xc5, 0xf9, 0x66, 0x38, 0xf5, 0xfd, 0x64, 0xc6, 0x08,
  37022. 0x99, 0xb3, 0x25, 0xad, 0xf4, 0xfd, 0x69, 0x2f, 0xf1, 0x18,
  37023. 0x46, 0xd6, 0x5c, 0x1a, 0x37, 0xcd, 0xee, 0xa3, 0xbf, 0x0f,
  37024. 0x57, 0x5c, 0xc3, 0x97, 0x94, 0x84, 0x89, 0xbe, 0x00, 0xf6,
  37025. 0x40, 0xe9, 0x5a, 0x52, 0xaf, 0x3a, 0x5b, 0xf4, 0x56, 0xb0,
  37026. 0x04, 0x49, 0xc6, 0x32, 0x8c, 0xa1, 0x0a, 0xd8, 0x88, 0xa1,
  37027. 0xc3, 0xb7, 0x8b, 0x96, 0xc3, 0x39, 0x51, 0x50, 0x83, 0xa6,
  37028. 0xf0, 0x6d, 0xe7, 0x6e, 0x20, 0xff, 0x9d, 0xac, 0x03, 0x57,
  37029. 0xbc, 0xcb, 0x6a, 0x19, 0xa7, 0xc5, 0xd2, 0x44, 0x4f, 0x17,
  37030. 0x1e, 0x9a, 0x8d, 0x97, 0x25, 0x55, 0x52, 0x49, 0xe2, 0x48,
  37031. 0xae, 0x4b, 0x3f, 0x94, 0x5a, 0xb2, 0x2d, 0x40, 0xd9, 0x85,
  37032. 0xef, 0x03, 0xa0, 0xd3, 0x66, 0x9a, 0x8f, 0x7b, 0xc0, 0x8d,
  37033. 0x54, 0x95, 0x42, 0x49, 0xeb, 0x15, 0x00, 0xf3, 0x6d, 0x6f,
  37034. 0x40, 0xf2, 0x8b, 0xc1, 0x50, 0xa6, 0x22, 0x3b, 0xd6, 0x88,
  37035. 0xa1, 0xf7, 0xb0, 0x1f, 0xcd, 0x20, 0x4e, 0x5b, 0xad, 0x66,
  37036. 0x4a, 0xda, 0x40, 0xee, 0x4c, 0x4c, 0x3e, 0xa7, 0x75, 0x51,
  37037. 0x90, 0xba, 0xee, 0x59, 0xbc, 0xe3, 0xcd, 0x4d, 0xb9, 0x57,
  37038. 0xb7, 0xf8, 0xc1, 0xb9, 0x8d, 0x0f, 0x58, 0x2c, 0x4c, 0x98,
  37039. 0xa6, 0x9c, 0xd9, 0x0e, 0x25, 0x4f, 0xea, 0x4c, 0x15, 0x0b,
  37040. 0x89, 0xe4, 0xac, 0xa1, 0x5a, 0xa1, 0xfd, 0x5b, 0xc6, 0xfe,
  37041. 0xf0, 0xf1, 0x4c, 0xa7, 0x60, 0xbc, 0xc3, 0xa5, 0x80, 0x00,
  37042. 0x3b, 0x3f, 0x22, 0x38, 0x60, 0x40, 0x76, 0x52, 0x83, 0x32,
  37043. 0xee, 0x20, 0x6a, 0xf9, 0x1e, 0x6b, 0x99, 0x52, 0xe7, 0x04,
  37044. 0xdc, 0x5a, 0x9d, 0x77, 0x8a, 0xdd, 0x9b, 0x53, 0x19, 0xff,
  37045. 0x69, 0x8c, 0xbc, 0xc6, 0xe0, 0x79, 0x0d, 0x3d, 0x3d, 0x54,
  37046. 0x5b, 0xe0, 0x47, 0x5b, 0x71, 0x05, 0x98, 0x8f, 0xbb, 0x65,
  37047. 0xe1, 0x31, 0x9a, 0xc8, 0x1e, 0x7a, 0x4a, 0xf8, 0xcb, 0x17,
  37048. 0xd1, 0x83, 0x58, 0xb1, 0xc0, 0xe4, 0xb1, 0x85, 0xca, 0xa5,
  37049. 0xf8, 0x0e, 0xd1, 0x0c, 0xe8, 0x71, 0xc3, 0xfa, 0xbf, 0x1d,
  37050. 0xd6, 0x98, 0x03, 0xed, 0x77, 0x3b, 0x55, 0xaf, 0x69, 0x72,
  37051. 0x6b, 0x42, 0x31, 0x98, 0x95, 0xd5, 0x79, 0xa5, 0x4c, 0x51,
  37052. 0xcf, 0x02, 0x65, 0x93, 0xf2, 0x71, 0xdc, 0xde, 0x9a, 0xa3,
  37053. 0x86, 0xa7, 0xea, 0xcf, 0xd7, 0xe5, 0x00, 0xde, 0x40, 0x02,
  37054. 0xcd, 0x6b, 0x46, 0x0b, 0xbb, 0xbf, 0x77, 0x5f, 0x9d, 0x7c,
  37055. 0xa4, 0x7f, 0x7c, 0x8a, 0xba, 0xd6, 0x99, 0xc5, 0xaa, 0x06,
  37056. 0x36, 0xe1, 0x7e, 0x9c, 0x6f, 0x28, 0xd4, 0x6e, 0x1d, 0x5b,
  37057. 0xdd, 0x01, 0x24, 0xbd, 0x6c, 0x5d, 0x87, 0x3c, 0xc1, 0xf6,
  37058. 0x93, 0x37, 0xe2, 0x3b, 0x70, 0xc4, 0xd8, 0x10, 0x0e, 0x44,
  37059. 0x37, 0x00, 0xe3, 0x07, 0xbd, 0x67, 0xd3, 0x9d, 0xe6, 0xe7,
  37060. 0x48, 0x1b, 0xe0, 0x79, 0xb3, 0x30, 0x91, 0x89, 0x0f, 0x89,
  37061. 0x77, 0xfa, 0x13, 0x85, 0xd0, 0x32, 0xbd, 0xc1, 0x9e, 0x52,
  37062. 0x04, 0x80, 0x54, 0xb1, 0x08, 0x39, 0x20, 0xda, 0x3e, 0xf1,
  37063. 0xd9, 0x15, 0x74, 0x55, 0x06, 0xfc, 0x4d, 0x85, 0xd4, 0x98,
  37064. 0x02, 0x64, 0x10, 0x86, 0xd7, 0xcd, 0x01, 0x0d, 0x85, 0xa0,
  37065. 0x78, 0xb0, 0x58, 0x99, 0x7b, 0xdf, 0xe4, 0x8c, 0x3f, 0xab,
  37066. 0xc0, 0xbc, 0xa5, 0x30, 0x28, 0xe1, 0x4e, 0x02, 0x98, 0xab,
  37067. 0x03, 0xf3, 0x21, 0xe7, 0xa7, 0xe7, 0xc3, 0x5f, 0x98, 0xc0,
  37068. 0x83, 0x02, 0xe8, 0x8a, 0x30, 0x75, 0x95, 0xcf, 0x77, 0x83,
  37069. 0xfb, 0x32, 0x5a, 0xf9, 0x13, 0xed, 0xdb, 0xda, 0xc3, 0x84,
  37070. 0x4b, 0x8f, 0x1a, 0xf0, 0xad, 0x8e, 0xcf, 0xe3, 0xa7, 0x2b,
  37071. 0xb5, 0x44, 0x75, 0xd6, 0xda, 0x33, 0x81, 0x22, 0xa7, 0x6a,
  37072. 0xbd, 0x21, 0x64, 0x85, 0xfa, 0x65, 0x8e, 0xc4, 0x58, 0xec,
  37073. 0xc4, 0x18, 0x90, 0xa3, 0xcc, 0x2e, 0xaa, 0xa2, 0x2e, 0x46,
  37074. 0x7a, 0x4a, 0x35, 0xbf, 0x58, 0x78, 0x2b, 0x1e, 0x72, 0xe5,
  37075. 0x80, 0xc9, 0xe0, 0x9e, 0x43, 0x01, 0xcc, 0xe1, 0x0c, 0x00,
  37076. 0xe9, 0xc1, 0xa5, 0x1a, 0x9b, 0x4e, 0x6e, 0x34, 0x32, 0xfd,
  37077. 0x86, 0xb7, 0xae, 0xc3, 0x6e, 0x69, 0x04, 0xf6, 0x6a, 0x92,
  37078. 0x78, 0xb1, 0x1f, 0x9d, 0x5e, 0x0c, 0xf9, 0xc4, 0x1a, 0xf6,
  37079. 0xb4, 0x8a, 0x63, 0xb5, 0x87, 0x5b, 0xfb, 0x50, 0xbf, 0xd5,
  37080. 0x17, 0x97, 0x8e, 0x55, 0x1c, 0xfe, 0x82, 0xf6, 0xa7, 0x9c,
  37081. 0x0b, 0xc9, 0x0a, 0xf6, 0x7f, 0x70, 0xd1, 0x00, 0xed, 0x1c,
  37082. 0x6c, 0x3a, 0x95, 0xed, 0x61, 0xa4, 0xd6, 0x57, 0xfb, 0x57,
  37083. 0xf8, 0x9b, 0x4c, 0xce, 0x50, 0x26, 0x5c, 0x19, 0xd2, 0xa7,
  37084. 0xd6, 0xe8, 0x3c, 0x29, 0x34, 0xfb, 0x26, 0x7f, 0xc5, 0x78,
  37085. 0xbf, 0xfe, 0xb6, 0x2a, 0x5a, 0x62, 0x8e, 0x31, 0x9b, 0x57,
  37086. 0xa4, 0xe7, 0x4d, 0x3d, 0x18, 0x05, 0xf0, 0x94, 0xbb, 0x04,
  37087. 0xfa, 0x0a, 0x92, 0xf4, 0xc6, 0x7f, 0x16, 0xa2, 0x31, 0xed,
  37088. 0xc1, 0xb4, 0x62, 0x54, 0x3a, 0x23, 0x12, 0x6a, 0x76, 0xcc,
  37089. 0x8c, 0x91, 0x89, 0x58, 0x8c, 0x20, 0x23, 0xd9, 0xaa, 0x0d,
  37090. 0x80, 0xbe, 0xb9, 0xb4, 0x40, 0x1e, 0xff, 0xa9, 0xf7, 0x71,
  37091. 0x0a, 0xa0, 0x0a, 0xdf, 0x11, 0x0b, 0x66, 0x3f, 0xf2, 0x4d,
  37092. 0x5d, 0x39, 0x7c, 0x77, 0xe1, 0xb1, 0x09, 0xa1, 0x6b, 0x2e,
  37093. 0x30, 0x43, 0x33, 0x80, 0x6e, 0x6a, 0x1d, 0x47, 0xd9, 0xd6,
  37094. 0xac, 0xdc, 0x3f, 0x16, 0xb1, 0x58, 0x11, 0x9f, 0x67, 0xd7,
  37095. 0x15, 0x45, 0xd8, 0xc3, 0x69, 0x24, 0x8d, 0xac, 0xff, 0xc3,
  37096. 0x43, 0xfd, 0x24, 0xaf, 0xf1, 0xc8, 0x3a, 0xc7, 0xd6, 0x1f,
  37097. 0x56, 0x26, 0x16, 0xe6, 0x30, 0xcd, 0x6e, 0x0a, 0x63, 0x2a,
  37098. 0x7b, 0x86, 0xd7, 0x65, 0x39, 0x45, 0x7c, 0xe6, 0xa0, 0xe6,
  37099. 0x38, 0xed, 0x54, 0x84, 0x00, 0x4d, 0x8e, 0xc2, 0xba, 0x56,
  37100. 0x9b, 0xf3, 0xe1, 0xe8, 0x7d, 0xfe, 0x47, 0xf0, 0x58, 0xe7,
  37101. 0x59, 0x60, 0x97, 0x2e, 0x57, 0x1a, 0x09, 0x1f, 0x8b, 0x2b,
  37102. 0x0b, 0x47, 0x75, 0xc0, 0xb3, 0x79, 0xce, 0x10, 0x47, 0x6d,
  37103. 0xfc, 0xcb, 0x22, 0x61, 0x5c, 0x39, 0xc4, 0x3f, 0xc5, 0xef,
  37104. 0xb8, 0xc8, 0x88, 0x52, 0xce, 0x90, 0x17, 0xf5, 0x3c, 0xa9,
  37105. 0x87, 0x6f, 0xcb, 0x2f, 0x11, 0x53, 0x65, 0x9b, 0x74, 0x21,
  37106. 0x3e, 0xdd, 0x7b, 0x1f, 0x19, 0x9f, 0x53, 0xe6, 0xab, 0xc0,
  37107. 0x56, 0xba, 0x80, 0x19, 0x5d, 0x3f, 0xc7, 0xe2, 0xfb, 0x8c,
  37108. 0xe2, 0x93, 0xe0, 0x31, 0xc9, 0x33, 0x31, 0x23, 0x31, 0xa1,
  37109. 0x36, 0x4c, 0x62, 0xd8, 0x0a, 0xfd, 0x85, 0x97, 0xae, 0xa9,
  37110. 0xe9, 0x58, 0x29, 0x17, 0x33, 0x09, 0x5a, 0x8e, 0xa3, 0x90,
  37111. 0x41, 0xd3, 0xfc, 0x24, 0x98, 0x61, 0x4d, 0x30, 0x1f, 0x76,
  37112. 0x8f, 0xfc, 0xd0, 0x96, 0x8b, 0x2e, 0x9b, 0x24, 0x73, 0x35,
  37113. 0x00, 0xb7, 0xf6, 0xe8, 0xba, 0xec, 0x98, 0x74, 0x41, 0xa4,
  37114. 0x47, 0x10, 0x0d, 0xbc, 0xba, 0xd1, 0xe7, 0xdb, 0x12, 0xcb,
  37115. 0x5f, 0x02, 0xb1, 0xa6, 0xa0, 0xd7, 0x28, 0x30, 0x3e, 0x0a,
  37116. 0x5c, 0x5f, 0xe6, 0x2f, 0x3c, 0xde, 0x46, 0x60, 0xaf, 0x07,
  37117. 0x5f, 0xed, 0x08, 0xc0, 0x06, 0x58, 0xba, 0xd7, 0x36, 0x5b,
  37118. 0xa0, 0x4a, 0xf7, 0xa1, 0x05, 0x9b, 0x00, 0xda, 0x49, 0xdc,
  37119. 0xbf, 0xea, 0xe1, 0x03, 0xda, 0x95, 0x95, 0xa0, 0xfa, 0x2e,
  37120. 0xf1, 0x60, 0x11, 0x47, 0xdd, 0xb3, 0xfb, 0x0b, 0xa2, 0x92,
  37121. 0xcf, 0x73, 0xbb, 0xce, 0x82, 0x71, 0xbc, 0xbd, 0x50, 0x64,
  37122. 0xf1, 0x96, 0x48, 0x48, 0x93, 0xf8, 0xdc, 0x1c, 0x18, 0x12,
  37123. 0xc6, 0x17, 0x6a, 0xa9, 0xc1, 0x4d, 0x6f, 0x76, 0xda, 0x2f,
  37124. 0x4e, 0x59, 0xdd, 0x8b, 0x1c, 0xa5, 0x30, 0xb6, 0xe9, 0x88,
  37125. 0x8f, 0x75, 0x0c, 0xcd, 0xd8, 0x61, 0xf4, 0x28, 0xc5, 0x9a,
  37126. 0xcd, 0x77, 0x0d, 0x36, 0x5f, 0x75, 0xa5, 0x0a, 0x77, 0x20,
  37127. 0x28, 0x5a, 0xac, 0x5f, 0xa1, 0x83, 0x67, 0x70, 0xb7, 0xd8,
  37128. 0x23, 0x48, 0x60, 0xa8, 0xd0, 0xaf, 0xee, 0x7a, 0xb8, 0x25,
  37129. 0xd7, 0x8f, 0x82, 0x8c, 0xd0, 0x81, 0x7a, 0x49, 0x69, 0xe4,
  37130. 0x22, 0x73, 0x29, 0x48, 0xc8, 0x09, 0x72, 0x16, 0xf8, 0x3d,
  37131. 0xff, 0x13, 0xac, 0x98, 0x03, 0x76, 0x33, 0xcb, 0x19, 0xb0,
  37132. 0x22, 0x5b, 0x1e, 0x16, 0x29, 0xb9, 0xcc, 0xa6, 0x92, 0xd8,
  37133. 0xed, 0x93, 0x0f, 0xbd, 0x10, 0x98, 0x53, 0x0a, 0x07, 0x7f,
  37134. 0xd6, 0x51, 0x76, 0xda, 0xdc, 0x0c, 0xeb, 0x2a, 0x95, 0xd0,
  37135. 0x3e, 0xa6, 0xc4, 0xc6, 0xd8, 0xfb, 0x1b, 0x2a, 0x7f, 0xf1,
  37136. 0x08, 0xbe, 0xd3, 0xed, 0x67, 0x63, 0x5f, 0x1d, 0x29, 0xdb,
  37137. 0x47, 0x03, 0x4a, 0xf4, 0x6b, 0xb4, 0x46, 0x02, 0x28, 0x4f,
  37138. 0x88, 0x9b, 0x46, 0x66, 0x40, 0x56, 0x34, 0x4c, 0xec, 0x8e,
  37139. 0x0b, 0x5d, 0x14, 0x94, 0x91, 0xfc, 0xdc, 0x0c, 0xdc, 0x5b,
  37140. 0x45, 0x12, 0x7e, 0xa1, 0xe9, 0x75, 0x38, 0xcb, 0xd3, 0x6b,
  37141. 0xd7, 0xa4, 0x24, 0x94, 0x78, 0x09, 0x7f, 0x77, 0xc8, 0x6d,
  37142. 0xe1, 0x82, 0x1c, 0x1c, 0x91, 0xc6, 0x38, 0x9e, 0x3b, 0x3d,
  37143. 0x31, 0xdd, 0x9e, 0x46, 0x58, 0x7a, 0x42, 0x16, 0x6f, 0xfd,
  37144. 0x7d, 0x8c, 0xf5, 0xf0, 0x9f, 0x92, 0x6e, 0xbe, 0x47, 0xa6,
  37145. 0x1e, 0x8e, 0x82, 0x15, 0x24, 0xc3, 0x1b, 0xb0, 0xd1, 0x68,
  37146. 0xf9, 0xd1, 0x7c, 0x60, 0x98, 0x86, 0xd9, 0x53, 0xa2, 0x38,
  37147. 0x62, 0xf4, 0x72, 0x71, 0xcb, 0xb9, 0x35, 0xef, 0xb9, 0x49,
  37148. 0x3a, 0x73, 0xb2, 0xd7, 0x0f, 0x90, 0xf5, 0x2c, 0x5b, 0xf5,
  37149. 0xfd, 0x39, 0x17, 0xf7, 0xe4, 0x69, 0x81, 0x0f, 0x6b, 0xe7,
  37150. 0x32, 0xd2, 0xdc, 0x5d, 0x40, 0xbf, 0x41, 0x95, 0x89, 0x81,
  37151. 0x29, 0x80, 0x40, 0xa3, 0xac, 0xd2, 0xc7, 0xf7, 0xe8, 0xd0,
  37152. 0x45, 0xed, 0x48, 0x43, 0x3a, 0xed, 0x8d, 0xef, 0x37, 0xe1,
  37153. 0x24, 0x9a, 0x67, 0x9a, 0x6b, 0x71, 0x4f, 0x9a, 0xb9, 0x2c,
  37154. 0x1b, 0x10, 0x48, 0xe2, 0x31, 0x1e, 0xbb, 0xf2, 0x4a, 0xad,
  37155. 0x04, 0xc7, 0xd7, 0xf2, 0xe8, 0x83, 0x5f, 0xe8, 0xa2, 0x81,
  37156. 0x95, 0xf9, 0x60, 0x51, 0x9c, 0x99, 0x76, 0x69, 0x76, 0x4e,
  37157. 0xbd, 0x44, 0x52, 0x36, 0xca, 0xd8, 0x6e, 0xf7, 0x1a, 0xa1,
  37158. 0x54, 0xdf, 0x90, 0x52, 0x94, 0xb6, 0x3a, 0xcb, 0x43, 0x56,
  37159. 0x11, 0xde, 0xa0, 0xe1, 0x45, 0x8a, 0x80, 0x2d, 0xaf, 0x1f,
  37160. 0x24, 0x3f, 0x80, 0x17, 0x1f, 0x28, 0xbb, 0xcc, 0x1a, 0xd2,
  37161. 0x2d, 0xa6, 0x9e, 0xe0, 0xdc, 0xf0, 0x98, 0x16, 0x58, 0x88,
  37162. 0xc6, 0xf1, 0x81, 0x71, 0x91, 0x8f, 0xa2, 0xab, 0xa5, 0xe6,
  37163. 0x68, 0x1f, 0xa5, 0x86, 0xb5, 0xd9, 0x05, 0xba, 0x50, 0x67,
  37164. 0x0b, 0x1e, 0xfe, 0x42, 0x50, 0xf8, 0x01, 0xf8, 0x38, 0x92,
  37165. 0x57, 0x86, 0x08, 0x47, 0xee, 0x23, 0x11, 0x60, 0x61, 0x1a,
  37166. 0x77, 0x3c, 0x1a, 0x8e, 0x08, 0xe3, 0xaf, 0x84, 0x04, 0x75,
  37167. 0x15, 0x47, 0x7a, 0x83, 0x8e, 0x92, 0x3e, 0xe8, 0xf0, 0xc2,
  37168. 0x81, 0x89, 0x3b, 0x73, 0x81, 0xe5, 0xe8, 0x97, 0x97, 0x63,
  37169. 0x64, 0xf3, 0xa9, 0x1b, 0x61, 0x65, 0x7f, 0x0e, 0x47, 0x6b,
  37170. 0x14, 0x57, 0x29, 0x8f, 0x91, 0x35, 0x43, 0x10, 0x12, 0x86,
  37171. 0x99, 0xec, 0xc8, 0x9e, 0x67, 0x90, 0x20, 0x21, 0x3c, 0x83,
  37172. 0xdb, 0x73, 0x4e, 0x8e, 0x7d, 0x86, 0xde, 0xb8, 0xd8, 0xfa,
  37173. 0x23, 0x1f, 0x5a, 0xe4, 0xc7, 0x0c, 0x1d, 0x5e, 0xd1, 0x10,
  37174. 0x58, 0xd5, 0x86, 0xfa, 0x40, 0x30, 0x0a, 0x78, 0x0a, 0xa5,
  37175. 0x56, 0xd5, 0xe6, 0x86, 0xd4, 0x14, 0x77, 0x32, 0xcd, 0x07,
  37176. 0xf9, 0xbe, 0x7a, 0xd8, 0xbc, 0x91, 0xe0, 0xda, 0x76, 0x6b,
  37177. 0x97, 0x10, 0xda, 0xea, 0x27, 0xa2, 0x67, 0x6d, 0x94, 0x27,
  37178. 0x6e, 0xea, 0xca, 0x56, 0x45, 0x32, 0x1d, 0x38, 0x12, 0x21,
  37179. 0x33, 0x2c, 0x3c, 0x5c, 0x33, 0xb0, 0x9e, 0x80, 0x0b, 0x4e,
  37180. 0xbb, 0x09, 0x5e, 0x56, 0x54, 0xb0, 0x9b, 0x7e, 0xb6, 0x00,
  37181. 0xe8, 0x63, 0x19, 0x85, 0xf1, 0x4d, 0x65, 0x9d, 0x1f, 0x8d,
  37182. 0x18, 0xcc, 0x63, 0xc6, 0xd9, 0xa6, 0xbc, 0xe7, 0x42, 0x55,
  37183. 0x12, 0xdc, 0x8c, 0x26, 0x2d, 0x8d, 0xc2, 0xe9, 0x3b, 0xbc,
  37184. 0xed, 0x06, 0x08, 0x31, 0xb0, 0xe0, 0x99, 0xe2, 0x86, 0x81,
  37185. 0x88, 0x4a, 0xac, 0x1f, 0x4a, 0xb2, 0x1e, 0x1e, 0x4c, 0xb2,
  37186. 0x9f, 0x27, 0xa0, 0xd9, 0x8a, 0x7e, 0xe7, 0xa3, 0xad, 0xeb,
  37187. 0x2c, 0xfd, 0x14, 0xc6, 0x4b, 0x26, 0xce, 0x38, 0xb9, 0x01,
  37188. 0x9e, 0xde, 0xc8, 0x7b, 0x82, 0x2f, 0xaa, 0x72, 0x80, 0xbe,
  37189. 0x3a, 0x35, 0x95, 0xc8, 0xf3, 0x7c, 0x36, 0x68, 0x02, 0xdc,
  37190. 0xa2, 0xda, 0xef, 0xd7, 0xf1, 0x3e, 0x81, 0xb3, 0x5d, 0x2f,
  37191. 0xcf, 0x7e, 0xe6, 0x9c, 0xa0, 0x32, 0x29, 0x8b, 0x52, 0x24,
  37192. 0xbd, 0x0d, 0x36, 0xdc, 0x1d, 0xcc, 0x6a, 0x0a, 0x74, 0x52,
  37193. 0x1b, 0x68, 0x4d, 0x15, 0x05, 0x47, 0xe1, 0x2f, 0x97, 0x45,
  37194. 0x52, 0x17, 0x4b, 0x2a, 0x3b, 0x74, 0xc5, 0x20, 0x35, 0x5c,
  37195. 0x37, 0xae, 0xe6, 0xa7, 0x24, 0x0f, 0x34, 0x70, 0xea, 0x7c,
  37196. 0x03, 0xa3, 0xde, 0x2d, 0x22, 0x55, 0x88, 0x01, 0x45, 0xf2,
  37197. 0x5f, 0x1f, 0xaf, 0x3b, 0xb1, 0xa6, 0x5d, 0xcd, 0x93, 0xfb,
  37198. 0xf8, 0x2f, 0x87, 0xcc, 0x26, 0xc5, 0x36, 0xde, 0x06, 0x9b,
  37199. 0xe9, 0xa7, 0x66, 0x7e, 0x8c, 0xcd, 0x99, 0x6b, 0x51, 0x1c,
  37200. 0xb0, 0xa0, 0xfa, 0xc7, 0x46, 0xfe, 0x65, 0xe4, 0x80, 0x5b,
  37201. 0x5f, 0x24, 0x3b, 0xa4, 0xe6, 0x81, 0x31, 0xe5, 0x87, 0x2c,
  37202. 0xa4, 0x83, 0xaf, 0x8b, 0x9f, 0x89, 0xb4, 0x3c, 0x7a, 0xbe,
  37203. 0x4c, 0xb3, 0xbf, 0x3d, 0xec, 0x78, 0xb0, 0x8a, 0xdd, 0xc8,
  37204. 0x43, 0x8c, 0x45, 0xa1, 0xa3, 0x3a, 0x82, 0x7d, 0x06, 0xdf,
  37205. 0x20, 0x27, 0x9b, 0x4e, 0x09, 0x90, 0x6a, 0x23, 0xbf, 0x1b,
  37206. 0x04, 0x1d, 0x50, 0xe2, 0xb4, 0xff, 0xe0, 0xd0, 0x9b, 0x40,
  37207. 0x2b, 0xc0, 0x52, 0xc1, 0x39, 0x29, 0x60, 0x83, 0x06, 0x9b,
  37208. 0x48, 0xb8, 0xa7, 0xe1, 0x2b, 0xfb, 0xf0, 0x2b, 0x82, 0xf1,
  37209. 0xda, 0xc9, 0x30, 0x47, 0x3f, 0xf5, 0xf9, 0xf7, 0x6c, 0xf0,
  37210. 0x0f, 0xe7, 0xb1, 0x4d, 0x46, 0x49, 0xf8, 0xb3, 0xe1, 0xfe,
  37211. 0x85, 0x61, 0xcc, 0xf7, 0xfa, 0xd2, 0xf1, 0xbc, 0xf0, 0x7f,
  37212. 0x3b, 0xe6, 0x45, 0xa2, 0x1b, 0x55, 0xf6, 0x0c, 0x02, 0x95,
  37213. 0xdc, 0x78, 0x94, 0xa0, 0xc4, 0x6a, 0x21, 0x7e, 0xa8, 0x5f,
  37214. 0xbd, 0xc3, 0xb3, 0x4d, 0x9b, 0x30, 0x31, 0x1d, 0x5b, 0x8b,
  37215. 0x45, 0x3c, 0x18, 0xe9, 0x61, 0xe8, 0x76, 0x3e, 0x91, 0xd2,
  37216. 0xfd, 0x1a, 0xd7, 0x30, 0x4d, 0xfe, 0xef, 0x7f, 0xc0, 0x7e,
  37217. 0x45, 0x43, 0xe9, 0xf9, 0x23, 0xfe, 0xd8, 0xef, 0xbc, 0xd6,
  37218. 0x99, 0x79, 0x54, 0xed, 0x7a, 0x8b, 0x39, 0xa6, 0xe7, 0x9d,
  37219. 0x3f, 0x9f, 0x35, 0xe1, 0xe4, 0xd5, 0x26, 0x31, 0x3a, 0x44,
  37220. 0x03, 0x79, 0xde, 0xdc, 0x29, 0x1e, 0x8e, 0x26, 0x41, 0xc6,
  37221. 0x60, 0xaa, 0xfd, 0xe1, 0x5e, 0xa6, 0xc0, 0x2f, 0x90, 0x1e,
  37222. 0x3b, 0xc1, 0xe6, 0xf6, 0xde, 0x60, 0x87, 0x57, 0x51, 0x11,
  37223. 0x6a, 0x8e, 0x9d, 0x70, 0x9d, 0x6d, 0x36, 0x21, 0x05, 0x55,
  37224. 0xc1, 0x56, 0x9b, 0xc9, 0x91, 0x50, 0x3e, 0xb4, 0xbd, 0x19,
  37225. 0x53, 0x44, 0x99, 0xc7, 0xb8, 0xce, 0xce, 0x86, 0x06, 0x5d,
  37226. 0x99, 0x85, 0x33, 0xd4, 0x16, 0x21, 0x4a, 0xe9, 0x7e, 0x2e,
  37227. 0xcc, 0x7e, 0x3f, 0xc1, 0x47, 0x3b, 0x32, 0xd0, 0x57, 0x1c,
  37228. 0xc2, 0x26, 0x67, 0xf0, 0xd9, 0xc4, 0x9e, 0xbb, 0x65, 0xa4,
  37229. 0xf7, 0xf7, 0x8d, 0x7d, 0x08, 0xd4, 0x9c, 0x1e, 0x0f, 0xb9,
  37230. 0xff, 0x24, 0x2f, 0xaf, 0xfa, 0x24, 0x26, 0xb7, 0xb1, 0x78,
  37231. 0xc1, 0xd1, 0xfe, 0x85, 0x55, 0xa0, 0x86, 0x77, 0xf6, 0xc2,
  37232. 0xe0, 0x12, 0xe4, 0x45, 0x85, 0xd0, 0xe7, 0x68, 0xf0, 0x31,
  37233. 0x4c, 0x9c, 0xb0, 0x5f, 0x89, 0xca, 0xfe, 0xc2, 0xf0, 0x1e,
  37234. 0xeb, 0xee, 0x75, 0x64, 0xea, 0x09, 0xd4, 0x1c, 0x72, 0x12,
  37235. 0xd4, 0x31, 0xf0, 0x89, 0x71, 0x74, 0x6e, 0x01, 0x32, 0xca,
  37236. 0x8a, 0x91, 0x0c, 0xdf, 0xd7, 0x05, 0xe9, 0x35, 0xed, 0x06,
  37237. 0x1a, 0x17, 0x5a, 0xf3, 0x65, 0xc5, 0xbd, 0x37, 0xf2, 0x53,
  37238. 0x49, 0x2f, 0xcd, 0xc6, 0x15, 0xb3, 0x36, 0x88, 0xd8, 0x7a,
  37239. 0x2f, 0xfa, 0x21, 0x7f, 0x55, 0x20, 0xc6, 0xf4, 0x23, 0x59,
  37240. 0x6b, 0x3c, 0xeb, 0xe5, 0xd3, 0x78, 0xdc, 0x31, 0xeb, 0x87,
  37241. 0x86, 0x3d, 0x7c, 0x10, 0x64, 0x66, 0xa4, 0xad, 0x07, 0xe1,
  37242. 0x93, 0x15, 0x07, 0x4c, 0xe4, 0xb4, 0x4a, 0x06, 0xca, 0x2a,
  37243. 0x50, 0xa2, 0x85, 0xc6, 0xa1, 0x19, 0x89, 0x7f, 0x8a, 0x05,
  37244. 0x00, 0x23, 0x72, 0x5f, 0x89, 0x74, 0x8e, 0x22, 0xa1, 0x5d,
  37245. 0x26, 0xf9, 0xfe, 0xdf, 0x6d, 0x98, 0x3a, 0xc4, 0x7c, 0x93,
  37246. 0xcf, 0xc4, 0xfe, 0xed, 0x98, 0xb0, 0x31, 0x4c, 0x81, 0x83,
  37247. 0x0d, 0x5d, 0x3d, 0x0c, 0x27, 0x4e, 0xca, 0xcf, 0x38, 0x0c,
  37248. 0x37, 0xb0, 0xf8, 0xc5, 0xc8, 0x52, 0x14, 0xec, 0x53, 0x80,
  37249. 0xb9, 0xd8, 0x8a, 0x05, 0x4e, 0x31, 0x3d, 0x67, 0x57, 0xf0,
  37250. 0x7a, 0xa2, 0xc5, 0xc9, 0x02, 0x25, 0x69, 0x83, 0xb9, 0x3e,
  37251. 0x1b, 0x04, 0xbf, 0xb2, 0xe6, 0x97, 0x7a, 0x6b, 0x8e, 0x37,
  37252. 0x77, 0x2e, 0x16, 0x8b, 0x33, 0xe1, 0xea, 0x2b, 0x30, 0x01,
  37253. 0x6e, 0xa0, 0x28, 0x14, 0x17, 0xe9, 0x98, 0xa8, 0x89, 0x72,
  37254. 0x68, 0x64, 0x81, 0x60, 0xa8, 0xf7, 0x72, 0xdf, 0x1a, 0xae,
  37255. 0xf5, 0xf0, 0x9f, 0x69, 0x35, 0xbc, 0x58, 0x27, 0x38, 0xd6,
  37256. 0x7f, 0x7a, 0xd4, 0xc4, 0xf1, 0xcf, 0xee, 0x59, 0x49, 0x31,
  37257. 0xda, 0xc1, 0x08, 0x46, 0x65, 0x68, 0xe9, 0x44, 0x18, 0x2b,
  37258. 0xf2, 0x2a, 0x13, 0x60, 0x07, 0xae, 0xe4, 0x96, 0xdb, 0x0a,
  37259. 0x6f, 0x52, 0x23, 0x9a, 0xcf, 0x9d, 0xa4, 0xc5, 0xc1, 0x74,
  37260. 0xa8, 0x0e, 0xe1, 0x5e, 0xfa, 0xa4, 0x06, 0x9c, 0x2e, 0x70,
  37261. 0x08, 0x22, 0x25, 0x4f, 0xc1, 0xf1, 0x13, 0x5a, 0x66, 0xa0,
  37262. 0x6c, 0x59, 0xa3, 0xfc, 0x03, 0x9c, 0x8a, 0x23, 0x01, 0x00,
  37263. 0xa9, 0x49, 0xf0, 0x22, 0xa3, 0x8f, 0x6c, 0xef, 0xcb, 0x69,
  37264. 0x06, 0x3a, 0x69, 0x99, 0x96, 0xd2, 0xa7, 0xa0, 0x0b, 0x7e,
  37265. 0x44, 0x7d, 0x04, 0xff, 0x7e, 0x9e, 0x1e, 0x77, 0xa0, 0x30,
  37266. 0xd1, 0xdf, 0x18, 0xe4, 0xd8, 0xa5, 0x64, 0xbe, 0x8c, 0x80,
  37267. 0x28, 0xe2, 0x98, 0x5e, 0xec, 0x9e, 0xb1, 0x0a, 0xb5, 0x25,
  37268. 0xaa, 0xb8, 0x0f, 0x78, 0x30, 0x48, 0x06, 0xe5, 0x76, 0xf9,
  37269. 0x24, 0x96, 0x87, 0x2a, 0x91, 0x89, 0xb6, 0xce, 0x04, 0xdf,
  37270. 0xfc, 0x13, 0x42, 0x19, 0xba, 0x14, 0x46, 0x20, 0x08, 0x47,
  37271. 0xe1, 0x82, 0x57, 0x51, 0x74, 0x3b, 0x5b, 0x23, 0x5c, 0xb2,
  37272. 0x85, 0x8c, 0xed, 0xe6, 0xda, 0x4d, 0x56, 0xe8, 0x61, 0x31,
  37273. 0xec, 0x97, 0x27, 0xeb, 0xf2, 0xa7, 0x7c, 0x13, 0x1b, 0xc5,
  37274. 0x44, 0xfe, 0x63, 0x4b, 0x2b, 0x33, 0x22, 0x23, 0x60, 0x86,
  37275. 0x7c, 0x3b, 0x57, 0xba, 0x16, 0xde, 0x47, 0x04, 0x3e, 0x2b,
  37276. 0xe5, 0xbd, 0x23, 0xa0, 0xab, 0xdf, 0x5d, 0x6e, 0x20, 0xb1,
  37277. 0x37, 0x44, 0xcb, 0xbd, 0x03, 0xa9, 0x5c, 0xe6, 0x92, 0x5e,
  37278. 0x2f, 0x6f, 0x95, 0xc6, 0x5b, 0x6d, 0xab, 0x39, 0xdd, 0x1e,
  37279. 0x34, 0xd5, 0x21, 0xca, 0x92, 0xee, 0x59, 0xf0, 0xb9, 0x65,
  37280. 0xe6, 0x81, 0x49, 0xf8, 0x11, 0xec, 0x45, 0x14, 0x6a, 0x19,
  37281. 0xb4, 0xce, 0xbf, 0x9e, 0xf7, 0x32, 0x8d, 0x99, 0x78, 0xc3,
  37282. 0x07, 0x3d, 0xfd, 0x18, 0x2d, 0x0e, 0x06, 0x2f, 0x27, 0x24,
  37283. 0x6f, 0x16, 0xd8, 0x01, 0x33, 0xc8, 0xbb, 0x7f, 0x7d, 0xfa,
  37284. 0x73, 0xf6, 0x7d, 0x54, 0xf2, 0xd4, 0x8a, 0x53, 0xe1, 0x62,
  37285. 0x45, 0xf4, 0x01, 0xa6, 0x31, 0x6b, 0x3a, 0x06, 0x56, 0xfd,
  37286. 0x79, 0x7f, 0x58, 0xd8, 0x47, 0x33, 0x53, 0xc5, 0x78, 0x70,
  37287. 0xce, 0x81, 0x7f, 0x66, 0xa1, 0x58, 0x7c, 0x5a, 0xdb, 0x4a,
  37288. 0xad, 0x29, 0xff, 0x93, 0x75, 0x95, 0x35, 0xa9, 0xd2, 0xb1,
  37289. 0xeb, 0xa0, 0x4f, 0x10, 0x0a, 0xc9, 0x38, 0x69, 0xc8, 0x8d,
  37290. 0x57, 0xef, 0x99, 0x0f, 0xa5, 0x69, 0x86, 0xa6, 0xfb, 0x2b,
  37291. 0x37, 0xe4, 0xc7, 0xab, 0x3e, 0xcd, 0x8f, 0x3f, 0x93, 0x8c,
  37292. 0x0b, 0xc4, 0x4d, 0x16, 0xe0, 0xb0, 0x94, 0x5a, 0x0d, 0x17,
  37293. 0xaf, 0x6e, 0x4b, 0x2e, 0x18, 0x29, 0x0e, 0xe0, 0xf5, 0x72,
  37294. 0x1a, 0x21, 0x37, 0xef, 0x7d, 0x6a, 0x39, 0xe9, 0xa8, 0xd7,
  37295. 0x96, 0xd6, 0xb3, 0x7d, 0x83, 0x0c, 0x13, 0x30, 0x49, 0x03,
  37296. 0xe8, 0x6b, 0xe6, 0x77, 0xe8, 0x69, 0x48, 0x56, 0x5f, 0x39,
  37297. 0x63, 0xbc, 0x86, 0xa8, 0x26, 0xa1, 0xbd, 0x4b, 0x24, 0xbd,
  37298. 0xdd, 0xe8, 0x02, 0x64, 0xcb, 0xae, 0x24, 0x17, 0x62, 0xbd,
  37299. 0x27, 0xa7, 0x22, 0x60, 0x51, 0x0c, 0x53, 0xff, 0x9d, 0x63,
  37300. 0x1b, 0xf9, 0xff, 0x76, 0x3b, 0x74, 0x05, 0x98, 0x46, 0x0b,
  37301. 0xe8, 0xcb, 0xd4, 0x0a, 0xcd, 0x91, 0xdb, 0x5b, 0x21, 0x4d,
  37302. 0xa1, 0x87, 0xbd, 0xb7, 0x58, 0xec, 0x28, 0x00, 0x92, 0xc2,
  37303. 0x98, 0xe4, 0x8c, 0x1f, 0x9d, 0xa4, 0x80, 0x83, 0x40, 0xb9,
  37304. 0x63, 0xfe, 0xc9, 0x18, 0x3f, 0xd6, 0xab, 0x34, 0x00, 0x2c,
  37305. 0x53, 0x40, 0x38, 0x0e, 0xb1, 0x69, 0xa8, 0xb8, 0xa9, 0x2e,
  37306. 0x9b, 0x7b, 0x89, 0x8d, 0xff, 0x86, 0x01, 0x51, 0x42, 0xde,
  37307. 0x04, 0xd6, 0x1d, 0xd1, 0x29, 0x8d, 0x42, 0x46, 0x5f, 0xd6,
  37308. 0x02, 0xde, 0x73, 0xee, 0x2d, 0xe9, 0x6e, 0xb0, 0x3f, 0xf0,
  37309. 0x47, 0x72, 0xfe, 0x45, 0xff, 0x05, 0x82, 0x2d, 0xc6, 0x4f,
  37310. 0xc9, 0xd3, 0xec, 0xf9, 0x5a, 0x22, 0x50, 0x6c, 0x4f, 0x1e,
  37311. 0xc8, 0x5f, 0xfc, 0x2c, 0x04, 0x4f, 0xdf, 0xce, 0xe4, 0x18,
  37312. 0xd2, 0xd7, 0x8b, 0x67, 0x83, 0x39, 0x96, 0x47, 0x5e, 0x5b,
  37313. 0xad, 0x7f, 0x5d, 0x42, 0x56, 0x97, 0x71, 0x39, 0x28, 0x44,
  37314. 0x9d, 0x35, 0xde, 0xde, 0x03, 0x20, 0x34, 0x44, 0xdb, 0xdf,
  37315. 0xfc, 0xff, 0x1e, 0x3d, 0x58, 0x5f, 0x7a, 0x8e, 0x90, 0xa1,
  37316. 0xd3, 0xeb, 0x0c, 0x23, 0x3f, 0x4e, 0x61, 0x77, 0x79, 0xb2,
  37317. 0xdc, 0xfb, 0x21, 0x46, 0x5c, 0x82, 0xb6, 0xf6, 0x34, 0x3c,
  37318. 0x3f, 0x45, 0x4b, 0x80, 0x9e, 0xa4, 0xe6, 0x02, 0x13, 0x38,
  37319. 0x40, 0x7e, 0x87, 0x92, 0x96, 0x51, 0x63, 0x87, 0xae, 0xc8,
  37320. 0x02, 0x6a, 0x70, 0xc8, 0xcd, 0xd0, 0xe2, 0x00, 0x00, 0x00,
  37321. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  37322. 0x12, 0x1c, 0x22, 0x2b, 0x33, 0x38, 0x3f,
  37323. };
  37324. return dilithium_param_vfy_test(WC_ML_DSA_87, ml_dsa_87_pub_key,
  37325. (word32)sizeof(ml_dsa_87_pub_key), ml_dsa_87_sig,
  37326. (word32)sizeof(ml_dsa_87_sig));
  37327. }
  37328. #endif
  37329. #endif
  37330. #ifndef WOLFSSL_DILITHIUM_NO_MAKE_KEY
  37331. static wc_test_ret_t dilithium_param_test(int param, WC_RNG* rng)
  37332. {
  37333. wc_test_ret_t ret;
  37334. dilithium_key* key;
  37335. byte* sig = NULL;
  37336. #ifndef WOLFSSL_DILITHIUM_NO_SIGN
  37337. word32 sigLen;
  37338. byte msg[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  37339. #ifndef WOLFSSL_DILITHIUM_NO_VERIFY
  37340. int res = 0;
  37341. #endif
  37342. #endif
  37343. key = (dilithium_key*)XMALLOC(sizeof(*key), HEAP_HINT,
  37344. DYNAMIC_TYPE_TMP_BUFFER);
  37345. if (key == NULL) {
  37346. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  37347. }
  37348. sig = (byte*)XMALLOC(DILITHIUM_MAX_SIG_SIZE, HEAP_HINT,
  37349. DYNAMIC_TYPE_TMP_BUFFER);
  37350. if (sig == NULL) {
  37351. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  37352. }
  37353. ret = wc_dilithium_init(key);
  37354. if (ret != 0) {
  37355. ret = WC_TEST_RET_ENC_EC(ret);
  37356. return ret;
  37357. }
  37358. ret = wc_dilithium_set_level(key, param);
  37359. if (ret != 0)
  37360. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37361. ret = wc_dilithium_make_key(key, rng);
  37362. if (ret != 0)
  37363. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37364. #ifndef WOLFSSL_DILITHIUM_NO_SIGN
  37365. sigLen = wc_dilithium_sig_size(key);
  37366. if (sigLen <= 0)
  37367. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37368. ret = wc_dilithium_sign_msg(msg, (word32)sizeof(msg), sig, &sigLen, key,
  37369. rng);
  37370. if (ret != 0)
  37371. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37372. #ifndef WOLFSSL_DILITHIUM_NO_VERIFY
  37373. ret = wc_dilithium_verify_msg(sig, sigLen, msg, (word32)sizeof(msg), &res,
  37374. key);
  37375. if (ret != 0)
  37376. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37377. if (res != 1)
  37378. ERROR_OUT(WC_TEST_RET_ENC_EC(res), out);
  37379. #endif
  37380. #endif
  37381. out:
  37382. wc_dilithium_free(key);
  37383. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37384. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37385. return ret;
  37386. }
  37387. #endif
  37388. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dilithium_test(void)
  37389. {
  37390. wc_test_ret_t ret;
  37391. WC_RNG rng;
  37392. #ifndef HAVE_FIPS
  37393. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  37394. #else
  37395. ret = wc_InitRng(&rng);
  37396. #endif
  37397. if (ret != 0) {
  37398. ret = WC_TEST_RET_ENC_EC(ret);
  37399. return ret;
  37400. }
  37401. #ifndef WOLFSSL_NO_ML_DSA_44
  37402. #ifndef WOLFSSL_DILITHIUM_NO_VERIFY
  37403. ret = dilithium_param_44_vfy_test();
  37404. if (ret != 0)
  37405. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37406. #endif
  37407. #ifndef WOLFSSL_DILITHIUM_NO_MAKE_KEY
  37408. ret = dilithium_param_test(WC_ML_DSA_44, &rng);
  37409. if (ret != 0)
  37410. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37411. #endif
  37412. #endif
  37413. #ifndef WOLFSSL_NO_ML_DSA_65
  37414. #ifndef WOLFSSL_DILITHIUM_NO_VERIFY
  37415. ret = dilithium_param_65_vfy_test();
  37416. if (ret != 0)
  37417. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37418. #endif
  37419. #ifndef WOLFSSL_DILITHIUM_NO_MAKE_KEY
  37420. ret = dilithium_param_test(WC_ML_DSA_65, &rng);
  37421. if (ret != 0)
  37422. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37423. #endif
  37424. #endif
  37425. #ifndef WOLFSSL_NO_ML_DSA_87
  37426. #ifndef WOLFSSL_DILITHIUM_NO_VERIFY
  37427. ret = dilithium_param_87_vfy_test();
  37428. if (ret != 0)
  37429. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37430. #endif
  37431. #ifndef WOLFSSL_DILITHIUM_NO_MAKE_KEY
  37432. ret = dilithium_param_test(WC_ML_DSA_87, &rng);
  37433. if (ret != 0)
  37434. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37435. #endif
  37436. #endif
  37437. #if !defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY) || \
  37438. !defined(WOLFSSL_DILITHIUM_NO_VERIFY)
  37439. out:
  37440. #endif
  37441. wc_FreeRng(&rng);
  37442. return ret;
  37443. }
  37444. #endif /* HAVE_DILITHIUM */
  37445. #if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  37446. static enum wc_XmssRc xmss_write_key_mem(const byte * priv, word32 privSz,
  37447. void *context)
  37448. {
  37449. /* WARNING: THIS IS AN INSECURE WRITE CALLBACK THAT SHOULD ONLY
  37450. * BE USED FOR TESTING PURPOSES! Production applications should
  37451. * write only to non-volatile storage. */
  37452. XMEMCPY(context, priv, privSz);
  37453. return WC_XMSS_RC_SAVED_TO_NV_MEMORY;
  37454. }
  37455. static enum wc_XmssRc xmss_read_key_mem(byte * priv, word32 privSz,
  37456. void *context)
  37457. {
  37458. /* WARNING: THIS IS AN INSECURE READ CALLBACK THAT SHOULD ONLY
  37459. * BE USED FOR TESTING PURPOSES! */
  37460. XMEMCPY(priv, context, privSz);
  37461. return WC_XMSS_RC_READ_TO_MEMORY;
  37462. }
  37463. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t xmss_test(void)
  37464. {
  37465. int i = 0;
  37466. int j = 0;
  37467. XmssKey signingKey;
  37468. XmssKey verifyKey;
  37469. WC_RNG rng;
  37470. word32 pkSz = 0;
  37471. word32 skSz = 0;
  37472. word32 sigSz = 0;
  37473. word32 bufSz = 0;
  37474. unsigned char * sk = NULL;
  37475. unsigned char * old_sk = NULL;
  37476. const char * msg = "XMSS post quantum signature test";
  37477. word32 msgSz = (word32) XSTRLEN(msg);
  37478. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10
  37479. const char * param = "XMSS-SHA2_10_256";
  37480. #elif WOLFSSL_XMSS_MIN_HEIGHT <= 20
  37481. const char * param = "XMSSMT-SHA2_20/4_256";
  37482. #elif WOLFSSL_XMSS_MIN_HEIGHT <= 40
  37483. const char * param = "XMSSMT-SHA2_40/8_256";
  37484. #else
  37485. const char * param = "XMSSMT-SHA2_60/12_256";
  37486. #endif
  37487. byte * sig = NULL;
  37488. int ret2 = -1;
  37489. int ret = WC_TEST_RET_ENC_NC;
  37490. WOLFSSL_ENTER("xmss_test");
  37491. #ifndef HAVE_FIPS
  37492. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  37493. #else
  37494. ret = wc_InitRng(&rng);
  37495. #endif
  37496. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  37497. ret = wc_XmssKey_Init(&signingKey, NULL, INVALID_DEVID);
  37498. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  37499. ret = wc_XmssKey_Init(&verifyKey, NULL, INVALID_DEVID);
  37500. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  37501. /* Set the parameter string to the signing key, and
  37502. * get sizes for secret key, pub key, and signature. */
  37503. ret = wc_XmssKey_SetParamStr(&signingKey, param);
  37504. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  37505. ret = wc_XmssKey_GetPubLen(&signingKey, &pkSz);
  37506. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  37507. if (pkSz != XMSS_SHA256_PUBLEN) {
  37508. return WC_TEST_RET_ENC_EC(pkSz);
  37509. }
  37510. ret = wc_XmssKey_GetPrivLen(&signingKey, &skSz);
  37511. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  37512. ret = wc_XmssKey_GetSigLen(&signingKey, &sigSz);
  37513. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  37514. /* Allocate signature array. */
  37515. sig = (byte *)XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37516. if (sig == NULL) { return WC_TEST_RET_ENC_ERRNO; }
  37517. bufSz = sigSz;
  37518. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  37519. fprintf(stderr, "param: %s\n", param);
  37520. fprintf(stderr, "pkSz: %d\n", pkSz);
  37521. fprintf(stderr, "skSz: %d\n", skSz);
  37522. fprintf(stderr, "sigSz: %d\n", sigSz);
  37523. #endif
  37524. /* Allocate current and old secret keys.*/
  37525. sk = (unsigned char *)XMALLOC(skSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37526. if (sk == NULL) { return WC_TEST_RET_ENC_ERRNO; }
  37527. old_sk = (unsigned char *)XMALLOC(skSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37528. if (old_sk == NULL) { return WC_TEST_RET_ENC_ERRNO; }
  37529. XMEMSET(sk, 0, skSz);
  37530. XMEMSET(old_sk, 0, skSz);
  37531. XMEMSET(sig, 0, sigSz);
  37532. ret = wc_XmssKey_SetWriteCb(&signingKey, xmss_write_key_mem);
  37533. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  37534. ret = wc_XmssKey_SetReadCb(&signingKey, xmss_read_key_mem);
  37535. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  37536. ret = wc_XmssKey_SetContext(&signingKey, (void *) sk);
  37537. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  37538. ret = wc_XmssKey_MakeKey(&signingKey, &rng);
  37539. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  37540. /* Export the pub to a verify key. */
  37541. ret = wc_XmssKey_ExportPub(&verifyKey, &signingKey);
  37542. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  37543. /* Repeat a few times to check that:
  37544. * 1. The secret key is mutated on each sign.
  37545. * 2. We can verify each new signature.
  37546. * Only do a few times, because the full signature space
  37547. * for this parameter set is huge. */
  37548. for (i = 0; i < 10; ++i) {
  37549. XMEMCPY(old_sk, sk, skSz);
  37550. ret = wc_XmssKey_Sign(&signingKey, sig, &sigSz, (byte *) msg, msgSz);
  37551. if (ret != 0) { return WC_TEST_RET_ENC_I(i); }
  37552. if (sigSz != bufSz) { return WC_TEST_RET_ENC_I(i); }
  37553. /* Old secret key and current secret key should not match. */
  37554. ret = XMEMCMP(old_sk, sk, skSz);
  37555. if (ret == 0) { return WC_TEST_RET_ENC_I(i); }
  37556. ret = wc_XmssKey_Verify(&verifyKey, sig, sigSz, (byte *) msg, msgSz);
  37557. if (ret != 0) { return WC_TEST_RET_ENC_I(i); }
  37558. /* Flip bits in a few places throughout the signature, stepping in multiple
  37559. * of hash size. These should all fail with -1. */
  37560. for (j = 0; j < (int) sigSz; j+= 4 * 32) {
  37561. sig[j] ^= 1;
  37562. ret2 = wc_XmssKey_Verify(&verifyKey, sig, sigSz, (byte *) msg,
  37563. msgSz);
  37564. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  37565. /* Verify passed when it should have failed. */
  37566. return WC_TEST_RET_ENC_I(j);
  37567. }
  37568. /* Flip this spot back. */
  37569. sig[j] ^= 1;
  37570. }
  37571. }
  37572. /* Cleanup everything. */
  37573. if (sig != NULL) {
  37574. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37575. sig = NULL;
  37576. }
  37577. if (sk != NULL) {
  37578. XFREE(sk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37579. sk = NULL;
  37580. }
  37581. if (old_sk != NULL) {
  37582. XFREE(old_sk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37583. old_sk = NULL;
  37584. }
  37585. wc_XmssKey_Free(&signingKey);
  37586. wc_FreeRng(&rng);
  37587. return ret;
  37588. }
  37589. #endif /*if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY)*/
  37590. #if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_SMALL_STACK) && \
  37591. WOLFSSL_XMSS_MIN_HEIGHT <= 10
  37592. /* A simple xmss verify only test using:
  37593. * XMSS-SHA2_10_256
  37594. * pub len: 68
  37595. * msg len: 32
  37596. * sig len: 2500
  37597. *
  37598. * These were generated with the test xmss_fast, from the unpatched
  37599. * xmss-reference repository:
  37600. * https://github.com/XMSS/xmss-reference
  37601. * */
  37602. static const byte xmss_pub[XMSS_SHA256_PUBLEN] =
  37603. {
  37604. 0x00,0x00,0x00,0x01,0xA5,0x41,0x31,0x96,
  37605. 0x0A,0xF9,0xF3,0xB2,0x4B,0x2E,0x5B,0x3E,
  37606. 0xCA,0x74,0xAD,0x6C,0xA5,0x89,0xAD,0x2C,
  37607. 0x0E,0x96,0xB3,0x54,0xFB,0x5B,0x63,0x50,
  37608. 0x96,0x81,0xE2,0x59,0x72,0x10,0x09,0x54,
  37609. 0xBB,0x39,0xAC,0xEE,0x78,0xEF,0x95,0xEC,
  37610. 0x01,0x1D,0xF0,0x36,0x68,0xE2,0xC4,0xA5,
  37611. 0x2F,0x60,0x42,0x7E,0xD3,0x8E,0xAA,0x27,
  37612. 0xC9,0xB7,0x39,0x4E
  37613. };
  37614. static /* not const */ byte xmss_msg[32] =
  37615. {
  37616. 0x07,0x9F,0x80,0x86,0xDB,0x76,0x27,0xDF,
  37617. 0xED,0x5B,0x2A,0x81,0x60,0x60,0x7D,0xB4,
  37618. 0xE8,0x7A,0x69,0x45,0x20,0x6B,0xA2,0x96,
  37619. 0xC0,0x21,0xA5,0x46,0x29,0x63,0x9B,0x37
  37620. };
  37621. /* This was actually the 5th signature produced from
  37622. * xmss_fast test in xmss-reference. */
  37623. static /* not const */ byte xmss_sig[2500] =
  37624. {
  37625. 0x00,0x00,0x00,0x05,0xF0,0x15,0x34,0xBA,
  37626. 0x92,0x03,0x6A,0xB9,0xA5,0x23,0x86,0x11,
  37627. 0xAE,0x65,0x0A,0x5C,0x78,0x2C,0xC9,0xBE,
  37628. 0x7E,0xA6,0xDC,0xA2,0x8B,0xA9,0x9C,0x50,
  37629. 0xF6,0x61,0x8D,0x9D,0xD7,0xE9,0xC0,0xF8,
  37630. 0x67,0xCD,0x8A,0xC4,0x9B,0x74,0x96,0x07,
  37631. 0x5D,0xF2,0xC9,0xCC,0x28,0x05,0xB1,0xBE,
  37632. 0x5E,0xA4,0xBA,0xBE,0xAB,0xD8,0x21,0x6B,
  37633. 0x21,0x5F,0xAB,0xB7,0x6C,0xEC,0x2F,0xC8,
  37634. 0xC6,0x74,0x3E,0x97,0x1B,0xC3,0x45,0x57,
  37635. 0xAF,0xAA,0x1E,0xA8,0xF2,0x86,0xA8,0xAA,
  37636. 0x43,0x6D,0x66,0xE9,0x81,0x14,0xDE,0x09,
  37637. 0x39,0xD2,0xAF,0xD1,0x4C,0xE7,0x75,0x18,
  37638. 0x0D,0xAA,0x29,0xA1,0x92,0x53,0xCC,0xE9,
  37639. 0xF3,0x0B,0x1E,0x3B,0xE2,0xAE,0x80,0x0C,
  37640. 0xE7,0x7A,0x7C,0x13,0x8A,0x28,0xC6,0x5F,
  37641. 0x0A,0xA4,0xA3,0x73,0x0A,0x3A,0xC2,0xA6,
  37642. 0x3B,0xB4,0x30,0x67,0xC0,0x36,0x18,0xA1,
  37643. 0x58,0xCD,0xAD,0x54,0x36,0x64,0xCE,0xFD,
  37644. 0x52,0xFF,0x70,0x7E,0x09,0xFB,0x13,0xA2,
  37645. 0xEA,0xDF,0x67,0x8D,0x6C,0x42,0xB2,0x78,
  37646. 0xF5,0x7D,0x5C,0x4B,0xF7,0x8E,0xCF,0x3E,
  37647. 0xB7,0xC6,0xC1,0x23,0xFA,0x65,0xDE,0xD2,
  37648. 0xFA,0x40,0x51,0x97,0x0D,0x52,0x32,0x76,
  37649. 0x7E,0x82,0x8D,0xD0,0xB9,0x1E,0x62,0xD9,
  37650. 0x1E,0xC1,0xDB,0x40,0x43,0x37,0x4A,0x23,
  37651. 0x8A,0x1D,0x35,0xFA,0xF4,0x53,0x11,0x5A,
  37652. 0xB5,0x6D,0x1E,0x8B,0x22,0xC8,0x7D,0x2A,
  37653. 0xE4,0x94,0xAA,0x25,0x20,0x40,0x96,0xDB,
  37654. 0x82,0x62,0xBA,0x8F,0x8B,0x45,0xCB,0x4F,
  37655. 0x35,0x88,0x33,0xEB,0xEF,0xB3,0xBA,0xA7,
  37656. 0x09,0x72,0xB3,0x4C,0xEC,0xF2,0xC3,0xC7,
  37657. 0x5E,0x02,0x6C,0x41,0x93,0xCB,0x3C,0x89,
  37658. 0x12,0x09,0x68,0x54,0x8E,0xEC,0x6A,0x7E,
  37659. 0x20,0xE1,0x70,0x3D,0x8C,0xEB,0xB4,0x36,
  37660. 0xBE,0x91,0xBE,0x97,0xB5,0xA6,0x34,0x16,
  37661. 0x95,0x0F,0x10,0x26,0xA9,0x13,0x80,0x88,
  37662. 0x9C,0xAA,0x68,0xEC,0x34,0x70,0x4A,0x15,
  37663. 0x9B,0x5E,0x57,0x05,0x87,0x1C,0xF8,0x35,
  37664. 0x45,0x29,0xE9,0x6E,0xF2,0x70,0x13,0x42,
  37665. 0x89,0x4E,0x77,0xC0,0x18,0xC7,0x55,0x6D,
  37666. 0xE7,0xFA,0x0D,0x63,0x83,0x16,0x19,0x01,
  37667. 0x2D,0xFD,0x31,0x14,0x94,0xCA,0x3E,0x0E,
  37668. 0xD6,0x11,0x34,0x81,0x57,0x58,0xEC,0x24,
  37669. 0xA4,0x17,0x63,0xD3,0x25,0x00,0xBF,0x7D,
  37670. 0x78,0x5D,0xC5,0xD8,0xC6,0xC1,0xBD,0x8C,
  37671. 0xD0,0x94,0x0A,0xB1,0x33,0xA5,0x4B,0x31,
  37672. 0x25,0xF5,0xAF,0xE7,0x84,0x26,0xAA,0x05,
  37673. 0xBB,0xF3,0x9A,0xAF,0x58,0x36,0x40,0xEF,
  37674. 0x3D,0xA2,0xBD,0xCA,0xA1,0x8D,0x2F,0x6D,
  37675. 0x54,0xD2,0x62,0x33,0x09,0xAE,0xE6,0x73,
  37676. 0xD6,0x44,0xE8,0x7C,0x5C,0x39,0x2B,0x78,
  37677. 0x94,0x14,0xC7,0xC9,0xAF,0xEC,0x77,0x36,
  37678. 0xA1,0x61,0x61,0xF1,0xD0,0x09,0xA2,0xEE,
  37679. 0xE7,0x55,0xD7,0x35,0x89,0x89,0x9B,0xCF,
  37680. 0xFA,0xA6,0x09,0x1E,0x3B,0xBD,0x5D,0xD9,
  37681. 0x25,0xE7,0xED,0xDD,0x7C,0xF0,0x1C,0x57,
  37682. 0xE0,0x06,0xBB,0x08,0x39,0x59,0xDF,0xD7,
  37683. 0xAF,0x4B,0x88,0x0D,0x87,0x8F,0x4A,0xF3,
  37684. 0x1C,0xD4,0x4B,0xB3,0xE2,0xF3,0x1B,0x86,
  37685. 0x4F,0xCD,0x35,0x75,0xE2,0x03,0xF9,0x1D,
  37686. 0xBF,0x3E,0xD1,0x7B,0xC7,0x23,0x11,0x75,
  37687. 0x5F,0x92,0x0D,0x98,0xEE,0x14,0xE1,0xDA,
  37688. 0x7A,0x02,0x17,0x47,0x6B,0x41,0xEA,0x47,
  37689. 0xA1,0xAF,0x06,0x79,0x1A,0x52,0x6F,0x19,
  37690. 0x31,0x70,0x71,0xBD,0xC2,0x61,0x8D,0xB7,
  37691. 0xEE,0x6B,0x69,0x2A,0xE8,0x21,0x7A,0x95,
  37692. 0xBE,0x86,0x2A,0xA1,0xF4,0xE2,0x2F,0x17,
  37693. 0x02,0xFD,0xAD,0x17,0x9F,0x0A,0x0A,0x78,
  37694. 0xA9,0x92,0x30,0x21,0x72,0x2B,0x28,0xF8,
  37695. 0xF2,0x3E,0x05,0xD5,0xAC,0xC0,0x82,0xF8,
  37696. 0xD2,0xDA,0xD0,0xA3,0xBC,0x93,0xDB,0xA5,
  37697. 0x46,0xDE,0x14,0x1E,0xD4,0x3A,0x5D,0x79,
  37698. 0x3D,0x31,0x4B,0x06,0xCE,0x22,0x29,0x3C,
  37699. 0x98,0xB6,0x18,0x8A,0xAE,0xF7,0xBA,0x22,
  37700. 0x88,0xA1,0xEE,0xC0,0x14,0x4C,0x4A,0xA0,
  37701. 0x57,0x0A,0xD3,0x18,0xA2,0x3D,0xDD,0xC7,
  37702. 0x83,0x73,0xFC,0x38,0x9B,0x31,0xA3,0xE1,
  37703. 0x17,0x76,0xA1,0xA2,0x69,0xFC,0xAB,0x08,
  37704. 0x80,0x72,0x8D,0xF5,0xE4,0x14,0xB7,0x6B,
  37705. 0x03,0xFF,0xE8,0x11,0x4B,0x06,0x55,0x7E,
  37706. 0x36,0x21,0x2F,0xD7,0x54,0x82,0xC9,0x31,
  37707. 0xB4,0x85,0x68,0x41,0xEF,0x75,0xB0,0x3A,
  37708. 0xEA,0x4F,0xE0,0xEC,0x72,0xCC,0x33,0x96,
  37709. 0xCE,0x7D,0xAD,0xDD,0x0D,0x27,0x05,0x6E,
  37710. 0xA2,0xD4,0x11,0x07,0xD8,0x7D,0x27,0xD4,
  37711. 0x80,0x8F,0x00,0x22,0xE4,0xFC,0x2C,0x9D,
  37712. 0xD5,0xD8,0x18,0x7F,0x4E,0xF4,0xB9,0x7F,
  37713. 0xEF,0xD6,0x00,0x08,0x5C,0x05,0x04,0x1E,
  37714. 0x9A,0xC6,0x8D,0xCC,0x19,0xD9,0x0B,0x06,
  37715. 0xCC,0x6A,0x17,0xE2,0x03,0x23,0xDB,0x1C,
  37716. 0xBC,0xA2,0xB9,0xA2,0x95,0x3C,0x73,0xD8,
  37717. 0xFF,0xE6,0x0E,0xAE,0x04,0xB2,0xFC,0x91,
  37718. 0x4F,0xEF,0x8A,0x58,0xB7,0x31,0x68,0x4C,
  37719. 0x1E,0xD0,0x5B,0x85,0xCC,0x03,0xDC,0xF4,
  37720. 0xAC,0xDB,0x03,0x9B,0x35,0x33,0x08,0x71,
  37721. 0xD0,0x50,0x8D,0xDC,0xE3,0x3A,0x98,0x40,
  37722. 0x41,0x80,0xDD,0x35,0xE1,0xA2,0xAF,0x14,
  37723. 0x9A,0xDB,0xD3,0x68,0x14,0xE2,0x50,0x7A,
  37724. 0x76,0x3F,0xE4,0xA4,0x1B,0xAA,0xC1,0x06,
  37725. 0x87,0x9A,0x92,0xF9,0xBE,0x9E,0x86,0x8C,
  37726. 0x92,0x1D,0x74,0xB1,0x7F,0x27,0x43,0xC0,
  37727. 0xEE,0x2E,0xC2,0x6C,0x6D,0xAA,0x0C,0x0E,
  37728. 0x71,0xC9,0x56,0xD6,0x3A,0x56,0xCB,0x90,
  37729. 0xD1,0x7E,0x6E,0x1C,0x6A,0x00,0x2D,0x02,
  37730. 0x2C,0x96,0xF0,0x2A,0x37,0x37,0x18,0x07,
  37731. 0x0B,0xF4,0xB4,0x8C,0x30,0xF2,0xA4,0xAB,
  37732. 0x66,0xFB,0x8B,0x22,0xC0,0x00,0x7E,0x05,
  37733. 0xB6,0xF9,0x95,0x49,0x33,0xA1,0xDC,0x97,
  37734. 0x0C,0x5C,0x61,0x46,0xE2,0xD7,0x87,0x4B,
  37735. 0xC4,0xC7,0x5F,0x26,0x06,0x84,0xD7,0x47,
  37736. 0x05,0xF1,0x33,0xFF,0x85,0x85,0xB2,0xBD,
  37737. 0x1F,0x44,0xC6,0xC2,0x7D,0x51,0xBE,0x0E,
  37738. 0xB5,0xC4,0x44,0x2F,0xFE,0x73,0x5F,0xF4,
  37739. 0xA4,0xEF,0xE2,0xF1,0x73,0x0B,0xEF,0x3E,
  37740. 0x2B,0xD7,0xCC,0x9F,0xDA,0x1A,0x7E,0x92,
  37741. 0x39,0xA1,0x55,0xBF,0x60,0x0A,0xDB,0x23,
  37742. 0x74,0xFE,0xE7,0x05,0x63,0xA9,0x85,0x52,
  37743. 0x9F,0xCC,0xC3,0xFF,0xF6,0x6C,0x1B,0x4E,
  37744. 0x4F,0x01,0xBD,0xC3,0xEB,0x37,0xEC,0x29,
  37745. 0x21,0x3B,0x2C,0xC9,0x2E,0x93,0x20,0x3E,
  37746. 0x19,0xC0,0x8B,0xE8,0x33,0xCD,0xC6,0x6A,
  37747. 0x6E,0x72,0x13,0x15,0xA1,0x90,0x20,0x0C,
  37748. 0x14,0x66,0xED,0xCC,0xA4,0xDD,0x7F,0x58,
  37749. 0x53,0xBC,0x4A,0x68,0xFC,0x86,0x3E,0xAA,
  37750. 0xF1,0x17,0x0F,0x3E,0x20,0x54,0x93,0xF4,
  37751. 0x98,0xBF,0xB4,0x07,0x05,0xBD,0x70,0xE7,
  37752. 0xD7,0x34,0xFD,0xE3,0x69,0xDF,0xCD,0xF5,
  37753. 0x1A,0x73,0x6E,0xC9,0x2B,0x21,0xFB,0xB8,
  37754. 0x7E,0x44,0x10,0x83,0x56,0xCE,0xD5,0x15,
  37755. 0x9A,0x75,0xFC,0x91,0x8E,0x6B,0x9E,0x1A,
  37756. 0x3A,0x33,0x39,0x35,0xB4,0x0D,0x74,0xF4,
  37757. 0xFB,0x4C,0x0E,0x37,0xFE,0x82,0x95,0x46,
  37758. 0x6B,0xD2,0x6E,0xEE,0xCD,0x4D,0x38,0xAF,
  37759. 0x0A,0xAA,0xF1,0xD5,0xA4,0x7C,0x04,0xD8,
  37760. 0xB9,0xDB,0x11,0x68,0x88,0x35,0x41,0xDE,
  37761. 0x31,0x33,0x0C,0xDC,0x2D,0x4C,0xA8,0x20,
  37762. 0xCC,0x2C,0x4C,0x63,0xAB,0xBA,0xDF,0x48,
  37763. 0x84,0xD5,0x25,0xBC,0x70,0xE3,0x49,0xAA,
  37764. 0x43,0xCA,0x8B,0xE7,0x9F,0xDD,0x20,0x76,
  37765. 0x9B,0x38,0xF4,0xBA,0x4D,0x4E,0x34,0x4A,
  37766. 0xAF,0x81,0xE7,0x0B,0xEC,0xE9,0x59,0xC1,
  37767. 0x35,0x22,0x7F,0x69,0x46,0x62,0xD2,0x18,
  37768. 0x6E,0x1F,0x79,0xD1,0xAD,0xC3,0x84,0x95,
  37769. 0x96,0xB2,0x18,0x58,0x5E,0x7E,0x0C,0x25,
  37770. 0x0A,0x0F,0x69,0xA3,0x1D,0xEC,0x29,0xCB,
  37771. 0xDA,0xA2,0xD1,0x1A,0x10,0xA5,0x52,0xC3,
  37772. 0x62,0x1E,0xC5,0x83,0xFF,0xA3,0x56,0xC2,
  37773. 0xFD,0x87,0x3B,0x57,0x52,0x98,0x36,0x95,
  37774. 0x77,0x6B,0xE5,0x49,0x10,0x8E,0x39,0xDD,
  37775. 0xCA,0x4B,0xB3,0x9F,0x4C,0x0C,0x11,0x62,
  37776. 0xF3,0x22,0x78,0xDB,0x48,0xEB,0x68,0xFE,
  37777. 0xE4,0x2A,0xE9,0xAA,0x8F,0x7A,0x2F,0x69,
  37778. 0xA5,0xC5,0x03,0x2D,0xEF,0x62,0xA8,0x71,
  37779. 0x65,0x06,0x40,0x84,0x10,0x0F,0xF2,0xED,
  37780. 0xBC,0x70,0x71,0x69,0x24,0xA2,0xBF,0x83,
  37781. 0x39,0xDD,0xFA,0xA2,0x7B,0xE5,0xEC,0x3D,
  37782. 0xFE,0x3B,0x52,0x6E,0x3D,0x82,0xA6,0x2A,
  37783. 0x86,0x01,0x61,0x51,0x63,0xBF,0xF9,0x0A,
  37784. 0x06,0x72,0xF1,0xD5,0x39,0x0C,0xBA,0xC9,
  37785. 0x78,0xC6,0x77,0x22,0xE4,0x96,0x6E,0xB1,
  37786. 0x48,0x62,0x84,0x62,0x2D,0xEA,0x49,0x56,
  37787. 0x50,0x86,0x3F,0x90,0xC3,0x01,0x42,0x45,
  37788. 0xED,0xE6,0x9A,0x65,0x19,0x93,0x7F,0x48,
  37789. 0x16,0xF2,0x50,0xA7,0x70,0xB3,0xF5,0xDB,
  37790. 0x0E,0x5E,0x22,0x9E,0x64,0x04,0x26,0x69,
  37791. 0xC1,0x16,0xEE,0x65,0x08,0x82,0x27,0x65,
  37792. 0xEC,0x3D,0xDF,0x51,0x5E,0x2D,0xE8,0x76,
  37793. 0xF2,0xE3,0xE4,0x24,0x04,0x88,0x06,0x0F,
  37794. 0xB2,0x7B,0x9B,0x72,0x3D,0x4C,0x7D,0x6A,
  37795. 0x1F,0xB2,0xA2,0xD2,0x35,0xD6,0x40,0x25,
  37796. 0xC2,0x0B,0x25,0xF9,0xDF,0x26,0xE4,0xDC,
  37797. 0xFB,0xB1,0x84,0x84,0x77,0x1B,0x45,0x51,
  37798. 0x60,0xD5,0xF0,0xB6,0x09,0xE6,0xBC,0xE3,
  37799. 0x1C,0x70,0x96,0x2C,0xD3,0x9D,0x7D,0x7F,
  37800. 0xB1,0x70,0xDA,0x79,0xB8,0x74,0x99,0xBF,
  37801. 0x84,0x95,0xCC,0x93,0xD7,0x51,0xDD,0x66,
  37802. 0xD3,0x70,0x0C,0x75,0x86,0x09,0x06,0xFD,
  37803. 0x66,0x14,0x80,0xCD,0xF3,0x59,0xB4,0x92,
  37804. 0x5F,0xE4,0xEE,0x00,0xA8,0xB0,0x8B,0x5C,
  37805. 0x3E,0xDB,0x8A,0x9C,0x0B,0xB5,0x99,0xC2,
  37806. 0x0D,0x81,0x09,0x06,0x6C,0x28,0xC0,0x7E,
  37807. 0xA5,0x07,0x70,0x64,0xD7,0x41,0xF4,0xC3,
  37808. 0x66,0x61,0x1C,0xA8,0x51,0xF6,0x3C,0xBA,
  37809. 0xE0,0x94,0xA3,0x11,0x8C,0x2E,0xBA,0x13,
  37810. 0xB2,0x47,0x48,0x93,0xB4,0x1A,0x2C,0x9A,
  37811. 0x6E,0x8E,0x30,0x66,0x7B,0xD3,0xBB,0x3B,
  37812. 0x5D,0x97,0x0D,0xE4,0xEA,0x24,0x28,0x9E,
  37813. 0xB4,0x88,0xCE,0x1D,0x7D,0x6F,0x39,0xB3,
  37814. 0x87,0x21,0xE5,0x08,0x93,0xF0,0xD4,0x9D,
  37815. 0x2D,0x91,0xC9,0xFD,0x0C,0x74,0x34,0xB4,
  37816. 0x1F,0xFE,0xDA,0xDC,0x10,0x5B,0x8D,0x2B,
  37817. 0x87,0xD3,0x42,0xB4,0xAE,0x32,0x9C,0xAE,
  37818. 0x4C,0x99,0xD8,0xED,0x44,0x41,0x07,0xE0,
  37819. 0x8F,0xBD,0xA5,0x7C,0x5A,0xDF,0x91,0x29,
  37820. 0x00,0xB5,0x4B,0xC3,0x3A,0x40,0x6C,0x48,
  37821. 0xAB,0x2A,0xF3,0x02,0xCB,0xB3,0x69,0xDA,
  37822. 0x06,0x0C,0x4D,0x5C,0x45,0xC3,0x28,0xAC,
  37823. 0x7A,0x01,0xD4,0xF8,0xCB,0x07,0x63,0x89,
  37824. 0x09,0x34,0x78,0xA7,0x14,0x39,0xCF,0x2D,
  37825. 0x94,0x8D,0x7A,0x4E,0x4E,0xBD,0xC4,0x32,
  37826. 0xAB,0x21,0xC9,0xDA,0x3F,0x5F,0x04,0x6B,
  37827. 0x14,0x40,0x18,0x18,0x2F,0xF9,0x46,0x17,
  37828. 0x57,0x54,0x9B,0x28,0x7B,0xBD,0xF9,0xA2,
  37829. 0x13,0xAC,0x69,0x24,0xB1,0x31,0x39,0xBF,
  37830. 0x8D,0x75,0xC3,0xFD,0x03,0x54,0x5A,0xFD,
  37831. 0xD4,0x7A,0xB7,0x56,0x4F,0x66,0x43,0x57,
  37832. 0x1B,0xFB,0xF9,0x92,0x7A,0x83,0xE6,0xFF,
  37833. 0xB4,0xBA,0x83,0xD2,0x61,0x8E,0x4A,0x82,
  37834. 0x82,0xA8,0xF5,0x0C,0xD2,0x43,0x53,0xA8,
  37835. 0x85,0x0A,0xD4,0x69,0x7B,0x04,0x71,0x3B,
  37836. 0x80,0x49,0x27,0x47,0x12,0xB6,0xB0,0xEA,
  37837. 0x90,0x0A,0xFA,0xA8,0xC8,0x78,0x61,0xDE,
  37838. 0x30,0x12,0xBB,0xDC,0xA6,0x57,0x56,0x30,
  37839. 0x6E,0xF1,0xA8,0x3B,0xF6,0x09,0x07,0xEA,
  37840. 0x31,0xE2,0x08,0x23,0x31,0x0F,0xD4,0x34,
  37841. 0xE3,0x60,0xC2,0x2B,0xDB,0x5A,0x99,0xCF,
  37842. 0xD4,0x6B,0x4E,0x75,0x65,0x35,0xE8,0x8B,
  37843. 0x93,0x7D,0xCA,0x11,0x47,0xF0,0x3E,0x11,
  37844. 0x5C,0xD1,0xEE,0x4B,0x11,0xB4,0x65,0x2B,
  37845. 0x6B,0x79,0xC0,0x86,0x60,0xA4,0x4B,0x24,
  37846. 0xA0,0x5C,0x70,0x34,0xC3,0x7C,0xE7,0x4F,
  37847. 0x97,0x89,0x4D,0xFE,0x22,0x89,0x3A,0xE9,
  37848. 0x07,0xB9,0x1A,0x86,0xB8,0x7A,0x12,0x38,
  37849. 0xE1,0x24,0x46,0xBC,0x9B,0x21,0xCD,0xAC,
  37850. 0x30,0xAB,0x98,0x21,0x31,0xC5,0x17,0x3F,
  37851. 0x1E,0x56,0xC3,0x18,0xCE,0xF0,0xA1,0xCC,
  37852. 0xFF,0x9D,0xA8,0x53,0xAF,0x74,0x77,0x54,
  37853. 0x02,0x9A,0x8F,0xA4,0xD4,0xBD,0xB2,0x1A,
  37854. 0xBA,0x52,0x2E,0x19,0xBE,0x49,0x11,0x45,
  37855. 0x02,0x01,0x7A,0xBF,0x28,0xD6,0x18,0xED,
  37856. 0xBD,0xCE,0xE4,0xDE,0xB5,0xF1,0x53,0x5D,
  37857. 0x65,0xF9,0x5F,0x83,0x8F,0x2D,0xF2,0x82,
  37858. 0xA0,0x2D,0x28,0xD3,0x0A,0x9E,0x0F,0x7F,
  37859. 0xC7,0xC4,0x43,0x7F,0xC3,0x0E,0x06,0xEB,
  37860. 0x4E,0xB4,0x2D,0xFA,0xDD,0x48,0xAB,0xF4,
  37861. 0x7D,0x41,0x48,0x33,0x5A,0xE6,0x70,0x02,
  37862. 0xE7,0x71,0x8D,0xD9,0x6B,0x0C,0x5A,0x8F,
  37863. 0xA4,0xC1,0xB7,0x4E,0x96,0x83,0xD6,0xA7,
  37864. 0x1D,0xF1,0x88,0xB3,0x6E,0xF4,0x12,0xA9,
  37865. 0xF6,0x31,0x69,0x66,0xFE,0xFE,0x02,0xF2,
  37866. 0x86,0x6D,0xBB,0x57,0x51,0x8C,0x4C,0xE9,
  37867. 0x7C,0x92,0x3E,0x3A,0xD3,0x2D,0xA8,0x82,
  37868. 0x53,0x84,0x26,0x89,0xBB,0xCC,0x13,0x12,
  37869. 0x3D,0x94,0xBB,0xDF,0x3D,0x4C,0xDF,0x27,
  37870. 0x9B,0x1F,0xB8,0xB6,0xE4,0xEA,0xA2,0x07,
  37871. 0xF8,0x4D,0x42,0x8F,0x29,0x90,0xFE,0x21,
  37872. 0x20,0xE9,0x55,0x02,0xAD,0x90,0xA7,0x77,
  37873. 0x4E,0x29,0xB6,0xD9,0x14,0x94,0xB2,0x25,
  37874. 0xA4,0xB2,0x0E,0x96,0x31,0xAB,0x9E,0x93,
  37875. 0x49,0xAC,0xA9,0xCB,0x68,0x22,0xBA,0xB8,
  37876. 0x57,0x5C,0x9D,0x65,0xC1,0xF1,0xFC,0x99,
  37877. 0x7C,0x3C,0xE9,0xEA,0x4B,0x29,0x22,0x2F,
  37878. 0xDB,0x17,0x21,0x8D,0xB0,0x13,0xBF,0xEE,
  37879. 0x7D,0xE4,0x8B,0x6D,0x17,0xE0,0x53,0x92,
  37880. 0x0B,0x32,0x6B,0xB1,0x65,0x2E,0xA7,0x83,
  37881. 0xFD,0x62,0x62,0xE3,0xAA,0x81,0xE8,0xD6,
  37882. 0xF7,0xB1,0x30,0x65,0x80,0x9F,0x77,0x1E,
  37883. 0x4A,0xEA,0xE8,0x45,0x32,0x12,0x3A,0xFB,
  37884. 0x22,0xE9,0xA9,0xF6,0xCB,0xAB,0xA8,0x0C,
  37885. 0x20,0xA8,0x7C,0xF9,0xF7,0x53,0xC1,0xB4,
  37886. 0xC0,0x5D,0x06,0x45,0xDD,0x7E,0xA7,0x34,
  37887. 0xA1,0x21,0xC2,0x62,0xAB,0x22,0x45,0x3D,
  37888. 0x73,0x4C,0x26,0xD1,0x1A,0xB2,0xF0,0xB2,
  37889. 0x6D,0x11,0x70,0x58,0xAA,0xF5,0xA4,0xF5,
  37890. 0xF8,0x0B,0x3D,0xC1,0xF6,0x17,0x70,0x15,
  37891. 0xCD,0x72,0x02,0x7E,0x4E,0x94,0x96,0x0A,
  37892. 0x56,0xCC,0xA5,0xA3,0xB3,0x7E,0xDD,0x5A,
  37893. 0x72,0xD2,0xFB,0xAC,0x3D,0x0E,0x66,0x65,
  37894. 0xE9,0x08,0x6C,0xB0,0x1C,0xE2,0x1A,0x82,
  37895. 0xF6,0xF3,0x34,0x89,0x73,0x02,0x5B,0x42,
  37896. 0x6D,0x40,0x61,0xB6,0xE0,0xE6,0x53,0x32,
  37897. 0xA5,0x72,0x17,0x4F,0x3B,0x51,0x4F,0xBC,
  37898. 0x00,0xE0,0x69,0x26,0xA9,0xAE,0x83,0xE3,
  37899. 0x73,0x7F,0x71,0x97,0xE0,0xDC,0x7C,0x63,
  37900. 0x9C,0x85,0x5F,0xDF,0x7D,0xE4,0x6C,0xD8,
  37901. 0xA9,0x3A,0x6F,0x5E,0x4A,0x2E,0xB0,0xE7,
  37902. 0x8B,0x45,0xE2,0x90,0x05,0x37,0xE8,0xAB,
  37903. 0x49,0x48,0x4C,0xC0,0x59,0x1D,0x8C,0x46,
  37904. 0x5B,0x84,0xE0,0x83,0xCE,0xEA,0x4B,0xF9,
  37905. 0xD4,0xDC,0x63,0xDF,0x79,0xB7,0x5C,0x11,
  37906. 0x25,0x7F,0x90,0x2E,0x0A,0x38,0x03,0xEA,
  37907. 0xEA,0xA1,0x26,0x52,0x20,0x19,0xA3,0xBE,
  37908. 0xFC,0x9D,0xB7,0x6E,0xA6,0x58,0x8E,0x6D,
  37909. 0xC5,0x58,0xE9,0xED,0x2F,0x55,0x43,0x8B,
  37910. 0x03,0x8B,0xE6,0xA4,0xC2,0x25,0x4B,0x36,
  37911. 0xBA,0xD3,0x27,0x48,0x40,0x2E,0x87,0xA2,
  37912. 0xD4,0x12,0xC6,0x05,0x36,0x03,0x11,0x51,
  37913. 0xD1,0xF2,0xAC,0x71,0x2C,0xB6,0xC3,0xA5,
  37914. 0x57,0x0F,0xAF,0x4B,0xBD,0xCD,0x47,0x4C,
  37915. 0x3A,0x52,0x6F,0x47,0xE7,0x0B,0xB7,0xD5,
  37916. 0xF7,0xA6,0x39,0x63,0x82,0x08,0x4C,0x41,
  37917. 0x0E,0x2A,0x52,0x42,0x5A,0xEA,0x59,0xC7,
  37918. 0x94,0xFB,0xD0,0x88,0x47,0x27,0xF6,0x97,
  37919. 0x03,0x9E,0x29,0xB8,0x3A,0x67,0xE6,0xF3,
  37920. 0x95,0xA7,0x42,0xC1,0x96,0xD1,0x9A,0xA6,
  37921. 0xF0,0x09,0x0C,0xEA,0xE0,0xAB,0x0F,0x15,
  37922. 0xE9,0xC3,0xEB,0xA5,0x89,0x86,0x98,0x32,
  37923. 0x83,0xAB,0x30,0x33,0xAE,0x90,0x8D,0x2E,
  37924. 0xB3,0xAA,0x91,0xA6,0xD9,0xA4,0x4A,0x54,
  37925. 0xE0,0xD3,0x08,0xCC,0x79,0xCE,0xE4,0x15,
  37926. 0x31,0xA6,0xCE,0x61,0xCF,0x03,0x06,0xEE,
  37927. 0x8E,0xE2,0x64,0x29,0xD1,0x54,0x9B,0xD0,
  37928. 0x5F,0x09,0x2B,0x8B,0xD5,0xF8,0xD4,0x7D,
  37929. 0xF1,0x97,0x32,0xD9,0xEA,0x5A,0x0E,0x10,
  37930. 0x8C,0x4D,0xFB,0x55,0xE6,0x27,0x0C,0xBA,
  37931. 0xC1,0x73,0xC1,0x73,0xE3,0x1C,0x09,0xB3,
  37932. 0x6F,0xB4,0x12,0xFA,0xF3,0x29,0xDC,0x23,
  37933. 0x32,0xED,0x80,0x87,0x83,0xC2,0xF6,0x07,
  37934. 0xB5,0xA9,0x22,0xDE,0x66,0x1A,0xA7,0x4A,
  37935. 0x86,0xF1,0x39,0x9B,0xF4,0xE7,0x50,0x15,
  37936. 0x4A,0x55,0x3C,0x93,0xB9,0xF9,0xFD,0xDC,
  37937. 0xB3,0x5D,0x73,0x52
  37938. };
  37939. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t xmss_test_verify_only(void)
  37940. {
  37941. XmssKey verifyKey;
  37942. unsigned char pub_raw[XMSS_SHA256_PUBLEN];
  37943. word32 pub_len = sizeof(pub_raw);
  37944. word32 pkSz = 0;
  37945. word32 sigSz = 0;
  37946. const char * param = "XMSS-SHA2_10_256";
  37947. int j = 0;
  37948. int ret2 = WC_TEST_RET_ENC_NC;
  37949. int ret = WC_TEST_RET_ENC_NC;
  37950. int n_diff = 0;
  37951. WOLFSSL_ENTER("xmss_test_verify_only");
  37952. XMEMSET(pub_raw, 0, sizeof(pub_raw));
  37953. ret = wc_XmssKey_Init(&verifyKey, NULL, INVALID_DEVID);
  37954. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  37955. ret = wc_XmssKey_SetParamStr(&verifyKey, param);
  37956. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  37957. ret = wc_XmssKey_GetPubLen(&verifyKey, &pkSz);
  37958. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  37959. if (pkSz != XMSS_SHA256_PUBLEN) {
  37960. return WC_TEST_RET_ENC_EC(pkSz);
  37961. }
  37962. ret = wc_XmssKey_GetSigLen(&verifyKey, &sigSz);
  37963. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  37964. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  37965. fprintf(stderr, "param: %s\n", param);
  37966. fprintf(stderr, "pkSz: %d\n", pkSz);
  37967. fprintf(stderr, "sigSz: %d\n", sigSz);
  37968. #endif
  37969. if (sigSz != sizeof(xmss_sig)) {
  37970. return WC_TEST_RET_ENC_EC(sigSz);
  37971. }
  37972. ret = wc_XmssKey_ImportPubRaw(&verifyKey, xmss_pub, XMSS_SHA256_PUBLEN);
  37973. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  37974. ret = wc_XmssKey_Verify(&verifyKey, xmss_sig, sizeof(xmss_sig),
  37975. (byte *) xmss_msg, sizeof(xmss_msg));
  37976. if (ret != 0) {
  37977. printf("error: wc_XmssKey_Verify returned %d, expected 0\n", ret);
  37978. return WC_TEST_RET_ENC_EC(ret);
  37979. }
  37980. /* Now test the ExportPubRaw API, verify we recover the original pub. */
  37981. ret = wc_XmssKey_ExportPubRaw(&verifyKey, pub_raw, &pub_len);
  37982. if (ret != 0) {
  37983. printf("error: wc_XmssKey_ExportPubRaw returned %d, expected 0\n", ret);
  37984. return WC_TEST_RET_ENC_EC(ret);
  37985. }
  37986. if (pub_len != XMSS_SHA256_PUBLEN) {
  37987. printf("error: xmss pub len %u, expected %d\n", pub_len,
  37988. XMSS_SHA256_PUBLEN);
  37989. return WC_TEST_RET_ENC_EC(pub_len);
  37990. }
  37991. n_diff = XMEMCMP(pub_raw, xmss_pub, sizeof(xmss_pub));
  37992. if (n_diff != 0) {
  37993. printf("error: exported and imported pub raw do not match: %d\n",
  37994. n_diff);
  37995. return WC_TEST_RET_ENC_EC(n_diff);
  37996. }
  37997. /* Flip bits in message. This should fail. */
  37998. xmss_msg[sizeof(xmss_msg) / 2] ^= 1;
  37999. ret2 = wc_XmssKey_Verify(&verifyKey, xmss_sig, sizeof(xmss_sig),
  38000. (byte *) xmss_msg, sizeof(xmss_msg));
  38001. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  38002. printf("error: wc_XmssKey_Verify returned %d, expected -1\n", ret2);
  38003. return WC_TEST_RET_ENC_EC(ret2);
  38004. }
  38005. /* Flip it back. This should pass again. */
  38006. xmss_msg[sizeof(xmss_msg) / 2] ^= 1;
  38007. ret = wc_XmssKey_Verify(&verifyKey, xmss_sig, sizeof(xmss_sig),
  38008. (byte *) xmss_msg, sizeof(xmss_msg));
  38009. if (ret != 0) {
  38010. printf("error: wc_XmssKey_Verify returned %d, expected 0\n", ret);
  38011. return WC_TEST_RET_ENC_EC(ret);
  38012. }
  38013. /* Flip bits in a few places throughout the signature, stepping in multiple
  38014. * of hash size. These should all fail with -1. */
  38015. for (j = 0; j < (int) sizeof(xmss_sig); j+= 4 * 32) {
  38016. xmss_sig[j] ^= 1;
  38017. ret2 = wc_XmssKey_Verify(&verifyKey, xmss_sig, sizeof(xmss_sig),
  38018. (byte *) xmss_msg, sizeof(xmss_msg));
  38019. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  38020. /* Verify passed when it should have failed. */
  38021. return WC_TEST_RET_ENC_I(j);
  38022. }
  38023. /* Flip this spot back. */
  38024. xmss_sig[j] ^= 1;
  38025. }
  38026. /* Cleanup everything. */
  38027. wc_XmssKey_Free(&verifyKey);
  38028. return ret;
  38029. }
  38030. #endif /* WOLFSSL_HAVE_XMSS && !WOLFSSL_SMALL_STACK &&
  38031. * WOLFSSL_XMSS_MIN_HEIGHT <= 10 */
  38032. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY)
  38033. static int lms_write_key_mem(const byte * priv, word32 privSz, void *context)
  38034. {
  38035. /* WARNING: THIS IS AN INSECURE WRITE CALLBACK THAT SHOULD ONLY
  38036. * BE USED FOR TESTING PURPOSES! Production applications should
  38037. * write only to non-volatile storage. */
  38038. XMEMCPY(context, priv, privSz);
  38039. return WC_LMS_RC_SAVED_TO_NV_MEMORY;
  38040. }
  38041. static int lms_read_key_mem(byte * priv, word32 privSz, void *context)
  38042. {
  38043. /* WARNING: THIS IS AN INSECURE READ CALLBACK THAT SHOULD ONLY
  38044. * BE USED FOR TESTING PURPOSES! */
  38045. XMEMCPY(priv, context, privSz);
  38046. return WC_LMS_RC_READ_TO_MEMORY;
  38047. }
  38048. /* LMS signature sizes are a function of their parameters. This
  38049. * test has a signature of 8688 bytes. */
  38050. #define WC_TEST_LMS_SIG_LEN (8688)
  38051. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t lms_test(void)
  38052. {
  38053. int i = 0;
  38054. int j = 0;
  38055. int ret = WC_TEST_RET_ENC_NC;
  38056. int ret2 = WC_TEST_RET_ENC_NC;
  38057. int sigsLeft = 0;
  38058. LmsKey signingKey;
  38059. LmsKey verifyKey;
  38060. WC_RNG rng;
  38061. word32 sigSz = 0;
  38062. const char * msg = "LMS HSS post quantum signature test";
  38063. word32 msgSz = (word32) XSTRLEN(msg);
  38064. unsigned char priv[HSS_MAX_PRIVATE_KEY_LEN];
  38065. unsigned char old_priv[HSS_MAX_PRIVATE_KEY_LEN];
  38066. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38067. byte * sig = XMALLOC(WC_TEST_LMS_SIG_LEN, HEAP_HINT,
  38068. DYNAMIC_TYPE_TMP_BUFFER);
  38069. if (sig == NULL) {
  38070. return WC_TEST_RET_ENC_ERRNO;
  38071. }
  38072. #else
  38073. byte sig[WC_TEST_LMS_SIG_LEN];
  38074. #endif
  38075. WOLFSSL_ENTER("lms_test");
  38076. XMEMSET(priv, 0, sizeof(priv));
  38077. XMEMSET(old_priv, 0, sizeof(old_priv));
  38078. XMEMSET(sig, 0, WC_TEST_LMS_SIG_LEN);
  38079. #ifndef HAVE_FIPS
  38080. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  38081. #else
  38082. ret = wc_InitRng(&rng);
  38083. #endif
  38084. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38085. /* This test:
  38086. * levels: 1
  38087. * height: 5
  38088. * winternitz: 1
  38089. *
  38090. * max sigs: 2 ** (1 * 5) = 32
  38091. * signature length: 8688
  38092. */
  38093. ret = wc_LmsKey_Init(&signingKey, NULL, INVALID_DEVID);
  38094. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38095. ret = wc_LmsKey_Init(&verifyKey, NULL, INVALID_DEVID);
  38096. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38097. ret = wc_LmsKey_SetParameters(&signingKey, 1, 5, 1);
  38098. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38099. ret = wc_LmsKey_SetWriteCb(&signingKey, lms_write_key_mem);
  38100. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38101. ret = wc_LmsKey_SetReadCb(&signingKey, lms_read_key_mem);
  38102. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38103. ret = wc_LmsKey_SetContext(&signingKey, (void *) priv);
  38104. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38105. ret = wc_LmsKey_MakeKey(&signingKey, &rng);
  38106. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38107. XMEMCPY(old_priv, priv, sizeof(priv));
  38108. ret = wc_LmsKey_ExportPub(&verifyKey, &signingKey);
  38109. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38110. ret = wc_LmsKey_GetSigLen(&verifyKey, &sigSz);
  38111. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38112. if (sigSz != WC_TEST_LMS_SIG_LEN) {
  38113. printf("error: got %u, expected %d\n", sigSz, WC_TEST_LMS_SIG_LEN);
  38114. return WC_TEST_RET_ENC_EC(sigSz);
  38115. }
  38116. /* 2 ** 5 should be the max number of signatures */
  38117. for (i = 0; i < 32; ++i) {
  38118. /* We should have remaining signstures. */
  38119. sigsLeft = wc_LmsKey_SigsLeft(&signingKey);
  38120. if (sigsLeft == 0) {
  38121. return WC_TEST_RET_ENC_EC(sigsLeft);
  38122. }
  38123. /* Sign with key. The private key will be updated on every signature. */
  38124. ret = wc_LmsKey_Sign(&signingKey, sig, &sigSz, (byte *) msg, msgSz);
  38125. if (ret != 0) { return WC_TEST_RET_ENC_I(i); }
  38126. /* The updated private key should not match the old one. */
  38127. if (XMEMCMP(old_priv, priv, sizeof(priv)) == 0) {
  38128. printf("error: current priv key should not match old: %d\n", i);
  38129. return WC_TEST_RET_ENC_I(i);
  38130. }
  38131. XMEMCPY(old_priv, priv, sizeof(priv));
  38132. ret = wc_LmsKey_Verify(&verifyKey, sig, sigSz, (byte *) msg, msgSz);
  38133. if (ret != 0) { return WC_TEST_RET_ENC_I(i); }
  38134. /* Flip bits in a few places throughout the signature, stepping in multiple
  38135. * of hash size. These should all fail with -1. */
  38136. for (j = 0; j < (int) sigSz; j+= 4 * 32) {
  38137. sig[j] ^= 1;
  38138. ret2 = wc_LmsKey_Verify(&verifyKey, sig, sigSz, (byte *) msg,
  38139. msgSz);
  38140. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  38141. /* Verify passed when it should have failed. */
  38142. return WC_TEST_RET_ENC_I(j);
  38143. }
  38144. /* Flip this spot back. */
  38145. sig[j] ^= 1;
  38146. }
  38147. }
  38148. /* This should be the last signature. */
  38149. sigsLeft = wc_LmsKey_SigsLeft(&signingKey);
  38150. if (sigsLeft != 0) {
  38151. return WC_TEST_RET_ENC_EC(sigsLeft);
  38152. }
  38153. wc_LmsKey_Free(&signingKey);
  38154. wc_LmsKey_Free(&verifyKey);
  38155. wc_FreeRng(&rng);
  38156. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38157. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38158. #endif
  38159. return ret;
  38160. }
  38161. #endif /* if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY) */
  38162. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_SMALL_STACK)
  38163. #if (defined(WOLFSSL_WC_LMS) && (LMS_MAX_HEIGHT >= 10)) || \
  38164. defined(HAVE_LIBLMS)
  38165. /* A simple LMS verify only test.
  38166. *
  38167. * Note: LMS signature sizes are a function of their parameters. This
  38168. * test has a signature of 1456 bytes:
  38169. * levels: 1
  38170. * height: 10
  38171. * winternitz: 8
  38172. * max sigs: 2 ** (1 * 10) = 1024
  38173. * signature length: 1456
  38174. * */
  38175. /* "wolfSSL LMS example message!" without null terminator. */
  38176. static byte lms_msg[28] =
  38177. {
  38178. 0x77,0x6F,0x6C,0x66,0x53,0x53,0x4C,0x20,
  38179. 0x4C,0x4D,0x53,0x20,0x65,0x78,0x61,0x6D,
  38180. 0x70,0x6C,0x65,0x20,0x6D,0x65,0x73,0x73,
  38181. 0x61,0x67,0x65,0x21
  38182. };
  38183. static const byte lms_L1H10W8_pub[HSS_MAX_PUBLIC_KEY_LEN] =
  38184. {
  38185. 0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x06,
  38186. 0x00,0x00,0x00,0x04,0xA1,0x26,0x76,0xF8,
  38187. 0xBB,0x0B,0xC0,0x82,0x21,0x71,0x0B,0x2E,
  38188. 0x8C,0xA6,0xEF,0x12,0xED,0x41,0x0E,0x8C,
  38189. 0xAF,0x11,0x93,0x34,0x7B,0x49,0x79,0xB7,
  38190. 0xDE,0x63,0x1C,0xFE,0x1F,0xD1,0x17,0x49,
  38191. 0xCD,0x5C,0xD4,0x26,0xA0,0x53,0x26,0x1A,
  38192. 0xC5,0xB4,0x8F,0x23
  38193. };
  38194. #define LMS_L1H10W8_SIGLEN (1456)
  38195. static byte lms_L1H10W8_sig[LMS_L1H10W8_SIGLEN] =
  38196. {
  38197. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
  38198. 0x00,0x00,0x00,0x04,0x18,0x70,0x09,0x2E,
  38199. 0x21,0xC9,0x6A,0xC9,0x5C,0xB6,0xB0,0xAA,
  38200. 0xC3,0xED,0x6E,0x66,0x2F,0xCC,0x45,0x81,
  38201. 0xBC,0xBA,0x44,0x96,0x1C,0xBF,0x4E,0xFB,
  38202. 0x7A,0x46,0xFB,0xBE,0x9A,0x0C,0xE4,0x50,
  38203. 0x90,0xC7,0x92,0xAC,0x53,0xAE,0x53,0x76,
  38204. 0x29,0xA6,0x65,0xF1,0x09,0xED,0x1A,0x8E,
  38205. 0x03,0x2E,0x5A,0x06,0x51,0xE3,0x1E,0xE6,
  38206. 0xF6,0xFE,0x3A,0x6E,0xD1,0x92,0x31,0x1D,
  38207. 0xA1,0x6A,0x5C,0x30,0x3A,0xC7,0xFD,0x5B,
  38208. 0xFE,0x71,0x2C,0x5C,0x2F,0x5B,0x5B,0xCF,
  38209. 0xBC,0x7F,0xBF,0x6C,0xAF,0x44,0x8A,0xAE,
  38210. 0x14,0x60,0xAB,0x88,0xED,0x0E,0x4F,0xF8,
  38211. 0xC7,0x1B,0x74,0x28,0x72,0xB3,0x96,0xA6,
  38212. 0xE6,0x46,0x22,0x82,0xCF,0x1F,0x4D,0xA6,
  38213. 0xEA,0x22,0x06,0x07,0x52,0xF5,0x26,0x16,
  38214. 0x0B,0x90,0xE3,0xFF,0x64,0xA9,0xE4,0x61,
  38215. 0x1E,0x9C,0x12,0x9C,0xF6,0xD4,0x63,0x29,
  38216. 0xEA,0x02,0xF7,0x18,0x52,0x79,0x6C,0x43,
  38217. 0xDC,0xCF,0x43,0x23,0xB9,0xCC,0x4A,0x25,
  38218. 0x9D,0x10,0xAF,0xA3,0xE6,0x47,0x5A,0x1C,
  38219. 0xFE,0x68,0x89,0xAF,0x1B,0x2D,0x88,0x3E,
  38220. 0xCA,0xDC,0x70,0xEA,0xAC,0x11,0x00,0x8A,
  38221. 0x6E,0xE0,0xC7,0xD0,0xD2,0x1A,0x36,0x18,
  38222. 0x97,0xB3,0x5F,0x0E,0x75,0x48,0x28,0xF8,
  38223. 0xA8,0xF5,0x90,0xD1,0xA1,0x84,0xFB,0xA4,
  38224. 0xAD,0x50,0xBE,0xE9,0x39,0x8C,0xC5,0xA1,
  38225. 0x67,0x51,0xA1,0x8C,0xD6,0x6B,0x97,0x1F,
  38226. 0x47,0x99,0xEE,0xE0,0x70,0x01,0xC7,0x07,
  38227. 0x50,0xF3,0x5E,0x3F,0xE7,0x06,0xD6,0x8D,
  38228. 0x26,0xD6,0x5A,0x59,0x18,0x72,0x6B,0x12,
  38229. 0xD2,0xAF,0x9B,0xB4,0x2B,0xD0,0xB2,0xF2,
  38230. 0x96,0x2F,0x40,0xEA,0xBE,0xE6,0xAC,0x1F,
  38231. 0xB8,0x33,0xC2,0x76,0xDC,0x8C,0xAC,0xC1,
  38232. 0x46,0x5E,0x04,0x84,0x1B,0xC8,0xB9,0x65,
  38233. 0x8D,0xAD,0x96,0xB5,0xB1,0xF6,0x17,0x4A,
  38234. 0x19,0x87,0xE7,0xBF,0x29,0xC7,0x9B,0xB9,
  38235. 0xD6,0x11,0x2C,0x92,0x2F,0xB7,0x24,0xD5,
  38236. 0x01,0x1D,0x80,0x37,0x54,0xED,0x33,0x32,
  38237. 0xAB,0x7A,0x12,0xD4,0x02,0x1D,0x27,0x52,
  38238. 0x89,0xDB,0x32,0xBF,0x61,0xD4,0xBB,0xB4,
  38239. 0x46,0x78,0x1B,0x64,0x17,0x84,0x4B,0x8A,
  38240. 0xBA,0xC6,0xC1,0xCF,0xC7,0x5D,0x8F,0x93,
  38241. 0xC5,0x9A,0x27,0x90,0xAC,0x17,0x98,0xFF,
  38242. 0xC8,0x22,0x59,0x55,0x90,0xB2,0x29,0x39,
  38243. 0xA0,0xBE,0x00,0x23,0x55,0x6B,0xDA,0x83,
  38244. 0xD8,0x5B,0x57,0x7C,0x67,0x1B,0xC3,0x6B,
  38245. 0x6D,0xC7,0x9B,0x2B,0x9E,0xB7,0x95,0xB3,
  38246. 0xF0,0x1B,0x89,0x5A,0xD7,0x4B,0x67,0xAF,
  38247. 0xDC,0x9E,0xCF,0x7E,0x1A,0xBA,0x1B,0xB9,
  38248. 0x3B,0x7A,0xDD,0x3F,0x0D,0xEE,0x4C,0x0B,
  38249. 0xD1,0x4F,0x34,0xF2,0x93,0xF7,0x21,0x64,
  38250. 0x2C,0x07,0x00,0x15,0x4F,0xE3,0x6A,0x9F,
  38251. 0x08,0x52,0xC2,0x65,0x47,0x1F,0x34,0x64,
  38252. 0x66,0x07,0xBC,0xEA,0xAF,0x9B,0xAA,0x39,
  38253. 0x15,0x8B,0x08,0x8C,0x24,0x41,0x9B,0x46,
  38254. 0x1B,0x5B,0x91,0x11,0xC4,0xFD,0xA9,0x88,
  38255. 0x35,0x0E,0x7D,0xAF,0xFD,0xB7,0x90,0x7E,
  38256. 0xD7,0x29,0x02,0x0A,0xDC,0xC8,0x3F,0xC0,
  38257. 0xFD,0x97,0xAF,0x50,0x49,0xA6,0x5E,0x12,
  38258. 0xC1,0xCD,0xEC,0x52,0xC5,0x51,0xF2,0x80,
  38259. 0x17,0x61,0xC7,0x7E,0xBE,0xD1,0x1B,0x65,
  38260. 0xA4,0xAB,0x92,0x8D,0x89,0xB2,0xC5,0x8F,
  38261. 0xFF,0xA5,0x6F,0xFA,0x62,0x75,0xE4,0xA1,
  38262. 0xD4,0x22,0xA8,0x9E,0x40,0x04,0x27,0x1F,
  38263. 0xCC,0x81,0xBA,0x28,0x67,0xA0,0x1C,0x80,
  38264. 0xEB,0xCA,0xB0,0x61,0xA5,0x48,0xD0,0x8A,
  38265. 0x25,0xEB,0x9E,0x67,0x8C,0x8E,0x9B,0xD1,
  38266. 0xAD,0xBB,0xC3,0xEA,0xD3,0xD4,0xC5,0x12,
  38267. 0x7B,0xDD,0x00,0x57,0x7F,0xF6,0xF7,0xF6,
  38268. 0x3C,0x05,0xCF,0xFC,0x12,0xE1,0x93,0x05,
  38269. 0xE5,0x9B,0x79,0x87,0x69,0xD8,0x82,0xD9,
  38270. 0xD7,0x1D,0x41,0x73,0xE4,0x52,0x1D,0x3E,
  38271. 0xE5,0x8C,0x8D,0x34,0xE1,0x75,0xA9,0xF1,
  38272. 0x9D,0x09,0xA2,0x5B,0xEF,0xDA,0x96,0x6E,
  38273. 0x76,0x3D,0xEA,0x50,0xD9,0xCF,0x4F,0xAC,
  38274. 0xAD,0x1D,0x35,0x72,0x1B,0x88,0x8B,0xCD,
  38275. 0x8C,0x8A,0x8A,0xE0,0x96,0x04,0xD8,0xBB,
  38276. 0x28,0x43,0x16,0x77,0x60,0x98,0x63,0xF9,
  38277. 0xB9,0x71,0x46,0xB7,0xE1,0xA7,0xA9,0x84,
  38278. 0xC3,0x65,0x82,0xE1,0x1B,0x67,0x04,0x2D,
  38279. 0x55,0x6B,0xF9,0xC0,0x79,0x09,0x09,0xE7,
  38280. 0xFD,0x06,0x4D,0x09,0x9B,0x1A,0xCE,0x35,
  38281. 0xFA,0x27,0x6F,0x2F,0x01,0x65,0x0D,0xA0,
  38282. 0x97,0x59,0x11,0xF0,0x48,0xD2,0xE7,0x46,
  38283. 0xBE,0xB4,0x0A,0xA3,0xE2,0x75,0x0E,0x09,
  38284. 0x94,0xD9,0x69,0x28,0xD4,0xDA,0x64,0xBA,
  38285. 0xFE,0xA4,0xB9,0xF0,0xBA,0xEB,0xBA,0xAC,
  38286. 0xA8,0xF9,0xD3,0x82,0x4C,0x36,0x80,0xFA,
  38287. 0xE5,0xF6,0x76,0xC3,0x80,0xFA,0x90,0x29,
  38288. 0xF4,0x85,0xA4,0xC6,0x25,0x22,0x79,0x7E,
  38289. 0x39,0x1E,0x30,0xB8,0x65,0x72,0xCF,0xE1,
  38290. 0x99,0xF0,0x75,0xE8,0x09,0xB4,0x92,0x96,
  38291. 0x1B,0x68,0x50,0x88,0xF1,0x2C,0x97,0xE3,
  38292. 0x2D,0x26,0x8F,0xC5,0x30,0xCF,0x24,0xCB,
  38293. 0xB2,0x60,0x77,0xDC,0x02,0x72,0x0D,0xD9,
  38294. 0x2E,0xF2,0x52,0xEA,0x00,0xF6,0x32,0x65,
  38295. 0xA5,0xC6,0x43,0x29,0x29,0x69,0xAB,0x27,
  38296. 0x0C,0x39,0xDF,0x76,0x3E,0x93,0x95,0xB1,
  38297. 0x2C,0xA2,0x0D,0x18,0xCE,0xA0,0x97,0x10,
  38298. 0x3C,0x90,0xC0,0xEF,0x0E,0x04,0xA6,0xC8,
  38299. 0xA0,0x21,0x3C,0x0B,0x22,0x77,0x7A,0x66,
  38300. 0xA5,0x90,0x25,0xA4,0x09,0x3E,0xD5,0x27,
  38301. 0x1F,0x6C,0x99,0x85,0x5C,0xA2,0x99,0x7A,
  38302. 0x25,0xEE,0x8D,0x32,0x3D,0xD3,0xDC,0xF5,
  38303. 0x00,0x5A,0x34,0x61,0xB6,0xCD,0x4E,0xBC,
  38304. 0x26,0x36,0xFB,0x44,0x97,0x35,0xBD,0x06,
  38305. 0x7D,0x2E,0x4A,0xA2,0xDC,0x24,0xFE,0x70,
  38306. 0x0A,0xF9,0x57,0xE3,0xEE,0xAB,0xD1,0x17,
  38307. 0xF3,0x7C,0xD6,0x37,0x26,0xFA,0x83,0x9F,
  38308. 0xDD,0xB2,0xE1,0xD7,0xF9,0xC7,0x0E,0x15,
  38309. 0x01,0xA6,0x58,0x32,0x98,0x04,0x32,0xD4,
  38310. 0xDE,0xB9,0xEF,0x09,0xFA,0xE4,0x5A,0xD7,
  38311. 0xDD,0x09,0x1C,0xC9,0xAC,0xB8,0x6A,0xF5,
  38312. 0x00,0x5D,0x6B,0x95,0x12,0x8C,0x2F,0xCC,
  38313. 0xD8,0xB9,0x50,0x3A,0xEB,0x74,0x86,0xD2,
  38314. 0x3F,0xA1,0x05,0x8F,0x6E,0xEF,0xF5,0xA4,
  38315. 0xD6,0x6E,0x53,0xFA,0x9E,0xFA,0xCE,0xDB,
  38316. 0x99,0x46,0xE7,0xC5,0xDA,0x92,0x51,0x4F,
  38317. 0x22,0x07,0xF3,0xA5,0x38,0x26,0xD3,0xEC,
  38318. 0xD6,0x01,0xDD,0x31,0x3A,0x48,0x93,0xF6,
  38319. 0x69,0x4F,0xD8,0xF6,0xC2,0x91,0xA5,0x7C,
  38320. 0xDF,0x51,0x64,0xF1,0x3B,0x79,0xBC,0x0A,
  38321. 0x2C,0xDC,0x33,0x5A,0x29,0xF6,0xB2,0x09,
  38322. 0x66,0xCA,0x24,0x9F,0x1A,0x18,0xF3,0x76,
  38323. 0x4C,0x5E,0x0B,0x81,0x7F,0x29,0x84,0xD8,
  38324. 0x7A,0xA8,0xD6,0x11,0xAC,0xEC,0xD9,0x07,
  38325. 0x91,0xEC,0xB6,0x6D,0xEC,0xDB,0xBE,0x6F,
  38326. 0x9F,0xC5,0x19,0x5E,0x56,0x87,0x20,0x80,
  38327. 0x75,0xD5,0x64,0xE9,0x80,0xBF,0x2D,0xD5,
  38328. 0x94,0x9F,0x8C,0xA4,0x54,0x41,0xAB,0xB1,
  38329. 0x8E,0xAD,0x51,0xE4,0x3C,0x24,0xF7,0x1D,
  38330. 0xFE,0x02,0x48,0x7C,0x6D,0xED,0xF1,0xAC,
  38331. 0xD9,0x79,0x42,0xE5,0x3A,0xCF,0x6A,0x4C,
  38332. 0x6D,0xE2,0x13,0xD2,0x2B,0x9D,0xAB,0x1F,
  38333. 0x70,0xD3,0xC0,0x6F,0x81,0xE9,0x9A,0x86,
  38334. 0x33,0x39,0x60,0xE7,0x6A,0x00,0x1F,0x97,
  38335. 0xEB,0xE5,0x1D,0x0D,0x66,0x15,0xC9,0xA2,
  38336. 0xB1,0xC0,0xF0,0x2E,0xF4,0x07,0xA2,0x2E,
  38337. 0x49,0x92,0x95,0x13,0xA3,0x18,0x46,0x25,
  38338. 0xB9,0x3C,0xA1,0x4B,0x00,0x00,0x00,0x06,
  38339. 0xAB,0xAA,0xF9,0x3F,0x7E,0x21,0xF4,0x0E,
  38340. 0xCE,0xFD,0xE0,0x44,0xAC,0xC7,0x1A,0x30,
  38341. 0x22,0x9D,0x0A,0xD7,0x96,0x2D,0x8F,0x9A,
  38342. 0x99,0x1F,0x40,0x75,0x7F,0x62,0xF9,0xC1,
  38343. 0x81,0x7B,0x4A,0x1B,0xFA,0xD6,0x87,0xB9,
  38344. 0xEF,0x58,0x48,0xE4,0x5C,0x79,0xE5,0xB1,
  38345. 0x2C,0x59,0xA4,0x42,0xDB,0xA6,0x53,0x70,
  38346. 0x80,0x61,0x17,0xD4,0xD3,0x77,0xBD,0x53,
  38347. 0x26,0x7C,0x0E,0x0E,0xFF,0x30,0x4B,0xD0,
  38348. 0x86,0xFC,0x02,0x20,0x24,0x46,0x5B,0xF5,
  38349. 0xE3,0x99,0x73,0x85,0x60,0x00,0x36,0x47,
  38350. 0x17,0xEE,0x0C,0xD2,0x80,0x71,0x46,0x0E,
  38351. 0x2B,0xB0,0xEF,0x7F,0xFE,0x3B,0xE5,0xE1,
  38352. 0x87,0xC2,0xAF,0x1A,0x6F,0x63,0xF4,0x5A,
  38353. 0xC4,0x16,0xF7,0xAD,0x07,0x70,0x71,0x85,
  38354. 0x7D,0x3D,0x67,0x08,0xB8,0xD8,0xE2,0xF0,
  38355. 0xA1,0xAC,0xD2,0x94,0x7D,0x93,0x03,0xDD,
  38356. 0x54,0xF9,0x64,0x19,0xB3,0xED,0x24,0x22,
  38357. 0x01,0xD7,0x12,0x5E,0xC1,0x2B,0x39,0x10,
  38358. 0x13,0xE2,0x56,0x1C,0xEE,0xF4,0x2A,0x49,
  38359. 0x7B,0xFB,0x36,0x8D,0xF8,0xAF,0x60,0xDF,
  38360. 0x10,0xF0,0x72,0xA2,0xED,0xB6,0x53,0x88,
  38361. 0xA9,0x0C,0xED,0x9C,0x18,0x33,0x7D,0x65,
  38362. 0x9B,0xB2,0x9C,0x3E,0xE9,0x1E,0x43,0x51,
  38363. 0x7E,0xBE,0x01,0x95,0xF6,0x60,0x65,0xBE,
  38364. 0xD1,0xF4,0xE2,0x83,0x6B,0xCA,0x7A,0x70,
  38365. 0x41,0x83,0x72,0xC0,0x23,0x51,0x13,0x11,
  38366. 0x2D,0xF9,0xC0,0x0D,0x7D,0x73,0x76,0xA5,
  38367. 0x30,0x83,0x68,0x10,0x35,0xA2,0x18,0x22,
  38368. 0x4E,0x21,0x93,0x27,0x6A,0x19,0x28,0x83,
  38369. 0x7F,0xDD,0xDD,0xFF,0xC3,0x8A,0x64,0x00,
  38370. 0x5F,0x1C,0x0D,0xF8,0xBB,0xD7,0x15,0xB9,
  38371. 0xEF,0xE0,0x07,0x62,0x05,0x9E,0xCF,0xFC,
  38372. 0x08,0x52,0x1E,0x65,0x41,0x56,0x6A,0xEB,
  38373. 0x81,0x53,0x30,0x7B,0xF2,0xFD,0x65,0xFF,
  38374. 0xA2,0x14,0xF5,0x62,0x1E,0x24,0x48,0x47,
  38375. 0xA5,0x41,0x80,0xB4,0xC5,0xDC,0xB2,0xB4,
  38376. 0x2D,0x17,0xE7,0xBE,0x49,0x53,0x7A,0x25,
  38377. 0xC5,0x0D,0x19,0x59,0xF4,0x88,0x59,0xED,
  38378. 0x92,0x13,0xEE,0x7A,0x4F,0x12,0x98,0x4C
  38379. };
  38380. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t lms_test_verify_only(void)
  38381. {
  38382. LmsKey verifyKey;
  38383. unsigned char pub_raw[HSS_MAX_PUBLIC_KEY_LEN];
  38384. word32 pub_len = sizeof(pub_raw);
  38385. word32 sigSz = 0;
  38386. word32 msgSz = sizeof(lms_msg);
  38387. word32 pubSz = 0;
  38388. int levels = 0;
  38389. int height = 0;
  38390. int winternitz = 0;
  38391. int ret = WC_TEST_RET_ENC_NC;
  38392. int ret2 = WC_TEST_RET_ENC_NC;
  38393. int j = 0;
  38394. int n_diff = 0;
  38395. WOLFSSL_ENTER("lms_test_verify_only");
  38396. XMEMSET(pub_raw, 0, sizeof(pub_raw));
  38397. ret = wc_LmsKey_Init(&verifyKey, NULL, INVALID_DEVID);
  38398. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38399. ret = wc_LmsKey_SetParameters(&verifyKey, 1, 10, 8);
  38400. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38401. ret = wc_LmsKey_ImportPubRaw(&verifyKey, lms_L1H10W8_pub,
  38402. HSS_MAX_PUBLIC_KEY_LEN);
  38403. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38404. /* Verify parameters, pub length, and sig length are correct. */
  38405. ret = wc_LmsKey_GetParameters(&verifyKey, &levels, &height, &winternitz);
  38406. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38407. if (levels != 1 || height != 10 || winternitz != 8) {
  38408. printf("error: invalid LMS parameters: L%d-H%d-W%d\n", levels, height,
  38409. winternitz);
  38410. return WC_TEST_RET_ENC_NC;
  38411. }
  38412. ret = wc_LmsKey_GetPubLen(&verifyKey, &pubSz);
  38413. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38414. if (pubSz != HSS_MAX_PUBLIC_KEY_LEN) {
  38415. printf("error: got %u, expected %d\n", pubSz, HSS_MAX_PUBLIC_KEY_LEN);
  38416. return WC_TEST_RET_ENC_EC(pubSz);
  38417. }
  38418. ret = wc_LmsKey_GetSigLen(&verifyKey, &sigSz);
  38419. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38420. if (sigSz != LMS_L1H10W8_SIGLEN) {
  38421. printf("error: got %u, expected %d\n", sigSz, LMS_L1H10W8_SIGLEN);
  38422. return WC_TEST_RET_ENC_EC(sigSz);
  38423. }
  38424. ret = wc_LmsKey_Verify(&verifyKey, lms_L1H10W8_sig, LMS_L1H10W8_SIGLEN,
  38425. (byte *) lms_msg, msgSz);
  38426. if (ret != 0) {
  38427. printf("error: wc_LmsKey_Verify returned %d\n", ret);
  38428. return WC_TEST_RET_ENC_EC(ret);
  38429. }
  38430. /* Now test the ExportPubRaw API, verify we recover the original pub. */
  38431. ret = wc_LmsKey_ExportPubRaw(&verifyKey, pub_raw, &pub_len);
  38432. if (ret != 0) {
  38433. printf("error: wc_LmsKey_ExportPubRaw returned %d, expected 0\n", ret);
  38434. return WC_TEST_RET_ENC_EC(ret);
  38435. }
  38436. if (pub_len != HSS_MAX_PUBLIC_KEY_LEN) {
  38437. printf("error: LMS pub len %u, expected %d\n", pub_len,
  38438. HSS_MAX_PUBLIC_KEY_LEN);
  38439. return WC_TEST_RET_ENC_EC(pub_len);
  38440. }
  38441. n_diff = XMEMCMP(pub_raw, lms_L1H10W8_pub, sizeof(lms_L1H10W8_pub));
  38442. if (n_diff != 0) {
  38443. printf("error: exported and imported pub raw do not match: %d\n",
  38444. n_diff);
  38445. return WC_TEST_RET_ENC_EC(n_diff);
  38446. }
  38447. /* Flip bits in message. This should fail. */
  38448. lms_msg[msgSz / 2] ^= 1;
  38449. ret2 = wc_LmsKey_Verify(&verifyKey, lms_L1H10W8_sig, LMS_L1H10W8_SIGLEN,
  38450. (byte *) lms_msg, msgSz);
  38451. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  38452. printf("error: wc_LmsKey_Verify returned %d, expected -1\n", ret2);
  38453. return WC_TEST_RET_ENC_EC(ret);
  38454. }
  38455. /* Flip it back. This should pass again. */
  38456. lms_msg[msgSz / 2] ^= 1;
  38457. ret = wc_LmsKey_Verify(&verifyKey, lms_L1H10W8_sig, LMS_L1H10W8_SIGLEN,
  38458. (byte *) lms_msg, msgSz);
  38459. if (ret != 0) {
  38460. printf("error: wc_LmsKey_Verify returned %d, expected 0\n", ret);
  38461. return WC_TEST_RET_ENC_EC(ret);
  38462. }
  38463. /* Flip bits in a few places throughout the signature, stepping in multiple
  38464. * of hash size. These should all fail with -1. */
  38465. for (j = 0; j < (int) sigSz; j+= 4 * 32) {
  38466. lms_L1H10W8_sig[j] ^= 1;
  38467. ret2 = wc_LmsKey_Verify(&verifyKey, lms_L1H10W8_sig,
  38468. LMS_L1H10W8_SIGLEN,
  38469. (byte *) lms_msg, msgSz);
  38470. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  38471. /* Verify passed when it should have failed. */
  38472. return WC_TEST_RET_ENC_I(j);
  38473. }
  38474. /* Flip this spot back. */
  38475. lms_L1H10W8_sig[j] ^= 1;
  38476. }
  38477. wc_LmsKey_Free(&verifyKey);
  38478. return ret;
  38479. }
  38480. #endif
  38481. #endif /* if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_SMALL_STACK) */
  38482. static const int fiducial3 = WC_TEST_RET_LN; /* source code reference point --
  38483. * see print_fiducials() below.
  38484. */
  38485. #ifdef WOLFCRYPT_HAVE_ECCSI
  38486. static wc_test_ret_t eccsi_api_test(WC_RNG* rng, EccsiKey* key, mp_int* ssk,
  38487. ecc_point* pvt)
  38488. {
  38489. wc_test_ret_t ret;
  38490. byte id[1] = { 0x00 };
  38491. int valid;
  38492. word32 sz;
  38493. byte data[256];
  38494. byte hash[WC_MAX_DIGEST_SIZE];
  38495. byte hashSz;
  38496. byte sig[257];
  38497. word32 sigSz;
  38498. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  38499. if (ret != BAD_FUNC_ARG)
  38500. return WC_TEST_RET_ENC_EC(ret);
  38501. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  38502. if (ret != BAD_FUNC_ARG)
  38503. return WC_TEST_RET_ENC_EC(ret);
  38504. ret = wc_InitEccsiKey(NULL, NULL, INVALID_DEVID);
  38505. if (ret != BAD_FUNC_ARG)
  38506. return WC_TEST_RET_ENC_EC(ret);
  38507. ret = wc_InitEccsiKey(NULL, HEAP_HINT, INVALID_DEVID);
  38508. if (ret != BAD_FUNC_ARG)
  38509. return WC_TEST_RET_ENC_EC(ret);
  38510. wc_FreeEccsiKey(NULL);
  38511. /* Create a valid key. */
  38512. ret = wc_InitEccsiKey(key, NULL, INVALID_DEVID);
  38513. if (ret != 0)
  38514. return WC_TEST_RET_ENC_EC(ret);
  38515. ret = wc_MakeEccsiKey(NULL, NULL);
  38516. if (ret != BAD_FUNC_ARG)
  38517. return WC_TEST_RET_ENC_EC(ret);
  38518. ret = wc_MakeEccsiKey(key, NULL);
  38519. if (ret != BAD_FUNC_ARG)
  38520. return WC_TEST_RET_ENC_EC(ret);
  38521. ret = wc_MakeEccsiKey(NULL, rng);
  38522. if (ret != BAD_FUNC_ARG)
  38523. return WC_TEST_RET_ENC_EC(ret);
  38524. ret = wc_MakeEccsiPair(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL,
  38525. NULL);
  38526. if (ret != BAD_FUNC_ARG)
  38527. return WC_TEST_RET_ENC_EC(ret);
  38528. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL);
  38529. if (ret != BAD_FUNC_ARG)
  38530. return WC_TEST_RET_ENC_EC(ret);
  38531. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt);
  38532. if (ret != BAD_FUNC_ARG)
  38533. return WC_TEST_RET_ENC_EC(ret);
  38534. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt);
  38535. if (ret != BAD_FUNC_ARG)
  38536. return WC_TEST_RET_ENC_EC(ret);
  38537. ret = wc_MakeEccsiPair(key, NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  38538. if (ret != BAD_FUNC_ARG)
  38539. return WC_TEST_RET_ENC_EC(ret);
  38540. ret = wc_MakeEccsiPair(NULL, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  38541. if (ret != BAD_FUNC_ARG)
  38542. return WC_TEST_RET_ENC_EC(ret);
  38543. /* No key set */
  38544. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  38545. if (ret != BAD_STATE_E)
  38546. return WC_TEST_RET_ENC_EC(ret);
  38547. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL,
  38548. NULL);
  38549. if (ret != BAD_FUNC_ARG)
  38550. return WC_TEST_RET_ENC_EC(ret);
  38551. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  38552. NULL);
  38553. if (ret != BAD_FUNC_ARG)
  38554. return WC_TEST_RET_ENC_EC(ret);
  38555. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL,
  38556. &valid);
  38557. if (ret != BAD_FUNC_ARG)
  38558. return WC_TEST_RET_ENC_EC(ret);
  38559. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt,
  38560. &valid);
  38561. if (ret != BAD_FUNC_ARG)
  38562. return WC_TEST_RET_ENC_EC(ret);
  38563. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt,
  38564. &valid);
  38565. if (ret != BAD_FUNC_ARG)
  38566. return WC_TEST_RET_ENC_EC(ret);
  38567. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  38568. &valid);
  38569. if (ret != BAD_FUNC_ARG)
  38570. return WC_TEST_RET_ENC_EC(ret);
  38571. /* No key set */
  38572. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  38573. &valid);
  38574. if (ret != BAD_STATE_E)
  38575. return WC_TEST_RET_ENC_EC(ret);
  38576. ret = wc_ValidateEccsiPvt(NULL, NULL, NULL);
  38577. if (ret != BAD_FUNC_ARG)
  38578. return WC_TEST_RET_ENC_EC(ret);
  38579. ret = wc_ValidateEccsiPvt(key, NULL, NULL);
  38580. if (ret != BAD_FUNC_ARG)
  38581. return WC_TEST_RET_ENC_EC(ret);
  38582. ret = wc_ValidateEccsiPvt(NULL, pvt, NULL);
  38583. if (ret != BAD_FUNC_ARG)
  38584. return WC_TEST_RET_ENC_EC(ret);
  38585. ret = wc_ValidateEccsiPvt(NULL, NULL, &valid);
  38586. if (ret != BAD_FUNC_ARG)
  38587. return WC_TEST_RET_ENC_EC(ret);
  38588. ret = wc_ValidateEccsiPvt(key, pvt, NULL);
  38589. if (ret != BAD_FUNC_ARG)
  38590. return WC_TEST_RET_ENC_EC(ret);
  38591. ret = wc_ValidateEccsiPvt(key, NULL, &valid);
  38592. if (ret != BAD_FUNC_ARG)
  38593. return WC_TEST_RET_ENC_EC(ret);
  38594. ret = wc_ValidateEccsiPvt(NULL, pvt, &valid);
  38595. if (ret != BAD_FUNC_ARG)
  38596. return WC_TEST_RET_ENC_EC(ret);
  38597. ret = wc_EncodeEccsiPair(NULL, NULL, NULL, data, NULL);
  38598. if (ret != BAD_FUNC_ARG)
  38599. return WC_TEST_RET_ENC_EC(ret);
  38600. ret = wc_EncodeEccsiPair(key, ssk, pvt, data, NULL);
  38601. if (ret != BAD_FUNC_ARG)
  38602. return WC_TEST_RET_ENC_EC(ret);
  38603. ret = wc_EncodeEccsiPair(key, ssk, NULL, data, &sz);
  38604. if (ret != BAD_FUNC_ARG)
  38605. return WC_TEST_RET_ENC_EC(ret);
  38606. ret = wc_EncodeEccsiPair(key, NULL, pvt, data, &sz);
  38607. if (ret != BAD_FUNC_ARG)
  38608. return WC_TEST_RET_ENC_EC(ret);
  38609. ret = wc_EncodeEccsiPair(NULL, ssk, pvt, data, &sz);
  38610. if (ret != BAD_FUNC_ARG)
  38611. return WC_TEST_RET_ENC_EC(ret);
  38612. /* No key created so no curve information. */
  38613. ret = wc_EncodeEccsiPair(key, ssk, pvt, NULL, &sz);
  38614. if (ret != LENGTH_ONLY_E)
  38615. return WC_TEST_RET_ENC_EC(ret);
  38616. ret = wc_EncodeEccsiSsk(NULL, NULL, data, NULL);
  38617. if (ret != BAD_FUNC_ARG)
  38618. return WC_TEST_RET_ENC_EC(ret);
  38619. ret = wc_EncodeEccsiSsk(key, ssk, data, NULL);
  38620. if (ret != BAD_FUNC_ARG)
  38621. return WC_TEST_RET_ENC_EC(ret);
  38622. ret = wc_EncodeEccsiSsk(key, NULL, data, &sz);
  38623. if (ret != BAD_FUNC_ARG)
  38624. return WC_TEST_RET_ENC_EC(ret);
  38625. ret = wc_EncodeEccsiSsk(NULL, ssk, data, &sz);
  38626. if (ret != BAD_FUNC_ARG)
  38627. return WC_TEST_RET_ENC_EC(ret);
  38628. ret = wc_EncodeEccsiPvt(NULL, NULL, data, NULL, 1);
  38629. if (ret != BAD_FUNC_ARG)
  38630. return WC_TEST_RET_ENC_EC(ret);
  38631. ret = wc_EncodeEccsiPvt(key, pvt, data, NULL, 1);
  38632. if (ret != BAD_FUNC_ARG)
  38633. return WC_TEST_RET_ENC_EC(ret);
  38634. ret = wc_EncodeEccsiPvt(key, NULL, data, &sz, 1);
  38635. if (ret != BAD_FUNC_ARG)
  38636. return WC_TEST_RET_ENC_EC(ret);
  38637. ret = wc_EncodeEccsiPvt(NULL, pvt, data, &sz, 1);
  38638. if (ret != BAD_FUNC_ARG)
  38639. return WC_TEST_RET_ENC_EC(ret);
  38640. ret = wc_DecodeEccsiPair(NULL, NULL, 0, NULL, NULL);
  38641. if (ret != BAD_FUNC_ARG)
  38642. return WC_TEST_RET_ENC_EC(ret);
  38643. ret = wc_DecodeEccsiPair(key, data, 0, ssk, NULL);
  38644. if (ret != BAD_FUNC_ARG)
  38645. return WC_TEST_RET_ENC_EC(ret);
  38646. ret = wc_DecodeEccsiPair(key, data, 0, NULL, pvt);
  38647. if (ret != BAD_FUNC_ARG)
  38648. return WC_TEST_RET_ENC_EC(ret);
  38649. ret = wc_DecodeEccsiPair(key, NULL, 0, ssk, pvt);
  38650. if (ret != BAD_FUNC_ARG)
  38651. return WC_TEST_RET_ENC_EC(ret);
  38652. ret = wc_DecodeEccsiPair(NULL, data, 0, ssk, pvt);
  38653. if (ret != BAD_FUNC_ARG)
  38654. return WC_TEST_RET_ENC_EC(ret);
  38655. ret = wc_DecodeEccsiSsk(NULL, NULL, 0, NULL);
  38656. if (ret != BAD_FUNC_ARG)
  38657. return WC_TEST_RET_ENC_EC(ret);
  38658. ret = wc_DecodeEccsiSsk(key, data, 0, NULL);
  38659. if (ret != BAD_FUNC_ARG)
  38660. return WC_TEST_RET_ENC_EC(ret);
  38661. ret = wc_DecodeEccsiSsk(key, NULL, 0, ssk);
  38662. if (ret != BAD_FUNC_ARG)
  38663. return WC_TEST_RET_ENC_EC(ret);
  38664. ret = wc_DecodeEccsiSsk(NULL, data, 0, ssk);
  38665. if (ret != BAD_FUNC_ARG)
  38666. return WC_TEST_RET_ENC_EC(ret);
  38667. ret = wc_DecodeEccsiPvt(NULL, NULL, 0, NULL);
  38668. if (ret != BAD_FUNC_ARG)
  38669. return WC_TEST_RET_ENC_EC(ret);
  38670. ret = wc_DecodeEccsiPvt(key, data, 0, NULL);
  38671. if (ret != BAD_FUNC_ARG)
  38672. return WC_TEST_RET_ENC_EC(ret);
  38673. ret = wc_DecodeEccsiPvt(key, NULL, 0, pvt);
  38674. if (ret != BAD_FUNC_ARG)
  38675. return WC_TEST_RET_ENC_EC(ret);
  38676. ret = wc_DecodeEccsiPvt(NULL, data, 0, pvt);
  38677. if (ret != BAD_FUNC_ARG)
  38678. return WC_TEST_RET_ENC_EC(ret);
  38679. ret = wc_DecodeEccsiPvtFromSig(NULL, NULL, 0, NULL);
  38680. if (ret != BAD_FUNC_ARG)
  38681. return WC_TEST_RET_ENC_EC(ret);
  38682. ret = wc_DecodeEccsiPvtFromSig(key, data, 0, NULL);
  38683. if (ret != BAD_FUNC_ARG)
  38684. return WC_TEST_RET_ENC_EC(ret);
  38685. ret = wc_DecodeEccsiPvtFromSig(key, NULL, 0, pvt);
  38686. if (ret != BAD_FUNC_ARG)
  38687. return WC_TEST_RET_ENC_EC(ret);
  38688. ret = wc_DecodeEccsiPvtFromSig(NULL, data, 0, pvt);
  38689. if (ret != BAD_FUNC_ARG)
  38690. return WC_TEST_RET_ENC_EC(ret);
  38691. ret = wc_ExportEccsiKey(NULL, data, NULL);
  38692. if (ret != BAD_FUNC_ARG)
  38693. return WC_TEST_RET_ENC_EC(ret);
  38694. ret = wc_ExportEccsiKey(key, data, NULL);
  38695. if (ret != BAD_FUNC_ARG)
  38696. return WC_TEST_RET_ENC_EC(ret);
  38697. ret = wc_ExportEccsiKey(NULL, data, &sz);
  38698. if (ret != BAD_FUNC_ARG)
  38699. return WC_TEST_RET_ENC_EC(ret);
  38700. /* No key to export */
  38701. ret = wc_ExportEccsiKey(key, NULL, &sz);
  38702. if (ret != BAD_STATE_E)
  38703. return WC_TEST_RET_ENC_EC(ret);
  38704. ret = wc_ImportEccsiKey(NULL, NULL, 0);
  38705. if (ret != BAD_FUNC_ARG)
  38706. return WC_TEST_RET_ENC_EC(ret);
  38707. ret = wc_ImportEccsiKey(key, NULL, 0);
  38708. if (ret != BAD_FUNC_ARG)
  38709. return WC_TEST_RET_ENC_EC(ret);
  38710. ret = wc_ImportEccsiKey(NULL, data, 0);
  38711. if (ret != BAD_FUNC_ARG)
  38712. return WC_TEST_RET_ENC_EC(ret);
  38713. ret = wc_ExportEccsiPrivateKey(NULL, data, NULL);
  38714. if (ret != BAD_FUNC_ARG)
  38715. return WC_TEST_RET_ENC_EC(ret);
  38716. ret = wc_ExportEccsiPrivateKey(key, data, NULL);
  38717. if (ret != BAD_FUNC_ARG)
  38718. return WC_TEST_RET_ENC_EC(ret);
  38719. ret = wc_ExportEccsiPrivateKey(NULL, data, &sz);
  38720. if (ret != BAD_FUNC_ARG)
  38721. return WC_TEST_RET_ENC_EC(ret);
  38722. /* No key to export */
  38723. ret = wc_ExportEccsiPrivateKey(key, NULL, &sz);
  38724. if (ret != BAD_STATE_E)
  38725. return WC_TEST_RET_ENC_EC(ret);
  38726. ret = wc_ImportEccsiPrivateKey(NULL, NULL, 0);
  38727. if (ret != BAD_FUNC_ARG)
  38728. return WC_TEST_RET_ENC_EC(ret);
  38729. ret = wc_ImportEccsiPrivateKey(key, NULL, 0);
  38730. if (ret != BAD_FUNC_ARG)
  38731. return WC_TEST_RET_ENC_EC(ret);
  38732. ret = wc_ImportEccsiPrivateKey(NULL, data, 0);
  38733. if (ret != BAD_FUNC_ARG)
  38734. return WC_TEST_RET_ENC_EC(ret);
  38735. ret = wc_ExportEccsiPublicKey(NULL, data, NULL, 1);
  38736. if (ret != BAD_FUNC_ARG)
  38737. return WC_TEST_RET_ENC_EC(ret);
  38738. ret = wc_ExportEccsiPublicKey(key, data, NULL, 1);
  38739. if (ret != BAD_FUNC_ARG)
  38740. return WC_TEST_RET_ENC_EC(ret);
  38741. ret = wc_ExportEccsiPublicKey(NULL, data, &sz, 1);
  38742. if (ret != BAD_FUNC_ARG)
  38743. return WC_TEST_RET_ENC_EC(ret);
  38744. /* No key to export */
  38745. ret = wc_ExportEccsiPublicKey(key, data, &sz, 1);
  38746. if (ret != BAD_STATE_E)
  38747. return WC_TEST_RET_ENC_EC(ret);
  38748. ret = wc_ImportEccsiPublicKey(NULL, NULL, 0, 1);
  38749. if (ret != BAD_FUNC_ARG)
  38750. return WC_TEST_RET_ENC_EC(ret);
  38751. ret = wc_ImportEccsiPublicKey(key, NULL, 0, 1);
  38752. if (ret != BAD_FUNC_ARG)
  38753. return WC_TEST_RET_ENC_EC(ret);
  38754. ret = wc_ImportEccsiPublicKey(NULL, data, 0, 1);
  38755. if (ret != BAD_FUNC_ARG)
  38756. return WC_TEST_RET_ENC_EC(ret);
  38757. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL, NULL);
  38758. if (ret != BAD_FUNC_ARG)
  38759. return WC_TEST_RET_ENC_EC(ret);
  38760. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, NULL);
  38761. if (ret != BAD_FUNC_ARG)
  38762. return WC_TEST_RET_ENC_EC(ret);
  38763. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, NULL, &hashSz);
  38764. if (ret != BAD_FUNC_ARG)
  38765. return WC_TEST_RET_ENC_EC(ret);
  38766. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, NULL, hash, &hashSz);
  38767. if (ret != BAD_FUNC_ARG)
  38768. return WC_TEST_RET_ENC_EC(ret);
  38769. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, NULL, 1, pvt, hash,
  38770. &hashSz);
  38771. if (ret != BAD_FUNC_ARG)
  38772. return WC_TEST_RET_ENC_EC(ret);
  38773. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  38774. if (ret != BAD_FUNC_ARG)
  38775. return WC_TEST_RET_ENC_EC(ret);
  38776. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  38777. if (ret != BAD_STATE_E)
  38778. return WC_TEST_RET_ENC_EC(ret);
  38779. ret = wc_SetEccsiHash(NULL, NULL, 1);
  38780. if (ret != BAD_FUNC_ARG)
  38781. return WC_TEST_RET_ENC_EC(ret);
  38782. ret = wc_SetEccsiHash(key, NULL, 1);
  38783. if (ret != BAD_FUNC_ARG)
  38784. return WC_TEST_RET_ENC_EC(ret);
  38785. ret = wc_SetEccsiHash(NULL, hash, 1);
  38786. if (ret != BAD_FUNC_ARG)
  38787. return WC_TEST_RET_ENC_EC(ret);
  38788. ret = wc_SetEccsiPair(NULL, NULL, NULL);
  38789. if (ret != BAD_FUNC_ARG)
  38790. return WC_TEST_RET_ENC_EC(ret);
  38791. ret = wc_SetEccsiPair(key, NULL, NULL);
  38792. if (ret != BAD_FUNC_ARG)
  38793. return WC_TEST_RET_ENC_EC(ret);
  38794. ret = wc_SetEccsiPair(NULL, ssk, NULL);
  38795. if (ret != BAD_FUNC_ARG)
  38796. return WC_TEST_RET_ENC_EC(ret);
  38797. ret = wc_SetEccsiPair(NULL, NULL, pvt);
  38798. if (ret != BAD_FUNC_ARG)
  38799. return WC_TEST_RET_ENC_EC(ret);
  38800. ret = wc_SetEccsiPair(key, ssk, NULL);
  38801. if (ret != BAD_FUNC_ARG)
  38802. return WC_TEST_RET_ENC_EC(ret);
  38803. ret = wc_SetEccsiPair(key, NULL, pvt);
  38804. if (ret != BAD_FUNC_ARG)
  38805. return WC_TEST_RET_ENC_EC(ret);
  38806. ret = wc_SetEccsiPair(NULL, ssk, pvt);
  38807. if (ret != BAD_FUNC_ARG)
  38808. return WC_TEST_RET_ENC_EC(ret);
  38809. ret = wc_SignEccsiHash(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, NULL);
  38810. if (ret != BAD_FUNC_ARG)
  38811. return WC_TEST_RET_ENC_EC(ret);
  38812. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, sig, NULL);
  38813. if (ret != BAD_FUNC_ARG)
  38814. return WC_TEST_RET_ENC_EC(ret);
  38815. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, NULL, 0, sig,
  38816. &sigSz);
  38817. if (ret != BAD_FUNC_ARG)
  38818. return WC_TEST_RET_ENC_EC(ret);
  38819. ret = wc_SignEccsiHash(key, NULL, WC_HASH_TYPE_SHA256, data, 0, sig,
  38820. &sigSz);
  38821. if (ret != BAD_FUNC_ARG)
  38822. return WC_TEST_RET_ENC_EC(ret);
  38823. ret = wc_SignEccsiHash(NULL, rng, WC_HASH_TYPE_SHA256, data, 0, sig,
  38824. &sigSz);
  38825. if (ret != BAD_FUNC_ARG)
  38826. return WC_TEST_RET_ENC_EC(ret);
  38827. /* Key not set. */
  38828. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  38829. &sigSz);
  38830. if (ret != BAD_STATE_E)
  38831. return WC_TEST_RET_ENC_EC(ret);
  38832. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  38833. if (ret != BAD_FUNC_ARG)
  38834. return WC_TEST_RET_ENC_EC(ret);
  38835. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  38836. if (ret != BAD_FUNC_ARG)
  38837. return WC_TEST_RET_ENC_EC(ret);
  38838. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, NULL, 0, NULL);
  38839. if (ret != BAD_FUNC_ARG)
  38840. return WC_TEST_RET_ENC_EC(ret);
  38841. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0, NULL);
  38842. if (ret != BAD_FUNC_ARG)
  38843. return WC_TEST_RET_ENC_EC(ret);
  38844. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0,
  38845. &valid);
  38846. if (ret != BAD_FUNC_ARG)
  38847. return WC_TEST_RET_ENC_EC(ret);
  38848. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0, NULL);
  38849. if (ret != BAD_FUNC_ARG)
  38850. return WC_TEST_RET_ENC_EC(ret);
  38851. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, NULL, 0,
  38852. &valid);
  38853. if (ret != BAD_FUNC_ARG)
  38854. return WC_TEST_RET_ENC_EC(ret);
  38855. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0,
  38856. &valid);
  38857. if (ret != BAD_FUNC_ARG)
  38858. return WC_TEST_RET_ENC_EC(ret);
  38859. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  38860. &valid);
  38861. if (ret != BAD_FUNC_ARG)
  38862. return WC_TEST_RET_ENC_EC(ret);
  38863. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  38864. &valid);
  38865. if (ret != BAD_STATE_E)
  38866. return WC_TEST_RET_ENC_EC(ret);
  38867. ret = wc_SetEccsiPair(key, ssk, pvt);
  38868. if (ret != 0)
  38869. return WC_TEST_RET_ENC_EC(ret);
  38870. /* Identity hash not set. */
  38871. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  38872. &sigSz);
  38873. if (ret != BAD_STATE_E)
  38874. return WC_TEST_RET_ENC_EC(ret);
  38875. wc_FreeEccsiKey(key);
  38876. return 0;
  38877. }
  38878. /* RFC 6507: Appendix A */
  38879. static wc_test_ret_t eccsi_kat_verify_test(EccsiKey* key, ecc_point* pvt)
  38880. {
  38881. wc_test_ret_t ret;
  38882. int verified;
  38883. const byte msg[] = { 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x00 };
  38884. word32 msgSz = sizeof(msg);
  38885. byte hash[WC_SHA256_DIGEST_SIZE];
  38886. byte hashSz = WC_SHA256_DIGEST_SIZE;
  38887. static const byte id[] = {
  38888. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  38889. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  38890. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  38891. 0x33, 0x00
  38892. };
  38893. word32 idSz = sizeof(id);
  38894. static const byte sig[] = {
  38895. 0x26, 0x9D, 0x4C, 0x8F, 0xDE, 0xB6, 0x6A, 0x74,
  38896. 0xE4, 0xEF, 0x8C, 0x0D, 0x5D, 0xCC, 0x59, 0x7D,
  38897. 0xDF, 0xE6, 0x02, 0x9C, 0x2A, 0xFF, 0xC4, 0x93,
  38898. 0x60, 0x08, 0xCD, 0x2C, 0xC1, 0x04, 0x5D, 0x81,
  38899. 0xE0, 0x9B, 0x52, 0x8D, 0x0E, 0xF8, 0xD6, 0xDF,
  38900. 0x1A, 0xA3, 0xEC, 0xBF, 0x80, 0x11, 0x0C, 0xFC,
  38901. 0xEC, 0x9F, 0xC6, 0x82, 0x52, 0xCE, 0xBB, 0x67,
  38902. 0x9F, 0x41, 0x34, 0x84, 0x69, 0x40, 0xCC, 0xFD,
  38903. 0x04,
  38904. 0x75, 0x8A, 0x14, 0x27, 0x79, 0xBE, 0x89, 0xE8,
  38905. 0x29, 0xE7, 0x19, 0x84, 0xCB, 0x40, 0xEF, 0x75,
  38906. 0x8C, 0xC4, 0xAD, 0x77, 0x5F, 0xC5, 0xB9, 0xA3,
  38907. 0xE1, 0xC8, 0xED, 0x52, 0xF6, 0xFA, 0x36, 0xD9,
  38908. 0xA7, 0x9D, 0x24, 0x76, 0x92, 0xF4, 0xED, 0xA3,
  38909. 0xA6, 0xBD, 0xAB, 0x77, 0xD6, 0xAA, 0x64, 0x74,
  38910. 0xA4, 0x64, 0xAE, 0x49, 0x34, 0x66, 0x3C, 0x52,
  38911. 0x65, 0xBA, 0x70, 0x18, 0xBA, 0x09, 0x1F, 0x79
  38912. };
  38913. word32 sigSz = sizeof(sig);
  38914. static const byte pubData[] = {
  38915. 0x50, 0xD4, 0x67, 0x0B, 0xDE, 0x75, 0x24, 0x4F,
  38916. 0x28, 0xD2, 0x83, 0x8A, 0x0D, 0x25, 0x55, 0x8A,
  38917. 0x7A, 0x72, 0x68, 0x6D, 0x45, 0x22, 0xD4, 0xC8,
  38918. 0x27, 0x3F, 0xB6, 0x44, 0x2A, 0xEB, 0xFA, 0x93,
  38919. 0xDB, 0xDD, 0x37, 0x55, 0x1A, 0xFD, 0x26, 0x3B,
  38920. 0x5D, 0xFD, 0x61, 0x7F, 0x39, 0x60, 0xC6, 0x5A,
  38921. 0x8C, 0x29, 0x88, 0x50, 0xFF, 0x99, 0xF2, 0x03,
  38922. 0x66, 0xDC, 0xE7, 0xD4, 0x36, 0x72, 0x17, 0xF4
  38923. };
  38924. static const byte expHash[] = {
  38925. 0x49, 0x0f, 0x3f, 0xeb, 0xbc, 0x1c, 0x90, 0x2f,
  38926. 0x62, 0x89, 0x72, 0x3d, 0x7f, 0x8c, 0xbf, 0x79,
  38927. 0xdb, 0x88, 0x93, 0x08, 0x49, 0xd1, 0x9f, 0x38,
  38928. 0xf0, 0x29, 0x5b, 0x5c, 0x27, 0x6c, 0x14, 0xd1
  38929. };
  38930. ret = wc_ImportEccsiPublicKey(key, pubData, sizeof(pubData), 0);
  38931. if (ret != 0)
  38932. return WC_TEST_RET_ENC_EC(ret);
  38933. ret = wc_DecodeEccsiPvtFromSig(key, sig, sigSz, pvt);
  38934. if (ret != 0)
  38935. return WC_TEST_RET_ENC_EC(ret);
  38936. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, idSz, pvt, hash,
  38937. &hashSz);
  38938. if (ret != 0)
  38939. return WC_TEST_RET_ENC_EC(ret);
  38940. if (hashSz != sizeof(expHash))
  38941. return WC_TEST_RET_ENC_NC;
  38942. if (XMEMCMP(hash, expHash, hashSz) != 0)
  38943. return WC_TEST_RET_ENC_NC;
  38944. ret = wc_SetEccsiHash(key, hash, hashSz);
  38945. if (ret != 0)
  38946. return WC_TEST_RET_ENC_EC(ret);
  38947. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  38948. &verified);
  38949. if (ret != 0)
  38950. return WC_TEST_RET_ENC_EC(ret);
  38951. if (!verified)
  38952. return WC_TEST_RET_ENC_NC;
  38953. return 0;
  38954. }
  38955. static wc_test_ret_t eccsi_enc_dec_pair_test(EccsiKey* priv, mp_int* ssk, ecc_point* pvt)
  38956. {
  38957. wc_test_ret_t ret;
  38958. byte data[32 * 3];
  38959. word32 sz;
  38960. ecc_point* decPvt = NULL;
  38961. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38962. mp_int *decSsk = (mp_int *)XMALLOC(sizeof(*decSsk), HEAP_HINT,
  38963. DYNAMIC_TYPE_TMP_BUFFER);
  38964. if (decSsk == NULL)
  38965. return WC_TEST_RET_ENC_ERRNO;
  38966. #else
  38967. mp_int decSsk[1];
  38968. #endif
  38969. ret = mp_init(decSsk);
  38970. if (ret != 0)
  38971. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38972. decPvt = wc_ecc_new_point();
  38973. if (decPvt == NULL)
  38974. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  38975. ret = wc_EncodeEccsiPair(priv, ssk, pvt, NULL, &sz);
  38976. if (ret != LENGTH_ONLY_E)
  38977. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38978. if (sz != 32 * 3)
  38979. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  38980. ret = wc_EncodeEccsiPair(priv, ssk, pvt, data, &sz);
  38981. if (ret != 0)
  38982. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38983. if (sz != 32* 3)
  38984. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  38985. ret = wc_DecodeEccsiPair(priv, data, sz, decSsk, decPvt);
  38986. if (ret != 0)
  38987. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38988. ret = mp_cmp(ssk, decSsk);
  38989. if (ret != MP_EQ)
  38990. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  38991. ret = wc_ecc_cmp_point(pvt, decPvt);
  38992. if (ret != MP_EQ)
  38993. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38994. ret = wc_EncodeEccsiSsk(priv, ssk, NULL, &sz);
  38995. if (ret != LENGTH_ONLY_E)
  38996. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38997. if (sz != 32)
  38998. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  38999. ret = wc_EncodeEccsiSsk(priv, ssk, data, &sz);
  39000. if (ret != 0)
  39001. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39002. if (sz != 32)
  39003. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39004. ret = wc_DecodeEccsiSsk(priv, data, sz, decSsk);
  39005. if (ret != 0)
  39006. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39007. ret = mp_cmp(ssk, decSsk);
  39008. if (ret != MP_EQ)
  39009. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39010. ret = wc_EncodeEccsiPvt(priv, pvt, NULL, &sz, 1);
  39011. if (ret != LENGTH_ONLY_E)
  39012. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39013. if (sz != 32 * 2)
  39014. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39015. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 1);
  39016. if (ret != 0)
  39017. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39018. if (sz != 32 * 2)
  39019. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39020. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  39021. if (ret != 0)
  39022. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39023. ret = wc_ecc_cmp_point(pvt, decPvt);
  39024. if (ret != MP_EQ)
  39025. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39026. sz = sizeof(data);
  39027. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 0);
  39028. if (ret != 0)
  39029. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39030. if (sz != 32 * 2 + 1)
  39031. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39032. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  39033. if (ret != 0)
  39034. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39035. ret = wc_ecc_cmp_point(pvt, decPvt);
  39036. if (ret != MP_EQ)
  39037. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39038. wc_ecc_del_point(decPvt);
  39039. out:
  39040. mp_free(decSsk);
  39041. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39042. XFREE(decSsk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39043. #endif
  39044. return ret;
  39045. }
  39046. static wc_test_ret_t eccsi_imp_exp_key_test(EccsiKey* priv)
  39047. {
  39048. wc_test_ret_t ret;
  39049. byte data[32 * 3];
  39050. byte out[32 * 3];
  39051. word32 sz;
  39052. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  39053. if (ret != LENGTH_ONLY_E)
  39054. return WC_TEST_RET_ENC_EC(ret);
  39055. if (sz != 32 * 3)
  39056. return WC_TEST_RET_ENC_NC;
  39057. ret = wc_ExportEccsiKey(priv, data, &sz);
  39058. if (ret != 0)
  39059. return WC_TEST_RET_ENC_EC(ret);
  39060. ret = wc_ImportEccsiKey(priv, data, sz);
  39061. if (ret != 0)
  39062. return WC_TEST_RET_ENC_EC(ret);
  39063. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  39064. if (ret != LENGTH_ONLY_E)
  39065. return WC_TEST_RET_ENC_EC(ret);
  39066. if (sz != 32 * 3)
  39067. return WC_TEST_RET_ENC_NC;
  39068. ret = wc_ExportEccsiKey(priv, out, &sz);
  39069. if (ret != 0)
  39070. return WC_TEST_RET_ENC_EC(ret);
  39071. if (sz != 32 * 3)
  39072. return WC_TEST_RET_ENC_NC;
  39073. if (XMEMCMP(data, out, sz) != 0)
  39074. return WC_TEST_RET_ENC_NC;
  39075. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  39076. if (ret != LENGTH_ONLY_E)
  39077. return WC_TEST_RET_ENC_EC(ret);
  39078. if (sz != 32)
  39079. return WC_TEST_RET_ENC_NC;
  39080. ret = wc_ExportEccsiPrivateKey(priv, data, &sz);
  39081. if (ret != 0)
  39082. return WC_TEST_RET_ENC_EC(ret);
  39083. ret = wc_ImportEccsiPrivateKey(priv, data, sz);
  39084. if (ret != 0)
  39085. return WC_TEST_RET_ENC_EC(ret);
  39086. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  39087. if (ret != LENGTH_ONLY_E)
  39088. return WC_TEST_RET_ENC_EC(ret);
  39089. if (sz != 32)
  39090. return WC_TEST_RET_ENC_NC;
  39091. ret = wc_ExportEccsiPrivateKey(priv, out, &sz);
  39092. if (ret != 0)
  39093. return WC_TEST_RET_ENC_EC(ret);
  39094. if (sz != 32)
  39095. return WC_TEST_RET_ENC_NC;
  39096. if (XMEMCMP(data, out, sz) != 0)
  39097. return WC_TEST_RET_ENC_NC;
  39098. return 0;
  39099. }
  39100. static wc_test_ret_t eccsi_imp_exp_pubkey_test(EccsiKey* key1, EccsiKey* key2)
  39101. {
  39102. wc_test_ret_t ret;
  39103. byte data[32 * 2 + 1];
  39104. byte pubData[32 * 2 + 1];
  39105. word32 sz;
  39106. ret = wc_ExportEccsiPublicKey(key1, NULL, &sz, 1);
  39107. if (ret != LENGTH_ONLY_E)
  39108. return WC_TEST_RET_ENC_EC(ret);
  39109. if (sz != 32 * 2)
  39110. return WC_TEST_RET_ENC_NC;
  39111. ret = wc_ExportEccsiPublicKey(key1, data, &sz, 1);
  39112. if (ret != 0)
  39113. return WC_TEST_RET_ENC_EC(ret);
  39114. ret = wc_ImportEccsiPublicKey(key2, data, sz, 1);
  39115. if (ret != 0)
  39116. return WC_TEST_RET_ENC_EC(ret);
  39117. sz = sizeof(pubData);
  39118. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 1);
  39119. if (ret != 0)
  39120. return WC_TEST_RET_ENC_EC(ret);
  39121. if (sz != 32 * 2)
  39122. return WC_TEST_RET_ENC_NC;
  39123. if (XMEMCMP(data, pubData, sz) != 0)
  39124. return WC_TEST_RET_ENC_NC;
  39125. sz = sizeof(pubData);
  39126. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 0);
  39127. if (ret != 0)
  39128. return WC_TEST_RET_ENC_EC(ret);
  39129. if (sz != 32 * 2 + 1)
  39130. return WC_TEST_RET_ENC_NC;
  39131. if (pubData[0] != 0x04)
  39132. return WC_TEST_RET_ENC_NC;
  39133. if (XMEMCMP(pubData + 1, data, sz - 1) != 0)
  39134. return WC_TEST_RET_ENC_NC;
  39135. ret = wc_ImportEccsiPublicKey(key2, pubData, sz, 0);
  39136. if (ret != 0)
  39137. return WC_TEST_RET_ENC_EC(ret);
  39138. return 0;
  39139. }
  39140. static wc_test_ret_t eccsi_make_key_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  39141. mp_int* ssk, ecc_point* pvt)
  39142. {
  39143. wc_test_ret_t ret;
  39144. char mail[] = "test@wolfssl.com";
  39145. byte* id = (byte*)mail;
  39146. word32 idSz = (word32) XSTRLEN(mail);
  39147. int valid;
  39148. ret = wc_MakeEccsiKey(priv, rng);
  39149. if (ret != 0)
  39150. return WC_TEST_RET_ENC_EC(ret);
  39151. ret = eccsi_imp_exp_key_test(priv);
  39152. if (ret < 0)
  39153. return ret;
  39154. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  39155. if (ret < 0)
  39156. return ret;
  39157. ret = wc_MakeEccsiPair(priv, rng, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt);
  39158. if (ret != 0)
  39159. return WC_TEST_RET_ENC_EC(ret);
  39160. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  39161. &valid);
  39162. if (ret != 0)
  39163. return WC_TEST_RET_ENC_EC(ret);
  39164. if (!valid)
  39165. return WC_TEST_RET_ENC_NC;
  39166. ret = eccsi_enc_dec_pair_test(priv, ssk, pvt);
  39167. if (ret != 0)
  39168. return ret;
  39169. return 0;
  39170. }
  39171. static wc_test_ret_t eccsi_sign_verify_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  39172. mp_int* ssk, ecc_point* pvt)
  39173. {
  39174. wc_test_ret_t ret;
  39175. byte hashPriv[WC_MAX_DIGEST_SIZE];
  39176. byte hashPub[WC_MAX_DIGEST_SIZE];
  39177. byte hashSz;
  39178. byte sig[144];
  39179. word32 sigSz;
  39180. int verified, valid;
  39181. char mail[] = "test@wolfssl.com";
  39182. byte* id = (byte*)mail;
  39183. word32 idSz = (word32) XSTRLEN(mail);
  39184. byte msg[] = { 0x00 };
  39185. word32 msgSz = sizeof(msg);
  39186. #ifdef WOLFSSL_SHA384
  39187. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA384, id, idSz, pvt, hashPriv,
  39188. &hashSz);
  39189. if (ret != BAD_FUNC_ARG)
  39190. return WC_TEST_RET_ENC_EC(ret);
  39191. #endif
  39192. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPriv,
  39193. &hashSz);
  39194. if (ret != 0)
  39195. return WC_TEST_RET_ENC_EC(ret);
  39196. if (hashSz != 32)
  39197. return WC_TEST_RET_ENC_NC;
  39198. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPub,
  39199. &hashSz);
  39200. if (ret != 0)
  39201. return WC_TEST_RET_ENC_EC(ret);
  39202. if (hashSz != 32)
  39203. return WC_TEST_RET_ENC_NC;
  39204. if (XMEMCMP(hashPriv, hashPub, hashSz) != 0)
  39205. return WC_TEST_RET_ENC_NC;
  39206. ret = wc_SetEccsiHash(priv, hashPriv, hashSz);
  39207. if (ret != 0)
  39208. return WC_TEST_RET_ENC_EC(ret);
  39209. ret = wc_SetEccsiPair(priv, ssk, pvt);
  39210. if (ret != 0)
  39211. return WC_TEST_RET_ENC_EC(ret);
  39212. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, NULL,
  39213. &sigSz);
  39214. if (ret != LENGTH_ONLY_E)
  39215. return WC_TEST_RET_ENC_EC(ret);
  39216. if (sigSz != 129)
  39217. return WC_TEST_RET_ENC_NC;
  39218. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, sig,
  39219. &sigSz);
  39220. if (ret != 0)
  39221. return WC_TEST_RET_ENC_EC(ret);
  39222. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  39223. if (ret != 0)
  39224. return WC_TEST_RET_ENC_EC(ret);
  39225. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  39226. &verified);
  39227. if (ret != 0)
  39228. return WC_TEST_RET_ENC_EC(ret);
  39229. if (!verified)
  39230. return WC_TEST_RET_ENC_NC;
  39231. /* Check that changing HS results in verification failure. */
  39232. hashPub[0] ^= 0x80;
  39233. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  39234. if (ret != 0)
  39235. return WC_TEST_RET_ENC_EC(ret);
  39236. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  39237. &verified);
  39238. if (ret != 0)
  39239. return WC_TEST_RET_ENC_EC(ret);
  39240. if (verified)
  39241. return WC_TEST_RET_ENC_NC;
  39242. hashPub[0] ^= 0x80;
  39243. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  39244. if (ret != 0)
  39245. return WC_TEST_RET_ENC_EC(ret);
  39246. /* Check that changing msg results in verification failure. */
  39247. msg[0] ^= 0x80;
  39248. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  39249. &verified);
  39250. if (ret != 0)
  39251. return WC_TEST_RET_ENC_EC(ret);
  39252. if (verified)
  39253. return WC_TEST_RET_ENC_NC;
  39254. msg[0] ^= 0x80;
  39255. /* Check that changing signature results in verification failure. */
  39256. sig[0] ^= 0x80;
  39257. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  39258. &verified);
  39259. if (ret != 0)
  39260. return WC_TEST_RET_ENC_EC(ret);
  39261. if (verified)
  39262. return WC_TEST_RET_ENC_NC;
  39263. sig[0] ^= 0x80;
  39264. /* Check that key state hasn't been invalidated. */
  39265. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  39266. &verified);
  39267. if (ret != 0)
  39268. return WC_TEST_RET_ENC_EC(ret);
  39269. if (!verified)
  39270. return WC_TEST_RET_ENC_NC;
  39271. /* Check that verifying with the private key works. */
  39272. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  39273. &verified);
  39274. if (ret != 0)
  39275. return WC_TEST_RET_ENC_EC(ret);
  39276. if (!verified)
  39277. return WC_TEST_RET_ENC_NC;
  39278. /* Check that the KPAK is converted from montgomery form. */
  39279. ret = eccsi_imp_exp_key_test(priv);
  39280. if (ret != 0)
  39281. return ret;
  39282. /* Check that KPAK can converted to Montgomery form again. */
  39283. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  39284. &verified);
  39285. if (ret != 0)
  39286. return WC_TEST_RET_ENC_EC(ret);
  39287. if (!verified)
  39288. return WC_TEST_RET_ENC_NC;
  39289. /* Check that the KPAK is converted from montgomery form. */
  39290. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  39291. &valid);
  39292. if (ret != 0)
  39293. return WC_TEST_RET_ENC_EC(ret);
  39294. if (!valid)
  39295. return WC_TEST_RET_ENC_NC;
  39296. /* Check that KPAK can converted to Montgomery form again. */
  39297. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  39298. &verified);
  39299. if (ret != 0)
  39300. return WC_TEST_RET_ENC_EC(ret);
  39301. if (!verified)
  39302. return WC_TEST_RET_ENC_NC;
  39303. /* Check that the KPAK is converted from montgomery form. */
  39304. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  39305. if (ret != 0)
  39306. return ret;
  39307. return 0;
  39308. }
  39309. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t eccsi_test(void)
  39310. {
  39311. wc_test_ret_t ret = 0;
  39312. WC_RNG rng;
  39313. int rng_inited = 0;
  39314. EccsiKey* priv = NULL;
  39315. EccsiKey* pub = NULL;
  39316. mp_int* ssk = NULL;
  39317. ecc_point* pvt = NULL;
  39318. WOLFSSL_ENTER("eccsi_test");
  39319. priv = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  39320. DYNAMIC_TYPE_TMP_BUFFER);
  39321. if (priv == NULL)
  39322. ret = WC_TEST_RET_ENC_NC;
  39323. else
  39324. XMEMSET(priv, 0, sizeof(*priv));
  39325. if (ret == 0) {
  39326. pub = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  39327. DYNAMIC_TYPE_TMP_BUFFER);
  39328. if (pub == NULL)
  39329. ret = WC_TEST_RET_ENC_NC;
  39330. else
  39331. XMEMSET(pub, 0, sizeof(*pub));
  39332. }
  39333. if (ret == 0) {
  39334. ssk = (mp_int*)XMALLOC(sizeof(mp_int), HEAP_HINT,
  39335. DYNAMIC_TYPE_TMP_BUFFER);
  39336. if (ssk == NULL)
  39337. ret = WC_TEST_RET_ENC_NC;
  39338. else
  39339. XMEMSET(ssk, 0, sizeof(*ssk));
  39340. }
  39341. if (ret == 0) {
  39342. #ifndef HAVE_FIPS
  39343. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  39344. #else
  39345. ret = wc_InitRng(&rng);
  39346. #endif
  39347. if (ret != 0)
  39348. ret = WC_TEST_RET_ENC_EC(ret);
  39349. else
  39350. rng_inited = 1;
  39351. }
  39352. if (ret == 0) {
  39353. pvt = wc_ecc_new_point();
  39354. if (pvt == NULL)
  39355. ret = WC_TEST_RET_ENC_NC;
  39356. }
  39357. if (ret == 0) {
  39358. ret = mp_init(ssk);
  39359. if (ret != 0)
  39360. ret = WC_TEST_RET_ENC_EC(ret);
  39361. }
  39362. if (ret == 0) {
  39363. ret = eccsi_api_test(&rng, priv, ssk, pvt);
  39364. }
  39365. if (ret == 0) {
  39366. ret = wc_InitEccsiKey(pub, HEAP_HINT, INVALID_DEVID);
  39367. if (ret != 0)
  39368. ret = WC_TEST_RET_ENC_EC(ret);
  39369. }
  39370. if (ret == 0) {
  39371. ret = wc_InitEccsiKey(priv, HEAP_HINT, INVALID_DEVID);
  39372. if (ret != 0)
  39373. ret = WC_TEST_RET_ENC_EC(ret);
  39374. }
  39375. if (ret == 0) {
  39376. ret = eccsi_kat_verify_test(pub, pvt);
  39377. }
  39378. if (ret == 0) {
  39379. ret = eccsi_make_key_test(priv, pub, &rng, ssk, pvt);
  39380. }
  39381. if (ret == 0) {
  39382. ret = eccsi_sign_verify_test(priv, pub, &rng, ssk, pvt);
  39383. }
  39384. if (pvt != NULL)
  39385. wc_ecc_del_point(pvt);
  39386. if (rng_inited)
  39387. wc_FreeRng(&rng);
  39388. if (ssk != NULL) {
  39389. mp_free(ssk);
  39390. XFREE(ssk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39391. }
  39392. if (pub != NULL) {
  39393. wc_FreeEccsiKey(pub);
  39394. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39395. }
  39396. if (priv != NULL) {
  39397. wc_FreeEccsiKey(priv);
  39398. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39399. }
  39400. return ret;
  39401. }
  39402. #endif /* WOLFCRYPT_HAVE_ECCSI */
  39403. #ifdef WOLFCRYPT_HAVE_SAKKE
  39404. static wc_test_ret_t sakke_api_test(WC_RNG* rng, SakkeKey* key, ecc_point* rsk)
  39405. {
  39406. wc_test_ret_t ret;
  39407. byte id[1] = { 0x00 };
  39408. int valid;
  39409. byte data[256];
  39410. word32 sz;
  39411. byte auth[257];
  39412. word16 authSz;
  39413. byte ssv[256];
  39414. word16 ssvSz;
  39415. word32 len;
  39416. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  39417. if (ret != BAD_FUNC_ARG)
  39418. return WC_TEST_RET_ENC_EC(ret);
  39419. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  39420. if (ret != BAD_FUNC_ARG)
  39421. return WC_TEST_RET_ENC_EC(ret);
  39422. wc_FreeSakkeKey(NULL);
  39423. XMEMSET(key, 0, sizeof(*key));
  39424. wc_FreeSakkeKey(key);
  39425. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  39426. if (ret != 0)
  39427. return WC_TEST_RET_ENC_EC(ret);
  39428. ret = wc_MakeSakkeKey(NULL, NULL);
  39429. if (ret != BAD_FUNC_ARG)
  39430. return WC_TEST_RET_ENC_EC(ret);
  39431. ret = wc_MakeSakkeKey(key, NULL);
  39432. if (ret != BAD_FUNC_ARG)
  39433. return WC_TEST_RET_ENC_EC(ret);
  39434. ret = wc_MakeSakkeKey(NULL, rng);
  39435. if (ret != BAD_FUNC_ARG)
  39436. return WC_TEST_RET_ENC_EC(ret);
  39437. ret = wc_MakeSakkePublicKey(NULL, NULL);
  39438. if (ret != BAD_FUNC_ARG)
  39439. return WC_TEST_RET_ENC_EC(ret);
  39440. ret = wc_MakeSakkePublicKey(key, NULL);
  39441. if (ret != BAD_FUNC_ARG)
  39442. return WC_TEST_RET_ENC_EC(ret);
  39443. ret = wc_MakeSakkePublicKey(NULL, rsk);
  39444. if (ret != BAD_FUNC_ARG)
  39445. return WC_TEST_RET_ENC_EC(ret);
  39446. ret = wc_MakeSakkeRsk(NULL, NULL, 1, NULL);
  39447. if (ret != BAD_FUNC_ARG)
  39448. return WC_TEST_RET_ENC_EC(ret);
  39449. ret = wc_MakeSakkeRsk(key, id, 1, NULL);
  39450. if (ret != BAD_FUNC_ARG)
  39451. return WC_TEST_RET_ENC_EC(ret);
  39452. ret = wc_MakeSakkeRsk(key, NULL, 1, rsk);
  39453. if (ret != BAD_FUNC_ARG)
  39454. return WC_TEST_RET_ENC_EC(ret);
  39455. ret = wc_MakeSakkeRsk(NULL, id, 1, rsk);
  39456. if (ret != BAD_FUNC_ARG)
  39457. return WC_TEST_RET_ENC_EC(ret);
  39458. ret = wc_ValidateSakkeRsk(NULL, NULL, 1, NULL, NULL);
  39459. if (ret != BAD_FUNC_ARG)
  39460. return WC_TEST_RET_ENC_EC(ret);
  39461. ret = wc_ValidateSakkeRsk(key, id, 1, rsk, NULL);
  39462. if (ret != BAD_FUNC_ARG)
  39463. return WC_TEST_RET_ENC_EC(ret);
  39464. ret = wc_ValidateSakkeRsk(NULL, id, 1, rsk, &valid);
  39465. if (ret != BAD_FUNC_ARG)
  39466. return WC_TEST_RET_ENC_EC(ret);
  39467. ret = wc_ExportSakkeKey(NULL, NULL, NULL);
  39468. if (ret != BAD_FUNC_ARG)
  39469. return WC_TEST_RET_ENC_EC(ret);
  39470. ret = wc_ExportSakkeKey(key, data, NULL);
  39471. if (ret != BAD_FUNC_ARG)
  39472. return WC_TEST_RET_ENC_EC(ret);
  39473. ret = wc_ExportSakkeKey(NULL, data, &sz);
  39474. if (ret != BAD_FUNC_ARG)
  39475. return WC_TEST_RET_ENC_EC(ret);
  39476. ret = wc_ImportSakkeKey(NULL, NULL, 1);
  39477. if (ret != BAD_FUNC_ARG)
  39478. return WC_TEST_RET_ENC_EC(ret);
  39479. ret = wc_ImportSakkeKey(key, NULL, 1);
  39480. if (ret != BAD_FUNC_ARG)
  39481. return WC_TEST_RET_ENC_EC(ret);
  39482. ret = wc_ImportSakkeKey(NULL, data, 1);
  39483. if (ret != BAD_FUNC_ARG)
  39484. return WC_TEST_RET_ENC_EC(ret);
  39485. ret = wc_ExportSakkePrivateKey(NULL, NULL, NULL);
  39486. if (ret != BAD_FUNC_ARG)
  39487. return WC_TEST_RET_ENC_EC(ret);
  39488. ret = wc_ExportSakkePrivateKey(key, data, NULL);
  39489. if (ret != BAD_FUNC_ARG)
  39490. return WC_TEST_RET_ENC_EC(ret);
  39491. ret = wc_ExportSakkePrivateKey(NULL, data, &sz);
  39492. if (ret != BAD_FUNC_ARG)
  39493. return WC_TEST_RET_ENC_EC(ret);
  39494. ret = wc_ImportSakkePrivateKey(NULL, NULL, 1);
  39495. if (ret != BAD_FUNC_ARG)
  39496. return WC_TEST_RET_ENC_EC(ret);
  39497. ret = wc_ImportSakkePrivateKey(key, NULL, 1);
  39498. if (ret != BAD_FUNC_ARG)
  39499. return WC_TEST_RET_ENC_EC(ret);
  39500. ret = wc_ImportSakkePrivateKey(NULL, data, 1);
  39501. if (ret != BAD_FUNC_ARG)
  39502. return WC_TEST_RET_ENC_EC(ret);
  39503. sz = sizeof(data);
  39504. ret = wc_EncodeSakkeRsk(NULL, NULL, data, NULL, 1);
  39505. if (ret != BAD_FUNC_ARG)
  39506. return WC_TEST_RET_ENC_EC(ret);
  39507. ret = wc_EncodeSakkeRsk(key, rsk, data, NULL, 1);
  39508. if (ret != BAD_FUNC_ARG)
  39509. return WC_TEST_RET_ENC_EC(ret);
  39510. ret = wc_EncodeSakkeRsk(key, NULL, data, &sz, 1);
  39511. if (ret != BAD_FUNC_ARG)
  39512. return WC_TEST_RET_ENC_EC(ret);
  39513. ret = wc_EncodeSakkeRsk(NULL, rsk, data, &sz, 1);
  39514. if (ret != BAD_FUNC_ARG)
  39515. return WC_TEST_RET_ENC_EC(ret);
  39516. ret = wc_DecodeSakkeRsk(NULL, NULL, sz, NULL);
  39517. if (ret != BAD_FUNC_ARG)
  39518. return WC_TEST_RET_ENC_EC(ret);
  39519. ret = wc_DecodeSakkeRsk(key, data, sz, NULL);
  39520. if (ret != BAD_FUNC_ARG)
  39521. return WC_TEST_RET_ENC_EC(ret);
  39522. ret = wc_DecodeSakkeRsk(key, NULL, sz, rsk);
  39523. if (ret != BAD_FUNC_ARG)
  39524. return WC_TEST_RET_ENC_EC(ret);
  39525. ret = wc_DecodeSakkeRsk(NULL, data, sz, rsk);
  39526. if (ret != BAD_FUNC_ARG)
  39527. return WC_TEST_RET_ENC_EC(ret);
  39528. ret = wc_ImportSakkeRsk(NULL, NULL, sz);
  39529. if (ret != BAD_FUNC_ARG)
  39530. return WC_TEST_RET_ENC_EC(ret);
  39531. ret = wc_ImportSakkeRsk(key, NULL, sz);
  39532. if (ret != BAD_FUNC_ARG)
  39533. return WC_TEST_RET_ENC_EC(ret);
  39534. ret = wc_ImportSakkeRsk(NULL, data, sz);
  39535. if (ret != BAD_FUNC_ARG)
  39536. return WC_TEST_RET_ENC_EC(ret);
  39537. ret = wc_ImportSakkeRsk(key, data, 1);
  39538. if (ret != BUFFER_E)
  39539. return WC_TEST_RET_ENC_EC(ret);
  39540. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, NULL);
  39541. if (ret != BAD_FUNC_ARG)
  39542. return WC_TEST_RET_ENC_EC(ret);
  39543. ret = wc_GenerateSakkeRskTable(key, NULL, data, NULL);
  39544. if (ret != BAD_FUNC_ARG)
  39545. return WC_TEST_RET_ENC_EC(ret);
  39546. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, NULL);
  39547. if (ret != BAD_FUNC_ARG)
  39548. return WC_TEST_RET_ENC_EC(ret);
  39549. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, &len);
  39550. if (ret != BAD_FUNC_ARG)
  39551. return WC_TEST_RET_ENC_EC(ret);
  39552. ret = wc_GenerateSakkeRskTable(key, rsk, data, NULL);
  39553. if (ret != BAD_FUNC_ARG)
  39554. return WC_TEST_RET_ENC_EC(ret);
  39555. ret = wc_GenerateSakkeRskTable(key, NULL, data, &len);
  39556. if (ret != BAD_FUNC_ARG)
  39557. return WC_TEST_RET_ENC_EC(ret);
  39558. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, &len);
  39559. if (ret != BAD_FUNC_ARG)
  39560. return WC_TEST_RET_ENC_EC(ret);
  39561. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  39562. if (ret != LENGTH_ONLY_E)
  39563. return WC_TEST_RET_ENC_EC(ret);
  39564. len--;
  39565. ret = wc_GenerateSakkeRskTable(key, rsk, data, &len);
  39566. if (ret != BUFFER_E)
  39567. return WC_TEST_RET_ENC_EC(ret);
  39568. ret = wc_ExportSakkePublicKey(NULL, data, NULL, 1);
  39569. if (ret != BAD_FUNC_ARG)
  39570. return WC_TEST_RET_ENC_EC(ret);
  39571. ret = wc_ExportSakkePublicKey(key, data, NULL, 1);
  39572. if (ret != BAD_FUNC_ARG)
  39573. return WC_TEST_RET_ENC_EC(ret);
  39574. ret = wc_ExportSakkePublicKey(NULL, data, &sz, 1);
  39575. if (ret != BAD_FUNC_ARG)
  39576. return WC_TEST_RET_ENC_EC(ret);
  39577. ret = wc_ImportSakkePublicKey(NULL, NULL, sz, 1);
  39578. if (ret != BAD_FUNC_ARG)
  39579. return WC_TEST_RET_ENC_EC(ret);
  39580. ret = wc_ImportSakkePublicKey(key, NULL, sz, 1);
  39581. if (ret != BAD_FUNC_ARG)
  39582. return WC_TEST_RET_ENC_EC(ret);
  39583. ret = wc_ImportSakkePublicKey(NULL, data, sz, 1);
  39584. if (ret != BAD_FUNC_ARG)
  39585. return WC_TEST_RET_ENC_EC(ret);
  39586. ret = wc_GetSakkeAuthSize(NULL, NULL);
  39587. if (ret != BAD_FUNC_ARG)
  39588. return WC_TEST_RET_ENC_EC(ret);
  39589. ret = wc_GetSakkeAuthSize(key, NULL);
  39590. if (ret != BAD_FUNC_ARG)
  39591. return WC_TEST_RET_ENC_EC(ret);
  39592. ret = wc_GetSakkeAuthSize(NULL, &authSz);
  39593. if (ret != BAD_FUNC_ARG)
  39594. return WC_TEST_RET_ENC_EC(ret);
  39595. ret = wc_MakeSakkePointI(NULL, NULL, SAKKE_ID_MAX_SIZE + 1);
  39596. if (ret != BAD_FUNC_ARG)
  39597. return WC_TEST_RET_ENC_EC(ret);
  39598. ret = wc_MakeSakkePointI(key, NULL, SAKKE_ID_MAX_SIZE + 1);
  39599. if (ret != BAD_FUNC_ARG)
  39600. return WC_TEST_RET_ENC_EC(ret);
  39601. ret = wc_MakeSakkePointI(NULL, id, 1);
  39602. if (ret != BAD_FUNC_ARG)
  39603. return WC_TEST_RET_ENC_EC(ret);
  39604. ret = wc_MakeSakkePointI(NULL, NULL, 1);
  39605. if (ret != BAD_FUNC_ARG)
  39606. return WC_TEST_RET_ENC_EC(ret);
  39607. ret = wc_MakeSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1);
  39608. if (ret != BAD_FUNC_ARG)
  39609. return WC_TEST_RET_ENC_EC(ret);
  39610. ret = wc_MakeSakkePointI(key, NULL, 1);
  39611. if (ret != BAD_FUNC_ARG)
  39612. return WC_TEST_RET_ENC_EC(ret);
  39613. ret = wc_MakeSakkePointI(NULL, id, 1);
  39614. if (ret != BAD_FUNC_ARG)
  39615. return WC_TEST_RET_ENC_EC(ret);
  39616. ret = wc_GenerateSakkePointITable(NULL, data, NULL);
  39617. if (ret != BAD_FUNC_ARG)
  39618. return WC_TEST_RET_ENC_EC(ret);
  39619. ret = wc_GenerateSakkePointITable(key, data, NULL);
  39620. if (ret != BAD_FUNC_ARG)
  39621. return WC_TEST_RET_ENC_EC(ret);
  39622. ret = wc_GenerateSakkePointITable(NULL, data, &len);
  39623. if (ret != BAD_FUNC_ARG)
  39624. return WC_TEST_RET_ENC_EC(ret);
  39625. ret = wc_GenerateSakkePointITable(key, NULL, &len);
  39626. if (ret != LENGTH_ONLY_E)
  39627. return WC_TEST_RET_ENC_EC(ret);
  39628. len--;
  39629. ret = wc_GenerateSakkePointITable(key, data, &len);
  39630. if (ret != BUFFER_E)
  39631. return WC_TEST_RET_ENC_EC(ret);
  39632. ret = wc_SetSakkePointITable(NULL, NULL, 1);
  39633. if (ret != BAD_FUNC_ARG)
  39634. return WC_TEST_RET_ENC_EC(ret);
  39635. ret = wc_SetSakkePointITable(key, NULL, 1);
  39636. if (ret != BAD_FUNC_ARG)
  39637. return WC_TEST_RET_ENC_EC(ret);
  39638. ret = wc_SetSakkePointITable(NULL, data, 1);
  39639. if (ret != BAD_FUNC_ARG)
  39640. return WC_TEST_RET_ENC_EC(ret);
  39641. ret = wc_SetSakkePointITable(key, data, 1);
  39642. if (ret != BUFFER_E)
  39643. return WC_TEST_RET_ENC_EC(ret);
  39644. ret = wc_ClearSakkePointITable(NULL);
  39645. if (ret != BAD_FUNC_ARG)
  39646. return WC_TEST_RET_ENC_EC(ret);
  39647. ret = wc_GetSakkePointI(NULL, data, NULL);
  39648. if (ret != BAD_FUNC_ARG)
  39649. return WC_TEST_RET_ENC_EC(ret);
  39650. ret = wc_GetSakkePointI(key, data, NULL);
  39651. if (ret != BAD_FUNC_ARG)
  39652. return WC_TEST_RET_ENC_EC(ret);
  39653. ret = wc_GetSakkePointI(NULL, data, &sz);
  39654. if (ret != BAD_FUNC_ARG)
  39655. return WC_TEST_RET_ENC_EC(ret);
  39656. sz = 1;
  39657. ret = wc_GetSakkePointI(key, data, &sz);
  39658. if (ret != BUFFER_E)
  39659. return WC_TEST_RET_ENC_EC(ret);
  39660. sz = 256;
  39661. ret = wc_SetSakkePointI(NULL, NULL, 1, NULL, sz);
  39662. if (ret != BAD_FUNC_ARG)
  39663. return WC_TEST_RET_ENC_EC(ret);
  39664. ret = wc_SetSakkePointI(key, NULL, 1, NULL, sz);
  39665. if (ret != BAD_FUNC_ARG)
  39666. return WC_TEST_RET_ENC_EC(ret);
  39667. ret = wc_SetSakkePointI(NULL, id, 1, NULL, sz);
  39668. if (ret != BAD_FUNC_ARG)
  39669. return WC_TEST_RET_ENC_EC(ret);
  39670. ret = wc_SetSakkePointI(NULL, NULL, 1, data, sz);
  39671. if (ret != BAD_FUNC_ARG)
  39672. return WC_TEST_RET_ENC_EC(ret);
  39673. ret = wc_SetSakkePointI(key, id, 1, NULL, sz);
  39674. if (ret != BAD_FUNC_ARG)
  39675. return WC_TEST_RET_ENC_EC(ret);
  39676. ret = wc_SetSakkePointI(key, NULL, 1, data, sz);
  39677. if (ret != BAD_FUNC_ARG)
  39678. return WC_TEST_RET_ENC_EC(ret);
  39679. ret = wc_SetSakkePointI(NULL, id, 1, data, sz);
  39680. if (ret != BAD_FUNC_ARG)
  39681. return WC_TEST_RET_ENC_EC(ret);
  39682. ret = wc_SetSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1, data, sz);
  39683. if (ret != BUFFER_E)
  39684. return WC_TEST_RET_ENC_EC(ret);
  39685. ret = wc_SetSakkePointI(key, id, 1, data, sz - 1);
  39686. if (ret != BUFFER_E)
  39687. return WC_TEST_RET_ENC_EC(ret);
  39688. ret = wc_SetSakkeIdentity(NULL, NULL, 1);
  39689. if (ret != BAD_FUNC_ARG)
  39690. return WC_TEST_RET_ENC_EC(ret);
  39691. ret = wc_SetSakkeIdentity(key, NULL, 1);
  39692. if (ret != BAD_FUNC_ARG)
  39693. return WC_TEST_RET_ENC_EC(ret);
  39694. ret = wc_SetSakkeIdentity(NULL, id, 1);
  39695. if (ret != BAD_FUNC_ARG)
  39696. return WC_TEST_RET_ENC_EC(ret);
  39697. ssvSz = sizeof(ssv);
  39698. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  39699. auth, NULL);
  39700. if (ret != BAD_FUNC_ARG)
  39701. return WC_TEST_RET_ENC_EC(ret);
  39702. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  39703. auth, NULL);
  39704. if (ret != BAD_FUNC_ARG)
  39705. return WC_TEST_RET_ENC_EC(ret);
  39706. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  39707. auth, NULL);
  39708. if (ret != BAD_FUNC_ARG)
  39709. return WC_TEST_RET_ENC_EC(ret);
  39710. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  39711. auth, &authSz);
  39712. if (ret != BAD_FUNC_ARG)
  39713. return WC_TEST_RET_ENC_EC(ret);
  39714. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  39715. auth, NULL);
  39716. if (ret != BAD_FUNC_ARG)
  39717. return WC_TEST_RET_ENC_EC(ret);
  39718. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  39719. auth, &authSz);
  39720. if (ret != BAD_FUNC_ARG)
  39721. return WC_TEST_RET_ENC_EC(ret);
  39722. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  39723. auth, &authSz);
  39724. if (ret != BAD_FUNC_ARG)
  39725. return WC_TEST_RET_ENC_EC(ret);
  39726. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  39727. auth, &authSz);
  39728. if (ret != BAD_STATE_E)
  39729. return WC_TEST_RET_ENC_EC(ret);
  39730. ret = wc_GenerateSakkeSSV(NULL, NULL, data, NULL);
  39731. if (ret != BAD_FUNC_ARG)
  39732. return WC_TEST_RET_ENC_EC(ret);
  39733. ret = wc_GenerateSakkeSSV(key, rng, data, NULL);
  39734. if (ret != BAD_FUNC_ARG)
  39735. return WC_TEST_RET_ENC_EC(ret);
  39736. ret = wc_GenerateSakkeSSV(key, NULL, data, &ssvSz);
  39737. if (ret != BAD_FUNC_ARG)
  39738. return WC_TEST_RET_ENC_EC(ret);
  39739. ret = wc_GenerateSakkeSSV(NULL, rng, data, &ssvSz);
  39740. if (ret != BAD_FUNC_ARG)
  39741. return WC_TEST_RET_ENC_EC(ret);
  39742. ret = wc_SetSakkeRsk(NULL, NULL, data, 1);
  39743. if (ret != BAD_FUNC_ARG)
  39744. return WC_TEST_RET_ENC_EC(ret);
  39745. ret = wc_SetSakkeRsk(key, NULL, data, 1);
  39746. if (ret != BAD_FUNC_ARG)
  39747. return WC_TEST_RET_ENC_EC(ret);
  39748. ret = wc_SetSakkeRsk(NULL, rsk, data, 1);
  39749. if (ret != BAD_FUNC_ARG)
  39750. return WC_TEST_RET_ENC_EC(ret);
  39751. ssvSz = sizeof(ssv);
  39752. authSz = sizeof(auth);
  39753. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  39754. authSz);
  39755. if (ret != BAD_FUNC_ARG)
  39756. return WC_TEST_RET_ENC_EC(ret);
  39757. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  39758. authSz);
  39759. if (ret != BAD_FUNC_ARG)
  39760. return WC_TEST_RET_ENC_EC(ret);
  39761. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  39762. authSz);
  39763. if (ret != BAD_FUNC_ARG)
  39764. return WC_TEST_RET_ENC_EC(ret);
  39765. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  39766. authSz);
  39767. if (ret != BAD_FUNC_ARG)
  39768. return WC_TEST_RET_ENC_EC(ret);
  39769. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  39770. authSz);
  39771. if (ret != BAD_FUNC_ARG)
  39772. return WC_TEST_RET_ENC_EC(ret);
  39773. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  39774. authSz);
  39775. if (ret != BAD_FUNC_ARG)
  39776. return WC_TEST_RET_ENC_EC(ret);
  39777. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  39778. authSz);
  39779. if (ret != BAD_FUNC_ARG)
  39780. return WC_TEST_RET_ENC_EC(ret);
  39781. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  39782. authSz);
  39783. if (ret != BAD_STATE_E)
  39784. return WC_TEST_RET_ENC_EC(ret);
  39785. ret = wc_SetSakkeIdentity(key, id, 1);
  39786. if (ret != 0)
  39787. return WC_TEST_RET_ENC_EC(ret);
  39788. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  39789. authSz);
  39790. if (ret != BAD_STATE_E)
  39791. return WC_TEST_RET_ENC_EC(ret);
  39792. ret = wc_SetSakkeIdentity(key, id, 0);
  39793. if (ret != 0)
  39794. return WC_TEST_RET_ENC_EC(ret);
  39795. ret = wc_SetSakkeRsk(key, rsk, data, 1);
  39796. if (ret != 0)
  39797. return WC_TEST_RET_ENC_EC(ret);
  39798. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  39799. authSz);
  39800. if (ret != BAD_STATE_E)
  39801. return WC_TEST_RET_ENC_EC(ret);
  39802. wc_FreeSakkeKey(key);
  39803. return 0;
  39804. }
  39805. static wc_test_ret_t sakke_kat_derive_test(SakkeKey* key, ecc_point* rsk)
  39806. {
  39807. WOLFSSL_SMALL_STACK_STATIC const byte pubData[] = {
  39808. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  39809. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  39810. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  39811. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  39812. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  39813. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  39814. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  39815. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  39816. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  39817. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  39818. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  39819. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  39820. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  39821. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  39822. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  39823. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  39824. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  39825. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  39826. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  39827. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  39828. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  39829. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  39830. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  39831. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  39832. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  39833. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  39834. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  39835. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  39836. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  39837. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  39838. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  39839. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  39840. };
  39841. WOLFSSL_SMALL_STACK_STATIC const byte rskData[] = {
  39842. 0x93, 0xAF, 0x67, 0xE5, 0x00, 0x7B, 0xA6, 0xE6,
  39843. 0xA8, 0x0D, 0xA7, 0x93, 0xDA, 0x30, 0x0F, 0xA4,
  39844. 0xB5, 0x2D, 0x0A, 0x74, 0xE2, 0x5E, 0x6E, 0x7B,
  39845. 0x2B, 0x3D, 0x6E, 0xE9, 0xD1, 0x8A, 0x9B, 0x5C,
  39846. 0x50, 0x23, 0x59, 0x7B, 0xD8, 0x2D, 0x80, 0x62,
  39847. 0xD3, 0x40, 0x19, 0x56, 0x3B, 0xA1, 0xD2, 0x5C,
  39848. 0x0D, 0xC5, 0x6B, 0x7B, 0x97, 0x9D, 0x74, 0xAA,
  39849. 0x50, 0xF2, 0x9F, 0xBF, 0x11, 0xCC, 0x2C, 0x93,
  39850. 0xF5, 0xDF, 0xCA, 0x61, 0x5E, 0x60, 0x92, 0x79,
  39851. 0xF6, 0x17, 0x5C, 0xEA, 0xDB, 0x00, 0xB5, 0x8C,
  39852. 0x6B, 0xEE, 0x1E, 0x7A, 0x2A, 0x47, 0xC4, 0xF0,
  39853. 0xC4, 0x56, 0xF0, 0x52, 0x59, 0xA6, 0xFA, 0x94,
  39854. 0xA6, 0x34, 0xA4, 0x0D, 0xAE, 0x1D, 0xF5, 0x93,
  39855. 0xD4, 0xFE, 0xCF, 0x68, 0x8D, 0x5F, 0xC6, 0x78,
  39856. 0xBE, 0x7E, 0xFC, 0x6D, 0xF3, 0xD6, 0x83, 0x53,
  39857. 0x25, 0xB8, 0x3B, 0x2C, 0x6E, 0x69, 0x03, 0x6B,
  39858. 0x15, 0x5F, 0x0A, 0x27, 0x24, 0x10, 0x94, 0xB0,
  39859. 0x4B, 0xFB, 0x0B, 0xDF, 0xAC, 0x6C, 0x67, 0x0A,
  39860. 0x65, 0xC3, 0x25, 0xD3, 0x9A, 0x06, 0x9F, 0x03,
  39861. 0x65, 0x9D, 0x44, 0xCA, 0x27, 0xD3, 0xBE, 0x8D,
  39862. 0xF3, 0x11, 0x17, 0x2B, 0x55, 0x41, 0x60, 0x18,
  39863. 0x1C, 0xBE, 0x94, 0xA2, 0xA7, 0x83, 0x32, 0x0C,
  39864. 0xED, 0x59, 0x0B, 0xC4, 0x26, 0x44, 0x70, 0x2C,
  39865. 0xF3, 0x71, 0x27, 0x1E, 0x49, 0x6B, 0xF2, 0x0F,
  39866. 0x58, 0x8B, 0x78, 0xA1, 0xBC, 0x01, 0xEC, 0xBB,
  39867. 0x65, 0x59, 0x93, 0x4B, 0xDD, 0x2F, 0xB6, 0x5D,
  39868. 0x28, 0x84, 0x31, 0x8A, 0x33, 0xD1, 0xA4, 0x2A,
  39869. 0xDF, 0x5E, 0x33, 0xCC, 0x58, 0x00, 0x28, 0x0B,
  39870. 0x28, 0x35, 0x64, 0x97, 0xF8, 0x71, 0x35, 0xBA,
  39871. 0xB9, 0x61, 0x2A, 0x17, 0x26, 0x04, 0x24, 0x40,
  39872. 0x9A, 0xC1, 0x5F, 0xEE, 0x99, 0x6B, 0x74, 0x4C,
  39873. 0x33, 0x21, 0x51, 0x23, 0x5D, 0xEC, 0xB0, 0xF5
  39874. };
  39875. WOLFSSL_SMALL_STACK_STATIC const byte id[] = {
  39876. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  39877. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  39878. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  39879. 0x33, 0x00
  39880. };
  39881. WOLFSSL_SMALL_STACK_STATIC const byte ssv[] = {
  39882. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  39883. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  39884. };
  39885. WOLFSSL_SMALL_STACK_STATIC const byte auth[] = {
  39886. 0x04,
  39887. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  39888. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  39889. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  39890. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  39891. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  39892. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  39893. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  39894. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  39895. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  39896. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  39897. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  39898. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  39899. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  39900. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  39901. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  39902. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  39903. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  39904. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  39905. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  39906. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  39907. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  39908. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  39909. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  39910. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  39911. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  39912. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  39913. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  39914. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  39915. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  39916. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  39917. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  39918. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  39919. };
  39920. WOLFSSL_SMALL_STACK_STATIC const byte encSsv[] = {
  39921. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  39922. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  39923. };
  39924. wc_test_ret_t ret;
  39925. int valid;
  39926. byte pubKey[sizeof(pubData) + 1];
  39927. word32 sz = sizeof(pubKey);
  39928. byte tmpSsv[sizeof(encSsv)];
  39929. byte* iTable = NULL;
  39930. word32 iTableLen;
  39931. byte* table = NULL;
  39932. word32 len;
  39933. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  39934. if (ret != 0)
  39935. return WC_TEST_RET_ENC_EC(ret);
  39936. ret = wc_DecodeSakkeRsk(key, rskData, sizeof(rskData), rsk);
  39937. if (ret != 0)
  39938. return WC_TEST_RET_ENC_EC(ret);
  39939. ret = wc_ValidateSakkeRsk(key, id, sizeof(id), rsk, &valid);
  39940. if (ret != 0)
  39941. return WC_TEST_RET_ENC_EC(ret);
  39942. if (valid != 1)
  39943. return WC_TEST_RET_ENC_NC;
  39944. ret = wc_SetSakkeRsk(key, rsk, NULL, 0);
  39945. if (ret != 0)
  39946. return WC_TEST_RET_ENC_EC(ret);
  39947. ret = wc_SetSakkeIdentity(key, id, sizeof(id));
  39948. if (ret != 0)
  39949. return WC_TEST_RET_ENC_EC(ret);
  39950. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  39951. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  39952. auth, sizeof(auth));
  39953. if (ret != 0)
  39954. return WC_TEST_RET_ENC_EC(ret);
  39955. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  39956. return WC_TEST_RET_ENC_NC;
  39957. ret = wc_MakeSakkePointI(key, id, sizeof(id));
  39958. if (ret != 0)
  39959. return WC_TEST_RET_ENC_EC(ret);
  39960. iTableLen = 0;
  39961. ret = wc_GenerateSakkePointITable(key, NULL, &iTableLen);
  39962. if (ret != LENGTH_ONLY_E)
  39963. return WC_TEST_RET_ENC_EC(ret);
  39964. if (iTableLen != 0) {
  39965. iTable = (byte*)XMALLOC(iTableLen, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39966. if (iTable == NULL)
  39967. return WC_TEST_RET_ENC_ERRNO;
  39968. ret = wc_GenerateSakkePointITable(key, iTable, &iTableLen);
  39969. if (ret != 0)
  39970. return WC_TEST_RET_ENC_EC(ret);
  39971. }
  39972. len = 0;
  39973. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  39974. if (ret != LENGTH_ONLY_E)
  39975. return WC_TEST_RET_ENC_EC(ret);
  39976. if (len > 0) {
  39977. table = (byte*)XMALLOC(len, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39978. if (table == NULL)
  39979. return WC_TEST_RET_ENC_ERRNO;
  39980. ret = wc_GenerateSakkeRskTable(key, rsk, table, &len);
  39981. if (ret != 0)
  39982. return WC_TEST_RET_ENC_EC(ret);
  39983. }
  39984. ret = wc_SetSakkeRsk(key, rsk, table, len);
  39985. if (ret != 0)
  39986. return WC_TEST_RET_ENC_EC(ret);
  39987. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  39988. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  39989. auth, sizeof(auth));
  39990. if (ret != 0)
  39991. return WC_TEST_RET_ENC_EC(ret);
  39992. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  39993. return WC_TEST_RET_ENC_NC;
  39994. /* Don't reference table that is about to be freed. */
  39995. ret = wc_ClearSakkePointITable(key);
  39996. if (ret != 0)
  39997. return WC_TEST_RET_ENC_EC(ret);
  39998. /* Dispose of tables */
  39999. if (iTable != NULL)
  40000. XFREE(iTable, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40001. if (table != NULL)
  40002. XFREE(table, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40003. /* Make sure the key public key is exportable - convert to Montgomery form
  40004. * in Validation.
  40005. */
  40006. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 1);
  40007. if (ret != 0)
  40008. return WC_TEST_RET_ENC_EC(ret);
  40009. if (sz != sizeof(pubData))
  40010. return WC_TEST_RET_ENC_NC;
  40011. if (XMEMCMP(pubKey, pubData, sizeof(pubData)) != 0)
  40012. return WC_TEST_RET_ENC_NC;
  40013. sz = sizeof(pubData) + 1;
  40014. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 0);
  40015. if (ret != 0)
  40016. return WC_TEST_RET_ENC_EC(ret);
  40017. if (sz != sizeof(pubData) + 1)
  40018. return WC_TEST_RET_ENC_NC;
  40019. if (pubKey[0] != 0x04)
  40020. return WC_TEST_RET_ENC_NC;
  40021. if (XMEMCMP(pubKey + 1, pubData, sizeof(pubData)) != 0)
  40022. return WC_TEST_RET_ENC_NC;
  40023. return 0;
  40024. }
  40025. static wc_test_ret_t sakke_kat_encapsulate_test(SakkeKey* key)
  40026. {
  40027. static const byte pubData[] = {
  40028. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  40029. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  40030. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  40031. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  40032. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  40033. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  40034. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  40035. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  40036. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  40037. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  40038. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  40039. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  40040. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  40041. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  40042. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  40043. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  40044. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  40045. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  40046. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  40047. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  40048. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  40049. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  40050. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  40051. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  40052. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  40053. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  40054. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  40055. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  40056. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  40057. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  40058. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  40059. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  40060. };
  40061. static const byte id[] = {
  40062. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  40063. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  40064. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  40065. 0x33, 0x00
  40066. };
  40067. static const word32 idSz = sizeof(id);
  40068. byte ssv[] = {
  40069. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  40070. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  40071. };
  40072. static const word16 ssvSz = sizeof(ssv);
  40073. static const byte expAuth[] = {
  40074. 0x04,
  40075. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  40076. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  40077. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  40078. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  40079. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  40080. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  40081. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  40082. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  40083. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  40084. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  40085. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  40086. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  40087. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  40088. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  40089. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  40090. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  40091. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  40092. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  40093. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  40094. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  40095. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  40096. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  40097. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  40098. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  40099. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  40100. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  40101. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  40102. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  40103. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  40104. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  40105. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  40106. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  40107. };
  40108. static const byte encSsv[] = {
  40109. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  40110. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  40111. };
  40112. wc_test_ret_t ret;
  40113. byte auth[257];
  40114. word16 authSz = sizeof(auth);
  40115. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  40116. if (ret != 0)
  40117. return WC_TEST_RET_ENC_EC(ret);
  40118. ret = wc_SetSakkeIdentity(key, id, idSz);
  40119. if (ret != 0)
  40120. return WC_TEST_RET_ENC_EC(ret);
  40121. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  40122. auth, &authSz);
  40123. if (ret != 0)
  40124. return WC_TEST_RET_ENC_EC(ret);
  40125. if (authSz != 257)
  40126. return WC_TEST_RET_ENC_NC;
  40127. if (XMEMCMP(ssv, encSsv, ssvSz) != 0)
  40128. return WC_TEST_RET_ENC_NC;
  40129. if (XMEMCMP(auth, expAuth, authSz) != 0)
  40130. return WC_TEST_RET_ENC_NC;
  40131. return 0;
  40132. }
  40133. static wc_test_ret_t sakke_make_key_test(SakkeKey* priv, SakkeKey* pub, SakkeKey* key,
  40134. WC_RNG* rng, ecc_point* rsk)
  40135. {
  40136. wc_test_ret_t ret;
  40137. byte data[440];
  40138. byte pubData[257];
  40139. word32 sz;
  40140. char mail[] = "test@wolfssl.com";
  40141. byte* id = (byte*)mail;
  40142. word32 idSz = (word32)XSTRLEN(mail);
  40143. int valid;
  40144. ecc_point* pubKey = rsk;
  40145. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  40146. if (ret != 0)
  40147. return WC_TEST_RET_ENC_EC(ret);
  40148. ret = wc_MakeSakkeKey(priv, rng);
  40149. if (ret != 0)
  40150. return WC_TEST_RET_ENC_EC(ret);
  40151. ret = wc_ExportSakkeKey(priv, NULL, &sz);
  40152. if (ret != LENGTH_ONLY_E)
  40153. return WC_TEST_RET_ENC_EC(ret);
  40154. if (sz != 384)
  40155. return WC_TEST_RET_ENC_NC;
  40156. sz--;
  40157. ret = wc_ExportSakkeKey(priv, data, &sz);
  40158. if (ret == 0)
  40159. return WC_TEST_RET_ENC_NC;
  40160. sz++;
  40161. ret = wc_ExportSakkeKey(priv, data, &sz);
  40162. if (ret != 0)
  40163. return WC_TEST_RET_ENC_EC(ret);
  40164. if (sz != 384)
  40165. return WC_TEST_RET_ENC_NC;
  40166. ret = wc_ImportSakkeKey(key, data, sz - 1);
  40167. if (ret == 0)
  40168. return WC_TEST_RET_ENC_NC;
  40169. ret = wc_ImportSakkeKey(key, data, sz);
  40170. if (ret != 0)
  40171. return WC_TEST_RET_ENC_EC(ret);
  40172. wc_FreeSakkeKey(key);
  40173. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  40174. if (ret != 0)
  40175. return WC_TEST_RET_ENC_EC(ret);
  40176. ret = wc_ExportSakkePrivateKey(priv, NULL, &sz);
  40177. if (ret != LENGTH_ONLY_E)
  40178. return WC_TEST_RET_ENC_EC(ret);
  40179. if (sz != 128)
  40180. return WC_TEST_RET_ENC_NC;
  40181. sz--;
  40182. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  40183. if (ret == 0)
  40184. return WC_TEST_RET_ENC_NC;
  40185. sz++;
  40186. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  40187. if (ret != 0)
  40188. return WC_TEST_RET_ENC_EC(ret);
  40189. if (sz != 128)
  40190. return WC_TEST_RET_ENC_NC;
  40191. ret = wc_ImportSakkePrivateKey(key, data, sz - 1);
  40192. if (ret == 0)
  40193. return WC_TEST_RET_ENC_NC;
  40194. ret = wc_ImportSakkePrivateKey(key, data, sz);
  40195. if (ret != 0)
  40196. return WC_TEST_RET_ENC_EC(ret);
  40197. ret = wc_MakeSakkePublicKey(key, pubKey);
  40198. if (ret != 0)
  40199. return WC_TEST_RET_ENC_EC(ret);
  40200. ret = wc_ExportSakkePublicKey(priv, NULL, &sz, 1);
  40201. if (ret != LENGTH_ONLY_E)
  40202. return WC_TEST_RET_ENC_EC(ret);
  40203. if (sz != 256)
  40204. return WC_TEST_RET_ENC_NC;
  40205. sz--;
  40206. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  40207. if (ret == 0)
  40208. return WC_TEST_RET_ENC_NC;
  40209. sz++;
  40210. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  40211. if (ret != 0)
  40212. return WC_TEST_RET_ENC_EC(ret);
  40213. if (sz != 256)
  40214. return WC_TEST_RET_ENC_NC;
  40215. ret = wc_ImportSakkePublicKey(pub, data, sz - 1, 1);
  40216. if (ret == 0)
  40217. return WC_TEST_RET_ENC_NC;
  40218. ret = wc_ImportSakkePublicKey(pub, data, sz, 1);
  40219. if (ret != 0)
  40220. return WC_TEST_RET_ENC_EC(ret);
  40221. ret = wc_ExportSakkePublicKey(pub, pubData, &sz, 1);
  40222. if (ret != 0)
  40223. return WC_TEST_RET_ENC_EC(ret);
  40224. if (sz != 256)
  40225. return WC_TEST_RET_ENC_NC;
  40226. if (XMEMCMP(data, pubData, sz) != 0)
  40227. return WC_TEST_RET_ENC_NC;
  40228. ret = wc_MakeSakkeRsk(priv, id, idSz, rsk);
  40229. if (ret != 0)
  40230. return WC_TEST_RET_ENC_EC(ret);
  40231. ret = wc_ValidateSakkeRsk(priv, id, idSz, rsk, &valid);
  40232. if (ret != 0)
  40233. return WC_TEST_RET_ENC_EC(ret);
  40234. if (valid != 1)
  40235. return WC_TEST_RET_ENC_NC;
  40236. ret = wc_ValidateSakkeRsk(pub, id, idSz, rsk, &valid);
  40237. if (ret != 0)
  40238. return WC_TEST_RET_ENC_EC(ret);
  40239. if (valid != 1)
  40240. return WC_TEST_RET_ENC_NC;
  40241. sz = sizeof(data);
  40242. ret = wc_EncodeSakkeRsk(priv, rsk, data, &sz, 1);
  40243. if (ret != 0)
  40244. return WC_TEST_RET_ENC_EC(ret);
  40245. if (sz != 256)
  40246. return WC_TEST_RET_ENC_NC;
  40247. ret = wc_DecodeSakkeRsk(priv, data, sz, rsk);
  40248. if (ret != 0)
  40249. return WC_TEST_RET_ENC_EC(ret);
  40250. sz = sizeof(pubData);
  40251. ret = wc_EncodeSakkeRsk(priv, rsk, pubData, &sz, 0);
  40252. if (ret != 0)
  40253. return WC_TEST_RET_ENC_EC(ret);
  40254. if (sz != sizeof(pubData))
  40255. return WC_TEST_RET_ENC_NC;
  40256. ret = wc_DecodeSakkeRsk(priv, pubData, sz, rsk);
  40257. if (ret != 0)
  40258. return WC_TEST_RET_ENC_EC(ret);
  40259. wc_FreeSakkeKey(key);
  40260. return 0;
  40261. }
  40262. static wc_test_ret_t sakke_op_test(SakkeKey* priv, SakkeKey* pub, WC_RNG* rng,
  40263. ecc_point* rsk)
  40264. {
  40265. wc_test_ret_t ret;
  40266. byte ssv[16];
  40267. word16 ssvSz;
  40268. byte auth[257];
  40269. word16 authSz;
  40270. char mail[] = "test@wolfssl.com";
  40271. byte* id = (byte*)mail;
  40272. word32 idSz = (word32)XSTRLEN(mail);
  40273. byte pointI[256];
  40274. word32 sz;
  40275. ret = wc_GenerateSakkeSSV(pub, rng, NULL, &ssvSz);
  40276. if (ret != LENGTH_ONLY_E)
  40277. return WC_TEST_RET_ENC_EC(ret);
  40278. if (ssvSz != 16)
  40279. return WC_TEST_RET_ENC_NC;
  40280. ssvSz += 128;
  40281. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  40282. if (ret == 0)
  40283. return WC_TEST_RET_ENC_NC;
  40284. ssvSz -= 128;
  40285. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  40286. if (ret != 0)
  40287. return WC_TEST_RET_ENC_EC(ret);
  40288. if (ssvSz != 16)
  40289. return WC_TEST_RET_ENC_NC;
  40290. ret = wc_GetSakkeAuthSize(pub, &authSz);
  40291. if (ret != 0)
  40292. return WC_TEST_RET_ENC_EC(ret);
  40293. ret = wc_SetSakkeIdentity(pub, id, idSz);
  40294. if (ret != 0)
  40295. return WC_TEST_RET_ENC_EC(ret);
  40296. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  40297. NULL, &authSz);
  40298. if (ret != LENGTH_ONLY_E)
  40299. return WC_TEST_RET_ENC_EC(ret);
  40300. if (authSz != 257)
  40301. return WC_TEST_RET_ENC_NC;
  40302. authSz--;
  40303. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  40304. auth, &authSz);
  40305. if (ret == 0)
  40306. return WC_TEST_RET_ENC_NC;
  40307. authSz++;
  40308. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  40309. auth, &authSz);
  40310. if (ret != 0)
  40311. return WC_TEST_RET_ENC_EC(ret);
  40312. if (authSz != 257)
  40313. return WC_TEST_RET_ENC_NC;
  40314. ret = wc_GetSakkePointI(pub, NULL, &sz);
  40315. if (ret != LENGTH_ONLY_E)
  40316. return WC_TEST_RET_ENC_EC(ret);
  40317. if (sz != 256)
  40318. return WC_TEST_RET_ENC_NC;
  40319. ret = wc_GetSakkePointI(pub, pointI, &sz);
  40320. if (ret != 0)
  40321. return WC_TEST_RET_ENC_EC(ret);
  40322. if (sz != 256)
  40323. return WC_TEST_RET_ENC_NC;
  40324. /* Bogus identity - make it check and regenerate I. */
  40325. ret = wc_MakeSakkePointI(pub, ssv, ssvSz);
  40326. if (ret != 0)
  40327. return WC_TEST_RET_ENC_EC(ret);
  40328. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  40329. auth, &authSz);
  40330. if (ret != 0)
  40331. return WC_TEST_RET_ENC_EC(ret);
  40332. if (authSz != 257)
  40333. return WC_TEST_RET_ENC_NC;
  40334. ret = wc_SetSakkeRsk(priv, rsk, NULL, 0);
  40335. if (ret != 0)
  40336. return WC_TEST_RET_ENC_EC(ret);
  40337. ret = wc_SetSakkeIdentity(priv, id, idSz);
  40338. if (ret != 0)
  40339. return WC_TEST_RET_ENC_EC(ret);
  40340. authSz--;
  40341. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  40342. authSz);
  40343. if (ret == 0)
  40344. return WC_TEST_RET_ENC_NC;
  40345. authSz++;
  40346. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  40347. authSz);
  40348. if (ret != 0)
  40349. return WC_TEST_RET_ENC_EC(ret);
  40350. ssv[0] ^= 0x80;
  40351. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  40352. authSz);
  40353. if (ret != SAKKE_VERIFY_FAIL_E)
  40354. return WC_TEST_RET_ENC_EC(ret);
  40355. ssv[0] ^= 0x80;
  40356. /* Bogus identity - make it check and regenerate I. */
  40357. ret = wc_MakeSakkePointI(pub, ssv, idSz);
  40358. if (ret != 0)
  40359. return WC_TEST_RET_ENC_EC(ret);
  40360. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  40361. authSz);
  40362. if (ret != 0)
  40363. return WC_TEST_RET_ENC_EC(ret);
  40364. return 0;
  40365. }
  40366. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sakke_test(void)
  40367. {
  40368. wc_test_ret_t ret = 0;
  40369. WC_RNG rng;
  40370. int rng_inited = 0;
  40371. SakkeKey* priv = NULL;
  40372. SakkeKey* pub = NULL;
  40373. SakkeKey* key = NULL;
  40374. ecc_point* rsk = NULL;
  40375. WOLFSSL_ENTER("sakke_test");
  40376. priv = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  40377. DYNAMIC_TYPE_TMP_BUFFER);
  40378. if (priv == NULL)
  40379. ret = WC_TEST_RET_ENC_NC;
  40380. else
  40381. XMEMSET(priv, 0, sizeof(*priv));
  40382. if (ret == 0) {
  40383. pub = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  40384. DYNAMIC_TYPE_TMP_BUFFER);
  40385. if (pub == NULL)
  40386. ret = WC_TEST_RET_ENC_NC;
  40387. else
  40388. XMEMSET(pub, 0, sizeof(*pub));
  40389. }
  40390. if (ret == 0) {
  40391. key = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  40392. DYNAMIC_TYPE_TMP_BUFFER);
  40393. if (key == NULL)
  40394. ret = WC_TEST_RET_ENC_NC;
  40395. else
  40396. XMEMSET(key, 0, sizeof(*key));
  40397. }
  40398. if (ret == 0) {
  40399. #ifndef HAVE_FIPS
  40400. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  40401. #else
  40402. ret = wc_InitRng(&rng);
  40403. #endif
  40404. if (ret == 0)
  40405. rng_inited = 1;
  40406. else
  40407. ret = WC_TEST_RET_ENC_EC(ret);
  40408. }
  40409. if (ret == 0) {
  40410. rsk = wc_ecc_new_point();
  40411. if (rsk == NULL)
  40412. ret = WC_TEST_RET_ENC_NC;
  40413. }
  40414. if (ret == 0) {
  40415. ret = wc_InitSakkeKey(pub, HEAP_HINT, INVALID_DEVID);
  40416. if (ret != 0)
  40417. ret = WC_TEST_RET_ENC_EC(ret);
  40418. }
  40419. if (ret == 0) {
  40420. ret = wc_InitSakkeKey(priv, HEAP_HINT, INVALID_DEVID);
  40421. if (ret != 0)
  40422. ret = WC_TEST_RET_ENC_EC(ret);
  40423. }
  40424. if (ret == 0) {
  40425. ret = sakke_api_test(&rng, key, rsk);
  40426. }
  40427. if (ret == 0) {
  40428. ret = sakke_kat_derive_test(pub, rsk);
  40429. }
  40430. if (ret == 0) {
  40431. ret = sakke_kat_encapsulate_test(pub);
  40432. }
  40433. if (ret == 0) {
  40434. ret = sakke_make_key_test(priv, pub, key, &rng, rsk);
  40435. }
  40436. if (ret == 0) {
  40437. ret = sakke_op_test(priv, pub, &rng, rsk);
  40438. }
  40439. if (rsk != NULL) {
  40440. wc_ecc_forcezero_point(rsk);
  40441. wc_ecc_del_point(rsk);
  40442. }
  40443. if (rng_inited)
  40444. wc_FreeRng(&rng);
  40445. if (key != NULL)
  40446. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40447. if (pub != NULL) {
  40448. wc_FreeSakkeKey(pub);
  40449. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40450. }
  40451. if (priv != NULL) {
  40452. wc_FreeSakkeKey(priv);
  40453. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40454. }
  40455. return ret;
  40456. }
  40457. #endif /* WOLFCRYPT_HAVE_SAKKE */
  40458. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  40459. typedef struct CMAC_Test_Case {
  40460. int type;
  40461. int partial;
  40462. const byte* m;
  40463. word32 mSz;
  40464. const byte* k;
  40465. word32 kSz;
  40466. const byte* t;
  40467. word32 tSz;
  40468. } CMAC_Test_Case;
  40469. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cmac_test(void)
  40470. {
  40471. #ifdef WOLFSSL_AES_128
  40472. WOLFSSL_SMALL_STACK_STATIC const byte k128[] =
  40473. {
  40474. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  40475. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  40476. };
  40477. #define KLEN_128 (sizeof(k128))
  40478. #endif
  40479. #ifdef WOLFSSL_AES_192
  40480. WOLFSSL_SMALL_STACK_STATIC const byte k192[] =
  40481. {
  40482. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  40483. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  40484. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  40485. };
  40486. #define KLEN_192 (sizeof(k192))
  40487. #endif
  40488. #ifdef WOLFSSL_AES_256
  40489. WOLFSSL_SMALL_STACK_STATIC const byte k256[] =
  40490. {
  40491. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  40492. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  40493. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  40494. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  40495. };
  40496. #define KLEN_256 (sizeof(k256))
  40497. #endif
  40498. WOLFSSL_SMALL_STACK_STATIC const byte m[] =
  40499. {
  40500. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  40501. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  40502. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  40503. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  40504. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  40505. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  40506. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  40507. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  40508. };
  40509. #define MLEN_0 (0)
  40510. #define MLEN_128 (128/8)
  40511. #define MLEN_320 (320/8)
  40512. #define MLEN_319 (MLEN_320 - 1)
  40513. #define MLEN_512 (512/8)
  40514. #ifdef WOLFSSL_AES_128
  40515. WOLFSSL_SMALL_STACK_STATIC const byte t128_0[] =
  40516. {
  40517. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  40518. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  40519. };
  40520. WOLFSSL_SMALL_STACK_STATIC const byte t128_128[] =
  40521. {
  40522. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  40523. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  40524. };
  40525. WOLFSSL_SMALL_STACK_STATIC const byte t128_319[] =
  40526. {
  40527. 0x2c, 0x17, 0x84, 0x4c, 0x93, 0x1c, 0x07, 0x95,
  40528. 0x15, 0x92, 0x73, 0x0a, 0x34, 0xd0, 0xd9, 0xd2
  40529. };
  40530. WOLFSSL_SMALL_STACK_STATIC const byte t128_320[] =
  40531. {
  40532. 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  40533. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27
  40534. };
  40535. WOLFSSL_SMALL_STACK_STATIC const byte t128_512[] =
  40536. {
  40537. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  40538. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  40539. };
  40540. #endif
  40541. #ifdef WOLFSSL_AES_192
  40542. WOLFSSL_SMALL_STACK_STATIC const byte t192_0[] =
  40543. {
  40544. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  40545. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  40546. };
  40547. WOLFSSL_SMALL_STACK_STATIC const byte t192_128[] =
  40548. {
  40549. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  40550. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  40551. };
  40552. WOLFSSL_SMALL_STACK_STATIC const byte t192_320[] =
  40553. {
  40554. 0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad,
  40555. 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e
  40556. };
  40557. WOLFSSL_SMALL_STACK_STATIC const byte t192_512[] =
  40558. {
  40559. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  40560. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  40561. };
  40562. #endif
  40563. #ifdef WOLFSSL_AES_256
  40564. WOLFSSL_SMALL_STACK_STATIC const byte t256_0[] =
  40565. {
  40566. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  40567. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  40568. };
  40569. WOLFSSL_SMALL_STACK_STATIC const byte t256_128[] =
  40570. {
  40571. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  40572. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  40573. };
  40574. WOLFSSL_SMALL_STACK_STATIC const byte t256_320[] =
  40575. {
  40576. 0xaa, 0xf3, 0xd8, 0xf1, 0xde, 0x56, 0x40, 0xc2,
  40577. 0x32, 0xf5, 0xb1, 0x69, 0xb9, 0xc9, 0x11, 0xe6
  40578. };
  40579. WOLFSSL_SMALL_STACK_STATIC const byte t256_512[] =
  40580. {
  40581. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  40582. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  40583. };
  40584. #endif
  40585. const CMAC_Test_Case testCases[] =
  40586. {
  40587. #ifdef WOLFSSL_AES_128
  40588. {WC_CMAC_AES, 0, m, MLEN_0, k128, KLEN_128, t128_0, AES_BLOCK_SIZE},
  40589. {WC_CMAC_AES, 0, m, MLEN_128, k128, KLEN_128, t128_128, AES_BLOCK_SIZE},
  40590. {WC_CMAC_AES, 0, m, MLEN_320, k128, KLEN_128, t128_320, AES_BLOCK_SIZE},
  40591. {WC_CMAC_AES, 0, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  40592. {WC_CMAC_AES, 5, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  40593. #endif
  40594. #ifdef WOLFSSL_AES_192
  40595. {WC_CMAC_AES, 0, m, MLEN_0, k192, KLEN_192, t192_0, AES_BLOCK_SIZE},
  40596. {WC_CMAC_AES, 0, m, MLEN_128, k192, KLEN_192, t192_128, AES_BLOCK_SIZE},
  40597. {WC_CMAC_AES, 0, m, MLEN_320, k192, KLEN_192, t192_320, AES_BLOCK_SIZE},
  40598. {WC_CMAC_AES, 0, m, MLEN_512, k192, KLEN_192, t192_512, AES_BLOCK_SIZE},
  40599. #endif
  40600. #ifdef WOLFSSL_AES_256
  40601. {WC_CMAC_AES, 0, m, MLEN_0, k256, KLEN_256, t256_0, AES_BLOCK_SIZE},
  40602. {WC_CMAC_AES, 0, m, MLEN_128, k256, KLEN_256, t256_128, AES_BLOCK_SIZE},
  40603. {WC_CMAC_AES, 0, m, MLEN_320, k256, KLEN_256, t256_320, AES_BLOCK_SIZE},
  40604. {WC_CMAC_AES, 0, m, MLEN_512, k256, KLEN_256, t256_512, AES_BLOCK_SIZE},
  40605. #endif
  40606. #ifdef WOLFSSL_AES_128
  40607. {WC_CMAC_AES, 0, m, MLEN_319, k128, KLEN_128, t128_319, AES_BLOCK_SIZE}
  40608. #endif
  40609. };
  40610. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40611. Cmac *cmac;
  40612. #else
  40613. Cmac cmac[1];
  40614. #endif
  40615. byte tag[AES_BLOCK_SIZE];
  40616. const CMAC_Test_Case* tc;
  40617. word32 i, tagSz;
  40618. wc_test_ret_t ret;
  40619. WOLFSSL_ENTER("cmac_test");
  40620. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40621. if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC)) == NULL)
  40622. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  40623. #endif
  40624. for (i = 0, tc = testCases;
  40625. i < sizeof(testCases)/sizeof(CMAC_Test_Case);
  40626. i++, tc++) {
  40627. XMEMSET(tag, 0, sizeof(tag));
  40628. tagSz = AES_BLOCK_SIZE;
  40629. #if !defined(HAVE_FIPS) || \
  40630. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)
  40631. ret = wc_InitCmac_ex(cmac, tc->k, tc->kSz, tc->type, NULL, HEAP_HINT, devId);
  40632. #else
  40633. ret = wc_InitCmac(cmac, tc->k, tc->kSz, tc->type, NULL);
  40634. #endif
  40635. if (ret != 0)
  40636. {
  40637. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40638. }
  40639. if (tc->partial) {
  40640. ret = wc_CmacUpdate(cmac, tc->m,
  40641. tc->mSz/2 - tc->partial);
  40642. if (ret != 0)
  40643. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40644. ret = wc_CmacUpdate(cmac, tc->m + tc->mSz/2 - tc->partial,
  40645. tc->mSz/2 + tc->partial);
  40646. if (ret != 0)
  40647. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40648. }
  40649. else {
  40650. ret = wc_CmacUpdate(cmac, tc->m, tc->mSz);
  40651. if (ret != 0)
  40652. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40653. }
  40654. ret = wc_CmacFinal(cmac, tag, &tagSz);
  40655. if (ret != 0)
  40656. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40657. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  40658. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40659. XMEMSET(tag, 0, sizeof(tag));
  40660. tagSz = sizeof(tag);
  40661. #if !defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)
  40662. ret = wc_AesCmacGenerate_ex(cmac, tag, &tagSz, tc->m, tc->mSz,
  40663. tc->k, tc->kSz, NULL, devId);
  40664. #else
  40665. ret = wc_AesCmacGenerate(tag, &tagSz, tc->m, tc->mSz,
  40666. tc->k, tc->kSz);
  40667. #endif
  40668. if (ret != 0)
  40669. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40670. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  40671. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40672. #if !defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)
  40673. ret = wc_AesCmacVerify_ex(cmac, tc->t, tc->tSz, tc->m, tc->mSz,
  40674. tc->k, tc->kSz, HEAP_HINT, devId);
  40675. #else
  40676. ret = wc_AesCmacVerify(tc->t, tc->tSz, tc->m, tc->mSz,
  40677. tc->k, tc->kSz);
  40678. #endif
  40679. if (ret != 0)
  40680. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40681. #if !defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)
  40682. /* Test that keyless generate with init is the same */
  40683. XMEMSET(tag, 0, sizeof(tag));
  40684. tagSz = sizeof(tag);
  40685. ret = wc_InitCmac_ex(cmac, tc->k, tc->kSz, tc->type, NULL, HEAP_HINT, devId);
  40686. if (ret != 0) {
  40687. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40688. }
  40689. ret = wc_AesCmacGenerate_ex(cmac, tag, &tagSz, tc->m, tc->mSz,
  40690. NULL, 0, HEAP_HINT, devId);
  40691. if (ret != 0) {
  40692. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40693. }
  40694. #endif
  40695. }
  40696. ret = 0;
  40697. out:
  40698. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40699. if (cmac)
  40700. XFREE(cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC);
  40701. #endif
  40702. return ret;
  40703. }
  40704. #endif /* NO_AES && WOLFSSL_CMAC */
  40705. #if defined(WOLFSSL_SIPHASH)
  40706. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  40707. /* Test vectors from:
  40708. * https://github.com/veorq/SipHash/blob/master/vectors.h
  40709. */
  40710. static const unsigned char siphash_key[SIPHASH_KEY_SIZE] = {
  40711. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  40712. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  40713. };
  40714. static const unsigned char siphash_msg[64] = {
  40715. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  40716. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  40717. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  40718. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  40719. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  40720. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  40721. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  40722. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
  40723. };
  40724. static const unsigned char siphash_r8[64][SIPHASH_MAC_SIZE_8] = {
  40725. { 0x31, 0x0e, 0x0e, 0xdd, 0x47, 0xdb, 0x6f, 0x72, },
  40726. { 0xfd, 0x67, 0xdc, 0x93, 0xc5, 0x39, 0xf8, 0x74, },
  40727. { 0x5a, 0x4f, 0xa9, 0xd9, 0x09, 0x80, 0x6c, 0x0d, },
  40728. { 0x2d, 0x7e, 0xfb, 0xd7, 0x96, 0x66, 0x67, 0x85, },
  40729. { 0xb7, 0x87, 0x71, 0x27, 0xe0, 0x94, 0x27, 0xcf, },
  40730. { 0x8d, 0xa6, 0x99, 0xcd, 0x64, 0x55, 0x76, 0x18, },
  40731. { 0xce, 0xe3, 0xfe, 0x58, 0x6e, 0x46, 0xc9, 0xcb, },
  40732. { 0x37, 0xd1, 0x01, 0x8b, 0xf5, 0x00, 0x02, 0xab, },
  40733. { 0x62, 0x24, 0x93, 0x9a, 0x79, 0xf5, 0xf5, 0x93, },
  40734. { 0xb0, 0xe4, 0xa9, 0x0b, 0xdf, 0x82, 0x00, 0x9e, },
  40735. { 0xf3, 0xb9, 0xdd, 0x94, 0xc5, 0xbb, 0x5d, 0x7a, },
  40736. { 0xa7, 0xad, 0x6b, 0x22, 0x46, 0x2f, 0xb3, 0xf4, },
  40737. { 0xfb, 0xe5, 0x0e, 0x86, 0xbc, 0x8f, 0x1e, 0x75, },
  40738. { 0x90, 0x3d, 0x84, 0xc0, 0x27, 0x56, 0xea, 0x14, },
  40739. { 0xee, 0xf2, 0x7a, 0x8e, 0x90, 0xca, 0x23, 0xf7, },
  40740. { 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1, },
  40741. { 0xdb, 0x9b, 0xc2, 0x57, 0x7f, 0xcc, 0x2a, 0x3f, },
  40742. { 0x94, 0x47, 0xbe, 0x2c, 0xf5, 0xe9, 0x9a, 0x69, },
  40743. { 0x9c, 0xd3, 0x8d, 0x96, 0xf0, 0xb3, 0xc1, 0x4b, },
  40744. { 0xbd, 0x61, 0x79, 0xa7, 0x1d, 0xc9, 0x6d, 0xbb, },
  40745. { 0x98, 0xee, 0xa2, 0x1a, 0xf2, 0x5c, 0xd6, 0xbe, },
  40746. { 0xc7, 0x67, 0x3b, 0x2e, 0xb0, 0xcb, 0xf2, 0xd0, },
  40747. { 0x88, 0x3e, 0xa3, 0xe3, 0x95, 0x67, 0x53, 0x93, },
  40748. { 0xc8, 0xce, 0x5c, 0xcd, 0x8c, 0x03, 0x0c, 0xa8, },
  40749. { 0x94, 0xaf, 0x49, 0xf6, 0xc6, 0x50, 0xad, 0xb8, },
  40750. { 0xea, 0xb8, 0x85, 0x8a, 0xde, 0x92, 0xe1, 0xbc, },
  40751. { 0xf3, 0x15, 0xbb, 0x5b, 0xb8, 0x35, 0xd8, 0x17, },
  40752. { 0xad, 0xcf, 0x6b, 0x07, 0x63, 0x61, 0x2e, 0x2f, },
  40753. { 0xa5, 0xc9, 0x1d, 0xa7, 0xac, 0xaa, 0x4d, 0xde, },
  40754. { 0x71, 0x65, 0x95, 0x87, 0x66, 0x50, 0xa2, 0xa6, },
  40755. { 0x28, 0xef, 0x49, 0x5c, 0x53, 0xa3, 0x87, 0xad, },
  40756. { 0x42, 0xc3, 0x41, 0xd8, 0xfa, 0x92, 0xd8, 0x32, },
  40757. { 0xce, 0x7c, 0xf2, 0x72, 0x2f, 0x51, 0x27, 0x71, },
  40758. { 0xe3, 0x78, 0x59, 0xf9, 0x46, 0x23, 0xf3, 0xa7, },
  40759. { 0x38, 0x12, 0x05, 0xbb, 0x1a, 0xb0, 0xe0, 0x12, },
  40760. { 0xae, 0x97, 0xa1, 0x0f, 0xd4, 0x34, 0xe0, 0x15, },
  40761. { 0xb4, 0xa3, 0x15, 0x08, 0xbe, 0xff, 0x4d, 0x31, },
  40762. { 0x81, 0x39, 0x62, 0x29, 0xf0, 0x90, 0x79, 0x02, },
  40763. { 0x4d, 0x0c, 0xf4, 0x9e, 0xe5, 0xd4, 0xdc, 0xca, },
  40764. { 0x5c, 0x73, 0x33, 0x6a, 0x76, 0xd8, 0xbf, 0x9a, },
  40765. { 0xd0, 0xa7, 0x04, 0x53, 0x6b, 0xa9, 0x3e, 0x0e, },
  40766. { 0x92, 0x59, 0x58, 0xfc, 0xd6, 0x42, 0x0c, 0xad, },
  40767. { 0xa9, 0x15, 0xc2, 0x9b, 0xc8, 0x06, 0x73, 0x18, },
  40768. { 0x95, 0x2b, 0x79, 0xf3, 0xbc, 0x0a, 0xa6, 0xd4, },
  40769. { 0xf2, 0x1d, 0xf2, 0xe4, 0x1d, 0x45, 0x35, 0xf9, },
  40770. { 0x87, 0x57, 0x75, 0x19, 0x04, 0x8f, 0x53, 0xa9, },
  40771. { 0x10, 0xa5, 0x6c, 0xf5, 0xdf, 0xcd, 0x9a, 0xdb, },
  40772. { 0xeb, 0x75, 0x09, 0x5c, 0xcd, 0x98, 0x6c, 0xd0, },
  40773. { 0x51, 0xa9, 0xcb, 0x9e, 0xcb, 0xa3, 0x12, 0xe6, },
  40774. { 0x96, 0xaf, 0xad, 0xfc, 0x2c, 0xe6, 0x66, 0xc7, },
  40775. { 0x72, 0xfe, 0x52, 0x97, 0x5a, 0x43, 0x64, 0xee, },
  40776. { 0x5a, 0x16, 0x45, 0xb2, 0x76, 0xd5, 0x92, 0xa1, },
  40777. { 0xb2, 0x74, 0xcb, 0x8e, 0xbf, 0x87, 0x87, 0x0a, },
  40778. { 0x6f, 0x9b, 0xb4, 0x20, 0x3d, 0xe7, 0xb3, 0x81, },
  40779. { 0xea, 0xec, 0xb2, 0xa3, 0x0b, 0x22, 0xa8, 0x7f, },
  40780. { 0x99, 0x24, 0xa4, 0x3c, 0xc1, 0x31, 0x57, 0x24, },
  40781. { 0xbd, 0x83, 0x8d, 0x3a, 0xaf, 0xbf, 0x8d, 0xb7, },
  40782. { 0x0b, 0x1a, 0x2a, 0x32, 0x65, 0xd5, 0x1a, 0xea, },
  40783. { 0x13, 0x50, 0x79, 0xa3, 0x23, 0x1c, 0xe6, 0x60, },
  40784. { 0x93, 0x2b, 0x28, 0x46, 0xe4, 0xd7, 0x06, 0x66, },
  40785. { 0xe1, 0x91, 0x5f, 0x5c, 0xb1, 0xec, 0xa4, 0x6c, },
  40786. { 0xf3, 0x25, 0x96, 0x5c, 0xa1, 0x6d, 0x62, 0x9f, },
  40787. { 0x57, 0x5f, 0xf2, 0x8e, 0x60, 0x38, 0x1b, 0xe5, },
  40788. { 0x72, 0x45, 0x06, 0xeb, 0x4c, 0x32, 0x8a, 0x95, },
  40789. };
  40790. static const unsigned char siphash_r16[64][SIPHASH_MAC_SIZE_16] = {
  40791. { 0xa3, 0x81, 0x7f, 0x04, 0xba, 0x25, 0xa8, 0xe6,
  40792. 0x6d, 0xf6, 0x72, 0x14, 0xc7, 0x55, 0x02, 0x93, },
  40793. { 0xda, 0x87, 0xc1, 0xd8, 0x6b, 0x99, 0xaf, 0x44,
  40794. 0x34, 0x76, 0x59, 0x11, 0x9b, 0x22, 0xfc, 0x45, },
  40795. { 0x81, 0x77, 0x22, 0x8d, 0xa4, 0xa4, 0x5d, 0xc7,
  40796. 0xfc, 0xa3, 0x8b, 0xde, 0xf6, 0x0a, 0xff, 0xe4, },
  40797. { 0x9c, 0x70, 0xb6, 0x0c, 0x52, 0x67, 0xa9, 0x4e,
  40798. 0x5f, 0x33, 0xb6, 0xb0, 0x29, 0x85, 0xed, 0x51, },
  40799. { 0xf8, 0x81, 0x64, 0xc1, 0x2d, 0x9c, 0x8f, 0xaf,
  40800. 0x7d, 0x0f, 0x6e, 0x7c, 0x7b, 0xcd, 0x55, 0x79, },
  40801. { 0x13, 0x68, 0x87, 0x59, 0x80, 0x77, 0x6f, 0x88,
  40802. 0x54, 0x52, 0x7a, 0x07, 0x69, 0x0e, 0x96, 0x27, },
  40803. { 0x14, 0xee, 0xca, 0x33, 0x8b, 0x20, 0x86, 0x13,
  40804. 0x48, 0x5e, 0xa0, 0x30, 0x8f, 0xd7, 0xa1, 0x5e, },
  40805. { 0xa1, 0xf1, 0xeb, 0xbe, 0xd8, 0xdb, 0xc1, 0x53,
  40806. 0xc0, 0xb8, 0x4a, 0xa6, 0x1f, 0xf0, 0x82, 0x39, },
  40807. { 0x3b, 0x62, 0xa9, 0xba, 0x62, 0x58, 0xf5, 0x61,
  40808. 0x0f, 0x83, 0xe2, 0x64, 0xf3, 0x14, 0x97, 0xb4, },
  40809. { 0x26, 0x44, 0x99, 0x06, 0x0a, 0xd9, 0xba, 0xab,
  40810. 0xc4, 0x7f, 0x8b, 0x02, 0xbb, 0x6d, 0x71, 0xed, },
  40811. { 0x00, 0x11, 0x0d, 0xc3, 0x78, 0x14, 0x69, 0x56,
  40812. 0xc9, 0x54, 0x47, 0xd3, 0xf3, 0xd0, 0xfb, 0xba, },
  40813. { 0x01, 0x51, 0xc5, 0x68, 0x38, 0x6b, 0x66, 0x77,
  40814. 0xa2, 0xb4, 0xdc, 0x6f, 0x81, 0xe5, 0xdc, 0x18, },
  40815. { 0xd6, 0x26, 0xb2, 0x66, 0x90, 0x5e, 0xf3, 0x58,
  40816. 0x82, 0x63, 0x4d, 0xf6, 0x85, 0x32, 0xc1, 0x25, },
  40817. { 0x98, 0x69, 0xe2, 0x47, 0xe9, 0xc0, 0x8b, 0x10,
  40818. 0xd0, 0x29, 0x93, 0x4f, 0xc4, 0xb9, 0x52, 0xf7, },
  40819. { 0x31, 0xfc, 0xef, 0xac, 0x66, 0xd7, 0xde, 0x9c,
  40820. 0x7e, 0xc7, 0x48, 0x5f, 0xe4, 0x49, 0x49, 0x02, },
  40821. { 0x54, 0x93, 0xe9, 0x99, 0x33, 0xb0, 0xa8, 0x11,
  40822. 0x7e, 0x08, 0xec, 0x0f, 0x97, 0xcf, 0xc3, 0xd9, },
  40823. { 0x6e, 0xe2, 0xa4, 0xca, 0x67, 0xb0, 0x54, 0xbb,
  40824. 0xfd, 0x33, 0x15, 0xbf, 0x85, 0x23, 0x05, 0x77, },
  40825. { 0x47, 0x3d, 0x06, 0xe8, 0x73, 0x8d, 0xb8, 0x98,
  40826. 0x54, 0xc0, 0x66, 0xc4, 0x7a, 0xe4, 0x77, 0x40, },
  40827. { 0xa4, 0x26, 0xe5, 0xe4, 0x23, 0xbf, 0x48, 0x85,
  40828. 0x29, 0x4d, 0xa4, 0x81, 0xfe, 0xae, 0xf7, 0x23, },
  40829. { 0x78, 0x01, 0x77, 0x31, 0xcf, 0x65, 0xfa, 0xb0,
  40830. 0x74, 0xd5, 0x20, 0x89, 0x52, 0x51, 0x2e, 0xb1, },
  40831. { 0x9e, 0x25, 0xfc, 0x83, 0x3f, 0x22, 0x90, 0x73,
  40832. 0x3e, 0x93, 0x44, 0xa5, 0xe8, 0x38, 0x39, 0xeb, },
  40833. { 0x56, 0x8e, 0x49, 0x5a, 0xbe, 0x52, 0x5a, 0x21,
  40834. 0x8a, 0x22, 0x14, 0xcd, 0x3e, 0x07, 0x1d, 0x12, },
  40835. { 0x4a, 0x29, 0xb5, 0x45, 0x52, 0xd1, 0x6b, 0x9a,
  40836. 0x46, 0x9c, 0x10, 0x52, 0x8e, 0xff, 0x0a, 0xae, },
  40837. { 0xc9, 0xd1, 0x84, 0xdd, 0xd5, 0xa9, 0xf5, 0xe0,
  40838. 0xcf, 0x8c, 0xe2, 0x9a, 0x9a, 0xbf, 0x69, 0x1c, },
  40839. { 0x2d, 0xb4, 0x79, 0xae, 0x78, 0xbd, 0x50, 0xd8,
  40840. 0x88, 0x2a, 0x8a, 0x17, 0x8a, 0x61, 0x32, 0xad, },
  40841. { 0x8e, 0xce, 0x5f, 0x04, 0x2d, 0x5e, 0x44, 0x7b,
  40842. 0x50, 0x51, 0xb9, 0xea, 0xcb, 0x8d, 0x8f, 0x6f, },
  40843. { 0x9c, 0x0b, 0x53, 0xb4, 0xb3, 0xc3, 0x07, 0xe8,
  40844. 0x7e, 0xae, 0xe0, 0x86, 0x78, 0x14, 0x1f, 0x66, },
  40845. { 0xab, 0xf2, 0x48, 0xaf, 0x69, 0xa6, 0xea, 0xe4,
  40846. 0xbf, 0xd3, 0xeb, 0x2f, 0x12, 0x9e, 0xeb, 0x94, },
  40847. { 0x06, 0x64, 0xda, 0x16, 0x68, 0x57, 0x4b, 0x88,
  40848. 0xb9, 0x35, 0xf3, 0x02, 0x73, 0x58, 0xae, 0xf4, },
  40849. { 0xaa, 0x4b, 0x9d, 0xc4, 0xbf, 0x33, 0x7d, 0xe9,
  40850. 0x0c, 0xd4, 0xfd, 0x3c, 0x46, 0x7c, 0x6a, 0xb7, },
  40851. { 0xea, 0x5c, 0x7f, 0x47, 0x1f, 0xaf, 0x6b, 0xde,
  40852. 0x2b, 0x1a, 0xd7, 0xd4, 0x68, 0x6d, 0x22, 0x87, },
  40853. { 0x29, 0x39, 0xb0, 0x18, 0x32, 0x23, 0xfa, 0xfc,
  40854. 0x17, 0x23, 0xde, 0x4f, 0x52, 0xc4, 0x3d, 0x35, },
  40855. { 0x7c, 0x39, 0x56, 0xca, 0x5e, 0xea, 0xfc, 0x3e,
  40856. 0x36, 0x3e, 0x9d, 0x55, 0x65, 0x46, 0xeb, 0x68, },
  40857. { 0x77, 0xc6, 0x07, 0x71, 0x46, 0xf0, 0x1c, 0x32,
  40858. 0xb6, 0xb6, 0x9d, 0x5f, 0x4e, 0xa9, 0xff, 0xcf, },
  40859. { 0x37, 0xa6, 0x98, 0x6c, 0xb8, 0x84, 0x7e, 0xdf,
  40860. 0x09, 0x25, 0xf0, 0xf1, 0x30, 0x9b, 0x54, 0xde, },
  40861. { 0xa7, 0x05, 0xf0, 0xe6, 0x9d, 0xa9, 0xa8, 0xf9,
  40862. 0x07, 0x24, 0x1a, 0x2e, 0x92, 0x3c, 0x8c, 0xc8, },
  40863. { 0x3d, 0xc4, 0x7d, 0x1f, 0x29, 0xc4, 0x48, 0x46,
  40864. 0x1e, 0x9e, 0x76, 0xed, 0x90, 0x4f, 0x67, 0x11, },
  40865. { 0x0d, 0x62, 0xbf, 0x01, 0xe6, 0xfc, 0x0e, 0x1a,
  40866. 0x0d, 0x3c, 0x47, 0x51, 0xc5, 0xd3, 0x69, 0x2b, },
  40867. { 0x8c, 0x03, 0x46, 0x8b, 0xca, 0x7c, 0x66, 0x9e,
  40868. 0xe4, 0xfd, 0x5e, 0x08, 0x4b, 0xbe, 0xe7, 0xb5, },
  40869. { 0x52, 0x8a, 0x5b, 0xb9, 0x3b, 0xaf, 0x2c, 0x9c,
  40870. 0x44, 0x73, 0xcc, 0xe5, 0xd0, 0xd2, 0x2b, 0xd9, },
  40871. { 0xdf, 0x6a, 0x30, 0x1e, 0x95, 0xc9, 0x5d, 0xad,
  40872. 0x97, 0xae, 0x0c, 0xc8, 0xc6, 0x91, 0x3b, 0xd8, },
  40873. { 0x80, 0x11, 0x89, 0x90, 0x2c, 0x85, 0x7f, 0x39,
  40874. 0xe7, 0x35, 0x91, 0x28, 0x5e, 0x70, 0xb6, 0xdb, },
  40875. { 0xe6, 0x17, 0x34, 0x6a, 0xc9, 0xc2, 0x31, 0xbb,
  40876. 0x36, 0x50, 0xae, 0x34, 0xcc, 0xca, 0x0c, 0x5b, },
  40877. { 0x27, 0xd9, 0x34, 0x37, 0xef, 0xb7, 0x21, 0xaa,
  40878. 0x40, 0x18, 0x21, 0xdc, 0xec, 0x5a, 0xdf, 0x89, },
  40879. { 0x89, 0x23, 0x7d, 0x9d, 0xed, 0x9c, 0x5e, 0x78,
  40880. 0xd8, 0xb1, 0xc9, 0xb1, 0x66, 0xcc, 0x73, 0x42, },
  40881. { 0x4a, 0x6d, 0x80, 0x91, 0xbf, 0x5e, 0x7d, 0x65,
  40882. 0x11, 0x89, 0xfa, 0x94, 0xa2, 0x50, 0xb1, 0x4c, },
  40883. { 0x0e, 0x33, 0xf9, 0x60, 0x55, 0xe7, 0xae, 0x89,
  40884. 0x3f, 0xfc, 0x0e, 0x3d, 0xcf, 0x49, 0x29, 0x02, },
  40885. { 0xe6, 0x1c, 0x43, 0x2b, 0x72, 0x0b, 0x19, 0xd1,
  40886. 0x8e, 0xc8, 0xd8, 0x4b, 0xdc, 0x63, 0x15, 0x1b, },
  40887. { 0xf7, 0xe5, 0xae, 0xf5, 0x49, 0xf7, 0x82, 0xcf,
  40888. 0x37, 0x90, 0x55, 0xa6, 0x08, 0x26, 0x9b, 0x16, },
  40889. { 0x43, 0x8d, 0x03, 0x0f, 0xd0, 0xb7, 0xa5, 0x4f,
  40890. 0xa8, 0x37, 0xf2, 0xad, 0x20, 0x1a, 0x64, 0x03, },
  40891. { 0xa5, 0x90, 0xd3, 0xee, 0x4f, 0xbf, 0x04, 0xe3,
  40892. 0x24, 0x7e, 0x0d, 0x27, 0xf2, 0x86, 0x42, 0x3f, },
  40893. { 0x5f, 0xe2, 0xc1, 0xa1, 0x72, 0xfe, 0x93, 0xc4,
  40894. 0xb1, 0x5c, 0xd3, 0x7c, 0xae, 0xf9, 0xf5, 0x38, },
  40895. { 0x2c, 0x97, 0x32, 0x5c, 0xbd, 0x06, 0xb3, 0x6e,
  40896. 0xb2, 0x13, 0x3d, 0xd0, 0x8b, 0x3a, 0x01, 0x7c, },
  40897. { 0x92, 0xc8, 0x14, 0x22, 0x7a, 0x6b, 0xca, 0x94,
  40898. 0x9f, 0xf0, 0x65, 0x9f, 0x00, 0x2a, 0xd3, 0x9e, },
  40899. { 0xdc, 0xe8, 0x50, 0x11, 0x0b, 0xd8, 0x32, 0x8c,
  40900. 0xfb, 0xd5, 0x08, 0x41, 0xd6, 0x91, 0x1d, 0x87, },
  40901. { 0x67, 0xf1, 0x49, 0x84, 0xc7, 0xda, 0x79, 0x12,
  40902. 0x48, 0xe3, 0x2b, 0xb5, 0x92, 0x25, 0x83, 0xda, },
  40903. { 0x19, 0x38, 0xf2, 0xcf, 0x72, 0xd5, 0x4e, 0xe9,
  40904. 0x7e, 0x94, 0x16, 0x6f, 0xa9, 0x1d, 0x2a, 0x36, },
  40905. { 0x74, 0x48, 0x1e, 0x96, 0x46, 0xed, 0x49, 0xfe,
  40906. 0x0f, 0x62, 0x24, 0x30, 0x16, 0x04, 0x69, 0x8e, },
  40907. { 0x57, 0xfc, 0xa5, 0xde, 0x98, 0xa9, 0xd6, 0xd8,
  40908. 0x00, 0x64, 0x38, 0xd0, 0x58, 0x3d, 0x8a, 0x1d, },
  40909. { 0x9f, 0xec, 0xde, 0x1c, 0xef, 0xdc, 0x1c, 0xbe,
  40910. 0xd4, 0x76, 0x36, 0x74, 0xd9, 0x57, 0x53, 0x59, },
  40911. { 0xe3, 0x04, 0x0c, 0x00, 0xeb, 0x28, 0xf1, 0x53,
  40912. 0x66, 0xca, 0x73, 0xcb, 0xd8, 0x72, 0xe7, 0x40, },
  40913. { 0x76, 0x97, 0x00, 0x9a, 0x6a, 0x83, 0x1d, 0xfe,
  40914. 0xcc, 0xa9, 0x1c, 0x59, 0x93, 0x67, 0x0f, 0x7a, },
  40915. { 0x58, 0x53, 0x54, 0x23, 0x21, 0xf5, 0x67, 0xa0,
  40916. 0x05, 0xd5, 0x47, 0xa4, 0xf0, 0x47, 0x59, 0xbd, },
  40917. { 0x51, 0x50, 0xd1, 0x77, 0x2f, 0x50, 0x83, 0x4a,
  40918. 0x50, 0x3e, 0x06, 0x9a, 0x97, 0x3f, 0xbd, 0x7c, },
  40919. };
  40920. #endif
  40921. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t siphash_test(void)
  40922. {
  40923. wc_test_ret_t ret = 0;
  40924. int i;
  40925. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  40926. unsigned char res[SIPHASH_MAC_SIZE_16];
  40927. unsigned char tmp[SIPHASH_MAC_SIZE_8];
  40928. SipHash siphash;
  40929. WOLFSSL_ENTER("siphash_test (1)");
  40930. for (i = 0; i < 64; i++) {
  40931. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  40932. if (ret != 0)
  40933. return WC_TEST_RET_ENC_I(i);
  40934. ret = wc_SipHashUpdate(&siphash, siphash_msg, (word32)i);
  40935. if (ret != 0)
  40936. return WC_TEST_RET_ENC_I(i);
  40937. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  40938. if (ret != 0)
  40939. return WC_TEST_RET_ENC_I(i);
  40940. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  40941. return WC_TEST_RET_ENC_I(i);
  40942. ret = wc_SipHash(siphash_key, siphash_msg, (word32)i, res, SIPHASH_MAC_SIZE_8);
  40943. if (ret != 0)
  40944. return WC_TEST_RET_ENC_I(i);
  40945. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  40946. return WC_TEST_RET_ENC_I(i);
  40947. }
  40948. for (i = 0; i < 64; i++) {
  40949. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_16);
  40950. if (ret != 0)
  40951. return WC_TEST_RET_ENC_I(i);
  40952. ret = wc_SipHashUpdate(&siphash, siphash_msg, (word32)i);
  40953. if (ret != 0)
  40954. return WC_TEST_RET_ENC_I(i);
  40955. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  40956. if (ret != 0)
  40957. return WC_TEST_RET_ENC_I(i);
  40958. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  40959. return WC_TEST_RET_ENC_I(i);
  40960. ret = wc_SipHash(siphash_key, siphash_msg, (word32)i, res, SIPHASH_MAC_SIZE_16);
  40961. if (ret != 0)
  40962. return WC_TEST_RET_ENC_I(i);
  40963. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  40964. return WC_TEST_RET_ENC_I(i);
  40965. }
  40966. #else
  40967. WOLFSSL_ENTER("siphash_test (1)");
  40968. #endif
  40969. /* Testing bad parameters. */
  40970. ret = wc_InitSipHash(NULL, NULL, SIPHASH_MAC_SIZE_8);
  40971. if (ret != BAD_FUNC_ARG)
  40972. return WC_TEST_RET_ENC_EC(ret);
  40973. ret = wc_InitSipHash(NULL, siphash_key, SIPHASH_MAC_SIZE_8);
  40974. if (ret != BAD_FUNC_ARG)
  40975. return WC_TEST_RET_ENC_EC(ret);
  40976. ret = wc_InitSipHash(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  40977. if (ret != BAD_FUNC_ARG)
  40978. return WC_TEST_RET_ENC_EC(ret);
  40979. ret = wc_InitSipHash(&siphash, siphash_key, 7);
  40980. if (ret != BAD_FUNC_ARG)
  40981. return WC_TEST_RET_ENC_EC(ret);
  40982. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  40983. if (ret != 0)
  40984. return WC_TEST_RET_ENC_EC(ret);
  40985. ret = wc_SipHashUpdate(NULL, NULL, 0);
  40986. if (ret != BAD_FUNC_ARG)
  40987. return WC_TEST_RET_ENC_EC(ret);
  40988. ret = wc_SipHashUpdate(&siphash, NULL, 1);
  40989. if (ret != BAD_FUNC_ARG)
  40990. return WC_TEST_RET_ENC_EC(ret);
  40991. ret = wc_SipHashFinal(NULL, NULL, SIPHASH_MAC_SIZE_8);
  40992. if (ret != BAD_FUNC_ARG)
  40993. return WC_TEST_RET_ENC_EC(ret);
  40994. ret = wc_SipHashFinal(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  40995. if (ret != BAD_FUNC_ARG)
  40996. return WC_TEST_RET_ENC_EC(ret);
  40997. ret = wc_SipHashFinal(NULL, res, SIPHASH_MAC_SIZE_8);
  40998. if (ret != BAD_FUNC_ARG)
  40999. return WC_TEST_RET_ENC_EC(ret);
  41000. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  41001. if (ret != BAD_FUNC_ARG)
  41002. return WC_TEST_RET_ENC_EC(ret);
  41003. ret = wc_SipHash(NULL, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  41004. if (ret != BAD_FUNC_ARG)
  41005. return WC_TEST_RET_ENC_EC(ret);
  41006. ret = wc_SipHash(siphash_key, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  41007. if (ret != BAD_FUNC_ARG)
  41008. return WC_TEST_RET_ENC_EC(ret);
  41009. ret = wc_SipHash(NULL, NULL, 0, res, SIPHASH_MAC_SIZE_16);
  41010. if (ret != BAD_FUNC_ARG)
  41011. return WC_TEST_RET_ENC_EC(ret);
  41012. ret = wc_SipHash(siphash_key, NULL, 0, res, 15);
  41013. if (ret != BAD_FUNC_ARG)
  41014. return WC_TEST_RET_ENC_EC(ret);
  41015. ret = wc_SipHash(siphash_key, NULL, 1, res, SIPHASH_MAC_SIZE_16);
  41016. if (ret != BAD_FUNC_ARG)
  41017. return WC_TEST_RET_ENC_EC(ret);
  41018. /* Test cache with multiple non blocksize bytes */
  41019. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  41020. if (ret != 0)
  41021. return WC_TEST_RET_ENC_EC(ret);
  41022. ret = wc_SipHashUpdate(&siphash, siphash_msg, 5);
  41023. if (ret != 0)
  41024. return WC_TEST_RET_ENC_EC(ret);
  41025. ret = wc_SipHashUpdate(&siphash, siphash_msg + 5, 4);
  41026. if (ret != 0)
  41027. return WC_TEST_RET_ENC_EC(ret);
  41028. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  41029. if (ret != 0)
  41030. return WC_TEST_RET_ENC_EC(ret);
  41031. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  41032. if (ret != 0)
  41033. return WC_TEST_RET_ENC_EC(ret);
  41034. ret = wc_SipHashUpdate(&siphash, siphash_msg, 9);
  41035. if (ret != 0)
  41036. return WC_TEST_RET_ENC_EC(ret);
  41037. ret = wc_SipHashFinal(&siphash, tmp, SIPHASH_MAC_SIZE_8);
  41038. if (ret != 0)
  41039. return WC_TEST_RET_ENC_EC(ret);
  41040. if (XMEMCMP(res, tmp, SIPHASH_MAC_SIZE_8) != 0)
  41041. return WC_TEST_RET_ENC_NC;
  41042. return 0;
  41043. }
  41044. #endif /* WOLFSSL_SIPHASH */
  41045. #ifdef HAVE_LIBZ
  41046. static const byte sample_text[] =
  41047. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  41048. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  41049. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  41050. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  41051. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  41052. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  41053. "\n"
  41054. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  41055. "four loko you probably haven't heard of them high life. Messenger bag\n"
  41056. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  41057. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  41058. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  41059. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  41060. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  41061. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  41062. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  41063. "four loko whatever street art yr farm-to-table.\n"
  41064. "\n"
  41065. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  41066. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  41067. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  41068. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  41069. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  41070. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  41071. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  41072. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  41073. "locavore.\n"
  41074. "\n"
  41075. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  41076. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  41077. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  41078. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  41079. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  41080. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  41081. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  41082. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  41083. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  41084. "neutra PBR selvage.\n"
  41085. "\n"
  41086. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  41087. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  41088. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  41089. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  41090. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  41091. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  41092. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  41093. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  41094. "mollit magna, sriracha sartorial helvetica.\n"
  41095. "\n"
  41096. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  41097. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  41098. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  41099. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  41100. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  41101. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  41102. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  41103. "\n"
  41104. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  41105. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  41106. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  41107. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  41108. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  41109. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  41110. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  41111. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  41112. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  41113. "\n"
  41114. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  41115. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  41116. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  41117. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  41118. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  41119. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  41120. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  41121. "bag dolor terry richardson sapiente.\n";
  41122. static const byte sample_text_gz[] = {
  41123. 0x1F, 0x8B, 0x08, 0x08, 0xC5, 0x49, 0xB5, 0x5B, 0x00, 0x03, 0x63, 0x69, 0x70,
  41124. 0x68, 0x65, 0x72, 0x74, 0x65, 0x78, 0x74, 0x2E, 0x74, 0x78, 0x74, 0x00, 0x8D,
  41125. 0x58, 0xCB, 0x92, 0xE4, 0xB6, 0x11, 0xBC, 0xE3, 0x2B, 0xEA, 0xA6, 0x83, 0xD9,
  41126. 0x1D, 0x72, 0xF8, 0x22, 0x1F, 0xB5, 0x96, 0xA5, 0xDD, 0x90, 0xBC, 0xAB, 0xD0,
  41127. 0x28, 0x36, 0x42, 0x47, 0x90, 0x2C, 0x36, 0xA1, 0x06, 0x09, 0x0A, 0x8F, 0xEE,
  41128. 0xE1, 0xDF, 0x3B, 0x0B, 0xE0, 0x73, 0x2C, 0x4B, 0xBA, 0xCD, 0xCE, 0x80, 0x78,
  41129. 0x64, 0x65, 0x65, 0x66, 0xED, 0x3B, 0xE3, 0x5A, 0xC3, 0x81, 0x2D, 0x35, 0x69,
  41130. 0x32, 0xAD, 0x8E, 0x3A, 0xD2, 0xA0, 0x7D, 0xA7, 0x2B, 0x6A, 0xAC, 0x69, 0x7A,
  41131. 0x26, 0x9D, 0x22, 0xD3, 0x94, 0x22, 0x69, 0xAA, 0x8D, 0x6F, 0xC9, 0x8D, 0x64,
  41132. 0x22, 0x99, 0xB1, 0x31, 0xAD, 0x69, 0xD3, 0x18, 0x89, 0xAD, 0x89, 0x6A, 0x72,
  41133. 0x56, 0x7B, 0x67, 0xDA, 0x2B, 0xBD, 0xC8, 0xEF, 0xB0, 0x4D, 0x74, 0x8E, 0x5B,
  41134. 0xAA, 0x39, 0x4C, 0xEE, 0xCE, 0xE4, 0x79, 0xF2, 0xDC, 0xF3, 0xD8, 0xB2, 0x37,
  41135. 0x11, 0x8B, 0x8C, 0x2C, 0x7A, 0x32, 0x93, 0xF3, 0x37, 0x3D, 0x9A, 0x86, 0x4C,
  41136. 0xAB, 0xF2, 0xB9, 0x57, 0xFA, 0x97, 0x1B, 0x06, 0xD7, 0x3A, 0x7A, 0xF0, 0x68,
  41137. 0xF4, 0x40, 0xBA, 0x25, 0x0E, 0x81, 0xE9, 0xA6, 0x43, 0xF4, 0x6E, 0x4A, 0xF5,
  41138. 0x95, 0xFE, 0x41, 0x4F, 0x67, 0x3B, 0x1A, 0x1C, 0xEE, 0x12, 0xB4, 0x8F, 0xCE,
  41139. 0x1B, 0x6D, 0xB1, 0xDE, 0xBB, 0x4A, 0x4D, 0x56, 0x9B, 0x96, 0x5A, 0xB6, 0xDC,
  41140. 0xC4, 0x14, 0x70, 0xE5, 0xF5, 0x7D, 0xE1, 0xB7, 0x84, 0x3F, 0xFC, 0xED, 0xEF,
  41141. 0xF4, 0x30, 0x0D, 0x5F, 0xE9, 0x47, 0x17, 0xE2, 0xC5, 0x78, 0x27, 0x67, 0xDF,
  41142. 0xB9, 0xEB, 0xCC, 0xCC, 0x3D, 0x59, 0xBE, 0xDD, 0xCC, 0x78, 0x0B, 0x0A, 0x1F,
  41143. 0x74, 0xF8, 0x8C, 0x1A, 0xAF, 0x67, 0xEA, 0xF4, 0x44, 0xBD, 0x93, 0x7D, 0x2A,
  41144. 0xEA, 0x9C, 0xD7, 0x37, 0x80, 0x32, 0x9A, 0x01, 0x37, 0xD5, 0xDE, 0xCA, 0xA2,
  41145. 0x0D, 0xB9, 0xD0, 0x3B, 0xCF, 0xAD, 0x89, 0x4D, 0x5F, 0xD1, 0xE7, 0xF7, 0x2F,
  41146. 0x2A, 0x0C, 0xDA, 0x5A, 0xAA, 0x35, 0x7E, 0x41, 0xC3, 0xB2, 0x37, 0xDD, 0xDD,
  41147. 0xCD, 0x50, 0xEB, 0x2C, 0x96, 0x62, 0x3B, 0xD7, 0x52, 0xF4, 0xA9, 0xB9, 0x6F,
  41148. 0x48, 0xED, 0xEF, 0x54, 0xEA, 0x67, 0xF6, 0x7E, 0x26, 0x8F, 0x3A, 0x68, 0xDF,
  41149. 0x06, 0xBC, 0x56, 0xB7, 0x66, 0x32, 0xC1, 0x34, 0xD8, 0x88, 0x34, 0x1E, 0x88,
  41150. 0xED, 0x67, 0x8A, 0xF3, 0xC4, 0x4F, 0xC0, 0xCA, 0x9E, 0x62, 0x1A, 0x6A, 0xEB,
  41151. 0xAB, 0x02, 0xED, 0xB3, 0xD7, 0x91, 0x81, 0x8A, 0xEA, 0x5C, 0xF2, 0x64, 0xDD,
  41152. 0xDD, 0xD1, 0xEC, 0x12, 0x4D, 0xDE, 0xD5, 0xBA, 0xC6, 0x77, 0xBD, 0x06, 0xC4,
  41153. 0x5F, 0x44, 0xEA, 0x59, 0x4B, 0x5D, 0x3B, 0x8A, 0x3D, 0x0F, 0xD4, 0x9B, 0x1B,
  41154. 0x80, 0x30, 0x1D, 0x30, 0xFA, 0x8F, 0x00, 0x3F, 0xDE, 0xB0, 0x6F, 0xAD, 0x6F,
  41155. 0x6A, 0xDD, 0x6E, 0x2F, 0x6E, 0xCB, 0x3C, 0xD1, 0x83, 0x06, 0x7B, 0x0F, 0xFD,
  41156. 0xFD, 0x4A, 0xEF, 0xBC, 0x73, 0x77, 0x3B, 0x8F, 0x34, 0xA1, 0xBA, 0xEC, 0x39,
  41157. 0x80, 0x33, 0x21, 0xA4, 0x01, 0x55, 0xD7, 0xD4, 0xF4, 0xC6, 0xDA, 0x27, 0x4E,
  41158. 0x54, 0x1C, 0x2B, 0xEC, 0x37, 0xDE, 0xC3, 0x4C, 0xC9, 0x5A, 0x3D, 0x34, 0x0E,
  41159. 0xD8, 0x1C, 0x0E, 0xA2, 0x34, 0xE8, 0xC1, 0xD0, 0xA4, 0x51, 0xD5, 0x88, 0x8B,
  41160. 0xB7, 0xC6, 0xA3, 0x96, 0x40, 0x49, 0xB7, 0xBC, 0xE0, 0x7F, 0x55, 0x3F, 0xEF,
  41161. 0x6F, 0x6E, 0x92, 0x9D, 0x34, 0xFE, 0x3C, 0x5F, 0x04, 0xA5, 0x6A, 0xFF, 0x30,
  41162. 0x08, 0xC9, 0xEA, 0xF5, 0x52, 0x2B, 0xFE, 0x57, 0xFA, 0x8E, 0xC7, 0xE8, 0x4D,
  41163. 0x37, 0xAB, 0x03, 0xFA, 0x23, 0xBF, 0x46, 0x94, 0xFF, 0xC1, 0x16, 0xE0, 0xB9,
  41164. 0x14, 0x2C, 0x9E, 0x27, 0xEC, 0x98, 0x69, 0x14, 0x92, 0xF1, 0x60, 0x5C, 0x34,
  41165. 0x4D, 0xA0, 0x1F, 0xDF, 0xFD, 0x44, 0x1C, 0x7B, 0xD3, 0x80, 0x70, 0x42, 0x02,
  41166. 0x30, 0x84, 0x5B, 0xE5, 0x59, 0xB7, 0xF3, 0x80, 0xFB, 0x01, 0x33, 0xA9, 0x00,
  41167. 0x37, 0x52, 0xDC, 0xDA, 0xA7, 0x11, 0x85, 0xB7, 0x6E, 0x70, 0xE4, 0xDA, 0x96,
  41168. 0xBA, 0x84, 0x5B, 0x81, 0x43, 0x93, 0xF3, 0xD1, 0xEA, 0xB1, 0xDD, 0xB8, 0x1F,
  41169. 0xA5, 0xCC, 0xEA, 0x50, 0x66, 0x69, 0xA9, 0x8D, 0x8C, 0xA7, 0xA2, 0xF3, 0x38,
  41170. 0x26, 0x43, 0x5E, 0x3F, 0x01, 0xBE, 0x1C, 0x0F, 0x20, 0x7F, 0x75, 0xA8, 0x20,
  41171. 0x80, 0xC4, 0xC3, 0x5C, 0x8B, 0x0D, 0xD4, 0x60, 0x5E, 0xA3, 0x9E, 0xD0, 0xB4,
  41172. 0x4B, 0x4F, 0xE6, 0x13, 0x85, 0x60, 0x42, 0x96, 0xED, 0xAA, 0xDB, 0xE9, 0x99,
  41173. 0xE3, 0x07, 0x0E, 0x61, 0xB3, 0x07, 0xE3, 0xB1, 0xFA, 0xC0, 0x9B, 0xAD, 0xF6,
  41174. 0xE0, 0x26, 0x33, 0xEA, 0xEA, 0x23, 0xCD, 0x1E, 0x9D, 0xE1, 0x87, 0x4B, 0x74,
  41175. 0x97, 0x08, 0x3E, 0xA1, 0x28, 0xEA, 0xB3, 0x19, 0x67, 0x8B, 0x76, 0x9A, 0xA3,
  41176. 0xF6, 0xB9, 0xCF, 0x80, 0x65, 0x97, 0xAE, 0xF4, 0x83, 0x6B, 0xF4, 0x43, 0x20,
  41177. 0xF9, 0x0B, 0xFC, 0x9B, 0xD2, 0x4D, 0x4D, 0xA6, 0xB9, 0xA3, 0x02, 0x55, 0x79,
  41178. 0x18, 0x36, 0x19, 0x5F, 0xC9, 0xEA, 0x5A, 0x76, 0x40, 0xB9, 0xBA, 0x0E, 0x9A,
  41179. 0x44, 0xDF, 0x7C, 0xF8, 0x65, 0x61, 0x5E, 0x81, 0xAB, 0x71, 0xA1, 0x9E, 0x29,
  41180. 0x3C, 0x59, 0xCB, 0x23, 0xA4, 0xF6, 0x60, 0x1A, 0x0D, 0x5B, 0x39, 0xAE, 0xF4,
  41181. 0x6F, 0x59, 0x16, 0x9E, 0x60, 0xD8, 0x56, 0xCF, 0xEA, 0x2C, 0x4C, 0x79, 0xD3,
  41182. 0x5D, 0x51, 0x46, 0xA0, 0x4E, 0xE9, 0xD6, 0xAB, 0x91, 0x43, 0x63, 0x44, 0xD7,
  41183. 0x70, 0xB9, 0x23, 0x98, 0x4F, 0x3D, 0x03, 0x02, 0xF6, 0x81, 0x56, 0xC1, 0x58,
  41184. 0x85, 0x07, 0xA7, 0x2D, 0x2C, 0x29, 0xCA, 0x01, 0x45, 0x31, 0x51, 0x8F, 0xD4,
  41185. 0x19, 0xA1, 0x79, 0x88, 0x5A, 0xA4, 0xF5, 0xAE, 0x2D, 0x4B, 0x63, 0x4C, 0x58,
  41186. 0xFE, 0xBF, 0xAD, 0xEE, 0xA3, 0x09, 0xF8, 0xE2, 0x89, 0xBE, 0x81, 0x0E, 0x86,
  41187. 0x3A, 0xF9, 0x5B, 0xA5, 0xD8, 0xA4, 0x00, 0x75, 0x04, 0xF2, 0x23, 0xB8, 0x39,
  41188. 0x69, 0x50, 0xB7, 0xD0, 0x34, 0x63, 0x54, 0xD8, 0x61, 0xDD, 0xA5, 0x33, 0x47,
  41189. 0x85, 0x96, 0x22, 0xD0, 0x2F, 0x9F, 0x7E, 0xF8, 0x74, 0x24, 0xEA, 0x57, 0x97,
  41190. 0x5A, 0xE0, 0x00, 0xCF, 0xC1, 0x67, 0xE1, 0x41, 0xBD, 0x94, 0xA1, 0x03, 0xD3,
  41191. 0xB4, 0x08, 0x64, 0xF2, 0x17, 0x27, 0x35, 0x37, 0x53, 0xEF, 0x46, 0xCE, 0xD8,
  41192. 0xD4, 0x09, 0x52, 0xC6, 0x1E, 0xF7, 0x28, 0xDF, 0x08, 0x0F, 0xD0, 0x6F, 0x71,
  41193. 0xA6, 0xDF, 0xE4, 0x60, 0x8E, 0xC0, 0x1E, 0x78, 0x86, 0x50, 0xB0, 0x9B, 0x84,
  41194. 0x7E, 0xE8, 0x36, 0xFA, 0x95, 0xF1, 0x12, 0x51, 0xC7, 0x18, 0x96, 0xA2, 0x29,
  41195. 0xBB, 0x70, 0x02, 0xB4, 0xF9, 0xA8, 0x3D, 0x08, 0x66, 0xA9, 0xB3, 0xFC, 0x0A,
  41196. 0x94, 0x80, 0xFD, 0x78, 0xDC, 0xAB, 0x82, 0x5A, 0xD2, 0xCD, 0xC2, 0x87, 0xC6,
  41197. 0x4B, 0x07, 0xFA, 0xD1, 0xC3, 0xD9, 0x34, 0x41, 0x85, 0xF8, 0xD0, 0xB6, 0x0A,
  41198. 0x9D, 0x00, 0x91, 0x35, 0x05, 0x88, 0xC3, 0xE3, 0x9B, 0x22, 0xD2, 0xB8, 0xFD,
  41199. 0x95, 0x3E, 0x6D, 0x5D, 0x48, 0xA3, 0x68, 0xCF, 0x02, 0x42, 0x79, 0x79, 0x8A,
  41200. 0xAA, 0x01, 0xD6, 0x09, 0x14, 0x2C, 0xF4, 0x83, 0xA3, 0x80, 0x31, 0x55, 0x46,
  41201. 0x6E, 0xC5, 0xE5, 0x2F, 0x30, 0x58, 0x81, 0xA2, 0x90, 0xBE, 0x2E, 0xA1, 0xC3,
  41202. 0x0F, 0xA6, 0xF5, 0x51, 0x00, 0x39, 0xB6, 0xF2, 0x2A, 0xA3, 0x15, 0x7D, 0x8D,
  41203. 0xF5, 0x66, 0x5C, 0xD9, 0xFC, 0xCF, 0x2F, 0xBF, 0x08, 0x27, 0xE7, 0xD0, 0x03,
  41204. 0xB8, 0xD9, 0x00, 0x13, 0x3D, 0x01, 0x6B, 0xB6, 0xA8, 0xCD, 0x5B, 0x3B, 0x3E,
  41205. 0x93, 0xBF, 0xE6, 0x2E, 0xB7, 0x4A, 0xCF, 0xB3, 0x0A, 0xCE, 0x62, 0x11, 0xD6,
  41206. 0x1F, 0x68, 0x9B, 0x1D, 0x68, 0xD1, 0x8C, 0x97, 0xBD, 0xA1, 0x07, 0x67, 0x73,
  41207. 0x87, 0xE0, 0x36, 0xDA, 0x8C, 0xD2, 0xD2, 0xBB, 0x84, 0x28, 0xA9, 0xFE, 0x52,
  41208. 0x74, 0xD6, 0xB9, 0x0F, 0x0A, 0x6A, 0x2D, 0x28, 0x35, 0x34, 0x3A, 0xD3, 0xE2,
  41209. 0xCD, 0x35, 0x06, 0x7D, 0x1B, 0x35, 0x85, 0x86, 0xD1, 0x3E, 0xF2, 0x6F, 0xA1,
  41210. 0xC4, 0x55, 0xBD, 0x00, 0xD8, 0xC3, 0x5D, 0xC2, 0x1D, 0x6B, 0x6B, 0x27, 0x5B,
  41211. 0x95, 0xF3, 0xAB, 0xB5, 0xD3, 0x37, 0xF2, 0x2C, 0x9C, 0xC7, 0x5D, 0xBD, 0xF1,
  41212. 0x68, 0x1C, 0xAD, 0xF8, 0xB5, 0xE1, 0x29, 0x72, 0x7A, 0x73, 0x62, 0x55, 0x24,
  41213. 0xB9, 0x85, 0xDF, 0x7B, 0x29, 0x7D, 0xDE, 0x08, 0xF5, 0xE4, 0x44, 0xDA, 0x1A,
  41214. 0x30, 0x74, 0xDA, 0xB4, 0x9B, 0x23, 0x9A, 0x3A, 0xC1, 0x53, 0xB2, 0xA2, 0xA3,
  41215. 0x7B, 0x1F, 0xD9, 0x56, 0xD4, 0x4F, 0x9B, 0xB2, 0x1E, 0xEE, 0xB8, 0x6A, 0x4E,
  41216. 0xB5, 0xF4, 0x5A, 0xC9, 0x18, 0x27, 0x9C, 0xDE, 0x14, 0x44, 0xED, 0xC4, 0x3C,
  41217. 0x71, 0x9F, 0x5F, 0xD9, 0x37, 0xA0, 0x78, 0x34, 0x6E, 0xBC, 0xD2, 0x7B, 0x1D,
  41218. 0xFA, 0x08, 0x39, 0x5A, 0x04, 0x73, 0x15, 0xD9, 0x0A, 0x48, 0xC1, 0x2D, 0x15,
  41219. 0x4E, 0x84, 0x30, 0x45, 0x69, 0xB3, 0xE5, 0xF6, 0xAD, 0x09, 0x1E, 0xCC, 0x5F,
  41220. 0x1F, 0x06, 0xD5, 0x58, 0xAD, 0x78, 0xD7, 0x9F, 0xE5, 0xED, 0x3B, 0x09, 0xD5,
  41221. 0xA6, 0x52, 0x6F, 0x92, 0xD3, 0x3C, 0xC6, 0x1E, 0xF2, 0x93, 0x7C, 0xD3, 0x5F,
  41222. 0x70, 0x85, 0x5D, 0xF8, 0xAA, 0x9D, 0xB7, 0x7B, 0x24, 0x5A, 0xE9, 0x0A, 0x35,
  41223. 0x2F, 0xF5, 0xD9, 0x82, 0x02, 0x8A, 0x90, 0x13, 0x5B, 0xB5, 0x67, 0x9C, 0xDD,
  41224. 0xA0, 0x4E, 0x82, 0x27, 0xDA, 0x7E, 0xE8, 0x8E, 0xCD, 0xE1, 0x56, 0x71, 0x2C,
  41225. 0xE6, 0x4E, 0x1F, 0x91, 0xCD, 0x7C, 0x6A, 0xB7, 0x78, 0xD0, 0x26, 0xF3, 0x56,
  41226. 0xA9, 0xD5, 0xA1, 0xC3, 0x3B, 0x98, 0xE9, 0x28, 0x09, 0xEF, 0x50, 0x90, 0xCD,
  41227. 0xC4, 0x8E, 0x75, 0xCC, 0xAC, 0x2D, 0xC9, 0x03, 0x6D, 0xAC, 0xFE, 0xC4, 0x88,
  41228. 0x36, 0xD1, 0x3F, 0xBB, 0x1C, 0x7D, 0xB3, 0x14, 0x61, 0x2C, 0xB7, 0x54, 0x4B,
  41229. 0xDB, 0x64, 0xB6, 0x57, 0x14, 0x16, 0x8E, 0x1E, 0x6C, 0x64, 0xBB, 0x8B, 0x48,
  41230. 0x5D, 0x96, 0x9D, 0xDC, 0x80, 0xA7, 0xF7, 0x54, 0xC7, 0x46, 0x38, 0x3E, 0x44,
  41231. 0xDE, 0x7E, 0x92, 0x8D, 0x07, 0xF6, 0x07, 0x37, 0x4E, 0x16, 0x10, 0xB4, 0x7D,
  41232. 0x88, 0x66, 0x7F, 0xBB, 0xFF, 0xEA, 0x00, 0xF3, 0xFF, 0x97, 0x2C, 0xB5, 0xBE,
  41233. 0x35, 0x4B, 0x5C, 0x36, 0xEC, 0x4C, 0xBD, 0x2B, 0x7D, 0xBF, 0x46, 0xE2, 0x9C,
  41234. 0x0E, 0x8A, 0xA3, 0xEC, 0xB1, 0x0E, 0x9A, 0xDA, 0x9A, 0x9B, 0x28, 0x92, 0x10,
  41235. 0x53, 0x57, 0xEA, 0xEC, 0xA2, 0x32, 0x32, 0x20, 0x1D, 0x97, 0x5C, 0xB6, 0x84,
  41236. 0xA9, 0x93, 0x8D, 0x95, 0x11, 0xA3, 0x24, 0xA3, 0x2D, 0xC6, 0x4A, 0xEF, 0xAA,
  41237. 0x1D, 0x85, 0x2B, 0x7D, 0x28, 0xBE, 0x53, 0xCE, 0x10, 0x1F, 0xAE, 0x0E, 0x41,
  41238. 0x6C, 0x4B, 0x79, 0x12, 0xFB, 0xF7, 0x54, 0xA3, 0x96, 0x54, 0x83, 0x20, 0x96,
  41239. 0x8F, 0x28, 0xA9, 0x3F, 0x8B, 0x3D, 0xBA, 0x77, 0xDC, 0x24, 0xE1, 0xD4, 0x49,
  41240. 0x40, 0xD8, 0x78, 0x31, 0x85, 0x43, 0xF6, 0xFE, 0x5C, 0xA6, 0x8F, 0x90, 0x09,
  41241. 0xB0, 0xE7, 0xC4, 0x95, 0xB2, 0x55, 0x49, 0x97, 0x8F, 0x1C, 0x78, 0x30, 0x20,
  41242. 0xA0, 0xB4, 0xEF, 0x73, 0x56, 0x59, 0x82, 0xFD, 0xCE, 0xBA, 0x6A, 0x8F, 0x2C,
  41243. 0x8B, 0x15, 0xFD, 0xA1, 0x85, 0xA8, 0x5C, 0x0F, 0x11, 0xA5, 0x9D, 0xC2, 0x46,
  41244. 0xC6, 0x9C, 0xC9, 0x40, 0x0B, 0x58, 0x6A, 0x1C, 0x7A, 0x23, 0xF9, 0xE0, 0x95,
  41245. 0x05, 0x13, 0x58, 0x72, 0xE8, 0x9F, 0x30, 0xAC, 0xCD, 0x26, 0xD4, 0x66, 0x13,
  41246. 0xDF, 0x1E, 0x7B, 0x4F, 0x9C, 0xBE, 0x38, 0x79, 0x75, 0x92, 0xA4, 0xDA, 0x26,
  41247. 0x44, 0x55, 0x17, 0xA3, 0xE5, 0x62, 0xDA, 0xEB, 0x86, 0xEA, 0x68, 0xC7, 0xAB,
  41248. 0xFD, 0x2D, 0x43, 0x59, 0x51, 0xC0, 0x75, 0x64, 0x91, 0x01, 0x29, 0x33, 0x28,
  41249. 0xF3, 0x04, 0x83, 0x80, 0x75, 0x37, 0x75, 0x0C, 0x03, 0x7B, 0x0A, 0xAB, 0x8E,
  41250. 0x60, 0x62, 0x8B, 0x4C, 0xAF, 0x2D, 0xA3, 0x2F, 0xFE, 0xAB, 0x45, 0xCF, 0xDA,
  41251. 0xAB, 0xFA, 0xFA, 0x30, 0x3D, 0xE8, 0xA1, 0x96, 0xA5, 0x7B, 0xE2, 0x2A, 0xD0,
  41252. 0xAF, 0x59, 0xF7, 0xD0, 0x32, 0x57, 0x19, 0xBD, 0xCA, 0x9F, 0xD5, 0x1A, 0xC7,
  41253. 0xAA, 0x65, 0x4A, 0x38, 0xB2, 0x70, 0x33, 0xB7, 0x75, 0xD2, 0xCD, 0xD1, 0xF0,
  41254. 0xA8, 0x87, 0x59, 0x20, 0xA5, 0x57, 0x55, 0xB1, 0xB2, 0xC9, 0x4D, 0x97, 0x34,
  41255. 0x41, 0xF3, 0xF0, 0x30, 0xA1, 0x2C, 0x1C, 0x49, 0x3E, 0x89, 0x7D, 0x12, 0xE2,
  41256. 0xC3, 0x04, 0xC3, 0x92, 0xC0, 0xF6, 0x39, 0x10, 0x80, 0x81, 0x8F, 0x08, 0xB4,
  41257. 0xF8, 0xB9, 0x13, 0x4E, 0x2C, 0xAE, 0xB3, 0x71, 0x82, 0x63, 0x98, 0xAB, 0x5C,
  41258. 0x1C, 0x10, 0xEA, 0x66, 0xF9, 0x02, 0x3A, 0x82, 0x61, 0xD0, 0xD4, 0xAE, 0x43,
  41259. 0xD4, 0x01, 0x3E, 0x9D, 0x04, 0x14, 0xF6, 0x60, 0xD8, 0xA7, 0xD6, 0xB8, 0x53,
  41260. 0xC8, 0xDA, 0x80, 0x93, 0xA0, 0x02, 0xDD, 0xCC, 0xE2, 0xF2, 0xBB, 0xFB, 0xE0,
  41261. 0x27, 0xD7, 0x34, 0x9A, 0x71, 0x49, 0xB5, 0x4F, 0x42, 0x1F, 0xB2, 0x9D, 0x6D,
  41262. 0xAA, 0x9D, 0xD3, 0x50, 0xB5, 0x8F, 0x6A, 0x4B, 0xDF, 0x1F, 0xD5, 0x27, 0x8F,
  41263. 0x3B, 0x27, 0xCF, 0x2F, 0x8C, 0xF8, 0x9D, 0x4C, 0x52, 0xBC, 0x32, 0x0F, 0x73,
  41264. 0xD5, 0x51, 0x8E, 0x36, 0x7E, 0xAD, 0x09, 0xF0, 0x94, 0x83, 0x5F, 0x36, 0xFD,
  41265. 0x7C, 0x03, 0xED, 0xF1, 0x5E, 0x4B, 0xF7, 0xAA, 0x55, 0x5C, 0x4A, 0x14, 0x59,
  41266. 0x85, 0x38, 0x2D, 0x8C, 0xDF, 0xEC, 0x65, 0x1B, 0xB8, 0x76, 0x57, 0x96, 0x3C,
  41267. 0x86, 0xED, 0xF2, 0x7F, 0x2D, 0x28, 0x48, 0xDA, 0x49, 0x7F, 0xF7, 0x54, 0x2B,
  41268. 0xD5, 0x39, 0xD5, 0x57, 0x0A, 0x75, 0x7A, 0x3E, 0x5E, 0x5D, 0xBA, 0x4A, 0x15,
  41269. 0xFA, 0xB8, 0x31, 0x80, 0x71, 0x2C, 0xCA, 0xC4, 0x51, 0x10, 0x16, 0x5D, 0x39,
  41270. 0xEC, 0x9D, 0x07, 0xB6, 0x6A, 0x89, 0x9F, 0x9B, 0x5B, 0x6F, 0x03, 0xB0, 0x92,
  41271. 0x01, 0x38, 0x6B, 0x48, 0x99, 0x0A, 0x8F, 0x13, 0xC1, 0xA6, 0x01, 0xEA, 0xBF,
  41272. 0x6F, 0x86, 0x43, 0x51, 0xB6, 0x11, 0x00, 0x00
  41273. };
  41274. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t compress_test(void)
  41275. {
  41276. wc_test_ret_t ret = 0;
  41277. word32 dSz = sizeof(sample_text);
  41278. word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
  41279. byte *c;
  41280. byte *d;
  41281. WOLFSSL_ENTER("compress_test");
  41282. c = (byte *)XMALLOC(cSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41283. d = (byte *)XMALLOC(dSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41284. if (c == NULL || d == NULL) {
  41285. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  41286. }
  41287. /* follow calloc and initialize to 0 */
  41288. XMEMSET(c, 0, cSz);
  41289. XMEMSET(d, 0, dSz);
  41290. if ((ret = wc_Compress(c, cSz, sample_text, dSz, 0)) < 0) {
  41291. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  41292. }
  41293. cSz = (word32)ret;
  41294. if ((ret = wc_DeCompress(d, dSz, c, cSz)) != (int)dSz) {
  41295. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  41296. }
  41297. dSz = (word32)ret;
  41298. if (XMEMCMP(d, sample_text, dSz) != 0) {
  41299. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  41300. }
  41301. /* GZIP tests */
  41302. cSz = (dSz + (word32)(dSz * 0.001) + 12); /* reset cSz */
  41303. XMEMSET(c, 0, cSz);
  41304. XMEMSET(d, 0, dSz);
  41305. ret = wc_Compress_ex(c, cSz, sample_text, dSz, 0, LIBZ_WINBITS_GZIP);
  41306. if (ret < 0)
  41307. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  41308. cSz = (word32)ret;
  41309. ret = wc_DeCompress_ex(d, dSz, c, cSz, LIBZ_WINBITS_GZIP);
  41310. if (ret < 0)
  41311. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  41312. if (XMEMCMP(d, sample_text, dSz) != 0) {
  41313. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  41314. }
  41315. /* Try with gzip generated output */
  41316. XMEMSET(d, 0, dSz);
  41317. ret = wc_DeCompress_ex(d, dSz, sample_text_gz, sizeof(sample_text_gz),
  41318. LIBZ_WINBITS_GZIP);
  41319. if (ret < 0)
  41320. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  41321. dSz = (word32)ret;
  41322. if (XMEMCMP(d, sample_text, dSz) != 0) {
  41323. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  41324. }
  41325. ret = 0; /* success */
  41326. exit:
  41327. if (c) XFREE(c, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41328. if (d) XFREE(d, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41329. return ret;
  41330. }
  41331. #endif /* HAVE_LIBZ */
  41332. #ifdef HAVE_PKCS7
  41333. /* External Debugging/Testing Note:
  41334. *
  41335. * PKCS#7 test functions can output generated PKCS#7/CMS bundles for
  41336. * additional testing. To dump bundles to files DER encoded files, please
  41337. * define:
  41338. *
  41339. * #define PKCS7_OUTPUT_TEST_BUNDLES
  41340. */
  41341. /* Loads certs and keys for use with PKCS7 tests, from either files
  41342. * or buffers.
  41343. *
  41344. * rsaClientCertBuf - output buffer for RSA client cert
  41345. * rsaClientCertBufSz - IN/OUT size of output buffer, size of RSA client cert
  41346. * rsaClientPrivKeyBuf - output buffer for RSA client private key
  41347. * rsaClientPrivKeyBufSz - IN/OUT size of output buffer, size of RSA client key
  41348. *
  41349. * rsaServerCertBuf - output buffer for RSA server cert
  41350. * rsaServerCertBufSz - IN/OUT size of output buffer, size of RSA server cert
  41351. * rsaServerPrivKeyBuf - output buffer for RSA server private key
  41352. * rsaServerPrivKeyBufSz - IN/OUT size of output buffer, size of RSA server key
  41353. *
  41354. * rsaCaCertBuf - output buffer for RSA CA cert
  41355. * rsaCaCertBufSz - IN/OUT size of output buffer, size of RSA ca cert
  41356. * rsaCaPrivKeyBuf - output buffer for RSA CA private key
  41357. * rsaCaPrivKeyBufSz - IN/OUT size of output buffer, size of RSA CA key
  41358. *
  41359. * eccClientCertBuf - output buffer for ECC cert
  41360. * eccClientCertBufSz - IN/OUT size of output buffer, size of ECC cert
  41361. * eccClientPrivKeyBuf - output buffer for ECC private key
  41362. * eccClientPrivKeyBufSz - IN/OUT size of output buffer, size of ECC private key
  41363. *
  41364. * Returns 0 on success, negative on error
  41365. */
  41366. static wc_test_ret_t pkcs7_load_certs_keys(
  41367. byte* rsaClientCertBuf, word32* rsaClientCertBufSz,
  41368. byte* rsaClientPrivKeyBuf, word32* rsaClientPrivKeyBufSz,
  41369. byte* rsaServerCertBuf, word32* rsaServerCertBufSz,
  41370. byte* rsaServerPrivKeyBuf, word32* rsaServerPrivKeyBufSz,
  41371. byte* rsaCaCertBuf, word32* rsaCaCertBufSz,
  41372. byte* rsaCaPrivKeyBuf, word32* rsaCaPrivKeyBufSz,
  41373. byte* eccClientCertBuf, word32* eccClientCertBufSz,
  41374. byte* eccClientPrivKeyBuf, word32* eccClientPrivKeyBufSz)
  41375. {
  41376. #ifndef NO_FILESYSTEM
  41377. XFILE certFile;
  41378. XFILE keyFile;
  41379. (void)certFile;
  41380. (void)keyFile;
  41381. #endif
  41382. #ifndef NO_RSA
  41383. if (rsaClientCertBuf == NULL || rsaClientCertBufSz == NULL ||
  41384. rsaClientPrivKeyBuf == NULL || rsaClientPrivKeyBufSz == NULL)
  41385. return BAD_FUNC_ARG;
  41386. #endif
  41387. #ifdef HAVE_ECC
  41388. if (eccClientCertBuf == NULL || eccClientCertBufSz == NULL ||
  41389. eccClientPrivKeyBuf == NULL || eccClientPrivKeyBufSz == NULL)
  41390. return BAD_FUNC_ARG;
  41391. #endif
  41392. /* RSA */
  41393. #ifndef NO_RSA
  41394. #ifdef USE_CERT_BUFFERS_1024
  41395. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_1024)
  41396. return WC_TEST_RET_ENC_NC;
  41397. XMEMCPY(rsaClientCertBuf, client_cert_der_1024,
  41398. sizeof_client_cert_der_1024);
  41399. *rsaClientCertBufSz = sizeof_client_cert_der_1024;
  41400. if (rsaServerCertBuf != NULL) {
  41401. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_1024)
  41402. return WC_TEST_RET_ENC_NC;
  41403. XMEMCPY(rsaServerCertBuf, server_cert_der_1024,
  41404. sizeof_server_cert_der_1024);
  41405. *rsaServerCertBufSz = sizeof_server_cert_der_1024;
  41406. }
  41407. if (rsaCaCertBuf != NULL) {
  41408. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_1024)
  41409. return WC_TEST_RET_ENC_NC;
  41410. XMEMCPY(rsaCaCertBuf, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  41411. *rsaCaCertBufSz = sizeof_ca_cert_der_1024;
  41412. }
  41413. #elif defined(USE_CERT_BUFFERS_2048)
  41414. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_2048)
  41415. return WC_TEST_RET_ENC_NC;
  41416. XMEMCPY(rsaClientCertBuf, client_cert_der_2048,
  41417. sizeof_client_cert_der_2048);
  41418. *rsaClientCertBufSz = sizeof_client_cert_der_2048;
  41419. if (rsaServerCertBuf != NULL) {
  41420. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_2048)
  41421. return WC_TEST_RET_ENC_NC;
  41422. XMEMCPY(rsaServerCertBuf, server_cert_der_2048,
  41423. sizeof_server_cert_der_2048);
  41424. *rsaServerCertBufSz = sizeof_server_cert_der_2048;
  41425. }
  41426. if (rsaCaCertBuf != NULL) {
  41427. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_2048)
  41428. return WC_TEST_RET_ENC_NC;
  41429. XMEMCPY(rsaCaCertBuf, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  41430. *rsaCaCertBufSz = sizeof_ca_cert_der_2048;
  41431. }
  41432. #else
  41433. certFile = XFOPEN(clientCert, "rb");
  41434. if (!certFile)
  41435. return WC_TEST_RET_ENC_ERRNO;
  41436. *rsaClientCertBufSz = (word32)XFREAD(rsaClientCertBuf, 1,
  41437. *rsaClientCertBufSz, certFile);
  41438. XFCLOSE(certFile);
  41439. if (*rsaClientCertBufSz == 0)
  41440. return WC_TEST_RET_ENC_ERRNO;
  41441. if (rsaServerCertBuf != NULL) {
  41442. certFile = XFOPEN(rsaServerCertDerFile, "rb");
  41443. if (!certFile)
  41444. return WC_TEST_RET_ENC_ERRNO;
  41445. *rsaServerCertBufSz = (word32)XFREAD(rsaServerCertBuf, 1,
  41446. *rsaServerCertBufSz, certFile);
  41447. XFCLOSE(certFile);
  41448. if (*rsaServerCertBufSz == 0)
  41449. return WC_TEST_RET_ENC_ERRNO;
  41450. }
  41451. if (rsaCaCertBuf != NULL) {
  41452. certFile = XFOPEN(rsaCaCertDerFile, "rb");
  41453. if (!certFile)
  41454. return WC_TEST_RET_ENC_ERRNO;
  41455. *rsaCaCertBufSz = (word32)XFREAD(rsaCaCertBuf, 1, *rsaCaCertBufSz,
  41456. certFile);
  41457. XFCLOSE(certFile);
  41458. if (*rsaCaCertBufSz == 0)
  41459. return WC_TEST_RET_ENC_ERRNO;
  41460. }
  41461. #endif
  41462. #ifdef USE_CERT_BUFFERS_1024
  41463. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_1024)
  41464. return WC_TEST_RET_ENC_NC;
  41465. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_1024,
  41466. sizeof_client_key_der_1024);
  41467. *rsaClientPrivKeyBufSz = sizeof_client_key_der_1024;
  41468. if (rsaServerPrivKeyBuf != NULL) {
  41469. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_1024)
  41470. return WC_TEST_RET_ENC_NC;
  41471. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_1024,
  41472. sizeof_server_key_der_1024);
  41473. *rsaServerPrivKeyBufSz = sizeof_server_key_der_1024;
  41474. }
  41475. if (rsaCaPrivKeyBuf != NULL) {
  41476. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_1024)
  41477. return WC_TEST_RET_ENC_NC;
  41478. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_1024, sizeof_ca_key_der_1024);
  41479. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_1024;
  41480. }
  41481. #elif defined(USE_CERT_BUFFERS_2048)
  41482. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_2048)
  41483. return WC_TEST_RET_ENC_NC;
  41484. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_2048,
  41485. sizeof_client_key_der_2048);
  41486. *rsaClientPrivKeyBufSz = sizeof_client_key_der_2048;
  41487. if (rsaServerPrivKeyBuf != NULL) {
  41488. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_2048)
  41489. return WC_TEST_RET_ENC_NC;
  41490. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_2048,
  41491. sizeof_server_key_der_2048);
  41492. *rsaServerPrivKeyBufSz = sizeof_server_key_der_2048;
  41493. }
  41494. if (rsaCaPrivKeyBuf != NULL) {
  41495. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_2048)
  41496. return WC_TEST_RET_ENC_NC;
  41497. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_2048, sizeof_ca_key_der_2048);
  41498. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_2048;
  41499. }
  41500. #else
  41501. keyFile = XFOPEN(clientKey, "rb");
  41502. if (!keyFile)
  41503. return WC_TEST_RET_ENC_ERRNO;
  41504. *rsaClientPrivKeyBufSz = (word32)XFREAD(rsaClientPrivKeyBuf, 1,
  41505. *rsaClientPrivKeyBufSz, keyFile);
  41506. XFCLOSE(keyFile);
  41507. if (*rsaClientPrivKeyBufSz == 0)
  41508. return WC_TEST_RET_ENC_ERRNO;
  41509. if (rsaServerPrivKeyBuf != NULL) {
  41510. keyFile = XFOPEN(rsaServerKeyDerFile, "rb");
  41511. if (!keyFile)
  41512. return WC_TEST_RET_ENC_ERRNO;
  41513. *rsaServerPrivKeyBufSz = (word32)XFREAD(rsaServerPrivKeyBuf, 1,
  41514. *rsaServerPrivKeyBufSz, keyFile);
  41515. XFCLOSE(keyFile);
  41516. if (*rsaServerPrivKeyBufSz == 0)
  41517. return WC_TEST_RET_ENC_ERRNO;
  41518. }
  41519. if (rsaCaPrivKeyBuf != NULL) {
  41520. keyFile = XFOPEN(rsaCaKeyFile, "rb");
  41521. if (!keyFile)
  41522. return WC_TEST_RET_ENC_ERRNO;
  41523. *rsaCaPrivKeyBufSz = (word32)XFREAD(rsaCaPrivKeyBuf, 1,
  41524. *rsaCaPrivKeyBufSz, keyFile);
  41525. XFCLOSE(keyFile);
  41526. if (*rsaCaPrivKeyBufSz == 0)
  41527. return WC_TEST_RET_ENC_ERRNO;
  41528. }
  41529. #endif /* USE_CERT_BUFFERS */
  41530. #endif /* NO_RSA */
  41531. /* ECC */
  41532. #ifdef HAVE_ECC
  41533. #ifdef USE_CERT_BUFFERS_256
  41534. if (*eccClientCertBufSz < (word32)sizeof_cliecc_cert_der_256)
  41535. return WC_TEST_RET_ENC_NC;
  41536. XMEMCPY(eccClientCertBuf, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  41537. *eccClientCertBufSz = sizeof_cliecc_cert_der_256;
  41538. #else
  41539. certFile = XFOPEN(eccClientCert, "rb");
  41540. if (!certFile)
  41541. return WC_TEST_RET_ENC_ERRNO;
  41542. *eccClientCertBufSz = (word32)XFREAD(eccClientCertBuf, 1,
  41543. *eccClientCertBufSz, certFile);
  41544. XFCLOSE(certFile);
  41545. if (*eccClientCertBufSz == 0)
  41546. return WC_TEST_RET_ENC_ERRNO;
  41547. #endif /* USE_CERT_BUFFERS_256 */
  41548. #ifdef USE_CERT_BUFFERS_256
  41549. if (*eccClientPrivKeyBufSz < (word32)sizeof_ecc_clikey_der_256)
  41550. return WC_TEST_RET_ENC_NC;
  41551. XMEMCPY(eccClientPrivKeyBuf, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  41552. *eccClientPrivKeyBufSz = sizeof_ecc_clikey_der_256;
  41553. #else
  41554. keyFile = XFOPEN(eccClientKey, "rb");
  41555. if (!keyFile)
  41556. return WC_TEST_RET_ENC_ERRNO;
  41557. *eccClientPrivKeyBufSz = (word32)XFREAD(eccClientPrivKeyBuf, 1,
  41558. *eccClientPrivKeyBufSz, keyFile);
  41559. XFCLOSE(keyFile);
  41560. if (*eccClientPrivKeyBufSz == 0)
  41561. return WC_TEST_RET_ENC_ERRNO;
  41562. #endif /* USE_CERT_BUFFERS_256 */
  41563. #endif /* HAVE_ECC */
  41564. #ifdef NO_RSA
  41565. (void)rsaClientCertBuf;
  41566. (void)rsaClientCertBufSz;
  41567. (void)rsaClientPrivKeyBuf;
  41568. (void)rsaClientPrivKeyBufSz;
  41569. (void)rsaServerCertBuf;
  41570. (void)rsaServerCertBufSz;
  41571. (void)rsaServerPrivKeyBuf;
  41572. (void)rsaServerPrivKeyBufSz;
  41573. (void)rsaCaCertBuf;
  41574. (void)rsaCaCertBufSz;
  41575. (void)rsaCaPrivKeyBuf;
  41576. (void)rsaCaPrivKeyBufSz;
  41577. #endif
  41578. #ifndef HAVE_ECC
  41579. (void)eccClientCertBuf;
  41580. (void)eccClientCertBufSz;
  41581. (void)eccClientPrivKeyBuf;
  41582. (void)eccClientPrivKeyBufSz;
  41583. #endif
  41584. #ifndef NO_FILESYSTEM
  41585. (void)certFile;
  41586. (void)keyFile;
  41587. #endif
  41588. return 0;
  41589. }
  41590. typedef struct {
  41591. const byte* content;
  41592. word32 contentSz;
  41593. int contentOID;
  41594. int encryptOID;
  41595. int keyWrapOID;
  41596. int keyAgreeOID;
  41597. byte* cert;
  41598. size_t certSz;
  41599. byte* privateKey;
  41600. word32 privateKeySz;
  41601. byte* optionalUkm;
  41602. word32 optionalUkmSz;
  41603. int ktriOptions; /* KTRI options flags */
  41604. int kariOptions; /* KARI options flags */
  41605. /* KEKRI specific */
  41606. const byte* secretKey; /* key, only for kekri RecipientInfo types */
  41607. word32 secretKeySz; /* size of secretKey, bytes */
  41608. const byte* secretKeyId; /* key identifier */
  41609. word32 secretKeyIdSz; /* size of key identifier, bytes */
  41610. void* timePtr; /* time_t pointer */
  41611. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  41612. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  41613. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  41614. word32 otherAttrSz; /* size of otherAttr, bytes */
  41615. int kekriOptions; /* KEKRI options flags */
  41616. /* PWRI specific */
  41617. const char* password;
  41618. word32 passwordSz;
  41619. const byte* salt;
  41620. word32 saltSz;
  41621. int kdfOID;
  41622. int hashOID;
  41623. int kdfIterations;
  41624. int pwriOptions; /* PWRI options flags */
  41625. /* ORI specific */
  41626. int isOri;
  41627. int oriOptions; /* ORI options flags */
  41628. const char* outFileName;
  41629. } pkcs7EnvelopedVector;
  41630. static const byte asnDataOid[] = {
  41631. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
  41632. };
  41633. /* ORI encrypt callback, responsible for encrypting content-encryption key (CEK)
  41634. * and giving wolfCrypt the value for oriOID and oriValue to place in
  41635. * OtherRecipientInfo.
  41636. *
  41637. * Returns 0 on success, negative upon error. */
  41638. static int myOriEncryptCb(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* oriType,
  41639. word32* oriTypeSz, byte* oriValue, word32* oriValueSz,
  41640. void* ctx)
  41641. {
  41642. int i;
  41643. /* make sure buffers are large enough */
  41644. if (*oriValueSz < (2 + cekSz))
  41645. return WC_TEST_RET_ENC_NC;
  41646. if (*oriTypeSz < sizeof(asnDataOid))
  41647. return WC_TEST_RET_ENC_NC;
  41648. /* our simple encryption algorithm will be take the bitwise complement */
  41649. oriValue[0] = 0x04; /*ASN OCTET STRING */
  41650. oriValue[1] = (byte)cekSz; /* length */
  41651. for (i = 0; i < (int)cekSz; i++) {
  41652. oriValue[2 + i] = ~cek[i];
  41653. }
  41654. *oriValueSz = 2 + cekSz;
  41655. /* set oriType to ASN.1 encoded data OID */
  41656. XMEMCPY(oriType, asnDataOid, sizeof(asnDataOid));
  41657. *oriTypeSz = sizeof(asnDataOid);
  41658. (void)pkcs7;
  41659. (void)ctx;
  41660. return 0;
  41661. }
  41662. /* ORI decrypt callback, responsible for providing a decrypted content
  41663. * encryption key (CEK) placed into decryptedKey and size placed into
  41664. * decryptedKeySz. oriOID and oriValue are given to the callback to help
  41665. * in decrypting the encrypted CEK.
  41666. *
  41667. * Returns 0 on success, negative upon error. */
  41668. static int myOriDecryptCb(PKCS7* pkcs7, byte* oriType, word32 oriTypeSz,
  41669. byte* oriValue, word32 oriValueSz, byte* decryptedKey,
  41670. word32* decryptedKeySz, void* ctx)
  41671. {
  41672. int i;
  41673. /* make sure oriType matches what we expect */
  41674. if (oriTypeSz != sizeof(asnDataOid))
  41675. return WC_TEST_RET_ENC_NC;
  41676. if (XMEMCMP(oriType, asnDataOid, sizeof(asnDataOid)) != 0)
  41677. return WC_TEST_RET_ENC_NC;
  41678. /* make sure decrypted buffer is large enough */
  41679. if (*decryptedKeySz < oriValueSz)
  41680. return WC_TEST_RET_ENC_NC;
  41681. /* decrypt encrypted CEK using simple bitwise complement,
  41682. only for example */
  41683. for (i = 0; i < (int)oriValueSz - 2; i++) {
  41684. decryptedKey[i] = ~oriValue[2 + i];
  41685. }
  41686. *decryptedKeySz = oriValueSz - 2;
  41687. (void)pkcs7;
  41688. (void)ctx;
  41689. return 0;
  41690. }
  41691. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  41692. /* returns 0 on success */
  41693. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  41694. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  41695. byte* in, int inSz, byte* out, void* usrCtx)
  41696. {
  41697. wc_test_ret_t ret;
  41698. int keyId = -1, keySz;
  41699. word32 keyIdSz = 8;
  41700. const byte* key;
  41701. byte keyIdRaw[8];
  41702. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  41703. Aes *aes;
  41704. #else
  41705. Aes aes[1];
  41706. #endif
  41707. /* looking for KEY ID
  41708. * fwDecryptKeyID OID "1.2.840.113549.1.9.16.2.37
  41709. */
  41710. WOLFSSL_SMALL_STACK_STATIC const unsigned char OID[] = {
  41711. /* 0x06, 0x0B do not pass in tag and length */
  41712. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  41713. 0x01, 0x09, 0x10, 0x02, 0x25
  41714. };
  41715. WOLFSSL_SMALL_STACK_STATIC const byte defKey[] = {
  41716. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  41717. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  41718. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  41719. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  41720. };
  41721. WOLFSSL_SMALL_STACK_STATIC const byte altKey[] = {
  41722. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  41723. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  41724. };
  41725. /* test user context passed in */
  41726. if (usrCtx == NULL || *(int*)usrCtx != 1) {
  41727. return WC_TEST_RET_ENC_NC;
  41728. }
  41729. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  41730. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  41731. return WC_TEST_RET_ENC_ERRNO;
  41732. #endif
  41733. /* if needing to find keyIdSz can call with NULL */
  41734. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), NULL,
  41735. &keyIdSz);
  41736. if (ret != LENGTH_ONLY_E) {
  41737. printf("Unexpected error %d when getting keyIdSz\n", ret);
  41738. printf("Possibly no KEY ID attribute set\n");
  41739. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  41740. }
  41741. else {
  41742. XMEMSET(keyIdRaw, 0, sizeof(keyIdRaw));
  41743. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), keyIdRaw,
  41744. &keyIdSz);
  41745. if (ret < 0) {
  41746. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  41747. }
  41748. if (keyIdSz < 3) {
  41749. printf("keyIdSz is smaller than expected\n");
  41750. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  41751. }
  41752. if (keyIdSz > 2 + sizeof(int)) {
  41753. printf("example case was only expecting a keyId of int size\n");
  41754. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  41755. }
  41756. /* keyIdRaw[0] OCTET TAG */
  41757. /* keyIdRaw[1] Length */
  41758. #ifdef BIG_ENDIAN_ORDER
  41759. if (keyIdRaw[1] == 0x01) {
  41760. keyId = 1;
  41761. }
  41762. #else
  41763. XMEMCPY(&keyId, keyIdRaw + 2, sizeof(keyId));
  41764. #endif
  41765. }
  41766. /* Use keyID here if found to select key and decrypt in HSM or in this
  41767. * example just select key and do software decryption */
  41768. if (keyId == 1) {
  41769. key = altKey;
  41770. keySz = sizeof(altKey);
  41771. }
  41772. else {
  41773. key = defKey;
  41774. keySz = sizeof(defKey);
  41775. }
  41776. switch (encryptOID) {
  41777. #ifdef WOLFSSL_AES_256
  41778. case AES256CBCb:
  41779. if ((keySz != 32 ) || (ivSz != AES_BLOCK_SIZE))
  41780. WARNING_OUT(BAD_FUNC_ARG, out);
  41781. break;
  41782. #endif
  41783. #ifdef WOLFSSL_AES_128
  41784. case AES128CBCb:
  41785. if ((keySz != 16 ) || (ivSz != AES_BLOCK_SIZE))
  41786. ERROR_OUT(BAD_FUNC_ARG, out);
  41787. break;
  41788. #endif
  41789. default:
  41790. printf("Unsupported content cipher type for example");
  41791. ERROR_OUT(ALGO_ID_E, out);
  41792. };
  41793. ret = wc_AesInit(aes, HEAP_HINT, INVALID_DEVID);
  41794. if (ret == 0) {
  41795. ret = wc_AesSetKey(aes, key, (word32)keySz, iv, AES_DECRYPTION);
  41796. if (ret == 0)
  41797. ret = wc_AesCbcDecrypt(aes, out, in, (word32)inSz);
  41798. wc_AesFree(aes);
  41799. }
  41800. out:
  41801. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  41802. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41803. #endif
  41804. (void)aad;
  41805. (void)aadSz;
  41806. (void)authTag;
  41807. (void)authTagSz;
  41808. return (int)ret;
  41809. }
  41810. #endif /* !NO_AES && HAVE_AES_CBC */
  41811. #define PKCS7_BUF_SIZE 2048
  41812. static wc_test_ret_t pkcs7enveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  41813. byte* rsaPrivKey, word32 rsaPrivKeySz,
  41814. byte* eccCert, word32 eccCertSz,
  41815. byte* eccPrivKey, word32 eccPrivKeySz)
  41816. {
  41817. wc_test_ret_t ret = 0;
  41818. int testSz = 0, i;
  41819. int envelopedSz, decodedSz;
  41820. byte *enveloped = NULL;
  41821. byte *decoded = NULL;
  41822. PKCS7* pkcs7 = NULL;
  41823. #ifdef ECC_TIMING_RESISTANT
  41824. WC_RNG rng;
  41825. #endif
  41826. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  41827. XFILE pkcs7File;
  41828. #endif
  41829. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  41830. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  41831. 0x72,0x6c,0x64
  41832. };
  41833. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256) && \
  41834. defined(HAVE_ECC) && defined(WOLFSSL_SHA512)
  41835. byte optionalUkm[] = {
  41836. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  41837. };
  41838. #endif /* NO_AES */
  41839. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) && \
  41840. !defined(NO_SHA)
  41841. /* encryption key for kekri recipient types */
  41842. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  41843. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  41844. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  41845. };
  41846. /* encryption key identifier */
  41847. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  41848. 0x02,0x02,0x03,0x04
  41849. };
  41850. #endif
  41851. #if !defined(NO_PWDBASED) && !defined(NO_SHA) && \
  41852. !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  41853. #ifndef HAVE_FIPS
  41854. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password"; /* NOTE: Password is too short for FIPS */
  41855. #else
  41856. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  41857. #endif
  41858. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  41859. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  41860. };
  41861. #endif
  41862. #define MAX_TESTVECTORS_LEN 13
  41863. #define ADD_PKCS7ENVELOPEDVECTOR(...) { \
  41864. pkcs7EnvelopedVector _this_vector = { __VA_ARGS__ }; \
  41865. if (testSz == MAX_TESTVECTORS_LEN) { \
  41866. ret = WC_TEST_RET_ENC_NC; \
  41867. goto out; \
  41868. } \
  41869. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  41870. }
  41871. pkcs7EnvelopedVector *testVectors = NULL;
  41872. #ifdef ECC_TIMING_RESISTANT
  41873. XMEMSET(&rng, 0, sizeof(rng));
  41874. #endif
  41875. testVectors = (pkcs7EnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  41876. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41877. if (testVectors == NULL) {
  41878. ret = WC_TEST_RET_ENC_ERRNO;
  41879. goto out;
  41880. }
  41881. {
  41882. /* key transport key encryption technique */
  41883. #ifndef NO_RSA
  41884. #ifndef NO_DES3
  41885. ADD_PKCS7ENVELOPEDVECTOR(
  41886. data, (word32)sizeof(data), DATA, DES3b, 0, 0, rsaCert, rsaCertSz,
  41887. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  41888. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  41889. "pkcs7envelopedDataDES3.der");
  41890. #endif
  41891. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  41892. #ifdef WOLFSSL_AES_128
  41893. ADD_PKCS7ENVELOPEDVECTOR(
  41894. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, rsaCert, rsaCertSz,
  41895. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  41896. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  41897. "pkcs7envelopedDataAES128CBC.der");
  41898. #endif
  41899. #ifdef WOLFSSL_AES_192
  41900. ADD_PKCS7ENVELOPEDVECTOR(
  41901. data, (word32)sizeof(data), DATA, AES192CBCb, 0, 0, rsaCert, rsaCertSz,
  41902. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  41903. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  41904. "pkcs7envelopedDataAES192CBC.der");
  41905. #endif
  41906. #ifdef WOLFSSL_AES_256
  41907. ADD_PKCS7ENVELOPEDVECTOR(
  41908. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  41909. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  41910. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  41911. "pkcs7envelopedDataAES256CBC.der");
  41912. /* explicitly using SKID for SubjectKeyIdentifier */
  41913. ADD_PKCS7ENVELOPEDVECTOR(
  41914. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  41915. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_SKID, 0, NULL, 0, NULL, 0, NULL,
  41916. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  41917. "pkcs7envelopedDataAES256CBC_SKID.der");
  41918. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  41919. ADD_PKCS7ENVELOPEDVECTOR(
  41920. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  41921. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_ISSUER_AND_SERIAL_NUMBER, 0,
  41922. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  41923. 0, 0, 0, 0, "pkcs7envelopedDataAES256CBC_IANDS.der");
  41924. #endif
  41925. #endif /* !NO_AES && HAVE_AES_CBC */
  41926. #endif
  41927. /* key agreement key encryption technique*/
  41928. #ifdef HAVE_ECC
  41929. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  41930. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  41931. ADD_PKCS7ENVELOPEDVECTOR(
  41932. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP,
  41933. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  41934. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  41935. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  41936. "pkcs7envelopedDataAES128CBC_ECDH_SHA1KDF.der");
  41937. #endif
  41938. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  41939. ADD_PKCS7ENVELOPEDVECTOR(
  41940. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  41941. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  41942. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  41943. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  41944. "pkcs7envelopedDataAES256CBC_ECDH_SHA256KDF.der");
  41945. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  41946. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  41947. ADD_PKCS7ENVELOPEDVECTOR(
  41948. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  41949. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  41950. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  41951. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  41952. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF.der");
  41953. /* with optional user keying material (ukm) */
  41954. ADD_PKCS7ENVELOPEDVECTOR(
  41955. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  41956. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  41957. eccPrivKeySz, optionalUkm, sizeof(optionalUkm), 0, 0, NULL, 0,
  41958. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  41959. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF_ukm.der");
  41960. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  41961. #endif /* !NO_AES && HAVE_AES_CBC */
  41962. #endif
  41963. /* kekri (KEKRecipientInfo) recipient types */
  41964. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  41965. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  41966. ADD_PKCS7ENVELOPEDVECTOR(
  41967. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP, 0,
  41968. NULL, 0, NULL, 0, NULL, 0, 0, 0, secretKey, sizeof(secretKey),
  41969. secretKeyId, sizeof(secretKeyId), NULL, NULL, 0, NULL, 0,
  41970. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  41971. "pkcs7envelopedDataAES128CBC_KEKRI.der");
  41972. #endif
  41973. #endif /* !NO_AES && HAVE_AES_CBC */
  41974. /* pwri (PasswordRecipientInfo) recipient types */
  41975. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  41976. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  41977. ADD_PKCS7ENVELOPEDVECTOR(
  41978. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0,
  41979. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  41980. NULL, 0, NULL, NULL, 0, NULL, 0, 0, password,
  41981. (word32)XSTRLEN(password), salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  41982. 0, 0, 0, "pkcs7envelopedDataAES128CBC_PWRI.der");
  41983. #endif
  41984. #endif
  41985. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_128)
  41986. /* ori (OtherRecipientInfo) recipient types */
  41987. ADD_PKCS7ENVELOPEDVECTOR(
  41988. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, NULL, 0, NULL, 0,
  41989. NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0,
  41990. NULL, 0, 0, 0, 0, 0, 1, 0, "pkcs7envelopedDataAES128CBC_ORI.der");
  41991. #endif
  41992. };
  41993. #undef MAX_TESTVECTORS_LEN
  41994. #undef ADD_PKCS7ENVELOPEDVECTOR
  41995. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41996. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41997. if ((! enveloped) || (! decoded)) {
  41998. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  41999. }
  42000. #ifdef ECC_TIMING_RESISTANT
  42001. #ifndef HAVE_FIPS
  42002. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  42003. #else
  42004. ret = wc_InitRng(&rng);
  42005. #endif
  42006. if (ret != 0)
  42007. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42008. #endif
  42009. for (i = 0; i < testSz; i++) {
  42010. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  42011. #ifdef WOLFSSL_ASYNC_CRYPT
  42012. INVALID_DEVID /* async PKCS7 is not supported */
  42013. #else
  42014. devId
  42015. #endif
  42016. );
  42017. if (pkcs7 == NULL) {
  42018. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42019. }
  42020. if (testVectors[i].secretKey != NULL) {
  42021. /* KEKRI recipient type */
  42022. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  42023. if (ret != 0)
  42024. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42025. pkcs7->content = (byte*)testVectors[i].content;
  42026. pkcs7->contentSz = testVectors[i].contentSz;
  42027. pkcs7->contentOID = testVectors[i].contentOID;
  42028. pkcs7->encryptOID = testVectors[i].encryptOID;
  42029. pkcs7->ukm = testVectors[i].optionalUkm;
  42030. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  42031. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  42032. (byte *)testVectors[i].secretKey, testVectors[i].secretKeySz,
  42033. (byte *)testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  42034. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  42035. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  42036. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  42037. if (ret < 0) {
  42038. wc_PKCS7_Free(pkcs7);
  42039. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42040. }
  42041. /* set key, for decryption */
  42042. ret = wc_PKCS7_SetKey(pkcs7, (byte *)testVectors[i].secretKey,
  42043. testVectors[i].secretKeySz);
  42044. if (ret != 0) {
  42045. wc_PKCS7_Free(pkcs7);
  42046. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42047. }
  42048. } else if (testVectors[i].password != NULL) {
  42049. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  42050. /* PWRI recipient type */
  42051. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  42052. if (ret != 0)
  42053. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42054. pkcs7->content = (byte*)testVectors[i].content;
  42055. pkcs7->contentSz = testVectors[i].contentSz;
  42056. pkcs7->contentOID = testVectors[i].contentOID;
  42057. pkcs7->encryptOID = testVectors[i].encryptOID;
  42058. pkcs7->ukm = testVectors[i].optionalUkm;
  42059. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  42060. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  42061. (byte *)testVectors[i].password, testVectors[i].passwordSz,
  42062. (byte *)testVectors[i].salt, testVectors[i].saltSz,
  42063. testVectors[i].kdfOID,
  42064. testVectors[i].hashOID, testVectors[i].kdfIterations,
  42065. testVectors[i].encryptOID, testVectors[i].pwriOptions);
  42066. if (ret < 0) {
  42067. wc_PKCS7_Free(pkcs7);
  42068. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42069. }
  42070. /* set password, for decryption */
  42071. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  42072. testVectors[i].passwordSz);
  42073. if (ret < 0) {
  42074. wc_PKCS7_Free(pkcs7);
  42075. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42076. }
  42077. #endif /* ! NO_PWDBASED && ! NO_SHA */
  42078. } else if (testVectors[i].isOri == 1) {
  42079. /* ORI recipient type */
  42080. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  42081. if (ret != 0)
  42082. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42083. pkcs7->content = (byte*)testVectors[i].content;
  42084. pkcs7->contentSz = testVectors[i].contentSz;
  42085. pkcs7->contentOID = testVectors[i].contentOID;
  42086. pkcs7->encryptOID = testVectors[i].encryptOID;
  42087. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  42088. testVectors[i].oriOptions);
  42089. if (ret < 0) {
  42090. wc_PKCS7_Free(pkcs7);
  42091. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42092. }
  42093. /* set decrypt callback for decryption */
  42094. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  42095. if (ret < 0) {
  42096. wc_PKCS7_Free(pkcs7);
  42097. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42098. }
  42099. } else {
  42100. /* KTRI or KARI recipient types */
  42101. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  42102. if (ret != 0)
  42103. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42104. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  42105. (word32)testVectors[i].certSz);
  42106. if (ret != 0) {
  42107. wc_PKCS7_Free(pkcs7);
  42108. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42109. }
  42110. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  42111. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  42112. pkcs7->privateKey = testVectors[i].privateKey;
  42113. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  42114. pkcs7->content = (byte*)testVectors[i].content;
  42115. pkcs7->contentSz = testVectors[i].contentSz;
  42116. pkcs7->contentOID = testVectors[i].contentOID;
  42117. pkcs7->encryptOID = testVectors[i].encryptOID;
  42118. pkcs7->ukm = testVectors[i].optionalUkm;
  42119. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  42120. /* set SubjectIdentifier type for KTRI types */
  42121. if (testVectors[i].ktriOptions & CMS_SKID) {
  42122. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  42123. if (ret != 0) {
  42124. wc_PKCS7_Free(pkcs7);
  42125. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42126. }
  42127. } else if (testVectors[i].ktriOptions &
  42128. CMS_ISSUER_AND_SERIAL_NUMBER) {
  42129. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  42130. CMS_ISSUER_AND_SERIAL_NUMBER);
  42131. if (ret != 0) {
  42132. wc_PKCS7_Free(pkcs7);
  42133. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42134. }
  42135. }
  42136. }
  42137. #ifdef ECC_TIMING_RESISTANT
  42138. pkcs7->rng = &rng;
  42139. #endif
  42140. /* encode envelopedData */
  42141. envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, enveloped,
  42142. PKCS7_BUF_SIZE);
  42143. if (envelopedSz <= 0) {
  42144. wc_PKCS7_Free(pkcs7);
  42145. ERROR_OUT(WC_TEST_RET_ENC_EC(envelopedSz), out);
  42146. }
  42147. /* decode envelopedData */
  42148. pkcs7->contentOID = 0;
  42149. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped, (word32)envelopedSz,
  42150. decoded, PKCS7_BUF_SIZE);
  42151. if (pkcs7->contentOID != testVectors[i].contentOID ||
  42152. decodedSz <= 0) {
  42153. wc_PKCS7_Free(pkcs7);
  42154. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  42155. }
  42156. /* test decode result */
  42157. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  42158. wc_PKCS7_Free(pkcs7);
  42159. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42160. }
  42161. #ifndef NO_PKCS7_STREAM
  42162. { /* test reading byte by byte */
  42163. int z;
  42164. for (z = 0; z < envelopedSz; z++) {
  42165. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped + z, 1,
  42166. decoded, PKCS7_BUF_SIZE);
  42167. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  42168. printf("unexpected error %d\n", decodedSz);
  42169. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  42170. }
  42171. }
  42172. /* test decode result */
  42173. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  42174. printf("stream read compare failed\n");
  42175. wc_PKCS7_Free(pkcs7);
  42176. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42177. }
  42178. }
  42179. #endif
  42180. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  42181. /* output pkcs7 envelopedData for external testing */
  42182. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  42183. if (!pkcs7File) {
  42184. wc_PKCS7_Free(pkcs7);
  42185. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42186. }
  42187. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  42188. XFCLOSE(pkcs7File);
  42189. if (ret != envelopedSz) {
  42190. wc_PKCS7_Free(pkcs7);
  42191. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42192. } else {
  42193. /* reset ret to 0 for success */
  42194. ret = 0;
  42195. }
  42196. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  42197. wc_PKCS7_Free(pkcs7);
  42198. pkcs7 = NULL;
  42199. }
  42200. #ifdef ECC_TIMING_RESISTANT
  42201. wc_FreeRng(&rng);
  42202. #endif
  42203. (void)eccCert;
  42204. (void)eccCertSz;
  42205. (void)eccPrivKey;
  42206. (void)eccPrivKeySz;
  42207. (void)rsaCert;
  42208. (void)rsaCertSz;
  42209. (void)rsaPrivKey;
  42210. (void)rsaPrivKeySz;
  42211. out:
  42212. if (testVectors)
  42213. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42214. if (enveloped)
  42215. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42216. if (decoded)
  42217. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42218. return ret;
  42219. }
  42220. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7enveloped_test(void)
  42221. {
  42222. wc_test_ret_t ret = 0;
  42223. byte* rsaCert = NULL;
  42224. byte* rsaPrivKey = NULL;
  42225. word32 rsaCertSz = 0;
  42226. word32 rsaPrivKeySz = 0;
  42227. byte* eccCert = NULL;
  42228. byte* eccPrivKey = NULL;
  42229. word32 eccCertSz = 0;
  42230. word32 eccPrivKeySz = 0;
  42231. WOLFSSL_ENTER("pkcs7enveloped_test");
  42232. #ifndef NO_RSA
  42233. /* read client RSA cert and key in DER format */
  42234. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42235. if (rsaCert == NULL)
  42236. return WC_TEST_RET_ENC_ERRNO;
  42237. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42238. if (rsaPrivKey == NULL) {
  42239. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42240. return WC_TEST_RET_ENC_NC;
  42241. }
  42242. rsaCertSz = FOURK_BUF;
  42243. rsaPrivKeySz = FOURK_BUF;
  42244. #endif /* NO_RSA */
  42245. #ifdef HAVE_ECC
  42246. /* read client ECC cert and key in DER format */
  42247. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42248. if (eccCert == NULL) {
  42249. #ifndef NO_RSA
  42250. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42251. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42252. #endif
  42253. return WC_TEST_RET_ENC_NC;
  42254. }
  42255. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42256. if (eccPrivKey == NULL) {
  42257. #ifndef NO_RSA
  42258. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42259. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42260. #endif
  42261. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42262. return WC_TEST_RET_ENC_NC;
  42263. }
  42264. eccCertSz = FOURK_BUF;
  42265. eccPrivKeySz = FOURK_BUF;
  42266. #endif /* HAVE_ECC */
  42267. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  42268. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  42269. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  42270. eccPrivKey, &eccPrivKeySz);
  42271. if (ret < 0) {
  42272. #ifndef NO_RSA
  42273. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42274. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42275. #endif
  42276. #ifdef HAVE_ECC
  42277. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42278. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42279. #endif
  42280. return WC_TEST_RET_ENC_EC(ret);
  42281. }
  42282. ret = pkcs7enveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  42283. rsaPrivKey, (word32)rsaPrivKeySz,
  42284. eccCert, (word32)eccCertSz,
  42285. eccPrivKey, (word32)eccPrivKeySz);
  42286. #ifndef NO_RSA
  42287. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42288. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42289. #endif
  42290. #ifdef HAVE_ECC
  42291. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42292. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42293. #endif
  42294. return ret;
  42295. }
  42296. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  42297. typedef struct {
  42298. const byte* content;
  42299. word32 contentSz;
  42300. int contentOID;
  42301. int encryptOID;
  42302. int keyWrapOID;
  42303. int keyAgreeOID;
  42304. byte* cert;
  42305. size_t certSz;
  42306. byte* privateKey;
  42307. word32 privateKeySz;
  42308. PKCS7Attrib* authAttribs;
  42309. word32 authAttribsSz;
  42310. PKCS7Attrib* unauthAttribs;
  42311. word32 unauthAttribsSz;
  42312. /* KARI / KTRI specific */
  42313. byte* optionalUkm;
  42314. word32 optionalUkmSz;
  42315. int ktriOptions; /* KTRI options flags */
  42316. int kariOptions; /* KARI options flags */
  42317. /* KEKRI specific */
  42318. byte* secretKey; /* key, only for kekri RecipientInfo types */
  42319. word32 secretKeySz; /* size of secretKey, bytes */
  42320. byte* secretKeyId; /* key identifier */
  42321. word32 secretKeyIdSz; /* size of key identifier, bytes */
  42322. void* timePtr; /* time_t pointer */
  42323. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  42324. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  42325. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  42326. word32 otherAttrSz; /* size of otherAttr, bytes */
  42327. int kekriOptions; /* KEKRI options flags */
  42328. /* PWRI specific */
  42329. char* password; /* password */
  42330. word32 passwordSz; /* password size, bytes */
  42331. byte* salt; /* KDF salt */
  42332. word32 saltSz; /* KDF salt size, bytes */
  42333. int kdfOID; /* KDF OID */
  42334. int hashOID; /* KDF hash algorithm OID */
  42335. int kdfIterations; /* KDF iterations */
  42336. int kekEncryptOID; /* KEK encryption algorithm OID */
  42337. int pwriOptions; /* PWRI options flags */
  42338. /* ORI specific */
  42339. int isOri;
  42340. int oriOptions; /* ORI options flags */
  42341. const char* outFileName;
  42342. } pkcs7AuthEnvelopedVector;
  42343. static wc_test_ret_t pkcs7authenveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  42344. byte* rsaPrivKey, word32 rsaPrivKeySz,
  42345. byte* eccCert, word32 eccCertSz,
  42346. byte* eccPrivKey, word32 eccPrivKeySz)
  42347. {
  42348. wc_test_ret_t ret = 0;
  42349. int testSz = 0, i;
  42350. int envelopedSz, decodedSz;
  42351. byte *enveloped = NULL;
  42352. byte *decoded = NULL;
  42353. WC_RNG rng;
  42354. PKCS7* pkcs7;
  42355. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  42356. XFILE pkcs7File;
  42357. #endif
  42358. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  42359. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  42360. 0x72,0x6c,0x64
  42361. };
  42362. byte senderNonce[PKCS7_NONCE_SZ + 2];
  42363. #ifdef HAVE_ECC
  42364. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  42365. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  42366. WOLFSSL_SMALL_STACK_STATIC const byte senderNonceOid[] =
  42367. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  42368. 0x09, 0x05 };
  42369. PKCS7Attrib attribs[] =
  42370. {
  42371. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  42372. sizeof(senderNonce) }
  42373. };
  42374. #endif
  42375. #endif
  42376. #endif
  42377. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  42378. defined(WOLFSSL_SHA512) && defined(HAVE_AESGCM)
  42379. WOLFSSL_SMALL_STACK_STATIC const byte optionalUkm[] = {
  42380. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  42381. };
  42382. #endif /* NO_AES */
  42383. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  42384. /* encryption key for kekri recipient types */
  42385. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  42386. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  42387. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  42388. };
  42389. /* encryption key identifier */
  42390. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  42391. 0x02,0x02,0x03,0x04
  42392. };
  42393. #endif
  42394. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  42395. !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  42396. #ifndef HAVE_FIPS
  42397. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password";
  42398. #else
  42399. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  42400. #endif
  42401. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  42402. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  42403. };
  42404. #endif
  42405. #define MAX_TESTVECTORS_LEN 20
  42406. #define ADD_PKCS7AUTHENVELOPEDVECTOR(...) { \
  42407. pkcs7AuthEnvelopedVector _this_vector = { __VA_ARGS__ }; \
  42408. if (testSz == MAX_TESTVECTORS_LEN) { \
  42409. ret = WC_TEST_RET_ENC_NC; \
  42410. goto out; \
  42411. } \
  42412. XMEMCPY(&testVectors[testSz++], &_this_vector, \
  42413. sizeof _this_vector); \
  42414. }
  42415. pkcs7AuthEnvelopedVector *testVectors = NULL;
  42416. XMEMSET(&rng, 0, sizeof(rng));
  42417. testVectors = (pkcs7AuthEnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  42418. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42419. if (testVectors == NULL) {
  42420. ret = WC_TEST_RET_ENC_ERRNO;
  42421. goto out;
  42422. }
  42423. {
  42424. /* key transport key encryption technique */
  42425. #ifndef NO_RSA
  42426. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  42427. #ifdef WOLFSSL_AES_128
  42428. ADD_PKCS7AUTHENVELOPEDVECTOR(
  42429. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, rsaCert, rsaCertSz,
  42430. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  42431. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  42432. 0, 0, "pkcs7authEnvelopedDataAES128GCM.der");
  42433. #endif
  42434. #ifdef WOLFSSL_AES_192
  42435. ADD_PKCS7AUTHENVELOPEDVECTOR(
  42436. data, (word32)sizeof(data), DATA, AES192GCMb, 0, 0, rsaCert, rsaCertSz,
  42437. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  42438. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  42439. 0, 0, "pkcs7authEnvelopedDataAES192GCM.der");
  42440. #endif
  42441. #ifdef WOLFSSL_AES_256
  42442. ADD_PKCS7AUTHENVELOPEDVECTOR(
  42443. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  42444. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  42445. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  42446. 0, 0, "pkcs7authEnvelopedDataAES256GCM.der");
  42447. /* test with contentType set to FirmwarePkgData */
  42448. ADD_PKCS7AUTHENVELOPEDVECTOR(
  42449. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, 0, 0,
  42450. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL,
  42451. 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL,
  42452. 0, 0, 0, 0, 0, 0, 0, 0,
  42453. "pkcs7authEnvelopedDataAES256GCM_firmwarePkgData.der");
  42454. /* explicitly using SKID for SubjectKeyIdentifier */
  42455. ADD_PKCS7AUTHENVELOPEDVECTOR(
  42456. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  42457. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, CMS_SKID, 0,
  42458. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  42459. 0, 0, 0, 0, 0, "pkcs7authEnvelopedDataAES256GCM_SKID.der");
  42460. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  42461. ADD_PKCS7AUTHENVELOPEDVECTOR(
  42462. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  42463. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0,
  42464. CMS_ISSUER_AND_SERIAL_NUMBER, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  42465. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  42466. "pkcs7authEnvelopedDataAES256GCM_IANDS.der");
  42467. #endif
  42468. #else /* NO_AES || !HAVE_AESGCM */
  42469. (void)rsaCert;
  42470. (void)rsaCertSz;
  42471. (void)rsaPrivKey;
  42472. (void)rsaPrivKeySz;
  42473. #endif /* NO_AES || !HAVE_AESGCM */
  42474. #endif
  42475. /* key agreement key encryption technique*/
  42476. #ifdef HAVE_ECC
  42477. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  42478. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  42479. ADD_PKCS7AUTHENVELOPEDVECTOR(
  42480. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP,
  42481. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  42482. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  42483. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  42484. "pkcs7authEnvelopedDataAES128GCM_ECDH_SHA1KDF.der");
  42485. #endif
  42486. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  42487. ADD_PKCS7AUTHENVELOPEDVECTOR(
  42488. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  42489. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  42490. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  42491. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  42492. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF.der");
  42493. /* with authenticated attributes */
  42494. ADD_PKCS7AUTHENVELOPEDVECTOR(
  42495. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  42496. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  42497. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  42498. NULL, 0, NULL, 0, 0, 0, NULL, 0,
  42499. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  42500. 0, 0, 0,
  42501. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_authAttribs.der");
  42502. /* with unauthenticated attributes */
  42503. ADD_PKCS7AUTHENVELOPEDVECTOR(
  42504. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  42505. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  42506. eccPrivKeySz, NULL, 0, attribs,
  42507. (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0, NULL, 0,
  42508. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  42509. 0, 0, 0,
  42510. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_unauthAttribs.der");
  42511. /* with authenticated AND unauthenticated attributes */
  42512. ADD_PKCS7AUTHENVELOPEDVECTOR(
  42513. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  42514. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  42515. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  42516. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  42517. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  42518. 0, 0, 0, 0, 0, 0,
  42519. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_bothAttribs.der");
  42520. /* with authenticated AND unauthenticated attributes AND
  42521. * contentType of FirmwarePkgData */
  42522. ADD_PKCS7AUTHENVELOPEDVECTOR(
  42523. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, AES256_WRAP,
  42524. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  42525. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  42526. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  42527. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  42528. 0, 0, 0, 0, 0, 0,
  42529. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_fw_bothAttribs.der");
  42530. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  42531. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  42532. ADD_PKCS7AUTHENVELOPEDVECTOR(
  42533. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  42534. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  42535. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL,
  42536. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  42537. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF.der");
  42538. /* with optional user keying material (ukm) */
  42539. ADD_PKCS7AUTHENVELOPEDVECTOR(
  42540. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  42541. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  42542. eccPrivKeySz, NULL, 0, NULL, 0, (byte *)optionalUkm, sizeof(optionalUkm), 0,
  42543. 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  42544. 0, 0, 0, 0, 0, 0,
  42545. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF_ukm.der");
  42546. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  42547. #endif /* NO_AES */
  42548. #endif
  42549. /* kekri (KEKRecipientInfo) recipient types */
  42550. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  42551. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  42552. ADD_PKCS7AUTHENVELOPEDVECTOR(
  42553. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP, 0,
  42554. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0,
  42555. (byte *)secretKey, sizeof(secretKey), (byte *)secretKeyId, sizeof(secretKeyId),
  42556. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  42557. "pkcs7authEnvelopedDataAES128GCM_KEKRI.der");
  42558. #endif
  42559. #endif
  42560. /* pwri (PasswordRecipientInfo) recipient types */
  42561. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM)
  42562. #if !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  42563. ADD_PKCS7AUTHENVELOPEDVECTOR(
  42564. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0,
  42565. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  42566. NULL, 0, NULL, NULL, 0, NULL, 0, 0, (char *)password,
  42567. (word32)XSTRLEN(password), (byte *)salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  42568. AES128CBCb, 0, 0, 0, "pkcs7authEnvelopedDataAES128GCM_PWRI.der");
  42569. #endif
  42570. #endif
  42571. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  42572. #ifdef WOLFSSL_AES_128
  42573. /* ori (OtherRecipientInfo) recipient types */
  42574. ADD_PKCS7AUTHENVELOPEDVECTOR(
  42575. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, NULL, 0, NULL, 0,
  42576. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  42577. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 1, 0,
  42578. "pkcs7authEnvelopedDataAES128GCM_ORI.der");
  42579. #endif
  42580. #endif
  42581. }
  42582. #undef MAX_TESTVECTORS_LEN
  42583. #undef ADD_PKCS7AUTHENVELOPEDVECTOR
  42584. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42585. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42586. if ((! enveloped) || (! decoded)) {
  42587. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42588. }
  42589. /* generate senderNonce */
  42590. {
  42591. #ifndef HAVE_FIPS
  42592. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  42593. #else
  42594. ret = wc_InitRng(&rng);
  42595. #endif
  42596. if (ret != 0)
  42597. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42598. senderNonce[0] = 0x04;
  42599. senderNonce[1] = PKCS7_NONCE_SZ;
  42600. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  42601. if (ret != 0) {
  42602. wc_FreeRng(&rng);
  42603. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42604. }
  42605. }
  42606. for (i = 0; i < testSz; i++) {
  42607. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  42608. #ifdef WOLFSSL_ASYNC_CRYPT
  42609. INVALID_DEVID /* async PKCS7 is not supported */
  42610. #else
  42611. devId
  42612. #endif
  42613. );
  42614. if (pkcs7 == NULL) {
  42615. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42616. }
  42617. if (testVectors[i].secretKey != NULL) {
  42618. /* KEKRI recipient type */
  42619. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  42620. if (ret != 0)
  42621. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42622. pkcs7->content = (byte*)testVectors[i].content;
  42623. pkcs7->contentSz = testVectors[i].contentSz;
  42624. pkcs7->contentOID = testVectors[i].contentOID;
  42625. pkcs7->encryptOID = testVectors[i].encryptOID;
  42626. pkcs7->ukm = testVectors[i].optionalUkm;
  42627. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  42628. pkcs7->authAttribs = testVectors[i].authAttribs;
  42629. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  42630. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  42631. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  42632. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  42633. testVectors[i].secretKey, testVectors[i].secretKeySz,
  42634. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  42635. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  42636. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  42637. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  42638. if (ret < 0) {
  42639. wc_PKCS7_Free(pkcs7);
  42640. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42641. }
  42642. /* set key, for decryption */
  42643. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  42644. testVectors[i].secretKeySz);
  42645. if (ret != 0) {
  42646. wc_PKCS7_Free(pkcs7);
  42647. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42648. }
  42649. } else if (testVectors[i].password != NULL) {
  42650. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  42651. /* PWRI recipient type */
  42652. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  42653. if (ret != 0)
  42654. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42655. pkcs7->content = (byte*)testVectors[i].content;
  42656. pkcs7->contentSz = testVectors[i].contentSz;
  42657. pkcs7->contentOID = testVectors[i].contentOID;
  42658. pkcs7->encryptOID = testVectors[i].encryptOID;
  42659. pkcs7->ukm = testVectors[i].optionalUkm;
  42660. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  42661. pkcs7->authAttribs = testVectors[i].authAttribs;
  42662. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  42663. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  42664. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  42665. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  42666. (byte*)testVectors[i].password,
  42667. testVectors[i].passwordSz, testVectors[i].salt,
  42668. testVectors[i].saltSz, testVectors[i].kdfOID,
  42669. testVectors[i].hashOID, testVectors[i].kdfIterations,
  42670. testVectors[i].kekEncryptOID, testVectors[i].pwriOptions);
  42671. if (ret < 0) {
  42672. wc_PKCS7_Free(pkcs7);
  42673. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42674. }
  42675. /* set password, for decryption */
  42676. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  42677. testVectors[i].passwordSz);
  42678. if (ret < 0) {
  42679. wc_PKCS7_Free(pkcs7);
  42680. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42681. }
  42682. #endif /* ! NO_PWDBASED && ! NO_SHA */
  42683. } else if (testVectors[i].isOri == 1) {
  42684. /* ORI recipient type */
  42685. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  42686. if (ret != 0)
  42687. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42688. pkcs7->content = (byte*)testVectors[i].content;
  42689. pkcs7->contentSz = testVectors[i].contentSz;
  42690. pkcs7->contentOID = testVectors[i].contentOID;
  42691. pkcs7->encryptOID = testVectors[i].encryptOID;
  42692. pkcs7->authAttribs = testVectors[i].authAttribs;
  42693. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  42694. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  42695. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  42696. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  42697. testVectors[i].oriOptions);
  42698. if (ret < 0) {
  42699. wc_PKCS7_Free(pkcs7);
  42700. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42701. }
  42702. /* set decrypt callback for decryption */
  42703. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  42704. if (ret < 0) {
  42705. wc_PKCS7_Free(pkcs7);
  42706. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42707. }
  42708. } else {
  42709. /* KTRI or KARI recipient types */
  42710. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  42711. (word32)testVectors[i].certSz);
  42712. if (ret != 0) {
  42713. wc_PKCS7_Free(pkcs7);
  42714. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42715. }
  42716. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  42717. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  42718. pkcs7->privateKey = testVectors[i].privateKey;
  42719. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  42720. pkcs7->content = (byte*)testVectors[i].content;
  42721. pkcs7->contentSz = testVectors[i].contentSz;
  42722. pkcs7->contentOID = testVectors[i].contentOID;
  42723. pkcs7->encryptOID = testVectors[i].encryptOID;
  42724. pkcs7->ukm = testVectors[i].optionalUkm;
  42725. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  42726. pkcs7->authAttribs = testVectors[i].authAttribs;
  42727. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  42728. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  42729. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  42730. /* set SubjectIdentifier type for KTRI types */
  42731. if (testVectors[i].ktriOptions & CMS_SKID) {
  42732. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  42733. if (ret != 0) {
  42734. wc_PKCS7_Free(pkcs7);
  42735. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42736. }
  42737. } else if (testVectors[i].ktriOptions &
  42738. CMS_ISSUER_AND_SERIAL_NUMBER) {
  42739. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  42740. CMS_ISSUER_AND_SERIAL_NUMBER);
  42741. if (ret != 0) {
  42742. wc_PKCS7_Free(pkcs7);
  42743. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42744. }
  42745. }
  42746. }
  42747. #ifdef ECC_TIMING_RESISTANT
  42748. pkcs7->rng = &rng;
  42749. #endif
  42750. /* encode envelopedData */
  42751. envelopedSz = wc_PKCS7_EncodeAuthEnvelopedData(pkcs7, enveloped,
  42752. PKCS7_BUF_SIZE);
  42753. if (envelopedSz <= 0) {
  42754. wc_PKCS7_Free(pkcs7);
  42755. ERROR_OUT(WC_TEST_RET_ENC_EC(envelopedSz), out);
  42756. }
  42757. #ifndef NO_PKCS7_STREAM
  42758. { /* test reading byte by byte */
  42759. int z;
  42760. for (z = 0; z < envelopedSz; z++) {
  42761. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7,
  42762. enveloped + z, 1, decoded, PKCS7_BUF_SIZE);
  42763. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  42764. printf("unexpected error %d\n", decodedSz);
  42765. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  42766. }
  42767. }
  42768. /* test decode result */
  42769. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  42770. printf("stream read compare failed\n");
  42771. wc_PKCS7_Free(pkcs7);
  42772. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42773. }
  42774. }
  42775. #endif
  42776. /* decode envelopedData */
  42777. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7, enveloped,
  42778. (word32)envelopedSz, decoded,
  42779. PKCS7_BUF_SIZE);
  42780. if (decodedSz <= 0) {
  42781. wc_PKCS7_Free(pkcs7);
  42782. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  42783. }
  42784. /* test decode result */
  42785. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  42786. wc_PKCS7_Free(pkcs7);
  42787. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42788. }
  42789. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  42790. /* output pkcs7 envelopedData for external testing */
  42791. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  42792. if (!pkcs7File) {
  42793. wc_PKCS7_Free(pkcs7);
  42794. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42795. }
  42796. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  42797. XFCLOSE(pkcs7File);
  42798. if (ret != envelopedSz) {
  42799. wc_PKCS7_Free(pkcs7);
  42800. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42801. } else {
  42802. /* reset ret to 0 for success */
  42803. ret = 0;
  42804. }
  42805. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  42806. wc_PKCS7_Free(pkcs7);
  42807. pkcs7 = NULL;
  42808. }
  42809. wc_FreeRng(&rng);
  42810. (void)eccCert;
  42811. (void)eccCertSz;
  42812. (void)eccPrivKey;
  42813. (void)eccPrivKeySz;
  42814. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  42815. (void)secretKey;
  42816. (void)secretKeyId;
  42817. #endif
  42818. #ifdef NO_RSA
  42819. (void)rsaCert;
  42820. (void)rsaCertSz;
  42821. (void)rsaPrivKey;
  42822. (void)rsaPrivKeySz;
  42823. #endif
  42824. out:
  42825. if (testVectors)
  42826. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42827. if (enveloped)
  42828. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42829. if (decoded)
  42830. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42831. return ret;
  42832. }
  42833. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7authenveloped_test(void)
  42834. {
  42835. wc_test_ret_t ret = 0;
  42836. byte* rsaCert = NULL;
  42837. byte* rsaPrivKey = NULL;
  42838. word32 rsaCertSz = 0;
  42839. word32 rsaPrivKeySz = 0;
  42840. byte* eccCert = NULL;
  42841. byte* eccPrivKey = NULL;
  42842. word32 eccCertSz = 0;
  42843. word32 eccPrivKeySz = 0;
  42844. WOLFSSL_ENTER("pkcs7authenveloped_test");
  42845. #ifndef NO_RSA
  42846. /* read client RSA cert and key in DER format */
  42847. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42848. if (rsaCert == NULL)
  42849. return WC_TEST_RET_ENC_ERRNO;
  42850. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42851. if (rsaPrivKey == NULL) {
  42852. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42853. return WC_TEST_RET_ENC_NC;
  42854. }
  42855. rsaCertSz = FOURK_BUF;
  42856. rsaPrivKeySz = FOURK_BUF;
  42857. #endif /* NO_RSA */
  42858. #ifdef HAVE_ECC
  42859. /* read client ECC cert and key in DER format */
  42860. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42861. if (eccCert == NULL) {
  42862. #ifndef NO_RSA
  42863. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42864. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42865. #endif
  42866. return WC_TEST_RET_ENC_NC;
  42867. }
  42868. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42869. if (eccPrivKey == NULL) {
  42870. #ifndef NO_RSA
  42871. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42872. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42873. #endif
  42874. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42875. return WC_TEST_RET_ENC_NC;
  42876. }
  42877. eccCertSz = FOURK_BUF;
  42878. eccPrivKeySz = FOURK_BUF;
  42879. #endif /* HAVE_ECC */
  42880. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  42881. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  42882. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  42883. eccPrivKey, &eccPrivKeySz);
  42884. if (ret < 0) {
  42885. #ifndef NO_RSA
  42886. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42887. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42888. #endif
  42889. #ifdef HAVE_ECC
  42890. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42891. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42892. #endif
  42893. return WC_TEST_RET_ENC_EC(ret);
  42894. }
  42895. ret = pkcs7authenveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  42896. rsaPrivKey, (word32)rsaPrivKeySz,
  42897. eccCert, (word32)eccCertSz,
  42898. eccPrivKey, (word32)eccPrivKeySz);
  42899. #ifndef NO_RSA
  42900. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42901. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42902. #endif
  42903. #ifdef HAVE_ECC
  42904. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42905. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42906. #endif
  42907. return ret;
  42908. }
  42909. #endif /* HAVE_AESGCM || HAVE_AESCCM */
  42910. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  42911. static const byte p7DefKey[] = {
  42912. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  42913. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  42914. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  42915. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  42916. };
  42917. static const byte p7AltKey[] = {
  42918. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  42919. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  42920. };
  42921. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  42922. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  42923. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  42924. {
  42925. wc_test_ret_t ret;
  42926. if (cek == NULL || out == NULL)
  42927. return BAD_FUNC_ARG;
  42928. /* test case sanity checks */
  42929. if (keyIdSz != 1) {
  42930. return WC_TEST_RET_ENC_NC;
  42931. }
  42932. if (keyId[0] != 0x00) {
  42933. return WC_TEST_RET_ENC_NC;
  42934. }
  42935. if (type != (int)PKCS7_KEKRI) {
  42936. return WC_TEST_RET_ENC_NC;
  42937. }
  42938. switch (keyWrapAlgo) {
  42939. case AES256_WRAP:
  42940. ret = wc_AesKeyUnWrap(p7DefKey, sizeof(p7DefKey), cek, cekSz,
  42941. out, outSz, NULL);
  42942. if (ret <= 0)
  42943. return (int)ret;
  42944. break;
  42945. default:
  42946. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  42947. return BAD_KEYWRAP_ALG_E;
  42948. };
  42949. (void)pkcs7;
  42950. (void)direction;
  42951. (void)orginKey; /* used with KAKRI */
  42952. (void)orginKeySz;
  42953. return (int)ret;
  42954. }
  42955. /* returns key size on success */
  42956. static wc_test_ret_t getFirmwareKey(PKCS7* pkcs7, byte* key, word32 keySz)
  42957. {
  42958. wc_test_ret_t ret;
  42959. word32 atrSz;
  42960. byte atr[256];
  42961. /* Additionally can look for fwWrappedFirmwareKey
  42962. * 1.2.840.113529.1.9.16.1.16 */
  42963. const unsigned char fwWrappedFirmwareKey[] = {
  42964. /* 0x06, 0x0B */
  42965. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  42966. 0x01, 0x09, 0x10, 0x02, 0x27
  42967. };
  42968. /* find keyID in fwWrappedFirmwareKey */
  42969. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  42970. sizeof(fwWrappedFirmwareKey), NULL, &atrSz);
  42971. if (ret == LENGTH_ONLY_E) {
  42972. XMEMSET(atr, 0, sizeof(atr));
  42973. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  42974. sizeof(fwWrappedFirmwareKey), atr, &atrSz);
  42975. /* keyIdRaw[0] OCTET TAG */
  42976. /* keyIdRaw[1] Length */
  42977. if (ret > 0) {
  42978. PKCS7* envPkcs7;
  42979. envPkcs7 = wc_PKCS7_New(NULL, 0);
  42980. if (envPkcs7 == NULL) {
  42981. return MEMORY_E;
  42982. }
  42983. wc_PKCS7_Init(envPkcs7, NULL, 0);
  42984. ret = wc_PKCS7_SetWrapCEKCb(envPkcs7, myCEKwrapFunc);
  42985. if (ret == 0) {
  42986. /* expecting FIRMWARE_PKG_DATA content */
  42987. envPkcs7->contentOID = FIRMWARE_PKG_DATA;
  42988. ret = wc_PKCS7_DecodeEnvelopedData(envPkcs7, atr, atrSz,
  42989. key, keySz);
  42990. if (envPkcs7->contentOID != FIRMWARE_PKG_DATA) {
  42991. /* the contentOID should have been set to the inner
  42992. * FIRMWARE_PKG_DATA content */
  42993. ret = BAD_STATE_E;
  42994. }
  42995. }
  42996. wc_PKCS7_Free(envPkcs7);
  42997. }
  42998. }
  42999. return ret;
  43000. }
  43001. /* create a KEKRI enveloped data
  43002. * return size on success */
  43003. static wc_test_ret_t envelopedData_encrypt(byte* in, word32 inSz, byte* out,
  43004. word32 outSz)
  43005. {
  43006. wc_test_ret_t ret;
  43007. PKCS7* pkcs7;
  43008. WOLFSSL_SMALL_STACK_STATIC const byte keyId[] = { 0x00 };
  43009. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  43010. if (pkcs7 == NULL)
  43011. return WC_TEST_RET_ENC_ERRNO;
  43012. pkcs7->content = in;
  43013. pkcs7->contentSz = inSz;
  43014. pkcs7->contentOID = FIRMWARE_PKG_DATA;
  43015. pkcs7->encryptOID = AES256CBCb;
  43016. pkcs7->ukm = NULL;
  43017. pkcs7->ukmSz = 0;
  43018. /* add recipient (KEKRI type) */
  43019. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP, (byte*)p7DefKey,
  43020. sizeof(p7DefKey), (byte*)keyId,
  43021. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0);
  43022. if (ret < 0) {
  43023. printf("wc_PKCS7_AddRecipient_KEKRI() failed\n");
  43024. wc_PKCS7_Free(pkcs7);
  43025. return WC_TEST_RET_ENC_EC(ret);
  43026. }
  43027. /* encode envelopedData, returns size */
  43028. ret = wc_PKCS7_EncodeEnvelopedData(pkcs7, out, outSz);
  43029. if (ret <= 0) {
  43030. printf("wc_PKCS7_EncodeEnvelopedData() failed\n");
  43031. wc_PKCS7_Free(pkcs7);
  43032. return WC_TEST_RET_ENC_EC(ret);
  43033. }
  43034. wc_PKCS7_Free(pkcs7);
  43035. return ret;
  43036. }
  43037. /*
  43038. * keyHint is the KeyID to be set in the fwDecryptKeyID attribute
  43039. * returns size of buffer output on success
  43040. */
  43041. static wc_test_ret_t generateBundle(byte* out, word32 *outSz, const byte* encryptKey,
  43042. word32 encryptKeySz, byte keyHint, byte* cert, word32 certSz,
  43043. byte* key, word32 keySz)
  43044. {
  43045. wc_test_ret_t ret;
  43046. int attribNum = 1;
  43047. PKCS7* pkcs7;
  43048. /* KEY ID
  43049. * fwDecryptKeyID OID 1.2.840.113549.1.9.16.2.37
  43050. */
  43051. const unsigned char fwDecryptKeyID[] = {
  43052. 0x06, 0x0B,
  43053. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  43054. 0x01, 0x09, 0x10, 0x02, 0x25
  43055. };
  43056. /* fwWrappedFirmwareKey 1.2.840.113529.1.9.16.1.16 */
  43057. const unsigned char fwWrappedFirmwareKey[] = {
  43058. 0x06, 0x0B, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  43059. 0x01, 0x09, 0x10, 0x02, 0x27
  43060. };
  43061. byte keyID[] = { 0x04, 0x01, 0x00 };
  43062. byte env[256];
  43063. char data[] = "Test of wolfSSL PKCS7 decrypt callback";
  43064. PKCS7Attrib attribs[] =
  43065. {
  43066. { fwDecryptKeyID, sizeof(fwDecryptKeyID), keyID, sizeof(keyID) },
  43067. { fwWrappedFirmwareKey, sizeof(fwWrappedFirmwareKey), env, 0 }
  43068. };
  43069. keyID[2] = keyHint;
  43070. /* If using keyHint 0 then create a bundle with fwWrappedFirmwareKey */
  43071. if (keyHint == 0) {
  43072. ret = envelopedData_encrypt((byte*)p7DefKey, sizeof(p7DefKey), env,
  43073. sizeof(env));
  43074. if (ret <= 0) {
  43075. return ret;
  43076. }
  43077. attribs[1].valueSz = (word32)ret;
  43078. attribNum++;
  43079. }
  43080. /* init PKCS7 */
  43081. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  43082. if (pkcs7 == NULL)
  43083. return WC_TEST_RET_ENC_ERRNO;
  43084. ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
  43085. if (ret != 0) {
  43086. printf("ERROR: wc_PKCS7_InitWithCert() failed, ret = %d\n", ret);
  43087. wc_PKCS7_Free(pkcs7);
  43088. return WC_TEST_RET_ENC_EC(ret);
  43089. }
  43090. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  43091. if (ret != 0) {
  43092. wc_PKCS7_Free(pkcs7);
  43093. return WC_TEST_RET_ENC_EC(ret);
  43094. }
  43095. /* encode Signed Encrypted FirmwarePkgData */
  43096. if (encryptKeySz == 16) {
  43097. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  43098. encryptKeySz, key, keySz, AES128CBCb, RSAk, SHA256h,
  43099. (byte*)data, sizeof(data), NULL, 0,
  43100. attribs, (word32)attribNum, out, *outSz);
  43101. }
  43102. else {
  43103. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  43104. encryptKeySz, key, keySz, AES256CBCb, RSAk, SHA256h,
  43105. (byte*)data, sizeof(data), NULL, 0,
  43106. attribs, (word32)attribNum, out, *outSz);
  43107. }
  43108. if (ret <= 0) {
  43109. printf("ERROR: wc_PKCS7_EncodeSignedEncryptedFPD() failed, "
  43110. "ret = %d\n", ret);
  43111. wc_PKCS7_Free(pkcs7);
  43112. return WC_TEST_RET_ENC_EC(ret);
  43113. } else {
  43114. *outSz = (word32)ret;
  43115. }
  43116. wc_PKCS7_Free(pkcs7);
  43117. return ret;
  43118. }
  43119. /* test verification and decryption of PKCS7 bundle
  43120. * return 0 on success
  43121. */
  43122. static wc_test_ret_t verifyBundle(byte* derBuf, word32 derSz, int keyHint)
  43123. {
  43124. wc_test_ret_t ret = 0;
  43125. int usrCtx = 1; /* test value to pass as user context to callback */
  43126. PKCS7* pkcs7 = NULL;
  43127. byte* sid = NULL;
  43128. word32 sidSz;
  43129. byte key[256];
  43130. word32 keySz = sizeof(key);
  43131. byte *decoded = NULL;
  43132. int decodedSz = FOURK_BUF/2;
  43133. WOLFSSL_SMALL_STACK_STATIC const byte expectedSid[] = {
  43134. #ifdef NO_SHA
  43135. #ifdef USE_CERT_BUFFERS_1024
  43136. 0x70, 0xe7, 0x79, 0x60, 0x8f, 0x41, 0xdc, 0xe9,
  43137. 0xad, 0x8b, 0x3d, 0x0c, 0x20, 0xf4, 0xc3, 0xf2,
  43138. 0x8e, 0x05, 0xe8, 0xa1, 0xb6, 0x68, 0x74, 0x06,
  43139. 0xbc, 0xe7, 0xc5, 0x3c, 0x13, 0x99, 0x79, 0xb9
  43140. #else
  43141. 0xce, 0x06, 0x07, 0xbe, 0xf1, 0xa6, 0x1e, 0x36,
  43142. 0xef, 0xfa, 0xbc, 0x89, 0x71, 0xf3, 0x23, 0x9e,
  43143. 0x34, 0x6d, 0xae, 0x86, 0xae, 0x2b, 0xdc, 0xf4,
  43144. 0x4a, 0x27, 0xd5, 0x63, 0x59, 0x4f, 0x4a, 0x71
  43145. #endif
  43146. #else /* !NO_SHA */
  43147. #ifdef USE_CERT_BUFFERS_1024
  43148. 0x81, 0x69, 0x0f, 0xf8, 0xdf, 0xdd, 0xcf, 0x34,
  43149. 0x29, 0xd5, 0x67, 0x75, 0x71, 0x85, 0xc7, 0x75,
  43150. 0x10, 0x69, 0x59, 0xec,
  43151. #else
  43152. 0x33, 0xD8, 0x45, 0x66, 0xD7, 0x68, 0x87, 0x18,
  43153. 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26,
  43154. 0xD7, 0x85, 0x65, 0xC0
  43155. #endif
  43156. #endif /* !NO_SHA */
  43157. };
  43158. decoded = (byte *)XMALLOC(decodedSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43159. if (decoded == NULL) {
  43160. ret = MEMORY_E;
  43161. goto out;
  43162. }
  43163. pkcs7 = wc_PKCS7_New(HEAP_HINT, INVALID_DEVID);
  43164. if (pkcs7 == NULL) {
  43165. ret = MEMORY_E;
  43166. goto out;
  43167. }
  43168. /* Test verify */
  43169. ret = wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID);
  43170. if (ret != 0)
  43171. goto out;
  43172. ret = wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  43173. if (ret != 0)
  43174. goto out;
  43175. ret = wc_PKCS7_VerifySignedData(pkcs7, derBuf, derSz);
  43176. if (ret != 0)
  43177. goto out;
  43178. /* Get size of SID and print it out */
  43179. ret = wc_PKCS7_GetSignerSID(pkcs7, NULL, &sidSz);
  43180. if (ret != LENGTH_ONLY_E)
  43181. goto out;
  43182. sid = (byte*)XMALLOC(sidSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43183. if (sid == NULL) {
  43184. ret = MEMORY_E;
  43185. goto out;
  43186. }
  43187. ret = wc_PKCS7_GetSignerSID(pkcs7, sid, &sidSz);
  43188. if (ret != 0)
  43189. goto out;
  43190. ret = XMEMCMP(sid, expectedSid, sidSz);
  43191. if (ret != 0) {
  43192. ret = PKCS7_NO_SIGNER_E; /* close enough */
  43193. goto out;
  43194. }
  43195. /* get expected fwWrappedFirmwareKey */
  43196. if (keyHint == 0) {
  43197. ret = getFirmwareKey(pkcs7, key, keySz);
  43198. if (ret < 0)
  43199. goto out;
  43200. pkcs7->encryptionKey = key;
  43201. pkcs7->encryptionKeySz = (word32)ret;
  43202. }
  43203. else {
  43204. decodedSz = PKCS7_BUF_SIZE;
  43205. ret = wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc);
  43206. if (ret != 0)
  43207. goto out;
  43208. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)&usrCtx);
  43209. if (ret != 0)
  43210. goto out;
  43211. }
  43212. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  43213. pkcs7->contentSz, decoded, (word32)decodedSz);
  43214. if (decodedSz < 0) {
  43215. ret = decodedSz;
  43216. goto out;
  43217. }
  43218. ret = 0;
  43219. out:
  43220. if (decoded)
  43221. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43222. if (pkcs7)
  43223. wc_PKCS7_Free(pkcs7);
  43224. if (sid)
  43225. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43226. return ret;
  43227. }
  43228. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7callback_test(byte* cert, word32 certSz, byte* key, word32 keySz)
  43229. {
  43230. wc_test_ret_t ret = 0;
  43231. word32 derSz;
  43232. byte *derBuf = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43233. WOLFSSL_ENTER("pkcs7callback_test");
  43234. if (! derBuf)
  43235. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  43236. /* Doing default generation and verify */
  43237. derSz = FOURK_BUF;
  43238. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 0, cert,
  43239. certSz, key, keySz);
  43240. if (ret <= 0) {
  43241. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43242. }
  43243. ret = verifyBundle(derBuf, derSz, 0);
  43244. if (ret != 0)
  43245. ERROR_OUT(ret, out);
  43246. /* test choosing other key with keyID */
  43247. derSz = FOURK_BUF;
  43248. ret = generateBundle(derBuf, &derSz, p7AltKey, sizeof(p7AltKey), 1,
  43249. cert, certSz, key, keySz);
  43250. if (ret <= 0) {
  43251. ERROR_OUT(ret, out);
  43252. }
  43253. ret = verifyBundle(derBuf, derSz, 1);
  43254. if (ret != 0)
  43255. ERROR_OUT(ret, out);
  43256. /* test fail case with wrong keyID */
  43257. derSz = FOURK_BUF;
  43258. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 1,
  43259. cert, certSz, key, keySz);
  43260. if (ret <= 0) {
  43261. ERROR_OUT(ret, out);
  43262. }
  43263. ret = verifyBundle(derBuf, derSz, 1);
  43264. if (ret == 0) {
  43265. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  43266. }
  43267. ret = 0;
  43268. out:
  43269. if (derBuf)
  43270. XFREE(derBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43271. return ret;
  43272. }
  43273. #endif /* !NO_AES && HAVE_AES_CBC */
  43274. #ifndef NO_PKCS7_ENCRYPTED_DATA
  43275. typedef struct {
  43276. const byte* content;
  43277. word32 contentSz;
  43278. int contentOID;
  43279. int encryptOID;
  43280. byte* encryptionKey;
  43281. word32 encryptionKeySz;
  43282. PKCS7Attrib* attribs;
  43283. word32 attribsSz;
  43284. const char* outFileName;
  43285. } pkcs7EncryptedVector;
  43286. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7encrypted_test(void)
  43287. {
  43288. wc_test_ret_t ret = 0;
  43289. int i, testSz;
  43290. int encryptedSz, decodedSz, attribIdx;
  43291. PKCS7* pkcs7;
  43292. byte *encrypted;
  43293. byte *decoded;
  43294. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  43295. XFILE pkcs7File;
  43296. #endif
  43297. PKCS7Attrib* expectedAttrib;
  43298. PKCS7DecodedAttrib* decodedAttrib;
  43299. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  43300. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  43301. 0x72,0x6c,0x64
  43302. };
  43303. #ifndef NO_DES3
  43304. byte desKey[] = {
  43305. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  43306. };
  43307. byte des3Key[] = {
  43308. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  43309. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  43310. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  43311. };
  43312. #endif
  43313. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  43314. #ifdef WOLFSSL_AES_128
  43315. byte aes128Key[] = {
  43316. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  43317. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  43318. };
  43319. #endif
  43320. #ifdef WOLFSSL_AES_192
  43321. byte aes192Key[] = {
  43322. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  43323. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  43324. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  43325. };
  43326. #endif
  43327. #ifdef WOLFSSL_AES_256
  43328. byte aes256Key[] = {
  43329. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  43330. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  43331. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  43332. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  43333. };
  43334. #endif
  43335. #ifdef WOLFSSL_AES_256
  43336. /* Attribute example from RFC 4134, Section 7.2
  43337. * OID = 1.2.5555
  43338. * OCTET STRING = 'This is a test General ASN Attribute, number 1.' */
  43339. static const byte genAttrOid[] = { 0x06, 0x03, 0x2a, 0xab, 0x33 };
  43340. static const byte genAttr[] = { 0x04, 47,
  43341. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  43342. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  43343. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  43344. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  43345. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  43346. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x31, 0x2e };
  43347. static const byte genAttrOid2[] = { 0x06, 0x03, 0x2a, 0xab, 0x34 };
  43348. static const byte genAttr2[] = { 0x04, 47,
  43349. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  43350. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  43351. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  43352. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  43353. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  43354. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x32, 0x2e };
  43355. PKCS7Attrib attribs[] =
  43356. {
  43357. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) }
  43358. };
  43359. PKCS7Attrib multiAttribs[] =
  43360. {
  43361. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) },
  43362. { genAttrOid2, sizeof(genAttrOid2), genAttr2, sizeof(genAttr2) }
  43363. };
  43364. #endif
  43365. #endif /* NO_AES */
  43366. const pkcs7EncryptedVector testVectors[] =
  43367. {
  43368. #ifndef NO_DES3
  43369. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key),
  43370. NULL, 0, "pkcs7encryptedDataDES3.der"},
  43371. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey),
  43372. NULL, 0, "pkcs7encryptedDataDES.der"},
  43373. #endif /* NO_DES3 */
  43374. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  43375. #ifdef WOLFSSL_AES_128
  43376. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  43377. sizeof(aes128Key), NULL, 0, "pkcs7encryptedDataAES128CBC.der"},
  43378. #endif
  43379. #ifdef WOLFSSL_AES_192
  43380. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  43381. sizeof(aes192Key), NULL, 0, "pkcs7encryptedDataAES192CBC.der"},
  43382. #endif
  43383. #ifdef WOLFSSL_AES_256
  43384. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  43385. sizeof(aes256Key), NULL, 0, "pkcs7encryptedDataAES256CBC.der"},
  43386. /* test with optional unprotected attributes */
  43387. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  43388. sizeof(aes256Key), attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  43389. "pkcs7encryptedDataAES256CBC_attribs.der"},
  43390. /* test with multiple optional unprotected attributes */
  43391. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  43392. sizeof(aes256Key), multiAttribs,
  43393. (sizeof(multiAttribs)/sizeof(PKCS7Attrib)),
  43394. "pkcs7encryptedDataAES256CBC_multi_attribs.der"},
  43395. /* test with contentType set to FirmwarePkgData */
  43396. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256CBCb, aes256Key,
  43397. sizeof(aes256Key), NULL, 0,
  43398. "pkcs7encryptedDataAES256CBC_firmwarePkgData.der"},
  43399. #endif
  43400. #endif /* !NO_AES && HAVE_AES_CBC */
  43401. };
  43402. WOLFSSL_ENTER("pkcs7encrypted_test");
  43403. encrypted = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43404. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43405. if ((! encrypted) || (! decoded)) {
  43406. ERROR_OUT(MEMORY_E, out);
  43407. }
  43408. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  43409. for (i = 0; i < testSz; i++) {
  43410. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  43411. if (pkcs7 == NULL) {
  43412. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  43413. }
  43414. pkcs7->content = (byte*)testVectors[i].content;
  43415. pkcs7->contentSz = testVectors[i].contentSz;
  43416. pkcs7->contentOID = testVectors[i].contentOID;
  43417. pkcs7->encryptOID = testVectors[i].encryptOID;
  43418. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  43419. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  43420. pkcs7->unprotectedAttribs = testVectors[i].attribs;
  43421. pkcs7->unprotectedAttribsSz = testVectors[i].attribsSz;
  43422. /* encode encryptedData */
  43423. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  43424. PKCS7_BUF_SIZE);
  43425. if (encryptedSz <= 0) {
  43426. wc_PKCS7_Free(pkcs7);
  43427. ERROR_OUT(WC_TEST_RET_ENC_EC(encryptedSz), out);
  43428. }
  43429. /* decode encryptedData */
  43430. #ifndef NO_PKCS7_STREAM
  43431. { /* test reading byte by byte */
  43432. int z;
  43433. for (z = 0; z < encryptedSz; z++) {
  43434. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted + z, 1,
  43435. decoded, PKCS7_BUF_SIZE);
  43436. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  43437. printf("unexpected error %d\n", decodedSz);
  43438. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  43439. }
  43440. }
  43441. /* test decode result */
  43442. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  43443. printf("stream read failed\n");
  43444. wc_PKCS7_Free(pkcs7);
  43445. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  43446. }
  43447. }
  43448. #endif
  43449. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  43450. decoded, PKCS7_BUF_SIZE);
  43451. if (decodedSz <= 0){
  43452. wc_PKCS7_Free(pkcs7);
  43453. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  43454. }
  43455. /* test decode result */
  43456. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  43457. wc_PKCS7_Free(pkcs7);
  43458. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  43459. }
  43460. /* verify decoded unprotected attributes */
  43461. if (pkcs7->decodedAttrib != NULL) {
  43462. decodedAttrib = pkcs7->decodedAttrib;
  43463. attribIdx = 1;
  43464. while (decodedAttrib != NULL) {
  43465. /* expected attribute, stored list is reversed */
  43466. expectedAttrib = &(pkcs7->unprotectedAttribs
  43467. [pkcs7->unprotectedAttribsSz - attribIdx]);
  43468. /* verify oid */
  43469. if (XMEMCMP(decodedAttrib->oid, expectedAttrib->oid,
  43470. decodedAttrib->oidSz) != 0) {
  43471. wc_PKCS7_Free(pkcs7);
  43472. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  43473. }
  43474. /* verify value */
  43475. if (XMEMCMP(decodedAttrib->value, expectedAttrib->value,
  43476. decodedAttrib->valueSz) != 0) {
  43477. wc_PKCS7_Free(pkcs7);
  43478. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  43479. }
  43480. decodedAttrib = decodedAttrib->next;
  43481. attribIdx++;
  43482. }
  43483. }
  43484. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  43485. /* output pkcs7 envelopedData for external testing */
  43486. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  43487. if (!pkcs7File) {
  43488. wc_PKCS7_Free(pkcs7);
  43489. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  43490. }
  43491. ret = (int)XFWRITE(encrypted, encryptedSz, 1, pkcs7File);
  43492. if (ret < 0)
  43493. ret = WC_TEST_RET_ENC_ERRNO;
  43494. else
  43495. ret = 0;
  43496. XFCLOSE(pkcs7File);
  43497. #endif
  43498. wc_PKCS7_Free(pkcs7);
  43499. }
  43500. out:
  43501. if (encrypted)
  43502. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43503. if (decoded)
  43504. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43505. return ret;
  43506. }
  43507. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  43508. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  43509. typedef struct {
  43510. const byte* content;
  43511. word32 contentSz;
  43512. int contentOID;
  43513. const char* outFileName;
  43514. } pkcs7CompressedVector;
  43515. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7compressed_test(void)
  43516. {
  43517. wc_test_ret_t ret = 0;
  43518. int i, testSz;
  43519. int compressedSz, decodedSz;
  43520. PKCS7* pkcs7;
  43521. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  43522. byte *compressed;
  43523. byte *decoded;
  43524. #else
  43525. byte compressed[PKCS7_BUF_SIZE];
  43526. byte decoded[PKCS7_BUF_SIZE];
  43527. #endif
  43528. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  43529. XFILE pkcs7File;
  43530. #endif
  43531. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  43532. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  43533. 0x72,0x6c,0x64
  43534. };
  43535. const pkcs7CompressedVector testVectors[] =
  43536. {
  43537. {data, (word32)sizeof(data), DATA,
  43538. "pkcs7compressedData_data_zlib.der"},
  43539. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA,
  43540. "pkcs7compressedData_firmwarePkgData_zlib.der"},
  43541. };
  43542. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  43543. compressed = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43544. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43545. if ((! compressed) || (! decoded)) {
  43546. ERROR_OUT(MEMORY_E, out);
  43547. }
  43548. #endif
  43549. WOLFSSL_ENTER("pkcs7compressed_test");
  43550. testSz = sizeof(testVectors) / sizeof(pkcs7CompressedVector);
  43551. for (i = 0; i < testSz; i++) {
  43552. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  43553. if (pkcs7 == NULL) {
  43554. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  43555. }
  43556. pkcs7->content = (byte*)testVectors[i].content;
  43557. pkcs7->contentSz = testVectors[i].contentSz;
  43558. pkcs7->contentOID = testVectors[i].contentOID;
  43559. /* encode compressedData */
  43560. compressedSz = wc_PKCS7_EncodeCompressedData(pkcs7, compressed,
  43561. PKCS7_BUF_SIZE);
  43562. if (compressedSz <= 0) {
  43563. wc_PKCS7_Free(pkcs7);
  43564. ERROR_OUT(WC_TEST_RET_ENC_EC(compressedSz), out);
  43565. }
  43566. /* decode compressedData */
  43567. decodedSz = wc_PKCS7_DecodeCompressedData(pkcs7, compressed,
  43568. compressedSz, decoded,
  43569. PKCS7_BUF_SIZE);
  43570. if (decodedSz <= 0){
  43571. wc_PKCS7_Free(pkcs7);
  43572. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  43573. }
  43574. /* test decode result */
  43575. if (XMEMCMP(decoded, testVectors[i].content,
  43576. testVectors[i].contentSz) != 0) {
  43577. wc_PKCS7_Free(pkcs7);
  43578. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  43579. }
  43580. /* make sure content type is the same */
  43581. if (testVectors[i].contentOID != pkcs7->contentOID) {
  43582. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  43583. }
  43584. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  43585. /* output pkcs7 compressedData for external testing */
  43586. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  43587. if (!pkcs7File) {
  43588. wc_PKCS7_Free(pkcs7);
  43589. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  43590. }
  43591. ret = (int)XFWRITE(compressed, compressedSz, 1, pkcs7File);
  43592. if (ret < 0)
  43593. ret = WC_TEST_RET_ENC_ERRNO;
  43594. else
  43595. ret = 0;
  43596. XFCLOSE(pkcs7File);
  43597. #endif
  43598. wc_PKCS7_Free(pkcs7);
  43599. }
  43600. out:
  43601. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  43602. if (compressed)
  43603. XFREE(compressed, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43604. if (decoded)
  43605. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43606. #endif
  43607. return ret;
  43608. } /* pkcs7compressed_test() */
  43609. #undef PKCS7_BUF_SIZE
  43610. #endif /* HAVE_LIBZ */
  43611. typedef struct {
  43612. const byte* content;
  43613. word32 contentSz;
  43614. int hashOID;
  43615. int signOID;
  43616. byte* privateKey;
  43617. word32 privateKeySz;
  43618. byte* cert;
  43619. size_t certSz;
  43620. byte* caCert;
  43621. size_t caCertSz;
  43622. PKCS7Attrib* signedAttribs;
  43623. word32 signedAttribsSz;
  43624. const char* outFileName;
  43625. int contentOID;
  43626. const byte* contentType;
  43627. word32 contentTypeSz;
  43628. int sidType;
  43629. int encryptOID; /* for single-shot encrypt alg OID */
  43630. int encCompFlag; /* for single-shot. 1 = enc, 2 = comp, 3 = both*/
  43631. const byte* encryptKey; /* for single-shot, encryptedData */
  43632. word32 encryptKeySz; /* for single-shot, encryptedData */
  43633. PKCS7Attrib* unprotectedAttribs; /* for single-shot, encryptedData */
  43634. word32 unprotectedAttribsSz; /* for single-shot, encryptedData */
  43635. word16 detachedSignature; /* generate detached signature (0:1) */
  43636. } pkcs7SignedVector;
  43637. static wc_test_ret_t pkcs7signed_run_vectors(
  43638. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  43639. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  43640. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  43641. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  43642. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  43643. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  43644. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  43645. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  43646. {
  43647. wc_test_ret_t ret;
  43648. int testSz = 0, i;
  43649. int encodedSz;
  43650. byte* out = NULL;
  43651. word32 outSz;
  43652. WC_RNG rng;
  43653. PKCS7* pkcs7 = NULL;
  43654. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  43655. XFILE file;
  43656. #endif
  43657. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  43658. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  43659. 0x72,0x6c,0x64
  43660. };
  43661. static const byte transIdOid[] =
  43662. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  43663. 0x09, 0x07 };
  43664. static const byte messageTypeOid[] =
  43665. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  43666. 0x09, 0x02 };
  43667. static const byte senderNonceOid[] =
  43668. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  43669. 0x09, 0x05 };
  43670. #ifndef NO_SHA
  43671. byte transId[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  43672. #else
  43673. byte transId[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  43674. #endif
  43675. static const byte messageType[] = { 0x13, 2, '1', '9' };
  43676. byte senderNonce[PKCS7_NONCE_SZ + 2];
  43677. PKCS7Attrib attribs[] =
  43678. {
  43679. { transIdOid, sizeof(transIdOid), transId,
  43680. sizeof(transId) - 1 }, /* take off the null */
  43681. { messageTypeOid, sizeof(messageTypeOid), messageType,
  43682. sizeof(messageType) },
  43683. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  43684. sizeof(senderNonce) }
  43685. };
  43686. /* for testing custom contentType, FirmwarePkgData */
  43687. static const byte customContentType[] = { 0x06, 0x0B, 0x2A, 0x86,
  43688. 0x48, 0x86, 0xF7, 0x0D,
  43689. 0x01, 0x09, 0x10, 0x01, 0x10 };
  43690. #define MAX_TESTVECTORS_LEN 20
  43691. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  43692. const pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  43693. if (testSz == MAX_TESTVECTORS_LEN) { \
  43694. ret = WC_TEST_RET_ENC_NC; \
  43695. goto out; \
  43696. } \
  43697. XMEMCPY(&testVectors[testSz++], &_this_vector, \
  43698. sizeof _this_vector); \
  43699. }
  43700. pkcs7SignedVector *testVectors = NULL;
  43701. XMEMSET(&rng, 0, sizeof(rng));
  43702. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  43703. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43704. if (testVectors == NULL) {
  43705. ret = WC_TEST_RET_ENC_ERRNO;
  43706. goto out;
  43707. }
  43708. {
  43709. #ifndef NO_RSA
  43710. #ifndef NO_SHA
  43711. /* RSA with SHA */
  43712. ADD_PKCS7SIGNEDVECTOR(
  43713. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  43714. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  43715. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  43716. "pkcs7signedData_RSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0, NULL,
  43717. 0, 0);
  43718. /* RSA with SHA, no signed attributes */
  43719. ADD_PKCS7SIGNEDVECTOR(
  43720. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  43721. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz,
  43722. NULL, 0, NULL, 0,
  43723. "pkcs7signedData_RSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  43724. NULL, 0, 0);
  43725. #endif
  43726. #ifdef WOLFSSL_SHA224
  43727. /* RSA with SHA224 */
  43728. ADD_PKCS7SIGNEDVECTOR(
  43729. data, (word32)sizeof(data), SHA224h, RSAk, rsaClientPrivKeyBuf,
  43730. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  43731. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  43732. "pkcs7signedData_RSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  43733. NULL, 0, 0);
  43734. #endif
  43735. #ifndef NO_SHA256
  43736. /* RSA with SHA256 */
  43737. ADD_PKCS7SIGNEDVECTOR(
  43738. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  43739. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  43740. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  43741. "pkcs7signedData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  43742. NULL, 0, 0);
  43743. /* RSA with SHA256, detached signature */
  43744. ADD_PKCS7SIGNEDVECTOR(
  43745. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  43746. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  43747. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  43748. "pkcs7signedData_RSA_SHA256_detachedSig.der", 0, NULL, 0, 0, 0, 0,
  43749. NULL, 0, NULL, 0, 1);
  43750. /* RSA with SHA256 and SubjectKeyIdentifier in SignerIdentifier */
  43751. ADD_PKCS7SIGNEDVECTOR(
  43752. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  43753. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  43754. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  43755. "pkcs7signedData_RSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  43756. NULL, 0, NULL, 0, 0);
  43757. /* RSA with SHA256 and custom contentType */
  43758. ADD_PKCS7SIGNEDVECTOR(
  43759. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  43760. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  43761. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  43762. "pkcs7signedData_RSA_SHA256_custom_contentType.der", 0,
  43763. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  43764. NULL, 0, 0);
  43765. /* RSA with SHA256 and FirmwarePkgData contentType */
  43766. ADD_PKCS7SIGNEDVECTOR(
  43767. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  43768. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  43769. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  43770. "pkcs7signedData_RSA_SHA256_firmwarePkgData.der",
  43771. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  43772. /* RSA with SHA256 using server cert and ca cert */
  43773. ADD_PKCS7SIGNEDVECTOR(
  43774. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  43775. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  43776. rsaCaCertBuf, rsaCaCertBufSz,
  43777. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  43778. "pkcs7signedData_RSA_SHA256_with_ca_cert.der", 0, NULL, 0, 0, 0, 0,
  43779. NULL, 0, NULL, 0, 0);
  43780. #endif
  43781. #if defined(WOLFSSL_SHA384)
  43782. /* RSA with SHA384 */
  43783. ADD_PKCS7SIGNEDVECTOR(
  43784. data, (word32)sizeof(data), SHA384h, RSAk, rsaClientPrivKeyBuf,
  43785. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  43786. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  43787. "pkcs7signedData_RSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  43788. NULL, 0, 0);
  43789. #endif
  43790. #if defined(WOLFSSL_SHA512)
  43791. /* RSA with SHA512 */
  43792. ADD_PKCS7SIGNEDVECTOR(
  43793. data, (word32)sizeof(data), SHA512h, RSAk, rsaClientPrivKeyBuf,
  43794. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  43795. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  43796. "pkcs7signedData_RSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  43797. NULL, 0, 0);
  43798. #endif
  43799. #endif /* NO_RSA */
  43800. #ifdef HAVE_ECC
  43801. #ifndef NO_SHA
  43802. /* ECDSA with SHA */
  43803. ADD_PKCS7SIGNEDVECTOR(
  43804. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  43805. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  43806. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  43807. "pkcs7signedData_ECDSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  43808. NULL, 0, 0);
  43809. /* ECDSA with SHA, no signed attributes */
  43810. ADD_PKCS7SIGNEDVECTOR(
  43811. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  43812. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz,
  43813. NULL, 0, NULL, 0,
  43814. "pkcs7signedData_ECDSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  43815. NULL, 0, 0);
  43816. #endif
  43817. #ifdef WOLFSSL_SHA224
  43818. /* ECDSA with SHA224 */
  43819. ADD_PKCS7SIGNEDVECTOR(
  43820. data, (word32)sizeof(data), SHA224h, ECDSAk, eccClientPrivKeyBuf,
  43821. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  43822. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  43823. "pkcs7signedData_ECDSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  43824. NULL, 0, 0);
  43825. #endif
  43826. #ifndef NO_SHA256
  43827. /* ECDSA with SHA256 */
  43828. ADD_PKCS7SIGNEDVECTOR(
  43829. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  43830. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  43831. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  43832. "pkcs7signedData_ECDSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  43833. NULL, 0, 0);
  43834. /* ECDSA with SHA256 and SubjectKeyIdentifier in SigherIdentifier */
  43835. ADD_PKCS7SIGNEDVECTOR(
  43836. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  43837. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  43838. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  43839. "pkcs7signedData_ECDSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  43840. NULL, 0, NULL, 0, 0);
  43841. /* ECDSA with SHA256 and custom contentType */
  43842. ADD_PKCS7SIGNEDVECTOR(
  43843. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  43844. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  43845. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  43846. "pkcs7signedData_ECDSA_SHA256_custom_contentType.der", 0,
  43847. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  43848. NULL, 0, 0);
  43849. /* ECDSA with SHA256 and FirmwarePkgData contentType */
  43850. ADD_PKCS7SIGNEDVECTOR(
  43851. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  43852. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  43853. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  43854. "pkcs7signedData_ECDSA_SHA256_firmwarePkgData.der",
  43855. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  43856. #endif
  43857. #ifdef WOLFSSL_SHA384
  43858. /* ECDSA with SHA384 */
  43859. ADD_PKCS7SIGNEDVECTOR(
  43860. data, (word32)sizeof(data), SHA384h, ECDSAk, eccClientPrivKeyBuf,
  43861. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  43862. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  43863. "pkcs7signedData_ECDSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  43864. NULL, 0, 0);
  43865. #endif
  43866. #ifdef WOLFSSL_SHA512
  43867. /* ECDSA with SHA512 */
  43868. ADD_PKCS7SIGNEDVECTOR(
  43869. data, (word32)sizeof(data), SHA512h, ECDSAk, eccClientPrivKeyBuf,
  43870. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  43871. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  43872. "pkcs7signedData_ECDSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  43873. NULL, 0, 0);
  43874. #endif
  43875. #endif /* HAVE_ECC */
  43876. };
  43877. #undef MAX_TESTVECTORS_LEN
  43878. #undef ADD_PKCS7SIGNEDVECTOR
  43879. outSz = FOURK_BUF;
  43880. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43881. if (out == NULL)
  43882. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  43883. XMEMSET(out, 0, outSz);
  43884. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  43885. if (ret < 0)
  43886. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43887. #ifndef HAVE_FIPS
  43888. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  43889. #else
  43890. ret = wc_InitRng(&rng);
  43891. #endif
  43892. if (ret != 0)
  43893. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43894. for (i = 0; i < testSz; i++) {
  43895. if (pkcs7)
  43896. wc_PKCS7_Free(pkcs7);
  43897. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  43898. if (pkcs7 == NULL)
  43899. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  43900. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  43901. (word32)testVectors[i].certSz);
  43902. if (ret != 0)
  43903. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43904. /* load CA certificate, if present */
  43905. if (testVectors[i].caCert != NULL) {
  43906. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  43907. (word32)testVectors[i].caCertSz);
  43908. if (ret != 0)
  43909. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43910. }
  43911. pkcs7->rng = &rng;
  43912. pkcs7->content = (byte*)testVectors[i].content;
  43913. pkcs7->contentSz = testVectors[i].contentSz;
  43914. pkcs7->contentOID = testVectors[i].contentOID;
  43915. pkcs7->hashOID = testVectors[i].hashOID;
  43916. pkcs7->encryptOID = testVectors[i].signOID;
  43917. pkcs7->privateKey = testVectors[i].privateKey;
  43918. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  43919. pkcs7->signedAttribs = testVectors[i].signedAttribs;
  43920. pkcs7->signedAttribsSz = testVectors[i].signedAttribsSz;
  43921. /* optional custom contentType, default is DATA,
  43922. overrides contentOID if set */
  43923. if (testVectors[i].contentType != NULL) {
  43924. ret = wc_PKCS7_SetContentType(pkcs7,
  43925. (byte *)testVectors[i].contentType,
  43926. testVectors[i].contentTypeSz);
  43927. if (ret != 0)
  43928. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43929. }
  43930. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  43931. default is IssuerAndSerialNumber */
  43932. if (testVectors[i].sidType == CMS_SKID) {
  43933. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  43934. if (ret != 0)
  43935. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43936. }
  43937. /* generate senderNonce */
  43938. {
  43939. senderNonce[0] = 0x04;
  43940. senderNonce[1] = PKCS7_NONCE_SZ;
  43941. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  43942. if (ret != 0)
  43943. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43944. }
  43945. /* generate transactionID (used with SCEP) */
  43946. {
  43947. #ifndef NO_SHA
  43948. wc_Sha sha;
  43949. byte digest[WC_SHA_DIGEST_SIZE];
  43950. #else
  43951. wc_Sha256 sha;
  43952. byte digest[WC_SHA256_DIGEST_SIZE];
  43953. #endif
  43954. int j,k;
  43955. transId[0] = 0x13;
  43956. transId[1] = sizeof(digest) * 2;
  43957. #ifndef NO_SHA
  43958. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  43959. if (ret != 0)
  43960. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43961. wc_ShaUpdate(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  43962. wc_ShaFinal(&sha, digest);
  43963. wc_ShaFree(&sha);
  43964. #else
  43965. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  43966. if (ret != 0)
  43967. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43968. wc_Sha256Update(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  43969. wc_Sha256Final(&sha, digest);
  43970. wc_Sha256Free(&sha);
  43971. #endif
  43972. for (j = 0, k = 2; j < (int)sizeof(digest); j++, k += 2) {
  43973. #if defined(WOLF_C89)
  43974. XSPRINTF((char*)&transId[k], "%02x", digest[j]);
  43975. #else
  43976. (void)XSNPRINTF((char*)&transId[k], 3, "%02x", digest[j]);
  43977. #endif
  43978. }
  43979. }
  43980. /* enable detached signature generation, if set */
  43981. if (testVectors[i].detachedSignature == 1) {
  43982. ret = wc_PKCS7_SetDetached(pkcs7, 1);
  43983. if (ret != 0)
  43984. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43985. }
  43986. encodedSz = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
  43987. if (encodedSz < 0)
  43988. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  43989. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  43990. /* write PKCS#7 to output file for more testing */
  43991. file = XFOPEN(testVectors[i].outFileName, "wb");
  43992. if (!file) {
  43993. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  43994. }
  43995. ret = (int)XFWRITE(out, 1, encodedSz, file);
  43996. XFCLOSE(file);
  43997. if (ret != (int)encodedSz)
  43998. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  43999. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  44000. wc_PKCS7_Free(pkcs7);
  44001. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  44002. if (pkcs7 == NULL)
  44003. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  44004. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  44005. if (testVectors[i].detachedSignature == 1) {
  44006. /* set content for verifying detached signatures */
  44007. pkcs7->content = (byte*)testVectors[i].content;
  44008. pkcs7->contentSz = testVectors[i].contentSz;
  44009. }
  44010. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  44011. if (ret < 0)
  44012. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  44013. /* verify contentType extracted successfully for custom content types */
  44014. if (testVectors[i].contentTypeSz > 0) {
  44015. if (pkcs7->contentTypeSz != testVectors[i].contentTypeSz) {
  44016. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  44017. } else if (XMEMCMP(pkcs7->contentType, testVectors[i].contentType,
  44018. pkcs7->contentTypeSz) != 0) {
  44019. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  44020. }
  44021. }
  44022. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  44023. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  44024. {
  44025. /* check getting signed attributes */
  44026. #ifndef NO_SHA
  44027. byte buf[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  44028. #else
  44029. byte buf[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  44030. #endif
  44031. const byte* oidPt = transIdOid + 2; /* skip object id tag and size */
  44032. int oidSz = (int)sizeof(transIdOid) - 2;
  44033. int bufSz = 0;
  44034. if (testVectors[i].signedAttribs != NULL) {
  44035. ret = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, (word32)oidSz,
  44036. NULL, (word32*)&bufSz);
  44037. if (ret != LENGTH_ONLY_E)
  44038. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  44039. ret = 0;
  44040. }
  44041. if (bufSz > (int)sizeof(buf))
  44042. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  44043. bufSz = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, (word32)oidSz,
  44044. buf, (word32*)&bufSz);
  44045. if ((testVectors[i].signedAttribs != NULL && bufSz < 0) ||
  44046. (testVectors[i].signedAttribs == NULL && bufSz > 0))
  44047. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  44048. }
  44049. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  44050. file = XFOPEN("./pkcs7cert.der", "wb");
  44051. if (!file)
  44052. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  44053. ret = (int)XFWRITE(pkcs7->singleCert, 1, pkcs7->singleCertSz, file);
  44054. if (ret < 0)
  44055. ret = WC_TEST_RET_ENC_ERRNO;
  44056. else
  44057. ret = 0;
  44058. XFCLOSE(file);
  44059. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  44060. }
  44061. out:
  44062. if (pkcs7 != NULL)
  44063. wc_PKCS7_Free(pkcs7);
  44064. if (out != NULL)
  44065. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44066. if (testVectors != NULL)
  44067. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44068. wc_FreeRng(&rng);
  44069. if (ret > 0)
  44070. return 0;
  44071. (void)rsaClientCertBuf;
  44072. (void)rsaClientCertBufSz;
  44073. (void)rsaClientPrivKeyBuf;
  44074. (void)rsaClientPrivKeyBufSz;
  44075. (void)rsaServerCertBuf;
  44076. (void)rsaServerCertBufSz;
  44077. (void)rsaServerPrivKeyBuf;
  44078. (void)rsaServerPrivKeyBufSz;
  44079. (void)rsaCaCertBuf;
  44080. (void)rsaCaCertBufSz;
  44081. (void)rsaCaPrivKeyBuf;
  44082. (void)rsaCaPrivKeyBufSz;
  44083. (void)eccClientCertBuf;
  44084. (void)eccClientCertBufSz;
  44085. (void)eccClientPrivKeyBuf;
  44086. (void)eccClientPrivKeyBufSz;
  44087. return ret;
  44088. }
  44089. static wc_test_ret_t pkcs7signed_run_SingleShotVectors(
  44090. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  44091. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  44092. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  44093. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  44094. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  44095. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  44096. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  44097. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  44098. {
  44099. wc_test_ret_t ret;
  44100. int testSz = 0, i;
  44101. int encodedSz;
  44102. byte* out = NULL;
  44103. word32 outSz;
  44104. WC_RNG rng;
  44105. PKCS7* pkcs7 = NULL;
  44106. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  44107. XFILE file;
  44108. #endif
  44109. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  44110. !defined(NO_PKCS7_ENCRYPTED_DATA)
  44111. byte* encryptedTmp = NULL;
  44112. int encryptedTmpSz;
  44113. #endif
  44114. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  44115. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  44116. 0x72,0x6c,0x64
  44117. };
  44118. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  44119. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  44120. static const byte aes256Key[] = {
  44121. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  44122. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  44123. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  44124. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  44125. };
  44126. #endif
  44127. static const byte messageTypeOid[] =
  44128. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  44129. 0x09, 0x02 };
  44130. static const byte messageType[] = { 0x13, 2, '1', '9' };
  44131. PKCS7Attrib attribs[] =
  44132. {
  44133. { messageTypeOid, sizeof(messageTypeOid), messageType,
  44134. sizeof(messageType) },
  44135. };
  44136. #define MAX_TESTVECTORS_LEN 19
  44137. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  44138. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  44139. if (testSz == MAX_TESTVECTORS_LEN) { \
  44140. ret = WC_TEST_RET_ENC_NC; \
  44141. goto out; \
  44142. } \
  44143. XMEMCPY(&testVectors[testSz++], &_this_vector, \
  44144. sizeof _this_vector); \
  44145. }
  44146. pkcs7SignedVector *testVectors = NULL;
  44147. XMEMSET(&rng, 0, sizeof(rng));
  44148. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  44149. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44150. if (testVectors == NULL) {
  44151. ret = WC_TEST_RET_ENC_ERRNO;
  44152. goto out;
  44153. }
  44154. {
  44155. #ifndef NO_RSA
  44156. #ifndef NO_SHA256
  44157. /* Signed FirmwarePkgData, RSA, SHA256, no attribs */
  44158. ADD_PKCS7SIGNEDVECTOR(
  44159. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  44160. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44161. NULL, 0,
  44162. "pkcs7signedFirmwarePkgData_RSA_SHA256_noattr.der", 0, NULL, 0, 0,
  44163. 0, 0, NULL, 0, NULL, 0, 0);
  44164. /* Signed FirmwarePkgData, RSA, SHA256, attrs */
  44165. ADD_PKCS7SIGNEDVECTOR(
  44166. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  44167. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44168. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44169. "pkcs7signedFirmwarePkgData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0,
  44170. NULL, 0, NULL, 0, 0);
  44171. /* Signed FirmwarePkgData, RSA, SHA256, SubjectKeyIdentifier, attrs */
  44172. ADD_PKCS7SIGNEDVECTOR(
  44173. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  44174. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44175. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44176. "pkcs7signedFirmwarePkgData_RSA_SHA256_SKID.der", 0, NULL,
  44177. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  44178. /* Signed FirmwraePkgData, RSA, SHA256, server cert and ca cert, attr */
  44179. ADD_PKCS7SIGNEDVECTOR(
  44180. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  44181. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  44182. rsaCaCertBuf, rsaCaCertBufSz,
  44183. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44184. "pkcs7signedFirmwarePkgData_RSA_SHA256_with_ca_cert.der", 0, NULL,
  44185. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  44186. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  44187. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  44188. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, no attribs */
  44189. ADD_PKCS7SIGNEDVECTOR(
  44190. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  44191. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44192. NULL, 0,
  44193. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  44194. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  44195. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, attribs */
  44196. ADD_PKCS7SIGNEDVECTOR(
  44197. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  44198. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44199. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44200. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256.der", 0,
  44201. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  44202. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  44203. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  44204. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  44205. /* Signed Compressed FirmwarePkgData, RSA, SHA256, no attribs */
  44206. ADD_PKCS7SIGNEDVECTOR(
  44207. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  44208. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44209. NULL, 0,
  44210. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  44211. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  44212. /* Signed Compressed FirmwarePkgData, RSA, SHA256, attribs */
  44213. ADD_PKCS7SIGNEDVECTOR(
  44214. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  44215. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44216. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44217. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256.der", 0,
  44218. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  44219. #ifndef NO_PKCS7_ENCRYPTED_DATA
  44220. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  44221. no attribs */
  44222. ADD_PKCS7SIGNEDVECTOR(
  44223. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  44224. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44225. NULL, 0,
  44226. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256_noattr.der",
  44227. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  44228. 0, 0);
  44229. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  44230. attribs */
  44231. ADD_PKCS7SIGNEDVECTOR(
  44232. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  44233. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44234. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44235. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256.der",
  44236. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  44237. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  44238. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  44239. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  44240. #endif /* NO_SHA256 */
  44241. #endif /* NO_RSA */
  44242. #ifdef HAVE_ECC
  44243. #ifndef NO_SHA256
  44244. /* Signed FirmwarePkgData, ECDSA, SHA256, no attribs */
  44245. ADD_PKCS7SIGNEDVECTOR(
  44246. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  44247. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44248. NULL, 0,
  44249. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  44250. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  44251. /* Signed FirmwarePkgData, ECDSA, SHA256, attribs */
  44252. ADD_PKCS7SIGNEDVECTOR(
  44253. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  44254. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44255. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44256. "pkcs7signedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  44257. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  44258. /* Signed FirmwarePkgData, ECDSA, SHA256, SubjectKeyIdentifier, attr */
  44259. ADD_PKCS7SIGNEDVECTOR(
  44260. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  44261. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44262. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44263. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_SKID.der", 0, NULL,
  44264. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  44265. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  44266. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  44267. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, no attribs */
  44268. ADD_PKCS7SIGNEDVECTOR(
  44269. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  44270. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44271. NULL, 0,
  44272. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  44273. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  44274. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, attribs */
  44275. ADD_PKCS7SIGNEDVECTOR(
  44276. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  44277. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44278. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44279. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  44280. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  44281. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  44282. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  44283. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  44284. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, no attribs */
  44285. ADD_PKCS7SIGNEDVECTOR(
  44286. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  44287. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44288. NULL, 0,
  44289. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  44290. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  44291. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, attrib */
  44292. ADD_PKCS7SIGNEDVECTOR(
  44293. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  44294. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44295. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44296. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  44297. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  44298. #ifndef NO_PKCS7_ENCRYPTED_DATA
  44299. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  44300. no attribs */
  44301. ADD_PKCS7SIGNEDVECTOR(
  44302. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  44303. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44304. NULL, 0,
  44305. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der",
  44306. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  44307. 0, 0);
  44308. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  44309. attribs */
  44310. ADD_PKCS7SIGNEDVECTOR(
  44311. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  44312. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44313. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44314. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256.der",
  44315. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  44316. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  44317. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  44318. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  44319. #endif /* NO_SHA256 */
  44320. #endif /* HAVE_ECC */
  44321. };
  44322. #undef MAX_TESTVECTORS_LEN
  44323. #undef ADD_PKCS7SIGNEDVECTOR
  44324. outSz = FOURK_BUF;
  44325. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44326. if (out == NULL)
  44327. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  44328. XMEMSET(out, 0, outSz);
  44329. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  44330. if (ret < 0)
  44331. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  44332. #ifndef HAVE_FIPS
  44333. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  44334. #else
  44335. ret = wc_InitRng(&rng);
  44336. #endif
  44337. if (ret != 0)
  44338. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  44339. for (i = 0; i < testSz; i++) {
  44340. if (pkcs7)
  44341. wc_PKCS7_Free(pkcs7);
  44342. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  44343. if (pkcs7 == NULL)
  44344. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  44345. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  44346. (word32)testVectors[i].certSz);
  44347. if (ret != 0)
  44348. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  44349. /* load CA certificate, if present */
  44350. if (testVectors[i].caCert != NULL) {
  44351. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  44352. (word32)testVectors[i].caCertSz);
  44353. if (ret != 0)
  44354. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  44355. }
  44356. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  44357. default is IssuerAndSerialNumber */
  44358. if (testVectors[i].sidType == CMS_SKID) {
  44359. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  44360. if (ret != 0)
  44361. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  44362. }
  44363. if (testVectors[i].encCompFlag == 0) {
  44364. /* encode Signed FirmwarePkgData */
  44365. encodedSz = wc_PKCS7_EncodeSignedFPD(pkcs7,
  44366. testVectors[i].privateKey, testVectors[i].privateKeySz,
  44367. testVectors[i].signOID, testVectors[i].hashOID,
  44368. (byte*)testVectors[i].content, testVectors[i].contentSz,
  44369. testVectors[i].signedAttribs,
  44370. testVectors[i].signedAttribsSz, out, outSz);
  44371. if (encodedSz < 0)
  44372. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  44373. #ifndef NO_PKCS7_ENCRYPTED_DATA
  44374. } else if (testVectors[i].encCompFlag == 1) {
  44375. /* encode Signed Encrypted FirmwarePkgData */
  44376. encodedSz = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7,
  44377. (byte *)testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  44378. testVectors[i].privateKey, testVectors[i].privateKeySz,
  44379. testVectors[i].encryptOID, testVectors[i].signOID,
  44380. testVectors[i].hashOID, (byte*)testVectors[i].content,
  44381. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  44382. testVectors[i].unprotectedAttribsSz,
  44383. testVectors[i].signedAttribs,
  44384. testVectors[i].signedAttribsSz, out, outSz);
  44385. if (encodedSz <= 0)
  44386. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  44387. #endif
  44388. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  44389. } else if (testVectors[i].encCompFlag == 2) {
  44390. /* encode Signed Compressed FirmwarePkgData */
  44391. encodedSz = wc_PKCS7_EncodeSignedCompressedFPD(pkcs7,
  44392. testVectors[i].privateKey, testVectors[i].privateKeySz,
  44393. testVectors[i].signOID, testVectors[i].hashOID,
  44394. (byte*)testVectors[i].content, testVectors[i].contentSz,
  44395. testVectors[i].signedAttribs,
  44396. testVectors[i].signedAttribsSz, out, outSz);
  44397. if (encodedSz <= 0)
  44398. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  44399. #ifndef NO_PKCS7_ENCRYPTED_DATA
  44400. } else if (testVectors[i].encCompFlag == 3) {
  44401. /* encode Signed Encrypted Compressed FirmwarePkgData */
  44402. encodedSz = wc_PKCS7_EncodeSignedEncryptedCompressedFPD(pkcs7,
  44403. (byte*)testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  44404. testVectors[i].privateKey, testVectors[i].privateKeySz,
  44405. testVectors[i].encryptOID, testVectors[i].signOID,
  44406. testVectors[i].hashOID, (byte*)testVectors[i].content,
  44407. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  44408. testVectors[i].unprotectedAttribsSz,
  44409. testVectors[i].signedAttribs,
  44410. testVectors[i].signedAttribsSz, out, outSz);
  44411. if (encodedSz <= 0)
  44412. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  44413. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  44414. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  44415. } else {
  44416. /* unsupported SignedData single-shot combination */
  44417. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  44418. }
  44419. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  44420. /* write PKCS#7 to output file for more testing */
  44421. file = XFOPEN(testVectors[i].outFileName, "wb");
  44422. if (!file)
  44423. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  44424. ret = (int)XFWRITE(out, 1, encodedSz, file);
  44425. XFCLOSE(file);
  44426. file = NULL;
  44427. if (ret != (int)encodedSz)
  44428. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  44429. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  44430. wc_PKCS7_Free(pkcs7);
  44431. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  44432. if (pkcs7 == NULL)
  44433. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  44434. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  44435. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  44436. if (ret < 0)
  44437. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  44438. #ifndef NO_PKCS7_STREAM
  44439. {
  44440. word32 z;
  44441. for (z = 0; z < outSz && ret != 0; z++) {
  44442. ret = wc_PKCS7_VerifySignedData(pkcs7, out + z, 1);
  44443. if (ret < 0 && ret != WC_PKCS7_WANT_READ_E) {
  44444. printf("unexpected error %d\n", ret);
  44445. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  44446. }
  44447. }
  44448. }
  44449. #endif
  44450. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  44451. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  44452. if (testVectors[i].encCompFlag == 0) {
  44453. /* verify decoded content matches expected */
  44454. if ((pkcs7->contentSz != testVectors[i].contentSz) ||
  44455. XMEMCMP(pkcs7->content, testVectors[i].content,
  44456. pkcs7->contentSz)) {
  44457. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  44458. }
  44459. }
  44460. #ifndef NO_PKCS7_ENCRYPTED_DATA
  44461. else if (testVectors[i].encCompFlag == 1) {
  44462. /* decrypt inner encryptedData */
  44463. pkcs7->encryptionKey = (byte *)testVectors[i].encryptKey;
  44464. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  44465. ret = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  44466. pkcs7->contentSz, out, outSz);
  44467. if (ret < 0)
  44468. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  44469. /* compare decrypted to expected */
  44470. if (((word32)ret != testVectors[i].contentSz) ||
  44471. XMEMCMP(out, testVectors[i].content, ret))
  44472. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  44473. }
  44474. #endif
  44475. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  44476. else if (testVectors[i].encCompFlag == 2) {
  44477. /* decompress inner compressedData */
  44478. ret = wc_PKCS7_DecodeCompressedData(pkcs7, pkcs7->content,
  44479. pkcs7->contentSz, out, outSz);
  44480. if (ret < 0)
  44481. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  44482. /* compare decompressed to expected */
  44483. if (((word32)ret != testVectors[i].contentSz) ||
  44484. XMEMCMP(out, testVectors[i].content, ret))
  44485. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  44486. }
  44487. #ifndef NO_PKCS7_ENCRYPTED_DATA
  44488. else if (testVectors[i].encCompFlag == 3) {
  44489. encryptedTmpSz = FOURK_BUF;
  44490. encryptedTmp = (byte*)XMALLOC(encryptedTmpSz, HEAP_HINT,
  44491. DYNAMIC_TYPE_TMP_BUFFER);
  44492. if (encryptedTmp == NULL)
  44493. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  44494. XMEMSET(encryptedTmp, 0, encryptedTmpSz);
  44495. /* decrypt inner encryptedData */
  44496. pkcs7->encryptionKey = (byte*)testVectors[i].encryptKey;
  44497. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  44498. encryptedTmpSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  44499. pkcs7->contentSz, encryptedTmp,
  44500. encryptedTmpSz);
  44501. if (encryptedTmpSz < 0 || pkcs7->contentOID != COMPRESSED_DATA)
  44502. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  44503. /* decompress inner compressedData */
  44504. ret = wc_PKCS7_DecodeCompressedData(pkcs7, encryptedTmp,
  44505. encryptedTmpSz, out, outSz);
  44506. if (ret < 0)
  44507. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  44508. /* compare decompressed to expected */
  44509. if (((word32)ret != testVectors[i].contentSz) ||
  44510. XMEMCMP(out, testVectors[i].content, ret))
  44511. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  44512. }
  44513. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  44514. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  44515. }
  44516. out:
  44517. if (pkcs7 != NULL)
  44518. wc_PKCS7_Free(pkcs7);
  44519. if (out != NULL)
  44520. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44521. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  44522. !defined(NO_PKCS7_ENCRYPTED_DATA)
  44523. if (encryptedTmp != NULL)
  44524. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44525. #endif
  44526. if (testVectors != NULL)
  44527. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44528. wc_FreeRng(&rng);
  44529. if (ret > 0)
  44530. return 0;
  44531. (void)eccClientCertBuf;
  44532. (void)eccClientCertBufSz;
  44533. (void)eccClientPrivKeyBuf;
  44534. (void)eccClientPrivKeyBufSz;
  44535. (void)rsaClientCertBuf;
  44536. (void)rsaClientCertBufSz;
  44537. (void)rsaClientPrivKeyBuf;
  44538. (void)rsaClientPrivKeyBufSz;
  44539. (void)rsaServerCertBuf;
  44540. (void)rsaServerCertBufSz;
  44541. (void)rsaServerPrivKeyBuf;
  44542. (void)rsaServerPrivKeyBufSz;
  44543. (void)rsaCaCertBuf;
  44544. (void)rsaCaCertBufSz;
  44545. (void)rsaCaPrivKeyBuf;
  44546. (void)rsaCaPrivKeyBufSz;
  44547. return ret;
  44548. }
  44549. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7signed_test(void)
  44550. {
  44551. wc_test_ret_t ret = 0;
  44552. byte* rsaClientCertBuf = NULL;
  44553. byte* rsaServerCertBuf = NULL;
  44554. byte* rsaCaCertBuf = NULL;
  44555. byte* eccClientCertBuf = NULL;
  44556. byte* rsaClientPrivKeyBuf = NULL;
  44557. byte* rsaServerPrivKeyBuf = NULL;
  44558. byte* rsaCaPrivKeyBuf = NULL;
  44559. byte* eccClientPrivKeyBuf = NULL;
  44560. word32 rsaClientCertBufSz = 0;
  44561. word32 rsaServerCertBufSz = 0;
  44562. word32 rsaCaCertBufSz = 0;
  44563. word32 eccClientCertBufSz = 0;
  44564. word32 rsaClientPrivKeyBufSz = 0;
  44565. word32 rsaServerPrivKeyBufSz = 0;
  44566. word32 rsaCaPrivKeyBufSz = 0;
  44567. word32 eccClientPrivKeyBufSz = 0;
  44568. WOLFSSL_ENTER("pkcs7signed_test");
  44569. #ifndef NO_RSA
  44570. /* read client RSA cert and key in DER format */
  44571. rsaClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  44572. DYNAMIC_TYPE_TMP_BUFFER);
  44573. if (rsaClientCertBuf == NULL)
  44574. ret = WC_TEST_RET_ENC_NC;
  44575. rsaClientPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  44576. DYNAMIC_TYPE_TMP_BUFFER);
  44577. if (ret == 0 && rsaClientPrivKeyBuf == NULL) {
  44578. ret = WC_TEST_RET_ENC_ERRNO;
  44579. }
  44580. rsaClientCertBufSz = FOURK_BUF;
  44581. rsaClientPrivKeyBufSz = FOURK_BUF;
  44582. /* read server RSA cert and key in DER format */
  44583. rsaServerCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  44584. DYNAMIC_TYPE_TMP_BUFFER);
  44585. if (ret == 0 && rsaServerCertBuf == NULL)
  44586. ret = WC_TEST_RET_ENC_NC;
  44587. rsaServerPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  44588. DYNAMIC_TYPE_TMP_BUFFER);
  44589. if (ret == 0 && rsaServerPrivKeyBuf == NULL) {
  44590. ret = WC_TEST_RET_ENC_ERRNO;
  44591. }
  44592. rsaServerCertBufSz = FOURK_BUF;
  44593. rsaServerPrivKeyBufSz = FOURK_BUF;
  44594. /* read CA RSA cert and key in DER format, for use with server cert */
  44595. rsaCaCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  44596. DYNAMIC_TYPE_TMP_BUFFER);
  44597. if (ret == 0 && rsaCaCertBuf == NULL)
  44598. ret = WC_TEST_RET_ENC_NC;
  44599. rsaCaPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  44600. DYNAMIC_TYPE_TMP_BUFFER);
  44601. if (ret == 0 && rsaCaPrivKeyBuf == NULL) {
  44602. ret = WC_TEST_RET_ENC_ERRNO;
  44603. }
  44604. rsaCaCertBufSz = FOURK_BUF;
  44605. rsaCaPrivKeyBufSz = FOURK_BUF;
  44606. #endif /* NO_RSA */
  44607. #ifdef HAVE_ECC
  44608. /* read client ECC cert and key in DER format */
  44609. eccClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  44610. DYNAMIC_TYPE_TMP_BUFFER);
  44611. if (ret == 0 && eccClientCertBuf == NULL) {
  44612. ret = WC_TEST_RET_ENC_ERRNO;
  44613. }
  44614. eccClientPrivKeyBuf =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  44615. DYNAMIC_TYPE_TMP_BUFFER);
  44616. if (ret == 0 && eccClientPrivKeyBuf == NULL) {
  44617. ret = WC_TEST_RET_ENC_ERRNO;
  44618. }
  44619. eccClientCertBufSz = FOURK_BUF;
  44620. eccClientPrivKeyBufSz = FOURK_BUF;
  44621. #endif /* HAVE_ECC */
  44622. if (ret >= 0)
  44623. ret = pkcs7_load_certs_keys(rsaClientCertBuf, &rsaClientCertBufSz,
  44624. rsaClientPrivKeyBuf, &rsaClientPrivKeyBufSz,
  44625. rsaServerCertBuf, &rsaServerCertBufSz,
  44626. rsaServerPrivKeyBuf, &rsaServerPrivKeyBufSz,
  44627. rsaCaCertBuf, &rsaCaCertBufSz,
  44628. rsaCaPrivKeyBuf, &rsaCaPrivKeyBufSz,
  44629. eccClientCertBuf, &eccClientCertBufSz,
  44630. eccClientPrivKeyBuf, &eccClientPrivKeyBufSz);
  44631. if (ret < 0) {
  44632. ret = WC_TEST_RET_ENC_EC(ret);
  44633. }
  44634. if (ret >= 0)
  44635. ret = pkcs7signed_run_vectors(rsaClientCertBuf, (word32)rsaClientCertBufSz,
  44636. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  44637. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  44638. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  44639. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  44640. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  44641. eccClientCertBuf, (word32)eccClientCertBufSz,
  44642. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  44643. if (ret >= 0)
  44644. ret = pkcs7signed_run_SingleShotVectors(
  44645. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  44646. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  44647. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  44648. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  44649. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  44650. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  44651. eccClientCertBuf, (word32)eccClientCertBufSz,
  44652. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  44653. #if !defined(NO_RSA) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  44654. if (ret >= 0)
  44655. ret = pkcs7callback_test(
  44656. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  44657. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz);
  44658. #endif
  44659. XFREE(rsaClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44660. XFREE(rsaClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44661. XFREE(rsaServerCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44662. XFREE(rsaServerPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44663. XFREE(rsaCaCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44664. XFREE(rsaCaPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44665. XFREE(eccClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44666. XFREE(eccClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44667. return ret;
  44668. }
  44669. #endif /* HAVE_PKCS7 */
  44670. #if defined(WOLFSSL_PUBLIC_MP) && \
  44671. ((defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  44672. defined(USE_FAST_MATH))
  44673. /* Maximum number of bytes in a number to test. */
  44674. #define MP_MAX_TEST_BYTE_LEN 32
  44675. static wc_test_ret_t randNum(mp_int* n, int len, WC_RNG* rng, void* heap)
  44676. {
  44677. byte d[MP_MAX_TEST_BYTE_LEN];
  44678. wc_test_ret_t ret;
  44679. (void)heap;
  44680. do {
  44681. ret = wc_RNG_GenerateBlock(rng, d, (word32)len);
  44682. if (ret != 0)
  44683. return ret;
  44684. ret = mp_read_unsigned_bin(n, d, (word32)len);
  44685. if (ret != 0)
  44686. return ret;
  44687. } while (mp_iszero(n));
  44688. return 0;
  44689. }
  44690. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  44691. static wc_test_ret_t mp_test_div_3(mp_int* a, mp_int* r, WC_RNG* rng)
  44692. {
  44693. int i, j;
  44694. mp_digit rem;
  44695. mp_digit rem2;
  44696. wc_test_ret_t ret;
  44697. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  44698. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  44699. for (i = 0; i < 10; i++) {
  44700. for (j = 1; j < 10; j++) {
  44701. ret = randNum(a, j, rng, NULL);
  44702. if (ret != 0)
  44703. return WC_TEST_RET_ENC_EC(ret);
  44704. ret = mp_div_3(a, r, &rem);
  44705. if (ret != 0)
  44706. return WC_TEST_RET_ENC_EC(ret);
  44707. ret = mp_mul_d(r, 3, r);
  44708. if (ret != 0)
  44709. return WC_TEST_RET_ENC_EC(ret);
  44710. ret = mp_add_d(r, rem, r);
  44711. if (ret != 0)
  44712. return WC_TEST_RET_ENC_EC(ret);
  44713. ret = mp_cmp(r, a);
  44714. if (ret != MP_EQ)
  44715. return WC_TEST_RET_ENC_NC;
  44716. }
  44717. }
  44718. ret = mp_div_3(a, r, &rem);
  44719. if (ret != 0)
  44720. return WC_TEST_RET_ENC_NC;
  44721. ret = mp_div_3(a, a, NULL);
  44722. if (ret != 0)
  44723. return WC_TEST_RET_ENC_NC;
  44724. ret = mp_cmp(r, a);
  44725. if (ret != MP_EQ)
  44726. return WC_TEST_RET_ENC_NC;
  44727. #endif
  44728. #if defined(WOLFSSL_SP_MATH_ALL)
  44729. ret = mp_div_d(a, 10, r, &rem);
  44730. if (ret != 0)
  44731. return WC_TEST_RET_ENC_EC(ret);
  44732. ret = mp_div_d(a, 10, a, NULL);
  44733. if (ret != 0)
  44734. return WC_TEST_RET_ENC_EC(ret);
  44735. ret = mp_cmp(r, a);
  44736. if (ret != MP_EQ)
  44737. return WC_TEST_RET_ENC_NC;
  44738. ret = mp_div_d(a, 12, r, &rem);
  44739. if (ret != 0)
  44740. return WC_TEST_RET_ENC_EC(ret);
  44741. ret = mp_div_d(a, 12, a, NULL);
  44742. if (ret != 0)
  44743. return WC_TEST_RET_ENC_EC(ret);
  44744. ret = mp_cmp(r, a);
  44745. if (ret != MP_EQ)
  44746. return WC_TEST_RET_ENC_NC;
  44747. ret = mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), r, &rem);
  44748. if (ret != 0)
  44749. return WC_TEST_RET_ENC_EC(ret);
  44750. ret = mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), NULL, &rem2);
  44751. if (ret != 0)
  44752. return WC_TEST_RET_ENC_EC(ret);
  44753. ret = mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), a, NULL);
  44754. if (ret != 0)
  44755. return WC_TEST_RET_ENC_EC(ret);
  44756. ret = mp_cmp(r, a);
  44757. if (ret != MP_EQ)
  44758. return WC_TEST_RET_ENC_NC;
  44759. if (rem != rem2)
  44760. return WC_TEST_RET_ENC_NC;
  44761. #endif
  44762. (void)a;
  44763. (void)r;
  44764. (void)rng;
  44765. (void)i;
  44766. (void)j;
  44767. (void)rem;
  44768. (void)rem2;
  44769. (void)ret;
  44770. return 0;
  44771. }
  44772. #endif /* WOLFSSL_SP_MATH || !USE_FAST_MATH */
  44773. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  44774. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  44775. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  44776. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  44777. static wc_test_ret_t mp_test_radix_10(mp_int* a, mp_int* r, WC_RNG* rng)
  44778. {
  44779. wc_test_ret_t ret;
  44780. int i, j;
  44781. int size;
  44782. char str[30];
  44783. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = "A";
  44784. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "a";
  44785. WOLFSSL_SMALL_STACK_STATIC const char* empty2 = " ";
  44786. WOLFSSL_SMALL_STACK_STATIC const char* zeros = "000";
  44787. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  44788. for (i = 0; i < 10; i++) {
  44789. for (j = 2; j < 12; j++) {
  44790. ret = randNum(a, j, rng, NULL);
  44791. if (ret != 0)
  44792. return WC_TEST_RET_ENC_EC(ret);
  44793. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  44794. if (ret != MP_OKAY)
  44795. return WC_TEST_RET_ENC_EC(ret);
  44796. ret = mp_toradix(a, str, MP_RADIX_DEC);
  44797. if (ret != MP_OKAY)
  44798. return WC_TEST_RET_ENC_EC(ret);
  44799. if ((int)XSTRLEN(str) != size - 1)
  44800. return WC_TEST_RET_ENC_NC;
  44801. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  44802. if (ret != MP_OKAY)
  44803. return WC_TEST_RET_ENC_EC(ret);
  44804. ret = mp_cmp(a, r);
  44805. if (ret != MP_EQ)
  44806. return WC_TEST_RET_ENC_NC;
  44807. }
  44808. }
  44809. ret = mp_read_radix(r, badStr1, MP_RADIX_DEC);
  44810. if (ret != MP_VAL)
  44811. return WC_TEST_RET_ENC_EC(ret);
  44812. ret = mp_read_radix(r, badStr2, MP_RADIX_DEC);
  44813. if (ret != MP_VAL)
  44814. return WC_TEST_RET_ENC_EC(ret);
  44815. ret = mp_read_radix(r, empty2, MP_RADIX_DEC);
  44816. if (ret != MP_OKAY)
  44817. return WC_TEST_RET_ENC_EC(ret);
  44818. ret = mp_read_radix(r, zeros, MP_RADIX_DEC);
  44819. if (ret != MP_OKAY)
  44820. return WC_TEST_RET_ENC_EC(ret);
  44821. if (!mp_iszero(r))
  44822. return WC_TEST_RET_ENC_NC;
  44823. mp_set(r, 1);
  44824. ret = mp_read_radix(r, empty, MP_RADIX_DEC);
  44825. if (ret != MP_OKAY)
  44826. return WC_TEST_RET_ENC_EC(ret);
  44827. if (!mp_iszero(r))
  44828. return WC_TEST_RET_ENC_NC;
  44829. mp_zero(a);
  44830. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  44831. if (ret != 0)
  44832. return WC_TEST_RET_ENC_EC(ret);
  44833. if (size != 2)
  44834. return WC_TEST_RET_ENC_NC;
  44835. ret = mp_toradix(a, str, MP_RADIX_DEC);
  44836. if (ret != 0)
  44837. return WC_TEST_RET_ENC_EC(ret);
  44838. if ((int)XSTRLEN(str) != size - 1)
  44839. return WC_TEST_RET_ENC_NC;
  44840. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  44841. if (ret != 0)
  44842. return WC_TEST_RET_ENC_EC(ret);
  44843. if (!mp_iszero(r))
  44844. return WC_TEST_RET_ENC_NC;
  44845. return 0;
  44846. }
  44847. #endif
  44848. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  44849. defined(HAVE_ECC))
  44850. static wc_test_ret_t mp_test_radix_16(mp_int* a, mp_int* r, WC_RNG* rng)
  44851. {
  44852. wc_test_ret_t ret;
  44853. int i, j;
  44854. int size;
  44855. char str[30];
  44856. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  44857. static char longStr[2 * sizeof(a->dp) + 2];
  44858. #endif
  44859. WOLFSSL_SMALL_STACK_STATIC const char* empty2 = " ";
  44860. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "}";
  44861. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  44862. for (i = 0; i < 10; i++) {
  44863. for (j = 2; j < 12; j++) {
  44864. ret = randNum(a, j, rng, NULL);
  44865. if (ret != 0)
  44866. return WC_TEST_RET_ENC_EC(ret);
  44867. mp_radix_size(a, MP_RADIX_HEX, &size);
  44868. mp_toradix(a, str, MP_RADIX_HEX);
  44869. if ((int)XSTRLEN(str) != size - 1)
  44870. return WC_TEST_RET_ENC_NC;
  44871. mp_read_radix(r, str, MP_RADIX_HEX);
  44872. ret = mp_cmp(a, r);
  44873. if (ret != MP_EQ)
  44874. return WC_TEST_RET_ENC_NC;
  44875. }
  44876. }
  44877. ret = mp_read_radix(r, empty2, MP_RADIX_HEX);
  44878. if (ret != MP_OKAY)
  44879. return WC_TEST_RET_ENC_EC(ret);
  44880. ret = mp_read_radix(r, badStr2, MP_RADIX_HEX);
  44881. if (ret != MP_VAL)
  44882. return WC_TEST_RET_ENC_EC(ret);
  44883. mp_set(r, 1);
  44884. ret = mp_read_radix(r, empty, MP_RADIX_HEX);
  44885. if (ret != MP_OKAY)
  44886. return WC_TEST_RET_ENC_EC(ret);
  44887. if (!mp_iszero(r))
  44888. return WC_TEST_RET_ENC_NC;
  44889. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  44890. /* Fixed MP data size - string can be too long. */
  44891. longStr[0] = '8';
  44892. XMEMSET(longStr+1, '0', sizeof(longStr) - 2);
  44893. longStr[sizeof(longStr)-1] = '\0';
  44894. ret = mp_read_radix(r, longStr, MP_RADIX_HEX);
  44895. if (ret != MP_VAL)
  44896. return WC_TEST_RET_ENC_EC(ret);
  44897. #endif
  44898. mp_zero(a);
  44899. ret = mp_radix_size(a, MP_RADIX_HEX, &size);
  44900. if (ret != 0)
  44901. return WC_TEST_RET_ENC_EC(ret);
  44902. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  44903. if (size != 3)
  44904. #else
  44905. if (size != 2)
  44906. #endif
  44907. return WC_TEST_RET_ENC_NC;
  44908. ret = mp_toradix(a, str, MP_RADIX_HEX);
  44909. if (ret != 0)
  44910. return WC_TEST_RET_ENC_EC(ret);
  44911. if ((int)XSTRLEN(str) != size - 1)
  44912. return WC_TEST_RET_ENC_NC;
  44913. ret = mp_read_radix(r, str, MP_RADIX_HEX);
  44914. if (ret != 0)
  44915. return WC_TEST_RET_ENC_EC(ret);
  44916. if (!mp_iszero(r))
  44917. return WC_TEST_RET_ENC_NC;
  44918. #ifdef WOLFSSL_SP_MATH
  44919. ret = mp_toradix(a, str, 8);
  44920. if (ret != MP_VAL)
  44921. return WC_TEST_RET_ENC_EC(ret);
  44922. ret = mp_radix_size(a, 8, &size);
  44923. if (ret != MP_VAL)
  44924. return WC_TEST_RET_ENC_EC(ret);
  44925. #endif
  44926. return 0;
  44927. }
  44928. #endif
  44929. static wc_test_ret_t mp_test_shift(mp_int* a, mp_int* r1, WC_RNG* rng)
  44930. {
  44931. int i;
  44932. wc_test_ret_t ret;
  44933. ret = randNum(a, 4, rng, NULL);
  44934. if (ret != 0)
  44935. return WC_TEST_RET_ENC_EC(ret);
  44936. for (i = 0; i < 4; i++) {
  44937. mp_copy(r1, a);
  44938. ret = mp_lshd(r1, i);
  44939. if (ret != MP_OKAY)
  44940. return WC_TEST_RET_ENC_EC(ret);
  44941. #ifndef WOLFSSL_SP_MATH
  44942. mp_rshd(r1, i);
  44943. #else
  44944. mp_rshb(r1, i * SP_WORD_SIZE);
  44945. #endif
  44946. ret = mp_cmp(a, r1);
  44947. if (ret != MP_EQ)
  44948. return WC_TEST_RET_ENC_NC;
  44949. }
  44950. #ifndef WOLFSSL_SP_MATH
  44951. for (i = 0; i < DIGIT_BIT+1; i++) {
  44952. ret = mp_mul_2d(a, i, r1);
  44953. if (ret != MP_OKAY)
  44954. return WC_TEST_RET_ENC_EC(ret);
  44955. mp_rshb(r1, i);
  44956. ret = mp_cmp(a, r1);
  44957. if (ret != MP_EQ)
  44958. return WC_TEST_RET_ENC_NC;
  44959. }
  44960. #endif
  44961. return 0;
  44962. }
  44963. static wc_test_ret_t mp_test_add_sub_d(mp_int* a, mp_int* r1)
  44964. {
  44965. int i, j;
  44966. wc_test_ret_t ret;
  44967. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  44968. mp_zero(a);
  44969. mp_set_bit(a, i);
  44970. if ((int)a->used != (i + DIGIT_BIT) / DIGIT_BIT)
  44971. return WC_TEST_RET_ENC_NC;
  44972. for (j = 0; j < i && j < DIGIT_BIT; j++) {
  44973. mp_zero(r1);
  44974. mp_set_bit(r1, i);
  44975. ret = mp_sub_d(r1, (mp_digit)1 << j, r1);
  44976. if (ret != MP_OKAY)
  44977. return WC_TEST_RET_ENC_EC(ret);
  44978. ret = mp_add_d(r1, (mp_digit)1 << j, r1);
  44979. if (ret != MP_OKAY)
  44980. return WC_TEST_RET_ENC_EC(ret);
  44981. ret = mp_cmp(a, r1);
  44982. if (ret != MP_EQ)
  44983. return WC_TEST_RET_ENC_NC;
  44984. }
  44985. }
  44986. mp_zero(r1);
  44987. ret = mp_add_d(r1, 1, r1);
  44988. if (ret != MP_OKAY)
  44989. return WC_TEST_RET_ENC_EC(ret);
  44990. if (r1->used != 1)
  44991. return WC_TEST_RET_ENC_NC;
  44992. ret = mp_sub_d(r1, 1, r1);
  44993. if (ret != MP_OKAY)
  44994. return WC_TEST_RET_ENC_EC(ret);
  44995. if (r1->used != 0)
  44996. return WC_TEST_RET_ENC_NC;
  44997. return 0;
  44998. }
  44999. static wc_test_ret_t mp_test_read_to_bin(mp_int* a)
  45000. {
  45001. WOLFSSL_SMALL_STACK_STATIC const byte in[16] = {
  45002. 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7, 0x08,
  45003. 0x93, 0xa4, 0xb4, 0xc5, 0xd6, 0xe7, 0xf8, 0x09
  45004. };
  45005. byte out[24];
  45006. int i, j, k;
  45007. const byte* p;
  45008. wc_test_ret_t ret;
  45009. for (i = 0; i < (int)sizeof(in); i++) {
  45010. p = in + sizeof(in) - i;
  45011. ret = mp_read_unsigned_bin(a, p, (word32)i);
  45012. if (ret != 0)
  45013. return WC_TEST_RET_ENC_EC(ret);
  45014. for (j = i; j < (int)sizeof(out); j++) {
  45015. XMEMSET(out, 0xff, sizeof(out));
  45016. ret = mp_to_unsigned_bin_len(a, out, j);
  45017. if (ret != 0)
  45018. return WC_TEST_RET_ENC_EC(ret);
  45019. for (k = 0; k < j - i; k++) {
  45020. if (out[k] != 0)
  45021. return WC_TEST_RET_ENC_NC;
  45022. }
  45023. for (; k < j; k++) {
  45024. if (out[k] != p[k - (j - i)])
  45025. return WC_TEST_RET_ENC_NC;
  45026. }
  45027. }
  45028. }
  45029. /* Length too small. */
  45030. ret = mp_to_unsigned_bin_len(a, out, 1);
  45031. if (ret != MP_VAL)
  45032. return WC_TEST_RET_ENC_EC(ret);
  45033. ret = mp_read_unsigned_bin(a, NULL, 0);
  45034. if (ret != 0)
  45035. return WC_TEST_RET_ENC_EC(ret);
  45036. if (!mp_iszero(a))
  45037. return WC_TEST_RET_ENC_NC;
  45038. return 0;
  45039. }
  45040. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  45041. static wc_test_ret_t mp_test_set_int(mp_int* a)
  45042. {
  45043. #if SP_ULONG_BITS == 64
  45044. unsigned long n = 0xfedcba9876543210UL;
  45045. byte exp[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
  45046. byte out[8] = { 0 };
  45047. #elif SP_ULONG_BITS == 32
  45048. unsigned long n = 0xfedcba98UL;
  45049. byte exp[4] = { 0xfe, 0xdc, 0xba, 0x98 };
  45050. byte out[4] = { 0 };
  45051. #elif SP_ULONG_BITS == 16
  45052. unsigned long n = 0xfedc;
  45053. byte exp[2] = { 0xfe, 0xdc };
  45054. byte out[2] = { 0 };
  45055. #elif SP_ULONG_BITS == 8
  45056. unsigned long n = 0xfe;
  45057. byte exp[1] = { 0xfe };
  45058. byte out[1] = { 0 };
  45059. #endif
  45060. wc_test_ret_t ret;
  45061. ret = mp_set_int(a, n);
  45062. if (ret != 0)
  45063. return WC_TEST_RET_ENC_EC(ret);
  45064. ret = mp_unsigned_bin_size(a);
  45065. if (ret != sizeof(exp))
  45066. return WC_TEST_RET_ENC_NC;
  45067. ret = mp_to_unsigned_bin(a, out);
  45068. if (ret != 0)
  45069. return WC_TEST_RET_ENC_EC(ret);
  45070. if (XMEMCMP(exp, out, sizeof(exp)) != 0)
  45071. return WC_TEST_RET_ENC_NC;
  45072. return 0;
  45073. }
  45074. #endif
  45075. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  45076. static wc_test_ret_t mp_test_param(mp_int* a, mp_int* b, mp_int* r, WC_RNG* rng)
  45077. {
  45078. byte buffer[16];
  45079. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  45080. char hexStr[] = "abcdef0123456789";
  45081. #ifndef WOLFSSL_SP_INT_NEGATIVE
  45082. char negStr[] = "-1234";
  45083. #endif
  45084. #endif
  45085. #if !defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_KEY_GEN) || \
  45086. defined(HAVE_COMP_KEY)
  45087. char decStr[] = "0987654321";
  45088. #endif
  45089. wc_test_ret_t ret;
  45090. #ifdef WOLFSSL_SP_MATH_ALL
  45091. mp_digit rho;
  45092. int size;
  45093. #endif
  45094. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  45095. int result;
  45096. #endif
  45097. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  45098. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  45099. mp_digit rd;
  45100. #endif
  45101. (void)rng;
  45102. (void)r;
  45103. ret = mp_init(NULL);
  45104. if (ret != MP_VAL)
  45105. return WC_TEST_RET_ENC_EC(ret);
  45106. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  45107. ret = mp_init_multi(NULL, NULL, NULL, NULL, NULL, NULL);
  45108. if (ret != MP_OKAY)
  45109. return WC_TEST_RET_ENC_EC(ret);
  45110. #endif
  45111. mp_free(NULL);
  45112. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || !defined(NO_DH) || defined(HAVE_ECC)
  45113. ret = mp_grow(NULL, 1);
  45114. if (ret != MP_VAL)
  45115. return WC_TEST_RET_ENC_EC(ret);
  45116. #ifdef WOLFSSL_SP_MATH
  45117. ret = mp_grow(a, SP_INT_DIGITS + 1);
  45118. if (ret != MP_MEM)
  45119. return WC_TEST_RET_ENC_EC(ret);
  45120. #endif
  45121. #endif
  45122. mp_clear(NULL);
  45123. ret = mp_abs(NULL, NULL);
  45124. if (ret != MP_VAL)
  45125. return WC_TEST_RET_ENC_EC(ret);
  45126. ret = mp_abs(a, NULL);
  45127. if (ret != MP_VAL)
  45128. return WC_TEST_RET_ENC_EC(ret);
  45129. ret = mp_abs(NULL, b);
  45130. if (ret != MP_VAL)
  45131. return WC_TEST_RET_ENC_EC(ret);
  45132. ret = mp_unsigned_bin_size(NULL);
  45133. if (ret != 0)
  45134. return WC_TEST_RET_ENC_EC(ret);
  45135. ret = mp_read_unsigned_bin(NULL, NULL, sizeof(buffer));
  45136. if (ret != MP_VAL)
  45137. return WC_TEST_RET_ENC_EC(ret);
  45138. ret = mp_read_unsigned_bin(NULL, buffer, sizeof(buffer));
  45139. if (ret != MP_VAL)
  45140. return WC_TEST_RET_ENC_EC(ret);
  45141. ret = mp_read_unsigned_bin(a, NULL, sizeof(buffer));
  45142. if (ret != MP_VAL)
  45143. return WC_TEST_RET_ENC_EC(ret);
  45144. ret = mp_read_unsigned_bin(a, buffer, SP_INT_DIGITS * SP_WORD_SIZEOF + 1);
  45145. if (ret != MP_VAL)
  45146. return WC_TEST_RET_ENC_EC(ret);
  45147. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  45148. ret = mp_read_radix(NULL, NULL, 16);
  45149. if (ret != MP_VAL)
  45150. return WC_TEST_RET_ENC_EC(ret);
  45151. ret = mp_read_radix(a, NULL, 16);
  45152. if (ret != MP_VAL)
  45153. return WC_TEST_RET_ENC_EC(ret);
  45154. ret = mp_read_radix(NULL, hexStr, 16);
  45155. if (ret != MP_VAL)
  45156. return WC_TEST_RET_ENC_EC(ret);
  45157. #ifndef WOLFSSL_SP_INT_NEGATIVE
  45158. ret = mp_read_radix(a, negStr, 16);
  45159. if (ret != MP_VAL)
  45160. return WC_TEST_RET_ENC_EC(ret);
  45161. #ifdef WOLFSSL_SP_MATH_ALL
  45162. ret = mp_read_radix(a, negStr, 10);
  45163. if (ret != MP_VAL)
  45164. return WC_TEST_RET_ENC_EC(ret);
  45165. #endif /* WOLFSSL_SP_MATH_ALL */
  45166. #endif /* WOLFSSL_SP_INT_NEGATIVE */
  45167. #endif
  45168. #ifndef WOLFSSL_SP_MATH_ALL
  45169. /* Radix 10 only supported with ALL. */
  45170. ret = mp_read_radix(a, decStr, 10);
  45171. if (ret != MP_VAL)
  45172. return WC_TEST_RET_ENC_EC(ret);
  45173. #endif
  45174. /* Radix 8 not supported SP_INT. */
  45175. ret = mp_read_radix(a, "0123", 8);
  45176. if (ret != MP_VAL)
  45177. return WC_TEST_RET_ENC_EC(ret);
  45178. ret = mp_count_bits(NULL);
  45179. if (ret != 0)
  45180. return WC_TEST_RET_ENC_EC(ret);
  45181. ret = mp_is_bit_set(NULL, 0);
  45182. if (ret != 0)
  45183. return WC_TEST_RET_ENC_EC(ret);
  45184. ret = mp_leading_bit(NULL);
  45185. if (ret != 0)
  45186. return WC_TEST_RET_ENC_EC(ret);
  45187. mp_zero(a);
  45188. ret = mp_leading_bit(a);
  45189. if (ret != 0)
  45190. return WC_TEST_RET_ENC_EC(ret);
  45191. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  45192. defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || \
  45193. !defined(NO_RSA)
  45194. ret = mp_set_bit(NULL, 1);
  45195. if (ret != MP_VAL)
  45196. return WC_TEST_RET_ENC_EC(ret);
  45197. #endif
  45198. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  45199. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  45200. ret = mp_to_unsigned_bin(NULL, NULL);
  45201. if (ret != MP_VAL)
  45202. return WC_TEST_RET_ENC_EC(ret);
  45203. ret = mp_to_unsigned_bin(a, NULL);
  45204. if (ret != MP_VAL)
  45205. return WC_TEST_RET_ENC_EC(ret);
  45206. ret = mp_to_unsigned_bin(NULL, buffer);
  45207. if (ret != MP_VAL)
  45208. return WC_TEST_RET_ENC_EC(ret);
  45209. #endif
  45210. ret = mp_to_unsigned_bin_len(NULL, NULL, 1);
  45211. if (ret != MP_VAL)
  45212. return WC_TEST_RET_ENC_EC(ret);
  45213. ret = mp_to_unsigned_bin_len(a, NULL, 1);
  45214. if (ret != MP_VAL)
  45215. return WC_TEST_RET_ENC_EC(ret);
  45216. ret = mp_to_unsigned_bin_len(NULL, buffer, 1);
  45217. if (ret != MP_VAL)
  45218. return WC_TEST_RET_ENC_EC(ret);
  45219. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  45220. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  45221. ret = mp_to_unsigned_bin_at_pos(0, NULL, NULL);
  45222. if (ret != MP_VAL)
  45223. return WC_TEST_RET_ENC_EC(ret);
  45224. ret = mp_to_unsigned_bin_at_pos(0, a, NULL);
  45225. if (ret != MP_VAL)
  45226. return WC_TEST_RET_ENC_EC(ret);
  45227. ret = mp_to_unsigned_bin_at_pos(0, NULL, buffer);
  45228. if (ret != MP_VAL)
  45229. return WC_TEST_RET_ENC_EC(ret);
  45230. ret = mp_to_unsigned_bin_at_pos(0, a, buffer);
  45231. if (ret != MP_OKAY)
  45232. return WC_TEST_RET_ENC_EC(ret);
  45233. #endif
  45234. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  45235. ret = mp_copy(NULL, NULL);
  45236. if (ret != MP_VAL)
  45237. return WC_TEST_RET_ENC_EC(ret);
  45238. ret = mp_copy(a, NULL);
  45239. if (ret != MP_VAL)
  45240. return WC_TEST_RET_ENC_EC(ret);
  45241. ret = mp_copy(NULL, b);
  45242. if (ret != MP_VAL)
  45243. return WC_TEST_RET_ENC_EC(ret);
  45244. #endif
  45245. #if defined(WOLFSSL_KEY_GEN) || !defined(NO_DH)
  45246. ret = sp_2expt(NULL, 1);
  45247. if (ret != MP_VAL)
  45248. return WC_TEST_RET_ENC_EC(ret);
  45249. #endif
  45250. ret = mp_set(NULL, 0);
  45251. if (ret != MP_VAL)
  45252. return WC_TEST_RET_ENC_EC(ret);
  45253. ret = mp_cmp_d(NULL, 0);
  45254. if (ret != MP_LT)
  45255. return WC_TEST_RET_ENC_EC(ret);
  45256. ret = mp_cmp(NULL, NULL);
  45257. if (ret != MP_EQ)
  45258. return WC_TEST_RET_ENC_NC;
  45259. ret = mp_cmp(a, NULL);
  45260. if (ret != MP_GT)
  45261. return WC_TEST_RET_ENC_NC;
  45262. ret = mp_cmp(NULL, b);
  45263. if (ret != MP_LT)
  45264. return WC_TEST_RET_ENC_NC;
  45265. #ifdef WOLFSSL_SP_MATH_ALL
  45266. mp_rshd(NULL, 1);
  45267. #endif
  45268. mp_zero(NULL);
  45269. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  45270. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  45271. ret = mp_lshd(NULL, 0);
  45272. if (ret != MP_VAL)
  45273. return WC_TEST_RET_ENC_EC(ret);
  45274. ret = mp_lshd(a, SP_INT_DIGITS + 1);
  45275. if (ret != MP_VAL)
  45276. return WC_TEST_RET_ENC_EC(ret);
  45277. #endif
  45278. #if defined(WOLFSSL_SP_MATH_ALL)
  45279. ret = mp_div(NULL, NULL, a, b);
  45280. if (ret != MP_VAL)
  45281. return WC_TEST_RET_ENC_EC(ret);
  45282. ret = mp_div(a, NULL, a, b);
  45283. if (ret != MP_VAL)
  45284. return WC_TEST_RET_ENC_EC(ret);
  45285. ret = mp_div(NULL, b, a, b);
  45286. if (ret != MP_VAL)
  45287. return WC_TEST_RET_ENC_EC(ret);
  45288. ret = mp_div(a, b, NULL, NULL);
  45289. if (ret != MP_VAL)
  45290. return WC_TEST_RET_ENC_EC(ret);
  45291. #endif
  45292. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  45293. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  45294. ret = mp_mod(NULL, NULL, NULL);
  45295. if (ret != MP_VAL)
  45296. return WC_TEST_RET_ENC_EC(ret);
  45297. ret = mp_mod(a, NULL, NULL);
  45298. if (ret != MP_VAL)
  45299. return WC_TEST_RET_ENC_EC(ret);
  45300. ret = mp_mod(NULL, b, NULL);
  45301. if (ret != MP_VAL)
  45302. return WC_TEST_RET_ENC_EC(ret);
  45303. ret = mp_mod(NULL, NULL, r);
  45304. if (ret != MP_VAL)
  45305. return WC_TEST_RET_ENC_EC(ret);
  45306. ret = mp_mod(a, b, NULL);
  45307. if (ret != MP_VAL)
  45308. return WC_TEST_RET_ENC_EC(ret);
  45309. ret = mp_mod(a, NULL, r);
  45310. if (ret != MP_VAL)
  45311. return WC_TEST_RET_ENC_EC(ret);
  45312. ret = mp_mod(NULL, b, r);
  45313. if (ret != MP_VAL)
  45314. return WC_TEST_RET_ENC_EC(ret);
  45315. #endif
  45316. #if !defined(NO_RSA) || defined(WOLFSSL_SP_MATH_ALL)
  45317. ret = mp_set_int(NULL, 0);
  45318. if (ret != MP_VAL)
  45319. return WC_TEST_RET_ENC_EC(ret);
  45320. #endif
  45321. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  45322. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  45323. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  45324. if (ret != MP_VAL)
  45325. return WC_TEST_RET_ENC_EC(ret);
  45326. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  45327. if (ret != MP_VAL)
  45328. return WC_TEST_RET_ENC_EC(ret);
  45329. ret = mp_exptmod_ex(NULL, a, 1, NULL, NULL);
  45330. if (ret != MP_VAL)
  45331. return WC_TEST_RET_ENC_EC(ret);
  45332. ret = mp_exptmod_ex(NULL, NULL, 1, a, NULL);
  45333. if (ret != MP_VAL)
  45334. return WC_TEST_RET_ENC_EC(ret);
  45335. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  45336. if (ret != MP_VAL)
  45337. return WC_TEST_RET_ENC_EC(ret);
  45338. ret = mp_exptmod_ex(a, a, 1, a, NULL);
  45339. if (ret != MP_VAL)
  45340. return WC_TEST_RET_ENC_EC(ret);
  45341. ret = mp_exptmod_ex(a, a, 1, NULL, a);
  45342. if (ret != MP_VAL)
  45343. return WC_TEST_RET_ENC_EC(ret);
  45344. ret = mp_exptmod_ex(a, NULL, 1, a, a);
  45345. if (ret != MP_VAL)
  45346. return WC_TEST_RET_ENC_EC(ret);
  45347. ret = mp_exptmod_ex(NULL, a, 1, a, a);
  45348. if (ret != MP_VAL)
  45349. return WC_TEST_RET_ENC_EC(ret);
  45350. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  45351. if (ret != MP_VAL)
  45352. return WC_TEST_RET_ENC_EC(ret);
  45353. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  45354. if (ret != MP_VAL)
  45355. return WC_TEST_RET_ENC_EC(ret);
  45356. ret = mp_exptmod_nct(NULL, a, NULL, NULL);
  45357. if (ret != MP_VAL)
  45358. return WC_TEST_RET_ENC_EC(ret);
  45359. ret = mp_exptmod_nct(NULL, NULL, a, NULL);
  45360. if (ret != MP_VAL)
  45361. return WC_TEST_RET_ENC_EC(ret);
  45362. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  45363. if (ret != MP_VAL)
  45364. return WC_TEST_RET_ENC_EC(ret);
  45365. ret = mp_exptmod_nct(a, a, a, NULL);
  45366. if (ret != MP_VAL)
  45367. return WC_TEST_RET_ENC_EC(ret);
  45368. ret = mp_exptmod_nct(a, a, NULL, a);
  45369. if (ret != MP_VAL)
  45370. return WC_TEST_RET_ENC_EC(ret);
  45371. ret = mp_exptmod_nct(a, NULL, a, a);
  45372. if (ret != MP_VAL)
  45373. return WC_TEST_RET_ENC_EC(ret);
  45374. ret = mp_exptmod_nct(NULL, a, a, a);
  45375. if (ret != MP_VAL)
  45376. return WC_TEST_RET_ENC_EC(ret);
  45377. #endif
  45378. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  45379. !defined(WC_NO_RNG)
  45380. ret = mp_rand_prime(NULL, 32, NULL, NULL);
  45381. if (ret != MP_VAL)
  45382. return WC_TEST_RET_ENC_EC(ret);
  45383. ret = mp_rand_prime(a, 32, NULL, NULL);
  45384. if (ret != MP_VAL)
  45385. return WC_TEST_RET_ENC_EC(ret);
  45386. ret = mp_rand_prime(NULL, 32, rng, NULL);
  45387. if (ret != MP_VAL)
  45388. return WC_TEST_RET_ENC_EC(ret);
  45389. ret = mp_rand_prime(a, 0, rng, NULL);
  45390. if (ret != MP_VAL)
  45391. return WC_TEST_RET_ENC_EC(ret);
  45392. #endif
  45393. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  45394. ret = mp_mul(NULL, NULL, NULL);
  45395. if (ret != MP_VAL)
  45396. return WC_TEST_RET_ENC_EC(ret);
  45397. ret = mp_mul(a, NULL, NULL);
  45398. if (ret != MP_VAL)
  45399. return WC_TEST_RET_ENC_EC(ret);
  45400. ret = mp_mul(NULL, b, NULL);
  45401. if (ret != MP_VAL)
  45402. return WC_TEST_RET_ENC_EC(ret);
  45403. ret = mp_mul(NULL, NULL, r);
  45404. if (ret != MP_VAL)
  45405. return WC_TEST_RET_ENC_EC(ret);
  45406. ret = mp_mul(a, b, NULL);
  45407. if (ret != MP_VAL)
  45408. return WC_TEST_RET_ENC_EC(ret);
  45409. ret = mp_mul(a, NULL, r);
  45410. if (ret != MP_VAL)
  45411. return WC_TEST_RET_ENC_EC(ret);
  45412. ret = mp_mul(NULL, b, r);
  45413. if (ret != MP_VAL)
  45414. return WC_TEST_RET_ENC_EC(ret);
  45415. #endif
  45416. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  45417. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  45418. ret = mp_sqr(NULL, NULL);
  45419. if (ret != MP_VAL)
  45420. return WC_TEST_RET_ENC_EC(ret);
  45421. ret = mp_sqr(a, NULL);
  45422. if (ret != MP_VAL)
  45423. return WC_TEST_RET_ENC_EC(ret);
  45424. ret = mp_sqr(NULL, r);
  45425. if (ret != MP_VAL)
  45426. return WC_TEST_RET_ENC_EC(ret);
  45427. #endif
  45428. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  45429. ret = mp_sqrmod(NULL, NULL, NULL);
  45430. if (ret != MP_VAL)
  45431. return WC_TEST_RET_ENC_EC(ret);
  45432. ret = mp_sqrmod(a, NULL, NULL);
  45433. if (ret != MP_VAL)
  45434. return WC_TEST_RET_ENC_EC(ret);
  45435. ret = mp_sqrmod(NULL, a, NULL);
  45436. if (ret != MP_VAL)
  45437. return WC_TEST_RET_ENC_EC(ret);
  45438. ret = mp_sqrmod(NULL, NULL, a);
  45439. if (ret != MP_VAL)
  45440. return WC_TEST_RET_ENC_EC(ret);
  45441. ret = mp_sqrmod(a, b, NULL);
  45442. if (ret != MP_VAL)
  45443. return WC_TEST_RET_ENC_EC(ret);
  45444. ret = mp_sqrmod(a, NULL, b);
  45445. if (ret != MP_VAL)
  45446. return WC_TEST_RET_ENC_EC(ret);
  45447. ret = mp_sqrmod(NULL, a, b);
  45448. if (ret != MP_VAL)
  45449. return WC_TEST_RET_ENC_EC(ret);
  45450. ret = mp_mulmod(NULL, NULL, NULL, NULL);
  45451. if (ret != MP_VAL)
  45452. return WC_TEST_RET_ENC_EC(ret);
  45453. ret = mp_mulmod(a, NULL, NULL, NULL);
  45454. if (ret != MP_VAL)
  45455. return WC_TEST_RET_ENC_EC(ret);
  45456. ret = mp_mulmod(NULL, a, NULL, NULL);
  45457. if (ret != MP_VAL)
  45458. return WC_TEST_RET_ENC_EC(ret);
  45459. ret = mp_mulmod(NULL, NULL, a, NULL);
  45460. if (ret != MP_VAL)
  45461. return WC_TEST_RET_ENC_EC(ret);
  45462. ret = mp_mulmod(NULL, NULL, NULL, a);
  45463. if (ret != MP_VAL)
  45464. return WC_TEST_RET_ENC_EC(ret);
  45465. ret = mp_mulmod(a, b, b, NULL);
  45466. if (ret != MP_VAL)
  45467. return WC_TEST_RET_ENC_EC(ret);
  45468. ret = mp_mulmod(a, b, NULL, a);
  45469. if (ret != MP_VAL)
  45470. return WC_TEST_RET_ENC_EC(ret);
  45471. ret = mp_mulmod(a, NULL, b, a);
  45472. if (ret != MP_VAL)
  45473. return WC_TEST_RET_ENC_EC(ret);
  45474. ret = mp_mulmod(NULL, b, b, a);
  45475. if (ret != MP_VAL)
  45476. return WC_TEST_RET_ENC_EC(ret);
  45477. #endif
  45478. #if !defined(NO_PWDBASED) || defined(WOLFSSL_KEY_GEN) || !defined(NO_DH) || \
  45479. !defined(NO_RSA) || !defined(NO_DSA)
  45480. ret = mp_add_d(NULL, 1, NULL);
  45481. if (ret != MP_VAL)
  45482. return WC_TEST_RET_ENC_EC(ret);
  45483. ret = mp_add_d(a, 1, NULL);
  45484. if (ret != MP_VAL)
  45485. return WC_TEST_RET_ENC_EC(ret);
  45486. ret = mp_add_d(NULL, 1, b);
  45487. if (ret != MP_VAL)
  45488. return WC_TEST_RET_ENC_EC(ret);
  45489. #endif
  45490. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  45491. !defined(NO_DH) || defined(HAVE_ECC) || !defined(NO_DSA)
  45492. ret = mp_sub_d(NULL, 1, NULL);
  45493. if (ret != MP_VAL)
  45494. return WC_TEST_RET_ENC_EC(ret);
  45495. ret = mp_sub_d(a, 1, NULL);
  45496. if (ret != MP_VAL)
  45497. return WC_TEST_RET_ENC_EC(ret);
  45498. ret = mp_sub_d(NULL, 1, b);
  45499. if (ret != MP_VAL)
  45500. return WC_TEST_RET_ENC_EC(ret);
  45501. #endif
  45502. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  45503. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  45504. ret = mp_div_d(NULL, 0, NULL, NULL);
  45505. if (ret != MP_VAL)
  45506. return WC_TEST_RET_ENC_EC(ret);
  45507. ret = mp_div_d(a, 0, NULL, NULL);
  45508. if (ret != MP_VAL)
  45509. return WC_TEST_RET_ENC_EC(ret);
  45510. ret = mp_div_d(NULL, 1, NULL, NULL);
  45511. if (ret != MP_VAL)
  45512. return WC_TEST_RET_ENC_EC(ret);
  45513. #endif
  45514. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  45515. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  45516. ret = mp_mod_d(NULL, 0, NULL);
  45517. if (ret != MP_VAL)
  45518. return WC_TEST_RET_ENC_EC(ret);
  45519. ret = mp_mod_d(a, 0, NULL);
  45520. if (ret != MP_VAL)
  45521. return WC_TEST_RET_ENC_EC(ret);
  45522. ret = mp_mod_d(NULL, 0, &rd);
  45523. if (ret != MP_VAL)
  45524. return WC_TEST_RET_ENC_EC(ret);
  45525. #endif
  45526. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  45527. ret = mp_gcd(NULL, NULL, NULL);
  45528. if (ret != MP_VAL)
  45529. return WC_TEST_RET_ENC_EC(ret);
  45530. ret = mp_gcd(a, NULL, NULL);
  45531. if (ret != MP_VAL)
  45532. return WC_TEST_RET_ENC_EC(ret);
  45533. ret = mp_gcd(NULL, a, NULL);
  45534. if (ret != MP_VAL)
  45535. return WC_TEST_RET_ENC_EC(ret);
  45536. ret = mp_gcd(NULL, NULL, a);
  45537. if (ret != MP_VAL)
  45538. return WC_TEST_RET_ENC_EC(ret);
  45539. ret = mp_gcd(a, b, NULL);
  45540. if (ret != MP_VAL)
  45541. return WC_TEST_RET_ENC_EC(ret);
  45542. ret = mp_gcd(a, NULL, b);
  45543. if (ret != MP_VAL)
  45544. return WC_TEST_RET_ENC_EC(ret);
  45545. ret = mp_gcd(NULL, a, b);
  45546. if (ret != MP_VAL)
  45547. return WC_TEST_RET_ENC_EC(ret);
  45548. #endif
  45549. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  45550. ret = mp_div_2_mod_ct(NULL, NULL, NULL);
  45551. if (ret != MP_VAL)
  45552. return WC_TEST_RET_ENC_EC(ret);
  45553. ret = mp_div_2_mod_ct(a, NULL, NULL);
  45554. if (ret != MP_VAL)
  45555. return WC_TEST_RET_ENC_EC(ret);
  45556. ret = mp_div_2_mod_ct(NULL, b, NULL);
  45557. if (ret != MP_VAL)
  45558. return WC_TEST_RET_ENC_EC(ret);
  45559. ret = mp_div_2_mod_ct(NULL, NULL, a);
  45560. if (ret != MP_VAL)
  45561. return WC_TEST_RET_ENC_EC(ret);
  45562. ret = mp_div_2_mod_ct(a, b, NULL);
  45563. if (ret != MP_VAL)
  45564. return WC_TEST_RET_ENC_EC(ret);
  45565. ret = mp_div_2_mod_ct(a, b, NULL);
  45566. if (ret != MP_VAL)
  45567. return WC_TEST_RET_ENC_EC(ret);
  45568. ret = mp_div_2_mod_ct(NULL, b, a);
  45569. if (ret != MP_VAL)
  45570. return WC_TEST_RET_ENC_EC(ret);
  45571. ret = mp_div_2(NULL, NULL);
  45572. if (ret != MP_VAL)
  45573. return WC_TEST_RET_ENC_EC(ret);
  45574. ret = mp_div_2(a, NULL);
  45575. if (ret != MP_VAL)
  45576. return WC_TEST_RET_ENC_EC(ret);
  45577. ret = mp_div_2(NULL, a);
  45578. if (ret != MP_VAL)
  45579. return WC_TEST_RET_ENC_EC(ret);
  45580. #endif
  45581. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  45582. defined(HAVE_ECC) || !defined(NO_DSA) || defined(OPENSSL_EXTRA)
  45583. ret = mp_invmod(NULL, NULL, NULL);
  45584. if (ret != MP_VAL)
  45585. return WC_TEST_RET_ENC_EC(ret);
  45586. ret = mp_invmod(a, NULL, NULL);
  45587. if (ret != MP_VAL)
  45588. return WC_TEST_RET_ENC_EC(ret);
  45589. ret = mp_invmod(NULL, b, NULL);
  45590. if (ret != MP_VAL)
  45591. return WC_TEST_RET_ENC_EC(ret);
  45592. ret = mp_invmod(NULL, NULL, a);
  45593. if (ret != MP_VAL)
  45594. return WC_TEST_RET_ENC_EC(ret);
  45595. ret = mp_invmod(a, b, NULL);
  45596. if (ret != MP_VAL)
  45597. return WC_TEST_RET_ENC_EC(ret);
  45598. ret = mp_invmod(a, NULL, a);
  45599. if (ret != MP_VAL)
  45600. return WC_TEST_RET_ENC_EC(ret);
  45601. ret = mp_invmod(NULL, b, a);
  45602. if (ret != MP_VAL)
  45603. return WC_TEST_RET_ENC_EC(ret);
  45604. #endif
  45605. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  45606. ret = mp_invmod_mont_ct(NULL, NULL, NULL, 1);
  45607. if (ret != MP_VAL)
  45608. return WC_TEST_RET_ENC_EC(ret);
  45609. ret = mp_invmod_mont_ct(a, NULL, NULL, 1);
  45610. if (ret != MP_VAL)
  45611. return WC_TEST_RET_ENC_EC(ret);
  45612. ret = mp_invmod_mont_ct(NULL, b, NULL, 1);
  45613. if (ret != MP_VAL)
  45614. return WC_TEST_RET_ENC_EC(ret);
  45615. ret = mp_invmod_mont_ct(NULL, NULL, a, 1);
  45616. if (ret != MP_VAL)
  45617. return WC_TEST_RET_ENC_EC(ret);
  45618. ret = mp_invmod_mont_ct(a, b, NULL, 1);
  45619. if (ret != MP_VAL)
  45620. return WC_TEST_RET_ENC_EC(ret);
  45621. ret = mp_invmod_mont_ct(a, NULL, a, 1);
  45622. if (ret != MP_VAL)
  45623. return WC_TEST_RET_ENC_EC(ret);
  45624. ret = mp_invmod_mont_ct(NULL, b, a, 1);
  45625. if (ret != MP_VAL)
  45626. return WC_TEST_RET_ENC_EC(ret);
  45627. #endif
  45628. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  45629. ret = mp_lcm(NULL, NULL, NULL);
  45630. if (ret != MP_VAL)
  45631. return WC_TEST_RET_ENC_EC(ret);
  45632. ret = mp_lcm(a, NULL, NULL);
  45633. if (ret != MP_VAL)
  45634. return WC_TEST_RET_ENC_EC(ret);
  45635. ret = mp_lcm(NULL, b, NULL);
  45636. if (ret != MP_VAL)
  45637. return WC_TEST_RET_ENC_EC(ret);
  45638. ret = mp_lcm(NULL, NULL, a);
  45639. if (ret != MP_VAL)
  45640. return WC_TEST_RET_ENC_EC(ret);
  45641. ret = mp_lcm(a, b, NULL);
  45642. if (ret != MP_VAL)
  45643. return WC_TEST_RET_ENC_EC(ret);
  45644. ret = mp_lcm(a, NULL, a);
  45645. if (ret != MP_VAL)
  45646. return WC_TEST_RET_ENC_EC(ret);
  45647. ret = mp_lcm(NULL, b, a);
  45648. if (ret != MP_VAL)
  45649. return WC_TEST_RET_ENC_EC(ret);
  45650. #endif
  45651. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  45652. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  45653. if (ret != MP_VAL)
  45654. return WC_TEST_RET_ENC_EC(ret);
  45655. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  45656. if (ret != MP_VAL)
  45657. return WC_TEST_RET_ENC_EC(ret);
  45658. ret = mp_exptmod_ex(NULL, b, 1, NULL, NULL);
  45659. if (ret != MP_VAL)
  45660. return WC_TEST_RET_ENC_EC(ret);
  45661. ret = mp_exptmod_ex(NULL, NULL, 1, b, NULL);
  45662. if (ret != MP_VAL)
  45663. return WC_TEST_RET_ENC_EC(ret);
  45664. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  45665. if (ret != MP_VAL)
  45666. return WC_TEST_RET_ENC_EC(ret);
  45667. ret = mp_exptmod_ex(a, b, 1, b, NULL);
  45668. if (ret != MP_VAL)
  45669. return WC_TEST_RET_ENC_EC(ret);
  45670. ret = mp_exptmod_ex(a, b, 1, NULL, a);
  45671. if (ret != MP_VAL)
  45672. return WC_TEST_RET_ENC_EC(ret);
  45673. ret = mp_exptmod_ex(a, NULL, 1, b, a);
  45674. if (ret != MP_VAL)
  45675. return WC_TEST_RET_ENC_EC(ret);
  45676. ret = mp_exptmod_ex(NULL, b, 1, b, a);
  45677. if (ret != MP_VAL)
  45678. return WC_TEST_RET_ENC_EC(ret);
  45679. ret = mp_exptmod(NULL, NULL, NULL, NULL);
  45680. if (ret != MP_VAL)
  45681. return WC_TEST_RET_ENC_EC(ret);
  45682. ret = mp_exptmod(a, NULL, NULL, NULL);
  45683. if (ret != MP_VAL)
  45684. return WC_TEST_RET_ENC_EC(ret);
  45685. ret = mp_exptmod(NULL, b, NULL, NULL);
  45686. if (ret != MP_VAL)
  45687. return WC_TEST_RET_ENC_EC(ret);
  45688. ret = mp_exptmod(NULL, NULL, b, NULL);
  45689. if (ret != MP_VAL)
  45690. return WC_TEST_RET_ENC_EC(ret);
  45691. ret = mp_exptmod(NULL, NULL, NULL, a);
  45692. if (ret != MP_VAL)
  45693. return WC_TEST_RET_ENC_EC(ret);
  45694. ret = mp_exptmod(a, b, b, NULL);
  45695. if (ret != MP_VAL)
  45696. return WC_TEST_RET_ENC_EC(ret);
  45697. ret = mp_exptmod(a, b, NULL, a);
  45698. if (ret != MP_VAL)
  45699. return WC_TEST_RET_ENC_EC(ret);
  45700. ret = mp_exptmod(a, NULL, b, a);
  45701. if (ret != MP_VAL)
  45702. return WC_TEST_RET_ENC_EC(ret);
  45703. ret = mp_exptmod(NULL, b, b, a);
  45704. if (ret != MP_VAL)
  45705. return WC_TEST_RET_ENC_EC(ret);
  45706. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  45707. if (ret != MP_VAL)
  45708. return WC_TEST_RET_ENC_EC(ret);
  45709. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  45710. if (ret != MP_VAL)
  45711. return WC_TEST_RET_ENC_EC(ret);
  45712. ret = mp_exptmod_nct(NULL, b, NULL, NULL);
  45713. if (ret != MP_VAL)
  45714. return WC_TEST_RET_ENC_EC(ret);
  45715. ret = mp_exptmod_nct(NULL, NULL, b, NULL);
  45716. if (ret != MP_VAL)
  45717. return WC_TEST_RET_ENC_EC(ret);
  45718. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  45719. if (ret != MP_VAL)
  45720. return WC_TEST_RET_ENC_EC(ret);
  45721. ret = mp_exptmod_nct(a, b, b, NULL);
  45722. if (ret != MP_VAL)
  45723. return WC_TEST_RET_ENC_EC(ret);
  45724. ret = mp_exptmod_nct(a, b, NULL, a);
  45725. if (ret != MP_VAL)
  45726. return WC_TEST_RET_ENC_EC(ret);
  45727. ret = mp_exptmod_nct(a, NULL, b, a);
  45728. if (ret != MP_VAL)
  45729. return WC_TEST_RET_ENC_EC(ret);
  45730. ret = mp_exptmod_nct(NULL, b, b, a);
  45731. if (ret != MP_VAL)
  45732. return WC_TEST_RET_ENC_EC(ret);
  45733. #endif
  45734. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  45735. ret = mp_cnt_lsb(NULL);
  45736. if (ret != 0)
  45737. return WC_TEST_RET_ENC_EC(ret);
  45738. #endif
  45739. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  45740. ret = mp_prime_is_prime(NULL, 1, NULL);
  45741. if (ret != MP_VAL)
  45742. return WC_TEST_RET_ENC_EC(ret);
  45743. ret = mp_prime_is_prime(a, 1, NULL);
  45744. if (ret != MP_VAL)
  45745. return WC_TEST_RET_ENC_EC(ret);
  45746. ret = mp_prime_is_prime(NULL, 1, &result);
  45747. if (ret != MP_VAL)
  45748. return WC_TEST_RET_ENC_EC(ret);
  45749. ret = mp_prime_is_prime(a, 0, &result);
  45750. if (ret != MP_VAL)
  45751. return WC_TEST_RET_ENC_EC(ret);
  45752. ret = mp_prime_is_prime(a, 1024, &result);
  45753. if (ret != MP_VAL)
  45754. return WC_TEST_RET_ENC_EC(ret);
  45755. ret = mp_prime_is_prime_ex(NULL, 1, NULL, NULL);
  45756. if (ret != MP_VAL)
  45757. return WC_TEST_RET_ENC_EC(ret);
  45758. ret = mp_prime_is_prime_ex(a, 1, NULL, NULL);
  45759. if (ret != MP_VAL)
  45760. return WC_TEST_RET_ENC_EC(ret);
  45761. ret = mp_prime_is_prime_ex(NULL, 1, &result, NULL);
  45762. if (ret != MP_VAL)
  45763. return WC_TEST_RET_ENC_EC(ret);
  45764. ret = mp_prime_is_prime_ex(NULL, 1, NULL, rng);
  45765. if (ret != MP_VAL)
  45766. return WC_TEST_RET_ENC_EC(ret);
  45767. ret = mp_prime_is_prime_ex(a, 1, &result, NULL);
  45768. if (ret != MP_VAL)
  45769. return WC_TEST_RET_ENC_EC(ret);
  45770. ret = mp_prime_is_prime_ex(a, 1, NULL, rng);
  45771. if (ret != MP_VAL)
  45772. return WC_TEST_RET_ENC_EC(ret);
  45773. ret = mp_prime_is_prime_ex(NULL, 1, &result, rng);
  45774. if (ret != MP_VAL)
  45775. return WC_TEST_RET_ENC_EC(ret);
  45776. #endif
  45777. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || !defined(NO_DSA)
  45778. ret = mp_exch(NULL, NULL);
  45779. if (ret != MP_VAL)
  45780. return WC_TEST_RET_ENC_EC(ret);
  45781. ret = mp_exch(a, NULL);
  45782. if (ret != MP_VAL)
  45783. return WC_TEST_RET_ENC_EC(ret);
  45784. ret = mp_exch(NULL, b);
  45785. if (ret != MP_VAL)
  45786. return WC_TEST_RET_ENC_EC(ret);
  45787. #endif
  45788. #if (defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)) || \
  45789. defined(WOLFSSL_SP_MATH_ALL)
  45790. ret = mp_mul_d(NULL, 1, NULL);
  45791. if (ret != MP_VAL)
  45792. return WC_TEST_RET_ENC_EC(ret);
  45793. ret = mp_mul_d(a, 1, NULL);
  45794. if (ret != MP_VAL)
  45795. return WC_TEST_RET_ENC_EC(ret);
  45796. ret = mp_mul_d(NULL, 1, b);
  45797. if (ret != MP_VAL)
  45798. return WC_TEST_RET_ENC_EC(ret);
  45799. #endif
  45800. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  45801. ret = mp_add(NULL, NULL, NULL);
  45802. if (ret != MP_VAL)
  45803. return WC_TEST_RET_ENC_EC(ret);
  45804. ret = mp_add(a, NULL, NULL);
  45805. if (ret != MP_VAL)
  45806. return WC_TEST_RET_ENC_EC(ret);
  45807. ret = mp_add(NULL, b, NULL);
  45808. if (ret != MP_VAL)
  45809. return WC_TEST_RET_ENC_EC(ret);
  45810. ret = mp_add(NULL, NULL, r);
  45811. if (ret != MP_VAL)
  45812. return WC_TEST_RET_ENC_EC(ret);
  45813. ret = mp_add(a, b, NULL);
  45814. if (ret != MP_VAL)
  45815. return WC_TEST_RET_ENC_EC(ret);
  45816. ret = mp_add(a, NULL, r);
  45817. if (ret != MP_VAL)
  45818. return WC_TEST_RET_ENC_EC(ret);
  45819. ret = mp_add(NULL, b, r);
  45820. if (ret != MP_VAL)
  45821. return WC_TEST_RET_ENC_EC(ret);
  45822. #endif
  45823. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  45824. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  45825. ret = mp_sub(NULL, NULL, NULL);
  45826. if (ret != MP_VAL)
  45827. return WC_TEST_RET_ENC_EC(ret);
  45828. ret = mp_sub(a, NULL, NULL);
  45829. if (ret != MP_VAL)
  45830. return WC_TEST_RET_ENC_EC(ret);
  45831. ret = mp_sub(NULL, b, NULL);
  45832. if (ret != MP_VAL)
  45833. return WC_TEST_RET_ENC_EC(ret);
  45834. ret = mp_sub(NULL, NULL, r);
  45835. if (ret != MP_VAL)
  45836. return WC_TEST_RET_ENC_EC(ret);
  45837. ret = mp_sub(a, b, NULL);
  45838. if (ret != MP_VAL)
  45839. return WC_TEST_RET_ENC_EC(ret);
  45840. ret = mp_sub(a, NULL, r);
  45841. if (ret != MP_VAL)
  45842. return WC_TEST_RET_ENC_EC(ret);
  45843. ret = mp_sub(NULL, b, r);
  45844. if (ret != MP_VAL)
  45845. return WC_TEST_RET_ENC_EC(ret);
  45846. #endif
  45847. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined(WOLFSSL_SP_MATH) && \
  45848. defined(WOLFSSL_CUSTOM_CURVES))
  45849. ret = mp_addmod(NULL, NULL, NULL, NULL);
  45850. if (ret != MP_VAL)
  45851. return WC_TEST_RET_ENC_EC(ret);
  45852. ret = mp_addmod(a, NULL, NULL, NULL);
  45853. if (ret != MP_VAL)
  45854. return WC_TEST_RET_ENC_EC(ret);
  45855. ret = mp_addmod(NULL, b, NULL, NULL);
  45856. if (ret != MP_VAL)
  45857. return WC_TEST_RET_ENC_EC(ret);
  45858. ret = mp_addmod(NULL, NULL, b, NULL);
  45859. if (ret != MP_VAL)
  45860. return WC_TEST_RET_ENC_EC(ret);
  45861. ret = mp_addmod(NULL, NULL, NULL, a);
  45862. if (ret != MP_VAL)
  45863. return WC_TEST_RET_ENC_EC(ret);
  45864. ret = mp_addmod(a, b, b, NULL);
  45865. if (ret != MP_VAL)
  45866. return WC_TEST_RET_ENC_EC(ret);
  45867. ret = mp_addmod(a, b, NULL, a);
  45868. if (ret != MP_VAL)
  45869. return WC_TEST_RET_ENC_EC(ret);
  45870. ret = mp_addmod(a, NULL, b, a);
  45871. if (ret != MP_VAL)
  45872. return WC_TEST_RET_ENC_EC(ret);
  45873. ret = mp_addmod(NULL, b, b, a);
  45874. if (ret != MP_VAL)
  45875. return WC_TEST_RET_ENC_EC(ret);
  45876. #endif
  45877. #ifdef WOLFSSL_SP_MATH_ALL
  45878. ret = mp_submod(NULL, NULL, NULL, NULL);
  45879. if (ret != MP_VAL)
  45880. return WC_TEST_RET_ENC_EC(ret);
  45881. ret = mp_submod(a, NULL, NULL, NULL);
  45882. if (ret != MP_VAL)
  45883. return WC_TEST_RET_ENC_EC(ret);
  45884. ret = mp_submod(NULL, b, NULL, NULL);
  45885. if (ret != MP_VAL)
  45886. return WC_TEST_RET_ENC_EC(ret);
  45887. ret = mp_submod(NULL, NULL, b, NULL);
  45888. if (ret != MP_VAL)
  45889. return WC_TEST_RET_ENC_EC(ret);
  45890. ret = mp_submod(NULL, NULL, NULL, a);
  45891. if (ret != MP_VAL)
  45892. return WC_TEST_RET_ENC_EC(ret);
  45893. ret = mp_submod(a, b, b, NULL);
  45894. if (ret != MP_VAL)
  45895. return WC_TEST_RET_ENC_EC(ret);
  45896. ret = mp_submod(a, b, NULL, a);
  45897. if (ret != MP_VAL)
  45898. return WC_TEST_RET_ENC_EC(ret);
  45899. ret = mp_submod(a, NULL, b, a);
  45900. if (ret != MP_VAL)
  45901. return WC_TEST_RET_ENC_EC(ret);
  45902. ret = mp_submod(NULL, b, b, a);
  45903. if (ret != MP_VAL)
  45904. return WC_TEST_RET_ENC_EC(ret);
  45905. #endif
  45906. #ifdef WOLFSSL_SP_MATH_ALL
  45907. ret = mp_div_2d(NULL, 1, a, b);
  45908. if (ret != MP_VAL)
  45909. return WC_TEST_RET_ENC_EC(ret);
  45910. ret = mp_mod_2d(NULL, 1, NULL);
  45911. if (ret != MP_VAL)
  45912. return WC_TEST_RET_ENC_EC(ret);
  45913. ret = mp_mod_2d(a, 1, NULL);
  45914. if (ret != MP_VAL)
  45915. return WC_TEST_RET_ENC_EC(ret);
  45916. ret = mp_mod_2d(NULL, 1, b);
  45917. if (ret != MP_VAL)
  45918. return WC_TEST_RET_ENC_EC(ret);
  45919. ret = mp_mul_2d(NULL, 1, NULL);
  45920. if (ret != MP_VAL)
  45921. return WC_TEST_RET_ENC_EC(ret);
  45922. ret = mp_mul_2d(a, 1, NULL);
  45923. if (ret != MP_VAL)
  45924. return WC_TEST_RET_ENC_EC(ret);
  45925. ret = mp_mul_2d(NULL, 1, b);
  45926. if (ret != MP_VAL)
  45927. return WC_TEST_RET_ENC_EC(ret);
  45928. #endif
  45929. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  45930. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  45931. ret = mp_montgomery_reduce(NULL, NULL, 1);
  45932. if (ret != MP_VAL)
  45933. return WC_TEST_RET_ENC_EC(ret);
  45934. ret = mp_montgomery_reduce(a, NULL, 1);
  45935. if (ret != MP_VAL)
  45936. return WC_TEST_RET_ENC_EC(ret);
  45937. ret = mp_montgomery_reduce(NULL, b, 1);
  45938. if (ret != MP_VAL)
  45939. return WC_TEST_RET_ENC_EC(ret);
  45940. mp_zero(b);
  45941. ret = mp_montgomery_reduce(a, b, 1);
  45942. if (ret != MP_VAL)
  45943. return WC_TEST_RET_ENC_EC(ret);
  45944. #endif
  45945. #ifdef WOLFSSL_SP_MATH_ALL
  45946. ret = mp_montgomery_setup(NULL, NULL);
  45947. if (ret != MP_VAL)
  45948. return WC_TEST_RET_ENC_EC(ret);
  45949. ret = mp_montgomery_setup(a, NULL);
  45950. if (ret != MP_VAL)
  45951. return WC_TEST_RET_ENC_EC(ret);
  45952. ret = mp_montgomery_setup(NULL, &rho);
  45953. if (ret != MP_VAL)
  45954. return WC_TEST_RET_ENC_EC(ret);
  45955. ret = mp_montgomery_calc_normalization(NULL, NULL);
  45956. if (ret != MP_VAL)
  45957. return WC_TEST_RET_ENC_EC(ret);
  45958. ret = mp_montgomery_calc_normalization(a, NULL);
  45959. if (ret != MP_VAL)
  45960. return WC_TEST_RET_ENC_EC(ret);
  45961. ret = mp_montgomery_calc_normalization(NULL, b);
  45962. if (ret != MP_VAL)
  45963. return WC_TEST_RET_ENC_EC(ret);
  45964. #endif
  45965. ret = mp_unsigned_bin_size(NULL);
  45966. if (ret != 0)
  45967. return WC_TEST_RET_ENC_EC(ret);
  45968. #if defined(WC_MP_TO_RADIX) || defined(WOLFSSL_SP_MATH_ALL)
  45969. ret = mp_tohex(NULL, NULL);
  45970. if (ret != MP_VAL)
  45971. return WC_TEST_RET_ENC_EC(ret);
  45972. ret = mp_tohex(a, NULL);
  45973. if (ret != MP_VAL)
  45974. return WC_TEST_RET_ENC_EC(ret);
  45975. ret = mp_tohex(NULL, hexStr);
  45976. if (ret != MP_VAL)
  45977. return WC_TEST_RET_ENC_EC(ret);
  45978. #endif
  45979. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  45980. ret = mp_todecimal(NULL, NULL);
  45981. if (ret != MP_VAL)
  45982. return WC_TEST_RET_ENC_EC(ret);
  45983. ret = mp_todecimal(a, NULL);
  45984. if (ret != MP_VAL)
  45985. return WC_TEST_RET_ENC_EC(ret);
  45986. ret = mp_todecimal(NULL, decStr);
  45987. if (ret != MP_VAL)
  45988. return WC_TEST_RET_ENC_EC(ret);
  45989. #endif
  45990. #ifdef WOLFSSL_SP_MATH_ALL
  45991. ret = mp_toradix(NULL, NULL, MP_RADIX_HEX);
  45992. if (ret != MP_VAL)
  45993. return WC_TEST_RET_ENC_EC(ret);
  45994. ret = mp_toradix(a, NULL, MP_RADIX_HEX);
  45995. if (ret != MP_VAL)
  45996. return WC_TEST_RET_ENC_EC(ret);
  45997. ret = mp_toradix(NULL, hexStr, MP_RADIX_HEX);
  45998. if (ret != MP_VAL)
  45999. return WC_TEST_RET_ENC_EC(ret);
  46000. ret = mp_toradix(a, hexStr, 3);
  46001. if (ret != MP_VAL)
  46002. return WC_TEST_RET_ENC_EC(ret);
  46003. ret = mp_radix_size(NULL, MP_RADIX_HEX, NULL);
  46004. if (ret != MP_VAL)
  46005. return WC_TEST_RET_ENC_EC(ret);
  46006. ret = mp_radix_size(a, MP_RADIX_HEX, NULL);
  46007. if (ret != MP_VAL)
  46008. return WC_TEST_RET_ENC_EC(ret);
  46009. ret = mp_radix_size(NULL, MP_RADIX_HEX, &size);
  46010. if (ret != MP_VAL)
  46011. return WC_TEST_RET_ENC_EC(ret);
  46012. ret = mp_radix_size(a, 3, &size);
  46013. if (ret != MP_VAL)
  46014. return WC_TEST_RET_ENC_EC(ret);
  46015. #endif
  46016. return 0;
  46017. }
  46018. #endif
  46019. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  46020. static wc_test_ret_t mp_test_set_is_bit(mp_int* a)
  46021. {
  46022. int i, j;
  46023. wc_test_ret_t ret;
  46024. mp_zero(a);
  46025. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  46026. if (mp_is_bit_set(a, i))
  46027. return WC_TEST_RET_ENC_NC;
  46028. for (j = 0; j < i; j++) {
  46029. if (!mp_is_bit_set(a, j))
  46030. return WC_TEST_RET_ENC_NC;
  46031. }
  46032. if (mp_set_bit(a, i) != 0)
  46033. return WC_TEST_RET_ENC_NC;
  46034. if (!mp_is_bit_set(a, i))
  46035. return WC_TEST_RET_ENC_NC;
  46036. }
  46037. mp_zero(a);
  46038. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  46039. if (mp_is_bit_set(a, i))
  46040. return WC_TEST_RET_ENC_NC;
  46041. }
  46042. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  46043. mp_zero(a);
  46044. if (mp_set_bit(a, i) != 0)
  46045. return WC_TEST_RET_ENC_NC;
  46046. for (j = 0; j < i; j++) {
  46047. if (mp_is_bit_set(a, j))
  46048. return WC_TEST_RET_ENC_NC;
  46049. }
  46050. if (!mp_is_bit_set(a, i))
  46051. return WC_TEST_RET_ENC_NC;
  46052. }
  46053. #ifdef WOLFSSL_KEY_GEN
  46054. for (i = 0; i < DIGIT_BIT * 2; i++) {
  46055. mp_set(a, 1);
  46056. ret = mp_2expt(a, i);
  46057. if (ret != 0)
  46058. return WC_TEST_RET_ENC_EC(ret);
  46059. for (j = 0; j < i; j++) {
  46060. if (mp_is_bit_set(a, j))
  46061. return WC_TEST_RET_ENC_NC;
  46062. }
  46063. if (!mp_is_bit_set(a, i))
  46064. return WC_TEST_RET_ENC_NC;
  46065. }
  46066. #endif
  46067. #ifdef WOLFSSL_SP_MATH
  46068. mp_zero(a);
  46069. for (j = 1; j <= 3; j++) {
  46070. i = SP_INT_MAX_BITS - j;
  46071. if (mp_is_bit_set(a, i))
  46072. return WC_TEST_RET_ENC_NC;
  46073. if (mp_set_bit(a, i) != 0)
  46074. return WC_TEST_RET_ENC_NC;
  46075. if (!mp_is_bit_set(a, i))
  46076. return WC_TEST_RET_ENC_NC;
  46077. #ifdef WOLFSSL_KEY_GEN
  46078. ret = mp_2expt(a, i);
  46079. if (ret != 0)
  46080. return WC_TEST_RET_ENC_EC(ret);
  46081. if (!mp_is_bit_set(a, i))
  46082. return WC_TEST_RET_ENC_NC;
  46083. #endif
  46084. }
  46085. mp_zero(a);
  46086. for (j = 0; j <= 3; j++) {
  46087. i = SP_INT_MAX_BITS + j;
  46088. if (mp_is_bit_set(a, i))
  46089. return WC_TEST_RET_ENC_NC;
  46090. if (mp_set_bit(a, i) != MP_VAL)
  46091. return WC_TEST_RET_ENC_NC;
  46092. #ifdef WOLFSSL_KEY_GEN
  46093. ret = mp_2expt(a, i);
  46094. if (ret != MP_VAL)
  46095. return WC_TEST_RET_ENC_EC(ret);
  46096. #endif
  46097. }
  46098. #endif
  46099. (void)ret;
  46100. return 0;
  46101. }
  46102. #endif /* !WOLFSSL_SP_MATH || WOLFSSL_SP_MATH_ALL */
  46103. static wc_test_ret_t mp_test_cmp(mp_int* a, mp_int* b)
  46104. {
  46105. wc_test_ret_t ret;
  46106. mp_zero(a);
  46107. mp_zero(b);
  46108. ret = mp_cmp_d(a, 0);
  46109. if (ret != MP_EQ)
  46110. return WC_TEST_RET_ENC_EC(ret);
  46111. ret = mp_cmp_d(a, 1);
  46112. if (ret != MP_LT)
  46113. return WC_TEST_RET_ENC_EC(ret);
  46114. ret = mp_cmp(a, b);
  46115. if (ret != MP_EQ)
  46116. return WC_TEST_RET_ENC_NC;
  46117. mp_set(a, 1);
  46118. ret = mp_cmp_d(a, 0);
  46119. if (ret != MP_GT)
  46120. return WC_TEST_RET_ENC_EC(ret);
  46121. ret = mp_cmp_d(a, 1);
  46122. if (ret != MP_EQ)
  46123. return WC_TEST_RET_ENC_EC(ret);
  46124. ret = mp_cmp_d(a, 2);
  46125. if (ret != MP_LT)
  46126. return WC_TEST_RET_ENC_EC(ret);
  46127. ret = mp_cmp(a, b);
  46128. if (ret != MP_GT)
  46129. return WC_TEST_RET_ENC_NC;
  46130. mp_read_radix(b, "1234567890123456789", MP_RADIX_HEX);
  46131. ret = mp_cmp_d(b, -1);
  46132. if (ret != MP_GT)
  46133. return WC_TEST_RET_ENC_EC(ret);
  46134. ret = mp_cmp(a, b);
  46135. if (ret != MP_LT)
  46136. return WC_TEST_RET_ENC_NC;
  46137. ret = mp_cmp(b, a);
  46138. if (ret != MP_GT)
  46139. return WC_TEST_RET_ENC_NC;
  46140. ret = mp_cmp(b, b);
  46141. if (ret != MP_EQ)
  46142. return WC_TEST_RET_ENC_NC;
  46143. #if (!defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)) || \
  46144. defined(WOLFSSL_SP_INT_NEGATIVE)
  46145. mp_read_radix(a, "-1", MP_RADIX_HEX);
  46146. mp_read_radix(a, "1", MP_RADIX_HEX);
  46147. ret = mp_cmp(a, b);
  46148. if (ret != MP_LT)
  46149. return WC_TEST_RET_ENC_NC;
  46150. ret = mp_cmp(b, a);
  46151. if (ret != MP_GT)
  46152. return WC_TEST_RET_ENC_NC;
  46153. mp_read_radix(b, "-2", MP_RADIX_HEX);
  46154. ret = mp_cmp(a, b);
  46155. if (ret != MP_GT)
  46156. return WC_TEST_RET_ENC_NC;
  46157. ret = mp_cmp(b, a);
  46158. if (ret != MP_LT)
  46159. return WC_TEST_RET_ENC_NC;
  46160. mp_read_radix(a, "-2", MP_RADIX_HEX);
  46161. ret = mp_cmp(a, b);
  46162. if (ret != MP_EQ)
  46163. return WC_TEST_RET_ENC_NC;
  46164. #endif
  46165. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  46166. defined(WOLFSSL_ECC_GEN_REJECT_SAMPLING)
  46167. mp_zero(a);
  46168. mp_zero(b);
  46169. ret = mp_cmp_ct(a, b, 1);
  46170. if (ret != MP_EQ)
  46171. return WC_TEST_RET_ENC_EC(ret);
  46172. ret = mp_cmp_ct(a, a, a->used);
  46173. if (ret != MP_EQ)
  46174. return WC_TEST_RET_ENC_EC(ret);
  46175. #ifdef WOLFSSL_SP_MATH_ALL
  46176. ret = mp_cmp_ct(a, NULL, a->used);
  46177. if (ret != MP_GT)
  46178. return WC_TEST_RET_ENC_EC(ret);
  46179. ret = mp_cmp_ct(NULL, a, a->used);
  46180. if (ret != MP_LT)
  46181. return WC_TEST_RET_ENC_EC(ret);
  46182. #endif
  46183. mp_read_radix(a, "1", MP_RADIX_HEX);
  46184. ret = mp_cmp_ct(a, b, 1);
  46185. if (ret != MP_GT)
  46186. return WC_TEST_RET_ENC_EC(ret);
  46187. ret = mp_cmp_ct(b, a, 1);
  46188. if (ret != MP_LT)
  46189. return WC_TEST_RET_ENC_EC(ret);
  46190. mp_read_radix(a, "0123456789abcdef0123456789abcdef", MP_RADIX_HEX);
  46191. ret = mp_cmp_ct(a, b, a->used);
  46192. if (ret != MP_GT)
  46193. return WC_TEST_RET_ENC_EC(ret);
  46194. ret = mp_cmp_ct(b, a, a->used);
  46195. if (ret != MP_LT)
  46196. return WC_TEST_RET_ENC_EC(ret);
  46197. mp_read_radix(b, "1123456789abcdef0123456789abcdef", MP_RADIX_HEX);
  46198. ret = mp_cmp_ct(b, a, a->used);
  46199. if (ret != MP_GT)
  46200. return WC_TEST_RET_ENC_EC(ret);
  46201. ret = mp_cmp_ct(a, b, a->used);
  46202. if (ret != MP_LT)
  46203. return WC_TEST_RET_ENC_EC(ret);
  46204. mp_read_radix(b, "0123456789abcdef0123456789abcdf0", MP_RADIX_HEX);
  46205. ret = mp_cmp_ct(b, a, a->used);
  46206. if (ret != MP_GT)
  46207. return WC_TEST_RET_ENC_EC(ret);
  46208. ret = mp_cmp_ct(a, b, a->used);
  46209. if (ret != MP_LT)
  46210. return WC_TEST_RET_ENC_EC(ret);
  46211. mp_read_radix(b, "0123456789abcdf0", MP_RADIX_HEX);
  46212. ret = mp_cmp_ct(a, b, a->used);
  46213. if (ret != MP_GT)
  46214. return WC_TEST_RET_ENC_EC(ret);
  46215. ret = mp_cmp_ct(b, a, a->used);
  46216. if (ret != MP_LT)
  46217. return WC_TEST_RET_ENC_EC(ret);
  46218. #endif
  46219. return 0;
  46220. }
  46221. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  46222. static wc_test_ret_t mp_test_shbd(mp_int* a, mp_int* b, WC_RNG* rng)
  46223. {
  46224. wc_test_ret_t ret;
  46225. int i, j, k;
  46226. #ifndef WOLFSSL_SP_MATH
  46227. for (i = 0; i < 10; i++) {
  46228. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  46229. ret = randNum(a, j, rng, NULL);
  46230. if (ret != MP_OKAY)
  46231. return WC_TEST_RET_ENC_EC(ret);
  46232. mp_copy(a, b);
  46233. for (k = 0; k <= DIGIT_BIT * 2; k++) {
  46234. ret = mp_mul_2d(a, k, a);
  46235. if (ret != MP_OKAY)
  46236. return WC_TEST_RET_ENC_EC(ret);
  46237. mp_rshb(a, k);
  46238. ret = mp_cmp(a, b);
  46239. if (ret != MP_EQ)
  46240. return WC_TEST_RET_ENC_NC;
  46241. }
  46242. }
  46243. }
  46244. #endif
  46245. for (i = 0; i < 10; i++) {
  46246. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  46247. ret = randNum(a, j, rng, NULL);
  46248. if (ret != MP_OKAY)
  46249. return WC_TEST_RET_ENC_EC(ret);
  46250. mp_copy(a, b);
  46251. for (k = 0; k < 10; k++) {
  46252. ret = mp_lshd(a, k);
  46253. if (ret != MP_OKAY)
  46254. return WC_TEST_RET_ENC_EC(ret);
  46255. #ifndef WOLFSSL_SP_MATH
  46256. mp_rshd(a, k);
  46257. #else
  46258. mp_rshb(a, k * SP_WORD_SIZE);
  46259. #endif
  46260. ret = mp_cmp(a, b);
  46261. if (ret != MP_EQ)
  46262. return WC_TEST_RET_ENC_NC;
  46263. }
  46264. }
  46265. }
  46266. #ifndef WOLFSSL_SP_MATH
  46267. mp_zero(a);
  46268. mp_rshd(a, 1);
  46269. if (!mp_iszero(a))
  46270. return WC_TEST_RET_ENC_NC;
  46271. mp_set(a, 1);
  46272. mp_rshd(a, 1);
  46273. if (!mp_iszero(a))
  46274. return WC_TEST_RET_ENC_NC;
  46275. mp_set(a, 1);
  46276. mp_rshd(a, 2);
  46277. if (!mp_iszero(a))
  46278. return WC_TEST_RET_ENC_NC;
  46279. #endif
  46280. return 0;
  46281. }
  46282. #endif
  46283. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  46284. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && \
  46285. !defined(WOLFSSL_RSA_PUBLIC_ONLY))
  46286. static wc_test_ret_t mp_test_div(mp_int* a, mp_int* d, mp_int* r, mp_int* rem,
  46287. WC_RNG* rng)
  46288. {
  46289. wc_test_ret_t ret;
  46290. int i, j, k;
  46291. mp_zero(a);
  46292. mp_zero(d);
  46293. ret = mp_div(a, d, r, rem);
  46294. if (ret != MP_VAL)
  46295. return WC_TEST_RET_ENC_EC(ret);
  46296. mp_set(d, 1);
  46297. ret = mp_div(a, d, r, rem);
  46298. if (ret != MP_OKAY)
  46299. return WC_TEST_RET_ENC_EC(ret);
  46300. if (!mp_iszero(r))
  46301. return WC_TEST_RET_ENC_EC(ret);
  46302. if (!mp_iszero(rem))
  46303. return WC_TEST_RET_ENC_EC(ret);
  46304. mp_set(a, 1);
  46305. ret = mp_div(a, d, r, rem);
  46306. if (ret != MP_OKAY)
  46307. return WC_TEST_RET_ENC_EC(ret);
  46308. if (!mp_isone(r))
  46309. return WC_TEST_RET_ENC_EC(ret);
  46310. if (!mp_iszero(rem))
  46311. return WC_TEST_RET_ENC_EC(ret);
  46312. for (i = 0; i < 100; i++) {
  46313. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 2; j++) {
  46314. ret = randNum(d, j, rng, NULL);
  46315. if (ret != MP_OKAY)
  46316. return WC_TEST_RET_ENC_EC(ret);
  46317. for (k = 1; k < (DIGIT_BIT + 7) / 8 * 2 + 1; k++) {
  46318. ret = randNum(a, k, rng, NULL);
  46319. if (ret != MP_OKAY)
  46320. return WC_TEST_RET_ENC_EC(ret);
  46321. ret = mp_div(a, d, NULL, rem);
  46322. if (ret != MP_OKAY)
  46323. return WC_TEST_RET_ENC_EC(ret);
  46324. ret = mp_div(a, d, r, NULL);
  46325. if (ret != MP_OKAY)
  46326. return WC_TEST_RET_ENC_EC(ret);
  46327. ret = mp_div(a, d, r, rem);
  46328. if (ret != MP_OKAY)
  46329. return WC_TEST_RET_ENC_EC(ret);
  46330. mp_mul(r, d, r);
  46331. mp_add(r, rem, r);
  46332. if (mp_cmp(r, a) != MP_EQ)
  46333. return WC_TEST_RET_ENC_NC;
  46334. }
  46335. }
  46336. }
  46337. ret = randNum(d, (DIGIT_BIT + 7) / 8 * 2, rng, NULL);
  46338. if (ret != MP_OKAY)
  46339. return WC_TEST_RET_ENC_EC(ret);
  46340. mp_add(d, d, a);
  46341. mp_set(rem, 1);
  46342. mp_div(a, d, NULL, rem);
  46343. if (ret != MP_OKAY)
  46344. return WC_TEST_RET_ENC_EC(ret);
  46345. if (!mp_iszero(rem))
  46346. return WC_TEST_RET_ENC_EC(ret);
  46347. mp_set(r, 1);
  46348. mp_div(a, d, r, NULL);
  46349. if (ret != MP_OKAY)
  46350. return WC_TEST_RET_ENC_EC(ret);
  46351. if (mp_cmp_d(r, 2) != MP_EQ)
  46352. return WC_TEST_RET_ENC_EC(ret);
  46353. mp_set(r, 1);
  46354. mp_set(rem, 1);
  46355. mp_div(a, d, r, rem);
  46356. if (ret != MP_OKAY)
  46357. return WC_TEST_RET_ENC_EC(ret);
  46358. if (mp_cmp_d(r, 2) != MP_EQ)
  46359. return WC_TEST_RET_ENC_EC(ret);
  46360. if (!mp_iszero(rem))
  46361. return WC_TEST_RET_ENC_EC(ret);
  46362. mp_set(a, 0xfe);
  46363. mp_lshd(a, 3);
  46364. mp_add_d(a, 0xff, a);
  46365. mp_set(d, 0xfe);
  46366. mp_lshd(d, 2);
  46367. ret = mp_div(a, d, r, rem);
  46368. if (ret != MP_OKAY)
  46369. return WC_TEST_RET_ENC_EC(ret);
  46370. mp_mul(r, d, d);
  46371. mp_add(rem, d, d);
  46372. if (mp_cmp(a, d) != MP_EQ)
  46373. return WC_TEST_RET_ENC_NC;
  46374. /* Force (hi | lo) / d to be (d | 0) / d which will would not fit in
  46375. * a digit. So mp_div must detect and handle.
  46376. * For example: 0x800000 / 0x8001, DIGIT_BIT = 8
  46377. */
  46378. mp_zero(a);
  46379. mp_set_bit(a, DIGIT_BIT * 3 - 1);
  46380. mp_zero(d);
  46381. mp_set_bit(d, DIGIT_BIT * 2 - 1);
  46382. mp_add_d(d, 1, d);
  46383. ret = mp_div(a, d, r, rem);
  46384. if (ret != MP_OKAY)
  46385. return WC_TEST_RET_ENC_EC(ret);
  46386. /* Make sure [d | d] / d is handled. */
  46387. mp_zero(a);
  46388. mp_set_bit(a, DIGIT_BIT * 2 - 1);
  46389. mp_set_bit(a, DIGIT_BIT * 1 - 1);
  46390. mp_zero(d);
  46391. mp_set_bit(d, DIGIT_BIT - 1);
  46392. ret = mp_div(a, d, r, rem);
  46393. if (ret != MP_OKAY)
  46394. return WC_TEST_RET_ENC_EC(ret);
  46395. mp_zero(a);
  46396. mp_set_bit(a, DIGIT_BIT);
  46397. mp_set_bit(a, 0);
  46398. mp_zero(d);
  46399. if (mp_cmp(r, a) != MP_EQ)
  46400. return WC_TEST_RET_ENC_NC;
  46401. if (mp_cmp(rem, d) != MP_EQ)
  46402. return WC_TEST_RET_ENC_NC;
  46403. return 0;
  46404. }
  46405. #endif
  46406. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  46407. !defined(WC_NO_RNG)
  46408. static wc_test_ret_t mp_test_prime(mp_int* a, WC_RNG* rng)
  46409. {
  46410. wc_test_ret_t ret;
  46411. int res;
  46412. ret = mp_rand_prime(a, 1, rng, NULL);
  46413. #if defined(WOLFSSL_SP_MATH_ALL)
  46414. if (ret != 0)
  46415. return WC_TEST_RET_ENC_EC(ret);
  46416. #else
  46417. if (ret != MP_VAL)
  46418. return WC_TEST_RET_ENC_NC;
  46419. #endif
  46420. #ifndef WOLFSSL_SP_MATH
  46421. ret = mp_rand_prime(a, -5, rng, NULL);
  46422. if (ret != 0 || (a->dp[0] & 3) != 3)
  46423. return WC_TEST_RET_ENC_NC;
  46424. #endif
  46425. ret = mp_prime_is_prime(a, 1, &res);
  46426. if (ret != MP_OKAY)
  46427. return WC_TEST_RET_ENC_EC(ret);
  46428. #ifndef WOLFSSL_SP_MATH
  46429. if (res != MP_YES)
  46430. return WC_TEST_RET_ENC_EC(res);
  46431. #else
  46432. if (res != MP_NO)
  46433. return WC_TEST_RET_ENC_EC(res);
  46434. #endif
  46435. ret = mp_prime_is_prime(a, 0, &res);
  46436. if (ret != MP_VAL)
  46437. return WC_TEST_RET_ENC_EC(ret);
  46438. ret = mp_prime_is_prime(a, -1, &res);
  46439. if (ret != MP_VAL)
  46440. return WC_TEST_RET_ENC_EC(ret);
  46441. ret = mp_prime_is_prime(a, 257, &res);
  46442. if (ret != MP_VAL)
  46443. return WC_TEST_RET_ENC_EC(ret);
  46444. mp_set(a, 1);
  46445. ret = mp_prime_is_prime(a, 1, &res);
  46446. if (ret != MP_OKAY)
  46447. return WC_TEST_RET_ENC_EC(ret);
  46448. if (res != MP_NO)
  46449. return WC_TEST_RET_ENC_EC(res);
  46450. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  46451. if (ret != MP_OKAY)
  46452. return WC_TEST_RET_ENC_EC(ret);
  46453. if (res != MP_NO)
  46454. return WC_TEST_RET_ENC_EC(res);
  46455. mp_set(a, 2);
  46456. ret = mp_prime_is_prime(a, 1, &res);
  46457. if (ret != MP_OKAY)
  46458. return WC_TEST_RET_ENC_EC(ret);
  46459. if (res != MP_YES)
  46460. return WC_TEST_RET_ENC_EC(res);
  46461. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  46462. if (ret != MP_OKAY)
  46463. return WC_TEST_RET_ENC_EC(ret);
  46464. if (res != MP_YES)
  46465. return WC_TEST_RET_ENC_EC(res);
  46466. mp_set(a, 0xfb);
  46467. ret = mp_prime_is_prime(a, 1, &res);
  46468. if (ret != MP_OKAY)
  46469. return WC_TEST_RET_ENC_EC(ret);
  46470. if (res != MP_YES)
  46471. return WC_TEST_RET_ENC_EC(res);
  46472. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  46473. if (ret != MP_OKAY)
  46474. return WC_TEST_RET_ENC_EC(ret);
  46475. if (res != MP_YES)
  46476. return WC_TEST_RET_ENC_EC(res);
  46477. mp_set(a, 0x6);
  46478. ret = mp_prime_is_prime(a, 1, &res);
  46479. if (ret != MP_OKAY)
  46480. return WC_TEST_RET_ENC_EC(ret);
  46481. if (res != MP_NO)
  46482. return WC_TEST_RET_ENC_EC(res);
  46483. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  46484. if (ret != MP_OKAY)
  46485. return WC_TEST_RET_ENC_EC(ret);
  46486. if (res != MP_NO)
  46487. return WC_TEST_RET_ENC_EC(res);
  46488. mp_set_int(a, 0x655 * 0x65b);
  46489. ret = mp_prime_is_prime(a, 10, &res);
  46490. if (ret != MP_OKAY)
  46491. return WC_TEST_RET_ENC_EC(ret);
  46492. if (res != MP_NO)
  46493. return WC_TEST_RET_ENC_EC(res);
  46494. ret = mp_prime_is_prime_ex(a, 10, &res, rng);
  46495. if (ret != MP_OKAY)
  46496. return WC_TEST_RET_ENC_EC(ret);
  46497. if (res != MP_NO)
  46498. return WC_TEST_RET_ENC_EC(res);
  46499. return 0;
  46500. }
  46501. #endif
  46502. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  46503. static wc_test_ret_t mp_test_lcm_gcd(mp_int* a, mp_int* b, mp_int* r, mp_int* exp,
  46504. WC_RNG* rng)
  46505. {
  46506. wc_test_ret_t ret;
  46507. int i;
  46508. WOLFSSL_SMALL_STACK_STATIC const int kat[][3] = {
  46509. { 1, 1, 1 }, { 2, 1, 2 }, { 1, 2, 2 }, { 2, 4, 4 }, { 4, 2, 4 },
  46510. { 12, 56, 168 }, { 56, 12, 168 }
  46511. };
  46512. (void)exp;
  46513. mp_set(a, 0);
  46514. mp_set(b, 1);
  46515. ret = mp_lcm(a, a, r);
  46516. if (ret != MP_VAL)
  46517. return WC_TEST_RET_ENC_EC(ret);
  46518. ret = mp_lcm(a, b, r);
  46519. if (ret != MP_VAL)
  46520. return WC_TEST_RET_ENC_EC(ret);
  46521. ret = mp_lcm(b, a, r);
  46522. if (ret != MP_VAL)
  46523. return WC_TEST_RET_ENC_EC(ret);
  46524. for (i = 0; i < (int)(sizeof(kat) / sizeof(*kat)); i++) {
  46525. mp_set(a, kat[i][0]);
  46526. mp_set(b, kat[i][1]);
  46527. ret = mp_lcm(a, b, r);
  46528. if (ret != MP_OKAY)
  46529. return WC_TEST_RET_ENC_EC(ret);
  46530. mp_set(exp, kat[i][2]);
  46531. ret = mp_cmp(r, exp);
  46532. if (ret != MP_EQ)
  46533. return WC_TEST_RET_ENC_NC;
  46534. }
  46535. (void)rng;
  46536. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  46537. !defined(WC_NO_RNG)
  46538. ret = mp_rand_prime(a, 20, rng, NULL);
  46539. if (ret != MP_OKAY)
  46540. return WC_TEST_RET_ENC_EC(ret);
  46541. ret = mp_rand_prime(b, 20, rng, NULL);
  46542. if (ret != MP_OKAY)
  46543. return WC_TEST_RET_ENC_EC(ret);
  46544. ret = mp_mul(a, b, exp);
  46545. if (ret != MP_OKAY)
  46546. return WC_TEST_RET_ENC_EC(ret);
  46547. ret = mp_lcm(a, b, r);
  46548. if (ret != MP_OKAY)
  46549. return WC_TEST_RET_ENC_EC(ret);
  46550. ret = mp_cmp(r, exp);
  46551. if (ret != MP_EQ)
  46552. return WC_TEST_RET_ENC_NC;
  46553. ret = mp_lcm(b, a, r);
  46554. if (ret != MP_OKAY)
  46555. return WC_TEST_RET_ENC_EC(ret);
  46556. ret = mp_cmp(r, exp);
  46557. if (ret != MP_EQ)
  46558. return WC_TEST_RET_ENC_NC;
  46559. #endif
  46560. mp_set(a, 11);
  46561. mp_zero(b);
  46562. ret = mp_gcd(a, b, r);
  46563. if (ret != MP_OKAY)
  46564. return WC_TEST_RET_ENC_EC(ret);
  46565. ret = mp_cmp_d(r, 11);
  46566. if (ret != MP_EQ)
  46567. return WC_TEST_RET_ENC_EC(ret);
  46568. ret = mp_gcd(b, a, r);
  46569. if (ret != MP_OKAY)
  46570. return WC_TEST_RET_ENC_EC(ret);
  46571. ret = mp_cmp_d(r, 11);
  46572. if (ret != MP_EQ)
  46573. return WC_TEST_RET_ENC_EC(ret);
  46574. ret = mp_gcd(b, b, r);
  46575. if (ret != MP_VAL)
  46576. return WC_TEST_RET_ENC_EC(ret);
  46577. return 0;
  46578. }
  46579. #endif
  46580. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  46581. defined(WOLFSSL_SP_MATH_ALL)
  46582. static wc_test_ret_t mp_test_mod_2d(mp_int* a, mp_int* r, mp_int* t, WC_RNG* rng)
  46583. {
  46584. wc_test_ret_t ret;
  46585. int i;
  46586. int j;
  46587. mp_set(a, 10);
  46588. ret = mp_mod_2d(a, 0, r);
  46589. if (ret != MP_OKAY)
  46590. return WC_TEST_RET_ENC_EC(ret);
  46591. if (!mp_iszero(r))
  46592. return WC_TEST_RET_ENC_NC;
  46593. ret = mp_mod_2d(a, 1, r);
  46594. if (ret != MP_OKAY)
  46595. return WC_TEST_RET_ENC_EC(ret);
  46596. if (!mp_iszero(r))
  46597. return WC_TEST_RET_ENC_NC;
  46598. ret = mp_mod_2d(a, 2, r);
  46599. if (ret != MP_OKAY)
  46600. return WC_TEST_RET_ENC_EC(ret);
  46601. ret = mp_cmp_d(r, 2);
  46602. if (ret != 0)
  46603. return WC_TEST_RET_ENC_EC(ret);
  46604. for (i = 2; i < 20; i++) {
  46605. ret = randNum(a, i, rng, NULL);
  46606. if (ret != 0)
  46607. return WC_TEST_RET_ENC_EC(ret);
  46608. for (j = 1; j <= mp_count_bits(a); j++) {
  46609. /* Get top part */
  46610. ret = mp_div_2d(a, j, t, NULL);
  46611. if (ret != 0)
  46612. return WC_TEST_RET_ENC_EC(ret);
  46613. ret = mp_mul_2d(t, j, t);
  46614. if (ret != 0)
  46615. return WC_TEST_RET_ENC_EC(ret);
  46616. /* Get bottom part */
  46617. ret = mp_mod_2d(a, j, r);
  46618. if (ret != 0)
  46619. return WC_TEST_RET_ENC_EC(ret);
  46620. /* Reassemble */
  46621. ret = mp_add(t, r, r);
  46622. if (ret != 0)
  46623. return WC_TEST_RET_ENC_EC(ret);
  46624. ret = mp_cmp(a, r);
  46625. if (ret != MP_EQ)
  46626. return WC_TEST_RET_ENC_NC;
  46627. }
  46628. }
  46629. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_SP_INT_NEGATIVE)
  46630. /* Test negative value being moded. */
  46631. for (j = 0; j < 20; j++) {
  46632. ret = randNum(a, 2, rng, NULL);
  46633. if (ret != 0)
  46634. return WC_TEST_RET_ENC_EC(ret);
  46635. a->sign = MP_NEG;
  46636. for (i = 1; i < DIGIT_BIT * 3 + 1; i++) {
  46637. ret = mp_mod_2d(a, i, r);
  46638. if (ret != 0)
  46639. return WC_TEST_RET_ENC_EC(ret);
  46640. mp_zero(t);
  46641. ret = mp_set_bit(t, i);
  46642. if (ret != 0)
  46643. return WC_TEST_RET_ENC_EC(ret);
  46644. ret = mp_mod(a, t, t);
  46645. if (ret != 0)
  46646. return WC_TEST_RET_ENC_EC(ret);
  46647. ret = mp_cmp(r, t);
  46648. if (ret != MP_EQ)
  46649. return WC_TEST_RET_ENC_NC;
  46650. }
  46651. }
  46652. #endif
  46653. return 0;
  46654. }
  46655. #endif
  46656. #if defined(WOLFSSL_SP_MATH_ALL) || defined(OPENSSL_EXTRA) || \
  46657. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY))
  46658. static wc_test_ret_t mp_test_mod_d(mp_int* a, WC_RNG* rng)
  46659. {
  46660. wc_test_ret_t ret;
  46661. mp_digit r;
  46662. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)
  46663. mp_digit rem;
  46664. int i;
  46665. #endif
  46666. (void)rng;
  46667. ret = mp_set(a, 1);
  46668. if (ret != MP_OKAY)
  46669. return WC_TEST_RET_ENC_EC(ret);
  46670. ret = mp_mod_d(a, 0, &r);
  46671. if (ret != MP_VAL)
  46672. return WC_TEST_RET_ENC_EC(ret);
  46673. mp_zero(a);
  46674. ret = mp_mod_d(a, 1, &r);
  46675. if (ret != MP_OKAY)
  46676. return WC_TEST_RET_ENC_EC(ret);
  46677. ret = mp_mod_d(a, 3, &r);
  46678. if (ret != MP_OKAY)
  46679. return WC_TEST_RET_ENC_EC(ret);
  46680. ret = mp_mod_d(a, 5, &r);
  46681. if (ret != MP_OKAY)
  46682. return WC_TEST_RET_ENC_EC(ret);
  46683. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)
  46684. for (i = MP_MAX_TEST_BYTE_LEN - 16; i <= MP_MAX_TEST_BYTE_LEN; i++) {
  46685. ret = randNum(a, i, rng, NULL);
  46686. if (ret != MP_OKAY)
  46687. return WC_TEST_RET_ENC_EC(ret);
  46688. ret = mp_mod_d(a, 3, &r);
  46689. if (ret != MP_OKAY)
  46690. return WC_TEST_RET_ENC_EC(ret);
  46691. ret = mp_div_d(a, 3, a, &rem);
  46692. if (ret != MP_OKAY)
  46693. return WC_TEST_RET_ENC_EC(ret);
  46694. if (r != rem)
  46695. return WC_TEST_RET_ENC_NC;
  46696. }
  46697. #endif
  46698. return 0;
  46699. }
  46700. #endif
  46701. static wc_test_ret_t mp_test_mul_sqr(mp_int* a, mp_int* b, mp_int* r1, mp_int* r2,
  46702. WC_RNG* rng)
  46703. {
  46704. wc_test_ret_t ret;
  46705. int i;
  46706. for (i = 1; i < 16; i++) {
  46707. ret = randNum(a, i, rng, NULL);
  46708. if (ret != 0)
  46709. return WC_TEST_RET_ENC_EC(ret);
  46710. ret = mp_mul(a, a, r1);
  46711. if (ret != 0)
  46712. return WC_TEST_RET_ENC_EC(ret);
  46713. ret = mp_sqr(a, r2);
  46714. if (ret != 0)
  46715. return WC_TEST_RET_ENC_EC(ret);
  46716. ret = mp_cmp(r1, r2);
  46717. if (ret != MP_EQ)
  46718. return WC_TEST_RET_ENC_NC;
  46719. }
  46720. ret = mp_set(b, 0);
  46721. if (ret != MP_OKAY)
  46722. return WC_TEST_RET_ENC_EC(ret);
  46723. ret = mp_mul(a, b, r1);
  46724. if (ret != MP_OKAY)
  46725. return WC_TEST_RET_ENC_EC(ret);
  46726. if (!mp_iszero(r1))
  46727. return WC_TEST_RET_ENC_EC(ret);
  46728. ret = mp_sqr(b, r1);
  46729. if (ret != MP_OKAY)
  46730. return WC_TEST_RET_ENC_EC(ret);
  46731. if (!mp_iszero(r1))
  46732. return WC_TEST_RET_ENC_NC;
  46733. #ifdef WOLFSSL_SP_MATH_ALL
  46734. ret = mp_set(a, 1);
  46735. if (ret != MP_OKAY)
  46736. return WC_TEST_RET_ENC_EC(ret);
  46737. i = (SP_INT_DIGITS / 2) + 1;
  46738. ret = mp_mul_2d(a, i * SP_WORD_SIZE - 1, a);
  46739. if (ret != MP_OKAY)
  46740. return WC_TEST_RET_ENC_EC(ret);
  46741. ret = mp_set(b, 1);
  46742. if (ret != MP_OKAY)
  46743. return WC_TEST_RET_ENC_EC(ret);
  46744. ret = mp_mul_2d(b, (SP_INT_DIGITS - 1 - i) * SP_WORD_SIZE - 1, b);
  46745. if (ret != MP_OKAY)
  46746. return WC_TEST_RET_ENC_EC(ret);
  46747. ret = mp_mul(a, b, r1);
  46748. if (ret != MP_OKAY)
  46749. return WC_TEST_RET_ENC_EC(ret);
  46750. ret = mp_mul(a, a, r1);
  46751. if (ret == MP_OKAY)
  46752. return WC_TEST_RET_ENC_NC;
  46753. ret = mp_sqr(a, r1);
  46754. if (ret == MP_OKAY)
  46755. return WC_TEST_RET_ENC_NC;
  46756. ret = mp_sqr(b, r1);
  46757. if (ret != MP_OKAY)
  46758. return WC_TEST_RET_ENC_EC(ret);
  46759. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  46760. (defined(HAVE_ECC) && defined(FP_ECC))
  46761. ret = mp_mulmod(a, b, b, r1);
  46762. if (ret != MP_OKAY)
  46763. return WC_TEST_RET_ENC_EC(ret);
  46764. ret = mp_mulmod(a, a, b, r1);
  46765. if (ret == MP_OKAY)
  46766. return WC_TEST_RET_ENC_NC;
  46767. #if defined(HAVE_ECC) && (defined(ECC_SHAMIR) || defined(FP_ECC))
  46768. ret = mp_sqrmod(a, b, r1);
  46769. if (ret == MP_OKAY)
  46770. return WC_TEST_RET_ENC_NC;
  46771. ret = mp_sqrmod(b, a, r1);
  46772. if (ret != MP_OKAY)
  46773. return WC_TEST_RET_ENC_EC(ret);
  46774. #endif /* HAVE_ECC && (ECC_SHAMIR || FP_ECC) */
  46775. #endif /* WOLFSSL_SP_MATH_ALL || WOLFSSL_HAVE_SP_DH || (HAVE_ECC && FP_ECC) */
  46776. #endif /* WOLFSSL_SP_MATH_ALL */
  46777. return 0;
  46778. }
  46779. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  46780. defined(OPENSSL_EXTRA)
  46781. static wc_test_ret_t mp_test_invmod(mp_int* a, mp_int* m, mp_int* r)
  46782. {
  46783. wc_test_ret_t ret;
  46784. mp_set(a, 0);
  46785. mp_set(m, 1);
  46786. ret = mp_invmod(a, m, r);
  46787. if (ret != MP_VAL)
  46788. return WC_TEST_RET_ENC_EC(ret);
  46789. ret = mp_invmod(m, a, r);
  46790. if (ret != MP_VAL)
  46791. return WC_TEST_RET_ENC_EC(ret);
  46792. mp_set(a, 2);
  46793. mp_set(m, 4);
  46794. ret = mp_invmod(a, m, r);
  46795. if (ret != MP_VAL)
  46796. return WC_TEST_RET_ENC_EC(ret);
  46797. mp_set(a, 3);
  46798. mp_set(m, 6);
  46799. ret = mp_invmod(a, m, r);
  46800. if (ret != MP_VAL)
  46801. return WC_TEST_RET_ENC_EC(ret);
  46802. mp_set(a, 5*9);
  46803. mp_set(m, 6*9);
  46804. ret = mp_invmod(a, m, r);
  46805. if (ret != MP_VAL)
  46806. return WC_TEST_RET_ENC_EC(ret);
  46807. mp_set(a, 1);
  46808. mp_set(m, 4);
  46809. ret = mp_invmod(a, m, r);
  46810. if (ret != MP_OKAY)
  46811. return WC_TEST_RET_ENC_EC(ret);
  46812. if (!mp_isone(r))
  46813. return WC_TEST_RET_ENC_NC;
  46814. mp_set(a, 3);
  46815. mp_set(m, 4);
  46816. ret = mp_invmod(a, m, r);
  46817. if (ret != MP_OKAY)
  46818. return WC_TEST_RET_ENC_EC(ret);
  46819. ret = mp_cmp_d(r, 3);
  46820. if (ret != 0)
  46821. return WC_TEST_RET_ENC_EC(ret);
  46822. mp_set(a, 3);
  46823. mp_set(m, 5);
  46824. ret = mp_invmod(a, m, r);
  46825. if (ret != MP_OKAY)
  46826. return WC_TEST_RET_ENC_EC(ret);
  46827. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  46828. /* Maximum 'a' */
  46829. mp_set(a, 0);
  46830. mp_set_bit(a, (r->size / 2)* SP_WORD_SIZE - 1);
  46831. mp_sub_d(a, 1, a);
  46832. /* Modulus too big. */
  46833. mp_set(m, 0);
  46834. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE);
  46835. ret = mp_invmod(a, m, r);
  46836. if (ret != MP_VAL)
  46837. return WC_TEST_RET_ENC_EC(ret);
  46838. /* Maximum modulus - even. */
  46839. mp_set(m, 0);
  46840. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE - 1);
  46841. ret = mp_invmod(a, m, r);
  46842. if (ret != MP_OKAY)
  46843. return WC_TEST_RET_ENC_EC(ret);
  46844. #endif
  46845. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_INT_NEGATIVE)
  46846. mp_read_radix(a, "-3", 16);
  46847. ret = mp_invmod(a, m, r);
  46848. if (ret != MP_OKAY)
  46849. return WC_TEST_RET_ENC_EC(ret);
  46850. #endif
  46851. #if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC)
  46852. mp_set(a, 0);
  46853. mp_set(m, 3);
  46854. ret = mp_invmod_mont_ct(a, m, r, 1);
  46855. if (ret != MP_VAL)
  46856. return WC_TEST_RET_ENC_EC(ret);
  46857. mp_set(a, 1);
  46858. mp_set(m, 0);
  46859. ret = mp_invmod_mont_ct(a, m, r, 1);
  46860. if (ret != MP_VAL)
  46861. return WC_TEST_RET_ENC_EC(ret);
  46862. mp_set(a, 1);
  46863. mp_set(m, 1);
  46864. ret = mp_invmod_mont_ct(a, m, r, 1);
  46865. if (ret != MP_VAL)
  46866. return WC_TEST_RET_ENC_EC(ret);
  46867. mp_set(a, 1);
  46868. mp_set(m, 2);
  46869. ret = mp_invmod_mont_ct(a, m, r, 1);
  46870. if (ret != MP_VAL)
  46871. return WC_TEST_RET_ENC_EC(ret);
  46872. mp_set(a, 1);
  46873. mp_set(m, 3);
  46874. ret = mp_invmod_mont_ct(a, m, r, 1);
  46875. if (ret != MP_OKAY)
  46876. return WC_TEST_RET_ENC_EC(ret);
  46877. #endif
  46878. return 0;
  46879. }
  46880. #endif /* !NO_RSA || HAVE_ECC || !NO_DSA || OPENSSL_EXTRA */
  46881. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  46882. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  46883. static wc_test_ret_t mp_test_exptmod(mp_int* b, mp_int* e, mp_int* m, mp_int* r)
  46884. {
  46885. wc_test_ret_t ret;
  46886. mp_set(b, 0x2);
  46887. mp_set(e, 0x3);
  46888. mp_set(m, 0x0);
  46889. ret = mp_exptmod_ex(b, e, 1, m, r);
  46890. if (ret != MP_VAL)
  46891. return WC_TEST_RET_ENC_EC(ret);
  46892. ret = mp_exptmod_nct(b, e, m, r);
  46893. if (ret != MP_VAL)
  46894. return WC_TEST_RET_ENC_EC(ret);
  46895. mp_set(b, 0x2);
  46896. mp_set(e, 0x3);
  46897. mp_set(m, 0x1);
  46898. ret = mp_exptmod_ex(b, e, 1, m, r);
  46899. if (ret != MP_OKAY)
  46900. return WC_TEST_RET_ENC_EC(ret);
  46901. if (!mp_iszero(r))
  46902. return WC_TEST_RET_ENC_NC;
  46903. ret = mp_exptmod_nct(b, e, m, r);
  46904. if (ret != MP_OKAY)
  46905. return WC_TEST_RET_ENC_EC(ret);
  46906. if (!mp_iszero(r))
  46907. return WC_TEST_RET_ENC_NC;
  46908. mp_set(b, 0x2);
  46909. mp_set(e, 0x0);
  46910. mp_set(m, 0x7);
  46911. ret = mp_exptmod_ex(b, e, 1, m, r);
  46912. if (ret != MP_OKAY)
  46913. return WC_TEST_RET_ENC_EC(ret);
  46914. if (!mp_isone(r))
  46915. return WC_TEST_RET_ENC_NC;
  46916. ret = mp_exptmod_nct(b, e, m, r);
  46917. if (ret != MP_OKAY)
  46918. return WC_TEST_RET_ENC_EC(ret);
  46919. if (!mp_isone(r))
  46920. return WC_TEST_RET_ENC_NC;
  46921. mp_set(b, 0x0);
  46922. mp_set(e, 0x3);
  46923. mp_set(m, 0x7);
  46924. ret = mp_exptmod_ex(b, e, 1, m, r);
  46925. if (ret != MP_OKAY)
  46926. return WC_TEST_RET_ENC_EC(ret);
  46927. if (!mp_iszero(r))
  46928. return WC_TEST_RET_ENC_NC;
  46929. ret = mp_exptmod_nct(b, e, m, r);
  46930. if (ret != MP_OKAY)
  46931. return WC_TEST_RET_ENC_EC(ret);
  46932. if (!mp_iszero(r))
  46933. return WC_TEST_RET_ENC_NC;
  46934. mp_set(b, 0x10);
  46935. mp_set(e, 0x3);
  46936. mp_set(m, 0x7);
  46937. ret = mp_exptmod_ex(b, e, 1, m, r);
  46938. if (ret != MP_OKAY)
  46939. return WC_TEST_RET_ENC_EC(ret);
  46940. ret = mp_exptmod_nct(b, e, m, r);
  46941. if (ret != MP_OKAY)
  46942. return WC_TEST_RET_ENC_EC(ret);
  46943. mp_set(b, 0x7);
  46944. mp_set(e, 0x3);
  46945. mp_set(m, 0x7);
  46946. ret = mp_exptmod_ex(b, e, 1, m, r);
  46947. if (ret != MP_OKAY)
  46948. return WC_TEST_RET_ENC_EC(ret);
  46949. if (!mp_iszero(r))
  46950. return WC_TEST_RET_ENC_NC;
  46951. ret = mp_exptmod_nct(b, e, m, r);
  46952. if (ret != MP_OKAY)
  46953. return WC_TEST_RET_ENC_EC(ret);
  46954. if (!mp_iszero(r))
  46955. return WC_TEST_RET_ENC_NC;
  46956. #ifndef WOLFSSL_SP_MATH
  46957. mp_set(b, 0x01);
  46958. mp_mul_2d(b, DIGIT_BIT, b);
  46959. mp_add_d(b, 1, b);
  46960. mp_set(e, 0x3);
  46961. mp_copy(b, m);
  46962. ret = mp_exptmod_ex(b, e, 1, m, r);
  46963. if (ret != MP_OKAY)
  46964. return WC_TEST_RET_ENC_EC(ret);
  46965. if (!mp_iszero(r))
  46966. return WC_TEST_RET_ENC_NC;
  46967. ret = mp_exptmod_nct(b, e, m, r);
  46968. if (ret != MP_OKAY)
  46969. return WC_TEST_RET_ENC_EC(ret);
  46970. if (!mp_iszero(r))
  46971. return WC_TEST_RET_ENC_NC;
  46972. #endif
  46973. mp_set(b, 0x2);
  46974. mp_set(e, 0x3);
  46975. mp_set(m, 0x7);
  46976. ret = mp_exptmod_ex(b, e, 1, m, r);
  46977. if (ret != MP_OKAY)
  46978. return WC_TEST_RET_ENC_EC(ret);
  46979. ret = mp_exptmod_nct(b, e, m, r);
  46980. if (ret != MP_OKAY)
  46981. return WC_TEST_RET_ENC_EC(ret);
  46982. #ifdef WOLFSSL_SP_MATH_ALL
  46983. mp_set(b, 0x2);
  46984. mp_set(e, 0x3);
  46985. mp_set(m, 0x01);
  46986. mp_mul_2d(m, SP_WORD_SIZE * SP_INT_DIGITS / 2, m);
  46987. mp_add_d(m, 0x01, m);
  46988. ret = mp_exptmod_ex(b, e, 1, m, r);
  46989. if (ret != MP_VAL)
  46990. return WC_TEST_RET_ENC_EC(ret);
  46991. ret = mp_exptmod_nct(b, e, m, r);
  46992. if (ret != MP_VAL)
  46993. return WC_TEST_RET_ENC_EC(ret);
  46994. #endif
  46995. return 0;
  46996. }
  46997. #endif /* !NO_RSA || !NO_DSA || !NO_DH || (HAVE_ECC && HAVE_COMP_KEY) ||
  46998. * OPENSSL_EXTRA */
  46999. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  47000. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  47001. static wc_test_ret_t mp_test_mont(mp_int* a, mp_int* m, mp_int* n, mp_int* r, WC_RNG* rng)
  47002. {
  47003. wc_test_ret_t ret;
  47004. mp_digit mp;
  47005. static int exp[] = { 7, 8, 16, 27, 32, 64,
  47006. 127, 128, 255, 256,
  47007. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  47008. 383, 384, 2033, 2048
  47009. #endif
  47010. };
  47011. static mp_digit sub[] = { 0x01, 0x05, 0x0f, 0x27, 0x05, 0x3b,
  47012. 0x01, 0x9f, 0x13, 0xbd,
  47013. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  47014. 0x1f, 0x13d, 0x45, 0x615
  47015. #endif
  47016. };
  47017. int bits[] = { 256, 384,
  47018. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 4096
  47019. 2048,
  47020. #endif
  47021. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 6144
  47022. 3072
  47023. #endif
  47024. };
  47025. int i;
  47026. int j;
  47027. for (i = 0; i < (int)(sizeof(exp) / sizeof(*exp)); i++) {
  47028. if (exp[i] >= DIGIT_BIT)
  47029. continue;
  47030. mp_zero(m);
  47031. ret = mp_set_bit(m, exp[i]);
  47032. if (ret != MP_OKAY)
  47033. return WC_TEST_RET_ENC_EC(ret);
  47034. ret = mp_sub_d(m, sub[i], m);
  47035. if (ret != MP_OKAY)
  47036. return WC_TEST_RET_ENC_EC(ret);
  47037. ret = mp_montgomery_setup(m, &mp);
  47038. if (ret != MP_OKAY)
  47039. return WC_TEST_RET_ENC_EC(ret);
  47040. ret = mp_montgomery_calc_normalization(n, m);
  47041. if (ret != MP_OKAY)
  47042. return WC_TEST_RET_ENC_EC(ret);
  47043. for (j = 0; j < 10; j++) {
  47044. ret = randNum(a, (exp[i] + DIGIT_BIT - 1) / DIGIT_BIT, rng, NULL);
  47045. if (ret != 0)
  47046. return WC_TEST_RET_ENC_EC(ret);
  47047. ret = mp_mod(a, m, a);
  47048. if (ret != 0)
  47049. return WC_TEST_RET_ENC_EC(ret);
  47050. /* r = a * a */
  47051. ret = mp_sqrmod(a, m, r);
  47052. if (ret != MP_OKAY)
  47053. return WC_TEST_RET_ENC_EC(ret);
  47054. /* Convert to Montgomery form = a*n */
  47055. ret = mp_mulmod(a, n, m, a);
  47056. if (ret != MP_OKAY)
  47057. return WC_TEST_RET_ENC_EC(ret);
  47058. /* a*a mod m == ((a*n) * (a*n)) / n / n */
  47059. ret = mp_sqr(a, a);
  47060. if (ret != MP_OKAY)
  47061. return WC_TEST_RET_ENC_EC(ret);
  47062. ret = mp_montgomery_reduce(a, m, mp);
  47063. if (ret != MP_OKAY)
  47064. return WC_TEST_RET_ENC_EC(ret);
  47065. ret = mp_montgomery_reduce(a, m, mp);
  47066. if (ret != MP_OKAY)
  47067. return WC_TEST_RET_ENC_EC(ret);
  47068. if (mp_cmp(a, r) != MP_EQ)
  47069. return WC_TEST_RET_ENC_NC;
  47070. }
  47071. }
  47072. /* Force carries. */
  47073. for (i = 0; i < (int)(sizeof(bits) / sizeof(*bits)); i++) {
  47074. /* a = 2^(bits*2) - 1 */
  47075. mp_zero(a);
  47076. mp_set_bit(a, bits[i] * 2);
  47077. mp_sub_d(a, 1, a);
  47078. /* m = 2^(bits) - 1 */
  47079. mp_zero(m);
  47080. mp_set_bit(m, bits[i]);
  47081. mp_sub_d(m, 1, m);
  47082. mp = 1;
  47083. /* result = r = 2^(bits) - 1 */
  47084. mp_zero(r);
  47085. mp_set_bit(r, bits[i]);
  47086. mp_sub_d(r, 1, r);
  47087. ret = mp_montgomery_reduce(a, m, mp);
  47088. if (ret != MP_OKAY)
  47089. return WC_TEST_RET_ENC_EC(ret);
  47090. /* Result is m or 0 if reduced to range of modulus. */
  47091. if (mp_cmp(a, r) != MP_EQ && mp_iszero(a) != MP_YES)
  47092. return WC_TEST_RET_ENC_NC;
  47093. }
  47094. return 0;
  47095. }
  47096. #endif
  47097. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mp_test(void)
  47098. {
  47099. WC_RNG rng;
  47100. int rng_inited = 0;
  47101. wc_test_ret_t ret;
  47102. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  47103. int i, j;
  47104. #ifndef WOLFSSL_SP_MATH
  47105. int k;
  47106. #endif
  47107. mp_digit d = 0;
  47108. #endif
  47109. #ifdef WOLFSSL_SMALL_STACK
  47110. mp_int *a = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  47111. DYNAMIC_TYPE_TMP_BUFFER),
  47112. *b = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  47113. DYNAMIC_TYPE_TMP_BUFFER),
  47114. *r1 = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  47115. DYNAMIC_TYPE_TMP_BUFFER),
  47116. *r2 = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  47117. DYNAMIC_TYPE_TMP_BUFFER),
  47118. *p = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  47119. DYNAMIC_TYPE_TMP_BUFFER);
  47120. if ((a == NULL) ||
  47121. (b == NULL) ||
  47122. (r1 == NULL) ||
  47123. (r2 == NULL) ||
  47124. (p == NULL))
  47125. {
  47126. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  47127. }
  47128. #else
  47129. mp_int a[1], b[1], r1[1], r2[1], p[1];
  47130. #endif
  47131. WOLFSSL_ENTER("mp_test");
  47132. ret = mp_init_multi(a, b, r1, r2, NULL, NULL);
  47133. if (ret != 0)
  47134. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47135. #ifdef WOLFSSL_SP_MATH_ALL
  47136. mp_init_copy(p, a);
  47137. #else
  47138. ret = mp_init(p);
  47139. if (ret != 0)
  47140. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47141. #endif
  47142. #ifndef HAVE_FIPS
  47143. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  47144. #else
  47145. ret = wc_InitRng(&rng);
  47146. #endif
  47147. if (ret != 0)
  47148. goto done;
  47149. rng_inited = 1;
  47150. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  47151. mp_set_int(a, 0);
  47152. if (a->used != 0 || a->dp[0] != 0)
  47153. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  47154. for (j = 1; j <= MP_MAX_TEST_BYTE_LEN; j++) {
  47155. for (i = 0; i < 4 * j; i++) {
  47156. /* New values to use. */
  47157. ret = randNum(p, j, &rng, NULL);
  47158. if (ret != 0)
  47159. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47160. ret = randNum(a, j, &rng, NULL);
  47161. if (ret != 0)
  47162. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47163. ret = randNum(b, j, &rng, NULL);
  47164. if (ret != 0)
  47165. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47166. ret = wc_RNG_GenerateBlock(&rng, (byte*)&d, sizeof(d));
  47167. if (ret != 0)
  47168. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47169. d &= MP_MASK;
  47170. #if !defined(WOLFSSL_SP_MATH) || (defined(HAVE_ECC) && \
  47171. (defined(ECC_SHAMIR) || defined(FP_ECC)))
  47172. /* Ensure sqrmod produce same result as mulmod. */
  47173. ret = mp_sqrmod(a, p, r1);
  47174. if (ret != 0)
  47175. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47176. ret = mp_mulmod(a, a, p, r2);
  47177. if (ret != 0)
  47178. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47179. if (mp_cmp(r1, r2) != 0) {
  47180. WOLFSSL_MSG("Fail: mp_mulmod result does not match mp_sqrmod!");
  47181. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  47182. }
  47183. #endif
  47184. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  47185. #if defined(WOLFSSL_SP_MATH) || (defined(WOLFSSL_SP_MATH_ALL) && \
  47186. !defined(WOLFSSL_SP_INT_NEGATIVE))
  47187. ret = mp_addmod(a, b, p, r1);
  47188. if (ret != 0)
  47189. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47190. ret = mp_submod(r1, b, p, r2);
  47191. if (ret != 0)
  47192. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47193. ret = mp_mod(a, p, r1);
  47194. if (ret != 0)
  47195. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47196. if (mp_cmp(r1, r2) != MP_EQ)
  47197. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  47198. #else
  47199. /* Ensure add with mod produce same result as sub with mod. */
  47200. ret = mp_addmod(a, b, p, r1);
  47201. if (ret != 0)
  47202. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47203. b->sign ^= 1;
  47204. ret = mp_submod(a, b, p, r2);
  47205. if (ret != 0)
  47206. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47207. if (mp_cmp(r1, r2) != 0)
  47208. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  47209. #endif
  47210. #endif
  47211. /* Ensure add digit produce same result as sub digit. */
  47212. ret = mp_add_d(a, d, r1);
  47213. if (ret != 0)
  47214. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47215. ret = mp_sub_d(r1, d, r2);
  47216. if (ret != 0)
  47217. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47218. if (mp_cmp(a, r2) != 0)
  47219. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  47220. /* Invert - if p is even it will use the slow impl.
  47221. * - if p and a are even it will fail.
  47222. */
  47223. ret = mp_invmod(a, p, r1);
  47224. if (ret != 0 && ret != MP_VAL)
  47225. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47226. #ifndef WOLFSSL_SP_MATH
  47227. /* Shift up and down number all bits in a digit. */
  47228. for (k = 0; k < DIGIT_BIT; k++) {
  47229. mp_mul_2d(a, k, r1);
  47230. mp_div_2d(r1, k, r2, p);
  47231. if (mp_cmp(a, r2) != 0)
  47232. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  47233. if (!mp_iszero(p))
  47234. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  47235. mp_rshb(r1, k);
  47236. if (mp_cmp(a, r1) != 0)
  47237. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  47238. }
  47239. #endif
  47240. }
  47241. }
  47242. /* Test adding and subtracting zero from zero. */
  47243. mp_zero(a);
  47244. ret = mp_add_d(a, 0, r1);
  47245. if (ret != 0)
  47246. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47247. if (!mp_iszero(r1)) {
  47248. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  47249. }
  47250. ret = mp_sub_d(a, 0, r2);
  47251. if (ret != 0)
  47252. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47253. if (!mp_iszero(r2)) {
  47254. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  47255. }
  47256. #if DIGIT_BIT >= 32
  47257. /* Check that setting a 32-bit digit works. */
  47258. d &= 0xffffffffU;
  47259. mp_set_int(a, d);
  47260. if (a->used != 1 || a->dp[0] != d)
  47261. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  47262. #endif
  47263. /* Check setting a bit and testing a bit works. */
  47264. for (i = 0; i < MP_MAX_TEST_BYTE_LEN * 8; i++) {
  47265. mp_zero(a);
  47266. mp_set_bit(a, i);
  47267. if (!mp_is_bit_set(a, i))
  47268. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  47269. }
  47270. #endif
  47271. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  47272. mp_zero(a);
  47273. i = mp_cnt_lsb(a);
  47274. if (i != 0)
  47275. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  47276. mp_set(a, 1);
  47277. i = mp_cnt_lsb(a);
  47278. if (i != 0)
  47279. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  47280. mp_set(a, 32);
  47281. i = mp_cnt_lsb(a);
  47282. if (i != 5)
  47283. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  47284. mp_zero(a);
  47285. mp_set_bit(a, 129);
  47286. i = mp_cnt_lsb(a);
  47287. if (i != 129)
  47288. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  47289. #endif
  47290. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  47291. if ((ret = mp_test_param(a, b, r1, &rng)) != 0)
  47292. goto done;
  47293. #endif
  47294. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  47295. if ((ret = mp_test_div_3(a, r1, &rng)) != 0)
  47296. goto done;
  47297. #endif
  47298. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  47299. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  47300. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  47301. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  47302. if ((ret = mp_test_radix_10(a, r1, &rng)) != 0)
  47303. goto done;
  47304. #endif
  47305. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  47306. defined(HAVE_ECC))
  47307. if ((ret = mp_test_radix_16(a, r1, &rng)) != 0)
  47308. goto done;
  47309. #endif
  47310. if ((ret = mp_test_shift(a, r1, &rng)) != 0)
  47311. goto done;
  47312. if ((ret = mp_test_add_sub_d(a, r1)) != 0)
  47313. goto done;
  47314. if ((ret = mp_test_read_to_bin(a)) != 0)
  47315. goto done;
  47316. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  47317. if ((ret = mp_test_set_int(a)) != 0)
  47318. goto done;
  47319. #endif
  47320. if ((ret = mp_test_cmp(a, r1)) != 0)
  47321. goto done;
  47322. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  47323. if ((ret = mp_test_shbd(a, b, &rng)) != 0)
  47324. goto done;
  47325. #endif
  47326. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  47327. if ((ret = mp_test_set_is_bit(a)) != 0)
  47328. goto done;
  47329. #endif
  47330. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  47331. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && \
  47332. !defined(WOLFSSL_RSA_PUBLIC_ONLY))
  47333. if ((ret = mp_test_div(a, b, r1, r2, &rng)) != 0)
  47334. goto done;
  47335. #endif
  47336. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  47337. !defined(WC_NO_RNG)
  47338. if ((ret = mp_test_prime(a, &rng)) != 0)
  47339. goto done;
  47340. #endif
  47341. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  47342. if ((ret = mp_test_lcm_gcd(a, b, r1, r2, &rng)) != 0)
  47343. goto done;
  47344. #endif
  47345. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  47346. defined(WOLFSSL_SP_MATH_ALL)
  47347. if ((ret = mp_test_mod_2d(a, r1, p, &rng)) != 0)
  47348. goto done;
  47349. #endif
  47350. #if defined(WOLFSSL_SP_MATH_ALL) || defined(OPENSSL_EXTRA) || \
  47351. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY))
  47352. if ((ret = mp_test_mod_d(a, &rng)) != 0)
  47353. goto done;
  47354. #endif
  47355. if ((ret = mp_test_mul_sqr(a, b, r1, r2, &rng)) != 0)
  47356. goto done;
  47357. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  47358. defined(OPENSSL_EXTRA)
  47359. if ((ret = mp_test_invmod(a, b, r1)) != 0)
  47360. goto done;
  47361. #endif
  47362. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  47363. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  47364. if ((ret = mp_test_exptmod(a, b, r1, r2)) != 0)
  47365. goto done;
  47366. #endif
  47367. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  47368. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  47369. if ((ret = mp_test_mont(a, b, r1, r2, &rng)) != 0)
  47370. goto done;
  47371. #endif
  47372. done:
  47373. #ifdef WOLFSSL_SMALL_STACK
  47374. if (p) {
  47375. mp_clear(p);
  47376. XFREE(p, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47377. }
  47378. if (r2) {
  47379. mp_clear(r2);
  47380. XFREE(r2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47381. }
  47382. if (r1) {
  47383. mp_clear(r1);
  47384. XFREE(r1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47385. }
  47386. if (b) {
  47387. mp_clear(b);
  47388. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47389. }
  47390. if (a) {
  47391. mp_clear(a);
  47392. XFREE(a, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47393. }
  47394. #else
  47395. mp_clear(p);
  47396. mp_clear(r2);
  47397. mp_clear(r1);
  47398. mp_clear(b);
  47399. mp_clear(a);
  47400. #endif
  47401. if (rng_inited)
  47402. wc_FreeRng(&rng);
  47403. return ret;
  47404. }
  47405. #endif /* WOLFSSL_PUBLIC_MP && ((WOLFSSL_SP_MATH_ALL &&
  47406. * !WOLFSSL_RSA_VERIFY_ONLY) || USE_FAST_MATH) */
  47407. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  47408. typedef struct pairs_t {
  47409. const unsigned char* coeff;
  47410. int coeffSz;
  47411. int exp;
  47412. } pairs_t;
  47413. /*
  47414. n =p1p2p3, where pi = ki(p1-1)+1 with (k2,k3) = (173,293)
  47415. p1 = 2^192 * 0x000000000000e24fd4f6d6363200bf2323ec46285cac1d3a
  47416. + 2^0 * 0x0b2488b0c29d96c5e67f8bec15b54b189ae5636efe89b45b
  47417. */
  47418. static const unsigned char c192a[] =
  47419. {
  47420. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x4f,
  47421. 0xd4, 0xf6, 0xd6, 0x36, 0x32, 0x00, 0xbf, 0x23,
  47422. 0x23, 0xec, 0x46, 0x28, 0x5c, 0xac, 0x1d, 0x3a
  47423. };
  47424. static const unsigned char c0a[] =
  47425. {
  47426. 0x0b, 0x24, 0x88, 0xb0, 0xc2, 0x9d, 0x96, 0xc5,
  47427. 0xe6, 0x7f, 0x8b, 0xec, 0x15, 0xb5, 0x4b, 0x18,
  47428. 0x9a, 0xe5, 0x63, 0x6e, 0xfe, 0x89, 0xb4, 0x5b
  47429. };
  47430. static const pairs_t ecPairsA[] =
  47431. {
  47432. {c192a, sizeof(c192a), 192},
  47433. {c0a, sizeof(c0a), 0}
  47434. };
  47435. static const int kA[] = {173, 293};
  47436. static const unsigned char controlPrime[] = {
  47437. 0xe1, 0x76, 0x45, 0x80, 0x59, 0xb6, 0xd3, 0x49,
  47438. 0xdf, 0x0a, 0xef, 0x12, 0xd6, 0x0f, 0xf0, 0xb7,
  47439. 0xcb, 0x2a, 0x37, 0xbf, 0xa7, 0xf8, 0xb5, 0x4d,
  47440. 0xf5, 0x31, 0x35, 0xad, 0xe4, 0xa3, 0x94, 0xa1,
  47441. 0xdb, 0xf1, 0x96, 0xad, 0xb5, 0x05, 0x64, 0x85,
  47442. 0x83, 0xfc, 0x1b, 0x5b, 0x29, 0xaa, 0xbe, 0xf8,
  47443. 0x26, 0x3f, 0x76, 0x7e, 0xad, 0x1c, 0xf0, 0xcb,
  47444. 0xd7, 0x26, 0xb4, 0x1b, 0x05, 0x8e, 0x56, 0x86,
  47445. 0x7e, 0x08, 0x62, 0x21, 0xc1, 0x86, 0xd6, 0x47,
  47446. 0x79, 0x3e, 0xb7, 0x5d, 0xa4, 0xc6, 0x3a, 0xd7,
  47447. 0xb1, 0x74, 0x20, 0xf6, 0x50, 0x97, 0x41, 0x04,
  47448. 0x53, 0xed, 0x3f, 0x26, 0xd6, 0x6f, 0x91, 0xfa,
  47449. 0x68, 0x26, 0xec, 0x2a, 0xdc, 0x9a, 0xf1, 0xe7,
  47450. 0xdc, 0xfb, 0x73, 0xf0, 0x79, 0x43, 0x1b, 0x21,
  47451. 0xa3, 0x59, 0x04, 0x63, 0x52, 0x07, 0xc9, 0xd7,
  47452. 0xe6, 0xd1, 0x1b, 0x5d, 0x5e, 0x96, 0xfa, 0x53
  47453. };
  47454. static const unsigned char testOne[] = { 1 };
  47455. static wc_test_ret_t GenerateNextP(mp_int* p1, mp_int* p2, int k)
  47456. {
  47457. wc_test_ret_t ret;
  47458. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  47459. mp_int *ki = (mp_int *)XMALLOC(sizeof(*ki), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47460. if (ki == NULL)
  47461. return MEMORY_E;
  47462. #else
  47463. mp_int ki[1];
  47464. #endif
  47465. ret = mp_init(ki);
  47466. if (ret != 0)
  47467. ret = WC_TEST_RET_ENC_EC(ret);
  47468. if (ret == 0) {
  47469. ret = mp_set(ki, (mp_digit)k);
  47470. if (ret != 0)
  47471. ret = WC_TEST_RET_ENC_EC(ret);
  47472. }
  47473. if (ret == 0) {
  47474. ret = mp_sub_d(p1, 1, p2);
  47475. if (ret != 0)
  47476. ret = WC_TEST_RET_ENC_EC(ret);
  47477. }
  47478. if (ret == 0) {
  47479. ret = mp_mul(p2, ki, p2);
  47480. if (ret != 0)
  47481. ret = WC_TEST_RET_ENC_EC(ret);
  47482. }
  47483. if (ret == 0) {
  47484. ret = mp_add_d(p2, 1, p2);
  47485. if (ret != 0)
  47486. ret = WC_TEST_RET_ENC_EC(ret);
  47487. }
  47488. mp_clear(ki);
  47489. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  47490. XFREE(ki, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47491. #endif
  47492. return ret;
  47493. }
  47494. static wc_test_ret_t GenerateP(mp_int* p1, mp_int* p2, mp_int* p3,
  47495. const pairs_t* ecPairs, int ecPairsSz,
  47496. const int* k)
  47497. {
  47498. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  47499. mp_int *x = NULL, *y = NULL;
  47500. #else
  47501. mp_int x[1], y[1];
  47502. #endif
  47503. wc_test_ret_t ret;
  47504. int i;
  47505. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  47506. if (((x = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) ||
  47507. ((y = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) {
  47508. ret = MEMORY_E;
  47509. goto out;
  47510. }
  47511. #endif
  47512. ret = mp_init_multi(x, y, NULL, NULL, NULL, NULL);
  47513. if (ret != 0) {
  47514. ret = WC_TEST_RET_ENC_EC(ret);
  47515. goto out;
  47516. }
  47517. for (i = 0; ret == 0 && i < ecPairsSz; i++) {
  47518. ret = mp_read_unsigned_bin(x, ecPairs[i].coeff, (word32)ecPairs[i].coeffSz);
  47519. if (ret != 0) {
  47520. ret = WC_TEST_RET_ENC_EC(ret);
  47521. break;
  47522. }
  47523. /* p1 = 2^exp */
  47524. ret = mp_2expt(y, ecPairs[i].exp);
  47525. if (ret != 0) {
  47526. ret = WC_TEST_RET_ENC_EC(ret);
  47527. break;
  47528. }
  47529. /* p1 = p1 * m */
  47530. ret = mp_mul(x, y, x);
  47531. if (ret != 0) {
  47532. ret = WC_TEST_RET_ENC_EC(ret);
  47533. break;
  47534. }
  47535. /* p1 += */
  47536. ret = mp_add(p1, x, p1);
  47537. if (ret != 0) {
  47538. ret = WC_TEST_RET_ENC_EC(ret);
  47539. break;
  47540. }
  47541. mp_zero(x);
  47542. mp_zero(y);
  47543. }
  47544. if (ret == 0)
  47545. ret = GenerateNextP(p1, p2, k[0]);
  47546. if (ret == 0)
  47547. ret = GenerateNextP(p1, p3, k[1]);
  47548. out:
  47549. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  47550. if (x != NULL) {
  47551. mp_clear(x);
  47552. XFREE(x, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47553. }
  47554. if (y != NULL) {
  47555. mp_clear(y);
  47556. XFREE(y, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47557. }
  47558. #else
  47559. mp_clear(x);
  47560. mp_clear(y);
  47561. #endif
  47562. return ret;
  47563. }
  47564. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t prime_test(void)
  47565. {
  47566. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  47567. mp_int *n = (mp_int *)XMALLOC(sizeof *n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  47568. *p1 = (mp_int *)XMALLOC(sizeof *p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  47569. *p2 = (mp_int *)XMALLOC(sizeof *p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  47570. *p3 = (mp_int *)XMALLOC(sizeof *p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47571. #else
  47572. mp_int n[1],
  47573. p1[1],
  47574. p2[1],
  47575. p3[1];
  47576. #endif
  47577. wc_test_ret_t ret;
  47578. int isPrime = 0;
  47579. WC_RNG rng;
  47580. WOLFSSL_ENTER("prime_test");
  47581. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  47582. if ((n == NULL) ||
  47583. (p1 == NULL) ||
  47584. (p2 == NULL) ||
  47585. (p3 == NULL))
  47586. ERROR_OUT(MEMORY_E, out);
  47587. #endif
  47588. ret = wc_InitRng(&rng);
  47589. if (ret != 0)
  47590. ret = WC_TEST_RET_ENC_EC(ret);
  47591. if (ret == 0) {
  47592. ret = mp_init_multi(n, p1, p2, p3, NULL, NULL);
  47593. if (ret != 0)
  47594. ret = WC_TEST_RET_ENC_EC(ret);
  47595. }
  47596. if (ret == 0)
  47597. ret = GenerateP(p1, p2, p3,
  47598. ecPairsA, sizeof(ecPairsA) / sizeof(ecPairsA[0]), kA);
  47599. if (ret == 0) {
  47600. ret = mp_mul(p1, p2, n);
  47601. if (ret != 0)
  47602. ret = WC_TEST_RET_ENC_EC(ret);
  47603. }
  47604. if (ret == 0) {
  47605. ret = mp_mul(n, p3, n);
  47606. if (ret != 0)
  47607. ret = WC_TEST_RET_ENC_EC(ret);
  47608. }
  47609. if (ret != 0)
  47610. ERROR_OUT(ret, out);
  47611. /* Check the old prime test using the number that false positives.
  47612. * This test result should indicate as not prime. */
  47613. ret = mp_prime_is_prime(n, 40, &isPrime);
  47614. if (ret != 0)
  47615. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  47616. if (isPrime)
  47617. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  47618. /* This test result should fail. It should indicate the value as prime. */
  47619. ret = mp_prime_is_prime(n, 8, &isPrime);
  47620. if (ret != 0)
  47621. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  47622. if (!isPrime)
  47623. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  47624. /* This test result should indicate the value as not prime. */
  47625. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  47626. if (ret != 0)
  47627. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  47628. if (isPrime)
  47629. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  47630. ret = mp_read_unsigned_bin(n, controlPrime, sizeof(controlPrime));
  47631. if (ret != 0)
  47632. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  47633. /* This test result should indicate the value as prime. */
  47634. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  47635. if (ret != 0)
  47636. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  47637. if (!isPrime)
  47638. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  47639. /* This test result should indicate the value as prime. */
  47640. isPrime = -1;
  47641. ret = mp_prime_is_prime(n, 8, &isPrime);
  47642. if (ret != 0)
  47643. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  47644. if (!isPrime)
  47645. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  47646. ret = mp_read_unsigned_bin(n, testOne, sizeof(testOne));
  47647. if (ret != 0)
  47648. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  47649. /* This test result should indicate the value as not prime. */
  47650. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  47651. if (ret != 0)
  47652. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  47653. if (isPrime)
  47654. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  47655. ret = mp_prime_is_prime(n, 8, &isPrime);
  47656. if (ret != 0)
  47657. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  47658. if (isPrime)
  47659. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  47660. ret = 0;
  47661. out:
  47662. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  47663. if (n != NULL) {
  47664. mp_clear(n);
  47665. XFREE(n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47666. }
  47667. if (p1 != NULL) {
  47668. mp_clear(p1);
  47669. XFREE(p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47670. }
  47671. if (p2 != NULL) {
  47672. mp_clear(p2);
  47673. XFREE(p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47674. }
  47675. if (p3 != NULL) {
  47676. mp_clear(p3);
  47677. XFREE(p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47678. }
  47679. #else
  47680. mp_clear(p3);
  47681. mp_clear(p2);
  47682. mp_clear(p1);
  47683. mp_clear(n);
  47684. #endif
  47685. wc_FreeRng(&rng);
  47686. return ret;
  47687. }
  47688. #endif /* WOLFSSL_PUBLIC_MP */
  47689. #if defined(ASN_BER_TO_DER) && \
  47690. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  47691. defined(OPENSSL_EXTRA_X509_SMALL))
  47692. /* wc_BerToDer is only public facing in the case of test cert or opensslextra */
  47693. typedef struct berDerTestData {
  47694. const byte *in;
  47695. word32 inSz;
  47696. const byte *out;
  47697. word32 outSz;
  47698. } berDerTestData;
  47699. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t berder_test(void)
  47700. {
  47701. wc_test_ret_t ret;
  47702. int i;
  47703. word32 len = 0, l;
  47704. byte out[32];
  47705. WOLFSSL_SMALL_STACK_STATIC const byte good1_in[] = { 0x30, 0x80, 0x00, 0x00 };
  47706. WOLFSSL_SMALL_STACK_STATIC const byte good1_out[] = { 0x30, 0x00 };
  47707. WOLFSSL_SMALL_STACK_STATIC const byte good2_in[] = { 0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00 };
  47708. WOLFSSL_SMALL_STACK_STATIC const byte good2_out[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  47709. WOLFSSL_SMALL_STACK_STATIC const byte good3_in[] = {
  47710. 0x24, 0x80, 0x04, 0x01, 0x01, 0x00, 0x00
  47711. };
  47712. WOLFSSL_SMALL_STACK_STATIC const byte good3_out[] = { 0x04, 0x1, 0x01 };
  47713. WOLFSSL_SMALL_STACK_STATIC const byte good4_in[] = {
  47714. 0x30, 0x80,
  47715. 0x02, 0x01, 0x01,
  47716. 0x30, 0x80,
  47717. 0x24, 0x80,
  47718. 0x04, 0x01, 0x01,
  47719. 0x04, 0x02, 0x02, 0x03,
  47720. 0x00, 0x00,
  47721. 0x06, 0x01, 0x01,
  47722. 0x00, 0x00,
  47723. 0x31, 0x80,
  47724. 0x06, 0x01, 0x01,
  47725. 0x00, 0x00,
  47726. 0x00, 0x00,
  47727. };
  47728. WOLFSSL_SMALL_STACK_STATIC const byte good4_out[] = {
  47729. 0x30, 0x12,
  47730. 0x02, 0x01, 0x01,
  47731. 0x30, 0x08,
  47732. 0x04, 0x03, 0x01, 0x02, 0x03,
  47733. 0x06, 0x01, 0x01,
  47734. 0x31, 0x03,
  47735. 0x06, 0x01, 0x01
  47736. };
  47737. WOLFSSL_SMALL_STACK_STATIC const byte good5_in[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  47738. berDerTestData testData[] = {
  47739. { good1_in, sizeof(good1_in), good1_out, sizeof(good1_out) },
  47740. { good2_in, sizeof(good2_in), good2_out, sizeof(good2_out) },
  47741. { good3_in, sizeof(good3_in), good3_out, sizeof(good3_out) },
  47742. { good4_in, sizeof(good4_in), good4_out, sizeof(good4_out) },
  47743. { good5_in, sizeof(good5_in), good5_in , sizeof(good5_in ) },
  47744. };
  47745. WOLFSSL_ENTER("berder_test");
  47746. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  47747. ret = wc_BerToDer(testData[i].in, testData[i].inSz, NULL, &len);
  47748. if (ret != LENGTH_ONLY_E)
  47749. return WC_TEST_RET_ENC_I(i);
  47750. if (len != testData[i].outSz)
  47751. return WC_TEST_RET_ENC_I(i);
  47752. len = testData[i].outSz;
  47753. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &len);
  47754. if (ret != 0)
  47755. return WC_TEST_RET_ENC_I(i);
  47756. if (XMEMCMP(out, testData[i].out, len) != 0)
  47757. return WC_TEST_RET_ENC_I(i);
  47758. for (l = 1; l < testData[i].inSz; l++) {
  47759. ret = wc_BerToDer(testData[i].in, l, NULL, &len);
  47760. if (ret != ASN_PARSE_E)
  47761. return WC_TEST_RET_ENC_EC(ret);
  47762. len = testData[i].outSz;
  47763. ret = wc_BerToDer(testData[i].in, l, out, &len);
  47764. if (ret != ASN_PARSE_E)
  47765. return WC_TEST_RET_ENC_EC(ret);
  47766. }
  47767. for (l = 0; l < testData[i].outSz-1; l++) {
  47768. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &l);
  47769. if (ret != BUFFER_E)
  47770. return WC_TEST_RET_ENC_EC(ret);
  47771. }
  47772. }
  47773. ret = wc_BerToDer(NULL, 4, NULL, NULL);
  47774. if (ret != BAD_FUNC_ARG)
  47775. return WC_TEST_RET_ENC_EC(ret);
  47776. ret = wc_BerToDer(out, 4, NULL, NULL);
  47777. if (ret != BAD_FUNC_ARG)
  47778. return WC_TEST_RET_ENC_EC(ret);
  47779. ret = wc_BerToDer(NULL, 4, NULL, &len);
  47780. if (ret != BAD_FUNC_ARG)
  47781. return WC_TEST_RET_ENC_EC(ret);
  47782. ret = wc_BerToDer(NULL, 4, out, NULL);
  47783. if (ret != BAD_FUNC_ARG)
  47784. return WC_TEST_RET_ENC_EC(ret);
  47785. ret = wc_BerToDer(out, 4, out, NULL);
  47786. if (ret != BAD_FUNC_ARG)
  47787. return WC_TEST_RET_ENC_EC(ret);
  47788. ret = wc_BerToDer(NULL, 4, out, &len);
  47789. if (ret != BAD_FUNC_ARG)
  47790. return WC_TEST_RET_ENC_EC(ret);
  47791. for (l = 1; l < sizeof(good4_out); l++) {
  47792. len = l;
  47793. ret = wc_BerToDer(good4_in, sizeof(good4_in), out, &len);
  47794. if (ret != BUFFER_E)
  47795. return WC_TEST_RET_ENC_EC(ret);
  47796. }
  47797. return 0;
  47798. }
  47799. #endif
  47800. #ifdef DEBUG_WOLFSSL
  47801. static THREAD_LS_T int log_cnt = 0;
  47802. static void my_Logging_cb(const int logLevel, const char *const logMessage)
  47803. {
  47804. (void)logLevel;
  47805. (void)logMessage;
  47806. log_cnt++;
  47807. }
  47808. #endif /* DEBUG_WOLFSSL */
  47809. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t logging_test(void)
  47810. {
  47811. wc_test_ret_t ret;
  47812. #ifdef DEBUG_WOLFSSL
  47813. const char* msg = "Testing, testing. 1, 2, 3, 4 ...";
  47814. byte a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  47815. byte b[256];
  47816. int i;
  47817. WOLFSSL_ENTER("logging_test (debug)");
  47818. for (i = 0; i < (int)sizeof(b); i++)
  47819. b[i] = i;
  47820. ret = wolfSSL_Debugging_ON();
  47821. if (ret != 0)
  47822. return WC_TEST_RET_ENC_EC(ret);
  47823. ret = wolfSSL_SetLoggingCb(my_Logging_cb);
  47824. if (ret != 0)
  47825. return WC_TEST_RET_ENC_EC(ret);
  47826. WOLFSSL_MSG(msg);
  47827. WOLFSSL_BUFFER(a, sizeof(a));
  47828. WOLFSSL_BUFFER(b, sizeof(b));
  47829. WOLFSSL_BUFFER(NULL, 0);
  47830. WOLFSSL_ERROR(MEMORY_E);
  47831. WOLFSSL_ERROR_MSG(msg);
  47832. /* turn off logs */
  47833. wolfSSL_Debugging_OFF();
  47834. /* capture log count */
  47835. i = log_cnt;
  47836. /* validate no logs are output when disabled */
  47837. WOLFSSL_MSG(msg);
  47838. WOLFSSL_BUFFER(a, sizeof(a));
  47839. WOLFSSL_BUFFER(b, sizeof(b));
  47840. WOLFSSL_BUFFER(NULL, 0);
  47841. WOLFSSL_ERROR(MEMORY_E);
  47842. WOLFSSL_ERROR_MSG(msg);
  47843. /* check the logs were disabled */
  47844. if (i != log_cnt)
  47845. return WC_TEST_RET_ENC_NC;
  47846. /* restore callback and leave logging enabled */
  47847. wolfSSL_SetLoggingCb(NULL);
  47848. wolfSSL_Debugging_ON();
  47849. /* suppress unused args */
  47850. (void)a;
  47851. (void)b;
  47852. #else
  47853. WOLFSSL_ENTER("logging_test");
  47854. ret = wolfSSL_Debugging_ON();
  47855. if (ret != NOT_COMPILED_IN)
  47856. return WC_TEST_RET_ENC_EC(ret);
  47857. wolfSSL_Debugging_OFF();
  47858. ret = wolfSSL_SetLoggingCb(NULL);
  47859. if (ret != NOT_COMPILED_IN)
  47860. return WC_TEST_RET_ENC_EC(ret);
  47861. #endif /* DEBUG_WOLFSSL */
  47862. return 0;
  47863. }
  47864. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  47865. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t wolfcrypt_mutex_test(void)
  47866. #else
  47867. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mutex_test(void)
  47868. #endif
  47869. {
  47870. #ifdef WOLFSSL_PTHREADS
  47871. wolfSSL_Mutex m;
  47872. #endif
  47873. #if defined(WOLFSSL_PTHREADS) || (!defined(WOLFSSL_NO_MALLOC) && \
  47874. !defined(WOLFSSL_USER_MUTEX) && defined(WOLFSSL_STATIC_MEMORY))
  47875. wc_test_ret_t ret;
  47876. #endif
  47877. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_USER_MUTEX)
  47878. #ifndef WOLFSSL_STATIC_MEMORY
  47879. wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
  47880. WOLFSSL_ENTER("[wolfcrypt_]mutex_test (1)");
  47881. #else
  47882. wolfSSL_Mutex *mm = (wolfSSL_Mutex*) XMALLOC(sizeof(wolfSSL_Mutex),
  47883. HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  47884. WOLFSSL_ENTER("[wolfcrypt_]mutex_test (2)");
  47885. if (mm != NULL) {
  47886. ret = wc_InitMutex(mm);
  47887. if (ret != 0) {
  47888. WOLFSSL_MSG("Init Mutex failed");
  47889. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  47890. return WC_TEST_RET_ENC_EC(ret);
  47891. }
  47892. }
  47893. #endif
  47894. if (mm == NULL)
  47895. return WC_TEST_RET_ENC_ERRNO;
  47896. wc_FreeMutex(mm);
  47897. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  47898. #endif
  47899. /* Can optionally enable advanced pthread tests using "ENABLE_PTHREAD_LOCKFREE_TESTS" */
  47900. #ifdef WOLFSSL_PTHREADS
  47901. ret = wc_InitMutex(&m);
  47902. if (ret != 0)
  47903. return WC_TEST_RET_ENC_EC(ret);
  47904. ret = wc_LockMutex(&m);
  47905. if (ret != 0)
  47906. return WC_TEST_RET_ENC_EC(ret);
  47907. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  47908. /* trying to free a locked mutex is not portable behavior with pthread */
  47909. /* Attempting to destroy a locked mutex results in undefined behavior */
  47910. ret = wc_FreeMutex(&m);
  47911. if (ret != BAD_MUTEX_E)
  47912. return WC_TEST_RET_ENC_EC(ret);
  47913. #endif
  47914. ret = wc_UnLockMutex(&m);
  47915. if (ret != 0)
  47916. return WC_TEST_RET_ENC_EC(ret);
  47917. ret = wc_FreeMutex(&m);
  47918. if (ret != 0)
  47919. return WC_TEST_RET_ENC_EC(ret);
  47920. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  47921. /* Trying to use a pthread after free'ing is not portable behavior */
  47922. ret = wc_LockMutex(&m);
  47923. if (ret != BAD_MUTEX_E)
  47924. return WC_TEST_RET_ENC_EC(ret);
  47925. ret = wc_UnLockMutex(&m);
  47926. if (ret != BAD_MUTEX_E)
  47927. return WC_TEST_RET_ENC_EC(ret);
  47928. #endif
  47929. #endif
  47930. return 0;
  47931. }
  47932. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  47933. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  47934. !defined(WOLFSSL_STATIC_MEMORY)
  47935. static wc_test_ret_t malloc_cnt = 0;
  47936. static wc_test_ret_t realloc_cnt = 0;
  47937. static wc_test_ret_t free_cnt = 0;
  47938. #ifdef WOLFSSL_DEBUG_MEMORY
  47939. static void *my_Malloc_cb(size_t size, const char* func, unsigned int line)
  47940. {
  47941. (void) func;
  47942. (void) line;
  47943. #else
  47944. static void *my_Malloc_cb(size_t size)
  47945. {
  47946. #endif
  47947. malloc_cnt++;
  47948. #ifndef WOLFSSL_NO_MALLOC
  47949. return malloc(size);
  47950. #else
  47951. WOLFSSL_MSG("No malloc available");
  47952. (void)size;
  47953. return NULL;
  47954. #endif
  47955. }
  47956. #ifdef WOLFSSL_DEBUG_MEMORY
  47957. static void my_Free_cb(void *ptr, const char* func, unsigned int line)
  47958. {
  47959. (void) func;
  47960. (void) line;
  47961. #else
  47962. static void my_Free_cb(void *ptr)
  47963. {
  47964. #endif
  47965. free_cnt++;
  47966. #ifndef WOLFSSL_NO_MALLOC
  47967. free(ptr);
  47968. #else
  47969. WOLFSSL_MSG("No free available");
  47970. (void)ptr;
  47971. #endif
  47972. }
  47973. #ifdef WOLFSSL_DEBUG_MEMORY
  47974. static void *my_Realloc_cb(void *ptr, size_t size, const char* func, unsigned int line)
  47975. {
  47976. (void) func;
  47977. (void) line;
  47978. #else
  47979. static void *my_Realloc_cb(void *ptr, size_t size)
  47980. {
  47981. #endif
  47982. realloc_cnt++;
  47983. #ifndef WOLFSSL_NO_MALLOC
  47984. return realloc(ptr, size);
  47985. #else
  47986. WOLFSSL_MSG("No realloc available");
  47987. (void)ptr;
  47988. (void)size;
  47989. return NULL;
  47990. #endif
  47991. }
  47992. #endif /* !WOLFSSL_NO_MALLOC */
  47993. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memcb_test(void)
  47994. {
  47995. wc_test_ret_t ret = 0;
  47996. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  47997. !defined(WOLFSSL_STATIC_MEMORY)
  47998. byte* b = NULL;
  47999. #endif
  48000. wolfSSL_Malloc_cb mc;
  48001. wolfSSL_Free_cb fc;
  48002. wolfSSL_Realloc_cb rc;
  48003. WOLFSSL_ENTER("memcb_test");
  48004. /* Save existing memory callbacks */
  48005. ret = wolfSSL_GetAllocators(&mc, &fc, &rc);
  48006. if (ret != 0)
  48007. return WC_TEST_RET_ENC_EC(ret);
  48008. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  48009. !defined(WOLFSSL_STATIC_MEMORY)
  48010. /* test realloc */
  48011. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48012. if (b == NULL) {
  48013. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_memcb);
  48014. }
  48015. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48016. b = NULL;
  48017. /* Use API. */
  48018. ret = wolfSSL_SetAllocators((wolfSSL_Malloc_cb)my_Malloc_cb,
  48019. (wolfSSL_Free_cb)my_Free_cb,
  48020. (wolfSSL_Realloc_cb)my_Realloc_cb);
  48021. if (ret != 0) {
  48022. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_memcb);
  48023. }
  48024. b = (byte*)XMALLOC(1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48025. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48026. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48027. #ifndef WOLFSSL_STATIC_MEMORY
  48028. #ifndef WOLFSSL_CHECK_MEM_ZERO
  48029. if (malloc_cnt != 1 || free_cnt != 1 || realloc_cnt != 1)
  48030. #else
  48031. /* Checking zeroized memory means realloc is a malloc and free. */
  48032. if (malloc_cnt != 2 || free_cnt != 2 || realloc_cnt != 0)
  48033. #endif
  48034. #else
  48035. if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
  48036. #endif
  48037. ret = WC_TEST_RET_ENC_NC;
  48038. #endif /* !WOLFSSL_NO_MALLOC */
  48039. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  48040. !defined(WOLFSSL_STATIC_MEMORY)
  48041. exit_memcb:
  48042. /* reset malloc/free/realloc counts */
  48043. malloc_cnt = 0;
  48044. free_cnt = 0;
  48045. realloc_cnt = 0;
  48046. #endif
  48047. /* restore memory callbacks */
  48048. wolfSSL_SetAllocators(mc, fc, rc);
  48049. return ret;
  48050. }
  48051. #endif /* USE_WOLFSSL_MEMORY && !WOLFSSL_NO_MALLOC */
  48052. #if defined(WOLFSSL_CAAM_BLOB)
  48053. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blob_test(void)
  48054. {
  48055. wc_test_ret_t ret = 0;
  48056. byte out[112];
  48057. byte blob[112];
  48058. word32 outSz;
  48059. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  48060. {
  48061. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  48062. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  48063. };
  48064. WOLFSSL_SMALL_STACK_STATIC const byte text[] =
  48065. {
  48066. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  48067. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  48068. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  48069. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  48070. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  48071. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  48072. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  48073. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  48074. };
  48075. WOLFSSL_ENTER("blob_test");
  48076. XMEMSET(blob, 0, sizeof(blob));
  48077. XMEMSET(out, 0, sizeof(out));
  48078. outSz = sizeof(blob);
  48079. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  48080. if (ret != 0)
  48081. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  48082. blob[outSz - 2] += 1;
  48083. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  48084. if (ret == 0) { /* should fail with altered blob */
  48085. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_blob);
  48086. }
  48087. XMEMSET(blob, 0, sizeof(blob));
  48088. outSz = sizeof(blob);
  48089. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  48090. if (ret != 0)
  48091. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  48092. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  48093. if (ret != 0)
  48094. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  48095. if (XMEMCMP(out, iv, sizeof(iv))) {
  48096. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_blob);
  48097. }
  48098. XMEMSET(blob, 0, sizeof(blob));
  48099. outSz = sizeof(blob);
  48100. ret = wc_caamCreateBlob((byte*)text, sizeof(text), blob, &outSz);
  48101. if (ret != 0)
  48102. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  48103. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  48104. if (ret != 0)
  48105. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  48106. if (XMEMCMP(out, text, sizeof(text))) {
  48107. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_blob);
  48108. }
  48109. exit_blob:
  48110. return ret;
  48111. }
  48112. #endif /* WOLFSSL_CAAM_BLOB */
  48113. #ifdef WOLF_CRYPTO_CB
  48114. /* Example custom context for crypto callback */
  48115. typedef struct {
  48116. int exampleVar; /* flag for testing if only crypt is enabled. */
  48117. } myCryptoDevCtx;
  48118. #ifdef WOLF_CRYPTO_CB_ONLY_RSA
  48119. /* Testing rsa cb when CB_ONLY_RSA is enabled
  48120. * When CB_ONLY_RSA is enabled, software imple. is not available.
  48121. *
  48122. * ctx callback ctx
  48123. * returen 0 on success, otherwise return negative
  48124. */
  48125. static wc_test_ret_t rsa_onlycb_test(myCryptoDevCtx *ctx)
  48126. {
  48127. wc_test_ret_t ret = 0;
  48128. #if !defined(NO_RSA)
  48129. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  48130. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key,
  48131. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48132. byte* tmp = NULL;
  48133. #else
  48134. RsaKey key[1];
  48135. byte tmp[FOURK_BUF];
  48136. #endif
  48137. size_t bytes;
  48138. const word32 inLen = (word32)TEST_STRING_SZ;
  48139. word32 idx = 0;
  48140. word32 sigSz;
  48141. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  48142. byte out[RSA_TEST_BYTES];
  48143. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  48144. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  48145. !defined(NO_FILESYSTEM)
  48146. XFILE file;
  48147. #endif
  48148. #ifdef WOLFSSL_KEY_GEN
  48149. WC_RNG rng;
  48150. #endif
  48151. #ifdef USE_CERT_BUFFERS_1024
  48152. bytes = (size_t)sizeof_client_key_der_1024;
  48153. if (bytes < (size_t)sizeof_client_cert_der_1024)
  48154. bytes = (size_t)sizeof_client_cert_der_1024;
  48155. #elif defined(USE_CERT_BUFFERS_2048)
  48156. bytes = (size_t)sizeof_client_key_der_2048;
  48157. if (bytes < (size_t)sizeof_client_cert_der_2048)
  48158. bytes = (size_t)sizeof_client_cert_der_2048;
  48159. #elif defined(USE_CERT_BUFFERS_3072)
  48160. bytes = (size_t)sizeof_client_key_der_3072;
  48161. if (bytes < (size_t)sizeof_client_cert_der_3072)
  48162. bytes = (size_t)sizeof_client_cert_der_3072;
  48163. #elif defined(USE_CERT_BUFFERS_4096)
  48164. bytes = (size_t)sizeof_client_key_der_4096;
  48165. if (bytes < (size_t)sizeof_client_cert_der_4096)
  48166. bytes = (size_t)sizeof_client_cert_der_4096;
  48167. #else
  48168. bytes = FOURK_BUF;
  48169. #endif
  48170. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  48171. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48172. if (tmp == NULL)
  48173. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_onlycb);
  48174. #endif
  48175. #ifdef USE_CERT_BUFFERS_1024
  48176. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  48177. #elif defined(USE_CERT_BUFFERS_2048)
  48178. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  48179. #elif defined(USE_CERT_BUFFERS_3072)
  48180. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  48181. #elif defined(USE_CERT_BUFFERS_4096)
  48182. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  48183. #elif !defined(NO_FILESYSTEM)
  48184. file = XFOPEN(clientKey, "rb");
  48185. if (!file) {
  48186. ret = WC_TEST_RET_ENC_ERRNO;
  48187. err_sys("can't open ./certs/client-key.der, "
  48188. "Please run from wolfSSL home dir", ret);
  48189. ERROR_OUT(ret, exit_onlycb);
  48190. }
  48191. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  48192. XFCLOSE(file);
  48193. if (bytes == 0)
  48194. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_onlycb);
  48195. #endif
  48196. #ifdef WOLFSSL_KEY_GEN
  48197. /* wc_CryptoCb_MakeRsaKey cb test, no actual making key
  48198. * wc_MakeRsaKey() -> rsa cb ->
  48199. * myCryptoDevCb -> wc_MakeRsaKey(CBONLY_TEST_DEVID)
  48200. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return 0(success)
  48201. */
  48202. ctx->exampleVar = 99;
  48203. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  48204. if (ret != 0)
  48205. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48206. /* wc_MakeRsaKey() -> rsa cb ->
  48207. * myCryptoDevCb -> wc_MakeRsaKey(INVALID_DEVID)
  48208. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return NO_VALID_DEVID(failure)
  48209. */
  48210. ctx->exampleVar = 1;
  48211. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  48212. if (ret != NO_VALID_DEVID) {
  48213. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48214. } else
  48215. /* reset return code */
  48216. ret = 0;
  48217. #endif
  48218. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  48219. if (ret != 0)
  48220. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48221. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  48222. if (ret != 0)
  48223. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48224. sigSz = (word32)wc_RsaEncryptSize(key);
  48225. /* wc_CryptoCb_Rsa cb test, no actual rsa operation */
  48226. if (ret == 0) {
  48227. /* wc_SignatureGenerate() -> rsa cb ->
  48228. * myCryptoDevCb -> wc_RsaFunction(CBONLY_TEST_DEVID)
  48229. * wc_RsaFunction(CBONLY_TEST_DEVID) expects to return 0(success)
  48230. */
  48231. ctx->exampleVar = 99;
  48232. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  48233. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  48234. if (ret != 0)
  48235. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48236. }
  48237. if (ret == 0) {
  48238. /* wc_SignatureGenerate() -> rsa cb ->
  48239. * myCryptoDevCb -> wc_RsaFunction(INVALID_DEVID)
  48240. * wc_SignatureGenerate(INVALID_DEVID) expects to
  48241. * return NO_VALID_DEVID(failure)
  48242. */
  48243. ctx->exampleVar = 1;
  48244. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  48245. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  48246. if (ret != NO_VALID_DEVID) {
  48247. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48248. } else
  48249. /* reset return code */
  48250. ret = 0;
  48251. }
  48252. exit_onlycb:
  48253. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  48254. if (key != NULL) {
  48255. wc_FreeRsaKey(key);
  48256. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48257. }
  48258. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48259. #else
  48260. wc_FreeRsaKey(key);
  48261. #endif
  48262. #endif
  48263. return ret;
  48264. }
  48265. #endif
  48266. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  48267. /* Testing rsa cb when CB_ONLY_ECC is enabled
  48268. * When CB_ONLY_ECC is enabled, software imple. is not available.
  48269. *
  48270. * ctx callback ctx
  48271. * returen 0 on success, otherwise return negative
  48272. */
  48273. static wc_test_ret_t ecc_onlycb_test(myCryptoDevCtx *ctx)
  48274. {
  48275. wc_test_ret_t ret = 0;
  48276. #if defined(HAVE_ECC)
  48277. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  48278. ecc_key* key = (ecc_key *)XMALLOC(sizeof *key,
  48279. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48280. ecc_key* pub = (ecc_key *)XMALLOC(sizeof *pub,
  48281. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48282. byte* out = (byte*)XMALLOC(sizeof(byte),
  48283. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48284. #ifdef OPENSSL_EXTRA
  48285. byte* check = (byte*)XMALLOC(sizeof(byte)*(256), HEAP_HINT,
  48286. DYNAMIC_TYPE_TMP_BUFFER);
  48287. #endif
  48288. #else
  48289. ecc_key key[1];
  48290. ecc_key pub[1];
  48291. byte out[256];
  48292. #ifdef OPENSSL_EXTRA
  48293. unsigned char check[256];
  48294. #endif
  48295. #endif
  48296. #ifdef OPENSSL_EXTRA
  48297. EVP_PKEY* privKey = NULL;
  48298. EVP_PKEY* pubKey = NULL;
  48299. #ifdef USE_CERT_BUFFERS_256
  48300. ecc_key* pkey;
  48301. const unsigned char* cp;
  48302. #endif
  48303. EVP_MD_CTX mdCtx;
  48304. const char testData[] = "Hi There";
  48305. size_t checkSz = -1;
  48306. const unsigned char* p;
  48307. const unsigned char check_v[256] = {
  48308. 0x30,0x45,0x02,0x20,0x1b,0x5c,0x2a,0xf0,0x18,0x09,
  48309. 0x74,0x65,0xa1,0x04,0x76,0x3a,0xce,0xcc,0xe5,0x34,
  48310. 0x5e,0x89,0xed,0x40,0x1e,0x5a,0xb1,0x53,0xb4,0xff,
  48311. 0xc7,0x18,0xfe,0x0f,0xc7,0xa6,0x02,0x21,0x00,0xe5,
  48312. 0x70,0x21,0xfc,0xf9,0x63,0x36,0xfd,0x16,0x18,0x08,
  48313. 0x9a,0x63,0x61,0x0f,0xe7,0x7c,0xa3,0xc9,0x14,0xa3,
  48314. 0x30,0x87,0xf7,0xf5,0x70,0x19,0xaf,0x56,0x96,0x9b,
  48315. 0xd8,0x64,0xcd,0xd9,0xff,0x7b,0x2a,0x55,0x52,0xca,
  48316. 0x41,0xb2,0xa6,0xa4,0x8a,0x3b,0x02,0x20,0x8c,0xc5,
  48317. 0xf9,0xc1,0x7d,0x2a,0x65,0x6c,0xe6,0x5a,0xe3,0x76,
  48318. 0x9b,0xab,0x0b,0x9f,0xaf,0x62,0x5d,0xb2,0x60,0xd7,
  48319. 0xeb,0xb4,0x1b,0x73,0xdc,0x01,0x7d,0x7b,0xab,0xc1,
  48320. 0x0c,0x74,0x96,0x41,0xe6,0x3f,0xc5,0x86,0xe6,0x7d,
  48321. 0x2b,0x9d,0x54,0x6b,0xcd,0x31,0x35,0x1f,0xdb,0x49,
  48322. 0x1f,0x32,0x34,0xf8,0x57,0x12,0x86,0x5c,0x0e,0x80,
  48323. 0x55,0x8d,0xff,0xd8,0xbd,0xdf,0x32,0x26,0x62,0x42,
  48324. 0x09,0xda,0xf7,0x74,0xf2,0x3f,0xe6,0xf1,0x77,0x82,
  48325. 0xce,0xe4,0xbb,0x61,0xa6,0xc0,0x17,0x0c,0x6c,0x47,
  48326. 0x2a,0x40,0x1c,0x2b,0xe0,0x98,0x3b,0xbf,0xc6,0xf8,
  48327. 0x6d,0xfd,0xd0,0xfa,0xc1,0x02,0xfb,0x5f,0xfb,0xb0,
  48328. 0xcb,0xd9,0xa3,0x59,0x94,0xe9,0x0f,0x74,0xbb,0x3f,
  48329. 0x64,0xa3,0x83,0xc4,0x2b,0xf7,0xd2,0x97,0xbf,0x3b,
  48330. 0xcf,0xbb,0x60,0x81,0x33,0x94,0xfa,0x0d,0x35,0xd2,
  48331. 0x3d,0xb9,0x99,0xe3,0x12,0xf8,0xf4,0xa3,0x74,0xf4,
  48332. 0x94,0x1d,0x7a,0x66,0xf8,0xd1,0x1d,0xcf,0xb0,0x48,
  48333. 0xef,0x8c,0x94,0x6f,0xdd,0x62,
  48334. };
  48335. #endif
  48336. WC_RNG rng;
  48337. EncryptedInfo encInfo;
  48338. int keyFormat = 0;
  48339. word32 keyIdx = 0;
  48340. byte in[] = "Everyone gets Friday off. ecc p";
  48341. word32 inLen = (word32)XSTRLEN((char*)in);
  48342. word32 outLen;
  48343. int verify;
  48344. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  48345. if (key == NULL || pub == NULL) {
  48346. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_onlycb);
  48347. }
  48348. #endif
  48349. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  48350. if (ret != 0)
  48351. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48352. /* wc_CryptoCb_MakeEccKey cb test, , no actual testing */
  48353. ctx->exampleVar = 99;
  48354. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  48355. if (ret != 0)
  48356. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48357. ctx->exampleVar = 1;
  48358. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  48359. if (ret != NO_VALID_DEVID) {
  48360. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48361. } else
  48362. /* reset return code */
  48363. ret = 0;
  48364. #ifdef USE_CERT_BUFFERS_256
  48365. if (ret == 0) {
  48366. /* load ECC private key and perform private transform */
  48367. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &keyIdx,
  48368. key, sizeof_ecc_key_der_256);
  48369. }
  48370. if (ret != 0)
  48371. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48372. /* wc_CryptoCb_EccSign cb test, no actual testing */
  48373. ctx->exampleVar = 99;
  48374. if (ret == 0) {
  48375. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  48376. }
  48377. if (ret != 0)
  48378. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48379. ctx->exampleVar = 1;
  48380. if (ret == 0) {
  48381. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  48382. }
  48383. if (ret != NO_VALID_DEVID) {
  48384. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48385. }
  48386. else
  48387. ret = 0;
  48388. /* wc_CryptoCb_EccVerify cb test, no actual testing */
  48389. ctx->exampleVar = 99;
  48390. if (ret == 0) {
  48391. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  48392. }
  48393. if (ret != 0)
  48394. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48395. ctx->exampleVar = 1;
  48396. if (ret == 0) {
  48397. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  48398. }
  48399. if (ret != NO_VALID_DEVID) {
  48400. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48401. }
  48402. else
  48403. ret = 0;
  48404. /* wc_CryptoCb_Ecdh cb test, no actual testing */
  48405. /* make public key for shared secret */
  48406. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  48407. ctx->exampleVar = 99;
  48408. if (ret == 0) {
  48409. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  48410. }
  48411. if (ret != 0)
  48412. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48413. ctx->exampleVar = 1;
  48414. if (ret == 0) {
  48415. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  48416. }
  48417. if (ret != NO_VALID_DEVID) {
  48418. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48419. }
  48420. else
  48421. ret = 0;
  48422. #ifdef OPENSSL_EXTRA
  48423. (void)pkey;
  48424. cp = ecc_clikey_der_256;
  48425. privKey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  48426. sizeof_ecc_clikey_der_256);
  48427. if (privKey == NULL) {
  48428. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  48429. }
  48430. pkey = (ecc_key*)privKey->ecc->internal;
  48431. pkey->devId = devId;
  48432. p = ecc_clikeypub_der_256;
  48433. pubKey = d2i_PUBKEY(NULL, &p, sizeof_ecc_clikeypub_der_256);
  48434. if (pubKey == NULL) {
  48435. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  48436. }
  48437. pkey = (ecc_key*)pubKey->ecc->internal;
  48438. pkey->devId = devId;
  48439. /* sign */
  48440. EVP_MD_CTX_init(&mdCtx);
  48441. ret = EVP_DigestSignInit(&mdCtx, NULL, EVP_sha256(), NULL, privKey);
  48442. if (ret != WOLFSSL_SUCCESS) {
  48443. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  48444. }
  48445. ret = EVP_DigestSignUpdate(&mdCtx, testData,
  48446. (unsigned int)XSTRLEN(testData));
  48447. if (ret != WOLFSSL_SUCCESS) {
  48448. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  48449. }
  48450. ret = EVP_DigestSignFinal(&mdCtx, NULL, &checkSz);
  48451. if (ret != WOLFSSL_SUCCESS) {
  48452. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  48453. }
  48454. ctx->exampleVar = 99;
  48455. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  48456. /* just called crypt callback as dummy
  48457. * EVP_DigestSignFinal returns 0 internally.
  48458. */
  48459. if (ret != 0)
  48460. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48461. ctx->exampleVar = 1;
  48462. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  48463. /* just called crypt callback as dummy
  48464. * EVP_DigestSignFinal returns 0 internally.
  48465. */
  48466. if (ret != 0)
  48467. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48468. /* restore checkSz for verify */
  48469. checkSz = 71;
  48470. ret = EVP_MD_CTX_cleanup(&mdCtx);
  48471. if (ret != SSL_SUCCESS) {
  48472. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  48473. }
  48474. /* verify */
  48475. EVP_MD_CTX_init(&mdCtx);
  48476. if (ret == SSL_SUCCESS) {
  48477. ret = EVP_DigestVerifyInit(&mdCtx, NULL, EVP_sha256(), NULL, pubKey);
  48478. }
  48479. if (ret != WOLFSSL_SUCCESS) {
  48480. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  48481. }
  48482. if (ret == WOLFSSL_SUCCESS) {
  48483. ret = EVP_DigestVerifyUpdate(&mdCtx, testData,
  48484. (unsigned int)XSTRLEN(testData));
  48485. }
  48486. if (ret != WOLFSSL_SUCCESS) {
  48487. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  48488. }
  48489. ctx->exampleVar = 99;
  48490. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  48491. /* just called crypt callback as dummy
  48492. * EVP_DigestSignFinal returns 0 internally.
  48493. */
  48494. if (ret != 0)
  48495. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48496. ctx->exampleVar = 1;
  48497. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  48498. /* just called crypt callback as dummy
  48499. * EVP_DigestVerifyFinal returns -1 internally rather than NO_VALID_DEVID.
  48500. */
  48501. if (ret != -1) {
  48502. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  48503. }
  48504. ret = EVP_MD_CTX_cleanup(&mdCtx);
  48505. if (ret != SSL_SUCCESS) {
  48506. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  48507. } else
  48508. ret = 0;
  48509. #endif
  48510. #else
  48511. (void)verify;
  48512. (void)outLen;
  48513. (void)inLen;
  48514. (void)out;
  48515. (void)pub;
  48516. #ifdef OPENSSL_EXTRA
  48517. (void)privKey;
  48518. (void)pubKey;
  48519. (void)mdCtx;
  48520. (void)check;
  48521. (void)checkSz;
  48522. (void)p;
  48523. #endif
  48524. #endif
  48525. (void)keyFormat;
  48526. (void)encInfo;
  48527. exit_onlycb:
  48528. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  48529. if (key != NULL) {
  48530. wc_ecc_free(key);
  48531. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48532. }
  48533. if (pub != NULL) {
  48534. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48535. }
  48536. if (out != NULL) {
  48537. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48538. }
  48539. #ifdef OPENSSL_EXTRA
  48540. if (check) {
  48541. FREE(check, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48542. }
  48543. #endif
  48544. #else
  48545. wc_ecc_free(key);
  48546. #ifdef OPENSSL_EXTRA
  48547. if (privKey)
  48548. EVP_PKEY_free(privKey);
  48549. if (pubKey)
  48550. EVP_PKEY_free(pubKey);
  48551. #endif
  48552. #endif
  48553. #endif /* HAVE_ECC */
  48554. return ret;
  48555. }
  48556. #endif
  48557. /* Example crypto dev callback function that calls software version */
  48558. static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
  48559. {
  48560. int ret = NOT_COMPILED_IN; /* return this to bypass HW and use SW */
  48561. myCryptoDevCtx* myCtx = (myCryptoDevCtx*)ctx;
  48562. if (info == NULL)
  48563. return BAD_FUNC_ARG;
  48564. #ifdef DEBUG_WOLFSSL
  48565. WOLFSSL_MSG_EX("CryptoDevCb: Algo Type %d\n", info->algo_type);
  48566. #endif
  48567. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  48568. #if defined(WOLF_CRYPTO_CB) && !defined(HAVE_HASHDRBG) && \
  48569. !defined(WC_NO_RNG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  48570. /* if RNG only supports crypto callback, just use seed */
  48571. ret = wc_GenerateSeed(&info->rng.rng->seed,
  48572. info->rng.out, info->rng.sz);
  48573. #elif !defined(WC_NO_RNG)
  48574. /* set devId to invalid, so software is used */
  48575. info->rng.rng->devId = INVALID_DEVID;
  48576. ret = wc_RNG_GenerateBlock(info->rng.rng,
  48577. info->rng.out, info->rng.sz);
  48578. /* reset devId */
  48579. info->rng.rng->devId = devIdArg;
  48580. #endif
  48581. }
  48582. else if (info->algo_type == WC_ALGO_TYPE_SEED) {
  48583. #ifndef WC_NO_RNG
  48584. ALIGN32 static byte seed[sizeof(word32)] = { 0x00, 0x00, 0x00, 0x01 };
  48585. word32* seedWord32 = (word32*)seed;
  48586. word32 len;
  48587. /* wc_GenerateSeed is a local symbol so we need to fake the entropy. */
  48588. while (info->seed.sz > 0) {
  48589. len = (word32)sizeof(seed);
  48590. if (info->seed.sz < len)
  48591. len = info->seed.sz;
  48592. XMEMCPY(info->seed.seed, seed, sizeof(seed));
  48593. info->seed.seed += len;
  48594. info->seed.sz -= len;
  48595. (*seedWord32)++;
  48596. }
  48597. ret = 0;
  48598. #endif
  48599. }
  48600. else if (info->algo_type == WC_ALGO_TYPE_PK) {
  48601. #ifdef DEBUG_WOLFSSL
  48602. WOLFSSL_MSG_EX("CryptoDevCb: Pk Type %d\n", info->pk.type);
  48603. #endif
  48604. #ifndef NO_RSA
  48605. if (info->pk.type == WC_PK_TYPE_RSA) {
  48606. /* set devId to invalid, so software is used */
  48607. info->pk.rsa.key->devId = INVALID_DEVID;
  48608. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  48609. #ifdef DEBUG_WOLFSSL
  48610. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  48611. #endif
  48612. if (myCtx->exampleVar == 99) {
  48613. info->pk.rsa.key->devId = devIdArg;
  48614. return 0;
  48615. }
  48616. #endif
  48617. switch (info->pk.rsa.type) {
  48618. case RSA_PUBLIC_ENCRYPT:
  48619. case RSA_PUBLIC_DECRYPT:
  48620. /* perform software based RSA public op */
  48621. ret = wc_RsaFunction(
  48622. info->pk.rsa.in, info->pk.rsa.inLen,
  48623. info->pk.rsa.out, info->pk.rsa.outLen,
  48624. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  48625. break;
  48626. case RSA_PRIVATE_ENCRYPT:
  48627. case RSA_PRIVATE_DECRYPT:
  48628. /* perform software based RSA private op */
  48629. ret = wc_RsaFunction(
  48630. info->pk.rsa.in, info->pk.rsa.inLen,
  48631. info->pk.rsa.out, info->pk.rsa.outLen,
  48632. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  48633. break;
  48634. }
  48635. /* reset devId */
  48636. info->pk.rsa.key->devId = devIdArg;
  48637. }
  48638. #ifdef WOLFSSL_KEY_GEN
  48639. else if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
  48640. info->pk.rsakg.key->devId = INVALID_DEVID;
  48641. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  48642. #ifdef DEBUG_WOLFSSL
  48643. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  48644. #endif
  48645. if (myCtx->exampleVar == 99) {
  48646. info->pk.rsakg.key->devId = devIdArg;
  48647. return 0;
  48648. }
  48649. #endif
  48650. #ifdef HAVE_FIPS
  48651. for (;;) {
  48652. #endif
  48653. ret = wc_MakeRsaKey(info->pk.rsakg.key, info->pk.rsakg.size,
  48654. info->pk.rsakg.e, info->pk.rsakg.rng);
  48655. #ifdef HAVE_FIPS
  48656. if (ret == PRIME_GEN_E)
  48657. continue;
  48658. break;
  48659. }
  48660. #endif
  48661. /* reset devId */
  48662. info->pk.rsakg.key->devId = devIdArg;
  48663. }
  48664. #endif
  48665. #endif /* !NO_RSA */
  48666. #ifdef HAVE_ECC
  48667. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  48668. /* set devId to invalid, so software is used */
  48669. info->pk.eckg.key->devId = INVALID_DEVID;
  48670. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  48671. #ifdef DEBUG_WOLFSSL
  48672. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  48673. #endif
  48674. if (myCtx->exampleVar == 99) {
  48675. info->pk.eckg.key->devId = devIdArg;
  48676. return 0;
  48677. }
  48678. #endif
  48679. ret = wc_ecc_make_key_ex(info->pk.eckg.rng, info->pk.eckg.size,
  48680. info->pk.eckg.key, info->pk.eckg.curveId);
  48681. /* reset devId */
  48682. info->pk.eckg.key->devId = devIdArg;
  48683. }
  48684. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  48685. /* set devId to invalid, so software is used */
  48686. info->pk.eccsign.key->devId = INVALID_DEVID;
  48687. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  48688. #ifdef DEBUG_WOLFSSL
  48689. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  48690. #endif
  48691. if (myCtx->exampleVar == 99) {
  48692. info->pk.eccsign.key->devId = devIdArg;
  48693. return 0;
  48694. }
  48695. #endif
  48696. ret = wc_ecc_sign_hash(
  48697. info->pk.eccsign.in, info->pk.eccsign.inlen,
  48698. info->pk.eccsign.out, info->pk.eccsign.outlen,
  48699. info->pk.eccsign.rng, info->pk.eccsign.key);
  48700. /* reset devId */
  48701. info->pk.eccsign.key->devId = devIdArg;
  48702. }
  48703. else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
  48704. /* set devId to invalid, so software is used */
  48705. info->pk.eccverify.key->devId = INVALID_DEVID;
  48706. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  48707. #ifdef DEBUG_WOLFSSL
  48708. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  48709. #endif
  48710. if (myCtx->exampleVar == 99) {
  48711. info->pk.eccverify.key->devId = devIdArg;
  48712. return 0;
  48713. }
  48714. #endif
  48715. ret = wc_ecc_verify_hash(
  48716. info->pk.eccverify.sig, info->pk.eccverify.siglen,
  48717. info->pk.eccverify.hash, info->pk.eccverify.hashlen,
  48718. info->pk.eccverify.res, info->pk.eccverify.key);
  48719. /* reset devId */
  48720. info->pk.eccverify.key->devId = devIdArg;
  48721. }
  48722. else if (info->pk.type == WC_PK_TYPE_ECDH) {
  48723. /* set devId to invalid, so software is used */
  48724. info->pk.ecdh.private_key->devId = INVALID_DEVID;
  48725. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  48726. #ifdef DEBUG_WOLFSSL
  48727. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  48728. #endif
  48729. if (myCtx->exampleVar == 99) {
  48730. info->pk.ecdh.private_key->devId = devIdArg;
  48731. return 0;
  48732. }
  48733. #endif
  48734. ret = wc_ecc_shared_secret(
  48735. info->pk.ecdh.private_key, info->pk.ecdh.public_key,
  48736. info->pk.ecdh.out, info->pk.ecdh.outlen);
  48737. /* reset devId */
  48738. info->pk.ecdh.private_key->devId = devIdArg;
  48739. }
  48740. #endif /* HAVE_ECC */
  48741. #ifdef HAVE_CURVE25519
  48742. if (info->pk.type == WC_PK_TYPE_CURVE25519_KEYGEN) {
  48743. /* set devId to invalid, so software is used */
  48744. info->pk.curve25519kg.key->devId = INVALID_DEVID;
  48745. ret = wc_curve25519_make_key(info->pk.curve25519kg.rng,
  48746. info->pk.curve25519kg.size, info->pk.curve25519kg.key);
  48747. /* reset devId */
  48748. info->pk.curve25519kg.key->devId = devIdArg;
  48749. }
  48750. else if (info->pk.type == WC_PK_TYPE_CURVE25519) {
  48751. /* set devId to invalid, so software is used */
  48752. info->pk.curve25519.private_key->devId = INVALID_DEVID;
  48753. ret = wc_curve25519_shared_secret_ex(
  48754. info->pk.curve25519.private_key, info->pk.curve25519.public_key,
  48755. info->pk.curve25519.out, info->pk.curve25519.outlen,
  48756. info->pk.curve25519.endian);
  48757. /* reset devId */
  48758. info->pk.curve25519.private_key->devId = devIdArg;
  48759. }
  48760. #endif /* HAVE_CURVE25519 */
  48761. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_MAKE_KEY)
  48762. if (info->pk.type == WC_PK_TYPE_ED25519_KEYGEN) {
  48763. /* set devId to invalid, so software is used */
  48764. info->pk.ed25519kg.key->devId = INVALID_DEVID;
  48765. ret = wc_ed25519_make_key(info->pk.ed25519kg.rng,
  48766. info->pk.ed25519kg.size, info->pk.ed25519kg.key);
  48767. /* reset devId */
  48768. info->pk.ed25519kg.key->devId = devIdArg;
  48769. }
  48770. #ifdef HAVE_ED25519_SIGN
  48771. else if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  48772. /* set devId to invalid, so software is used */
  48773. info->pk.ed25519sign.key->devId = INVALID_DEVID;
  48774. ret = wc_ed25519_sign_msg_ex(
  48775. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  48776. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  48777. info->pk.ed25519sign.key, info->pk.ed25519sign.type,
  48778. info->pk.ed25519sign.context, info->pk.ed25519sign.contextLen);
  48779. /* reset devId */
  48780. info->pk.ed25519sign.key->devId = devIdArg;
  48781. }
  48782. #endif
  48783. #ifdef HAVE_ED25519_VERIFY
  48784. else if (info->pk.type == WC_PK_TYPE_ED25519_VERIFY) {
  48785. /* set devId to invalid, so software is used */
  48786. info->pk.ed25519verify.key->devId = INVALID_DEVID;
  48787. ret = wc_ed25519_verify_msg_ex(
  48788. info->pk.ed25519verify.sig, info->pk.ed25519verify.sigLen,
  48789. info->pk.ed25519verify.msg, info->pk.ed25519verify.msgLen,
  48790. info->pk.ed25519verify.res, info->pk.ed25519verify.key,
  48791. info->pk.ed25519verify.type, info->pk.ed25519verify.context,
  48792. info->pk.ed25519verify.contextLen);
  48793. /* reset devId */
  48794. info->pk.ed25519verify.key->devId = devIdArg;
  48795. }
  48796. #endif
  48797. #endif /* HAVE_ED25519 */
  48798. }
  48799. else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  48800. #if !defined(NO_AES) || !defined(NO_DES3)
  48801. #ifdef HAVE_AESGCM
  48802. if (info->cipher.type == WC_CIPHER_AES_GCM) {
  48803. if (info->cipher.enc) {
  48804. /* set devId to invalid, so software is used */
  48805. info->cipher.aesgcm_enc.aes->devId = INVALID_DEVID;
  48806. ret = wc_AesGcmEncrypt(
  48807. info->cipher.aesgcm_enc.aes,
  48808. info->cipher.aesgcm_enc.out,
  48809. info->cipher.aesgcm_enc.in,
  48810. info->cipher.aesgcm_enc.sz,
  48811. info->cipher.aesgcm_enc.iv,
  48812. info->cipher.aesgcm_enc.ivSz,
  48813. info->cipher.aesgcm_enc.authTag,
  48814. info->cipher.aesgcm_enc.authTagSz,
  48815. info->cipher.aesgcm_enc.authIn,
  48816. info->cipher.aesgcm_enc.authInSz);
  48817. /* reset devId */
  48818. info->cipher.aesgcm_enc.aes->devId = devIdArg;
  48819. }
  48820. else {
  48821. /* set devId to invalid, so software is used */
  48822. info->cipher.aesgcm_dec.aes->devId = INVALID_DEVID;
  48823. ret = wc_AesGcmDecrypt(
  48824. info->cipher.aesgcm_dec.aes,
  48825. info->cipher.aesgcm_dec.out,
  48826. info->cipher.aesgcm_dec.in,
  48827. info->cipher.aesgcm_dec.sz,
  48828. info->cipher.aesgcm_dec.iv,
  48829. info->cipher.aesgcm_dec.ivSz,
  48830. info->cipher.aesgcm_dec.authTag,
  48831. info->cipher.aesgcm_dec.authTagSz,
  48832. info->cipher.aesgcm_dec.authIn,
  48833. info->cipher.aesgcm_dec.authInSz);
  48834. /* reset devId */
  48835. info->cipher.aesgcm_dec.aes->devId = devIdArg;
  48836. }
  48837. }
  48838. #endif /* HAVE_AESGCM */
  48839. #ifdef HAVE_AES_CBC
  48840. if (info->cipher.type == WC_CIPHER_AES_CBC) {
  48841. if (info->cipher.enc) {
  48842. /* set devId to invalid, so software is used */
  48843. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  48844. ret = wc_AesCbcEncrypt(
  48845. info->cipher.aescbc.aes,
  48846. info->cipher.aescbc.out,
  48847. info->cipher.aescbc.in,
  48848. info->cipher.aescbc.sz);
  48849. /* reset devId */
  48850. info->cipher.aescbc.aes->devId = devIdArg;
  48851. }
  48852. else {
  48853. /* set devId to invalid, so software is used */
  48854. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  48855. ret = wc_AesCbcDecrypt(
  48856. info->cipher.aescbc.aes,
  48857. info->cipher.aescbc.out,
  48858. info->cipher.aescbc.in,
  48859. info->cipher.aescbc.sz);
  48860. /* reset devId */
  48861. info->cipher.aescbc.aes->devId = devIdArg;
  48862. }
  48863. }
  48864. #endif /* HAVE_AES_CBC */
  48865. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  48866. if (info->cipher.type == WC_CIPHER_AES_ECB) {
  48867. if (info->cipher.enc) {
  48868. /* set devId to invalid, so software is used */
  48869. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  48870. ret = wc_AesEcbEncrypt(
  48871. info->cipher.aesecb.aes,
  48872. info->cipher.aesecb.out,
  48873. info->cipher.aesecb.in,
  48874. info->cipher.aesecb.sz);
  48875. /* reset devId */
  48876. info->cipher.aesecb.aes->devId = devIdArg;
  48877. }
  48878. else {
  48879. /* set devId to invalid, so software is used */
  48880. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  48881. ret = wc_AesEcbDecrypt(
  48882. info->cipher.aesecb.aes,
  48883. info->cipher.aesecb.out,
  48884. info->cipher.aesecb.in,
  48885. info->cipher.aesecb.sz);
  48886. /* reset devId */
  48887. info->cipher.aesecb.aes->devId = devIdArg;
  48888. }
  48889. }
  48890. #endif /* HAVE_AES_ECB */
  48891. #if defined(WOLFSSL_AES_COUNTER) && !defined(HAVE_FIPS) && \
  48892. !defined(HAVE_SELFTEST)
  48893. if (info->cipher.type == WC_CIPHER_AES_CTR) {
  48894. /* set devId to invalid, so software is used */
  48895. info->cipher.aesctr.aes->devId = INVALID_DEVID;
  48896. ret = wc_AesCtrEncrypt(
  48897. info->cipher.aesctr.aes,
  48898. info->cipher.aesctr.out,
  48899. info->cipher.aesctr.in,
  48900. info->cipher.aesctr.sz);
  48901. /* reset devId */
  48902. info->cipher.aesctr.aes->devId = devIdArg;
  48903. }
  48904. #endif /* WOLFSSL_AES_COUNTER */
  48905. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  48906. if (info->cipher.type == WC_CIPHER_AES_CCM) {
  48907. if (info->cipher.enc) {
  48908. /* set devId to invalid, so software is used */
  48909. info->cipher.aesccm_enc.aes->devId = INVALID_DEVID;
  48910. ret = wc_AesCcmEncrypt(
  48911. info->cipher.aesccm_enc.aes,
  48912. info->cipher.aesccm_enc.out,
  48913. info->cipher.aesccm_enc.in,
  48914. info->cipher.aesccm_enc.sz,
  48915. info->cipher.aesccm_enc.nonce,
  48916. info->cipher.aesccm_enc.nonceSz,
  48917. info->cipher.aesccm_enc.authTag,
  48918. info->cipher.aesccm_enc.authTagSz,
  48919. info->cipher.aesccm_enc.authIn,
  48920. info->cipher.aesccm_enc.authInSz);
  48921. /* reset devId */
  48922. info->cipher.aesccm_enc.aes->devId = devIdArg;
  48923. }
  48924. else {
  48925. /* set devId to invalid, so software is used */
  48926. info->cipher.aesccm_dec.aes->devId = INVALID_DEVID;
  48927. ret = wc_AesCcmDecrypt(
  48928. info->cipher.aesccm_dec.aes,
  48929. info->cipher.aesccm_dec.out,
  48930. info->cipher.aesccm_dec.in,
  48931. info->cipher.aesccm_dec.sz,
  48932. info->cipher.aesccm_dec.nonce,
  48933. info->cipher.aesccm_dec.nonceSz,
  48934. info->cipher.aesccm_dec.authTag,
  48935. info->cipher.aesccm_dec.authTagSz,
  48936. info->cipher.aesccm_dec.authIn,
  48937. info->cipher.aesccm_dec.authInSz);
  48938. /* reset devId */
  48939. info->cipher.aesccm_dec.aes->devId = devIdArg;
  48940. }
  48941. }
  48942. #endif
  48943. #ifndef NO_DES3
  48944. if (info->cipher.type == WC_CIPHER_DES3) {
  48945. if (info->cipher.enc) {
  48946. /* set devId to invalid, so software is used */
  48947. info->cipher.des3.des->devId = INVALID_DEVID;
  48948. ret = wc_Des3_CbcEncrypt(
  48949. info->cipher.des3.des,
  48950. info->cipher.des3.out,
  48951. info->cipher.des3.in,
  48952. info->cipher.des3.sz);
  48953. /* reset devId */
  48954. info->cipher.des3.des->devId = devIdArg;
  48955. }
  48956. else {
  48957. /* set devId to invalid, so software is used */
  48958. info->cipher.des3.des->devId = INVALID_DEVID;
  48959. ret = wc_Des3_CbcDecrypt(
  48960. info->cipher.des3.des,
  48961. info->cipher.des3.out,
  48962. info->cipher.des3.in,
  48963. info->cipher.des3.sz);
  48964. /* reset devId */
  48965. info->cipher.des3.des->devId = devIdArg;
  48966. }
  48967. }
  48968. #endif /* !NO_DES3 */
  48969. #endif /* !NO_AES || !NO_DES3 */
  48970. }
  48971. #if !defined(NO_SHA) || !defined(NO_SHA256) || \
  48972. defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
  48973. else if (info->algo_type == WC_ALGO_TYPE_HASH) {
  48974. #if !defined(NO_SHA)
  48975. if (info->hash.type == WC_HASH_TYPE_SHA) {
  48976. if (info->hash.sha1 == NULL)
  48977. return NOT_COMPILED_IN;
  48978. /* set devId to invalid, so software is used */
  48979. info->hash.sha1->devId = INVALID_DEVID;
  48980. if (info->hash.in != NULL) {
  48981. ret = wc_ShaUpdate(
  48982. info->hash.sha1,
  48983. info->hash.in,
  48984. info->hash.inSz);
  48985. }
  48986. if (info->hash.digest != NULL) {
  48987. ret = wc_ShaFinal(
  48988. info->hash.sha1,
  48989. info->hash.digest);
  48990. }
  48991. /* reset devId */
  48992. info->hash.sha1->devId = devIdArg;
  48993. }
  48994. else
  48995. #endif
  48996. #if !defined(NO_SHA256)
  48997. if (info->hash.type == WC_HASH_TYPE_SHA256) {
  48998. if (info->hash.sha256 == NULL)
  48999. return NOT_COMPILED_IN;
  49000. /* set devId to invalid, so software is used */
  49001. info->hash.sha256->devId = INVALID_DEVID;
  49002. if (info->hash.in != NULL) {
  49003. ret = wc_Sha256Update(
  49004. info->hash.sha256,
  49005. info->hash.in,
  49006. info->hash.inSz);
  49007. }
  49008. if (info->hash.digest != NULL) {
  49009. ret = wc_Sha256Final(
  49010. info->hash.sha256,
  49011. info->hash.digest);
  49012. }
  49013. /* reset devId */
  49014. info->hash.sha256->devId = devIdArg;
  49015. }
  49016. else
  49017. #endif
  49018. #ifdef WOLFSSL_SHA384
  49019. if (info->hash.type == WC_HASH_TYPE_SHA384) {
  49020. if (info->hash.sha384 == NULL)
  49021. return NOT_COMPILED_IN;
  49022. #ifndef NO_SHA2_CRYPTO_CB
  49023. /* set devId to invalid, so software is used */
  49024. info->hash.sha384->devId = INVALID_DEVID;
  49025. #endif
  49026. if (info->hash.in != NULL) {
  49027. ret = wc_Sha384Update(
  49028. info->hash.sha384,
  49029. info->hash.in,
  49030. info->hash.inSz);
  49031. }
  49032. if (info->hash.digest != NULL) {
  49033. ret = wc_Sha384Final(
  49034. info->hash.sha384,
  49035. info->hash.digest);
  49036. }
  49037. #ifndef NO_SHA2_CRYPTO_CB
  49038. /* reset devId */
  49039. info->hash.sha384->devId = devIdArg;
  49040. #endif
  49041. }
  49042. else
  49043. #endif
  49044. #ifdef WOLFSSL_SHA512
  49045. if (info->hash.type == WC_HASH_TYPE_SHA512) {
  49046. if (info->hash.sha512 == NULL)
  49047. return NOT_COMPILED_IN;
  49048. #ifndef NO_SHA2_CRYPTO_CB
  49049. /* set devId to invalid, so software is used */
  49050. info->hash.sha512->devId = INVALID_DEVID;
  49051. #endif
  49052. if (info->hash.in != NULL) {
  49053. ret = wc_Sha512Update(
  49054. info->hash.sha512,
  49055. info->hash.in,
  49056. info->hash.inSz);
  49057. }
  49058. if (info->hash.digest != NULL) {
  49059. ret = wc_Sha512Final(
  49060. info->hash.sha512,
  49061. info->hash.digest);
  49062. }
  49063. #ifndef NO_SHA2_CRYPTO_CB
  49064. /* reset devId */
  49065. info->hash.sha512->devId = devIdArg;
  49066. #endif
  49067. }
  49068. else
  49069. #endif
  49070. #if defined(WOLFSSL_SHA3) && (!defined(HAVE_FIPS) || FIPS_VERSION_GE(6, 0))
  49071. if (info->hash.type == WC_HASH_TYPE_SHA3_224) {
  49072. if (info->hash.sha3 == NULL)
  49073. return NOT_COMPILED_IN;
  49074. /* set devId to invalid, so software is used */
  49075. info->hash.sha3->devId = INVALID_DEVID;
  49076. if (info->hash.in != NULL) {
  49077. ret = wc_Sha3_224_Update(
  49078. info->hash.sha3,
  49079. info->hash.in,
  49080. info->hash.inSz);
  49081. }
  49082. if (info->hash.digest != NULL) {
  49083. ret = wc_Sha3_224_Final(
  49084. info->hash.sha3,
  49085. info->hash.digest);
  49086. }
  49087. /* reset devId */
  49088. info->hash.sha3->devId = devIdArg;
  49089. }
  49090. else if (info->hash.type == WC_HASH_TYPE_SHA3_256) {
  49091. if (info->hash.sha3 == NULL)
  49092. return NOT_COMPILED_IN;
  49093. /* set devId to invalid, so software is used */
  49094. info->hash.sha3->devId = INVALID_DEVID;
  49095. if (info->hash.in != NULL) {
  49096. ret = wc_Sha3_256_Update(
  49097. info->hash.sha3,
  49098. info->hash.in,
  49099. info->hash.inSz);
  49100. }
  49101. if (info->hash.digest != NULL) {
  49102. ret = wc_Sha3_256_Final(
  49103. info->hash.sha3,
  49104. info->hash.digest);
  49105. }
  49106. /* reset devId */
  49107. info->hash.sha3->devId = devIdArg;
  49108. }
  49109. else if (info->hash.type == WC_HASH_TYPE_SHA3_384) {
  49110. if (info->hash.sha3 == NULL)
  49111. return NOT_COMPILED_IN;
  49112. /* set devId to invalid, so software is used */
  49113. info->hash.sha3->devId = INVALID_DEVID;
  49114. if (info->hash.in != NULL) {
  49115. ret = wc_Sha3_384_Update(
  49116. info->hash.sha3,
  49117. info->hash.in,
  49118. info->hash.inSz);
  49119. }
  49120. if (info->hash.digest != NULL) {
  49121. ret = wc_Sha3_384_Final(
  49122. info->hash.sha3,
  49123. info->hash.digest);
  49124. }
  49125. /* reset devId */
  49126. info->hash.sha3->devId = devIdArg;
  49127. }
  49128. else if (info->hash.type == WC_HASH_TYPE_SHA3_512) {
  49129. if (info->hash.sha3 == NULL)
  49130. return NOT_COMPILED_IN;
  49131. /* set devId to invalid, so software is used */
  49132. info->hash.sha3->devId = INVALID_DEVID;
  49133. if (info->hash.in != NULL) {
  49134. ret = wc_Sha3_512_Update(
  49135. info->hash.sha3,
  49136. info->hash.in,
  49137. info->hash.inSz);
  49138. }
  49139. if (info->hash.digest != NULL) {
  49140. ret = wc_Sha3_512_Final(
  49141. info->hash.sha3,
  49142. info->hash.digest);
  49143. }
  49144. /* reset devId */
  49145. info->hash.sha3->devId = devIdArg;
  49146. }
  49147. else
  49148. #endif
  49149. {
  49150. }
  49151. }
  49152. #endif /* !NO_SHA || !NO_SHA256 */
  49153. #ifndef NO_HMAC
  49154. else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
  49155. if (info->hmac.hmac == NULL)
  49156. return NOT_COMPILED_IN;
  49157. /* set devId to invalid, so software is used */
  49158. info->hmac.hmac->devId = INVALID_DEVID;
  49159. if (info->hmac.in != NULL) {
  49160. ret = wc_HmacUpdate(
  49161. info->hmac.hmac,
  49162. info->hmac.in,
  49163. info->hmac.inSz);
  49164. }
  49165. else if (info->hmac.digest != NULL) {
  49166. ret = wc_HmacFinal(
  49167. info->hmac.hmac,
  49168. info->hmac.digest);
  49169. }
  49170. /* reset devId */
  49171. info->hmac.hmac->devId = devIdArg;
  49172. }
  49173. #endif
  49174. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  49175. else if (info->algo_type == WC_ALGO_TYPE_CMAC) {
  49176. if (info->cmac.cmac == NULL) {
  49177. return NOT_COMPILED_IN;
  49178. }
  49179. /* set devId to invalid so software is used */
  49180. info->cmac.cmac->devId = INVALID_DEVID;
  49181. /* Handle one-shot cases */
  49182. if (info->cmac.key != NULL && info->cmac.in != NULL
  49183. && info->cmac.out != NULL) {
  49184. ret = wc_AesCmacGenerate(info->cmac.out,
  49185. info->cmac.outSz,
  49186. info->cmac.in,
  49187. info->cmac.inSz,
  49188. info->cmac.key,
  49189. info->cmac.keySz);
  49190. /* Sequentially handle incremental cases */
  49191. } else {
  49192. if (info->cmac.key != NULL) {
  49193. ret = wc_InitCmac(info->cmac.cmac,
  49194. info->cmac.key,
  49195. info->cmac.keySz,
  49196. info->cmac.type,
  49197. NULL);
  49198. }
  49199. if ((ret == 0) && (info->cmac.in != NULL)) {
  49200. ret = wc_CmacUpdate(info->cmac.cmac,
  49201. info->cmac.in,
  49202. info->cmac.inSz);
  49203. }
  49204. if ((ret ==0) && (info->cmac.out != NULL)) {
  49205. ret = wc_CmacFinal(info->cmac.cmac,
  49206. info->cmac.out,
  49207. info->cmac.outSz);
  49208. }
  49209. }
  49210. /* reset devId */
  49211. info->cmac.cmac->devId = devIdArg;
  49212. }
  49213. #endif /* WOLFSSL_CMAC && !(NO_AES) && WOLFSSL_AES_DIRECT */
  49214. (void)devIdArg;
  49215. (void)myCtx;
  49216. return ret;
  49217. }
  49218. #ifdef WOLF_CRYPTO_CB_FIND
  49219. static int myCryptoCbFind(int currentId, int algoType)
  49220. {
  49221. /* can have algo specific overrides here
  49222. switch (algoType) {
  49223. i.e.
  49224. WC_ALGO_TYPE_CMAC
  49225. WC_ALGO_TYPE_SEED
  49226. WC_ALGO_TYPE_HMAC
  49227. WC_ALGO_TYPE_HASH
  49228. WC_ALGO_TYPE_CIPHER
  49229. WC_ALGO_TYPE_PK
  49230. }
  49231. */
  49232. (void)algoType;
  49233. if (currentId == INVALID_DEVID) {
  49234. /* can override invalid devid found with 1 */
  49235. }
  49236. return currentId;
  49237. }
  49238. #endif /* WOLF_CRYPTO_CB_FIND */
  49239. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cryptocb_test(void)
  49240. {
  49241. wc_test_ret_t ret = 0;
  49242. int origDevId = devId;
  49243. myCryptoDevCtx myCtx;
  49244. WOLFSSL_ENTER("cryptocb_test");
  49245. /* example data for callback */
  49246. myCtx.exampleVar = 1;
  49247. /* set devId to something other than INVALID_DEVID */
  49248. devId = 1;
  49249. ret = wc_CryptoCb_RegisterDevice(devId, myCryptoDevCb, &myCtx);
  49250. #ifdef WOLF_CRYPTO_CB_FIND
  49251. wc_CryptoCb_SetDeviceFindCb(myCryptoCbFind);
  49252. #endif /* WOLF_CRYPTO_CB_FIND */
  49253. #ifndef WC_NO_RNG
  49254. if (ret == 0)
  49255. ret = random_test();
  49256. #endif /* WC_NO_RNG */
  49257. #if !defined(NO_RSA)
  49258. PRIVATE_KEY_UNLOCK();
  49259. if (ret == 0)
  49260. ret = rsa_test();
  49261. PRIVATE_KEY_LOCK();
  49262. #endif
  49263. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  49264. PRIVATE_KEY_UNLOCK();
  49265. if (ret == 0)
  49266. ret = rsa_onlycb_test(&myCtx);
  49267. PRIVATE_KEY_LOCK();
  49268. #endif
  49269. #if defined(HAVE_ECC)
  49270. PRIVATE_KEY_UNLOCK();
  49271. if (ret == 0)
  49272. ret = ecc_test();
  49273. PRIVATE_KEY_LOCK();
  49274. #endif
  49275. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  49276. PRIVATE_KEY_UNLOCK();
  49277. if (ret == 0)
  49278. ret = ecc_onlycb_test(&myCtx);
  49279. PRIVATE_KEY_LOCK();
  49280. #endif
  49281. #ifdef HAVE_ED25519
  49282. PRIVATE_KEY_UNLOCK();
  49283. if (ret == 0)
  49284. ret = ed25519_test();
  49285. PRIVATE_KEY_LOCK();
  49286. #endif
  49287. #ifdef HAVE_CURVE25519
  49288. if (ret == 0)
  49289. ret = curve25519_test();
  49290. #endif
  49291. #ifndef NO_AES
  49292. #ifdef HAVE_AESGCM
  49293. if (ret == 0)
  49294. ret = aesgcm_test();
  49295. #endif
  49296. #ifdef HAVE_AES_CBC
  49297. if (ret == 0)
  49298. ret = aes_test();
  49299. #endif
  49300. #ifdef WOLFSSL_AES_XTS
  49301. if (ret == 0)
  49302. ret = aes_xts_test();
  49303. #endif
  49304. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  49305. if (ret == 0)
  49306. ret = aesccm_test();
  49307. #endif
  49308. #endif /* !NO_AES */
  49309. #ifndef NO_DES3
  49310. if (ret == 0)
  49311. ret = des3_test();
  49312. #endif /* !NO_DES3 */
  49313. #ifndef NO_SHA
  49314. if (ret == 0)
  49315. ret = sha_test();
  49316. #endif
  49317. #ifndef NO_SHA256
  49318. if (ret == 0)
  49319. ret = sha256_test();
  49320. #endif
  49321. #ifdef WOLFSSL_SHA384
  49322. if (ret == 0)
  49323. ret = sha384_test();
  49324. #endif
  49325. #ifdef WOLFSSL_SHA512
  49326. if (ret == 0)
  49327. ret = sha512_test();
  49328. #ifdef WOLFSSL_SHA3
  49329. if (ret == 0)
  49330. ret = sha3_test();
  49331. #endif
  49332. #endif
  49333. #ifndef NO_HMAC
  49334. #ifndef NO_SHA
  49335. if (ret == 0)
  49336. ret = hmac_sha_test();
  49337. #endif
  49338. #ifndef NO_SHA256
  49339. if (ret == 0)
  49340. ret = hmac_sha256_test();
  49341. #endif
  49342. #ifdef WOLFSSL_SHA3
  49343. if (ret == 0)
  49344. ret = hmac_sha3_test();
  49345. #endif
  49346. #endif
  49347. #ifndef NO_PWDBASED
  49348. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  49349. PRIVATE_KEY_UNLOCK();
  49350. if (ret == 0)
  49351. ret = pbkdf2_test();
  49352. PRIVATE_KEY_LOCK();
  49353. #endif
  49354. #endif
  49355. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  49356. if (ret == 0)
  49357. ret = cmac_test();
  49358. #endif
  49359. /* restore devId */
  49360. devId = origDevId;
  49361. return ret;
  49362. }
  49363. #endif /* WOLF_CRYPTO_CB */
  49364. #ifdef WOLFSSL_CERT_PIV
  49365. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certpiv_test(void)
  49366. {
  49367. wc_test_ret_t ret;
  49368. wc_CertPIV piv;
  49369. /* Template for Identiv PIV cert, nonce and signature */
  49370. WOLFSSL_SMALL_STACK_STATIC const byte pivCertIdentiv[] = {
  49371. 0x0A, 0x0B,
  49372. 0x53, 0x09, /* NIST PIV Cert */
  49373. 0x70, 0x02, /* Certificate */
  49374. 0x30, 0x00,
  49375. 0x71, 0x01, 0x05, /* Cert Info */
  49376. 0xFE, 0x00, /* Error Detection */
  49377. 0x0B, 0x01, 0x00, /* Nonce */
  49378. 0x0C, 0x01, 0x00, /* Signed Nonce */
  49379. };
  49380. /* PIV certificate data including certificate, info and error detection. */
  49381. WOLFSSL_SMALL_STACK_STATIC const byte pivCert[] = {
  49382. 0x53, 0x09, /* NIST PIV Cert */
  49383. 0x70, 0x02, /* Certificate */
  49384. 0x30, 0x00,
  49385. 0x71, 0x01, 0x04, /* Cert Info */
  49386. 0xFE, 0x00, /* Error Detection */
  49387. };
  49388. WOLFSSL_ENTER("certpiv_test");
  49389. XMEMSET(&piv, 0, sizeof(piv));
  49390. /* Test with Identiv 0x0A, 0x0B and 0x0C markers */
  49391. ret = wc_ParseCertPIV(&piv, pivCertIdentiv, sizeof(pivCertIdentiv));
  49392. if (ret != 0) {
  49393. return WC_TEST_RET_ENC_EC(ret);
  49394. }
  49395. if (!piv.isIdentiv) {
  49396. return WC_TEST_RET_ENC_NC;
  49397. }
  49398. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  49399. return WC_TEST_RET_ENC_NC;
  49400. }
  49401. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  49402. return WC_TEST_RET_ENC_NC;
  49403. }
  49404. if ((piv.compression != ASN_PIV_CERT_INFO_GZIP)) {
  49405. return WC_TEST_RET_ENC_NC;
  49406. }
  49407. if (!piv.isX509) {
  49408. return WC_TEST_RET_ENC_NC;
  49409. }
  49410. if ((piv.nonce == NULL) || (piv.nonceSz != 1)) {
  49411. return WC_TEST_RET_ENC_NC;
  49412. }
  49413. if ((piv.signedNonce == NULL) || (piv.signedNonceSz != 1)) {
  49414. return WC_TEST_RET_ENC_NC;
  49415. }
  49416. XMEMSET(&piv, 0, sizeof(piv));
  49417. /* Test with NIST PIV format */
  49418. ret = wc_ParseCertPIV(&piv, pivCert, sizeof(pivCert));
  49419. if (ret != 0) {
  49420. return WC_TEST_RET_ENC_EC(ret);
  49421. }
  49422. if (piv.isIdentiv) {
  49423. return WC_TEST_RET_ENC_NC;
  49424. }
  49425. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  49426. return WC_TEST_RET_ENC_NC;
  49427. }
  49428. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  49429. return WC_TEST_RET_ENC_NC;
  49430. }
  49431. if ((piv.compression != 0)) {
  49432. return WC_TEST_RET_ENC_NC;
  49433. }
  49434. if (!piv.isX509) {
  49435. return WC_TEST_RET_ENC_NC;
  49436. }
  49437. return ret;
  49438. }
  49439. #endif /* WOLFSSL_CERT_PIV */
  49440. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  49441. static time_t time_cb(time_t* t)
  49442. {
  49443. if (t != NULL) {
  49444. *t = 99;
  49445. }
  49446. return 99;
  49447. }
  49448. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t time_test(void)
  49449. {
  49450. time_t t;
  49451. wc_test_ret_t ret;
  49452. WOLFSSL_ENTER("time_test");
  49453. ret = wc_SetTimeCb(time_cb);
  49454. if (ret != 0)
  49455. return WC_TEST_RET_ENC_EC(ret);
  49456. t = wc_Time(NULL);
  49457. if (t != 99)
  49458. return WC_TEST_RET_ENC_NC;
  49459. ret = wc_GetTime(&t, sizeof(time_t));
  49460. if (ret != 0)
  49461. return WC_TEST_RET_ENC_EC(ret);
  49462. if (t != 99)
  49463. return WC_TEST_RET_ENC_NC;
  49464. ret = wc_SetTimeCb(NULL);
  49465. if (ret != 0)
  49466. return WC_TEST_RET_ENC_EC(ret);
  49467. return 0;
  49468. }
  49469. #endif
  49470. #ifdef WOLFSSL_AES_SIV
  49471. typedef struct {
  49472. const byte key[33];
  49473. word32 keySz;
  49474. const byte nonce[49];
  49475. word32 nonceSz;
  49476. const byte assoc[81];
  49477. word32 assocSz;
  49478. const byte plaintext[83];
  49479. word32 plaintextSz;
  49480. const byte siv[AES_BLOCK_SIZE+1];
  49481. const byte ciphertext[82];
  49482. word32 ciphertextSz;
  49483. } AesSivTestVector;
  49484. #define AES_SIV_TEST_VECTORS 7
  49485. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_siv_test(void)
  49486. {
  49487. /* These test vectors come from chrony 4.1's SIV unit tests. */
  49488. WOLFSSL_SMALL_STACK_STATIC const AesSivTestVector testVectors[AES_SIV_TEST_VECTORS] = {
  49489. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  49490. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  49491. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  49492. "", 0,
  49493. "", 0,
  49494. "\x22\x3e\xb5\x94\xe0\xe0\x25\x4b\x00\x25\x8e\x21\x9a\x1c\xa4\x21",
  49495. "", 0
  49496. },
  49497. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  49498. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  49499. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  49500. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  49501. "", 0,
  49502. "\xd7\x20\x19\x89\xc6\xdb\xc6\xd6\x61\xfc\x62\xbc\x86\x5e\xee\xef",
  49503. "", 0
  49504. },
  49505. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  49506. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  49507. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  49508. "", 0,
  49509. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  49510. "\xb6\xc1\x60\xe9\xc2\xfd\x2a\xe8\xde\xc5\x36\x8b\x2a\x33\xed\xe1",
  49511. "\x14\xff\xb3\x97\x34\x5c\xcb\xe4\x4a\xa4\xde\xac\xd9\x36\x90\x46", 16
  49512. },
  49513. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  49514. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  49515. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e", 15,
  49516. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c", 15,
  49517. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4", 15,
  49518. "\x03\x8c\x41\x51\xba\x7a\x8f\x77\x6e\x56\x31\x99\x42\x0b\xc7\x03",
  49519. "\xe7\x6c\x67\xc9\xda\xb7\x0d\x5b\x44\x06\x26\x5a\xd0\xd2\x3b", 15
  49520. },
  49521. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  49522. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  49523. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  49524. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  49525. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7", 16,
  49526. "\x5c\x05\x23\x65\xf4\x57\x0a\xa0\xfb\x38\x3e\xce\x9b\x75\x85\xeb",
  49527. "\x68\x85\x19\x36\x0c\x7c\x48\x11\x40\xcb\x9b\x57\x9a\x0e\x65\x32", 16
  49528. },
  49529. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  49530. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  49531. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
  49532. "\xd5", 17,
  49533. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b"
  49534. "\xa0", 17,
  49535. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7"
  49536. "\x08", 17,
  49537. "\xaf\x58\x4b\xe7\x82\x1e\x96\x19\x29\x91\x25\xe0\xdd\x80\x3b\x49",
  49538. "\xa5\x11\xcd\xb6\x08\xf3\x76\xa0\xb6\xfa\x15\x82\xf3\x95\xe1\xeb"
  49539. "\xbd", 17
  49540. },
  49541. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  49542. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  49543. "\xb0\x5a\x1b\xc7\x56\xe7\xb6\x2c\xb4\x85\xe5\x56\xa5\x28\xc0\x6c"
  49544. "\x2f\x3b\x0b\x9d\x1a\x0c\xdf\x69\x47\xe0\xcc\xc0\x87\xaa\x5c\x09"
  49545. "\x98\x48\x8d\x6a\x8e\x1e\x05\xd7\x8b\x68\x74\x83\xb5\x1d\xf1\x2c", 48,
  49546. "\xe5\x8b\xd2\x6a\x30\xc5\xc5\x61\xcc\xbd\x7c\x27\xbf\xfe\xf9\x06"
  49547. "\x00\x5b\xd7\xfc\x11\x0b\xcf\x16\x61\xef\xac\x05\xa7\xaf\xec\x27"
  49548. "\x41\xc8\x5e\x9e\x0d\xf9\x2f\xaf\x20\x79\x17\xe5\x17\x91\x2a\x27"
  49549. "\x34\x1c\xbc\xaf\xeb\xef\x7f\x52\xe7\x1e\x4c\x2a\xca\xbd\x2b\xbe"
  49550. "\x34\xd6\xfb\x69\xd3\x3e\x49\x59\x60\xb4\x26\xc9\xb8\xce\xba", 79,
  49551. "\x6c\xe7\xcf\x7e\xab\x7b\xa0\xe1\xa7\x22\xcb\x88\xde\x5e\x42\xd2"
  49552. "\xec\x79\xe0\xa2\xcf\x5f\x0f\x6f\x6b\x89\x57\xcd\xae\x17\xd4\xc2"
  49553. "\xf3\x1b\xa2\xa8\x13\x78\x23\x2f\x83\xa8\xd4\x0c\xc0\xd2\xf3\x99"
  49554. "\xae\x81\xa1\xca\x5b\x5f\x45\xa6\x6f\x0c\x8a\xf3\xd4\x67\x40\x81"
  49555. "\x26\xe2\x01\x86\xe8\x5a\xd5\xf8\x58\x80\x9f\x56\xaa\x76\x96\xbf"
  49556. "\x31", 81,
  49557. "\x9a\x06\x33\xe0\xee\x00\x6a\x9b\xc8\x20\xd5\xe2\xc2\xed\xb5\x75",
  49558. "\xfa\x9e\x42\x2a\x31\x6b\xda\xca\xaa\x7d\x31\x8b\x84\x7a\xb8\xd7"
  49559. "\x8a\x81\x25\x64\xed\x41\x9b\xa9\x77\x10\xbd\x05\x0c\x4e\xc5\x31"
  49560. "\x0c\xa2\x86\xec\x8a\x94\xc8\x24\x23\x3c\x13\xee\xa5\x51\xc9\xdf"
  49561. "\x48\xc9\x55\xc5\x2f\x40\x73\x3f\x98\xbb\x8d\x69\x78\x46\x64\x17"
  49562. "\x8d\x49\x2f\x14\x62\xa4\x7c\x2a\x57\x38\x87\xce\xc6\x72\xd3\x5c"
  49563. "\xa1", 81
  49564. }};
  49565. int i;
  49566. byte computedCiphertext[82];
  49567. byte computedPlaintext[82];
  49568. byte siv[AES_BLOCK_SIZE];
  49569. wc_test_ret_t ret = 0;
  49570. WOLFSSL_ENTER("aes_siv_test");
  49571. for (i = 0; i < AES_SIV_TEST_VECTORS; ++i) {
  49572. ret = wc_AesSivEncrypt(testVectors[i].key, testVectors[i].keySz,
  49573. testVectors[i].assoc, testVectors[i].assocSz,
  49574. testVectors[i].nonce, testVectors[i].nonceSz,
  49575. testVectors[i].plaintext,
  49576. testVectors[i].plaintextSz, siv,
  49577. computedCiphertext);
  49578. if (ret != 0) {
  49579. return WC_TEST_RET_ENC_EC(ret);
  49580. }
  49581. ret = XMEMCMP(siv, testVectors[i].siv, AES_BLOCK_SIZE);
  49582. if (ret != 0) {
  49583. return WC_TEST_RET_ENC_NC;
  49584. }
  49585. ret = XMEMCMP(computedCiphertext, testVectors[i].ciphertext,
  49586. testVectors[i].ciphertextSz);
  49587. if (ret != 0) {
  49588. return WC_TEST_RET_ENC_NC;
  49589. }
  49590. ret = wc_AesSivDecrypt(testVectors[i].key, testVectors[i].keySz,
  49591. testVectors[i].assoc, testVectors[i].assocSz,
  49592. testVectors[i].nonce, testVectors[i].nonceSz,
  49593. computedCiphertext, testVectors[i].plaintextSz,
  49594. siv, computedPlaintext);
  49595. if (ret != 0) {
  49596. return WC_TEST_RET_ENC_EC(ret);
  49597. }
  49598. ret = XMEMCMP(computedPlaintext, testVectors[i].plaintext,
  49599. testVectors[i].plaintextSz);
  49600. if (ret != 0) {
  49601. return WC_TEST_RET_ENC_NC;
  49602. }
  49603. }
  49604. return 0;
  49605. }
  49606. #endif
  49607. #undef ERROR_OUT
  49608. static const int fiducial4 = WC_TEST_RET_LN;
  49609. /* print the fiducial line numbers assigned above, allowing confirmation of
  49610. * source code version match when in doubt.
  49611. */
  49612. static void print_fiducials(void) {
  49613. printf(" [fiducial line numbers: %d %d %d %d]\n",
  49614. fiducial1, fiducial2, fiducial3, fiducial4);
  49615. }
  49616. #else
  49617. #ifndef NO_MAIN_DRIVER
  49618. int main(void) { return 0; }
  49619. #endif
  49620. #endif /* NO_CRYPT_TEST */