test.c 2.0 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267442684426944270442714427244273442744427544276442774427844279442804428144282442834428444285442864428744288442894429044291442924429344294442954429644297442984429944300443014430244303443044430544306443074430844309443104431144312443134431444315443164431744318443194432044321443224432344324443254432644327443284432944330443314433244333443344433544336443374433844339443404434144342443434434444345443464434744348443494435044351443524435344354443554435644357443584435944360443614436244363443644436544366443674436844369443704437144372443734437444375443764437744378443794438044381443824438344384443854438644387443884438944390443914439244393443944439544396443974439844399444004440144402444034440444405444064440744408444094441044411444124441344414444154441644417444184441944420444214442244423444244442544426444274442844429444304443144432444334443444435444364443744438444394444044441444424444344444444454444644447444484444944450444514445244453444544445544456444574445844459444604446144462444634446444465444664446744468444694447044471444724447344474444754447644477444784447944480444814448244483444844448544486444874448844489444904449144492444934449444495444964449744498444994450044501445024450344504445054450644507445084450944510445114451244513445144451544516445174451844519445204452144522445234452444525445264452744528445294453044531445324453344534445354453644537445384453944540445414454244543445444454544546445474454844549445504455144552445534455444555445564455744558445594456044561445624456344564445654456644567445684456944570445714457244573445744457544576445774457844579445804458144582445834458444585445864458744588445894459044591445924459344594445954459644597445984459944600446014460244603446044460544606446074460844609446104461144612446134461444615446164461744618446194462044621446224462344624446254462644627446284462944630446314463244633446344463544636446374463844639446404464144642446434464444645446464464744648446494465044651446524465344654446554465644657446584465944660446614466244663446644466544666446674466844669446704467144672446734467444675446764467744678446794468044681446824468344684446854468644687446884468944690446914469244693446944469544696446974469844699447004470144702447034470444705447064470744708447094471044711447124471344714447154471644717447184471944720447214472244723447244472544726447274472844729447304473144732447334473444735447364473744738447394474044741447424474344744447454474644747447484474944750447514475244753447544475544756447574475844759447604476144762447634476444765447664476744768447694477044771447724477344774447754477644777447784477944780447814478244783447844478544786447874478844789447904479144792447934479444795447964479744798447994480044801448024480344804448054480644807448084480944810448114481244813448144481544816448174481844819448204482144822448234482444825448264482744828448294483044831448324483344834448354483644837448384483944840448414484244843448444484544846448474484844849448504485144852448534485444855448564485744858448594486044861448624486344864448654486644867448684486944870448714487244873448744487544876448774487844879448804488144882448834488444885448864488744888448894489044891448924489344894448954489644897448984489944900449014490244903449044490544906449074490844909449104491144912449134491444915449164491744918449194492044921449224492344924449254492644927449284492944930449314493244933449344493544936449374493844939449404494144942449434494444945449464494744948449494495044951449524495344954449554495644957449584495944960449614496244963449644496544966449674496844969449704497144972449734497444975449764497744978449794498044981449824498344984449854498644987449884498944990449914499244993449944499544996449974499844999450004500145002450034500445005450064500745008450094501045011450124501345014450154501645017450184501945020450214502245023450244502545026450274502845029450304503145032450334503445035450364503745038450394504045041450424504345044450454504645047450484504945050450514505245053450544505545056450574505845059450604506145062450634506445065450664506745068450694507045071450724507345074450754507645077450784507945080450814508245083450844508545086450874508845089450904509145092450934509445095450964509745098450994510045101451024510345104451054510645107451084510945110451114511245113451144511545116451174511845119451204512145122451234512445125451264512745128451294513045131451324513345134451354513645137451384513945140451414514245143451444514545146451474514845149451504515145152451534515445155451564515745158451594516045161451624516345164451654516645167451684516945170451714517245173451744517545176451774517845179451804518145182451834518445185451864518745188451894519045191451924519345194451954519645197451984519945200452014520245203452044520545206452074520845209452104521145212452134521445215452164521745218452194522045221452224522345224452254522645227452284522945230452314523245233452344523545236452374523845239452404524145242452434524445245452464524745248452494525045251452524525345254452554525645257452584525945260452614526245263452644526545266452674526845269452704527145272452734527445275452764527745278452794528045281452824528345284452854528645287452884528945290452914529245293452944529545296452974529845299453004530145302453034530445305453064530745308453094531045311453124531345314453154531645317453184531945320453214532245323453244532545326453274532845329453304533145332453334533445335453364533745338453394534045341453424534345344453454534645347453484534945350453514535245353453544535545356453574535845359453604536145362453634536445365453664536745368453694537045371453724537345374453754537645377453784537945380453814538245383453844538545386453874538845389453904539145392453934539445395453964539745398453994540045401454024540345404454054540645407454084540945410454114541245413454144541545416454174541845419454204542145422454234542445425454264542745428454294543045431454324543345434454354543645437454384543945440454414544245443454444544545446454474544845449454504545145452454534545445455454564545745458454594546045461454624546345464454654546645467454684546945470454714547245473454744547545476454774547845479454804548145482454834548445485454864548745488454894549045491454924549345494454954549645497454984549945500455014550245503455044550545506455074550845509455104551145512455134551445515455164551745518455194552045521455224552345524455254552645527455284552945530455314553245533455344553545536455374553845539455404554145542455434554445545455464554745548455494555045551455524555345554455554555645557455584555945560455614556245563455644556545566455674556845569455704557145572455734557445575455764557745578455794558045581455824558345584455854558645587455884558945590455914559245593455944559545596455974559845599456004560145602456034560445605456064560745608456094561045611456124561345614456154561645617456184561945620456214562245623456244562545626456274562845629456304563145632456334563445635456364563745638456394564045641456424564345644456454564645647456484564945650456514565245653456544565545656456574565845659456604566145662456634566445665456664566745668456694567045671456724567345674456754567645677456784567945680456814568245683456844568545686456874568845689456904569145692456934569445695456964569745698456994570045701457024570345704457054570645707457084570945710457114571245713457144571545716457174571845719457204572145722457234572445725457264572745728457294573045731457324573345734457354573645737457384573945740457414574245743457444574545746457474574845749457504575145752457534575445755457564575745758457594576045761457624576345764457654576645767457684576945770457714577245773457744577545776457774577845779457804578145782457834578445785457864578745788457894579045791457924579345794457954579645797457984579945800458014580245803458044580545806458074580845809458104581145812458134581445815458164581745818458194582045821458224582345824458254582645827458284582945830458314583245833458344583545836458374583845839458404584145842458434584445845458464584745848458494585045851458524585345854458554585645857458584585945860458614586245863458644586545866458674586845869458704587145872458734587445875458764587745878458794588045881458824588345884458854588645887458884588945890458914589245893458944589545896458974589845899459004590145902459034590445905459064590745908459094591045911459124591345914459154591645917459184591945920459214592245923459244592545926459274592845929459304593145932459334593445935459364593745938459394594045941459424594345944459454594645947459484594945950459514595245953459544595545956459574595845959459604596145962459634596445965459664596745968459694597045971459724597345974459754597645977459784597945980459814598245983459844598545986459874598845989459904599145992459934599445995459964599745998459994600046001460024600346004460054600646007460084600946010460114601246013460144601546016460174601846019460204602146022460234602446025460264602746028460294603046031460324603346034460354603646037460384603946040460414604246043460444604546046460474604846049460504605146052460534605446055460564605746058460594606046061460624606346064460654606646067460684606946070460714607246073460744607546076460774607846079460804608146082460834608446085460864608746088460894609046091460924609346094460954609646097460984609946100461014610246103461044610546106461074610846109461104611146112461134611446115461164611746118461194612046121461224612346124461254612646127461284612946130461314613246133461344613546136461374613846139461404614146142461434614446145461464614746148461494615046151461524615346154461554615646157461584615946160461614616246163461644616546166461674616846169461704617146172461734617446175461764617746178461794618046181461824618346184461854618646187461884618946190461914619246193461944619546196461974619846199462004620146202462034620446205462064620746208462094621046211462124621346214462154621646217462184621946220462214622246223462244622546226462274622846229462304623146232462334623446235462364623746238462394624046241462424624346244462454624646247462484624946250462514625246253462544625546256462574625846259462604626146262462634626446265462664626746268462694627046271462724627346274462754627646277462784627946280462814628246283462844628546286462874628846289462904629146292462934629446295462964629746298462994630046301463024630346304463054630646307463084630946310463114631246313463144631546316463174631846319463204632146322463234632446325463264632746328463294633046331463324633346334463354633646337463384633946340463414634246343463444634546346463474634846349463504635146352463534635446355463564635746358463594636046361463624636346364463654636646367463684636946370463714637246373463744637546376463774637846379463804638146382463834638446385463864638746388463894639046391463924639346394463954639646397463984639946400464014640246403464044640546406464074640846409464104641146412464134641446415464164641746418464194642046421464224642346424464254642646427464284642946430464314643246433464344643546436464374643846439464404644146442464434644446445464464644746448464494645046451464524645346454464554645646457464584645946460464614646246463464644646546466464674646846469464704647146472464734647446475464764647746478464794648046481464824648346484464854648646487464884648946490464914649246493464944649546496464974649846499465004650146502465034650446505465064650746508465094651046511465124651346514465154651646517465184651946520465214652246523465244652546526465274652846529465304653146532465334653446535465364653746538465394654046541465424654346544465454654646547465484654946550465514655246553465544655546556465574655846559465604656146562465634656446565465664656746568465694657046571465724657346574465754657646577465784657946580465814658246583465844658546586465874658846589465904659146592465934659446595465964659746598465994660046601466024660346604466054660646607466084660946610466114661246613466144661546616466174661846619466204662146622466234662446625466264662746628466294663046631466324663346634466354663646637466384663946640466414664246643466444664546646466474664846649466504665146652466534665446655466564665746658466594666046661466624666346664466654666646667466684666946670466714667246673466744667546676466774667846679466804668146682466834668446685466864668746688466894669046691466924669346694466954669646697466984669946700467014670246703467044670546706467074670846709467104671146712467134671446715467164671746718467194672046721467224672346724467254672646727467284672946730467314673246733467344673546736467374673846739467404674146742467434674446745467464674746748467494675046751467524675346754467554675646757467584675946760467614676246763467644676546766467674676846769467704677146772467734677446775467764677746778467794678046781467824678346784467854678646787467884678946790467914679246793467944679546796467974679846799468004680146802468034680446805468064680746808468094681046811468124681346814468154681646817468184681946820468214682246823468244682546826468274682846829468304683146832468334683446835468364683746838468394684046841468424684346844468454684646847468484684946850468514685246853468544685546856468574685846859468604686146862468634686446865468664686746868468694687046871468724687346874468754687646877468784687946880468814688246883468844688546886468874688846889468904689146892468934689446895468964689746898468994690046901469024690346904469054690646907469084690946910469114691246913469144691546916469174691846919469204692146922469234692446925469264692746928469294693046931469324693346934469354693646937469384693946940469414694246943469444694546946469474694846949469504695146952469534695446955469564695746958469594696046961469624696346964469654696646967469684696946970469714697246973469744697546976469774697846979469804698146982469834698446985469864698746988469894699046991469924699346994469954699646997469984699947000470014700247003470044700547006470074700847009470104701147012470134701447015470164701747018470194702047021470224702347024470254702647027470284702947030470314703247033470344703547036470374703847039470404704147042470434704447045470464704747048470494705047051470524705347054470554705647057470584705947060470614706247063470644706547066470674706847069470704707147072470734707447075470764707747078470794708047081470824708347084470854708647087470884708947090470914709247093470944709547096470974709847099471004710147102471034710447105471064710747108471094711047111471124711347114471154711647117471184711947120471214712247123471244712547126471274712847129471304713147132471334713447135471364713747138471394714047141471424714347144471454714647147471484714947150471514715247153471544715547156471574715847159471604716147162471634716447165471664716747168471694717047171471724717347174471754717647177471784717947180471814718247183471844718547186471874718847189471904719147192471934719447195471964719747198471994720047201472024720347204472054720647207472084720947210472114721247213472144721547216472174721847219472204722147222472234722447225472264722747228472294723047231472324723347234472354723647237472384723947240472414724247243472444724547246472474724847249472504725147252472534725447255472564725747258472594726047261472624726347264472654726647267472684726947270472714727247273472744727547276472774727847279472804728147282472834728447285472864728747288472894729047291472924729347294472954729647297472984729947300473014730247303473044730547306473074730847309473104731147312473134731447315473164731747318473194732047321473224732347324473254732647327473284732947330473314733247333473344733547336473374733847339473404734147342473434734447345473464734747348473494735047351473524735347354473554735647357473584735947360473614736247363473644736547366473674736847369473704737147372473734737447375473764737747378473794738047381473824738347384473854738647387473884738947390473914739247393473944739547396473974739847399474004740147402474034740447405474064740747408474094741047411474124741347414474154741647417474184741947420474214742247423474244742547426474274742847429474304743147432474334743447435474364743747438474394744047441474424744347444474454744647447474484744947450474514745247453474544745547456474574745847459474604746147462474634746447465474664746747468474694747047471474724747347474474754747647477474784747947480474814748247483474844748547486474874748847489474904749147492474934749447495474964749747498474994750047501475024750347504475054750647507475084750947510475114751247513475144751547516475174751847519475204752147522475234752447525475264752747528475294753047531475324753347534475354753647537475384753947540475414754247543475444754547546475474754847549475504755147552475534755447555475564755747558475594756047561475624756347564475654756647567475684756947570475714757247573475744757547576475774757847579475804758147582475834758447585475864758747588475894759047591475924759347594475954759647597475984759947600476014760247603476044760547606476074760847609476104761147612476134761447615476164761747618476194762047621476224762347624476254762647627476284762947630476314763247633476344763547636476374763847639476404764147642476434764447645476464764747648476494765047651476524765347654476554765647657476584765947660476614766247663476644766547666476674766847669476704767147672476734767447675476764767747678476794768047681476824768347684476854768647687476884768947690476914769247693476944769547696476974769847699477004770147702477034770447705477064770747708477094771047711477124771347714477154771647717477184771947720477214772247723477244772547726477274772847729477304773147732477334773447735477364773747738477394774047741477424774347744477454774647747477484774947750477514775247753477544775547756477574775847759477604776147762477634776447765477664776747768477694777047771477724777347774477754777647777477784777947780477814778247783477844778547786477874778847789477904779147792477934779447795477964779747798477994780047801478024780347804478054780647807478084780947810478114781247813478144781547816478174781847819478204782147822478234782447825478264782747828478294783047831478324783347834478354783647837478384783947840478414784247843478444784547846478474784847849478504785147852478534785447855478564785747858478594786047861478624786347864478654786647867478684786947870478714787247873478744787547876478774787847879478804788147882478834788447885478864788747888478894789047891478924789347894478954789647897478984789947900479014790247903479044790547906479074790847909479104791147912479134791447915479164791747918479194792047921479224792347924479254792647927479284792947930479314793247933479344793547936479374793847939479404794147942479434794447945479464794747948479494795047951479524795347954479554795647957479584795947960479614796247963479644796547966479674796847969479704797147972479734797447975479764797747978479794798047981479824798347984479854798647987479884798947990479914799247993479944799547996479974799847999480004800148002480034800448005480064800748008480094801048011480124801348014480154801648017480184801948020480214802248023480244802548026480274802848029480304803148032480334803448035480364803748038480394804048041480424804348044480454804648047480484804948050480514805248053480544805548056480574805848059480604806148062480634806448065480664806748068480694807048071480724807348074480754807648077480784807948080480814808248083480844808548086480874808848089480904809148092480934809448095480964809748098480994810048101481024810348104481054810648107481084810948110481114811248113481144811548116481174811848119481204812148122481234812448125481264812748128481294813048131481324813348134481354813648137481384813948140481414814248143481444814548146481474814848149481504815148152481534815448155481564815748158481594816048161481624816348164481654816648167481684816948170481714817248173481744817548176481774817848179481804818148182481834818448185481864818748188481894819048191481924819348194481954819648197481984819948200482014820248203482044820548206482074820848209482104821148212482134821448215482164821748218482194822048221482224822348224482254822648227482284822948230482314823248233482344823548236482374823848239482404824148242482434824448245482464824748248482494825048251482524825348254482554825648257482584825948260482614826248263482644826548266482674826848269482704827148272482734827448275482764827748278482794828048281482824828348284482854828648287482884828948290482914829248293482944829548296482974829848299483004830148302483034830448305483064830748308483094831048311483124831348314483154831648317483184831948320483214832248323483244832548326483274832848329483304833148332483334833448335483364833748338483394834048341483424834348344483454834648347483484834948350483514835248353483544835548356483574835848359483604836148362483634836448365483664836748368483694837048371483724837348374483754837648377483784837948380483814838248383483844838548386483874838848389483904839148392483934839448395483964839748398483994840048401484024840348404484054840648407484084840948410484114841248413484144841548416484174841848419484204842148422484234842448425484264842748428484294843048431484324843348434484354843648437484384843948440484414844248443484444844548446484474844848449484504845148452484534845448455484564845748458484594846048461484624846348464484654846648467484684846948470484714847248473484744847548476484774847848479484804848148482484834848448485484864848748488484894849048491484924849348494484954849648497484984849948500485014850248503485044850548506485074850848509485104851148512485134851448515485164851748518485194852048521485224852348524485254852648527485284852948530485314853248533485344853548536485374853848539485404854148542485434854448545485464854748548485494855048551485524855348554485554855648557485584855948560485614856248563485644856548566485674856848569485704857148572485734857448575485764857748578485794858048581485824858348584485854858648587485884858948590485914859248593485944859548596485974859848599486004860148602486034860448605486064860748608486094861048611486124861348614486154861648617486184861948620486214862248623486244862548626486274862848629486304863148632486334863448635486364863748638486394864048641486424864348644486454864648647486484864948650486514865248653486544865548656486574865848659486604866148662486634866448665486664866748668486694867048671486724867348674486754867648677486784867948680486814868248683486844868548686486874868848689486904869148692486934869448695486964869748698486994870048701487024870348704487054870648707487084870948710487114871248713487144871548716487174871848719487204872148722487234872448725487264872748728487294873048731487324873348734487354873648737487384873948740487414874248743487444874548746487474874848749487504875148752487534875448755487564875748758487594876048761487624876348764487654876648767487684876948770487714877248773487744877548776487774877848779487804878148782487834878448785487864878748788487894879048791487924879348794487954879648797487984879948800488014880248803488044880548806488074880848809488104881148812488134881448815488164881748818488194882048821488224882348824488254882648827488284882948830488314883248833488344883548836488374883848839488404884148842488434884448845488464884748848488494885048851488524885348854488554885648857488584885948860488614886248863488644886548866488674886848869488704887148872488734887448875488764887748878488794888048881488824888348884488854888648887488884888948890488914889248893488944889548896488974889848899489004890148902489034890448905489064890748908489094891048911489124891348914489154891648917489184891948920489214892248923489244892548926489274892848929489304893148932489334893448935489364893748938489394894048941489424894348944489454894648947489484894948950489514895248953489544895548956489574895848959489604896148962489634896448965489664896748968489694897048971489724897348974489754897648977489784897948980489814898248983489844898548986489874898848989489904899148992489934899448995489964899748998489994900049001490024900349004490054900649007490084900949010490114901249013490144901549016490174901849019490204902149022490234902449025490264902749028490294903049031490324903349034490354903649037490384903949040490414904249043490444904549046490474904849049490504905149052490534905449055490564905749058490594906049061490624906349064490654906649067490684906949070490714907249073490744907549076490774907849079490804908149082490834908449085490864908749088490894909049091490924909349094490954909649097490984909949100491014910249103491044910549106491074910849109491104911149112491134911449115491164911749118491194912049121491224912349124491254912649127491284912949130491314913249133491344913549136491374913849139491404914149142491434914449145491464914749148491494915049151491524915349154491554915649157491584915949160491614916249163491644916549166491674916849169491704917149172491734917449175491764917749178491794918049181491824918349184491854918649187491884918949190491914919249193491944919549196491974919849199492004920149202492034920449205492064920749208492094921049211492124921349214492154921649217492184921949220492214922249223492244922549226492274922849229492304923149232492334923449235492364923749238492394924049241492424924349244492454924649247492484924949250492514925249253492544925549256492574925849259492604926149262492634926449265492664926749268492694927049271492724927349274492754927649277492784927949280492814928249283492844928549286492874928849289492904929149292492934929449295492964929749298492994930049301493024930349304493054930649307493084930949310493114931249313493144931549316493174931849319493204932149322493234932449325493264932749328493294933049331493324933349334493354933649337493384933949340493414934249343493444934549346493474934849349493504935149352493534935449355493564935749358493594936049361493624936349364493654936649367493684936949370493714937249373493744937549376493774937849379493804938149382493834938449385493864938749388493894939049391493924939349394493954939649397493984939949400494014940249403494044940549406494074940849409494104941149412494134941449415494164941749418494194942049421494224942349424494254942649427494284942949430494314943249433494344943549436494374943849439494404944149442494434944449445494464944749448494494945049451494524945349454494554945649457494584945949460494614946249463494644946549466494674946849469494704947149472494734947449475494764947749478494794948049481494824948349484494854948649487494884948949490494914949249493494944949549496494974949849499495004950149502495034950449505495064950749508495094951049511495124951349514495154951649517495184951949520495214952249523495244952549526495274952849529495304953149532495334953449535495364953749538495394954049541495424954349544495454954649547495484954949550495514955249553495544955549556495574955849559495604956149562495634956449565495664956749568495694957049571495724957349574495754957649577495784957949580495814958249583495844958549586495874958849589495904959149592495934959449595495964959749598495994960049601496024960349604496054960649607496084960949610496114961249613496144961549616496174961849619496204962149622496234962449625496264962749628496294963049631496324963349634496354963649637496384963949640496414964249643496444964549646496474964849649496504965149652496534965449655496564965749658496594966049661496624966349664496654966649667496684966949670496714967249673496744967549676496774967849679496804968149682496834968449685496864968749688496894969049691496924969349694496954969649697496984969949700497014970249703497044970549706497074970849709497104971149712497134971449715497164971749718497194972049721497224972349724497254972649727497284972949730497314973249733497344973549736497374973849739497404974149742497434974449745497464974749748497494975049751497524975349754497554975649757497584975949760497614976249763497644976549766497674976849769497704977149772497734977449775497764977749778497794978049781497824978349784497854978649787497884978949790497914979249793497944979549796497974979849799498004980149802498034980449805498064980749808498094981049811498124981349814498154981649817498184981949820498214982249823498244982549826498274982849829498304983149832498334983449835498364983749838498394984049841498424984349844498454984649847498484984949850498514985249853498544985549856498574985849859498604986149862498634986449865498664986749868498694987049871498724987349874498754987649877498784987949880498814988249883498844988549886498874988849889498904989149892498934989449895498964989749898498994990049901499024990349904499054990649907499084990949910499114991249913499144991549916499174991849919499204992149922499234992449925499264992749928499294993049931499324993349934499354993649937499384993949940499414994249943499444994549946499474994849949499504995149952499534995449955499564995749958499594996049961499624996349964499654996649967499684996949970499714997249973499744997549976499774997849979499804998149982499834998449985499864998749988499894999049991499924999349994499954999649997499984999950000500015000250003500045000550006500075000850009500105001150012500135001450015500165001750018500195002050021500225002350024500255002650027500285002950030500315003250033500345003550036500375003850039500405004150042500435004450045500465004750048500495005050051500525005350054500555005650057500585005950060500615006250063500645006550066500675006850069500705007150072500735007450075500765007750078500795008050081500825008350084500855008650087500885008950090500915009250093500945009550096500975009850099501005010150102501035010450105501065010750108501095011050111501125011350114501155011650117501185011950120501215012250123501245012550126501275012850129501305013150132501335013450135501365013750138501395014050141501425014350144501455014650147501485014950150501515015250153501545015550156501575015850159501605016150162501635016450165501665016750168501695017050171501725017350174501755017650177501785017950180501815018250183501845018550186501875018850189501905019150192501935019450195501965019750198501995020050201502025020350204502055020650207502085020950210502115021250213502145021550216502175021850219502205022150222502235022450225502265022750228502295023050231502325023350234502355023650237502385023950240502415024250243502445024550246502475024850249502505025150252502535025450255502565025750258502595026050261502625026350264502655026650267502685026950270502715027250273502745027550276502775027850279502805028150282502835028450285502865028750288502895029050291502925029350294502955029650297502985029950300503015030250303503045030550306503075030850309503105031150312503135031450315503165031750318503195032050321503225032350324503255032650327503285032950330503315033250333503345033550336503375033850339503405034150342503435034450345503465034750348503495035050351503525035350354503555035650357503585035950360503615036250363503645036550366503675036850369503705037150372503735037450375503765037750378503795038050381503825038350384503855038650387503885038950390503915039250393503945039550396503975039850399504005040150402504035040450405504065040750408504095041050411504125041350414504155041650417504185041950420504215042250423504245042550426504275042850429504305043150432504335043450435504365043750438504395044050441504425044350444504455044650447504485044950450504515045250453504545045550456504575045850459504605046150462504635046450465504665046750468504695047050471504725047350474504755047650477504785047950480504815048250483504845048550486504875048850489504905049150492504935049450495504965049750498504995050050501505025050350504505055050650507505085050950510505115051250513505145051550516505175051850519505205052150522505235052450525505265052750528505295053050531505325053350534505355053650537505385053950540505415054250543505445054550546505475054850549505505055150552505535055450555505565055750558505595056050561505625056350564505655056650567505685056950570505715057250573505745057550576505775057850579505805058150582505835058450585505865058750588505895059050591505925059350594505955059650597505985059950600506015060250603506045060550606506075060850609506105061150612506135061450615506165061750618506195062050621506225062350624506255062650627506285062950630506315063250633506345063550636506375063850639506405064150642506435064450645506465064750648506495065050651506525065350654506555065650657506585065950660506615066250663506645066550666506675066850669506705067150672506735067450675506765067750678506795068050681506825068350684506855068650687506885068950690506915069250693506945069550696506975069850699507005070150702507035070450705507065070750708507095071050711507125071350714507155071650717507185071950720507215072250723507245072550726507275072850729507305073150732507335073450735507365073750738507395074050741507425074350744507455074650747507485074950750507515075250753507545075550756507575075850759507605076150762507635076450765507665076750768507695077050771507725077350774507755077650777507785077950780507815078250783507845078550786507875078850789507905079150792507935079450795507965079750798507995080050801508025080350804508055080650807508085080950810508115081250813508145081550816508175081850819508205082150822508235082450825508265082750828508295083050831508325083350834508355083650837508385083950840508415084250843508445084550846508475084850849508505085150852508535085450855508565085750858508595086050861508625086350864508655086650867508685086950870508715087250873508745087550876508775087850879508805088150882508835088450885508865088750888508895089050891508925089350894508955089650897508985089950900509015090250903509045090550906509075090850909509105091150912509135091450915509165091750918509195092050921509225092350924509255092650927509285092950930509315093250933509345093550936509375093850939509405094150942509435094450945509465094750948509495095050951509525095350954509555095650957509585095950960509615096250963509645096550966509675096850969509705097150972509735097450975509765097750978509795098050981509825098350984509855098650987509885098950990509915099250993509945099550996509975099850999510005100151002510035100451005510065100751008510095101051011510125101351014510155101651017510185101951020510215102251023510245102551026510275102851029510305103151032510335103451035510365103751038510395104051041510425104351044510455104651047510485104951050510515105251053510545105551056510575105851059510605106151062510635106451065510665106751068510695107051071510725107351074510755107651077510785107951080510815108251083510845108551086510875108851089510905109151092510935109451095510965109751098510995110051101511025110351104511055110651107511085110951110511115111251113511145111551116511175111851119511205112151122511235112451125511265112751128511295113051131511325113351134511355113651137511385113951140511415114251143511445114551146511475114851149511505115151152511535115451155511565115751158511595116051161511625116351164511655116651167511685116951170511715117251173511745117551176511775117851179511805118151182511835118451185511865118751188511895119051191511925119351194511955119651197511985119951200512015120251203512045120551206512075120851209512105121151212512135121451215512165121751218512195122051221512225122351224512255122651227512285122951230512315123251233512345123551236512375123851239512405124151242512435124451245512465124751248512495125051251512525125351254512555125651257512585125951260512615126251263512645126551266512675126851269512705127151272512735127451275512765127751278512795128051281512825128351284512855128651287512885128951290512915129251293512945129551296512975129851299513005130151302513035130451305513065130751308513095131051311513125131351314513155131651317513185131951320513215132251323513245132551326513275132851329513305133151332513335133451335513365133751338513395134051341513425134351344513455134651347513485134951350513515135251353513545135551356513575135851359513605136151362513635136451365513665136751368513695137051371513725137351374513755137651377513785137951380513815138251383513845138551386513875138851389513905139151392513935139451395513965139751398513995140051401514025140351404514055140651407514085140951410514115141251413514145141551416514175141851419514205142151422514235142451425514265142751428514295143051431514325143351434514355143651437514385143951440514415144251443514445144551446514475144851449514505145151452514535145451455514565145751458514595146051461514625146351464514655146651467514685146951470514715147251473514745147551476514775147851479514805148151482514835148451485514865148751488514895149051491514925149351494514955149651497514985149951500515015150251503515045150551506515075150851509515105151151512515135151451515515165151751518515195152051521515225152351524515255152651527515285152951530515315153251533515345153551536515375153851539515405154151542515435154451545515465154751548515495155051551515525155351554515555155651557515585155951560515615156251563515645156551566515675156851569515705157151572515735157451575515765157751578515795158051581515825158351584515855158651587515885158951590515915159251593515945159551596515975159851599516005160151602516035160451605516065160751608516095161051611516125161351614516155161651617516185161951620516215162251623516245162551626516275162851629516305163151632516335163451635516365163751638516395164051641516425164351644516455164651647516485164951650516515165251653516545165551656516575165851659516605166151662516635166451665516665166751668516695167051671516725167351674516755167651677516785167951680516815168251683516845168551686516875168851689516905169151692516935169451695516965169751698516995170051701517025170351704517055170651707517085170951710517115171251713517145171551716517175171851719517205172151722517235172451725517265172751728517295173051731517325173351734517355173651737517385173951740517415174251743517445174551746517475174851749517505175151752517535175451755517565175751758517595176051761517625176351764517655176651767517685176951770517715177251773517745177551776517775177851779517805178151782517835178451785517865178751788517895179051791517925179351794517955179651797517985179951800518015180251803518045180551806518075180851809518105181151812518135181451815518165181751818518195182051821518225182351824518255182651827518285182951830518315183251833518345183551836518375183851839518405184151842518435184451845518465184751848518495185051851518525185351854518555185651857518585185951860518615186251863518645186551866518675186851869518705187151872518735187451875518765187751878518795188051881518825188351884518855188651887518885188951890518915189251893518945189551896518975189851899519005190151902519035190451905519065190751908519095191051911519125191351914519155191651917519185191951920519215192251923519245192551926519275192851929519305193151932519335193451935519365193751938519395194051941519425194351944519455194651947519485194951950519515195251953519545195551956519575195851959519605196151962519635196451965519665196751968519695197051971519725197351974519755197651977519785197951980519815198251983519845198551986519875198851989519905199151992519935199451995519965199751998519995200052001520025200352004520055200652007520085200952010520115201252013520145201552016520175201852019520205202152022520235202452025520265202752028520295203052031520325203352034520355203652037520385203952040520415204252043520445204552046520475204852049520505205152052520535205452055520565205752058520595206052061520625206352064520655206652067520685206952070520715207252073520745207552076520775207852079520805208152082520835208452085520865208752088520895209052091520925209352094520955209652097520985209952100521015210252103521045210552106521075210852109521105211152112521135211452115521165211752118521195212052121521225212352124521255212652127521285212952130521315213252133521345213552136521375213852139521405214152142521435214452145521465214752148521495215052151521525215352154521555215652157521585215952160521615216252163521645216552166521675216852169521705217152172521735217452175521765217752178521795218052181521825218352184521855218652187521885218952190521915219252193521945219552196521975219852199522005220152202522035220452205522065220752208522095221052211522125221352214522155221652217522185221952220522215222252223522245222552226522275222852229522305223152232522335223452235522365223752238522395224052241522425224352244522455224652247522485224952250522515225252253522545225552256522575225852259522605226152262522635226452265522665226752268522695227052271522725227352274522755227652277522785227952280522815228252283522845228552286522875228852289522905229152292522935229452295522965229752298522995230052301523025230352304523055230652307523085230952310523115231252313523145231552316523175231852319523205232152322523235232452325523265232752328523295233052331523325233352334523355233652337523385233952340523415234252343523445234552346523475234852349523505235152352523535235452355523565235752358523595236052361523625236352364523655236652367523685236952370523715237252373523745237552376523775237852379523805238152382523835238452385523865238752388523895239052391523925239352394523955239652397523985239952400524015240252403524045240552406524075240852409524105241152412524135241452415524165241752418524195242052421524225242352424524255242652427524285242952430524315243252433524345243552436524375243852439524405244152442524435244452445524465244752448524495245052451524525245352454524555245652457524585245952460524615246252463524645246552466524675246852469524705247152472524735247452475524765247752478524795248052481524825248352484524855248652487524885248952490524915249252493524945249552496524975249852499525005250152502525035250452505525065250752508525095251052511525125251352514525155251652517525185251952520525215252252523525245252552526525275252852529525305253152532525335253452535525365253752538525395254052541525425254352544525455254652547525485254952550525515255252553525545255552556525575255852559525605256152562525635256452565525665256752568525695257052571525725257352574525755257652577525785257952580525815258252583525845258552586525875258852589525905259152592525935259452595525965259752598525995260052601526025260352604526055260652607526085260952610526115261252613526145261552616526175261852619526205262152622526235262452625526265262752628526295263052631526325263352634526355263652637526385263952640526415264252643526445264552646526475264852649526505265152652526535265452655526565265752658526595266052661526625266352664526655266652667526685266952670526715267252673526745267552676526775267852679526805268152682526835268452685526865268752688526895269052691526925269352694526955269652697526985269952700527015270252703527045270552706527075270852709527105271152712527135271452715527165271752718527195272052721527225272352724527255272652727527285272952730527315273252733527345273552736527375273852739527405274152742527435274452745527465274752748527495275052751527525275352754527555275652757527585275952760527615276252763527645276552766527675276852769527705277152772527735277452775527765277752778527795278052781527825278352784527855278652787527885278952790527915279252793527945279552796527975279852799528005280152802528035280452805528065280752808528095281052811528125281352814528155281652817528185281952820528215282252823528245282552826528275282852829528305283152832528335283452835528365283752838528395284052841528425284352844528455284652847528485284952850528515285252853528545285552856528575285852859528605286152862528635286452865528665286752868528695287052871528725287352874528755287652877528785287952880528815288252883528845288552886528875288852889528905289152892528935289452895528965289752898528995290052901529025290352904529055290652907529085290952910529115291252913529145291552916529175291852919529205292152922529235292452925529265292752928529295293052931529325293352934529355293652937529385293952940529415294252943529445294552946529475294852949529505295152952529535295452955529565295752958529595296052961529625296352964529655296652967529685296952970529715297252973529745297552976529775297852979529805298152982529835298452985529865298752988529895299052991529925299352994529955299652997529985299953000530015300253003530045300553006530075300853009530105301153012530135301453015530165301753018530195302053021530225302353024530255302653027530285302953030530315303253033530345303553036530375303853039530405304153042530435304453045530465304753048530495305053051530525305353054530555305653057530585305953060530615306253063530645306553066530675306853069530705307153072530735307453075530765307753078530795308053081530825308353084530855308653087530885308953090530915309253093530945309553096530975309853099531005310153102531035310453105531065310753108531095311053111531125311353114531155311653117531185311953120531215312253123531245312553126531275312853129531305313153132531335313453135531365313753138531395314053141531425314353144531455314653147531485314953150531515315253153531545315553156531575315853159531605316153162531635316453165531665316753168531695317053171531725317353174531755317653177531785317953180531815318253183531845318553186531875318853189531905319153192531935319453195531965319753198531995320053201532025320353204532055320653207532085320953210532115321253213532145321553216532175321853219532205322153222532235322453225532265322753228532295323053231532325323353234532355323653237532385323953240532415324253243532445324553246532475324853249532505325153252532535325453255532565325753258532595326053261532625326353264532655326653267532685326953270532715327253273532745327553276532775327853279532805328153282532835328453285532865328753288532895329053291532925329353294532955329653297532985329953300533015330253303533045330553306533075330853309533105331153312533135331453315533165331753318533195332053321533225332353324533255332653327533285332953330533315333253333533345333553336533375333853339533405334153342533435334453345533465334753348533495335053351533525335353354533555335653357533585335953360533615336253363533645336553366533675336853369533705337153372533735337453375533765337753378533795338053381533825338353384533855338653387533885338953390533915339253393533945339553396533975339853399534005340153402534035340453405534065340753408534095341053411534125341353414534155341653417534185341953420534215342253423534245342553426534275342853429534305343153432534335343453435534365343753438534395344053441534425344353444534455344653447534485344953450534515345253453534545345553456534575345853459534605346153462534635346453465534665346753468534695347053471534725347353474534755347653477534785347953480534815348253483534845348553486534875348853489534905349153492534935349453495534965349753498534995350053501535025350353504535055350653507535085350953510535115351253513535145351553516535175351853519535205352153522535235352453525535265352753528535295353053531535325353353534535355353653537535385353953540535415354253543535445354553546535475354853549535505355153552535535355453555535565355753558535595356053561535625356353564535655356653567535685356953570535715357253573535745357553576535775357853579535805358153582535835358453585535865358753588535895359053591535925359353594535955359653597535985359953600536015360253603536045360553606536075360853609536105361153612536135361453615536165361753618536195362053621536225362353624536255362653627536285362953630536315363253633536345363553636536375363853639536405364153642536435364453645536465364753648536495365053651536525365353654536555365653657536585365953660536615366253663536645366553666536675366853669536705367153672536735367453675536765367753678536795368053681536825368353684536855368653687536885368953690536915369253693536945369553696536975369853699537005370153702537035370453705537065370753708537095371053711537125371353714537155371653717537185371953720537215372253723537245372553726537275372853729537305373153732537335373453735537365373753738537395374053741537425374353744537455374653747537485374953750537515375253753537545375553756537575375853759537605376153762537635376453765537665376753768537695377053771537725377353774537755377653777537785377953780537815378253783537845378553786537875378853789537905379153792537935379453795537965379753798537995380053801538025380353804538055380653807538085380953810538115381253813538145381553816538175381853819538205382153822538235382453825538265382753828538295383053831538325383353834538355383653837538385383953840538415384253843538445384553846538475384853849538505385153852538535385453855538565385753858538595386053861538625386353864538655386653867538685386953870538715387253873538745387553876538775387853879538805388153882538835388453885538865388753888538895389053891538925389353894538955389653897538985389953900539015390253903539045390553906539075390853909539105391153912539135391453915539165391753918539195392053921539225392353924539255392653927539285392953930539315393253933539345393553936539375393853939539405394153942539435394453945539465394753948539495395053951539525395353954539555395653957539585395953960539615396253963539645396553966539675396853969539705397153972539735397453975539765397753978539795398053981539825398353984539855398653987539885398953990539915399253993539945399553996539975399853999540005400154002540035400454005540065400754008540095401054011540125401354014540155401654017540185401954020540215402254023540245402554026540275402854029540305403154032540335403454035540365403754038540395404054041540425404354044540455404654047540485404954050540515405254053540545405554056540575405854059540605406154062540635406454065540665406754068540695407054071540725407354074540755407654077540785407954080540815408254083540845408554086540875408854089540905409154092540935409454095540965409754098540995410054101541025410354104541055410654107541085410954110541115411254113541145411554116541175411854119541205412154122541235412454125541265412754128541295413054131541325413354134541355413654137541385413954140541415414254143541445414554146541475414854149541505415154152541535415454155541565415754158541595416054161541625416354164541655416654167541685416954170541715417254173541745417554176541775417854179541805418154182541835418454185541865418754188541895419054191541925419354194541955419654197541985419954200542015420254203542045420554206542075420854209542105421154212542135421454215542165421754218542195422054221542225422354224542255422654227542285422954230542315423254233542345423554236542375423854239542405424154242542435424454245542465424754248542495425054251542525425354254542555425654257542585425954260542615426254263542645426554266542675426854269542705427154272542735427454275542765427754278542795428054281542825428354284542855428654287542885428954290542915429254293542945429554296542975429854299543005430154302543035430454305543065430754308543095431054311543125431354314543155431654317543185431954320543215432254323543245432554326543275432854329543305433154332543335433454335543365433754338543395434054341543425434354344543455434654347543485434954350543515435254353543545435554356543575435854359543605436154362543635436454365543665436754368543695437054371543725437354374543755437654377543785437954380543815438254383543845438554386543875438854389543905439154392543935439454395543965439754398543995440054401544025440354404544055440654407544085440954410544115441254413544145441554416544175441854419544205442154422544235442454425544265442754428544295443054431544325443354434544355443654437544385443954440544415444254443544445444554446544475444854449544505445154452544535445454455544565445754458544595446054461544625446354464544655446654467544685446954470544715447254473544745447554476544775447854479544805448154482544835448454485544865448754488544895449054491544925449354494544955449654497544985449954500545015450254503545045450554506545075450854509545105451154512545135451454515545165451754518545195452054521545225452354524545255452654527545285452954530545315453254533545345453554536545375453854539545405454154542545435454454545545465454754548545495455054551545525455354554545555455654557545585455954560545615456254563545645456554566545675456854569545705457154572545735457454575545765457754578545795458054581545825458354584545855458654587545885458954590545915459254593545945459554596545975459854599546005460154602546035460454605546065460754608546095461054611546125461354614546155461654617546185461954620546215462254623546245462554626546275462854629546305463154632546335463454635546365463754638546395464054641546425464354644546455464654647546485464954650546515465254653546545465554656546575465854659546605466154662546635466454665546665466754668546695467054671546725467354674546755467654677546785467954680546815468254683546845468554686546875468854689546905469154692546935469454695546965469754698546995470054701547025470354704547055470654707547085470954710547115471254713547145471554716547175471854719547205472154722547235472454725547265472754728547295473054731547325473354734547355473654737547385473954740547415474254743547445474554746547475474854749547505475154752547535475454755547565475754758547595476054761547625476354764547655476654767547685476954770547715477254773547745477554776547775477854779547805478154782547835478454785547865478754788547895479054791547925479354794547955479654797547985479954800548015480254803548045480554806548075480854809548105481154812548135481454815548165481754818548195482054821548225482354824548255482654827548285482954830548315483254833548345483554836548375483854839548405484154842548435484454845548465484754848548495485054851548525485354854548555485654857548585485954860548615486254863548645486554866548675486854869548705487154872548735487454875548765487754878548795488054881548825488354884548855488654887548885488954890548915489254893548945489554896548975489854899549005490154902549035490454905549065490754908549095491054911549125491354914549155491654917549185491954920549215492254923549245492554926549275492854929549305493154932549335493454935549365493754938549395494054941549425494354944549455494654947549485494954950549515495254953549545495554956549575495854959549605496154962549635496454965549665496754968549695497054971549725497354974549755497654977549785497954980549815498254983549845498554986549875498854989549905499154992549935499454995549965499754998549995500055001550025500355004550055500655007550085500955010550115501255013550145501555016550175501855019550205502155022550235502455025550265502755028550295503055031550325503355034550355503655037550385503955040550415504255043550445504555046550475504855049550505505155052550535505455055550565505755058550595506055061550625506355064550655506655067550685506955070550715507255073550745507555076550775507855079550805508155082550835508455085550865508755088550895509055091550925509355094550955509655097550985509955100551015510255103551045510555106551075510855109551105511155112551135511455115551165511755118551195512055121551225512355124551255512655127551285512955130551315513255133551345513555136551375513855139551405514155142551435514455145551465514755148551495515055151551525515355154551555515655157551585515955160551615516255163551645516555166551675516855169551705517155172551735517455175551765517755178551795518055181551825518355184551855518655187551885518955190551915519255193551945519555196551975519855199552005520155202552035520455205552065520755208552095521055211552125521355214552155521655217552185521955220552215522255223552245522555226552275522855229552305523155232552335523455235552365523755238552395524055241552425524355244552455524655247552485524955250552515525255253552545525555256552575525855259552605526155262552635526455265552665526755268552695527055271552725527355274552755527655277552785527955280552815528255283552845528555286552875528855289552905529155292552935529455295552965529755298552995530055301553025530355304553055530655307553085530955310553115531255313553145531555316553175531855319553205532155322553235532455325553265532755328553295533055331553325533355334553355533655337553385533955340553415534255343553445534555346553475534855349553505535155352553535535455355553565535755358553595536055361553625536355364553655536655367553685536955370553715537255373553745537555376553775537855379553805538155382553835538455385553865538755388553895539055391553925539355394553955539655397553985539955400554015540255403554045540555406554075540855409554105541155412554135541455415554165541755418554195542055421554225542355424554255542655427554285542955430554315543255433554345543555436554375543855439554405544155442554435544455445554465544755448554495545055451554525545355454554555545655457554585545955460554615546255463554645546555466554675546855469554705547155472554735547455475554765547755478554795548055481554825548355484554855548655487554885548955490554915549255493554945549555496554975549855499555005550155502555035550455505555065550755508555095551055511555125551355514555155551655517555185551955520555215552255523555245552555526555275552855529555305553155532555335553455535555365553755538555395554055541555425554355544555455554655547555485554955550555515555255553555545555555556555575555855559555605556155562555635556455565555665556755568555695557055571555725557355574555755557655577555785557955580555815558255583555845558555586555875558855589555905559155592555935559455595555965559755598555995560055601556025560355604556055560655607556085560955610556115561255613556145561555616556175561855619556205562155622556235562455625556265562755628556295563055631556325563355634556355563655637556385563955640556415564255643556445564555646556475564855649556505565155652556535565455655556565565755658556595566055661556625566355664556655566655667556685566955670556715567255673556745567555676556775567855679556805568155682556835568455685556865568755688556895569055691556925569355694556955569655697556985569955700557015570255703557045570555706557075570855709557105571155712557135571455715557165571755718557195572055721557225572355724557255572655727557285572955730557315573255733557345573555736557375573855739557405574155742557435574455745557465574755748557495575055751557525575355754557555575655757557585575955760557615576255763557645576555766557675576855769557705577155772557735577455775557765577755778557795578055781557825578355784557855578655787557885578955790557915579255793557945579555796557975579855799558005580155802558035580455805558065580755808558095581055811558125581355814558155581655817558185581955820558215582255823558245582555826558275582855829558305583155832558335583455835558365583755838558395584055841558425584355844558455584655847558485584955850558515585255853558545585555856558575585855859558605586155862558635586455865558665586755868558695587055871558725587355874558755587655877558785587955880558815588255883558845588555886558875588855889558905589155892558935589455895558965589755898558995590055901559025590355904559055590655907559085590955910559115591255913559145591555916559175591855919559205592155922559235592455925559265592755928559295593055931559325593355934559355593655937559385593955940559415594255943559445594555946559475594855949559505595155952559535595455955559565595755958559595596055961559625596355964559655596655967559685596955970559715597255973559745597555976559775597855979559805598155982559835598455985559865598755988559895599055991559925599355994559955599655997559985599956000560015600256003560045600556006560075600856009560105601156012560135601456015560165601756018560195602056021560225602356024560255602656027560285602956030560315603256033560345603556036560375603856039560405604156042560435604456045560465604756048560495605056051560525605356054560555605656057560585605956060560615606256063560645606556066560675606856069560705607156072560735607456075560765607756078560795608056081560825608356084560855608656087560885608956090560915609256093560945609556096560975609856099561005610156102561035610456105
  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. PRIVATE_KEY_UNLOCK();
  1643. if ( (ret = hpke_test()) != 0)
  1644. TEST_FAIL("HPKE test failed!\n", ret);
  1645. else
  1646. TEST_PASS("HPKE test passed!\n");
  1647. PRIVATE_KEY_LOCK();
  1648. #endif
  1649. #if defined(WC_SRTP_KDF)
  1650. PRIVATE_KEY_UNLOCK();
  1651. if ( (ret = srtpkdf_test()) != 0)
  1652. TEST_FAIL("SRTP KDF test failed!\n", ret);
  1653. else
  1654. TEST_PASS("SRTP KDF test passed!\n");
  1655. PRIVATE_KEY_LOCK();
  1656. #endif
  1657. #if defined(WC_KDF_NIST_SP_800_56C)
  1658. if ( (ret = nist_sp80056c_kdf_test()) != 0)
  1659. TEST_FAIL("NIST SP 800-56C KDF test failed!\n", ret);
  1660. else
  1661. TEST_PASS("NIST SP 800-56C KDF test passed!\n");
  1662. #endif
  1663. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128) && \
  1664. !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  1665. !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
  1666. if ( (ret = gmac_test()) != 0)
  1667. TEST_FAIL("GMAC test failed!\n", ret);
  1668. else
  1669. TEST_PASS("GMAC test passed!\n");
  1670. #endif
  1671. #ifdef WC_RC2
  1672. if ( (ret = rc2_test()) != 0)
  1673. TEST_FAIL("RC2 test failed!\n", ret);
  1674. else
  1675. TEST_PASS("RC2 test passed!\n");
  1676. #endif
  1677. #ifndef NO_RC4
  1678. if ( (ret = arc4_test()) != 0)
  1679. TEST_FAIL("ARC4 test failed!\n", ret);
  1680. else
  1681. TEST_PASS("ARC4 test passed!\n");
  1682. #endif
  1683. #ifdef HAVE_CHACHA
  1684. if ( (ret = chacha_test()) != 0)
  1685. TEST_FAIL("Chacha test failed!\n", ret);
  1686. else
  1687. TEST_PASS("Chacha test passed!\n");
  1688. #endif
  1689. #ifdef HAVE_XCHACHA
  1690. if ( (ret = XChaCha_test()) != 0)
  1691. TEST_FAIL("XChacha test failed!\n", ret);
  1692. else
  1693. TEST_PASS("XChacha test passed!\n");
  1694. #endif
  1695. #ifdef HAVE_POLY1305
  1696. if ( (ret = poly1305_test()) != 0)
  1697. TEST_FAIL("POLY1305 test failed!\n", ret);
  1698. else
  1699. TEST_PASS("POLY1305 test passed!\n");
  1700. #endif
  1701. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  1702. if ( (ret = chacha20_poly1305_aead_test()) != 0)
  1703. TEST_FAIL("ChaCha20-Poly1305 AEAD test failed!\n", ret);
  1704. else
  1705. TEST_PASS("ChaCha20-Poly1305 AEAD test passed!\n");
  1706. #endif
  1707. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  1708. if ( (ret = XChaCha20Poly1305_test()) != 0)
  1709. TEST_FAIL("XChaCha20-Poly1305 AEAD test failed!\n", ret);
  1710. else
  1711. TEST_PASS("XChaCha20-Poly1305 AEAD test passed!\n");
  1712. #endif
  1713. #ifndef NO_DES3
  1714. if ( (ret = des_test()) != 0)
  1715. TEST_FAIL("DES test failed!\n", ret);
  1716. else
  1717. TEST_PASS("DES test passed!\n");
  1718. #endif
  1719. #ifndef NO_DES3
  1720. if ( (ret = des3_test()) != 0)
  1721. TEST_FAIL("DES3 test failed!\n", ret);
  1722. else
  1723. TEST_PASS("DES3 test passed!\n");
  1724. #endif
  1725. #ifndef NO_AES
  1726. if ( (ret = aes_test()) != 0)
  1727. TEST_FAIL("AES test failed!\n", ret);
  1728. else
  1729. TEST_PASS("AES test passed!\n");
  1730. #if defined(WOLFSSL_AES_192) && \
  1731. !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
  1732. if ( (ret = aes192_test()) != 0)
  1733. TEST_FAIL("AES192 test failed!\n", ret);
  1734. else
  1735. TEST_PASS("AES192 test passed!\n");
  1736. #endif
  1737. #if defined(WOLFSSL_AES_256)
  1738. if ( (ret = aes256_test()) != 0)
  1739. TEST_FAIL("AES256 test failed!\n", ret);
  1740. else
  1741. TEST_PASS("AES256 test passed!\n");
  1742. #endif
  1743. #ifdef WOLFSSL_AES_OFB
  1744. if ( (ret = aesofb_test()) != 0)
  1745. TEST_FAIL("AES-OFB test failed!\n", ret);
  1746. else
  1747. TEST_PASS("AES-OFB test passed!\n");
  1748. #endif
  1749. #ifdef HAVE_AESGCM
  1750. #if !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO)
  1751. if ( (ret = aesgcm_test()) != 0)
  1752. TEST_FAIL("AES-GCM test failed!\n", ret);
  1753. #endif
  1754. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  1755. !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY) && \
  1756. !defined(WOLFSSL_KCAPI_AES) && !(defined(WOLF_CRYPTO_CB) && \
  1757. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  1758. if ((ret = aesgcm_default_test()) != 0) {
  1759. TEST_FAIL("AES-GCM test failed!\n", ret);
  1760. }
  1761. #endif
  1762. if (ret == 0) {
  1763. TEST_PASS("AES-GCM test passed!\n");
  1764. }
  1765. #endif
  1766. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  1767. if ( (ret = aesccm_test()) != 0)
  1768. TEST_FAIL("AES-CCM test failed!\n", ret);
  1769. else
  1770. TEST_PASS("AES-CCM test passed!\n");
  1771. #endif
  1772. #ifdef WOLFSSL_AES_CFB
  1773. if ( (ret = aes_cfb_test()) != 0)
  1774. TEST_FAIL("AES-CFB test failed!\n", ret);
  1775. else
  1776. TEST_PASS("AES-CFB test passed!\n");
  1777. #endif
  1778. #ifdef WOLFSSL_AES_XTS
  1779. if ( (ret = aes_xts_test()) != 0)
  1780. TEST_FAIL("AES-XTS test failed!\n", ret);
  1781. else
  1782. TEST_PASS("AES-XTS test passed!\n");
  1783. #endif
  1784. #ifdef HAVE_AES_KEYWRAP
  1785. if ( (ret = aeskeywrap_test()) != 0)
  1786. TEST_FAIL("AES Key Wrap test failed!\n", ret);
  1787. else
  1788. TEST_PASS("AES Key Wrap test passed!\n");
  1789. #endif
  1790. #ifdef WOLFSSL_AES_SIV
  1791. if ( (ret = aes_siv_test()) != 0)
  1792. TEST_FAIL("AES-SIV test failed!\n", ret);
  1793. else
  1794. TEST_PASS("AES-SIV test passed!\n");
  1795. #endif
  1796. #endif
  1797. #if defined(WOLFSSL_AES_EAX) && \
  1798. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  1799. if ( (ret = aes_eax_test()) != 0)
  1800. TEST_FAIL("AES-EAX test failed!\n", ret);
  1801. else
  1802. TEST_PASS("AES-EAX test passed!\n");
  1803. #endif /* WOLFSSL_AES_EAX */
  1804. #ifdef HAVE_ARIA
  1805. if ( (ret = ariagcm_test(MC_ALGID_ARIA_128BITKEY)) != 0)
  1806. TEST_FAIL("ARIA128 test failed!\n", ret);
  1807. else
  1808. TEST_PASS("ARIA128 test passed!\n");
  1809. if ( (ret = ariagcm_test(MC_ALGID_ARIA_192BITKEY)) != 0)
  1810. TEST_FAIL("ARIA192 test failed!\n", ret);
  1811. else
  1812. TEST_PASS("ARIA192 test passed!\n");
  1813. if ( (ret = ariagcm_test(MC_ALGID_ARIA_256BITKEY)) != 0)
  1814. TEST_FAIL("ARIA256 test failed!\n", ret);
  1815. else
  1816. TEST_PASS("ARIA256 test passed!\n");
  1817. #endif
  1818. #ifdef HAVE_CAMELLIA
  1819. if ( (ret = camellia_test()) != 0)
  1820. TEST_FAIL("CAMELLIA test failed!\n", ret);
  1821. else
  1822. TEST_PASS("CAMELLIA test passed!\n");
  1823. #endif
  1824. #ifdef WOLFSSL_SM4
  1825. if ( (ret = sm4_test()) != 0)
  1826. return err_sys("SM-4 test failed!\n", ret);
  1827. else
  1828. TEST_PASS("SM-4 test passed!\n");
  1829. #endif
  1830. #if !defined(NO_RSA) && !defined(HAVE_RENESAS_SYNC)
  1831. #ifdef WC_RSA_NO_PADDING
  1832. if ( (ret = rsa_no_pad_test()) != 0)
  1833. TEST_FAIL("RSA NOPAD test failed!\n", ret);
  1834. else
  1835. TEST_PASS("RSA NOPAD test passed!\n");
  1836. #endif
  1837. if ( (ret = rsa_test()) != 0)
  1838. TEST_FAIL("RSA test failed!\n", ret);
  1839. else
  1840. TEST_PASS("RSA test passed!\n");
  1841. #endif
  1842. #ifndef NO_DH
  1843. PRIVATE_KEY_UNLOCK();
  1844. if ( (ret = dh_test()) != 0)
  1845. TEST_FAIL("DH test failed!\n", ret);
  1846. else
  1847. TEST_PASS("DH test passed!\n");
  1848. PRIVATE_KEY_LOCK();
  1849. #endif
  1850. #ifndef NO_DSA
  1851. if ( (ret = dsa_test()) != 0)
  1852. TEST_FAIL("DSA test failed!\n", ret);
  1853. else
  1854. TEST_PASS("DSA test passed!\n");
  1855. #endif
  1856. #ifdef WOLFCRYPT_HAVE_SRP
  1857. if ( (ret = srp_test()) != 0)
  1858. TEST_FAIL("SRP test failed!\n", ret);
  1859. else
  1860. TEST_PASS("SRP test passed!\n");
  1861. #endif
  1862. #ifndef NO_PWDBASED
  1863. PRIVATE_KEY_UNLOCK();
  1864. if ( (ret = pwdbased_test()) != 0)
  1865. TEST_FAIL("PWDBASED test failed!\n", ret);
  1866. else
  1867. TEST_PASS("PWDBASED test passed!\n");
  1868. PRIVATE_KEY_LOCK();
  1869. #endif
  1870. #if defined(USE_CERT_BUFFERS_2048) && \
  1871. defined(HAVE_PKCS12) && \
  1872. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_HMAC) && \
  1873. !defined(NO_CERTS) && !defined(NO_DES3)
  1874. if ( (ret = pkcs12_test()) != 0)
  1875. TEST_FAIL("PKCS12 test failed!\n", ret);
  1876. else
  1877. TEST_PASS("PKCS12 test passed!\n");
  1878. #endif
  1879. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  1880. if ( (ret = openssl_test()) != 0)
  1881. TEST_FAIL("OPENSSL test failed!\n", ret);
  1882. else
  1883. TEST_PASS("OPENSSL test passed!\n");
  1884. if ( (ret = openSSL_evpMD_test()) != 0)
  1885. TEST_FAIL("OPENSSL (EVP MD) test failed!\n", ret);
  1886. else
  1887. TEST_PASS("OPENSSL (EVP MD) passed!\n");
  1888. if ( (ret = openssl_pkey0_test()) != 0)
  1889. TEST_FAIL("OPENSSL (PKEY0) test failed!\n", ret);
  1890. else
  1891. TEST_PASS("OPENSSL (PKEY0) passed!\n");
  1892. if ( (ret = openssl_pkey1_test()) != 0)
  1893. TEST_FAIL("OPENSSL (PKEY1) test failed!\n", ret);
  1894. else
  1895. TEST_PASS("OPENSSL (PKEY1) passed!\n");
  1896. #if !defined(WOLF_CRYPTO_CB_ONLY_RSA) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1897. if ( (ret = openssl_evpSig_test()) != 0)
  1898. TEST_FAIL("OPENSSL (EVP Sign/Verify) test failed!\n", ret);
  1899. else
  1900. TEST_PASS("OPENSSL (EVP Sign/Verify) passed!\n");
  1901. #endif
  1902. #endif
  1903. #if defined(HAVE_ECC)
  1904. PRIVATE_KEY_UNLOCK();
  1905. if ( (ret = ecc_test()) != 0)
  1906. TEST_FAIL("ECC test failed!\n", ret);
  1907. else
  1908. TEST_PASS("ECC test passed!\n");
  1909. PRIVATE_KEY_LOCK();
  1910. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  1911. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  1912. if ( (ret = ecc_encrypt_test()) != 0)
  1913. TEST_FAIL("ECC Enc test failed!\n", ret);
  1914. else
  1915. TEST_PASS("ECC Enc test passed!\n");
  1916. #endif
  1917. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  1918. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  1919. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  1920. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  1921. /* skip for ATECC508/608A, cannot import private key buffers */
  1922. if ( (ret = ecc_test_buffers()) != 0)
  1923. TEST_FAIL("ECC buffer test failed!\n", ret);
  1924. else
  1925. TEST_PASS("ECC buffer test passed!\n");
  1926. #endif
  1927. #endif
  1928. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  1929. !defined(NO_FILESYSTEM)
  1930. if ( (ret = cert_test()) != 0)
  1931. TEST_FAIL("CERT test failed!\n", ret);
  1932. else
  1933. TEST_PASS("CERT test passed!\n");
  1934. #endif
  1935. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  1936. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(WOLFSSL_GEN_CERT)
  1937. if ( (ret = certext_test()) != 0)
  1938. TEST_FAIL("CERT EXT test failed!\n", ret);
  1939. else
  1940. TEST_PASS("CERT EXT test passed!\n");
  1941. #endif
  1942. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  1943. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  1944. if ( (ret = decodedCertCache_test()) != 0)
  1945. TEST_FAIL("DECODED CERT CACHE test failed!\n", ret);
  1946. else
  1947. TEST_PASS("DECODED CERT CACHE test passed!\n");
  1948. #endif
  1949. #ifdef HAVE_CURVE25519
  1950. if ( (ret = curve25519_test()) != 0)
  1951. TEST_FAIL("CURVE25519 test failed!\n", ret);
  1952. else
  1953. TEST_PASS("CURVE25519 test passed!\n");
  1954. #endif
  1955. #ifdef HAVE_ED25519
  1956. PRIVATE_KEY_UNLOCK();
  1957. if ( (ret = ed25519_test()) != 0)
  1958. TEST_FAIL("ED25519 test failed!\n", ret);
  1959. else
  1960. TEST_PASS("ED25519 test passed!\n");
  1961. PRIVATE_KEY_LOCK();
  1962. #endif
  1963. #ifdef HAVE_CURVE448
  1964. if ( (ret = curve448_test()) != 0)
  1965. TEST_FAIL("CURVE448 test failed!\n", ret);
  1966. else
  1967. TEST_PASS("CURVE448 test passed!\n");
  1968. #endif
  1969. #ifdef HAVE_ED448
  1970. PRIVATE_KEY_UNLOCK();
  1971. if ( (ret = ed448_test()) != 0)
  1972. TEST_FAIL("ED448 test failed!\n", ret);
  1973. else
  1974. TEST_PASS("ED448 test passed!\n");
  1975. PRIVATE_KEY_LOCK();
  1976. #endif
  1977. #ifdef WOLFSSL_HAVE_KYBER
  1978. if ( (ret = kyber_test()) != 0)
  1979. TEST_FAIL("KYBER test failed!\n", ret);
  1980. else
  1981. TEST_PASS("KYBER test passed!\n");
  1982. #endif
  1983. #ifdef HAVE_DILITHIUM
  1984. if ( (ret = dilithium_test()) != 0)
  1985. TEST_FAIL("DILITHIUM test failed!\n", ret);
  1986. else
  1987. TEST_PASS("DILITHIUM test passed!\n");
  1988. #endif
  1989. #if defined(WOLFSSL_HAVE_XMSS)
  1990. #if !defined(WOLFSSL_SMALL_STACK) && WOLFSSL_XMSS_MIN_HEIGHT <= 10
  1991. if ( (ret = xmss_test_verify_only()) != 0)
  1992. TEST_FAIL("XMSS Vfy test failed!\n", ret);
  1993. else
  1994. TEST_PASS("XMSS Vfy test passed!\n");
  1995. #endif
  1996. #if !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  1997. if ( (ret = xmss_test()) != 0)
  1998. TEST_FAIL("XMSS test failed!\n", ret);
  1999. else
  2000. TEST_PASS("XMSS test passed!\n");
  2001. #endif
  2002. #endif /* if defined(WOLFSSL_HAVE_XMSS) */
  2003. #if defined(WOLFSSL_HAVE_LMS)
  2004. #if !defined(WOLFSSL_SMALL_STACK)
  2005. #if (defined(WOLFSSL_WC_LMS) && (LMS_MAX_HEIGHT >= 10)) || \
  2006. defined(HAVE_LIBLMS)
  2007. if ( (ret = lms_test_verify_only()) != 0)
  2008. TEST_FAIL("LMS Vfy test failed!\n", ret);
  2009. else
  2010. TEST_PASS("LMS Vfy test passed!\n");
  2011. #endif
  2012. #endif
  2013. #if !defined(WOLFSSL_LMS_VERIFY_ONLY)
  2014. if ( (ret = lms_test()) != 0)
  2015. TEST_FAIL("LMS test failed!\n", ret);
  2016. else
  2017. TEST_PASS("LMS test passed!\n");
  2018. #endif
  2019. #endif /* if defined(WOLFSSL_HAVE_LMS) */
  2020. #ifdef WOLFCRYPT_HAVE_ECCSI
  2021. if ( (ret = eccsi_test()) != 0)
  2022. TEST_FAIL("ECCSI test failed!\n", ret);
  2023. else
  2024. TEST_PASS("ECCSI test passed!\n");
  2025. #endif
  2026. #ifdef WOLFCRYPT_HAVE_SAKKE
  2027. if ( (ret = sakke_test()) != 0)
  2028. TEST_FAIL("SAKKE test failed!\n", ret);
  2029. else
  2030. TEST_PASS("SAKKE test passed!\n");
  2031. #endif
  2032. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  2033. if ( (ret = cmac_test()) != 0)
  2034. TEST_FAIL("CMAC test failed!\n", ret);
  2035. else
  2036. TEST_PASS("CMAC test passed!\n");
  2037. #endif
  2038. #if defined(WOLFSSL_SIPHASH)
  2039. if ( (ret = siphash_test()) != 0)
  2040. TEST_FAIL("SipHash test failed!\n", ret);
  2041. else
  2042. TEST_PASS("SipHash test passed!\n");
  2043. #endif
  2044. #ifdef HAVE_LIBZ
  2045. if ( (ret = compress_test()) != 0)
  2046. TEST_FAIL("COMPRESS test failed!\n", ret);
  2047. else
  2048. TEST_PASS("COMPRESS test passed!\n");
  2049. #endif
  2050. #ifdef HAVE_PKCS7
  2051. #ifndef NO_PKCS7_ENCRYPTED_DATA
  2052. if ( (ret = pkcs7encrypted_test()) != 0)
  2053. TEST_FAIL("PKCS7encrypted test failed!\n", ret);
  2054. else
  2055. TEST_PASS("PKCS7encrypted test passed!\n");
  2056. #endif
  2057. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  2058. if ( (ret = pkcs7compressed_test()) != 0)
  2059. TEST_FAIL("PKCS7compressed test failed!\n", ret);
  2060. else
  2061. TEST_PASS("PKCS7compressed test passed!\n");
  2062. #endif
  2063. if ( (ret = pkcs7signed_test()) != 0)
  2064. TEST_FAIL("PKCS7signed test failed!\n", ret);
  2065. else
  2066. TEST_PASS("PKCS7signed test passed!\n");
  2067. if ( (ret = pkcs7enveloped_test()) != 0)
  2068. TEST_FAIL("PKCS7enveloped test failed!\n", ret);
  2069. else
  2070. TEST_PASS("PKCS7enveloped test passed!\n");
  2071. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  2072. if ( (ret = pkcs7authenveloped_test()) != 0)
  2073. TEST_FAIL("PKCS7authenveloped test failed!\n", ret);
  2074. else
  2075. TEST_PASS("PKCS7authenveloped test passed!\n");
  2076. #endif
  2077. #endif
  2078. #if defined(WOLFSSL_PUBLIC_MP) && \
  2079. ((defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  2080. defined(USE_FAST_MATH))
  2081. if ( (ret = mp_test()) != 0)
  2082. TEST_FAIL("mp test failed!\n", ret);
  2083. else
  2084. TEST_PASS("mp test passed!\n");
  2085. #endif
  2086. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  2087. if ( (ret = prime_test()) != 0)
  2088. TEST_FAIL("prime test failed!\n", ret);
  2089. else
  2090. TEST_PASS("prime test passed!\n");
  2091. #endif
  2092. #if defined(ASN_BER_TO_DER) && \
  2093. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  2094. defined(OPENSSL_EXTRA_X509_SMALL))
  2095. if ( (ret = berder_test()) != 0)
  2096. TEST_FAIL("ber-der test failed!\n", ret);
  2097. else
  2098. TEST_PASS("ber-der test passed!\n");
  2099. #endif
  2100. if ( (ret = logging_test()) != 0)
  2101. TEST_FAIL("logging test failed!\n", ret);
  2102. else
  2103. TEST_PASS("logging test passed!\n");
  2104. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  2105. if ( (ret = time_test()) != 0)
  2106. TEST_FAIL("time test failed!\n", ret);
  2107. else
  2108. TEST_PASS("time test passed!\n");
  2109. #endif
  2110. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  2111. if ((ret = wolfcrypt_mutex_test()) != 0)
  2112. #else
  2113. if ((ret = mutex_test()) != 0)
  2114. #endif
  2115. TEST_FAIL("mutex test failed!\n", ret);
  2116. else
  2117. TEST_PASS("mutex test passed!\n");
  2118. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  2119. if ( (ret = memcb_test()) != 0)
  2120. TEST_FAIL("memcb test failed!\n", ret);
  2121. else
  2122. TEST_PASS("memcb test passed!\n");
  2123. #endif
  2124. #ifdef WOLFSSL_CAAM_BLOB
  2125. if ( (ret = blob_test()) != 0)
  2126. TEST_FAIL("blob test failed!\n", ret);
  2127. else
  2128. TEST_PASS("blob test passed!\n");
  2129. #endif
  2130. #if defined(WOLF_CRYPTO_CB) && \
  2131. !(defined(HAVE_INTEL_QAT_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC) || \
  2132. defined(WOLFSSL_QNX_CAAM) || defined(HAVE_RENESAS_SYNC))
  2133. if ( (ret = cryptocb_test()) != 0)
  2134. TEST_FAIL("crypto callback test failed!\n", ret);
  2135. else
  2136. TEST_PASS("crypto callback test passed!\n");
  2137. #endif
  2138. #ifdef WOLFSSL_CERT_PIV
  2139. if ( (ret = certpiv_test()) != 0)
  2140. TEST_FAIL("cert piv test failed!\n", ret);
  2141. else
  2142. TEST_PASS("cert piv test passed!\n");
  2143. #endif
  2144. #ifdef WOLF_CRYPTO_CB
  2145. #ifdef HAVE_INTEL_QA_SYNC
  2146. wc_CryptoCb_CleanupIntelQa(&devId);
  2147. #endif
  2148. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  2149. wc_CryptoCb_CleanupOcteon(&devId);
  2150. #endif
  2151. #endif
  2152. #ifdef WOLFSSL_ASYNC_CRYPT
  2153. wolfAsync_DevClose(&devId);
  2154. #endif
  2155. /* cleanup the thread if fixed point cache is enabled and have thread local */
  2156. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  2157. wc_ecc_fp_free();
  2158. #endif
  2159. #ifdef TEST_ALWAYS_RUN_TO_END
  2160. if (last_failed_test_ret != 0)
  2161. ret = last_failed_test_ret;
  2162. #endif
  2163. if (args)
  2164. ((func_args*)args)->return_code = ret;
  2165. /* If hardware acceleration and respective metrics tracked, show results: */
  2166. #ifdef WOLFSSL_HW_METRICS
  2167. #if defined(WOLFSSL_ESP32_CRYPT_RSA_PRI) && defined(WOLFSSL_HW_METRICS)
  2168. esp_hw_show_mp_metrics();
  2169. #endif
  2170. #endif
  2171. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(OPENSSL_EXTRA)
  2172. wolfSSL_SetGlobalHeapHint(NULL);
  2173. #endif
  2174. TEST_PASS("Test complete\n");
  2175. EXIT_TEST(ret);
  2176. } /* end of wolfcrypt_test() */
  2177. #ifndef NO_MAIN_DRIVER
  2178. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  2179. int myoptind = 0;
  2180. char* myoptarg = NULL;
  2181. #endif
  2182. /* so overall tests can pull in test function */
  2183. #if defined(WOLFSSL_ESPIDF) || defined(_WIN32_WCE)
  2184. wc_test_ret_t wolf_test_task(void)
  2185. #else
  2186. #ifndef NO_MAIN_FUNCTION
  2187. int main(int argc, char** argv)
  2188. {
  2189. return (int)wolfcrypt_test_main(argc, argv);
  2190. }
  2191. #endif
  2192. wc_test_ret_t wolfcrypt_test_main(int argc, char** argv)
  2193. #endif
  2194. {
  2195. wc_test_ret_t ret;
  2196. func_args args = { 0, 0, 0 };
  2197. #if defined(WOLFSSL_ESPIDF) || defined(WOLFSSL_SE050)
  2198. /* set dummy wallclock time. */
  2199. struct timeval utctime;
  2200. struct timezone tz;
  2201. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  2202. utctime.tv_usec = 0;
  2203. tz.tz_minuteswest = 0;
  2204. tz.tz_dsttime = 0;
  2205. settimeofday(&utctime, &tz);
  2206. #endif
  2207. #ifdef WOLFSSL_APACHE_MYNEWT
  2208. #ifdef ARCH_sim
  2209. mcu_sim_parse_args(argc, argv);
  2210. #endif
  2211. sysinit();
  2212. /* set dummy wallclock time. */
  2213. struct os_timeval utctime;
  2214. struct os_timezone tz;
  2215. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  2216. utctime.tv_usec = 0;
  2217. tz.tz_minuteswest = 0;
  2218. tz.tz_dsttime = 0;
  2219. os_settimeofday(&utctime, &tz);
  2220. #endif
  2221. #ifdef WOLFSSL_ZEPHYR
  2222. /* set dummy wallclock time. */
  2223. struct timespec utctime;
  2224. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  2225. utctime.tv_nsec = 0;
  2226. clock_settime(CLOCK_REALTIME, &utctime);
  2227. #endif
  2228. #ifdef DEVKITPRO
  2229. void *framebuffer;
  2230. GXRModeObj *rmode = NULL;
  2231. VIDEO_Init();
  2232. WPAD_Init();
  2233. rmode = VIDEO_GetPreferredMode(NULL);
  2234. #pragma GCC diagnostic ignored "-Wbad-function-cast"
  2235. framebuffer = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
  2236. #pragma GCC diagnostic pop
  2237. console_init(framebuffer,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
  2238. VIDEO_Configure(rmode);
  2239. VIDEO_SetNextFramebuffer(framebuffer);
  2240. VIDEO_SetBlack(FALSE);
  2241. VIDEO_Flush();
  2242. VIDEO_WaitVSync();
  2243. if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
  2244. #endif
  2245. #ifdef HAVE_WNR
  2246. if ((ret = wc_InitNetRandom(wnrConfigFile, NULL, 5000)) != 0) {
  2247. err_sys("Whitewood netRandom global config failed",
  2248. WC_TEST_RET_ENC_EC(ret));
  2249. return WC_TEST_RET_ENC_EC(ret);
  2250. }
  2251. #endif
  2252. #ifndef WOLFSSL_ESPIDF
  2253. args.argc = argc;
  2254. args.argv = argv;
  2255. #endif
  2256. if ((ret = wolfCrypt_Init()) != 0) {
  2257. printf("wolfCrypt_Init failed %d\n", (int)ret);
  2258. err_sys("Error with wolfCrypt_Init!\n", WC_TEST_RET_ENC_EC(ret));
  2259. }
  2260. #ifdef HAVE_WC_INTROSPECTION
  2261. printf("Math: %s\n", wc_GetMathInfo());
  2262. #endif
  2263. #ifdef HAVE_STACK_SIZE
  2264. StackSizeCheck(&args, wolfcrypt_test);
  2265. #else
  2266. wolfcrypt_test(&args);
  2267. #endif
  2268. if ((ret = wolfCrypt_Cleanup()) != 0) {
  2269. printf("wolfCrypt_Cleanup failed %d\n", (int)ret);
  2270. err_sys("Error with wolfCrypt_Cleanup!\n", WC_TEST_RET_ENC_EC(ret));
  2271. }
  2272. #ifdef HAVE_WNR
  2273. if ((ret = wc_FreeNetRandom()) < 0)
  2274. err_sys("Failed to free netRandom context",
  2275. WC_TEST_RET_ENC_EC(ret));
  2276. #endif /* HAVE_WNR */
  2277. #ifdef DOLPHIN_EMULATOR
  2278. /* Returning from main panics the emulator. Just hang
  2279. * and let the user force quit the emulator window. */
  2280. printf("args.return_code: %d\n", args.return_code);
  2281. printf("Testing complete. You may close the window now\n");
  2282. while (1);
  2283. #endif
  2284. #if defined(WOLFSSL_ESPIDF)
  2285. /* ESP_LOGI to print takes up a lot less memory than printf */
  2286. ESP_LOGI(ESPIDF_TAG, "Exiting main with return code: % d\n",
  2287. args.return_code);
  2288. #else
  2289. /* gate this for target platforms wishing to avoid printf reference */
  2290. printf("Exiting main with return code: %ld\n", (long int)args.return_code);
  2291. #endif
  2292. return args.return_code;
  2293. } /* wolfcrypt_test_main or wolf_test_task */
  2294. #endif /* NO_MAIN_DRIVER */
  2295. /* helper to save DER, convert to PEM and save PEM */
  2296. #if !defined(NO_ASN) && (defined(HAVE_ECC) || !defined(NO_DSA) || \
  2297. (!defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)))) \
  2298. && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  2299. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  2300. #define SaveDerAndPem(d, dSz, fD, fP, pT) _SaveDerAndPem(d, dSz, fD, fP, pT, WC_TEST_RET_LN)
  2301. #else
  2302. #define SaveDerAndPem(d, dSz, fD, fP, pT) _SaveDerAndPem(d, dSz, NULL, NULL, pT, WC_TEST_RET_LN)
  2303. #endif
  2304. static wc_test_ret_t _SaveDerAndPem(const byte* der, int derSz,
  2305. const char* fileDer, const char* filePem, int pemType, int calling_line)
  2306. {
  2307. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  2308. wc_test_ret_t ret;
  2309. XFILE derFile;
  2310. derFile = XFOPEN(fileDer, "wb");
  2311. if (!derFile) {
  2312. return WC_TEST_RET_ENC(calling_line, 0, WC_TEST_RET_TAG_I);
  2313. }
  2314. ret = (int)XFWRITE(der, 1, (size_t)derSz, derFile);
  2315. XFCLOSE(derFile);
  2316. if (ret != derSz) {
  2317. return WC_TEST_RET_ENC(calling_line, 1, WC_TEST_RET_TAG_I);
  2318. }
  2319. #endif
  2320. #ifdef WOLFSSL_DER_TO_PEM
  2321. if (filePem) {
  2322. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  2323. XFILE pemFile;
  2324. #endif
  2325. #ifndef WOLFSSL_NO_MALLOC
  2326. byte* pem;
  2327. #else
  2328. byte pem[1024];
  2329. #endif
  2330. int pemSz;
  2331. /* calculate PEM size */
  2332. pemSz = wc_DerToPem(der, (word32)derSz, NULL, 0, pemType);
  2333. if (pemSz < 0) {
  2334. return WC_TEST_RET_ENC(calling_line, 2, WC_TEST_RET_TAG_I);
  2335. }
  2336. #ifndef WOLFSSL_NO_MALLOC
  2337. pem = (byte*)XMALLOC(pemSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2338. if (pem == NULL) {
  2339. return WC_TEST_RET_ENC(calling_line, 3, WC_TEST_RET_TAG_I);
  2340. }
  2341. #else
  2342. if (pemSz > (int)sizeof(pem))
  2343. return BAD_FUNC_ARG;
  2344. #endif
  2345. /* Convert to PEM */
  2346. pemSz = wc_DerToPem(der, (word32)derSz, pem, pemSz, pemType);
  2347. if (pemSz < 0) {
  2348. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2349. return WC_TEST_RET_ENC(calling_line, 4, WC_TEST_RET_TAG_I);
  2350. }
  2351. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  2352. pemFile = XFOPEN(filePem, "wb");
  2353. if (!pemFile) {
  2354. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2355. return WC_TEST_RET_ENC(calling_line, 5, WC_TEST_RET_TAG_I);
  2356. }
  2357. ret = (int)XFWRITE(pem, 1, (size_t)pemSz, pemFile);
  2358. XFCLOSE(pemFile);
  2359. if (ret != pemSz) {
  2360. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2361. return WC_TEST_RET_ENC(calling_line, 6, WC_TEST_RET_TAG_I);
  2362. }
  2363. #endif
  2364. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2365. }
  2366. #endif /* WOLFSSL_DER_TO_PEM */
  2367. /* suppress unused variable warnings */
  2368. (void)der;
  2369. (void)derSz;
  2370. (void)filePem;
  2371. (void)fileDer;
  2372. (void)pemType;
  2373. (void)calling_line;
  2374. return 0;
  2375. }
  2376. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  2377. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t error_test(void)
  2378. {
  2379. const char* errStr;
  2380. char out[WOLFSSL_MAX_ERROR_SZ];
  2381. const char* unknownStr = wc_GetErrorString(0);
  2382. #ifdef NO_ERROR_STRINGS
  2383. /* Ensure a valid error code's string matches an invalid code's.
  2384. * The string is that error strings are not available.
  2385. */
  2386. WOLFSSL_ENTER("error_test NO_ERROR_STRINGS");
  2387. errStr = wc_GetErrorString(OPEN_RAN_E);
  2388. wc_ErrorString(OPEN_RAN_E, out);
  2389. if (XSTRCMP(errStr, unknownStr) != 0)
  2390. return WC_TEST_RET_ENC_NC;
  2391. if (XSTRCMP(out, unknownStr) != 0)
  2392. return WC_TEST_RET_ENC_NC;
  2393. #else
  2394. int i;
  2395. int j = 0;
  2396. /* Values that are not or no longer error codes. */
  2397. int missing[] = { -124, -166, -167, -168, -169, 0 };
  2398. /* Check that all errors have a string and it's the same through the two
  2399. * APIs. Check that the values that are not errors map to the unknown
  2400. * string.
  2401. */
  2402. for (i = MAX_CODE_E-1; i >= WC_LAST_E; i--) {
  2403. errStr = wc_GetErrorString(i);
  2404. wc_ErrorString(i, out);
  2405. if (i != missing[j]) {
  2406. if (XSTRCMP(errStr, unknownStr) == 0) {
  2407. WOLFSSL_MSG("errStr unknown");
  2408. return WC_TEST_RET_ENC_NC;
  2409. }
  2410. if (XSTRCMP(out, unknownStr) == 0) {
  2411. WOLFSSL_MSG("out unknown");
  2412. return WC_TEST_RET_ENC_NC;
  2413. }
  2414. if (XSTRCMP(errStr, out) != 0) {
  2415. WOLFSSL_MSG("errStr does not match output");
  2416. return WC_TEST_RET_ENC_NC;
  2417. }
  2418. if (XSTRLEN(errStr) >= WOLFSSL_MAX_ERROR_SZ) {
  2419. WOLFSSL_MSG("errStr too long");
  2420. return WC_TEST_RET_ENC_NC;
  2421. }
  2422. }
  2423. else {
  2424. j++;
  2425. if (XSTRCMP(errStr, unknownStr) != 0)
  2426. return WC_TEST_RET_ENC_NC;
  2427. if (XSTRCMP(out, unknownStr) != 0)
  2428. return WC_TEST_RET_ENC_NC;
  2429. }
  2430. }
  2431. /* Check if the next possible value has been given a string. */
  2432. errStr = wc_GetErrorString(i);
  2433. wc_ErrorString(i, out);
  2434. if (XSTRCMP(errStr, unknownStr) != 0)
  2435. return WC_TEST_RET_ENC_NC;
  2436. if (XSTRCMP(out, unknownStr) != 0)
  2437. return WC_TEST_RET_ENC_NC;
  2438. #endif
  2439. return 0;
  2440. }
  2441. #ifndef NO_CODING
  2442. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base64_test(void)
  2443. {
  2444. wc_test_ret_t ret;
  2445. WOLFSSL_SMALL_STACK_STATIC const byte good[] = "A+Gd\0\0\0";
  2446. WOLFSSL_SMALL_STACK_STATIC const byte goodEnd[] = "A+Gd \r\n";
  2447. WOLFSSL_SMALL_STACK_STATIC const byte good_spaces[] = " A + G d \0";
  2448. byte out[128];
  2449. word32 outLen;
  2450. #ifdef WOLFSSL_BASE64_ENCODE
  2451. byte data[3];
  2452. word32 dataLen;
  2453. byte longData[79] = { 0 };
  2454. WOLFSSL_SMALL_STACK_STATIC const byte symbols[] = "+/A=";
  2455. #endif
  2456. WOLFSSL_SMALL_STACK_STATIC const byte badSmall[] = "AAA!Gdj=";
  2457. WOLFSSL_SMALL_STACK_STATIC const byte badLarge[] = "AAA~Gdj=";
  2458. WOLFSSL_SMALL_STACK_STATIC const byte badEOL[] = "A+Gd!AA";
  2459. WOLFSSL_SMALL_STACK_STATIC const byte badPadding[] = "AA=A";
  2460. WOLFSSL_SMALL_STACK_STATIC const byte badChar[] = ",-.:;<=>?@[\\]^_`";
  2461. byte goodChar[] =
  2462. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  2463. "abcdefghijklmnopqrstuvwxyz"
  2464. "0123456789+/;";
  2465. byte charTest[] = "A+Gd\0\0\0";
  2466. int i;
  2467. WOLFSSL_ENTER("base64_test");
  2468. /* Good Base64 encodings. */
  2469. outLen = sizeof(out);
  2470. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  2471. if (ret != 0)
  2472. return WC_TEST_RET_ENC_EC(ret);
  2473. outLen = sizeof(out);
  2474. ret = Base64_Decode(goodEnd, sizeof(goodEnd), out, &outLen);
  2475. if (ret != 0)
  2476. return WC_TEST_RET_ENC_EC(ret);
  2477. outLen = sizeof(goodChar);
  2478. ret = Base64_Decode(goodChar, sizeof(goodChar), goodChar, &outLen);
  2479. if (ret != 0)
  2480. return WC_TEST_RET_ENC_EC(ret);
  2481. if (outLen != 64 / 4 * 3)
  2482. return WC_TEST_RET_ENC_NC;
  2483. outLen = sizeof(out);
  2484. ret = Base64_Decode(good_spaces, sizeof(good_spaces), out, &outLen);
  2485. if (ret != 0)
  2486. return WC_TEST_RET_ENC_EC(ret);
  2487. /* Bad parameters. */
  2488. outLen = 1;
  2489. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  2490. if (ret != BAD_FUNC_ARG)
  2491. return WC_TEST_RET_ENC_EC(ret);
  2492. outLen = sizeof(out);
  2493. ret = Base64_Decode(badEOL, sizeof(badEOL), out, &outLen);
  2494. if (ret != ASN_INPUT_E)
  2495. return WC_TEST_RET_ENC_EC(ret);
  2496. outLen = sizeof(out);
  2497. ret = Base64_Decode(badPadding, sizeof(badPadding), out, &outLen);
  2498. if (ret != ASN_INPUT_E)
  2499. return WC_TEST_RET_ENC_EC(ret);
  2500. /* Bad character at each offset 0-3. */
  2501. for (i = 0; i < 4; i++) {
  2502. outLen = sizeof(out);
  2503. ret = Base64_Decode(badSmall + i, 4, out, &outLen);
  2504. if (ret != ASN_INPUT_E)
  2505. return WC_TEST_RET_ENC_I(i);
  2506. ret = Base64_Decode(badLarge + i, 4, out, &outLen);
  2507. if (ret != ASN_INPUT_E)
  2508. return WC_TEST_RET_ENC_I(i);
  2509. }
  2510. /* Invalid character less than 0x2b */
  2511. for (i = 1; i < 0x2b; i++) {
  2512. outLen = sizeof(out);
  2513. charTest[0] = (byte)i;
  2514. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  2515. if (ret != ASN_INPUT_E)
  2516. return WC_TEST_RET_ENC_I(i);
  2517. }
  2518. /* Bad characters in range 0x2b - 0x7a. */
  2519. for (i = 0; i < (int)sizeof(badChar) - 1; i++) {
  2520. outLen = sizeof(out);
  2521. charTest[0] = badChar[i];
  2522. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  2523. if (ret != ASN_INPUT_E)
  2524. return WC_TEST_RET_ENC_I(i);
  2525. }
  2526. /* Invalid character greater than 0x7a */
  2527. for (i = 0x7b; i < 0x100; i++) {
  2528. outLen = sizeof(out);
  2529. charTest[0] = (byte)i;
  2530. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  2531. if (ret != ASN_INPUT_E)
  2532. return WC_TEST_RET_ENC_I(i);
  2533. }
  2534. #ifdef WOLFSSL_BASE64_ENCODE
  2535. /* Decode and encode all symbols - non-alphanumeric. */
  2536. dataLen = sizeof(data);
  2537. ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen);
  2538. if (ret != 0)
  2539. return WC_TEST_RET_ENC_EC(ret);
  2540. outLen = sizeof(out);
  2541. ret = Base64_Encode(data, dataLen, NULL, &outLen);
  2542. if (ret != LENGTH_ONLY_E)
  2543. return WC_TEST_RET_ENC_EC(ret);
  2544. outLen = sizeof(out);
  2545. ret = Base64_Encode(data, dataLen, out, &outLen);
  2546. if (ret != 0)
  2547. return WC_TEST_RET_ENC_EC(ret);
  2548. outLen = 7;
  2549. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  2550. if (ret != BUFFER_E)
  2551. return WC_TEST_RET_ENC_EC(ret);
  2552. outLen = sizeof(out);
  2553. ret = Base64_EncodeEsc(data, dataLen, NULL, &outLen);
  2554. if (ret != LENGTH_ONLY_E)
  2555. return WC_TEST_RET_ENC_EC(ret);
  2556. outLen = sizeof(out);
  2557. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  2558. if (ret != 0)
  2559. return WC_TEST_RET_ENC_EC(ret);
  2560. outLen = sizeof(out);
  2561. ret = Base64_Encode_NoNl(data, dataLen, out, &outLen);
  2562. if (ret != 0)
  2563. return WC_TEST_RET_ENC_EC(ret);
  2564. /* Data that results in an encoding longer than one line. */
  2565. outLen = sizeof(out);
  2566. dataLen = sizeof(longData);
  2567. ret = Base64_Encode(longData, dataLen, out, &outLen);
  2568. if (ret != 0)
  2569. return WC_TEST_RET_ENC_EC(ret);
  2570. outLen = sizeof(out);
  2571. ret = Base64_EncodeEsc(longData, dataLen, out, &outLen);
  2572. if (ret != 0)
  2573. return WC_TEST_RET_ENC_EC(ret);
  2574. outLen = sizeof(out);
  2575. ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen);
  2576. if (ret != 0)
  2577. return WC_TEST_RET_ENC_EC(ret);
  2578. #endif
  2579. return 0;
  2580. }
  2581. #ifdef WOLFSSL_BASE16
  2582. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base16_test(void)
  2583. {
  2584. wc_test_ret_t ret;
  2585. WOLFSSL_SMALL_STACK_STATIC const byte testData[] = "SomeDataToEncode\n";
  2586. WOLFSSL_SMALL_STACK_STATIC const byte encodedTestData[] = "536F6D6544617461546F456E636F64650A00";
  2587. byte encoded[40];
  2588. word32 encodedLen;
  2589. byte plain[40];
  2590. word32 len;
  2591. WOLFSSL_ENTER("base16_test");
  2592. /* length returned includes null termination */
  2593. encodedLen = sizeof(encoded);
  2594. ret = Base16_Encode(testData, sizeof(testData), encoded, &encodedLen);
  2595. if (ret != 0)
  2596. return WC_TEST_RET_ENC_EC(ret);
  2597. len = (word32)XSTRLEN((char*)encoded);
  2598. if (len != encodedLen - 1)
  2599. return WC_TEST_RET_ENC_NC;
  2600. len = sizeof(plain);
  2601. ret = Base16_Decode(encoded, encodedLen - 1, plain, &len);
  2602. if (ret != 0)
  2603. return WC_TEST_RET_ENC_EC(ret);
  2604. if (len != sizeof(testData) || XMEMCMP(testData, plain, len) != 0)
  2605. return WC_TEST_RET_ENC_NC;
  2606. if (encodedLen != sizeof(encodedTestData) ||
  2607. XMEMCMP(encoded, encodedTestData, encodedLen) != 0) {
  2608. return WC_TEST_RET_ENC_NC;
  2609. }
  2610. return 0;
  2611. }
  2612. #endif /* WOLFSSL_BASE16 */
  2613. #endif /* !NO_CODING */
  2614. #ifndef NO_ASN
  2615. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t asn_test(void)
  2616. {
  2617. wc_test_ret_t ret;
  2618. /* ASN1 encoded date buffer */
  2619. WOLFSSL_SMALL_STACK_STATIC const byte dateBuf[] = {
  2620. 0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31,
  2621. 0x32, 0x30, 0x30, 0x37, 0x33, 0x37, 0x5a
  2622. };
  2623. byte format;
  2624. int length;
  2625. const byte* datePart;
  2626. #ifndef NO_ASN_TIME
  2627. struct tm timearg;
  2628. time_t now;
  2629. #endif
  2630. WOLFSSL_ENTER("asn_test");
  2631. ret = wc_GetDateInfo(dateBuf, (int)sizeof(dateBuf), &datePart, &format,
  2632. &length);
  2633. if (ret != 0)
  2634. return WC_TEST_RET_ENC_EC(ret);
  2635. #ifndef NO_ASN_TIME
  2636. /* Parameter Validation tests. */
  2637. if ((ret = wc_GetTime(NULL, sizeof(now))) != BAD_FUNC_ARG)
  2638. return WC_TEST_RET_ENC_EC(ret);
  2639. if ((ret = wc_GetTime(&now, 0)) != BUFFER_E)
  2640. return WC_TEST_RET_ENC_EC(ret);
  2641. now = 0;
  2642. if ((ret = wc_GetTime(&now, sizeof(now))) != 0) {
  2643. return WC_TEST_RET_ENC_EC(ret);
  2644. }
  2645. if (now == 0) {
  2646. printf("RTC/Time not set!\n");
  2647. return WC_TEST_RET_ENC_NC;
  2648. }
  2649. ret = wc_GetDateAsCalendarTime(datePart, length, format, &timearg);
  2650. if (ret != 0)
  2651. return WC_TEST_RET_ENC_EC(ret);
  2652. #endif /* !NO_ASN_TIME */
  2653. return 0;
  2654. }
  2655. #endif /* !NO_ASN */
  2656. #ifdef WOLFSSL_MD2
  2657. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md2_test(void)
  2658. {
  2659. wc_test_ret_t ret = 0;
  2660. Md2 md2;
  2661. byte hash[MD2_DIGEST_SIZE];
  2662. testVector a, b, c, d, e, f, g;
  2663. testVector test_md2[7];
  2664. int times = sizeof(test_md2) / sizeof(testVector), i;
  2665. WOLFSSL_ENTER("md2_test");
  2666. a.input = "";
  2667. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  2668. "\x27\x73";
  2669. a.inLen = XSTRLEN(a.input);
  2670. a.outLen = MD2_DIGEST_SIZE;
  2671. b.input = "a";
  2672. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  2673. "\xb5\xd1";
  2674. b.inLen = XSTRLEN(b.input);
  2675. b.outLen = MD2_DIGEST_SIZE;
  2676. c.input = "abc";
  2677. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  2678. "\xd6\xbb";
  2679. c.inLen = XSTRLEN(c.input);
  2680. c.outLen = MD2_DIGEST_SIZE;
  2681. d.input = "message digest";
  2682. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  2683. "\x06\xb0";
  2684. d.inLen = XSTRLEN(d.input);
  2685. d.outLen = MD2_DIGEST_SIZE;
  2686. e.input = "abcdefghijklmnopqrstuvwxyz";
  2687. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  2688. "\x94\x0b";
  2689. e.inLen = XSTRLEN(e.input);
  2690. e.outLen = MD2_DIGEST_SIZE;
  2691. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2692. "6789";
  2693. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  2694. "\x38\xcd";
  2695. f.inLen = XSTRLEN(f.input);
  2696. f.outLen = MD2_DIGEST_SIZE;
  2697. g.input = "1234567890123456789012345678901234567890123456789012345678"
  2698. "9012345678901234567890";
  2699. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  2700. "\xef\xd8";
  2701. g.inLen = XSTRLEN(g.input);
  2702. g.outLen = MD2_DIGEST_SIZE;
  2703. test_md2[0] = a;
  2704. test_md2[1] = b;
  2705. test_md2[2] = c;
  2706. test_md2[3] = d;
  2707. test_md2[4] = e;
  2708. test_md2[5] = f;
  2709. test_md2[6] = g;
  2710. wc_InitMd2(&md2);
  2711. for (i = 0; i < times; ++i) {
  2712. wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  2713. wc_Md2Final(&md2, hash);
  2714. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  2715. return WC_TEST_RET_ENC_I(i);
  2716. }
  2717. for (i = 0; i < times; ++i) {
  2718. ret = wc_Md2Hash((byte*)test_md2[i].input, (word32)test_md2[i].inLen, hash);
  2719. if (ret != 0) {
  2720. return WC_TEST_RET_ENC_I(i);
  2721. }
  2722. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0) {
  2723. return WC_TEST_RET_ENC_I(i);
  2724. }
  2725. }
  2726. return 0;
  2727. }
  2728. #endif
  2729. #ifndef NO_MD5
  2730. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md5_test(void)
  2731. {
  2732. wc_test_ret_t ret = 0;
  2733. wc_Md5 md5, md5Copy;
  2734. byte hash[WC_MD5_DIGEST_SIZE];
  2735. byte hashcopy[WC_MD5_DIGEST_SIZE];
  2736. testVector a, b, c, d, e, f;
  2737. testVector test_md5[6];
  2738. int times = sizeof(test_md5) / sizeof(testVector), i;
  2739. WOLFSSL_ENTER("md5_test");
  2740. a.input = "";
  2741. a.output = "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42"
  2742. "\x7e";
  2743. a.inLen = XSTRLEN(a.input);
  2744. a.outLen = WC_MD5_DIGEST_SIZE;
  2745. b.input = "abc";
  2746. b.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  2747. "\x72";
  2748. b.inLen = XSTRLEN(b.input);
  2749. b.outLen = WC_MD5_DIGEST_SIZE;
  2750. c.input = "message digest";
  2751. c.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  2752. "\xd0";
  2753. c.inLen = XSTRLEN(c.input);
  2754. c.outLen = WC_MD5_DIGEST_SIZE;
  2755. d.input = "abcdefghijklmnopqrstuvwxyz";
  2756. d.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  2757. "\x3b";
  2758. d.inLen = XSTRLEN(d.input);
  2759. d.outLen = WC_MD5_DIGEST_SIZE;
  2760. e.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2761. "6789";
  2762. e.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  2763. "\x9f";
  2764. e.inLen = XSTRLEN(e.input);
  2765. e.outLen = WC_MD5_DIGEST_SIZE;
  2766. f.input = "1234567890123456789012345678901234567890123456789012345678"
  2767. "9012345678901234567890";
  2768. f.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  2769. "\x7a";
  2770. f.inLen = XSTRLEN(f.input);
  2771. f.outLen = WC_MD5_DIGEST_SIZE;
  2772. test_md5[0] = a;
  2773. test_md5[1] = b;
  2774. test_md5[2] = c;
  2775. test_md5[3] = d;
  2776. test_md5[4] = e;
  2777. test_md5[5] = f;
  2778. ret = wc_InitMd5_ex(&md5, HEAP_HINT, devId);
  2779. if (ret != 0)
  2780. return WC_TEST_RET_ENC_EC(ret);
  2781. ret = wc_InitMd5_ex(&md5Copy, HEAP_HINT, devId);
  2782. if (ret != 0) {
  2783. wc_Md5Free(&md5);
  2784. return WC_TEST_RET_ENC_EC(ret);
  2785. }
  2786. for (i = 0; i < times; ++i) {
  2787. ret = wc_Md5Update(&md5, (byte*)test_md5[i].input,
  2788. (word32)test_md5[i].inLen);
  2789. if (ret != 0)
  2790. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2791. ret = wc_Md5GetHash(&md5, hashcopy);
  2792. if (ret != 0)
  2793. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2794. ret = wc_Md5Copy(&md5, &md5Copy);
  2795. if (ret != 0)
  2796. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2797. ret = wc_Md5Final(&md5, hash);
  2798. if (ret != 0)
  2799. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2800. wc_Md5Free(&md5Copy);
  2801. if (XMEMCMP(hash, test_md5[i].output, WC_MD5_DIGEST_SIZE) != 0)
  2802. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2803. if (XMEMCMP(hash, hashcopy, WC_MD5_DIGEST_SIZE) != 0)
  2804. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2805. }
  2806. #ifndef NO_LARGE_HASH_TEST
  2807. /* BEGIN LARGE HASH TEST */ {
  2808. byte large_input[1024];
  2809. const char* large_digest =
  2810. "\x44\xd0\x88\xce\xf1\x36\xd1\x78\xe9\xc8\xba\x84\xc3\xfd\xf6\xca";
  2811. for (i = 0; i < (int)sizeof(large_input); i++) {
  2812. large_input[i] = (byte)(i & 0xFF);
  2813. }
  2814. times = 100;
  2815. #ifdef WOLFSSL_PIC32MZ_HASH
  2816. wc_Md5SizeSet(&md5, times * sizeof(large_input));
  2817. #endif
  2818. for (i = 0; i < times; ++i) {
  2819. ret = wc_Md5Update(&md5, (byte*)large_input,
  2820. (word32)sizeof(large_input));
  2821. if (ret != 0)
  2822. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2823. }
  2824. ret = wc_Md5Final(&md5, hash);
  2825. if (ret != 0)
  2826. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2827. if (XMEMCMP(hash, large_digest, WC_MD5_DIGEST_SIZE) != 0)
  2828. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  2829. } /* END LARGE HASH TEST */
  2830. #endif /* NO_LARGE_HASH_TEST */
  2831. exit:
  2832. wc_Md5Free(&md5);
  2833. wc_Md5Free(&md5Copy);
  2834. return ret;
  2835. }
  2836. #endif /* NO_MD5 */
  2837. #ifndef NO_MD4
  2838. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md4_test(void)
  2839. {
  2840. Md4 md4;
  2841. byte hash[MD4_DIGEST_SIZE];
  2842. testVector a, b, c, d, e, f, g;
  2843. testVector test_md4[7];
  2844. int times = sizeof(test_md4) / sizeof(testVector), i;
  2845. WOLFSSL_ENTER("md4_test");
  2846. a.input = "";
  2847. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  2848. "\xc0";
  2849. a.inLen = XSTRLEN(a.input);
  2850. a.outLen = MD4_DIGEST_SIZE;
  2851. b.input = "a";
  2852. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  2853. "\x24";
  2854. b.inLen = XSTRLEN(b.input);
  2855. b.outLen = MD4_DIGEST_SIZE;
  2856. c.input = "abc";
  2857. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  2858. "\x9d";
  2859. c.inLen = XSTRLEN(c.input);
  2860. c.outLen = MD4_DIGEST_SIZE;
  2861. d.input = "message digest";
  2862. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  2863. "\x4b";
  2864. d.inLen = XSTRLEN(d.input);
  2865. d.outLen = MD4_DIGEST_SIZE;
  2866. e.input = "abcdefghijklmnopqrstuvwxyz";
  2867. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  2868. "\xa9";
  2869. e.inLen = XSTRLEN(e.input);
  2870. e.outLen = MD4_DIGEST_SIZE;
  2871. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2872. "6789";
  2873. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  2874. "\xe4";
  2875. f.inLen = XSTRLEN(f.input);
  2876. f.outLen = MD4_DIGEST_SIZE;
  2877. g.input = "1234567890123456789012345678901234567890123456789012345678"
  2878. "9012345678901234567890";
  2879. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  2880. "\x36";
  2881. g.inLen = XSTRLEN(g.input);
  2882. g.outLen = MD4_DIGEST_SIZE;
  2883. test_md4[0] = a;
  2884. test_md4[1] = b;
  2885. test_md4[2] = c;
  2886. test_md4[3] = d;
  2887. test_md4[4] = e;
  2888. test_md4[5] = f;
  2889. test_md4[6] = g;
  2890. wc_InitMd4(&md4);
  2891. for (i = 0; i < times; ++i) {
  2892. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  2893. wc_Md4Final(&md4, hash);
  2894. if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  2895. return WC_TEST_RET_ENC_I(i);
  2896. }
  2897. return 0;
  2898. }
  2899. #endif /* NO_MD4 */
  2900. #ifndef NO_SHA
  2901. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha_test(void)
  2902. {
  2903. wc_test_ret_t ret = 0;
  2904. wc_Sha sha, shaCopy;
  2905. byte hash[WC_SHA_DIGEST_SIZE];
  2906. byte hashcopy[WC_SHA_DIGEST_SIZE];
  2907. testVector a, b, c, d, e;
  2908. testVector test_sha[5];
  2909. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2910. WOLFSSL_ENTER("sha_test");
  2911. a.input = "";
  2912. a.output = "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18"
  2913. "\x90\xaf\xd8\x07\x09";
  2914. a.inLen = XSTRLEN(a.input);
  2915. a.outLen = WC_SHA_DIGEST_SIZE;
  2916. b.input = "abc";
  2917. b.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  2918. "\x6C\x9C\xD0\xD8\x9D";
  2919. b.inLen = XSTRLEN(b.input);
  2920. b.outLen = WC_SHA_DIGEST_SIZE;
  2921. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2922. c.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  2923. "\xE5\xE5\x46\x70\xF1";
  2924. c.inLen = XSTRLEN(c.input);
  2925. c.outLen = WC_SHA_DIGEST_SIZE;
  2926. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2927. "aaaaaa";
  2928. d.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  2929. "\x2A\x25\xEC\x64\x4D";
  2930. d.inLen = XSTRLEN(d.input);
  2931. d.outLen = WC_SHA_DIGEST_SIZE;
  2932. e.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2933. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2934. "aaaaaaaaaa";
  2935. e.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  2936. "\x53\x99\x5E\x26\xA0";
  2937. e.inLen = XSTRLEN(e.input);
  2938. e.outLen = WC_SHA_DIGEST_SIZE;
  2939. test_sha[0] = a;
  2940. test_sha[1] = b;
  2941. test_sha[2] = c;
  2942. test_sha[3] = d;
  2943. test_sha[4] = e;
  2944. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  2945. if (ret != 0)
  2946. return WC_TEST_RET_ENC_EC(ret);
  2947. ret = wc_InitSha_ex(&shaCopy, HEAP_HINT, devId);
  2948. if (ret != 0) {
  2949. wc_ShaFree(&sha);
  2950. return WC_TEST_RET_ENC_EC(ret);
  2951. }
  2952. for (i = 0; i < times; ++i) {
  2953. ret = wc_ShaUpdate(&sha, (byte*)test_sha[i].input,
  2954. (word32)test_sha[i].inLen);
  2955. if (ret != 0)
  2956. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2957. ret = wc_ShaGetHash(&sha, hashcopy);
  2958. if (ret != 0)
  2959. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2960. ret = wc_ShaCopy(&sha, &shaCopy);
  2961. if (ret != 0)
  2962. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2963. ret = wc_ShaFinal(&sha, hash);
  2964. if (ret != 0)
  2965. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2966. wc_ShaFree(&shaCopy);
  2967. if (XMEMCMP(hash, test_sha[i].output, WC_SHA_DIGEST_SIZE) != 0)
  2968. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2969. if (XMEMCMP(hash, hashcopy, WC_SHA_DIGEST_SIZE) != 0)
  2970. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2971. }
  2972. #ifndef NO_LARGE_HASH_TEST
  2973. /* BEGIN LARGE HASH TEST */ {
  2974. byte large_input[1024];
  2975. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2976. defined(HASH_SIZE_LIMIT)
  2977. const char* large_digest =
  2978. "\x1d\x6a\x5a\xf6\xe5\x7c\x86\xce\x7f\x7c\xaf\xd5\xdb\x08\xcd\x59"
  2979. "\x15\x8c\x6d\xb6";
  2980. #else
  2981. const char* large_digest =
  2982. "\x8b\x77\x02\x48\x39\xe8\xdb\xd3\x9a\xf4\x05\x24\x66\x12\x2d\x9e"
  2983. "\xc5\xd9\x0a\xac";
  2984. #endif
  2985. for (i = 0; i < (int)sizeof(large_input); i++) {
  2986. large_input[i] = (byte)(i & 0xFF);
  2987. }
  2988. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2989. defined(HASH_SIZE_LIMIT)
  2990. times = 20;
  2991. #else
  2992. times = 100;
  2993. #endif
  2994. #ifdef WOLFSSL_PIC32MZ_HASH
  2995. wc_ShaSizeSet(&sha, times * sizeof(large_input));
  2996. #endif
  2997. for (i = 0; i < times; ++i) {
  2998. ret = wc_ShaUpdate(&sha, (byte*)large_input,
  2999. (word32)sizeof(large_input));
  3000. if (ret != 0)
  3001. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3002. }
  3003. ret = wc_ShaFinal(&sha, hash);
  3004. if (ret != 0)
  3005. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3006. if (XMEMCMP(hash, large_digest, WC_SHA_DIGEST_SIZE) != 0)
  3007. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3008. } /* END LARGE HASH TEST */
  3009. #endif /* NO_LARGE_HASH_TEST */
  3010. exit:
  3011. wc_ShaFree(&sha);
  3012. wc_ShaFree(&shaCopy);
  3013. return ret;
  3014. }
  3015. #endif /* NO_SHA */
  3016. #ifdef WOLFSSL_RIPEMD
  3017. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ripemd_test(void)
  3018. {
  3019. RipeMd ripemd;
  3020. wc_test_ret_t ret;
  3021. byte hash[RIPEMD_DIGEST_SIZE];
  3022. testVector a, b, c, d;
  3023. testVector test_ripemd[4];
  3024. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  3025. WOLFSSL_ENTER("ripemd_test");
  3026. a.input = "abc";
  3027. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  3028. "\xb0\x87\xf1\x5a\x0b\xfc";
  3029. a.inLen = XSTRLEN(a.input);
  3030. a.outLen = RIPEMD_DIGEST_SIZE;
  3031. b.input = "message digest";
  3032. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  3033. "\x5f\xfa\x21\x59\x5f\x36";
  3034. b.inLen = XSTRLEN(b.input);
  3035. b.outLen = RIPEMD_DIGEST_SIZE;
  3036. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3037. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  3038. "\xf4\x9a\xda\x62\xeb\x2b";
  3039. c.inLen = XSTRLEN(c.input);
  3040. c.outLen = RIPEMD_DIGEST_SIZE;
  3041. d.input = "12345678901234567890123456789012345678901234567890123456"
  3042. "789012345678901234567890";
  3043. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  3044. "\x82\xbf\x63\x32\x6b\xfb";
  3045. d.inLen = XSTRLEN(d.input);
  3046. d.outLen = RIPEMD_DIGEST_SIZE;
  3047. test_ripemd[0] = a;
  3048. test_ripemd[1] = b;
  3049. test_ripemd[2] = c;
  3050. test_ripemd[3] = d;
  3051. ret = wc_InitRipeMd(&ripemd);
  3052. if (ret != 0) {
  3053. return WC_TEST_RET_ENC_EC(ret);
  3054. }
  3055. for (i = 0; i < times; ++i) {
  3056. ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  3057. (word32)test_ripemd[i].inLen);
  3058. if (ret != 0) {
  3059. return WC_TEST_RET_ENC_I(i);
  3060. }
  3061. ret = wc_RipeMdFinal(&ripemd, hash);
  3062. if (ret != 0) {
  3063. return WC_TEST_RET_ENC_I(i);
  3064. }
  3065. if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  3066. return WC_TEST_RET_ENC_I(i);
  3067. }
  3068. return 0;
  3069. }
  3070. #endif /* WOLFSSL_RIPEMD */
  3071. #ifdef HAVE_BLAKE2
  3072. #define BLAKE2B_TESTS 3
  3073. static const byte blake2b_vec[BLAKE2B_TESTS][BLAKE2B_OUTBYTES] =
  3074. {
  3075. {
  3076. 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
  3077. 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
  3078. 0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
  3079. 0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
  3080. 0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
  3081. 0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
  3082. 0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
  3083. 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
  3084. },
  3085. {
  3086. 0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
  3087. 0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
  3088. 0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
  3089. 0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
  3090. 0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
  3091. 0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
  3092. 0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
  3093. 0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
  3094. },
  3095. {
  3096. 0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
  3097. 0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
  3098. 0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
  3099. 0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
  3100. 0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
  3101. 0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
  3102. 0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
  3103. 0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
  3104. }
  3105. };
  3106. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2b_test(void)
  3107. {
  3108. Blake2b b2b;
  3109. byte digest[64];
  3110. byte input[64];
  3111. int i, ret;
  3112. WOLFSSL_ENTER("blake2b_test");
  3113. for (i = 0; i < (int)sizeof(input); i++)
  3114. input[i] = (byte)i;
  3115. for (i = 0; i < BLAKE2B_TESTS; i++) {
  3116. ret = wc_InitBlake2b(&b2b, 64);
  3117. if (ret != 0)
  3118. return WC_TEST_RET_ENC_I(i);
  3119. ret = wc_Blake2bUpdate(&b2b, input, (word32)i);
  3120. if (ret != 0)
  3121. return WC_TEST_RET_ENC_I(i);
  3122. ret = wc_Blake2bFinal(&b2b, digest, 64);
  3123. if (ret != 0)
  3124. return WC_TEST_RET_ENC_I(i);
  3125. if (XMEMCMP(digest, blake2b_vec[i], 64) != 0) {
  3126. return WC_TEST_RET_ENC_I(i);
  3127. }
  3128. }
  3129. return 0;
  3130. }
  3131. #endif /* HAVE_BLAKE2 */
  3132. #ifdef HAVE_BLAKE2S
  3133. #define BLAKE2S_TESTS 3
  3134. static const byte blake2s_vec[BLAKE2S_TESTS][BLAKE2S_OUTBYTES] =
  3135. {
  3136. {
  3137. 0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94,
  3138. 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
  3139. 0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e,
  3140. 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
  3141. },
  3142. {
  3143. 0xe3, 0x4d, 0x74, 0xdb, 0xaf, 0x4f, 0xf4, 0xc6,
  3144. 0xab, 0xd8, 0x71, 0xcc, 0x22, 0x04, 0x51, 0xd2,
  3145. 0xea, 0x26, 0x48, 0x84, 0x6c, 0x77, 0x57, 0xfb,
  3146. 0xaa, 0xc8, 0x2f, 0xe5, 0x1a, 0xd6, 0x4b, 0xea,
  3147. },
  3148. {
  3149. 0xdd, 0xad, 0x9a, 0xb1, 0x5d, 0xac, 0x45, 0x49,
  3150. 0xba, 0x42, 0xf4, 0x9d, 0x26, 0x24, 0x96, 0xbe,
  3151. 0xf6, 0xc0, 0xba, 0xe1, 0xdd, 0x34, 0x2a, 0x88,
  3152. 0x08, 0xf8, 0xea, 0x26, 0x7c, 0x6e, 0x21, 0x0c,
  3153. }
  3154. };
  3155. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2s_test(void)
  3156. {
  3157. Blake2s b2s;
  3158. byte digest[32];
  3159. byte input[64];
  3160. int i, ret;
  3161. WOLFSSL_ENTER("blake2s_test");
  3162. for (i = 0; i < (int)sizeof(input); i++)
  3163. input[i] = (byte)i;
  3164. for (i = 0; i < BLAKE2S_TESTS; i++) {
  3165. ret = wc_InitBlake2s(&b2s, 32);
  3166. if (ret != 0)
  3167. return WC_TEST_RET_ENC_I(i);
  3168. ret = wc_Blake2sUpdate(&b2s, input, (word32)i);
  3169. if (ret != 0)
  3170. return WC_TEST_RET_ENC_I(i);
  3171. ret = wc_Blake2sFinal(&b2s, digest, 32);
  3172. if (ret != 0)
  3173. return WC_TEST_RET_ENC_I(i);
  3174. if (XMEMCMP(digest, blake2s_vec[i], 32) != 0) {
  3175. return WC_TEST_RET_ENC_I(i);
  3176. }
  3177. }
  3178. return 0;
  3179. }
  3180. #endif /* HAVE_BLAKE2S */
  3181. #ifdef WOLFSSL_SHA224
  3182. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha224_test(void)
  3183. {
  3184. wc_Sha224 sha, shaCopy;
  3185. byte hash[WC_SHA224_DIGEST_SIZE];
  3186. byte hashcopy[WC_SHA224_DIGEST_SIZE];
  3187. wc_test_ret_t ret = 0;
  3188. testVector a, b, c;
  3189. testVector test_sha[3];
  3190. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3191. WOLFSSL_ENTER("sha224_test");
  3192. a.input = "";
  3193. a.output = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34"
  3194. "\xc4\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
  3195. a.inLen = XSTRLEN(a.input);
  3196. a.outLen = WC_SHA224_DIGEST_SIZE;
  3197. b.input = "abc";
  3198. b.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
  3199. "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  3200. b.inLen = XSTRLEN(b.input);
  3201. b.outLen = WC_SHA224_DIGEST_SIZE;
  3202. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3203. c.output = "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  3204. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  3205. c.inLen = XSTRLEN(c.input);
  3206. c.outLen = WC_SHA224_DIGEST_SIZE;
  3207. test_sha[0] = a;
  3208. test_sha[1] = b;
  3209. test_sha[2] = c;
  3210. ret = wc_InitSha224_ex(&sha, HEAP_HINT, devId);
  3211. if (ret != 0)
  3212. return WC_TEST_RET_ENC_EC(ret);
  3213. ret = wc_InitSha224_ex(&shaCopy, HEAP_HINT, devId);
  3214. if (ret != 0) {
  3215. wc_Sha224Free(&sha);
  3216. return WC_TEST_RET_ENC_EC(ret);
  3217. }
  3218. for (i = 0; i < times; ++i) {
  3219. ret = wc_Sha224Update(&sha, (byte*)test_sha[i].input,
  3220. (word32)test_sha[i].inLen);
  3221. if (ret != 0)
  3222. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3223. ret = wc_Sha224GetHash(&sha, hashcopy);
  3224. if (ret != 0)
  3225. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3226. ret = wc_Sha224Copy(&sha, &shaCopy);
  3227. if (ret != 0)
  3228. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3229. ret = wc_Sha224Final(&sha, hash);
  3230. if (ret != 0)
  3231. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3232. wc_Sha224Free(&shaCopy);
  3233. if (XMEMCMP(hash, test_sha[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  3234. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3235. if (XMEMCMP(hash, hashcopy, WC_SHA224_DIGEST_SIZE) != 0)
  3236. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3237. }
  3238. exit:
  3239. wc_Sha224Free(&sha);
  3240. wc_Sha224Free(&shaCopy);
  3241. return ret;
  3242. }
  3243. #endif
  3244. #ifndef NO_SHA256
  3245. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha256_test(void)
  3246. {
  3247. wc_Sha256 sha, shaCopy;
  3248. byte hash[WC_SHA256_DIGEST_SIZE];
  3249. byte hashcopy[WC_SHA256_DIGEST_SIZE];
  3250. wc_test_ret_t ret = 0;
  3251. testVector a, b, c, d;
  3252. testVector test_sha[4];
  3253. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3254. byte i_hash[WC_SHA256_DIGEST_SIZE];
  3255. byte i_hashcopy[WC_SHA256_DIGEST_SIZE];
  3256. testVector interleave_test_sha[4];
  3257. wc_Sha256 i_sha, i_shaCopy;
  3258. #endif
  3259. #ifndef NO_LARGE_HASH_TEST
  3260. #define LARGE_HASH_TEST_INPUT_SZ 1024
  3261. #ifdef WOLFSSL_SMALL_STACK
  3262. byte *large_input = NULL;
  3263. #else
  3264. byte large_input[LARGE_HASH_TEST_INPUT_SZ];
  3265. #endif
  3266. #endif
  3267. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3268. WOLFSSL_ENTER("sha256_test");
  3269. a.input = "";
  3270. a.output = "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9"
  3271. "\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52"
  3272. "\xb8\x55";
  3273. a.inLen = XSTRLEN(a.input);
  3274. a.outLen = WC_SHA256_DIGEST_SIZE;
  3275. b.input = "abc";
  3276. b.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  3277. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  3278. "\x15\xAD";
  3279. b.inLen = XSTRLEN(b.input);
  3280. b.outLen = WC_SHA256_DIGEST_SIZE;
  3281. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3282. c.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  3283. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  3284. "\x06\xC1";
  3285. c.inLen = XSTRLEN(c.input);
  3286. c.outLen = WC_SHA256_DIGEST_SIZE;
  3287. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  3288. "aaaaaa"; /* this is BLOCKSIZE length */
  3289. d.output = "\xFF\xE0\x54\xFE\x7A\xE0\xCB\x6D\xC6\x5C\x3A\xF9\xB6\x1D\x52"
  3290. "\x09\xF4\x39\x85\x1D\xB4\x3D\x0B\xA5\x99\x73\x37\xDF\x15\x46"
  3291. "\x68\xEB";
  3292. d.inLen = XSTRLEN(d.input);
  3293. d.outLen = WC_SHA256_DIGEST_SIZE;
  3294. test_sha[0] = a;
  3295. test_sha[1] = b;
  3296. test_sha[2] = c;
  3297. test_sha[3] = d;
  3298. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3299. interleave_test_sha[0] = a;
  3300. interleave_test_sha[1] = b;
  3301. interleave_test_sha[2] = c;
  3302. interleave_test_sha[3] = d;
  3303. #endif
  3304. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  3305. if (ret != 0)
  3306. return WC_TEST_RET_ENC_EC(ret);
  3307. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3308. ret = wc_InitSha256_ex(&i_sha, HEAP_HINT, devId);
  3309. if (ret != 0)
  3310. return WC_TEST_RET_ENC_EC(ret);
  3311. #endif
  3312. ret = wc_InitSha256_ex(&shaCopy, HEAP_HINT, devId);
  3313. if (ret != 0) {
  3314. wc_Sha256Free(&sha);
  3315. return WC_TEST_RET_ENC_EC(ret);
  3316. }
  3317. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3318. ret = wc_InitSha256_ex(&i_shaCopy, HEAP_HINT, devId);
  3319. if (ret != 0) {
  3320. wc_Sha256Free(&sha);
  3321. wc_Sha256Free(&i_sha);
  3322. return WC_TEST_RET_ENC_EC(ret);
  3323. }
  3324. #endif
  3325. for (i = 0; i < times; ++i) {
  3326. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,
  3327. (word32)test_sha[i].inLen);
  3328. if (ret != 0) {
  3329. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3330. }
  3331. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3332. ret = wc_Sha256Update(&i_sha, (byte*)interleave_test_sha[i].input,
  3333. (word32)interleave_test_sha[i].inLen);
  3334. if (ret != 0) {
  3335. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3336. }
  3337. #endif
  3338. ret = wc_Sha256GetHash(&sha, hashcopy);
  3339. if (ret != 0)
  3340. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3341. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3342. ret = wc_Sha256GetHash(&i_sha, i_hashcopy);
  3343. if (ret != 0)
  3344. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3345. #endif
  3346. ret = wc_Sha256Copy(&sha, &shaCopy);
  3347. if (ret != 0)
  3348. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3349. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3350. ret = wc_Sha256Copy(&i_sha, &i_shaCopy);
  3351. if (ret != 0)
  3352. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3353. #endif
  3354. ret = wc_Sha256Final(&sha, hash);
  3355. if (ret != 0)
  3356. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3357. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3358. ret = wc_Sha256Final(&i_sha, i_hash);
  3359. if (ret != 0)
  3360. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3361. #endif
  3362. wc_Sha256Free(&shaCopy);
  3363. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3364. wc_Sha256Free(&i_shaCopy);
  3365. #endif
  3366. if (XMEMCMP(hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0) {
  3367. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3368. }
  3369. if (XMEMCMP(hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0) {
  3370. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3371. }
  3372. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3373. if (XMEMCMP(i_hash, interleave_test_sha[i].output,
  3374. WC_SHA256_DIGEST_SIZE) != 0) {
  3375. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3376. }
  3377. if (XMEMCMP(i_hash, i_hashcopy, WC_SHA256_DIGEST_SIZE) != 0) {
  3378. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3379. }
  3380. if (XMEMCMP(i_hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0) {
  3381. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3382. }
  3383. if (XMEMCMP(i_hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0) {
  3384. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3385. }
  3386. #endif
  3387. }
  3388. #ifndef NO_LARGE_HASH_TEST
  3389. /* BEGIN LARGE HASH TEST */ {
  3390. #ifdef HASH_SIZE_LIMIT
  3391. WOLFSSL_SMALL_STACK_STATIC const char* large_digest =
  3392. "\xa4\x75\x9e\x7a\xa2\x03\x38\x32\x88\x66\xa2\xea\x17\xea\xf8\xc7"
  3393. "\xfe\x4e\xc6\xbb\xe3\xbb\x71\xce\xe7\xdf\x7c\x04\x61\xb3\xc2\x2f";
  3394. #else
  3395. WOLFSSL_SMALL_STACK_STATIC const char* large_digest =
  3396. "\x27\x78\x3e\x87\x96\x3a\x4e\xfb\x68\x29\xb5\x31\xc9\xba\x57\xb4"
  3397. "\x4f\x45\x79\x7f\x67\x70\xbd\x63\x7f\xbf\x0d\x80\x7c\xbd\xba\xe0";
  3398. #endif
  3399. #ifdef WOLFSSL_SMALL_STACK
  3400. large_input = (byte *)XMALLOC(LARGE_HASH_TEST_INPUT_SZ, HEAP_HINT,
  3401. DYNAMIC_TYPE_TMP_BUFFER);
  3402. if (large_input == NULL) {
  3403. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), exit);
  3404. }
  3405. #endif
  3406. for (i = 0; i < LARGE_HASH_TEST_INPUT_SZ; i++) {
  3407. large_input[i] = (byte)(i & 0xFF);
  3408. }
  3409. #ifdef HASH_SIZE_LIMIT
  3410. times = 20;
  3411. #else
  3412. times = 100;
  3413. #endif
  3414. #ifdef WOLFSSL_PIC32MZ_HASH
  3415. wc_Sha256SizeSet(&sha, times * LARGE_HASH_TEST_INPUT_SZ);
  3416. #endif
  3417. for (i = 0; i < times; ++i) {
  3418. ret = wc_Sha256Update(&sha, (byte*)large_input,
  3419. LARGE_HASH_TEST_INPUT_SZ);
  3420. if (ret != 0)
  3421. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3422. }
  3423. ret = wc_Sha256Final(&sha, hash);
  3424. if (ret != 0)
  3425. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3426. if (XMEMCMP(hash, large_digest, WC_SHA256_DIGEST_SIZE) != 0)
  3427. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3428. } /* END LARGE HASH TEST */
  3429. #undef LARGE_HASH_TEST_INPUT_SZ
  3430. #endif /* NO_LARGE_HASH_TEST */
  3431. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_FULL_HASH)
  3432. {
  3433. WOLFSSL_SMALL_STACK_STATIC const unsigned char
  3434. data_hb[WC_SHA256_BLOCK_SIZE] = {
  3435. 0x61, 0x62, 0x63, 0x80, 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, 0x00,
  3439. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3440. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3441. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3442. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18
  3443. };
  3444. ret = wc_Sha256HashBlock(&sha, data_hb, hash);
  3445. if (ret != 0) {
  3446. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3447. }
  3448. if (XMEMCMP(hash, b.output, WC_SHA256_DIGEST_SIZE) != 0) {
  3449. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3450. }
  3451. }
  3452. #endif
  3453. exit:
  3454. #if !defined(NO_LARGE_HASH_TEST) && defined(WOLFSSL_SMALL_STACK)
  3455. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3456. #endif
  3457. wc_Sha256Free(&sha);
  3458. wc_Sha256Free(&shaCopy);
  3459. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3460. wc_Sha256Free(&i_sha);
  3461. wc_Sha256Free(&i_shaCopy);
  3462. #endif
  3463. return ret;
  3464. }
  3465. #endif
  3466. #ifdef WOLFSSL_SHA512
  3467. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_test(void)
  3468. {
  3469. /*
  3470. ** See https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512.pdf
  3471. */
  3472. wc_Sha512 sha, shaCopy;
  3473. byte hash[WC_SHA512_DIGEST_SIZE];
  3474. byte hashcopy[WC_SHA512_DIGEST_SIZE];
  3475. wc_test_ret_t ret = 0;
  3476. testVector a, b, c;
  3477. testVector test_sha[3];
  3478. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3479. wc_Sha512 i_sha, i_shaCopy;
  3480. byte i_hash[WC_SHA512_DIGEST_SIZE];
  3481. byte i_hashcopy[WC_SHA512_DIGEST_SIZE];
  3482. testVector interleave_test_sha[3];
  3483. #endif
  3484. #ifndef NO_LARGE_HASH_TEST
  3485. #define LARGE_HASH_TEST_INPUT_SZ 1024
  3486. #ifdef WOLFSSL_SMALL_STACK
  3487. byte *large_input = NULL;
  3488. #else
  3489. byte large_input[LARGE_HASH_TEST_INPUT_SZ];
  3490. #endif
  3491. #endif
  3492. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3493. WOLFSSL_ENTER("sha512_test");
  3494. a.input = "";
  3495. a.output = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80"
  3496. "\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c"
  3497. "\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87"
  3498. "\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a"
  3499. "\xf9\x27\xda\x3e";
  3500. a.inLen = XSTRLEN(a.input);
  3501. a.outLen = WC_SHA512_DIGEST_SIZE;
  3502. b.input = "abc";
  3503. b.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  3504. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  3505. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  3506. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  3507. "\xa5\x4c\xa4\x9f";
  3508. b.inLen = XSTRLEN(b.input);
  3509. b.outLen = WC_SHA512_DIGEST_SIZE;
  3510. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3511. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3512. c.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  3513. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  3514. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  3515. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  3516. "\x87\x4b\xe9\x09";
  3517. c.inLen = XSTRLEN(c.input);
  3518. c.outLen = WC_SHA512_DIGEST_SIZE;
  3519. test_sha[0] = a;
  3520. test_sha[1] = b;
  3521. test_sha[2] = c;
  3522. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3523. interleave_test_sha[0] = a;
  3524. interleave_test_sha[1] = b;
  3525. interleave_test_sha[2] = c;
  3526. #endif
  3527. ret = wc_InitSha512_ex(&sha, HEAP_HINT, devId);
  3528. if (ret != 0)
  3529. return WC_TEST_RET_ENC_EC(ret);
  3530. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3531. ret = wc_InitSha512_ex(&i_sha, HEAP_HINT, devId);
  3532. if (ret != 0)
  3533. return WC_TEST_RET_ENC_EC(ret);
  3534. #endif
  3535. ret = wc_InitSha512_ex(&shaCopy, HEAP_HINT, devId);
  3536. if (ret != 0) {
  3537. wc_Sha512Free(&sha);
  3538. return WC_TEST_RET_ENC_EC(ret);
  3539. }
  3540. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3541. ret = wc_InitSha512_ex(&i_shaCopy, HEAP_HINT, devId);
  3542. if (ret != 0) {
  3543. wc_Sha512Free(&sha);
  3544. wc_Sha512Free(&i_sha);
  3545. return WC_TEST_RET_ENC_EC(ret);
  3546. }
  3547. #endif
  3548. for (i = 0; i < times; ++i) {
  3549. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,
  3550. (word32)test_sha[i].inLen);
  3551. if (ret != 0)
  3552. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3553. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3554. ret = wc_Sha512Update(&i_sha, (byte*)interleave_test_sha[i].input,
  3555. (word32)interleave_test_sha[i].inLen);
  3556. if (ret != 0)
  3557. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3558. #endif
  3559. ret = wc_Sha512GetHash(&sha, hashcopy);
  3560. if (ret != 0)
  3561. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3562. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3563. ret = wc_Sha512GetHash(&i_sha, i_hashcopy);
  3564. if (ret != 0)
  3565. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3566. #endif
  3567. ret = wc_Sha512Copy(&sha, &shaCopy);
  3568. if (ret != 0)
  3569. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3570. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3571. ret = wc_Sha512Copy(&i_sha, &i_shaCopy);
  3572. if (ret != 0)
  3573. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3574. #endif
  3575. ret = wc_Sha512Final(&sha, hash);
  3576. if (ret != 0)
  3577. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3578. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3579. ret = wc_Sha512Final(&i_sha, i_hash);
  3580. if (ret != 0)
  3581. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3582. #endif
  3583. wc_Sha512Free(&shaCopy);
  3584. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3585. wc_Sha512Free(&i_shaCopy);
  3586. #endif
  3587. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  3588. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3589. if (XMEMCMP(hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0)
  3590. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3591. #ifndef NO_WOLFSSL_SHA512_INTERLEAVE
  3592. if (XMEMCMP(i_hash, interleave_test_sha[i].output,
  3593. WC_SHA512_DIGEST_SIZE) != 0) {
  3594. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3595. }
  3596. if (XMEMCMP(i_hash, i_hashcopy, WC_SHA512_DIGEST_SIZE) != 0) {
  3597. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3598. }
  3599. if (XMEMCMP(i_hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0) {
  3600. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3601. }
  3602. if (XMEMCMP(i_hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0) {
  3603. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3604. }
  3605. #endif
  3606. }
  3607. #ifndef NO_LARGE_HASH_TEST
  3608. /* BEGIN LARGE HASH TEST */ {
  3609. #ifdef HASH_SIZE_LIMIT
  3610. WOLFSSL_SMALL_STACK_STATIC const char* large_digest =
  3611. "\x30\x9B\x96\xA6\xE9\x43\x78\x30\xA3\x71\x51\x61\xC1\xEB\xE1\xBE"
  3612. "\xC8\xA5\xF9\x13\x5A\xD6\x6D\x9E\x46\x31\x31\x67\x8D\xE2\xC0\x0B"
  3613. "\x2A\x1A\x03\xE1\xF3\x48\xA7\x33\xBD\x49\xF8\xFF\xF1\xC2\xC2\x95"
  3614. "\xCB\xF0\xAF\x87\x61\x85\x58\x63\x6A\xCA\x70\x9C\x8B\x83\x3F\x5D";
  3615. #else
  3616. WOLFSSL_SMALL_STACK_STATIC const char* large_digest =
  3617. "\x5a\x1f\x73\x90\xbd\x8c\xe4\x63\x54\xce\xa0\x9b\xef\x32\x78\x2d"
  3618. "\x2e\xe7\x0d\x5e\x2f\x9d\x15\x1b\xdd\x2d\xde\x65\x0c\x7b\xfa\x83"
  3619. "\x5e\x80\x02\x13\x84\xb8\x3f\xff\x71\x62\xb5\x09\x89\x63\xe1\xdc"
  3620. "\xa5\xdc\xfc\xfa\x9d\x1a\x4d\xc0\xfa\x3a\x14\xf6\x01\x51\x90\xa4";
  3621. #endif
  3622. #ifdef WOLFSSL_SMALL_STACK
  3623. large_input = (byte *)XMALLOC(LARGE_HASH_TEST_INPUT_SZ, HEAP_HINT,
  3624. DYNAMIC_TYPE_TMP_BUFFER);
  3625. if (large_input == NULL) {
  3626. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), exit);
  3627. }
  3628. #endif
  3629. for (i = 0; i < LARGE_HASH_TEST_INPUT_SZ; i++) {
  3630. large_input[i] = (byte)(i & 0xFF);
  3631. }
  3632. #ifdef HASH_SIZE_LIMIT
  3633. times = 20;
  3634. #else
  3635. times = 100;
  3636. #endif
  3637. for (i = 0; i < times; ++i) {
  3638. ret = wc_Sha512Update(&sha, (byte*)large_input,
  3639. LARGE_HASH_TEST_INPUT_SZ);
  3640. if (ret != 0)
  3641. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3642. }
  3643. ret = wc_Sha512Final(&sha, hash);
  3644. if (ret != 0)
  3645. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3646. if (XMEMCMP(hash, large_digest, WC_SHA512_DIGEST_SIZE) != 0)
  3647. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3648. #ifndef NO_UNALIGNED_MEMORY_TEST
  3649. /* Unaligned memory access test */
  3650. for (i = 1; i < 16; i++) {
  3651. ret = wc_Sha512Update(&sha, (byte*)large_input + i,
  3652. LARGE_HASH_TEST_INPUT_SZ - i);
  3653. if (ret != 0)
  3654. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3655. ret = wc_Sha512Final(&sha, hash);
  3656. }
  3657. #endif
  3658. } /* END LARGE HASH TEST */
  3659. #undef LARGE_HASH_TEST_INPUT_SZ
  3660. #endif /* NO_LARGE_HASH_TEST */
  3661. exit:
  3662. #if !defined(NO_LARGE_HASH_TEST) && defined(WOLFSSL_SMALL_STACK)
  3663. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3664. #endif
  3665. wc_Sha512Free(&sha);
  3666. wc_Sha512Free(&shaCopy);
  3667. #ifndef NO_WOLFSSL_SHA256_INTERLEAVE
  3668. wc_Sha512Free(&i_sha);
  3669. wc_Sha512Free(&i_shaCopy);
  3670. #endif
  3671. return ret;
  3672. }
  3673. #if !defined(WOLFSSL_NOSHA512_224) && \
  3674. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  3675. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_224_test(void)
  3676. {
  3677. /*
  3678. ** See https://csrc.nist.gov/Projects/cryptographic-standards-and-guidelines/example-values
  3679. **
  3680. ** NIST SHA512/224 Document Example:
  3681. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512_224.pdf
  3682. */
  3683. wc_Sha512 sha, shaCopy;
  3684. byte hash[WC_SHA512_224_DIGEST_SIZE];
  3685. byte hashcopy[WC_SHA512_224_DIGEST_SIZE];
  3686. wc_test_ret_t ret = 0;
  3687. testVector a, b, c;
  3688. testVector test_sha[3];
  3689. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3690. WOLFSSL_ENTER("sha512_224_test");
  3691. a.input = "";
  3692. a.output = "\x6e\xd0\xdd\x02"
  3693. "\x80\x6f\xa8\x9e" "\x25\xde\x06\x0c"
  3694. "\x19\xd3\xac\x86" "\xca\xbb\x87\xd6"
  3695. "\xa0\xdd\xd0\x5c" "\x33\x3b\x84\xf4";
  3696. a.inLen = XSTRLEN(a.input);
  3697. a.outLen = WC_SHA512_224_DIGEST_SIZE;
  3698. /*
  3699. ** See page 1 in above document for the SHA512/224 "abc" test:
  3700. */
  3701. b.input = "abc";
  3702. /*
  3703. ** See page 1 in above document for the SHA512/224 "abc" test digest:
  3704. */
  3705. b.output = "\x46\x34\x27\x0f"
  3706. "\x70\x7b\x6a\x54" "\xda\xae\x75\x30"
  3707. "\x46\x08\x42\xe2" "\x0e\x37\xed\x26"
  3708. "\x5c\xee\xe9\xa4" "\x3e\x89\x24\xaa";
  3709. b.inLen = XSTRLEN(b.input);
  3710. b.outLen = WC_SHA512_224_DIGEST_SIZE;
  3711. /*
  3712. ** See page 4 in above for the 2-block test:
  3713. */
  3714. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3715. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3716. /*
  3717. ** see page 9 in above document for the 2-block test message digest:
  3718. */
  3719. c.output = "\x23\xfe\xc5\xbb"
  3720. "\x94\xd6\x0b\x23" "\x30\x81\x92\x64"
  3721. "\x0b\x0c\x45\x33" "\x35\xd6\x64\x73"
  3722. "\x4f\xe4\x0e\x72" "\x68\x67\x4a\xf9";
  3723. c.inLen = XSTRLEN(c.input);
  3724. c.outLen = WC_SHA512_224_DIGEST_SIZE;
  3725. test_sha[0] = a;
  3726. test_sha[1] = b;
  3727. test_sha[2] = c;
  3728. ret = wc_InitSha512_224_ex(&sha, HEAP_HINT, devId);
  3729. if (ret != 0)
  3730. return WC_TEST_RET_ENC_EC(ret);
  3731. ret = wc_InitSha512_224_ex(&shaCopy, HEAP_HINT, devId);
  3732. if (ret != 0) {
  3733. wc_Sha512_224Free(&sha);
  3734. return WC_TEST_RET_ENC_EC(ret);
  3735. }
  3736. for (i = 0; i < times; ++i) {
  3737. ret = wc_Sha512_224Update(&sha, (byte*)test_sha[i].input,
  3738. (word32)test_sha[i].inLen);
  3739. if (ret != 0)
  3740. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3741. ret = wc_Sha512_224GetHash(&sha, hashcopy);
  3742. if (ret != 0)
  3743. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3744. ret = wc_Sha512_224Copy(&sha, &shaCopy);
  3745. if (ret != 0)
  3746. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3747. ret = wc_Sha512_224Final(&sha, hash);
  3748. if (ret != 0)
  3749. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3750. wc_Sha512_224Free(&shaCopy);
  3751. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_224_DIGEST_SIZE) != 0)
  3752. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3753. if (XMEMCMP(hash, hashcopy, WC_SHA512_224_DIGEST_SIZE) != 0)
  3754. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3755. }
  3756. #ifndef NO_LARGE_HASH_TEST
  3757. /* BEGIN LARGE HASH TEST */ {
  3758. byte large_input[1024];
  3759. #ifdef HASH_SIZE_LIMIT
  3760. const char* large_digest =
  3761. "\x98\x68\xc3\xd9\xb9\xef\x17\x53"
  3762. "\x43\x66\x0e\x60\xdf\x29\xf8\xef"
  3763. "\x96\xe3\x93\x34\x8c\x6f\xc0\xeb"
  3764. "\x14\x6c\xcf\x6a";
  3765. #else
  3766. const char* large_digest =
  3767. "\x26\x5f\x98\xd1\x76\x49\x71\x4e"
  3768. "\x82\xb7\x9d\x52\x32\x67\x9d\x56"
  3769. "\x91\xf5\x88\xc3\x05\xbb\x3f\x90"
  3770. "\xe2\x4e\x85\x05";
  3771. #endif
  3772. for (i = 0; i < (int)sizeof(large_input); i++) {
  3773. large_input[i] = (byte)(i & 0xFF);
  3774. }
  3775. #ifdef HASH_SIZE_LIMIT
  3776. times = 20;
  3777. #else
  3778. times = 100;
  3779. #endif
  3780. for (i = 0; i < times; ++i) {
  3781. ret = wc_Sha512_224Update(&sha, (byte*)large_input,
  3782. (word32)sizeof(large_input));
  3783. if (ret != 0)
  3784. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3785. }
  3786. ret = wc_Sha512_224Final(&sha, hash);
  3787. if (ret != 0)
  3788. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3789. if (XMEMCMP(hash, large_digest, WC_SHA512_224_DIGEST_SIZE) != 0)
  3790. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3791. #ifndef NO_UNALIGNED_MEMORY_TEST
  3792. /* Unaligned memory access test */
  3793. for (i = 1; i < 16; i++) {
  3794. ret = wc_Sha512_224Update(&sha, (byte*)large_input + i,
  3795. (word32)sizeof(large_input) - i);
  3796. if (ret != 0)
  3797. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3798. ret = wc_Sha512_224Final(&sha, hash);
  3799. }
  3800. #endif
  3801. } /* END LARGE HASH TEST */
  3802. #endif /* NO_LARGE_HASH_TEST */
  3803. exit:
  3804. wc_Sha512_224Free(&sha);
  3805. wc_Sha512_224Free(&shaCopy);
  3806. return ret;
  3807. } /* sha512_224_test */
  3808. #endif /* !defined(WOLFSSL_NOSHA512_224) && !FIPS ... */
  3809. #if !defined(WOLFSSL_NOSHA512_256) && \
  3810. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  3811. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_256_test(void)
  3812. {
  3813. /*
  3814. ** See https://csrc.nist.gov/Projects/cryptographic-standards-and-guidelines/example-values
  3815. ** NIST SHA512/256 Document Example:
  3816. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512_256.pdf
  3817. */
  3818. wc_Sha512 sha, shaCopy;
  3819. byte hash[WC_SHA512_256_DIGEST_SIZE];
  3820. byte hashcopy[WC_SHA512_256_DIGEST_SIZE];
  3821. wc_test_ret_t ret = 0;
  3822. testVector a, b, c;
  3823. testVector test_sha[3];
  3824. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3825. WOLFSSL_ENTER("sha512_256_test");
  3826. a.input = "";
  3827. a.output = "\xc6\x72\xb8\xd1" "\xef\x56\xed\x28"
  3828. "\xab\x87\xc3\x62" "\x2c\x51\x14\x06"
  3829. "\x9b\xdd\x3a\xd7" "\xb8\xf9\x73\x74"
  3830. "\x98\xd0\xc0\x1e" "\xce\xf0\x96\x7a";
  3831. a.inLen = XSTRLEN(a.input);
  3832. a.outLen = WC_SHA512_256_DIGEST_SIZE;
  3833. /*
  3834. ** See page 1 of above document for "abc" example:
  3835. */
  3836. b.input = "abc";
  3837. /*
  3838. ** See page 4 of above document for "abc" example digest:
  3839. */
  3840. b.output = "\x53\x04\x8e\x26" "\x81\x94\x1e\xf9"
  3841. "\x9b\x2e\x29\xb7" "\x6b\x4c\x7d\xab"
  3842. "\xe4\xc2\xd0\xc6" "\x34\xfc\x6d\x46"
  3843. "\xe0\xe2\xf1\x31" "\x07\xe7\xaf\x23";
  3844. b.inLen = XSTRLEN(b.input);
  3845. b.outLen = WC_SHA512_256_DIGEST_SIZE;
  3846. /*
  3847. ** See page 4 of above document for Two Block Message Sample:
  3848. */
  3849. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3850. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3851. /*
  3852. ** See page 10 of above document for Two Block Message Sample digest:
  3853. */
  3854. c.output = "\x39\x28\xe1\x84" "\xfb\x86\x90\xf8"
  3855. "\x40\xda\x39\x88" "\x12\x1d\x31\xbe"
  3856. "\x65\xcb\x9d\x3e" "\xf8\x3e\xe6\x14"
  3857. "\x6f\xea\xc8\x61" "\xe1\x9b\x56\x3a";
  3858. c.inLen = XSTRLEN(c.input);
  3859. c.outLen = WC_SHA512_256_DIGEST_SIZE;
  3860. test_sha[0] = a;
  3861. test_sha[1] = b;
  3862. test_sha[2] = c;
  3863. ret = wc_InitSha512_256_ex(&sha, HEAP_HINT, devId);
  3864. if (ret != 0)
  3865. return WC_TEST_RET_ENC_EC(ret);
  3866. ret = wc_InitSha512_256_ex(&shaCopy, HEAP_HINT, devId);
  3867. if (ret != 0) {
  3868. wc_Sha512_256Free(&sha);
  3869. return WC_TEST_RET_ENC_EC(ret);
  3870. }
  3871. for (i = 0; i < times; ++i) {
  3872. ret = wc_Sha512_256Update(&sha, (byte*)test_sha[i].input,
  3873. (word32)test_sha[i].inLen);
  3874. if (ret != 0)
  3875. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3876. ret = wc_Sha512_256GetHash(&sha, hashcopy);
  3877. if (ret != 0)
  3878. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3879. ret = wc_Sha512_256Copy(&sha, &shaCopy);
  3880. if (ret != 0)
  3881. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3882. ret = wc_Sha512_256Final(&sha, hash);
  3883. if (ret != 0)
  3884. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3885. wc_Sha512_256Free(&shaCopy);
  3886. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_256_DIGEST_SIZE) != 0)
  3887. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3888. if (XMEMCMP(hash, hashcopy, WC_SHA512_256_DIGEST_SIZE) != 0)
  3889. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3890. }
  3891. #ifndef NO_LARGE_HASH_TEST
  3892. /* BEGIN LARGE HASH TEST */ {
  3893. byte large_input[1024];
  3894. #ifdef HASH_SIZE_LIMIT
  3895. const char* large_digest =
  3896. "\x49\xcc\xbc\x7a\x93\x0b\x02\xb8"
  3897. "\xad\x9a\x46\x51\x00\x1f\x13\x80"
  3898. "\x35\x84\x36\xf1\xf2\x3c\xeb\xd8"
  3899. "\x41\xd4\x06\x8b\x1d\x19\xad\x72";
  3900. #else
  3901. const char* large_digest =
  3902. "\x7a\xe3\x84\x05\xcb\x06\x22\x08"
  3903. "\x7e\x2c\x65\x89\x1f\x26\x45\xfd"
  3904. "\xad\xbc\x2e\x29\x83\x12\x84\x4b"
  3905. "\xf2\xa0\xde\xbe\x06\x11\xd7\x44";
  3906. #endif
  3907. for (i = 0; i < (int)sizeof(large_input); i++) {
  3908. large_input[i] = (byte)(i & 0xFF);
  3909. }
  3910. #ifdef HASH_SIZE_LIMIT
  3911. times = 20;
  3912. #else
  3913. times = 100;
  3914. #endif
  3915. for (i = 0; i < times; ++i) {
  3916. ret = wc_Sha512_256Update(&sha, (byte*)large_input,
  3917. (word32)sizeof(large_input));
  3918. if (ret != 0)
  3919. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3920. }
  3921. ret = wc_Sha512_256Final(&sha, hash);
  3922. if (ret != 0)
  3923. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3924. if (XMEMCMP(hash, large_digest, WC_SHA512_256_DIGEST_SIZE) != 0)
  3925. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3926. #ifndef NO_UNALIGNED_MEMORY_TEST
  3927. /* Unaligned memory access test */
  3928. for (i = 1; i < 16; i++) {
  3929. ret = wc_Sha512_256Update(&sha, (byte*)large_input + i,
  3930. (word32)sizeof(large_input) - i);
  3931. if (ret != 0)
  3932. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3933. ret = wc_Sha512_256Final(&sha, hash);
  3934. }
  3935. #endif
  3936. } /* END LARGE HASH TEST */
  3937. #endif /* NO_LARGE_HASH_TEST */
  3938. exit:
  3939. wc_Sha512_256Free(&sha);
  3940. wc_Sha512_256Free(&shaCopy);
  3941. return ret;
  3942. } /* sha512_256_test */
  3943. #endif /* !defined(WOLFSSL_NOSHA512_256) && !FIPS ... */
  3944. #endif /* WOLFSSL_SHA512 */
  3945. #ifdef WOLFSSL_SHA384
  3946. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha384_test(void)
  3947. {
  3948. wc_Sha384 sha, shaCopy;
  3949. byte hash[WC_SHA384_DIGEST_SIZE];
  3950. byte hashcopy[WC_SHA384_DIGEST_SIZE];
  3951. wc_test_ret_t ret = 0;
  3952. testVector a, b, c;
  3953. testVector test_sha[3];
  3954. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3955. WOLFSSL_ENTER("sha384_test");
  3956. a.input = "";
  3957. a.output = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e\xb1\xb1\xe3"
  3958. "\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c\xc7\xbf\x63\xf6"
  3959. "\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb\xd5\x1a\xd2\xf1\x48"
  3960. "\x98\xb9\x5b";
  3961. a.inLen = XSTRLEN(a.input);
  3962. a.outLen = WC_SHA384_DIGEST_SIZE;
  3963. b.input = "abc";
  3964. b.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  3965. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  3966. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  3967. "\xc8\x25\xa7";
  3968. b.inLen = XSTRLEN(b.input);
  3969. b.outLen = WC_SHA384_DIGEST_SIZE;
  3970. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3971. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3972. c.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  3973. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  3974. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  3975. "\x74\x60\x39";
  3976. c.inLen = XSTRLEN(c.input);
  3977. c.outLen = WC_SHA384_DIGEST_SIZE;
  3978. test_sha[0] = a;
  3979. test_sha[1] = b;
  3980. test_sha[2] = c;
  3981. ret = wc_InitSha384_ex(&sha, HEAP_HINT, devId);
  3982. if (ret != 0)
  3983. return WC_TEST_RET_ENC_EC(ret);
  3984. ret = wc_InitSha384_ex(&shaCopy, HEAP_HINT, devId);
  3985. if (ret != 0) {
  3986. wc_Sha384Free(&sha);
  3987. return WC_TEST_RET_ENC_EC(ret);
  3988. }
  3989. for (i = 0; i < times; ++i) {
  3990. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,
  3991. (word32)test_sha[i].inLen);
  3992. if (ret != 0)
  3993. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3994. ret = wc_Sha384GetHash(&sha, hashcopy);
  3995. if (ret != 0)
  3996. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3997. ret = wc_Sha384Copy(&sha, &shaCopy);
  3998. if (ret != 0)
  3999. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4000. ret = wc_Sha384Final(&sha, hash);
  4001. if (ret != 0)
  4002. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4003. wc_Sha384Free(&shaCopy);
  4004. if (XMEMCMP(hash, test_sha[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  4005. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4006. if (XMEMCMP(hash, hashcopy, WC_SHA384_DIGEST_SIZE) != 0)
  4007. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4008. }
  4009. #ifndef NO_LARGE_HASH_TEST
  4010. /* BEGIN LARGE HASH TEST */ {
  4011. byte large_input[1024];
  4012. #ifdef HASH_SIZE_LIMIT
  4013. const char* large_digest =
  4014. "\xB5\xAD\x66\x6F\xD9\x58\x5E\x68\xDD\x5E\x30\xD3\x95\x72\x33\xA4"
  4015. "\xE9\x4B\x99\x3A\xEF\xF8\xE1\xBF\x1F\x05\x32\xAA\x16\x00\x82\xEC"
  4016. "\x15\xDA\xF2\x75\xEE\xE9\x06\xAF\x52\x8A\x5C\xEF\x72\x81\x80\xD6";
  4017. #else
  4018. const char* large_digest =
  4019. "\x37\x01\xdb\xff\x1e\x40\x4f\xe1\xe2\xea\x0b\x40\xbb\x3b\x39\x9a"
  4020. "\xcc\xe8\x44\x8e\x7e\xe5\x64\xb5\x6b\x7f\x56\x64\xa7\x2b\x84\xe3"
  4021. "\xc5\xd7\x79\x03\x25\x90\xf7\xa4\x58\xcb\x97\xa8\x8b\xb1\xa4\x81";
  4022. #endif
  4023. for (i = 0; i < (int)sizeof(large_input); i++) {
  4024. large_input[i] = (byte)(i & 0xFF);
  4025. }
  4026. #ifdef HASH_SIZE_LIMIT
  4027. times = 20;
  4028. #else
  4029. times = 100;
  4030. #endif
  4031. for (i = 0; i < times; ++i) {
  4032. ret = wc_Sha384Update(&sha, (byte*)large_input,
  4033. (word32)sizeof(large_input));
  4034. if (ret != 0)
  4035. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4036. }
  4037. ret = wc_Sha384Final(&sha, hash);
  4038. if (ret != 0)
  4039. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4040. if (XMEMCMP(hash, large_digest, WC_SHA384_DIGEST_SIZE) != 0)
  4041. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4042. } /* END LARGE HASH TEST */
  4043. #endif /* NO_LARGE_HASH_TEST */
  4044. exit:
  4045. wc_Sha384Free(&sha);
  4046. wc_Sha384Free(&shaCopy);
  4047. return ret;
  4048. }
  4049. #endif /* WOLFSSL_SHA384 */
  4050. #ifdef WOLFSSL_SHA3
  4051. #ifndef WOLFSSL_NOSHA3_224
  4052. static wc_test_ret_t sha3_224_test(void)
  4053. {
  4054. wc_Sha3 sha;
  4055. byte hash[WC_SHA3_224_DIGEST_SIZE];
  4056. byte hashcopy[WC_SHA3_224_DIGEST_SIZE];
  4057. testVector a, b, c;
  4058. testVector test_sha[3];
  4059. wc_test_ret_t ret = 0;
  4060. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4061. a.input = "";
  4062. a.output = "\x6b\x4e\x03\x42\x36\x67\xdb\xb7\x3b\x6e\x15\x45\x4f\x0e\xb1"
  4063. "\xab\xd4\x59\x7f\x9a\x1b\x07\x8e\x3f\x5b\x5a\x6b\xc7";
  4064. a.inLen = XSTRLEN(a.input);
  4065. a.outLen = WC_SHA3_224_DIGEST_SIZE;
  4066. b.input = "abc";
  4067. b.output = "\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76"
  4068. "\x6f\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf";
  4069. b.inLen = XSTRLEN(b.input);
  4070. b.outLen = WC_SHA3_224_DIGEST_SIZE;
  4071. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4072. c.output = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55\x74\x49\x44\x79"
  4073. "\xba\x5c\x7e\x7a\xb7\x6e\xf2\x64\xea\xd0\xfc\xce\x33";
  4074. c.inLen = XSTRLEN(c.input);
  4075. c.outLen = WC_SHA3_224_DIGEST_SIZE;
  4076. test_sha[0] = a;
  4077. test_sha[1] = b;
  4078. test_sha[2] = c;
  4079. ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
  4080. if (ret != 0)
  4081. return WC_TEST_RET_ENC_EC(ret);
  4082. for (i = 0; i < times; ++i) {
  4083. ret = wc_Sha3_224_Update(&sha, (byte*)test_sha[i].input,
  4084. (word32)test_sha[i].inLen);
  4085. if (ret != 0)
  4086. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4087. ret = wc_Sha3_224_GetHash(&sha, hashcopy);
  4088. if (ret != 0)
  4089. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4090. ret = wc_Sha3_224_Final(&sha, hash);
  4091. if (ret != 0)
  4092. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4093. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_224_DIGEST_SIZE) != 0)
  4094. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4095. if (XMEMCMP(hash, hashcopy, WC_SHA3_224_DIGEST_SIZE) != 0)
  4096. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4097. }
  4098. #ifndef NO_LARGE_HASH_TEST
  4099. /* BEGIN LARGE HASH TEST */ {
  4100. byte large_input[1024];
  4101. const char* large_digest =
  4102. "\x13\xe5\xd3\x98\x7b\x94\xda\x41\x12\xc7\x1e\x92\x3a\x19"
  4103. "\x21\x20\x86\x6f\x24\xbf\x0a\x31\xbc\xfd\xd6\x70\x36\xf3";
  4104. for (i = 0; i < (int)sizeof(large_input); i++) {
  4105. large_input[i] = (byte)(i & 0xFF);
  4106. }
  4107. times = 100;
  4108. for (i = 0; i < times; ++i) {
  4109. ret = wc_Sha3_224_Update(&sha, (byte*)large_input,
  4110. (word32)sizeof(large_input));
  4111. if (ret != 0)
  4112. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4113. }
  4114. ret = wc_Sha3_224_Final(&sha, hash);
  4115. if (ret != 0)
  4116. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4117. if (XMEMCMP(hash, large_digest, WC_SHA3_224_DIGEST_SIZE) != 0)
  4118. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4119. } /* END LARGE HASH TEST */
  4120. #endif /* NO_LARGE_HASH_TEST */
  4121. exit:
  4122. wc_Sha3_224_Free(&sha);
  4123. return ret;
  4124. }
  4125. #endif /* WOLFSSL_NOSHA3_224 */
  4126. #ifndef WOLFSSL_NOSHA3_256
  4127. static wc_test_ret_t sha3_256_test(void)
  4128. {
  4129. wc_Sha3 sha;
  4130. byte hash[WC_SHA3_256_DIGEST_SIZE];
  4131. byte hashcopy[WC_SHA3_256_DIGEST_SIZE];
  4132. testVector a, b, c;
  4133. testVector test_sha[3];
  4134. wc_test_ret_t ret = 0;
  4135. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4136. byte large_input[1024];
  4137. const char* large_digest =
  4138. "\xdc\x90\xc0\xb1\x25\xdb\x2c\x34\x81\xa3\xff\xbc\x1e\x2e\x87\xeb"
  4139. "\x6d\x70\x85\x61\xe0\xe9\x63\x61\xff\xe5\x84\x4b\x1f\x68\x05\x15";
  4140. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  4141. /* test vector with hash of empty string */
  4142. const char* Keccak256EmptyOut =
  4143. "\xc5\xd2\x46\x01\x86\xf7\x23\x3c\x92\x7e\x7d\xb2\xdc\xc7\x03\xc0"
  4144. "\xe5\x00\xb6\x53\xca\x82\x27\x3b\x7b\xfa\xd8\x04\x5d\x85\xa4\x70";
  4145. #endif
  4146. /*
  4147. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-256_Msg0.pdf
  4148. */
  4149. a.input = "";
  4150. a.output = "\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6"
  4151. "\x62\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8"
  4152. "\x43\x4a";
  4153. a.inLen = XSTRLEN(a.input);
  4154. a.outLen = WC_SHA3_256_DIGEST_SIZE;
  4155. b.input = "abc";
  4156. b.output = "\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90"
  4157. "\xbd\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43"
  4158. "\x15\x32";
  4159. b.inLen = XSTRLEN(b.input);
  4160. b.outLen = WC_SHA3_256_DIGEST_SIZE;
  4161. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4162. c.output = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8\x23\x5e"
  4163. "\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32\xdd\x97\x49\x6d"
  4164. "\x33\x76";
  4165. c.inLen = XSTRLEN(c.input);
  4166. c.outLen = WC_SHA3_256_DIGEST_SIZE;
  4167. test_sha[0] = a;
  4168. test_sha[1] = b;
  4169. test_sha[2] = c;
  4170. ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
  4171. if (ret != 0)
  4172. return WC_TEST_RET_ENC_EC(ret);
  4173. for (i = 0; i < times; ++i) {
  4174. ret = wc_Sha3_256_Update(&sha, (byte*)test_sha[i].input,
  4175. (word32)test_sha[i].inLen);
  4176. if (ret != 0)
  4177. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4178. ret = wc_Sha3_256_GetHash(&sha, hashcopy);
  4179. if (ret != 0)
  4180. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4181. ret = wc_Sha3_256_Final(&sha, hash);
  4182. if (ret != 0)
  4183. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4184. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_256_DIGEST_SIZE) != 0)
  4185. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4186. if (XMEMCMP(hash, hashcopy, WC_SHA3_256_DIGEST_SIZE) != 0)
  4187. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4188. }
  4189. #ifndef NO_LARGE_HASH_TEST
  4190. /* BEGIN LARGE HASH TEST */ {
  4191. for (i = 0; i < (int)sizeof(large_input); i++) {
  4192. large_input[i] = (byte)(i & 0xFF);
  4193. }
  4194. times = 100;
  4195. for (i = 0; i < times; ++i) {
  4196. ret = wc_Sha3_256_Update(&sha, (byte*)large_input,
  4197. (word32)sizeof(large_input));
  4198. if (ret != 0)
  4199. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4200. }
  4201. ret = wc_Sha3_256_Final(&sha, hash);
  4202. if (ret != 0)
  4203. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4204. if (XMEMCMP(hash, large_digest, WC_SHA3_256_DIGEST_SIZE) != 0)
  4205. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4206. } /* END LARGE HASH TEST */
  4207. #endif /* NO_LARGE_HASH_TEST */
  4208. /* this is a software only variant of SHA3 not supported by external hardware devices */
  4209. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  4210. /* Test for Keccak256 */
  4211. ret = wc_Sha3_SetFlags(&sha, WC_HASH_SHA3_KECCAK256);
  4212. if (ret != 0) {
  4213. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4214. }
  4215. ret = wc_Sha3_256_Update(&sha, (byte*)"", 0);
  4216. if (ret != 0) {
  4217. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4218. }
  4219. ret = wc_Sha3_256_Final(&sha, hash);
  4220. if (ret != 0) {
  4221. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4222. }
  4223. if (XMEMCMP(hash, Keccak256EmptyOut, WC_SHA3_256_DIGEST_SIZE) != 0) {
  4224. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4225. }
  4226. #endif /* WOLFSSL_HASH_FLAGS && !WOLFSSL_ASYNC_CRYPT */
  4227. exit:
  4228. wc_Sha3_256_Free(&sha);
  4229. return ret;
  4230. }
  4231. #endif /* WOLFSSL_NOSHA3_256 */
  4232. #ifndef WOLFSSL_NOSHA3_384
  4233. static wc_test_ret_t sha3_384_test(void)
  4234. {
  4235. wc_Sha3 sha;
  4236. byte hash[WC_SHA3_384_DIGEST_SIZE];
  4237. byte buf[64];
  4238. #ifndef NO_INTM_HASH_TEST
  4239. byte hashcopy[WC_SHA3_384_DIGEST_SIZE];
  4240. #endif
  4241. testVector a, b, c;
  4242. testVector test_sha[3];
  4243. wc_test_ret_t ret;
  4244. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4245. /*
  4246. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-384_Msg0.pdf
  4247. */
  4248. a.input = "";
  4249. a.output = "\x0c\x63\xa7\x5b\x84\x5e\x4f\x7d\x01\x10\x7d\x85\x2e\x4c\x24"
  4250. "\x85\xc5\x1a\x50\xaa\xaa\x94\xfc\x61\x99\x5e\x71\xbb\xee\x98"
  4251. "\x3a\x2a\xc3\x71\x38\x31\x26\x4a\xdb\x47\xfb\x6b\xd1\xe0\x58"
  4252. "\xd5\xf0\x04";
  4253. a.inLen = XSTRLEN(a.input);
  4254. a.outLen = WC_SHA3_384_DIGEST_SIZE;
  4255. #if defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  4256. /* NIST test vector with a length that is a multiple of 4 */
  4257. b.input = "\x7d\x80\xb1\x60\xc4\xb5\x36\xa3\xbe\xb7\x99\x80\x59\x93\x44"
  4258. "\x04\x7c\x5f\x82\xa1\xdf\xc3\xee\xd4";
  4259. b.output = "\x04\x1c\xc5\x86\x1b\xa3\x34\x56\x3c\x61\xd4\xef\x97\x10\xd4"
  4260. "\x89\x6c\x31\x1c\x92\xed\xbe\x0d\x7c\xd5\x3e\x80\x3b\xf2\xf4"
  4261. "\xeb\x60\x57\x23\x55\x70\x77\x0c\xe8\x7c\x55\x20\xd7\xec\x14"
  4262. "\x19\x87\x22";
  4263. b.inLen = XSTRLEN(b.input);
  4264. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  4265. #else
  4266. b.input = "abc";
  4267. b.output = "\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad"
  4268. "\x8d\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b"
  4269. "\xe4\xb2\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28"
  4270. "\x37\x6d\x25";
  4271. b.inLen = XSTRLEN(b.input);
  4272. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  4273. #endif
  4274. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4275. c.output = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7\x8a\x49"
  4276. "\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd\xbc\x32\xb9\xd4"
  4277. "\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe\xa1\x9e\xef\x51\xac\xd0"
  4278. "\x65\x7c\x22";
  4279. c.inLen = XSTRLEN(c.input);
  4280. c.outLen = WC_SHA3_384_DIGEST_SIZE;
  4281. #if defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  4282. test_sha[0] = b; /* hardware acc. pre-Versal can not handle "" string */
  4283. #else
  4284. test_sha[0] = a;
  4285. #endif
  4286. test_sha[1] = b;
  4287. test_sha[2] = c;
  4288. ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
  4289. if (ret != 0)
  4290. return WC_TEST_RET_ENC_EC(ret);
  4291. for (i = 0; i < times; ++i) {
  4292. XMEMCPY(buf, test_sha[i].input, test_sha[i].inLen);
  4293. ret = wc_Sha3_384_Update(&sha, buf,
  4294. (word32)test_sha[i].inLen);
  4295. if (ret != 0)
  4296. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4297. #ifndef NO_INTM_HASH_TEST
  4298. ret = wc_Sha3_384_GetHash(&sha, hashcopy);
  4299. if (ret != 0)
  4300. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4301. #endif
  4302. ret = wc_Sha3_384_Final(&sha, hash);
  4303. if (ret != 0)
  4304. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4305. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_384_DIGEST_SIZE) != 0)
  4306. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4307. #ifndef NO_INTM_HASH_TEST
  4308. if (XMEMCMP(hash, hashcopy, WC_SHA3_384_DIGEST_SIZE) != 0)
  4309. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4310. #endif
  4311. }
  4312. #ifndef NO_LARGE_HASH_TEST
  4313. /* BEGIN LARGE HASH TEST */ {
  4314. byte large_input[1024];
  4315. const char* large_digest =
  4316. "\x30\x44\xec\x17\xef\x47\x9f\x55\x36\x11\xd6\x3f\x8a\x31\x5a\x71"
  4317. "\x8a\x71\xa7\x1d\x8e\x84\xe8\x6c\x24\x02\x2f\x7a\x08\x4e\xea\xd7"
  4318. "\x42\x36\x5d\xa8\xc2\xb7\x42\xad\xec\x19\xfb\xca\xc6\x64\xb3\xa4";
  4319. for (i = 0; i < (int)sizeof(large_input); i++) {
  4320. large_input[i] = (byte)(i & 0xFF);
  4321. }
  4322. times = 100;
  4323. for (i = 0; i < times; ++i) {
  4324. ret = wc_Sha3_384_Update(&sha, (byte*)large_input,
  4325. (word32)sizeof(large_input));
  4326. if (ret != 0)
  4327. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4328. }
  4329. ret = wc_Sha3_384_Final(&sha, hash);
  4330. if (ret != 0)
  4331. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4332. if (XMEMCMP(hash, large_digest, WC_SHA3_384_DIGEST_SIZE) != 0)
  4333. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4334. } /* END LARGE HASH TEST */
  4335. #endif /* NO_LARGE_HASH_TEST */
  4336. exit:
  4337. wc_Sha3_384_Free(&sha);
  4338. return ret;
  4339. }
  4340. #endif /* WOLFSSL_NOSHA3_384 */
  4341. #ifndef WOLFSSL_NOSHA3_512
  4342. static wc_test_ret_t sha3_512_test(void)
  4343. {
  4344. wc_Sha3 sha;
  4345. byte hash[WC_SHA3_512_DIGEST_SIZE];
  4346. byte hashcopy[WC_SHA3_512_DIGEST_SIZE];
  4347. testVector a, b, c;
  4348. testVector test_sha[3];
  4349. wc_test_ret_t ret;
  4350. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4351. /*
  4352. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-512_Msg0.pdf
  4353. */
  4354. a.input = "";
  4355. a.output = "\xa6\x9f\x73\xcc\xa2\x3a\x9a\xc5\xc8\xb5\x67\xdc\x18\x5a\x75"
  4356. "\x6e\x97\xc9\x82\x16\x4f\xe2\x58\x59\xe0\xd1\xdc\xc1\x47\x5c"
  4357. "\x80\xa6\x15\xb2\x12\x3a\xf1\xf5\xf9\x4c\x11\xe3\xe9\x40\x2c"
  4358. "\x3a\xc5\x58\xf5\x00\x19\x9d\x95\xb6\xd3\xe3\x01\x75\x85\x86"
  4359. "\x28\x1d\xcd\x26";
  4360. a.inLen = XSTRLEN(a.input);
  4361. a.outLen = WC_SHA3_512_DIGEST_SIZE;
  4362. b.input = "abc";
  4363. b.output = "\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09"
  4364. "\x6e\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2"
  4365. "\x71\x2e\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47"
  4366. "\xe3\x93\x40\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27"
  4367. "\x4e\xec\x53\xf0";
  4368. b.inLen = XSTRLEN(b.input);
  4369. b.outLen = WC_SHA3_512_DIGEST_SIZE;
  4370. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4371. c.output = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10\xfc\xa8"
  4372. "\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7\xec\x2f\x1e\x91"
  4373. "\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53\x02\xba\x1b\x0d\x8d\xc7"
  4374. "\x8c\x08\x63\x46\xb5\x33\xb4\x9c\x03\x0d\x99\xa2\x7d\xaf\x11"
  4375. "\x39\xd6\xe7\x5e";
  4376. c.inLen = XSTRLEN(c.input);
  4377. c.outLen = WC_SHA3_512_DIGEST_SIZE;
  4378. test_sha[0] = a;
  4379. test_sha[1] = b;
  4380. test_sha[2] = c;
  4381. ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
  4382. if (ret != 0)
  4383. return WC_TEST_RET_ENC_EC(ret);
  4384. for (i = 0; i < times; ++i) {
  4385. ret = wc_Sha3_512_Update(&sha, (byte*)test_sha[i].input,
  4386. (word32)test_sha[i].inLen);
  4387. if (ret != 0)
  4388. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4389. ret = wc_Sha3_512_GetHash(&sha, hashcopy);
  4390. if (ret != 0)
  4391. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4392. ret = wc_Sha3_512_Final(&sha, hash);
  4393. if (ret != 0)
  4394. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4395. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_512_DIGEST_SIZE) != 0)
  4396. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4397. if (XMEMCMP(hash, hashcopy, WC_SHA3_512_DIGEST_SIZE) != 0)
  4398. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4399. }
  4400. #ifndef NO_LARGE_HASH_TEST
  4401. /* BEGIN LARGE HASH TEST */ {
  4402. byte large_input[1024];
  4403. const char* large_digest =
  4404. "\x9c\x13\x26\xb6\x26\xb2\x94\x31\xbc\xf4\x34\xe9\x6f\xf2\xd6\x29"
  4405. "\x9a\xd0\x9b\x32\x63\x2f\x18\xa7\x5f\x23\xc9\x60\xc2\x32\x0c\xbc"
  4406. "\x57\x77\x33\xf1\x83\x81\x8a\xd3\x15\x7c\x93\xdc\x80\x9f\xed\x61"
  4407. "\x41\xa7\x5b\xfd\x32\x0e\x38\x15\xb0\x46\x3b\x7a\x4f\xfd\x44\x88";
  4408. for (i = 0; i < (int)sizeof(large_input); i++) {
  4409. large_input[i] = (byte)(i & 0xFF);
  4410. }
  4411. times = 100;
  4412. for (i = 0; i < times; ++i) {
  4413. ret = wc_Sha3_512_Update(&sha, (byte*)large_input,
  4414. (word32)sizeof(large_input));
  4415. if (ret != 0)
  4416. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4417. }
  4418. ret = wc_Sha3_512_Final(&sha, hash);
  4419. if (ret != 0)
  4420. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4421. if (XMEMCMP(hash, large_digest, WC_SHA3_512_DIGEST_SIZE) != 0)
  4422. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4423. } /* END LARGE HASH TEST */
  4424. #endif /* NO_LARGE_HASH_TEST */
  4425. exit:
  4426. wc_Sha3_512_Free(&sha);
  4427. return ret;
  4428. }
  4429. #endif /* WOLFSSL_NOSHA3_512 */
  4430. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha3_test(void)
  4431. {
  4432. wc_test_ret_t ret;
  4433. (void)ret;
  4434. WOLFSSL_ENTER("sha3_test");
  4435. #ifndef WOLFSSL_NOSHA3_224
  4436. if ((ret = sha3_224_test()) != 0)
  4437. return ret;
  4438. #endif
  4439. #ifndef WOLFSSL_NOSHA3_256
  4440. if ((ret = sha3_256_test()) != 0)
  4441. return ret;
  4442. #endif
  4443. #ifndef WOLFSSL_NOSHA3_384
  4444. if ((ret = sha3_384_test()) != 0)
  4445. return ret;
  4446. #endif
  4447. #ifndef WOLFSSL_NOSHA3_512
  4448. if ((ret = sha3_512_test()) != 0)
  4449. return ret;
  4450. #endif
  4451. return 0;
  4452. }
  4453. #endif /* WOLFSSL_SHA3 */
  4454. #ifdef WOLFSSL_SHAKE128
  4455. static wc_test_ret_t shake128_absorb_test(wc_Shake* sha, byte *large_input_buf,
  4456. size_t large_input_buf_size)
  4457. {
  4458. testVector a, b, c, d, e;
  4459. testVector test_sha[5];
  4460. wc_test_ret_t ret = 0;
  4461. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4462. static const char large_digest[] =
  4463. "\x2b\xd1\x69\x9f\xb3\x75\x40\x74\xb8\xb2\xd2\x0b\x92\x47\x9b\xfe"
  4464. "\xc9\x91\x48\xbe\xda\xa4\x09\xd7\x61\x35\x18\x05\x07\x71\xa5\x61"
  4465. "\x4d\xc4\x94\xad\xbe\x04\x7d\xad\x95\x2f\xeb\x2c\xc0\x10\x67\x43"
  4466. "\x40\xf1\x4a\x58\x1c\x54\xfa\x24\x1c\x1a\x4e\x8d\x9b\xbc\xea\xa7"
  4467. "\x32\xf2\x4c\xc7\x86\x05\x36\xdc\xb4\x42\xd8\x35\xd1\xb4\xa2\x79"
  4468. "\xa2\xe6\xee\x67\x4f\xbf\x2a\x93\x41\x88\x25\x56\x29\x90\x1a\x06"
  4469. "\xba\xfe\x9f\xa6\x1a\x74\xe8\x7e\x85\x4a\xc8\x58\x60\xb1\x7b\x18"
  4470. "\xdf\x77\x59\x46\x04\xc1\xff\x4b\x9b\xcb\xad\xfe\x91\x28\xf0\x01"
  4471. "\xc1\x33\xd0\x99\x99\x2e\x0c\x86\x84\x67\x4d\x37\xa4\x42\x45\x10"
  4472. "\xdc\x8f\xdb\x6f\xa6\x9b\xee\x8a\x60\xa5\x1f\x95\x3f\x8f\xf5\x31"
  4473. "\x4b\x1d\x48\x1e\x45\xff\x79\x5c\xbe\x72\xfc\x56\xed\x6d\x1a\x99"
  4474. "\x7f\x23\x7c\xd1\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b"
  4475. "\xa3\x60\x51\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41"
  4476. "\x77\x2c\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59"
  4477. "\x0d\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  4478. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d\xa8"
  4479. "\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb\x67\x86"
  4480. "\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b\x3d\xca\x95"
  4481. "\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47\xcf\x56\xba\x71"
  4482. "\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a\xff\xb4\xbe\x72\x26"
  4483. "\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e\xd9\xe9\xe6\xf9\xff\xa5";
  4484. byte hash[sizeof(large_digest) - 1];
  4485. /*
  4486. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE128_Msg0.pdf
  4487. */
  4488. a.input = "";
  4489. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  4490. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  4491. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  4492. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  4493. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  4494. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  4495. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  4496. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a\x04\x30\x2e\x10\xc8\xbc"
  4497. "\x1c\xbf\x1a\x0b\x3a\x51\x20\xea\x17\xcd\xa7\xcf\xad\x76\x5f"
  4498. "\x56\x23\x47\x4d\x36\x8c\xcc\xa8\xaf\x00\x07\xcd\x9f\x5e\x4c"
  4499. "\x84\x9f\x16\x7a\x58\x0b\x14\xaa\xbd\xef\xae\xe7\xee\xf4\x7c"
  4500. "\xb0\xfc\xa9";
  4501. a.inLen = XSTRLEN(a.input);
  4502. a.outLen = WC_SHA3_128_BLOCK_SIZE;
  4503. b.input = "abc";
  4504. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  4505. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  4506. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  4507. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  4508. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  4509. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  4510. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  4511. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3\x77\xcf\xb9\xcd\xe4\xc6"
  4512. "\x35\x99\xb2\x7f\x34\x62\xbb\xa4\xa0\xed\x29\x6c\x80\x1f\x9f"
  4513. "\xf7\xf5\x73\x02\xbb\x30\x76\xee\x14\x5f\x97\xa3\x2a\xe6\x8e"
  4514. "\x76\xab\x66\xc4\x8d\x51\x67\x5b\xd4\x9a\xcc\x29\x08\x2f\x56"
  4515. "\x47\x58\x4e";
  4516. b.inLen = XSTRLEN(b.input);
  4517. b.outLen = WC_SHA3_128_BLOCK_SIZE;
  4518. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4519. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  4520. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  4521. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  4522. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  4523. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  4524. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  4525. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  4526. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa\x54\x56\x54\xf7\x0a\xe5"
  4527. "\x86\xff\x10\x13\x14\x20\x77\x14\x83\xec\x92\xed\xab\x40\x8c"
  4528. "\x76\x7b\xf4\xc5\xb4\xff\xfa\xa8\x0c\x8c\xa2\x14\xd8\x4c\x4d"
  4529. "\xc7\x00\xd0\xc5\x06\x30\xb2\xff\xc3\x79\x3e\xa4\xd8\x72\x58"
  4530. "\xb4\xc9\x54";
  4531. c.inLen = XSTRLEN(c.input);
  4532. c.outLen = WC_SHA3_128_BLOCK_SIZE;
  4533. /* Taken from NIST CAVP test vectors - full rate output. */
  4534. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  4535. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  4536. "\x85\xe0";
  4537. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  4538. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  4539. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  4540. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  4541. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  4542. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  4543. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  4544. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  4545. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  4546. "\xa1\x41\x9b\xaf\x60\x52\xc0\xc1\xb4\x45\xf8\x35\x17\x57\xb0"
  4547. "\xd0\x22\x87\x21\x89\xe2\xc0\x27\x3f\x82\xd9\x69\x69\x66\x3e"
  4548. "\x55\x4d\x09";
  4549. d.inLen = 32;
  4550. d.outLen = WC_SHA3_128_BLOCK_SIZE;
  4551. /* Taken from NIST CAVP test vectors - more than one output block. */
  4552. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  4553. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  4554. "\x47\xe4";
  4555. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  4556. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  4557. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  4558. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  4559. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  4560. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  4561. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  4562. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  4563. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  4564. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  4565. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  4566. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  4567. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  4568. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  4569. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  4570. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  4571. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d"
  4572. "\xa8\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb"
  4573. "\x67\x86\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b"
  4574. "\x3d\xca\x95\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47"
  4575. "\xcf\x56\xba\x71\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a"
  4576. "\xff\xb4\xbe\x72\x26\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e"
  4577. "\xd9\xe9\xe6\xf9\xff\xa5";
  4578. e.inLen = 32;
  4579. e.outLen = 2 * WC_SHA3_128_BLOCK_SIZE;
  4580. test_sha[0] = a;
  4581. test_sha[1] = b;
  4582. test_sha[2] = c;
  4583. test_sha[3] = d;
  4584. test_sha[4] = e;
  4585. for (i = 0; i < times; ++i) {
  4586. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  4587. if (ret != 0)
  4588. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4589. ret = wc_Shake128_Absorb(sha, (byte*)test_sha[i].input,
  4590. (word32)test_sha[i].inLen);
  4591. if (ret != 0)
  4592. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4593. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  4594. (word32)test_sha[i].outLen / WC_SHA3_128_BLOCK_SIZE);
  4595. if (ret != 0)
  4596. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4597. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  4598. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4599. }
  4600. #ifndef NO_LARGE_HASH_TEST
  4601. /* BEGIN LARGE HASH TEST */ {
  4602. for (i = 0; i < (int)large_input_buf_size; i++) {
  4603. large_input_buf[i] = (byte)(i & 0xFF);
  4604. }
  4605. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  4606. if (ret != 0)
  4607. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4608. /* Absorb is non-incremental. */
  4609. ret = wc_Shake128_Absorb(sha, (byte*)large_input_buf,
  4610. (word32)large_input_buf_size);
  4611. if (ret != 0)
  4612. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4613. /* Able to squeeze out blocks incrementally. */
  4614. ret = wc_Shake128_SqueezeBlocks(sha, hash, 1);
  4615. if (ret != 0)
  4616. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4617. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  4618. ((word32)sizeof(hash) / WC_SHA3_128_BLOCK_SIZE) - 1);
  4619. if (ret != 0)
  4620. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4621. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  4622. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4623. } /* END LARGE HASH TEST */
  4624. #endif /* NO_LARGE_HASH_TEST */
  4625. exit:
  4626. return ret;
  4627. }
  4628. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake128_test(void)
  4629. {
  4630. wc_Shake sha;
  4631. byte hash[250];
  4632. testVector a, b, c, d, e;
  4633. testVector test_sha[5];
  4634. wc_test_ret_t ret = 0;
  4635. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4636. #define SHAKE128_LARGE_INPUT_BUFSIZ 1024
  4637. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4638. byte *large_input = NULL;
  4639. #else
  4640. byte large_input[SHAKE128_LARGE_INPUT_BUFSIZ];
  4641. #endif
  4642. static const char large_digest[] =
  4643. "\x88\xd7\x0e\x86\x46\x72\x6b\x3d\x7d\x22\xe1\xa9\x2d\x02\xdb\x35"
  4644. "\x92\x4f\x1b\x03\x90\xee\xa3\xce\xd1\x3a\x08\x3a\xd7\x4e\x10\xdf"
  4645. "\x09\x67\x33\x35\x4f\xdd\x38\x50\x5b\xcb\x75\xc7\xba\x65\xe5\xe8"
  4646. "\xb8\x76\xde\xc5\xee\xd7\xf1\x65\x93\x4e\x5e\xc4\xb1\xd7\x6b\xee"
  4647. "\x4b\x57\x48\xf5\x38\x49\x9e\x45\xa0\xf7\x32\xe9\x05\x26\x6a\x10"
  4648. "\x70\xd4\x7c\x19\x01\x1f\x6d\x37\xba\x7b\x74\xc2\xbc\xb6\xbc\x74"
  4649. "\xa3\x66\x6c\x9b\x11\x84\x9d\x4a\x36\xbc\x8a\x0d\x4c\xe3\x39\xfa"
  4650. "\xfa\x1b";
  4651. WOLFSSL_ENTER("shake128_test");
  4652. /*
  4653. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE128_Msg0.pdf
  4654. */
  4655. a.input = "";
  4656. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  4657. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  4658. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  4659. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  4660. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  4661. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  4662. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  4663. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a";
  4664. a.inLen = XSTRLEN(a.input);
  4665. a.outLen = 114;
  4666. b.input = "abc";
  4667. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  4668. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  4669. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  4670. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  4671. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  4672. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  4673. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  4674. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3";
  4675. b.inLen = XSTRLEN(b.input);
  4676. b.outLen = 114;
  4677. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4678. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  4679. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  4680. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  4681. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  4682. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  4683. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  4684. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  4685. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa";
  4686. c.inLen = XSTRLEN(c.input);
  4687. c.outLen = 114;
  4688. /* Taken from NIST CAVP test vectors - full rate output. */
  4689. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  4690. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  4691. "\x85\xe0";
  4692. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  4693. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  4694. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  4695. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  4696. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  4697. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  4698. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  4699. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  4700. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  4701. "\xa1";
  4702. d.inLen = 32;
  4703. d.outLen = 136;
  4704. /* Taken from NIST CAVP test vectors - more than one output block. */
  4705. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  4706. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  4707. "\x47\xe4";
  4708. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  4709. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  4710. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  4711. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  4712. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  4713. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  4714. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  4715. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  4716. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  4717. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  4718. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  4719. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  4720. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  4721. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  4722. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  4723. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  4724. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4";
  4725. e.inLen = 32;
  4726. e.outLen = 250;
  4727. test_sha[0] = a;
  4728. test_sha[1] = b;
  4729. test_sha[2] = c;
  4730. test_sha[3] = d;
  4731. test_sha[4] = e;
  4732. ret = wc_InitShake128(&sha, HEAP_HINT, devId);
  4733. if (ret != 0)
  4734. return WC_TEST_RET_ENC_EC(ret);
  4735. for (i = 0; i < times; ++i) {
  4736. ret = wc_Shake128_Update(&sha, (byte*)test_sha[i].input,
  4737. (word32)test_sha[i].inLen);
  4738. if (ret != 0)
  4739. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4740. ret = wc_Shake128_Final(&sha, hash, (word32)test_sha[i].outLen);
  4741. if (ret != 0)
  4742. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4743. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  4744. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4745. }
  4746. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4747. large_input = (byte *)XMALLOC(SHAKE128_LARGE_INPUT_BUFSIZ, NULL,
  4748. DYNAMIC_TYPE_TMP_BUFFER);
  4749. if (large_input == NULL)
  4750. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  4751. #endif
  4752. #ifndef NO_LARGE_HASH_TEST
  4753. /* BEGIN LARGE HASH TEST */ {
  4754. for (i = 0; i < SHAKE128_LARGE_INPUT_BUFSIZ; i++) {
  4755. large_input[i] = (byte)(i & 0xFF);
  4756. }
  4757. times = 100;
  4758. for (i = 0; i < times; ++i) {
  4759. ret = wc_Shake128_Update(&sha, (byte*)large_input,
  4760. SHAKE128_LARGE_INPUT_BUFSIZ);
  4761. if (ret != 0)
  4762. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4763. }
  4764. ret = wc_Shake128_Final(&sha, hash, (word32)sizeof(hash));
  4765. if (ret != 0)
  4766. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4767. if (XMEMCMP(hash, large_digest, sizeof(large_digest) - 1) != 0)
  4768. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4769. } /* END LARGE HASH TEST */
  4770. #endif /* NO_LARGE_HASH_TEST */
  4771. ret = shake128_absorb_test(&sha, large_input, SHAKE128_LARGE_INPUT_BUFSIZ);
  4772. exit:
  4773. wc_Shake128_Free(&sha);
  4774. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4775. if (large_input != NULL)
  4776. XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4777. #endif
  4778. return ret;
  4779. }
  4780. #endif
  4781. #ifdef WOLFSSL_SHAKE256
  4782. static wc_test_ret_t shake256_absorb_test(wc_Shake* sha, byte *large_input_buf,
  4783. size_t large_input_buf_size)
  4784. {
  4785. testVector a, b, c, d, e;
  4786. testVector test_sha[5];
  4787. wc_test_ret_t ret = 0;
  4788. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4789. static const char large_digest[] =
  4790. "\x21\x25\x8e\xae\x6e\x4f\xa7\xe1\xb9\x6d\xa7\xc9\x7d\x46\x03\x69"
  4791. "\x29\x0d\x81\x49\xba\x5d\xaf\x37\xfd\xeb\x25\x52\x1d\xd9\xbd\x65"
  4792. "\xfa\x99\xb9\xd1\x70\x6b\xeb\xd4\xc1\x2c\xea\x24\x20\x27\xa7\xcd"
  4793. "\xfa\xe1\x81\xd9\xd5\xc1\x1c\xc7\xe9\x70\xc3\xc7\x21\x6f\x32\x22"
  4794. "\xe3\x27\xdb\x58\x5e\xea\x18\x2d\x63\x4d\x14\x6c\x94\xcf\x2b\x7e"
  4795. "\x6e\x2a\x74\xf3\xe0\xac\xb3\xb2\xcc\xef\x38\xe9\xe7\x35\xb3\xc5"
  4796. "\x77\x9d\xff\xe3\x08\x8e\xf8\x2c\x89\xbb\x45\x22\x16\x99\x91\xc0"
  4797. "\xe7\x71\x57\x75\xc5\xb1\xc6\xaf\x27\xcb\x64\x8c\xc4\xee\x3d\x5f"
  4798. "\x4c\x35\xfb\x1c\xf3\xf8\x0e\xfd\x5e\xfc\x07\xd8\x4d\x55\x32\x49"
  4799. "\x45\x0d\xab\x4a\x49\xc4\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93"
  4800. "\x7a\xe6\x6b\xb4\x36\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43"
  4801. "\x2f\x3b\xfc\x09\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48"
  4802. "\x3d\x0e\xda\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08"
  4803. "\xd9\xdc\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a"
  4804. "\xb7\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  4805. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4\xe6"
  4806. "\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97\x68\x44";
  4807. byte hash[sizeof(large_digest) - 1];
  4808. /*
  4809. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE256_Msg0.pdf
  4810. */
  4811. a.input = "";
  4812. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  4813. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  4814. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  4815. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  4816. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  4817. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  4818. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  4819. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46\xc1\x85\xc1\x51\x11\xe5"
  4820. "\x95\x52\x2a\x6b\xcd\x16\xcf\x86\xf3\xd1\x22\x10\x9e\x3b\x1f"
  4821. "\xdd";
  4822. a.inLen = XSTRLEN(a.input);
  4823. a.outLen = WC_SHA3_256_BLOCK_SIZE;
  4824. b.input = "abc";
  4825. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  4826. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  4827. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  4828. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  4829. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  4830. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  4831. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  4832. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0\x9a\x3e\x6d\x51\xcb\xfc"
  4833. "\x62\x27\x20\xd7\xa7\x5c\x63\x34\xe8\xa2\xd7\xec\x71\xa7\xcc"
  4834. "\x29";
  4835. b.inLen = XSTRLEN(b.input);
  4836. b.outLen = WC_SHA3_256_BLOCK_SIZE;
  4837. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4838. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  4839. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  4840. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  4841. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  4842. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  4843. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  4844. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  4845. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4\x9d\xd3\xef\x7e\x18\x2b"
  4846. "\x15\x24\xdf\x82\xea\x1c\xef\xe1\xc6\xc3\x96\x61\x75\xf0\x22"
  4847. "\x8d";
  4848. c.inLen = XSTRLEN(c.input);
  4849. c.outLen = WC_SHA3_256_BLOCK_SIZE;
  4850. /* Taken from NIST CAVP test vectors - full rate output. */
  4851. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  4852. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  4853. "\x85\xe0";
  4854. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  4855. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  4856. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  4857. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  4858. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  4859. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  4860. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  4861. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  4862. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  4863. "\xc2";
  4864. d.inLen = 32;
  4865. d.outLen = WC_SHA3_256_BLOCK_SIZE;
  4866. /* Taken from NIST CAVP test vectors - more than one output block. */
  4867. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  4868. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  4869. "\x47\xe4";
  4870. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  4871. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  4872. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  4873. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  4874. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  4875. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  4876. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  4877. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  4878. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  4879. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  4880. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  4881. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  4882. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  4883. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  4884. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  4885. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  4886. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4"
  4887. "\xe6\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97"
  4888. "\x68\x44";
  4889. e.inLen = 32;
  4890. e.outLen = 2 * WC_SHA3_256_BLOCK_SIZE;
  4891. test_sha[0] = a;
  4892. test_sha[1] = b;
  4893. test_sha[2] = c;
  4894. test_sha[3] = d;
  4895. test_sha[4] = e;
  4896. for (i = 0; i < times; ++i) {
  4897. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  4898. if (ret != 0)
  4899. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4900. ret = wc_Shake256_Absorb(sha, (byte*)test_sha[i].input,
  4901. (word32)test_sha[i].inLen);
  4902. if (ret != 0)
  4903. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4904. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  4905. (word32)test_sha[i].outLen / WC_SHA3_256_BLOCK_SIZE);
  4906. if (ret != 0)
  4907. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4908. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  4909. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4910. }
  4911. #ifndef NO_LARGE_HASH_TEST
  4912. /* BEGIN LARGE HASH TEST */ {
  4913. for (i = 0; i < (int)large_input_buf_size; i++) {
  4914. large_input_buf[i] = (byte)(i & 0xFF);
  4915. }
  4916. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  4917. if (ret != 0)
  4918. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4919. /* Absorb is non-incremental. */
  4920. ret = wc_Shake256_Absorb(sha, large_input_buf,
  4921. (word32)large_input_buf_size);
  4922. if (ret != 0)
  4923. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4924. /* Able to squeeze out blocks incrementally. */
  4925. ret = wc_Shake256_SqueezeBlocks(sha, hash, 1);
  4926. if (ret != 0)
  4927. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4928. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  4929. ((word32)sizeof(hash) / WC_SHA3_256_BLOCK_SIZE) - 1);
  4930. if (ret != 0)
  4931. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4932. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  4933. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4934. } /* END LARGE HASH TEST */
  4935. #endif /* NO_LARGE_HASH_TEST */
  4936. exit:
  4937. return ret;
  4938. }
  4939. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake256_test(void)
  4940. {
  4941. wc_Shake sha;
  4942. byte hash[250];
  4943. testVector a, b, c, d, e;
  4944. testVector test_sha[5];
  4945. wc_test_ret_t ret = 0;
  4946. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4947. #define SHAKE256_LARGE_INPUT_BUFSIZ 1024
  4948. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4949. byte *large_input = NULL;
  4950. #else
  4951. byte large_input[SHAKE256_LARGE_INPUT_BUFSIZ];
  4952. #endif
  4953. const char* large_digest =
  4954. "\x90\x32\x4a\xcc\xd1\xdf\xb8\x0b\x79\x1f\xb8\xc8\x5b\x54\xc8\xe7"
  4955. "\x45\xf5\x60\x6b\x38\x26\xb2\x0a\xee\x38\x01\xf3\xd9\xfa\x96\x9f"
  4956. "\x6a\xd7\x15\xdf\xb6\xc2\xf4\x20\x33\x44\x55\xe8\x2a\x09\x2b\x68"
  4957. "\x2e\x18\x65\x5e\x65\x93\x28\xbc\xb1\x9e\xe2\xb1\x92\xea\x98\xac"
  4958. "\x21\xef\x4c\xe1\xb4\xb7\xbe\x81\x5c\x1d\xd3\xb7\x17\xe5\xbb\xc5"
  4959. "\x8c\x68\xb7\xfb\xac\x55\x8a\x9b\x4d\x91\xe4\x9f\x72\xbb\x6e\x38"
  4960. "\xaf\x21\x7d\x21\xaa\x98\x4e\x75\xc4\xb4\x1c\x7c\x50\x45\x54\xf9"
  4961. "\xea\x26";
  4962. WOLFSSL_ENTER("shake256_test");
  4963. /*
  4964. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE256_Msg0.pdf
  4965. */
  4966. a.input = "";
  4967. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  4968. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  4969. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  4970. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  4971. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  4972. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  4973. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  4974. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46";
  4975. a.inLen = XSTRLEN(a.input);
  4976. a.outLen = 114;
  4977. b.input = "abc";
  4978. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  4979. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  4980. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  4981. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  4982. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  4983. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  4984. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  4985. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0";
  4986. b.inLen = XSTRLEN(b.input);
  4987. b.outLen = 114;
  4988. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4989. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  4990. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  4991. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  4992. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  4993. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  4994. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  4995. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  4996. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  4997. c.inLen = XSTRLEN(c.input);
  4998. c.outLen = 114;
  4999. /* Taken from NIST CAVP test vectors - full rate output. */
  5000. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  5001. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  5002. "\x85\xe0";
  5003. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  5004. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  5005. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  5006. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  5007. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  5008. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  5009. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  5010. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  5011. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  5012. "\xc2";
  5013. d.inLen = 32;
  5014. d.outLen = 136;
  5015. /* Taken from NIST CAVP test vectors - more than one output block. */
  5016. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  5017. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  5018. "\x47\xe4";
  5019. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  5020. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  5021. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  5022. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  5023. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  5024. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  5025. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  5026. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  5027. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  5028. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  5029. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  5030. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  5031. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  5032. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  5033. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  5034. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  5035. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c";
  5036. e.inLen = 32;
  5037. e.outLen = 250;
  5038. test_sha[0] = a;
  5039. test_sha[1] = b;
  5040. test_sha[2] = c;
  5041. test_sha[3] = d;
  5042. test_sha[4] = e;
  5043. ret = wc_InitShake256(&sha, HEAP_HINT, devId);
  5044. if (ret != 0)
  5045. return WC_TEST_RET_ENC_EC(ret);
  5046. for (i = 0; i < times; ++i) {
  5047. ret = wc_Shake256_Update(&sha, (byte*)test_sha[i].input,
  5048. (word32)test_sha[i].inLen);
  5049. if (ret != 0)
  5050. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5051. ret = wc_Shake256_Final(&sha, hash, (word32)test_sha[i].outLen);
  5052. if (ret != 0)
  5053. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5054. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  5055. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5056. }
  5057. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  5058. large_input = (byte *)XMALLOC(SHAKE256_LARGE_INPUT_BUFSIZ, NULL,
  5059. DYNAMIC_TYPE_TMP_BUFFER);
  5060. if (large_input == NULL)
  5061. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  5062. #endif
  5063. #ifndef NO_LARGE_HASH_TEST
  5064. /* BEGIN LARGE HASH TEST */ {
  5065. for (i = 0; i < SHAKE256_LARGE_INPUT_BUFSIZ; i++) {
  5066. large_input[i] = (byte)(i & 0xFF);
  5067. }
  5068. times = 100;
  5069. for (i = 0; i < times; ++i) {
  5070. ret = wc_Shake256_Update(&sha, (byte*)large_input,
  5071. SHAKE256_LARGE_INPUT_BUFSIZ);
  5072. if (ret != 0)
  5073. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  5074. }
  5075. ret = wc_Shake256_Final(&sha, hash, (word32)sizeof(hash));
  5076. if (ret != 0)
  5077. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  5078. if (XMEMCMP(hash, large_digest, 114) != 0)
  5079. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  5080. } /* END LARGE HASH TEST */
  5081. #endif /* NO_LARGE_HASH_TEST */
  5082. ret = shake256_absorb_test(&sha, large_input, SHAKE256_LARGE_INPUT_BUFSIZ);
  5083. exit:
  5084. wc_Shake256_Free(&sha);
  5085. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  5086. if (large_input != NULL)
  5087. XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5088. #endif
  5089. return ret;
  5090. }
  5091. #endif
  5092. #ifdef WOLFSSL_SM3
  5093. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm3_test(void)
  5094. {
  5095. wc_Sm3 sm3, sm3Copy;
  5096. byte hash[WC_SM3_DIGEST_SIZE];
  5097. byte hashGet[WC_SM3_DIGEST_SIZE];
  5098. byte hashCopy[WC_SM3_DIGEST_SIZE];
  5099. wc_test_ret_t ret = 0;
  5100. WOLFSSL_ENTER("sm3_test");
  5101. testVector a, b, c;
  5102. testVector test_sm3[3];
  5103. int times = sizeof(test_sm3) / sizeof(struct testVector), i;
  5104. a.input = "";
  5105. a.output = "\x1a\xb2\x1d\x83\x55\xcf\xa1\x7f\x8e\x61\x19\x48\x31\xe8\x1a"
  5106. "\x8f\x22\xbe\xc8\xc7\x28\xfe\xfb\x74\x7e\xd0\x35\xeb\x50\x82"
  5107. "\xaa\x2b";
  5108. a.inLen = XSTRLEN(a.input);
  5109. a.outLen = WC_SM3_DIGEST_SIZE;
  5110. b.input = "abc";
  5111. b.output = "\x66\xc7\xf0\xf4\x62\xee\xed\xd9\xd1\xf2\xd4\x6b\xdc\x10\xe4"
  5112. "\xe2\x41\x67\xc4\x87\x5c\xf2\xf7\xa2\x29\x7d\xa0\x2b\x8f\x4b"
  5113. "\xa8\xe0";
  5114. b.inLen = XSTRLEN(b.input);
  5115. b.outLen = WC_SM3_DIGEST_SIZE;
  5116. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  5117. c.output = "\x63\x9b\x6c\xc5\xe6\x4d\x9e\x37\xa3\x90\xb1\x92\xdf\x4f\xa1"
  5118. "\xea\x07\x20\xab\x74\x7f\xf6\x92\xb9\xf3\x8c\x4e\x66\xad\x7b"
  5119. "\x8c\x05";
  5120. c.inLen = XSTRLEN(c.input);
  5121. c.outLen = WC_SM3_DIGEST_SIZE;
  5122. test_sm3[0] = a;
  5123. test_sm3[1] = b;
  5124. test_sm3[2] = c;
  5125. ret = wc_InitSm3(&sm3, HEAP_HINT, devId);
  5126. if (ret != 0)
  5127. return WC_TEST_RET_ENC_EC(ret);
  5128. ret = wc_InitSm3(&sm3Copy, HEAP_HINT, devId);
  5129. if (ret != 0) {
  5130. wc_Sm3Free(&sm3);
  5131. return WC_TEST_RET_ENC_EC(ret);
  5132. }
  5133. /* Test all the KATs. */
  5134. for (i = 0; i < times; ++i) {
  5135. ret = wc_Sm3Update(&sm3, (byte*)test_sm3[i].input,
  5136. (word32)test_sm3[i].inLen);
  5137. if (ret != 0) {
  5138. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5139. }
  5140. /* Get the final hash but leave ready for more updates. */
  5141. ret = wc_Sm3GetHash(&sm3, hashGet);
  5142. if (ret != 0)
  5143. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5144. /* Make a copy of the hash. */
  5145. ret = wc_Sm3Copy(&sm3, &sm3Copy);
  5146. if (ret != 0)
  5147. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5148. /* Get the final hash with original. */
  5149. ret = wc_Sm3Final(&sm3, hash);
  5150. if (ret != 0)
  5151. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5152. /* Get the final hash with copy. */
  5153. ret = wc_Sm3Final(&sm3Copy, hashCopy);
  5154. if (ret != 0)
  5155. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5156. /* Dispose of copy. */
  5157. wc_Sm3Free(&sm3Copy);
  5158. /* Check hashes match expected. */
  5159. if (XMEMCMP(hash, test_sm3[i].output, WC_SM3_DIGEST_SIZE) != 0)
  5160. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5161. if (XMEMCMP(hash, hashGet, WC_SM3_DIGEST_SIZE) != 0)
  5162. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5163. if (XMEMCMP(hash, hashCopy, WC_SM3_DIGEST_SIZE) != 0)
  5164. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5165. }
  5166. #ifndef NO_LARGE_HASH_TEST
  5167. {
  5168. word32 sz;
  5169. byte large_input[1024];
  5170. #ifdef HASH_SIZE_LIMIT
  5171. const char* large_digest =
  5172. "\x6c\x42\x57\x64\x8e\x45\xf3\xb6\xc0\x83\xd3\x41\x83\x66\x51\xb4"
  5173. "\x50\xfe\x06\xb5\xb7\x1e\xd5\x0d\x41\xfc\x1e\xe5\xc6\x57\x95\x0f";
  5174. times = 20;
  5175. #else
  5176. const char* large_digest =
  5177. "\x34\x51\x3c\xde\x7c\x30\xb7\xc5\xaa\x97\x3b\xed\xb3\x16\xb9\x76"
  5178. "\x35\x46\x14\x80\x2a\x57\xca\xd9\x48\xf9\x93\xcc\x1f\xdd\xab\x79";
  5179. times = 100;
  5180. #endif
  5181. /* Set large input to something. */
  5182. for (i = 0; i < (int)sizeof(large_input); i++) {
  5183. large_input[i] = (byte)(i & 0xFF);
  5184. }
  5185. /* Hash a large number of times. */
  5186. for (i = 0; i < times; ++i) {
  5187. ret = wc_Sm3Update(&sm3, (byte*)large_input,
  5188. (word32)sizeof(large_input));
  5189. if (ret != 0)
  5190. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5191. }
  5192. /* Calculate hash and compare to expected. */
  5193. ret = wc_Sm3Final(&sm3, hash);
  5194. if (ret != 0)
  5195. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  5196. if (XMEMCMP(hash, large_digest, WC_SM3_DIGEST_SIZE) != 0)
  5197. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  5198. /* Check updating with various sizes works. */
  5199. for (sz = 1; sz <= 64; sz++) {
  5200. /* Hash a large number of times. */
  5201. for (i = 0; i < times; ++i) {
  5202. word32 o;
  5203. /* Update sz bytes at a time from large input buffer. */
  5204. for (o = 0; o + sz <= (word32)sizeof(large_input); o += sz) {
  5205. ret = wc_Sm3Update(&sm3, (byte*)(large_input + o), sz);
  5206. if (ret != 0)
  5207. ERROR_OUT(WC_TEST_RET_ENC_I(o), exit);
  5208. }
  5209. /* Check for left-overs. */
  5210. if (o < (word32)sizeof(large_input)) {
  5211. ret = wc_Sm3Update(&sm3, (byte*)(large_input + o),
  5212. (word32)sizeof(large_input) - o);
  5213. if (ret != 0)
  5214. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  5215. }
  5216. }
  5217. /* Calculate hash and compare to expected. */
  5218. ret = wc_Sm3Final(&sm3, hash);
  5219. if (ret != 0)
  5220. ERROR_OUT(WC_TEST_RET_ENC_I(sz), exit);
  5221. if (XMEMCMP(hash, large_digest, WC_SM3_DIGEST_SIZE) != 0)
  5222. ERROR_OUT(WC_TEST_RET_ENC_I(sz), exit);
  5223. }
  5224. }
  5225. #endif /* NO_LARGE_HASH_TEST */
  5226. exit:
  5227. wc_Sm3Free(&sm3);
  5228. wc_Sm3Free(&sm3Copy);
  5229. return ret;
  5230. }
  5231. #endif
  5232. #ifndef NO_HASH_WRAPPER
  5233. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hash_test(void)
  5234. {
  5235. wc_HashAlg hash;
  5236. int ret, exp_ret;
  5237. int i, j;
  5238. int digestSz;
  5239. byte data[] = "0123456789abcdef0123456789abcdef0123456";
  5240. byte out[WC_MAX_DIGEST_SIZE];
  5241. byte hashOut[WC_MAX_DIGEST_SIZE];
  5242. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  5243. enum wc_HashType hashType;
  5244. #endif
  5245. enum wc_HashType typesGood[] = { WC_HASH_TYPE_MD5, WC_HASH_TYPE_SHA,
  5246. WC_HASH_TYPE_SHA224, WC_HASH_TYPE_SHA256,
  5247. WC_HASH_TYPE_SHA384, WC_HASH_TYPE_SHA512,
  5248. WC_HASH_TYPE_SHA3_224,
  5249. WC_HASH_TYPE_SHA3_256,
  5250. WC_HASH_TYPE_SHA3_384,
  5251. WC_HASH_TYPE_SHA3_512 };
  5252. enum wc_HashType typesNoImpl[] = {
  5253. #ifdef NO_MD5
  5254. WC_HASH_TYPE_MD5,
  5255. #endif
  5256. #ifdef NO_SHA
  5257. WC_HASH_TYPE_SHA,
  5258. #endif
  5259. #ifndef WOLFSSL_SHA224
  5260. WC_HASH_TYPE_SHA224,
  5261. #endif
  5262. #ifdef NO_SHA256
  5263. WC_HASH_TYPE_SHA256,
  5264. #endif
  5265. #ifndef WOLFSSL_SHA384
  5266. WC_HASH_TYPE_SHA384,
  5267. #endif
  5268. #ifndef WOLFSSL_SHA512
  5269. WC_HASH_TYPE_SHA512,
  5270. #endif
  5271. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_224)
  5272. WC_HASH_TYPE_SHA3_224,
  5273. #endif
  5274. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_256)
  5275. WC_HASH_TYPE_SHA3_256,
  5276. #endif
  5277. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_384)
  5278. WC_HASH_TYPE_SHA3_384,
  5279. #endif
  5280. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_512)
  5281. WC_HASH_TYPE_SHA3_512,
  5282. #endif
  5283. WC_HASH_TYPE_NONE
  5284. };
  5285. enum wc_HashType typesBad[] = { WC_HASH_TYPE_NONE, WC_HASH_TYPE_MD5_SHA,
  5286. WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4 };
  5287. enum wc_HashType typesHashBad[] = { WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4,
  5288. WC_HASH_TYPE_BLAKE2B,
  5289. WC_HASH_TYPE_NONE };
  5290. WOLFSSL_ENTER("hash_test");
  5291. /* Parameter Validation testing. */
  5292. ret = wc_HashInit(NULL, WC_HASH_TYPE_SHA256);
  5293. if (ret != BAD_FUNC_ARG)
  5294. return WC_TEST_RET_ENC_EC(ret);
  5295. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  5296. if (ret != BAD_FUNC_ARG)
  5297. return WC_TEST_RET_ENC_EC(ret);
  5298. ret = wc_HashUpdate(&hash, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  5299. if (ret != BAD_FUNC_ARG)
  5300. return WC_TEST_RET_ENC_EC(ret);
  5301. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, data, sizeof(data));
  5302. if (ret != BAD_FUNC_ARG)
  5303. return WC_TEST_RET_ENC_EC(ret);
  5304. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, NULL);
  5305. if (ret != BAD_FUNC_ARG)
  5306. return WC_TEST_RET_ENC_EC(ret);
  5307. ret = wc_HashFinal(&hash, WC_HASH_TYPE_SHA256, NULL);
  5308. if (ret != BAD_FUNC_ARG)
  5309. return WC_TEST_RET_ENC_EC(ret);
  5310. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, out);
  5311. if (ret != BAD_FUNC_ARG)
  5312. return WC_TEST_RET_ENC_EC(ret);
  5313. /* Try invalid hash algorithms. */
  5314. for (i = 0; i < (int)(sizeof(typesBad)/sizeof(*typesBad)); i++) {
  5315. ret = wc_HashInit(&hash, typesBad[i]);
  5316. if (ret != BAD_FUNC_ARG)
  5317. return WC_TEST_RET_ENC_I(i);
  5318. ret = wc_HashUpdate(&hash, typesBad[i], data, sizeof(data));
  5319. if (ret != BAD_FUNC_ARG)
  5320. return WC_TEST_RET_ENC_I(i);
  5321. ret = wc_HashFinal(&hash, typesBad[i], out);
  5322. if (ret != BAD_FUNC_ARG)
  5323. return WC_TEST_RET_ENC_I(i);
  5324. wc_HashFree(&hash, typesBad[i]);
  5325. }
  5326. /* Try valid hash algorithms. */
  5327. for (i = 0, j = 0; i < (int)(sizeof(typesGood)/sizeof(*typesGood)); i++) {
  5328. exp_ret = 0;
  5329. if (typesGood[i] == typesNoImpl[j]) {
  5330. /* Recognized but no implementation compiled in. */
  5331. exp_ret = HASH_TYPE_E;
  5332. j++;
  5333. }
  5334. ret = wc_HashInit(&hash, typesGood[i]);
  5335. if (ret != exp_ret)
  5336. return WC_TEST_RET_ENC_I(i);
  5337. ret = wc_HashUpdate(&hash, typesGood[i], data, sizeof(data));
  5338. if (ret != exp_ret)
  5339. return WC_TEST_RET_ENC_I(i);
  5340. ret = wc_HashFinal(&hash, typesGood[i], out);
  5341. if (ret != exp_ret)
  5342. return WC_TEST_RET_ENC_I(i);
  5343. wc_HashFree(&hash, typesGood[i]);
  5344. digestSz = wc_HashGetDigestSize(typesGood[i]);
  5345. if (exp_ret < 0 && digestSz != exp_ret)
  5346. return WC_TEST_RET_ENC_I(i);
  5347. if (exp_ret == 0 && digestSz < 0)
  5348. return WC_TEST_RET_ENC_I(i);
  5349. if (exp_ret == 0) {
  5350. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut,
  5351. digestSz - 1);
  5352. if (ret != BUFFER_E)
  5353. return WC_TEST_RET_ENC_I(i);
  5354. }
  5355. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut, (word32)digestSz);
  5356. if (ret != exp_ret)
  5357. return WC_TEST_RET_ENC_I(i);
  5358. if (exp_ret == 0 && XMEMCMP(out, hashOut, digestSz) != 0)
  5359. return WC_TEST_RET_ENC_I(i);
  5360. ret = wc_HashGetBlockSize(typesGood[i]);
  5361. if (exp_ret < 0 && ret != exp_ret)
  5362. return WC_TEST_RET_ENC_I(i);
  5363. if (exp_ret == 0 && ret < 0)
  5364. return WC_TEST_RET_ENC_I(i);
  5365. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  5366. ret = wc_HashGetOID(typesGood[i]);
  5367. if (ret == BAD_FUNC_ARG ||
  5368. (exp_ret == 0 && ret == HASH_TYPE_E) ||
  5369. (exp_ret != 0 && ret != HASH_TYPE_E)) {
  5370. return WC_TEST_RET_ENC_I(i);
  5371. }
  5372. hashType = wc_OidGetHash(ret);
  5373. if (exp_ret == 0 && hashType != typesGood[i])
  5374. return WC_TEST_RET_ENC_I(i);
  5375. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  5376. }
  5377. for (i = 0; i < (int)(sizeof(typesHashBad)/sizeof(*typesHashBad)); i++) {
  5378. ret = wc_Hash(typesHashBad[i], data, sizeof(data), out, sizeof(out));
  5379. if ((ret != BAD_FUNC_ARG) && (ret != BUFFER_E) && (ret != HASH_TYPE_E))
  5380. return WC_TEST_RET_ENC_I(i);
  5381. }
  5382. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  5383. ret = wc_HashGetOID(WC_HASH_TYPE_MD2);
  5384. #ifdef WOLFSSL_MD2
  5385. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5386. return WC_TEST_RET_ENC_EC(ret);
  5387. #else
  5388. if (ret != HASH_TYPE_E)
  5389. return WC_TEST_RET_ENC_EC(ret);
  5390. #endif
  5391. hashType = wc_OidGetHash(646); /* Md2h */
  5392. #ifdef WOLFSSL_MD2
  5393. if (hashType != WC_HASH_TYPE_MD2)
  5394. return WC_TEST_RET_ENC_NC;
  5395. #else
  5396. if (hashType != WC_HASH_TYPE_NONE)
  5397. return WC_TEST_RET_ENC_NC;
  5398. #endif
  5399. ret = wc_HashGetOID(WC_HASH_TYPE_MD5_SHA);
  5400. #ifndef NO_MD5
  5401. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5402. return WC_TEST_RET_ENC_EC(ret);
  5403. #else
  5404. if (ret != HASH_TYPE_E)
  5405. return WC_TEST_RET_ENC_EC(ret);
  5406. #endif
  5407. ret = wc_HashGetOID(WC_HASH_TYPE_MD4);
  5408. if (ret != BAD_FUNC_ARG)
  5409. return WC_TEST_RET_ENC_EC(ret);
  5410. ret = wc_HashGetOID(WC_HASH_TYPE_NONE);
  5411. if (ret != BAD_FUNC_ARG)
  5412. return WC_TEST_RET_ENC_EC(ret);
  5413. hashType = wc_OidGetHash(0);
  5414. if (hashType != WC_HASH_TYPE_NONE)
  5415. return WC_TEST_RET_ENC_NC;
  5416. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  5417. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD2);
  5418. #ifdef WOLFSSL_MD2
  5419. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5420. return WC_TEST_RET_ENC_EC(ret);
  5421. #else
  5422. if (ret != HASH_TYPE_E)
  5423. return WC_TEST_RET_ENC_EC(ret);
  5424. #endif
  5425. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD2);
  5426. #ifdef WOLFSSL_MD2
  5427. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5428. return WC_TEST_RET_ENC_EC(ret);
  5429. #else
  5430. if (ret != HASH_TYPE_E)
  5431. return WC_TEST_RET_ENC_EC(ret);
  5432. #endif
  5433. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD4);
  5434. #ifndef NO_MD4
  5435. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5436. return WC_TEST_RET_ENC_EC(ret);
  5437. #else
  5438. if (ret != HASH_TYPE_E)
  5439. return WC_TEST_RET_ENC_EC(ret);
  5440. #endif
  5441. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD4);
  5442. #ifndef NO_MD4
  5443. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5444. return WC_TEST_RET_ENC_EC(ret);
  5445. #else
  5446. if (ret != HASH_TYPE_E)
  5447. return WC_TEST_RET_ENC_EC(ret);
  5448. #endif
  5449. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD5_SHA);
  5450. #if !defined(NO_MD5) && !defined(NO_SHA)
  5451. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5452. return WC_TEST_RET_ENC_EC(ret);
  5453. #else
  5454. if (ret != HASH_TYPE_E)
  5455. return WC_TEST_RET_ENC_EC(ret);
  5456. #endif
  5457. ret = wc_HashGetBlockSize(WC_HASH_TYPE_BLAKE2B);
  5458. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  5459. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5460. return WC_TEST_RET_ENC_EC(ret);
  5461. #else
  5462. if (ret != HASH_TYPE_E)
  5463. return WC_TEST_RET_ENC_EC(ret);
  5464. #endif
  5465. ret = wc_HashGetDigestSize(WC_HASH_TYPE_BLAKE2B);
  5466. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  5467. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  5468. return WC_TEST_RET_ENC_EC(ret);
  5469. #else
  5470. if (ret != HASH_TYPE_E)
  5471. return WC_TEST_RET_ENC_EC(ret);
  5472. #endif
  5473. ret = wc_HashGetBlockSize(WC_HASH_TYPE_NONE);
  5474. if (ret != BAD_FUNC_ARG)
  5475. return WC_TEST_RET_ENC_EC(ret);
  5476. ret = wc_HashGetDigestSize(WC_HASH_TYPE_NONE);
  5477. if (ret != BAD_FUNC_ARG)
  5478. return WC_TEST_RET_ENC_EC(ret);
  5479. #if !defined(NO_CERTS) && !defined(NO_ASN)
  5480. #if defined(WOLFSSL_MD2) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5481. ret = wc_GetCTC_HashOID(MD2);
  5482. if (ret == 0)
  5483. return WC_TEST_RET_ENC_EC(ret);
  5484. #endif
  5485. #ifndef NO_MD5
  5486. ret = wc_GetCTC_HashOID(WC_MD5);
  5487. if (ret == 0)
  5488. return WC_TEST_RET_ENC_EC(ret);
  5489. #endif
  5490. #ifndef NO_SHA
  5491. ret = wc_GetCTC_HashOID(WC_SHA);
  5492. if (ret == 0)
  5493. return WC_TEST_RET_ENC_EC(ret);
  5494. #endif
  5495. #ifdef WOLFSSL_SHA224
  5496. ret = wc_GetCTC_HashOID(WC_SHA224);
  5497. if (ret == 0)
  5498. return WC_TEST_RET_ENC_EC(ret);
  5499. #endif
  5500. #ifndef NO_SHA256
  5501. ret = wc_GetCTC_HashOID(WC_SHA256);
  5502. if (ret == 0)
  5503. return WC_TEST_RET_ENC_EC(ret);
  5504. #endif
  5505. #ifdef WOLFSSL_SHA384
  5506. ret = wc_GetCTC_HashOID(WC_SHA384);
  5507. if (ret == 0)
  5508. return WC_TEST_RET_ENC_EC(ret);
  5509. #endif
  5510. #ifdef WOLFSSL_SHA512
  5511. ret = wc_GetCTC_HashOID(WC_SHA512);
  5512. if (ret == 0)
  5513. return WC_TEST_RET_ENC_EC(ret);
  5514. #endif
  5515. ret = wc_GetCTC_HashOID(-1);
  5516. if (ret != 0)
  5517. return WC_TEST_RET_ENC_EC(ret);
  5518. #endif
  5519. return 0;
  5520. }
  5521. #endif /* !NO_HASH_WRAPPER */
  5522. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS) && \
  5523. defined(HAVE_FIPS_VERSION) && \
  5524. (HAVE_FIPS_VERSION >= 5))
  5525. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_md5_test(void)
  5526. {
  5527. Hmac hmac;
  5528. byte hash[WC_MD5_DIGEST_SIZE];
  5529. const char* keys[]=
  5530. {
  5531. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  5532. "Jefe",
  5533. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA",
  5534. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5535. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5536. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5537. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5538. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5539. };
  5540. testVector a, b, c, d;
  5541. testVector test_hmac[4];
  5542. wc_test_ret_t ret;
  5543. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5544. WOLFSSL_ENTER("hmac_md5_test");
  5545. /* Following test vectors are from RFC 2202 section 2 */
  5546. a.input = "Hi There";
  5547. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  5548. "\x9d";
  5549. a.inLen = XSTRLEN(a.input);
  5550. a.outLen = WC_MD5_DIGEST_SIZE;
  5551. b.input = "what do ya want for nothing?";
  5552. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  5553. "\x38";
  5554. b.inLen = XSTRLEN(b.input);
  5555. b.outLen = WC_MD5_DIGEST_SIZE;
  5556. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5557. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5558. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5559. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5560. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  5561. "\xf6";
  5562. c.inLen = XSTRLEN(c.input);
  5563. c.outLen = WC_MD5_DIGEST_SIZE;
  5564. d.input = "Test Using Larger Than Block-Size Key - Hash Key First";
  5565. d.output = "\x6b\x1a\xb7\xfe\x4b\xd7\xbf\x8f\x0b\x62\xe6\xce\x61\xb9\xd0"
  5566. "\xcd";
  5567. d.inLen = XSTRLEN(d.input);
  5568. d.outLen = WC_MD5_DIGEST_SIZE;
  5569. test_hmac[0] = a;
  5570. test_hmac[1] = b;
  5571. test_hmac[2] = c;
  5572. test_hmac[3] = d;
  5573. for (i = 0; i < times; ++i) {
  5574. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  5575. if (i == 1) {
  5576. continue; /* cavium can't handle short keys, fips not allowed */
  5577. }
  5578. #endif
  5579. ret = wc_HmacInit(&hmac, HEAP_HINT, devId);
  5580. if (ret != 0)
  5581. return WC_TEST_RET_ENC_EC(ret);
  5582. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[i],
  5583. (word32)XSTRLEN(keys[i]));
  5584. if (ret != 0)
  5585. return WC_TEST_RET_ENC_EC(ret);
  5586. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5587. (word32)test_hmac[i].inLen);
  5588. if (ret != 0)
  5589. return WC_TEST_RET_ENC_EC(ret);
  5590. ret = wc_HmacFinal(&hmac, hash);
  5591. if (ret != 0)
  5592. return WC_TEST_RET_ENC_EC(ret);
  5593. if (XMEMCMP(hash, test_hmac[i].output, WC_MD5_DIGEST_SIZE) != 0)
  5594. return WC_TEST_RET_ENC_I(i);
  5595. wc_HmacFree(&hmac);
  5596. }
  5597. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  5598. if ((ret = wc_HmacSizeByType(WC_MD5)) != WC_MD5_DIGEST_SIZE)
  5599. return WC_TEST_RET_ENC_EC(ret);
  5600. #endif
  5601. return 0;
  5602. }
  5603. #endif /* !NO_HMAC && !NO_MD5 && (!HAVE_FIPS || (HAVE_FIPS_VERSION < 5)) */
  5604. #if !defined(NO_HMAC) && !defined(NO_SHA)
  5605. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha_test(void)
  5606. {
  5607. Hmac hmac;
  5608. byte hash[WC_SHA_DIGEST_SIZE];
  5609. const char* keys[]=
  5610. {
  5611. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5612. "\x0b\x0b\x0b",
  5613. "Jefe",
  5614. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5615. "\xAA\xAA\xAA",
  5616. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5617. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5618. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5619. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5620. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5621. };
  5622. testVector a, b, c, d;
  5623. testVector test_hmac[4];
  5624. wc_test_ret_t ret;
  5625. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5626. WOLFSSL_ENTER("hmac_sha_test");
  5627. /* Following test vectors are from RFC 2202 section 3 */
  5628. a.input = "Hi There";
  5629. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  5630. "\x8e\xf1\x46\xbe\x00";
  5631. a.inLen = XSTRLEN(a.input);
  5632. a.outLen = WC_SHA_DIGEST_SIZE;
  5633. b.input = "what do ya want for nothing?";
  5634. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  5635. "\x9c\x25\x9a\x7c\x79";
  5636. b.inLen = XSTRLEN(b.input);
  5637. b.outLen = WC_SHA_DIGEST_SIZE;
  5638. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5639. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5640. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5641. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5642. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  5643. "\x4f\x63\xf1\x75\xd3";
  5644. c.inLen = XSTRLEN(c.input);
  5645. c.outLen = WC_SHA_DIGEST_SIZE;
  5646. d.input = "Test Using Larger Than Block-Size Key - Hash Key First";
  5647. d.output = "\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b"
  5648. "\x55\xed\x40\x21\x12";
  5649. d.inLen = XSTRLEN(d.input);
  5650. d.outLen = WC_SHA_DIGEST_SIZE;
  5651. test_hmac[0] = a;
  5652. test_hmac[1] = b;
  5653. test_hmac[2] = c;
  5654. test_hmac[3] = d;
  5655. #if FIPS_VERSION3_GE(6,0,0)
  5656. int allowShortKeyWithFips = 1;
  5657. #endif
  5658. for (i = 0; i < times; ++i) {
  5659. #if defined(HAVE_CAVIUM) || (defined(HAVE_FIPS) && FIPS_VERSION3_LT(6,0,0))
  5660. if (i == 1)
  5661. continue; /* cavium can't handle short keys, fips not allowed */
  5662. #endif
  5663. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5664. return WC_TEST_RET_ENC_EC(ret);
  5665. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[i],
  5666. (word32)XSTRLEN(keys[i]));
  5667. #if FIPS_VERSION3_GE(6,0,0)
  5668. if (i == 1) {
  5669. if (ret != HMAC_MIN_KEYLEN_E)
  5670. return WC_TEST_RET_ENC_EC(ret);
  5671. /* Now use the ex and allow short keys with FIPS option */
  5672. ret = wc_HmacSetKey_ex(&hmac, WC_SHA, (byte*) keys[i],
  5673. (word32)XSTRLEN(keys[i]), allowShortKeyWithFips);
  5674. }
  5675. #endif
  5676. if (ret != 0)
  5677. return WC_TEST_RET_ENC_EC(ret);
  5678. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5679. (word32)test_hmac[i].inLen);
  5680. if (ret != 0)
  5681. return WC_TEST_RET_ENC_EC(ret);
  5682. ret = wc_HmacFinal(&hmac, hash);
  5683. if (ret != 0)
  5684. return WC_TEST_RET_ENC_EC(ret);
  5685. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA_DIGEST_SIZE) != 0)
  5686. return WC_TEST_RET_ENC_I(i);
  5687. wc_HmacFree(&hmac);
  5688. }
  5689. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  5690. if ((ret = wc_HmacSizeByType(WC_SHA)) != WC_SHA_DIGEST_SIZE)
  5691. return WC_TEST_RET_ENC_EC(ret);
  5692. #endif
  5693. return 0;
  5694. }
  5695. #endif
  5696. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  5697. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha224_test(void)
  5698. {
  5699. Hmac hmac;
  5700. byte hash[WC_SHA224_DIGEST_SIZE];
  5701. const char* keys[]=
  5702. {
  5703. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5704. "\x0b\x0b\x0b",
  5705. "Jefe",
  5706. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5707. "\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\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5712. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5713. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5714. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5715. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5716. "\xAA\xAA\xAA"
  5717. };
  5718. testVector a, b, c, d;
  5719. testVector test_hmac[4];
  5720. wc_test_ret_t ret;
  5721. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5722. WOLFSSL_ENTER("hmac_sha224_test");
  5723. /* Following test vectors are from RFC 4231 section 4 */
  5724. a.input = "Hi There";
  5725. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  5726. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  5727. a.inLen = XSTRLEN(a.input);
  5728. a.outLen = WC_SHA224_DIGEST_SIZE;
  5729. b.input = "what do ya want for nothing?";
  5730. b.output = "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d"
  5731. "\x0f\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44";
  5732. b.inLen = XSTRLEN(b.input);
  5733. b.outLen = WC_SHA224_DIGEST_SIZE;
  5734. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5735. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5736. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5737. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5738. c.output = "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2"
  5739. "\x64\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea";
  5740. c.inLen = XSTRLEN(c.input);
  5741. c.outLen = WC_SHA224_DIGEST_SIZE;
  5742. d.input = "Test Using Larger Than Block-Size Key - Hash Key First";
  5743. 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";
  5744. d.inLen = XSTRLEN(d.input);
  5745. d.outLen = WC_SHA224_DIGEST_SIZE;
  5746. test_hmac[0] = a;
  5747. test_hmac[1] = b;
  5748. test_hmac[2] = c;
  5749. test_hmac[3] = d;
  5750. for (i = 0; i < times; ++i) {
  5751. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  5752. if (i == 1)
  5753. continue; /* cavium can't handle short keys, fips not allowed */
  5754. #endif
  5755. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5756. return WC_TEST_RET_ENC_EC(ret);
  5757. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[i],
  5758. (word32)XSTRLEN(keys[i]));
  5759. if (ret != 0)
  5760. return WC_TEST_RET_ENC_EC(ret);
  5761. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5762. (word32)test_hmac[i].inLen);
  5763. if (ret != 0)
  5764. return WC_TEST_RET_ENC_EC(ret);
  5765. ret = wc_HmacFinal(&hmac, hash);
  5766. if (ret != 0)
  5767. return WC_TEST_RET_ENC_EC(ret);
  5768. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  5769. return WC_TEST_RET_ENC_I(i);
  5770. wc_HmacFree(&hmac);
  5771. }
  5772. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  5773. if ((ret = wc_HmacSizeByType(WC_SHA224)) != WC_SHA224_DIGEST_SIZE)
  5774. return WC_TEST_RET_ENC_EC(ret);
  5775. #endif
  5776. return 0;
  5777. }
  5778. #endif
  5779. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  5780. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha256_test(void)
  5781. {
  5782. Hmac hmac;
  5783. byte hash[WC_SHA256_DIGEST_SIZE];
  5784. const char* keys[]=
  5785. {
  5786. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5787. "\x0b\x0b\x0b",
  5788. "Jefe",
  5789. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5790. "\xAA\xAA\xAA",
  5791. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5792. "\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\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5797. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5798. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5799. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5800. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5801. "\xAA\xAA\xAA"
  5802. };
  5803. testVector a, b, c, d, e;
  5804. testVector test_hmac[5];
  5805. wc_test_ret_t ret;
  5806. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5807. WOLFSSL_ENTER("hmac_sha256_test");
  5808. /* Following test vectors are from RFC 4231 section 4 */
  5809. a.input = "Hi There";
  5810. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  5811. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  5812. "\xcf\xf7";
  5813. a.inLen = XSTRLEN(a.input);
  5814. a.outLen = WC_SHA256_DIGEST_SIZE;
  5815. b.input = "what do ya want for nothing?";
  5816. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  5817. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  5818. "\x38\x43";
  5819. b.inLen = XSTRLEN(b.input);
  5820. b.outLen = WC_SHA256_DIGEST_SIZE;
  5821. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5822. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5823. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5824. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5825. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  5826. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  5827. "\x65\xfe";
  5828. c.inLen = XSTRLEN(c.input);
  5829. c.outLen = WC_SHA256_DIGEST_SIZE;
  5830. d.input = 0;
  5831. d.output = "\x86\xe5\x4f\xd4\x48\x72\x5d\x7e\x5d\xcf\xe2\x23\x53\xc8\x28"
  5832. "\xaf\x48\x78\x1e\xb4\x8c\xae\x81\x06\xa7\xe1\xd4\x98\x94\x9f"
  5833. "\x3e\x46";
  5834. d.inLen = 0;
  5835. d.outLen = WC_SHA256_DIGEST_SIZE;
  5836. e.input = "Test Using Larger Than Block-Size Key - Hash Key First";
  5837. e.output = "\x60\xe4\x31\x59\x1e\xe0\xb6\x7f\x0d\x8a\x26\xaa\xcb\xf5\xb7"
  5838. "\x7f\x8e\x0b\xc6\x21\x37\x28\xc5\x14\x05\x46\x04\x0f\x0e\xe3"
  5839. "\x7f\x54";
  5840. e.inLen = XSTRLEN(e.input);;
  5841. e.outLen = WC_SHA256_DIGEST_SIZE;
  5842. test_hmac[0] = a;
  5843. test_hmac[1] = b;
  5844. test_hmac[2] = c;
  5845. test_hmac[3] = d;
  5846. test_hmac[4] = e;
  5847. for (i = 0; i < times; ++i) {
  5848. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  5849. if (i == 1)
  5850. continue; /* cavium can't handle short keys, fips not allowed */
  5851. #endif
  5852. #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
  5853. if (i == 3)
  5854. continue; /* QuickAssist can't handle empty HMAC */
  5855. #endif
  5856. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  5857. return WC_TEST_RET_ENC_I(i);
  5858. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[i],
  5859. (word32)XSTRLEN(keys[i]));
  5860. if (ret != 0)
  5861. return WC_TEST_RET_ENC_I(i);
  5862. if (test_hmac[i].input != NULL) {
  5863. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5864. (word32)test_hmac[i].inLen);
  5865. if (ret != 0)
  5866. return WC_TEST_RET_ENC_I(i);
  5867. }
  5868. ret = wc_HmacFinal(&hmac, hash);
  5869. if (ret != 0)
  5870. return WC_TEST_RET_ENC_I(i);
  5871. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  5872. return WC_TEST_RET_ENC_I(i);
  5873. wc_HmacFree(&hmac);
  5874. }
  5875. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  5876. if ((ret = wc_HmacSizeByType(WC_SHA256)) != WC_SHA256_DIGEST_SIZE)
  5877. return WC_TEST_RET_ENC_EC(ret);
  5878. #if FIPS_VERSION3_GE(6,0,0)
  5879. if ((ret = wc_HmacSizeByType(21)) != HMAC_KAT_FIPS_E)
  5880. #else
  5881. if ((ret = wc_HmacSizeByType(21)) != BAD_FUNC_ARG)
  5882. #endif
  5883. {
  5884. return WC_TEST_RET_ENC_EC(ret);
  5885. }
  5886. #endif
  5887. if ((ret = wolfSSL_GetHmacMaxSize()) != WC_MAX_DIGEST_SIZE)
  5888. return WC_TEST_RET_ENC_EC(ret);
  5889. return 0;
  5890. }
  5891. #endif
  5892. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  5893. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha384_test(void)
  5894. {
  5895. Hmac hmac;
  5896. byte hash[WC_SHA384_DIGEST_SIZE];
  5897. const char* keys[]=
  5898. {
  5899. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5900. "\x0b\x0b\x0b",
  5901. "Jefe",
  5902. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5903. "\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\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5908. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5909. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5910. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5911. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5912. "\xAA\xAA\xAA"
  5913. };
  5914. testVector a, b, c, d;
  5915. testVector test_hmac[4];
  5916. wc_test_ret_t ret;
  5917. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5918. WOLFSSL_ENTER("hmac_sha384_test");
  5919. /* Following test vectors are from RFC 4231 section 4 */
  5920. a.input = "Hi There";
  5921. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  5922. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  5923. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  5924. "\xfa\x9c\xb6";
  5925. a.inLen = XSTRLEN(a.input);
  5926. a.outLen = WC_SHA384_DIGEST_SIZE;
  5927. b.input = "what do ya want for nothing?";
  5928. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  5929. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  5930. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  5931. "\xb2\x16\x49";
  5932. b.inLen = XSTRLEN(b.input);
  5933. b.outLen = WC_SHA384_DIGEST_SIZE;
  5934. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5935. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5936. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5937. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5938. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  5939. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  5940. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  5941. "\xa3\x4f\x27";
  5942. c.inLen = XSTRLEN(c.input);
  5943. c.outLen = WC_SHA384_DIGEST_SIZE;
  5944. d.input = "Test Using Larger Than Block-Size Key - Hash Key First";
  5945. d.output = "\x4e\xce\x08\x44\x85\x81\x3e\x90\x88\xd2\xc6\x3a\x04\x1b\xc5"
  5946. "\xb4\x4f\x9e\xf1\x01\x2a\x2b\x58\x8f\x3c\xd1\x1f\x05\x03\x3a"
  5947. "\xc4\xc6\x0c\x2e\xf6\xab\x40\x30\xfe\x82\x96\x24\x8d\xf1\x63"
  5948. "\xf4\x49\x52";
  5949. d.inLen = XSTRLEN(d.input);
  5950. d.outLen = WC_SHA384_DIGEST_SIZE;
  5951. test_hmac[0] = a;
  5952. test_hmac[1] = b;
  5953. test_hmac[2] = c;
  5954. test_hmac[3] = d;
  5955. for (i = 0; i < times; ++i) {
  5956. #if defined(HAVE_FIPS)
  5957. if (i == 1)
  5958. continue; /* fips not allowed */
  5959. #endif
  5960. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5961. return WC_TEST_RET_ENC_EC(ret);
  5962. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[i],
  5963. (word32)XSTRLEN(keys[i]));
  5964. if (ret != 0)
  5965. return WC_TEST_RET_ENC_EC(ret);
  5966. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5967. (word32)test_hmac[i].inLen);
  5968. if (ret != 0)
  5969. return WC_TEST_RET_ENC_EC(ret);
  5970. ret = wc_HmacFinal(&hmac, hash);
  5971. if (ret != 0)
  5972. return WC_TEST_RET_ENC_EC(ret);
  5973. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  5974. return WC_TEST_RET_ENC_I(i);
  5975. wc_HmacFree(&hmac);
  5976. }
  5977. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  5978. if ((ret = wc_HmacSizeByType(WC_SHA384)) != WC_SHA384_DIGEST_SIZE)
  5979. return WC_TEST_RET_ENC_EC(ret);
  5980. #endif
  5981. return 0;
  5982. }
  5983. #endif
  5984. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA512)
  5985. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha512_test(void)
  5986. {
  5987. Hmac hmac;
  5988. byte hash[WC_SHA512_DIGEST_SIZE];
  5989. const char* keys[]=
  5990. {
  5991. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5992. "\x0b\x0b\x0b",
  5993. "Jefe",
  5994. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5995. "\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\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  6000. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  6001. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  6002. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  6003. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  6004. "\xAA\xAA\xAA"
  6005. };
  6006. testVector a, b, c, d;
  6007. testVector test_hmac[4];
  6008. wc_test_ret_t ret;
  6009. int times = sizeof(test_hmac) / sizeof(testVector), i;
  6010. WOLFSSL_ENTER("hmac_sha512_test");
  6011. /* Following test vectors are from RFC 4231 section 4 */
  6012. a.input = "Hi There";
  6013. a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
  6014. "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
  6015. "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
  6016. "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
  6017. "\x3a\x12\x68\x54";
  6018. a.inLen = XSTRLEN(a.input);
  6019. a.outLen = WC_SHA512_DIGEST_SIZE;
  6020. b.input = "what do ya want for nothing?";
  6021. b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
  6022. "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
  6023. "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
  6024. "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
  6025. "\x38\xbc\xe7\x37";
  6026. b.inLen = XSTRLEN(b.input);
  6027. b.outLen = WC_SHA512_DIGEST_SIZE;
  6028. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  6029. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  6030. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  6031. "\xDD\xDD\xDD\xDD\xDD\xDD";
  6032. c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
  6033. "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
  6034. "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
  6035. "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
  6036. "\xe1\x32\x92\xfb";
  6037. c.inLen = XSTRLEN(c.input);
  6038. c.outLen = WC_SHA512_DIGEST_SIZE;
  6039. d.input = "Test Using Larger Than Block-Size Key - Hash Key First";
  6040. d.output = "\x80\xb2\x42\x63\xc7\xc1\xa3\xeb\xb7\x14\x93\xc1\xdd\x7b\xe8"
  6041. "\xb4\x9b\x46\xd1\xf4\x1b\x4a\xee\xc1\x12\x1b\x01\x37\x83\xf8"
  6042. "\xf3\x52\x6b\x56\xd0\x37\xe0\x5f\x25\x98\xbd\x0f\xd2\x21\x5d"
  6043. "\x6a\x1e\x52\x95\xe6\x4f\x73\xf6\x3f\x0a\xec\x8b\x91\x5a\x98"
  6044. "\x5d\x78\x65\x98";
  6045. d.inLen = XSTRLEN(d.input);
  6046. d.outLen = WC_SHA512_DIGEST_SIZE;
  6047. test_hmac[0] = a;
  6048. test_hmac[1] = b;
  6049. test_hmac[2] = c;
  6050. test_hmac[3] = d;
  6051. for (i = 0; i < times; ++i) {
  6052. #if defined(HAVE_FIPS)
  6053. if (i == 1)
  6054. continue; /* fips not allowed */
  6055. #endif
  6056. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  6057. return WC_TEST_RET_ENC_EC(ret);
  6058. ret = wc_HmacSetKey(&hmac, WC_SHA512, (byte*)keys[i],
  6059. (word32)XSTRLEN(keys[i]));
  6060. if (ret != 0)
  6061. return WC_TEST_RET_ENC_EC(ret);
  6062. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  6063. (word32)test_hmac[i].inLen);
  6064. if (ret != 0)
  6065. return WC_TEST_RET_ENC_EC(ret);
  6066. ret = wc_HmacFinal(&hmac, hash);
  6067. if (ret != 0)
  6068. return WC_TEST_RET_ENC_EC(ret);
  6069. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  6070. return WC_TEST_RET_ENC_I(i);
  6071. wc_HmacFree(&hmac);
  6072. }
  6073. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  6074. if ((ret = wc_HmacSizeByType(WC_SHA512)) != WC_SHA512_DIGEST_SIZE)
  6075. return WC_TEST_RET_ENC_EC(ret);
  6076. #endif
  6077. return 0;
  6078. }
  6079. #endif
  6080. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  6081. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  6082. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  6083. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha3_test(void)
  6084. {
  6085. Hmac hmac;
  6086. byte hash[WC_SHA3_512_DIGEST_SIZE];
  6087. const char* key[4] =
  6088. {
  6089. "Jefe",
  6090. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  6091. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  6092. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  6093. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  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. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6100. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6101. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6102. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6103. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  6104. };
  6105. const char* input[4] =
  6106. {
  6107. "what do ya want for nothing?",
  6108. "Hi There",
  6109. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  6110. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  6111. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  6112. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  6113. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
  6114. "Big Key Input"
  6115. };
  6116. const int hashType[4] =
  6117. {
  6118. WC_SHA3_224, WC_SHA3_256, WC_SHA3_384, WC_SHA3_512
  6119. };
  6120. const int hashSz[4] =
  6121. {
  6122. WC_SHA3_224_DIGEST_SIZE, WC_SHA3_256_DIGEST_SIZE,
  6123. WC_SHA3_384_DIGEST_SIZE, WC_SHA3_512_DIGEST_SIZE
  6124. };
  6125. const char* output[16] =
  6126. {
  6127. /* key = jefe, input = what do ya want for nothing? */
  6128. /* HMAC-SHA3-224 */
  6129. "\x7f\xdb\x8d\xd8\x8b\xd2\xf6\x0d\x1b\x79\x86\x34\xad\x38\x68\x11"
  6130. "\xc2\xcf\xc8\x5b\xfa\xf5\xd5\x2b\xba\xce\x5e\x66",
  6131. /* HMAC-SHA3-256 */
  6132. "\xc7\xd4\x07\x2e\x78\x88\x77\xae\x35\x96\xbb\xb0\xda\x73\xb8\x87"
  6133. "\xc9\x17\x1f\x93\x09\x5b\x29\x4a\xe8\x57\xfb\xe2\x64\x5e\x1b\xa5",
  6134. /* HMAC-SHA3-384 */
  6135. "\xf1\x10\x1f\x8c\xbf\x97\x66\xfd\x67\x64\xd2\xed\x61\x90\x3f\x21"
  6136. "\xca\x9b\x18\xf5\x7c\xf3\xe1\xa2\x3c\xa1\x35\x08\xa9\x32\x43\xce"
  6137. "\x48\xc0\x45\xdc\x00\x7f\x26\xa2\x1b\x3f\x5e\x0e\x9d\xf4\xc2\x0a",
  6138. /* HMAC-SHA3-512 */
  6139. "\x5a\x4b\xfe\xab\x61\x66\x42\x7c\x7a\x36\x47\xb7\x47\x29\x2b\x83"
  6140. "\x84\x53\x7c\xdb\x89\xaf\xb3\xbf\x56\x65\xe4\xc5\xe7\x09\x35\x0b"
  6141. "\x28\x7b\xae\xc9\x21\xfd\x7c\xa0\xee\x7a\x0c\x31\xd0\x22\xa9\x5e"
  6142. "\x1f\xc9\x2b\xa9\xd7\x7d\xf8\x83\x96\x02\x75\xbe\xb4\xe6\x20\x24",
  6143. /* key = 0b..., input = Hi There */
  6144. /* HMAC-SHA3-224 */
  6145. "\x3b\x16\x54\x6b\xbc\x7b\xe2\x70\x6a\x03\x1d\xca\xfd\x56\x37\x3d"
  6146. "\x98\x84\x36\x76\x41\xd8\xc5\x9a\xf3\xc8\x60\xf7",
  6147. /* HMAC-SHA3-256 */
  6148. "\xba\x85\x19\x23\x10\xdf\xfa\x96\xe2\xa3\xa4\x0e\x69\x77\x43\x51"
  6149. "\x14\x0b\xb7\x18\x5e\x12\x02\xcd\xcc\x91\x75\x89\xf9\x5e\x16\xbb",
  6150. /* HMAC-SHA3-384 */
  6151. "\x68\xd2\xdc\xf7\xfd\x4d\xdd\x0a\x22\x40\xc8\xa4\x37\x30\x5f\x61"
  6152. "\xfb\x73\x34\xcf\xb5\xd0\x22\x6e\x1b\xc2\x7d\xc1\x0a\x2e\x72\x3a"
  6153. "\x20\xd3\x70\xb4\x77\x43\x13\x0e\x26\xac\x7e\x3d\x53\x28\x86\xbd",
  6154. /* HMAC-SHA3-512 */
  6155. "\xeb\x3f\xbd\x4b\x2e\xaa\xb8\xf5\xc5\x04\xbd\x3a\x41\x46\x5a\xac"
  6156. "\xec\x15\x77\x0a\x7c\xab\xac\x53\x1e\x48\x2f\x86\x0b\x5e\xc7\xba"
  6157. "\x47\xcc\xb2\xc6\xf2\xaf\xce\x8f\x88\xd2\x2b\x6d\xc6\x13\x80\xf2"
  6158. "\x3a\x66\x8f\xd3\x88\x8b\xb8\x05\x37\xc0\xa0\xb8\x64\x07\x68\x9e",
  6159. /* key = aa..., output = dd... */
  6160. /* HMAC-SHA3-224 */
  6161. "\x67\x6c\xfc\x7d\x16\x15\x36\x38\x78\x03\x90\x69\x2b\xe1\x42\xd2"
  6162. "\xdf\x7c\xe9\x24\xb9\x09\xc0\xc0\x8d\xbf\xdc\x1a",
  6163. /* HMAC-SHA3-256 */
  6164. "\x84\xec\x79\x12\x4a\x27\x10\x78\x65\xce\xdd\x8b\xd8\x2d\xa9\x96"
  6165. "\x5e\x5e\xd8\xc3\x7b\x0a\xc9\x80\x05\xa7\xf3\x9e\xd5\x8a\x42\x07",
  6166. /* HMAC-SHA3-384 */
  6167. "\x27\x5c\xd0\xe6\x61\xbb\x8b\x15\x1c\x64\xd2\x88\xf1\xf7\x82\xfb"
  6168. "\x91\xa8\xab\xd5\x68\x58\xd7\x2b\xab\xb2\xd4\x76\xf0\x45\x83\x73"
  6169. "\xb4\x1b\x6a\xb5\xbf\x17\x4b\xec\x42\x2e\x53\xfc\x31\x35\xac\x6e",
  6170. /* HMAC-SHA3-512 */
  6171. "\x30\x9e\x99\xf9\xec\x07\x5e\xc6\xc6\xd4\x75\xed\xa1\x18\x06\x87"
  6172. "\xfc\xf1\x53\x11\x95\x80\x2a\x99\xb5\x67\x74\x49\xa8\x62\x51\x82"
  6173. "\x85\x1c\xb3\x32\xaf\xb6\xa8\x9c\x41\x13\x25\xfb\xcb\xcd\x42\xaf"
  6174. "\xcb\x7b\x6e\x5a\xab\x7e\xa4\x2c\x66\x0f\x97\xfd\x85\x84\xbf\x03",
  6175. /* key = big key, input = Big Key Input */
  6176. /* HMAC-SHA3-224 */
  6177. "\x29\xe0\x5e\x46\xc4\xa4\x5e\x46\x74\xbf\xd7\x2d\x1a\xd8\x66\xdb"
  6178. "\x2d\x0d\x10\x4e\x2b\xfa\xad\x53\x7d\x15\x69\x8b",
  6179. /* HMAC-SHA3-256 */
  6180. "\xb5\x5b\x8d\x64\xb6\x9c\x21\xd0\xbf\x20\x5c\xa2\xf7\xb9\xb1\x4e"
  6181. "\x88\x21\x61\x2c\x66\xc3\x91\xae\x6c\x95\x16\x85\x83\xe6\xf4\x9b",
  6182. /* HMAC-SHA3-384 */
  6183. "\xaa\x91\xb3\xa6\x2f\x56\xa1\xbe\x8c\x3e\x74\x38\xdb\x58\xd9\xd3"
  6184. "\x34\xde\xa0\x60\x6d\x8d\x46\xe0\xec\xa9\xf6\x06\x35\x14\xe6\xed"
  6185. "\x83\xe6\x7c\x77\x24\x6c\x11\xb5\x90\x82\xb5\x75\xda\x7b\x83\x2d",
  6186. /* HMAC-SHA3-512 */
  6187. "\x1c\xc3\xa9\x24\x4a\x4a\x3f\xbd\xc7\x20\x00\x16\x9b\x79\x47\x03"
  6188. "\x78\x75\x2c\xb5\xf1\x2e\x62\x7c\xbe\xef\x4e\x8f\x0b\x11\x2b\x32"
  6189. "\xa0\xee\xc9\xd0\x4d\x64\x64\x0b\x37\xf4\xdd\x66\xf7\x8b\xb3\xad"
  6190. "\x52\x52\x6b\x65\x12\xde\x0d\x7c\xc0\x8b\x60\x01\x6c\x37\xd7\xa8"
  6191. };
  6192. int i = 0, iMax = sizeof(input) / sizeof(input[0]),
  6193. j, jMax = sizeof(hashType) / sizeof(hashType[0]);
  6194. int ret;
  6195. WOLFSSL_ENTER("hmac_sha3_test");
  6196. #ifdef HAVE_FIPS
  6197. /* FIPS requires a minimum length for HMAC keys, and "Jefe" is too
  6198. * short. Skip it in FIPS builds. */
  6199. i = 1;
  6200. #endif
  6201. for (; i < iMax; i++) {
  6202. for (j = 0; j < jMax; j++) {
  6203. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  6204. return WC_TEST_RET_ENC_EC(ret);
  6205. ret = wc_HmacSetKey(&hmac, hashType[j], (byte*)key[i],
  6206. (word32)XSTRLEN(key[i]));
  6207. if (ret != 0)
  6208. return WC_TEST_RET_ENC_EC(ret);
  6209. ret = wc_HmacUpdate(&hmac, (byte*)input[i],
  6210. (word32)XSTRLEN(input[i]));
  6211. if (ret != 0)
  6212. return WC_TEST_RET_ENC_EC(ret);
  6213. ret = wc_HmacFinal(&hmac, hash);
  6214. if (ret != 0)
  6215. return WC_TEST_RET_ENC_EC(ret);
  6216. if (XMEMCMP(hash, output[(i*jMax) + j], (size_t)hashSz[j]) != 0)
  6217. return WC_TEST_RET_ENC_NC;
  6218. wc_HmacFree(&hmac);
  6219. if (i > 0)
  6220. continue;
  6221. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  6222. ret = wc_HmacSizeByType(hashType[j]);
  6223. if (ret != hashSz[j])
  6224. return WC_TEST_RET_ENC_EC(ret);
  6225. #endif
  6226. }
  6227. }
  6228. return 0;
  6229. }
  6230. #endif
  6231. #ifdef WC_RC2
  6232. typedef struct rc2TestVector {
  6233. const char* input;
  6234. const char* output;
  6235. const char* key; /* Key, variable up to 128 bytes */
  6236. const char* iv; /* IV, 8-bytes */
  6237. int inLen;
  6238. int outLen;
  6239. int keyLen;
  6240. int effectiveKeyBits; /* Up to 1024 bits supported */
  6241. } rc2TestVector;
  6242. static wc_test_ret_t rc2_ecb_test(void)
  6243. {
  6244. wc_test_ret_t ret = 0;
  6245. byte cipher[RC2_BLOCK_SIZE];
  6246. byte plain[RC2_BLOCK_SIZE];
  6247. rc2TestVector a, b, c, d, e, f, g, h;
  6248. rc2TestVector test_rc2[8];
  6249. int times = sizeof(test_rc2) / sizeof(rc2TestVector), i;
  6250. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6251. a.output = "\xeb\xb7\x73\xf9\x93\x27\x8e\xff";
  6252. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6253. a.inLen = RC2_BLOCK_SIZE;
  6254. a.outLen = RC2_BLOCK_SIZE;
  6255. a.keyLen = 8;
  6256. a.effectiveKeyBits = 63;
  6257. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff";
  6258. b.output = "\x27\x8b\x27\xe4\x2e\x2f\x0d\x49";
  6259. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  6260. b.inLen = RC2_BLOCK_SIZE;
  6261. b.outLen = RC2_BLOCK_SIZE;
  6262. b.keyLen = 8;
  6263. b.effectiveKeyBits = 64;
  6264. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01";
  6265. c.output = "\x30\x64\x9e\xdf\x9b\xe7\xd2\xc2";
  6266. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  6267. c.inLen = RC2_BLOCK_SIZE;
  6268. c.outLen = RC2_BLOCK_SIZE;
  6269. c.keyLen = 8;
  6270. c.effectiveKeyBits = 64;
  6271. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6272. d.output = "\x61\xa8\xa2\x44\xad\xac\xcc\xf0";
  6273. d.key = "\x88";
  6274. d.inLen = RC2_BLOCK_SIZE;
  6275. d.outLen = RC2_BLOCK_SIZE;
  6276. d.keyLen = 1;
  6277. d.effectiveKeyBits = 64;
  6278. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6279. e.output = "\x6c\xcf\x43\x08\x97\x4c\x26\x7f";
  6280. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  6281. e.inLen = RC2_BLOCK_SIZE;
  6282. e.outLen = RC2_BLOCK_SIZE;
  6283. e.keyLen = 7;
  6284. e.effectiveKeyBits = 64;
  6285. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6286. f.output = "\x1a\x80\x7d\x27\x2b\xbe\x5d\xb1";
  6287. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  6288. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  6289. f.inLen = RC2_BLOCK_SIZE;
  6290. f.outLen = RC2_BLOCK_SIZE;
  6291. f.keyLen = 16;
  6292. f.effectiveKeyBits = 64;
  6293. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6294. g.output = "\x22\x69\x55\x2a\xb0\xf8\x5c\xa6";
  6295. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  6296. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  6297. g.inLen = RC2_BLOCK_SIZE;
  6298. g.outLen = RC2_BLOCK_SIZE;
  6299. g.keyLen = 16;
  6300. g.effectiveKeyBits = 128;
  6301. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6302. h.output = "\x5b\x78\xd3\xa4\x3d\xff\xf1\xf1";
  6303. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  6304. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  6305. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  6306. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  6307. "\x1e";
  6308. h.inLen = RC2_BLOCK_SIZE;
  6309. h.outLen = RC2_BLOCK_SIZE;
  6310. h.keyLen = 33;
  6311. h.effectiveKeyBits = 129;
  6312. a.iv = b.iv = c.iv = d.iv = e.iv = f.iv = g.iv = h.iv = NULL;
  6313. test_rc2[0] = a;
  6314. test_rc2[1] = b;
  6315. test_rc2[2] = c;
  6316. test_rc2[3] = d;
  6317. test_rc2[4] = e;
  6318. test_rc2[5] = f;
  6319. test_rc2[6] = g;
  6320. test_rc2[7] = h;
  6321. for (i = 0; i < times; ++i) {
  6322. Rc2 enc;
  6323. XMEMSET(cipher, 0, RC2_BLOCK_SIZE);
  6324. XMEMSET(plain, 0, RC2_BLOCK_SIZE);
  6325. ret = wc_Rc2SetKey(&enc, (byte*)test_rc2[i].key, test_rc2[i].keyLen,
  6326. NULL, test_rc2[i].effectiveKeyBits);
  6327. if (ret != 0) {
  6328. return WC_TEST_RET_ENC_EC(ret);
  6329. }
  6330. /* ECB encrypt */
  6331. ret = wc_Rc2EcbEncrypt(&enc, cipher, (byte*)test_rc2[i].input,
  6332. (word32)test_rc2[i].outLen);
  6333. if (ret != 0) {
  6334. return WC_TEST_RET_ENC_EC(ret);
  6335. }
  6336. if (XMEMCMP(cipher, test_rc2[i].output, test_rc2[i].outLen)) {
  6337. return WC_TEST_RET_ENC_NC;
  6338. }
  6339. /* ECB decrypt */
  6340. ret = wc_Rc2EcbDecrypt(&enc, plain, cipher, RC2_BLOCK_SIZE);
  6341. if (ret != 0) {
  6342. return WC_TEST_RET_ENC_EC(ret);
  6343. }
  6344. if (XMEMCMP(plain, test_rc2[i].input, RC2_BLOCK_SIZE)) {
  6345. return WC_TEST_RET_ENC_NC;
  6346. }
  6347. }
  6348. return 0;
  6349. }
  6350. static wc_test_ret_t rc2_cbc_test(void)
  6351. {
  6352. wc_test_ret_t ret = 0;
  6353. byte cipher[128];
  6354. byte plain[128];
  6355. rc2TestVector a, b, c, d, e, f, g, h, i;
  6356. rc2TestVector test_rc2[9];
  6357. int times = sizeof(test_rc2) / sizeof(rc2TestVector), j;
  6358. /* key length = 7, effective key bits = 63 */
  6359. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  6360. "\x00\x00\x00\x00\x00\x00\x00\x00";
  6361. a.output = "\xEB\xB7\x73\xF9\x93\x27\x8E\xFF"
  6362. "\xF0\x51\x77\x8B\x65\xDB\x13\x57";
  6363. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6364. a.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6365. a.inLen = RC2_BLOCK_SIZE*2;
  6366. a.outLen = RC2_BLOCK_SIZE*2;
  6367. a.keyLen = 8;
  6368. a.effectiveKeyBits = 63;
  6369. /* key length = 8, effective key bits = 64, all 0xFF */
  6370. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff"
  6371. "\xff\xff\xff\xff\xff\xff\xff\xff";
  6372. b.output = "\xA3\xA1\x12\x65\x4F\x81\xC5\xCD"
  6373. "\xB6\x94\x3E\xEA\x3E\x8B\x9D\x1F";
  6374. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  6375. b.iv = "\xff\xff\xff\xff\xff\xff\xff\xff";
  6376. b.inLen = RC2_BLOCK_SIZE*2;
  6377. b.outLen = RC2_BLOCK_SIZE*2;
  6378. b.keyLen = 8;
  6379. b.effectiveKeyBits = 64;
  6380. /* key length = 8, effective key bits = 64 */
  6381. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01"
  6382. "\x10\x00\x00\x00\x00\x00\x00\x01";
  6383. c.output = "\xB5\x70\x14\xA2\x5F\x40\xE3\x6D"
  6384. "\x81\x99\x8D\xE0\xB5\xD5\x3A\x05";
  6385. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  6386. c.iv = "\x30\x00\x00\x00\x00\x00\x00\x00";
  6387. c.inLen = RC2_BLOCK_SIZE*2;
  6388. c.outLen = RC2_BLOCK_SIZE*2;
  6389. c.keyLen = 8;
  6390. c.effectiveKeyBits = 64;
  6391. /* key length = 1, effective key bits = 64 */
  6392. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  6393. "\x00\x00\x00\x00\x00\x00\x00\x00";
  6394. d.output = "\x61\xA8\xA2\x44\xAD\xAC\xCC\xF0"
  6395. "\x6D\x19\xE8\xF1\xFC\xE7\x38\x87";
  6396. d.key = "\x88";
  6397. d.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6398. d.inLen = RC2_BLOCK_SIZE*2;
  6399. d.outLen = RC2_BLOCK_SIZE*2;
  6400. d.keyLen = 1;
  6401. d.effectiveKeyBits = 64;
  6402. /* key length = 7, effective key bits = 64 */
  6403. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  6404. "\x00\x00\x00\x00\x00\x00\x00\x00";
  6405. e.output = "\x6C\xCF\x43\x08\x97\x4C\x26\x7F"
  6406. "\xCC\x3C\x53\x57\x7C\xA1\xA4\x4B";
  6407. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  6408. e.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6409. e.inLen = RC2_BLOCK_SIZE*2;
  6410. e.outLen = RC2_BLOCK_SIZE*2;
  6411. e.keyLen = 7;
  6412. e.effectiveKeyBits = 64;
  6413. /* key length = 16, effective key bits = 64 */
  6414. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  6415. "\x00\x00\x00\x00\x00\x00\x00\x00";
  6416. f.output = "\x1A\x80\x7D\x27\x2B\xBE\x5D\xB1"
  6417. "\x64\xEF\xE1\xC3\xB8\xAD\xFB\xBA";
  6418. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  6419. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  6420. f.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6421. f.inLen = RC2_BLOCK_SIZE*2;
  6422. f.outLen = RC2_BLOCK_SIZE*2;
  6423. f.keyLen = 16;
  6424. f.effectiveKeyBits = 64;
  6425. /* key length = 16, effective bits = 128 */
  6426. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  6427. "\x00\x00\x00\x00\x00\x00\x00\x00";
  6428. g.output = "\x22\x69\x55\x2A\xB0\xF8\x5C\xA6"
  6429. "\x53\x6E\xFD\x2D\x89\xE1\x2A\x73";
  6430. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  6431. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  6432. g.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6433. g.inLen = RC2_BLOCK_SIZE*2;
  6434. g.outLen = RC2_BLOCK_SIZE*2;
  6435. g.keyLen = 16;
  6436. g.effectiveKeyBits = 128;
  6437. /* key length = 33, effective bits = 129 */
  6438. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  6439. "\x00\x00\x00\x00\x00\x00\x00\x00";
  6440. h.output = "\x5B\x78\xD3\xA4\x3D\xFF\xF1\xF1"
  6441. "\x45\x30\xA8\xD5\xC7\x7C\x46\x19";
  6442. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  6443. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  6444. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  6445. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  6446. "\x1e";
  6447. h.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6448. h.inLen = RC2_BLOCK_SIZE*2;
  6449. h.outLen = RC2_BLOCK_SIZE*2;
  6450. h.keyLen = 33;
  6451. h.effectiveKeyBits = 129;
  6452. /* key length = 10, effective bits = 40 */
  6453. i.input = "\x11\x22\x33\x44\x55\x66\x77\x88"
  6454. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00"
  6455. "\x11\x22\x33\x44\x55\x66\x77\x88"
  6456. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00";
  6457. i.output = "\x71\x2D\x11\x99\xC9\xA0\x78\x4F"
  6458. "\xCD\xF1\x1E\x3D\xFD\x21\x7E\xDB"
  6459. "\xB2\x6E\x0D\xA4\x72\xBC\x31\x51"
  6460. "\x48\xEF\x4E\x68\x3B\xDC\xCD\x7D";
  6461. i.key = "\x26\x1E\x57\x8E\xC9\x62\xBF\xB8"
  6462. "\x3E\x96";
  6463. i.iv = "\x01\x02\x03\x04\x05\x06\x07\x08";
  6464. i.inLen = RC2_BLOCK_SIZE*4;
  6465. i.outLen = RC2_BLOCK_SIZE*4;
  6466. i.keyLen = 10;
  6467. i.effectiveKeyBits = 40;
  6468. test_rc2[0] = a;
  6469. test_rc2[1] = b;
  6470. test_rc2[2] = c;
  6471. test_rc2[3] = d;
  6472. test_rc2[4] = e;
  6473. test_rc2[5] = f;
  6474. test_rc2[6] = g;
  6475. test_rc2[7] = h;
  6476. test_rc2[8] = i;
  6477. for (j = 0; j < times; ++j) {
  6478. Rc2 rc2;
  6479. XMEMSET(cipher, 0, sizeof(cipher));
  6480. XMEMSET(plain, 0, sizeof(plain));
  6481. ret = wc_Rc2SetKey(&rc2, (byte*)test_rc2[j].key, test_rc2[j].keyLen,
  6482. (byte*)test_rc2[j].iv, test_rc2[j].effectiveKeyBits);
  6483. if (ret != 0) {
  6484. return WC_TEST_RET_ENC_EC(ret);
  6485. }
  6486. ret = wc_Rc2CbcEncrypt(&rc2, cipher, (byte*)test_rc2[j].input,
  6487. test_rc2[j].inLen);
  6488. if (ret != 0) {
  6489. return WC_TEST_RET_ENC_EC(ret);
  6490. }
  6491. if (XMEMCMP(cipher, (byte*)test_rc2[j].output, test_rc2[j].outLen)) {
  6492. return WC_TEST_RET_ENC_NC;
  6493. }
  6494. /* reset IV for decrypt, since overridden by encrypt operation */
  6495. ret = wc_Rc2SetIV(&rc2, (byte*)test_rc2[j].iv);
  6496. if (ret != 0) {
  6497. return WC_TEST_RET_ENC_EC(ret);
  6498. }
  6499. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, test_rc2[j].outLen);
  6500. if (ret != 0) {
  6501. return WC_TEST_RET_ENC_EC(ret);
  6502. }
  6503. if (XMEMCMP(plain, (byte*)test_rc2[j].input, test_rc2[j].inLen)) {
  6504. return WC_TEST_RET_ENC_NC;
  6505. }
  6506. }
  6507. return 0;
  6508. }
  6509. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rc2_test(void)
  6510. {
  6511. wc_test_ret_t ret = 0;
  6512. WOLFSSL_ENTER("rc2_test");
  6513. ret = rc2_ecb_test();
  6514. if (ret != 0) {
  6515. return ret;
  6516. }
  6517. return rc2_cbc_test();
  6518. }
  6519. #endif
  6520. #ifndef NO_RC4
  6521. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t arc4_test(void)
  6522. {
  6523. byte cipher[16];
  6524. byte plain[16];
  6525. wc_test_ret_t ret;
  6526. const char* keys[] =
  6527. {
  6528. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  6529. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  6530. "\x00\x00\x00\x00\x00\x00\x00\x00",
  6531. "\xef\x01\x23\x45"
  6532. };
  6533. testVector a, b, c, d;
  6534. testVector test_arc4[4];
  6535. int times = sizeof(test_arc4) / sizeof(testVector), i;
  6536. WOLFSSL_ENTER("arc4_test");
  6537. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  6538. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  6539. a.inLen = 8;
  6540. a.outLen = 8;
  6541. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6542. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  6543. b.inLen = 8;
  6544. b.outLen = 8;
  6545. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  6546. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  6547. c.inLen = 8;
  6548. c.outLen = 8;
  6549. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  6550. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  6551. d.inLen = 10;
  6552. d.outLen = 10;
  6553. test_arc4[0] = a;
  6554. test_arc4[1] = b;
  6555. test_arc4[2] = c;
  6556. test_arc4[3] = d;
  6557. for (i = 0; i < times; ++i) {
  6558. Arc4 enc;
  6559. Arc4 dec;
  6560. int keylen = 8; /* XSTRLEN with key 0x00 not good */
  6561. if (i == 3)
  6562. keylen = 4;
  6563. ret = wc_Arc4Init(&enc, HEAP_HINT, devId);
  6564. if (ret != 0)
  6565. return WC_TEST_RET_ENC_EC(ret);
  6566. ret = wc_Arc4Init(&dec, HEAP_HINT, devId);
  6567. if (ret != 0)
  6568. return WC_TEST_RET_ENC_EC(ret);
  6569. ret = wc_Arc4SetKey(&enc, (byte*)keys[i], (word32)keylen);
  6570. if (ret != 0)
  6571. return WC_TEST_RET_ENC_EC(ret);
  6572. ret = wc_Arc4SetKey(&dec, (byte*)keys[i], (word32)keylen);
  6573. if (ret != 0)
  6574. return WC_TEST_RET_ENC_EC(ret);
  6575. ret = wc_Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  6576. (word32)test_arc4[i].outLen);
  6577. if (ret != 0)
  6578. return WC_TEST_RET_ENC_EC(ret);
  6579. ret = wc_Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  6580. if (ret != 0)
  6581. return WC_TEST_RET_ENC_EC(ret);
  6582. if (XMEMCMP(plain, test_arc4[i].input, test_arc4[i].outLen))
  6583. return WC_TEST_RET_ENC_I(i);
  6584. if (XMEMCMP(cipher, test_arc4[i].output, test_arc4[i].outLen))
  6585. return WC_TEST_RET_ENC_I(i);
  6586. wc_Arc4Free(&enc);
  6587. wc_Arc4Free(&dec);
  6588. }
  6589. return 0;
  6590. }
  6591. #endif
  6592. #ifdef HAVE_CHACHA
  6593. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha_test(void)
  6594. {
  6595. ChaCha enc;
  6596. ChaCha dec;
  6597. byte cipher[128];
  6598. byte plain[128];
  6599. byte sliver[64];
  6600. byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  6601. word32 keySz = 32;
  6602. wc_test_ret_t ret = 0;
  6603. int i;
  6604. int times = 4;
  6605. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6606. {
  6607. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6608. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6609. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6610. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6611. };
  6612. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6613. {
  6614. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6615. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6616. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6617. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  6618. };
  6619. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6620. {
  6621. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6622. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6623. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6624. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6625. };
  6626. /* 128 bit key */
  6627. WOLFSSL_SMALL_STACK_STATIC const byte key4[] =
  6628. {
  6629. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6630. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6631. };
  6632. const byte* keys[] = {key1, key2, key3, key4};
  6633. WOLFSSL_SMALL_STACK_STATIC const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  6634. WOLFSSL_SMALL_STACK_STATIC const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  6635. WOLFSSL_SMALL_STACK_STATIC const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};
  6636. WOLFSSL_SMALL_STACK_STATIC const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  6637. const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4};
  6638. #ifndef BENCH_EMBEDDED
  6639. WOLFSSL_SMALL_STACK_STATIC const byte cipher_big_result[] = {
  6640. 0x06, 0xa6, 0x5d, 0x31, 0x21, 0x6c, 0xdb, 0x37, 0x48, 0x7c, 0x01, 0x9d,
  6641. 0x72, 0xdf, 0x0a, 0x5b, 0x64, 0x74, 0x20, 0xba, 0x9e, 0xe0, 0x26, 0x7a,
  6642. 0xbf, 0xdf, 0x83, 0x34, 0x3b, 0x4f, 0x94, 0x3f, 0x37, 0x89, 0xaf, 0x00,
  6643. 0xdf, 0x0f, 0x2e, 0x75, 0x16, 0x41, 0xf6, 0x7a, 0x86, 0x94, 0x9d, 0x32,
  6644. 0x56, 0xf0, 0x79, 0x71, 0x68, 0x6f, 0xa6, 0x6b, 0xc6, 0x59, 0x49, 0xf6,
  6645. 0x10, 0x34, 0x03, 0x03, 0x16, 0x53, 0x9a, 0x98, 0x2a, 0x46, 0xde, 0x17,
  6646. 0x06, 0x65, 0x70, 0xca, 0x0a, 0x1f, 0xab, 0x80, 0x26, 0x96, 0x3f, 0x3e,
  6647. 0x7a, 0x3c, 0xa8, 0x87, 0xbb, 0x65, 0xdd, 0x5e, 0x07, 0x7b, 0x34, 0xe0,
  6648. 0x56, 0xda, 0x32, 0x13, 0x30, 0xc9, 0x0c, 0xd7, 0xba, 0xe4, 0x1f, 0xa6,
  6649. 0x91, 0x4f, 0x72, 0x9f, 0xd9, 0x5c, 0x62, 0x7d, 0xa6, 0xc2, 0xbc, 0x87,
  6650. 0xae, 0x64, 0x11, 0x94, 0x3b, 0xbc, 0x6c, 0x23, 0xbd, 0x7d, 0x00, 0xb4,
  6651. 0x99, 0xf2, 0x68, 0xb5, 0x59, 0x70, 0x93, 0xad, 0x69, 0xd0, 0xb1, 0x28,
  6652. 0x70, 0x92, 0xeb, 0xec, 0x39, 0x80, 0x82, 0xde, 0x44, 0xe2, 0x8a, 0x26,
  6653. 0xb3, 0xe9, 0x45, 0xcf, 0x83, 0x76, 0x9f, 0x6a, 0xa0, 0x46, 0x4a, 0x3d,
  6654. 0x26, 0x56, 0xaf, 0x49, 0x41, 0x26, 0x1b, 0x6a, 0x41, 0x37, 0x65, 0x91,
  6655. 0x72, 0xc4, 0xe7, 0x3c, 0x17, 0x31, 0xae, 0x2e, 0x2b, 0x31, 0x45, 0xe4,
  6656. 0x93, 0xd3, 0x10, 0xaa, 0xc5, 0x62, 0xd5, 0x11, 0x4b, 0x57, 0x1d, 0xad,
  6657. 0x48, 0x06, 0xd0, 0x0d, 0x98, 0xa5, 0xc6, 0x5b, 0xd0, 0x9e, 0x22, 0xc0,
  6658. 0x00, 0x32, 0x5a, 0xf5, 0x1c, 0x89, 0x6d, 0x54, 0x97, 0x55, 0x6b, 0x46,
  6659. 0xc5, 0xc7, 0xc4, 0x48, 0x9c, 0xbf, 0x47, 0xdc, 0x03, 0xc4, 0x1b, 0xcb,
  6660. 0x65, 0xa6, 0x91, 0x9d, 0x6d, 0xf1, 0xb0, 0x7a, 0x4d, 0x3b, 0x03, 0x95,
  6661. 0xf4, 0x8b, 0x0b, 0xae, 0x39, 0xff, 0x3f, 0xf6, 0xc0, 0x14, 0x18, 0x8a,
  6662. 0xe5, 0x19, 0xbd, 0xc1, 0xb4, 0x05, 0x4e, 0x29, 0x2f, 0x0b, 0x33, 0x76,
  6663. 0x28, 0x16, 0xa4, 0xa6, 0x93, 0x04, 0xb5, 0x55, 0x6b, 0x89, 0x3d, 0xa5,
  6664. 0x0f, 0xd3, 0xad, 0xfa, 0xd9, 0xfd, 0x05, 0x5d, 0x48, 0x94, 0x25, 0x5a,
  6665. 0x2c, 0x9a, 0x94, 0x80, 0xb0, 0xe7, 0xcb, 0x4d, 0x77, 0xbf, 0xca, 0xd8,
  6666. 0x55, 0x48, 0xbd, 0x66, 0xb1, 0x85, 0x81, 0xb1, 0x37, 0x79, 0xab, 0x52,
  6667. 0x08, 0x14, 0x12, 0xac, 0xcd, 0x45, 0x4d, 0x53, 0x6b, 0xca, 0x96, 0xc7,
  6668. 0x3b, 0x2f, 0x73, 0xb1, 0x5a, 0x23, 0xbd, 0x65, 0xd5, 0xea, 0x17, 0xb3,
  6669. 0xdc, 0xa1, 0x17, 0x1b, 0x2d, 0xb3, 0x9c, 0xd0, 0xdb, 0x41, 0x77, 0xef,
  6670. 0x93, 0x20, 0x52, 0x3e, 0x9d, 0xf5, 0xbf, 0x33, 0xf7, 0x52, 0xc1, 0x90,
  6671. 0xa0, 0x15, 0x17, 0xce, 0xf7, 0xf7, 0xd0, 0x3a, 0x3b, 0xd1, 0x72, 0x56,
  6672. 0x31, 0x81, 0xae, 0x60, 0xab, 0x40, 0xc1, 0xd1, 0x28, 0x77, 0x53, 0xac,
  6673. 0x9f, 0x11, 0x0a, 0x88, 0x36, 0x4b, 0xda, 0x57, 0xa7, 0x28, 0x5c, 0x85,
  6674. 0xd3, 0x85, 0x9b, 0x79, 0xad, 0x05, 0x1c, 0x37, 0x14, 0x5e, 0x0d, 0xd0,
  6675. 0x23, 0x03, 0x42, 0x1d, 0x48, 0x5d, 0xc5, 0x3c, 0x5a, 0x08, 0xa9, 0x0d,
  6676. 0x6e, 0x82, 0x7c, 0x2e, 0x3c, 0x41, 0xcc, 0x96, 0x8e, 0xad, 0xee, 0x2a,
  6677. 0x61, 0x0b, 0x16, 0x0f, 0xa9, 0x24, 0x40, 0x85, 0xbc, 0x9f, 0x28, 0x8d,
  6678. 0xe6, 0x68, 0x4d, 0x8f, 0x30, 0x48, 0xd9, 0x73, 0x73, 0x6c, 0x9a, 0x7f,
  6679. 0x67, 0xf7, 0xde, 0x4c, 0x0a, 0x8b, 0xe4, 0xb3, 0x08, 0x2a, 0x52, 0xda,
  6680. 0x54, 0xee, 0xcd, 0xb5, 0x62, 0x4a, 0x26, 0x20, 0xfb, 0x40, 0xbb, 0x39,
  6681. 0x3a, 0x0f, 0x09, 0xe8, 0x00, 0xd1, 0x24, 0x97, 0x60, 0xe9, 0x83, 0x83,
  6682. 0xfe, 0x9f, 0x9c, 0x15, 0xcf, 0x69, 0x03, 0x9f, 0x03, 0xe1, 0xe8, 0x6e,
  6683. 0xbd, 0x87, 0x58, 0x68, 0xee, 0xec, 0xd8, 0x29, 0x46, 0x23, 0x49, 0x92,
  6684. 0x72, 0x95, 0x5b, 0x49, 0xca, 0xe0, 0x45, 0x59, 0xb2, 0xca, 0xf4, 0xfc,
  6685. 0xb7, 0x59, 0x37, 0x49, 0x28, 0xbc, 0xf3, 0xd7, 0x61, 0xbc, 0x4b, 0xf3,
  6686. 0xa9, 0x4b, 0x2f, 0x05, 0xa8, 0x01, 0xa5, 0xdc, 0x00, 0x6e, 0x01, 0xb6,
  6687. 0x45, 0x3c, 0xd5, 0x49, 0x7d, 0x5c, 0x25, 0xe8, 0x31, 0x87, 0xb2, 0xb9,
  6688. 0xbf, 0xb3, 0x01, 0x62, 0x0c, 0xd0, 0x48, 0x77, 0xa2, 0x34, 0x0f, 0x16,
  6689. 0x22, 0x28, 0xee, 0x54, 0x08, 0x93, 0x3b, 0xe4, 0xde, 0x7e, 0x63, 0xf7,
  6690. 0x97, 0x16, 0x5d, 0x71, 0x58, 0xc2, 0x2e, 0xf2, 0x36, 0xa6, 0x12, 0x65,
  6691. 0x94, 0x17, 0xac, 0x66, 0x23, 0x7e, 0xc6, 0x72, 0x79, 0x24, 0xce, 0x8f,
  6692. 0x55, 0x19, 0x97, 0x44, 0xfc, 0x55, 0xec, 0x85, 0x26, 0x27, 0xdb, 0x38,
  6693. 0xb1, 0x42, 0x0a, 0xdd, 0x05, 0x99, 0x28, 0xeb, 0x03, 0x6c, 0x9a, 0xe9,
  6694. 0x17, 0xf6, 0x2c, 0xb0, 0xfe, 0xe7, 0xa4, 0xa7, 0x31, 0xda, 0x4d, 0xb0,
  6695. 0x29, 0xdb, 0xdd, 0x8d, 0x12, 0x13, 0x9c, 0xb4, 0xcc, 0x83, 0x97, 0xfb,
  6696. 0x1a, 0xdc, 0x08, 0xd6, 0x30, 0x62, 0xe8, 0xeb, 0x8b, 0x61, 0xcb, 0x1d,
  6697. 0x06, 0xe3, 0xa5, 0x4d, 0x35, 0xdb, 0x59, 0xa8, 0x2d, 0x87, 0x27, 0x44,
  6698. 0x6f, 0xc0, 0x38, 0x97, 0xe4, 0x85, 0x00, 0x02, 0x09, 0xf6, 0x69, 0x3a,
  6699. 0xcf, 0x08, 0x1b, 0x21, 0xbb, 0x79, 0xb1, 0xa1, 0x34, 0x09, 0xe0, 0x80,
  6700. 0xca, 0xb0, 0x78, 0x8a, 0x11, 0x97, 0xd4, 0x07, 0xbe, 0x1b, 0x6a, 0x5d,
  6701. 0xdb, 0xd6, 0x1f, 0x76, 0x6b, 0x16, 0xf0, 0x58, 0x84, 0x5f, 0x59, 0xce,
  6702. 0x62, 0x34, 0xc3, 0xdf, 0x94, 0xb8, 0x2f, 0x84, 0x68, 0xf0, 0xb8, 0x51,
  6703. 0xd9, 0x6d, 0x8e, 0x4a, 0x1d, 0xe6, 0x5c, 0xd8, 0x86, 0x25, 0xe3, 0x24,
  6704. 0xfd, 0x21, 0x61, 0x13, 0x48, 0x3e, 0xf6, 0x7d, 0xa6, 0x71, 0x9b, 0xd2,
  6705. 0x6e, 0xe6, 0xd2, 0x08, 0x94, 0x62, 0x6c, 0x98, 0xfe, 0x2f, 0x9c, 0x88,
  6706. 0x7e, 0x78, 0x15, 0x02, 0x00, 0xf0, 0xba, 0x24, 0x91, 0xf2, 0xdc, 0x47,
  6707. 0x51, 0x4d, 0x15, 0x5e, 0x91, 0x5f, 0x57, 0x5b, 0x1d, 0x35, 0x24, 0x45,
  6708. 0x75, 0x9b, 0x88, 0x75, 0xf1, 0x2f, 0x85, 0xe7, 0x89, 0xd1, 0x01, 0xb4,
  6709. 0xc8, 0x18, 0xb7, 0x97, 0xef, 0x4b, 0x90, 0xf4, 0xbf, 0x10, 0x27, 0x3c,
  6710. 0x60, 0xff, 0xc4, 0x94, 0x20, 0x2f, 0x93, 0x4b, 0x4d, 0xe3, 0x80, 0xf7,
  6711. 0x2c, 0x71, 0xd9, 0xe3, 0x68, 0xb4, 0x77, 0x2b, 0xc7, 0x0d, 0x39, 0x92,
  6712. 0xef, 0x91, 0x0d, 0xb2, 0x11, 0x50, 0x0e, 0xe8, 0xad, 0x3b, 0xf6, 0xb5,
  6713. 0xc6, 0x14, 0x4d, 0x33, 0x53, 0xa7, 0x60, 0x15, 0xc7, 0x27, 0x51, 0xdc,
  6714. 0x54, 0x29, 0xa7, 0x0d, 0x6a, 0x7b, 0x72, 0x13, 0xad, 0x7d, 0x41, 0x19,
  6715. 0x4e, 0x42, 0x49, 0xcc, 0x42, 0xe4, 0xbd, 0x99, 0x13, 0xd9, 0x7f, 0xf3,
  6716. 0x38, 0xa4, 0xb6, 0x33, 0xed, 0x07, 0x48, 0x7e, 0x8e, 0x82, 0xfe, 0x3a,
  6717. 0x9d, 0x75, 0x93, 0xba, 0x25, 0x4e, 0x37, 0x3c, 0x0c, 0xd5, 0x69, 0xa9,
  6718. 0x2d, 0x9e, 0xfd, 0xe8, 0xbb, 0xf5, 0x0c, 0xe2, 0x86, 0xb9, 0x5e, 0x6f,
  6719. 0x28, 0xe4, 0x19, 0xb3, 0x0b, 0xa4, 0x86, 0xd7, 0x24, 0xd0, 0xb8, 0x89,
  6720. 0x7b, 0x76, 0xec, 0x05, 0x10, 0x5b, 0x68, 0xe9, 0x58, 0x66, 0xa3, 0xc5,
  6721. 0xb6, 0x63, 0x20, 0x0e, 0x0e, 0xea, 0x3d, 0x61, 0x5e, 0xda, 0x3d, 0x3c,
  6722. 0xf9, 0xfd, 0xed, 0xa9, 0xdb, 0x52, 0x94, 0x8a, 0x00, 0xca, 0x3c, 0x8d,
  6723. 0x66, 0x8f, 0xb0, 0xf0, 0x5a, 0xca, 0x3f, 0x63, 0x71, 0xbf, 0xca, 0x99,
  6724. 0x37, 0x9b, 0x75, 0x97, 0x89, 0x10, 0x6e, 0xcf, 0xf2, 0xf5, 0xe3, 0xd5,
  6725. 0x45, 0x9b, 0xad, 0x10, 0x71, 0x6c, 0x5f, 0x6f, 0x7f, 0x22, 0x77, 0x18,
  6726. 0x2f, 0xf9, 0x99, 0xc5, 0x69, 0x58, 0x03, 0x12, 0x86, 0x82, 0x3e, 0xbf,
  6727. 0xc2, 0x12, 0x35, 0x43, 0xa3, 0xd9, 0x18, 0x4f, 0x41, 0x11, 0x6b, 0xf3,
  6728. 0x67, 0xaf, 0x3d, 0x78, 0xe4, 0x22, 0x2d, 0xb3, 0x48, 0x43, 0x31, 0x1d,
  6729. 0xef, 0xa8, 0xba, 0x49, 0x8e, 0xa9, 0xa7, 0xb6, 0x18, 0x77, 0x84, 0xca,
  6730. 0xbd, 0xa2, 0x02, 0x1b, 0x6a, 0xf8, 0x5f, 0xda, 0xff, 0xcf, 0x01, 0x6a,
  6731. 0x86, 0x69, 0xa9, 0xe9, 0xcb, 0x60, 0x1e, 0x15, 0xdc, 0x8f, 0x5d, 0x39,
  6732. 0xb5, 0xce, 0x55, 0x5f, 0x47, 0x97, 0xb1, 0x19, 0x6e, 0x21, 0xd6, 0x13,
  6733. 0x39, 0xb2, 0x24, 0xe0, 0x62, 0x82, 0x9f, 0xed, 0x12, 0x81, 0xed, 0xee,
  6734. 0xab, 0xd0, 0x2f, 0x19, 0x89, 0x3f, 0x57, 0x2e, 0xc2, 0xe2, 0x67, 0xe8,
  6735. 0xae, 0x03, 0x56, 0xba, 0xd4, 0xd0, 0xa4, 0x89, 0x03, 0x06, 0x5b, 0xcc,
  6736. 0xf2, 0x22, 0xb8, 0x0e, 0x76, 0x79, 0x4a, 0x42, 0x1d, 0x37, 0x51, 0x5a,
  6737. 0xaa, 0x46, 0x6c, 0x2a, 0xdd, 0x66, 0xfe, 0xc6, 0x68, 0xc3, 0x38, 0xa2,
  6738. 0xae, 0x5b, 0x98, 0x24, 0x5d, 0x43, 0x05, 0x82, 0x38, 0x12, 0xd3, 0xd1,
  6739. 0x75, 0x2d, 0x4f, 0x61, 0xbd, 0xb9, 0x10, 0x87, 0x44, 0x2a, 0x78, 0x07,
  6740. 0xff, 0xf4, 0x0f, 0xa1, 0xf3, 0x68, 0x9f, 0xbe, 0xae, 0xa2, 0x91, 0xf0,
  6741. 0xc7, 0x55, 0x7a, 0x52, 0xd5, 0xa3, 0x8d, 0x6f, 0xe4, 0x90, 0x5c, 0xf3,
  6742. 0x5f, 0xce, 0x3d, 0x23, 0xf9, 0x8e, 0xae, 0x14, 0xfb, 0x82, 0x9a, 0xa3,
  6743. 0x04, 0x5f, 0xbf, 0xad, 0x3e, 0xf2, 0x97, 0x0a, 0x60, 0x40, 0x70, 0x19,
  6744. 0x72, 0xad, 0x66, 0xfb, 0x78, 0x1b, 0x84, 0x6c, 0x98, 0xbc, 0x8c, 0xf8,
  6745. 0x4f, 0xcb, 0xb5, 0xf6, 0xaf, 0x7a, 0xb7, 0x93, 0xef, 0x67, 0x48, 0x02,
  6746. 0x2c, 0xcb, 0xe6, 0x77, 0x0f, 0x7b, 0xc1, 0xee, 0xc5, 0xb6, 0x2d, 0x7e,
  6747. 0x62, 0xa0, 0xc0, 0xa7, 0xa5, 0x80, 0x31, 0x92, 0x50, 0xa1, 0x28, 0x22,
  6748. 0x95, 0x03, 0x17, 0xd1, 0x0f, 0xf6, 0x08, 0xe5, 0xec
  6749. };
  6750. #define CHACHA_BIG_TEST_SIZE 1305
  6751. #if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC)
  6752. byte cipher_big[CHACHA_BIG_TEST_SIZE] = {0};
  6753. byte plain_big[CHACHA_BIG_TEST_SIZE] = {0};
  6754. byte input_big[CHACHA_BIG_TEST_SIZE] = {0};
  6755. #else
  6756. byte* cipher_big;
  6757. byte* plain_big;
  6758. byte* input_big;
  6759. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  6760. int block_size;
  6761. #endif /* BENCH_EMBEDDED */
  6762. byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90};
  6763. byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96};
  6764. byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5};
  6765. byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd};
  6766. byte* test_chacha[4];
  6767. test_chacha[0] = a;
  6768. test_chacha[1] = b;
  6769. test_chacha[2] = c;
  6770. test_chacha[3] = d;
  6771. WOLFSSL_ENTER("chacha_test");
  6772. #ifndef BENCH_EMBEDDED
  6773. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6774. cipher_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  6775. DYNAMIC_TYPE_TMP_BUFFER);
  6776. if (cipher_big == NULL) {
  6777. return MEMORY_E;
  6778. }
  6779. plain_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  6780. DYNAMIC_TYPE_TMP_BUFFER);
  6781. if (plain_big == NULL) {
  6782. return MEMORY_E;
  6783. }
  6784. input_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  6785. DYNAMIC_TYPE_TMP_BUFFER);
  6786. if (input_big == NULL) {
  6787. return MEMORY_E;
  6788. }
  6789. XMEMSET(cipher_big, 0, CHACHA_BIG_TEST_SIZE);
  6790. XMEMSET(plain_big, 0, CHACHA_BIG_TEST_SIZE);
  6791. XMEMSET(input_big, 0, CHACHA_BIG_TEST_SIZE);
  6792. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  6793. #endif /* BENCH_EMBEDDED */
  6794. for (i = 0; i < times; ++i) {
  6795. if (i < 3) {
  6796. keySz = 32;
  6797. }
  6798. else {
  6799. keySz = 16;
  6800. }
  6801. XMEMCPY(plain, keys[i], keySz);
  6802. XMEMSET(cipher, 0, 32);
  6803. XMEMCPY(cipher + 4, ivs[i], 8);
  6804. ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
  6805. ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
  6806. if (ret != 0)
  6807. return ret;
  6808. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  6809. ret |= wc_Chacha_SetIV(&dec, cipher, 0);
  6810. if (ret != 0)
  6811. return ret;
  6812. XMEMCPY(plain, input, 8);
  6813. ret |= wc_Chacha_Process(&enc, cipher, plain, (word32)8);
  6814. ret |= wc_Chacha_Process(&dec, plain, cipher, (word32)8);
  6815. if (ret != 0)
  6816. return ret;
  6817. if (XMEMCMP(test_chacha[i], cipher, 8))
  6818. return WC_TEST_RET_ENC_I(i);
  6819. if (XMEMCMP(plain, input, 8))
  6820. return WC_TEST_RET_ENC_I(i);
  6821. }
  6822. /* test of starting at a different counter
  6823. encrypts all of the information and decrypts starting at 2nd chunk */
  6824. XMEMSET(plain, 0, sizeof(plain));
  6825. XMEMSET(sliver, 1, sizeof(sliver)); /* set as 1's to not match plain */
  6826. XMEMSET(cipher, 0, sizeof(cipher));
  6827. XMEMCPY(cipher + 4, ivs[0], 8);
  6828. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  6829. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  6830. if (ret != 0)
  6831. return ret;
  6832. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  6833. ret |= wc_Chacha_SetIV(&dec, cipher, 1);
  6834. if (ret != 0)
  6835. return ret;
  6836. ret |= wc_Chacha_Process(&enc, cipher, plain, sizeof(plain));
  6837. ret |= wc_Chacha_Process(&dec, sliver, cipher + 64, sizeof(sliver));
  6838. if (ret != 0)
  6839. return ret;
  6840. if (XMEMCMP(plain + 64, sliver, 64))
  6841. return WC_TEST_RET_ENC_NC;
  6842. #ifndef BENCH_EMBEDDED
  6843. /* test of encrypting more data */
  6844. keySz = 32;
  6845. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  6846. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  6847. if (ret != 0)
  6848. return ret;
  6849. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  6850. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  6851. if (ret != 0)
  6852. return ret;
  6853. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, CHACHA_BIG_TEST_SIZE);
  6854. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big,
  6855. CHACHA_BIG_TEST_SIZE);
  6856. if (ret != 0)
  6857. return ret;
  6858. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  6859. return WC_TEST_RET_ENC_NC;
  6860. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  6861. return WC_TEST_RET_ENC_NC;
  6862. for (i = 0; i < 18; ++i) {
  6863. /* this will test all paths
  6864. * block sizes: 1 3 7 15 31 63 127 255 511 (i = 0- 8)
  6865. * 2 4 8 16 32 64 128 256 512 (i = 9-17)
  6866. */
  6867. block_size = (2 << (i%9)) - (i<9?1:0);
  6868. keySz = 32;
  6869. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  6870. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  6871. if (ret != 0)
  6872. return ret;
  6873. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  6874. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  6875. if (ret != 0)
  6876. return ret;
  6877. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big , (word32)block_size);
  6878. ret |= wc_Chacha_Process(&dec, plain_big , cipher_big, (word32)block_size);
  6879. if (ret != 0)
  6880. return ret;
  6881. if (XMEMCMP(plain_big, input_big, block_size))
  6882. return WC_TEST_RET_ENC_I(i);
  6883. if (XMEMCMP(cipher_big, cipher_big_result, block_size))
  6884. return WC_TEST_RET_ENC_I(i);
  6885. }
  6886. /* Streaming test */
  6887. for (i = 1; i <= (int)CHACHA_CHUNK_BYTES + 1; i++) {
  6888. int j, rem;
  6889. ret = wc_Chacha_SetKey(&enc, keys[0], keySz);
  6890. if (ret != 0)
  6891. return WC_TEST_RET_ENC_EC(ret);
  6892. ret = wc_Chacha_SetKey(&dec, keys[0], keySz);
  6893. if (ret != 0)
  6894. return WC_TEST_RET_ENC_EC(ret);
  6895. ret = wc_Chacha_SetIV(&enc, ivs[2], 0);
  6896. if (ret != 0)
  6897. return WC_TEST_RET_ENC_EC(ret);
  6898. ret = wc_Chacha_SetIV(&dec, ivs[2], 0);
  6899. if (ret != 0)
  6900. return WC_TEST_RET_ENC_EC(ret);
  6901. for (j = 0; j < CHACHA_BIG_TEST_SIZE - i; j+= i) {
  6902. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, (word32)i);
  6903. if (ret != 0)
  6904. return WC_TEST_RET_ENC_EC(ret);
  6905. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, (word32)i);
  6906. if (ret != 0)
  6907. return WC_TEST_RET_ENC_EC(ret);
  6908. }
  6909. rem = CHACHA_BIG_TEST_SIZE - j;
  6910. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, (word32)rem);
  6911. if (ret != 0)
  6912. return WC_TEST_RET_ENC_EC(ret);
  6913. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, (word32)rem);
  6914. if (ret != 0)
  6915. return WC_TEST_RET_ENC_EC(ret);
  6916. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  6917. return WC_TEST_RET_ENC_NC;
  6918. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  6919. return WC_TEST_RET_ENC_NC;
  6920. }
  6921. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6922. XFREE(cipher_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6923. XFREE(plain_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6924. XFREE(input_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6925. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  6926. #endif /* BENCH_EMBEDDED */
  6927. return 0;
  6928. }
  6929. #endif /* HAVE_CHACHA */
  6930. #ifdef HAVE_POLY1305
  6931. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t poly1305_test(void)
  6932. {
  6933. byte tag[16];
  6934. Poly1305 enc;
  6935. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6936. {
  6937. 0x43,0x72,0x79,0x70,0x74,0x6f,0x67,0x72,
  6938. 0x61,0x70,0x68,0x69,0x63,0x20,0x46,0x6f,
  6939. 0x72,0x75,0x6d,0x20,0x52,0x65,0x73,0x65,
  6940. 0x61,0x72,0x63,0x68,0x20,0x47,0x72,0x6f,
  6941. 0x75,0x70
  6942. };
  6943. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6944. {
  6945. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,
  6946. 0x6c,0x64,0x21
  6947. };
  6948. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6949. {
  6950. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6951. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6952. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6953. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6954. };
  6955. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] =
  6956. {
  6957. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  6958. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  6959. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  6960. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  6961. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  6962. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  6963. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  6964. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  6965. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  6966. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  6967. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6968. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  6969. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  6970. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  6971. 0x61,0x16
  6972. };
  6973. WOLFSSL_SMALL_STACK_STATIC const byte msg5[] =
  6974. {
  6975. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  6976. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  6977. };
  6978. WOLFSSL_SMALL_STACK_STATIC const byte msg6[] =
  6979. {
  6980. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  6981. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  6982. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  6983. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  6984. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  6985. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  6986. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6987. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  6988. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  6989. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  6990. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6991. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  6992. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  6993. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  6994. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  6995. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6996. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  6997. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  6998. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  6999. 0x61,0x16
  7000. };
  7001. byte additional[] =
  7002. {
  7003. 0x50,0x51,0x52,0x53,0xc0,0xc1,0xc2,0xc3,
  7004. 0xc4,0xc5,0xc6,0xc7
  7005. };
  7006. WOLFSSL_SMALL_STACK_STATIC const byte correct0[] =
  7007. {
  7008. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  7009. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  7010. };
  7011. WOLFSSL_SMALL_STACK_STATIC const byte correct1[] =
  7012. {
  7013. 0xa8,0x06,0x1d,0xc1,0x30,0x51,0x36,0xc6,
  7014. 0xc2,0x2b,0x8b,0xaf,0x0c,0x01,0x27,0xa9
  7015. };
  7016. WOLFSSL_SMALL_STACK_STATIC const byte correct2[] =
  7017. {
  7018. 0xa6,0xf7,0x45,0x00,0x8f,0x81,0xc9,0x16,
  7019. 0xa2,0x0d,0xcc,0x74,0xee,0xf2,0xb2,0xf0
  7020. };
  7021. WOLFSSL_SMALL_STACK_STATIC const byte correct3[] =
  7022. {
  7023. 0x49,0xec,0x78,0x09,0x0e,0x48,0x1e,0xc6,
  7024. 0xc2,0x6b,0x33,0xb9,0x1c,0xcc,0x03,0x07
  7025. };
  7026. WOLFSSL_SMALL_STACK_STATIC const byte correct4[] =
  7027. {
  7028. 0x1a,0xe1,0x0b,0x59,0x4f,0x09,0xe2,0x6a,
  7029. 0x7e,0x90,0x2e,0xcb,0xd0,0x60,0x06,0x91
  7030. };
  7031. WOLFSSL_SMALL_STACK_STATIC const byte correct5[] =
  7032. {
  7033. 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7034. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7035. };
  7036. WOLFSSL_SMALL_STACK_STATIC const byte correct6[] =
  7037. {
  7038. 0xea,0x11,0x5c,0x4f,0xd0,0xc0,0x10,0xae,
  7039. 0xf7,0xdf,0xda,0x77,0xa2,0xe9,0xaf,0xca
  7040. };
  7041. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  7042. 0x85,0xd6,0xbe,0x78,0x57,0x55,0x6d,0x33,
  7043. 0x7f,0x44,0x52,0xfe,0x42,0xd5,0x06,0xa8,
  7044. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  7045. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  7046. };
  7047. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  7048. 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,
  7049. 0x33,0x32,0x2d,0x62,0x79,0x74,0x65,0x20,
  7050. 0x6b,0x65,0x79,0x20,0x66,0x6f,0x72,0x20,
  7051. 0x50,0x6f,0x6c,0x79,0x31,0x33,0x30,0x35
  7052. };
  7053. WOLFSSL_SMALL_STACK_STATIC const byte key4[] = {
  7054. 0x7b,0xac,0x2b,0x25,0x2d,0xb4,0x47,0xaf,
  7055. 0x09,0xb6,0x7a,0x55,0xa4,0xe9,0x55,0x84,
  7056. 0x0a,0xe1,0xd6,0x73,0x10,0x75,0xd9,0xeb,
  7057. 0x2a,0x93,0x75,0x78,0x3e,0xd5,0x53,0xff
  7058. };
  7059. WOLFSSL_SMALL_STACK_STATIC const byte key5[] = {
  7060. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7061. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7062. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7063. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7064. };
  7065. const byte* msgs[] = {NULL, msg1, msg2, msg3, msg5, msg6};
  7066. word32 szm[] = {0, sizeof(msg1), sizeof(msg2),
  7067. sizeof(msg3), sizeof(msg5), sizeof(msg6)};
  7068. const byte* keys[] = {key, key, key2, key2, key5, key};
  7069. const byte* tests[] = {correct0, correct1, correct2, correct3, correct5,
  7070. correct6};
  7071. int i;
  7072. wc_test_ret_t ret = 0;
  7073. WOLFSSL_ENTER("poly1305_test");
  7074. for (i = 0; i < 6; i++) {
  7075. ret = wc_Poly1305SetKey(&enc, keys[i], 32);
  7076. if (ret != 0)
  7077. return WC_TEST_RET_ENC_I(i);
  7078. ret = wc_Poly1305Update(&enc, msgs[i], szm[i]);
  7079. if (ret != 0)
  7080. return WC_TEST_RET_ENC_I(i);
  7081. ret = wc_Poly1305Final(&enc, tag);
  7082. if (ret != 0)
  7083. return WC_TEST_RET_ENC_I(i);
  7084. if (XMEMCMP(tag, tests[i], sizeof(tag)))
  7085. return WC_TEST_RET_ENC_I(i);
  7086. }
  7087. /* Check TLS MAC function from 2.8.2 https://tools.ietf.org/html/rfc7539 */
  7088. XMEMSET(tag, 0, sizeof(tag));
  7089. ret = wc_Poly1305SetKey(&enc, key4, sizeof(key4));
  7090. if (ret != 0)
  7091. return WC_TEST_RET_ENC_EC(ret);
  7092. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  7093. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  7094. if (ret != 0)
  7095. return WC_TEST_RET_ENC_EC(ret);
  7096. if (XMEMCMP(tag, correct4, sizeof(tag)))
  7097. return WC_TEST_RET_ENC_NC;
  7098. /* Check fail of TLS MAC function if altering additional data */
  7099. XMEMSET(tag, 0, sizeof(tag));
  7100. additional[0]++;
  7101. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  7102. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  7103. if (ret != 0)
  7104. return WC_TEST_RET_ENC_EC(ret);
  7105. if (XMEMCMP(tag, correct4, sizeof(tag)) == 0)
  7106. return WC_TEST_RET_ENC_NC;
  7107. return 0;
  7108. }
  7109. #endif /* HAVE_POLY1305 */
  7110. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  7111. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha20_poly1305_aead_test(void)
  7112. {
  7113. /* Test #1 from Section 2.8.2 of draft-irtf-cfrg-chacha20-poly1305-10 */
  7114. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  7115. WOLFSSL_SMALL_STACK_STATIC const byte key1[] = {
  7116. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  7117. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  7118. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  7119. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  7120. };
  7121. WOLFSSL_SMALL_STACK_STATIC const byte plaintext1[] = {
  7122. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  7123. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  7124. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  7125. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  7126. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  7127. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  7128. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  7129. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  7130. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  7131. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  7132. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  7133. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  7134. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  7135. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  7136. 0x74, 0x2e
  7137. };
  7138. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] = {
  7139. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  7140. 0x44, 0x45, 0x46, 0x47
  7141. };
  7142. WOLFSSL_SMALL_STACK_STATIC const byte aad1[] = { /* additional data */
  7143. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  7144. 0xc4, 0xc5, 0xc6, 0xc7
  7145. };
  7146. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] = { /* expected output from operation */
  7147. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  7148. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  7149. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  7150. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  7151. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  7152. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  7153. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  7154. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  7155. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  7156. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  7157. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  7158. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  7159. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  7160. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  7161. 0x61, 0x16
  7162. };
  7163. WOLFSSL_SMALL_STACK_STATIC const byte authTag1[] = { /* expected output from operation */
  7164. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  7165. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  7166. };
  7167. /* Test #2 from Appendix A.2 in draft-irtf-cfrg-chacha20-poly1305-10 */
  7168. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  7169. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  7170. 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
  7171. 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
  7172. 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
  7173. 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
  7174. };
  7175. WOLFSSL_SMALL_STACK_STATIC const byte plaintext2[] = {
  7176. 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
  7177. 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
  7178. 0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
  7179. 0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  7180. 0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69,
  7181. 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20,
  7182. 0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20,
  7183. 0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d,
  7184. 0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e,
  7185. 0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65,
  7186. 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
  7187. 0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63,
  7188. 0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f,
  7189. 0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64,
  7190. 0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65,
  7191. 0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  7192. 0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
  7193. 0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e,
  7194. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
  7195. 0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72,
  7196. 0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20,
  7197. 0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65,
  7198. 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61,
  7199. 0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
  7200. 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65,
  7201. 0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
  7202. 0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20,
  7203. 0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
  7204. 0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
  7205. 0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20,
  7206. 0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b,
  7207. 0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67,
  7208. 0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80,
  7209. 0x9d
  7210. };
  7211. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  7212. 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  7213. 0x05, 0x06, 0x07, 0x08
  7214. };
  7215. WOLFSSL_SMALL_STACK_STATIC const byte aad2[] = { /* additional data */
  7216. 0xf3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
  7217. 0x00, 0x00, 0x4e, 0x91
  7218. };
  7219. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] = { /* expected output from operation */
  7220. 0x64, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4,
  7221. 0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd,
  7222. 0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89,
  7223. 0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2,
  7224. 0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee,
  7225. 0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0,
  7226. 0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00,
  7227. 0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf,
  7228. 0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce,
  7229. 0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81,
  7230. 0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd,
  7231. 0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55,
  7232. 0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61,
  7233. 0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38,
  7234. 0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0,
  7235. 0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4,
  7236. 0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46,
  7237. 0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9,
  7238. 0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e,
  7239. 0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e,
  7240. 0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15,
  7241. 0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a,
  7242. 0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea,
  7243. 0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a,
  7244. 0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99,
  7245. 0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e,
  7246. 0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10,
  7247. 0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10,
  7248. 0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94,
  7249. 0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30,
  7250. 0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf,
  7251. 0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29,
  7252. 0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70,
  7253. 0x9b
  7254. };
  7255. WOLFSSL_SMALL_STACK_STATIC const byte authTag2[] = { /* expected output from operation */
  7256. 0xee, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22,
  7257. 0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38
  7258. };
  7259. byte generatedCiphertext[265]; /* max plaintext2/cipher2 */
  7260. byte generatedPlaintext[265]; /* max plaintext2/cipher2 */
  7261. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  7262. ChaChaPoly_Aead aead;
  7263. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  7264. #define TEST_SMALL_CHACHA_CHUNKS 32
  7265. #else
  7266. #define TEST_SMALL_CHACHA_CHUNKS 64
  7267. #endif
  7268. #ifdef TEST_SMALL_CHACHA_CHUNKS
  7269. word32 testLen;
  7270. #endif
  7271. wc_test_ret_t err;
  7272. WOLFSSL_ENTER("chacha20_poly1305_aead_test");
  7273. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  7274. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  7275. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  7276. /* Parameter Validation testing */
  7277. /* Encrypt */
  7278. err = wc_ChaCha20Poly1305_Encrypt(NULL, iv1, aad1, sizeof(aad1), plaintext1,
  7279. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  7280. if (err != BAD_FUNC_ARG)
  7281. return WC_TEST_RET_ENC_EC(err);
  7282. err = wc_ChaCha20Poly1305_Encrypt(key1, NULL, aad1, sizeof(aad1),
  7283. plaintext1, sizeof(plaintext1), generatedCiphertext,
  7284. generatedAuthTag);
  7285. if (err != BAD_FUNC_ARG)
  7286. return WC_TEST_RET_ENC_EC(err);
  7287. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  7288. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  7289. if (err != BAD_FUNC_ARG)
  7290. return WC_TEST_RET_ENC_EC(err);
  7291. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  7292. sizeof(plaintext1), NULL, generatedAuthTag);
  7293. if (err != BAD_FUNC_ARG)
  7294. return WC_TEST_RET_ENC_EC(err);
  7295. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  7296. sizeof(plaintext1), generatedCiphertext, NULL);
  7297. if (err != BAD_FUNC_ARG)
  7298. return WC_TEST_RET_ENC_EC(err);
  7299. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  7300. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  7301. if (err != BAD_FUNC_ARG)
  7302. return WC_TEST_RET_ENC_EC(err);
  7303. /* Decrypt */
  7304. err = wc_ChaCha20Poly1305_Decrypt(NULL, iv2, aad2, sizeof(aad2), cipher2,
  7305. sizeof(cipher2), authTag2, generatedPlaintext);
  7306. if (err != BAD_FUNC_ARG)
  7307. return WC_TEST_RET_ENC_EC(err);
  7308. err = wc_ChaCha20Poly1305_Decrypt(key2, NULL, aad2, sizeof(aad2), cipher2,
  7309. sizeof(cipher2), authTag2, generatedPlaintext);
  7310. if (err != BAD_FUNC_ARG)
  7311. return WC_TEST_RET_ENC_EC(err);
  7312. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  7313. sizeof(cipher2), authTag2, generatedPlaintext);
  7314. if (err != BAD_FUNC_ARG)
  7315. return WC_TEST_RET_ENC_EC(err);
  7316. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  7317. sizeof(cipher2), NULL, generatedPlaintext);
  7318. if (err != BAD_FUNC_ARG)
  7319. return WC_TEST_RET_ENC_EC(err);
  7320. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  7321. sizeof(cipher2), authTag2, NULL);
  7322. if (err != BAD_FUNC_ARG)
  7323. return WC_TEST_RET_ENC_EC(err);
  7324. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  7325. sizeof(cipher2), authTag2, generatedPlaintext);
  7326. if (err != BAD_FUNC_ARG)
  7327. return WC_TEST_RET_ENC_EC(err);
  7328. /* Test #1 */
  7329. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1,
  7330. aad1, sizeof(aad1),
  7331. plaintext1, sizeof(plaintext1),
  7332. generatedCiphertext, generatedAuthTag);
  7333. if (err) {
  7334. return WC_TEST_RET_ENC_EC(err);
  7335. }
  7336. /* -- Check the ciphertext and authtag */
  7337. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  7338. return WC_TEST_RET_ENC_NC;
  7339. }
  7340. if (XMEMCMP(generatedAuthTag, authTag1, sizeof(authTag1))) {
  7341. return WC_TEST_RET_ENC_NC;
  7342. }
  7343. /* -- Verify decryption works */
  7344. err = wc_ChaCha20Poly1305_Decrypt(key1, iv1,
  7345. aad1, sizeof(aad1),
  7346. cipher1, sizeof(cipher1),
  7347. authTag1, generatedPlaintext);
  7348. if (err) {
  7349. return err;
  7350. }
  7351. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  7352. return WC_TEST_RET_ENC_NC;
  7353. }
  7354. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  7355. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  7356. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  7357. /* Test #2 */
  7358. err = wc_ChaCha20Poly1305_Encrypt(key2, iv2,
  7359. aad2, sizeof(aad2),
  7360. plaintext2, sizeof(plaintext2),
  7361. generatedCiphertext, generatedAuthTag);
  7362. if (err) {
  7363. return err;
  7364. }
  7365. /* -- Check the ciphertext and authtag */
  7366. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  7367. return WC_TEST_RET_ENC_NC;
  7368. }
  7369. if (XMEMCMP(generatedAuthTag, authTag2, sizeof(authTag2))) {
  7370. return WC_TEST_RET_ENC_NC;
  7371. }
  7372. /* -- Verify decryption works */
  7373. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2,
  7374. aad2, sizeof(aad2),
  7375. cipher2, sizeof(cipher2),
  7376. authTag2, generatedPlaintext);
  7377. if (err) {
  7378. return err;
  7379. }
  7380. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  7381. return WC_TEST_RET_ENC_NC;
  7382. }
  7383. /* AEAD init/update/final - bad argument tests */
  7384. err = wc_ChaCha20Poly1305_Init(NULL, key1, iv1,
  7385. CHACHA20_POLY1305_AEAD_DECRYPT);
  7386. if (err != BAD_FUNC_ARG)
  7387. return WC_TEST_RET_ENC_EC(err);
  7388. err = wc_ChaCha20Poly1305_Init(&aead, NULL, iv1,
  7389. CHACHA20_POLY1305_AEAD_DECRYPT);
  7390. if (err != BAD_FUNC_ARG)
  7391. return WC_TEST_RET_ENC_EC(err);
  7392. err = wc_ChaCha20Poly1305_Init(&aead, key1, NULL,
  7393. CHACHA20_POLY1305_AEAD_DECRYPT);
  7394. if (err != BAD_FUNC_ARG)
  7395. return WC_TEST_RET_ENC_EC(err);
  7396. err = wc_ChaCha20Poly1305_UpdateAad(NULL, aad1, sizeof(aad1));
  7397. if (err != BAD_FUNC_ARG)
  7398. return WC_TEST_RET_ENC_EC(err);
  7399. err = wc_ChaCha20Poly1305_UpdateAad(&aead, NULL, sizeof(aad1));
  7400. if (err != BAD_FUNC_ARG)
  7401. return WC_TEST_RET_ENC_EC(err);
  7402. err = wc_ChaCha20Poly1305_UpdateData(NULL, generatedPlaintext,
  7403. generatedPlaintext, sizeof(plaintext1));
  7404. if (err != BAD_FUNC_ARG)
  7405. return WC_TEST_RET_ENC_EC(err);
  7406. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext, NULL,
  7407. sizeof(plaintext1));
  7408. if (err != BAD_FUNC_ARG)
  7409. return WC_TEST_RET_ENC_EC(err);
  7410. err = wc_ChaCha20Poly1305_UpdateData(&aead, NULL, generatedPlaintext,
  7411. sizeof(plaintext1));
  7412. if (err != BAD_FUNC_ARG)
  7413. return WC_TEST_RET_ENC_EC(err);
  7414. err = wc_ChaCha20Poly1305_Final(NULL, generatedAuthTag);
  7415. if (err != BAD_FUNC_ARG)
  7416. return WC_TEST_RET_ENC_EC(err);
  7417. err = wc_ChaCha20Poly1305_Final(&aead, NULL);
  7418. if (err != BAD_FUNC_ARG)
  7419. return WC_TEST_RET_ENC_EC(err);
  7420. /* AEAD init/update/final - bad state tests */
  7421. /* clear struct - make valgrind happy to resolve
  7422. "Conditional jump or move depends on uninitialised value(s)".
  7423. The enum is "int" size and aead.state is "byte" */
  7424. /* The wc_ChaCha20Poly1305_Init function does this normally */
  7425. XMEMSET(&aead, 0, sizeof(aead));
  7426. aead.state = CHACHA20_POLY1305_STATE_INIT;
  7427. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  7428. if (err != BAD_STATE_E)
  7429. return WC_TEST_RET_ENC_EC(err);
  7430. aead.state = CHACHA20_POLY1305_STATE_DATA;
  7431. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  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_UpdateData(&aead, generatedPlaintext,
  7436. generatedPlaintext, sizeof(plaintext1));
  7437. if (err != BAD_STATE_E)
  7438. return WC_TEST_RET_ENC_EC(err);
  7439. aead.state = CHACHA20_POLY1305_STATE_INIT;
  7440. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7441. if (err != BAD_STATE_E)
  7442. return WC_TEST_RET_ENC_EC(err);
  7443. aead.state = CHACHA20_POLY1305_STATE_READY;
  7444. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7445. if (err != BAD_STATE_E)
  7446. return WC_TEST_RET_ENC_EC(err);
  7447. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  7448. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  7449. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  7450. /* Test 1 - Encrypt */
  7451. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  7452. CHACHA20_POLY1305_AEAD_ENCRYPT);
  7453. if (err != 0)
  7454. return WC_TEST_RET_ENC_EC(err);
  7455. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  7456. if (err != 0)
  7457. return WC_TEST_RET_ENC_EC(err);
  7458. #ifdef TEST_SMALL_CHACHA_CHUNKS
  7459. /* test doing data in smaller chunks */
  7460. for (testLen=0; testLen<sizeof(plaintext1); ) {
  7461. word32 dataLen = sizeof(plaintext1) - testLen;
  7462. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  7463. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  7464. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext1[testLen],
  7465. &generatedCiphertext[testLen], dataLen);
  7466. if (err != 0)
  7467. return WC_TEST_RET_ENC_EC(err);
  7468. testLen += dataLen;
  7469. }
  7470. #else
  7471. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext1,
  7472. generatedCiphertext, sizeof(plaintext1));
  7473. #endif
  7474. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7475. if (err != 0)
  7476. return WC_TEST_RET_ENC_EC(err);
  7477. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  7478. if (err != 0)
  7479. return WC_TEST_RET_ENC_EC(err);
  7480. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  7481. return WC_TEST_RET_ENC_NC;
  7482. }
  7483. /* Test 1 - Decrypt */
  7484. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  7485. CHACHA20_POLY1305_AEAD_DECRYPT);
  7486. if (err != 0)
  7487. return WC_TEST_RET_ENC_EC(err);
  7488. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  7489. if (err != 0)
  7490. return WC_TEST_RET_ENC_EC(err);
  7491. #ifdef TEST_SMALL_CHACHA_CHUNKS
  7492. /* test doing data in smaller chunks */
  7493. for (testLen=0; testLen<sizeof(plaintext1); ) {
  7494. word32 dataLen = sizeof(plaintext1) - testLen;
  7495. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  7496. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  7497. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  7498. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  7499. dataLen);
  7500. if (err != 0)
  7501. return WC_TEST_RET_ENC_EC(err);
  7502. testLen += dataLen;
  7503. }
  7504. #else
  7505. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  7506. generatedPlaintext, sizeof(cipher1));
  7507. #endif
  7508. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7509. if (err != 0)
  7510. return WC_TEST_RET_ENC_EC(err);
  7511. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  7512. if (err != 0)
  7513. return WC_TEST_RET_ENC_EC(err);
  7514. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  7515. return WC_TEST_RET_ENC_NC;
  7516. }
  7517. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  7518. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  7519. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  7520. /* Test 2 - Encrypt */
  7521. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  7522. CHACHA20_POLY1305_AEAD_ENCRYPT);
  7523. if (err != 0)
  7524. return WC_TEST_RET_ENC_EC(err);
  7525. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  7526. if (err != 0)
  7527. return WC_TEST_RET_ENC_EC(err);
  7528. #ifdef TEST_SMALL_CHACHA_CHUNKS
  7529. /* test doing data in smaller chunks */
  7530. for (testLen=0; testLen<sizeof(plaintext2); ) {
  7531. word32 dataLen = sizeof(plaintext2) - testLen;
  7532. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  7533. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  7534. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext2[testLen],
  7535. &generatedCiphertext[testLen], dataLen);
  7536. if (err != 0)
  7537. return WC_TEST_RET_ENC_EC(err);
  7538. testLen += dataLen;
  7539. }
  7540. #else
  7541. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext2, generatedCiphertext,
  7542. sizeof(plaintext2));
  7543. #endif
  7544. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7545. if (err != 0)
  7546. return WC_TEST_RET_ENC_EC(err);
  7547. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  7548. if (err != 0)
  7549. return WC_TEST_RET_ENC_EC(err);
  7550. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  7551. return WC_TEST_RET_ENC_NC;
  7552. }
  7553. /* Test 2 - Decrypt */
  7554. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  7555. CHACHA20_POLY1305_AEAD_DECRYPT);
  7556. if (err != 0)
  7557. return WC_TEST_RET_ENC_EC(err);
  7558. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  7559. if (err != 0)
  7560. return WC_TEST_RET_ENC_EC(err);
  7561. #ifdef TEST_SMALL_CHACHA_CHUNKS
  7562. /* test doing data in smaller chunks */
  7563. for (testLen=0; testLen<sizeof(plaintext2); ) {
  7564. word32 dataLen = sizeof(plaintext2) - testLen;
  7565. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  7566. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  7567. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  7568. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  7569. dataLen);
  7570. if (err != 0)
  7571. return WC_TEST_RET_ENC_EC(err);
  7572. testLen += dataLen;
  7573. }
  7574. #else
  7575. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  7576. generatedPlaintext, sizeof(cipher2));
  7577. #endif
  7578. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  7579. if (err != 0)
  7580. return WC_TEST_RET_ENC_EC(err);
  7581. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  7582. if (err != 0)
  7583. return WC_TEST_RET_ENC_EC(err);
  7584. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  7585. return WC_TEST_RET_ENC_NC;
  7586. }
  7587. return err;
  7588. }
  7589. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  7590. #ifndef NO_DES3
  7591. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des_test(void)
  7592. {
  7593. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  7594. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7595. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7596. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7597. };
  7598. byte plain[24];
  7599. byte cipher[24];
  7600. Des enc;
  7601. Des dec;
  7602. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  7603. {
  7604. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  7605. };
  7606. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  7607. {
  7608. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  7609. };
  7610. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  7611. {
  7612. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  7613. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  7614. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  7615. };
  7616. wc_test_ret_t ret;
  7617. WOLFSSL_ENTER("des_test");
  7618. ret = wc_Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  7619. if (ret != 0)
  7620. return WC_TEST_RET_ENC_EC(ret);
  7621. ret = wc_Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  7622. if (ret != 0)
  7623. return WC_TEST_RET_ENC_EC(ret);
  7624. ret = wc_Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  7625. if (ret != 0)
  7626. return WC_TEST_RET_ENC_EC(ret);
  7627. ret = wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  7628. if (ret != 0)
  7629. return WC_TEST_RET_ENC_EC(ret);
  7630. if (XMEMCMP(plain, vector, sizeof(plain)))
  7631. return WC_TEST_RET_ENC_NC;
  7632. if (XMEMCMP(cipher, verify, sizeof(cipher)))
  7633. return WC_TEST_RET_ENC_NC;
  7634. ret = wc_Des_CbcEncryptWithKey(cipher, vector, sizeof(vector), key, iv);
  7635. if (ret != 0)
  7636. return WC_TEST_RET_ENC_EC(ret);
  7637. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  7638. {
  7639. EncryptedInfo info;
  7640. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  7641. XMEMCPY(info.iv, iv, sizeof(iv));
  7642. info.ivSz = sizeof(iv);
  7643. info.keySz = sizeof(key);
  7644. info.cipherType = WC_CIPHER_DES;
  7645. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  7646. sizeof(key), WC_HASH_TYPE_SHA);
  7647. if (ret != 0)
  7648. return WC_TEST_RET_ENC_EC(ret);
  7649. /* Test invalid info ptr */
  7650. ret = wc_BufferKeyEncrypt(NULL, cipher, sizeof(cipher), key,
  7651. sizeof(key), WC_HASH_TYPE_SHA);
  7652. if (ret != BAD_FUNC_ARG)
  7653. return WC_TEST_RET_ENC_EC(ret);
  7654. #ifndef NO_PWDBASED
  7655. /* Test invalid hash type - only applies to wc_PBKDF1 call */
  7656. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  7657. sizeof(key), WC_HASH_TYPE_NONE);
  7658. if (ret == 0)
  7659. return WC_TEST_RET_ENC_EC(ret);
  7660. #endif /* !NO_PWDBASED */
  7661. }
  7662. #endif
  7663. return 0;
  7664. }
  7665. #endif /* !NO_DES3 */
  7666. #ifndef NO_DES3
  7667. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des3_test(void)
  7668. {
  7669. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  7670. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7671. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7672. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7673. };
  7674. byte plain[24];
  7675. byte cipher[24];
  7676. Des3 enc;
  7677. Des3 dec;
  7678. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  7679. {
  7680. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  7681. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  7682. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  7683. };
  7684. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  7685. {
  7686. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  7687. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  7688. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  7689. };
  7690. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] =
  7691. {
  7692. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  7693. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  7694. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  7695. };
  7696. wc_test_ret_t ret;
  7697. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7698. size_t i;
  7699. #endif
  7700. WOLFSSL_ENTER("des3_test");
  7701. ret = wc_Des3Init(&enc, HEAP_HINT, devId);
  7702. if (ret != 0)
  7703. return WC_TEST_RET_ENC_EC(ret);
  7704. ret = wc_Des3Init(&dec, HEAP_HINT, devId);
  7705. if (ret != 0)
  7706. return WC_TEST_RET_ENC_EC(ret);
  7707. ret = wc_Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  7708. if (ret != 0)
  7709. return WC_TEST_RET_ENC_EC(ret);
  7710. ret = wc_Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  7711. if (ret != 0)
  7712. return WC_TEST_RET_ENC_EC(ret);
  7713. ret = wc_Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  7714. #if defined(WOLFSSL_ASYNC_CRYPT)
  7715. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  7716. #endif
  7717. if (ret != 0)
  7718. return WC_TEST_RET_ENC_EC(ret);
  7719. ret = wc_Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  7720. #if defined(WOLFSSL_ASYNC_CRYPT)
  7721. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  7722. #endif
  7723. if (ret != 0)
  7724. return WC_TEST_RET_ENC_EC(ret);
  7725. if (XMEMCMP(plain, vector, sizeof(plain)))
  7726. return WC_TEST_RET_ENC_NC;
  7727. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  7728. return WC_TEST_RET_ENC_NC;
  7729. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7730. /* test the same vectors with using compatibility layer */
  7731. for (i = 0; i < sizeof(vector); i += DES_BLOCK_SIZE){
  7732. DES_key_schedule ks1;
  7733. DES_key_schedule ks2;
  7734. DES_key_schedule ks3;
  7735. DES_cblock iv4;
  7736. byte tmp[sizeof(vector)];
  7737. XMEMCPY(ks1, key3, sizeof(DES_key_schedule));
  7738. XMEMCPY(ks2, key3 + 8, sizeof(DES_key_schedule));
  7739. XMEMCPY(ks3, key3 + 16, sizeof(DES_key_schedule));
  7740. XMEMSET(plain, 0, sizeof(plain));
  7741. XMEMSET(cipher, 0, sizeof(cipher));
  7742. /* Test in-place encrypt/decrypt */
  7743. XMEMCPY(tmp, vector, sizeof(vector));
  7744. /* Use i as the splitter */
  7745. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  7746. DES_ede3_cbc_encrypt(tmp, tmp, (long)i, &ks1, &ks2, &ks3,
  7747. &iv4, DES_ENCRYPT);
  7748. DES_ede3_cbc_encrypt(tmp + i, tmp + i, (long)(sizeof(vector) - i),
  7749. &ks1, &ks2, &ks3, &iv4, DES_ENCRYPT);
  7750. XMEMCPY(cipher, tmp, sizeof(cipher));
  7751. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  7752. DES_ede3_cbc_encrypt(tmp, tmp, (long)i, &ks1, &ks2, &ks3,
  7753. &iv4, DES_DECRYPT);
  7754. DES_ede3_cbc_encrypt(tmp + i, tmp + i, (long)(sizeof(cipher) - i),
  7755. &ks1, &ks2, &ks3, &iv4, DES_DECRYPT);
  7756. XMEMCPY(plain, tmp, sizeof(plain));
  7757. if (XMEMCMP(plain, vector, sizeof(plain)))
  7758. return WC_TEST_RET_ENC_NC;
  7759. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  7760. return WC_TEST_RET_ENC_NC;
  7761. }
  7762. #endif /* OPENSSL_EXTRA */
  7763. wc_Des3Free(&enc);
  7764. wc_Des3Free(&dec);
  7765. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  7766. {
  7767. EncryptedInfo info;
  7768. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  7769. XMEMCPY(info.iv, iv3, sizeof(iv3));
  7770. info.ivSz = sizeof(iv3);
  7771. info.keySz = sizeof(key3);
  7772. info.cipherType = WC_CIPHER_DES3;
  7773. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key3,
  7774. sizeof(key3), WC_HASH_TYPE_SHA);
  7775. if (ret != 0)
  7776. return WC_TEST_RET_ENC_EC(ret);
  7777. }
  7778. #endif
  7779. return 0;
  7780. }
  7781. #endif /* NO_DES3 */
  7782. static const int fiducial1 = WC_TEST_RET_LN; /* source code reference point --
  7783. * see print_fiducials() below.
  7784. */
  7785. #ifndef NO_AES
  7786. #if defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_CFB) || \
  7787. defined(WOLFSSL_AES_XTS)
  7788. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7789. && !defined(HAVE_SELFTEST)
  7790. #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
  7791. /* pass in the function, key, iv, plain text and expected and this function
  7792. * tests that the encryption and decryption is successful */
  7793. static wc_test_ret_t EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key,
  7794. const byte* iv, const byte* plain, int plainSz,
  7795. const byte* expected, int expectedSz)
  7796. {
  7797. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7798. EVP_CIPHER_CTX *ctx = NULL;
  7799. #else
  7800. EVP_CIPHER_CTX ctx[1];
  7801. #endif
  7802. int ctx_inited = 0;
  7803. int idx, cipherSz;
  7804. wc_test_ret_t ret = 0;
  7805. byte* cipher;
  7806. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7807. if ((ctx = wolfSSL_EVP_CIPHER_CTX_new()) == NULL)
  7808. return MEMORY_E;
  7809. #endif
  7810. cipher = (byte*)XMALLOC(plainSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7811. if (cipher == NULL) {
  7812. ret = WC_TEST_RET_ENC_ERRNO;
  7813. goto EVP_TEST_END;
  7814. }
  7815. /* test encrypt */
  7816. EVP_CIPHER_CTX_init(ctx);
  7817. ctx_inited = 1;
  7818. if (EVP_CipherInit(ctx, type, key, iv, 1) == 0) {
  7819. ret = WC_TEST_RET_ENC_NC;
  7820. goto EVP_TEST_END;
  7821. }
  7822. if (EVP_CipherUpdate(ctx, cipher, &idx, plain, expectedSz) == 0) {
  7823. ret = WC_TEST_RET_ENC_NC;
  7824. goto EVP_TEST_END;
  7825. }
  7826. cipherSz = idx;
  7827. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  7828. ret = WC_TEST_RET_ENC_NC;
  7829. goto EVP_TEST_END;
  7830. }
  7831. cipherSz += idx;
  7832. if (XMEMCMP(cipher, expected, plainSz)) {
  7833. ret = WC_TEST_RET_ENC_NC;
  7834. goto EVP_TEST_END;
  7835. }
  7836. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(ctx);
  7837. ctx_inited = 0;
  7838. if (ret == WOLFSSL_SUCCESS)
  7839. ret = 0;
  7840. else {
  7841. ret = WC_TEST_RET_ENC_NC;
  7842. goto EVP_TEST_END;
  7843. }
  7844. /* test decrypt */
  7845. EVP_CIPHER_CTX_init(ctx);
  7846. ctx_inited = 1;
  7847. if (EVP_CipherInit(ctx, type, key, iv, 0) == 0) {
  7848. ret = WC_TEST_RET_ENC_NC;
  7849. goto EVP_TEST_END;
  7850. }
  7851. if (EVP_CipherUpdate(ctx, cipher, &idx, cipher, expectedSz) == 0) {
  7852. ret = WC_TEST_RET_ENC_NC;
  7853. goto EVP_TEST_END;
  7854. }
  7855. cipherSz = idx;
  7856. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  7857. ret = WC_TEST_RET_ENC_NC;
  7858. goto EVP_TEST_END;
  7859. }
  7860. cipherSz += idx;
  7861. if ((expectedSz != cipherSz) || XMEMCMP(plain, cipher, plainSz)) {
  7862. ret = WC_TEST_RET_ENC_NC;
  7863. goto EVP_TEST_END;
  7864. }
  7865. EVP_TEST_END:
  7866. if (cipher)
  7867. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7868. (void)cipherSz;
  7869. if (ctx_inited) {
  7870. int cleanup_ret = wolfSSL_EVP_CIPHER_CTX_cleanup(ctx);
  7871. if (cleanup_ret != WOLFSSL_SUCCESS)
  7872. ret = WC_TEST_RET_ENC_NC;
  7873. }
  7874. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7875. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  7876. #endif
  7877. return ret;
  7878. }
  7879. #endif /* !HAVE_FIPS || FIPS_VERSION3_GE(6,0,0) */
  7880. #endif /* OPENSSL_EXTRA && !WOLFCRYPT_ONLY && !HAVE_SELFTEST */
  7881. #endif /* WOLFSSL_AES_OFB || WOLFSSL_AES_CFB */
  7882. #ifdef WOLFSSL_AES_OFB
  7883. /* test vector from https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/Block-Ciphers */
  7884. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesofb_test(void)
  7885. {
  7886. #ifdef WOLFSSL_AES_256
  7887. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  7888. {
  7889. 0xc4,0xc7,0xfa,0xd6,0x53,0x5c,0xb8,0x71,
  7890. 0x4a,0x5c,0x40,0x77,0x9a,0x8b,0xa1,0xd2,
  7891. 0x53,0x3e,0x23,0xb4,0xb2,0x58,0x73,0x2a,
  7892. 0x5b,0x78,0x01,0xf4,0xe3,0x71,0xa7,0x94
  7893. };
  7894. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  7895. {
  7896. 0x5e,0xb9,0x33,0x13,0xb8,0x71,0xff,0x16,
  7897. 0xb9,0x8a,0x9b,0xcb,0x43,0x33,0x0d,0x6f
  7898. };
  7899. WOLFSSL_SMALL_STACK_STATIC const byte plain1[] =
  7900. {
  7901. 0x6d,0x0b,0xb0,0x79,0x63,0x84,0x71,0xe9,
  7902. 0x39,0xd4,0x53,0x14,0x86,0xc1,0x4c,0x25,
  7903. 0x9a,0xee,0xc6,0xf3,0xc0,0x0d,0xfd,0xd6,
  7904. 0xc0,0x50,0xa8,0xba,0xa8,0x20,0xdb,0x71,
  7905. 0xcc,0x12,0x2c,0x4e,0x0c,0x17,0x15,0xef,
  7906. 0x55,0xf3,0x99,0x5a,0x6b,0xf0,0x2a,0x4c
  7907. };
  7908. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  7909. {
  7910. 0x0f,0x54,0x61,0x71,0x59,0xd0,0x3f,0xfc,
  7911. 0x1b,0xfa,0xfb,0x60,0x29,0x30,0xd7,0x00,
  7912. 0xf4,0xa4,0xa8,0xe6,0xdd,0x93,0x94,0x46,
  7913. 0x64,0xd2,0x19,0xc4,0xc5,0x4d,0xde,0x1b,
  7914. 0x04,0x53,0xe1,0x73,0xf5,0x18,0x74,0xae,
  7915. 0xfd,0x64,0xa2,0xe1,0xe2,0x76,0x13,0xb0
  7916. };
  7917. #endif /* WOLFSSL_AES_256 */
  7918. #ifdef WOLFSSL_AES_128
  7919. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  7920. {
  7921. 0x10,0xa5,0x88,0x69,0xd7,0x4b,0xe5,0xa3,
  7922. 0x74,0xcf,0x86,0x7c,0xfb,0x47,0x38,0x59
  7923. };
  7924. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  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 plain2[] =
  7930. {
  7931. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7932. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7933. };
  7934. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  7935. {
  7936. 0x6d,0x25,0x1e,0x69,0x44,0xb0,0x51,0xe0,
  7937. 0x4e,0xaa,0x6f,0xb4,0xdb,0xf7,0x84,0x65
  7938. };
  7939. #endif /* WOLFSSL_AES_128 */
  7940. #ifdef WOLFSSL_AES_192
  7941. WOLFSSL_SMALL_STACK_STATIC const byte key3[] = {
  7942. 0xd0,0x77,0xa0,0x3b,0xd8,0xa3,0x89,0x73,
  7943. 0x92,0x8c,0xca,0xfe,0x4a,0x9d,0x2f,0x45,
  7944. 0x51,0x30,0xbd,0x0a,0xf5,0xae,0x46,0xa9
  7945. };
  7946. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  7947. {
  7948. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7949. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7950. };
  7951. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  7952. {
  7953. 0xab,0xc7,0x86,0xfb,0x1e,0xdb,0x50,0x45,
  7954. 0x80,0xc4,0xd8,0x82,0xef,0x29,0xa0,0xc7
  7955. };
  7956. WOLFSSL_SMALL_STACK_STATIC const byte plain3[] =
  7957. {
  7958. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7959. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7960. };
  7961. #endif /* WOLFSSL_AES_192 */
  7962. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7963. Aes *enc = NULL;
  7964. #else
  7965. Aes enc[1];
  7966. #endif
  7967. byte cipher[AES_BLOCK_SIZE * 4];
  7968. #ifdef HAVE_AES_DECRYPT
  7969. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7970. Aes *dec = NULL;
  7971. #else
  7972. Aes dec[1];
  7973. #endif
  7974. byte plain [AES_BLOCK_SIZE * 4];
  7975. #endif
  7976. wc_test_ret_t ret = 0;
  7977. WOLFSSL_ENTER("aesofb_test");
  7978. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7979. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7980. ERROR_OUT(-1, out);
  7981. #ifdef HAVE_AES_DECRYPT
  7982. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7983. ERROR_OUT(-1, out);
  7984. #endif
  7985. #endif
  7986. XMEMSET(enc, 0, sizeof *enc);
  7987. #ifdef HAVE_AES_DECRYPT
  7988. XMEMSET(dec, 0, sizeof *dec);
  7989. #endif
  7990. #ifdef WOLFSSL_AES_128
  7991. /* 128 key size test */
  7992. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7993. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7994. ret = EVP_test(EVP_aes_128_ofb(), key2, iv2, plain2, sizeof(plain2),
  7995. cipher2, sizeof(cipher2));
  7996. if (ret != 0) {
  7997. goto out;
  7998. }
  7999. #endif
  8000. ret = wc_AesInit(enc, HEAP_HINT, INVALID_DEVID);
  8001. if (ret != 0)
  8002. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8003. #ifdef HAVE_AES_DECRYPT
  8004. ret = wc_AesInit(dec, HEAP_HINT, INVALID_DEVID);
  8005. if (ret != 0)
  8006. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8007. #endif
  8008. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8009. if (ret != 0)
  8010. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8011. #ifdef HAVE_AES_DECRYPT
  8012. /* decrypt uses AES_ENCRYPTION */
  8013. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8014. if (ret != 0)
  8015. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8016. #endif
  8017. XMEMSET(cipher, 0, sizeof(cipher));
  8018. ret = wc_AesOfbEncrypt(enc, cipher, plain2, AES_BLOCK_SIZE);
  8019. if (ret != 0)
  8020. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8021. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE))
  8022. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8023. #ifdef HAVE_AES_DECRYPT
  8024. ret = wc_AesOfbDecrypt(dec, plain, cipher2, AES_BLOCK_SIZE);
  8025. if (ret != 0)
  8026. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8027. if (XMEMCMP(plain, plain2, AES_BLOCK_SIZE))
  8028. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8029. #endif /* HAVE_AES_DECRYPT */
  8030. #endif /* WOLFSSL_AES_128 */
  8031. #ifdef WOLFSSL_AES_192
  8032. /* 192 key size test */
  8033. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8034. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8035. ret = EVP_test(EVP_aes_192_ofb(), key3, iv3, plain3, sizeof(plain3),
  8036. cipher3, sizeof(cipher3));
  8037. if (ret != 0) {
  8038. goto out;
  8039. }
  8040. #endif
  8041. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  8042. if (ret != 0)
  8043. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8044. #ifdef HAVE_AES_DECRYPT
  8045. /* decrypt uses AES_ENCRYPTION */
  8046. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  8047. if (ret != 0)
  8048. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8049. #endif
  8050. XMEMSET(cipher, 0, sizeof(cipher));
  8051. ret = wc_AesOfbEncrypt(enc, cipher, plain3, AES_BLOCK_SIZE);
  8052. if (ret != 0)
  8053. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8054. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE))
  8055. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8056. #ifdef HAVE_AES_DECRYPT
  8057. ret = wc_AesOfbDecrypt(dec, plain, cipher3, AES_BLOCK_SIZE);
  8058. if (ret != 0)
  8059. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8060. if (XMEMCMP(plain, plain3, AES_BLOCK_SIZE))
  8061. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8062. #endif /* HAVE_AES_DECRYPT */
  8063. #endif /* WOLFSSL_AES_192 */
  8064. #ifdef WOLFSSL_AES_256
  8065. /* 256 key size test */
  8066. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8067. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8068. ret = EVP_test(EVP_aes_256_ofb(), key1, iv1, plain1, sizeof(plain1),
  8069. cipher1, sizeof(cipher1));
  8070. if (ret != 0) {
  8071. goto out;
  8072. }
  8073. #endif
  8074. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8075. if (ret != 0)
  8076. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8077. #ifdef HAVE_AES_DECRYPT
  8078. /* decrypt uses AES_ENCRYPTION */
  8079. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8080. if (ret != 0)
  8081. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8082. #endif
  8083. XMEMSET(cipher, 0, sizeof(cipher));
  8084. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE);
  8085. if (ret != 0)
  8086. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8087. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE))
  8088. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8089. ret = wc_AesOfbEncrypt(enc, cipher + AES_BLOCK_SIZE,
  8090. plain1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  8091. if (ret != 0)
  8092. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8093. if (XMEMCMP(cipher + AES_BLOCK_SIZE, cipher1 + AES_BLOCK_SIZE,
  8094. AES_BLOCK_SIZE))
  8095. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8096. #ifdef HAVE_AES_DECRYPT
  8097. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE);
  8098. if (ret != 0)
  8099. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8100. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE))
  8101. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8102. ret = wc_AesOfbDecrypt(dec, plain + AES_BLOCK_SIZE,
  8103. cipher1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  8104. if (ret != 0)
  8105. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8106. if (XMEMCMP(plain + AES_BLOCK_SIZE, plain1 + AES_BLOCK_SIZE,
  8107. AES_BLOCK_SIZE))
  8108. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8109. #endif /* HAVE_AES_DECRYPT */
  8110. /* multiple blocks at once */
  8111. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8112. if (ret != 0)
  8113. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8114. #ifdef HAVE_AES_DECRYPT
  8115. /* decrypt uses AES_ENCRYPTION */
  8116. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8117. if (ret != 0)
  8118. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8119. #endif
  8120. XMEMSET(cipher, 0, sizeof(cipher));
  8121. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE * 3);
  8122. if (ret != 0)
  8123. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8124. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 3))
  8125. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8126. #ifdef HAVE_AES_DECRYPT
  8127. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE * 3);
  8128. if (ret != 0)
  8129. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8130. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE * 3))
  8131. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8132. #endif /* HAVE_AES_DECRYPT */
  8133. /* inline decrypt/encrypt*/
  8134. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8135. if (ret != 0)
  8136. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8137. #ifdef HAVE_AES_DECRYPT
  8138. /* decrypt uses AES_ENCRYPTION */
  8139. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8140. if (ret != 0)
  8141. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8142. #endif
  8143. XMEMCPY(cipher, plain1, AES_BLOCK_SIZE * 2);
  8144. ret = wc_AesOfbEncrypt(enc, cipher, cipher, AES_BLOCK_SIZE * 2);
  8145. if (ret != 0)
  8146. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8147. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  8148. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8149. #ifdef HAVE_AES_DECRYPT
  8150. ret = wc_AesOfbDecrypt(dec, cipher, cipher, AES_BLOCK_SIZE * 2);
  8151. if (ret != 0)
  8152. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8153. if (XMEMCMP(cipher, plain1, AES_BLOCK_SIZE * 2))
  8154. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8155. #endif /* HAVE_AES_DECRYPT */
  8156. /* 256 key size test leftover support */
  8157. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8158. if (ret != 0)
  8159. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8160. #ifdef HAVE_AES_DECRYPT
  8161. /* decrypt uses AES_ENCRYPTION */
  8162. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  8163. if (ret != 0)
  8164. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8165. #endif
  8166. XMEMSET(cipher, 0, sizeof(cipher));
  8167. ret = wc_AesOfbEncrypt(enc, cipher, plain1, 3);
  8168. if (ret != 0)
  8169. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8170. if (XMEMCMP(cipher, cipher1, 3))
  8171. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8172. ret = wc_AesOfbEncrypt(enc, cipher + 3, plain1 + 3, AES_BLOCK_SIZE);
  8173. if (ret != 0)
  8174. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8175. if (XMEMCMP(cipher + 3, cipher1 + 3, AES_BLOCK_SIZE))
  8176. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8177. #ifdef HAVE_AES_DECRYPT
  8178. ret = wc_AesOfbDecrypt(dec, plain, cipher1, 6);
  8179. if (ret != 0)
  8180. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8181. if (XMEMCMP(plain, plain1, 6))
  8182. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8183. ret = wc_AesOfbDecrypt(dec, plain + 6, cipher1 + 6, AES_BLOCK_SIZE);
  8184. if (ret != 0)
  8185. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8186. if (XMEMCMP(plain + 6, plain1 + 6, AES_BLOCK_SIZE))
  8187. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8188. #endif /* HAVE_AES_DECRYPT */
  8189. out:
  8190. wc_AesFree(enc);
  8191. #ifdef HAVE_AES_DECRYPT
  8192. wc_AesFree(dec);
  8193. #endif
  8194. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8195. if (enc)
  8196. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8197. #ifdef HAVE_AES_DECRYPT
  8198. if (dec)
  8199. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8200. #endif
  8201. #endif
  8202. #endif /* WOLFSSL_AES_256 */
  8203. return ret;
  8204. }
  8205. #endif /* WOLFSSL_AES_OFB */
  8206. #if defined(WOLFSSL_AES_CFB)
  8207. /* Test cases from NIST SP 800-38A, Recommendation for Block Cipher Modes of
  8208. * Operation Methods and Techniques
  8209. */
  8210. static wc_test_ret_t aescfb_test_0(void)
  8211. {
  8212. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8213. Aes *enc = NULL;
  8214. #else
  8215. Aes enc[1];
  8216. #endif
  8217. int enc_inited = 0;
  8218. byte cipher[AES_BLOCK_SIZE * 4];
  8219. #ifdef HAVE_AES_DECRYPT
  8220. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8221. Aes *dec = NULL;
  8222. #else
  8223. Aes dec[1];
  8224. #endif
  8225. int dec_inited = 0;
  8226. byte plain [AES_BLOCK_SIZE * 4];
  8227. #endif
  8228. wc_test_ret_t ret = 0;
  8229. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  8230. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  8231. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  8232. };
  8233. #ifdef WOLFSSL_AES_128
  8234. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  8235. {
  8236. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  8237. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  8238. };
  8239. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  8240. {
  8241. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  8242. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  8243. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  8244. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b,
  8245. 0x26,0x75,0x1f,0x67,0xa3,0xcb,0xb1,0x40,
  8246. 0xb1,0x80,0x8c,0xf1,0x87,0xa4,0xf4,0xdf
  8247. };
  8248. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  8249. {
  8250. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8251. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  8252. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  8253. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  8254. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  8255. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef
  8256. };
  8257. #endif /* WOLFSSL_AES_128 */
  8258. #ifdef WOLFSSL_AES_192
  8259. /* 192 size key test */
  8260. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  8261. {
  8262. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  8263. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  8264. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  8265. };
  8266. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  8267. {
  8268. 0xcd,0xc8,0x0d,0x6f,0xdd,0xf1,0x8c,0xab,
  8269. 0x34,0xc2,0x59,0x09,0xc9,0x9a,0x41,0x74,
  8270. 0x67,0xce,0x7f,0x7f,0x81,0x17,0x36,0x21,
  8271. 0x96,0x1a,0x2b,0x70,0x17,0x1d,0x3d,0x7a,
  8272. 0x2e,0x1e,0x8a,0x1d,0xd5,0x9b,0x88,0xb1,
  8273. 0xc8,0xe6,0x0f,0xed,0x1e,0xfa,0xc4,0xc9,
  8274. 0xc0,0x5f,0x9f,0x9c,0xa9,0x83,0x4f,0xa0,
  8275. 0x42,0xae,0x8f,0xba,0x58,0x4b,0x09,0xff
  8276. };
  8277. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  8278. {
  8279. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8280. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  8281. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  8282. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  8283. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  8284. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  8285. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  8286. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  8287. };
  8288. #endif /* WOLFSSL_AES_192 */
  8289. #ifdef WOLFSSL_AES_256
  8290. /* 256 size key simple test */
  8291. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  8292. {
  8293. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8294. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8295. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8296. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8297. };
  8298. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  8299. {
  8300. 0xdc,0x7e,0x84,0xbf,0xda,0x79,0x16,0x4b,
  8301. 0x7e,0xcd,0x84,0x86,0x98,0x5d,0x38,0x60,
  8302. 0x39,0xff,0xed,0x14,0x3b,0x28,0xb1,0xc8,
  8303. 0x32,0x11,0x3c,0x63,0x31,0xe5,0x40,0x7b,
  8304. 0xdf,0x10,0x13,0x24,0x15,0xe5,0x4b,0x92,
  8305. 0xa1,0x3e,0xd0,0xa8,0x26,0x7a,0xe2,0xf9,
  8306. 0x75,0xa3,0x85,0x74,0x1a,0xb9,0xce,0xf8,
  8307. 0x20,0x31,0x62,0x3d,0x55,0xb1,0xe4,0x71
  8308. };
  8309. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  8310. {
  8311. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8312. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  8313. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  8314. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  8315. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  8316. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  8317. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  8318. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  8319. };
  8320. #endif /* WOLFSSL_AES_256 */
  8321. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8322. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8323. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8324. #ifdef HAVE_AES_DECRYPT
  8325. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8326. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8327. #endif
  8328. #endif
  8329. ret = wc_AesInit(enc, HEAP_HINT, devId);
  8330. if (ret != 0)
  8331. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8332. else
  8333. enc_inited = 1;
  8334. #ifdef HAVE_AES_DECRYPT
  8335. ret = wc_AesInit(dec, HEAP_HINT, devId);
  8336. if (ret != 0)
  8337. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8338. else
  8339. dec_inited = 1;
  8340. #endif
  8341. #ifdef WOLFSSL_AES_128
  8342. /* 128 key tests */
  8343. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8344. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8345. ret = EVP_test(EVP_aes_128_cfb128(), key1, iv, msg1, sizeof(msg1),
  8346. cipher1, sizeof(cipher1));
  8347. if (ret != 0) {
  8348. return ret;
  8349. }
  8350. #endif
  8351. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8352. if (ret != 0)
  8353. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8354. #ifdef HAVE_AES_DECRYPT
  8355. /* decrypt uses AES_ENCRYPTION */
  8356. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8357. if (ret != 0)
  8358. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8359. #endif
  8360. XMEMSET(cipher, 0, sizeof(cipher));
  8361. ret = wc_AesCfbEncrypt(enc, cipher, msg1, AES_BLOCK_SIZE * 2);
  8362. if (ret != 0)
  8363. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8364. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  8365. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8366. /* test restarting encryption process */
  8367. ret = wc_AesCfbEncrypt(enc, cipher + (AES_BLOCK_SIZE * 2),
  8368. msg1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE);
  8369. if (ret != 0)
  8370. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8371. if (XMEMCMP(cipher + (AES_BLOCK_SIZE * 2),
  8372. cipher1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE))
  8373. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8374. #ifdef HAVE_AES_DECRYPT
  8375. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 3);
  8376. if (ret != 0)
  8377. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8378. if (XMEMCMP(plain, msg1, AES_BLOCK_SIZE * 3))
  8379. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8380. #endif /* HAVE_AES_DECRYPT */
  8381. #endif /* WOLFSSL_AES_128 */
  8382. #ifdef WOLFSSL_AES_192
  8383. /* 192 key size test */
  8384. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8385. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8386. ret = EVP_test(EVP_aes_192_cfb128(), key2, iv, msg2, sizeof(msg2),
  8387. cipher2, sizeof(cipher2));
  8388. if (ret != 0) {
  8389. return ret;
  8390. }
  8391. #endif
  8392. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv, AES_ENCRYPTION);
  8393. if (ret != 0)
  8394. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8395. #ifdef HAVE_AES_DECRYPT
  8396. /* decrypt uses AES_ENCRYPTION */
  8397. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv, AES_ENCRYPTION);
  8398. if (ret != 0)
  8399. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8400. #endif
  8401. XMEMSET(cipher, 0, sizeof(cipher));
  8402. ret = wc_AesCfbEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE * 4);
  8403. if (ret != 0)
  8404. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8405. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE * 4))
  8406. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8407. #ifdef HAVE_AES_DECRYPT
  8408. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 4);
  8409. if (ret != 0)
  8410. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8411. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE * 4))
  8412. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8413. #endif /* HAVE_AES_DECRYPT */
  8414. #endif /* WOLFSSL_AES_192 */
  8415. #ifdef WOLFSSL_AES_256
  8416. /* 256 key size test */
  8417. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8418. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8419. ret = EVP_test(EVP_aes_256_cfb128(), key3, iv, msg3, sizeof(msg3),
  8420. cipher3, sizeof(cipher3));
  8421. if (ret != 0) {
  8422. return ret;
  8423. }
  8424. #endif
  8425. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv, AES_ENCRYPTION);
  8426. if (ret != 0)
  8427. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8428. #ifdef HAVE_AES_DECRYPT
  8429. /* decrypt uses AES_ENCRYPTION */
  8430. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv, AES_ENCRYPTION);
  8431. if (ret != 0)
  8432. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8433. #endif
  8434. /* test with data left overs, magic lengths are checking near edges */
  8435. XMEMSET(cipher, 0, sizeof(cipher));
  8436. ret = wc_AesCfbEncrypt(enc, cipher, msg3, 4);
  8437. if (ret != 0)
  8438. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8439. if (XMEMCMP(cipher, cipher3, 4))
  8440. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8441. ret = wc_AesCfbEncrypt(enc, cipher + 4, msg3 + 4, 27);
  8442. if (ret != 0)
  8443. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8444. if (XMEMCMP(cipher + 4, cipher3 + 4, 27))
  8445. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8446. ret = wc_AesCfbEncrypt(enc, cipher + 31, msg3 + 31,
  8447. (AES_BLOCK_SIZE * 4) - 31);
  8448. if (ret != 0)
  8449. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8450. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE * 4))
  8451. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8452. #ifdef HAVE_AES_DECRYPT
  8453. ret = wc_AesCfbDecrypt(dec, plain, cipher, 4);
  8454. if (ret != 0)
  8455. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8456. if (XMEMCMP(plain, msg3, 4))
  8457. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8458. ret = wc_AesCfbDecrypt(dec, plain + 4, cipher + 4, 4);
  8459. if (ret != 0)
  8460. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8461. ret = wc_AesCfbDecrypt(dec, plain + 8, cipher + 8, 23);
  8462. if (ret != 0)
  8463. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8464. if (XMEMCMP(plain + 4, msg3 + 4, 27))
  8465. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8466. ret = wc_AesCfbDecrypt(dec, plain + 31, cipher + 31,
  8467. (AES_BLOCK_SIZE * 4) - 31);
  8468. if (ret != 0)
  8469. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8470. if (XMEMCMP(plain, msg3, AES_BLOCK_SIZE * 4))
  8471. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8472. #endif /* HAVE_AES_DECRYPT */
  8473. #endif /* WOLFSSL_AES_256 */
  8474. out:
  8475. if (enc_inited)
  8476. wc_AesFree(enc);
  8477. #ifdef HAVE_AES_DECRYPT
  8478. if (dec_inited)
  8479. wc_AesFree(dec);
  8480. #endif
  8481. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8482. if (enc)
  8483. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8484. #ifdef HAVE_AES_DECRYPT
  8485. if (dec)
  8486. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8487. #endif
  8488. #endif
  8489. return ret;
  8490. }
  8491. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0))
  8492. static wc_test_ret_t aescfb1_test(void)
  8493. {
  8494. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8495. Aes *enc = NULL;
  8496. #else
  8497. Aes enc[1];
  8498. #endif
  8499. int enc_inited = 0;
  8500. byte cipher[AES_BLOCK_SIZE];
  8501. #ifdef HAVE_AES_DECRYPT
  8502. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8503. Aes *dec = NULL;
  8504. #else
  8505. Aes dec[1];
  8506. #endif
  8507. int dec_inited = 0;
  8508. byte plain [AES_BLOCK_SIZE];
  8509. #endif
  8510. wc_test_ret_t ret = 0;
  8511. #ifdef WOLFSSL_AES_128
  8512. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  8513. 0x4d,0xbb,0xdc,0xaa,0x59,0xf3,0x63,0xc9,
  8514. 0x2a,0x3b,0x98,0x43,0xad,0x20,0xe2,0xb7
  8515. };
  8516. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  8517. {
  8518. 0xcd,0xef,0x9d,0x06,0x61,0xba,0xe4,0x73,
  8519. 0x8d,0x1a,0x58,0xa2,0xa6,0x22,0x8b,0x66
  8520. };
  8521. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  8522. {
  8523. 0x00
  8524. };
  8525. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  8526. {
  8527. 0xC0
  8528. };
  8529. WOLFSSL_SMALL_STACK_STATIC const byte cipher1_7bit[] =
  8530. {
  8531. 0x1C
  8532. };
  8533. #endif /* WOLFSSL_AES_128 */
  8534. #ifdef WOLFSSL_AES_192
  8535. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  8536. 0x57,0xc6,0x89,0x7c,0x99,0x52,0x28,0x13,
  8537. 0xbf,0x67,0x9c,0xe1,0x13,0x70,0xaf,0x5e
  8538. };
  8539. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  8540. {
  8541. 0xba,0xa1,0x58,0xa1,0x6b,0x50,0x4a,0x10,
  8542. 0x8e,0xd4,0x33,0x2e,0xe7,0xf2,0x9b,0xf6,
  8543. 0xd1,0xac,0x46,0xa8,0xde,0x5a,0xfe,0x7a
  8544. };
  8545. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  8546. {
  8547. 0x30
  8548. };
  8549. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  8550. {
  8551. 0x80
  8552. };
  8553. #endif /* WOLFSSL_AES_192 */
  8554. #ifdef WOLFSSL_AES_256
  8555. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  8556. 0x63,0x2e,0x9f,0x83,0x1f,0xa3,0x80,0x5e,
  8557. 0x52,0x02,0xbc,0xe0,0x6d,0x04,0xf9,0xa0
  8558. };
  8559. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  8560. {
  8561. 0xf6,0xfa,0xe4,0xf1,0x5d,0x91,0xfc,0x50,
  8562. 0x88,0x78,0x4f,0x84,0xa5,0x37,0x12,0x7e,
  8563. 0x32,0x63,0x55,0x9c,0x62,0x73,0x88,0x20,
  8564. 0xc2,0xcf,0x3d,0xe1,0x1c,0x2a,0x30,0x40
  8565. };
  8566. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  8567. {
  8568. 0xF7, 0x00
  8569. };
  8570. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  8571. {
  8572. 0x41, 0xC0
  8573. };
  8574. #endif /* WOLFSSL_AES_256 */
  8575. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8576. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8577. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8578. #ifdef HAVE_AES_DECRYPT
  8579. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8580. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8581. #endif
  8582. #endif
  8583. ret = wc_AesInit(enc, HEAP_HINT, devId);
  8584. if (ret != 0)
  8585. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8586. else
  8587. enc_inited = 1;
  8588. #ifdef HAVE_AES_DECRYPT
  8589. ret = wc_AesInit(dec, HEAP_HINT, devId);
  8590. if (ret != 0)
  8591. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8592. else
  8593. dec_inited = 1;
  8594. #endif
  8595. #ifdef WOLFSSL_AES_128
  8596. /* 128 key tests */
  8597. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8598. if (ret != 0)
  8599. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8600. #ifdef HAVE_AES_DECRYPT
  8601. /* decrypt uses AES_ENCRYPTION */
  8602. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8603. if (ret != 0)
  8604. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8605. #endif
  8606. XMEMSET(cipher, 0, sizeof(cipher));
  8607. ret = wc_AesCfb1Encrypt(enc, cipher, msg1, 2);
  8608. if (ret != 0)
  8609. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8610. if (cipher[0] != cipher1[0])
  8611. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8612. #ifdef HAVE_AES_DECRYPT
  8613. ret = wc_AesCfb1Decrypt(dec, plain, cipher, 2);
  8614. if (ret != 0)
  8615. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8616. if (plain[0] != msg1[0])
  8617. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8618. #endif /* HAVE_AES_DECRYPT */
  8619. XMEMSET(cipher, 0, sizeof(cipher));
  8620. ret = wc_AesCfb1Encrypt(enc, cipher, msg1, 7);
  8621. if (ret != 0)
  8622. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8623. if (cipher[0] != cipher1_7bit[0])
  8624. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8625. #ifdef OPENSSL_EXTRA
  8626. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8627. if (ret != 0)
  8628. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8629. XMEMSET(cipher, 0, sizeof(cipher));
  8630. ret = wc_AesCfb1Encrypt(enc, cipher, msg1,
  8631. sizeof(msg1) * WOLFSSL_BIT_SIZE);
  8632. if (ret != 0)
  8633. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8634. #ifndef WOLFCRYPT_ONLY
  8635. ret = EVP_test(EVP_aes_128_cfb1(), key1, iv, msg1, sizeof(msg1),
  8636. cipher, sizeof(msg1));
  8637. if (ret != 0) {
  8638. goto out;
  8639. }
  8640. #endif
  8641. #endif
  8642. #endif /* WOLFSSL_AES_128 */
  8643. #ifdef WOLFSSL_AES_192
  8644. /* 192 key tests */
  8645. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8646. if (ret != 0)
  8647. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8648. XMEMSET(cipher, 0, sizeof(cipher));
  8649. ret = wc_AesCfb1Encrypt(enc, cipher, msg2, 4);
  8650. if (ret != 0)
  8651. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8652. if (XMEMCMP(cipher, cipher2, sizeof(cipher2)) != 0)
  8653. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8654. #ifdef OPENSSL_EXTRA
  8655. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8656. if (ret != 0)
  8657. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8658. XMEMSET(cipher, 0, sizeof(cipher));
  8659. ret = wc_AesCfb1Encrypt(enc, cipher, msg2,
  8660. sizeof(msg2) * WOLFSSL_BIT_SIZE);
  8661. if (ret != 0)
  8662. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8663. #ifndef WOLFCRYPT_ONLY
  8664. ret = EVP_test(EVP_aes_192_cfb1(), key2, iv2, msg2, sizeof(msg2),
  8665. cipher, sizeof(msg2));
  8666. if (ret != 0) {
  8667. goto out;
  8668. }
  8669. #endif
  8670. #endif
  8671. #endif /* WOLFSSL_AES_192 */
  8672. #ifdef WOLFSSL_AES_256
  8673. /* 256 key tests */
  8674. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  8675. if (ret != 0)
  8676. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8677. XMEMSET(cipher, 0, sizeof(cipher));
  8678. ret = wc_AesCfb1Encrypt(enc, cipher, msg3, 10);
  8679. if (ret != 0)
  8680. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8681. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  8682. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8683. #ifdef OPENSSL_EXTRA
  8684. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  8685. if (ret != 0)
  8686. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8687. XMEMSET(cipher, 0, sizeof(cipher));
  8688. ret = wc_AesCfb1Encrypt(enc, cipher, msg3,
  8689. sizeof(msg3) * WOLFSSL_BIT_SIZE);
  8690. if (ret != 0)
  8691. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8692. #ifndef WOLFCRYPT_ONLY
  8693. ret = EVP_test(EVP_aes_256_cfb1(), key3, iv3, msg3, sizeof(msg3),
  8694. cipher, sizeof(msg3));
  8695. if (ret != 0) {
  8696. goto out;
  8697. }
  8698. #endif
  8699. #endif
  8700. #endif /* WOLFSSL_AES_256 */
  8701. out:
  8702. if (enc_inited)
  8703. wc_AesFree(enc);
  8704. #ifdef HAVE_AES_DECRYPT
  8705. if (dec_inited)
  8706. wc_AesFree(dec);
  8707. #endif
  8708. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8709. if (enc)
  8710. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8711. #ifdef HAVE_AES_DECRYPT
  8712. if (dec)
  8713. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8714. #endif
  8715. #endif
  8716. return ret;
  8717. }
  8718. static wc_test_ret_t aescfb8_test(void)
  8719. {
  8720. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8721. Aes *enc = NULL;
  8722. #else
  8723. Aes enc[1];
  8724. #endif
  8725. int enc_inited = 0;
  8726. byte cipher[AES_BLOCK_SIZE];
  8727. #ifdef HAVE_AES_DECRYPT
  8728. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8729. Aes *dec = NULL;
  8730. #else
  8731. Aes dec[1];
  8732. #endif
  8733. int dec_inited = 0;
  8734. byte plain [AES_BLOCK_SIZE];
  8735. #endif
  8736. wc_test_ret_t ret = 0;
  8737. #ifdef WOLFSSL_AES_128
  8738. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  8739. 0xf4,0x75,0xc6,0x49,0x91,0xb2,0x0e,0xae,
  8740. 0xe1,0x83,0xa2,0x26,0x29,0xe2,0x1e,0x22
  8741. };
  8742. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  8743. {
  8744. 0xc8,0xfe,0x9b,0xf7,0x7b,0x93,0x0f,0x46,
  8745. 0xd2,0x07,0x8b,0x8c,0x0e,0x65,0x7c,0xd4
  8746. };
  8747. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  8748. {
  8749. 0xd2,0x76,0x91
  8750. };
  8751. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  8752. {
  8753. 0xc9,0x06,0x35
  8754. };
  8755. #endif /* WOLFSSL_AES_128 */
  8756. #ifdef WOLFSSL_AES_192
  8757. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  8758. 0x0a,0x02,0x84,0x6b,0x62,0xab,0xb6,0x93,
  8759. 0xef,0x31,0xd7,0x54,0x84,0x2e,0xed,0x29
  8760. };
  8761. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  8762. {
  8763. 0xba,0xf0,0x8b,0x76,0x31,0x7a,0x65,0xc5,
  8764. 0xf0,0x7a,0xe6,0xf5,0x7e,0xb0,0xe6,0x54,
  8765. 0x88,0x65,0x93,0x24,0xd2,0x97,0x09,0xe3
  8766. };
  8767. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  8768. {
  8769. 0x72,0x9c,0x0b,0x6d,0xeb,0x75,0xfa,0x6e,
  8770. 0xb5,0xe8
  8771. };
  8772. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  8773. {
  8774. 0x98,0x95,0x93,0x24,0x02,0x39,0x3d,0xc3,
  8775. 0x3a,0x60
  8776. };
  8777. #endif
  8778. #ifdef WOLFSSL_AES_256
  8779. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  8780. 0x33,0x8c,0x55,0x2f,0xf1,0xec,0xa1,0x44,
  8781. 0x08,0xe0,0x5d,0x8c,0xf9,0xf3,0xb3,0x1b
  8782. };
  8783. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  8784. {
  8785. 0x06,0x48,0x74,0x09,0x2f,0x7a,0x13,0xcc,
  8786. 0x44,0x62,0x24,0x7a,0xd4,0x23,0xd0,0xe9,
  8787. 0x6e,0xdf,0x42,0xe8,0xb6,0x7a,0x5a,0x23,
  8788. 0xb7,0xa0,0xa6,0x47,0x7b,0x09,0x8e,0x66
  8789. };
  8790. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  8791. {
  8792. 0x1c,0xff,0x95
  8793. };
  8794. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  8795. {
  8796. 0xb9,0x74,0xfa
  8797. };
  8798. #endif
  8799. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8800. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8801. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8802. #ifdef HAVE_AES_DECRYPT
  8803. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8804. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8805. #endif
  8806. #endif
  8807. ret = wc_AesInit(enc, HEAP_HINT, devId);
  8808. if (ret != 0)
  8809. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8810. else
  8811. enc_inited = 1;
  8812. #ifdef HAVE_AES_DECRYPT
  8813. ret = wc_AesInit(dec, HEAP_HINT, devId);
  8814. if (ret != 0)
  8815. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8816. else
  8817. dec_inited = 1;
  8818. #endif
  8819. #ifdef WOLFSSL_AES_128
  8820. /* 128 key tests */
  8821. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  8822. ret = EVP_test(EVP_aes_128_cfb8(), key1, iv, msg1, sizeof(msg1),
  8823. cipher1, sizeof(cipher1));
  8824. if (ret != 0) {
  8825. return ret;
  8826. }
  8827. #endif
  8828. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8829. if (ret != 0)
  8830. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8831. #ifdef HAVE_AES_DECRYPT
  8832. /* decrypt uses AES_ENCRYPTION */
  8833. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8834. if (ret != 0)
  8835. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8836. #endif
  8837. XMEMSET(cipher, 0, sizeof(cipher));
  8838. ret = wc_AesCfb8Encrypt(enc, cipher, msg1, sizeof(msg1));
  8839. if (ret != 0)
  8840. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8841. if (XMEMCMP(cipher, cipher1, sizeof(cipher1)) != 0)
  8842. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8843. #ifdef HAVE_AES_DECRYPT
  8844. ret = wc_AesCfb8Decrypt(dec, plain, cipher, sizeof(msg1));
  8845. if (ret != 0)
  8846. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8847. if (XMEMCMP(plain, msg1, sizeof(msg1)) != 0)
  8848. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8849. #endif /* HAVE_AES_DECRYPT */
  8850. #endif /* WOLFSSL_AES_128 */
  8851. #ifdef WOLFSSL_AES_192
  8852. /* 192 key tests */
  8853. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8854. if (ret != 0)
  8855. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8856. XMEMSET(cipher, 0, sizeof(cipher));
  8857. ret = wc_AesCfb8Encrypt(enc, cipher, msg2, sizeof(msg2));
  8858. if (ret != 0)
  8859. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8860. if (XMEMCMP(cipher, cipher2, sizeof(msg2)) != 0)
  8861. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8862. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  8863. ret = EVP_test(EVP_aes_192_cfb8(), key2, iv2, msg2, sizeof(msg2),
  8864. cipher2, sizeof(msg2));
  8865. if (ret != 0) {
  8866. return ret;
  8867. }
  8868. #endif
  8869. #endif /* WOLFSSL_AES_192 */
  8870. #ifdef WOLFSSL_AES_256
  8871. /* 256 key tests */
  8872. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  8873. if (ret != 0)
  8874. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8875. XMEMSET(cipher, 0, sizeof(cipher));
  8876. ret = wc_AesCfb8Encrypt(enc, cipher, msg3, sizeof(msg3));
  8877. if (ret != 0)
  8878. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8879. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  8880. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8881. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  8882. ret = EVP_test(EVP_aes_256_cfb8(), key3, iv3, msg3, sizeof(msg3),
  8883. cipher3, sizeof(msg3));
  8884. if (ret != 0) {
  8885. goto out;
  8886. }
  8887. #endif
  8888. #endif /* WOLFSSL_AES_256 */
  8889. out:
  8890. if (enc_inited)
  8891. wc_AesFree(enc);
  8892. #ifdef HAVE_AES_DECRYPT
  8893. if (dec_inited)
  8894. wc_AesFree(dec);
  8895. #endif
  8896. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8897. if (enc)
  8898. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8899. #ifdef HAVE_AES_DECRYPT
  8900. if (dec)
  8901. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8902. #endif
  8903. #endif
  8904. return ret;
  8905. }
  8906. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  8907. #endif /* WOLFSSL_AES_CFB */
  8908. static wc_test_ret_t aes_key_size_test(void)
  8909. {
  8910. wc_test_ret_t ret;
  8911. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8912. Aes *aes;
  8913. #else
  8914. Aes aes[1];
  8915. #endif
  8916. byte key16[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8917. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  8918. #ifndef WOLFSSL_CRYPTOCELL
  8919. byte key24[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8920. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  8921. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
  8922. #endif
  8923. byte key32[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8924. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  8925. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8926. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  8927. byte iv[] = "1234567890abcdef";
  8928. #ifndef HAVE_FIPS
  8929. word32 keySize;
  8930. #endif
  8931. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8932. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8933. return WC_TEST_RET_ENC_ERRNO;
  8934. #endif
  8935. #if !defined(HAVE_FIPS) || \
  8936. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  8937. /* w/ FIPS v1 (cert 2425) wc_AesInit just returns 0 always as it's not
  8938. * supported with that FIPS version */
  8939. ret = wc_AesInit(NULL, HEAP_HINT, devId);
  8940. if (ret != BAD_FUNC_ARG)
  8941. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8942. #endif
  8943. ret = wc_AesInit(aes, HEAP_HINT, devId);
  8944. /* 0 check OK for FIPSv1 */
  8945. if (ret != 0)
  8946. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8947. #ifndef HAVE_FIPS
  8948. /* Parameter Validation testing. */
  8949. ret = wc_AesGetKeySize(NULL, NULL);
  8950. if (ret != BAD_FUNC_ARG)
  8951. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8952. ret = wc_AesGetKeySize(aes, NULL);
  8953. if (ret != BAD_FUNC_ARG)
  8954. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8955. ret = wc_AesGetKeySize(NULL, &keySize);
  8956. if (ret != BAD_FUNC_ARG)
  8957. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8958. /* Crashes in FIPS */
  8959. ret = wc_AesSetKey(NULL, key16, sizeof(key16), iv, AES_ENCRYPTION);
  8960. if (ret != BAD_FUNC_ARG)
  8961. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8962. #endif
  8963. /* NULL IV indicates to use all zeros IV. */
  8964. ret = wc_AesSetKey(aes, key16, sizeof(key16), NULL, AES_ENCRYPTION);
  8965. #ifdef WOLFSSL_AES_128
  8966. if (ret != 0)
  8967. #else
  8968. if (ret != BAD_FUNC_ARG)
  8969. #endif
  8970. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8971. ret = wc_AesSetKey(aes, key32, sizeof(key32) - 1, iv, AES_ENCRYPTION);
  8972. if (ret != BAD_FUNC_ARG)
  8973. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8974. /* CryptoCell handles rounds internally */
  8975. #if !defined(HAVE_FIPS) && !defined(WOLFSSL_CRYPTOCELL)
  8976. /* PSA don't use aes->rounds */
  8977. #if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_AES)
  8978. /* Force invalid rounds */
  8979. aes->rounds = 16;
  8980. ret = wc_AesGetKeySize(aes, &keySize);
  8981. if (ret != BAD_FUNC_ARG)
  8982. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8983. #endif
  8984. #endif
  8985. ret = wc_AesSetKey(aes, key16, sizeof(key16), iv, AES_ENCRYPTION);
  8986. #ifdef WOLFSSL_AES_128
  8987. if (ret != 0)
  8988. #else
  8989. if (ret != BAD_FUNC_ARG)
  8990. #endif
  8991. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8992. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_128)
  8993. ret = wc_AesGetKeySize(aes, &keySize);
  8994. if (ret != 0 || keySize != sizeof(key16))
  8995. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8996. #endif
  8997. #ifndef WOLFSSL_CRYPTOCELL
  8998. /* Cryptocell only supports AES-128 key size */
  8999. ret = wc_AesSetKey(aes, key24, sizeof(key24), iv, AES_ENCRYPTION);
  9000. #ifdef WOLFSSL_AES_192
  9001. if (ret != 0)
  9002. #else
  9003. if (ret != BAD_FUNC_ARG)
  9004. #endif
  9005. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9006. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_192)
  9007. ret = wc_AesGetKeySize(aes, &keySize);
  9008. if (ret != 0 || keySize != sizeof(key24))
  9009. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9010. #endif
  9011. ret = wc_AesSetKey(aes, key32, sizeof(key32), iv, AES_ENCRYPTION);
  9012. #ifdef WOLFSSL_AES_256
  9013. if (ret != 0)
  9014. #else
  9015. if (ret != BAD_FUNC_ARG)
  9016. #endif
  9017. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9018. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_256)
  9019. ret = wc_AesGetKeySize(aes, &keySize);
  9020. if (ret != 0 || keySize != sizeof(key32))
  9021. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9022. #endif
  9023. #endif /* !WOLFSSL_CRYPTOCELL */
  9024. ret = 0; /* success */
  9025. out:
  9026. wc_AesFree(aes);
  9027. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9028. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  9029. #endif
  9030. return ret;
  9031. }
  9032. #if defined(WOLFSSL_AES_XTS) && (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3))
  9033. /* test vectors from http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html */
  9034. #ifdef WOLFSSL_AES_128
  9035. static wc_test_ret_t aes_xts_128_test(void)
  9036. {
  9037. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9038. XtsAes *aes = NULL;
  9039. #else
  9040. XtsAes aes[1];
  9041. #endif
  9042. int aes_inited = 0;
  9043. wc_test_ret_t ret = 0;
  9044. unsigned char buf[AES_BLOCK_SIZE * 2 + 8];
  9045. unsigned char cipher[AES_BLOCK_SIZE * 2 + 8];
  9046. #ifdef WOLFSSL_AESXTS_STREAM
  9047. struct XtsAesStreamData stream;
  9048. #endif
  9049. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  9050. !defined(WOLFSSL_AFALG)
  9051. #define LARGE_XTS_SZ 1024
  9052. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9053. byte* large_input = NULL;
  9054. #else
  9055. byte large_input[LARGE_XTS_SZ];
  9056. #endif
  9057. #endif
  9058. /* 128 key tests */
  9059. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  9060. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  9061. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  9062. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  9063. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  9064. };
  9065. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  9066. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  9067. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  9068. };
  9069. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  9070. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  9071. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  9072. };
  9073. /* plain text test of partial block is not from NIST test vector list */
  9074. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  9075. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  9076. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  9077. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  9078. };
  9079. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  9080. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a,
  9081. 0x82, 0x50, 0x81, 0xd5, 0xbe, 0x47, 0x1c, 0x63
  9082. };
  9083. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  9084. 0x39, 0x25, 0x79, 0x05, 0xdf, 0xcc, 0x77, 0x76,
  9085. 0x6c, 0x87, 0x0a, 0x80, 0x6a, 0x60, 0xe3, 0xc0,
  9086. 0x93, 0xd1, 0x2a, 0xcf, 0xcb, 0x51, 0x42, 0xfa,
  9087. 0x09, 0x69, 0x89, 0x62, 0x5b, 0x60, 0xdb, 0x16
  9088. };
  9089. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  9090. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  9091. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  9092. };
  9093. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  9094. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  9095. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  9096. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  9097. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  9098. };
  9099. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  9100. 0xd6, 0xbe, 0x04, 0x6d, 0x41, 0xf2, 0x3b, 0x5e,
  9101. 0xd7, 0x0b, 0x6b, 0x3d, 0x5c, 0x8e, 0x66, 0x23,
  9102. 0x2b, 0xe6, 0xb8, 0x07, 0xd4, 0xdc, 0xc6, 0x0e,
  9103. 0xff, 0x8d, 0xbc, 0x1d, 0x9f, 0x7f, 0xc8, 0x22
  9104. };
  9105. WOLFSSL_SMALL_STACK_STATIC unsigned char cp2[] = {
  9106. 0x2b, 0xf7, 0x2c, 0xf3, 0xeb, 0x85, 0xef, 0x7b,
  9107. 0x0b, 0x76, 0xa0, 0xaa, 0xf3, 0x3f, 0x25, 0x8b,
  9108. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a
  9109. };
  9110. #ifndef HAVE_FIPS /* FIPS requires different keys for main and tweak. */
  9111. WOLFSSL_SMALL_STACK_STATIC unsigned char k3[] = {
  9112. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9113. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9114. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9115. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9116. };
  9117. WOLFSSL_SMALL_STACK_STATIC unsigned char i3[] = {
  9118. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9119. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9120. };
  9121. WOLFSSL_SMALL_STACK_STATIC unsigned char p3[] = {
  9122. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9123. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9124. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9125. 0x20, 0xff, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9126. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
  9127. };
  9128. WOLFSSL_SMALL_STACK_STATIC unsigned char c3[] = {
  9129. 0xA2, 0x07, 0x47, 0x76, 0x3F, 0xEC, 0x0C, 0x23,
  9130. 0x1B, 0xD0, 0xBD, 0x46, 0x9A, 0x27, 0x38, 0x12,
  9131. 0x95, 0x02, 0x3D, 0x5D, 0xC6, 0x94, 0x51, 0x36,
  9132. 0xA0, 0x85, 0xD2, 0x69, 0x6E, 0x87, 0x0A, 0xBF,
  9133. 0xB5, 0x5A, 0xDD, 0xCB, 0x80, 0xE0, 0xFC, 0xCD
  9134. };
  9135. #endif /* HAVE_FIPS */
  9136. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9137. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9138. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9139. #endif
  9140. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  9141. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  9142. ret = EVP_test(EVP_aes_128_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  9143. if (ret != 0) {
  9144. printf("EVP_aes_128_xts failed!\n");
  9145. goto out;
  9146. }
  9147. #endif
  9148. XMEMSET(buf, 0, sizeof(buf));
  9149. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  9150. if (ret != 0)
  9151. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9152. else
  9153. aes_inited = 1;
  9154. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_ENCRYPTION);
  9155. if (ret != 0)
  9156. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9157. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  9158. #if defined(WOLFSSL_ASYNC_CRYPT)
  9159. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9160. #endif
  9161. if (ret != 0)
  9162. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9163. if (XMEMCMP(c2, buf, sizeof(c2)))
  9164. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9165. #ifdef WOLFSSL_AESXTS_STREAM
  9166. ret = wc_AesXtsEncryptInit(aes, i2, sizeof(i2), &stream);
  9167. #if defined(WOLFSSL_ASYNC_CRYPT)
  9168. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9169. #endif
  9170. if (ret != 0)
  9171. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9172. ret = wc_AesXtsEncryptUpdate(aes, buf, p2, AES_BLOCK_SIZE, &stream);
  9173. #if defined(WOLFSSL_ASYNC_CRYPT)
  9174. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9175. #endif
  9176. if (ret != 0)
  9177. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9178. ret = wc_AesXtsEncryptFinal(aes, buf + AES_BLOCK_SIZE, p2 + AES_BLOCK_SIZE, sizeof(p2) - AES_BLOCK_SIZE, &stream);
  9179. #if defined(WOLFSSL_ASYNC_CRYPT)
  9180. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9181. #endif
  9182. if (ret != 0)
  9183. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9184. if (XMEMCMP(c2, buf, sizeof(c2)))
  9185. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9186. #endif /* WOLFSSL_AESXTS_STREAM */
  9187. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9188. defined(WC_C_DYNAMIC_FALLBACK)
  9189. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9190. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  9191. #if defined(WOLFSSL_ASYNC_CRYPT)
  9192. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9193. #endif
  9194. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9195. if (ret != 0)
  9196. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9197. if (XMEMCMP(c2, buf, sizeof(c2)))
  9198. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9199. #endif
  9200. XMEMSET(buf, 0, sizeof(buf));
  9201. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  9202. if (ret != 0)
  9203. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9204. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  9205. #if defined(WOLFSSL_ASYNC_CRYPT)
  9206. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9207. #endif
  9208. if (ret != 0)
  9209. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9210. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  9211. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9212. #ifdef WOLFSSL_AESXTS_STREAM
  9213. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i2), &stream);
  9214. #if defined(WOLFSSL_ASYNC_CRYPT)
  9215. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9216. #endif
  9217. if (ret != 0)
  9218. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9219. ret = wc_AesXtsEncryptUpdate(aes, buf, p1, sizeof(p1), &stream);
  9220. #if defined(WOLFSSL_ASYNC_CRYPT)
  9221. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9222. #endif
  9223. if (ret != 0)
  9224. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9225. ret = wc_AesXtsEncryptFinal(aes, buf, NULL, 0, &stream);
  9226. #if defined(WOLFSSL_ASYNC_CRYPT)
  9227. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9228. #endif
  9229. if (ret != 0)
  9230. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9231. if (XMEMCMP(c1, buf, sizeof(c1)))
  9232. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9233. #endif /* WOLFSSL_AESXTS_STREAM */
  9234. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9235. defined(WC_C_DYNAMIC_FALLBACK)
  9236. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9237. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  9238. #if defined(WOLFSSL_ASYNC_CRYPT)
  9239. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9240. #endif
  9241. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9242. if (ret != 0)
  9243. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9244. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  9245. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9246. #endif
  9247. /* partial block encryption test */
  9248. XMEMSET(cipher, 0, sizeof(cipher));
  9249. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  9250. #if defined(WOLFSSL_ASYNC_CRYPT)
  9251. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9252. #endif
  9253. if (ret != 0)
  9254. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9255. if (XMEMCMP(cp2, cipher, sizeof(cp2)))
  9256. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9257. #ifdef WOLFSSL_AESXTS_STREAM
  9258. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &stream);
  9259. #if defined(WOLFSSL_ASYNC_CRYPT)
  9260. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9261. #endif
  9262. if (ret != 0)
  9263. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9264. ret = wc_AesXtsEncryptFinal(aes, buf, pp, sizeof(pp), &stream);
  9265. #if defined(WOLFSSL_ASYNC_CRYPT)
  9266. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9267. #endif
  9268. if (ret != 0)
  9269. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9270. ret = wc_AesXtsEncryptFinal(aes, buf, NULL, 0, &stream);
  9271. #if defined(WOLFSSL_ASYNC_CRYPT)
  9272. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9273. #endif
  9274. if (ret != 0)
  9275. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9276. if (XMEMCMP(cp2, buf, sizeof(cp2)))
  9277. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9278. #endif /* WOLFSSL_AESXTS_STREAM */
  9279. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9280. defined(WC_C_DYNAMIC_FALLBACK)
  9281. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9282. XMEMSET(cipher, 0, sizeof(cipher));
  9283. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  9284. #if defined(WOLFSSL_ASYNC_CRYPT)
  9285. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9286. #endif
  9287. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9288. if (ret != 0)
  9289. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9290. if (XMEMCMP(cp2, cipher, sizeof(cp2)))
  9291. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9292. #endif
  9293. /* partial block decrypt test */
  9294. XMEMSET(buf, 0, sizeof(buf));
  9295. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  9296. if (ret != 0)
  9297. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9298. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  9299. #if defined(WOLFSSL_ASYNC_CRYPT)
  9300. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9301. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9302. #else
  9303. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9304. #endif
  9305. #endif
  9306. if (ret != 0)
  9307. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9308. if (XMEMCMP(pp, buf, sizeof(pp)))
  9309. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9310. #ifdef WOLFSSL_AESXTS_STREAM
  9311. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  9312. #if defined(WOLFSSL_ASYNC_CRYPT)
  9313. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9314. #endif
  9315. if (ret != 0)
  9316. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9317. ret = wc_AesXtsDecryptFinal(aes, buf, cipher, sizeof(pp), &stream);
  9318. #if defined(WOLFSSL_ASYNC_CRYPT)
  9319. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9320. #endif
  9321. if (ret != 0)
  9322. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9323. if (XMEMCMP(pp, buf, sizeof(pp)))
  9324. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9325. #endif /* WOLFSSL_AESXTS_STREAM */
  9326. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9327. defined(WC_C_DYNAMIC_FALLBACK)
  9328. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9329. XMEMSET(buf, 0, sizeof(buf));
  9330. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  9331. #if defined(WOLFSSL_ASYNC_CRYPT)
  9332. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9333. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9334. #else
  9335. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9336. #endif
  9337. #endif
  9338. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9339. if (ret != 0)
  9340. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9341. if (XMEMCMP(pp, buf, sizeof(pp)))
  9342. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9343. #endif
  9344. /* NIST decrypt test vector */
  9345. XMEMSET(buf, 0, sizeof(buf));
  9346. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  9347. #if defined(WOLFSSL_ASYNC_CRYPT)
  9348. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9349. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9350. #else
  9351. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9352. #endif
  9353. #endif
  9354. if (ret != 0)
  9355. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9356. if (XMEMCMP(p1, buf, sizeof(p1)))
  9357. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9358. #ifdef WOLFSSL_AESXTS_STREAM
  9359. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  9360. #if defined(WOLFSSL_ASYNC_CRYPT)
  9361. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9362. #endif
  9363. if (ret != 0)
  9364. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9365. ret = wc_AesXtsDecryptFinal(aes, buf, c1, sizeof(c1), &stream);
  9366. #if defined(WOLFSSL_ASYNC_CRYPT)
  9367. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9368. #endif
  9369. if (ret != 0)
  9370. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9371. if (XMEMCMP(p1, buf, sizeof(p1)))
  9372. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9373. #endif /* WOLFSSL_AESXTS_STREAM */
  9374. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9375. defined(WC_C_DYNAMIC_FALLBACK)
  9376. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9377. XMEMSET(buf, 0, sizeof(buf));
  9378. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  9379. #if defined(WOLFSSL_ASYNC_CRYPT)
  9380. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9381. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9382. #else
  9383. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9384. #endif
  9385. #endif
  9386. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9387. if (ret != 0)
  9388. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9389. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  9390. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9391. #endif
  9392. /* fail case with decrypting using wrong key */
  9393. XMEMSET(buf, 0, sizeof(buf));
  9394. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  9395. #if defined(WOLFSSL_ASYNC_CRYPT)
  9396. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9397. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9398. #else
  9399. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9400. #endif
  9401. #endif
  9402. if (ret != 0)
  9403. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9404. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  9405. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9406. /* set correct key and retest */
  9407. XMEMSET(buf, 0, sizeof(buf));
  9408. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_DECRYPTION);
  9409. if (ret != 0)
  9410. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9411. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  9412. #if defined(WOLFSSL_ASYNC_CRYPT)
  9413. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9414. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9415. #else
  9416. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9417. #endif
  9418. #endif
  9419. if (ret != 0)
  9420. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9421. if (XMEMCMP(p2, buf, sizeof(p2)))
  9422. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9423. #ifndef HAVE_FIPS
  9424. /* Test ciphertext stealing in-place. */
  9425. XMEMCPY(buf, p3, sizeof(p3));
  9426. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_ENCRYPTION);
  9427. if (ret != 0)
  9428. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9429. ret = wc_AesXtsEncrypt(aes, buf, buf, sizeof(p3), i3, sizeof(i3));
  9430. #if defined(WOLFSSL_ASYNC_CRYPT)
  9431. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9432. #endif
  9433. if (ret != 0)
  9434. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9435. if (XMEMCMP(c3, buf, sizeof(c3)))
  9436. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9437. #ifdef WOLFSSL_AESXTS_STREAM
  9438. ret = wc_AesXtsEncryptInit(aes, i3, sizeof(i3), &stream);
  9439. #if defined(WOLFSSL_ASYNC_CRYPT)
  9440. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9441. #endif
  9442. if (ret != 0)
  9443. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9444. ret = wc_AesXtsEncryptUpdate(aes, buf, p3, AES_BLOCK_SIZE, &stream);
  9445. #if defined(WOLFSSL_ASYNC_CRYPT)
  9446. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9447. #endif
  9448. if (ret != 0)
  9449. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9450. ret = wc_AesXtsEncryptFinal(aes, buf + AES_BLOCK_SIZE, p3 + AES_BLOCK_SIZE, sizeof(p3) - AES_BLOCK_SIZE, &stream);
  9451. #if defined(WOLFSSL_ASYNC_CRYPT)
  9452. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9453. #endif
  9454. if (ret != 0)
  9455. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9456. if (XMEMCMP(c3, buf, sizeof(c3)))
  9457. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9458. #endif /* WOLFSSL_AESXTS_STREAM */
  9459. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_DECRYPTION);
  9460. if (ret != 0)
  9461. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9462. ret = wc_AesXtsDecrypt(aes, buf, buf, sizeof(c3), i3, sizeof(i3));
  9463. #if defined(WOLFSSL_ASYNC_CRYPT)
  9464. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9465. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9466. #else
  9467. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9468. #endif
  9469. #endif
  9470. if (ret != 0)
  9471. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9472. if (XMEMCMP(p3, buf, sizeof(p3)))
  9473. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9474. #ifdef WOLFSSL_AESXTS_STREAM
  9475. ret = wc_AesXtsDecryptInit(aes, i3, sizeof(i3), &stream);
  9476. #if defined(WOLFSSL_ASYNC_CRYPT)
  9477. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9478. #endif
  9479. if (ret != 0)
  9480. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9481. ret = wc_AesXtsDecryptUpdate(aes, buf, c3, AES_BLOCK_SIZE, &stream);
  9482. #if defined(WOLFSSL_ASYNC_CRYPT)
  9483. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9484. #endif
  9485. if (ret != 0)
  9486. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9487. ret = wc_AesXtsDecryptFinal(aes, buf + AES_BLOCK_SIZE, c3 + AES_BLOCK_SIZE, sizeof(c3) - AES_BLOCK_SIZE, &stream);
  9488. #if defined(WOLFSSL_ASYNC_CRYPT)
  9489. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9490. #endif
  9491. if (ret != 0)
  9492. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9493. if (XMEMCMP(p3, buf, sizeof(p3)))
  9494. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9495. #endif /* WOLFSSL_AESXTS_STREAM */
  9496. #endif /* !HAVE_FIPS */
  9497. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  9498. !defined(WOLFSSL_AFALG)
  9499. {
  9500. int i;
  9501. int j;
  9502. #ifdef WOLFSSL_AESXTS_STREAM
  9503. int k;
  9504. #endif
  9505. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9506. large_input = (byte *)XMALLOC(LARGE_XTS_SZ, HEAP_HINT,
  9507. DYNAMIC_TYPE_TMP_BUFFER);
  9508. if (large_input == NULL)
  9509. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), out);
  9510. #endif
  9511. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  9512. large_input[i] = (byte)i;
  9513. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  9514. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  9515. if (ret != 0)
  9516. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9517. ret = wc_AesXtsEncrypt(aes, large_input, large_input, (word32)j, i1,
  9518. sizeof(i1));
  9519. #if defined(WOLFSSL_ASYNC_CRYPT)
  9520. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9521. #endif
  9522. if (ret != 0)
  9523. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9524. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  9525. if (ret != 0)
  9526. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9527. ret = wc_AesXtsDecrypt(aes, large_input, large_input, (word32)j, i1,
  9528. sizeof(i1));
  9529. #if defined(WOLFSSL_ASYNC_CRYPT)
  9530. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9531. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  9532. WC_ASYNC_FLAG_NONE);
  9533. #else
  9534. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9535. #endif
  9536. #endif
  9537. if (ret != 0)
  9538. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9539. for (i = 0; i < j; i++) {
  9540. if (large_input[i] != (byte)i) {
  9541. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9542. }
  9543. }
  9544. }
  9545. #ifdef WOLFSSL_AESXTS_STREAM
  9546. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  9547. large_input[i] = (byte)i;
  9548. /* first, encrypt block by block then decrypt with a one-shot call. */
  9549. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  9550. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  9551. if (ret != 0)
  9552. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9553. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &stream);
  9554. #if defined(WOLFSSL_ASYNC_CRYPT)
  9555. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9556. #endif
  9557. if (ret != 0)
  9558. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9559. for (k = 0; k < j; k += AES_BLOCK_SIZE) {
  9560. if ((j - k) < AES_BLOCK_SIZE*2)
  9561. ret = wc_AesXtsEncryptFinal(aes, large_input + k, large_input + k, j - k, &stream);
  9562. else
  9563. ret = wc_AesXtsEncryptUpdate(aes, large_input + k, large_input + k, AES_BLOCK_SIZE, &stream);
  9564. #if defined(WOLFSSL_ASYNC_CRYPT)
  9565. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9566. #endif
  9567. if (ret != 0)
  9568. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9569. if ((j - k) < AES_BLOCK_SIZE*2)
  9570. break;
  9571. }
  9572. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  9573. if (ret != 0)
  9574. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9575. ret = wc_AesXtsDecrypt(aes, large_input, large_input, (word32)j, i1,
  9576. sizeof(i1));
  9577. #if defined(WOLFSSL_ASYNC_CRYPT)
  9578. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9579. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  9580. WC_ASYNC_FLAG_NONE);
  9581. #else
  9582. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9583. #endif
  9584. #endif
  9585. if (ret != 0)
  9586. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9587. for (i = 0; i < j; i++) {
  9588. if (large_input[i] != (byte)i) {
  9589. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9590. }
  9591. }
  9592. }
  9593. /* second, encrypt with a one-shot call then decrypt block by block. */
  9594. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  9595. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  9596. if (ret != 0)
  9597. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9598. ret = wc_AesXtsEncrypt(aes, large_input, large_input, (word32)j, i1,
  9599. sizeof(i1));
  9600. #if defined(WOLFSSL_ASYNC_CRYPT)
  9601. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9602. #endif
  9603. if (ret != 0)
  9604. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9605. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  9606. if (ret != 0)
  9607. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9608. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  9609. #if defined(WOLFSSL_ASYNC_CRYPT)
  9610. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9611. #endif
  9612. if (ret != 0)
  9613. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9614. for (k = 0; k < j; k += AES_BLOCK_SIZE) {
  9615. if ((j - k) < AES_BLOCK_SIZE*2)
  9616. ret = wc_AesXtsDecryptFinal(aes, large_input + k, large_input + k, j - k, &stream);
  9617. else
  9618. ret = wc_AesXtsDecryptUpdate(aes, large_input + k, large_input + k, AES_BLOCK_SIZE, &stream);
  9619. #if defined(WOLFSSL_ASYNC_CRYPT)
  9620. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9621. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  9622. WC_ASYNC_FLAG_NONE);
  9623. #else
  9624. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9625. #endif
  9626. #endif
  9627. if (ret != 0)
  9628. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9629. if ((j - k) < AES_BLOCK_SIZE*2)
  9630. break;
  9631. }
  9632. for (i = 0; i < j; i++) {
  9633. if (large_input[i] != (byte)i) {
  9634. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9635. }
  9636. }
  9637. }
  9638. #endif /* WOLFSSL_AESXTS_STREAM */
  9639. }
  9640. #endif /* !BENCH_EMBEDDED && !HAVE_CAVIUM &&
  9641. * !WOLFSSL_AFALG
  9642. */
  9643. out:
  9644. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  9645. !defined(WOLFSSL_AFALG) && defined(WOLFSSL_SMALL_STACK) && \
  9646. !defined(WOLFSSL_NO_MALLOC)
  9647. if (large_input)
  9648. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9649. #endif
  9650. if (aes_inited)
  9651. wc_AesXtsFree(aes);
  9652. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9653. if (aes)
  9654. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  9655. #endif
  9656. return ret;
  9657. }
  9658. #endif /* WOLFSSL_AES_128 */
  9659. #ifdef WOLFSSL_AES_192
  9660. static wc_test_ret_t aes_xts_192_test(void)
  9661. {
  9662. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9663. XtsAes *aes = NULL;
  9664. #else
  9665. XtsAes aes[1];
  9666. #endif
  9667. int aes_inited = 0;
  9668. wc_test_ret_t ret = 0;
  9669. unsigned char buf[AES_BLOCK_SIZE * 2 + 8];
  9670. unsigned char cipher[AES_BLOCK_SIZE * 2 + 8];
  9671. #ifdef WOLFSSL_AESXTS_STREAM
  9672. struct XtsAesStreamData stream;
  9673. #endif
  9674. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  9675. !defined(WOLFSSL_AFALG)
  9676. #define LARGE_XTS_SZ 1024
  9677. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9678. byte* large_input = NULL;
  9679. #else
  9680. byte large_input[LARGE_XTS_SZ];
  9681. #endif
  9682. #endif
  9683. /* 192 bit key tests */
  9684. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  9685. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  9686. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  9687. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  9688. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  9689. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  9690. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3
  9691. };
  9692. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  9693. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  9694. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  9695. };
  9696. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  9697. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  9698. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  9699. };
  9700. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  9701. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  9702. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  9703. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  9704. };
  9705. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  9706. 0x65, 0x37, 0x15, 0x53, 0xf1, 0x98, 0xab, 0xb4,
  9707. 0xdb, 0x4e, 0xd3, 0x69, 0xdf, 0x8e, 0x3a, 0xe0
  9708. };
  9709. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  9710. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  9711. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  9712. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  9713. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  9714. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  9715. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a
  9716. };
  9717. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  9718. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  9719. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  9720. };
  9721. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  9722. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  9723. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  9724. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  9725. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  9726. };
  9727. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  9728. 0x6c, 0xa6, 0xb5, 0x73, 0x48, 0xf1, 0x89, 0xfa,
  9729. 0xdd, 0x80, 0x72, 0x1f, 0xb8, 0x56, 0x0c, 0xa2,
  9730. 0x35, 0xd4, 0x08, 0xbf, 0x24, 0xcb, 0xec, 0xdb,
  9731. 0x81, 0xe0, 0xe6, 0x4f, 0x3d, 0x1c, 0x5c, 0x46
  9732. };
  9733. WOLFSSL_SMALL_STACK_STATIC unsigned char cp2[] = {
  9734. 0xe9, 0x58, 0xfe, 0xab, 0x66, 0xb4, 0xf1, 0x79,
  9735. 0x91, 0x3f, 0x91, 0xdc, 0x6f, 0xdf, 0xd6, 0xac,
  9736. 0x65, 0x37, 0x15, 0x53, 0xf1, 0x98, 0xab, 0xb4
  9737. };
  9738. #ifndef HAVE_FIPS /* FIPS requires different keys for main and tweak. */
  9739. WOLFSSL_SMALL_STACK_STATIC unsigned char k3[] = {
  9740. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9741. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9742. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9743. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9744. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9745. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
  9746. };
  9747. WOLFSSL_SMALL_STACK_STATIC unsigned char i3[] = {
  9748. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9749. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9750. };
  9751. WOLFSSL_SMALL_STACK_STATIC unsigned char p3[] = {
  9752. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9753. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9754. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9755. 0x20, 0xff, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  9756. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
  9757. };
  9758. WOLFSSL_SMALL_STACK_STATIC unsigned char c3[] = {
  9759. 0xa4, 0xf2, 0x71, 0x5d, 0x80, 0x60, 0x68, 0xa0,
  9760. 0x80, 0x61, 0xd7, 0xc1, 0x55, 0xc8, 0x3a, 0x2e,
  9761. 0xd7, 0xf4, 0x62, 0xaf, 0xbd, 0x2d, 0xf9, 0x5f,
  9762. 0xe8, 0xc5, 0x99, 0x3d, 0x58, 0x3c, 0xeb, 0xba,
  9763. 0x86, 0xea, 0x2c, 0x7e, 0x1f, 0xba, 0x81, 0xde
  9764. };
  9765. #endif /* HAVE_FIPS */
  9766. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9767. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9768. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9769. #endif
  9770. XMEMSET(buf, 0, sizeof(buf));
  9771. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  9772. if (ret != 0)
  9773. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9774. else
  9775. aes_inited = 1;
  9776. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_ENCRYPTION);
  9777. if (ret != 0)
  9778. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9779. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  9780. #if defined(WOLFSSL_ASYNC_CRYPT)
  9781. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9782. #endif
  9783. if (ret != 0)
  9784. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9785. if (XMEMCMP(c2, buf, sizeof(c2)))
  9786. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9787. #ifdef WOLFSSL_AESXTS_STREAM
  9788. ret = wc_AesXtsEncryptInit(aes, i2, sizeof(i2), &stream);
  9789. #if defined(WOLFSSL_ASYNC_CRYPT)
  9790. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9791. #endif
  9792. if (ret != 0)
  9793. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9794. ret = wc_AesXtsEncryptUpdate(aes, buf, p2, AES_BLOCK_SIZE, &stream);
  9795. #if defined(WOLFSSL_ASYNC_CRYPT)
  9796. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9797. #endif
  9798. if (ret != 0)
  9799. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9800. ret = wc_AesXtsEncryptFinal(aes, buf + AES_BLOCK_SIZE, p2 + AES_BLOCK_SIZE, sizeof(p2) - AES_BLOCK_SIZE, &stream);
  9801. #if defined(WOLFSSL_ASYNC_CRYPT)
  9802. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9803. #endif
  9804. if (ret != 0)
  9805. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9806. if (XMEMCMP(c2, buf, sizeof(c2)))
  9807. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9808. #endif /* WOLFSSL_AESXTS_STREAM */
  9809. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9810. defined(WC_C_DYNAMIC_FALLBACK)
  9811. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9812. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  9813. #if defined(WOLFSSL_ASYNC_CRYPT)
  9814. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9815. #endif
  9816. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9817. if (ret != 0)
  9818. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9819. if (XMEMCMP(c2, buf, sizeof(c2)))
  9820. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9821. #endif
  9822. XMEMSET(buf, 0, sizeof(buf));
  9823. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  9824. if (ret != 0)
  9825. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9826. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  9827. #if defined(WOLFSSL_ASYNC_CRYPT)
  9828. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9829. #endif
  9830. if (ret != 0)
  9831. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9832. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  9833. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9834. #ifdef WOLFSSL_AESXTS_STREAM
  9835. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &stream);
  9836. #if defined(WOLFSSL_ASYNC_CRYPT)
  9837. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9838. #endif
  9839. if (ret != 0)
  9840. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9841. ret = wc_AesXtsEncryptUpdate(aes, buf, p1, sizeof(p1), &stream);
  9842. #if defined(WOLFSSL_ASYNC_CRYPT)
  9843. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9844. #endif
  9845. if (ret != 0)
  9846. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9847. ret = wc_AesXtsEncryptFinal(aes, buf, NULL, 0, &stream);
  9848. #if defined(WOLFSSL_ASYNC_CRYPT)
  9849. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9850. #endif
  9851. if (ret != 0)
  9852. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9853. if (XMEMCMP(c1, buf, sizeof(c1)))
  9854. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9855. #endif /* WOLFSSL_AESXTS_STREAM */
  9856. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9857. defined(WC_C_DYNAMIC_FALLBACK)
  9858. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9859. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  9860. #if defined(WOLFSSL_ASYNC_CRYPT)
  9861. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9862. #endif
  9863. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9864. if (ret != 0)
  9865. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9866. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  9867. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9868. #endif
  9869. /* partial block encryption test */
  9870. XMEMSET(cipher, 0, sizeof(cipher));
  9871. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  9872. #if defined(WOLFSSL_ASYNC_CRYPT)
  9873. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9874. #endif
  9875. if (ret != 0)
  9876. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9877. if (XMEMCMP(cp2, cipher, sizeof(cp2)))
  9878. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9879. #ifdef WOLFSSL_AESXTS_STREAM
  9880. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &stream);
  9881. #if defined(WOLFSSL_ASYNC_CRYPT)
  9882. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9883. #endif
  9884. if (ret != 0)
  9885. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9886. ret = wc_AesXtsEncryptFinal(aes, buf, pp, sizeof(pp), &stream);
  9887. #if defined(WOLFSSL_ASYNC_CRYPT)
  9888. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9889. #endif
  9890. if (ret != 0)
  9891. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9892. ret = wc_AesXtsEncryptFinal(aes, buf, NULL, 0, &stream);
  9893. #if defined(WOLFSSL_ASYNC_CRYPT)
  9894. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9895. #endif
  9896. if (ret != 0)
  9897. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9898. if (XMEMCMP(cp2, buf, sizeof(cp2)))
  9899. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9900. #endif /* WOLFSSL_AESXTS_STREAM */
  9901. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9902. defined(WC_C_DYNAMIC_FALLBACK)
  9903. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9904. XMEMSET(cipher, 0, sizeof(cipher));
  9905. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  9906. #if defined(WOLFSSL_ASYNC_CRYPT)
  9907. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9908. #endif
  9909. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9910. if (ret != 0)
  9911. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9912. if (XMEMCMP(cp2, cipher, sizeof(cp2)))
  9913. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9914. #endif
  9915. /* partial block decrypt test */
  9916. XMEMSET(buf, 0, sizeof(buf));
  9917. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  9918. if (ret != 0)
  9919. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9920. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  9921. #if defined(WOLFSSL_ASYNC_CRYPT)
  9922. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9923. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9924. #else
  9925. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9926. #endif
  9927. #endif
  9928. if (ret != 0)
  9929. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9930. if (XMEMCMP(pp, buf, sizeof(pp)))
  9931. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9932. #ifdef WOLFSSL_AESXTS_STREAM
  9933. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  9934. #if defined(WOLFSSL_ASYNC_CRYPT)
  9935. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9936. #endif
  9937. if (ret != 0)
  9938. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9939. ret = wc_AesXtsDecryptFinal(aes, buf, cipher, sizeof(pp), &stream);
  9940. #if defined(WOLFSSL_ASYNC_CRYPT)
  9941. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9942. #endif
  9943. if (ret != 0)
  9944. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9945. if (XMEMCMP(pp, buf, sizeof(pp)))
  9946. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9947. #endif /* WOLFSSL_AESXTS_STREAM */
  9948. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9949. defined(WC_C_DYNAMIC_FALLBACK)
  9950. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9951. XMEMSET(buf, 0, sizeof(buf));
  9952. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  9953. #if defined(WOLFSSL_ASYNC_CRYPT)
  9954. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9955. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9956. #else
  9957. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9958. #endif
  9959. #endif
  9960. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  9961. if (ret != 0)
  9962. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9963. if (XMEMCMP(pp, buf, sizeof(pp)))
  9964. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9965. #endif
  9966. /* NIST decrypt test vector */
  9967. XMEMSET(buf, 0, sizeof(buf));
  9968. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  9969. #if defined(WOLFSSL_ASYNC_CRYPT)
  9970. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  9971. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  9972. #else
  9973. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9974. #endif
  9975. #endif
  9976. if (ret != 0)
  9977. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9978. if (XMEMCMP(p1, buf, sizeof(p1)))
  9979. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9980. #ifdef WOLFSSL_AESXTS_STREAM
  9981. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  9982. #if defined(WOLFSSL_ASYNC_CRYPT)
  9983. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9984. #endif
  9985. if (ret != 0)
  9986. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9987. ret = wc_AesXtsDecryptFinal(aes, buf, c1, sizeof(c1), &stream);
  9988. #if defined(WOLFSSL_ASYNC_CRYPT)
  9989. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  9990. #endif
  9991. if (ret != 0)
  9992. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9993. if (XMEMCMP(p1, buf, sizeof(p1)))
  9994. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9995. #endif /* WOLFSSL_AESXTS_STREAM */
  9996. #if defined(DEBUG_VECTOR_REGISTER_ACCESS_AESXTS) && \
  9997. defined(WC_C_DYNAMIC_FALLBACK)
  9998. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  9999. XMEMSET(buf, 0, sizeof(buf));
  10000. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  10001. #if defined(WOLFSSL_ASYNC_CRYPT)
  10002. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10003. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10004. #else
  10005. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10006. #endif
  10007. #endif
  10008. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  10009. if (ret != 0)
  10010. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10011. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  10012. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10013. #endif
  10014. /* fail case with decrypting using wrong key */
  10015. XMEMSET(buf, 0, sizeof(buf));
  10016. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  10017. #if defined(WOLFSSL_ASYNC_CRYPT)
  10018. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10019. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10020. #else
  10021. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10022. #endif
  10023. #endif
  10024. if (ret != 0)
  10025. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10026. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  10027. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10028. /* set correct key and retest */
  10029. XMEMSET(buf, 0, sizeof(buf));
  10030. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_DECRYPTION);
  10031. if (ret != 0)
  10032. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10033. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  10034. #if defined(WOLFSSL_ASYNC_CRYPT)
  10035. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10036. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10037. #else
  10038. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10039. #endif
  10040. #endif
  10041. if (ret != 0)
  10042. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10043. if (XMEMCMP(p2, buf, sizeof(p2)))
  10044. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10045. #ifndef HAVE_FIPS
  10046. /* Test ciphertext stealing in-place. */
  10047. XMEMCPY(buf, p3, sizeof(p3));
  10048. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_ENCRYPTION);
  10049. if (ret != 0)
  10050. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10051. ret = wc_AesXtsEncrypt(aes, buf, buf, sizeof(p3), i3, sizeof(i3));
  10052. #if defined(WOLFSSL_ASYNC_CRYPT)
  10053. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10054. #endif
  10055. if (ret != 0)
  10056. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10057. if (XMEMCMP(c3, buf, sizeof(c3)))
  10058. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10059. #ifdef WOLFSSL_AESXTS_STREAM
  10060. ret = wc_AesXtsEncryptInit(aes, i3, sizeof(i3), &stream);
  10061. #if defined(WOLFSSL_ASYNC_CRYPT)
  10062. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10063. #endif
  10064. if (ret != 0)
  10065. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10066. ret = wc_AesXtsEncryptUpdate(aes, buf, p3, AES_BLOCK_SIZE, &stream);
  10067. #if defined(WOLFSSL_ASYNC_CRYPT)
  10068. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10069. #endif
  10070. if (ret != 0)
  10071. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10072. ret = wc_AesXtsEncryptFinal(aes, buf + AES_BLOCK_SIZE, p3 + AES_BLOCK_SIZE, sizeof(p3) - AES_BLOCK_SIZE, &stream);
  10073. #if defined(WOLFSSL_ASYNC_CRYPT)
  10074. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10075. #endif
  10076. if (ret != 0)
  10077. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10078. if (XMEMCMP(c3, buf, sizeof(c3)))
  10079. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10080. #endif /* WOLFSSL_AESXTS_STREAM */
  10081. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_DECRYPTION);
  10082. if (ret != 0)
  10083. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10084. ret = wc_AesXtsDecrypt(aes, buf, buf, sizeof(c3), i3, sizeof(i3));
  10085. #if defined(WOLFSSL_ASYNC_CRYPT)
  10086. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10087. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10088. #else
  10089. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10090. #endif
  10091. #endif
  10092. if (ret != 0)
  10093. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10094. if (XMEMCMP(p3, buf, sizeof(p3)))
  10095. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10096. #ifdef WOLFSSL_AESXTS_STREAM
  10097. ret = wc_AesXtsDecryptInit(aes, i3, sizeof(i3), &stream);
  10098. #if defined(WOLFSSL_ASYNC_CRYPT)
  10099. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10100. #endif
  10101. if (ret != 0)
  10102. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10103. ret = wc_AesXtsDecryptUpdate(aes, buf, c3, AES_BLOCK_SIZE, &stream);
  10104. #if defined(WOLFSSL_ASYNC_CRYPT)
  10105. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10106. #endif
  10107. if (ret != 0)
  10108. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10109. ret = wc_AesXtsDecryptFinal(aes, buf + AES_BLOCK_SIZE, c3 + AES_BLOCK_SIZE, sizeof(c3) - AES_BLOCK_SIZE, &stream);
  10110. #if defined(WOLFSSL_ASYNC_CRYPT)
  10111. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10112. #endif
  10113. if (ret != 0)
  10114. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10115. if (XMEMCMP(p3, buf, sizeof(p3)))
  10116. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10117. #endif /* WOLFSSL_AESXTS_STREAM */
  10118. #endif /* !HAVE_FIPS */
  10119. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10120. !defined(WOLFSSL_AFALG)
  10121. {
  10122. int i;
  10123. int j;
  10124. #ifdef WOLFSSL_AESXTS_STREAM
  10125. int k;
  10126. #endif
  10127. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10128. large_input = (byte *)XMALLOC(LARGE_XTS_SZ, HEAP_HINT,
  10129. DYNAMIC_TYPE_TMP_BUFFER);
  10130. if (large_input == NULL)
  10131. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), out);
  10132. #endif
  10133. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  10134. large_input[i] = (byte)i;
  10135. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  10136. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10137. if (ret != 0)
  10138. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10139. ret = wc_AesXtsEncrypt(aes, large_input, large_input, j, i1,
  10140. sizeof(i1));
  10141. #if defined(WOLFSSL_ASYNC_CRYPT)
  10142. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10143. #endif
  10144. if (ret != 0)
  10145. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10146. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10147. if (ret != 0)
  10148. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10149. ret = wc_AesXtsDecrypt(aes, large_input, large_input, j, i1,
  10150. sizeof(i1));
  10151. #if defined(WOLFSSL_ASYNC_CRYPT)
  10152. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10153. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  10154. WC_ASYNC_FLAG_NONE);
  10155. #else
  10156. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10157. #endif
  10158. #endif
  10159. if (ret != 0)
  10160. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10161. for (i = 0; i < j; i++) {
  10162. if (large_input[i] != (byte)i) {
  10163. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10164. }
  10165. }
  10166. }
  10167. #ifdef WOLFSSL_AESXTS_STREAM
  10168. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  10169. large_input[i] = (byte)i;
  10170. /* first, encrypt block by block then decrypt with a one-shot call. */
  10171. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  10172. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10173. if (ret != 0)
  10174. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10175. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &stream);
  10176. #if defined(WOLFSSL_ASYNC_CRYPT)
  10177. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10178. #endif
  10179. if (ret != 0)
  10180. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10181. for (k = 0; k < j; k += AES_BLOCK_SIZE) {
  10182. if ((j - k) < AES_BLOCK_SIZE*2)
  10183. ret = wc_AesXtsEncryptFinal(aes, large_input + k, large_input + k, j - k, &stream);
  10184. else
  10185. ret = wc_AesXtsEncryptUpdate(aes, large_input + k, large_input + k, AES_BLOCK_SIZE, &stream);
  10186. #if defined(WOLFSSL_ASYNC_CRYPT)
  10187. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10188. #endif
  10189. if (ret != 0)
  10190. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10191. if ((j - k) < AES_BLOCK_SIZE*2)
  10192. break;
  10193. }
  10194. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10195. if (ret != 0)
  10196. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10197. ret = wc_AesXtsDecrypt(aes, large_input, large_input, (word32)j, i1,
  10198. sizeof(i1));
  10199. #if defined(WOLFSSL_ASYNC_CRYPT)
  10200. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10201. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  10202. WC_ASYNC_FLAG_NONE);
  10203. #else
  10204. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10205. #endif
  10206. #endif
  10207. if (ret != 0)
  10208. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10209. for (i = 0; i < j; i++) {
  10210. if (large_input[i] != (byte)i) {
  10211. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10212. }
  10213. }
  10214. }
  10215. /* second, encrypt with a one-shot call then decrypt block by block. */
  10216. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  10217. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10218. if (ret != 0)
  10219. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10220. ret = wc_AesXtsEncrypt(aes, large_input, large_input, (word32)j, i1,
  10221. sizeof(i1));
  10222. #if defined(WOLFSSL_ASYNC_CRYPT)
  10223. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10224. #endif
  10225. if (ret != 0)
  10226. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10227. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10228. if (ret != 0)
  10229. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10230. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  10231. #if defined(WOLFSSL_ASYNC_CRYPT)
  10232. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10233. #endif
  10234. if (ret != 0)
  10235. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10236. for (k = 0; k < j; k += AES_BLOCK_SIZE) {
  10237. if ((j - k) < AES_BLOCK_SIZE*2)
  10238. ret = wc_AesXtsDecryptFinal(aes, large_input + k, large_input + k, j - k, &stream);
  10239. else
  10240. ret = wc_AesXtsDecryptUpdate(aes, large_input + k, large_input + k, AES_BLOCK_SIZE, &stream);
  10241. #if defined(WOLFSSL_ASYNC_CRYPT)
  10242. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10243. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  10244. WC_ASYNC_FLAG_NONE);
  10245. #else
  10246. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10247. #endif
  10248. #endif
  10249. if (ret != 0)
  10250. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10251. if ((j - k) < AES_BLOCK_SIZE*2)
  10252. break;
  10253. }
  10254. for (i = 0; i < j; i++) {
  10255. if (large_input[i] != (byte)i) {
  10256. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10257. }
  10258. }
  10259. }
  10260. #endif /* WOLFSSL_AESXTS_STREAM */
  10261. }
  10262. #endif /* !BENCH_EMBEDDED && !HAVE_CAVIUM &&
  10263. * !WOLFSSL_AFALG
  10264. */
  10265. out:
  10266. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10267. !defined(WOLFSSL_AFALG) && defined(WOLFSSL_SMALL_STACK) && \
  10268. !defined(WOLFSSL_NO_MALLOC)
  10269. if (large_input)
  10270. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10271. #endif
  10272. if (aes_inited)
  10273. wc_AesXtsFree(aes);
  10274. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10275. if (aes)
  10276. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  10277. #endif
  10278. return ret;
  10279. }
  10280. #endif /* WOLFSSL_AES_192 */
  10281. #ifdef WOLFSSL_AES_256
  10282. static wc_test_ret_t aes_xts_256_test(void)
  10283. {
  10284. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10285. XtsAes *aes = NULL;
  10286. #else
  10287. XtsAes aes[1];
  10288. #endif
  10289. int aes_inited = 0;
  10290. wc_test_ret_t ret = 0;
  10291. unsigned char buf[AES_BLOCK_SIZE * 3];
  10292. unsigned char cipher[AES_BLOCK_SIZE * 3];
  10293. #ifdef WOLFSSL_AESXTS_STREAM
  10294. struct XtsAesStreamData stream;
  10295. #endif
  10296. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10297. !defined(WOLFSSL_AFALG)
  10298. #define LARGE_XTS_SZ 1024
  10299. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10300. byte* large_input = NULL;
  10301. #else
  10302. byte large_input[LARGE_XTS_SZ];
  10303. #endif
  10304. #endif
  10305. /* 256 key tests */
  10306. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  10307. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  10308. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  10309. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  10310. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  10311. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  10312. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3,
  10313. 0xd6, 0x19, 0xb7, 0xe2, 0xb9, 0x14, 0x1e, 0x58,
  10314. 0x31, 0x8e, 0xea, 0x39, 0x2c, 0xf4, 0x1b, 0x08
  10315. };
  10316. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  10317. 0xad, 0xf8, 0xd9, 0x26, 0x27, 0x46, 0x4a, 0xd2,
  10318. 0xf0, 0x42, 0x8e, 0x84, 0xa9, 0xf8, 0x75, 0x64
  10319. };
  10320. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  10321. 0x2e, 0xed, 0xea, 0x52, 0xcd, 0x82, 0x15, 0xe1,
  10322. 0xac, 0xc6, 0x47, 0xe8, 0x10, 0xbb, 0xc3, 0x64,
  10323. 0x2e, 0x87, 0x28, 0x7f, 0x8d, 0x2e, 0x57, 0xe3,
  10324. 0x6c, 0x0a, 0x24, 0xfb, 0xc1, 0x2a, 0x20, 0x2e
  10325. };
  10326. /* plain text test of partial block is not from NIST test vector list */
  10327. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  10328. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  10329. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  10330. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  10331. };
  10332. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  10333. 0xcb, 0xaa, 0xd0, 0xe2, 0xf6, 0xce, 0xa3, 0xf5,
  10334. 0x0b, 0x37, 0xf9, 0x34, 0xd4, 0x6a, 0x9b, 0x13,
  10335. 0x0b, 0x9d, 0x54, 0xf0, 0x7e, 0x34, 0xf3, 0x6a,
  10336. 0xf7, 0x93, 0xe8, 0x6f, 0x73, 0xc6, 0xd7, 0xdb
  10337. };
  10338. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  10339. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  10340. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  10341. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  10342. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  10343. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  10344. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a,
  10345. 0xb5, 0xb0, 0xc7, 0xe5, 0x2d, 0xb5, 0x06, 0x12,
  10346. 0xd2, 0xb5, 0x3a, 0xcb, 0x47, 0x8a, 0x53, 0xb4
  10347. };
  10348. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  10349. 0xe6, 0x42, 0x19, 0xed, 0xe0, 0xe1, 0xc2, 0xa0,
  10350. 0x0e, 0xf5, 0x58, 0x6a, 0xc4, 0x9b, 0xeb, 0x6f
  10351. };
  10352. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  10353. 0x24, 0xcb, 0x76, 0x22, 0x55, 0xb5, 0xa8, 0x00,
  10354. 0xf4, 0x6e, 0x80, 0x60, 0x56, 0x9e, 0x05, 0x53,
  10355. 0xbc, 0xfe, 0x86, 0x55, 0x3b, 0xca, 0xd5, 0x89,
  10356. 0xc7, 0x54, 0x1a, 0x73, 0xac, 0xc3, 0x9a, 0xbd,
  10357. 0x53, 0xc4, 0x07, 0x76, 0xd8, 0xe8, 0x22, 0x61,
  10358. 0x9e, 0xa9, 0xad, 0x77, 0xa0, 0x13, 0x4c, 0xfc
  10359. };
  10360. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  10361. 0xa3, 0xc6, 0xf3, 0xf3, 0x82, 0x79, 0x5b, 0x10,
  10362. 0x87, 0xd7, 0x02, 0x50, 0xdb, 0x2c, 0xd3, 0xb1,
  10363. 0xa1, 0x62, 0xa8, 0xb6, 0xdc, 0x12, 0x60, 0x61,
  10364. 0xc1, 0x0a, 0x84, 0xa5, 0x85, 0x3f, 0x3a, 0x89,
  10365. 0xe6, 0x6c, 0xdb, 0xb7, 0x9a, 0xb4, 0x28, 0x9b,
  10366. 0xc3, 0xea, 0xd8, 0x10, 0xe9, 0xc0, 0xaf, 0x92
  10367. };
  10368. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10369. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10370. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10371. #endif
  10372. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  10373. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  10374. ret = EVP_test(EVP_aes_256_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  10375. if (ret != 0) {
  10376. printf("EVP_aes_256_xts failed\n");
  10377. goto out;
  10378. }
  10379. #endif
  10380. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  10381. if (ret != 0)
  10382. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10383. else
  10384. aes_inited = 1;
  10385. XMEMSET(buf, 0, sizeof(buf));
  10386. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_ENCRYPTION);
  10387. if (ret != 0)
  10388. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10389. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  10390. #if defined(WOLFSSL_ASYNC_CRYPT)
  10391. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10392. #endif
  10393. if (ret != 0)
  10394. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10395. if (XMEMCMP(c2, buf, sizeof(c2)))
  10396. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10397. #ifdef WOLFSSL_AESXTS_STREAM
  10398. ret = wc_AesXtsEncryptInit(aes, i2, sizeof(i2), &stream);
  10399. #if defined(WOLFSSL_ASYNC_CRYPT)
  10400. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10401. #endif
  10402. if (ret != 0)
  10403. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10404. ret = wc_AesXtsEncryptUpdate(aes, buf, p2, AES_BLOCK_SIZE, &stream);
  10405. #if defined(WOLFSSL_ASYNC_CRYPT)
  10406. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10407. #endif
  10408. if (ret != 0)
  10409. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10410. ret = wc_AesXtsEncryptFinal(aes, buf + AES_BLOCK_SIZE, p2 + AES_BLOCK_SIZE, sizeof(p2) - AES_BLOCK_SIZE, &stream);
  10411. #if defined(WOLFSSL_ASYNC_CRYPT)
  10412. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10413. #endif
  10414. if (ret != 0)
  10415. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10416. if (XMEMCMP(c2, buf, sizeof(c2)))
  10417. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10418. #endif /* WOLFSSL_AESXTS_STREAM */
  10419. XMEMSET(buf, 0, sizeof(buf));
  10420. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10421. if (ret != 0)
  10422. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10423. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  10424. #if defined(WOLFSSL_ASYNC_CRYPT)
  10425. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10426. #endif
  10427. if (ret != 0)
  10428. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10429. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  10430. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10431. #ifdef WOLFSSL_AESXTS_STREAM
  10432. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &stream);
  10433. #if defined(WOLFSSL_ASYNC_CRYPT)
  10434. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10435. #endif
  10436. if (ret != 0)
  10437. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10438. ret = wc_AesXtsEncryptUpdate(aes, buf, p1, sizeof(p1), &stream);
  10439. #if defined(WOLFSSL_ASYNC_CRYPT)
  10440. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10441. #endif
  10442. if (ret != 0)
  10443. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10444. ret = wc_AesXtsEncryptFinal(aes, buf, NULL, 0, &stream);
  10445. #if defined(WOLFSSL_ASYNC_CRYPT)
  10446. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10447. #endif
  10448. if (ret != 0)
  10449. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10450. if (XMEMCMP(c1, buf, sizeof(c1)))
  10451. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10452. #endif /* WOLFSSL_AESXTS_STREAM */
  10453. /* partial block encryption test */
  10454. XMEMSET(cipher, 0, sizeof(cipher));
  10455. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  10456. #if defined(WOLFSSL_ASYNC_CRYPT)
  10457. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10458. #endif
  10459. if (ret != 0)
  10460. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10461. /* partial block decrypt test */
  10462. XMEMSET(buf, 0, sizeof(buf));
  10463. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10464. if (ret != 0)
  10465. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10466. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  10467. #if defined(WOLFSSL_ASYNC_CRYPT)
  10468. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10469. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10470. #else
  10471. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10472. #endif
  10473. #endif
  10474. if (ret != 0)
  10475. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10476. if (XMEMCMP(pp, buf, sizeof(pp)))
  10477. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10478. /* NIST decrypt test vector */
  10479. XMEMSET(buf, 0, sizeof(buf));
  10480. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  10481. #if defined(WOLFSSL_ASYNC_CRYPT)
  10482. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10483. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10484. #else
  10485. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10486. #endif
  10487. #endif
  10488. if (ret != 0)
  10489. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10490. if (XMEMCMP(p1, buf, sizeof(p1)))
  10491. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10492. #ifdef WOLFSSL_AESXTS_STREAM
  10493. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  10494. #if defined(WOLFSSL_ASYNC_CRYPT)
  10495. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10496. #endif
  10497. if (ret != 0)
  10498. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10499. ret = wc_AesXtsDecryptFinal(aes, buf, c1, sizeof(c1), &stream);
  10500. #if defined(WOLFSSL_ASYNC_CRYPT)
  10501. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10502. #endif
  10503. if (ret != 0)
  10504. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10505. if (XMEMCMP(p1, buf, sizeof(p1)))
  10506. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10507. #endif /* WOLFSSL_AESXTS_STREAM */
  10508. XMEMSET(buf, 0, sizeof(buf));
  10509. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_DECRYPTION);
  10510. if (ret != 0)
  10511. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10512. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  10513. #if defined(WOLFSSL_ASYNC_CRYPT)
  10514. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10515. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10516. #else
  10517. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10518. #endif
  10519. #endif
  10520. if (ret != 0)
  10521. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10522. if (XMEMCMP(p2, buf, sizeof(p2)))
  10523. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10524. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10525. !defined(WOLFSSL_AFALG)
  10526. {
  10527. int i;
  10528. int j;
  10529. #ifdef WOLFSSL_AESXTS_STREAM
  10530. int k;
  10531. #endif
  10532. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10533. large_input = (byte *)XMALLOC(LARGE_XTS_SZ, HEAP_HINT,
  10534. DYNAMIC_TYPE_TMP_BUFFER);
  10535. if (large_input == NULL)
  10536. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), out);
  10537. #endif
  10538. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  10539. large_input[i] = (byte)i;
  10540. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  10541. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10542. if (ret != 0)
  10543. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10544. ret = wc_AesXtsEncrypt(aes, large_input, large_input, j, i1,
  10545. sizeof(i1));
  10546. #if defined(WOLFSSL_ASYNC_CRYPT)
  10547. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10548. #endif
  10549. if (ret != 0)
  10550. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10551. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10552. if (ret != 0)
  10553. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10554. ret = wc_AesXtsDecrypt(aes, large_input, large_input, j, i1,
  10555. sizeof(i1));
  10556. #if defined(WOLFSSL_ASYNC_CRYPT)
  10557. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10558. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  10559. WC_ASYNC_FLAG_NONE);
  10560. #else
  10561. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10562. #endif
  10563. #endif
  10564. if (ret != 0)
  10565. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10566. for (i = 0; i < j; i++) {
  10567. if (large_input[i] != (byte)i) {
  10568. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10569. }
  10570. }
  10571. }
  10572. #ifdef WOLFSSL_AESXTS_STREAM
  10573. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  10574. large_input[i] = (byte)i;
  10575. /* first, encrypt block by block then decrypt with a one-shot call. */
  10576. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  10577. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10578. if (ret != 0)
  10579. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10580. ret = wc_AesXtsEncryptInit(aes, i1, sizeof(i1), &stream);
  10581. #if defined(WOLFSSL_ASYNC_CRYPT)
  10582. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10583. #endif
  10584. if (ret != 0)
  10585. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10586. for (k = 0; k < j; k += AES_BLOCK_SIZE) {
  10587. if ((j - k) < AES_BLOCK_SIZE*2)
  10588. ret = wc_AesXtsEncryptFinal(aes, large_input + k, large_input + k, j - k, &stream);
  10589. else
  10590. ret = wc_AesXtsEncryptUpdate(aes, large_input + k, large_input + k, AES_BLOCK_SIZE, &stream);
  10591. #if defined(WOLFSSL_ASYNC_CRYPT)
  10592. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10593. #endif
  10594. if (ret != 0)
  10595. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10596. if ((j - k) < AES_BLOCK_SIZE*2)
  10597. break;
  10598. }
  10599. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10600. if (ret != 0)
  10601. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10602. ret = wc_AesXtsDecrypt(aes, large_input, large_input, (word32)j, i1,
  10603. sizeof(i1));
  10604. #if defined(WOLFSSL_ASYNC_CRYPT)
  10605. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10606. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  10607. WC_ASYNC_FLAG_NONE);
  10608. #else
  10609. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10610. #endif
  10611. #endif
  10612. if (ret != 0)
  10613. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10614. for (i = 0; i < j; i++) {
  10615. if (large_input[i] != (byte)i) {
  10616. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10617. }
  10618. }
  10619. }
  10620. /* second, encrypt with a one-shot call then decrypt block by block. */
  10621. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  10622. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10623. if (ret != 0)
  10624. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10625. ret = wc_AesXtsEncrypt(aes, large_input, large_input, (word32)j, i1,
  10626. sizeof(i1));
  10627. #if defined(WOLFSSL_ASYNC_CRYPT)
  10628. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10629. #endif
  10630. if (ret != 0)
  10631. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10632. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10633. if (ret != 0)
  10634. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10635. ret = wc_AesXtsDecryptInit(aes, i1, sizeof(i1), &stream);
  10636. #if defined(WOLFSSL_ASYNC_CRYPT)
  10637. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10638. #endif
  10639. if (ret != 0)
  10640. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10641. for (k = 0; k < j; k += AES_BLOCK_SIZE) {
  10642. if ((j - k) < AES_BLOCK_SIZE*2)
  10643. ret = wc_AesXtsDecryptFinal(aes, large_input + k, large_input + k, j - k, &stream);
  10644. else
  10645. ret = wc_AesXtsDecryptUpdate(aes, large_input + k, large_input + k, AES_BLOCK_SIZE, &stream);
  10646. #if defined(WOLFSSL_ASYNC_CRYPT)
  10647. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10648. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev,
  10649. WC_ASYNC_FLAG_NONE);
  10650. #else
  10651. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10652. #endif
  10653. #endif
  10654. if (ret != 0)
  10655. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10656. if ((j - k) < AES_BLOCK_SIZE*2)
  10657. break;
  10658. }
  10659. for (i = 0; i < j; i++) {
  10660. if (large_input[i] != (byte)i) {
  10661. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10662. }
  10663. }
  10664. }
  10665. #endif /* WOLFSSL_AESXTS_STREAM */
  10666. }
  10667. #endif /* !BENCH_EMBEDDED && !HAVE_CAVIUM &&
  10668. * !WOLFSSL_AFALG
  10669. */
  10670. out:
  10671. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10672. !defined(WOLFSSL_AFALG) && defined(WOLFSSL_SMALL_STACK) && \
  10673. !defined(WOLFSSL_NO_MALLOC)
  10674. if (large_input)
  10675. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10676. #endif
  10677. if (aes_inited)
  10678. wc_AesXtsFree(aes);
  10679. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10680. if (aes)
  10681. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  10682. #endif
  10683. return ret;
  10684. }
  10685. #endif /* WOLFSSL_AES_256 */
  10686. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  10687. /* both 128 and 256 bit key test */
  10688. static wc_test_ret_t aes_xts_sector_test(void)
  10689. {
  10690. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10691. XtsAes *aes = NULL;
  10692. #else
  10693. XtsAes aes[1];
  10694. #endif
  10695. int aes_inited = 0;
  10696. wc_test_ret_t ret = 0;
  10697. unsigned char buf[AES_BLOCK_SIZE * 2];
  10698. /* 128 key tests */
  10699. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  10700. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  10701. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  10702. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  10703. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  10704. };
  10705. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  10706. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  10707. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  10708. };
  10709. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  10710. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  10711. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  10712. };
  10713. word64 s1 = 141;
  10714. /* 256 key tests */
  10715. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  10716. 0xef, 0x01, 0x0c, 0xa1, 0xa3, 0x66, 0x3e, 0x32,
  10717. 0x53, 0x43, 0x49, 0xbc, 0x0b, 0xae, 0x62, 0x23,
  10718. 0x2a, 0x15, 0x73, 0x34, 0x85, 0x68, 0xfb, 0x9e,
  10719. 0xf4, 0x17, 0x68, 0xa7, 0x67, 0x4f, 0x50, 0x7a,
  10720. 0x72, 0x7f, 0x98, 0x75, 0x53, 0x97, 0xd0, 0xe0,
  10721. 0xaa, 0x32, 0xf8, 0x30, 0x33, 0x8c, 0xc7, 0xa9,
  10722. 0x26, 0xc7, 0x73, 0xf0, 0x9e, 0x57, 0xb3, 0x57,
  10723. 0xcd, 0x15, 0x6a, 0xfb, 0xca, 0x46, 0xe1, 0xa0
  10724. };
  10725. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  10726. 0xed, 0x98, 0xe0, 0x17, 0x70, 0xa8, 0x53, 0xb4,
  10727. 0x9d, 0xb9, 0xe6, 0xaa, 0xf8, 0x8f, 0x0a, 0x41,
  10728. 0xb9, 0xb5, 0x6e, 0x91, 0xa5, 0xa2, 0xb1, 0x1d,
  10729. 0x40, 0x52, 0x92, 0x54, 0xf5, 0x52, 0x3e, 0x75
  10730. };
  10731. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  10732. 0xca, 0x20, 0xc5, 0x5e, 0x8d, 0xc1, 0x49, 0x68,
  10733. 0x7d, 0x25, 0x41, 0xde, 0x39, 0xc3, 0xdf, 0x63,
  10734. 0x00, 0xbb, 0x5a, 0x16, 0x3c, 0x10, 0xce, 0xd3,
  10735. 0x66, 0x6b, 0x13, 0x57, 0xdb, 0x8b, 0xd3, 0x9d
  10736. };
  10737. word64 s2 = 187;
  10738. #if !defined(BENCH_EMBEDDED) && \
  10739. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  10740. /* Sector size for encrypt/decrypt consecutive sectors testcase */
  10741. word32 sectorSz = 512;
  10742. unsigned char data[550];
  10743. WOLFSSL_SMALL_STACK_STATIC unsigned char k3[] = {
  10744. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  10745. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  10746. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  10747. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22
  10748. };
  10749. WOLFSSL_SMALL_STACK_STATIC unsigned char p3[] = {
  10750. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  10751. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10752. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
  10753. 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  10754. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
  10755. 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  10756. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53,
  10757. 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  10758. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
  10759. 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  10760. 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
  10761. 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  10762. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
  10763. 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  10764. 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
  10765. 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  10766. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb,
  10767. 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  10768. 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
  10769. 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  10770. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
  10771. 0xfc, 0xfd, 0xfe, 0xff,
  10772. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  10773. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10774. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
  10775. 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  10776. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
  10777. 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  10778. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53,
  10779. 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  10780. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
  10781. 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  10782. 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
  10783. 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  10784. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
  10785. 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  10786. 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
  10787. 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  10788. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb,
  10789. 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  10790. 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
  10791. 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  10792. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
  10793. 0xfc, 0xfd, 0xfe, 0xff,
  10794. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  10795. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10796. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  10797. };
  10798. WOLFSSL_SMALL_STACK_STATIC unsigned char c3[] = {
  10799. 0xb9, 0x6b, 0x2b, 0xfd, 0x61, 0x87, 0x84, 0xd5, 0x26, 0xd2, 0x8c, 0x62,
  10800. 0x63, 0x01, 0xca, 0x46, 0xb1, 0x82, 0xfa, 0xdc, 0xbc, 0x32, 0x18, 0xe9,
  10801. 0xda, 0xe6, 0xda, 0xd1, 0x1a, 0x52, 0x77, 0xca, 0xdb, 0x0e, 0xbe, 0x37,
  10802. 0x88, 0x36, 0x1c, 0x87, 0x16, 0x60, 0xfe, 0xa8, 0x9e, 0xf6, 0x48, 0x64,
  10803. 0x94, 0x34, 0x64, 0xed, 0xf6, 0x9a, 0xc5, 0x28, 0xc9, 0xed, 0x64, 0x80,
  10804. 0x85, 0xd8, 0x93, 0xa7, 0x50, 0xb1, 0x9d, 0x2f, 0x1e, 0x34, 0xcc, 0xb4,
  10805. 0x03, 0xfb, 0x6b, 0x43, 0x21, 0xa8, 0x5b, 0xc6, 0x59, 0x13, 0xd2, 0xb5,
  10806. 0xf5, 0x7b, 0xf6, 0xb2, 0xa4, 0x7a, 0xd2, 0x50, 0x26, 0xcb, 0xa4, 0x83,
  10807. 0xc3, 0x56, 0xb0, 0xb1, 0x14, 0x34, 0x12, 0x1b, 0xea, 0x26, 0x97, 0x24,
  10808. 0x54, 0xcc, 0x32, 0x4c, 0xa4, 0xc2, 0xa3, 0x07, 0xfa, 0x30, 0xa9, 0xf0,
  10809. 0x91, 0x17, 0x60, 0x68, 0x88, 0x7f, 0x34, 0x7e, 0xbd, 0x20, 0x33, 0x95,
  10810. 0x6e, 0xc0, 0xb6, 0x2b, 0xff, 0x7e, 0x61, 0x35, 0x9a, 0x88, 0xff, 0xd9,
  10811. 0x69, 0x21, 0xe7, 0x8f, 0x45, 0x02, 0xf9, 0xd7, 0xeb, 0xa6, 0x53, 0xf1,
  10812. 0x73, 0x04, 0xf1, 0x0b, 0x85, 0xc6, 0x1f, 0x4a, 0x51, 0x2f, 0x95, 0x87,
  10813. 0x5a, 0x67, 0x37, 0xb2, 0x87, 0xf7, 0xbe, 0x2a, 0x17, 0x57, 0xca, 0xfc,
  10814. 0xdd, 0x5f, 0x37, 0x48, 0x78, 0xbd, 0xfa, 0x75, 0xc9, 0xfa, 0x86, 0x7e,
  10815. 0xc4, 0x0f, 0x60, 0x85, 0xce, 0x12, 0x44, 0x7c, 0xd9, 0xb2, 0x50, 0xd9,
  10816. 0x57, 0x85, 0xa5, 0xd7, 0x68, 0x59, 0x03, 0x09, 0x97, 0x2e, 0x8e, 0xa5,
  10817. 0xe3, 0x98, 0xac, 0x16, 0xfb, 0x6d, 0x54, 0xc5, 0x5d, 0x7a, 0x33, 0x44,
  10818. 0x0a, 0x39, 0x91, 0xcc, 0x9f, 0x67, 0xf9, 0x89, 0xbb, 0x62, 0x02, 0xc4,
  10819. 0x22, 0xec, 0xcf, 0x97, 0x69, 0x81, 0x3d, 0x00, 0xfd, 0xeb, 0x55, 0x08,
  10820. 0xa2, 0xff, 0x97, 0xaa, 0x79, 0xde, 0x3c, 0x8a, 0x78, 0x71, 0x73, 0xa2,
  10821. 0x98, 0x2f, 0xd8, 0x5c, 0x62, 0x1c, 0x5c, 0x23, 0x0a, 0xd1, 0xf1, 0x81,
  10822. 0x8a, 0x12, 0xe7, 0x4d, 0xdd, 0x4f, 0xd4, 0xf1, 0xe8, 0x0f, 0x25, 0x79,
  10823. 0x45, 0x4a, 0x49, 0x49, 0x7e, 0x56, 0x91, 0x4e, 0xaa, 0xba, 0x18, 0xe1,
  10824. 0xe4, 0xbe, 0x21, 0xdc, 0x58, 0x60, 0x6f, 0x6a, 0x7f, 0xdc, 0x5e, 0x74,
  10825. 0x47, 0xbf, 0xeb, 0x84, 0xc4, 0x1e, 0x5a, 0x61, 0x64, 0xc8, 0x63, 0x68,
  10826. 0xfa, 0x17, 0x9c, 0xac, 0x60, 0x1c, 0xa5, 0x6e, 0x00, 0x21, 0x93, 0x3c,
  10827. 0xd7, 0xbb, 0x73, 0x45, 0xf7, 0x34, 0x81, 0x6c, 0xfa, 0xf2, 0x33, 0xfd,
  10828. 0xb1, 0x40, 0x30, 0x6b, 0x30, 0xd1, 0x83, 0x5e, 0x2e, 0x7a, 0xce, 0xa6,
  10829. 0x12, 0x2a, 0x15, 0x03, 0x78, 0x29, 0xb9, 0x07, 0xae, 0xe7, 0xc2, 0x78,
  10830. 0x74, 0x72, 0xa5, 0x0e, 0x6b, 0x1f, 0x78, 0xf2, 0x5a, 0x69, 0xb6, 0x2b,
  10831. 0x99, 0x94, 0x1f, 0x89, 0xd1, 0x21, 0x14, 0x4a, 0x54, 0xab, 0x5a, 0x9f,
  10832. 0xaa, 0xa7, 0x96, 0x0a, 0x21, 0xce, 0x30, 0xb6, 0x70, 0x81, 0xe9, 0xd3,
  10833. 0x71, 0xc0, 0xf1, 0x15, 0xe2, 0xf6, 0xd3, 0xcc, 0x41, 0x15, 0x9d, 0xd5,
  10834. 0xa3, 0xa4, 0xe0, 0xf8, 0x62, 0xc4, 0x76, 0x65, 0x63, 0x89, 0xa7, 0xe2,
  10835. 0xfb, 0xf5, 0xc9, 0x80, 0x15, 0x5b, 0xc1, 0x59, 0xb2, 0xd0, 0x01, 0x3a,
  10836. 0xf9, 0xab, 0x5b, 0x79, 0x54, 0xed, 0x6b, 0xf9, 0x1d, 0x9d, 0x87, 0x63,
  10837. 0x80, 0x4f, 0xec, 0x9c, 0x4f, 0xad, 0x97, 0x04, 0xff, 0x62, 0x4a, 0x17,
  10838. 0xc0, 0x09, 0x2a, 0x2c, 0x23, 0x4b, 0xc3, 0xb6, 0x6d, 0xed, 0xdb, 0x1a,
  10839. 0x6f, 0x56, 0x2b, 0x78, 0x92, 0x3a, 0x5c, 0x7f, 0xb2, 0x63, 0xd3, 0xd5,
  10840. 0x1a, 0xbe, 0xc2, 0x34, 0xc8, 0xad, 0x36, 0xb7, 0x12, 0xb8, 0xe1, 0xb7,
  10841. 0x52, 0x7f, 0x16, 0x84, 0x2c, 0x47, 0x7e, 0xf2, 0xa5, 0x36, 0x2e, 0xad,
  10842. 0xe7, 0xbb, 0xc0, 0x6f, 0x27, 0x8e, 0x41, 0x08, 0x75, 0xe5, 0xff, 0xde,
  10843. 0x08, 0x9f, 0x8c, 0x91, 0xba, 0xc9, 0x9d, 0x9f, 0x27, 0x90, 0x50, 0x44,
  10844. 0x24, 0xe7, 0x3d, 0x6f
  10845. };
  10846. word64 s3 = 0x000000ffffffffff;
  10847. #endif
  10848. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10849. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10850. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10851. #endif
  10852. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  10853. if (ret != 0)
  10854. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10855. else
  10856. aes_inited = 1;
  10857. XMEMSET(buf, 0, sizeof(buf));
  10858. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  10859. if (ret != 0)
  10860. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10861. ret = wc_AesXtsEncryptSector(aes, buf, p1, sizeof(p1), s1);
  10862. #if defined(WOLFSSL_ASYNC_CRYPT)
  10863. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10864. #endif
  10865. if (ret != 0)
  10866. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10867. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  10868. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10869. /* decrypt test */
  10870. XMEMSET(buf, 0, sizeof(buf));
  10871. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  10872. if (ret != 0)
  10873. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10874. ret = wc_AesXtsDecryptSector(aes, buf, c1, sizeof(c1), s1);
  10875. #if defined(WOLFSSL_ASYNC_CRYPT)
  10876. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10877. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10878. #else
  10879. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10880. #endif
  10881. #endif
  10882. if (ret != 0)
  10883. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10884. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  10885. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10886. /* 256 bit key tests */
  10887. XMEMSET(buf, 0, sizeof(buf));
  10888. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_ENCRYPTION);
  10889. if (ret != 0)
  10890. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10891. ret = wc_AesXtsEncryptSector(aes, buf, p2, sizeof(p2), s2);
  10892. #if defined(WOLFSSL_ASYNC_CRYPT)
  10893. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10894. #endif
  10895. if (ret != 0)
  10896. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10897. if (XMEMCMP(c2, buf, sizeof(c2)))
  10898. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10899. /* decrypt test */
  10900. XMEMSET(buf, 0, sizeof(buf));
  10901. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_DECRYPTION);
  10902. if (ret != 0)
  10903. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10904. ret = wc_AesXtsDecryptSector(aes, buf, c2, sizeof(c2), s2);
  10905. #if defined(WOLFSSL_ASYNC_CRYPT)
  10906. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10907. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10908. #else
  10909. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10910. #endif
  10911. #endif
  10912. if (ret != 0)
  10913. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10914. if (XMEMCMP(p2, buf, sizeof(p2)))
  10915. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10916. #if !defined(BENCH_EMBEDDED) && \
  10917. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  10918. /* encrypt consecutive sectors test */
  10919. XMEMSET(data, 0, sizeof(buf));
  10920. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_ENCRYPTION);
  10921. if (ret != 0)
  10922. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10923. ret = wc_AesXtsEncryptConsecutiveSectors(aes, data, p3,
  10924. sizeof(p3), s3, sectorSz);
  10925. #if defined(WOLFSSL_ASYNC_CRYPT)
  10926. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10927. #endif
  10928. if (ret != 0)
  10929. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10930. if (XMEMCMP(c3, data, sizeof(c3)))
  10931. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10932. /* decrypt consecutive sectors test */
  10933. XMEMSET(data, 0, sizeof(buf));
  10934. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_DECRYPTION);
  10935. if (ret != 0)
  10936. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10937. ret = wc_AesXtsDecryptConsecutiveSectors(aes, data, c3,
  10938. sizeof(c3), s3, sectorSz);
  10939. #if defined(WOLFSSL_ASYNC_CRYPT)
  10940. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  10941. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  10942. #else
  10943. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  10944. #endif
  10945. #endif
  10946. if (ret != 0)
  10947. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10948. if (XMEMCMP(p3, data, sizeof(p3)))
  10949. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10950. #endif /* !BENCH_EMBEDDED && (!HAVE_FIPS || FIPS_VERSION_GE(5, 3)) */
  10951. out:
  10952. if (aes_inited)
  10953. wc_AesXtsFree(aes);
  10954. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10955. if (aes)
  10956. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  10957. #endif
  10958. return ret;
  10959. }
  10960. #endif /* WOLFSSL_AES_128 && WOLFSSL_AES_256 */
  10961. #ifdef WOLFSSL_AES_128
  10962. /* testing of bad arguments */
  10963. static wc_test_ret_t aes_xts_args_test(void)
  10964. {
  10965. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10966. XtsAes *aes = NULL;
  10967. #else
  10968. XtsAes aes[1];
  10969. #endif
  10970. int aes_inited = 0;
  10971. wc_test_ret_t ret;
  10972. unsigned char buf[AES_BLOCK_SIZE * 2];
  10973. /* 128 key tests */
  10974. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  10975. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  10976. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  10977. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  10978. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  10979. };
  10980. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  10981. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  10982. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  10983. };
  10984. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  10985. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  10986. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  10987. };
  10988. word64 s1 = 141;
  10989. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10990. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10991. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10992. #endif
  10993. ret = wc_AesXtsInit(aes, HEAP_HINT, devId);
  10994. if (ret != 0)
  10995. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10996. else
  10997. aes_inited = 1;
  10998. if (wc_AesXtsSetKeyNoInit(NULL, k1, sizeof(k1), AES_ENCRYPTION) == 0)
  10999. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11000. if (wc_AesXtsSetKeyNoInit(aes, NULL, sizeof(k1), AES_ENCRYPTION) == 0)
  11001. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11002. /* encryption operations */
  11003. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  11004. if (ret != 0)
  11005. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11006. ret = wc_AesXtsEncryptSector(NULL, buf, p1, sizeof(p1), s1);
  11007. #if defined(WOLFSSL_ASYNC_CRYPT)
  11008. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  11009. #endif
  11010. if (ret == 0)
  11011. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11012. ret = wc_AesXtsEncryptSector(aes, NULL, p1, sizeof(p1), s1);
  11013. #if defined(WOLFSSL_ASYNC_CRYPT)
  11014. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  11015. #endif
  11016. if (ret == 0)
  11017. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11018. /* decryption operations */
  11019. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  11020. if (ret != 0)
  11021. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11022. ret = wc_AesXtsDecryptSector(NULL, buf, c1, sizeof(c1), s1);
  11023. #if defined(WOLFSSL_ASYNC_CRYPT)
  11024. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  11025. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  11026. #else
  11027. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  11028. #endif
  11029. #endif
  11030. if (ret == 0)
  11031. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11032. ret = wc_AesXtsDecryptSector(aes, NULL, c1, sizeof(c1), s1);
  11033. #if defined(WOLFSSL_ASYNC_CRYPT)
  11034. #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  11035. ret = wc_AsyncWait(ret, &aes->aes_decrypt.asyncDev, WC_ASYNC_FLAG_NONE);
  11036. #else
  11037. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  11038. #endif
  11039. #endif
  11040. if (ret == 0)
  11041. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11042. ret = 0;
  11043. out:
  11044. if (aes_inited)
  11045. wc_AesXtsFree(aes);
  11046. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11047. if (aes)
  11048. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  11049. #endif
  11050. return ret;
  11051. }
  11052. #endif /* WOLFSSL_AES_128 */
  11053. #endif /* WOLFSSL_AES_XTS && (!HAVE_FIPS || FIPS_VERSION_GE(5,3)) */
  11054. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  11055. static wc_test_ret_t aes_cbc_test(void)
  11056. {
  11057. byte cipher[AES_BLOCK_SIZE];
  11058. byte plain[AES_BLOCK_SIZE];
  11059. wc_test_ret_t ret;
  11060. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  11061. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  11062. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  11063. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  11064. };
  11065. byte key[] = "0123456789abcdef "; /* align */
  11066. byte iv[] = "1234567890abcdef "; /* align */
  11067. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  11068. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  11069. /* Parameter Validation testing. */
  11070. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key, 17, NULL);
  11071. if (ret != BAD_FUNC_ARG)
  11072. return WC_TEST_RET_ENC_EC(ret);
  11073. #ifdef HAVE_AES_DECRYPT
  11074. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key, 17, NULL);
  11075. if (ret != BAD_FUNC_ARG)
  11076. return WC_TEST_RET_ENC_EC(ret);
  11077. #endif
  11078. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key,
  11079. AES_BLOCK_SIZE, iv);
  11080. if (ret != 0)
  11081. return WC_TEST_RET_ENC_EC(ret);
  11082. #ifdef HAVE_AES_DECRYPT
  11083. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key,
  11084. AES_BLOCK_SIZE, iv);
  11085. if (ret != 0)
  11086. return WC_TEST_RET_ENC_EC(ret);
  11087. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE) != 0)
  11088. return WC_TEST_RET_ENC_NC;
  11089. #endif /* HAVE_AES_DECRYPT */
  11090. (void)plain;
  11091. return 0;
  11092. }
  11093. #endif
  11094. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11095. static wc_test_ret_t aesecb_test(void)
  11096. {
  11097. wc_test_ret_t ret = 0;
  11098. #if defined(WOLFSSL_AES_256)
  11099. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11100. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  11101. #else
  11102. Aes enc[1];
  11103. #endif
  11104. int enc_inited = 0;
  11105. byte cipher[AES_BLOCK_SIZE * 4];
  11106. #ifdef HAVE_AES_DECRYPT
  11107. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11108. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  11109. #else
  11110. Aes dec[1];
  11111. #endif
  11112. int dec_inited = 0;
  11113. byte plain[AES_BLOCK_SIZE * 4];
  11114. #endif /* HAVE_AES_DECRYPT */
  11115. {
  11116. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  11117. {
  11118. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  11119. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  11120. };
  11121. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  11122. {
  11123. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  11124. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  11125. };
  11126. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  11127. {
  11128. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  11129. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  11130. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  11131. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  11132. };
  11133. ret = wc_AesInit(enc, HEAP_HINT, devId);
  11134. if (ret != 0)
  11135. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11136. enc_inited = 1;
  11137. #if defined(HAVE_AES_DECRYPT)
  11138. ret = wc_AesInit(dec, HEAP_HINT, devId);
  11139. if (ret != 0)
  11140. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11141. dec_inited = 1;
  11142. #endif
  11143. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  11144. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  11145. if (ret != 0)
  11146. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11147. if (wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE) != 0)
  11148. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11149. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  11150. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11151. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  11152. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  11153. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11154. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  11155. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11156. if (ret != 0)
  11157. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11158. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11159. ret = wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE);
  11160. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11161. if (ret != 0)
  11162. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11163. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  11164. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11165. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  11166. ret = wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE);
  11167. if (ret != 0)
  11168. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11169. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  11170. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11171. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  11172. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  11173. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11174. ret = wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE);
  11175. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11176. if (ret != 0)
  11177. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11178. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  11179. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11180. #endif
  11181. #ifdef HAVE_AES_DECRYPT
  11182. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  11183. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  11184. if (ret != 0)
  11185. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11186. if (wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE) != 0)
  11187. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11188. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  11189. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11190. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  11191. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  11192. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11193. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  11194. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11195. if (ret != 0)
  11196. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11197. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11198. ret = wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  11199. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11200. if (ret != 0)
  11201. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11202. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  11203. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11204. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  11205. ret = wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  11206. if (ret != 0)
  11207. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11208. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  11209. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11210. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  11211. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  11212. if (ret != 0)
  11213. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11214. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11215. ret = wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  11216. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11217. if (ret != 0)
  11218. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11219. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  11220. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11221. #endif
  11222. #endif /* HAVE_AES_DECRYPT */
  11223. }
  11224. out:
  11225. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11226. if (enc) {
  11227. if (enc_inited)
  11228. wc_AesFree(enc);
  11229. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  11230. }
  11231. #ifdef HAVE_AES_DECRYPT
  11232. if (dec) {
  11233. if (dec_inited)
  11234. wc_AesFree(dec);
  11235. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  11236. }
  11237. #endif
  11238. #else
  11239. if (enc_inited)
  11240. wc_AesFree(enc);
  11241. #ifdef HAVE_AES_DECRYPT
  11242. if (dec_inited)
  11243. wc_AesFree(dec);
  11244. #endif
  11245. #endif
  11246. #endif /* WOLFSSL_AES_256 */
  11247. return ret;
  11248. }
  11249. #endif /* HAVE_AES_ECB */
  11250. #ifdef WOLFSSL_AES_COUNTER
  11251. static wc_test_ret_t aesctr_test(Aes* enc, Aes* dec, byte* cipher, byte* plain)
  11252. {
  11253. wc_test_ret_t ret = 0;
  11254. /* test vectors from "Recommendation for Block Cipher Modes of
  11255. * Operation" NIST Special Publication 800-38A */
  11256. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  11257. {
  11258. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  11259. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  11260. };
  11261. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  11262. {
  11263. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  11264. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  11265. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  11266. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  11267. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  11268. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  11269. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  11270. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  11271. };
  11272. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11273. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32[] =
  11274. {
  11275. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,
  11276. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  11277. };
  11278. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32_2[] =
  11279. {
  11280. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  11281. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xfe
  11282. };
  11283. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64[] =
  11284. {
  11285. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  11286. 0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  11287. };
  11288. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64_2[] =
  11289. {
  11290. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0,
  11291. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  11292. };
  11293. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96[] =
  11294. {
  11295. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  11296. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff
  11297. };
  11298. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96_2[] =
  11299. {
  11300. 0xff,0xff,0xff,0xf0,0xff,0xff,0xff,0xff,
  11301. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  11302. };
  11303. #endif
  11304. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128[] =
  11305. {
  11306. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  11307. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  11308. };
  11309. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11310. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128_2[] =
  11311. {
  11312. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  11313. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  11314. };
  11315. #endif
  11316. #ifdef WOLFSSL_AES_128
  11317. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  11318. {
  11319. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  11320. 0xc2
  11321. };
  11322. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Key[] =
  11323. {
  11324. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  11325. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  11326. };
  11327. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Cipher[] =
  11328. {
  11329. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  11330. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  11331. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  11332. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  11333. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  11334. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  11335. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  11336. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  11337. };
  11338. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11339. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32Cipher[] =
  11340. {
  11341. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  11342. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  11343. 0x7e,0xad,0xea,0x5c,0xd1
  11344. };
  11345. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32CipherLong[] =
  11346. {
  11347. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  11348. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  11349. 0x7e,0xad,0xea,0x5c,0xd1,0xb7,0x98,0xf0,
  11350. 0x22,0x20,0xfe,0x67,0xb0,0x02,0x23,0x50
  11351. };
  11352. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32_2CipherLong[] =
  11353. {
  11354. 0x6e,0xa1,0x27,0x4d,0xea,0x20,0x5f,0x39,
  11355. 0x68,0xc8,0xb6,0x78,0xde,0xfc,0x53,0x5c,
  11356. 0x90,0xc8,0xf6,0xc6,0xfa,0xe0,0x7b,0x09,
  11357. 0x7c,0xf8,0x9c,0x6a,0x5a,0xa5,0x17,0x7f,
  11358. 0x03,0x92,0x5f,0x4e,0x85,0xea,0x26,0xc9,
  11359. 0x5a,0xc2,0x74,0xe2,0xbf,0xe4,0x1b,0xd4
  11360. };
  11361. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64Cipher[] =
  11362. {
  11363. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  11364. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  11365. 0xae,0x74,0x94,0x72,0x8e
  11366. };
  11367. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64CipherLong[] =
  11368. {
  11369. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  11370. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  11371. 0xae,0x74,0x94,0x72,0x8e,0xd0,0x71,0xc0,
  11372. 0x89,0x8a,0xa1,0xb0,0x29,0xa0,0x10,0x9e
  11373. };
  11374. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64_2CipherLong[] =
  11375. {
  11376. 0x3f,0xe7,0xd5,0xf3,0xfa,0x09,0xfe,0x40,
  11377. 0xa6,0xa1,0x32,0x8b,0x57,0x12,0xb9,0xfa,
  11378. 0xf2,0x2d,0xe4,0x3c,0x66,0x1d,0x0a,0x8e,
  11379. 0x46,0xf8,0x2e,0x33,0xce,0x8d,0x4e,0x3b,
  11380. 0x17,0x67,0x9e,0x9f,0x76,0x9e,0xc2,0x99,
  11381. 0xd5,0xd4,0x71,0xed,0xb4,0x33,0xb2,0xcd
  11382. };
  11383. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96Cipher[] =
  11384. {
  11385. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  11386. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  11387. 0x9d,0x77,0xc9,0x5f,0x38
  11388. };
  11389. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96CipherLong[] =
  11390. {
  11391. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  11392. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  11393. 0x9d,0x77,0xc9,0x5f,0x38,0xb5,0x6e,0x44,
  11394. 0x21,0x8e,0xda,0x57,0xe0,0x41,0xc7,0x6a
  11395. };
  11396. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96_2CipherLong[] =
  11397. {
  11398. 0xc8,0x81,0x1a,0xbe,0xc7,0x5b,0x93,0x6f,
  11399. 0xe6,0x52,0xe4,0xb1,0x2d,0x1c,0x39,0xbc,
  11400. 0xeb,0x82,0x27,0x0a,0x7e,0xa5,0x0e,0x2d,
  11401. 0x32,0xda,0xbe,0x10,0x7a,0x10,0xcc,0xd3,
  11402. 0x6f,0xc6,0x83,0x28,0x05,0x57,0x8a,0x24,
  11403. 0x44,0x76,0x17,0x81,0xb9,0x5c,0x94,0x81
  11404. };
  11405. #endif
  11406. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128Cipher[] =
  11407. {
  11408. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  11409. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  11410. 0xd3,0xda,0xe1,0x5b,0x04
  11411. };
  11412. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128CipherLong[] =
  11413. {
  11414. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  11415. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  11416. 0xd3,0xda,0xe1,0x5b,0x04,0xbb,0x35,0x2f,
  11417. 0xa0,0xf5,0x9f,0xeb,0xfc,0xb4,0xda,0x3e
  11418. };
  11419. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11420. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128_2CipherLong[] =
  11421. {
  11422. 0xba,0x76,0xaa,0x54,0xd5,0xb5,0x60,0x67,
  11423. 0xc1,0xa7,0x90,0x3b,0x3f,0xdd,0xfa,0x89,
  11424. 0x24,0xdf,0x0c,0x56,0x5c,0xf4,0x2a,0x68,
  11425. 0x97,0x87,0x13,0xb6,0x7a,0xd1,0x24,0xfd,
  11426. 0x4d,0x3f,0x77,0x4a,0xb9,0xe4,0x7d,0xa2,
  11427. 0xdb,0xb9,0x31,0x5e,0xa3,0x11,0x06,0x80
  11428. };
  11429. #endif
  11430. #endif /* WOLFSSL_AES_128 */
  11431. #ifdef WOLFSSL_AES_192
  11432. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  11433. {
  11434. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  11435. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  11436. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  11437. };
  11438. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  11439. {
  11440. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  11441. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b,
  11442. 0x09,0x03,0x39,0xec,0x0a,0xa6,0xfa,0xef,
  11443. 0xd5,0xcc,0xc2,0xc6,0xf4,0xce,0x8e,0x94,
  11444. 0x1e,0x36,0xb2,0x6b,0xd1,0xeb,0xc6,0x70,
  11445. 0xd1,0xbd,0x1d,0x66,0x56,0x20,0xab,0xf7,
  11446. 0x4f,0x78,0xa7,0xf6,0xd2,0x98,0x09,0x58,
  11447. 0x5a,0x97,0xda,0xec,0x58,0xc6,0xb0,0x50
  11448. };
  11449. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11450. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32Cipher[] =
  11451. {
  11452. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  11453. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  11454. 0x0c,0x33,0x97,0x06,0xc0
  11455. };
  11456. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32CipherLong[] =
  11457. {
  11458. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  11459. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  11460. 0x0c,0x33,0x97,0x06,0xc0,0xbe,0x83,0x87,
  11461. 0xdd,0xd3,0xff,0xd8,0xe4,0x6a,0x5b,0x84
  11462. };
  11463. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32_2CipherLong[] =
  11464. {
  11465. 0xf5,0x00,0xa2,0x91,0x54,0xa3,0x76,0xa2,
  11466. 0xdd,0xad,0x16,0x89,0xe5,0xf0,0x1d,0x40,
  11467. 0x84,0xcd,0x74,0x84,0xcb,0x8b,0x9e,0x29,
  11468. 0xa9,0x8a,0x12,0x65,0xa0,0x79,0x5e,0xce,
  11469. 0xd9,0x50,0x65,0x21,0x86,0xb0,0x85,0x0d,
  11470. 0x98,0x2d,0x9a,0x5a,0x11,0xbe,0xa5,0x7f
  11471. };
  11472. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64Cipher[] =
  11473. {
  11474. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  11475. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  11476. 0x3d,0xf7,0xb4,0xfd,0x8c
  11477. };
  11478. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64CipherLong[] =
  11479. {
  11480. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  11481. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  11482. 0x3d,0xf7,0xb4,0xfd,0x8c,0x55,0x47,0x10,
  11483. 0xd5,0x91,0x35,0xbe,0xd8,0x0d,0xa5,0x9e
  11484. };
  11485. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64_2CipherLong[] =
  11486. {
  11487. 0x59,0xf1,0xed,0x70,0x62,0x42,0xa8,0x06,
  11488. 0x07,0x36,0xe1,0xc5,0x04,0x79,0xc3,0x9b,
  11489. 0xd1,0x14,0x5c,0xcc,0x6f,0x81,0x5f,0x2f,
  11490. 0xa0,0xde,0xcf,0x61,0x55,0x18,0x7a,0xac,
  11491. 0xb0,0x59,0x37,0x90,0x53,0xb3,0x00,0x88,
  11492. 0xb4,0x49,0x90,0x7b,0x96,0xcd,0xcc,0xc3
  11493. };
  11494. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96Cipher[] =
  11495. {
  11496. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  11497. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  11498. 0x47,0xb5,0xf3,0x30,0x3b
  11499. };
  11500. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96CipherLong[] =
  11501. {
  11502. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  11503. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  11504. 0x47,0xb5,0xf3,0x30,0x3b,0xef,0xcd,0x80,
  11505. 0xe3,0x61,0x34,0xef,0x4e,0x1b,0x79,0xc1
  11506. };
  11507. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96_2CipherLong[] =
  11508. {
  11509. 0x3c,0xb2,0xff,0xc0,0x24,0xe1,0xf5,0xc4,
  11510. 0x0f,0xd1,0x0a,0x1b,0xbe,0x1f,0x23,0xa1,
  11511. 0x8e,0xbf,0x2b,0x96,0xb6,0x37,0xc8,0x25,
  11512. 0x06,0x90,0xe2,0xca,0x71,0x24,0x52,0x95,
  11513. 0xaa,0x8c,0x80,0xdf,0xb7,0xd7,0x30,0xb0,
  11514. 0xcc,0x06,0x4f,0x28,0xa2,0x74,0x27,0xf8
  11515. };
  11516. #endif
  11517. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128Cipher[] =
  11518. {
  11519. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  11520. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  11521. 0x8c,0x68,0xa7,0xd9,0x57
  11522. };
  11523. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128CipherLong[] =
  11524. {
  11525. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  11526. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  11527. 0x8c,0x68,0xa7,0xd9,0x57,0xab,0x09,0x0f,
  11528. 0x01,0xc4,0x4e,0x62,0xaf,0xc2,0xdf,0x1a
  11529. };
  11530. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11531. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128_2CipherLong[] =
  11532. {
  11533. 0x88,0x0a,0x26,0x4e,0xa8,0x26,0x21,0xe0,
  11534. 0xfc,0xbc,0x63,0xdc,0xd9,0x60,0x52,0xb2,
  11535. 0x99,0x2f,0xbb,0x1e,0x00,0xf5,0x9f,0x6d,
  11536. 0xab,0x48,0x0f,0xc6,0x37,0x12,0x56,0xe3,
  11537. 0x12,0x8d,0x31,0xc8,0xea,0xf4,0x41,0x82,
  11538. 0x7a,0x88,0xe0,0xd7,0xf0,0x67,0x03,0xa4
  11539. };
  11540. #endif
  11541. #endif
  11542. #ifdef WOLFSSL_AES_256
  11543. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  11544. {
  11545. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  11546. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  11547. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  11548. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  11549. };
  11550. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  11551. {
  11552. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  11553. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,
  11554. 0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,
  11555. 0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5,
  11556. 0x2b,0x09,0x30,0xda,0xa2,0x3d,0xe9,0x4c,
  11557. 0xe8,0x70,0x17,0xba,0x2d,0x84,0x98,0x8d,
  11558. 0xdf,0xc9,0xc5,0x8d,0xb6,0x7a,0xad,0xa6,
  11559. 0x13,0xc2,0xdd,0x08,0x45,0x79,0x41,0xa6
  11560. };
  11561. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11562. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32Cipher[] =
  11563. {
  11564. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  11565. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  11566. 0xa2,0x13,0x47,0x74,0xda
  11567. };
  11568. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32CipherLong[] =
  11569. {
  11570. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  11571. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  11572. 0xa2,0x13,0x47,0x74,0xda,0x89,0x77,0x40,
  11573. 0x28,0x9c,0xe8,0x19,0x26,0x32,0xd8,0x1f
  11574. };
  11575. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32_2CipherLong[] =
  11576. {
  11577. 0xf6,0xd9,0x22,0xc6,0x80,0x29,0xaf,0x14,
  11578. 0x54,0x6c,0x0a,0xce,0x42,0xea,0x3c,0xa1,
  11579. 0x7c,0xeb,0x36,0x0d,0x8e,0xd7,0x8c,0x59,
  11580. 0xa8,0x09,0x9f,0x9e,0xba,0x5b,0x95,0xfa,
  11581. 0x26,0x8c,0x37,0x59,0xf8,0xae,0x8e,0xaa,
  11582. 0x4d,0xe4,0x1c,0xfe,0x51,0xc7,0xb7,0xcc
  11583. };
  11584. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64Cipher[] =
  11585. {
  11586. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  11587. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  11588. 0xd4,0x94,0x4c,0xcd,0x4d
  11589. };
  11590. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64CipherLong[] =
  11591. {
  11592. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  11593. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  11594. 0xd4,0x94,0x4c,0xcd,0x4d,0x47,0x5a,0x92,
  11595. 0x26,0x49,0x81,0x7a,0xda,0x36,0x27,0x01
  11596. };
  11597. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64_2CipherLong[] =
  11598. {
  11599. 0xf7,0x9c,0xbf,0xf6,0xa2,0xaa,0x8a,0x0a,
  11600. 0x63,0x8a,0x20,0x2f,0x12,0xf1,0x8e,0x49,
  11601. 0x30,0xc0,0x8d,0x5c,0x5f,0x8b,0xbc,0x16,
  11602. 0xdd,0x71,0xee,0x13,0x14,0x7b,0xe1,0x25,
  11603. 0xcb,0x87,0x8a,0xc6,0xdc,0x1d,0x54,0x7a,
  11604. 0xe1,0xe4,0x6f,0x0d,0x95,0x1b,0xd1,0x8b
  11605. };
  11606. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96Cipher[] =
  11607. {
  11608. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  11609. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  11610. 0xb8,0x69,0xa1,0x48,0x45
  11611. };
  11612. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96CipherLong[] =
  11613. {
  11614. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  11615. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  11616. 0xb8,0x69,0xa1,0x48,0x45,0xf1,0xc6,0x27,
  11617. 0x36,0xa8,0xb2,0x4b,0x0e,0x62,0x6b,0x72
  11618. };
  11619. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96_2CipherLong[] =
  11620. {
  11621. 0xd5,0x56,0x73,0xaa,0xb8,0xe4,0x06,0xf6,
  11622. 0x83,0x45,0x3a,0xb4,0xb9,0x63,0xec,0xad,
  11623. 0x73,0xc5,0xab,0x78,0xb1,0x21,0xab,0xef,
  11624. 0x69,0x15,0xb7,0x0c,0xe9,0xb4,0x3a,0xe7,
  11625. 0xbc,0xc4,0x22,0xbd,0x93,0xba,0x52,0xe0,
  11626. 0x91,0x2f,0x5e,0x8d,0x6d,0x59,0xf7,0xc2
  11627. };
  11628. #endif
  11629. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128Cipher[] =
  11630. {
  11631. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  11632. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  11633. 0x4b,0x45,0x7c,0xd6,0x8a
  11634. };
  11635. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128CipherLong[] =
  11636. {
  11637. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  11638. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  11639. 0x4b,0x45,0x7c,0xd6,0x8a,0xcc,0xda,0x4a,
  11640. 0x89,0xfa,0x23,0x6c,0x06,0xbf,0x26,0x05
  11641. };
  11642. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11643. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128_2CipherLong[] =
  11644. {
  11645. 0x24,0x5c,0x09,0xa0,0x3b,0x1a,0x5a,0x94,
  11646. 0x2b,0x93,0x56,0x13,0x48,0xa0,0x21,0xce,
  11647. 0x95,0x11,0xa3,0x76,0xd6,0x59,0x88,0x42,
  11648. 0x04,0x71,0x69,0x62,0x28,0xb2,0xee,0x9d,
  11649. 0xd5,0xa0,0xea,0xc7,0x37,0x93,0x92,0xc7,
  11650. 0xf2,0xb6,0x8d,0xd9,0x59,0x1a,0xfa,0xbb
  11651. };
  11652. #endif
  11653. #endif
  11654. int i;
  11655. struct {
  11656. const byte* key;
  11657. int keySz;
  11658. const byte* iv;
  11659. const byte* plain;
  11660. int len;
  11661. const byte* cipher;
  11662. } testVec[] = {
  11663. #ifdef WOLFSSL_AES_128
  11664. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  11665. ctrPlain, (int)sizeof(ctrPlain), ctr128Cipher },
  11666. /* let's try with just 9 bytes, non block size test */
  11667. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  11668. ctrPlain, (int)sizeof(oddCipher), ctr128Cipher },
  11669. /* and an additional 9 bytes to reuse tmp left buffer */
  11670. { NULL, 0, NULL, ctrPlain, (int)sizeof(oddCipher), oddCipher },
  11671. /* Counter wrapping */
  11672. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  11673. ctrPlain, (int)sizeof(ctr128Wrap128Cipher), ctr128Wrap128Cipher },
  11674. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  11675. ctrPlain, (int)sizeof(ctr128Wrap128CipherLong),
  11676. ctr128Wrap128CipherLong },
  11677. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11678. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128_2,
  11679. ctrPlain, (int)sizeof(ctr128Wrap128_2CipherLong),
  11680. ctr128Wrap128_2CipherLong },
  11681. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  11682. ctrPlain, (int)sizeof(ctr128Wrap96Cipher), ctr128Wrap96Cipher },
  11683. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  11684. ctrPlain, (int)sizeof(ctr128Wrap96CipherLong),
  11685. ctr128Wrap96CipherLong },
  11686. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96_2,
  11687. ctrPlain, (int)sizeof(ctr128Wrap96_2CipherLong),
  11688. ctr128Wrap96_2CipherLong },
  11689. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  11690. ctrPlain, (int)sizeof(ctr128Wrap64Cipher), ctr128Wrap64Cipher },
  11691. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  11692. ctrPlain, (int)sizeof(ctr128Wrap64CipherLong),
  11693. ctr128Wrap64CipherLong },
  11694. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64_2,
  11695. ctrPlain, (int)sizeof(ctr128Wrap64_2CipherLong),
  11696. ctr128Wrap64_2CipherLong },
  11697. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  11698. ctrPlain, (int)sizeof(ctr128Wrap32Cipher), ctr128Wrap32Cipher },
  11699. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  11700. ctrPlain, (int)sizeof(ctr128Wrap32CipherLong),
  11701. ctr128Wrap32CipherLong },
  11702. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32_2,
  11703. ctrPlain, (int)sizeof(ctr128Wrap32_2CipherLong),
  11704. ctr128Wrap32_2CipherLong },
  11705. #endif
  11706. #endif
  11707. #ifdef WOLFSSL_AES_192
  11708. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  11709. ctrPlain, (int)sizeof(ctrPlain), ctr192Cipher },
  11710. /* let's try with just 9 bytes, non block size test */
  11711. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  11712. ctrPlain, (int)sizeof(oddCipher), ctr192Cipher },
  11713. /* Counter wrapping */
  11714. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  11715. ctrPlain, (int)sizeof(ctr192Wrap128Cipher), ctr192Wrap128Cipher },
  11716. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  11717. ctrPlain, (int)sizeof(ctr192Wrap128CipherLong),
  11718. ctr192Wrap128CipherLong },
  11719. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11720. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128_2,
  11721. ctrPlain, (int)sizeof(ctr192Wrap128_2CipherLong),
  11722. ctr192Wrap128_2CipherLong },
  11723. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  11724. ctrPlain, (int)sizeof(ctr192Wrap96Cipher), ctr192Wrap96Cipher },
  11725. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  11726. ctrPlain, (int)sizeof(ctr192Wrap96CipherLong),
  11727. ctr192Wrap96CipherLong },
  11728. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96_2,
  11729. ctrPlain, (int)sizeof(ctr192Wrap96_2CipherLong),
  11730. ctr192Wrap96_2CipherLong },
  11731. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  11732. ctrPlain, (int)sizeof(ctr192Wrap64Cipher), ctr192Wrap64Cipher },
  11733. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  11734. ctrPlain, (int)sizeof(ctr192Wrap64CipherLong),
  11735. ctr192Wrap64CipherLong },
  11736. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64_2,
  11737. ctrPlain, (int)sizeof(ctr192Wrap64_2CipherLong),
  11738. ctr192Wrap64_2CipherLong },
  11739. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  11740. ctrPlain, (int)sizeof(ctr192Wrap32Cipher), ctr192Wrap32Cipher },
  11741. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  11742. ctrPlain, (int)sizeof(ctr192Wrap32CipherLong),
  11743. ctr192Wrap32CipherLong },
  11744. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32_2,
  11745. ctrPlain, (int)sizeof(ctr192Wrap32_2CipherLong),
  11746. ctr192Wrap32_2CipherLong },
  11747. #endif
  11748. #endif
  11749. #ifdef WOLFSSL_AES_256
  11750. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  11751. ctrPlain, (int)sizeof(ctrPlain), ctr256Cipher },
  11752. /* let's try with just 9 bytes, non block size test */
  11753. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  11754. ctrPlain, (int)sizeof(oddCipher), ctr256Cipher },
  11755. /* Counter wrapping */
  11756. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  11757. ctrPlain, (int)sizeof(ctr256Wrap128Cipher), ctr256Wrap128Cipher },
  11758. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  11759. ctrPlain, (int)sizeof(ctr256Wrap128CipherLong),
  11760. ctr256Wrap128CipherLong },
  11761. #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
  11762. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128_2,
  11763. ctrPlain, (int)sizeof(ctr256Wrap128_2CipherLong),
  11764. ctr256Wrap128_2CipherLong },
  11765. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  11766. ctrPlain, (int)sizeof(ctr256Wrap96Cipher), ctr256Wrap96Cipher },
  11767. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  11768. ctrPlain, (int)sizeof(ctr256Wrap96CipherLong),
  11769. ctr256Wrap96CipherLong },
  11770. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96_2,
  11771. ctrPlain, (int)sizeof(ctr256Wrap96_2CipherLong),
  11772. ctr256Wrap96_2CipherLong },
  11773. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  11774. ctrPlain, (int)sizeof(ctr256Wrap64Cipher), ctr256Wrap64Cipher },
  11775. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  11776. ctrPlain, (int)sizeof(ctr256Wrap64CipherLong),
  11777. ctr256Wrap64CipherLong },
  11778. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64_2,
  11779. ctrPlain, (int)sizeof(ctr256Wrap64_2CipherLong),
  11780. ctr256Wrap64_2CipherLong },
  11781. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  11782. ctrPlain, (int)sizeof(ctr256Wrap32Cipher), ctr256Wrap32Cipher },
  11783. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  11784. ctrPlain, (int)sizeof(ctr256Wrap32CipherLong),
  11785. ctr256Wrap32CipherLong },
  11786. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32_2,
  11787. ctrPlain, (int)sizeof(ctr256Wrap32_2CipherLong),
  11788. ctr256Wrap32_2CipherLong },
  11789. #endif
  11790. #endif
  11791. };
  11792. #define AES_CTR_TEST_LEN (int)(sizeof(testVec) / sizeof(*testVec))
  11793. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  11794. if (testVec[i].key != NULL) {
  11795. ret = wc_AesSetKeyDirect(enc, testVec[i].key, (word32)testVec[i].keySz,
  11796. testVec[i].iv, AES_ENCRYPTION);
  11797. if (ret != 0) {
  11798. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11799. }
  11800. /* Ctr only uses encrypt, even on key setup */
  11801. ret = wc_AesSetKeyDirect(dec, testVec[i].key, (word32)testVec[i].keySz,
  11802. testVec[i].iv, AES_ENCRYPTION);
  11803. if (ret != 0) {
  11804. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11805. }
  11806. }
  11807. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, (word32)testVec[i].len);
  11808. if (ret != 0) {
  11809. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11810. }
  11811. ret = wc_AesCtrEncrypt(dec, plain, cipher, (word32)testVec[i].len);
  11812. if (ret != 0) {
  11813. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11814. }
  11815. if (XMEMCMP(plain, ctrPlain, (size_t)testVec[i].len)) {
  11816. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11817. }
  11818. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  11819. if (XMEMCMP(cipher, testVec[i].cipher, (size_t)testVec[i].len)) {
  11820. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11821. }
  11822. #endif
  11823. }
  11824. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  11825. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  11826. if (testVec[i].key != NULL) {
  11827. ret = wc_AesSetKeyDirect(enc, testVec[i].key, testVec[i].keySz,
  11828. testVec[i].iv, AES_ENCRYPTION);
  11829. if (ret != 0) {
  11830. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11831. }
  11832. /* Ctr only uses encrypt, even on key setup */
  11833. ret = wc_AesSetKeyDirect(dec, testVec[i].key, testVec[i].keySz,
  11834. testVec[i].iv, AES_ENCRYPTION);
  11835. if (ret != 0) {
  11836. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11837. }
  11838. }
  11839. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11840. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, testVec[i].len);
  11841. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11842. if (ret != 0) {
  11843. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11844. }
  11845. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11846. ret = wc_AesCtrEncrypt(dec, plain, cipher, testVec[i].len);
  11847. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11848. if (ret != 0) {
  11849. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11850. }
  11851. if (XMEMCMP(plain, ctrPlain, testVec[i].len)) {
  11852. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11853. }
  11854. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  11855. if (XMEMCMP(cipher, testVec[i].cipher, testVec[i].len)) {
  11856. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11857. }
  11858. #endif
  11859. }
  11860. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  11861. if (testVec[i].key != NULL) {
  11862. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11863. ret = wc_AesSetKeyDirect(enc, testVec[i].key, testVec[i].keySz,
  11864. testVec[i].iv, AES_ENCRYPTION);
  11865. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11866. if (ret != 0) {
  11867. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11868. }
  11869. /* Ctr only uses encrypt, even on key setup */
  11870. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  11871. ret = wc_AesSetKeyDirect(dec, testVec[i].key, testVec[i].keySz,
  11872. testVec[i].iv, AES_ENCRYPTION);
  11873. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  11874. if (ret != 0) {
  11875. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11876. }
  11877. }
  11878. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, testVec[i].len);
  11879. if (ret != 0) {
  11880. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11881. }
  11882. ret = wc_AesCtrEncrypt(dec, plain, cipher, testVec[i].len);
  11883. if (ret != 0) {
  11884. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11885. }
  11886. if (XMEMCMP(plain, ctrPlain, testVec[i].len)) {
  11887. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11888. }
  11889. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  11890. if (XMEMCMP(cipher, testVec[i].cipher, testVec[i].len)) {
  11891. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11892. }
  11893. #endif
  11894. }
  11895. #endif /* DEBUG_VECTOR_REGISTER_ACCESS && WC_C_DYNAMIC_FALLBACK */
  11896. out:
  11897. return ret;
  11898. }
  11899. #endif /* WOLFSSL_AES_COUNTER */
  11900. #ifdef HAVE_AES_ECB
  11901. static wc_test_ret_t aes_ecb_test(Aes* enc, Aes* dec, byte* cipher, byte* plain)
  11902. {
  11903. wc_test_ret_t ret = 0;
  11904. WOLFSSL_SMALL_STACK_STATIC const byte key_128[] = "0123456789abcdef ";
  11905. WOLFSSL_SMALL_STACK_STATIC const byte key_192[] =
  11906. "0123456789abcdef01234567 ";
  11907. WOLFSSL_SMALL_STACK_STATIC const byte key_256[] =
  11908. "0123456789abcdef0123456789abcdef ";
  11909. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = "1234567890abcdef ";
  11910. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  11911. 0x6e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
  11912. 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20
  11913. };
  11914. WOLFSSL_SMALL_STACK_STATIC const byte verify_ecb_128[AES_BLOCK_SIZE] = {
  11915. 0xd0, 0xc9, 0xd9, 0xc9, 0x40, 0xe8, 0x97, 0xb6,
  11916. 0xc8, 0x8c, 0x33, 0x3b, 0xb5, 0x8f, 0x85, 0xd1
  11917. };
  11918. WOLFSSL_SMALL_STACK_STATIC const byte verify_ecb_192[AES_BLOCK_SIZE] = {
  11919. 0x06, 0x57, 0xee, 0x78, 0x3f, 0x96, 0x00, 0xb1,
  11920. 0xec, 0x76, 0x94, 0x30, 0x29, 0xbe, 0x15, 0xab
  11921. };
  11922. WOLFSSL_SMALL_STACK_STATIC const byte verify_ecb_256[AES_BLOCK_SIZE] = {
  11923. 0xcd, 0xf2, 0x81, 0x3e, 0x73, 0x3e, 0xf7, 0x33,
  11924. 0x3d, 0x18, 0xfd, 0x41, 0x85, 0x37, 0x04, 0x82
  11925. };
  11926. int i;
  11927. struct {
  11928. const byte* key;
  11929. int keySz;
  11930. const byte* iv;
  11931. const byte* verify;
  11932. } testVec[] = {
  11933. { key_128, 16, iv, verify_ecb_128 },
  11934. { key_192, 24, iv, verify_ecb_192 },
  11935. { key_256, 32, iv, verify_ecb_256 },
  11936. };
  11937. #define AES_ECB_TEST_LEN (int)(sizeof(testVec) / sizeof(*testVec))
  11938. for (i = 0; i < AES_ECB_TEST_LEN; i++) {
  11939. ret = wc_AesSetKey(enc, testVec[i].key, testVec[i].keySz, testVec[i].iv,
  11940. AES_ENCRYPTION);
  11941. if (ret != 0)
  11942. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11943. #ifdef HAVE_AES_DECRYPT
  11944. ret = wc_AesSetKey(dec, testVec[i].key, testVec[i].keySz, testVec[i].iv,
  11945. AES_DECRYPTION);
  11946. if (ret != 0)
  11947. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11948. #endif
  11949. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  11950. ret = wc_AesEcbEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  11951. #if defined(WOLFSSL_ASYNC_CRYPT)
  11952. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  11953. #endif
  11954. if (ret != 0)
  11955. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11956. if (XMEMCMP(cipher, testVec[i].verify, AES_BLOCK_SIZE)) {
  11957. WOLFSSL_MSG("aes_test cipher vs verify_ecb mismatch!");
  11958. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11959. }
  11960. #ifdef HAVE_AES_DECRYPT
  11961. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  11962. ret = wc_AesEcbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  11963. #if defined(WOLFSSL_ASYNC_CRYPT)
  11964. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  11965. #endif
  11966. if (ret != 0)
  11967. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11968. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  11969. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  11970. #endif /* HAVE_AES_DECRYPT */
  11971. (void)dec;
  11972. (void)plain;
  11973. }
  11974. out:
  11975. return ret;
  11976. }
  11977. #endif
  11978. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_test(void)
  11979. {
  11980. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  11981. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11982. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  11983. #else
  11984. Aes enc[1];
  11985. #endif
  11986. int enc_inited = 0;
  11987. byte cipher[AES_BLOCK_SIZE * 4];
  11988. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  11989. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11990. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  11991. #else
  11992. Aes dec[1];
  11993. #endif
  11994. int dec_inited = 0;
  11995. byte plain [AES_BLOCK_SIZE * 4];
  11996. #endif /* HAVE_AES_DECRYPT || WOLFSSL_AES_COUNTER */
  11997. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  11998. wc_test_ret_t ret = 0;
  11999. #ifdef HAVE_AES_CBC
  12000. #ifdef WOLFSSL_AES_128
  12001. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  12002. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  12003. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  12004. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  12005. };
  12006. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  12007. {
  12008. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  12009. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  12010. };
  12011. #ifdef HAVE_RENESAS_SYNC
  12012. const byte *key =
  12013. (byte*)guser_PKCbInfo.wrapped_key_aes128;
  12014. #else
  12015. WOLFSSL_SMALL_STACK_STATIC const
  12016. byte key[] = "0123456789abcdef "; /* align */
  12017. #endif
  12018. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = "1234567890abcdef "; /* align */
  12019. WOLFSSL_ENTER("aes_test");
  12020. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12021. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || \
  12022. defined(WOLFSSL_AES_DIRECT)
  12023. if (enc == NULL)
  12024. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12025. #endif
  12026. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  12027. if (dec == NULL)
  12028. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12029. #endif
  12030. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  12031. ret = wc_AesInit(enc, HEAP_HINT, devId);
  12032. if (ret != 0)
  12033. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12034. enc_inited = 1;
  12035. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  12036. ret = wc_AesInit(dec, HEAP_HINT, devId);
  12037. if (ret != 0)
  12038. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12039. dec_inited = 1;
  12040. #endif
  12041. #ifdef HAVE_AES_ECB
  12042. ret = aes_ecb_test(enc, dec, cipher, plain);
  12043. if (ret != 0)
  12044. return ret;
  12045. #endif
  12046. ret = wc_AesSetKey(enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  12047. if (ret != 0)
  12048. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12049. #ifdef HAVE_AES_DECRYPT
  12050. ret = wc_AesSetKey(dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  12051. if (ret != 0)
  12052. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12053. #endif
  12054. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  12055. ret = wc_AesCbcEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  12056. #if defined(WOLFSSL_ASYNC_CRYPT)
  12057. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12058. #endif
  12059. if (ret != 0)
  12060. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12061. #ifdef HAVE_AES_DECRYPT
  12062. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  12063. ret = wc_AesCbcDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  12064. #if defined(WOLFSSL_ASYNC_CRYPT)
  12065. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12066. #endif
  12067. if (ret != 0) {
  12068. WOLFSSL_MSG("failed wc_AesCbcDecrypt");
  12069. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12070. }
  12071. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE)) {
  12072. WOLFSSL_MSG("wc_AesCbcDecrypt failed plain compare");
  12073. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12074. }
  12075. #endif /* HAVE_AES_DECRYPT */
  12076. /* skipped because wrapped key use in case of renesas sm */
  12077. #ifndef HAVE_RENESAS_SYNC
  12078. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE)) {
  12079. WOLFSSL_MSG("wc_AesCbcDecrypt failed cipher-verify compare");
  12080. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12081. }
  12082. #endif
  12083. #endif /* WOLFSSL_AES_128 */
  12084. #if defined(WOLFSSL_AESNI) && defined(HAVE_AES_DECRYPT)
  12085. {
  12086. WOLFSSL_SMALL_STACK_STATIC const byte bigMsg[] = {
  12087. /* "All work and no play makes Jack a dull boy. " */
  12088. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  12089. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  12090. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  12091. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  12092. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  12093. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  12094. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  12095. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  12096. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  12097. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  12098. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  12099. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  12100. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  12101. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  12102. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  12103. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  12104. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  12105. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  12106. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  12107. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  12108. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  12109. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  12110. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  12111. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  12112. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  12113. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  12114. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  12115. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  12116. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  12117. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  12118. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  12119. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  12120. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  12121. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  12122. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  12123. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  12124. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  12125. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  12126. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  12127. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  12128. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  12129. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  12130. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  12131. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  12132. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  12133. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  12134. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  12135. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20
  12136. };
  12137. WOLFSSL_SMALL_STACK_STATIC const byte bigKey[] = "0123456789abcdeffedcba9876543210";
  12138. word32 keySz, msgSz;
  12139. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12140. byte *bigCipher = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12141. byte *bigPlain = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12142. if ((bigCipher == NULL) ||
  12143. (bigPlain == NULL)) {
  12144. if (bigCipher != NULL)
  12145. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12146. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12147. }
  12148. #else
  12149. byte bigCipher[sizeof(bigMsg)];
  12150. byte bigPlain[sizeof(bigMsg)];
  12151. #endif
  12152. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  12153. * message by AES_BLOCK_SIZE for each size of AES key. */
  12154. for (keySz = 16; keySz <= 32; keySz += 8) {
  12155. for (msgSz = AES_BLOCK_SIZE;
  12156. msgSz <= sizeof(bigMsg);
  12157. msgSz += AES_BLOCK_SIZE) {
  12158. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  12159. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  12160. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  12161. if (ret != 0) {
  12162. ret = WC_TEST_RET_ENC_EC(ret);
  12163. break;
  12164. }
  12165. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  12166. if (ret != 0) {
  12167. ret = WC_TEST_RET_ENC_EC(ret);
  12168. break;
  12169. }
  12170. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  12171. #if defined(WOLFSSL_ASYNC_CRYPT)
  12172. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12173. #endif
  12174. if (ret != 0) {
  12175. ret = WC_TEST_RET_ENC_EC(ret);
  12176. break;
  12177. }
  12178. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  12179. #if defined(WOLFSSL_ASYNC_CRYPT)
  12180. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12181. #endif
  12182. if (ret != 0) {
  12183. ret = WC_TEST_RET_ENC_EC(ret);
  12184. break;
  12185. }
  12186. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  12187. ret = WC_TEST_RET_ENC_NC;
  12188. break;
  12189. }
  12190. }
  12191. if (ret != 0)
  12192. break;
  12193. }
  12194. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  12195. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  12196. * message by AES_BLOCK_SIZE for each size of AES key. */
  12197. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12198. for (keySz = 16; keySz <= 32; keySz += 8) {
  12199. for (msgSz = AES_BLOCK_SIZE;
  12200. msgSz <= sizeof(bigMsg);
  12201. msgSz += AES_BLOCK_SIZE) {
  12202. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  12203. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  12204. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  12205. if (ret != 0) {
  12206. ret = WC_TEST_RET_ENC_EC(ret);
  12207. break;
  12208. }
  12209. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  12210. if (ret != 0) {
  12211. ret = WC_TEST_RET_ENC_EC(ret);
  12212. break;
  12213. }
  12214. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  12215. #if defined(WOLFSSL_ASYNC_CRYPT)
  12216. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12217. #endif
  12218. if (ret != 0) {
  12219. ret = WC_TEST_RET_ENC_EC(ret);
  12220. break;
  12221. }
  12222. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  12223. #if defined(WOLFSSL_ASYNC_CRYPT)
  12224. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12225. #endif
  12226. if (ret != 0) {
  12227. ret = WC_TEST_RET_ENC_EC(ret);
  12228. break;
  12229. }
  12230. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  12231. ret = WC_TEST_RET_ENC_NC;
  12232. break;
  12233. }
  12234. }
  12235. if (ret != 0)
  12236. break;
  12237. }
  12238. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12239. #endif /* DEBUG_VECTOR_REGISTER_ACCESS && WC_C_DYNAMIC_FALLBACK */
  12240. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12241. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12242. XFREE(bigPlain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12243. #endif
  12244. if (ret != 0)
  12245. goto out;
  12246. }
  12247. #endif /* WOLFSSL_AESNI && HAVE_AES_DECRYPT */
  12248. /* Test of AES IV state with encrypt/decrypt */
  12249. #if defined(WOLFSSL_AES_128) && !defined(HAVE_RENESAS_SYNC)
  12250. {
  12251. /* Test Vector from "NIST Special Publication 800-38A, 2001 Edition"
  12252. * https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38a.pdf
  12253. */
  12254. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  12255. {
  12256. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  12257. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  12258. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  12259. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
  12260. };
  12261. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] =
  12262. {
  12263. 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
  12264. 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  12265. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
  12266. 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2
  12267. };
  12268. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  12269. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  12270. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  12271. };
  12272. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  12273. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12274. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  12275. };
  12276. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  12277. if (ret != 0)
  12278. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12279. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 2);
  12280. ret = wc_AesCbcEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE);
  12281. #if defined(WOLFSSL_ASYNC_CRYPT)
  12282. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12283. #endif
  12284. if (ret != 0)
  12285. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12286. #ifndef HAVE_RENESAS_SYNC
  12287. if (XMEMCMP(cipher, verify2, AES_BLOCK_SIZE))
  12288. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12289. #endif
  12290. ret = wc_AesCbcEncrypt(enc, cipher + AES_BLOCK_SIZE,
  12291. msg2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  12292. #if defined(WOLFSSL_ASYNC_CRYPT)
  12293. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12294. #endif
  12295. if (ret != 0)
  12296. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12297. if (XMEMCMP(cipher + AES_BLOCK_SIZE, verify2 + AES_BLOCK_SIZE,
  12298. AES_BLOCK_SIZE))
  12299. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12300. #if defined(HAVE_AES_DECRYPT)
  12301. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_DECRYPTION);
  12302. if (ret != 0)
  12303. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12304. XMEMSET(plain, 0, AES_BLOCK_SIZE * 2);
  12305. ret = wc_AesCbcDecrypt(dec, plain, verify2, AES_BLOCK_SIZE);
  12306. #if defined(WOLFSSL_ASYNC_CRYPT)
  12307. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12308. #endif
  12309. if (ret != 0)
  12310. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12311. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE))
  12312. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12313. ret = wc_AesCbcDecrypt(dec, plain + AES_BLOCK_SIZE,
  12314. verify2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  12315. #if defined(WOLFSSL_ASYNC_CRYPT)
  12316. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12317. #endif
  12318. if (ret != 0)
  12319. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12320. if (XMEMCMP(plain + AES_BLOCK_SIZE, msg2 + AES_BLOCK_SIZE,
  12321. AES_BLOCK_SIZE)) {
  12322. WOLFSSL_MSG("wc_AesCbcDecrypt failed plain-msg2 compare");
  12323. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12324. }
  12325. #endif /* HAVE_AES_DECRYPT */
  12326. }
  12327. #endif /* WOLFSSL_AES_128 && !HAVE_RENESAS_SYNC */
  12328. #endif /* HAVE_AES_CBC */
  12329. #ifdef WOLFSSL_AES_COUNTER
  12330. ret = aesctr_test(enc, dec, cipher, plain);
  12331. if (ret != 0)
  12332. return ret;
  12333. #endif
  12334. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  12335. {
  12336. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  12337. {
  12338. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  12339. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  12340. };
  12341. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  12342. {
  12343. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  12344. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  12345. };
  12346. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  12347. {
  12348. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  12349. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  12350. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  12351. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  12352. };
  12353. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  12354. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  12355. if (ret != 0)
  12356. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12357. #if !defined(HAVE_SELFTEST) && \
  12358. (defined(WOLFSSL_LINUXKM) || \
  12359. !defined(HAVE_FIPS) || \
  12360. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  12361. ret = wc_AesEncryptDirect(enc, cipher, niPlain);
  12362. if (ret != 0)
  12363. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12364. #else
  12365. wc_AesEncryptDirect(enc, cipher, niPlain);
  12366. #endif
  12367. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  12368. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12369. #ifdef HAVE_AES_DECRYPT
  12370. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  12371. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  12372. if (ret != 0)
  12373. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12374. #if !defined(HAVE_SELFTEST) && \
  12375. (defined(WOLFSSL_LINUXKM) || \
  12376. !defined(HAVE_FIPS) || \
  12377. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  12378. ret = wc_AesDecryptDirect(dec, plain, niCipher);
  12379. if (ret != 0)
  12380. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12381. #else
  12382. wc_AesDecryptDirect(dec, plain, niCipher);
  12383. #endif
  12384. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  12385. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12386. #endif /* HAVE_AES_DECRYPT */
  12387. }
  12388. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  12389. #ifndef HAVE_RENESAS_SYNC
  12390. ret = aes_key_size_test();
  12391. if (ret != 0)
  12392. goto out;
  12393. #endif
  12394. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) && \
  12395. !defined(HAVE_RENESAS_SYNC)
  12396. ret = aes_cbc_test();
  12397. if (ret != 0)
  12398. goto out;
  12399. #endif
  12400. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12401. ret = aesecb_test();
  12402. if (ret != 0)
  12403. goto out;
  12404. #endif
  12405. out:
  12406. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  12407. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12408. if (enc) {
  12409. if (enc_inited)
  12410. wc_AesFree(enc);
  12411. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  12412. }
  12413. #else
  12414. if (enc_inited)
  12415. wc_AesFree(enc);
  12416. #endif
  12417. (void)cipher;
  12418. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  12419. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12420. if (dec) {
  12421. if (dec_inited)
  12422. wc_AesFree(dec);
  12423. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  12424. }
  12425. #else
  12426. if (dec_inited)
  12427. wc_AesFree(dec);
  12428. #endif
  12429. (void)plain;
  12430. #endif /* HAVE_AES_DECRYPT || WOLFSSL_AES_COUNTER */
  12431. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  12432. return ret;
  12433. }
  12434. #if defined(WOLFSSL_AES_CFB)
  12435. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_cfb_test(void)
  12436. {
  12437. int ret;
  12438. WOLFSSL_ENTER("aes_cfb_test");
  12439. ret = aescfb_test_0();
  12440. if (ret != 0)
  12441. return ret;
  12442. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  12443. ret = aescfb1_test();
  12444. if (ret != 0)
  12445. return ret;
  12446. ret = aescfb8_test();
  12447. if (ret != 0)
  12448. return ret;
  12449. #endif
  12450. return 0;
  12451. }
  12452. #endif
  12453. #if defined(WOLFSSL_AES_XTS)
  12454. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_xts_test(void)
  12455. {
  12456. int ret = 0;
  12457. WOLFSSL_ENTER("aes_xts_test");
  12458. #ifdef WOLFSSL_AES_128
  12459. ret = aes_xts_128_test();
  12460. if (ret != 0)
  12461. return ret;
  12462. #endif
  12463. #ifdef WOLFSSL_AES_192
  12464. ret = aes_xts_192_test();
  12465. if (ret != 0)
  12466. return ret;
  12467. #endif
  12468. #ifdef WOLFSSL_AES_256
  12469. ret = aes_xts_256_test();
  12470. if (ret != 0)
  12471. return ret;
  12472. #endif
  12473. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  12474. ret = aes_xts_sector_test();
  12475. if (ret != 0)
  12476. return ret;
  12477. #endif
  12478. #ifdef WOLFSSL_AES_128
  12479. ret = aes_xts_args_test();
  12480. if (ret != 0)
  12481. return ret;
  12482. #endif
  12483. return 0;
  12484. }
  12485. #endif
  12486. #ifdef WOLFSSL_AES_192
  12487. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes192_test(void)
  12488. {
  12489. wc_test_ret_t ret = 0;
  12490. #ifdef HAVE_AES_CBC
  12491. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12492. Aes *enc = NULL;
  12493. #else
  12494. Aes enc[1];
  12495. #endif
  12496. int enc_inited = 0;
  12497. byte cipher[AES_BLOCK_SIZE];
  12498. #ifdef HAVE_AES_DECRYPT
  12499. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12500. Aes *dec = NULL;
  12501. #else
  12502. Aes dec[1];
  12503. #endif
  12504. byte plain[AES_BLOCK_SIZE];
  12505. #endif
  12506. #ifdef HAVE_AES_DECRYPT
  12507. int dec_inited = 0;
  12508. #endif
  12509. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  12510. * Appendix F.2.3 */
  12511. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  12512. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  12513. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  12514. };
  12515. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  12516. {
  12517. 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  12518. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8
  12519. };
  12520. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  12521. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  12522. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  12523. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  12524. };
  12525. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  12526. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  12527. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  12528. };
  12529. WOLFSSL_ENTER("aes192_test");
  12530. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12531. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12532. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12533. #ifdef HAVE_AES_DECRYPT
  12534. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12535. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12536. #endif
  12537. #endif
  12538. ret = wc_AesInit(enc, HEAP_HINT, devId);
  12539. if (ret != 0)
  12540. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12541. enc_inited = 1;
  12542. #ifdef HAVE_AES_DECRYPT
  12543. ret = wc_AesInit(dec, HEAP_HINT, devId);
  12544. if (ret != 0)
  12545. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12546. dec_inited = 1;
  12547. #endif
  12548. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  12549. if (ret != 0)
  12550. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12551. #ifdef HAVE_AES_DECRYPT
  12552. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  12553. if (ret != 0)
  12554. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12555. #endif
  12556. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  12557. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  12558. #if defined(WOLFSSL_ASYNC_CRYPT)
  12559. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12560. #endif
  12561. if (ret != 0)
  12562. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12563. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  12564. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12565. #ifdef HAVE_AES_DECRYPT
  12566. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  12567. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  12568. #if defined(WOLFSSL_ASYNC_CRYPT)
  12569. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12570. #endif
  12571. if (ret != 0)
  12572. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12573. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  12574. WOLFSSL_MSG("failed wc_AesCbcDecrypt plain-msg compare");
  12575. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12576. }
  12577. #endif
  12578. out:
  12579. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12580. if (enc) {
  12581. if (enc_inited)
  12582. wc_AesFree(enc);
  12583. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  12584. }
  12585. #ifdef HAVE_AES_DECRYPT
  12586. if (dec) {
  12587. if (dec_inited)
  12588. wc_AesFree(dec);
  12589. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  12590. }
  12591. #endif
  12592. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  12593. if (enc_inited)
  12594. wc_AesFree(enc);
  12595. #ifdef HAVE_AES_DECRYPT
  12596. if (dec_inited)
  12597. wc_AesFree(dec);
  12598. #endif
  12599. #endif
  12600. #endif /* HAVE_AES_CBC */
  12601. return ret;
  12602. }
  12603. #endif /* WOLFSSL_AES_192 */
  12604. #ifdef WOLFSSL_AES_256
  12605. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes256_test(void)
  12606. {
  12607. #ifdef HAVE_AES_CBC
  12608. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12609. Aes *enc = NULL;
  12610. #else
  12611. Aes enc[1];
  12612. #endif
  12613. int enc_inited = 0;
  12614. byte cipher[AES_BLOCK_SIZE];
  12615. #ifdef HAVE_AES_DECRYPT
  12616. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12617. Aes *dec = NULL;
  12618. #else
  12619. Aes dec[1];
  12620. #endif
  12621. int dec_inited = 0;
  12622. byte plain[AES_BLOCK_SIZE];
  12623. #endif
  12624. #endif /* HAVE_AES_CBC */
  12625. wc_test_ret_t ret = 0;
  12626. #ifdef HAVE_AES_CBC
  12627. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  12628. * Appendix F.2.5 */
  12629. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  12630. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  12631. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  12632. };
  12633. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  12634. {
  12635. 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  12636. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6
  12637. };
  12638. #ifdef HAVE_RENESAS_SYNC
  12639. byte *key =
  12640. (byte*)guser_PKCbInfo.wrapped_key_aes256;
  12641. int keySz = (256/8);
  12642. #else
  12643. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  12644. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  12645. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  12646. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  12647. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  12648. };
  12649. int keySz = (int)sizeof(key);
  12650. #endif
  12651. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  12652. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  12653. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  12654. };
  12655. WOLFSSL_ENTER("aes256_test");
  12656. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12657. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12658. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12659. #ifdef HAVE_AES_DECRYPT
  12660. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12661. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12662. #endif
  12663. #endif
  12664. ret = wc_AesInit(enc, HEAP_HINT, devId);
  12665. if (ret != 0)
  12666. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12667. enc_inited = 1;
  12668. #ifdef HAVE_AES_DECRYPT
  12669. ret = wc_AesInit(dec, HEAP_HINT, devId);
  12670. if (ret != 0)
  12671. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12672. dec_inited = 1;
  12673. #endif
  12674. ret = wc_AesSetKey(enc, key, (word32)keySz, iv, AES_ENCRYPTION);
  12675. if (ret != 0)
  12676. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12677. #ifdef HAVE_AES_DECRYPT
  12678. ret = wc_AesSetKey(dec, key, (word32)keySz, iv, AES_DECRYPTION);
  12679. if (ret != 0)
  12680. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12681. #endif
  12682. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  12683. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  12684. #if defined(WOLFSSL_ASYNC_CRYPT)
  12685. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12686. #endif
  12687. if (ret != 0)
  12688. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12689. #ifdef HAVE_AES_DECRYPT
  12690. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  12691. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  12692. #if defined(WOLFSSL_ASYNC_CRYPT)
  12693. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12694. #endif
  12695. if (ret != 0)
  12696. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12697. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  12698. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12699. }
  12700. #endif
  12701. #ifndef HAVE_RENESAS_SYNC
  12702. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  12703. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12704. #endif
  12705. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  12706. ret = wc_AesSetKey(enc, key, keySz, iv, AES_ENCRYPTION);
  12707. if (ret != 0)
  12708. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12709. #ifdef HAVE_AES_DECRYPT
  12710. ret = wc_AesSetKey(dec, key, keySz, iv, AES_DECRYPTION);
  12711. if (ret != 0)
  12712. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12713. #endif
  12714. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  12715. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12716. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  12717. #if defined(WOLFSSL_ASYNC_CRYPT)
  12718. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12719. #endif
  12720. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12721. if (ret != 0)
  12722. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12723. #ifdef HAVE_AES_DECRYPT
  12724. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  12725. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12726. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  12727. #if defined(WOLFSSL_ASYNC_CRYPT)
  12728. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12729. #endif
  12730. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12731. if (ret != 0)
  12732. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12733. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  12734. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12735. }
  12736. #endif
  12737. #ifndef HAVE_RENESAS_SYNC
  12738. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  12739. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12740. #endif
  12741. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12742. ret = wc_AesSetKey(enc, key, keySz, iv, AES_ENCRYPTION);
  12743. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12744. if (ret != 0)
  12745. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12746. #ifdef HAVE_AES_DECRYPT
  12747. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12748. ret = wc_AesSetKey(dec, key, keySz, iv, AES_DECRYPTION);
  12749. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12750. if (ret != 0)
  12751. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12752. #endif
  12753. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  12754. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  12755. #if defined(WOLFSSL_ASYNC_CRYPT)
  12756. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12757. #endif
  12758. if (ret != 0)
  12759. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12760. #ifdef HAVE_AES_DECRYPT
  12761. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  12762. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  12763. #if defined(WOLFSSL_ASYNC_CRYPT)
  12764. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12765. #endif
  12766. if (ret != 0)
  12767. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12768. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  12769. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12770. }
  12771. #endif
  12772. #ifndef HAVE_RENESAS_SYNC
  12773. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  12774. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12775. #endif
  12776. #endif /* DEBUG_VECTOR_REGISTER_ACCESS && WC_C_DYNAMIC_FALLBACK */
  12777. out:
  12778. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12779. if (enc) {
  12780. if (enc_inited)
  12781. wc_AesFree(enc);
  12782. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  12783. }
  12784. #ifdef HAVE_AES_DECRYPT
  12785. if (dec) {
  12786. if (dec_inited)
  12787. wc_AesFree(dec);
  12788. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  12789. }
  12790. #endif
  12791. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  12792. if (enc_inited)
  12793. wc_AesFree(enc);
  12794. #ifdef HAVE_AES_DECRYPT
  12795. if (dec_inited)
  12796. wc_AesFree(dec);
  12797. #endif /* HAVE_AES_DECRYPT */
  12798. #endif /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  12799. #endif /* HAVE_AES_CBC */
  12800. return ret;
  12801. }
  12802. #endif /* WOLFSSL_AES_256 */
  12803. #ifdef HAVE_AESGCM
  12804. #ifdef WOLFSSL_AES_128
  12805. static wc_test_ret_t aesgcm_default_test_helper(byte* key, int keySz, byte* iv, int ivSz,
  12806. byte* plain, int plainSz, byte* cipher, int cipherSz,
  12807. byte* aad, int aadSz, byte* tag, int tagSz)
  12808. {
  12809. wc_test_ret_t ret;
  12810. int enc_inited = 0, dec_inited = 0;
  12811. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12812. Aes *enc = NULL;
  12813. Aes *dec = NULL;
  12814. #else
  12815. Aes enc[1];
  12816. Aes dec[1];
  12817. #endif
  12818. byte resultT[AES_BLOCK_SIZE];
  12819. byte resultP[AES_BLOCK_SIZE * 3];
  12820. byte resultC[AES_BLOCK_SIZE * 3];
  12821. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12822. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12823. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12824. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  12825. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  12826. #endif
  12827. XMEMSET(resultT, 0, sizeof(resultT));
  12828. XMEMSET(resultC, 0, sizeof(resultC));
  12829. XMEMSET(resultP, 0, sizeof(resultP));
  12830. ret = wc_AesInit(enc, HEAP_HINT, devId);
  12831. if (ret != 0)
  12832. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12833. else
  12834. enc_inited = 1;
  12835. ret = wc_AesInit(dec, HEAP_HINT, devId);
  12836. if (ret != 0)
  12837. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12838. else
  12839. dec_inited = 1;
  12840. ret = wc_AesGcmSetKey(enc, key, (word32)keySz);
  12841. if (ret != 0)
  12842. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12843. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  12844. ret = wc_AesGcmEncrypt(enc, resultC, plain, (word32)plainSz, iv, ivSz,
  12845. resultT, (word32)tagSz, aad, aadSz);
  12846. #if defined(WOLFSSL_ASYNC_CRYPT)
  12847. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12848. #endif
  12849. if (ret != 0)
  12850. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12851. if (cipher != NULL) {
  12852. if (XMEMCMP(cipher, resultC, cipherSz))
  12853. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12854. }
  12855. if (XMEMCMP(tag, resultT, tagSz))
  12856. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12857. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  12858. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12859. ret = wc_AesGcmEncrypt(enc, resultC, plain, plainSz, iv, ivSz,
  12860. resultT, tagSz, aad, aadSz);
  12861. #if defined(WOLFSSL_ASYNC_CRYPT)
  12862. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  12863. #endif
  12864. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12865. if (ret != 0)
  12866. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12867. if (cipher != NULL) {
  12868. if (XMEMCMP(cipher, resultC, cipherSz))
  12869. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12870. }
  12871. if (XMEMCMP(tag, resultT, tagSz))
  12872. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12873. #endif
  12874. #ifdef HAVE_AES_DECRYPT
  12875. ret = wc_AesGcmSetKey(dec, key, (word32)keySz);
  12876. if (ret != 0)
  12877. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12878. ret = wc_AesGcmDecrypt(dec, resultP, resultC, (word32)cipherSz,
  12879. iv, (word32)ivSz, resultT, tagSz, aad, aadSz);
  12880. #if defined(WOLFSSL_ASYNC_CRYPT)
  12881. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12882. #endif
  12883. if (ret != 0)
  12884. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12885. if (plain != NULL) {
  12886. if (XMEMCMP(plain, resultP, plainSz))
  12887. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12888. }
  12889. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  12890. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  12891. ret = wc_AesGcmDecrypt(dec, resultP, resultC, cipherSz,
  12892. iv, ivSz, resultT, tagSz, aad, aadSz);
  12893. #if defined(WOLFSSL_ASYNC_CRYPT)
  12894. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  12895. #endif
  12896. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  12897. if (ret != 0)
  12898. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12899. if (plain != NULL) {
  12900. if (XMEMCMP(plain, resultP, plainSz))
  12901. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12902. }
  12903. #endif
  12904. #endif /* HAVE_AES_DECRYPT */
  12905. ret = 0;
  12906. out:
  12907. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12908. if (enc) {
  12909. if (enc_inited)
  12910. wc_AesFree(enc);
  12911. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  12912. }
  12913. if (dec) {
  12914. if (dec_inited)
  12915. wc_AesFree(dec);
  12916. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  12917. }
  12918. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  12919. if (enc_inited)
  12920. wc_AesFree(enc);
  12921. if (dec_inited)
  12922. wc_AesFree(dec);
  12923. #endif
  12924. return ret;
  12925. }
  12926. #endif
  12927. /* tests that only use 12 byte IV and 16 or less byte AAD
  12928. * test vectors are from NIST SP 800-38D
  12929. * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/CAVP-TESTING-BLOCK-CIPHER-MODES*/
  12930. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_default_test(void)
  12931. {
  12932. #ifdef WOLFSSL_AES_128
  12933. byte key1[] = {
  12934. 0x29, 0x8e, 0xfa, 0x1c, 0xcf, 0x29, 0xcf, 0x62,
  12935. 0xae, 0x68, 0x24, 0xbf, 0xc1, 0x95, 0x57, 0xfc
  12936. };
  12937. byte iv1[] = {
  12938. 0x6f, 0x58, 0xa9, 0x3f, 0xe1, 0xd2, 0x07, 0xfa,
  12939. 0xe4, 0xed, 0x2f, 0x6d
  12940. };
  12941. ALIGN64 byte plain1[] = {
  12942. 0xcc, 0x38, 0xbc, 0xcd, 0x6b, 0xc5, 0x36, 0xad,
  12943. 0x91, 0x9b, 0x13, 0x95, 0xf5, 0xd6, 0x38, 0x01,
  12944. 0xf9, 0x9f, 0x80, 0x68, 0xd6, 0x5c, 0xa5, 0xac,
  12945. 0x63, 0x87, 0x2d, 0xaf, 0x16, 0xb9, 0x39, 0x01
  12946. };
  12947. byte aad1[] = {
  12948. 0x02, 0x1f, 0xaf, 0xd2, 0x38, 0x46, 0x39, 0x73,
  12949. 0xff, 0xe8, 0x02, 0x56, 0xe5, 0xb1, 0xc6, 0xb1
  12950. };
  12951. ALIGN64 byte cipher1[] = {
  12952. 0xdf, 0xce, 0x4e, 0x9c, 0xd2, 0x91, 0x10, 0x3d,
  12953. 0x7f, 0xe4, 0xe6, 0x33, 0x51, 0xd9, 0xe7, 0x9d,
  12954. 0x3d, 0xfd, 0x39, 0x1e, 0x32, 0x67, 0x10, 0x46,
  12955. 0x58, 0x21, 0x2d, 0xa9, 0x65, 0x21, 0xb7, 0xdb
  12956. };
  12957. byte tag1[] = {
  12958. 0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
  12959. 0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
  12960. };
  12961. byte key2[] = {
  12962. 0x01, 0x6d, 0xbb, 0x38, 0xda, 0xa7, 0x6d, 0xfe,
  12963. 0x7d, 0xa3, 0x84, 0xeb, 0xf1, 0x24, 0x03, 0x64
  12964. };
  12965. byte iv2[] = {
  12966. 0x07, 0x93, 0xef, 0x3a, 0xda, 0x78, 0x2f, 0x78,
  12967. 0xc9, 0x8a, 0xff, 0xe3
  12968. };
  12969. ALIGN64 byte plain2[] = {
  12970. 0x4b, 0x34, 0xa9, 0xec, 0x57, 0x63, 0x52, 0x4b,
  12971. 0x19, 0x1d, 0x56, 0x16, 0xc5, 0x47, 0xf6, 0xb7
  12972. };
  12973. ALIGN64 byte cipher2[] = {
  12974. 0x60, 0x9a, 0xa3, 0xf4, 0x54, 0x1b, 0xc0, 0xfe,
  12975. 0x99, 0x31, 0xda, 0xad, 0x2e, 0xe1, 0x5d, 0x0c
  12976. };
  12977. byte tag2[] = {
  12978. 0x33, 0xaf, 0xec, 0x59, 0xc4, 0x5b, 0xaf, 0x68,
  12979. 0x9a, 0x5e, 0x1b, 0x13, 0xae, 0x42, 0x36, 0x19
  12980. };
  12981. byte key3[] = {
  12982. 0xb0, 0x1e, 0x45, 0xcc, 0x30, 0x88, 0xaa, 0xba,
  12983. 0x9f, 0xa4, 0x3d, 0x81, 0xd4, 0x81, 0x82, 0x3f
  12984. };
  12985. byte iv3[] = {
  12986. 0x5a, 0x2c, 0x4a, 0x66, 0x46, 0x87, 0x13, 0x45,
  12987. 0x6a, 0x4b, 0xd5, 0xe1
  12988. };
  12989. byte tag3[] = {
  12990. 0x01, 0x42, 0x80, 0xf9, 0x44, 0xf5, 0x3c, 0x68,
  12991. 0x11, 0x64, 0xb2, 0xff
  12992. };
  12993. wc_test_ret_t ret;
  12994. WOLFSSL_ENTER("aesgcm_default_test");
  12995. ret = aesgcm_default_test_helper(key1, sizeof(key1), iv1, sizeof(iv1),
  12996. plain1, sizeof(plain1), cipher1, sizeof(cipher1),
  12997. aad1, sizeof(aad1), tag1, sizeof(tag1));
  12998. if (ret != 0) {
  12999. return ret;
  13000. }
  13001. ret = aesgcm_default_test_helper(key2, sizeof(key2), iv2, sizeof(iv2),
  13002. plain2, sizeof(plain2), cipher2, sizeof(cipher2),
  13003. NULL, 0, tag2, sizeof(tag2));
  13004. if (ret != 0) {
  13005. return ret;
  13006. }
  13007. ret = aesgcm_default_test_helper(key3, sizeof(key3), iv3, sizeof(iv3),
  13008. NULL, 0, NULL, 0,
  13009. NULL, 0, tag3, sizeof(tag3));
  13010. if (ret != 0) {
  13011. return ret;
  13012. }
  13013. #endif
  13014. return 0;
  13015. }
  13016. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_test(void)
  13017. {
  13018. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13019. Aes *enc = NULL;
  13020. Aes *dec = NULL;
  13021. #else
  13022. Aes enc[1];
  13023. Aes dec[1];
  13024. #endif
  13025. /*
  13026. * This is Test Case 16 from the document Galois/
  13027. * Counter Mode of Operation (GCM) by McGrew and
  13028. * Viega.
  13029. */
  13030. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  13031. {
  13032. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  13033. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  13034. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  13035. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  13036. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  13037. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  13038. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  13039. 0xba, 0x63, 0x7b, 0x39
  13040. };
  13041. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  13042. {
  13043. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  13044. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  13045. 0xab, 0xad, 0xda, 0xd2
  13046. };
  13047. #ifdef WOLFSSL_AES_256
  13048. #ifdef HAVE_RENESAS_SYNC
  13049. const byte *k1 = (byte*)guser_PKCbInfo.wrapped_key_aes256;
  13050. int k1Sz = (int)(256/8);
  13051. #else
  13052. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  13053. {
  13054. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  13055. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  13056. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  13057. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  13058. };
  13059. int k1Sz = (int)sizeof(k1);
  13060. #endif
  13061. #endif /* WOLFSSL_AES_256 */
  13062. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  13063. {
  13064. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  13065. 0xde, 0xca, 0xf8, 0x88
  13066. };
  13067. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  13068. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  13069. {
  13070. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  13071. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  13072. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  13073. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  13074. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  13075. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  13076. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  13077. 0xbc, 0xc9, 0xf6, 0x62
  13078. };
  13079. #endif /* WOLFSSL_AES_256 || WOLFSSL_AES_192 */
  13080. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  13081. {
  13082. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  13083. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  13084. };
  13085. /* FIPS, QAT and PIC32MZ HW Crypto only support 12-byte IV */
  13086. #if !defined(HAVE_FIPS) && \
  13087. !defined(WOLFSSL_PIC32MZ_CRYPT) && \
  13088. !defined(FREESCALE_LTC) && !defined(FREESCALE_MMCAU) && \
  13089. !defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  13090. !defined(WOLFSSL_SILABS_SE_ACCEL) && !defined(WOLFSSL_KCAPI_AES) && \
  13091. !(defined(WOLF_CRYPTO_CB) && \
  13092. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  13093. #define ENABLE_NON_12BYTE_IV_TEST
  13094. #ifdef WOLFSSL_AES_192
  13095. /* Test Case 12, uses same plaintext and AAD data. */
  13096. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  13097. {
  13098. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  13099. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  13100. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c
  13101. };
  13102. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  13103. {
  13104. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  13105. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  13106. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  13107. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  13108. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  13109. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  13110. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  13111. 0xa6, 0x37, 0xb3, 0x9b
  13112. };
  13113. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  13114. {
  13115. 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  13116. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  13117. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  13118. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  13119. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  13120. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  13121. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  13122. 0xe9, 0xb7, 0x37, 0x3b
  13123. };
  13124. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  13125. {
  13126. 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  13127. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9
  13128. };
  13129. #endif /* WOLFSSL_AES_192 */
  13130. #ifdef WOLFSSL_AES_128
  13131. /* The following is an interesting test case from the example
  13132. * FIPS test vectors for AES-GCM. IVlen = 1 byte */
  13133. WOLFSSL_SMALL_STACK_STATIC const byte p3[] =
  13134. {
  13135. 0x57, 0xce, 0x45, 0x1f, 0xa5, 0xe2, 0x35, 0xa5,
  13136. 0x8e, 0x1a, 0xa2, 0x3b, 0x77, 0xcb, 0xaf, 0xe2
  13137. };
  13138. #ifdef HAVE_RENESAS_SYNC
  13139. const byte *k3 =
  13140. (byte*)guser_PKCbInfo.wrapped_key_aes128;
  13141. int k3Sz = (int)(128/8);
  13142. #else
  13143. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  13144. {
  13145. 0xbb, 0x01, 0xd7, 0x03, 0x81, 0x1c, 0x10, 0x1a,
  13146. 0x35, 0xe0, 0xff, 0xd2, 0x91, 0xba, 0xf2, 0x4b
  13147. };
  13148. int k3Sz = (int)sizeof(k3);
  13149. #endif
  13150. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  13151. {
  13152. 0xca
  13153. };
  13154. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  13155. {
  13156. 0x6b, 0x5f, 0xb3, 0x9d, 0xc1, 0xc5, 0x7a, 0x4f,
  13157. 0xf3, 0x51, 0x4d, 0xc2, 0xd5, 0xf0, 0xd0, 0x07
  13158. };
  13159. WOLFSSL_SMALL_STACK_STATIC const byte a3[] =
  13160. {
  13161. 0x40, 0xfc, 0xdc, 0xd7, 0x4a, 0xd7, 0x8b, 0xf1,
  13162. 0x3e, 0x7c, 0x60, 0x55, 0x50, 0x51, 0xdd, 0x54
  13163. };
  13164. WOLFSSL_SMALL_STACK_STATIC const byte t3[] =
  13165. {
  13166. 0x06, 0x90, 0xed, 0x01, 0x34, 0xdd, 0xc6, 0x95,
  13167. 0x31, 0x2e, 0x2a, 0xf9, 0x57, 0x7a, 0x1e, 0xa6
  13168. };
  13169. #endif /* WOLFSSL_AES_128 */
  13170. #ifdef WOLFSSL_AES_256
  13171. int ivlen;
  13172. #endif
  13173. #endif
  13174. byte resultT[sizeof(t1) + AES_BLOCK_SIZE];
  13175. byte resultP[sizeof(p) + AES_BLOCK_SIZE];
  13176. byte resultC[sizeof(p) + AES_BLOCK_SIZE];
  13177. wc_test_ret_t ret = 0;
  13178. int alen;
  13179. int plen;
  13180. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  13181. byte buf[sizeof(p) + AES_BLOCK_SIZE];
  13182. byte bufA[sizeof(a) + 1];
  13183. byte *large_aad = (byte*)XMALLOC((size_t)1024 + 16, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13184. #endif
  13185. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM)
  13186. #if !defined(BENCH_AESGCM_LARGE)
  13187. #define BENCH_AESGCM_LARGE 1024
  13188. #endif
  13189. #ifndef WOLFSSL_NO_MALLOC
  13190. byte *large_input = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13191. byte *large_output = (byte *)XMALLOC(BENCH_AESGCM_LARGE + AES_BLOCK_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13192. byte *large_outdec = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13193. (void)alen;
  13194. (void)plen;
  13195. if ((! large_input) || (! large_output) || (! large_outdec))
  13196. ERROR_OUT(MEMORY_E, out);
  13197. #else
  13198. byte large_input[BENCH_AESGCM_LARGE];
  13199. byte large_output[BENCH_AESGCM_LARGE + AES_BLOCK_SIZE];
  13200. byte large_outdec[BENCH_AESGCM_LARGE];
  13201. #endif
  13202. XMEMSET(large_input, 0, BENCH_AESGCM_LARGE);
  13203. XMEMSET(large_output, 0, BENCH_AESGCM_LARGE + AES_BLOCK_SIZE);
  13204. XMEMSET(large_outdec, 0, BENCH_AESGCM_LARGE);
  13205. #endif
  13206. WOLFSSL_ENTER("aesgcm_test");
  13207. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13208. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  13209. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  13210. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  13211. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  13212. #endif
  13213. XMEMSET(resultT, 0, sizeof(resultT));
  13214. XMEMSET(resultC, 0, sizeof(resultC));
  13215. XMEMSET(resultP, 0, sizeof(resultP));
  13216. ret = wc_AesInit(enc, HEAP_HINT, devId);
  13217. if (ret != 0)
  13218. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13219. ret = wc_AesInit(dec, HEAP_HINT, devId);
  13220. if (ret != 0)
  13221. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13222. #ifdef WOLFSSL_AES_256
  13223. ret = wc_AesGcmSetKey(enc, k1, (word32)k1Sz);
  13224. if (ret != 0)
  13225. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13226. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13227. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  13228. resultT, sizeof(t1), a, sizeof(a));
  13229. #if defined(WOLFSSL_ASYNC_CRYPT)
  13230. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13231. #endif
  13232. if (ret != 0)
  13233. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13234. #ifndef HAVE_RENESAS_SYNC
  13235. if (XMEMCMP(c1, resultC, sizeof(c1)))
  13236. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13237. if (XMEMCMP(t1, resultT, sizeof(t1)))
  13238. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13239. #endif
  13240. #ifdef HAVE_AES_DECRYPT
  13241. ret = wc_AesGcmSetKey(dec, k1, (word32)k1Sz);
  13242. if (ret != 0)
  13243. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13244. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1),
  13245. iv1, sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  13246. #if defined(WOLFSSL_ASYNC_CRYPT)
  13247. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13248. #endif
  13249. if (ret != 0)
  13250. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13251. if (XMEMCMP(p, resultP, sizeof(p)))
  13252. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13253. #endif /* HAVE_AES_DECRYPT */
  13254. /* Large buffer test */
  13255. #ifdef BENCH_AESGCM_LARGE
  13256. /* setup test buffer */
  13257. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  13258. large_input[alen] = (byte)alen;
  13259. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13260. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  13261. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  13262. resultT, sizeof(t1), a, sizeof(a));
  13263. #if defined(WOLFSSL_ASYNC_CRYPT)
  13264. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13265. #endif
  13266. if (ret != 0)
  13267. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13268. #ifdef HAVE_AES_DECRYPT
  13269. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  13270. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  13271. sizeof(t1), a, sizeof(a));
  13272. #if defined(WOLFSSL_ASYNC_CRYPT)
  13273. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13274. #endif
  13275. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  13276. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13277. if (ret != 0)
  13278. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13279. #endif /* HAVE_AES_DECRYPT */
  13280. #endif /* BENCH_AESGCM_LARGE */
  13281. #if defined(ENABLE_NON_12BYTE_IV_TEST) && defined(WOLFSSL_AES_256)
  13282. /* Variable IV length test */
  13283. for (ivlen=1; ivlen<k1Sz; ivlen++) {
  13284. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13285. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), k1,
  13286. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  13287. #if defined(WOLFSSL_ASYNC_CRYPT)
  13288. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13289. #endif
  13290. if (ret != 0)
  13291. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13292. #ifdef HAVE_AES_DECRYPT
  13293. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), k1,
  13294. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  13295. #if defined(WOLFSSL_ASYNC_CRYPT)
  13296. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13297. #endif
  13298. if (ret != 0)
  13299. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13300. #endif /* HAVE_AES_DECRYPT */
  13301. }
  13302. #endif
  13303. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  13304. /* Variable authenticated data length test */
  13305. for (alen=0; alen<(int)sizeof(p); alen++) {
  13306. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13307. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  13308. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  13309. #if defined(WOLFSSL_ASYNC_CRYPT)
  13310. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13311. #endif
  13312. if (ret != 0)
  13313. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13314. #ifdef HAVE_AES_DECRYPT
  13315. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  13316. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  13317. #if defined(WOLFSSL_ASYNC_CRYPT)
  13318. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13319. #endif
  13320. if (ret != 0)
  13321. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13322. #endif /* HAVE_AES_DECRYPT */
  13323. }
  13324. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  13325. if (! large_aad)
  13326. ERROR_OUT(MEMORY_E, out);
  13327. XMEMSET(large_aad, 0, 1024+16);
  13328. /* Variable authenticated data length test */
  13329. for (alen=0; alen<=1024; alen+=16) {
  13330. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13331. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), 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. #ifdef HAVE_AES_DECRYPT
  13336. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  13337. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  13338. if (ret != 0)
  13339. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13340. #endif /* HAVE_AES_DECRYPT */
  13341. }
  13342. /* Test unaligned memory of all potential arguments */
  13343. ret = wc_AesGcmSetKey(enc, k1, k1Sz);
  13344. if (ret != 0)
  13345. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13346. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13347. XMEMCPY(&buf[1], p, sizeof(p));
  13348. XMEMCPY(&bufA[1], a, sizeof(a));
  13349. ret = wc_AesGcmEncrypt(enc, &resultC[1], &buf[1], sizeof(p), iv1, sizeof(iv1),
  13350. &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  13351. if (ret != 0)
  13352. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13353. if (XMEMCMP(c1, &resultC[1], sizeof(c1)))
  13354. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13355. if (XMEMCMP(t1, &resultT[1], sizeof(t1)))
  13356. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13357. #ifdef HAVE_AES_DECRYPT
  13358. ret = wc_AesGcmSetKey(dec, k1, k1Sz);
  13359. if (ret != 0)
  13360. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13361. ret = wc_AesGcmDecrypt(dec, &resultP[1], &resultC[1], sizeof(c1),
  13362. iv1, sizeof(iv1), &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  13363. if (ret != 0)
  13364. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13365. if (XMEMCMP(p, &resultP[1], sizeof(p)))
  13366. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13367. #endif /* HAVE_AES_DECRYPT */
  13368. #endif /* Xilinx Versal */
  13369. #endif
  13370. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  13371. #ifdef BENCH_AESGCM_LARGE
  13372. /* Variable plain text length test */
  13373. for (plen=1; plen<BENCH_AESGCM_LARGE; plen++) {
  13374. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13375. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  13376. (word32)plen, iv1, sizeof(iv1), resultT,
  13377. sizeof(t1), a, sizeof(a));
  13378. #if defined(WOLFSSL_ASYNC_CRYPT)
  13379. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13380. #endif
  13381. if (ret != 0)
  13382. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13383. #ifdef HAVE_AES_DECRYPT
  13384. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  13385. (word32)plen, iv1, sizeof(iv1), resultT,
  13386. sizeof(t1), a, sizeof(a));
  13387. #if defined(WOLFSSL_ASYNC_CRYPT)
  13388. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13389. #endif
  13390. if (ret != 0)
  13391. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13392. #endif /* HAVE_AES_DECRYPT */
  13393. }
  13394. #else /* BENCH_AESGCM_LARGE */
  13395. /* Variable plain text length test */
  13396. for (plen=1; plen<(int)sizeof(p); plen++) {
  13397. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13398. ret = wc_AesGcmEncrypt(enc, resultC, p, (word32)plen, iv1,
  13399. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  13400. #if defined(WOLFSSL_ASYNC_CRYPT)
  13401. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13402. #endif
  13403. if (ret != 0)
  13404. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13405. #ifdef HAVE_AES_DECRYPT
  13406. ret = wc_AesGcmDecrypt(dec, resultP, resultC, (word32)plen, iv1,
  13407. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  13408. #if defined(WOLFSSL_ASYNC_CRYPT)
  13409. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13410. #endif
  13411. if (ret != 0)
  13412. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13413. #endif /* HAVE_AES_DECRYPT */
  13414. }
  13415. #endif /* BENCH_AESGCM_LARGE */
  13416. #endif
  13417. #endif /* WOLFSSL_AES_256 */
  13418. /* test with IV != 12 bytes */
  13419. #ifdef ENABLE_NON_12BYTE_IV_TEST
  13420. XMEMSET(resultT, 0, sizeof(resultT));
  13421. XMEMSET(resultC, 0, sizeof(resultC));
  13422. XMEMSET(resultP, 0, sizeof(resultP));
  13423. #ifdef WOLFSSL_AES_192
  13424. wc_AesGcmSetKey(enc, k2, sizeof(k2));
  13425. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13426. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv2, sizeof(iv2),
  13427. resultT, sizeof(t1), a, sizeof(a));
  13428. #if defined(WOLFSSL_ASYNC_CRYPT)
  13429. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13430. #endif
  13431. if (ret != 0)
  13432. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13433. if (XMEMCMP(c2, resultC, sizeof(c2)))
  13434. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13435. if (XMEMCMP(t2, resultT, sizeof(t1)))
  13436. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13437. #ifdef HAVE_AES_DECRYPT
  13438. ret = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c1),
  13439. iv2, sizeof(iv2), resultT, sizeof(t1), a, sizeof(a));
  13440. #if defined(WOLFSSL_ASYNC_CRYPT)
  13441. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13442. #endif
  13443. if (ret != 0)
  13444. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13445. if (XMEMCMP(p, resultP, sizeof(p)))
  13446. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13447. #endif /* HAVE_AES_DECRYPT */
  13448. /* Large buffer test */
  13449. #ifdef BENCH_AESGCM_LARGE
  13450. wc_AesGcmSetKey(enc, k2, (word32)sizeof(k2));
  13451. wc_AesGcmSetKey(dec, k2, (word32)sizeof(k2));
  13452. /* setup test buffer */
  13453. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  13454. large_input[alen] = (byte)alen;
  13455. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13456. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  13457. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  13458. resultT, sizeof(t1), a, sizeof(a));
  13459. #if defined(WOLFSSL_ASYNC_CRYPT)
  13460. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13461. #endif
  13462. if (ret != 0)
  13463. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13464. #ifdef HAVE_AES_DECRYPT
  13465. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  13466. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  13467. sizeof(t1), a, sizeof(a));
  13468. #if defined(WOLFSSL_ASYNC_CRYPT)
  13469. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13470. #endif
  13471. if (ret != 0)
  13472. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13473. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  13474. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13475. #endif /* HAVE_AES_DECRYPT */
  13476. #endif /* BENCH_AESGCM_LARGE */
  13477. XMEMSET(resultT, 0, sizeof(resultT));
  13478. XMEMSET(resultC, 0, sizeof(resultC));
  13479. XMEMSET(resultP, 0, sizeof(resultP));
  13480. #endif /* WOLFSSL_AES_192 */
  13481. #ifdef WOLFSSL_AES_128
  13482. wc_AesGcmSetKey(enc, k3, (word32)k3Sz);
  13483. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13484. ret = wc_AesGcmEncrypt(enc, resultC, p3, sizeof(p3), iv3, sizeof(iv3),
  13485. resultT, sizeof(t3), a3, sizeof(a3));
  13486. #if defined(WOLFSSL_ASYNC_CRYPT)
  13487. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13488. #endif
  13489. if (ret != 0)
  13490. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13491. #ifndef HAVE_RENESAS_SYNC
  13492. if (XMEMCMP(c3, resultC, sizeof(c3)))
  13493. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13494. if (XMEMCMP(t3, resultT, sizeof(t3)))
  13495. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13496. #endif
  13497. #ifdef HAVE_AES_DECRYPT
  13498. ret = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c3),
  13499. iv3, sizeof(iv3), resultT, sizeof(t3), a3, sizeof(a3));
  13500. #if defined(WOLFSSL_ASYNC_CRYPT)
  13501. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13502. #endif
  13503. if (ret != 0)
  13504. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13505. if (XMEMCMP(p3, resultP, sizeof(p3)))
  13506. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13507. #endif /* HAVE_AES_DECRYPT */
  13508. /* Large buffer test */
  13509. #ifdef BENCH_AESGCM_LARGE
  13510. wc_AesGcmSetKey(enc, k3, (word32)k3Sz);
  13511. wc_AesGcmSetKey(dec, k3, (word32)k3Sz);
  13512. /* setup test buffer */
  13513. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  13514. large_input[alen] = (byte)alen;
  13515. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13516. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  13517. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  13518. resultT, sizeof(t1), a, sizeof(a));
  13519. #if defined(WOLFSSL_ASYNC_CRYPT)
  13520. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13521. #endif
  13522. if (ret != 0)
  13523. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13524. #ifdef HAVE_AES_DECRYPT
  13525. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  13526. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  13527. sizeof(t1), a, sizeof(a));
  13528. #if defined(WOLFSSL_ASYNC_CRYPT)
  13529. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13530. #endif
  13531. if (ret != 0)
  13532. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13533. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  13534. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13535. #endif /* HAVE_AES_DECRYPT */
  13536. #endif /* BENCH_AESGCM_LARGE */
  13537. #endif /* WOLFSSL_AES_128 */
  13538. #endif /* ENABLE_NON_12BYTE_IV_TEST */
  13539. #if defined(WOLFSSL_AES_256) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  13540. !defined(WOLFSSL_XILINX_CRYPT) && \
  13541. !(defined(WOLF_CRYPTO_CB) && \
  13542. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC))
  13543. XMEMSET(resultT, 0, sizeof(resultT));
  13544. XMEMSET(resultC, 0, sizeof(resultC));
  13545. XMEMSET(resultP, 0, sizeof(resultP));
  13546. wc_AesGcmSetKey(enc, k1, (word32)k1Sz);
  13547. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  13548. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  13549. resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  13550. #if defined(WOLFSSL_ASYNC_CRYPT)
  13551. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13552. #endif
  13553. if (ret != 0)
  13554. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13555. #ifndef HAVE_RENESAS_SYNC
  13556. if (XMEMCMP(c1, resultC, sizeof(c1)))
  13557. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13558. if (XMEMCMP(t1, resultT + 1, sizeof(t1) - 1))
  13559. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13560. #endif
  13561. #ifdef HAVE_AES_DECRYPT
  13562. ret = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(p),
  13563. iv1, sizeof(iv1), resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  13564. #if defined(WOLFSSL_ASYNC_CRYPT)
  13565. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13566. #endif
  13567. if (ret != 0)
  13568. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13569. if (XMEMCMP(p, resultP, sizeof(p)))
  13570. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13571. #endif /* HAVE_AES_DECRYPT */
  13572. #endif /* WOLFSSL_AES_256 */
  13573. #if !defined(HAVE_FIPS) || \
  13574. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  13575. /* Test encrypt with internally generated IV */
  13576. #if defined(WOLFSSL_AES_256) && !(defined(WC_NO_RNG) || defined(HAVE_SELFTEST)) \
  13577. && !(defined(WOLF_CRYPTO_CB) && defined(HAVE_CAVIUM_OCTEON_SYNC))
  13578. {
  13579. WC_RNG rng;
  13580. byte randIV[12];
  13581. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  13582. if (ret != 0)
  13583. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13584. XMEMSET(randIV, 0, sizeof(randIV));
  13585. XMEMSET(resultT, 0, sizeof(resultT));
  13586. XMEMSET(resultC, 0, sizeof(resultC));
  13587. XMEMSET(resultP, 0, sizeof(resultP));
  13588. wc_AesGcmSetKey(enc, k1, (word32)k1Sz);
  13589. ret = wc_AesGcmSetIV(enc, sizeof(randIV), NULL, 0, &rng);
  13590. if (ret != 0)
  13591. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13592. ret = wc_AesGcmEncrypt_ex(enc,
  13593. resultC, p, sizeof(p),
  13594. randIV, sizeof(randIV),
  13595. resultT, sizeof(t1),
  13596. a, sizeof(a));
  13597. #if defined(WOLFSSL_ASYNC_CRYPT)
  13598. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  13599. #endif
  13600. if (ret != 0)
  13601. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13602. /* Check the IV has been set. */
  13603. {
  13604. word32 i, ivSum = 0;
  13605. for (i = 0; i < sizeof(randIV); i++)
  13606. ivSum += randIV[i];
  13607. if (ivSum == 0)
  13608. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13609. }
  13610. #ifdef HAVE_AES_DECRYPT
  13611. wc_AesGcmSetKey(dec, k1, (word32)k1Sz);
  13612. ret = wc_AesGcmSetIV(dec, sizeof(randIV), NULL, 0, &rng);
  13613. if (ret != 0)
  13614. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13615. ret = wc_AesGcmDecrypt(dec,
  13616. resultP, resultC, sizeof(c1),
  13617. randIV, sizeof(randIV),
  13618. resultT, sizeof(t1),
  13619. a, sizeof(a));
  13620. #if defined(WOLFSSL_ASYNC_CRYPT)
  13621. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  13622. #endif
  13623. if (ret != 0)
  13624. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13625. if (XMEMCMP(p, resultP, sizeof(p)))
  13626. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13627. #endif /* HAVE_AES_DECRYPT */
  13628. wc_FreeRng(&rng);
  13629. }
  13630. #endif /* WOLFSSL_AES_256 && !(WC_NO_RNG || HAVE_SELFTEST) */
  13631. #endif /* HAVE_FIPS_VERSION >= 2 */
  13632. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  13633. #ifdef WOLFSSL_AES_256
  13634. #ifdef WOLFSSL_AESGCM_STREAM
  13635. ret = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  13636. if (ret != 0)
  13637. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13638. ret = wc_AesGcmEncryptUpdate(enc, resultC, p, sizeof(p), a, sizeof(a));
  13639. if (ret != 0)
  13640. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13641. ret = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  13642. if (ret != 0)
  13643. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13644. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  13645. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13646. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  13647. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13648. #ifdef HAVE_AES_DECRYPT
  13649. ret = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  13650. if (ret != 0)
  13651. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13652. ret = wc_AesGcmDecryptUpdate(enc, resultP, c1, sizeof(c1), a, sizeof(a));
  13653. if (ret != 0)
  13654. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13655. ret = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  13656. if (ret != 0)
  13657. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13658. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  13659. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13660. #endif
  13661. /* alen is the size to pass in with each update. */
  13662. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  13663. ret = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  13664. if (ret != 0)
  13665. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13666. /* plen is the offset into AAD to update with. */
  13667. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  13668. int len = sizeof(a) - plen;
  13669. if (len > alen) len = alen;
  13670. ret = wc_AesGcmEncryptUpdate(enc, NULL, NULL, 0, a + plen, (word32)len);
  13671. if (ret != 0)
  13672. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13673. }
  13674. /* plen is the offset into plaintext to update with. */
  13675. for (plen = 0; plen < (int)sizeof(p); plen += alen) {
  13676. int len = sizeof(p) - plen;
  13677. if (len > alen) len = alen;
  13678. ret = wc_AesGcmEncryptUpdate(enc, resultC + plen, p + plen, (word32)len,
  13679. NULL, 0);
  13680. if (ret != 0)
  13681. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13682. }
  13683. ret = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  13684. if (ret != 0)
  13685. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13686. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  13687. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13688. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  13689. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13690. }
  13691. #ifdef HAVE_AES_DECRYPT
  13692. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  13693. ret = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  13694. if (ret != 0)
  13695. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13696. /* plen is the offset into AAD to update with. */
  13697. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  13698. int len = sizeof(a) - plen;
  13699. if (len > alen) len = alen;
  13700. ret = wc_AesGcmDecryptUpdate(enc, NULL, NULL, 0, a + plen, (word32)len);
  13701. if (ret != 0)
  13702. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13703. }
  13704. /* plen is the offset into cipher text to update with. */
  13705. for (plen = 0; plen < (int)sizeof(c1); plen += alen) {
  13706. int len = sizeof(c1) - plen;
  13707. if (len > alen) len = alen;
  13708. ret = wc_AesGcmDecryptUpdate(enc, resultP + plen, c1 + plen, (word32)len,
  13709. NULL, 0);
  13710. if (ret != 0)
  13711. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13712. }
  13713. ret = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  13714. if (ret != 0)
  13715. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13716. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  13717. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13718. }
  13719. #endif /* HAVE_AES_DECRYPT */
  13720. #ifdef BENCH_AESGCM_LARGE
  13721. /* setup test buffer */
  13722. ret = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  13723. if (ret != 0)
  13724. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13725. ret = wc_AesGcmEncryptUpdate(enc, large_output, large_input,
  13726. BENCH_AESGCM_LARGE, a, sizeof(a));
  13727. if (ret != 0)
  13728. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13729. ret = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  13730. if (ret != 0)
  13731. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13732. #ifdef HAVE_AES_DECRYPT
  13733. ret = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  13734. if (ret != 0)
  13735. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13736. ret = wc_AesGcmDecryptUpdate(enc, large_outdec, large_output,
  13737. BENCH_AESGCM_LARGE, a, sizeof(a));
  13738. if (ret != 0)
  13739. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13740. ret = wc_AesGcmDecryptFinal(enc, resultT, sizeof(t1));
  13741. if (ret != 0)
  13742. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13743. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  13744. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13745. #endif /* HAVE_AES_DECRYPT */
  13746. #endif /* BENCH_AESGCM_LARGE */
  13747. #endif /* WOLFSSL_AESGCM_STREAM */
  13748. #endif /* WOLFSSL_AES_256 */
  13749. #endif /* !WOLFSSL_AFALG_XILINX_AES && !WOLFSSL_XILINX_CRYPT */
  13750. wc_AesFree(enc);
  13751. wc_AesFree(dec);
  13752. ret = 0;
  13753. out:
  13754. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  13755. !defined(WOLFSSL_NO_MALLOC)
  13756. if (large_input)
  13757. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13758. if (large_output)
  13759. XFREE(large_output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13760. if (large_outdec)
  13761. XFREE(large_outdec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13762. #endif
  13763. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13764. if (enc)
  13765. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  13766. if (dec)
  13767. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  13768. #endif
  13769. return ret;
  13770. }
  13771. #ifdef WOLFSSL_AES_128
  13772. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t gmac_test(void)
  13773. {
  13774. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13775. Gmac *gmac;
  13776. #else
  13777. Gmac gmac[1];
  13778. #endif
  13779. wc_test_ret_t ret;
  13780. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  13781. {
  13782. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  13783. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  13784. };
  13785. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  13786. {
  13787. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  13788. 0xe2, 0x8c, 0x8f, 0x16
  13789. };
  13790. WOLFSSL_SMALL_STACK_STATIC const byte a1[] =
  13791. {
  13792. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  13793. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  13794. };
  13795. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  13796. {
  13797. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  13798. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  13799. };
  13800. #if (!defined(HAVE_FIPS) || \
  13801. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  13802. /* FIPS builds only allow 16-byte auth tags. */
  13803. /* This sample uses a 15-byte auth tag. */
  13804. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  13805. {
  13806. 0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
  13807. 0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
  13808. };
  13809. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  13810. {
  13811. 0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
  13812. 0x1a, 0x60, 0x24, 0xa7
  13813. };
  13814. WOLFSSL_SMALL_STACK_STATIC const byte a2[] =
  13815. {
  13816. 0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
  13817. 0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
  13818. };
  13819. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  13820. {
  13821. 0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
  13822. 0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
  13823. };
  13824. #endif
  13825. byte tag[16];
  13826. WOLFSSL_ENTER("gmac_test");
  13827. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13828. if ((gmac = (Gmac *)XMALLOC(sizeof *gmac, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  13829. return WC_TEST_RET_ENC_ERRNO;
  13830. #endif
  13831. XMEMSET(gmac, 0, sizeof *gmac); /* clear context */
  13832. (void)wc_AesInit(&gmac->aes, HEAP_HINT, INVALID_DEVID); /* Make sure devId updated */
  13833. XMEMSET(tag, 0, sizeof(tag));
  13834. wc_GmacSetKey(gmac, k1, sizeof(k1));
  13835. wc_GmacUpdate(gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
  13836. if (XMEMCMP(t1, tag, sizeof(t1)) != 0)
  13837. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13838. #if (!defined(HAVE_FIPS) || \
  13839. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) )
  13840. XMEMSET(tag, 0, sizeof(tag));
  13841. wc_GmacSetKey(gmac, k2, sizeof(k2));
  13842. wc_GmacUpdate(gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
  13843. if (XMEMCMP(t2, tag, sizeof(t2)) != 0)
  13844. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  13845. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && !defined(NO_AES_DECRYPT)
  13846. {
  13847. WOLFSSL_SMALL_STACK_STATIC const byte badT[] =
  13848. {
  13849. 0xde, 0xad, 0xbe, 0xef, 0x17, 0x2e, 0xd0, 0x43,
  13850. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  13851. };
  13852. WC_RNG rng;
  13853. byte iv[12];
  13854. #ifndef HAVE_FIPS
  13855. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  13856. if (ret != 0)
  13857. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13858. #else
  13859. ret = wc_InitRng(&rng);
  13860. if (ret != 0)
  13861. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13862. #endif
  13863. ret = wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  13864. t1, sizeof(t1));
  13865. if (ret != 0)
  13866. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13867. ret = wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  13868. badT, sizeof(badT));
  13869. if (ret != AES_GCM_AUTH_E)
  13870. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13871. ret = wc_GmacVerify(k2, sizeof(k2), iv2, sizeof(iv2), a2, sizeof(a2),
  13872. t2, sizeof(t2));
  13873. if (ret != 0)
  13874. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13875. XMEMSET(tag, 0, sizeof(tag));
  13876. XMEMSET(iv, 0, sizeof(iv));
  13877. ret = wc_Gmac(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  13878. tag, sizeof(tag), &rng);
  13879. if (ret != 0)
  13880. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13881. ret = wc_GmacVerify(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  13882. tag, sizeof(tag));
  13883. if (ret != 0)
  13884. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  13885. wc_FreeRng(&rng);
  13886. }
  13887. #endif /* !WC_NO_RNG && !HAVE_SELFTEST && !NO_AES_DECRYPT */
  13888. #endif /* HAVE_FIPS */
  13889. ret = 0;
  13890. out:
  13891. wc_AesFree(&gmac->aes);
  13892. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13893. XFREE(gmac, HEAP_HINT, DYNAMIC_TYPE_AES);
  13894. #endif
  13895. return ret;
  13896. }
  13897. #endif /* WOLFSSL_AES_128 */
  13898. #endif /* HAVE_AESGCM */
  13899. #if defined(HAVE_AESCCM)
  13900. #if defined(WOLFSSL_AES_256)
  13901. static wc_test_ret_t aesccm_256_test(void)
  13902. {
  13903. wc_test_ret_t ret;
  13904. /* Test vectors from NIST AES CCM 256-bit CAST Example #1 */
  13905. WOLFSSL_SMALL_STACK_STATIC const byte in_key[32] = {
  13906. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  13907. 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
  13908. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  13909. 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F
  13910. };
  13911. WOLFSSL_SMALL_STACK_STATIC const byte in_nonce[7] = {
  13912. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
  13913. WOLFSSL_SMALL_STACK_STATIC const byte in_auth[8] = {
  13914. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  13915. WOLFSSL_SMALL_STACK_STATIC const byte in_plaintext[4] = {
  13916. 0x20, 0x21, 0x22, 0x23};
  13917. WOLFSSL_SMALL_STACK_STATIC const byte exp_ciphertext[4] = {
  13918. 0x8A, 0xB1, 0xA8, 0x74};
  13919. WOLFSSL_SMALL_STACK_STATIC const byte exp_tag[4] = {
  13920. 0x95, 0xFC, 0x08, 0x20};
  13921. byte output[sizeof(in_plaintext)];
  13922. byte atag[sizeof(exp_tag)];
  13923. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13924. Aes* aes = (Aes*)XMALLOC(sizeof(Aes), HEAP_HINT, DYNAMIC_TYPE_AES);
  13925. if (aes == NULL) {
  13926. return MEMORY_E;
  13927. }
  13928. #else
  13929. Aes aes[1];
  13930. #endif
  13931. ret = wc_AesInit(aes, HEAP_HINT, devId);
  13932. if (ret == 0) {
  13933. ret = wc_AesCcmSetKey(aes, in_key, sizeof(in_key));
  13934. }
  13935. if (ret == 0) {
  13936. ret = wc_AesCcmEncrypt(aes, output, in_plaintext, sizeof(in_plaintext),
  13937. in_nonce, sizeof(in_nonce),
  13938. atag, sizeof(atag),
  13939. in_auth, sizeof(in_auth));
  13940. }
  13941. /* Verify we produce the proper ciphertext and tag */
  13942. if (ret == 0 &&
  13943. (XMEMCMP(output, exp_ciphertext, sizeof(output)) ||
  13944. XMEMCMP(atag, exp_tag, sizeof(atag)))) {
  13945. ret = WC_TEST_RET_ENC_NC;
  13946. }
  13947. #ifdef HAVE_AES_DECRYPT
  13948. if (ret == 0) {
  13949. /* decrypt inline */
  13950. ret = wc_AesCcmDecrypt(aes, output, output, sizeof(output),
  13951. in_nonce, sizeof(in_nonce),
  13952. atag, sizeof(atag),
  13953. in_auth, sizeof(in_auth));
  13954. }
  13955. /* Verify decryption was successful */
  13956. if (ret == 0 &&
  13957. XMEMCMP(output, in_plaintext, sizeof(output))) {
  13958. ret = WC_TEST_RET_ENC_NC;
  13959. }
  13960. #endif
  13961. wc_AesFree(aes);
  13962. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13963. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  13964. #endif
  13965. return ret;
  13966. }
  13967. #endif /* WOLFSSL_AES_256 */
  13968. #if defined(WOLFSSL_AES_128)
  13969. static wc_test_ret_t aesccm_128_test(void)
  13970. {
  13971. wc_test_ret_t ret;
  13972. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13973. Aes *enc;
  13974. #else
  13975. Aes enc[1];
  13976. #endif
  13977. /* key */
  13978. WOLFSSL_SMALL_STACK_STATIC const byte k[] =
  13979. {
  13980. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  13981. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  13982. };
  13983. /* nonce */
  13984. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  13985. {
  13986. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  13987. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  13988. };
  13989. /* plaintext */
  13990. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  13991. {
  13992. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  13993. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  13994. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  13995. };
  13996. /* plaintext - long */
  13997. WOLFSSL_SMALL_STACK_STATIC const byte pl[] =
  13998. {
  13999. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  14000. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  14001. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  14002. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  14003. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  14004. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14005. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  14006. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  14007. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  14008. 0x50
  14009. };
  14010. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  14011. {
  14012. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  14013. };
  14014. /* ciphertext */
  14015. WOLFSSL_SMALL_STACK_STATIC const byte c[] =
  14016. {
  14017. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  14018. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  14019. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  14020. };
  14021. /* tag - authentication */
  14022. WOLFSSL_SMALL_STACK_STATIC const byte t[] =
  14023. {
  14024. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  14025. };
  14026. /* ciphertext - long */
  14027. WOLFSSL_SMALL_STACK_STATIC const byte cl[] =
  14028. {
  14029. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  14030. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  14031. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0xe0,
  14032. 0x44, 0x2d, 0xbe, 0x25, 0xfa, 0x48, 0x2b, 0xa8,
  14033. 0x36, 0x0b, 0xbf, 0x01, 0xc0, 0x12, 0x45, 0xa4,
  14034. 0x82, 0x9f, 0x20, 0x6c, 0xc3, 0xd6, 0xae, 0x5b,
  14035. 0x54, 0x8d, 0xd0, 0xb1, 0x69, 0x2c, 0xec, 0x5e,
  14036. 0x95, 0xa5, 0x6b, 0x48, 0xc3, 0xc6, 0xc8, 0x9e,
  14037. 0xc7, 0x92, 0x98, 0x9d, 0x26, 0x7d, 0x2a, 0x10,
  14038. 0x0b
  14039. };
  14040. /* tag - authentication - long */
  14041. WOLFSSL_SMALL_STACK_STATIC const byte tl[] =
  14042. {
  14043. 0x89, 0xd8, 0xd2, 0x02, 0xc5, 0xcf, 0xae, 0xf4
  14044. };
  14045. /* tag - authentication - empty plaintext */
  14046. WOLFSSL_SMALL_STACK_STATIC const byte t_empty[] =
  14047. {
  14048. 0xe4, 0x28, 0x8a, 0xc3, 0x78, 0x00, 0x0f, 0xf5
  14049. };
  14050. byte t2[sizeof(t)];
  14051. byte p2[sizeof(p)];
  14052. byte c2[sizeof(c)];
  14053. byte iv2[sizeof(iv)];
  14054. byte pl2[sizeof(pl)];
  14055. byte cl2[sizeof(cl)];
  14056. byte tl2[sizeof(tl)];
  14057. byte t_empty2[sizeof(t_empty)];
  14058. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14059. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  14060. return WC_TEST_RET_ENC_ERRNO;
  14061. #endif
  14062. XMEMSET(enc, 0, sizeof *enc); /* clear context */
  14063. XMEMSET(t2, 0, sizeof(t2));
  14064. XMEMSET(c2, 0, sizeof(c2));
  14065. XMEMSET(p2, 0, sizeof(p2));
  14066. ret = wc_AesInit(enc, HEAP_HINT, devId);
  14067. if (ret != 0)
  14068. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14069. ret = wc_AesCcmSetKey(enc, k, sizeof(k));
  14070. if (ret != 0)
  14071. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14072. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  14073. ret = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  14074. t2, sizeof(t2), a, sizeof(a));
  14075. if (ret != 0)
  14076. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14077. if (XMEMCMP(c, c2, sizeof(c2)))
  14078. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14079. if (XMEMCMP(t, t2, sizeof(t2)))
  14080. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14081. #ifdef HAVE_AES_DECRYPT
  14082. ret = wc_AesCcmDecrypt(enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  14083. t2, sizeof(t2), a, sizeof(a));
  14084. if (ret != 0)
  14085. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14086. if (XMEMCMP(p, p2, sizeof(p2)))
  14087. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14088. /* Test the authentication failure */
  14089. t2[0]++; /* Corrupt the authentication tag. */
  14090. ret = wc_AesCcmDecrypt(enc, p2, c, sizeof(p2), iv, sizeof(iv),
  14091. t2, sizeof(t2), a, sizeof(a));
  14092. if (ret == 0)
  14093. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14094. /* Clear c2 to compare against p2. p2 should be set to zero in case of
  14095. * authentication fail. */
  14096. XMEMSET(c2, 0, sizeof(c2));
  14097. if (XMEMCMP(p2, c2, sizeof(p2)))
  14098. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14099. #endif
  14100. wc_AesFree(enc);
  14101. XMEMSET(enc, 0, sizeof(Aes)); /* clear context */
  14102. XMEMSET(t2, 0, sizeof(t2));
  14103. XMEMSET(c2, 0, sizeof(c2));
  14104. XMEMSET(p2, 0, sizeof(p2));
  14105. XMEMSET(iv2, 0, sizeof(iv2));
  14106. ret = wc_AesInit(enc, HEAP_HINT, devId);
  14107. if (ret != 0)
  14108. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14109. #ifndef HAVE_SELFTEST
  14110. /* selftest build does not have wc_AesCcmSetNonce() or
  14111. * wc_AesCcmEncrypt_ex() */
  14112. ret = wc_AesCcmSetKey(enc, k, sizeof(k));
  14113. if (ret != 0)
  14114. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14115. ret = wc_AesCcmSetNonce(enc, iv, sizeof(iv));
  14116. if (ret != 0)
  14117. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14118. ret = wc_AesCcmEncrypt_ex(enc, c2, p, sizeof(c2), iv2, sizeof(iv2),
  14119. t2, sizeof(t2), a, sizeof(a));
  14120. if (ret != 0)
  14121. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14122. if (XMEMCMP(iv, iv2, sizeof(iv2)))
  14123. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14124. if (XMEMCMP(c, c2, sizeof(c2)))
  14125. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14126. if (XMEMCMP(t, t2, sizeof(t2)))
  14127. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14128. #endif
  14129. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  14130. /* test fail on invalid IV sizes */
  14131. ret = wc_AesCcmSetKey(enc, k, sizeof(k));
  14132. if (ret != 0)
  14133. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14134. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  14135. ret = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  14136. t2, 1, a, sizeof(a));
  14137. if (ret == 0) {
  14138. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14139. }
  14140. #endif
  14141. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  14142. ret = wc_AesCcmEncrypt(enc, cl2, pl, sizeof(cl2), iv, sizeof(iv),
  14143. tl2, sizeof(tl2), a, sizeof(a));
  14144. if (ret != 0)
  14145. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14146. if (XMEMCMP(cl, cl2, sizeof(cl2)))
  14147. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14148. if (XMEMCMP(tl, tl2, sizeof(tl2)))
  14149. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14150. #ifdef HAVE_AES_DECRYPT
  14151. ret = wc_AesCcmDecrypt(enc, pl2, cl2, sizeof(pl2), iv, sizeof(iv),
  14152. tl2, sizeof(tl2), a, sizeof(a));
  14153. if (ret != 0)
  14154. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14155. if (XMEMCMP(pl, pl2, sizeof(pl2)))
  14156. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14157. #endif
  14158. /* test empty message as null input or output with nonzero inSz. */
  14159. ret = wc_AesCcmEncrypt(enc, pl2 /* out */, NULL /* 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. ret = wc_AesCcmEncrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  14165. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  14166. a, sizeof(a));
  14167. if (ret != BAD_FUNC_ARG)
  14168. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14169. #ifdef HAVE_AES_DECRYPT
  14170. ret = wc_AesCcmDecrypt(enc, pl2, NULL /* 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. ret = wc_AesCcmDecrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  14176. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  14177. sizeof(a));
  14178. if (ret != BAD_FUNC_ARG)
  14179. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14180. #endif
  14181. /* test empty message as null input and output with zero inSz --
  14182. * must either succeed, or fail early with BAD_FUNC_ARG.
  14183. */
  14184. ret = wc_AesCcmEncrypt(enc, NULL /* out */, NULL /* in */, 0 /* inSz */,
  14185. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  14186. a, sizeof(a));
  14187. if (ret != BAD_FUNC_ARG) {
  14188. if (ret != 0)
  14189. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14190. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  14191. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14192. #ifdef HAVE_AES_DECRYPT
  14193. ret = wc_AesCcmDecrypt(enc, NULL /* out */, NULL /* in */,
  14194. 0 /* inSz */, iv, sizeof(iv), t_empty2,
  14195. sizeof(t_empty2), a, sizeof(a));
  14196. if (ret != 0)
  14197. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14198. #endif
  14199. }
  14200. /* test empty message as zero-length string -- must work. */
  14201. ret = wc_AesCcmEncrypt(enc, pl2, (const byte *)"", 0 /* inSz */, iv,
  14202. sizeof(iv), t_empty2, sizeof(t_empty2), a,
  14203. sizeof(a));
  14204. if (ret != 0)
  14205. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14206. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  14207. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  14208. #ifdef HAVE_AES_DECRYPT
  14209. ret = wc_AesCcmDecrypt(enc, pl2, (const byte *)"", 0 /* inSz */,
  14210. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  14211. sizeof(a));
  14212. if (ret != 0)
  14213. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14214. #endif
  14215. wc_AesFree(enc);
  14216. ret = 0;
  14217. out:
  14218. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14219. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  14220. #endif
  14221. return ret;
  14222. }
  14223. #endif /* WOLFSSL_AES_128 */
  14224. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesccm_test(void)
  14225. {
  14226. wc_test_ret_t ret = 0;
  14227. WOLFSSL_ENTER("aesccm_test");
  14228. #ifdef WOLFSSL_AES_128
  14229. if (ret == 0)
  14230. ret = aesccm_128_test();
  14231. #endif
  14232. #ifdef WOLFSSL_AES_256
  14233. if (ret == 0)
  14234. ret = aesccm_256_test();
  14235. #endif
  14236. return ret;
  14237. }
  14238. #endif /* HAVE_AESCCM */
  14239. #if defined(WOLFSSL_AES_EAX) && \
  14240. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  14241. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_eax_test(void)
  14242. {
  14243. typedef struct {
  14244. byte key[AES_256_KEY_SIZE];
  14245. int key_length;
  14246. byte iv[AES_BLOCK_SIZE];
  14247. int iv_length;
  14248. byte aad[AES_BLOCK_SIZE * 2];
  14249. int aad_length;
  14250. byte msg[AES_BLOCK_SIZE * 2];
  14251. int msg_length;
  14252. byte ct[AES_BLOCK_SIZE * 2];
  14253. int ct_length;
  14254. byte tag[AES_BLOCK_SIZE];
  14255. int tag_length;
  14256. int valid;
  14257. } AadVector;
  14258. /* A small selection of Google wycheproof vectors that use vectors
  14259. * from the original paper: eprint.iacr.org/2003/069
  14260. * https://github.com/google/wycheproof/blob/master/testvectors/aes_eax_test.json
  14261. */
  14262. WOLFSSL_SMALL_STACK_STATIC const AadVector vectors[] = {
  14263. /* Vector from paper - empty message with auth data */
  14264. {
  14265. /* key, key length */
  14266. {0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f,
  14267. 0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78}, 16,
  14268. /* iv, iv length */
  14269. {0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07,
  14270. 0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3}, 16,
  14271. /* aad, aad length */
  14272. {0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b}, 8,
  14273. /* msg, msg length */
  14274. {0}, 0,
  14275. /* ct, ct length */
  14276. {0}, 0,
  14277. /* tag, tag length */
  14278. {0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2,
  14279. 0x7b, 0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01}, 16,
  14280. /* valid */
  14281. 1,
  14282. },
  14283. /* Vector from paper - no auth data, valid auth tag */
  14284. {
  14285. /* key, key length */
  14286. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14287. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  14288. /* iv , iv length */
  14289. {0x3c, 0x8c, 0xc2, 0x97, 0x0a, 0x00, 0x8f, 0x75,
  14290. 0xcc, 0x5b, 0xea, 0xe2, 0x84, 0x72, 0x58, 0xc2}, 16,
  14291. /* aad, aad length */
  14292. {0}, 0,
  14293. /* msg, msg length */
  14294. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  14295. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  14296. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  14297. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  14298. /* ct, ct length */
  14299. {0x3c, 0x44, 0x1f, 0x32, 0xce, 0x07, 0x82, 0x23,
  14300. 0x64, 0xd7, 0xa2, 0x99, 0x0e, 0x50, 0xbb, 0x13,
  14301. 0xd7, 0xb0, 0x2a, 0x26, 0x96, 0x9e, 0x4a, 0x93,
  14302. 0x7e, 0x5e, 0x90, 0x73, 0xb0, 0xd9, 0xc9, 0x68}, 32,
  14303. /* tag, tag length */
  14304. {0xdb, 0x90, 0xbd, 0xb3, 0xda, 0x3d, 0x00, 0xaf,
  14305. 0xd0, 0xfc, 0x6a, 0x83, 0x55, 0x1d, 0xa9, 0x5e}, 16,
  14306. /* valid */
  14307. 1,
  14308. },
  14309. /* Vector from paper - no auth data with invalid auth tag */
  14310. {
  14311. /* key, key length */
  14312. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14313. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  14314. /* iv, iv length */
  14315. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  14316. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  14317. /* aad, aad length */
  14318. {0}, 0,
  14319. /* msg, msg length */
  14320. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  14321. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  14322. /* ct , ct length */
  14323. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  14324. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  14325. /* tag, tag length */
  14326. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  14327. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  14328. /* valid */
  14329. 0,
  14330. },
  14331. };
  14332. WOLFSSL_SMALL_STACK_STATIC byte ciphertext[sizeof(vectors[0].ct)];
  14333. WOLFSSL_SMALL_STACK_STATIC byte authtag[sizeof(vectors[0].tag)];
  14334. int i;
  14335. int len;
  14336. wc_test_ret_t ret;
  14337. WOLFSSL_ENTER("aes_eax_test");
  14338. for (i = 0; i < (int)(sizeof(vectors)/sizeof(vectors[0])); i++) {
  14339. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  14340. len = sizeof(authtag);
  14341. ret = wc_AesEaxEncryptAuth(vectors[i].key, (word32)vectors[i].key_length,
  14342. ciphertext,
  14343. vectors[i].msg, (word32)vectors[i].msg_length,
  14344. vectors[i].iv, (word32)vectors[i].iv_length,
  14345. authtag, (word32)len,
  14346. vectors[i].aad, (word32)vectors[i].aad_length);
  14347. if (ret != 0) {
  14348. return WC_TEST_RET_ENC_EC(ret);
  14349. }
  14350. /* check ciphertext matches vector */
  14351. if (XMEMCMP(ciphertext, vectors[i].ct, (size_t)vectors[i].ct_length)) {
  14352. return WC_TEST_RET_ENC_NC;
  14353. }
  14354. /* check that tag matches vector only for vectors marked as valid */
  14355. ret = XMEMCMP(authtag, vectors[i].tag, len);
  14356. if (vectors[i].valid == 1 && ret != 0 ) {
  14357. return WC_TEST_RET_ENC_NC;
  14358. }
  14359. else if (vectors[i].valid == 0 && ret == 0) {
  14360. return WC_TEST_RET_ENC_NC;
  14361. }
  14362. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  14363. ret = wc_AesEaxDecryptAuth(vectors[i].key, (word32)vectors[i].key_length,
  14364. ciphertext,
  14365. vectors[i].ct, (word32)vectors[i].ct_length,
  14366. vectors[i].iv, (word32)vectors[i].iv_length,
  14367. authtag, (word32)len,
  14368. vectors[i].aad, (word32)vectors[i].aad_length);
  14369. if (ret != 0) {
  14370. return WC_TEST_RET_ENC_EC(ret);
  14371. }
  14372. /* check decrypted ciphertext matches vector plaintext */
  14373. if (XMEMCMP(ciphertext, vectors[i].msg, (size_t)vectors[i].msg_length)) {
  14374. return WC_TEST_RET_ENC_NC;
  14375. }
  14376. }
  14377. return 0;
  14378. }
  14379. #endif /* WOLFSSL_AES_EAX */
  14380. #ifdef HAVE_AES_KEYWRAP
  14381. #define MAX_KEYWRAP_TEST_OUTLEN 40
  14382. #define MAX_KEYWRAP_TEST_PLAINLEN 32
  14383. typedef struct keywrapVector {
  14384. const byte* kek;
  14385. const byte* data;
  14386. const byte* verify;
  14387. word32 kekLen;
  14388. word32 dataLen;
  14389. word32 verifyLen;
  14390. } keywrapVector;
  14391. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aeskeywrap_test(void)
  14392. {
  14393. int wrapSz, plainSz, testSz, i;
  14394. /* test vectors from RFC 3394 (kek, data, verify) */
  14395. #ifdef WOLFSSL_AES_128
  14396. /* Wrap 128 bits of Key Data with a 128-bit KEK */
  14397. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  14398. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14399. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  14400. };
  14401. WOLFSSL_SMALL_STACK_STATIC const byte d1[] = {
  14402. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14403. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  14404. };
  14405. WOLFSSL_SMALL_STACK_STATIC const byte v1[] = {
  14406. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  14407. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  14408. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  14409. };
  14410. #endif /* WOLFSSL_AES_128 */
  14411. #ifdef WOLFSSL_AES_192
  14412. /* Wrap 128 bits of Key Data with a 192-bit KEK */
  14413. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  14414. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14415. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  14416. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  14417. };
  14418. WOLFSSL_SMALL_STACK_STATIC const byte d2[] = {
  14419. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14420. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  14421. };
  14422. WOLFSSL_SMALL_STACK_STATIC const byte v2[] = {
  14423. 0x96, 0x77, 0x8B, 0x25, 0xAE, 0x6C, 0xA4, 0x35,
  14424. 0xF9, 0x2B, 0x5B, 0x97, 0xC0, 0x50, 0xAE, 0xD2,
  14425. 0x46, 0x8A, 0xB8, 0xA1, 0x7A, 0xD8, 0x4E, 0x5D
  14426. };
  14427. #endif
  14428. #ifdef WOLFSSL_AES_256
  14429. /* Wrap 128 bits of Key Data with a 256-bit KEK */
  14430. WOLFSSL_SMALL_STACK_STATIC const byte k3[] = {
  14431. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14432. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  14433. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  14434. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  14435. };
  14436. WOLFSSL_SMALL_STACK_STATIC const byte d3[] = {
  14437. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14438. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  14439. };
  14440. WOLFSSL_SMALL_STACK_STATIC const byte v3[] = {
  14441. 0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2,
  14442. 0x63, 0xE9, 0x77, 0x79, 0x05, 0x81, 0x8A, 0x2A,
  14443. 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7
  14444. };
  14445. #endif
  14446. #ifdef WOLFSSL_AES_192
  14447. /* Wrap 192 bits of Key Data with a 192-bit KEK */
  14448. WOLFSSL_SMALL_STACK_STATIC const byte k4[] = {
  14449. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14450. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  14451. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  14452. };
  14453. WOLFSSL_SMALL_STACK_STATIC const byte d4[] = {
  14454. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14455. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  14456. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  14457. };
  14458. WOLFSSL_SMALL_STACK_STATIC const byte v4[] = {
  14459. 0x03, 0x1D, 0x33, 0x26, 0x4E, 0x15, 0xD3, 0x32,
  14460. 0x68, 0xF2, 0x4E, 0xC2, 0x60, 0x74, 0x3E, 0xDC,
  14461. 0xE1, 0xC6, 0xC7, 0xDD, 0xEE, 0x72, 0x5A, 0x93,
  14462. 0x6B, 0xA8, 0x14, 0x91, 0x5C, 0x67, 0x62, 0xD2
  14463. };
  14464. #endif
  14465. #ifdef WOLFSSL_AES_256
  14466. /* Wrap 192 bits of Key Data with a 256-bit KEK */
  14467. WOLFSSL_SMALL_STACK_STATIC const byte k5[] = {
  14468. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14469. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  14470. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  14471. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  14472. };
  14473. WOLFSSL_SMALL_STACK_STATIC const byte d5[] = {
  14474. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14475. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  14476. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  14477. };
  14478. WOLFSSL_SMALL_STACK_STATIC const byte v5[] = {
  14479. 0xA8, 0xF9, 0xBC, 0x16, 0x12, 0xC6, 0x8B, 0x3F,
  14480. 0xF6, 0xE6, 0xF4, 0xFB, 0xE3, 0x0E, 0x71, 0xE4,
  14481. 0x76, 0x9C, 0x8B, 0x80, 0xA3, 0x2C, 0xB8, 0x95,
  14482. 0x8C, 0xD5, 0xD1, 0x7D, 0x6B, 0x25, 0x4D, 0xA1
  14483. };
  14484. /* Wrap 256 bits of Key Data with a 256-bit KEK */
  14485. WOLFSSL_SMALL_STACK_STATIC const byte k6[] = {
  14486. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14487. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  14488. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  14489. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  14490. };
  14491. WOLFSSL_SMALL_STACK_STATIC const byte d6[] = {
  14492. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14493. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  14494. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14495. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  14496. };
  14497. WOLFSSL_SMALL_STACK_STATIC const byte v6[] = {
  14498. 0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4,
  14499. 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, 0xF8, 0x26,
  14500. 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26,
  14501. 0xCB, 0xC7, 0xF0, 0xE7, 0x1A, 0x99, 0xF4, 0x3B,
  14502. 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21
  14503. };
  14504. #endif /* WOLFSSL_AES_256 */
  14505. byte output[MAX_KEYWRAP_TEST_OUTLEN];
  14506. byte plain [MAX_KEYWRAP_TEST_PLAINLEN];
  14507. const keywrapVector test_wrap[] =
  14508. {
  14509. #ifdef WOLFSSL_AES_128
  14510. {k1, d1, v1, sizeof(k1), sizeof(d1), sizeof(v1)},
  14511. #endif
  14512. #ifdef WOLFSSL_AES_192
  14513. {k2, d2, v2, sizeof(k2), sizeof(d2), sizeof(v2)},
  14514. #endif
  14515. #ifdef WOLFSSL_AES_256
  14516. {k3, d3, v3, sizeof(k3), sizeof(d3), sizeof(v3)},
  14517. #endif
  14518. #ifdef WOLFSSL_AES_192
  14519. {k4, d4, v4, sizeof(k4), sizeof(d4), sizeof(v4)},
  14520. #endif
  14521. #ifdef WOLFSSL_AES_256
  14522. {k5, d5, v5, sizeof(k5), sizeof(d5), sizeof(v5)},
  14523. {k6, d6, v6, sizeof(k6), sizeof(d6), sizeof(v6)}
  14524. #endif
  14525. };
  14526. WOLFSSL_ENTER("aeskeywrap_test");
  14527. testSz = sizeof(test_wrap) / sizeof(keywrapVector);
  14528. XMEMSET(output, 0, sizeof(output));
  14529. XMEMSET(plain, 0, sizeof(plain));
  14530. for (i = 0; i < testSz; i++) {
  14531. wrapSz = wc_AesKeyWrap(test_wrap[i].kek, test_wrap[i].kekLen,
  14532. test_wrap[i].data, test_wrap[i].dataLen,
  14533. output, sizeof(output), NULL);
  14534. if ( (wrapSz < 0) || (wrapSz != (int)test_wrap[i].verifyLen) )
  14535. return WC_TEST_RET_ENC_NC;
  14536. if (XMEMCMP(output, test_wrap[i].verify, test_wrap[i].verifyLen) != 0)
  14537. return WC_TEST_RET_ENC_NC;
  14538. plainSz = wc_AesKeyUnWrap((byte*)test_wrap[i].kek, test_wrap[i].kekLen,
  14539. output, (word32)wrapSz,
  14540. plain, sizeof(plain), NULL);
  14541. if ( (plainSz < 0) || (plainSz != (int)test_wrap[i].dataLen) )
  14542. return WC_TEST_RET_ENC_NC;
  14543. if (XMEMCMP(plain, test_wrap[i].data, test_wrap[i].dataLen) != 0)
  14544. return WC_TEST_RET_ENC_I(i);
  14545. }
  14546. return 0;
  14547. }
  14548. #endif /* HAVE_AES_KEYWRAP */
  14549. #endif /* NO_AES */
  14550. #ifdef HAVE_ARIA
  14551. void printOutput(const char *strName, unsigned char *data, unsigned int dataSz)
  14552. {
  14553. #ifndef DEBUG_WOLFSSL
  14554. (void)strName;
  14555. (void)data;
  14556. (void)dataSz;
  14557. #else
  14558. WOLFSSL_MSG_EX("%s (%d):", strName,dataSz);
  14559. WOLFSSL_BUFFER(data,dataSz);
  14560. #endif
  14561. }
  14562. WOLFSSL_TEST_SUBROUTINE int ariagcm_test(MC_ALGID algo)
  14563. {
  14564. byte data[] = TEST_STRING;
  14565. word32 dataSz = TEST_STRING_SZ;
  14566. /* Arbitrarily random long key that we will truncate to the right size */
  14567. byte key[] = { 0x1E, 0xCC, 0x95, 0xCB, 0xD3, 0x74, 0x58, 0x4F,
  14568. 0x6F, 0x8A, 0x70, 0x26, 0xF7, 0x3C, 0x8D, 0xB6,
  14569. 0xDC, 0x32, 0x76, 0x20, 0xCF, 0x05, 0x4A, 0xCF,
  14570. 0x11, 0x86, 0xCD, 0x23, 0x5E, 0xC1, 0x6E, 0x2B };
  14571. byte cipher[2*TEST_STRING_SZ], plain[TEST_STRING_SZ], ad[256], authTag[AES_BLOCK_SIZE];
  14572. word32 keySz, adSz = 256, authTagSz = sizeof(authTag);
  14573. wc_Aria aria;
  14574. int ret = 0;
  14575. WOLFSSL_ENTER("ariagcm_test");
  14576. XMEMSET((void *)&aria, 0, sizeof(aria));
  14577. ret = wc_AriaInitCrypt(&aria, algo);
  14578. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  14579. ret = wc_AriaSetKey(&aria, key);
  14580. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  14581. MC_GetObjectValue(aria.hSession, aria.hKey, key, &keySz);
  14582. printOutput("Key", key, keySz);
  14583. WC_RNG rng;
  14584. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14585. if (ret != 0)
  14586. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14587. ret = wc_AriaGcmSetIV(&aria, GCM_NONCE_MID_SZ, NULL, 0, &rng);
  14588. if (ret != 0)
  14589. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  14590. wc_FreeRng(&rng);
  14591. printOutput("Plaintext", data, sizeof(data));
  14592. XMEMSET(cipher, 0, sizeof(cipher));
  14593. ret = wc_AriaEncrypt(&aria, cipher, data, dataSz,
  14594. (byte *)aria.nonce, aria.nonceSz, ad, adSz,
  14595. authTag, authTagSz);
  14596. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  14597. printOutput("Ciphertext", cipher, sizeof(cipher));
  14598. printOutput("AuthTag", authTag, sizeof(authTag));
  14599. XMEMSET(plain, 0, sizeof(plain));
  14600. ret = wc_AriaDecrypt(&aria, plain, cipher, dataSz,
  14601. (byte *)aria.nonce, aria.nonceSz, ad, adSz,
  14602. authTag, authTagSz);
  14603. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  14604. printOutput("Plaintext", plain, sizeof(plain));
  14605. if (XMEMCMP(plain, data, dataSz) != 0)
  14606. ERROR_OUT(WC_TEST_RET_ENC_NC,out);
  14607. out:
  14608. if (ret != 0) { wc_AriaFreeCrypt(&aria); }
  14609. else { ret = wc_AriaFreeCrypt(&aria); }
  14610. return ret;
  14611. }
  14612. #endif /* HAVE_ARIA */
  14613. #ifdef HAVE_CAMELLIA
  14614. enum {
  14615. CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
  14616. };
  14617. typedef struct {
  14618. int type;
  14619. const byte* plaintext;
  14620. const byte* iv;
  14621. const byte* ciphertext;
  14622. const byte* key;
  14623. word32 keySz;
  14624. int errorCode;
  14625. } test_vector_t;
  14626. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t camellia_test(void)
  14627. {
  14628. /* Camellia ECB Test Plaintext */
  14629. WOLFSSL_SMALL_STACK_STATIC const byte pte[] =
  14630. {
  14631. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  14632. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  14633. };
  14634. /* Camellia ECB Test Initialization Vector */
  14635. WOLFSSL_SMALL_STACK_STATIC const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  14636. /* Test 1: Camellia ECB 128-bit key */
  14637. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  14638. {
  14639. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  14640. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  14641. };
  14642. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  14643. {
  14644. 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  14645. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
  14646. };
  14647. /* Test 2: Camellia ECB 192-bit key */
  14648. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  14649. {
  14650. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  14651. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  14652. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  14653. };
  14654. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  14655. {
  14656. 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  14657. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
  14658. };
  14659. /* Test 3: Camellia ECB 256-bit key */
  14660. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  14661. {
  14662. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  14663. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  14664. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14665. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  14666. };
  14667. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  14668. {
  14669. 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  14670. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
  14671. };
  14672. /* Camellia CBC Test Plaintext */
  14673. WOLFSSL_SMALL_STACK_STATIC const byte ptc[] =
  14674. {
  14675. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  14676. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  14677. };
  14678. /* Camellia CBC Test Initialization Vector */
  14679. WOLFSSL_SMALL_STACK_STATIC const byte ivc[] =
  14680. {
  14681. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14682. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  14683. };
  14684. /* Test 4: Camellia-CBC 128-bit key */
  14685. WOLFSSL_SMALL_STACK_STATIC const byte k4[] =
  14686. {
  14687. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  14688. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
  14689. };
  14690. WOLFSSL_SMALL_STACK_STATIC const byte c4[] =
  14691. {
  14692. 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  14693. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
  14694. };
  14695. /* Test 5: Camellia-CBC 192-bit key */
  14696. WOLFSSL_SMALL_STACK_STATIC const byte k5[] =
  14697. {
  14698. 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  14699. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  14700. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
  14701. };
  14702. WOLFSSL_SMALL_STACK_STATIC const byte c5[] =
  14703. {
  14704. 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  14705. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
  14706. };
  14707. /* Test 6: CBC 256-bit key */
  14708. WOLFSSL_SMALL_STACK_STATIC const byte k6[] =
  14709. {
  14710. 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  14711. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  14712. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  14713. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
  14714. };
  14715. WOLFSSL_SMALL_STACK_STATIC const byte c6[] =
  14716. {
  14717. 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  14718. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
  14719. };
  14720. byte out[CAMELLIA_BLOCK_SIZE];
  14721. Camellia cam;
  14722. WOLFSSL_SMALL_STACK_STATIC const test_vector_t testVectors[] =
  14723. {
  14724. {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
  14725. {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
  14726. {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
  14727. {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
  14728. {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
  14729. {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
  14730. {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
  14731. {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
  14732. {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
  14733. {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
  14734. {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
  14735. {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
  14736. };
  14737. int i, testsSz;
  14738. int ret;
  14739. WOLFSSL_ENTER("camellia_test");
  14740. testsSz = sizeof(testVectors)/sizeof(test_vector_t);
  14741. for (i = 0; i < testsSz; i++) {
  14742. if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
  14743. testVectors[i].iv) != 0)
  14744. return testVectors[i].errorCode;
  14745. switch (testVectors[i].type) {
  14746. case CAM_ECB_ENC:
  14747. ret = wc_CamelliaEncryptDirect(&cam, out,
  14748. testVectors[i].plaintext);
  14749. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  14750. CAMELLIA_BLOCK_SIZE))
  14751. return testVectors[i].errorCode;
  14752. break;
  14753. case CAM_ECB_DEC:
  14754. ret = wc_CamelliaDecryptDirect(&cam, out,
  14755. testVectors[i].ciphertext);
  14756. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  14757. CAMELLIA_BLOCK_SIZE))
  14758. return testVectors[i].errorCode;
  14759. break;
  14760. case CAM_CBC_ENC:
  14761. ret = wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
  14762. CAMELLIA_BLOCK_SIZE);
  14763. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  14764. CAMELLIA_BLOCK_SIZE))
  14765. return testVectors[i].errorCode;
  14766. break;
  14767. case CAM_CBC_DEC:
  14768. ret = wc_CamelliaCbcDecrypt(&cam, out,
  14769. testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE);
  14770. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  14771. CAMELLIA_BLOCK_SIZE))
  14772. return testVectors[i].errorCode;
  14773. break;
  14774. default:
  14775. break;
  14776. }
  14777. }
  14778. /* Setting the IV and checking it was actually set. */
  14779. ret = wc_CamelliaSetIV(&cam, ivc);
  14780. if (ret != 0)
  14781. return WC_TEST_RET_ENC_EC(ret);
  14782. if (XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE) != 0)
  14783. return WC_TEST_RET_ENC_NC;
  14784. /* Setting the IV to NULL should be same as all zeros IV */
  14785. ret = wc_CamelliaSetIV(&cam, NULL);
  14786. if (ret != 0)
  14787. return WC_TEST_RET_ENC_EC(ret);
  14788. if (XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE) != 0)
  14789. return WC_TEST_RET_ENC_NC;
  14790. /* First parameter should never be null */
  14791. if (wc_CamelliaSetIV(NULL, NULL) == 0)
  14792. return WC_TEST_RET_ENC_NC;
  14793. /* First parameter should never be null, check it fails */
  14794. if (wc_CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
  14795. return WC_TEST_RET_ENC_NC;
  14796. /* Key should have a size of 16, 24, or 32 */
  14797. if (wc_CamelliaSetKey(&cam, k1, 0, NULL) == 0)
  14798. return WC_TEST_RET_ENC_NC;
  14799. return 0;
  14800. }
  14801. #endif /* HAVE_CAMELLIA */
  14802. #ifdef WOLFSSL_SM4
  14803. #ifdef WOLFSSL_SM4_ECB
  14804. static int sm4_ecb_test(void)
  14805. {
  14806. /* draft-ribose-cfrg-sm4-10 A.2.1.1 */
  14807. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  14808. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  14809. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  14810. };
  14811. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  14812. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  14813. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  14814. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  14815. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  14816. };
  14817. WOLFSSL_SMALL_STACK_STATIC const byte c1_ecb[] = {
  14818. 0x5E, 0xC8, 0x14, 0x3D, 0xE5, 0x09, 0xCF, 0xF7,
  14819. 0xB5, 0x17, 0x9F, 0x8F, 0x47, 0x4B, 0x86, 0x19,
  14820. 0x2F, 0x1D, 0x30, 0x5A, 0x7F, 0xB1, 0x7D, 0xF9,
  14821. 0x85, 0xF8, 0x1C, 0x84, 0x82, 0x19, 0x23, 0x04
  14822. };
  14823. wc_Sm4 sm4;
  14824. byte enc[SM4_BLOCK_SIZE * 4];
  14825. byte dec[SM4_BLOCK_SIZE * 4];
  14826. int ret;
  14827. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  14828. if (ret != 0)
  14829. return WC_TEST_RET_ENC_EC(ret);
  14830. /* Encrypt and decrypt with ECB. */
  14831. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  14832. if (ret != 0)
  14833. return WC_TEST_RET_ENC_EC(ret);
  14834. ret = wc_Sm4EcbEncrypt(&sm4, enc, p1, sizeof(p1));
  14835. if (ret != 0)
  14836. return WC_TEST_RET_ENC_EC(ret);
  14837. if (XMEMCMP(enc, c1_ecb, sizeof(c1_ecb)) != 0)
  14838. return WC_TEST_RET_ENC_NC;
  14839. ret = wc_Sm4EcbDecrypt(&sm4, dec, enc, sizeof(c1_ecb));
  14840. if (ret != 0)
  14841. return WC_TEST_RET_ENC_EC(ret);
  14842. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  14843. return WC_TEST_RET_ENC_NC;
  14844. wc_Sm4Free(&sm4);
  14845. return 0;
  14846. }
  14847. #endif
  14848. #ifdef WOLFSSL_SM4_CBC
  14849. static int sm4_cbc_test(void)
  14850. {
  14851. /* draft-ribose-cfrg-sm4-10 A.2.2.1 */
  14852. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  14853. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  14854. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  14855. };
  14856. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  14857. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  14858. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  14859. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  14860. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  14861. };
  14862. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  14863. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14864. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  14865. };
  14866. WOLFSSL_SMALL_STACK_STATIC const byte c1_cbc[] = {
  14867. 0x78, 0xEB, 0xB1, 0x1C, 0xC4, 0x0B, 0x0A, 0x48,
  14868. 0x31, 0x2A, 0xAE, 0xB2, 0x04, 0x02, 0x44, 0xCB,
  14869. 0x4C, 0xB7, 0x01, 0x69, 0x51, 0x90, 0x92, 0x26,
  14870. 0x97, 0x9B, 0x0D, 0x15, 0xDC, 0x6A, 0x8F, 0x6D
  14871. };
  14872. wc_Sm4 sm4;
  14873. byte enc[SM4_BLOCK_SIZE * 4];
  14874. byte dec[SM4_BLOCK_SIZE * 4];
  14875. int ret;
  14876. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  14877. if (ret != 0)
  14878. return WC_TEST_RET_ENC_EC(ret);
  14879. /* Encrypt and decrypt with CBC. */
  14880. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  14881. if (ret != 0)
  14882. return WC_TEST_RET_ENC_EC(ret);
  14883. ret = wc_Sm4SetIV(&sm4, i1);
  14884. if (ret != 0)
  14885. return WC_TEST_RET_ENC_EC(ret);
  14886. ret = wc_Sm4CbcEncrypt(&sm4, enc, p1, sizeof(p1));
  14887. if (ret != 0)
  14888. return WC_TEST_RET_ENC_EC(ret);
  14889. if (XMEMCMP(enc, c1_cbc, sizeof(c1_cbc)) != 0)
  14890. return WC_TEST_RET_ENC_NC;
  14891. ret = wc_Sm4SetIV(&sm4, i1);
  14892. if (ret != 0)
  14893. return WC_TEST_RET_ENC_EC(ret);
  14894. ret = wc_Sm4CbcDecrypt(&sm4, dec, enc, sizeof(c1_cbc));
  14895. if (ret != 0)
  14896. return WC_TEST_RET_ENC_EC(ret);
  14897. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  14898. return WC_TEST_RET_ENC_NC;
  14899. /* Encrypt and decrypt in-place with CBC. */
  14900. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  14901. if (ret != 0)
  14902. return WC_TEST_RET_ENC_EC(ret);
  14903. ret = wc_Sm4SetIV(&sm4, i1);
  14904. if (ret != 0)
  14905. return WC_TEST_RET_ENC_EC(ret);
  14906. XMEMCPY(enc, p1, sizeof(p1));
  14907. ret = wc_Sm4CbcEncrypt(&sm4, enc, enc, sizeof(p1));
  14908. if (ret != 0)
  14909. return WC_TEST_RET_ENC_EC(ret);
  14910. if (XMEMCMP(enc, c1_cbc, sizeof(c1_cbc)) != 0)
  14911. return WC_TEST_RET_ENC_NC;
  14912. ret = wc_Sm4SetIV(&sm4, i1);
  14913. if (ret != 0)
  14914. return WC_TEST_RET_ENC_EC(ret);
  14915. ret = wc_Sm4CbcDecrypt(&sm4, enc, enc, sizeof(c1_cbc));
  14916. if (ret != 0)
  14917. return WC_TEST_RET_ENC_EC(ret);
  14918. if (XMEMCMP(enc, p1, sizeof(p1)) != 0)
  14919. return WC_TEST_RET_ENC_NC;
  14920. wc_Sm4Free(&sm4);
  14921. return 0;
  14922. }
  14923. #endif
  14924. #ifdef WOLFSSL_SM4_CTR
  14925. static int sm4_ctr_test(void)
  14926. {
  14927. /* draft-ribose-cfrg-sm4-10 A.2.5.1 */
  14928. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  14929. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  14930. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  14931. };
  14932. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  14933. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14934. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  14935. };
  14936. WOLFSSL_SMALL_STACK_STATIC const byte p2[] = {
  14937. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  14938. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  14939. 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
  14940. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  14941. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  14942. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  14943. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  14944. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB
  14945. };
  14946. WOLFSSL_SMALL_STACK_STATIC const byte c2_ctr[] = {
  14947. 0xAC, 0x32, 0x36, 0xCB, 0x97, 0x0C, 0xC2, 0x07,
  14948. 0x91, 0x36, 0x4C, 0x39, 0x5A, 0x13, 0x42, 0xD1,
  14949. 0xA3, 0xCB, 0xC1, 0x87, 0x8C, 0x6F, 0x30, 0xCD,
  14950. 0x07, 0x4C, 0xCE, 0x38, 0x5C, 0xDD, 0x70, 0xC7,
  14951. 0xF2, 0x34, 0xBC, 0x0E, 0x24, 0xC1, 0x19, 0x80,
  14952. 0xFD, 0x12, 0x86, 0x31, 0x0C, 0xE3, 0x7B, 0x92,
  14953. 0x6E, 0x02, 0xFC, 0xD0, 0xFA, 0xA0, 0xBA, 0xF3,
  14954. 0x8B, 0x29, 0x33, 0x85, 0x1D, 0x82, 0x45, 0x14
  14955. };
  14956. wc_Sm4 sm4;
  14957. byte enc[SM4_BLOCK_SIZE * 4];
  14958. byte dec[SM4_BLOCK_SIZE * 4];
  14959. int chunk;
  14960. int i;
  14961. int ret;
  14962. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  14963. if (ret != 0)
  14964. return WC_TEST_RET_ENC_EC(ret);
  14965. /* Encrypt and decrypt using encrypt with CTR. */
  14966. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  14967. if (ret != 0)
  14968. return WC_TEST_RET_ENC_EC(ret);
  14969. ret = wc_Sm4SetIV(&sm4, i1);
  14970. if (ret != 0)
  14971. return WC_TEST_RET_ENC_EC(ret);
  14972. ret = wc_Sm4CtrEncrypt(&sm4, enc, p2, sizeof(p2));
  14973. if (ret != 0)
  14974. return WC_TEST_RET_ENC_EC(ret);
  14975. if (XMEMCMP(enc, c2_ctr, sizeof(c2_ctr)) != 0)
  14976. return WC_TEST_RET_ENC_NC;
  14977. ret = wc_Sm4SetIV(&sm4, i1);
  14978. if (ret != 0)
  14979. return WC_TEST_RET_ENC_EC(ret);
  14980. ret = wc_Sm4CtrEncrypt(&sm4, dec, enc, sizeof(c2_ctr));
  14981. if (ret != 0)
  14982. return WC_TEST_RET_ENC_EC(ret);
  14983. if (XMEMCMP(dec, p2, sizeof(p2)) != 0)
  14984. return WC_TEST_RET_ENC_NC;
  14985. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  14986. ret = wc_Sm4SetIV(&sm4, i1);
  14987. if (ret != 0)
  14988. return WC_TEST_RET_ENC_I(chunk);
  14989. XMEMSET(enc, 0, sizeof(enc));
  14990. for (i = 0; i + chunk <= (int)sizeof(p2); i += chunk) {
  14991. ret = wc_Sm4CtrEncrypt(&sm4, enc + i, p2 + i, chunk);
  14992. if (ret != 0)
  14993. return WC_TEST_RET_ENC_I(i);
  14994. }
  14995. if (i < (int)sizeof(p2)) {
  14996. ret = wc_Sm4CtrEncrypt(&sm4, enc + i, p2 + i, sizeof(p2) - i);
  14997. if (ret != 0)
  14998. return WC_TEST_RET_ENC_I(chunk);
  14999. }
  15000. if (XMEMCMP(enc, c2_ctr, sizeof(c2_ctr)) != 0)
  15001. return WC_TEST_RET_ENC_I(chunk);
  15002. }
  15003. wc_Sm4Free(&sm4);
  15004. return 0;
  15005. }
  15006. #endif
  15007. #ifdef WOLFSSL_SM4_GCM
  15008. static int sm4_gcm_test(void)
  15009. {
  15010. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  15011. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  15012. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  15013. };
  15014. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  15015. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  15016. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  15017. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  15018. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  15019. };
  15020. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  15021. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15022. 0x08, 0x09, 0x0A, 0x0B
  15023. };
  15024. WOLFSSL_SMALL_STACK_STATIC const byte a1[] = {
  15025. 0xFF, 0xEE, 0xDD
  15026. };
  15027. WOLFSSL_SMALL_STACK_STATIC const byte tag1[] = {
  15028. 0x83, 0xb2, 0x91, 0xcf, 0x22, 0xc9, 0x5f, 0x89,
  15029. 0xde, 0x3d, 0x52, 0x8d, 0xd7, 0x13, 0x50, 0x89
  15030. };
  15031. WOLFSSL_SMALL_STACK_STATIC const byte c1[] = {
  15032. 0xff, 0x8b, 0xb2, 0x3b, 0x0a, 0x0a, 0x12, 0xa4,
  15033. 0xa8, 0x4c, 0x4f, 0x67, 0x06, 0x81, 0xbb, 0x88,
  15034. 0x66, 0x17, 0xc7, 0x43, 0xbf, 0xae, 0x41, 0x40,
  15035. 0xec, 0x1e, 0x03, 0x85, 0x2b, 0x56, 0xa8, 0xc0
  15036. };
  15037. /* RFC8998 A.1. */
  15038. WOLFSSL_SMALL_STACK_STATIC const byte i2[] = {
  15039. 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, 0x00,
  15040. 0x00, 0x00, 0xAB, 0xCD
  15041. };
  15042. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  15043. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  15044. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  15045. };
  15046. WOLFSSL_SMALL_STACK_STATIC const byte p2[] = {
  15047. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  15048. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  15049. 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
  15050. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  15051. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  15052. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  15053. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  15054. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA
  15055. };
  15056. WOLFSSL_SMALL_STACK_STATIC const byte a2[] = {
  15057. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  15058. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  15059. 0xAB, 0xAD, 0xDA, 0xD2
  15060. };
  15061. WOLFSSL_SMALL_STACK_STATIC const byte c2[] = {
  15062. 0x17, 0xF3, 0x99, 0xF0, 0x8C, 0x67, 0xD5, 0xEE,
  15063. 0x19, 0xD0, 0xDC, 0x99, 0x69, 0xC4, 0xBB, 0x7D,
  15064. 0x5F, 0xD4, 0x6F, 0xD3, 0x75, 0x64, 0x89, 0x06,
  15065. 0x91, 0x57, 0xB2, 0x82, 0xBB, 0x20, 0x07, 0x35,
  15066. 0xD8, 0x27, 0x10, 0xCA, 0x5C, 0x22, 0xF0, 0xCC,
  15067. 0xFA, 0x7C, 0xBF, 0x93, 0xD4, 0x96, 0xAC, 0x15,
  15068. 0xA5, 0x68, 0x34, 0xCB, 0xCF, 0x98, 0xC3, 0x97,
  15069. 0xB4, 0x02, 0x4A, 0x26, 0x91, 0x23, 0x3B, 0x8D
  15070. };
  15071. WOLFSSL_SMALL_STACK_STATIC const byte tag2[] = {
  15072. 0x83, 0xDE, 0x35, 0x41, 0xE4, 0xC2, 0xB5, 0x81,
  15073. 0x77, 0xE0, 0x65, 0xA9, 0xBF, 0x7B, 0x62, 0xEC
  15074. };
  15075. wc_Sm4 sm4;
  15076. byte enc[SM4_BLOCK_SIZE * 4];
  15077. byte dec[SM4_BLOCK_SIZE * 4];
  15078. byte tag[SM4_BLOCK_SIZE];
  15079. int ret;
  15080. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  15081. if (ret != 0)
  15082. return WC_TEST_RET_ENC_EC(ret);
  15083. /* Encrypt and decrypt using encrypt with GCM. */
  15084. ret = wc_Sm4GcmSetKey(&sm4, k1, sizeof(k1));
  15085. if (ret != 0)
  15086. return WC_TEST_RET_ENC_EC(ret);
  15087. ret = wc_Sm4GcmEncrypt(&sm4, enc, p1, sizeof(p1), i1, sizeof(i1), tag,
  15088. sizeof(tag), a1, sizeof(a1));
  15089. if (ret != 0)
  15090. return WC_TEST_RET_ENC_EC(ret);
  15091. if (XMEMCMP(enc, c1, sizeof(c1)) != 0)
  15092. return WC_TEST_RET_ENC_NC;
  15093. if (XMEMCMP(tag, tag1, sizeof(tag1)) != 0)
  15094. return WC_TEST_RET_ENC_NC;
  15095. ret = wc_Sm4GcmDecrypt(&sm4, dec, enc, sizeof(c1), i1, sizeof(i1), tag,
  15096. sizeof(tag), a1, sizeof(a1));
  15097. if (ret != 0)
  15098. return WC_TEST_RET_ENC_EC(ret);
  15099. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  15100. return WC_TEST_RET_ENC_NC;
  15101. /* RFC8998 test vector. */
  15102. ret = wc_Sm4GcmSetKey(&sm4, k2, sizeof(k2));
  15103. if (ret != 0)
  15104. return WC_TEST_RET_ENC_EC(ret);
  15105. ret = wc_Sm4GcmEncrypt(&sm4, enc, p2, sizeof(p2), i2, sizeof(i2), tag,
  15106. sizeof(tag), a2, sizeof(a2));
  15107. if (ret != 0)
  15108. return WC_TEST_RET_ENC_EC(ret);
  15109. if (XMEMCMP(enc, c2, sizeof(c2)) != 0)
  15110. return WC_TEST_RET_ENC_NC;
  15111. if (XMEMCMP(tag, tag2, sizeof(tag2)) != 0)
  15112. return WC_TEST_RET_ENC_NC;
  15113. ret = wc_Sm4GcmDecrypt(&sm4, dec, enc, sizeof(c2), i2, sizeof(i2), tag,
  15114. sizeof(tag), a2, sizeof(a2));
  15115. if (ret != 0)
  15116. return WC_TEST_RET_ENC_EC(ret);
  15117. if (XMEMCMP(dec, p2, sizeof(p2)) != 0)
  15118. return WC_TEST_RET_ENC_NC;
  15119. wc_Sm4Free(&sm4);
  15120. return 0;
  15121. }
  15122. #endif
  15123. #ifdef WOLFSSL_SM4_CCM
  15124. static int sm4_ccm_test(void)
  15125. {
  15126. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  15127. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  15128. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  15129. };
  15130. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  15131. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  15132. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  15133. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  15134. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  15135. };
  15136. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  15137. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15138. 0x08, 0x09, 0x0A, 0x0B
  15139. };
  15140. WOLFSSL_SMALL_STACK_STATIC const byte a1[] = {
  15141. 0xFF, 0xEE, 0xDD
  15142. };
  15143. WOLFSSL_SMALL_STACK_STATIC const byte tag1[] = {
  15144. 0x9a, 0x98, 0x04, 0xb6, 0x0f, 0x19, 0x4a, 0x46,
  15145. 0xba, 0xed, 0xe6, 0x89, 0x69, 0x34, 0xad, 0x61
  15146. };
  15147. WOLFSSL_SMALL_STACK_STATIC const byte c1[] = {
  15148. 0xbd, 0xc0, 0x72, 0x60, 0xda, 0x2d, 0x11, 0xdc,
  15149. 0x66, 0x33, 0xcc, 0xec, 0xb2, 0xf4, 0x53, 0x59,
  15150. 0x9e, 0xb1, 0xb3, 0x6b, 0x1f, 0x1c, 0xfb, 0x29,
  15151. 0xf5, 0x37, 0xfc, 0x00, 0xf2, 0x4e, 0x70, 0x6f
  15152. };
  15153. /* RFC8998 A.1. */
  15154. WOLFSSL_SMALL_STACK_STATIC const byte i2[] = {
  15155. 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, 0x00,
  15156. 0x00, 0x00, 0xAB, 0xCD
  15157. };
  15158. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  15159. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  15160. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  15161. };
  15162. WOLFSSL_SMALL_STACK_STATIC const byte p2[] = {
  15163. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  15164. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  15165. 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
  15166. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  15167. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  15168. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  15169. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  15170. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA
  15171. };
  15172. WOLFSSL_SMALL_STACK_STATIC const byte a2[] = {
  15173. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  15174. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  15175. 0xAB, 0xAD, 0xDA, 0xD2
  15176. };
  15177. WOLFSSL_SMALL_STACK_STATIC const byte c2[] = {
  15178. 0x48, 0xAF, 0x93, 0x50, 0x1F, 0xA6, 0x2A, 0xDB,
  15179. 0xCD, 0x41, 0x4C, 0xCE, 0x60, 0x34, 0xD8, 0x95,
  15180. 0xDD, 0xA1, 0xBF, 0x8F, 0x13, 0x2F, 0x04, 0x20,
  15181. 0x98, 0x66, 0x15, 0x72, 0xE7, 0x48, 0x30, 0x94,
  15182. 0xFD, 0x12, 0xE5, 0x18, 0xCE, 0x06, 0x2C, 0x98,
  15183. 0xAC, 0xEE, 0x28, 0xD9, 0x5D, 0xF4, 0x41, 0x6B,
  15184. 0xED, 0x31, 0xA2, 0xF0, 0x44, 0x76, 0xC1, 0x8B,
  15185. 0xB4, 0x0C, 0x84, 0xA7, 0x4B, 0x97, 0xDC, 0x5B
  15186. };
  15187. WOLFSSL_SMALL_STACK_STATIC const byte tag2[] = {
  15188. 0x16, 0x84, 0x2D, 0x4F, 0xA1, 0x86, 0xF5, 0x6A,
  15189. 0xB3, 0x32, 0x56, 0x97, 0x1F, 0xA1, 0x10, 0xF4
  15190. };
  15191. wc_Sm4 sm4;
  15192. byte enc[SM4_BLOCK_SIZE * 4];
  15193. byte dec[SM4_BLOCK_SIZE * 4];
  15194. byte tag[SM4_BLOCK_SIZE];
  15195. int ret;
  15196. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  15197. if (ret != 0)
  15198. return WC_TEST_RET_ENC_EC(ret);
  15199. /* Encrypt and decrypt using encrypt with CCM. */
  15200. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  15201. if (ret != 0)
  15202. return WC_TEST_RET_ENC_EC(ret);
  15203. ret = wc_Sm4CcmEncrypt(&sm4, enc, p1, sizeof(p1), i1, sizeof(i1), tag,
  15204. sizeof(tag), a1, sizeof(a1));
  15205. if (ret != 0)
  15206. return WC_TEST_RET_ENC_EC(ret);
  15207. if (XMEMCMP(enc, c1, sizeof(c1)) != 0)
  15208. return WC_TEST_RET_ENC_NC;
  15209. if (XMEMCMP(tag, tag1, sizeof(tag1)) != 0)
  15210. return WC_TEST_RET_ENC_NC;
  15211. ret = wc_Sm4CcmDecrypt(&sm4, dec, enc, sizeof(c1), i1, sizeof(i1), tag,
  15212. sizeof(tag), a1, sizeof(a1));
  15213. if (ret != 0)
  15214. return WC_TEST_RET_ENC_EC(ret);
  15215. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  15216. return WC_TEST_RET_ENC_NC;
  15217. /* RFC8998 test vector. */
  15218. ret = wc_Sm4SetKey(&sm4, k2, sizeof(k2));
  15219. if (ret != 0)
  15220. return WC_TEST_RET_ENC_EC(ret);
  15221. ret = wc_Sm4CcmEncrypt(&sm4, enc, p2, sizeof(p2), i2, sizeof(i2), tag,
  15222. sizeof(tag), a2, sizeof(a2));
  15223. if (ret != 0)
  15224. return WC_TEST_RET_ENC_EC(ret);
  15225. if (XMEMCMP(enc, c2, sizeof(c2)) != 0)
  15226. return WC_TEST_RET_ENC_NC;
  15227. if (XMEMCMP(tag, tag2, sizeof(tag2)) != 0)
  15228. return WC_TEST_RET_ENC_NC;
  15229. ret = wc_Sm4CcmDecrypt(&sm4, dec, enc, sizeof(c2), i2, sizeof(i2), tag,
  15230. sizeof(tag), a2, sizeof(a2));
  15231. if (ret != 0)
  15232. return WC_TEST_RET_ENC_EC(ret);
  15233. if (XMEMCMP(dec, p2, sizeof(p2)) != 0)
  15234. return WC_TEST_RET_ENC_NC;
  15235. wc_Sm4Free(&sm4);
  15236. return 0;
  15237. }
  15238. #endif
  15239. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm4_test(void)
  15240. {
  15241. wc_test_ret_t ret;
  15242. WOLFSSL_ENTER("sm4_test");
  15243. #ifdef WOLFSSL_SM4_ECB
  15244. ret = sm4_ecb_test();
  15245. if (ret != 0)
  15246. return ret;
  15247. #endif
  15248. #ifdef WOLFSSL_SM4_CBC
  15249. ret = sm4_cbc_test();
  15250. if (ret != 0)
  15251. return ret;
  15252. #endif
  15253. #ifdef WOLFSSL_SM4_CTR
  15254. ret = sm4_ctr_test();
  15255. if (ret != 0)
  15256. return ret;
  15257. #endif
  15258. #ifdef WOLFSSL_SM4_GCM
  15259. ret = sm4_gcm_test();
  15260. if (ret != 0)
  15261. return ret;
  15262. #endif
  15263. #ifdef WOLFSSL_SM4_CCM
  15264. ret = sm4_ccm_test();
  15265. if (ret != 0)
  15266. return ret;
  15267. #endif
  15268. return 0;
  15269. }
  15270. #endif
  15271. #ifdef HAVE_XCHACHA
  15272. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha_test(void) {
  15273. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  15274. 0x54, 0x68, 0x65, 0x20, 0x64, 0x68, 0x6f, 0x6c, 0x65, 0x20, 0x28, 0x70, 0x72, 0x6f, 0x6e, 0x6f, /* The dhole (prono */
  15275. 0x75, 0x6e, 0x63, 0x65, 0x64, 0x20, 0x22, 0x64, 0x6f, 0x6c, 0x65, 0x22, 0x29, 0x20, 0x69, 0x73, /* unced "dole") is */
  15276. 0x20, 0x61, 0x6c, 0x73, 0x6f, 0x20, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x20, 0x61, 0x73, 0x20, 0x74, /* also known as t */
  15277. 0x68, 0x65, 0x20, 0x41, 0x73, 0x69, 0x61, 0x74, 0x69, 0x63, 0x20, 0x77, 0x69, 0x6c, 0x64, 0x20, /* he Asiatic wild */
  15278. 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x72, 0x65, 0x64, 0x20, 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x61, 0x6e, /* dog, red dog, an */
  15279. 0x64, 0x20, 0x77, 0x68, 0x69, 0x73, 0x74, 0x6c, 0x69, 0x6e, 0x67, 0x20, 0x64, 0x6f, 0x67, 0x2e, /* d whistling dog. */
  15280. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x62, 0x6f, 0x75, 0x74, 0x20, 0x74, 0x68, 0x65, /* It is about the */
  15281. 0x20, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x61, 0x20, 0x47, 0x65, 0x72, 0x6d, 0x61, /* size of a Germa */
  15282. 0x6e, 0x20, 0x73, 0x68, 0x65, 0x70, 0x68, 0x65, 0x72, 0x64, 0x20, 0x62, 0x75, 0x74, 0x20, 0x6c, /* n shepherd but l */
  15283. 0x6f, 0x6f, 0x6b, 0x73, 0x20, 0x6d, 0x6f, 0x72, 0x65, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x61, /* ooks more like a */
  15284. 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x2d, 0x6c, 0x65, 0x67, 0x67, 0x65, 0x64, 0x20, 0x66, 0x6f, 0x78, /* long-legged fox */
  15285. 0x2e, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x68, 0x69, 0x67, 0x68, 0x6c, 0x79, 0x20, 0x65, 0x6c, /* . This highly el */
  15286. 0x75, 0x73, 0x69, 0x76, 0x65, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x6b, 0x69, 0x6c, 0x6c, 0x65, /* usive and skille */
  15287. 0x64, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x72, 0x20, 0x69, 0x73, 0x20, 0x63, 0x6c, 0x61, 0x73, /* d jumper is clas */
  15288. 0x73, 0x69, 0x66, 0x69, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x77, 0x6f, 0x6c, 0x76, /* sified with wolv */
  15289. 0x65, 0x73, 0x2c, 0x20, 0x63, 0x6f, 0x79, 0x6f, 0x74, 0x65, 0x73, 0x2c, 0x20, 0x6a, 0x61, 0x63, /* es, coyotes, jac */
  15290. 0x6b, 0x61, 0x6c, 0x73, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x66, 0x6f, 0x78, 0x65, 0x73, 0x20, /* kals, and foxes */
  15291. 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 0x61, 0x78, 0x6f, 0x6e, 0x6f, 0x6d, 0x69, 0x63, /* in the taxonomic */
  15292. 0x20, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x20, 0x43, 0x61, 0x6e, 0x69, 0x64, 0x61, 0x65, 0x2e /* family Canidae. */
  15293. };
  15294. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  15295. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  15296. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  15297. };
  15298. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  15299. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  15300. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x58 }; /* PQRSTUVW */
  15301. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  15302. 0x45, 0x59, 0xab, 0xba, 0x4e, 0x48, 0xc1, 0x61, 0x02, 0xe8, 0xbb, 0x2c, 0x05, 0xe6, 0x94, 0x7f,
  15303. 0x50, 0xa7, 0x86, 0xde, 0x16, 0x2f, 0x9b, 0x0b, 0x7e, 0x59, 0x2a, 0x9b, 0x53, 0xd0, 0xd4, 0xe9,
  15304. 0x8d, 0x8d, 0x64, 0x10, 0xd5, 0x40, 0xa1, 0xa6, 0x37, 0x5b, 0x26, 0xd8, 0x0d, 0xac, 0xe4, 0xfa,
  15305. 0xb5, 0x23, 0x84, 0xc7, 0x31, 0xac, 0xbf, 0x16, 0xa5, 0x92, 0x3c, 0x0c, 0x48, 0xd3, 0x57, 0x5d,
  15306. 0x4d, 0x0d, 0x2c, 0x67, 0x3b, 0x66, 0x6f, 0xaa, 0x73, 0x10, 0x61, 0x27, 0x77, 0x01, 0x09, 0x3a,
  15307. 0x6b, 0xf7, 0xa1, 0x58, 0xa8, 0x86, 0x42, 0x92, 0xa4, 0x1c, 0x48, 0xe3, 0xa9, 0xb4, 0xc0, 0xda,
  15308. 0xec, 0xe0, 0xf8, 0xd9, 0x8d, 0x0d, 0x7e, 0x05, 0xb3, 0x7a, 0x30, 0x7b, 0xbb, 0x66, 0x33, 0x31,
  15309. 0x64, 0xec, 0x9e, 0x1b, 0x24, 0xea, 0x0d, 0x6c, 0x3f, 0xfd, 0xdc, 0xec, 0x4f, 0x68, 0xe7, 0x44,
  15310. 0x30, 0x56, 0x19, 0x3a, 0x03, 0xc8, 0x10, 0xe1, 0x13, 0x44, 0xca, 0x06, 0xd8, 0xed, 0x8a, 0x2b,
  15311. 0xfb, 0x1e, 0x8d, 0x48, 0xcf, 0xa6, 0xbc, 0x0e, 0xb4, 0xe2, 0x46, 0x4b, 0x74, 0x81, 0x42, 0x40,
  15312. 0x7c, 0x9f, 0x43, 0x1a, 0xee, 0x76, 0x99, 0x60, 0xe1, 0x5b, 0xa8, 0xb9, 0x68, 0x90, 0x46, 0x6e,
  15313. 0xf2, 0x45, 0x75, 0x99, 0x85, 0x23, 0x85, 0xc6, 0x61, 0xf7, 0x52, 0xce, 0x20, 0xf9, 0xda, 0x0c,
  15314. 0x09, 0xab, 0x6b, 0x19, 0xdf, 0x74, 0xe7, 0x6a, 0x95, 0x96, 0x74, 0x46, 0xf8, 0xd0, 0xfd, 0x41,
  15315. 0x5e, 0x7b, 0xee, 0x2a, 0x12, 0xa1, 0x14, 0xc2, 0x0e, 0xb5, 0x29, 0x2a, 0xe7, 0xa3, 0x49, 0xae,
  15316. 0x57, 0x78, 0x20, 0xd5, 0x52, 0x0a, 0x1f, 0x3f, 0xb6, 0x2a, 0x17, 0xce, 0x6a, 0x7e, 0x68, 0xfa,
  15317. 0x7c, 0x79, 0x11, 0x1d, 0x88, 0x60, 0x92, 0x0b, 0xc0, 0x48, 0xef, 0x43, 0xfe, 0x84, 0x48, 0x6c,
  15318. 0xcb, 0x87, 0xc2, 0x5f, 0x0a, 0xe0, 0x45, 0xf0, 0xcc, 0xe1, 0xe7, 0x98, 0x9a, 0x9a, 0xa2, 0x20,
  15319. 0xa2, 0x8b, 0xdd, 0x48, 0x27, 0xe7, 0x51, 0xa2, 0x4a, 0x6d, 0x5c, 0x62, 0xd7, 0x90, 0xa6, 0x63,
  15320. 0x93, 0xb9, 0x31, 0x11, 0xc1, 0xa5, 0x5d, 0xd7, 0x42, 0x1a, 0x10, 0x18, 0x49, 0x74, 0xc7, 0xc5
  15321. };
  15322. wc_test_ret_t ret;
  15323. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15324. struct ChaCha *chacha = (struct ChaCha *)XMALLOC(sizeof *chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  15325. byte *buf1 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15326. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15327. WOLFSSL_ENTER("XChaCha_test");
  15328. if ((chacha == NULL) || (buf1 == NULL) || (buf2 == NULL))
  15329. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15330. #else
  15331. struct ChaCha chacha[1];
  15332. byte buf1[sizeof Plaintext];
  15333. byte buf2[sizeof Plaintext];
  15334. WOLFSSL_ENTER("XChaCha_test");
  15335. #endif
  15336. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  15337. if (ret < 0)
  15338. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15339. ret = wc_Chacha_Process(chacha, buf1, Plaintext, sizeof Plaintext);
  15340. if (ret < 0)
  15341. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15342. if (XMEMCMP(buf1, Ciphertext, sizeof Plaintext))
  15343. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15344. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  15345. if (ret < 0)
  15346. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15347. ret = wc_Chacha_Process(chacha, buf2, buf1, sizeof Plaintext);
  15348. if (ret < 0)
  15349. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15350. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  15351. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15352. out:
  15353. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15354. if (chacha)
  15355. XFREE(chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  15356. if (buf1)
  15357. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15358. if (buf2)
  15359. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15360. #endif
  15361. return ret;
  15362. }
  15363. #endif /* HAVE_XCHACHA */
  15364. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  15365. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha20Poly1305_test(void) {
  15366. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  15367. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c, /* Ladies and Gentl */
  15368. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73, /* emen of the clas */
  15369. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63, /* s of '99: If I c */
  15370. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f, /* ould offer you o */
  15371. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20, /* nly one tip for */
  15372. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73, /* the future, suns */
  15373. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69, /* creen would be i */
  15374. 0x74, 0x2e }; /* t. */
  15375. WOLFSSL_SMALL_STACK_STATIC const byte AAD[] = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 }; /* PQRS........ */
  15376. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  15377. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  15378. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  15379. };
  15380. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  15381. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  15382. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 }; /* PQRSTUVW */
  15383. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  15384. 0xbd, 0x6d, 0x17, 0x9d, 0x3e, 0x83, 0xd4, 0x3b, 0x95, 0x76, 0x57, 0x94, 0x93, 0xc0, 0xe9, 0x39,
  15385. 0x57, 0x2a, 0x17, 0x00, 0x25, 0x2b, 0xfa, 0xcc, 0xbe, 0xd2, 0x90, 0x2c, 0x21, 0x39, 0x6c, 0xbb,
  15386. 0x73, 0x1c, 0x7f, 0x1b, 0x0b, 0x4a, 0xa6, 0x44, 0x0b, 0xf3, 0xa8, 0x2f, 0x4e, 0xda, 0x7e, 0x39,
  15387. 0xae, 0x64, 0xc6, 0x70, 0x8c, 0x54, 0xc2, 0x16, 0xcb, 0x96, 0xb7, 0x2e, 0x12, 0x13, 0xb4, 0x52,
  15388. 0x2f, 0x8c, 0x9b, 0xa4, 0x0d, 0xb5, 0xd9, 0x45, 0xb1, 0x1b, 0x69, 0xb9, 0x82, 0xc1, 0xbb, 0x9e,
  15389. 0x3f, 0x3f, 0xac, 0x2b, 0xc3, 0x69, 0x48, 0x8f, 0x76, 0xb2, 0x38, 0x35, 0x65, 0xd3, 0xff, 0xf9,
  15390. 0x21, 0xf9, 0x66, 0x4c, 0x97, 0x63, 0x7d, 0xa9, 0x76, 0x88, 0x12, 0xf6, 0x15, 0xc6, 0x8b, 0x13,
  15391. 0xb5, 0x2e };
  15392. WOLFSSL_SMALL_STACK_STATIC const byte Tag[] = {
  15393. 0xc0, 0x87, 0x59, 0x24, 0xc1, 0xc7, 0x98, 0x79, 0x47, 0xde, 0xaf, 0xd8, 0x78, 0x0a, 0xcf, 0x49
  15394. };
  15395. wc_test_ret_t ret;
  15396. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15397. byte *buf1 = (byte *)XMALLOC(sizeof Ciphertext + sizeof Tag, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15398. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15399. WOLFSSL_ENTER("XChaCha20Poly1305_test");
  15400. if ((buf1 == NULL) || (buf2 == NULL))
  15401. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15402. #else
  15403. byte buf1[sizeof Ciphertext + sizeof Tag];
  15404. byte buf2[sizeof Plaintext];
  15405. WOLFSSL_ENTER("XChaCha20Poly1305_test");
  15406. #endif
  15407. ret = wc_XChaCha20Poly1305_Encrypt(buf1, sizeof Ciphertext + sizeof Tag,
  15408. Plaintext, sizeof Plaintext,
  15409. AAD, sizeof AAD,
  15410. IV, sizeof IV,
  15411. Key, sizeof Key);
  15412. if (ret < 0)
  15413. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15414. if (XMEMCMP(buf1, Ciphertext, sizeof Ciphertext))
  15415. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15416. if (XMEMCMP(buf1 + sizeof Ciphertext, Tag, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE))
  15417. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15418. ret = wc_XChaCha20Poly1305_Decrypt(buf2, sizeof Plaintext,
  15419. buf1, sizeof Ciphertext + sizeof Tag,
  15420. AAD, sizeof AAD,
  15421. IV, sizeof IV,
  15422. Key, sizeof Key);
  15423. if (ret < 0)
  15424. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15425. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  15426. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15427. out:
  15428. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15429. if (buf1 != NULL)
  15430. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15431. if (buf2 != NULL)
  15432. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15433. #endif
  15434. return ret;
  15435. }
  15436. #endif /* defined(HAVE_XCHACHA) && defined(HAVE_POLY1305) */
  15437. #ifndef WC_NO_RNG
  15438. static wc_test_ret_t _rng_test(WC_RNG* rng, int errorOffset)
  15439. {
  15440. byte block[32];
  15441. wc_test_ret_t ret;
  15442. int i;
  15443. XMEMSET(block, 0, sizeof(block));
  15444. ret = wc_RNG_GenerateBlock(rng, block, sizeof(block));
  15445. if (ret != 0) {
  15446. ret = 1;
  15447. goto exit;
  15448. }
  15449. /* Check for 0's */
  15450. for (i=0; i<(int)sizeof(block); i++) {
  15451. if (block[i] == 0) {
  15452. ret++;
  15453. }
  15454. }
  15455. /* All zeros count check */
  15456. if (ret >= (int)sizeof(block)) {
  15457. ret = 2;
  15458. goto exit;
  15459. }
  15460. ret = wc_RNG_GenerateByte(rng, block);
  15461. if (ret != 0) {
  15462. ret = 3;
  15463. goto exit;
  15464. }
  15465. /* Parameter validation testing. */
  15466. ret = wc_RNG_GenerateBlock(NULL, block, sizeof(block));
  15467. if (ret != BAD_FUNC_ARG) {
  15468. ret = 4;
  15469. goto exit;
  15470. }
  15471. ret = wc_RNG_GenerateBlock(rng, NULL, sizeof(block));
  15472. if (ret != BAD_FUNC_ARG) {
  15473. ret = 5;
  15474. goto exit;
  15475. }
  15476. ret = wc_RNG_GenerateByte(NULL, block);
  15477. if (ret != BAD_FUNC_ARG) {
  15478. ret = 6;
  15479. goto exit;
  15480. }
  15481. ret = wc_RNG_GenerateByte(rng, NULL);
  15482. if (ret != BAD_FUNC_ARG) {
  15483. ret = 7;
  15484. goto exit;
  15485. }
  15486. ret = 0;
  15487. exit:
  15488. if (ret != 0)
  15489. ret = errorOffset - (ret * 1000000);
  15490. return ret;
  15491. }
  15492. static wc_test_ret_t random_rng_test(void)
  15493. {
  15494. WC_RNG localRng;
  15495. WC_RNG* rng;
  15496. wc_test_ret_t ret;
  15497. rng = &localRng;
  15498. /* Test stack based RNG. */
  15499. #ifndef HAVE_FIPS
  15500. ret = wc_InitRng_ex(rng, HEAP_HINT, devId);
  15501. #else
  15502. ret = wc_InitRng(rng);
  15503. #endif
  15504. if (ret != 0)
  15505. return WC_TEST_RET_ENC_EC(ret);
  15506. ret = _rng_test(rng, WC_TEST_RET_ENC_NC);
  15507. /* Make sure and free RNG */
  15508. wc_FreeRng(rng);
  15509. if (ret != 0)
  15510. return ret;
  15511. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  15512. {
  15513. byte nonce[8] = { 0 };
  15514. /* Test dynamic RNG */
  15515. rng = wc_rng_new(nonce, (word32)sizeof(nonce), HEAP_HINT);
  15516. if (rng == NULL)
  15517. return WC_TEST_RET_ENC_ERRNO;
  15518. ret = _rng_test(rng, WC_TEST_RET_ENC_NC);
  15519. wc_rng_free(rng);
  15520. rng = NULL;
  15521. if (ret != 0)
  15522. return ret;
  15523. /* Test dynamic RNG using extended API */
  15524. ret = wc_rng_new_ex(&rng, nonce, (word32)sizeof(nonce),
  15525. HEAP_HINT, devId);
  15526. if (ret != 0)
  15527. return WC_TEST_RET_ENC_EC(ret);
  15528. ret = _rng_test(rng, WC_TEST_RET_ENC_NC);
  15529. wc_rng_free(rng);
  15530. if (ret != 0)
  15531. return ret;
  15532. }
  15533. #endif
  15534. return ret;
  15535. }
  15536. #if defined(HAVE_HASHDRBG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  15537. #ifdef WC_RNG_SEED_CB
  15538. static int seed_cb(OS_Seed* os, byte* output, word32 sz)
  15539. {
  15540. word32 i;
  15541. (void)os;
  15542. /* Known answer test. Set the seed to the same value every time. */
  15543. for (i = 0; i < sz; i++)
  15544. output[i] = (byte)i;
  15545. return 0;
  15546. }
  15547. static wc_test_ret_t rng_seed_test(void)
  15548. {
  15549. #ifndef HAVE_FIPS
  15550. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  15551. {
  15552. 0x83, 0x46, 0x65, 0x2f, 0x5c, 0x44, 0x16, 0x5f,
  15553. 0xb3, 0x89, 0x26, 0xde, 0x0b, 0x6b, 0xa2, 0x06,
  15554. 0x7e, 0xa7, 0x9a, 0x55, 0x22, 0x01, 0xb0, 0x22,
  15555. 0xf4, 0x7e, 0xa2, 0x66, 0xc4, 0x08, 0x6f, 0xba
  15556. };
  15557. #else
  15558. /* FIPS uses a longer seed, so different check value. */
  15559. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  15560. {
  15561. 0xaf, 0x31, 0xcc, 0xef, 0xa9, 0x29, 0x4c, 0x24,
  15562. 0xbd, 0xa5, 0xa3, 0x52, 0x69, 0xf3, 0xb9, 0xb2,
  15563. 0x1e, 0xd4, 0x52, 0x3b, 0x9a, 0x96, 0x06, 0x20,
  15564. 0xc0, 0x5f, 0x44, 0x06, 0x1f, 0x80, 0xdf, 0xe0
  15565. };
  15566. #endif
  15567. byte output[WC_SHA256_DIGEST_SIZE];
  15568. WC_RNG rng;
  15569. wc_test_ret_t ret;
  15570. ret = wc_SetSeed_Cb(seed_cb);
  15571. if (ret != 0) {
  15572. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15573. }
  15574. ret = wc_InitRng(&rng);
  15575. if (ret != 0) {
  15576. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15577. }
  15578. ret = wc_RNG_GenerateBlock(&rng, output, sizeof(output));
  15579. if (ret != 0) {
  15580. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15581. }
  15582. ret = XMEMCMP(output, check, sizeof(output));
  15583. if (ret != 0) {
  15584. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  15585. }
  15586. ret = wc_FreeRng(&rng);
  15587. if (ret != 0) {
  15588. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15589. }
  15590. ret = wc_SetSeed_Cb(wc_GenerateSeed);
  15591. if (ret != 0) {
  15592. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  15593. }
  15594. out:
  15595. return ret;
  15596. }
  15597. #endif
  15598. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_test(void)
  15599. {
  15600. WOLFSSL_SMALL_STACK_STATIC const byte test1Entropy[] =
  15601. {
  15602. 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
  15603. 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
  15604. 0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, 0x85, 0x81, 0xf9, 0x31,
  15605. 0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d, 0xdb, 0xcb, 0xcc, 0x2e
  15606. };
  15607. WOLFSSL_SMALL_STACK_STATIC const byte test1Output[] =
  15608. {
  15609. 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
  15610. 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
  15611. 0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
  15612. 0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
  15613. 0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
  15614. 0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
  15615. 0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
  15616. 0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
  15617. 0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
  15618. 0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
  15619. 0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
  15620. };
  15621. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyA[] =
  15622. {
  15623. 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
  15624. 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
  15625. 0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
  15626. 0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
  15627. };
  15628. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyB[] =
  15629. {
  15630. 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
  15631. 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
  15632. 0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
  15633. };
  15634. WOLFSSL_SMALL_STACK_STATIC const byte test2Output[] =
  15635. {
  15636. 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
  15637. 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
  15638. 0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
  15639. 0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
  15640. 0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
  15641. 0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
  15642. 0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
  15643. 0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
  15644. 0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
  15645. 0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
  15646. 0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
  15647. };
  15648. byte output[WC_SHA256_DIGEST_SIZE * 4];
  15649. wc_test_ret_t ret;
  15650. WOLFSSL_ENTER("random_test");
  15651. ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
  15652. output, sizeof(output));
  15653. if (ret != 0)
  15654. return WC_TEST_RET_ENC_EC(ret);
  15655. if (XMEMCMP(test1Output, output, sizeof(output)) != 0)
  15656. return WC_TEST_RET_ENC_NC;
  15657. ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
  15658. test2EntropyB, sizeof(test2EntropyB),
  15659. output, sizeof(output));
  15660. if (ret != 0)
  15661. return WC_TEST_RET_ENC_EC(ret);
  15662. if (XMEMCMP(test2Output, output, sizeof(output)) != 0)
  15663. return WC_TEST_RET_ENC_NC;
  15664. /* Basic RNG generate block test */
  15665. if ((ret = random_rng_test()) != 0)
  15666. return ret;
  15667. /* Test the seed check function. */
  15668. #if !(defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) || \
  15669. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  15670. {
  15671. word32 i, outputSz;
  15672. /* Repeat the same byte over and over. Should fail. */
  15673. outputSz = sizeof(output);
  15674. XMEMSET(output, 1, outputSz);
  15675. ret = wc_RNG_TestSeed(output, outputSz);
  15676. if (ret == 0)
  15677. return WC_TEST_RET_ENC_NC;
  15678. /* Every byte of the entropy scratch is different,
  15679. * entropy is a single byte that shouldn't match. */
  15680. outputSz = (sizeof(output) / 2) + 1;
  15681. for (i = 0; i < outputSz; i++)
  15682. output[i] = (byte)i;
  15683. ret = wc_RNG_TestSeed(output, outputSz);
  15684. if (ret != 0)
  15685. return WC_TEST_RET_ENC_EC(ret);
  15686. outputSz = sizeof(output);
  15687. for (i = 0; i < outputSz; i++)
  15688. output[i] = (byte)i;
  15689. ret = wc_RNG_TestSeed(output, outputSz);
  15690. if (ret != 0)
  15691. return WC_TEST_RET_ENC_EC(ret);
  15692. }
  15693. #endif
  15694. /* Test the seed callback. */
  15695. #ifdef WC_RNG_SEED_CB
  15696. if ((ret = rng_seed_test()) != 0)
  15697. return ret;
  15698. #endif
  15699. return 0;
  15700. }
  15701. #else
  15702. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_test(void)
  15703. {
  15704. WOLFSSL_ENTER("random_test");
  15705. /* Basic RNG generate block test */
  15706. return random_rng_test();
  15707. }
  15708. #endif /* HAVE_HASHDRBG && !CUSTOM_RAND_GENERATE_BLOCK */
  15709. #endif /* WC_NO_RNG */
  15710. #ifndef MEM_TEST_SZ
  15711. #define MEM_TEST_SZ 1024
  15712. #endif
  15713. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  15714. static int simple_mem_test(int sz)
  15715. {
  15716. int ret = 0;
  15717. byte* b;
  15718. int i;
  15719. b = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15720. if (b == NULL) {
  15721. return WC_TEST_RET_ENC_NC;
  15722. }
  15723. /* utilize memory */
  15724. for (i = 0; i < sz; i++) {
  15725. b[i] = (byte)i;
  15726. }
  15727. /* read back and verify */
  15728. for (i = 0; i < sz; i++) {
  15729. if (b[i] != (byte)i) {
  15730. ret = WC_TEST_RET_ENC_NC;
  15731. break;
  15732. }
  15733. }
  15734. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15735. return ret;
  15736. }
  15737. #endif
  15738. /* If successful, returns the first letter of the byte array `in`.
  15739. **
  15740. ** This is a deceptively simple test of a read-only embedded Linux file system.
  15741. ** (e.g CFLAGS `-mfdpic` and `-mforce-l32`) for Xtensa Linux ESP32. When the
  15742. ** `-mforce-l32` is missing, access to `in` will fail with Illegal Instruction.
  15743. ** Const is on read-only memory-mapped file system, *not* loaded in app memory.
  15744. **
  15745. ** Edit with caution. See PR #6523. */
  15746. static wc_test_ret_t const_byte_ptr_test(const byte* in, word32 *outJ)
  15747. {
  15748. wc_test_ret_t ret = 0;
  15749. volatile word32 j = (word32)-1; /* must be volatile to properly detect error */
  15750. ret = (wc_test_ret_t)*in; /* accessed *in value. */
  15751. (void)ret;
  15752. j = *outJ; /* Found index to use in const array. */
  15753. if (j == 0) {
  15754. #ifdef WOLFSSL_DEBUG
  15755. printf("Testing const byte ptr reference...\n");
  15756. #endif
  15757. /* although j is zero, in[0] does not detect the Illegal instruction */
  15758. ret = in[j]; /* The big test: can we actually access the `in` data? */
  15759. }
  15760. else {
  15761. ret = WC_TEST_RET_ENC_I(j);
  15762. }
  15763. return ret;
  15764. }
  15765. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memory_test(void)
  15766. {
  15767. wc_test_ret_t ret = 0;
  15768. word32 j = 0; /* used in embedded const pointer test */
  15769. WOLFSSL_ENTER("memory_test");
  15770. #if defined(COMPLEX_MEM_TEST) || defined(WOLFSSL_STATIC_MEMORY)
  15771. int i;
  15772. #endif
  15773. #ifdef WOLFSSL_STATIC_MEMORY
  15774. word32 size[] = { WOLFMEM_BUCKETS };
  15775. word32 dist[] = { WOLFMEM_DIST };
  15776. byte buffer[30000]; /* make large enough to involve many bucket sizes */
  15777. int pad = -(int)((wc_ptr_t)buffer) & (WOLFSSL_STATIC_ALIGN - 1);
  15778. /* pad to account for if head of buffer is not at set memory
  15779. * alignment when tests are ran */
  15780. #endif
  15781. #ifdef WOLFSSL_STATIC_MEMORY
  15782. /* check macro settings */
  15783. if (sizeof(size)/sizeof(word32) != WOLFMEM_DEF_BUCKETS) {
  15784. return WC_TEST_RET_ENC_NC;
  15785. }
  15786. if (sizeof(dist)/sizeof(word32) != WOLFMEM_DEF_BUCKETS) {
  15787. return WC_TEST_RET_ENC_NC;
  15788. }
  15789. if (WOLFMEM_DEF_BUCKETS > WOLFMEM_MAX_BUCKETS) {
  15790. return WC_TEST_RET_ENC_NC;
  15791. }
  15792. for (i = 0; i < WOLFMEM_DEF_BUCKETS; i++) {
  15793. if ((size[i] % WOLFSSL_STATIC_ALIGN) != 0) {
  15794. /* each element in array should be divisible by alignment size */
  15795. return WC_TEST_RET_ENC_NC;
  15796. }
  15797. }
  15798. for (i = 1; i < WOLFMEM_DEF_BUCKETS; i++) {
  15799. if (size[i - 1] >= size[i]) {
  15800. return WC_TEST_RET_ENC_NC; /* sizes should be in increasing order */
  15801. }
  15802. }
  15803. #ifndef WOLFSSL_STATIC_MEMORY_LEAN
  15804. /* check that padding size returned is possible */
  15805. if (wolfSSL_MemoryPaddingSz() < WOLFSSL_STATIC_ALIGN) {
  15806. return WC_TEST_RET_ENC_NC; /* no room for wc_Memory struct */
  15807. }
  15808. ret = wolfSSL_MemoryPaddingSz();
  15809. if (ret < 0) {
  15810. return WC_TEST_RET_ENC_EC(ret);
  15811. }
  15812. if (wolfSSL_MemoryPaddingSz() % WOLFSSL_STATIC_ALIGN != 0) {
  15813. return WC_TEST_RET_ENC_NC; /* not aligned! */
  15814. }
  15815. /* check function to return optimum buffer size (rounded down) */
  15816. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_GENERAL);
  15817. if (ret < 0) {
  15818. return WC_TEST_RET_ENC_EC(ret);
  15819. }
  15820. if ((ret - pad) % WOLFSSL_STATIC_ALIGN != 0) {
  15821. return WC_TEST_RET_ENC_NC; /* not aligned! */
  15822. }
  15823. if ((unsigned int)ret > sizeof(buffer)) {
  15824. return WC_TEST_RET_ENC_NC; /* did not round down as expected */
  15825. }
  15826. if (ret != wolfSSL_StaticBufferSz(buffer, ret, WOLFMEM_GENERAL)) {
  15827. return WC_TEST_RET_ENC_NC; /* return value changed when using suggested
  15828. * value
  15829. */
  15830. }
  15831. ret = wolfSSL_MemoryPaddingSz();
  15832. ret += pad; /* add space that is going to be needed if buffer not aligned */
  15833. if (wolfSSL_StaticBufferSz(buffer, size[0] + ret + 1, WOLFMEM_GENERAL) !=
  15834. (ret + (int)size[0])) {
  15835. return WC_TEST_RET_ENC_NC; /* did not round down to nearest bucket
  15836. * value
  15837. */
  15838. }
  15839. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_IO_POOL);
  15840. if ((ret - pad) < 0) {
  15841. return WC_TEST_RET_ENC_NC;
  15842. }
  15843. if (((ret - pad) % (WOLFMEM_IO_SZ + wolfSSL_MemoryPaddingSz())) != 0) {
  15844. return WC_TEST_RET_ENC_NC; /* not even chunks of memory for IO size */
  15845. }
  15846. if (((ret - pad) % WOLFSSL_STATIC_ALIGN) != 0) {
  15847. return WC_TEST_RET_ENC_NC; /* memory not aligned */
  15848. }
  15849. /* check for passing bad or unknown arguments to functions */
  15850. if (wolfSSL_StaticBufferSz(NULL, 1, WOLFMEM_GENERAL) > 0) {
  15851. return WC_TEST_RET_ENC_NC;
  15852. }
  15853. if (wolfSSL_StaticBufferSz(buffer, 1, WOLFMEM_GENERAL) != 0) {
  15854. return WC_TEST_RET_ENC_NC; /* should round to 0
  15855. since struct + bucket will not fit */
  15856. }
  15857. #endif
  15858. (void)pad;
  15859. (void)dist; /* avoid static analysis warning of variable not used */
  15860. #endif
  15861. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  15862. /* simple test */
  15863. ret = simple_mem_test(MEM_TEST_SZ);
  15864. if (ret != 0)
  15865. return ret;
  15866. #endif
  15867. #ifdef COMPLEX_MEM_TEST
  15868. /* test various size blocks */
  15869. for (i = 1; i < MEM_TEST_SZ; i*=2) {
  15870. ret = simple_mem_test(i);
  15871. if (ret != 0)
  15872. return ret;
  15873. }
  15874. #endif
  15875. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC) && defined(XREALLOC)
  15876. /* realloc test */
  15877. {
  15878. byte *b = (byte*)XMALLOC(MEM_TEST_SZ, HEAP_HINT,
  15879. DYNAMIC_TYPE_TMP_BUFFER);
  15880. #ifndef WOLFSSL_NO_REALLOC
  15881. byte *c = NULL;
  15882. if (b) {
  15883. c = (byte*)XREALLOC(b, MEM_TEST_SZ+sizeof(word32), HEAP_HINT,
  15884. DYNAMIC_TYPE_TMP_BUFFER);
  15885. if (c)
  15886. b = c;
  15887. }
  15888. #endif
  15889. if (b)
  15890. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15891. if (b == NULL
  15892. #ifndef WOLFSSL_NO_REALLOC
  15893. || c == NULL
  15894. #endif
  15895. ) {
  15896. return WC_TEST_RET_ENC_ERRNO;
  15897. }
  15898. }
  15899. #endif
  15900. if (ret == 0) {
  15901. /* This test is only interesting on embedded R/O Flash systems */
  15902. if (const_byte_ptr_test(const_byte_array, &j) != CBPTR_EXPECTED) {
  15903. ret = 1;
  15904. }
  15905. }
  15906. return ret;
  15907. }
  15908. #ifndef NO_FILESYSTEM
  15909. /* Cert Paths */
  15910. #ifdef FREESCALE_MQX
  15911. #define CERT_PREFIX "a:\\"
  15912. #define CERT_PATH_SEP "\\"
  15913. #elif defined(WOLFSSL_uTKERNEL2)
  15914. #define CERT_PREFIX "/uda/"
  15915. #define CERT_PATH_SEP "/"
  15916. #elif defined(_WIN32_WCE)
  15917. #define CERT_PREFIX "\\windows\\"
  15918. #define CERT_PATH_SEP "\\"
  15919. #endif
  15920. #ifndef CERT_PREFIX
  15921. #define CERT_PREFIX "./"
  15922. #endif
  15923. #ifndef CERT_PATH_SEP
  15924. #define CERT_PATH_SEP "/"
  15925. #endif
  15926. #ifndef CERT_WRITE_TEMP_DIR
  15927. #define CERT_WRITE_TEMP_DIR CERT_PREFIX
  15928. #endif
  15929. #define CERT_ROOT CERT_PREFIX "certs" CERT_PATH_SEP
  15930. /* Generated Test Certs */
  15931. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  15932. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  15933. #if !defined(NO_RSA) && !defined(NO_ASN)
  15934. static const char* clientKey = CERT_ROOT "client-key.der";
  15935. static const char* clientCert = CERT_ROOT "client-cert.der";
  15936. #ifdef WOLFSSL_CERT_EXT
  15937. static const char* clientKeyPub = CERT_ROOT "client-keyPub.der";
  15938. #endif
  15939. #endif /* !NO_RSA && !NO_ASN */
  15940. #endif
  15941. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  15942. #if !defined(NO_RSA) && !defined(NO_ASN)
  15943. #if defined(WOLFSSL_CERT_GEN) || defined(HAVE_PKCS7)
  15944. static const char* rsaCaKeyFile = CERT_ROOT "ca-key.der";
  15945. #ifdef WOLFSSL_CERT_GEN
  15946. static const char* rsaCaCertFile = CERT_ROOT "ca-cert.pem";
  15947. #endif
  15948. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  15949. static const char* rsaCaCertDerFile = CERT_ROOT "ca-cert.der";
  15950. #endif
  15951. #ifdef HAVE_PKCS7
  15952. static const char* rsaServerCertDerFile =
  15953. CERT_ROOT "server-cert.der";
  15954. static const char* rsaServerKeyDerFile =
  15955. CERT_ROOT "server-key.der";
  15956. #endif
  15957. #endif
  15958. #endif /* !NO_RSA && !NO_ASN */
  15959. #endif /* !USE_CERT_BUFFER_* */
  15960. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  15961. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  15962. #if !defined(NO_ASN) && !defined(NO_DH)
  15963. static const char* dhParamsFile = CERT_ROOT "dh2048.der";
  15964. #endif
  15965. #endif
  15966. #if !defined(NO_ASN) && !defined(NO_DH)
  15967. #if defined(WOLFSSL_DH_EXTRA) && (!defined(HAVE_FIPS) || \
  15968. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  15969. #if !defined(USE_CERT_BUFFERS_2048)
  15970. static const char* dhKeyFile = CERT_ROOT "statickeys/dh-ffdhe2048.der";
  15971. static const char* dhKeyPubFile = CERT_ROOT "statickeys/dh-ffdhe2048-pub.der";
  15972. #endif
  15973. #endif
  15974. #endif
  15975. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  15976. #ifndef NO_DSA
  15977. static const char* dsaKey = CERT_ROOT "dsa2048.der";
  15978. #endif
  15979. #endif /* !USE_CERT_BUFFER_* */
  15980. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ECC256)
  15981. #ifdef HAVE_ECC
  15982. /* cert files to be used in rsa cert gen test, check if RSA enabled */
  15983. #ifdef HAVE_ECC_KEY_IMPORT
  15984. static const char* eccKeyDerFile = CERT_ROOT "ecc-key.der";
  15985. #endif
  15986. #endif
  15987. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ASN)
  15988. #if defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN) && \
  15989. !defined(NO_ECC_SECP)
  15990. #ifndef NO_RSA
  15991. static const char* eccKeyPubFileDer = CERT_ROOT "ecc-keyPub.der";
  15992. #endif
  15993. #ifndef NO_ASN_TIME
  15994. static const char* eccCaKeyFile = CERT_ROOT "ca-ecc-key.der";
  15995. static const char* eccCaCertFile = CERT_ROOT "ca-ecc-cert.pem";
  15996. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  15997. static const char* eccCaKey384File =
  15998. CERT_ROOT "ca-ecc384-key.der";
  15999. static const char* eccCaCert384File =
  16000. CERT_ROOT "ca-ecc384-cert.pem";
  16001. #endif
  16002. #endif
  16003. #endif
  16004. #if defined(HAVE_PKCS7) && defined(HAVE_ECC)
  16005. static const char* eccClientKey = CERT_ROOT "ecc-client-key.der";
  16006. static const char* eccClientCert = CERT_ROOT "client-ecc-cert.der";
  16007. #endif
  16008. #endif /* HAVE_ECC */
  16009. #ifdef HAVE_ED25519
  16010. #ifdef WOLFSSL_TEST_CERT
  16011. static const char* serverEd25519Cert =
  16012. CERT_ROOT "ed25519/server-ed25519.der";
  16013. static const char* caEd25519Cert =
  16014. CERT_ROOT "ed25519/ca-ed25519.der";
  16015. #endif
  16016. #endif
  16017. #ifdef HAVE_ED448
  16018. #ifdef WOLFSSL_TEST_CERT
  16019. static const char* serverEd448Cert =
  16020. CERT_ROOT "ed448/server-ed448.der";
  16021. static const char* caEd448Cert = CERT_ROOT "ed448/ca-ed448.der";
  16022. #endif
  16023. #endif
  16024. #endif /* !USE_CERT_BUFFER_* */
  16025. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  16026. !defined(NO_FILESYSTEM)
  16027. static const char* certExtNc =
  16028. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nc.der";
  16029. static const char* certExtIa =
  16030. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-ia.der";
  16031. static const char* certExtNct =
  16032. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nct.der";
  16033. #ifndef WOLFSSL_ASN_INT_LEAD_0_ANY
  16034. static const char* certBadNegInt =
  16035. CERT_ROOT "test" CERT_PATH_SEP "cert-bad-neg-int.der";
  16036. #endif
  16037. static const char* certBadOid =
  16038. CERT_ROOT "test" CERT_PATH_SEP "cert-bad-oid.der";
  16039. #ifndef WOLFSSL_NO_ASN_STRICT
  16040. static const char* certBadUtf8 =
  16041. CERT_ROOT "test" CERT_PATH_SEP "cert-bad-utf8.der";
  16042. #endif
  16043. #endif
  16044. #ifndef NO_WRITE_TEMP_FILES
  16045. #ifdef HAVE_ECC
  16046. #ifndef NO_ECC_SECP
  16047. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  16048. static const char* certEccPemFile = CERT_WRITE_TEMP_DIR "certecc.pem";
  16049. static const char* certEccDerFile = CERT_WRITE_TEMP_DIR "certecc.der";
  16050. #endif
  16051. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  16052. static const char* certEccRsaPemFile = CERT_WRITE_TEMP_DIR "certeccrsa.pem";
  16053. static const char* certEccRsaDerFile = CERT_WRITE_TEMP_DIR "certeccrsa.der";
  16054. #endif
  16055. #endif
  16056. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  16057. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ASN_CRYPT)
  16058. static const char* eccCaKeyPemFile = CERT_WRITE_TEMP_DIR "ecc-key.pem";
  16059. static const char* eccPubKeyDerFile = CERT_WRITE_TEMP_DIR "ecc-public-key.der";
  16060. static const char* eccCaKeyTempFile = CERT_WRITE_TEMP_DIR "ecc-key.der";
  16061. #if defined(HAVE_PKCS8) && !defined(WC_NO_RNG) && \
  16062. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  16063. static const char* eccPkcs8KeyDerFile = CERT_WRITE_TEMP_DIR "ecc-key-pkcs8.der";
  16064. #endif
  16065. #endif /* HAVE_ECC_KEY_EXPORT */
  16066. #endif /* HAVE_ECC */
  16067. #ifndef NO_RSA
  16068. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  16069. static const char* otherCertDerFile = CERT_WRITE_TEMP_DIR "othercert.der";
  16070. static const char* certDerFile = CERT_WRITE_TEMP_DIR "cert.der";
  16071. static const char* otherCertPemFile = CERT_WRITE_TEMP_DIR "othercert.pem";
  16072. static const char* certPemFile = CERT_WRITE_TEMP_DIR "cert.pem";
  16073. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  16074. static const char* certReqDerFile = CERT_WRITE_TEMP_DIR "certreq.der";
  16075. static const char* certReqPemFile = CERT_WRITE_TEMP_DIR "certreq.pem";
  16076. #endif
  16077. #endif
  16078. #endif /* !NO_RSA */
  16079. #if !defined(NO_RSA) || !defined(NO_DSA)
  16080. #ifdef WOLFSSL_KEY_GEN
  16081. static const char* keyDerFile = CERT_WRITE_TEMP_DIR "key.der";
  16082. static const char* keyPemFile = CERT_WRITE_TEMP_DIR "key.pem";
  16083. #endif
  16084. #endif
  16085. #endif /* !NO_WRITE_TEMP_FILES */
  16086. #endif /* !NO_FILESYSTEM */
  16087. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  16088. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  16089. static CertName certDefaultName;
  16090. static void initDefaultName(void)
  16091. {
  16092. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  16093. NameAttrib* n;
  16094. #endif
  16095. XMEMCPY(certDefaultName.country, "US", sizeof("US"));
  16096. certDefaultName.countryEnc = CTC_PRINTABLE;
  16097. XMEMCPY(certDefaultName.state, "Oregon", sizeof("Oregon"));
  16098. certDefaultName.stateEnc = CTC_UTF8;
  16099. XMEMCPY(certDefaultName.street, "Main St", sizeof("Main St"));
  16100. certDefaultName.streetEnc = CTC_UTF8;
  16101. XMEMCPY(certDefaultName.locality, "Portland", sizeof("Portland"));
  16102. certDefaultName.localityEnc = CTC_UTF8;
  16103. XMEMCPY(certDefaultName.sur, "Test", sizeof("Test"));
  16104. certDefaultName.surEnc = CTC_UTF8;
  16105. XMEMCPY(certDefaultName.org, "wolfSSL", sizeof("wolfSSL"));
  16106. certDefaultName.orgEnc = CTC_UTF8;
  16107. XMEMCPY(certDefaultName.unit, "Development", sizeof("Development"));
  16108. certDefaultName.unitEnc = CTC_UTF8;
  16109. XMEMCPY(certDefaultName.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  16110. certDefaultName.commonNameEnc = CTC_UTF8;
  16111. XMEMCPY(certDefaultName.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  16112. certDefaultName.serialDevEnc = CTC_PRINTABLE;
  16113. XMEMCPY(certDefaultName.postalCode, "12-456", sizeof("12-456"));
  16114. certDefaultName.postalCodeEnc = CTC_PRINTABLE;
  16115. #ifdef WOLFSSL_CERT_EXT
  16116. XMEMCPY(certDefaultName.busCat, "Private Organization", sizeof("Private Organization"));
  16117. certDefaultName.busCatEnc = CTC_UTF8;
  16118. XMEMCPY(certDefaultName.joiSt, "US", sizeof("US"));
  16119. certDefaultName.joiStEnc = CTC_PRINTABLE;
  16120. XMEMCPY(certDefaultName.joiC, "Oregon", sizeof("Oregon"));
  16121. certDefaultName.joiCEnc = CTC_PRINTABLE;
  16122. #endif
  16123. XMEMCPY(certDefaultName.email, "info@wolfssl.com", sizeof("info@wolfssl.com"));
  16124. XMEMCPY(certDefaultName.userId, "TestUserID", sizeof("TestUserID"));
  16125. certDefaultName.userIdEnc = CTC_PRINTABLE;
  16126. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  16127. /* test having additional OUs and setting DC */
  16128. n = &certDefaultName.name[0];
  16129. n->id = ASN_ORGUNIT_NAME;
  16130. n->type = CTC_UTF8;
  16131. n->sz = XSTRLEN("Development-2");
  16132. XMEMCPY(n->value, "Development-2", sizeof("Development-2"));
  16133. #if CTC_MAX_ATTRIB > 3
  16134. n = &certDefaultName.name[1];
  16135. n->id = ASN_DOMAIN_COMPONENT;
  16136. n->type = CTC_UTF8;
  16137. n->sz = XSTRLEN("com");
  16138. XMEMCPY(n->value, "com", sizeof("com"));
  16139. n = &certDefaultName.name[2];
  16140. n->id = ASN_DOMAIN_COMPONENT;
  16141. n->type = CTC_UTF8;
  16142. n->sz = XSTRLEN("wolfssl");
  16143. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  16144. #endif
  16145. #endif /* WOLFSSL_MULTI_ATTRIB && WOLFSSL_TEST_CERT */
  16146. #ifdef WOLFSSL_CUSTOM_OID
  16147. /* TODO: Add test case for custom OID's */
  16148. #endif
  16149. }
  16150. #ifdef WOLFSSL_CERT_EXT
  16151. #if ((defined(HAVE_ED25519) || defined(HAVE_ED448)) && \
  16152. defined(WOLFSSL_TEST_CERT)) || defined(HAVE_ECC)
  16153. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage[] =
  16154. "digitalSignature,nonRepudiation";
  16155. #endif
  16156. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_ASN_TIME)
  16157. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage2[] =
  16158. "digitalSignature,nonRepudiation,keyEncipherment,keyAgreement";
  16159. #endif
  16160. #endif /* WOLFSSL_CERT_EXT */
  16161. #endif /* WOLFSSL_CERT_GEN */
  16162. #ifndef NO_RSA
  16163. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  16164. !defined(NO_FILESYSTEM)
  16165. static const byte minSerial[] = { 0x02, 0x01, 0x01 };
  16166. static const byte minName[] = { 0x30, 0x00 };
  16167. static const byte nameBad[] = {
  16168. 0x30, 0x08,
  16169. 0x31, 0x06,
  16170. 0x30, 0x04,
  16171. 0x06, 0x02,
  16172. 0x55, 0x04,
  16173. };
  16174. static const byte minDates[] = {
  16175. 0x30, 0x1e,
  16176. 0x17, 0x0d,
  16177. 0x31, 0x38, 0x30, 0x34, 0x31, 0x33, 0x31, 0x35,
  16178. 0x32, 0x33, 0x31, 0x30, 0x5a,
  16179. 0x17, 0x0d,
  16180. 0x32, 0x31, 0x30, 0x31, 0x30, 0x37, 0x31, 0x35,
  16181. 0x32, 0x33, 0x31, 0x30, 0x5a
  16182. };
  16183. static const byte minPubKey[] = {
  16184. 0x30, 0x1c,
  16185. 0x30, 0x0d,
  16186. 0x06, 0x09,
  16187. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  16188. 0x01,
  16189. 0x05, 0x00,
  16190. 0x03, 0x0b,
  16191. 0x00, 0x30, 0x08,
  16192. 0x02, 0x01,
  16193. 0x03,
  16194. 0x02, 0x03,
  16195. 0x01, 0x00, 0x01
  16196. };
  16197. static const byte minSigAlg[] = {
  16198. 0x30, 0x0d,
  16199. 0x06, 0x09,
  16200. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  16201. 0x0b,
  16202. 0x05, 0x00
  16203. };
  16204. static const byte minSig[] = {
  16205. 0x03, 0x01,
  16206. 0x00
  16207. };
  16208. static int add_seq(byte* certData, int offset, byte* data, byte length)
  16209. {
  16210. XMEMMOVE(certData + offset + 2, data, length);
  16211. certData[offset++] = 0x30;
  16212. certData[offset++] = length;
  16213. return offset + length;
  16214. }
  16215. static int add_data(byte* certData, int offset, const byte* data, byte length)
  16216. {
  16217. XMEMCPY(certData + offset, data, length);
  16218. return offset + length;
  16219. }
  16220. static wc_test_ret_t cert_asn1_test(void)
  16221. {
  16222. wc_test_ret_t ret;
  16223. int len[3];
  16224. DecodedCert cert;
  16225. byte certData[114];
  16226. byte* badCert = NULL;
  16227. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  16228. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  16229. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  16230. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  16231. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  16232. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  16233. len[1] = add_seq(certData, 0, certData, len[2]);
  16234. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  16235. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  16236. len[0] = add_seq(certData, 0, certData, len[1]);
  16237. /* Minimal good certificate */
  16238. InitDecodedCert(&cert, certData, len[0], 0);
  16239. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  16240. FreeDecodedCert(&cert);
  16241. if (ret != 0)
  16242. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16243. /* Bad issuer name */
  16244. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  16245. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  16246. len[2] = add_data(certData, len[2], nameBad, (byte)sizeof(nameBad));
  16247. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  16248. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  16249. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  16250. len[1] = add_seq(certData, 0, certData, len[2]);
  16251. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  16252. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  16253. len[0] = add_seq(certData, 0, certData, len[1]);
  16254. /* Put data into allocated buffer to allow access error checking. */
  16255. badCert = (byte*)XMALLOC(len[0], HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16256. XMEMCPY(badCert, certData, len[0]);
  16257. InitDecodedCert(&cert, badCert, len[0], 0);
  16258. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  16259. FreeDecodedCert(&cert);
  16260. if (ret != ASN_PARSE_E) {
  16261. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16262. }
  16263. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16264. badCert = NULL;
  16265. ret = 0;
  16266. done:
  16267. if (badCert != NULL)
  16268. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16269. return ret;
  16270. }
  16271. static wc_test_ret_t cert_load_bad(const char* fname, byte* tmp, int err)
  16272. {
  16273. wc_test_ret_t ret;
  16274. DecodedCert cert;
  16275. XFILE file;
  16276. size_t bytes;
  16277. if ((fname == NULL) || (tmp == NULL)) {
  16278. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  16279. }
  16280. file = XFOPEN(fname, "rb");
  16281. if (!file) {
  16282. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16283. }
  16284. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16285. XFCLOSE(file);
  16286. if (bytes == 0) {
  16287. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16288. }
  16289. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16290. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  16291. FreeDecodedCert(&cert);
  16292. if (ret != err) {
  16293. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16294. }
  16295. ret = 0;
  16296. done:
  16297. return ret;
  16298. }
  16299. static wc_test_ret_t cert_bad_asn1_test(void)
  16300. {
  16301. wc_test_ret_t ret = 0;
  16302. byte* tmp;
  16303. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16304. if (tmp == NULL) {
  16305. ret = WC_TEST_RET_ENC_ERRNO;
  16306. }
  16307. #ifndef WOLFSSL_ASN_INT_LEAD_0_ANY
  16308. if (ret == 0) {
  16309. /* Serial number: 0xff 0xa8. 0xff and top bit set on next byte invalid.
  16310. */
  16311. ret = cert_load_bad(certBadNegInt, tmp, ASN_EXPECT_0_E);
  16312. }
  16313. #endif
  16314. if (ret == 0) {
  16315. /* Subject name OID: 55 04 f4. Last byte with top bit set invalid. */
  16316. ret = cert_load_bad(certBadOid, tmp, ASN_PARSE_E);
  16317. }
  16318. #ifndef WOLFSSL_NO_ASN_STRICT
  16319. if (ret == 0) {
  16320. /* Issuer name UTF8STRING: df 52 4e 44. Top bit of second byte not set.
  16321. */
  16322. ret = cert_load_bad(certBadUtf8, tmp, ASN_PARSE_E);
  16323. }
  16324. #endif
  16325. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16326. return ret;
  16327. }
  16328. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cert_test(void)
  16329. {
  16330. #if !defined(NO_FILESYSTEM)
  16331. DecodedCert cert;
  16332. byte* tmp;
  16333. size_t bytes;
  16334. XFILE file;
  16335. wc_test_ret_t ret;
  16336. WOLFSSL_ENTER("cert_test");
  16337. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16338. if (tmp == NULL)
  16339. return WC_TEST_RET_ENC_ERRNO;
  16340. /* Certificate with Name Constraints extension. */
  16341. file = XFOPEN(certExtNc, "rb");
  16342. if (!file) {
  16343. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16344. }
  16345. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16346. XFCLOSE(file);
  16347. if (bytes == 0)
  16348. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16349. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16350. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  16351. if (ret != 0)
  16352. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16353. FreeDecodedCert(&cert);
  16354. /* Certificate with Inhibit Any Policy extension. */
  16355. file = XFOPEN(certExtIa, "rb");
  16356. if (!file) {
  16357. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16358. }
  16359. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16360. XFCLOSE(file);
  16361. if (bytes == 0)
  16362. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16363. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16364. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  16365. if (ret != 0)
  16366. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16367. FreeDecodedCert(&cert);
  16368. /* Certificate with Netscape Certificate Type extension. */
  16369. file = XFOPEN(certExtNct, "rb");
  16370. if (!file) {
  16371. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16372. }
  16373. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16374. XFCLOSE(file);
  16375. if (bytes == 0)
  16376. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16377. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16378. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  16379. #ifndef IGNORE_NETSCAPE_CERT_TYPE
  16380. if (ret != 0)
  16381. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16382. #else
  16383. if (ret != ASN_CRIT_EXT_E) {
  16384. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16385. }
  16386. ret = 0;
  16387. #endif
  16388. done:
  16389. FreeDecodedCert(&cert);
  16390. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16391. #endif /* !NO_FILESYSTEM */
  16392. if (ret == 0)
  16393. ret = cert_asn1_test();
  16394. if (ret == 0)
  16395. ret = cert_bad_asn1_test();
  16396. return ret;
  16397. }
  16398. #endif /* WOLFSSL_TEST_CERT */
  16399. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  16400. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  16401. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certext_test(void)
  16402. {
  16403. DecodedCert cert;
  16404. byte* tmp;
  16405. size_t bytes;
  16406. XFILE file;
  16407. wc_test_ret_t ret;
  16408. /* created from rsa_test : othercert.der */
  16409. byte skid_rsa[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  16410. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  16411. /* created from rsa_test : othercert.der */
  16412. byte akid_rsa[] = "\x27\x8E\x67\x11\x74\xC3\x26\x1D\x3F\xED"
  16413. "\x33\x63\xB3\xA4\xD8\x1D\x30\xE5\xE8\xD5";
  16414. #ifdef HAVE_ECC
  16415. /* created from ecc_test_cert_gen : certecc.der */
  16416. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  16417. /* Authority key id from ./certs/ca-ecc384-cert.pem */
  16418. byte akid_ecc[] = "\xAB\xE0\xC3\x26\x4C\x18\xD4\x72\xBB\xD2"
  16419. "\x84\x8C\x9C\x0A\x05\x92\x80\x12\x53\x52";
  16420. #else
  16421. /* Authority key id from ./certs/ca-ecc-cert.pem */
  16422. byte akid_ecc[] = "\x56\x8E\x9A\xC3\xF0\x42\xDE\x18\xB9\x45"
  16423. "\x55\x6E\xF9\x93\xCF\xEA\xC3\xF3\xA5\x21";
  16424. #endif
  16425. #endif /* HAVE_ECC */
  16426. /* created from rsa_test : cert.der */
  16427. byte kid_ca[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  16428. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  16429. WOLFSSL_ENTER("certext_test");
  16430. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16431. if (tmp == NULL)
  16432. return WC_TEST_RET_ENC_ERRNO;
  16433. /* load othercert.der (Cert signed by an authority) */
  16434. file = XFOPEN(otherCertDerFile, "rb");
  16435. if (!file) {
  16436. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16437. return WC_TEST_RET_ENC_ERRNO;
  16438. }
  16439. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16440. XFCLOSE(file);
  16441. if (bytes == 0)
  16442. return WC_TEST_RET_ENC_ERRNO;
  16443. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16444. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  16445. if (ret != 0)
  16446. return WC_TEST_RET_ENC_EC(ret);
  16447. /* check the SKID from a RSA certificate */
  16448. if (XMEMCMP(skid_rsa, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  16449. return WC_TEST_RET_ENC_NC;
  16450. /* check the AKID from an RSA certificate */
  16451. if (XMEMCMP(akid_rsa, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  16452. return WC_TEST_RET_ENC_NC;
  16453. /* check the Key Usage from an RSA certificate */
  16454. if (!cert.extKeyUsageSet)
  16455. return WC_TEST_RET_ENC_NC;
  16456. if (cert.extKeyUsage != (KEYUSE_KEY_ENCIPHER|KEYUSE_KEY_AGREE))
  16457. return WC_TEST_RET_ENC_NC;
  16458. /* check the CA Basic Constraints from an RSA certificate */
  16459. if (cert.isCA)
  16460. return WC_TEST_RET_ENC_NC;
  16461. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  16462. /* check the Certificate Policies Id */
  16463. if (cert.extCertPoliciesNb != 1)
  16464. return WC_TEST_RET_ENC_NC;
  16465. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  16466. return WC_TEST_RET_ENC_NC;
  16467. #endif
  16468. FreeDecodedCert(&cert);
  16469. #ifdef HAVE_ECC
  16470. /* load certecc.der (Cert signed by our ECC CA test in ecc_test_cert_gen) */
  16471. file = XFOPEN(certEccDerFile, "rb");
  16472. if (!file) {
  16473. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16474. return WC_TEST_RET_ENC_ERRNO;
  16475. }
  16476. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16477. XFCLOSE(file);
  16478. if (bytes == 0)
  16479. return WC_TEST_RET_ENC_ERRNO;
  16480. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16481. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  16482. if (ret != 0)
  16483. return WC_TEST_RET_ENC_EC(ret);
  16484. /* check the SKID from a ECC certificate - generated dynamically */
  16485. /* check the AKID from an ECC certificate */
  16486. if (XMEMCMP(akid_ecc, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  16487. return WC_TEST_RET_ENC_NC;
  16488. /* check the Key Usage from an ECC certificate */
  16489. if (!cert.extKeyUsageSet)
  16490. return WC_TEST_RET_ENC_NC;
  16491. if (cert.extKeyUsage != (KEYUSE_DIGITAL_SIG|KEYUSE_CONTENT_COMMIT))
  16492. return WC_TEST_RET_ENC_NC;
  16493. /* check the CA Basic Constraints from an ECC certificate */
  16494. if (cert.isCA)
  16495. return WC_TEST_RET_ENC_NC;
  16496. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  16497. /* check the Certificate Policies Id */
  16498. if (cert.extCertPoliciesNb != 2)
  16499. return WC_TEST_RET_ENC_NC;
  16500. if (strncmp(cert.extCertPolicies[0], "2.4.589440.587.101.2.1.9632587.1", 32))
  16501. return WC_TEST_RET_ENC_NC;
  16502. if (strncmp(cert.extCertPolicies[1], "1.2.13025.489.1.113549", 22))
  16503. return WC_TEST_RET_ENC_NC;
  16504. #endif
  16505. FreeDecodedCert(&cert);
  16506. #endif /* HAVE_ECC */
  16507. /* load cert.der (self signed certificate) */
  16508. file = XFOPEN(certDerFile, "rb");
  16509. if (!file) {
  16510. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16511. return WC_TEST_RET_ENC_ERRNO;
  16512. }
  16513. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16514. XFCLOSE(file);
  16515. if (bytes == 0)
  16516. return WC_TEST_RET_ENC_ERRNO;
  16517. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  16518. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  16519. if (ret != 0)
  16520. return WC_TEST_RET_ENC_EC(ret);
  16521. /* check the SKID from a CA certificate */
  16522. if (XMEMCMP(kid_ca, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  16523. return WC_TEST_RET_ENC_NC;
  16524. /* check the AKID from an CA certificate */
  16525. if (XMEMCMP(kid_ca, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  16526. return WC_TEST_RET_ENC_NC;
  16527. /* check the Key Usage from CA certificate */
  16528. if (!cert.extKeyUsageSet)
  16529. return WC_TEST_RET_ENC_NC;
  16530. if (cert.extKeyUsage != (KEYUSE_KEY_CERT_SIGN|KEYUSE_CRL_SIGN))
  16531. return WC_TEST_RET_ENC_NC;
  16532. /* check the CA Basic Constraints CA certificate */
  16533. if (!cert.isCA)
  16534. return WC_TEST_RET_ENC_NC;
  16535. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  16536. /* check the Certificate Policies Id */
  16537. if (cert.extCertPoliciesNb != 2)
  16538. return WC_TEST_RET_ENC_NC;
  16539. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  16540. return WC_TEST_RET_ENC_NC;
  16541. if (strncmp(cert.extCertPolicies[1], "1.2.840.113549.1.9.16.6.5", 25))
  16542. return WC_TEST_RET_ENC_NC;
  16543. #endif
  16544. FreeDecodedCert(&cert);
  16545. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16546. return 0;
  16547. }
  16548. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_TEST_CERT &&
  16549. !NO_FILESYSTEM && WOLFSSL_CERT_GEN */
  16550. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  16551. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  16552. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t decodedCertCache_test(void)
  16553. {
  16554. wc_test_ret_t ret = 0;
  16555. Cert cert;
  16556. FILE* file;
  16557. byte* der;
  16558. word32 derSz;
  16559. WOLFSSL_ENTER("decodedCertCache_test");
  16560. derSz = FOURK_BUF;
  16561. der = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16562. if (der == NULL)
  16563. ret = WC_TEST_RET_ENC_NC;
  16564. if (ret == 0) {
  16565. /* load cert.der */
  16566. file = XFOPEN(certDerFile, "rb");
  16567. if (file != NULL) {
  16568. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  16569. XFCLOSE(file);
  16570. if (derSz == 0)
  16571. ret = WC_TEST_RET_ENC_ERRNO;
  16572. }
  16573. else
  16574. ret = WC_TEST_RET_ENC_ERRNO;
  16575. }
  16576. if (ret == 0) {
  16577. ret = wc_InitCert_ex(&cert, HEAP_HINT, devId);
  16578. if (ret != 0)
  16579. ret = WC_TEST_RET_ENC_EC(ret);
  16580. }
  16581. if (ret == 0) {
  16582. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  16583. if (ret != 0)
  16584. ret = WC_TEST_RET_ENC_EC(ret);
  16585. }
  16586. if (ret == 0) {
  16587. ret = wc_SetSubjectBuffer(NULL, der, derSz);
  16588. if (ret == BAD_FUNC_ARG)
  16589. ret = 0;
  16590. else
  16591. ret = WC_TEST_RET_ENC_EC(ret);
  16592. }
  16593. if (ret == 0) {
  16594. ret = wc_SetSubjectRaw(&cert, der, derSz);
  16595. if (ret != 0)
  16596. ret = WC_TEST_RET_ENC_EC(ret);
  16597. }
  16598. if (ret == 0) {
  16599. ret = wc_SetSubjectRaw(NULL, der, derSz);
  16600. if (ret == BAD_FUNC_ARG)
  16601. ret = 0;
  16602. else
  16603. ret = WC_TEST_RET_ENC_EC(ret);
  16604. }
  16605. if (ret == 0) {
  16606. ret = wc_SetIssuerBuffer(&cert, der, derSz);
  16607. if (ret != 0)
  16608. ret = WC_TEST_RET_ENC_EC(ret);
  16609. }
  16610. if (ret == 0) {
  16611. ret = wc_SetIssuerBuffer(NULL, der, derSz);
  16612. if (ret == BAD_FUNC_ARG)
  16613. ret = 0;
  16614. else
  16615. ret = WC_TEST_RET_ENC_EC(ret);
  16616. }
  16617. if (ret == 0) {
  16618. ret = wc_SetIssuerRaw(&cert, der, derSz);
  16619. if (ret != 0)
  16620. ret = WC_TEST_RET_ENC_EC(ret);
  16621. }
  16622. if (ret == 0) {
  16623. ret = wc_SetIssuerRaw(NULL, der, derSz);
  16624. if (ret == BAD_FUNC_ARG)
  16625. ret = 0;
  16626. else
  16627. ret = WC_TEST_RET_ENC_EC(ret);
  16628. }
  16629. #ifdef WOLFSSL_ALT_NAMES
  16630. if (ret == 0) {
  16631. ret = wc_SetAltNamesBuffer(&cert, der, derSz);
  16632. if (ret != 0)
  16633. ret = WC_TEST_RET_ENC_EC(ret);
  16634. }
  16635. if (ret == 0) {
  16636. ret = wc_SetAltNamesBuffer(NULL, der, derSz);
  16637. if (ret == BAD_FUNC_ARG)
  16638. ret = 0;
  16639. else
  16640. ret = WC_TEST_RET_ENC_EC(ret);
  16641. }
  16642. if (ret == 0) {
  16643. ret = wc_SetDatesBuffer(&cert, der, derSz);
  16644. if (ret != 0)
  16645. ret = WC_TEST_RET_ENC_EC(ret);
  16646. }
  16647. if (ret == 0) {
  16648. ret = wc_SetDatesBuffer(NULL, der, derSz);
  16649. if (ret == BAD_FUNC_ARG)
  16650. ret = 0;
  16651. else
  16652. ret = WC_TEST_RET_ENC_EC(ret);
  16653. }
  16654. #endif
  16655. if (ret == 0) {
  16656. ret = wc_SetAuthKeyIdFromCert(&cert, der, derSz);
  16657. if (ret != 0)
  16658. ret = WC_TEST_RET_ENC_EC(ret);
  16659. }
  16660. if (ret == 0) {
  16661. ret = wc_SetAuthKeyIdFromCert(NULL, der, derSz);
  16662. if (ret == BAD_FUNC_ARG)
  16663. ret = 0;
  16664. else
  16665. ret = WC_TEST_RET_ENC_NC;
  16666. }
  16667. wc_SetCert_Free(&cert);
  16668. if (ret == 0) {
  16669. if(cert.decodedCert != NULL)
  16670. ret = WC_TEST_RET_ENC_NC;
  16671. }
  16672. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16673. return ret;
  16674. }
  16675. #endif /* defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) &&
  16676. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) */
  16677. #define RSA_TEST_BYTES 512 /* up to 4096-bit key */
  16678. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  16679. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  16680. static wc_test_ret_t rsa_flatten_test(RsaKey* key)
  16681. {
  16682. wc_test_ret_t ret;
  16683. byte e[RSA_TEST_BYTES];
  16684. byte n[RSA_TEST_BYTES];
  16685. word32 eSz = sizeof(e);
  16686. word32 nSz = sizeof(n);
  16687. /* Parameter Validation testing. */
  16688. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  16689. if (ret != BAD_FUNC_ARG)
  16690. return WC_TEST_RET_ENC_EC(ret);
  16691. ret = wc_RsaFlattenPublicKey(key, NULL, &eSz, n, &nSz);
  16692. if (ret != BAD_FUNC_ARG)
  16693. return WC_TEST_RET_ENC_EC(ret);
  16694. ret = wc_RsaFlattenPublicKey(key, e, NULL, n, &nSz);
  16695. if (ret != BAD_FUNC_ARG)
  16696. return WC_TEST_RET_ENC_EC(ret);
  16697. ret = wc_RsaFlattenPublicKey(key, e, &eSz, NULL, &nSz);
  16698. if (ret != BAD_FUNC_ARG)
  16699. return WC_TEST_RET_ENC_EC(ret);
  16700. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, NULL);
  16701. if (ret != BAD_FUNC_ARG)
  16702. return WC_TEST_RET_ENC_EC(ret);
  16703. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  16704. if (ret != 0)
  16705. return WC_TEST_RET_ENC_EC(ret);
  16706. eSz = 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. eSz = sizeof(e);
  16711. nSz = 0;
  16712. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  16713. if (ret != RSA_BUFFER_E)
  16714. return WC_TEST_RET_ENC_EC(ret);
  16715. return 0;
  16716. }
  16717. #endif /* NO_ASN */
  16718. #if !defined(HAVE_FIPS) && !defined(NO_ASN) \
  16719. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  16720. static wc_test_ret_t rsa_export_key_test(RsaKey* key)
  16721. {
  16722. wc_test_ret_t ret;
  16723. byte e[3];
  16724. word32 eSz = sizeof(e);
  16725. byte n[RSA_TEST_BYTES];
  16726. word32 nSz = sizeof(n);
  16727. byte d[RSA_TEST_BYTES];
  16728. word32 dSz = sizeof(d);
  16729. byte p[RSA_TEST_BYTES/2];
  16730. word32 pSz = sizeof(p);
  16731. byte q[RSA_TEST_BYTES/2];
  16732. word32 qSz = sizeof(q);
  16733. word32 zero = 0;
  16734. ret = wc_RsaExportKey(NULL, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  16735. if (ret != BAD_FUNC_ARG)
  16736. return WC_TEST_RET_ENC_EC(ret);
  16737. ret = wc_RsaExportKey(key, NULL, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  16738. if (ret != BAD_FUNC_ARG)
  16739. return WC_TEST_RET_ENC_EC(ret);
  16740. ret = wc_RsaExportKey(key, e, NULL, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  16741. if (ret != BAD_FUNC_ARG)
  16742. return WC_TEST_RET_ENC_EC(ret);
  16743. ret = wc_RsaExportKey(key, e, &eSz, NULL, &nSz, d, &dSz, p, &pSz, q, &qSz);
  16744. if (ret != BAD_FUNC_ARG)
  16745. return WC_TEST_RET_ENC_EC(ret);
  16746. ret = wc_RsaExportKey(key, e, &eSz, n, NULL, d, &dSz, p, &pSz, q, &qSz);
  16747. if (ret != BAD_FUNC_ARG)
  16748. return WC_TEST_RET_ENC_EC(ret);
  16749. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, NULL, &dSz, p, &pSz, q, &qSz);
  16750. if (ret != BAD_FUNC_ARG)
  16751. return WC_TEST_RET_ENC_EC(ret);
  16752. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, NULL, p, &pSz, q, &qSz);
  16753. if (ret != BAD_FUNC_ARG)
  16754. return WC_TEST_RET_ENC_EC(ret);
  16755. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, NULL, &pSz, q, &qSz);
  16756. if (ret != BAD_FUNC_ARG)
  16757. return WC_TEST_RET_ENC_EC(ret);
  16758. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, NULL, q, &qSz);
  16759. if (ret != BAD_FUNC_ARG)
  16760. return WC_TEST_RET_ENC_EC(ret);
  16761. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, NULL, &qSz);
  16762. if (ret != BAD_FUNC_ARG)
  16763. return WC_TEST_RET_ENC_EC(ret);
  16764. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, NULL);
  16765. if (ret != BAD_FUNC_ARG)
  16766. return WC_TEST_RET_ENC_EC(ret);
  16767. ret = wc_RsaExportKey(key, e, &zero, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  16768. if (ret != RSA_BUFFER_E)
  16769. return WC_TEST_RET_ENC_EC(ret);
  16770. ret = wc_RsaExportKey(key, e, &eSz, n, &zero, d, &dSz, p, &pSz, q, &qSz);
  16771. if (ret != RSA_BUFFER_E)
  16772. return WC_TEST_RET_ENC_EC(ret);
  16773. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16774. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &zero, p, &pSz, q, &qSz);
  16775. if (ret != RSA_BUFFER_E)
  16776. return WC_TEST_RET_ENC_EC(ret);
  16777. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &zero, q, &qSz);
  16778. if (ret != RSA_BUFFER_E)
  16779. return WC_TEST_RET_ENC_EC(ret);
  16780. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &zero);
  16781. if (ret != RSA_BUFFER_E)
  16782. return WC_TEST_RET_ENC_EC(ret);
  16783. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  16784. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  16785. if (ret != 0)
  16786. return WC_TEST_RET_ENC_EC(ret);
  16787. return 0;
  16788. }
  16789. #endif /* !HAVE_FIPS && !NO_ASN && !WOLFSSL_RSA_VERIFY_ONLY */
  16790. #ifndef NO_SIG_WRAPPER
  16791. static wc_test_ret_t rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng)
  16792. {
  16793. wc_test_ret_t ret;
  16794. word32 sigSz;
  16795. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  16796. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  16797. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  16798. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  16799. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  16800. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  16801. };
  16802. WOLFSSL_SMALL_STACK_STATIC const byte hashEnc[] = {
  16803. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  16804. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  16805. 0x00, 0x04, 0x20,
  16806. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  16807. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  16808. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  16809. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  16810. };
  16811. word32 inLen = (word32)XSTRLEN((char*)in);
  16812. byte out[RSA_TEST_BYTES];
  16813. /* Parameter Validation testing. */
  16814. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_NONE, key, keyLen);
  16815. if (ret != BAD_FUNC_ARG)
  16816. return WC_TEST_RET_ENC_EC(ret);
  16817. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, 0);
  16818. if (ret != BAD_FUNC_ARG)
  16819. return WC_TEST_RET_ENC_EC(ret);
  16820. sigSz = (word32)modLen;
  16821. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  16822. inLen, out, &sigSz, key, keyLen, rng);
  16823. if (ret != BAD_FUNC_ARG)
  16824. return WC_TEST_RET_ENC_EC(ret);
  16825. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16826. 0, out, &sigSz, key, keyLen, rng);
  16827. if (ret != BAD_FUNC_ARG)
  16828. return WC_TEST_RET_ENC_EC(ret);
  16829. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16830. inLen, NULL, &sigSz, key, keyLen, rng);
  16831. if (ret != BAD_FUNC_ARG)
  16832. return WC_TEST_RET_ENC_EC(ret);
  16833. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16834. inLen, out, NULL, key, keyLen, rng);
  16835. if (ret != BAD_FUNC_ARG)
  16836. return WC_TEST_RET_ENC_EC(ret);
  16837. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16838. inLen, out, &sigSz, NULL, keyLen, rng);
  16839. if (ret != BAD_FUNC_ARG)
  16840. return WC_TEST_RET_ENC_EC(ret);
  16841. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16842. inLen, out, &sigSz, key, 0, rng);
  16843. if (ret != BAD_FUNC_ARG)
  16844. return WC_TEST_RET_ENC_EC(ret);
  16845. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16846. inLen, out, &sigSz, key, keyLen, NULL);
  16847. #if defined(WOLFSSL_AFALG_XILINX_RSA) || defined(WOLFSSL_XILINX_CRYPT)
  16848. /* blinding / rng handled with hardware acceleration */
  16849. if (ret != 0)
  16850. #elif defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  16851. /* async may not require RNG */
  16852. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  16853. if (ret != NO_VALID_DEVID)
  16854. #else
  16855. if (ret != 0 && ret != MISSING_RNG_E)
  16856. #endif
  16857. #elif defined(HAVE_FIPS) || !defined(WC_RSA_BLINDING)
  16858. /* FIPS140 implementation does not do blinding */
  16859. if (ret != 0)
  16860. #elif defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_RSA_VERIFY_ONLY)
  16861. if (ret != SIG_TYPE_E)
  16862. #elif defined(WOLFSSL_CRYPTOCELL) || defined(WOLFSSL_SE050)
  16863. /* RNG is handled by hardware */
  16864. if (ret != 0)
  16865. #else
  16866. if (ret != MISSING_RNG_E)
  16867. #endif
  16868. return WC_TEST_RET_ENC_EC(ret);
  16869. sigSz = 0;
  16870. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16871. inLen, out, &sigSz, key, keyLen, rng);
  16872. if (ret != BAD_FUNC_ARG)
  16873. return WC_TEST_RET_ENC_EC(ret);
  16874. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  16875. inLen, out, (word32)modLen, key, keyLen);
  16876. if (ret != BAD_FUNC_ARG)
  16877. return WC_TEST_RET_ENC_EC(ret);
  16878. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16879. 0, out, (word32)modLen, key, keyLen);
  16880. if (ret != BAD_FUNC_ARG)
  16881. return WC_TEST_RET_ENC_EC(ret);
  16882. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16883. inLen, NULL, (word32)modLen, key, keyLen);
  16884. if (ret != BAD_FUNC_ARG)
  16885. return WC_TEST_RET_ENC_EC(ret);
  16886. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16887. inLen, out, 0, key, keyLen);
  16888. if (ret != BAD_FUNC_ARG)
  16889. return WC_TEST_RET_ENC_EC(ret);
  16890. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16891. inLen, out, (word32)modLen, NULL, keyLen);
  16892. if (ret != BAD_FUNC_ARG)
  16893. return WC_TEST_RET_ENC_EC(ret);
  16894. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16895. inLen, out, (word32)modLen, key, 0);
  16896. if (ret != BAD_FUNC_ARG)
  16897. return WC_TEST_RET_ENC_EC(ret);
  16898. #ifndef HAVE_ECC
  16899. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, keyLen);
  16900. if (ret != SIG_TYPE_E)
  16901. return WC_TEST_RET_ENC_EC(ret);
  16902. #endif
  16903. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  16904. return 0;
  16905. #endif
  16906. /* Use APIs. */
  16907. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, keyLen);
  16908. if (ret != modLen)
  16909. return WC_TEST_RET_ENC_EC(ret);
  16910. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA_W_ENC, key, keyLen);
  16911. if (ret != modLen)
  16912. return WC_TEST_RET_ENC_EC(ret);
  16913. sigSz = (word32)ret;
  16914. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  16915. XMEMSET(out, 0, sizeof(out));
  16916. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16917. inLen, out, &sigSz, key, keyLen, rng);
  16918. if (ret != 0)
  16919. return WC_TEST_RET_ENC_EC(ret);
  16920. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16921. inLen, out, (word32)modLen, key, keyLen);
  16922. if (ret != 0)
  16923. return WC_TEST_RET_ENC_EC(ret);
  16924. sigSz = (word32)sizeof(out);
  16925. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  16926. in, inLen, out, &sigSz, key, keyLen, rng);
  16927. if (ret != 0)
  16928. return WC_TEST_RET_ENC_EC(ret);
  16929. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  16930. in, inLen, out, (word32)modLen, key, keyLen);
  16931. if (ret != 0)
  16932. return WC_TEST_RET_ENC_EC(ret);
  16933. /* Wrong signature type. */
  16934. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  16935. inLen, out, (word32)modLen, key, keyLen);
  16936. if (ret == 0)
  16937. return WC_TEST_RET_ENC_EC(ret);
  16938. /* check hash functions */
  16939. sigSz = (word32)sizeof(out);
  16940. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  16941. hash, (int)sizeof(hash), out, &sigSz, key, keyLen, rng);
  16942. if (ret != 0)
  16943. return WC_TEST_RET_ENC_EC(ret);
  16944. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  16945. hash, (int)sizeof(hash), out, (word32)modLen, key, keyLen);
  16946. if (ret != 0)
  16947. return WC_TEST_RET_ENC_EC(ret);
  16948. sigSz = (word32)sizeof(out);
  16949. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  16950. hashEnc, (int)sizeof(hashEnc), out, &sigSz, key, keyLen, rng);
  16951. if (ret != 0)
  16952. return WC_TEST_RET_ENC_EC(ret);
  16953. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  16954. hashEnc, (int)sizeof(hashEnc), out, (word32)modLen, key, keyLen);
  16955. if (ret != 0)
  16956. return WC_TEST_RET_ENC_EC(ret);
  16957. #else
  16958. (void)hash;
  16959. (void)hashEnc;
  16960. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */
  16961. return 0;
  16962. }
  16963. #endif /* !NO_SIG_WRAPPER */
  16964. #ifdef WC_RSA_NONBLOCK
  16965. static wc_test_ret_t rsa_nb_test(RsaKey* key, const byte* in, word32 inLen, byte* out,
  16966. word32 outSz, byte* plain, word32 plainSz, WC_RNG* rng)
  16967. {
  16968. wc_test_ret_t ret = 0;
  16969. int count;
  16970. int signSz = 0;
  16971. RsaNb nb;
  16972. byte* inlinePlain = NULL;
  16973. /* Enable non-blocking RSA mode - provide context */
  16974. ret = wc_RsaSetNonBlock(key, &nb);
  16975. if (ret != 0)
  16976. return ret;
  16977. #ifdef WC_RSA_NONBLOCK_TIME
  16978. /* Enable time based RSA blocking. 8 microseconds max (3.1GHz) */
  16979. ret = wc_RsaSetNonBlockTime(key, 8, 3100);
  16980. if (ret != 0)
  16981. return ret;
  16982. #endif
  16983. count = 0;
  16984. do {
  16985. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, rng);
  16986. count++; /* track number of would blocks */
  16987. if (ret == FP_WOULDBLOCK) {
  16988. /* do "other" work here */
  16989. }
  16990. } while (ret == FP_WOULDBLOCK);
  16991. if (ret < 0) {
  16992. return ret;
  16993. }
  16994. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  16995. printf("RSA non-block sign: %d times\n", count);
  16996. #endif
  16997. signSz = ret;
  16998. /* Test non-blocking verify */
  16999. XMEMSET(plain, 0, plainSz);
  17000. count = 0;
  17001. do {
  17002. ret = wc_RsaSSL_Verify(out, (word32)signSz, plain, plainSz, key);
  17003. count++; /* track number of would blocks */
  17004. if (ret == FP_WOULDBLOCK) {
  17005. /* do "other" work here */
  17006. }
  17007. } while (ret == FP_WOULDBLOCK);
  17008. if (ret < 0) {
  17009. return ret;
  17010. }
  17011. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  17012. printf("RSA non-block verify: %d times\n", count);
  17013. #endif
  17014. if (signSz == ret && XMEMCMP(plain, in, (size_t)ret)) {
  17015. return SIG_VERIFY_E;
  17016. }
  17017. /* Test inline non-blocking verify */
  17018. count = 0;
  17019. do {
  17020. ret = wc_RsaSSL_VerifyInline(out, (word32)signSz, &inlinePlain, key);
  17021. count++; /* track number of would blocks */
  17022. if (ret == FP_WOULDBLOCK) {
  17023. /* do "other" work here */
  17024. }
  17025. } while (ret == FP_WOULDBLOCK);
  17026. if (ret < 0) {
  17027. return ret;
  17028. }
  17029. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  17030. printf("RSA non-block inline verify: %d times\n", count);
  17031. #endif
  17032. if (signSz == ret && XMEMCMP(inlinePlain, in, (size_t)ret)) {
  17033. return SIG_VERIFY_E;
  17034. }
  17035. /* Disabling non-block RSA mode */
  17036. ret = wc_RsaSetNonBlock(key, NULL);
  17037. (void)count;
  17038. return 0;
  17039. }
  17040. #endif
  17041. #if !defined(NO_ASN)
  17042. static wc_test_ret_t rsa_decode_test(RsaKey* keyPub)
  17043. {
  17044. wc_test_ret_t ret;
  17045. word32 inSz;
  17046. word32 inOutIdx;
  17047. WOLFSSL_SMALL_STACK_STATIC const byte n[2] = { 0x00, 0x23 };
  17048. WOLFSSL_SMALL_STACK_STATIC const byte e[2] = { 0x00, 0x03 };
  17049. WOLFSSL_SMALL_STACK_STATIC const byte good[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1,
  17050. 0x03 };
  17051. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgId[] = {
  17052. 0x30, 0x18, 0x30, 0x16,
  17053. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17054. 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17055. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgIdNull[] = {
  17056. 0x30, 0x1a, 0x30, 0x18,
  17057. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17058. 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  17059. 0x02, 0x1, 0x03 };
  17060. WOLFSSL_SMALL_STACK_STATIC const byte badAlgIdNull[] = {
  17061. 0x30, 0x1b, 0x30, 0x19,
  17062. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17063. 0x05, 0x01, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  17064. 0x02, 0x1, 0x03 };
  17065. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitString[] = {
  17066. 0x30, 0x18, 0x30, 0x16,
  17067. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17068. 0x04, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17069. WOLFSSL_SMALL_STACK_STATIC const byte badBitStringLen[] = {
  17070. 0x30, 0x18, 0x30, 0x16,
  17071. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17072. 0x03, 0x0a, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17073. WOLFSSL_SMALL_STACK_STATIC const byte badNoSeq[] = {
  17074. 0x30, 0x16, 0x30, 0x14,
  17075. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17076. 0x07, 0x00, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17077. WOLFSSL_SMALL_STACK_STATIC const byte badNoObj[] = {
  17078. 0x30, 0x0f, 0x30, 0x0d, 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06,
  17079. 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17080. WOLFSSL_SMALL_STACK_STATIC const byte badIntN[] = {
  17081. 0x30, 0x06, 0x02, 0x05, 0x23, 0x02, 0x1, 0x03 };
  17082. WOLFSSL_SMALL_STACK_STATIC const byte badNotIntE[] = {
  17083. 0x30, 0x06, 0x02, 0x01, 0x23, 0x04, 0x1, 0x03 };
  17084. WOLFSSL_SMALL_STACK_STATIC const byte badLength[] = {
  17085. 0x30, 0x04, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17086. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrNoZero[] = {
  17087. 0x30, 0x17, 0x30, 0x15,
  17088. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  17089. 0x03, 0x08, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  17090. ret = wc_InitRsaKey(keyPub, NULL);
  17091. if (ret != 0)
  17092. return WC_TEST_RET_ENC_EC(ret);
  17093. /* Parameter Validation testing. */
  17094. ret = wc_RsaPublicKeyDecodeRaw(NULL, sizeof(n), e, 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), NULL, sizeof(e), keyPub);
  17100. if (ret != BAD_FUNC_ARG) {
  17101. ret = WC_TEST_RET_ENC_EC(ret);
  17102. goto done;
  17103. }
  17104. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), NULL);
  17105. if (ret != BAD_FUNC_ARG) {
  17106. ret = WC_TEST_RET_ENC_EC(ret);
  17107. goto done;
  17108. }
  17109. ret = wc_RsaPublicKeyDecodeRaw(n, (word32)-1, e, sizeof(e), keyPub);
  17110. #if defined(USE_INTEGER_HEAP_MATH)
  17111. if (ret != 0)
  17112. #else
  17113. if (ret != ASN_GETINT_E)
  17114. #endif
  17115. {
  17116. ret = WC_TEST_RET_ENC_EC(ret);
  17117. goto done;
  17118. }
  17119. wc_FreeRsaKey(keyPub);
  17120. ret = wc_InitRsaKey(keyPub, NULL);
  17121. if (ret != 0)
  17122. return WC_TEST_RET_ENC_EC(ret);
  17123. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, (word32)-1, keyPub);
  17124. #if defined(USE_INTEGER_HEAP_MATH)
  17125. if (ret != 0)
  17126. #else
  17127. if (ret != ASN_GETINT_E)
  17128. #endif
  17129. {
  17130. ret = WC_TEST_RET_ENC_EC(ret);
  17131. goto done;
  17132. }
  17133. wc_FreeRsaKey(keyPub);
  17134. ret = wc_InitRsaKey(keyPub, NULL);
  17135. if (ret != 0)
  17136. return WC_TEST_RET_ENC_EC(ret);
  17137. /* Use API. */
  17138. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), keyPub);
  17139. if (ret != 0) {
  17140. ret = WC_TEST_RET_ENC_EC(ret);
  17141. goto done;
  17142. }
  17143. wc_FreeRsaKey(keyPub);
  17144. ret = wc_InitRsaKey(keyPub, NULL);
  17145. if (ret != 0)
  17146. return WC_TEST_RET_ENC_EC(ret);
  17147. /* Parameter Validation testing. */
  17148. inSz = sizeof(good);
  17149. ret = wc_RsaPublicKeyDecode(NULL, &inOutIdx, 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, NULL, keyPub, inSz);
  17155. if (ret != BAD_FUNC_ARG) {
  17156. ret = WC_TEST_RET_ENC_EC(ret);
  17157. goto done;
  17158. }
  17159. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  17160. if (ret != BAD_FUNC_ARG) {
  17161. ret = WC_TEST_RET_ENC_EC(ret);
  17162. goto done;
  17163. }
  17164. /* Use good data and offset to bad data. */
  17165. inOutIdx = 2;
  17166. inSz = sizeof(good) - inOutIdx;
  17167. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  17168. if (ret != ASN_PARSE_E) {
  17169. ret = WC_TEST_RET_ENC_EC(ret);
  17170. goto done;
  17171. }
  17172. inOutIdx = 2;
  17173. inSz = sizeof(goodAlgId) - inOutIdx;
  17174. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  17175. if (ret != ASN_PARSE_E) {
  17176. ret = WC_TEST_RET_ENC_EC(ret);
  17177. goto done;
  17178. }
  17179. inOutIdx = 2;
  17180. inSz = sizeof(goodAlgId);
  17181. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  17182. #ifndef WOLFSSL_NO_DECODE_EXTRA
  17183. if (ret != ASN_PARSE_E)
  17184. #else
  17185. if (ret != ASN_RSA_KEY_E)
  17186. #endif
  17187. {
  17188. ret = WC_TEST_RET_ENC_EC(ret);
  17189. goto done;
  17190. }
  17191. /* Try different bad data. */
  17192. inSz = sizeof(badAlgIdNull);
  17193. inOutIdx = 0;
  17194. ret = wc_RsaPublicKeyDecode(badAlgIdNull, &inOutIdx, keyPub, inSz);
  17195. if (ret != ASN_EXPECT_0_E) {
  17196. ret = WC_TEST_RET_ENC_EC(ret);
  17197. goto done;
  17198. }
  17199. inSz = sizeof(badNotBitString);
  17200. inOutIdx = 0;
  17201. ret = wc_RsaPublicKeyDecode(badNotBitString, &inOutIdx, keyPub, inSz);
  17202. if (ret != ASN_BITSTR_E) {
  17203. ret = WC_TEST_RET_ENC_EC(ret);
  17204. goto done;
  17205. }
  17206. inSz = sizeof(badBitStringLen);
  17207. inOutIdx = 0;
  17208. ret = wc_RsaPublicKeyDecode(badBitStringLen, &inOutIdx, keyPub, inSz);
  17209. if (ret != ASN_PARSE_E) {
  17210. ret = WC_TEST_RET_ENC_EC(ret);
  17211. goto done;
  17212. }
  17213. inSz = sizeof(badNoSeq);
  17214. inOutIdx = 0;
  17215. ret = wc_RsaPublicKeyDecode(badNoSeq, &inOutIdx, keyPub, inSz);
  17216. if (ret != ASN_PARSE_E) {
  17217. ret = WC_TEST_RET_ENC_EC(ret);
  17218. goto done;
  17219. }
  17220. inSz = sizeof(badNoObj);
  17221. inOutIdx = 0;
  17222. ret = wc_RsaPublicKeyDecode(badNoObj, &inOutIdx, keyPub, inSz);
  17223. if (ret != ASN_PARSE_E && ret != ASN_OBJECT_ID_E) {
  17224. ret = WC_TEST_RET_ENC_EC(ret);
  17225. goto done;
  17226. }
  17227. inSz = sizeof(badIntN);
  17228. inOutIdx = 0;
  17229. ret = wc_RsaPublicKeyDecode(badIntN, &inOutIdx, keyPub, inSz);
  17230. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  17231. ret = WC_TEST_RET_ENC_EC(ret);
  17232. goto done;
  17233. }
  17234. inSz = sizeof(badNotIntE);
  17235. inOutIdx = 0;
  17236. ret = wc_RsaPublicKeyDecode(badNotIntE, &inOutIdx, keyPub, inSz);
  17237. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  17238. ret = WC_TEST_RET_ENC_EC(ret);
  17239. goto done;
  17240. }
  17241. /* TODO: Shouldn't pass as the sequence length is too small. */
  17242. inSz = sizeof(badLength);
  17243. inOutIdx = 0;
  17244. ret = wc_RsaPublicKeyDecode(badLength, &inOutIdx, keyPub, inSz);
  17245. #ifndef WOLFSSL_ASN_TEMPLATE
  17246. if (ret != 0)
  17247. #else
  17248. if (ret != ASN_PARSE_E)
  17249. #endif
  17250. {
  17251. ret = WC_TEST_RET_ENC_EC(ret);
  17252. goto done;
  17253. }
  17254. /* TODO: Shouldn't ignore object id's data. */
  17255. wc_FreeRsaKey(keyPub);
  17256. ret = wc_InitRsaKey(keyPub, NULL);
  17257. if (ret != 0)
  17258. return WC_TEST_RET_ENC_EC(ret);
  17259. inSz = sizeof(badBitStrNoZero);
  17260. inOutIdx = 0;
  17261. ret = wc_RsaPublicKeyDecode(badBitStrNoZero, &inOutIdx, keyPub, inSz);
  17262. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  17263. ret = WC_TEST_RET_ENC_EC(ret);
  17264. goto done;
  17265. }
  17266. wc_FreeRsaKey(keyPub);
  17267. ret = wc_InitRsaKey(keyPub, NULL);
  17268. if (ret != 0)
  17269. return WC_TEST_RET_ENC_EC(ret);
  17270. /* Valid data cases. */
  17271. inSz = sizeof(good);
  17272. inOutIdx = 0;
  17273. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  17274. if (ret != 0) {
  17275. ret = WC_TEST_RET_ENC_EC(ret);
  17276. goto done;
  17277. }
  17278. if (inOutIdx != inSz) {
  17279. ret = WC_TEST_RET_ENC_NC;
  17280. goto done;
  17281. }
  17282. wc_FreeRsaKey(keyPub);
  17283. ret = wc_InitRsaKey(keyPub, NULL);
  17284. if (ret != 0)
  17285. return WC_TEST_RET_ENC_EC(ret);
  17286. inSz = sizeof(goodAlgId);
  17287. inOutIdx = 0;
  17288. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  17289. if (ret != 0) {
  17290. ret = WC_TEST_RET_ENC_EC(ret);
  17291. goto done;
  17292. }
  17293. if (inOutIdx != inSz) {
  17294. ret = WC_TEST_RET_ENC_NC;
  17295. goto done;
  17296. }
  17297. wc_FreeRsaKey(keyPub);
  17298. ret = wc_InitRsaKey(keyPub, NULL);
  17299. if (ret != 0)
  17300. return WC_TEST_RET_ENC_EC(ret);
  17301. inSz = sizeof(goodAlgIdNull);
  17302. inOutIdx = 0;
  17303. ret = wc_RsaPublicKeyDecode(goodAlgIdNull, &inOutIdx, keyPub, inSz);
  17304. if (ret != 0) {
  17305. ret = WC_TEST_RET_ENC_EC(ret);
  17306. goto done;
  17307. }
  17308. if (inOutIdx != inSz) {
  17309. ret = WC_TEST_RET_ENC_NC;
  17310. goto done;
  17311. }
  17312. done:
  17313. wc_FreeRsaKey(keyPub);
  17314. return ret;
  17315. }
  17316. #endif
  17317. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  17318. /* Need to create known good signatures to test with this. */
  17319. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  17320. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  17321. static wc_test_ret_t rsa_pss_test(WC_RNG* rng, RsaKey* key)
  17322. {
  17323. byte digest[WC_MAX_DIGEST_SIZE];
  17324. wc_test_ret_t ret = 0;
  17325. const char inStr[] = TEST_STRING;
  17326. word32 inLen = (word32)TEST_STRING_SZ;
  17327. word32 outSz;
  17328. word32 plainSz;
  17329. word32 digestSz;
  17330. int i, j;
  17331. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  17332. int k, l;
  17333. #endif
  17334. #ifndef WOLFSSL_SE050
  17335. int len;
  17336. #endif
  17337. byte* plain;
  17338. int mgf[] = {
  17339. #ifndef NO_SHA
  17340. WC_MGF1SHA1,
  17341. #endif
  17342. #ifdef WOLFSSL_SHA224
  17343. WC_MGF1SHA224,
  17344. #endif
  17345. WC_MGF1SHA256,
  17346. #ifdef WOLFSSL_SHA384
  17347. WC_MGF1SHA384,
  17348. #endif
  17349. #ifdef WOLFSSL_SHA512
  17350. WC_MGF1SHA512
  17351. #endif
  17352. };
  17353. enum wc_HashType hash[] = {
  17354. #ifndef NO_SHA
  17355. WC_HASH_TYPE_SHA,
  17356. #endif
  17357. #ifdef WOLFSSL_SHA224
  17358. WC_HASH_TYPE_SHA224,
  17359. #endif
  17360. WC_HASH_TYPE_SHA256,
  17361. #ifdef WOLFSSL_SHA384
  17362. WC_HASH_TYPE_SHA384,
  17363. #endif
  17364. #ifdef WOLFSSL_SHA512
  17365. WC_HASH_TYPE_SHA512,
  17366. #endif
  17367. };
  17368. WC_DECLARE_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
  17369. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17370. WC_DECLARE_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  17371. WC_ALLOC_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
  17372. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17373. WC_ALLOC_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  17374. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17375. if (in == NULL || out == NULL || sig == NULL)
  17376. ERROR_OUT(MEMORY_E, exit_rsa_pss);
  17377. #endif
  17378. XMEMCPY(in, inStr, inLen);
  17379. /* Test all combinations of hash and MGF. */
  17380. for (j = 0; j < (int)(sizeof(hash)/sizeof(*hash)); j++) {
  17381. /* Calculate hash of message. */
  17382. ret = wc_Hash(hash[j], in, inLen, digest, sizeof(digest));
  17383. if (ret != 0)
  17384. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17385. digestSz = (word32)wc_HashGetDigestSize(hash[j]);
  17386. #ifdef WOLFSSL_SE050
  17387. /* SE050 only supports MGF matched to same hash type */
  17388. i = j;
  17389. #else
  17390. for (i = 0; i < (int)(sizeof(mgf)/sizeof(*mgf)); i++) {
  17391. #endif
  17392. outSz = RSA_TEST_BYTES;
  17393. do {
  17394. #if defined(WOLFSSL_ASYNC_CRYPT)
  17395. ret = wc_AsyncWait(ret, &key->asyncDev,
  17396. WC_ASYNC_FLAG_CALL_AGAIN);
  17397. #endif
  17398. if (ret >= 0) {
  17399. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz,
  17400. hash[j], mgf[i], -1, key, rng);
  17401. }
  17402. } while (ret == WC_PENDING_E);
  17403. if (ret <= 0)
  17404. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17405. outSz = (word32)ret;
  17406. XMEMCPY(sig, out, outSz);
  17407. plain = NULL;
  17408. TEST_SLEEP();
  17409. do {
  17410. #if defined(WOLFSSL_ASYNC_CRYPT)
  17411. ret = wc_AsyncWait(ret, &key->asyncDev,
  17412. WC_ASYNC_FLAG_CALL_AGAIN);
  17413. #endif
  17414. if (ret >= 0) {
  17415. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[j],
  17416. mgf[i], -1, key);
  17417. }
  17418. } while (ret == WC_PENDING_E);
  17419. if (ret <= 0)
  17420. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17421. plainSz = (word32)ret;
  17422. TEST_SLEEP();
  17423. #if defined(HAVE_SELFTEST) && \
  17424. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  17425. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17426. hash[j], -1);
  17427. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  17428. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17429. hash[j], -1, 0);
  17430. #else
  17431. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz,
  17432. hash[j], -1, wc_RsaEncryptSize(key)*8, HEAP_HINT);
  17433. #endif
  17434. if (ret != 0)
  17435. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17436. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  17437. for (k = 0; k < (int)(sizeof(mgf)/sizeof(*mgf)); k++) {
  17438. for (l = 0; l < (int)(sizeof(hash)/sizeof(*hash)); l++) {
  17439. if (i == k && j == l)
  17440. continue;
  17441. XMEMCPY(sig, out, outSz);
  17442. do {
  17443. #if defined(WOLFSSL_ASYNC_CRYPT)
  17444. ret = wc_AsyncWait(ret, &key->asyncDev,
  17445. WC_ASYNC_FLAG_CALL_AGAIN);
  17446. #endif
  17447. if (ret >= 0) {
  17448. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz,
  17449. (byte**)&plain, hash[l], mgf[k], -1, key);
  17450. }
  17451. } while (ret == WC_PENDING_E);
  17452. if (ret >= 0)
  17453. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17454. }
  17455. }
  17456. #endif
  17457. #ifndef WOLFSSL_SE050
  17458. } /* end mgf for loop */
  17459. #endif
  17460. }
  17461. /* SE050 generates salts internally only of hash length */
  17462. #ifndef WOLFSSL_SE050
  17463. /* Test that a salt length of zero works. */
  17464. digestSz = (word32)wc_HashGetDigestSize(hash[0]);
  17465. outSz = RSA_TEST_BYTES;
  17466. do {
  17467. #if defined(WOLFSSL_ASYNC_CRYPT)
  17468. ret = wc_AsyncWait(ret, &key->asyncDev,
  17469. WC_ASYNC_FLAG_CALL_AGAIN);
  17470. #endif
  17471. if (ret >= 0) {
  17472. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  17473. mgf[0], 0, key, rng);
  17474. }
  17475. } while (ret == WC_PENDING_E);
  17476. if (ret <= 0)
  17477. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17478. outSz = (word32)ret;
  17479. TEST_SLEEP();
  17480. do {
  17481. #if defined(WOLFSSL_ASYNC_CRYPT)
  17482. ret = wc_AsyncWait(ret, &key->asyncDev,
  17483. WC_ASYNC_FLAG_CALL_AGAIN);
  17484. #endif
  17485. if (ret >= 0) {
  17486. ret = wc_RsaPSS_Verify_ex(out, outSz, sig, outSz, hash[0], mgf[0],
  17487. 0, key);
  17488. }
  17489. } while (ret == WC_PENDING_E);
  17490. if (ret <= 0)
  17491. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17492. plainSz = (word32)ret;
  17493. TEST_SLEEP();
  17494. do {
  17495. #if defined(WOLFSSL_ASYNC_CRYPT)
  17496. ret = wc_AsyncWait(ret, &key->asyncDev,
  17497. WC_ASYNC_FLAG_CALL_AGAIN);
  17498. #endif
  17499. if (ret >= 0) {
  17500. #if defined(HAVE_SELFTEST) && \
  17501. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  17502. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  17503. hash[0], 0);
  17504. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  17505. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  17506. hash[0], 0, 0);
  17507. #else
  17508. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, sig, plainSz,
  17509. hash[0], 0, 0, HEAP_HINT);
  17510. #endif
  17511. }
  17512. } while (ret == WC_PENDING_E);
  17513. if (ret != 0)
  17514. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17515. XMEMCPY(sig, out, outSz);
  17516. plain = NULL;
  17517. do {
  17518. #if defined(WOLFSSL_ASYNC_CRYPT)
  17519. ret = wc_AsyncWait(ret, &key->asyncDev,
  17520. WC_ASYNC_FLAG_CALL_AGAIN);
  17521. #endif
  17522. if (ret >= 0) {
  17523. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  17524. 0, key);
  17525. }
  17526. } while (ret == WC_PENDING_E);
  17527. if (ret <= 0)
  17528. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17529. plainSz = (word32)ret;
  17530. TEST_SLEEP();
  17531. #if defined(HAVE_SELFTEST) && \
  17532. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  17533. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17534. hash[0], 0);
  17535. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  17536. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17537. hash[0], 0, 0);
  17538. #else
  17539. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  17540. 0, 0, HEAP_HINT);
  17541. #endif
  17542. if (ret != 0)
  17543. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17544. /* Test bad salt lengths in various APIs. */
  17545. digestSz = (word32)wc_HashGetDigestSize(hash[0]);
  17546. outSz = RSA_TEST_BYTES;
  17547. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  17548. len = -2;
  17549. #else
  17550. len = -3;
  17551. #endif
  17552. do {
  17553. #if defined(WOLFSSL_ASYNC_CRYPT)
  17554. ret = wc_AsyncWait(ret, &key->asyncDev,
  17555. WC_ASYNC_FLAG_CALL_AGAIN);
  17556. #endif
  17557. if (ret >= 0) {
  17558. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  17559. mgf[0], len, key, rng);
  17560. }
  17561. } while (ret == WC_PENDING_E);
  17562. if (ret != PSS_SALTLEN_E)
  17563. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17564. do {
  17565. #if defined(WOLFSSL_ASYNC_CRYPT)
  17566. ret = wc_AsyncWait(ret, &key->asyncDev,
  17567. WC_ASYNC_FLAG_CALL_AGAIN);
  17568. #endif
  17569. if (ret >= 0) {
  17570. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  17571. mgf[0], digestSz + 1, key, rng);
  17572. }
  17573. } while (ret == WC_PENDING_E);
  17574. if (ret != PSS_SALTLEN_E)
  17575. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17576. TEST_SLEEP();
  17577. do {
  17578. #if defined(WOLFSSL_ASYNC_CRYPT)
  17579. ret = wc_AsyncWait(ret, &key->asyncDev,
  17580. WC_ASYNC_FLAG_CALL_AGAIN);
  17581. #endif
  17582. if (ret >= 0) {
  17583. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0],
  17584. mgf[0], -2, key);
  17585. }
  17586. } while (ret == WC_PENDING_E);
  17587. if (ret != PSS_SALTLEN_E)
  17588. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17589. TEST_SLEEP();
  17590. do {
  17591. #if defined(WOLFSSL_ASYNC_CRYPT)
  17592. ret = wc_AsyncWait(ret, &key->asyncDev,
  17593. WC_ASYNC_FLAG_CALL_AGAIN);
  17594. #endif
  17595. if (ret >= 0) {
  17596. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  17597. digestSz + 1, key);
  17598. }
  17599. } while (ret == WC_PENDING_E);
  17600. if (ret != PSS_SALTLEN_E)
  17601. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17602. TEST_SLEEP();
  17603. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  17604. len = -2;
  17605. #else
  17606. len = -3;
  17607. #endif
  17608. #if defined(HAVE_SELFTEST) && \
  17609. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  17610. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17611. hash[0], len);
  17612. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  17613. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17614. hash[0], len, 0);
  17615. #else
  17616. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  17617. len, 0, HEAP_HINT);
  17618. #endif
  17619. if (ret != PSS_SALTLEN_E)
  17620. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17621. #ifndef WOLFSSL_PSS_LONG_SALT
  17622. len = (int)(digestSz + 1);
  17623. #else
  17624. len = (int)(plainSz - digestSz - 1);
  17625. #endif
  17626. #if defined(HAVE_SELFTEST) && \
  17627. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  17628. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17629. hash[0], len);
  17630. if (ret != PSS_SALTLEN_E)
  17631. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17632. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  17633. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  17634. hash[0], len, 0);
  17635. if (ret != BAD_PADDING_E)
  17636. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17637. #else
  17638. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  17639. len, 0, HEAP_HINT);
  17640. if (ret != PSS_SALTLEN_E)
  17641. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  17642. #endif
  17643. ret = 0;
  17644. #endif /* WOLFSSL_SE050 */
  17645. exit_rsa_pss:
  17646. WC_FREE_VAR(sig, HEAP_HINT);
  17647. WC_FREE_VAR(in, HEAP_HINT);
  17648. WC_FREE_VAR(out, HEAP_HINT);
  17649. return ret;
  17650. }
  17651. #endif /* !WOLFSSL_RSA_VERIFY_ONLY && !WOLFSSL_RSA_PUBLIC_ONLY */
  17652. #endif
  17653. #ifdef WC_RSA_NO_PADDING
  17654. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_no_pad_test(void)
  17655. {
  17656. WC_RNG rng;
  17657. byte* tmp = NULL;
  17658. size_t bytes;
  17659. wc_test_ret_t ret;
  17660. word32 inLen = 0;
  17661. word32 idx = 0;
  17662. word32 outSz = RSA_TEST_BYTES;
  17663. word32 plainSz = RSA_TEST_BYTES;
  17664. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  17665. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  17666. !defined(NO_FILESYSTEM)
  17667. XFILE file;
  17668. #endif
  17669. WC_DECLARE_VAR(key, RsaKey, 1, HEAP_HINT);
  17670. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17671. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  17672. WC_ALLOC_VAR(key, RsaKey, 1, HEAP_HINT);
  17673. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17674. WC_ALLOC_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  17675. WOLFSSL_ENTER("rsa_no_pad_test");
  17676. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17677. if (key == NULL || out == NULL || plain == NULL)
  17678. ERROR_OUT(MEMORY_E, exit_rsa_nopadding);
  17679. #endif
  17680. /* initialize stack structures */
  17681. XMEMSET(&rng, 0, sizeof(rng));
  17682. XMEMSET(key, 0, sizeof(RsaKey));
  17683. #ifdef USE_CERT_BUFFERS_1024
  17684. bytes = (size_t)sizeof_client_key_der_1024;
  17685. if (bytes < (size_t)sizeof_client_cert_der_1024)
  17686. bytes = (size_t)sizeof_client_cert_der_1024;
  17687. #elif defined(USE_CERT_BUFFERS_2048)
  17688. bytes = (size_t)sizeof_client_key_der_2048;
  17689. if (bytes < (size_t)sizeof_client_cert_der_2048)
  17690. bytes = (size_t)sizeof_client_cert_der_2048;
  17691. #else
  17692. bytes = FOURK_BUF;
  17693. #endif
  17694. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17695. if (tmp == NULL
  17696. #ifdef WOLFSSL_ASYNC_CRYPT
  17697. || out == NULL || plain == NULL
  17698. #endif
  17699. ) {
  17700. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  17701. }
  17702. #ifdef USE_CERT_BUFFERS_1024
  17703. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  17704. #elif defined(USE_CERT_BUFFERS_2048)
  17705. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  17706. #elif defined(USE_CERT_BUFFERS_3072)
  17707. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  17708. #elif defined(USE_CERT_BUFFERS_4096)
  17709. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  17710. #elif !defined(NO_FILESYSTEM)
  17711. file = XFOPEN(clientKey, "rb");
  17712. if (!file) {
  17713. err_sys("can't open clientKey, Please run from wolfSSL home dir",
  17714. WC_TEST_RET_ENC_ERRNO);
  17715. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_nopadding);
  17716. }
  17717. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  17718. XFCLOSE(file);
  17719. if (bytes == 0)
  17720. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_nopadding);
  17721. #else
  17722. /* No key to use. */
  17723. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  17724. #endif /* USE_CERT_BUFFERS */
  17725. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  17726. if (ret != 0) {
  17727. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17728. }
  17729. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  17730. if (ret != 0) {
  17731. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17732. }
  17733. /* after loading in key use tmp as the test buffer */
  17734. #ifndef HAVE_FIPS
  17735. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  17736. #else
  17737. ret = wc_InitRng(&rng);
  17738. #endif
  17739. if (ret != 0) {
  17740. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17741. }
  17742. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  17743. inLen = (word32)wc_RsaEncryptSize(key);
  17744. outSz = inLen;
  17745. plainSz = inLen;
  17746. XMEMSET(tmp, 7, inLen);
  17747. do {
  17748. #if defined(WOLFSSL_ASYNC_CRYPT)
  17749. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17750. #endif
  17751. if (ret >= 0) {
  17752. ret = wc_RsaDirect(tmp, inLen, out, &outSz, key,
  17753. RSA_PRIVATE_ENCRYPT, &rng);
  17754. }
  17755. } while (ret == WC_PENDING_E);
  17756. if (ret <= 0) {
  17757. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17758. }
  17759. /* encrypted result should not be the same as input */
  17760. if (XMEMCMP(out, tmp, inLen) == 0) {
  17761. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  17762. }
  17763. TEST_SLEEP();
  17764. /* decrypt with public key and compare result */
  17765. do {
  17766. #if defined(WOLFSSL_ASYNC_CRYPT)
  17767. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17768. #endif
  17769. if (ret >= 0) {
  17770. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key,
  17771. RSA_PUBLIC_DECRYPT, &rng);
  17772. }
  17773. } while (ret == WC_PENDING_E);
  17774. if (ret <= 0) {
  17775. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17776. }
  17777. if (XMEMCMP(plain, tmp, inLen) != 0) {
  17778. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  17779. }
  17780. TEST_SLEEP();
  17781. #endif
  17782. #ifdef WC_RSA_BLINDING
  17783. ret = wc_RsaSetRNG(NULL, &rng);
  17784. if (ret != BAD_FUNC_ARG) {
  17785. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17786. }
  17787. ret = wc_RsaSetRNG(key, &rng);
  17788. if (ret < 0) {
  17789. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17790. }
  17791. #endif
  17792. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  17793. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  17794. do {
  17795. #if defined(WOLFSSL_ASYNC_CRYPT)
  17796. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17797. #endif
  17798. if (ret >= 0) {
  17799. ret = wc_RsaPublicEncrypt_ex(tmp, inLen, out, outSz, key, &rng,
  17800. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  17801. }
  17802. } while (ret == WC_PENDING_E);
  17803. if (ret < 0) {
  17804. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17805. }
  17806. TEST_SLEEP();
  17807. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  17808. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17809. do {
  17810. #if defined(WOLFSSL_ASYNC_CRYPT)
  17811. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17812. #endif
  17813. if (ret >= 0) {
  17814. ret = wc_RsaPrivateDecrypt_ex(out, outSz, plain, plainSz, key,
  17815. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  17816. }
  17817. } while (ret == WC_PENDING_E);
  17818. if (ret < 0) {
  17819. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17820. }
  17821. if (XMEMCMP(plain, tmp, inLen) != 0) {
  17822. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  17823. }
  17824. TEST_SLEEP();
  17825. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  17826. /* test some bad arguments */
  17827. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key, -1,
  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, plain, &plainSz, NULL, RSA_PUBLIC_DECRYPT,
  17833. &rng);
  17834. if (ret != BAD_FUNC_ARG) {
  17835. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17836. }
  17837. ret = wc_RsaDirect(out, outSz, NULL, &plainSz, key, RSA_PUBLIC_DECRYPT,
  17838. &rng);
  17839. if (ret != LENGTH_ONLY_E || plainSz != inLen) {
  17840. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17841. }
  17842. ret = wc_RsaDirect(out, outSz - 10, plain, &plainSz, key,
  17843. RSA_PUBLIC_DECRYPT, &rng);
  17844. if (ret != BAD_FUNC_ARG) {
  17845. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  17846. }
  17847. /* if making it to this point of code without hitting an ERROR_OUT then
  17848. * all tests have passed */
  17849. ret = 0;
  17850. exit_rsa_nopadding:
  17851. wc_FreeRsaKey(key);
  17852. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17853. WC_FREE_VAR(key, HEAP_HINT);
  17854. WC_FREE_VAR(out, HEAP_HINT);
  17855. WC_FREE_VAR(plain, HEAP_HINT);
  17856. wc_FreeRng(&rng);
  17857. return ret;
  17858. }
  17859. #endif /* WC_RSA_NO_PADDING */
  17860. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  17861. static wc_test_ret_t rsa_even_mod_test(WC_RNG* rng, RsaKey* key)
  17862. {
  17863. byte* tmp = NULL;
  17864. size_t bytes;
  17865. wc_test_ret_t ret;
  17866. word32 inLen = 0;
  17867. #ifndef NO_ASN
  17868. word32 idx = 0;
  17869. #endif
  17870. word32 outSz = RSA_TEST_BYTES;
  17871. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17872. word32 plainSz = RSA_TEST_BYTES;
  17873. #endif
  17874. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_3072) && \
  17875. !defined(USE_CERT_BUFFERS_4096) && !defined(NO_FILESYSTEM)
  17876. XFILE file;
  17877. #endif
  17878. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17879. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17880. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  17881. #endif
  17882. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  17883. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17884. WC_ALLOC_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  17885. #endif
  17886. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17887. if (out == NULL
  17888. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17889. || plain == NULL
  17890. #endif
  17891. ) {
  17892. ERROR_OUT(MEMORY_E, exit_rsa_even_mod);
  17893. }
  17894. #endif
  17895. #if defined(USE_CERT_BUFFERS_2048)
  17896. bytes = (size_t)sizeof_client_key_der_2048;
  17897. if (bytes < (size_t)sizeof_client_cert_der_2048)
  17898. bytes = (size_t)sizeof_client_cert_der_2048;
  17899. #else
  17900. bytes = FOURK_BUF;
  17901. #endif
  17902. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17903. if (tmp == NULL
  17904. #ifdef WOLFSSL_ASYNC_CRYPT
  17905. || out == NULL || plain == NULL
  17906. #endif
  17907. ) {
  17908. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_even_mod);
  17909. }
  17910. #if defined(USE_CERT_BUFFERS_2048)
  17911. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  17912. #elif defined(USE_CERT_BUFFERS_3072)
  17913. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  17914. #elif defined(USE_CERT_BUFFERS_4096)
  17915. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  17916. #elif !defined(NO_FILESYSTEM)
  17917. file = XFOPEN(clientKey, "rb");
  17918. if (!file) {
  17919. err_sys("can't open ./certs/client-key.der, "
  17920. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  17921. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_even_mod);
  17922. }
  17923. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  17924. XFCLOSE(file);
  17925. if (bytes == 0)
  17926. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_even_mod);
  17927. #else
  17928. /* No key to use. */
  17929. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_even_mod);
  17930. #endif /* USE_CERT_BUFFERS */
  17931. #ifndef NO_ASN
  17932. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  17933. if (ret != 0) {
  17934. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17935. }
  17936. #else
  17937. #ifdef USE_CERT_BUFFERS_2048
  17938. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  17939. if (ret != 0) {
  17940. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17941. }
  17942. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  17943. if (ret != 0) {
  17944. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17945. }
  17946. #ifndef NO_SIG_WRAPPER
  17947. modLen = 2048;
  17948. #endif
  17949. #else
  17950. #error Not supported yet!
  17951. #endif
  17952. #endif
  17953. key->n.dp[0] &= (mp_digit)-2;
  17954. if (ret != 0) {
  17955. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17956. }
  17957. /* after loading in key use tmp as the test buffer */
  17958. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  17959. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM))) && \
  17960. !defined(WOLFSSL_XILINX_CRYPT)
  17961. /* The ARM64_ASM code that was FIPS validated did not return these expected
  17962. * failure codes. These tests cases were added after the assembly was
  17963. * in-lined in the module and validated, these tests will be available in
  17964. * the 140-3 module */
  17965. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  17966. inLen = 32;
  17967. outSz = wc_RsaEncryptSize(key);
  17968. XMEMSET(tmp, 7, plainSz);
  17969. ret = wc_RsaSSL_Sign(tmp, inLen, out, outSz, key, rng);
  17970. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  17971. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17972. }
  17973. ret = wc_RsaSSL_Verify(out, outSz, tmp, inLen, key);
  17974. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  17975. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17976. }
  17977. #endif
  17978. #ifdef WC_RSA_BLINDING
  17979. ret = wc_RsaSetRNG(key, rng);
  17980. if (ret < 0) {
  17981. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17982. }
  17983. #endif
  17984. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  17985. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  17986. ret = wc_RsaPublicEncrypt(tmp, inLen, out, (int)outSz, key, rng);
  17987. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  17988. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17989. }
  17990. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  17991. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17992. ret = wc_RsaPrivateDecrypt(out, outSz, plain, (int)plainSz, key);
  17993. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  17994. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  17995. }
  17996. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  17997. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  17998. /* if making it to this point of code without hitting an ERROR_OUT then
  17999. * all tests have passed */
  18000. ret = 0;
  18001. exit_rsa_even_mod:
  18002. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18003. WC_FREE_VAR(out, HEAP_HINT);
  18004. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18005. WC_FREE_VAR(plain, HEAP_HINT);
  18006. #endif
  18007. (void)out;
  18008. (void)outSz;
  18009. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18010. (void)plain;
  18011. (void)plainSz;
  18012. #endif
  18013. (void)inLen;
  18014. (void)rng;
  18015. return ret;
  18016. }
  18017. #endif /* WOLFSSL_HAVE_SP_RSA */
  18018. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  18019. static wc_test_ret_t rsa_certgen_test(RsaKey* key, RsaKey* keypub, WC_RNG* rng, byte* tmp)
  18020. {
  18021. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18022. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18023. #ifdef WOLFSSL_TEST_CERT
  18024. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18025. #endif
  18026. #else
  18027. RsaKey caKey[1];
  18028. #ifdef WOLFSSL_TEST_CERT
  18029. DecodedCert decode[1];
  18030. #endif
  18031. #endif
  18032. byte* der = NULL;
  18033. wc_test_ret_t ret;
  18034. Cert* myCert = NULL;
  18035. int certSz;
  18036. size_t bytes3;
  18037. word32 idx3 = 0;
  18038. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  18039. XFILE file3;
  18040. #endif
  18041. #if defined(WOLFSSL_ALT_NAMES) && !defined(NO_ASN_TIME)
  18042. struct tm beforeTime;
  18043. struct tm afterTime;
  18044. #endif
  18045. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  18046. (void)keypub;
  18047. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18048. if (caKey == NULL)
  18049. ERROR_OUT(MEMORY_E, exit_rsa);
  18050. #ifdef WOLFSSL_TEST_CERT
  18051. if (decode == NULL)
  18052. ERROR_OUT(MEMORY_E, exit_rsa);
  18053. #endif
  18054. #endif
  18055. XMEMSET(caKey, 0, sizeof *caKey);
  18056. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18057. if (der == NULL) {
  18058. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18059. }
  18060. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18061. if (myCert == NULL) {
  18062. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18063. }
  18064. /* self signed */
  18065. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  18066. if (ret != 0) {
  18067. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18068. }
  18069. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  18070. XMEMCPY(myCert->serial, mySerial, sizeof(mySerial));
  18071. myCert->serialSz = (int)sizeof(mySerial);
  18072. myCert->isCA = 1;
  18073. #ifndef NO_SHA256
  18074. myCert->sigType = CTC_SHA256wRSA;
  18075. #else
  18076. myCert->sigType = CTC_SHAwRSA;
  18077. #endif
  18078. #ifdef WOLFSSL_CERT_EXT
  18079. /* add Policies */
  18080. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  18081. CTC_MAX_CERTPOL_SZ);
  18082. XSTRNCPY(myCert->certPolicies[1], "1.2.840.113549.1.9.16.6.5",
  18083. CTC_MAX_CERTPOL_SZ);
  18084. myCert->certPoliciesNb = 2;
  18085. /* add SKID from the Public Key */
  18086. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, keypub, NULL);
  18087. if (ret != 0) {
  18088. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18089. }
  18090. /* add AKID from the Public Key */
  18091. ret = wc_SetAuthKeyIdFromPublicKey(myCert, keypub, NULL);
  18092. if (ret != 0) {
  18093. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18094. }
  18095. /* add Key Usage */
  18096. ret = wc_SetKeyUsage(myCert,"cRLSign,keyCertSign");
  18097. if (ret != 0) {
  18098. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18099. }
  18100. #ifdef WOLFSSL_EKU_OID
  18101. {
  18102. const char unique[] = "2.16.840.1.111111.100.1.10.1";
  18103. ret = wc_SetExtKeyUsageOID(myCert, unique, sizeof(unique), 0,
  18104. HEAP_HINT);
  18105. if (ret != 0) {
  18106. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18107. }
  18108. }
  18109. #endif /* WOLFSSL_EKU_OID */
  18110. #endif /* WOLFSSL_CERT_EXT */
  18111. do {
  18112. #if defined(WOLFSSL_ASYNC_CRYPT)
  18113. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18114. #endif
  18115. if (ret >= 0) {
  18116. ret = wc_MakeSelfCert(myCert, der, FOURK_BUF, key, rng);
  18117. }
  18118. } while (ret == WC_PENDING_E);
  18119. if (ret < 0) {
  18120. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18121. }
  18122. certSz = (int)ret;
  18123. #ifdef WOLFSSL_TEST_CERT
  18124. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  18125. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  18126. if (ret != 0) {
  18127. FreeDecodedCert(decode);
  18128. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18129. }
  18130. FreeDecodedCert(decode);
  18131. #endif
  18132. ret = SaveDerAndPem(der, certSz, certDerFile, certPemFile,
  18133. CERT_TYPE);
  18134. if (ret != 0) {
  18135. goto exit_rsa;
  18136. }
  18137. /* Setup Certificate */
  18138. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  18139. if (ret < 0) {
  18140. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18141. }
  18142. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  18143. /* Get CA Cert for testing */
  18144. #ifdef USE_CERT_BUFFERS_1024
  18145. XMEMCPY(tmp, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  18146. bytes3 = sizeof_ca_cert_der_1024;
  18147. #elif defined(USE_CERT_BUFFERS_2048)
  18148. XMEMCPY(tmp, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  18149. bytes3 = sizeof_ca_cert_der_2048;
  18150. #else
  18151. file3 = XFOPEN(rsaCaCertDerFile, "rb");
  18152. if (!file3) {
  18153. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18154. }
  18155. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  18156. XFCLOSE(file3);
  18157. if (bytes3 == 0)
  18158. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18159. #endif /* USE_CERT_BUFFERS */
  18160. #if defined(WOLFSSL_ALT_NAMES)
  18161. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_1024) && \
  18162. !defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  18163. ret = wc_SetAltNames(myCert, rsaCaCertFile);
  18164. if (ret != 0)
  18165. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18166. #endif
  18167. /* get alt names from der */
  18168. ret = wc_SetAltNamesBuffer(myCert, tmp, (int)bytes3);
  18169. if (ret != 0)
  18170. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18171. /* get dates from der */
  18172. ret = wc_SetDatesBuffer(myCert, tmp, (int)bytes3);
  18173. if (ret != 0)
  18174. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18175. #ifndef NO_ASN_TIME
  18176. ret = wc_GetCertDates(myCert, &beforeTime, &afterTime);
  18177. if (ret < 0)
  18178. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18179. #endif
  18180. #endif /* WOLFSSL_ALT_NAMES */
  18181. #endif /* WOLFSSL_ALT_NAMES || HAVE_PKCS7 */
  18182. /* Get CA Key */
  18183. #ifdef USE_CERT_BUFFERS_1024
  18184. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  18185. bytes3 = sizeof_ca_key_der_1024;
  18186. #elif defined(USE_CERT_BUFFERS_2048)
  18187. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  18188. bytes3 = sizeof_ca_key_der_2048;
  18189. #else
  18190. file3 = XFOPEN(rsaCaKeyFile, "rb");
  18191. if (!file3) {
  18192. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18193. }
  18194. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  18195. XFCLOSE(file3);
  18196. if (bytes3 == 0)
  18197. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18198. #endif /* USE_CERT_BUFFERS */
  18199. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  18200. if (ret != 0)
  18201. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18202. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  18203. if (ret != 0)
  18204. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18205. #ifndef NO_SHA256
  18206. myCert->sigType = CTC_SHA256wRSA;
  18207. #else
  18208. myCert->sigType = CTC_SHAwRSA;
  18209. #endif
  18210. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  18211. #ifdef WOLFSSL_CERT_EXT
  18212. /* add Policies */
  18213. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  18214. CTC_MAX_CERTPOL_SZ);
  18215. myCert->certPoliciesNb =1;
  18216. /* add SKID from the Public Key */
  18217. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, key, NULL);
  18218. if (ret != 0)
  18219. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18220. /* add AKID from the CA certificate */
  18221. #if defined(USE_CERT_BUFFERS_2048)
  18222. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  18223. sizeof_ca_cert_der_2048);
  18224. #elif defined(USE_CERT_BUFFERS_1024)
  18225. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  18226. sizeof_ca_cert_der_1024);
  18227. #else
  18228. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  18229. #endif
  18230. if (ret != 0)
  18231. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18232. /* add Key Usage */
  18233. ret = wc_SetKeyUsage(myCert,"keyEncipherment,keyAgreement");
  18234. if (ret != 0)
  18235. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18236. #endif /* WOLFSSL_CERT_EXT */
  18237. #if defined(USE_CERT_BUFFERS_2048)
  18238. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  18239. sizeof_ca_cert_der_2048);
  18240. #elif defined(USE_CERT_BUFFERS_1024)
  18241. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  18242. sizeof_ca_cert_der_1024);
  18243. #else
  18244. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  18245. #endif
  18246. if (ret < 0)
  18247. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18248. certSz = wc_MakeCert(myCert, der, FOURK_BUF, key, NULL, rng);
  18249. if (certSz < 0) {
  18250. ERROR_OUT(WC_TEST_RET_ENC_EC(certSz), exit_rsa);
  18251. }
  18252. ret = 0;
  18253. do {
  18254. #if defined(WOLFSSL_ASYNC_CRYPT)
  18255. ret = wc_AsyncWait(ret, &caKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18256. #endif
  18257. if (ret >= 0) {
  18258. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der, FOURK_BUF,
  18259. caKey, NULL, rng);
  18260. }
  18261. } while (ret == WC_PENDING_E);
  18262. if (ret < 0)
  18263. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18264. certSz = (int)ret;
  18265. #ifdef WOLFSSL_TEST_CERT
  18266. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  18267. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  18268. if (ret != 0) {
  18269. FreeDecodedCert(decode);
  18270. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18271. }
  18272. FreeDecodedCert(decode);
  18273. #endif
  18274. ret = SaveDerAndPem(der, certSz, otherCertDerFile, otherCertPemFile,
  18275. CERT_TYPE);
  18276. if (ret != 0) {
  18277. goto exit_rsa;
  18278. }
  18279. exit_rsa:
  18280. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18281. if (caKey != NULL) {
  18282. wc_FreeRsaKey(caKey);
  18283. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18284. }
  18285. #ifdef WOLFSSL_TEST_CERT
  18286. if (decode != NULL)
  18287. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18288. #endif
  18289. #else
  18290. wc_FreeRsaKey(caKey);
  18291. #endif
  18292. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18293. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18294. return ret;
  18295. }
  18296. #endif
  18297. #if !defined(NO_RSA) && defined(HAVE_ECC) && !defined(NO_ECC_SECP) && \
  18298. defined(WOLFSSL_CERT_GEN)
  18299. /* Make Cert / Sign example for ECC cert and RSA CA */
  18300. static wc_test_ret_t rsa_ecc_certgen_test(WC_RNG* rng, byte* tmp)
  18301. {
  18302. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18303. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18304. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18305. ecc_key *caEccKeyPub = (ecc_key *)XMALLOC(sizeof *caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18306. #ifdef WOLFSSL_TEST_CERT
  18307. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18308. #endif
  18309. #else
  18310. RsaKey caKey[1];
  18311. ecc_key caEccKey[1];
  18312. ecc_key caEccKeyPub[1];
  18313. #ifdef WOLFSSL_TEST_CERT
  18314. DecodedCert decode[1];
  18315. #endif
  18316. #endif
  18317. byte* der = NULL;
  18318. Cert* myCert = NULL;
  18319. int certSz;
  18320. size_t bytes3;
  18321. word32 idx3 = 0;
  18322. #if (!defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)) \
  18323. || !defined(USE_CERT_BUFFERS_256)
  18324. #ifndef NO_FILESYSTEM
  18325. XFILE file3;
  18326. #endif
  18327. #endif
  18328. wc_test_ret_t ret;
  18329. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18330. if ((caKey == NULL) || (caEccKey == NULL) || (caEccKeyPub == NULL)
  18331. #ifdef WOLFSSL_TEST_CERT
  18332. || (decode == NULL)
  18333. #endif
  18334. )
  18335. ERROR_OUT(MEMORY_E, exit_rsa);
  18336. #endif
  18337. XMEMSET(caKey, 0, sizeof *caKey);
  18338. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  18339. XMEMSET(caEccKeyPub, 0, sizeof *caEccKeyPub);
  18340. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18341. if (der == NULL) {
  18342. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18343. }
  18344. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18345. if (myCert == NULL) {
  18346. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18347. }
  18348. /* Get CA Key */
  18349. #ifdef USE_CERT_BUFFERS_1024
  18350. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  18351. bytes3 = sizeof_ca_key_der_1024;
  18352. #elif defined(USE_CERT_BUFFERS_2048)
  18353. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  18354. bytes3 = sizeof_ca_key_der_2048;
  18355. #else
  18356. file3 = XFOPEN(rsaCaKeyFile, "rb");
  18357. if (!file3) {
  18358. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18359. }
  18360. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  18361. XFCLOSE(file3);
  18362. if (bytes3 == 0)
  18363. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18364. #endif /* USE_CERT_BUFFERS */
  18365. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  18366. if (ret != 0)
  18367. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18368. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  18369. if (ret != 0)
  18370. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18371. /* Get Cert Key */
  18372. #if defined(USE_CERT_BUFFERS_256)
  18373. {
  18374. XMEMCPY(tmp, ecc_key_pub_der_256, sizeof_ecc_key_pub_der_256);
  18375. bytes3 = sizeof_ecc_key_pub_der_256;
  18376. }
  18377. #elif !defined(NO_FILESYSTEM)
  18378. {
  18379. file3 = XFOPEN(eccKeyPubFileDer, "rb");
  18380. if (!file3) {
  18381. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18382. }
  18383. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  18384. XFCLOSE(file3);
  18385. if (bytes3 == 0)
  18386. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18387. }
  18388. #else
  18389. {
  18390. WOLFSSL_MSG("No file system and USE_CERT_BUFFERS_256 not defined.(1)");
  18391. ERROR_OUT(ASN_PARSE_E, exit_rsa);
  18392. }
  18393. #endif
  18394. ret = wc_ecc_init_ex(caEccKeyPub, HEAP_HINT, devId);
  18395. if (ret != 0)
  18396. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18397. idx3 = 0;
  18398. ret = wc_EccPublicKeyDecode(tmp, &idx3, caEccKeyPub, (word32)bytes3);
  18399. if (ret != 0)
  18400. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18401. /* Setup Certificate */
  18402. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  18403. if (ret != 0)
  18404. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18405. #ifndef NO_SHA256
  18406. myCert->sigType = CTC_SHA256wRSA;
  18407. #else
  18408. myCert->sigType = CTC_SHAwRSA;
  18409. #endif
  18410. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  18411. #ifdef WOLFSSL_CERT_EXT
  18412. /* add Policies */
  18413. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  18414. CTC_MAX_CERTPOL_SZ);
  18415. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  18416. CTC_MAX_CERTPOL_SZ);
  18417. myCert->certPoliciesNb = 2;
  18418. /* add SKID from the Public Key */
  18419. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, caEccKeyPub);
  18420. if (ret != 0)
  18421. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18422. /* add AKID from the CA certificate */
  18423. #if defined(USE_CERT_BUFFERS_2048)
  18424. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  18425. sizeof_ca_cert_der_2048);
  18426. #elif defined(USE_CERT_BUFFERS_1024)
  18427. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  18428. sizeof_ca_cert_der_1024);
  18429. #else
  18430. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  18431. #endif
  18432. if (ret != 0)
  18433. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18434. /* add Key Usage */
  18435. ret = wc_SetKeyUsage(myCert, certKeyUsage);
  18436. if (ret != 0)
  18437. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18438. #endif /* WOLFSSL_CERT_EXT */
  18439. #if defined(USE_CERT_BUFFERS_2048)
  18440. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  18441. sizeof_ca_cert_der_2048);
  18442. #elif defined(USE_CERT_BUFFERS_1024)
  18443. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  18444. sizeof_ca_cert_der_1024);
  18445. #else
  18446. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  18447. #endif
  18448. if (ret < 0)
  18449. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18450. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, caEccKeyPub, rng);
  18451. if (certSz < 0) {
  18452. ERROR_OUT(WC_TEST_RET_ENC_EC(certSz), exit_rsa);
  18453. }
  18454. ret = 0;
  18455. do {
  18456. #if defined(WOLFSSL_ASYNC_CRYPT)
  18457. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18458. #endif
  18459. if (ret >= 0) {
  18460. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  18461. FOURK_BUF, caKey, NULL, rng);
  18462. }
  18463. } while (ret == WC_PENDING_E);
  18464. if (ret < 0)
  18465. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18466. certSz = (int)ret;
  18467. #ifdef WOLFSSL_TEST_CERT
  18468. InitDecodedCert(decode, der, certSz, 0);
  18469. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  18470. if (ret != 0) {
  18471. FreeDecodedCert(decode);
  18472. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18473. }
  18474. FreeDecodedCert(decode);
  18475. #endif
  18476. ret = SaveDerAndPem(der, certSz, certEccRsaDerFile, certEccRsaPemFile,
  18477. CERT_TYPE);
  18478. if (ret != 0) {
  18479. goto exit_rsa;
  18480. }
  18481. exit_rsa:
  18482. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18483. if (caKey != NULL) {
  18484. wc_FreeRsaKey(caKey);
  18485. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18486. }
  18487. if (caEccKey != NULL) {
  18488. wc_ecc_free(caEccKey);
  18489. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18490. }
  18491. if (caEccKeyPub != NULL) {
  18492. wc_ecc_free(caEccKeyPub);
  18493. XFREE(caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18494. }
  18495. #ifdef WOLFSSL_TEST_CERT
  18496. if (decode != NULL)
  18497. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18498. #endif
  18499. #else
  18500. wc_FreeRsaKey(caKey);
  18501. wc_ecc_free(caEccKey);
  18502. wc_ecc_free(caEccKeyPub);
  18503. #endif
  18504. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18505. myCert = NULL;
  18506. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18507. der = NULL;
  18508. if (ret >= 0)
  18509. ret = 0;
  18510. return ret;
  18511. }
  18512. #endif /* !NO_RSA && HAVE_ECC && WOLFSSL_CERT_GEN */
  18513. #ifdef WOLFSSL_KEY_GEN
  18514. static wc_test_ret_t rsa_keygen_test(WC_RNG* rng)
  18515. {
  18516. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18517. RsaKey *genKey = (RsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18518. #else
  18519. RsaKey genKey[1];
  18520. #endif
  18521. wc_test_ret_t ret;
  18522. #ifndef WOLFSSL_NO_MALLOC
  18523. byte* der = NULL;
  18524. #else
  18525. byte der[1024];
  18526. #endif
  18527. #ifndef WOLFSSL_CRYPTOCELL
  18528. word32 idx = 0;
  18529. #endif
  18530. int derSz = 0;
  18531. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FIPS)
  18532. int keySz = 1024;
  18533. #else
  18534. int keySz = 2048;
  18535. #endif
  18536. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18537. if (! genKey)
  18538. ERROR_OUT(MEMORY_E, exit_rsa);
  18539. #endif
  18540. XMEMSET(genKey, 0, sizeof *genKey);
  18541. ret = wc_InitRsaKey_ex(genKey, HEAP_HINT, devId);
  18542. if (ret != 0)
  18543. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18544. #ifdef HAVE_FIPS
  18545. for (;;) {
  18546. #endif
  18547. ret = wc_MakeRsaKey(genKey, keySz, WC_RSA_EXPONENT, rng);
  18548. #if defined(WOLFSSL_ASYNC_CRYPT)
  18549. ret = wc_AsyncWait(ret, &genKey->asyncDev, WC_ASYNC_FLAG_NONE);
  18550. #endif
  18551. #ifdef HAVE_FIPS
  18552. if (ret == PRIME_GEN_E)
  18553. continue;
  18554. break;
  18555. }
  18556. #endif
  18557. if (ret != 0)
  18558. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18559. TEST_SLEEP();
  18560. #ifdef WOLFSSL_RSA_KEY_CHECK
  18561. ret = wc_CheckRsaKey(genKey);
  18562. if (ret != 0)
  18563. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18564. #endif
  18565. #ifndef WOLFSSL_NO_MALLOC
  18566. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18567. if (der == NULL) {
  18568. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  18569. }
  18570. #endif
  18571. derSz = wc_RsaKeyToDer(genKey, der, FOURK_BUF);
  18572. if (derSz < 0) {
  18573. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  18574. }
  18575. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  18576. PRIVATEKEY_TYPE);
  18577. if (ret != 0) {
  18578. goto exit_rsa;
  18579. }
  18580. wc_FreeRsaKey(genKey);
  18581. ret = wc_InitRsaKey(genKey, HEAP_HINT);
  18582. if (ret != 0)
  18583. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18584. #ifndef WOLFSSL_CRYPTOCELL
  18585. idx = 0;
  18586. /* The private key part of the key gen pairs from cryptocell can't be exported */
  18587. ret = wc_RsaPrivateKeyDecode(der, &idx, genKey, (word32)derSz);
  18588. if (ret != 0)
  18589. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18590. #endif /* WOLFSSL_CRYPTOCELL */
  18591. exit_rsa:
  18592. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18593. if (genKey) {
  18594. wc_FreeRsaKey(genKey);
  18595. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18596. }
  18597. #else
  18598. wc_FreeRsaKey(genKey);
  18599. #endif
  18600. #ifndef WOLFSSL_NO_MALLOC
  18601. if (der != NULL) {
  18602. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18603. der = NULL;
  18604. }
  18605. #endif
  18606. return ret;
  18607. }
  18608. #endif
  18609. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  18610. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG) && \
  18611. (!defined(HAVE_FIPS) || \
  18612. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  18613. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  18614. static wc_test_ret_t rsa_oaep_padding_test(RsaKey* key, WC_RNG* rng)
  18615. {
  18616. wc_test_ret_t ret = 0;
  18617. word32 idx = 0;
  18618. const char inStr[] = TEST_STRING;
  18619. const word32 inLen = (word32)TEST_STRING_SZ;
  18620. const word32 outSz = RSA_TEST_BYTES;
  18621. const word32 plainSz = RSA_TEST_BYTES;
  18622. byte* res = NULL;
  18623. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  18624. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  18625. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  18626. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  18627. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  18628. WC_ALLOC_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  18629. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  18630. if (in == NULL || out == NULL || plain == NULL)
  18631. ERROR_OUT(MEMORY_E, exit_rsa);
  18632. #endif
  18633. XMEMCPY(in, inStr, inLen);
  18634. #ifndef NO_SHA
  18635. do {
  18636. #if defined(WOLFSSL_ASYNC_CRYPT)
  18637. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18638. #endif
  18639. if (ret >= 0) {
  18640. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18641. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  18642. }
  18643. } while (ret == WC_PENDING_E);
  18644. if (ret < 0)
  18645. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18646. TEST_SLEEP();
  18647. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18648. idx = (word32)ret;
  18649. do {
  18650. #if defined(WOLFSSL_ASYNC_CRYPT)
  18651. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18652. #endif
  18653. if (ret >= 0) {
  18654. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18655. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  18656. }
  18657. } while (ret == WC_PENDING_E);
  18658. if (ret < 0)
  18659. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18660. if (XMEMCMP(plain, in, inLen)) {
  18661. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18662. }
  18663. TEST_SLEEP();
  18664. #endif /* NO_SHA */
  18665. #endif
  18666. #ifndef NO_SHA256
  18667. XMEMSET(plain, 0, plainSz);
  18668. do {
  18669. #if defined(WOLFSSL_ASYNC_CRYPT)
  18670. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18671. #endif
  18672. if (ret >= 0) {
  18673. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18674. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  18675. }
  18676. } while (ret == WC_PENDING_E);
  18677. if (ret < 0)
  18678. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18679. TEST_SLEEP();
  18680. idx = (word32)ret;
  18681. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18682. do {
  18683. #if defined(WOLFSSL_ASYNC_CRYPT)
  18684. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18685. #endif
  18686. if (ret >= 0) {
  18687. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18688. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  18689. }
  18690. } while (ret == WC_PENDING_E);
  18691. if (ret < 0)
  18692. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18693. if (XMEMCMP(plain, in, inLen)) {
  18694. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18695. }
  18696. TEST_SLEEP();
  18697. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  18698. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18699. do {
  18700. #if defined(WOLFSSL_ASYNC_CRYPT)
  18701. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18702. #endif
  18703. if (ret >= 0) {
  18704. ret = wc_RsaPrivateDecryptInline_ex(out, idx, &res, key,
  18705. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  18706. }
  18707. } while (ret == WC_PENDING_E);
  18708. if (ret < 0)
  18709. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18710. if (ret != (int)inLen) {
  18711. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18712. }
  18713. if (XMEMCMP(res, in, inLen)) {
  18714. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18715. }
  18716. TEST_SLEEP();
  18717. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  18718. /* check fails if not using the same optional label */
  18719. XMEMSET(plain, 0, plainSz);
  18720. do {
  18721. #if defined(WOLFSSL_ASYNC_CRYPT)
  18722. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18723. #endif
  18724. if (ret >= 0) {
  18725. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18726. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  18727. }
  18728. } while (ret == WC_PENDING_E);
  18729. if (ret < 0)
  18730. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18731. TEST_SLEEP();
  18732. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  18733. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  18734. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  18735. /* label is unused in cryptocell and SE050 so it won't detect decrypt error
  18736. * due to label */
  18737. idx = (word32)ret;
  18738. do {
  18739. #if defined(WOLFSSL_ASYNC_CRYPT)
  18740. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18741. #endif
  18742. if (ret >= 0) {
  18743. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18744. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  18745. }
  18746. } while (ret == WC_PENDING_E);
  18747. if (ret > 0) { /* in this case decrypt should fail */
  18748. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18749. }
  18750. ret = 0;
  18751. TEST_SLEEP();
  18752. #endif /* !HAVE_CAVIUM */
  18753. /* check using optional label with encrypt/decrypt */
  18754. XMEMSET(plain, 0, plainSz);
  18755. do {
  18756. #if defined(WOLFSSL_ASYNC_CRYPT)
  18757. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18758. #endif
  18759. if (ret >= 0) {
  18760. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18761. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  18762. }
  18763. } while (ret == WC_PENDING_E);
  18764. if (ret < 0)
  18765. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18766. TEST_SLEEP();
  18767. idx = (word32)ret;
  18768. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18769. do {
  18770. #if defined(WOLFSSL_ASYNC_CRYPT)
  18771. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18772. #endif
  18773. if (ret >= 0) {
  18774. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18775. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  18776. }
  18777. } while (ret == WC_PENDING_E);
  18778. if (ret < 0)
  18779. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18780. if (XMEMCMP(plain, in, inLen)) {
  18781. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18782. }
  18783. TEST_SLEEP();
  18784. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  18785. #ifndef NO_SHA
  18786. /* check fail using mismatch hash algorithms */
  18787. XMEMSET(plain, 0, plainSz);
  18788. do {
  18789. #if defined(WOLFSSL_ASYNC_CRYPT)
  18790. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18791. #endif
  18792. if (ret >= 0) {
  18793. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18794. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, in, inLen);
  18795. }
  18796. } while (ret == WC_PENDING_E);
  18797. if (ret < 0)
  18798. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18799. TEST_SLEEP();
  18800. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  18801. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  18802. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  18803. idx = (word32)ret;
  18804. do {
  18805. #if defined(WOLFSSL_ASYNC_CRYPT)
  18806. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18807. #endif
  18808. if (ret >= 0) {
  18809. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18810. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256,
  18811. in, inLen);
  18812. }
  18813. } while (ret == WC_PENDING_E);
  18814. if (ret > 0) { /* should fail */
  18815. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18816. }
  18817. ret = 0;
  18818. TEST_SLEEP();
  18819. #endif /* !HAVE_CAVIUM */
  18820. #endif /* NO_SHA */
  18821. #endif /* NO_SHA256 */
  18822. #ifdef WOLFSSL_SHA512
  18823. /* Check valid RSA key size is used while using hash length of SHA512
  18824. If key size is less than (hash length * 2) + 2 then is invalid use
  18825. and test, since OAEP padding requires this.
  18826. BAD_FUNC_ARG is returned when this case is not met */
  18827. if (wc_RsaEncryptSize(key) > ((int)WC_SHA512_DIGEST_SIZE * 2) + 2) {
  18828. XMEMSET(plain, 0, plainSz);
  18829. do {
  18830. #if defined(WOLFSSL_ASYNC_CRYPT)
  18831. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18832. #endif
  18833. if (ret >= 0) {
  18834. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18835. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  18836. }
  18837. } while (ret == WC_PENDING_E);
  18838. if (ret < 0)
  18839. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18840. TEST_SLEEP();
  18841. idx = (word32)ret;
  18842. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18843. do {
  18844. #if defined(WOLFSSL_ASYNC_CRYPT)
  18845. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18846. #endif
  18847. if (ret >= 0) {
  18848. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18849. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  18850. }
  18851. } while (ret == WC_PENDING_E);
  18852. if (ret < 0)
  18853. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18854. if (XMEMCMP(plain, in, inLen)) {
  18855. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18856. }
  18857. TEST_SLEEP();
  18858. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  18859. }
  18860. #endif /* WOLFSSL_SHA512 */
  18861. /* check using pkcsv15 padding with _ex API */
  18862. XMEMSET(plain, 0, plainSz);
  18863. do {
  18864. #if defined(WOLFSSL_ASYNC_CRYPT)
  18865. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18866. #endif
  18867. if (ret >= 0) {
  18868. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  18869. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  18870. }
  18871. } while (ret == WC_PENDING_E);
  18872. if (ret < 0)
  18873. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18874. TEST_SLEEP();
  18875. idx = (word32)ret;
  18876. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18877. do {
  18878. #if defined(WOLFSSL_ASYNC_CRYPT)
  18879. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18880. #endif
  18881. if (ret >= 0) {
  18882. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  18883. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  18884. }
  18885. } while (ret == WC_PENDING_E);
  18886. if (ret < 0)
  18887. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  18888. if (XMEMCMP(plain, in, inLen)) {
  18889. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  18890. }
  18891. TEST_SLEEP();
  18892. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  18893. exit_rsa:
  18894. WC_FREE_VAR(in, HEAP_HINT);
  18895. WC_FREE_VAR(out, HEAP_HINT);
  18896. WC_FREE_VAR(plain, HEAP_HINT);
  18897. (void)idx;
  18898. (void)inStr;
  18899. (void)res;
  18900. if (ret >= 0)
  18901. ret = 0;
  18902. return ret;
  18903. }
  18904. #endif
  18905. #endif
  18906. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void)
  18907. {
  18908. wc_test_ret_t ret;
  18909. size_t bytes;
  18910. WC_RNG rng;
  18911. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18912. byte* tmp = NULL;
  18913. byte* der = NULL;
  18914. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18915. #else
  18916. RsaKey key[1];
  18917. byte tmp[FOURK_BUF];
  18918. #endif
  18919. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  18920. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18921. RsaKey *keypub = (RsaKey *)XMALLOC(sizeof *keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18922. #else
  18923. RsaKey keypub[1];
  18924. #endif
  18925. #endif
  18926. word32 idx = 0;
  18927. const char inStr[] = TEST_STRING;
  18928. const word32 inLen = (word32)TEST_STRING_SZ;
  18929. const word32 outSz = RSA_TEST_BYTES;
  18930. const word32 plainSz = RSA_TEST_BYTES;
  18931. byte* res = NULL;
  18932. #ifndef NO_SIG_WRAPPER
  18933. int modLen;
  18934. #endif
  18935. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  18936. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  18937. !defined(NO_FILESYSTEM)
  18938. XFILE file;
  18939. #ifdef WOLFSSL_TEST_CERT
  18940. XFILE file2;
  18941. #endif
  18942. #endif
  18943. #ifdef WOLFSSL_TEST_CERT
  18944. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18945. DecodedCert *cert = (DecodedCert *)XMALLOC(sizeof *cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18946. #else
  18947. DecodedCert cert[1];
  18948. #endif
  18949. #ifndef NO_ASN_TIME
  18950. struct tm timearg;
  18951. const byte* date;
  18952. byte dateFormat;
  18953. int dateLength;
  18954. #endif
  18955. #endif
  18956. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  18957. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  18958. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  18959. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  18960. WC_ALLOC_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  18961. WC_ALLOC_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  18962. WOLFSSL_ENTER("rsa_test");
  18963. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  18964. if (in == NULL || out == NULL || plain == NULL)
  18965. ERROR_OUT(MEMORY_E, exit_rsa);
  18966. #endif
  18967. XMEMCPY(in, inStr, inLen);
  18968. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18969. if (key == NULL)
  18970. ERROR_OUT(MEMORY_E, exit_rsa);
  18971. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  18972. if (keypub == NULL)
  18973. ERROR_OUT(MEMORY_E, exit_rsa);
  18974. #endif
  18975. #ifdef WOLFSSL_TEST_CERT
  18976. if (cert == NULL)
  18977. ERROR_OUT(MEMORY_E, exit_rsa);
  18978. #endif
  18979. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  18980. /* initialize stack structures */
  18981. XMEMSET(&rng, 0, sizeof(rng));
  18982. XMEMSET(key, 0, sizeof *key);
  18983. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  18984. XMEMSET(keypub, 0, sizeof *keypub);
  18985. #endif
  18986. #if !defined(NO_ASN)
  18987. ret = rsa_decode_test(key);
  18988. if (ret != 0)
  18989. ERROR_OUT(ret, exit_rsa);
  18990. #endif
  18991. #ifdef USE_CERT_BUFFERS_1024
  18992. bytes = (size_t)sizeof_client_key_der_1024;
  18993. if (bytes < (size_t)sizeof_client_cert_der_1024)
  18994. bytes = (size_t)sizeof_client_cert_der_1024;
  18995. #elif defined(USE_CERT_BUFFERS_2048)
  18996. bytes = (size_t)sizeof_client_key_der_2048;
  18997. if (bytes < (size_t)sizeof_client_cert_der_2048)
  18998. bytes = (size_t)sizeof_client_cert_der_2048;
  18999. #elif defined(USE_CERT_BUFFERS_3072)
  19000. bytes = (size_t)sizeof_client_key_der_3072;
  19001. if (bytes < (size_t)sizeof_client_cert_der_3072)
  19002. bytes = (size_t)sizeof_client_cert_der_3072;
  19003. #elif defined(USE_CERT_BUFFERS_4096)
  19004. bytes = (size_t)sizeof_client_key_der_4096;
  19005. if (bytes < (size_t)sizeof_client_cert_der_4096)
  19006. bytes = (size_t)sizeof_client_cert_der_4096;
  19007. #else
  19008. bytes = FOURK_BUF;
  19009. #endif
  19010. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19011. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19012. if (tmp == NULL)
  19013. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19014. #endif
  19015. #ifdef USE_CERT_BUFFERS_1024
  19016. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  19017. #elif defined(USE_CERT_BUFFERS_2048)
  19018. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  19019. #elif defined(USE_CERT_BUFFERS_3072)
  19020. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  19021. #elif defined(USE_CERT_BUFFERS_4096)
  19022. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  19023. #elif !defined(NO_FILESYSTEM)
  19024. file = XFOPEN(clientKey, "rb");
  19025. if (!file) {
  19026. err_sys("can't open ./certs/client-key.der, "
  19027. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  19028. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19029. }
  19030. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  19031. XFCLOSE(file);
  19032. if (bytes == 0)
  19033. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19034. #else
  19035. /* No key to use. */
  19036. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  19037. #endif /* USE_CERT_BUFFERS */
  19038. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  19039. if (ret != 0)
  19040. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19041. #ifndef NO_ASN
  19042. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  19043. if (ret != 0)
  19044. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19045. #ifndef NO_SIG_WRAPPER
  19046. modLen = wc_RsaEncryptSize(key);
  19047. #endif
  19048. #else
  19049. #ifdef USE_CERT_BUFFERS_2048
  19050. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  19051. if (ret != 0)
  19052. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19053. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  19054. if (ret != 0)
  19055. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19056. #ifndef NO_SIG_WRAPPER
  19057. modLen = 2048;
  19058. #endif
  19059. #else
  19060. #error Not supported yet!
  19061. #endif
  19062. #endif
  19063. #ifndef WC_NO_RNG
  19064. #ifndef HAVE_FIPS
  19065. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  19066. #else
  19067. ret = wc_InitRng(&rng);
  19068. #endif
  19069. if (ret != 0)
  19070. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19071. #endif
  19072. #ifndef NO_SIG_WRAPPER
  19073. ret = rsa_sig_test(key, sizeof *key, modLen, &rng);
  19074. if (ret != 0)
  19075. goto exit_rsa;
  19076. #endif
  19077. #ifdef WC_RSA_NONBLOCK
  19078. ret = rsa_nb_test(key, in, inLen, out, outSz, plain, plainSz, &rng);
  19079. if (ret != 0)
  19080. goto exit_rsa;
  19081. #endif
  19082. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  19083. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  19084. do {
  19085. #if defined(WOLFSSL_ASYNC_CRYPT)
  19086. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19087. #endif
  19088. if (ret >= 0) {
  19089. ret = wc_RsaPublicEncrypt(in, inLen, out, outSz, key, &rng);
  19090. }
  19091. } while (ret == WC_PENDING_E);
  19092. if (ret < 0)
  19093. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19094. TEST_SLEEP();
  19095. #ifdef WC_RSA_BLINDING
  19096. {
  19097. wc_test_ret_t tmpret = ret;
  19098. ret = wc_RsaSetRNG(key, &rng);
  19099. if (ret < 0)
  19100. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19101. ret = tmpret;
  19102. }
  19103. #endif
  19104. idx = (word32)ret; /* save off encrypted length */
  19105. do {
  19106. #if defined(WOLFSSL_ASYNC_CRYPT)
  19107. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19108. #endif
  19109. if (ret >= 0) {
  19110. ret = wc_RsaPrivateDecrypt(out, idx, plain, plainSz, key);
  19111. }
  19112. } while (ret == WC_PENDING_E);
  19113. if (ret < 0)
  19114. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19115. if (XMEMCMP(plain, in, inLen)) {
  19116. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  19117. }
  19118. TEST_SLEEP();
  19119. do {
  19120. #if defined(WOLFSSL_ASYNC_CRYPT)
  19121. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19122. #endif
  19123. if (ret >= 0) {
  19124. ret = wc_RsaPrivateDecryptInline(out, idx, &res, key);
  19125. }
  19126. } while (ret == WC_PENDING_E);
  19127. if (ret < 0)
  19128. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19129. if (ret != (int)inLen) {
  19130. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  19131. }
  19132. if (XMEMCMP(res, in, inLen)) {
  19133. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  19134. }
  19135. TEST_SLEEP();
  19136. do {
  19137. #if defined(WOLFSSL_ASYNC_CRYPT)
  19138. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19139. #endif
  19140. if (ret >= 0) {
  19141. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, &rng);
  19142. }
  19143. } while (ret == WC_PENDING_E);
  19144. if (ret < 0)
  19145. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19146. TEST_SLEEP();
  19147. #elif defined(WOLFSSL_PUBLIC_MP)
  19148. {
  19149. static const byte signature_2048[] = {
  19150. 0x07, 0x6f, 0xc9, 0x85, 0x73, 0x9e, 0x21, 0x79,
  19151. 0x47, 0xf1, 0xa3, 0xd7, 0xf4, 0x27, 0x29, 0xbe,
  19152. 0x99, 0x5d, 0xac, 0xb2, 0x10, 0x3f, 0x95, 0xda,
  19153. 0x89, 0x23, 0xb8, 0x96, 0x13, 0x57, 0x72, 0x30,
  19154. 0xa1, 0xfe, 0x5a, 0x68, 0x9c, 0x99, 0x9d, 0x1e,
  19155. 0x05, 0xa4, 0x80, 0xb0, 0xbb, 0xd9, 0xd9, 0xa1,
  19156. 0x69, 0x97, 0x74, 0xb3, 0x41, 0x21, 0x3b, 0x47,
  19157. 0xf5, 0x51, 0xb1, 0xfb, 0xc7, 0xaa, 0xcc, 0xdc,
  19158. 0xcd, 0x76, 0xa0, 0x28, 0x4d, 0x27, 0x14, 0xa4,
  19159. 0xb9, 0x41, 0x68, 0x7c, 0xb3, 0x66, 0xe6, 0x6f,
  19160. 0x40, 0x76, 0xe4, 0x12, 0xfd, 0xae, 0x29, 0xb5,
  19161. 0x63, 0x60, 0x87, 0xce, 0x49, 0x6b, 0xf3, 0x05,
  19162. 0x9a, 0x14, 0xb5, 0xcc, 0xcd, 0xf7, 0x30, 0x95,
  19163. 0xd2, 0x72, 0x52, 0x1d, 0x5b, 0x7e, 0xef, 0x4a,
  19164. 0x02, 0x96, 0x21, 0x6c, 0x55, 0xa5, 0x15, 0xb1,
  19165. 0x57, 0x63, 0x2c, 0xa3, 0x8e, 0x9d, 0x3d, 0x45,
  19166. 0xcc, 0xb8, 0xe6, 0xa1, 0xc8, 0x59, 0xcd, 0xf5,
  19167. 0xdc, 0x0a, 0x51, 0xb6, 0x9d, 0xfb, 0xf4, 0x6b,
  19168. 0xfd, 0x32, 0x71, 0x6e, 0xcf, 0xcb, 0xb3, 0xd9,
  19169. 0xe0, 0x4a, 0x77, 0x34, 0xd6, 0x61, 0xf5, 0x7c,
  19170. 0xf9, 0xa9, 0xa4, 0xb0, 0x8e, 0x3b, 0xd6, 0x04,
  19171. 0xe0, 0xde, 0x2b, 0x5b, 0x5a, 0xbf, 0xd9, 0xef,
  19172. 0x8d, 0xa3, 0xf5, 0xb1, 0x67, 0xf3, 0xb9, 0x72,
  19173. 0x0a, 0x37, 0x12, 0x35, 0x6c, 0x8e, 0x10, 0x8b,
  19174. 0x38, 0x06, 0x16, 0x4b, 0x20, 0x20, 0x13, 0x00,
  19175. 0x2e, 0x6d, 0xc2, 0x59, 0x23, 0x67, 0x4a, 0x6d,
  19176. 0xa1, 0x46, 0x8b, 0xee, 0xcf, 0x44, 0xb4, 0x3e,
  19177. 0x56, 0x75, 0x00, 0x68, 0xb5, 0x7d, 0x0f, 0x20,
  19178. 0x79, 0x5d, 0x7f, 0x12, 0x15, 0x32, 0x89, 0x61,
  19179. 0x6b, 0x29, 0xb7, 0x52, 0xf5, 0x25, 0xd8, 0x98,
  19180. 0xe8, 0x6f, 0xf9, 0x22, 0xb4, 0xbb, 0xe5, 0xff,
  19181. 0xd0, 0x92, 0x86, 0x9a, 0x88, 0xa2, 0xaf, 0x6b
  19182. };
  19183. ret = sizeof(signature_2048);
  19184. XMEMCPY(out, signature_2048, ret);
  19185. }
  19186. #endif
  19187. #if !defined(WC_NO_RNG) && !defined(WC_NO_RSA_OAEP) && \
  19188. ((!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || \
  19189. defined(WOLFSSL_PUBLIC_MP)) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  19190. idx = (word32)ret;
  19191. XMEMSET(plain, 0, plainSz);
  19192. do {
  19193. #if defined(WOLFSSL_ASYNC_CRYPT)
  19194. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19195. #endif
  19196. if (ret >= 0) {
  19197. #ifndef WOLFSSL_RSA_VERIFY_INLINE
  19198. #if defined(WOLFSSL_CRYPTOCELL)
  19199. /*
  19200. Cryptocell requires the input data and signature byte array to verify.
  19201. first argument must be the input data
  19202. second argument must be the length of input data
  19203. third argument must be the signature byte array or the output from
  19204. wc_RsaSSL_Sign()
  19205. fourth argument must be the length of the signature byte array
  19206. */
  19207. ret = wc_RsaSSL_Verify(in, inLen, out, outSz, key);
  19208. #else
  19209. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, key);
  19210. #endif /* WOLFSSL_CRYPTOCELL */
  19211. #else
  19212. byte* dec = NULL;
  19213. ret = wc_RsaSSL_VerifyInline(out, idx, &dec, key);
  19214. if (ret > 0) {
  19215. XMEMCPY(plain, dec, ret);
  19216. }
  19217. #endif
  19218. }
  19219. } while (ret == WC_PENDING_E);
  19220. if (ret < 0)
  19221. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19222. if (XMEMCMP(plain, in, (size_t)ret)) {
  19223. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  19224. }
  19225. TEST_SLEEP();
  19226. #endif
  19227. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  19228. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  19229. #if (!defined(HAVE_FIPS) || \
  19230. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  19231. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  19232. ret = rsa_oaep_padding_test(key, &rng);
  19233. if (ret != 0)
  19234. return ret;
  19235. #endif /* !HAVE_FIPS */
  19236. #endif /* WC_NO_RSA_OAEP && !WC_NO_RNG */
  19237. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  19238. #if !defined(HAVE_FIPS) && !defined(NO_ASN) \
  19239. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  19240. ret = rsa_export_key_test(key);
  19241. if (ret != 0)
  19242. return ret;
  19243. #endif
  19244. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  19245. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  19246. ret = rsa_flatten_test(key);
  19247. if (ret != 0)
  19248. return ret;
  19249. #endif
  19250. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_ASN) && \
  19251. !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  19252. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  19253. (void)clientCert;
  19254. #endif
  19255. #ifdef WOLFSSL_TEST_CERT
  19256. #if defined(WOLFSSL_MDK_ARM)
  19257. #define sizeof(s) XSTRLEN((char *)(s))
  19258. #endif
  19259. #ifdef USE_CERT_BUFFERS_1024
  19260. XMEMCPY(tmp, client_cert_der_1024, (size_t)sizeof_client_cert_der_1024);
  19261. bytes = (size_t)sizeof_client_cert_der_1024;
  19262. #elif defined(USE_CERT_BUFFERS_2048)
  19263. XMEMCPY(tmp, client_cert_der_2048, (size_t)sizeof_client_cert_der_2048);
  19264. bytes = (size_t)sizeof_client_cert_der_2048;
  19265. #elif defined(USE_CERT_BUFFERS_3072)
  19266. XMEMCPY(tmp, client_cert_der_3072, (size_t)sizeof_client_cert_der_3072);
  19267. bytes = (size_t)sizeof_client_cert_der_3072;
  19268. #elif defined(USE_CERT_BUFFERS_4096)
  19269. XMEMCPY(tmp, client_cert_der_4096, (size_t)sizeof_client_cert_der_4096);
  19270. bytes = (size_t)sizeof_client_cert_der_4096;
  19271. #elif !defined(NO_FILESYSTEM)
  19272. file2 = XFOPEN(clientCert, "rb");
  19273. if (!file2) {
  19274. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19275. }
  19276. bytes = XFREAD(tmp, 1, FOURK_BUF, file2);
  19277. XFCLOSE(file2);
  19278. if (bytes == 0)
  19279. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19280. #else
  19281. /* No certificate to use. */
  19282. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  19283. #endif
  19284. #ifdef sizeof
  19285. #undef sizeof
  19286. #endif
  19287. InitDecodedCert(cert, tmp, (word32)bytes, NULL);
  19288. ret = ParseCert(cert, CERT_TYPE, NO_VERIFY, NULL);
  19289. if (ret != 0) {
  19290. FreeDecodedCert(cert);
  19291. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19292. }
  19293. #ifndef NO_ASN_TIME
  19294. ret = wc_GetDateInfo(cert->afterDate, cert->afterDateLen, &date,
  19295. &dateFormat, &dateLength);
  19296. if (ret != 0) {
  19297. FreeDecodedCert(cert);
  19298. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19299. }
  19300. ret = wc_GetDateAsCalendarTime(date, dateLength, dateFormat, &timearg);
  19301. if (ret != 0) {
  19302. FreeDecodedCert(cert);
  19303. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19304. }
  19305. #endif
  19306. FreeDecodedCert(cert);
  19307. #endif /* WOLFSSL_TEST_CERT */
  19308. #ifdef WOLFSSL_CERT_EXT
  19309. #ifdef USE_CERT_BUFFERS_1024
  19310. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  19311. bytes = sizeof_client_keypub_der_1024;
  19312. #elif defined(USE_CERT_BUFFERS_2048)
  19313. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  19314. bytes = sizeof_client_keypub_der_2048;
  19315. #elif defined(USE_CERT_BUFFERS_3072)
  19316. XMEMCPY(tmp, client_keypub_der_3072, sizeof_client_keypub_der_3072);
  19317. bytes = sizeof_client_keypub_der_3072;
  19318. #elif defined(USE_CERT_BUFFERS_4096)
  19319. XMEMCPY(tmp, client_keypub_der_4096, sizeof_client_keypub_der_4096);
  19320. bytes = sizeof_client_keypub_der_4096;
  19321. #else
  19322. file = XFOPEN(clientKeyPub, "rb");
  19323. if (!file) {
  19324. err_sys("can't open ./certs/client-keyPub.der, "
  19325. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  19326. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19327. }
  19328. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  19329. XFCLOSE(file);
  19330. if (bytes == 0)
  19331. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19332. #endif /* USE_CERT_BUFFERS */
  19333. ret = wc_InitRsaKey(keypub, HEAP_HINT);
  19334. if (ret != 0)
  19335. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19336. idx = 0;
  19337. ret = wc_RsaPublicKeyDecode(tmp, &idx, keypub, (word32)bytes);
  19338. if (ret != 0)
  19339. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19340. #endif /* WOLFSSL_CERT_EXT */
  19341. #ifdef WOLFSSL_KEY_GEN
  19342. ret = rsa_keygen_test(&rng);
  19343. if (ret != 0)
  19344. goto exit_rsa;
  19345. #endif
  19346. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  19347. /* Make Cert / Sign example for RSA cert and RSA CA */
  19348. ret = rsa_certgen_test(key, keypub, &rng, tmp);
  19349. if (ret != 0)
  19350. goto exit_rsa;
  19351. #if !defined(NO_RSA) && defined(HAVE_ECC) && !defined(NO_ECC_SECP)
  19352. ret = rsa_ecc_certgen_test(&rng, tmp);
  19353. if (ret != 0)
  19354. goto exit_rsa;
  19355. #endif
  19356. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  19357. {
  19358. Cert *req;
  19359. int derSz;
  19360. #ifndef WOLFSSL_SMALL_STACK
  19361. byte* der = NULL;
  19362. #endif
  19363. req = (Cert *)XMALLOC(sizeof *req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19364. if (! req)
  19365. ERROR_OUT(MEMORY_E, exit_rsa);
  19366. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  19367. if (der == NULL) {
  19368. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  19369. }
  19370. ret = wc_InitCert_ex(req, HEAP_HINT, devId);
  19371. if (ret != 0)
  19372. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19373. req->version = 0;
  19374. req->isCA = 1;
  19375. XSTRNCPY(req->challengePw, "wolf123", CTC_NAME_SIZE);
  19376. XMEMCPY(&req->subject, &certDefaultName, sizeof(CertName));
  19377. #ifndef NO_SHA256
  19378. req->sigType = CTC_SHA256wRSA;
  19379. #else
  19380. req->sigType = CTC_SHAwRSA;
  19381. #endif
  19382. #ifdef WOLFSSL_CERT_EXT
  19383. /* add SKID from the Public Key */
  19384. ret = wc_SetSubjectKeyIdFromPublicKey(req, keypub, NULL);
  19385. if (ret != 0)
  19386. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19387. /* add Key Usage */
  19388. ret = wc_SetKeyUsage(req, certKeyUsage2);
  19389. if (ret != 0)
  19390. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19391. /* add Extended Key Usage */
  19392. ret = wc_SetExtKeyUsage(req,
  19393. "serverAuth,clientAuth,codeSigning,"
  19394. "emailProtection,timeStamping,OCSPSigning");
  19395. if (ret != 0)
  19396. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19397. #ifdef WOLFSSL_EKU_OID
  19398. {
  19399. WOLFSSL_SMALL_STACK_STATIC const char unique[] =
  19400. "2.16.840.1.111111.100.1.10.1";
  19401. ret = wc_SetExtKeyUsageOID(req, unique, sizeof(unique), 0,
  19402. HEAP_HINT);
  19403. if (ret != 0)
  19404. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19405. }
  19406. #endif /* WOLFSSL_EKU_OID */
  19407. #endif /* WOLFSSL_CERT_EXT */
  19408. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  19409. if (derSz < 0) {
  19410. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  19411. }
  19412. #ifdef WOLFSSL_CERT_EXT
  19413. /* Try again with "any" flag set, will override all others */
  19414. ret = wc_SetExtKeyUsage(req, "any");
  19415. if (ret != 0)
  19416. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19417. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  19418. if (derSz < 0) {
  19419. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  19420. }
  19421. #endif /* WOLFSSL_CERT_EXT */
  19422. ret = 0;
  19423. do {
  19424. #if defined(WOLFSSL_ASYNC_CRYPT)
  19425. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19426. #endif
  19427. if (ret >= 0) {
  19428. ret = wc_SignCert(req->bodySz, req->sigType, der, FOURK_BUF,
  19429. key, NULL, &rng);
  19430. }
  19431. } while (ret == WC_PENDING_E);
  19432. if (ret < 0)
  19433. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  19434. derSz = (int)ret;
  19435. ret = SaveDerAndPem(der, derSz, certReqDerFile, certReqPemFile,
  19436. CERTREQ_TYPE);
  19437. if (ret != 0) {
  19438. goto exit_rsa;
  19439. }
  19440. derSz = wc_MakeCertReq_ex(req, der, FOURK_BUF, RSA_TYPE, key);
  19441. if (derSz < 0) {
  19442. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  19443. }
  19444. /* Test getting the size of the buffer without providing the buffer.
  19445. * derSz is set to the "largest buffer" we are willing to allocate. */
  19446. derSz = wc_MakeCertReq(req, NULL, 10000, key, NULL);
  19447. if (derSz < 0) {
  19448. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  19449. }
  19450. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19451. XFREE(req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19452. der = NULL;
  19453. }
  19454. #endif /* WOLFSSL_CERT_REQ */
  19455. #endif /* WOLFSSL_CERT_GEN */
  19456. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  19457. /* Need to create known good signatures to test with this. */
  19458. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  19459. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  19460. ret = rsa_pss_test(&rng, key);
  19461. if (ret != 0)
  19462. goto exit_rsa;
  19463. #endif
  19464. #endif
  19465. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  19466. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19467. /* New key to be loaded in rsa_even_mod_test(). */
  19468. if (key != NULL)
  19469. #endif
  19470. wc_FreeRsaKey(key);
  19471. /* New key to be loaded in rsa_even_mod_test(). */
  19472. ret = rsa_even_mod_test(&rng, key);
  19473. #endif
  19474. exit_rsa:
  19475. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19476. if (key != NULL) {
  19477. wc_FreeRsaKey(key);
  19478. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19479. }
  19480. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  19481. if (keypub != NULL) {
  19482. wc_FreeRsaKey(keypub);
  19483. XFREE(keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19484. }
  19485. #endif
  19486. #ifdef WOLFSSL_TEST_CERT
  19487. if (cert != NULL)
  19488. XFREE(cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19489. #endif
  19490. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19491. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19492. #else
  19493. wc_FreeRsaKey(key);
  19494. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  19495. wc_FreeRsaKey(keypub);
  19496. #endif
  19497. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  19498. wc_FreeRng(&rng);
  19499. WC_FREE_VAR(in, HEAP_HINT);
  19500. WC_FREE_VAR(out, HEAP_HINT);
  19501. WC_FREE_VAR(plain, HEAP_HINT);
  19502. (void)res;
  19503. (void)bytes;
  19504. (void)idx;
  19505. (void)in;
  19506. (void)out;
  19507. (void)plain;
  19508. (void)idx;
  19509. (void)inStr;
  19510. (void)inLen;
  19511. (void)outSz;
  19512. (void)plainSz;
  19513. /* ret can be greater then 0 with certgen but all negative values should
  19514. * be returned and treated as an error */
  19515. if (ret >= 0) {
  19516. return 0;
  19517. }
  19518. else {
  19519. return ret;
  19520. }
  19521. }
  19522. #endif /* !NO_RSA */
  19523. #ifndef NO_DH
  19524. static wc_test_ret_t dh_fips_generate_test(WC_RNG *rng)
  19525. {
  19526. wc_test_ret_t ret = 0;
  19527. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19528. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19529. #else
  19530. DhKey key[1];
  19531. #endif
  19532. WOLFSSL_SMALL_STACK_STATIC const byte p[] = {
  19533. 0xc5, 0x7c, 0xa2, 0x4f, 0x4b, 0xd6, 0x8c, 0x3c,
  19534. 0xda, 0xc7, 0xba, 0xaa, 0xea, 0x2e, 0x5c, 0x1e,
  19535. 0x18, 0xb2, 0x7b, 0x8c, 0x55, 0x65, 0x9f, 0xea,
  19536. 0xe0, 0xa1, 0x36, 0x53, 0x2b, 0x36, 0xe0, 0x4e,
  19537. 0x3e, 0x64, 0xa9, 0xe4, 0xfc, 0x8f, 0x32, 0x62,
  19538. 0x97, 0xe4, 0xbe, 0xf7, 0xc1, 0xde, 0x07, 0x5a,
  19539. 0x89, 0x28, 0xf3, 0xfe, 0x4f, 0xfe, 0x68, 0xbc,
  19540. 0xfb, 0x0a, 0x7c, 0xa4, 0xb3, 0x14, 0x48, 0x89,
  19541. 0x9f, 0xaf, 0xb8, 0x43, 0xe2, 0xa0, 0x62, 0x5c,
  19542. 0xb4, 0x88, 0x3f, 0x06, 0x50, 0x11, 0xfe, 0x65,
  19543. 0x8d, 0x49, 0xd2, 0xf5, 0x4b, 0x74, 0x79, 0xdb,
  19544. 0x06, 0x62, 0x92, 0x89, 0xed, 0xda, 0xcb, 0x87,
  19545. 0x37, 0x16, 0xd2, 0xa1, 0x7a, 0xe8, 0xde, 0x92,
  19546. 0xee, 0x3e, 0x41, 0x4a, 0x91, 0x5e, 0xed, 0xf3,
  19547. 0x6c, 0x6b, 0x7e, 0xfd, 0x15, 0x92, 0x18, 0xfc,
  19548. 0xa7, 0xac, 0x42, 0x85, 0x57, 0xe9, 0xdc, 0xda,
  19549. 0x55, 0xc9, 0x8b, 0x28, 0x9e, 0xc1, 0xc4, 0x46,
  19550. 0x4d, 0x88, 0xed, 0x62, 0x8e, 0xdb, 0x3f, 0xb9,
  19551. 0xd7, 0xc8, 0xe3, 0xcf, 0xb8, 0x34, 0x2c, 0xd2,
  19552. 0x6f, 0x28, 0x06, 0x41, 0xe3, 0x66, 0x8c, 0xfc,
  19553. 0x72, 0xff, 0x26, 0x3b, 0x6b, 0x6c, 0x6f, 0x73,
  19554. 0xde, 0xf2, 0x90, 0x29, 0xe0, 0x61, 0x32, 0xc4,
  19555. 0x12, 0x74, 0x09, 0x52, 0xec, 0xf3, 0x1b, 0xa6,
  19556. 0x45, 0x98, 0xac, 0xf9, 0x1c, 0x65, 0x8e, 0x3a,
  19557. 0x91, 0x84, 0x4b, 0x23, 0x8a, 0xb2, 0x3c, 0xc9,
  19558. 0xfa, 0xea, 0xf1, 0x38, 0xce, 0xd8, 0x05, 0xe0,
  19559. 0xfa, 0x44, 0x68, 0x1f, 0xeb, 0xd9, 0x57, 0xb8,
  19560. 0x4a, 0x97, 0x5b, 0x88, 0xc5, 0xf1, 0xbb, 0xb0,
  19561. 0x49, 0xc3, 0x91, 0x7c, 0xd3, 0x13, 0xb9, 0x47,
  19562. 0xbb, 0x91, 0x8f, 0xe5, 0x26, 0x07, 0xab, 0xa9,
  19563. 0xc5, 0xd0, 0x3d, 0x95, 0x41, 0x26, 0x92, 0x9d,
  19564. 0x13, 0x67, 0xf2, 0x7e, 0x11, 0x88, 0xdc, 0x2d
  19565. };
  19566. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  19567. 0x4a, 0x1a, 0xf3, 0xa4, 0x92, 0xe9, 0xee, 0x74,
  19568. 0x6e, 0x57, 0xd5, 0x8c, 0x2c, 0x5b, 0x41, 0x41,
  19569. 0x5e, 0xd4, 0x55, 0x19, 0xdc, 0xd9, 0x32, 0x91,
  19570. 0xf7, 0xfd, 0xc2, 0x57, 0xff, 0x03, 0x14, 0xdb,
  19571. 0xf1, 0xb7, 0x60, 0x0c, 0x43, 0x59, 0x3f, 0xff,
  19572. 0xac, 0xf1, 0x80, 0x9a, 0x15, 0x6f, 0xd8, 0x6e,
  19573. 0xb7, 0x85, 0x18, 0xc8, 0xec, 0x4e, 0x59, 0x4a,
  19574. 0xe2, 0x91, 0x43, 0x4c, 0xeb, 0x95, 0xb6, 0x2e,
  19575. 0x9a, 0xea, 0x53, 0x68, 0x80, 0x64, 0x69, 0x40,
  19576. 0xf9, 0xec, 0xbd, 0x85, 0x89, 0x26, 0x97, 0x67,
  19577. 0xaf, 0xb0, 0xad, 0x00, 0x1b, 0xd4, 0xfd, 0x94,
  19578. 0xd3, 0xe9, 0x92, 0xb1, 0xb4, 0xbc, 0x5a, 0xaa,
  19579. 0x92, 0x80, 0x89, 0x3b, 0x39, 0x05, 0x6c, 0x22,
  19580. 0x26, 0xfe, 0x5a, 0x28, 0x6c, 0x37, 0x50, 0x5a,
  19581. 0x38, 0x99, 0xcf, 0xf3, 0xc1, 0x96, 0x45, 0xdc,
  19582. 0x01, 0xcb, 0x20, 0x87, 0xa5, 0x00, 0x8c, 0xf5,
  19583. 0x4d, 0xc2, 0xef, 0xb8, 0x9b, 0xd1, 0x87, 0xbe,
  19584. 0xed, 0xd5, 0x0a, 0x29, 0x15, 0x34, 0x59, 0x4c,
  19585. 0x3a, 0x05, 0x22, 0x05, 0x44, 0x4f, 0x9f, 0xc8,
  19586. 0x47, 0x12, 0x24, 0x8e, 0xa8, 0x79, 0xe4, 0x67,
  19587. 0xba, 0x4d, 0x5b, 0x75, 0x56, 0x95, 0xeb, 0xe8,
  19588. 0x8a, 0xfa, 0x8e, 0x01, 0x8c, 0x1b, 0x74, 0x63,
  19589. 0xd9, 0x2f, 0xf7, 0xd3, 0x44, 0x8f, 0xa8, 0xf5,
  19590. 0xaf, 0x6c, 0x4f, 0xdb, 0xe7, 0xc9, 0x6c, 0x71,
  19591. 0x22, 0xa3, 0x1d, 0xf1, 0x40, 0xb2, 0xe0, 0x9a,
  19592. 0xb6, 0x72, 0xc9, 0xc0, 0x13, 0x16, 0xa2, 0x4a,
  19593. 0xe1, 0x92, 0xc7, 0x54, 0x23, 0xab, 0x9d, 0xa1,
  19594. 0xa1, 0xe5, 0x0b, 0xed, 0xba, 0xe8, 0x84, 0x37,
  19595. 0xb2, 0xe7, 0xfe, 0x32, 0x8d, 0xfa, 0x1c, 0x53,
  19596. 0x77, 0x97, 0xc7, 0xf3, 0x48, 0xc9, 0xdb, 0x2d,
  19597. 0x75, 0x52, 0x9d, 0x42, 0x51, 0x78, 0x62, 0x68,
  19598. 0x05, 0x45, 0x15, 0xf8, 0xa2, 0x4e, 0xf3, 0x0b
  19599. };
  19600. WOLFSSL_SMALL_STACK_STATIC const byte q[] = {
  19601. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  19602. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  19603. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  19604. 0x40, 0x52, 0xed, 0x41
  19605. };
  19606. WOLFSSL_SMALL_STACK_STATIC const byte q0[] = {
  19607. 0x00,
  19608. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  19609. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  19610. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  19611. 0x40, 0x52, 0xed, 0x41
  19612. };
  19613. byte priv[256];
  19614. byte pub[256];
  19615. word32 privSz = sizeof(priv);
  19616. word32 pubSz = sizeof(pub);
  19617. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19618. if (key == NULL)
  19619. ERROR_OUT(MEMORY_E, exit_gen_test);
  19620. #endif
  19621. /* Parameter Validation testing. */
  19622. ret = wc_DhGenerateKeyPair(NULL, rng, priv, &privSz, pub, &pubSz);
  19623. if (ret != BAD_FUNC_ARG)
  19624. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19625. ret = wc_DhGenerateKeyPair(key, NULL, priv, &privSz, pub, &pubSz);
  19626. if (ret != BAD_FUNC_ARG)
  19627. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19628. ret = wc_DhGenerateKeyPair(key, rng, NULL, &privSz, pub, &pubSz);
  19629. if (ret != BAD_FUNC_ARG)
  19630. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19631. ret = wc_DhGenerateKeyPair(key, rng, priv, NULL, pub, &pubSz);
  19632. if (ret != BAD_FUNC_ARG)
  19633. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19634. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, NULL, &pubSz);
  19635. if (ret != BAD_FUNC_ARG)
  19636. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19637. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, NULL);
  19638. if (ret != BAD_FUNC_ARG)
  19639. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19640. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  19641. if (ret != 0)
  19642. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19643. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q0, sizeof(q0));
  19644. if (ret != 0)
  19645. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19646. wc_FreeDhKey(key);
  19647. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  19648. if (ret != 0)
  19649. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19650. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q, sizeof(q));
  19651. if (ret != 0)
  19652. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19653. /* Use API. */
  19654. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  19655. #if defined(WOLFSSL_ASYNC_CRYPT)
  19656. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  19657. #endif
  19658. if (ret != 0)
  19659. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19660. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q0, sizeof(q0));
  19661. if (ret != 0)
  19662. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19663. wc_FreeDhKey(key);
  19664. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  19665. if (ret != 0)
  19666. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19667. ret = wc_DhSetKey(key, p, sizeof(p), g, sizeof(g));
  19668. if (ret != 0)
  19669. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19670. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q, sizeof(q));
  19671. if (ret != 0)
  19672. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19673. #ifndef HAVE_SELFTEST
  19674. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  19675. if (ret != 0)
  19676. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19677. /* Taint the public key so the check fails. */
  19678. pub[0]++;
  19679. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  19680. if (ret != MP_CMP_E) {
  19681. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19682. }
  19683. #ifdef WOLFSSL_KEY_GEN
  19684. wc_FreeDhKey(key);
  19685. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  19686. if (ret != 0)
  19687. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19688. ret = wc_DhGenerateParams(rng, 2048, key);
  19689. if (ret != 0)
  19690. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19691. privSz = sizeof(priv);
  19692. pubSz = sizeof(pub);
  19693. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  19694. #if defined(WOLFSSL_ASYNC_CRYPT)
  19695. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  19696. #endif
  19697. if (ret != 0)
  19698. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19699. #endif /* WOLFSSL_KEY_GEN */
  19700. #endif /* HAVE_SELFTEST */
  19701. ret = 0;
  19702. exit_gen_test:
  19703. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19704. if (key) {
  19705. wc_FreeDhKey(key);
  19706. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19707. }
  19708. #else
  19709. wc_FreeDhKey(key);
  19710. #endif
  19711. return ret;
  19712. }
  19713. static wc_test_ret_t dh_generate_test(WC_RNG *rng)
  19714. {
  19715. wc_test_ret_t ret = 0;
  19716. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19717. DhKey *smallKey = NULL;
  19718. #else
  19719. DhKey smallKey[1];
  19720. #endif
  19721. byte p[2] = { 1, 7 }; /* 263 in decimal */
  19722. byte g[2] = { 0, 2 };
  19723. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  19724. #ifdef WOLFSSL_DH_CONST
  19725. /* the table for constant DH lookup will round to the lowest byte size 21 */
  19726. byte priv[21];
  19727. byte pub[21];
  19728. #else
  19729. byte priv[2];
  19730. byte pub[2];
  19731. #endif
  19732. word32 privSz = sizeof(priv);
  19733. word32 pubSz = sizeof(pub);
  19734. #endif
  19735. int smallKey_inited = 0;
  19736. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19737. if ((smallKey = (DhKey *)XMALLOC(sizeof(*smallKey), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  19738. return WC_TEST_RET_ENC_ERRNO;
  19739. #endif
  19740. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  19741. if (ret != 0)
  19742. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19743. smallKey_inited = 1;
  19744. /* Parameter Validation testing. */
  19745. ret = wc_InitDhKey_ex(NULL, HEAP_HINT, devId);
  19746. if (ret != BAD_FUNC_ARG)
  19747. return WC_TEST_RET_ENC_EC(ret);
  19748. wc_FreeDhKey(NULL);
  19749. ret = wc_DhSetKey(NULL, p, sizeof(p), g, sizeof(g));
  19750. if (ret != BAD_FUNC_ARG) {
  19751. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19752. }
  19753. ret = wc_DhSetKey(smallKey, NULL, sizeof(p), g, sizeof(g));
  19754. if (ret != BAD_FUNC_ARG) {
  19755. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19756. }
  19757. ret = wc_DhSetKey(smallKey, p, 0, g, sizeof(g));
  19758. if (ret != BAD_FUNC_ARG) {
  19759. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19760. }
  19761. ret = wc_DhSetKey(smallKey, p, sizeof(p), NULL, sizeof(g));
  19762. if (ret != BAD_FUNC_ARG) {
  19763. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19764. }
  19765. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, 0);
  19766. if (ret != BAD_FUNC_ARG) {
  19767. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19768. }
  19769. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, sizeof(g));
  19770. if (ret != 0)
  19771. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19772. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  19773. /* Use API. */
  19774. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  19775. #if defined(WOLFSSL_ASYNC_CRYPT)
  19776. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  19777. #endif
  19778. if (ret != 0) {
  19779. ret = WC_TEST_RET_ENC_EC(ret);
  19780. }
  19781. #else
  19782. (void)rng;
  19783. #if defined(HAVE_FIPS) || !defined(WOLFSSL_NO_DH186)
  19784. ret = 0;
  19785. #endif
  19786. #endif
  19787. #if !defined(HAVE_FIPS) && defined(WOLFSSL_NO_DH186)
  19788. {
  19789. byte priv[260];
  19790. byte pub[260];
  19791. word32 privSz = sizeof(priv);
  19792. word32 pubSz = sizeof(pub);
  19793. /* test odd ball param generation with DH */
  19794. wc_FreeDhKey(smallKey);
  19795. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  19796. if (ret != 0)
  19797. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19798. /* should fail since modSz is 16 and group size is 20 */
  19799. ret = wc_DhGenerateParams(rng, 128, smallKey);
  19800. if (ret == 0)
  19801. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19802. ret = wc_DhGenerateParams(rng, 2056, smallKey);
  19803. if (ret != 0)
  19804. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19805. privSz = sizeof(priv);
  19806. pubSz = sizeof(pub);
  19807. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  19808. #if defined(WOLFSSL_ASYNC_CRYPT)
  19809. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  19810. #endif
  19811. if (ret != 0)
  19812. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  19813. }
  19814. #endif /* !HAVE_FIPS and WOLFSSL_NO_DH186 */
  19815. exit_gen_test:
  19816. if (smallKey_inited)
  19817. wc_FreeDhKey(smallKey);
  19818. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19819. if (smallKey != NULL)
  19820. XFREE(smallKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19821. #endif
  19822. return ret;
  19823. }
  19824. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  19825. typedef struct dh_pubvalue_test {
  19826. const byte* data;
  19827. word32 len;
  19828. } dh_pubvalue_test;
  19829. static wc_test_ret_t dh_test_check_pubvalue(void)
  19830. {
  19831. wc_test_ret_t ret;
  19832. word32 i;
  19833. WOLFSSL_SMALL_STACK_STATIC const byte prime[] = {0x01, 0x00, 0x01};
  19834. WOLFSSL_SMALL_STACK_STATIC const byte pubValZero[] = { 0x00 };
  19835. WOLFSSL_SMALL_STACK_STATIC const byte pubValZeroLong[] = { 0x00, 0x00, 0x00 };
  19836. WOLFSSL_SMALL_STACK_STATIC const byte pubValOne[] = { 0x01 };
  19837. WOLFSSL_SMALL_STACK_STATIC const byte pubValOneLong[] = { 0x00, 0x00, 0x01 };
  19838. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeMinusOne[] = { 0x01, 0x00, 0x00 };
  19839. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeLong[] = {0x00, 0x01, 0x00, 0x01};
  19840. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimePlusOne[] = { 0x01, 0x00, 0x02 };
  19841. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig0[] = { 0x02, 0x00, 0x01 };
  19842. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig1[] = { 0x01, 0x01, 0x01 };
  19843. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooLong[] = { 0x01, 0x00, 0x00, 0x01 };
  19844. const dh_pubvalue_test dh_pubval_fail[] = {
  19845. { prime, sizeof(prime) },
  19846. { pubValZero, sizeof(pubValZero) },
  19847. { pubValZeroLong, sizeof(pubValZeroLong) },
  19848. { pubValOne, sizeof(pubValOne) },
  19849. { pubValOneLong, sizeof(pubValOneLong) },
  19850. { pubValPrimeMinusOne, sizeof(pubValPrimeMinusOne) },
  19851. { pubValPrimeLong, sizeof(pubValPrimeLong) },
  19852. { pubValPrimePlusOne, sizeof(pubValPrimePlusOne) },
  19853. { pubValTooBig0, sizeof(pubValTooBig0) },
  19854. { pubValTooBig1, sizeof(pubValTooBig1) },
  19855. { pubValTooLong, sizeof(pubValTooLong) },
  19856. };
  19857. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwo[] = { 0x02 };
  19858. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwoLong[] = { 0x00, 0x00, 0x02 };
  19859. WOLFSSL_SMALL_STACK_STATIC const byte pubValGood[] = { 0x12, 0x34 };
  19860. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLen[] = { 0x00, 0x12, 0x34 };
  19861. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLong[] = { 0x00, 0x00, 0x12, 0x34 };
  19862. const dh_pubvalue_test dh_pubval_pass[] = {
  19863. { pubValTwo, sizeof(pubValTwo) },
  19864. { pubValTwoLong, sizeof(pubValTwoLong) },
  19865. { pubValGood, sizeof(pubValGood) },
  19866. { pubValGoodLen, sizeof(pubValGoodLen) },
  19867. { pubValGoodLong, sizeof(pubValGoodLong) },
  19868. };
  19869. for (i = 0; i < sizeof(dh_pubval_fail) / sizeof(*dh_pubval_fail); i++) {
  19870. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_fail[i].data,
  19871. dh_pubval_fail[i].len);
  19872. if (ret != MP_VAL)
  19873. return WC_TEST_RET_ENC_I(i);
  19874. }
  19875. for (i = 0; i < sizeof(dh_pubval_pass) / sizeof(*dh_pubval_pass); i++) {
  19876. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_pass[i].data,
  19877. dh_pubval_pass[i].len);
  19878. if (ret != 0)
  19879. return WC_TEST_RET_ENC_I(i);
  19880. }
  19881. return 0;
  19882. }
  19883. #endif
  19884. #if defined(HAVE_FFDHE)
  19885. #if defined(HAVE_FFDHE_4096)
  19886. #define MAX_DH_PRIV_SZ 39
  19887. #define MAX_DH_KEY_SZ 512
  19888. #elif defined(HAVE_FFDHE_3072)
  19889. #define MAX_DH_PRIV_SZ 34
  19890. #define MAX_DH_KEY_SZ 384
  19891. #else
  19892. #define MAX_DH_PRIV_SZ 29
  19893. #define MAX_DH_KEY_SZ 256
  19894. #endif
  19895. #ifndef WC_NO_RNG
  19896. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  19897. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  19898. #ifdef HAVE_PUBLIC_FFDHE
  19899. static wc_test_ret_t dh_ffdhe_test(WC_RNG *rng, const DhParams* params)
  19900. #else
  19901. static wc_test_ret_t dh_ffdhe_test(WC_RNG *rng, int name)
  19902. #endif
  19903. {
  19904. wc_test_ret_t ret;
  19905. word32 privSz, pubSz, privSz2, pubSz2;
  19906. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19907. byte *priv = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19908. byte *pub = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19909. byte *priv2 = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19910. byte *pub2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19911. byte *agree = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19912. byte *agree2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19913. DhKey *key = (DhKey*)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19914. DhKey *key2 = (DhKey*)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19915. #else
  19916. byte priv[MAX_DH_PRIV_SZ];
  19917. byte pub[MAX_DH_KEY_SZ];
  19918. byte priv2[MAX_DH_PRIV_SZ];
  19919. byte pub2[MAX_DH_KEY_SZ];
  19920. byte agree[MAX_DH_KEY_SZ];
  19921. byte agree2[MAX_DH_KEY_SZ];
  19922. DhKey key[1];
  19923. DhKey key2[1];
  19924. #endif
  19925. word32 agreeSz = MAX_DH_KEY_SZ;
  19926. word32 agreeSz2 = MAX_DH_KEY_SZ;
  19927. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19928. if ((priv == NULL) ||
  19929. (pub == NULL) ||
  19930. (priv2 == NULL) ||
  19931. (pub2 == NULL) ||
  19932. (agree == NULL) ||
  19933. (agree2 == NULL) ||
  19934. (key == NULL) ||
  19935. (key2 == NULL))
  19936. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  19937. #endif
  19938. pubSz = MAX_DH_KEY_SZ;
  19939. pubSz2 = MAX_DH_KEY_SZ;
  19940. #ifdef HAVE_PUBLIC_FFDHE
  19941. privSz = MAX_DH_PRIV_SZ;
  19942. privSz2 = MAX_DH_PRIV_SZ;
  19943. #else
  19944. privSz = wc_DhGetNamedKeyMinSize(name);
  19945. privSz2 = privSz;
  19946. #endif
  19947. XMEMSET(key, 0, sizeof(*key));
  19948. XMEMSET(key2, 0, sizeof(*key2));
  19949. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  19950. if (ret != 0)
  19951. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19952. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  19953. if (ret != 0)
  19954. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19955. #ifdef HAVE_PUBLIC_FFDHE
  19956. ret = wc_DhSetKey(key, params->p, params->p_len, params->g, params->g_len);
  19957. #else
  19958. ret = wc_DhSetNamedKey(key, name);
  19959. #endif
  19960. if (ret != 0)
  19961. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19962. #ifdef HAVE_PUBLIC_FFDHE
  19963. ret = wc_DhSetKey(key2, params->p, params->p_len, params->g,
  19964. params->g_len);
  19965. #else
  19966. ret = wc_DhSetNamedKey(key2, name);
  19967. #endif
  19968. if (ret != 0)
  19969. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19970. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  19971. #if defined(WOLFSSL_ASYNC_CRYPT)
  19972. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  19973. #endif
  19974. if (ret != 0)
  19975. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19976. ret = wc_DhGenerateKeyPair(key2, rng, priv2, &privSz2, pub2, &pubSz2);
  19977. #if defined(WOLFSSL_ASYNC_CRYPT)
  19978. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  19979. #endif
  19980. if (ret != 0)
  19981. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19982. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  19983. #if defined(WOLFSSL_ASYNC_CRYPT)
  19984. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  19985. #endif
  19986. if (ret != 0)
  19987. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19988. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  19989. #if defined(WOLFSSL_ASYNC_CRYPT)
  19990. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  19991. #endif
  19992. if (ret != 0)
  19993. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  19994. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  19995. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  19996. }
  19997. #if (defined(WOLFSSL_HAVE_SP_DH) || defined(USE_FAST_MATH)) && \
  19998. !defined(HAVE_INTEL_QA)
  19999. /* Make p even */
  20000. key->p.dp[0] &= (mp_digit)-2;
  20001. if (ret != 0)
  20002. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20003. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  20004. #if defined(WOLFSSL_ASYNC_CRYPT)
  20005. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  20006. #endif
  20007. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  20008. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20009. }
  20010. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  20011. #if defined(WOLFSSL_ASYNC_CRYPT)
  20012. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  20013. #endif
  20014. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != ASYNC_OP_E) {
  20015. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20016. }
  20017. #ifndef HAVE_SELFTEST
  20018. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  20019. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_CMP_E &&
  20020. ret != ASYNC_OP_E) {
  20021. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20022. }
  20023. #endif
  20024. /* Getting here means success - set ret to 0. */
  20025. ret = 0;
  20026. #endif /* (SP DH or Fast Math) and not Intel QuickAssist */
  20027. done:
  20028. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) && \
  20029. !defined(WC_NO_RNG)
  20030. if (priv)
  20031. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20032. if (pub)
  20033. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20034. if (priv2)
  20035. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20036. if (pub2)
  20037. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20038. if (agree)
  20039. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20040. if (agree2)
  20041. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20042. if (key) {
  20043. wc_FreeDhKey(key);
  20044. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20045. }
  20046. if (key2) {
  20047. wc_FreeDhKey(key2);
  20048. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20049. }
  20050. #else
  20051. wc_FreeDhKey(key);
  20052. wc_FreeDhKey(key2);
  20053. #endif
  20054. return ret;
  20055. }
  20056. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  20057. #endif /* !WC_NO_RNG */
  20058. #endif /* HAVE_FFDHE */
  20059. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dh_test(void)
  20060. {
  20061. wc_test_ret_t ret;
  20062. word32 bytes;
  20063. word32 idx = 0, privSz, pubSz, privSz2, pubSz2;
  20064. #ifndef WC_NO_RNG
  20065. WC_RNG rng;
  20066. int rngInit = 0;
  20067. #endif
  20068. int keyInit = 0, key2Init = 0;
  20069. #define DH_TEST_TMP_SIZE 1024
  20070. #if !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  20071. #define DH_TEST_BUF_SIZE 256
  20072. #else
  20073. #define DH_TEST_BUF_SIZE 512
  20074. #endif
  20075. #ifndef WC_NO_RNG
  20076. word32 agreeSz = DH_TEST_BUF_SIZE;
  20077. word32 agreeSz2 = DH_TEST_BUF_SIZE;
  20078. #endif
  20079. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20080. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20081. DhKey *key2 = (DhKey *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20082. byte *tmp = (byte *)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20083. #else
  20084. DhKey key[1];
  20085. DhKey key2[1];
  20086. byte tmp[DH_TEST_TMP_SIZE];
  20087. #endif
  20088. #ifndef WC_NO_RNG
  20089. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20090. byte *priv = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20091. byte *pub = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20092. byte *priv2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20093. byte *pub2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20094. byte *agree = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20095. byte *agree2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20096. if (priv == NULL || pub == NULL || priv2 == NULL || pub2 == NULL ||
  20097. agree == NULL || agree2 == NULL) {
  20098. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20099. }
  20100. #else
  20101. byte priv[DH_TEST_BUF_SIZE];
  20102. byte pub[DH_TEST_BUF_SIZE];
  20103. byte priv2[DH_TEST_BUF_SIZE];
  20104. byte pub2[DH_TEST_BUF_SIZE];
  20105. byte agree[DH_TEST_BUF_SIZE];
  20106. byte agree2[DH_TEST_BUF_SIZE];
  20107. #endif
  20108. #endif /* !WC_NO_RNG */
  20109. WOLFSSL_ENTER("dh_test");
  20110. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20111. if (key == NULL || key2 == NULL || tmp == NULL) {
  20112. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20113. }
  20114. #endif
  20115. #ifdef USE_CERT_BUFFERS_1024
  20116. XMEMCPY(tmp, dh_key_der_1024, (size_t)sizeof_dh_key_der_1024);
  20117. bytes = (size_t)sizeof_dh_key_der_1024;
  20118. #elif defined(USE_CERT_BUFFERS_2048)
  20119. XMEMCPY(tmp, dh_key_der_2048, (size_t)sizeof_dh_key_der_2048);
  20120. bytes = (size_t)sizeof_dh_key_der_2048;
  20121. #elif defined(USE_CERT_BUFFERS_3072)
  20122. XMEMCPY(tmp, dh_key_der_3072, (size_t)sizeof_dh_key_der_3072);
  20123. bytes = (size_t)sizeof_dh_key_der_3072;
  20124. #elif defined(USE_CERT_BUFFERS_4096)
  20125. XMEMCPY(tmp, dh_key_der_4096, (size_t)sizeof_dh_key_der_4096);
  20126. bytes = (size_t)sizeof_dh_key_der_4096;
  20127. #elif defined(NO_ASN)
  20128. /* don't use file, no DER parsing */
  20129. #elif !defined(NO_FILESYSTEM)
  20130. {
  20131. XFILE file = XFOPEN(dhParamsFile, "rb");
  20132. if (! file)
  20133. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20134. bytes = (word32) XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  20135. XFCLOSE(file);
  20136. if (bytes == 0)
  20137. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20138. }
  20139. #else
  20140. /* No DH key to use. */
  20141. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  20142. #endif /* USE_CERT_BUFFERS */
  20143. (void)idx;
  20144. (void)tmp;
  20145. (void)bytes;
  20146. pubSz = DH_TEST_BUF_SIZE;
  20147. pubSz2 = DH_TEST_BUF_SIZE;
  20148. privSz = DH_TEST_BUF_SIZE;
  20149. privSz2 = DH_TEST_BUF_SIZE;
  20150. #ifndef WC_NO_RNG
  20151. XMEMSET(&rng, 0, sizeof(rng));
  20152. #endif
  20153. /* Use API for coverage. */
  20154. ret = wc_InitDhKey(key);
  20155. if (ret != 0)
  20156. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20157. wc_FreeDhKey(key);
  20158. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  20159. if (ret != 0)
  20160. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20161. keyInit = 1;
  20162. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  20163. if (ret != 0)
  20164. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20165. key2Init = 1;
  20166. #ifdef NO_ASN
  20167. #ifndef WOLFSSL_SP_MATH
  20168. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  20169. if (ret != 0)
  20170. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20171. ret = wc_DhSetKey(key2, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  20172. if (ret != 0)
  20173. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20174. #else
  20175. ret = wc_DhSetKey(key, dh2048_p, sizeof(dh2048_p), dh2048_g,
  20176. sizeof(dh2048_g));
  20177. if (ret != 0)
  20178. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20179. ret = wc_DhSetKey(key2, dh2048_p, sizeof(dh2048_p), dh2048_g,
  20180. sizeof(dh2048_g));
  20181. if (ret != 0)
  20182. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20183. #endif
  20184. #else
  20185. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  20186. if (ret != 0)
  20187. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20188. idx = 0;
  20189. ret = wc_DhKeyDecode(tmp, &idx, key2, bytes);
  20190. if (ret != 0)
  20191. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20192. #endif
  20193. #ifndef WC_NO_RNG
  20194. #ifndef HAVE_FIPS
  20195. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  20196. #else
  20197. ret = wc_InitRng(&rng);
  20198. #endif
  20199. if (ret != 0)
  20200. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20201. rngInit = 1;
  20202. ret = wc_DhGenerateKeyPair(key, &rng, priv, &privSz, pub, &pubSz);
  20203. #if defined(WOLFSSL_ASYNC_CRYPT)
  20204. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  20205. #endif
  20206. if (ret != 0)
  20207. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20208. ret = wc_DhGenerateKeyPair(key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  20209. #if defined(WOLFSSL_ASYNC_CRYPT)
  20210. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  20211. #endif
  20212. if (ret != 0)
  20213. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20214. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  20215. #if defined(WOLFSSL_ASYNC_CRYPT)
  20216. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  20217. #endif
  20218. if (ret != 0)
  20219. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20220. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  20221. #if defined(WOLFSSL_ASYNC_CRYPT)
  20222. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  20223. #endif
  20224. if (ret != 0)
  20225. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20226. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  20227. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  20228. }
  20229. #endif /* !WC_NO_RNG */
  20230. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  20231. ret = wc_DhCheckPrivKey(NULL, NULL, 0);
  20232. if (ret != BAD_FUNC_ARG)
  20233. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20234. ret = wc_DhCheckPrivKey(key, priv, privSz);
  20235. if (ret != 0)
  20236. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20237. ret = wc_DhExportParamsRaw(NULL, NULL, NULL, NULL, NULL, NULL, NULL);
  20238. if (ret != BAD_FUNC_ARG)
  20239. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20240. {
  20241. word32 pSz, qSz, gSz;
  20242. ret = wc_DhExportParamsRaw(key, NULL, &pSz, NULL, &qSz, NULL, &gSz);
  20243. if (ret != LENGTH_ONLY_E)
  20244. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20245. }
  20246. #endif
  20247. /* Test DH key import / export */
  20248. #if defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM) && \
  20249. (!defined(HAVE_FIPS) || \
  20250. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  20251. wc_FreeDhKey(key);
  20252. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  20253. if (ret != 0)
  20254. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20255. #ifndef NO_ASN
  20256. {
  20257. /* DH Private - Key Export / Import */
  20258. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20259. byte *tmp2;
  20260. #else
  20261. byte tmp2[DH_TEST_TMP_SIZE];
  20262. #endif
  20263. #if defined(USE_CERT_BUFFERS_2048)
  20264. XMEMCPY(tmp, dh_ffdhe_statickey_der_2048, sizeof_dh_ffdhe_statickey_der_2048);
  20265. bytes = sizeof_dh_ffdhe_statickey_der_2048;
  20266. #else
  20267. XFILE file = XFOPEN(dhKeyFile, "rb");
  20268. if (!file)
  20269. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20270. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  20271. XFCLOSE(file);
  20272. if (bytes == 0)
  20273. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20274. #endif
  20275. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20276. tmp2 = (byte*)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20277. if (tmp2 == NULL)
  20278. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20279. #endif
  20280. idx = 0;
  20281. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  20282. /* Import DH Private key as DER */
  20283. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  20284. if (ret == 0) {
  20285. /* Export as DER */
  20286. idx = DH_TEST_TMP_SIZE;
  20287. ret = wc_DhPrivKeyToDer(key, tmp2, &idx);
  20288. }
  20289. /* Verify export matches original */
  20290. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  20291. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  20292. }
  20293. /* DH Public Key - Export / Import */
  20294. #if defined(USE_CERT_BUFFERS_2048)
  20295. XMEMCPY(tmp, dh_ffdhe_pub_statickey_der_2048, sizeof_dh_ffdhe_pub_statickey_der_2048);
  20296. bytes = sizeof_dh_ffdhe_pub_statickey_der_2048;
  20297. #else
  20298. file = XFOPEN(dhKeyPubFile, "rb");
  20299. if (!file)
  20300. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20301. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  20302. XFCLOSE(file);
  20303. if (bytes == 0)
  20304. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  20305. #endif
  20306. /* for HAVE_WOLF_BIGINT prevent leak */
  20307. wc_FreeDhKey(key);
  20308. (void)wc_InitDhKey_ex(key, HEAP_HINT, devId);
  20309. idx = 0;
  20310. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  20311. /* Import DH Public key as DER */
  20312. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  20313. if (ret == 0) {
  20314. /* Export as DER */
  20315. idx = DH_TEST_TMP_SIZE;
  20316. ret = wc_DhPubKeyToDer(key, tmp2, &idx);
  20317. }
  20318. /* Verify export matches original */
  20319. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  20320. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  20321. }
  20322. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20323. XFREE(tmp2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20324. #endif
  20325. }
  20326. #else
  20327. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  20328. if (ret != 0)
  20329. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20330. #endif /* !NO_ASN */
  20331. privSz = DH_TEST_BUF_SIZE;
  20332. pubSz = DH_TEST_BUF_SIZE;
  20333. ret = wc_DhExportKeyPair(key, priv, &privSz, pub, &pubSz);
  20334. if (ret != 0)
  20335. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20336. ret = wc_DhImportKeyPair(key2, priv, privSz, pub, pubSz);
  20337. if (ret != 0)
  20338. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20339. #endif /* WOLFSSL_DH_EXTRA && !NO_FILESYSTEM && !FIPS <= 2 */
  20340. #ifndef WC_NO_RNG
  20341. ret = dh_generate_test(&rng);
  20342. if (ret != 0)
  20343. ERROR_OUT(ret, done);
  20344. ret = dh_fips_generate_test(&rng);
  20345. if (ret != 0)
  20346. ERROR_OUT(ret, done);
  20347. #endif /* !WC_NO_RNG */
  20348. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  20349. ret = dh_test_check_pubvalue();
  20350. if (ret != 0)
  20351. ERROR_OUT(ret, done);
  20352. #endif
  20353. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  20354. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  20355. /* RNG with DH and SP_ASM code not supported in the in-lined FIPS ASM code,
  20356. * this will be available for testing in the 140-3 module */
  20357. #ifndef WC_NO_RNG
  20358. /* Specialized code for key gen when using FFDHE-2048, FFDHE-3072 and FFDHE-4096 */
  20359. #ifdef HAVE_FFDHE_2048
  20360. #ifdef HAVE_PUBLIC_FFDHE
  20361. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe2048_Get());
  20362. #else
  20363. ret = dh_ffdhe_test(&rng, WC_FFDHE_2048);
  20364. #endif
  20365. if (ret != 0)
  20366. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20367. #endif
  20368. #ifdef HAVE_FFDHE_3072
  20369. #ifdef HAVE_PUBLIC_FFDHE
  20370. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe3072_Get());
  20371. #else
  20372. ret = dh_ffdhe_test(&rng, WC_FFDHE_3072);
  20373. #endif
  20374. if (ret != 0)
  20375. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20376. #endif
  20377. #ifdef HAVE_FFDHE_4096
  20378. #ifdef HAVE_PUBLIC_FFDHE
  20379. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe4096_Get());
  20380. #else
  20381. ret = dh_ffdhe_test(&rng, WC_FFDHE_4096);
  20382. #endif
  20383. if (ret != 0)
  20384. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20385. #endif
  20386. #endif /* !WC_NO_RNG */
  20387. #endif /* HAVE_FIPS_VERSION == 2 && !WOLFSSL_SP_ARM64_ASM */
  20388. wc_FreeDhKey(key);
  20389. keyInit = 0;
  20390. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  20391. !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(WC_NO_RNG)
  20392. /* Test Check Key */
  20393. ret = wc_DhSetCheckKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g),
  20394. NULL, 0, 0, &rng);
  20395. if (ret != 0)
  20396. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  20397. keyInit = 1; /* DhSetCheckKey also initializes the key, free it */
  20398. #endif
  20399. done:
  20400. #ifndef WC_NO_RNG
  20401. if (rngInit)
  20402. wc_FreeRng(&rng);
  20403. #endif
  20404. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20405. if (key) {
  20406. if (keyInit)
  20407. wc_FreeDhKey(key);
  20408. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20409. }
  20410. if (key2) {
  20411. if (key2Init)
  20412. wc_FreeDhKey(key2);
  20413. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20414. }
  20415. if (tmp)
  20416. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20417. if (priv)
  20418. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20419. if (pub)
  20420. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20421. if (priv2)
  20422. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20423. if (pub2)
  20424. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20425. if (agree)
  20426. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20427. if (agree2)
  20428. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20429. #else
  20430. if (keyInit)
  20431. wc_FreeDhKey(key);
  20432. if (key2Init)
  20433. wc_FreeDhKey(key2);
  20434. #endif
  20435. (void)privSz;
  20436. (void)pubSz;
  20437. (void)pubSz2;
  20438. (void)privSz2;
  20439. return ret;
  20440. #undef DH_TEST_BUF_SIZE
  20441. #undef DH_TEST_TMP_SIZE
  20442. }
  20443. #endif /* NO_DH */
  20444. #ifndef NO_DSA
  20445. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dsa_test(void)
  20446. {
  20447. wc_test_ret_t ret = 0;
  20448. int answer;
  20449. word32 bytes;
  20450. word32 idx = 0;
  20451. WC_RNG rng;
  20452. int rng_inited = 0;
  20453. wc_Sha sha;
  20454. byte hash[WC_SHA_DIGEST_SIZE];
  20455. byte signature[40];
  20456. int key_inited = 0;
  20457. #ifdef WOLFSSL_KEY_GEN
  20458. byte* der = 0;
  20459. int derIn_inited = 0;
  20460. int genKey_inited = 0;
  20461. #endif
  20462. #define DSA_TEST_TMP_SIZE 1024
  20463. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20464. byte *tmp = (byte *)XMALLOC(DSA_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20465. DsaKey *key = (DsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20466. #ifdef WOLFSSL_KEY_GEN
  20467. DsaKey *derIn = (DsaKey *)XMALLOC(sizeof *derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20468. DsaKey *genKey = (DsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20469. #endif
  20470. WOLFSSL_ENTER("dsa_test");
  20471. if ((tmp == NULL) ||
  20472. (key == NULL)
  20473. #ifdef WOLFSSL_KEY_GEN
  20474. || (derIn == NULL)
  20475. || (genKey == NULL)
  20476. #endif
  20477. ) {
  20478. ret = WC_TEST_RET_ENC_NC;
  20479. goto out;
  20480. }
  20481. #else
  20482. byte tmp[1024];
  20483. DsaKey key[1];
  20484. #ifdef WOLFSSL_KEY_GEN
  20485. DsaKey derIn[1];
  20486. DsaKey genKey[1];
  20487. #endif
  20488. #endif
  20489. #ifdef USE_CERT_BUFFERS_1024
  20490. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  20491. bytes = sizeof_dsa_key_der_1024;
  20492. #elif defined(USE_CERT_BUFFERS_2048)
  20493. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  20494. bytes = sizeof_dsa_key_der_2048;
  20495. #else
  20496. {
  20497. XFILE file = XFOPEN(dsaKey, "rb");
  20498. if (!file)
  20499. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  20500. bytes = (word32) XFREAD(tmp, 1, DSA_TEST_TMP_SIZE, file);
  20501. XFCLOSE(file);
  20502. if (bytes == 0)
  20503. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  20504. }
  20505. #endif /* USE_CERT_BUFFERS */
  20506. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  20507. if (ret != 0)
  20508. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20509. wc_ShaUpdate(&sha, tmp, bytes);
  20510. wc_ShaFinal(&sha, hash);
  20511. wc_ShaFree(&sha);
  20512. ret = wc_InitDsaKey(key);
  20513. if (ret != 0)
  20514. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20515. key_inited = 1;
  20516. ret = wc_DsaPrivateKeyDecode(tmp, &idx, key, bytes);
  20517. if (ret != 0)
  20518. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20519. #ifndef HAVE_FIPS
  20520. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  20521. #else
  20522. ret = wc_InitRng(&rng);
  20523. #endif
  20524. if (ret != 0)
  20525. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20526. rng_inited = 1;
  20527. ret = wc_DsaSign(hash, signature, key, &rng);
  20528. if (ret != 0)
  20529. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20530. ret = wc_DsaVerify(hash, signature, key, &answer);
  20531. if (ret != 0)
  20532. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20533. if (answer != 1)
  20534. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  20535. wc_FreeDsaKey(key);
  20536. key_inited = 0;
  20537. ret = wc_InitDsaKey_h(key, NULL);
  20538. if (ret != 0)
  20539. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20540. key_inited = 1;
  20541. #ifdef WOLFSSL_KEY_GEN
  20542. {
  20543. int derSz = 0;
  20544. ret = wc_InitDsaKey(genKey);
  20545. if (ret != 0)
  20546. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20547. genKey_inited = 1;
  20548. ret = wc_MakeDsaParameters(&rng, 1024, genKey);
  20549. if (ret != 0)
  20550. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20551. ret = wc_MakeDsaKey(&rng, genKey);
  20552. if (ret != 0)
  20553. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20554. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20555. if (der == NULL)
  20556. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  20557. derSz = wc_DsaKeyToDer(genKey, der, FOURK_BUF);
  20558. if (derSz < 0)
  20559. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), out);
  20560. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  20561. DSA_PRIVATEKEY_TYPE);
  20562. if (ret != 0)
  20563. goto out;
  20564. ret = wc_InitDsaKey(derIn);
  20565. if (ret != 0)
  20566. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20567. derIn_inited = 1;
  20568. idx = 0;
  20569. ret = wc_DsaPrivateKeyDecode(der, &idx, derIn, (word32)derSz);
  20570. if (ret != 0)
  20571. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  20572. }
  20573. #endif /* WOLFSSL_KEY_GEN */
  20574. out:
  20575. #ifdef WOLFSSL_KEY_GEN
  20576. if (der)
  20577. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20578. #endif
  20579. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20580. if (tmp)
  20581. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20582. if (key) {
  20583. if (key_inited)
  20584. wc_FreeDsaKey(key);
  20585. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20586. }
  20587. #ifdef WOLFSSL_KEY_GEN
  20588. if (derIn) {
  20589. if (derIn_inited)
  20590. wc_FreeDsaKey(derIn);
  20591. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20592. }
  20593. if (genKey) {
  20594. if (genKey_inited)
  20595. wc_FreeDsaKey(genKey);
  20596. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20597. }
  20598. #endif
  20599. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  20600. if (key_inited)
  20601. wc_FreeDsaKey(key);
  20602. #ifdef WOLFSSL_KEY_GEN
  20603. if (derIn_inited)
  20604. wc_FreeDsaKey(derIn);
  20605. if (genKey_inited)
  20606. wc_FreeDsaKey(genKey);
  20607. #endif
  20608. #endif
  20609. if (rng_inited)
  20610. wc_FreeRng(&rng);
  20611. return ret;
  20612. }
  20613. #endif /* NO_DSA */
  20614. #ifdef WOLFCRYPT_HAVE_SRP
  20615. static wc_test_ret_t generate_random_salt(byte *buf, word32 size)
  20616. {
  20617. wc_test_ret_t ret = WC_TEST_RET_ENC_NC;
  20618. WC_RNG rng;
  20619. if(NULL == buf || !size)
  20620. return WC_TEST_RET_ENC_NC;
  20621. if (buf && size && wc_InitRng_ex(&rng, HEAP_HINT, devId) == 0) {
  20622. ret = wc_RNG_GenerateBlock(&rng, (byte *)buf, size);
  20623. wc_FreeRng(&rng);
  20624. }
  20625. return ret;
  20626. }
  20627. static wc_test_ret_t srp_test_digest(SrpType dgstType)
  20628. {
  20629. wc_test_ret_t r;
  20630. byte clientPubKey[192]; /* A */
  20631. byte serverPubKey[192]; /* B */
  20632. word32 clientPubKeySz = 192;
  20633. word32 serverPubKeySz = 192;
  20634. byte username[] = "user";
  20635. word32 usernameSz = 4;
  20636. byte password[] = "password";
  20637. word32 passwordSz = 8;
  20638. WOLFSSL_SMALL_STACK_STATIC const byte N[] = {
  20639. 0xfc, 0x58, 0x7a, 0x8a, 0x70, 0xfb, 0x5a, 0x9a,
  20640. 0x5d, 0x39, 0x48, 0xbf, 0x1c, 0x46, 0xd8, 0x3b,
  20641. 0x7a, 0xe9, 0x1f, 0x85, 0x36, 0x18, 0xc4, 0x35,
  20642. 0x3f, 0xf8, 0x8a, 0x8f, 0x8c, 0x10, 0x2e, 0x01,
  20643. 0x58, 0x1d, 0x41, 0xcb, 0xc4, 0x47, 0xa8, 0xaf,
  20644. 0x9a, 0x6f, 0x58, 0x14, 0xa4, 0x68, 0xf0, 0x9c,
  20645. 0xa6, 0xe7, 0xbf, 0x0d, 0xe9, 0x62, 0x0b, 0xd7,
  20646. 0x26, 0x46, 0x5b, 0x27, 0xcb, 0x4c, 0xf9, 0x7e,
  20647. 0x1e, 0x8b, 0xe6, 0xdd, 0x29, 0xb7, 0xb7, 0x15,
  20648. 0x2e, 0xcf, 0x23, 0xa6, 0x4b, 0x97, 0x9f, 0x89,
  20649. 0xd4, 0x86, 0xc4, 0x90, 0x63, 0x92, 0xf4, 0x30,
  20650. 0x26, 0x69, 0x48, 0x9d, 0x7a, 0x4f, 0xad, 0xb5,
  20651. 0x6a, 0x51, 0xad, 0xeb, 0xf9, 0x90, 0x31, 0x77,
  20652. 0x53, 0x30, 0x2a, 0x85, 0xf7, 0x11, 0x21, 0x0c,
  20653. 0xb8, 0x4b, 0x56, 0x03, 0x5e, 0xbb, 0x25, 0x33,
  20654. 0x7c, 0xd9, 0x5a, 0xd1, 0x5c, 0xb2, 0xd4, 0x53,
  20655. 0xc5, 0x16, 0x68, 0xf0, 0xdf, 0x48, 0x55, 0x3e,
  20656. 0xd4, 0x59, 0x87, 0x64, 0x59, 0xaa, 0x39, 0x01,
  20657. 0x45, 0x89, 0x9c, 0x72, 0xff, 0xdd, 0x8f, 0x6d,
  20658. 0xa0, 0x42, 0xbc, 0x6f, 0x6e, 0x62, 0x18, 0x2d,
  20659. 0x50, 0xe8, 0x18, 0x97, 0x87, 0xfc, 0xef, 0x1f,
  20660. 0xf5, 0x53, 0x68, 0xe8, 0x49, 0xd1, 0xa2, 0xe8,
  20661. 0xb9, 0x26, 0x03, 0xba, 0xb5, 0x58, 0x6f, 0x6c,
  20662. 0x8b, 0x08, 0xa1, 0x7b, 0x6f, 0x42, 0xc9, 0x53
  20663. };
  20664. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  20665. 0x02
  20666. };
  20667. byte salt[10];
  20668. byte verifier[192];
  20669. word32 v_size = sizeof(verifier);
  20670. word32 clientProofSz = SRP_MAX_DIGEST_SIZE;
  20671. word32 serverProofSz = SRP_MAX_DIGEST_SIZE;
  20672. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20673. Srp *cli = (Srp *)XMALLOC(sizeof *cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20674. Srp *srv = (Srp *)XMALLOC(sizeof *srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20675. byte *clientProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  20676. DYNAMIC_TYPE_TMP_BUFFER); /* M1 */
  20677. byte *serverProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  20678. DYNAMIC_TYPE_TMP_BUFFER); /* M2 */
  20679. if ((cli == NULL) ||
  20680. (srv == NULL) ||
  20681. (clientProof == NULL) ||
  20682. (serverProof == NULL)) {
  20683. r = WC_TEST_RET_ENC_NC;
  20684. goto out;
  20685. }
  20686. #else
  20687. Srp cli[1], srv[1];
  20688. byte clientProof[SRP_MAX_DIGEST_SIZE]; /* M1 */
  20689. byte serverProof[SRP_MAX_DIGEST_SIZE]; /* M2 */
  20690. #endif
  20691. /* set as 0's so if second init on srv not called SrpTerm is not on
  20692. * garbage values */
  20693. XMEMSET(srv, 0, sizeof *srv);
  20694. XMEMSET(cli, 0, sizeof *cli);
  20695. /* generating random salt */
  20696. r = generate_random_salt(salt, sizeof(salt));
  20697. /* client knows username and password. */
  20698. /* server knows N, g, salt and verifier. */
  20699. if (!r) r = wc_SrpInit_ex(cli, dgstType, SRP_CLIENT_SIDE, HEAP_HINT, devId);
  20700. if (!r) r = wc_SrpSetUsername(cli, username, usernameSz);
  20701. /* loading N, g and salt in advance to generate the verifier. */
  20702. if (!r) r = wc_SrpSetParams(cli, N, sizeof(N),
  20703. g, sizeof(g),
  20704. salt, sizeof(salt));
  20705. if (!r) r = wc_SrpSetPassword(cli, password, passwordSz);
  20706. if (!r) r = wc_SrpGetVerifier(cli, verifier, &v_size);
  20707. /* client sends username to server */
  20708. if (!r) r = wc_SrpInit_ex(srv, dgstType, SRP_SERVER_SIDE, HEAP_HINT, devId);
  20709. if (!r) r = wc_SrpSetUsername(srv, username, usernameSz);
  20710. if (!r) r = wc_SrpSetParams(srv, N, sizeof(N),
  20711. g, sizeof(g),
  20712. salt, sizeof(salt));
  20713. if (!r) r = wc_SrpSetVerifier(srv, verifier, v_size);
  20714. if (!r) r = wc_SrpGetPublic(srv, serverPubKey, &serverPubKeySz);
  20715. /* server sends N, g, salt and B to client */
  20716. if (!r) r = wc_SrpGetPublic(cli, clientPubKey, &clientPubKeySz);
  20717. if (!r) r = wc_SrpComputeKey(cli, clientPubKey, clientPubKeySz,
  20718. serverPubKey, serverPubKeySz);
  20719. if (!r) r = wc_SrpGetProof(cli, clientProof, &clientProofSz);
  20720. /* client sends A and M1 to server */
  20721. if (!r) r = wc_SrpComputeKey(srv, clientPubKey, clientPubKeySz,
  20722. serverPubKey, serverPubKeySz);
  20723. if (!r) r = wc_SrpVerifyPeersProof(srv, clientProof, clientProofSz);
  20724. if (!r) r = wc_SrpGetProof(srv, serverProof, &serverProofSz);
  20725. /* server sends M2 to client */
  20726. if (!r) r = wc_SrpVerifyPeersProof(cli, serverProof, serverProofSz);
  20727. wc_SrpTerm(cli);
  20728. wc_SrpTerm(srv);
  20729. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20730. out:
  20731. if (cli)
  20732. XFREE(cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20733. if (srv)
  20734. XFREE(srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20735. if (clientProof)
  20736. XFREE(clientProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20737. if (serverProof)
  20738. XFREE(serverProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20739. #endif
  20740. return r;
  20741. }
  20742. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srp_test(void)
  20743. {
  20744. wc_test_ret_t ret;
  20745. WOLFSSL_ENTER("srp_test");
  20746. #ifndef NO_SHA
  20747. ret = srp_test_digest(SRP_TYPE_SHA);
  20748. if (ret != 0)
  20749. return ret;
  20750. #endif
  20751. #ifndef NO_SHA256
  20752. ret = srp_test_digest(SRP_TYPE_SHA256);
  20753. if (ret != 0)
  20754. return ret;
  20755. #endif
  20756. #ifdef WOLFSSL_SHA384
  20757. ret = srp_test_digest(SRP_TYPE_SHA384);
  20758. if (ret != 0)
  20759. return ret;
  20760. #endif
  20761. #ifdef WOLFSSL_SHA512
  20762. ret = srp_test_digest(SRP_TYPE_SHA512);
  20763. if (ret != 0)
  20764. return ret;
  20765. #endif
  20766. return ret;
  20767. }
  20768. #endif /* WOLFCRYPT_HAVE_SRP */
  20769. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  20770. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  20771. static wc_test_ret_t openssl_aes_test(void)
  20772. {
  20773. #ifdef HAVE_AES_CBC
  20774. #ifdef WOLFSSL_AES_128
  20775. {
  20776. /* EVP_CipherUpdate test */
  20777. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  20778. {
  20779. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  20780. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  20781. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  20782. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  20783. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  20784. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  20785. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  20786. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  20787. };
  20788. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  20789. "0123456789abcdef "; /* align */
  20790. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  20791. "1234567890abcdef "; /* align */
  20792. byte cipher[AES_BLOCK_SIZE * 4];
  20793. byte plain [AES_BLOCK_SIZE * 4];
  20794. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20795. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  20796. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  20797. #else
  20798. EVP_CIPHER_CTX en[1];
  20799. EVP_CIPHER_CTX de[1];
  20800. #endif
  20801. int outlen ;
  20802. int total = 0;
  20803. int i;
  20804. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20805. if ((en == NULL) || (de == NULL))
  20806. return MEMORY_E;
  20807. #endif
  20808. EVP_CIPHER_CTX_init(en);
  20809. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  20810. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  20811. return WC_TEST_RET_ENC_NC;
  20812. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  20813. (byte*)cbcPlain, 9) == 0)
  20814. return WC_TEST_RET_ENC_NC;
  20815. if (outlen != 0)
  20816. return WC_TEST_RET_ENC_NC;
  20817. total += outlen;
  20818. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  20819. (byte*)&cbcPlain[9] , 9) == 0)
  20820. return WC_TEST_RET_ENC_NC;
  20821. if (outlen != 16)
  20822. return WC_TEST_RET_ENC_NC;
  20823. total += outlen;
  20824. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  20825. return WC_TEST_RET_ENC_NC;
  20826. if (outlen != 16)
  20827. return WC_TEST_RET_ENC_NC;
  20828. total += outlen;
  20829. if (total != 32)
  20830. return 3408;
  20831. EVP_CIPHER_CTX_cleanup(en);
  20832. total = 0;
  20833. EVP_CIPHER_CTX_init(de);
  20834. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  20835. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  20836. return WC_TEST_RET_ENC_NC;
  20837. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  20838. return WC_TEST_RET_ENC_NC;
  20839. if (outlen != 0)
  20840. return WC_TEST_RET_ENC_NC;
  20841. total += outlen;
  20842. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  20843. (byte*)&cipher[6], 12) == 0)
  20844. return WC_TEST_RET_ENC_NC;
  20845. if (outlen != 0)
  20846. total += outlen;
  20847. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  20848. (byte*)&cipher[6+12], 14) == 0)
  20849. return WC_TEST_RET_ENC_NC;
  20850. if (outlen != 16)
  20851. return WC_TEST_RET_ENC_NC;
  20852. total += outlen;
  20853. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  20854. return WC_TEST_RET_ENC_NC;
  20855. if (outlen != 2)
  20856. return WC_TEST_RET_ENC_NC;
  20857. total += outlen;
  20858. if (total != 18)
  20859. return 3427;
  20860. if (XMEMCMP(plain, cbcPlain, 18))
  20861. return WC_TEST_RET_ENC_NC;
  20862. EVP_CIPHER_CTX_cleanup(de);
  20863. /* test with encrypting/decrypting more than 16 bytes at once */
  20864. total = 0;
  20865. EVP_CIPHER_CTX_init(en);
  20866. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  20867. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  20868. return WC_TEST_RET_ENC_NC;
  20869. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  20870. (byte*)cbcPlain, 17) == 0)
  20871. return WC_TEST_RET_ENC_NC;
  20872. if (outlen != 16)
  20873. return WC_TEST_RET_ENC_NC;
  20874. total += outlen;
  20875. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  20876. (byte*)&cbcPlain[17] , 1) == 0)
  20877. return WC_TEST_RET_ENC_NC;
  20878. if (outlen != 0)
  20879. return WC_TEST_RET_ENC_NC;
  20880. total += outlen;
  20881. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  20882. return WC_TEST_RET_ENC_NC;
  20883. if (outlen != 16)
  20884. return WC_TEST_RET_ENC_NC;
  20885. total += outlen;
  20886. if (total != 32)
  20887. return WC_TEST_RET_ENC_NC;
  20888. EVP_CIPHER_CTX_cleanup(en);
  20889. total = 0;
  20890. EVP_CIPHER_CTX_init(de);
  20891. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  20892. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  20893. return WC_TEST_RET_ENC_NC;
  20894. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 17) == 0)
  20895. return WC_TEST_RET_ENC_NC;
  20896. if (outlen != 16)
  20897. return WC_TEST_RET_ENC_NC;
  20898. total += outlen;
  20899. /* final call on non block size should fail */
  20900. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  20901. return WC_TEST_RET_ENC_NC;
  20902. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  20903. (byte*)&cipher[17], 1) == 0)
  20904. return WC_TEST_RET_ENC_NC;
  20905. if (outlen != 0)
  20906. total += outlen;
  20907. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  20908. (byte*)&cipher[17+1], 14) == 0)
  20909. return WC_TEST_RET_ENC_NC;
  20910. if (outlen != 0)
  20911. return WC_TEST_RET_ENC_NC;
  20912. total += outlen;
  20913. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  20914. return WC_TEST_RET_ENC_NC;
  20915. if (outlen != 2)
  20916. return WC_TEST_RET_ENC_NC;
  20917. total += outlen;
  20918. if (total != 18)
  20919. return WC_TEST_RET_ENC_NC;
  20920. if (XMEMCMP(plain, cbcPlain, 18))
  20921. return WC_TEST_RET_ENC_NC;
  20922. /* test byte by byte decrypt */
  20923. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  20924. plain[i] = i;
  20925. }
  20926. EVP_CIPHER_CTX_cleanup(de);
  20927. total = 0;
  20928. EVP_CIPHER_CTX_init(en);
  20929. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  20930. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  20931. return WC_TEST_RET_ENC_NC;
  20932. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  20933. (byte*)plain, AES_BLOCK_SIZE * 3) == 0)
  20934. return WC_TEST_RET_ENC_NC;
  20935. if (outlen != AES_BLOCK_SIZE * 3)
  20936. return WC_TEST_RET_ENC_NC;
  20937. total += outlen;
  20938. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  20939. return WC_TEST_RET_ENC_NC;
  20940. if (outlen != AES_BLOCK_SIZE)
  20941. return WC_TEST_RET_ENC_NC;
  20942. total += outlen;
  20943. if (total != sizeof(plain))
  20944. return WC_TEST_RET_ENC_NC;
  20945. EVP_CIPHER_CTX_cleanup(en);
  20946. total = 0;
  20947. EVP_CIPHER_CTX_init(de);
  20948. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  20949. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  20950. return WC_TEST_RET_ENC_NC;
  20951. for (i = 0; i < AES_BLOCK_SIZE * 4; i++) {
  20952. if (EVP_CipherUpdate(de, (byte*)plain + total, &outlen,
  20953. (byte*)cipher + i, 1) == 0)
  20954. return WC_TEST_RET_ENC_NC;
  20955. if (outlen > 0) {
  20956. int j;
  20957. total += outlen;
  20958. for (j = 0; j < total; j++) {
  20959. if (plain[j] != j) {
  20960. return WC_TEST_RET_ENC_NC;
  20961. }
  20962. }
  20963. }
  20964. }
  20965. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  20966. return WC_TEST_RET_ENC_NC;
  20967. total += outlen;
  20968. if (total != AES_BLOCK_SIZE * 3) {
  20969. return WC_TEST_RET_ENC_NC;
  20970. }
  20971. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  20972. if (plain[i] != i) {
  20973. return WC_TEST_RET_ENC_NC;
  20974. }
  20975. }
  20976. EVP_CIPHER_CTX_cleanup(de);
  20977. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20978. wolfSSL_EVP_CIPHER_CTX_free(en);
  20979. wolfSSL_EVP_CIPHER_CTX_free(de);
  20980. #endif
  20981. }
  20982. /* set buffers to be exact size to catch potential over read/write */
  20983. {
  20984. /* EVP_CipherUpdate test */
  20985. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  20986. {
  20987. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  20988. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  20989. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  20990. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  20991. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  20992. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  20993. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  20994. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  20995. };
  20996. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  20997. "0123456789abcdef "; /* align */
  20998. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  20999. "1234567890abcdef "; /* align */
  21000. #define EVP_TEST_BUF_SZ 18
  21001. #define EVP_TEST_BUF_PAD 32
  21002. byte cipher[EVP_TEST_BUF_SZ];
  21003. byte plain [EVP_TEST_BUF_SZ];
  21004. byte padded[EVP_TEST_BUF_PAD];
  21005. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21006. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  21007. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  21008. #else
  21009. EVP_CIPHER_CTX en[1];
  21010. EVP_CIPHER_CTX de[1];
  21011. #endif
  21012. int outlen ;
  21013. int total = 0;
  21014. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21015. if ((en == NULL) || (de == NULL))
  21016. return MEMORY_E;
  21017. #endif
  21018. EVP_CIPHER_CTX_init(en);
  21019. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  21020. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  21021. return WC_TEST_RET_ENC_NC;
  21022. if (EVP_CIPHER_CTX_set_padding(en, 0) != 1)
  21023. return WC_TEST_RET_ENC_NC;
  21024. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  21025. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  21026. return WC_TEST_RET_ENC_NC;
  21027. if (outlen != 16)
  21028. return WC_TEST_RET_ENC_NC;
  21029. total += outlen;
  21030. /* should fail here */
  21031. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) != 0)
  21032. return WC_TEST_RET_ENC_NC;
  21033. EVP_CIPHER_CTX_cleanup(en);
  21034. /* turn padding back on and do successful encrypt */
  21035. total = 0;
  21036. EVP_CIPHER_CTX_init(en);
  21037. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  21038. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  21039. return WC_TEST_RET_ENC_NC;
  21040. if (EVP_CIPHER_CTX_set_padding(en, 1) != 1)
  21041. return WC_TEST_RET_ENC_NC;
  21042. if (EVP_CipherUpdate(en, (byte*)padded, &outlen,
  21043. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  21044. return WC_TEST_RET_ENC_NC;
  21045. if (outlen != 16)
  21046. return WC_TEST_RET_ENC_NC;
  21047. total += outlen;
  21048. if (EVP_CipherFinal(en, (byte*)&padded[total], &outlen) == 0)
  21049. return WC_TEST_RET_ENC_NC;
  21050. total += outlen;
  21051. if (total != 32)
  21052. return WC_TEST_RET_ENC_NC;
  21053. XMEMCPY(cipher, padded, EVP_TEST_BUF_SZ);
  21054. EVP_CIPHER_CTX_cleanup(en);
  21055. /* test out of bounds read on buffers w/o padding during decryption */
  21056. total = 0;
  21057. EVP_CIPHER_CTX_init(de);
  21058. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  21059. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  21060. return WC_TEST_RET_ENC_NC;
  21061. if (EVP_CIPHER_CTX_set_padding(de, 0) != 1)
  21062. return WC_TEST_RET_ENC_NC;
  21063. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher,
  21064. EVP_TEST_BUF_SZ) == 0)
  21065. return WC_TEST_RET_ENC_NC;
  21066. if (outlen != 16)
  21067. return WC_TEST_RET_ENC_NC;
  21068. total += outlen;
  21069. /* should fail since not using padding */
  21070. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  21071. return WC_TEST_RET_ENC_NC;
  21072. EVP_CIPHER_CTX_cleanup(de);
  21073. total = 0;
  21074. EVP_CIPHER_CTX_init(de);
  21075. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  21076. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  21077. return WC_TEST_RET_ENC_NC;
  21078. if (EVP_CIPHER_CTX_set_padding(de, 1) != 1)
  21079. return WC_TEST_RET_ENC_NC;
  21080. if (EVP_CipherUpdate(de, (byte*)padded, &outlen, (byte*)padded,
  21081. EVP_TEST_BUF_PAD) == 0)
  21082. return WC_TEST_RET_ENC_NC;
  21083. if (outlen != 16)
  21084. return WC_TEST_RET_ENC_NC;
  21085. total += outlen;
  21086. if (EVP_CipherFinal(de, (byte*)&padded[total], &outlen) == 0)
  21087. return WC_TEST_RET_ENC_NC;
  21088. if (XMEMCMP(padded, cbcPlain, EVP_TEST_BUF_SZ))
  21089. return WC_TEST_RET_ENC_NC;
  21090. EVP_CIPHER_CTX_cleanup(de);
  21091. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21092. wolfSSL_EVP_CIPHER_CTX_free(en);
  21093. wolfSSL_EVP_CIPHER_CTX_free(de);
  21094. #endif
  21095. }
  21096. { /* evp_cipher test: EVP_aes_128_cbc */
  21097. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21098. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  21099. #else
  21100. EVP_CIPHER_CTX ctx[1];
  21101. #endif
  21102. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  21103. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  21104. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  21105. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  21106. };
  21107. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  21108. {
  21109. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  21110. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  21111. };
  21112. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  21113. "0123456789abcdef "; /* align */
  21114. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  21115. "1234567890abcdef "; /* align */
  21116. byte cipher[AES_BLOCK_SIZE * 4];
  21117. byte plain [AES_BLOCK_SIZE * 4];
  21118. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21119. if (ctx == NULL)
  21120. return MEMORY_E;
  21121. #endif
  21122. EVP_CIPHER_CTX_init(ctx);
  21123. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  21124. return WC_TEST_RET_ENC_NC;
  21125. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  21126. return WC_TEST_RET_ENC_NC;
  21127. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  21128. return WC_TEST_RET_ENC_NC;
  21129. EVP_CIPHER_CTX_cleanup(ctx);
  21130. EVP_CIPHER_CTX_init(ctx);
  21131. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  21132. return WC_TEST_RET_ENC_NC;
  21133. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  21134. return WC_TEST_RET_ENC_NC;
  21135. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  21136. return WC_TEST_RET_ENC_NC;
  21137. EVP_CIPHER_CTX_cleanup(ctx);
  21138. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21139. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  21140. #endif
  21141. } /* end evp_cipher test: EVP_aes_128_cbc*/
  21142. #endif /* WOLFSSL_AES_128 */
  21143. #endif /* HAVE_AES_CBC */
  21144. #ifndef WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API
  21145. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  21146. { /* evp_cipher test: EVP_aes_256_ecb*/
  21147. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21148. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  21149. #else
  21150. EVP_CIPHER_CTX ctx[1];
  21151. #endif
  21152. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  21153. {
  21154. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21155. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  21156. };
  21157. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  21158. {
  21159. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  21160. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  21161. };
  21162. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  21163. {
  21164. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  21165. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  21166. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  21167. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  21168. };
  21169. byte cipher[AES_BLOCK_SIZE * 4];
  21170. byte plain [AES_BLOCK_SIZE * 4];
  21171. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21172. if (ctx == NULL)
  21173. return MEMORY_E;
  21174. #endif
  21175. EVP_CIPHER_CTX_init(ctx);
  21176. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1) == 0)
  21177. return WC_TEST_RET_ENC_NC;
  21178. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  21179. return WC_TEST_RET_ENC_NC;
  21180. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  21181. return WC_TEST_RET_ENC_NC;
  21182. EVP_CIPHER_CTX_init(ctx);
  21183. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0) == 0)
  21184. return WC_TEST_RET_ENC_NC;
  21185. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  21186. return WC_TEST_RET_ENC_NC;
  21187. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  21188. return WC_TEST_RET_ENC_NC;
  21189. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21190. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  21191. #endif
  21192. } /* end evp_cipher test */
  21193. #endif /* HAVE_AES_ECB && WOLFSSL_AES_256 */
  21194. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  21195. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  21196. {
  21197. /* Test: AES_encrypt/decrypt/set Key */
  21198. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21199. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  21200. #ifdef HAVE_AES_DECRYPT
  21201. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  21202. #endif
  21203. #else
  21204. AES_KEY enc[1];
  21205. #ifdef HAVE_AES_DECRYPT
  21206. AES_KEY dec[1];
  21207. #endif
  21208. #endif
  21209. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  21210. {
  21211. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21212. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  21213. };
  21214. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  21215. {
  21216. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  21217. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  21218. };
  21219. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  21220. {
  21221. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  21222. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  21223. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  21224. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  21225. };
  21226. byte plain[sizeof(msg)];
  21227. byte cipher[sizeof(msg)];
  21228. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21229. if (enc == NULL)
  21230. return MEMORY_E;
  21231. #ifdef HAVE_AES_DECRYPT
  21232. if (dec == NULL)
  21233. return MEMORY_E;
  21234. #endif
  21235. #endif
  21236. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  21237. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  21238. AES_encrypt(msg, cipher, enc);
  21239. #ifdef HAVE_AES_DECRYPT
  21240. AES_decrypt(cipher, plain, dec);
  21241. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  21242. return WC_TEST_RET_ENC_NC;
  21243. #endif /* HAVE_AES_DECRYPT */
  21244. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  21245. return WC_TEST_RET_ENC_NC;
  21246. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21247. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  21248. #ifdef HAVE_AES_DECRYPT
  21249. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  21250. #endif
  21251. #endif
  21252. }
  21253. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  21254. #endif /* !WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API */
  21255. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  21256. #ifdef WOLFSSL_AES_COUNTER
  21257. {
  21258. byte plainBuff [64];
  21259. byte cipherBuff[64];
  21260. #ifdef WOLFSSL_AES_128
  21261. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  21262. {
  21263. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  21264. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  21265. };
  21266. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  21267. {
  21268. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  21269. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  21270. };
  21271. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  21272. {
  21273. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21274. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  21275. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  21276. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  21277. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  21278. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  21279. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  21280. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  21281. };
  21282. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  21283. {
  21284. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  21285. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  21286. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  21287. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  21288. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  21289. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  21290. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  21291. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  21292. };
  21293. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  21294. {
  21295. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  21296. 0xc2
  21297. };
  21298. #endif
  21299. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  21300. * NIST Special Publication 800-38A */
  21301. #ifdef WOLFSSL_AES_192
  21302. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  21303. {
  21304. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  21305. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  21306. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  21307. };
  21308. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  21309. {
  21310. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  21311. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  21312. };
  21313. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  21314. {
  21315. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21316. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  21317. };
  21318. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  21319. {
  21320. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  21321. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  21322. };
  21323. #endif /* WOLFSSL_AES_192 */
  21324. #ifdef WOLFSSL_AES_256
  21325. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  21326. * NIST Special Publication 800-38A */
  21327. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  21328. {
  21329. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  21330. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  21331. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  21332. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  21333. };
  21334. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  21335. {
  21336. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  21337. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  21338. };
  21339. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  21340. {
  21341. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21342. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  21343. };
  21344. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  21345. {
  21346. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  21347. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  21348. };
  21349. #endif /* WOLFSSL_AES_256 */
  21350. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21351. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  21352. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  21353. #else
  21354. EVP_CIPHER_CTX en[1];
  21355. EVP_CIPHER_CTX de[1];
  21356. #endif
  21357. #ifdef WOLFSSL_AES_128
  21358. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21359. EVP_CIPHER_CTX *p_en;
  21360. EVP_CIPHER_CTX *p_de;
  21361. #endif
  21362. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21363. if ((en == NULL) || (de == NULL))
  21364. return MEMORY_E;
  21365. #endif
  21366. EVP_CIPHER_CTX_init(en);
  21367. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  21368. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  21369. return WC_TEST_RET_ENC_NC;
  21370. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  21371. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  21372. return WC_TEST_RET_ENC_NC;
  21373. EVP_CIPHER_CTX_init(de);
  21374. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  21375. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  21376. return WC_TEST_RET_ENC_NC;
  21377. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  21378. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  21379. return WC_TEST_RET_ENC_NC;
  21380. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  21381. return WC_TEST_RET_ENC_NC;
  21382. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  21383. return WC_TEST_RET_ENC_NC;
  21384. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  21385. return WC_TEST_RET_ENC_NC;
  21386. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  21387. return WC_TEST_RET_ENC_NC;
  21388. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21389. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  21390. if (p_en == NULL)
  21391. return WC_TEST_RET_ENC_ERRNO;
  21392. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  21393. if (p_de == NULL)
  21394. return WC_TEST_RET_ENC_ERRNO;
  21395. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  21396. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  21397. return WC_TEST_RET_ENC_NC;
  21398. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  21399. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  21400. return WC_TEST_RET_ENC_NC;
  21401. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  21402. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  21403. return WC_TEST_RET_ENC_NC;
  21404. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  21405. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  21406. return WC_TEST_RET_ENC_NC;
  21407. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  21408. return WC_TEST_RET_ENC_NC;
  21409. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  21410. return WC_TEST_RET_ENC_NC;
  21411. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  21412. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  21413. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  21414. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  21415. return WC_TEST_RET_ENC_NC;
  21416. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  21417. return WC_TEST_RET_ENC_NC;
  21418. EVP_CIPHER_CTX_init(en);
  21419. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  21420. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  21421. return WC_TEST_RET_ENC_NC;
  21422. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  21423. return WC_TEST_RET_ENC_NC;
  21424. EVP_CIPHER_CTX_init(de);
  21425. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  21426. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  21427. return WC_TEST_RET_ENC_NC;
  21428. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  21429. return WC_TEST_RET_ENC_NC;
  21430. if (XMEMCMP(plainBuff, ctrPlain, 9))
  21431. return WC_TEST_RET_ENC_NC;
  21432. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  21433. return WC_TEST_RET_ENC_NC;
  21434. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  21435. return WC_TEST_RET_ENC_NC;
  21436. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  21437. return WC_TEST_RET_ENC_NC;
  21438. if (XMEMCMP(plainBuff, ctrPlain, 9))
  21439. return WC_TEST_RET_ENC_NC;
  21440. if (XMEMCMP(cipherBuff, oddCipher, 9))
  21441. return WC_TEST_RET_ENC_NC;
  21442. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  21443. return WC_TEST_RET_ENC_NC;
  21444. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  21445. return WC_TEST_RET_ENC_NC;
  21446. #endif /* WOLFSSL_AES_128 */
  21447. #ifdef WOLFSSL_AES_192
  21448. EVP_CIPHER_CTX_init(en);
  21449. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  21450. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  21451. return WC_TEST_RET_ENC_NC;
  21452. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  21453. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  21454. return WC_TEST_RET_ENC_NC;
  21455. EVP_CIPHER_CTX_init(de);
  21456. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  21457. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  21458. return WC_TEST_RET_ENC_NC;
  21459. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  21460. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  21461. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  21462. return WC_TEST_RET_ENC_NC;
  21463. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  21464. return WC_TEST_RET_ENC_NC;
  21465. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  21466. return WC_TEST_RET_ENC_NC;
  21467. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  21468. return WC_TEST_RET_ENC_NC;
  21469. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  21470. return WC_TEST_RET_ENC_NC;
  21471. #endif /* WOLFSSL_AES_192 */
  21472. #ifdef WOLFSSL_AES_256
  21473. EVP_CIPHER_CTX_init(en);
  21474. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  21475. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  21476. return WC_TEST_RET_ENC_NC;
  21477. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  21478. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  21479. return WC_TEST_RET_ENC_NC;
  21480. EVP_CIPHER_CTX_init(de);
  21481. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  21482. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  21483. return WC_TEST_RET_ENC_NC;
  21484. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  21485. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  21486. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  21487. return WC_TEST_RET_ENC_NC;
  21488. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  21489. return WC_TEST_RET_ENC_NC;
  21490. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  21491. return WC_TEST_RET_ENC_NC;
  21492. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  21493. return WC_TEST_RET_ENC_NC;
  21494. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  21495. return WC_TEST_RET_ENC_NC;
  21496. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21497. wolfSSL_EVP_CIPHER_CTX_free(en);
  21498. wolfSSL_EVP_CIPHER_CTX_free(de);
  21499. #endif
  21500. #endif /* WOLFSSL_AES_256 */
  21501. }
  21502. #endif /* HAVE_AES_COUNTER */
  21503. #ifndef WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API
  21504. #if defined(WOLFSSL_AES_CFB) && defined(WOLFSSL_AES_128)
  21505. {
  21506. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21507. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  21508. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  21509. #else
  21510. AES_KEY enc[1];
  21511. AES_KEY dec[1];
  21512. #endif
  21513. WOLFSSL_SMALL_STACK_STATIC const byte setIv[] = {
  21514. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  21515. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  21516. };
  21517. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  21518. {
  21519. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  21520. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  21521. };
  21522. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  21523. {
  21524. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  21525. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  21526. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  21527. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b
  21528. };
  21529. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  21530. {
  21531. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  21532. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  21533. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  21534. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51
  21535. };
  21536. byte cipher[AES_BLOCK_SIZE * 2];
  21537. byte iv[AES_BLOCK_SIZE]; /* iv buffer is updeated by API */
  21538. int num = 0;
  21539. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21540. if ((enc == NULL) || (dec == NULL))
  21541. return MEMORY_E;
  21542. #endif
  21543. XMEMCPY(iv, setIv, sizeof(setIv));
  21544. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, enc);
  21545. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, dec);
  21546. wolfSSL_AES_cfb128_encrypt(msg, cipher, AES_BLOCK_SIZE - 1, enc, iv,
  21547. &num, AES_ENCRYPT);
  21548. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE - 1))
  21549. return WC_TEST_RET_ENC_NC;
  21550. if (num != 15) /* should have used 15 of the 16 bytes */
  21551. return WC_TEST_RET_ENC_NC;
  21552. wolfSSL_AES_cfb128_encrypt(msg + AES_BLOCK_SIZE - 1,
  21553. cipher + AES_BLOCK_SIZE - 1, AES_BLOCK_SIZE + 1, enc, iv,
  21554. &num, AES_ENCRYPT);
  21555. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  21556. return WC_TEST_RET_ENC_NC;
  21557. if (num != 0)
  21558. return WC_TEST_RET_ENC_NC;
  21559. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21560. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  21561. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  21562. #endif
  21563. }
  21564. #endif /* WOLFSSL_AES_CFB && WOLFSSL_AES_128 */
  21565. #endif /* !WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API */
  21566. return 0;
  21567. }
  21568. #endif /* !defined(NO_AES) && !defined(WOLFCRYPT_ONLY) */
  21569. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_test(void)
  21570. {
  21571. wc_test_ret_t ret;
  21572. #ifdef WOLFSSL_SMALL_STACK
  21573. EVP_MD_CTX *md_ctx = (EVP_MD_CTX *)XMALLOC(sizeof(EVP_MD_CTX), NULL, DYNAMIC_TYPE_OPENSSL);
  21574. #else
  21575. EVP_MD_CTX md_ctx[1];
  21576. #endif
  21577. testVector a, b, c, d, e, f;
  21578. byte hash[WC_SHA256_DIGEST_SIZE*2]; /* max size */
  21579. #ifdef WOLFSSL_SMALL_STACK
  21580. if (md_ctx == NULL)
  21581. return WC_TEST_RET_ENC_EC(MEMORY_E);
  21582. #endif
  21583. WOLFSSL_ENTER("openssl_test");
  21584. a.inLen = 0;
  21585. b.inLen = c.inLen = d.inLen = e.inLen = f.inLen = a.inLen;
  21586. (void)a;
  21587. (void)b;
  21588. (void)c;
  21589. (void)d;
  21590. (void)e;
  21591. (void)f;
  21592. /* test malloc / free , 10 is an arbitrary amount of memory chosen */
  21593. {
  21594. byte* p;
  21595. p = (byte*)CRYPTO_malloc(10, "", 0);
  21596. if (p == NULL) {
  21597. return WC_TEST_RET_ENC_NC;
  21598. }
  21599. XMEMSET(p, 0, 10);
  21600. CRYPTO_free(p, "", 0);
  21601. }
  21602. #ifndef NO_MD5
  21603. a.input = "1234567890123456789012345678901234567890123456789012345678"
  21604. "9012345678901234567890";
  21605. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  21606. "\x7a";
  21607. a.inLen = XSTRLEN(a.input);
  21608. a.outLen = WC_MD5_DIGEST_SIZE;
  21609. EVP_MD_CTX_init(md_ctx);
  21610. ret = EVP_DigestInit(md_ctx, EVP_md5());
  21611. if (ret == WOLFSSL_SUCCESS) {
  21612. ret = EVP_DigestUpdate(md_ctx, a.input, (unsigned long)a.inLen);
  21613. }
  21614. if (ret == WOLFSSL_SUCCESS) {
  21615. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21616. }
  21617. EVP_MD_CTX_cleanup(md_ctx);
  21618. if (ret != WOLFSSL_SUCCESS)
  21619. return WC_TEST_RET_ENC_NC;
  21620. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0)
  21621. return WC_TEST_RET_ENC_NC;
  21622. #endif /* NO_MD5 */
  21623. #ifndef NO_SHA
  21624. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  21625. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  21626. "aaaaaaaaaa";
  21627. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  21628. "\x53\x99\x5E\x26\xA0";
  21629. b.inLen = XSTRLEN(b.input);
  21630. b.outLen = WC_SHA_DIGEST_SIZE;
  21631. EVP_MD_CTX_init(md_ctx);
  21632. ret = EVP_DigestInit(md_ctx, EVP_sha1());
  21633. if (ret == WOLFSSL_SUCCESS) {
  21634. ret = EVP_DigestUpdate(md_ctx, b.input, (unsigned long)b.inLen);
  21635. if (ret == WOLFSSL_SUCCESS)
  21636. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21637. }
  21638. EVP_MD_CTX_cleanup(md_ctx);
  21639. if (ret != WOLFSSL_SUCCESS)
  21640. return WC_TEST_RET_ENC_NC;
  21641. if (XMEMCMP(hash, b.output, b.outLen) != 0)
  21642. return WC_TEST_RET_ENC_NC;
  21643. #endif /* NO_SHA */
  21644. #ifdef WOLFSSL_SHA224
  21645. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21646. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21647. e.output = "\xc9\x7c\xa9\xa5\x59\x85\x0c\xe9\x7a\x04\xa9\x6d\xef\x6d\x99"
  21648. "\xa9\xe0\xe0\xe2\xab\x14\xe6\xb8\xdf\x26\x5f\xc0\xb3";
  21649. e.inLen = XSTRLEN(e.input);
  21650. e.outLen = WC_SHA224_DIGEST_SIZE;
  21651. EVP_MD_CTX_init(md_ctx);
  21652. ret = EVP_DigestInit(md_ctx, EVP_sha224());
  21653. if (ret == WOLFSSL_SUCCESS) {
  21654. ret = EVP_DigestUpdate(md_ctx, e.input, (unsigned long)e.inLen);
  21655. if (ret == WOLFSSL_SUCCESS)
  21656. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21657. }
  21658. EVP_MD_CTX_cleanup(md_ctx);
  21659. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  21660. return WC_TEST_RET_ENC_NC;
  21661. }
  21662. #endif /* WOLFSSL_SHA224 */
  21663. #ifndef NO_SHA256
  21664. d.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  21665. d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  21666. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  21667. "\x06\xC1";
  21668. d.inLen = XSTRLEN(d.input);
  21669. d.outLen = WC_SHA256_DIGEST_SIZE;
  21670. EVP_MD_CTX_init(md_ctx);
  21671. ret = EVP_DigestInit(md_ctx, EVP_sha256());
  21672. if (ret == WOLFSSL_SUCCESS) {
  21673. ret = EVP_DigestUpdate(md_ctx, d.input, (unsigned long)d.inLen);
  21674. if (ret == WOLFSSL_SUCCESS)
  21675. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21676. }
  21677. EVP_MD_CTX_cleanup(md_ctx);
  21678. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, d.output, d.outLen) != 0) {
  21679. return WC_TEST_RET_ENC_NC;
  21680. }
  21681. #endif /* !NO_SHA256 */
  21682. #ifdef WOLFSSL_SHA384
  21683. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21684. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21685. e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  21686. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  21687. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  21688. "\x74\x60\x39";
  21689. e.inLen = XSTRLEN(e.input);
  21690. e.outLen = WC_SHA384_DIGEST_SIZE;
  21691. EVP_MD_CTX_init(md_ctx);
  21692. ret = EVP_DigestInit(md_ctx, EVP_sha384());
  21693. if (ret == WOLFSSL_SUCCESS) {
  21694. ret = EVP_DigestUpdate(md_ctx, e.input, (unsigned long)e.inLen);
  21695. if (ret == WOLFSSL_SUCCESS)
  21696. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21697. }
  21698. EVP_MD_CTX_cleanup(md_ctx);
  21699. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  21700. return WC_TEST_RET_ENC_NC;
  21701. }
  21702. #endif /* WOLFSSL_SHA384 */
  21703. #ifdef WOLFSSL_SHA512
  21704. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21705. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21706. f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  21707. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  21708. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  21709. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  21710. "\x87\x4b\xe9\x09";
  21711. f.inLen = XSTRLEN(f.input);
  21712. f.outLen = WC_SHA512_DIGEST_SIZE;
  21713. EVP_MD_CTX_init(md_ctx);
  21714. ret = EVP_DigestInit(md_ctx, EVP_sha512());
  21715. if (ret == WOLFSSL_SUCCESS) {
  21716. ret = EVP_DigestUpdate(md_ctx, f.input, (unsigned long)f.inLen);
  21717. if (ret == WOLFSSL_SUCCESS)
  21718. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21719. }
  21720. EVP_MD_CTX_cleanup(md_ctx);
  21721. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, f.output, f.outLen) != 0) {
  21722. return WC_TEST_RET_ENC_NC;
  21723. }
  21724. #if !defined(WOLFSSL_NOSHA512_224) && \
  21725. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  21726. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21727. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21728. f.output = "\x23\xfe\xc5\xbb\x94\xd6\x0b\x23\x30\x81\x92\x64\x0b\x0c\x45"
  21729. "\x33\x35\xd6\x64\x73\x4f\xe4\x0e\x72\x68\x67\x4a\xf9";
  21730. f.inLen = XSTRLEN(f.input);
  21731. f.outLen = WC_SHA512_224_DIGEST_SIZE;
  21732. EVP_MD_CTX_init(md_ctx);
  21733. ret = EVP_DigestInit(md_ctx, EVP_sha512_224());
  21734. if (ret == WOLFSSL_SUCCESS) {
  21735. ret = EVP_DigestUpdate(md_ctx, f.input, (unsigned long)f.inLen);
  21736. if (ret == WOLFSSL_SUCCESS)
  21737. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21738. }
  21739. EVP_MD_CTX_cleanup(md_ctx);
  21740. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, f.output, f.outLen) != 0) {
  21741. return WC_TEST_RET_ENC_NC;
  21742. }
  21743. #endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
  21744. #if !defined(WOLFSSL_NOSHA512_256) && \
  21745. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  21746. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21747. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21748. f.output = "\x39\x28\xe1\x84\xfb\x86\x90\xf8\x40\xda\x39\x88\x12\x1d\x31"
  21749. "\xbe\x65\xcb\x9d\x3e\xf8\x3e\xe6\x14\x6f\xea\xc8\x61\xe1\x9b"
  21750. "\x56\x3a";
  21751. f.inLen = XSTRLEN(f.input);
  21752. f.outLen = WC_SHA512_256_DIGEST_SIZE;
  21753. EVP_MD_CTX_init(md_ctx);
  21754. ret = EVP_DigestInit(md_ctx, EVP_sha512_256());
  21755. if (ret == WOLFSSL_SUCCESS) {
  21756. ret = EVP_DigestUpdate(md_ctx, f.input, (unsigned long)f.inLen);
  21757. if (ret == WOLFSSL_SUCCESS)
  21758. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21759. }
  21760. EVP_MD_CTX_cleanup(md_ctx);
  21761. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, f.output, f.outLen) != 0) {
  21762. return WC_TEST_RET_ENC_NC;
  21763. }
  21764. #endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
  21765. #endif /* WOLFSSL_SHA512 */
  21766. #ifdef WOLFSSL_SHA3
  21767. #ifndef WOLFSSL_NOSHA3_224
  21768. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21769. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21770. e.output = "\x54\x3e\x68\x68\xe1\x66\x6c\x1a\x64\x36\x30\xdf\x77\x36\x7a"
  21771. "\xe5\xa6\x2a\x85\x07\x0a\x51\xc1\x4c\xbf\x66\x5c\xbc";
  21772. e.inLen = XSTRLEN(e.input);
  21773. e.outLen = WC_SHA3_224_DIGEST_SIZE;
  21774. EVP_MD_CTX_init(md_ctx);
  21775. ret = EVP_DigestInit(md_ctx, EVP_sha3_224());
  21776. if (ret == WOLFSSL_SUCCESS) {
  21777. ret = EVP_DigestUpdate(md_ctx, e.input, (unsigned long)e.inLen);
  21778. if (ret == WOLFSSL_SUCCESS)
  21779. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21780. }
  21781. EVP_MD_CTX_cleanup(md_ctx);
  21782. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  21783. return WC_TEST_RET_ENC_NC;
  21784. }
  21785. #endif /* WOLFSSL_NOSHA3_224 */
  21786. #ifndef WOLFSSL_NOSHA3_256
  21787. d.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21788. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21789. d.output = "\x91\x6f\x60\x61\xfe\x87\x97\x41\xca\x64\x69\xb4\x39\x71\xdf"
  21790. "\xdb\x28\xb1\xa3\x2d\xc3\x6c\xb3\x25\x4e\x81\x2b\xe2\x7a\xad"
  21791. "\x1d\x18";
  21792. d.inLen = XSTRLEN(d.input);
  21793. d.outLen = WC_SHA3_256_DIGEST_SIZE;
  21794. EVP_MD_CTX_init(md_ctx);
  21795. ret = EVP_DigestInit(md_ctx, EVP_sha3_256());
  21796. if (ret == WOLFSSL_SUCCESS) {
  21797. ret = EVP_DigestUpdate(md_ctx, d.input, (unsigned long)d.inLen);
  21798. if (ret == WOLFSSL_SUCCESS)
  21799. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21800. }
  21801. EVP_MD_CTX_cleanup(md_ctx);
  21802. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, d.output, d.outLen) != 0) {
  21803. return WC_TEST_RET_ENC_NC;
  21804. }
  21805. #endif /* WOLFSSL_NOSHA3_256 */
  21806. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21807. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21808. e.output = "\x79\x40\x7d\x3b\x59\x16\xb5\x9c\x3e\x30\xb0\x98\x22\x97\x47"
  21809. "\x91\xc3\x13\xfb\x9e\xcc\x84\x9e\x40\x6f\x23\x59\x2d\x04\xf6"
  21810. "\x25\xdc\x8c\x70\x9b\x98\xb4\x3b\x38\x52\xb3\x37\x21\x61\x79"
  21811. "\xaa\x7f\xc7";
  21812. e.inLen = XSTRLEN(e.input);
  21813. e.outLen = WC_SHA3_384_DIGEST_SIZE;
  21814. EVP_MD_CTX_init(md_ctx);
  21815. ret = EVP_DigestInit(md_ctx, EVP_sha3_384());
  21816. if (ret == WOLFSSL_SUCCESS) {
  21817. ret = EVP_DigestUpdate(md_ctx, e.input, (unsigned long)e.inLen);
  21818. if (ret == WOLFSSL_SUCCESS)
  21819. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21820. }
  21821. EVP_MD_CTX_cleanup(md_ctx);
  21822. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  21823. return WC_TEST_RET_ENC_NC;
  21824. }
  21825. #ifndef WOLFSSL_NOSHA3_512
  21826. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  21827. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  21828. f.output = "\xaf\xeb\xb2\xef\x54\x2e\x65\x79\xc5\x0c\xad\x06\xd2\xe5\x78"
  21829. "\xf9\xf8\xdd\x68\x81\xd7\xdc\x82\x4d\x26\x36\x0f\xee\xbf\x18"
  21830. "\xa4\xfa\x73\xe3\x26\x11\x22\x94\x8e\xfc\xfd\x49\x2e\x74\xe8"
  21831. "\x2e\x21\x89\xed\x0f\xb4\x40\xd1\x87\xf3\x82\x27\x0c\xb4\x55"
  21832. "\xf2\x1d\xd1\x85";
  21833. f.inLen = XSTRLEN(f.input);
  21834. f.outLen = WC_SHA3_512_DIGEST_SIZE;
  21835. EVP_MD_CTX_init(md_ctx);
  21836. ret = EVP_DigestInit(md_ctx, EVP_sha3_512());
  21837. if (ret == WOLFSSL_SUCCESS) {
  21838. ret = EVP_DigestUpdate(md_ctx, f.input, (unsigned long)f.inLen);
  21839. if (ret == WOLFSSL_SUCCESS)
  21840. ret = EVP_DigestFinal(md_ctx, hash, 0);
  21841. }
  21842. EVP_MD_CTX_cleanup(md_ctx);
  21843. if (ret != WOLFSSL_SUCCESS ||
  21844. XMEMCMP(hash, f.output, f.outLen) != 0) {
  21845. return WC_TEST_RET_ENC_NC;
  21846. }
  21847. #endif /* WOLFSSL_NOSHA3_512 */
  21848. #endif /* WOLFSSL_SHA3 */
  21849. #ifdef WOLFSSL_SMALL_STACK
  21850. XFREE(md_ctx, NULL, DYNAMIC_TYPE_OPENSSL);
  21851. md_ctx = NULL;
  21852. #endif
  21853. #ifndef WC_NO_RNG
  21854. if (RAND_bytes(hash, sizeof(hash)) != WOLFSSL_SUCCESS)
  21855. return WC_TEST_RET_ENC_NC;
  21856. #endif
  21857. #ifndef NO_MD5
  21858. c.input = "what do ya want for nothing?";
  21859. c.output = "\x55\x78\xe8\x48\x4b\xcc\x93\x80\x93\xec\x53\xaf\x22\xd6\x14"
  21860. "\x76";
  21861. c.inLen = XSTRLEN(c.input);
  21862. c.outLen = WC_MD5_DIGEST_SIZE;
  21863. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)
  21864. /* Expect failure with MD5 + HMAC when using FIPS 140-3. */
  21865. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  21866. hash, 0) != NULL)
  21867. #else
  21868. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  21869. hash, 0) == NULL ||
  21870. XMEMCMP(hash, c.output, c.outLen) != 0)
  21871. #endif
  21872. {
  21873. return WC_TEST_RET_ENC_NC;
  21874. }
  21875. #endif /* NO_MD5 */
  21876. #ifndef NO_DES3
  21877. { /* des test */
  21878. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  21879. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  21880. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  21881. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  21882. };
  21883. byte plain[24];
  21884. byte cipher[24];
  21885. const_DES_cblock key = {
  21886. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  21887. };
  21888. DES_cblock iv = {
  21889. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  21890. };
  21891. DES_key_schedule sched;
  21892. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  21893. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  21894. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  21895. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  21896. };
  21897. DES_key_sched(&key, &sched);
  21898. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  21899. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  21900. if (XMEMCMP(plain, vector, sizeof(vector)) != 0)
  21901. return WC_TEST_RET_ENC_NC;
  21902. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  21903. return WC_TEST_RET_ENC_NC;
  21904. /* test changing iv */
  21905. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  21906. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  21907. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  21908. return WC_TEST_RET_ENC_NC;
  21909. } /* end des test */
  21910. #endif /* NO_DES3 */
  21911. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  21912. if ((ret = openssl_aes_test()) != 0) {
  21913. return ret;
  21914. }
  21915. #if defined(WOLFSSL_AES_128) && defined(HAVE_AES_CBC)
  21916. { /* evp_cipher test: EVP_aes_128_cbc */
  21917. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21918. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  21919. #else
  21920. EVP_CIPHER_CTX ctx[1];
  21921. #endif
  21922. int idx, cipherSz, plainSz;
  21923. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  21924. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  21925. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  21926. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  21927. };
  21928. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  21929. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  21930. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  21931. 0x3b,0x5d,0x41,0x97,0x94,0x25,0xa4,0xb4,
  21932. 0xae,0x7b,0x34,0xd0,0x3f,0x0c,0xbc,0x06
  21933. };
  21934. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  21935. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  21936. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  21937. 0x7d,0x37,0x7b,0x0b,0x44,0xaa,0xb5,0xf0,
  21938. 0x5f,0x34,0xb4,0xde,0xb5,0xbd,0x2a,0xbb
  21939. };
  21940. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  21941. "0123456789abcdef "; /* align */
  21942. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  21943. "1234567890abcdef "; /* align */
  21944. byte cipher[AES_BLOCK_SIZE * 4];
  21945. byte plain [AES_BLOCK_SIZE * 4];
  21946. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21947. if (ctx == NULL)
  21948. return MEMORY_E;
  21949. #endif
  21950. cipherSz = 0;
  21951. EVP_CIPHER_CTX_init(ctx);
  21952. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  21953. if (ret == WOLFSSL_SUCCESS) {
  21954. ret = EVP_CipherUpdate(ctx, cipher, &idx, (byte*)msg, sizeof(msg));
  21955. if (ret == WOLFSSL_SUCCESS)
  21956. cipherSz += idx;
  21957. }
  21958. if (ret == WOLFSSL_SUCCESS) {
  21959. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  21960. if (ret == WOLFSSL_SUCCESS)
  21961. cipherSz += idx;
  21962. }
  21963. EVP_CIPHER_CTX_cleanup(ctx);
  21964. if (ret != WOLFSSL_SUCCESS)
  21965. return WC_TEST_RET_ENC_NC;
  21966. if (cipherSz != (int)sizeof(verify) || XMEMCMP(cipher, verify, cipherSz))
  21967. return WC_TEST_RET_ENC_NC;
  21968. /* check partial decrypt (not enough padding for full block) */
  21969. plainSz = 0;
  21970. EVP_CIPHER_CTX_init(ctx);
  21971. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  21972. if (ret == WOLFSSL_SUCCESS) {
  21973. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, 1);
  21974. if (ret == WOLFSSL_SUCCESS)
  21975. plainSz += idx;
  21976. }
  21977. if (ret == WOLFSSL_SUCCESS) {
  21978. /* this test should fail... not enough padding for full block */
  21979. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  21980. if (plainSz == 0 && ret != WOLFSSL_SUCCESS)
  21981. ret = WOLFSSL_SUCCESS;
  21982. else
  21983. ret = WC_TEST_RET_ENC_NC;
  21984. }
  21985. else
  21986. ret = WC_TEST_RET_ENC_NC;
  21987. EVP_CIPHER_CTX_cleanup(ctx);
  21988. if (ret != WOLFSSL_SUCCESS)
  21989. return ret;
  21990. plainSz = 0;
  21991. EVP_CIPHER_CTX_init(ctx);
  21992. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  21993. if (ret == WOLFSSL_SUCCESS) {
  21994. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, cipherSz);
  21995. if (ret == WOLFSSL_SUCCESS)
  21996. plainSz += idx;
  21997. }
  21998. if (ret == WOLFSSL_SUCCESS) {
  21999. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  22000. if (ret == WOLFSSL_SUCCESS)
  22001. plainSz += idx;
  22002. }
  22003. EVP_CIPHER_CTX_cleanup(ctx);
  22004. if (ret != WOLFSSL_SUCCESS)
  22005. return WC_TEST_RET_ENC_NC;
  22006. if (plainSz != (int)sizeof(msg) || XMEMCMP(plain, msg, sizeof(msg)))
  22007. return WC_TEST_RET_ENC_NC;
  22008. cipherSz = 0;
  22009. EVP_CIPHER_CTX_init(ctx);
  22010. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  22011. if (ret == WOLFSSL_SUCCESS) {
  22012. ret = EVP_CipherUpdate(ctx, cipher, &idx, msg, AES_BLOCK_SIZE);
  22013. if (ret == WOLFSSL_SUCCESS)
  22014. cipherSz += idx;
  22015. }
  22016. if (ret == WOLFSSL_SUCCESS) {
  22017. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  22018. if (ret == WOLFSSL_SUCCESS)
  22019. cipherSz += idx;
  22020. }
  22021. EVP_CIPHER_CTX_cleanup(ctx);
  22022. if (ret != WOLFSSL_SUCCESS)
  22023. return WC_TEST_RET_ENC_NC;
  22024. if (cipherSz != (int)sizeof(verify2) || XMEMCMP(cipher, verify2, cipherSz))
  22025. return WC_TEST_RET_ENC_NC;
  22026. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22027. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  22028. #endif
  22029. } /* end evp_cipher test: EVP_aes_128_cbc*/
  22030. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  22031. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  22032. { /* evp_cipher test: EVP_aes_256_ecb*/
  22033. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22034. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  22035. #else
  22036. EVP_CIPHER_CTX ctx[1];
  22037. #endif
  22038. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  22039. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  22040. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  22041. };
  22042. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  22043. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  22044. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  22045. };
  22046. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  22047. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  22048. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  22049. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  22050. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  22051. };
  22052. byte cipher[AES_BLOCK_SIZE * 4];
  22053. byte plain [AES_BLOCK_SIZE * 4];
  22054. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22055. if (ctx == NULL)
  22056. return MEMORY_E;
  22057. #endif
  22058. EVP_CIPHER_CTX_init(ctx);
  22059. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1);
  22060. if (ret == WOLFSSL_SUCCESS)
  22061. ret = EVP_Cipher(ctx, cipher, (byte*)msg, 16);
  22062. EVP_CIPHER_CTX_cleanup(ctx);
  22063. if (ret != 16)
  22064. return WC_TEST_RET_ENC_NC;
  22065. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  22066. return WC_TEST_RET_ENC_NC;
  22067. EVP_CIPHER_CTX_init(ctx);
  22068. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0);
  22069. if (ret == WOLFSSL_SUCCESS)
  22070. ret = EVP_Cipher(ctx, plain, cipher, 16);
  22071. EVP_CIPHER_CTX_cleanup(ctx);
  22072. if (ret != 16)
  22073. return WC_TEST_RET_ENC_NC;
  22074. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  22075. return WC_TEST_RET_ENC_NC;
  22076. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22077. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  22078. #endif
  22079. } /* end evp_cipher test */
  22080. #endif /* HAVE_AES_ECB && WOLFSSL_AES_128 */
  22081. #ifndef WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API
  22082. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  22083. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  22084. {
  22085. /* Test: AES_encrypt/decrypt/set Key */
  22086. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22087. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  22088. #ifdef HAVE_AES_DECRYPT
  22089. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  22090. #endif
  22091. #else
  22092. AES_KEY enc[1];
  22093. #ifdef HAVE_AES_DECRYPT
  22094. AES_KEY dec[1];
  22095. #endif
  22096. #endif
  22097. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  22098. {
  22099. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  22100. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  22101. };
  22102. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  22103. {
  22104. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  22105. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  22106. };
  22107. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  22108. {
  22109. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  22110. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  22111. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  22112. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  22113. };
  22114. byte plain[sizeof(msg)];
  22115. byte cipher[sizeof(msg)];
  22116. printf("openSSL extra test\n") ;
  22117. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22118. if (enc == NULL)
  22119. return MEMORY_E;
  22120. #ifdef HAVE_AES_DECRYPT
  22121. if (dec == NULL)
  22122. return MEMORY_E;
  22123. #endif
  22124. #endif
  22125. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  22126. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  22127. AES_encrypt(msg, cipher, enc);
  22128. #ifdef HAVE_AES_DECRYPT
  22129. AES_decrypt(cipher, plain, dec);
  22130. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  22131. return WC_TEST_RET_ENC_NC;
  22132. #endif /* HAVE_AES_DECRYPT */
  22133. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  22134. return WC_TEST_RET_ENC_NC;
  22135. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22136. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  22137. #ifdef HAVE_AES_DECRYPT
  22138. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  22139. #endif
  22140. #endif
  22141. }
  22142. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  22143. #endif /* !WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API */
  22144. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  22145. #ifdef WOLFSSL_AES_COUNTER
  22146. {
  22147. byte plainBuff [64];
  22148. byte cipherBuff[64];
  22149. #ifdef WOLFSSL_AES_128
  22150. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  22151. {
  22152. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  22153. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  22154. };
  22155. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  22156. {
  22157. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  22158. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  22159. };
  22160. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  22161. {
  22162. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  22163. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  22164. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  22165. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  22166. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  22167. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  22168. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  22169. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  22170. };
  22171. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  22172. {
  22173. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  22174. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  22175. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  22176. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  22177. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  22178. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  22179. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  22180. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  22181. };
  22182. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  22183. {
  22184. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  22185. 0xc2
  22186. };
  22187. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22188. EVP_CIPHER_CTX *p_en;
  22189. EVP_CIPHER_CTX *p_de;
  22190. #endif
  22191. #endif /* WOLFSSL_AES_128 */
  22192. #ifdef WOLFSSL_AES_192
  22193. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  22194. * NIST Special Publication 800-38A */
  22195. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  22196. {
  22197. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  22198. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  22199. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  22200. };
  22201. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  22202. {
  22203. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  22204. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  22205. };
  22206. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  22207. {
  22208. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  22209. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  22210. };
  22211. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  22212. {
  22213. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  22214. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  22215. };
  22216. #endif /* WOLFSSL_AES_192 */
  22217. #ifdef WOLFSSL_AES_256
  22218. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  22219. * NIST Special Publication 800-38A */
  22220. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  22221. {
  22222. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  22223. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  22224. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  22225. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  22226. };
  22227. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  22228. {
  22229. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  22230. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  22231. };
  22232. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  22233. {
  22234. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  22235. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  22236. };
  22237. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  22238. {
  22239. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  22240. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  22241. };
  22242. #endif /* WOLFSSL_AES_256 */
  22243. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22244. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  22245. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  22246. if ((en == NULL) || (de == NULL))
  22247. return MEMORY_E;
  22248. #else
  22249. EVP_CIPHER_CTX en[1];
  22250. EVP_CIPHER_CTX de[1];
  22251. #endif
  22252. #ifdef WOLFSSL_AES_128
  22253. EVP_CIPHER_CTX_init(en);
  22254. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  22255. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  22256. return WC_TEST_RET_ENC_NC;
  22257. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  22258. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  22259. return WC_TEST_RET_ENC_NC;
  22260. EVP_CIPHER_CTX_init(de);
  22261. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  22262. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  22263. return WC_TEST_RET_ENC_NC;
  22264. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  22265. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  22266. return WC_TEST_RET_ENC_NC;
  22267. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  22268. return WC_TEST_RET_ENC_NC;
  22269. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  22270. return WC_TEST_RET_ENC_NC;
  22271. EVP_CIPHER_CTX_cleanup(en);
  22272. EVP_CIPHER_CTX_cleanup(de);
  22273. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22274. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  22275. if (p_en == NULL)
  22276. return WC_TEST_RET_ENC_ERRNO;
  22277. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  22278. if (p_de == NULL)
  22279. return WC_TEST_RET_ENC_ERRNO;
  22280. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  22281. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  22282. return WC_TEST_RET_ENC_NC;
  22283. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  22284. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  22285. return WC_TEST_RET_ENC_NC;
  22286. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  22287. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  22288. return WC_TEST_RET_ENC_NC;
  22289. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  22290. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  22291. return WC_TEST_RET_ENC_NC;
  22292. EVP_CIPHER_CTX_cleanup(p_en);
  22293. EVP_CIPHER_CTX_cleanup(p_de);
  22294. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  22295. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  22296. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  22297. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  22298. return WC_TEST_RET_ENC_NC;
  22299. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  22300. return WC_TEST_RET_ENC_NC;
  22301. EVP_CIPHER_CTX_init(en);
  22302. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  22303. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  22304. return WC_TEST_RET_ENC_NC;
  22305. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  22306. return WC_TEST_RET_ENC_NC;
  22307. EVP_CIPHER_CTX_init(de);
  22308. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  22309. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  22310. return WC_TEST_RET_ENC_NC;
  22311. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  22312. return WC_TEST_RET_ENC_NC;
  22313. if (XMEMCMP(plainBuff, ctrPlain, 9))
  22314. return WC_TEST_RET_ENC_NC;
  22315. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  22316. return WC_TEST_RET_ENC_NC;
  22317. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  22318. return WC_TEST_RET_ENC_NC;
  22319. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  22320. return WC_TEST_RET_ENC_NC;
  22321. if (XMEMCMP(plainBuff, ctrPlain, 9))
  22322. return WC_TEST_RET_ENC_NC;
  22323. if (XMEMCMP(cipherBuff, oddCipher, 9))
  22324. return WC_TEST_RET_ENC_NC;
  22325. EVP_CIPHER_CTX_cleanup(en);
  22326. EVP_CIPHER_CTX_cleanup(de);
  22327. #endif /* WOLFSSL_AES_128 */
  22328. #ifdef WOLFSSL_AES_192
  22329. EVP_CIPHER_CTX_init(en);
  22330. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  22331. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  22332. return WC_TEST_RET_ENC_NC;
  22333. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  22334. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  22335. return WC_TEST_RET_ENC_NC;
  22336. EVP_CIPHER_CTX_init(de);
  22337. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  22338. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  22339. return WC_TEST_RET_ENC_NC;
  22340. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  22341. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  22342. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  22343. return WC_TEST_RET_ENC_NC;
  22344. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  22345. return WC_TEST_RET_ENC_NC;
  22346. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  22347. return WC_TEST_RET_ENC_NC;
  22348. EVP_CIPHER_CTX_cleanup(en);
  22349. EVP_CIPHER_CTX_cleanup(de);
  22350. #endif /* WOLFSSL_AES_192 */
  22351. #ifdef WOLFSSL_AES_256
  22352. EVP_CIPHER_CTX_init(en);
  22353. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  22354. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  22355. return WC_TEST_RET_ENC_NC;
  22356. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  22357. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  22358. return WC_TEST_RET_ENC_NC;
  22359. EVP_CIPHER_CTX_init(de);
  22360. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  22361. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  22362. return WC_TEST_RET_ENC_NC;
  22363. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  22364. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  22365. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  22366. return WC_TEST_RET_ENC_NC;
  22367. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  22368. return WC_TEST_RET_ENC_NC;
  22369. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  22370. return WC_TEST_RET_ENC_NC;
  22371. EVP_CIPHER_CTX_cleanup(en);
  22372. EVP_CIPHER_CTX_cleanup(de);
  22373. #endif /* WOLFSSL_AES_256 */
  22374. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22375. wolfSSL_EVP_CIPHER_CTX_free(en);
  22376. wolfSSL_EVP_CIPHER_CTX_free(de);
  22377. #endif
  22378. }
  22379. #endif /* HAVE_AES_COUNTER */
  22380. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  22381. {
  22382. /* EVP_CipherUpdate test */
  22383. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  22384. {
  22385. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  22386. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  22387. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  22388. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  22389. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  22390. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  22391. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  22392. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  22393. };
  22394. byte key[] = "0123456789abcdef "; /* align */
  22395. byte iv[] = "1234567890abcdef "; /* align */
  22396. byte cipher[AES_BLOCK_SIZE * 4];
  22397. byte plain [AES_BLOCK_SIZE * 4];
  22398. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22399. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  22400. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  22401. #else
  22402. EVP_CIPHER_CTX en[1];
  22403. EVP_CIPHER_CTX de[1];
  22404. #endif
  22405. int outlen ;
  22406. int total = 0;
  22407. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22408. if ((en == NULL) || (de == NULL))
  22409. return MEMORY_E;
  22410. #endif
  22411. EVP_CIPHER_CTX_init(en);
  22412. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  22413. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  22414. return WC_TEST_RET_ENC_NC;
  22415. /* openSSL compatibility, if(inlen == 0)return 1; */
  22416. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  22417. (byte*)cbcPlain, 0) != 1)
  22418. return WC_TEST_RET_ENC_NC;
  22419. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  22420. return WC_TEST_RET_ENC_NC;
  22421. EVP_CIPHER_CTX_init(en);
  22422. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  22423. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  22424. return WC_TEST_RET_ENC_NC;
  22425. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  22426. (byte*)cbcPlain, 9) == 0)
  22427. return WC_TEST_RET_ENC_NC;
  22428. if(outlen != 0)
  22429. return WC_TEST_RET_ENC_NC;
  22430. total += outlen;
  22431. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  22432. (byte*)&cbcPlain[9] , 9) == 0)
  22433. return WC_TEST_RET_ENC_NC;
  22434. if(outlen != 16)
  22435. return WC_TEST_RET_ENC_NC;
  22436. total += outlen;
  22437. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  22438. return WC_TEST_RET_ENC_NC;
  22439. if(outlen != 16)
  22440. return WC_TEST_RET_ENC_NC;
  22441. total += outlen;
  22442. if(total != 32)
  22443. return WC_TEST_RET_ENC_NC;
  22444. total = 0;
  22445. EVP_CIPHER_CTX_init(de);
  22446. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  22447. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  22448. return WC_TEST_RET_ENC_NC;
  22449. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  22450. return WC_TEST_RET_ENC_NC;
  22451. if(outlen != 0)
  22452. return WC_TEST_RET_ENC_NC;
  22453. total += outlen;
  22454. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  22455. (byte*)&cipher[6], 12) == 0)
  22456. return WC_TEST_RET_ENC_NC;
  22457. if(outlen != 0)
  22458. total += outlen;
  22459. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  22460. (byte*)&cipher[6+12], 14) == 0)
  22461. return WC_TEST_RET_ENC_NC;
  22462. if(outlen != 16)
  22463. return WC_TEST_RET_ENC_NC;
  22464. total += outlen;
  22465. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  22466. return WC_TEST_RET_ENC_NC;
  22467. if(outlen != 2)
  22468. return WC_TEST_RET_ENC_NC;
  22469. total += outlen;
  22470. if(total != 18)
  22471. return WC_TEST_RET_ENC_NC;
  22472. if (XMEMCMP(plain, cbcPlain, 18))
  22473. return WC_TEST_RET_ENC_NC;
  22474. total = 0;
  22475. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  22476. return WC_TEST_RET_ENC_NC;
  22477. EVP_CIPHER_CTX_init(en);
  22478. if (EVP_EncryptInit(en, EVP_aes_128_cbc(),
  22479. (unsigned char*)key, (unsigned char*)iv) == 0)
  22480. return WC_TEST_RET_ENC_NC;
  22481. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen, (byte*)cbcPlain, 9) == 0)
  22482. return WC_TEST_RET_ENC_NC;
  22483. if(outlen != 0)
  22484. return WC_TEST_RET_ENC_NC;
  22485. total += outlen;
  22486. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen, (byte*)&cbcPlain[9] , 9) == 0)
  22487. return WC_TEST_RET_ENC_NC;
  22488. if(outlen != 16)
  22489. return WC_TEST_RET_ENC_NC;
  22490. total += outlen;
  22491. if (EVP_EncryptFinal(en, (byte*)&cipher[total], &outlen) == 0)
  22492. return WC_TEST_RET_ENC_NC;
  22493. if(outlen != 16)
  22494. return WC_TEST_RET_ENC_NC;
  22495. total += outlen;
  22496. if(total != 32)
  22497. return 3438;
  22498. total = 0;
  22499. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  22500. return WC_TEST_RET_ENC_NC;
  22501. EVP_CIPHER_CTX_init(de);
  22502. if (EVP_DecryptInit(de, EVP_aes_128_cbc(),
  22503. (unsigned char*)key, (unsigned char*)iv) == 0)
  22504. return WC_TEST_RET_ENC_NC;
  22505. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  22506. return WC_TEST_RET_ENC_NC;
  22507. if(outlen != 0)
  22508. return WC_TEST_RET_ENC_NC;
  22509. total += outlen;
  22510. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6], 12) == 0)
  22511. return WC_TEST_RET_ENC_NC;
  22512. if(outlen != 0)
  22513. total += outlen;
  22514. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6+12], 14) == 0)
  22515. return WC_TEST_RET_ENC_NC;
  22516. if(outlen != 16)
  22517. return WC_TEST_RET_ENC_NC;
  22518. total += outlen;
  22519. if (EVP_DecryptFinal(de, (byte*)&plain[total], &outlen) == 0)
  22520. return WC_TEST_RET_ENC_NC;
  22521. if(outlen != 2)
  22522. return WC_TEST_RET_ENC_NC;
  22523. total += outlen;
  22524. if(total != 18)
  22525. return 3447;
  22526. if (XMEMCMP(plain, cbcPlain, 18))
  22527. return WC_TEST_RET_ENC_NC;
  22528. if (EVP_CIPHER_key_length(NULL) != 0)
  22529. return WC_TEST_RET_ENC_NC;
  22530. if (EVP_CIPHER_key_length(EVP_aes_128_cbc()) != 16)
  22531. return WC_TEST_RET_ENC_NC;
  22532. if (EVP_CIPHER_CTX_mode(NULL) != 0)
  22533. return WC_TEST_RET_ENC_NC;
  22534. if (EVP_CIPHER_CTX_mode(en) != (en->flags & WOLFSSL_EVP_CIPH_MODE))
  22535. return WC_TEST_RET_ENC_NC;
  22536. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  22537. return WC_TEST_RET_ENC_NC;
  22538. EVP_CIPHER_CTX_init(en);
  22539. if (EVP_CipherInit_ex(en, EVP_aes_128_cbc(), NULL,
  22540. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  22541. return WC_TEST_RET_ENC_NC;
  22542. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  22543. return WC_TEST_RET_ENC_NC;
  22544. EVP_CIPHER_CTX_init(en);
  22545. if (EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  22546. (unsigned char*)key, (unsigned char*)iv) == 0)
  22547. return WC_TEST_RET_ENC_NC;
  22548. if (wolfSSL_EVP_EncryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  22549. return WC_TEST_RET_ENC_NC;
  22550. if (wolfSSL_EVP_EncryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  22551. return WC_TEST_RET_ENC_NC;
  22552. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  22553. return WC_TEST_RET_ENC_NC;
  22554. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  22555. return WC_TEST_RET_ENC_NC;
  22556. EVP_CIPHER_CTX_init(de);
  22557. if (EVP_DecryptInit_ex(de, EVP_aes_128_cbc(), NULL,
  22558. (unsigned char*)key, (unsigned char*)iv) == 0)
  22559. return WC_TEST_RET_ENC_NC;
  22560. if (wolfSSL_EVP_DecryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  22561. return WC_TEST_RET_ENC_NC;
  22562. if (wolfSSL_EVP_DecryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  22563. return WC_TEST_RET_ENC_NC;
  22564. if (EVP_CIPHER_CTX_block_size(NULL) != BAD_FUNC_ARG)
  22565. return WC_TEST_RET_ENC_NC;
  22566. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  22567. return WC_TEST_RET_ENC_NC;
  22568. EVP_CIPHER_CTX_init(en);
  22569. EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  22570. (unsigned char*)key, (unsigned char*)iv);
  22571. if (EVP_CIPHER_CTX_block_size(en) != en->block_size)
  22572. return WC_TEST_RET_ENC_NC;
  22573. if (EVP_CIPHER_block_size(NULL) != BAD_FUNC_ARG)
  22574. return WC_TEST_RET_ENC_NC;
  22575. if (EVP_CIPHER_block_size(EVP_aes_128_cbc()) != AES_BLOCK_SIZE)
  22576. return WC_TEST_RET_ENC_NC;
  22577. if (WOLFSSL_EVP_CIPHER_mode(NULL) != 0)
  22578. return WC_TEST_RET_ENC_NC;
  22579. if (EVP_CIPHER_flags(EVP_aes_128_cbc()) != WOLFSSL_EVP_CIPH_CBC_MODE)
  22580. return WC_TEST_RET_ENC_NC;
  22581. EVP_CIPHER_CTX_clear_flags(en, 0xFFFFFFFF);
  22582. EVP_CIPHER_CTX_set_flags(en, 42);
  22583. if (en->flags != 42)
  22584. return WC_TEST_RET_ENC_NC;
  22585. if (EVP_CIPHER_CTX_set_padding(NULL, 0) != BAD_FUNC_ARG)
  22586. return WC_TEST_RET_ENC_NC;
  22587. if (EVP_CIPHER_CTX_set_padding(en, 0) != WOLFSSL_SUCCESS)
  22588. return WC_TEST_RET_ENC_NC;
  22589. if (EVP_CIPHER_CTX_set_padding(en, 1) != WOLFSSL_SUCCESS)
  22590. return WC_TEST_RET_ENC_NC;
  22591. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  22592. return WC_TEST_RET_ENC_NC;
  22593. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  22594. return WC_TEST_RET_ENC_NC;
  22595. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22596. wolfSSL_EVP_CIPHER_CTX_free(en);
  22597. wolfSSL_EVP_CIPHER_CTX_free(de);
  22598. #endif
  22599. }
  22600. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  22601. #endif /* ifndef NO_AES */
  22602. return 0;
  22603. }
  22604. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openSSL_evpMD_test(void)
  22605. {
  22606. wc_test_ret_t ret = 0;
  22607. #if !defined(NO_SHA256) && !defined(NO_SHA)
  22608. WOLFSSL_EVP_MD_CTX* ctx;
  22609. WOLFSSL_EVP_MD_CTX* ctx2;
  22610. WOLFSSL_ENTER("openSSL_evpMD_test");
  22611. ctx = EVP_MD_CTX_create();
  22612. ctx2 = EVP_MD_CTX_create();
  22613. ret = EVP_DigestInit(ctx, EVP_sha256());
  22614. if (ret != SSL_SUCCESS) {
  22615. ret = WC_TEST_RET_ENC_NC;
  22616. goto openSSL_evpMD_test_done;
  22617. }
  22618. ret = EVP_MD_CTX_copy(ctx2, ctx);
  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_DigestInit(ctx, EVP_sha1());
  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. ret = EVP_MD_CTX_copy_ex(ctx2, ctx);
  22637. if (ret != SSL_SUCCESS) {
  22638. ret = WC_TEST_RET_ENC_NC;
  22639. goto openSSL_evpMD_test_done;
  22640. }
  22641. if (EVP_MD_type(EVP_sha256()) == EVP_MD_CTX_type(ctx2)) {
  22642. ret = WC_TEST_RET_ENC_NC;
  22643. goto openSSL_evpMD_test_done;
  22644. }
  22645. if (EVP_MD_type(EVP_sha1()) != EVP_MD_CTX_type(ctx2)) {
  22646. ret = WC_TEST_RET_ENC_NC;
  22647. goto openSSL_evpMD_test_done;
  22648. }
  22649. if (EVP_DigestInit_ex(ctx, EVP_sha1(), NULL) != SSL_SUCCESS) {
  22650. ret = WC_TEST_RET_ENC_NC;
  22651. goto openSSL_evpMD_test_done;
  22652. }
  22653. if (EVP_add_digest(NULL) != 0) {
  22654. ret = WC_TEST_RET_ENC_NC;
  22655. goto openSSL_evpMD_test_done;
  22656. }
  22657. if (wolfSSL_EVP_add_cipher(NULL) != 0) {
  22658. ret = WC_TEST_RET_ENC_NC;
  22659. goto openSSL_evpMD_test_done;
  22660. }
  22661. ret = 0; /* got to success state without jumping to end with a fail */
  22662. openSSL_evpMD_test_done:
  22663. EVP_MD_CTX_destroy(ctx);
  22664. EVP_MD_CTX_destroy(ctx2);
  22665. #endif /* NO_SHA256 */
  22666. return ret;
  22667. }
  22668. #ifdef DEBUG_SIGN
  22669. static void show(const char *title, const char *p, unsigned int s) {
  22670. char* i;
  22671. printf("%s: ", title);
  22672. for (i = p;
  22673. i < p + s;
  22674. printf("%c", *i), i++);
  22675. printf("\n");
  22676. }
  22677. #else
  22678. #define show(a,b,c) WC_DO_NOTHING
  22679. #endif
  22680. #define FOURK_BUFF 4096
  22681. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey0_test(void)
  22682. {
  22683. wc_test_ret_t ret = 0;
  22684. #if !defined(NO_RSA) && !defined(NO_SHA)
  22685. byte* prvTmp;
  22686. byte* pubTmp;
  22687. int prvBytes;
  22688. int pubBytes;
  22689. RSA *prvRsa = NULL;
  22690. RSA *pubRsa = NULL;
  22691. EVP_PKEY *prvPkey = NULL;
  22692. EVP_PKEY *pubPkey = NULL;
  22693. EVP_PKEY_CTX *enc = NULL;
  22694. EVP_PKEY_CTX *dec = NULL;
  22695. byte in[] = TEST_STRING;
  22696. byte out[256];
  22697. size_t outlen;
  22698. size_t keySz;
  22699. byte plain[256];
  22700. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  22701. XFILE keyFile;
  22702. XFILE keypubFile;
  22703. char cliKey[] = "./certs/client-key.der";
  22704. char cliKeypub[] = "./certs/client-keyPub.der";
  22705. #endif
  22706. WOLFSSL_ENTER("openssl_pkey0_test");
  22707. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22708. if (prvTmp == NULL)
  22709. return WC_TEST_RET_ENC_ERRNO;
  22710. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22711. if (pubTmp == NULL) {
  22712. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22713. return WC_TEST_RET_ENC_NC;
  22714. }
  22715. #ifdef USE_CERT_BUFFERS_1024
  22716. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  22717. prvBytes = sizeof_client_key_der_1024;
  22718. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  22719. pubBytes = sizeof_client_keypub_der_1024;
  22720. #elif defined(USE_CERT_BUFFERS_2048)
  22721. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  22722. prvBytes = sizeof_client_key_der_2048;
  22723. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  22724. pubBytes = sizeof_client_keypub_der_2048;
  22725. #else
  22726. keyFile = XFOPEN(cliKey, "rb");
  22727. if (!keyFile) {
  22728. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22729. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22730. err_sys("can't open ./certs/client-key.der, "
  22731. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  22732. return WC_TEST_RET_ENC_ERRNO;
  22733. }
  22734. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  22735. XFCLOSE(keyFile);
  22736. if (prvBytes == 0) {
  22737. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22738. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22739. return WC_TEST_RET_ENC_ERRNO;
  22740. }
  22741. keypubFile = XFOPEN(cliKeypub, "rb");
  22742. if (!keypubFile) {
  22743. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22744. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22745. err_sys("can't open ./certs/client-cert.der, "
  22746. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  22747. return WC_TEST_RET_ENC_ERRNO;
  22748. }
  22749. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  22750. XFCLOSE(keypubFile);
  22751. if (pubBytes == 0) {
  22752. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22753. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22754. return WC_TEST_RET_ENC_ERRNO;
  22755. }
  22756. #endif /* USE_CERT_BUFFERS */
  22757. prvRsa = wolfSSL_RSA_new();
  22758. pubRsa = wolfSSL_RSA_new();
  22759. if((prvRsa == NULL) || (pubRsa == NULL)){
  22760. printf("error with RSA_new\n");
  22761. ret = WC_TEST_RET_ENC_ERRNO;
  22762. goto openssl_pkey0_test_done;
  22763. }
  22764. ret = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  22765. if(ret != SSL_SUCCESS){
  22766. printf("error with RSA_LoadDer_ex\n");
  22767. ret = WC_TEST_RET_ENC_EC(ret);
  22768. goto openssl_pkey0_test_done;
  22769. }
  22770. ret = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  22771. if(ret != SSL_SUCCESS){
  22772. printf("error with RSA_LoadDer_ex\n");
  22773. ret = WC_TEST_RET_ENC_EC(ret);
  22774. goto openssl_pkey0_test_done;
  22775. }
  22776. keySz = (size_t)RSA_size(pubRsa);
  22777. prvPkey = wolfSSL_EVP_PKEY_new();
  22778. pubPkey = wolfSSL_EVP_PKEY_new();
  22779. if((prvPkey == NULL) || (pubPkey == NULL)){
  22780. printf("error with PKEY_new\n");
  22781. ret = WC_TEST_RET_ENC_NC;
  22782. goto openssl_pkey0_test_done;
  22783. }
  22784. ret = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  22785. ret += wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  22786. if(ret != 2){
  22787. printf("error with PKEY_set1_RSA\n");
  22788. ret = WC_TEST_RET_ENC_I(ret);
  22789. goto openssl_pkey0_test_done;
  22790. }
  22791. dec = EVP_PKEY_CTX_new(prvPkey, NULL);
  22792. enc = EVP_PKEY_CTX_new(pubPkey, NULL);
  22793. if((dec == NULL)||(enc==NULL)){
  22794. printf("error with EVP_PKEY_CTX_new\n");
  22795. ret = WC_TEST_RET_ENC_NC;
  22796. goto openssl_pkey0_test_done;
  22797. }
  22798. ret = EVP_PKEY_decrypt_init(dec);
  22799. if (ret != 1) {
  22800. printf("error with decrypt init\n");
  22801. ret = WC_TEST_RET_ENC_NC;
  22802. goto openssl_pkey0_test_done;
  22803. }
  22804. ret = EVP_PKEY_encrypt_init(enc);
  22805. if (ret != 1) {
  22806. printf("error with encrypt init\n");
  22807. ret = WC_TEST_RET_ENC_NC;
  22808. goto openssl_pkey0_test_done;
  22809. }
  22810. XMEMSET(out, 0, sizeof(out));
  22811. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  22812. if (ret != 1) {
  22813. printf("error encrypting msg\n");
  22814. ret = WC_TEST_RET_ENC_NC;
  22815. goto openssl_pkey0_test_done;
  22816. }
  22817. show("encrypted msg", out, outlen);
  22818. XMEMSET(plain, 0, sizeof(plain));
  22819. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  22820. if (ret != 1) {
  22821. printf("error decrypting msg\n");
  22822. ret = WC_TEST_RET_ENC_NC;
  22823. goto openssl_pkey0_test_done;
  22824. }
  22825. show("decrypted msg", plain, outlen);
  22826. /* RSA_PKCS1_OAEP_PADDING test */
  22827. ret = EVP_PKEY_decrypt_init(dec);
  22828. if (ret != 1) {
  22829. printf("error with decrypt init\n");
  22830. ret = WC_TEST_RET_ENC_NC;
  22831. goto openssl_pkey0_test_done;
  22832. }
  22833. ret = EVP_PKEY_encrypt_init(enc);
  22834. if (ret != 1) {
  22835. printf("error with encrypt init\n");
  22836. ret = WC_TEST_RET_ENC_NC;
  22837. goto openssl_pkey0_test_done;
  22838. }
  22839. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  22840. printf("first set rsa padding error\n");
  22841. ret = WC_TEST_RET_ENC_EC(ret);
  22842. goto openssl_pkey0_test_done;
  22843. }
  22844. #ifndef HAVE_FIPS
  22845. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  22846. printf("second set rsa padding error\n");
  22847. ret = WC_TEST_RET_ENC_EC(ret);
  22848. goto openssl_pkey0_test_done;
  22849. }
  22850. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  22851. printf("third set rsa padding error\n");
  22852. ret = WC_TEST_RET_ENC_EC(ret);
  22853. goto openssl_pkey0_test_done;
  22854. }
  22855. #endif
  22856. XMEMSET(out, 0, sizeof(out));
  22857. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  22858. if (ret != 1) {
  22859. printf("error encrypting msg\n");
  22860. ret = WC_TEST_RET_ENC_NC;
  22861. goto openssl_pkey0_test_done;
  22862. }
  22863. show("encrypted msg", out, outlen);
  22864. XMEMSET(plain, 0, sizeof(plain));
  22865. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  22866. if (ret != 1) {
  22867. printf("error decrypting msg\n");
  22868. ret = WC_TEST_RET_ENC_NC;
  22869. goto openssl_pkey0_test_done;
  22870. }
  22871. show("decrypted msg", plain, outlen);
  22872. ret = 0; /* made it to this point without error then set success */
  22873. openssl_pkey0_test_done:
  22874. wolfSSL_RSA_free(prvRsa);
  22875. wolfSSL_RSA_free(pubRsa);
  22876. EVP_PKEY_free(pubPkey);
  22877. EVP_PKEY_free(prvPkey);
  22878. EVP_PKEY_CTX_free(dec);
  22879. EVP_PKEY_CTX_free(enc);
  22880. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22881. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22882. #endif /* NO_RSA */
  22883. return ret;
  22884. }
  22885. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey1_test(void)
  22886. {
  22887. wc_test_ret_t ret = 0;
  22888. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_SHA)
  22889. EVP_PKEY_CTX* dec = NULL;
  22890. EVP_PKEY_CTX* enc = NULL;
  22891. EVP_PKEY* pubKey = NULL;
  22892. EVP_PKEY* prvKey = NULL;
  22893. X509* x509 = NULL;
  22894. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sugar slapped";
  22895. const unsigned char* clikey;
  22896. long cliKeySz;
  22897. size_t outlen;
  22898. int keyLenBits = 2048;
  22899. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22900. unsigned char *tmp = (unsigned char *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22901. unsigned char *cipher = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22902. unsigned char *plain = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22903. if ((tmp == NULL) ||
  22904. (cipher == NULL) ||
  22905. (plain == NULL)) {
  22906. ret = WC_TEST_RET_ENC_NC;
  22907. goto openssl_pkey1_test_done;
  22908. }
  22909. #else
  22910. unsigned char tmp[FOURK_BUF];
  22911. unsigned char cipher[RSA_TEST_BYTES];
  22912. unsigned char plain[RSA_TEST_BYTES];
  22913. #endif
  22914. WOLFSSL_ENTER("openssl_pkey1_test");
  22915. #if defined(USE_CERT_BUFFERS_1024)
  22916. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  22917. cliKeySz = (long)sizeof_client_key_der_1024;
  22918. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_1024,
  22919. sizeof_client_cert_der_1024, SSL_FILETYPE_ASN1);
  22920. keyLenBits = 1024;
  22921. #elif defined(USE_CERT_BUFFERS_2048)
  22922. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  22923. cliKeySz = (long)sizeof_client_key_der_2048;
  22924. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_2048,
  22925. sizeof_client_cert_der_2048, SSL_FILETYPE_ASN1);
  22926. #elif defined(USE_CERT_BUFFERS_3072)
  22927. XMEMCPY(tmp, client_key_der_3072, sizeof_client_key_der_3072);
  22928. cliKeySz = (long)sizeof_client_key_der_3072;
  22929. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_3072,
  22930. sizeof_client_cert_der_3072, SSL_FILETYPE_ASN1);
  22931. keyLenBits = 3072;
  22932. #elif defined(USE_CERT_BUFFERS_4096)
  22933. XMEMCPY(tmp, client_key_der_4096, sizeof_client_key_der_4096);
  22934. cliKeySz = (long)sizeof_client_key_der_4096;
  22935. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_4096,
  22936. sizeof_client_cert_der_4096, SSL_FILETYPE_ASN1);
  22937. keyLenBits = 4096;
  22938. #else
  22939. {
  22940. XFILE f;
  22941. f = XFOPEN(clientKey, "rb");
  22942. if (!f) {
  22943. err_sys("can't open ./certs/client-key.der, "
  22944. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  22945. ret = WC_TEST_RET_ENC_ERRNO;
  22946. goto openssl_pkey1_test_done;
  22947. }
  22948. cliKeySz = (long)XFREAD(tmp, 1, FOURK_BUF, f);
  22949. XFCLOSE(f);
  22950. if (cliKeySz == 0)
  22951. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, openssl_pkey1_test_done);
  22952. }
  22953. /* using existing wolfSSL api to get public and private key */
  22954. x509 = wolfSSL_X509_load_certificate_file(clientCert, SSL_FILETYPE_ASN1);
  22955. #endif /* USE_CERT_BUFFERS */
  22956. clikey = tmp;
  22957. if ((prvKey = EVP_PKEY_new()) == NULL) {
  22958. ret = WC_TEST_RET_ENC_ERRNO;
  22959. goto openssl_pkey1_test_done;
  22960. }
  22961. EVP_PKEY_free(prvKey);
  22962. prvKey = NULL;
  22963. if (x509 == NULL) {
  22964. ret = WC_TEST_RET_ENC_NC;
  22965. goto openssl_pkey1_test_done;
  22966. }
  22967. pubKey = X509_get_pubkey(x509);
  22968. if (pubKey == NULL) {
  22969. ret = WC_TEST_RET_ENC_NC;
  22970. goto openssl_pkey1_test_done;
  22971. }
  22972. prvKey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &clikey, cliKeySz);
  22973. if (prvKey == NULL) {
  22974. ret = WC_TEST_RET_ENC_NC;
  22975. goto openssl_pkey1_test_done;
  22976. }
  22977. /* phase 2 API to create EVP_PKEY_CTX and encrypt/decrypt */
  22978. if (EVP_PKEY_bits(prvKey) != keyLenBits) {
  22979. ret = WC_TEST_RET_ENC_NC;
  22980. goto openssl_pkey1_test_done;
  22981. }
  22982. if (EVP_PKEY_size(prvKey) != keyLenBits/8) {
  22983. ret = WC_TEST_RET_ENC_NC;
  22984. goto openssl_pkey1_test_done;
  22985. }
  22986. dec = EVP_PKEY_CTX_new(prvKey, NULL);
  22987. enc = EVP_PKEY_CTX_new(pubKey, NULL);
  22988. if (dec == NULL || enc == NULL) {
  22989. ret = WC_TEST_RET_ENC_ERRNO;
  22990. goto openssl_pkey1_test_done;
  22991. }
  22992. if (EVP_PKEY_decrypt_init(dec) != 1) {
  22993. ret = WC_TEST_RET_ENC_NC;
  22994. goto openssl_pkey1_test_done;
  22995. }
  22996. if (EVP_PKEY_encrypt_init(enc) != 1) {
  22997. ret = WC_TEST_RET_ENC_NC;
  22998. goto openssl_pkey1_test_done;
  22999. }
  23000. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  23001. ret = WC_TEST_RET_ENC_NC;
  23002. goto openssl_pkey1_test_done;
  23003. }
  23004. #ifndef HAVE_FIPS
  23005. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  23006. ret = WC_TEST_RET_ENC_EC(ret);
  23007. goto openssl_pkey1_test_done;
  23008. }
  23009. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  23010. ret = WC_TEST_RET_ENC_EC(ret);
  23011. goto openssl_pkey1_test_done;
  23012. }
  23013. #endif
  23014. XMEMSET(cipher, 0, RSA_TEST_BYTES);
  23015. outlen = (size_t)(keyLenBits/8);
  23016. if (EVP_PKEY_encrypt(enc, cipher, &outlen, msg, sizeof(msg)) < 0) {
  23017. ret = WC_TEST_RET_ENC_EC(ret);
  23018. goto openssl_pkey1_test_done;
  23019. }
  23020. XMEMSET(plain, 0, RSA_TEST_BYTES);
  23021. if (EVP_PKEY_decrypt(dec, plain, &outlen, cipher, outlen) != 1) {
  23022. ret = WC_TEST_RET_ENC_NC;
  23023. goto openssl_pkey1_test_done;
  23024. }
  23025. openssl_pkey1_test_done:
  23026. if (pubKey != NULL) {
  23027. EVP_PKEY_free(pubKey);
  23028. }
  23029. if (prvKey != NULL) {
  23030. EVP_PKEY_free(prvKey);
  23031. }
  23032. if (dec != NULL) {
  23033. EVP_PKEY_CTX_free(dec);
  23034. }
  23035. if (enc != NULL) {
  23036. EVP_PKEY_CTX_free(enc);
  23037. }
  23038. if (x509 != NULL) {
  23039. X509_free(x509);
  23040. }
  23041. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23042. if (tmp != NULL)
  23043. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23044. if (cipher != NULL)
  23045. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23046. if (plain != NULL)
  23047. XFREE(plain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23048. #endif
  23049. #endif
  23050. return ret;
  23051. }
  23052. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_evpSig_test(void)
  23053. {
  23054. #if !defined(NO_RSA) && !defined(NO_SHA)
  23055. byte* prvTmp;
  23056. byte* pubTmp;
  23057. int prvBytes;
  23058. int pubBytes;
  23059. RSA *prvRsa;
  23060. RSA *pubRsa;
  23061. EVP_PKEY *prvPkey;
  23062. EVP_PKEY *pubPkey;
  23063. EVP_MD_CTX* sign;
  23064. EVP_MD_CTX* verf;
  23065. char msg[] = "see spot run";
  23066. unsigned char sig[256];
  23067. unsigned int sigSz;
  23068. const void* pt;
  23069. unsigned int count;
  23070. wc_test_ret_t ret, ret1, ret2;
  23071. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  23072. XFILE keyFile;
  23073. XFILE keypubFile;
  23074. char cliKey[] = "./certs/client-key.der";
  23075. char cliKeypub[] = "./certs/client-keyPub.der";
  23076. #endif
  23077. WOLFSSL_ENTER("openssl_evpSig_test");
  23078. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23079. if (prvTmp == NULL)
  23080. return WC_TEST_RET_ENC_ERRNO;
  23081. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23082. if (pubTmp == NULL) {
  23083. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23084. return WC_TEST_RET_ENC_NC;
  23085. }
  23086. #ifdef USE_CERT_BUFFERS_1024
  23087. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  23088. prvBytes = sizeof_client_key_der_1024;
  23089. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  23090. pubBytes = sizeof_client_keypub_der_1024;
  23091. #elif defined(USE_CERT_BUFFERS_2048)
  23092. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  23093. prvBytes = sizeof_client_key_der_2048;
  23094. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  23095. pubBytes = sizeof_client_keypub_der_2048;
  23096. #else
  23097. keyFile = XFOPEN(cliKey, "rb");
  23098. if (!keyFile) {
  23099. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23100. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23101. err_sys("can't open ./certs/client-key.der, "
  23102. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  23103. return WC_TEST_RET_ENC_ERRNO;
  23104. }
  23105. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  23106. XFCLOSE(keyFile);
  23107. if (prvBytes == 0) {
  23108. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23109. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23110. return WC_TEST_RET_ENC_ERRNO;
  23111. }
  23112. keypubFile = XFOPEN(cliKeypub, "rb");
  23113. if (!keypubFile) {
  23114. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23115. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23116. err_sys("can't open ./certs/client-cert.der, "
  23117. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  23118. return WC_TEST_RET_ENC_ERRNO;
  23119. }
  23120. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  23121. XFCLOSE(keypubFile);
  23122. if (pubBytes == 0) {
  23123. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23124. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23125. return WC_TEST_RET_ENC_ERRNO;
  23126. }
  23127. #endif /* USE_CERT_BUFFERS */
  23128. prvRsa = wolfSSL_RSA_new();
  23129. pubRsa = wolfSSL_RSA_new();
  23130. if((prvRsa == NULL) || (pubRsa == NULL)){
  23131. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23132. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23133. err_sys("ERROR with RSA_new", WC_TEST_RET_ENC_NC);
  23134. return WC_TEST_RET_ENC_NC;
  23135. }
  23136. ret1 = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  23137. ret2 = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  23138. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  23139. printf("error with RSA_LoadDer_ex\n");
  23140. return WC_TEST_RET_ENC_NC;
  23141. }
  23142. prvPkey = wolfSSL_EVP_PKEY_new();
  23143. pubPkey = wolfSSL_EVP_PKEY_new();
  23144. if((prvPkey == NULL) || (pubPkey == NULL)){
  23145. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23146. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23147. printf("error with KEY_new\n");
  23148. return WC_TEST_RET_ENC_NC;
  23149. }
  23150. ret1 = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  23151. ret2 = wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  23152. if((ret1 != 1) || (ret2 != 1)){
  23153. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23154. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23155. printf("error with EVP_PKEY_set1_RSA\n");
  23156. return WC_TEST_RET_ENC_NC;
  23157. }
  23158. /****************** sign and verify *******************/
  23159. sign = EVP_MD_CTX_create();
  23160. verf = EVP_MD_CTX_create();
  23161. if((sign == NULL)||(verf == NULL)){
  23162. printf("error with EVP_MD_CTX_create\n");
  23163. EVP_MD_CTX_destroy(sign);
  23164. EVP_MD_CTX_destroy(verf);
  23165. return WC_TEST_RET_ENC_NC;
  23166. }
  23167. ret = EVP_SignInit(sign, EVP_sha1());
  23168. if (ret != SSL_SUCCESS){
  23169. printf("error with EVP_SignInit\n");
  23170. EVP_MD_CTX_destroy(sign);
  23171. EVP_MD_CTX_destroy(verf);
  23172. return WC_TEST_RET_ENC_NC;
  23173. }
  23174. count = sizeof(msg);
  23175. show("message = ", (char *)msg, count);
  23176. /* sign */
  23177. XMEMSET(sig, 0, sizeof(sig));
  23178. pt = (const void*)msg;
  23179. ret1 = EVP_SignUpdate(sign, pt, count);
  23180. ret2 = EVP_SignFinal(sign, sig, &sigSz, prvPkey);
  23181. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  23182. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23183. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23184. EVP_MD_CTX_destroy(sign);
  23185. EVP_MD_CTX_destroy(verf);
  23186. printf("error with EVP_MD_CTX_create\n");
  23187. return WC_TEST_RET_ENC_NC;
  23188. }
  23189. show("signature = ", (char *)sig, sigSz);
  23190. /* verify */
  23191. pt = (const void*)msg;
  23192. ret1 = EVP_VerifyInit(verf, EVP_sha1());
  23193. ret2 = EVP_VerifyUpdate(verf, pt, count);
  23194. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  23195. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23196. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23197. EVP_MD_CTX_destroy(sign);
  23198. EVP_MD_CTX_destroy(verf);
  23199. printf("error with EVP_Verify\n");
  23200. return WC_TEST_RET_ENC_NC;
  23201. }
  23202. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) != 1) {
  23203. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23204. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23205. EVP_MD_CTX_destroy(sign);
  23206. EVP_MD_CTX_destroy(verf);
  23207. printf("error with EVP_VerifyFinal\n");
  23208. return WC_TEST_RET_ENC_NC;
  23209. }
  23210. /* expect fail without update */
  23211. EVP_VerifyInit(verf, EVP_sha1());
  23212. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) == 1) {
  23213. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23214. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23215. EVP_MD_CTX_destroy(sign);
  23216. EVP_MD_CTX_destroy(verf);
  23217. printf("EVP_VerifyInit without update not detected\n");
  23218. return WC_TEST_RET_ENC_NC;
  23219. }
  23220. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23221. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23222. EVP_MD_CTX_destroy(sign);
  23223. EVP_MD_CTX_destroy(verf);
  23224. wolfSSL_RSA_free(prvRsa);
  23225. wolfSSL_RSA_free(pubRsa);
  23226. EVP_PKEY_free(pubPkey);
  23227. EVP_PKEY_free(prvPkey);
  23228. #endif /* NO_RSA */
  23229. return 0;
  23230. }
  23231. #endif /* OPENSSL_EXTRA */
  23232. #ifndef NO_PWDBASED
  23233. #ifdef HAVE_SCRYPT
  23234. /* Test vectors taken from RFC 7914: scrypt PBKDF - Section 12. */
  23235. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t scrypt_test(void)
  23236. {
  23237. #ifdef HAVE_FIPS
  23238. /* RFC 7914 test vector keys are too short for FIPS. */
  23239. #else
  23240. wc_test_ret_t ret;
  23241. byte derived[64];
  23242. WOLFSSL_SMALL_STACK_STATIC const byte verify1[] = {
  23243. 0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20,
  23244. 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04, 0x97,
  23245. 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8,
  23246. 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2, 0x14, 0x42,
  23247. 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8,
  23248. 0x32, 0x6a, 0x75, 0x3a, 0x0f, 0xc8, 0x1f, 0x17,
  23249. 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28,
  23250. 0xcf, 0x35, 0xe2, 0x0c, 0x38, 0xd1, 0x89, 0x06
  23251. };
  23252. #if !defined(BENCH_EMBEDDED)
  23253. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  23254. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  23255. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  23256. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  23257. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  23258. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  23259. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  23260. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  23261. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  23262. };
  23263. #endif
  23264. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  23265. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  23266. 0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48,
  23267. 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb,
  23268. 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e,
  23269. 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2,
  23270. 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf,
  23271. 0x62, 0xd4, 0x97, 0x05, 0x24, 0x2a, 0x9a, 0xf9,
  23272. 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40,
  23273. 0xdf, 0xcf, 0x01, 0x7b, 0x45, 0x57, 0x58, 0x87
  23274. };
  23275. #endif
  23276. #ifdef SCRYPT_TEST_ALL
  23277. /* Test case is very slow.
  23278. * Use for confirmation after code change or new platform.
  23279. */
  23280. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  23281. 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae,
  23282. 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81,
  23283. 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d,
  23284. 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad, 0xaa, 0x47,
  23285. 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f,
  23286. 0xfa, 0x1c, 0x6d, 0x92, 0x7c, 0x40, 0xf4, 0xc3,
  23287. 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb,
  23288. 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41, 0xa4
  23289. };
  23290. #endif
  23291. WOLFSSL_ENTER("scrypt_test");
  23292. ret = wc_scrypt(derived, NULL, 0, NULL, 0, 4, 1, 1, sizeof(verify1));
  23293. if (ret != 0)
  23294. return WC_TEST_RET_ENC_EC(ret);
  23295. if (XMEMCMP(derived, verify1, sizeof(verify1)) != 0)
  23296. return WC_TEST_RET_ENC_NC;
  23297. #if !defined(BENCH_EMBEDDED)
  23298. ret = wc_scrypt(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 10, 8, 16,
  23299. sizeof(verify2));
  23300. if (ret != 0)
  23301. return WC_TEST_RET_ENC_EC(ret);
  23302. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  23303. return WC_TEST_RET_ENC_NC;
  23304. #endif
  23305. /* Test case with parallel overflowing */
  23306. ret = wc_scrypt(derived, (byte*)"password", 16, (byte*)"NaCl", 16, 2, 4, 8388608,
  23307. sizeof(verify1));
  23308. if (ret != BAD_FUNC_ARG)
  23309. return WC_TEST_RET_ENC_EC(ret);
  23310. /* Don't run these test on embedded, since they use large mallocs */
  23311. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  23312. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  23313. (byte*)"SodiumChloride", 14, 14, 8, 1, sizeof(verify3));
  23314. if (ret != 0)
  23315. return WC_TEST_RET_ENC_EC(ret);
  23316. if (XMEMCMP(derived, verify3, sizeof(verify3)) != 0)
  23317. return WC_TEST_RET_ENC_NC;
  23318. #ifdef SCRYPT_TEST_ALL
  23319. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  23320. (byte*)"SodiumChloride", 14, 20, 8, 1, sizeof(verify4));
  23321. if (ret != 0)
  23322. return WC_TEST_RET_ENC_EC(ret);
  23323. if (XMEMCMP(derived, verify4, sizeof(verify4)) != 0)
  23324. return WC_TEST_RET_ENC_NC;
  23325. #endif
  23326. #else
  23327. #ifdef SCRYPT_TEST_ALL
  23328. (void)verify4;
  23329. #endif
  23330. #endif /* !BENCH_EMBEDDED && !defined(WOLFSSL_LINUXKM) && !HAVE_INTEL_QA */
  23331. #if !defined(BENCH_EMBEDDED)
  23332. ret = wc_scrypt_ex(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 1<<10,
  23333. 8, 16, sizeof(verify2));
  23334. if (ret != 0)
  23335. return WC_TEST_RET_ENC_EC(ret);
  23336. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  23337. return WC_TEST_RET_ENC_NC;
  23338. #endif
  23339. #endif /* !HAVE_FIPS */
  23340. return 0;
  23341. }
  23342. #endif
  23343. #ifdef HAVE_PKCS12
  23344. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_pbkdf_test(void)
  23345. {
  23346. WOLFSSL_SMALL_STACK_STATIC const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
  23347. 0x00, 0x00 };
  23348. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
  23349. WOLFSSL_SMALL_STACK_STATIC const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
  23350. 0x00, 0x67, 0x00, 0x00 };
  23351. WOLFSSL_SMALL_STACK_STATIC const byte salt2[] = { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
  23352. byte derived[64];
  23353. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  23354. 0x27, 0xE9, 0x0D, 0x7E, 0xD5, 0xA1, 0xC4, 0x11,
  23355. 0xBA, 0x87, 0x8B, 0xC0, 0x90, 0xF5, 0xCE, 0xBE,
  23356. 0x5E, 0x9D, 0x5F, 0xE3, 0xD6, 0x2B, 0x73, 0xAA
  23357. };
  23358. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  23359. 0x90, 0x1B, 0x49, 0x70, 0xF0, 0x94, 0xF0, 0xF8,
  23360. 0x45, 0xC0, 0xF3, 0xF3, 0x13, 0x59, 0x18, 0x6A,
  23361. 0x35, 0xE3, 0x67, 0xFE, 0xD3, 0x21, 0xFD, 0x7C
  23362. };
  23363. int id = 1;
  23364. int kLen = 24;
  23365. int iterations = 1;
  23366. wc_test_ret_t ret;
  23367. WOLFSSL_ENTER("pkcs12_pbkdf_test");
  23368. ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
  23369. iterations, kLen, WC_SHA256, id);
  23370. if (ret < 0)
  23371. return WC_TEST_RET_ENC_EC(ret);
  23372. if (XMEMCMP(derived, verify, kLen) != 0)
  23373. return WC_TEST_RET_ENC_NC;
  23374. iterations = 1000;
  23375. ret = wc_PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8,
  23376. iterations, kLen, WC_SHA256, id);
  23377. if (ret < 0)
  23378. return WC_TEST_RET_ENC_EC(ret);
  23379. ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8,
  23380. iterations, kLen, WC_SHA256, id, HEAP_HINT);
  23381. if (ret < 0)
  23382. return WC_TEST_RET_ENC_EC(ret);
  23383. if (XMEMCMP(derived, verify2, 24) != 0)
  23384. return WC_TEST_RET_ENC_NC;
  23385. return 0;
  23386. }
  23387. #endif /* HAVE_PKCS12 */
  23388. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  23389. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf2_test(void)
  23390. {
  23391. char passwd[] = "passwordpassword";
  23392. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a,
  23393. 0x5d, 0x63, 0xcb, 0x06 };
  23394. int iterations = 2048;
  23395. int kLen = 24;
  23396. byte derived[64];
  23397. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  23398. 0x43, 0x6d, 0xb5, 0xe8, 0xd0, 0xfb, 0x3f, 0x35, 0x42, 0x48, 0x39, 0xbc,
  23399. 0x2d, 0xd4, 0xf9, 0x37, 0xd4, 0x95, 0x16, 0xa7, 0x2a, 0x9a, 0x21, 0xd1
  23400. };
  23401. wc_test_ret_t ret;
  23402. WOLFSSL_ENTER("pbkdf2_test");
  23403. ret = wc_PBKDF2_ex(derived, (byte*)passwd, (int)XSTRLEN(passwd),
  23404. salt, (int)sizeof(salt), iterations,
  23405. kLen, WC_SHA256, HEAP_HINT, devId);
  23406. if (ret != 0)
  23407. return WC_TEST_RET_ENC_EC(ret);
  23408. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  23409. return WC_TEST_RET_ENC_NC;
  23410. return 0;
  23411. }
  23412. #endif /* HAVE_PBKDF2 && !NO_SHA256 && !NO_HMAC */
  23413. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  23414. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf1_test(void)
  23415. {
  23416. char passwd[] = "password";
  23417. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  23418. int iterations = 1000;
  23419. int kLen = 16;
  23420. byte derived[16];
  23421. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  23422. 0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F,
  23423. 0xAF, 0x10, 0xEB, 0xFB, 0x4A, 0x3D, 0x2A, 0x20
  23424. };
  23425. wc_test_ret_t ret;
  23426. WOLFSSL_ENTER("pbkdf1_test");
  23427. ret = wc_PBKDF1_ex(derived, kLen, NULL, 0, (byte*)passwd,
  23428. (int)XSTRLEN(passwd), salt, (int)sizeof(salt), iterations, WC_SHA,
  23429. HEAP_HINT);
  23430. if (ret != 0)
  23431. return ret;
  23432. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  23433. return WC_TEST_RET_ENC_NC;
  23434. return 0;
  23435. }
  23436. #endif /* HAVE_PBKDF2 && !NO_SHA */
  23437. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pwdbased_test(void)
  23438. {
  23439. wc_test_ret_t ret = 0;
  23440. WOLFSSL_ENTER("pwdbased_test");
  23441. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  23442. ret = pbkdf1_test();
  23443. if (ret != 0)
  23444. return ret;
  23445. #endif
  23446. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  23447. ret = pbkdf2_test();
  23448. if (ret != 0)
  23449. return ret;
  23450. #endif
  23451. #ifdef HAVE_PKCS12
  23452. ret = pkcs12_pbkdf_test();
  23453. if (ret != 0)
  23454. return ret;
  23455. #endif
  23456. #ifdef HAVE_SCRYPT
  23457. ret = scrypt_test();
  23458. if (ret != 0)
  23459. return ret;
  23460. #endif
  23461. return ret;
  23462. }
  23463. #endif /* NO_PWDBASED */
  23464. #if defined(USE_CERT_BUFFERS_2048) && \
  23465. defined(HAVE_PKCS12) && \
  23466. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_HMAC) && \
  23467. !defined(NO_CERTS) && !defined(NO_DES3)
  23468. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_test(void)
  23469. {
  23470. wc_test_ret_t ret = 0;
  23471. WC_PKCS12* pkcs12 = NULL;
  23472. /* Gen vars */
  23473. byte* pkcs12der = NULL;
  23474. int pkcs12derSz = 0;
  23475. WC_DerCertList derCaList = {
  23476. (byte*)ca_cert_der_2048, sizeof_ca_cert_der_2048, NULL
  23477. };
  23478. char* pass = (char*)"wolfSSL test";
  23479. /* Parsing vars */
  23480. WC_DerCertList* derCaListOut = NULL;
  23481. byte* keyDer = NULL;
  23482. byte* certDer = NULL;
  23483. word32 keySz;
  23484. word32 certSz;
  23485. WOLFSSL_ENTER("pkcs12_test");
  23486. pkcs12 = wc_PKCS12_create(pass, (word32)XSTRLEN(pass),
  23487. (char*)"friendlyName" /* not used currently */,
  23488. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  23489. (byte*)server_cert_der_2048, sizeof_server_cert_der_2048,
  23490. &derCaList, PBE_SHA1_DES3, PBE_SHA1_DES3, 100, 100,
  23491. 0 /* not used currently */, HEAP_HINT);
  23492. if (pkcs12 == NULL) {
  23493. ret = WC_TEST_RET_ENC_EC(MEMORY_E);
  23494. goto out;
  23495. }
  23496. ret = wc_i2d_PKCS12(pkcs12, NULL, &pkcs12derSz);
  23497. if (ret != LENGTH_ONLY_E) {
  23498. if (ret == 0)
  23499. ret = WC_TEST_RET_ENC_NC;
  23500. else
  23501. ret = WC_TEST_RET_ENC_I(ret);
  23502. goto out;
  23503. }
  23504. pkcs12der = (byte*)XMALLOC(pkcs12derSz, HEAP_HINT, DYNAMIC_TYPE_PKCS);
  23505. if (pkcs12der == NULL) {
  23506. ret = WC_TEST_RET_ENC_EC(MEMORY_E);
  23507. goto out;
  23508. }
  23509. {
  23510. /* Use tmp pointer to avoid advancing pkcs12der */
  23511. byte* tmp = pkcs12der;
  23512. ret = wc_i2d_PKCS12(pkcs12, &tmp, &pkcs12derSz);
  23513. if (ret <= 0) {
  23514. if (ret == 0)
  23515. ret = WC_TEST_RET_ENC_NC;
  23516. else
  23517. ret = WC_TEST_RET_ENC_I(ret);
  23518. goto out;
  23519. }
  23520. }
  23521. wc_PKCS12_free(pkcs12);
  23522. pkcs12 = wc_PKCS12_new_ex(HEAP_HINT);
  23523. if (pkcs12 == NULL) {
  23524. ret = WC_TEST_RET_ENC_EC(MEMORY_E);
  23525. goto out;
  23526. }
  23527. /* convert the DER file into an internal structure */
  23528. ret = wc_d2i_PKCS12(pkcs12der, pkcs12derSz, pkcs12);
  23529. if (ret != 0) {
  23530. ret = WC_TEST_RET_ENC_EC(ret);
  23531. goto out;
  23532. }
  23533. /* parse the internal structure into its parts */
  23534. ret = wc_PKCS12_parse(pkcs12, "wolfSSL test", &keyDer, &keySz,
  23535. &certDer, &certSz, &derCaListOut);
  23536. if (ret != 0) {
  23537. ret = WC_TEST_RET_ENC_EC(ret);
  23538. goto out;
  23539. }
  23540. if (keyDer == NULL || certDer == NULL || derCaListOut == NULL) {
  23541. ret = WC_TEST_RET_ENC_NC;
  23542. goto out;
  23543. }
  23544. out:
  23545. if (derCaListOut)
  23546. wc_FreeCertList(derCaListOut, HEAP_HINT);
  23547. XFREE(keyDer, HEAP_HINT, DYNAMIC_TYPE_PKCS);
  23548. XFREE(certDer, HEAP_HINT, DYNAMIC_TYPE_PKCS);
  23549. if (pkcs12)
  23550. wc_PKCS12_free(pkcs12);
  23551. XFREE(pkcs12der, HEAP_HINT, DYNAMIC_TYPE_PKCS);
  23552. return ret;
  23553. }
  23554. #endif
  23555. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  23556. #if defined(WOLFSSL_AFALG_XILINX) || defined(WOLFSSL_AFALG_XILINX_AES) || \
  23557. defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_AFALG_HASH_KEEP) || \
  23558. defined(WOLFSSL_AFALG_XILINX_RSA)
  23559. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  23560. static wc_test_ret_t hkdf_test(void)
  23561. #else
  23562. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hkdf_test(void)
  23563. #endif
  23564. {
  23565. wc_test_ret_t ret = 0;
  23566. #if !defined(NO_SHA) || !defined(NO_SHA256)
  23567. int L;
  23568. byte okm1[42];
  23569. byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  23570. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  23571. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  23572. #ifndef HAVE_FIPS
  23573. byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23574. 0x08, 0x09, 0x0a, 0x0b, 0x0c };
  23575. byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  23576. 0xf8, 0xf9 };
  23577. #endif
  23578. #ifndef NO_SHA
  23579. byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
  23580. 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
  23581. 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
  23582. 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
  23583. 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
  23584. 0x49, 0x18 };
  23585. #ifndef HAVE_FIPS
  23586. byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
  23587. 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
  23588. 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
  23589. 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
  23590. 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
  23591. 0xf8, 0x96 };
  23592. #endif
  23593. #endif /* !NO_SHA */
  23594. #ifndef NO_SHA256
  23595. byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  23596. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  23597. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  23598. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  23599. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  23600. 0x96, 0xc8 };
  23601. #ifndef HAVE_FIPS
  23602. byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
  23603. 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  23604. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  23605. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  23606. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
  23607. 0x58, 0x65 };
  23608. #endif
  23609. #endif /* !NO_SHA256 */
  23610. WOLFSSL_ENTER("hkdf_test");
  23611. XMEMSET(okm1, 0, sizeof(okm1));
  23612. L = (int)sizeof(okm1);
  23613. #ifndef NO_SHA
  23614. ret = wc_HKDF(WC_SHA, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  23615. okm1, (word32)L);
  23616. if (ret != 0)
  23617. return WC_TEST_RET_ENC_EC(ret);
  23618. if (XMEMCMP(okm1, res1, L) != 0)
  23619. return WC_TEST_RET_ENC_NC;
  23620. #ifndef HAVE_FIPS
  23621. /* fips can't have key size under 14 bytes, salt is key too */
  23622. L = (int)sizeof(okm1);
  23623. ret = wc_HKDF(WC_SHA, ikm1, 11, salt1, (word32)sizeof(salt1),
  23624. info1, (word32)sizeof(info1), okm1, (word32)L);
  23625. if (ret != 0)
  23626. return WC_TEST_RET_ENC_EC(ret);
  23627. if (XMEMCMP(okm1, res2, L) != 0)
  23628. return WC_TEST_RET_ENC_NC;
  23629. #endif /* HAVE_FIPS */
  23630. #endif /* !NO_SHA */
  23631. #ifndef NO_SHA256
  23632. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  23633. okm1, (word32)L);
  23634. if (ret != 0)
  23635. return WC_TEST_RET_ENC_EC(ret);
  23636. if (XMEMCMP(okm1, res3, L) != 0)
  23637. return WC_TEST_RET_ENC_NC;
  23638. #ifndef HAVE_FIPS
  23639. /* fips can't have key size under 14 bytes, salt is key too */
  23640. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1),
  23641. salt1, (word32)sizeof(salt1), info1, (word32)sizeof(info1), okm1, (word32)L);
  23642. if (ret != 0)
  23643. return WC_TEST_RET_ENC_EC(ret);
  23644. if (XMEMCMP(okm1, res4, L) != 0)
  23645. return WC_TEST_RET_ENC_NC;
  23646. #endif /* HAVE_FIPS */
  23647. #endif /* !NO_SHA256 */
  23648. #endif /* !NO_SHA || !NO_SHA256 */
  23649. return ret;
  23650. }
  23651. #endif /* HAVE_HKDF */
  23652. #ifdef WOLFSSL_WOLFSSH
  23653. typedef struct {
  23654. byte hashId;
  23655. byte keyId;
  23656. const byte* k;
  23657. word32 kSz;
  23658. const byte* h;
  23659. word32 hSz;
  23660. const byte* sessionId;
  23661. word32 sessionIdSz;
  23662. const byte* expectedKey;
  23663. word32 expectedKeySz;
  23664. } SshKdfTestVector;
  23665. /** Test Vector Set #3: SHA-256 **/
  23666. static const byte sshKdfTvSet3k[] = {
  23667. 0x6A, 0xC3, 0x82, 0xEA, 0xAC, 0xA0, 0x93, 0xE1,
  23668. 0x25, 0xE2, 0x5C, 0x24, 0xBE, 0xBC, 0x84, 0x64,
  23669. 0x0C, 0x11, 0x98, 0x75, 0x07, 0x34, 0x4B, 0x5C,
  23670. 0x73, 0x9C, 0xEB, 0x84, 0xA9, 0xE0, 0xB2, 0x22,
  23671. 0xB9, 0xA8, 0xB5, 0x1C, 0x83, 0x9E, 0x5E, 0xBE,
  23672. 0x49, 0xCF, 0xAD, 0xBF, 0xB3, 0x95, 0x99, 0x76,
  23673. 0x4E, 0xD5, 0x22, 0x09, 0x9D, 0xC9, 0x12, 0x75,
  23674. 0x19, 0x50, 0xDC, 0x7D, 0xC9, 0x7F, 0xBD, 0xC0,
  23675. 0x63, 0x28, 0xB6, 0x8F, 0x22, 0x78, 0x1F, 0xD3,
  23676. 0x15, 0xAF, 0x56, 0x80, 0x09, 0xA5, 0x50, 0x9E,
  23677. 0x5B, 0x87, 0xA1, 0x1B, 0xF5, 0x27, 0xC0, 0x56,
  23678. 0xDA, 0xFF, 0xD8, 0x2A, 0xB6, 0xCB, 0xC2, 0x5C,
  23679. 0xCA, 0x37, 0x14, 0x34, 0x59, 0xE7, 0xBC, 0x63,
  23680. 0xBC, 0xDE, 0x52, 0x75, 0x7A, 0xDE, 0xB7, 0xDF,
  23681. 0x01, 0xCF, 0x12, 0x17, 0x3F, 0x1F, 0xEF, 0x81,
  23682. 0x02, 0xEC, 0x5A, 0xB1, 0x42, 0xC2, 0x13, 0xDD,
  23683. 0x9D, 0x30, 0x69, 0x62, 0x78, 0xA8, 0xD8, 0xBC,
  23684. 0x32, 0xDD, 0xE9, 0x59, 0x2D, 0x28, 0xC0, 0x78,
  23685. 0xC6, 0xD9, 0x2B, 0x94, 0x7D, 0x82, 0x5A, 0xCA,
  23686. 0xAB, 0x64, 0x94, 0x84, 0x6A, 0x49, 0xDE, 0x24,
  23687. 0xB9, 0x62, 0x3F, 0x48, 0x89, 0xE8, 0xAD, 0xC3,
  23688. 0x8E, 0x8C, 0x66, 0x9E, 0xFF, 0xEF, 0x17, 0x60,
  23689. 0x40, 0xAD, 0x94, 0x5E, 0x90, 0xA7, 0xD3, 0xEE,
  23690. 0xC1, 0x5E, 0xFE, 0xEE, 0x78, 0xAE, 0x71, 0x04,
  23691. 0x3C, 0x96, 0x51, 0x11, 0x03, 0xA1, 0x6B, 0xA7,
  23692. 0xCA, 0xF0, 0xAC, 0xD0, 0x64, 0x2E, 0xFD, 0xBE,
  23693. 0x80, 0x99, 0x34, 0xFA, 0xA1, 0xA5, 0xF1, 0xBD,
  23694. 0x11, 0x04, 0x36, 0x49, 0xB2, 0x5C, 0xCD, 0x1F,
  23695. 0xEE, 0x2E, 0x38, 0x81, 0x5D, 0x4D, 0x5F, 0x5F,
  23696. 0xC6, 0xB4, 0x10, 0x29, 0x69, 0xF2, 0x1C, 0x22,
  23697. 0xAE, 0x1B, 0x0E, 0x7D, 0x36, 0x03, 0xA5, 0x56,
  23698. 0xA1, 0x32, 0x62, 0xFF, 0x62, 0x8D, 0xE2, 0x22
  23699. };
  23700. static const byte sshKdfTvSet3h[] = {
  23701. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  23702. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  23703. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  23704. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  23705. };
  23706. static const byte sshKdfTvSet3sid[] = {
  23707. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  23708. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  23709. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  23710. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  23711. };
  23712. static const byte sshKdfTvSet3a[] = {
  23713. 0x81, 0xF0, 0x33, 0x0E, 0xF6, 0xF0, 0x53, 0x61,
  23714. 0xB3, 0x82, 0x3B, 0xFD, 0xED, 0x6E, 0x1D, 0xE9
  23715. };
  23716. static const byte sshKdfTvSet3b[] = {
  23717. 0x3F, 0x6F, 0xD2, 0x06, 0x5E, 0xEB, 0x2B, 0x0B,
  23718. 0x1D, 0x93, 0x19, 0x5A, 0x1F, 0xED, 0x48, 0xA5
  23719. };
  23720. static const byte sshKdfTvSet3c[] = {
  23721. 0xC3, 0x54, 0x71, 0x03, 0x4E, 0x6F, 0xD6, 0x54,
  23722. 0x76, 0x13, 0x17, 0x8E, 0x23, 0x43, 0x5F, 0x21
  23723. };
  23724. static const byte sshKdfTvSet3d[] = {
  23725. 0x7E, 0x9D, 0x79, 0x03, 0x20, 0x90, 0xD9, 0x9F,
  23726. 0x98, 0xB0, 0x15, 0x63, 0x4D, 0xD9, 0xF4, 0x62
  23727. };
  23728. static const byte sshKdfTvSet3e[] = {
  23729. 0x24, 0xEE, 0x55, 0x9A, 0xD7, 0xCE, 0x71, 0x2B,
  23730. 0x68, 0x5D, 0x0B, 0x22, 0x71, 0xE4, 0x43, 0xC1,
  23731. 0x7A, 0xB1, 0xD1, 0xDC, 0xEB, 0x5A, 0x36, 0x05,
  23732. 0x69, 0xD2, 0x5D, 0x5D, 0xC2, 0x43, 0x00, 0x2F
  23733. };
  23734. static const byte sshKdfTvSet3f[] = {
  23735. 0xC3, 0x41, 0x9C, 0x2B, 0x96, 0x62, 0x35, 0x86,
  23736. 0x9D, 0x71, 0x4B, 0xA5, 0xAC, 0x48, 0xDD, 0xB7,
  23737. 0xD9, 0xE3, 0x5C, 0x8C, 0x19, 0xAA, 0xC7, 0x34,
  23738. 0x22, 0x33, 0x7A, 0x37, 0x34, 0x53, 0x60, 0x7E
  23739. };
  23740. static const SshKdfTestVector sshKdfTestVectors[] = {
  23741. {WC_HASH_TYPE_SHA256, 'A',
  23742. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  23743. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  23744. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  23745. sshKdfTvSet3a, sizeof(sshKdfTvSet3a)},
  23746. {WC_HASH_TYPE_SHA256, 'B',
  23747. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  23748. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  23749. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  23750. sshKdfTvSet3b, sizeof(sshKdfTvSet3b)},
  23751. {WC_HASH_TYPE_SHA256, 'C',
  23752. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  23753. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  23754. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  23755. sshKdfTvSet3c, sizeof(sshKdfTvSet3c)},
  23756. {WC_HASH_TYPE_SHA256, 'D',
  23757. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  23758. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  23759. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  23760. sshKdfTvSet3d, sizeof(sshKdfTvSet3d)},
  23761. {WC_HASH_TYPE_SHA256, 'E',
  23762. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  23763. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  23764. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  23765. sshKdfTvSet3e, sizeof(sshKdfTvSet3e)},
  23766. {WC_HASH_TYPE_SHA256, 'F',
  23767. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  23768. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  23769. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  23770. sshKdfTvSet3f, sizeof(sshKdfTvSet3f)},
  23771. };
  23772. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sshkdf_test(void)
  23773. {
  23774. byte cKey[32]; /* Greater of SHA256_DIGEST_SIZE and AES_BLOCK_SIZE */
  23775. word32 i;
  23776. word32 tc = sizeof(sshKdfTestVectors)/sizeof(SshKdfTestVector);
  23777. const SshKdfTestVector* tv = NULL;
  23778. wc_test_ret_t result = 0;
  23779. WOLFSSL_ENTER("sshkdf_test");
  23780. /* sId - Session ID, eKey - Expected Key, cKey - Calculated Key */
  23781. for (i = 0, tv = sshKdfTestVectors; i < tc; i++, tv++) {
  23782. result = wc_SSH_KDF(tv->hashId, tv->keyId,
  23783. cKey, tv->expectedKeySz,
  23784. tv->k, tv->kSz, tv->h, tv->hSz,
  23785. tv->sessionId, tv->sessionIdSz);
  23786. if (result != 0) {
  23787. printf("KDF: Could not derive key.\n");
  23788. result = WC_TEST_RET_ENC_EC(result);
  23789. }
  23790. else {
  23791. if (XMEMCMP(cKey, tv->expectedKey, tv->expectedKeySz) != 0) {
  23792. printf("KDF: Calculated Key does not match Expected Key.\n");
  23793. result = WC_TEST_RET_ENC_EC(result);
  23794. }
  23795. }
  23796. if (result != 0) break;
  23797. }
  23798. return result;
  23799. }
  23800. #endif /* WOLFSSL_WOLFSSH */
  23801. #if defined(WOLFSSL_HAVE_PRF) && !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  23802. #define DIGL 12
  23803. #define SECL 48
  23804. #define LBSL 63
  23805. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t prf_test(void)
  23806. {
  23807. int ret;
  23808. byte dig[DIGL] = {0};
  23809. byte secret[SECL] = {
  23810. 0x10, 0xbc, 0xb4, 0xa2, 0xe8, 0xdc, 0xf1, 0x9b, 0x4c,
  23811. 0x51, 0x9c, 0xed, 0x31, 0x1b, 0x51, 0x57, 0x02, 0x3f,
  23812. 0xa1, 0x7d, 0xfb, 0x0e, 0xf3, 0x4e, 0x8f, 0x6f, 0x71,
  23813. 0xa3, 0x67, 0x76, 0x6b, 0xfa, 0x5d, 0x46, 0x4a, 0xe8,
  23814. 0x61, 0x18, 0x81, 0xc4, 0x66, 0xcc, 0x6f, 0x09, 0x99,
  23815. 0x9d, 0xfc, 0x47
  23816. };
  23817. byte lablSd[LBSL] = {
  23818. 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x66, 0x69,
  23819. 0x6e, 0x69, 0x73, 0x68, 0x65, 0x64, 0x0b, 0x46, 0xba,
  23820. 0x56, 0xbf, 0x1f, 0x5d, 0x99, 0xff, 0xe9, 0xbb, 0x43,
  23821. 0x01, 0xe7, 0xca, 0x2c, 0x00, 0xdf, 0x9a, 0x39, 0x6e,
  23822. 0xcf, 0x6d, 0x15, 0x27, 0x4d, 0xf2, 0x93, 0x96, 0x4a,
  23823. 0x91, 0xde, 0x5c, 0xc0, 0x47, 0x7c, 0xa8, 0xae, 0xcf,
  23824. 0x5d, 0x93, 0x5f, 0x4c, 0x92, 0xcc, 0x98, 0x5b, 0x43
  23825. };
  23826. byte expected[DIGL] = {
  23827. 0xee, 0xcb, 0xb1, 0x30, 0xf2, 0xcd, 0xb3, 0x4a,
  23828. 0xbe, 0xda, 0xc1, 0xf6
  23829. };
  23830. int digL = DIGL;
  23831. int secL = SECL;
  23832. int lblsdL = LBSL;
  23833. int hash_type = sha384_mac;
  23834. ret = wc_PRF(dig, (word32)digL, secret, secL, lablSd, lblsdL, hash_type,
  23835. HEAP_HINT, INVALID_DEVID);
  23836. if (ret != 0) {
  23837. printf("Failed w/ code: %d\n", ret);
  23838. return WC_TEST_RET_ENC_EC(ret);
  23839. }
  23840. if (XMEMCMP(expected, dig, DIGL) != 0) {
  23841. printf("Got unexpected digest\n");
  23842. return WC_TEST_RET_ENC_NC;
  23843. }
  23844. return 0;
  23845. }
  23846. #endif /* WOLFSSL_HAVE_PRF && !NO_HMAC */
  23847. #ifdef WOLFSSL_HAVE_PRF
  23848. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  23849. #ifdef WOLFSSL_BASE16
  23850. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t tls12_kdf_test(void)
  23851. {
  23852. const char* preMasterSecret = "D06F9C19BFF49B1E91E4EFE97345D089"
  23853. "4E6C2E6C34A165B24540E2970875D641"
  23854. "2AA6515871B389B4C199BB8389C71CED";
  23855. const char* helloRandom = "162B81EDFBEAE4F25240320B87E7651C"
  23856. "865564191DD782DB0B9ECA275FBA1BB9"
  23857. "5A1DA3DF436D68DA86C5E7B4B4A36E46"
  23858. "B977C61767983A31BE270D74517BD0F6";
  23859. const char* masterSecret = "EB38B8D89B98B1C266DE44BB3CA14E83"
  23860. "C32F009F9955B1D994E61D3C51EE8760"
  23861. "90B4EF89CC7AF42F46E72201BFCC7977";
  23862. const char* label = "master secret";
  23863. byte pms[48] = {0};
  23864. byte seed[64] = {0};
  23865. byte ms[48] = {0};
  23866. byte result[48] = {0};
  23867. word32 pmsSz = (word32)sizeof(pms);
  23868. word32 seedSz = (word32)sizeof(seed);
  23869. word32 msSz = (word32)sizeof(ms);
  23870. int ret;
  23871. ret = Base16_Decode((const byte*)preMasterSecret,
  23872. (word32)XSTRLEN(preMasterSecret), pms, &pmsSz);
  23873. if (ret != 0)
  23874. return ret;
  23875. ret = Base16_Decode((const byte*)helloRandom,
  23876. (word32)XSTRLEN(helloRandom), seed, &seedSz);
  23877. if (ret != 0)
  23878. return ret;
  23879. ret = Base16_Decode((const byte*)masterSecret,
  23880. (word32)XSTRLEN(masterSecret), ms, &msSz);
  23881. if (ret != 0)
  23882. return ret;
  23883. ret = wc_PRF_TLS(result, msSz, pms, pmsSz,
  23884. (const byte*)label, (word32)XSTRLEN(label), seed, seedSz,
  23885. 1, sha256_mac, NULL, INVALID_DEVID);
  23886. if (ret != 0) {
  23887. if (ret == FIPS_PRIVATE_KEY_LOCKED_E) {
  23888. printf(" wc_PRF_TLSv12: Private key locked.\n");
  23889. }
  23890. return WC_TEST_RET_ENC_NC;
  23891. }
  23892. if (XMEMCMP(result, ms, msSz) != 0)
  23893. return WC_TEST_RET_ENC_NC;
  23894. return 0;
  23895. }
  23896. #endif /* WOLFSSL_BASE16 */
  23897. #endif /* WOLFSSL_HAVE_HKDF && !NO_HMAC */
  23898. #endif /* WOLFSSL_HAVE_PRF */
  23899. #ifdef WOLFSSL_TLS13
  23900. #define TLSV13_PSK_DHE_SZ 40
  23901. typedef struct {
  23902. enum wc_HashType hashAlg;
  23903. word32 pskSz;
  23904. word32 dheSz;
  23905. byte psk[TLSV13_PSK_DHE_SZ];
  23906. byte dhe[TLSV13_PSK_DHE_SZ];
  23907. byte hashHello1[WC_MAX_DIGEST_SIZE];
  23908. byte hashHello2[WC_MAX_DIGEST_SIZE];
  23909. byte hashFinished1[WC_MAX_DIGEST_SIZE];
  23910. byte hashFinished2[WC_MAX_DIGEST_SIZE];
  23911. /* Expected */
  23912. byte clientEarlyTrafficSecret[WC_MAX_DIGEST_SIZE];
  23913. byte earlyExporterMasterSecret[WC_MAX_DIGEST_SIZE];
  23914. byte clientHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  23915. byte serverHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  23916. byte clientApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  23917. byte serverApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  23918. byte exporterMasterSecret[WC_MAX_DIGEST_SIZE];
  23919. byte resumptionMasterSecret[WC_MAX_DIGEST_SIZE];
  23920. } Tls13KdfTestVector;
  23921. /* The following tests come from the CAVP test vectors we used for
  23922. * our FIPS validation. The hash values used are the components from
  23923. * the test hashed together. hashHello1 is the hash of the
  23924. * clientHelloRandom value of the test vector. hashHello2 is the hash
  23925. * of the clientHelloRandom and serverHelloRandom values from the test
  23926. * vector. hashFinished1 is clientHelloRandom, serverHelloRandom, and
  23927. * serverFinishedRandom. hashFinished2 is clientHelloRandom,
  23928. * serverHelloRandom, serverFinishedRandom, and clietnFinishedRandom
  23929. * hashed together. */
  23930. static const Tls13KdfTestVector tls13KdfTestVectors[] = {
  23931. { /* 1 */
  23932. WC_HASH_TYPE_SHA256, 35, 35,
  23933. { /* PSK */
  23934. 0x7b, 0xf1, 0x05, 0x31, 0x36, 0xfa, 0x03, 0xdc,
  23935. 0x31, 0x97, 0x88, 0x04, 0x9c, 0xbc, 0xee, 0xf7,
  23936. 0x8d, 0x84, 0x95, 0x26, 0xaf, 0x1d, 0x68, 0xb0,
  23937. 0x60, 0x7a, 0xcc, 0x4f, 0xc1, 0xd3, 0xa1, 0x68,
  23938. 0x7f, 0x6d, 0xbe
  23939. },
  23940. { /* DHE */
  23941. 0x6e, 0xa1, 0x77, 0xab, 0x2f, 0x43, 0xd2, 0x4b,
  23942. 0xe5, 0xa1, 0x09, 0xe0, 0x7a, 0xd0, 0x01, 0x35,
  23943. 0x8d, 0xf8, 0xf2, 0x5c, 0x91, 0x02, 0xb0, 0x6c,
  23944. 0x3f, 0xeb, 0xee, 0xa4, 0x42, 0x19, 0xce, 0xdc,
  23945. 0x81, 0x26, 0x40
  23946. },
  23947. { /* Hello 1 */
  23948. 0xd9, 0x4b, 0xe4, 0x17, 0xef, 0x58, 0x73, 0x7d,
  23949. 0x28, 0x3d, 0xf0, 0xcc, 0x05, 0x03, 0xaf, 0xac,
  23950. 0x3d, 0x92, 0x79, 0x48, 0xe8, 0x8c, 0xdb, 0xce,
  23951. 0x95, 0x82, 0x21, 0x31, 0x7b, 0x61, 0xd7, 0xc6
  23952. },
  23953. { /* Hello 2 */
  23954. 0xb7, 0x7f, 0x29, 0x91, 0xa4, 0x8b, 0x34, 0xdb,
  23955. 0xbd, 0xc7, 0x54, 0x1c, 0x3b, 0x86, 0xa3, 0x69,
  23956. 0xfe, 0x26, 0xe4, 0x7b, 0xac, 0x57, 0x71, 0xb3,
  23957. 0x32, 0x97, 0xed, 0xd2, 0x0e, 0x95, 0xb8, 0x63
  23958. },
  23959. { /* Finished 1 */
  23960. 0x65, 0xdb, 0x6d, 0x71, 0x71, 0xd0, 0xd8, 0x49,
  23961. 0xd0, 0x3c, 0x8e, 0x2b, 0x24, 0xdf, 0xc2, 0xe9,
  23962. 0xd6, 0xfd, 0xea, 0x04, 0x95, 0x7c, 0xf0, 0x7e,
  23963. 0x57, 0x74, 0x7c, 0xdd, 0xa3, 0x0b, 0x2b, 0x36
  23964. },
  23965. { /* Finished 2 */
  23966. 0x28, 0xf2, 0xf2, 0x79, 0xcf, 0x20, 0x52, 0x90,
  23967. 0x1d, 0x91, 0x05, 0xad, 0x44, 0x26, 0x23, 0x96,
  23968. 0x32, 0xce, 0xec, 0x61, 0xd1, 0xbf, 0x00, 0x48,
  23969. 0x4a, 0xa5, 0x60, 0xcc, 0x28, 0xb5, 0x8d, 0x98
  23970. },
  23971. { /* Client Early Traffic Secret */
  23972. 0x07, 0x14, 0x6a, 0x26, 0x5b, 0x6c, 0x7f, 0x4d, 0x6b, 0x47, 0x3f, 0xd5,
  23973. 0x03, 0x1d, 0xd2, 0x23, 0x3d, 0x89, 0x3e, 0xc6, 0x51, 0xd1, 0xac, 0xf8,
  23974. 0x28, 0xae, 0x4b, 0x76, 0xc8, 0x10, 0x7e, 0xdd
  23975. },
  23976. { /* Early Exporter Master Secret */
  23977. 0xb8, 0xd3, 0x25, 0x7e, 0x2d, 0x41, 0x7b, 0xcb, 0x5e, 0x82, 0x49, 0xf5,
  23978. 0x51, 0x3d, 0xb7, 0x59, 0x32, 0xb3, 0xdf, 0x99, 0x4e, 0x04, 0x69, 0xc6,
  23979. 0x96, 0x8e, 0xe6, 0x3d, 0x91, 0xe4, 0x81, 0x11
  23980. },
  23981. { /* Client Handshake Traffic Secret */
  23982. 0xd9, 0x3b, 0x54, 0xe2, 0xb0, 0xd1, 0x85, 0xf0, 0xfd, 0xf3, 0x48, 0x4a,
  23983. 0xf8, 0x0b, 0xa5, 0xdc, 0x4c, 0x37, 0xcb, 0xd4, 0x20, 0xaf, 0x60, 0xc7,
  23984. 0xd5, 0x50, 0x5d, 0x0c, 0x77, 0x3b, 0x6f, 0xd2
  23985. },
  23986. { /* Server Handshake Traffic Secret */
  23987. 0x4d, 0x40, 0x2b, 0xd2, 0x8c, 0x33, 0x90, 0x39, 0x67, 0x67, 0x05, 0xf7,
  23988. 0x5d, 0x37, 0x1e, 0xdc, 0x4a, 0x70, 0x6b, 0x9e, 0xf8, 0x06, 0x61, 0x89,
  23989. 0x70, 0xe1, 0x3d, 0x36, 0xad, 0x88, 0x7e, 0x5b
  23990. },
  23991. { /* Client Application Traffic Secret */
  23992. 0x74, 0x6e, 0xa0, 0x13, 0x18, 0x34, 0x48, 0x4d, 0x23, 0x31, 0xf1, 0xf9,
  23993. 0xee, 0x44, 0x6d, 0xad, 0xc1, 0xad, 0x92, 0x73, 0xca, 0x27, 0x16, 0x91,
  23994. 0xa2, 0x50, 0x9a, 0xfc, 0xec, 0xf0, 0x6b, 0x24
  23995. },
  23996. { /* Server Application Traffic Secret */
  23997. 0x89, 0x18, 0x7e, 0x34, 0x8d, 0xfc, 0x14, 0xb1, 0x4f, 0x21, 0xd8, 0x29,
  23998. 0xdb, 0x9b, 0xfb, 0x55, 0xcf, 0xa1, 0x4f, 0x95, 0xf8, 0xe0, 0xb0, 0x83,
  23999. 0xd5, 0x34, 0x9e, 0x0b, 0x83, 0x37, 0x42, 0x93
  24000. },
  24001. { /* Exporter Master Secret */
  24002. 0x7d, 0xc8, 0x88, 0x46, 0xd5, 0x57, 0x15, 0xb6, 0x24, 0x25, 0x92, 0x61,
  24003. 0xb1, 0x18, 0x86, 0x2a, 0x6d, 0xa5, 0x84, 0xeb, 0x59, 0xdf, 0x13, 0xbd,
  24004. 0x73, 0xaa, 0x5d, 0x65, 0xab, 0xd9, 0xb4, 0x56
  24005. },
  24006. { /* Resumption Master Secret */
  24007. 0x20, 0xb7, 0xd0, 0xe3, 0x82, 0x01, 0xa1, 0x04, 0xb8, 0x13, 0x29, 0xed,
  24008. 0x35, 0xe4, 0x2f, 0xbf, 0x58, 0x23, 0x7f, 0x21, 0xdb, 0x9f, 0xf8, 0xe0,
  24009. 0xe8, 0xe4, 0xab, 0xc4, 0xa1, 0x61, 0xb9, 0xbb
  24010. }
  24011. },
  24012. { /* 6 */
  24013. WC_HASH_TYPE_SHA256, 0, 33,
  24014. { 0 }, /* PSK */
  24015. { /* DHE */
  24016. 0x7a, 0x46, 0x8c, 0x5a, 0xd1, 0x8e, 0x95, 0xba,
  24017. 0x61, 0xe6, 0x6f, 0xe6, 0x76, 0x0c, 0x20, 0x43,
  24018. 0x16, 0x82, 0x15, 0xfe, 0x54, 0xa3, 0xc7, 0xfd,
  24019. 0x3b, 0x2c, 0x88, 0xb4, 0xd3, 0x42, 0x70, 0x12,
  24020. 0x18
  24021. },
  24022. { /* Hello 1 */
  24023. 0x63, 0x83, 0x58, 0xab, 0x36, 0xcd, 0x0c, 0xf3,
  24024. 0x26, 0x07, 0xb5, 0x5f, 0x0b, 0x8b, 0x45, 0xd6,
  24025. 0x7d, 0x5b, 0x42, 0xdc, 0xa8, 0xaa, 0x06, 0xfb,
  24026. 0x20, 0xa5, 0xbb, 0x85, 0xdb, 0x54, 0xd8, 0x8b
  24027. },
  24028. { /* Hello 2 */
  24029. 0xea, 0xfe, 0x9e, 0x8e, 0xff, 0x1f, 0x6f, 0x43,
  24030. 0xf9, 0x5d, 0xfd, 0xbf, 0xe2, 0x5f, 0x02, 0x2f,
  24031. 0x6d, 0x47, 0x60, 0x9a, 0x48, 0x9a, 0x75, 0xfb,
  24032. 0xb5, 0x4a, 0xbf, 0x9c, 0x4e, 0xff, 0xbf, 0x0b
  24033. },
  24034. { /* Finished 1 */
  24035. 0xca, 0x25, 0xb3, 0x53, 0x8e, 0x6d, 0xc3, 0x36,
  24036. 0x17, 0x30, 0x07, 0xdf, 0x0d, 0xd7, 0x79, 0xb0,
  24037. 0x7f, 0xcb, 0xbe, 0x7a, 0xbc, 0x2d, 0x9f, 0x2d,
  24038. 0x94, 0x44, 0x94, 0xe6, 0xa4, 0xf3, 0xe8, 0x53
  24039. },
  24040. { /* Finished 2 */
  24041. 0x2e, 0xa6, 0x5a, 0xaf, 0xb5, 0xba, 0x9f, 0x2f,
  24042. 0x74, 0x83, 0x5d, 0xbf, 0x86, 0xa4, 0xa6, 0xf6,
  24043. 0xb9, 0x89, 0xdf, 0x17, 0xe1, 0xa8, 0x14, 0xc0,
  24044. 0xe1, 0x50, 0xfa, 0xec, 0xfa, 0xae, 0x8b, 0x7b
  24045. },
  24046. {
  24047. 0x20, 0x18, 0x72, 0x7c, 0xde, 0x3a, 0x85, 0x17, 0x72, 0xdc, 0xd7, 0x72,
  24048. 0xb0, 0xfc, 0x45, 0xd0, 0x62, 0xb9, 0xbb, 0x38, 0x69, 0x05, 0x7b, 0xb4,
  24049. 0x5e, 0x58, 0x5d, 0xed, 0xcd, 0x0b, 0x96, 0xd3
  24050. },
  24051. {
  24052. 0x68, 0x10, 0x20, 0xd1, 0x5e, 0xfc, 0x0c, 0x53, 0x85, 0xbb, 0xdb, 0x18,
  24053. 0xa8, 0x78, 0xf1, 0x2b, 0x13, 0xba, 0x64, 0x1d, 0xe7, 0x09, 0xbe, 0x13,
  24054. 0x49, 0x26, 0xf9, 0x98, 0x56, 0xf1, 0x43, 0xfb
  24055. },
  24056. {
  24057. 0x24, 0x35, 0x3e, 0x10, 0x6f, 0x39, 0x50, 0xd6, 0xa2, 0x12, 0x99, 0xf2,
  24058. 0xd5, 0xf5, 0x19, 0xf5, 0x84, 0xed, 0xee, 0x78, 0x2a, 0xa6, 0xfa, 0x3d,
  24059. 0x06, 0xa8, 0xa7, 0x5d, 0x97, 0x78, 0xd6, 0x58
  24060. },
  24061. {
  24062. 0xf4, 0x57, 0xac, 0x24, 0x7a, 0xfb, 0x7c, 0x3b, 0xb6, 0x39, 0x17, 0x14,
  24063. 0xd9, 0xd4, 0x58, 0x4d, 0x46, 0xd5, 0x1b, 0xde, 0xf7, 0x9d, 0x06, 0xee,
  24064. 0x8d, 0x1a, 0x2c, 0x25, 0x6d, 0x64, 0xde, 0x89
  24065. },
  24066. {
  24067. 0xb6, 0x00, 0xce, 0x63, 0xed, 0x65, 0x8b, 0x66, 0x66, 0x42, 0xc6, 0xbd,
  24068. 0x89, 0xc4, 0x71, 0x6f, 0xce, 0x28, 0xb2, 0xac, 0x97, 0x07, 0x5b, 0xea,
  24069. 0xb8, 0x1d, 0x4c, 0xeb, 0x9e, 0x71, 0x07, 0x8f
  24070. },
  24071. {
  24072. 0xf8, 0x92, 0xc8, 0xba, 0xe7, 0x83, 0xfe, 0x68, 0xe4, 0xd6, 0x5e, 0xcb,
  24073. 0xb3, 0xef, 0x49, 0xd0, 0xe7, 0xb1, 0xac, 0xcb, 0x39, 0x19, 0xfd, 0xa7,
  24074. 0xf7, 0xca, 0xab, 0x1e, 0x42, 0x14, 0xd8, 0xe7
  24075. },
  24076. {
  24077. 0x32, 0x4a, 0x1a, 0xad, 0xe2, 0xbb, 0x55, 0x8a, 0xdd, 0xe9, 0xa5, 0x2a,
  24078. 0x46, 0x5e, 0x6c, 0x83, 0x66, 0x27, 0x27, 0x94, 0xdd, 0x68, 0x59, 0xa0,
  24079. 0xbb, 0xe8, 0x31, 0x7c, 0x39, 0xd7, 0xfd, 0x6d
  24080. },
  24081. {
  24082. 0x58, 0xbc, 0x6c, 0x5b, 0x24, 0xad, 0x82, 0xb3, 0xcc, 0xc7, 0xd1, 0xa1,
  24083. 0xaa, 0x2b, 0x98, 0x9f, 0x2f, 0x7e, 0xa9, 0x63, 0xc2, 0x8e, 0xb6, 0x06,
  24084. 0xc2, 0x2b, 0x74, 0x4b, 0x79, 0x19, 0x7e, 0x2e
  24085. }
  24086. },
  24087. { /* 11 */
  24088. WC_HASH_TYPE_SHA256, 33, 0,
  24089. { /* PSK */
  24090. 0x3d, 0x39, 0x49, 0x36, 0x98, 0xc5, 0xfd, 0xcd,
  24091. 0xa0, 0x17, 0xbd, 0x65, 0x0a, 0xdb, 0xd4, 0x07,
  24092. 0x56, 0xa2, 0x7b, 0xb8, 0x2a, 0x7e, 0xfb, 0x26,
  24093. 0x74, 0xe1, 0xbc, 0x08, 0x4b, 0xf0, 0x30, 0x14,
  24094. 0x12
  24095. },
  24096. { 0 }, /* DHE */
  24097. { /* Hello 1 */
  24098. 0xb7, 0x44, 0x74, 0x6c, 0x57, 0x1f, 0xf3, 0x84,
  24099. 0x8f, 0x63, 0xfb, 0x8c, 0x94, 0x6c, 0x16, 0x68,
  24100. 0x4b, 0xe1, 0xb5, 0xb5, 0x2a, 0x4e, 0x5f, 0xdf,
  24101. 0x4b, 0x53, 0xb2, 0x35, 0xfc, 0x30, 0xf1, 0x36
  24102. },
  24103. { /* Hello 2 */
  24104. 0xe6, 0x4f, 0x3a, 0x4f, 0xd7, 0xe0, 0x64, 0xd4,
  24105. 0x69, 0x50, 0xe4, 0x8b, 0xba, 0xbc, 0x47, 0x74,
  24106. 0xa7, 0x9b, 0x40, 0x91, 0x8f, 0xa8, 0x72, 0x22,
  24107. 0x97, 0xad, 0x43, 0xa7, 0x11, 0x86, 0xb5, 0x72
  24108. },
  24109. { /* Finished 1 */
  24110. 0x5f, 0xa6, 0x10, 0xe2, 0xa3, 0x99, 0x0b, 0x5e,
  24111. 0x57, 0xee, 0xc3, 0x3a, 0x8e, 0x04, 0xf3, 0x0e,
  24112. 0x58, 0x02, 0x09, 0xb2, 0x7e, 0x2d, 0xc6, 0xd2,
  24113. 0x08, 0xae, 0x68, 0x0a, 0x55, 0xa5, 0xda, 0x51
  24114. },
  24115. { /* Finished 2 */
  24116. 0xfc, 0x5b, 0xc0, 0x7e, 0x1b, 0xaa, 0xc0, 0xb4,
  24117. 0x34, 0x85, 0x49, 0x8e, 0x16, 0x31, 0x98, 0xdf,
  24118. 0x10, 0x54, 0x22, 0xda, 0x1e, 0x6b, 0x51, 0xf6,
  24119. 0x97, 0x57, 0xa0, 0x7a, 0x92, 0xe7, 0x47, 0x52
  24120. },
  24121. {
  24122. 0x80, 0xfa, 0x36, 0x30, 0xb8, 0x65, 0xb3, 0x2a, 0x1d, 0x68, 0x91, 0x06,
  24123. 0x98, 0xa0, 0x17, 0x8f, 0xee, 0xb7, 0x9e, 0x3d, 0xd8, 0x84, 0x99, 0x30,
  24124. 0xb9, 0xd6, 0x09, 0x25, 0x5e, 0xfb, 0x8f, 0xd3 },
  24125. {
  24126. 0xa9, 0x89, 0x29, 0x70, 0xe4, 0x55, 0xec, 0x97, 0xfb, 0x24, 0x5b, 0xf9,
  24127. 0xf1, 0xa3, 0x19, 0x3d, 0xf1, 0x31, 0x14, 0xcd, 0x2a, 0xed, 0x21, 0xc8,
  24128. 0xb1, 0x53, 0xad, 0x11, 0x0b, 0x9e, 0x5a, 0xee },
  24129. {
  24130. 0x72, 0xad, 0x8d, 0x7f, 0xfc, 0xb7, 0x68, 0xda, 0x27, 0x60, 0x37, 0xa3,
  24131. 0x4a, 0x63, 0xe8, 0xa5, 0xc8, 0xcd, 0x36, 0x6a, 0x77, 0x99, 0x0d, 0xa9,
  24132. 0xb1, 0x5b, 0x2f, 0x47, 0x2e, 0x22, 0xa7, 0x5e },
  24133. {
  24134. 0x95, 0x6e, 0x85, 0x09, 0xe5, 0x04, 0x88, 0x14, 0x28, 0x8d, 0xdf, 0xe6,
  24135. 0x0d, 0x0f, 0x0d, 0x6b, 0x4e, 0x66, 0x1c, 0x03, 0xb9, 0xaa, 0x2d, 0x45,
  24136. 0x56, 0x67, 0x5c, 0x55, 0x29, 0xd6, 0x89, 0xd0 },
  24137. {
  24138. 0xe8, 0xf2, 0x14, 0xf9, 0x9b, 0x2b, 0x9f, 0x24, 0x2b, 0x37, 0xbe, 0x86,
  24139. 0xdb, 0x23, 0x4b, 0xbe, 0x39, 0x57, 0xe8, 0xa9, 0xa5, 0xee, 0x08, 0xf2,
  24140. 0x75, 0x58, 0xdb, 0xd9, 0x51, 0xc1, 0x46, 0x02 },
  24141. {
  24142. 0x3d, 0x19, 0xaf, 0xa3, 0x0b, 0x21, 0xf7, 0x3d, 0xe7, 0x37, 0x6e, 0x32,
  24143. 0x13, 0x48, 0x9d, 0xea, 0xe0, 0x90, 0xbf, 0x64, 0x48, 0xf7, 0x1e, 0xcc,
  24144. 0xf0, 0xbc, 0x92, 0xd7, 0x8a, 0x4a, 0xa8, 0xc1 },
  24145. {
  24146. 0x16, 0x35, 0xb1, 0x66, 0x28, 0xa3, 0x3e, 0x19, 0xf5, 0x2d, 0x92, 0x22,
  24147. 0x95, 0x48, 0xe8, 0x34, 0x7b, 0x30, 0x50, 0xa2, 0xa0, 0xd9, 0xc2, 0x59,
  24148. 0x39, 0xf9, 0x8c, 0x69, 0xf2, 0x2a, 0xb9, 0xff },
  24149. {
  24150. 0x32, 0x71, 0xa6, 0x87, 0x0c, 0x97, 0x42, 0x07, 0xdd, 0x5f, 0xc9, 0x44,
  24151. 0xa5, 0x7c, 0x50, 0x14, 0xfd, 0xe7, 0x5f, 0x8b, 0xd3, 0x2f, 0xdc, 0x9b,
  24152. 0xa9, 0x93, 0x22, 0x19, 0xe6, 0xf2, 0x0c, 0xd8 }
  24153. },
  24154. #ifdef WOLFSSL_SHA384
  24155. { /* 26 */
  24156. WC_HASH_TYPE_SHA384, 35, 35,
  24157. { /* PSK */
  24158. 0x62, 0x83, 0x25, 0xc7, 0xcc, 0x08, 0x5e, 0x63,
  24159. 0x64, 0x56, 0xf0, 0xc6, 0x88, 0x27, 0x5a, 0x5b,
  24160. 0x68, 0x59, 0x0b, 0x14, 0x55, 0x13, 0x2e, 0xfd,
  24161. 0x8f, 0x28, 0x5b, 0x3d, 0xe3, 0xad, 0x67, 0xe4,
  24162. 0x68, 0xba, 0xf9
  24163. },
  24164. { /* DHE */
  24165. 0xa8, 0xb1, 0xab, 0xd8, 0xc8, 0x5b, 0x52, 0xdf,
  24166. 0x7f, 0x49, 0x10, 0xf4, 0xa1, 0x31, 0xd1, 0x91,
  24167. 0x36, 0xc1, 0x87, 0x5d, 0x42, 0x2a, 0xe7, 0x1d,
  24168. 0x2c, 0x29, 0x3d, 0x40, 0x64, 0x61, 0x63, 0x76,
  24169. 0xd8, 0x66, 0xac
  24170. },
  24171. { /* Hello 1 */
  24172. 0x6f, 0xc6, 0x4c, 0xe1, 0xc6, 0x68, 0x34, 0x8c,
  24173. 0x0a, 0xe1, 0xf8, 0xb8, 0x3e, 0xd4, 0xf8, 0x0b,
  24174. 0x54, 0x50, 0xe4, 0xc5, 0x4a, 0x33, 0x7d, 0xbd,
  24175. 0x90, 0xd2, 0xa2, 0xb9, 0xb7, 0x92, 0xed, 0xab,
  24176. 0x14, 0xf1, 0xe4, 0x86, 0x22, 0x67, 0xd7, 0x44,
  24177. 0x03, 0x21, 0xdc, 0x51, 0x52, 0x7f, 0x35, 0x80
  24178. },
  24179. { /* Hello 2 */
  24180. 0x3e, 0xcf, 0x2f, 0xc3, 0x87, 0xba, 0xc5, 0xbd,
  24181. 0x7c, 0xe8, 0x35, 0x5b, 0x95, 0x51, 0x30, 0x3b,
  24182. 0x08, 0xcc, 0x2a, 0x7d, 0xb5, 0x74, 0x7c, 0x16,
  24183. 0xb3, 0x0b, 0xe7, 0x61, 0xa3, 0x7c, 0x6c, 0xbd,
  24184. 0x39, 0x74, 0xfd, 0x1e, 0x4c, 0xff, 0xc8, 0xcc,
  24185. 0xa0, 0xef, 0x29, 0x4d, 0x94, 0xaa, 0x55, 0x6f,
  24186. },
  24187. { /* Finished 1 */
  24188. 0x06, 0xc1, 0x47, 0x78, 0x66, 0x53, 0x6f, 0x24,
  24189. 0x94, 0x61, 0x69, 0xec, 0xd8, 0x60, 0x31, 0x2f,
  24190. 0xbf, 0xd6, 0x8a, 0x29, 0x17, 0xff, 0xa3, 0x88,
  24191. 0x13, 0x09, 0x8c, 0x9d, 0x6c, 0x64, 0x84, 0x48,
  24192. 0x44, 0xdd, 0x2d, 0x29, 0x4d, 0xe6, 0x98, 0x2b,
  24193. 0x45, 0x3b, 0x84, 0x33, 0x79, 0xb2, 0x75, 0x68
  24194. },
  24195. { /* Finished 2 */
  24196. 0x28, 0x1e, 0x18, 0xf7, 0x9c, 0x32, 0xa9, 0xbf,
  24197. 0x0c, 0x24, 0x58, 0x21, 0xce, 0xbc, 0xf2, 0x44,
  24198. 0xb1, 0x18, 0xaf, 0x9d, 0xd9, 0x20, 0xf9, 0xf4,
  24199. 0xed, 0xcc, 0x53, 0x82, 0x66, 0x5c, 0x46, 0x94,
  24200. 0x8c, 0x36, 0x5e, 0xca, 0x9f, 0xd8, 0x9a, 0xd3,
  24201. 0xf0, 0xe1, 0x53, 0x71, 0xdd, 0x19, 0x1e, 0x59
  24202. },
  24203. {
  24204. 0xd0, 0xef, 0xa8, 0xcb, 0x5b, 0x14, 0x0f, 0x0a, 0x62, 0xba, 0x5a, 0xb1,
  24205. 0xc5, 0xb5, 0x3f, 0x11, 0xda, 0xa1, 0x0c, 0x9c, 0xb4, 0x32, 0x48, 0x4e,
  24206. 0xfa, 0x84, 0x4f, 0xe4, 0xe7, 0x91, 0x8f, 0x42, 0x3f, 0xc7, 0x4e, 0xd3,
  24207. 0x83, 0x3d, 0x7f, 0x70, 0x12, 0xee, 0x9a, 0x37, 0x01, 0xbb, 0x14, 0xd3
  24208. },
  24209. {
  24210. 0x48, 0x6f, 0x77, 0x1d, 0x39, 0x1b, 0xa5, 0x9a, 0x76, 0xd9, 0x1d, 0x7d,
  24211. 0xb3, 0xd9, 0xb9, 0x78, 0x35, 0x0f, 0xd0, 0xe1, 0x07, 0x1f, 0x8d, 0xe5,
  24212. 0x75, 0x00, 0xda, 0xc0, 0x19, 0x01, 0xfb, 0x08, 0x35, 0xe7, 0x18, 0x8f,
  24213. 0xf0, 0x19, 0xfb, 0x46, 0xf6, 0xa5, 0x77, 0x0e, 0x90, 0x38, 0x8b, 0x15
  24214. },
  24215. {
  24216. 0x80, 0x8c, 0xa7, 0x24, 0x97, 0xf9, 0xd3, 0x52, 0xb0, 0x69, 0x9d, 0x4b,
  24217. 0xa4, 0x19, 0x4a, 0xb1, 0x46, 0x53, 0x3a, 0xc8, 0xe4, 0x02, 0x69, 0xf2,
  24218. 0xe7, 0xb6, 0x1d, 0x33, 0x51, 0xcc, 0x14, 0x40, 0x4a, 0xb0, 0xe7, 0x58,
  24219. 0x84, 0xba, 0xc2, 0x14, 0x58, 0x6b, 0xb9, 0xdc, 0x50, 0x98, 0x67, 0x01
  24220. },
  24221. {
  24222. 0xb1, 0xa8, 0xc0, 0x06, 0xb3, 0x2e, 0xa7, 0x8a, 0x6a, 0x12, 0x88, 0x00,
  24223. 0x65, 0x88, 0x9c, 0x5d, 0x35, 0xee, 0xe5, 0x51, 0x0b, 0x62, 0xf8, 0x67,
  24224. 0xe5, 0xef, 0x15, 0x1f, 0x23, 0x02, 0x74, 0x08, 0x9c, 0xc8, 0xba, 0x27,
  24225. 0x5d, 0x32, 0x19, 0x6f, 0x6d, 0x5d, 0x72, 0x5e, 0x15, 0xde, 0x30, 0xc3
  24226. },
  24227. {
  24228. 0xfd, 0xce, 0xf5, 0x65, 0x45, 0x84, 0xfb, 0x8c, 0x79, 0xa4, 0x6c, 0x1b,
  24229. 0x0e, 0x1b, 0xfd, 0x26, 0xa2, 0x53, 0xf4, 0x4e, 0x00, 0x4d, 0x4b, 0x0b,
  24230. 0x24, 0x6d, 0x35, 0x35, 0xd9, 0x97, 0x70, 0xc5, 0xf4, 0xee, 0xe3, 0xba,
  24231. 0x31, 0x1e, 0x2a, 0x42, 0xcb, 0xdf, 0x40, 0xb1, 0x14, 0xb8, 0x53, 0xce
  24232. },
  24233. {
  24234. 0xbb, 0xb3, 0x26, 0x7c, 0x22, 0x21, 0x9b, 0x72, 0x32, 0xa1, 0x97, 0xfb,
  24235. 0x78, 0x8c, 0xbe, 0x3d, 0x71, 0x45, 0xb8, 0xf5, 0x24, 0x8f, 0x0f, 0xac,
  24236. 0x42, 0x5b, 0x81, 0xe8, 0xd0, 0x71, 0x4a, 0xcb, 0x32, 0x3f, 0x03, 0xfb,
  24237. 0xec, 0x6a, 0x1f, 0x76, 0x80, 0x65, 0x01, 0x7a, 0x3d, 0xce, 0xc4, 0xdf
  24238. },
  24239. {
  24240. 0x3f, 0xcf, 0x2f, 0x63, 0x94, 0x94, 0x99, 0xfd, 0x04, 0x3a, 0x89, 0x83,
  24241. 0xcf, 0x06, 0x05, 0xec, 0x20, 0x3e, 0x5f, 0x51, 0x9d, 0x6e, 0x4a, 0xc6,
  24242. 0xf1, 0x2b, 0x37, 0x17, 0x34, 0x72, 0x6e, 0x1d, 0x2a, 0xfd, 0xc7, 0x73,
  24243. 0xb5, 0x07, 0x22, 0x81, 0x32, 0x2e, 0x21, 0x85, 0xaf, 0x10, 0xb2, 0x73
  24244. },
  24245. {
  24246. 0x52, 0x0c, 0x3d, 0x2e, 0x2d, 0x4a, 0x11, 0xae, 0x96, 0x78, 0xe9, 0x5b,
  24247. 0xd8, 0x0f, 0x6c, 0xf4, 0xbd, 0x96, 0x13, 0x55, 0x88, 0xdd, 0xa3, 0x67,
  24248. 0x36, 0x86, 0x1e, 0x0b, 0x36, 0x41, 0xec, 0xf6, 0x04, 0xb2, 0xc4, 0x16,
  24249. 0xbc, 0x2c, 0xdb, 0x30, 0x02, 0x94, 0xd4, 0x42, 0xbf, 0x38, 0xee, 0x9d
  24250. }
  24251. },
  24252. { /* 36 */
  24253. WC_HASH_TYPE_SHA384, 0, 33,
  24254. { 0 }, /* PSK */
  24255. { /* DHE */
  24256. 0xd3, 0x00, 0x72, 0x9a, 0xa8, 0xc5, 0xf3, 0xc4,
  24257. 0xf1, 0xa0, 0x26, 0x89, 0x65, 0x70, 0xc7, 0x0b,
  24258. 0x77, 0xbb, 0xe1, 0x4b, 0x2b, 0xa8, 0x4f, 0xa6,
  24259. 0x09, 0x4b, 0xba, 0x45, 0x36, 0x15, 0xee, 0x68,
  24260. 0xfd
  24261. },
  24262. { /* Hello 1 */
  24263. 0x10, 0x9d, 0x8b, 0xa2, 0x93, 0xe7, 0xd3, 0xb9,
  24264. 0xb4, 0x0f, 0xeb, 0x6a, 0xb9, 0x69, 0xcb, 0x39,
  24265. 0x16, 0x29, 0xcc, 0xd3, 0xcc, 0x1a, 0x4c, 0x1b,
  24266. 0x53, 0x7c, 0x33, 0x88, 0x06, 0xbc, 0x0a, 0x02,
  24267. 0xa0, 0xbe, 0x62, 0xc0, 0xe6, 0x5e, 0x97, 0x5b,
  24268. 0x6a, 0xa1, 0x98, 0xf3, 0xd2, 0x1e, 0xcd, 0xc5
  24269. },
  24270. { /* Hello 2 */
  24271. 0x74, 0xc0, 0x07, 0x2c, 0xc1, 0x63, 0xcc, 0x11,
  24272. 0xad, 0x1a, 0x55, 0x63, 0xbc, 0x20, 0x77, 0x96,
  24273. 0x30, 0x1c, 0x68, 0x45, 0x1e, 0x9b, 0xa7, 0xb4,
  24274. 0xf3, 0x04, 0x45, 0x16, 0x76, 0x55, 0xf9, 0xdf,
  24275. 0x4b, 0x2f, 0x1a, 0xdf, 0x5a, 0xb0, 0x93, 0xc9,
  24276. 0xab, 0xf5, 0x32, 0x47, 0x79, 0x9c, 0x01, 0xeb
  24277. },
  24278. { /* Finished 1 */
  24279. 0x27, 0x08, 0x8e, 0xa5, 0xf1, 0x30, 0xe1, 0xd6,
  24280. 0x4f, 0xa2, 0x9e, 0x3b, 0x03, 0x2d, 0x2e, 0xa3,
  24281. 0x84, 0x75, 0x51, 0x3a, 0xc3, 0xf6, 0xee, 0x2e,
  24282. 0x37, 0x0c, 0xe3, 0x28, 0x46, 0xa5, 0x2d, 0xc7,
  24283. 0xf0, 0x64, 0x78, 0x53, 0x66, 0x43, 0x02, 0xa4,
  24284. 0x7a, 0x43, 0x66, 0x4b, 0xa7, 0xcb, 0x97, 0x16
  24285. },
  24286. { /* Finished 2 */
  24287. 0x1d, 0x0d, 0xf8, 0xe1, 0x81, 0xa5, 0xbd, 0xa8,
  24288. 0x6f, 0x9d, 0x01, 0xa4, 0x9a, 0x92, 0xe2, 0xef,
  24289. 0x08, 0xab, 0xef, 0x3e, 0x2d, 0xd4, 0x82, 0xac,
  24290. 0x68, 0x9d, 0xe0, 0x54, 0x17, 0xde, 0x1a, 0xed,
  24291. 0x57, 0xcb, 0xd9, 0x2d, 0xc8, 0xbc, 0x93, 0xe6,
  24292. 0xa3, 0xec, 0xde, 0xee, 0xa1, 0x1c, 0x41, 0x85
  24293. },
  24294. {
  24295. 0x7f, 0x1f, 0xe6, 0x7b, 0xd8, 0xf5, 0x2b, 0x37, 0xbe, 0xb7, 0xd0, 0x37,
  24296. 0xce, 0x46, 0xad, 0x04, 0x2f, 0xc7, 0xdb, 0xc9, 0x9a, 0xb6, 0x00, 0x3f,
  24297. 0xc1, 0x97, 0xe9, 0x5c, 0x5e, 0x14, 0xd1, 0x38, 0x4d, 0x55, 0xe1, 0x07,
  24298. 0xb5, 0x85, 0x6d, 0xfa, 0xa7, 0x66, 0xad, 0xfa, 0xb6, 0xad, 0x29, 0x44
  24299. },
  24300. {
  24301. 0x4e, 0x6b, 0x20, 0x99, 0x55, 0x1b, 0x21, 0x89, 0xb6, 0x70, 0xdb, 0xe8,
  24302. 0xa7, 0x16, 0x55, 0xf2, 0x93, 0x13, 0x90, 0x7d, 0xfa, 0x62, 0x65, 0x53,
  24303. 0xa0, 0x97, 0xe9, 0xb4, 0xc0, 0xf1, 0xc9, 0x1a, 0x67, 0xdd, 0xca, 0x57,
  24304. 0xbc, 0xca, 0x39, 0xe6, 0x39, 0x6b, 0x63, 0x47, 0x25, 0x08, 0x3a, 0xd7
  24305. },
  24306. {
  24307. 0x35, 0x0d, 0xac, 0xd8, 0x10, 0x6a, 0x46, 0x50, 0x66, 0xae, 0x02, 0xc9,
  24308. 0xde, 0x13, 0x48, 0xce, 0x53, 0xd4, 0x92, 0x62, 0xc5, 0x65, 0x10, 0x08,
  24309. 0xc2, 0xc2, 0x82, 0xed, 0x9d, 0xc9, 0x6f, 0xa8, 0xc3, 0xc1, 0x0b, 0x7c,
  24310. 0xe1, 0x97, 0x85, 0xd6, 0x46, 0x29, 0x0e, 0x42, 0x51, 0xc1, 0x35, 0xcf
  24311. },
  24312. {
  24313. 0x3d, 0x5d, 0x84, 0xbd, 0x16, 0x46, 0x34, 0xb3, 0xf6, 0x31, 0x49, 0x3e,
  24314. 0x8d, 0xdc, 0xcb, 0x8c, 0x6a, 0x42, 0xf4, 0x88, 0xfc, 0x19, 0xfa, 0xa2,
  24315. 0x25, 0xc7, 0xa0, 0xa4, 0xca, 0xf0, 0xea, 0x2d, 0xe8, 0xc4, 0x02, 0x14,
  24316. 0x63, 0xfb, 0xd3, 0x7b, 0x51, 0x1c, 0xce, 0xca, 0xa3, 0xc3, 0xe4, 0xa5
  24317. },
  24318. {
  24319. 0x7c, 0x3a, 0x55, 0x92, 0x2e, 0xdd, 0x75, 0xdd, 0x76, 0x54, 0x4a, 0x9f,
  24320. 0xd0, 0xa2, 0x88, 0x83, 0xe9, 0x27, 0xda, 0x30, 0xe9, 0x96, 0x58, 0xc5,
  24321. 0xb7, 0x56, 0xfc, 0x4b, 0xb8, 0x5d, 0xee, 0x46, 0x70, 0x4e, 0x1b, 0x06,
  24322. 0x86, 0xaf, 0x48, 0x5c, 0x17, 0x35, 0xfa, 0x69, 0xc2, 0x4d, 0xfb, 0x09
  24323. },
  24324. {
  24325. 0x00, 0x0e, 0x28, 0x51, 0xc1, 0x7f, 0x41, 0x89, 0x6f, 0x9a, 0xca, 0x15,
  24326. 0xee, 0xed, 0x43, 0xca, 0x6d, 0x65, 0x6f, 0x51, 0x18, 0x6c, 0x08, 0x4b,
  24327. 0x77, 0xca, 0x75, 0xc4, 0xc3, 0xde, 0x29, 0x41, 0x8b, 0xaf, 0xa7, 0x1c,
  24328. 0x28, 0x37, 0xa0, 0xa0, 0x74, 0x8e, 0x09, 0x42, 0x7a, 0x1b, 0x68, 0xdb
  24329. },
  24330. {
  24331. 0x14, 0x8f, 0xab, 0x28, 0x64, 0xea, 0x45, 0x88, 0xdb, 0xc1, 0xc6, 0xa0,
  24332. 0x48, 0xdf, 0x15, 0xd0, 0x28, 0x07, 0x2d, 0x6c, 0xb8, 0x42, 0xbb, 0x60,
  24333. 0x02, 0x08, 0x9e, 0x29, 0x9b, 0x8d, 0xd6, 0x1c, 0xaf, 0xf2, 0x1a, 0xdc,
  24334. 0xf0, 0x78, 0x0b, 0x4d, 0x90, 0xa1, 0x0c, 0xb3, 0x13, 0xde, 0xca, 0x5a
  24335. },
  24336. {
  24337. 0x4d, 0x80, 0x7d, 0x0b, 0xb9, 0x00, 0x6f, 0x65, 0x51, 0x65, 0x23, 0xde,
  24338. 0x72, 0xdc, 0x4f, 0x04, 0xa5, 0xa2, 0x90, 0x45, 0x51, 0x9e, 0xd0, 0x3a,
  24339. 0xe4, 0xd7, 0x78, 0xa3, 0x0f, 0x2d, 0x65, 0x12, 0xad, 0xc8, 0x92, 0x30,
  24340. 0x79, 0x9d, 0x9d, 0x08, 0x7a, 0x9c, 0x9f, 0x83, 0xb1, 0xca, 0x59, 0x56
  24341. }
  24342. },
  24343. { /* 41 */
  24344. WC_HASH_TYPE_SHA384, 33, 0,
  24345. { /* PSK */
  24346. 0xa4, 0x8b, 0x1b, 0x5f, 0xd0, 0xea, 0x75, 0x62,
  24347. 0x06, 0x4d, 0x68, 0x40, 0x85, 0x20, 0x45, 0x95,
  24348. 0x4a, 0x00, 0xca, 0x05, 0xeb, 0xd4, 0x1d, 0x48,
  24349. 0x81, 0x89, 0xe8, 0x86, 0x43, 0xfa, 0x28, 0x17,
  24350. 0x12
  24351. },
  24352. { 0 }, /* DHE */
  24353. { /* Hello 1 */
  24354. 0x03, 0x7c, 0x33, 0x75, 0xdc, 0xc5, 0x46, 0x3a,
  24355. 0x0d, 0x56, 0xc6, 0xfb, 0xab, 0x1e, 0x1d, 0xda,
  24356. 0x59, 0xc2, 0xb2, 0xb1, 0x7c, 0x48, 0x9b, 0x06,
  24357. 0x0a, 0x5a, 0xbb, 0xf8, 0x98, 0x53, 0x78, 0x2d,
  24358. 0xd2, 0xcc, 0x87, 0x68, 0x25, 0xdd, 0x88, 0x22,
  24359. 0xcd, 0xb7, 0x74, 0x55, 0x21, 0xf9, 0x34, 0x98
  24360. },
  24361. { /* Hello 2 */
  24362. 0x03, 0xb4, 0xfb, 0xcc, 0x28, 0x2c, 0xc1, 0x70,
  24363. 0x42, 0x73, 0x57, 0xac, 0xdb, 0x47, 0x71, 0xf6,
  24364. 0x2e, 0x11, 0x8a, 0x5b, 0x47, 0x2f, 0x02, 0x54,
  24365. 0x95, 0x34, 0xed, 0x5f, 0x19, 0xc1, 0x75, 0xe0,
  24366. 0x76, 0xad, 0xb0, 0x90, 0x57, 0xcd, 0xfd, 0xd7,
  24367. 0x58, 0x1f, 0x0d, 0x6b, 0x9e, 0x51, 0x3c, 0x08
  24368. },
  24369. { /* Finished 1 */
  24370. 0x2b, 0x50, 0xd9, 0xa7, 0x43, 0x24, 0xda, 0x2c,
  24371. 0x7a, 0xaa, 0x0e, 0x37, 0xd7, 0x6b, 0x2c, 0xab,
  24372. 0x8e, 0xb2, 0xfe, 0x31, 0x1b, 0xa8, 0x12, 0x59,
  24373. 0x5b, 0x7b, 0xdc, 0x3e, 0xa7, 0x86, 0xa5, 0x48,
  24374. 0xe4, 0x46, 0x2b, 0x4c, 0xc1, 0x66, 0x4b, 0xf3,
  24375. 0x2a, 0x99, 0x93, 0x08, 0xbc, 0x3d, 0x08, 0x76
  24376. },
  24377. { /* Finished 2 */
  24378. 0x7c, 0x34, 0xc8, 0x56, 0x17, 0xf1, 0x62, 0x1c,
  24379. 0x9f, 0x0b, 0xeb, 0xfd, 0x69, 0x72, 0x51, 0xc5,
  24380. 0xfa, 0x74, 0x87, 0xc9, 0xbd, 0x50, 0xe9, 0x48,
  24381. 0xa7, 0x3c, 0x94, 0x3e, 0x06, 0x7d, 0xe8, 0x8e,
  24382. 0xc1, 0xd1, 0x08, 0x1f, 0x5d, 0x48, 0x8a, 0x25,
  24383. 0xfc, 0xea, 0xe7, 0xd9, 0xd4, 0xd0, 0xf9, 0xad
  24384. },
  24385. {
  24386. 0x4b, 0x0b, 0xed, 0xb9, 0xc8, 0xb8, 0xa8, 0x1e, 0xb0, 0x81, 0x76, 0xd5,
  24387. 0x33, 0x22, 0x71, 0x33, 0x3a, 0x85, 0x19, 0x67, 0x7e, 0x91, 0x37, 0xf2,
  24388. 0xa6, 0x11, 0x22, 0xdf, 0x41, 0x04, 0x3d, 0xa9, 0x13, 0xb9, 0xb2, 0xb1,
  24389. 0xbb, 0xd8, 0xef, 0x23, 0x7c, 0xc2, 0xab, 0x70, 0x1b, 0x51, 0x9f, 0xc9
  24390. },
  24391. {
  24392. 0xeb, 0x96, 0x10, 0x8c, 0x7d, 0x92, 0xea, 0x80, 0x86, 0xb2, 0xf8, 0x27,
  24393. 0xf2, 0x9a, 0x09, 0xc1, 0x7c, 0x09, 0x43, 0xbc, 0xfe, 0xc8, 0x75, 0xe0,
  24394. 0x97, 0xe7, 0x6d, 0xd5, 0xb2, 0x3c, 0xed, 0x12, 0xb7, 0x74, 0x0e, 0xe3,
  24395. 0xb6, 0xe0, 0xba, 0xe1, 0x8d, 0x89, 0xcf, 0x4f, 0x57, 0xf6, 0x6d, 0x90
  24396. },
  24397. {
  24398. 0x22, 0xb0, 0x39, 0x34, 0xb6, 0x6c, 0x2d, 0x7a, 0x97, 0x1c, 0x5d, 0xcc,
  24399. 0x78, 0x84, 0x71, 0xbb, 0xc6, 0x7b, 0xb6, 0xbc, 0xcc, 0x0b, 0xf8, 0xac,
  24400. 0x8e, 0xd7, 0x20, 0xbd, 0xbe, 0x32, 0xf0, 0xd6, 0xe9, 0x69, 0x13, 0xf2,
  24401. 0x9a, 0xce, 0xfe, 0x86, 0xd3, 0xee, 0xba, 0x69, 0x51, 0xb6, 0x77, 0x56
  24402. },
  24403. {
  24404. 0x16, 0xfd, 0xda, 0xf3, 0x5e, 0xb9, 0xa6, 0x17, 0x24, 0xb2, 0x16, 0x9f,
  24405. 0xb6, 0x59, 0x13, 0x0f, 0x25, 0x5a, 0xf1, 0x5b, 0x5f, 0xe4, 0x54, 0x2a,
  24406. 0xa7, 0xbf, 0x29, 0xaf, 0x5a, 0x77, 0xf4, 0x4f, 0x25, 0xba, 0x94, 0xad,
  24407. 0x6b, 0x91, 0x3b, 0xe7, 0xd5, 0x73, 0x0d, 0xff, 0xaa, 0xe3, 0x72, 0x2c
  24408. },
  24409. {
  24410. 0x22, 0xb4, 0x94, 0xc0, 0x53, 0xd7, 0x82, 0x06, 0x38, 0x9d, 0x4a, 0xa0,
  24411. 0x3f, 0xf1, 0x5f, 0x6e, 0x23, 0x8d, 0x09, 0x62, 0xbf, 0x6f, 0x7c, 0x84,
  24412. 0xc6, 0x3e, 0x15, 0xad, 0x18, 0x37, 0x76, 0x29, 0xc7, 0xd6, 0x68, 0x0c,
  24413. 0x1e, 0xc6, 0x93, 0x31, 0xef, 0x85, 0x69, 0x30, 0x68, 0xf0, 0x1e, 0x37
  24414. },
  24415. {
  24416. 0x6d, 0x4d, 0x20, 0xaf, 0x47, 0xe8, 0x1b, 0xfa, 0xd0, 0xb6, 0xc8, 0x97,
  24417. 0xd1, 0x03, 0xfc, 0x9d, 0x59, 0xa0, 0x68, 0x9d, 0xe9, 0x17, 0x8b, 0xce,
  24418. 0x48, 0x2c, 0x77, 0x8a, 0x22, 0x4b, 0x5c, 0x54, 0x22, 0xa1, 0x15, 0x12,
  24419. 0xe1, 0x07, 0x8e, 0x15, 0xd8, 0x7b, 0x16, 0x65, 0x99, 0x6b, 0xcb, 0x71
  24420. },
  24421. {
  24422. 0x79, 0x64, 0x79, 0xdd, 0x75, 0x5c, 0x6f, 0x98, 0xac, 0x03, 0xe0, 0xcd,
  24423. 0x92, 0xba, 0x0e, 0x2d, 0xb4, 0xd1, 0x8b, 0x97, 0xd0, 0x85, 0xbb, 0x2e,
  24424. 0x4f, 0x26, 0x93, 0xf5, 0x1d, 0xf3, 0xd2, 0x43, 0x4f, 0xd2, 0x47, 0xaa,
  24425. 0x91, 0x1e, 0xf3, 0x67, 0x10, 0x18, 0x2c, 0xb9, 0x01, 0xba, 0x10, 0x9f
  24426. },
  24427. {
  24428. 0x79, 0xb6, 0x9c, 0xbe, 0xf1, 0x6a, 0xb0, 0x92, 0xa0, 0x29, 0x52, 0x61,
  24429. 0xf1, 0xcd, 0x3a, 0x67, 0xe1, 0x6b, 0xb8, 0x9d, 0x0d, 0x95, 0xb6, 0x03,
  24430. 0x80, 0x1f, 0xd5, 0x75, 0xb6, 0x1d, 0x79, 0x02, 0x93, 0x43, 0x77, 0xa7,
  24431. 0x9d, 0x2f, 0xc3, 0x84, 0xc6, 0x83, 0x76, 0x16, 0x06, 0x98, 0x7b, 0x79
  24432. }
  24433. },
  24434. #endif /* WOLFSSL_SHA384 */
  24435. };
  24436. static const char protocolLabel[] = "tls13 ";
  24437. static const char ceTrafficLabel[] = "c e traffic";
  24438. static const char eExpMasterLabel[] = "e exp master";
  24439. static const char cHsTrafficLabel[] = "c hs traffic";
  24440. static const char sHsTrafficLabel[] = "s hs traffic";
  24441. static const char cAppTrafficLabel[] = "c ap traffic";
  24442. static const char sAppTrafficLabel[] = "s ap traffic";
  24443. static const char expMasterLabel[] = "exp master";
  24444. static const char resMasterLabel[] = "res master";
  24445. static const char derivedLabel[] = "derived";
  24446. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t tls13_kdf_test(void)
  24447. {
  24448. wc_test_ret_t ret = 0;
  24449. word32 i;
  24450. word32 tc = sizeof(tls13KdfTestVectors)/sizeof(Tls13KdfTestVector);
  24451. const Tls13KdfTestVector* tv = NULL;
  24452. WOLFSSL_ENTER("tls13_kdf_test");
  24453. for (i = 0, tv = tls13KdfTestVectors; i < tc; i++, tv++) {
  24454. byte output[WC_MAX_DIGEST_SIZE];
  24455. byte secret[WC_MAX_DIGEST_SIZE];
  24456. byte salt[WC_MAX_DIGEST_SIZE];
  24457. byte zeroes[WC_MAX_DIGEST_SIZE];
  24458. byte hashZero[WC_MAX_DIGEST_SIZE];
  24459. int hashAlgSz;
  24460. XMEMSET(zeroes, 0, sizeof zeroes);
  24461. hashAlgSz = wc_HashGetDigestSize(tv->hashAlg);
  24462. if (hashAlgSz == BAD_FUNC_ARG) break;
  24463. ret = wc_Hash(tv->hashAlg, NULL, 0, hashZero, (word32)hashAlgSz);
  24464. if (ret != 0) break;
  24465. ret = wc_Tls13_HKDF_Extract(secret, NULL, 0,
  24466. (tv->pskSz == 0) ? zeroes : (byte*)tv->psk,
  24467. tv->pskSz, tv->hashAlg);
  24468. if (ret != 0) break;
  24469. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24470. secret, (word32)hashAlgSz,
  24471. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24472. (byte*)ceTrafficLabel, (word32)XSTRLEN(ceTrafficLabel),
  24473. tv->hashHello1, (word32)hashAlgSz, tv->hashAlg);
  24474. if (ret != 0) break;
  24475. ret = XMEMCMP(tv->clientEarlyTrafficSecret, output, hashAlgSz);
  24476. if (ret != 0) break;
  24477. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24478. secret, (word32)hashAlgSz,
  24479. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24480. (byte*)eExpMasterLabel, (word32)XSTRLEN(eExpMasterLabel),
  24481. tv->hashHello1, (word32)hashAlgSz, tv->hashAlg);
  24482. if (ret != 0) break;
  24483. ret = XMEMCMP(tv->earlyExporterMasterSecret, output, hashAlgSz);
  24484. if (ret != 0) break;
  24485. ret = wc_Tls13_HKDF_Expand_Label(salt, (word32)hashAlgSz,
  24486. secret, (word32)hashAlgSz,
  24487. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24488. (byte*)derivedLabel, (word32)XSTRLEN(derivedLabel),
  24489. hashZero, (word32)hashAlgSz, tv->hashAlg);
  24490. if (ret != 0) break;
  24491. ret = wc_Tls13_HKDF_Extract(secret, salt, (word32)(word32)hashAlgSz,
  24492. (tv->dheSz == 0) ? zeroes : (byte*)tv->dhe,
  24493. tv->dheSz, tv->hashAlg);
  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*)cHsTrafficLabel, (word32)XSTRLEN(cHsTrafficLabel),
  24499. tv->hashHello2, (word32)hashAlgSz, tv->hashAlg);
  24500. if (ret != 0) break;
  24501. ret = XMEMCMP(tv->clientHandshakeTrafficSecret,
  24502. output, hashAlgSz);
  24503. if (ret != 0) break;
  24504. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24505. secret, (word32)hashAlgSz,
  24506. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24507. (byte*)sHsTrafficLabel, (word32)XSTRLEN(sHsTrafficLabel),
  24508. tv->hashHello2, (word32)hashAlgSz, tv->hashAlg);
  24509. if (ret != 0) break;
  24510. ret = XMEMCMP(tv->serverHandshakeTrafficSecret, output, hashAlgSz);
  24511. if (ret != 0) break;
  24512. ret = wc_Tls13_HKDF_Expand_Label(salt, (word32)hashAlgSz,
  24513. secret, (word32)hashAlgSz,
  24514. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24515. (byte*)derivedLabel, (word32)XSTRLEN(derivedLabel),
  24516. hashZero, (word32)hashAlgSz, tv->hashAlg);
  24517. if (ret != 0) break;
  24518. ret = wc_Tls13_HKDF_Extract(secret, salt, (word32)(word32)hashAlgSz,
  24519. zeroes, (word32)(word32)hashAlgSz, tv->hashAlg);
  24520. if (ret != 0) break;
  24521. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24522. secret, (word32)hashAlgSz,
  24523. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24524. (byte*)cAppTrafficLabel, (word32)XSTRLEN(cAppTrafficLabel),
  24525. tv->hashFinished1, (word32)hashAlgSz, tv->hashAlg);
  24526. if (ret != 0) break;
  24527. ret = XMEMCMP(tv->clientApplicationTrafficSecret, output, hashAlgSz);
  24528. if (ret != 0) break;
  24529. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24530. secret, (word32)hashAlgSz,
  24531. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24532. (byte*)sAppTrafficLabel, (word32)XSTRLEN(sAppTrafficLabel),
  24533. tv->hashFinished1, (word32)hashAlgSz, tv->hashAlg);
  24534. if (ret != 0) break;
  24535. ret = XMEMCMP(tv->serverApplicationTrafficSecret, output, hashAlgSz);
  24536. if (ret != 0) break;
  24537. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24538. secret, (word32)hashAlgSz,
  24539. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24540. (byte*)expMasterLabel, (word32)XSTRLEN(expMasterLabel),
  24541. tv->hashFinished1, (word32)hashAlgSz, tv->hashAlg);
  24542. if (ret != 0) break;
  24543. ret = XMEMCMP(tv->exporterMasterSecret, output, hashAlgSz);
  24544. if (ret != 0) break;
  24545. ret = wc_Tls13_HKDF_Expand_Label(output, (word32)hashAlgSz,
  24546. secret, (word32)hashAlgSz,
  24547. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  24548. (byte*)resMasterLabel, (word32)XSTRLEN(resMasterLabel),
  24549. tv->hashFinished2, (word32)hashAlgSz, tv->hashAlg);
  24550. if (ret != 0) break;
  24551. ret = XMEMCMP(tv->resumptionMasterSecret, output, hashAlgSz);
  24552. if (ret != 0) break;
  24553. }
  24554. return ret;
  24555. }
  24556. #endif /* WOLFSSL_TLS13 */
  24557. static const int fiducial2 = WC_TEST_RET_LN; /* source code reference point --
  24558. * see print_fiducials() below.
  24559. */
  24560. #if defined(HAVE_ECC) && defined(HAVE_X963_KDF)
  24561. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t x963kdf_test(void)
  24562. {
  24563. wc_test_ret_t ret;
  24564. byte kek[128];
  24565. #ifndef NO_SHA
  24566. /* SHA-1, COUNT = 0
  24567. * shared secret length: 192
  24568. * SharedInfo length: 0
  24569. * key data length: 128
  24570. */
  24571. WOLFSSL_SMALL_STACK_STATIC const byte Z[] = {
  24572. 0x1c, 0x7d, 0x7b, 0x5f, 0x05, 0x97, 0xb0, 0x3d,
  24573. 0x06, 0xa0, 0x18, 0x46, 0x6e, 0xd1, 0xa9, 0x3e,
  24574. 0x30, 0xed, 0x4b, 0x04, 0xdc, 0x64, 0xcc, 0xdd
  24575. };
  24576. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  24577. 0xbf, 0x71, 0xdf, 0xfd, 0x8f, 0x4d, 0x99, 0x22,
  24578. 0x39, 0x36, 0xbe, 0xb4, 0x6f, 0xee, 0x8c, 0xcc
  24579. };
  24580. #endif
  24581. #ifndef NO_SHA256
  24582. /* SHA-256, COUNT = 3
  24583. * shared secret length: 192
  24584. * SharedInfo length: 0
  24585. * key data length: 128
  24586. */
  24587. WOLFSSL_SMALL_STACK_STATIC const byte Z2[] = {
  24588. 0xd3, 0x8b, 0xdb, 0xe5, 0xc4, 0xfc, 0x16, 0x4c,
  24589. 0xdd, 0x96, 0x7f, 0x63, 0xc0, 0x4f, 0xe0, 0x7b,
  24590. 0x60, 0xcd, 0xe8, 0x81, 0xc2, 0x46, 0x43, 0x8c
  24591. };
  24592. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  24593. 0x5e, 0x67, 0x4d, 0xb9, 0x71, 0xba, 0xc2, 0x0a,
  24594. 0x80, 0xba, 0xd0, 0xd4, 0x51, 0x4d, 0xc4, 0x84
  24595. };
  24596. #endif
  24597. #ifdef WOLFSSL_SHA512
  24598. /* SHA-512, COUNT = 0
  24599. * shared secret length: 192
  24600. * SharedInfo length: 0
  24601. * key data length: 128
  24602. */
  24603. WOLFSSL_SMALL_STACK_STATIC const byte Z3[] = {
  24604. 0x87, 0xfc, 0x0d, 0x8c, 0x44, 0x77, 0x48, 0x5b,
  24605. 0xb5, 0x74, 0xf5, 0xfc, 0xea, 0x26, 0x4b, 0x30,
  24606. 0x88, 0x5d, 0xc8, 0xd9, 0x0a, 0xd8, 0x27, 0x82
  24607. };
  24608. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  24609. 0x94, 0x76, 0x65, 0xfb, 0xb9, 0x15, 0x21, 0x53,
  24610. 0xef, 0x46, 0x02, 0x38, 0x50, 0x6a, 0x02, 0x45
  24611. };
  24612. /* SHA-512, COUNT = 0
  24613. * shared secret length: 521
  24614. * SharedInfo length: 128
  24615. * key data length: 1024
  24616. */
  24617. WOLFSSL_SMALL_STACK_STATIC const byte Z4[] = {
  24618. 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89,
  24619. 0xfa, 0x58, 0xce, 0xad, 0xc0, 0x47, 0x19, 0x7f,
  24620. 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
  24621. 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8,
  24622. 0x15, 0x07, 0x39, 0x2f, 0x1a, 0x86, 0xdd, 0xfd,
  24623. 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
  24624. 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47,
  24625. 0x47, 0xa9, 0xe2, 0xe7, 0x53, 0xf5, 0x5e, 0xf0,
  24626. 0x5a, 0x2d
  24627. };
  24628. WOLFSSL_SMALL_STACK_STATIC const byte info4[] = {
  24629. 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d,
  24630. 0x2b, 0x3a, 0x2f, 0x99, 0x37, 0x89, 0x5d, 0x31
  24631. };
  24632. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  24633. 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76,
  24634. 0x9b, 0x52, 0x26, 0x4b, 0x01, 0x12, 0xb5, 0x85,
  24635. 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
  24636. 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d,
  24637. 0x6e, 0x4d, 0xd2, 0xa5, 0x99, 0xac, 0xce, 0xb3,
  24638. 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
  24639. 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50,
  24640. 0xa5, 0xa8, 0xee, 0xee, 0x20, 0x80, 0x02, 0x26,
  24641. 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
  24642. 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d,
  24643. 0xea, 0x9c, 0x4f, 0xf7, 0x45, 0xa2, 0x5a, 0xc2,
  24644. 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
  24645. 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc,
  24646. 0x0a, 0xb2, 0xaa, 0x8e, 0x2e, 0xfa, 0x7b, 0x17,
  24647. 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
  24648. 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
  24649. };
  24650. #endif
  24651. WOLFSSL_ENTER("x963kdf_test");
  24652. #ifndef NO_SHA
  24653. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, Z, sizeof(Z), NULL, 0,
  24654. kek, sizeof(verify));
  24655. if (ret != 0)
  24656. return WC_TEST_RET_ENC_EC(ret);
  24657. if (XMEMCMP(verify, kek, sizeof(verify)) != 0)
  24658. return WC_TEST_RET_ENC_NC;
  24659. #endif
  24660. #ifndef NO_SHA256
  24661. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, Z2, sizeof(Z2), NULL, 0,
  24662. kek, sizeof(verify2));
  24663. if (ret != 0)
  24664. return WC_TEST_RET_ENC_EC(ret);
  24665. if (XMEMCMP(verify2, kek, sizeof(verify2)) != 0)
  24666. return WC_TEST_RET_ENC_NC;
  24667. #endif
  24668. #ifdef WOLFSSL_SHA512
  24669. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z3, sizeof(Z3), NULL, 0,
  24670. kek, sizeof(verify3));
  24671. if (ret != 0)
  24672. return WC_TEST_RET_ENC_EC(ret);
  24673. if (XMEMCMP(verify3, kek, sizeof(verify3)) != 0)
  24674. return WC_TEST_RET_ENC_NC;
  24675. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z4, sizeof(Z4), info4,
  24676. sizeof(info4), kek, sizeof(verify4));
  24677. if (ret != 0)
  24678. return WC_TEST_RET_ENC_EC(ret);
  24679. if (XMEMCMP(verify4, kek, sizeof(verify4)) != 0)
  24680. return WC_TEST_RET_ENC_NC;
  24681. #endif
  24682. return 0;
  24683. }
  24684. #endif /* HAVE_X963_KDF */
  24685. #if defined(HAVE_HPKE) && \
  24686. (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)) && \
  24687. defined(HAVE_AESGCM)
  24688. static wc_test_ret_t hpke_test_single(Hpke* hpke)
  24689. {
  24690. wc_test_ret_t ret = 0;
  24691. int rngRet = 0;
  24692. WC_RNG rng[1];
  24693. const char* start_text = "this is a test";
  24694. const char* info_text = "info";
  24695. const char* aad_text = "aad";
  24696. byte ciphertext[MAX_HPKE_LABEL_SZ];
  24697. byte plaintext[MAX_HPKE_LABEL_SZ];
  24698. void* receiverKey = NULL;
  24699. void* ephemeralKey = NULL;
  24700. #ifdef WOLFSSL_SMALL_STACK
  24701. byte *pubKey = NULL; /* public key */
  24702. word16 pubKeySz = (word16)HPKE_Npk_MAX;
  24703. #else
  24704. byte pubKey[HPKE_Npk_MAX]; /* public key */
  24705. word16 pubKeySz = (word16)sizeof(pubKey);
  24706. #endif
  24707. rngRet = ret = wc_InitRng(rng);
  24708. if (ret != 0)
  24709. return WC_TEST_RET_ENC_EC(ret);
  24710. #ifdef WOLFSSL_SMALL_STACK
  24711. if (ret == 0) {
  24712. pubKey = (byte *)XMALLOC(pubKeySz, HEAP_HINT,
  24713. DYNAMIC_TYPE_TMP_BUFFER);
  24714. if (pubKey == NULL)
  24715. ret = WC_TEST_RET_ENC_EC(MEMORY_E);
  24716. }
  24717. #endif
  24718. /* generate the keys */
  24719. if (ret == 0) {
  24720. ret = wc_HpkeGenerateKeyPair(hpke, &ephemeralKey, rng);
  24721. if (ret != 0)
  24722. ret = WC_TEST_RET_ENC_EC(ret);
  24723. }
  24724. if (ret == 0) {
  24725. ret = wc_HpkeGenerateKeyPair(hpke, &receiverKey, rng);
  24726. if (ret != 0)
  24727. ret = WC_TEST_RET_ENC_EC(ret);
  24728. }
  24729. /* seal */
  24730. if (ret == 0) {
  24731. ret = wc_HpkeSealBase(hpke, ephemeralKey, receiverKey,
  24732. (byte*)info_text, (word32)XSTRLEN(info_text),
  24733. (byte*)aad_text, (word32)XSTRLEN(aad_text),
  24734. (byte*)start_text, (word32)XSTRLEN(start_text),
  24735. ciphertext);
  24736. if (ret != 0)
  24737. ret = WC_TEST_RET_ENC_EC(ret);
  24738. }
  24739. /* export ephemeral key */
  24740. if (ret == 0) {
  24741. ret = wc_HpkeSerializePublicKey(hpke, ephemeralKey, pubKey, &pubKeySz);
  24742. if (ret != 0)
  24743. ret = WC_TEST_RET_ENC_EC(ret);
  24744. }
  24745. /* open with exported ephemeral key */
  24746. if (ret == 0) {
  24747. ret = wc_HpkeOpenBase(hpke, receiverKey, pubKey, pubKeySz,
  24748. (byte*)info_text, (word32)XSTRLEN(info_text),
  24749. (byte*)aad_text, (word32)XSTRLEN(aad_text),
  24750. ciphertext, (word32)XSTRLEN(start_text),
  24751. plaintext);
  24752. if (ret != 0)
  24753. ret = WC_TEST_RET_ENC_EC(ret);
  24754. }
  24755. if (ret == 0) {
  24756. ret = XMEMCMP(plaintext, start_text, XSTRLEN(start_text));
  24757. if (ret != 0)
  24758. ret = WC_TEST_RET_ENC_NC;
  24759. }
  24760. /* Negative test case with NULL argument */
  24761. if (ret == 0) {
  24762. ret = wc_HpkeGenerateKeyPair(NULL, &receiverKey, rng);
  24763. if (ret != BAD_FUNC_ARG)
  24764. ret = WC_TEST_RET_ENC_EC(ret);
  24765. else
  24766. ret = 0;
  24767. }
  24768. if (ret == 0) {
  24769. ret = wc_HpkeGenerateKeyPair(hpke, NULL, rng);
  24770. if (ret != BAD_FUNC_ARG)
  24771. ret = WC_TEST_RET_ENC_EC(ret);
  24772. else
  24773. ret = 0;
  24774. }
  24775. if (ret == 0) {
  24776. ret = wc_HpkeGenerateKeyPair(hpke, &receiverKey, NULL);
  24777. if (ret != BAD_FUNC_ARG)
  24778. ret = WC_TEST_RET_ENC_EC(ret);
  24779. else
  24780. ret = 0;
  24781. }
  24782. if (ephemeralKey != NULL)
  24783. wc_HpkeFreeKey(hpke, hpke->kem, ephemeralKey, hpke->heap);
  24784. if (receiverKey != NULL)
  24785. wc_HpkeFreeKey(hpke, hpke->kem, receiverKey, hpke->heap);
  24786. #ifdef WOLFSSL_SMALL_STACK
  24787. if (pubKey != NULL)
  24788. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24789. #endif
  24790. if (rngRet == 0)
  24791. wc_FreeRng(rng);
  24792. return ret;
  24793. }
  24794. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hpke_test(void)
  24795. {
  24796. wc_test_ret_t ret = 0;
  24797. Hpke hpke[1];
  24798. WOLFSSL_ENTER("hpke_test");
  24799. #if defined(HAVE_ECC)
  24800. #if defined(WOLFSSL_SHA224) || !defined(NO_SHA256)
  24801. /* p256 */
  24802. ret = wc_HpkeInit(hpke, DHKEM_P256_HKDF_SHA256, HKDF_SHA256,
  24803. HPKE_AES_128_GCM, NULL);
  24804. if (ret != 0)
  24805. return WC_TEST_RET_ENC_EC(ret);
  24806. ret = hpke_test_single(hpke);
  24807. if (ret != 0)
  24808. return ret;
  24809. #endif
  24810. #if defined(WOLFSSL_SHA384) && \
  24811. (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  24812. /* p384 */
  24813. ret = wc_HpkeInit(hpke, DHKEM_P384_HKDF_SHA384, HKDF_SHA384,
  24814. HPKE_AES_128_GCM, NULL);
  24815. if (ret != 0)
  24816. return WC_TEST_RET_ENC_EC(ret);
  24817. ret = hpke_test_single(hpke);
  24818. if (ret != 0)
  24819. return ret;
  24820. #endif
  24821. #if (defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)) && \
  24822. (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  24823. /* p521 */
  24824. ret = wc_HpkeInit(hpke, DHKEM_P521_HKDF_SHA512, HKDF_SHA512,
  24825. HPKE_AES_128_GCM, NULL);
  24826. if (ret != 0)
  24827. return WC_TEST_RET_ENC_EC(ret);
  24828. ret = hpke_test_single(hpke);
  24829. if (ret != 0)
  24830. return ret;
  24831. #endif
  24832. #endif
  24833. #if defined(HAVE_CURVE25519)
  24834. /* test with curve25519 and aes256 */
  24835. ret = wc_HpkeInit(hpke, DHKEM_X25519_HKDF_SHA256, HKDF_SHA256,
  24836. HPKE_AES_256_GCM, NULL);
  24837. if (ret != 0)
  24838. return WC_TEST_RET_ENC_EC(ret);
  24839. ret = hpke_test_single(hpke);
  24840. if (ret != 0)
  24841. return ret;
  24842. #endif
  24843. #if defined(HAVE_CURVE448) && \
  24844. (defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512))
  24845. /* test with curve448 and aes256 */
  24846. ret = wc_HpkeInit(hpke, DHKEM_X448_HKDF_SHA512, HKDF_SHA512,
  24847. HPKE_AES_256_GCM, NULL);
  24848. /* HPKE does not support X448 yet, so expect failure */
  24849. if (ret != BAD_FUNC_ARG)
  24850. return WC_TEST_RET_ENC_EC(ret);
  24851. ret = hpke_test_single(hpke);
  24852. /* HPKE does not support X448 yet, so expect failure */
  24853. if (WC_TEST_RET_DEC_EC(ret) != BAD_FUNC_ARG)
  24854. return ret;
  24855. ret = 0; /* reset error code */
  24856. #endif
  24857. /* TODO: HPKE chacha20 is not implemented */
  24858. return ret;
  24859. }
  24860. #endif /* HAVE_HPKE && HAVE_ECC && HAVE_AESGCM */
  24861. #if defined(WC_SRTP_KDF)
  24862. typedef struct Srtp_Kdf_Tv {
  24863. const unsigned char* key;
  24864. word32 keySz;
  24865. const unsigned char* salt;
  24866. word32 saltSz;
  24867. int kdfIdx;
  24868. const unsigned char* index;
  24869. const unsigned char* ke;
  24870. const unsigned char* ka;
  24871. const unsigned char* ks;
  24872. const unsigned char* index_c;
  24873. const unsigned char* ke_c;
  24874. const unsigned char* ka_c;
  24875. const unsigned char* ks_c;
  24876. word32 keSz;
  24877. word32 kaSz;
  24878. word32 ksSz;
  24879. } Srtp_Kdf_Tv;
  24880. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srtpkdf_test(void)
  24881. {
  24882. wc_test_ret_t ret = 0;
  24883. /* 128-bit key, kdrIdx = -1 */
  24884. WOLFSSL_SMALL_STACK_STATIC const byte key_0[] = {
  24885. 0xc4, 0x80, 0x9f, 0x6d, 0x36, 0x98, 0x88, 0x72,
  24886. 0x8e, 0x26, 0xad, 0xb5, 0x32, 0x12, 0x98, 0x90
  24887. };
  24888. WOLFSSL_SMALL_STACK_STATIC const byte salt_0[] = {
  24889. 0x0e, 0x23, 0x00, 0x6c, 0x6c, 0x04, 0x4f, 0x56,
  24890. 0x62, 0x40, 0x0e, 0x9d, 0x1b, 0xd6
  24891. };
  24892. WOLFSSL_SMALL_STACK_STATIC const byte index_0[] = {
  24893. 0x48, 0x71, 0x65, 0x64, 0x9c, 0xca
  24894. };
  24895. WOLFSSL_SMALL_STACK_STATIC const byte ke_0[] = {
  24896. 0xdc, 0x38, 0x21, 0x92, 0xab, 0x65, 0x10, 0x8a,
  24897. 0x86, 0xb2, 0x59, 0xb6, 0x1b, 0x3a, 0xf4, 0x6f
  24898. };
  24899. WOLFSSL_SMALL_STACK_STATIC const byte ka_0[] = {
  24900. 0xb8, 0x39, 0x37, 0xfb, 0x32, 0x17, 0x92, 0xee,
  24901. 0x87, 0xb7, 0x88, 0x19, 0x3b, 0xe5, 0xa4, 0xe3,
  24902. 0xbd, 0x32, 0x6e, 0xe4
  24903. };
  24904. WOLFSSL_SMALL_STACK_STATIC const byte ks_0[] = {
  24905. 0xf1, 0xc0, 0x35, 0xc0, 0x0b, 0x5a, 0x54, 0xa6,
  24906. 0x16, 0x92, 0xc0, 0x16, 0x27, 0x6c
  24907. };
  24908. WOLFSSL_SMALL_STACK_STATIC const byte index_c_0[] = {
  24909. 0x56, 0xf3, 0xf1, 0x97
  24910. };
  24911. WOLFSSL_SMALL_STACK_STATIC const byte ke_c_0[] = {
  24912. 0xab, 0x5b, 0xe0, 0xb4, 0x56, 0x23, 0x5d, 0xcf,
  24913. 0x77, 0xd5, 0x08, 0x69, 0x29, 0xba, 0xfb, 0x38
  24914. };
  24915. WOLFSSL_SMALL_STACK_STATIC const byte ka_c_0[] = {
  24916. 0xc5, 0x2f, 0xde, 0x0b, 0x80, 0xb0, 0xf0, 0xba,
  24917. 0xd8, 0xd1, 0x56, 0x45, 0xcb, 0x86, 0xe7, 0xc7,
  24918. 0xc3, 0xd8, 0x77, 0x0e
  24919. };
  24920. WOLFSSL_SMALL_STACK_STATIC const byte ks_c_0[] = {
  24921. 0xde, 0xb5, 0xf8, 0x5f, 0x81, 0x33, 0x6a, 0x96,
  24922. 0x5e, 0xd3, 0x2b, 0xb7, 0xed, 0xe8
  24923. };
  24924. /* 192-bit key, kdrIdx = 0 */
  24925. WOLFSSL_SMALL_STACK_STATIC const byte key_1[] = {
  24926. 0xbb, 0x04, 0x5b, 0x1f, 0x53, 0xc6, 0x93, 0x2c,
  24927. 0x2b, 0xa6, 0x88, 0xf5, 0xe3, 0xf2, 0x24, 0x70,
  24928. 0xe1, 0x7d, 0x7d, 0xec, 0x8a, 0x93, 0x4d, 0xf2
  24929. };
  24930. WOLFSSL_SMALL_STACK_STATIC const byte salt_1[] = {
  24931. 0xe7, 0x22, 0xab, 0x92, 0xfc, 0x7c, 0x89, 0xb6,
  24932. 0x53, 0x8a, 0xf9, 0x3c, 0xb9, 0x52
  24933. };
  24934. WOLFSSL_SMALL_STACK_STATIC const byte index_1[] = {
  24935. 0xd7, 0x87, 0x8f, 0x33, 0xb1, 0x76
  24936. };
  24937. WOLFSSL_SMALL_STACK_STATIC const byte ke_1[] = {
  24938. 0x2c, 0xc8, 0x3e, 0x54, 0xb2, 0x33, 0x89, 0xb3,
  24939. 0x71, 0x65, 0x0f, 0x51, 0x61, 0x65, 0xe4, 0x93,
  24940. 0x07, 0x4e, 0xb3, 0x47, 0xba, 0x2d, 0x60, 0x60
  24941. };
  24942. WOLFSSL_SMALL_STACK_STATIC const byte ka_1[] = {
  24943. 0x2e, 0x80, 0xe4, 0x82, 0x55, 0xa2, 0xbe, 0x6d,
  24944. 0xe0, 0x46, 0xcc, 0xc1, 0x75, 0x78, 0x6e, 0x78,
  24945. 0xd1, 0xd1, 0x47, 0x08
  24946. };
  24947. WOLFSSL_SMALL_STACK_STATIC const byte ks_1[] = {
  24948. 0xe0, 0xc1, 0xe6, 0xaf, 0x1e, 0x8d, 0x8c, 0xfe,
  24949. 0xe5, 0x60, 0x70, 0xb5, 0xe6, 0xea
  24950. };
  24951. WOLFSSL_SMALL_STACK_STATIC const byte index_c_1[] = {
  24952. 0x40, 0xbf, 0xd4, 0xa9
  24953. };
  24954. WOLFSSL_SMALL_STACK_STATIC const byte ke_c_1[] = {
  24955. 0x94, 0x0f, 0x55, 0xce, 0x58, 0xd8, 0x16, 0x65,
  24956. 0xf0, 0xfa, 0x46, 0x40, 0x0c, 0xda, 0xb1, 0x11,
  24957. 0x9e, 0x69, 0xa0, 0x93, 0x4e, 0xd7, 0xf2, 0x84
  24958. };
  24959. WOLFSSL_SMALL_STACK_STATIC const byte ka_c_1[] = {
  24960. 0xf5, 0x41, 0x6f, 0xc2, 0x65, 0xc5, 0xb3, 0xef,
  24961. 0xbb, 0x22, 0xc8, 0xfc, 0x6b, 0x00, 0x14, 0xb2,
  24962. 0xf3, 0x3b, 0x8e, 0x29
  24963. };
  24964. WOLFSSL_SMALL_STACK_STATIC const byte ks_c_1[] = {
  24965. 0x35, 0xb7, 0x42, 0x43, 0xf0, 0x01, 0x01, 0xb4,
  24966. 0x68, 0xa1, 0x28, 0x80, 0x37, 0xf0
  24967. };
  24968. /* 256-bit key, kdrIdx = 1 */
  24969. WOLFSSL_SMALL_STACK_STATIC const byte key_2[] = {
  24970. 0x10, 0x38, 0x0a, 0xcd, 0xd6, 0x47, 0xab, 0xee,
  24971. 0xc0, 0xd4, 0x44, 0xf4, 0x7e, 0x51, 0x36, 0x02,
  24972. 0x79, 0xa8, 0x94, 0x80, 0x35, 0x40, 0xed, 0x50,
  24973. 0xf4, 0x45, 0x30, 0x3d, 0xb5, 0xf0, 0x2b, 0xbb
  24974. };
  24975. WOLFSSL_SMALL_STACK_STATIC const byte salt_2[] = {
  24976. 0xc7, 0x31, 0xf2, 0xc8, 0x40, 0x43, 0xb8, 0x74,
  24977. 0x8a, 0x61, 0x84, 0x7a, 0x25, 0x8a
  24978. };
  24979. WOLFSSL_SMALL_STACK_STATIC const byte index_2[] = {
  24980. 0x82, 0xf1, 0x84, 0x8c, 0xac, 0x42
  24981. };
  24982. WOLFSSL_SMALL_STACK_STATIC const byte ke_2[] = {
  24983. 0xb2, 0x26, 0x60, 0xaf, 0x08, 0x23, 0x14, 0x98,
  24984. 0x91, 0xde, 0x5d, 0x87, 0x95, 0x61, 0xca, 0x8f,
  24985. 0x0e, 0xce, 0xfb, 0x68, 0x4d, 0xd6, 0x28, 0xcb,
  24986. 0x28, 0xe2, 0x27, 0x20, 0x2d, 0xff, 0x64, 0xbb
  24987. };
  24988. WOLFSSL_SMALL_STACK_STATIC const byte ka_2[] = {
  24989. 0x12, 0x6f, 0x52, 0xe8, 0x07, 0x7f, 0x07, 0x84,
  24990. 0xa0, 0x61, 0x96, 0xf8, 0xee, 0x4d, 0x05, 0x57,
  24991. 0x65, 0xc7, 0x50, 0xc1
  24992. };
  24993. WOLFSSL_SMALL_STACK_STATIC const byte ks_2[] = {
  24994. 0x18, 0x5a, 0x59, 0xe5, 0x91, 0x4d, 0xc9, 0x6c,
  24995. 0xfa, 0x5b, 0x36, 0x06, 0x8c, 0x9a
  24996. };
  24997. WOLFSSL_SMALL_STACK_STATIC const byte index_c_2[] = {
  24998. 0x31, 0x2d, 0x58, 0x15
  24999. };
  25000. WOLFSSL_SMALL_STACK_STATIC const byte ke_c_2[] = {
  25001. 0x14, 0xf2, 0xc8, 0x25, 0x02, 0x79, 0x22, 0xa1,
  25002. 0x96, 0xb6, 0xf7, 0x07, 0x76, 0xa6, 0xa3, 0xc4,
  25003. 0x37, 0xdf, 0xa0, 0xf8, 0x78, 0x93, 0x2c, 0xfa,
  25004. 0xea, 0x35, 0xf0, 0xf3, 0x3f, 0x32, 0x6e, 0xfd
  25005. };
  25006. WOLFSSL_SMALL_STACK_STATIC const byte ka_c_2[] = {
  25007. 0x6e, 0x3d, 0x4a, 0x99, 0xea, 0x2f, 0x9d, 0x13,
  25008. 0x4a, 0x1e, 0x71, 0x2e, 0x15, 0xc0, 0xca, 0xb6,
  25009. 0x35, 0x78, 0xdf, 0xa4
  25010. };
  25011. WOLFSSL_SMALL_STACK_STATIC const byte ks_c_2[] = {
  25012. 0xae, 0xe4, 0xec, 0x18, 0x31, 0x70, 0x5d, 0x3f,
  25013. 0xdc, 0x97, 0x89, 0x88, 0xfd, 0xff
  25014. };
  25015. /* 128-bit key, kdrIdx = 8 */
  25016. WOLFSSL_SMALL_STACK_STATIC const byte key_3[] = {
  25017. 0x36, 0xb4, 0xde, 0xcb, 0x2e, 0x51, 0x23, 0x76,
  25018. 0xe0, 0x27, 0x7e, 0x3e, 0xc8, 0xf6, 0x54, 0x04
  25019. };
  25020. WOLFSSL_SMALL_STACK_STATIC const byte salt_3[] = {
  25021. 0x73, 0x26, 0xf4, 0x3f, 0xc0, 0xd9, 0xc6, 0xe3,
  25022. 0x2f, 0x92, 0x7d, 0x46, 0x12, 0x76
  25023. };
  25024. WOLFSSL_SMALL_STACK_STATIC const byte index_3[] = {
  25025. 0x44, 0x73, 0xb2, 0x2d, 0xb2, 0x60
  25026. };
  25027. WOLFSSL_SMALL_STACK_STATIC const byte ke_3[] = {
  25028. 0x79, 0x91, 0x3d, 0x7b, 0x20, 0x5d, 0xea, 0xe2,
  25029. 0xeb, 0x46, 0x89, 0x68, 0x5a, 0x06, 0x73, 0x74
  25030. };
  25031. WOLFSSL_SMALL_STACK_STATIC const byte ka_3[] = {
  25032. 0x2d, 0x2e, 0x97, 0x4e, 0x76, 0x8c, 0x62, 0xa6,
  25033. 0x57, 0x80, 0x13, 0x42, 0x0b, 0x51, 0xa7, 0x66,
  25034. 0xea, 0x31, 0x24, 0xe6
  25035. };
  25036. WOLFSSL_SMALL_STACK_STATIC const byte ks_3[] = {
  25037. 0xcc, 0xd7, 0x31, 0xf6, 0x3b, 0xf3, 0x89, 0x8a,
  25038. 0x5b, 0x7b, 0xb5, 0x8b, 0x4c, 0x3f
  25039. };
  25040. WOLFSSL_SMALL_STACK_STATIC const byte index_c_3[] = {
  25041. 0x4a, 0x7d, 0xaa, 0x85
  25042. };
  25043. WOLFSSL_SMALL_STACK_STATIC const byte ke_c_3[] = {
  25044. 0x34, 0x99, 0x71, 0xfe, 0x12, 0x93, 0xae, 0x8c,
  25045. 0x4a, 0xe9, 0x84, 0xe4, 0x93, 0x53, 0x63, 0x88
  25046. };
  25047. WOLFSSL_SMALL_STACK_STATIC const byte ka_c_3[] = {
  25048. 0xa4, 0x53, 0x5e, 0x0a, 0x9c, 0xf2, 0xce, 0x13,
  25049. 0xef, 0x7a, 0x13, 0xee, 0x0a, 0xef, 0xba, 0x17,
  25050. 0x05, 0x18, 0xe3, 0xed
  25051. };
  25052. WOLFSSL_SMALL_STACK_STATIC const byte ks_c_3[] = {
  25053. 0xe1, 0x29, 0x4f, 0x61, 0x30, 0x3c, 0x4d, 0x46,
  25054. 0x5f, 0x5c, 0x81, 0x3c, 0x38, 0xb6
  25055. };
  25056. /* SRTCP w/ 48-bit idx - KDR 0 (-1) */
  25057. WOLFSSL_SMALL_STACK_STATIC const byte mk48_1[] = {
  25058. 0xFF, 0xB6, 0xCB, 0x09, 0x71, 0x3F, 0x63, 0x4D,
  25059. 0x7F, 0x42, 0xED, 0xA8, 0x12, 0x81, 0x50, 0xE6
  25060. };
  25061. WOLFSSL_SMALL_STACK_STATIC const byte ms48_1[] = {
  25062. 0x1F, 0x04, 0x76, 0xC8, 0x7F, 0x58, 0x23, 0xEF,
  25063. 0xD3, 0x57, 0xB2, 0xBD, 0xF1, 0x32
  25064. };
  25065. WOLFSSL_SMALL_STACK_STATIC const byte srtcp48idx_1[] = {
  25066. 0x00, 0x00, 0x08, 0x56, 0xBC, 0x39
  25067. };
  25068. WOLFSSL_SMALL_STACK_STATIC const byte srtcpKe_48_1[] = {
  25069. 0xD2, 0xC3, 0xF3, 0x49, 0x00, 0x1A, 0x18, 0x0F,
  25070. 0xB6, 0x05, 0x5A, 0x5A, 0x67, 0x8E, 0xE5, 0xB2
  25071. };
  25072. WOLFSSL_SMALL_STACK_STATIC const byte srtcpKa_48_1[] = {
  25073. 0x8D, 0x54, 0xBE, 0xB5, 0x7B, 0x7F, 0x7A, 0xAB,
  25074. 0xF5, 0x46, 0xCE, 0x5B, 0x45, 0x69, 0x4A, 0x75,
  25075. 0x81, 0x2A, 0xE2, 0xCB
  25076. };
  25077. WOLFSSL_SMALL_STACK_STATIC const byte srtcpKs_48_1[] = {
  25078. 0x76, 0x3C, 0x97, 0x6A, 0x45, 0x31, 0xA7, 0x79,
  25079. 0x3C, 0x28, 0x4A, 0xA6, 0x82, 0x03
  25080. };
  25081. /* SRTCP w/ 48-bit idx - KDR 19 */
  25082. WOLFSSL_SMALL_STACK_STATIC const byte mk48_2[] = {
  25083. 0xBD, 0x1D, 0x71, 0x6B, 0xDA, 0x28, 0xE3, 0xFC,
  25084. 0xA5, 0xA0, 0x66, 0x3F, 0x2E, 0x34, 0xA8, 0x58
  25085. };
  25086. WOLFSSL_SMALL_STACK_STATIC const byte ms48_2[] = {
  25087. 0x79, 0x06, 0xE5, 0xAB, 0x5C, 0x2B, 0x1B, 0x69,
  25088. 0xFA, 0xEE, 0xD2, 0x29, 0x57, 0x3C
  25089. };
  25090. WOLFSSL_SMALL_STACK_STATIC const byte srtcp48idx_2[] = {
  25091. 0x00, 0x00, 0x59, 0xD0, 0xC2, 0xE8
  25092. };
  25093. WOLFSSL_SMALL_STACK_STATIC const byte srtcpKe_48_2[] = {
  25094. 0xB9, 0xD7, 0xAD, 0xD8, 0x90, 0x94, 0xC2, 0x92,
  25095. 0xA5, 0x04, 0x87, 0xC4, 0x8C, 0xEF, 0xE2, 0xA3
  25096. };
  25097. WOLFSSL_SMALL_STACK_STATIC const byte srtcpKa_48_2[] = {
  25098. 0x07, 0xD5, 0xC4, 0xD2, 0x06, 0xFB, 0x63, 0x15,
  25099. 0xC2, 0x9C, 0x7F, 0x55, 0xD1, 0x16, 0x5C, 0xB5,
  25100. 0xB7, 0x44, 0x54, 0xBD
  25101. };
  25102. WOLFSSL_SMALL_STACK_STATIC const byte srtcpKs_48_2[] = {
  25103. 0x0C, 0x5E, 0x53, 0xC1, 0xD0, 0x75, 0xAD, 0x65,
  25104. 0xBF, 0x51, 0x74, 0x50, 0x89, 0xD7
  25105. };
  25106. int kdr_48_1 = -1;
  25107. int kdr_48_2 = 19;
  25108. #define SRTP_TV_CNT 4
  25109. Srtp_Kdf_Tv tv[SRTP_TV_CNT] = {
  25110. { key_0, (word32)sizeof(key_0), salt_0, (word32)sizeof(salt_0), -1,
  25111. index_0, ke_0, ka_0, ks_0, index_c_0, ke_c_0, ka_c_0, ks_c_0,
  25112. 16, 20, 14 },
  25113. { key_1, (word32)sizeof(key_1), salt_1, (word32)sizeof(salt_1), 0,
  25114. index_1, ke_1, ka_1, ks_1, index_c_1, ke_c_1, ka_c_1, ks_c_1,
  25115. 24, 20, 14 },
  25116. { key_2, (word32)sizeof(key_2), salt_2, (word32)sizeof(salt_2), 1,
  25117. index_2, ke_2, ka_2, ks_2, index_c_2, ke_c_2, ka_c_2, ks_c_2,
  25118. 32, 20, 14 },
  25119. { key_3, (word32)sizeof(key_3), salt_3, (word32)sizeof(salt_3), 8,
  25120. index_3, ke_3, ka_3, ks_3, index_c_3, ke_c_3, ka_c_3, ks_c_3,
  25121. 16, 20, 14 },
  25122. };
  25123. int i;
  25124. int idx;
  25125. unsigned char keyE[32];
  25126. unsigned char keyA[20];
  25127. unsigned char keyS[14];
  25128. WOLFSSL_ENTER("srtpkdf_test");
  25129. for (i = 0; (ret == 0) && (i < SRTP_TV_CNT); i++) {
  25130. #ifndef WOLFSSL_AES_128
  25131. if (tv[i].keySz == AES_128_KEY_SIZE) {
  25132. continue;
  25133. }
  25134. #endif
  25135. #ifndef WOLFSSL_AES_192
  25136. if (tv[i].keySz == AES_192_KEY_SIZE) {
  25137. continue;
  25138. }
  25139. #endif
  25140. #ifndef WOLFSSL_AES_256
  25141. if (tv[i].keySz == AES_256_KEY_SIZE) {
  25142. continue;
  25143. }
  25144. #endif
  25145. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25146. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25147. keyS, tv[i].ksSz);
  25148. if (ret != 0)
  25149. return WC_TEST_RET_ENC_EC(ret);
  25150. if (XMEMCMP(keyE, tv[i].ke, tv[i].keSz) != 0)
  25151. return WC_TEST_RET_ENC_NC;
  25152. if (XMEMCMP(keyA, tv[i].ka, tv[i].kaSz) != 0)
  25153. return WC_TEST_RET_ENC_NC;
  25154. if (XMEMCMP(keyS, tv[i].ks, tv[i].ksSz) != 0)
  25155. return WC_TEST_RET_ENC_NC;
  25156. ret = wc_SRTP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  25157. tv[i].saltSz, tv[i].kdfIdx, tv[i].index, WC_SRTP_LABEL_ENCRYPTION,
  25158. keyE, tv[i].keSz);
  25159. if (ret != 0)
  25160. return WC_TEST_RET_ENC_EC(ret);
  25161. if (XMEMCMP(keyE, tv[i].ke, tv[i].keSz) != 0)
  25162. return WC_TEST_RET_ENC_NC;
  25163. ret = wc_SRTP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  25164. tv[i].saltSz, tv[i].kdfIdx, tv[i].index, WC_SRTP_LABEL_MSG_AUTH,
  25165. keyA, tv[i].kaSz);
  25166. if (ret != 0)
  25167. return WC_TEST_RET_ENC_EC(ret);
  25168. if (XMEMCMP(keyA, tv[i].ka, tv[i].kaSz) != 0)
  25169. return WC_TEST_RET_ENC_NC;
  25170. ret = wc_SRTP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  25171. tv[i].saltSz, tv[i].kdfIdx, tv[i].index, WC_SRTP_LABEL_SALT, keyS,
  25172. tv[i].ksSz);
  25173. if (ret != 0)
  25174. return WC_TEST_RET_ENC_EC(ret);
  25175. if (XMEMCMP(keyS, tv[i].ks, tv[i].ksSz) != 0)
  25176. return WC_TEST_RET_ENC_NC;
  25177. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25178. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25179. keyS, tv[i].ksSz);
  25180. if (ret != 0)
  25181. return WC_TEST_RET_ENC_EC(ret);
  25182. if (XMEMCMP(keyE, tv[i].ke_c, tv[i].keSz) != 0)
  25183. return WC_TEST_RET_ENC_NC;
  25184. if (XMEMCMP(keyA, tv[i].ka_c, tv[i].kaSz) != 0)
  25185. return WC_TEST_RET_ENC_NC;
  25186. if (XMEMCMP(keyS, tv[i].ks_c, tv[i].ksSz) != 0)
  25187. return WC_TEST_RET_ENC_NC;
  25188. ret = wc_SRTCP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  25189. tv[i].saltSz, tv[i].kdfIdx, tv[i].index_c,
  25190. WC_SRTCP_LABEL_ENCRYPTION, keyE, tv[i].keSz);
  25191. if (ret != 0)
  25192. return WC_TEST_RET_ENC_EC(ret);
  25193. if (XMEMCMP(keyE, tv[i].ke_c, tv[i].keSz) != 0)
  25194. return WC_TEST_RET_ENC_NC;
  25195. ret = wc_SRTCP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  25196. tv[i].saltSz, tv[i].kdfIdx, tv[i].index_c, WC_SRTCP_LABEL_MSG_AUTH,
  25197. keyA, tv[i].kaSz);
  25198. if (ret != 0)
  25199. return WC_TEST_RET_ENC_EC(ret);
  25200. if (XMEMCMP(keyA, tv[i].ka_c, tv[i].kaSz) != 0)
  25201. return WC_TEST_RET_ENC_NC;
  25202. ret = wc_SRTCP_KDF_label(tv[i].key, tv[i].keySz, tv[i].salt,
  25203. tv[i].saltSz, tv[i].kdfIdx, tv[i].index_c, WC_SRTCP_LABEL_SALT,
  25204. keyS, tv[i].ksSz);
  25205. if (ret != 0)
  25206. return WC_TEST_RET_ENC_EC(ret);
  25207. if (XMEMCMP(keyS, tv[i].ks_c, tv[i].ksSz) != 0)
  25208. return WC_TEST_RET_ENC_NC;
  25209. }
  25210. #ifdef WOLFSSL_AES_128
  25211. i = 0;
  25212. #elif defined(WOLFSSL_AES_192)
  25213. i = 1;
  25214. #else
  25215. i = 2;
  25216. #endif
  25217. ret = wc_SRTP_KDF(tv[i].key, 33, tv[i].salt, tv[i].saltSz,
  25218. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25219. keyS, tv[i].ksSz);
  25220. if (ret != BAD_FUNC_ARG)
  25221. return WC_TEST_RET_ENC_EC(ret);
  25222. ret = wc_SRTCP_KDF(tv[i].key, 33, tv[i].salt, tv[i].saltSz,
  25223. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25224. keyS, tv[i].ksSz);
  25225. if (ret != BAD_FUNC_ARG)
  25226. return WC_TEST_RET_ENC_EC(ret);
  25227. ret = wc_SRTP_KDF(tv[i].key, 15, tv[i].salt, tv[i].saltSz,
  25228. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25229. keyS, tv[i].ksSz);
  25230. if (ret != BAD_FUNC_ARG)
  25231. return WC_TEST_RET_ENC_EC(ret);
  25232. ret = wc_SRTCP_KDF(tv[i].key, 15, tv[i].salt, tv[i].saltSz,
  25233. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25234. keyS, tv[i].ksSz);
  25235. if (ret != BAD_FUNC_ARG)
  25236. return WC_TEST_RET_ENC_EC(ret);
  25237. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, 15,
  25238. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25239. keyS, tv[i].ksSz);
  25240. if (ret != BAD_FUNC_ARG)
  25241. return WC_TEST_RET_ENC_EC(ret);
  25242. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, 15,
  25243. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25244. keyS, tv[i].ksSz);
  25245. if (ret != BAD_FUNC_ARG)
  25246. return WC_TEST_RET_ENC_EC(ret);
  25247. ret = wc_SRTP_KDF(NULL, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25248. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25249. keyS, tv[i].ksSz);
  25250. if (ret != BAD_FUNC_ARG)
  25251. return WC_TEST_RET_ENC_EC(ret);
  25252. ret = wc_SRTCP_KDF(NULL, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25253. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25254. keyS, tv[i].ksSz);
  25255. if (ret != BAD_FUNC_ARG)
  25256. return WC_TEST_RET_ENC_EC(ret);
  25257. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, NULL, tv[i].saltSz,
  25258. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25259. keyS, tv[i].ksSz);
  25260. if (ret != BAD_FUNC_ARG)
  25261. return WC_TEST_RET_ENC_EC(ret);
  25262. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, NULL, tv[i].saltSz,
  25263. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25264. keyS, tv[i].ksSz);
  25265. if (ret != BAD_FUNC_ARG)
  25266. return WC_TEST_RET_ENC_EC(ret);
  25267. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25268. 25, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25269. keyS, tv[i].ksSz);
  25270. if (ret != BAD_FUNC_ARG)
  25271. return WC_TEST_RET_ENC_EC(ret);
  25272. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25273. 25, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25274. keyS, tv[i].ksSz);
  25275. if (ret != BAD_FUNC_ARG)
  25276. return WC_TEST_RET_ENC_EC(ret);
  25277. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25278. -2, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25279. keyS, tv[i].ksSz);
  25280. if (ret != BAD_FUNC_ARG)
  25281. return WC_TEST_RET_ENC_EC(ret);
  25282. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25283. -2, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25284. keyS, tv[i].ksSz);
  25285. if (ret != BAD_FUNC_ARG)
  25286. return WC_TEST_RET_ENC_EC(ret);
  25287. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25288. tv[i].kdfIdx, tv[i].index, NULL, tv[i].keSz, keyA, tv[i].kaSz,
  25289. keyS, tv[i].ksSz);
  25290. if (ret != 0)
  25291. return WC_TEST_RET_ENC_EC(ret);
  25292. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25293. tv[i].kdfIdx, tv[i].index_c, NULL, tv[i].keSz, keyA, tv[i].kaSz,
  25294. keyS, tv[i].ksSz);
  25295. if (ret != 0)
  25296. return WC_TEST_RET_ENC_EC(ret);
  25297. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25298. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, NULL, tv[i].kaSz,
  25299. keyS, tv[i].ksSz);
  25300. if (ret != 0)
  25301. return WC_TEST_RET_ENC_EC(ret);
  25302. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25303. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, NULL, tv[i].kaSz,
  25304. keyS, tv[i].ksSz);
  25305. if (ret != 0)
  25306. return WC_TEST_RET_ENC_EC(ret);
  25307. ret = wc_SRTP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25308. tv[i].kdfIdx, tv[i].index, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25309. NULL, tv[i].ksSz);
  25310. if (ret != 0)
  25311. return WC_TEST_RET_ENC_EC(ret);
  25312. ret = wc_SRTCP_KDF(tv[i].key, tv[i].keySz, tv[i].salt, tv[i].saltSz,
  25313. tv[i].kdfIdx, tv[i].index_c, keyE, tv[i].keSz, keyA, tv[i].kaSz,
  25314. NULL, tv[i].ksSz);
  25315. if (ret != 0)
  25316. return WC_TEST_RET_ENC_EC(ret);
  25317. idx = wc_SRTP_KDF_kdr_to_idx(0);
  25318. if (idx != -1)
  25319. return WC_TEST_RET_ENC_NC;
  25320. for (i = 0; i < 32; i++) {
  25321. word32 kdr = 1U << i;
  25322. /* SRTCP w/ 48-bit IDX, 128-bit key test */
  25323. if (i == 0) {
  25324. ret = wc_SRTCP_KDF_ex(mk48_1, (word32)sizeof(mk48_1),
  25325. ms48_1, (word32)sizeof(ms48_1),
  25326. kdr_48_1, srtcp48idx_1, keyE, tv[i].keSz,
  25327. keyA, tv[i].kaSz, keyS, tv[i].ksSz,
  25328. WC_SRTCP_48BIT_IDX);
  25329. if (ret != 0)
  25330. return WC_TEST_RET_ENC_EC(ret);
  25331. if (XMEMCMP(keyE, srtcpKe_48_1, tv[i].keSz) != 0)
  25332. return WC_TEST_RET_ENC_NC;
  25333. if (XMEMCMP(keyA, srtcpKa_48_1, tv[i].kaSz) != 0)
  25334. return WC_TEST_RET_ENC_NC;
  25335. if (XMEMCMP(keyS, srtcpKs_48_1, tv[i].ksSz) != 0)
  25336. return WC_TEST_RET_ENC_NC;
  25337. ret = wc_SRTCP_KDF_ex(mk48_2, (word32)sizeof(mk48_2),
  25338. ms48_2, (word32)sizeof(ms48_2),
  25339. kdr_48_2, srtcp48idx_2, keyE, tv[i].keSz,
  25340. keyA, tv[i].kaSz, keyS, tv[i].ksSz,
  25341. WC_SRTCP_48BIT_IDX);
  25342. if (ret != 0)
  25343. return WC_TEST_RET_ENC_EC(ret);
  25344. if (XMEMCMP(keyE, srtcpKe_48_2, tv[i].keSz) != 0)
  25345. return WC_TEST_RET_ENC_NC;
  25346. if (XMEMCMP(keyA, srtcpKa_48_2, tv[i].kaSz) != 0)
  25347. return WC_TEST_RET_ENC_NC;
  25348. if (XMEMCMP(keyS, srtcpKs_48_2, tv[i].ksSz) != 0)
  25349. return WC_TEST_RET_ENC_NC;
  25350. }
  25351. idx = wc_SRTP_KDF_kdr_to_idx(kdr);
  25352. if (idx != i)
  25353. return WC_TEST_RET_ENC_NC;
  25354. }
  25355. return 0;
  25356. }
  25357. #endif
  25358. #ifdef HAVE_ECC
  25359. /* size to use for ECC key gen tests */
  25360. #ifndef ECC_KEYGEN_SIZE
  25361. #if !defined(NO_ECC256) || defined(WOLFSSL_SM2)
  25362. #define ECC_KEYGEN_SIZE 32
  25363. #elif defined(HAVE_ECC384)
  25364. #define ECC_KEYGEN_SIZE 48
  25365. #elif defined(HAVE_ECC224)
  25366. #define ECC_KEYGEN_SIZE 28
  25367. #elif defined(HAVE_ECC521)
  25368. #define ECC_KEYGEN_SIZE 66
  25369. #else
  25370. #error No ECC keygen size defined for test
  25371. #endif
  25372. #endif
  25373. #ifdef BENCH_EMBEDDED
  25374. #define ECC_SHARED_SIZE 128
  25375. #else
  25376. #define ECC_SHARED_SIZE MAX_ECC_BYTES
  25377. #endif
  25378. #if defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  25379. #define HAVE_ECC_DETERMINISTIC_K
  25380. #define ECC_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
  25381. #else
  25382. #define ECC_DIGEST_SIZE MAX_ECC_BYTES
  25383. #endif
  25384. #define ECC_SIG_SIZE ECC_MAX_SIG_SIZE
  25385. #ifdef NO_ECC_SECP
  25386. #define NO_ECC_VECTOR_TEST
  25387. #endif
  25388. #ifndef NO_ECC_VECTOR_TEST
  25389. #if (defined(HAVE_ECC192) || defined(HAVE_ECC224) ||\
  25390. !defined(NO_ECC256) || defined(HAVE_ECC384) ||\
  25391. defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  25392. #define HAVE_ECC_VECTOR_TEST
  25393. #endif
  25394. #endif
  25395. #ifdef HAVE_ECC_VECTOR_TEST
  25396. typedef struct eccVector {
  25397. const char* msg; /* SHA-1 Encoded Message */
  25398. const char* Qx;
  25399. const char* Qy;
  25400. const char* d; /* Private Key */
  25401. const char* R;
  25402. const char* S;
  25403. const char* curveName;
  25404. word32 msgLen;
  25405. word32 keySize;
  25406. const byte* r;
  25407. word32 rSz;
  25408. const byte* s;
  25409. word32 sSz;
  25410. } eccVector;
  25411. #if !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  25412. static wc_test_ret_t ecc_test_vector_item(const eccVector* vector)
  25413. {
  25414. wc_test_ret_t ret = 0;
  25415. int verify = 0;
  25416. word32 sigSz;
  25417. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25418. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25419. #else
  25420. ecc_key userA[1];
  25421. #endif
  25422. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  25423. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  25424. word32 sigRawSz, rSz = MAX_ECC_BYTES, sSz = MAX_ECC_BYTES;
  25425. WC_DECLARE_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  25426. WC_DECLARE_VAR(r, byte, MAX_ECC_BYTES, HEAP_HINT);
  25427. WC_DECLARE_VAR(s, byte, MAX_ECC_BYTES, HEAP_HINT);
  25428. #endif
  25429. WC_ALLOC_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  25430. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  25431. WC_ALLOC_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  25432. WC_ALLOC_VAR(r, byte, MAX_ECC_BYTES, HEAP_HINT);
  25433. WC_ALLOC_VAR(s, byte, MAX_ECC_BYTES, HEAP_HINT);
  25434. #endif
  25435. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  25436. if (sig == NULL)
  25437. ERROR_OUT(MEMORY_E, done);
  25438. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  25439. if (sigRaw == NULL || r == NULL || s == NULL)
  25440. ERROR_OUT(MEMORY_E, done);
  25441. #endif
  25442. #endif
  25443. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25444. if (userA == NULL)
  25445. ERROR_OUT(MEMORY_E, done);
  25446. #endif
  25447. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  25448. if (ret != 0)
  25449. goto done;
  25450. ret = wc_ecc_import_raw(userA, vector->Qx, vector->Qy,
  25451. vector->d, vector->curveName);
  25452. if (ret != 0)
  25453. goto done;
  25454. #if !defined(NO_ASN)
  25455. XMEMSET(sig, 0, ECC_SIG_SIZE);
  25456. sigSz = ECC_SIG_SIZE;
  25457. ret = wc_ecc_rs_to_sig(vector->R, vector->S, sig, &sigSz);
  25458. if (ret != 0)
  25459. goto done;
  25460. #if !defined(HAVE_SELFTEST)
  25461. XMEMSET(sigRaw, 0, ECC_SIG_SIZE);
  25462. sigRawSz = ECC_SIG_SIZE;
  25463. ret = wc_ecc_rs_raw_to_sig(vector->r, vector->rSz, vector->s, vector->sSz,
  25464. sigRaw, &sigRawSz);
  25465. if (ret != 0)
  25466. goto done;
  25467. if (sigSz != sigRawSz || XMEMCMP(sig, sigRaw, sigSz) != 0) {
  25468. ret = WC_TEST_RET_ENC_NC;
  25469. goto done;
  25470. }
  25471. ret = wc_ecc_sig_to_rs(sig, sigSz, r, &rSz, s, &sSz);
  25472. if (ret != 0)
  25473. goto done;
  25474. if (rSz != vector->rSz || XMEMCMP(r, vector->r, rSz) != 0 ||
  25475. sSz != vector->sSz || XMEMCMP(s, vector->s, sSz) != 0) {
  25476. ret = WC_TEST_RET_ENC_NC;
  25477. goto done;
  25478. }
  25479. #endif /* !HAVE_SELFTEST */
  25480. #else
  25481. /* Signature will be R+S directly */
  25482. /* Make sure and zero pad if r or s is less than key size */
  25483. XMEMSET(sig, 0, ECC_SIG_SIZE);
  25484. sigSz = vector->keySize * 2;
  25485. XMEMCPY(sig + (vector->keySize - vector->rSz),
  25486. vector->r, vector->rSz);
  25487. XMEMCPY(sig + vector->keySize + (vector->keySize - vector->sSz),
  25488. vector->s, vector->sSz);
  25489. #endif /* !NO_ASN */
  25490. #ifdef HAVE_ECC_VERIFY
  25491. do {
  25492. #if defined(WOLFSSL_ASYNC_CRYPT)
  25493. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  25494. #endif
  25495. if (ret == 0)
  25496. ret = wc_ecc_verify_hash(sig, sigSz, (byte*)vector->msg,
  25497. vector->msgLen, &verify, userA);
  25498. } while (ret == WC_PENDING_E);
  25499. if (ret != 0)
  25500. goto done;
  25501. TEST_SLEEP();
  25502. if (verify != 1)
  25503. ret = WC_TEST_RET_ENC_NC;
  25504. #endif
  25505. done:
  25506. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25507. if (userA != NULL) {
  25508. wc_ecc_free(userA);
  25509. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25510. }
  25511. #else
  25512. wc_ecc_free(userA);
  25513. #endif
  25514. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  25515. WC_FREE_VAR(sigRaw, HEAP_HINT);
  25516. WC_FREE_VAR(r, HEAP_HINT);
  25517. WC_FREE_VAR(s, HEAP_HINT);
  25518. #endif
  25519. WC_FREE_VAR(sig, HEAP_HINT);
  25520. return ret;
  25521. }
  25522. static wc_test_ret_t ecc_test_vector(int keySize)
  25523. {
  25524. wc_test_ret_t ret;
  25525. eccVector vec;
  25526. XMEMSET(&vec, 0, sizeof(vec));
  25527. vec.keySize = (word32)keySize;
  25528. switch(keySize) {
  25529. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  25530. case 14:
  25531. return 0;
  25532. #endif /* HAVE_ECC112 */
  25533. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  25534. case 16:
  25535. return 0;
  25536. #endif /* HAVE_ECC128 */
  25537. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  25538. case 20:
  25539. return 0;
  25540. #endif /* HAVE_ECC160 */
  25541. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  25542. case 24:
  25543. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  25544. #if 1
  25545. vec.msg = "\x60\x80\x79\x42\x3f\x12\x42\x1d\xe6\x16\xb7\x49\x3e\xbe\x55\x1c\xf4\xd6\x5b\x92";
  25546. vec.msgLen = 20;
  25547. #else
  25548. /* This is the raw message prior to SHA-1 */
  25549. vec.msg =
  25550. "\xeb\xf7\x48\xd7\x48\xeb\xbc\xa7\xd2\x9f\xb4\x73\x69\x8a\x6e\x6b"
  25551. "\x4f\xb1\x0c\x86\x5d\x4a\xf0\x24\xcc\x39\xae\x3d\xf3\x46\x4b\xa4"
  25552. "\xf1\xd6\xd4\x0f\x32\xbf\x96\x18\xa9\x1b\xb5\x98\x6f\xa1\xa2\xaf"
  25553. "\x04\x8a\x0e\x14\xdc\x51\xe5\x26\x7e\xb0\x5e\x12\x7d\x68\x9d\x0a"
  25554. "\xc6\xf1\xa7\xf1\x56\xce\x06\x63\x16\xb9\x71\xcc\x7a\x11\xd0\xfd"
  25555. "\x7a\x20\x93\xe2\x7c\xf2\xd0\x87\x27\xa4\xe6\x74\x8c\xc3\x2f\xd5"
  25556. "\x9c\x78\x10\xc5\xb9\x01\x9d\xf2\x1c\xdc\xc0\xbc\xa4\x32\xc0\xa3"
  25557. "\xee\xd0\x78\x53\x87\x50\x88\x77\x11\x43\x59\xce\xe4\xa0\x71\xcf";
  25558. vec.msgLen = 128;
  25559. #endif
  25560. vec.Qx = "07008ea40b08dbe76432096e80a2494c94982d2d5bcf98e6";
  25561. vec.Qy = "76fab681d00b414ea636ba215de26d98c41bd7f2e4d65477";
  25562. vec.d = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3";
  25563. vec.R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  25564. vec.S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  25565. vec.curveName = "SECP192R1";
  25566. vec.r = (byte*)"\x69\x94\xd9\x62\xbd\xd0\xd7\x93\xff\xdd\xf8\x55"
  25567. "\xec\x5b\xf2\xf9\x1a\x96\x98\xb4\x62\x58\xa6\x3e";
  25568. vec.rSz = 24;
  25569. vec.s = (byte*)"\x02\xba\x64\x65\xa2\x34\x90\x37\x44\xab\x02\xbc"
  25570. "\x85\x21\x40\x5b\x73\xcf\x5f\xc0\x0e\x1a\x9f\x41";
  25571. vec.sSz = 24;
  25572. break;
  25573. #endif /* HAVE_ECC192 */
  25574. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  25575. case 28:
  25576. /* first [P-224,SHA-1] vector from FIPS 186-3 NIST vectors */
  25577. #if 1
  25578. vec.msg = "\xb9\xa3\xb8\x6d\xb0\xba\x99\xfd\xc6\xd2\x94\x6b\xfe\xbe\x9c\xe8\x3f\x10\x74\xfc";
  25579. vec.msgLen = 20;
  25580. #else
  25581. /* This is the raw message prior to SHA-1 */
  25582. vec.msg =
  25583. "\x36\xc8\xb2\x29\x86\x48\x7f\x67\x7c\x18\xd0\x97\x2a\x9e\x20\x47"
  25584. "\xb3\xaf\xa5\x9e\xc1\x62\x76\x4e\xc3\x0b\x5b\x69\xe0\x63\x0f\x99"
  25585. "\x0d\x4e\x05\xc2\x73\xb0\xe5\xa9\xd4\x28\x27\xb6\x95\xfc\x2d\x64"
  25586. "\xd9\x13\x8b\x1c\xf4\xc1\x21\x55\x89\x4c\x42\x13\x21\xa7\xbb\x97"
  25587. "\x0b\xdc\xe0\xfb\xf0\xd2\xae\x85\x61\xaa\xd8\x71\x7f\x2e\x46\xdf"
  25588. "\xe3\xff\x8d\xea\xb4\xd7\x93\x23\x56\x03\x2c\x15\x13\x0d\x59\x9e"
  25589. "\x26\xc1\x0f\x2f\xec\x96\x30\x31\xac\x69\x38\xa1\x8d\x66\x45\x38"
  25590. "\xb9\x4d\xac\x55\x34\xef\x7b\x59\x94\x24\xd6\x9b\xe1\xf7\x1c\x20";
  25591. vec.msgLen = 128;
  25592. #endif
  25593. vec.Qx = "8a4dca35136c4b70e588e23554637ae251077d1365a6ba5db9585de7";
  25594. vec.Qy = "ad3dee06de0be8279d4af435d7245f14f3b4f82eb578e519ee0057b1";
  25595. vec.d = "97c4b796e1639dd1035b708fc00dc7ba1682cec44a1002a1a820619f";
  25596. vec.R = "147b33758321e722a0360a4719738af848449e2c1d08defebc1671a7";
  25597. vec.S = "24fc7ed7f1352ca3872aa0916191289e2e04d454935d50fe6af3ad5b";
  25598. vec.curveName = "SECP224R1";
  25599. vec.r = (byte*)"\x14\x7b\x33\x75\x83\x21\xe7\x22\xa0\x36\x0a\x47"
  25600. "\x19\x73\x8a\xf8\x48\x44\x9e\x2c\x1d\x08\xde\xfe"
  25601. "\xbc\x16\x71\xa7";
  25602. vec.rSz = 28;
  25603. vec.s = (byte*)"\x24\xfc\x7e\xd7\xf1\x35\x2c\xa3\x87\x2a\xa0\x91"
  25604. "\x61\x91\x28\x9e\x2e\x04\xd4\x54\x93\x5d\x50\xfe"
  25605. "\x6a\xf3\xad\x5b";
  25606. vec.sSz = 28;
  25607. break;
  25608. #endif /* HAVE_ECC224 */
  25609. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  25610. case 30:
  25611. return 0;
  25612. #endif /* HAVE_ECC239 */
  25613. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  25614. case 32:
  25615. /* first [P-256,SHA-1] vector from FIPS 186-3 NIST vectors */
  25616. #if 1
  25617. vec.msg = "\xa3\xf9\x1a\xe2\x1b\xa6\xb3\x03\x98\x64\x47\x2f\x18\x41\x44\xc6\xaf\x62\xcd\x0e";
  25618. vec.msgLen = 20;
  25619. #else
  25620. /* This is the raw message prior to SHA-1 */
  25621. vec.msg =
  25622. "\xa2\x4b\x21\x76\x2e\x6e\xdb\x15\x3c\xc1\x14\x38\xdb\x0e\x92\xcd"
  25623. "\xf5\x2b\x86\xb0\x6c\xa9\x70\x16\x06\x27\x59\xc7\x0d\x36\xd1\x56"
  25624. "\x2c\xc9\x63\x0d\x7f\xc7\xc7\x74\xb2\x8b\x54\xe3\x1e\xf5\x58\x72"
  25625. "\xb2\xa6\x5d\xf1\xd7\xec\x26\xde\xbb\x33\xe7\xd9\x27\xef\xcc\xf4"
  25626. "\x6b\x63\xde\x52\xa4\xf4\x31\xea\xca\x59\xb0\x5d\x2e\xde\xc4\x84"
  25627. "\x5f\xff\xc0\xee\x15\x03\x94\xd6\x1f\x3d\xfe\xcb\xcd\xbf\x6f\x5a"
  25628. "\x73\x38\xd0\xbe\x3f\x2a\x77\x34\x51\x98\x3e\xba\xeb\x48\xf6\x73"
  25629. "\x8f\xc8\x95\xdf\x35\x7e\x1a\x48\xa6\x53\xbb\x35\x5a\x31\xa1\xb4"
  25630. vec.msgLen = 128;
  25631. #endif
  25632. vec.Qx = "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  25633. vec.Qy = "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  25634. vec.d = "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  25635. vec.R = "2b826f5d44e2d0b6de531ad96b51e8f0c56fdfead3c236892e4d84eacfc3b75c";
  25636. vec.S = "a2248b62c03db35a7cd63e8a120a3521a89d3d2f61ff99035a2148ae32e3a248";
  25637. vec.r = (byte*)"\x2b\x82\x6f\x5d\x44\xe2\xd0\xb6\xde\x53\x1a\xd9"
  25638. "\x6b\x51\xe8\xf0\xc5\x6f\xdf\xea\xd3\xc2\x36\x89"
  25639. "\x2e\x4d\x84\xea\xcf\xc3\xb7\x5c";
  25640. vec.rSz = 32;
  25641. vec.s = (byte*)"\xa2\x24\x8b\x62\xc0\x3d\xb3\x5a\x7c\xd6\x3e\x8a"
  25642. "\x12\x0a\x35\x21\xa8\x9d\x3d\x2f\x61\xff\x99\x03"
  25643. "\x5a\x21\x48\xae\x32\xe3\xa2\x48";
  25644. vec.sSz = 32;
  25645. vec.curveName = "SECP256R1";
  25646. break;
  25647. #endif /* !NO_ECC256 */
  25648. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  25649. case 40:
  25650. return 0;
  25651. #endif /* HAVE_ECC320 */
  25652. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  25653. case 48:
  25654. /* first [P-384,SHA-1] vector from FIPS 186-3 NIST vectors */
  25655. #if 1
  25656. vec.msg = "\x9b\x9f\x8c\x95\x35\xa5\xca\x26\x60\x5d\xb7\xf2\xfa\x57\x3b\xdf\xc3\x2e\xab\x8b";
  25657. vec.msgLen = 20;
  25658. #else
  25659. /* This is the raw message prior to SHA-1 */
  25660. vec.msg =
  25661. "\xab\xe1\x0a\xce\x13\xe7\xe1\xd9\x18\x6c\x48\xf7\x88\x9d\x51\x47"
  25662. "\x3d\x3a\x09\x61\x98\x4b\xc8\x72\xdf\x70\x8e\xcc\x3e\xd3\xb8\x16"
  25663. "\x9d\x01\xe3\xd9\x6f\xc4\xf1\xd5\xea\x00\xa0\x36\x92\xbc\xc5\xcf"
  25664. "\xfd\x53\x78\x7c\x88\xb9\x34\xaf\x40\x4c\x03\x9d\x32\x89\xb5\xba"
  25665. "\xc5\xae\x7d\xb1\x49\x68\x75\xb5\xdc\x73\xc3\x09\xf9\x25\xc1\x3d"
  25666. "\x1c\x01\xab\xda\xaf\xeb\xcd\xac\x2c\xee\x43\x39\x39\xce\x8d\x4a"
  25667. "\x0a\x5d\x57\xbb\x70\x5f\x3b\xf6\xec\x08\x47\x95\x11\xd4\xb4\xa3"
  25668. "\x21\x1f\x61\x64\x9a\xd6\x27\x43\x14\xbf\x0d\x43\x8a\x81\xe0\x60"
  25669. vec.msgLen = 128;
  25670. #endif
  25671. vec.Qx = "e55fee6c49d8d523f5ce7bf9c0425ce4ff650708b7de5cfb095901523979a7f042602db30854735369813b5c3f5ef868";
  25672. vec.Qy = "28f59cc5dc509892a988d38a8e2519de3d0c4fd0fbdb0993e38f18506c17606c5e24249246f1ce94983a5361c5be983e";
  25673. vec.d = "a492ce8fa90084c227e1a32f7974d39e9ff67a7e8705ec3419b35fb607582bebd461e0b1520ac76ec2dd4e9b63ebae71";
  25674. vec.R = "6820b8585204648aed63bdff47f6d9acebdea62944774a7d14f0e14aa0b9a5b99545b2daee6b3c74ebf606667a3f39b7";
  25675. vec.S = "491af1d0cccd56ddd520b233775d0bc6b40a6255cc55207d8e9356741f23c96c14714221078dbd5c17f4fdd89b32a907";
  25676. vec.curveName = "SECP384R1";
  25677. vec.r = (byte*)"\x68\x20\xb8\x58\x52\x04\x64\x8a\xed\x63\xbd\xff"
  25678. "\x47\xf6\xd9\xac\xeb\xde\xa6\x29\x44\x77\x4a\x7d"
  25679. "\x14\xf0\xe1\x4a\xa0\xb9\xa5\xb9\x95\x45\xb2\xda"
  25680. "\xee\x6b\x3c\x74\xeb\xf6\x06\x66\x7a\x3f\x39\xb7";
  25681. vec.rSz = 48;
  25682. vec.s = (byte*)"\x49\x1a\xf1\xd0\xcc\xcd\x56\xdd\xd5\x20\xb2\x33"
  25683. "\x77\x5d\x0b\xc6\xb4\x0a\x62\x55\xcc\x55\x20\x7d"
  25684. "\x8e\x93\x56\x74\x1f\x23\xc9\x6c\x14\x71\x42\x21"
  25685. "\x07\x8d\xbd\x5c\x17\xf4\xfd\xd8\x9b\x32\xa9\x07";
  25686. vec.sSz = 48;
  25687. break;
  25688. #endif /* HAVE_ECC384 */
  25689. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  25690. case 64:
  25691. return 0;
  25692. #endif /* HAVE_ECC512 */
  25693. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  25694. case 66:
  25695. /* first [P-521,SHA-1] vector from FIPS 186-3 NIST vectors */
  25696. #if 1
  25697. vec.msg = "\x1b\xf7\x03\x9c\xca\x23\x94\x27\x3f\x11\xa1\xd4\x8d\xcc\xb4\x46\x6f\x31\x61\xdf";
  25698. vec.msgLen = 20;
  25699. #else
  25700. /* This is the raw message prior to SHA-1 */
  25701. vec.msg =
  25702. "\x50\x3f\x79\x39\x34\x0a\xc7\x23\xcd\x4a\x2f\x4e\x6c\xcc\x27\x33"
  25703. "\x38\x3a\xca\x2f\xba\x90\x02\x19\x9d\x9e\x1f\x94\x8b\xe0\x41\x21"
  25704. "\x07\xa3\xfd\xd5\x14\xd9\x0c\xd4\xf3\x7c\xc3\xac\x62\xef\x00\x3a"
  25705. "\x2d\xb1\xd9\x65\x7a\xb7\x7f\xe7\x55\xbf\x71\xfa\x59\xe4\xd9\x6e"
  25706. "\xa7\x2a\xe7\xbf\x9d\xe8\x7d\x79\x34\x3b\xc1\xa4\xbb\x14\x4d\x16"
  25707. "\x28\xd1\xe9\xe9\xc8\xed\x80\x8b\x96\x2c\x54\xe5\xf9\x6d\x53\xda"
  25708. "\x14\x7a\x96\x38\xf9\x4a\x91\x75\xd8\xed\x61\x05\x5f\x0b\xa5\x73"
  25709. "\xa8\x2b\xb7\xe0\x18\xee\xda\xc4\xea\x7b\x36\x2e\xc8\x9c\x38\x2b"
  25710. vec.msgLen = 128;
  25711. #endif
  25712. vec.Qx = "12fbcaeffa6a51f3ee4d3d2b51c5dec6d7c726ca353fc014ea2bf7cfbb9b910d32cbfa6a00fe39b6cdb8946f22775398b2e233c0cf144d78c8a7742b5c7a3bb5d23";
  25713. vec.Qy = "09cdef823dd7bf9a79e8cceacd2e4527c231d0ae5967af0958e931d7ddccf2805a3e618dc3039fec9febbd33052fe4c0fee98f033106064982d88f4e03549d4a64d";
  25714. vec.d = "1bd56bd106118eda246155bd43b42b8e13f0a6e25dd3bb376026fab4dc92b6157bc6dfec2d15dd3d0cf2a39aa68494042af48ba9601118da82c6f2108a3a203ad74";
  25715. vec.R = "0bd117b4807710898f9dd7778056485777668f0e78e6ddf5b000356121eb7a220e9493c7f9a57c077947f89ac45d5acb6661bbcd17abb3faea149ba0aa3bb1521be";
  25716. vec.S = "019cd2c5c3f9870ecdeb9b323abdf3a98cd5e231d85c6ddc5b71ab190739f7f226e6b134ba1d5889ddeb2751dabd97911dff90c34684cdbe7bb669b6c3d22f2480c";
  25717. vec.curveName = "SECP521R1";
  25718. vec.r = (byte*)"\xbd\x11\x7b\x48\x07\x71\x08\x98\xf9\xdd\x77\x78"
  25719. "\x05\x64\x85\x77\x76\x68\xf0\xe7\x8e\x6d\xdf\x5b"
  25720. "\x00\x03\x56\x12\x1e\xb7\xa2\x20\xe9\x49\x3c\x7f"
  25721. "\x9a\x57\xc0\x77\x94\x7f\x89\xac\x45\xd5\xac\xb6"
  25722. "\x66\x1b\xbc\xd1\x7a\xbb\x3f\xae\xa1\x49\xba\x0a"
  25723. "\xa3\xbb\x15\x21\xbe";
  25724. vec.rSz = 65;
  25725. vec.s = (byte*)"\x19\xcd\x2c\x5c\x3f\x98\x70\xec\xde\xb9\xb3\x23"
  25726. "\xab\xdf\x3a\x98\xcd\x5e\x23\x1d\x85\xc6\xdd\xc5"
  25727. "\xb7\x1a\xb1\x90\x73\x9f\x7f\x22\x6e\x6b\x13\x4b"
  25728. "\xa1\xd5\x88\x9d\xde\xb2\x75\x1d\xab\xd9\x79\x11"
  25729. "\xdf\xf9\x0c\x34\x68\x4c\xdb\xe7\xbb\x66\x9b\x6c"
  25730. "\x3d\x22\xf2\x48\x0c";
  25731. vec.sSz = 65;
  25732. break;
  25733. #endif /* HAVE_ECC521 */
  25734. default:
  25735. return NOT_COMPILED_IN; /* Invalid key size / Not supported */
  25736. }; /* Switch */
  25737. ret = ecc_test_vector_item(&vec);
  25738. if (ret < 0) {
  25739. return ret;
  25740. }
  25741. return 0;
  25742. }
  25743. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  25744. #if defined(HAVE_ECC_SIGN) && \
  25745. (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  25746. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) && \
  25747. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3))
  25748. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  25749. static wc_test_ret_t ecdsa_test_deterministic_k_sig(ecc_key *key,
  25750. enum wc_HashType hashType, const char* msg, WC_RNG* rng, const byte* expSig,
  25751. size_t expSigSz)
  25752. {
  25753. wc_test_ret_t ret;
  25754. int verify;
  25755. byte sig[ECC_MAX_SIG_SIZE];
  25756. word32 sigSz;
  25757. unsigned char hash[WC_MAX_DIGEST_SIZE];
  25758. ret = wc_Hash(hashType,
  25759. (byte*)msg, (word32)XSTRLEN(msg),
  25760. hash, sizeof(hash));
  25761. if (ret != 0) {
  25762. goto done;
  25763. }
  25764. /* Sign test */
  25765. sigSz = sizeof(sig);
  25766. do {
  25767. #if defined(WOLFSSL_ASYNC_CRYPT)
  25768. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  25769. #endif
  25770. if (ret == 0)
  25771. ret = wc_ecc_sign_hash(hash, wc_HashGetDigestSize(hashType),
  25772. sig, &sigSz, rng, key);
  25773. } while (ret == WC_PENDING_E);
  25774. if (ret != 0) {
  25775. goto done;
  25776. }
  25777. TEST_SLEEP();
  25778. /* Compare test vector */
  25779. if (sigSz != expSigSz) {
  25780. ret = WC_TEST_RET_ENC_NC;
  25781. goto done;
  25782. }
  25783. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  25784. ret = WC_TEST_RET_ENC_NC;
  25785. goto done;
  25786. }
  25787. /* Verificiation */
  25788. verify = 0;
  25789. do {
  25790. #if defined(WOLFSSL_ASYNC_CRYPT)
  25791. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  25792. #endif
  25793. if (ret == 0)
  25794. ret = wc_ecc_verify_hash(sig, sigSz,
  25795. hash, wc_HashGetDigestSize(hashType), &verify, key);
  25796. } while (ret == WC_PENDING_E);
  25797. if (ret != 0) {
  25798. goto done;
  25799. }
  25800. if (verify != 1) {
  25801. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  25802. }
  25803. done:
  25804. return ret;
  25805. }
  25806. static wc_test_ret_t ecc_test_deterministic_k(WC_RNG* rng)
  25807. {
  25808. wc_test_ret_t ret;
  25809. #ifdef WOLFSSL_SMALL_STACK
  25810. ecc_key *key = NULL;
  25811. #else
  25812. ecc_key key[1];
  25813. #endif
  25814. int key_inited = 0;
  25815. WOLFSSL_SMALL_STACK_STATIC const char* msg = "sample";
  25816. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  25817. "C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721";
  25818. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  25819. "60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6";
  25820. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  25821. "7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299";
  25822. #ifndef NO_SHA256
  25823. WOLFSSL_SMALL_STACK_STATIC const byte expSig256[] = {
  25824. 0x30, 0x46, /* CONSTRUCTED SEQUENCE: (0x20 | 0x10) = 68 bytes */
  25825. 0x02, 0x21, /* ASN_INTEGER = 0x02 (32 bytes) - SIG R */
  25826. 0x00, 0xEF, 0xD4, 0x8B, 0x2A, 0xAC, 0xB6, 0xA8,
  25827. 0xFD, 0x11, 0x40, 0xDD, 0x9C, 0xD4, 0x5E, 0x81,
  25828. 0xD6, 0x9D, 0x2C, 0x87, 0x7B, 0x56, 0xAA, 0xF9,
  25829. 0x91, 0xC3, 0x4D, 0x0E, 0xA8, 0x4E, 0xAF, 0x37,
  25830. 0x16,
  25831. 0x02, 0x21, /* ASN_INTEGER = 0x02 (32 bytes) - SIG S */
  25832. 0x00, 0xF7, 0xCB, 0x1C, 0x94, 0x2D, 0x65, 0x7C,
  25833. 0x41, 0xD4, 0x36, 0xC7, 0xA1, 0xB6, 0xE2, 0x9F,
  25834. 0x65, 0xF3, 0xE9, 0x00, 0xDB, 0xB9, 0xAF, 0xF4,
  25835. 0x06, 0x4D, 0xC4, 0xAB, 0x2F, 0x84, 0x3A, 0xCD,
  25836. 0xA8
  25837. };
  25838. #endif
  25839. #ifdef WOLFSSL_SHA384
  25840. WOLFSSL_SMALL_STACK_STATIC const byte expSig384[] = {
  25841. 0x30, 0x44, /* CONSTRUCTED SEQUENCE: (0x20 | 0x10) = 68 bytes */
  25842. 0x02, 0x20, /* ASN_INTEGER = 0x02 (32 bytes) - SIG R */
  25843. 0x0e, 0xaf, 0xea, 0x03, 0x9b, 0x20, 0xe9, 0xb4,
  25844. 0x23, 0x09, 0xfb, 0x1d, 0x89, 0xe2, 0x13, 0x05,
  25845. 0x7c, 0xbf, 0x97, 0x3d, 0xc0, 0xcf, 0xc8, 0xf1,
  25846. 0x29, 0xed, 0xdd, 0xc8, 0x00, 0xef, 0x77, 0x19,
  25847. 0x02, 0x20, /* ASN_INTEGER = 0x02 (32 bytes) - SIG S */
  25848. 0x48, 0x61, 0xf0, 0x49, 0x1e, 0x69, 0x98, 0xb9,
  25849. 0x45, 0x51, 0x93, 0xe3, 0x4e, 0x7b, 0x0d, 0x28,
  25850. 0x4d, 0xdd, 0x71, 0x49, 0xa7, 0x4b, 0x95, 0xb9,
  25851. 0x26, 0x1f, 0x13, 0xab, 0xde, 0x94, 0x09, 0x54
  25852. };
  25853. #endif
  25854. #ifdef WOLFSSL_SHA512
  25855. WOLFSSL_SMALL_STACK_STATIC const byte expSig512[] = {
  25856. 0x30, 0x45, /* CONSTRUCTED SEQUENCE: (0x20 | 0x10) = 68 bytes */
  25857. 0x02, 0x21, /* ASN_INTEGER = 0x02 (32 bytes) - SIG R */
  25858. 0x00, 0x84, 0x96, 0xa6, 0x0b, 0x5e, 0x9b, 0x47,
  25859. 0xc8, 0x25, 0x48, 0x88, 0x27, 0xe0, 0x49, 0x5b,
  25860. 0x0e, 0x3f, 0xa1, 0x09, 0xec, 0x45, 0x68, 0xfd,
  25861. 0x3f, 0x8d, 0x10, 0x97, 0x67, 0x8e, 0xb9, 0x7f,
  25862. 0x00,
  25863. 0x02, 0x20, /* ASN_INTEGER = 0x02 (32 bytes) - SIG S */
  25864. 0x23, 0x62, 0xab, 0x1a, 0xdb, 0xe2, 0xb8, 0xad,
  25865. 0xf9, 0xcb, 0x9e, 0xda, 0xb7, 0x40, 0xea, 0x60,
  25866. 0x49, 0xc0, 0x28, 0x11, 0x4f, 0x24, 0x60, 0xf9,
  25867. 0x65, 0x54, 0xf6, 0x1f, 0xae, 0x33, 0x02, 0xfe
  25868. };
  25869. #endif
  25870. #ifdef WOLFSSL_SMALL_STACK
  25871. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25872. if (key == NULL)
  25873. return MEMORY_E;
  25874. #endif
  25875. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  25876. if (ret != 0) {
  25877. goto done;
  25878. }
  25879. key_inited = 1;
  25880. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  25881. if (ret != 0) {
  25882. goto done;
  25883. }
  25884. ret = wc_ecc_set_deterministic(key, 1);
  25885. if (ret != 0) {
  25886. goto done;
  25887. }
  25888. #ifndef NO_SHA256
  25889. /* Test for SHA2-256 */
  25890. ret = ecdsa_test_deterministic_k_sig(key, WC_HASH_TYPE_SHA256, msg, rng,
  25891. expSig256, sizeof(expSig256));
  25892. if (ret != 0) {
  25893. goto done;
  25894. }
  25895. #endif /* !NO_SHA256 */
  25896. #ifdef WOLFSSL_SHA384
  25897. /* Test for SHA2-384 */
  25898. ret = ecdsa_test_deterministic_k_sig(key, WC_HASH_TYPE_SHA384, msg, rng,
  25899. expSig384, sizeof(expSig384));
  25900. if (ret != 0) {
  25901. goto done;
  25902. }
  25903. #endif /* WOLFSSL_SHA384 */
  25904. #ifdef WOLFSSL_SHA512
  25905. /* Test for SHA2-512 */
  25906. ret = ecdsa_test_deterministic_k_sig(key, WC_HASH_TYPE_SHA512, msg, rng,
  25907. expSig512, sizeof(expSig512));
  25908. if (ret != 0) {
  25909. goto done;
  25910. }
  25911. #endif /* WOLFSSL_SHA512 */
  25912. done:
  25913. if (key_inited)
  25914. wc_ecc_free(key);
  25915. #ifdef WOLFSSL_SMALL_STACK
  25916. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25917. #endif
  25918. return ret;
  25919. }
  25920. #endif /* NO_ECC256 || HAVE_ALL_CURVES */
  25921. #ifdef WOLFSSL_PUBLIC_MP
  25922. static wc_test_ret_t ecdsa_test_deterministic_k_rs(ecc_key *key,
  25923. enum wc_HashType hashType, const char* msg, WC_RNG* rng,
  25924. mp_int* r, mp_int* s,
  25925. mp_int* expR, mp_int* expS)
  25926. {
  25927. wc_test_ret_t ret;
  25928. unsigned char hash[WC_MAX_DIGEST_SIZE];
  25929. int verify;
  25930. ret = wc_Hash(hashType,
  25931. (byte*)msg, (word32)XSTRLEN(msg),
  25932. hash, sizeof(hash));
  25933. if (ret != 0) {
  25934. goto done;
  25935. }
  25936. ret = wc_ecc_sign_hash_ex(hash, wc_HashGetDigestSize(hashType), rng, key,
  25937. r, s);
  25938. #if defined(WOLFSSL_ASYNC_CRYPT)
  25939. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  25940. #endif
  25941. if (ret != 0) {
  25942. goto done;
  25943. }
  25944. TEST_SLEEP();
  25945. if (mp_cmp(r, expR) != MP_EQ && mp_cmp(s, expS) != MP_EQ) {
  25946. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  25947. }
  25948. /* Verificiation */
  25949. verify = 0;
  25950. ret = wc_ecc_verify_hash_ex(r, s, hash, wc_HashGetDigestSize(hashType),
  25951. &verify, key);
  25952. #if defined(WOLFSSL_ASYNC_CRYPT)
  25953. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  25954. #endif
  25955. if (ret != 0) {
  25956. goto done;
  25957. }
  25958. if (verify != 1) {
  25959. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  25960. }
  25961. done:
  25962. return ret;
  25963. }
  25964. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  25965. /* KAT from RFC6979 */
  25966. static wc_test_ret_t ecc384_test_deterministic_k(WC_RNG* rng)
  25967. {
  25968. wc_test_ret_t ret;
  25969. #ifdef WOLFSSL_SMALL_STACK
  25970. ecc_key *key;
  25971. mp_int *r, *s, *expR, *expS;
  25972. #else
  25973. ecc_key key[1];
  25974. mp_int r[1], s[1], expR[1], expS[1];
  25975. #endif
  25976. int key_inited = 0,
  25977. tmp_mp_ints_inited = 0;
  25978. WOLFSSL_SMALL_STACK_STATIC const char* msg = "sample";
  25979. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  25980. "6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D8"
  25981. "96D5724E4C70A825F872C9EA60D2EDF5";
  25982. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  25983. "EC3A4E415B4E19A4568618029F427FA5DA9A8BC4AE92E02E06AAE5286B300C64"
  25984. "DEF8F0EA9055866064A254515480BC13";
  25985. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  25986. "8015D9B72D7D57244EA8EF9AC0C621896708A59367F9DFB9F54CA84B3F1C9DB1"
  25987. "288B231C3AE0D4FE7344FD2533264720";
  25988. #ifndef NO_SHA256
  25989. WOLFSSL_SMALL_STACK_STATIC const char* expRstr256 =
  25990. "21B13D1E013C7FA1392D03C5F99AF8B30C570C6F98D4EA8E354B63A21D3DAA33"
  25991. "BDE1E888E63355D92FA2B3C36D8FB2CD";
  25992. WOLFSSL_SMALL_STACK_STATIC const char* expSstr256 =
  25993. "F3AA443FB107745BF4BD77CB3891674632068A10CA67E3D45DB2266FA7D1FEEB"
  25994. "EFDC63ECCD1AC42EC0CB8668A4FA0AB0";
  25995. #endif
  25996. #ifdef WOLFSSL_SHA384
  25997. WOLFSSL_SMALL_STACK_STATIC const char* expRstr384 =
  25998. "94EDBB92A5ECB8AAD4736E56C691916B3F88140666CE9FA73D64C4EA95AD133C"
  25999. "81A648152E44ACF96E36DD1E80FABE46";
  26000. WOLFSSL_SMALL_STACK_STATIC const char* expSstr384 =
  26001. "99EF4AEB15F178CEA1FE40DB2603138F130E740A19624526203B6351D0A3A94F"
  26002. "A329C145786E679E7B82C71A38628AC8";
  26003. #endif
  26004. #ifdef WOLFSSL_SHA512
  26005. WOLFSSL_SMALL_STACK_STATIC const char* expRstr512 =
  26006. "ED0959D5880AB2D869AE7F6C2915C6D60F96507F9CB3E047C0046861DA4A799C"
  26007. "FE30F35CC900056D7C99CD7882433709";
  26008. WOLFSSL_SMALL_STACK_STATIC const char* expSstr512 =
  26009. "512C8CCEEE3890A84058CE1E22DBC2198F42323CE8ACA9135329F03C068E5112"
  26010. "DC7CC3EF3446DEFCEB01A45C2667FDD5";
  26011. #endif
  26012. #ifdef WOLFSSL_SMALL_STACK
  26013. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26014. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26015. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26016. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26017. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26018. if ((key == NULL) ||
  26019. (r == NULL) ||
  26020. (s == NULL) ||
  26021. (expR == NULL) ||
  26022. (expS == NULL))
  26023. {
  26024. ret = MEMORY_E;
  26025. goto done;
  26026. }
  26027. #endif
  26028. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  26029. if (ret != MP_OKAY) {
  26030. goto done;
  26031. }
  26032. tmp_mp_ints_inited = 1;
  26033. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  26034. if (ret != 0) {
  26035. goto done;
  26036. }
  26037. key_inited = 1;
  26038. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP384R1");
  26039. if (ret != 0) {
  26040. goto done;
  26041. }
  26042. ret = wc_ecc_set_deterministic(key, 1);
  26043. if (ret != 0) {
  26044. goto done;
  26045. }
  26046. #ifndef NO_SHA256
  26047. /* Test for SHA2-256 */
  26048. mp_read_radix(expR, expRstr256, MP_RADIX_HEX);
  26049. mp_read_radix(expS, expSstr256, MP_RADIX_HEX);
  26050. ret = ecdsa_test_deterministic_k_rs(key, WC_HASH_TYPE_SHA256, msg, rng,
  26051. r, s, expR, expS);
  26052. if (ret != 0) {
  26053. goto done;
  26054. }
  26055. #endif /* NO_SHA256 */
  26056. #ifdef WOLFSSL_SHA384
  26057. /* Test for SHA2-384 */
  26058. mp_read_radix(expR, expRstr384, MP_RADIX_HEX);
  26059. mp_read_radix(expS, expSstr384, MP_RADIX_HEX);
  26060. ret = ecdsa_test_deterministic_k_rs(key, WC_HASH_TYPE_SHA384, msg, rng,
  26061. r, s, expR, expS);
  26062. if (ret != 0) {
  26063. goto done;
  26064. }
  26065. #endif /* WOLFSSL_SHA384 */
  26066. #ifdef WOLFSSL_SHA512
  26067. /* Test for SHA2-512 */
  26068. mp_read_radix(expR, expRstr512, MP_RADIX_HEX);
  26069. mp_read_radix(expS, expSstr512, MP_RADIX_HEX);
  26070. ret = ecdsa_test_deterministic_k_rs(key, WC_HASH_TYPE_SHA512, msg, rng,
  26071. r, s, expR, expS);
  26072. if (ret != 0) {
  26073. goto done;
  26074. }
  26075. #endif /* WOLFSSL_SHA512 */
  26076. done:
  26077. if (key_inited)
  26078. wc_ecc_free(key);
  26079. if (tmp_mp_ints_inited) {
  26080. mp_free(r);
  26081. mp_free(s);
  26082. mp_free(expR);
  26083. mp_free(expS);
  26084. }
  26085. #ifdef WOLFSSL_SMALL_STACK
  26086. if (key != NULL)
  26087. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26088. if (r != NULL)
  26089. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26090. if (s != NULL)
  26091. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26092. if (expR != NULL)
  26093. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26094. if (expS != NULL)
  26095. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26096. #endif
  26097. return ret;
  26098. }
  26099. #endif /* HAVE_ECC384 */
  26100. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  26101. /* KAT from RFC6979 */
  26102. static wc_test_ret_t ecc521_test_deterministic_k(WC_RNG* rng)
  26103. {
  26104. wc_test_ret_t ret;
  26105. #ifdef WOLFSSL_SMALL_STACK
  26106. ecc_key *key;
  26107. mp_int *r, *s, *expR, *expS;
  26108. #else
  26109. ecc_key key[1];
  26110. mp_int r[1], s[1], expR[1], expS[1];
  26111. #endif
  26112. int key_inited = 0,
  26113. tmp_mp_ints_inited = 0;
  26114. WOLFSSL_SMALL_STACK_STATIC const char* msg = "sample";
  26115. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  26116. "0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75C"
  26117. "AA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83"
  26118. "538";
  26119. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  26120. "1894550D0785932E00EAA23B694F213F8C3121F86DC97A04E5A7167DB4E5BCD3"
  26121. "71123D46E45DB6B5D5370A7F20FB633155D38FFA16D2BD761DCAC474B9A2F502"
  26122. "3A4";
  26123. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  26124. "0493101C962CD4D2FDDF782285E64584139C2F91B47F87FF82354D6630F746A2"
  26125. "8A0DB25741B5B34A828008B22ACC23F924FAAFBD4D33F81EA66956DFEAA2BFDF"
  26126. "CF5";
  26127. #ifndef NO_SHA256
  26128. WOLFSSL_SMALL_STACK_STATIC const char* expRstr256 =
  26129. "1511BB4D675114FE266FC4372B87682BAECC01D3CC62CF2303C92B3526012659"
  26130. "D16876E25C7C1E57648F23B73564D67F61C6F14D527D54972810421E7D87589E"
  26131. "1A7";
  26132. WOLFSSL_SMALL_STACK_STATIC const char* expSstr256 =
  26133. "04A171143A83163D6DF460AAF61522695F207A58B95C0644D87E52AA1A347916"
  26134. "E4F7A72930B1BC06DBE22CE3F58264AFD23704CBB63B29B931F7DE6C9D949A7E"
  26135. "CFC";
  26136. #endif
  26137. #ifdef WOLFSSL_SHA384
  26138. WOLFSSL_SMALL_STACK_STATIC const char* expRstr384 =
  26139. "1EA842A0E17D2DE4F92C15315C63DDF72685C18195C2BB95E572B9C5136CA4B4"
  26140. "B576AD712A52BE9730627D16054BA40CC0B8D3FF035B12AE75168397F5D50C67"
  26141. "451";
  26142. WOLFSSL_SMALL_STACK_STATIC const char* expSstr384 =
  26143. "1F21A3CEE066E1961025FB048BD5FE2B7924D0CD797BABE0A83B66F1E35EEAF5"
  26144. "FDE143FA85DC394A7DEE766523393784484BDF3E00114A1C857CDE1AA203DB65"
  26145. "D61";
  26146. #endif
  26147. #ifdef WOLFSSL_SHA512
  26148. WOLFSSL_SMALL_STACK_STATIC const char* expRstr512 =
  26149. "0C328FAFCBD79DD77850370C46325D987CB525569FB63C5D3BC53950E6D4C5F1"
  26150. "74E25A1EE9017B5D450606ADD152B534931D7D4E8455CC91F9B15BF05EC36E37"
  26151. "7FA";
  26152. WOLFSSL_SMALL_STACK_STATIC const char* expSstr512 =
  26153. "0617CCE7CF5064806C467F678D3B4080D6F1CC50AF26CA209417308281B68AF2"
  26154. "82623EAA63E5B5C0723D8B8C37FF0777B1A20F8CCB1DCCC43997F1EE0E44DA4A"
  26155. "67A";
  26156. #endif
  26157. #ifdef WOLFSSL_SMALL_STACK
  26158. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26159. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26160. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26161. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26162. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26163. if ((key == NULL) ||
  26164. (r == NULL) ||
  26165. (s == NULL) ||
  26166. (expR == NULL) ||
  26167. (expS == NULL))
  26168. {
  26169. ret = MEMORY_E;
  26170. goto done;
  26171. }
  26172. #endif
  26173. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  26174. if (ret != MP_OKAY) {
  26175. goto done;
  26176. }
  26177. tmp_mp_ints_inited = 1;
  26178. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  26179. if (ret != 0) {
  26180. return WC_TEST_RET_ENC_EC(ret);
  26181. }
  26182. key_inited = 1;
  26183. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP521R1");
  26184. if (ret != 0) {
  26185. goto done;
  26186. }
  26187. ret = wc_ecc_set_deterministic(key, 1);
  26188. if (ret != 0) {
  26189. goto done;
  26190. }
  26191. #ifndef NO_SHA256
  26192. /* Test for SHA2-256 */
  26193. mp_read_radix(expR, expRstr256, MP_RADIX_HEX);
  26194. mp_read_radix(expS, expSstr256, MP_RADIX_HEX);
  26195. ret = ecdsa_test_deterministic_k_rs(key, WC_HASH_TYPE_SHA256, msg, rng,
  26196. r, s, expR, expS);
  26197. if (ret != 0) {
  26198. goto done;
  26199. }
  26200. #endif /* NO_SHA256 */
  26201. #ifdef WOLFSSL_SHA384
  26202. /* Test for SHA2-384 */
  26203. mp_read_radix(expR, expRstr384, MP_RADIX_HEX);
  26204. mp_read_radix(expS, expSstr384, MP_RADIX_HEX);
  26205. ret = ecdsa_test_deterministic_k_rs(key, WC_HASH_TYPE_SHA384, msg, rng,
  26206. r, s, expR, expS);
  26207. if (ret != 0) {
  26208. goto done;
  26209. }
  26210. #endif /* WOLFSSL_SHA384 */
  26211. #ifdef WOLFSSL_SHA512
  26212. /* Test for SHA2-512 */
  26213. mp_read_radix(expR, expRstr512, MP_RADIX_HEX);
  26214. mp_read_radix(expS, expSstr512, MP_RADIX_HEX);
  26215. ret = ecdsa_test_deterministic_k_rs(key, WC_HASH_TYPE_SHA512, msg, rng,
  26216. r, s, expR, expS);
  26217. if (ret != 0) {
  26218. goto done;
  26219. }
  26220. #endif /* WOLFSSL_SHA512 */
  26221. done:
  26222. if (key_inited)
  26223. wc_ecc_free(key);
  26224. if (tmp_mp_ints_inited) {
  26225. mp_free(r);
  26226. mp_free(s);
  26227. mp_free(expR);
  26228. mp_free(expS);
  26229. }
  26230. #ifdef WOLFSSL_SMALL_STACK
  26231. if (key != NULL)
  26232. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26233. if (r != NULL)
  26234. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26235. if (s != NULL)
  26236. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26237. if (expR != NULL)
  26238. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26239. if (expS != NULL)
  26240. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26241. #endif
  26242. return ret;
  26243. }
  26244. #endif /* HAVE_ECC521 */
  26245. #endif /* WOLFSSL_PUBLIC_MP */
  26246. #endif /* HAVE_ECC_SIGN && (WOLFSSL_ECDSA_DETERMINISTIC_K ||
  26247. WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  26248. && (!HAVE_FIPS || FIPS_VERSION_GE(5,3)) */
  26249. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  26250. !defined(WOLFSSL_KCAPI_ECC)
  26251. static wc_test_ret_t ecc_test_sign_vectors(WC_RNG* rng)
  26252. {
  26253. wc_test_ret_t ret;
  26254. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26255. ecc_key *key = NULL;
  26256. #else
  26257. ecc_key key[1];
  26258. #endif
  26259. int key_inited = 0;
  26260. byte sig[72];
  26261. word32 sigSz;
  26262. WOLFSSL_SMALL_STACK_STATIC const unsigned char hash[32] = "test wolfSSL deterministic sign";
  26263. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  26264. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  26265. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  26266. WOLFSSL_SMALL_STACK_STATIC const byte k[1] = { 0x02 };
  26267. WOLFSSL_SMALL_STACK_STATIC const byte expSig[71] = {
  26268. 0x30, 0x45, 0x02, 0x20, 0x7c, 0xf2, 0x7b, 0x18,
  26269. 0x8d, 0x03, 0x4f, 0x7e, 0x8a, 0x52, 0x38, 0x03,
  26270. 0x04, 0xb5, 0x1a, 0xc3, 0xc0, 0x89, 0x69, 0xe2,
  26271. 0x77, 0xf2, 0x1b, 0x35, 0xa6, 0x0b, 0x48, 0xfc,
  26272. 0x47, 0x66, 0x99, 0x78, 0x02, 0x21, 0x00, 0xa8,
  26273. 0x43, 0xa0, 0xce, 0x6c, 0x5e, 0x17, 0x8a, 0x53,
  26274. 0x4d, 0xaf, 0xd2, 0x95, 0x78, 0x9f, 0x84, 0x4f,
  26275. 0x94, 0xb8, 0x75, 0xa3, 0x19, 0xa5, 0xd4, 0xdf,
  26276. 0xe1, 0xd4, 0x5e, 0x9d, 0x97, 0xfe, 0x81
  26277. };
  26278. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26279. if ((key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  26280. return MEMORY_E;
  26281. #endif
  26282. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  26283. if (ret != 0) {
  26284. goto done;
  26285. }
  26286. key_inited = 1;
  26287. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  26288. if (ret != 0) {
  26289. goto done;
  26290. }
  26291. #if (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) \
  26292. && (HAVE_FIPS_VERSION > 2)))
  26293. wc_ecc_set_flags(key, WC_ECC_FLAG_DEC_SIGN);
  26294. #endif
  26295. ret = wc_ecc_sign_set_k(k, sizeof(k), key);
  26296. if (ret != 0) {
  26297. goto done;
  26298. }
  26299. sigSz = sizeof(sig);
  26300. do {
  26301. #if defined(WOLFSSL_ASYNC_CRYPT)
  26302. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26303. #endif
  26304. if (ret == 0)
  26305. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  26306. } while (ret == WC_PENDING_E);
  26307. if (ret != 0) {
  26308. goto done;
  26309. }
  26310. TEST_SLEEP();
  26311. if (sigSz != sizeof(expSig)) {
  26312. ret = WC_TEST_RET_ENC_NC;
  26313. goto done;
  26314. }
  26315. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  26316. ret = WC_TEST_RET_ENC_NC;
  26317. goto done;
  26318. }
  26319. sigSz = sizeof(sig);
  26320. do {
  26321. #if defined(WOLFSSL_ASYNC_CRYPT)
  26322. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26323. #endif
  26324. if (ret == 0)
  26325. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  26326. } while (ret == WC_PENDING_E);
  26327. if (ret != 0) {
  26328. goto done;
  26329. }
  26330. TEST_SLEEP();
  26331. done:
  26332. if (key_inited)
  26333. wc_ecc_free(key);
  26334. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26335. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26336. #endif
  26337. return ret;
  26338. }
  26339. #endif
  26340. #if defined(HAVE_ECC_CDH) && defined(HAVE_ECC_DHE)
  26341. static wc_test_ret_t ecc_test_cdh_vectors(WC_RNG* rng)
  26342. {
  26343. wc_test_ret_t ret;
  26344. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26345. ecc_key *pub_key = (ecc_key *)XMALLOC(sizeof *pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26346. ecc_key *priv_key = (ecc_key *)XMALLOC(sizeof *priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26347. #else
  26348. ecc_key pub_key[1], priv_key[1];
  26349. #endif
  26350. byte sharedA[32] = {0}, sharedB[32] = {0};
  26351. word32 x, z;
  26352. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSx = "700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287";
  26353. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSy = "db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac";
  26354. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  26355. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  26356. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  26357. WOLFSSL_SMALL_STACK_STATIC const char* ZIUT = "46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b";
  26358. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26359. if ((pub_key == NULL) ||
  26360. (priv_key == NULL)) {
  26361. ret = MEMORY_E;
  26362. goto done;
  26363. }
  26364. #endif
  26365. XMEMSET(pub_key, 0, sizeof *pub_key);
  26366. XMEMSET(priv_key, 0, sizeof *priv_key);
  26367. /* setup private and public keys */
  26368. ret = wc_ecc_init_ex(pub_key, HEAP_HINT, devId);
  26369. if (ret != 0)
  26370. goto done;
  26371. ret = wc_ecc_init_ex(priv_key, HEAP_HINT, devId);
  26372. if (ret != 0)
  26373. goto done;
  26374. wc_ecc_set_flags(pub_key, WC_ECC_FLAG_COFACTOR);
  26375. wc_ecc_set_flags(priv_key, WC_ECC_FLAG_COFACTOR);
  26376. ret = wc_ecc_import_raw(pub_key, QCAVSx, QCAVSy, NULL, "SECP256R1");
  26377. if (ret != 0)
  26378. goto done;
  26379. ret = wc_ecc_import_raw(priv_key, QIUTx, QIUTy, dIUT, "SECP256R1");
  26380. if (ret != 0)
  26381. goto done;
  26382. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  26383. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  26384. !defined(HAVE_SELFTEST)
  26385. ret = wc_ecc_set_rng(priv_key, rng);
  26386. if (ret != 0)
  26387. goto done;
  26388. #else
  26389. (void)rng;
  26390. #endif
  26391. /* compute ECC Cofactor shared secret */
  26392. x = sizeof(sharedA);
  26393. do {
  26394. #if defined(WOLFSSL_ASYNC_CRYPT)
  26395. ret = wc_AsyncWait(ret, &priv_key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26396. #endif
  26397. if (ret == 0)
  26398. ret = wc_ecc_shared_secret(priv_key, pub_key, sharedA, &x);
  26399. } while (ret == WC_PENDING_E);
  26400. if (ret != 0) {
  26401. goto done;
  26402. }
  26403. TEST_SLEEP();
  26404. /* read in expected Z */
  26405. z = sizeof(sharedB);
  26406. ret = Base16_Decode((const byte*)ZIUT, (word32)XSTRLEN(ZIUT), sharedB, &z);
  26407. if (ret != 0)
  26408. goto done;
  26409. /* compare results */
  26410. if (x != z || XMEMCMP(sharedA, sharedB, x)) {
  26411. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26412. }
  26413. done:
  26414. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26415. if (priv_key) {
  26416. wc_ecc_free(priv_key);
  26417. XFREE(priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26418. }
  26419. if (pub_key) {
  26420. wc_ecc_free(pub_key);
  26421. XFREE(pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26422. }
  26423. #else
  26424. wc_ecc_free(priv_key);
  26425. wc_ecc_free(pub_key);
  26426. #endif
  26427. return ret;
  26428. }
  26429. #endif /* HAVE_ECC_CDH && HAVE_ECC_DHE */
  26430. #endif /* HAVE_ECC_VECTOR_TEST */
  26431. #ifdef HAVE_ECC_KEY_IMPORT
  26432. /* returns 0 on success */
  26433. static wc_test_ret_t ecc_test_make_pub(WC_RNG* rng)
  26434. {
  26435. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26436. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26437. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  26438. ecc_key *pub = (ecc_key *)XMALLOC(sizeof *pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26439. #endif
  26440. byte *exportBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26441. byte *tmp = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26442. #else
  26443. ecc_key key[1];
  26444. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  26445. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  26446. ecc_key pub[1];
  26447. #endif
  26448. byte exportBuf[ECC_BUFSIZE];
  26449. byte tmp[ECC_BUFSIZE];
  26450. #endif
  26451. const byte* msg = (const byte*)"test wolfSSL ECC public gen";
  26452. word32 x;
  26453. word32 tmpSz;
  26454. wc_test_ret_t ret = 0;
  26455. ecc_point* pubPoint = NULL;
  26456. #ifdef HAVE_ECC_VERIFY
  26457. int verify = 0;
  26458. #endif
  26459. #ifdef NO_ASN
  26460. /* private d for eccKeyDerFile / ecc_key_der_256 */
  26461. const byte keyPriv[] = {
  26462. 0x45, 0xB6, 0x69, 0x02, 0x73, 0x9C, 0x6C, 0x85,
  26463. 0xA1, 0x38, 0x5B, 0x72, 0xE8, 0xE8, 0xC7, 0xAC,
  26464. 0xC4, 0x03, 0x8D, 0x53, 0x35, 0x04, 0xFA, 0x6C,
  26465. 0x28, 0xDC, 0x34, 0x8D, 0xE1, 0xA8, 0x09, 0x8C
  26466. };
  26467. #endif
  26468. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26469. if ((key == NULL) ||
  26470. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  26471. (pub == NULL) ||
  26472. #endif
  26473. (exportBuf == NULL) ||
  26474. (tmp == NULL))
  26475. ERROR_OUT(MEMORY_E, done);
  26476. #endif
  26477. (void)msg;
  26478. (void)verify;
  26479. (void)exportBuf;
  26480. (void)rng;
  26481. wc_ecc_init_ex(key, HEAP_HINT, devId);
  26482. #ifndef NO_ECC256
  26483. #if defined(USE_CERT_BUFFERS_256)
  26484. {
  26485. XMEMCPY(tmp, ecc_key_der_256, (size_t)sizeof_ecc_key_der_256);
  26486. tmpSz = (size_t)sizeof_ecc_key_der_256;
  26487. }
  26488. #elif !defined(NO_FILESYSTEM)
  26489. {
  26490. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  26491. if (!file) {
  26492. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26493. }
  26494. tmpSz = (word32)XFREAD(tmp, 1, ECC_BUFSIZE, file);
  26495. XFCLOSE(file);
  26496. if (tmpSz == 0)
  26497. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26498. }
  26499. #else
  26500. {
  26501. WOLFSSL_MSG("No file system and USE_CERT_BUFFERS_256 not defined.(2)");
  26502. ERROR_OUT(ASN_PARSE_E, done);
  26503. }
  26504. #endif /* USE_CERT_BUFFERS_256 */
  26505. /* import private only then test with */
  26506. ret = wc_ecc_import_private_key(tmp, tmpSz, NULL, 0, NULL);
  26507. if (ret == 0) {
  26508. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26509. }
  26510. ret = wc_ecc_import_private_key(NULL, tmpSz, NULL, 0, key);
  26511. if (ret == 0) {
  26512. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26513. }
  26514. #ifndef NO_ASN
  26515. x = 0;
  26516. ret = wc_EccPrivateKeyDecode(tmp, &x, key, tmpSz);
  26517. if (ret != 0)
  26518. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26519. #ifdef HAVE_ECC_KEY_EXPORT
  26520. x = ECC_BUFSIZE;
  26521. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  26522. if (ret != 0)
  26523. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26524. /* make private only key */
  26525. wc_ecc_free(key);
  26526. wc_ecc_init_ex(key, HEAP_HINT, devId);
  26527. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  26528. if (ret != 0)
  26529. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26530. x = ECC_BUFSIZE;
  26531. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  26532. if (ret == 0) {
  26533. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26534. }
  26535. #endif /* HAVE_ECC_KEY_EXPORT */
  26536. #else
  26537. /* Load raw private d directly */
  26538. ret = wc_ecc_import_private_key(keyPriv, sizeof(keyPriv), NULL, 0, key);
  26539. #endif /* !NO_ASN */
  26540. ret = wc_ecc_make_pub(NULL, NULL);
  26541. if (ret == 0) {
  26542. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26543. }
  26544. TEST_SLEEP();
  26545. #ifndef WOLFSSL_NO_MALLOC
  26546. pubPoint = wc_ecc_new_point_h(HEAP_HINT);
  26547. if (pubPoint == NULL) {
  26548. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26549. }
  26550. #if !defined(WOLFSSL_CRYPTOCELL)
  26551. ret = wc_ecc_make_pub(key, pubPoint);
  26552. #if defined(WOLFSSL_ASYNC_CRYPT)
  26553. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  26554. #endif
  26555. if (ret != 0)
  26556. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26557. #endif /* !WOLFSSL_CRYPTOCELL */
  26558. TEST_SLEEP();
  26559. #ifdef HAVE_ECC_KEY_EXPORT
  26560. /* export should still fail, is private only key */
  26561. x = ECC_BUFSIZE;
  26562. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  26563. if (ret == 0) {
  26564. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26565. }
  26566. #endif /* HAVE_ECC_KEY_EXPORT */
  26567. #endif /* !WOLFSSL_NO_MALLOC */
  26568. #endif /* !NO_ECC256 */
  26569. /* create a new key since above test for loading key is not supported */
  26570. #if defined(WOLFSSL_CRYPTOCELL) || defined(NO_ECC256) || \
  26571. defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_SE050) || \
  26572. defined(WOLFSSL_SECO_CAAM) || defined(WOLFSSL_IMXRT1170_CAAM)
  26573. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  26574. if (ret != 0)
  26575. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26576. #endif
  26577. #if defined(HAVE_ECC_SIGN) && (!defined(ECC_TIMING_RESISTANT) || \
  26578. (defined(ECC_TIMING_RESISTANT) && !defined(WC_NO_RNG))) && \
  26579. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(HAVE_ECC_DETERMINISTIC_K)
  26580. tmpSz = ECC_BUFSIZE;
  26581. ret = 0;
  26582. do {
  26583. #if defined(WOLFSSL_ASYNC_CRYPT)
  26584. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26585. #endif
  26586. if (ret == 0) {
  26587. ret = wc_ecc_sign_hash(msg, (word32)XSTRLEN((const char* )msg), tmp,
  26588. &tmpSz, rng, key);
  26589. }
  26590. } while (ret == WC_PENDING_E);
  26591. if (ret != 0)
  26592. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26593. TEST_SLEEP();
  26594. #ifdef HAVE_ECC_VERIFY
  26595. /* try verify with private only key */
  26596. ret = 0;
  26597. do {
  26598. #if defined(WOLFSSL_ASYNC_CRYPT)
  26599. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26600. #endif
  26601. if (ret == 0) {
  26602. ret = wc_ecc_verify_hash(tmp, tmpSz, msg,
  26603. (word32)XSTRLEN((const char*)msg), &verify, key);
  26604. }
  26605. } while (ret == WC_PENDING_E);
  26606. if (ret != 0)
  26607. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26608. if (verify != 1) {
  26609. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26610. }
  26611. TEST_SLEEP();
  26612. #ifdef HAVE_ECC_KEY_EXPORT
  26613. /* exporting the public part should now work */
  26614. x = ECC_BUFSIZE;
  26615. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  26616. if (ret != 0)
  26617. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26618. #endif /* HAVE_ECC_KEY_EXPORT */
  26619. #endif /* HAVE_ECC_VERIFY */
  26620. #endif /* HAVE_ECC_SIGN */
  26621. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  26622. /* now test private only key with creating a shared secret */
  26623. x = ECC_BUFSIZE;
  26624. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  26625. if (ret != 0)
  26626. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26627. #if !defined(WOLFSSL_QNX_CAAM) && !defined(WOLFSSL_SE050)
  26628. /* make private only key */
  26629. wc_ecc_free(key);
  26630. wc_ecc_init_ex(key, HEAP_HINT, devId);
  26631. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  26632. if (ret != 0)
  26633. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26634. /* check that public export fails with private only key */
  26635. x = ECC_BUFSIZE;
  26636. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  26637. if (ret == 0) {
  26638. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26639. }
  26640. #endif /* WOLFSSL_QNX_CAAM */
  26641. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  26642. /* make public key for shared secret */
  26643. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  26644. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, pub);
  26645. #ifdef HAVE_ECC_CDH
  26646. wc_ecc_set_flags(key, WC_ECC_FLAG_COFACTOR);
  26647. #endif
  26648. #if defined(WOLFSSL_ASYNC_CRYPT)
  26649. ret = wc_AsyncWait(ret, &pub->asyncDev, WC_ASYNC_FLAG_NONE);
  26650. #endif
  26651. if (ret != 0)
  26652. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26653. TEST_SLEEP();
  26654. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  26655. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  26656. !defined(HAVE_SELFTEST)
  26657. ret = wc_ecc_set_rng(key, rng);
  26658. if (ret != 0)
  26659. goto done;
  26660. #endif
  26661. x = ECC_BUFSIZE;
  26662. do {
  26663. #if defined(WOLFSSL_ASYNC_CRYPT)
  26664. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26665. #endif
  26666. if (ret == 0) {
  26667. ret = wc_ecc_shared_secret(key, pub, exportBuf, &x);
  26668. }
  26669. } while (ret == WC_PENDING_E);
  26670. wc_ecc_free(pub);
  26671. if (ret != 0)
  26672. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26673. TEST_SLEEP();
  26674. #endif /* HAVE_ECC_DHE && HAVE_ECC_KEY_EXPORT && !WC_NO_RNG */
  26675. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  26676. ret = 0;
  26677. done:
  26678. wc_ecc_del_point_h(pubPoint, HEAP_HINT);
  26679. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26680. if (key != NULL) {
  26681. wc_ecc_free(key);
  26682. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26683. }
  26684. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  26685. if (pub != NULL)
  26686. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26687. #endif
  26688. if (exportBuf != NULL)
  26689. XFREE(exportBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26690. if (tmp != NULL)
  26691. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26692. #else
  26693. wc_ecc_free(key);
  26694. #endif
  26695. return ret;
  26696. }
  26697. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  26698. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  26699. static wc_test_ret_t ecc_test_key_decode(WC_RNG* rng, int keySize)
  26700. {
  26701. wc_test_ret_t ret;
  26702. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26703. ecc_key *eccKey = (ecc_key *)XMALLOC(sizeof *eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26704. byte *tmpBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26705. #else
  26706. ecc_key eccKey[1];
  26707. byte tmpBuf[ECC_BUFSIZE];
  26708. #endif
  26709. word32 tmpSz;
  26710. word32 idx;
  26711. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26712. if ((eccKey == NULL) || (tmpBuf == NULL))
  26713. ERROR_OUT(MEMORY_E, done);
  26714. #endif
  26715. ret = wc_ecc_init(eccKey);
  26716. if (ret != 0) {
  26717. goto done;
  26718. }
  26719. ret = wc_ecc_make_key(rng, keySize, eccKey);
  26720. #if defined(WOLFSSL_ASYNC_CRYPT)
  26721. ret = wc_AsyncWait(ret, &eccKey->asyncDev, WC_ASYNC_FLAG_NONE);
  26722. #endif
  26723. if (ret != 0) {
  26724. goto done;
  26725. }
  26726. tmpSz = ECC_BUFSIZE;
  26727. ret = wc_EccKeyToDer(eccKey, tmpBuf, tmpSz);
  26728. wc_ecc_free(eccKey);
  26729. if (ret < 0) {
  26730. goto done;
  26731. }
  26732. tmpSz = (word32)ret;
  26733. ret = wc_ecc_init(eccKey);
  26734. if (ret != 0) {
  26735. goto done;
  26736. }
  26737. idx = 0;
  26738. ret = wc_EccPrivateKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  26739. if (ret != 0) {
  26740. goto done;
  26741. }
  26742. wc_ecc_free(eccKey);
  26743. ret = wc_ecc_init(eccKey);
  26744. if (ret != 0) {
  26745. goto done;
  26746. }
  26747. idx = 0;
  26748. ret = wc_EccPublicKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  26749. if (ret != 0) {
  26750. goto done;
  26751. }
  26752. ret = 0;
  26753. done:
  26754. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26755. if (eccKey != NULL) {
  26756. wc_ecc_free(eccKey);
  26757. XFREE(eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26758. }
  26759. if (tmpBuf != NULL)
  26760. XFREE(tmpBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26761. #else
  26762. wc_ecc_free(eccKey);
  26763. #endif
  26764. return ret;
  26765. }
  26766. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  26767. #endif /* HAVE_ECC_KEY_IMPORT */
  26768. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  26769. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  26770. static wc_test_ret_t ecc_test_key_gen(WC_RNG* rng, int keySize)
  26771. {
  26772. wc_test_ret_t ret = 0;
  26773. int derSz;
  26774. #ifdef HAVE_PKCS8
  26775. word32 pkcs8Sz;
  26776. #endif
  26777. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26778. byte *der = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26779. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26780. #else
  26781. byte der[ECC_BUFSIZE];
  26782. ecc_key userA[1];
  26783. #endif
  26784. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26785. if ((der == NULL) || (userA == NULL))
  26786. ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), done);
  26787. #endif
  26788. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  26789. if (ret != 0)
  26790. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26791. ret = wc_ecc_make_key(rng, keySize, userA);
  26792. #if defined(WOLFSSL_ASYNC_CRYPT)
  26793. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  26794. #endif
  26795. if (ret != 0)
  26796. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26797. TEST_SLEEP();
  26798. ret = wc_ecc_check_key(userA);
  26799. if (ret != 0)
  26800. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26801. TEST_SLEEP();
  26802. derSz = wc_EccKeyToDer(userA, der, ECC_BUFSIZE);
  26803. if (derSz < 0) {
  26804. ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
  26805. }
  26806. ret = SaveDerAndPem(der, derSz, eccCaKeyTempFile, eccCaKeyPemFile,
  26807. ECC_PRIVATEKEY_TYPE);
  26808. if (ret != 0)
  26809. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26810. /* test export of public key */
  26811. derSz = wc_EccPublicKeyToDer(userA, der, ECC_BUFSIZE, 1);
  26812. if (derSz < 0) {
  26813. ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
  26814. }
  26815. if (derSz == 0) {
  26816. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26817. }
  26818. #ifdef HAVE_COMP_KEY
  26819. /* test export of compressed public key */
  26820. derSz = wc_EccPublicKeyToDer_ex(userA, der, ECC_BUFSIZE, 1, 1);
  26821. if (derSz < 0) {
  26822. ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
  26823. }
  26824. if (derSz == 0) {
  26825. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26826. }
  26827. #endif
  26828. ret = SaveDerAndPem(der, derSz, eccPubKeyDerFile, NULL, 0);
  26829. if (ret != 0)
  26830. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26831. #ifdef HAVE_PKCS8
  26832. /* test export of PKCS#8 unencrypted private key */
  26833. pkcs8Sz = FOURK_BUF;
  26834. derSz = wc_EccPrivateKeyToPKCS8(userA, der, &pkcs8Sz);
  26835. if (derSz < 0) {
  26836. ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
  26837. }
  26838. if (derSz == 0) {
  26839. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26840. }
  26841. ret = SaveDerAndPem(der, derSz, eccPkcs8KeyDerFile, NULL, 0);
  26842. if (ret != 0) {
  26843. goto done;
  26844. }
  26845. #endif /* HAVE_PKCS8 */
  26846. done:
  26847. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26848. if (der != NULL)
  26849. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26850. if (userA != NULL) {
  26851. wc_ecc_free(userA);
  26852. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26853. }
  26854. #else
  26855. wc_ecc_free(userA);
  26856. #endif
  26857. return ret;
  26858. }
  26859. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  26860. static wc_test_ret_t ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount,
  26861. int curve_id, const ecc_set_type* dp)
  26862. {
  26863. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  26864. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  26865. WC_DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  26866. WC_DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  26867. word32 y;
  26868. #endif
  26869. #ifdef HAVE_ECC_KEY_EXPORT
  26870. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  26871. WC_DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  26872. #endif
  26873. word32 x = 0;
  26874. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  26875. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC)) && \
  26876. defined(HAVE_ECC_SIGN)
  26877. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  26878. WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  26879. int i;
  26880. #ifdef HAVE_ECC_VERIFY
  26881. int verify;
  26882. #endif /* HAVE_ECC_VERIFY */
  26883. #endif /* HAVE_ECC_SIGN */
  26884. wc_test_ret_t ret;
  26885. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26886. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26887. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26888. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26889. #else
  26890. ecc_key userA[1];
  26891. ecc_key userB[1];
  26892. ecc_key pubKey[1];
  26893. #endif
  26894. #ifndef WC_NO_RNG
  26895. int curveSize;
  26896. #endif
  26897. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  26898. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  26899. WC_ALLOC_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  26900. WC_ALLOC_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  26901. #endif
  26902. #ifdef HAVE_ECC_KEY_EXPORT
  26903. WC_ALLOC_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  26904. #endif
  26905. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  26906. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC)) && \
  26907. defined(HAVE_ECC_SIGN)
  26908. WC_ALLOC_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  26909. WC_ALLOC_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  26910. #endif
  26911. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  26912. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  26913. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  26914. if (sharedA == NULL || sharedB == NULL)
  26915. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26916. #endif
  26917. #ifdef HAVE_ECC_KEY_EXPORT
  26918. if (exportBuf == NULL)
  26919. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26920. #endif
  26921. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  26922. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC)) && \
  26923. defined(HAVE_ECC_SIGN)
  26924. if (sig == NULL || digest == NULL)
  26925. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26926. #endif
  26927. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  26928. (void)testVerifyCount;
  26929. (void)dp;
  26930. (void)x;
  26931. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26932. if ((userA == NULL) ||
  26933. (userB == NULL) ||
  26934. (pubKey == NULL))
  26935. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26936. #endif
  26937. XMEMSET(userA, 0, sizeof *userA);
  26938. XMEMSET(userB, 0, sizeof *userB);
  26939. XMEMSET(pubKey, 0, sizeof *pubKey);
  26940. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  26941. if (ret != 0)
  26942. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26943. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  26944. if (ret != 0)
  26945. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26946. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  26947. if (ret != 0)
  26948. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26949. #ifdef WOLFSSL_CUSTOM_CURVES
  26950. if (dp != NULL) {
  26951. ret = wc_ecc_set_custom_curve(userA, dp);
  26952. if (ret != 0)
  26953. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26954. ret = wc_ecc_set_custom_curve(userB, dp);
  26955. if (ret != 0)
  26956. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26957. }
  26958. #endif
  26959. #ifndef WC_NO_RNG
  26960. ret = wc_ecc_make_key_ex(rng, keySize, userA, curve_id);
  26961. #if defined(WOLFSSL_ASYNC_CRYPT)
  26962. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  26963. #endif
  26964. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  26965. if (ret == NO_VALID_DEVID) {
  26966. ret = 0;
  26967. goto done; /* no software case */
  26968. }
  26969. #endif
  26970. if (ret == ECC_CURVE_OID_E)
  26971. goto done; /* catch case, where curve is not supported */
  26972. if (ret != 0)
  26973. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26974. TEST_SLEEP();
  26975. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  26976. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  26977. if (curveSize != userA->dp->size)
  26978. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26979. }
  26980. ret = wc_ecc_check_key(userA);
  26981. if (ret != 0)
  26982. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26983. TEST_SLEEP();
  26984. /* ATECC508/608 configuration may not support more than one ECDH key */
  26985. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  26986. ret = wc_ecc_make_key_ex(rng, keySize, userB, curve_id);
  26987. #if defined(WOLFSSL_ASYNC_CRYPT)
  26988. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  26989. #endif
  26990. if (ret != 0)
  26991. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26992. TEST_SLEEP();
  26993. /* only perform the below tests if the key size matches */
  26994. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  26995. /* Not an error, just not a key size match */
  26996. WARNING_OUT(ECC_CURVE_OID_E, done);
  26997. #ifdef HAVE_ECC_DHE
  26998. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  26999. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  27000. !defined(HAVE_SELFTEST)
  27001. ret = wc_ecc_set_rng(userA, rng);
  27002. if (ret != 0)
  27003. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27004. ret = wc_ecc_set_rng(userB, rng);
  27005. if (ret != 0)
  27006. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27007. #endif
  27008. x = ECC_SHARED_SIZE;
  27009. do {
  27010. #if defined(WOLFSSL_ASYNC_CRYPT)
  27011. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  27012. #endif
  27013. if (ret == 0)
  27014. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  27015. } while (ret == WC_PENDING_E);
  27016. if (ret != 0)
  27017. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27018. TEST_SLEEP();
  27019. y = ECC_SHARED_SIZE;
  27020. do {
  27021. #if defined(WOLFSSL_ASYNC_CRYPT)
  27022. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  27023. #endif
  27024. if (ret == 0)
  27025. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  27026. } while (ret == WC_PENDING_E);
  27027. if (ret != 0)
  27028. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27029. if (y != x)
  27030. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  27031. if (XMEMCMP(sharedA, sharedB, x))
  27032. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  27033. TEST_SLEEP();
  27034. #ifdef HAVE_ECC_CDH
  27035. /* add cofactor flag */
  27036. wc_ecc_set_flags(userA, WC_ECC_FLAG_COFACTOR);
  27037. wc_ecc_set_flags(userB, WC_ECC_FLAG_COFACTOR);
  27038. x = ECC_SHARED_SIZE;
  27039. do {
  27040. #if defined(WOLFSSL_ASYNC_CRYPT)
  27041. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  27042. #endif
  27043. if (ret == 0)
  27044. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  27045. } while (ret == WC_PENDING_E);
  27046. if (ret != 0)
  27047. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27048. TEST_SLEEP();
  27049. y = ECC_SHARED_SIZE;
  27050. do {
  27051. #if defined(WOLFSSL_ASYNC_CRYPT)
  27052. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  27053. #endif
  27054. if (ret == 0)
  27055. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  27056. } while (ret == WC_PENDING_E);
  27057. if (ret != 0)
  27058. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27059. if (y != x)
  27060. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  27061. if (XMEMCMP(sharedA, sharedB, x))
  27062. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  27063. TEST_SLEEP();
  27064. /* remove cofactor flag */
  27065. wc_ecc_set_flags(userA, 0);
  27066. wc_ecc_set_flags(userB, 0);
  27067. #endif /* HAVE_ECC_CDH */
  27068. #endif /* HAVE_ECC_DHE */
  27069. #endif /* !WOLFSSL_ATECC508A && WOLFSSL_ATECC608A */
  27070. #ifdef HAVE_ECC_KEY_EXPORT
  27071. x = ECC_KEY_EXPORT_BUF_SIZE;
  27072. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  27073. if (ret != 0)
  27074. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27075. #ifdef HAVE_ECC_KEY_IMPORT
  27076. #ifdef WOLFSSL_CUSTOM_CURVES
  27077. if (dp != NULL) {
  27078. ret = wc_ecc_set_custom_curve(pubKey, dp);
  27079. if (ret != 0)
  27080. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27081. }
  27082. #endif
  27083. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  27084. if (ret != 0)
  27085. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27086. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  27087. #ifdef HAVE_ECC_DHE
  27088. y = ECC_SHARED_SIZE;
  27089. do {
  27090. #if defined(WOLFSSL_ASYNC_CRYPT)
  27091. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  27092. #endif
  27093. if (ret == 0)
  27094. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  27095. } while (ret == WC_PENDING_E);
  27096. if (ret != 0)
  27097. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27098. if (XMEMCMP(sharedA, sharedB, y))
  27099. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  27100. TEST_SLEEP();
  27101. #endif /* HAVE_ECC_DHE */
  27102. #ifdef HAVE_COMP_KEY
  27103. /* try compressed export / import too */
  27104. x = ECC_KEY_EXPORT_BUF_SIZE;
  27105. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  27106. if (ret != 0)
  27107. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27108. wc_ecc_free(pubKey);
  27109. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  27110. if (ret != 0)
  27111. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27112. #ifdef WOLFSSL_CUSTOM_CURVES
  27113. if (dp != NULL) {
  27114. ret = wc_ecc_set_custom_curve(pubKey, dp);
  27115. if (ret != 0)
  27116. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27117. }
  27118. #endif
  27119. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  27120. if (ret != 0)
  27121. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27122. #ifdef HAVE_ECC_DHE
  27123. y = ECC_SHARED_SIZE;
  27124. do {
  27125. #if defined(WOLFSSL_ASYNC_CRYPT)
  27126. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  27127. #endif
  27128. if (ret == 0)
  27129. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  27130. } while (ret == WC_PENDING_E);
  27131. if (ret != 0)
  27132. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27133. if (XMEMCMP(sharedA, sharedB, y))
  27134. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  27135. TEST_SLEEP();
  27136. #endif /* HAVE_ECC_DHE */
  27137. #endif /* HAVE_COMP_KEY */
  27138. #endif /* !WOLFSSL_ATECC508A && !WOLFSSL_ATECC608A */
  27139. #endif /* !WC_NO_RNG */
  27140. #endif /* HAVE_ECC_KEY_IMPORT */
  27141. #endif /* HAVE_ECC_KEY_EXPORT */
  27142. /* For KCAPI cannot sign using generated ECDH key */
  27143. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  27144. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC))
  27145. #ifdef HAVE_ECC_SIGN
  27146. /* ECC w/out Shamir has issue with all 0 digest */
  27147. /* WC_BIGINT doesn't have 0 len well on hardware */
  27148. /* Cryptocell has issues with all 0 digest */
  27149. #if defined(ECC_SHAMIR) && !defined(WOLFSSL_ASYNC_CRYPT) && \
  27150. !defined(WOLFSSL_CRYPTOCELL)
  27151. /* test DSA sign hash with zeros */
  27152. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  27153. digest[i] = 0;
  27154. }
  27155. x = ECC_SIG_SIZE;
  27156. do {
  27157. #if defined(WOLFSSL_ASYNC_CRYPT)
  27158. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  27159. #endif
  27160. if (ret == 0)
  27161. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  27162. userA);
  27163. } while (ret == WC_PENDING_E);
  27164. if (ret != 0)
  27165. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27166. TEST_SLEEP();
  27167. #ifdef HAVE_ECC_VERIFY
  27168. for (i=0; i<testVerifyCount; i++) {
  27169. verify = 0;
  27170. do {
  27171. #if defined(WOLFSSL_ASYNC_CRYPT)
  27172. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  27173. #endif
  27174. if (ret == 0)
  27175. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  27176. &verify, userA);
  27177. } while (ret == WC_PENDING_E);
  27178. if (ret != 0)
  27179. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27180. if (verify != 1)
  27181. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  27182. TEST_SLEEP();
  27183. }
  27184. #endif /* HAVE_ECC_VERIFY */
  27185. #endif /* ECC_SHAMIR && !WOLFSSL_ASYNC_CRYPT && !WOLFSSL_CRYPTOCELL */
  27186. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  27187. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  27188. digest[i] = (byte)i;
  27189. }
  27190. x = ECC_SIG_SIZE;
  27191. do {
  27192. #if defined(WOLFSSL_ASYNC_CRYPT)
  27193. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  27194. #endif
  27195. if (ret == 0)
  27196. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  27197. } while (ret == WC_PENDING_E);
  27198. if (ret != 0)
  27199. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27200. TEST_SLEEP();
  27201. #ifdef HAVE_ECC_VERIFY
  27202. for (i=0; i<testVerifyCount; i++) {
  27203. verify = 0;
  27204. do {
  27205. #if defined(WOLFSSL_ASYNC_CRYPT)
  27206. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  27207. #endif
  27208. if (ret == 0)
  27209. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify, userA);
  27210. } while (ret == WC_PENDING_E);
  27211. if (ret != 0)
  27212. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27213. if (verify != 1)
  27214. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  27215. TEST_SLEEP();
  27216. }
  27217. #endif /* HAVE_ECC_VERIFY */
  27218. #endif /* HAVE_ECC_SIGN */
  27219. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT &&
  27220. * !WC_NO_RNG && !WOLFSSL_KCAPI_ECC) */
  27221. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  27222. !defined(WOLFSSL_ATECC508) && !defined(WOLFSSL_ATECC608A) && \
  27223. !defined(WOLFSSL_KCAPI_ECC)
  27224. x = ECC_KEY_EXPORT_BUF_SIZE;
  27225. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  27226. if (ret != 0)
  27227. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27228. #elif defined(HAVE_ECC_KEY_EXPORT)
  27229. (void)exportBuf;
  27230. #endif /* HAVE_ECC_KEY_EXPORT */
  27231. done:
  27232. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27233. if (userA != NULL) {
  27234. wc_ecc_free(userA);
  27235. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27236. }
  27237. if (userB != NULL) {
  27238. wc_ecc_free(userB);
  27239. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27240. }
  27241. if (pubKey != NULL) {
  27242. wc_ecc_free(pubKey);
  27243. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27244. }
  27245. #else
  27246. wc_ecc_free(pubKey);
  27247. wc_ecc_free(userB);
  27248. wc_ecc_free(userA);
  27249. #endif
  27250. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  27251. WC_FREE_VAR(sharedA, HEAP_HINT);
  27252. WC_FREE_VAR(sharedB, HEAP_HINT);
  27253. #endif
  27254. #ifdef HAVE_ECC_KEY_EXPORT
  27255. WC_FREE_VAR(exportBuf, HEAP_HINT);
  27256. #endif
  27257. #ifdef HAVE_ECC_SIGN
  27258. WC_FREE_VAR(sig, HEAP_HINT);
  27259. WC_FREE_VAR(digest, HEAP_HINT);
  27260. #endif
  27261. (void)keySize;
  27262. (void)curve_id;
  27263. (void)rng;
  27264. return ret;
  27265. }
  27266. #undef ECC_TEST_VERIFY_COUNT
  27267. #define ECC_TEST_VERIFY_COUNT 2
  27268. static wc_test_ret_t ecc_test_curve(WC_RNG* rng, int keySize, int curve_id)
  27269. {
  27270. wc_test_ret_t ret;
  27271. WOLFSSL_MSG_EX("ecc_test_curve keySize = %d", keySize);
  27272. #if FIPS_VERSION3_GE(6,0,0)
  27273. #ifdef DEBUG_WOLFSSL
  27274. printf("keySize is %d\n", keySize);
  27275. #endif
  27276. if (keySize < WC_ECC_FIPS_GEN_MIN) {
  27277. goto skip_A;
  27278. }
  27279. #endif
  27280. ret = ecc_test_curve_size(rng, keySize, ECC_TEST_VERIFY_COUNT, curve_id,
  27281. NULL);
  27282. if (ret < 0) {
  27283. if (ret == ECC_CURVE_OID_E) {
  27284. /* ignore error for curves not found */
  27285. /* some curve sizes are only available with:
  27286. HAVE_ECC_SECPR2, HAVE_ECC_SECPR3, HAVE_ECC_BRAINPOOL
  27287. and HAVE_ECC_KOBLITZ */
  27288. }
  27289. else {
  27290. printf("ecc_test_curve_size %d failed!\n", keySize);
  27291. return ret;
  27292. }
  27293. }
  27294. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  27295. #if FIPS_VERSION3_GE(6,0,0)
  27296. skip_A:
  27297. #endif
  27298. #ifdef HAVE_ECC_VECTOR_TEST
  27299. ret = ecc_test_vector(keySize);
  27300. if (ret < 0) {
  27301. printf("ecc_test_vector %d failed!\n", keySize);
  27302. return ret;
  27303. }
  27304. #endif
  27305. #if FIPS_VERSION3_GE(6,0,0)
  27306. if (keySize < WC_ECC_FIPS_GEN_MIN) {
  27307. goto skip_B;
  27308. }
  27309. #endif
  27310. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  27311. !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  27312. ret = ecc_test_key_decode(rng, keySize);
  27313. if (ret < 0) {
  27314. if (ret == ECC_CURVE_OID_E) {
  27315. /* ignore error for curves not found */
  27316. }
  27317. else {
  27318. printf("ecc_test_key_decode %d failed!\n", keySize);
  27319. return ret;
  27320. }
  27321. }
  27322. #endif
  27323. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  27324. ret = ecc_test_key_gen(rng, keySize);
  27325. if (ret < 0) {
  27326. if (ret == ECC_CURVE_OID_E) {
  27327. /* ignore error for curves not found */
  27328. }
  27329. else {
  27330. printf("ecc_test_key_gen %d failed!\n", keySize);
  27331. return ret;
  27332. }
  27333. }
  27334. #endif
  27335. #if FIPS_VERSION3_GE(6,0,0)
  27336. skip_B:
  27337. #endif
  27338. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  27339. return 0;
  27340. }
  27341. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  27342. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  27343. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  27344. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  27345. static wc_test_ret_t ecc_point_test(void)
  27346. {
  27347. wc_test_ret_t ret;
  27348. ecc_point* point;
  27349. ecc_point* point2;
  27350. #ifdef HAVE_COMP_KEY
  27351. ecc_point* point3;
  27352. ecc_point* point4;
  27353. #endif
  27354. word32 outLen;
  27355. byte out[65];
  27356. byte der[] = { 0x04, /* = Uncompressed */
  27357. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27358. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27359. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27360. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27361. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27362. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27363. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27364. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  27365. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  27366. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  27367. byte derComp0[] = { 0x02, /* = Compressed, y even */
  27368. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27369. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27370. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27371. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  27372. byte derComp1[] = { 0x03, /* = Compressed, y odd */
  27373. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27374. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27375. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27376. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  27377. #endif
  27378. byte altDer[] = { 0x04, /* = Uncompressed */
  27379. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27380. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27381. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27382. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27383. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27384. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27385. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27386. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  27387. int curve_idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
  27388. /* if curve P256 is not enabled then test should not fail */
  27389. if (curve_idx == ECC_CURVE_INVALID)
  27390. return 0;
  27391. outLen = sizeof(out);
  27392. point = wc_ecc_new_point();
  27393. if (point == NULL)
  27394. return WC_TEST_RET_ENC_ERRNO;
  27395. point2 = wc_ecc_new_point();
  27396. if (point2 == NULL) {
  27397. wc_ecc_del_point(point);
  27398. return WC_TEST_RET_ENC_NC;
  27399. }
  27400. #ifdef HAVE_COMP_KEY
  27401. point3 = wc_ecc_new_point();
  27402. if (point3 == NULL) {
  27403. wc_ecc_del_point(point2);
  27404. wc_ecc_del_point(point);
  27405. return WC_TEST_RET_ENC_NC;
  27406. }
  27407. point4 = wc_ecc_new_point();
  27408. if (point4 == NULL) {
  27409. wc_ecc_del_point(point3);
  27410. wc_ecc_del_point(point2);
  27411. wc_ecc_del_point(point);
  27412. return WC_TEST_RET_ENC_NC;
  27413. }
  27414. #endif
  27415. /* Parameter Validation testing. */
  27416. wc_ecc_del_point(NULL);
  27417. ret = wc_ecc_import_point_der(NULL, sizeof(der), curve_idx, point);
  27418. if (ret != ECC_BAD_ARG_E) {
  27419. ret = WC_TEST_RET_ENC_EC(ret);
  27420. goto done;
  27421. }
  27422. ret = wc_ecc_import_point_der(der, sizeof(der), ECC_CURVE_INVALID, point);
  27423. if (ret != ECC_BAD_ARG_E) {
  27424. ret = WC_TEST_RET_ENC_EC(ret);
  27425. goto done;
  27426. }
  27427. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, NULL);
  27428. if (ret != ECC_BAD_ARG_E) {
  27429. ret = WC_TEST_RET_ENC_EC(ret);
  27430. goto done;
  27431. }
  27432. ret = wc_ecc_export_point_der(-1, point, out, &outLen);
  27433. if (ret != ECC_BAD_ARG_E) {
  27434. ret = WC_TEST_RET_ENC_EC(ret);
  27435. goto done;
  27436. }
  27437. ret = wc_ecc_export_point_der(curve_idx, NULL, out, &outLen);
  27438. if (ret != ECC_BAD_ARG_E) {
  27439. ret = WC_TEST_RET_ENC_EC(ret);
  27440. goto done;
  27441. }
  27442. ret = wc_ecc_export_point_der(curve_idx, point, NULL, &outLen);
  27443. if (ret != LENGTH_ONLY_E || outLen != sizeof(out)) {
  27444. ret = WC_TEST_RET_ENC_EC(ret);
  27445. goto done;
  27446. }
  27447. ret = wc_ecc_export_point_der(curve_idx, point, out, NULL);
  27448. if (ret != ECC_BAD_ARG_E) {
  27449. ret = WC_TEST_RET_ENC_EC(ret);
  27450. goto done;
  27451. }
  27452. outLen = 0;
  27453. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  27454. if (ret != BUFFER_E) {
  27455. ret = WC_TEST_RET_ENC_EC(ret);
  27456. goto done;
  27457. }
  27458. ret = wc_ecc_copy_point(NULL, NULL);
  27459. if (ret != ECC_BAD_ARG_E) {
  27460. ret = WC_TEST_RET_ENC_EC(ret);
  27461. goto done;
  27462. }
  27463. ret = wc_ecc_copy_point(NULL, point2);
  27464. if (ret != ECC_BAD_ARG_E) {
  27465. ret = WC_TEST_RET_ENC_EC(ret);
  27466. goto done;
  27467. }
  27468. ret = wc_ecc_copy_point(point, NULL);
  27469. if (ret != ECC_BAD_ARG_E) {
  27470. ret = WC_TEST_RET_ENC_EC(ret);
  27471. goto done;
  27472. }
  27473. ret = wc_ecc_cmp_point(NULL, NULL);
  27474. if (ret != BAD_FUNC_ARG) {
  27475. ret = WC_TEST_RET_ENC_EC(ret);
  27476. goto done;
  27477. }
  27478. ret = wc_ecc_cmp_point(NULL, point2);
  27479. if (ret != BAD_FUNC_ARG) {
  27480. ret = WC_TEST_RET_ENC_EC(ret);
  27481. goto done;
  27482. }
  27483. ret = wc_ecc_cmp_point(point, NULL);
  27484. if (ret != BAD_FUNC_ARG) {
  27485. ret = WC_TEST_RET_ENC_EC(ret);
  27486. goto done;
  27487. }
  27488. /* Use API. */
  27489. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, point);
  27490. if (ret != 0) {
  27491. ret = WC_TEST_RET_ENC_EC(ret);
  27492. goto done;
  27493. }
  27494. outLen = sizeof(out);
  27495. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  27496. if (ret != 0) {
  27497. ret = WC_TEST_RET_ENC_EC(ret);
  27498. goto done;
  27499. }
  27500. if (outLen != sizeof(der)) {
  27501. ret = WC_TEST_RET_ENC_NC;
  27502. goto done;
  27503. }
  27504. if (XMEMCMP(out, der, outLen) != 0) {
  27505. ret = WC_TEST_RET_ENC_NC;
  27506. goto done;
  27507. }
  27508. ret = wc_ecc_copy_point(point2, point);
  27509. if (ret != MP_OKAY) {
  27510. ret = WC_TEST_RET_ENC_EC(ret);
  27511. goto done;
  27512. }
  27513. ret = wc_ecc_cmp_point(point2, point);
  27514. if (ret != MP_EQ) {
  27515. ret = WC_TEST_RET_ENC_EC(ret);
  27516. goto done;
  27517. }
  27518. ret = wc_ecc_import_point_der(altDer, sizeof(altDer), curve_idx, point2);
  27519. if (ret != 0) {
  27520. ret = WC_TEST_RET_ENC_EC(ret);
  27521. goto done;
  27522. }
  27523. ret = wc_ecc_cmp_point(point2, point);
  27524. if (ret != MP_GT) {
  27525. ret = WC_TEST_RET_ENC_EC(ret);
  27526. goto done;
  27527. }
  27528. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  27529. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  27530. ret = wc_ecc_import_point_der(derComp0, sizeof(derComp0)*2-1, curve_idx, point3);
  27531. if (ret != 0) {
  27532. ret = WC_TEST_RET_ENC_EC(ret);
  27533. goto done;
  27534. }
  27535. ret = wc_ecc_import_point_der_ex(derComp0, sizeof(derComp0), curve_idx, point4, 0);
  27536. if (ret != 0) {
  27537. ret = WC_TEST_RET_ENC_EC(ret);
  27538. goto done;
  27539. }
  27540. ret = wc_ecc_cmp_point(point3, point4);
  27541. if (ret != MP_EQ) {
  27542. ret = WC_TEST_RET_ENC_EC(ret);
  27543. goto done;
  27544. }
  27545. ret = wc_ecc_import_point_der(derComp1, sizeof(derComp1)*2-1, curve_idx, point3);
  27546. if (ret != 0) {
  27547. ret = WC_TEST_RET_ENC_EC(ret);
  27548. goto done;
  27549. }
  27550. ret = wc_ecc_import_point_der_ex(derComp1, sizeof(derComp1), curve_idx, point4, 0);
  27551. if (ret != 0) {
  27552. ret = WC_TEST_RET_ENC_EC(ret);
  27553. goto done;
  27554. }
  27555. ret = wc_ecc_cmp_point(point3, point4);
  27556. if (ret != MP_EQ) {
  27557. ret = WC_TEST_RET_ENC_EC(ret);
  27558. goto done;
  27559. }
  27560. #endif
  27561. done:
  27562. #ifdef HAVE_COMP_KEY
  27563. wc_ecc_del_point(point4);
  27564. wc_ecc_del_point(point3);
  27565. #endif
  27566. wc_ecc_del_point(point2);
  27567. wc_ecc_del_point(point);
  27568. return ret;
  27569. }
  27570. #endif /* !WOLFSSL_ATECC508A && HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  27571. #if !defined(NO_SIG_WRAPPER) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  27572. static wc_test_ret_t ecc_sig_test(WC_RNG* rng, ecc_key* key)
  27573. {
  27574. wc_test_ret_t ret;
  27575. word32 sigSz;
  27576. int size;
  27577. byte out[ECC_MAX_SIG_SIZE];
  27578. byte in[] = TEST_STRING;
  27579. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  27580. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  27581. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  27582. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  27583. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  27584. };
  27585. word32 inLen = (word32)XSTRLEN((char*)in);
  27586. size = wc_ecc_sig_size(key);
  27587. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, sizeof(*key));
  27588. if (ret != size)
  27589. return WC_TEST_RET_ENC_NC;
  27590. sigSz = (word32)ret;
  27591. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  27592. inLen, out, &sigSz, key, sizeof(*key), rng);
  27593. if (ret != 0)
  27594. return WC_TEST_RET_ENC_EC(ret);
  27595. TEST_SLEEP();
  27596. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  27597. inLen, out, sigSz, key, sizeof(*key));
  27598. if (ret != 0)
  27599. return WC_TEST_RET_ENC_EC(ret);
  27600. TEST_SLEEP();
  27601. sigSz = (word32)sizeof(out);
  27602. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  27603. hash, (int)sizeof(hash), out, &sigSz, key, sizeof(*key), rng);
  27604. if (ret != 0)
  27605. return WC_TEST_RET_ENC_EC(ret);
  27606. TEST_SLEEP();
  27607. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  27608. hash, (int)sizeof(hash), out, sigSz, key, sizeof(*key));
  27609. if (ret != 0)
  27610. return WC_TEST_RET_ENC_EC(ret);
  27611. TEST_SLEEP();
  27612. return 0;
  27613. }
  27614. #endif
  27615. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  27616. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  27617. static wc_test_ret_t ecc_exp_imp_test(ecc_key* key)
  27618. {
  27619. wc_test_ret_t ret;
  27620. int curve_id;
  27621. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27622. ecc_key *keyImp = (ecc_key *)XMALLOC(sizeof *keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27623. #else
  27624. ecc_key keyImp[1];
  27625. #endif
  27626. byte priv[32];
  27627. word32 privLen;
  27628. byte pub[65*2];
  27629. word32 pubLen, pubLenX, pubLenY;
  27630. const char qx[] = "7a4e287890a1a47ad3457e52f2f76a83"
  27631. "ce46cbc947616d0cbaa82323818a793d";
  27632. const char qy[] = "eec4084f5b29ebf29c44cce3b3059610"
  27633. "922f8b30ea6e8811742ac7238fe87308";
  27634. const char d[] = "8c14b793cb19137e323a6d2e2a870bca"
  27635. "2e7a493ec1153b3a95feb8a4873f8d08";
  27636. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27637. if (keyImp == NULL)
  27638. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  27639. #endif
  27640. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  27641. privLen = sizeof(priv);
  27642. ret = wc_ecc_export_private_only(key, priv, &privLen);
  27643. if (ret != 0) {
  27644. ret = WC_TEST_RET_ENC_EC(ret);
  27645. goto done;
  27646. }
  27647. pubLen = sizeof(pub);
  27648. ret = wc_ecc_export_point_der(key->idx, &key->pubkey, pub, &pubLen);
  27649. if (ret != 0) {
  27650. ret = WC_TEST_RET_ENC_EC(ret);
  27651. goto done;
  27652. }
  27653. ret = wc_ecc_import_private_key(priv, privLen, pub, pubLen, keyImp);
  27654. if (ret != 0) {
  27655. ret = WC_TEST_RET_ENC_EC(ret);
  27656. goto done;
  27657. }
  27658. wc_ecc_free(keyImp);
  27659. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  27660. ret = wc_ecc_import_raw_ex(keyImp, qx, qy, d, ECC_SECP256R1);
  27661. if (ret != 0) {
  27662. ret = WC_TEST_RET_ENC_EC(ret);
  27663. goto done;
  27664. }
  27665. wc_ecc_free(keyImp);
  27666. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  27667. curve_id = wc_ecc_get_curve_id(key->idx);
  27668. if (curve_id < 0) {
  27669. ret = WC_TEST_RET_ENC_EC(curve_id);
  27670. goto done;
  27671. }
  27672. /* test import private only */
  27673. ret = wc_ecc_import_private_key_ex(priv, privLen, NULL, 0, keyImp,
  27674. curve_id);
  27675. if (ret != 0) {
  27676. ret = WC_TEST_RET_ENC_EC(ret);
  27677. goto done;
  27678. }
  27679. wc_ecc_free(keyImp);
  27680. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  27681. /* test export public raw */
  27682. pubLenX = pubLenY = 32;
  27683. ret = wc_ecc_export_public_raw(key, pub, &pubLenX, &pub[32], &pubLenY);
  27684. if (ret != 0) {
  27685. ret = WC_TEST_RET_ENC_EC(ret);
  27686. goto done;
  27687. }
  27688. #ifndef HAVE_SELFTEST
  27689. /* test import of public */
  27690. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], NULL, ECC_SECP256R1);
  27691. if (ret != 0) {
  27692. ret = WC_TEST_RET_ENC_EC(ret);
  27693. goto done;
  27694. }
  27695. #endif
  27696. wc_ecc_free(keyImp);
  27697. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  27698. /* test export private and public raw */
  27699. pubLenX = pubLenY = privLen = 32;
  27700. ret = wc_ecc_export_private_raw(key, pub, &pubLenX, &pub[32], &pubLenY,
  27701. priv, &privLen);
  27702. if (ret != 0) {
  27703. ret = WC_TEST_RET_ENC_EC(ret);
  27704. goto done;
  27705. }
  27706. #ifndef HAVE_SELFTEST
  27707. /* test import of private and public */
  27708. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], priv, ECC_SECP256R1);
  27709. if (ret != 0) {
  27710. ret = WC_TEST_RET_ENC_EC(ret);
  27711. goto done;
  27712. }
  27713. #endif
  27714. done:
  27715. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27716. if (keyImp != NULL) {
  27717. wc_ecc_free(keyImp);
  27718. XFREE(keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27719. }
  27720. #else
  27721. wc_ecc_free(keyImp);
  27722. #endif
  27723. return ret;
  27724. }
  27725. #endif /* HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  27726. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  27727. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  27728. static wc_test_ret_t ecc_mulmod_test(ecc_key* key1)
  27729. {
  27730. wc_test_ret_t ret;
  27731. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27732. ecc_key *key2 = (ecc_key *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27733. ecc_key *key3 = (ecc_key *)XMALLOC(sizeof *key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27734. #else
  27735. ecc_key key2[1];
  27736. ecc_key key3[1];
  27737. #endif
  27738. #ifdef WOLFSSL_PUBLIC_MP
  27739. mp_int* priv;
  27740. #endif
  27741. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27742. if ((key2 == NULL) || (key3 == NULL))
  27743. ERROR_OUT(MEMORY_E, done);
  27744. #endif
  27745. wc_ecc_init_ex(key2, HEAP_HINT, devId);
  27746. wc_ecc_init_ex(key3, HEAP_HINT, devId);
  27747. /* TODO: Use test data, test with WOLFSSL_VALIDATE_ECC_IMPORT. */
  27748. /* Need base point (Gx,Gy) and parameter A - load them as the public and
  27749. * private key in key2.
  27750. */
  27751. ret = wc_ecc_import_raw_ex(key2, key1->dp->Gx, key1->dp->Gy, key1->dp->Af,
  27752. ECC_SECP256R1);
  27753. if (ret != 0)
  27754. goto done;
  27755. /* Need a point (Gx,Gy) and prime - load them as the public and private key
  27756. * in key3.
  27757. */
  27758. ret = wc_ecc_import_raw_ex(key3, key1->dp->Gx, key1->dp->Gy,
  27759. key1->dp->prime, ECC_SECP256R1);
  27760. if (ret != 0)
  27761. goto done;
  27762. ret = wc_ecc_mulmod(wc_ecc_key_get_priv(key1), &key2->pubkey, &key3->pubkey,
  27763. wc_ecc_key_get_priv(key2), wc_ecc_key_get_priv(key3),
  27764. 1);
  27765. if (ret != 0) {
  27766. ret = WC_TEST_RET_ENC_EC(ret);
  27767. goto done;
  27768. }
  27769. #ifdef WOLFSSL_PUBLIC_MP
  27770. priv = wc_ecc_key_get_priv(key1);
  27771. mp_zero(priv);
  27772. ret = wc_ecc_mulmod(wc_ecc_key_get_priv(key1), &key2->pubkey, &key3->pubkey,
  27773. wc_ecc_key_get_priv(key2), wc_ecc_key_get_priv(key3),
  27774. 1);
  27775. if (ret != 0) {
  27776. ret = WC_TEST_RET_ENC_EC(ret);
  27777. goto done;
  27778. }
  27779. if (!wc_ecc_point_is_at_infinity(&key3->pubkey)) {
  27780. ret = WC_TEST_RET_ENC_EC(ret);
  27781. goto done;
  27782. }
  27783. if (mp_cmp_d(key3->pubkey.z, 1) != MP_EQ) {
  27784. ret = WC_TEST_RET_ENC_EC(ret);
  27785. goto done;
  27786. }
  27787. #endif
  27788. done:
  27789. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27790. if (key2 != NULL) {
  27791. wc_ecc_free(key2);
  27792. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27793. }
  27794. if (key3 != NULL) {
  27795. wc_ecc_free(key3);
  27796. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27797. }
  27798. #else
  27799. wc_ecc_free(key3);
  27800. wc_ecc_free(key2);
  27801. #endif
  27802. return ret;
  27803. }
  27804. #endif
  27805. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  27806. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(WOLFSSL_ATECC508A) && \
  27807. !defined(WOLFSSL_ATECC608A) && !defined(PLUTON_CRYPTO_ECC) && \
  27808. !defined(WOLFSSL_CRYPTOCELL)
  27809. static wc_test_ret_t ecc_ssh_test(ecc_key* key, WC_RNG* rng)
  27810. {
  27811. wc_test_ret_t ret;
  27812. byte out[128];
  27813. word32 outLen = sizeof(out);
  27814. /* Parameter Validation testing. */
  27815. ret = wc_ecc_shared_secret_ssh(NULL, &key->pubkey, out, &outLen);
  27816. if (ret != BAD_FUNC_ARG)
  27817. return WC_TEST_RET_ENC_EC(ret);
  27818. ret = wc_ecc_shared_secret_ssh(key, NULL, out, &outLen);
  27819. if (ret != BAD_FUNC_ARG)
  27820. return WC_TEST_RET_ENC_EC(ret);
  27821. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, NULL, &outLen);
  27822. if (ret != BAD_FUNC_ARG)
  27823. return WC_TEST_RET_ENC_EC(ret);
  27824. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, NULL);
  27825. if (ret != BAD_FUNC_ARG)
  27826. return WC_TEST_RET_ENC_EC(ret);
  27827. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  27828. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  27829. !defined(HAVE_SELFTEST)
  27830. ret = wc_ecc_set_rng(key, rng);
  27831. if (ret != 0)
  27832. return WC_TEST_RET_ENC_EC(ret);
  27833. #else
  27834. (void)rng;
  27835. #endif
  27836. /* Use API. */
  27837. ret = 0;
  27838. do {
  27839. #if defined(WOLFSSL_ASYNC_CRYPT)
  27840. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  27841. #endif
  27842. if (ret == 0)
  27843. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, &outLen);
  27844. } while (ret == WC_PENDING_E);
  27845. if (ret != 0)
  27846. return WC_TEST_RET_ENC_EC(ret);
  27847. TEST_SLEEP();
  27848. return 0;
  27849. }
  27850. #endif /* HAVE_ECC_DHE && !WC_NO_RNG */
  27851. static wc_test_ret_t ecc_def_curve_test(WC_RNG *rng)
  27852. {
  27853. wc_test_ret_t ret;
  27854. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27855. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27856. #else
  27857. ecc_key key[1];
  27858. #endif
  27859. #if !defined(NO_ECC_SECP) && !defined(NO_ASN) && \
  27860. ((defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  27861. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)))
  27862. word32 idx = 0;
  27863. #endif
  27864. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27865. if (key == NULL)
  27866. ERROR_OUT(MEMORY_E, done);
  27867. #endif
  27868. wc_ecc_init_ex(key, HEAP_HINT, devId);
  27869. /* Use API */
  27870. ret = wc_ecc_set_flags(NULL, 0);
  27871. if (ret != BAD_FUNC_ARG) {
  27872. ret = WC_TEST_RET_ENC_EC(ret);
  27873. goto done;
  27874. }
  27875. ret = wc_ecc_set_flags(key, 0);
  27876. if (ret != 0) {
  27877. ret = WC_TEST_RET_ENC_EC(ret);
  27878. goto done;
  27879. }
  27880. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  27881. #ifndef WC_NO_RNG
  27882. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  27883. #if defined(WOLFSSL_ASYNC_CRYPT)
  27884. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  27885. #endif
  27886. if (ret != 0) {
  27887. goto done;
  27888. }
  27889. #ifndef NO_SIG_WRAPPER
  27890. ret = ecc_sig_test(rng, key);
  27891. if (ret < 0)
  27892. goto done;
  27893. #endif
  27894. TEST_SLEEP();
  27895. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_CRYPTOCELL) && \
  27896. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  27897. ret = ecc_ssh_test(key, rng);
  27898. if (ret < 0)
  27899. goto done;
  27900. #endif
  27901. wc_ecc_free(key);
  27902. #else
  27903. (void)rng;
  27904. #endif /* !WC_NO_RNG */
  27905. #if !defined(NO_ECC_SECP) && !defined(NO_ASN) && \
  27906. ((defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  27907. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)))
  27908. /* Use test ECC key - ensure real private "d" exists */
  27909. #if defined(USE_CERT_BUFFERS_256)
  27910. {
  27911. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  27912. sizeof_ecc_key_der_256);
  27913. }
  27914. #elif !defined(NO_FILESYSTEM)
  27915. {
  27916. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  27917. byte der[128];
  27918. word32 derSz;
  27919. if (!file) {
  27920. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  27921. }
  27922. derSz = (word32)XFREAD(der, 1, sizeof(der), file);
  27923. XFCLOSE(file);
  27924. if (derSz == 0)
  27925. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  27926. ret = wc_EccPrivateKeyDecode(der, &idx, key, derSz);
  27927. }
  27928. #else
  27929. {
  27930. (void)idx;
  27931. WOLFSSL_MSG("No file system and USE_CERT_BUFFERS_256 not defined.(3)");
  27932. ERROR_OUT(ASN_PARSE_E, done);
  27933. }
  27934. #endif
  27935. if (ret != 0) {
  27936. goto done;
  27937. }
  27938. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)
  27939. ret = ecc_exp_imp_test(key);
  27940. if (ret < 0)
  27941. goto done;
  27942. #endif
  27943. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  27944. !defined(WOLFSSL_CRYPTOCELL)
  27945. ret = ecc_mulmod_test(key);
  27946. if (ret < 0)
  27947. goto done;
  27948. #endif
  27949. #endif
  27950. #else
  27951. (void)rng;
  27952. (void)idx;
  27953. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  27954. done:
  27955. wc_ecc_free(key);
  27956. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27957. if (key != NULL) {
  27958. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27959. }
  27960. #endif
  27961. return ret;
  27962. }
  27963. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  27964. #if defined(WOLFSSL_CERT_EXT) && \
  27965. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  27966. static wc_test_ret_t ecc_decode_test(void)
  27967. {
  27968. wc_test_ret_t ret;
  27969. word32 inSz;
  27970. word32 inOutIdx;
  27971. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  27972. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27973. #else
  27974. ecc_key key[1];
  27975. #endif
  27976. /* SECP256R1 OID: 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 */
  27977. /* This is ecc_clikeypub_der_256. */
  27978. WOLFSSL_SMALL_STACK_STATIC const byte good[] = {
  27979. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
  27980. 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  27981. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xbf, 0xf4,
  27982. 0x0f, 0x44, 0x50, 0x9a, 0x3d, 0xce, 0x9b, 0xb7, 0xf0, 0xc5,
  27983. 0x4d, 0xf5, 0x70, 0x7b, 0xd4, 0xec, 0x24, 0x8e, 0x19, 0x80,
  27984. 0xec, 0x5a, 0x4c, 0xa2, 0x24, 0x03, 0x62, 0x2c, 0x9b, 0xda,
  27985. 0xef, 0xa2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xc6, 0x56,
  27986. 0x95, 0x06, 0xcc, 0x01, 0xa9, 0xbd, 0xf6, 0x75, 0x1a, 0x42,
  27987. 0xf7, 0xbd, 0xa9, 0xb2, 0x36, 0x22, 0x5f, 0xc7, 0x5d, 0x7f,
  27988. 0xb4 };
  27989. WOLFSSL_SMALL_STACK_STATIC const byte badNoObjId[] = { 0x30, 0x08, 0x30, 0x06, 0x03, 0x04,
  27990. 0x00, 0x04, 0x01, 0x01 };
  27991. WOLFSSL_SMALL_STACK_STATIC const byte badOneObjId[] = { 0x30, 0x0a, 0x30, 0x08, 0x06, 0x00,
  27992. 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  27993. WOLFSSL_SMALL_STACK_STATIC const byte badObjId1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x09,
  27994. 0x06, 0x00, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  27995. WOLFSSL_SMALL_STACK_STATIC const byte badObj2d1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00,
  27996. 0x06, 0x07, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  27997. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitStr[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  27998. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  27999. 0x04, 0x04, 0x00, 0x04, 0x01, 0x01 };
  28000. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrLen[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  28001. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  28002. 0x03, 0x05, 0x00, 0x04, 0x01, 0x01 };
  28003. WOLFSSL_SMALL_STACK_STATIC const byte badNoBitStrZero[] = { 0x30, 0x13, 0x30, 0x0a, 0x06, 0x00,
  28004. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  28005. 0x03, 0x03, 0x04, 0x01, 0x01 };
  28006. WOLFSSL_SMALL_STACK_STATIC const byte badPoint[] = { 0x30, 0x12, 0x30, 0x09, 0x06, 0x00,
  28007. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  28008. 0x03, 0x03, 0x00, 0x04, 0x01 };
  28009. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28010. if (key == NULL)
  28011. ERROR_OUT(MEMORY_E, done);
  28012. #endif
  28013. XMEMSET(key, 0, sizeof *key);
  28014. wc_ecc_init_ex(key, HEAP_HINT, devId);
  28015. inSz = sizeof(good);
  28016. ret = wc_EccPublicKeyDecode(NULL, &inOutIdx, key, inSz);
  28017. if (ret != BAD_FUNC_ARG) {
  28018. ret = WC_TEST_RET_ENC_EC(ret);
  28019. goto done;
  28020. }
  28021. ret = wc_EccPublicKeyDecode(good, NULL, key, inSz);
  28022. if (ret != BAD_FUNC_ARG) {
  28023. ret = WC_TEST_RET_ENC_EC(ret);
  28024. goto done;
  28025. }
  28026. ret = wc_EccPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  28027. if (ret != BAD_FUNC_ARG) {
  28028. ret = WC_TEST_RET_ENC_EC(ret);
  28029. goto done;
  28030. }
  28031. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, 0);
  28032. if (ret != BAD_FUNC_ARG) {
  28033. ret = WC_TEST_RET_ENC_EC(ret);
  28034. goto done;
  28035. }
  28036. /* Change offset to produce bad input data. */
  28037. inOutIdx = 2;
  28038. inSz = sizeof(good) - inOutIdx;
  28039. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  28040. if (ret != ASN_PARSE_E) {
  28041. ret = WC_TEST_RET_ENC_EC(ret);
  28042. goto done;
  28043. }
  28044. inOutIdx = 4;
  28045. inSz = sizeof(good) - inOutIdx;
  28046. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  28047. if (ret != ASN_PARSE_E) {
  28048. ret = WC_TEST_RET_ENC_EC(ret);
  28049. goto done;
  28050. }
  28051. /* Bad data. */
  28052. inSz = sizeof(badNoObjId);
  28053. inOutIdx = 0;
  28054. ret = wc_EccPublicKeyDecode(badNoObjId, &inOutIdx, key, inSz);
  28055. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  28056. ret = WC_TEST_RET_ENC_EC(ret);
  28057. goto done;
  28058. }
  28059. inSz = sizeof(badOneObjId);
  28060. inOutIdx = 0;
  28061. ret = wc_EccPublicKeyDecode(badOneObjId, &inOutIdx, key, inSz);
  28062. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  28063. ret = WC_TEST_RET_ENC_EC(ret);
  28064. goto done;
  28065. }
  28066. inSz = sizeof(badObjId1Len);
  28067. inOutIdx = 0;
  28068. ret = wc_EccPublicKeyDecode(badObjId1Len, &inOutIdx, key, inSz);
  28069. if (ret != ASN_PARSE_E) {
  28070. ret = WC_TEST_RET_ENC_EC(ret);
  28071. goto done;
  28072. }
  28073. inSz = sizeof(badObj2d1Len);
  28074. inOutIdx = 0;
  28075. ret = wc_EccPublicKeyDecode(badObj2d1Len, &inOutIdx, key, inSz);
  28076. if (ret != ASN_PARSE_E) {
  28077. ret = WC_TEST_RET_ENC_EC(ret);
  28078. goto done;
  28079. }
  28080. inSz = sizeof(badNotBitStr);
  28081. inOutIdx = 0;
  28082. ret = wc_EccPublicKeyDecode(badNotBitStr, &inOutIdx, key, inSz);
  28083. if (ret != ASN_BITSTR_E && ret != ASN_PARSE_E) {
  28084. ret = WC_TEST_RET_ENC_EC(ret);
  28085. goto done;
  28086. }
  28087. inSz = sizeof(badBitStrLen);
  28088. inOutIdx = 0;
  28089. ret = wc_EccPublicKeyDecode(badBitStrLen, &inOutIdx, key, inSz);
  28090. if (ret != ASN_PARSE_E) {
  28091. ret = WC_TEST_RET_ENC_EC(ret);
  28092. goto done;
  28093. }
  28094. inSz = sizeof(badNoBitStrZero);
  28095. inOutIdx = 0;
  28096. ret = wc_EccPublicKeyDecode(badNoBitStrZero, &inOutIdx, key, inSz);
  28097. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  28098. ret = WC_TEST_RET_ENC_EC(ret);
  28099. goto done;
  28100. }
  28101. inSz = sizeof(badPoint);
  28102. inOutIdx = 0;
  28103. ret = wc_EccPublicKeyDecode(badPoint, &inOutIdx, key, inSz);
  28104. if (ret != ASN_ECC_KEY_E && ret != ASN_PARSE_E) {
  28105. ret = WC_TEST_RET_ENC_EC(ret);
  28106. goto done;
  28107. }
  28108. inSz = sizeof(good);
  28109. inOutIdx = 0;
  28110. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  28111. if (ret != 0) {
  28112. ret = WC_TEST_RET_ENC_EC(ret);
  28113. goto done;
  28114. }
  28115. done:
  28116. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28117. if (key != NULL) {
  28118. wc_ecc_free(key);
  28119. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28120. }
  28121. #else
  28122. wc_ecc_free(key);
  28123. #endif
  28124. return ret;
  28125. }
  28126. #endif /* WOLFSSL_CERT_EXT */
  28127. #ifdef WOLFSSL_CUSTOM_CURVES
  28128. static const byte eccKeyExplicitCurve[] = {
  28129. 0x30, 0x81, 0xf5, 0x30, 0x81, 0xae, 0x06, 0x07,
  28130. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30,
  28131. 0x81, 0xa2, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06,
  28132. 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01,
  28133. 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  28134. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  28135. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  28136. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
  28137. 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00,
  28138. 0x04, 0x01, 0x07, 0x04, 0x41, 0x04, 0x79, 0xbe,
  28139. 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
  28140. 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b,
  28141. 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2,
  28142. 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a,
  28143. 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4,
  28144. 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17,
  28145. 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47,
  28146. 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8, 0x02, 0x21,
  28147. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  28148. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  28149. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  28150. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  28151. 0x41, 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04,
  28152. 0x3c, 0x4c, 0xc9, 0x5e, 0x2e, 0xa2, 0x3d, 0x49,
  28153. 0xcc, 0x5b, 0xff, 0x4f, 0xc9, 0x2e, 0x1d, 0x4a,
  28154. 0xc6, 0x21, 0xf6, 0xf3, 0xe6, 0x0b, 0x4f, 0xa9,
  28155. 0x9d, 0x74, 0x99, 0xdd, 0x97, 0xc7, 0x6e, 0xbe,
  28156. 0x14, 0x2b, 0x39, 0x9d, 0x63, 0xc7, 0x97, 0x0d,
  28157. 0x45, 0x25, 0x40, 0x30, 0x77, 0x05, 0x76, 0x88,
  28158. 0x38, 0x96, 0x29, 0x7d, 0x9c, 0xe1, 0x50, 0xbe,
  28159. 0xac, 0xf0, 0x1d, 0x86, 0xf4, 0x2f, 0x65, 0x0b
  28160. };
  28161. static wc_test_ret_t ecc_test_custom_curves(WC_RNG* rng)
  28162. {
  28163. wc_test_ret_t ret;
  28164. word32 inOutIdx;
  28165. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28166. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28167. #else
  28168. ecc_key key[1];
  28169. #endif
  28170. /* test use of custom curve - using BRAINPOOLP256R1 for test */
  28171. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  28172. #ifndef WOLFSSL_ECC_CURVE_STATIC
  28173. WOLFSSL_SMALL_STACK_STATIC const ecc_oid_t ecc_oid_brainpoolp256r1[] = {
  28174. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07
  28175. };
  28176. #define ecc_oid_brainpoolp256r1_sz \
  28177. (sizeof(ecc_oid_brainpoolp256r1) / sizeof(ecc_oid_t))
  28178. #else
  28179. #define ecc_oid_brainpoolp256r1 { \
  28180. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07 \
  28181. }
  28182. #define ecc_oid_brainpoolp256r1_sz 9
  28183. #endif
  28184. #define ecc_oid_brainpoolp256r1_sum 104
  28185. WOLFSSL_SMALL_STACK_STATIC const ecc_set_type ecc_dp_brainpool256r1 = {
  28186. 32, /* size/bytes */
  28187. ECC_CURVE_CUSTOM, /* ID */
  28188. "BRAINPOOLP256R1", /* curve name */
  28189. "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", /* prime */
  28190. "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", /* A */
  28191. "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", /* B */
  28192. "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", /* order */
  28193. "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", /* Gx */
  28194. "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", /* Gy */
  28195. ecc_oid_brainpoolp256r1, /* oid/oidSz */
  28196. ecc_oid_brainpoolp256r1_sz,
  28197. ecc_oid_brainpoolp256r1_sum, /* oid sum */
  28198. 1, /* cofactor */
  28199. };
  28200. #endif /* HAVE_ECC_BRAINPOOL */
  28201. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28202. if (! key) {
  28203. ret = MEMORY_E;
  28204. goto done;
  28205. }
  28206. #endif
  28207. XMEMSET(key, 0, sizeof *key);
  28208. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  28209. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, ECC_CURVE_DEF,
  28210. &ecc_dp_brainpool256r1);
  28211. if (ret != 0) {
  28212. printf("ECC test for custom curve failed!\n");
  28213. goto done;
  28214. }
  28215. #endif
  28216. #if defined(HAVE_ECC_BRAINPOOL) || defined(HAVE_ECC_KOBLITZ)
  28217. {
  28218. int curve_id;
  28219. #ifdef HAVE_ECC_BRAINPOOL
  28220. curve_id = ECC_BRAINPOOLP256R1;
  28221. #else
  28222. curve_id = ECC_SECP256K1;
  28223. #endif
  28224. /* Test and demonstrate use of non-SECP curve */
  28225. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, curve_id, NULL);
  28226. if (ret < 0) {
  28227. printf("ECC test for curve_id %d failed!\n", curve_id);
  28228. goto done;
  28229. }
  28230. }
  28231. #endif
  28232. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  28233. if (ret != 0) {
  28234. ret = WC_TEST_RET_ENC_EC(ret);
  28235. goto done;
  28236. }
  28237. inOutIdx = 0;
  28238. ret = wc_EccPublicKeyDecode(eccKeyExplicitCurve, &inOutIdx, key,
  28239. sizeof(eccKeyExplicitCurve));
  28240. if (ret != 0)
  28241. ret = WC_TEST_RET_ENC_EC(ret);
  28242. done:
  28243. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28244. if (key) {
  28245. wc_ecc_free(key);
  28246. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28247. }
  28248. #else
  28249. wc_ecc_free(key);
  28250. #endif
  28251. (void)rng;
  28252. return ret;
  28253. }
  28254. #endif /* WOLFSSL_CUSTOM_CURVES */
  28255. #ifdef WOLFSSL_SM2
  28256. #ifdef HAVE_ECC_VERIFY
  28257. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_CUSTOM_CURVES)
  28258. #ifdef HAVE_OID_ENCODING
  28259. #define CODED_SM2P256V1 {1,2,156,10197,1,301}
  28260. #define CODED_SM2P256V1_SZ 6
  28261. #else
  28262. #define CODED_SM2P256V1 {0x06,0x08,0x2A,0x81,0x1C,0xCF,0x55,0x01,0x82,0x2D}
  28263. #define CODED_SM2P256V1_SZ 10
  28264. #endif
  28265. #ifndef WOLFSSL_ECC_CURVE_STATIC
  28266. static const ecc_oid_t ecc_oid_sm2p256v1[] = CODED_SM2P256V1;
  28267. #else
  28268. #define ecc_oid_sm2p256v1 CODED_SM2P256V1
  28269. #endif
  28270. #define ecc_oid_sm2p256v1_sz CODED_SM2P256V1_SZ
  28271. #define ECC_SM2P256V1_TEST 102
  28272. static int test_sm2_verify_caseA2(void)
  28273. {
  28274. ecc_key key;
  28275. int ret, res;
  28276. mp_int r,s;
  28277. /* test key values */
  28278. const char qx[] = "0AE4C7798AA0F119471BEE11825BE46202BB79E2A5844495E97C04FF4DF2548A";
  28279. const char qy[] = "7C0240F88F1CD4E16352A73C17B7F16F07353E53A176D684A9FE0C6BB798E857";
  28280. const char d[] = "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263";
  28281. const ecc_set_type ecc_sm2_A2 = {
  28282. 32, /* size/bytes */
  28283. ECC_SM2P256V1_TEST, /* ID */
  28284. "SM2P256V1_TEST", /* curve name */
  28285. /* from test case A.2 in draft-shen-sm2-ecdsa-02 */
  28286. "8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3", /* prime */
  28287. "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498", /* A */
  28288. "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A", /* B */
  28289. "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7", /* order n */
  28290. "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D", /* Gx */
  28291. "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2", /* Gy */
  28292. ecc_oid_sm2p256v1, /* oid/oidSz */
  28293. ecc_oid_sm2p256v1_sz,
  28294. ECC_SM2P256V1_OID, /* oid sum */
  28295. 1, /* cofactor */
  28296. };
  28297. /* use canned hash value hash = H(ZA||M) */
  28298. const byte hash[] = {
  28299. 0xB5,0x24,0xF5,0x52,0xCD,0x82,0xB8,0xB0,
  28300. 0x28,0x47,0x6E,0x00,0x5C,0x37,0x7F,0xB1,
  28301. 0x9A,0x87,0xE6,0xFC,0x68,0x2D,0x48,0xBB,
  28302. 0x5D,0x42,0xE3,0xD9,0xB9,0xEF,0xFE,0x76
  28303. };
  28304. /* canned r and s */
  28305. const byte rCan[] = {
  28306. 0x40,0xF1,0xEC,0x59,0xF7,0x93,0xD9,0xF4,
  28307. 0x9E,0x09,0xDC,0xEF,0x49,0x13,0x0D,0x41,
  28308. 0x94,0xF7,0x9F,0xB1,0xEE,0xD2,0xCA,0xA5,
  28309. 0x5B,0xAC,0xDB,0x49,0xC4,0xE7,0x55,0xD1
  28310. };
  28311. const byte sCan[] = {
  28312. 0x6F,0xC6,0xDA,0xC3,0x2C,0x5D,0x5C,0xF1,
  28313. 0x0C,0x77,0xDF,0xB2,0x0F,0x7C,0x2E,0xB6,
  28314. 0x67,0xA4,0x57,0x87,0x2F,0xB0,0x9E,0xC5,
  28315. 0x63,0x27,0xA6,0x7E,0xC7,0xDE,0xEB,0xE7
  28316. };
  28317. mp_init(&r);
  28318. mp_init(&s);
  28319. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  28320. if (ret != 0)
  28321. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28322. ret = wc_ecc_set_custom_curve(&key, &ecc_sm2_A2);
  28323. if (ret != 0)
  28324. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28325. ret = wc_ecc_import_raw_ex(&key, qx, qy, d, ECC_SM2P256V1_TEST);
  28326. if (ret != 0)
  28327. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28328. mp_read_unsigned_bin(&r, rCan, sizeof(rCan));
  28329. mp_read_unsigned_bin(&s, sCan, sizeof(sCan));
  28330. ret = wc_ecc_sm2_verify_hash_ex(&r, &s, hash, sizeof(hash), &res, &key);
  28331. if (ret != 0)
  28332. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28333. if (res != 1)
  28334. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28335. done:
  28336. mp_free(&r);
  28337. mp_free(&s);
  28338. wc_ecc_free(&key);
  28339. return ret;
  28340. }
  28341. #endif /* WOLFSSL_PUBLIC_MP && WOLFSSL_CUSTOM_CURVES */
  28342. static int test_sm2_verify_case(void)
  28343. {
  28344. ecc_key key;
  28345. int ret, res;
  28346. /* test key values */
  28347. const char qx[] = "637F1B135036C933DC3F7A8EBB1B7B2FD1DFBD268D4F894B5AD47DBDBECD558F";
  28348. const char qy[] = "E88101D08048E36CCBF61CA38DDF7ABA542B4486E99E49F3A7470A857A096433";
  28349. /* use canned hash value hash = H(ZA||M) */
  28350. const byte hash[] = {
  28351. 0x3B,0xFA,0x5F,0xFB,0xC4,0x27,0x8C,0x9D,
  28352. 0x02,0x3A,0x19,0xCB,0x1E,0xAA,0xD2,0xF1,
  28353. 0x50,0x69,0x5B,0x20
  28354. };
  28355. const byte sig[] = {
  28356. 0x30,0x45,0x02,0x21,0x00,0xD2,0xFC,0xA3,
  28357. 0x88,0xE3,0xDF,0xA3,0x00,0x73,0x9B,0x3C,
  28358. 0x2A,0x0D,0xAD,0x44,0xA2,0xFC,0x62,0xD5,
  28359. 0x6B,0x84,0x54,0xD8,0x40,0x22,0x62,0x3D,
  28360. 0x5C,0xA6,0x61,0x9B,0xE7,0x02,0x20,0x1D,
  28361. 0xB5,0xB5,0xD9,0xD8,0xF1,0x20,0xDD,0x97,
  28362. 0x92,0xBF,0x7E,0x9B,0x3F,0xE6,0x3C,0x4B,
  28363. 0x03,0xD8,0x80,0xBD,0xB7,0x27,0x7E,0x6A,
  28364. 0x84,0x23,0xDE,0x61,0x7C,0x8D,0xDC
  28365. };
  28366. const byte badSig[] = {
  28367. 0x30,0x45,0x02,0x21,0x00,0xD2,0xFC,0xA3,
  28368. 0x88,0xE3,0xDF,0xA3,0x00,0x73,0x9B,0x3C,
  28369. 0x2A,0x0D,0xAD,0x44,0xA2,0xFC,0x62,0xD5,
  28370. 0x6B,0x84,0x54,0xD8,0x40,0x22,0x62,0x3D,
  28371. 0x5C,0xA6,0x61,0x9B,0xE7,0x02,0x20,0x1D,
  28372. 0xB5,0xB5,0xE9,0xD8,0xF1,0x20,0xDD,0x97,
  28373. 0x92,0xBF,0x7E,0x9B,0x3F,0xE6,0x3C,0x4B,
  28374. 0x03,0xD8,0x80,0xBD,0xB7,0x27,0x7E,0x6A,
  28375. 0x84,0x23,0xDE,0x61,0x7C,0x8D,0xDC
  28376. };
  28377. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  28378. if (ret != 0)
  28379. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28380. ret = wc_ecc_import_raw(&key, qx, qy, NULL, "SM2P256V1");
  28381. if (ret != 0)
  28382. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28383. ret = wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash), &res,
  28384. &key);
  28385. if (ret != 0)
  28386. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28387. if (res != 1)
  28388. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28389. /* now test a case that should fail */
  28390. ret = wc_ecc_sm2_verify_hash(badSig, sizeof(badSig), hash, sizeof(hash),
  28391. &res, &key);
  28392. if (ret != 0)
  28393. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28394. if (res == 1)
  28395. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28396. done:
  28397. wc_ecc_free(&key);
  28398. return ret;
  28399. }
  28400. static int ecc_sm2_test_curve(WC_RNG* rng, int testVerifyCount)
  28401. {
  28402. const ecc_set_type* dp = wc_ecc_get_curve_params(
  28403. wc_ecc_get_curve_idx(ECC_SM2P256V1));
  28404. int keySize = 32;
  28405. int curve_id = ECC_SM2P256V1;
  28406. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  28407. WC_DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  28408. WC_DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  28409. #endif
  28410. #ifdef HAVE_ECC_KEY_EXPORT
  28411. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  28412. WC_DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  28413. #endif
  28414. word32 x = 0;
  28415. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  28416. word32 y;
  28417. #endif
  28418. #ifdef HAVE_ECC_SIGN
  28419. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  28420. WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  28421. int i;
  28422. int verify;
  28423. #endif /* HAVE_ECC_SIGN */
  28424. int ret;
  28425. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28426. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28427. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28428. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28429. #else
  28430. ecc_key userA[1];
  28431. ecc_key userB[1];
  28432. ecc_key pubKey[1];
  28433. #endif
  28434. #ifndef WC_NO_RNG
  28435. int curveSize;
  28436. #endif
  28437. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  28438. WC_ALLOC_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  28439. WC_ALLOC_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  28440. #endif
  28441. #ifdef HAVE_ECC_KEY_EXPORT
  28442. WC_ALLOC_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  28443. #endif
  28444. #ifdef HAVE_ECC_SIGN
  28445. WC_ALLOC_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  28446. WC_ALLOC_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  28447. #endif
  28448. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  28449. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  28450. if (sharedA == NULL || sharedB == NULL)
  28451. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28452. #endif
  28453. #ifdef HAVE_ECC_KEY_EXPORT
  28454. if (exportBuf == NULL)
  28455. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28456. #endif
  28457. #ifdef HAVE_ECC_SIGN
  28458. if (sig == NULL || digest == NULL)
  28459. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28460. #endif
  28461. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  28462. (void)testVerifyCount;
  28463. (void)dp;
  28464. (void)x;
  28465. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28466. if ((userA == NULL) ||
  28467. (userB == NULL) ||
  28468. (pubKey == NULL))
  28469. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28470. #endif
  28471. XMEMSET(userA, 0, sizeof *userA);
  28472. XMEMSET(userB, 0, sizeof *userB);
  28473. XMEMSET(pubKey, 0, sizeof *pubKey);
  28474. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  28475. if (ret != 0)
  28476. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28477. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  28478. if (ret != 0)
  28479. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28480. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  28481. if (ret != 0)
  28482. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28483. #ifndef WC_NO_RNG
  28484. ret = wc_ecc_sm2_make_key(rng, userA, WC_ECC_FLAG_NONE);
  28485. if (ret == ECC_CURVE_OID_E)
  28486. goto done; /* catch case, where curve is not supported */
  28487. if (ret != 0)
  28488. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28489. TEST_SLEEP();
  28490. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  28491. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  28492. if (curveSize != userA->dp->size) {
  28493. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28494. }
  28495. }
  28496. ret = wc_ecc_check_key(userA);
  28497. if (ret != 0)
  28498. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28499. TEST_SLEEP();
  28500. ret = wc_ecc_sm2_make_key(rng, userB, WC_ECC_FLAG_NONE);
  28501. if (ret != 0)
  28502. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28503. /* only perform the below tests if the key size matches */
  28504. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  28505. if (ret != 0) {
  28506. ret = ECC_CURVE_OID_E;
  28507. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28508. }
  28509. #ifdef HAVE_ECC_DHE
  28510. #if defined(ECC_TIMING_RESISTANT)
  28511. ret = wc_ecc_set_rng(userA, rng);
  28512. if (ret != 0)
  28513. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28514. ret = wc_ecc_set_rng(userB, rng);
  28515. if (ret != 0)
  28516. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28517. #endif
  28518. x = ECC_SHARED_SIZE;
  28519. ret = wc_ecc_sm2_shared_secret(userA, userB, sharedA, &x);
  28520. if (ret != 0)
  28521. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28522. y = ECC_SHARED_SIZE;
  28523. ret = wc_ecc_sm2_shared_secret(userB, userA, sharedB, &y);
  28524. if (ret != 0)
  28525. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28526. if (y != x)
  28527. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28528. if (XMEMCMP(sharedA, sharedB, x))
  28529. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28530. #endif /* HAVE_ECC_DHE */
  28531. #ifdef HAVE_ECC_KEY_EXPORT
  28532. x = ECC_KEY_EXPORT_BUF_SIZE;
  28533. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  28534. if (ret != 0)
  28535. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28536. #ifdef HAVE_ECC_KEY_IMPORT
  28537. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  28538. if (ret != 0)
  28539. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28540. #ifdef HAVE_ECC_DHE
  28541. y = ECC_SHARED_SIZE;
  28542. ret = wc_ecc_sm2_shared_secret(userB, pubKey, sharedB, &y);
  28543. if (ret != 0)
  28544. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28545. if (XMEMCMP(sharedA, sharedB, y))
  28546. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28547. #endif /* HAVE_ECC_DHE */
  28548. #ifdef HAVE_COMP_KEY
  28549. /* try compressed export / import too */
  28550. x = ECC_KEY_EXPORT_BUF_SIZE;
  28551. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  28552. if (ret != 0)
  28553. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28554. wc_ecc_free(pubKey);
  28555. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  28556. if (ret != 0)
  28557. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28558. #endif
  28559. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  28560. if (ret != 0)
  28561. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28562. #ifdef HAVE_ECC_DHE
  28563. y = ECC_SHARED_SIZE;
  28564. ret = wc_ecc_sm2_shared_secret(userB, pubKey, sharedB, &y);
  28565. if (ret != 0)
  28566. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28567. if (XMEMCMP(sharedA, sharedB, y))
  28568. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28569. #endif /* HAVE_ECC_DHE */
  28570. #endif /* HAVE_ECC_KEY_IMPORT */
  28571. #endif /* HAVE_ECC_KEY_EXPORT */
  28572. #endif /* !WC_NO_RNG */
  28573. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  28574. !defined(WC_NO_RNG))
  28575. #ifdef HAVE_ECC_SIGN
  28576. /* ECC w/out Shamir has issue with all 0 digest */
  28577. /* WC_BIGINT doesn't have 0 len well on hardware */
  28578. /* Cryptocell has issues with all 0 digest */
  28579. #if defined(ECC_SHAMIR)
  28580. /* test DSA sign hash with zeros */
  28581. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  28582. digest[i] = 0;
  28583. }
  28584. x = ECC_SIG_SIZE;
  28585. ret = wc_ecc_sm2_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  28586. if (ret != 0)
  28587. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28588. for (i = 0; i < testVerifyCount; i++) {
  28589. verify = 0;
  28590. ret = wc_ecc_sm2_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify,
  28591. userA);
  28592. if (ret != 0)
  28593. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28594. if (verify != 1)
  28595. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28596. }
  28597. #endif /* ECC_SHAMIR */
  28598. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  28599. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  28600. digest[i] = (byte)i;
  28601. }
  28602. x = ECC_SIG_SIZE;
  28603. ret = wc_ecc_sm2_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  28604. if (ret != 0)
  28605. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28606. for (i = 0; i < testVerifyCount; i++) {
  28607. verify = 0;
  28608. ret = wc_ecc_sm2_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify,
  28609. userA);
  28610. if (ret != 0)
  28611. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28612. if (verify != 1)
  28613. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28614. }
  28615. #endif /* HAVE_ECC_SIGN */
  28616. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT && !WC_NO_RNG) */
  28617. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  28618. x = ECC_KEY_EXPORT_BUF_SIZE;
  28619. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  28620. if (ret != 0)
  28621. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28622. #elif defined(HAVE_ECC_KEY_EXPORT)
  28623. (void)exportBuf;
  28624. #endif /* HAVE_ECC_KEY_EXPORT */
  28625. done:
  28626. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28627. if (userA != NULL) {
  28628. wc_ecc_free(userA);
  28629. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28630. }
  28631. if (userB != NULL) {
  28632. wc_ecc_free(userB);
  28633. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28634. }
  28635. if (pubKey != NULL) {
  28636. wc_ecc_free(pubKey);
  28637. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28638. }
  28639. #else
  28640. wc_ecc_free(pubKey);
  28641. wc_ecc_free(userB);
  28642. wc_ecc_free(userA);
  28643. #endif
  28644. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  28645. WC_FREE_VAR(sharedA, HEAP_HINT);
  28646. WC_FREE_VAR(sharedB, HEAP_HINT);
  28647. #endif
  28648. #ifdef HAVE_ECC_KEY_EXPORT
  28649. WC_FREE_VAR(exportBuf, HEAP_HINT);
  28650. #endif
  28651. #ifdef HAVE_ECC_SIGN
  28652. WC_FREE_VAR(sig, HEAP_HINT);
  28653. WC_FREE_VAR(digest, HEAP_HINT);
  28654. #endif
  28655. (void)keySize;
  28656. (void)curve_id;
  28657. (void)rng;
  28658. return ret;
  28659. }
  28660. #endif /* HAVE_ECC_VERIFY */
  28661. static int test_sm2_create_digest(void)
  28662. {
  28663. #ifndef WOLFSSL_SM3
  28664. const byte msg[] = "message to sign";
  28665. const byte id[] = "0123456789";
  28666. const byte badId[] = "0123556789";
  28667. byte expected[] = {
  28668. 0xdd, 0x4d, 0x65, 0x49, 0xa3, 0x64, 0x76, 0xc0,
  28669. 0x73, 0x05, 0xdc, 0x05, 0x16, 0xb5, 0xee, 0x9f,
  28670. 0x82, 0xf9, 0xe9, 0x7d, 0x01, 0x1a, 0xdc, 0x88,
  28671. 0x5a, 0x59, 0x9c, 0x44, 0xcc, 0x47, 0xa4, 0x78
  28672. };
  28673. ecc_key key;
  28674. int ret;
  28675. /* test key values */
  28676. const char qx[] =
  28677. "af178b7b8740cc9d5b493fbd22049c12621bc27dcc5802e75ff4d045a4158baf";
  28678. const char qy[] =
  28679. "89933faf7a4798f48c5b9b4cd3a7693d54c9e05449946eb489c0dd50a5294805";
  28680. const char d[] =
  28681. "b3e66c2dbfb50c6ff6830c1fac4b51293a2562f9e667052b03df2d4b43c1f34a";
  28682. int hash_type = WC_HASH_TYPE_SHA256;
  28683. byte digest[WC_SHA256_DIGEST_SIZE];
  28684. #else
  28685. ecc_key key;
  28686. int ret;
  28687. const byte msg[] = { 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20,
  28688. 0x64, 0x69, 0x67, 0x65, 0x73, 0x74, 0x00 };
  28689. const byte id[] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
  28690. 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
  28691. 0x00 };
  28692. const byte badId[] = "0123556789";
  28693. const char qx[] =
  28694. "09F9DF311E5421A150DD7D161E4BC5C672179FAD1833FC076BB08FF356F35020";
  28695. const char qy[] =
  28696. "CCEA490CE26775A52DC6EA718CC1AA600AED05FBF35E084A6632F6072DA9AD13";
  28697. const char d[] =
  28698. "3945208F7B2144B13F36E38AC6D39F95889393692860B51A42FB81EF4DF7C5B8";
  28699. byte expected[] = {
  28700. 0xf0, 0xb4, 0x3e, 0x94, 0xba, 0x45, 0xac, 0xca,
  28701. 0xac, 0xe6, 0x92, 0xed, 0x53, 0x43, 0x82, 0xeb,
  28702. 0x17, 0xe6, 0xab, 0x5a, 0x19, 0xce, 0x7b, 0x31,
  28703. 0xf4, 0x48, 0x6f, 0xdf, 0xc0, 0xd2, 0x86, 0x40
  28704. };
  28705. int hash_type = WC_HASH_TYPE_SM3;
  28706. byte digest[WC_SM3_DIGEST_SIZE];
  28707. #endif
  28708. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  28709. if (ret != 0)
  28710. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28711. ret = wc_ecc_import_raw(&key, qx, qy, d, "SM2P256V1");
  28712. if (ret != 0)
  28713. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28714. ret = wc_ecc_sm2_create_digest(id, (int)XSTRLEN((const char*)id),
  28715. msg, (int)XSTRLEN((const char*)msg), hash_type, digest, sizeof(digest),
  28716. &key);
  28717. if (ret != 0)
  28718. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28719. if (XMEMCMP(digest, expected, WC_SHA256_DIGEST_SIZE) != 0)
  28720. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28721. ret = wc_ecc_sm2_create_digest(badId, (int)XSTRLEN((const char*)badId),
  28722. msg, (int)XSTRLEN((const char*)msg), hash_type, digest, sizeof(digest),
  28723. &key);
  28724. if (ret != 0)
  28725. goto done;
  28726. /* should be different than the previous ID used */
  28727. if (XMEMCMP(digest, expected, WC_SHA256_DIGEST_SIZE) == 0)
  28728. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  28729. done:
  28730. wc_ecc_free(&key);
  28731. return ret;
  28732. }
  28733. static int test_sm2_verify(void)
  28734. {
  28735. int ret = 0;
  28736. #ifdef HAVE_ECC_VERIFY
  28737. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_CUSTOM_CURVES)
  28738. ret = test_sm2_verify_caseA2();
  28739. if (ret != 0)
  28740. return ret;
  28741. #endif
  28742. ret = test_sm2_verify_case();
  28743. if (ret != 0)
  28744. return ret;
  28745. #endif /* HAVE_ECC_VERIFY */
  28746. ret = test_sm2_create_digest();
  28747. return ret;
  28748. }
  28749. #endif /* WOLFSSL_SM2 */
  28750. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ECC_SECP) && !defined(NO_ASN_TIME)
  28751. /* Make Cert / Sign example for ECC cert and ECC CA */
  28752. static wc_test_ret_t ecc_test_cert_gen(WC_RNG* rng)
  28753. {
  28754. wc_test_ret_t ret;
  28755. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28756. Cert *myCert = (Cert *)XMALLOC(sizeof *myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28757. #ifdef WOLFSSL_TEST_CERT
  28758. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28759. #endif
  28760. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28761. ecc_key *certPubKey = (ecc_key *)XMALLOC(sizeof *certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28762. #else
  28763. Cert myCert[1];
  28764. #ifdef WOLFSSL_TEST_CERT
  28765. DecodedCert decode[1];
  28766. #endif
  28767. ecc_key caEccKey[1];
  28768. ecc_key certPubKey[1];
  28769. #endif
  28770. int certSz;
  28771. size_t bytes = 0;
  28772. word32 idx = 0;
  28773. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_FILESYSTEM)
  28774. XFILE file;
  28775. #endif
  28776. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28777. byte* der = NULL;
  28778. #else
  28779. byte der[FOURK_BUF];
  28780. #endif
  28781. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28782. if ((myCert == NULL)
  28783. #ifdef WOLFSSL_TEST_CERT
  28784. || (decode == NULL)
  28785. #endif
  28786. || (caEccKey == NULL) || (certPubKey == NULL))
  28787. ERROR_OUT(MEMORY_E, exit);
  28788. #endif
  28789. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  28790. XMEMSET(certPubKey, 0, sizeof *certPubKey);
  28791. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28792. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28793. if (der == NULL) {
  28794. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  28795. }
  28796. #endif
  28797. /* Get cert private key */
  28798. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  28799. /* Get Cert Key 384 */
  28800. #ifdef USE_CERT_BUFFERS_256
  28801. XMEMCPY(der, ca_ecc_key_der_384, sizeof_ca_ecc_key_der_384);
  28802. bytes = sizeof_ca_ecc_key_der_384;
  28803. #elif !defined(NO_FILESYSTEM)
  28804. file = XFOPEN(eccCaKey384File, "rb");
  28805. if (!file) {
  28806. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  28807. }
  28808. bytes = XFREAD(der, 1, FOURK_BUF, file);
  28809. XFCLOSE(file);
  28810. if (bytes == 0)
  28811. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  28812. (void)eccCaKeyFile;
  28813. #else
  28814. WOLFSSL_MSG("No file system and USE_CERT_BUFFERS_256 not defined.(4)");
  28815. ERROR_OUT(ASN_PARSE_E, exit);
  28816. #endif /* USE_CERT_BUFFERS_256 */
  28817. /* end if ENABLE_ECC384_CERT_GEN_TEST */
  28818. #else
  28819. /* !ENABLE_ECC384_CERT_GEN_TEST */
  28820. #ifdef USE_CERT_BUFFERS_256
  28821. XMEMCPY(der, ca_ecc_key_der_256, sizeof_ca_ecc_key_der_256);
  28822. bytes = sizeof_ca_ecc_key_der_256;
  28823. #else
  28824. file = XFOPEN(eccCaKeyFile, "rb");
  28825. if (!file) {
  28826. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  28827. }
  28828. bytes = XFREAD(der, 1, FOURK_BUF, file);
  28829. XFCLOSE(file);
  28830. if (bytes == 0)
  28831. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  28832. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  28833. (void)eccCaKey384File;
  28834. #endif
  28835. #endif /* USE_CERT_BUFFERS_256 */
  28836. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  28837. /* Get CA Key */
  28838. ret = wc_ecc_init_ex(caEccKey, HEAP_HINT, devId);
  28839. if (ret != 0)
  28840. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28841. ret = wc_EccPrivateKeyDecode(der, &idx, caEccKey, (word32)bytes);
  28842. if (ret != 0)
  28843. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28844. /* Make a public key */
  28845. ret = wc_ecc_init_ex(certPubKey, HEAP_HINT, devId);
  28846. if (ret != 0)
  28847. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28848. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, certPubKey);
  28849. #if defined(WOLFSSL_ASYNC_CRYPT)
  28850. ret = wc_AsyncWait(ret, &certPubKey->asyncDev, WC_ASYNC_FLAG_NONE);
  28851. #endif
  28852. if (ret != 0)
  28853. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28854. TEST_SLEEP();
  28855. /* Setup Certificate */
  28856. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  28857. if (ret != 0)
  28858. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28859. #ifndef NO_SHA256
  28860. myCert->sigType = CTC_SHA256wECDSA;
  28861. #else
  28862. myCert->sigType = CTC_SHAwECDSA;
  28863. #endif
  28864. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  28865. #ifdef WOLFSSL_CERT_EXT
  28866. /* add Policies */
  28867. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  28868. CTC_MAX_CERTPOL_SZ);
  28869. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  28870. CTC_MAX_CERTPOL_SZ);
  28871. myCert->certPoliciesNb = 2;
  28872. /* add SKID from the Public Key */
  28873. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, certPubKey);
  28874. if (ret != 0)
  28875. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28876. /* add AKID from the Public Key */
  28877. ret = wc_SetAuthKeyIdFromPublicKey(myCert, NULL, caEccKey);
  28878. if (ret != 0)
  28879. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28880. /* add Key Usage */
  28881. ret = wc_SetKeyUsage(myCert, certKeyUsage);
  28882. if (ret != 0)
  28883. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28884. #endif /* WOLFSSL_CERT_EXT */
  28885. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  28886. #if defined(USE_CERT_BUFFERS_256)
  28887. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_384,
  28888. sizeof_ca_ecc_cert_der_384);
  28889. #elif !defined(NO_FILESYSTEM)
  28890. ret = wc_SetIssuer(myCert, eccCaCert384File);
  28891. (void)eccCaCertFile;
  28892. #else
  28893. /* not testing with embedded, no file system target */
  28894. ERROR_OUT(ASN_PARSE_E, exit);
  28895. #endif /* USE_CERT_BUFFERS_256 */
  28896. #else
  28897. /* not ENABLE_ECC384_CERT_GEN_TEST */
  28898. #if defined(USE_CERT_BUFFERS_256)
  28899. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_256,
  28900. sizeof_ca_ecc_cert_der_256);
  28901. #else
  28902. ret = wc_SetIssuer(myCert, eccCaCertFile);
  28903. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  28904. (void)eccCaCert384File;
  28905. #endif
  28906. #endif
  28907. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  28908. if (ret < 0)
  28909. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28910. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, certPubKey, rng);
  28911. if (certSz < 0) {
  28912. ERROR_OUT(WC_TEST_RET_ENC_EC(certSz), exit);
  28913. }
  28914. ret = 0;
  28915. do {
  28916. #if defined(WOLFSSL_ASYNC_CRYPT)
  28917. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  28918. #endif
  28919. if (ret >= 0) {
  28920. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  28921. FOURK_BUF, NULL, caEccKey, rng);
  28922. }
  28923. } while (ret == WC_PENDING_E);
  28924. if (ret < 0)
  28925. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28926. certSz = (int)ret;
  28927. TEST_SLEEP();
  28928. #ifdef WOLFSSL_TEST_CERT
  28929. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  28930. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  28931. if (ret != 0) {
  28932. FreeDecodedCert(decode);
  28933. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28934. }
  28935. FreeDecodedCert(decode);
  28936. #endif
  28937. ret = SaveDerAndPem(der, certSz, certEccDerFile, certEccPemFile,
  28938. CERT_TYPE);
  28939. if (ret != 0) {
  28940. goto exit;
  28941. }
  28942. exit:
  28943. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28944. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28945. #endif
  28946. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28947. if (myCert != NULL)
  28948. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28949. #ifdef WOLFSSL_TEST_CERT
  28950. if (decode != NULL)
  28951. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28952. #endif
  28953. if (caEccKey != NULL) {
  28954. wc_ecc_free(caEccKey);
  28955. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28956. }
  28957. if (certPubKey != NULL) {
  28958. wc_ecc_free(certPubKey);
  28959. XFREE(certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28960. }
  28961. #else
  28962. wc_ecc_free(certPubKey);
  28963. wc_ecc_free(caEccKey);
  28964. #endif
  28965. return ret;
  28966. }
  28967. #endif /* WOLFSSL_CERT_GEN */
  28968. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  28969. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC) && \
  28970. (!defined(NO_ECC_SECP) || defined(WOLFSSL_CUSTOM_CURVES))
  28971. /* Test for the wc_ecc_key_new() and wc_ecc_key_free() functions. */
  28972. static wc_test_ret_t ecc_test_allocator(WC_RNG* rng)
  28973. {
  28974. wc_test_ret_t ret = 0;
  28975. ecc_key* key;
  28976. #ifdef WC_NO_RNG
  28977. word32 idx = 0;
  28978. #endif
  28979. key = wc_ecc_key_new(HEAP_HINT);
  28980. if (key == NULL) {
  28981. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  28982. }
  28983. #ifndef WC_NO_RNG
  28984. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  28985. #if defined(WOLFSSL_ASYNC_CRYPT)
  28986. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  28987. #endif
  28988. if (ret != 0)
  28989. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  28990. #else
  28991. /* use test ECC key */
  28992. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  28993. (word32)sizeof_ecc_key_der_256);
  28994. (void)rng;
  28995. #endif
  28996. exit:
  28997. wc_ecc_key_free(key);
  28998. return ret;
  28999. }
  29000. #endif
  29001. /* ECC Non-blocking tests for Sign and Verify */
  29002. /* Requires SP math and supports P384 or P256 */
  29003. /* ./configure --enable-ecc=nonblock --enable-sp=yes,nonblock CFLAGS="-DWOLFSSL_PUBLIC_MP" */
  29004. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_HAVE_SP_ECC) && \
  29005. defined(WOLFSSL_PUBLIC_MP)
  29006. /* ECC Private Key "d" */
  29007. static const byte p256PrivKey[] = {
  29008. /* SECP256R1 */
  29009. /* d */
  29010. 0x1e, 0xe7, 0x70, 0x07, 0xd3, 0x30, 0x94, 0x39,
  29011. 0x28, 0x90, 0xdf, 0x23, 0x88, 0x2c, 0x4a, 0x34,
  29012. 0x15, 0xdb, 0x4c, 0x43, 0xcd, 0xfa, 0xe5, 0x1f,
  29013. 0x3d, 0x4c, 0x37, 0xfe, 0x59, 0x3b, 0x96, 0xd8
  29014. };
  29015. #ifdef HAVE_ECC384
  29016. static const byte p384PrivKey[] = {
  29017. /* SECP384R1 */
  29018. /* d */
  29019. 0xa4, 0xe5, 0x06, 0xe8, 0x06, 0x16, 0x3e, 0xab,
  29020. 0x89, 0xf8, 0x60, 0x43, 0xc0, 0x60, 0x25, 0xdb,
  29021. 0xba, 0x7b, 0xfe, 0x19, 0x35, 0x08, 0x55, 0x65,
  29022. 0x76, 0xe2, 0xdc, 0xe0, 0x01, 0x8b, 0x6b, 0x68,
  29023. 0xdf, 0xcf, 0x6f, 0x80, 0x12, 0xce, 0x79, 0x37,
  29024. 0xeb, 0x2b, 0x9c, 0x7b, 0xc4, 0x68, 0x1c, 0x74
  29025. };
  29026. #endif /* HAVE_ECC384 */
  29027. #ifdef HAVE_ECC521
  29028. static const byte p521PrivKey[] = {
  29029. /* SECP521R1 */
  29030. /* d */
  29031. 0x01, 0x68, 0x91, 0x33, 0x53, 0xe2, 0x90, 0x68,
  29032. 0x11, 0x8f, 0xaa, 0xa8, 0x76, 0x0c, 0xf7, 0x2a,
  29033. 0x07, 0x1b, 0x92, 0x2a, 0xa7, 0x82, 0x3d, 0xfa,
  29034. 0x83, 0xce, 0x70, 0xc8, 0xc2, 0x60, 0x82, 0xfe,
  29035. 0x18, 0x88, 0x68, 0xda, 0x6a, 0x83, 0x46, 0x78,
  29036. 0xe4, 0xe9, 0xe9, 0xcc, 0x51, 0x7f, 0xed, 0x81,
  29037. 0x02, 0x32, 0xee, 0x26, 0x87, 0xcc, 0xed, 0x63,
  29038. 0x3f, 0x39, 0x27, 0xf0, 0xd7, 0x17, 0x77, 0xa1,
  29039. 0xa4, 0x36
  29040. };
  29041. #endif /* HAVE_ECC521 */
  29042. /* ECC public key Qx/Qy */
  29043. static const byte p256PubKey[] = {
  29044. /* SECP256R1 */
  29045. /* Qx */
  29046. 0x96, 0x93, 0x1c, 0x53, 0x0b, 0x43, 0x6c, 0x42,
  29047. 0x0c, 0x52, 0x90, 0xe4, 0xa7, 0xec, 0x98, 0xb1,
  29048. 0xaf, 0xd4, 0x14, 0x49, 0xd8, 0xc1, 0x42, 0x82,
  29049. 0x04, 0x78, 0xd1, 0x90, 0xae, 0xa0, 0x6c, 0x07,
  29050. /* Qy */
  29051. 0xf2, 0x3a, 0xb5, 0x10, 0x32, 0x8d, 0xce, 0x9e,
  29052. 0x76, 0xa0, 0xd2, 0x8c, 0xf3, 0xfc, 0xa9, 0x94,
  29053. 0x43, 0x24, 0xe6, 0x82, 0x00, 0x40, 0xc6, 0xdb,
  29054. 0x1c, 0x2f, 0xcd, 0x38, 0x4b, 0x60, 0xdd, 0x61
  29055. };
  29056. #ifdef HAVE_ECC384
  29057. static const byte p384PubKey[] = {
  29058. /* SECP384R1 */
  29059. /* Qx */
  29060. 0xea, 0xcf, 0x93, 0x4f, 0x2c, 0x09, 0xbb, 0x39,
  29061. 0x14, 0x0f, 0x56, 0x64, 0xc3, 0x40, 0xb4, 0xdf,
  29062. 0x0e, 0x63, 0xae, 0xe5, 0x71, 0x4b, 0x00, 0xcc,
  29063. 0x04, 0x97, 0xff, 0xe1, 0xe9, 0x38, 0x96, 0xbb,
  29064. 0x5f, 0x91, 0xb2, 0x6a, 0xcc, 0xb5, 0x39, 0x5f,
  29065. 0x8f, 0x70, 0x59, 0xf1, 0x01, 0xf6, 0x5a, 0x2b,
  29066. /* Qy */
  29067. 0x01, 0x6c, 0x68, 0x0b, 0xcf, 0x55, 0x25, 0xaf,
  29068. 0x6d, 0x98, 0x48, 0x0a, 0xa8, 0x74, 0xc9, 0xa9,
  29069. 0x17, 0xa0, 0x0c, 0xc3, 0xfb, 0xd3, 0x23, 0x68,
  29070. 0xfe, 0x04, 0x3c, 0x63, 0x50, 0x88, 0x3b, 0xb9,
  29071. 0x4f, 0x7c, 0x67, 0x34, 0xf7, 0x3b, 0xa9, 0x73,
  29072. 0xe7, 0x1b, 0xc3, 0x51, 0x5e, 0x22, 0x18, 0xec
  29073. };
  29074. #endif
  29075. #ifdef HAVE_ECC521
  29076. static const byte p521PubKey[] = {
  29077. /* SECP521R1 */
  29078. /* Qx */
  29079. 0x01, 0x62, 0x6e, 0xf1, 0x00, 0xec, 0xd8, 0x99,
  29080. 0x58, 0x9b, 0x80, 0x6b, 0xfe, 0x2c, 0xf1, 0xb2,
  29081. 0xf0, 0xc8, 0x48, 0xdf, 0xac, 0xd2, 0x3b, 0x71,
  29082. 0x29, 0xab, 0xf0, 0x66, 0x63, 0xd8, 0x8e, 0xb5,
  29083. 0xc8, 0xc2, 0xfc, 0x99, 0x44, 0xe2, 0x45, 0xb1,
  29084. 0x5a, 0x7b, 0xb9, 0x73, 0x01, 0xda, 0x79, 0xec,
  29085. 0x9c, 0x26, 0x27, 0x34, 0x45, 0x26, 0xd5, 0x89,
  29086. 0x4b, 0x44, 0xfe, 0x69, 0x4e, 0x72, 0x14, 0xe3,
  29087. 0x8b, 0xbc,
  29088. /* Qy */
  29089. 0x00, 0x0f, 0x09, 0xa2, 0x03, 0xc3, 0x5a, 0xdc,
  29090. 0x95, 0x82, 0xf6, 0xf9, 0xf6, 0x9c, 0xff, 0xb5,
  29091. 0x6b, 0x75, 0x95, 0x4b, 0xa4, 0x28, 0x5d, 0x9e,
  29092. 0x90, 0x04, 0xd1, 0xc0, 0x1e, 0xd5, 0xfd, 0x43,
  29093. 0x9e, 0x1e, 0x83, 0xc0, 0x11, 0x2b, 0x2b, 0x07,
  29094. 0x6d, 0xa9, 0x7a, 0x10, 0xd7, 0x67, 0xe7, 0x51,
  29095. 0x37, 0x24, 0xd8, 0xbf, 0x03, 0x0d, 0x8b, 0xb5,
  29096. 0x40, 0x5c, 0x4f, 0xd6, 0x13, 0x73, 0x42, 0xbc,
  29097. 0x91, 0xd9
  29098. };
  29099. /* perform verify of signature and hash using public key */
  29100. /* key is public Qx + public Qy */
  29101. /* sig is r + s */
  29102. static wc_test_ret_t crypto_ecc_verify(const byte *key, uint32_t keySz,
  29103. const byte *hash, uint32_t hashSz, const byte *sig, uint32_t sigSz,
  29104. uint32_t curveSz, int curveId)
  29105. {
  29106. wc_test_ret_t ret;
  29107. int verify_res = 0, count = 0;
  29108. mp_int r, s;
  29109. ecc_key ecc;
  29110. ecc_nb_ctx_t nb_ctx;
  29111. /* validate arguments */
  29112. if (key == NULL || hash == NULL || sig == NULL || curveSz == 0 ||
  29113. hashSz == 0 || keySz < (curveSz*2) || sigSz < (curveSz*2))
  29114. {
  29115. return WC_TEST_RET_ENC_NC;
  29116. }
  29117. /* Setup the ECC key */
  29118. ret = wc_ecc_init(&ecc);
  29119. if (ret < 0) {
  29120. return WC_TEST_RET_ENC_EC(ret);
  29121. }
  29122. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  29123. if (ret != MP_OKAY) {
  29124. wc_ecc_free(&ecc);
  29125. return WC_TEST_RET_ENC_EC(ret);
  29126. }
  29127. /* Setup the signature r/s variables */
  29128. ret = mp_init(&r);
  29129. if (ret != MP_OKAY) {
  29130. wc_ecc_free(&ecc);
  29131. return WC_TEST_RET_ENC_EC(ret);
  29132. }
  29133. ret = mp_init(&s);
  29134. if (ret != MP_OKAY) {
  29135. mp_clear(&r);
  29136. wc_ecc_free(&ecc);
  29137. return WC_TEST_RET_ENC_EC(ret);
  29138. }
  29139. /* Import public key x/y */
  29140. ret = wc_ecc_import_unsigned(
  29141. &ecc,
  29142. (byte*)key, /* Public "x" Coordinate */
  29143. (byte*)(key + curveSz), /* Public "y" Coordinate */
  29144. NULL, /* Private "d" (optional) */
  29145. curveId /* ECC Curve Id */
  29146. );
  29147. /* Make sure it was a public key imported */
  29148. if (ret == 0 && ecc.type != ECC_PUBLICKEY) {
  29149. ret = WC_TEST_RET_ENC_NC; /* ECC_BAD_ARG_E */
  29150. }
  29151. /* Import signature r/s */
  29152. if (ret == 0) {
  29153. ret = mp_read_unsigned_bin(&r, sig, curveSz);
  29154. if (ret < 0)
  29155. ret = WC_TEST_RET_ENC_EC(ret);
  29156. }
  29157. if (ret == 0) {
  29158. ret = mp_read_unsigned_bin(&s, sig + curveSz, curveSz);
  29159. if (ret < 0)
  29160. ret = WC_TEST_RET_ENC_EC(ret);
  29161. }
  29162. /* Verify ECC Signature */
  29163. if (ret == 0) {
  29164. do {
  29165. ret = wc_ecc_verify_hash_ex(
  29166. &r, &s, /* r/s as mp_int */
  29167. hash, hashSz, /* computed hash digest */
  29168. &verify_res, /* verification result 1=success */
  29169. &ecc
  29170. );
  29171. count++;
  29172. /* This is where real-time work could be called */
  29173. } while (ret == FP_WOULDBLOCK);
  29174. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  29175. printf("ECC non-block verify: %d times\n", count);
  29176. #endif
  29177. if (ret < 0)
  29178. ret = WC_TEST_RET_ENC_EC(ret);
  29179. }
  29180. /* check verify result */
  29181. if (ret == 0 && verify_res == 0) {
  29182. ret = WC_TEST_RET_ENC_NC /* SIG_VERIFY_E */;
  29183. }
  29184. mp_clear(&r);
  29185. mp_clear(&s);
  29186. wc_ecc_free(&ecc);
  29187. (void)count;
  29188. return ret;
  29189. }
  29190. /* perform signature operation against hash using private key */
  29191. static wc_test_ret_t crypto_ecc_sign(const byte *key, uint32_t keySz,
  29192. const byte *hash, uint32_t hashSz, byte *sig, uint32_t* sigSz,
  29193. uint32_t curveSz, int curveId, WC_RNG* rng)
  29194. {
  29195. wc_test_ret_t ret;
  29196. int count = 0;
  29197. mp_int r, s;
  29198. ecc_key ecc;
  29199. ecc_nb_ctx_t nb_ctx;
  29200. /* validate arguments */
  29201. if (key == NULL || hash == NULL || sig == NULL || sigSz == NULL ||
  29202. curveSz == 0 || hashSz == 0 || keySz < curveSz || *sigSz < (curveSz*2))
  29203. {
  29204. return WC_TEST_RET_ENC_NC /* BAD_FUNC_ARG */;
  29205. }
  29206. /* Initialize signature result */
  29207. memset(sig, 0, curveSz*2);
  29208. /* Setup the ECC key */
  29209. ret = wc_ecc_init(&ecc);
  29210. if (ret < 0) {
  29211. return WC_TEST_RET_ENC_EC(ret);
  29212. }
  29213. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  29214. if (ret != MP_OKAY) {
  29215. wc_ecc_free(&ecc);
  29216. return WC_TEST_RET_ENC_EC(ret);
  29217. }
  29218. /* Setup the signature r/s variables */
  29219. ret = mp_init(&r);
  29220. if (ret != MP_OKAY) {
  29221. wc_ecc_free(&ecc);
  29222. return WC_TEST_RET_ENC_EC(ret);
  29223. }
  29224. ret = mp_init(&s);
  29225. if (ret != MP_OKAY) {
  29226. mp_clear(&r);
  29227. wc_ecc_free(&ecc);
  29228. return WC_TEST_RET_ENC_EC(ret);
  29229. }
  29230. /* Import private key "k" */
  29231. ret = wc_ecc_import_private_key_ex(
  29232. key, keySz, /* private key "d" */
  29233. NULL, 0, /* public (optional) */
  29234. &ecc,
  29235. curveId /* ECC Curve Id */
  29236. );
  29237. if (ret < 0)
  29238. ret = WC_TEST_RET_ENC_EC(ret);
  29239. if (ret == 0) {
  29240. do {
  29241. /* Verify ECC Signature */
  29242. ret = wc_ecc_sign_hash_ex(
  29243. hash, hashSz, /* computed hash digest */
  29244. rng, &ecc, /* random and key context */
  29245. &r, &s /* r/s as mp_int */
  29246. );
  29247. count++;
  29248. /* This is where real-time work could be called */
  29249. } while (ret == FP_WOULDBLOCK);
  29250. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  29251. printf("ECC non-block sign: %d times\n", count);
  29252. #endif
  29253. if (ret < 0)
  29254. ret = WC_TEST_RET_ENC_EC(ret);
  29255. }
  29256. if (ret == 0) {
  29257. /* export r/s */
  29258. mp_to_unsigned_bin_len(&r, sig, curveSz);
  29259. mp_to_unsigned_bin_len(&s, sig + curveSz, curveSz);
  29260. }
  29261. mp_clear(&r);
  29262. mp_clear(&s);
  29263. wc_ecc_free(&ecc);
  29264. (void)count;
  29265. return ret;
  29266. }
  29267. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  29268. /*
  29269. * This test doesn't work with WOLFSSL_VALIDATE_ECC_KEYGEN defined because we
  29270. * don't have non-blocking versions of the key checking functions, yet.
  29271. */
  29272. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  29273. static wc_test_ret_t ecc_test_nonblock_dhe(int curveId, word32 curveSz,
  29274. const byte* privKey, const byte* pubKey, WC_RNG* rng)
  29275. {
  29276. wc_test_ret_t ret;
  29277. ecc_key keyA;
  29278. ecc_key keyB;
  29279. ecc_nb_ctx_t nbCtxA;
  29280. ecc_nb_ctx_t nbCtxB;
  29281. byte secretA[ECC_SHARED_SIZE];
  29282. byte secretB[ECC_SHARED_SIZE];
  29283. word32 secretSzA = ECC_SHARED_SIZE;
  29284. word32 secretSzB = ECC_SHARED_SIZE;
  29285. int count = 0;
  29286. ret = wc_ecc_init(&keyA);
  29287. if (ret == 0) {
  29288. ret = wc_ecc_init(&keyB);
  29289. if (ret < 0)
  29290. ret = WC_TEST_RET_ENC_EC(ret);
  29291. }
  29292. if (ret == 0) {
  29293. ret = wc_ecc_set_nonblock(&keyA, &nbCtxA);
  29294. if (ret < 0)
  29295. ret = WC_TEST_RET_ENC_EC(ret);
  29296. }
  29297. if (ret == 0) {
  29298. ret = wc_ecc_set_nonblock(&keyB, &nbCtxB);
  29299. if (ret < 0)
  29300. ret = WC_TEST_RET_ENC_EC(ret);
  29301. }
  29302. if (ret == 0) {
  29303. do {
  29304. ret = wc_ecc_make_key_ex(rng, curveSz, &keyA, curveId);
  29305. count++;
  29306. } while (ret == FP_WOULDBLOCK);
  29307. if (ret < 0)
  29308. ret = WC_TEST_RET_ENC_EC(ret);
  29309. }
  29310. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  29311. fprintf(stderr, "ECC non-block key gen: %d times\n", count);
  29312. #endif
  29313. if (ret == 0) {
  29314. ret = wc_ecc_check_key(&keyA);
  29315. if (ret < 0)
  29316. ret = WC_TEST_RET_ENC_EC(ret);
  29317. }
  29318. if (ret == 0) {
  29319. ret = wc_ecc_import_unsigned(&keyB, pubKey, pubKey + curveSz,
  29320. privKey, curveId);
  29321. if (ret < 0)
  29322. ret = WC_TEST_RET_ENC_EC(ret);
  29323. }
  29324. count = 0;
  29325. if (ret == 0) {
  29326. do {
  29327. ret = wc_ecc_shared_secret(&keyA, &keyB, secretA, &secretSzA);
  29328. count++;
  29329. } while (ret == FP_WOULDBLOCK);
  29330. if (ret < 0)
  29331. ret = WC_TEST_RET_ENC_EC(ret);
  29332. }
  29333. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  29334. fprintf(stderr, "ECC non-block shared secret: %d times\n", count);
  29335. #endif
  29336. if (ret == 0) {
  29337. do {
  29338. ret = wc_ecc_shared_secret(&keyB, &keyA, secretB, &secretSzB);
  29339. } while (ret == FP_WOULDBLOCK);
  29340. if (ret < 0)
  29341. ret = WC_TEST_RET_ENC_EC(ret);
  29342. }
  29343. if (ret == 0) {
  29344. if (secretSzA != secretSzB ||
  29345. XMEMCMP(secretA, secretB, secretSzA) != 0) {
  29346. ret = WC_TEST_RET_ENC_NC;
  29347. }
  29348. }
  29349. wc_ecc_free(&keyA);
  29350. wc_ecc_free(&keyB);
  29351. return ret;
  29352. }
  29353. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  29354. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  29355. static wc_test_ret_t ecc_test_nonblock_ecdsa(int curveId, word32 curveSz,
  29356. const byte* privKey, word32 privKeySz, const byte* pubKey, word32 pubKeySz,
  29357. WC_RNG* rng)
  29358. {
  29359. wc_test_ret_t ret = 0;
  29360. byte* sig = NULL;
  29361. word32 sigSz = curveSz * 2;
  29362. static const byte hash[] = {
  29363. 0x8d, 0x28, 0xa3, 0x8b, 0x0b, 0xa9, 0xfe, 0xd4, 0x0e, 0x54, 0xc4, 0x17,
  29364. 0x3d, 0x54, 0x66, 0x34, 0xbf, 0x5d, 0x6f, 0x46, 0xc2, 0x20, 0xcb, 0xc3,
  29365. 0x22, 0xe9, 0xb0, 0xdf, 0xe7, 0x64, 0x3f, 0xd9
  29366. };
  29367. sig = (byte*)XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  29368. if (sig == NULL) {
  29369. ret = WC_TEST_RET_ENC_ERRNO;
  29370. }
  29371. if (ret == 0) {
  29372. /* Sign hash using private key */
  29373. /* Note: result of an ECC sign varies for each call even with same
  29374. private key and hash. This is because a new random public key is
  29375. used for each operation. */
  29376. ret = crypto_ecc_sign(privKey, privKeySz, hash, sizeof(hash), sig,
  29377. &sigSz, curveSz, curveId, rng);
  29378. }
  29379. if (ret == 0) {
  29380. /* Verify generated signature is valid */
  29381. ret = crypto_ecc_verify(pubKey, pubKeySz, hash, sizeof(hash), sig,
  29382. sigSz, curveSz, curveId);
  29383. }
  29384. if (sig != NULL) {
  29385. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  29386. }
  29387. return ret;
  29388. }
  29389. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  29390. static wc_test_ret_t ecc_test_nonblock(WC_RNG* rng)
  29391. {
  29392. wc_test_ret_t ret = 0;
  29393. word32 i;
  29394. int curveIds[3] = {0, 0, 0};
  29395. word32 curveSzs[3] = {0, 0, 0};
  29396. const byte* privKeys[3] = {NULL, NULL, NULL};
  29397. word32 privKeySzs[3] = {0, 0, 0};
  29398. const byte* pubKeys[3] = {NULL, NULL, NULL};
  29399. word32 pubKeySzs[3] = {0, 0, 0};
  29400. curveIds[0] = ECC_SECP256R1;
  29401. curveSzs[0] = 32;
  29402. privKeys[0] = p256PrivKey;
  29403. privKeySzs[0] = sizeof(p256PrivKey);
  29404. pubKeys[0] = p256PubKey;
  29405. pubKeySzs[0] = sizeof(p256PubKey);
  29406. #ifdef HAVE_ECC384
  29407. curveIds[1] = ECC_SECP384R1;
  29408. curveSzs[1] = 48;
  29409. privKeys[1] = p384PrivKey;
  29410. privKeySzs[1] = sizeof(p384PrivKey);
  29411. pubKeys[1] = p384PubKey;
  29412. pubKeySzs[1] = sizeof(p384PubKey);
  29413. #endif
  29414. #ifdef HAVE_ECC521
  29415. curveIds[2] = ECC_SECP521R1;
  29416. curveSzs[2] = 66;
  29417. privKeys[2] = p521PrivKey;
  29418. privKeySzs[2] = sizeof(p521PrivKey);
  29419. pubKeys[2] = p521PubKey;
  29420. pubKeySzs[2] = sizeof(p521PubKey);
  29421. #endif
  29422. for (i = 0; ret == 0 && i < sizeof(curveIds) / sizeof(curveIds[0]); ++i) {
  29423. if (curveIds[i] == 0) {
  29424. continue;
  29425. }
  29426. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  29427. ret = ecc_test_nonblock_ecdsa(curveIds[i], curveSzs[i], privKeys[i],
  29428. privKeySzs[i], pubKeys[i], pubKeySzs[i], rng);
  29429. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  29430. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  29431. if (ret == 0) {
  29432. ret = ecc_test_nonblock_dhe(curveIds[i], curveSzs[i], privKeys[i],
  29433. pubKeys[i], rng);
  29434. }
  29435. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  29436. }
  29437. return ret;
  29438. }
  29439. #endif /* WC_ECC_NONBLOCK && WOLFSSL_HAVE_SP_ECC && WOLFSSL_PUBLIC_MP */
  29440. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST) && \
  29441. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  29442. (HAVE_FIPS_VERSION > 2)))
  29443. static int ecc_test_raw_enc_dec(void)
  29444. {
  29445. int ret;
  29446. unsigned char r[1];
  29447. word32 rSz;
  29448. unsigned char s[1];
  29449. word32 sSz;
  29450. unsigned char rZero[] = { 0, 0, 0, 0 };
  29451. unsigned char sOne[] = { 0, 0, 1 };
  29452. unsigned char sigRaw[32];
  29453. word32 sigRawSz;
  29454. unsigned char expSig[] = { 0x30, 0x06, 0x02, 0x01, 0x00, 0x02, 0x01, 0x01 };
  29455. sigRawSz = sizeof(sigRaw);
  29456. ret = wc_ecc_rs_raw_to_sig(rZero, sizeof(rZero), sOne, sizeof(sOne),
  29457. sigRaw, &sigRawSz);
  29458. if (ret != 0) {
  29459. return WC_TEST_RET_ENC_EC(ret);
  29460. }
  29461. if (sigRawSz != sizeof(expSig)) {
  29462. return WC_TEST_RET_ENC_EC((int)sigRawSz);
  29463. }
  29464. if (XMEMCMP(sigRaw, expSig, sizeof(expSig)) != 0) {
  29465. return WC_TEST_RET_ENC_NC;
  29466. }
  29467. rSz = sizeof(r);
  29468. sSz = sizeof(s);
  29469. ret = wc_ecc_sig_to_rs(sigRaw, sigRawSz, r, &rSz, s, &sSz);
  29470. if (ret != 0) {
  29471. return WC_TEST_RET_ENC_EC(ret);
  29472. }
  29473. if (rSz != 1) {
  29474. return WC_TEST_RET_ENC_EC((int)rSz);
  29475. }
  29476. if (sSz != 1) {
  29477. return WC_TEST_RET_ENC_EC((int)sSz);
  29478. }
  29479. if (r[0] != 0) {
  29480. return WC_TEST_RET_ENC_EC(r[0]);
  29481. }
  29482. if (s[0] != 1) {
  29483. return WC_TEST_RET_ENC_EC(s[0]);
  29484. }
  29485. return ret;
  29486. }
  29487. #endif
  29488. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test(void)
  29489. {
  29490. wc_test_ret_t ret;
  29491. WC_RNG rng;
  29492. WOLFSSL_ENTER("ecc_test");
  29493. #if defined(ECC_MIN_KEY_SZ)
  29494. WOLFSSL_MSG_EX("ecc_test ECC_MIN_KEY_SZ = %d\n", ECC_MIN_KEY_SZ);
  29495. #else
  29496. WOLFSSL_MSG("ecc_test ECC_MIN_KEY_SZ not defined.");
  29497. #endif
  29498. #if defined(WOLFSSL_CERT_EXT) && \
  29499. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  29500. ret = ecc_decode_test();
  29501. if (ret < 0)
  29502. return ret;
  29503. #endif
  29504. #ifndef HAVE_FIPS
  29505. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  29506. #else
  29507. ret = wc_InitRng(&rng);
  29508. #endif
  29509. #ifndef WC_NO_RNG
  29510. if (ret != 0)
  29511. return WC_TEST_RET_ENC_EC(ret);
  29512. #else
  29513. (void)ret;
  29514. #endif
  29515. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  29516. ret = ecc_test_curve(&rng, 14, ECC_CURVE_DEF);
  29517. if (ret < 0) {
  29518. printf("keySize=14, Default\n");
  29519. goto done;
  29520. }
  29521. #endif /* HAVE_ECC112 */
  29522. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  29523. ret = ecc_test_curve(&rng, 16, ECC_CURVE_DEF);
  29524. if (ret < 0) {
  29525. printf("keySize=16, Default\n");
  29526. goto done;
  29527. }
  29528. #endif /* HAVE_ECC128 */
  29529. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  29530. ret = ecc_test_curve(&rng, 20, ECC_CURVE_DEF);
  29531. if (ret < 0) {
  29532. printf("keySize=20, Default\n");
  29533. goto done;
  29534. }
  29535. #endif /* HAVE_ECC160 */
  29536. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  29537. #if !FIPS_VERSION3_GE(6,0,0)
  29538. ret = ecc_test_curve(&rng, 24, ECC_CURVE_DEF);
  29539. if (ret < 0) {
  29540. printf("keySize=24, Default\n");
  29541. goto done;
  29542. }
  29543. #endif
  29544. #endif /* HAVE_ECC192 */
  29545. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  29546. ret = ecc_test_curve(&rng, 28, ECC_CURVE_DEF);
  29547. if (ret < 0) {
  29548. printf("keySize=28, Default\n");
  29549. goto done;
  29550. }
  29551. #endif /* HAVE_ECC224 */
  29552. #if (defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 239
  29553. ret = ecc_test_curve(&rng, 30, ECC_CURVE_DEF);
  29554. if (ret < 0) {
  29555. printf("keySize=30, Default\n");
  29556. goto done;
  29557. }
  29558. #endif /* HAVE_ECC239 */
  29559. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  29560. ret = ecc_test_curve(&rng, 32, ECC_CURVE_DEF);
  29561. if (ret < 0) {
  29562. printf("keySize=32, Default\n");
  29563. goto done;
  29564. }
  29565. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  29566. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  29567. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  29568. ret = ecc_point_test();
  29569. if (ret < 0) {
  29570. goto done;
  29571. }
  29572. #endif
  29573. #if !defined(NO_ECC_SECP) || defined(WOLFSSL_CUSTOM_CURVES)
  29574. ret = ecc_def_curve_test(&rng);
  29575. if (ret < 0) {
  29576. printf("Default\n");
  29577. goto done;
  29578. }
  29579. #endif
  29580. #endif /* !NO_ECC256 */
  29581. #if (defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 320
  29582. ret = ecc_test_curve(&rng, 40, ECC_CURVE_DEF);
  29583. if (ret < 0) {
  29584. printf("keySize=40, Default\n");
  29585. goto done;
  29586. }
  29587. #endif /* HAVE_ECC320 */
  29588. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  29589. ret = ecc_test_curve(&rng, 48, ECC_CURVE_DEF);
  29590. if (ret < 0) {
  29591. printf("keySize=48, Default\n");
  29592. goto done;
  29593. }
  29594. #endif /* HAVE_ECC384 */
  29595. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  29596. ret = ecc_test_curve(&rng, 64, ECC_CURVE_DEF);
  29597. if (ret < 0) {
  29598. printf("keySize=64, Default\n");
  29599. goto done;
  29600. }
  29601. #endif /* HAVE_ECC512 */
  29602. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  29603. ret = ecc_test_curve(&rng, 66, ECC_CURVE_DEF);
  29604. if (ret < 0) {
  29605. printf("keySize=66, Default\n");
  29606. goto done;
  29607. }
  29608. #endif /* HAVE_ECC521 */
  29609. #ifdef WOLFSSL_SM2
  29610. ret = ecc_test_curve(&rng, 32, ECC_SM2P256V1);
  29611. if (ret < 0) {
  29612. printf("SM2\n");
  29613. goto done;
  29614. }
  29615. #endif
  29616. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST) && \
  29617. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  29618. (HAVE_FIPS_VERSION > 2)))
  29619. ret = ecc_test_raw_enc_dec();
  29620. if (ret != 0) {
  29621. printf("raw sig encode/decode\n");
  29622. goto done;
  29623. }
  29624. #endif
  29625. #if defined(WOLFSSL_CUSTOM_CURVES)
  29626. ret = ecc_test_custom_curves(&rng);
  29627. if (ret != 0) {
  29628. printf("Custom\n");
  29629. goto done;
  29630. }
  29631. #endif
  29632. #if defined(WOLFSSL_SM2)
  29633. ret = test_sm2_verify();
  29634. if (ret != 0) {
  29635. printf("SM2 Verify\n");
  29636. goto done;
  29637. }
  29638. ret = ecc_sm2_test_curve(&rng, ECC_TEST_VERIFY_COUNT);
  29639. if (ret != 0) {
  29640. printf("SM2 test\n");
  29641. goto done;
  29642. }
  29643. #endif
  29644. #if defined(HAVE_ECC_SIGN) && \
  29645. (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  29646. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) && \
  29647. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3))
  29648. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  29649. ret = ecc_test_deterministic_k(&rng);
  29650. if (ret != 0) {
  29651. printf("ecc_test_deterministic_k failed!\n");
  29652. goto done;
  29653. }
  29654. #endif
  29655. #ifdef WOLFSSL_PUBLIC_MP
  29656. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  29657. ECC_MIN_KEY_SZ <= 384
  29658. ret = ecc384_test_deterministic_k(&rng);
  29659. if (ret != 0) {
  29660. printf("ecc384_test_deterministic_k failed!\n");
  29661. goto done;
  29662. }
  29663. #endif
  29664. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && \
  29665. ECC_MIN_KEY_SZ <= 521
  29666. ret = ecc521_test_deterministic_k(&rng);
  29667. if (ret != 0) {
  29668. printf("ecc512_test_deterministic_k failed!\n");
  29669. goto done;
  29670. }
  29671. #endif
  29672. #endif
  29673. #endif
  29674. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  29675. !defined(WOLFSSL_KCAPI_ECC)
  29676. ret = ecc_test_sign_vectors(&rng);
  29677. if (ret != 0) {
  29678. printf("ecc_test_sign_vectors failed!\n");
  29679. goto done;
  29680. }
  29681. #endif
  29682. #if defined(HAVE_ECC_VECTOR_TEST) && defined(HAVE_ECC_CDH) && \
  29683. defined(HAVE_ECC_DHE)
  29684. ret = ecc_test_cdh_vectors(&rng);
  29685. if (ret != 0) {
  29686. printf("ecc_test_cdh_vectors failed!\n");
  29687. goto done;
  29688. }
  29689. #endif
  29690. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  29691. !defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
  29692. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  29693. ret = ecc_test_make_pub(&rng);
  29694. if (ret != 0) {
  29695. printf("ecc_test_make_pub failed!\n");
  29696. goto done;
  29697. }
  29698. #elif defined(HAVE_ECC_KEY_IMPORT)
  29699. (void)ecc_test_make_pub; /* for compiler warning */
  29700. #endif
  29701. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ECC_SECP) && !defined(NO_ASN_TIME)
  29702. ret = ecc_test_cert_gen(&rng);
  29703. if (ret != 0) {
  29704. printf("ecc_test_cert_gen failed!\n");
  29705. goto done;
  29706. }
  29707. #endif
  29708. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC) && \
  29709. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && (!defined(NO_ECC_SECP) || \
  29710. defined(WOLFSSL_CUSTOM_CURVES))
  29711. ret = ecc_test_allocator(&rng);
  29712. if (ret != 0) {
  29713. printf("ecc_test_allocator failed!\n");
  29714. goto done;
  29715. }
  29716. #endif
  29717. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  29718. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  29719. ret = ecc_test_nonblock(&rng);
  29720. if (ret != 0) {
  29721. printf("ecc_test_nonblock failed!\n");
  29722. goto done;
  29723. }
  29724. #endif
  29725. done:
  29726. wc_FreeRng(&rng);
  29727. return ret;
  29728. }
  29729. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  29730. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  29731. #if ((! defined(HAVE_FIPS)) || FIPS_VERSION_GE(5,3))
  29732. static wc_test_ret_t ecc_ctx_kdf_salt_test(WC_RNG* rng, ecc_key* a, ecc_key* b)
  29733. {
  29734. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29735. byte* plaintext;
  29736. byte* encrypted;
  29737. byte* decrypted;
  29738. #else
  29739. byte plaintext[128];
  29740. byte encrypted[128];
  29741. byte decrypted[128];
  29742. #endif
  29743. ecEncCtx* aCtx = NULL;
  29744. ecEncCtx* bCtx = NULL;
  29745. static const byte salt[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  29746. 14, 15};
  29747. wc_test_ret_t ret = 0;
  29748. static const char message[] = "Hello wolfSSL!";
  29749. word32 plaintextLen;
  29750. word32 encryptLen = 128;
  29751. word32 decryptLen = 128;
  29752. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29753. plaintext = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29754. encrypted = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29755. decrypted = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29756. #endif
  29757. wc_ecc_free(a);
  29758. wc_ecc_free(b);
  29759. ret = wc_ecc_init(a);
  29760. if (ret != 0)
  29761. ret = WC_TEST_RET_ENC_EC(ret);
  29762. if (ret == 0) {
  29763. ret = wc_ecc_init(b);
  29764. if (ret != 0)
  29765. ret = WC_TEST_RET_ENC_EC(ret);
  29766. }
  29767. if (ret == 0)
  29768. ret = wc_ecc_make_key(rng, 32, a);
  29769. if (ret == 0)
  29770. ret = wc_ecc_make_key(rng, 32, b);
  29771. /* create context */
  29772. if (ret == 0) {
  29773. aCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  29774. if (aCtx == NULL)
  29775. ret = WC_TEST_RET_ENC_NC;
  29776. }
  29777. if (ret == 0) {
  29778. bCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  29779. if (bCtx == NULL)
  29780. ret = WC_TEST_RET_ENC_NC;
  29781. }
  29782. /* set salt */
  29783. if (ret == 0) {
  29784. ret = wc_ecc_ctx_set_kdf_salt(aCtx, salt, sizeof(salt));
  29785. if (ret != 0)
  29786. ret = 10472;
  29787. }
  29788. if (ret == 0) {
  29789. ret = wc_ecc_ctx_set_kdf_salt(bCtx, salt, sizeof(salt));
  29790. if (ret != 0)
  29791. ret = 10473;
  29792. }
  29793. XMEMSET(plaintext, 0, 128);
  29794. XSTRLCPY((char *)plaintext, message, sizeof plaintext);
  29795. plaintextLen = (((word32)XSTRLEN(message) + AES_BLOCK_SIZE - 1) /
  29796. AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
  29797. /* encrypt */
  29798. if (ret == 0) {
  29799. ret = wc_ecc_encrypt(a, b, plaintext, plaintextLen, encrypted,
  29800. &encryptLen, aCtx);
  29801. if (ret != 0)
  29802. ret = WC_TEST_RET_ENC_EC(ret);
  29803. }
  29804. /* decrypt */
  29805. if (ret == 0) {
  29806. ret = wc_ecc_decrypt(b, a, encrypted, encryptLen, decrypted,
  29807. &decryptLen, bCtx);
  29808. if (ret != 0)
  29809. ret = WC_TEST_RET_ENC_EC(ret);
  29810. }
  29811. /* compare */
  29812. if (ret == 0 && XMEMCMP(decrypted, plaintext, plaintextLen) != 0)
  29813. ret = WC_TEST_RET_ENC_NC;
  29814. wc_ecc_free(a);
  29815. wc_ecc_free(b);
  29816. wc_ecc_ctx_free(aCtx);
  29817. wc_ecc_ctx_free(bCtx);
  29818. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29819. XFREE(plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29820. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29821. XFREE(decrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29822. #endif
  29823. return ret;
  29824. }
  29825. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  29826. /* ecc_encrypt_e2e_test() uses wc_ecc_ctx_set_algo(), which was added in
  29827. * wolfFIPS 5.3.
  29828. * ecc_encrypt_kat() is used only by ecc_encrypt_e2e_test().
  29829. */
  29830. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  29831. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  29832. ECC_MIN_KEY_SZ <= 256 && defined(WOLFSSL_AES_128)
  29833. static wc_test_ret_t ecc_encrypt_kat(WC_RNG *rng)
  29834. {
  29835. wc_test_ret_t ret = 0;
  29836. #ifdef WOLFSSL_ECIES_OLD
  29837. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29838. ecc_key* userA = NULL;
  29839. #else
  29840. ecc_key userA[1];
  29841. #endif
  29842. int userAInit = 0;
  29843. #endif
  29844. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29845. ecc_key* userB = NULL;
  29846. #else
  29847. ecc_key userB[1];
  29848. #endif
  29849. int userBInit = 0;
  29850. ecc_key* tmpKey;
  29851. byte plain[48];
  29852. word32 plainSz = sizeof(plain);
  29853. WOLFSSL_SMALL_STACK_STATIC const byte privKey[] = {
  29854. 0x04, 0x80, 0xef, 0x1d, 0xbe, 0x02, 0x0c, 0x20,
  29855. 0x5b, 0xab, 0x80, 0x35, 0x5b, 0x2a, 0x0f, 0x6d,
  29856. 0xd3, 0xb0, 0x7f, 0x7e, 0x7f, 0x86, 0x8a, 0x49,
  29857. 0xee, 0xb4, 0xaa, 0x09, 0x2d, 0x1e, 0x1d, 0x02
  29858. };
  29859. #if defined(WOLFSSL_ECIES_OLD) || defined(WOLFSSL_QNX_CAAM)
  29860. WOLFSSL_SMALL_STACK_STATIC const byte pubKey[] = {
  29861. 0x04,
  29862. /* X */
  29863. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  29864. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  29865. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  29866. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  29867. /* X */
  29868. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  29869. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  29870. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  29871. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28
  29872. };
  29873. #endif
  29874. WOLFSSL_SMALL_STACK_STATIC const byte enc_msg[] = {
  29875. #ifdef WOLFSSL_ECIES_OLD
  29876. 0x42, 0x70, 0xbf, 0xf9, 0xf4, 0x7e, 0x4b, 0x9b,
  29877. 0xb5, 0x4c, 0xcc, 0xc5, 0x94, 0xa7, 0xef, 0xaa,
  29878. 0xc3, 0x7c, 0x85, 0xa6, 0x51, 0x6e, 0xd3, 0xfa,
  29879. 0x56, 0xc9, 0x10, 0x4d, 0x14, 0x32, 0x61, 0xb8,
  29880. 0xbb, 0x66, 0x7a, 0xb5, 0xbc, 0x95, 0xf8, 0xca,
  29881. 0xd1, 0x2a, 0x19, 0x51, 0x44, 0xd8, 0x0e, 0x57,
  29882. 0x34, 0xed, 0x45, 0x89, 0x2e, 0x57, 0xbe, 0xd5,
  29883. 0x06, 0x22, 0xd7, 0x13, 0x0a, 0x0e, 0x40, 0x36,
  29884. 0x0d, 0x05, 0x0d, 0xb6, 0xae, 0x61, 0x37, 0x18,
  29885. 0x83, 0x90, 0x0a, 0x27, 0x95, 0x41, 0x8c, 0x45
  29886. #elif defined(WOLFSSL_ECIES_ISO18033)
  29887. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  29888. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  29889. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  29890. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  29891. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  29892. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  29893. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  29894. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  29895. 0x28, 0xbb, 0x9f, 0xa8, 0x2d, 0xe1, 0xf1, 0x67,
  29896. 0x45, 0x02, 0x19, 0xdc, 0xc8, 0x24, 0x8b, 0x20,
  29897. 0x02, 0xa0, 0x8f, 0x95, 0x12, 0x55, 0x51, 0xf8,
  29898. 0x03, 0xc4, 0x54, 0x13, 0x98, 0x2d, 0xf0, 0x31,
  29899. 0x51, 0x80, 0x45, 0x24, 0xcb, 0x8b, 0x48, 0xa6,
  29900. 0x8b, 0x8e, 0x97, 0x9c, 0x56, 0x4d, 0x70, 0x00,
  29901. 0x53, 0xd3, 0x47, 0x00, 0x5a, 0x23, 0x8c, 0xf9,
  29902. 0xfd, 0xd2, 0x33, 0x2c, 0x43, 0x6e, 0x9e, 0xb2,
  29903. 0xf4, 0x95, 0xd4, 0xcf, 0x30, 0xd6, 0xa2, 0xc5,
  29904. 0x35, 0x96, 0x6a, 0xd4, 0x36, 0x15, 0xa9, 0xbd,
  29905. 0x7f
  29906. #elif defined(WOLFSSL_ECIES_GEN_IV)
  29907. /* EC P-256 point */
  29908. 0x04,
  29909. /* X */
  29910. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  29911. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  29912. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  29913. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  29914. /* Y */
  29915. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  29916. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  29917. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  29918. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28,
  29919. /* IV */
  29920. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29921. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29922. /* Encrypted Msg */
  29923. 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d, 0x50,
  29924. 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c, 0x1f,
  29925. 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc, 0x44,
  29926. 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca, 0x65,
  29927. 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61, 0xbd,
  29928. 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1, 0xde,
  29929. /* HMAC */
  29930. 0x5a, 0x22, 0xc1, 0x5d, 0x99, 0x66, 0x3f, 0x24,
  29931. 0x35, 0x96, 0xac, 0xf7, 0xf6, 0x28, 0x45, 0x16,
  29932. 0x52, 0x19, 0x0d, 0xe4, 0xb2, 0xca, 0x5b, 0x28,
  29933. 0x4e, 0xbb, 0xf3, 0x98, 0x57, 0xd7, 0x3b, 0xe2
  29934. #else
  29935. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  29936. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  29937. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  29938. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  29939. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  29940. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  29941. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  29942. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  29943. 0x28, 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d,
  29944. 0x50, 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c,
  29945. 0x1f, 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc,
  29946. 0x44, 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca,
  29947. 0x65, 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61,
  29948. 0xbd, 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1,
  29949. 0xde, 0xc7, 0x3f, 0x6f, 0xce, 0xbe, 0x49, 0x61,
  29950. 0x48, 0x01, 0x77, 0x41, 0xd0, 0xd8, 0x5b, 0x48,
  29951. 0xca, 0x4e, 0x47, 0x3e, 0x47, 0xbf, 0x1d, 0x28,
  29952. 0x4c, 0x18, 0x1a, 0xfb, 0x96, 0x95, 0xda, 0xde,
  29953. 0x55
  29954. #endif
  29955. };
  29956. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  29957. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  29958. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  29959. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  29960. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  29961. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  29962. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  29963. };
  29964. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29965. userB = (ecc_key *)XMALLOC(sizeof(*userB), HEAP_HINT,
  29966. DYNAMIC_TYPE_TMP_BUFFER);
  29967. if (userB == NULL) {
  29968. ret = WC_TEST_RET_ENC_ERRNO;
  29969. }
  29970. #ifdef WOLFSSL_ECIES_OLD
  29971. if (ret == 0) {
  29972. userA = (ecc_key *)XMALLOC(sizeof(*userA), HEAP_HINT,
  29973. DYNAMIC_TYPE_TMP_BUFFER);
  29974. if (userA == NULL) {
  29975. ret = WC_TEST_RET_ENC_ERRNO;
  29976. }
  29977. }
  29978. #endif
  29979. #endif
  29980. if (ret == 0) {
  29981. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  29982. if (ret != 0)
  29983. ret = WC_TEST_RET_ENC_EC(ret);
  29984. }
  29985. if (ret == 0) {
  29986. userBInit = 1;
  29987. #ifdef WOLFSSL_ECIES_OLD
  29988. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  29989. if (ret != 0)
  29990. ret = WC_TEST_RET_ENC_EC(ret);
  29991. }
  29992. if (ret == 0) {
  29993. userAInit = 1;
  29994. tmpKey = userA;
  29995. #else
  29996. tmpKey = NULL;
  29997. #endif
  29998. }
  29999. if (ret == 0) {
  30000. #ifdef WOLFSSL_QNX_CAAM
  30001. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), pubKey,
  30002. sizeof(pubKey), userB, ECC_SECP256R1);
  30003. #else
  30004. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), NULL, 0,
  30005. userB, ECC_SECP256R1);
  30006. #endif
  30007. if (ret != 0)
  30008. ret = WC_TEST_RET_ENC_EC(ret);
  30009. }
  30010. #ifdef WOLFSSL_ECIES_OLD
  30011. if (ret == 0) {
  30012. ret = wc_ecc_import_x963_ex(pubKey, sizeof(pubKey), userA,
  30013. ECC_SECP256R1);
  30014. if (ret != 0)
  30015. ret = WC_TEST_RET_ENC_EC(ret);
  30016. }
  30017. #endif
  30018. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  30019. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  30020. !defined(HAVE_SELFTEST)
  30021. if (ret == 0) {
  30022. ret = wc_ecc_set_rng(userB, rng);
  30023. if (ret != 0) {
  30024. ret = WC_TEST_RET_ENC_EC(ret);
  30025. }
  30026. }
  30027. #else
  30028. (void)rng;
  30029. #endif
  30030. if (ret == 0) {
  30031. ret = wc_ecc_decrypt(userB, tmpKey, enc_msg, sizeof(enc_msg), plain,
  30032. &plainSz, NULL);
  30033. if (ret != 0)
  30034. ret = WC_TEST_RET_ENC_EC(ret);
  30035. }
  30036. if (ret == 0) {
  30037. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  30038. ret = WC_TEST_RET_ENC_NC;
  30039. }
  30040. }
  30041. if (userBInit)
  30042. wc_ecc_free(userB);
  30043. #ifdef WOLFSSL_ECIES_OLD
  30044. if (userAInit)
  30045. wc_ecc_free(userA);
  30046. #endif
  30047. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30048. if (userB != NULL) {
  30049. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30050. }
  30051. #ifdef WOLFSSL_ECIES_OLD
  30052. if (userA != NULL) {
  30053. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30054. }
  30055. #endif
  30056. #endif
  30057. return ret;
  30058. }
  30059. #endif
  30060. static wc_test_ret_t ecc_encrypt_e2e_test(WC_RNG* rng, ecc_key* userA, ecc_key* userB,
  30061. byte encAlgo, byte kdfAlgo, byte macAlgo)
  30062. {
  30063. wc_test_ret_t ret = 0;
  30064. byte msg[48];
  30065. byte plain[48];
  30066. #ifdef WOLFSSL_ECIES_OLD
  30067. byte out[80];
  30068. #elif defined(WOLFSSL_ECIES_GEN_IV)
  30069. byte out[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  30070. #else
  30071. byte out[1 + ECC_KEYGEN_SIZE * 2 + 80];
  30072. #endif
  30073. word32 outSz = sizeof(out);
  30074. word32 plainSz = sizeof(plain);
  30075. int i;
  30076. ecEncCtx* cliCtx = NULL;
  30077. ecEncCtx* srvCtx = NULL;
  30078. byte cliSalt[EXCHANGE_SALT_SZ];
  30079. byte srvSalt[EXCHANGE_SALT_SZ];
  30080. const byte* tmpSalt;
  30081. byte msg2[48];
  30082. byte plain2[48];
  30083. #ifdef WOLFSSL_ECIES_OLD
  30084. byte out2[80];
  30085. #elif defined(WOLFSSL_ECIES_GEN_IV)
  30086. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  30087. #else
  30088. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 80];
  30089. #endif
  30090. word32 outSz2 = sizeof(out2);
  30091. word32 plainSz2 = sizeof(plain2);
  30092. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30093. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof(ecc_key), HEAP_HINT,
  30094. DYNAMIC_TYPE_TMP_BUFFER);
  30095. #else
  30096. ecc_key tmpKey[1];
  30097. #endif
  30098. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30099. if (tmpKey == NULL) {
  30100. ERROR_OUT(MEMORY_E, done);
  30101. }
  30102. #endif
  30103. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  30104. if (ret != 0)
  30105. goto done;
  30106. /* set message to incrementing 0,1,2,etc... */
  30107. for (i = 0; i < (int)sizeof(msg); i++)
  30108. msg[i] = i;
  30109. /* encrypt msg to B */
  30110. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz, NULL);
  30111. if (ret != 0) {
  30112. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  30113. }
  30114. #ifdef WOLFSSL_ECIES_OLD
  30115. tmpKey->dp = userA->dp;
  30116. ret = wc_ecc_copy_point(&userA->pubkey, &tmpKey->pubkey);
  30117. if (ret != 0) {
  30118. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  30119. }
  30120. #endif
  30121. /* decrypt msg from A */
  30122. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, NULL);
  30123. if (ret != 0) {
  30124. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  30125. }
  30126. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  30127. ret = WC_TEST_RET_ENC_NC; goto done;
  30128. }
  30129. #ifndef WOLFSSL_ECIES_OLD
  30130. /* A decrypts msg (response) from B */
  30131. ret = wc_ecc_decrypt(userB, NULL, out, outSz, plain2, &plainSz2, NULL);
  30132. if (ret != 0)
  30133. goto done;
  30134. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  30135. ret = WC_TEST_RET_ENC_NC; goto done;
  30136. }
  30137. #endif
  30138. /* let's verify message exchange works, A is client, B is server */
  30139. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  30140. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  30141. if (cliCtx == NULL || srvCtx == NULL) {
  30142. ret = WC_TEST_RET_ENC_ERRNO; goto done;
  30143. }
  30144. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  30145. if (ret != 0)
  30146. goto done;
  30147. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  30148. if (ret != 0)
  30149. goto done;
  30150. /* get salt to send to peer */
  30151. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  30152. if (tmpSalt == NULL) {
  30153. ret = WC_TEST_RET_ENC_NC; goto done;
  30154. }
  30155. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  30156. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  30157. if (tmpSalt == NULL) {
  30158. ret = WC_TEST_RET_ENC_NC; goto done;
  30159. }
  30160. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  30161. /* in actual use, we'd get the peer's salt over the transport */
  30162. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  30163. if (ret != 0)
  30164. goto done;
  30165. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  30166. if (ret != 0)
  30167. goto done;
  30168. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11);
  30169. if (ret != 0)
  30170. goto done;
  30171. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11);
  30172. if (ret != 0)
  30173. goto done;
  30174. /* get encrypted msg (request) to send to B */
  30175. outSz = sizeof(out);
  30176. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz,cliCtx);
  30177. if (ret != 0)
  30178. goto done;
  30179. #ifndef WOLFSSL_ECIES_OLD
  30180. wc_ecc_free(tmpKey);
  30181. #endif
  30182. /* B decrypts msg (request) from A */
  30183. plainSz = sizeof(plain);
  30184. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  30185. if (ret != 0)
  30186. goto done;
  30187. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  30188. ret = WC_TEST_RET_ENC_NC; goto done;
  30189. }
  30190. /* msg2 (response) from B to A */
  30191. for (i = 0; i < (int)sizeof(msg2); i++)
  30192. msg2[i] = i + sizeof(msg2);
  30193. /* get encrypted msg (response) to send to B */
  30194. ret = wc_ecc_encrypt(userB, userA, msg2, sizeof(msg2), out2,
  30195. &outSz2, srvCtx);
  30196. if (ret != 0)
  30197. goto done;
  30198. #ifdef WOLFSSL_ECIES_OLD
  30199. tmpKey->dp = userB->dp;
  30200. ret = wc_ecc_copy_point(&userB->pubkey, &tmpKey->pubkey);
  30201. if (ret != 0) {
  30202. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  30203. }
  30204. #else
  30205. wc_ecc_free(tmpKey);
  30206. #endif
  30207. /* A decrypts msg (response) from B */
  30208. ret = wc_ecc_decrypt(userA, tmpKey, out2, outSz2, plain2, &plainSz2,
  30209. cliCtx);
  30210. if (ret != 0)
  30211. goto done;
  30212. if (XMEMCMP(plain2, msg2, sizeof(msg2)) != 0) {
  30213. ret = WC_TEST_RET_ENC_NC; goto done;
  30214. }
  30215. #if defined(HAVE_COMP_KEY) && \
  30216. (! defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  30217. /* Create new client and server contexts. */
  30218. wc_ecc_ctx_free(srvCtx);
  30219. wc_ecc_ctx_free(cliCtx);
  30220. /* let's verify message exchange works, A is client, B is server */
  30221. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  30222. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  30223. if (cliCtx == NULL || srvCtx == NULL) {
  30224. ret = WC_TEST_RET_ENC_ERRNO; goto done;
  30225. }
  30226. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  30227. if (ret != 0)
  30228. goto done;
  30229. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  30230. if (ret != 0)
  30231. goto done;
  30232. /* get salt to send to peer */
  30233. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  30234. if (tmpSalt == NULL) {
  30235. ret = WC_TEST_RET_ENC_NC; goto done;
  30236. }
  30237. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  30238. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  30239. if (tmpSalt == NULL) {
  30240. ret = WC_TEST_RET_ENC_NC; goto done;
  30241. }
  30242. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  30243. /* in actual use, we'd get the peer's salt over the transport */
  30244. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  30245. if (ret != 0)
  30246. goto done;
  30247. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  30248. if (ret != 0)
  30249. goto done;
  30250. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 12);
  30251. if (ret != 0)
  30252. goto done;
  30253. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 12);
  30254. if (ret != 0)
  30255. goto done;
  30256. /* get encrypted msg (request) to send to B - compressed public key */
  30257. outSz = sizeof(out);
  30258. ret = wc_ecc_encrypt_ex(userA, userB, msg, sizeof(msg), out, &outSz, cliCtx,
  30259. 1);
  30260. if (ret != 0)
  30261. goto done;
  30262. #ifndef WOLFSSL_ECIES_OLD
  30263. wc_ecc_free(tmpKey);
  30264. #endif
  30265. /* B decrypts msg (request) from A - out has a compressed public key */
  30266. plainSz = sizeof(plain);
  30267. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  30268. if (ret != 0)
  30269. goto done;
  30270. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  30271. ret = WC_TEST_RET_ENC_NC; goto done;
  30272. }
  30273. #endif /* HAVE_COMP_KEY && (!FIPS || FIPS>=5.3) */
  30274. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  30275. (ECC_MIN_KEY_SZ <= 256) && defined(WOLFSSL_AES_128)
  30276. ret = ecc_encrypt_kat(rng);
  30277. #endif
  30278. done:
  30279. /* cleanup */
  30280. wc_ecc_ctx_free(srvCtx);
  30281. wc_ecc_ctx_free(cliCtx);
  30282. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30283. if (tmpKey != NULL) {
  30284. wc_ecc_free(tmpKey);
  30285. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30286. }
  30287. #else
  30288. wc_ecc_free(tmpKey);
  30289. #endif
  30290. return ret;
  30291. }
  30292. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  30293. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_encrypt_test(void)
  30294. {
  30295. WC_RNG rng;
  30296. wc_test_ret_t ret;
  30297. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30298. ecc_key *userA;
  30299. ecc_key *userB;
  30300. #else
  30301. ecc_key userA[1];
  30302. ecc_key userB[1];
  30303. #endif
  30304. WOLFSSL_ENTER("ecc_encrypt_test");
  30305. #ifndef HAVE_FIPS
  30306. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  30307. #else
  30308. ret = wc_InitRng(&rng);
  30309. #endif
  30310. if (ret != 0)
  30311. return WC_TEST_RET_ENC_EC(ret);
  30312. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30313. userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT,
  30314. DYNAMIC_TYPE_TMP_BUFFER);
  30315. userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT,
  30316. DYNAMIC_TYPE_TMP_BUFFER);
  30317. if ((userA == NULL) || (userB == NULL)) {
  30318. ERROR_OUT(MEMORY_E, done);
  30319. }
  30320. #endif
  30321. XMEMSET(userA, 0, sizeof *userA);
  30322. XMEMSET(userB, 0, sizeof *userB);
  30323. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  30324. if (ret != 0)
  30325. goto done;
  30326. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  30327. if (ret != 0)
  30328. goto done;
  30329. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userA);
  30330. #if defined(WOLFSSL_ASYNC_CRYPT)
  30331. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  30332. #endif
  30333. if (ret != 0){
  30334. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  30335. }
  30336. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userB);
  30337. #if defined(WOLFSSL_ASYNC_CRYPT)
  30338. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  30339. #endif
  30340. if (ret != 0){
  30341. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  30342. }
  30343. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  30344. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  30345. !defined(HAVE_SELFTEST)
  30346. ret = wc_ecc_set_rng(userA, &rng);
  30347. if (ret != 0) {
  30348. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  30349. }
  30350. ret = wc_ecc_set_rng(userB, &rng);
  30351. if (ret != 0) {
  30352. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  30353. }
  30354. #endif
  30355. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  30356. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  30357. #ifdef WOLFSSL_AES_128
  30358. if (ret == 0) {
  30359. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  30360. ecHKDF_SHA256, ecHMAC_SHA256);
  30361. if (ret != 0) {
  30362. printf("ECIES: AES_128_CBC, HKDF_SHA256, HMAC_SHA256\n");
  30363. }
  30364. }
  30365. #ifdef HAVE_X963_KDF
  30366. if (ret == 0) {
  30367. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  30368. ecKDF_X963_SHA256, ecHMAC_SHA256);
  30369. if (ret != 0) {
  30370. printf("ECIES: AES_128_CBC, KDF_X963_SHA256, HMAC_SHA256\n");
  30371. }
  30372. }
  30373. if (ret == 0) {
  30374. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  30375. ecKDF_SHA256, ecHMAC_SHA256);
  30376. if (ret != 0) {
  30377. printf("ECIES: AES_128_CBC, KDF_SHA256, HMAC_SHA256\n");
  30378. }
  30379. }
  30380. #endif
  30381. #endif
  30382. #ifdef WOLFSSL_AES_256
  30383. if (ret == 0) {
  30384. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CBC,
  30385. ecHKDF_SHA256, ecHMAC_SHA256);
  30386. if (ret != 0) {
  30387. printf("ECIES: AES_256_CBC, HKDF_SHA256, HMAC_SHA256\n");
  30388. }
  30389. }
  30390. #endif
  30391. #endif
  30392. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER)
  30393. #ifdef WOLFSSL_AES_128
  30394. if (ret == 0) {
  30395. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CTR,
  30396. ecHKDF_SHA256, ecHMAC_SHA256);
  30397. if (ret != 0) {
  30398. printf("ECIES: AES_128_CTR, HKDF_SHA256, HMAC_SHA256\n");
  30399. }
  30400. }
  30401. #endif
  30402. #ifdef WOLFSSL_AES_256
  30403. if (ret == 0) {
  30404. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CTR,
  30405. ecHKDF_SHA256, ecHMAC_SHA256);
  30406. if (ret != 0) {
  30407. printf("ECIES: AES_256_CTR, HKDF_SHA256, HMAC_SHA256\n");
  30408. }
  30409. }
  30410. #endif
  30411. #endif /* !NO_AES && WOLFSSL_AES_COUNTER */
  30412. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  30413. if (ret == 0) {
  30414. ret = ecc_ctx_kdf_salt_test(&rng, userA, userB);
  30415. }
  30416. #endif
  30417. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  30418. done:
  30419. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30420. if (userA != NULL) {
  30421. wc_ecc_free(userA);
  30422. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30423. }
  30424. if (userB != NULL) {
  30425. wc_ecc_free(userB);
  30426. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30427. }
  30428. #else
  30429. wc_ecc_free(userB);
  30430. wc_ecc_free(userA);
  30431. #endif
  30432. wc_FreeRng(&rng);
  30433. return ret;
  30434. }
  30435. #endif /* HAVE_ECC_ENCRYPT && HAVE_AES_CBC && WOLFSSL_AES_128 */
  30436. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  30437. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  30438. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  30439. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  30440. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test_buffers(void)
  30441. {
  30442. size_t bytes;
  30443. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30444. ecc_key *cliKey = (ecc_key *)XMALLOC(sizeof *cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30445. ecc_key *servKey = (ecc_key *)XMALLOC(sizeof *servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30446. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof *tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30447. #else
  30448. ecc_key cliKey[1];
  30449. ecc_key servKey[1];
  30450. ecc_key tmpKey[1];
  30451. #endif
  30452. WC_RNG rng;
  30453. word32 idx = 0;
  30454. wc_test_ret_t ret;
  30455. /* pad our test message to 32 bytes so evenly divisible by AES_BLOCK_SZ */
  30456. byte in[] = "Everyone gets Friday off. ecc p";
  30457. word32 inLen = (word32)XSTRLEN((char*)in);
  30458. byte out[256];
  30459. byte plain[256];
  30460. int verify = 0;
  30461. word32 x;
  30462. WOLFSSL_ENTER("ecc_test_buffers");
  30463. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30464. if ((cliKey == NULL) || (servKey == NULL) || (tmpKey == NULL))
  30465. ERROR_OUT(MEMORY_E, done);
  30466. #endif
  30467. ret = wc_ecc_init_ex(cliKey, HEAP_HINT, devId);
  30468. if (ret != 0)
  30469. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30470. ret = wc_ecc_init_ex(servKey, HEAP_HINT, devId);
  30471. if (ret != 0)
  30472. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30473. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  30474. if (ret != 0)
  30475. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30476. bytes = (size_t)sizeof_ecc_clikey_der_256;
  30477. /* place client key into ecc_key struct cliKey */
  30478. ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, cliKey,
  30479. (word32)bytes);
  30480. if (ret != 0)
  30481. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30482. idx = 0;
  30483. bytes = (size_t)sizeof_ecc_key_der_256;
  30484. /* place server key into ecc_key struct servKey */
  30485. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, servKey,
  30486. (word32)bytes);
  30487. if (ret != 0)
  30488. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30489. #ifndef WC_NO_RNG
  30490. #ifndef HAVE_FIPS
  30491. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  30492. #else
  30493. ret = wc_InitRng(&rng);
  30494. #endif
  30495. if (ret != 0)
  30496. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30497. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  30498. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  30499. !defined(HAVE_SELFTEST)
  30500. ret = wc_ecc_set_rng(cliKey, &rng);
  30501. if (ret != 0)
  30502. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30503. ret = wc_ecc_set_rng(servKey, &rng);
  30504. if (ret != 0)
  30505. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30506. #endif
  30507. #endif /* !WC_NO_RNG */
  30508. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_HKDF) && \
  30509. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  30510. {
  30511. word32 y;
  30512. /* test encrypt and decrypt if they're available */
  30513. x = sizeof(out);
  30514. ret = wc_ecc_encrypt(cliKey, servKey, in, sizeof(in), out, &x, NULL);
  30515. if (ret < 0)
  30516. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30517. #ifdef WOLFSSL_ECIES_OLD
  30518. tmpKey->dp = cliKey->dp;
  30519. ret = wc_ecc_copy_point(&cliKey->pubkey, &tmpKey->pubkey);
  30520. if (ret != 0) {
  30521. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  30522. }
  30523. #endif
  30524. y = sizeof(plain);
  30525. ret = wc_ecc_decrypt(servKey, tmpKey, out, x, plain, &y, NULL);
  30526. if (ret < 0)
  30527. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30528. if (XMEMCMP(plain, in, inLen))
  30529. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  30530. }
  30531. #endif
  30532. x = sizeof(out);
  30533. do {
  30534. #if defined(WOLFSSL_ASYNC_CRYPT)
  30535. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  30536. #endif
  30537. if (ret == 0)
  30538. ret = wc_ecc_sign_hash(in, inLen, out, &x, &rng, cliKey);
  30539. } while (ret == WC_PENDING_E);
  30540. if (ret < 0)
  30541. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30542. TEST_SLEEP();
  30543. XMEMSET(plain, 0, sizeof(plain));
  30544. do {
  30545. #if defined(WOLFSSL_ASYNC_CRYPT)
  30546. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  30547. #endif
  30548. if (ret == 0)
  30549. ret = wc_ecc_verify_hash(out, x, in, inLen, &verify,
  30550. cliKey);
  30551. } while (ret == WC_PENDING_E);
  30552. if (ret < 0)
  30553. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30554. if (verify != 1)
  30555. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  30556. TEST_SLEEP();
  30557. #ifdef WOLFSSL_CERT_EXT
  30558. idx = 0;
  30559. bytes = sizeof_ecc_clikeypub_der_256;
  30560. ret = wc_EccPublicKeyDecode(ecc_clikeypub_der_256, &idx, cliKey,
  30561. (word32) bytes);
  30562. if (ret != 0)
  30563. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  30564. #endif
  30565. ret = 0;
  30566. done:
  30567. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30568. if (cliKey != NULL) {
  30569. wc_ecc_free(cliKey);
  30570. XFREE(cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30571. }
  30572. if (servKey != NULL) {
  30573. wc_ecc_free(servKey);
  30574. XFREE(servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30575. }
  30576. if (tmpKey != NULL) {
  30577. wc_ecc_free(tmpKey);
  30578. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30579. }
  30580. #else
  30581. wc_ecc_free(cliKey);
  30582. wc_ecc_free(servKey);
  30583. wc_ecc_free(tmpKey);
  30584. #endif
  30585. wc_FreeRng(&rng);
  30586. return ret;
  30587. }
  30588. #endif /* USE_CERT_BUFFERS_256 && !WOLFSSL_ATECCX08A && !NO_ECC256 */
  30589. #endif /* HAVE_ECC */
  30590. #ifdef HAVE_CURVE25519
  30591. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  30592. defined(HAVE_CURVE25519_KEY_IMPORT)
  30593. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  30594. #define X25519_TEST_CNT 5
  30595. #else
  30596. #define X25519_TEST_CNT 1
  30597. #endif
  30598. static wc_test_ret_t curve25519_overflow_test(void)
  30599. {
  30600. /* secret key for party a */
  30601. byte sa[X25519_TEST_CNT][32] = {
  30602. {
  30603. 0x8d,0xaf,0x6e,0x7a,0xc1,0xeb,0x8d,0x30,
  30604. 0x99,0x86,0xd3,0x90,0x47,0x96,0x21,0x3c,
  30605. 0x3a,0x75,0xc0,0x7b,0x75,0x01,0x75,0xa3,
  30606. 0x81,0x4b,0xff,0x5a,0xbc,0x96,0x87,0x28
  30607. },
  30608. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  30609. {
  30610. 0x9d,0x63,0x5f,0xce,0xe2,0xe8,0xd7,0xfb,
  30611. 0x68,0x77,0x0e,0x44,0xd1,0xad,0x87,0x2b,
  30612. 0xf4,0x65,0x06,0xb7,0xbb,0xdb,0xbe,0x6e,
  30613. 0x02,0x43,0x24,0xc7,0x3d,0x7b,0x88,0x60
  30614. },
  30615. {
  30616. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  30617. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  30618. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  30619. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  30620. },
  30621. {
  30622. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  30623. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  30624. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  30625. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  30626. },
  30627. {
  30628. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  30629. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  30630. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  30631. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  30632. }
  30633. #endif
  30634. };
  30635. /* public key for party b */
  30636. byte pb[X25519_TEST_CNT][32] = {
  30637. {
  30638. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30639. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30640. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30641. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  30642. },
  30643. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  30644. {
  30645. /* 0xff first byte in original - invalid! */
  30646. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30647. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30648. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  30649. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  30650. },
  30651. {
  30652. 0x36,0x1a,0x74,0x87,0x28,0x59,0xe0,0xb6,
  30653. 0xe4,0x2b,0x17,0x9b,0x16,0xb0,0x3b,0xf8,
  30654. 0xb8,0x9f,0x2a,0x8f,0xc5,0x33,0x68,0x4f,
  30655. 0xde,0x4d,0xd8,0x80,0x63,0xe7,0xb4,0x0a
  30656. },
  30657. {
  30658. 0x00,0x80,0x38,0x59,0x19,0x3a,0x66,0x12,
  30659. 0xfd,0xa1,0xec,0x1c,0x40,0x84,0x40,0xbd,
  30660. 0x64,0x10,0x8b,0x53,0x81,0x21,0x03,0x2d,
  30661. 0x7d,0x33,0xb4,0x01,0x57,0x0d,0xe1,0x89
  30662. },
  30663. {
  30664. 0x1d,0xf8,0xf8,0x33,0x89,0x6c,0xb7,0xba,
  30665. 0x94,0x73,0xfa,0xc2,0x36,0xac,0xbe,0x49,
  30666. 0xaf,0x85,0x3e,0x93,0x5f,0xae,0xb2,0xc0,
  30667. 0xc8,0x80,0x8f,0x4a,0xaa,0xd3,0x55,0x2b
  30668. }
  30669. #endif
  30670. };
  30671. /* expected shared key */
  30672. byte ss[X25519_TEST_CNT][32] = {
  30673. {
  30674. 0x5c,0x4c,0x85,0x5f,0xfb,0x20,0x38,0xcc,
  30675. 0x55,0x16,0x5b,0x8a,0xa7,0xed,0x57,0x6e,
  30676. 0x35,0xaa,0x71,0x67,0x85,0x1f,0xb6,0x28,
  30677. 0x17,0x07,0x7b,0xda,0x76,0xdd,0xe0,0xb4
  30678. },
  30679. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  30680. {
  30681. 0x33,0xf6,0xc1,0x34,0x62,0x92,0x06,0x02,
  30682. 0x95,0xdb,0x91,0x4c,0x5d,0x52,0x54,0xc7,
  30683. 0xd2,0x5b,0x24,0xb5,0x4f,0x33,0x59,0x79,
  30684. 0x9f,0x6d,0x7e,0x4a,0x4c,0x30,0xd6,0x38
  30685. },
  30686. {
  30687. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30688. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30689. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30690. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02
  30691. },
  30692. {
  30693. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30694. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30695. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30696. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09
  30697. },
  30698. {
  30699. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30700. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30701. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30702. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10
  30703. }
  30704. #endif
  30705. };
  30706. wc_test_ret_t ret = 0;
  30707. int i;
  30708. word32 y;
  30709. byte shared[32];
  30710. curve25519_key userA;
  30711. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  30712. for (i = 0; i < X25519_TEST_CNT; i++) {
  30713. if (wc_curve25519_import_private_raw(sa[i], sizeof(sa[i]), pb[i],
  30714. sizeof(pb[i]), &userA) != 0) {
  30715. ret = WC_TEST_RET_ENC_I(i); break;
  30716. }
  30717. /* test against known test vector */
  30718. XMEMSET(shared, 0, sizeof(shared));
  30719. y = sizeof(shared);
  30720. if (wc_curve25519_shared_secret(&userA, &userA, shared, &y) != 0) {
  30721. ret = WC_TEST_RET_ENC_I(i); break;
  30722. }
  30723. if (XMEMCMP(ss[i], shared, y)) {
  30724. ret = WC_TEST_RET_ENC_I(i); break;
  30725. }
  30726. }
  30727. wc_curve25519_free(&userA);
  30728. return ret;
  30729. }
  30730. /* Test the wc_curve25519_check_public API.
  30731. *
  30732. * returns 0 on success and -ve on failure.
  30733. */
  30734. static wc_test_ret_t curve25519_check_public_test(void)
  30735. {
  30736. wc_test_ret_t ret;
  30737. /* Little-endian values that will fail */
  30738. byte fail_le[][CURVE25519_KEYSIZE] = {
  30739. {
  30740. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30741. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30742. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30743. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  30744. },
  30745. {
  30746. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30747. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30748. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30749. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  30750. },
  30751. {
  30752. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30753. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30754. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30755. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x81
  30756. },
  30757. };
  30758. /* Big-endian values that will fail */
  30759. byte fail_be[][CURVE25519_KEYSIZE] = {
  30760. {
  30761. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30762. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30763. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30764. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  30765. },
  30766. {
  30767. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30768. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30769. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30770. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  30771. },
  30772. {
  30773. 0x81,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30774. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30775. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30776. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  30777. },
  30778. };
  30779. /* Good or valid public value */
  30780. byte good[CURVE25519_KEYSIZE] = {
  30781. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30782. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30783. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30784. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  30785. };
  30786. int i;
  30787. /* Parameter checks */
  30788. /* NULL pointer */
  30789. ret = wc_curve25519_check_public(NULL, 0, EC25519_LITTLE_ENDIAN);
  30790. if (ret != BAD_FUNC_ARG) {
  30791. return WC_TEST_RET_ENC_EC(ret);
  30792. }
  30793. ret = wc_curve25519_check_public(NULL, 0, EC25519_BIG_ENDIAN);
  30794. if (ret != BAD_FUNC_ARG) {
  30795. return WC_TEST_RET_ENC_EC(ret);
  30796. }
  30797. /* Length of 0 treated differently to other invalid lengths for TLS */
  30798. ret = wc_curve25519_check_public(good, 0, EC25519_LITTLE_ENDIAN);
  30799. if (ret != BUFFER_E)
  30800. return WC_TEST_RET_ENC_EC(ret);
  30801. ret = wc_curve25519_check_public(good, 0, EC25519_BIG_ENDIAN);
  30802. if (ret != BUFFER_E)
  30803. return WC_TEST_RET_ENC_EC(ret);
  30804. /* Length not CURVE25519_KEYSIZE */
  30805. for (i = 1; i < CURVE25519_KEYSIZE + 2; i++) {
  30806. if (i == CURVE25519_KEYSIZE)
  30807. continue;
  30808. if (wc_curve25519_check_public(good, (word32)i, EC25519_LITTLE_ENDIAN) !=
  30809. ECC_BAD_ARG_E) {
  30810. return WC_TEST_RET_ENC_I(i);
  30811. }
  30812. if (wc_curve25519_check_public(good, (word32)i, EC25519_BIG_ENDIAN) !=
  30813. ECC_BAD_ARG_E) {
  30814. return WC_TEST_RET_ENC_I(i);
  30815. }
  30816. }
  30817. /* Little-endian fail cases */
  30818. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  30819. if (wc_curve25519_check_public(fail_le[i], CURVE25519_KEYSIZE,
  30820. EC25519_LITTLE_ENDIAN) == 0) {
  30821. return WC_TEST_RET_ENC_I(i);
  30822. }
  30823. }
  30824. /* Big-endian fail cases */
  30825. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  30826. if (wc_curve25519_check_public(fail_be[i], CURVE25519_KEYSIZE,
  30827. EC25519_BIG_ENDIAN) == 0) {
  30828. return WC_TEST_RET_ENC_I(i);
  30829. }
  30830. }
  30831. /* Check a valid public value works! */
  30832. ret = wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  30833. EC25519_LITTLE_ENDIAN);
  30834. if (ret != 0) {
  30835. return WC_TEST_RET_ENC_EC(ret);
  30836. }
  30837. ret = wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  30838. EC25519_BIG_ENDIAN);
  30839. if (ret != 0) {
  30840. return WC_TEST_RET_ENC_EC(ret);
  30841. }
  30842. return 0;
  30843. }
  30844. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  30845. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  30846. defined(HAVE_CURVE25519_KEY_IMPORT)
  30847. static wc_test_ret_t curve255519_der_test(void)
  30848. {
  30849. wc_test_ret_t ret = 0;
  30850. /* certs/statickeys/x25519.der */
  30851. const byte kCurve25519PrivDer[] = {
  30852. 0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E,
  30853. 0x04, 0x22, 0x04, 0x20, 0x78, 0x8E, 0x31, 0x5C, 0x33, 0xA9, 0x19, 0xC0,
  30854. 0x5E, 0x36, 0x70, 0x1B, 0xA4, 0xE8, 0xEF, 0xC1, 0x89, 0x8C, 0xB3, 0x15,
  30855. 0xC6, 0x79, 0xD3, 0xAC, 0x22, 0x00, 0xAE, 0xFA, 0xB3, 0xB7, 0x0F, 0x78
  30856. };
  30857. /* certs/statickeys/x25519-pub.der */
  30858. const byte kCurve25519PubDer[] = {
  30859. 0x30, 0x2A, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E, 0x03, 0x21, 0x00,
  30860. 0x09, 0xBC, 0x8C, 0xC7, 0x45, 0x0D, 0xC1, 0xC2, 0x02, 0x57, 0x9A, 0x68,
  30861. 0x3A, 0xFD, 0x7A, 0xA8, 0xA5, 0x2F, 0xF0, 0x99, 0x39, 0x98, 0xEA, 0x26,
  30862. 0xA2, 0x5B, 0x38, 0xFD, 0x96, 0xDB, 0x2A, 0x26
  30863. };
  30864. curve25519_key key;
  30865. byte output[128];
  30866. word32 outputSz = 128;
  30867. word32 idx;
  30868. ret = wc_curve25519_init_ex(&key, HEAP_HINT, devId);
  30869. if (ret != 0)
  30870. return WC_TEST_RET_ENC_EC(ret);
  30871. /* Test decode / encode of Curve25519 private key only */
  30872. if (ret == 0) {
  30873. idx = 0;
  30874. ret = wc_Curve25519PrivateKeyDecode(kCurve25519PrivDer, &idx, &key,
  30875. (word32)sizeof(kCurve25519PrivDer));
  30876. if (ret < 0)
  30877. ret = WC_TEST_RET_ENC_EC(ret);
  30878. }
  30879. if (ret == 0) {
  30880. outputSz = (word32)sizeof(output);
  30881. ret = wc_Curve25519PrivateKeyToDer(&key, output, outputSz);
  30882. if (ret >= 0) {
  30883. outputSz = (word32)ret;
  30884. ret = 0;
  30885. }
  30886. else {
  30887. ret = WC_TEST_RET_ENC_EC(ret);
  30888. }
  30889. }
  30890. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PrivDer) ||
  30891. XMEMCMP(output, kCurve25519PrivDer, outputSz) != 0)) {
  30892. ret = WC_TEST_RET_ENC_NC;
  30893. }
  30894. /* Test decode / encode of Curve25519 public key only */
  30895. if (ret == 0) {
  30896. idx = 0;
  30897. ret = wc_Curve25519PublicKeyDecode(kCurve25519PubDer, &idx, &key,
  30898. (word32)sizeof(kCurve25519PubDer));
  30899. if (ret < 0)
  30900. ret = WC_TEST_RET_ENC_EC(ret);
  30901. }
  30902. if (ret == 0) {
  30903. outputSz = (word32)sizeof(output);
  30904. ret = wc_Curve25519PublicKeyToDer(&key, output, outputSz, 1);
  30905. if (ret >= 0) {
  30906. outputSz = (word32)ret;
  30907. ret = 0;
  30908. }
  30909. else {
  30910. ret = WC_TEST_RET_ENC_EC(ret);
  30911. }
  30912. }
  30913. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PubDer) ||
  30914. XMEMCMP(output, kCurve25519PubDer, outputSz) != 0)) {
  30915. ret = WC_TEST_RET_ENC_NC;
  30916. }
  30917. wc_curve25519_free(&key);
  30918. return ret;
  30919. }
  30920. #endif /* !NO_ASN && HAVE_CURVE25519_KEY_EXPORT && HAVE_CURVE25519_KEY_IMPORT */
  30921. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve25519_test(void)
  30922. {
  30923. WC_RNG rng;
  30924. wc_test_ret_t ret;
  30925. #ifdef HAVE_CURVE25519_SHARED_SECRET
  30926. byte sharedA[32];
  30927. byte sharedB[32];
  30928. word32 y;
  30929. #endif
  30930. #ifdef HAVE_CURVE25519_KEY_EXPORT
  30931. byte exportBuf[32];
  30932. #endif
  30933. word32 x = 0;
  30934. curve25519_key userA, userB, pubKey;
  30935. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  30936. defined(HAVE_CURVE25519_KEY_IMPORT)
  30937. /* test vectors from
  30938. https://tools.ietf.org/html/draft-josefsson-tls-curve25519-03
  30939. */
  30940. /* secret key for party a */
  30941. byte sa[] = {
  30942. 0x5A,0xC9,0x9F,0x33,0x63,0x2E,0x5A,0x76,
  30943. 0x8D,0xE7,0xE8,0x1B,0xF8,0x54,0xC2,0x7C,
  30944. 0x46,0xE3,0xFB,0xF2,0xAB,0xBA,0xCD,0x29,
  30945. 0xEC,0x4A,0xFF,0x51,0x73,0x69,0xC6,0x60
  30946. };
  30947. /* public key for party a */
  30948. byte pa[] = {
  30949. 0x05,0x7E,0x23,0xEA,0x9F,0x1C,0xBE,0x8A,
  30950. 0x27,0x16,0x8F,0x6E,0x69,0x6A,0x79,0x1D,
  30951. 0xE6,0x1D,0xD3,0xAF,0x7A,0xCD,0x4E,0xEA,
  30952. 0xCC,0x6E,0x7B,0xA5,0x14,0xFD,0xA8,0x63
  30953. };
  30954. /* secret key for party b */
  30955. byte sb[] = {
  30956. 0x47,0xDC,0x3D,0x21,0x41,0x74,0x82,0x0E,
  30957. 0x11,0x54,0xB4,0x9B,0xC6,0xCD,0xB2,0xAB,
  30958. 0xD4,0x5E,0xE9,0x58,0x17,0x05,0x5D,0x25,
  30959. 0x5A,0xA3,0x58,0x31,0xB7,0x0D,0x32,0x60
  30960. };
  30961. /* public key for party b */
  30962. byte pb[] = {
  30963. 0x6E,0xB8,0x9D,0xA9,0x19,0x89,0xAE,0x37,
  30964. 0xC7,0xEA,0xC7,0x61,0x8D,0x9E,0x5C,0x49,
  30965. 0x51,0xDB,0xA1,0xD7,0x3C,0x28,0x5A,0xE1,
  30966. 0xCD,0x26,0xA8,0x55,0x02,0x0E,0xEF,0x04
  30967. };
  30968. /* expected shared key */
  30969. byte ss[] = {
  30970. 0x61,0x45,0x0C,0xD9,0x8E,0x36,0x01,0x6B,
  30971. 0x58,0x77,0x6A,0x89,0x7A,0x9F,0x0A,0xEF,
  30972. 0x73,0x8B,0x99,0xF0,0x94,0x68,0xB8,0xD6,
  30973. 0xB8,0x51,0x11,0x84,0xD5,0x34,0x94,0xAB
  30974. };
  30975. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  30976. (void)x;
  30977. WOLFSSL_ENTER("curve25519_test");
  30978. #ifndef HAVE_FIPS
  30979. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  30980. #else
  30981. ret = wc_InitRng(&rng);
  30982. #endif
  30983. if (ret != 0)
  30984. return WC_TEST_RET_ENC_EC(ret);
  30985. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  30986. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  30987. wc_curve25519_init_ex(&pubKey, HEAP_HINT, devId);
  30988. /* make curve25519 keys */
  30989. ret = wc_curve25519_make_key(&rng, 32, &userA);
  30990. if (ret != 0)
  30991. return WC_TEST_RET_ENC_EC(ret);
  30992. ret = wc_curve25519_make_key(&rng, 32, &userB);
  30993. if (ret != 0)
  30994. return WC_TEST_RET_ENC_EC(ret);
  30995. #ifdef HAVE_CURVE25519_SHARED_SECRET
  30996. /* find shared secret key */
  30997. x = sizeof(sharedA);
  30998. if ((ret = wc_curve25519_shared_secret(&userA, &userB, sharedA, &x)) != 0) {
  30999. printf("wc_curve25519_shared_secret 1 failed\n");
  31000. return WC_TEST_RET_ENC_EC(ret);
  31001. }
  31002. y = sizeof(sharedB);
  31003. if ((ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y)) != 0) {
  31004. printf("wc_curve25519_shared_secret 2 failed\n");
  31005. return WC_TEST_RET_ENC_EC(ret);
  31006. }
  31007. /* compare shared secret keys to test they are the same */
  31008. if (y != x)
  31009. return WC_TEST_RET_ENC_NC;
  31010. if (XMEMCMP(sharedA, sharedB, x))
  31011. return WC_TEST_RET_ENC_NC;
  31012. #endif
  31013. #ifdef HAVE_CURVE25519_KEY_EXPORT
  31014. /* export a public key and import it for another user */
  31015. x = sizeof(exportBuf);
  31016. ret = wc_curve25519_export_public(&userA, exportBuf, &x);
  31017. if (ret != 0)
  31018. return WC_TEST_RET_ENC_EC(ret);
  31019. #ifdef HAVE_CURVE25519_KEY_IMPORT
  31020. ret = wc_curve25519_import_public(exportBuf, x, &pubKey);
  31021. if (ret != 0)
  31022. return WC_TEST_RET_ENC_EC(ret);
  31023. #endif
  31024. #endif
  31025. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  31026. defined(HAVE_CURVE25519_KEY_IMPORT)
  31027. /* test shared key after importing a public key */
  31028. XMEMSET(sharedB, 0, sizeof(sharedB));
  31029. y = sizeof(sharedB);
  31030. if (wc_curve25519_shared_secret(&userB, &pubKey, sharedB, &y) != 0) {
  31031. return WC_TEST_RET_ENC_NC;
  31032. }
  31033. if (XMEMCMP(sharedA, sharedB, y))
  31034. return WC_TEST_RET_ENC_NC;
  31035. /* import RFC test vectors and compare shared key */
  31036. ret = wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa),
  31037. &userA);
  31038. if (ret != 0)
  31039. return WC_TEST_RET_ENC_EC(ret);
  31040. ret = wc_curve25519_import_private_raw(sb, sizeof(sb), pb, sizeof(pb),
  31041. &userB);
  31042. if (ret != 0)
  31043. return WC_TEST_RET_ENC_EC(ret);
  31044. /* test against known test vector */
  31045. XMEMSET(sharedB, 0, sizeof(sharedB));
  31046. y = sizeof(sharedB);
  31047. ret = wc_curve25519_shared_secret(&userA, &userB, sharedB, &y);
  31048. if (ret != 0)
  31049. return WC_TEST_RET_ENC_EC(ret);
  31050. if (XMEMCMP(ss, sharedB, y))
  31051. return WC_TEST_RET_ENC_NC;
  31052. /* test swapping roles of keys and generating same shared key */
  31053. XMEMSET(sharedB, 0, sizeof(sharedB));
  31054. y = sizeof(sharedB);
  31055. ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y);
  31056. if (ret != 0)
  31057. return WC_TEST_RET_ENC_EC(ret);
  31058. if (XMEMCMP(ss, sharedB, y))
  31059. return WC_TEST_RET_ENC_NC;
  31060. /* test with 1 generated key and 1 from known test vector */
  31061. ret = wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa),
  31062. &userA);
  31063. if (ret != 0)
  31064. return WC_TEST_RET_ENC_EC(ret);
  31065. wc_curve25519_free(&userB);
  31066. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  31067. ret = wc_curve25519_make_key(&rng, 32, &userB);
  31068. if (ret != 0)
  31069. return WC_TEST_RET_ENC_EC(ret);
  31070. x = sizeof(sharedA);
  31071. ret = wc_curve25519_shared_secret(&userA, &userB, sharedA, &x);
  31072. if (ret != 0)
  31073. return WC_TEST_RET_ENC_EC(ret);
  31074. y = sizeof(sharedB);
  31075. ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y);
  31076. if (ret != 0)
  31077. return WC_TEST_RET_ENC_EC(ret);
  31078. /* compare shared secret keys to test they are the same */
  31079. if (y != x)
  31080. return WC_TEST_RET_ENC_NC;
  31081. if (XMEMCMP(sharedA, sharedB, x))
  31082. return WC_TEST_RET_ENC_NC;
  31083. ret = curve25519_overflow_test();
  31084. if (ret != 0)
  31085. return ret;
  31086. ret = curve25519_check_public_test();
  31087. if (ret != 0)
  31088. return ret;
  31089. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  31090. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  31091. defined(HAVE_CURVE25519_KEY_IMPORT)
  31092. ret = curve255519_der_test();
  31093. if (ret != 0)
  31094. return ret;
  31095. #endif
  31096. /* clean up keys when done */
  31097. wc_curve25519_free(&pubKey);
  31098. wc_curve25519_free(&userB);
  31099. wc_curve25519_free(&userA);
  31100. wc_FreeRng(&rng);
  31101. return 0;
  31102. }
  31103. #endif /* HAVE_CURVE25519 */
  31104. #ifdef HAVE_ED25519
  31105. #ifdef WOLFSSL_TEST_CERT
  31106. static wc_test_ret_t ed25519_test_cert(void)
  31107. {
  31108. DecodedCert cert[2];
  31109. DecodedCert* serverCert = NULL;
  31110. DecodedCert* caCert = NULL;
  31111. #ifdef HAVE_ED25519_VERIFY
  31112. ed25519_key key;
  31113. ed25519_key* pubKey = NULL;
  31114. int verify;
  31115. #endif /* HAVE_ED25519_VERIFY */
  31116. wc_test_ret_t ret;
  31117. byte* tmp;
  31118. size_t bytes;
  31119. XFILE file;
  31120. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31121. if (tmp == NULL) {
  31122. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  31123. }
  31124. #ifdef USE_CERT_BUFFERS_256
  31125. XMEMCPY(tmp, ca_ed25519_cert, sizeof_ca_ed25519_cert);
  31126. bytes = sizeof_ca_ed25519_cert;
  31127. #elif !defined(NO_FILESYSTEM)
  31128. file = XFOPEN(caEd25519Cert, "rb");
  31129. if (file == NULL) {
  31130. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  31131. }
  31132. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  31133. XFCLOSE(file);
  31134. if (bytes == 0)
  31135. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  31136. #else
  31137. /* No certificate to use. */
  31138. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  31139. #endif
  31140. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  31141. caCert = &cert[0];
  31142. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  31143. if (ret != 0)
  31144. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  31145. #ifdef USE_CERT_BUFFERS_256
  31146. XMEMCPY(tmp, server_ed25519_cert, sizeof_server_ed25519_cert);
  31147. bytes = sizeof_server_ed25519_cert;
  31148. #elif !defined(NO_FILESYSTEM)
  31149. file = XFOPEN(serverEd25519Cert, "rb");
  31150. if (file == NULL) {
  31151. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  31152. }
  31153. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  31154. XFCLOSE(file);
  31155. if (bytes == 0)
  31156. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  31157. #else
  31158. /* No certificate to use. */
  31159. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  31160. #endif
  31161. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  31162. serverCert = &cert[1];
  31163. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  31164. if (ret != 0)
  31165. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  31166. #ifdef HAVE_ED25519_VERIFY
  31167. ret = wc_ed25519_init(&key);
  31168. if (ret < 0)
  31169. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  31170. pubKey = &key;
  31171. ret = wc_ed25519_import_public(caCert->publicKey, caCert->pubKeySize,
  31172. pubKey);
  31173. if (ret < 0)
  31174. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  31175. ret = wc_ed25519_verify_msg(serverCert->signature, serverCert->sigLength,
  31176. serverCert->source + serverCert->certBegin,
  31177. serverCert->sigIndex - serverCert->certBegin,
  31178. &verify, pubKey);
  31179. if (ret < 0 || verify != 1)
  31180. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  31181. #endif /* HAVE_ED25519_VERIFY */
  31182. done:
  31183. if (tmp != NULL)
  31184. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31185. #ifdef HAVE_ED25519_VERIFY
  31186. wc_ed25519_free(pubKey);
  31187. #endif /* HAVE_ED25519_VERIFY */
  31188. if (caCert != NULL)
  31189. FreeDecodedCert(caCert);
  31190. if (serverCert != NULL)
  31191. FreeDecodedCert(serverCert);
  31192. return ret;
  31193. }
  31194. static wc_test_ret_t ed25519_test_make_cert(void)
  31195. {
  31196. WC_RNG rng;
  31197. Cert cert;
  31198. DecodedCert decode;
  31199. ed25519_key key;
  31200. ed25519_key* privKey = NULL;
  31201. wc_test_ret_t ret = 0;
  31202. byte* tmp = NULL;
  31203. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  31204. #ifndef HAVE_FIPS
  31205. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  31206. #else
  31207. ret = wc_InitRng(&rng);
  31208. #endif
  31209. if (ret != 0)
  31210. return WC_TEST_RET_ENC_EC(ret);
  31211. wc_ed25519_init(&key);
  31212. privKey = &key;
  31213. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, privKey);
  31214. cert.daysValid = 365 * 2;
  31215. cert.selfSigned = 1;
  31216. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  31217. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  31218. cert.isCA = 0;
  31219. #ifdef WOLFSSL_CERT_EXT
  31220. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  31221. if (ret < 0)
  31222. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  31223. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  31224. if (ret < 0)
  31225. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  31226. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  31227. if (ret < 0)
  31228. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  31229. #endif
  31230. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31231. if (tmp == NULL) {
  31232. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  31233. }
  31234. cert.sigType = CTC_ED25519;
  31235. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED25519_TYPE, privKey, &rng);
  31236. if (ret < 0)
  31237. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  31238. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF,
  31239. ED25519_TYPE, privKey, &rng);
  31240. if (ret < 0)
  31241. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  31242. InitDecodedCert(&decode, tmp, (word32)ret, HEAP_HINT);
  31243. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  31244. FreeDecodedCert(&decode);
  31245. if (ret != 0)
  31246. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  31247. done:
  31248. if (tmp != NULL)
  31249. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31250. wc_ed25519_free(privKey);
  31251. wc_FreeRng(&rng);
  31252. return ret;
  31253. }
  31254. #endif /* WOLFSSL_TEST_CERT */
  31255. #if defined(HAVE_ED25519_KEY_IMPORT)
  31256. static wc_test_ret_t ed25519_test_check_key(void)
  31257. {
  31258. /* Fails to find x-ordinate from this y-ordinate. */
  31259. WOLFSSL_SMALL_STACK_STATIC const byte key_bad_y[] = {
  31260. 0x40,
  31261. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31262. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31263. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31264. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31265. };
  31266. /* Y-ordinate value larger than prime. */
  31267. WOLFSSL_SMALL_STACK_STATIC const byte key_bad_y_max[] = {
  31268. 0x40,
  31269. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31270. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31271. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31272. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  31273. };
  31274. /* Y-ordinate value equal to prime. */
  31275. WOLFSSL_SMALL_STACK_STATIC const byte key_bad_y_is_p[] = {
  31276. 0x40,
  31277. 0xed,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31278. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31279. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31280. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  31281. };
  31282. /* Y-ordinate value equal to prime - 1. */
  31283. WOLFSSL_SMALL_STACK_STATIC const byte key_y_is_p_minus_1[] = {
  31284. 0x40,
  31285. 0xec,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31286. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31287. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  31288. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  31289. };
  31290. ed25519_key key;
  31291. int ret;
  31292. int res = 0;
  31293. /* Initialize key for use. */
  31294. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  31295. if (ret != 0) {
  31296. return WC_TEST_RET_ENC_NC;
  31297. }
  31298. /* Load bad public key only and perform checks. */
  31299. ret = wc_ed25519_import_public(key_bad_y, ED25519_PUB_KEY_SIZE + 1, &key);
  31300. if (ret != PUBLIC_KEY_E) {
  31301. res = WC_TEST_RET_ENC_NC;
  31302. }
  31303. if (res == 0) {
  31304. /* Load bad public key only and perform checks. */
  31305. ret = wc_ed25519_import_public(key_bad_y_max, ED25519_PUB_KEY_SIZE + 1,
  31306. &key);
  31307. if (ret != PUBLIC_KEY_E) {
  31308. res = WC_TEST_RET_ENC_NC;
  31309. }
  31310. }
  31311. if (res == 0) {
  31312. /* Load bad public key only and perform checks. */
  31313. ret = wc_ed25519_import_public(key_bad_y_is_p, ED25519_PUB_KEY_SIZE + 1,
  31314. &key);
  31315. if (ret != PUBLIC_KEY_E) {
  31316. res = WC_TEST_RET_ENC_NC;
  31317. }
  31318. }
  31319. if (res == 0) {
  31320. /* Load good public key only and perform checks. */
  31321. ret = wc_ed25519_import_public(key_y_is_p_minus_1,
  31322. ED25519_PUB_KEY_SIZE + 1, &key);
  31323. if (ret != 0) {
  31324. res = WC_TEST_RET_ENC_NC;
  31325. }
  31326. }
  31327. /* Dispose of key. */
  31328. wc_ed25519_free(&key);
  31329. return res;
  31330. }
  31331. #endif
  31332. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  31333. defined(HAVE_ED25519_KEY_IMPORT)
  31334. static wc_test_ret_t ed25519ctx_test(void)
  31335. {
  31336. wc_test_ret_t ret;
  31337. byte out[ED25519_SIG_SIZE];
  31338. word32 outlen;
  31339. #ifdef HAVE_ED25519_VERIFY
  31340. int verify = 0;
  31341. #endif /* HAVE_ED25519_VERIFY */
  31342. ed25519_key key;
  31343. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  31344. 0x03,0x05,0x33,0x4e,0x38,0x1a,0xf7,0x8f,
  31345. 0x14,0x1c,0xb6,0x66,0xf6,0x19,0x9f,0x57,
  31346. 0xbc,0x34,0x95,0x33,0x5a,0x25,0x6a,0x95,
  31347. 0xbd,0x2a,0x55,0xbf,0x54,0x66,0x63,0xf6
  31348. };
  31349. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  31350. 0xdf,0xc9,0x42,0x5e,0x4f,0x96,0x8f,0x7f,
  31351. 0x0c,0x29,0xf0,0x25,0x9c,0xf5,0xf9,0xae,
  31352. 0xd6,0x85,0x1c,0x2b,0xb4,0xad,0x8b,0xfb,
  31353. 0x86,0x0c,0xfe,0xe0,0xab,0x24,0x82,0x92
  31354. };
  31355. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx1[] = {
  31356. 0x55,0xa4,0xcc,0x2f,0x70,0xa5,0x4e,0x04,
  31357. 0x28,0x8c,0x5f,0x4c,0xd1,0xe4,0x5a,0x7b,
  31358. 0xb5,0x20,0xb3,0x62,0x92,0x91,0x18,0x76,
  31359. 0xca,0xda,0x73,0x23,0x19,0x8d,0xd8,0x7a,
  31360. 0x8b,0x36,0x95,0x0b,0x95,0x13,0x00,0x22,
  31361. 0x90,0x7a,0x7f,0xb7,0xc4,0xe9,0xb2,0xd5,
  31362. 0xf6,0xcc,0xa6,0x85,0xa5,0x87,0xb4,0xb2,
  31363. 0x1f,0x4b,0x88,0x8e,0x4e,0x7e,0xdb,0x0d
  31364. };
  31365. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx2[] = {
  31366. 0xcc,0x5e,0x63,0xa2,0x7e,0x94,0xaf,0xd3,
  31367. 0x41,0x83,0x38,0xd2,0x48,0x6f,0xa9,0x2a,
  31368. 0xf9,0x91,0x7c,0x2d,0x98,0x9e,0x06,0xe5,
  31369. 0x02,0x77,0x72,0x1c,0x34,0x38,0x18,0xb4,
  31370. 0x21,0x96,0xbc,0x29,0x2e,0x68,0xf3,0x4d,
  31371. 0x85,0x9b,0xbe,0xad,0x17,0x9f,0x54,0x54,
  31372. 0x2d,0x4b,0x04,0xdc,0xfb,0xfa,0x4a,0x68,
  31373. 0x4e,0x39,0x50,0xfb,0x1c,0xcd,0x8d,0x0d
  31374. };
  31375. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  31376. 0xf7,0x26,0x93,0x6d,0x19,0xc8,0x00,0x49,
  31377. 0x4e,0x3f,0xda,0xff,0x20,0xb2,0x76,0xa8
  31378. };
  31379. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  31380. 0x66,0x6f,0x6f
  31381. };
  31382. outlen = sizeof(out);
  31383. XMEMSET(out, 0, sizeof(out));
  31384. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  31385. if (ret != 0)
  31386. return 10800;
  31387. ret = wc_ed25519_import_private_key(sKeyCtx, ED25519_KEY_SIZE, pKeyCtx,
  31388. sizeof(pKeyCtx), &key);
  31389. if (ret == 0)
  31390. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  31391. contextCtx, sizeof(contextCtx));
  31392. if (ret == 0 && XMEMCMP(out, sigCtx1, 64) != 0)
  31393. ret = WC_TEST_RET_ENC_NC;
  31394. #if defined(HAVE_ED25519_VERIFY)
  31395. /* test verify on good msg */
  31396. if (ret == 0)
  31397. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  31398. &verify, &key, contextCtx, sizeof(contextCtx));
  31399. if (ret == 0 && verify != 1)
  31400. ret = WC_TEST_RET_ENC_NC;
  31401. #endif
  31402. if (ret == 0)
  31403. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  31404. NULL, 0);
  31405. if (ret == 0 && XMEMCMP(out, sigCtx2, 64) != 0)
  31406. ret = WC_TEST_RET_ENC_NC;
  31407. #if defined(HAVE_ED25519_VERIFY)
  31408. /* test verify on good msg */
  31409. if (ret == 0)
  31410. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  31411. &verify, &key, NULL, 0);
  31412. if (ret == 0 && verify != 1)
  31413. ret = WC_TEST_RET_ENC_NC;
  31414. #endif
  31415. wc_ed25519_free(&key);
  31416. return ret;
  31417. }
  31418. static wc_test_ret_t ed25519ph_test(void)
  31419. {
  31420. wc_test_ret_t ret = 0;
  31421. byte out[ED25519_SIG_SIZE];
  31422. word32 outlen;
  31423. #ifdef HAVE_ED25519_VERIFY
  31424. int verify = 0;
  31425. #endif /* HAVE_ED25519_VERIFY */
  31426. ed25519_key key;
  31427. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  31428. 0x83,0x3f,0xe6,0x24,0x09,0x23,0x7b,0x9d,
  31429. 0x62,0xec,0x77,0x58,0x75,0x20,0x91,0x1e,
  31430. 0x9a,0x75,0x9c,0xec,0x1d,0x19,0x75,0x5b,
  31431. 0x7d,0xa9,0x01,0xb9,0x6d,0xca,0x3d,0x42
  31432. };
  31433. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  31434. 0xec,0x17,0x2b,0x93,0xad,0x5e,0x56,0x3b,
  31435. 0xf4,0x93,0x2c,0x70,0xe1,0x24,0x50,0x34,
  31436. 0xc3,0x54,0x67,0xef,0x2e,0xfd,0x4d,0x64,
  31437. 0xeb,0xf8,0x19,0x68,0x34,0x67,0xe2,0xbf
  31438. };
  31439. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  31440. 0x98,0xa7,0x02,0x22,0xf0,0xb8,0x12,0x1a,
  31441. 0xa9,0xd3,0x0f,0x81,0x3d,0x68,0x3f,0x80,
  31442. 0x9e,0x46,0x2b,0x46,0x9c,0x7f,0xf8,0x76,
  31443. 0x39,0x49,0x9b,0xb9,0x4e,0x6d,0xae,0x41,
  31444. 0x31,0xf8,0x50,0x42,0x46,0x3c,0x2a,0x35,
  31445. 0x5a,0x20,0x03,0xd0,0x62,0xad,0xf5,0xaa,
  31446. 0xa1,0x0b,0x8c,0x61,0xe6,0x36,0x06,0x2a,
  31447. 0xaa,0xd1,0x1c,0x2a,0x26,0x08,0x34,0x06
  31448. };
  31449. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  31450. 0xe0,0x39,0x70,0x2b,0x4c,0x25,0x95,0xa6,
  31451. 0xa5,0x41,0xac,0x85,0x09,0x23,0x6e,0x29,
  31452. 0x90,0x47,0x47,0x95,0x33,0x0c,0x9b,0x34,
  31453. 0xa7,0x5f,0x58,0xa6,0x60,0x12,0x9e,0x08,
  31454. 0xfd,0x73,0x69,0x43,0xfb,0x19,0x43,0xa5,
  31455. 0x57,0x20,0xb9,0xe0,0x95,0x7b,0x1e,0xd6,
  31456. 0x73,0x48,0x16,0x61,0x9f,0x13,0x88,0xf4,
  31457. 0x3f,0x73,0xe6,0xe3,0xba,0xa8,0x1c,0x0e
  31458. };
  31459. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  31460. 0x61,0x62,0x63
  31461. };
  31462. /* SHA-512 hash of msgPh */
  31463. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  31464. 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
  31465. 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
  31466. 0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,
  31467. 0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
  31468. 0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,
  31469. 0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
  31470. 0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,
  31471. 0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f
  31472. };
  31473. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  31474. 0x66,0x6f,0x6f
  31475. };
  31476. outlen = sizeof(out);
  31477. XMEMSET(out, 0, sizeof(out));
  31478. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  31479. if (ret != 0)
  31480. return WC_TEST_RET_ENC_EC(ret);
  31481. ret = wc_ed25519_import_private_key(sKeyPh, ED25519_KEY_SIZE, pKeyPh,
  31482. sizeof(pKeyPh), &key);
  31483. if (ret == 0)
  31484. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  31485. NULL, 0);
  31486. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  31487. ret = WC_TEST_RET_ENC_NC;
  31488. #if defined(HAVE_ED25519_VERIFY)
  31489. /* test verify on good msg */
  31490. if (ret == 0)
  31491. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh),
  31492. &verify, &key, NULL, 0);
  31493. if (ret == 0 && verify != 1)
  31494. ret = WC_TEST_RET_ENC_NC;
  31495. #endif
  31496. if (ret == 0)
  31497. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  31498. contextPh2, sizeof(contextPh2));
  31499. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  31500. ret = WC_TEST_RET_ENC_NC;
  31501. #if defined(HAVE_ED25519_VERIFY)
  31502. /* test verify on good msg */
  31503. if (ret == 0)
  31504. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify,
  31505. &key, contextPh2, sizeof(contextPh2));
  31506. if (ret == 0 && verify != 1)
  31507. ret = WC_TEST_RET_ENC_NC;
  31508. #endif
  31509. if (ret == 0)
  31510. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  31511. NULL, 0);
  31512. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  31513. ret = WC_TEST_RET_ENC_NC;
  31514. #if defined(HAVE_ED25519_VERIFY)
  31515. if (ret == 0)
  31516. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh),
  31517. &verify, &key, NULL, 0);
  31518. if (ret == 0 && verify != 1)
  31519. ret = WC_TEST_RET_ENC_NC;
  31520. #endif
  31521. if (ret == 0)
  31522. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  31523. contextPh2, sizeof(contextPh2));
  31524. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  31525. ret = WC_TEST_RET_ENC_NC;
  31526. #if defined(HAVE_ED25519_VERIFY)
  31527. if (ret == 0)
  31528. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  31529. &key, contextPh2, sizeof(contextPh2));
  31530. if (ret == 0 && verify != 1)
  31531. ret = WC_TEST_RET_ENC_NC;
  31532. #endif
  31533. wc_ed25519_free(&key);
  31534. return ret;
  31535. }
  31536. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  31537. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed25519_test(void)
  31538. {
  31539. wc_test_ret_t ret;
  31540. WC_RNG rng;
  31541. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  31542. defined(HAVE_ED25519_KEY_IMPORT)
  31543. byte out[ED25519_SIG_SIZE];
  31544. byte exportPKey[ED25519_KEY_SIZE];
  31545. byte exportSKey[ED25519_KEY_SIZE];
  31546. word32 exportPSz;
  31547. word32 exportSSz;
  31548. int i;
  31549. word32 outlen;
  31550. #ifdef HAVE_ED25519_VERIFY
  31551. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  31552. int j;
  31553. #endif
  31554. int verify;
  31555. #endif /* HAVE_ED25519_VERIFY */
  31556. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  31557. word32 keySz, sigSz;
  31558. ed25519_key key;
  31559. ed25519_key key2;
  31560. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  31561. defined(HAVE_ED25519_KEY_IMPORT)
  31562. /* test vectors from
  31563. https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-02
  31564. */
  31565. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  31566. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  31567. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  31568. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  31569. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  31570. };
  31571. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  31572. 0x4c,0xcd,0x08,0x9b,0x28,0xff,0x96,0xda,
  31573. 0x9d,0xb6,0xc3,0x46,0xec,0x11,0x4e,0x0f,
  31574. 0x5b,0x8a,0x31,0x9f,0x35,0xab,0xa6,0x24,
  31575. 0xda,0x8c,0xf6,0xed,0x4f,0xb8,0xa6,0xfb
  31576. };
  31577. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  31578. 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
  31579. 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
  31580. 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
  31581. 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
  31582. };
  31583. /* uncompressed test */
  31584. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  31585. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  31586. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  31587. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  31588. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  31589. };
  31590. /* compressed prefix test */
  31591. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  31592. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  31593. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  31594. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  31595. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  31596. };
  31597. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  31598. 0xf5,0xe5,0x76,0x7c,0xf1,0x53,0x31,0x95,
  31599. 0x17,0x63,0x0f,0x22,0x68,0x76,0xb8,0x6c,
  31600. 0x81,0x60,0xcc,0x58,0x3b,0xc0,0x13,0x74,
  31601. 0x4c,0x6b,0xf2,0x55,0xf5,0xcc,0x0e,0xe5
  31602. };
  31603. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  31604. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  31605. 0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  31606. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  31607. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  31608. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  31609. };
  31610. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  31611. 0x3d,0x40,0x17,0xc3,0xe8,0x43,0x89,0x5a,
  31612. 0x92,0xb7,0x0a,0xa7,0x4d,0x1b,0x7e,0xbc,
  31613. 0x9c,0x98,0x2c,0xcf,0x2e,0xc4,0x96,0x8c,
  31614. 0xc0,0xcd,0x55,0xf1,0x2a,0xf4,0x66,0x0c
  31615. };
  31616. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  31617. 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
  31618. 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
  31619. 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
  31620. 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
  31621. };
  31622. /* uncompressed test */
  31623. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  31624. 0x04,0x55,0xd0,0xe0,0x9a,0x2b,0x9d,0x34,
  31625. 0x29,0x22,0x97,0xe0,0x8d,0x60,0xd0,0xf6,
  31626. 0x20,0xc5,0x13,0xd4,0x72,0x53,0x18,0x7c,
  31627. 0x24,0xb1,0x27,0x86,0xbd,0x77,0x76,0x45,
  31628. 0xce,0x1a,0x51,0x07,0xf7,0x68,0x1a,0x02,
  31629. 0xaf,0x25,0x23,0xa6,0xda,0xf3,0x72,0xe1,
  31630. 0x0e,0x3a,0x07,0x64,0xc9,0xd3,0xfe,0x4b,
  31631. 0xd5,0xb7,0x0a,0xb1,0x82,0x01,0x98,0x5a,
  31632. 0xd7
  31633. };
  31634. /* compressed prefix */
  31635. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  31636. 0x40,0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  31637. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  31638. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  31639. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  31640. };
  31641. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  31642. 0x27,0x81,0x17,0xfc,0x14,0x4c,0x72,0x34,
  31643. 0x0f,0x67,0xd0,0xf2,0x31,0x6e,0x83,0x86,
  31644. 0xce,0xff,0xbf,0x2b,0x24,0x28,0xc9,0xc5,
  31645. 0x1f,0xef,0x7c,0x59,0x7f,0x1d,0x42,0x6e
  31646. };
  31647. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  31648. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  31649. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  31650. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  31651. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  31652. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  31653. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  31654. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  31655. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  31656. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  31657. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  31658. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  31659. };
  31660. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  31661. 0x92,0xa0,0x09,0xa9,0xf0,0xd4,0xca,0xb8,
  31662. 0x72,0x0e,0x82,0x0b,0x5f,0x64,0x25,0x40,
  31663. 0xa2,0xb2,0x7b,0x54,0x16,0x50,0x3f,0x8f,
  31664. 0xb3,0x76,0x22,0x23,0xeb,0xdb,0x69,0xda,
  31665. 0x08,0x5a,0xc1,0xe4,0x3e,0x15,0x99,0x6e,
  31666. 0x45,0x8f,0x36,0x13,0xd0,0xf1,0x1d,0x8c,
  31667. 0x38,0x7b,0x2e,0xae,0xb4,0x30,0x2a,0xee,
  31668. 0xb0,0x0d,0x29,0x16,0x12,0xbb,0x0c,0x00
  31669. };
  31670. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  31671. 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
  31672. 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
  31673. 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
  31674. 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
  31675. 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
  31676. 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
  31677. 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
  31678. 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
  31679. };
  31680. /* uncompressed test */
  31681. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  31682. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  31683. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  31684. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  31685. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  31686. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  31687. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  31688. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  31689. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  31690. };
  31691. /* compressed prefix */
  31692. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  31693. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  31694. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  31695. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  31696. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  31697. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  31698. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  31699. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  31700. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  31701. };
  31702. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  31703. 0x0a,0xab,0x4c,0x90,0x05,0x01,0xb3,0xe2,
  31704. 0x4d,0x7c,0xdf,0x46,0x63,0x32,0x6a,0x3a,
  31705. 0x87,0xdf,0x5e,0x48,0x43,0xb2,0xcb,0xdb,
  31706. 0x67,0xcb,0xf6,0xe4,0x60,0xfe,0xc3,0x50,
  31707. 0xaa,0x53,0x71,0xb1,0x50,0x8f,0x9f,0x45,
  31708. 0x28,0xec,0xea,0x23,0xc4,0x36,0xd9,0x4b,
  31709. 0x5e,0x8f,0xcd,0x4f,0x68,0x1e,0x30,0xa6,
  31710. 0xac,0x00,0xa9,0x70,0x4a,0x18,0x8a,0x03
  31711. };
  31712. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  31713. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = {0x0 };
  31714. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = {0x72};
  31715. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = {0xAF,0x82};
  31716. /* test of a 1024 byte long message */
  31717. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  31718. 0x08,0xb8,0xb2,0xb7,0x33,0x42,0x42,0x43,
  31719. 0x76,0x0f,0xe4,0x26,0xa4,0xb5,0x49,0x08,
  31720. 0x63,0x21,0x10,0xa6,0x6c,0x2f,0x65,0x91,
  31721. 0xea,0xbd,0x33,0x45,0xe3,0xe4,0xeb,0x98,
  31722. 0xfa,0x6e,0x26,0x4b,0xf0,0x9e,0xfe,0x12,
  31723. 0xee,0x50,0xf8,0xf5,0x4e,0x9f,0x77,0xb1,
  31724. 0xe3,0x55,0xf6,0xc5,0x05,0x44,0xe2,0x3f,
  31725. 0xb1,0x43,0x3d,0xdf,0x73,0xbe,0x84,0xd8,
  31726. 0x79,0xde,0x7c,0x00,0x46,0xdc,0x49,0x96,
  31727. 0xd9,0xe7,0x73,0xf4,0xbc,0x9e,0xfe,0x57,
  31728. 0x38,0x82,0x9a,0xdb,0x26,0xc8,0x1b,0x37,
  31729. 0xc9,0x3a,0x1b,0x27,0x0b,0x20,0x32,0x9d,
  31730. 0x65,0x86,0x75,0xfc,0x6e,0xa5,0x34,0xe0,
  31731. 0x81,0x0a,0x44,0x32,0x82,0x6b,0xf5,0x8c,
  31732. 0x94,0x1e,0xfb,0x65,0xd5,0x7a,0x33,0x8b,
  31733. 0xbd,0x2e,0x26,0x64,0x0f,0x89,0xff,0xbc,
  31734. 0x1a,0x85,0x8e,0xfc,0xb8,0x55,0x0e,0xe3,
  31735. 0xa5,0xe1,0x99,0x8b,0xd1,0x77,0xe9,0x3a,
  31736. 0x73,0x63,0xc3,0x44,0xfe,0x6b,0x19,0x9e,
  31737. 0xe5,0xd0,0x2e,0x82,0xd5,0x22,0xc4,0xfe,
  31738. 0xba,0x15,0x45,0x2f,0x80,0x28,0x8a,0x82,
  31739. 0x1a,0x57,0x91,0x16,0xec,0x6d,0xad,0x2b,
  31740. 0x3b,0x31,0x0d,0xa9,0x03,0x40,0x1a,0xa6,
  31741. 0x21,0x00,0xab,0x5d,0x1a,0x36,0x55,0x3e,
  31742. 0x06,0x20,0x3b,0x33,0x89,0x0c,0xc9,0xb8,
  31743. 0x32,0xf7,0x9e,0xf8,0x05,0x60,0xcc,0xb9,
  31744. 0xa3,0x9c,0xe7,0x67,0x96,0x7e,0xd6,0x28,
  31745. 0xc6,0xad,0x57,0x3c,0xb1,0x16,0xdb,0xef,
  31746. 0xef,0xd7,0x54,0x99,0xda,0x96,0xbd,0x68,
  31747. 0xa8,0xa9,0x7b,0x92,0x8a,0x8b,0xbc,0x10,
  31748. 0x3b,0x66,0x21,0xfc,0xde,0x2b,0xec,0xa1,
  31749. 0x23,0x1d,0x20,0x6b,0xe6,0xcd,0x9e,0xc7,
  31750. 0xaf,0xf6,0xf6,0xc9,0x4f,0xcd,0x72,0x04,
  31751. 0xed,0x34,0x55,0xc6,0x8c,0x83,0xf4,0xa4,
  31752. 0x1d,0xa4,0xaf,0x2b,0x74,0xef,0x5c,0x53,
  31753. 0xf1,0xd8,0xac,0x70,0xbd,0xcb,0x7e,0xd1,
  31754. 0x85,0xce,0x81,0xbd,0x84,0x35,0x9d,0x44,
  31755. 0x25,0x4d,0x95,0x62,0x9e,0x98,0x55,0xa9,
  31756. 0x4a,0x7c,0x19,0x58,0xd1,0xf8,0xad,0xa5,
  31757. 0xd0,0x53,0x2e,0xd8,0xa5,0xaa,0x3f,0xb2,
  31758. 0xd1,0x7b,0xa7,0x0e,0xb6,0x24,0x8e,0x59,
  31759. 0x4e,0x1a,0x22,0x97,0xac,0xbb,0xb3,0x9d,
  31760. 0x50,0x2f,0x1a,0x8c,0x6e,0xb6,0xf1,0xce,
  31761. 0x22,0xb3,0xde,0x1a,0x1f,0x40,0xcc,0x24,
  31762. 0x55,0x41,0x19,0xa8,0x31,0xa9,0xaa,0xd6,
  31763. 0x07,0x9c,0xad,0x88,0x42,0x5d,0xe6,0xbd,
  31764. 0xe1,0xa9,0x18,0x7e,0xbb,0x60,0x92,0xcf,
  31765. 0x67,0xbf,0x2b,0x13,0xfd,0x65,0xf2,0x70,
  31766. 0x88,0xd7,0x8b,0x7e,0x88,0x3c,0x87,0x59,
  31767. 0xd2,0xc4,0xf5,0xc6,0x5a,0xdb,0x75,0x53,
  31768. 0x87,0x8a,0xd5,0x75,0xf9,0xfa,0xd8,0x78,
  31769. 0xe8,0x0a,0x0c,0x9b,0xa6,0x3b,0xcb,0xcc,
  31770. 0x27,0x32,0xe6,0x94,0x85,0xbb,0xc9,0xc9,
  31771. 0x0b,0xfb,0xd6,0x24,0x81,0xd9,0x08,0x9b,
  31772. 0xec,0xcf,0x80,0xcf,0xe2,0xdf,0x16,0xa2,
  31773. 0xcf,0x65,0xbd,0x92,0xdd,0x59,0x7b,0x07,
  31774. 0x07,0xe0,0x91,0x7a,0xf4,0x8b,0xbb,0x75,
  31775. 0xfe,0xd4,0x13,0xd2,0x38,0xf5,0x55,0x5a,
  31776. 0x7a,0x56,0x9d,0x80,0xc3,0x41,0x4a,0x8d,
  31777. 0x08,0x59,0xdc,0x65,0xa4,0x61,0x28,0xba,
  31778. 0xb2,0x7a,0xf8,0x7a,0x71,0x31,0x4f,0x31,
  31779. 0x8c,0x78,0x2b,0x23,0xeb,0xfe,0x80,0x8b,
  31780. 0x82,0xb0,0xce,0x26,0x40,0x1d,0x2e,0x22,
  31781. 0xf0,0x4d,0x83,0xd1,0x25,0x5d,0xc5,0x1a,
  31782. 0xdd,0xd3,0xb7,0x5a,0x2b,0x1a,0xe0,0x78,
  31783. 0x45,0x04,0xdf,0x54,0x3a,0xf8,0x96,0x9b,
  31784. 0xe3,0xea,0x70,0x82,0xff,0x7f,0xc9,0x88,
  31785. 0x8c,0x14,0x4d,0xa2,0xaf,0x58,0x42,0x9e,
  31786. 0xc9,0x60,0x31,0xdb,0xca,0xd3,0xda,0xd9,
  31787. 0xaf,0x0d,0xcb,0xaa,0xaf,0x26,0x8c,0xb8,
  31788. 0xfc,0xff,0xea,0xd9,0x4f,0x3c,0x7c,0xa4,
  31789. 0x95,0xe0,0x56,0xa9,0xb4,0x7a,0xcd,0xb7,
  31790. 0x51,0xfb,0x73,0xe6,0x66,0xc6,0xc6,0x55,
  31791. 0xad,0xe8,0x29,0x72,0x97,0xd0,0x7a,0xd1,
  31792. 0xba,0x5e,0x43,0xf1,0xbc,0xa3,0x23,0x01,
  31793. 0x65,0x13,0x39,0xe2,0x29,0x04,0xcc,0x8c,
  31794. 0x42,0xf5,0x8c,0x30,0xc0,0x4a,0xaf,0xdb,
  31795. 0x03,0x8d,0xda,0x08,0x47,0xdd,0x98,0x8d,
  31796. 0xcd,0xa6,0xf3,0xbf,0xd1,0x5c,0x4b,0x4c,
  31797. 0x45,0x25,0x00,0x4a,0xa0,0x6e,0xef,0xf8,
  31798. 0xca,0x61,0x78,0x3a,0xac,0xec,0x57,0xfb,
  31799. 0x3d,0x1f,0x92,0xb0,0xfe,0x2f,0xd1,0xa8,
  31800. 0x5f,0x67,0x24,0x51,0x7b,0x65,0xe6,0x14,
  31801. 0xad,0x68,0x08,0xd6,0xf6,0xee,0x34,0xdf,
  31802. 0xf7,0x31,0x0f,0xdc,0x82,0xae,0xbf,0xd9,
  31803. 0x04,0xb0,0x1e,0x1d,0xc5,0x4b,0x29,0x27,
  31804. 0x09,0x4b,0x2d,0xb6,0x8d,0x6f,0x90,0x3b,
  31805. 0x68,0x40,0x1a,0xde,0xbf,0x5a,0x7e,0x08,
  31806. 0xd7,0x8f,0xf4,0xef,0x5d,0x63,0x65,0x3a,
  31807. 0x65,0x04,0x0c,0xf9,0xbf,0xd4,0xac,0xa7,
  31808. 0x98,0x4a,0x74,0xd3,0x71,0x45,0x98,0x67,
  31809. 0x80,0xfc,0x0b,0x16,0xac,0x45,0x16,0x49,
  31810. 0xde,0x61,0x88,0xa7,0xdb,0xdf,0x19,0x1f,
  31811. 0x64,0xb5,0xfc,0x5e,0x2a,0xb4,0x7b,0x57,
  31812. 0xf7,0xf7,0x27,0x6c,0xd4,0x19,0xc1,0x7a,
  31813. 0x3c,0xa8,0xe1,0xb9,0x39,0xae,0x49,0xe4,
  31814. 0x88,0xac,0xba,0x6b,0x96,0x56,0x10,0xb5,
  31815. 0x48,0x01,0x09,0xc8,0xb1,0x7b,0x80,0xe1,
  31816. 0xb7,0xb7,0x50,0xdf,0xc7,0x59,0x8d,0x5d,
  31817. 0x50,0x11,0xfd,0x2d,0xcc,0x56,0x00,0xa3,
  31818. 0x2e,0xf5,0xb5,0x2a,0x1e,0xcc,0x82,0x0e,
  31819. 0x30,0x8a,0xa3,0x42,0x72,0x1a,0xac,0x09,
  31820. 0x43,0xbf,0x66,0x86,0xb6,0x4b,0x25,0x79,
  31821. 0x37,0x65,0x04,0xcc,0xc4,0x93,0xd9,0x7e,
  31822. 0x6a,0xed,0x3f,0xb0,0xf9,0xcd,0x71,0xa4,
  31823. 0x3d,0xd4,0x97,0xf0,0x1f,0x17,0xc0,0xe2,
  31824. 0xcb,0x37,0x97,0xaa,0x2a,0x2f,0x25,0x66,
  31825. 0x56,0x16,0x8e,0x6c,0x49,0x6a,0xfc,0x5f,
  31826. 0xb9,0x32,0x46,0xf6,0xb1,0x11,0x63,0x98,
  31827. 0xa3,0x46,0xf1,0xa6,0x41,0xf3,0xb0,0x41,
  31828. 0xe9,0x89,0xf7,0x91,0x4f,0x90,0xcc,0x2c,
  31829. 0x7f,0xff,0x35,0x78,0x76,0xe5,0x06,0xb5,
  31830. 0x0d,0x33,0x4b,0xa7,0x7c,0x22,0x5b,0xc3,
  31831. 0x07,0xba,0x53,0x71,0x52,0xf3,0xf1,0x61,
  31832. 0x0e,0x4e,0xaf,0xe5,0x95,0xf6,0xd9,0xd9,
  31833. 0x0d,0x11,0xfa,0xa9,0x33,0xa1,0x5e,0xf1,
  31834. 0x36,0x95,0x46,0x86,0x8a,0x7f,0x3a,0x45,
  31835. 0xa9,0x67,0x68,0xd4,0x0f,0xd9,0xd0,0x34,
  31836. 0x12,0xc0,0x91,0xc6,0x31,0x5c,0xf4,0xfd,
  31837. 0xe7,0xcb,0x68,0x60,0x69,0x37,0x38,0x0d,
  31838. 0xb2,0xea,0xaa,0x70,0x7b,0x4c,0x41,0x85,
  31839. 0xc3,0x2e,0xdd,0xcd,0xd3,0x06,0x70,0x5e,
  31840. 0x4d,0xc1,0xff,0xc8,0x72,0xee,0xee,0x47,
  31841. 0x5a,0x64,0xdf,0xac,0x86,0xab,0xa4,0x1c,
  31842. 0x06,0x18,0x98,0x3f,0x87,0x41,0xc5,0xef,
  31843. 0x68,0xd3,0xa1,0x01,0xe8,0xa3,0xb8,0xca,
  31844. 0xc6,0x0c,0x90,0x5c,0x15,0xfc,0x91,0x08,
  31845. 0x40,0xb9,0x4c,0x00,0xa0,0xb9,0xd0
  31846. };
  31847. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  31848. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  31849. sizeof(msg2),
  31850. sizeof(msg3),
  31851. 0 /*sizeof(msg1)*/,
  31852. 0 /*sizeof(msg1)*/,
  31853. sizeof(msg4)
  31854. };
  31855. #ifndef NO_ASN
  31856. static const byte privateEd25519[] = {
  31857. 0x30,0x2e,0x02,0x01,0x00,0x30,0x05,0x06,
  31858. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  31859. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  31860. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  31861. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  31862. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  31863. };
  31864. static const byte badPrivateEd25519[] = {
  31865. 0x30,0x52,0x02,0x01,0x00,0x30,0x05,0x06,
  31866. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  31867. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  31868. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  31869. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  31870. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  31871. 0xa1,0x22,0x04,0x21,0xd7,0x5a,0x98,0x01, /* octet len 0x20 -> 0x21 */
  31872. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  31873. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  31874. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  31875. 0xf7,0x07,0x51,0x1a,
  31876. 0x00 /* add additional bytes to make the pubkey bigger */
  31877. };
  31878. static const byte publicEd25519[] = {
  31879. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  31880. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  31881. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  31882. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  31883. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  31884. 0xf7,0x07,0x51,0x1a
  31885. };
  31886. /* size has been altered to catch if sanity check is done */
  31887. static const byte badPublicEd25519[] = {
  31888. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  31889. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  31890. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  31891. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  31892. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  31893. 0xf7,0x07,0x51,0x1a,
  31894. 0x00 /* add an additional byte to make the pubkey appear bigger */
  31895. };
  31896. static const byte privPubEd25519[] = {
  31897. 0x30,0x50,0x02,0x01,0x00,0x30,0x05,0x06,
  31898. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  31899. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  31900. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  31901. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  31902. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  31903. 0x81,0x20,0xd7,0x5a,0x98,0x01,0x82,0xb1,
  31904. 0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,0xc9,0x64,
  31905. 0x07,0x3a,0x0e,0xe1,0x72,0xf3,0xda,0xa6,
  31906. 0x23,0x25,0xaf,0x02,0x1a,0x68,0xf7,0x07,
  31907. 0x51,0x1a
  31908. };
  31909. word32 idx;
  31910. #endif /* NO_ASN */
  31911. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  31912. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  31913. ed25519_key key3;
  31914. #endif
  31915. WOLFSSL_ENTER("ed25519_test");
  31916. /* create ed25519 keys */
  31917. #ifndef HAVE_FIPS
  31918. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  31919. #else
  31920. ret = wc_InitRng(&rng);
  31921. #endif
  31922. if (ret != 0)
  31923. return WC_TEST_RET_ENC_EC(ret);
  31924. wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  31925. wc_ed25519_init_ex(&key2, HEAP_HINT, devId);
  31926. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  31927. wc_ed25519_init_ex(&key3, HEAP_HINT, devId);
  31928. #endif
  31929. #ifdef HAVE_ED25519_MAKE_KEY
  31930. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  31931. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key2);
  31932. #endif
  31933. /* helper functions for signature and key size */
  31934. keySz = (word32)wc_ed25519_size(&key);
  31935. sigSz = (word32)wc_ed25519_sig_size(&key);
  31936. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  31937. defined(HAVE_ED25519_KEY_IMPORT)
  31938. for (i = 0; i < 6; i++) {
  31939. outlen = sizeof(out);
  31940. XMEMSET(out, 0, sizeof(out));
  31941. if (wc_ed25519_import_private_key(sKeys[i], ED25519_KEY_SIZE, pKeys[i],
  31942. pKeySz[i], &key) != 0)
  31943. return WC_TEST_RET_ENC_I(i);
  31944. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key) != 0)
  31945. return WC_TEST_RET_ENC_I(i);
  31946. if (XMEMCMP(out, sigs[i], 64))
  31947. return WC_TEST_RET_ENC_I(i);
  31948. #if defined(HAVE_ED25519_VERIFY)
  31949. /* test verify on good msg */
  31950. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  31951. &key) != 0 || verify != 1)
  31952. return WC_TEST_RET_ENC_I(i);
  31953. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  31954. /* test verify on good msg using streaming interface directly */
  31955. if (wc_ed25519_verify_msg_init(out, outlen,
  31956. &key, (byte)Ed25519, NULL, 0) != 0)
  31957. return WC_TEST_RET_ENC_I(i);
  31958. for (j = 0; j < msgSz[i]; j += i) {
  31959. if (wc_ed25519_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), &key) != 0)
  31960. return WC_TEST_RET_ENC_I(i);
  31961. }
  31962. if (wc_ed25519_verify_msg_final(out, outlen, &verify,
  31963. &key) != 0 || verify != 1)
  31964. return WC_TEST_RET_ENC_I(i);
  31965. #endif /* WOLFSSL_ED25519_STREAMING_VERIFY */
  31966. /* test verify on bad msg */
  31967. out[outlen-1] = out[outlen-1] + 1;
  31968. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  31969. &key) == 0 || verify == 1)
  31970. return WC_TEST_RET_ENC_I(i);
  31971. #endif /* HAVE_ED25519_VERIFY */
  31972. /* test api for import/exporting keys */
  31973. exportPSz = sizeof(exportPKey);
  31974. exportSSz = sizeof(exportSKey);
  31975. if (wc_ed25519_export_public(&key, exportPKey, &exportPSz) != 0)
  31976. return WC_TEST_RET_ENC_I(i);
  31977. if (wc_ed25519_import_public_ex(exportPKey, exportPSz, &key2, 1) != 0)
  31978. return WC_TEST_RET_ENC_I(i);
  31979. if (wc_ed25519_export_private_only(&key, exportSKey, &exportSSz) != 0)
  31980. return WC_TEST_RET_ENC_I(i);
  31981. if (wc_ed25519_import_private_key(exportSKey, exportSSz,
  31982. exportPKey, exportPSz, &key2) != 0)
  31983. return WC_TEST_RET_ENC_I(i);
  31984. /* clear "out" buffer and test sign with imported keys */
  31985. outlen = sizeof(out);
  31986. XMEMSET(out, 0, sizeof(out));
  31987. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2) != 0)
  31988. return WC_TEST_RET_ENC_I(i);
  31989. #if defined(HAVE_ED25519_VERIFY)
  31990. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  31991. &key2) != 0 || verify != 1)
  31992. return WC_TEST_RET_ENC_I(i);
  31993. if (XMEMCMP(out, sigs[i], 64))
  31994. return WC_TEST_RET_ENC_I(i);
  31995. #endif /* HAVE_ED25519_VERIFY */
  31996. }
  31997. {
  31998. /* Run tests for some rare code paths */
  31999. /* sig is exactly equal to the order */
  32000. static const byte rareEd1[] = {
  32001. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32002. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32003. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32004. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32005. 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
  32006. 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
  32007. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32008. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10
  32009. };
  32010. /* sig is larger than the order before we get to the low part */
  32011. static const byte rareEd2[] = {
  32012. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32013. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32014. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32015. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32016. 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
  32017. 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
  32018. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32019. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x10
  32020. };
  32021. /* sig is larger than the order in the low part */
  32022. static const byte rareEd3[] = {
  32023. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32024. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32025. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32026. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32027. 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
  32028. 0xd6, 0x9c, 0xf9, 0xa2, 0xde, 0xf9, 0xde, 0x14,
  32029. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32030. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10
  32031. };
  32032. /* sig is smaller than the order */
  32033. static const byte rareEd4[] = {
  32034. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32035. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32036. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32037. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32038. 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
  32039. 0xd6, 0x9c, 0xf1, 0xa2, 0xde, 0xf9, 0xde, 0x14,
  32040. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32041. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10
  32042. };
  32043. ret = wc_ed25519_import_private_key(sKeys[0], ED25519_KEY_SIZE,
  32044. pKeys[0], pKeySz[0], &key);
  32045. if (ret != 0)
  32046. return ret;
  32047. ret = wc_ed25519_verify_msg(rareEd1, sizeof(rareEd1), msgs[0], msgSz[0],
  32048. &verify, &key);
  32049. if (ret != BAD_FUNC_ARG)
  32050. return ret;
  32051. ret = wc_ed25519_verify_msg(rareEd2, sizeof(rareEd2), msgs[0], msgSz[0],
  32052. &verify, &key);
  32053. if (ret != BAD_FUNC_ARG)
  32054. return ret;
  32055. ret = wc_ed25519_verify_msg(rareEd3, sizeof(rareEd3), msgs[0], msgSz[0],
  32056. &verify, &key);
  32057. if (ret != BAD_FUNC_ARG)
  32058. return ret;
  32059. ret = wc_ed25519_verify_msg(rareEd4, sizeof(rareEd4), msgs[0], msgSz[0],
  32060. &verify, &key);
  32061. if (ret != SIG_VERIFY_E)
  32062. return ret;
  32063. }
  32064. ret = ed25519ctx_test();
  32065. if (ret != 0)
  32066. return ret;
  32067. ret = ed25519ph_test();
  32068. if (ret != 0)
  32069. return ret;
  32070. #ifndef NO_ASN
  32071. /* Try ASN.1 encoded private-only key and public key. */
  32072. idx = 0;
  32073. ret = wc_Ed25519PrivateKeyDecode(privateEd25519, &idx, &key3,
  32074. sizeof(privateEd25519));
  32075. if (ret != 0)
  32076. return WC_TEST_RET_ENC_EC(ret);
  32077. idx = 0;
  32078. if (wc_Ed25519PrivateKeyDecode(badPrivateEd25519, &idx, &key3,
  32079. sizeof(badPrivateEd25519)) == 0)
  32080. return WC_TEST_RET_ENC_NC;
  32081. ret = wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3);
  32082. if (ret != BAD_FUNC_ARG)
  32083. return WC_TEST_RET_ENC_EC(ret);
  32084. /* try with a buffer size that is too large */
  32085. idx = 0;
  32086. if (wc_Ed25519PublicKeyDecode(badPublicEd25519, &idx, &key3,
  32087. sizeof(badPublicEd25519)) == 0)
  32088. return WC_TEST_RET_ENC_NC;
  32089. idx = 0;
  32090. ret = wc_Ed25519PublicKeyDecode(publicEd25519, &idx, &key3,
  32091. sizeof(publicEd25519));
  32092. if (ret != 0)
  32093. return WC_TEST_RET_ENC_EC(ret);
  32094. ret = wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3);
  32095. if (ret != 0)
  32096. return WC_TEST_RET_ENC_EC(ret);
  32097. if (XMEMCMP(out, sigs[0], 64))
  32098. return WC_TEST_RET_ENC_NC;
  32099. #if defined(HAVE_ED25519_VERIFY)
  32100. /* test verify on good msg */
  32101. ret = wc_ed25519_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3);
  32102. if (ret != 0 || verify != 1)
  32103. return WC_TEST_RET_ENC_EC(ret);
  32104. #endif /* HAVE_ED25519_VERIFY */
  32105. wc_ed25519_free(&key3);
  32106. wc_ed25519_init(&key3);
  32107. idx = 0;
  32108. ret = wc_Ed25519PrivateKeyDecode(privPubEd25519, &idx, &key3,
  32109. sizeof(privPubEd25519));
  32110. if (ret != 0)
  32111. return WC_TEST_RET_ENC_EC(ret);
  32112. ret = wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3);
  32113. if (ret != 0)
  32114. return WC_TEST_RET_ENC_EC(ret);
  32115. if (XMEMCMP(out, sigs[0], 64))
  32116. return WC_TEST_RET_ENC_NC;
  32117. wc_ed25519_free(&key3);
  32118. #endif /* NO_ASN */
  32119. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  32120. /* clean up keys when done */
  32121. wc_ed25519_free(&key);
  32122. wc_ed25519_free(&key2);
  32123. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  32124. wc_FreeRng(&rng);
  32125. #endif
  32126. /* hush warnings of unused keySz and sigSz */
  32127. (void)keySz;
  32128. (void)sigSz;
  32129. ret = ed25519_test_check_key();
  32130. if (ret < 0)
  32131. return ret;
  32132. #ifdef WOLFSSL_TEST_CERT
  32133. ret = ed25519_test_cert();
  32134. if (ret < 0)
  32135. return ret;
  32136. #if defined(WOLFSSL_CERT_GEN) && defined(HAVE_ED25519_MAKE_KEY)
  32137. ret = ed25519_test_make_cert();
  32138. if (ret < 0)
  32139. return ret;
  32140. #endif /* WOLFSSL_CERT_GEN */
  32141. #endif /* WOLFSSL_TEST_CERT */
  32142. return 0;
  32143. }
  32144. #endif /* HAVE_ED25519 */
  32145. #ifdef HAVE_CURVE448
  32146. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  32147. defined(HAVE_CURVE448_KEY_IMPORT)
  32148. /* Test the wc_curve448_check_public API.
  32149. *
  32150. * returns 0 on success and -ve on failure.
  32151. */
  32152. static wc_test_ret_t curve448_check_public_test(void)
  32153. {
  32154. /* Little-endian values that will fail */
  32155. byte fail_le[][CURVE448_KEY_SIZE] = {
  32156. {
  32157. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32158. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32159. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32160. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32161. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32162. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32163. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  32164. },
  32165. {
  32166. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32167. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32168. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32169. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32170. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32171. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32172. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  32173. },
  32174. };
  32175. /* Big-endian values that will fail */
  32176. byte fail_be[][CURVE448_KEY_SIZE] = {
  32177. {
  32178. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32179. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32180. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32181. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32182. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32183. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32184. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  32185. },
  32186. {
  32187. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32188. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32189. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32190. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32191. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32192. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32193. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  32194. },
  32195. };
  32196. /* Good or valid public value */
  32197. byte good[CURVE448_KEY_SIZE] = {
  32198. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32199. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32200. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32201. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32202. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32203. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32204. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  32205. };
  32206. int i;
  32207. wc_test_ret_t ret;
  32208. /* Parameter checks */
  32209. /* NULL pointer */
  32210. ret = wc_curve448_check_public(NULL, 0, EC448_LITTLE_ENDIAN);
  32211. if (ret != BAD_FUNC_ARG)
  32212. return WC_TEST_RET_ENC_EC(ret);
  32213. ret = wc_curve448_check_public(NULL, 0, EC448_BIG_ENDIAN);
  32214. if (ret != BAD_FUNC_ARG)
  32215. return WC_TEST_RET_ENC_EC(ret);
  32216. /* Length of 0 treated differently to other invalid lengths for TLS */
  32217. ret = wc_curve448_check_public(good, 0, EC448_LITTLE_ENDIAN);
  32218. if (ret != BUFFER_E)
  32219. return WC_TEST_RET_ENC_EC(ret);
  32220. ret = wc_curve448_check_public(good, 0, EC448_BIG_ENDIAN);
  32221. if (ret != BUFFER_E)
  32222. return WC_TEST_RET_ENC_EC(ret);
  32223. /* Length not CURVE448_KEY_SIZE */
  32224. for (i = 1; i < CURVE448_KEY_SIZE + 2; i++) {
  32225. if (i == CURVE448_KEY_SIZE)
  32226. continue;
  32227. if (wc_curve448_check_public(good, (word32)i, EC448_LITTLE_ENDIAN) !=
  32228. ECC_BAD_ARG_E) {
  32229. return WC_TEST_RET_ENC_I(i);
  32230. }
  32231. if (wc_curve448_check_public(good, (word32)i, EC448_BIG_ENDIAN) !=
  32232. ECC_BAD_ARG_E) {
  32233. return WC_TEST_RET_ENC_I(i);
  32234. }
  32235. }
  32236. /* Little-endian fail cases */
  32237. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  32238. if (wc_curve448_check_public(fail_le[i], CURVE448_KEY_SIZE,
  32239. EC448_LITTLE_ENDIAN) == 0) {
  32240. return WC_TEST_RET_ENC_I(i);
  32241. }
  32242. }
  32243. /* Big-endian fail cases */
  32244. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  32245. if (wc_curve448_check_public(fail_be[i], CURVE448_KEY_SIZE,
  32246. EC448_BIG_ENDIAN) == 0) {
  32247. return WC_TEST_RET_ENC_I(i);
  32248. }
  32249. }
  32250. /* Check a valid public value works! */
  32251. ret = wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  32252. EC448_LITTLE_ENDIAN);
  32253. if (ret != 0)
  32254. return WC_TEST_RET_ENC_EC(ret);
  32255. ret = wc_curve448_check_public(good, CURVE448_KEY_SIZE, EC448_BIG_ENDIAN);
  32256. if (ret != 0)
  32257. return WC_TEST_RET_ENC_EC(ret);
  32258. return 0;
  32259. }
  32260. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  32261. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve448_test(void)
  32262. {
  32263. WC_RNG rng;
  32264. wc_test_ret_t ret;
  32265. #ifdef HAVE_CURVE448_SHARED_SECRET
  32266. byte sharedA[CURVE448_KEY_SIZE];
  32267. byte sharedB[CURVE448_KEY_SIZE];
  32268. word32 y;
  32269. #endif
  32270. #ifdef HAVE_CURVE448_KEY_EXPORT
  32271. byte exportBuf[CURVE448_KEY_SIZE];
  32272. #endif
  32273. word32 x = 0;
  32274. curve448_key userA, userB, pubKey;
  32275. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  32276. defined(HAVE_CURVE448_KEY_IMPORT)
  32277. /* test vectors from
  32278. https://www.rfc-editor.org/rfc/rfc7748.html
  32279. */
  32280. /* secret key for party a */
  32281. byte sa[] = {
  32282. 0x6b, 0x72, 0x98, 0xa5, 0xc0, 0xd8, 0xc2, 0x9a,
  32283. 0x1d, 0xab, 0x27, 0xf1, 0xa6, 0x82, 0x63, 0x00,
  32284. 0x91, 0x73, 0x89, 0x44, 0x97, 0x41, 0xa9, 0x74,
  32285. 0xf5, 0xba, 0xc9, 0xd9, 0x8d, 0xc2, 0x98, 0xd4,
  32286. 0x65, 0x55, 0xbc, 0xe8, 0xba, 0xe8, 0x9e, 0xee,
  32287. 0xd4, 0x00, 0x58, 0x4b, 0xb0, 0x46, 0xcf, 0x75,
  32288. 0x57, 0x9f, 0x51, 0xd1, 0x25, 0x49, 0x8f, 0x9a,
  32289. };
  32290. /* public key for party a */
  32291. byte pa[] = {
  32292. 0xa0, 0x1f, 0xc4, 0x32, 0xe5, 0x80, 0x7f, 0x17,
  32293. 0x53, 0x0d, 0x12, 0x88, 0xda, 0x12, 0x5b, 0x0c,
  32294. 0xd4, 0x53, 0xd9, 0x41, 0x72, 0x64, 0x36, 0xc8,
  32295. 0xbb, 0xd9, 0xc5, 0x22, 0x2c, 0x3d, 0xa7, 0xfa,
  32296. 0x63, 0x9c, 0xe0, 0x3d, 0xb8, 0xd2, 0x3b, 0x27,
  32297. 0x4a, 0x07, 0x21, 0xa1, 0xae, 0xd5, 0x22, 0x7d,
  32298. 0xe6, 0xe3, 0xb7, 0x31, 0xcc, 0xf7, 0x08, 0x9b,
  32299. };
  32300. /* secret key for party b */
  32301. byte sb[] = {
  32302. 0x2d, 0x99, 0x73, 0x51, 0xb6, 0x10, 0x6f, 0x36,
  32303. 0xb0, 0xd1, 0x09, 0x1b, 0x92, 0x9c, 0x4c, 0x37,
  32304. 0x21, 0x3e, 0x0d, 0x2b, 0x97, 0xe8, 0x5e, 0xbb,
  32305. 0x20, 0xc1, 0x27, 0x69, 0x1d, 0x0d, 0xad, 0x8f,
  32306. 0x1d, 0x81, 0x75, 0xb0, 0x72, 0x37, 0x45, 0xe6,
  32307. 0x39, 0xa3, 0xcb, 0x70, 0x44, 0x29, 0x0b, 0x99,
  32308. 0xe0, 0xe2, 0xa0, 0xc2, 0x7a, 0x6a, 0x30, 0x1c,
  32309. };
  32310. /* public key for party b */
  32311. byte pb[] = {
  32312. 0x09, 0x36, 0xf3, 0x7b, 0xc6, 0xc1, 0xbd, 0x07,
  32313. 0xae, 0x3d, 0xec, 0x7a, 0xb5, 0xdc, 0x06, 0xa7,
  32314. 0x3c, 0xa1, 0x32, 0x42, 0xfb, 0x34, 0x3e, 0xfc,
  32315. 0x72, 0xb9, 0xd8, 0x27, 0x30, 0xb4, 0x45, 0xf3,
  32316. 0xd4, 0xb0, 0xbd, 0x07, 0x71, 0x62, 0xa4, 0x6d,
  32317. 0xcf, 0xec, 0x6f, 0x9b, 0x59, 0x0b, 0xfc, 0xbc,
  32318. 0xf5, 0x20, 0xcd, 0xb0, 0x29, 0xa8, 0xb7, 0x3e,
  32319. };
  32320. /* expected shared key */
  32321. byte ss[] = {
  32322. 0x9d, 0x87, 0x4a, 0x51, 0x37, 0x50, 0x9a, 0x44,
  32323. 0x9a, 0xd5, 0x85, 0x30, 0x40, 0x24, 0x1c, 0x52,
  32324. 0x36, 0x39, 0x54, 0x35, 0xc3, 0x64, 0x24, 0xfd,
  32325. 0x56, 0x0b, 0x0c, 0xb6, 0x2b, 0x28, 0x1d, 0x28,
  32326. 0x52, 0x75, 0xa7, 0x40, 0xce, 0x32, 0xa2, 0x2d,
  32327. 0xd1, 0x74, 0x0f, 0x4a, 0xa9, 0x16, 0x1c, 0xec,
  32328. 0x95, 0xcc, 0xc6, 0x1a, 0x18, 0xf4, 0xff, 0x07,
  32329. };
  32330. #endif /* HAVE_CURVE448_SHARED_SECRET */
  32331. (void)x;
  32332. WOLFSSL_ENTER("curve448_test");
  32333. #ifndef HAVE_FIPS
  32334. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  32335. #else
  32336. ret = wc_InitRng(&rng);
  32337. #endif
  32338. if (ret != 0)
  32339. return WC_TEST_RET_ENC_EC(ret);
  32340. wc_curve448_init(&userA);
  32341. wc_curve448_init(&userB);
  32342. wc_curve448_init(&pubKey);
  32343. /* make curve448 keys */
  32344. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userA);
  32345. if (ret != 0)
  32346. return WC_TEST_RET_ENC_EC(ret);
  32347. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userB);
  32348. if (ret != 0)
  32349. return WC_TEST_RET_ENC_EC(ret);
  32350. #ifdef HAVE_CURVE448_SHARED_SECRET
  32351. /* find shared secret key */
  32352. x = sizeof(sharedA);
  32353. ret = wc_curve448_shared_secret(&userA, &userB, sharedA, &x);
  32354. if (ret != 0)
  32355. return WC_TEST_RET_ENC_EC(ret);
  32356. y = sizeof(sharedB);
  32357. ret = wc_curve448_shared_secret(&userB, &userA, sharedB, &y);
  32358. if (ret != 0)
  32359. return WC_TEST_RET_ENC_EC(ret);
  32360. /* compare shared secret keys to test they are the same */
  32361. if (y != x)
  32362. return WC_TEST_RET_ENC_NC;
  32363. if (XMEMCMP(sharedA, sharedB, x))
  32364. return WC_TEST_RET_ENC_NC;
  32365. #endif
  32366. #ifdef HAVE_CURVE448_KEY_EXPORT
  32367. /* export a public key and import it for another user */
  32368. x = sizeof(exportBuf);
  32369. ret = wc_curve448_export_public(&userA, exportBuf, &x);
  32370. if (ret != 0)
  32371. return WC_TEST_RET_ENC_EC(ret);
  32372. #ifdef HAVE_CURVE448_KEY_IMPORT
  32373. ret = wc_curve448_import_public(exportBuf, x, &pubKey);
  32374. if (ret != 0)
  32375. return WC_TEST_RET_ENC_EC(ret);
  32376. #endif
  32377. #endif
  32378. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  32379. defined(HAVE_CURVE448_KEY_IMPORT)
  32380. /* test shared key after importing a public key */
  32381. XMEMSET(sharedB, 0, sizeof(sharedB));
  32382. y = sizeof(sharedB);
  32383. ret = wc_curve448_shared_secret(&userB, &pubKey, sharedB, &y);
  32384. if (ret != 0)
  32385. return WC_TEST_RET_ENC_EC(ret);
  32386. if (XMEMCMP(sharedA, sharedB, y))
  32387. return WC_TEST_RET_ENC_NC;
  32388. /* import RFC test vectors and compare shared key */
  32389. ret = wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA);
  32390. if (ret != 0)
  32391. return WC_TEST_RET_ENC_EC(ret);
  32392. ret = wc_curve448_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB);
  32393. if (ret != 0)
  32394. return WC_TEST_RET_ENC_EC(ret);
  32395. /* test against known test vector */
  32396. XMEMSET(sharedB, 0, sizeof(sharedB));
  32397. y = sizeof(sharedB);
  32398. ret = wc_curve448_shared_secret(&userA, &userB, sharedB, &y);
  32399. if (ret != 0)
  32400. return WC_TEST_RET_ENC_EC(ret);
  32401. if (XMEMCMP(ss, sharedB, y))
  32402. return WC_TEST_RET_ENC_NC;
  32403. /* test swapping roles of keys and generating same shared key */
  32404. XMEMSET(sharedB, 0, sizeof(sharedB));
  32405. y = sizeof(sharedB);
  32406. ret = wc_curve448_shared_secret(&userB, &userA, sharedB, &y);
  32407. if (ret != 0)
  32408. return WC_TEST_RET_ENC_EC(ret);
  32409. if (XMEMCMP(ss, sharedB, y))
  32410. return WC_TEST_RET_ENC_NC;
  32411. /* test with 1 generated key and 1 from known test vector */
  32412. ret = wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA);
  32413. if (ret != 0)
  32414. return WC_TEST_RET_ENC_EC(ret);
  32415. ret = wc_curve448_make_key(&rng, 56, &userB);
  32416. if (ret != 0)
  32417. return WC_TEST_RET_ENC_EC(ret);
  32418. x = sizeof(sharedA);
  32419. ret = wc_curve448_shared_secret(&userA, &userB, sharedA, &x);
  32420. if (ret != 0)
  32421. return WC_TEST_RET_ENC_EC(ret);
  32422. y = sizeof(sharedB);
  32423. ret = wc_curve448_shared_secret(&userB, &userA, sharedB, &y);
  32424. if (ret != 0)
  32425. return WC_TEST_RET_ENC_EC(ret);
  32426. /* compare shared secret keys to test they are the same */
  32427. if (y != x)
  32428. return WC_TEST_RET_ENC_NC;
  32429. if (XMEMCMP(sharedA, sharedB, x))
  32430. return WC_TEST_RET_ENC_NC;
  32431. ret = curve448_check_public_test();
  32432. if (ret != 0)
  32433. return ret;
  32434. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  32435. /* clean up keys when done */
  32436. wc_curve448_free(&pubKey);
  32437. wc_curve448_free(&userB);
  32438. wc_curve448_free(&userA);
  32439. wc_FreeRng(&rng);
  32440. return 0;
  32441. }
  32442. #endif /* HAVE_CURVE448 */
  32443. #ifdef HAVE_ED448
  32444. #ifdef WOLFSSL_TEST_CERT
  32445. static wc_test_ret_t ed448_test_cert(void)
  32446. {
  32447. DecodedCert cert[2];
  32448. DecodedCert* serverCert = NULL;
  32449. DecodedCert* caCert = NULL;
  32450. #ifdef HAVE_ED448_VERIFY
  32451. ed448_key key;
  32452. ed448_key* pubKey = NULL;
  32453. int verify;
  32454. #endif /* HAVE_ED448_VERIFY */
  32455. wc_test_ret_t ret;
  32456. byte* tmp;
  32457. size_t bytes;
  32458. XFILE file;
  32459. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32460. if (tmp == NULL) {
  32461. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  32462. }
  32463. #ifdef USE_CERT_BUFFERS_256
  32464. XMEMCPY(tmp, ca_ed448_cert, sizeof_ca_ed448_cert);
  32465. bytes = sizeof_ca_ed448_cert;
  32466. #elif !defined(NO_FILESYSTEM)
  32467. file = XFOPEN(caEd448Cert, "rb");
  32468. if (file == NULL) {
  32469. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  32470. }
  32471. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  32472. XFCLOSE(file);
  32473. if (bytes == 0)
  32474. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  32475. #else
  32476. /* No certificate to use. */
  32477. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  32478. #endif
  32479. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  32480. caCert = &cert[0];
  32481. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  32482. if (ret != 0)
  32483. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32484. #ifdef USE_CERT_BUFFERS_256
  32485. XMEMCPY(tmp, server_ed448_cert, sizeof_server_ed448_cert);
  32486. bytes = sizeof_server_ed448_cert;
  32487. #elif !defined(NO_FILESYSTEM)
  32488. file = XFOPEN(serverEd448Cert, "rb");
  32489. if (file == NULL) {
  32490. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  32491. }
  32492. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  32493. XFCLOSE(file);
  32494. if (bytes == 0)
  32495. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  32496. #else
  32497. /* No certificate to use. */
  32498. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  32499. #endif
  32500. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  32501. serverCert = &cert[1];
  32502. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  32503. if (ret != 0)
  32504. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32505. #ifdef HAVE_ED448_VERIFY
  32506. ret = wc_ed448_init(&key);
  32507. if (ret < 0)
  32508. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32509. pubKey = &key;
  32510. ret = wc_ed448_import_public(caCert->publicKey, caCert->pubKeySize, pubKey);
  32511. if (ret < 0)
  32512. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32513. ret = wc_ed448_verify_msg(serverCert->signature, serverCert->sigLength,
  32514. serverCert->source + serverCert->certBegin,
  32515. serverCert->sigIndex - serverCert->certBegin,
  32516. &verify, pubKey, NULL, 0);
  32517. if (ret < 0 || verify != 1) {
  32518. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32519. }
  32520. #endif /* HAVE_ED448_VERIFY */
  32521. done:
  32522. if (tmp != NULL)
  32523. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32524. #ifdef HAVE_ED448_VERIFY
  32525. wc_ed448_free(pubKey);
  32526. #endif /* HAVE_ED448_VERIFY */
  32527. if (caCert != NULL)
  32528. FreeDecodedCert(caCert);
  32529. if (serverCert != NULL)
  32530. FreeDecodedCert(serverCert);
  32531. return ret;
  32532. }
  32533. static wc_test_ret_t ed448_test_make_cert(void)
  32534. {
  32535. WC_RNG rng;
  32536. Cert cert;
  32537. DecodedCert decode;
  32538. ed448_key key;
  32539. ed448_key* privKey = NULL;
  32540. wc_test_ret_t ret = 0;
  32541. byte* tmp = NULL;
  32542. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  32543. #ifndef HAVE_FIPS
  32544. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  32545. #else
  32546. ret = wc_InitRng(&rng);
  32547. #endif
  32548. if (ret != 0)
  32549. return WC_TEST_RET_ENC_EC(ret);
  32550. wc_ed448_init(&key);
  32551. privKey = &key;
  32552. wc_ed448_make_key(&rng, ED448_KEY_SIZE, privKey);
  32553. cert.daysValid = 365 * 2;
  32554. cert.selfSigned = 1;
  32555. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  32556. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  32557. cert.isCA = 0;
  32558. #ifdef WOLFSSL_CERT_EXT
  32559. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  32560. if (ret < 0)
  32561. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32562. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  32563. if (ret < 0)
  32564. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32565. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  32566. if (ret < 0)
  32567. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32568. #endif
  32569. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32570. if (tmp == NULL) {
  32571. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  32572. }
  32573. cert.sigType = CTC_ED448;
  32574. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED448_TYPE, privKey, &rng);
  32575. if (ret < 0)
  32576. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32577. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF, ED448_TYPE,
  32578. privKey, &rng);
  32579. if (ret < 0)
  32580. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32581. InitDecodedCert(&decode, tmp, (word32)ret, HEAP_HINT);
  32582. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  32583. FreeDecodedCert(&decode);
  32584. if (ret != 0)
  32585. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  32586. done:
  32587. if (tmp != NULL)
  32588. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32589. wc_ed448_free(privKey);
  32590. wc_FreeRng(&rng);
  32591. return ret;
  32592. }
  32593. #endif /* WOLFSSL_TEST_CERT */
  32594. #if defined(HAVE_ED448_KEY_IMPORT)
  32595. static wc_test_ret_t ed448_test_check_key(void)
  32596. {
  32597. /* Fails to find x-ordinate from this y-ordinate. */
  32598. WOLFSSL_SMALL_STACK_STATIC const byte key_bad_y[] = {
  32599. 0x40,
  32600. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32601. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32602. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32603. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32604. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32605. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32606. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32607. 0x00
  32608. };
  32609. /* Y-ordinate value larger than prime. */
  32610. WOLFSSL_SMALL_STACK_STATIC const byte key_bad_y_max[] = {
  32611. 0x40,
  32612. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32613. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32614. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32615. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32616. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32617. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32618. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32619. 0xff
  32620. };
  32621. /* Y-ordinate value equal to prime. */
  32622. WOLFSSL_SMALL_STACK_STATIC const byte key_bad_y_is_p[] = {
  32623. 0x40,
  32624. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32625. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32626. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32627. 0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,
  32628. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32629. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32630. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32631. 0xff
  32632. };
  32633. /* Y-ordinate value equal to prime - 1. */
  32634. WOLFSSL_SMALL_STACK_STATIC const byte key_y_is_p_minus_1[] = {
  32635. 0x40,
  32636. 0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32637. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32638. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32639. 0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,
  32640. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32641. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32642. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  32643. 0xff
  32644. };
  32645. ed448_key key;
  32646. int ret;
  32647. int res = 0;
  32648. /* Initialize key for use. */
  32649. ret = wc_ed448_init_ex(&key, HEAP_HINT, devId);
  32650. if (ret != 0) {
  32651. return WC_TEST_RET_ENC_NC;
  32652. }
  32653. /* Load bad public key only and perform checks. */
  32654. ret = wc_ed448_import_public(key_bad_y, ED448_PUB_KEY_SIZE + 1, &key);
  32655. if (ret != PUBLIC_KEY_E) {
  32656. res = WC_TEST_RET_ENC_NC;
  32657. }
  32658. if (ret == 0) {
  32659. /* Load bad public key only and perform checks. */
  32660. ret = wc_ed448_import_public(key_bad_y_max, ED448_PUB_KEY_SIZE + 1,
  32661. &key);
  32662. if (ret != PUBLIC_KEY_E) {
  32663. res = WC_TEST_RET_ENC_NC;
  32664. }
  32665. }
  32666. if (res == 0) {
  32667. /* Load bad public key only and perform checks. */
  32668. ret = wc_ed448_import_public(key_bad_y_is_p, ED448_PUB_KEY_SIZE + 1,
  32669. &key);
  32670. if (ret != PUBLIC_KEY_E) {
  32671. res = WC_TEST_RET_ENC_NC;
  32672. }
  32673. }
  32674. if (res == 0) {
  32675. /* Load good public key only and perform checks. */
  32676. ret = wc_ed448_import_public(key_y_is_p_minus_1, ED448_PUB_KEY_SIZE + 1,
  32677. &key);
  32678. if (ret != 0) {
  32679. res = WC_TEST_RET_ENC_NC;
  32680. }
  32681. }
  32682. /* Dispose of key. */
  32683. wc_ed448_free(&key);
  32684. return res;
  32685. }
  32686. #endif
  32687. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  32688. defined(HAVE_ED448_KEY_IMPORT)
  32689. static wc_test_ret_t ed448_ctx_test(void)
  32690. {
  32691. wc_test_ret_t ret;
  32692. byte out[ED448_SIG_SIZE];
  32693. word32 outlen;
  32694. #ifdef HAVE_ED448_VERIFY
  32695. int verify;
  32696. #endif /* HAVE_ED448_VERIFY */
  32697. ed448_key key;
  32698. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  32699. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  32700. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  32701. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  32702. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  32703. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  32704. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  32705. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  32706. 0x4e
  32707. };
  32708. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  32709. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  32710. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  32711. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  32712. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  32713. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  32714. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  32715. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  32716. 0x80
  32717. };
  32718. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx[] = {
  32719. 0xd4, 0xf8, 0xf6, 0x13, 0x17, 0x70, 0xdd, 0x46,
  32720. 0xf4, 0x08, 0x67, 0xd6, 0xfd, 0x5d, 0x50, 0x55,
  32721. 0xde, 0x43, 0x54, 0x1f, 0x8c, 0x5e, 0x35, 0xab,
  32722. 0xbc, 0xd0, 0x01, 0xb3, 0x2a, 0x89, 0xf7, 0xd2,
  32723. 0x15, 0x1f, 0x76, 0x47, 0xf1, 0x1d, 0x8c, 0xa2,
  32724. 0xae, 0x27, 0x9f, 0xb8, 0x42, 0xd6, 0x07, 0x21,
  32725. 0x7f, 0xce, 0x6e, 0x04, 0x2f, 0x68, 0x15, 0xea,
  32726. 0x00, 0x0c, 0x85, 0x74, 0x1d, 0xe5, 0xc8, 0xda,
  32727. 0x11, 0x44, 0xa6, 0xa1, 0xab, 0xa7, 0xf9, 0x6d,
  32728. 0xe4, 0x25, 0x05, 0xd7, 0xa7, 0x29, 0x85, 0x24,
  32729. 0xfd, 0xa5, 0x38, 0xfc, 0xcb, 0xbb, 0x75, 0x4f,
  32730. 0x57, 0x8c, 0x1c, 0xad, 0x10, 0xd5, 0x4d, 0x0d,
  32731. 0x54, 0x28, 0x40, 0x7e, 0x85, 0xdc, 0xbc, 0x98,
  32732. 0xa4, 0x91, 0x55, 0xc1, 0x37, 0x64, 0xe6, 0x6c,
  32733. 0x3c, 0x00
  32734. };
  32735. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  32736. 0x03
  32737. };
  32738. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  32739. 0x66,0x6f,0x6f
  32740. };
  32741. outlen = sizeof(out);
  32742. XMEMSET(out, 0, sizeof(out));
  32743. ret = wc_ed448_init_ex(&key, HEAP_HINT, devId);
  32744. if (ret != 0)
  32745. return WC_TEST_RET_ENC_EC(ret);
  32746. ret = wc_ed448_import_private_key(sKeyCtx, ED448_KEY_SIZE, pKeyCtx,
  32747. sizeof(pKeyCtx), &key);
  32748. if (ret != 0)
  32749. return WC_TEST_RET_ENC_EC(ret);
  32750. ret = wc_ed448_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  32751. contextCtx, sizeof(contextCtx));
  32752. if (ret != 0)
  32753. return WC_TEST_RET_ENC_EC(ret);
  32754. if (XMEMCMP(out, sigCtx, sizeof(sigCtx)))
  32755. return WC_TEST_RET_ENC_NC;
  32756. #if defined(HAVE_ED448_VERIFY)
  32757. /* test verify on good msg */
  32758. ret = wc_ed448_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify, &key,
  32759. contextCtx, sizeof(contextCtx));
  32760. if (ret != 0 || verify != 1)
  32761. return WC_TEST_RET_ENC_EC(ret);
  32762. #endif
  32763. wc_ed448_free(&key);
  32764. return 0;
  32765. }
  32766. static wc_test_ret_t ed448ph_test(void)
  32767. {
  32768. wc_test_ret_t ret;
  32769. byte out[ED448_SIG_SIZE];
  32770. word32 outlen;
  32771. #ifdef HAVE_ED448_VERIFY
  32772. int verify;
  32773. #endif /* HAVE_ED448_VERIFY */
  32774. ed448_key key;
  32775. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  32776. 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d,
  32777. 0x62, 0xec, 0x77, 0x58, 0x75, 0x20, 0x91, 0x1e,
  32778. 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b,
  32779. 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42,
  32780. 0xef, 0x78, 0x22, 0xe0, 0xd5, 0x10, 0x41, 0x27,
  32781. 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3,
  32782. 0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c,
  32783. 0x49
  32784. };
  32785. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  32786. 0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77,
  32787. 0xa7, 0xab, 0xd2, 0x65, 0x24, 0xcb, 0xdb, 0x31,
  32788. 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde,
  32789. 0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43,
  32790. 0xc0, 0x94, 0x28, 0xa1, 0x31, 0xd6, 0xb1, 0xb5,
  32791. 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76,
  32792. 0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38,
  32793. 0x80
  32794. };
  32795. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  32796. 0x82, 0x2f, 0x69, 0x01, 0xf7, 0x48, 0x0f, 0x3d,
  32797. 0x5f, 0x56, 0x2c, 0x59, 0x29, 0x94, 0xd9, 0x69,
  32798. 0x36, 0x02, 0x87, 0x56, 0x14, 0x48, 0x32, 0x56,
  32799. 0x50, 0x56, 0x00, 0xbb, 0xc2, 0x81, 0xae, 0x38,
  32800. 0x1f, 0x54, 0xd6, 0xbc, 0xe2, 0xea, 0x91, 0x15,
  32801. 0x74, 0x93, 0x2f, 0x52, 0xa4, 0xe6, 0xca, 0xdd,
  32802. 0x78, 0x76, 0x93, 0x75, 0xec, 0x3f, 0xfd, 0x1b,
  32803. 0x80, 0x1a, 0x0d, 0x9b, 0x3f, 0x40, 0x30, 0xcd,
  32804. 0x43, 0x39, 0x64, 0xb6, 0x45, 0x7e, 0xa3, 0x94,
  32805. 0x76, 0x51, 0x12, 0x14, 0xf9, 0x74, 0x69, 0xb5,
  32806. 0x7d, 0xd3, 0x2d, 0xbc, 0x56, 0x0a, 0x9a, 0x94,
  32807. 0xd0, 0x0b, 0xff, 0x07, 0x62, 0x04, 0x64, 0xa3,
  32808. 0xad, 0x20, 0x3d, 0xf7, 0xdc, 0x7c, 0xe3, 0x60,
  32809. 0xc3, 0xcd, 0x36, 0x96, 0xd9, 0xd9, 0xfa, 0xb9,
  32810. 0x0f, 0x00
  32811. };
  32812. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  32813. 0xc3, 0x22, 0x99, 0xd4, 0x6e, 0xc8, 0xff, 0x02,
  32814. 0xb5, 0x45, 0x40, 0x98, 0x28, 0x14, 0xdc, 0xe9,
  32815. 0xa0, 0x58, 0x12, 0xf8, 0x19, 0x62, 0xb6, 0x49,
  32816. 0xd5, 0x28, 0x09, 0x59, 0x16, 0xa2, 0xaa, 0x48,
  32817. 0x10, 0x65, 0xb1, 0x58, 0x04, 0x23, 0xef, 0x92,
  32818. 0x7e, 0xcf, 0x0a, 0xf5, 0x88, 0x8f, 0x90, 0xda,
  32819. 0x0f, 0x6a, 0x9a, 0x85, 0xad, 0x5d, 0xc3, 0xf2,
  32820. 0x80, 0xd9, 0x12, 0x24, 0xba, 0x99, 0x11, 0xa3,
  32821. 0x65, 0x3d, 0x00, 0xe4, 0x84, 0xe2, 0xce, 0x23,
  32822. 0x25, 0x21, 0x48, 0x1c, 0x86, 0x58, 0xdf, 0x30,
  32823. 0x4b, 0xb7, 0x74, 0x5a, 0x73, 0x51, 0x4c, 0xdb,
  32824. 0x9b, 0xf3, 0xe1, 0x57, 0x84, 0xab, 0x71, 0x28,
  32825. 0x4f, 0x8d, 0x07, 0x04, 0xa6, 0x08, 0xc5, 0x4a,
  32826. 0x6b, 0x62, 0xd9, 0x7b, 0xeb, 0x51, 0x1d, 0x13,
  32827. 0x21, 0x00
  32828. };
  32829. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  32830. 0x61,0x62,0x63
  32831. };
  32832. /* SHA-512 hash of msgPh */
  32833. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  32834. 0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77,
  32835. 0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
  32836. 0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee,
  32837. 0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39,
  32838. 0xd5, 0xa1, 0x5b, 0xef, 0x18, 0x6a, 0x53, 0x86,
  32839. 0xc7, 0x57, 0x44, 0xc0, 0x52, 0x7e, 0x1f, 0xaa,
  32840. 0x9f, 0x87, 0x26, 0xe4, 0x62, 0xa1, 0x2a, 0x4f,
  32841. 0xeb, 0x06, 0xbd, 0x88, 0x01, 0xe7, 0x51, 0xe4
  32842. };
  32843. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  32844. 0x66,0x6f,0x6f
  32845. };
  32846. outlen = sizeof(out);
  32847. XMEMSET(out, 0, sizeof(out));
  32848. ret = wc_ed448_init_ex(&key, HEAP_HINT, devId);
  32849. if (ret != 0)
  32850. return WC_TEST_RET_ENC_EC(ret);
  32851. ret = wc_ed448_import_private_key(sKeyPh, ED448_KEY_SIZE, pKeyPh,
  32852. sizeof(pKeyPh), &key);
  32853. if (ret != 0)
  32854. return WC_TEST_RET_ENC_EC(ret);
  32855. ret = wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key, NULL,
  32856. 0);
  32857. if (ret != 0)
  32858. return WC_TEST_RET_ENC_EC(ret);
  32859. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  32860. return WC_TEST_RET_ENC_NC;
  32861. #if defined(HAVE_ED448_VERIFY)
  32862. /* test verify on good msg */
  32863. ret = wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  32864. NULL, 0);
  32865. if (ret != 0 || verify != 1) {
  32866. return WC_TEST_RET_ENC_EC(ret);
  32867. }
  32868. #endif
  32869. ret = wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  32870. contextPh2, sizeof(contextPh2));
  32871. if (ret != 0)
  32872. return WC_TEST_RET_ENC_EC(ret);
  32873. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  32874. return WC_TEST_RET_ENC_NC;
  32875. #if defined(HAVE_ED448_VERIFY)
  32876. /* test verify on good msg */
  32877. ret = wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  32878. contextPh2, sizeof(contextPh2));
  32879. if (ret != 0 || verify != 1) {
  32880. return WC_TEST_RET_ENC_EC(ret);
  32881. }
  32882. #endif
  32883. ret = wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key, NULL,
  32884. 0);
  32885. if (ret != 0)
  32886. return WC_TEST_RET_ENC_EC(ret);
  32887. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  32888. return WC_TEST_RET_ENC_NC;
  32889. #if defined(HAVE_ED448_VERIFY)
  32890. ret = wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  32891. &key, NULL, 0);
  32892. if (ret != 0 || verify != 1) {
  32893. return WC_TEST_RET_ENC_EC(ret);
  32894. }
  32895. #endif
  32896. ret = wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  32897. contextPh2, sizeof(contextPh2));
  32898. if (ret != 0)
  32899. return WC_TEST_RET_ENC_EC(ret);
  32900. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  32901. return WC_TEST_RET_ENC_NC;
  32902. #if defined(HAVE_ED448_VERIFY)
  32903. ret = wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  32904. &key, contextPh2, sizeof(contextPh2));
  32905. if (ret != 0 || verify != 1) {
  32906. return WC_TEST_RET_ENC_EC(ret);
  32907. }
  32908. #endif
  32909. wc_ed448_free(&key);
  32910. return 0;
  32911. }
  32912. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  32913. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed448_test(void)
  32914. {
  32915. wc_test_ret_t ret;
  32916. WC_RNG rng;
  32917. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  32918. defined(HAVE_ED448_KEY_IMPORT)
  32919. byte out[ED448_SIG_SIZE];
  32920. int i;
  32921. word32 outlen;
  32922. #ifdef HAVE_ED448_VERIFY
  32923. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  32924. int j;
  32925. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  32926. int verify;
  32927. #endif /* HAVE_ED448_VERIFY */
  32928. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  32929. word32 keySz, sigSz;
  32930. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  32931. ed448_key *key = NULL;
  32932. ed448_key *key2 = NULL;
  32933. #else
  32934. ed448_key key[1];
  32935. ed448_key key2[1];
  32936. #endif
  32937. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  32938. defined(HAVE_ED448_KEY_IMPORT)
  32939. /* test vectors from
  32940. https://tools.ietf.org/html/rfc8032
  32941. */
  32942. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  32943. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  32944. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  32945. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  32946. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  32947. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  32948. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  32949. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  32950. 0x5b
  32951. };
  32952. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  32953. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  32954. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  32955. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  32956. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  32957. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  32958. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  32959. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  32960. 0x4e
  32961. };
  32962. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  32963. 0x25, 0x8c, 0xdd, 0x4a, 0xda, 0x32, 0xed, 0x9c,
  32964. 0x9f, 0xf5, 0x4e, 0x63, 0x75, 0x6a, 0xe5, 0x82,
  32965. 0xfb, 0x8f, 0xab, 0x2a, 0xc7, 0x21, 0xf2, 0xc8,
  32966. 0xe6, 0x76, 0xa7, 0x27, 0x68, 0x51, 0x3d, 0x93,
  32967. 0x9f, 0x63, 0xdd, 0xdb, 0x55, 0x60, 0x91, 0x33,
  32968. 0xf2, 0x9a, 0xdf, 0x86, 0xec, 0x99, 0x29, 0xdc,
  32969. 0xcb, 0x52, 0xc1, 0xc5, 0xfd, 0x2f, 0xf7, 0xe2,
  32970. 0x1b
  32971. };
  32972. /* uncompressed test */
  32973. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  32974. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  32975. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  32976. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  32977. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  32978. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  32979. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  32980. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  32981. 0x5b
  32982. };
  32983. /* compressed prefix test */
  32984. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  32985. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  32986. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  32987. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  32988. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  32989. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  32990. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  32991. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  32992. 0x5b
  32993. };
  32994. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  32995. 0x87, 0x2d, 0x09, 0x37, 0x80, 0xf5, 0xd3, 0x73,
  32996. 0x0d, 0xf7, 0xc2, 0x12, 0x66, 0x4b, 0x37, 0xb8,
  32997. 0xa0, 0xf2, 0x4f, 0x56, 0x81, 0x0d, 0xaa, 0x83,
  32998. 0x82, 0xcd, 0x4f, 0xa3, 0xf7, 0x76, 0x34, 0xec,
  32999. 0x44, 0xdc, 0x54, 0xf1, 0xc2, 0xed, 0x9b, 0xea,
  33000. 0x86, 0xfa, 0xfb, 0x76, 0x32, 0xd8, 0xbe, 0x19,
  33001. 0x9e, 0xa1, 0x65, 0xf5, 0xad, 0x55, 0xdd, 0x9c,
  33002. 0xe8
  33003. };
  33004. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  33005. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  33006. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  33007. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  33008. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  33009. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  33010. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  33011. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  33012. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  33013. 0x80
  33014. };
  33015. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  33016. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  33017. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  33018. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  33019. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  33020. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  33021. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  33022. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  33023. 0x80
  33024. };
  33025. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  33026. 0x3b, 0xa1, 0x6d, 0xa0, 0xc6, 0xf2, 0xcc, 0x1f,
  33027. 0x30, 0x18, 0x77, 0x40, 0x75, 0x6f, 0x5e, 0x79,
  33028. 0x8d, 0x6b, 0xc5, 0xfc, 0x01, 0x5d, 0x7c, 0x63,
  33029. 0xcc, 0x95, 0x10, 0xee, 0x3f, 0xd4, 0x4a, 0xdc,
  33030. 0x24, 0xd8, 0xe9, 0x68, 0xb6, 0xe4, 0x6e, 0x6f,
  33031. 0x94, 0xd1, 0x9b, 0x94, 0x53, 0x61, 0x72, 0x6b,
  33032. 0xd7, 0x5e, 0x14, 0x9e, 0xf0, 0x98, 0x17, 0xf5,
  33033. 0x80
  33034. };
  33035. /* uncompressed test */
  33036. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  33037. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  33038. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  33039. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  33040. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  33041. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  33042. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  33043. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  33044. 0x80
  33045. };
  33046. /* compressed prefix */
  33047. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  33048. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  33049. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  33050. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  33051. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  33052. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  33053. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  33054. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  33055. 0x80
  33056. };
  33057. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  33058. 0xa8, 0x1b, 0x2e, 0x8a, 0x70, 0xa5, 0xac, 0x94,
  33059. 0xff, 0xdb, 0xcc, 0x9b, 0xad, 0xfc, 0x3f, 0xeb,
  33060. 0x08, 0x01, 0xf2, 0x58, 0x57, 0x8b, 0xb1, 0x14,
  33061. 0xad, 0x44, 0xec, 0xe1, 0xec, 0x0e, 0x79, 0x9d,
  33062. 0xa0, 0x8e, 0xff, 0xb8, 0x1c, 0x5d, 0x68, 0x5c,
  33063. 0x0c, 0x56, 0xf6, 0x4e, 0xec, 0xae, 0xf8, 0xcd,
  33064. 0xf1, 0x1c, 0xc3, 0x87, 0x37, 0x83, 0x8c, 0xf4,
  33065. 0x00
  33066. };
  33067. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  33068. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  33069. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  33070. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  33071. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  33072. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  33073. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  33074. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  33075. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  33076. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  33077. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  33078. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  33079. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  33080. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  33081. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  33082. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  33083. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  33084. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  33085. 0x26, 0x00
  33086. };
  33087. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  33088. 0x26, 0xb8, 0xf9, 0x17, 0x27, 0xbd, 0x62, 0x89,
  33089. 0x7a, 0xf1, 0x5e, 0x41, 0xeb, 0x43, 0xc3, 0x77,
  33090. 0xef, 0xb9, 0xc6, 0x10, 0xd4, 0x8f, 0x23, 0x35,
  33091. 0xcb, 0x0b, 0xd0, 0x08, 0x78, 0x10, 0xf4, 0x35,
  33092. 0x25, 0x41, 0xb1, 0x43, 0xc4, 0xb9, 0x81, 0xb7,
  33093. 0xe1, 0x8f, 0x62, 0xde, 0x8c, 0xcd, 0xf6, 0x33,
  33094. 0xfc, 0x1b, 0xf0, 0x37, 0xab, 0x7c, 0xd7, 0x79,
  33095. 0x80, 0x5e, 0x0d, 0xbc, 0xc0, 0xaa, 0xe1, 0xcb,
  33096. 0xce, 0xe1, 0xaf, 0xb2, 0xe0, 0x27, 0xdf, 0x36,
  33097. 0xbc, 0x04, 0xdc, 0xec, 0xbf, 0x15, 0x43, 0x36,
  33098. 0xc1, 0x9f, 0x0a, 0xf7, 0xe0, 0xa6, 0x47, 0x29,
  33099. 0x05, 0xe7, 0x99, 0xf1, 0x95, 0x3d, 0x2a, 0x0f,
  33100. 0xf3, 0x34, 0x8a, 0xb2, 0x1a, 0xa4, 0xad, 0xaf,
  33101. 0xd1, 0xd2, 0x34, 0x44, 0x1c, 0xf8, 0x07, 0xc0,
  33102. 0x3a, 0x00
  33103. };
  33104. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  33105. 0x7e, 0xee, 0xab, 0x7c, 0x4e, 0x50, 0xfb, 0x79,
  33106. 0x9b, 0x41, 0x8e, 0xe5, 0xe3, 0x19, 0x7f, 0xf6,
  33107. 0xbf, 0x15, 0xd4, 0x3a, 0x14, 0xc3, 0x43, 0x89,
  33108. 0xb5, 0x9d, 0xd1, 0xa7, 0xb1, 0xb8, 0x5b, 0x4a,
  33109. 0xe9, 0x04, 0x38, 0xac, 0xa6, 0x34, 0xbe, 0xa4,
  33110. 0x5e, 0x3a, 0x26, 0x95, 0xf1, 0x27, 0x0f, 0x07,
  33111. 0xfd, 0xcd, 0xf7, 0xc6, 0x2b, 0x8e, 0xfe, 0xaf,
  33112. 0x00, 0xb4, 0x5c, 0x2c, 0x96, 0xba, 0x45, 0x7e,
  33113. 0xb1, 0xa8, 0xbf, 0x07, 0x5a, 0x3d, 0xb2, 0x8e,
  33114. 0x5c, 0x24, 0xf6, 0xb9, 0x23, 0xed, 0x4a, 0xd7,
  33115. 0x47, 0xc3, 0xc9, 0xe0, 0x3c, 0x70, 0x79, 0xef,
  33116. 0xb8, 0x7c, 0xb1, 0x10, 0xd3, 0xa9, 0x98, 0x61,
  33117. 0xe7, 0x20, 0x03, 0xcb, 0xae, 0x6d, 0x6b, 0x8b,
  33118. 0x82, 0x7e, 0x4e, 0x6c, 0x14, 0x30, 0x64, 0xff,
  33119. 0x3c, 0x00
  33120. };
  33121. /* uncompressed test */
  33122. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  33123. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  33124. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  33125. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  33126. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  33127. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  33128. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  33129. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  33130. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  33131. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  33132. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  33133. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  33134. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  33135. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  33136. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  33137. 0x26, 0x00
  33138. };
  33139. /* compressed prefix */
  33140. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  33141. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  33142. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  33143. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  33144. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  33145. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  33146. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  33147. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  33148. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  33149. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  33150. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  33151. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  33152. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  33153. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  33154. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  33155. 0x26, 0x00
  33156. };
  33157. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  33158. 0xe3, 0x01, 0x34, 0x5a, 0x41, 0xa3, 0x9a, 0x4d,
  33159. 0x72, 0xff, 0xf8, 0xdf, 0x69, 0xc9, 0x80, 0x75,
  33160. 0xa0, 0xcc, 0x08, 0x2b, 0x80, 0x2f, 0xc9, 0xb2,
  33161. 0xb6, 0xbc, 0x50, 0x3f, 0x92, 0x6b, 0x65, 0xbd,
  33162. 0xdf, 0x7f, 0x4c, 0x8f, 0x1c, 0xb4, 0x9f, 0x63,
  33163. 0x96, 0xaf, 0xc8, 0xa7, 0x0a, 0xbe, 0x6d, 0x8a,
  33164. 0xef, 0x0d, 0xb4, 0x78, 0xd4, 0xc6, 0xb2, 0x97,
  33165. 0x00, 0x76, 0xc6, 0xa0, 0x48, 0x4f, 0xe7, 0x6d,
  33166. 0x76, 0xb3, 0xa9, 0x76, 0x25, 0xd7, 0x9f, 0x1c,
  33167. 0xe2, 0x40, 0xe7, 0xc5, 0x76, 0x75, 0x0d, 0x29,
  33168. 0x55, 0x28, 0x28, 0x6f, 0x71, 0x9b, 0x41, 0x3d,
  33169. 0xe9, 0xad, 0xa3, 0xe8, 0xeb, 0x78, 0xed, 0x57,
  33170. 0x36, 0x03, 0xce, 0x30, 0xd8, 0xbb, 0x76, 0x17,
  33171. 0x85, 0xdc, 0x30, 0xdb, 0xc3, 0x20, 0x86, 0x9e,
  33172. 0x1a, 0x00
  33173. };
  33174. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  33175. #define SIGSZ sizeof(sig1)
  33176. PEDANTIC_EXTENSION WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = { 0 };
  33177. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = { 0x03 };
  33178. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = { 0x64, 0xa6, 0x5f, 0x3c, 0xde, 0xdc, 0xdd,
  33179. 0x66, 0x81, 0x1e, 0x29, 0x15 };
  33180. /* test of a 1023 byte long message */
  33181. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  33182. 0x6d, 0xdf, 0x80, 0x2e, 0x1a, 0xae, 0x49, 0x86,
  33183. 0x93, 0x5f, 0x7f, 0x98, 0x1b, 0xa3, 0xf0, 0x35,
  33184. 0x1d, 0x62, 0x73, 0xc0, 0xa0, 0xc2, 0x2c, 0x9c,
  33185. 0x0e, 0x83, 0x39, 0x16, 0x8e, 0x67, 0x54, 0x12,
  33186. 0xa3, 0xde, 0xbf, 0xaf, 0x43, 0x5e, 0xd6, 0x51,
  33187. 0x55, 0x80, 0x07, 0xdb, 0x43, 0x84, 0xb6, 0x50,
  33188. 0xfc, 0xc0, 0x7e, 0x3b, 0x58, 0x6a, 0x27, 0xa4,
  33189. 0xf7, 0xa0, 0x0a, 0xc8, 0xa6, 0xfe, 0xc2, 0xcd,
  33190. 0x86, 0xae, 0x4b, 0xf1, 0x57, 0x0c, 0x41, 0xe6,
  33191. 0xa4, 0x0c, 0x93, 0x1d, 0xb2, 0x7b, 0x2f, 0xaa,
  33192. 0x15, 0xa8, 0xce, 0xdd, 0x52, 0xcf, 0xf7, 0x36,
  33193. 0x2c, 0x4e, 0x6e, 0x23, 0xda, 0xec, 0x0f, 0xbc,
  33194. 0x3a, 0x79, 0xb6, 0x80, 0x6e, 0x31, 0x6e, 0xfc,
  33195. 0xc7, 0xb6, 0x81, 0x19, 0xbf, 0x46, 0xbc, 0x76,
  33196. 0xa2, 0x60, 0x67, 0xa5, 0x3f, 0x29, 0x6d, 0xaf,
  33197. 0xdb, 0xdc, 0x11, 0xc7, 0x7f, 0x77, 0x77, 0xe9,
  33198. 0x72, 0x66, 0x0c, 0xf4, 0xb6, 0xa9, 0xb3, 0x69,
  33199. 0xa6, 0x66, 0x5f, 0x02, 0xe0, 0xcc, 0x9b, 0x6e,
  33200. 0xdf, 0xad, 0x13, 0x6b, 0x4f, 0xab, 0xe7, 0x23,
  33201. 0xd2, 0x81, 0x3d, 0xb3, 0x13, 0x6c, 0xfd, 0xe9,
  33202. 0xb6, 0xd0, 0x44, 0x32, 0x2f, 0xee, 0x29, 0x47,
  33203. 0x95, 0x2e, 0x03, 0x1b, 0x73, 0xab, 0x5c, 0x60,
  33204. 0x33, 0x49, 0xb3, 0x07, 0xbd, 0xc2, 0x7b, 0xc6,
  33205. 0xcb, 0x8b, 0x8b, 0xbd, 0x7b, 0xd3, 0x23, 0x21,
  33206. 0x9b, 0x80, 0x33, 0xa5, 0x81, 0xb5, 0x9e, 0xad,
  33207. 0xeb, 0xb0, 0x9b, 0x3c, 0x4f, 0x3d, 0x22, 0x77,
  33208. 0xd4, 0xf0, 0x34, 0x36, 0x24, 0xac, 0xc8, 0x17,
  33209. 0x80, 0x47, 0x28, 0xb2, 0x5a, 0xb7, 0x97, 0x17,
  33210. 0x2b, 0x4c, 0x5c, 0x21, 0xa2, 0x2f, 0x9c, 0x78,
  33211. 0x39, 0xd6, 0x43, 0x00, 0x23, 0x2e, 0xb6, 0x6e,
  33212. 0x53, 0xf3, 0x1c, 0x72, 0x3f, 0xa3, 0x7f, 0xe3,
  33213. 0x87, 0xc7, 0xd3, 0xe5, 0x0b, 0xdf, 0x98, 0x13,
  33214. 0xa3, 0x0e, 0x5b, 0xb1, 0x2c, 0xf4, 0xcd, 0x93,
  33215. 0x0c, 0x40, 0xcf, 0xb4, 0xe1, 0xfc, 0x62, 0x25,
  33216. 0x92, 0xa4, 0x95, 0x88, 0x79, 0x44, 0x94, 0xd5,
  33217. 0x6d, 0x24, 0xea, 0x4b, 0x40, 0xc8, 0x9f, 0xc0,
  33218. 0x59, 0x6c, 0xc9, 0xeb, 0xb9, 0x61, 0xc8, 0xcb,
  33219. 0x10, 0xad, 0xde, 0x97, 0x6a, 0x5d, 0x60, 0x2b,
  33220. 0x1c, 0x3f, 0x85, 0xb9, 0xb9, 0xa0, 0x01, 0xed,
  33221. 0x3c, 0x6a, 0x4d, 0x3b, 0x14, 0x37, 0xf5, 0x20,
  33222. 0x96, 0xcd, 0x19, 0x56, 0xd0, 0x42, 0xa5, 0x97,
  33223. 0xd5, 0x61, 0xa5, 0x96, 0xec, 0xd3, 0xd1, 0x73,
  33224. 0x5a, 0x8d, 0x57, 0x0e, 0xa0, 0xec, 0x27, 0x22,
  33225. 0x5a, 0x2c, 0x4a, 0xaf, 0xf2, 0x63, 0x06, 0xd1,
  33226. 0x52, 0x6c, 0x1a, 0xf3, 0xca, 0x6d, 0x9c, 0xf5,
  33227. 0xa2, 0xc9, 0x8f, 0x47, 0xe1, 0xc4, 0x6d, 0xb9,
  33228. 0xa3, 0x32, 0x34, 0xcf, 0xd4, 0xd8, 0x1f, 0x2c,
  33229. 0x98, 0x53, 0x8a, 0x09, 0xeb, 0xe7, 0x69, 0x98,
  33230. 0xd0, 0xd8, 0xfd, 0x25, 0x99, 0x7c, 0x7d, 0x25,
  33231. 0x5c, 0x6d, 0x66, 0xec, 0xe6, 0xfa, 0x56, 0xf1,
  33232. 0x11, 0x44, 0x95, 0x0f, 0x02, 0x77, 0x95, 0xe6,
  33233. 0x53, 0x00, 0x8f, 0x4b, 0xd7, 0xca, 0x2d, 0xee,
  33234. 0x85, 0xd8, 0xe9, 0x0f, 0x3d, 0xc3, 0x15, 0x13,
  33235. 0x0c, 0xe2, 0xa0, 0x03, 0x75, 0xa3, 0x18, 0xc7,
  33236. 0xc3, 0xd9, 0x7b, 0xe2, 0xc8, 0xce, 0x5b, 0x6d,
  33237. 0xb4, 0x1a, 0x62, 0x54, 0xff, 0x26, 0x4f, 0xa6,
  33238. 0x15, 0x5b, 0xae, 0xe3, 0xb0, 0x77, 0x3c, 0x0f,
  33239. 0x49, 0x7c, 0x57, 0x3f, 0x19, 0xbb, 0x4f, 0x42,
  33240. 0x40, 0x28, 0x1f, 0x0b, 0x1f, 0x4f, 0x7b, 0xe8,
  33241. 0x57, 0xa4, 0xe5, 0x9d, 0x41, 0x6c, 0x06, 0xb4,
  33242. 0xc5, 0x0f, 0xa0, 0x9e, 0x18, 0x10, 0xdd, 0xc6,
  33243. 0xb1, 0x46, 0x7b, 0xae, 0xac, 0x5a, 0x36, 0x68,
  33244. 0xd1, 0x1b, 0x6e, 0xca, 0xa9, 0x01, 0x44, 0x00,
  33245. 0x16, 0xf3, 0x89, 0xf8, 0x0a, 0xcc, 0x4d, 0xb9,
  33246. 0x77, 0x02, 0x5e, 0x7f, 0x59, 0x24, 0x38, 0x8c,
  33247. 0x7e, 0x34, 0x0a, 0x73, 0x2e, 0x55, 0x44, 0x40,
  33248. 0xe7, 0x65, 0x70, 0xf8, 0xdd, 0x71, 0xb7, 0xd6,
  33249. 0x40, 0xb3, 0x45, 0x0d, 0x1f, 0xd5, 0xf0, 0x41,
  33250. 0x0a, 0x18, 0xf9, 0xa3, 0x49, 0x4f, 0x70, 0x7c,
  33251. 0x71, 0x7b, 0x79, 0xb4, 0xbf, 0x75, 0xc9, 0x84,
  33252. 0x00, 0xb0, 0x96, 0xb2, 0x16, 0x53, 0xb5, 0xd2,
  33253. 0x17, 0xcf, 0x35, 0x65, 0xc9, 0x59, 0x74, 0x56,
  33254. 0xf7, 0x07, 0x03, 0x49, 0x7a, 0x07, 0x87, 0x63,
  33255. 0x82, 0x9b, 0xc0, 0x1b, 0xb1, 0xcb, 0xc8, 0xfa,
  33256. 0x04, 0xea, 0xdc, 0x9a, 0x6e, 0x3f, 0x66, 0x99,
  33257. 0x58, 0x7a, 0x9e, 0x75, 0xc9, 0x4e, 0x5b, 0xab,
  33258. 0x00, 0x36, 0xe0, 0xb2, 0xe7, 0x11, 0x39, 0x2c,
  33259. 0xff, 0x00, 0x47, 0xd0, 0xd6, 0xb0, 0x5b, 0xd2,
  33260. 0xa5, 0x88, 0xbc, 0x10, 0x97, 0x18, 0x95, 0x42,
  33261. 0x59, 0xf1, 0xd8, 0x66, 0x78, 0xa5, 0x79, 0xa3,
  33262. 0x12, 0x0f, 0x19, 0xcf, 0xb2, 0x96, 0x3f, 0x17,
  33263. 0x7a, 0xeb, 0x70, 0xf2, 0xd4, 0x84, 0x48, 0x26,
  33264. 0x26, 0x2e, 0x51, 0xb8, 0x02, 0x71, 0x27, 0x20,
  33265. 0x68, 0xef, 0x5b, 0x38, 0x56, 0xfa, 0x85, 0x35,
  33266. 0xaa, 0x2a, 0x88, 0xb2, 0xd4, 0x1f, 0x2a, 0x0e,
  33267. 0x2f, 0xda, 0x76, 0x24, 0xc2, 0x85, 0x02, 0x72,
  33268. 0xac, 0x4a, 0x2f, 0x56, 0x1f, 0x8f, 0x2f, 0x7a,
  33269. 0x31, 0x8b, 0xfd, 0x5c, 0xaf, 0x96, 0x96, 0x14,
  33270. 0x9e, 0x4a, 0xc8, 0x24, 0xad, 0x34, 0x60, 0x53,
  33271. 0x8f, 0xdc, 0x25, 0x42, 0x1b, 0xee, 0xc2, 0xcc,
  33272. 0x68, 0x18, 0x16, 0x2d, 0x06, 0xbb, 0xed, 0x0c,
  33273. 0x40, 0xa3, 0x87, 0x19, 0x23, 0x49, 0xdb, 0x67,
  33274. 0xa1, 0x18, 0xba, 0xda, 0x6c, 0xd5, 0xab, 0x01,
  33275. 0x40, 0xee, 0x27, 0x32, 0x04, 0xf6, 0x28, 0xaa,
  33276. 0xd1, 0xc1, 0x35, 0xf7, 0x70, 0x27, 0x9a, 0x65,
  33277. 0x1e, 0x24, 0xd8, 0xc1, 0x4d, 0x75, 0xa6, 0x05,
  33278. 0x9d, 0x76, 0xb9, 0x6a, 0x6f, 0xd8, 0x57, 0xde,
  33279. 0xf5, 0xe0, 0xb3, 0x54, 0xb2, 0x7a, 0xb9, 0x37,
  33280. 0xa5, 0x81, 0x5d, 0x16, 0xb5, 0xfa, 0xe4, 0x07,
  33281. 0xff, 0x18, 0x22, 0x2c, 0x6d, 0x1e, 0xd2, 0x63,
  33282. 0xbe, 0x68, 0xc9, 0x5f, 0x32, 0xd9, 0x08, 0xbd,
  33283. 0x89, 0x5c, 0xd7, 0x62, 0x07, 0xae, 0x72, 0x64,
  33284. 0x87, 0x56, 0x7f, 0x9a, 0x67, 0xda, 0xd7, 0x9a,
  33285. 0xbe, 0xc3, 0x16, 0xf6, 0x83, 0xb1, 0x7f, 0x2d,
  33286. 0x02, 0xbf, 0x07, 0xe0, 0xac, 0x8b, 0x5b, 0xc6,
  33287. 0x16, 0x2c, 0xf9, 0x46, 0x97, 0xb3, 0xc2, 0x7c,
  33288. 0xd1, 0xfe, 0xa4, 0x9b, 0x27, 0xf2, 0x3b, 0xa2,
  33289. 0x90, 0x18, 0x71, 0x96, 0x25, 0x06, 0x52, 0x0c,
  33290. 0x39, 0x2d, 0xa8, 0xb6, 0xad, 0x0d, 0x99, 0xf7,
  33291. 0x01, 0x3f, 0xbc, 0x06, 0xc2, 0xc1, 0x7a, 0x56,
  33292. 0x95, 0x00, 0xc8, 0xa7, 0x69, 0x64, 0x81, 0xc1,
  33293. 0xcd, 0x33, 0xe9, 0xb1, 0x4e, 0x40, 0xb8, 0x2e,
  33294. 0x79, 0xa5, 0xf5, 0xdb, 0x82, 0x57, 0x1b, 0xa9,
  33295. 0x7b, 0xae, 0x3a, 0xd3, 0xe0, 0x47, 0x95, 0x15,
  33296. 0xbb, 0x0e, 0x2b, 0x0f, 0x3b, 0xfc, 0xd1, 0xfd,
  33297. 0x33, 0x03, 0x4e, 0xfc, 0x62, 0x45, 0xed, 0xdd,
  33298. 0x7e, 0xe2, 0x08, 0x6d, 0xda, 0xe2, 0x60, 0x0d,
  33299. 0x8c, 0xa7, 0x3e, 0x21, 0x4e, 0x8c, 0x2b, 0x0b,
  33300. 0xdb, 0x2b, 0x04, 0x7c, 0x6a, 0x46, 0x4a, 0x56,
  33301. 0x2e, 0xd7, 0x7b, 0x73, 0xd2, 0xd8, 0x41, 0xc4,
  33302. 0xb3, 0x49, 0x73, 0x55, 0x12, 0x57, 0x71, 0x3b,
  33303. 0x75, 0x36, 0x32, 0xef, 0xba, 0x34, 0x81, 0x69,
  33304. 0xab, 0xc9, 0x0a, 0x68, 0xf4, 0x26, 0x11, 0xa4,
  33305. 0x01, 0x26, 0xd7, 0xcb, 0x21, 0xb5, 0x86, 0x95,
  33306. 0x56, 0x81, 0x86, 0xf7, 0xe5, 0x69, 0xd2, 0xff,
  33307. 0x0f, 0x9e, 0x74, 0x5d, 0x04, 0x87, 0xdd, 0x2e,
  33308. 0xb9, 0x97, 0xca, 0xfc, 0x5a, 0xbf, 0x9d, 0xd1,
  33309. 0x02, 0xe6, 0x2f, 0xf6, 0x6c, 0xba, 0x87
  33310. };
  33311. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  33312. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  33313. sizeof(msg2),
  33314. sizeof(msg3),
  33315. 0 /*sizeof(msg1)*/,
  33316. 0 /*sizeof(msg1)*/,
  33317. sizeof(msg4)
  33318. };
  33319. #ifndef NO_ASN
  33320. static const byte privateEd448[] = {
  33321. 0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
  33322. 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04, 0x39,
  33323. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  33324. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  33325. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  33326. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  33327. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  33328. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  33329. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  33330. 0x5b
  33331. };
  33332. static const byte publicEd448[] = {
  33333. 0x30, 0x43, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
  33334. 0x71, 0x03, 0x3a, 0x00, 0x5f, 0xd7, 0x44, 0x9b,
  33335. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  33336. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  33337. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  33338. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  33339. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  33340. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  33341. 0xaf, 0xe8, 0x25, 0x61, 0x80
  33342. };
  33343. static const byte privPubEd448[] = {
  33344. 0x30, 0x81, 0x82, 0x02, 0x01, 0x00, 0x30, 0x05,
  33345. 0x06, 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04,
  33346. 0x39, 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d,
  33347. 0x10, 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e,
  33348. 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c,
  33349. 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48,
  33350. 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04,
  33351. 0x4e, 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f,
  33352. 0x8f, 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98,
  33353. 0xf9, 0x5b, 0x81, 0x39, 0x5f, 0xd7, 0x44, 0x9b,
  33354. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  33355. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  33356. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  33357. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  33358. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  33359. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  33360. 0xaf, 0xe8, 0x25, 0x61, 0x80
  33361. };
  33362. word32 idx;
  33363. #endif /* NO_ASN */
  33364. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  33365. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  33366. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33367. ed448_key *key3 = NULL;
  33368. #else
  33369. ed448_key key3[1];
  33370. #endif
  33371. #endif
  33372. WOLFSSL_ENTER("ed448_test");
  33373. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33374. key = (ed448_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33375. key2 = (ed448_key *)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33376. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  33377. key3 = (ed448_key *)XMALLOC(sizeof(*key3), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33378. #endif
  33379. #endif
  33380. /* create ed448 keys */
  33381. #ifndef HAVE_FIPS
  33382. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  33383. #else
  33384. ret = wc_InitRng(&rng);
  33385. #endif
  33386. if (ret != 0) {
  33387. XMEMSET(&rng, 0, sizeof(rng));
  33388. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33389. }
  33390. ret = wc_ed448_init(key);
  33391. if (ret < 0)
  33392. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33393. ret = wc_ed448_init(key2);
  33394. if (ret < 0)
  33395. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33396. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  33397. ret = wc_ed448_init(key3);
  33398. if (ret < 0)
  33399. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33400. #endif
  33401. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, key);
  33402. if (ret < 0)
  33403. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33404. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, key2);
  33405. if (ret < 0)
  33406. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33407. /* helper functions for signature and key size */
  33408. keySz = (word32)wc_ed448_size(key);
  33409. sigSz = (word32)wc_ed448_sig_size(key);
  33410. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  33411. defined(HAVE_ED448_KEY_IMPORT)
  33412. for (i = 0; i < 6; i++) {
  33413. outlen = sizeof(out);
  33414. XMEMSET(out, 0, sizeof(out));
  33415. if (wc_ed448_import_private_key(sKeys[i], ED448_KEY_SIZE, pKeys[i],
  33416. pKeySz[i], key) != 0)
  33417. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33418. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key, NULL,
  33419. 0) != 0)
  33420. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33421. if (XMEMCMP(out, sigs[i], 114))
  33422. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33423. #if defined(HAVE_ED448_VERIFY)
  33424. /* test verify on good msg */
  33425. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  33426. NULL, 0) != 0 || verify != 1)
  33427. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33428. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  33429. /* test verify on good msg using streaming interface directly */
  33430. if (wc_ed448_verify_msg_init(out, outlen,
  33431. key, (byte)Ed448, NULL, 0) != 0)
  33432. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33433. for (j = 0; j < msgSz[i]; j += i) {
  33434. if (wc_ed448_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), key) != 0)
  33435. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33436. }
  33437. if (wc_ed448_verify_msg_final(out, outlen, &verify,
  33438. key) != 0 || verify != 1)
  33439. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33440. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  33441. /* test verify on bad msg */
  33442. out[outlen-2] = out[outlen-2] + 1;
  33443. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  33444. NULL, 0) == 0 || verify == 1)
  33445. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33446. #endif /* HAVE_ED448_VERIFY */
  33447. /* test api for import/exporting keys */
  33448. {
  33449. byte *exportPKey = NULL;
  33450. byte *exportSKey = NULL;
  33451. word32 exportPSz = ED448_KEY_SIZE;
  33452. word32 exportSSz = ED448_KEY_SIZE;
  33453. exportPKey = (byte *)XMALLOC(exportPSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33454. exportSKey = (byte *)XMALLOC(exportSSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33455. if ((exportPKey == NULL) || (exportSKey == NULL))
  33456. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33457. ret = 0;
  33458. do {
  33459. if (wc_ed448_export_public(key, exportPKey, &exportPSz) != 0) {
  33460. ret = WC_TEST_RET_ENC_I(i);
  33461. break;
  33462. }
  33463. if (wc_ed448_import_public_ex(exportPKey, exportPSz, key2, 1) != 0) {
  33464. ret = WC_TEST_RET_ENC_I(i);
  33465. break;
  33466. }
  33467. if (wc_ed448_export_private_only(key, exportSKey, &exportSSz) != 0) {
  33468. ret = WC_TEST_RET_ENC_I(i);
  33469. break;
  33470. }
  33471. if (wc_ed448_import_private_key(exportSKey, exportSSz,
  33472. exportPKey, exportPSz, key2) != 0) {
  33473. ret = WC_TEST_RET_ENC_I(i);
  33474. break;
  33475. }
  33476. /* clear "out" buffer and test sign with imported keys */
  33477. outlen = sizeof(out);
  33478. XMEMSET(out, 0, sizeof(out));
  33479. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key2, NULL,
  33480. 0) != 0) {
  33481. ret = WC_TEST_RET_ENC_I(i);
  33482. break;
  33483. }
  33484. } while(0);
  33485. XFREE(exportPKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33486. XFREE(exportSKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33487. if (ret != 0)
  33488. goto out;
  33489. }
  33490. #if defined(HAVE_ED448_VERIFY)
  33491. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key2,
  33492. NULL, 0) != 0 || verify != 1)
  33493. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33494. if (XMEMCMP(out, sigs[i], SIGSZ))
  33495. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  33496. #endif /* HAVE_ED448_VERIFY */
  33497. }
  33498. ret = ed448_ctx_test();
  33499. if (ret != 0)
  33500. goto out;
  33501. ret = ed448ph_test();
  33502. if (ret != 0)
  33503. goto out;
  33504. #ifndef NO_ASN
  33505. /* Try ASN.1 encoded private-only key and public key. */
  33506. idx = 0;
  33507. ret = wc_Ed448PrivateKeyDecode(privateEd448, &idx, key3,
  33508. sizeof(privateEd448));
  33509. if (ret != 0)
  33510. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33511. ret = wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0);
  33512. if (ret != BAD_FUNC_ARG)
  33513. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33514. idx = 0;
  33515. ret = wc_Ed448PublicKeyDecode(publicEd448, &idx, key3, sizeof(publicEd448));
  33516. if (ret != 0)
  33517. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33518. ret = wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0);
  33519. if (ret != 0)
  33520. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33521. if (XMEMCMP(out, sigs[0], SIGSZ))
  33522. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33523. #if defined(HAVE_ED448_VERIFY)
  33524. /* test verify on good msg */
  33525. ret = wc_ed448_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, key3,
  33526. NULL, 0);
  33527. if (ret != 0 || verify != 1)
  33528. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33529. #endif /* HAVE_ED448_VERIFY */
  33530. wc_ed448_free(key3);
  33531. ret = wc_ed448_init(key3);
  33532. if (ret < 0)
  33533. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33534. idx = 0;
  33535. ret = wc_Ed448PrivateKeyDecode(privPubEd448, &idx, key3,
  33536. sizeof(privPubEd448));
  33537. if (ret != 0)
  33538. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33539. ret = wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0);
  33540. if (ret != 0)
  33541. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33542. if (XMEMCMP(out, sigs[0], SIGSZ))
  33543. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33544. #endif /* NO_ASN */
  33545. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  33546. ret = 0;
  33547. out:
  33548. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33549. if (key) {
  33550. wc_ed448_free(key);
  33551. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33552. }
  33553. if (key2) {
  33554. wc_ed448_free(key2);
  33555. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33556. }
  33557. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  33558. if (key3) {
  33559. wc_ed448_free(key3);
  33560. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33561. }
  33562. #endif
  33563. #else
  33564. wc_ed448_free(key);
  33565. wc_ed448_free(key2);
  33566. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  33567. wc_ed448_free(key3);
  33568. #endif
  33569. #endif
  33570. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  33571. wc_FreeRng(&rng);
  33572. #endif
  33573. if (ret < 0)
  33574. return ret;
  33575. /* hush warnings of unused keySz and sigSz */
  33576. (void)keySz;
  33577. (void)sigSz;
  33578. ret = ed448_test_check_key();
  33579. if (ret < 0)
  33580. return ret;
  33581. #ifdef WOLFSSL_TEST_CERT
  33582. ret = ed448_test_cert();
  33583. if (ret < 0)
  33584. return ret;
  33585. #ifdef WOLFSSL_CERT_GEN
  33586. ret = ed448_test_make_cert();
  33587. if (ret < 0)
  33588. return ret;
  33589. #endif /* WOLFSSL_CERT_GEN */
  33590. #endif /* WOLFSSL_TEST_CERT */
  33591. return 0;
  33592. }
  33593. #endif /* HAVE_ED448 */
  33594. #ifdef WOLFSSL_HAVE_KYBER
  33595. #ifdef WOLFSSL_WC_KYBER /* OQS and PQM4 do not support KATs */
  33596. #ifdef WOLFSSL_KYBER512
  33597. static wc_test_ret_t kyber512_kat(void)
  33598. {
  33599. wc_test_ret_t ret;
  33600. #ifdef WOLFSSL_SMALL_STACK
  33601. KyberKey *key = NULL;
  33602. byte *priv = NULL;
  33603. byte *pub = NULL;
  33604. byte *ct = NULL;
  33605. byte *ss = NULL;
  33606. byte *ss_dec = NULL;
  33607. #else
  33608. KyberKey key[1];
  33609. byte priv[KYBER512_PRIVATE_KEY_SIZE];
  33610. byte pub[KYBER512_PUBLIC_KEY_SIZE];
  33611. byte ct[KYBER512_CIPHER_TEXT_SIZE];
  33612. byte ss[KYBER_SS_SZ];
  33613. byte ss_dec[KYBER_SS_SZ];
  33614. #endif
  33615. int key_inited = 0;
  33616. WOLFSSL_SMALL_STACK_STATIC const byte kyber512_rand[] = {
  33617. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  33618. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  33619. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  33620. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  33621. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  33622. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  33623. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  33624. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  33625. };
  33626. WOLFSSL_SMALL_STACK_STATIC const byte kyber512enc_rand[] = {
  33627. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  33628. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  33629. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  33630. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  33631. };
  33632. WOLFSSL_SMALL_STACK_STATIC const byte kyber512_pk[] = {
  33633. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  33634. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  33635. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  33636. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  33637. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  33638. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  33639. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  33640. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  33641. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  33642. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  33643. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  33644. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  33645. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  33646. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  33647. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  33648. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  33649. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  33650. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  33651. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  33652. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  33653. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  33654. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  33655. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  33656. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  33657. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  33658. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  33659. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  33660. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  33661. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  33662. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  33663. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  33664. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  33665. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  33666. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  33667. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  33668. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  33669. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  33670. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  33671. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  33672. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  33673. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  33674. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  33675. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  33676. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  33677. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  33678. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  33679. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  33680. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  33681. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  33682. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  33683. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  33684. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  33685. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  33686. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  33687. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  33688. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  33689. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  33690. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  33691. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  33692. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  33693. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  33694. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  33695. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  33696. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  33697. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  33698. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  33699. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  33700. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  33701. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  33702. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  33703. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  33704. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  33705. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  33706. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  33707. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  33708. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  33709. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  33710. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  33711. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  33712. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  33713. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  33714. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  33715. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  33716. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  33717. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  33718. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  33719. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  33720. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  33721. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  33722. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  33723. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  33724. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  33725. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  33726. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  33727. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  33728. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  33729. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  33730. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  33731. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  33732. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  33733. };
  33734. WOLFSSL_SMALL_STACK_STATIC const byte kyber512_sk[] = {
  33735. 0x6C, 0x89, 0x2B, 0x02, 0x97, 0xA9, 0xC7, 0x64,
  33736. 0x14, 0x93, 0xF8, 0x7D, 0xAF, 0x35, 0x33, 0xEE,
  33737. 0xD6, 0x1F, 0x07, 0xF4, 0x65, 0x20, 0x66, 0x33,
  33738. 0x7E, 0xD7, 0x40, 0x46, 0xDC, 0xC7, 0x1B, 0xA0,
  33739. 0x3F, 0x30, 0x96, 0x01, 0x03, 0x16, 0x1F, 0x7D,
  33740. 0xEB, 0x53, 0xA7, 0x1B, 0x11, 0x61, 0x72, 0x63,
  33741. 0xFE, 0x2A, 0x80, 0x97, 0x69, 0xCE, 0x6D, 0x70,
  33742. 0xA8, 0x5F, 0xE6, 0x00, 0xEC, 0xE2, 0x9D, 0x7F,
  33743. 0x36, 0xA1, 0x6D, 0x33, 0x1B, 0x8B, 0x2A, 0x9E,
  33744. 0x1D, 0xB8, 0xC0, 0x90, 0x74, 0x2D, 0xF0, 0x73,
  33745. 0x9F, 0xF0, 0x60, 0xCE, 0xB4, 0xEC, 0xC5, 0xAB,
  33746. 0x1C, 0x5E, 0x55, 0xAC, 0x97, 0xBB, 0x66, 0xA7,
  33747. 0xF8, 0x95, 0x10, 0x5D, 0x57, 0x78, 0x2B, 0x22,
  33748. 0x95, 0x38, 0xE3, 0x42, 0x15, 0x44, 0xA3, 0x42,
  33749. 0x14, 0x08, 0xDB, 0xF4, 0x49, 0x10, 0x93, 0x4C,
  33750. 0xC4, 0x23, 0x77, 0x4F, 0x16, 0x76, 0xFF, 0x1C,
  33751. 0x30, 0x6F, 0x97, 0x55, 0x5F, 0x57, 0xB4, 0xAE,
  33752. 0xD7, 0xA6, 0xBA, 0xB9, 0x50, 0xA8, 0x16, 0x3C,
  33753. 0x8D, 0x31, 0x8D, 0xEA, 0x62, 0x75, 0x1B, 0xD6,
  33754. 0xAB, 0xC5, 0x06, 0x9C, 0x06, 0xC8, 0x8F, 0x33,
  33755. 0x00, 0x26, 0xA1, 0x98, 0x06, 0xA0, 0x3B, 0x97,
  33756. 0xA7, 0x69, 0x6B, 0x56, 0xDA, 0x21, 0x82, 0x7B,
  33757. 0xB4, 0xE8, 0xDC, 0x03, 0x11, 0x52, 0xB4, 0x1B,
  33758. 0x89, 0x2A, 0x9E, 0x99, 0xAD, 0xF6, 0xE1, 0x96,
  33759. 0x3E, 0x96, 0x57, 0x88, 0x28, 0x15, 0x4F, 0x46,
  33760. 0x70, 0x33, 0x84, 0x69, 0x20, 0xFB, 0xB4, 0xB8,
  33761. 0x05, 0x44, 0xE7, 0xE8, 0xA8, 0x1A, 0xE9, 0x63,
  33762. 0xCF, 0x36, 0x8C, 0x9B, 0xA0, 0x37, 0xA8, 0xC2,
  33763. 0xAD, 0x62, 0xE3, 0x2B, 0x6E, 0x61, 0xC9, 0x1D,
  33764. 0x75, 0xCE, 0x00, 0x5A, 0xB3, 0x0F, 0x80, 0x99,
  33765. 0xA1, 0xF2, 0x9D, 0x7B, 0x63, 0x05, 0xB4, 0xDC,
  33766. 0x06, 0xE2, 0x56, 0x80, 0xBB, 0x00, 0x99, 0x2F,
  33767. 0x71, 0x7F, 0xE6, 0xC1, 0x15, 0xA8, 0x08, 0x42,
  33768. 0x31, 0xCC, 0x79, 0xDD, 0x70, 0x0E, 0xA6, 0x91,
  33769. 0x2A, 0xC7, 0xFA, 0x0D, 0x93, 0x7B, 0xB6, 0xA7,
  33770. 0x56, 0x66, 0x22, 0x30, 0x47, 0x0C, 0x18, 0x9B,
  33771. 0x5A, 0xA1, 0x65, 0x3D, 0xEB, 0x93, 0x7D, 0x5A,
  33772. 0x9C, 0x25, 0xA2, 0x1D, 0x93, 0xB1, 0x90, 0x74,
  33773. 0xFC, 0x23, 0x9D, 0x81, 0x53, 0x53, 0x97, 0x97,
  33774. 0xC7, 0xD4, 0xAB, 0x62, 0x64, 0x9D, 0x76, 0xAA,
  33775. 0x55, 0x37, 0x36, 0xA9, 0x49, 0x02, 0x2C, 0x22,
  33776. 0xC5, 0x2B, 0xAE, 0xEC, 0x60, 0x5B, 0x32, 0xCE,
  33777. 0x9E, 0x5B, 0x93, 0x84, 0x90, 0x35, 0x58, 0xCA,
  33778. 0x9D, 0x6A, 0x3A, 0xBA, 0x90, 0x42, 0x3E, 0xED,
  33779. 0xA0, 0x1C, 0x94, 0x19, 0x8B, 0x19, 0x2A, 0x8B,
  33780. 0xA9, 0x06, 0x34, 0x97, 0xA0, 0xC5, 0x01, 0x33,
  33781. 0x07, 0xDD, 0xD8, 0x63, 0x52, 0x64, 0x71, 0xA4,
  33782. 0xD9, 0x95, 0x23, 0xEB, 0x41, 0x7F, 0x29, 0x1A,
  33783. 0xAC, 0x0C, 0x3A, 0x58, 0x1B, 0x6D, 0xA0, 0x07,
  33784. 0x32, 0xE5, 0xE8, 0x1B, 0x1F, 0x7C, 0x87, 0x9B,
  33785. 0x16, 0x93, 0xC1, 0x3B, 0x6F, 0x9F, 0x79, 0x31,
  33786. 0x62, 0x24, 0x29, 0xE5, 0x42, 0xAF, 0x40, 0x69,
  33787. 0x22, 0x2F, 0x04, 0x55, 0x44, 0xE0, 0xCC, 0x4F,
  33788. 0xB2, 0x4D, 0x44, 0x48, 0xCF, 0x2C, 0x65, 0x96,
  33789. 0xF5, 0xCB, 0x08, 0x62, 0x4B, 0x11, 0x85, 0x01,
  33790. 0x3B, 0x6B, 0x02, 0x08, 0x92, 0xF9, 0x6B, 0xDF,
  33791. 0xD4, 0xAD, 0xA9, 0x17, 0x9D, 0xE7, 0x27, 0xB8,
  33792. 0xD9, 0x42, 0x6E, 0x09, 0x96, 0xB5, 0xD3, 0x49,
  33793. 0x48, 0xCE, 0x02, 0xD0, 0xC3, 0x69, 0xB3, 0x7C,
  33794. 0xBB, 0x54, 0xD3, 0x47, 0x9E, 0xD8, 0xB5, 0x82,
  33795. 0xE9, 0xE7, 0x28, 0x92, 0x9B, 0x4C, 0x71, 0xC9,
  33796. 0xBE, 0x11, 0xD4, 0x5B, 0x20, 0xC4, 0xBD, 0xC3,
  33797. 0xC7, 0x43, 0x13, 0x22, 0x3F, 0x58, 0x27, 0x4E,
  33798. 0x8B, 0xA5, 0x24, 0x44, 0x47, 0xC4, 0x95, 0x95,
  33799. 0x0B, 0x84, 0xCB, 0x0C, 0x3C, 0x27, 0x36, 0x40,
  33800. 0x10, 0x8A, 0x33, 0x97, 0x94, 0x45, 0x73, 0x27,
  33801. 0x93, 0x28, 0x99, 0x6C, 0xDC, 0x0C, 0x91, 0x3C,
  33802. 0x95, 0x8A, 0xD6, 0x20, 0xBA, 0x8B, 0x5E, 0x5E,
  33803. 0xCB, 0xBB, 0x7E, 0x13, 0xCB, 0x9C, 0x70, 0xBD,
  33804. 0x5A, 0xB3, 0x0E, 0xB7, 0x48, 0x8C, 0x97, 0x00,
  33805. 0x1C, 0x20, 0x49, 0x8F, 0x1D, 0x7C, 0xC0, 0x6D,
  33806. 0xA7, 0x6B, 0xF5, 0x20, 0xC6, 0x58, 0xCC, 0xAD,
  33807. 0xFA, 0x29, 0x56, 0x42, 0x45, 0x57, 0xAB, 0xEA,
  33808. 0x8A, 0xB8, 0x92, 0x39, 0xC1, 0x78, 0x33, 0xDC,
  33809. 0x3A, 0x49, 0xB3, 0x6A, 0x9A, 0xE9, 0xA4, 0x86,
  33810. 0x94, 0x05, 0x40, 0xEB, 0x44, 0x4F, 0x97, 0x15,
  33811. 0x23, 0x57, 0xE0, 0x20, 0x35, 0x93, 0x9D, 0x75,
  33812. 0xA3, 0xC0, 0x25, 0xF4, 0x1A, 0x40, 0x08, 0x23,
  33813. 0x82, 0xA0, 0x73, 0x3C, 0x39, 0xB0, 0x62, 0x2B,
  33814. 0x74, 0x0E, 0x40, 0x75, 0x92, 0xC6, 0x2E, 0xCA,
  33815. 0xEB, 0x14, 0x32, 0xC4, 0x45, 0xB3, 0x70, 0x3A,
  33816. 0x86, 0xF6, 0x98, 0x1A, 0x27, 0x81, 0x57, 0xEA,
  33817. 0x95, 0xA6, 0xE9, 0x2D, 0x55, 0xE4, 0xB9, 0x72,
  33818. 0xF9, 0x36, 0xC2, 0xF0, 0xA6, 0x58, 0x28, 0x0E,
  33819. 0xA2, 0xB0, 0x7A, 0x48, 0x99, 0x2D, 0xF8, 0x93,
  33820. 0x7E, 0x0A, 0x2A, 0xC1, 0xDC, 0xC9, 0x74, 0xFE,
  33821. 0x00, 0xAA, 0xE1, 0xF5, 0x61, 0xFA, 0x25, 0x8E,
  33822. 0x2D, 0x25, 0x9C, 0x3E, 0x86, 0x1D, 0xCE, 0x23,
  33823. 0x60, 0x39, 0x12, 0x76, 0x06, 0xFC, 0x1C, 0xE0,
  33824. 0x09, 0x00, 0x3A, 0x7B, 0xAC, 0x94, 0x21, 0x01,
  33825. 0xDC, 0xB8, 0x22, 0xB1, 0xF3, 0xC1, 0x2B, 0xF7,
  33826. 0x32, 0x38, 0xF5, 0x46, 0xE0, 0x1C, 0x36, 0xB5,
  33827. 0xA6, 0x93, 0x61, 0x92, 0x99, 0x5C, 0xC6, 0x9C,
  33828. 0x63, 0x23, 0x74, 0x09, 0xCB, 0x53, 0xC2, 0xE3,
  33829. 0x5D, 0x74, 0x89, 0x0D, 0x18, 0x88, 0x53, 0x76,
  33830. 0xFA, 0x55, 0x03, 0xB1, 0x07, 0xA2, 0xA3, 0x92,
  33831. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  33832. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  33833. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  33834. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  33835. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  33836. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  33837. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  33838. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  33839. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  33840. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  33841. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  33842. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  33843. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  33844. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  33845. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  33846. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  33847. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  33848. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  33849. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  33850. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  33851. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  33852. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  33853. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  33854. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  33855. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  33856. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  33857. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  33858. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  33859. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  33860. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  33861. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  33862. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  33863. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  33864. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  33865. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  33866. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  33867. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  33868. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  33869. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  33870. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  33871. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  33872. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  33873. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  33874. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  33875. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  33876. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  33877. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  33878. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  33879. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  33880. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  33881. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  33882. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  33883. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  33884. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  33885. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  33886. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  33887. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  33888. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  33889. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  33890. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  33891. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  33892. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  33893. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  33894. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  33895. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  33896. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  33897. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  33898. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  33899. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  33900. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  33901. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  33902. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  33903. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  33904. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  33905. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  33906. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  33907. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  33908. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  33909. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  33910. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  33911. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  33912. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  33913. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  33914. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  33915. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  33916. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  33917. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  33918. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  33919. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  33920. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  33921. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  33922. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  33923. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  33924. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  33925. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  33926. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  33927. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  33928. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  33929. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  33930. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  33931. 0x7F, 0xFA, 0xD1, 0xBC, 0x8A, 0xF7, 0x3B, 0x7E,
  33932. 0x87, 0x49, 0x56, 0xB8, 0x1C, 0x2A, 0x2E, 0xF0,
  33933. 0xBF, 0xAB, 0xE8, 0xDC, 0x93, 0xD7, 0x7B, 0x2F,
  33934. 0xBC, 0x9E, 0x0C, 0x64, 0xEF, 0xA0, 0x1E, 0x84,
  33935. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  33936. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  33937. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  33938. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  33939. };
  33940. WOLFSSL_SMALL_STACK_STATIC const byte kyber512_ct[] = {
  33941. #ifndef WOLFSSL_ML_KEM
  33942. 0xED, 0xF2, 0x41, 0x45, 0xE4, 0x3B, 0x4F, 0x6D,
  33943. 0xC6, 0xBF, 0x83, 0x32, 0xF5, 0x4E, 0x02, 0xCA,
  33944. 0xB0, 0x2D, 0xBF, 0x3B, 0x56, 0x05, 0xDD, 0xC9,
  33945. 0x0A, 0x15, 0xC8, 0x86, 0xAD, 0x3E, 0xD4, 0x89,
  33946. 0x46, 0x26, 0x99, 0xE4, 0xAB, 0xED, 0x44, 0x35,
  33947. 0x0B, 0xC3, 0x75, 0x7E, 0x26, 0x96, 0xFB, 0xFB,
  33948. 0x25, 0x34, 0x41, 0x2E, 0x8D, 0xD2, 0x01, 0xF1,
  33949. 0xE4, 0x54, 0x0A, 0x39, 0x70, 0xB0, 0x55, 0xFE,
  33950. 0x3B, 0x0B, 0xEC, 0x3A, 0x71, 0xF9, 0xE1, 0x15,
  33951. 0xB3, 0xF9, 0xF3, 0x91, 0x02, 0x06, 0x5B, 0x1C,
  33952. 0xCA, 0x83, 0x14, 0xDC, 0xC7, 0x95, 0xE3, 0xC0,
  33953. 0xE8, 0xFA, 0x98, 0xEE, 0x83, 0xCA, 0x66, 0x28,
  33954. 0x45, 0x70, 0x28, 0xA4, 0xD0, 0x9E, 0x83, 0x9E,
  33955. 0x55, 0x48, 0x62, 0xCF, 0x0B, 0x7B, 0xF5, 0x6C,
  33956. 0x5C, 0x0A, 0x82, 0x9E, 0x86, 0x57, 0x94, 0x79,
  33957. 0x45, 0xFE, 0x9C, 0x22, 0x56, 0x4F, 0xBA, 0xEB,
  33958. 0xC1, 0xB3, 0xAF, 0x35, 0x0D, 0x79, 0x55, 0x50,
  33959. 0x8A, 0x26, 0xD8, 0xA8, 0xEB, 0x54, 0x7B, 0x8B,
  33960. 0x1A, 0x2C, 0xF0, 0x3C, 0xCA, 0x1A, 0xAB, 0xCE,
  33961. 0x6C, 0x34, 0x97, 0x78, 0x3B, 0x64, 0x65, 0xBA,
  33962. 0x0B, 0x6E, 0x7A, 0xCB, 0xA8, 0x21, 0x19, 0x51,
  33963. 0x24, 0xAE, 0xF0, 0x9E, 0x62, 0x83, 0x82, 0xA1,
  33964. 0xF9, 0x14, 0x04, 0x3B, 0xE7, 0x09, 0x6E, 0x95,
  33965. 0x2C, 0xBC, 0x4F, 0xB4, 0xAF, 0xED, 0x13, 0x60,
  33966. 0x90, 0x46, 0x11, 0x7C, 0x01, 0x1F, 0xD7, 0x41,
  33967. 0xEE, 0x28, 0x6C, 0x83, 0x77, 0x16, 0x90, 0xF0,
  33968. 0xAE, 0xB5, 0x0D, 0xA0, 0xD7, 0x12, 0x85, 0xA1,
  33969. 0x79, 0xB2, 0x15, 0xC6, 0x03, 0x6D, 0xEB, 0x78,
  33970. 0x0F, 0x4D, 0x16, 0x76, 0x9F, 0x72, 0xDE, 0x16,
  33971. 0xFD, 0xAD, 0xAC, 0x73, 0xBE, 0xFA, 0x5B, 0xEF,
  33972. 0x89, 0x43, 0x19, 0x7F, 0x44, 0xC5, 0x95, 0x89,
  33973. 0xDC, 0x9F, 0x49, 0x73, 0xDE, 0x14, 0x50, 0xBA,
  33974. 0x1D, 0x0C, 0x32, 0x90, 0xD6, 0xB1, 0xD6, 0x83,
  33975. 0xF2, 0x94, 0xE7, 0x59, 0xC9, 0x54, 0xAB, 0xE8,
  33976. 0xA7, 0xDA, 0x5B, 0x10, 0x54, 0xFD, 0x6D, 0x21,
  33977. 0x32, 0x9B, 0x8E, 0x73, 0xD3, 0x75, 0x6A, 0xFD,
  33978. 0xA0, 0xDC, 0xB1, 0xFC, 0x8B, 0x15, 0x82, 0xD1,
  33979. 0xF9, 0x0C, 0xF2, 0x75, 0xA1, 0x02, 0xAB, 0xC6,
  33980. 0xAC, 0x69, 0x9D, 0xF0, 0xC5, 0x87, 0x0E, 0x50,
  33981. 0xA1, 0xF9, 0x89, 0xE4, 0xE6, 0x24, 0x1B, 0x60,
  33982. 0xAA, 0xA2, 0xEC, 0xF9, 0xE8, 0xE3, 0x3E, 0x0F,
  33983. 0xFC, 0xF4, 0x0F, 0xE8, 0x31, 0xE8, 0xFD, 0xC2,
  33984. 0xE8, 0x3B, 0x52, 0xCA, 0x7A, 0xB6, 0xD9, 0x3F,
  33985. 0x14, 0x6D, 0x29, 0xDC, 0xA5, 0x3C, 0x7D, 0xA1,
  33986. 0xDB, 0x4A, 0xC4, 0xF2, 0xDB, 0x39, 0xEA, 0x12,
  33987. 0x0D, 0x90, 0xFA, 0x60, 0xF4, 0xD4, 0x37, 0xC6,
  33988. 0xD0, 0x0E, 0xF4, 0x83, 0xBC, 0x94, 0xA3, 0x17,
  33989. 0x5C, 0xDA, 0x16, 0x3F, 0xC1, 0xC2, 0x82, 0x8B,
  33990. 0xE4, 0xDB, 0xD6, 0x43, 0x05, 0x07, 0xB5, 0x84,
  33991. 0xBB, 0x51, 0x77, 0xE1, 0x71, 0xB8, 0xDD, 0xA9,
  33992. 0xA4, 0x29, 0x3C, 0x32, 0x00, 0x29, 0x5C, 0x80,
  33993. 0x3A, 0x86, 0x5D, 0x6D, 0x21, 0x66, 0xF6, 0x6B,
  33994. 0xA5, 0x40, 0x1F, 0xB7, 0xA0, 0xE8, 0x53, 0x16,
  33995. 0x86, 0x00, 0xA2, 0x94, 0x84, 0x37, 0xE0, 0x36,
  33996. 0xE3, 0xBF, 0x19, 0xE1, 0x2F, 0xD3, 0xF2, 0xA2,
  33997. 0xB8, 0xB3, 0x43, 0xF7, 0x84, 0x24, 0x8E, 0x8D,
  33998. 0x68, 0x5E, 0xB0, 0xAF, 0xDE, 0x63, 0x15, 0x33,
  33999. 0x87, 0x30, 0xE7, 0xA1, 0x00, 0x1C, 0x27, 0xD8,
  34000. 0xD2, 0xA7, 0x6F, 0xA6, 0x9D, 0x15, 0x7B, 0xA1,
  34001. 0xAC, 0x7A, 0xD5, 0x6D, 0xA5, 0xA8, 0xC7, 0x0F,
  34002. 0xE4, 0xB5, 0xB8, 0xD7, 0x86, 0xDC, 0x6F, 0xC0,
  34003. 0x56, 0x6B, 0xA8, 0xE1, 0xB8, 0x81, 0x63, 0x34,
  34004. 0xD3, 0x2A, 0x3F, 0xB1, 0xCE, 0x7D, 0x4D, 0x5E,
  34005. 0x4C, 0x33, 0x2A, 0xF7, 0xB0, 0x03, 0xD0, 0x91,
  34006. 0x74, 0x1A, 0x3D, 0x5C, 0x96, 0x52, 0x92, 0x25,
  34007. 0x5D, 0xFF, 0x8E, 0xD2, 0xBB, 0xF1, 0xF9, 0x11,
  34008. 0x6B, 0xE5, 0x0C, 0x17, 0xB8, 0xE5, 0x48, 0x74,
  34009. 0x8A, 0xD4, 0xB2, 0xE9, 0x57, 0xBB, 0xD1, 0x95,
  34010. 0x34, 0x82, 0xA2, 0xE1, 0x71, 0x8C, 0xEC, 0x66,
  34011. 0xCD, 0x2C, 0x81, 0xF5, 0x72, 0xD5, 0x52, 0xB7,
  34012. 0x18, 0x78, 0x85, 0xE6, 0xB8, 0x94, 0x3D, 0x64,
  34013. 0x31, 0x41, 0x3C, 0x59, 0xEB, 0xB7, 0xE0, 0x36,
  34014. 0x04, 0x84, 0x90, 0xBE, 0x52, 0x89, 0xE9, 0x5B,
  34015. 0x20, 0xA8, 0x9E, 0x8B, 0x15, 0x9F, 0x61, 0xA9,
  34016. 0xA9, 0x88, 0x6E, 0x14, 0x75, 0x68, 0xF4, 0xC9,
  34017. 0x02, 0x1F, 0x36, 0x2F, 0x02, 0x68, 0x8A, 0x1C,
  34018. 0x8C, 0x3B, 0xB0, 0xD2, 0x40, 0x86, 0x88, 0x0E,
  34019. 0x55, 0xB6, 0xED, 0xB4, 0x3F, 0x37, 0x45, 0xD2,
  34020. 0xC1, 0x66, 0xDC, 0x1C, 0xB7, 0x43, 0xC7, 0x6F,
  34021. 0xE6, 0xBE, 0x52, 0x3A, 0x89, 0x3C, 0xC7, 0x64,
  34022. 0xD1, 0x64, 0x35, 0xC3, 0x78, 0x51, 0x25, 0x2A,
  34023. 0x81, 0xE2, 0xFF, 0xBA, 0x0F, 0x18, 0x97, 0x1A,
  34024. 0x3D, 0xEE, 0x37, 0xD4, 0x87, 0x7C, 0xB9, 0x28,
  34025. 0xE3, 0x6E, 0x52, 0x35, 0x03, 0x7A, 0x6B, 0x20,
  34026. 0x57, 0x89, 0x7D, 0x51, 0x8A, 0x5F, 0x0E, 0x34,
  34027. 0x8E, 0x3A, 0xB6, 0xD5, 0xB5, 0x2D, 0xFC, 0x60,
  34028. 0x75, 0x7F, 0x3B, 0x41, 0xA4, 0xFE, 0xC7, 0x82,
  34029. 0x8F, 0x1D, 0xEE, 0xAF, 0x45, 0x87, 0xCC, 0xC8,
  34030. 0xEA, 0xDF, 0x64, 0x7F, 0x4D, 0x20, 0x3B, 0x2F,
  34031. 0xAA, 0x05, 0xA6, 0x49, 0xB5, 0x82, 0x34, 0x0C,
  34032. 0xB4, 0xCA, 0xCE, 0x57, 0xA3, 0x07, 0x11, 0xBE,
  34033. 0x75, 0x2F, 0xAC, 0xF0, 0x22, 0x7D, 0x0A, 0x80,
  34034. 0xC4, 0x12, 0x84, 0x42, 0xDD, 0xC5, 0x44, 0xBE,
  34035. 0x80, 0x5B, 0x9C, 0xFE, 0x8F, 0xE9, 0xB1, 0x23,
  34036. 0x7C, 0x80, 0xF9, 0x67, 0x87, 0xCD, 0x92, 0x81,
  34037. 0xCC, 0xF2, 0x70, 0xC1, 0xAF, 0xC0, 0x67, 0x0D
  34038. #else
  34039. 0x75, 0x49, 0x99, 0x8d, 0x46, 0x9e, 0x2e, 0x47,
  34040. 0x90, 0x02, 0x30, 0x5b, 0x09, 0xb4, 0x4d, 0xba,
  34041. 0xdb, 0xc2, 0x45, 0x7f, 0xfd, 0x31, 0x25, 0xf6,
  34042. 0xd3, 0x1b, 0x0f, 0x27, 0xb8, 0x03, 0xd5, 0x81,
  34043. 0x07, 0x1c, 0x1d, 0xc6, 0x18, 0x11, 0x96, 0xfe,
  34044. 0x76, 0xdf, 0x78, 0xde, 0x20, 0xdd, 0xa6, 0x09,
  34045. 0xcf, 0x1b, 0x7c, 0xb7, 0xa3, 0x52, 0xc4, 0xdd,
  34046. 0x9c, 0x2c, 0xfc, 0x18, 0x80, 0x1f, 0x03, 0x6f,
  34047. 0xe4, 0x0f, 0x8f, 0x7e, 0x6f, 0x3d, 0xd7, 0x3f,
  34048. 0x38, 0x71, 0x30, 0xbe, 0x38, 0x7b, 0x17, 0x13,
  34049. 0x41, 0x8f, 0x83, 0xd9, 0x3d, 0xc7, 0xf8, 0x07,
  34050. 0x4a, 0x03, 0x24, 0x55, 0xc4, 0x6f, 0x85, 0x7c,
  34051. 0x6b, 0x6b, 0x35, 0x42, 0x9c, 0x79, 0x00, 0x65,
  34052. 0x42, 0x0d, 0x74, 0x22, 0x52, 0xee, 0x53, 0xf5,
  34053. 0x3f, 0x6e, 0x64, 0xa9, 0xb7, 0x8a, 0x49, 0xbc,
  34054. 0x29, 0xb8, 0xce, 0x84, 0x83, 0x1a, 0x01, 0xc3,
  34055. 0x42, 0x9e, 0x34, 0x69, 0x60, 0xdc, 0x55, 0x95,
  34056. 0x26, 0xd9, 0x78, 0x53, 0xc3, 0x66, 0x31, 0xb4,
  34057. 0x77, 0x32, 0x85, 0xfa, 0xfe, 0x8e, 0x3c, 0xa4,
  34058. 0x25, 0x5a, 0x87, 0x23, 0xae, 0x4f, 0x02, 0xdd,
  34059. 0xd8, 0x5a, 0x47, 0x81, 0xb9, 0xf4, 0x18, 0x6d,
  34060. 0x67, 0xa8, 0x3b, 0x5d, 0x9e, 0xdd, 0xc3, 0xae,
  34061. 0x7c, 0xd4, 0x09, 0x6c, 0x33, 0xf4, 0xd9, 0x7f,
  34062. 0xe0, 0x20, 0x30, 0xec, 0xb6, 0xa1, 0xa8, 0xad,
  34063. 0x9b, 0x19, 0xd3, 0xeb, 0x32, 0xf1, 0xb8, 0xf2,
  34064. 0x71, 0xb3, 0x03, 0x53, 0xe9, 0xe1, 0x9d, 0xd1,
  34065. 0x83, 0xf0, 0x6b, 0x54, 0xc3, 0xcb, 0x02, 0xef,
  34066. 0x16, 0x62, 0x82, 0x75, 0x2a, 0xa1, 0x1c, 0x81,
  34067. 0x58, 0xe4, 0x8b, 0xbc, 0x68, 0x30, 0x17, 0x1c,
  34068. 0xa7, 0xdd, 0xb7, 0x5a, 0x35, 0xe4, 0x6c, 0x35,
  34069. 0x32, 0x1a, 0xbe, 0x6a, 0x74, 0x20, 0x32, 0xc7,
  34070. 0x72, 0xa1, 0x6b, 0x3d, 0x1c, 0xdd, 0xfc, 0x6f,
  34071. 0x28, 0x01, 0xe2, 0xb8, 0x17, 0x30, 0x2d, 0xbc,
  34072. 0x94, 0xf3, 0x33, 0xc0, 0xcb, 0x91, 0xe1, 0xce,
  34073. 0xbd, 0x5e, 0xc6, 0x1e, 0x49, 0xfa, 0x5a, 0x14,
  34074. 0xaa, 0xa3, 0x93, 0x75, 0x5f, 0xc3, 0xe6, 0xf4,
  34075. 0xb8, 0xc5, 0xc4, 0xfa, 0x4b, 0xaa, 0x07, 0xa0,
  34076. 0x8c, 0x4f, 0x33, 0x94, 0x62, 0x63, 0x58, 0xa1,
  34077. 0x5e, 0x69, 0x0e, 0xe1, 0xe4, 0x82, 0x9b, 0x11,
  34078. 0x1c, 0x17, 0x24, 0x1a, 0xee, 0x37, 0xd5, 0xc8,
  34079. 0x32, 0xf4, 0x84, 0x76, 0x88, 0xfe, 0x5b, 0x5d,
  34080. 0x1b, 0x19, 0xe8, 0xe0, 0x4d, 0x9d, 0x19, 0x37,
  34081. 0x00, 0x19, 0x87, 0xf3, 0xb4, 0xb8, 0x35, 0x49,
  34082. 0xc3, 0xe5, 0x30, 0xe4, 0x11, 0x9d, 0x16, 0x4b,
  34083. 0x20, 0xef, 0x9d, 0x3a, 0x72, 0xf7, 0x4c, 0x04,
  34084. 0x4a, 0x97, 0x45, 0x91, 0x22, 0x8b, 0x41, 0xe6,
  34085. 0x80, 0xec, 0x56, 0x40, 0xa9, 0x72, 0x34, 0xc2,
  34086. 0xc6, 0x01, 0x7c, 0x95, 0xe9, 0x1b, 0xe2, 0xbd,
  34087. 0x49, 0x85, 0x47, 0xd5, 0x7a, 0x52, 0x22, 0xb8,
  34088. 0x16, 0x2a, 0x35, 0x46, 0x65, 0x6d, 0x59, 0x98,
  34089. 0x0d, 0x51, 0xaf, 0x59, 0x5b, 0xf5, 0xf2, 0x3a,
  34090. 0x63, 0x2f, 0x6d, 0x85, 0x44, 0xb8, 0x10, 0x74,
  34091. 0xae, 0xd3, 0x4c, 0x03, 0x52, 0xba, 0x56, 0x0d,
  34092. 0xea, 0xfb, 0x07, 0x44, 0x1a, 0x55, 0xa9, 0x37,
  34093. 0x63, 0x42, 0xe5, 0x0a, 0x0e, 0xc2, 0x53, 0x72,
  34094. 0x28, 0x25, 0x5a, 0x4b, 0x5d, 0x03, 0xc9, 0x29,
  34095. 0x57, 0xf4, 0xea, 0x35, 0x07, 0xb4, 0xba, 0xad,
  34096. 0xce, 0x53, 0xcc, 0xdf, 0xb7, 0x36, 0x4f, 0xfc,
  34097. 0x18, 0x17, 0xb5, 0x8c, 0x50, 0xef, 0x28, 0xe3,
  34098. 0x22, 0xe1, 0xb9, 0x45, 0xe0, 0xeb, 0x9b, 0x12,
  34099. 0x33, 0x97, 0x5c, 0x30, 0xa5, 0x54, 0x53, 0x68,
  34100. 0x68, 0x27, 0x14, 0xbf, 0x50, 0x2b, 0x61, 0xe1,
  34101. 0xd0, 0x45, 0x7a, 0x97, 0x53, 0xe1, 0x0d, 0xe0,
  34102. 0xf1, 0xbf, 0x35, 0xec, 0x3a, 0x3f, 0x47, 0x0a,
  34103. 0x3c, 0x69, 0xcc, 0xb0, 0x4d, 0x2d, 0x98, 0xfa,
  34104. 0xb3, 0xa0, 0xb6, 0x72, 0x9a, 0x98, 0x75, 0xe1,
  34105. 0xdb, 0x53, 0x3c, 0x96, 0xb4, 0x1e, 0x3d, 0x98,
  34106. 0x62, 0x8a, 0x6f, 0x8c, 0xf6, 0x68, 0x40, 0x6c,
  34107. 0x5f, 0x03, 0x8e, 0x6b, 0x7b, 0x24, 0x2f, 0xdf,
  34108. 0x86, 0xa7, 0xf1, 0xe6, 0x97, 0xae, 0xb1, 0x36,
  34109. 0x11, 0x41, 0x67, 0xb1, 0x3f, 0x89, 0xf2, 0x31,
  34110. 0xbc, 0xec, 0x7a, 0x41, 0x66, 0xb3, 0x9e, 0xab,
  34111. 0x4a, 0x37, 0x09, 0x23, 0x78, 0x22, 0x05, 0x0c,
  34112. 0x49, 0xc9, 0x25, 0x95, 0xa2, 0x37, 0xf2, 0xeb,
  34113. 0x48, 0x3b, 0x9e, 0x1d, 0xd6, 0x12, 0x4b, 0xed,
  34114. 0x5e, 0xb9, 0xb7, 0xb5, 0x12, 0x12, 0x96, 0x37,
  34115. 0x6b, 0x7d, 0x20, 0x14, 0xa7, 0x75, 0x60, 0xca,
  34116. 0x65, 0x83, 0x3d, 0x8b, 0xeb, 0x4d, 0x6a, 0xe6,
  34117. 0x8e, 0xfd, 0x7a, 0x11, 0xac, 0xc7, 0xde, 0x87,
  34118. 0xd8, 0x2b, 0xe1, 0xad, 0x57, 0x3a, 0xe9, 0xf6,
  34119. 0xf0, 0x76, 0x6f, 0xd7, 0x86, 0x38, 0x7d, 0x1a,
  34120. 0x8c, 0x12, 0xd1, 0xc8, 0xa2, 0x96, 0xb4, 0xf7,
  34121. 0x26, 0x34, 0xf7, 0x05, 0x77, 0x68, 0x88, 0x48,
  34122. 0xe5, 0x76, 0x85, 0x1f, 0x13, 0xbe, 0x48, 0xdf,
  34123. 0x33, 0x5d, 0x4a, 0xcd, 0x89, 0x79, 0x3a, 0x6c,
  34124. 0x6c, 0x06, 0x55, 0xfc, 0x39, 0xbc, 0x9e, 0x1e,
  34125. 0x27, 0xb4, 0xa5, 0x00, 0xf7, 0x08, 0xcd, 0x4a,
  34126. 0x9f, 0x2e, 0xc6, 0x72, 0xba, 0x5b, 0xf8, 0xad,
  34127. 0x23, 0x99, 0x8d, 0x4c, 0x0c, 0x95, 0x8f, 0x29,
  34128. 0x0f, 0x2a, 0x6c, 0x4e, 0x6c, 0xd8, 0xc0, 0xcd,
  34129. 0xc8, 0x5f, 0x57, 0x16, 0xec, 0x98, 0xa4, 0xc8,
  34130. 0x99, 0x5d, 0x37, 0x8c, 0xc6, 0xe2, 0xa1, 0xe8,
  34131. 0xb8, 0x28, 0x00, 0xdd, 0xf0, 0x3b, 0x32, 0x26,
  34132. 0xa2, 0xe7, 0x81, 0x77, 0x71, 0xe5, 0x09, 0xb4,
  34133. 0x95, 0x5e, 0xe2, 0xbe, 0xd4, 0x21, 0x7b, 0xdf,
  34134. 0x06, 0x30, 0xb5, 0x84, 0x0f, 0x25, 0x24, 0xab
  34135. #endif
  34136. };
  34137. WOLFSSL_SMALL_STACK_STATIC const byte kyber512_ss[] = {
  34138. #ifndef WOLFSSL_ML_KEM
  34139. 0x0A, 0x69, 0x25, 0x67, 0x6F, 0x24, 0xB2, 0x2C,
  34140. 0x28, 0x6F, 0x4C, 0x81, 0xA4, 0x22, 0x4C, 0xEC,
  34141. 0x50, 0x6C, 0x9B, 0x25, 0x7D, 0x48, 0x0E, 0x02,
  34142. 0xE3, 0xB4, 0x9F, 0x44, 0xCA, 0xA3, 0x23, 0x7F
  34143. #else
  34144. 0xc6, 0x08, 0x77, 0x70, 0x86, 0xed, 0x9f, 0xfd,
  34145. 0xf9, 0x2c, 0xd4, 0xf1, 0xc9, 0x99, 0xae, 0xdd,
  34146. 0x0b, 0x42, 0xe5, 0xe8, 0xef, 0x67, 0x32, 0xf4,
  34147. 0x11, 0x12, 0x46, 0x48, 0x1e, 0x26, 0x04, 0x63
  34148. #endif
  34149. };
  34150. #ifdef WOLFSSL_SMALL_STACK
  34151. key = (KyberKey *)XMALLOC(sizeof(KyberKey), HEAP_HINT,
  34152. DYNAMIC_TYPE_TMP_BUFFER);
  34153. priv = (byte *)XMALLOC(KYBER512_PRIVATE_KEY_SIZE, HEAP_HINT,
  34154. DYNAMIC_TYPE_TMP_BUFFER);
  34155. pub = (byte *)XMALLOC(KYBER512_PUBLIC_KEY_SIZE, HEAP_HINT,
  34156. DYNAMIC_TYPE_TMP_BUFFER);
  34157. ct = (byte *)XMALLOC(KYBER512_CIPHER_TEXT_SIZE, HEAP_HINT,
  34158. DYNAMIC_TYPE_TMP_BUFFER);
  34159. ss = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  34160. DYNAMIC_TYPE_TMP_BUFFER);
  34161. ss_dec = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  34162. DYNAMIC_TYPE_TMP_BUFFER);
  34163. if (! (key && priv && pub && ct && ss && ss_dec))
  34164. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  34165. #endif
  34166. ret = wc_KyberKey_Init(KYBER512, key, HEAP_HINT, INVALID_DEVID);
  34167. if (ret != 0)
  34168. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34169. else
  34170. key_inited = 1;
  34171. ret = wc_KyberKey_MakeKeyWithRandom(key, kyber512_rand,
  34172. sizeof(kyber512_rand));
  34173. if (ret != 0)
  34174. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34175. ret = wc_KyberKey_EncodePublicKey(key, pub, KYBER512_PUBLIC_KEY_SIZE);
  34176. if (ret != 0)
  34177. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34178. ret = wc_KyberKey_EncodePrivateKey(key, priv, KYBER512_PRIVATE_KEY_SIZE);
  34179. if (ret != 0)
  34180. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34181. if (XMEMCMP(pub, kyber512_pk, sizeof(kyber512_pk)) != 0)
  34182. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34183. if (XMEMCMP(priv, kyber512_sk, sizeof(kyber512_sk)) != 0)
  34184. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34185. ret = wc_KyberKey_EncapsulateWithRandom(key, ct, ss, kyber512enc_rand,
  34186. sizeof(kyber512enc_rand));
  34187. if (ret != 0)
  34188. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34189. if (XMEMCMP(ct, kyber512_ct, sizeof(kyber512_ct)) != 0)
  34190. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34191. if (XMEMCMP(ss, kyber512_ss, sizeof(kyber512_ss)) != 0)
  34192. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34193. ret = wc_KyberKey_Decapsulate(key, ss_dec, ct, sizeof(kyber512_ct));
  34194. if (ret != 0)
  34195. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34196. if (XMEMCMP(ss_dec, kyber512_ss, sizeof(kyber512_ss)) != 0)
  34197. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34198. out:
  34199. if (key_inited)
  34200. wc_KyberKey_Free(key);
  34201. #ifdef WOLFSSL_SMALL_STACK
  34202. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34203. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34204. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34205. XFREE(ct, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34206. XFREE(ss, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34207. XFREE(ss_dec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34208. #endif
  34209. return ret;
  34210. }
  34211. #endif /* WOLFSSL_KYBER512 */
  34212. #ifdef WOLFSSL_KYBER768
  34213. static wc_test_ret_t kyber768_kat(void)
  34214. {
  34215. wc_test_ret_t ret;
  34216. #ifdef WOLFSSL_SMALL_STACK
  34217. KyberKey *key = NULL;
  34218. byte *priv = NULL;
  34219. byte *pub = NULL;
  34220. byte *ct = NULL;
  34221. byte *ss = NULL;
  34222. byte *ss_dec = NULL;
  34223. #else
  34224. KyberKey key[1];
  34225. byte priv[KYBER768_PRIVATE_KEY_SIZE];
  34226. byte pub[KYBER768_PUBLIC_KEY_SIZE];
  34227. byte ct[KYBER768_CIPHER_TEXT_SIZE];
  34228. byte ss[KYBER_SS_SZ];
  34229. byte ss_dec[KYBER_SS_SZ];
  34230. #endif
  34231. int key_inited = 0;
  34232. WOLFSSL_SMALL_STACK_STATIC const byte kyber768_rand[] = {
  34233. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  34234. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  34235. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  34236. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  34237. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  34238. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  34239. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  34240. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  34241. };
  34242. WOLFSSL_SMALL_STACK_STATIC const byte kyber768enc_rand[] = {
  34243. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  34244. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  34245. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  34246. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  34247. };
  34248. WOLFSSL_SMALL_STACK_STATIC const byte kyber768_pk[] = {
  34249. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  34250. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  34251. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  34252. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  34253. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  34254. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  34255. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  34256. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  34257. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  34258. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  34259. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  34260. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  34261. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  34262. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  34263. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  34264. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  34265. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  34266. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  34267. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  34268. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  34269. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  34270. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  34271. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  34272. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  34273. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  34274. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  34275. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  34276. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  34277. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  34278. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  34279. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  34280. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  34281. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  34282. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  34283. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  34284. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  34285. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  34286. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  34287. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  34288. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  34289. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  34290. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  34291. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  34292. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  34293. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  34294. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  34295. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  34296. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  34297. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  34298. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  34299. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  34300. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  34301. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  34302. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  34303. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  34304. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  34305. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  34306. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  34307. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  34308. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  34309. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  34310. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  34311. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  34312. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  34313. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  34314. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  34315. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  34316. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  34317. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  34318. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  34319. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  34320. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  34321. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  34322. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  34323. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  34324. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  34325. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  34326. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  34327. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  34328. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  34329. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  34330. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  34331. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  34332. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  34333. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  34334. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  34335. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  34336. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  34337. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  34338. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  34339. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  34340. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  34341. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  34342. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  34343. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  34344. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  34345. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  34346. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  34347. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  34348. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  34349. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  34350. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  34351. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  34352. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  34353. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  34354. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  34355. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  34356. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  34357. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  34358. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  34359. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  34360. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  34361. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  34362. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  34363. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  34364. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  34365. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  34366. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  34367. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  34368. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  34369. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  34370. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  34371. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  34372. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  34373. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  34374. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  34375. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  34376. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  34377. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  34378. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  34379. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  34380. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  34381. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  34382. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  34383. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  34384. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  34385. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  34386. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  34387. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  34388. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  34389. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  34390. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  34391. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  34392. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  34393. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  34394. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  34395. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  34396. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  34397. };
  34398. WOLFSSL_SMALL_STACK_STATIC const byte kyber768_sk[] = {
  34399. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  34400. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  34401. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  34402. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  34403. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  34404. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  34405. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  34406. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  34407. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  34408. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  34409. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  34410. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  34411. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  34412. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  34413. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  34414. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  34415. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  34416. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  34417. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  34418. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  34419. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  34420. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  34421. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  34422. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  34423. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  34424. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  34425. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  34426. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  34427. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  34428. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  34429. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  34430. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  34431. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  34432. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  34433. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  34434. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  34435. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  34436. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  34437. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  34438. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  34439. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  34440. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  34441. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  34442. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  34443. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  34444. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  34445. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  34446. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  34447. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  34448. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  34449. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  34450. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  34451. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  34452. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  34453. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  34454. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  34455. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  34456. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  34457. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  34458. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  34459. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  34460. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  34461. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  34462. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  34463. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  34464. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  34465. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  34466. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  34467. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  34468. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  34469. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  34470. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  34471. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  34472. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  34473. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  34474. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  34475. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  34476. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  34477. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  34478. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  34479. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  34480. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  34481. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  34482. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  34483. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  34484. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  34485. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  34486. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  34487. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  34488. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  34489. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  34490. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  34491. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  34492. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  34493. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  34494. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  34495. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  34496. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  34497. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  34498. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  34499. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  34500. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  34501. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  34502. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  34503. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  34504. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  34505. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  34506. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  34507. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  34508. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  34509. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  34510. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  34511. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  34512. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  34513. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  34514. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  34515. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  34516. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  34517. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  34518. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  34519. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  34520. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  34521. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  34522. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  34523. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  34524. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  34525. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  34526. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  34527. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  34528. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  34529. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  34530. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  34531. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  34532. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  34533. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  34534. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  34535. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  34536. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  34537. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  34538. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  34539. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  34540. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  34541. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  34542. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  34543. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  34544. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  34545. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  34546. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  34547. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  34548. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  34549. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  34550. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  34551. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  34552. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  34553. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  34554. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  34555. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  34556. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  34557. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  34558. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  34559. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  34560. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  34561. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  34562. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  34563. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  34564. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  34565. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  34566. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  34567. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  34568. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  34569. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  34570. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  34571. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  34572. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  34573. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  34574. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  34575. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  34576. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  34577. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  34578. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  34579. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  34580. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  34581. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  34582. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  34583. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  34584. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  34585. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  34586. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  34587. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  34588. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  34589. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  34590. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  34591. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  34592. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  34593. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  34594. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  34595. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  34596. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  34597. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  34598. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  34599. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  34600. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  34601. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  34602. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  34603. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  34604. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  34605. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  34606. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  34607. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  34608. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  34609. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  34610. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  34611. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  34612. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  34613. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  34614. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  34615. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  34616. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  34617. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  34618. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  34619. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  34620. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  34621. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  34622. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  34623. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  34624. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  34625. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  34626. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  34627. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  34628. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  34629. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  34630. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  34631. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  34632. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  34633. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  34634. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  34635. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  34636. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  34637. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  34638. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  34639. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  34640. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  34641. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  34642. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  34643. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  34644. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  34645. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  34646. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  34647. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  34648. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  34649. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  34650. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  34651. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  34652. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  34653. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  34654. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  34655. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  34656. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  34657. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  34658. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  34659. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  34660. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  34661. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  34662. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  34663. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  34664. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  34665. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  34666. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  34667. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  34668. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  34669. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  34670. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  34671. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  34672. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  34673. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  34674. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  34675. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  34676. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  34677. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  34678. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  34679. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  34680. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  34681. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  34682. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  34683. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  34684. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  34685. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  34686. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  34687. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  34688. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  34689. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  34690. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  34691. 0xD4, 0xEC, 0x14, 0x3B, 0x50, 0xF0, 0x14, 0x23,
  34692. 0xB1, 0x77, 0x89, 0x5E, 0xDE, 0xE2, 0x2B, 0xB7,
  34693. 0x39, 0xF6, 0x47, 0xEC, 0xF8, 0x5F, 0x50, 0xBC,
  34694. 0x25, 0xEF, 0x7B, 0x5A, 0x72, 0x5D, 0xEE, 0x86,
  34695. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  34696. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  34697. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  34698. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  34699. };
  34700. WOLFSSL_SMALL_STACK_STATIC const byte kyber768_ct[] = {
  34701. #ifndef WOLFSSL_ML_KEM
  34702. 0xB5, 0x2C, 0x56, 0xB9, 0x2A, 0x4B, 0x7C, 0xE9,
  34703. 0xE4, 0xCB, 0x7C, 0x5B, 0x1B, 0x16, 0x31, 0x67,
  34704. 0xA8, 0xA1, 0x67, 0x5B, 0x2F, 0xDE, 0xF8, 0x4A,
  34705. 0x5B, 0x67, 0xCA, 0x15, 0xDB, 0x69, 0x4C, 0x9F,
  34706. 0x11, 0xBD, 0x02, 0x7C, 0x30, 0xAE, 0x22, 0xEC,
  34707. 0x92, 0x1A, 0x1D, 0x91, 0x15, 0x99, 0xAF, 0x05,
  34708. 0x85, 0xE4, 0x8D, 0x20, 0xDA, 0x70, 0xDF, 0x9F,
  34709. 0x39, 0xE3, 0x2E, 0xF9, 0x5D, 0x4C, 0x8F, 0x44,
  34710. 0xBF, 0xEF, 0xDA, 0xA5, 0xDA, 0x64, 0xF1, 0x05,
  34711. 0x46, 0x31, 0xD0, 0x4D, 0x6D, 0x3C, 0xFD, 0x0A,
  34712. 0x54, 0x0D, 0xD7, 0xBA, 0x38, 0x86, 0xE4, 0xB5,
  34713. 0xF1, 0x3E, 0x87, 0x87, 0x88, 0x60, 0x4C, 0x95,
  34714. 0xC0, 0x96, 0xEA, 0xB3, 0x91, 0x9F, 0x42, 0x75,
  34715. 0x21, 0x41, 0x9A, 0x94, 0x6C, 0x26, 0xCC, 0x04,
  34716. 0x14, 0x75, 0xD7, 0x12, 0x4C, 0xDC, 0x01, 0xD0,
  34717. 0x37, 0x3E, 0x5B, 0x09, 0xC7, 0xA7, 0x06, 0x03,
  34718. 0xCF, 0xDB, 0x4F, 0xB3, 0x40, 0x50, 0x23, 0xF2,
  34719. 0x26, 0x4D, 0xC3, 0xF9, 0x83, 0xC4, 0xFC, 0x02,
  34720. 0xA2, 0xD1, 0xB2, 0x68, 0xF2, 0x20, 0x8A, 0x1F,
  34721. 0x6E, 0x2A, 0x62, 0x09, 0xBF, 0xF1, 0x2F, 0x6F,
  34722. 0x46, 0x5F, 0x0B, 0x06, 0x9C, 0x3A, 0x7F, 0x84,
  34723. 0xF6, 0x06, 0xD8, 0xA9, 0x40, 0x64, 0x00, 0x3D,
  34724. 0x6E, 0xC1, 0x14, 0xC8, 0xE8, 0x08, 0xD3, 0x05,
  34725. 0x38, 0x84, 0xC1, 0xD5, 0xA1, 0x42, 0xFB, 0xF2,
  34726. 0x01, 0x12, 0xEB, 0x36, 0x0F, 0xDA, 0x3F, 0x0F,
  34727. 0x28, 0xB1, 0x72, 0xAE, 0x50, 0xF5, 0xE7, 0xD8,
  34728. 0x38, 0x01, 0xFB, 0x3F, 0x00, 0x64, 0xB6, 0x87,
  34729. 0x18, 0x70, 0x74, 0xBD, 0x7F, 0xE3, 0x0E, 0xDD,
  34730. 0xAA, 0x33, 0x4C, 0xF8, 0xFC, 0x04, 0xFA, 0x8C,
  34731. 0xED, 0x89, 0x9C, 0xEA, 0xDE, 0x4B, 0x4F, 0x28,
  34732. 0xB6, 0x83, 0x72, 0xBA, 0xF9, 0x8F, 0xF4, 0x82,
  34733. 0xA4, 0x15, 0xB7, 0x31, 0x15, 0x5B, 0x75, 0xCE,
  34734. 0xB9, 0x76, 0xBE, 0x0E, 0xA0, 0x28, 0x5B, 0xA0,
  34735. 0x1A, 0x27, 0xF1, 0x85, 0x7A, 0x8F, 0xB3, 0x77,
  34736. 0xA3, 0xAE, 0x0C, 0x23, 0xB2, 0xAA, 0x9A, 0x07,
  34737. 0x9B, 0xFA, 0xBF, 0xF0, 0xD5, 0xB2, 0xF1, 0xCD,
  34738. 0x9B, 0x71, 0x8B, 0xEA, 0x03, 0xC4, 0x2F, 0x34,
  34739. 0x3A, 0x39, 0xB4, 0xF1, 0x42, 0xD0, 0x1A, 0xD8,
  34740. 0xAC, 0xBB, 0x50, 0xE3, 0x88, 0x53, 0xCF, 0x9A,
  34741. 0x50, 0xC8, 0xB4, 0x4C, 0x3C, 0xF6, 0x71, 0xA4,
  34742. 0xA9, 0x04, 0x3B, 0x26, 0xDD, 0xBB, 0x24, 0x95,
  34743. 0x9A, 0xD6, 0x71, 0x5C, 0x08, 0x52, 0x18, 0x55,
  34744. 0xC7, 0x9A, 0x23, 0xB9, 0xC3, 0xD6, 0x47, 0x17,
  34745. 0x49, 0xC4, 0x07, 0x25, 0xBD, 0xD5, 0xC2, 0x77,
  34746. 0x6D, 0x43, 0xAE, 0xD2, 0x02, 0x04, 0xBA, 0xA1,
  34747. 0x41, 0xEF, 0xB3, 0x30, 0x49, 0x17, 0x47, 0x4B,
  34748. 0x7F, 0x9F, 0x7A, 0x4B, 0x08, 0xB1, 0xA9, 0x3D,
  34749. 0xAE, 0xD9, 0x8C, 0x67, 0x49, 0x53, 0x59, 0xD3,
  34750. 0x7D, 0x67, 0xF7, 0x43, 0x8B, 0xEE, 0x5E, 0x43,
  34751. 0x58, 0x56, 0x34, 0xB2, 0x6C, 0x6B, 0x38, 0x10,
  34752. 0xD7, 0xCD, 0xCB, 0xC0, 0xF6, 0xEB, 0x87, 0x7A,
  34753. 0x60, 0x87, 0xE6, 0x8A, 0xCB, 0x84, 0x80, 0xD3,
  34754. 0xA8, 0xCF, 0x69, 0x00, 0x44, 0x7E, 0x49, 0xB4,
  34755. 0x17, 0xF1, 0x5A, 0x53, 0xB6, 0x07, 0xA0, 0xE2,
  34756. 0x16, 0xB8, 0x55, 0x97, 0x0D, 0x37, 0x40, 0x68,
  34757. 0x70, 0xB4, 0x56, 0x87, 0x22, 0xDA, 0x77, 0xA4,
  34758. 0x08, 0x47, 0x03, 0x81, 0x67, 0x84, 0xE2, 0xF1,
  34759. 0x6B, 0xED, 0x18, 0x99, 0x65, 0x32, 0xC5, 0xD8,
  34760. 0xB7, 0xF5, 0xD2, 0x14, 0x46, 0x4E, 0x5F, 0x3F,
  34761. 0x6E, 0x90, 0x58, 0x67, 0xB0, 0xCE, 0x11, 0x9E,
  34762. 0x25, 0x2A, 0x66, 0x71, 0x32, 0x53, 0x54, 0x46,
  34763. 0x85, 0xD2, 0x08, 0xE1, 0x72, 0x39, 0x08, 0xA0,
  34764. 0xCE, 0x97, 0x83, 0x46, 0x52, 0xE0, 0x8A, 0xE7,
  34765. 0xBD, 0xC8, 0x81, 0xA1, 0x31, 0xB7, 0x3C, 0x71,
  34766. 0xE8, 0x4D, 0x20, 0xD6, 0x8F, 0xDE, 0xFF, 0x4F,
  34767. 0x5D, 0x70, 0xCD, 0x1A, 0xF5, 0x7B, 0x78, 0xE3,
  34768. 0x49, 0x1A, 0x98, 0x65, 0x94, 0x23, 0x21, 0x80,
  34769. 0x0A, 0x20, 0x3C, 0x05, 0xED, 0x1F, 0xEE, 0xB5,
  34770. 0xA2, 0x8E, 0x58, 0x4E, 0x19, 0xF6, 0x53, 0x5E,
  34771. 0x7F, 0x84, 0xE4, 0xA2, 0x4F, 0x84, 0xA7, 0x2D,
  34772. 0xCA, 0xF5, 0x64, 0x8B, 0x4A, 0x42, 0x35, 0xDD,
  34773. 0x66, 0x44, 0x64, 0x48, 0x2F, 0x03, 0x17, 0x6E,
  34774. 0x88, 0x8C, 0x28, 0xBF, 0xC6, 0xC1, 0xCB, 0x23,
  34775. 0x8C, 0xFF, 0xA3, 0x5A, 0x32, 0x1E, 0x71, 0x79,
  34776. 0x1D, 0x9E, 0xA8, 0xED, 0x08, 0x78, 0xC6, 0x11,
  34777. 0x21, 0xBF, 0x8D, 0x2A, 0x4A, 0xB2, 0xC1, 0xA5,
  34778. 0xE1, 0x20, 0xBC, 0x40, 0xAB, 0xB1, 0x89, 0x2D,
  34779. 0x17, 0x15, 0x09, 0x0A, 0x0E, 0xE4, 0x82, 0x52,
  34780. 0xCA, 0x29, 0x7A, 0x99, 0xAA, 0x0E, 0x51, 0x0C,
  34781. 0xF2, 0x6B, 0x1A, 0xDD, 0x06, 0xCA, 0x54, 0x3E,
  34782. 0x1C, 0x5D, 0x6B, 0xDC, 0xD3, 0xB9, 0xC5, 0x85,
  34783. 0xC8, 0x53, 0x80, 0x45, 0xDB, 0x5C, 0x25, 0x2E,
  34784. 0xC3, 0xC8, 0xC3, 0xC9, 0x54, 0xD9, 0xBE, 0x59,
  34785. 0x07, 0x09, 0x4A, 0x89, 0x4E, 0x60, 0xEA, 0xB4,
  34786. 0x35, 0x38, 0xCF, 0xEE, 0x82, 0xE8, 0xFF, 0xC0,
  34787. 0x79, 0x1B, 0x0D, 0x0F, 0x43, 0xAC, 0x16, 0x27,
  34788. 0x83, 0x0A, 0x61, 0xD5, 0x6D, 0xAD, 0x96, 0xC6,
  34789. 0x29, 0x58, 0xB0, 0xDE, 0x78, 0x0B, 0x78, 0xBD,
  34790. 0x47, 0xA6, 0x04, 0x55, 0x0D, 0xAB, 0x83, 0xFF,
  34791. 0xF2, 0x27, 0xC3, 0x24, 0x04, 0x94, 0x71, 0xF3,
  34792. 0x52, 0x48, 0xCF, 0xB8, 0x49, 0xB2, 0x57, 0x24,
  34793. 0xFF, 0x70, 0x4D, 0x52, 0x77, 0xAA, 0x35, 0x2D,
  34794. 0x55, 0x09, 0x58, 0xBE, 0x3B, 0x23, 0x7D, 0xFF,
  34795. 0x47, 0x3E, 0xC2, 0xAD, 0xBA, 0xEA, 0x48, 0xCA,
  34796. 0x26, 0x58, 0xAE, 0xFC, 0xC7, 0x7B, 0xBD, 0x42,
  34797. 0x64, 0xAB, 0x37, 0x4D, 0x70, 0xEA, 0xE5, 0xB9,
  34798. 0x64, 0x41, 0x6C, 0xE8, 0x22, 0x6A, 0x7E, 0x32,
  34799. 0x55, 0xA0, 0xF8, 0xD7, 0xE2, 0xAD, 0xCA, 0x06,
  34800. 0x2B, 0xCD, 0x6D, 0x78, 0xD6, 0x0D, 0x1B, 0x32,
  34801. 0xE1, 0x14, 0x05, 0xBE, 0x54, 0xB6, 0x6E, 0xF0,
  34802. 0xFD, 0xDD, 0x56, 0x77, 0x02, 0xA3, 0xBC, 0xCF,
  34803. 0xED, 0xE3, 0xC5, 0x84, 0x70, 0x12, 0x69, 0xED,
  34804. 0x14, 0x80, 0x9F, 0x06, 0xF8, 0x96, 0x83, 0x56,
  34805. 0xBB, 0x92, 0x67, 0xFE, 0x86, 0xE5, 0x14, 0x25,
  34806. 0x2E, 0x88, 0xBB, 0x5C, 0x30, 0xA7, 0xEC, 0xB3,
  34807. 0xD0, 0xE6, 0x21, 0x02, 0x1E, 0xE0, 0xFB, 0xF7,
  34808. 0x87, 0x1B, 0x09, 0x34, 0x2B, 0xF8, 0x4F, 0x55,
  34809. 0xC9, 0x7E, 0xAF, 0x86, 0xC4, 0x81, 0x89, 0xC7,
  34810. 0xFF, 0x4D, 0xF3, 0x89, 0xF0, 0x77, 0xE2, 0x80,
  34811. 0x6E, 0x5F, 0xA7, 0x3B, 0x3E, 0x94, 0x58, 0xA1,
  34812. 0x6C, 0x7E, 0x27, 0x5F, 0x4F, 0x60, 0x22, 0x75,
  34813. 0x58, 0x0E, 0xB7, 0xB7, 0x13, 0x5F, 0xB5, 0x37,
  34814. 0xFA, 0x0C, 0xD9, 0x5D, 0x6E, 0xA5, 0x8C, 0x10,
  34815. 0x8C, 0xD8, 0x94, 0x3D, 0x70, 0xC1, 0x64, 0x31,
  34816. 0x11, 0xF4, 0xF0, 0x1C, 0xA8, 0xA8, 0x27, 0x6A,
  34817. 0x90, 0x26, 0x66, 0xED, 0x81, 0xB7, 0x8D, 0x16,
  34818. 0x8B, 0x00, 0x6F, 0x16, 0xAA, 0xA3, 0xD8, 0xE4,
  34819. 0xCE, 0x4F, 0x4D, 0x0F, 0xB0, 0x99, 0x7E, 0x41,
  34820. 0xAE, 0xFF, 0xB5, 0xB3, 0xDA, 0xA8, 0x38, 0x73,
  34821. 0x2F, 0x35, 0x73, 0x49, 0x44, 0x7F, 0x38, 0x77,
  34822. 0x76, 0xC7, 0x93, 0xC0, 0x47, 0x9D, 0xE9, 0xE9,
  34823. 0x94, 0x98, 0xCC, 0x35, 0x6F, 0xDB, 0x00, 0x75,
  34824. 0xA7, 0x03, 0xF2, 0x3C, 0x55, 0xD4, 0x7B, 0x55,
  34825. 0x0E, 0xC8, 0x9B, 0x02, 0xAD, 0xE8, 0x93, 0x29,
  34826. 0x08, 0x6A, 0x50, 0x84, 0x34, 0x56, 0xFE, 0xDC,
  34827. 0x37, 0x88, 0xAC, 0x8D, 0x97, 0x23, 0x3C, 0x54,
  34828. 0x56, 0x04, 0x67, 0xEE, 0x1D, 0x0F, 0x02, 0x4B,
  34829. 0x18, 0x42, 0x8F, 0x0D, 0x73, 0xB3, 0x0E, 0x19,
  34830. 0xF5, 0xC6, 0x3B, 0x9A, 0xBF, 0x11, 0x41, 0x5B,
  34831. 0xEA, 0x4D, 0x01, 0x70, 0x13, 0x0B, 0xAA, 0xBD,
  34832. 0x33, 0xC0, 0x5E, 0x65, 0x24, 0xE5, 0xFB, 0x55,
  34833. 0x81, 0xB2, 0x2B, 0x04, 0x33, 0x34, 0x22, 0x48,
  34834. 0x26, 0x6D, 0x0F, 0x10, 0x53, 0xB2, 0x45, 0xCC,
  34835. 0x24, 0x62, 0xDC, 0x44, 0xD3, 0x49, 0x65, 0x10,
  34836. 0x24, 0x82, 0xA8, 0xED, 0x9E, 0x4E, 0x96, 0x4D,
  34837. 0x56, 0x83, 0xE5, 0xD4, 0x5D, 0x0C, 0x82, 0x69
  34838. #else
  34839. 0xa4, 0x13, 0xbe, 0x81, 0x04, 0x72, 0x59, 0x20,
  34840. 0x24, 0x01, 0xee, 0x35, 0x98, 0x9d, 0x25, 0xa3,
  34841. 0x85, 0x6c, 0xd1, 0xc0, 0x26, 0x0c, 0xe2, 0x39,
  34842. 0x1d, 0xe3, 0x23, 0x73, 0x6b, 0x67, 0x8f, 0x32,
  34843. 0x80, 0x05, 0xc8, 0x21, 0xad, 0x09, 0x21, 0x80,
  34844. 0xb4, 0x49, 0x6f, 0x21, 0x29, 0x28, 0x0f, 0x4f,
  34845. 0x29, 0x94, 0x04, 0x36, 0x2b, 0x9d, 0x14, 0x19,
  34846. 0x48, 0xb6, 0xbb, 0x02, 0xac, 0xd5, 0x73, 0x65,
  34847. 0x59, 0xfc, 0x90, 0x39, 0x01, 0x8c, 0x96, 0x1d,
  34848. 0xdd, 0xd9, 0x4e, 0xe5, 0x59, 0x19, 0x84, 0x71,
  34849. 0xd4, 0xa0, 0x49, 0xe5, 0x47, 0xb5, 0x63, 0x6c,
  34850. 0xf8, 0xbb, 0xf7, 0xdb, 0x1a, 0x90, 0xc7, 0x2b,
  34851. 0x87, 0x09, 0x23, 0xdc, 0xd5, 0x4b, 0x14, 0x8c,
  34852. 0x60, 0xc9, 0xc8, 0xee, 0x60, 0x4d, 0x30, 0xee,
  34853. 0xbb, 0x69, 0x01, 0xe6, 0xdf, 0x25, 0x96, 0x12,
  34854. 0x18, 0x26, 0x05, 0x8d, 0x25, 0x02, 0x9a, 0xe3,
  34855. 0x99, 0xc9, 0x5f, 0x6a, 0xac, 0xba, 0xaf, 0xe3,
  34856. 0x4f, 0x11, 0x8d, 0xdb, 0xa7, 0xa6, 0x9d, 0x7c,
  34857. 0xd8, 0x99, 0xb5, 0xf4, 0xd5, 0x8d, 0x3d, 0xf2,
  34858. 0xa8, 0x89, 0xb0, 0x30, 0xce, 0x9a, 0x7e, 0xa6,
  34859. 0x44, 0x6d, 0x41, 0xa6, 0x0a, 0x17, 0x5f, 0x12,
  34860. 0x7d, 0xa9, 0x4c, 0x27, 0x6b, 0xaa, 0x1e, 0xdf,
  34861. 0xb3, 0x57, 0xd4, 0x1b, 0x28, 0x57, 0xad, 0x46,
  34862. 0x2c, 0x83, 0xd8, 0xff, 0x00, 0x23, 0x6d, 0x9b,
  34863. 0xac, 0x59, 0x32, 0x5e, 0x0c, 0x3b, 0xdd, 0xcb,
  34864. 0x37, 0xbf, 0xe0, 0xfd, 0xa4, 0xe1, 0x67, 0xfc,
  34865. 0xf6, 0xae, 0xc1, 0x49, 0xfe, 0x5f, 0x9f, 0x63,
  34866. 0x93, 0xfc, 0x47, 0x15, 0xc6, 0x99, 0x5d, 0x67,
  34867. 0xf2, 0xb4, 0xdd, 0xb0, 0xc7, 0x67, 0x8e, 0xe1,
  34868. 0x40, 0xbc, 0xfd, 0xd2, 0x36, 0x5e, 0x81, 0x22,
  34869. 0xca, 0x92, 0xcb, 0xba, 0x1a, 0xc7, 0x03, 0x35,
  34870. 0x7e, 0xdf, 0x15, 0x21, 0x0c, 0x68, 0x92, 0x66,
  34871. 0x9f, 0x1a, 0x2b, 0x88, 0xd7, 0x92, 0xbe, 0x7d,
  34872. 0x9a, 0xa5, 0x6c, 0x5e, 0x8d, 0xf7, 0x58, 0xab,
  34873. 0xb4, 0xbb, 0xae, 0x83, 0x14, 0x1d, 0x27, 0x59,
  34874. 0xdf, 0xc4, 0xea, 0x8f, 0x2c, 0xf0, 0x0d, 0xd8,
  34875. 0x6a, 0x73, 0x12, 0xfb, 0xae, 0xa9, 0xcf, 0xe6,
  34876. 0xd7, 0xfd, 0x3f, 0x13, 0xfc, 0x8c, 0xb7, 0x5d,
  34877. 0x25, 0x2c, 0xb3, 0xec, 0x7e, 0x7b, 0x37, 0xcd,
  34878. 0x81, 0xd8, 0x8f, 0x38, 0xae, 0x59, 0x3e, 0xde,
  34879. 0x6f, 0x8a, 0x81, 0xd5, 0x11, 0x83, 0xd7, 0xdc,
  34880. 0x7f, 0x57, 0xab, 0xb2, 0x1c, 0xe2, 0xc5, 0x93,
  34881. 0xdb, 0x72, 0xf0, 0xbf, 0x77, 0x9c, 0xcc, 0xc8,
  34882. 0x24, 0x20, 0xf5, 0x3c, 0x2f, 0xe3, 0x64, 0xb1,
  34883. 0xfd, 0x3c, 0xd2, 0xec, 0x54, 0xb9, 0x24, 0xa6,
  34884. 0x2a, 0xfa, 0x4c, 0x31, 0x95, 0x57, 0x8e, 0x48,
  34885. 0xaa, 0x5f, 0x50, 0x7e, 0x79, 0x28, 0xd7, 0x52,
  34886. 0x7d, 0x65, 0x77, 0xd3, 0xfc, 0xa8, 0x7e, 0x7b,
  34887. 0x7b, 0x19, 0xa8, 0x9f, 0x69, 0xf0, 0x01, 0x8e,
  34888. 0xeb, 0x36, 0x87, 0x1b, 0xaa, 0xdf, 0xcc, 0x70,
  34889. 0x94, 0xe3, 0x44, 0xfb, 0x36, 0x48, 0x1f, 0xb1,
  34890. 0x4a, 0x5c, 0x53, 0xc3, 0x08, 0x67, 0xcf, 0x1c,
  34891. 0x5c, 0x02, 0xcf, 0x62, 0x27, 0xf9, 0xaa, 0xe8,
  34892. 0xd8, 0xa1, 0x2b, 0x24, 0xc5, 0xac, 0x2b, 0x8e,
  34893. 0xb9, 0x12, 0xb8, 0x7d, 0xe8, 0x32, 0x54, 0x09,
  34894. 0xe4, 0x40, 0xa4, 0x7b, 0x5c, 0x74, 0x23, 0x71,
  34895. 0x79, 0xa6, 0xce, 0x55, 0x58, 0xee, 0x09, 0x10,
  34896. 0x1c, 0xa4, 0xe6, 0x45, 0xe2, 0x4b, 0xdc, 0x28,
  34897. 0x77, 0x87, 0x35, 0xab, 0xf9, 0x8b, 0x06, 0x88,
  34898. 0xf6, 0x28, 0x9d, 0x50, 0x32, 0x51, 0x58, 0x2a,
  34899. 0xab, 0x6e, 0x81, 0xce, 0xd0, 0x17, 0x98, 0x29,
  34900. 0xf7, 0x31, 0x17, 0x31, 0xd0, 0x61, 0x5d, 0x0a,
  34901. 0x0d, 0x95, 0x59, 0x78, 0xaa, 0xfb, 0xf8, 0xaa,
  34902. 0x44, 0x0a, 0x5c, 0x85, 0x87, 0x0c, 0x58, 0xb3,
  34903. 0xe5, 0xc1, 0xff, 0x92, 0x67, 0xf0, 0x94, 0xb7,
  34904. 0x42, 0xf5, 0x16, 0xe8, 0xe9, 0x75, 0x9d, 0x0f,
  34905. 0x88, 0x02, 0x1d, 0x99, 0xa7, 0xfd, 0x65, 0xbb,
  34906. 0xee, 0x80, 0x12, 0x17, 0x27, 0x66, 0x56, 0xd2,
  34907. 0x1f, 0x37, 0x34, 0xde, 0x0a, 0x55, 0x89, 0xb3,
  34908. 0x3f, 0xe9, 0x96, 0xec, 0xb9, 0x9c, 0x0d, 0x8a,
  34909. 0x52, 0xd5, 0x4b, 0x39, 0xdc, 0xfe, 0x70, 0x7f,
  34910. 0xc1, 0x1e, 0x35, 0x63, 0x8a, 0x69, 0xd9, 0x08,
  34911. 0xcc, 0xb0, 0xed, 0xac, 0xfb, 0x2a, 0xa4, 0x35,
  34912. 0xe3, 0xbe, 0xb9, 0x81, 0xd3, 0xfd, 0xef, 0x59,
  34913. 0xca, 0xde, 0x6f, 0x63, 0xcd, 0xa0, 0x56, 0xc5,
  34914. 0x26, 0xcd, 0xc5, 0x5b, 0x87, 0xa3, 0xef, 0x26,
  34915. 0x38, 0xbc, 0xae, 0xed, 0xf4, 0x06, 0x71, 0x10,
  34916. 0x53, 0xa0, 0x9d, 0x31, 0x06, 0x99, 0xdc, 0x8e,
  34917. 0x3d, 0x07, 0xac, 0xc1, 0x0e, 0x1e, 0xa8, 0xec,
  34918. 0x8d, 0x51, 0xab, 0x31, 0xc0, 0x4c, 0xa8, 0x8c,
  34919. 0x21, 0x77, 0xa5, 0x11, 0x93, 0xb4, 0x18, 0xcc,
  34920. 0xc4, 0xb2, 0x54, 0x8e, 0xcd, 0xa8, 0x61, 0x59,
  34921. 0x8f, 0xfa, 0xa8, 0xb1, 0x6e, 0xaf, 0x89, 0xd5,
  34922. 0x9c, 0x84, 0x03, 0xc3, 0x9c, 0x8d, 0x94, 0xc4,
  34923. 0x28, 0xcf, 0x19, 0x18, 0x0e, 0x14, 0x20, 0x28,
  34924. 0x7b, 0x45, 0x5f, 0xb6, 0xe4, 0xe5, 0xbf, 0xbd,
  34925. 0x38, 0x3a, 0xef, 0x18, 0xca, 0x99, 0xf8, 0x10,
  34926. 0xf6, 0xce, 0xa7, 0x03, 0xbe, 0x4b, 0x9b, 0xf0,
  34927. 0xcb, 0x6f, 0x0c, 0x53, 0x83, 0xe8, 0x3e, 0xd3,
  34928. 0xa7, 0x23, 0xa2, 0x7d, 0x8e, 0x39, 0x91, 0x06,
  34929. 0x76, 0x56, 0x72, 0x69, 0x25, 0xb2, 0x0f, 0xb7,
  34930. 0x35, 0xb1, 0x27, 0x52, 0xfa, 0xcf, 0x68, 0x4e,
  34931. 0x5c, 0x03, 0xdc, 0x5b, 0xe7, 0xa6, 0x3a, 0xf4,
  34932. 0xbd, 0x93, 0x07, 0x54, 0xfd, 0xb5, 0xf7, 0x49,
  34933. 0x30, 0x6c, 0x2c, 0xfa, 0x6e, 0x39, 0x89, 0x25,
  34934. 0xc3, 0x46, 0xd9, 0xd5, 0x72, 0x92, 0x4b, 0x15,
  34935. 0x3b, 0x76, 0x73, 0xb7, 0xa5, 0x02, 0x21, 0x40,
  34936. 0x26, 0x4f, 0xd5, 0xa0, 0xab, 0xe0, 0x0b, 0x5d,
  34937. 0x85, 0xc6, 0x86, 0xf2, 0x96, 0xfb, 0xc4, 0x9d,
  34938. 0xd3, 0x15, 0x5a, 0xd2, 0xf7, 0x48, 0x25, 0x55,
  34939. 0x06, 0x90, 0x9b, 0x35, 0x5c, 0x70, 0x60, 0xdd,
  34940. 0xed, 0x4d, 0xd2, 0xfa, 0x21, 0xc7, 0xbf, 0x68,
  34941. 0x12, 0x51, 0xe7, 0xd6, 0x32, 0x89, 0xe1, 0x5f,
  34942. 0x85, 0x85, 0x4a, 0x25, 0xb4, 0xfb, 0x08, 0x5e,
  34943. 0xf0, 0x3a, 0x03, 0xcd, 0x05, 0x0f, 0x4f, 0x50,
  34944. 0x21, 0xd1, 0x12, 0xf3, 0x29, 0x1a, 0x9f, 0xd6,
  34945. 0x0a, 0xd0, 0x1e, 0x0b, 0x57, 0x97, 0xe7, 0x8d,
  34946. 0x9b, 0x94, 0xbe, 0xfe, 0x97, 0x46, 0xd7, 0x54,
  34947. 0xe6, 0xce, 0x41, 0xda, 0x34, 0xc5, 0x7d, 0xa3,
  34948. 0xd7, 0xde, 0xda, 0x6b, 0x23, 0x30, 0x82, 0xc4,
  34949. 0x13, 0x76, 0x14, 0xe9, 0x64, 0xff, 0xf0, 0xe3,
  34950. 0x84, 0x72, 0xe3, 0x6e, 0x49, 0x5f, 0x54, 0xe2,
  34951. 0xd2, 0x37, 0x1a, 0x75, 0x81, 0xb6, 0x94, 0xcb,
  34952. 0x26, 0x3d, 0xdf, 0x80, 0xbd, 0xd4, 0x3f, 0x63,
  34953. 0x83, 0x57, 0x8b, 0x5e, 0x18, 0x24, 0x4a, 0x69,
  34954. 0xca, 0xc9, 0xcd, 0xab, 0xea, 0x3d, 0x05, 0x71,
  34955. 0x8f, 0x5c, 0x23, 0xb1, 0xd4, 0xce, 0x66, 0x84,
  34956. 0x45, 0x75, 0x97, 0xd0, 0x12, 0x84, 0xb3, 0x8b,
  34957. 0x9d, 0x3e, 0xb1, 0xeb, 0xa4, 0xf5, 0xbe, 0xff,
  34958. 0x99, 0x0b, 0xb7, 0x49, 0xf0, 0x96, 0xa3, 0x0f,
  34959. 0x1b, 0xde, 0x72, 0x4a, 0xde, 0x37, 0x89, 0xde,
  34960. 0x5e, 0xf1, 0x83, 0xa6, 0x01, 0x63, 0xe2, 0x8f,
  34961. 0x15, 0x84, 0x50, 0x09, 0x84, 0xca, 0x51, 0x53,
  34962. 0x55, 0x5c, 0x38, 0xc6, 0x16, 0x49, 0x68, 0x3a,
  34963. 0x72, 0x79, 0x21, 0xff, 0xcc, 0xe3, 0x00, 0x7c,
  34964. 0x26, 0x77, 0x83, 0xbd, 0xdb, 0xdb, 0x9d, 0xe4,
  34965. 0x88, 0x80, 0xc4, 0xe8, 0x45, 0x2d, 0xab, 0x29,
  34966. 0xe7, 0xc4, 0xf8, 0xd1, 0xd5, 0xdf, 0xd3, 0x03,
  34967. 0xa9, 0x08, 0xb1, 0xce, 0x08, 0xbe, 0x0b, 0x9f,
  34968. 0xae, 0x98, 0x89, 0x4d, 0xcb, 0x26, 0x92, 0xd0,
  34969. 0xb3, 0x2f, 0xa3, 0x9d, 0xa9, 0x8b, 0xd4, 0xff,
  34970. 0x0e, 0xa1, 0x0f, 0x0b, 0x43, 0x8a, 0x49, 0x71,
  34971. 0xa7, 0xfc, 0x47, 0x18, 0x2f, 0xbe, 0x52, 0xd6,
  34972. 0xde, 0x71, 0xfe, 0xe3, 0xe8, 0x24, 0xa3, 0x9f,
  34973. 0x19, 0xc2, 0x7f, 0x51, 0xae, 0xc6, 0xd9, 0x2b,
  34974. 0xc7, 0xf8, 0xb8, 0xf0, 0x71, 0x84, 0x7b, 0xca
  34975. #endif
  34976. };
  34977. WOLFSSL_SMALL_STACK_STATIC const byte kyber768_ss[] = {
  34978. #ifndef WOLFSSL_ML_KEM
  34979. 0x91, 0x4C, 0xB6, 0x7F, 0xE5, 0xC3, 0x8E, 0x73,
  34980. 0xBF, 0x74, 0x18, 0x1C, 0x0A, 0xC5, 0x04, 0x28,
  34981. 0xDE, 0xDF, 0x77, 0x50, 0xA9, 0x80, 0x58, 0xF7,
  34982. 0xD5, 0x36, 0x70, 0x87, 0x74, 0x53, 0x5B, 0x29
  34983. #else
  34984. 0x72, 0x9f, 0xa0, 0x6a, 0xc9, 0x3c, 0x5e, 0xfd,
  34985. 0xfb, 0xf1, 0x27, 0x2a, 0x96, 0xce, 0xf1, 0x67,
  34986. 0xa3, 0x93, 0x94, 0x7a, 0xb7, 0xdc, 0x2d, 0x11,
  34987. 0xed, 0x7d, 0xe8, 0xac, 0x3c, 0x94, 0x7f, 0xa8
  34988. #endif
  34989. };
  34990. #ifdef WOLFSSL_SMALL_STACK
  34991. key = (KyberKey *)XMALLOC(sizeof(KyberKey), HEAP_HINT,
  34992. DYNAMIC_TYPE_TMP_BUFFER);
  34993. priv = (byte *)XMALLOC(KYBER768_PRIVATE_KEY_SIZE, HEAP_HINT,
  34994. DYNAMIC_TYPE_TMP_BUFFER);
  34995. pub = (byte *)XMALLOC(KYBER768_PUBLIC_KEY_SIZE, HEAP_HINT,
  34996. DYNAMIC_TYPE_TMP_BUFFER);
  34997. ct = (byte *)XMALLOC(KYBER768_CIPHER_TEXT_SIZE, HEAP_HINT,
  34998. DYNAMIC_TYPE_TMP_BUFFER);
  34999. ss = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  35000. DYNAMIC_TYPE_TMP_BUFFER);
  35001. ss_dec = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  35002. DYNAMIC_TYPE_TMP_BUFFER);
  35003. if (! (key && priv && pub && ct && ss && ss_dec))
  35004. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  35005. #endif
  35006. ret = wc_KyberKey_Init(KYBER768, key, HEAP_HINT, INVALID_DEVID);
  35007. if (ret != 0)
  35008. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35009. else
  35010. key_inited = 1;
  35011. ret = wc_KyberKey_MakeKeyWithRandom(key, kyber768_rand,
  35012. sizeof(kyber768_rand));
  35013. if (ret != 0)
  35014. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35015. ret = wc_KyberKey_EncodePublicKey(key, pub, KYBER768_PUBLIC_KEY_SIZE);
  35016. if (ret != 0)
  35017. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35018. ret = wc_KyberKey_EncodePrivateKey(key, priv, KYBER768_PRIVATE_KEY_SIZE);
  35019. if (ret != 0)
  35020. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35021. if (XMEMCMP(pub, kyber768_pk, sizeof(kyber768_pk)) != 0)
  35022. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35023. if (XMEMCMP(priv, kyber768_sk, sizeof(kyber768_sk)) != 0)
  35024. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35025. ret = wc_KyberKey_EncapsulateWithRandom(key, ct, ss, kyber768enc_rand,
  35026. sizeof(kyber768enc_rand));
  35027. if (ret != 0)
  35028. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35029. if (XMEMCMP(ct, kyber768_ct, sizeof(kyber768_ct)) != 0)
  35030. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35031. if (XMEMCMP(ss, kyber768_ss, sizeof(kyber768_ss)) != 0)
  35032. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35033. ret = wc_KyberKey_Decapsulate(key, ss_dec, ct, sizeof(kyber768_ct));
  35034. if (ret != 0)
  35035. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35036. if (XMEMCMP(ss_dec, kyber768_ss, sizeof(kyber768_ss)) != 0)
  35037. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35038. out:
  35039. if (key_inited)
  35040. wc_KyberKey_Free(key);
  35041. #ifdef WOLFSSL_SMALL_STACK
  35042. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35043. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35044. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35045. XFREE(ct, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35046. XFREE(ss, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35047. XFREE(ss_dec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35048. #endif
  35049. return ret;
  35050. }
  35051. #endif /* WOLFSSL_KYBER768 */
  35052. #ifdef WOLFSSL_KYBER1024
  35053. static wc_test_ret_t kyber1024_kat(void)
  35054. {
  35055. wc_test_ret_t ret;
  35056. #ifdef WOLFSSL_SMALL_STACK
  35057. KyberKey *key = NULL;
  35058. byte *priv = NULL;
  35059. byte *pub = NULL;
  35060. byte *ct = NULL;
  35061. byte *ss = NULL;
  35062. byte *ss_dec = NULL;
  35063. #else
  35064. KyberKey key[1];
  35065. byte priv[KYBER1024_PRIVATE_KEY_SIZE];
  35066. byte pub[KYBER1024_PUBLIC_KEY_SIZE];
  35067. byte ct[KYBER1024_CIPHER_TEXT_SIZE];
  35068. byte ss[KYBER_SS_SZ];
  35069. byte ss_dec[KYBER_SS_SZ];
  35070. #endif
  35071. int key_inited = 0;
  35072. WOLFSSL_SMALL_STACK_STATIC const byte kyber1024_rand[] = {
  35073. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  35074. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  35075. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  35076. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  35077. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  35078. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  35079. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  35080. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  35081. };
  35082. WOLFSSL_SMALL_STACK_STATIC const byte kyber1024enc_rand[] = {
  35083. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  35084. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  35085. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  35086. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  35087. };
  35088. WOLFSSL_SMALL_STACK_STATIC const byte kyber1024_pk[] = {
  35089. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  35090. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  35091. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  35092. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  35093. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  35094. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  35095. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  35096. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  35097. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  35098. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  35099. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  35100. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  35101. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  35102. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  35103. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  35104. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  35105. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  35106. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  35107. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  35108. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  35109. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  35110. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  35111. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  35112. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  35113. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  35114. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  35115. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  35116. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  35117. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  35118. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  35119. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  35120. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  35121. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  35122. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  35123. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  35124. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  35125. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  35126. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  35127. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  35128. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  35129. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  35130. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  35131. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  35132. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  35133. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  35134. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  35135. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  35136. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  35137. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  35138. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  35139. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  35140. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  35141. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  35142. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  35143. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  35144. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  35145. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  35146. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  35147. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  35148. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  35149. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  35150. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  35151. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  35152. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  35153. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  35154. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  35155. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  35156. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  35157. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  35158. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  35159. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  35160. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  35161. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  35162. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  35163. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  35164. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  35165. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  35166. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  35167. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  35168. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  35169. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  35170. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  35171. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  35172. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  35173. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  35174. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  35175. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  35176. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  35177. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  35178. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  35179. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  35180. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  35181. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  35182. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  35183. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  35184. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  35185. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  35186. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  35187. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  35188. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  35189. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  35190. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  35191. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  35192. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  35193. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  35194. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  35195. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  35196. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  35197. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  35198. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  35199. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  35200. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  35201. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  35202. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  35203. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  35204. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  35205. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  35206. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  35207. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  35208. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  35209. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  35210. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  35211. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  35212. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  35213. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  35214. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  35215. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  35216. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  35217. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  35218. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  35219. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  35220. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  35221. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  35222. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  35223. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  35224. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  35225. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  35226. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  35227. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  35228. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  35229. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  35230. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  35231. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  35232. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  35233. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  35234. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  35235. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  35236. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  35237. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  35238. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  35239. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  35240. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  35241. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  35242. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  35243. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  35244. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  35245. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  35246. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  35247. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  35248. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  35249. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  35250. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  35251. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  35252. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  35253. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  35254. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  35255. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  35256. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  35257. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  35258. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  35259. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  35260. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  35261. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  35262. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  35263. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  35264. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  35265. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  35266. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  35267. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  35268. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  35269. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  35270. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  35271. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  35272. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  35273. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  35274. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  35275. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  35276. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  35277. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  35278. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  35279. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  35280. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  35281. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  35282. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  35283. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  35284. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  35285. };
  35286. WOLFSSL_SMALL_STACK_STATIC const byte kyber1024_sk[] = {
  35287. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  35288. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  35289. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  35290. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  35291. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  35292. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  35293. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  35294. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  35295. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  35296. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  35297. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  35298. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  35299. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  35300. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  35301. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  35302. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  35303. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  35304. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  35305. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  35306. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  35307. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  35308. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  35309. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  35310. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  35311. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  35312. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  35313. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  35314. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  35315. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  35316. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  35317. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  35318. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  35319. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  35320. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  35321. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  35322. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  35323. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  35324. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  35325. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  35326. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  35327. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  35328. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  35329. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  35330. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  35331. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  35332. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  35333. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  35334. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  35335. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  35336. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  35337. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  35338. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  35339. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  35340. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  35341. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  35342. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  35343. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  35344. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  35345. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  35346. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  35347. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  35348. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  35349. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  35350. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  35351. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  35352. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  35353. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  35354. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  35355. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  35356. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  35357. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  35358. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  35359. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  35360. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  35361. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  35362. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  35363. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  35364. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  35365. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  35366. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  35367. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  35368. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  35369. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  35370. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  35371. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  35372. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  35373. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  35374. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  35375. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  35376. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  35377. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  35378. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  35379. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  35380. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  35381. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  35382. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  35383. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  35384. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  35385. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  35386. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  35387. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  35388. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  35389. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  35390. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  35391. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  35392. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  35393. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  35394. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  35395. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  35396. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  35397. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  35398. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  35399. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  35400. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  35401. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  35402. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  35403. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  35404. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  35405. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  35406. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  35407. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  35408. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  35409. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  35410. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  35411. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  35412. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  35413. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  35414. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  35415. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  35416. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  35417. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  35418. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  35419. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  35420. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  35421. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  35422. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  35423. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  35424. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  35425. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  35426. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  35427. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  35428. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  35429. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  35430. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  35431. 0x85, 0xA4, 0xC1, 0xA1, 0xAB, 0x9B, 0x6A, 0xEB,
  35432. 0x49, 0xCC, 0xE1, 0xC2, 0xF8, 0xA9, 0x7C, 0x35,
  35433. 0x16, 0xC7, 0x2A, 0x00, 0xA4, 0x62, 0x63, 0xBA,
  35434. 0xA6, 0x96, 0xBF, 0x25, 0x72, 0x77, 0x19, 0xC3,
  35435. 0x21, 0x64, 0x23, 0x61, 0x8F, 0xF3, 0x33, 0x80,
  35436. 0x93, 0x4A, 0x6C, 0x10, 0x54, 0x5C, 0x4C, 0x5C,
  35437. 0x51, 0x55, 0xB1, 0x24, 0x86, 0x18, 0x1F, 0xC7,
  35438. 0xA2, 0x31, 0x98, 0x73, 0x97, 0x8B, 0x6A, 0x2A,
  35439. 0x67, 0x49, 0x0F, 0x82, 0x56, 0xBD, 0x21, 0x96,
  35440. 0xFE, 0x17, 0x92, 0xA4, 0xC0, 0x00, 0x77, 0xB8,
  35441. 0x12, 0xEA, 0xE8, 0xBE, 0xD3, 0x57, 0x24, 0x99,
  35442. 0x68, 0x4A, 0xB3, 0x37, 0x18, 0x76, 0x76, 0x1E,
  35443. 0x45, 0x0C, 0x9F, 0x9D, 0x27, 0x68, 0xA3, 0x68,
  35444. 0x06, 0xD7, 0xAB, 0x20, 0x46, 0xC9, 0x1F, 0x17,
  35445. 0x59, 0x9E, 0x9A, 0xC5, 0x92, 0x99, 0x08, 0x08,
  35446. 0xDC, 0xD7, 0xB4, 0xD0, 0x91, 0x90, 0x72, 0xF1,
  35447. 0x4E, 0xC3, 0x61, 0x77, 0x3B, 0x72, 0x52, 0x44,
  35448. 0x4C, 0x32, 0x3C, 0x30, 0x83, 0x26, 0xF4, 0xA3,
  35449. 0x0F, 0x86, 0x80, 0xD2, 0xF7, 0x48, 0xF5, 0x6A,
  35450. 0x13, 0x2B, 0x82, 0x67, 0x4E, 0xD0, 0x18, 0x46,
  35451. 0x20, 0xB8, 0x2A, 0xD2, 0xCB, 0x18, 0x2C, 0x97,
  35452. 0xB4, 0x81, 0x62, 0x66, 0x47, 0x49, 0x12, 0x90,
  35453. 0xA0, 0x11, 0xCC, 0x73, 0x82, 0x86, 0x85, 0xA8,
  35454. 0xC3, 0x67, 0xA5, 0xB9, 0xCF, 0x8D, 0x62, 0x1B,
  35455. 0x0D, 0x5C, 0x1E, 0xFF, 0x03, 0x17, 0x27, 0x58,
  35456. 0xBD, 0x00, 0x49, 0x78, 0xC2, 0x51, 0xCD, 0x51,
  35457. 0x34, 0x22, 0x28, 0x98, 0x9C, 0xAE, 0x63, 0x32,
  35458. 0xAC, 0x48, 0x64, 0x37, 0xCB, 0x5C, 0x57, 0xD4,
  35459. 0x30, 0x74, 0x62, 0x86, 0x52, 0x53, 0xBE, 0x21,
  35460. 0x7B, 0x35, 0x15, 0xC7, 0x3D, 0xF4, 0x05, 0xB7,
  35461. 0xF2, 0x82, 0x17, 0xAD, 0x0B, 0x8C, 0xF6, 0x0C,
  35462. 0x2F, 0xFF, 0xAA, 0x0A, 0x00, 0x48, 0xB1, 0xFB,
  35463. 0x4A, 0xCD, 0xCD, 0xC3, 0x8B, 0x52, 0x50, 0xCF,
  35464. 0xEC, 0x35, 0x6A, 0x6D, 0xE2, 0x6C, 0xFA, 0x7A,
  35465. 0x58, 0x8F, 0xDC, 0x86, 0xF9, 0x8C, 0x85, 0x4A,
  35466. 0xC6, 0x4C, 0x7B, 0xFA, 0xA9, 0x6F, 0x5A, 0x32,
  35467. 0xCC, 0x06, 0x10, 0x93, 0x4B, 0xAA, 0x6A, 0x58,
  35468. 0x6B, 0x9A, 0x20, 0x54, 0xF1, 0x3B, 0xA2, 0x74,
  35469. 0x17, 0x4A, 0xA0, 0xD2, 0xB3, 0xA8, 0x1B, 0x96,
  35470. 0xA9, 0x40, 0x66, 0x6F, 0x78, 0x9B, 0x5A, 0x6B,
  35471. 0xCD, 0xC0, 0xA6, 0xA0, 0x17, 0x8A, 0x0C, 0x9A,
  35472. 0x02, 0x57, 0x8A, 0x49, 0x3F, 0x6E, 0xEA, 0x0D,
  35473. 0x2E, 0x6C, 0x13, 0x95, 0x1C, 0x9F, 0x24, 0x9A,
  35474. 0x5E, 0x8D, 0xD7, 0x1D, 0xD4, 0x9A, 0x74, 0x2D,
  35475. 0x45, 0x1F, 0x1A, 0xBB, 0xA1, 0x9A, 0xF8, 0xC5,
  35476. 0x47, 0x85, 0x5E, 0x0A, 0xFC, 0x72, 0x8E, 0x90,
  35477. 0xAB, 0xB4, 0x99, 0xC9, 0xBE, 0xEB, 0x76, 0x6F,
  35478. 0x47, 0x29, 0xCD, 0xA2, 0x22, 0x63, 0xE3, 0x24,
  35479. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  35480. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  35481. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  35482. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  35483. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  35484. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  35485. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  35486. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  35487. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  35488. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  35489. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  35490. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  35491. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  35492. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  35493. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  35494. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  35495. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  35496. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  35497. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  35498. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  35499. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  35500. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  35501. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  35502. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  35503. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  35504. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  35505. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  35506. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  35507. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  35508. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  35509. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  35510. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  35511. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  35512. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  35513. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  35514. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  35515. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  35516. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  35517. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  35518. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  35519. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  35520. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  35521. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  35522. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  35523. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  35524. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  35525. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  35526. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  35527. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  35528. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  35529. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  35530. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  35531. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  35532. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  35533. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  35534. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  35535. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  35536. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  35537. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  35538. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  35539. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  35540. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  35541. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  35542. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  35543. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  35544. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  35545. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  35546. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  35547. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  35548. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  35549. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  35550. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  35551. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  35552. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  35553. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  35554. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  35555. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  35556. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  35557. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  35558. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  35559. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  35560. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  35561. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  35562. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  35563. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  35564. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  35565. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  35566. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  35567. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  35568. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  35569. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  35570. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  35571. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  35572. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  35573. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  35574. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  35575. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  35576. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  35577. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  35578. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  35579. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  35580. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  35581. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  35582. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  35583. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  35584. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  35585. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  35586. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  35587. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  35588. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  35589. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  35590. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  35591. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  35592. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  35593. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  35594. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  35595. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  35596. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  35597. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  35598. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  35599. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  35600. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  35601. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  35602. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  35603. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  35604. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  35605. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  35606. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  35607. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  35608. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  35609. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  35610. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  35611. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  35612. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  35613. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  35614. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  35615. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  35616. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  35617. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  35618. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  35619. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  35620. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  35621. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  35622. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  35623. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  35624. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  35625. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  35626. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  35627. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  35628. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  35629. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  35630. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  35631. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  35632. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  35633. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  35634. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  35635. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  35636. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  35637. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  35638. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  35639. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  35640. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  35641. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  35642. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  35643. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  35644. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  35645. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  35646. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  35647. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  35648. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  35649. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  35650. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  35651. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  35652. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  35653. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  35654. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  35655. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  35656. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  35657. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  35658. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  35659. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  35660. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  35661. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  35662. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  35663. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  35664. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  35665. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  35666. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  35667. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  35668. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  35669. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  35670. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  35671. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  35672. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  35673. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  35674. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  35675. 0x8A, 0x39, 0xE8, 0x7D, 0x53, 0x1F, 0x35, 0x27,
  35676. 0xC2, 0x07, 0xED, 0xCC, 0x1D, 0xB7, 0xFA, 0xDD,
  35677. 0xCF, 0x96, 0x28, 0x39, 0x18, 0x79, 0xB3, 0x35,
  35678. 0xC7, 0x07, 0x83, 0x9A, 0x0D, 0xB0, 0x51, 0xA8,
  35679. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  35680. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  35681. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  35682. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  35683. };
  35684. WOLFSSL_SMALL_STACK_STATIC const byte kyber1024_ct[] = {
  35685. #ifndef WOLFSSL_ML_KEM
  35686. 0xA6, 0xAF, 0x29, 0xD5, 0xF5, 0xB8, 0x0B, 0xD1,
  35687. 0x30, 0xF5, 0x18, 0xBA, 0xDD, 0xD6, 0xC8, 0xF1,
  35688. 0x75, 0x45, 0x41, 0x3D, 0x86, 0x0F, 0xB3, 0xDE,
  35689. 0x45, 0x19, 0x79, 0xEB, 0xFA, 0x5E, 0x4E, 0x31,
  35690. 0x12, 0xC7, 0xC0, 0xAD, 0xF9, 0x98, 0x24, 0xBB,
  35691. 0x52, 0x6F, 0x2C, 0x35, 0x50, 0x74, 0x8E, 0xD0,
  35692. 0xE1, 0x34, 0xF0, 0x45, 0x7A, 0x7C, 0x61, 0xF9,
  35693. 0xF5, 0x26, 0xF0, 0x02, 0xBA, 0xAD, 0xC0, 0x3F,
  35694. 0xC1, 0x3E, 0x38, 0x13, 0x12, 0x19, 0x51, 0x3C,
  35695. 0x3E, 0xDE, 0x06, 0x16, 0x61, 0xE7, 0x4F, 0x60,
  35696. 0x3C, 0x4F, 0xCF, 0x79, 0x51, 0xC8, 0xE5, 0x2C,
  35697. 0x9C, 0x21, 0x3B, 0x0D, 0x22, 0xD9, 0x29, 0x36,
  35698. 0x63, 0xD6, 0x69, 0xA6, 0xB5, 0x8E, 0xD8, 0xFC,
  35699. 0xEF, 0xCF, 0x82, 0x49, 0xD7, 0xBB, 0x52, 0x98,
  35700. 0xF5, 0x57, 0x61, 0x44, 0x5B, 0x2B, 0x83, 0xCE,
  35701. 0x7F, 0x00, 0x5C, 0xB0, 0x42, 0x48, 0xAE, 0xC8,
  35702. 0xBD, 0xA2, 0x2F, 0xD2, 0xD4, 0x2A, 0xA7, 0x66,
  35703. 0x32, 0x20, 0x14, 0xEA, 0x03, 0x8C, 0xC3, 0x2C,
  35704. 0x55, 0xC8, 0xE4, 0xB9, 0xE2, 0x8E, 0xC9, 0x11,
  35705. 0x9F, 0x52, 0x73, 0x41, 0xE4, 0xF6, 0x6A, 0x03,
  35706. 0x51, 0x21, 0x07, 0x3B, 0x85, 0xDE, 0x67, 0x06,
  35707. 0xDA, 0x19, 0xE0, 0x83, 0x8A, 0x9F, 0x33, 0xB7,
  35708. 0x19, 0xA6, 0x8F, 0x03, 0x9B, 0x66, 0x4D, 0xC0,
  35709. 0x02, 0x65, 0x9E, 0xAB, 0xFC, 0x39, 0x86, 0x79,
  35710. 0xAA, 0x70, 0x09, 0xCE, 0x0C, 0xD0, 0x1C, 0xDA,
  35711. 0xFB, 0x6C, 0xD2, 0xA2, 0x6F, 0xE4, 0x10, 0x16,
  35712. 0x72, 0xC9, 0x8F, 0xF5, 0x8F, 0x7C, 0x47, 0xD5,
  35713. 0xBD, 0xA2, 0x90, 0x66, 0x53, 0xB3, 0xA6, 0xF9,
  35714. 0x65, 0x1F, 0x7A, 0x12, 0x1E, 0xA7, 0x7E, 0xA7,
  35715. 0x47, 0x23, 0xFA, 0xE5, 0xB8, 0x73, 0xF9, 0xBB,
  35716. 0x7B, 0x66, 0x4F, 0x0C, 0x8A, 0x93, 0x83, 0x1E,
  35717. 0xF9, 0xD5, 0x1C, 0x7C, 0xC1, 0xEF, 0x44, 0xAC,
  35718. 0x0E, 0x55, 0xA5, 0x5C, 0xA7, 0x6D, 0x13, 0x7F,
  35719. 0xE9, 0xB7, 0x5F, 0x40, 0x50, 0x9C, 0xEF, 0x15,
  35720. 0x6E, 0x5A, 0xD1, 0x8F, 0x9F, 0xB9, 0x99, 0x68,
  35721. 0x00, 0x08, 0xE5, 0x47, 0xD5, 0x5E, 0xEC, 0xD5,
  35722. 0xB4, 0xD1, 0xCB, 0x1D, 0x9F, 0x07, 0x6C, 0xEC,
  35723. 0x21, 0x50, 0x1C, 0x74, 0x02, 0x50, 0x9E, 0xCB,
  35724. 0x77, 0xAF, 0xB2, 0xCB, 0x9A, 0x61, 0x34, 0x0A,
  35725. 0x8B, 0xD1, 0x51, 0x4C, 0x6E, 0x71, 0xB4, 0xAA,
  35726. 0x45, 0xE4, 0x7E, 0xC3, 0x75, 0x12, 0x27, 0x1B,
  35727. 0x91, 0x1F, 0x8F, 0xB4, 0x6C, 0x90, 0x82, 0xC9,
  35728. 0xDF, 0x07, 0x20, 0x4A, 0xBB, 0x5A, 0x50, 0xE6,
  35729. 0xE3, 0x64, 0x7A, 0x8A, 0xD4, 0xD8, 0xD5, 0xD7,
  35730. 0xBF, 0xF1, 0x9C, 0x8A, 0x50, 0x93, 0x08, 0xBC,
  35731. 0xFB, 0x89, 0x55, 0x36, 0xD0, 0x45, 0xCA, 0x2B,
  35732. 0x97, 0xCB, 0x16, 0xA2, 0x9B, 0xB7, 0x18, 0x1C,
  35733. 0xAD, 0x05, 0x09, 0xDD, 0xB9, 0x17, 0x35, 0x02,
  35734. 0x8E, 0xBA, 0x8C, 0x31, 0xD7, 0x4B, 0xD2, 0x75,
  35735. 0xEA, 0xA6, 0x5B, 0x53, 0x40, 0xB3, 0xA4, 0x3F,
  35736. 0xBF, 0xE0, 0xB3, 0x06, 0x1D, 0x6B, 0xAE, 0x7E,
  35737. 0x75, 0xB7, 0x09, 0x8C, 0xDA, 0xBE, 0x91, 0xD4,
  35738. 0xB3, 0x1E, 0x36, 0xC9, 0xAA, 0x7A, 0x82, 0x98,
  35739. 0x86, 0x2A, 0xD6, 0x3C, 0x8F, 0xD2, 0x82, 0xE0,
  35740. 0x3B, 0x46, 0x0B, 0x3A, 0xB4, 0x64, 0xCE, 0x0F,
  35741. 0x27, 0xB1, 0xC3, 0xD1, 0x11, 0x55, 0xAC, 0xAA,
  35742. 0x01, 0x1E, 0xB9, 0xE2, 0xAE, 0x3E, 0x6D, 0xDA,
  35743. 0x07, 0xD6, 0xF4, 0x91, 0x73, 0x7C, 0xBC, 0xE9,
  35744. 0xB0, 0x5F, 0x9B, 0xC5, 0x6B, 0xE2, 0x0E, 0x8D,
  35745. 0x32, 0x6B, 0xA1, 0x32, 0xC5, 0x7F, 0xB2, 0x35,
  35746. 0x16, 0x11, 0x44, 0x51, 0x9C, 0xDF, 0x40, 0x56,
  35747. 0x0F, 0xBE, 0x27, 0x9B, 0xDE, 0x41, 0x1E, 0x11,
  35748. 0x25, 0x31, 0xF8, 0x26, 0xD6, 0xAB, 0x10, 0xD4,
  35749. 0x54, 0x73, 0x50, 0xAD, 0xD2, 0xA9, 0xDE, 0x8D,
  35750. 0x62, 0xC2, 0xAC, 0x82, 0xCA, 0xBE, 0x68, 0x15,
  35751. 0x64, 0x6F, 0x4D, 0xC9, 0x74, 0x2B, 0xB0, 0xC2,
  35752. 0xA3, 0xF7, 0x7E, 0xC7, 0xB4, 0x6C, 0x6B, 0x53,
  35753. 0x76, 0x05, 0xFA, 0x31, 0x79, 0x8C, 0xD8, 0x92,
  35754. 0x81, 0x22, 0x1A, 0x33, 0xDF, 0xB9, 0x79, 0x6E,
  35755. 0x64, 0x43, 0x05, 0x63, 0x03, 0x32, 0xC2, 0xCB,
  35756. 0x93, 0x14, 0x08, 0xAB, 0x48, 0x1A, 0x16, 0xD9,
  35757. 0x53, 0xF6, 0xBE, 0xAE, 0x38, 0x91, 0xD6, 0xD9,
  35758. 0xAC, 0x1F, 0xAB, 0x38, 0x22, 0x2D, 0x92, 0x71,
  35759. 0x87, 0x2D, 0x9D, 0x0C, 0xAD, 0xB9, 0x1A, 0xBE,
  35760. 0x9B, 0x4E, 0x26, 0x5F, 0x75, 0xC6, 0xE5, 0xE8,
  35761. 0x29, 0xE1, 0x46, 0xC3, 0xD8, 0xCE, 0x1E, 0x9D,
  35762. 0x12, 0xE0, 0xD1, 0x29, 0x80, 0x19, 0x57, 0xF4,
  35763. 0x6B, 0x0D, 0x2D, 0xBE, 0x1F, 0x74, 0x9B, 0x1D,
  35764. 0x08, 0xE2, 0x34, 0x5F, 0x62, 0x39, 0xA7, 0x31,
  35765. 0x34, 0x2E, 0xB7, 0x5B, 0x0C, 0xF1, 0xBF, 0x41,
  35766. 0x17, 0x49, 0xBC, 0x2C, 0xAF, 0x28, 0x10, 0xB7,
  35767. 0x88, 0xC6, 0xB7, 0x23, 0x8B, 0x4D, 0x3D, 0xA2,
  35768. 0xD6, 0x31, 0x5C, 0xE9, 0x54, 0x2E, 0x24, 0x40,
  35769. 0x4F, 0x14, 0x57, 0x55, 0xA3, 0x0A, 0xB8, 0x51,
  35770. 0xE4, 0x44, 0x58, 0x41, 0xBD, 0x33, 0xF7, 0x16,
  35771. 0xA5, 0x86, 0x88, 0x48, 0x88, 0xEC, 0xC6, 0xBC,
  35772. 0x64, 0x98, 0xAA, 0x32, 0x91, 0x9A, 0xE8, 0x1D,
  35773. 0x20, 0xC2, 0x69, 0x73, 0xC2, 0xBD, 0x54, 0x58,
  35774. 0x2A, 0x0F, 0x6A, 0xD9, 0x8A, 0xBF, 0xD2, 0x62,
  35775. 0x7E, 0x15, 0x69, 0x0A, 0x72, 0x7E, 0x69, 0xF5,
  35776. 0x81, 0xDD, 0x2A, 0x71, 0x27, 0x98, 0x2A, 0x90,
  35777. 0xE3, 0x3E, 0x2D, 0x4A, 0x03, 0xFE, 0x33, 0x91,
  35778. 0x42, 0xC7, 0xE4, 0x4C, 0x32, 0x6A, 0xC4, 0x6E,
  35779. 0xD3, 0x95, 0xA2, 0x25, 0xD3, 0x03, 0x33, 0x89,
  35780. 0x91, 0x73, 0x28, 0xB4, 0x53, 0x16, 0xB1, 0x58,
  35781. 0x5A, 0x01, 0xB2, 0xC3, 0x04, 0xB2, 0x94, 0x4E,
  35782. 0x90, 0x3A, 0xBB, 0xB3, 0xEC, 0x56, 0x19, 0x44,
  35783. 0x1C, 0xFC, 0x89, 0x65, 0xA4, 0x46, 0xDF, 0x75,
  35784. 0xDE, 0xFA, 0x80, 0xC6, 0xE1, 0x5A, 0xDB, 0xD5,
  35785. 0x06, 0xB7, 0xAB, 0x2D, 0xE1, 0x2D, 0xDA, 0x9B,
  35786. 0xC8, 0x14, 0x41, 0xCF, 0xC8, 0x90, 0x52, 0xE2,
  35787. 0xE5, 0x80, 0x8F, 0x71, 0x26, 0xC6, 0xFD, 0x3A,
  35788. 0xC6, 0xAC, 0x80, 0x81, 0x25, 0x8A, 0x84, 0xA0,
  35789. 0x9A, 0xE5, 0x0F, 0x6C, 0xD7, 0xCC, 0x0F, 0x4A,
  35790. 0xF3, 0x36, 0xFD, 0x1D, 0x64, 0x3E, 0x99, 0x07,
  35791. 0x99, 0x96, 0x26, 0x8C, 0x2D, 0x32, 0xD9, 0x09,
  35792. 0xF2, 0x2E, 0x35, 0x04, 0xF0, 0x7F, 0xBB, 0x56,
  35793. 0x31, 0x96, 0xD4, 0x31, 0x2F, 0xDD, 0xB9, 0x33,
  35794. 0x5D, 0x5C, 0x1D, 0x36, 0xE8, 0xC5, 0xEE, 0xA2,
  35795. 0x27, 0x8D, 0xBA, 0x23, 0xB9, 0x4D, 0x19, 0x3C,
  35796. 0x94, 0x7C, 0xC4, 0x1C, 0xA9, 0x93, 0xDC, 0x7D,
  35797. 0xB1, 0x39, 0x63, 0x40, 0xAD, 0x9C, 0x4F, 0xE6,
  35798. 0x87, 0xDD, 0x7B, 0x8D, 0x0C, 0x7A, 0x51, 0x20,
  35799. 0xAE, 0x02, 0x04, 0xF2, 0xC6, 0x65, 0xBD, 0x5F,
  35800. 0x47, 0x3D, 0x64, 0x4C, 0x7F, 0xF2, 0x6B, 0xFF,
  35801. 0xBA, 0x7A, 0x36, 0x98, 0x08, 0x30, 0x70, 0x21,
  35802. 0x28, 0xA7, 0xE6, 0x61, 0xD6, 0x77, 0xA0, 0x92,
  35803. 0xA3, 0x6E, 0x74, 0x28, 0xA4, 0x13, 0x9F, 0xB2,
  35804. 0x9B, 0x00, 0x95, 0xCC, 0x11, 0x08, 0x6F, 0x44,
  35805. 0x7D, 0x2A, 0x9E, 0xF6, 0xC9, 0xB1, 0x61, 0xF1,
  35806. 0x89, 0xC6, 0x29, 0x9E, 0x08, 0x4C, 0xB7, 0xAA,
  35807. 0x00, 0xFA, 0xF7, 0x87, 0x79, 0x7B, 0xFB, 0x06,
  35808. 0x9F, 0xBC, 0x08, 0x7F, 0xDE, 0x26, 0x25, 0x2A,
  35809. 0x16, 0x64, 0xF1, 0x9C, 0x5A, 0x8A, 0x22, 0xEC,
  35810. 0x5E, 0xE1, 0xAE, 0xB0, 0x76, 0x35, 0x7B, 0x7D,
  35811. 0xC3, 0x7E, 0x6B, 0x0F, 0x15, 0x20, 0xF9, 0x58,
  35812. 0xF7, 0x85, 0x1B, 0xAC, 0xB9, 0x2C, 0x89, 0xFD,
  35813. 0x11, 0x4A, 0x72, 0xFE, 0xAC, 0x54, 0x65, 0x2D,
  35814. 0x45, 0xB0, 0x9E, 0x1A, 0xE7, 0x65, 0x1A, 0xBD,
  35815. 0x16, 0x4B, 0xCD, 0x53, 0x7D, 0x58, 0xFA, 0x39,
  35816. 0xD3, 0xEC, 0x8A, 0xCD, 0xCD, 0xF9, 0x84, 0x25,
  35817. 0x00, 0x58, 0x62, 0xFA, 0x59, 0x69, 0x2D, 0xE1,
  35818. 0x62, 0xB7, 0x7E, 0x62, 0x97, 0xC6, 0x62, 0x33,
  35819. 0x34, 0x84, 0x08, 0xA8, 0xAB, 0x69, 0x5C, 0xE2,
  35820. 0xF2, 0x72, 0x8D, 0xB9, 0xFB, 0xE2, 0x7E, 0x95,
  35821. 0x89, 0x67, 0xEC, 0x59, 0x74, 0x76, 0x7C, 0x5A,
  35822. 0x66, 0x02, 0x30, 0x74, 0xB4, 0xA7, 0x1A, 0xFD,
  35823. 0x26, 0x4A, 0xD2, 0x89, 0x0E, 0x97, 0x0A, 0x1F,
  35824. 0x31, 0xD6, 0xE3, 0x31, 0x1B, 0x73, 0x6F, 0x9F,
  35825. 0x94, 0x88, 0x79, 0x3D, 0xDC, 0x88, 0xF2, 0x34,
  35826. 0x58, 0x06, 0x42, 0x54, 0xC8, 0x2A, 0x1D, 0x9E,
  35827. 0x59, 0xEA, 0xD2, 0xFC, 0xEC, 0x40, 0xB4, 0x30,
  35828. 0x68, 0x7C, 0x4B, 0x7E, 0x28, 0x96, 0x09, 0x26,
  35829. 0xAF, 0xCA, 0xCC, 0x9B, 0xD7, 0x56, 0xA7, 0x10,
  35830. 0x88, 0xC7, 0x84, 0x50, 0xE2, 0x0A, 0x2E, 0x98,
  35831. 0x0A, 0xED, 0xE9, 0xEB, 0xED, 0xFE, 0x7F, 0xAB,
  35832. 0xD6, 0xAB, 0xFE, 0x96, 0xF9, 0x34, 0xC4, 0xB0,
  35833. 0x2C, 0x01, 0xCA, 0x19, 0x4D, 0x01, 0xB7, 0x3C,
  35834. 0x25, 0xD5, 0x99, 0x70, 0x39, 0xD3, 0xFC, 0xD0,
  35835. 0xF0, 0x99, 0x52, 0x1F, 0x70, 0xCA, 0xEE, 0x69,
  35836. 0x11, 0x0A, 0xC1, 0xFC, 0x5A, 0x99, 0x91, 0x7A,
  35837. 0xD7, 0x52, 0xFC, 0x96, 0xAD, 0xFA, 0xD7, 0x18,
  35838. 0x6D, 0x0A, 0x7C, 0x9C, 0xFE, 0x56, 0x01, 0xC0,
  35839. 0x75, 0x14, 0xEA, 0x64, 0x48, 0xD6, 0x61, 0xC5,
  35840. 0x7A, 0xA2, 0x02, 0x42, 0x10, 0x3C, 0x42, 0x76,
  35841. 0xA0, 0x70, 0xA4, 0x89, 0xA4, 0xCB, 0x6B, 0xCA,
  35842. 0x0F, 0x9E, 0xCC, 0x43, 0x79, 0xFB, 0x22, 0x02,
  35843. 0x15, 0xFD, 0x91, 0xF8, 0x10, 0x19, 0xD5, 0xB0,
  35844. 0xAE, 0x61, 0x93, 0x58, 0xB5, 0x24, 0x68, 0xF2,
  35845. 0x72, 0xC1, 0x78, 0xE3, 0xA7, 0x4C, 0xF6, 0x77,
  35846. 0x5A, 0xA9, 0x24, 0xFE, 0x32, 0x9C, 0x31, 0x75,
  35847. 0xD9, 0xE4, 0xC3, 0xE2, 0x1A, 0xB9, 0xEC, 0x83,
  35848. 0x6E, 0xDC, 0x3A, 0xCA, 0xB2, 0xE3, 0x89, 0x1E,
  35849. 0xE8, 0xDE, 0xDA, 0x51, 0x5D, 0x39, 0xAF, 0x9B,
  35850. 0x8D, 0xDD, 0x0E, 0xE7, 0xB0, 0x16, 0x4F, 0x80,
  35851. 0x5C, 0x38, 0x35, 0xF6, 0xD2, 0xBA, 0xBD, 0xB3,
  35852. 0x0E, 0xAB, 0x47, 0x56, 0xE7, 0xEC, 0x7F, 0x82,
  35853. 0x9E, 0xCE, 0x01, 0xE8, 0xEA, 0xDF, 0xBB, 0xED,
  35854. 0x12, 0xFC, 0x28, 0x3B, 0x3D, 0x4C, 0x69, 0xF5,
  35855. 0x75, 0xE7, 0xF8, 0x04, 0x17, 0x68, 0x9F, 0xDF,
  35856. 0xCF, 0xC7, 0xBE, 0x27, 0xEE, 0x3B, 0x8C, 0xDF,
  35857. 0x57, 0xAA, 0xEB, 0xEC, 0x4A, 0x95, 0xB7, 0xE5,
  35858. 0xBB, 0x58, 0x5B, 0x85, 0x22, 0x7F, 0x7C, 0x32,
  35859. 0xBE, 0x30, 0xDB, 0x3E, 0x65, 0xE4, 0x2E, 0x30,
  35860. 0xDC, 0xF5, 0xA5, 0xFA, 0x07, 0x3D, 0xBA, 0x39,
  35861. 0x9D, 0x94, 0x2F, 0x22, 0x22, 0xAD, 0xB9, 0xB9,
  35862. 0x89, 0x81, 0x02, 0xAF, 0xE5, 0x43, 0x2E, 0xDC,
  35863. 0x7F, 0x04, 0xAE, 0x34, 0xA8, 0xFE, 0xC2, 0xD8,
  35864. 0x1C, 0xB4, 0x9A, 0x9A, 0x9B, 0x43, 0x81, 0x4C,
  35865. 0xE7, 0x1D, 0x97, 0xF7, 0x26, 0xE2, 0xB1, 0xE8,
  35866. 0xF6, 0x4B, 0x50, 0xE6, 0x5D, 0xFB, 0x48, 0x16,
  35867. 0xE1, 0x2E, 0x82, 0xA3, 0x19, 0x74, 0x84, 0xA4,
  35868. 0xE9, 0xBB, 0xA4, 0xD2, 0xD6, 0x9E, 0x3F, 0x19,
  35869. 0xD0, 0xB7, 0x5C, 0x21, 0xE2, 0xBF, 0xFE, 0x9F,
  35870. 0xC0, 0xC9, 0x8C, 0xF4, 0x8A, 0x3A, 0xAF, 0x08,
  35871. 0xD4, 0x67, 0xF7, 0x26, 0x87, 0xDF, 0x01, 0x78,
  35872. 0x17, 0x4B, 0x78, 0x97, 0xF7, 0x34, 0x34, 0x9B,
  35873. 0x18, 0x1E, 0xCA, 0x86, 0xA5, 0x98, 0xA0, 0xC5,
  35874. 0xE8, 0xC2, 0x59, 0x46, 0xF2, 0x4D, 0xC5, 0x57,
  35875. 0x2B, 0xD3, 0x24, 0xA4, 0x04, 0x58, 0xA7, 0x88,
  35876. 0xE5, 0x13, 0x7F, 0x3C, 0x7A, 0x7C, 0x97, 0xFC,
  35877. 0x9F, 0x12, 0xA3, 0xC4, 0x63, 0xA8, 0xFE, 0x94,
  35878. 0x49, 0x10, 0x1C, 0xCE, 0x96, 0x6D, 0x7C, 0x00,
  35879. 0x93, 0x23, 0x93, 0x29, 0x98, 0xD5, 0x6E, 0xF4,
  35880. 0x30, 0xC7, 0x3B, 0xC2, 0x4F, 0x5D, 0x95, 0xF7,
  35881. 0x37, 0x85, 0x8D, 0xDC, 0x4F, 0x32, 0xC0, 0x13
  35882. #else
  35883. 0xb1, 0x56, 0x96, 0xac, 0xab, 0xf3, 0xf5, 0xc7,
  35884. 0x1c, 0x09, 0x60, 0x5d, 0xf3, 0x50, 0xf9, 0x8e,
  35885. 0xf9, 0x89, 0x74, 0x74, 0xf2, 0x41, 0xc7, 0xf7,
  35886. 0xd1, 0x6f, 0x7a, 0x69, 0x60, 0x43, 0x58, 0x50,
  35887. 0x84, 0x58, 0x91, 0x6d, 0x2c, 0x85, 0x52, 0xc7,
  35888. 0x04, 0xcb, 0x6e, 0x0d, 0xa4, 0x30, 0x5a, 0x11,
  35889. 0x72, 0x0a, 0x2b, 0x59, 0xa6, 0xd8, 0x19, 0x0f,
  35890. 0xc3, 0xe3, 0x89, 0xe6, 0x55, 0x1e, 0x7c, 0x59,
  35891. 0x57, 0x8f, 0xe2, 0xb0, 0x5d, 0x75, 0x91, 0xba,
  35892. 0xb3, 0x26, 0xd8, 0x94, 0xe2, 0x36, 0x56, 0xc6,
  35893. 0xb5, 0xfe, 0x4b, 0x7a, 0xbd, 0x65, 0x05, 0xa8,
  35894. 0xc2, 0x6d, 0x1d, 0xf8, 0xb4, 0x4a, 0x0e, 0xd5,
  35895. 0x3a, 0xff, 0xff, 0x9a, 0x58, 0x5f, 0xce, 0x86,
  35896. 0xda, 0x3b, 0x3f, 0xf2, 0x2b, 0x1f, 0xda, 0xe2,
  35897. 0xf6, 0xd2, 0x55, 0xc2, 0x65, 0x35, 0xf6, 0x16,
  35898. 0x94, 0xd5, 0x04, 0x71, 0xf1, 0xd8, 0x4d, 0x17,
  35899. 0x67, 0x3e, 0x48, 0x1f, 0x4e, 0x82, 0x4c, 0x82,
  35900. 0x81, 0x0c, 0xe8, 0xf2, 0xec, 0xc9, 0xbb, 0x3f,
  35901. 0x5f, 0x07, 0xbf, 0x14, 0x30, 0xe7, 0xbf, 0xd9,
  35902. 0x9f, 0xb9, 0x8f, 0x3d, 0x2f, 0x94, 0x1e, 0xdf,
  35903. 0x64, 0x2d, 0x7b, 0x9e, 0x73, 0xbc, 0x15, 0x95,
  35904. 0xa0, 0x4c, 0xd9, 0x49, 0xfa, 0x76, 0x64, 0x68,
  35905. 0x48, 0x9f, 0x40, 0x2f, 0x92, 0x32, 0x5f, 0x6d,
  35906. 0x9e, 0xc5, 0xd6, 0x96, 0xe0, 0xfa, 0xc2, 0xc6,
  35907. 0xa3, 0x79, 0x83, 0xa6, 0x45, 0x13, 0x84, 0xce,
  35908. 0x9d, 0x9d, 0xe4, 0x48, 0xf1, 0x58, 0x74, 0xe5,
  35909. 0x30, 0x94, 0x3e, 0x8e, 0x0f, 0xe2, 0x99, 0x60,
  35910. 0x58, 0x7a, 0x89, 0xb8, 0x05, 0x2c, 0x9d, 0x0e,
  35911. 0x6b, 0x0e, 0xa5, 0xdb, 0xb7, 0x34, 0xd2, 0xa9,
  35912. 0xc0, 0x5c, 0xbb, 0x6d, 0x0c, 0x79, 0xf4, 0xa5,
  35913. 0x7d, 0xb5, 0xaf, 0xde, 0xd6, 0xa7, 0xdf, 0x0e,
  35914. 0xcf, 0x47, 0xb7, 0xa3, 0x1d, 0xea, 0xf3, 0x7d,
  35915. 0xfa, 0x1d, 0xc8, 0x97, 0x22, 0xa4, 0x7b, 0x40,
  35916. 0xb4, 0xa5, 0x0c, 0x7a, 0x2f, 0x32, 0xe8, 0xab,
  35917. 0x3d, 0xa9, 0x73, 0xaa, 0x68, 0x3c, 0x0a, 0x69,
  35918. 0x82, 0x94, 0xca, 0x3a, 0x04, 0x54, 0x83, 0x17,
  35919. 0x5d, 0x87, 0x78, 0x6b, 0x47, 0xf7, 0x8a, 0xb0,
  35920. 0x29, 0x5b, 0x82, 0x67, 0xed, 0xfe, 0xed, 0xa4,
  35921. 0x42, 0xc3, 0x51, 0xa3, 0x8e, 0x95, 0xcf, 0x43,
  35922. 0xb0, 0x83, 0x42, 0xc6, 0x7d, 0x0b, 0xc5, 0xdd,
  35923. 0x59, 0x74, 0xf6, 0xc5, 0xc0, 0x03, 0xea, 0x31,
  35924. 0xb5, 0x80, 0x4a, 0x31, 0x1c, 0x29, 0xeb, 0xa1,
  35925. 0x7b, 0xdb, 0x54, 0x7a, 0x62, 0x9e, 0xc8, 0x39,
  35926. 0x74, 0x04, 0x3b, 0xd0, 0x3a, 0x37, 0xd0, 0xec,
  35927. 0x7f, 0xf3, 0x9d, 0xe1, 0xdf, 0xb8, 0xe6, 0x32,
  35928. 0xa8, 0x6b, 0x26, 0x02, 0x1e, 0x75, 0x3e, 0x7d,
  35929. 0xc7, 0x31, 0xbc, 0x4e, 0xd7, 0xe7, 0xfe, 0xce,
  35930. 0x78, 0xb0, 0x7d, 0xff, 0x5e, 0x87, 0x75, 0xb2,
  35931. 0x22, 0x3e, 0x19, 0xdf, 0xb3, 0xa0, 0x6a, 0x18,
  35932. 0x65, 0xed, 0x4f, 0x08, 0xa8, 0x45, 0x44, 0x49,
  35933. 0x25, 0x04, 0xf2, 0x96, 0x2e, 0x00, 0x5f, 0xde,
  35934. 0x5d, 0xe6, 0xe4, 0xff, 0x48, 0x99, 0x4d, 0x4f,
  35935. 0xf8, 0x11, 0xce, 0x31, 0xe3, 0x90, 0x98, 0x03,
  35936. 0xc3, 0x53, 0x4c, 0x1c, 0x6c, 0x6d, 0xdc, 0x9e,
  35937. 0x1a, 0x43, 0xc8, 0x45, 0xe7, 0xe7, 0xe2, 0xa1,
  35938. 0x08, 0x11, 0x86, 0xbe, 0xde, 0x4b, 0x5b, 0xcf,
  35939. 0x1b, 0x80, 0xb1, 0x3d, 0x21, 0x8a, 0x4a, 0xb4,
  35940. 0x46, 0xf4, 0x79, 0xe3, 0x8c, 0x1d, 0xe4, 0xa5,
  35941. 0x94, 0xe0, 0xf2, 0x0c, 0xdc, 0x23, 0xa9, 0xea,
  35942. 0x58, 0x77, 0x8c, 0xb2, 0xfb, 0x10, 0x4f, 0x1d,
  35943. 0xc9, 0x1b, 0xa5, 0xc1, 0x7e, 0x74, 0x00, 0x4c,
  35944. 0x43, 0x0a, 0xab, 0x1a, 0x3d, 0x16, 0x79, 0xda,
  35945. 0xeb, 0x50, 0x82, 0xe5, 0x17, 0xaf, 0x6a, 0x4e,
  35946. 0x28, 0xf5, 0x64, 0x44, 0x1b, 0x73, 0x23, 0x5d,
  35947. 0x08, 0x4a, 0x58, 0x31, 0xcb, 0xb3, 0x94, 0xce,
  35948. 0xcd, 0x99, 0x7f, 0xe0, 0x8b, 0x1b, 0x4a, 0xa9,
  35949. 0x95, 0xbd, 0xd9, 0x72, 0x6c, 0x04, 0x61, 0x58,
  35950. 0x59, 0xff, 0xfd, 0x1c, 0x99, 0x06, 0xfb, 0x2d,
  35951. 0x04, 0x01, 0xed, 0x6f, 0x59, 0x1e, 0x13, 0xc6,
  35952. 0xe7, 0x9e, 0xc5, 0xe8, 0x62, 0x07, 0x9e, 0x6d,
  35953. 0xfd, 0x3c, 0xa6, 0x88, 0xfa, 0x5a, 0xc7, 0xf8,
  35954. 0xd2, 0x79, 0x07, 0x53, 0x05, 0x49, 0xac, 0x71,
  35955. 0xfd, 0xeb, 0x5f, 0x86, 0x9d, 0x2a, 0x9b, 0x23,
  35956. 0x5a, 0x0e, 0x54, 0xb3, 0xb8, 0x66, 0x02, 0x1d,
  35957. 0x6b, 0x5c, 0x98, 0xd1, 0xc6, 0xc0, 0xc0, 0x04,
  35958. 0x89, 0x99, 0x5e, 0x3c, 0x34, 0x96, 0xb6, 0x9c,
  35959. 0x76, 0x33, 0x8a, 0xc4, 0x22, 0xc9, 0xf9, 0x41,
  35960. 0x58, 0xed, 0x5c, 0xf9, 0xcc, 0x6b, 0xe9, 0x38,
  35961. 0x46, 0x03, 0x4d, 0x4b, 0xa5, 0x33, 0xa3, 0x42,
  35962. 0x2c, 0x29, 0xd6, 0x75, 0x40, 0x5a, 0xe8, 0x53,
  35963. 0xb8, 0x49, 0x7d, 0xc9, 0x12, 0xf4, 0xa8, 0x35,
  35964. 0x00, 0xc8, 0x9d, 0xb7, 0x6b, 0xcd, 0xd7, 0x04,
  35965. 0x6d, 0x98, 0x32, 0xfa, 0x3d, 0x2b, 0xf3, 0xd0,
  35966. 0x24, 0x65, 0xeb, 0xb4, 0x16, 0x80, 0x35, 0x11,
  35967. 0x8f, 0x08, 0x7d, 0xdd, 0x47, 0x64, 0x3d, 0x2d,
  35968. 0xb7, 0x1f, 0x47, 0x41, 0x9a, 0xed, 0x97, 0x3e,
  35969. 0xc4, 0x38, 0x48, 0x66, 0xd4, 0x5e, 0x1d, 0xf2,
  35970. 0x16, 0xed, 0xa5, 0xbf, 0x61, 0x33, 0xf8, 0x4a,
  35971. 0x32, 0x8c, 0x11, 0x29, 0x62, 0x1e, 0xc1, 0x50,
  35972. 0x1f, 0xc4, 0x6f, 0xe3, 0xed, 0xa4, 0x60, 0x68,
  35973. 0x0b, 0x39, 0x7a, 0x42, 0xa4, 0x36, 0x8e, 0xb3,
  35974. 0xdc, 0x88, 0x45, 0x1b, 0x2b, 0x61, 0x6e, 0xec,
  35975. 0x5f, 0x1e, 0xc0, 0x5e, 0x5b, 0xd2, 0x08, 0x4b,
  35976. 0x49, 0x45, 0xd2, 0x0e, 0xa8, 0x63, 0x0d, 0x81,
  35977. 0x49, 0x2c, 0x78, 0xcf, 0x06, 0xa7, 0x8e, 0xc5,
  35978. 0x08, 0xa0, 0xc5, 0x71, 0x3b, 0x48, 0x60, 0x21,
  35979. 0x64, 0x32, 0x66, 0xa6, 0x0d, 0x99, 0x74, 0x46,
  35980. 0x78, 0xe5, 0x98, 0xb1, 0x52, 0xcf, 0x18, 0x29,
  35981. 0x89, 0x7a, 0xf7, 0x20, 0xc1, 0x8e, 0xb6, 0x89,
  35982. 0x3d, 0x8b, 0x1f, 0x81, 0xc8, 0xbb, 0xc5, 0x5c,
  35983. 0x80, 0x47, 0x39, 0x1a, 0x90, 0x51, 0xa6, 0xd0,
  35984. 0xd6, 0x54, 0x71, 0x89, 0x1a, 0xc2, 0xdf, 0xd8,
  35985. 0xac, 0x98, 0x4a, 0xa7, 0xaf, 0xf3, 0x97, 0x44,
  35986. 0x92, 0xb7, 0x51, 0xfd, 0xc5, 0xde, 0xa1, 0x39,
  35987. 0x68, 0x86, 0xe8, 0xc9, 0x4b, 0x94, 0x48, 0xe1,
  35988. 0xee, 0x63, 0x3f, 0x6c, 0x21, 0x43, 0x55, 0x2e,
  35989. 0xb8, 0x49, 0xc6, 0xd7, 0xe7, 0x2b, 0x6a, 0x7c,
  35990. 0x81, 0x21, 0x46, 0x15, 0x44, 0x51, 0x5e, 0x34,
  35991. 0x6d, 0x69, 0x97, 0x31, 0x11, 0xc9, 0x24, 0xac,
  35992. 0xda, 0x06, 0x47, 0x0b, 0x5d, 0xf2, 0x32, 0x0e,
  35993. 0x06, 0xb8, 0xf1, 0xaf, 0xf4, 0x13, 0xf9, 0xe8,
  35994. 0x88, 0x43, 0x6c, 0x31, 0x37, 0x80, 0x25, 0xbe,
  35995. 0x66, 0x72, 0x6a, 0x0b, 0xa5, 0x59, 0x17, 0x7b,
  35996. 0x11, 0x37, 0xc4, 0x31, 0xcc, 0xe1, 0xa6, 0xfb,
  35997. 0x00, 0xde, 0x36, 0xc6, 0xaf, 0xce, 0x92, 0xf1,
  35998. 0x1b, 0xc7, 0x5a, 0x88, 0x5f, 0xbb, 0xd9, 0xb1,
  35999. 0x08, 0xe6, 0xea, 0xd4, 0x3f, 0xee, 0x47, 0xda,
  36000. 0x31, 0x12, 0x30, 0xf9, 0x22, 0xba, 0xb9, 0x3a,
  36001. 0x76, 0x77, 0x71, 0x1e, 0x97, 0xea, 0x41, 0x29,
  36002. 0x80, 0x2c, 0x5d, 0xff, 0x20, 0xd0, 0xd9, 0xdb,
  36003. 0x97, 0xf8, 0x82, 0xc6, 0x33, 0x79, 0xfc, 0x27,
  36004. 0xd0, 0x8a, 0x6f, 0xec, 0xc2, 0x88, 0xcc, 0x06,
  36005. 0xe2, 0x57, 0x65, 0x4b, 0x38, 0x8e, 0x50, 0x18,
  36006. 0x80, 0x1f, 0xf1, 0x9b, 0xac, 0x2c, 0x94, 0x87,
  36007. 0xa5, 0xf4, 0x7e, 0x07, 0x31, 0x01, 0xdd, 0x37,
  36008. 0xb1, 0x0d, 0x43, 0xc9, 0x35, 0x11, 0x9b, 0x6f,
  36009. 0x70, 0xed, 0xdf, 0x9b, 0xa5, 0x14, 0x9e, 0xd6,
  36010. 0x4e, 0x8c, 0x12, 0x9d, 0x97, 0x8c, 0xbf, 0x2c,
  36011. 0x1a, 0x30, 0x6f, 0x83, 0xa6, 0x34, 0x7b, 0xfc,
  36012. 0x44, 0x5e, 0x8f, 0xd6, 0x45, 0xab, 0x0e, 0x4b,
  36013. 0x2a, 0x93, 0x93, 0x28, 0xcb, 0x55, 0xa7, 0x95,
  36014. 0x1a, 0x3a, 0x93, 0x8a, 0x06, 0xae, 0xd2, 0x4e,
  36015. 0xf3, 0x25, 0x62, 0x14, 0x9c, 0x8b, 0x7f, 0x2d,
  36016. 0xad, 0xd7, 0x5d, 0x2d, 0xb3, 0x78, 0x2b, 0x64,
  36017. 0xf4, 0xcb, 0x1f, 0x56, 0xa0, 0x94, 0x52, 0x77,
  36018. 0xc9, 0xc5, 0xe1, 0x60, 0x5a, 0x0c, 0x0e, 0xb6,
  36019. 0x5d, 0x7f, 0xe3, 0x42, 0x1c, 0x90, 0x87, 0x4b,
  36020. 0xb9, 0xcc, 0xb9, 0x45, 0xea, 0x74, 0xc9, 0x97,
  36021. 0xec, 0xa7, 0x3c, 0x94, 0x05, 0x9b, 0x77, 0xc7,
  36022. 0xc3, 0xf0, 0x8d, 0xd6, 0xe5, 0xe4, 0x94, 0xe3,
  36023. 0xf2, 0x33, 0x99, 0xf2, 0xfc, 0xe0, 0x56, 0xe9,
  36024. 0xf1, 0x30, 0xfe, 0xb6, 0xce, 0xcb, 0xb3, 0xe8,
  36025. 0xde, 0xce, 0x49, 0xe5, 0x67, 0x37, 0x57, 0x51,
  36026. 0x57, 0x42, 0x09, 0x4a, 0xc3, 0x82, 0x56, 0x38,
  36027. 0x57, 0xb5, 0xd4, 0x12, 0xd7, 0x19, 0x1c, 0xdc,
  36028. 0x5d, 0xe0, 0x68, 0x1c, 0x72, 0xc5, 0xda, 0xf8,
  36029. 0x33, 0xbe, 0xed, 0xcd, 0x88, 0xe4, 0x01, 0x23,
  36030. 0xa6, 0x17, 0xd2, 0xbd, 0x0d, 0x75, 0x95, 0x91,
  36031. 0x6a, 0x68, 0x94, 0xb9, 0x26, 0x3b, 0x6e, 0xb9,
  36032. 0x1e, 0x23, 0x80, 0x15, 0x4e, 0x0c, 0xfc, 0x52,
  36033. 0x78, 0x42, 0x58, 0xcf, 0x73, 0x20, 0xc4, 0xf0,
  36034. 0x2e, 0xad, 0x0b, 0x44, 0x71, 0xea, 0x0d, 0xd0,
  36035. 0xab, 0xeb, 0x5e, 0x63, 0x62, 0x8f, 0x48, 0x36,
  36036. 0xa2, 0x3d, 0xb2, 0x69, 0xce, 0xe0, 0x9e, 0x46,
  36037. 0xf3, 0x5b, 0x9d, 0xe8, 0x7e, 0xcb, 0x04, 0x22,
  36038. 0xad, 0x8e, 0xc6, 0xcb, 0x60, 0xb7, 0x17, 0xee,
  36039. 0xc2, 0x12, 0x16, 0xf0, 0xb7, 0x91, 0xc8, 0x52,
  36040. 0xa9, 0xf8, 0xa4, 0xc2, 0x61, 0x67, 0xd7, 0x35,
  36041. 0x0e, 0x17, 0xfa, 0x3e, 0xc7, 0x45, 0xb4, 0x63,
  36042. 0x11, 0xa6, 0x71, 0xcb, 0x8d, 0x7f, 0x14, 0x88,
  36043. 0x5f, 0xf0, 0x1b, 0xab, 0x0e, 0xf4, 0x69, 0xd0,
  36044. 0x85, 0x0f, 0xe8, 0x07, 0xe7, 0x0d, 0x36, 0xa4,
  36045. 0xb7, 0x36, 0xb2, 0xc3, 0x2c, 0x67, 0x62, 0x58,
  36046. 0xae, 0x7d, 0x55, 0x3d, 0xca, 0xb8, 0xad, 0xe6,
  36047. 0x36, 0x8a, 0xa2, 0x31, 0x79, 0x77, 0x8a, 0xdd,
  36048. 0x21, 0x4d, 0xd9, 0x15, 0x4f, 0x51, 0x0f, 0x50,
  36049. 0xe5, 0x56, 0x0c, 0x65, 0xfc, 0xec, 0x35, 0xe8,
  36050. 0x78, 0x82, 0x53, 0x0e, 0xdc, 0x65, 0xbe, 0xe6,
  36051. 0x6b, 0x25, 0xa5, 0x35, 0xda, 0x21, 0x46, 0xe1,
  36052. 0x4f, 0x83, 0x90, 0xbc, 0xf5, 0x79, 0xc3, 0x37,
  36053. 0xba, 0xd9, 0x74, 0x57, 0x5d, 0x5f, 0x09, 0xa8,
  36054. 0x76, 0x88, 0x83, 0xf9, 0x39, 0x3a, 0x14, 0x41,
  36055. 0x71, 0xfe, 0xb3, 0xfc, 0x66, 0x3e, 0xee, 0xf1,
  36056. 0x38, 0xb2, 0x1b, 0xd5, 0x57, 0xab, 0xbb, 0xad,
  36057. 0x2c, 0x02, 0x24, 0x34, 0x1f, 0xab, 0xfb, 0x6c,
  36058. 0x6f, 0xde, 0xa6, 0xf3, 0xc2, 0x65, 0xe5, 0xd8,
  36059. 0xaf, 0x16, 0x9a, 0x96, 0x4a, 0x7a, 0x86, 0xa1,
  36060. 0xa0, 0xe2, 0x22, 0xdd, 0x63, 0x71, 0x38, 0x20,
  36061. 0x79, 0xf8, 0x38, 0x23, 0x4a, 0xea, 0x8f, 0xf6,
  36062. 0xe2, 0xb8, 0x94, 0xe0, 0x4a, 0x9a, 0x34, 0xcf,
  36063. 0xac, 0xab, 0x81, 0xb0, 0x70, 0x4b, 0x14, 0xd7,
  36064. 0xd1, 0x09, 0x94, 0x9c, 0x68, 0x64, 0x4c, 0x01,
  36065. 0x5b, 0xc2, 0xf2, 0x6a, 0x9c, 0x29, 0x42, 0x87,
  36066. 0xec, 0xf1, 0x52, 0xd8, 0x46, 0xf9, 0x14, 0xc0,
  36067. 0x74, 0x5c, 0xeb, 0x7e, 0xc3, 0xd3, 0x6a, 0xd9,
  36068. 0xd7, 0x23, 0xc1, 0x5f, 0x89, 0xe2, 0xd8, 0xfe,
  36069. 0x2c, 0x18, 0xe1, 0x11, 0x80, 0xd0, 0x70, 0x9d,
  36070. 0x70, 0x02, 0xa5, 0x0f, 0x87, 0xd1, 0xc7, 0x3d,
  36071. 0xbb, 0x61, 0x6f, 0xd8, 0xde, 0x30, 0x33, 0x9d,
  36072. 0x83, 0xda, 0x5a, 0x8e, 0xd5, 0x03, 0x21, 0xcc,
  36073. 0x12, 0xf9, 0x4d, 0xff, 0x84, 0xd9, 0xe5, 0x44,
  36074. 0xf1, 0x7e, 0xc3, 0x4a, 0x60, 0xeb, 0x41, 0x4b,
  36075. 0x28, 0x78, 0xcc, 0x1a, 0x05, 0xc2, 0x64, 0xa0,
  36076. 0x66, 0xdd, 0xd3, 0x7f, 0x0c, 0x50, 0x4f, 0x0e,
  36077. 0x6c, 0xcd, 0x0f, 0x31, 0x1e, 0xb5, 0x21, 0x2d,
  36078. 0xe3, 0x2e, 0x9d, 0x65, 0x51, 0x25, 0xe4, 0xf4
  36079. #endif
  36080. };
  36081. WOLFSSL_SMALL_STACK_STATIC const byte kyber1024_ss[] = {
  36082. #ifndef WOLFSSL_ML_KEM
  36083. 0xB1, 0x0F, 0x73, 0x94, 0x92, 0x6A, 0xD3, 0xB4,
  36084. 0x9C, 0x5D, 0x62, 0xD5, 0xAE, 0xB5, 0x31, 0xD5,
  36085. 0x75, 0x75, 0x38, 0xBC, 0xC0, 0xDA, 0x9E, 0x55,
  36086. 0x0D, 0x43, 0x8F, 0x1B, 0x61, 0xBD, 0x74, 0x19
  36087. #else
  36088. 0x63, 0xa1, 0x03, 0x90, 0x74, 0xf0, 0x1f, 0x26,
  36089. 0x51, 0x21, 0x3a, 0xd9, 0x35, 0x0d, 0x65, 0x61,
  36090. 0xcb, 0x03, 0xa6, 0x04, 0x00, 0xe7, 0x41, 0x18,
  36091. 0xbb, 0x44, 0x64, 0xd8, 0x7b, 0x9d, 0xb2, 0x05
  36092. #endif
  36093. };
  36094. #ifdef WOLFSSL_SMALL_STACK
  36095. key = (KyberKey *)XMALLOC(sizeof(KyberKey), HEAP_HINT,
  36096. DYNAMIC_TYPE_TMP_BUFFER);
  36097. priv = (byte *)XMALLOC(KYBER1024_PRIVATE_KEY_SIZE, HEAP_HINT,
  36098. DYNAMIC_TYPE_TMP_BUFFER);
  36099. pub = (byte *)XMALLOC(KYBER1024_PUBLIC_KEY_SIZE, HEAP_HINT,
  36100. DYNAMIC_TYPE_TMP_BUFFER);
  36101. ct = (byte *)XMALLOC(KYBER1024_CIPHER_TEXT_SIZE, HEAP_HINT,
  36102. DYNAMIC_TYPE_TMP_BUFFER);
  36103. ss = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  36104. DYNAMIC_TYPE_TMP_BUFFER);
  36105. ss_dec = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  36106. DYNAMIC_TYPE_TMP_BUFFER);
  36107. if (! (key && priv && pub && ct && ss && ss_dec))
  36108. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36109. #endif
  36110. ret = wc_KyberKey_Init(KYBER1024, key, HEAP_HINT, INVALID_DEVID);
  36111. if (ret != 0)
  36112. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36113. else
  36114. key_inited = 1;
  36115. ret = wc_KyberKey_MakeKeyWithRandom(key, kyber1024_rand,
  36116. sizeof(kyber1024_rand));
  36117. if (ret != 0)
  36118. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36119. ret = wc_KyberKey_EncodePublicKey(key, pub, KYBER_MAX_PUBLIC_KEY_SIZE);
  36120. if (ret != 0)
  36121. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36122. ret = wc_KyberKey_EncodePrivateKey(key, priv, KYBER_MAX_PRIVATE_KEY_SIZE);
  36123. if (ret != 0)
  36124. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36125. if (XMEMCMP(pub, kyber1024_pk, sizeof(kyber1024_pk)) != 0)
  36126. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36127. if (XMEMCMP(priv, kyber1024_sk, sizeof(kyber1024_sk)) != 0)
  36128. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36129. ret = wc_KyberKey_EncapsulateWithRandom(key, ct, ss, kyber1024enc_rand,
  36130. sizeof(kyber1024enc_rand));
  36131. if (ret != 0)
  36132. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36133. if (XMEMCMP(ct, kyber1024_ct, sizeof(kyber1024_ct)) != 0)
  36134. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36135. if (XMEMCMP(ss, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  36136. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36137. ret = wc_KyberKey_Decapsulate(key, ss_dec, ct, sizeof(kyber1024_ct));
  36138. if (ret != 0)
  36139. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36140. if (XMEMCMP(ss_dec, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  36141. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36142. out:
  36143. if (key_inited)
  36144. wc_KyberKey_Free(key);
  36145. #ifdef WOLFSSL_SMALL_STACK
  36146. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36147. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36148. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36149. XFREE(ct, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36150. XFREE(ss, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36151. XFREE(ss_dec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36152. #endif
  36153. return ret;
  36154. }
  36155. #endif /* WOLFSSL_KYBER1024 */
  36156. #endif /* WOLFSSL_WC_KYBER */
  36157. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t kyber_test(void)
  36158. {
  36159. wc_test_ret_t ret;
  36160. WC_RNG rng;
  36161. int i;
  36162. #ifdef WOLFSSL_SMALL_STACK
  36163. KyberKey *key = NULL;
  36164. byte *priv = NULL;
  36165. byte *pub = NULL;
  36166. byte *priv2 = NULL;
  36167. byte *pub2 = NULL;
  36168. byte *ct = NULL;
  36169. byte *ss = NULL;
  36170. byte *ss_dec = NULL;
  36171. #else
  36172. KyberKey key[1];
  36173. byte priv[KYBER_MAX_PRIVATE_KEY_SIZE];
  36174. byte pub[KYBER_MAX_PUBLIC_KEY_SIZE];
  36175. byte priv2[KYBER_MAX_PRIVATE_KEY_SIZE];
  36176. byte pub2[KYBER_MAX_PUBLIC_KEY_SIZE];
  36177. byte ct[KYBER_MAX_CIPHER_TEXT_SIZE];
  36178. byte ss[KYBER_SS_SZ];
  36179. byte ss_dec[KYBER_SS_SZ];
  36180. #endif
  36181. int key_inited = 0;
  36182. static const int testData[][4] = {
  36183. #ifdef WOLFSSL_KYBER512
  36184. { KYBER512, KYBER512_PRIVATE_KEY_SIZE, KYBER512_PUBLIC_KEY_SIZE,
  36185. KYBER512_CIPHER_TEXT_SIZE },
  36186. #endif
  36187. #ifdef WOLFSSL_KYBER768
  36188. { KYBER768, KYBER768_PRIVATE_KEY_SIZE, KYBER768_PUBLIC_KEY_SIZE,
  36189. KYBER768_CIPHER_TEXT_SIZE },
  36190. #endif
  36191. #ifdef WOLFSSL_KYBER1024
  36192. { KYBER1024, KYBER1024_PRIVATE_KEY_SIZE, KYBER1024_PUBLIC_KEY_SIZE,
  36193. KYBER1024_CIPHER_TEXT_SIZE },
  36194. #endif
  36195. };
  36196. WOLFSSL_ENTER("kyber_test");
  36197. #ifdef WOLFSSL_SMALL_STACK
  36198. key = (KyberKey *)XMALLOC(sizeof(KyberKey), HEAP_HINT,
  36199. DYNAMIC_TYPE_TMP_BUFFER);
  36200. priv = (byte *)XMALLOC(KYBER_MAX_PRIVATE_KEY_SIZE, HEAP_HINT,
  36201. DYNAMIC_TYPE_TMP_BUFFER);
  36202. pub = (byte *)XMALLOC(KYBER_MAX_PUBLIC_KEY_SIZE, HEAP_HINT,
  36203. DYNAMIC_TYPE_TMP_BUFFER);
  36204. priv2 = (byte *)XMALLOC(KYBER_MAX_PRIVATE_KEY_SIZE, HEAP_HINT,
  36205. DYNAMIC_TYPE_TMP_BUFFER);
  36206. pub2 = (byte *)XMALLOC(KYBER_MAX_PUBLIC_KEY_SIZE, HEAP_HINT,
  36207. DYNAMIC_TYPE_TMP_BUFFER);
  36208. ct = (byte *)XMALLOC(KYBER_MAX_CIPHER_TEXT_SIZE, HEAP_HINT,
  36209. DYNAMIC_TYPE_TMP_BUFFER);
  36210. ss = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  36211. DYNAMIC_TYPE_TMP_BUFFER);
  36212. ss_dec = (byte *)XMALLOC(KYBER_SS_SZ, HEAP_HINT,
  36213. DYNAMIC_TYPE_TMP_BUFFER);
  36214. if (! (key && priv && pub && priv2 && pub2 && ct && ss && ss_dec))
  36215. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36216. #endif
  36217. #ifndef HAVE_FIPS
  36218. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  36219. #else
  36220. ret = wc_InitRng(&rng);
  36221. #endif
  36222. if (ret != 0)
  36223. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36224. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  36225. ret = wc_KyberKey_Init(testData[i][0], key, HEAP_HINT, INVALID_DEVID);
  36226. if (ret != 0)
  36227. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  36228. else
  36229. key_inited = 1;
  36230. ret = wc_KyberKey_MakeKey(key, &rng);
  36231. if (ret != 0)
  36232. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  36233. ret = wc_KyberKey_EncodePublicKey(key, pub, testData[i][2]);
  36234. if (ret != 0)
  36235. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  36236. ret = wc_KyberKey_EncodePrivateKey(key, priv, testData[i][1]);
  36237. if (ret != 0)
  36238. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  36239. ret = wc_KyberKey_Init(testData[i][0], key, HEAP_HINT, INVALID_DEVID);
  36240. if (ret != 0)
  36241. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  36242. ret = wc_KyberKey_DecodePublicKey(key, pub, testData[i][2]);
  36243. if (ret != 0)
  36244. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  36245. ret = wc_KyberKey_Encapsulate(key, ct, ss, &rng);
  36246. if (ret != 0)
  36247. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  36248. ret = wc_KyberKey_EncodePublicKey(key, pub2, testData[i][2]);
  36249. if (ret != 0)
  36250. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  36251. if (XMEMCMP(pub, pub2, testData[i][2]) != 0)
  36252. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  36253. ret = wc_KyberKey_Init(testData[i][0], key, HEAP_HINT, INVALID_DEVID);
  36254. if (ret != 0)
  36255. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  36256. ret = wc_KyberKey_DecodePrivateKey(key, priv, testData[i][1]);
  36257. if (ret != 0)
  36258. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  36259. ret = wc_KyberKey_Decapsulate(key, ss_dec, ct, testData[i][3]);
  36260. if (ret != 0)
  36261. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  36262. if (XMEMCMP(ss, ss_dec, KYBER_SS_SZ) != 0)
  36263. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  36264. ret = wc_KyberKey_EncodePrivateKey(key, priv2, testData[i][1]);
  36265. if (ret != 0)
  36266. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  36267. if (XMEMCMP(priv, priv2, testData[i][2]) != 0)
  36268. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  36269. }
  36270. wc_FreeRng(&rng);
  36271. #ifdef WOLFSSL_WC_KYBER
  36272. #ifdef WOLFSSL_KYBER512
  36273. ret = kyber512_kat();
  36274. if (ret != 0)
  36275. goto out;
  36276. #endif
  36277. #ifdef WOLFSSL_KYBER768
  36278. ret = kyber768_kat();
  36279. if (ret != 0)
  36280. goto out;
  36281. #endif
  36282. #ifdef WOLFSSL_KYBER1024
  36283. ret = kyber1024_kat();
  36284. if (ret != 0)
  36285. goto out;
  36286. #endif
  36287. #endif /* WOLFSSL_WC_KYBER */
  36288. out:
  36289. if (key_inited)
  36290. wc_KyberKey_Free(key);
  36291. #ifdef WOLFSSL_SMALL_STACK
  36292. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36293. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36294. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36295. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36296. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36297. XFREE(ct, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36298. XFREE(ss, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36299. XFREE(ss_dec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36300. #endif
  36301. return ret;
  36302. }
  36303. #endif /* WOLFSSL_HAVE_KYBER */
  36304. #ifdef HAVE_DILITHIUM
  36305. #ifndef WOLFSSL_DILITHIUM_NO_VERIFY
  36306. static wc_test_ret_t dilithium_param_vfy_test(int param, const byte* pubKey,
  36307. word32 pubKeyLen, const byte* sig, word32 sigLen)
  36308. {
  36309. byte msg[512];
  36310. dilithium_key* key;
  36311. wc_test_ret_t ret;
  36312. int i;
  36313. int res = 0;
  36314. key = (dilithium_key*)XMALLOC(sizeof(*key), HEAP_HINT,
  36315. DYNAMIC_TYPE_TMP_BUFFER);
  36316. if (key == NULL) {
  36317. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36318. }
  36319. /* make dummy msg */
  36320. for (i = 0; i < (int)sizeof(msg); i++) {
  36321. msg[i] = (byte)i;
  36322. }
  36323. ret = wc_dilithium_init(key);
  36324. if (ret != 0) {
  36325. ret = WC_TEST_RET_ENC_EC(ret);
  36326. return ret;
  36327. }
  36328. ret = wc_dilithium_set_level(key, param);
  36329. if (ret != 0)
  36330. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36331. ret = wc_dilithium_import_public(pubKey, pubKeyLen, key);
  36332. if (ret != 0)
  36333. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36334. ret = wc_dilithium_verify_msg(sig, sigLen, msg, (word32)sizeof(msg), &res,
  36335. key);
  36336. if (ret != 0)
  36337. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36338. if (res != 1)
  36339. ERROR_OUT(WC_TEST_RET_ENC_EC(res), out);
  36340. out:
  36341. wc_dilithium_free(key);
  36342. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36343. return ret;
  36344. }
  36345. #ifndef WOLFSSL_NO_ML_DSA_44
  36346. static wc_test_ret_t dilithium_param_44_vfy_test(void)
  36347. {
  36348. WOLFSSL_SMALL_STACK_STATIC const byte ml_dsa_44_pub_key[] = {
  36349. 0xea, 0x05, 0x24, 0x0d, 0x80, 0x72, 0x25, 0x55, 0xf4, 0x5b,
  36350. 0xc2, 0x13, 0x8b, 0x87, 0x5d, 0x31, 0x99, 0x2f, 0x1d, 0xa9,
  36351. 0x41, 0x09, 0x05, 0x76, 0xa7, 0xb7, 0x5e, 0x8c, 0x44, 0xe2,
  36352. 0x64, 0x79, 0xa0, 0xec, 0x1f, 0x24, 0xb6, 0xc8, 0x05, 0x5b,
  36353. 0xc1, 0x18, 0xb0, 0xb7, 0xcf, 0x8c, 0x60, 0x67, 0x6b, 0x81,
  36354. 0x44, 0x27, 0xb6, 0x0e, 0xfd, 0x9b, 0xc3, 0xcb, 0x52, 0x31,
  36355. 0xfa, 0xc9, 0x34, 0x8d, 0x22, 0x1e, 0x07, 0x9d, 0x96, 0x6a,
  36356. 0x63, 0x83, 0x5c, 0xd7, 0x83, 0x2d, 0x7f, 0x48, 0x64, 0x79,
  36357. 0xca, 0xb4, 0x9f, 0xa2, 0x02, 0xb7, 0x86, 0x1d, 0x0e, 0xc7,
  36358. 0xf9, 0x6c, 0x07, 0xc0, 0x35, 0x6a, 0x34, 0x79, 0x7c, 0xb8,
  36359. 0x0f, 0xed, 0x98, 0x50, 0xfb, 0x51, 0xe0, 0x36, 0x44, 0x4c,
  36360. 0xc6, 0x35, 0xa2, 0xbb, 0x55, 0xb0, 0x5c, 0x39, 0x08, 0x02,
  36361. 0x20, 0x35, 0x5c, 0x56, 0x6d, 0x2e, 0xb9, 0xef, 0x21, 0x26,
  36362. 0x87, 0x87, 0x85, 0x8a, 0x32, 0xb5, 0xa7, 0x68, 0x70, 0x3a,
  36363. 0xfd, 0x0d, 0x21, 0x48, 0x91, 0xa3, 0x29, 0xc1, 0x2a, 0x38,
  36364. 0xe5, 0x26, 0x31, 0x1f, 0x42, 0xde, 0x0b, 0x25, 0xff, 0x1d,
  36365. 0x6b, 0xb4, 0xe0, 0x5d, 0x2d, 0xcf, 0x44, 0xd5, 0x7d, 0xc4,
  36366. 0xf6, 0x95, 0xf2, 0x06, 0x4f, 0x83, 0x88, 0x9d, 0x1e, 0xeb,
  36367. 0x1c, 0x09, 0x45, 0x62, 0x67, 0x3d, 0xff, 0x51, 0x47, 0xe8,
  36368. 0xbc, 0x9b, 0x03, 0x1f, 0xc7, 0x72, 0x65, 0xce, 0xa8, 0x8c,
  36369. 0xc2, 0xa0, 0xc2, 0xbd, 0x5b, 0x7c, 0x17, 0x16, 0x8b, 0x72,
  36370. 0xfa, 0xb1, 0xbd, 0xdf, 0x49, 0xd6, 0xa1, 0x00, 0x65, 0xbe,
  36371. 0x82, 0xe7, 0x68, 0xc7, 0xe7, 0xbc, 0xc2, 0xa4, 0xdb, 0xaa,
  36372. 0xcc, 0xea, 0x41, 0x52, 0x7f, 0x56, 0xb4, 0x68, 0x1f, 0x92,
  36373. 0x96, 0x0f, 0xce, 0xd4, 0xd0, 0x87, 0x4c, 0x4a, 0x73, 0xb5,
  36374. 0x6c, 0xd4, 0x69, 0x55, 0x15, 0x47, 0xdc, 0x94, 0x7f, 0xd2,
  36375. 0x54, 0x5e, 0xb2, 0x90, 0xc2, 0x47, 0xe4, 0xf5, 0xde, 0x8b,
  36376. 0x9b, 0xc6, 0x5d, 0x50, 0x95, 0x60, 0xe0, 0xf0, 0xa7, 0x4e,
  36377. 0xe0, 0xcd, 0x41, 0x09, 0xef, 0xb3, 0x3d, 0x90, 0x5c, 0x77,
  36378. 0x54, 0xec, 0x9e, 0x5d, 0x8a, 0xe7, 0x09, 0x5c, 0xc9, 0x58,
  36379. 0x0c, 0xd0, 0x42, 0x35, 0xd2, 0x14, 0x59, 0x38, 0x69, 0xad,
  36380. 0xf9, 0xb5, 0xbf, 0x8a, 0x8e, 0x33, 0xd8, 0x5e, 0x7a, 0x55,
  36381. 0xd0, 0x53, 0x15, 0x40, 0x4e, 0xc5, 0x86, 0xd7, 0x8f, 0x5f,
  36382. 0x2f, 0x55, 0x82, 0xc2, 0x4f, 0x16, 0xe5, 0xea, 0x1c, 0xbc,
  36383. 0xff, 0x5e, 0x1f, 0x39, 0x46, 0x70, 0x54, 0x7a, 0x3a, 0x27,
  36384. 0x16, 0x1a, 0x2b, 0x6c, 0xd2, 0xb7, 0x80, 0xd3, 0xd1, 0x9d,
  36385. 0x25, 0x59, 0xed, 0xe6, 0x51, 0xb1, 0xf2, 0xad, 0x7e, 0x51,
  36386. 0x78, 0x14, 0x2b, 0x19, 0xae, 0x64, 0x72, 0x0f, 0xd8, 0x18,
  36387. 0x79, 0x8e, 0x66, 0x88, 0xd3, 0xa4, 0xa3, 0xc3, 0x76, 0x21,
  36388. 0xcb, 0xe4, 0x79, 0x5e, 0x95, 0x74, 0xe3, 0x31, 0x18, 0x79,
  36389. 0xed, 0xc7, 0xe7, 0xfb, 0x86, 0x48, 0x1b, 0x7b, 0x75, 0x5b,
  36390. 0x7f, 0x7c, 0x82, 0xc5, 0xab, 0x11, 0xb4, 0x5d, 0x59, 0x6f,
  36391. 0x78, 0xb2, 0xa5, 0x39, 0xc6, 0x63, 0x38, 0x6c, 0xeb, 0x50,
  36392. 0x06, 0x14, 0x76, 0xf0, 0xe8, 0xfb, 0x11, 0x95, 0x1f, 0x9d,
  36393. 0x9c, 0xa6, 0xe1, 0xe2, 0x0d, 0xa3, 0x66, 0xfc, 0x20, 0x83,
  36394. 0x50, 0x0e, 0x53, 0x75, 0xb5, 0x12, 0xf4, 0xdf, 0x31, 0x46,
  36395. 0x83, 0xac, 0x5b, 0xf3, 0x99, 0xa6, 0xd1, 0x7b, 0x2b, 0xc5,
  36396. 0xdc, 0x71, 0x07, 0x27, 0x33, 0x35, 0x34, 0xf5, 0x30, 0x19,
  36397. 0xc1, 0x3b, 0xba, 0x8a, 0xaf, 0x7e, 0x49, 0x93, 0x48, 0x5b,
  36398. 0x38, 0xc0, 0xbc, 0x2e, 0xc7, 0x59, 0x1b, 0xd9, 0xf5, 0xcc,
  36399. 0x86, 0xf5, 0x7b, 0x4d, 0xd7, 0x39, 0xa7, 0xa2, 0x56, 0x20,
  36400. 0x48, 0x98, 0x7d, 0x4f, 0x75, 0x56, 0x9b, 0xb8, 0x95, 0x45,
  36401. 0x17, 0xf3, 0x86, 0x3d, 0x97, 0x0a, 0x49, 0x1b, 0xca, 0xff,
  36402. 0x20, 0xc0, 0x24, 0x2c, 0x51, 0xc2, 0x0a, 0x3c, 0xbf, 0x07,
  36403. 0x60, 0x1c, 0x88, 0x85, 0x9b, 0x85, 0x2d, 0x4a, 0xfe, 0x5a,
  36404. 0x1c, 0x90, 0xf5, 0x90, 0x12, 0xd3, 0x03, 0x3c, 0x8c, 0x2e,
  36405. 0x95, 0x4a, 0x47, 0x76, 0x0f, 0x1f, 0x5d, 0x9e, 0xed, 0xc5,
  36406. 0x64, 0xc4, 0x9b, 0xbf, 0x86, 0xc5, 0x63, 0x84, 0x33, 0x00,
  36407. 0xf1, 0x26, 0x18, 0x21, 0xf3, 0x88, 0x1a, 0x08, 0x18, 0x6d,
  36408. 0x2f, 0xef, 0xd5, 0xeb, 0x2f, 0x69, 0xc8, 0x6e, 0x92, 0x34,
  36409. 0xfc, 0x72, 0x3d, 0x9a, 0xa7, 0x9e, 0x51, 0xfb, 0x56, 0xe3,
  36410. 0xdc, 0xf4, 0x8f, 0x9b, 0x6d, 0x0d, 0x2a, 0xec, 0x66, 0x12,
  36411. 0x26, 0x35, 0xbd, 0x61, 0xc2, 0x67, 0x19, 0xf5, 0x7e, 0xa1,
  36412. 0x67, 0xa2, 0x9c, 0x3b, 0x67, 0xb0, 0xc2, 0x51, 0x6a, 0x37,
  36413. 0x7c, 0x48, 0xe9, 0x4b, 0xb9, 0xa3, 0x38, 0x2f, 0xfc, 0xde,
  36414. 0xb4, 0x7c, 0xda, 0x52, 0x84, 0x0b, 0xb0, 0xd9, 0x08, 0xe9,
  36415. 0x7a, 0x4a, 0x6f, 0x79, 0x29, 0x3d, 0xc4, 0x5c, 0x78, 0xee,
  36416. 0x63, 0xb6, 0x96, 0x68, 0xd9, 0x82, 0x4e, 0xc1, 0x1b, 0x6f,
  36417. 0x52, 0xf5, 0xb3, 0xfb, 0xe8, 0xc4, 0x2a, 0x07, 0xc6, 0x3b,
  36418. 0x85, 0x0d, 0xf4, 0xbf, 0xb0, 0x6b, 0xfb, 0xce, 0x1d, 0xb4,
  36419. 0xbf, 0x63, 0x0b, 0x91, 0x67, 0xc4, 0xa3, 0x06, 0xa4, 0xaf,
  36420. 0x6c, 0xd3, 0xe5, 0x8b, 0x87, 0x4e, 0x64, 0x9c, 0xb1, 0xf3,
  36421. 0x70, 0x7c, 0x68, 0x43, 0x46, 0x13, 0x46, 0xee, 0x27, 0x75,
  36422. 0x12, 0x45, 0x42, 0xde, 0xa5, 0x8d, 0xcf, 0xf7, 0x09, 0x87,
  36423. 0xa8, 0x80, 0x3d, 0xb6, 0x45, 0xee, 0x41, 0x2d, 0x7c, 0x45,
  36424. 0x01, 0x9d, 0xaa, 0x78, 0xa8, 0x10, 0xa4, 0xfd, 0xb5, 0x5f,
  36425. 0xee, 0x0f, 0x77, 0xba, 0x73, 0xff, 0x49, 0xdc, 0xfa, 0x39,
  36426. 0xd6, 0xa3, 0x6f, 0x25, 0xb9, 0x63, 0x2c, 0x92, 0xc5, 0xdf,
  36427. 0xfb, 0xba, 0x89, 0xf9, 0xfa, 0x94, 0x5b, 0x6f, 0x5a, 0x4d,
  36428. 0x1c, 0xe4, 0xc9, 0x10, 0xf9, 0xa0, 0xe8, 0xc4, 0xcb, 0x55,
  36429. 0x1a, 0xdb, 0x56, 0x5f, 0x8e, 0x91, 0x03, 0x23, 0xca, 0xb0,
  36430. 0x1f, 0xef, 0xb8, 0x6c, 0x13, 0x5a, 0x99, 0x25, 0xf0, 0x49,
  36431. 0xa9, 0x5a, 0x45, 0xf7, 0xfd, 0x1a, 0xc2, 0x71, 0x06, 0xe3,
  36432. 0x2d, 0x25, 0x64, 0xb0, 0x52, 0x12, 0x03, 0x62, 0xc7, 0xb6,
  36433. 0xf9, 0xdc, 0x1f, 0x78, 0xff, 0x8b, 0xfa, 0xde, 0x7f, 0x71,
  36434. 0xa6, 0x35, 0x3e, 0xac, 0x20, 0x54, 0x94, 0xa7, 0x2e, 0x9d,
  36435. 0x47, 0x17, 0x4b, 0xad, 0x92, 0xb3, 0x14, 0x26, 0x8c, 0x5a,
  36436. 0xd0, 0x16, 0x4b, 0x22, 0xe9, 0x0c, 0x79, 0x6b, 0x8e, 0xac,
  36437. 0x0d, 0x12, 0xf5, 0x66, 0x8e, 0x82, 0x1a, 0x44, 0xf3, 0xe9,
  36438. 0x56, 0x5a, 0xcd, 0x1c, 0x1b, 0x81, 0x7b, 0x63, 0x59, 0xfe,
  36439. 0xc8, 0xc0, 0xe3, 0xda, 0x16, 0x6b, 0x6f, 0x0d, 0xba, 0x0e,
  36440. 0x47, 0x12, 0x86, 0x9e, 0xf0, 0x3b, 0x4d, 0x87, 0x3b, 0xf2,
  36441. 0x75, 0x73, 0x2d, 0xdf, 0xca, 0x76, 0x0b, 0xbd, 0xe7, 0xb7,
  36442. 0x74, 0x24, 0xf3, 0xc6, 0xe6, 0x75, 0x3f, 0x8b, 0x6a, 0xd9,
  36443. 0xad, 0xed, 0xc0, 0x70, 0x04, 0x1e, 0x0b, 0x8e, 0x8b, 0x7f,
  36444. 0xea, 0xbc, 0x39, 0x6b, 0x8a, 0x44, 0xa6, 0x9a, 0x2d, 0x0d,
  36445. 0x8c, 0x21, 0x60, 0x09, 0xd2, 0x4a, 0xe0, 0x62, 0xcf, 0xfa,
  36446. 0xe8, 0x9b, 0x35, 0x6f, 0x23, 0x2f, 0xb5, 0x65, 0x08, 0x60,
  36447. 0x92, 0x15, 0xd0, 0x5b, 0x63, 0xcc, 0x65, 0x05, 0xd1, 0xef,
  36448. 0x0f, 0x7e, 0x1b, 0xb3, 0x8e, 0xc6, 0x12, 0x85, 0xc9, 0x82,
  36449. 0x53, 0x79, 0x2e, 0x80, 0x5f, 0x0c, 0x7b, 0xc7, 0x1c, 0x83,
  36450. 0x41, 0x06, 0xd8, 0x41, 0xc9, 0xe7, 0xb9, 0x4b, 0xa1, 0x61,
  36451. 0xc6, 0x86, 0x67, 0xf5, 0x10, 0xf7, 0x34, 0x0d, 0x39, 0x9e,
  36452. 0x2b, 0x5f, 0x19, 0x06, 0x02, 0xa5, 0x02, 0x23, 0x71, 0xc2,
  36453. 0x12, 0x65, 0xcc, 0x81, 0x06, 0xfd, 0x8d, 0x09, 0x68, 0x37,
  36454. 0x06, 0x3b, 0xff, 0xc4, 0x24, 0xb3, 0x1f, 0xd6, 0xe6, 0x8f,
  36455. 0x9c, 0x74, 0x2c, 0x5e, 0xc5, 0xf4, 0xe9, 0xeb, 0xca, 0xd3,
  36456. 0x04, 0x5b, 0x92, 0x9e, 0x5c, 0x1a, 0x1d, 0xa1, 0xa7, 0x34,
  36457. 0xd2, 0x05, 0xae, 0xdb, 0x3d, 0x71, 0x10, 0x6e, 0x30, 0xd9,
  36458. 0xa3, 0x44, 0xa0, 0xbd, 0x9e, 0x7b, 0xb5, 0x12, 0x8a, 0x12,
  36459. 0x07, 0x60, 0xd7, 0x1f, 0x92, 0xe6, 0xfe, 0x04, 0xa9, 0x3e,
  36460. 0x62, 0x64, 0x00, 0x5f, 0x7c, 0x7b, 0x34, 0x09, 0xeb, 0x4a,
  36461. 0x18, 0x9e, 0x77, 0x72, 0x3a, 0x31, 0x1a, 0x62, 0x2a, 0xb5,
  36462. 0xcb, 0x4e, 0x53, 0xce, 0xad, 0x8b, 0x5a, 0x20, 0x4f, 0xd7,
  36463. 0x3e, 0x16, 0xf8, 0x10, 0xe2, 0xae, 0xbd, 0x3f, 0x02, 0xa9,
  36464. 0x18, 0xa0, 0x01, 0x18, 0x84, 0x95, 0x22, 0x2e, 0x93, 0x76,
  36465. 0x44, 0x4e, 0x11, 0x7b, 0x03, 0x51, 0x50, 0x19, 0x79, 0xe7,
  36466. 0xbb, 0x5c, 0x7b, 0xca, 0x74, 0xb4, 0x25, 0x26, 0xdb, 0x66,
  36467. 0xaa, 0x0b, 0x21, 0x07, 0xfb, 0x7a, 0x96, 0x10, 0x7d, 0x99,
  36468. 0xa9, 0x16, 0xcb, 0x0e, 0xba, 0x63, 0xab, 0x95, 0xfc, 0x5a,
  36469. 0xbe, 0xa6, 0x7f, 0xd8, 0xb4, 0xcd, 0x7c, 0xc5, 0xd0, 0xb1,
  36470. 0x1b, 0x48, 0x40, 0xfb, 0xe6, 0x2f, 0x2b, 0x94, 0xfe, 0x68,
  36471. 0xa2, 0xc4, 0x36, 0xd9, 0xcd, 0xc1, 0x93, 0x6d, 0xef, 0x39,
  36472. 0x5e, 0x43, 0x30, 0x5a, 0x2e, 0x66, 0xb6, 0xf2, 0xed, 0x9a,
  36473. 0x8d, 0x12, 0xdf, 0x5c, 0xae, 0xad, 0x16, 0x12, 0x7e, 0x81,
  36474. 0x82, 0x91, 0x7d, 0x2b, 0x12, 0xe9, 0x96, 0xb8, 0xb7, 0x42,
  36475. 0xcb, 0x1f, 0xf8, 0xd1, 0xfd, 0x83, 0x7a, 0xe4, 0x36, 0x1d,
  36476. 0x04, 0x27, 0x4c, 0xe5, 0xbd, 0x75, 0x24, 0xf7, 0xbd, 0xb6,
  36477. 0x6a, 0x68, 0x4e, 0x2c, 0x1b, 0x56, 0x3e, 0x60, 0xa4, 0x42,
  36478. 0xca, 0x7a, 0x54, 0xe5, 0x06, 0xe3, 0xda, 0x05, 0xf7, 0x77,
  36479. 0x36, 0x8b, 0x81, 0x26, 0x99, 0x92, 0x42, 0xda, 0x45, 0xb1,
  36480. 0xfe, 0x4b,
  36481. };
  36482. WOLFSSL_SMALL_STACK_STATIC const byte ml_dsa_44_sig[] = {
  36483. 0x5e, 0xc1, 0xce, 0x0e, 0x31, 0xea, 0x10, 0x52, 0xa3, 0x7a,
  36484. 0xfe, 0x4d, 0xac, 0x07, 0x89, 0x5a, 0x45, 0xbd, 0x5a, 0xe5,
  36485. 0x22, 0xed, 0x98, 0x4d, 0x2f, 0xc8, 0x27, 0x00, 0x99, 0x40,
  36486. 0x00, 0x79, 0xcd, 0x93, 0x27, 0xd0, 0x40, 0x33, 0x79, 0x4f,
  36487. 0xe5, 0x16, 0x89, 0x9f, 0xbd, 0xa6, 0x3f, 0xdd, 0x68, 0x74,
  36488. 0x73, 0xc3, 0x97, 0x54, 0x11, 0x1d, 0xc8, 0xb8, 0xc8, 0xfd,
  36489. 0x3a, 0xbe, 0xca, 0x17, 0x0f, 0x10, 0x6d, 0x89, 0x6d, 0xe0,
  36490. 0xb2, 0xff, 0x3b, 0xe5, 0xa1, 0x75, 0xea, 0x35, 0x16, 0xa3,
  36491. 0x0c, 0x6e, 0x4a, 0x7b, 0xdb, 0x28, 0xc6, 0x2a, 0x76, 0x0e,
  36492. 0x78, 0x78, 0xa0, 0x4f, 0x4e, 0xf8, 0x99, 0xff, 0xe7, 0x47,
  36493. 0x7e, 0xc4, 0x62, 0xa7, 0xb4, 0xb9, 0x2b, 0xc1, 0xc7, 0xd0,
  36494. 0x00, 0xb6, 0xaa, 0xa7, 0x37, 0xd5, 0x1e, 0x19, 0xc4, 0xc4,
  36495. 0x59, 0x2f, 0xa5, 0x09, 0xa3, 0xda, 0x5d, 0xd4, 0x48, 0x64,
  36496. 0x16, 0x0e, 0x92, 0xdf, 0x61, 0xb7, 0x25, 0x3b, 0x90, 0x5a,
  36497. 0x08, 0xb5, 0x88, 0xe8, 0x64, 0x80, 0x63, 0xee, 0xbf, 0x59,
  36498. 0x0f, 0x4a, 0x48, 0x1e, 0x77, 0xa9, 0x46, 0xc6, 0x9c, 0x0b,
  36499. 0x83, 0xad, 0xb5, 0xbf, 0xb5, 0x5b, 0x99, 0xf3, 0x55, 0xe8,
  36500. 0xe5, 0xe7, 0x5c, 0x12, 0xac, 0x06, 0x06, 0xe0, 0xc0, 0x32,
  36501. 0x5d, 0xb6, 0x9f, 0x2b, 0x8e, 0x19, 0x5c, 0x2a, 0x58, 0xbb,
  36502. 0x37, 0xf1, 0x68, 0x56, 0x8b, 0x74, 0x94, 0x58, 0x48, 0x28,
  36503. 0xee, 0xf7, 0x0a, 0x8f, 0xad, 0x43, 0x67, 0xe1, 0xa3, 0x8c,
  36504. 0x3b, 0x35, 0x48, 0xcc, 0x52, 0x14, 0x36, 0x99, 0x18, 0x71,
  36505. 0x1c, 0xb2, 0xfc, 0x82, 0xda, 0xac, 0xd5, 0x55, 0x0a, 0x77,
  36506. 0x44, 0x6a, 0x48, 0xed, 0xfc, 0x5a, 0x68, 0xa6, 0x4d, 0x65,
  36507. 0xe7, 0x30, 0xaa, 0x23, 0x66, 0x84, 0xdf, 0x83, 0xf1, 0x17,
  36508. 0x5c, 0x46, 0xfe, 0x63, 0xcb, 0xc3, 0x6e, 0x4e, 0x47, 0x8d,
  36509. 0x30, 0x48, 0x06, 0xda, 0x97, 0x6b, 0x04, 0x5d, 0x44, 0xf3,
  36510. 0xb7, 0x2a, 0x6d, 0x2b, 0xbb, 0xcd, 0x97, 0x4e, 0x26, 0x8e,
  36511. 0xc9, 0x03, 0x0b, 0x5d, 0x68, 0xed, 0x81, 0xf7, 0x19, 0x61,
  36512. 0x81, 0xe9, 0xac, 0x3a, 0x35, 0xcd, 0xe8, 0xfd, 0x99, 0xdb,
  36513. 0x89, 0x83, 0x7d, 0x23, 0x6a, 0xc1, 0xc1, 0x10, 0xe9, 0xd3,
  36514. 0xfa, 0x9e, 0x5a, 0xcd, 0x73, 0xa3, 0x0a, 0x37, 0xa3, 0x12,
  36515. 0xef, 0x72, 0xa2, 0x28, 0xd4, 0x3d, 0x67, 0x53, 0x24, 0x0d,
  36516. 0x61, 0x98, 0xbb, 0x07, 0xf3, 0xa7, 0x79, 0x22, 0x74, 0x57,
  36517. 0x99, 0xe8, 0x7a, 0xbf, 0x90, 0x84, 0xa2, 0x6b, 0x29, 0x34,
  36518. 0xac, 0xc9, 0xff, 0x67, 0x82, 0xd0, 0xd2, 0x7d, 0x69, 0xc0,
  36519. 0xf3, 0xd7, 0x4b, 0x5c, 0xf2, 0xa8, 0x53, 0x8b, 0x78, 0x57,
  36520. 0xfc, 0x74, 0xf5, 0x81, 0x6e, 0xc2, 0x5b, 0x32, 0x52, 0x9e,
  36521. 0x58, 0x84, 0xa1, 0x71, 0xd5, 0x8c, 0xf5, 0x16, 0x36, 0x4d,
  36522. 0x11, 0xd4, 0xb5, 0xc2, 0x05, 0xc4, 0x03, 0xce, 0x83, 0xea,
  36523. 0x0b, 0x6a, 0x2e, 0xf6, 0x28, 0x5e, 0xb2, 0x40, 0x8c, 0xa3,
  36524. 0x6a, 0xc7, 0xee, 0x04, 0x54, 0x93, 0x0f, 0x3b, 0xf9, 0x57,
  36525. 0x92, 0x00, 0xf1, 0xc7, 0x1b, 0x48, 0x63, 0xcb, 0xd3, 0xdd,
  36526. 0x40, 0x90, 0x46, 0xb0, 0x87, 0x2a, 0xb8, 0xec, 0xbc, 0x07,
  36527. 0x09, 0x83, 0x25, 0xb1, 0x88, 0x2c, 0xa0, 0x0a, 0x40, 0x4f,
  36528. 0xfd, 0xec, 0xfd, 0xbe, 0x18, 0xae, 0xdd, 0x83, 0x89, 0x83,
  36529. 0x2d, 0x10, 0xb4, 0x14, 0x30, 0xac, 0x6c, 0xd9, 0xc9, 0xaa,
  36530. 0xbc, 0xdb, 0x5e, 0x14, 0xab, 0x19, 0x64, 0xaa, 0xb1, 0x9c,
  36531. 0xc3, 0xf5, 0xdc, 0x2b, 0xcd, 0x26, 0x0b, 0x81, 0x1a, 0x0e,
  36532. 0x0a, 0xd6, 0x39, 0x79, 0x10, 0x06, 0xbf, 0xe0, 0xc1, 0x8b,
  36533. 0x20, 0x24, 0x90, 0x8b, 0x0f, 0xa4, 0x2d, 0x2d, 0x46, 0x2a,
  36534. 0xd4, 0xf3, 0xa9, 0x58, 0x4b, 0xd9, 0xa6, 0x6c, 0x75, 0x3d,
  36535. 0xbc, 0x36, 0x76, 0x7f, 0xef, 0x1b, 0xa1, 0x41, 0xba, 0xd0,
  36536. 0xfe, 0x16, 0x19, 0xc3, 0x92, 0xe3, 0x59, 0x07, 0x3f, 0x48,
  36537. 0x11, 0x70, 0xe0, 0x8a, 0xff, 0x97, 0xbc, 0x71, 0xd5, 0xb9,
  36538. 0x4a, 0x9b, 0x4c, 0xb8, 0x4b, 0x50, 0xd6, 0x43, 0xe8, 0x84,
  36539. 0x0a, 0x95, 0xd0, 0x20, 0x28, 0xd3, 0x20, 0x4a, 0x0e, 0x1b,
  36540. 0xe6, 0x5d, 0x2f, 0x0c, 0xdb, 0x76, 0xab, 0xa3, 0xc2, 0xad,
  36541. 0xd5, 0x86, 0xae, 0xb9, 0x26, 0xb2, 0x5d, 0x72, 0x27, 0xbb,
  36542. 0xec, 0x23, 0x9f, 0x42, 0x90, 0x58, 0xe1, 0xf8, 0xe9, 0x63,
  36543. 0xdf, 0x1a, 0x46, 0x53, 0x65, 0x05, 0xfb, 0x20, 0x21, 0xa6,
  36544. 0x64, 0xc8, 0x5c, 0x67, 0x6b, 0x41, 0x6c, 0x04, 0x34, 0xeb,
  36545. 0x05, 0x71, 0xeb, 0xbe, 0xed, 0x6d, 0xa2, 0x96, 0x67, 0x45,
  36546. 0xe7, 0x47, 0x22, 0x64, 0xaf, 0x82, 0xf8, 0x78, 0x0e, 0xe6,
  36547. 0xa1, 0x4a, 0x2d, 0x82, 0x1e, 0xd0, 0xc2, 0x79, 0x4e, 0x29,
  36548. 0x89, 0xd9, 0xf3, 0x3f, 0xb6, 0xc4, 0xee, 0x69, 0xb2, 0x8f,
  36549. 0x8b, 0xd9, 0x13, 0xd9, 0x6e, 0x3a, 0xc5, 0x9f, 0xdf, 0x25,
  36550. 0xb7, 0xc3, 0x16, 0xb8, 0xa2, 0x85, 0x17, 0xae, 0xe9, 0x95,
  36551. 0x5d, 0xb8, 0x1d, 0x21, 0xbb, 0xd9, 0x38, 0x11, 0x8f, 0x44,
  36552. 0xea, 0xe8, 0x4c, 0x91, 0x82, 0xf5, 0x45, 0xee, 0x8f, 0xf5,
  36553. 0x6a, 0x0d, 0x08, 0xe7, 0x6b, 0xb0, 0x91, 0xd5, 0x42, 0x17,
  36554. 0x8c, 0x37, 0x6a, 0x5a, 0x0a, 0x87, 0x53, 0x76, 0xc3, 0x59,
  36555. 0x35, 0x13, 0x1c, 0xf1, 0x72, 0x2c, 0x2b, 0xb2, 0x9e, 0xda,
  36556. 0x10, 0x2a, 0xce, 0x38, 0xb4, 0x67, 0x8c, 0x4b, 0x08, 0xa1,
  36557. 0xb6, 0xa3, 0x08, 0x9c, 0xeb, 0xd8, 0x93, 0x1b, 0x29, 0x5a,
  36558. 0xa7, 0x03, 0x17, 0x7e, 0xec, 0x58, 0x6b, 0x5b, 0xc5, 0x46,
  36559. 0x03, 0x33, 0x7f, 0x0e, 0x93, 0x9a, 0xdd, 0xb5, 0x89, 0xb1,
  36560. 0x16, 0x4c, 0xa7, 0xd8, 0x0e, 0x73, 0xd8, 0xc3, 0xd2, 0x36,
  36561. 0x85, 0x66, 0xcb, 0x5b, 0x64, 0xf2, 0xdc, 0xba, 0x39, 0xcc,
  36562. 0xa5, 0xe0, 0x9b, 0xaa, 0x2a, 0x95, 0x6d, 0xdc, 0x49, 0xde,
  36563. 0x3b, 0x61, 0xa2, 0x3b, 0x1f, 0xed, 0x32, 0xfa, 0x10, 0xe4,
  36564. 0x88, 0x59, 0xca, 0x5a, 0xe4, 0xf9, 0x5e, 0xe2, 0xca, 0x21,
  36565. 0x5a, 0xdc, 0x02, 0x73, 0x7a, 0xc8, 0x90, 0x7a, 0x8e, 0x91,
  36566. 0x19, 0x04, 0x53, 0x3c, 0x50, 0x15, 0x8a, 0x84, 0x93, 0x8f,
  36567. 0xac, 0x99, 0x82, 0xdd, 0xc6, 0xce, 0xfb, 0x18, 0x84, 0x29,
  36568. 0x2a, 0x8d, 0xa2, 0xc5, 0x7f, 0x87, 0xce, 0x4c, 0xf5, 0xdf,
  36569. 0x73, 0xd2, 0xba, 0xc2, 0x4f, 0xe3, 0x74, 0xa5, 0x8f, 0xc3,
  36570. 0xf4, 0x99, 0xd1, 0xe8, 0x4e, 0xb8, 0xe0, 0x2e, 0xef, 0xd6,
  36571. 0x87, 0x70, 0xcf, 0x45, 0x3b, 0xff, 0x03, 0xfd, 0x59, 0x7f,
  36572. 0x7c, 0xd0, 0x4e, 0x49, 0xf7, 0xd5, 0x08, 0xd9, 0x06, 0x53,
  36573. 0x90, 0x0a, 0x5a, 0x1b, 0x2e, 0xf5, 0xb0, 0x85, 0xb6, 0xb6,
  36574. 0x61, 0xa5, 0x71, 0x47, 0xbf, 0x4a, 0xf6, 0xae, 0x9a, 0x19,
  36575. 0x6c, 0xd8, 0x2d, 0x9b, 0xb4, 0x40, 0x9e, 0x15, 0x77, 0x2e,
  36576. 0x7e, 0xe9, 0xb4, 0x3d, 0x0f, 0x1b, 0xb5, 0x1c, 0xc2, 0x58,
  36577. 0x4e, 0x4b, 0xf6, 0x53, 0x9e, 0x6f, 0x09, 0x55, 0xa0, 0xb8,
  36578. 0x73, 0x11, 0x64, 0x70, 0x54, 0xb4, 0xcb, 0xb7, 0x27, 0xe5,
  36579. 0xdf, 0x58, 0x67, 0x5b, 0xc0, 0xd6, 0xf5, 0x64, 0xa6, 0x66,
  36580. 0x6d, 0xdf, 0xd8, 0xf8, 0xd6, 0x85, 0xba, 0xba, 0x30, 0xa7,
  36581. 0xca, 0x34, 0xf4, 0x9a, 0xba, 0x0a, 0xfb, 0x0e, 0xa0, 0x65,
  36582. 0x98, 0x78, 0xee, 0xaa, 0x14, 0x6a, 0x99, 0x77, 0x67, 0xad,
  36583. 0x01, 0x95, 0x5e, 0x50, 0x22, 0xe9, 0x74, 0x95, 0xa7, 0x13,
  36584. 0x3f, 0xdd, 0xa6, 0x69, 0x64, 0xf6, 0x50, 0x06, 0x6d, 0xba,
  36585. 0x90, 0x5a, 0x8c, 0x81, 0xa0, 0xda, 0x55, 0xe9, 0x97, 0x0e,
  36586. 0xd7, 0x10, 0x8e, 0x1f, 0x23, 0x65, 0xd9, 0x14, 0xd4, 0xde,
  36587. 0xa5, 0xf9, 0xec, 0xb6, 0xad, 0x65, 0xce, 0x0b, 0x1b, 0x0a,
  36588. 0x4c, 0x7d, 0xb0, 0x97, 0xa6, 0xfe, 0x67, 0xfb, 0x4f, 0x8f,
  36589. 0x00, 0x92, 0xb6, 0x0d, 0x20, 0x78, 0x65, 0x1d, 0x9a, 0x56,
  36590. 0x57, 0xc6, 0x15, 0x88, 0xba, 0x55, 0x02, 0x7a, 0x9a, 0xac,
  36591. 0x50, 0x4c, 0xc7, 0x9e, 0x66, 0x8b, 0xfc, 0xf3, 0x67, 0x48,
  36592. 0x07, 0xbf, 0x84, 0x94, 0x9b, 0x22, 0x2a, 0xae, 0x1b, 0x25,
  36593. 0xe9, 0x94, 0x06, 0xa7, 0xe8, 0x61, 0x52, 0x89, 0xdc, 0x93,
  36594. 0x6e, 0x89, 0xdc, 0x30, 0x6e, 0xd9, 0xee, 0xcb, 0x12, 0x38,
  36595. 0x58, 0x9d, 0x8b, 0xc5, 0x05, 0x2c, 0x50, 0x4e, 0xc8, 0xc2,
  36596. 0xe0, 0x65, 0xb6, 0x49, 0xc4, 0xf0, 0x1e, 0x5c, 0x8e, 0x3c,
  36597. 0xe9, 0x77, 0xd2, 0x9e, 0xa8, 0xd5, 0xf5, 0xd9, 0xc5, 0xad,
  36598. 0x5b, 0x74, 0x48, 0x08, 0x3a, 0x30, 0x84, 0x57, 0x71, 0x1e,
  36599. 0x69, 0x45, 0x09, 0xdd, 0xea, 0x62, 0xec, 0x7c, 0xa3, 0xf9,
  36600. 0x92, 0xee, 0x16, 0xdc, 0xe5, 0x9d, 0xcf, 0xb7, 0x08, 0x51,
  36601. 0x8a, 0x76, 0x3a, 0x23, 0x94, 0x50, 0x8e, 0x4d, 0x3a, 0xea,
  36602. 0xf3, 0xc1, 0x53, 0x2c, 0x65, 0x9c, 0x36, 0x8c, 0x10, 0xe3,
  36603. 0x9c, 0x01, 0xa4, 0xe6, 0x45, 0x77, 0xa6, 0x5d, 0x7e, 0x37,
  36604. 0x31, 0x95, 0x2f, 0xec, 0x61, 0x92, 0x69, 0x65, 0x53, 0x54,
  36605. 0x6d, 0xbe, 0x9e, 0x5a, 0x68, 0x12, 0xc4, 0xe7, 0xe4, 0x06,
  36606. 0x51, 0x5a, 0xc0, 0x63, 0xb9, 0x69, 0xb8, 0x3c, 0xd8, 0xae,
  36607. 0x8b, 0xff, 0x96, 0x4d, 0x55, 0xce, 0x25, 0x2b, 0x8b, 0x89,
  36608. 0xc9, 0x3a, 0x16, 0x48, 0x2a, 0x73, 0xb2, 0x70, 0x8b, 0x62,
  36609. 0xd5, 0xb1, 0xa0, 0x30, 0xe5, 0x46, 0xab, 0x8b, 0xc3, 0xeb,
  36610. 0x37, 0x2f, 0xbd, 0xb8, 0x4e, 0x6c, 0x30, 0xdc, 0x6c, 0x8a,
  36611. 0xf1, 0x89, 0x06, 0xce, 0x64, 0x0a, 0x3e, 0xb2, 0x16, 0x31,
  36612. 0xa1, 0xe4, 0x4b, 0x98, 0xe7, 0xf1, 0x99, 0x76, 0x00, 0x5f,
  36613. 0xd2, 0xd3, 0x30, 0xf0, 0xbf, 0xa7, 0x4a, 0xf6, 0x9e, 0xa5,
  36614. 0x75, 0x74, 0x78, 0xfe, 0xec, 0x72, 0x7c, 0x89, 0xe9, 0xf6,
  36615. 0x0d, 0x7e, 0x15, 0xd6, 0xd8, 0x79, 0x85, 0x3c, 0xcf, 0xb0,
  36616. 0x21, 0xc8, 0x9c, 0x54, 0x87, 0x63, 0xb3, 0x05, 0xbb, 0x8a,
  36617. 0x02, 0xe4, 0x79, 0xdc, 0xa1, 0xa2, 0xd3, 0x19, 0xd8, 0x86,
  36618. 0xff, 0x8a, 0x0e, 0x82, 0x89, 0xaf, 0xaa, 0x62, 0x2e, 0xd4,
  36619. 0xb2, 0xd0, 0x5d, 0x0d, 0x4f, 0x2a, 0xda, 0x0e, 0x9f, 0x8a,
  36620. 0x2b, 0x32, 0xe9, 0x09, 0xf5, 0x55, 0x51, 0xe7, 0xd5, 0x69,
  36621. 0x12, 0xdd, 0x33, 0x6b, 0x3d, 0xd7, 0xe9, 0xfd, 0xb2, 0xa7,
  36622. 0xf5, 0x97, 0x2a, 0x6d, 0x89, 0x30, 0x65, 0x2a, 0x0d, 0xf2,
  36623. 0x00, 0x81, 0xbe, 0xfb, 0xd9, 0xd7, 0x1b, 0xc2, 0x48, 0x7a,
  36624. 0x22, 0x30, 0xae, 0x35, 0xf6, 0x32, 0x41, 0x9d, 0xd9, 0x12,
  36625. 0xb3, 0xa7, 0x6d, 0xba, 0x74, 0x93, 0x2d, 0x0d, 0xb2, 0xb6,
  36626. 0xdc, 0xa9, 0x98, 0x5b, 0x3b, 0xaa, 0x2b, 0x47, 0x06, 0xc4,
  36627. 0x36, 0xfd, 0x04, 0x10, 0x94, 0x61, 0x61, 0x47, 0x1c, 0x02,
  36628. 0x54, 0x85, 0x4a, 0xcb, 0x75, 0x6b, 0x75, 0xf5, 0xb4, 0x61,
  36629. 0x26, 0xb3, 0x12, 0x43, 0x31, 0x55, 0xb5, 0xda, 0x4b, 0xb5,
  36630. 0x11, 0xb4, 0xb8, 0xfb, 0x0a, 0xd9, 0xa7, 0x0e, 0x9f, 0x2a,
  36631. 0x74, 0x01, 0xf6, 0x1a, 0x33, 0x10, 0x9e, 0x66, 0xff, 0x82,
  36632. 0xfa, 0xa9, 0xa4, 0xa0, 0x9b, 0x25, 0x2d, 0x16, 0xbf, 0x60,
  36633. 0x0d, 0x87, 0xea, 0x94, 0xad, 0xdd, 0xc4, 0xd0, 0xa8, 0xdd,
  36634. 0x2d, 0xc7, 0xc8, 0xac, 0x39, 0x9e, 0x87, 0x69, 0xc4, 0x3a,
  36635. 0xbc, 0x28, 0x7e, 0x36, 0x69, 0xfd, 0x20, 0x25, 0xac, 0xa3,
  36636. 0xa7, 0x37, 0x96, 0xe9, 0x8a, 0x65, 0xe4, 0xb0, 0x2a, 0x61,
  36637. 0x23, 0x28, 0x64, 0xff, 0x17, 0x6c, 0x36, 0x9e, 0x0a, 0xba,
  36638. 0xe4, 0x4b, 0xeb, 0x84, 0x24, 0x20, 0x57, 0x0f, 0x34, 0x05,
  36639. 0x95, 0x56, 0xc3, 0x2f, 0x2b, 0xf0, 0x36, 0xef, 0xca, 0x68,
  36640. 0xfe, 0x78, 0xf8, 0x98, 0x09, 0x4a, 0x25, 0xcc, 0x17, 0xbe,
  36641. 0x05, 0x00, 0xff, 0xf9, 0xa5, 0x5b, 0xe6, 0xaa, 0x5b, 0x56,
  36642. 0xb6, 0x89, 0x64, 0x9c, 0x16, 0x48, 0xe1, 0xcd, 0x67, 0x87,
  36643. 0xdd, 0xba, 0xbd, 0x02, 0x0d, 0xd8, 0xb4, 0xc9, 0x7c, 0x37,
  36644. 0x92, 0xd0, 0x39, 0x46, 0xd2, 0xc4, 0x78, 0x13, 0xf0, 0x76,
  36645. 0x45, 0x5f, 0xeb, 0x52, 0xd2, 0x3f, 0x61, 0x87, 0x34, 0x09,
  36646. 0xb7, 0x24, 0x4e, 0x93, 0xf3, 0xc5, 0x10, 0x19, 0x66, 0x66,
  36647. 0x3f, 0x15, 0xe3, 0x05, 0x55, 0x43, 0xb7, 0xf4, 0x62, 0x57,
  36648. 0xb4, 0xd9, 0xef, 0x46, 0x47, 0xb5, 0xfb, 0x79, 0xc9, 0x67,
  36649. 0xc5, 0xc3, 0x18, 0x91, 0x73, 0x75, 0xec, 0xd5, 0x68, 0x2b,
  36650. 0xf6, 0x42, 0xb4, 0xff, 0xfb, 0x27, 0x61, 0x77, 0x28, 0x10,
  36651. 0x6b, 0xce, 0x19, 0xad, 0x87, 0xc3, 0x85, 0xe3, 0x78, 0x00,
  36652. 0xdb, 0x21, 0xee, 0xd8, 0xfa, 0x9c, 0x81, 0x11, 0x97, 0xac,
  36653. 0xd0, 0x50, 0x89, 0x45, 0x23, 0xf6, 0x85, 0x7d, 0x60, 0xb2,
  36654. 0xad, 0x0c, 0x5d, 0xd8, 0x9e, 0xe4, 0xe1, 0x25, 0xb2, 0x13,
  36655. 0x1a, 0x54, 0x54, 0xfd, 0x7b, 0xab, 0x85, 0x20, 0xe8, 0xda,
  36656. 0x52, 0x0f, 0xac, 0x49, 0x70, 0xf1, 0x4c, 0x66, 0x74, 0x8c,
  36657. 0x87, 0x6e, 0xca, 0xc1, 0x0d, 0x92, 0xc0, 0xa8, 0x08, 0xfd,
  36658. 0x0f, 0x60, 0x55, 0xaf, 0x24, 0xcb, 0x04, 0xb7, 0xff, 0xa9,
  36659. 0xc5, 0x07, 0x26, 0xf6, 0xe2, 0x1e, 0x2f, 0xd1, 0x99, 0x6d,
  36660. 0xef, 0xc0, 0xdb, 0x5b, 0xf7, 0x06, 0x80, 0x92, 0x5f, 0x56,
  36661. 0x54, 0xdb, 0x2e, 0xba, 0x93, 0xb2, 0x94, 0xf2, 0xad, 0xbc,
  36662. 0x91, 0x6e, 0x4e, 0xce, 0x21, 0xc4, 0x8b, 0x18, 0xc4, 0xfc,
  36663. 0xab, 0xb4, 0x4f, 0xd7, 0xa2, 0xef, 0x55, 0x00, 0x6d, 0x34,
  36664. 0x17, 0x59, 0x8d, 0x79, 0x75, 0x02, 0xa3, 0x7a, 0x52, 0x57,
  36665. 0x5c, 0x26, 0xb9, 0xae, 0xd6, 0x19, 0x2e, 0x31, 0x02, 0x98,
  36666. 0x98, 0xe5, 0x3d, 0xc2, 0xa5, 0x56, 0xb6, 0x02, 0xae, 0x0d,
  36667. 0x3b, 0x35, 0x97, 0xd2, 0x43, 0x38, 0x8a, 0x65, 0xfa, 0x86,
  36668. 0x20, 0xb7, 0xb5, 0xb0, 0xda, 0x19, 0x01, 0x2f, 0x13, 0xb5,
  36669. 0x6d, 0xbd, 0xb2, 0x34, 0xa7, 0xff, 0xae, 0x7e, 0x8f, 0x98,
  36670. 0x1b, 0xc4, 0x27, 0xbd, 0xa9, 0x64, 0xdc, 0xab, 0x2a, 0xd2,
  36671. 0xb4, 0x27, 0xd0, 0x25, 0xdd, 0xff, 0xdc, 0x0a, 0x96, 0xd3,
  36672. 0x85, 0x3e, 0xc5, 0x11, 0x34, 0x60, 0xa2, 0x33, 0x92, 0x90,
  36673. 0xbb, 0x4c, 0x86, 0xdd, 0xd6, 0x1e, 0xcb, 0x0a, 0x17, 0xc6,
  36674. 0x87, 0x4e, 0x3e, 0x7a, 0x4b, 0xab, 0xef, 0x0a, 0x00, 0x3d,
  36675. 0x94, 0x34, 0x8b, 0x63, 0x36, 0xd9, 0xaf, 0x5d, 0x63, 0x40,
  36676. 0xbb, 0x32, 0x4b, 0x64, 0xf0, 0x31, 0x48, 0xdb, 0x44, 0x2b,
  36677. 0x48, 0x60, 0x6a, 0xea, 0xa4, 0x8c, 0xdd, 0xaf, 0x81, 0x3f,
  36678. 0x86, 0x81, 0x99, 0x7a, 0x98, 0xe1, 0xff, 0x21, 0x7a, 0x28,
  36679. 0xbc, 0x33, 0xe6, 0x4e, 0xb0, 0x85, 0x6b, 0xec, 0x11, 0x37,
  36680. 0x81, 0x7f, 0xf9, 0xdc, 0xbf, 0x1a, 0xa6, 0x6d, 0x4d, 0x0f,
  36681. 0x5b, 0x99, 0x73, 0xb8, 0xd2, 0x6e, 0x37, 0xf0, 0x71, 0xf1,
  36682. 0x1a, 0xc3, 0x5c, 0xea, 0x12, 0x5f, 0x2e, 0x85, 0x3f, 0xfd,
  36683. 0xd5, 0x87, 0x67, 0x9f, 0x67, 0x9f, 0xd7, 0xef, 0x9f, 0x81,
  36684. 0xa4, 0xbc, 0x63, 0x1d, 0x00, 0x81, 0xf6, 0x20, 0x77, 0xae,
  36685. 0x0b, 0x90, 0xe5, 0x9c, 0xa9, 0x44, 0xb5, 0xd7, 0xb1, 0x61,
  36686. 0x33, 0x4f, 0x75, 0xa9, 0xb7, 0xf4, 0xa4, 0x72, 0x9e, 0x72,
  36687. 0xec, 0x7b, 0xcd, 0x83, 0xb3, 0xd6, 0x22, 0x50, 0x50, 0x97,
  36688. 0x0f, 0x63, 0x0f, 0xe1, 0x15, 0xb3, 0x07, 0xb6, 0xa3, 0xfa,
  36689. 0x2f, 0xb5, 0xf3, 0x5b, 0x5d, 0x7f, 0x90, 0x20, 0xcd, 0x5f,
  36690. 0x40, 0x48, 0x87, 0x43, 0xfd, 0xa3, 0x69, 0xdc, 0xf8, 0x51,
  36691. 0x08, 0x67, 0xc2, 0x2d, 0xff, 0xfe, 0xbf, 0x85, 0x3e, 0x80,
  36692. 0xff, 0x91, 0x62, 0xc5, 0x83, 0xe0, 0x80, 0xeb, 0xce, 0xdc,
  36693. 0xff, 0xb1, 0xdb, 0x02, 0xb7, 0x01, 0x1e, 0xa6, 0xf0, 0x32,
  36694. 0xfb, 0x95, 0x6a, 0x47, 0x44, 0x84, 0x42, 0x6e, 0x3a, 0xb1,
  36695. 0xcf, 0xf9, 0x28, 0xb4, 0x3a, 0x8e, 0xa7, 0x8d, 0x48, 0x81,
  36696. 0x1c, 0x7e, 0xf5, 0x0b, 0x46, 0x7e, 0x92, 0x4e, 0xb9, 0xa8,
  36697. 0x36, 0xb8, 0x81, 0x6d, 0x8c, 0x70, 0x59, 0x33, 0x12, 0x61,
  36698. 0xbb, 0xe6, 0x10, 0x8a, 0xe4, 0xc1, 0x2c, 0x50, 0x12, 0xbf,
  36699. 0xd3, 0xc6, 0x3c, 0x53, 0x91, 0x50, 0x07, 0xc8, 0x85, 0x32,
  36700. 0x3c, 0xe1, 0x67, 0x99, 0x68, 0xc1, 0xf4, 0x74, 0x86, 0x35,
  36701. 0x8a, 0x6c, 0x75, 0x1d, 0x8f, 0x8a, 0x60, 0xe1, 0xc7, 0x59,
  36702. 0x4e, 0xb0, 0xe0, 0x45, 0x5a, 0x11, 0x05, 0x24, 0xa7, 0x8d,
  36703. 0x39, 0x93, 0x60, 0x4c, 0xc5, 0x9e, 0x8a, 0x70, 0xcc, 0x44,
  36704. 0x96, 0x92, 0xc8, 0xf7, 0x23, 0x14, 0xc7, 0xf4, 0x82, 0x9d,
  36705. 0x5b, 0x1c, 0x26, 0xd0, 0x3c, 0x76, 0x36, 0xe9, 0x98, 0x8a,
  36706. 0xbb, 0xe6, 0xa0, 0xad, 0xed, 0xf7, 0xd9, 0x06, 0x50, 0x67,
  36707. 0x79, 0x50, 0x4e, 0xd5, 0x80, 0x4e, 0x59, 0x72, 0x5d, 0x8b,
  36708. 0xcb, 0x86, 0x3b, 0x57, 0xc4, 0xb2, 0x3d, 0xbc, 0x35, 0x6d,
  36709. 0xb1, 0x50, 0xf5, 0x8c, 0xf2, 0x89, 0x72, 0x20, 0xd0, 0x47,
  36710. 0x68, 0x13, 0x42, 0x25, 0x1a, 0xb6, 0xc5, 0x07, 0xdf, 0x45,
  36711. 0x11, 0xa9, 0x05, 0x5d, 0xad, 0xf0, 0x49, 0x9e, 0x70, 0x78,
  36712. 0xed, 0xe7, 0xf9, 0x00, 0x1f, 0x62, 0x76, 0x47, 0xb5, 0x48,
  36713. 0x4f, 0x2c, 0x2e, 0xe3, 0x78, 0x6a, 0x44, 0x46, 0x1e, 0x6b,
  36714. 0x00, 0x74, 0x54, 0xb9, 0xd1, 0x4f, 0x6d, 0x45, 0xc1, 0xa6,
  36715. 0x45, 0x2e, 0x1a, 0xaf, 0x94, 0x3f, 0xd0, 0x72, 0x67, 0x0d,
  36716. 0x2e, 0xa9, 0x8d, 0x16, 0xc4, 0x05, 0x01, 0x07, 0x13, 0x1b,
  36717. 0x1c, 0x3d, 0x43, 0x71, 0x91, 0x95, 0x9a, 0xae, 0xaf, 0xc4,
  36718. 0xe5, 0xe6, 0xe9, 0xff, 0x02, 0x0c, 0x0f, 0x3e, 0x62, 0x67,
  36719. 0x68, 0x81, 0xc7, 0xd0, 0xd8, 0xdd, 0xe0, 0xf5, 0x0b, 0x25,
  36720. 0x35, 0x45, 0x4a, 0x4b, 0x63, 0x74, 0x79, 0x7e, 0x82, 0xa2,
  36721. 0xaf, 0xc6, 0xc7, 0xcc, 0xd2, 0xfa, 0x2a, 0x2d, 0x2f, 0x32,
  36722. 0x35, 0x38, 0x3f, 0x4c, 0x7f, 0x80, 0x81, 0x8b, 0x9b, 0x9c,
  36723. 0x9d, 0xa7, 0xa9, 0xcb, 0xe9, 0xf0, 0x00, 0x00, 0x00, 0x00,
  36724. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x20, 0x32, 0x46,
  36725. };
  36726. return dilithium_param_vfy_test(WC_ML_DSA_44, ml_dsa_44_pub_key,
  36727. (word32)sizeof(ml_dsa_44_pub_key), ml_dsa_44_sig,
  36728. (word32)sizeof(ml_dsa_44_sig));
  36729. }
  36730. #endif
  36731. #ifndef WOLFSSL_NO_ML_DSA_65
  36732. static wc_test_ret_t dilithium_param_65_vfy_test(void)
  36733. {
  36734. WOLFSSL_SMALL_STACK_STATIC const byte ml_dsa_65_pub_key[] = {
  36735. 0x15, 0xc9, 0xe5, 0x53, 0x2f, 0xd8, 0x1f, 0xb4, 0xa3, 0x9f,
  36736. 0xae, 0xad, 0xb3, 0x10, 0xd0, 0x72, 0x69, 0xd3, 0x02, 0xf3,
  36737. 0xdf, 0x67, 0x5a, 0x31, 0x52, 0x19, 0xca, 0x39, 0x27, 0x77,
  36738. 0x61, 0x6d, 0x0f, 0xc1, 0x33, 0x26, 0x09, 0xf0, 0xf9, 0x4d,
  36739. 0x12, 0x7a, 0xef, 0xf7, 0x21, 0x26, 0x2c, 0xe0, 0xe2, 0x92,
  36740. 0x1f, 0x9d, 0xd1, 0xaa, 0xaf, 0x08, 0x14, 0xf2, 0xaa, 0x24,
  36741. 0x99, 0x0f, 0x20, 0x57, 0x35, 0x04, 0x32, 0x96, 0x8e, 0x6e,
  36742. 0x10, 0x64, 0xe3, 0xe3, 0x57, 0x26, 0x33, 0x32, 0x7b, 0xe4,
  36743. 0x18, 0x41, 0x77, 0xd3, 0x24, 0x63, 0x3d, 0x11, 0xea, 0xdc,
  36744. 0xbe, 0x59, 0xff, 0x8d, 0xc2, 0xe4, 0xc7, 0x04, 0xf3, 0xd4,
  36745. 0xe0, 0x1d, 0x5e, 0x09, 0x46, 0xbf, 0x02, 0x05, 0xc7, 0xa6,
  36746. 0xb7, 0x82, 0x40, 0x1f, 0x55, 0xe9, 0x77, 0x82, 0xc0, 0xcc,
  36747. 0x86, 0x99, 0x19, 0x99, 0xa2, 0xc9, 0x1b, 0x4f, 0xdd, 0x49,
  36748. 0x4c, 0x78, 0x0a, 0x58, 0xb8, 0xf0, 0x23, 0xac, 0x1a, 0x71,
  36749. 0x57, 0x6d, 0xd6, 0x3a, 0x3a, 0x6f, 0x93, 0xb3, 0x2b, 0x09,
  36750. 0xbe, 0xec, 0x7b, 0x5b, 0xf7, 0x3a, 0xed, 0xf9, 0xd0, 0xb1,
  36751. 0xfe, 0x9f, 0x9b, 0xec, 0x11, 0xb6, 0x6b, 0xd1, 0xb6, 0x00,
  36752. 0x72, 0x7f, 0x68, 0x9a, 0x61, 0xa5, 0xf5, 0x6e, 0xe9, 0x46,
  36753. 0xa4, 0x82, 0x08, 0x9f, 0x50, 0x4c, 0x75, 0xc3, 0x48, 0x85,
  36754. 0x76, 0x39, 0xea, 0x0c, 0xf2, 0xe8, 0x7e, 0x48, 0x69, 0xd9,
  36755. 0x6f, 0x9a, 0x89, 0x7d, 0x98, 0xc1, 0x16, 0xdc, 0x2f, 0xc7,
  36756. 0x0a, 0x11, 0xa8, 0xbb, 0xe7, 0x91, 0xb1, 0x0f, 0x0e, 0xf0,
  36757. 0xb4, 0xc8, 0x41, 0x7e, 0x62, 0x9e, 0x3c, 0x30, 0x4c, 0xbc,
  36758. 0x4c, 0xeb, 0x37, 0xaf, 0x48, 0x72, 0x59, 0x64, 0x8e, 0xfb,
  36759. 0x77, 0x11, 0x28, 0xdd, 0x30, 0x52, 0x8e, 0x69, 0x8c, 0x9f,
  36760. 0x3d, 0xec, 0xdf, 0xa7, 0x5f, 0x42, 0x18, 0xda, 0xba, 0x1a,
  36761. 0x96, 0x91, 0x7d, 0x62, 0xd5, 0x52, 0xff, 0x44, 0xc9, 0x1d,
  36762. 0x29, 0xa6, 0xb9, 0x03, 0x9a, 0x26, 0x26, 0xcf, 0x57, 0x40,
  36763. 0x70, 0x7e, 0x2b, 0xbd, 0xf0, 0x81, 0x71, 0x0f, 0x0b, 0x2e,
  36764. 0x9b, 0x03, 0xba, 0x31, 0x41, 0x68, 0x37, 0xc8, 0xff, 0xea,
  36765. 0xc4, 0x73, 0xa5, 0xf9, 0xc2, 0x92, 0x78, 0x0c, 0xe7, 0xfd,
  36766. 0x5d, 0xb2, 0x01, 0xb5, 0x8d, 0xeb, 0x64, 0xd4, 0x14, 0xea,
  36767. 0x7a, 0xd1, 0x42, 0xc8, 0x99, 0xe4, 0x7d, 0x5b, 0x7e, 0x3b,
  36768. 0x8f, 0xab, 0x82, 0x12, 0xdf, 0xbb, 0xa1, 0x45, 0x30, 0xc9,
  36769. 0x0f, 0xb9, 0xe5, 0xba, 0xe6, 0x8a, 0xf3, 0x78, 0x61, 0xcc,
  36770. 0x9f, 0xe1, 0x46, 0x2a, 0x9a, 0x18, 0x0e, 0x2a, 0x57, 0xf3,
  36771. 0xe5, 0x56, 0xd1, 0x42, 0x48, 0xe1, 0x5a, 0x8e, 0x33, 0xce,
  36772. 0x19, 0xe5, 0x3e, 0x7f, 0x00, 0x70, 0x9c, 0x4c, 0xd3, 0xe1,
  36773. 0x0c, 0xa1, 0x7e, 0xd4, 0xa9, 0x9e, 0x8b, 0xe2, 0xf0, 0xac,
  36774. 0xdb, 0xa6, 0x72, 0x75, 0x67, 0xa6, 0x57, 0xed, 0x79, 0x2e,
  36775. 0xca, 0x8d, 0xeb, 0x9b, 0x9e, 0xb7, 0xbf, 0x30, 0x02, 0x2b,
  36776. 0xb3, 0x43, 0x89, 0x9b, 0xa8, 0x88, 0xa5, 0xbb, 0x33, 0xd9,
  36777. 0x99, 0x30, 0x7c, 0xc7, 0xd4, 0x28, 0x5e, 0x5e, 0x3f, 0x9d,
  36778. 0x6d, 0x35, 0x75, 0x33, 0x8e, 0xff, 0x84, 0x2e, 0x2d, 0xda,
  36779. 0xf0, 0xff, 0x70, 0xe5, 0xb5, 0x62, 0x96, 0x33, 0x3a, 0xd9,
  36780. 0xb5, 0x82, 0x25, 0x81, 0x81, 0x40, 0x5d, 0x4f, 0x11, 0x86,
  36781. 0x63, 0x1a, 0x06, 0xc1, 0x67, 0xc7, 0x49, 0x03, 0xc7, 0xe4,
  36782. 0x6f, 0xb4, 0x13, 0x3e, 0x57, 0x62, 0xfd, 0x8a, 0xc6, 0x2b,
  36783. 0x65, 0x5b, 0xa4, 0x29, 0x57, 0x8d, 0xde, 0xa5, 0xee, 0x32,
  36784. 0xc2, 0x76, 0x03, 0xca, 0xce, 0xc1, 0x48, 0xec, 0x45, 0xcf,
  36785. 0x30, 0x21, 0x28, 0x7f, 0x10, 0x47, 0xd2, 0xdb, 0xee, 0xca,
  36786. 0x5b, 0x0f, 0xd5, 0x39, 0x3a, 0xc3, 0xa6, 0x78, 0xb2, 0x15,
  36787. 0xaf, 0x82, 0x3c, 0x2f, 0xc4, 0x51, 0x5c, 0x52, 0xad, 0xf2,
  36788. 0x89, 0x92, 0x8e, 0xf3, 0x50, 0x38, 0xed, 0xf8, 0xc9, 0x14,
  36789. 0x4c, 0xe4, 0xa3, 0x9a, 0xaf, 0xc4, 0x5c, 0xf3, 0x9f, 0xc3,
  36790. 0xa3, 0xc0, 0xbe, 0x45, 0x1b, 0x21, 0x63, 0xfa, 0xe0, 0xe0,
  36791. 0x91, 0x2b, 0x42, 0xca, 0x91, 0xfb, 0x5e, 0x97, 0x9a, 0x0a,
  36792. 0xd4, 0x88, 0xba, 0xb8, 0x22, 0xc6, 0xbf, 0x56, 0x58, 0x1e,
  36793. 0x92, 0xa9, 0x9d, 0xa7, 0xed, 0xc9, 0xab, 0x54, 0x4f, 0x75,
  36794. 0x8d, 0x42, 0xc1, 0xe1, 0x61, 0xd0, 0x91, 0x9a, 0x3a, 0x40,
  36795. 0x9a, 0xa3, 0xfb, 0x7b, 0x4e, 0xf0, 0x85, 0xf0, 0xdc, 0x40,
  36796. 0x72, 0x9f, 0x05, 0xa8, 0xbe, 0x95, 0x5a, 0x7f, 0xba, 0x75,
  36797. 0x00, 0x6e, 0x95, 0x76, 0xbd, 0xb2, 0x40, 0xf5, 0xb0, 0x64,
  36798. 0x0a, 0x2f, 0x06, 0x3d, 0x9f, 0xac, 0x6a, 0xa5, 0x46, 0x5a,
  36799. 0x85, 0xa4, 0x6f, 0xee, 0x27, 0xa0, 0xeb, 0x5f, 0x1f, 0x91,
  36800. 0xbd, 0x2b, 0x02, 0x16, 0xdf, 0x74, 0x97, 0x2c, 0xd0, 0xa8,
  36801. 0x9f, 0x3a, 0x7b, 0xdf, 0x3e, 0x98, 0x4a, 0x91, 0xdc, 0x19,
  36802. 0x96, 0x88, 0x75, 0x21, 0x1a, 0x6a, 0xa8, 0x4b, 0x1f, 0x35,
  36803. 0xd1, 0x92, 0xf5, 0x76, 0xf4, 0x72, 0x55, 0x13, 0xdb, 0x5d,
  36804. 0x07, 0x8d, 0xd9, 0x72, 0xe4, 0x75, 0xde, 0x80, 0xbc, 0xe9,
  36805. 0x9c, 0xf0, 0x5c, 0x6a, 0x8a, 0x0e, 0x34, 0xf6, 0x3f, 0x5c,
  36806. 0xef, 0x0e, 0xcc, 0x52, 0x38, 0x2d, 0x7b, 0xc2, 0x1b, 0x69,
  36807. 0x9f, 0xe5, 0xed, 0x14, 0xb0, 0x91, 0x0b, 0xe9, 0x4d, 0x34,
  36808. 0xd5, 0xaa, 0xd4, 0xd2, 0x46, 0x39, 0x45, 0x7e, 0x85, 0x2f,
  36809. 0xdb, 0x89, 0xf4, 0xff, 0x05, 0x74, 0x51, 0xba, 0xdd, 0xee,
  36810. 0xf6, 0xc2, 0xc1, 0x0a, 0x8f, 0xd9, 0xeb, 0xc7, 0x61, 0x30,
  36811. 0x8f, 0x86, 0x8b, 0x1f, 0x82, 0xc1, 0x22, 0xfd, 0x83, 0xf4,
  36812. 0x5d, 0xc5, 0x94, 0xf5, 0xd7, 0x17, 0xc7, 0x7b, 0x71, 0xf5,
  36813. 0x5e, 0x15, 0x49, 0x70, 0xb2, 0x57, 0xa0, 0xc0, 0x57, 0x63,
  36814. 0x53, 0x35, 0xb6, 0x52, 0x20, 0x7b, 0x83, 0xd4, 0x57, 0x63,
  36815. 0x25, 0x8e, 0x83, 0xb3, 0x8e, 0x26, 0x1f, 0x09, 0xde, 0x14,
  36816. 0xd6, 0xa6, 0xfc, 0xe5, 0x93, 0x3c, 0x88, 0x8e, 0xf5, 0x10,
  36817. 0x57, 0xb9, 0xc9, 0x9b, 0xff, 0x72, 0x9d, 0x3d, 0x3f, 0x97,
  36818. 0xd9, 0x3c, 0x20, 0xe2, 0x57, 0xfd, 0x2a, 0x5c, 0x17, 0x12,
  36819. 0xe6, 0x08, 0xaf, 0xe4, 0x26, 0x96, 0xb9, 0x6d, 0xc3, 0xac,
  36820. 0x22, 0xf3, 0x8b, 0x89, 0xde, 0xc7, 0x8a, 0x93, 0x06, 0xf7,
  36821. 0x1d, 0x08, 0x21, 0x36, 0x16, 0x74, 0x2b, 0x97, 0x23, 0xe4,
  36822. 0x79, 0x31, 0x08, 0x23, 0x62, 0x30, 0x67, 0xe2, 0xed, 0x30,
  36823. 0x9b, 0x0c, 0xf9, 0x08, 0x7a, 0x29, 0x73, 0xc6, 0x77, 0x8a,
  36824. 0xbb, 0x2a, 0x1c, 0x66, 0xd0, 0xdd, 0x9e, 0xa3, 0xe9, 0x62,
  36825. 0xcc, 0xb7, 0x88, 0x25, 0x4a, 0x5f, 0xbc, 0xaa, 0xe3, 0xe4,
  36826. 0x4f, 0xec, 0xa6, 0x8e, 0xa6, 0xa4, 0x1b, 0x22, 0x2b, 0x2c,
  36827. 0x8f, 0x57, 0x7f, 0xb7, 0x33, 0xfe, 0x16, 0x43, 0x85, 0xc5,
  36828. 0xd2, 0x95, 0xe6, 0xb9, 0x21, 0x68, 0x88, 0x98, 0x33, 0x8c,
  36829. 0x1d, 0x15, 0x9c, 0x4d, 0x62, 0x1f, 0x6b, 0xe8, 0x7a, 0x2d,
  36830. 0x6b, 0x0e, 0xc3, 0xde, 0x1a, 0xa8, 0xed, 0x67, 0xb3, 0xb3,
  36831. 0x36, 0x5b, 0x4b, 0xcb, 0xe8, 0xa8, 0x5c, 0x0b, 0x2f, 0xca,
  36832. 0xd7, 0x71, 0xe8, 0x85, 0xe7, 0x4d, 0xe5, 0x7b, 0x45, 0xed,
  36833. 0xb2, 0x4c, 0x69, 0x04, 0x7e, 0x4f, 0xc0, 0xef, 0x1a, 0xca,
  36834. 0x0d, 0xa6, 0xc4, 0x79, 0x15, 0x78, 0x9c, 0xd2, 0x91, 0x3c,
  36835. 0x32, 0x55, 0x40, 0xe7, 0xcb, 0x7e, 0xde, 0x07, 0xa6, 0x97,
  36836. 0x00, 0x2d, 0x70, 0xf6, 0x3d, 0x15, 0xdf, 0x29, 0x8e, 0xa3,
  36837. 0x96, 0x6d, 0xf2, 0xbb, 0xa5, 0x1b, 0x7b, 0x58, 0x30, 0xf6,
  36838. 0x17, 0xbd, 0xda, 0x13, 0xf7, 0x33, 0xc2, 0x62, 0x32, 0xd4,
  36839. 0x1c, 0x2e, 0x31, 0x74, 0x92, 0xad, 0x99, 0x8c, 0x0e, 0x7c,
  36840. 0x50, 0x21, 0xcd, 0xff, 0x41, 0xeb, 0xd1, 0xca, 0x14, 0xb7,
  36841. 0xb2, 0x31, 0x2f, 0xbe, 0x16, 0xce, 0x4f, 0x26, 0x16, 0x04,
  36842. 0xc2, 0xaf, 0xbe, 0x0d, 0x24, 0xab, 0x9a, 0x21, 0x37, 0x06,
  36843. 0xac, 0x50, 0x23, 0xf1, 0xbe, 0x5c, 0xbb, 0x64, 0xf3, 0xd3,
  36844. 0x66, 0xa3, 0xb8, 0xbe, 0x8b, 0x49, 0x8d, 0xf6, 0xc7, 0xb9,
  36845. 0x8f, 0x4e, 0x31, 0x06, 0x51, 0xe5, 0xf3, 0x0e, 0x56, 0xc4,
  36846. 0x24, 0x30, 0xf5, 0xe9, 0x36, 0x71, 0xbc, 0xc9, 0x70, 0x2c,
  36847. 0x6c, 0x4c, 0x15, 0x43, 0x44, 0xa4, 0xfc, 0xf1, 0xd2, 0x71,
  36848. 0x6c, 0x4c, 0xce, 0x30, 0x6c, 0x05, 0x7d, 0x2e, 0xb7, 0xbc,
  36849. 0xe4, 0x65, 0x76, 0x24, 0x75, 0x36, 0xdf, 0x28, 0xfc, 0xcd,
  36850. 0x9a, 0xba, 0xc2, 0xcd, 0xb0, 0x30, 0xdb, 0xe7, 0x2e, 0x3c,
  36851. 0x92, 0x63, 0x1d, 0x30, 0x23, 0x74, 0xb1, 0xb8, 0xcc, 0xd7,
  36852. 0xb6, 0x90, 0x65, 0x73, 0xa2, 0x2a, 0x6e, 0x49, 0x95, 0x0d,
  36853. 0xab, 0x24, 0xdf, 0x2d, 0xbf, 0x76, 0x46, 0x01, 0x44, 0xe4,
  36854. 0x18, 0x8e, 0xd5, 0x9a, 0x76, 0xc9, 0xc6, 0xbc, 0xdb, 0x7f,
  36855. 0x80, 0x52, 0xc6, 0x40, 0x41, 0x12, 0x36, 0x7c, 0x80, 0x69,
  36856. 0xce, 0x7b, 0xe1, 0xa0, 0x53, 0xa2, 0xd6, 0x8f, 0x3f, 0xf7,
  36857. 0xd7, 0x61, 0x09, 0x70, 0xa2, 0xa0, 0xc6, 0xaf, 0xa0, 0xd0,
  36858. 0xfa, 0x13, 0xbf, 0xc0, 0x69, 0x15, 0xce, 0x15, 0xec, 0x24,
  36859. 0x4b, 0x6b, 0xdc, 0x93, 0x51, 0xc6, 0x82, 0x19, 0x92, 0x84,
  36860. 0x5d, 0x99, 0xb0, 0x90, 0x2c, 0xcc, 0x2a, 0x81, 0x6b, 0x22,
  36861. 0x64, 0x0a, 0xcb, 0x51, 0x25, 0x82, 0x50, 0x02, 0x2d, 0x3e,
  36862. 0xd4, 0x72, 0xb3, 0x0c, 0x15, 0x77, 0xd2, 0xca, 0x98, 0x2f,
  36863. 0x41, 0x93, 0x14, 0xb2, 0x7f, 0xa1, 0x97, 0xa3, 0xb8, 0x8a,
  36864. 0x56, 0x24, 0x38, 0xa7, 0x36, 0xc5, 0x01, 0xc0, 0x9f, 0x3f,
  36865. 0x3e, 0x9a, 0xf6, 0xe9, 0x16, 0x82, 0x01, 0x58, 0x70, 0x0e,
  36866. 0x0d, 0xbc, 0xfa, 0x03, 0x57, 0x65, 0xa8, 0x5a, 0x3d, 0x57,
  36867. 0x81, 0x23, 0xbe, 0x6e, 0xa9, 0xe8, 0x22, 0xdf, 0x2f, 0x70,
  36868. 0xeb, 0x0a, 0x03, 0x96, 0x6b, 0xef, 0x20, 0x9f, 0xf2, 0x62,
  36869. 0xe7, 0xb2, 0x6e, 0x3a, 0x1e, 0x40, 0x1f, 0xd2, 0x97, 0x48,
  36870. 0xd1, 0x18, 0xf0, 0xeb, 0x52, 0x58, 0x02, 0x26, 0xce, 0x75,
  36871. 0xb1, 0x3a, 0x9d, 0x5b, 0x52, 0x94, 0xb2, 0x6e, 0x0e, 0x3f,
  36872. 0x39, 0xb6, 0xd9, 0x8a, 0x9d, 0xe8, 0x7c, 0x83, 0x32, 0xcc,
  36873. 0x43, 0x35, 0x9b, 0x7a, 0xed, 0xb2, 0x1e, 0x51, 0x37, 0x6c,
  36874. 0x14, 0xd8, 0xb8, 0x55, 0xb3, 0x91, 0xef, 0x0c, 0x3a, 0xe5,
  36875. 0x77, 0xd0, 0xbd, 0xb0, 0x7d, 0x38, 0x84, 0x2a, 0x47, 0xb2,
  36876. 0xb6, 0xda, 0xd7, 0x75, 0xd6, 0x2e, 0x60, 0xc7, 0x10, 0x52,
  36877. 0xf7, 0xdd, 0x09, 0x15, 0x6f, 0x04, 0x31, 0xc3, 0x5a, 0x6b,
  36878. 0x0c, 0x60, 0x10, 0xa8, 0x6e, 0x20, 0xa9, 0xdd, 0xb7, 0x72,
  36879. 0xc3, 0x9e, 0x85, 0xd2, 0x8f, 0x16, 0x7e, 0x3d, 0xe0, 0x63,
  36880. 0x81, 0x32, 0xfd, 0xca, 0xbc, 0x0f, 0xef, 0x3e, 0x74, 0x6a,
  36881. 0xb1, 0x60, 0xc1, 0x10, 0x50, 0x7c, 0x67, 0xa4, 0x19, 0xa7,
  36882. 0xb8, 0xed, 0xe6, 0xf5, 0x4e, 0x41, 0x53, 0xa6, 0x72, 0x1b,
  36883. 0x2c, 0x33, 0x6a, 0x37, 0xf1, 0xb5, 0x1c, 0x01, 0x7d, 0xa2,
  36884. 0x1f, 0x2c, 0x4e, 0x0a, 0xbf, 0xd4, 0x2c, 0x24, 0x91, 0x58,
  36885. 0x62, 0xfb, 0xf8, 0x63, 0xd9, 0xf8, 0x78, 0xf5, 0xc7, 0x78,
  36886. 0x32, 0xda, 0x99, 0xeb, 0x58, 0x20, 0x25, 0x19, 0xb1, 0x06,
  36887. 0x7f, 0x6a, 0x29, 0x20, 0xdb, 0xc8, 0x22, 0x48, 0xa9, 0x7f,
  36888. 0x24, 0x54, 0x8d, 0x7d, 0x8d, 0xb1, 0x69, 0xb2, 0xa3, 0x98,
  36889. 0x14, 0x0f, 0xba, 0xfa, 0xb6, 0x15, 0xe8, 0x28, 0x99, 0x3f,
  36890. 0x30, 0x04, 0x50, 0xab, 0x5a, 0x3c, 0xf1, 0x97, 0xe1, 0xc8,
  36891. 0x0f, 0x0e, 0xb4, 0x11, 0x63, 0x5a, 0x79, 0x08, 0x48, 0x75,
  36892. 0xaf, 0x9b, 0xca, 0xd9, 0x13, 0x18, 0xcc, 0xb1, 0xb3, 0xee,
  36893. 0xdd, 0x63, 0xdd, 0xf4, 0x21, 0x98, 0x76, 0xe2, 0x3e, 0xd5,
  36894. 0x86, 0x23, 0x33, 0x7e, 0xc7, 0xb4, 0x35, 0x4b, 0xc2, 0x2d,
  36895. 0xe1, 0xe2, 0xb0, 0x6c, 0x8b, 0x9b, 0x20, 0x3d, 0x48, 0x24,
  36896. 0x7c, 0xea, 0xa1, 0x75, 0x27, 0xe5, 0xf4, 0x70, 0xeb, 0x3b,
  36897. 0xc7, 0x26, 0x37, 0x04, 0xff, 0x8a, 0x7a, 0xd0, 0xc2, 0xb7,
  36898. 0x84, 0xb7, 0x29, 0xfb, 0x0e, 0xa3, 0xa8, 0x71, 0xcd, 0x58,
  36899. 0x06, 0x36, 0xe2, 0xf2, 0x77, 0xcc, 0x0f, 0x78, 0x08, 0x2b,
  36900. 0xbb, 0xe3, 0x53, 0x05, 0x71, 0xdc, 0x6c, 0x37, 0x32, 0x91,
  36901. 0x46, 0x42, 0x4f, 0x21, 0xe0, 0x34, 0xad, 0x3f, 0x30, 0x5a,
  36902. 0xc7, 0x0d, 0x17, 0x19, 0x39, 0x31, 0x58, 0x69, 0x3c, 0x8c,
  36903. 0xbe, 0xe7, 0xa6, 0x3b, 0xad, 0xfb, 0x46, 0x89, 0x06, 0xc1,
  36904. 0x8c, 0x16, 0x9a, 0x06, 0x3a, 0xd0, 0x7e, 0xd6, 0xb0, 0x7b,
  36905. 0x7d, 0xf8, 0x91, 0x7c, 0xfa, 0xd9, 0x66, 0x39, 0xfa, 0xbc,
  36906. 0x57, 0xa7, 0x78, 0x8b, 0x36, 0x78, 0xc0, 0x1c, 0x0e, 0x23,
  36907. 0x05, 0x0e, 0x04, 0x61, 0x16, 0x34, 0xf9, 0xc6, 0x63, 0x58,
  36908. 0xdf, 0xf4, 0x52, 0xce, 0xd0, 0x0f, 0x0c, 0xec, 0xb1, 0x82,
  36909. 0xf4, 0x72, 0x73, 0x72, 0x3f, 0x02, 0xbe, 0xe3, 0x9c, 0x63,
  36910. 0x73, 0xc8, 0x21, 0x65, 0xba, 0x57, 0x52, 0xa9, 0x19, 0xac,
  36911. 0x68, 0x50, 0xbd, 0x2d, 0x72, 0x5b, 0x93, 0x0f, 0x1c, 0x81,
  36912. 0x77, 0xd7, 0x2e, 0xc3, 0x93, 0x52, 0x6e, 0xdc, 0x79, 0x52,
  36913. 0x9f, 0xe3, 0xde, 0xe1, 0xba, 0x58, 0x55, 0xab, 0x8a, 0xf2,
  36914. 0x35, 0x6a, 0xcf, 0x94, 0x1f, 0x17, 0xa4, 0x23, 0x2e, 0x8e,
  36915. 0x18, 0x21, 0xbe, 0x14, 0xfa, 0xe7, 0x59, 0xc5, 0x44, 0x34,
  36916. 0xce, 0x03, 0xf4, 0xb7, 0x75, 0xd3, 0x51, 0x55, 0xdf, 0xff,
  36917. 0xcf, 0x4f, 0x44, 0xee, 0x13, 0x9b, 0xcb, 0x12, 0xae, 0xe5,
  36918. 0x5b, 0x44, 0x65, 0x28, 0xcb, 0x6a, 0x9c, 0x24, 0x1d, 0xea,
  36919. 0x2d, 0x5e, 0xa5, 0xc3, 0x78, 0xad, 0xed, 0x0c, 0x05, 0xa6,
  36920. 0xaf, 0x95, 0x04, 0xd2, 0xb5, 0x91, 0x0e, 0xa0, 0x06, 0x77,
  36921. 0xc5, 0x82, 0xf6, 0xdd, 0x72, 0x83, 0x04, 0xcc, 0xb0, 0xab,
  36922. 0x7a, 0xf0, 0xb4, 0x4d, 0x36, 0x71, 0x72, 0x1a, 0x9a, 0x0d,
  36923. 0xcd, 0xa3, 0x11, 0xa8, 0x0d, 0x7d, 0x49, 0xce, 0x9c, 0x09,
  36924. 0x1d, 0x08, 0xa4, 0x39, 0x2e, 0x03, 0xdf, 0x3a, 0xc8, 0xfe,
  36925. 0x6a, 0x2b, 0x0b, 0x07, 0x80, 0x55, 0x8a, 0xa8, 0xe6, 0x0e,
  36926. 0xc9, 0x7e, 0x83, 0xce, 0x3a, 0x98, 0x98, 0x4e, 0x3e, 0x08,
  36927. 0x20, 0x8f, 0x10, 0xfc, 0xc1, 0xc4, 0xcf, 0x37, 0x8d, 0x69,
  36928. 0xd8, 0x57, 0x9d, 0x48, 0x80, 0x6a, 0xef, 0x0c, 0xdd, 0x27,
  36929. 0x99, 0xf9, 0xe7, 0xd0, 0xd2, 0x36, 0xd8, 0xed, 0x41, 0x14,
  36930. 0x1b, 0x10,
  36931. };
  36932. WOLFSSL_SMALL_STACK_STATIC const byte ml_dsa_65_sig[] = {
  36933. 0x3e, 0xff, 0xf4, 0x48, 0x80, 0x2d, 0x88, 0x87, 0xf4, 0xcc,
  36934. 0xa4, 0x61, 0xe1, 0x27, 0x20, 0x55, 0x66, 0xc8, 0xfe, 0x3e,
  36935. 0xdd, 0xf5, 0x5c, 0x70, 0x6c, 0x54, 0xba, 0x50, 0x8a, 0xa2,
  36936. 0x4b, 0x88, 0xbc, 0xb8, 0x87, 0xf9, 0x4e, 0x50, 0x3a, 0x04,
  36937. 0x18, 0xb3, 0xf4, 0x5f, 0x77, 0x4a, 0x7e, 0xa8, 0xf5, 0xca,
  36938. 0x49, 0x00, 0xdc, 0x24, 0xaa, 0x05, 0x35, 0x0f, 0x34, 0xf7,
  36939. 0xbf, 0x09, 0xa6, 0xcf, 0x75, 0x37, 0x07, 0xcd, 0x07, 0x99,
  36940. 0x92, 0x1d, 0xc7, 0xc9, 0x17, 0x1c, 0xdd, 0x27, 0x8c, 0x66,
  36941. 0xf2, 0x8b, 0x75, 0xb0, 0x86, 0x2d, 0xbd, 0x51, 0x16, 0xc2,
  36942. 0x50, 0xe0, 0x7e, 0x0a, 0x21, 0x58, 0x93, 0x22, 0x06, 0xcb,
  36943. 0x85, 0x8b, 0xfd, 0x97, 0x61, 0xc0, 0xdb, 0xab, 0xfa, 0x4a,
  36944. 0x69, 0xef, 0x9c, 0xc1, 0x4e, 0xae, 0xb2, 0xb3, 0xa2, 0x74,
  36945. 0xa4, 0x94, 0x0a, 0xed, 0x39, 0x9e, 0xe8, 0x58, 0xeb, 0xfd,
  36946. 0x43, 0x05, 0x73, 0x38, 0xd6, 0xbb, 0xeb, 0xb9, 0x9d, 0x3b,
  36947. 0xf8, 0x85, 0xb4, 0x4b, 0x16, 0x5c, 0x9e, 0xfe, 0xb8, 0x13,
  36948. 0xf8, 0x68, 0x44, 0x90, 0x05, 0x61, 0xb3, 0xed, 0x6f, 0x47,
  36949. 0xc9, 0x50, 0xcf, 0x6c, 0xc0, 0xac, 0xdf, 0x4c, 0x4c, 0x1b,
  36950. 0x42, 0xce, 0x0a, 0x32, 0x69, 0xb0, 0xfd, 0x87, 0xef, 0xf3,
  36951. 0x9c, 0xcc, 0xba, 0x2f, 0x03, 0xd7, 0xdb, 0x76, 0xee, 0xa0,
  36952. 0x71, 0x4a, 0x80, 0xcb, 0x90, 0x9e, 0xbb, 0x8f, 0x00, 0x46,
  36953. 0x81, 0xe0, 0xde, 0xa6, 0x43, 0xb5, 0x37, 0x79, 0xf2, 0x35,
  36954. 0xce, 0x9e, 0xd2, 0xb1, 0x5b, 0xff, 0x91, 0xfb, 0x98, 0xc1,
  36955. 0xe1, 0x66, 0x2c, 0x00, 0x1b, 0x89, 0xf2, 0x57, 0x81, 0x73,
  36956. 0x7e, 0x9f, 0x8d, 0x50, 0xd0, 0xe0, 0xe3, 0x93, 0xf2, 0x87,
  36957. 0x41, 0x64, 0x6c, 0xb7, 0x09, 0x60, 0x91, 0x4e, 0x0b, 0xbe,
  36958. 0xbe, 0xd4, 0x98, 0xfa, 0x14, 0x8c, 0x46, 0x09, 0xfa, 0xaa,
  36959. 0x82, 0xd6, 0xdd, 0x65, 0x93, 0x39, 0x45, 0x50, 0x90, 0x10,
  36960. 0xae, 0x1b, 0xff, 0xab, 0x7e, 0x86, 0xda, 0xb9, 0x4d, 0xf1,
  36961. 0xc2, 0x00, 0x54, 0x66, 0xee, 0x40, 0xc0, 0x56, 0x2f, 0xe8,
  36962. 0x43, 0x89, 0xbb, 0xb8, 0x59, 0x24, 0x63, 0x45, 0x9a, 0xde,
  36963. 0x08, 0xf3, 0x16, 0x94, 0xd2, 0x8d, 0xee, 0xf9, 0xbe, 0x4f,
  36964. 0x29, 0xe1, 0x4b, 0x5e, 0x2b, 0x14, 0xef, 0x66, 0xe2, 0x12,
  36965. 0xf8, 0x87, 0x2e, 0xb1, 0x75, 0x8b, 0x21, 0xb5, 0x8f, 0x8e,
  36966. 0xc5, 0x0e, 0x60, 0x27, 0x15, 0xbd, 0x72, 0xe4, 0x26, 0x4e,
  36967. 0x62, 0x7d, 0x3a, 0x46, 0x49, 0x93, 0xa9, 0x52, 0x7f, 0xc2,
  36968. 0x27, 0xb9, 0x55, 0x6a, 0x45, 0x9f, 0x2c, 0x7a, 0x5a, 0xc9,
  36969. 0xf4, 0x55, 0xaf, 0x49, 0xb3, 0xd5, 0xc0, 0x84, 0xdb, 0x89,
  36970. 0x5f, 0x21, 0x04, 0xf5, 0x4c, 0x66, 0x1e, 0x2e, 0x69, 0xdf,
  36971. 0x5b, 0x14, 0x60, 0x89, 0x84, 0xf8, 0xa3, 0xaf, 0xdf, 0xb9,
  36972. 0x18, 0x5e, 0xbf, 0x81, 0x95, 0x9a, 0x5e, 0x4f, 0x24, 0x45,
  36973. 0xad, 0xab, 0xe2, 0x36, 0x7c, 0x19, 0xde, 0xc0, 0xf4, 0x1a,
  36974. 0x42, 0xb2, 0xc2, 0x58, 0x2f, 0x5f, 0xd0, 0x2e, 0x28, 0x33,
  36975. 0x59, 0x75, 0xc2, 0xde, 0x41, 0xe3, 0x9b, 0x85, 0x46, 0xad,
  36976. 0x6d, 0xf1, 0x06, 0xf0, 0x6a, 0xb9, 0xed, 0x71, 0x7b, 0xfd,
  36977. 0xf1, 0xc4, 0x56, 0xd8, 0xb3, 0x1a, 0x5f, 0x04, 0xae, 0xe8,
  36978. 0xce, 0xde, 0xa1, 0x6d, 0x46, 0x2a, 0x4f, 0x62, 0xee, 0x25,
  36979. 0xdf, 0x22, 0x21, 0xb2, 0x8f, 0x5f, 0x26, 0x33, 0x5a, 0xdd,
  36980. 0xbe, 0x08, 0xb3, 0x93, 0x16, 0x16, 0xad, 0x2e, 0x00, 0xb8,
  36981. 0x14, 0x0c, 0x10, 0xa3, 0x29, 0x89, 0x1f, 0xd7, 0x06, 0x7a,
  36982. 0x09, 0xf3, 0x84, 0xf9, 0x18, 0x04, 0x56, 0x2f, 0x7f, 0xbd,
  36983. 0x8e, 0x12, 0xdf, 0x4d, 0x58, 0x5c, 0x1d, 0x81, 0x0c, 0x7d,
  36984. 0x62, 0x02, 0xe0, 0xf9, 0x1b, 0x69, 0xe9, 0x38, 0x45, 0x84,
  36985. 0x2d, 0x9a, 0x4a, 0x3d, 0x7b, 0x48, 0xd5, 0x0d, 0x76, 0xba,
  36986. 0xff, 0x20, 0x00, 0xf8, 0x42, 0x7f, 0xd2, 0x25, 0x70, 0x90,
  36987. 0x88, 0xb3, 0x98, 0xac, 0xe9, 0xd9, 0xac, 0x58, 0xa6, 0x49,
  36988. 0xcc, 0x93, 0xa5, 0x04, 0x0c, 0x68, 0x53, 0x64, 0x72, 0x8c,
  36989. 0xfc, 0x8d, 0x61, 0xeb, 0x3f, 0x93, 0x8b, 0x85, 0x98, 0x05,
  36990. 0xce, 0x06, 0xd7, 0xbf, 0xbb, 0xa5, 0x22, 0xda, 0xe9, 0x8a,
  36991. 0x29, 0x30, 0x5e, 0x82, 0xe4, 0x46, 0x7c, 0x36, 0x5e, 0xf5,
  36992. 0xc7, 0xe3, 0x09, 0xdf, 0x20, 0x76, 0x73, 0x33, 0x31, 0x75,
  36993. 0xc2, 0x99, 0xe9, 0x74, 0x43, 0x82, 0xb1, 0xeb, 0x74, 0x6f,
  36994. 0xad, 0x59, 0x48, 0x12, 0xa0, 0x24, 0xe3, 0x38, 0x48, 0x61,
  36995. 0x0c, 0xf6, 0x38, 0x83, 0x3a, 0xcd, 0xd6, 0x45, 0x10, 0x0e,
  36996. 0x09, 0x79, 0x31, 0x30, 0x80, 0xfb, 0x34, 0x60, 0x1e, 0x72,
  36997. 0x98, 0xe9, 0x5c, 0xbf, 0xab, 0x21, 0x7f, 0xa3, 0x19, 0x7e,
  36998. 0x8c, 0xa9, 0xa7, 0xfc, 0x25, 0xe0, 0x8e, 0x6d, 0xa1, 0xb9,
  36999. 0x7b, 0x5b, 0x37, 0x33, 0x96, 0xd8, 0x6e, 0x7a, 0xce, 0xa6,
  37000. 0x1a, 0xbd, 0xe6, 0x6e, 0x62, 0xc4, 0x8c, 0x69, 0xfe, 0xe4,
  37001. 0xcb, 0x0a, 0xa1, 0x6c, 0x66, 0x0e, 0x1a, 0x5e, 0xb9, 0xd1,
  37002. 0x4a, 0xa3, 0x91, 0x39, 0xcf, 0x85, 0x07, 0x5b, 0xaf, 0x99,
  37003. 0x11, 0xca, 0xee, 0x6f, 0x2e, 0x33, 0xda, 0x60, 0xbf, 0xd6,
  37004. 0xa0, 0x7a, 0xdb, 0x91, 0x13, 0xb7, 0xa3, 0x5d, 0x0e, 0x1e,
  37005. 0x3b, 0xf9, 0x7a, 0x3e, 0x4f, 0x8d, 0xb3, 0x81, 0xe8, 0x0c,
  37006. 0x4d, 0x48, 0x61, 0x06, 0x14, 0x0f, 0x3e, 0x33, 0x9e, 0xea,
  37007. 0xa6, 0xd8, 0xd8, 0x4d, 0x9b, 0x00, 0x34, 0x0d, 0x31, 0x62,
  37008. 0x54, 0x93, 0x04, 0xd2, 0x02, 0x21, 0x38, 0x91, 0x58, 0xca,
  37009. 0x77, 0xd3, 0x6c, 0xd1, 0x94, 0x05, 0xfa, 0x30, 0x6a, 0x0b,
  37010. 0xf0, 0x52, 0x52, 0xb7, 0xdb, 0x34, 0xff, 0x18, 0x5c, 0x78,
  37011. 0x25, 0x44, 0x39, 0xe4, 0x54, 0x8a, 0xf1, 0x49, 0x04, 0xab,
  37012. 0x8a, 0x5f, 0x87, 0xe1, 0x6e, 0x1a, 0xf2, 0xba, 0x39, 0xb4,
  37013. 0x7c, 0x71, 0x5b, 0xbe, 0x8d, 0xbb, 0xed, 0x3b, 0xed, 0x20,
  37014. 0x95, 0xdf, 0xa7, 0x50, 0xb5, 0x66, 0xff, 0xd0, 0x3a, 0x92,
  37015. 0xde, 0xf2, 0xa3, 0xf2, 0xd6, 0x48, 0x6b, 0xd8, 0xef, 0x80,
  37016. 0x4d, 0xc2, 0x3c, 0xc7, 0xc6, 0x6e, 0xdf, 0xd1, 0x54, 0xfb,
  37017. 0x22, 0xac, 0x1a, 0x11, 0x81, 0x02, 0xc7, 0x66, 0xe0, 0xf3,
  37018. 0xad, 0x0b, 0xd0, 0xec, 0xae, 0x93, 0x53, 0xa5, 0xbf, 0xa5,
  37019. 0x17, 0x59, 0x14, 0x7d, 0x7e, 0x1e, 0x26, 0x15, 0x7a, 0x74,
  37020. 0xfb, 0xb1, 0x7a, 0x0e, 0xd3, 0xb5, 0x7c, 0x8c, 0x3a, 0xd7,
  37021. 0x45, 0x38, 0x55, 0xae, 0x4b, 0xe1, 0xfe, 0x5b, 0x57, 0x20,
  37022. 0x73, 0x38, 0xb9, 0x67, 0x34, 0xb1, 0xf3, 0x15, 0xb0, 0xb7,
  37023. 0x46, 0xa7, 0x1b, 0x19, 0x6d, 0xaf, 0x5e, 0x2c, 0x9c, 0x02,
  37024. 0x3f, 0x0f, 0xa3, 0x56, 0x2f, 0x9f, 0x1a, 0x82, 0x0e, 0xb4,
  37025. 0x46, 0xf5, 0x69, 0x89, 0x91, 0xf9, 0x2d, 0x99, 0x45, 0xa6,
  37026. 0x3c, 0x82, 0x74, 0xac, 0xeb, 0x58, 0x4a, 0xdd, 0x03, 0xaf,
  37027. 0xd1, 0x0a, 0xca, 0x4b, 0xe8, 0x4c, 0x63, 0xd4, 0x73, 0x94,
  37028. 0xbf, 0xd1, 0xc5, 0x8a, 0x3f, 0x6e, 0x58, 0xfc, 0x70, 0x76,
  37029. 0x69, 0x92, 0x05, 0xe0, 0xb9, 0xed, 0x5f, 0x19, 0xd7, 0x6f,
  37030. 0xd0, 0x35, 0xbb, 0x5a, 0x8d, 0x45, 0xac, 0x43, 0xcb, 0x74,
  37031. 0xcc, 0x92, 0xc3, 0x62, 0x56, 0x02, 0xb0, 0x0a, 0xb6, 0x88,
  37032. 0x40, 0x6f, 0x76, 0x1b, 0x89, 0xe4, 0x51, 0xeb, 0x7e, 0x08,
  37033. 0x8c, 0xce, 0x24, 0xc8, 0xd8, 0x58, 0xbd, 0x0e, 0x48, 0x57,
  37034. 0xc8, 0x9f, 0xad, 0x64, 0xcf, 0x69, 0x72, 0x35, 0xbf, 0x04,
  37035. 0x09, 0xfb, 0x0e, 0x62, 0x92, 0x76, 0x8b, 0x8d, 0xd5, 0x16,
  37036. 0xa2, 0x51, 0xdb, 0x71, 0xa9, 0x08, 0xb2, 0xf9, 0x1e, 0x07,
  37037. 0xe7, 0xf8, 0xf4, 0x79, 0x59, 0x2f, 0x8f, 0xf1, 0x5b, 0x45,
  37038. 0xe1, 0xb8, 0xb7, 0xef, 0x86, 0x69, 0x71, 0x51, 0x1c, 0xe5,
  37039. 0x61, 0xee, 0xb8, 0x1d, 0xa7, 0xdc, 0x48, 0xba, 0x51, 0xa5,
  37040. 0x70, 0x4d, 0xfd, 0x2c, 0x46, 0x21, 0x63, 0x0c, 0x9f, 0xb7,
  37041. 0x68, 0x58, 0x7b, 0xb3, 0x7d, 0x64, 0xfd, 0xaf, 0x87, 0x3d,
  37042. 0x86, 0x06, 0x36, 0x8a, 0x6d, 0xfe, 0xdf, 0xce, 0xa8, 0x16,
  37043. 0x42, 0x46, 0x15, 0xe5, 0xcf, 0x48, 0xa6, 0x4b, 0xe5, 0xc1,
  37044. 0xad, 0x14, 0x3a, 0x6d, 0xeb, 0xf9, 0xc9, 0x32, 0xd1, 0x82,
  37045. 0x60, 0x23, 0xf0, 0xff, 0xa7, 0xe6, 0x2e, 0xd6, 0x8d, 0x9d,
  37046. 0x4f, 0x6d, 0xb3, 0xc4, 0xad, 0xd9, 0xf0, 0xf5, 0x5c, 0x47,
  37047. 0x6c, 0x67, 0xf4, 0x0e, 0x18, 0x25, 0xbb, 0x67, 0xfa, 0x11,
  37048. 0x70, 0xd5, 0xbc, 0x3a, 0x34, 0xae, 0xa2, 0x76, 0x4b, 0x9f,
  37049. 0x59, 0x01, 0x18, 0x69, 0x44, 0xc4, 0x8a, 0xff, 0x00, 0xfc,
  37050. 0x2a, 0x45, 0xa9, 0x50, 0x8e, 0x37, 0x6b, 0x78, 0x14, 0x69,
  37051. 0xe7, 0x92, 0x3d, 0xf1, 0x34, 0xd5, 0x5c, 0x48, 0xc2, 0x50,
  37052. 0xb3, 0x0c, 0x7d, 0x54, 0x05, 0x31, 0x1e, 0xce, 0xaa, 0xc1,
  37053. 0x4c, 0xc9, 0x13, 0x33, 0x26, 0x1f, 0x56, 0x7e, 0x7e, 0x74,
  37054. 0xd3, 0x78, 0x3e, 0x00, 0x4a, 0xc8, 0xc6, 0x20, 0x5b, 0xb8,
  37055. 0x80, 0xb4, 0x13, 0x35, 0x23, 0xff, 0x50, 0xde, 0x25, 0x92,
  37056. 0x67, 0x08, 0xb8, 0xa3, 0xb6, 0x39, 0xd4, 0x30, 0xdc, 0xa5,
  37057. 0x88, 0x8a, 0x44, 0x08, 0x8b, 0x6d, 0x2e, 0xb8, 0xf3, 0x0d,
  37058. 0x23, 0xda, 0x35, 0x08, 0x5a, 0x92, 0xe1, 0x40, 0xac, 0xc7,
  37059. 0x15, 0x05, 0x8a, 0xdf, 0xe5, 0x71, 0xd8, 0xe0, 0xd7, 0x9f,
  37060. 0x58, 0x03, 0xf4, 0xec, 0x99, 0x3c, 0xb0, 0xe0, 0x07, 0x42,
  37061. 0x9b, 0xa0, 0x10, 0x7c, 0x24, 0x60, 0x19, 0xe8, 0x84, 0xd4,
  37062. 0xb1, 0x86, 0x19, 0x0a, 0x52, 0x70, 0x6e, 0xc2, 0x3c, 0xe2,
  37063. 0x73, 0x8d, 0xfe, 0xf8, 0x7e, 0xdf, 0x78, 0xe7, 0x92, 0x36,
  37064. 0x10, 0xf7, 0x2d, 0x76, 0x93, 0x8a, 0x0f, 0x20, 0xc8, 0x30,
  37065. 0x59, 0x81, 0xff, 0x3b, 0x70, 0x22, 0xce, 0x6e, 0x23, 0x68,
  37066. 0x35, 0x59, 0x0e, 0xcf, 0xf8, 0xf6, 0xcd, 0x45, 0xb6, 0x41,
  37067. 0xba, 0xda, 0xe6, 0x35, 0x0b, 0xd1, 0xef, 0xa5, 0x7c, 0xe0,
  37068. 0xb9, 0x6f, 0x5b, 0xa9, 0xab, 0x87, 0xe3, 0x3b, 0x92, 0xce,
  37069. 0xbe, 0xfe, 0xf7, 0xab, 0x82, 0xa3, 0xe6, 0xbd, 0xfe, 0xce,
  37070. 0xa6, 0x17, 0xcb, 0x4c, 0xb4, 0x4c, 0xd6, 0xfe, 0xbb, 0x1c,
  37071. 0x10, 0xde, 0x29, 0x3e, 0x92, 0x66, 0x20, 0xf8, 0xee, 0x83,
  37072. 0x86, 0x66, 0xe0, 0x66, 0x97, 0x85, 0xaf, 0x3a, 0x8f, 0xa9,
  37073. 0x97, 0x09, 0xde, 0x77, 0xda, 0xb7, 0x81, 0x41, 0x10, 0xca,
  37074. 0x66, 0x00, 0xec, 0xf8, 0x46, 0x73, 0xa6, 0x24, 0x36, 0xec,
  37075. 0x25, 0xbe, 0x93, 0x5e, 0x74, 0x9f, 0xbe, 0xf4, 0x84, 0x15,
  37076. 0x9c, 0xc5, 0x43, 0xd9, 0xea, 0x5a, 0xcc, 0x2c, 0x4e, 0x2e,
  37077. 0x4e, 0x32, 0xa6, 0x88, 0xb1, 0x25, 0x34, 0xf7, 0xba, 0xab,
  37078. 0xd3, 0xa0, 0xc2, 0x06, 0x70, 0xed, 0x66, 0x4d, 0x71, 0x34,
  37079. 0xaf, 0x10, 0x99, 0x10, 0x11, 0x4f, 0xe4, 0x7d, 0x42, 0x03,
  37080. 0x04, 0x02, 0xc2, 0x41, 0x85, 0x1e, 0xc4, 0xca, 0xae, 0xf0,
  37081. 0x83, 0x78, 0x34, 0x98, 0x55, 0x8b, 0x4c, 0xa0, 0x14, 0xea,
  37082. 0x15, 0x2c, 0xa1, 0x30, 0xd8, 0xcf, 0xac, 0xd4, 0xca, 0xf7,
  37083. 0xf4, 0xc4, 0x20, 0xca, 0xa1, 0xef, 0xce, 0x5d, 0x6b, 0x32,
  37084. 0xb6, 0xf0, 0x22, 0x08, 0x49, 0x21, 0x0c, 0x57, 0x0f, 0xf8,
  37085. 0xc0, 0xd2, 0xe3, 0xc0, 0xa6, 0x31, 0xc7, 0x87, 0x96, 0xa9,
  37086. 0xfe, 0x69, 0xa0, 0x7f, 0xf7, 0x8e, 0x31, 0x92, 0x37, 0xce,
  37087. 0xde, 0x36, 0x3f, 0xf5, 0x7d, 0x07, 0xaa, 0xa9, 0x43, 0xee,
  37088. 0x3c, 0x8c, 0xd3, 0x7d, 0x2c, 0xa6, 0xc3, 0x98, 0xab, 0xbe,
  37089. 0x90, 0x4c, 0xa5, 0x5a, 0x27, 0xeb, 0x0e, 0xed, 0xa1, 0x1e,
  37090. 0x3e, 0x44, 0xa3, 0x4b, 0x49, 0xad, 0xe4, 0x19, 0x90, 0xc8,
  37091. 0x9e, 0x6e, 0x5b, 0x68, 0xbc, 0x37, 0x54, 0xaf, 0xa6, 0xb7,
  37092. 0x71, 0x5c, 0x5d, 0x74, 0x83, 0xf4, 0xb9, 0x2f, 0xe5, 0x1a,
  37093. 0x0c, 0x73, 0x30, 0x56, 0x82, 0x04, 0xb3, 0x0e, 0x32, 0x98,
  37094. 0xfd, 0x27, 0xa0, 0xfe, 0xe0, 0xe0, 0xf5, 0xb7, 0xe0, 0x47,
  37095. 0x2a, 0xa6, 0x4a, 0xe0, 0xfc, 0xb5, 0xd8, 0xfd, 0x01, 0xfe,
  37096. 0x4e, 0x96, 0x17, 0x06, 0xcc, 0x92, 0x7c, 0xa1, 0x2f, 0xb5,
  37097. 0x04, 0x08, 0x76, 0xcc, 0x40, 0x75, 0x37, 0x4d, 0x2c, 0x74,
  37098. 0xcd, 0xc7, 0x62, 0xa6, 0xe6, 0xd8, 0x9e, 0x21, 0x7f, 0x2e,
  37099. 0xf5, 0x2c, 0xcf, 0x0b, 0x3f, 0xd7, 0xed, 0x17, 0xee, 0x92,
  37100. 0xaf, 0xf9, 0xa4, 0x71, 0x5d, 0x5f, 0x81, 0xb9, 0x2f, 0x12,
  37101. 0xe5, 0x57, 0x2d, 0x1e, 0xf1, 0x67, 0x47, 0x2a, 0xde, 0xab,
  37102. 0xf2, 0xea, 0xb7, 0xb5, 0x83, 0xdc, 0x46, 0xd4, 0xf3, 0x25,
  37103. 0x65, 0x15, 0x4d, 0x66, 0x34, 0x54, 0xab, 0x94, 0x89, 0x80,
  37104. 0x39, 0xd3, 0x39, 0xe3, 0xa2, 0xb1, 0x91, 0x2a, 0x5e, 0x55,
  37105. 0xe1, 0xa4, 0x0f, 0xc3, 0x4b, 0x5a, 0xa5, 0x4a, 0xb3, 0xc0,
  37106. 0x40, 0xea, 0x16, 0x0c, 0xd5, 0x2d, 0x83, 0x3e, 0x28, 0x20,
  37107. 0xac, 0x0a, 0x1b, 0x5b, 0x87, 0xcf, 0xf1, 0x51, 0xd6, 0xda,
  37108. 0xd1, 0xc9, 0xb1, 0x27, 0xf5, 0x62, 0x03, 0x10, 0xcf, 0x76,
  37109. 0x28, 0xa2, 0xea, 0x4b, 0x76, 0xaf, 0x9c, 0x3d, 0xf1, 0x1b,
  37110. 0x92, 0xff, 0xb0, 0xca, 0x16, 0xa2, 0x29, 0x94, 0x0e, 0x1e,
  37111. 0x51, 0xfb, 0xe1, 0x2b, 0x5a, 0x50, 0xfd, 0xaf, 0xab, 0xd7,
  37112. 0x32, 0xaa, 0x43, 0xa7, 0xcb, 0xd3, 0xd3, 0xe9, 0x1e, 0xb1,
  37113. 0x70, 0xd2, 0xbb, 0x15, 0x68, 0x49, 0xee, 0x6e, 0x1e, 0xc5,
  37114. 0x64, 0x4b, 0x26, 0x08, 0xe7, 0x32, 0x1c, 0x1d, 0x73, 0x8f,
  37115. 0x42, 0xfe, 0xeb, 0x67, 0x89, 0x42, 0x25, 0x40, 0xd6, 0x15,
  37116. 0x02, 0x55, 0x87, 0xe3, 0x87, 0xdd, 0x78, 0xc1, 0x01, 0x94,
  37117. 0xbc, 0x30, 0x5f, 0xbd, 0x89, 0xe1, 0xb0, 0x5c, 0xcd, 0xb7,
  37118. 0x68, 0xd5, 0xbb, 0xf4, 0xa0, 0x5d, 0x3d, 0xdd, 0x89, 0x12,
  37119. 0xc7, 0xb8, 0x5d, 0x51, 0x8a, 0xf4, 0xd5, 0x05, 0xc6, 0xdd,
  37120. 0x7b, 0x44, 0x38, 0xce, 0xb1, 0x24, 0x24, 0xe1, 0x9d, 0xc7,
  37121. 0x80, 0x86, 0x46, 0x2a, 0xd2, 0xa4, 0x0f, 0xec, 0xd3, 0x6b,
  37122. 0x31, 0xc0, 0x05, 0x31, 0xff, 0xf5, 0x1a, 0x33, 0x35, 0x68,
  37123. 0x2e, 0x68, 0x24, 0xbd, 0x62, 0xfc, 0x46, 0x79, 0x54, 0x5e,
  37124. 0x1e, 0x27, 0x93, 0x07, 0xed, 0x78, 0x94, 0x50, 0x42, 0x98,
  37125. 0x53, 0x88, 0xb7, 0x57, 0x04, 0x7d, 0xe2, 0xe1, 0xb5, 0x61,
  37126. 0x9e, 0x5a, 0x88, 0x31, 0x3e, 0x6c, 0x69, 0xbc, 0x8a, 0xe6,
  37127. 0xbc, 0x9d, 0x20, 0x7a, 0x86, 0xe5, 0x73, 0x93, 0x02, 0xc5,
  37128. 0xde, 0xdc, 0xcc, 0xbf, 0x89, 0x76, 0xdc, 0x4e, 0xa1, 0x89,
  37129. 0xe7, 0x95, 0x75, 0x01, 0xf7, 0x43, 0xaa, 0x3f, 0x1b, 0xb7,
  37130. 0x8c, 0x92, 0x66, 0x22, 0xbe, 0x34, 0xf1, 0x2f, 0xc3, 0xc7,
  37131. 0x21, 0xaf, 0x25, 0x57, 0x9a, 0x2c, 0x80, 0xf0, 0xb3, 0xdd,
  37132. 0xb3, 0xb2, 0x82, 0x97, 0x85, 0x73, 0xa9, 0x76, 0xe4, 0x37,
  37133. 0xa2, 0x65, 0xf9, 0xc1, 0x3d, 0x11, 0xbf, 0xcb, 0x3c, 0x8e,
  37134. 0xdd, 0xaf, 0x98, 0x57, 0x6a, 0xe1, 0x33, 0xe7, 0xf0, 0xff,
  37135. 0xed, 0x61, 0x53, 0xfe, 0x1e, 0x2d, 0x06, 0x2f, 0xb8, 0x9e,
  37136. 0xf9, 0xa5, 0x21, 0x06, 0xf3, 0x72, 0xf6, 0xa3, 0x77, 0xbb,
  37137. 0x63, 0x6e, 0x52, 0xb2, 0x42, 0x47, 0x9b, 0x92, 0x4c, 0xf8,
  37138. 0xd2, 0xe6, 0x02, 0xa5, 0x57, 0x2d, 0x6f, 0x30, 0x05, 0xe2,
  37139. 0xfd, 0x33, 0xe5, 0xb6, 0x23, 0x85, 0x89, 0x4a, 0x99, 0x20,
  37140. 0x33, 0xea, 0x2f, 0xcd, 0x28, 0x27, 0xff, 0xfd, 0x2e, 0x73,
  37141. 0x52, 0x29, 0x19, 0x7c, 0x65, 0xf5, 0x6a, 0xaa, 0x97, 0x6e,
  37142. 0xe9, 0x42, 0xa8, 0x55, 0x97, 0x56, 0x92, 0x9d, 0xd2, 0xd1,
  37143. 0xc4, 0x30, 0xaa, 0x95, 0x86, 0xba, 0x71, 0xdd, 0x2f, 0xf1,
  37144. 0xed, 0x66, 0x54, 0x78, 0x4b, 0x13, 0x31, 0xed, 0x9d, 0x2c,
  37145. 0xae, 0x0a, 0xc3, 0xca, 0xfb, 0x3f, 0x92, 0x92, 0x30, 0xa3,
  37146. 0x8e, 0xc8, 0x6d, 0x7b, 0x42, 0xd5, 0x5d, 0x99, 0x79, 0x42,
  37147. 0x28, 0x63, 0x9f, 0x97, 0x8e, 0x94, 0x6d, 0x1d, 0xb4, 0x21,
  37148. 0x39, 0xc7, 0x64, 0x48, 0x44, 0x5e, 0x15, 0x10, 0x45, 0x9f,
  37149. 0x8a, 0x01, 0x45, 0x20, 0x5c, 0xd1, 0x28, 0x0d, 0xe9, 0xfb,
  37150. 0xa9, 0x72, 0x68, 0x07, 0x31, 0x20, 0x75, 0x76, 0x82, 0x76,
  37151. 0x5d, 0x7c, 0xc1, 0x5d, 0x42, 0x40, 0xfd, 0x06, 0xa9, 0x66,
  37152. 0xb0, 0x36, 0x55, 0x86, 0x6c, 0x96, 0xbd, 0xb8, 0xf7, 0x36,
  37153. 0x87, 0xf2, 0xa1, 0x37, 0xd8, 0x2d, 0x83, 0xf5, 0xdc, 0xd8,
  37154. 0xde, 0x9e, 0x69, 0xd6, 0xe1, 0x0d, 0xd5, 0x93, 0xc5, 0xee,
  37155. 0xba, 0xd3, 0x40, 0x71, 0xbb, 0xc7, 0xbb, 0x50, 0x1a, 0x10,
  37156. 0x80, 0x99, 0x62, 0x1c, 0xe3, 0x1f, 0xa2, 0xcc, 0x98, 0xe1,
  37157. 0xaa, 0xff, 0xd9, 0x69, 0xe7, 0x87, 0x04, 0x87, 0x76, 0xec,
  37158. 0x55, 0x18, 0xaf, 0x82, 0x34, 0x4d, 0x4f, 0xf7, 0x57, 0x1f,
  37159. 0xa5, 0x43, 0xcc, 0xe9, 0x7a, 0x4a, 0xc8, 0xb4, 0x1f, 0x61,
  37160. 0x40, 0x5e, 0x1d, 0x11, 0xdd, 0xdc, 0xdc, 0xb4, 0x57, 0xf9,
  37161. 0x47, 0x96, 0xbc, 0x47, 0x29, 0xf8, 0xf2, 0x43, 0xc4, 0xa0,
  37162. 0x8c, 0x14, 0x5e, 0x73, 0x52, 0xac, 0xac, 0x39, 0x3b, 0x06,
  37163. 0x19, 0x1a, 0xca, 0x22, 0xc8, 0x96, 0x12, 0x2e, 0x4c, 0x7b,
  37164. 0xa0, 0x96, 0x53, 0x16, 0xce, 0x6d, 0x6e, 0xac, 0xb2, 0x07,
  37165. 0x17, 0x22, 0x07, 0x30, 0x20, 0x84, 0x9b, 0x0e, 0x92, 0x31,
  37166. 0x07, 0xe2, 0x77, 0xcd, 0x6a, 0x3e, 0x16, 0x4f, 0xd6, 0x12,
  37167. 0x88, 0x8a, 0x70, 0x5a, 0x87, 0xd8, 0xb9, 0xef, 0x76, 0xab,
  37168. 0x14, 0x65, 0x87, 0x3a, 0xef, 0xd8, 0x0e, 0x24, 0x40, 0x73,
  37169. 0x93, 0x2b, 0xbf, 0xac, 0xfe, 0x96, 0x8a, 0x9d, 0x12, 0xe6,
  37170. 0xc1, 0x5b, 0x00, 0x3b, 0x23, 0xee, 0xe2, 0x10, 0xb6, 0xbe,
  37171. 0x0e, 0x2f, 0xa2, 0x77, 0x16, 0x17, 0xfc, 0x4b, 0x2c, 0xd7,
  37172. 0x9c, 0xad, 0x66, 0xb4, 0xf2, 0xfd, 0xc1, 0xaf, 0x81, 0x12,
  37173. 0xd9, 0xed, 0x14, 0x32, 0xcf, 0x1b, 0xee, 0xc6, 0x63, 0xe8,
  37174. 0xe5, 0xe6, 0xb6, 0x91, 0x8d, 0x1b, 0x90, 0x75, 0x5d, 0x69,
  37175. 0x4c, 0x5d, 0xd6, 0xac, 0x79, 0xe8, 0xb6, 0xdf, 0xbf, 0x43,
  37176. 0x39, 0xd3, 0xb8, 0xf0, 0x39, 0xf4, 0x90, 0xaf, 0x73, 0x26,
  37177. 0xc7, 0x73, 0x6f, 0x93, 0xbb, 0xce, 0x6e, 0xdc, 0x1c, 0xd0,
  37178. 0x36, 0x23, 0x17, 0xb2, 0x39, 0x37, 0x15, 0xf5, 0x3a, 0x61,
  37179. 0xa9, 0x15, 0x52, 0x6e, 0xc5, 0x3a, 0x63, 0x79, 0x5d, 0x45,
  37180. 0xdc, 0x3a, 0xd5, 0x26, 0x01, 0x56, 0x97, 0x80, 0x7f, 0x83,
  37181. 0xf9, 0xec, 0xde, 0xa0, 0x2e, 0x7a, 0xb2, 0x4b, 0x04, 0x63,
  37182. 0x60, 0x05, 0xce, 0x96, 0xeb, 0xe0, 0x0a, 0x5f, 0xb0, 0x7e,
  37183. 0x6d, 0x0a, 0x24, 0x32, 0x47, 0x82, 0x7f, 0x0b, 0xd7, 0xe9,
  37184. 0xd5, 0x14, 0xa9, 0x6b, 0x10, 0x5d, 0x1e, 0x1f, 0x8a, 0xad,
  37185. 0x70, 0x91, 0xd4, 0x33, 0x1d, 0xc2, 0x3e, 0xf8, 0xc8, 0x52,
  37186. 0x9a, 0x27, 0x1f, 0x45, 0x2f, 0xb5, 0xc7, 0xb1, 0x8b, 0xf9,
  37187. 0xc6, 0x7b, 0xb5, 0x92, 0x7a, 0xdd, 0xeb, 0x07, 0x6c, 0x6f,
  37188. 0x11, 0xd7, 0x5b, 0x56, 0x56, 0xec, 0x88, 0x1c, 0xc9, 0xb4,
  37189. 0xe8, 0x43, 0xab, 0xdf, 0x0b, 0xc5, 0x28, 0xba, 0x70, 0x5d,
  37190. 0xd3, 0xb2, 0xe2, 0xcf, 0xa7, 0xbb, 0x53, 0x04, 0x6b, 0x73,
  37191. 0xdf, 0x27, 0xa6, 0x63, 0x58, 0xe1, 0x39, 0x26, 0x2a, 0x1a,
  37192. 0x21, 0xec, 0xbb, 0x5f, 0x46, 0x98, 0x3d, 0x48, 0x66, 0xfe,
  37193. 0xf3, 0xcb, 0xfc, 0x6e, 0x99, 0x82, 0x91, 0xce, 0x53, 0xfd,
  37194. 0x75, 0xc9, 0xb6, 0x08, 0xa8, 0xf3, 0xe4, 0xe0, 0xa0, 0x24,
  37195. 0x45, 0xb4, 0x69, 0x11, 0xac, 0x06, 0x1c, 0x39, 0x71, 0xcf,
  37196. 0x72, 0xfc, 0x77, 0x9b, 0x5f, 0xf4, 0x8b, 0x02, 0x31, 0xf3,
  37197. 0x67, 0xd1, 0x9b, 0xe0, 0x49, 0xa4, 0x69, 0x20, 0x99, 0x38,
  37198. 0xa7, 0xf5, 0x43, 0xd2, 0x45, 0x9f, 0x7a, 0xe7, 0xad, 0x7e,
  37199. 0x36, 0xee, 0xfd, 0x8c, 0xc5, 0x6a, 0x12, 0x58, 0x15, 0x3b,
  37200. 0x02, 0x81, 0x73, 0x8b, 0x10, 0xda, 0x21, 0xc7, 0x1d, 0x38,
  37201. 0xd8, 0x40, 0x7a, 0xa3, 0x59, 0x55, 0x35, 0x44, 0xa9, 0x9c,
  37202. 0xf5, 0xf4, 0xe4, 0x14, 0xc1, 0xc4, 0x15, 0x26, 0x01, 0xe3,
  37203. 0x31, 0xbf, 0xdc, 0xbc, 0x69, 0x0b, 0xcf, 0x71, 0x8c, 0xdb,
  37204. 0x16, 0xab, 0x36, 0x3e, 0xb3, 0xa4, 0x9f, 0xcc, 0xbf, 0xa2,
  37205. 0x93, 0x93, 0x9a, 0x3b, 0xaf, 0x72, 0x8d, 0x8b, 0x92, 0x44,
  37206. 0x5d, 0x6f, 0xc5, 0xf0, 0xdc, 0x65, 0x62, 0xea, 0xba, 0x33,
  37207. 0xe7, 0x6c, 0xa4, 0x35, 0xcf, 0xd9, 0xbc, 0x3c, 0xbf, 0x25,
  37208. 0x7b, 0x7c, 0x0b, 0x62, 0x92, 0x5a, 0x66, 0x63, 0xe1, 0x27,
  37209. 0x89, 0x12, 0xe2, 0xae, 0xb7, 0xf8, 0x04, 0x70, 0xda, 0x4a,
  37210. 0x3d, 0xa6, 0x67, 0x12, 0x14, 0x9e, 0x8e, 0xdc, 0xa2, 0xf2,
  37211. 0x3d, 0xc7, 0xd2, 0x8f, 0x18, 0x3a, 0x53, 0x8c, 0x83, 0x5d,
  37212. 0x66, 0xbb, 0x9f, 0x8c, 0xaf, 0xa8, 0x73, 0x08, 0x2e, 0x6d,
  37213. 0x30, 0xa0, 0xd0, 0x20, 0x94, 0x48, 0xad, 0x5e, 0x31, 0xfd,
  37214. 0x5e, 0xfd, 0xf9, 0xb5, 0xa2, 0x39, 0xa3, 0xb9, 0xdf, 0x4d,
  37215. 0xa4, 0xb1, 0x54, 0xcc, 0x92, 0x63, 0x2c, 0x66, 0x2d, 0x01,
  37216. 0x88, 0x8b, 0x7d, 0xc6, 0x5c, 0x9f, 0x18, 0x9a, 0x53, 0x91,
  37217. 0x59, 0x66, 0x70, 0xd7, 0x81, 0x0e, 0xa1, 0x3c, 0x7e, 0x86,
  37218. 0x85, 0x64, 0x38, 0x6f, 0xec, 0x76, 0x57, 0x80, 0x41, 0x9d,
  37219. 0xef, 0x61, 0xb8, 0xb2, 0x8a, 0xeb, 0xe9, 0x26, 0xbb, 0x69,
  37220. 0xb3, 0x8d, 0xd4, 0x6b, 0x05, 0xd8, 0x55, 0x1c, 0xbd, 0x9f,
  37221. 0x6b, 0x23, 0x46, 0x2b, 0xf7, 0xfb, 0x4d, 0x33, 0x3b, 0x21,
  37222. 0x6d, 0xea, 0x1b, 0x15, 0xaf, 0x0f, 0x8c, 0x98, 0xc8, 0xf4,
  37223. 0xd1, 0x3c, 0xdd, 0x21, 0xd0, 0x45, 0xdc, 0xaf, 0x89, 0x89,
  37224. 0xbf, 0xde, 0xbf, 0x46, 0x9e, 0x9e, 0x18, 0x56, 0x9d, 0x05,
  37225. 0x4d, 0x63, 0x5f, 0x1c, 0xd9, 0x15, 0xd1, 0x43, 0x17, 0x0c,
  37226. 0x48, 0x3d, 0x36, 0x8b, 0x14, 0x87, 0xc8, 0x10, 0x44, 0xdf,
  37227. 0x9c, 0xfd, 0x6e, 0x88, 0x88, 0xae, 0x7f, 0x7f, 0x67, 0xa3,
  37228. 0x33, 0x4d, 0xa3, 0x84, 0x8b, 0x58, 0x07, 0x17, 0xd8, 0x1d,
  37229. 0x9e, 0x43, 0xd6, 0x41, 0x9c, 0xff, 0xfa, 0x35, 0xa2, 0x42,
  37230. 0xa9, 0x5d, 0xa9, 0x4b, 0x95, 0x23, 0x6a, 0x6e, 0x42, 0xd7,
  37231. 0xa2, 0x0a, 0x70, 0x00, 0x61, 0x8b, 0x45, 0xbb, 0xac, 0x20,
  37232. 0x27, 0xcd, 0xfc, 0x61, 0x17, 0xfe, 0xab, 0x6b, 0xe8, 0xe0,
  37233. 0x51, 0xab, 0xa3, 0xbf, 0xe4, 0x85, 0x69, 0x8e, 0xd7, 0xa6,
  37234. 0x62, 0x33, 0x8f, 0x7c, 0xba, 0x48, 0xfa, 0x83, 0x94, 0xa5,
  37235. 0xdf, 0xa1, 0x76, 0xdc, 0xa9, 0x4b, 0x3c, 0x27, 0xff, 0xd9,
  37236. 0xbe, 0xf4, 0x80, 0x5a, 0xca, 0x33, 0xf3, 0x9a, 0x1d, 0xf8,
  37237. 0xf3, 0xe1, 0x83, 0x27, 0x0b, 0x59, 0x87, 0x31, 0x7d, 0x4f,
  37238. 0x5a, 0x5e, 0xe1, 0xbe, 0xa9, 0x68, 0xe9, 0x6f, 0x10, 0x0a,
  37239. 0xe2, 0x70, 0x05, 0xaa, 0xcb, 0xdd, 0x41, 0xd7, 0x49, 0x8a,
  37240. 0x98, 0xa0, 0x40, 0x2d, 0xc6, 0x56, 0x49, 0xca, 0x60, 0x16,
  37241. 0x9c, 0x38, 0xc9, 0xfe, 0x99, 0x15, 0xfb, 0x79, 0x01, 0x33,
  37242. 0xcd, 0x54, 0x2f, 0xf3, 0x70, 0x37, 0x82, 0x36, 0x32, 0x76,
  37243. 0x8f, 0x63, 0x00, 0xa2, 0x42, 0xce, 0x39, 0x90, 0xfc, 0xf8,
  37244. 0xff, 0x34, 0x38, 0x0a, 0x17, 0x5e, 0x9d, 0x34, 0x86, 0xde,
  37245. 0x33, 0x45, 0xac, 0xbf, 0x81, 0xdf, 0xd2, 0xbc, 0xc7, 0xd7,
  37246. 0xd1, 0xee, 0xde, 0x2b, 0x5b, 0x50, 0x56, 0xb5, 0x88, 0x00,
  37247. 0x92, 0x76, 0x5a, 0x34, 0x0c, 0xfe, 0x8f, 0xc5, 0xa0, 0x92,
  37248. 0xb0, 0xed, 0x43, 0xe7, 0x81, 0x39, 0x36, 0x6e, 0xb7, 0x4d,
  37249. 0x5b, 0xcf, 0xc7, 0xf0, 0x83, 0xe5, 0xdc, 0xb7, 0x74, 0xf4,
  37250. 0xf3, 0xbd, 0xa8, 0xa6, 0x7b, 0xe0, 0xc5, 0x50, 0xaa, 0xc7,
  37251. 0x83, 0x4d, 0xd9, 0xc5, 0x97, 0x03, 0x7c, 0x0c, 0x3b, 0x3a,
  37252. 0x18, 0xb2, 0x8c, 0xee, 0x67, 0x91, 0x38, 0x84, 0x8f, 0xef,
  37253. 0xb4, 0xf4, 0xe4, 0x7c, 0x1a, 0x3f, 0xa3, 0x0a, 0xd9, 0xba,
  37254. 0xff, 0x56, 0xd8, 0xe2, 0x82, 0xfc, 0x58, 0x8f, 0xf6, 0x12,
  37255. 0x10, 0x65, 0x6a, 0x68, 0x53, 0x2d, 0x9f, 0x2c, 0x77, 0xd1,
  37256. 0xb8, 0x21, 0x8a, 0xcb, 0xe9, 0xd4, 0x25, 0x18, 0x22, 0x46,
  37257. 0x3e, 0x72, 0x29, 0x2a, 0x68, 0x70, 0x73, 0xe2, 0x61, 0xa2,
  37258. 0xa8, 0x1f, 0x24, 0x48, 0x92, 0xa0, 0xd4, 0xdd, 0xde, 0xe5,
  37259. 0x02, 0x1b, 0x59, 0x5c, 0x7e, 0x92, 0x9c, 0xd8, 0xf4, 0x2d,
  37260. 0x6b, 0x79, 0x7b, 0xc7, 0xcd, 0xef, 0x21, 0x2a, 0x50, 0x7e,
  37261. 0xba, 0xdd, 0x02, 0x45, 0x7e, 0xc1, 0xdd, 0xeb, 0x00, 0x00,
  37262. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  37263. 0x00, 0x00, 0x00, 0x03, 0x0c, 0x15, 0x1c, 0x22, 0x28,
  37264. };
  37265. return dilithium_param_vfy_test(WC_ML_DSA_65, ml_dsa_65_pub_key,
  37266. (word32)sizeof(ml_dsa_65_pub_key), ml_dsa_65_sig,
  37267. (word32)sizeof(ml_dsa_65_sig));
  37268. }
  37269. #endif
  37270. #ifndef WOLFSSL_NO_ML_DSA_87
  37271. static wc_test_ret_t dilithium_param_87_vfy_test(void)
  37272. {
  37273. WOLFSSL_SMALL_STACK_STATIC const byte ml_dsa_87_pub_key[] = {
  37274. 0xef, 0x49, 0x79, 0x47, 0x15, 0xc4, 0x8a, 0xa9, 0x74, 0x2a,
  37275. 0xf0, 0x36, 0x94, 0x5c, 0x91, 0x1c, 0x5d, 0xff, 0x2c, 0x83,
  37276. 0xf2, 0x8b, 0x04, 0xfc, 0x5d, 0x64, 0xbd, 0x49, 0x73, 0xcd,
  37277. 0xcc, 0x99, 0x50, 0x5f, 0x2b, 0x16, 0x3a, 0xbb, 0x98, 0xc0,
  37278. 0xa7, 0x69, 0x0e, 0x95, 0x99, 0x0b, 0xa2, 0x6c, 0xfe, 0x6c,
  37279. 0xdb, 0xc8, 0xa7, 0x09, 0x46, 0x6c, 0x90, 0x50, 0xa4, 0x75,
  37280. 0x30, 0xf7, 0x90, 0xac, 0x31, 0xb6, 0xdd, 0x21, 0xaf, 0xc6,
  37281. 0xf9, 0xfe, 0xee, 0xc6, 0x5b, 0xa8, 0x8f, 0x0a, 0x2e, 0xd0,
  37282. 0x42, 0xab, 0xa8, 0x3c, 0x8d, 0xbf, 0xf7, 0x44, 0xbd, 0x0d,
  37283. 0xcf, 0xf4, 0x68, 0xfc, 0x16, 0x67, 0xf7, 0x39, 0x48, 0x5f,
  37284. 0x56, 0xd1, 0xe7, 0x1f, 0x49, 0x80, 0x50, 0xbe, 0x54, 0xd1,
  37285. 0xb7, 0xc9, 0xd2, 0x32, 0xc7, 0x08, 0x8c, 0xde, 0x2c, 0x31,
  37286. 0xf6, 0x1d, 0xc7, 0xac, 0xb3, 0x79, 0xd7, 0x4b, 0x1b, 0x23,
  37287. 0x89, 0x0a, 0xdc, 0x8e, 0x44, 0x41, 0x14, 0x28, 0x99, 0x13,
  37288. 0xb3, 0x26, 0xa6, 0x0e, 0x83, 0x60, 0xaa, 0x8d, 0x7c, 0x23,
  37289. 0x13, 0xba, 0x6c, 0x28, 0x90, 0x56, 0x84, 0xa1, 0x23, 0x8b,
  37290. 0x81, 0x20, 0x97, 0x7c, 0x66, 0x3f, 0xed, 0x5d, 0xd0, 0xe4,
  37291. 0x5d, 0xee, 0x46, 0xbc, 0x4b, 0x3c, 0x03, 0xb5, 0xbc, 0x4d,
  37292. 0x8d, 0x37, 0xa3, 0x56, 0x4b, 0x33, 0xad, 0xef, 0xd4, 0xb6,
  37293. 0xec, 0xdb, 0x04, 0x9a, 0x19, 0x58, 0x57, 0xd8, 0x00, 0x3a,
  37294. 0x92, 0x61, 0x0c, 0x0b, 0xc8, 0x52, 0xe5, 0x04, 0x02, 0x9a,
  37295. 0x00, 0x7e, 0xec, 0x7e, 0x94, 0xaa, 0xef, 0x2d, 0x7f, 0xb6,
  37296. 0x2e, 0x7c, 0xb0, 0x73, 0xa2, 0x20, 0xc0, 0x07, 0x30, 0x41,
  37297. 0x50, 0x20, 0x14, 0x18, 0x21, 0x5e, 0x2a, 0x6f, 0x70, 0x21,
  37298. 0xd6, 0x97, 0x13, 0xb9, 0xc1, 0x9e, 0x90, 0x67, 0xcc, 0x55,
  37299. 0x8a, 0xec, 0xec, 0x0a, 0x1e, 0x90, 0xdc, 0x3f, 0xb0, 0x4d,
  37300. 0xd1, 0x18, 0xea, 0x4f, 0xcb, 0x5d, 0x15, 0x4c, 0xb8, 0x35,
  37301. 0x9b, 0x34, 0x24, 0x30, 0x06, 0x53, 0x17, 0xf0, 0xbe, 0x27,
  37302. 0x36, 0xb3, 0x04, 0x6a, 0xbd, 0xbf, 0xa7, 0x39, 0xee, 0xa9,
  37303. 0x8f, 0x0e, 0x98, 0xc5, 0xf5, 0x9f, 0x46, 0x25, 0x93, 0xc9,
  37304. 0xf2, 0xf6, 0x2b, 0x8e, 0x92, 0x06, 0x01, 0x3d, 0x81, 0x18,
  37305. 0xf2, 0xec, 0xf1, 0x05, 0x4c, 0xad, 0x4b, 0xcb, 0x98, 0xa4,
  37306. 0xb5, 0x61, 0x20, 0xda, 0x81, 0xa1, 0xfb, 0x92, 0x4c, 0xaf,
  37307. 0x87, 0x6f, 0x6e, 0xd2, 0x57, 0xec, 0xcd, 0x94, 0xb3, 0x79,
  37308. 0xbf, 0x59, 0x88, 0x17, 0x81, 0xce, 0x8a, 0x57, 0xce, 0x57,
  37309. 0xae, 0x3e, 0x82, 0x81, 0x2f, 0x83, 0x61, 0xd8, 0xf9, 0x68,
  37310. 0x21, 0xe7, 0x72, 0x5b, 0xd6, 0x80, 0x55, 0x68, 0x5d, 0x67,
  37311. 0x15, 0x0c, 0x8b, 0xdc, 0x4f, 0xc3, 0x89, 0x36, 0x3c, 0xac,
  37312. 0xaf, 0x16, 0x5e, 0x1c, 0xfa, 0x68, 0x74, 0x6a, 0xab, 0x68,
  37313. 0xd8, 0x59, 0x96, 0x2d, 0x33, 0x62, 0xe4, 0xbd, 0xb3, 0xb7,
  37314. 0x4d, 0x88, 0x35, 0xb8, 0xed, 0xb2, 0x16, 0x85, 0x97, 0x08,
  37315. 0x71, 0x71, 0x39, 0x7e, 0x0c, 0x53, 0x16, 0xda, 0x38, 0xe5,
  37316. 0x28, 0x09, 0x9c, 0xd9, 0x46, 0xec, 0x68, 0xda, 0x8d, 0xd0,
  37317. 0xad, 0xb2, 0x79, 0x28, 0x3b, 0x1e, 0x12, 0xc9, 0xdf, 0xa9,
  37318. 0x6d, 0x3d, 0x29, 0x99, 0x2f, 0x53, 0xc2, 0xd0, 0xf9, 0x88,
  37319. 0x26, 0x94, 0x47, 0xaf, 0xf6, 0x96, 0xf3, 0xe1, 0x11, 0xa6,
  37320. 0x82, 0x3d, 0x43, 0x3f, 0x1f, 0xbc, 0xf6, 0x98, 0xbe, 0xff,
  37321. 0x06, 0x86, 0x61, 0x27, 0xdc, 0x91, 0x54, 0xd4, 0xfc, 0x68,
  37322. 0x83, 0xe8, 0x35, 0x3e, 0xee, 0x94, 0x59, 0x28, 0x2f, 0xde,
  37323. 0xdd, 0x03, 0x60, 0x66, 0xc1, 0x49, 0x57, 0xdd, 0xbc, 0xd5,
  37324. 0x0a, 0x67, 0x34, 0xf1, 0xa6, 0x0a, 0x57, 0x94, 0x65, 0x02,
  37325. 0x2c, 0x52, 0x43, 0x70, 0x3b, 0xc1, 0x9a, 0xff, 0xda, 0x6f,
  37326. 0xb9, 0x54, 0x47, 0x01, 0xda, 0x27, 0xe4, 0x48, 0x4a, 0x90,
  37327. 0x9f, 0xb5, 0xc3, 0xee, 0x0e, 0x09, 0x57, 0xfe, 0x48, 0x51,
  37328. 0x08, 0x34, 0x5e, 0x8f, 0x16, 0xc9, 0x0b, 0x74, 0xd9, 0x7d,
  37329. 0x22, 0x3f, 0xd6, 0xb7, 0x5d, 0xd6, 0x76, 0x00, 0x8d, 0x4e,
  37330. 0x78, 0x73, 0x86, 0xd6, 0xdb, 0x2a, 0x65, 0xab, 0xdf, 0xb0,
  37331. 0xea, 0x11, 0xad, 0xdf, 0xba, 0x43, 0xdb, 0xa8, 0x0a, 0xfb,
  37332. 0x04, 0x38, 0x81, 0x2b, 0xa3, 0x29, 0xfc, 0x95, 0x73, 0x9a,
  37333. 0x0c, 0x6c, 0x9e, 0xcd, 0xdc, 0xcf, 0x0a, 0x0c, 0x18, 0x41,
  37334. 0x6f, 0x1d, 0xa3, 0xf6, 0x12, 0x4c, 0x13, 0xf2, 0x02, 0xc6,
  37335. 0x50, 0x99, 0x86, 0x73, 0xa7, 0xf9, 0x7e, 0x84, 0x7f, 0x4c,
  37336. 0x00, 0xce, 0x2e, 0x21, 0x76, 0x8e, 0x17, 0x7a, 0x87, 0x6f,
  37337. 0x81, 0xe6, 0xc0, 0x52, 0xa5, 0xa0, 0x3c, 0x54, 0x3c, 0xec,
  37338. 0xb0, 0x9d, 0x1c, 0x3b, 0xec, 0xe5, 0x4e, 0x4a, 0x37, 0xe7,
  37339. 0xd5, 0xa9, 0x07, 0x87, 0x23, 0x28, 0x5d, 0x3d, 0x22, 0x02,
  37340. 0x79, 0x40, 0x3f, 0x2d, 0x40, 0xc9, 0xe5, 0xa6, 0x9b, 0xa8,
  37341. 0xb8, 0x76, 0xf6, 0x77, 0x5b, 0x8d, 0x72, 0x96, 0x3e, 0x13,
  37342. 0xbf, 0x76, 0xfa, 0x7b, 0xb7, 0x82, 0x5f, 0xe7, 0x9d, 0x54,
  37343. 0x0e, 0x05, 0x1a, 0x9f, 0xa4, 0x42, 0xa5, 0xb4, 0x93, 0x23,
  37344. 0x06, 0x59, 0x43, 0xa8, 0xe8, 0x5c, 0xfc, 0x18, 0x97, 0xdb,
  37345. 0xad, 0x9a, 0x80, 0x0a, 0xf2, 0x20, 0x50, 0xac, 0xc1, 0x13,
  37346. 0x3e, 0x98, 0x09, 0xde, 0xf2, 0x70, 0x9e, 0x14, 0xc2, 0x5c,
  37347. 0xec, 0x65, 0x07, 0x0b, 0xfa, 0x02, 0x5c, 0xf8, 0x71, 0xaa,
  37348. 0x9b, 0x45, 0x62, 0xe2, 0x27, 0xaf, 0x77, 0xf8, 0xe3, 0xeb,
  37349. 0x7b, 0x24, 0x7b, 0x3c, 0x67, 0xc2, 0x6d, 0x6e, 0x17, 0xae,
  37350. 0x6e, 0x86, 0x6f, 0x98, 0xc9, 0xac, 0x13, 0x9f, 0x87, 0x64,
  37351. 0x3d, 0x4d, 0x6f, 0xa0, 0xb3, 0x39, 0xc6, 0x68, 0x1b, 0xa7,
  37352. 0xeb, 0x3e, 0x0f, 0x6b, 0xc7, 0xa4, 0xe2, 0x20, 0x27, 0x75,
  37353. 0x3f, 0x09, 0x16, 0xff, 0x1a, 0xcc, 0xa7, 0xc4, 0x6d, 0xc2,
  37354. 0xfc, 0xc3, 0x0b, 0x37, 0x63, 0xff, 0x9b, 0x10, 0xe6, 0x00,
  37355. 0xf7, 0x18, 0x43, 0x9f, 0x07, 0x50, 0x31, 0x51, 0xd4, 0xfd,
  37356. 0xad, 0xa2, 0x0f, 0x77, 0xda, 0x41, 0xc1, 0x0a, 0x6f, 0x86,
  37357. 0xd7, 0xdc, 0x8a, 0x52, 0xd6, 0xa1, 0x27, 0xdb, 0x14, 0x67,
  37358. 0x26, 0x91, 0xb3, 0xcd, 0x01, 0x5f, 0x60, 0xa1, 0x7f, 0x43,
  37359. 0x15, 0x1a, 0x82, 0x0f, 0xd3, 0x66, 0x5f, 0x60, 0x57, 0x2f,
  37360. 0xb2, 0x8c, 0x27, 0x2a, 0x9d, 0x1b, 0xf9, 0xf2, 0x59, 0x20,
  37361. 0x39, 0xd9, 0xc5, 0xaf, 0xf2, 0x36, 0x8c, 0x58, 0x00, 0x1b,
  37362. 0xd0, 0xc5, 0x8e, 0x1a, 0x49, 0xa8, 0x60, 0xbe, 0xd1, 0xd7,
  37363. 0x2a, 0xb0, 0xc2, 0xab, 0x58, 0x8a, 0x7a, 0xa9, 0x41, 0x68,
  37364. 0x70, 0xbd, 0xea, 0x73, 0xa5, 0x03, 0x11, 0xb2, 0x27, 0xd9,
  37365. 0xcd, 0xf5, 0x09, 0xe8, 0x1c, 0xe2, 0x4f, 0x50, 0x6a, 0x84,
  37366. 0x34, 0x62, 0x2e, 0x36, 0xaa, 0x4c, 0xc1, 0x83, 0x78, 0x98,
  37367. 0x35, 0x7a, 0x27, 0x7e, 0xfe, 0xf1, 0x6f, 0x59, 0x27, 0x35,
  37368. 0x73, 0xce, 0x74, 0xaa, 0xb4, 0x72, 0x82, 0xa8, 0xe2, 0x81,
  37369. 0x7a, 0x6b, 0xca, 0x33, 0xa5, 0xda, 0xa2, 0x63, 0xca, 0x2e,
  37370. 0x90, 0x03, 0x32, 0xec, 0x63, 0xdb, 0x52, 0x7b, 0x16, 0xfc,
  37371. 0x01, 0x2d, 0x30, 0x12, 0x1e, 0xf9, 0xa3, 0x72, 0x21, 0x3c,
  37372. 0x75, 0x0c, 0x61, 0x9c, 0x7e, 0x73, 0x04, 0x71, 0x41, 0x45,
  37373. 0x5d, 0x7f, 0x49, 0x1c, 0x09, 0x08, 0xa4, 0xec, 0x2f, 0xfd,
  37374. 0xc4, 0xfb, 0x59, 0x6a, 0x27, 0x7a, 0xd4, 0xfc, 0x5f, 0x20,
  37375. 0x04, 0x34, 0x7d, 0x08, 0xed, 0x82, 0x5a, 0x90, 0xe1, 0xab,
  37376. 0xfd, 0x35, 0x3a, 0x8d, 0xbb, 0x0a, 0x9d, 0x73, 0xff, 0x69,
  37377. 0xe5, 0xe9, 0x09, 0x55, 0x14, 0xd9, 0x7b, 0x6f, 0x0d, 0x99,
  37378. 0xd2, 0x7e, 0x71, 0xf8, 0x4f, 0x72, 0x2f, 0xbb, 0xc6, 0xc4,
  37379. 0x36, 0xc9, 0x01, 0xd3, 0x9b, 0x94, 0xab, 0x41, 0x0f, 0x4a,
  37380. 0x61, 0x5c, 0x68, 0xe5, 0xd7, 0x0d, 0x94, 0xaa, 0xee, 0xba,
  37381. 0x95, 0xcb, 0x8c, 0x0e, 0x85, 0x3a, 0x02, 0x6b, 0x95, 0x50,
  37382. 0xfd, 0x02, 0xfd, 0xa4, 0x58, 0x29, 0x78, 0x4f, 0xd0, 0xae,
  37383. 0x66, 0xd6, 0x5c, 0xe7, 0x45, 0xfe, 0x98, 0xb0, 0xa3, 0xe2,
  37384. 0x87, 0xc0, 0xd2, 0x81, 0x08, 0xf1, 0xf1, 0xe7, 0xda, 0x62,
  37385. 0x9e, 0xa0, 0x34, 0x86, 0xeb, 0xa1, 0x6e, 0x4a, 0x26, 0x8e,
  37386. 0x39, 0x0c, 0x51, 0x10, 0x33, 0x11, 0x87, 0xf8, 0x79, 0x3c,
  37387. 0x49, 0x7a, 0x8b, 0xce, 0xc1, 0x0a, 0x0e, 0xe1, 0xd5, 0x2a,
  37388. 0xac, 0xf0, 0x3a, 0x1d, 0x6a, 0x6a, 0xe5, 0xe1, 0x81, 0x70,
  37389. 0xad, 0xaf, 0x15, 0x4c, 0x2a, 0x70, 0x2a, 0x6b, 0x22, 0x0d,
  37390. 0x30, 0xe7, 0x56, 0xed, 0x2d, 0x4b, 0x85, 0x17, 0x49, 0x72,
  37391. 0x3a, 0x1b, 0x6f, 0x57, 0x1c, 0xf7, 0x72, 0x9e, 0x20, 0xdb,
  37392. 0x57, 0x1c, 0xfb, 0x36, 0x50, 0x52, 0xec, 0x5b, 0xd6, 0x6a,
  37393. 0x1b, 0xf8, 0x74, 0xad, 0xe6, 0x00, 0x74, 0x04, 0xc5, 0x99,
  37394. 0x83, 0xe4, 0x5a, 0x0c, 0xc3, 0xe8, 0x6d, 0x3a, 0xd7, 0x3c,
  37395. 0x3c, 0xc0, 0x1a, 0x28, 0xb3, 0x29, 0x7a, 0x10, 0x9e, 0x39,
  37396. 0x66, 0x5b, 0xc1, 0x38, 0xac, 0x21, 0x4e, 0xcd, 0x01, 0xf2,
  37397. 0xf6, 0x30, 0x2c, 0x2b, 0xb6, 0xbf, 0xf5, 0xea, 0x61, 0xaf,
  37398. 0x0c, 0xa6, 0x01, 0x11, 0x15, 0x19, 0x09, 0x8c, 0x7e, 0x69,
  37399. 0xdf, 0x3b, 0xea, 0xd3, 0x0a, 0x3a, 0xd7, 0xbd, 0xe1, 0x17,
  37400. 0xaf, 0x92, 0x3c, 0xf5, 0xfe, 0x35, 0xd6, 0xcf, 0x07, 0xa6,
  37401. 0xf7, 0xe9, 0xc1, 0x99, 0xed, 0x80, 0xe3, 0x12, 0xd5, 0x4b,
  37402. 0xb9, 0xdf, 0xaf, 0x4e, 0x52, 0xad, 0x8e, 0x66, 0x87, 0xe5,
  37403. 0x2c, 0xd0, 0x45, 0x70, 0xd9, 0x78, 0x8f, 0x4b, 0xf4, 0xe1,
  37404. 0xf1, 0x22, 0xf2, 0xe3, 0xed, 0x1f, 0xeb, 0xe9, 0x70, 0x31,
  37405. 0x4c, 0x65, 0x5f, 0x55, 0xee, 0x5d, 0xaa, 0x83, 0x87, 0x76,
  37406. 0xbe, 0x11, 0xae, 0xd7, 0xf2, 0xfb, 0x43, 0xe7, 0x17, 0x81,
  37407. 0x33, 0x15, 0x47, 0xa0, 0xf3, 0x8e, 0x84, 0x57, 0xff, 0x35,
  37408. 0x9e, 0x4a, 0x8a, 0xab, 0x50, 0x3a, 0x45, 0xe0, 0xc3, 0x73,
  37409. 0xca, 0x77, 0x61, 0x68, 0x38, 0xd0, 0xa3, 0x5f, 0x03, 0x8d,
  37410. 0x41, 0xc2, 0xd3, 0x4a, 0x17, 0xe0, 0xa8, 0xaa, 0x00, 0xf3,
  37411. 0xf2, 0x5b, 0xa8, 0xe1, 0x06, 0xa6, 0x2b, 0xdb, 0xe1, 0x74,
  37412. 0xbd, 0xc4, 0xd2, 0x2b, 0x55, 0x9a, 0xb0, 0xf8, 0x35, 0xd8,
  37413. 0x6b, 0xec, 0xdb, 0xc5, 0xf4, 0x6c, 0x40, 0x90, 0x6a, 0x68,
  37414. 0xc9, 0xb5, 0xcb, 0xbb, 0xd0, 0xb0, 0xbc, 0x9f, 0xb9, 0xaa,
  37415. 0x50, 0x14, 0x93, 0x3b, 0x9f, 0x25, 0xcb, 0x40, 0xb8, 0x08,
  37416. 0xcc, 0x13, 0xe5, 0xdc, 0x3f, 0x84, 0x96, 0xe0, 0x73, 0x7b,
  37417. 0x7d, 0x9e, 0x41, 0x92, 0x5d, 0xcc, 0xa4, 0xea, 0x4f, 0x93,
  37418. 0x0c, 0x40, 0x2e, 0x42, 0x8a, 0xe9, 0xb9, 0x12, 0x74, 0xbb,
  37419. 0x79, 0x7c, 0xb0, 0x37, 0x20, 0xb6, 0xaf, 0x43, 0x3a, 0x88,
  37420. 0x59, 0x7c, 0x68, 0x28, 0x5f, 0x98, 0xc2, 0xf0, 0x2a, 0xbc,
  37421. 0xa1, 0x61, 0x88, 0x1f, 0x43, 0xbc, 0x42, 0x8f, 0x43, 0xf3,
  37422. 0x7e, 0x16, 0x96, 0xfa, 0x92, 0x70, 0xaf, 0x3c, 0x9f, 0x4b,
  37423. 0xd9, 0x60, 0xe9, 0xf6, 0x2e, 0x84, 0xda, 0x88, 0x31, 0x34,
  37424. 0xa6, 0x85, 0x10, 0x05, 0xef, 0x40, 0xa8, 0xa5, 0x4f, 0x92,
  37425. 0x59, 0xf7, 0xe0, 0xc4, 0x2b, 0x12, 0x17, 0x71, 0xbe, 0x8c,
  37426. 0x4a, 0x02, 0xfe, 0x12, 0xb6, 0x3b, 0x85, 0x75, 0x37, 0xf3,
  37427. 0x73, 0x2d, 0x9c, 0x00, 0x5d, 0x80, 0xad, 0x20, 0x2f, 0x5a,
  37428. 0x0b, 0x17, 0x7e, 0x67, 0x72, 0x24, 0x5a, 0xb9, 0xf3, 0xb1,
  37429. 0x33, 0xa4, 0x57, 0x1d, 0x49, 0x72, 0x2c, 0x7f, 0x47, 0x15,
  37430. 0x07, 0xe0, 0x45, 0x14, 0xdd, 0x77, 0x86, 0x6d, 0x03, 0xbe,
  37431. 0x57, 0xd0, 0xaa, 0x18, 0xa6, 0xdd, 0x94, 0x18, 0x3f, 0x8a,
  37432. 0xf3, 0xb5, 0xd7, 0x5a, 0xec, 0xc8, 0x79, 0x7f, 0x51, 0x61,
  37433. 0x3c, 0x9b, 0xb2, 0x9b, 0xf3, 0xb4, 0x35, 0xd1, 0x38, 0xbf,
  37434. 0x37, 0xce, 0x54, 0xd1, 0xf8, 0xb6, 0x45, 0xeb, 0x52, 0x0d,
  37435. 0x9a, 0x09, 0x58, 0x0d, 0x2c, 0x0b, 0xb1, 0xf2, 0x30, 0x3a,
  37436. 0x95, 0xc1, 0x13, 0x91, 0xd2, 0x9f, 0x8d, 0x8d, 0xd0, 0x38,
  37437. 0x3e, 0x4c, 0xae, 0x4a, 0x55, 0xa7, 0x42, 0x11, 0x83, 0xc4,
  37438. 0x70, 0xf0, 0x2b, 0x68, 0x9e, 0x07, 0xad, 0xb7, 0x83, 0xc6,
  37439. 0x53, 0x3c, 0xfb, 0x0a, 0x5d, 0x24, 0xdc, 0xe1, 0x55, 0x72,
  37440. 0xcf, 0xce, 0x3e, 0xc8, 0xd0, 0x57, 0x8a, 0x82, 0x5e, 0x78,
  37441. 0x2b, 0x80, 0xc5, 0xb9, 0x09, 0x46, 0xf8, 0x90, 0x39, 0x52,
  37442. 0xa9, 0xce, 0x3f, 0x3d, 0x41, 0x3b, 0x28, 0x45, 0xa3, 0xb3,
  37443. 0x21, 0xc2, 0xcd, 0x14, 0x49, 0x41, 0x6c, 0x38, 0xda, 0x1b,
  37444. 0x5f, 0x16, 0x49, 0xf9, 0x65, 0x00, 0x4e, 0xb4, 0x20, 0x55,
  37445. 0x70, 0xe8, 0x58, 0x1a, 0x18, 0xbf, 0x41, 0xef, 0x31, 0xb1,
  37446. 0xe7, 0x8d, 0x89, 0xc1, 0x48, 0xe8, 0xf5, 0x57, 0x35, 0xfa,
  37447. 0xc1, 0x79, 0xee, 0x2c, 0xe8, 0x7d, 0xb6, 0x03, 0xcc, 0x66,
  37448. 0x09, 0x6f, 0x52, 0x84, 0x0a, 0x34, 0x18, 0x2c, 0x01, 0x45,
  37449. 0x81, 0x00, 0xe5, 0x5e, 0x8d, 0xae, 0x1c, 0x96, 0x8b, 0x45,
  37450. 0x73, 0x00, 0x0a, 0xb5, 0xcf, 0x8d, 0x0e, 0x35, 0x5d, 0x1a,
  37451. 0x0e, 0xbf, 0x64, 0x9a, 0x52, 0x20, 0x48, 0xc6, 0xb9, 0x40,
  37452. 0xd3, 0x2c, 0x52, 0xca, 0x93, 0xcf, 0xbb, 0x94, 0x06, 0xf3,
  37453. 0x97, 0xee, 0xcc, 0x5d, 0xa3, 0xea, 0xf8, 0x5a, 0x39, 0x77,
  37454. 0x34, 0xd7, 0xf6, 0x4e, 0xbe, 0x8a, 0x07, 0x5f, 0x51, 0x53,
  37455. 0xc5, 0x1b, 0x8c, 0x47, 0x8f, 0x34, 0x0e, 0x60, 0x0a, 0x90,
  37456. 0xe2, 0xda, 0x7b, 0xef, 0xd6, 0xf5, 0x5d, 0xe5, 0x32, 0x37,
  37457. 0x75, 0x99, 0x81, 0x4a, 0x2a, 0x78, 0x71, 0xdc, 0xf4, 0xe5,
  37458. 0xca, 0xd8, 0x6b, 0x3b, 0x90, 0x68, 0x2e, 0x93, 0xc5, 0x10,
  37459. 0x42, 0x5d, 0x38, 0x90, 0x32, 0x46, 0xea, 0x87, 0xe0, 0xbc,
  37460. 0xb8, 0x9a, 0x18, 0x20, 0x68, 0x85, 0x6d, 0x9b, 0xc9, 0x8f,
  37461. 0x9b, 0xd2, 0xbe, 0x15, 0x12, 0x68, 0xd0, 0xb0, 0x16, 0x5f,
  37462. 0xe2, 0x69, 0x1d, 0x04, 0x00, 0xfc, 0x63, 0x33, 0xcd, 0x1f,
  37463. 0x89, 0xcd, 0x52, 0xff, 0xec, 0x19, 0x69, 0x74, 0xa3, 0xce,
  37464. 0x4d, 0xab, 0x93, 0xe4, 0xc6, 0x13, 0x56, 0x27, 0xc9, 0x25,
  37465. 0x5a, 0x01, 0xb2, 0x36, 0x8b, 0x61, 0xe5, 0x8b, 0x98, 0xac,
  37466. 0xe4, 0x2a, 0xb6, 0x40, 0x9f, 0x42, 0xe4, 0x1b, 0x52, 0xf7,
  37467. 0xfd, 0xd8, 0x30, 0x07, 0x33, 0xf9, 0x47, 0xcb, 0x3c, 0xad,
  37468. 0x12, 0xc1, 0xcc, 0x29, 0x62, 0x49, 0x04, 0x0c, 0x23, 0x97,
  37469. 0x5a, 0xa4, 0x84, 0x67, 0xde, 0x5a, 0xe5, 0x36, 0xd2, 0x88,
  37470. 0xf1, 0xd4, 0xeb, 0x13, 0x81, 0x54, 0x51, 0x11, 0xe3, 0xba,
  37471. 0xbc, 0xee, 0xdd, 0x6c, 0xcd, 0xe6, 0xb4, 0xa1, 0x8b, 0x0b,
  37472. 0x66, 0xfb, 0x8e, 0x50, 0xa0, 0xda, 0x69, 0x8d, 0xcc, 0x2d,
  37473. 0xe4, 0x2c, 0xc4, 0x37, 0xdf, 0x61, 0xc0, 0x03, 0xbd, 0x8b,
  37474. 0x28, 0xca, 0xd2, 0x8c, 0x1c, 0xf1, 0xa4, 0x26, 0x69, 0xe5,
  37475. 0xcf, 0x45, 0xdb, 0x5a, 0x47, 0x79, 0xed, 0x9f, 0xf7, 0xd2,
  37476. 0xdb, 0xba, 0x46, 0x53, 0x4f, 0xce, 0xa8, 0xbe, 0x8f, 0x4a,
  37477. 0xd6, 0xdf, 0x2e, 0x06, 0xe6, 0x4c, 0x9a, 0xc1, 0xb6, 0x49,
  37478. 0xed, 0xc4, 0xeb, 0xaa, 0xa4, 0x29, 0x6d, 0xd4, 0xcc, 0x8c,
  37479. 0xb6, 0x40, 0x11, 0x39, 0x69, 0xf7, 0x75, 0xcd, 0xb1, 0x99,
  37480. 0x46, 0x4e, 0xde, 0xcb, 0xf6, 0x9d, 0x32, 0xf3, 0xc9, 0x47,
  37481. 0x47, 0x7a, 0xcb, 0xfb, 0xa3, 0x0c, 0x3b, 0xdf, 0xb7, 0xde,
  37482. 0xec, 0x99, 0xde, 0xb0, 0x26, 0x04, 0x34, 0xae, 0x6b, 0xfc,
  37483. 0x99, 0xbc, 0xde, 0xd5, 0xbe, 0xe7, 0xeb, 0xf9, 0xe7, 0xa6,
  37484. 0x01, 0x9a, 0x0c, 0x5e, 0x66, 0xe6, 0x53, 0xe4, 0xd1, 0x58,
  37485. 0xac, 0xda, 0x69, 0x77, 0x7b, 0x68, 0xd6, 0x30, 0x2a, 0x9c,
  37486. 0x6b, 0xbe, 0x9f, 0x3d, 0x71, 0xd6, 0x54, 0xcd, 0x59, 0x4e,
  37487. 0x1f, 0xe3, 0x83, 0x4e, 0xd1, 0x8e, 0xaf, 0x97, 0xa8, 0xe5,
  37488. 0xb6, 0x59, 0x77, 0xa8, 0x02, 0x20, 0xe4, 0xeb, 0x44, 0x71,
  37489. 0xbc, 0x07, 0x14, 0x79, 0x4f, 0x0c, 0x27, 0x06, 0x39, 0xcf,
  37490. 0x7c, 0xef, 0x2b, 0x9b, 0x5e, 0xc4, 0x6d, 0x79, 0x13, 0x00,
  37491. 0x43, 0x6f, 0x51, 0x77, 0xb5, 0xc3, 0x72, 0xad, 0x13, 0xa9,
  37492. 0xe5, 0x9a, 0x5b, 0x1a, 0x99, 0x74, 0xc0, 0x7a, 0xf9, 0xc5,
  37493. 0xb0, 0x58, 0x35, 0x1c, 0xa5, 0x51, 0xdb, 0xa1, 0x14, 0xcd,
  37494. 0x26, 0x71, 0xb1, 0xe7, 0xaa, 0x14, 0xa7, 0x46, 0x93, 0xd3,
  37495. 0x5c, 0x8c, 0x1a, 0x91, 0x77, 0x46, 0x2e, 0x15, 0xaa, 0x9e,
  37496. 0xf7, 0x2b, 0x79, 0x41, 0x76, 0xf7, 0x22, 0x53, 0x7d, 0x51,
  37497. 0xdb, 0x98, 0x3d, 0x5b, 0x78, 0x5f, 0xc3, 0xc9, 0x29, 0xa3,
  37498. 0xff, 0x75, 0x82, 0x06, 0x9a, 0x16, 0x5e, 0xa4, 0x79, 0x0d,
  37499. 0xd1, 0x6d, 0x08, 0xff, 0x43, 0xef, 0x9c, 0xf3, 0x1b, 0x7a,
  37500. 0x3f, 0x34, 0xbe, 0x19, 0x15, 0x06, 0x33, 0xdb, 0xa5, 0x71,
  37501. 0xcb, 0x5f, 0x6b, 0x8d, 0xbd, 0x5b, 0x32, 0x91, 0xb2, 0x37,
  37502. 0x3d, 0xb4, 0x40, 0x9e, 0x02, 0x9b, 0xb7, 0x68, 0x20, 0x58,
  37503. 0x5c, 0xab, 0xcb, 0xc8, 0x23, 0x2d, 0x77, 0xcc, 0x0b, 0xf6,
  37504. 0x78, 0x6b, 0x80, 0x06, 0x91, 0xa9, 0xfd, 0x7e, 0xfa, 0x25,
  37505. 0x98, 0x9f, 0xcc, 0x79, 0x0a, 0x1a, 0x54, 0x83, 0xac, 0x64,
  37506. 0x16, 0x90, 0xe5, 0xd9, 0xa7, 0xd7, 0x1b, 0x86, 0x0d, 0xe6,
  37507. 0xe6, 0x22, 0x2b, 0x1f, 0x44, 0x49, 0x98, 0x9c, 0x51, 0x6f,
  37508. 0xcf, 0x58, 0x4a, 0xfa, 0xfa, 0x84, 0x12, 0xa5, 0x10, 0xf4,
  37509. 0xca, 0xf0, 0x98, 0x2b, 0xc9, 0x03, 0x71, 0x37, 0xe7, 0xdc,
  37510. 0xc2, 0xb1, 0x4e, 0x64, 0xde, 0x4f, 0x46, 0x0d, 0x6b, 0x25,
  37511. 0x88, 0x5d, 0xd6, 0xff, 0x23, 0x46, 0x57, 0x36, 0x14, 0x18,
  37512. 0xa7, 0xcb, 0xb8, 0xbd, 0xf0, 0xc5, 0x37, 0x36, 0xee, 0xe1,
  37513. 0xed, 0x9f, 0x4d, 0xd4, 0x39, 0xe5, 0x92, 0xcf, 0x95, 0x4d,
  37514. 0x66, 0x36, 0x5d, 0xd0, 0xcc, 0x07, 0xcf, 0x15, 0x5a, 0xce,
  37515. 0x14, 0xb8, 0xda, 0x0d, 0x3d, 0x1b, 0x45, 0xc5, 0x2e, 0x34,
  37516. 0x43, 0x25, 0x02, 0x3a, 0xcd, 0x14, 0x45, 0xfb, 0x3e, 0xf9,
  37517. 0x88, 0x5d, 0x0d, 0x29, 0x31, 0xb9, 0xa1, 0xe6, 0x31, 0x18,
  37518. 0x52, 0x46, 0x3f, 0x22, 0x4f, 0x9f, 0x7a, 0x65, 0x36, 0x88,
  37519. 0xa3, 0x1c, 0x3e, 0x6f, 0x50, 0x7a, 0x36, 0xbe, 0x56, 0x7e,
  37520. 0x50, 0xcb, 0x7a, 0x10, 0xa0, 0xec, 0xf6, 0x82, 0xd6, 0x30,
  37521. 0x1c, 0xe8, 0x4c, 0x50, 0xf9, 0x3e, 0xdb, 0xac, 0xbe, 0x4f,
  37522. 0x90, 0xb1, 0xd5, 0x1b, 0x12, 0x95, 0xfb, 0xe8, 0x08, 0x64,
  37523. 0x56, 0x7c, 0x96, 0xcc, 0x90, 0xb1, 0xbc, 0xa0, 0xf5, 0x32,
  37524. 0x69, 0xb3, 0x5f, 0x27, 0x0f, 0xbe, 0xc9, 0xbd, 0xeb, 0xfa,
  37525. 0x4b, 0x5c, 0xc5, 0x99, 0x9e, 0x5a, 0x04, 0xcc, 0xd0, 0x4d,
  37526. 0x29, 0xe8, 0x84, 0x55, 0x8c, 0xd7, 0xc4, 0x06, 0x13, 0x4d,
  37527. 0x92, 0xe5, 0x98, 0x9c, 0x4c, 0xc1, 0xf7, 0xaf, 0x7b, 0xd5,
  37528. 0x2b, 0x92, 0x68, 0x68, 0x19, 0x70, 0x4c, 0x9e, 0x46, 0xb8,
  37529. 0x34, 0xeb, 0x01, 0x47, 0xbe, 0x59, 0xab, 0x0b, 0x22, 0x25,
  37530. 0xe7, 0x56, 0xa8, 0xb4, 0x93, 0x3c, 0xd5, 0x98, 0x9f, 0x61,
  37531. 0x2e, 0xfa, 0xcb, 0x5f, 0x5b, 0xd8, 0x09, 0x83, 0xe9, 0x40,
  37532. 0xe9, 0x0e, 0x42, 0xdd, 0x17, 0xd7, 0x6e, 0x19, 0x8d, 0x95,
  37533. 0x0a, 0x93,
  37534. };
  37535. WOLFSSL_SMALL_STACK_STATIC const byte ml_dsa_87_sig[] = {
  37536. 0x78, 0xed, 0x1a, 0x3f, 0x41, 0xab, 0xf8, 0x93, 0x80, 0xf0,
  37537. 0xc6, 0xbf, 0x4a, 0xde, 0xaf, 0x29, 0x93, 0xe5, 0x9a, 0xbf,
  37538. 0x38, 0x08, 0x18, 0x33, 0xca, 0x7d, 0x5e, 0x65, 0xa4, 0xd2,
  37539. 0xd7, 0x45, 0xe3, 0xe7, 0x58, 0xfb, 0x05, 0xab, 0x65, 0x57,
  37540. 0xac, 0x6f, 0xf5, 0x43, 0x28, 0x5f, 0x9c, 0x9a, 0x3e, 0x35,
  37541. 0x84, 0xe4, 0xef, 0xa5, 0x57, 0x17, 0xad, 0x51, 0x44, 0x70,
  37542. 0x09, 0x00, 0x81, 0xbe, 0xfe, 0x14, 0x01, 0xfe, 0x0c, 0x94,
  37543. 0xbe, 0xa9, 0x89, 0xfd, 0x47, 0xfc, 0xb9, 0xd8, 0x17, 0x4d,
  37544. 0xd8, 0x73, 0xd5, 0x50, 0x9f, 0x13, 0x6c, 0x07, 0x71, 0x47,
  37545. 0xaa, 0x3c, 0xc0, 0x64, 0x00, 0x19, 0x2e, 0x74, 0x51, 0x0e,
  37546. 0x0f, 0x25, 0x30, 0x7f, 0x13, 0x96, 0xc6, 0xc5, 0xbf, 0xd4,
  37547. 0x82, 0xd3, 0x0d, 0xd3, 0x65, 0x4c, 0x72, 0x67, 0xe2, 0x37,
  37548. 0x6b, 0x3c, 0x8e, 0xa3, 0x36, 0x84, 0xe9, 0xaa, 0xac, 0x7d,
  37549. 0xf3, 0xac, 0xfc, 0x01, 0x50, 0x87, 0x88, 0xf6, 0xbf, 0x84,
  37550. 0xc3, 0xa0, 0x23, 0xe4, 0xe8, 0x01, 0x38, 0x39, 0x30, 0x8a,
  37551. 0xf3, 0xba, 0x92, 0x62, 0x37, 0xd7, 0x20, 0xd7, 0xf7, 0x41,
  37552. 0xff, 0xae, 0x81, 0x02, 0x29, 0x2a, 0x66, 0x8b, 0x20, 0xbe,
  37553. 0x61, 0x8d, 0xfb, 0x7c, 0x70, 0x14, 0xad, 0xf4, 0x94, 0x8c,
  37554. 0xee, 0x64, 0x3b, 0x9f, 0xe1, 0x6e, 0x68, 0x17, 0x07, 0xb8,
  37555. 0xfc, 0x99, 0xdc, 0xde, 0x69, 0x58, 0x8c, 0x97, 0x7d, 0xb3,
  37556. 0x2c, 0x9e, 0x90, 0x33, 0x2e, 0x7b, 0xbf, 0xf8, 0x6f, 0xf8,
  37557. 0x12, 0x64, 0xda, 0xc0, 0xfb, 0x30, 0xe6, 0xbf, 0x7b, 0x9a,
  37558. 0xde, 0xb5, 0xac, 0x9d, 0x6b, 0xcb, 0xe1, 0x0d, 0xf1, 0xbb,
  37559. 0xf3, 0x97, 0xc5, 0x08, 0xd3, 0x3e, 0xe3, 0xa4, 0xeb, 0x6f,
  37560. 0x6b, 0x62, 0x61, 0xc5, 0x0b, 0xa8, 0x02, 0xc2, 0xf1, 0xbe,
  37561. 0xbb, 0x93, 0x13, 0xa5, 0x8d, 0x7b, 0x5a, 0x6d, 0x1f, 0x28,
  37562. 0xbc, 0x35, 0xd8, 0xe8, 0xcf, 0x80, 0x8b, 0x4b, 0x02, 0x80,
  37563. 0x3b, 0xdc, 0x00, 0xce, 0x88, 0xb0, 0x62, 0x35, 0x7d, 0x51,
  37564. 0x7f, 0x5c, 0xb2, 0x23, 0x85, 0x47, 0x7e, 0x73, 0x88, 0x65,
  37565. 0xfd, 0x0d, 0x47, 0x33, 0xef, 0xb9, 0x75, 0x05, 0x86, 0x5d,
  37566. 0xd3, 0x98, 0xa6, 0x91, 0xe6, 0x8c, 0xe2, 0x71, 0x7a, 0x95,
  37567. 0xe0, 0x8c, 0x54, 0x4b, 0x68, 0x4d, 0x5a, 0xec, 0xad, 0xae,
  37568. 0x54, 0x4e, 0x3b, 0x0e, 0xcd, 0x70, 0xe6, 0x81, 0xbf, 0xf4,
  37569. 0x86, 0xab, 0xfe, 0xd8, 0xed, 0x69, 0xdd, 0x0f, 0x75, 0x8f,
  37570. 0x8e, 0xcd, 0x72, 0x40, 0x21, 0xee, 0x80, 0x6f, 0x9e, 0xa0,
  37571. 0x80, 0xf7, 0xf6, 0xa2, 0xf5, 0x04, 0x82, 0xea, 0xb6, 0xb1,
  37572. 0xa3, 0xfe, 0xa2, 0x2d, 0x83, 0xc7, 0x01, 0x4b, 0x27, 0x19,
  37573. 0x6a, 0x31, 0x04, 0x70, 0xce, 0x75, 0x22, 0x4b, 0x7a, 0x21,
  37574. 0x29, 0xfd, 0xe9, 0xcb, 0xbb, 0xca, 0x95, 0x0a, 0xd8, 0xcd,
  37575. 0x20, 0x2a, 0xb7, 0xbe, 0xdf, 0x2f, 0x0f, 0xfa, 0xf1, 0xc0,
  37576. 0x39, 0xf3, 0x74, 0x22, 0x05, 0x33, 0xca, 0x2a, 0x9c, 0x9f,
  37577. 0x06, 0x71, 0x90, 0x1e, 0x74, 0x4b, 0xbe, 0x9a, 0xc7, 0x1e,
  37578. 0x37, 0x9b, 0x96, 0x19, 0xfd, 0xa0, 0x61, 0x87, 0x93, 0xab,
  37579. 0x75, 0x79, 0xac, 0x2f, 0x83, 0xe1, 0x8c, 0x70, 0x54, 0x70,
  37580. 0x01, 0x93, 0xce, 0x76, 0x7a, 0x08, 0xe7, 0x75, 0xfb, 0x5e,
  37581. 0xa4, 0xcc, 0xd6, 0xeb, 0x90, 0xe2, 0x57, 0x07, 0x53, 0x88,
  37582. 0x8f, 0x7f, 0x29, 0x39, 0x80, 0xc4, 0x7f, 0x70, 0x6f, 0xff,
  37583. 0x44, 0x25, 0x2b, 0x9e, 0xa1, 0xbb, 0xda, 0x43, 0x53, 0x14,
  37584. 0xf8, 0x97, 0x08, 0xa4, 0xaf, 0xa0, 0xa5, 0x0c, 0xfa, 0xcc,
  37585. 0xba, 0xcd, 0x4f, 0xd3, 0x90, 0x28, 0x02, 0x25, 0xbe, 0xc6,
  37586. 0x35, 0x66, 0x99, 0xb0, 0x69, 0x46, 0xe5, 0xbf, 0x7e, 0x4f,
  37587. 0x53, 0x11, 0x1f, 0xa5, 0x2c, 0x9b, 0xd1, 0x70, 0x90, 0x34,
  37588. 0x66, 0xaa, 0x9f, 0xa8, 0x02, 0x3a, 0x05, 0x2b, 0x0a, 0xd0,
  37589. 0x72, 0x5d, 0x01, 0x7b, 0x02, 0xce, 0x18, 0xb9, 0x63, 0xd1,
  37590. 0x7d, 0xd2, 0x34, 0xa3, 0x2d, 0xaa, 0x78, 0xf0, 0x30, 0x6e,
  37591. 0x59, 0xe3, 0xf1, 0x1e, 0xf1, 0x33, 0x41, 0xde, 0xc4, 0x4e,
  37592. 0x88, 0x61, 0xc3, 0xb4, 0x6b, 0x21, 0x5d, 0xcc, 0x69, 0x44,
  37593. 0xf3, 0xb0, 0x84, 0x54, 0x2a, 0x23, 0x22, 0xa2, 0xc4, 0xba,
  37594. 0xad, 0x00, 0x57, 0x5b, 0xdf, 0xa0, 0xf7, 0x1c, 0x00, 0xc3,
  37595. 0x23, 0x93, 0xc0, 0x2f, 0x3b, 0x9d, 0x6e, 0x8c, 0x38, 0xa6,
  37596. 0x5e, 0xd8, 0x98, 0x7a, 0x6c, 0x90, 0xd5, 0x40, 0x3f, 0x8c,
  37597. 0xc3, 0xf0, 0x92, 0x66, 0xc4, 0xe5, 0xa8, 0x42, 0x25, 0x4c,
  37598. 0x56, 0x42, 0x37, 0x9a, 0xa4, 0x1d, 0xf5, 0xb0, 0xe3, 0x8a,
  37599. 0x9c, 0x57, 0x52, 0x63, 0xdc, 0xd9, 0xb0, 0xbf, 0xc3, 0xfc,
  37600. 0xfc, 0x6c, 0xab, 0x41, 0xae, 0xec, 0xc7, 0x40, 0x80, 0xb6,
  37601. 0x0b, 0x3c, 0xa9, 0xf5, 0x4f, 0x2d, 0xf6, 0x72, 0xe3, 0xba,
  37602. 0x13, 0x2c, 0x73, 0x61, 0x98, 0x66, 0x6f, 0x03, 0x88, 0x3b,
  37603. 0xe6, 0x95, 0x43, 0x33, 0x3b, 0xfe, 0xfd, 0x63, 0x8c, 0x00,
  37604. 0x8a, 0x67, 0x1c, 0x46, 0x0e, 0x0b, 0x51, 0x26, 0x79, 0x4f,
  37605. 0x7b, 0xb1, 0x36, 0x34, 0x52, 0x41, 0x7e, 0x74, 0xbb, 0x71,
  37606. 0x52, 0x8f, 0xcc, 0xf2, 0x99, 0x24, 0x3f, 0x18, 0xe6, 0xcf,
  37607. 0xdf, 0x6b, 0xfe, 0x77, 0xfa, 0xa8, 0x3f, 0xe3, 0x6b, 0xb7,
  37608. 0x32, 0x30, 0x8e, 0x16, 0x08, 0x59, 0x66, 0xdf, 0x95, 0x75,
  37609. 0x7d, 0xa3, 0x80, 0xf0, 0x0c, 0x1a, 0xa8, 0xe7, 0x87, 0x2f,
  37610. 0xe3, 0x39, 0x11, 0x82, 0x00, 0x3e, 0xe5, 0x71, 0x05, 0x7d,
  37611. 0x0c, 0x90, 0xae, 0xbc, 0xbf, 0xe0, 0x4b, 0x8f, 0x91, 0x85,
  37612. 0x1d, 0x0a, 0xa2, 0x36, 0x66, 0x18, 0x78, 0xd0, 0x0a, 0xa0,
  37613. 0xaf, 0x0f, 0x1c, 0x01, 0xdb, 0xb2, 0x21, 0x96, 0x25, 0xf7,
  37614. 0x9e, 0x3a, 0x9e, 0xc3, 0xe8, 0x92, 0x34, 0xaf, 0x7e, 0x3b,
  37615. 0x5f, 0xd9, 0x23, 0x97, 0x09, 0xf1, 0x87, 0x31, 0x3a, 0x94,
  37616. 0xc8, 0x9b, 0x52, 0xf4, 0x57, 0x54, 0x7b, 0x3e, 0x50, 0xd3,
  37617. 0x75, 0x2a, 0xba, 0x97, 0xd7, 0xec, 0x95, 0x6c, 0x35, 0x63,
  37618. 0xa4, 0xa1, 0x8f, 0xf5, 0xcc, 0xbe, 0x42, 0x65, 0x4e, 0x69,
  37619. 0x35, 0x55, 0xa5, 0x3e, 0xc4, 0xf0, 0xde, 0x60, 0x54, 0xdf,
  37620. 0xbb, 0x83, 0xad, 0xdf, 0xa5, 0x24, 0x8f, 0xbe, 0x0b, 0x16,
  37621. 0xfc, 0xf2, 0x64, 0xd5, 0x79, 0x68, 0xf3, 0x91, 0x81, 0x2a,
  37622. 0xd7, 0x1c, 0xc0, 0xdd, 0xe6, 0xb6, 0xb3, 0xa2, 0x4f, 0xc0,
  37623. 0x6d, 0x77, 0x02, 0xee, 0x43, 0xd6, 0x5e, 0x82, 0x66, 0x7f,
  37624. 0xb4, 0xe6, 0x5c, 0xff, 0x87, 0x1e, 0x1d, 0x6f, 0x1d, 0x96,
  37625. 0x6d, 0xbd, 0x90, 0x57, 0x65, 0xc2, 0x01, 0x35, 0xfa, 0x9a,
  37626. 0xc6, 0xe0, 0x4e, 0x2c, 0x4b, 0x16, 0xfa, 0x0d, 0x38, 0x87,
  37627. 0x39, 0x2c, 0x2b, 0x48, 0x14, 0x92, 0x3d, 0x83, 0x00, 0xa9,
  37628. 0x1a, 0x3d, 0x4d, 0x30, 0x23, 0x48, 0xcd, 0xd5, 0xcd, 0x01,
  37629. 0xb1, 0x45, 0x85, 0xcc, 0x66, 0x47, 0x1d, 0x63, 0x3d, 0x70,
  37630. 0xb8, 0x0c, 0xfd, 0xe3, 0xb2, 0x0f, 0x64, 0x6e, 0xb9, 0x2b,
  37631. 0xe5, 0xb0, 0x4d, 0x44, 0x4d, 0x66, 0x1a, 0xfa, 0x49, 0xbb,
  37632. 0xc3, 0xb8, 0xad, 0x64, 0x23, 0x7e, 0x71, 0x9f, 0x59, 0xec,
  37633. 0x25, 0xa8, 0x5e, 0x11, 0xd6, 0x6e, 0xc9, 0x09, 0xe7, 0xb9,
  37634. 0x6a, 0x63, 0x91, 0xaa, 0x5d, 0xd2, 0x8c, 0x91, 0xe8, 0x8d,
  37635. 0x35, 0x6d, 0x10, 0xf6, 0xfc, 0x6a, 0x3c, 0x77, 0x90, 0xf8,
  37636. 0x2a, 0x49, 0x13, 0x7f, 0xdb, 0xf5, 0x0c, 0xe9, 0xc8, 0x57,
  37637. 0xc6, 0xfd, 0x26, 0x8d, 0x79, 0xb5, 0xdd, 0x47, 0x74, 0x6e,
  37638. 0xe8, 0x8f, 0x50, 0xf5, 0xa7, 0x9e, 0xd1, 0x74, 0x10, 0xbb,
  37639. 0xf4, 0x8f, 0x8f, 0x0d, 0xcd, 0x1f, 0xf6, 0x59, 0xb8, 0x6c,
  37640. 0xd2, 0x37, 0x83, 0x28, 0xb2, 0x36, 0xc1, 0x39, 0x5b, 0xde,
  37641. 0x59, 0xee, 0x77, 0xa2, 0x6e, 0x67, 0xc6, 0xea, 0x1d, 0x2b,
  37642. 0x41, 0x8f, 0x6f, 0x96, 0x94, 0x1b, 0x5d, 0xab, 0x30, 0x53,
  37643. 0x1e, 0xf8, 0x17, 0x06, 0xea, 0xcc, 0x98, 0xa8, 0xdf, 0x81,
  37644. 0xe1, 0x80, 0xb7, 0xad, 0x69, 0xcb, 0x8f, 0x81, 0x1e, 0x76,
  37645. 0x75, 0x3c, 0x11, 0x9b, 0x38, 0x95, 0xa7, 0x87, 0x1f, 0xd9,
  37646. 0x76, 0x82, 0x21, 0x13, 0x25, 0x20, 0x42, 0xd3, 0x8c, 0xd9,
  37647. 0x1c, 0x64, 0xed, 0xe9, 0x55, 0xb5, 0x29, 0x98, 0x85, 0x7c,
  37648. 0x01, 0x94, 0xaa, 0xdd, 0x8c, 0x78, 0x08, 0x99, 0x99, 0x5a,
  37649. 0xf6, 0x61, 0x4c, 0xe0, 0x99, 0xf8, 0x15, 0x74, 0x2e, 0x0d,
  37650. 0x14, 0x89, 0x11, 0x84, 0xcd, 0x78, 0x0c, 0x6b, 0x48, 0xde,
  37651. 0xb4, 0xd6, 0x05, 0xbd, 0x99, 0x58, 0xb7, 0xe5, 0xc5, 0x7a,
  37652. 0x43, 0x18, 0x55, 0x33, 0x16, 0x2b, 0xfa, 0x27, 0xf5, 0xbb,
  37653. 0xaa, 0x52, 0xb5, 0x28, 0x5c, 0xfe, 0x61, 0x7f, 0x7a, 0x70,
  37654. 0xc2, 0x32, 0x4b, 0x05, 0x8d, 0x7b, 0x4d, 0x22, 0x57, 0x25,
  37655. 0x40, 0x46, 0x7c, 0xad, 0x2f, 0x8a, 0xc8, 0x16, 0xd6, 0xac,
  37656. 0x4e, 0xe3, 0xe3, 0x29, 0xe4, 0xe8, 0x00, 0x2b, 0xc9, 0xe3,
  37657. 0x3a, 0x6f, 0x66, 0xf1, 0x37, 0x37, 0x52, 0x88, 0x77, 0xf6,
  37658. 0xbd, 0x59, 0x5f, 0xf8, 0x11, 0x46, 0x7b, 0x12, 0x88, 0x2f,
  37659. 0x4b, 0x0d, 0x16, 0x89, 0x3e, 0x2a, 0x56, 0x58, 0xa8, 0x1c,
  37660. 0xee, 0x23, 0xd5, 0x66, 0x86, 0x5f, 0x59, 0x55, 0xac, 0x07,
  37661. 0xfd, 0xda, 0x6b, 0xf1, 0xc7, 0x01, 0x19, 0xdb, 0xff, 0x63,
  37662. 0x6f, 0x27, 0xdb, 0xa1, 0xc7, 0xe9, 0xe0, 0xdb, 0xe4, 0x9a,
  37663. 0xce, 0xf5, 0xac, 0x68, 0xab, 0x59, 0x0c, 0x83, 0xa3, 0x1c,
  37664. 0x2a, 0x86, 0x55, 0xe2, 0xaa, 0xa1, 0xb3, 0xed, 0xc2, 0x2d,
  37665. 0x43, 0xc5, 0x13, 0x68, 0xe4, 0x83, 0x3e, 0xd5, 0x7f, 0xf7,
  37666. 0xd5, 0xd0, 0x60, 0xd3, 0x70, 0x7f, 0x88, 0xaa, 0xca, 0x74,
  37667. 0xcc, 0x50, 0x8d, 0x55, 0x9c, 0xfe, 0x4a, 0xc6, 0xc9, 0x36,
  37668. 0xf7, 0x27, 0x26, 0x64, 0xd3, 0x6c, 0xdb, 0x16, 0x31, 0x81,
  37669. 0xe9, 0xce, 0x73, 0x60, 0x61, 0x9c, 0x0f, 0xb5, 0x6e, 0x68,
  37670. 0xbc, 0xb1, 0x9e, 0x9f, 0xcd, 0x6c, 0x27, 0x31, 0x2d, 0x40,
  37671. 0x36, 0xce, 0x91, 0xee, 0x47, 0xdc, 0xa0, 0x4f, 0xd7, 0x14,
  37672. 0x4f, 0x93, 0x00, 0xc4, 0x34, 0xca, 0xd4, 0x42, 0x21, 0x90,
  37673. 0xf6, 0x9d, 0xea, 0x45, 0x15, 0xfe, 0x2d, 0xd6, 0xab, 0xc2,
  37674. 0x36, 0x47, 0xc0, 0x5b, 0xd2, 0xae, 0x53, 0x33, 0xb0, 0x2d,
  37675. 0x29, 0xa3, 0x14, 0xda, 0xa4, 0x48, 0xc1, 0x57, 0x0c, 0xdc,
  37676. 0x72, 0x4a, 0xd0, 0xf5, 0x5b, 0x9a, 0x57, 0x1d, 0x06, 0xc8,
  37677. 0x0f, 0xc7, 0x5b, 0x70, 0xbb, 0x27, 0xf4, 0xe2, 0xf4, 0xf3,
  37678. 0x3c, 0xdc, 0xba, 0x43, 0xc4, 0x4e, 0xe2, 0x96, 0xd4, 0x6c,
  37679. 0x33, 0x3e, 0xbf, 0x85, 0xf7, 0x3c, 0x1d, 0x46, 0x59, 0x4e,
  37680. 0xa1, 0xa7, 0xa3, 0x76, 0x55, 0x8a, 0x72, 0x83, 0xd0, 0x45,
  37681. 0x86, 0x38, 0xa5, 0x4d, 0xc8, 0x62, 0xe4, 0x8a, 0xd5, 0x8e,
  37682. 0xb7, 0x4c, 0x6e, 0xaf, 0xa4, 0xbe, 0x88, 0x87, 0x77, 0xd1,
  37683. 0x7b, 0xb2, 0x1d, 0xe0, 0x1e, 0x53, 0x30, 0x31, 0x15, 0x6c,
  37684. 0x10, 0x81, 0x03, 0x55, 0xa7, 0x69, 0xb6, 0xa5, 0x48, 0xf4,
  37685. 0xb2, 0x3b, 0x76, 0x8b, 0x2e, 0x42, 0xa6, 0xaa, 0x7e, 0x66,
  37686. 0x57, 0xc2, 0x11, 0xc5, 0x2c, 0x7d, 0x96, 0xdf, 0xe3, 0x58,
  37687. 0x12, 0x98, 0x18, 0x0d, 0x87, 0xbd, 0x64, 0xbd, 0xfe, 0x6d,
  37688. 0xad, 0x6d, 0x1e, 0xf6, 0x34, 0x01, 0xb5, 0x56, 0xe8, 0x6a,
  37689. 0xb3, 0x8c, 0x70, 0x84, 0x36, 0x17, 0xd6, 0x4b, 0xaa, 0x57,
  37690. 0xab, 0xb3, 0x45, 0x30, 0x36, 0x10, 0xd4, 0xee, 0x8a, 0xc9,
  37691. 0x29, 0xd1, 0x92, 0x9b, 0xe2, 0x7c, 0x12, 0xd1, 0x29, 0x62,
  37692. 0x41, 0x69, 0xae, 0x3a, 0x50, 0xcc, 0x89, 0x50, 0x2e, 0xe6,
  37693. 0x07, 0xf8, 0x9c, 0x98, 0x80, 0xd5, 0xa3, 0xc8, 0x74, 0xfb,
  37694. 0xfc, 0x91, 0x16, 0x02, 0xdc, 0xf0, 0x42, 0x49, 0xbc, 0xc9,
  37695. 0x2f, 0x7f, 0x8d, 0x93, 0xf7, 0xf0, 0x74, 0xb7, 0xd1, 0x55,
  37696. 0xfc, 0x79, 0x03, 0x37, 0xfb, 0xf6, 0x7d, 0x2f, 0x2d, 0xf8,
  37697. 0x6b, 0xc5, 0xf9, 0x66, 0x38, 0xf5, 0xfd, 0x64, 0xc6, 0x08,
  37698. 0x99, 0xb3, 0x25, 0xad, 0xf4, 0xfd, 0x69, 0x2f, 0xf1, 0x18,
  37699. 0x46, 0xd6, 0x5c, 0x1a, 0x37, 0xcd, 0xee, 0xa3, 0xbf, 0x0f,
  37700. 0x57, 0x5c, 0xc3, 0x97, 0x94, 0x84, 0x89, 0xbe, 0x00, 0xf6,
  37701. 0x40, 0xe9, 0x5a, 0x52, 0xaf, 0x3a, 0x5b, 0xf4, 0x56, 0xb0,
  37702. 0x04, 0x49, 0xc6, 0x32, 0x8c, 0xa1, 0x0a, 0xd8, 0x88, 0xa1,
  37703. 0xc3, 0xb7, 0x8b, 0x96, 0xc3, 0x39, 0x51, 0x50, 0x83, 0xa6,
  37704. 0xf0, 0x6d, 0xe7, 0x6e, 0x20, 0xff, 0x9d, 0xac, 0x03, 0x57,
  37705. 0xbc, 0xcb, 0x6a, 0x19, 0xa7, 0xc5, 0xd2, 0x44, 0x4f, 0x17,
  37706. 0x1e, 0x9a, 0x8d, 0x97, 0x25, 0x55, 0x52, 0x49, 0xe2, 0x48,
  37707. 0xae, 0x4b, 0x3f, 0x94, 0x5a, 0xb2, 0x2d, 0x40, 0xd9, 0x85,
  37708. 0xef, 0x03, 0xa0, 0xd3, 0x66, 0x9a, 0x8f, 0x7b, 0xc0, 0x8d,
  37709. 0x54, 0x95, 0x42, 0x49, 0xeb, 0x15, 0x00, 0xf3, 0x6d, 0x6f,
  37710. 0x40, 0xf2, 0x8b, 0xc1, 0x50, 0xa6, 0x22, 0x3b, 0xd6, 0x88,
  37711. 0xa1, 0xf7, 0xb0, 0x1f, 0xcd, 0x20, 0x4e, 0x5b, 0xad, 0x66,
  37712. 0x4a, 0xda, 0x40, 0xee, 0x4c, 0x4c, 0x3e, 0xa7, 0x75, 0x51,
  37713. 0x90, 0xba, 0xee, 0x59, 0xbc, 0xe3, 0xcd, 0x4d, 0xb9, 0x57,
  37714. 0xb7, 0xf8, 0xc1, 0xb9, 0x8d, 0x0f, 0x58, 0x2c, 0x4c, 0x98,
  37715. 0xa6, 0x9c, 0xd9, 0x0e, 0x25, 0x4f, 0xea, 0x4c, 0x15, 0x0b,
  37716. 0x89, 0xe4, 0xac, 0xa1, 0x5a, 0xa1, 0xfd, 0x5b, 0xc6, 0xfe,
  37717. 0xf0, 0xf1, 0x4c, 0xa7, 0x60, 0xbc, 0xc3, 0xa5, 0x80, 0x00,
  37718. 0x3b, 0x3f, 0x22, 0x38, 0x60, 0x40, 0x76, 0x52, 0x83, 0x32,
  37719. 0xee, 0x20, 0x6a, 0xf9, 0x1e, 0x6b, 0x99, 0x52, 0xe7, 0x04,
  37720. 0xdc, 0x5a, 0x9d, 0x77, 0x8a, 0xdd, 0x9b, 0x53, 0x19, 0xff,
  37721. 0x69, 0x8c, 0xbc, 0xc6, 0xe0, 0x79, 0x0d, 0x3d, 0x3d, 0x54,
  37722. 0x5b, 0xe0, 0x47, 0x5b, 0x71, 0x05, 0x98, 0x8f, 0xbb, 0x65,
  37723. 0xe1, 0x31, 0x9a, 0xc8, 0x1e, 0x7a, 0x4a, 0xf8, 0xcb, 0x17,
  37724. 0xd1, 0x83, 0x58, 0xb1, 0xc0, 0xe4, 0xb1, 0x85, 0xca, 0xa5,
  37725. 0xf8, 0x0e, 0xd1, 0x0c, 0xe8, 0x71, 0xc3, 0xfa, 0xbf, 0x1d,
  37726. 0xd6, 0x98, 0x03, 0xed, 0x77, 0x3b, 0x55, 0xaf, 0x69, 0x72,
  37727. 0x6b, 0x42, 0x31, 0x98, 0x95, 0xd5, 0x79, 0xa5, 0x4c, 0x51,
  37728. 0xcf, 0x02, 0x65, 0x93, 0xf2, 0x71, 0xdc, 0xde, 0x9a, 0xa3,
  37729. 0x86, 0xa7, 0xea, 0xcf, 0xd7, 0xe5, 0x00, 0xde, 0x40, 0x02,
  37730. 0xcd, 0x6b, 0x46, 0x0b, 0xbb, 0xbf, 0x77, 0x5f, 0x9d, 0x7c,
  37731. 0xa4, 0x7f, 0x7c, 0x8a, 0xba, 0xd6, 0x99, 0xc5, 0xaa, 0x06,
  37732. 0x36, 0xe1, 0x7e, 0x9c, 0x6f, 0x28, 0xd4, 0x6e, 0x1d, 0x5b,
  37733. 0xdd, 0x01, 0x24, 0xbd, 0x6c, 0x5d, 0x87, 0x3c, 0xc1, 0xf6,
  37734. 0x93, 0x37, 0xe2, 0x3b, 0x70, 0xc4, 0xd8, 0x10, 0x0e, 0x44,
  37735. 0x37, 0x00, 0xe3, 0x07, 0xbd, 0x67, 0xd3, 0x9d, 0xe6, 0xe7,
  37736. 0x48, 0x1b, 0xe0, 0x79, 0xb3, 0x30, 0x91, 0x89, 0x0f, 0x89,
  37737. 0x77, 0xfa, 0x13, 0x85, 0xd0, 0x32, 0xbd, 0xc1, 0x9e, 0x52,
  37738. 0x04, 0x80, 0x54, 0xb1, 0x08, 0x39, 0x20, 0xda, 0x3e, 0xf1,
  37739. 0xd9, 0x15, 0x74, 0x55, 0x06, 0xfc, 0x4d, 0x85, 0xd4, 0x98,
  37740. 0x02, 0x64, 0x10, 0x86, 0xd7, 0xcd, 0x01, 0x0d, 0x85, 0xa0,
  37741. 0x78, 0xb0, 0x58, 0x99, 0x7b, 0xdf, 0xe4, 0x8c, 0x3f, 0xab,
  37742. 0xc0, 0xbc, 0xa5, 0x30, 0x28, 0xe1, 0x4e, 0x02, 0x98, 0xab,
  37743. 0x03, 0xf3, 0x21, 0xe7, 0xa7, 0xe7, 0xc3, 0x5f, 0x98, 0xc0,
  37744. 0x83, 0x02, 0xe8, 0x8a, 0x30, 0x75, 0x95, 0xcf, 0x77, 0x83,
  37745. 0xfb, 0x32, 0x5a, 0xf9, 0x13, 0xed, 0xdb, 0xda, 0xc3, 0x84,
  37746. 0x4b, 0x8f, 0x1a, 0xf0, 0xad, 0x8e, 0xcf, 0xe3, 0xa7, 0x2b,
  37747. 0xb5, 0x44, 0x75, 0xd6, 0xda, 0x33, 0x81, 0x22, 0xa7, 0x6a,
  37748. 0xbd, 0x21, 0x64, 0x85, 0xfa, 0x65, 0x8e, 0xc4, 0x58, 0xec,
  37749. 0xc4, 0x18, 0x90, 0xa3, 0xcc, 0x2e, 0xaa, 0xa2, 0x2e, 0x46,
  37750. 0x7a, 0x4a, 0x35, 0xbf, 0x58, 0x78, 0x2b, 0x1e, 0x72, 0xe5,
  37751. 0x80, 0xc9, 0xe0, 0x9e, 0x43, 0x01, 0xcc, 0xe1, 0x0c, 0x00,
  37752. 0xe9, 0xc1, 0xa5, 0x1a, 0x9b, 0x4e, 0x6e, 0x34, 0x32, 0xfd,
  37753. 0x86, 0xb7, 0xae, 0xc3, 0x6e, 0x69, 0x04, 0xf6, 0x6a, 0x92,
  37754. 0x78, 0xb1, 0x1f, 0x9d, 0x5e, 0x0c, 0xf9, 0xc4, 0x1a, 0xf6,
  37755. 0xb4, 0x8a, 0x63, 0xb5, 0x87, 0x5b, 0xfb, 0x50, 0xbf, 0xd5,
  37756. 0x17, 0x97, 0x8e, 0x55, 0x1c, 0xfe, 0x82, 0xf6, 0xa7, 0x9c,
  37757. 0x0b, 0xc9, 0x0a, 0xf6, 0x7f, 0x70, 0xd1, 0x00, 0xed, 0x1c,
  37758. 0x6c, 0x3a, 0x95, 0xed, 0x61, 0xa4, 0xd6, 0x57, 0xfb, 0x57,
  37759. 0xf8, 0x9b, 0x4c, 0xce, 0x50, 0x26, 0x5c, 0x19, 0xd2, 0xa7,
  37760. 0xd6, 0xe8, 0x3c, 0x29, 0x34, 0xfb, 0x26, 0x7f, 0xc5, 0x78,
  37761. 0xbf, 0xfe, 0xb6, 0x2a, 0x5a, 0x62, 0x8e, 0x31, 0x9b, 0x57,
  37762. 0xa4, 0xe7, 0x4d, 0x3d, 0x18, 0x05, 0xf0, 0x94, 0xbb, 0x04,
  37763. 0xfa, 0x0a, 0x92, 0xf4, 0xc6, 0x7f, 0x16, 0xa2, 0x31, 0xed,
  37764. 0xc1, 0xb4, 0x62, 0x54, 0x3a, 0x23, 0x12, 0x6a, 0x76, 0xcc,
  37765. 0x8c, 0x91, 0x89, 0x58, 0x8c, 0x20, 0x23, 0xd9, 0xaa, 0x0d,
  37766. 0x80, 0xbe, 0xb9, 0xb4, 0x40, 0x1e, 0xff, 0xa9, 0xf7, 0x71,
  37767. 0x0a, 0xa0, 0x0a, 0xdf, 0x11, 0x0b, 0x66, 0x3f, 0xf2, 0x4d,
  37768. 0x5d, 0x39, 0x7c, 0x77, 0xe1, 0xb1, 0x09, 0xa1, 0x6b, 0x2e,
  37769. 0x30, 0x43, 0x33, 0x80, 0x6e, 0x6a, 0x1d, 0x47, 0xd9, 0xd6,
  37770. 0xac, 0xdc, 0x3f, 0x16, 0xb1, 0x58, 0x11, 0x9f, 0x67, 0xd7,
  37771. 0x15, 0x45, 0xd8, 0xc3, 0x69, 0x24, 0x8d, 0xac, 0xff, 0xc3,
  37772. 0x43, 0xfd, 0x24, 0xaf, 0xf1, 0xc8, 0x3a, 0xc7, 0xd6, 0x1f,
  37773. 0x56, 0x26, 0x16, 0xe6, 0x30, 0xcd, 0x6e, 0x0a, 0x63, 0x2a,
  37774. 0x7b, 0x86, 0xd7, 0x65, 0x39, 0x45, 0x7c, 0xe6, 0xa0, 0xe6,
  37775. 0x38, 0xed, 0x54, 0x84, 0x00, 0x4d, 0x8e, 0xc2, 0xba, 0x56,
  37776. 0x9b, 0xf3, 0xe1, 0xe8, 0x7d, 0xfe, 0x47, 0xf0, 0x58, 0xe7,
  37777. 0x59, 0x60, 0x97, 0x2e, 0x57, 0x1a, 0x09, 0x1f, 0x8b, 0x2b,
  37778. 0x0b, 0x47, 0x75, 0xc0, 0xb3, 0x79, 0xce, 0x10, 0x47, 0x6d,
  37779. 0xfc, 0xcb, 0x22, 0x61, 0x5c, 0x39, 0xc4, 0x3f, 0xc5, 0xef,
  37780. 0xb8, 0xc8, 0x88, 0x52, 0xce, 0x90, 0x17, 0xf5, 0x3c, 0xa9,
  37781. 0x87, 0x6f, 0xcb, 0x2f, 0x11, 0x53, 0x65, 0x9b, 0x74, 0x21,
  37782. 0x3e, 0xdd, 0x7b, 0x1f, 0x19, 0x9f, 0x53, 0xe6, 0xab, 0xc0,
  37783. 0x56, 0xba, 0x80, 0x19, 0x5d, 0x3f, 0xc7, 0xe2, 0xfb, 0x8c,
  37784. 0xe2, 0x93, 0xe0, 0x31, 0xc9, 0x33, 0x31, 0x23, 0x31, 0xa1,
  37785. 0x36, 0x4c, 0x62, 0xd8, 0x0a, 0xfd, 0x85, 0x97, 0xae, 0xa9,
  37786. 0xe9, 0x58, 0x29, 0x17, 0x33, 0x09, 0x5a, 0x8e, 0xa3, 0x90,
  37787. 0x41, 0xd3, 0xfc, 0x24, 0x98, 0x61, 0x4d, 0x30, 0x1f, 0x76,
  37788. 0x8f, 0xfc, 0xd0, 0x96, 0x8b, 0x2e, 0x9b, 0x24, 0x73, 0x35,
  37789. 0x00, 0xb7, 0xf6, 0xe8, 0xba, 0xec, 0x98, 0x74, 0x41, 0xa4,
  37790. 0x47, 0x10, 0x0d, 0xbc, 0xba, 0xd1, 0xe7, 0xdb, 0x12, 0xcb,
  37791. 0x5f, 0x02, 0xb1, 0xa6, 0xa0, 0xd7, 0x28, 0x30, 0x3e, 0x0a,
  37792. 0x5c, 0x5f, 0xe6, 0x2f, 0x3c, 0xde, 0x46, 0x60, 0xaf, 0x07,
  37793. 0x5f, 0xed, 0x08, 0xc0, 0x06, 0x58, 0xba, 0xd7, 0x36, 0x5b,
  37794. 0xa0, 0x4a, 0xf7, 0xa1, 0x05, 0x9b, 0x00, 0xda, 0x49, 0xdc,
  37795. 0xbf, 0xea, 0xe1, 0x03, 0xda, 0x95, 0x95, 0xa0, 0xfa, 0x2e,
  37796. 0xf1, 0x60, 0x11, 0x47, 0xdd, 0xb3, 0xfb, 0x0b, 0xa2, 0x92,
  37797. 0xcf, 0x73, 0xbb, 0xce, 0x82, 0x71, 0xbc, 0xbd, 0x50, 0x64,
  37798. 0xf1, 0x96, 0x48, 0x48, 0x93, 0xf8, 0xdc, 0x1c, 0x18, 0x12,
  37799. 0xc6, 0x17, 0x6a, 0xa9, 0xc1, 0x4d, 0x6f, 0x76, 0xda, 0x2f,
  37800. 0x4e, 0x59, 0xdd, 0x8b, 0x1c, 0xa5, 0x30, 0xb6, 0xe9, 0x88,
  37801. 0x8f, 0x75, 0x0c, 0xcd, 0xd8, 0x61, 0xf4, 0x28, 0xc5, 0x9a,
  37802. 0xcd, 0x77, 0x0d, 0x36, 0x5f, 0x75, 0xa5, 0x0a, 0x77, 0x20,
  37803. 0x28, 0x5a, 0xac, 0x5f, 0xa1, 0x83, 0x67, 0x70, 0xb7, 0xd8,
  37804. 0x23, 0x48, 0x60, 0xa8, 0xd0, 0xaf, 0xee, 0x7a, 0xb8, 0x25,
  37805. 0xd7, 0x8f, 0x82, 0x8c, 0xd0, 0x81, 0x7a, 0x49, 0x69, 0xe4,
  37806. 0x22, 0x73, 0x29, 0x48, 0xc8, 0x09, 0x72, 0x16, 0xf8, 0x3d,
  37807. 0xff, 0x13, 0xac, 0x98, 0x03, 0x76, 0x33, 0xcb, 0x19, 0xb0,
  37808. 0x22, 0x5b, 0x1e, 0x16, 0x29, 0xb9, 0xcc, 0xa6, 0x92, 0xd8,
  37809. 0xed, 0x93, 0x0f, 0xbd, 0x10, 0x98, 0x53, 0x0a, 0x07, 0x7f,
  37810. 0xd6, 0x51, 0x76, 0xda, 0xdc, 0x0c, 0xeb, 0x2a, 0x95, 0xd0,
  37811. 0x3e, 0xa6, 0xc4, 0xc6, 0xd8, 0xfb, 0x1b, 0x2a, 0x7f, 0xf1,
  37812. 0x08, 0xbe, 0xd3, 0xed, 0x67, 0x63, 0x5f, 0x1d, 0x29, 0xdb,
  37813. 0x47, 0x03, 0x4a, 0xf4, 0x6b, 0xb4, 0x46, 0x02, 0x28, 0x4f,
  37814. 0x88, 0x9b, 0x46, 0x66, 0x40, 0x56, 0x34, 0x4c, 0xec, 0x8e,
  37815. 0x0b, 0x5d, 0x14, 0x94, 0x91, 0xfc, 0xdc, 0x0c, 0xdc, 0x5b,
  37816. 0x45, 0x12, 0x7e, 0xa1, 0xe9, 0x75, 0x38, 0xcb, 0xd3, 0x6b,
  37817. 0xd7, 0xa4, 0x24, 0x94, 0x78, 0x09, 0x7f, 0x77, 0xc8, 0x6d,
  37818. 0xe1, 0x82, 0x1c, 0x1c, 0x91, 0xc6, 0x38, 0x9e, 0x3b, 0x3d,
  37819. 0x31, 0xdd, 0x9e, 0x46, 0x58, 0x7a, 0x42, 0x16, 0x6f, 0xfd,
  37820. 0x7d, 0x8c, 0xf5, 0xf0, 0x9f, 0x92, 0x6e, 0xbe, 0x47, 0xa6,
  37821. 0x1e, 0x8e, 0x82, 0x15, 0x24, 0xc3, 0x1b, 0xb0, 0xd1, 0x68,
  37822. 0xf9, 0xd1, 0x7c, 0x60, 0x98, 0x86, 0xd9, 0x53, 0xa2, 0x38,
  37823. 0x62, 0xf4, 0x72, 0x71, 0xcb, 0xb9, 0x35, 0xef, 0xb9, 0x49,
  37824. 0x3a, 0x73, 0xb2, 0xd7, 0x0f, 0x90, 0xf5, 0x2c, 0x5b, 0xf5,
  37825. 0xfd, 0x39, 0x17, 0xf7, 0xe4, 0x69, 0x81, 0x0f, 0x6b, 0xe7,
  37826. 0x32, 0xd2, 0xdc, 0x5d, 0x40, 0xbf, 0x41, 0x95, 0x89, 0x81,
  37827. 0x29, 0x80, 0x40, 0xa3, 0xac, 0xd2, 0xc7, 0xf7, 0xe8, 0xd0,
  37828. 0x45, 0xed, 0x48, 0x43, 0x3a, 0xed, 0x8d, 0xef, 0x37, 0xe1,
  37829. 0x24, 0x9a, 0x67, 0x9a, 0x6b, 0x71, 0x4f, 0x9a, 0xb9, 0x2c,
  37830. 0x1b, 0x10, 0x48, 0xe2, 0x31, 0x1e, 0xbb, 0xf2, 0x4a, 0xad,
  37831. 0x04, 0xc7, 0xd7, 0xf2, 0xe8, 0x83, 0x5f, 0xe8, 0xa2, 0x81,
  37832. 0x95, 0xf9, 0x60, 0x51, 0x9c, 0x99, 0x76, 0x69, 0x76, 0x4e,
  37833. 0xbd, 0x44, 0x52, 0x36, 0xca, 0xd8, 0x6e, 0xf7, 0x1a, 0xa1,
  37834. 0x54, 0xdf, 0x90, 0x52, 0x94, 0xb6, 0x3a, 0xcb, 0x43, 0x56,
  37835. 0x11, 0xde, 0xa0, 0xe1, 0x45, 0x8a, 0x80, 0x2d, 0xaf, 0x1f,
  37836. 0x24, 0x3f, 0x80, 0x17, 0x1f, 0x28, 0xbb, 0xcc, 0x1a, 0xd2,
  37837. 0x2d, 0xa6, 0x9e, 0xe0, 0xdc, 0xf0, 0x98, 0x16, 0x58, 0x88,
  37838. 0xc6, 0xf1, 0x81, 0x71, 0x91, 0x8f, 0xa2, 0xab, 0xa5, 0xe6,
  37839. 0x68, 0x1f, 0xa5, 0x86, 0xb5, 0xd9, 0x05, 0xba, 0x50, 0x67,
  37840. 0x0b, 0x1e, 0xfe, 0x42, 0x50, 0xf8, 0x01, 0xf8, 0x38, 0x92,
  37841. 0x57, 0x86, 0x08, 0x47, 0xee, 0x23, 0x11, 0x60, 0x61, 0x1a,
  37842. 0x77, 0x3c, 0x1a, 0x8e, 0x08, 0xe3, 0xaf, 0x84, 0x04, 0x75,
  37843. 0x15, 0x47, 0x7a, 0x83, 0x8e, 0x92, 0x3e, 0xe8, 0xf0, 0xc2,
  37844. 0x81, 0x89, 0x3b, 0x73, 0x81, 0xe5, 0xe8, 0x97, 0x97, 0x63,
  37845. 0x64, 0xf3, 0xa9, 0x1b, 0x61, 0x65, 0x7f, 0x0e, 0x47, 0x6b,
  37846. 0x14, 0x57, 0x29, 0x8f, 0x91, 0x35, 0x43, 0x10, 0x12, 0x86,
  37847. 0x99, 0xec, 0xc8, 0x9e, 0x67, 0x90, 0x20, 0x21, 0x3c, 0x83,
  37848. 0xdb, 0x73, 0x4e, 0x8e, 0x7d, 0x86, 0xde, 0xb8, 0xd8, 0xfa,
  37849. 0x23, 0x1f, 0x5a, 0xe4, 0xc7, 0x0c, 0x1d, 0x5e, 0xd1, 0x10,
  37850. 0x58, 0xd5, 0x86, 0xfa, 0x40, 0x30, 0x0a, 0x78, 0x0a, 0xa5,
  37851. 0x56, 0xd5, 0xe6, 0x86, 0xd4, 0x14, 0x77, 0x32, 0xcd, 0x07,
  37852. 0xf9, 0xbe, 0x7a, 0xd8, 0xbc, 0x91, 0xe0, 0xda, 0x76, 0x6b,
  37853. 0x97, 0x10, 0xda, 0xea, 0x27, 0xa2, 0x67, 0x6d, 0x94, 0x27,
  37854. 0x6e, 0xea, 0xca, 0x56, 0x45, 0x32, 0x1d, 0x38, 0x12, 0x21,
  37855. 0x33, 0x2c, 0x3c, 0x5c, 0x33, 0xb0, 0x9e, 0x80, 0x0b, 0x4e,
  37856. 0xbb, 0x09, 0x5e, 0x56, 0x54, 0xb0, 0x9b, 0x7e, 0xb6, 0x00,
  37857. 0xe8, 0x63, 0x19, 0x85, 0xf1, 0x4d, 0x65, 0x9d, 0x1f, 0x8d,
  37858. 0x18, 0xcc, 0x63, 0xc6, 0xd9, 0xa6, 0xbc, 0xe7, 0x42, 0x55,
  37859. 0x12, 0xdc, 0x8c, 0x26, 0x2d, 0x8d, 0xc2, 0xe9, 0x3b, 0xbc,
  37860. 0xed, 0x06, 0x08, 0x31, 0xb0, 0xe0, 0x99, 0xe2, 0x86, 0x81,
  37861. 0x88, 0x4a, 0xac, 0x1f, 0x4a, 0xb2, 0x1e, 0x1e, 0x4c, 0xb2,
  37862. 0x9f, 0x27, 0xa0, 0xd9, 0x8a, 0x7e, 0xe7, 0xa3, 0xad, 0xeb,
  37863. 0x2c, 0xfd, 0x14, 0xc6, 0x4b, 0x26, 0xce, 0x38, 0xb9, 0x01,
  37864. 0x9e, 0xde, 0xc8, 0x7b, 0x82, 0x2f, 0xaa, 0x72, 0x80, 0xbe,
  37865. 0x3a, 0x35, 0x95, 0xc8, 0xf3, 0x7c, 0x36, 0x68, 0x02, 0xdc,
  37866. 0xa2, 0xda, 0xef, 0xd7, 0xf1, 0x3e, 0x81, 0xb3, 0x5d, 0x2f,
  37867. 0xcf, 0x7e, 0xe6, 0x9c, 0xa0, 0x32, 0x29, 0x8b, 0x52, 0x24,
  37868. 0xbd, 0x0d, 0x36, 0xdc, 0x1d, 0xcc, 0x6a, 0x0a, 0x74, 0x52,
  37869. 0x1b, 0x68, 0x4d, 0x15, 0x05, 0x47, 0xe1, 0x2f, 0x97, 0x45,
  37870. 0x52, 0x17, 0x4b, 0x2a, 0x3b, 0x74, 0xc5, 0x20, 0x35, 0x5c,
  37871. 0x37, 0xae, 0xe6, 0xa7, 0x24, 0x0f, 0x34, 0x70, 0xea, 0x7c,
  37872. 0x03, 0xa3, 0xde, 0x2d, 0x22, 0x55, 0x88, 0x01, 0x45, 0xf2,
  37873. 0x5f, 0x1f, 0xaf, 0x3b, 0xb1, 0xa6, 0x5d, 0xcd, 0x93, 0xfb,
  37874. 0xf8, 0x2f, 0x87, 0xcc, 0x26, 0xc5, 0x36, 0xde, 0x06, 0x9b,
  37875. 0xe9, 0xa7, 0x66, 0x7e, 0x8c, 0xcd, 0x99, 0x6b, 0x51, 0x1c,
  37876. 0xb0, 0xa0, 0xfa, 0xc7, 0x46, 0xfe, 0x65, 0xe4, 0x80, 0x5b,
  37877. 0x5f, 0x24, 0x3b, 0xa4, 0xe6, 0x81, 0x31, 0xe5, 0x87, 0x2c,
  37878. 0xa4, 0x83, 0xaf, 0x8b, 0x9f, 0x89, 0xb4, 0x3c, 0x7a, 0xbe,
  37879. 0x4c, 0xb3, 0xbf, 0x3d, 0xec, 0x78, 0xb0, 0x8a, 0xdd, 0xc8,
  37880. 0x43, 0x8c, 0x45, 0xa1, 0xa3, 0x3a, 0x82, 0x7d, 0x06, 0xdf,
  37881. 0x20, 0x27, 0x9b, 0x4e, 0x09, 0x90, 0x6a, 0x23, 0xbf, 0x1b,
  37882. 0x04, 0x1d, 0x50, 0xe2, 0xb4, 0xff, 0xe0, 0xd0, 0x9b, 0x40,
  37883. 0x2b, 0xc0, 0x52, 0xc1, 0x39, 0x29, 0x60, 0x83, 0x06, 0x9b,
  37884. 0x48, 0xb8, 0xa7, 0xe1, 0x2b, 0xfb, 0xf0, 0x2b, 0x82, 0xf1,
  37885. 0xda, 0xc9, 0x30, 0x47, 0x3f, 0xf5, 0xf9, 0xf7, 0x6c, 0xf0,
  37886. 0x0f, 0xe7, 0xb1, 0x4d, 0x46, 0x49, 0xf8, 0xb3, 0xe1, 0xfe,
  37887. 0x85, 0x61, 0xcc, 0xf7, 0xfa, 0xd2, 0xf1, 0xbc, 0xf0, 0x7f,
  37888. 0x3b, 0xe6, 0x45, 0xa2, 0x1b, 0x55, 0xf6, 0x0c, 0x02, 0x95,
  37889. 0xdc, 0x78, 0x94, 0xa0, 0xc4, 0x6a, 0x21, 0x7e, 0xa8, 0x5f,
  37890. 0xbd, 0xc3, 0xb3, 0x4d, 0x9b, 0x30, 0x31, 0x1d, 0x5b, 0x8b,
  37891. 0x45, 0x3c, 0x18, 0xe9, 0x61, 0xe8, 0x76, 0x3e, 0x91, 0xd2,
  37892. 0xfd, 0x1a, 0xd7, 0x30, 0x4d, 0xfe, 0xef, 0x7f, 0xc0, 0x7e,
  37893. 0x45, 0x43, 0xe9, 0xf9, 0x23, 0xfe, 0xd8, 0xef, 0xbc, 0xd6,
  37894. 0x99, 0x79, 0x54, 0xed, 0x7a, 0x8b, 0x39, 0xa6, 0xe7, 0x9d,
  37895. 0x3f, 0x9f, 0x35, 0xe1, 0xe4, 0xd5, 0x26, 0x31, 0x3a, 0x44,
  37896. 0x03, 0x79, 0xde, 0xdc, 0x29, 0x1e, 0x8e, 0x26, 0x41, 0xc6,
  37897. 0x60, 0xaa, 0xfd, 0xe1, 0x5e, 0xa6, 0xc0, 0x2f, 0x90, 0x1e,
  37898. 0x3b, 0xc1, 0xe6, 0xf6, 0xde, 0x60, 0x87, 0x57, 0x51, 0x11,
  37899. 0x6a, 0x8e, 0x9d, 0x70, 0x9d, 0x6d, 0x36, 0x21, 0x05, 0x55,
  37900. 0xc1, 0x56, 0x9b, 0xc9, 0x91, 0x50, 0x3e, 0xb4, 0xbd, 0x19,
  37901. 0x53, 0x44, 0x99, 0xc7, 0xb8, 0xce, 0xce, 0x86, 0x06, 0x5d,
  37902. 0x99, 0x85, 0x33, 0xd4, 0x16, 0x21, 0x4a, 0xe9, 0x7e, 0x2e,
  37903. 0xcc, 0x7e, 0x3f, 0xc1, 0x47, 0x3b, 0x32, 0xd0, 0x57, 0x1c,
  37904. 0xc2, 0x26, 0x67, 0xf0, 0xd9, 0xc4, 0x9e, 0xbb, 0x65, 0xa4,
  37905. 0xf7, 0xf7, 0x8d, 0x7d, 0x08, 0xd4, 0x9c, 0x1e, 0x0f, 0xb9,
  37906. 0xff, 0x24, 0x2f, 0xaf, 0xfa, 0x24, 0x26, 0xb7, 0xb1, 0x78,
  37907. 0xc1, 0xd1, 0xfe, 0x85, 0x55, 0xa0, 0x86, 0x77, 0xf6, 0xc2,
  37908. 0xe0, 0x12, 0xe4, 0x45, 0x85, 0xd0, 0xe7, 0x68, 0xf0, 0x31,
  37909. 0x4c, 0x9c, 0xb0, 0x5f, 0x89, 0xca, 0xfe, 0xc2, 0xf0, 0x1e,
  37910. 0xeb, 0xee, 0x75, 0x64, 0xea, 0x09, 0xd4, 0x1c, 0x72, 0x12,
  37911. 0xd4, 0x31, 0xf0, 0x89, 0x71, 0x74, 0x6e, 0x01, 0x32, 0xca,
  37912. 0x8a, 0x91, 0x0c, 0xdf, 0xd7, 0x05, 0xe9, 0x35, 0xed, 0x06,
  37913. 0x1a, 0x17, 0x5a, 0xf3, 0x65, 0xc5, 0xbd, 0x37, 0xf2, 0x53,
  37914. 0x49, 0x2f, 0xcd, 0xc6, 0x15, 0xb3, 0x36, 0x88, 0xd8, 0x7a,
  37915. 0x2f, 0xfa, 0x21, 0x7f, 0x55, 0x20, 0xc6, 0xf4, 0x23, 0x59,
  37916. 0x6b, 0x3c, 0xeb, 0xe5, 0xd3, 0x78, 0xdc, 0x31, 0xeb, 0x87,
  37917. 0x86, 0x3d, 0x7c, 0x10, 0x64, 0x66, 0xa4, 0xad, 0x07, 0xe1,
  37918. 0x93, 0x15, 0x07, 0x4c, 0xe4, 0xb4, 0x4a, 0x06, 0xca, 0x2a,
  37919. 0x50, 0xa2, 0x85, 0xc6, 0xa1, 0x19, 0x89, 0x7f, 0x8a, 0x05,
  37920. 0x00, 0x23, 0x72, 0x5f, 0x89, 0x74, 0x8e, 0x22, 0xa1, 0x5d,
  37921. 0x26, 0xf9, 0xfe, 0xdf, 0x6d, 0x98, 0x3a, 0xc4, 0x7c, 0x93,
  37922. 0xcf, 0xc4, 0xfe, 0xed, 0x98, 0xb0, 0x31, 0x4c, 0x81, 0x83,
  37923. 0x0d, 0x5d, 0x3d, 0x0c, 0x27, 0x4e, 0xca, 0xcf, 0x38, 0x0c,
  37924. 0x37, 0xb0, 0xf8, 0xc5, 0xc8, 0x52, 0x14, 0xec, 0x53, 0x80,
  37925. 0xb9, 0xd8, 0x8a, 0x05, 0x4e, 0x31, 0x3d, 0x67, 0x57, 0xf0,
  37926. 0x7a, 0xa2, 0xc5, 0xc9, 0x02, 0x25, 0x69, 0x83, 0xb9, 0x3e,
  37927. 0x1b, 0x04, 0xbf, 0xb2, 0xe6, 0x97, 0x7a, 0x6b, 0x8e, 0x37,
  37928. 0x77, 0x2e, 0x16, 0x8b, 0x33, 0xe1, 0xea, 0x2b, 0x30, 0x01,
  37929. 0x6e, 0xa0, 0x28, 0x14, 0x17, 0xe9, 0x98, 0xa8, 0x89, 0x72,
  37930. 0x68, 0x64, 0x81, 0x60, 0xa8, 0xf7, 0x72, 0xdf, 0x1a, 0xae,
  37931. 0xf5, 0xf0, 0x9f, 0x69, 0x35, 0xbc, 0x58, 0x27, 0x38, 0xd6,
  37932. 0x7f, 0x7a, 0xd4, 0xc4, 0xf1, 0xcf, 0xee, 0x59, 0x49, 0x31,
  37933. 0xda, 0xc1, 0x08, 0x46, 0x65, 0x68, 0xe9, 0x44, 0x18, 0x2b,
  37934. 0xf2, 0x2a, 0x13, 0x60, 0x07, 0xae, 0xe4, 0x96, 0xdb, 0x0a,
  37935. 0x6f, 0x52, 0x23, 0x9a, 0xcf, 0x9d, 0xa4, 0xc5, 0xc1, 0x74,
  37936. 0xa8, 0x0e, 0xe1, 0x5e, 0xfa, 0xa4, 0x06, 0x9c, 0x2e, 0x70,
  37937. 0x08, 0x22, 0x25, 0x4f, 0xc1, 0xf1, 0x13, 0x5a, 0x66, 0xa0,
  37938. 0x6c, 0x59, 0xa3, 0xfc, 0x03, 0x9c, 0x8a, 0x23, 0x01, 0x00,
  37939. 0xa9, 0x49, 0xf0, 0x22, 0xa3, 0x8f, 0x6c, 0xef, 0xcb, 0x69,
  37940. 0x06, 0x3a, 0x69, 0x99, 0x96, 0xd2, 0xa7, 0xa0, 0x0b, 0x7e,
  37941. 0x44, 0x7d, 0x04, 0xff, 0x7e, 0x9e, 0x1e, 0x77, 0xa0, 0x30,
  37942. 0xd1, 0xdf, 0x18, 0xe4, 0xd8, 0xa5, 0x64, 0xbe, 0x8c, 0x80,
  37943. 0x28, 0xe2, 0x98, 0x5e, 0xec, 0x9e, 0xb1, 0x0a, 0xb5, 0x25,
  37944. 0xaa, 0xb8, 0x0f, 0x78, 0x30, 0x48, 0x06, 0xe5, 0x76, 0xf9,
  37945. 0x24, 0x96, 0x87, 0x2a, 0x91, 0x89, 0xb6, 0xce, 0x04, 0xdf,
  37946. 0xfc, 0x13, 0x42, 0x19, 0xba, 0x14, 0x46, 0x20, 0x08, 0x47,
  37947. 0xe1, 0x82, 0x57, 0x51, 0x74, 0x3b, 0x5b, 0x23, 0x5c, 0xb2,
  37948. 0x85, 0x8c, 0xed, 0xe6, 0xda, 0x4d, 0x56, 0xe8, 0x61, 0x31,
  37949. 0xec, 0x97, 0x27, 0xeb, 0xf2, 0xa7, 0x7c, 0x13, 0x1b, 0xc5,
  37950. 0x44, 0xfe, 0x63, 0x4b, 0x2b, 0x33, 0x22, 0x23, 0x60, 0x86,
  37951. 0x7c, 0x3b, 0x57, 0xba, 0x16, 0xde, 0x47, 0x04, 0x3e, 0x2b,
  37952. 0xe5, 0xbd, 0x23, 0xa0, 0xab, 0xdf, 0x5d, 0x6e, 0x20, 0xb1,
  37953. 0x37, 0x44, 0xcb, 0xbd, 0x03, 0xa9, 0x5c, 0xe6, 0x92, 0x5e,
  37954. 0x2f, 0x6f, 0x95, 0xc6, 0x5b, 0x6d, 0xab, 0x39, 0xdd, 0x1e,
  37955. 0x34, 0xd5, 0x21, 0xca, 0x92, 0xee, 0x59, 0xf0, 0xb9, 0x65,
  37956. 0xe6, 0x81, 0x49, 0xf8, 0x11, 0xec, 0x45, 0x14, 0x6a, 0x19,
  37957. 0xb4, 0xce, 0xbf, 0x9e, 0xf7, 0x32, 0x8d, 0x99, 0x78, 0xc3,
  37958. 0x07, 0x3d, 0xfd, 0x18, 0x2d, 0x0e, 0x06, 0x2f, 0x27, 0x24,
  37959. 0x6f, 0x16, 0xd8, 0x01, 0x33, 0xc8, 0xbb, 0x7f, 0x7d, 0xfa,
  37960. 0x73, 0xf6, 0x7d, 0x54, 0xf2, 0xd4, 0x8a, 0x53, 0xe1, 0x62,
  37961. 0x45, 0xf4, 0x01, 0xa6, 0x31, 0x6b, 0x3a, 0x06, 0x56, 0xfd,
  37962. 0x79, 0x7f, 0x58, 0xd8, 0x47, 0x33, 0x53, 0xc5, 0x78, 0x70,
  37963. 0xce, 0x81, 0x7f, 0x66, 0xa1, 0x58, 0x7c, 0x5a, 0xdb, 0x4a,
  37964. 0xad, 0x29, 0xff, 0x93, 0x75, 0x95, 0x35, 0xa9, 0xd2, 0xb1,
  37965. 0xeb, 0xa0, 0x4f, 0x10, 0x0a, 0xc9, 0x38, 0x69, 0xc8, 0x8d,
  37966. 0x57, 0xef, 0x99, 0x0f, 0xa5, 0x69, 0x86, 0xa6, 0xfb, 0x2b,
  37967. 0x37, 0xe4, 0xc7, 0xab, 0x3e, 0xcd, 0x8f, 0x3f, 0x93, 0x8c,
  37968. 0x0b, 0xc4, 0x4d, 0x16, 0xe0, 0xb0, 0x94, 0x5a, 0x0d, 0x17,
  37969. 0xaf, 0x6e, 0x4b, 0x2e, 0x18, 0x29, 0x0e, 0xe0, 0xf5, 0x72,
  37970. 0x1a, 0x21, 0x37, 0xef, 0x7d, 0x6a, 0x39, 0xe9, 0xa8, 0xd7,
  37971. 0x96, 0xd6, 0xb3, 0x7d, 0x83, 0x0c, 0x13, 0x30, 0x49, 0x03,
  37972. 0xe8, 0x6b, 0xe6, 0x77, 0xe8, 0x69, 0x48, 0x56, 0x5f, 0x39,
  37973. 0x63, 0xbc, 0x86, 0xa8, 0x26, 0xa1, 0xbd, 0x4b, 0x24, 0xbd,
  37974. 0xdd, 0xe8, 0x02, 0x64, 0xcb, 0xae, 0x24, 0x17, 0x62, 0xbd,
  37975. 0x27, 0xa7, 0x22, 0x60, 0x51, 0x0c, 0x53, 0xff, 0x9d, 0x63,
  37976. 0x1b, 0xf9, 0xff, 0x76, 0x3b, 0x74, 0x05, 0x98, 0x46, 0x0b,
  37977. 0xe8, 0xcb, 0xd4, 0x0a, 0xcd, 0x91, 0xdb, 0x5b, 0x21, 0x4d,
  37978. 0xa1, 0x87, 0xbd, 0xb7, 0x58, 0xec, 0x28, 0x00, 0x92, 0xc2,
  37979. 0x98, 0xe4, 0x8c, 0x1f, 0x9d, 0xa4, 0x80, 0x83, 0x40, 0xb9,
  37980. 0x63, 0xfe, 0xc9, 0x18, 0x3f, 0xd6, 0xab, 0x34, 0x00, 0x2c,
  37981. 0x53, 0x40, 0x38, 0x0e, 0xb1, 0x69, 0xa8, 0xb8, 0xa9, 0x2e,
  37982. 0x9b, 0x7b, 0x89, 0x8d, 0xff, 0x86, 0x01, 0x51, 0x42, 0xde,
  37983. 0x04, 0xd6, 0x1d, 0xd1, 0x29, 0x8d, 0x42, 0x46, 0x5f, 0xd6,
  37984. 0x02, 0xde, 0x73, 0xee, 0x2d, 0xe9, 0x6e, 0xb0, 0x3f, 0xf0,
  37985. 0x47, 0x72, 0xfe, 0x45, 0xff, 0x05, 0x82, 0x2d, 0xc6, 0x4f,
  37986. 0xc9, 0xd3, 0xec, 0xf9, 0x5a, 0x22, 0x50, 0x6c, 0x4f, 0x1e,
  37987. 0xc8, 0x5f, 0xfc, 0x2c, 0x04, 0x4f, 0xdf, 0xce, 0xe4, 0x18,
  37988. 0xd2, 0xd7, 0x8b, 0x67, 0x83, 0x39, 0x96, 0x47, 0x5e, 0x5b,
  37989. 0xad, 0x7f, 0x5d, 0x42, 0x56, 0x97, 0x71, 0x39, 0x28, 0x44,
  37990. 0x9d, 0x35, 0xde, 0xde, 0x03, 0x20, 0x34, 0x44, 0xdb, 0xdf,
  37991. 0xfc, 0xff, 0x1e, 0x3d, 0x58, 0x5f, 0x7a, 0x8e, 0x90, 0xa1,
  37992. 0xd3, 0xeb, 0x0c, 0x23, 0x3f, 0x4e, 0x61, 0x77, 0x79, 0xb2,
  37993. 0xdc, 0xfb, 0x21, 0x46, 0x5c, 0x82, 0xb6, 0xf6, 0x34, 0x3c,
  37994. 0x3f, 0x45, 0x4b, 0x80, 0x9e, 0xa4, 0xe6, 0x02, 0x13, 0x38,
  37995. 0x40, 0x7e, 0x87, 0x92, 0x96, 0x51, 0x63, 0x87, 0xae, 0xc8,
  37996. 0x02, 0x6a, 0x70, 0xc8, 0xcd, 0xd0, 0xe2, 0x00, 0x00, 0x00,
  37997. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  37998. 0x12, 0x1c, 0x22, 0x2b, 0x33, 0x38, 0x3f,
  37999. };
  38000. return dilithium_param_vfy_test(WC_ML_DSA_87, ml_dsa_87_pub_key,
  38001. (word32)sizeof(ml_dsa_87_pub_key), ml_dsa_87_sig,
  38002. (word32)sizeof(ml_dsa_87_sig));
  38003. }
  38004. #endif
  38005. #endif
  38006. #ifndef WOLFSSL_DILITHIUM_NO_MAKE_KEY
  38007. static wc_test_ret_t dilithium_param_test(int param, WC_RNG* rng)
  38008. {
  38009. wc_test_ret_t ret;
  38010. dilithium_key* key;
  38011. byte* sig = NULL;
  38012. #ifndef WOLFSSL_DILITHIUM_NO_SIGN
  38013. word32 sigLen;
  38014. byte msg[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  38015. #ifndef WOLFSSL_DILITHIUM_NO_VERIFY
  38016. int res = 0;
  38017. #endif
  38018. #endif
  38019. key = (dilithium_key*)XMALLOC(sizeof(*key), HEAP_HINT,
  38020. DYNAMIC_TYPE_TMP_BUFFER);
  38021. if (key == NULL) {
  38022. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  38023. }
  38024. sig = (byte*)XMALLOC(DILITHIUM_MAX_SIG_SIZE, HEAP_HINT,
  38025. DYNAMIC_TYPE_TMP_BUFFER);
  38026. if (sig == NULL) {
  38027. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  38028. }
  38029. ret = wc_dilithium_init(key);
  38030. if (ret != 0) {
  38031. ret = WC_TEST_RET_ENC_EC(ret);
  38032. return ret;
  38033. }
  38034. ret = wc_dilithium_set_level(key, param);
  38035. if (ret != 0)
  38036. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38037. ret = wc_dilithium_make_key(key, rng);
  38038. if (ret != 0)
  38039. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38040. #ifndef WOLFSSL_DILITHIUM_NO_SIGN
  38041. sigLen = wc_dilithium_sig_size(key);
  38042. if (sigLen <= 0)
  38043. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38044. ret = wc_dilithium_sign_msg(msg, (word32)sizeof(msg), sig, &sigLen, key,
  38045. rng);
  38046. if (ret != 0)
  38047. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38048. #ifndef WOLFSSL_DILITHIUM_NO_VERIFY
  38049. ret = wc_dilithium_verify_msg(sig, sigLen, msg, (word32)sizeof(msg), &res,
  38050. key);
  38051. if (ret != 0)
  38052. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38053. if (res != 1)
  38054. ERROR_OUT(WC_TEST_RET_ENC_EC(res), out);
  38055. #endif
  38056. #endif
  38057. out:
  38058. wc_dilithium_free(key);
  38059. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38060. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38061. return ret;
  38062. }
  38063. #endif
  38064. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dilithium_test(void)
  38065. {
  38066. wc_test_ret_t ret;
  38067. WC_RNG rng;
  38068. #ifndef HAVE_FIPS
  38069. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  38070. #else
  38071. ret = wc_InitRng(&rng);
  38072. #endif
  38073. if (ret != 0) {
  38074. ret = WC_TEST_RET_ENC_EC(ret);
  38075. return ret;
  38076. }
  38077. #ifndef WOLFSSL_NO_ML_DSA_44
  38078. #ifndef WOLFSSL_DILITHIUM_NO_VERIFY
  38079. ret = dilithium_param_44_vfy_test();
  38080. if (ret != 0)
  38081. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38082. #endif
  38083. #ifndef WOLFSSL_DILITHIUM_NO_MAKE_KEY
  38084. ret = dilithium_param_test(WC_ML_DSA_44, &rng);
  38085. if (ret != 0)
  38086. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38087. #endif
  38088. #endif
  38089. #ifndef WOLFSSL_NO_ML_DSA_65
  38090. #ifndef WOLFSSL_DILITHIUM_NO_VERIFY
  38091. ret = dilithium_param_65_vfy_test();
  38092. if (ret != 0)
  38093. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38094. #endif
  38095. #ifndef WOLFSSL_DILITHIUM_NO_MAKE_KEY
  38096. ret = dilithium_param_test(WC_ML_DSA_65, &rng);
  38097. if (ret != 0)
  38098. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38099. #endif
  38100. #endif
  38101. #ifndef WOLFSSL_NO_ML_DSA_87
  38102. #ifndef WOLFSSL_DILITHIUM_NO_VERIFY
  38103. ret = dilithium_param_87_vfy_test();
  38104. if (ret != 0)
  38105. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38106. #endif
  38107. #ifndef WOLFSSL_DILITHIUM_NO_MAKE_KEY
  38108. ret = dilithium_param_test(WC_ML_DSA_87, &rng);
  38109. if (ret != 0)
  38110. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  38111. #endif
  38112. #endif
  38113. #if !defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY) || \
  38114. !defined(WOLFSSL_DILITHIUM_NO_VERIFY)
  38115. out:
  38116. #endif
  38117. wc_FreeRng(&rng);
  38118. return ret;
  38119. }
  38120. #endif /* HAVE_DILITHIUM */
  38121. #if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  38122. static enum wc_XmssRc xmss_write_key_mem(const byte * priv, word32 privSz,
  38123. void *context)
  38124. {
  38125. /* WARNING: THIS IS AN INSECURE WRITE CALLBACK THAT SHOULD ONLY
  38126. * BE USED FOR TESTING PURPOSES! Production applications should
  38127. * write only to non-volatile storage. */
  38128. XMEMCPY(context, priv, privSz);
  38129. return WC_XMSS_RC_SAVED_TO_NV_MEMORY;
  38130. }
  38131. static enum wc_XmssRc xmss_read_key_mem(byte * priv, word32 privSz,
  38132. void *context)
  38133. {
  38134. /* WARNING: THIS IS AN INSECURE READ CALLBACK THAT SHOULD ONLY
  38135. * BE USED FOR TESTING PURPOSES! */
  38136. XMEMCPY(priv, context, privSz);
  38137. return WC_XMSS_RC_READ_TO_MEMORY;
  38138. }
  38139. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t xmss_test(void)
  38140. {
  38141. int i = 0;
  38142. int j = 0;
  38143. XmssKey signingKey;
  38144. XmssKey verifyKey;
  38145. WC_RNG rng;
  38146. word32 pkSz = 0;
  38147. word32 skSz = 0;
  38148. word32 sigSz = 0;
  38149. word32 bufSz = 0;
  38150. unsigned char * sk = NULL;
  38151. unsigned char * old_sk = NULL;
  38152. const char * msg = "XMSS post quantum signature test";
  38153. word32 msgSz = (word32) XSTRLEN(msg);
  38154. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10
  38155. const char * param = "XMSS-SHA2_10_256";
  38156. #elif WOLFSSL_XMSS_MIN_HEIGHT <= 20
  38157. const char * param = "XMSSMT-SHA2_20/4_256";
  38158. #elif WOLFSSL_XMSS_MIN_HEIGHT <= 40
  38159. const char * param = "XMSSMT-SHA2_40/8_256";
  38160. #else
  38161. const char * param = "XMSSMT-SHA2_60/12_256";
  38162. #endif
  38163. byte * sig = NULL;
  38164. int ret2 = -1;
  38165. int ret = WC_TEST_RET_ENC_NC;
  38166. WOLFSSL_ENTER("xmss_test");
  38167. #ifndef HAVE_FIPS
  38168. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  38169. #else
  38170. ret = wc_InitRng(&rng);
  38171. #endif
  38172. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38173. ret = wc_XmssKey_Init(&signingKey, NULL, INVALID_DEVID);
  38174. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38175. ret = wc_XmssKey_Init(&verifyKey, NULL, INVALID_DEVID);
  38176. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38177. /* Set the parameter string to the signing key, and
  38178. * get sizes for secret key, pub key, and signature. */
  38179. ret = wc_XmssKey_SetParamStr(&signingKey, param);
  38180. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38181. ret = wc_XmssKey_GetPubLen(&signingKey, &pkSz);
  38182. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38183. if (pkSz != XMSS_SHA256_PUBLEN) {
  38184. return WC_TEST_RET_ENC_EC(pkSz);
  38185. }
  38186. ret = wc_XmssKey_GetPrivLen(&signingKey, &skSz);
  38187. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38188. ret = wc_XmssKey_GetSigLen(&signingKey, &sigSz);
  38189. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38190. /* Allocate signature array. */
  38191. sig = (byte *)XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38192. if (sig == NULL) { return WC_TEST_RET_ENC_ERRNO; }
  38193. bufSz = sigSz;
  38194. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  38195. fprintf(stderr, "param: %s\n", param);
  38196. fprintf(stderr, "pkSz: %d\n", pkSz);
  38197. fprintf(stderr, "skSz: %d\n", skSz);
  38198. fprintf(stderr, "sigSz: %d\n", sigSz);
  38199. #endif
  38200. /* Allocate current and old secret keys.*/
  38201. sk = (unsigned char *)XMALLOC(skSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38202. if (sk == NULL) { return WC_TEST_RET_ENC_ERRNO; }
  38203. old_sk = (unsigned char *)XMALLOC(skSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38204. if (old_sk == NULL) { return WC_TEST_RET_ENC_ERRNO; }
  38205. XMEMSET(sk, 0, skSz);
  38206. XMEMSET(old_sk, 0, skSz);
  38207. XMEMSET(sig, 0, sigSz);
  38208. ret = wc_XmssKey_SetWriteCb(&signingKey, xmss_write_key_mem);
  38209. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38210. ret = wc_XmssKey_SetReadCb(&signingKey, xmss_read_key_mem);
  38211. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38212. ret = wc_XmssKey_SetContext(&signingKey, (void *) sk);
  38213. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38214. ret = wc_XmssKey_MakeKey(&signingKey, &rng);
  38215. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38216. /* Export the pub to a verify key. */
  38217. ret = wc_XmssKey_ExportPub(&verifyKey, &signingKey);
  38218. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38219. /* Repeat a few times to check that:
  38220. * 1. The secret key is mutated on each sign.
  38221. * 2. We can verify each new signature.
  38222. * Only do a few times, because the full signature space
  38223. * for this parameter set is huge. */
  38224. for (i = 0; i < 10; ++i) {
  38225. XMEMCPY(old_sk, sk, skSz);
  38226. ret = wc_XmssKey_Sign(&signingKey, sig, &sigSz, (byte *) msg, msgSz);
  38227. if (ret != 0) { return WC_TEST_RET_ENC_I(i); }
  38228. if (sigSz != bufSz) { return WC_TEST_RET_ENC_I(i); }
  38229. /* Old secret key and current secret key should not match. */
  38230. ret = XMEMCMP(old_sk, sk, skSz);
  38231. if (ret == 0) { return WC_TEST_RET_ENC_I(i); }
  38232. ret = wc_XmssKey_Verify(&verifyKey, sig, sigSz, (byte *) msg, msgSz);
  38233. if (ret != 0) { return WC_TEST_RET_ENC_I(i); }
  38234. /* Flip bits in a few places throughout the signature, stepping in multiple
  38235. * of hash size. These should all fail with -1. */
  38236. for (j = 0; j < (int) sigSz; j+= 4 * 32) {
  38237. sig[j] ^= 1;
  38238. ret2 = wc_XmssKey_Verify(&verifyKey, sig, sigSz, (byte *) msg,
  38239. msgSz);
  38240. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  38241. /* Verify passed when it should have failed. */
  38242. return WC_TEST_RET_ENC_I(j);
  38243. }
  38244. /* Flip this spot back. */
  38245. sig[j] ^= 1;
  38246. }
  38247. }
  38248. /* Cleanup everything. */
  38249. if (sig != NULL) {
  38250. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38251. sig = NULL;
  38252. }
  38253. if (sk != NULL) {
  38254. XFREE(sk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38255. sk = NULL;
  38256. }
  38257. if (old_sk != NULL) {
  38258. XFREE(old_sk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38259. old_sk = NULL;
  38260. }
  38261. wc_XmssKey_Free(&signingKey);
  38262. wc_FreeRng(&rng);
  38263. return ret;
  38264. }
  38265. #endif /*if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY)*/
  38266. #if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_SMALL_STACK) && \
  38267. WOLFSSL_XMSS_MIN_HEIGHT <= 10
  38268. /* A simple xmss verify only test using:
  38269. * XMSS-SHA2_10_256
  38270. * pub len: 68
  38271. * msg len: 32
  38272. * sig len: 2500
  38273. *
  38274. * These were generated with the test xmss_fast, from the unpatched
  38275. * xmss-reference repository:
  38276. * https://github.com/XMSS/xmss-reference
  38277. * */
  38278. static const byte xmss_pub[XMSS_SHA256_PUBLEN] =
  38279. {
  38280. 0x00,0x00,0x00,0x01,0xA5,0x41,0x31,0x96,
  38281. 0x0A,0xF9,0xF3,0xB2,0x4B,0x2E,0x5B,0x3E,
  38282. 0xCA,0x74,0xAD,0x6C,0xA5,0x89,0xAD,0x2C,
  38283. 0x0E,0x96,0xB3,0x54,0xFB,0x5B,0x63,0x50,
  38284. 0x96,0x81,0xE2,0x59,0x72,0x10,0x09,0x54,
  38285. 0xBB,0x39,0xAC,0xEE,0x78,0xEF,0x95,0xEC,
  38286. 0x01,0x1D,0xF0,0x36,0x68,0xE2,0xC4,0xA5,
  38287. 0x2F,0x60,0x42,0x7E,0xD3,0x8E,0xAA,0x27,
  38288. 0xC9,0xB7,0x39,0x4E
  38289. };
  38290. static /* not const */ byte xmss_msg[32] =
  38291. {
  38292. 0x07,0x9F,0x80,0x86,0xDB,0x76,0x27,0xDF,
  38293. 0xED,0x5B,0x2A,0x81,0x60,0x60,0x7D,0xB4,
  38294. 0xE8,0x7A,0x69,0x45,0x20,0x6B,0xA2,0x96,
  38295. 0xC0,0x21,0xA5,0x46,0x29,0x63,0x9B,0x37
  38296. };
  38297. /* This was actually the 5th signature produced from
  38298. * xmss_fast test in xmss-reference. */
  38299. static /* not const */ byte xmss_sig[2500] =
  38300. {
  38301. 0x00,0x00,0x00,0x05,0xF0,0x15,0x34,0xBA,
  38302. 0x92,0x03,0x6A,0xB9,0xA5,0x23,0x86,0x11,
  38303. 0xAE,0x65,0x0A,0x5C,0x78,0x2C,0xC9,0xBE,
  38304. 0x7E,0xA6,0xDC,0xA2,0x8B,0xA9,0x9C,0x50,
  38305. 0xF6,0x61,0x8D,0x9D,0xD7,0xE9,0xC0,0xF8,
  38306. 0x67,0xCD,0x8A,0xC4,0x9B,0x74,0x96,0x07,
  38307. 0x5D,0xF2,0xC9,0xCC,0x28,0x05,0xB1,0xBE,
  38308. 0x5E,0xA4,0xBA,0xBE,0xAB,0xD8,0x21,0x6B,
  38309. 0x21,0x5F,0xAB,0xB7,0x6C,0xEC,0x2F,0xC8,
  38310. 0xC6,0x74,0x3E,0x97,0x1B,0xC3,0x45,0x57,
  38311. 0xAF,0xAA,0x1E,0xA8,0xF2,0x86,0xA8,0xAA,
  38312. 0x43,0x6D,0x66,0xE9,0x81,0x14,0xDE,0x09,
  38313. 0x39,0xD2,0xAF,0xD1,0x4C,0xE7,0x75,0x18,
  38314. 0x0D,0xAA,0x29,0xA1,0x92,0x53,0xCC,0xE9,
  38315. 0xF3,0x0B,0x1E,0x3B,0xE2,0xAE,0x80,0x0C,
  38316. 0xE7,0x7A,0x7C,0x13,0x8A,0x28,0xC6,0x5F,
  38317. 0x0A,0xA4,0xA3,0x73,0x0A,0x3A,0xC2,0xA6,
  38318. 0x3B,0xB4,0x30,0x67,0xC0,0x36,0x18,0xA1,
  38319. 0x58,0xCD,0xAD,0x54,0x36,0x64,0xCE,0xFD,
  38320. 0x52,0xFF,0x70,0x7E,0x09,0xFB,0x13,0xA2,
  38321. 0xEA,0xDF,0x67,0x8D,0x6C,0x42,0xB2,0x78,
  38322. 0xF5,0x7D,0x5C,0x4B,0xF7,0x8E,0xCF,0x3E,
  38323. 0xB7,0xC6,0xC1,0x23,0xFA,0x65,0xDE,0xD2,
  38324. 0xFA,0x40,0x51,0x97,0x0D,0x52,0x32,0x76,
  38325. 0x7E,0x82,0x8D,0xD0,0xB9,0x1E,0x62,0xD9,
  38326. 0x1E,0xC1,0xDB,0x40,0x43,0x37,0x4A,0x23,
  38327. 0x8A,0x1D,0x35,0xFA,0xF4,0x53,0x11,0x5A,
  38328. 0xB5,0x6D,0x1E,0x8B,0x22,0xC8,0x7D,0x2A,
  38329. 0xE4,0x94,0xAA,0x25,0x20,0x40,0x96,0xDB,
  38330. 0x82,0x62,0xBA,0x8F,0x8B,0x45,0xCB,0x4F,
  38331. 0x35,0x88,0x33,0xEB,0xEF,0xB3,0xBA,0xA7,
  38332. 0x09,0x72,0xB3,0x4C,0xEC,0xF2,0xC3,0xC7,
  38333. 0x5E,0x02,0x6C,0x41,0x93,0xCB,0x3C,0x89,
  38334. 0x12,0x09,0x68,0x54,0x8E,0xEC,0x6A,0x7E,
  38335. 0x20,0xE1,0x70,0x3D,0x8C,0xEB,0xB4,0x36,
  38336. 0xBE,0x91,0xBE,0x97,0xB5,0xA6,0x34,0x16,
  38337. 0x95,0x0F,0x10,0x26,0xA9,0x13,0x80,0x88,
  38338. 0x9C,0xAA,0x68,0xEC,0x34,0x70,0x4A,0x15,
  38339. 0x9B,0x5E,0x57,0x05,0x87,0x1C,0xF8,0x35,
  38340. 0x45,0x29,0xE9,0x6E,0xF2,0x70,0x13,0x42,
  38341. 0x89,0x4E,0x77,0xC0,0x18,0xC7,0x55,0x6D,
  38342. 0xE7,0xFA,0x0D,0x63,0x83,0x16,0x19,0x01,
  38343. 0x2D,0xFD,0x31,0x14,0x94,0xCA,0x3E,0x0E,
  38344. 0xD6,0x11,0x34,0x81,0x57,0x58,0xEC,0x24,
  38345. 0xA4,0x17,0x63,0xD3,0x25,0x00,0xBF,0x7D,
  38346. 0x78,0x5D,0xC5,0xD8,0xC6,0xC1,0xBD,0x8C,
  38347. 0xD0,0x94,0x0A,0xB1,0x33,0xA5,0x4B,0x31,
  38348. 0x25,0xF5,0xAF,0xE7,0x84,0x26,0xAA,0x05,
  38349. 0xBB,0xF3,0x9A,0xAF,0x58,0x36,0x40,0xEF,
  38350. 0x3D,0xA2,0xBD,0xCA,0xA1,0x8D,0x2F,0x6D,
  38351. 0x54,0xD2,0x62,0x33,0x09,0xAE,0xE6,0x73,
  38352. 0xD6,0x44,0xE8,0x7C,0x5C,0x39,0x2B,0x78,
  38353. 0x94,0x14,0xC7,0xC9,0xAF,0xEC,0x77,0x36,
  38354. 0xA1,0x61,0x61,0xF1,0xD0,0x09,0xA2,0xEE,
  38355. 0xE7,0x55,0xD7,0x35,0x89,0x89,0x9B,0xCF,
  38356. 0xFA,0xA6,0x09,0x1E,0x3B,0xBD,0x5D,0xD9,
  38357. 0x25,0xE7,0xED,0xDD,0x7C,0xF0,0x1C,0x57,
  38358. 0xE0,0x06,0xBB,0x08,0x39,0x59,0xDF,0xD7,
  38359. 0xAF,0x4B,0x88,0x0D,0x87,0x8F,0x4A,0xF3,
  38360. 0x1C,0xD4,0x4B,0xB3,0xE2,0xF3,0x1B,0x86,
  38361. 0x4F,0xCD,0x35,0x75,0xE2,0x03,0xF9,0x1D,
  38362. 0xBF,0x3E,0xD1,0x7B,0xC7,0x23,0x11,0x75,
  38363. 0x5F,0x92,0x0D,0x98,0xEE,0x14,0xE1,0xDA,
  38364. 0x7A,0x02,0x17,0x47,0x6B,0x41,0xEA,0x47,
  38365. 0xA1,0xAF,0x06,0x79,0x1A,0x52,0x6F,0x19,
  38366. 0x31,0x70,0x71,0xBD,0xC2,0x61,0x8D,0xB7,
  38367. 0xEE,0x6B,0x69,0x2A,0xE8,0x21,0x7A,0x95,
  38368. 0xBE,0x86,0x2A,0xA1,0xF4,0xE2,0x2F,0x17,
  38369. 0x02,0xFD,0xAD,0x17,0x9F,0x0A,0x0A,0x78,
  38370. 0xA9,0x92,0x30,0x21,0x72,0x2B,0x28,0xF8,
  38371. 0xF2,0x3E,0x05,0xD5,0xAC,0xC0,0x82,0xF8,
  38372. 0xD2,0xDA,0xD0,0xA3,0xBC,0x93,0xDB,0xA5,
  38373. 0x46,0xDE,0x14,0x1E,0xD4,0x3A,0x5D,0x79,
  38374. 0x3D,0x31,0x4B,0x06,0xCE,0x22,0x29,0x3C,
  38375. 0x98,0xB6,0x18,0x8A,0xAE,0xF7,0xBA,0x22,
  38376. 0x88,0xA1,0xEE,0xC0,0x14,0x4C,0x4A,0xA0,
  38377. 0x57,0x0A,0xD3,0x18,0xA2,0x3D,0xDD,0xC7,
  38378. 0x83,0x73,0xFC,0x38,0x9B,0x31,0xA3,0xE1,
  38379. 0x17,0x76,0xA1,0xA2,0x69,0xFC,0xAB,0x08,
  38380. 0x80,0x72,0x8D,0xF5,0xE4,0x14,0xB7,0x6B,
  38381. 0x03,0xFF,0xE8,0x11,0x4B,0x06,0x55,0x7E,
  38382. 0x36,0x21,0x2F,0xD7,0x54,0x82,0xC9,0x31,
  38383. 0xB4,0x85,0x68,0x41,0xEF,0x75,0xB0,0x3A,
  38384. 0xEA,0x4F,0xE0,0xEC,0x72,0xCC,0x33,0x96,
  38385. 0xCE,0x7D,0xAD,0xDD,0x0D,0x27,0x05,0x6E,
  38386. 0xA2,0xD4,0x11,0x07,0xD8,0x7D,0x27,0xD4,
  38387. 0x80,0x8F,0x00,0x22,0xE4,0xFC,0x2C,0x9D,
  38388. 0xD5,0xD8,0x18,0x7F,0x4E,0xF4,0xB9,0x7F,
  38389. 0xEF,0xD6,0x00,0x08,0x5C,0x05,0x04,0x1E,
  38390. 0x9A,0xC6,0x8D,0xCC,0x19,0xD9,0x0B,0x06,
  38391. 0xCC,0x6A,0x17,0xE2,0x03,0x23,0xDB,0x1C,
  38392. 0xBC,0xA2,0xB9,0xA2,0x95,0x3C,0x73,0xD8,
  38393. 0xFF,0xE6,0x0E,0xAE,0x04,0xB2,0xFC,0x91,
  38394. 0x4F,0xEF,0x8A,0x58,0xB7,0x31,0x68,0x4C,
  38395. 0x1E,0xD0,0x5B,0x85,0xCC,0x03,0xDC,0xF4,
  38396. 0xAC,0xDB,0x03,0x9B,0x35,0x33,0x08,0x71,
  38397. 0xD0,0x50,0x8D,0xDC,0xE3,0x3A,0x98,0x40,
  38398. 0x41,0x80,0xDD,0x35,0xE1,0xA2,0xAF,0x14,
  38399. 0x9A,0xDB,0xD3,0x68,0x14,0xE2,0x50,0x7A,
  38400. 0x76,0x3F,0xE4,0xA4,0x1B,0xAA,0xC1,0x06,
  38401. 0x87,0x9A,0x92,0xF9,0xBE,0x9E,0x86,0x8C,
  38402. 0x92,0x1D,0x74,0xB1,0x7F,0x27,0x43,0xC0,
  38403. 0xEE,0x2E,0xC2,0x6C,0x6D,0xAA,0x0C,0x0E,
  38404. 0x71,0xC9,0x56,0xD6,0x3A,0x56,0xCB,0x90,
  38405. 0xD1,0x7E,0x6E,0x1C,0x6A,0x00,0x2D,0x02,
  38406. 0x2C,0x96,0xF0,0x2A,0x37,0x37,0x18,0x07,
  38407. 0x0B,0xF4,0xB4,0x8C,0x30,0xF2,0xA4,0xAB,
  38408. 0x66,0xFB,0x8B,0x22,0xC0,0x00,0x7E,0x05,
  38409. 0xB6,0xF9,0x95,0x49,0x33,0xA1,0xDC,0x97,
  38410. 0x0C,0x5C,0x61,0x46,0xE2,0xD7,0x87,0x4B,
  38411. 0xC4,0xC7,0x5F,0x26,0x06,0x84,0xD7,0x47,
  38412. 0x05,0xF1,0x33,0xFF,0x85,0x85,0xB2,0xBD,
  38413. 0x1F,0x44,0xC6,0xC2,0x7D,0x51,0xBE,0x0E,
  38414. 0xB5,0xC4,0x44,0x2F,0xFE,0x73,0x5F,0xF4,
  38415. 0xA4,0xEF,0xE2,0xF1,0x73,0x0B,0xEF,0x3E,
  38416. 0x2B,0xD7,0xCC,0x9F,0xDA,0x1A,0x7E,0x92,
  38417. 0x39,0xA1,0x55,0xBF,0x60,0x0A,0xDB,0x23,
  38418. 0x74,0xFE,0xE7,0x05,0x63,0xA9,0x85,0x52,
  38419. 0x9F,0xCC,0xC3,0xFF,0xF6,0x6C,0x1B,0x4E,
  38420. 0x4F,0x01,0xBD,0xC3,0xEB,0x37,0xEC,0x29,
  38421. 0x21,0x3B,0x2C,0xC9,0x2E,0x93,0x20,0x3E,
  38422. 0x19,0xC0,0x8B,0xE8,0x33,0xCD,0xC6,0x6A,
  38423. 0x6E,0x72,0x13,0x15,0xA1,0x90,0x20,0x0C,
  38424. 0x14,0x66,0xED,0xCC,0xA4,0xDD,0x7F,0x58,
  38425. 0x53,0xBC,0x4A,0x68,0xFC,0x86,0x3E,0xAA,
  38426. 0xF1,0x17,0x0F,0x3E,0x20,0x54,0x93,0xF4,
  38427. 0x98,0xBF,0xB4,0x07,0x05,0xBD,0x70,0xE7,
  38428. 0xD7,0x34,0xFD,0xE3,0x69,0xDF,0xCD,0xF5,
  38429. 0x1A,0x73,0x6E,0xC9,0x2B,0x21,0xFB,0xB8,
  38430. 0x7E,0x44,0x10,0x83,0x56,0xCE,0xD5,0x15,
  38431. 0x9A,0x75,0xFC,0x91,0x8E,0x6B,0x9E,0x1A,
  38432. 0x3A,0x33,0x39,0x35,0xB4,0x0D,0x74,0xF4,
  38433. 0xFB,0x4C,0x0E,0x37,0xFE,0x82,0x95,0x46,
  38434. 0x6B,0xD2,0x6E,0xEE,0xCD,0x4D,0x38,0xAF,
  38435. 0x0A,0xAA,0xF1,0xD5,0xA4,0x7C,0x04,0xD8,
  38436. 0xB9,0xDB,0x11,0x68,0x88,0x35,0x41,0xDE,
  38437. 0x31,0x33,0x0C,0xDC,0x2D,0x4C,0xA8,0x20,
  38438. 0xCC,0x2C,0x4C,0x63,0xAB,0xBA,0xDF,0x48,
  38439. 0x84,0xD5,0x25,0xBC,0x70,0xE3,0x49,0xAA,
  38440. 0x43,0xCA,0x8B,0xE7,0x9F,0xDD,0x20,0x76,
  38441. 0x9B,0x38,0xF4,0xBA,0x4D,0x4E,0x34,0x4A,
  38442. 0xAF,0x81,0xE7,0x0B,0xEC,0xE9,0x59,0xC1,
  38443. 0x35,0x22,0x7F,0x69,0x46,0x62,0xD2,0x18,
  38444. 0x6E,0x1F,0x79,0xD1,0xAD,0xC3,0x84,0x95,
  38445. 0x96,0xB2,0x18,0x58,0x5E,0x7E,0x0C,0x25,
  38446. 0x0A,0x0F,0x69,0xA3,0x1D,0xEC,0x29,0xCB,
  38447. 0xDA,0xA2,0xD1,0x1A,0x10,0xA5,0x52,0xC3,
  38448. 0x62,0x1E,0xC5,0x83,0xFF,0xA3,0x56,0xC2,
  38449. 0xFD,0x87,0x3B,0x57,0x52,0x98,0x36,0x95,
  38450. 0x77,0x6B,0xE5,0x49,0x10,0x8E,0x39,0xDD,
  38451. 0xCA,0x4B,0xB3,0x9F,0x4C,0x0C,0x11,0x62,
  38452. 0xF3,0x22,0x78,0xDB,0x48,0xEB,0x68,0xFE,
  38453. 0xE4,0x2A,0xE9,0xAA,0x8F,0x7A,0x2F,0x69,
  38454. 0xA5,0xC5,0x03,0x2D,0xEF,0x62,0xA8,0x71,
  38455. 0x65,0x06,0x40,0x84,0x10,0x0F,0xF2,0xED,
  38456. 0xBC,0x70,0x71,0x69,0x24,0xA2,0xBF,0x83,
  38457. 0x39,0xDD,0xFA,0xA2,0x7B,0xE5,0xEC,0x3D,
  38458. 0xFE,0x3B,0x52,0x6E,0x3D,0x82,0xA6,0x2A,
  38459. 0x86,0x01,0x61,0x51,0x63,0xBF,0xF9,0x0A,
  38460. 0x06,0x72,0xF1,0xD5,0x39,0x0C,0xBA,0xC9,
  38461. 0x78,0xC6,0x77,0x22,0xE4,0x96,0x6E,0xB1,
  38462. 0x48,0x62,0x84,0x62,0x2D,0xEA,0x49,0x56,
  38463. 0x50,0x86,0x3F,0x90,0xC3,0x01,0x42,0x45,
  38464. 0xED,0xE6,0x9A,0x65,0x19,0x93,0x7F,0x48,
  38465. 0x16,0xF2,0x50,0xA7,0x70,0xB3,0xF5,0xDB,
  38466. 0x0E,0x5E,0x22,0x9E,0x64,0x04,0x26,0x69,
  38467. 0xC1,0x16,0xEE,0x65,0x08,0x82,0x27,0x65,
  38468. 0xEC,0x3D,0xDF,0x51,0x5E,0x2D,0xE8,0x76,
  38469. 0xF2,0xE3,0xE4,0x24,0x04,0x88,0x06,0x0F,
  38470. 0xB2,0x7B,0x9B,0x72,0x3D,0x4C,0x7D,0x6A,
  38471. 0x1F,0xB2,0xA2,0xD2,0x35,0xD6,0x40,0x25,
  38472. 0xC2,0x0B,0x25,0xF9,0xDF,0x26,0xE4,0xDC,
  38473. 0xFB,0xB1,0x84,0x84,0x77,0x1B,0x45,0x51,
  38474. 0x60,0xD5,0xF0,0xB6,0x09,0xE6,0xBC,0xE3,
  38475. 0x1C,0x70,0x96,0x2C,0xD3,0x9D,0x7D,0x7F,
  38476. 0xB1,0x70,0xDA,0x79,0xB8,0x74,0x99,0xBF,
  38477. 0x84,0x95,0xCC,0x93,0xD7,0x51,0xDD,0x66,
  38478. 0xD3,0x70,0x0C,0x75,0x86,0x09,0x06,0xFD,
  38479. 0x66,0x14,0x80,0xCD,0xF3,0x59,0xB4,0x92,
  38480. 0x5F,0xE4,0xEE,0x00,0xA8,0xB0,0x8B,0x5C,
  38481. 0x3E,0xDB,0x8A,0x9C,0x0B,0xB5,0x99,0xC2,
  38482. 0x0D,0x81,0x09,0x06,0x6C,0x28,0xC0,0x7E,
  38483. 0xA5,0x07,0x70,0x64,0xD7,0x41,0xF4,0xC3,
  38484. 0x66,0x61,0x1C,0xA8,0x51,0xF6,0x3C,0xBA,
  38485. 0xE0,0x94,0xA3,0x11,0x8C,0x2E,0xBA,0x13,
  38486. 0xB2,0x47,0x48,0x93,0xB4,0x1A,0x2C,0x9A,
  38487. 0x6E,0x8E,0x30,0x66,0x7B,0xD3,0xBB,0x3B,
  38488. 0x5D,0x97,0x0D,0xE4,0xEA,0x24,0x28,0x9E,
  38489. 0xB4,0x88,0xCE,0x1D,0x7D,0x6F,0x39,0xB3,
  38490. 0x87,0x21,0xE5,0x08,0x93,0xF0,0xD4,0x9D,
  38491. 0x2D,0x91,0xC9,0xFD,0x0C,0x74,0x34,0xB4,
  38492. 0x1F,0xFE,0xDA,0xDC,0x10,0x5B,0x8D,0x2B,
  38493. 0x87,0xD3,0x42,0xB4,0xAE,0x32,0x9C,0xAE,
  38494. 0x4C,0x99,0xD8,0xED,0x44,0x41,0x07,0xE0,
  38495. 0x8F,0xBD,0xA5,0x7C,0x5A,0xDF,0x91,0x29,
  38496. 0x00,0xB5,0x4B,0xC3,0x3A,0x40,0x6C,0x48,
  38497. 0xAB,0x2A,0xF3,0x02,0xCB,0xB3,0x69,0xDA,
  38498. 0x06,0x0C,0x4D,0x5C,0x45,0xC3,0x28,0xAC,
  38499. 0x7A,0x01,0xD4,0xF8,0xCB,0x07,0x63,0x89,
  38500. 0x09,0x34,0x78,0xA7,0x14,0x39,0xCF,0x2D,
  38501. 0x94,0x8D,0x7A,0x4E,0x4E,0xBD,0xC4,0x32,
  38502. 0xAB,0x21,0xC9,0xDA,0x3F,0x5F,0x04,0x6B,
  38503. 0x14,0x40,0x18,0x18,0x2F,0xF9,0x46,0x17,
  38504. 0x57,0x54,0x9B,0x28,0x7B,0xBD,0xF9,0xA2,
  38505. 0x13,0xAC,0x69,0x24,0xB1,0x31,0x39,0xBF,
  38506. 0x8D,0x75,0xC3,0xFD,0x03,0x54,0x5A,0xFD,
  38507. 0xD4,0x7A,0xB7,0x56,0x4F,0x66,0x43,0x57,
  38508. 0x1B,0xFB,0xF9,0x92,0x7A,0x83,0xE6,0xFF,
  38509. 0xB4,0xBA,0x83,0xD2,0x61,0x8E,0x4A,0x82,
  38510. 0x82,0xA8,0xF5,0x0C,0xD2,0x43,0x53,0xA8,
  38511. 0x85,0x0A,0xD4,0x69,0x7B,0x04,0x71,0x3B,
  38512. 0x80,0x49,0x27,0x47,0x12,0xB6,0xB0,0xEA,
  38513. 0x90,0x0A,0xFA,0xA8,0xC8,0x78,0x61,0xDE,
  38514. 0x30,0x12,0xBB,0xDC,0xA6,0x57,0x56,0x30,
  38515. 0x6E,0xF1,0xA8,0x3B,0xF6,0x09,0x07,0xEA,
  38516. 0x31,0xE2,0x08,0x23,0x31,0x0F,0xD4,0x34,
  38517. 0xE3,0x60,0xC2,0x2B,0xDB,0x5A,0x99,0xCF,
  38518. 0xD4,0x6B,0x4E,0x75,0x65,0x35,0xE8,0x8B,
  38519. 0x93,0x7D,0xCA,0x11,0x47,0xF0,0x3E,0x11,
  38520. 0x5C,0xD1,0xEE,0x4B,0x11,0xB4,0x65,0x2B,
  38521. 0x6B,0x79,0xC0,0x86,0x60,0xA4,0x4B,0x24,
  38522. 0xA0,0x5C,0x70,0x34,0xC3,0x7C,0xE7,0x4F,
  38523. 0x97,0x89,0x4D,0xFE,0x22,0x89,0x3A,0xE9,
  38524. 0x07,0xB9,0x1A,0x86,0xB8,0x7A,0x12,0x38,
  38525. 0xE1,0x24,0x46,0xBC,0x9B,0x21,0xCD,0xAC,
  38526. 0x30,0xAB,0x98,0x21,0x31,0xC5,0x17,0x3F,
  38527. 0x1E,0x56,0xC3,0x18,0xCE,0xF0,0xA1,0xCC,
  38528. 0xFF,0x9D,0xA8,0x53,0xAF,0x74,0x77,0x54,
  38529. 0x02,0x9A,0x8F,0xA4,0xD4,0xBD,0xB2,0x1A,
  38530. 0xBA,0x52,0x2E,0x19,0xBE,0x49,0x11,0x45,
  38531. 0x02,0x01,0x7A,0xBF,0x28,0xD6,0x18,0xED,
  38532. 0xBD,0xCE,0xE4,0xDE,0xB5,0xF1,0x53,0x5D,
  38533. 0x65,0xF9,0x5F,0x83,0x8F,0x2D,0xF2,0x82,
  38534. 0xA0,0x2D,0x28,0xD3,0x0A,0x9E,0x0F,0x7F,
  38535. 0xC7,0xC4,0x43,0x7F,0xC3,0x0E,0x06,0xEB,
  38536. 0x4E,0xB4,0x2D,0xFA,0xDD,0x48,0xAB,0xF4,
  38537. 0x7D,0x41,0x48,0x33,0x5A,0xE6,0x70,0x02,
  38538. 0xE7,0x71,0x8D,0xD9,0x6B,0x0C,0x5A,0x8F,
  38539. 0xA4,0xC1,0xB7,0x4E,0x96,0x83,0xD6,0xA7,
  38540. 0x1D,0xF1,0x88,0xB3,0x6E,0xF4,0x12,0xA9,
  38541. 0xF6,0x31,0x69,0x66,0xFE,0xFE,0x02,0xF2,
  38542. 0x86,0x6D,0xBB,0x57,0x51,0x8C,0x4C,0xE9,
  38543. 0x7C,0x92,0x3E,0x3A,0xD3,0x2D,0xA8,0x82,
  38544. 0x53,0x84,0x26,0x89,0xBB,0xCC,0x13,0x12,
  38545. 0x3D,0x94,0xBB,0xDF,0x3D,0x4C,0xDF,0x27,
  38546. 0x9B,0x1F,0xB8,0xB6,0xE4,0xEA,0xA2,0x07,
  38547. 0xF8,0x4D,0x42,0x8F,0x29,0x90,0xFE,0x21,
  38548. 0x20,0xE9,0x55,0x02,0xAD,0x90,0xA7,0x77,
  38549. 0x4E,0x29,0xB6,0xD9,0x14,0x94,0xB2,0x25,
  38550. 0xA4,0xB2,0x0E,0x96,0x31,0xAB,0x9E,0x93,
  38551. 0x49,0xAC,0xA9,0xCB,0x68,0x22,0xBA,0xB8,
  38552. 0x57,0x5C,0x9D,0x65,0xC1,0xF1,0xFC,0x99,
  38553. 0x7C,0x3C,0xE9,0xEA,0x4B,0x29,0x22,0x2F,
  38554. 0xDB,0x17,0x21,0x8D,0xB0,0x13,0xBF,0xEE,
  38555. 0x7D,0xE4,0x8B,0x6D,0x17,0xE0,0x53,0x92,
  38556. 0x0B,0x32,0x6B,0xB1,0x65,0x2E,0xA7,0x83,
  38557. 0xFD,0x62,0x62,0xE3,0xAA,0x81,0xE8,0xD6,
  38558. 0xF7,0xB1,0x30,0x65,0x80,0x9F,0x77,0x1E,
  38559. 0x4A,0xEA,0xE8,0x45,0x32,0x12,0x3A,0xFB,
  38560. 0x22,0xE9,0xA9,0xF6,0xCB,0xAB,0xA8,0x0C,
  38561. 0x20,0xA8,0x7C,0xF9,0xF7,0x53,0xC1,0xB4,
  38562. 0xC0,0x5D,0x06,0x45,0xDD,0x7E,0xA7,0x34,
  38563. 0xA1,0x21,0xC2,0x62,0xAB,0x22,0x45,0x3D,
  38564. 0x73,0x4C,0x26,0xD1,0x1A,0xB2,0xF0,0xB2,
  38565. 0x6D,0x11,0x70,0x58,0xAA,0xF5,0xA4,0xF5,
  38566. 0xF8,0x0B,0x3D,0xC1,0xF6,0x17,0x70,0x15,
  38567. 0xCD,0x72,0x02,0x7E,0x4E,0x94,0x96,0x0A,
  38568. 0x56,0xCC,0xA5,0xA3,0xB3,0x7E,0xDD,0x5A,
  38569. 0x72,0xD2,0xFB,0xAC,0x3D,0x0E,0x66,0x65,
  38570. 0xE9,0x08,0x6C,0xB0,0x1C,0xE2,0x1A,0x82,
  38571. 0xF6,0xF3,0x34,0x89,0x73,0x02,0x5B,0x42,
  38572. 0x6D,0x40,0x61,0xB6,0xE0,0xE6,0x53,0x32,
  38573. 0xA5,0x72,0x17,0x4F,0x3B,0x51,0x4F,0xBC,
  38574. 0x00,0xE0,0x69,0x26,0xA9,0xAE,0x83,0xE3,
  38575. 0x73,0x7F,0x71,0x97,0xE0,0xDC,0x7C,0x63,
  38576. 0x9C,0x85,0x5F,0xDF,0x7D,0xE4,0x6C,0xD8,
  38577. 0xA9,0x3A,0x6F,0x5E,0x4A,0x2E,0xB0,0xE7,
  38578. 0x8B,0x45,0xE2,0x90,0x05,0x37,0xE8,0xAB,
  38579. 0x49,0x48,0x4C,0xC0,0x59,0x1D,0x8C,0x46,
  38580. 0x5B,0x84,0xE0,0x83,0xCE,0xEA,0x4B,0xF9,
  38581. 0xD4,0xDC,0x63,0xDF,0x79,0xB7,0x5C,0x11,
  38582. 0x25,0x7F,0x90,0x2E,0x0A,0x38,0x03,0xEA,
  38583. 0xEA,0xA1,0x26,0x52,0x20,0x19,0xA3,0xBE,
  38584. 0xFC,0x9D,0xB7,0x6E,0xA6,0x58,0x8E,0x6D,
  38585. 0xC5,0x58,0xE9,0xED,0x2F,0x55,0x43,0x8B,
  38586. 0x03,0x8B,0xE6,0xA4,0xC2,0x25,0x4B,0x36,
  38587. 0xBA,0xD3,0x27,0x48,0x40,0x2E,0x87,0xA2,
  38588. 0xD4,0x12,0xC6,0x05,0x36,0x03,0x11,0x51,
  38589. 0xD1,0xF2,0xAC,0x71,0x2C,0xB6,0xC3,0xA5,
  38590. 0x57,0x0F,0xAF,0x4B,0xBD,0xCD,0x47,0x4C,
  38591. 0x3A,0x52,0x6F,0x47,0xE7,0x0B,0xB7,0xD5,
  38592. 0xF7,0xA6,0x39,0x63,0x82,0x08,0x4C,0x41,
  38593. 0x0E,0x2A,0x52,0x42,0x5A,0xEA,0x59,0xC7,
  38594. 0x94,0xFB,0xD0,0x88,0x47,0x27,0xF6,0x97,
  38595. 0x03,0x9E,0x29,0xB8,0x3A,0x67,0xE6,0xF3,
  38596. 0x95,0xA7,0x42,0xC1,0x96,0xD1,0x9A,0xA6,
  38597. 0xF0,0x09,0x0C,0xEA,0xE0,0xAB,0x0F,0x15,
  38598. 0xE9,0xC3,0xEB,0xA5,0x89,0x86,0x98,0x32,
  38599. 0x83,0xAB,0x30,0x33,0xAE,0x90,0x8D,0x2E,
  38600. 0xB3,0xAA,0x91,0xA6,0xD9,0xA4,0x4A,0x54,
  38601. 0xE0,0xD3,0x08,0xCC,0x79,0xCE,0xE4,0x15,
  38602. 0x31,0xA6,0xCE,0x61,0xCF,0x03,0x06,0xEE,
  38603. 0x8E,0xE2,0x64,0x29,0xD1,0x54,0x9B,0xD0,
  38604. 0x5F,0x09,0x2B,0x8B,0xD5,0xF8,0xD4,0x7D,
  38605. 0xF1,0x97,0x32,0xD9,0xEA,0x5A,0x0E,0x10,
  38606. 0x8C,0x4D,0xFB,0x55,0xE6,0x27,0x0C,0xBA,
  38607. 0xC1,0x73,0xC1,0x73,0xE3,0x1C,0x09,0xB3,
  38608. 0x6F,0xB4,0x12,0xFA,0xF3,0x29,0xDC,0x23,
  38609. 0x32,0xED,0x80,0x87,0x83,0xC2,0xF6,0x07,
  38610. 0xB5,0xA9,0x22,0xDE,0x66,0x1A,0xA7,0x4A,
  38611. 0x86,0xF1,0x39,0x9B,0xF4,0xE7,0x50,0x15,
  38612. 0x4A,0x55,0x3C,0x93,0xB9,0xF9,0xFD,0xDC,
  38613. 0xB3,0x5D,0x73,0x52
  38614. };
  38615. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t xmss_test_verify_only(void)
  38616. {
  38617. XmssKey verifyKey;
  38618. unsigned char pub_raw[XMSS_SHA256_PUBLEN];
  38619. word32 pub_len = sizeof(pub_raw);
  38620. word32 pkSz = 0;
  38621. word32 sigSz = 0;
  38622. const char * param = "XMSS-SHA2_10_256";
  38623. int j = 0;
  38624. int ret2 = WC_TEST_RET_ENC_NC;
  38625. int ret = WC_TEST_RET_ENC_NC;
  38626. int n_diff = 0;
  38627. WOLFSSL_ENTER("xmss_test_verify_only");
  38628. XMEMSET(pub_raw, 0, sizeof(pub_raw));
  38629. ret = wc_XmssKey_Init(&verifyKey, NULL, INVALID_DEVID);
  38630. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38631. ret = wc_XmssKey_SetParamStr(&verifyKey, param);
  38632. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38633. ret = wc_XmssKey_GetPubLen(&verifyKey, &pkSz);
  38634. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38635. if (pkSz != XMSS_SHA256_PUBLEN) {
  38636. return WC_TEST_RET_ENC_EC(pkSz);
  38637. }
  38638. ret = wc_XmssKey_GetSigLen(&verifyKey, &sigSz);
  38639. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38640. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  38641. fprintf(stderr, "param: %s\n", param);
  38642. fprintf(stderr, "pkSz: %d\n", pkSz);
  38643. fprintf(stderr, "sigSz: %d\n", sigSz);
  38644. #endif
  38645. if (sigSz != sizeof(xmss_sig)) {
  38646. return WC_TEST_RET_ENC_EC(sigSz);
  38647. }
  38648. ret = wc_XmssKey_ImportPubRaw(&verifyKey, xmss_pub, XMSS_SHA256_PUBLEN);
  38649. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38650. ret = wc_XmssKey_Verify(&verifyKey, xmss_sig, sizeof(xmss_sig),
  38651. (byte *) xmss_msg, sizeof(xmss_msg));
  38652. if (ret != 0) {
  38653. printf("error: wc_XmssKey_Verify returned %d, expected 0\n", ret);
  38654. return WC_TEST_RET_ENC_EC(ret);
  38655. }
  38656. /* Now test the ExportPubRaw API, verify we recover the original pub. */
  38657. ret = wc_XmssKey_ExportPubRaw(&verifyKey, pub_raw, &pub_len);
  38658. if (ret != 0) {
  38659. printf("error: wc_XmssKey_ExportPubRaw returned %d, expected 0\n", ret);
  38660. return WC_TEST_RET_ENC_EC(ret);
  38661. }
  38662. if (pub_len != XMSS_SHA256_PUBLEN) {
  38663. printf("error: xmss pub len %u, expected %d\n", pub_len,
  38664. XMSS_SHA256_PUBLEN);
  38665. return WC_TEST_RET_ENC_EC(pub_len);
  38666. }
  38667. n_diff = XMEMCMP(pub_raw, xmss_pub, sizeof(xmss_pub));
  38668. if (n_diff != 0) {
  38669. printf("error: exported and imported pub raw do not match: %d\n",
  38670. n_diff);
  38671. return WC_TEST_RET_ENC_EC(n_diff);
  38672. }
  38673. /* Flip bits in message. This should fail. */
  38674. xmss_msg[sizeof(xmss_msg) / 2] ^= 1;
  38675. ret2 = wc_XmssKey_Verify(&verifyKey, xmss_sig, sizeof(xmss_sig),
  38676. (byte *) xmss_msg, sizeof(xmss_msg));
  38677. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  38678. printf("error: wc_XmssKey_Verify returned %d, expected -1\n", ret2);
  38679. return WC_TEST_RET_ENC_EC(ret2);
  38680. }
  38681. /* Flip it back. This should pass again. */
  38682. xmss_msg[sizeof(xmss_msg) / 2] ^= 1;
  38683. ret = wc_XmssKey_Verify(&verifyKey, xmss_sig, sizeof(xmss_sig),
  38684. (byte *) xmss_msg, sizeof(xmss_msg));
  38685. if (ret != 0) {
  38686. printf("error: wc_XmssKey_Verify returned %d, expected 0\n", ret);
  38687. return WC_TEST_RET_ENC_EC(ret);
  38688. }
  38689. /* Flip bits in a few places throughout the signature, stepping in multiple
  38690. * of hash size. These should all fail with -1. */
  38691. for (j = 0; j < (int) sizeof(xmss_sig); j+= 4 * 32) {
  38692. xmss_sig[j] ^= 1;
  38693. ret2 = wc_XmssKey_Verify(&verifyKey, xmss_sig, sizeof(xmss_sig),
  38694. (byte *) xmss_msg, sizeof(xmss_msg));
  38695. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  38696. /* Verify passed when it should have failed. */
  38697. return WC_TEST_RET_ENC_I(j);
  38698. }
  38699. /* Flip this spot back. */
  38700. xmss_sig[j] ^= 1;
  38701. }
  38702. /* Cleanup everything. */
  38703. wc_XmssKey_Free(&verifyKey);
  38704. return ret;
  38705. }
  38706. #endif /* WOLFSSL_HAVE_XMSS && !WOLFSSL_SMALL_STACK &&
  38707. * WOLFSSL_XMSS_MIN_HEIGHT <= 10 */
  38708. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY)
  38709. static int lms_write_key_mem(const byte * priv, word32 privSz, void *context)
  38710. {
  38711. /* WARNING: THIS IS AN INSECURE WRITE CALLBACK THAT SHOULD ONLY
  38712. * BE USED FOR TESTING PURPOSES! Production applications should
  38713. * write only to non-volatile storage. */
  38714. XMEMCPY(context, priv, privSz);
  38715. return WC_LMS_RC_SAVED_TO_NV_MEMORY;
  38716. }
  38717. static int lms_read_key_mem(byte * priv, word32 privSz, void *context)
  38718. {
  38719. /* WARNING: THIS IS AN INSECURE READ CALLBACK THAT SHOULD ONLY
  38720. * BE USED FOR TESTING PURPOSES! */
  38721. XMEMCPY(priv, context, privSz);
  38722. return WC_LMS_RC_READ_TO_MEMORY;
  38723. }
  38724. /* LMS signature sizes are a function of their parameters. This
  38725. * test has a signature of 8688 bytes. */
  38726. #define WC_TEST_LMS_SIG_LEN (8688)
  38727. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t lms_test(void)
  38728. {
  38729. int i = 0;
  38730. int j = 0;
  38731. int ret = WC_TEST_RET_ENC_NC;
  38732. int ret2 = WC_TEST_RET_ENC_NC;
  38733. int sigsLeft = 0;
  38734. LmsKey signingKey;
  38735. LmsKey verifyKey;
  38736. WC_RNG rng;
  38737. word32 sigSz = 0;
  38738. const char * msg = "LMS HSS post quantum signature test";
  38739. word32 msgSz = (word32) XSTRLEN(msg);
  38740. unsigned char priv[HSS_MAX_PRIVATE_KEY_LEN];
  38741. unsigned char old_priv[HSS_MAX_PRIVATE_KEY_LEN];
  38742. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38743. byte * sig = XMALLOC(WC_TEST_LMS_SIG_LEN, HEAP_HINT,
  38744. DYNAMIC_TYPE_TMP_BUFFER);
  38745. if (sig == NULL) {
  38746. return WC_TEST_RET_ENC_ERRNO;
  38747. }
  38748. #else
  38749. byte sig[WC_TEST_LMS_SIG_LEN];
  38750. #endif
  38751. WOLFSSL_ENTER("lms_test");
  38752. XMEMSET(priv, 0, sizeof(priv));
  38753. XMEMSET(old_priv, 0, sizeof(old_priv));
  38754. XMEMSET(sig, 0, WC_TEST_LMS_SIG_LEN);
  38755. #ifndef HAVE_FIPS
  38756. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  38757. #else
  38758. ret = wc_InitRng(&rng);
  38759. #endif
  38760. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38761. /* This test:
  38762. * levels: 1
  38763. * height: 5
  38764. * winternitz: 1
  38765. *
  38766. * max sigs: 2 ** (1 * 5) = 32
  38767. * signature length: 8688
  38768. */
  38769. ret = wc_LmsKey_Init(&signingKey, NULL, INVALID_DEVID);
  38770. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38771. ret = wc_LmsKey_Init(&verifyKey, NULL, INVALID_DEVID);
  38772. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38773. ret = wc_LmsKey_SetParameters(&signingKey, 1, 5, 1);
  38774. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38775. ret = wc_LmsKey_SetWriteCb(&signingKey, lms_write_key_mem);
  38776. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38777. ret = wc_LmsKey_SetReadCb(&signingKey, lms_read_key_mem);
  38778. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38779. ret = wc_LmsKey_SetContext(&signingKey, (void *) priv);
  38780. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38781. ret = wc_LmsKey_MakeKey(&signingKey, &rng);
  38782. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38783. XMEMCPY(old_priv, priv, sizeof(priv));
  38784. ret = wc_LmsKey_ExportPub(&verifyKey, &signingKey);
  38785. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38786. ret = wc_LmsKey_GetSigLen(&verifyKey, &sigSz);
  38787. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  38788. if (sigSz != WC_TEST_LMS_SIG_LEN) {
  38789. printf("error: got %u, expected %d\n", sigSz, WC_TEST_LMS_SIG_LEN);
  38790. return WC_TEST_RET_ENC_EC(sigSz);
  38791. }
  38792. /* 2 ** 5 should be the max number of signatures */
  38793. for (i = 0; i < 32; ++i) {
  38794. /* We should have remaining signstures. */
  38795. sigsLeft = wc_LmsKey_SigsLeft(&signingKey);
  38796. if (sigsLeft == 0) {
  38797. return WC_TEST_RET_ENC_EC(sigsLeft);
  38798. }
  38799. /* Sign with key. The private key will be updated on every signature. */
  38800. ret = wc_LmsKey_Sign(&signingKey, sig, &sigSz, (byte *) msg, msgSz);
  38801. if (ret != 0) { return WC_TEST_RET_ENC_I(i); }
  38802. /* The updated private key should not match the old one. */
  38803. if (XMEMCMP(old_priv, priv, sizeof(priv)) == 0) {
  38804. printf("error: current priv key should not match old: %d\n", i);
  38805. return WC_TEST_RET_ENC_I(i);
  38806. }
  38807. XMEMCPY(old_priv, priv, sizeof(priv));
  38808. ret = wc_LmsKey_Verify(&verifyKey, sig, sigSz, (byte *) msg, msgSz);
  38809. if (ret != 0) { return WC_TEST_RET_ENC_I(i); }
  38810. /* Flip bits in a few places throughout the signature, stepping in multiple
  38811. * of hash size. These should all fail with -1. */
  38812. for (j = 0; j < (int) sigSz; j+= 4 * 32) {
  38813. sig[j] ^= 1;
  38814. ret2 = wc_LmsKey_Verify(&verifyKey, sig, sigSz, (byte *) msg,
  38815. msgSz);
  38816. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  38817. /* Verify passed when it should have failed. */
  38818. return WC_TEST_RET_ENC_I(j);
  38819. }
  38820. /* Flip this spot back. */
  38821. sig[j] ^= 1;
  38822. }
  38823. }
  38824. /* This should be the last signature. */
  38825. sigsLeft = wc_LmsKey_SigsLeft(&signingKey);
  38826. if (sigsLeft != 0) {
  38827. return WC_TEST_RET_ENC_EC(sigsLeft);
  38828. }
  38829. wc_LmsKey_Free(&signingKey);
  38830. wc_LmsKey_Free(&verifyKey);
  38831. wc_FreeRng(&rng);
  38832. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38833. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38834. #endif
  38835. return ret;
  38836. }
  38837. #endif /* if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY) */
  38838. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_SMALL_STACK)
  38839. #if (defined(WOLFSSL_WC_LMS) && (LMS_MAX_HEIGHT >= 10)) || \
  38840. defined(HAVE_LIBLMS)
  38841. /* A simple LMS verify only test.
  38842. *
  38843. * Note: LMS signature sizes are a function of their parameters. This
  38844. * test has a signature of 1456 bytes:
  38845. * levels: 1
  38846. * height: 10
  38847. * winternitz: 8
  38848. * max sigs: 2 ** (1 * 10) = 1024
  38849. * signature length: 1456
  38850. * */
  38851. /* "wolfSSL LMS example message!" without null terminator. */
  38852. static byte lms_msg[28] =
  38853. {
  38854. 0x77,0x6F,0x6C,0x66,0x53,0x53,0x4C,0x20,
  38855. 0x4C,0x4D,0x53,0x20,0x65,0x78,0x61,0x6D,
  38856. 0x70,0x6C,0x65,0x20,0x6D,0x65,0x73,0x73,
  38857. 0x61,0x67,0x65,0x21
  38858. };
  38859. static const byte lms_L1H10W8_pub[HSS_MAX_PUBLIC_KEY_LEN] =
  38860. {
  38861. 0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x06,
  38862. 0x00,0x00,0x00,0x04,0xA1,0x26,0x76,0xF8,
  38863. 0xBB,0x0B,0xC0,0x82,0x21,0x71,0x0B,0x2E,
  38864. 0x8C,0xA6,0xEF,0x12,0xED,0x41,0x0E,0x8C,
  38865. 0xAF,0x11,0x93,0x34,0x7B,0x49,0x79,0xB7,
  38866. 0xDE,0x63,0x1C,0xFE,0x1F,0xD1,0x17,0x49,
  38867. 0xCD,0x5C,0xD4,0x26,0xA0,0x53,0x26,0x1A,
  38868. 0xC5,0xB4,0x8F,0x23
  38869. };
  38870. #define LMS_L1H10W8_SIGLEN (1456)
  38871. static byte lms_L1H10W8_sig[LMS_L1H10W8_SIGLEN] =
  38872. {
  38873. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
  38874. 0x00,0x00,0x00,0x04,0x18,0x70,0x09,0x2E,
  38875. 0x21,0xC9,0x6A,0xC9,0x5C,0xB6,0xB0,0xAA,
  38876. 0xC3,0xED,0x6E,0x66,0x2F,0xCC,0x45,0x81,
  38877. 0xBC,0xBA,0x44,0x96,0x1C,0xBF,0x4E,0xFB,
  38878. 0x7A,0x46,0xFB,0xBE,0x9A,0x0C,0xE4,0x50,
  38879. 0x90,0xC7,0x92,0xAC,0x53,0xAE,0x53,0x76,
  38880. 0x29,0xA6,0x65,0xF1,0x09,0xED,0x1A,0x8E,
  38881. 0x03,0x2E,0x5A,0x06,0x51,0xE3,0x1E,0xE6,
  38882. 0xF6,0xFE,0x3A,0x6E,0xD1,0x92,0x31,0x1D,
  38883. 0xA1,0x6A,0x5C,0x30,0x3A,0xC7,0xFD,0x5B,
  38884. 0xFE,0x71,0x2C,0x5C,0x2F,0x5B,0x5B,0xCF,
  38885. 0xBC,0x7F,0xBF,0x6C,0xAF,0x44,0x8A,0xAE,
  38886. 0x14,0x60,0xAB,0x88,0xED,0x0E,0x4F,0xF8,
  38887. 0xC7,0x1B,0x74,0x28,0x72,0xB3,0x96,0xA6,
  38888. 0xE6,0x46,0x22,0x82,0xCF,0x1F,0x4D,0xA6,
  38889. 0xEA,0x22,0x06,0x07,0x52,0xF5,0x26,0x16,
  38890. 0x0B,0x90,0xE3,0xFF,0x64,0xA9,0xE4,0x61,
  38891. 0x1E,0x9C,0x12,0x9C,0xF6,0xD4,0x63,0x29,
  38892. 0xEA,0x02,0xF7,0x18,0x52,0x79,0x6C,0x43,
  38893. 0xDC,0xCF,0x43,0x23,0xB9,0xCC,0x4A,0x25,
  38894. 0x9D,0x10,0xAF,0xA3,0xE6,0x47,0x5A,0x1C,
  38895. 0xFE,0x68,0x89,0xAF,0x1B,0x2D,0x88,0x3E,
  38896. 0xCA,0xDC,0x70,0xEA,0xAC,0x11,0x00,0x8A,
  38897. 0x6E,0xE0,0xC7,0xD0,0xD2,0x1A,0x36,0x18,
  38898. 0x97,0xB3,0x5F,0x0E,0x75,0x48,0x28,0xF8,
  38899. 0xA8,0xF5,0x90,0xD1,0xA1,0x84,0xFB,0xA4,
  38900. 0xAD,0x50,0xBE,0xE9,0x39,0x8C,0xC5,0xA1,
  38901. 0x67,0x51,0xA1,0x8C,0xD6,0x6B,0x97,0x1F,
  38902. 0x47,0x99,0xEE,0xE0,0x70,0x01,0xC7,0x07,
  38903. 0x50,0xF3,0x5E,0x3F,0xE7,0x06,0xD6,0x8D,
  38904. 0x26,0xD6,0x5A,0x59,0x18,0x72,0x6B,0x12,
  38905. 0xD2,0xAF,0x9B,0xB4,0x2B,0xD0,0xB2,0xF2,
  38906. 0x96,0x2F,0x40,0xEA,0xBE,0xE6,0xAC,0x1F,
  38907. 0xB8,0x33,0xC2,0x76,0xDC,0x8C,0xAC,0xC1,
  38908. 0x46,0x5E,0x04,0x84,0x1B,0xC8,0xB9,0x65,
  38909. 0x8D,0xAD,0x96,0xB5,0xB1,0xF6,0x17,0x4A,
  38910. 0x19,0x87,0xE7,0xBF,0x29,0xC7,0x9B,0xB9,
  38911. 0xD6,0x11,0x2C,0x92,0x2F,0xB7,0x24,0xD5,
  38912. 0x01,0x1D,0x80,0x37,0x54,0xED,0x33,0x32,
  38913. 0xAB,0x7A,0x12,0xD4,0x02,0x1D,0x27,0x52,
  38914. 0x89,0xDB,0x32,0xBF,0x61,0xD4,0xBB,0xB4,
  38915. 0x46,0x78,0x1B,0x64,0x17,0x84,0x4B,0x8A,
  38916. 0xBA,0xC6,0xC1,0xCF,0xC7,0x5D,0x8F,0x93,
  38917. 0xC5,0x9A,0x27,0x90,0xAC,0x17,0x98,0xFF,
  38918. 0xC8,0x22,0x59,0x55,0x90,0xB2,0x29,0x39,
  38919. 0xA0,0xBE,0x00,0x23,0x55,0x6B,0xDA,0x83,
  38920. 0xD8,0x5B,0x57,0x7C,0x67,0x1B,0xC3,0x6B,
  38921. 0x6D,0xC7,0x9B,0x2B,0x9E,0xB7,0x95,0xB3,
  38922. 0xF0,0x1B,0x89,0x5A,0xD7,0x4B,0x67,0xAF,
  38923. 0xDC,0x9E,0xCF,0x7E,0x1A,0xBA,0x1B,0xB9,
  38924. 0x3B,0x7A,0xDD,0x3F,0x0D,0xEE,0x4C,0x0B,
  38925. 0xD1,0x4F,0x34,0xF2,0x93,0xF7,0x21,0x64,
  38926. 0x2C,0x07,0x00,0x15,0x4F,0xE3,0x6A,0x9F,
  38927. 0x08,0x52,0xC2,0x65,0x47,0x1F,0x34,0x64,
  38928. 0x66,0x07,0xBC,0xEA,0xAF,0x9B,0xAA,0x39,
  38929. 0x15,0x8B,0x08,0x8C,0x24,0x41,0x9B,0x46,
  38930. 0x1B,0x5B,0x91,0x11,0xC4,0xFD,0xA9,0x88,
  38931. 0x35,0x0E,0x7D,0xAF,0xFD,0xB7,0x90,0x7E,
  38932. 0xD7,0x29,0x02,0x0A,0xDC,0xC8,0x3F,0xC0,
  38933. 0xFD,0x97,0xAF,0x50,0x49,0xA6,0x5E,0x12,
  38934. 0xC1,0xCD,0xEC,0x52,0xC5,0x51,0xF2,0x80,
  38935. 0x17,0x61,0xC7,0x7E,0xBE,0xD1,0x1B,0x65,
  38936. 0xA4,0xAB,0x92,0x8D,0x89,0xB2,0xC5,0x8F,
  38937. 0xFF,0xA5,0x6F,0xFA,0x62,0x75,0xE4,0xA1,
  38938. 0xD4,0x22,0xA8,0x9E,0x40,0x04,0x27,0x1F,
  38939. 0xCC,0x81,0xBA,0x28,0x67,0xA0,0x1C,0x80,
  38940. 0xEB,0xCA,0xB0,0x61,0xA5,0x48,0xD0,0x8A,
  38941. 0x25,0xEB,0x9E,0x67,0x8C,0x8E,0x9B,0xD1,
  38942. 0xAD,0xBB,0xC3,0xEA,0xD3,0xD4,0xC5,0x12,
  38943. 0x7B,0xDD,0x00,0x57,0x7F,0xF6,0xF7,0xF6,
  38944. 0x3C,0x05,0xCF,0xFC,0x12,0xE1,0x93,0x05,
  38945. 0xE5,0x9B,0x79,0x87,0x69,0xD8,0x82,0xD9,
  38946. 0xD7,0x1D,0x41,0x73,0xE4,0x52,0x1D,0x3E,
  38947. 0xE5,0x8C,0x8D,0x34,0xE1,0x75,0xA9,0xF1,
  38948. 0x9D,0x09,0xA2,0x5B,0xEF,0xDA,0x96,0x6E,
  38949. 0x76,0x3D,0xEA,0x50,0xD9,0xCF,0x4F,0xAC,
  38950. 0xAD,0x1D,0x35,0x72,0x1B,0x88,0x8B,0xCD,
  38951. 0x8C,0x8A,0x8A,0xE0,0x96,0x04,0xD8,0xBB,
  38952. 0x28,0x43,0x16,0x77,0x60,0x98,0x63,0xF9,
  38953. 0xB9,0x71,0x46,0xB7,0xE1,0xA7,0xA9,0x84,
  38954. 0xC3,0x65,0x82,0xE1,0x1B,0x67,0x04,0x2D,
  38955. 0x55,0x6B,0xF9,0xC0,0x79,0x09,0x09,0xE7,
  38956. 0xFD,0x06,0x4D,0x09,0x9B,0x1A,0xCE,0x35,
  38957. 0xFA,0x27,0x6F,0x2F,0x01,0x65,0x0D,0xA0,
  38958. 0x97,0x59,0x11,0xF0,0x48,0xD2,0xE7,0x46,
  38959. 0xBE,0xB4,0x0A,0xA3,0xE2,0x75,0x0E,0x09,
  38960. 0x94,0xD9,0x69,0x28,0xD4,0xDA,0x64,0xBA,
  38961. 0xFE,0xA4,0xB9,0xF0,0xBA,0xEB,0xBA,0xAC,
  38962. 0xA8,0xF9,0xD3,0x82,0x4C,0x36,0x80,0xFA,
  38963. 0xE5,0xF6,0x76,0xC3,0x80,0xFA,0x90,0x29,
  38964. 0xF4,0x85,0xA4,0xC6,0x25,0x22,0x79,0x7E,
  38965. 0x39,0x1E,0x30,0xB8,0x65,0x72,0xCF,0xE1,
  38966. 0x99,0xF0,0x75,0xE8,0x09,0xB4,0x92,0x96,
  38967. 0x1B,0x68,0x50,0x88,0xF1,0x2C,0x97,0xE3,
  38968. 0x2D,0x26,0x8F,0xC5,0x30,0xCF,0x24,0xCB,
  38969. 0xB2,0x60,0x77,0xDC,0x02,0x72,0x0D,0xD9,
  38970. 0x2E,0xF2,0x52,0xEA,0x00,0xF6,0x32,0x65,
  38971. 0xA5,0xC6,0x43,0x29,0x29,0x69,0xAB,0x27,
  38972. 0x0C,0x39,0xDF,0x76,0x3E,0x93,0x95,0xB1,
  38973. 0x2C,0xA2,0x0D,0x18,0xCE,0xA0,0x97,0x10,
  38974. 0x3C,0x90,0xC0,0xEF,0x0E,0x04,0xA6,0xC8,
  38975. 0xA0,0x21,0x3C,0x0B,0x22,0x77,0x7A,0x66,
  38976. 0xA5,0x90,0x25,0xA4,0x09,0x3E,0xD5,0x27,
  38977. 0x1F,0x6C,0x99,0x85,0x5C,0xA2,0x99,0x7A,
  38978. 0x25,0xEE,0x8D,0x32,0x3D,0xD3,0xDC,0xF5,
  38979. 0x00,0x5A,0x34,0x61,0xB6,0xCD,0x4E,0xBC,
  38980. 0x26,0x36,0xFB,0x44,0x97,0x35,0xBD,0x06,
  38981. 0x7D,0x2E,0x4A,0xA2,0xDC,0x24,0xFE,0x70,
  38982. 0x0A,0xF9,0x57,0xE3,0xEE,0xAB,0xD1,0x17,
  38983. 0xF3,0x7C,0xD6,0x37,0x26,0xFA,0x83,0x9F,
  38984. 0xDD,0xB2,0xE1,0xD7,0xF9,0xC7,0x0E,0x15,
  38985. 0x01,0xA6,0x58,0x32,0x98,0x04,0x32,0xD4,
  38986. 0xDE,0xB9,0xEF,0x09,0xFA,0xE4,0x5A,0xD7,
  38987. 0xDD,0x09,0x1C,0xC9,0xAC,0xB8,0x6A,0xF5,
  38988. 0x00,0x5D,0x6B,0x95,0x12,0x8C,0x2F,0xCC,
  38989. 0xD8,0xB9,0x50,0x3A,0xEB,0x74,0x86,0xD2,
  38990. 0x3F,0xA1,0x05,0x8F,0x6E,0xEF,0xF5,0xA4,
  38991. 0xD6,0x6E,0x53,0xFA,0x9E,0xFA,0xCE,0xDB,
  38992. 0x99,0x46,0xE7,0xC5,0xDA,0x92,0x51,0x4F,
  38993. 0x22,0x07,0xF3,0xA5,0x38,0x26,0xD3,0xEC,
  38994. 0xD6,0x01,0xDD,0x31,0x3A,0x48,0x93,0xF6,
  38995. 0x69,0x4F,0xD8,0xF6,0xC2,0x91,0xA5,0x7C,
  38996. 0xDF,0x51,0x64,0xF1,0x3B,0x79,0xBC,0x0A,
  38997. 0x2C,0xDC,0x33,0x5A,0x29,0xF6,0xB2,0x09,
  38998. 0x66,0xCA,0x24,0x9F,0x1A,0x18,0xF3,0x76,
  38999. 0x4C,0x5E,0x0B,0x81,0x7F,0x29,0x84,0xD8,
  39000. 0x7A,0xA8,0xD6,0x11,0xAC,0xEC,0xD9,0x07,
  39001. 0x91,0xEC,0xB6,0x6D,0xEC,0xDB,0xBE,0x6F,
  39002. 0x9F,0xC5,0x19,0x5E,0x56,0x87,0x20,0x80,
  39003. 0x75,0xD5,0x64,0xE9,0x80,0xBF,0x2D,0xD5,
  39004. 0x94,0x9F,0x8C,0xA4,0x54,0x41,0xAB,0xB1,
  39005. 0x8E,0xAD,0x51,0xE4,0x3C,0x24,0xF7,0x1D,
  39006. 0xFE,0x02,0x48,0x7C,0x6D,0xED,0xF1,0xAC,
  39007. 0xD9,0x79,0x42,0xE5,0x3A,0xCF,0x6A,0x4C,
  39008. 0x6D,0xE2,0x13,0xD2,0x2B,0x9D,0xAB,0x1F,
  39009. 0x70,0xD3,0xC0,0x6F,0x81,0xE9,0x9A,0x86,
  39010. 0x33,0x39,0x60,0xE7,0x6A,0x00,0x1F,0x97,
  39011. 0xEB,0xE5,0x1D,0x0D,0x66,0x15,0xC9,0xA2,
  39012. 0xB1,0xC0,0xF0,0x2E,0xF4,0x07,0xA2,0x2E,
  39013. 0x49,0x92,0x95,0x13,0xA3,0x18,0x46,0x25,
  39014. 0xB9,0x3C,0xA1,0x4B,0x00,0x00,0x00,0x06,
  39015. 0xAB,0xAA,0xF9,0x3F,0x7E,0x21,0xF4,0x0E,
  39016. 0xCE,0xFD,0xE0,0x44,0xAC,0xC7,0x1A,0x30,
  39017. 0x22,0x9D,0x0A,0xD7,0x96,0x2D,0x8F,0x9A,
  39018. 0x99,0x1F,0x40,0x75,0x7F,0x62,0xF9,0xC1,
  39019. 0x81,0x7B,0x4A,0x1B,0xFA,0xD6,0x87,0xB9,
  39020. 0xEF,0x58,0x48,0xE4,0x5C,0x79,0xE5,0xB1,
  39021. 0x2C,0x59,0xA4,0x42,0xDB,0xA6,0x53,0x70,
  39022. 0x80,0x61,0x17,0xD4,0xD3,0x77,0xBD,0x53,
  39023. 0x26,0x7C,0x0E,0x0E,0xFF,0x30,0x4B,0xD0,
  39024. 0x86,0xFC,0x02,0x20,0x24,0x46,0x5B,0xF5,
  39025. 0xE3,0x99,0x73,0x85,0x60,0x00,0x36,0x47,
  39026. 0x17,0xEE,0x0C,0xD2,0x80,0x71,0x46,0x0E,
  39027. 0x2B,0xB0,0xEF,0x7F,0xFE,0x3B,0xE5,0xE1,
  39028. 0x87,0xC2,0xAF,0x1A,0x6F,0x63,0xF4,0x5A,
  39029. 0xC4,0x16,0xF7,0xAD,0x07,0x70,0x71,0x85,
  39030. 0x7D,0x3D,0x67,0x08,0xB8,0xD8,0xE2,0xF0,
  39031. 0xA1,0xAC,0xD2,0x94,0x7D,0x93,0x03,0xDD,
  39032. 0x54,0xF9,0x64,0x19,0xB3,0xED,0x24,0x22,
  39033. 0x01,0xD7,0x12,0x5E,0xC1,0x2B,0x39,0x10,
  39034. 0x13,0xE2,0x56,0x1C,0xEE,0xF4,0x2A,0x49,
  39035. 0x7B,0xFB,0x36,0x8D,0xF8,0xAF,0x60,0xDF,
  39036. 0x10,0xF0,0x72,0xA2,0xED,0xB6,0x53,0x88,
  39037. 0xA9,0x0C,0xED,0x9C,0x18,0x33,0x7D,0x65,
  39038. 0x9B,0xB2,0x9C,0x3E,0xE9,0x1E,0x43,0x51,
  39039. 0x7E,0xBE,0x01,0x95,0xF6,0x60,0x65,0xBE,
  39040. 0xD1,0xF4,0xE2,0x83,0x6B,0xCA,0x7A,0x70,
  39041. 0x41,0x83,0x72,0xC0,0x23,0x51,0x13,0x11,
  39042. 0x2D,0xF9,0xC0,0x0D,0x7D,0x73,0x76,0xA5,
  39043. 0x30,0x83,0x68,0x10,0x35,0xA2,0x18,0x22,
  39044. 0x4E,0x21,0x93,0x27,0x6A,0x19,0x28,0x83,
  39045. 0x7F,0xDD,0xDD,0xFF,0xC3,0x8A,0x64,0x00,
  39046. 0x5F,0x1C,0x0D,0xF8,0xBB,0xD7,0x15,0xB9,
  39047. 0xEF,0xE0,0x07,0x62,0x05,0x9E,0xCF,0xFC,
  39048. 0x08,0x52,0x1E,0x65,0x41,0x56,0x6A,0xEB,
  39049. 0x81,0x53,0x30,0x7B,0xF2,0xFD,0x65,0xFF,
  39050. 0xA2,0x14,0xF5,0x62,0x1E,0x24,0x48,0x47,
  39051. 0xA5,0x41,0x80,0xB4,0xC5,0xDC,0xB2,0xB4,
  39052. 0x2D,0x17,0xE7,0xBE,0x49,0x53,0x7A,0x25,
  39053. 0xC5,0x0D,0x19,0x59,0xF4,0x88,0x59,0xED,
  39054. 0x92,0x13,0xEE,0x7A,0x4F,0x12,0x98,0x4C
  39055. };
  39056. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t lms_test_verify_only(void)
  39057. {
  39058. LmsKey verifyKey;
  39059. unsigned char pub_raw[HSS_MAX_PUBLIC_KEY_LEN];
  39060. word32 pub_len = sizeof(pub_raw);
  39061. word32 sigSz = 0;
  39062. word32 msgSz = sizeof(lms_msg);
  39063. word32 pubSz = 0;
  39064. int levels = 0;
  39065. int height = 0;
  39066. int winternitz = 0;
  39067. int ret = WC_TEST_RET_ENC_NC;
  39068. int ret2 = WC_TEST_RET_ENC_NC;
  39069. int j = 0;
  39070. int n_diff = 0;
  39071. WOLFSSL_ENTER("lms_test_verify_only");
  39072. XMEMSET(pub_raw, 0, sizeof(pub_raw));
  39073. ret = wc_LmsKey_Init(&verifyKey, NULL, INVALID_DEVID);
  39074. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  39075. ret = wc_LmsKey_SetParameters(&verifyKey, 1, 10, 8);
  39076. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  39077. ret = wc_LmsKey_ImportPubRaw(&verifyKey, lms_L1H10W8_pub,
  39078. HSS_MAX_PUBLIC_KEY_LEN);
  39079. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  39080. /* Verify parameters, pub length, and sig length are correct. */
  39081. ret = wc_LmsKey_GetParameters(&verifyKey, &levels, &height, &winternitz);
  39082. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  39083. if (levels != 1 || height != 10 || winternitz != 8) {
  39084. printf("error: invalid LMS parameters: L%d-H%d-W%d\n", levels, height,
  39085. winternitz);
  39086. return WC_TEST_RET_ENC_NC;
  39087. }
  39088. ret = wc_LmsKey_GetPubLen(&verifyKey, &pubSz);
  39089. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  39090. if (pubSz != HSS_MAX_PUBLIC_KEY_LEN) {
  39091. printf("error: got %u, expected %d\n", pubSz, HSS_MAX_PUBLIC_KEY_LEN);
  39092. return WC_TEST_RET_ENC_EC(pubSz);
  39093. }
  39094. ret = wc_LmsKey_GetSigLen(&verifyKey, &sigSz);
  39095. if (ret != 0) { return WC_TEST_RET_ENC_EC(ret); }
  39096. if (sigSz != LMS_L1H10W8_SIGLEN) {
  39097. printf("error: got %u, expected %d\n", sigSz, LMS_L1H10W8_SIGLEN);
  39098. return WC_TEST_RET_ENC_EC(sigSz);
  39099. }
  39100. ret = wc_LmsKey_Verify(&verifyKey, lms_L1H10W8_sig, LMS_L1H10W8_SIGLEN,
  39101. (byte *) lms_msg, msgSz);
  39102. if (ret != 0) {
  39103. printf("error: wc_LmsKey_Verify returned %d\n", ret);
  39104. return WC_TEST_RET_ENC_EC(ret);
  39105. }
  39106. /* Now test the ExportPubRaw API, verify we recover the original pub. */
  39107. ret = wc_LmsKey_ExportPubRaw(&verifyKey, pub_raw, &pub_len);
  39108. if (ret != 0) {
  39109. printf("error: wc_LmsKey_ExportPubRaw returned %d, expected 0\n", ret);
  39110. return WC_TEST_RET_ENC_EC(ret);
  39111. }
  39112. if (pub_len != HSS_MAX_PUBLIC_KEY_LEN) {
  39113. printf("error: LMS pub len %u, expected %d\n", pub_len,
  39114. HSS_MAX_PUBLIC_KEY_LEN);
  39115. return WC_TEST_RET_ENC_EC(pub_len);
  39116. }
  39117. n_diff = XMEMCMP(pub_raw, lms_L1H10W8_pub, sizeof(lms_L1H10W8_pub));
  39118. if (n_diff != 0) {
  39119. printf("error: exported and imported pub raw do not match: %d\n",
  39120. n_diff);
  39121. return WC_TEST_RET_ENC_EC(n_diff);
  39122. }
  39123. /* Flip bits in message. This should fail. */
  39124. lms_msg[msgSz / 2] ^= 1;
  39125. ret2 = wc_LmsKey_Verify(&verifyKey, lms_L1H10W8_sig, LMS_L1H10W8_SIGLEN,
  39126. (byte *) lms_msg, msgSz);
  39127. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  39128. printf("error: wc_LmsKey_Verify returned %d, expected -1\n", ret2);
  39129. return WC_TEST_RET_ENC_EC(ret);
  39130. }
  39131. /* Flip it back. This should pass again. */
  39132. lms_msg[msgSz / 2] ^= 1;
  39133. ret = wc_LmsKey_Verify(&verifyKey, lms_L1H10W8_sig, LMS_L1H10W8_SIGLEN,
  39134. (byte *) lms_msg, msgSz);
  39135. if (ret != 0) {
  39136. printf("error: wc_LmsKey_Verify returned %d, expected 0\n", ret);
  39137. return WC_TEST_RET_ENC_EC(ret);
  39138. }
  39139. /* Flip bits in a few places throughout the signature, stepping in multiple
  39140. * of hash size. These should all fail with -1. */
  39141. for (j = 0; j < (int) sigSz; j+= 4 * 32) {
  39142. lms_L1H10W8_sig[j] ^= 1;
  39143. ret2 = wc_LmsKey_Verify(&verifyKey, lms_L1H10W8_sig,
  39144. LMS_L1H10W8_SIGLEN,
  39145. (byte *) lms_msg, msgSz);
  39146. if ((ret2 != -1) && (ret2 != SIG_VERIFY_E)) {
  39147. /* Verify passed when it should have failed. */
  39148. return WC_TEST_RET_ENC_I(j);
  39149. }
  39150. /* Flip this spot back. */
  39151. lms_L1H10W8_sig[j] ^= 1;
  39152. }
  39153. wc_LmsKey_Free(&verifyKey);
  39154. return ret;
  39155. }
  39156. #endif
  39157. #endif /* if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_SMALL_STACK) */
  39158. static const int fiducial3 = WC_TEST_RET_LN; /* source code reference point --
  39159. * see print_fiducials() below.
  39160. */
  39161. #ifdef WOLFCRYPT_HAVE_ECCSI
  39162. static wc_test_ret_t eccsi_api_test(WC_RNG* rng, EccsiKey* key, mp_int* ssk,
  39163. ecc_point* pvt)
  39164. {
  39165. wc_test_ret_t ret;
  39166. byte id[1] = { 0x00 };
  39167. int valid;
  39168. word32 sz;
  39169. byte data[256];
  39170. byte hash[WC_MAX_DIGEST_SIZE];
  39171. byte hashSz;
  39172. byte sig[257];
  39173. word32 sigSz;
  39174. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  39175. if (ret != BAD_FUNC_ARG)
  39176. return WC_TEST_RET_ENC_EC(ret);
  39177. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  39178. if (ret != BAD_FUNC_ARG)
  39179. return WC_TEST_RET_ENC_EC(ret);
  39180. ret = wc_InitEccsiKey(NULL, NULL, INVALID_DEVID);
  39181. if (ret != BAD_FUNC_ARG)
  39182. return WC_TEST_RET_ENC_EC(ret);
  39183. ret = wc_InitEccsiKey(NULL, HEAP_HINT, INVALID_DEVID);
  39184. if (ret != BAD_FUNC_ARG)
  39185. return WC_TEST_RET_ENC_EC(ret);
  39186. wc_FreeEccsiKey(NULL);
  39187. /* Create a valid key. */
  39188. ret = wc_InitEccsiKey(key, NULL, INVALID_DEVID);
  39189. if (ret != 0)
  39190. return WC_TEST_RET_ENC_EC(ret);
  39191. ret = wc_MakeEccsiKey(NULL, NULL);
  39192. if (ret != BAD_FUNC_ARG)
  39193. return WC_TEST_RET_ENC_EC(ret);
  39194. ret = wc_MakeEccsiKey(key, NULL);
  39195. if (ret != BAD_FUNC_ARG)
  39196. return WC_TEST_RET_ENC_EC(ret);
  39197. ret = wc_MakeEccsiKey(NULL, rng);
  39198. if (ret != BAD_FUNC_ARG)
  39199. return WC_TEST_RET_ENC_EC(ret);
  39200. ret = wc_MakeEccsiPair(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL,
  39201. NULL);
  39202. if (ret != BAD_FUNC_ARG)
  39203. return WC_TEST_RET_ENC_EC(ret);
  39204. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL);
  39205. if (ret != BAD_FUNC_ARG)
  39206. return WC_TEST_RET_ENC_EC(ret);
  39207. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt);
  39208. if (ret != BAD_FUNC_ARG)
  39209. return WC_TEST_RET_ENC_EC(ret);
  39210. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt);
  39211. if (ret != BAD_FUNC_ARG)
  39212. return WC_TEST_RET_ENC_EC(ret);
  39213. ret = wc_MakeEccsiPair(key, NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  39214. if (ret != BAD_FUNC_ARG)
  39215. return WC_TEST_RET_ENC_EC(ret);
  39216. ret = wc_MakeEccsiPair(NULL, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  39217. if (ret != BAD_FUNC_ARG)
  39218. return WC_TEST_RET_ENC_EC(ret);
  39219. /* No key set */
  39220. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  39221. if (ret != BAD_STATE_E)
  39222. return WC_TEST_RET_ENC_EC(ret);
  39223. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL,
  39224. NULL);
  39225. if (ret != BAD_FUNC_ARG)
  39226. return WC_TEST_RET_ENC_EC(ret);
  39227. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  39228. NULL);
  39229. if (ret != BAD_FUNC_ARG)
  39230. return WC_TEST_RET_ENC_EC(ret);
  39231. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL,
  39232. &valid);
  39233. if (ret != BAD_FUNC_ARG)
  39234. return WC_TEST_RET_ENC_EC(ret);
  39235. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt,
  39236. &valid);
  39237. if (ret != BAD_FUNC_ARG)
  39238. return WC_TEST_RET_ENC_EC(ret);
  39239. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt,
  39240. &valid);
  39241. if (ret != BAD_FUNC_ARG)
  39242. return WC_TEST_RET_ENC_EC(ret);
  39243. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  39244. &valid);
  39245. if (ret != BAD_FUNC_ARG)
  39246. return WC_TEST_RET_ENC_EC(ret);
  39247. /* No key set */
  39248. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  39249. &valid);
  39250. if (ret != BAD_STATE_E)
  39251. return WC_TEST_RET_ENC_EC(ret);
  39252. ret = wc_ValidateEccsiPvt(NULL, NULL, NULL);
  39253. if (ret != BAD_FUNC_ARG)
  39254. return WC_TEST_RET_ENC_EC(ret);
  39255. ret = wc_ValidateEccsiPvt(key, NULL, NULL);
  39256. if (ret != BAD_FUNC_ARG)
  39257. return WC_TEST_RET_ENC_EC(ret);
  39258. ret = wc_ValidateEccsiPvt(NULL, pvt, NULL);
  39259. if (ret != BAD_FUNC_ARG)
  39260. return WC_TEST_RET_ENC_EC(ret);
  39261. ret = wc_ValidateEccsiPvt(NULL, NULL, &valid);
  39262. if (ret != BAD_FUNC_ARG)
  39263. return WC_TEST_RET_ENC_EC(ret);
  39264. ret = wc_ValidateEccsiPvt(key, pvt, NULL);
  39265. if (ret != BAD_FUNC_ARG)
  39266. return WC_TEST_RET_ENC_EC(ret);
  39267. ret = wc_ValidateEccsiPvt(key, NULL, &valid);
  39268. if (ret != BAD_FUNC_ARG)
  39269. return WC_TEST_RET_ENC_EC(ret);
  39270. ret = wc_ValidateEccsiPvt(NULL, pvt, &valid);
  39271. if (ret != BAD_FUNC_ARG)
  39272. return WC_TEST_RET_ENC_EC(ret);
  39273. ret = wc_EncodeEccsiPair(NULL, NULL, NULL, data, NULL);
  39274. if (ret != BAD_FUNC_ARG)
  39275. return WC_TEST_RET_ENC_EC(ret);
  39276. ret = wc_EncodeEccsiPair(key, ssk, pvt, data, NULL);
  39277. if (ret != BAD_FUNC_ARG)
  39278. return WC_TEST_RET_ENC_EC(ret);
  39279. ret = wc_EncodeEccsiPair(key, ssk, NULL, data, &sz);
  39280. if (ret != BAD_FUNC_ARG)
  39281. return WC_TEST_RET_ENC_EC(ret);
  39282. ret = wc_EncodeEccsiPair(key, NULL, pvt, data, &sz);
  39283. if (ret != BAD_FUNC_ARG)
  39284. return WC_TEST_RET_ENC_EC(ret);
  39285. ret = wc_EncodeEccsiPair(NULL, ssk, pvt, data, &sz);
  39286. if (ret != BAD_FUNC_ARG)
  39287. return WC_TEST_RET_ENC_EC(ret);
  39288. /* No key created so no curve information. */
  39289. ret = wc_EncodeEccsiPair(key, ssk, pvt, NULL, &sz);
  39290. if (ret != LENGTH_ONLY_E)
  39291. return WC_TEST_RET_ENC_EC(ret);
  39292. ret = wc_EncodeEccsiSsk(NULL, NULL, data, NULL);
  39293. if (ret != BAD_FUNC_ARG)
  39294. return WC_TEST_RET_ENC_EC(ret);
  39295. ret = wc_EncodeEccsiSsk(key, ssk, data, NULL);
  39296. if (ret != BAD_FUNC_ARG)
  39297. return WC_TEST_RET_ENC_EC(ret);
  39298. ret = wc_EncodeEccsiSsk(key, NULL, data, &sz);
  39299. if (ret != BAD_FUNC_ARG)
  39300. return WC_TEST_RET_ENC_EC(ret);
  39301. ret = wc_EncodeEccsiSsk(NULL, ssk, data, &sz);
  39302. if (ret != BAD_FUNC_ARG)
  39303. return WC_TEST_RET_ENC_EC(ret);
  39304. ret = wc_EncodeEccsiPvt(NULL, NULL, data, NULL, 1);
  39305. if (ret != BAD_FUNC_ARG)
  39306. return WC_TEST_RET_ENC_EC(ret);
  39307. ret = wc_EncodeEccsiPvt(key, pvt, data, NULL, 1);
  39308. if (ret != BAD_FUNC_ARG)
  39309. return WC_TEST_RET_ENC_EC(ret);
  39310. ret = wc_EncodeEccsiPvt(key, NULL, data, &sz, 1);
  39311. if (ret != BAD_FUNC_ARG)
  39312. return WC_TEST_RET_ENC_EC(ret);
  39313. ret = wc_EncodeEccsiPvt(NULL, pvt, data, &sz, 1);
  39314. if (ret != BAD_FUNC_ARG)
  39315. return WC_TEST_RET_ENC_EC(ret);
  39316. ret = wc_DecodeEccsiPair(NULL, NULL, 0, NULL, NULL);
  39317. if (ret != BAD_FUNC_ARG)
  39318. return WC_TEST_RET_ENC_EC(ret);
  39319. ret = wc_DecodeEccsiPair(key, data, 0, ssk, NULL);
  39320. if (ret != BAD_FUNC_ARG)
  39321. return WC_TEST_RET_ENC_EC(ret);
  39322. ret = wc_DecodeEccsiPair(key, data, 0, NULL, pvt);
  39323. if (ret != BAD_FUNC_ARG)
  39324. return WC_TEST_RET_ENC_EC(ret);
  39325. ret = wc_DecodeEccsiPair(key, NULL, 0, ssk, pvt);
  39326. if (ret != BAD_FUNC_ARG)
  39327. return WC_TEST_RET_ENC_EC(ret);
  39328. ret = wc_DecodeEccsiPair(NULL, data, 0, ssk, pvt);
  39329. if (ret != BAD_FUNC_ARG)
  39330. return WC_TEST_RET_ENC_EC(ret);
  39331. ret = wc_DecodeEccsiSsk(NULL, NULL, 0, NULL);
  39332. if (ret != BAD_FUNC_ARG)
  39333. return WC_TEST_RET_ENC_EC(ret);
  39334. ret = wc_DecodeEccsiSsk(key, data, 0, NULL);
  39335. if (ret != BAD_FUNC_ARG)
  39336. return WC_TEST_RET_ENC_EC(ret);
  39337. ret = wc_DecodeEccsiSsk(key, NULL, 0, ssk);
  39338. if (ret != BAD_FUNC_ARG)
  39339. return WC_TEST_RET_ENC_EC(ret);
  39340. ret = wc_DecodeEccsiSsk(NULL, data, 0, ssk);
  39341. if (ret != BAD_FUNC_ARG)
  39342. return WC_TEST_RET_ENC_EC(ret);
  39343. ret = wc_DecodeEccsiPvt(NULL, NULL, 0, NULL);
  39344. if (ret != BAD_FUNC_ARG)
  39345. return WC_TEST_RET_ENC_EC(ret);
  39346. ret = wc_DecodeEccsiPvt(key, data, 0, NULL);
  39347. if (ret != BAD_FUNC_ARG)
  39348. return WC_TEST_RET_ENC_EC(ret);
  39349. ret = wc_DecodeEccsiPvt(key, NULL, 0, pvt);
  39350. if (ret != BAD_FUNC_ARG)
  39351. return WC_TEST_RET_ENC_EC(ret);
  39352. ret = wc_DecodeEccsiPvt(NULL, data, 0, pvt);
  39353. if (ret != BAD_FUNC_ARG)
  39354. return WC_TEST_RET_ENC_EC(ret);
  39355. ret = wc_DecodeEccsiPvtFromSig(NULL, NULL, 0, NULL);
  39356. if (ret != BAD_FUNC_ARG)
  39357. return WC_TEST_RET_ENC_EC(ret);
  39358. ret = wc_DecodeEccsiPvtFromSig(key, data, 0, NULL);
  39359. if (ret != BAD_FUNC_ARG)
  39360. return WC_TEST_RET_ENC_EC(ret);
  39361. ret = wc_DecodeEccsiPvtFromSig(key, NULL, 0, pvt);
  39362. if (ret != BAD_FUNC_ARG)
  39363. return WC_TEST_RET_ENC_EC(ret);
  39364. ret = wc_DecodeEccsiPvtFromSig(NULL, data, 0, pvt);
  39365. if (ret != BAD_FUNC_ARG)
  39366. return WC_TEST_RET_ENC_EC(ret);
  39367. ret = wc_ExportEccsiKey(NULL, data, NULL);
  39368. if (ret != BAD_FUNC_ARG)
  39369. return WC_TEST_RET_ENC_EC(ret);
  39370. ret = wc_ExportEccsiKey(key, data, NULL);
  39371. if (ret != BAD_FUNC_ARG)
  39372. return WC_TEST_RET_ENC_EC(ret);
  39373. ret = wc_ExportEccsiKey(NULL, data, &sz);
  39374. if (ret != BAD_FUNC_ARG)
  39375. return WC_TEST_RET_ENC_EC(ret);
  39376. /* No key to export */
  39377. ret = wc_ExportEccsiKey(key, NULL, &sz);
  39378. if (ret != BAD_STATE_E)
  39379. return WC_TEST_RET_ENC_EC(ret);
  39380. ret = wc_ImportEccsiKey(NULL, NULL, 0);
  39381. if (ret != BAD_FUNC_ARG)
  39382. return WC_TEST_RET_ENC_EC(ret);
  39383. ret = wc_ImportEccsiKey(key, NULL, 0);
  39384. if (ret != BAD_FUNC_ARG)
  39385. return WC_TEST_RET_ENC_EC(ret);
  39386. ret = wc_ImportEccsiKey(NULL, data, 0);
  39387. if (ret != BAD_FUNC_ARG)
  39388. return WC_TEST_RET_ENC_EC(ret);
  39389. ret = wc_ExportEccsiPrivateKey(NULL, data, NULL);
  39390. if (ret != BAD_FUNC_ARG)
  39391. return WC_TEST_RET_ENC_EC(ret);
  39392. ret = wc_ExportEccsiPrivateKey(key, data, NULL);
  39393. if (ret != BAD_FUNC_ARG)
  39394. return WC_TEST_RET_ENC_EC(ret);
  39395. ret = wc_ExportEccsiPrivateKey(NULL, data, &sz);
  39396. if (ret != BAD_FUNC_ARG)
  39397. return WC_TEST_RET_ENC_EC(ret);
  39398. /* No key to export */
  39399. ret = wc_ExportEccsiPrivateKey(key, NULL, &sz);
  39400. if (ret != BAD_STATE_E)
  39401. return WC_TEST_RET_ENC_EC(ret);
  39402. ret = wc_ImportEccsiPrivateKey(NULL, NULL, 0);
  39403. if (ret != BAD_FUNC_ARG)
  39404. return WC_TEST_RET_ENC_EC(ret);
  39405. ret = wc_ImportEccsiPrivateKey(key, NULL, 0);
  39406. if (ret != BAD_FUNC_ARG)
  39407. return WC_TEST_RET_ENC_EC(ret);
  39408. ret = wc_ImportEccsiPrivateKey(NULL, data, 0);
  39409. if (ret != BAD_FUNC_ARG)
  39410. return WC_TEST_RET_ENC_EC(ret);
  39411. ret = wc_ExportEccsiPublicKey(NULL, data, NULL, 1);
  39412. if (ret != BAD_FUNC_ARG)
  39413. return WC_TEST_RET_ENC_EC(ret);
  39414. ret = wc_ExportEccsiPublicKey(key, data, NULL, 1);
  39415. if (ret != BAD_FUNC_ARG)
  39416. return WC_TEST_RET_ENC_EC(ret);
  39417. ret = wc_ExportEccsiPublicKey(NULL, data, &sz, 1);
  39418. if (ret != BAD_FUNC_ARG)
  39419. return WC_TEST_RET_ENC_EC(ret);
  39420. /* No key to export */
  39421. ret = wc_ExportEccsiPublicKey(key, data, &sz, 1);
  39422. if (ret != BAD_STATE_E)
  39423. return WC_TEST_RET_ENC_EC(ret);
  39424. ret = wc_ImportEccsiPublicKey(NULL, NULL, 0, 1);
  39425. if (ret != BAD_FUNC_ARG)
  39426. return WC_TEST_RET_ENC_EC(ret);
  39427. ret = wc_ImportEccsiPublicKey(key, NULL, 0, 1);
  39428. if (ret != BAD_FUNC_ARG)
  39429. return WC_TEST_RET_ENC_EC(ret);
  39430. ret = wc_ImportEccsiPublicKey(NULL, data, 0, 1);
  39431. if (ret != BAD_FUNC_ARG)
  39432. return WC_TEST_RET_ENC_EC(ret);
  39433. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL, NULL);
  39434. if (ret != BAD_FUNC_ARG)
  39435. return WC_TEST_RET_ENC_EC(ret);
  39436. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, NULL);
  39437. if (ret != BAD_FUNC_ARG)
  39438. return WC_TEST_RET_ENC_EC(ret);
  39439. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, NULL, &hashSz);
  39440. if (ret != BAD_FUNC_ARG)
  39441. return WC_TEST_RET_ENC_EC(ret);
  39442. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, NULL, hash, &hashSz);
  39443. if (ret != BAD_FUNC_ARG)
  39444. return WC_TEST_RET_ENC_EC(ret);
  39445. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, NULL, 1, pvt, hash,
  39446. &hashSz);
  39447. if (ret != BAD_FUNC_ARG)
  39448. return WC_TEST_RET_ENC_EC(ret);
  39449. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  39450. if (ret != BAD_FUNC_ARG)
  39451. return WC_TEST_RET_ENC_EC(ret);
  39452. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  39453. if (ret != BAD_STATE_E)
  39454. return WC_TEST_RET_ENC_EC(ret);
  39455. ret = wc_SetEccsiHash(NULL, NULL, 1);
  39456. if (ret != BAD_FUNC_ARG)
  39457. return WC_TEST_RET_ENC_EC(ret);
  39458. ret = wc_SetEccsiHash(key, NULL, 1);
  39459. if (ret != BAD_FUNC_ARG)
  39460. return WC_TEST_RET_ENC_EC(ret);
  39461. ret = wc_SetEccsiHash(NULL, hash, 1);
  39462. if (ret != BAD_FUNC_ARG)
  39463. return WC_TEST_RET_ENC_EC(ret);
  39464. ret = wc_SetEccsiPair(NULL, NULL, NULL);
  39465. if (ret != BAD_FUNC_ARG)
  39466. return WC_TEST_RET_ENC_EC(ret);
  39467. ret = wc_SetEccsiPair(key, NULL, NULL);
  39468. if (ret != BAD_FUNC_ARG)
  39469. return WC_TEST_RET_ENC_EC(ret);
  39470. ret = wc_SetEccsiPair(NULL, ssk, NULL);
  39471. if (ret != BAD_FUNC_ARG)
  39472. return WC_TEST_RET_ENC_EC(ret);
  39473. ret = wc_SetEccsiPair(NULL, NULL, pvt);
  39474. if (ret != BAD_FUNC_ARG)
  39475. return WC_TEST_RET_ENC_EC(ret);
  39476. ret = wc_SetEccsiPair(key, ssk, NULL);
  39477. if (ret != BAD_FUNC_ARG)
  39478. return WC_TEST_RET_ENC_EC(ret);
  39479. ret = wc_SetEccsiPair(key, NULL, pvt);
  39480. if (ret != BAD_FUNC_ARG)
  39481. return WC_TEST_RET_ENC_EC(ret);
  39482. ret = wc_SetEccsiPair(NULL, ssk, pvt);
  39483. if (ret != BAD_FUNC_ARG)
  39484. return WC_TEST_RET_ENC_EC(ret);
  39485. ret = wc_SignEccsiHash(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, NULL);
  39486. if (ret != BAD_FUNC_ARG)
  39487. return WC_TEST_RET_ENC_EC(ret);
  39488. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, sig, NULL);
  39489. if (ret != BAD_FUNC_ARG)
  39490. return WC_TEST_RET_ENC_EC(ret);
  39491. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, NULL, 0, sig,
  39492. &sigSz);
  39493. if (ret != BAD_FUNC_ARG)
  39494. return WC_TEST_RET_ENC_EC(ret);
  39495. ret = wc_SignEccsiHash(key, NULL, WC_HASH_TYPE_SHA256, data, 0, sig,
  39496. &sigSz);
  39497. if (ret != BAD_FUNC_ARG)
  39498. return WC_TEST_RET_ENC_EC(ret);
  39499. ret = wc_SignEccsiHash(NULL, rng, WC_HASH_TYPE_SHA256, data, 0, sig,
  39500. &sigSz);
  39501. if (ret != BAD_FUNC_ARG)
  39502. return WC_TEST_RET_ENC_EC(ret);
  39503. /* Key not set. */
  39504. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  39505. &sigSz);
  39506. if (ret != BAD_STATE_E)
  39507. return WC_TEST_RET_ENC_EC(ret);
  39508. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  39509. if (ret != BAD_FUNC_ARG)
  39510. return WC_TEST_RET_ENC_EC(ret);
  39511. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  39512. if (ret != BAD_FUNC_ARG)
  39513. return WC_TEST_RET_ENC_EC(ret);
  39514. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, NULL, 0, NULL);
  39515. if (ret != BAD_FUNC_ARG)
  39516. return WC_TEST_RET_ENC_EC(ret);
  39517. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0, NULL);
  39518. if (ret != BAD_FUNC_ARG)
  39519. return WC_TEST_RET_ENC_EC(ret);
  39520. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0,
  39521. &valid);
  39522. if (ret != BAD_FUNC_ARG)
  39523. return WC_TEST_RET_ENC_EC(ret);
  39524. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0, NULL);
  39525. if (ret != BAD_FUNC_ARG)
  39526. return WC_TEST_RET_ENC_EC(ret);
  39527. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, NULL, 0,
  39528. &valid);
  39529. if (ret != BAD_FUNC_ARG)
  39530. return WC_TEST_RET_ENC_EC(ret);
  39531. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0,
  39532. &valid);
  39533. if (ret != BAD_FUNC_ARG)
  39534. return WC_TEST_RET_ENC_EC(ret);
  39535. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  39536. &valid);
  39537. if (ret != BAD_FUNC_ARG)
  39538. return WC_TEST_RET_ENC_EC(ret);
  39539. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  39540. &valid);
  39541. if (ret != BAD_STATE_E)
  39542. return WC_TEST_RET_ENC_EC(ret);
  39543. ret = wc_SetEccsiPair(key, ssk, pvt);
  39544. if (ret != 0)
  39545. return WC_TEST_RET_ENC_EC(ret);
  39546. /* Identity hash not set. */
  39547. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  39548. &sigSz);
  39549. if (ret != BAD_STATE_E)
  39550. return WC_TEST_RET_ENC_EC(ret);
  39551. wc_FreeEccsiKey(key);
  39552. return 0;
  39553. }
  39554. /* RFC 6507: Appendix A */
  39555. static wc_test_ret_t eccsi_kat_verify_test(EccsiKey* key, ecc_point* pvt)
  39556. {
  39557. wc_test_ret_t ret;
  39558. int verified;
  39559. const byte msg[] = { 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x00 };
  39560. word32 msgSz = sizeof(msg);
  39561. byte hash[WC_SHA256_DIGEST_SIZE];
  39562. byte hashSz = WC_SHA256_DIGEST_SIZE;
  39563. static const byte id[] = {
  39564. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  39565. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  39566. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  39567. 0x33, 0x00
  39568. };
  39569. word32 idSz = sizeof(id);
  39570. static const byte sig[] = {
  39571. 0x26, 0x9D, 0x4C, 0x8F, 0xDE, 0xB6, 0x6A, 0x74,
  39572. 0xE4, 0xEF, 0x8C, 0x0D, 0x5D, 0xCC, 0x59, 0x7D,
  39573. 0xDF, 0xE6, 0x02, 0x9C, 0x2A, 0xFF, 0xC4, 0x93,
  39574. 0x60, 0x08, 0xCD, 0x2C, 0xC1, 0x04, 0x5D, 0x81,
  39575. 0xE0, 0x9B, 0x52, 0x8D, 0x0E, 0xF8, 0xD6, 0xDF,
  39576. 0x1A, 0xA3, 0xEC, 0xBF, 0x80, 0x11, 0x0C, 0xFC,
  39577. 0xEC, 0x9F, 0xC6, 0x82, 0x52, 0xCE, 0xBB, 0x67,
  39578. 0x9F, 0x41, 0x34, 0x84, 0x69, 0x40, 0xCC, 0xFD,
  39579. 0x04,
  39580. 0x75, 0x8A, 0x14, 0x27, 0x79, 0xBE, 0x89, 0xE8,
  39581. 0x29, 0xE7, 0x19, 0x84, 0xCB, 0x40, 0xEF, 0x75,
  39582. 0x8C, 0xC4, 0xAD, 0x77, 0x5F, 0xC5, 0xB9, 0xA3,
  39583. 0xE1, 0xC8, 0xED, 0x52, 0xF6, 0xFA, 0x36, 0xD9,
  39584. 0xA7, 0x9D, 0x24, 0x76, 0x92, 0xF4, 0xED, 0xA3,
  39585. 0xA6, 0xBD, 0xAB, 0x77, 0xD6, 0xAA, 0x64, 0x74,
  39586. 0xA4, 0x64, 0xAE, 0x49, 0x34, 0x66, 0x3C, 0x52,
  39587. 0x65, 0xBA, 0x70, 0x18, 0xBA, 0x09, 0x1F, 0x79
  39588. };
  39589. word32 sigSz = sizeof(sig);
  39590. static const byte pubData[] = {
  39591. 0x50, 0xD4, 0x67, 0x0B, 0xDE, 0x75, 0x24, 0x4F,
  39592. 0x28, 0xD2, 0x83, 0x8A, 0x0D, 0x25, 0x55, 0x8A,
  39593. 0x7A, 0x72, 0x68, 0x6D, 0x45, 0x22, 0xD4, 0xC8,
  39594. 0x27, 0x3F, 0xB6, 0x44, 0x2A, 0xEB, 0xFA, 0x93,
  39595. 0xDB, 0xDD, 0x37, 0x55, 0x1A, 0xFD, 0x26, 0x3B,
  39596. 0x5D, 0xFD, 0x61, 0x7F, 0x39, 0x60, 0xC6, 0x5A,
  39597. 0x8C, 0x29, 0x88, 0x50, 0xFF, 0x99, 0xF2, 0x03,
  39598. 0x66, 0xDC, 0xE7, 0xD4, 0x36, 0x72, 0x17, 0xF4
  39599. };
  39600. static const byte expHash[] = {
  39601. 0x49, 0x0f, 0x3f, 0xeb, 0xbc, 0x1c, 0x90, 0x2f,
  39602. 0x62, 0x89, 0x72, 0x3d, 0x7f, 0x8c, 0xbf, 0x79,
  39603. 0xdb, 0x88, 0x93, 0x08, 0x49, 0xd1, 0x9f, 0x38,
  39604. 0xf0, 0x29, 0x5b, 0x5c, 0x27, 0x6c, 0x14, 0xd1
  39605. };
  39606. ret = wc_ImportEccsiPublicKey(key, pubData, sizeof(pubData), 0);
  39607. if (ret != 0)
  39608. return WC_TEST_RET_ENC_EC(ret);
  39609. ret = wc_DecodeEccsiPvtFromSig(key, sig, sigSz, pvt);
  39610. if (ret != 0)
  39611. return WC_TEST_RET_ENC_EC(ret);
  39612. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, idSz, pvt, hash,
  39613. &hashSz);
  39614. if (ret != 0)
  39615. return WC_TEST_RET_ENC_EC(ret);
  39616. if (hashSz != sizeof(expHash))
  39617. return WC_TEST_RET_ENC_NC;
  39618. if (XMEMCMP(hash, expHash, hashSz) != 0)
  39619. return WC_TEST_RET_ENC_NC;
  39620. ret = wc_SetEccsiHash(key, hash, hashSz);
  39621. if (ret != 0)
  39622. return WC_TEST_RET_ENC_EC(ret);
  39623. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  39624. &verified);
  39625. if (ret != 0)
  39626. return WC_TEST_RET_ENC_EC(ret);
  39627. if (!verified)
  39628. return WC_TEST_RET_ENC_NC;
  39629. return 0;
  39630. }
  39631. static wc_test_ret_t eccsi_enc_dec_pair_test(EccsiKey* priv, mp_int* ssk, ecc_point* pvt)
  39632. {
  39633. wc_test_ret_t ret;
  39634. byte data[32 * 3];
  39635. word32 sz;
  39636. ecc_point* decPvt = NULL;
  39637. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39638. mp_int *decSsk = (mp_int *)XMALLOC(sizeof(*decSsk), HEAP_HINT,
  39639. DYNAMIC_TYPE_TMP_BUFFER);
  39640. if (decSsk == NULL)
  39641. return WC_TEST_RET_ENC_ERRNO;
  39642. #else
  39643. mp_int decSsk[1];
  39644. #endif
  39645. ret = mp_init(decSsk);
  39646. if (ret != 0)
  39647. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39648. decPvt = wc_ecc_new_point();
  39649. if (decPvt == NULL)
  39650. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  39651. ret = wc_EncodeEccsiPair(priv, ssk, pvt, NULL, &sz);
  39652. if (ret != LENGTH_ONLY_E)
  39653. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39654. if (sz != 32 * 3)
  39655. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39656. ret = wc_EncodeEccsiPair(priv, ssk, pvt, data, &sz);
  39657. if (ret != 0)
  39658. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39659. if (sz != 32* 3)
  39660. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39661. ret = wc_DecodeEccsiPair(priv, data, sz, decSsk, decPvt);
  39662. if (ret != 0)
  39663. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39664. ret = mp_cmp(ssk, decSsk);
  39665. if (ret != MP_EQ)
  39666. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39667. ret = wc_ecc_cmp_point(pvt, decPvt);
  39668. if (ret != MP_EQ)
  39669. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39670. ret = wc_EncodeEccsiSsk(priv, ssk, NULL, &sz);
  39671. if (ret != LENGTH_ONLY_E)
  39672. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39673. if (sz != 32)
  39674. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39675. ret = wc_EncodeEccsiSsk(priv, ssk, data, &sz);
  39676. if (ret != 0)
  39677. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39678. if (sz != 32)
  39679. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39680. ret = wc_DecodeEccsiSsk(priv, data, sz, decSsk);
  39681. if (ret != 0)
  39682. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39683. ret = mp_cmp(ssk, decSsk);
  39684. if (ret != MP_EQ)
  39685. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39686. ret = wc_EncodeEccsiPvt(priv, pvt, NULL, &sz, 1);
  39687. if (ret != LENGTH_ONLY_E)
  39688. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39689. if (sz != 32 * 2)
  39690. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39691. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 1);
  39692. if (ret != 0)
  39693. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39694. if (sz != 32 * 2)
  39695. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39696. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  39697. if (ret != 0)
  39698. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39699. ret = wc_ecc_cmp_point(pvt, decPvt);
  39700. if (ret != MP_EQ)
  39701. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39702. sz = sizeof(data);
  39703. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 0);
  39704. if (ret != 0)
  39705. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39706. if (sz != 32 * 2 + 1)
  39707. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39708. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  39709. if (ret != 0)
  39710. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  39711. ret = wc_ecc_cmp_point(pvt, decPvt);
  39712. if (ret != MP_EQ)
  39713. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  39714. wc_ecc_del_point(decPvt);
  39715. out:
  39716. mp_free(decSsk);
  39717. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39718. XFREE(decSsk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39719. #endif
  39720. return ret;
  39721. }
  39722. static wc_test_ret_t eccsi_imp_exp_key_test(EccsiKey* priv)
  39723. {
  39724. wc_test_ret_t ret;
  39725. byte data[32 * 3];
  39726. byte out[32 * 3];
  39727. word32 sz;
  39728. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  39729. if (ret != LENGTH_ONLY_E)
  39730. return WC_TEST_RET_ENC_EC(ret);
  39731. if (sz != 32 * 3)
  39732. return WC_TEST_RET_ENC_NC;
  39733. ret = wc_ExportEccsiKey(priv, data, &sz);
  39734. if (ret != 0)
  39735. return WC_TEST_RET_ENC_EC(ret);
  39736. ret = wc_ImportEccsiKey(priv, data, sz);
  39737. if (ret != 0)
  39738. return WC_TEST_RET_ENC_EC(ret);
  39739. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  39740. if (ret != LENGTH_ONLY_E)
  39741. return WC_TEST_RET_ENC_EC(ret);
  39742. if (sz != 32 * 3)
  39743. return WC_TEST_RET_ENC_NC;
  39744. ret = wc_ExportEccsiKey(priv, out, &sz);
  39745. if (ret != 0)
  39746. return WC_TEST_RET_ENC_EC(ret);
  39747. if (sz != 32 * 3)
  39748. return WC_TEST_RET_ENC_NC;
  39749. if (XMEMCMP(data, out, sz) != 0)
  39750. return WC_TEST_RET_ENC_NC;
  39751. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  39752. if (ret != LENGTH_ONLY_E)
  39753. return WC_TEST_RET_ENC_EC(ret);
  39754. if (sz != 32)
  39755. return WC_TEST_RET_ENC_NC;
  39756. ret = wc_ExportEccsiPrivateKey(priv, data, &sz);
  39757. if (ret != 0)
  39758. return WC_TEST_RET_ENC_EC(ret);
  39759. ret = wc_ImportEccsiPrivateKey(priv, data, sz);
  39760. if (ret != 0)
  39761. return WC_TEST_RET_ENC_EC(ret);
  39762. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  39763. if (ret != LENGTH_ONLY_E)
  39764. return WC_TEST_RET_ENC_EC(ret);
  39765. if (sz != 32)
  39766. return WC_TEST_RET_ENC_NC;
  39767. ret = wc_ExportEccsiPrivateKey(priv, out, &sz);
  39768. if (ret != 0)
  39769. return WC_TEST_RET_ENC_EC(ret);
  39770. if (sz != 32)
  39771. return WC_TEST_RET_ENC_NC;
  39772. if (XMEMCMP(data, out, sz) != 0)
  39773. return WC_TEST_RET_ENC_NC;
  39774. return 0;
  39775. }
  39776. static wc_test_ret_t eccsi_imp_exp_pubkey_test(EccsiKey* key1, EccsiKey* key2)
  39777. {
  39778. wc_test_ret_t ret;
  39779. byte data[32 * 2 + 1];
  39780. byte pubData[32 * 2 + 1];
  39781. word32 sz;
  39782. ret = wc_ExportEccsiPublicKey(key1, NULL, &sz, 1);
  39783. if (ret != LENGTH_ONLY_E)
  39784. return WC_TEST_RET_ENC_EC(ret);
  39785. if (sz != 32 * 2)
  39786. return WC_TEST_RET_ENC_NC;
  39787. ret = wc_ExportEccsiPublicKey(key1, data, &sz, 1);
  39788. if (ret != 0)
  39789. return WC_TEST_RET_ENC_EC(ret);
  39790. ret = wc_ImportEccsiPublicKey(key2, data, sz, 1);
  39791. if (ret != 0)
  39792. return WC_TEST_RET_ENC_EC(ret);
  39793. sz = sizeof(pubData);
  39794. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 1);
  39795. if (ret != 0)
  39796. return WC_TEST_RET_ENC_EC(ret);
  39797. if (sz != 32 * 2)
  39798. return WC_TEST_RET_ENC_NC;
  39799. if (XMEMCMP(data, pubData, sz) != 0)
  39800. return WC_TEST_RET_ENC_NC;
  39801. sz = sizeof(pubData);
  39802. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 0);
  39803. if (ret != 0)
  39804. return WC_TEST_RET_ENC_EC(ret);
  39805. if (sz != 32 * 2 + 1)
  39806. return WC_TEST_RET_ENC_NC;
  39807. if (pubData[0] != 0x04)
  39808. return WC_TEST_RET_ENC_NC;
  39809. if (XMEMCMP(pubData + 1, data, sz - 1) != 0)
  39810. return WC_TEST_RET_ENC_NC;
  39811. ret = wc_ImportEccsiPublicKey(key2, pubData, sz, 0);
  39812. if (ret != 0)
  39813. return WC_TEST_RET_ENC_EC(ret);
  39814. return 0;
  39815. }
  39816. static wc_test_ret_t eccsi_make_key_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  39817. mp_int* ssk, ecc_point* pvt)
  39818. {
  39819. wc_test_ret_t ret;
  39820. char mail[] = "test@wolfssl.com";
  39821. byte* id = (byte*)mail;
  39822. word32 idSz = (word32) XSTRLEN(mail);
  39823. int valid;
  39824. ret = wc_MakeEccsiKey(priv, rng);
  39825. if (ret != 0)
  39826. return WC_TEST_RET_ENC_EC(ret);
  39827. ret = eccsi_imp_exp_key_test(priv);
  39828. if (ret < 0)
  39829. return ret;
  39830. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  39831. if (ret < 0)
  39832. return ret;
  39833. ret = wc_MakeEccsiPair(priv, rng, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt);
  39834. if (ret != 0)
  39835. return WC_TEST_RET_ENC_EC(ret);
  39836. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  39837. &valid);
  39838. if (ret != 0)
  39839. return WC_TEST_RET_ENC_EC(ret);
  39840. if (!valid)
  39841. return WC_TEST_RET_ENC_NC;
  39842. ret = eccsi_enc_dec_pair_test(priv, ssk, pvt);
  39843. if (ret != 0)
  39844. return ret;
  39845. return 0;
  39846. }
  39847. static wc_test_ret_t eccsi_sign_verify_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  39848. mp_int* ssk, ecc_point* pvt)
  39849. {
  39850. wc_test_ret_t ret;
  39851. byte hashPriv[WC_MAX_DIGEST_SIZE];
  39852. byte hashPub[WC_MAX_DIGEST_SIZE];
  39853. byte hashSz;
  39854. byte sig[144];
  39855. word32 sigSz;
  39856. int verified, valid;
  39857. char mail[] = "test@wolfssl.com";
  39858. byte* id = (byte*)mail;
  39859. word32 idSz = (word32) XSTRLEN(mail);
  39860. byte msg[] = { 0x00 };
  39861. word32 msgSz = sizeof(msg);
  39862. #ifdef WOLFSSL_SHA384
  39863. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA384, id, idSz, pvt, hashPriv,
  39864. &hashSz);
  39865. if (ret != BAD_FUNC_ARG)
  39866. return WC_TEST_RET_ENC_EC(ret);
  39867. #endif
  39868. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPriv,
  39869. &hashSz);
  39870. if (ret != 0)
  39871. return WC_TEST_RET_ENC_EC(ret);
  39872. if (hashSz != 32)
  39873. return WC_TEST_RET_ENC_NC;
  39874. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPub,
  39875. &hashSz);
  39876. if (ret != 0)
  39877. return WC_TEST_RET_ENC_EC(ret);
  39878. if (hashSz != 32)
  39879. return WC_TEST_RET_ENC_NC;
  39880. if (XMEMCMP(hashPriv, hashPub, hashSz) != 0)
  39881. return WC_TEST_RET_ENC_NC;
  39882. ret = wc_SetEccsiHash(priv, hashPriv, hashSz);
  39883. if (ret != 0)
  39884. return WC_TEST_RET_ENC_EC(ret);
  39885. ret = wc_SetEccsiPair(priv, ssk, pvt);
  39886. if (ret != 0)
  39887. return WC_TEST_RET_ENC_EC(ret);
  39888. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, NULL,
  39889. &sigSz);
  39890. if (ret != LENGTH_ONLY_E)
  39891. return WC_TEST_RET_ENC_EC(ret);
  39892. if (sigSz != 129)
  39893. return WC_TEST_RET_ENC_NC;
  39894. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, sig,
  39895. &sigSz);
  39896. if (ret != 0)
  39897. return WC_TEST_RET_ENC_EC(ret);
  39898. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  39899. if (ret != 0)
  39900. return WC_TEST_RET_ENC_EC(ret);
  39901. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  39902. &verified);
  39903. if (ret != 0)
  39904. return WC_TEST_RET_ENC_EC(ret);
  39905. if (!verified)
  39906. return WC_TEST_RET_ENC_NC;
  39907. /* Check that changing HS results in verification failure. */
  39908. hashPub[0] ^= 0x80;
  39909. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  39910. if (ret != 0)
  39911. return WC_TEST_RET_ENC_EC(ret);
  39912. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  39913. &verified);
  39914. if (ret != 0)
  39915. return WC_TEST_RET_ENC_EC(ret);
  39916. if (verified)
  39917. return WC_TEST_RET_ENC_NC;
  39918. hashPub[0] ^= 0x80;
  39919. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  39920. if (ret != 0)
  39921. return WC_TEST_RET_ENC_EC(ret);
  39922. /* Check that changing msg results in verification failure. */
  39923. msg[0] ^= 0x80;
  39924. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  39925. &verified);
  39926. if (ret != 0)
  39927. return WC_TEST_RET_ENC_EC(ret);
  39928. if (verified)
  39929. return WC_TEST_RET_ENC_NC;
  39930. msg[0] ^= 0x80;
  39931. /* Check that changing signature results in verification failure. */
  39932. sig[0] ^= 0x80;
  39933. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  39934. &verified);
  39935. if (ret != 0)
  39936. return WC_TEST_RET_ENC_EC(ret);
  39937. if (verified)
  39938. return WC_TEST_RET_ENC_NC;
  39939. sig[0] ^= 0x80;
  39940. /* Check that key state hasn't been invalidated. */
  39941. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  39942. &verified);
  39943. if (ret != 0)
  39944. return WC_TEST_RET_ENC_EC(ret);
  39945. if (!verified)
  39946. return WC_TEST_RET_ENC_NC;
  39947. /* Check that verifying with the private key works. */
  39948. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  39949. &verified);
  39950. if (ret != 0)
  39951. return WC_TEST_RET_ENC_EC(ret);
  39952. if (!verified)
  39953. return WC_TEST_RET_ENC_NC;
  39954. /* Check that the KPAK is converted from montgomery form. */
  39955. ret = eccsi_imp_exp_key_test(priv);
  39956. if (ret != 0)
  39957. return ret;
  39958. /* Check that KPAK can converted to Montgomery form again. */
  39959. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  39960. &verified);
  39961. if (ret != 0)
  39962. return WC_TEST_RET_ENC_EC(ret);
  39963. if (!verified)
  39964. return WC_TEST_RET_ENC_NC;
  39965. /* Check that the KPAK is converted from montgomery form. */
  39966. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  39967. &valid);
  39968. if (ret != 0)
  39969. return WC_TEST_RET_ENC_EC(ret);
  39970. if (!valid)
  39971. return WC_TEST_RET_ENC_NC;
  39972. /* Check that KPAK can converted to Montgomery form again. */
  39973. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  39974. &verified);
  39975. if (ret != 0)
  39976. return WC_TEST_RET_ENC_EC(ret);
  39977. if (!verified)
  39978. return WC_TEST_RET_ENC_NC;
  39979. /* Check that the KPAK is converted from montgomery form. */
  39980. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  39981. if (ret != 0)
  39982. return ret;
  39983. return 0;
  39984. }
  39985. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t eccsi_test(void)
  39986. {
  39987. wc_test_ret_t ret = 0;
  39988. WC_RNG rng;
  39989. int rng_inited = 0;
  39990. EccsiKey* priv = NULL;
  39991. EccsiKey* pub = NULL;
  39992. mp_int* ssk = NULL;
  39993. ecc_point* pvt = NULL;
  39994. WOLFSSL_ENTER("eccsi_test");
  39995. priv = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  39996. DYNAMIC_TYPE_TMP_BUFFER);
  39997. if (priv == NULL)
  39998. ret = WC_TEST_RET_ENC_NC;
  39999. else
  40000. XMEMSET(priv, 0, sizeof(*priv));
  40001. if (ret == 0) {
  40002. pub = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  40003. DYNAMIC_TYPE_TMP_BUFFER);
  40004. if (pub == NULL)
  40005. ret = WC_TEST_RET_ENC_NC;
  40006. else
  40007. XMEMSET(pub, 0, sizeof(*pub));
  40008. }
  40009. if (ret == 0) {
  40010. ssk = (mp_int*)XMALLOC(sizeof(mp_int), HEAP_HINT,
  40011. DYNAMIC_TYPE_TMP_BUFFER);
  40012. if (ssk == NULL)
  40013. ret = WC_TEST_RET_ENC_NC;
  40014. else
  40015. XMEMSET(ssk, 0, sizeof(*ssk));
  40016. }
  40017. if (ret == 0) {
  40018. #ifndef HAVE_FIPS
  40019. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  40020. #else
  40021. ret = wc_InitRng(&rng);
  40022. #endif
  40023. if (ret != 0)
  40024. ret = WC_TEST_RET_ENC_EC(ret);
  40025. else
  40026. rng_inited = 1;
  40027. }
  40028. if (ret == 0) {
  40029. pvt = wc_ecc_new_point();
  40030. if (pvt == NULL)
  40031. ret = WC_TEST_RET_ENC_NC;
  40032. }
  40033. if (ret == 0) {
  40034. ret = mp_init(ssk);
  40035. if (ret != 0)
  40036. ret = WC_TEST_RET_ENC_EC(ret);
  40037. }
  40038. if (ret == 0) {
  40039. ret = eccsi_api_test(&rng, priv, ssk, pvt);
  40040. }
  40041. if (ret == 0) {
  40042. ret = wc_InitEccsiKey(pub, HEAP_HINT, INVALID_DEVID);
  40043. if (ret != 0)
  40044. ret = WC_TEST_RET_ENC_EC(ret);
  40045. }
  40046. if (ret == 0) {
  40047. ret = wc_InitEccsiKey(priv, HEAP_HINT, INVALID_DEVID);
  40048. if (ret != 0)
  40049. ret = WC_TEST_RET_ENC_EC(ret);
  40050. }
  40051. if (ret == 0) {
  40052. ret = eccsi_kat_verify_test(pub, pvt);
  40053. }
  40054. if (ret == 0) {
  40055. ret = eccsi_make_key_test(priv, pub, &rng, ssk, pvt);
  40056. }
  40057. if (ret == 0) {
  40058. ret = eccsi_sign_verify_test(priv, pub, &rng, ssk, pvt);
  40059. }
  40060. if (pvt != NULL)
  40061. wc_ecc_del_point(pvt);
  40062. if (rng_inited)
  40063. wc_FreeRng(&rng);
  40064. if (ssk != NULL) {
  40065. mp_free(ssk);
  40066. XFREE(ssk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40067. }
  40068. if (pub != NULL) {
  40069. wc_FreeEccsiKey(pub);
  40070. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40071. }
  40072. if (priv != NULL) {
  40073. wc_FreeEccsiKey(priv);
  40074. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40075. }
  40076. return ret;
  40077. }
  40078. #endif /* WOLFCRYPT_HAVE_ECCSI */
  40079. #ifdef WOLFCRYPT_HAVE_SAKKE
  40080. static wc_test_ret_t sakke_api_test(WC_RNG* rng, SakkeKey* key, ecc_point* rsk)
  40081. {
  40082. wc_test_ret_t ret;
  40083. byte id[1] = { 0x00 };
  40084. int valid;
  40085. byte data[256];
  40086. word32 sz;
  40087. byte auth[257];
  40088. word16 authSz;
  40089. byte ssv[256];
  40090. word16 ssvSz;
  40091. word32 len;
  40092. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  40093. if (ret != BAD_FUNC_ARG)
  40094. return WC_TEST_RET_ENC_EC(ret);
  40095. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  40096. if (ret != BAD_FUNC_ARG)
  40097. return WC_TEST_RET_ENC_EC(ret);
  40098. wc_FreeSakkeKey(NULL);
  40099. XMEMSET(key, 0, sizeof(*key));
  40100. wc_FreeSakkeKey(key);
  40101. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  40102. if (ret != 0)
  40103. return WC_TEST_RET_ENC_EC(ret);
  40104. ret = wc_MakeSakkeKey(NULL, NULL);
  40105. if (ret != BAD_FUNC_ARG)
  40106. return WC_TEST_RET_ENC_EC(ret);
  40107. ret = wc_MakeSakkeKey(key, NULL);
  40108. if (ret != BAD_FUNC_ARG)
  40109. return WC_TEST_RET_ENC_EC(ret);
  40110. ret = wc_MakeSakkeKey(NULL, rng);
  40111. if (ret != BAD_FUNC_ARG)
  40112. return WC_TEST_RET_ENC_EC(ret);
  40113. ret = wc_MakeSakkePublicKey(NULL, NULL);
  40114. if (ret != BAD_FUNC_ARG)
  40115. return WC_TEST_RET_ENC_EC(ret);
  40116. ret = wc_MakeSakkePublicKey(key, NULL);
  40117. if (ret != BAD_FUNC_ARG)
  40118. return WC_TEST_RET_ENC_EC(ret);
  40119. ret = wc_MakeSakkePublicKey(NULL, rsk);
  40120. if (ret != BAD_FUNC_ARG)
  40121. return WC_TEST_RET_ENC_EC(ret);
  40122. ret = wc_MakeSakkeRsk(NULL, NULL, 1, NULL);
  40123. if (ret != BAD_FUNC_ARG)
  40124. return WC_TEST_RET_ENC_EC(ret);
  40125. ret = wc_MakeSakkeRsk(key, id, 1, NULL);
  40126. if (ret != BAD_FUNC_ARG)
  40127. return WC_TEST_RET_ENC_EC(ret);
  40128. ret = wc_MakeSakkeRsk(key, NULL, 1, rsk);
  40129. if (ret != BAD_FUNC_ARG)
  40130. return WC_TEST_RET_ENC_EC(ret);
  40131. ret = wc_MakeSakkeRsk(NULL, id, 1, rsk);
  40132. if (ret != BAD_FUNC_ARG)
  40133. return WC_TEST_RET_ENC_EC(ret);
  40134. ret = wc_ValidateSakkeRsk(NULL, NULL, 1, NULL, NULL);
  40135. if (ret != BAD_FUNC_ARG)
  40136. return WC_TEST_RET_ENC_EC(ret);
  40137. ret = wc_ValidateSakkeRsk(key, id, 1, rsk, NULL);
  40138. if (ret != BAD_FUNC_ARG)
  40139. return WC_TEST_RET_ENC_EC(ret);
  40140. ret = wc_ValidateSakkeRsk(NULL, id, 1, rsk, &valid);
  40141. if (ret != BAD_FUNC_ARG)
  40142. return WC_TEST_RET_ENC_EC(ret);
  40143. ret = wc_ExportSakkeKey(NULL, NULL, NULL);
  40144. if (ret != BAD_FUNC_ARG)
  40145. return WC_TEST_RET_ENC_EC(ret);
  40146. ret = wc_ExportSakkeKey(key, data, NULL);
  40147. if (ret != BAD_FUNC_ARG)
  40148. return WC_TEST_RET_ENC_EC(ret);
  40149. ret = wc_ExportSakkeKey(NULL, data, &sz);
  40150. if (ret != BAD_FUNC_ARG)
  40151. return WC_TEST_RET_ENC_EC(ret);
  40152. ret = wc_ImportSakkeKey(NULL, NULL, 1);
  40153. if (ret != BAD_FUNC_ARG)
  40154. return WC_TEST_RET_ENC_EC(ret);
  40155. ret = wc_ImportSakkeKey(key, NULL, 1);
  40156. if (ret != BAD_FUNC_ARG)
  40157. return WC_TEST_RET_ENC_EC(ret);
  40158. ret = wc_ImportSakkeKey(NULL, data, 1);
  40159. if (ret != BAD_FUNC_ARG)
  40160. return WC_TEST_RET_ENC_EC(ret);
  40161. ret = wc_ExportSakkePrivateKey(NULL, NULL, NULL);
  40162. if (ret != BAD_FUNC_ARG)
  40163. return WC_TEST_RET_ENC_EC(ret);
  40164. ret = wc_ExportSakkePrivateKey(key, data, NULL);
  40165. if (ret != BAD_FUNC_ARG)
  40166. return WC_TEST_RET_ENC_EC(ret);
  40167. ret = wc_ExportSakkePrivateKey(NULL, data, &sz);
  40168. if (ret != BAD_FUNC_ARG)
  40169. return WC_TEST_RET_ENC_EC(ret);
  40170. ret = wc_ImportSakkePrivateKey(NULL, NULL, 1);
  40171. if (ret != BAD_FUNC_ARG)
  40172. return WC_TEST_RET_ENC_EC(ret);
  40173. ret = wc_ImportSakkePrivateKey(key, NULL, 1);
  40174. if (ret != BAD_FUNC_ARG)
  40175. return WC_TEST_RET_ENC_EC(ret);
  40176. ret = wc_ImportSakkePrivateKey(NULL, data, 1);
  40177. if (ret != BAD_FUNC_ARG)
  40178. return WC_TEST_RET_ENC_EC(ret);
  40179. sz = sizeof(data);
  40180. ret = wc_EncodeSakkeRsk(NULL, NULL, data, NULL, 1);
  40181. if (ret != BAD_FUNC_ARG)
  40182. return WC_TEST_RET_ENC_EC(ret);
  40183. ret = wc_EncodeSakkeRsk(key, rsk, data, NULL, 1);
  40184. if (ret != BAD_FUNC_ARG)
  40185. return WC_TEST_RET_ENC_EC(ret);
  40186. ret = wc_EncodeSakkeRsk(key, NULL, data, &sz, 1);
  40187. if (ret != BAD_FUNC_ARG)
  40188. return WC_TEST_RET_ENC_EC(ret);
  40189. ret = wc_EncodeSakkeRsk(NULL, rsk, data, &sz, 1);
  40190. if (ret != BAD_FUNC_ARG)
  40191. return WC_TEST_RET_ENC_EC(ret);
  40192. ret = wc_DecodeSakkeRsk(NULL, NULL, sz, NULL);
  40193. if (ret != BAD_FUNC_ARG)
  40194. return WC_TEST_RET_ENC_EC(ret);
  40195. ret = wc_DecodeSakkeRsk(key, data, sz, NULL);
  40196. if (ret != BAD_FUNC_ARG)
  40197. return WC_TEST_RET_ENC_EC(ret);
  40198. ret = wc_DecodeSakkeRsk(key, NULL, sz, rsk);
  40199. if (ret != BAD_FUNC_ARG)
  40200. return WC_TEST_RET_ENC_EC(ret);
  40201. ret = wc_DecodeSakkeRsk(NULL, data, sz, rsk);
  40202. if (ret != BAD_FUNC_ARG)
  40203. return WC_TEST_RET_ENC_EC(ret);
  40204. ret = wc_ImportSakkeRsk(NULL, NULL, sz);
  40205. if (ret != BAD_FUNC_ARG)
  40206. return WC_TEST_RET_ENC_EC(ret);
  40207. ret = wc_ImportSakkeRsk(key, NULL, sz);
  40208. if (ret != BAD_FUNC_ARG)
  40209. return WC_TEST_RET_ENC_EC(ret);
  40210. ret = wc_ImportSakkeRsk(NULL, data, sz);
  40211. if (ret != BAD_FUNC_ARG)
  40212. return WC_TEST_RET_ENC_EC(ret);
  40213. ret = wc_ImportSakkeRsk(key, data, 1);
  40214. if (ret != BUFFER_E)
  40215. return WC_TEST_RET_ENC_EC(ret);
  40216. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, NULL);
  40217. if (ret != BAD_FUNC_ARG)
  40218. return WC_TEST_RET_ENC_EC(ret);
  40219. ret = wc_GenerateSakkeRskTable(key, NULL, data, NULL);
  40220. if (ret != BAD_FUNC_ARG)
  40221. return WC_TEST_RET_ENC_EC(ret);
  40222. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, NULL);
  40223. if (ret != BAD_FUNC_ARG)
  40224. return WC_TEST_RET_ENC_EC(ret);
  40225. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, &len);
  40226. if (ret != BAD_FUNC_ARG)
  40227. return WC_TEST_RET_ENC_EC(ret);
  40228. ret = wc_GenerateSakkeRskTable(key, rsk, data, NULL);
  40229. if (ret != BAD_FUNC_ARG)
  40230. return WC_TEST_RET_ENC_EC(ret);
  40231. ret = wc_GenerateSakkeRskTable(key, NULL, data, &len);
  40232. if (ret != BAD_FUNC_ARG)
  40233. return WC_TEST_RET_ENC_EC(ret);
  40234. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, &len);
  40235. if (ret != BAD_FUNC_ARG)
  40236. return WC_TEST_RET_ENC_EC(ret);
  40237. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  40238. if (ret != LENGTH_ONLY_E)
  40239. return WC_TEST_RET_ENC_EC(ret);
  40240. len--;
  40241. ret = wc_GenerateSakkeRskTable(key, rsk, data, &len);
  40242. if (ret != BUFFER_E)
  40243. return WC_TEST_RET_ENC_EC(ret);
  40244. ret = wc_ExportSakkePublicKey(NULL, data, NULL, 1);
  40245. if (ret != BAD_FUNC_ARG)
  40246. return WC_TEST_RET_ENC_EC(ret);
  40247. ret = wc_ExportSakkePublicKey(key, data, NULL, 1);
  40248. if (ret != BAD_FUNC_ARG)
  40249. return WC_TEST_RET_ENC_EC(ret);
  40250. ret = wc_ExportSakkePublicKey(NULL, data, &sz, 1);
  40251. if (ret != BAD_FUNC_ARG)
  40252. return WC_TEST_RET_ENC_EC(ret);
  40253. ret = wc_ImportSakkePublicKey(NULL, NULL, sz, 1);
  40254. if (ret != BAD_FUNC_ARG)
  40255. return WC_TEST_RET_ENC_EC(ret);
  40256. ret = wc_ImportSakkePublicKey(key, NULL, sz, 1);
  40257. if (ret != BAD_FUNC_ARG)
  40258. return WC_TEST_RET_ENC_EC(ret);
  40259. ret = wc_ImportSakkePublicKey(NULL, data, sz, 1);
  40260. if (ret != BAD_FUNC_ARG)
  40261. return WC_TEST_RET_ENC_EC(ret);
  40262. ret = wc_GetSakkeAuthSize(NULL, NULL);
  40263. if (ret != BAD_FUNC_ARG)
  40264. return WC_TEST_RET_ENC_EC(ret);
  40265. ret = wc_GetSakkeAuthSize(key, NULL);
  40266. if (ret != BAD_FUNC_ARG)
  40267. return WC_TEST_RET_ENC_EC(ret);
  40268. ret = wc_GetSakkeAuthSize(NULL, &authSz);
  40269. if (ret != BAD_FUNC_ARG)
  40270. return WC_TEST_RET_ENC_EC(ret);
  40271. ret = wc_MakeSakkePointI(NULL, NULL, SAKKE_ID_MAX_SIZE + 1);
  40272. if (ret != BAD_FUNC_ARG)
  40273. return WC_TEST_RET_ENC_EC(ret);
  40274. ret = wc_MakeSakkePointI(key, NULL, SAKKE_ID_MAX_SIZE + 1);
  40275. if (ret != BAD_FUNC_ARG)
  40276. return WC_TEST_RET_ENC_EC(ret);
  40277. ret = wc_MakeSakkePointI(NULL, id, 1);
  40278. if (ret != BAD_FUNC_ARG)
  40279. return WC_TEST_RET_ENC_EC(ret);
  40280. ret = wc_MakeSakkePointI(NULL, NULL, 1);
  40281. if (ret != BAD_FUNC_ARG)
  40282. return WC_TEST_RET_ENC_EC(ret);
  40283. ret = wc_MakeSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1);
  40284. if (ret != BAD_FUNC_ARG)
  40285. return WC_TEST_RET_ENC_EC(ret);
  40286. ret = wc_MakeSakkePointI(key, NULL, 1);
  40287. if (ret != BAD_FUNC_ARG)
  40288. return WC_TEST_RET_ENC_EC(ret);
  40289. ret = wc_MakeSakkePointI(NULL, id, 1);
  40290. if (ret != BAD_FUNC_ARG)
  40291. return WC_TEST_RET_ENC_EC(ret);
  40292. ret = wc_GenerateSakkePointITable(NULL, data, NULL);
  40293. if (ret != BAD_FUNC_ARG)
  40294. return WC_TEST_RET_ENC_EC(ret);
  40295. ret = wc_GenerateSakkePointITable(key, data, NULL);
  40296. if (ret != BAD_FUNC_ARG)
  40297. return WC_TEST_RET_ENC_EC(ret);
  40298. ret = wc_GenerateSakkePointITable(NULL, data, &len);
  40299. if (ret != BAD_FUNC_ARG)
  40300. return WC_TEST_RET_ENC_EC(ret);
  40301. ret = wc_GenerateSakkePointITable(key, NULL, &len);
  40302. if (ret != LENGTH_ONLY_E)
  40303. return WC_TEST_RET_ENC_EC(ret);
  40304. len--;
  40305. ret = wc_GenerateSakkePointITable(key, data, &len);
  40306. if (ret != BUFFER_E)
  40307. return WC_TEST_RET_ENC_EC(ret);
  40308. ret = wc_SetSakkePointITable(NULL, NULL, 1);
  40309. if (ret != BAD_FUNC_ARG)
  40310. return WC_TEST_RET_ENC_EC(ret);
  40311. ret = wc_SetSakkePointITable(key, NULL, 1);
  40312. if (ret != BAD_FUNC_ARG)
  40313. return WC_TEST_RET_ENC_EC(ret);
  40314. ret = wc_SetSakkePointITable(NULL, data, 1);
  40315. if (ret != BAD_FUNC_ARG)
  40316. return WC_TEST_RET_ENC_EC(ret);
  40317. ret = wc_SetSakkePointITable(key, data, 1);
  40318. if (ret != BUFFER_E)
  40319. return WC_TEST_RET_ENC_EC(ret);
  40320. ret = wc_ClearSakkePointITable(NULL);
  40321. if (ret != BAD_FUNC_ARG)
  40322. return WC_TEST_RET_ENC_EC(ret);
  40323. ret = wc_GetSakkePointI(NULL, data, NULL);
  40324. if (ret != BAD_FUNC_ARG)
  40325. return WC_TEST_RET_ENC_EC(ret);
  40326. ret = wc_GetSakkePointI(key, data, NULL);
  40327. if (ret != BAD_FUNC_ARG)
  40328. return WC_TEST_RET_ENC_EC(ret);
  40329. ret = wc_GetSakkePointI(NULL, data, &sz);
  40330. if (ret != BAD_FUNC_ARG)
  40331. return WC_TEST_RET_ENC_EC(ret);
  40332. sz = 1;
  40333. ret = wc_GetSakkePointI(key, data, &sz);
  40334. if (ret != BUFFER_E)
  40335. return WC_TEST_RET_ENC_EC(ret);
  40336. sz = 256;
  40337. ret = wc_SetSakkePointI(NULL, NULL, 1, NULL, sz);
  40338. if (ret != BAD_FUNC_ARG)
  40339. return WC_TEST_RET_ENC_EC(ret);
  40340. ret = wc_SetSakkePointI(key, NULL, 1, NULL, sz);
  40341. if (ret != BAD_FUNC_ARG)
  40342. return WC_TEST_RET_ENC_EC(ret);
  40343. ret = wc_SetSakkePointI(NULL, id, 1, NULL, sz);
  40344. if (ret != BAD_FUNC_ARG)
  40345. return WC_TEST_RET_ENC_EC(ret);
  40346. ret = wc_SetSakkePointI(NULL, NULL, 1, data, sz);
  40347. if (ret != BAD_FUNC_ARG)
  40348. return WC_TEST_RET_ENC_EC(ret);
  40349. ret = wc_SetSakkePointI(key, id, 1, NULL, sz);
  40350. if (ret != BAD_FUNC_ARG)
  40351. return WC_TEST_RET_ENC_EC(ret);
  40352. ret = wc_SetSakkePointI(key, NULL, 1, data, sz);
  40353. if (ret != BAD_FUNC_ARG)
  40354. return WC_TEST_RET_ENC_EC(ret);
  40355. ret = wc_SetSakkePointI(NULL, id, 1, data, sz);
  40356. if (ret != BAD_FUNC_ARG)
  40357. return WC_TEST_RET_ENC_EC(ret);
  40358. ret = wc_SetSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1, data, sz);
  40359. if (ret != BUFFER_E)
  40360. return WC_TEST_RET_ENC_EC(ret);
  40361. ret = wc_SetSakkePointI(key, id, 1, data, sz - 1);
  40362. if (ret != BUFFER_E)
  40363. return WC_TEST_RET_ENC_EC(ret);
  40364. ret = wc_SetSakkeIdentity(NULL, NULL, 1);
  40365. if (ret != BAD_FUNC_ARG)
  40366. return WC_TEST_RET_ENC_EC(ret);
  40367. ret = wc_SetSakkeIdentity(key, NULL, 1);
  40368. if (ret != BAD_FUNC_ARG)
  40369. return WC_TEST_RET_ENC_EC(ret);
  40370. ret = wc_SetSakkeIdentity(NULL, id, 1);
  40371. if (ret != BAD_FUNC_ARG)
  40372. return WC_TEST_RET_ENC_EC(ret);
  40373. ssvSz = sizeof(ssv);
  40374. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  40375. auth, NULL);
  40376. if (ret != BAD_FUNC_ARG)
  40377. return WC_TEST_RET_ENC_EC(ret);
  40378. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  40379. auth, NULL);
  40380. if (ret != BAD_FUNC_ARG)
  40381. return WC_TEST_RET_ENC_EC(ret);
  40382. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  40383. auth, NULL);
  40384. if (ret != BAD_FUNC_ARG)
  40385. return WC_TEST_RET_ENC_EC(ret);
  40386. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  40387. auth, &authSz);
  40388. if (ret != BAD_FUNC_ARG)
  40389. return WC_TEST_RET_ENC_EC(ret);
  40390. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  40391. auth, NULL);
  40392. if (ret != BAD_FUNC_ARG)
  40393. return WC_TEST_RET_ENC_EC(ret);
  40394. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  40395. auth, &authSz);
  40396. if (ret != BAD_FUNC_ARG)
  40397. return WC_TEST_RET_ENC_EC(ret);
  40398. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  40399. auth, &authSz);
  40400. if (ret != BAD_FUNC_ARG)
  40401. return WC_TEST_RET_ENC_EC(ret);
  40402. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  40403. auth, &authSz);
  40404. if (ret != BAD_STATE_E)
  40405. return WC_TEST_RET_ENC_EC(ret);
  40406. ret = wc_GenerateSakkeSSV(NULL, NULL, data, NULL);
  40407. if (ret != BAD_FUNC_ARG)
  40408. return WC_TEST_RET_ENC_EC(ret);
  40409. ret = wc_GenerateSakkeSSV(key, rng, data, NULL);
  40410. if (ret != BAD_FUNC_ARG)
  40411. return WC_TEST_RET_ENC_EC(ret);
  40412. ret = wc_GenerateSakkeSSV(key, NULL, data, &ssvSz);
  40413. if (ret != BAD_FUNC_ARG)
  40414. return WC_TEST_RET_ENC_EC(ret);
  40415. ret = wc_GenerateSakkeSSV(NULL, rng, data, &ssvSz);
  40416. if (ret != BAD_FUNC_ARG)
  40417. return WC_TEST_RET_ENC_EC(ret);
  40418. ret = wc_SetSakkeRsk(NULL, NULL, data, 1);
  40419. if (ret != BAD_FUNC_ARG)
  40420. return WC_TEST_RET_ENC_EC(ret);
  40421. ret = wc_SetSakkeRsk(key, NULL, data, 1);
  40422. if (ret != BAD_FUNC_ARG)
  40423. return WC_TEST_RET_ENC_EC(ret);
  40424. ret = wc_SetSakkeRsk(NULL, rsk, data, 1);
  40425. if (ret != BAD_FUNC_ARG)
  40426. return WC_TEST_RET_ENC_EC(ret);
  40427. ssvSz = sizeof(ssv);
  40428. authSz = sizeof(auth);
  40429. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  40430. authSz);
  40431. if (ret != BAD_FUNC_ARG)
  40432. return WC_TEST_RET_ENC_EC(ret);
  40433. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  40434. authSz);
  40435. if (ret != BAD_FUNC_ARG)
  40436. return WC_TEST_RET_ENC_EC(ret);
  40437. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  40438. authSz);
  40439. if (ret != BAD_FUNC_ARG)
  40440. return WC_TEST_RET_ENC_EC(ret);
  40441. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  40442. authSz);
  40443. if (ret != BAD_FUNC_ARG)
  40444. return WC_TEST_RET_ENC_EC(ret);
  40445. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  40446. authSz);
  40447. if (ret != BAD_FUNC_ARG)
  40448. return WC_TEST_RET_ENC_EC(ret);
  40449. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  40450. authSz);
  40451. if (ret != BAD_FUNC_ARG)
  40452. return WC_TEST_RET_ENC_EC(ret);
  40453. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  40454. authSz);
  40455. if (ret != BAD_FUNC_ARG)
  40456. return WC_TEST_RET_ENC_EC(ret);
  40457. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  40458. authSz);
  40459. if (ret != BAD_STATE_E)
  40460. return WC_TEST_RET_ENC_EC(ret);
  40461. ret = wc_SetSakkeIdentity(key, id, 1);
  40462. if (ret != 0)
  40463. return WC_TEST_RET_ENC_EC(ret);
  40464. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  40465. authSz);
  40466. if (ret != BAD_STATE_E)
  40467. return WC_TEST_RET_ENC_EC(ret);
  40468. ret = wc_SetSakkeIdentity(key, id, 0);
  40469. if (ret != 0)
  40470. return WC_TEST_RET_ENC_EC(ret);
  40471. ret = wc_SetSakkeRsk(key, rsk, data, 1);
  40472. if (ret != 0)
  40473. return WC_TEST_RET_ENC_EC(ret);
  40474. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  40475. authSz);
  40476. if (ret != BAD_STATE_E)
  40477. return WC_TEST_RET_ENC_EC(ret);
  40478. wc_FreeSakkeKey(key);
  40479. return 0;
  40480. }
  40481. static wc_test_ret_t sakke_kat_derive_test(SakkeKey* key, ecc_point* rsk)
  40482. {
  40483. WOLFSSL_SMALL_STACK_STATIC const byte pubData[] = {
  40484. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  40485. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  40486. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  40487. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  40488. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  40489. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  40490. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  40491. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  40492. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  40493. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  40494. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  40495. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  40496. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  40497. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  40498. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  40499. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  40500. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  40501. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  40502. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  40503. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  40504. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  40505. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  40506. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  40507. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  40508. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  40509. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  40510. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  40511. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  40512. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  40513. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  40514. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  40515. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  40516. };
  40517. WOLFSSL_SMALL_STACK_STATIC const byte rskData[] = {
  40518. 0x93, 0xAF, 0x67, 0xE5, 0x00, 0x7B, 0xA6, 0xE6,
  40519. 0xA8, 0x0D, 0xA7, 0x93, 0xDA, 0x30, 0x0F, 0xA4,
  40520. 0xB5, 0x2D, 0x0A, 0x74, 0xE2, 0x5E, 0x6E, 0x7B,
  40521. 0x2B, 0x3D, 0x6E, 0xE9, 0xD1, 0x8A, 0x9B, 0x5C,
  40522. 0x50, 0x23, 0x59, 0x7B, 0xD8, 0x2D, 0x80, 0x62,
  40523. 0xD3, 0x40, 0x19, 0x56, 0x3B, 0xA1, 0xD2, 0x5C,
  40524. 0x0D, 0xC5, 0x6B, 0x7B, 0x97, 0x9D, 0x74, 0xAA,
  40525. 0x50, 0xF2, 0x9F, 0xBF, 0x11, 0xCC, 0x2C, 0x93,
  40526. 0xF5, 0xDF, 0xCA, 0x61, 0x5E, 0x60, 0x92, 0x79,
  40527. 0xF6, 0x17, 0x5C, 0xEA, 0xDB, 0x00, 0xB5, 0x8C,
  40528. 0x6B, 0xEE, 0x1E, 0x7A, 0x2A, 0x47, 0xC4, 0xF0,
  40529. 0xC4, 0x56, 0xF0, 0x52, 0x59, 0xA6, 0xFA, 0x94,
  40530. 0xA6, 0x34, 0xA4, 0x0D, 0xAE, 0x1D, 0xF5, 0x93,
  40531. 0xD4, 0xFE, 0xCF, 0x68, 0x8D, 0x5F, 0xC6, 0x78,
  40532. 0xBE, 0x7E, 0xFC, 0x6D, 0xF3, 0xD6, 0x83, 0x53,
  40533. 0x25, 0xB8, 0x3B, 0x2C, 0x6E, 0x69, 0x03, 0x6B,
  40534. 0x15, 0x5F, 0x0A, 0x27, 0x24, 0x10, 0x94, 0xB0,
  40535. 0x4B, 0xFB, 0x0B, 0xDF, 0xAC, 0x6C, 0x67, 0x0A,
  40536. 0x65, 0xC3, 0x25, 0xD3, 0x9A, 0x06, 0x9F, 0x03,
  40537. 0x65, 0x9D, 0x44, 0xCA, 0x27, 0xD3, 0xBE, 0x8D,
  40538. 0xF3, 0x11, 0x17, 0x2B, 0x55, 0x41, 0x60, 0x18,
  40539. 0x1C, 0xBE, 0x94, 0xA2, 0xA7, 0x83, 0x32, 0x0C,
  40540. 0xED, 0x59, 0x0B, 0xC4, 0x26, 0x44, 0x70, 0x2C,
  40541. 0xF3, 0x71, 0x27, 0x1E, 0x49, 0x6B, 0xF2, 0x0F,
  40542. 0x58, 0x8B, 0x78, 0xA1, 0xBC, 0x01, 0xEC, 0xBB,
  40543. 0x65, 0x59, 0x93, 0x4B, 0xDD, 0x2F, 0xB6, 0x5D,
  40544. 0x28, 0x84, 0x31, 0x8A, 0x33, 0xD1, 0xA4, 0x2A,
  40545. 0xDF, 0x5E, 0x33, 0xCC, 0x58, 0x00, 0x28, 0x0B,
  40546. 0x28, 0x35, 0x64, 0x97, 0xF8, 0x71, 0x35, 0xBA,
  40547. 0xB9, 0x61, 0x2A, 0x17, 0x26, 0x04, 0x24, 0x40,
  40548. 0x9A, 0xC1, 0x5F, 0xEE, 0x99, 0x6B, 0x74, 0x4C,
  40549. 0x33, 0x21, 0x51, 0x23, 0x5D, 0xEC, 0xB0, 0xF5
  40550. };
  40551. WOLFSSL_SMALL_STACK_STATIC const byte id[] = {
  40552. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  40553. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  40554. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  40555. 0x33, 0x00
  40556. };
  40557. WOLFSSL_SMALL_STACK_STATIC const byte ssv[] = {
  40558. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  40559. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  40560. };
  40561. WOLFSSL_SMALL_STACK_STATIC const byte auth[] = {
  40562. 0x04,
  40563. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  40564. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  40565. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  40566. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  40567. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  40568. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  40569. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  40570. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  40571. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  40572. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  40573. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  40574. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  40575. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  40576. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  40577. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  40578. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  40579. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  40580. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  40581. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  40582. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  40583. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  40584. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  40585. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  40586. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  40587. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  40588. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  40589. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  40590. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  40591. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  40592. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  40593. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  40594. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  40595. };
  40596. WOLFSSL_SMALL_STACK_STATIC const byte encSsv[] = {
  40597. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  40598. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  40599. };
  40600. wc_test_ret_t ret;
  40601. int valid;
  40602. byte pubKey[sizeof(pubData) + 1];
  40603. word32 sz = sizeof(pubKey);
  40604. byte tmpSsv[sizeof(encSsv)];
  40605. byte* iTable = NULL;
  40606. word32 iTableLen;
  40607. byte* table = NULL;
  40608. word32 len;
  40609. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  40610. if (ret != 0)
  40611. return WC_TEST_RET_ENC_EC(ret);
  40612. ret = wc_DecodeSakkeRsk(key, rskData, sizeof(rskData), rsk);
  40613. if (ret != 0)
  40614. return WC_TEST_RET_ENC_EC(ret);
  40615. ret = wc_ValidateSakkeRsk(key, id, sizeof(id), rsk, &valid);
  40616. if (ret != 0)
  40617. return WC_TEST_RET_ENC_EC(ret);
  40618. if (valid != 1)
  40619. return WC_TEST_RET_ENC_NC;
  40620. ret = wc_SetSakkeRsk(key, rsk, NULL, 0);
  40621. if (ret != 0)
  40622. return WC_TEST_RET_ENC_EC(ret);
  40623. ret = wc_SetSakkeIdentity(key, id, sizeof(id));
  40624. if (ret != 0)
  40625. return WC_TEST_RET_ENC_EC(ret);
  40626. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  40627. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  40628. auth, sizeof(auth));
  40629. if (ret != 0)
  40630. return WC_TEST_RET_ENC_EC(ret);
  40631. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  40632. return WC_TEST_RET_ENC_NC;
  40633. ret = wc_MakeSakkePointI(key, id, sizeof(id));
  40634. if (ret != 0)
  40635. return WC_TEST_RET_ENC_EC(ret);
  40636. iTableLen = 0;
  40637. ret = wc_GenerateSakkePointITable(key, NULL, &iTableLen);
  40638. if (ret != LENGTH_ONLY_E)
  40639. return WC_TEST_RET_ENC_EC(ret);
  40640. if (iTableLen != 0) {
  40641. iTable = (byte*)XMALLOC(iTableLen, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40642. if (iTable == NULL)
  40643. return WC_TEST_RET_ENC_ERRNO;
  40644. ret = wc_GenerateSakkePointITable(key, iTable, &iTableLen);
  40645. if (ret != 0)
  40646. return WC_TEST_RET_ENC_EC(ret);
  40647. }
  40648. len = 0;
  40649. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  40650. if (ret != LENGTH_ONLY_E)
  40651. return WC_TEST_RET_ENC_EC(ret);
  40652. if (len > 0) {
  40653. table = (byte*)XMALLOC(len, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40654. if (table == NULL)
  40655. return WC_TEST_RET_ENC_ERRNO;
  40656. ret = wc_GenerateSakkeRskTable(key, rsk, table, &len);
  40657. if (ret != 0)
  40658. return WC_TEST_RET_ENC_EC(ret);
  40659. }
  40660. ret = wc_SetSakkeRsk(key, rsk, table, len);
  40661. if (ret != 0)
  40662. return WC_TEST_RET_ENC_EC(ret);
  40663. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  40664. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  40665. auth, sizeof(auth));
  40666. if (ret != 0)
  40667. return WC_TEST_RET_ENC_EC(ret);
  40668. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  40669. return WC_TEST_RET_ENC_NC;
  40670. /* Don't reference table that is about to be freed. */
  40671. ret = wc_ClearSakkePointITable(key);
  40672. if (ret != 0)
  40673. return WC_TEST_RET_ENC_EC(ret);
  40674. /* Dispose of tables */
  40675. if (iTable != NULL)
  40676. XFREE(iTable, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40677. if (table != NULL)
  40678. XFREE(table, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40679. /* Make sure the key public key is exportable - convert to Montgomery form
  40680. * in Validation.
  40681. */
  40682. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 1);
  40683. if (ret != 0)
  40684. return WC_TEST_RET_ENC_EC(ret);
  40685. if (sz != sizeof(pubData))
  40686. return WC_TEST_RET_ENC_NC;
  40687. if (XMEMCMP(pubKey, pubData, sizeof(pubData)) != 0)
  40688. return WC_TEST_RET_ENC_NC;
  40689. sz = sizeof(pubData) + 1;
  40690. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 0);
  40691. if (ret != 0)
  40692. return WC_TEST_RET_ENC_EC(ret);
  40693. if (sz != sizeof(pubData) + 1)
  40694. return WC_TEST_RET_ENC_NC;
  40695. if (pubKey[0] != 0x04)
  40696. return WC_TEST_RET_ENC_NC;
  40697. if (XMEMCMP(pubKey + 1, pubData, sizeof(pubData)) != 0)
  40698. return WC_TEST_RET_ENC_NC;
  40699. return 0;
  40700. }
  40701. static wc_test_ret_t sakke_kat_encapsulate_test(SakkeKey* key)
  40702. {
  40703. static const byte pubData[] = {
  40704. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  40705. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  40706. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  40707. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  40708. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  40709. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  40710. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  40711. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  40712. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  40713. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  40714. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  40715. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  40716. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  40717. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  40718. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  40719. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  40720. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  40721. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  40722. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  40723. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  40724. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  40725. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  40726. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  40727. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  40728. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  40729. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  40730. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  40731. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  40732. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  40733. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  40734. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  40735. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  40736. };
  40737. static const byte id[] = {
  40738. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  40739. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  40740. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  40741. 0x33, 0x00
  40742. };
  40743. static const word32 idSz = sizeof(id);
  40744. byte ssv[] = {
  40745. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  40746. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  40747. };
  40748. static const word16 ssvSz = sizeof(ssv);
  40749. static const byte expAuth[] = {
  40750. 0x04,
  40751. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  40752. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  40753. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  40754. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  40755. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  40756. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  40757. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  40758. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  40759. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  40760. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  40761. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  40762. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  40763. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  40764. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  40765. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  40766. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  40767. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  40768. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  40769. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  40770. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  40771. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  40772. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  40773. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  40774. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  40775. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  40776. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  40777. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  40778. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  40779. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  40780. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  40781. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  40782. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  40783. };
  40784. static const byte encSsv[] = {
  40785. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  40786. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  40787. };
  40788. wc_test_ret_t ret;
  40789. byte auth[257];
  40790. word16 authSz = sizeof(auth);
  40791. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  40792. if (ret != 0)
  40793. return WC_TEST_RET_ENC_EC(ret);
  40794. ret = wc_SetSakkeIdentity(key, id, idSz);
  40795. if (ret != 0)
  40796. return WC_TEST_RET_ENC_EC(ret);
  40797. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  40798. auth, &authSz);
  40799. if (ret != 0)
  40800. return WC_TEST_RET_ENC_EC(ret);
  40801. if (authSz != 257)
  40802. return WC_TEST_RET_ENC_NC;
  40803. if (XMEMCMP(ssv, encSsv, ssvSz) != 0)
  40804. return WC_TEST_RET_ENC_NC;
  40805. if (XMEMCMP(auth, expAuth, authSz) != 0)
  40806. return WC_TEST_RET_ENC_NC;
  40807. return 0;
  40808. }
  40809. static wc_test_ret_t sakke_make_key_test(SakkeKey* priv, SakkeKey* pub, SakkeKey* key,
  40810. WC_RNG* rng, ecc_point* rsk)
  40811. {
  40812. wc_test_ret_t ret;
  40813. byte data[440];
  40814. byte pubData[257];
  40815. word32 sz;
  40816. char mail[] = "test@wolfssl.com";
  40817. byte* id = (byte*)mail;
  40818. word32 idSz = (word32)XSTRLEN(mail);
  40819. int valid;
  40820. ecc_point* pubKey = rsk;
  40821. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  40822. if (ret != 0)
  40823. return WC_TEST_RET_ENC_EC(ret);
  40824. ret = wc_MakeSakkeKey(priv, rng);
  40825. if (ret != 0)
  40826. return WC_TEST_RET_ENC_EC(ret);
  40827. ret = wc_ExportSakkeKey(priv, NULL, &sz);
  40828. if (ret != LENGTH_ONLY_E)
  40829. return WC_TEST_RET_ENC_EC(ret);
  40830. if (sz != 384)
  40831. return WC_TEST_RET_ENC_NC;
  40832. sz--;
  40833. ret = wc_ExportSakkeKey(priv, data, &sz);
  40834. if (ret == 0)
  40835. return WC_TEST_RET_ENC_NC;
  40836. sz++;
  40837. ret = wc_ExportSakkeKey(priv, data, &sz);
  40838. if (ret != 0)
  40839. return WC_TEST_RET_ENC_EC(ret);
  40840. if (sz != 384)
  40841. return WC_TEST_RET_ENC_NC;
  40842. ret = wc_ImportSakkeKey(key, data, sz - 1);
  40843. if (ret == 0)
  40844. return WC_TEST_RET_ENC_NC;
  40845. ret = wc_ImportSakkeKey(key, data, sz);
  40846. if (ret != 0)
  40847. return WC_TEST_RET_ENC_EC(ret);
  40848. wc_FreeSakkeKey(key);
  40849. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  40850. if (ret != 0)
  40851. return WC_TEST_RET_ENC_EC(ret);
  40852. ret = wc_ExportSakkePrivateKey(priv, NULL, &sz);
  40853. if (ret != LENGTH_ONLY_E)
  40854. return WC_TEST_RET_ENC_EC(ret);
  40855. if (sz != 128)
  40856. return WC_TEST_RET_ENC_NC;
  40857. sz--;
  40858. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  40859. if (ret == 0)
  40860. return WC_TEST_RET_ENC_NC;
  40861. sz++;
  40862. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  40863. if (ret != 0)
  40864. return WC_TEST_RET_ENC_EC(ret);
  40865. if (sz != 128)
  40866. return WC_TEST_RET_ENC_NC;
  40867. ret = wc_ImportSakkePrivateKey(key, data, sz - 1);
  40868. if (ret == 0)
  40869. return WC_TEST_RET_ENC_NC;
  40870. ret = wc_ImportSakkePrivateKey(key, data, sz);
  40871. if (ret != 0)
  40872. return WC_TEST_RET_ENC_EC(ret);
  40873. ret = wc_MakeSakkePublicKey(key, pubKey);
  40874. if (ret != 0)
  40875. return WC_TEST_RET_ENC_EC(ret);
  40876. ret = wc_ExportSakkePublicKey(priv, NULL, &sz, 1);
  40877. if (ret != LENGTH_ONLY_E)
  40878. return WC_TEST_RET_ENC_EC(ret);
  40879. if (sz != 256)
  40880. return WC_TEST_RET_ENC_NC;
  40881. sz--;
  40882. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  40883. if (ret == 0)
  40884. return WC_TEST_RET_ENC_NC;
  40885. sz++;
  40886. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  40887. if (ret != 0)
  40888. return WC_TEST_RET_ENC_EC(ret);
  40889. if (sz != 256)
  40890. return WC_TEST_RET_ENC_NC;
  40891. ret = wc_ImportSakkePublicKey(pub, data, sz - 1, 1);
  40892. if (ret == 0)
  40893. return WC_TEST_RET_ENC_NC;
  40894. ret = wc_ImportSakkePublicKey(pub, data, sz, 1);
  40895. if (ret != 0)
  40896. return WC_TEST_RET_ENC_EC(ret);
  40897. ret = wc_ExportSakkePublicKey(pub, pubData, &sz, 1);
  40898. if (ret != 0)
  40899. return WC_TEST_RET_ENC_EC(ret);
  40900. if (sz != 256)
  40901. return WC_TEST_RET_ENC_NC;
  40902. if (XMEMCMP(data, pubData, sz) != 0)
  40903. return WC_TEST_RET_ENC_NC;
  40904. ret = wc_MakeSakkeRsk(priv, id, idSz, rsk);
  40905. if (ret != 0)
  40906. return WC_TEST_RET_ENC_EC(ret);
  40907. ret = wc_ValidateSakkeRsk(priv, id, idSz, rsk, &valid);
  40908. if (ret != 0)
  40909. return WC_TEST_RET_ENC_EC(ret);
  40910. if (valid != 1)
  40911. return WC_TEST_RET_ENC_NC;
  40912. ret = wc_ValidateSakkeRsk(pub, id, idSz, rsk, &valid);
  40913. if (ret != 0)
  40914. return WC_TEST_RET_ENC_EC(ret);
  40915. if (valid != 1)
  40916. return WC_TEST_RET_ENC_NC;
  40917. sz = sizeof(data);
  40918. ret = wc_EncodeSakkeRsk(priv, rsk, data, &sz, 1);
  40919. if (ret != 0)
  40920. return WC_TEST_RET_ENC_EC(ret);
  40921. if (sz != 256)
  40922. return WC_TEST_RET_ENC_NC;
  40923. ret = wc_DecodeSakkeRsk(priv, data, sz, rsk);
  40924. if (ret != 0)
  40925. return WC_TEST_RET_ENC_EC(ret);
  40926. sz = sizeof(pubData);
  40927. ret = wc_EncodeSakkeRsk(priv, rsk, pubData, &sz, 0);
  40928. if (ret != 0)
  40929. return WC_TEST_RET_ENC_EC(ret);
  40930. if (sz != sizeof(pubData))
  40931. return WC_TEST_RET_ENC_NC;
  40932. ret = wc_DecodeSakkeRsk(priv, pubData, sz, rsk);
  40933. if (ret != 0)
  40934. return WC_TEST_RET_ENC_EC(ret);
  40935. wc_FreeSakkeKey(key);
  40936. return 0;
  40937. }
  40938. static wc_test_ret_t sakke_op_test(SakkeKey* priv, SakkeKey* pub, WC_RNG* rng,
  40939. ecc_point* rsk)
  40940. {
  40941. wc_test_ret_t ret;
  40942. byte ssv[16];
  40943. word16 ssvSz;
  40944. byte auth[257];
  40945. word16 authSz;
  40946. char mail[] = "test@wolfssl.com";
  40947. byte* id = (byte*)mail;
  40948. word32 idSz = (word32)XSTRLEN(mail);
  40949. byte pointI[256];
  40950. word32 sz;
  40951. ret = wc_GenerateSakkeSSV(pub, rng, NULL, &ssvSz);
  40952. if (ret != LENGTH_ONLY_E)
  40953. return WC_TEST_RET_ENC_EC(ret);
  40954. if (ssvSz != 16)
  40955. return WC_TEST_RET_ENC_NC;
  40956. ssvSz += 128;
  40957. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  40958. if (ret == 0)
  40959. return WC_TEST_RET_ENC_NC;
  40960. ssvSz -= 128;
  40961. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  40962. if (ret != 0)
  40963. return WC_TEST_RET_ENC_EC(ret);
  40964. if (ssvSz != 16)
  40965. return WC_TEST_RET_ENC_NC;
  40966. ret = wc_GetSakkeAuthSize(pub, &authSz);
  40967. if (ret != 0)
  40968. return WC_TEST_RET_ENC_EC(ret);
  40969. ret = wc_SetSakkeIdentity(pub, id, idSz);
  40970. if (ret != 0)
  40971. return WC_TEST_RET_ENC_EC(ret);
  40972. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  40973. NULL, &authSz);
  40974. if (ret != LENGTH_ONLY_E)
  40975. return WC_TEST_RET_ENC_EC(ret);
  40976. if (authSz != 257)
  40977. return WC_TEST_RET_ENC_NC;
  40978. authSz--;
  40979. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  40980. auth, &authSz);
  40981. if (ret == 0)
  40982. return WC_TEST_RET_ENC_NC;
  40983. authSz++;
  40984. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  40985. auth, &authSz);
  40986. if (ret != 0)
  40987. return WC_TEST_RET_ENC_EC(ret);
  40988. if (authSz != 257)
  40989. return WC_TEST_RET_ENC_NC;
  40990. ret = wc_GetSakkePointI(pub, NULL, &sz);
  40991. if (ret != LENGTH_ONLY_E)
  40992. return WC_TEST_RET_ENC_EC(ret);
  40993. if (sz != 256)
  40994. return WC_TEST_RET_ENC_NC;
  40995. ret = wc_GetSakkePointI(pub, pointI, &sz);
  40996. if (ret != 0)
  40997. return WC_TEST_RET_ENC_EC(ret);
  40998. if (sz != 256)
  40999. return WC_TEST_RET_ENC_NC;
  41000. /* Bogus identity - make it check and regenerate I. */
  41001. ret = wc_MakeSakkePointI(pub, ssv, ssvSz);
  41002. if (ret != 0)
  41003. return WC_TEST_RET_ENC_EC(ret);
  41004. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  41005. auth, &authSz);
  41006. if (ret != 0)
  41007. return WC_TEST_RET_ENC_EC(ret);
  41008. if (authSz != 257)
  41009. return WC_TEST_RET_ENC_NC;
  41010. ret = wc_SetSakkeRsk(priv, rsk, NULL, 0);
  41011. if (ret != 0)
  41012. return WC_TEST_RET_ENC_EC(ret);
  41013. ret = wc_SetSakkeIdentity(priv, id, idSz);
  41014. if (ret != 0)
  41015. return WC_TEST_RET_ENC_EC(ret);
  41016. authSz--;
  41017. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  41018. authSz);
  41019. if (ret == 0)
  41020. return WC_TEST_RET_ENC_NC;
  41021. authSz++;
  41022. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  41023. authSz);
  41024. if (ret != 0)
  41025. return WC_TEST_RET_ENC_EC(ret);
  41026. ssv[0] ^= 0x80;
  41027. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  41028. authSz);
  41029. if (ret != SAKKE_VERIFY_FAIL_E)
  41030. return WC_TEST_RET_ENC_EC(ret);
  41031. ssv[0] ^= 0x80;
  41032. /* Bogus identity - make it check and regenerate I. */
  41033. ret = wc_MakeSakkePointI(pub, ssv, idSz);
  41034. if (ret != 0)
  41035. return WC_TEST_RET_ENC_EC(ret);
  41036. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  41037. authSz);
  41038. if (ret != 0)
  41039. return WC_TEST_RET_ENC_EC(ret);
  41040. return 0;
  41041. }
  41042. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sakke_test(void)
  41043. {
  41044. wc_test_ret_t ret = 0;
  41045. WC_RNG rng;
  41046. int rng_inited = 0;
  41047. SakkeKey* priv = NULL;
  41048. SakkeKey* pub = NULL;
  41049. SakkeKey* key = NULL;
  41050. ecc_point* rsk = NULL;
  41051. WOLFSSL_ENTER("sakke_test");
  41052. priv = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  41053. DYNAMIC_TYPE_TMP_BUFFER);
  41054. if (priv == NULL)
  41055. ret = WC_TEST_RET_ENC_NC;
  41056. else
  41057. XMEMSET(priv, 0, sizeof(*priv));
  41058. if (ret == 0) {
  41059. pub = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  41060. DYNAMIC_TYPE_TMP_BUFFER);
  41061. if (pub == NULL)
  41062. ret = WC_TEST_RET_ENC_NC;
  41063. else
  41064. XMEMSET(pub, 0, sizeof(*pub));
  41065. }
  41066. if (ret == 0) {
  41067. key = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  41068. DYNAMIC_TYPE_TMP_BUFFER);
  41069. if (key == NULL)
  41070. ret = WC_TEST_RET_ENC_NC;
  41071. else
  41072. XMEMSET(key, 0, sizeof(*key));
  41073. }
  41074. if (ret == 0) {
  41075. #ifndef HAVE_FIPS
  41076. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  41077. #else
  41078. ret = wc_InitRng(&rng);
  41079. #endif
  41080. if (ret == 0)
  41081. rng_inited = 1;
  41082. else
  41083. ret = WC_TEST_RET_ENC_EC(ret);
  41084. }
  41085. if (ret == 0) {
  41086. rsk = wc_ecc_new_point();
  41087. if (rsk == NULL)
  41088. ret = WC_TEST_RET_ENC_NC;
  41089. }
  41090. if (ret == 0) {
  41091. ret = wc_InitSakkeKey(pub, HEAP_HINT, INVALID_DEVID);
  41092. if (ret != 0)
  41093. ret = WC_TEST_RET_ENC_EC(ret);
  41094. }
  41095. if (ret == 0) {
  41096. ret = wc_InitSakkeKey(priv, HEAP_HINT, INVALID_DEVID);
  41097. if (ret != 0)
  41098. ret = WC_TEST_RET_ENC_EC(ret);
  41099. }
  41100. if (ret == 0) {
  41101. ret = sakke_api_test(&rng, key, rsk);
  41102. }
  41103. if (ret == 0) {
  41104. ret = sakke_kat_derive_test(pub, rsk);
  41105. }
  41106. if (ret == 0) {
  41107. ret = sakke_kat_encapsulate_test(pub);
  41108. }
  41109. if (ret == 0) {
  41110. ret = sakke_make_key_test(priv, pub, key, &rng, rsk);
  41111. }
  41112. if (ret == 0) {
  41113. ret = sakke_op_test(priv, pub, &rng, rsk);
  41114. }
  41115. if (rsk != NULL) {
  41116. wc_ecc_forcezero_point(rsk);
  41117. wc_ecc_del_point(rsk);
  41118. }
  41119. if (rng_inited)
  41120. wc_FreeRng(&rng);
  41121. if (key != NULL)
  41122. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41123. if (pub != NULL) {
  41124. wc_FreeSakkeKey(pub);
  41125. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41126. }
  41127. if (priv != NULL) {
  41128. wc_FreeSakkeKey(priv);
  41129. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41130. }
  41131. return ret;
  41132. }
  41133. #endif /* WOLFCRYPT_HAVE_SAKKE */
  41134. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  41135. typedef struct CMAC_Test_Case {
  41136. int type;
  41137. int partial;
  41138. const byte* m;
  41139. word32 mSz;
  41140. const byte* k;
  41141. word32 kSz;
  41142. const byte* t;
  41143. word32 tSz;
  41144. } CMAC_Test_Case;
  41145. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cmac_test(void)
  41146. {
  41147. #ifdef WOLFSSL_AES_128
  41148. WOLFSSL_SMALL_STACK_STATIC const byte k128[] =
  41149. {
  41150. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  41151. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  41152. };
  41153. #define KLEN_128 (sizeof(k128))
  41154. #endif
  41155. #ifdef WOLFSSL_AES_192
  41156. WOLFSSL_SMALL_STACK_STATIC const byte k192[] =
  41157. {
  41158. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  41159. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  41160. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  41161. };
  41162. #define KLEN_192 (sizeof(k192))
  41163. #endif
  41164. #ifdef WOLFSSL_AES_256
  41165. WOLFSSL_SMALL_STACK_STATIC const byte k256[] =
  41166. {
  41167. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  41168. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  41169. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  41170. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  41171. };
  41172. #define KLEN_256 (sizeof(k256))
  41173. #endif
  41174. WOLFSSL_SMALL_STACK_STATIC const byte m[] =
  41175. {
  41176. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  41177. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  41178. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  41179. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  41180. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  41181. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  41182. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  41183. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  41184. };
  41185. #define MLEN_0 (0)
  41186. #define MLEN_128 (128/8)
  41187. #define MLEN_320 (320/8)
  41188. #define MLEN_319 (MLEN_320 - 1)
  41189. #define MLEN_512 (512/8)
  41190. #ifdef WOLFSSL_AES_128
  41191. WOLFSSL_SMALL_STACK_STATIC const byte t128_0[] =
  41192. {
  41193. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  41194. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  41195. };
  41196. WOLFSSL_SMALL_STACK_STATIC const byte t128_128[] =
  41197. {
  41198. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  41199. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  41200. };
  41201. WOLFSSL_SMALL_STACK_STATIC const byte t128_319[] =
  41202. {
  41203. 0x2c, 0x17, 0x84, 0x4c, 0x93, 0x1c, 0x07, 0x95,
  41204. 0x15, 0x92, 0x73, 0x0a, 0x34, 0xd0, 0xd9, 0xd2
  41205. };
  41206. WOLFSSL_SMALL_STACK_STATIC const byte t128_320[] =
  41207. {
  41208. 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  41209. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27
  41210. };
  41211. WOLFSSL_SMALL_STACK_STATIC const byte t128_512[] =
  41212. {
  41213. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  41214. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  41215. };
  41216. #endif
  41217. #ifdef WOLFSSL_AES_192
  41218. WOLFSSL_SMALL_STACK_STATIC const byte t192_0[] =
  41219. {
  41220. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  41221. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  41222. };
  41223. WOLFSSL_SMALL_STACK_STATIC const byte t192_128[] =
  41224. {
  41225. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  41226. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  41227. };
  41228. WOLFSSL_SMALL_STACK_STATIC const byte t192_320[] =
  41229. {
  41230. 0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad,
  41231. 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e
  41232. };
  41233. WOLFSSL_SMALL_STACK_STATIC const byte t192_512[] =
  41234. {
  41235. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  41236. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  41237. };
  41238. #endif
  41239. #ifdef WOLFSSL_AES_256
  41240. WOLFSSL_SMALL_STACK_STATIC const byte t256_0[] =
  41241. {
  41242. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  41243. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  41244. };
  41245. WOLFSSL_SMALL_STACK_STATIC const byte t256_128[] =
  41246. {
  41247. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  41248. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  41249. };
  41250. WOLFSSL_SMALL_STACK_STATIC const byte t256_320[] =
  41251. {
  41252. 0xaa, 0xf3, 0xd8, 0xf1, 0xde, 0x56, 0x40, 0xc2,
  41253. 0x32, 0xf5, 0xb1, 0x69, 0xb9, 0xc9, 0x11, 0xe6
  41254. };
  41255. WOLFSSL_SMALL_STACK_STATIC const byte t256_512[] =
  41256. {
  41257. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  41258. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  41259. };
  41260. #endif
  41261. const CMAC_Test_Case testCases[] =
  41262. {
  41263. #ifdef WOLFSSL_AES_128
  41264. {WC_CMAC_AES, 0, m, MLEN_0, k128, KLEN_128, t128_0, AES_BLOCK_SIZE},
  41265. {WC_CMAC_AES, 0, m, MLEN_128, k128, KLEN_128, t128_128, AES_BLOCK_SIZE},
  41266. {WC_CMAC_AES, 0, m, MLEN_320, k128, KLEN_128, t128_320, AES_BLOCK_SIZE},
  41267. {WC_CMAC_AES, 0, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  41268. {WC_CMAC_AES, 5, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  41269. #endif
  41270. #ifdef WOLFSSL_AES_192
  41271. {WC_CMAC_AES, 0, m, MLEN_0, k192, KLEN_192, t192_0, AES_BLOCK_SIZE},
  41272. {WC_CMAC_AES, 0, m, MLEN_128, k192, KLEN_192, t192_128, AES_BLOCK_SIZE},
  41273. {WC_CMAC_AES, 0, m, MLEN_320, k192, KLEN_192, t192_320, AES_BLOCK_SIZE},
  41274. {WC_CMAC_AES, 0, m, MLEN_512, k192, KLEN_192, t192_512, AES_BLOCK_SIZE},
  41275. #endif
  41276. #ifdef WOLFSSL_AES_256
  41277. {WC_CMAC_AES, 0, m, MLEN_0, k256, KLEN_256, t256_0, AES_BLOCK_SIZE},
  41278. {WC_CMAC_AES, 0, m, MLEN_128, k256, KLEN_256, t256_128, AES_BLOCK_SIZE},
  41279. {WC_CMAC_AES, 0, m, MLEN_320, k256, KLEN_256, t256_320, AES_BLOCK_SIZE},
  41280. {WC_CMAC_AES, 0, m, MLEN_512, k256, KLEN_256, t256_512, AES_BLOCK_SIZE},
  41281. #endif
  41282. #ifdef WOLFSSL_AES_128
  41283. {WC_CMAC_AES, 0, m, MLEN_319, k128, KLEN_128, t128_319, AES_BLOCK_SIZE}
  41284. #endif
  41285. };
  41286. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  41287. Cmac *cmac;
  41288. #else
  41289. Cmac cmac[1];
  41290. #endif
  41291. byte tag[AES_BLOCK_SIZE];
  41292. const CMAC_Test_Case* tc;
  41293. word32 i, tagSz;
  41294. wc_test_ret_t ret;
  41295. WOLFSSL_ENTER("cmac_test");
  41296. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  41297. if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC)) == NULL)
  41298. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  41299. #endif
  41300. for (i = 0, tc = testCases;
  41301. i < sizeof(testCases)/sizeof(CMAC_Test_Case);
  41302. i++, tc++) {
  41303. XMEMSET(tag, 0, sizeof(tag));
  41304. tagSz = AES_BLOCK_SIZE;
  41305. #if !defined(HAVE_FIPS) || \
  41306. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)
  41307. ret = wc_InitCmac_ex(cmac, tc->k, tc->kSz, tc->type, NULL, HEAP_HINT, devId);
  41308. #else
  41309. ret = wc_InitCmac(cmac, tc->k, tc->kSz, tc->type, NULL);
  41310. #endif
  41311. if (ret != 0)
  41312. {
  41313. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  41314. }
  41315. if (tc->partial) {
  41316. ret = wc_CmacUpdate(cmac, tc->m,
  41317. tc->mSz/2 - tc->partial);
  41318. if (ret != 0)
  41319. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  41320. ret = wc_CmacUpdate(cmac, tc->m + tc->mSz/2 - tc->partial,
  41321. tc->mSz/2 + tc->partial);
  41322. if (ret != 0)
  41323. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  41324. }
  41325. else {
  41326. ret = wc_CmacUpdate(cmac, tc->m, tc->mSz);
  41327. if (ret != 0)
  41328. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  41329. }
  41330. ret = wc_CmacFinal(cmac, tag, &tagSz);
  41331. if (ret != 0)
  41332. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  41333. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  41334. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  41335. XMEMSET(tag, 0, sizeof(tag));
  41336. tagSz = sizeof(tag);
  41337. #if !defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)
  41338. ret = wc_AesCmacGenerate_ex(cmac, tag, &tagSz, tc->m, tc->mSz,
  41339. tc->k, tc->kSz, NULL, devId);
  41340. #else
  41341. ret = wc_AesCmacGenerate(tag, &tagSz, tc->m, tc->mSz,
  41342. tc->k, tc->kSz);
  41343. #endif
  41344. if (ret != 0)
  41345. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  41346. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  41347. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  41348. #if !defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)
  41349. ret = wc_AesCmacVerify_ex(cmac, tc->t, tc->tSz, tc->m, tc->mSz,
  41350. tc->k, tc->kSz, HEAP_HINT, devId);
  41351. #else
  41352. ret = wc_AesCmacVerify(tc->t, tc->tSz, tc->m, tc->mSz,
  41353. tc->k, tc->kSz);
  41354. #endif
  41355. if (ret != 0)
  41356. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  41357. #if !defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)
  41358. /* Test that keyless generate with init is the same */
  41359. XMEMSET(tag, 0, sizeof(tag));
  41360. tagSz = sizeof(tag);
  41361. ret = wc_InitCmac_ex(cmac, tc->k, tc->kSz, tc->type, NULL, HEAP_HINT, devId);
  41362. if (ret != 0) {
  41363. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  41364. }
  41365. ret = wc_AesCmacGenerate_ex(cmac, tag, &tagSz, tc->m, tc->mSz,
  41366. NULL, 0, HEAP_HINT, devId);
  41367. if (ret != 0) {
  41368. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  41369. }
  41370. #endif
  41371. }
  41372. ret = 0;
  41373. out:
  41374. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  41375. if (cmac)
  41376. XFREE(cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC);
  41377. #endif
  41378. return ret;
  41379. }
  41380. #endif /* NO_AES && WOLFSSL_CMAC */
  41381. #if defined(WOLFSSL_SIPHASH)
  41382. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  41383. /* Test vectors from:
  41384. * https://github.com/veorq/SipHash/blob/master/vectors.h
  41385. */
  41386. static const unsigned char siphash_key[SIPHASH_KEY_SIZE] = {
  41387. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  41388. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  41389. };
  41390. static const unsigned char siphash_msg[64] = {
  41391. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  41392. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  41393. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  41394. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  41395. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  41396. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  41397. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  41398. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
  41399. };
  41400. static const unsigned char siphash_r8[64][SIPHASH_MAC_SIZE_8] = {
  41401. { 0x31, 0x0e, 0x0e, 0xdd, 0x47, 0xdb, 0x6f, 0x72, },
  41402. { 0xfd, 0x67, 0xdc, 0x93, 0xc5, 0x39, 0xf8, 0x74, },
  41403. { 0x5a, 0x4f, 0xa9, 0xd9, 0x09, 0x80, 0x6c, 0x0d, },
  41404. { 0x2d, 0x7e, 0xfb, 0xd7, 0x96, 0x66, 0x67, 0x85, },
  41405. { 0xb7, 0x87, 0x71, 0x27, 0xe0, 0x94, 0x27, 0xcf, },
  41406. { 0x8d, 0xa6, 0x99, 0xcd, 0x64, 0x55, 0x76, 0x18, },
  41407. { 0xce, 0xe3, 0xfe, 0x58, 0x6e, 0x46, 0xc9, 0xcb, },
  41408. { 0x37, 0xd1, 0x01, 0x8b, 0xf5, 0x00, 0x02, 0xab, },
  41409. { 0x62, 0x24, 0x93, 0x9a, 0x79, 0xf5, 0xf5, 0x93, },
  41410. { 0xb0, 0xe4, 0xa9, 0x0b, 0xdf, 0x82, 0x00, 0x9e, },
  41411. { 0xf3, 0xb9, 0xdd, 0x94, 0xc5, 0xbb, 0x5d, 0x7a, },
  41412. { 0xa7, 0xad, 0x6b, 0x22, 0x46, 0x2f, 0xb3, 0xf4, },
  41413. { 0xfb, 0xe5, 0x0e, 0x86, 0xbc, 0x8f, 0x1e, 0x75, },
  41414. { 0x90, 0x3d, 0x84, 0xc0, 0x27, 0x56, 0xea, 0x14, },
  41415. { 0xee, 0xf2, 0x7a, 0x8e, 0x90, 0xca, 0x23, 0xf7, },
  41416. { 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1, },
  41417. { 0xdb, 0x9b, 0xc2, 0x57, 0x7f, 0xcc, 0x2a, 0x3f, },
  41418. { 0x94, 0x47, 0xbe, 0x2c, 0xf5, 0xe9, 0x9a, 0x69, },
  41419. { 0x9c, 0xd3, 0x8d, 0x96, 0xf0, 0xb3, 0xc1, 0x4b, },
  41420. { 0xbd, 0x61, 0x79, 0xa7, 0x1d, 0xc9, 0x6d, 0xbb, },
  41421. { 0x98, 0xee, 0xa2, 0x1a, 0xf2, 0x5c, 0xd6, 0xbe, },
  41422. { 0xc7, 0x67, 0x3b, 0x2e, 0xb0, 0xcb, 0xf2, 0xd0, },
  41423. { 0x88, 0x3e, 0xa3, 0xe3, 0x95, 0x67, 0x53, 0x93, },
  41424. { 0xc8, 0xce, 0x5c, 0xcd, 0x8c, 0x03, 0x0c, 0xa8, },
  41425. { 0x94, 0xaf, 0x49, 0xf6, 0xc6, 0x50, 0xad, 0xb8, },
  41426. { 0xea, 0xb8, 0x85, 0x8a, 0xde, 0x92, 0xe1, 0xbc, },
  41427. { 0xf3, 0x15, 0xbb, 0x5b, 0xb8, 0x35, 0xd8, 0x17, },
  41428. { 0xad, 0xcf, 0x6b, 0x07, 0x63, 0x61, 0x2e, 0x2f, },
  41429. { 0xa5, 0xc9, 0x1d, 0xa7, 0xac, 0xaa, 0x4d, 0xde, },
  41430. { 0x71, 0x65, 0x95, 0x87, 0x66, 0x50, 0xa2, 0xa6, },
  41431. { 0x28, 0xef, 0x49, 0x5c, 0x53, 0xa3, 0x87, 0xad, },
  41432. { 0x42, 0xc3, 0x41, 0xd8, 0xfa, 0x92, 0xd8, 0x32, },
  41433. { 0xce, 0x7c, 0xf2, 0x72, 0x2f, 0x51, 0x27, 0x71, },
  41434. { 0xe3, 0x78, 0x59, 0xf9, 0x46, 0x23, 0xf3, 0xa7, },
  41435. { 0x38, 0x12, 0x05, 0xbb, 0x1a, 0xb0, 0xe0, 0x12, },
  41436. { 0xae, 0x97, 0xa1, 0x0f, 0xd4, 0x34, 0xe0, 0x15, },
  41437. { 0xb4, 0xa3, 0x15, 0x08, 0xbe, 0xff, 0x4d, 0x31, },
  41438. { 0x81, 0x39, 0x62, 0x29, 0xf0, 0x90, 0x79, 0x02, },
  41439. { 0x4d, 0x0c, 0xf4, 0x9e, 0xe5, 0xd4, 0xdc, 0xca, },
  41440. { 0x5c, 0x73, 0x33, 0x6a, 0x76, 0xd8, 0xbf, 0x9a, },
  41441. { 0xd0, 0xa7, 0x04, 0x53, 0x6b, 0xa9, 0x3e, 0x0e, },
  41442. { 0x92, 0x59, 0x58, 0xfc, 0xd6, 0x42, 0x0c, 0xad, },
  41443. { 0xa9, 0x15, 0xc2, 0x9b, 0xc8, 0x06, 0x73, 0x18, },
  41444. { 0x95, 0x2b, 0x79, 0xf3, 0xbc, 0x0a, 0xa6, 0xd4, },
  41445. { 0xf2, 0x1d, 0xf2, 0xe4, 0x1d, 0x45, 0x35, 0xf9, },
  41446. { 0x87, 0x57, 0x75, 0x19, 0x04, 0x8f, 0x53, 0xa9, },
  41447. { 0x10, 0xa5, 0x6c, 0xf5, 0xdf, 0xcd, 0x9a, 0xdb, },
  41448. { 0xeb, 0x75, 0x09, 0x5c, 0xcd, 0x98, 0x6c, 0xd0, },
  41449. { 0x51, 0xa9, 0xcb, 0x9e, 0xcb, 0xa3, 0x12, 0xe6, },
  41450. { 0x96, 0xaf, 0xad, 0xfc, 0x2c, 0xe6, 0x66, 0xc7, },
  41451. { 0x72, 0xfe, 0x52, 0x97, 0x5a, 0x43, 0x64, 0xee, },
  41452. { 0x5a, 0x16, 0x45, 0xb2, 0x76, 0xd5, 0x92, 0xa1, },
  41453. { 0xb2, 0x74, 0xcb, 0x8e, 0xbf, 0x87, 0x87, 0x0a, },
  41454. { 0x6f, 0x9b, 0xb4, 0x20, 0x3d, 0xe7, 0xb3, 0x81, },
  41455. { 0xea, 0xec, 0xb2, 0xa3, 0x0b, 0x22, 0xa8, 0x7f, },
  41456. { 0x99, 0x24, 0xa4, 0x3c, 0xc1, 0x31, 0x57, 0x24, },
  41457. { 0xbd, 0x83, 0x8d, 0x3a, 0xaf, 0xbf, 0x8d, 0xb7, },
  41458. { 0x0b, 0x1a, 0x2a, 0x32, 0x65, 0xd5, 0x1a, 0xea, },
  41459. { 0x13, 0x50, 0x79, 0xa3, 0x23, 0x1c, 0xe6, 0x60, },
  41460. { 0x93, 0x2b, 0x28, 0x46, 0xe4, 0xd7, 0x06, 0x66, },
  41461. { 0xe1, 0x91, 0x5f, 0x5c, 0xb1, 0xec, 0xa4, 0x6c, },
  41462. { 0xf3, 0x25, 0x96, 0x5c, 0xa1, 0x6d, 0x62, 0x9f, },
  41463. { 0x57, 0x5f, 0xf2, 0x8e, 0x60, 0x38, 0x1b, 0xe5, },
  41464. { 0x72, 0x45, 0x06, 0xeb, 0x4c, 0x32, 0x8a, 0x95, },
  41465. };
  41466. static const unsigned char siphash_r16[64][SIPHASH_MAC_SIZE_16] = {
  41467. { 0xa3, 0x81, 0x7f, 0x04, 0xba, 0x25, 0xa8, 0xe6,
  41468. 0x6d, 0xf6, 0x72, 0x14, 0xc7, 0x55, 0x02, 0x93, },
  41469. { 0xda, 0x87, 0xc1, 0xd8, 0x6b, 0x99, 0xaf, 0x44,
  41470. 0x34, 0x76, 0x59, 0x11, 0x9b, 0x22, 0xfc, 0x45, },
  41471. { 0x81, 0x77, 0x22, 0x8d, 0xa4, 0xa4, 0x5d, 0xc7,
  41472. 0xfc, 0xa3, 0x8b, 0xde, 0xf6, 0x0a, 0xff, 0xe4, },
  41473. { 0x9c, 0x70, 0xb6, 0x0c, 0x52, 0x67, 0xa9, 0x4e,
  41474. 0x5f, 0x33, 0xb6, 0xb0, 0x29, 0x85, 0xed, 0x51, },
  41475. { 0xf8, 0x81, 0x64, 0xc1, 0x2d, 0x9c, 0x8f, 0xaf,
  41476. 0x7d, 0x0f, 0x6e, 0x7c, 0x7b, 0xcd, 0x55, 0x79, },
  41477. { 0x13, 0x68, 0x87, 0x59, 0x80, 0x77, 0x6f, 0x88,
  41478. 0x54, 0x52, 0x7a, 0x07, 0x69, 0x0e, 0x96, 0x27, },
  41479. { 0x14, 0xee, 0xca, 0x33, 0x8b, 0x20, 0x86, 0x13,
  41480. 0x48, 0x5e, 0xa0, 0x30, 0x8f, 0xd7, 0xa1, 0x5e, },
  41481. { 0xa1, 0xf1, 0xeb, 0xbe, 0xd8, 0xdb, 0xc1, 0x53,
  41482. 0xc0, 0xb8, 0x4a, 0xa6, 0x1f, 0xf0, 0x82, 0x39, },
  41483. { 0x3b, 0x62, 0xa9, 0xba, 0x62, 0x58, 0xf5, 0x61,
  41484. 0x0f, 0x83, 0xe2, 0x64, 0xf3, 0x14, 0x97, 0xb4, },
  41485. { 0x26, 0x44, 0x99, 0x06, 0x0a, 0xd9, 0xba, 0xab,
  41486. 0xc4, 0x7f, 0x8b, 0x02, 0xbb, 0x6d, 0x71, 0xed, },
  41487. { 0x00, 0x11, 0x0d, 0xc3, 0x78, 0x14, 0x69, 0x56,
  41488. 0xc9, 0x54, 0x47, 0xd3, 0xf3, 0xd0, 0xfb, 0xba, },
  41489. { 0x01, 0x51, 0xc5, 0x68, 0x38, 0x6b, 0x66, 0x77,
  41490. 0xa2, 0xb4, 0xdc, 0x6f, 0x81, 0xe5, 0xdc, 0x18, },
  41491. { 0xd6, 0x26, 0xb2, 0x66, 0x90, 0x5e, 0xf3, 0x58,
  41492. 0x82, 0x63, 0x4d, 0xf6, 0x85, 0x32, 0xc1, 0x25, },
  41493. { 0x98, 0x69, 0xe2, 0x47, 0xe9, 0xc0, 0x8b, 0x10,
  41494. 0xd0, 0x29, 0x93, 0x4f, 0xc4, 0xb9, 0x52, 0xf7, },
  41495. { 0x31, 0xfc, 0xef, 0xac, 0x66, 0xd7, 0xde, 0x9c,
  41496. 0x7e, 0xc7, 0x48, 0x5f, 0xe4, 0x49, 0x49, 0x02, },
  41497. { 0x54, 0x93, 0xe9, 0x99, 0x33, 0xb0, 0xa8, 0x11,
  41498. 0x7e, 0x08, 0xec, 0x0f, 0x97, 0xcf, 0xc3, 0xd9, },
  41499. { 0x6e, 0xe2, 0xa4, 0xca, 0x67, 0xb0, 0x54, 0xbb,
  41500. 0xfd, 0x33, 0x15, 0xbf, 0x85, 0x23, 0x05, 0x77, },
  41501. { 0x47, 0x3d, 0x06, 0xe8, 0x73, 0x8d, 0xb8, 0x98,
  41502. 0x54, 0xc0, 0x66, 0xc4, 0x7a, 0xe4, 0x77, 0x40, },
  41503. { 0xa4, 0x26, 0xe5, 0xe4, 0x23, 0xbf, 0x48, 0x85,
  41504. 0x29, 0x4d, 0xa4, 0x81, 0xfe, 0xae, 0xf7, 0x23, },
  41505. { 0x78, 0x01, 0x77, 0x31, 0xcf, 0x65, 0xfa, 0xb0,
  41506. 0x74, 0xd5, 0x20, 0x89, 0x52, 0x51, 0x2e, 0xb1, },
  41507. { 0x9e, 0x25, 0xfc, 0x83, 0x3f, 0x22, 0x90, 0x73,
  41508. 0x3e, 0x93, 0x44, 0xa5, 0xe8, 0x38, 0x39, 0xeb, },
  41509. { 0x56, 0x8e, 0x49, 0x5a, 0xbe, 0x52, 0x5a, 0x21,
  41510. 0x8a, 0x22, 0x14, 0xcd, 0x3e, 0x07, 0x1d, 0x12, },
  41511. { 0x4a, 0x29, 0xb5, 0x45, 0x52, 0xd1, 0x6b, 0x9a,
  41512. 0x46, 0x9c, 0x10, 0x52, 0x8e, 0xff, 0x0a, 0xae, },
  41513. { 0xc9, 0xd1, 0x84, 0xdd, 0xd5, 0xa9, 0xf5, 0xe0,
  41514. 0xcf, 0x8c, 0xe2, 0x9a, 0x9a, 0xbf, 0x69, 0x1c, },
  41515. { 0x2d, 0xb4, 0x79, 0xae, 0x78, 0xbd, 0x50, 0xd8,
  41516. 0x88, 0x2a, 0x8a, 0x17, 0x8a, 0x61, 0x32, 0xad, },
  41517. { 0x8e, 0xce, 0x5f, 0x04, 0x2d, 0x5e, 0x44, 0x7b,
  41518. 0x50, 0x51, 0xb9, 0xea, 0xcb, 0x8d, 0x8f, 0x6f, },
  41519. { 0x9c, 0x0b, 0x53, 0xb4, 0xb3, 0xc3, 0x07, 0xe8,
  41520. 0x7e, 0xae, 0xe0, 0x86, 0x78, 0x14, 0x1f, 0x66, },
  41521. { 0xab, 0xf2, 0x48, 0xaf, 0x69, 0xa6, 0xea, 0xe4,
  41522. 0xbf, 0xd3, 0xeb, 0x2f, 0x12, 0x9e, 0xeb, 0x94, },
  41523. { 0x06, 0x64, 0xda, 0x16, 0x68, 0x57, 0x4b, 0x88,
  41524. 0xb9, 0x35, 0xf3, 0x02, 0x73, 0x58, 0xae, 0xf4, },
  41525. { 0xaa, 0x4b, 0x9d, 0xc4, 0xbf, 0x33, 0x7d, 0xe9,
  41526. 0x0c, 0xd4, 0xfd, 0x3c, 0x46, 0x7c, 0x6a, 0xb7, },
  41527. { 0xea, 0x5c, 0x7f, 0x47, 0x1f, 0xaf, 0x6b, 0xde,
  41528. 0x2b, 0x1a, 0xd7, 0xd4, 0x68, 0x6d, 0x22, 0x87, },
  41529. { 0x29, 0x39, 0xb0, 0x18, 0x32, 0x23, 0xfa, 0xfc,
  41530. 0x17, 0x23, 0xde, 0x4f, 0x52, 0xc4, 0x3d, 0x35, },
  41531. { 0x7c, 0x39, 0x56, 0xca, 0x5e, 0xea, 0xfc, 0x3e,
  41532. 0x36, 0x3e, 0x9d, 0x55, 0x65, 0x46, 0xeb, 0x68, },
  41533. { 0x77, 0xc6, 0x07, 0x71, 0x46, 0xf0, 0x1c, 0x32,
  41534. 0xb6, 0xb6, 0x9d, 0x5f, 0x4e, 0xa9, 0xff, 0xcf, },
  41535. { 0x37, 0xa6, 0x98, 0x6c, 0xb8, 0x84, 0x7e, 0xdf,
  41536. 0x09, 0x25, 0xf0, 0xf1, 0x30, 0x9b, 0x54, 0xde, },
  41537. { 0xa7, 0x05, 0xf0, 0xe6, 0x9d, 0xa9, 0xa8, 0xf9,
  41538. 0x07, 0x24, 0x1a, 0x2e, 0x92, 0x3c, 0x8c, 0xc8, },
  41539. { 0x3d, 0xc4, 0x7d, 0x1f, 0x29, 0xc4, 0x48, 0x46,
  41540. 0x1e, 0x9e, 0x76, 0xed, 0x90, 0x4f, 0x67, 0x11, },
  41541. { 0x0d, 0x62, 0xbf, 0x01, 0xe6, 0xfc, 0x0e, 0x1a,
  41542. 0x0d, 0x3c, 0x47, 0x51, 0xc5, 0xd3, 0x69, 0x2b, },
  41543. { 0x8c, 0x03, 0x46, 0x8b, 0xca, 0x7c, 0x66, 0x9e,
  41544. 0xe4, 0xfd, 0x5e, 0x08, 0x4b, 0xbe, 0xe7, 0xb5, },
  41545. { 0x52, 0x8a, 0x5b, 0xb9, 0x3b, 0xaf, 0x2c, 0x9c,
  41546. 0x44, 0x73, 0xcc, 0xe5, 0xd0, 0xd2, 0x2b, 0xd9, },
  41547. { 0xdf, 0x6a, 0x30, 0x1e, 0x95, 0xc9, 0x5d, 0xad,
  41548. 0x97, 0xae, 0x0c, 0xc8, 0xc6, 0x91, 0x3b, 0xd8, },
  41549. { 0x80, 0x11, 0x89, 0x90, 0x2c, 0x85, 0x7f, 0x39,
  41550. 0xe7, 0x35, 0x91, 0x28, 0x5e, 0x70, 0xb6, 0xdb, },
  41551. { 0xe6, 0x17, 0x34, 0x6a, 0xc9, 0xc2, 0x31, 0xbb,
  41552. 0x36, 0x50, 0xae, 0x34, 0xcc, 0xca, 0x0c, 0x5b, },
  41553. { 0x27, 0xd9, 0x34, 0x37, 0xef, 0xb7, 0x21, 0xaa,
  41554. 0x40, 0x18, 0x21, 0xdc, 0xec, 0x5a, 0xdf, 0x89, },
  41555. { 0x89, 0x23, 0x7d, 0x9d, 0xed, 0x9c, 0x5e, 0x78,
  41556. 0xd8, 0xb1, 0xc9, 0xb1, 0x66, 0xcc, 0x73, 0x42, },
  41557. { 0x4a, 0x6d, 0x80, 0x91, 0xbf, 0x5e, 0x7d, 0x65,
  41558. 0x11, 0x89, 0xfa, 0x94, 0xa2, 0x50, 0xb1, 0x4c, },
  41559. { 0x0e, 0x33, 0xf9, 0x60, 0x55, 0xe7, 0xae, 0x89,
  41560. 0x3f, 0xfc, 0x0e, 0x3d, 0xcf, 0x49, 0x29, 0x02, },
  41561. { 0xe6, 0x1c, 0x43, 0x2b, 0x72, 0x0b, 0x19, 0xd1,
  41562. 0x8e, 0xc8, 0xd8, 0x4b, 0xdc, 0x63, 0x15, 0x1b, },
  41563. { 0xf7, 0xe5, 0xae, 0xf5, 0x49, 0xf7, 0x82, 0xcf,
  41564. 0x37, 0x90, 0x55, 0xa6, 0x08, 0x26, 0x9b, 0x16, },
  41565. { 0x43, 0x8d, 0x03, 0x0f, 0xd0, 0xb7, 0xa5, 0x4f,
  41566. 0xa8, 0x37, 0xf2, 0xad, 0x20, 0x1a, 0x64, 0x03, },
  41567. { 0xa5, 0x90, 0xd3, 0xee, 0x4f, 0xbf, 0x04, 0xe3,
  41568. 0x24, 0x7e, 0x0d, 0x27, 0xf2, 0x86, 0x42, 0x3f, },
  41569. { 0x5f, 0xe2, 0xc1, 0xa1, 0x72, 0xfe, 0x93, 0xc4,
  41570. 0xb1, 0x5c, 0xd3, 0x7c, 0xae, 0xf9, 0xf5, 0x38, },
  41571. { 0x2c, 0x97, 0x32, 0x5c, 0xbd, 0x06, 0xb3, 0x6e,
  41572. 0xb2, 0x13, 0x3d, 0xd0, 0x8b, 0x3a, 0x01, 0x7c, },
  41573. { 0x92, 0xc8, 0x14, 0x22, 0x7a, 0x6b, 0xca, 0x94,
  41574. 0x9f, 0xf0, 0x65, 0x9f, 0x00, 0x2a, 0xd3, 0x9e, },
  41575. { 0xdc, 0xe8, 0x50, 0x11, 0x0b, 0xd8, 0x32, 0x8c,
  41576. 0xfb, 0xd5, 0x08, 0x41, 0xd6, 0x91, 0x1d, 0x87, },
  41577. { 0x67, 0xf1, 0x49, 0x84, 0xc7, 0xda, 0x79, 0x12,
  41578. 0x48, 0xe3, 0x2b, 0xb5, 0x92, 0x25, 0x83, 0xda, },
  41579. { 0x19, 0x38, 0xf2, 0xcf, 0x72, 0xd5, 0x4e, 0xe9,
  41580. 0x7e, 0x94, 0x16, 0x6f, 0xa9, 0x1d, 0x2a, 0x36, },
  41581. { 0x74, 0x48, 0x1e, 0x96, 0x46, 0xed, 0x49, 0xfe,
  41582. 0x0f, 0x62, 0x24, 0x30, 0x16, 0x04, 0x69, 0x8e, },
  41583. { 0x57, 0xfc, 0xa5, 0xde, 0x98, 0xa9, 0xd6, 0xd8,
  41584. 0x00, 0x64, 0x38, 0xd0, 0x58, 0x3d, 0x8a, 0x1d, },
  41585. { 0x9f, 0xec, 0xde, 0x1c, 0xef, 0xdc, 0x1c, 0xbe,
  41586. 0xd4, 0x76, 0x36, 0x74, 0xd9, 0x57, 0x53, 0x59, },
  41587. { 0xe3, 0x04, 0x0c, 0x00, 0xeb, 0x28, 0xf1, 0x53,
  41588. 0x66, 0xca, 0x73, 0xcb, 0xd8, 0x72, 0xe7, 0x40, },
  41589. { 0x76, 0x97, 0x00, 0x9a, 0x6a, 0x83, 0x1d, 0xfe,
  41590. 0xcc, 0xa9, 0x1c, 0x59, 0x93, 0x67, 0x0f, 0x7a, },
  41591. { 0x58, 0x53, 0x54, 0x23, 0x21, 0xf5, 0x67, 0xa0,
  41592. 0x05, 0xd5, 0x47, 0xa4, 0xf0, 0x47, 0x59, 0xbd, },
  41593. { 0x51, 0x50, 0xd1, 0x77, 0x2f, 0x50, 0x83, 0x4a,
  41594. 0x50, 0x3e, 0x06, 0x9a, 0x97, 0x3f, 0xbd, 0x7c, },
  41595. };
  41596. #endif
  41597. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t siphash_test(void)
  41598. {
  41599. wc_test_ret_t ret = 0;
  41600. int i;
  41601. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  41602. unsigned char res[SIPHASH_MAC_SIZE_16];
  41603. unsigned char tmp[SIPHASH_MAC_SIZE_8];
  41604. SipHash siphash;
  41605. WOLFSSL_ENTER("siphash_test (1)");
  41606. for (i = 0; i < 64; i++) {
  41607. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  41608. if (ret != 0)
  41609. return WC_TEST_RET_ENC_I(i);
  41610. ret = wc_SipHashUpdate(&siphash, siphash_msg, (word32)i);
  41611. if (ret != 0)
  41612. return WC_TEST_RET_ENC_I(i);
  41613. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  41614. if (ret != 0)
  41615. return WC_TEST_RET_ENC_I(i);
  41616. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  41617. return WC_TEST_RET_ENC_I(i);
  41618. ret = wc_SipHash(siphash_key, siphash_msg, (word32)i, res, SIPHASH_MAC_SIZE_8);
  41619. if (ret != 0)
  41620. return WC_TEST_RET_ENC_I(i);
  41621. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  41622. return WC_TEST_RET_ENC_I(i);
  41623. }
  41624. for (i = 0; i < 64; i++) {
  41625. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_16);
  41626. if (ret != 0)
  41627. return WC_TEST_RET_ENC_I(i);
  41628. ret = wc_SipHashUpdate(&siphash, siphash_msg, (word32)i);
  41629. if (ret != 0)
  41630. return WC_TEST_RET_ENC_I(i);
  41631. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  41632. if (ret != 0)
  41633. return WC_TEST_RET_ENC_I(i);
  41634. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  41635. return WC_TEST_RET_ENC_I(i);
  41636. ret = wc_SipHash(siphash_key, siphash_msg, (word32)i, res, SIPHASH_MAC_SIZE_16);
  41637. if (ret != 0)
  41638. return WC_TEST_RET_ENC_I(i);
  41639. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  41640. return WC_TEST_RET_ENC_I(i);
  41641. }
  41642. #else
  41643. WOLFSSL_ENTER("siphash_test (1)");
  41644. #endif
  41645. /* Testing bad parameters. */
  41646. ret = wc_InitSipHash(NULL, NULL, SIPHASH_MAC_SIZE_8);
  41647. if (ret != BAD_FUNC_ARG)
  41648. return WC_TEST_RET_ENC_EC(ret);
  41649. ret = wc_InitSipHash(NULL, siphash_key, SIPHASH_MAC_SIZE_8);
  41650. if (ret != BAD_FUNC_ARG)
  41651. return WC_TEST_RET_ENC_EC(ret);
  41652. ret = wc_InitSipHash(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  41653. if (ret != BAD_FUNC_ARG)
  41654. return WC_TEST_RET_ENC_EC(ret);
  41655. ret = wc_InitSipHash(&siphash, siphash_key, 7);
  41656. if (ret != BAD_FUNC_ARG)
  41657. return WC_TEST_RET_ENC_EC(ret);
  41658. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  41659. if (ret != 0)
  41660. return WC_TEST_RET_ENC_EC(ret);
  41661. ret = wc_SipHashUpdate(NULL, NULL, 0);
  41662. if (ret != BAD_FUNC_ARG)
  41663. return WC_TEST_RET_ENC_EC(ret);
  41664. ret = wc_SipHashUpdate(&siphash, NULL, 1);
  41665. if (ret != BAD_FUNC_ARG)
  41666. return WC_TEST_RET_ENC_EC(ret);
  41667. ret = wc_SipHashFinal(NULL, NULL, SIPHASH_MAC_SIZE_8);
  41668. if (ret != BAD_FUNC_ARG)
  41669. return WC_TEST_RET_ENC_EC(ret);
  41670. ret = wc_SipHashFinal(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  41671. if (ret != BAD_FUNC_ARG)
  41672. return WC_TEST_RET_ENC_EC(ret);
  41673. ret = wc_SipHashFinal(NULL, res, SIPHASH_MAC_SIZE_8);
  41674. if (ret != BAD_FUNC_ARG)
  41675. return WC_TEST_RET_ENC_EC(ret);
  41676. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  41677. if (ret != BAD_FUNC_ARG)
  41678. return WC_TEST_RET_ENC_EC(ret);
  41679. ret = wc_SipHash(NULL, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  41680. if (ret != BAD_FUNC_ARG)
  41681. return WC_TEST_RET_ENC_EC(ret);
  41682. ret = wc_SipHash(siphash_key, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  41683. if (ret != BAD_FUNC_ARG)
  41684. return WC_TEST_RET_ENC_EC(ret);
  41685. ret = wc_SipHash(NULL, NULL, 0, res, SIPHASH_MAC_SIZE_16);
  41686. if (ret != BAD_FUNC_ARG)
  41687. return WC_TEST_RET_ENC_EC(ret);
  41688. ret = wc_SipHash(siphash_key, NULL, 0, res, 15);
  41689. if (ret != BAD_FUNC_ARG)
  41690. return WC_TEST_RET_ENC_EC(ret);
  41691. ret = wc_SipHash(siphash_key, NULL, 1, res, SIPHASH_MAC_SIZE_16);
  41692. if (ret != BAD_FUNC_ARG)
  41693. return WC_TEST_RET_ENC_EC(ret);
  41694. /* Test cache with multiple non blocksize bytes */
  41695. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  41696. if (ret != 0)
  41697. return WC_TEST_RET_ENC_EC(ret);
  41698. ret = wc_SipHashUpdate(&siphash, siphash_msg, 5);
  41699. if (ret != 0)
  41700. return WC_TEST_RET_ENC_EC(ret);
  41701. ret = wc_SipHashUpdate(&siphash, siphash_msg + 5, 4);
  41702. if (ret != 0)
  41703. return WC_TEST_RET_ENC_EC(ret);
  41704. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  41705. if (ret != 0)
  41706. return WC_TEST_RET_ENC_EC(ret);
  41707. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  41708. if (ret != 0)
  41709. return WC_TEST_RET_ENC_EC(ret);
  41710. ret = wc_SipHashUpdate(&siphash, siphash_msg, 9);
  41711. if (ret != 0)
  41712. return WC_TEST_RET_ENC_EC(ret);
  41713. ret = wc_SipHashFinal(&siphash, tmp, SIPHASH_MAC_SIZE_8);
  41714. if (ret != 0)
  41715. return WC_TEST_RET_ENC_EC(ret);
  41716. if (XMEMCMP(res, tmp, SIPHASH_MAC_SIZE_8) != 0)
  41717. return WC_TEST_RET_ENC_NC;
  41718. return 0;
  41719. }
  41720. #endif /* WOLFSSL_SIPHASH */
  41721. #ifdef HAVE_LIBZ
  41722. static const byte sample_text[] =
  41723. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  41724. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  41725. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  41726. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  41727. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  41728. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  41729. "\n"
  41730. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  41731. "four loko you probably haven't heard of them high life. Messenger bag\n"
  41732. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  41733. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  41734. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  41735. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  41736. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  41737. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  41738. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  41739. "four loko whatever street art yr farm-to-table.\n"
  41740. "\n"
  41741. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  41742. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  41743. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  41744. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  41745. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  41746. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  41747. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  41748. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  41749. "locavore.\n"
  41750. "\n"
  41751. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  41752. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  41753. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  41754. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  41755. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  41756. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  41757. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  41758. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  41759. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  41760. "neutra PBR selvage.\n"
  41761. "\n"
  41762. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  41763. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  41764. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  41765. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  41766. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  41767. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  41768. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  41769. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  41770. "mollit magna, sriracha sartorial helvetica.\n"
  41771. "\n"
  41772. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  41773. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  41774. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  41775. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  41776. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  41777. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  41778. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  41779. "\n"
  41780. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  41781. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  41782. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  41783. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  41784. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  41785. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  41786. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  41787. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  41788. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  41789. "\n"
  41790. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  41791. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  41792. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  41793. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  41794. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  41795. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  41796. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  41797. "bag dolor terry richardson sapiente.\n";
  41798. static const byte sample_text_gz[] = {
  41799. 0x1F, 0x8B, 0x08, 0x08, 0xC5, 0x49, 0xB5, 0x5B, 0x00, 0x03, 0x63, 0x69, 0x70,
  41800. 0x68, 0x65, 0x72, 0x74, 0x65, 0x78, 0x74, 0x2E, 0x74, 0x78, 0x74, 0x00, 0x8D,
  41801. 0x58, 0xCB, 0x92, 0xE4, 0xB6, 0x11, 0xBC, 0xE3, 0x2B, 0xEA, 0xA6, 0x83, 0xD9,
  41802. 0x1D, 0x72, 0xF8, 0x22, 0x1F, 0xB5, 0x96, 0xA5, 0xDD, 0x90, 0xBC, 0xAB, 0xD0,
  41803. 0x28, 0x36, 0x42, 0x47, 0x90, 0x2C, 0x36, 0xA1, 0x06, 0x09, 0x0A, 0x8F, 0xEE,
  41804. 0xE1, 0xDF, 0x3B, 0x0B, 0xE0, 0x73, 0x2C, 0x4B, 0xBA, 0xCD, 0xCE, 0x80, 0x78,
  41805. 0x64, 0x65, 0x65, 0x66, 0xED, 0x3B, 0xE3, 0x5A, 0xC3, 0x81, 0x2D, 0x35, 0x69,
  41806. 0x32, 0xAD, 0x8E, 0x3A, 0xD2, 0xA0, 0x7D, 0xA7, 0x2B, 0x6A, 0xAC, 0x69, 0x7A,
  41807. 0x26, 0x9D, 0x22, 0xD3, 0x94, 0x22, 0x69, 0xAA, 0x8D, 0x6F, 0xC9, 0x8D, 0x64,
  41808. 0x22, 0x99, 0xB1, 0x31, 0xAD, 0x69, 0xD3, 0x18, 0x89, 0xAD, 0x89, 0x6A, 0x72,
  41809. 0x56, 0x7B, 0x67, 0xDA, 0x2B, 0xBD, 0xC8, 0xEF, 0xB0, 0x4D, 0x74, 0x8E, 0x5B,
  41810. 0xAA, 0x39, 0x4C, 0xEE, 0xCE, 0xE4, 0x79, 0xF2, 0xDC, 0xF3, 0xD8, 0xB2, 0x37,
  41811. 0x11, 0x8B, 0x8C, 0x2C, 0x7A, 0x32, 0x93, 0xF3, 0x37, 0x3D, 0x9A, 0x86, 0x4C,
  41812. 0xAB, 0xF2, 0xB9, 0x57, 0xFA, 0x97, 0x1B, 0x06, 0xD7, 0x3A, 0x7A, 0xF0, 0x68,
  41813. 0xF4, 0x40, 0xBA, 0x25, 0x0E, 0x81, 0xE9, 0xA6, 0x43, 0xF4, 0x6E, 0x4A, 0xF5,
  41814. 0x95, 0xFE, 0x41, 0x4F, 0x67, 0x3B, 0x1A, 0x1C, 0xEE, 0x12, 0xB4, 0x8F, 0xCE,
  41815. 0x1B, 0x6D, 0xB1, 0xDE, 0xBB, 0x4A, 0x4D, 0x56, 0x9B, 0x96, 0x5A, 0xB6, 0xDC,
  41816. 0xC4, 0x14, 0x70, 0xE5, 0xF5, 0x7D, 0xE1, 0xB7, 0x84, 0x3F, 0xFC, 0xED, 0xEF,
  41817. 0xF4, 0x30, 0x0D, 0x5F, 0xE9, 0x47, 0x17, 0xE2, 0xC5, 0x78, 0x27, 0x67, 0xDF,
  41818. 0xB9, 0xEB, 0xCC, 0xCC, 0x3D, 0x59, 0xBE, 0xDD, 0xCC, 0x78, 0x0B, 0x0A, 0x1F,
  41819. 0x74, 0xF8, 0x8C, 0x1A, 0xAF, 0x67, 0xEA, 0xF4, 0x44, 0xBD, 0x93, 0x7D, 0x2A,
  41820. 0xEA, 0x9C, 0xD7, 0x37, 0x80, 0x32, 0x9A, 0x01, 0x37, 0xD5, 0xDE, 0xCA, 0xA2,
  41821. 0x0D, 0xB9, 0xD0, 0x3B, 0xCF, 0xAD, 0x89, 0x4D, 0x5F, 0xD1, 0xE7, 0xF7, 0x2F,
  41822. 0x2A, 0x0C, 0xDA, 0x5A, 0xAA, 0x35, 0x7E, 0x41, 0xC3, 0xB2, 0x37, 0xDD, 0xDD,
  41823. 0xCD, 0x50, 0xEB, 0x2C, 0x96, 0x62, 0x3B, 0xD7, 0x52, 0xF4, 0xA9, 0xB9, 0x6F,
  41824. 0x48, 0xED, 0xEF, 0x54, 0xEA, 0x67, 0xF6, 0x7E, 0x26, 0x8F, 0x3A, 0x68, 0xDF,
  41825. 0x06, 0xBC, 0x56, 0xB7, 0x66, 0x32, 0xC1, 0x34, 0xD8, 0x88, 0x34, 0x1E, 0x88,
  41826. 0xED, 0x67, 0x8A, 0xF3, 0xC4, 0x4F, 0xC0, 0xCA, 0x9E, 0x62, 0x1A, 0x6A, 0xEB,
  41827. 0xAB, 0x02, 0xED, 0xB3, 0xD7, 0x91, 0x81, 0x8A, 0xEA, 0x5C, 0xF2, 0x64, 0xDD,
  41828. 0xDD, 0xD1, 0xEC, 0x12, 0x4D, 0xDE, 0xD5, 0xBA, 0xC6, 0x77, 0xBD, 0x06, 0xC4,
  41829. 0x5F, 0x44, 0xEA, 0x59, 0x4B, 0x5D, 0x3B, 0x8A, 0x3D, 0x0F, 0xD4, 0x9B, 0x1B,
  41830. 0x80, 0x30, 0x1D, 0x30, 0xFA, 0x8F, 0x00, 0x3F, 0xDE, 0xB0, 0x6F, 0xAD, 0x6F,
  41831. 0x6A, 0xDD, 0x6E, 0x2F, 0x6E, 0xCB, 0x3C, 0xD1, 0x83, 0x06, 0x7B, 0x0F, 0xFD,
  41832. 0xFD, 0x4A, 0xEF, 0xBC, 0x73, 0x77, 0x3B, 0x8F, 0x34, 0xA1, 0xBA, 0xEC, 0x39,
  41833. 0x80, 0x33, 0x21, 0xA4, 0x01, 0x55, 0xD7, 0xD4, 0xF4, 0xC6, 0xDA, 0x27, 0x4E,
  41834. 0x54, 0x1C, 0x2B, 0xEC, 0x37, 0xDE, 0xC3, 0x4C, 0xC9, 0x5A, 0x3D, 0x34, 0x0E,
  41835. 0xD8, 0x1C, 0x0E, 0xA2, 0x34, 0xE8, 0xC1, 0xD0, 0xA4, 0x51, 0xD5, 0x88, 0x8B,
  41836. 0xB7, 0xC6, 0xA3, 0x96, 0x40, 0x49, 0xB7, 0xBC, 0xE0, 0x7F, 0x55, 0x3F, 0xEF,
  41837. 0x6F, 0x6E, 0x92, 0x9D, 0x34, 0xFE, 0x3C, 0x5F, 0x04, 0xA5, 0x6A, 0xFF, 0x30,
  41838. 0x08, 0xC9, 0xEA, 0xF5, 0x52, 0x2B, 0xFE, 0x57, 0xFA, 0x8E, 0xC7, 0xE8, 0x4D,
  41839. 0x37, 0xAB, 0x03, 0xFA, 0x23, 0xBF, 0x46, 0x94, 0xFF, 0xC1, 0x16, 0xE0, 0xB9,
  41840. 0x14, 0x2C, 0x9E, 0x27, 0xEC, 0x98, 0x69, 0x14, 0x92, 0xF1, 0x60, 0x5C, 0x34,
  41841. 0x4D, 0xA0, 0x1F, 0xDF, 0xFD, 0x44, 0x1C, 0x7B, 0xD3, 0x80, 0x70, 0x42, 0x02,
  41842. 0x30, 0x84, 0x5B, 0xE5, 0x59, 0xB7, 0xF3, 0x80, 0xFB, 0x01, 0x33, 0xA9, 0x00,
  41843. 0x37, 0x52, 0xDC, 0xDA, 0xA7, 0x11, 0x85, 0xB7, 0x6E, 0x70, 0xE4, 0xDA, 0x96,
  41844. 0xBA, 0x84, 0x5B, 0x81, 0x43, 0x93, 0xF3, 0xD1, 0xEA, 0xB1, 0xDD, 0xB8, 0x1F,
  41845. 0xA5, 0xCC, 0xEA, 0x50, 0x66, 0x69, 0xA9, 0x8D, 0x8C, 0xA7, 0xA2, 0xF3, 0x38,
  41846. 0x26, 0x43, 0x5E, 0x3F, 0x01, 0xBE, 0x1C, 0x0F, 0x20, 0x7F, 0x75, 0xA8, 0x20,
  41847. 0x80, 0xC4, 0xC3, 0x5C, 0x8B, 0x0D, 0xD4, 0x60, 0x5E, 0xA3, 0x9E, 0xD0, 0xB4,
  41848. 0x4B, 0x4F, 0xE6, 0x13, 0x85, 0x60, 0x42, 0x96, 0xED, 0xAA, 0xDB, 0xE9, 0x99,
  41849. 0xE3, 0x07, 0x0E, 0x61, 0xB3, 0x07, 0xE3, 0xB1, 0xFA, 0xC0, 0x9B, 0xAD, 0xF6,
  41850. 0xE0, 0x26, 0x33, 0xEA, 0xEA, 0x23, 0xCD, 0x1E, 0x9D, 0xE1, 0x87, 0x4B, 0x74,
  41851. 0x97, 0x08, 0x3E, 0xA1, 0x28, 0xEA, 0xB3, 0x19, 0x67, 0x8B, 0x76, 0x9A, 0xA3,
  41852. 0xF6, 0xB9, 0xCF, 0x80, 0x65, 0x97, 0xAE, 0xF4, 0x83, 0x6B, 0xF4, 0x43, 0x20,
  41853. 0xF9, 0x0B, 0xFC, 0x9B, 0xD2, 0x4D, 0x4D, 0xA6, 0xB9, 0xA3, 0x02, 0x55, 0x79,
  41854. 0x18, 0x36, 0x19, 0x5F, 0xC9, 0xEA, 0x5A, 0x76, 0x40, 0xB9, 0xBA, 0x0E, 0x9A,
  41855. 0x44, 0xDF, 0x7C, 0xF8, 0x65, 0x61, 0x5E, 0x81, 0xAB, 0x71, 0xA1, 0x9E, 0x29,
  41856. 0x3C, 0x59, 0xCB, 0x23, 0xA4, 0xF6, 0x60, 0x1A, 0x0D, 0x5B, 0x39, 0xAE, 0xF4,
  41857. 0x6F, 0x59, 0x16, 0x9E, 0x60, 0xD8, 0x56, 0xCF, 0xEA, 0x2C, 0x4C, 0x79, 0xD3,
  41858. 0x5D, 0x51, 0x46, 0xA0, 0x4E, 0xE9, 0xD6, 0xAB, 0x91, 0x43, 0x63, 0x44, 0xD7,
  41859. 0x70, 0xB9, 0x23, 0x98, 0x4F, 0x3D, 0x03, 0x02, 0xF6, 0x81, 0x56, 0xC1, 0x58,
  41860. 0x85, 0x07, 0xA7, 0x2D, 0x2C, 0x29, 0xCA, 0x01, 0x45, 0x31, 0x51, 0x8F, 0xD4,
  41861. 0x19, 0xA1, 0x79, 0x88, 0x5A, 0xA4, 0xF5, 0xAE, 0x2D, 0x4B, 0x63, 0x4C, 0x58,
  41862. 0xFE, 0xBF, 0xAD, 0xEE, 0xA3, 0x09, 0xF8, 0xE2, 0x89, 0xBE, 0x81, 0x0E, 0x86,
  41863. 0x3A, 0xF9, 0x5B, 0xA5, 0xD8, 0xA4, 0x00, 0x75, 0x04, 0xF2, 0x23, 0xB8, 0x39,
  41864. 0x69, 0x50, 0xB7, 0xD0, 0x34, 0x63, 0x54, 0xD8, 0x61, 0xDD, 0xA5, 0x33, 0x47,
  41865. 0x85, 0x96, 0x22, 0xD0, 0x2F, 0x9F, 0x7E, 0xF8, 0x74, 0x24, 0xEA, 0x57, 0x97,
  41866. 0x5A, 0xE0, 0x00, 0xCF, 0xC1, 0x67, 0xE1, 0x41, 0xBD, 0x94, 0xA1, 0x03, 0xD3,
  41867. 0xB4, 0x08, 0x64, 0xF2, 0x17, 0x27, 0x35, 0x37, 0x53, 0xEF, 0x46, 0xCE, 0xD8,
  41868. 0xD4, 0x09, 0x52, 0xC6, 0x1E, 0xF7, 0x28, 0xDF, 0x08, 0x0F, 0xD0, 0x6F, 0x71,
  41869. 0xA6, 0xDF, 0xE4, 0x60, 0x8E, 0xC0, 0x1E, 0x78, 0x86, 0x50, 0xB0, 0x9B, 0x84,
  41870. 0x7E, 0xE8, 0x36, 0xFA, 0x95, 0xF1, 0x12, 0x51, 0xC7, 0x18, 0x96, 0xA2, 0x29,
  41871. 0xBB, 0x70, 0x02, 0xB4, 0xF9, 0xA8, 0x3D, 0x08, 0x66, 0xA9, 0xB3, 0xFC, 0x0A,
  41872. 0x94, 0x80, 0xFD, 0x78, 0xDC, 0xAB, 0x82, 0x5A, 0xD2, 0xCD, 0xC2, 0x87, 0xC6,
  41873. 0x4B, 0x07, 0xFA, 0xD1, 0xC3, 0xD9, 0x34, 0x41, 0x85, 0xF8, 0xD0, 0xB6, 0x0A,
  41874. 0x9D, 0x00, 0x91, 0x35, 0x05, 0x88, 0xC3, 0xE3, 0x9B, 0x22, 0xD2, 0xB8, 0xFD,
  41875. 0x95, 0x3E, 0x6D, 0x5D, 0x48, 0xA3, 0x68, 0xCF, 0x02, 0x42, 0x79, 0x79, 0x8A,
  41876. 0xAA, 0x01, 0xD6, 0x09, 0x14, 0x2C, 0xF4, 0x83, 0xA3, 0x80, 0x31, 0x55, 0x46,
  41877. 0x6E, 0xC5, 0xE5, 0x2F, 0x30, 0x58, 0x81, 0xA2, 0x90, 0xBE, 0x2E, 0xA1, 0xC3,
  41878. 0x0F, 0xA6, 0xF5, 0x51, 0x00, 0x39, 0xB6, 0xF2, 0x2A, 0xA3, 0x15, 0x7D, 0x8D,
  41879. 0xF5, 0x66, 0x5C, 0xD9, 0xFC, 0xCF, 0x2F, 0xBF, 0x08, 0x27, 0xE7, 0xD0, 0x03,
  41880. 0xB8, 0xD9, 0x00, 0x13, 0x3D, 0x01, 0x6B, 0xB6, 0xA8, 0xCD, 0x5B, 0x3B, 0x3E,
  41881. 0x93, 0xBF, 0xE6, 0x2E, 0xB7, 0x4A, 0xCF, 0xB3, 0x0A, 0xCE, 0x62, 0x11, 0xD6,
  41882. 0x1F, 0x68, 0x9B, 0x1D, 0x68, 0xD1, 0x8C, 0x97, 0xBD, 0xA1, 0x07, 0x67, 0x73,
  41883. 0x87, 0xE0, 0x36, 0xDA, 0x8C, 0xD2, 0xD2, 0xBB, 0x84, 0x28, 0xA9, 0xFE, 0x52,
  41884. 0x74, 0xD6, 0xB9, 0x0F, 0x0A, 0x6A, 0x2D, 0x28, 0x35, 0x34, 0x3A, 0xD3, 0xE2,
  41885. 0xCD, 0x35, 0x06, 0x7D, 0x1B, 0x35, 0x85, 0x86, 0xD1, 0x3E, 0xF2, 0x6F, 0xA1,
  41886. 0xC4, 0x55, 0xBD, 0x00, 0xD8, 0xC3, 0x5D, 0xC2, 0x1D, 0x6B, 0x6B, 0x27, 0x5B,
  41887. 0x95, 0xF3, 0xAB, 0xB5, 0xD3, 0x37, 0xF2, 0x2C, 0x9C, 0xC7, 0x5D, 0xBD, 0xF1,
  41888. 0x68, 0x1C, 0xAD, 0xF8, 0xB5, 0xE1, 0x29, 0x72, 0x7A, 0x73, 0x62, 0x55, 0x24,
  41889. 0xB9, 0x85, 0xDF, 0x7B, 0x29, 0x7D, 0xDE, 0x08, 0xF5, 0xE4, 0x44, 0xDA, 0x1A,
  41890. 0x30, 0x74, 0xDA, 0xB4, 0x9B, 0x23, 0x9A, 0x3A, 0xC1, 0x53, 0xB2, 0xA2, 0xA3,
  41891. 0x7B, 0x1F, 0xD9, 0x56, 0xD4, 0x4F, 0x9B, 0xB2, 0x1E, 0xEE, 0xB8, 0x6A, 0x4E,
  41892. 0xB5, 0xF4, 0x5A, 0xC9, 0x18, 0x27, 0x9C, 0xDE, 0x14, 0x44, 0xED, 0xC4, 0x3C,
  41893. 0x71, 0x9F, 0x5F, 0xD9, 0x37, 0xA0, 0x78, 0x34, 0x6E, 0xBC, 0xD2, 0x7B, 0x1D,
  41894. 0xFA, 0x08, 0x39, 0x5A, 0x04, 0x73, 0x15, 0xD9, 0x0A, 0x48, 0xC1, 0x2D, 0x15,
  41895. 0x4E, 0x84, 0x30, 0x45, 0x69, 0xB3, 0xE5, 0xF6, 0xAD, 0x09, 0x1E, 0xCC, 0x5F,
  41896. 0x1F, 0x06, 0xD5, 0x58, 0xAD, 0x78, 0xD7, 0x9F, 0xE5, 0xED, 0x3B, 0x09, 0xD5,
  41897. 0xA6, 0x52, 0x6F, 0x92, 0xD3, 0x3C, 0xC6, 0x1E, 0xF2, 0x93, 0x7C, 0xD3, 0x5F,
  41898. 0x70, 0x85, 0x5D, 0xF8, 0xAA, 0x9D, 0xB7, 0x7B, 0x24, 0x5A, 0xE9, 0x0A, 0x35,
  41899. 0x2F, 0xF5, 0xD9, 0x82, 0x02, 0x8A, 0x90, 0x13, 0x5B, 0xB5, 0x67, 0x9C, 0xDD,
  41900. 0xA0, 0x4E, 0x82, 0x27, 0xDA, 0x7E, 0xE8, 0x8E, 0xCD, 0xE1, 0x56, 0x71, 0x2C,
  41901. 0xE6, 0x4E, 0x1F, 0x91, 0xCD, 0x7C, 0x6A, 0xB7, 0x78, 0xD0, 0x26, 0xF3, 0x56,
  41902. 0xA9, 0xD5, 0xA1, 0xC3, 0x3B, 0x98, 0xE9, 0x28, 0x09, 0xEF, 0x50, 0x90, 0xCD,
  41903. 0xC4, 0x8E, 0x75, 0xCC, 0xAC, 0x2D, 0xC9, 0x03, 0x6D, 0xAC, 0xFE, 0xC4, 0x88,
  41904. 0x36, 0xD1, 0x3F, 0xBB, 0x1C, 0x7D, 0xB3, 0x14, 0x61, 0x2C, 0xB7, 0x54, 0x4B,
  41905. 0xDB, 0x64, 0xB6, 0x57, 0x14, 0x16, 0x8E, 0x1E, 0x6C, 0x64, 0xBB, 0x8B, 0x48,
  41906. 0x5D, 0x96, 0x9D, 0xDC, 0x80, 0xA7, 0xF7, 0x54, 0xC7, 0x46, 0x38, 0x3E, 0x44,
  41907. 0xDE, 0x7E, 0x92, 0x8D, 0x07, 0xF6, 0x07, 0x37, 0x4E, 0x16, 0x10, 0xB4, 0x7D,
  41908. 0x88, 0x66, 0x7F, 0xBB, 0xFF, 0xEA, 0x00, 0xF3, 0xFF, 0x97, 0x2C, 0xB5, 0xBE,
  41909. 0x35, 0x4B, 0x5C, 0x36, 0xEC, 0x4C, 0xBD, 0x2B, 0x7D, 0xBF, 0x46, 0xE2, 0x9C,
  41910. 0x0E, 0x8A, 0xA3, 0xEC, 0xB1, 0x0E, 0x9A, 0xDA, 0x9A, 0x9B, 0x28, 0x92, 0x10,
  41911. 0x53, 0x57, 0xEA, 0xEC, 0xA2, 0x32, 0x32, 0x20, 0x1D, 0x97, 0x5C, 0xB6, 0x84,
  41912. 0xA9, 0x93, 0x8D, 0x95, 0x11, 0xA3, 0x24, 0xA3, 0x2D, 0xC6, 0x4A, 0xEF, 0xAA,
  41913. 0x1D, 0x85, 0x2B, 0x7D, 0x28, 0xBE, 0x53, 0xCE, 0x10, 0x1F, 0xAE, 0x0E, 0x41,
  41914. 0x6C, 0x4B, 0x79, 0x12, 0xFB, 0xF7, 0x54, 0xA3, 0x96, 0x54, 0x83, 0x20, 0x96,
  41915. 0x8F, 0x28, 0xA9, 0x3F, 0x8B, 0x3D, 0xBA, 0x77, 0xDC, 0x24, 0xE1, 0xD4, 0x49,
  41916. 0x40, 0xD8, 0x78, 0x31, 0x85, 0x43, 0xF6, 0xFE, 0x5C, 0xA6, 0x8F, 0x90, 0x09,
  41917. 0xB0, 0xE7, 0xC4, 0x95, 0xB2, 0x55, 0x49, 0x97, 0x8F, 0x1C, 0x78, 0x30, 0x20,
  41918. 0xA0, 0xB4, 0xEF, 0x73, 0x56, 0x59, 0x82, 0xFD, 0xCE, 0xBA, 0x6A, 0x8F, 0x2C,
  41919. 0x8B, 0x15, 0xFD, 0xA1, 0x85, 0xA8, 0x5C, 0x0F, 0x11, 0xA5, 0x9D, 0xC2, 0x46,
  41920. 0xC6, 0x9C, 0xC9, 0x40, 0x0B, 0x58, 0x6A, 0x1C, 0x7A, 0x23, 0xF9, 0xE0, 0x95,
  41921. 0x05, 0x13, 0x58, 0x72, 0xE8, 0x9F, 0x30, 0xAC, 0xCD, 0x26, 0xD4, 0x66, 0x13,
  41922. 0xDF, 0x1E, 0x7B, 0x4F, 0x9C, 0xBE, 0x38, 0x79, 0x75, 0x92, 0xA4, 0xDA, 0x26,
  41923. 0x44, 0x55, 0x17, 0xA3, 0xE5, 0x62, 0xDA, 0xEB, 0x86, 0xEA, 0x68, 0xC7, 0xAB,
  41924. 0xFD, 0x2D, 0x43, 0x59, 0x51, 0xC0, 0x75, 0x64, 0x91, 0x01, 0x29, 0x33, 0x28,
  41925. 0xF3, 0x04, 0x83, 0x80, 0x75, 0x37, 0x75, 0x0C, 0x03, 0x7B, 0x0A, 0xAB, 0x8E,
  41926. 0x60, 0x62, 0x8B, 0x4C, 0xAF, 0x2D, 0xA3, 0x2F, 0xFE, 0xAB, 0x45, 0xCF, 0xDA,
  41927. 0xAB, 0xFA, 0xFA, 0x30, 0x3D, 0xE8, 0xA1, 0x96, 0xA5, 0x7B, 0xE2, 0x2A, 0xD0,
  41928. 0xAF, 0x59, 0xF7, 0xD0, 0x32, 0x57, 0x19, 0xBD, 0xCA, 0x9F, 0xD5, 0x1A, 0xC7,
  41929. 0xAA, 0x65, 0x4A, 0x38, 0xB2, 0x70, 0x33, 0xB7, 0x75, 0xD2, 0xCD, 0xD1, 0xF0,
  41930. 0xA8, 0x87, 0x59, 0x20, 0xA5, 0x57, 0x55, 0xB1, 0xB2, 0xC9, 0x4D, 0x97, 0x34,
  41931. 0x41, 0xF3, 0xF0, 0x30, 0xA1, 0x2C, 0x1C, 0x49, 0x3E, 0x89, 0x7D, 0x12, 0xE2,
  41932. 0xC3, 0x04, 0xC3, 0x92, 0xC0, 0xF6, 0x39, 0x10, 0x80, 0x81, 0x8F, 0x08, 0xB4,
  41933. 0xF8, 0xB9, 0x13, 0x4E, 0x2C, 0xAE, 0xB3, 0x71, 0x82, 0x63, 0x98, 0xAB, 0x5C,
  41934. 0x1C, 0x10, 0xEA, 0x66, 0xF9, 0x02, 0x3A, 0x82, 0x61, 0xD0, 0xD4, 0xAE, 0x43,
  41935. 0xD4, 0x01, 0x3E, 0x9D, 0x04, 0x14, 0xF6, 0x60, 0xD8, 0xA7, 0xD6, 0xB8, 0x53,
  41936. 0xC8, 0xDA, 0x80, 0x93, 0xA0, 0x02, 0xDD, 0xCC, 0xE2, 0xF2, 0xBB, 0xFB, 0xE0,
  41937. 0x27, 0xD7, 0x34, 0x9A, 0x71, 0x49, 0xB5, 0x4F, 0x42, 0x1F, 0xB2, 0x9D, 0x6D,
  41938. 0xAA, 0x9D, 0xD3, 0x50, 0xB5, 0x8F, 0x6A, 0x4B, 0xDF, 0x1F, 0xD5, 0x27, 0x8F,
  41939. 0x3B, 0x27, 0xCF, 0x2F, 0x8C, 0xF8, 0x9D, 0x4C, 0x52, 0xBC, 0x32, 0x0F, 0x73,
  41940. 0xD5, 0x51, 0x8E, 0x36, 0x7E, 0xAD, 0x09, 0xF0, 0x94, 0x83, 0x5F, 0x36, 0xFD,
  41941. 0x7C, 0x03, 0xED, 0xF1, 0x5E, 0x4B, 0xF7, 0xAA, 0x55, 0x5C, 0x4A, 0x14, 0x59,
  41942. 0x85, 0x38, 0x2D, 0x8C, 0xDF, 0xEC, 0x65, 0x1B, 0xB8, 0x76, 0x57, 0x96, 0x3C,
  41943. 0x86, 0xED, 0xF2, 0x7F, 0x2D, 0x28, 0x48, 0xDA, 0x49, 0x7F, 0xF7, 0x54, 0x2B,
  41944. 0xD5, 0x39, 0xD5, 0x57, 0x0A, 0x75, 0x7A, 0x3E, 0x5E, 0x5D, 0xBA, 0x4A, 0x15,
  41945. 0xFA, 0xB8, 0x31, 0x80, 0x71, 0x2C, 0xCA, 0xC4, 0x51, 0x10, 0x16, 0x5D, 0x39,
  41946. 0xEC, 0x9D, 0x07, 0xB6, 0x6A, 0x89, 0x9F, 0x9B, 0x5B, 0x6F, 0x03, 0xB0, 0x92,
  41947. 0x01, 0x38, 0x6B, 0x48, 0x99, 0x0A, 0x8F, 0x13, 0xC1, 0xA6, 0x01, 0xEA, 0xBF,
  41948. 0x6F, 0x86, 0x43, 0x51, 0xB6, 0x11, 0x00, 0x00
  41949. };
  41950. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t compress_test(void)
  41951. {
  41952. wc_test_ret_t ret = 0;
  41953. word32 dSz = sizeof(sample_text);
  41954. word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
  41955. byte *c;
  41956. byte *d;
  41957. WOLFSSL_ENTER("compress_test");
  41958. c = (byte *)XMALLOC(cSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41959. d = (byte *)XMALLOC(dSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41960. if (c == NULL || d == NULL) {
  41961. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  41962. }
  41963. /* follow calloc and initialize to 0 */
  41964. XMEMSET(c, 0, cSz);
  41965. XMEMSET(d, 0, dSz);
  41966. if ((ret = wc_Compress(c, cSz, sample_text, dSz, 0)) < 0) {
  41967. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  41968. }
  41969. cSz = (word32)ret;
  41970. if ((ret = wc_DeCompress(d, dSz, c, cSz)) != (int)dSz) {
  41971. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  41972. }
  41973. dSz = (word32)ret;
  41974. if (XMEMCMP(d, sample_text, dSz) != 0) {
  41975. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  41976. }
  41977. /* GZIP tests */
  41978. cSz = (dSz + (word32)(dSz * 0.001) + 12); /* reset cSz */
  41979. XMEMSET(c, 0, cSz);
  41980. XMEMSET(d, 0, dSz);
  41981. ret = wc_Compress_ex(c, cSz, sample_text, dSz, 0, LIBZ_WINBITS_GZIP);
  41982. if (ret < 0)
  41983. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  41984. cSz = (word32)ret;
  41985. ret = wc_DeCompress_ex(d, dSz, c, cSz, LIBZ_WINBITS_GZIP);
  41986. if (ret < 0)
  41987. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  41988. if (XMEMCMP(d, sample_text, dSz) != 0) {
  41989. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  41990. }
  41991. /* Try with gzip generated output */
  41992. XMEMSET(d, 0, dSz);
  41993. ret = wc_DeCompress_ex(d, dSz, sample_text_gz, sizeof(sample_text_gz),
  41994. LIBZ_WINBITS_GZIP);
  41995. if (ret < 0)
  41996. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  41997. dSz = (word32)ret;
  41998. if (XMEMCMP(d, sample_text, dSz) != 0) {
  41999. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  42000. }
  42001. ret = 0; /* success */
  42002. exit:
  42003. if (c) XFREE(c, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42004. if (d) XFREE(d, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42005. return ret;
  42006. }
  42007. #endif /* HAVE_LIBZ */
  42008. #ifdef HAVE_PKCS7
  42009. /* External Debugging/Testing Note:
  42010. *
  42011. * PKCS#7 test functions can output generated PKCS#7/CMS bundles for
  42012. * additional testing. To dump bundles to files DER encoded files, please
  42013. * define:
  42014. *
  42015. * #define PKCS7_OUTPUT_TEST_BUNDLES
  42016. */
  42017. /* Loads certs and keys for use with PKCS7 tests, from either files
  42018. * or buffers.
  42019. *
  42020. * rsaClientCertBuf - output buffer for RSA client cert
  42021. * rsaClientCertBufSz - IN/OUT size of output buffer, size of RSA client cert
  42022. * rsaClientPrivKeyBuf - output buffer for RSA client private key
  42023. * rsaClientPrivKeyBufSz - IN/OUT size of output buffer, size of RSA client key
  42024. *
  42025. * rsaServerCertBuf - output buffer for RSA server cert
  42026. * rsaServerCertBufSz - IN/OUT size of output buffer, size of RSA server cert
  42027. * rsaServerPrivKeyBuf - output buffer for RSA server private key
  42028. * rsaServerPrivKeyBufSz - IN/OUT size of output buffer, size of RSA server key
  42029. *
  42030. * rsaCaCertBuf - output buffer for RSA CA cert
  42031. * rsaCaCertBufSz - IN/OUT size of output buffer, size of RSA ca cert
  42032. * rsaCaPrivKeyBuf - output buffer for RSA CA private key
  42033. * rsaCaPrivKeyBufSz - IN/OUT size of output buffer, size of RSA CA key
  42034. *
  42035. * eccClientCertBuf - output buffer for ECC cert
  42036. * eccClientCertBufSz - IN/OUT size of output buffer, size of ECC cert
  42037. * eccClientPrivKeyBuf - output buffer for ECC private key
  42038. * eccClientPrivKeyBufSz - IN/OUT size of output buffer, size of ECC private key
  42039. *
  42040. * Returns 0 on success, negative on error
  42041. */
  42042. static wc_test_ret_t pkcs7_load_certs_keys(
  42043. byte* rsaClientCertBuf, word32* rsaClientCertBufSz,
  42044. byte* rsaClientPrivKeyBuf, word32* rsaClientPrivKeyBufSz,
  42045. byte* rsaServerCertBuf, word32* rsaServerCertBufSz,
  42046. byte* rsaServerPrivKeyBuf, word32* rsaServerPrivKeyBufSz,
  42047. byte* rsaCaCertBuf, word32* rsaCaCertBufSz,
  42048. byte* rsaCaPrivKeyBuf, word32* rsaCaPrivKeyBufSz,
  42049. byte* eccClientCertBuf, word32* eccClientCertBufSz,
  42050. byte* eccClientPrivKeyBuf, word32* eccClientPrivKeyBufSz)
  42051. {
  42052. #ifndef NO_FILESYSTEM
  42053. XFILE certFile;
  42054. XFILE keyFile;
  42055. (void)certFile;
  42056. (void)keyFile;
  42057. #endif
  42058. #ifndef NO_RSA
  42059. if (rsaClientCertBuf == NULL || rsaClientCertBufSz == NULL ||
  42060. rsaClientPrivKeyBuf == NULL || rsaClientPrivKeyBufSz == NULL)
  42061. return BAD_FUNC_ARG;
  42062. #endif
  42063. #ifdef HAVE_ECC
  42064. if (eccClientCertBuf == NULL || eccClientCertBufSz == NULL ||
  42065. eccClientPrivKeyBuf == NULL || eccClientPrivKeyBufSz == NULL)
  42066. return BAD_FUNC_ARG;
  42067. #endif
  42068. /* RSA */
  42069. #ifndef NO_RSA
  42070. #ifdef USE_CERT_BUFFERS_1024
  42071. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_1024)
  42072. return WC_TEST_RET_ENC_NC;
  42073. XMEMCPY(rsaClientCertBuf, client_cert_der_1024,
  42074. sizeof_client_cert_der_1024);
  42075. *rsaClientCertBufSz = sizeof_client_cert_der_1024;
  42076. if (rsaServerCertBuf != NULL) {
  42077. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_1024)
  42078. return WC_TEST_RET_ENC_NC;
  42079. XMEMCPY(rsaServerCertBuf, server_cert_der_1024,
  42080. sizeof_server_cert_der_1024);
  42081. *rsaServerCertBufSz = sizeof_server_cert_der_1024;
  42082. }
  42083. if (rsaCaCertBuf != NULL) {
  42084. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_1024)
  42085. return WC_TEST_RET_ENC_NC;
  42086. XMEMCPY(rsaCaCertBuf, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  42087. *rsaCaCertBufSz = sizeof_ca_cert_der_1024;
  42088. }
  42089. #elif defined(USE_CERT_BUFFERS_2048)
  42090. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_2048)
  42091. return WC_TEST_RET_ENC_NC;
  42092. XMEMCPY(rsaClientCertBuf, client_cert_der_2048,
  42093. sizeof_client_cert_der_2048);
  42094. *rsaClientCertBufSz = sizeof_client_cert_der_2048;
  42095. if (rsaServerCertBuf != NULL) {
  42096. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_2048)
  42097. return WC_TEST_RET_ENC_NC;
  42098. XMEMCPY(rsaServerCertBuf, server_cert_der_2048,
  42099. sizeof_server_cert_der_2048);
  42100. *rsaServerCertBufSz = sizeof_server_cert_der_2048;
  42101. }
  42102. if (rsaCaCertBuf != NULL) {
  42103. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_2048)
  42104. return WC_TEST_RET_ENC_NC;
  42105. XMEMCPY(rsaCaCertBuf, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  42106. *rsaCaCertBufSz = sizeof_ca_cert_der_2048;
  42107. }
  42108. #else
  42109. certFile = XFOPEN(clientCert, "rb");
  42110. if (!certFile)
  42111. return WC_TEST_RET_ENC_ERRNO;
  42112. *rsaClientCertBufSz = (word32)XFREAD(rsaClientCertBuf, 1,
  42113. *rsaClientCertBufSz, certFile);
  42114. XFCLOSE(certFile);
  42115. if (*rsaClientCertBufSz == 0)
  42116. return WC_TEST_RET_ENC_ERRNO;
  42117. if (rsaServerCertBuf != NULL) {
  42118. certFile = XFOPEN(rsaServerCertDerFile, "rb");
  42119. if (!certFile)
  42120. return WC_TEST_RET_ENC_ERRNO;
  42121. *rsaServerCertBufSz = (word32)XFREAD(rsaServerCertBuf, 1,
  42122. *rsaServerCertBufSz, certFile);
  42123. XFCLOSE(certFile);
  42124. if (*rsaServerCertBufSz == 0)
  42125. return WC_TEST_RET_ENC_ERRNO;
  42126. }
  42127. if (rsaCaCertBuf != NULL) {
  42128. certFile = XFOPEN(rsaCaCertDerFile, "rb");
  42129. if (!certFile)
  42130. return WC_TEST_RET_ENC_ERRNO;
  42131. *rsaCaCertBufSz = (word32)XFREAD(rsaCaCertBuf, 1, *rsaCaCertBufSz,
  42132. certFile);
  42133. XFCLOSE(certFile);
  42134. if (*rsaCaCertBufSz == 0)
  42135. return WC_TEST_RET_ENC_ERRNO;
  42136. }
  42137. #endif
  42138. #ifdef USE_CERT_BUFFERS_1024
  42139. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_1024)
  42140. return WC_TEST_RET_ENC_NC;
  42141. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_1024,
  42142. sizeof_client_key_der_1024);
  42143. *rsaClientPrivKeyBufSz = sizeof_client_key_der_1024;
  42144. if (rsaServerPrivKeyBuf != NULL) {
  42145. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_1024)
  42146. return WC_TEST_RET_ENC_NC;
  42147. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_1024,
  42148. sizeof_server_key_der_1024);
  42149. *rsaServerPrivKeyBufSz = sizeof_server_key_der_1024;
  42150. }
  42151. if (rsaCaPrivKeyBuf != NULL) {
  42152. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_1024)
  42153. return WC_TEST_RET_ENC_NC;
  42154. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_1024, sizeof_ca_key_der_1024);
  42155. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_1024;
  42156. }
  42157. #elif defined(USE_CERT_BUFFERS_2048)
  42158. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_2048)
  42159. return WC_TEST_RET_ENC_NC;
  42160. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_2048,
  42161. sizeof_client_key_der_2048);
  42162. *rsaClientPrivKeyBufSz = sizeof_client_key_der_2048;
  42163. if (rsaServerPrivKeyBuf != NULL) {
  42164. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_2048)
  42165. return WC_TEST_RET_ENC_NC;
  42166. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_2048,
  42167. sizeof_server_key_der_2048);
  42168. *rsaServerPrivKeyBufSz = sizeof_server_key_der_2048;
  42169. }
  42170. if (rsaCaPrivKeyBuf != NULL) {
  42171. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_2048)
  42172. return WC_TEST_RET_ENC_NC;
  42173. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_2048, sizeof_ca_key_der_2048);
  42174. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_2048;
  42175. }
  42176. #else
  42177. keyFile = XFOPEN(clientKey, "rb");
  42178. if (!keyFile)
  42179. return WC_TEST_RET_ENC_ERRNO;
  42180. *rsaClientPrivKeyBufSz = (word32)XFREAD(rsaClientPrivKeyBuf, 1,
  42181. *rsaClientPrivKeyBufSz, keyFile);
  42182. XFCLOSE(keyFile);
  42183. if (*rsaClientPrivKeyBufSz == 0)
  42184. return WC_TEST_RET_ENC_ERRNO;
  42185. if (rsaServerPrivKeyBuf != NULL) {
  42186. keyFile = XFOPEN(rsaServerKeyDerFile, "rb");
  42187. if (!keyFile)
  42188. return WC_TEST_RET_ENC_ERRNO;
  42189. *rsaServerPrivKeyBufSz = (word32)XFREAD(rsaServerPrivKeyBuf, 1,
  42190. *rsaServerPrivKeyBufSz, keyFile);
  42191. XFCLOSE(keyFile);
  42192. if (*rsaServerPrivKeyBufSz == 0)
  42193. return WC_TEST_RET_ENC_ERRNO;
  42194. }
  42195. if (rsaCaPrivKeyBuf != NULL) {
  42196. keyFile = XFOPEN(rsaCaKeyFile, "rb");
  42197. if (!keyFile)
  42198. return WC_TEST_RET_ENC_ERRNO;
  42199. *rsaCaPrivKeyBufSz = (word32)XFREAD(rsaCaPrivKeyBuf, 1,
  42200. *rsaCaPrivKeyBufSz, keyFile);
  42201. XFCLOSE(keyFile);
  42202. if (*rsaCaPrivKeyBufSz == 0)
  42203. return WC_TEST_RET_ENC_ERRNO;
  42204. }
  42205. #endif /* USE_CERT_BUFFERS */
  42206. #endif /* NO_RSA */
  42207. /* ECC */
  42208. #ifdef HAVE_ECC
  42209. #ifdef USE_CERT_BUFFERS_256
  42210. if (*eccClientCertBufSz < (word32)sizeof_cliecc_cert_der_256)
  42211. return WC_TEST_RET_ENC_NC;
  42212. XMEMCPY(eccClientCertBuf, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  42213. *eccClientCertBufSz = sizeof_cliecc_cert_der_256;
  42214. #else
  42215. certFile = XFOPEN(eccClientCert, "rb");
  42216. if (!certFile)
  42217. return WC_TEST_RET_ENC_ERRNO;
  42218. *eccClientCertBufSz = (word32)XFREAD(eccClientCertBuf, 1,
  42219. *eccClientCertBufSz, certFile);
  42220. XFCLOSE(certFile);
  42221. if (*eccClientCertBufSz == 0)
  42222. return WC_TEST_RET_ENC_ERRNO;
  42223. #endif /* USE_CERT_BUFFERS_256 */
  42224. #ifdef USE_CERT_BUFFERS_256
  42225. if (*eccClientPrivKeyBufSz < (word32)sizeof_ecc_clikey_der_256)
  42226. return WC_TEST_RET_ENC_NC;
  42227. XMEMCPY(eccClientPrivKeyBuf, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  42228. *eccClientPrivKeyBufSz = sizeof_ecc_clikey_der_256;
  42229. #else
  42230. keyFile = XFOPEN(eccClientKey, "rb");
  42231. if (!keyFile)
  42232. return WC_TEST_RET_ENC_ERRNO;
  42233. *eccClientPrivKeyBufSz = (word32)XFREAD(eccClientPrivKeyBuf, 1,
  42234. *eccClientPrivKeyBufSz, keyFile);
  42235. XFCLOSE(keyFile);
  42236. if (*eccClientPrivKeyBufSz == 0)
  42237. return WC_TEST_RET_ENC_ERRNO;
  42238. #endif /* USE_CERT_BUFFERS_256 */
  42239. #endif /* HAVE_ECC */
  42240. #ifdef NO_RSA
  42241. (void)rsaClientCertBuf;
  42242. (void)rsaClientCertBufSz;
  42243. (void)rsaClientPrivKeyBuf;
  42244. (void)rsaClientPrivKeyBufSz;
  42245. (void)rsaServerCertBuf;
  42246. (void)rsaServerCertBufSz;
  42247. (void)rsaServerPrivKeyBuf;
  42248. (void)rsaServerPrivKeyBufSz;
  42249. (void)rsaCaCertBuf;
  42250. (void)rsaCaCertBufSz;
  42251. (void)rsaCaPrivKeyBuf;
  42252. (void)rsaCaPrivKeyBufSz;
  42253. #endif
  42254. #ifndef HAVE_ECC
  42255. (void)eccClientCertBuf;
  42256. (void)eccClientCertBufSz;
  42257. (void)eccClientPrivKeyBuf;
  42258. (void)eccClientPrivKeyBufSz;
  42259. #endif
  42260. #ifndef NO_FILESYSTEM
  42261. (void)certFile;
  42262. (void)keyFile;
  42263. #endif
  42264. return 0;
  42265. }
  42266. typedef struct {
  42267. const byte* content;
  42268. word32 contentSz;
  42269. int contentOID;
  42270. int encryptOID;
  42271. int keyWrapOID;
  42272. int keyAgreeOID;
  42273. byte* cert;
  42274. size_t certSz;
  42275. byte* privateKey;
  42276. word32 privateKeySz;
  42277. byte* optionalUkm;
  42278. word32 optionalUkmSz;
  42279. int ktriOptions; /* KTRI options flags */
  42280. int kariOptions; /* KARI options flags */
  42281. /* KEKRI specific */
  42282. const byte* secretKey; /* key, only for kekri RecipientInfo types */
  42283. word32 secretKeySz; /* size of secretKey, bytes */
  42284. const byte* secretKeyId; /* key identifier */
  42285. word32 secretKeyIdSz; /* size of key identifier, bytes */
  42286. void* timePtr; /* time_t pointer */
  42287. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  42288. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  42289. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  42290. word32 otherAttrSz; /* size of otherAttr, bytes */
  42291. int kekriOptions; /* KEKRI options flags */
  42292. /* PWRI specific */
  42293. const char* password;
  42294. word32 passwordSz;
  42295. const byte* salt;
  42296. word32 saltSz;
  42297. int kdfOID;
  42298. int hashOID;
  42299. int kdfIterations;
  42300. int pwriOptions; /* PWRI options flags */
  42301. /* ORI specific */
  42302. int isOri;
  42303. int oriOptions; /* ORI options flags */
  42304. const char* outFileName;
  42305. } pkcs7EnvelopedVector;
  42306. static const byte asnDataOid[] = {
  42307. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
  42308. };
  42309. /* ORI encrypt callback, responsible for encrypting content-encryption key (CEK)
  42310. * and giving wolfCrypt the value for oriOID and oriValue to place in
  42311. * OtherRecipientInfo.
  42312. *
  42313. * Returns 0 on success, negative upon error. */
  42314. static int myOriEncryptCb(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* oriType,
  42315. word32* oriTypeSz, byte* oriValue, word32* oriValueSz,
  42316. void* ctx)
  42317. {
  42318. int i;
  42319. /* make sure buffers are large enough */
  42320. if (*oriValueSz < (2 + cekSz))
  42321. return WC_TEST_RET_ENC_NC;
  42322. if (*oriTypeSz < sizeof(asnDataOid))
  42323. return WC_TEST_RET_ENC_NC;
  42324. /* our simple encryption algorithm will be take the bitwise complement */
  42325. oriValue[0] = 0x04; /*ASN OCTET STRING */
  42326. oriValue[1] = (byte)cekSz; /* length */
  42327. for (i = 0; i < (int)cekSz; i++) {
  42328. oriValue[2 + i] = ~cek[i];
  42329. }
  42330. *oriValueSz = 2 + cekSz;
  42331. /* set oriType to ASN.1 encoded data OID */
  42332. XMEMCPY(oriType, asnDataOid, sizeof(asnDataOid));
  42333. *oriTypeSz = sizeof(asnDataOid);
  42334. (void)pkcs7;
  42335. (void)ctx;
  42336. return 0;
  42337. }
  42338. /* ORI decrypt callback, responsible for providing a decrypted content
  42339. * encryption key (CEK) placed into decryptedKey and size placed into
  42340. * decryptedKeySz. oriOID and oriValue are given to the callback to help
  42341. * in decrypting the encrypted CEK.
  42342. *
  42343. * Returns 0 on success, negative upon error. */
  42344. static int myOriDecryptCb(PKCS7* pkcs7, byte* oriType, word32 oriTypeSz,
  42345. byte* oriValue, word32 oriValueSz, byte* decryptedKey,
  42346. word32* decryptedKeySz, void* ctx)
  42347. {
  42348. int i;
  42349. /* make sure oriType matches what we expect */
  42350. if (oriTypeSz != sizeof(asnDataOid))
  42351. return WC_TEST_RET_ENC_NC;
  42352. if (XMEMCMP(oriType, asnDataOid, sizeof(asnDataOid)) != 0)
  42353. return WC_TEST_RET_ENC_NC;
  42354. /* make sure decrypted buffer is large enough */
  42355. if (*decryptedKeySz < oriValueSz)
  42356. return WC_TEST_RET_ENC_NC;
  42357. /* decrypt encrypted CEK using simple bitwise complement,
  42358. only for example */
  42359. for (i = 0; i < (int)oriValueSz - 2; i++) {
  42360. decryptedKey[i] = ~oriValue[2 + i];
  42361. }
  42362. *decryptedKeySz = oriValueSz - 2;
  42363. (void)pkcs7;
  42364. (void)ctx;
  42365. return 0;
  42366. }
  42367. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  42368. /* returns 0 on success */
  42369. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  42370. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  42371. byte* in, int inSz, byte* out, void* usrCtx)
  42372. {
  42373. wc_test_ret_t ret;
  42374. int keyId = -1, keySz;
  42375. word32 keyIdSz = 8;
  42376. const byte* key;
  42377. byte keyIdRaw[8];
  42378. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  42379. Aes *aes;
  42380. #else
  42381. Aes aes[1];
  42382. #endif
  42383. /* looking for KEY ID
  42384. * fwDecryptKeyID OID "1.2.840.113549.1.9.16.2.37
  42385. */
  42386. WOLFSSL_SMALL_STACK_STATIC const unsigned char OID[] = {
  42387. /* 0x06, 0x0B do not pass in tag and length */
  42388. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  42389. 0x01, 0x09, 0x10, 0x02, 0x25
  42390. };
  42391. WOLFSSL_SMALL_STACK_STATIC const byte defKey[] = {
  42392. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  42393. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  42394. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  42395. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  42396. };
  42397. WOLFSSL_SMALL_STACK_STATIC const byte altKey[] = {
  42398. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  42399. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  42400. };
  42401. /* test user context passed in */
  42402. if (usrCtx == NULL || *(int*)usrCtx != 1) {
  42403. return WC_TEST_RET_ENC_NC;
  42404. }
  42405. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  42406. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  42407. return WC_TEST_RET_ENC_ERRNO;
  42408. #endif
  42409. /* if needing to find keyIdSz can call with NULL */
  42410. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), NULL,
  42411. &keyIdSz);
  42412. if (ret != LENGTH_ONLY_E) {
  42413. printf("Unexpected error %d when getting keyIdSz\n", ret);
  42414. printf("Possibly no KEY ID attribute set\n");
  42415. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42416. }
  42417. else {
  42418. XMEMSET(keyIdRaw, 0, sizeof(keyIdRaw));
  42419. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), keyIdRaw,
  42420. &keyIdSz);
  42421. if (ret < 0) {
  42422. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42423. }
  42424. if (keyIdSz < 3) {
  42425. printf("keyIdSz is smaller than expected\n");
  42426. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42427. }
  42428. if (keyIdSz > 2 + sizeof(int)) {
  42429. printf("example case was only expecting a keyId of int size\n");
  42430. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42431. }
  42432. /* keyIdRaw[0] OCTET TAG */
  42433. /* keyIdRaw[1] Length */
  42434. #ifdef BIG_ENDIAN_ORDER
  42435. if (keyIdRaw[1] == 0x01) {
  42436. keyId = 1;
  42437. }
  42438. #else
  42439. XMEMCPY(&keyId, keyIdRaw + 2, sizeof(keyId));
  42440. #endif
  42441. }
  42442. /* Use keyID here if found to select key and decrypt in HSM or in this
  42443. * example just select key and do software decryption */
  42444. if (keyId == 1) {
  42445. key = altKey;
  42446. keySz = sizeof(altKey);
  42447. }
  42448. else {
  42449. key = defKey;
  42450. keySz = sizeof(defKey);
  42451. }
  42452. switch (encryptOID) {
  42453. #ifdef WOLFSSL_AES_256
  42454. case AES256CBCb:
  42455. if ((keySz != 32 ) || (ivSz != AES_BLOCK_SIZE))
  42456. WARNING_OUT(BAD_FUNC_ARG, out);
  42457. break;
  42458. #endif
  42459. #ifdef WOLFSSL_AES_128
  42460. case AES128CBCb:
  42461. if ((keySz != 16 ) || (ivSz != AES_BLOCK_SIZE))
  42462. ERROR_OUT(BAD_FUNC_ARG, out);
  42463. break;
  42464. #endif
  42465. default:
  42466. printf("Unsupported content cipher type for example");
  42467. ERROR_OUT(ALGO_ID_E, out);
  42468. };
  42469. ret = wc_AesInit(aes, HEAP_HINT, INVALID_DEVID);
  42470. if (ret == 0) {
  42471. ret = wc_AesSetKey(aes, key, (word32)keySz, iv, AES_DECRYPTION);
  42472. if (ret == 0)
  42473. ret = wc_AesCbcDecrypt(aes, out, in, (word32)inSz);
  42474. wc_AesFree(aes);
  42475. }
  42476. out:
  42477. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  42478. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42479. #endif
  42480. (void)aad;
  42481. (void)aadSz;
  42482. (void)authTag;
  42483. (void)authTagSz;
  42484. return (int)ret;
  42485. }
  42486. #endif /* !NO_AES && HAVE_AES_CBC */
  42487. #define PKCS7_BUF_SIZE 2048
  42488. static wc_test_ret_t pkcs7enveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  42489. byte* rsaPrivKey, word32 rsaPrivKeySz,
  42490. byte* eccCert, word32 eccCertSz,
  42491. byte* eccPrivKey, word32 eccPrivKeySz)
  42492. {
  42493. wc_test_ret_t ret = 0;
  42494. int testSz = 0, i;
  42495. int envelopedSz, decodedSz;
  42496. byte *enveloped = NULL;
  42497. byte *decoded = NULL;
  42498. PKCS7* pkcs7 = NULL;
  42499. #ifdef ECC_TIMING_RESISTANT
  42500. WC_RNG rng;
  42501. #endif
  42502. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  42503. XFILE pkcs7File;
  42504. #endif
  42505. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  42506. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  42507. 0x72,0x6c,0x64
  42508. };
  42509. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256) && \
  42510. defined(HAVE_ECC) && defined(WOLFSSL_SHA512)
  42511. byte optionalUkm[] = {
  42512. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  42513. };
  42514. #endif /* NO_AES */
  42515. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) && \
  42516. !defined(NO_SHA)
  42517. /* encryption key for kekri recipient types */
  42518. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  42519. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  42520. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  42521. };
  42522. /* encryption key identifier */
  42523. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  42524. 0x02,0x02,0x03,0x04
  42525. };
  42526. #endif
  42527. #if !defined(NO_PWDBASED) && !defined(NO_SHA) && \
  42528. !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  42529. #ifndef HAVE_FIPS
  42530. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password"; /* NOTE: Password is too short for FIPS */
  42531. #else
  42532. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  42533. #endif
  42534. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  42535. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  42536. };
  42537. #endif
  42538. #define MAX_TESTVECTORS_LEN 13
  42539. #define ADD_PKCS7ENVELOPEDVECTOR(...) { \
  42540. pkcs7EnvelopedVector _this_vector = { __VA_ARGS__ }; \
  42541. if (testSz == MAX_TESTVECTORS_LEN) { \
  42542. ret = WC_TEST_RET_ENC_NC; \
  42543. goto out; \
  42544. } \
  42545. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  42546. }
  42547. pkcs7EnvelopedVector *testVectors = NULL;
  42548. #ifdef ECC_TIMING_RESISTANT
  42549. XMEMSET(&rng, 0, sizeof(rng));
  42550. #endif
  42551. testVectors = (pkcs7EnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  42552. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42553. if (testVectors == NULL) {
  42554. ret = WC_TEST_RET_ENC_ERRNO;
  42555. goto out;
  42556. }
  42557. {
  42558. /* key transport key encryption technique */
  42559. #ifndef NO_RSA
  42560. #ifndef NO_DES3
  42561. ADD_PKCS7ENVELOPEDVECTOR(
  42562. data, (word32)sizeof(data), DATA, DES3b, 0, 0, rsaCert, rsaCertSz,
  42563. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  42564. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  42565. "pkcs7envelopedDataDES3.der");
  42566. #endif
  42567. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  42568. #ifdef WOLFSSL_AES_128
  42569. ADD_PKCS7ENVELOPEDVECTOR(
  42570. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, rsaCert, rsaCertSz,
  42571. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  42572. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  42573. "pkcs7envelopedDataAES128CBC.der");
  42574. #endif
  42575. #ifdef WOLFSSL_AES_192
  42576. ADD_PKCS7ENVELOPEDVECTOR(
  42577. data, (word32)sizeof(data), DATA, AES192CBCb, 0, 0, rsaCert, rsaCertSz,
  42578. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  42579. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  42580. "pkcs7envelopedDataAES192CBC.der");
  42581. #endif
  42582. #ifdef WOLFSSL_AES_256
  42583. ADD_PKCS7ENVELOPEDVECTOR(
  42584. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  42585. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  42586. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  42587. "pkcs7envelopedDataAES256CBC.der");
  42588. /* explicitly using SKID for SubjectKeyIdentifier */
  42589. ADD_PKCS7ENVELOPEDVECTOR(
  42590. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  42591. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_SKID, 0, NULL, 0, NULL, 0, NULL,
  42592. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  42593. "pkcs7envelopedDataAES256CBC_SKID.der");
  42594. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  42595. ADD_PKCS7ENVELOPEDVECTOR(
  42596. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  42597. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_ISSUER_AND_SERIAL_NUMBER, 0,
  42598. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  42599. 0, 0, 0, 0, "pkcs7envelopedDataAES256CBC_IANDS.der");
  42600. #endif
  42601. #endif /* !NO_AES && HAVE_AES_CBC */
  42602. #endif
  42603. /* key agreement key encryption technique*/
  42604. #ifdef HAVE_ECC
  42605. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  42606. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  42607. ADD_PKCS7ENVELOPEDVECTOR(
  42608. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP,
  42609. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  42610. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  42611. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  42612. "pkcs7envelopedDataAES128CBC_ECDH_SHA1KDF.der");
  42613. #endif
  42614. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  42615. ADD_PKCS7ENVELOPEDVECTOR(
  42616. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  42617. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  42618. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  42619. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  42620. "pkcs7envelopedDataAES256CBC_ECDH_SHA256KDF.der");
  42621. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  42622. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  42623. ADD_PKCS7ENVELOPEDVECTOR(
  42624. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  42625. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  42626. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  42627. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  42628. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF.der");
  42629. /* with optional user keying material (ukm) */
  42630. ADD_PKCS7ENVELOPEDVECTOR(
  42631. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  42632. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  42633. eccPrivKeySz, optionalUkm, sizeof(optionalUkm), 0, 0, NULL, 0,
  42634. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  42635. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF_ukm.der");
  42636. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  42637. #endif /* !NO_AES && HAVE_AES_CBC */
  42638. #endif
  42639. /* kekri (KEKRecipientInfo) recipient types */
  42640. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  42641. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  42642. ADD_PKCS7ENVELOPEDVECTOR(
  42643. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP, 0,
  42644. NULL, 0, NULL, 0, NULL, 0, 0, 0, secretKey, sizeof(secretKey),
  42645. secretKeyId, sizeof(secretKeyId), NULL, NULL, 0, NULL, 0,
  42646. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  42647. "pkcs7envelopedDataAES128CBC_KEKRI.der");
  42648. #endif
  42649. #endif /* !NO_AES && HAVE_AES_CBC */
  42650. /* pwri (PasswordRecipientInfo) recipient types */
  42651. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  42652. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  42653. ADD_PKCS7ENVELOPEDVECTOR(
  42654. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0,
  42655. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  42656. NULL, 0, NULL, NULL, 0, NULL, 0, 0, password,
  42657. (word32)XSTRLEN(password), salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  42658. 0, 0, 0, "pkcs7envelopedDataAES128CBC_PWRI.der");
  42659. #endif
  42660. #endif
  42661. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_128)
  42662. /* ori (OtherRecipientInfo) recipient types */
  42663. ADD_PKCS7ENVELOPEDVECTOR(
  42664. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, NULL, 0, NULL, 0,
  42665. NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0,
  42666. NULL, 0, 0, 0, 0, 0, 1, 0, "pkcs7envelopedDataAES128CBC_ORI.der");
  42667. #endif
  42668. };
  42669. #undef MAX_TESTVECTORS_LEN
  42670. #undef ADD_PKCS7ENVELOPEDVECTOR
  42671. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42672. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42673. if ((! enveloped) || (! decoded)) {
  42674. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42675. }
  42676. #ifdef ECC_TIMING_RESISTANT
  42677. #ifndef HAVE_FIPS
  42678. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  42679. #else
  42680. ret = wc_InitRng(&rng);
  42681. #endif
  42682. if (ret != 0)
  42683. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42684. #endif
  42685. for (i = 0; i < testSz; i++) {
  42686. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  42687. #ifdef WOLFSSL_ASYNC_CRYPT
  42688. INVALID_DEVID /* async PKCS7 is not supported */
  42689. #else
  42690. devId
  42691. #endif
  42692. );
  42693. if (pkcs7 == NULL) {
  42694. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42695. }
  42696. if (testVectors[i].secretKey != NULL) {
  42697. /* KEKRI recipient type */
  42698. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  42699. if (ret != 0)
  42700. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42701. pkcs7->content = (byte*)testVectors[i].content;
  42702. pkcs7->contentSz = testVectors[i].contentSz;
  42703. pkcs7->contentOID = testVectors[i].contentOID;
  42704. pkcs7->encryptOID = testVectors[i].encryptOID;
  42705. pkcs7->ukm = testVectors[i].optionalUkm;
  42706. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  42707. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  42708. (byte *)testVectors[i].secretKey, testVectors[i].secretKeySz,
  42709. (byte *)testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  42710. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  42711. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  42712. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  42713. if (ret < 0) {
  42714. wc_PKCS7_Free(pkcs7);
  42715. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42716. }
  42717. /* set key, for decryption */
  42718. ret = wc_PKCS7_SetKey(pkcs7, (byte *)testVectors[i].secretKey,
  42719. testVectors[i].secretKeySz);
  42720. if (ret != 0) {
  42721. wc_PKCS7_Free(pkcs7);
  42722. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42723. }
  42724. } else if (testVectors[i].password != NULL) {
  42725. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  42726. /* PWRI recipient type */
  42727. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  42728. if (ret != 0)
  42729. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42730. pkcs7->content = (byte*)testVectors[i].content;
  42731. pkcs7->contentSz = testVectors[i].contentSz;
  42732. pkcs7->contentOID = testVectors[i].contentOID;
  42733. pkcs7->encryptOID = testVectors[i].encryptOID;
  42734. pkcs7->ukm = testVectors[i].optionalUkm;
  42735. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  42736. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  42737. (byte *)testVectors[i].password, testVectors[i].passwordSz,
  42738. (byte *)testVectors[i].salt, testVectors[i].saltSz,
  42739. testVectors[i].kdfOID,
  42740. testVectors[i].hashOID, testVectors[i].kdfIterations,
  42741. testVectors[i].encryptOID, testVectors[i].pwriOptions);
  42742. if (ret < 0) {
  42743. wc_PKCS7_Free(pkcs7);
  42744. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42745. }
  42746. /* set password, for decryption */
  42747. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  42748. testVectors[i].passwordSz);
  42749. if (ret < 0) {
  42750. wc_PKCS7_Free(pkcs7);
  42751. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42752. }
  42753. #endif /* ! NO_PWDBASED && ! NO_SHA */
  42754. } else if (testVectors[i].isOri == 1) {
  42755. /* ORI recipient type */
  42756. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  42757. if (ret != 0)
  42758. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42759. pkcs7->content = (byte*)testVectors[i].content;
  42760. pkcs7->contentSz = testVectors[i].contentSz;
  42761. pkcs7->contentOID = testVectors[i].contentOID;
  42762. pkcs7->encryptOID = testVectors[i].encryptOID;
  42763. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  42764. testVectors[i].oriOptions);
  42765. if (ret < 0) {
  42766. wc_PKCS7_Free(pkcs7);
  42767. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42768. }
  42769. /* set decrypt callback for decryption */
  42770. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  42771. if (ret < 0) {
  42772. wc_PKCS7_Free(pkcs7);
  42773. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42774. }
  42775. } else {
  42776. /* KTRI or KARI recipient types */
  42777. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  42778. if (ret != 0)
  42779. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42780. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  42781. (word32)testVectors[i].certSz);
  42782. if (ret != 0) {
  42783. wc_PKCS7_Free(pkcs7);
  42784. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42785. }
  42786. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  42787. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  42788. pkcs7->privateKey = testVectors[i].privateKey;
  42789. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  42790. pkcs7->content = (byte*)testVectors[i].content;
  42791. pkcs7->contentSz = testVectors[i].contentSz;
  42792. pkcs7->contentOID = testVectors[i].contentOID;
  42793. pkcs7->encryptOID = testVectors[i].encryptOID;
  42794. pkcs7->ukm = testVectors[i].optionalUkm;
  42795. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  42796. /* set SubjectIdentifier type for KTRI types */
  42797. if (testVectors[i].ktriOptions & CMS_SKID) {
  42798. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  42799. if (ret != 0) {
  42800. wc_PKCS7_Free(pkcs7);
  42801. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42802. }
  42803. } else if (testVectors[i].ktriOptions &
  42804. CMS_ISSUER_AND_SERIAL_NUMBER) {
  42805. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  42806. CMS_ISSUER_AND_SERIAL_NUMBER);
  42807. if (ret != 0) {
  42808. wc_PKCS7_Free(pkcs7);
  42809. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  42810. }
  42811. }
  42812. }
  42813. #ifdef ECC_TIMING_RESISTANT
  42814. pkcs7->rng = &rng;
  42815. #endif
  42816. /* encode envelopedData */
  42817. envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, enveloped,
  42818. PKCS7_BUF_SIZE);
  42819. if (envelopedSz <= 0) {
  42820. wc_PKCS7_Free(pkcs7);
  42821. ERROR_OUT(WC_TEST_RET_ENC_EC(envelopedSz), out);
  42822. }
  42823. /* decode envelopedData */
  42824. pkcs7->contentOID = 0;
  42825. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped, (word32)envelopedSz,
  42826. decoded, PKCS7_BUF_SIZE);
  42827. if (pkcs7->contentOID != testVectors[i].contentOID ||
  42828. decodedSz <= 0) {
  42829. wc_PKCS7_Free(pkcs7);
  42830. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  42831. }
  42832. /* test decode result */
  42833. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  42834. wc_PKCS7_Free(pkcs7);
  42835. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42836. }
  42837. #ifndef NO_PKCS7_STREAM
  42838. { /* test reading byte by byte */
  42839. int z;
  42840. for (z = 0; z < envelopedSz; z++) {
  42841. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped + z, 1,
  42842. decoded, PKCS7_BUF_SIZE);
  42843. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  42844. printf("unexpected error %d\n", decodedSz);
  42845. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  42846. }
  42847. }
  42848. /* test decode result */
  42849. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  42850. printf("stream read compare failed\n");
  42851. wc_PKCS7_Free(pkcs7);
  42852. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  42853. }
  42854. }
  42855. #endif
  42856. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  42857. /* output pkcs7 envelopedData for external testing */
  42858. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  42859. if (!pkcs7File) {
  42860. wc_PKCS7_Free(pkcs7);
  42861. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42862. }
  42863. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  42864. XFCLOSE(pkcs7File);
  42865. if (ret != envelopedSz) {
  42866. wc_PKCS7_Free(pkcs7);
  42867. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  42868. } else {
  42869. /* reset ret to 0 for success */
  42870. ret = 0;
  42871. }
  42872. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  42873. wc_PKCS7_Free(pkcs7);
  42874. pkcs7 = NULL;
  42875. }
  42876. #ifdef ECC_TIMING_RESISTANT
  42877. wc_FreeRng(&rng);
  42878. #endif
  42879. (void)eccCert;
  42880. (void)eccCertSz;
  42881. (void)eccPrivKey;
  42882. (void)eccPrivKeySz;
  42883. (void)rsaCert;
  42884. (void)rsaCertSz;
  42885. (void)rsaPrivKey;
  42886. (void)rsaPrivKeySz;
  42887. out:
  42888. if (testVectors)
  42889. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42890. if (enveloped)
  42891. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42892. if (decoded)
  42893. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42894. return ret;
  42895. }
  42896. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7enveloped_test(void)
  42897. {
  42898. wc_test_ret_t ret = 0;
  42899. byte* rsaCert = NULL;
  42900. byte* rsaPrivKey = NULL;
  42901. word32 rsaCertSz = 0;
  42902. word32 rsaPrivKeySz = 0;
  42903. byte* eccCert = NULL;
  42904. byte* eccPrivKey = NULL;
  42905. word32 eccCertSz = 0;
  42906. word32 eccPrivKeySz = 0;
  42907. WOLFSSL_ENTER("pkcs7enveloped_test");
  42908. #ifndef NO_RSA
  42909. /* read client RSA cert and key in DER format */
  42910. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42911. if (rsaCert == NULL)
  42912. return WC_TEST_RET_ENC_ERRNO;
  42913. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42914. if (rsaPrivKey == NULL) {
  42915. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42916. return WC_TEST_RET_ENC_NC;
  42917. }
  42918. rsaCertSz = FOURK_BUF;
  42919. rsaPrivKeySz = FOURK_BUF;
  42920. #endif /* NO_RSA */
  42921. #ifdef HAVE_ECC
  42922. /* read client ECC cert and key in DER format */
  42923. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42924. if (eccCert == NULL) {
  42925. #ifndef NO_RSA
  42926. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42927. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42928. #endif
  42929. return WC_TEST_RET_ENC_NC;
  42930. }
  42931. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42932. if (eccPrivKey == NULL) {
  42933. #ifndef NO_RSA
  42934. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42935. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42936. #endif
  42937. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42938. return WC_TEST_RET_ENC_NC;
  42939. }
  42940. eccCertSz = FOURK_BUF;
  42941. eccPrivKeySz = FOURK_BUF;
  42942. #endif /* HAVE_ECC */
  42943. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  42944. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  42945. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  42946. eccPrivKey, &eccPrivKeySz);
  42947. if (ret < 0) {
  42948. #ifndef NO_RSA
  42949. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42950. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42951. #endif
  42952. #ifdef HAVE_ECC
  42953. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42954. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42955. #endif
  42956. return WC_TEST_RET_ENC_EC(ret);
  42957. }
  42958. ret = pkcs7enveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  42959. rsaPrivKey, (word32)rsaPrivKeySz,
  42960. eccCert, (word32)eccCertSz,
  42961. eccPrivKey, (word32)eccPrivKeySz);
  42962. #ifndef NO_RSA
  42963. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42964. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42965. #endif
  42966. #ifdef HAVE_ECC
  42967. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42968. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42969. #endif
  42970. return ret;
  42971. }
  42972. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  42973. typedef struct {
  42974. const byte* content;
  42975. word32 contentSz;
  42976. int contentOID;
  42977. int encryptOID;
  42978. int keyWrapOID;
  42979. int keyAgreeOID;
  42980. byte* cert;
  42981. size_t certSz;
  42982. byte* privateKey;
  42983. word32 privateKeySz;
  42984. PKCS7Attrib* authAttribs;
  42985. word32 authAttribsSz;
  42986. PKCS7Attrib* unauthAttribs;
  42987. word32 unauthAttribsSz;
  42988. /* KARI / KTRI specific */
  42989. byte* optionalUkm;
  42990. word32 optionalUkmSz;
  42991. int ktriOptions; /* KTRI options flags */
  42992. int kariOptions; /* KARI options flags */
  42993. /* KEKRI specific */
  42994. byte* secretKey; /* key, only for kekri RecipientInfo types */
  42995. word32 secretKeySz; /* size of secretKey, bytes */
  42996. byte* secretKeyId; /* key identifier */
  42997. word32 secretKeyIdSz; /* size of key identifier, bytes */
  42998. void* timePtr; /* time_t pointer */
  42999. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  43000. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  43001. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  43002. word32 otherAttrSz; /* size of otherAttr, bytes */
  43003. int kekriOptions; /* KEKRI options flags */
  43004. /* PWRI specific */
  43005. char* password; /* password */
  43006. word32 passwordSz; /* password size, bytes */
  43007. byte* salt; /* KDF salt */
  43008. word32 saltSz; /* KDF salt size, bytes */
  43009. int kdfOID; /* KDF OID */
  43010. int hashOID; /* KDF hash algorithm OID */
  43011. int kdfIterations; /* KDF iterations */
  43012. int kekEncryptOID; /* KEK encryption algorithm OID */
  43013. int pwriOptions; /* PWRI options flags */
  43014. /* ORI specific */
  43015. int isOri;
  43016. int oriOptions; /* ORI options flags */
  43017. const char* outFileName;
  43018. } pkcs7AuthEnvelopedVector;
  43019. static wc_test_ret_t pkcs7authenveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  43020. byte* rsaPrivKey, word32 rsaPrivKeySz,
  43021. byte* eccCert, word32 eccCertSz,
  43022. byte* eccPrivKey, word32 eccPrivKeySz)
  43023. {
  43024. wc_test_ret_t ret = 0;
  43025. int testSz = 0, i;
  43026. int envelopedSz, decodedSz;
  43027. byte *enveloped = NULL;
  43028. byte *decoded = NULL;
  43029. WC_RNG rng;
  43030. PKCS7* pkcs7;
  43031. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  43032. XFILE pkcs7File;
  43033. #endif
  43034. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  43035. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  43036. 0x72,0x6c,0x64
  43037. };
  43038. byte senderNonce[PKCS7_NONCE_SZ + 2];
  43039. #ifdef HAVE_ECC
  43040. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  43041. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  43042. WOLFSSL_SMALL_STACK_STATIC const byte senderNonceOid[] =
  43043. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  43044. 0x09, 0x05 };
  43045. PKCS7Attrib attribs[] =
  43046. {
  43047. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  43048. sizeof(senderNonce) }
  43049. };
  43050. #endif
  43051. #endif
  43052. #endif
  43053. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  43054. defined(WOLFSSL_SHA512) && defined(HAVE_AESGCM)
  43055. WOLFSSL_SMALL_STACK_STATIC const byte optionalUkm[] = {
  43056. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  43057. };
  43058. #endif /* NO_AES */
  43059. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  43060. /* encryption key for kekri recipient types */
  43061. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  43062. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  43063. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  43064. };
  43065. /* encryption key identifier */
  43066. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  43067. 0x02,0x02,0x03,0x04
  43068. };
  43069. #endif
  43070. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  43071. !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  43072. #ifndef HAVE_FIPS
  43073. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password";
  43074. #else
  43075. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  43076. #endif
  43077. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  43078. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  43079. };
  43080. #endif
  43081. #define MAX_TESTVECTORS_LEN 20
  43082. #define ADD_PKCS7AUTHENVELOPEDVECTOR(...) { \
  43083. pkcs7AuthEnvelopedVector _this_vector = { __VA_ARGS__ }; \
  43084. if (testSz == MAX_TESTVECTORS_LEN) { \
  43085. ret = WC_TEST_RET_ENC_NC; \
  43086. goto out; \
  43087. } \
  43088. XMEMCPY(&testVectors[testSz++], &_this_vector, \
  43089. sizeof _this_vector); \
  43090. }
  43091. pkcs7AuthEnvelopedVector *testVectors = NULL;
  43092. XMEMSET(&rng, 0, sizeof(rng));
  43093. testVectors = (pkcs7AuthEnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  43094. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43095. if (testVectors == NULL) {
  43096. ret = WC_TEST_RET_ENC_ERRNO;
  43097. goto out;
  43098. }
  43099. {
  43100. /* key transport key encryption technique */
  43101. #ifndef NO_RSA
  43102. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  43103. #ifdef WOLFSSL_AES_128
  43104. ADD_PKCS7AUTHENVELOPEDVECTOR(
  43105. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, rsaCert, rsaCertSz,
  43106. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  43107. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  43108. 0, 0, "pkcs7authEnvelopedDataAES128GCM.der");
  43109. #endif
  43110. #ifdef WOLFSSL_AES_192
  43111. ADD_PKCS7AUTHENVELOPEDVECTOR(
  43112. data, (word32)sizeof(data), DATA, AES192GCMb, 0, 0, rsaCert, rsaCertSz,
  43113. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  43114. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  43115. 0, 0, "pkcs7authEnvelopedDataAES192GCM.der");
  43116. #endif
  43117. #ifdef WOLFSSL_AES_256
  43118. ADD_PKCS7AUTHENVELOPEDVECTOR(
  43119. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  43120. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  43121. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  43122. 0, 0, "pkcs7authEnvelopedDataAES256GCM.der");
  43123. /* test with contentType set to FirmwarePkgData */
  43124. ADD_PKCS7AUTHENVELOPEDVECTOR(
  43125. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, 0, 0,
  43126. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL,
  43127. 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL,
  43128. 0, 0, 0, 0, 0, 0, 0, 0,
  43129. "pkcs7authEnvelopedDataAES256GCM_firmwarePkgData.der");
  43130. /* explicitly using SKID for SubjectKeyIdentifier */
  43131. ADD_PKCS7AUTHENVELOPEDVECTOR(
  43132. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  43133. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, CMS_SKID, 0,
  43134. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  43135. 0, 0, 0, 0, 0, "pkcs7authEnvelopedDataAES256GCM_SKID.der");
  43136. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  43137. ADD_PKCS7AUTHENVELOPEDVECTOR(
  43138. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  43139. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0,
  43140. CMS_ISSUER_AND_SERIAL_NUMBER, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  43141. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  43142. "pkcs7authEnvelopedDataAES256GCM_IANDS.der");
  43143. #endif
  43144. #else /* NO_AES || !HAVE_AESGCM */
  43145. (void)rsaCert;
  43146. (void)rsaCertSz;
  43147. (void)rsaPrivKey;
  43148. (void)rsaPrivKeySz;
  43149. #endif /* NO_AES || !HAVE_AESGCM */
  43150. #endif
  43151. /* key agreement key encryption technique*/
  43152. #ifdef HAVE_ECC
  43153. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  43154. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  43155. ADD_PKCS7AUTHENVELOPEDVECTOR(
  43156. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP,
  43157. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  43158. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  43159. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  43160. "pkcs7authEnvelopedDataAES128GCM_ECDH_SHA1KDF.der");
  43161. #endif
  43162. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  43163. ADD_PKCS7AUTHENVELOPEDVECTOR(
  43164. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  43165. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  43166. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  43167. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  43168. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF.der");
  43169. /* with authenticated attributes */
  43170. ADD_PKCS7AUTHENVELOPEDVECTOR(
  43171. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  43172. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  43173. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  43174. NULL, 0, NULL, 0, 0, 0, NULL, 0,
  43175. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  43176. 0, 0, 0,
  43177. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_authAttribs.der");
  43178. /* with unauthenticated attributes */
  43179. ADD_PKCS7AUTHENVELOPEDVECTOR(
  43180. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  43181. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  43182. eccPrivKeySz, NULL, 0, attribs,
  43183. (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0, NULL, 0,
  43184. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  43185. 0, 0, 0,
  43186. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_unauthAttribs.der");
  43187. /* with authenticated AND unauthenticated attributes */
  43188. ADD_PKCS7AUTHENVELOPEDVECTOR(
  43189. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  43190. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  43191. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  43192. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  43193. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  43194. 0, 0, 0, 0, 0, 0,
  43195. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_bothAttribs.der");
  43196. /* with authenticated AND unauthenticated attributes AND
  43197. * contentType of FirmwarePkgData */
  43198. ADD_PKCS7AUTHENVELOPEDVECTOR(
  43199. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, AES256_WRAP,
  43200. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  43201. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  43202. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  43203. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  43204. 0, 0, 0, 0, 0, 0,
  43205. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_fw_bothAttribs.der");
  43206. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  43207. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  43208. ADD_PKCS7AUTHENVELOPEDVECTOR(
  43209. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  43210. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  43211. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL,
  43212. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  43213. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF.der");
  43214. /* with optional user keying material (ukm) */
  43215. ADD_PKCS7AUTHENVELOPEDVECTOR(
  43216. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  43217. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  43218. eccPrivKeySz, NULL, 0, NULL, 0, (byte *)optionalUkm, sizeof(optionalUkm), 0,
  43219. 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  43220. 0, 0, 0, 0, 0, 0,
  43221. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF_ukm.der");
  43222. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  43223. #endif /* NO_AES */
  43224. #endif
  43225. /* kekri (KEKRecipientInfo) recipient types */
  43226. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  43227. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  43228. ADD_PKCS7AUTHENVELOPEDVECTOR(
  43229. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP, 0,
  43230. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0,
  43231. (byte *)secretKey, sizeof(secretKey), (byte *)secretKeyId, sizeof(secretKeyId),
  43232. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  43233. "pkcs7authEnvelopedDataAES128GCM_KEKRI.der");
  43234. #endif
  43235. #endif
  43236. /* pwri (PasswordRecipientInfo) recipient types */
  43237. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM)
  43238. #if !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  43239. ADD_PKCS7AUTHENVELOPEDVECTOR(
  43240. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0,
  43241. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  43242. NULL, 0, NULL, NULL, 0, NULL, 0, 0, (char *)password,
  43243. (word32)XSTRLEN(password), (byte *)salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  43244. AES128CBCb, 0, 0, 0, "pkcs7authEnvelopedDataAES128GCM_PWRI.der");
  43245. #endif
  43246. #endif
  43247. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  43248. #ifdef WOLFSSL_AES_128
  43249. /* ori (OtherRecipientInfo) recipient types */
  43250. ADD_PKCS7AUTHENVELOPEDVECTOR(
  43251. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, NULL, 0, NULL, 0,
  43252. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  43253. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 1, 0,
  43254. "pkcs7authEnvelopedDataAES128GCM_ORI.der");
  43255. #endif
  43256. #endif
  43257. }
  43258. #undef MAX_TESTVECTORS_LEN
  43259. #undef ADD_PKCS7AUTHENVELOPEDVECTOR
  43260. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43261. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43262. if ((! enveloped) || (! decoded)) {
  43263. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  43264. }
  43265. /* generate senderNonce */
  43266. {
  43267. #ifndef HAVE_FIPS
  43268. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  43269. #else
  43270. ret = wc_InitRng(&rng);
  43271. #endif
  43272. if (ret != 0)
  43273. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43274. senderNonce[0] = 0x04;
  43275. senderNonce[1] = PKCS7_NONCE_SZ;
  43276. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  43277. if (ret != 0) {
  43278. wc_FreeRng(&rng);
  43279. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43280. }
  43281. }
  43282. for (i = 0; i < testSz; i++) {
  43283. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  43284. #ifdef WOLFSSL_ASYNC_CRYPT
  43285. INVALID_DEVID /* async PKCS7 is not supported */
  43286. #else
  43287. devId
  43288. #endif
  43289. );
  43290. if (pkcs7 == NULL) {
  43291. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  43292. }
  43293. if (testVectors[i].secretKey != NULL) {
  43294. /* KEKRI recipient type */
  43295. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  43296. if (ret != 0)
  43297. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43298. pkcs7->content = (byte*)testVectors[i].content;
  43299. pkcs7->contentSz = testVectors[i].contentSz;
  43300. pkcs7->contentOID = testVectors[i].contentOID;
  43301. pkcs7->encryptOID = testVectors[i].encryptOID;
  43302. pkcs7->ukm = testVectors[i].optionalUkm;
  43303. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  43304. pkcs7->authAttribs = testVectors[i].authAttribs;
  43305. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  43306. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  43307. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  43308. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  43309. testVectors[i].secretKey, testVectors[i].secretKeySz,
  43310. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  43311. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  43312. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  43313. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  43314. if (ret < 0) {
  43315. wc_PKCS7_Free(pkcs7);
  43316. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43317. }
  43318. /* set key, for decryption */
  43319. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  43320. testVectors[i].secretKeySz);
  43321. if (ret != 0) {
  43322. wc_PKCS7_Free(pkcs7);
  43323. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43324. }
  43325. } else if (testVectors[i].password != NULL) {
  43326. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  43327. /* PWRI recipient type */
  43328. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  43329. if (ret != 0)
  43330. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43331. pkcs7->content = (byte*)testVectors[i].content;
  43332. pkcs7->contentSz = testVectors[i].contentSz;
  43333. pkcs7->contentOID = testVectors[i].contentOID;
  43334. pkcs7->encryptOID = testVectors[i].encryptOID;
  43335. pkcs7->ukm = testVectors[i].optionalUkm;
  43336. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  43337. pkcs7->authAttribs = testVectors[i].authAttribs;
  43338. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  43339. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  43340. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  43341. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  43342. (byte*)testVectors[i].password,
  43343. testVectors[i].passwordSz, testVectors[i].salt,
  43344. testVectors[i].saltSz, testVectors[i].kdfOID,
  43345. testVectors[i].hashOID, testVectors[i].kdfIterations,
  43346. testVectors[i].kekEncryptOID, testVectors[i].pwriOptions);
  43347. if (ret < 0) {
  43348. wc_PKCS7_Free(pkcs7);
  43349. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43350. }
  43351. /* set password, for decryption */
  43352. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  43353. testVectors[i].passwordSz);
  43354. if (ret < 0) {
  43355. wc_PKCS7_Free(pkcs7);
  43356. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43357. }
  43358. #endif /* ! NO_PWDBASED && ! NO_SHA */
  43359. } else if (testVectors[i].isOri == 1) {
  43360. /* ORI recipient type */
  43361. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  43362. if (ret != 0)
  43363. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43364. pkcs7->content = (byte*)testVectors[i].content;
  43365. pkcs7->contentSz = testVectors[i].contentSz;
  43366. pkcs7->contentOID = testVectors[i].contentOID;
  43367. pkcs7->encryptOID = testVectors[i].encryptOID;
  43368. pkcs7->authAttribs = testVectors[i].authAttribs;
  43369. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  43370. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  43371. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  43372. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  43373. testVectors[i].oriOptions);
  43374. if (ret < 0) {
  43375. wc_PKCS7_Free(pkcs7);
  43376. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43377. }
  43378. /* set decrypt callback for decryption */
  43379. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  43380. if (ret < 0) {
  43381. wc_PKCS7_Free(pkcs7);
  43382. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43383. }
  43384. } else {
  43385. /* KTRI or KARI recipient types */
  43386. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  43387. (word32)testVectors[i].certSz);
  43388. if (ret != 0) {
  43389. wc_PKCS7_Free(pkcs7);
  43390. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43391. }
  43392. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  43393. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  43394. pkcs7->privateKey = testVectors[i].privateKey;
  43395. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  43396. pkcs7->content = (byte*)testVectors[i].content;
  43397. pkcs7->contentSz = testVectors[i].contentSz;
  43398. pkcs7->contentOID = testVectors[i].contentOID;
  43399. pkcs7->encryptOID = testVectors[i].encryptOID;
  43400. pkcs7->ukm = testVectors[i].optionalUkm;
  43401. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  43402. pkcs7->authAttribs = testVectors[i].authAttribs;
  43403. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  43404. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  43405. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  43406. /* set SubjectIdentifier type for KTRI types */
  43407. if (testVectors[i].ktriOptions & CMS_SKID) {
  43408. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  43409. if (ret != 0) {
  43410. wc_PKCS7_Free(pkcs7);
  43411. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43412. }
  43413. } else if (testVectors[i].ktriOptions &
  43414. CMS_ISSUER_AND_SERIAL_NUMBER) {
  43415. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  43416. CMS_ISSUER_AND_SERIAL_NUMBER);
  43417. if (ret != 0) {
  43418. wc_PKCS7_Free(pkcs7);
  43419. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43420. }
  43421. }
  43422. }
  43423. #ifdef ECC_TIMING_RESISTANT
  43424. pkcs7->rng = &rng;
  43425. #endif
  43426. /* encode envelopedData */
  43427. envelopedSz = wc_PKCS7_EncodeAuthEnvelopedData(pkcs7, enveloped,
  43428. PKCS7_BUF_SIZE);
  43429. if (envelopedSz <= 0) {
  43430. wc_PKCS7_Free(pkcs7);
  43431. ERROR_OUT(WC_TEST_RET_ENC_EC(envelopedSz), out);
  43432. }
  43433. #ifndef NO_PKCS7_STREAM
  43434. { /* test reading byte by byte */
  43435. int z;
  43436. for (z = 0; z < envelopedSz; z++) {
  43437. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7,
  43438. enveloped + z, 1, decoded, PKCS7_BUF_SIZE);
  43439. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  43440. printf("unexpected error %d\n", decodedSz);
  43441. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  43442. }
  43443. }
  43444. /* test decode result */
  43445. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  43446. printf("stream read compare failed\n");
  43447. wc_PKCS7_Free(pkcs7);
  43448. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  43449. }
  43450. }
  43451. #endif
  43452. /* decode envelopedData */
  43453. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7, enveloped,
  43454. (word32)envelopedSz, decoded,
  43455. PKCS7_BUF_SIZE);
  43456. if (decodedSz <= 0) {
  43457. wc_PKCS7_Free(pkcs7);
  43458. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  43459. }
  43460. /* test decode result */
  43461. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  43462. wc_PKCS7_Free(pkcs7);
  43463. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  43464. }
  43465. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  43466. /* output pkcs7 envelopedData for external testing */
  43467. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  43468. if (!pkcs7File) {
  43469. wc_PKCS7_Free(pkcs7);
  43470. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  43471. }
  43472. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  43473. XFCLOSE(pkcs7File);
  43474. if (ret != envelopedSz) {
  43475. wc_PKCS7_Free(pkcs7);
  43476. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  43477. } else {
  43478. /* reset ret to 0 for success */
  43479. ret = 0;
  43480. }
  43481. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  43482. wc_PKCS7_Free(pkcs7);
  43483. pkcs7 = NULL;
  43484. }
  43485. wc_FreeRng(&rng);
  43486. (void)eccCert;
  43487. (void)eccCertSz;
  43488. (void)eccPrivKey;
  43489. (void)eccPrivKeySz;
  43490. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  43491. (void)secretKey;
  43492. (void)secretKeyId;
  43493. #endif
  43494. #ifdef NO_RSA
  43495. (void)rsaCert;
  43496. (void)rsaCertSz;
  43497. (void)rsaPrivKey;
  43498. (void)rsaPrivKeySz;
  43499. #endif
  43500. out:
  43501. if (testVectors)
  43502. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43503. if (enveloped)
  43504. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43505. if (decoded)
  43506. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43507. return ret;
  43508. }
  43509. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7authenveloped_test(void)
  43510. {
  43511. wc_test_ret_t ret = 0;
  43512. byte* rsaCert = NULL;
  43513. byte* rsaPrivKey = NULL;
  43514. word32 rsaCertSz = 0;
  43515. word32 rsaPrivKeySz = 0;
  43516. byte* eccCert = NULL;
  43517. byte* eccPrivKey = NULL;
  43518. word32 eccCertSz = 0;
  43519. word32 eccPrivKeySz = 0;
  43520. WOLFSSL_ENTER("pkcs7authenveloped_test");
  43521. #ifndef NO_RSA
  43522. /* read client RSA cert and key in DER format */
  43523. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43524. if (rsaCert == NULL)
  43525. return WC_TEST_RET_ENC_ERRNO;
  43526. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43527. if (rsaPrivKey == NULL) {
  43528. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43529. return WC_TEST_RET_ENC_NC;
  43530. }
  43531. rsaCertSz = FOURK_BUF;
  43532. rsaPrivKeySz = FOURK_BUF;
  43533. #endif /* NO_RSA */
  43534. #ifdef HAVE_ECC
  43535. /* read client ECC cert and key in DER format */
  43536. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43537. if (eccCert == NULL) {
  43538. #ifndef NO_RSA
  43539. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43540. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43541. #endif
  43542. return WC_TEST_RET_ENC_NC;
  43543. }
  43544. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43545. if (eccPrivKey == NULL) {
  43546. #ifndef NO_RSA
  43547. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43548. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43549. #endif
  43550. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43551. return WC_TEST_RET_ENC_NC;
  43552. }
  43553. eccCertSz = FOURK_BUF;
  43554. eccPrivKeySz = FOURK_BUF;
  43555. #endif /* HAVE_ECC */
  43556. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  43557. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  43558. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  43559. eccPrivKey, &eccPrivKeySz);
  43560. if (ret < 0) {
  43561. #ifndef NO_RSA
  43562. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43563. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43564. #endif
  43565. #ifdef HAVE_ECC
  43566. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43567. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43568. #endif
  43569. return WC_TEST_RET_ENC_EC(ret);
  43570. }
  43571. ret = pkcs7authenveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  43572. rsaPrivKey, (word32)rsaPrivKeySz,
  43573. eccCert, (word32)eccCertSz,
  43574. eccPrivKey, (word32)eccPrivKeySz);
  43575. #ifndef NO_RSA
  43576. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43577. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43578. #endif
  43579. #ifdef HAVE_ECC
  43580. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43581. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43582. #endif
  43583. return ret;
  43584. }
  43585. #endif /* HAVE_AESGCM || HAVE_AESCCM */
  43586. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  43587. static const byte p7DefKey[] = {
  43588. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  43589. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  43590. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  43591. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  43592. };
  43593. static const byte p7AltKey[] = {
  43594. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  43595. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  43596. };
  43597. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  43598. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  43599. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  43600. {
  43601. wc_test_ret_t ret;
  43602. if (cek == NULL || out == NULL)
  43603. return BAD_FUNC_ARG;
  43604. /* test case sanity checks */
  43605. if (keyIdSz != 1) {
  43606. return WC_TEST_RET_ENC_NC;
  43607. }
  43608. if (keyId[0] != 0x00) {
  43609. return WC_TEST_RET_ENC_NC;
  43610. }
  43611. if (type != (int)PKCS7_KEKRI) {
  43612. return WC_TEST_RET_ENC_NC;
  43613. }
  43614. switch (keyWrapAlgo) {
  43615. case AES256_WRAP:
  43616. ret = wc_AesKeyUnWrap(p7DefKey, sizeof(p7DefKey), cek, cekSz,
  43617. out, outSz, NULL);
  43618. if (ret <= 0)
  43619. return (int)ret;
  43620. break;
  43621. default:
  43622. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  43623. return BAD_KEYWRAP_ALG_E;
  43624. };
  43625. (void)pkcs7;
  43626. (void)direction;
  43627. (void)orginKey; /* used with KAKRI */
  43628. (void)orginKeySz;
  43629. return (int)ret;
  43630. }
  43631. /* returns key size on success */
  43632. static wc_test_ret_t getFirmwareKey(PKCS7* pkcs7, byte* key, word32 keySz)
  43633. {
  43634. wc_test_ret_t ret;
  43635. word32 atrSz;
  43636. byte atr[256];
  43637. /* Additionally can look for fwWrappedFirmwareKey
  43638. * 1.2.840.113529.1.9.16.1.16 */
  43639. const unsigned char fwWrappedFirmwareKey[] = {
  43640. /* 0x06, 0x0B */
  43641. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  43642. 0x01, 0x09, 0x10, 0x02, 0x27
  43643. };
  43644. /* find keyID in fwWrappedFirmwareKey */
  43645. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  43646. sizeof(fwWrappedFirmwareKey), NULL, &atrSz);
  43647. if (ret == LENGTH_ONLY_E) {
  43648. XMEMSET(atr, 0, sizeof(atr));
  43649. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  43650. sizeof(fwWrappedFirmwareKey), atr, &atrSz);
  43651. /* keyIdRaw[0] OCTET TAG */
  43652. /* keyIdRaw[1] Length */
  43653. if (ret > 0) {
  43654. PKCS7* envPkcs7;
  43655. envPkcs7 = wc_PKCS7_New(NULL, 0);
  43656. if (envPkcs7 == NULL) {
  43657. return MEMORY_E;
  43658. }
  43659. wc_PKCS7_Init(envPkcs7, NULL, 0);
  43660. ret = wc_PKCS7_SetWrapCEKCb(envPkcs7, myCEKwrapFunc);
  43661. if (ret == 0) {
  43662. /* expecting FIRMWARE_PKG_DATA content */
  43663. envPkcs7->contentOID = FIRMWARE_PKG_DATA;
  43664. ret = wc_PKCS7_DecodeEnvelopedData(envPkcs7, atr, atrSz,
  43665. key, keySz);
  43666. if (envPkcs7->contentOID != FIRMWARE_PKG_DATA) {
  43667. /* the contentOID should have been set to the inner
  43668. * FIRMWARE_PKG_DATA content */
  43669. ret = BAD_STATE_E;
  43670. }
  43671. }
  43672. wc_PKCS7_Free(envPkcs7);
  43673. }
  43674. }
  43675. return ret;
  43676. }
  43677. /* create a KEKRI enveloped data
  43678. * return size on success */
  43679. static wc_test_ret_t envelopedData_encrypt(byte* in, word32 inSz, byte* out,
  43680. word32 outSz)
  43681. {
  43682. wc_test_ret_t ret;
  43683. PKCS7* pkcs7;
  43684. WOLFSSL_SMALL_STACK_STATIC const byte keyId[] = { 0x00 };
  43685. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  43686. if (pkcs7 == NULL)
  43687. return WC_TEST_RET_ENC_ERRNO;
  43688. pkcs7->content = in;
  43689. pkcs7->contentSz = inSz;
  43690. pkcs7->contentOID = FIRMWARE_PKG_DATA;
  43691. pkcs7->encryptOID = AES256CBCb;
  43692. pkcs7->ukm = NULL;
  43693. pkcs7->ukmSz = 0;
  43694. /* add recipient (KEKRI type) */
  43695. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP, (byte*)p7DefKey,
  43696. sizeof(p7DefKey), (byte*)keyId,
  43697. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0);
  43698. if (ret < 0) {
  43699. printf("wc_PKCS7_AddRecipient_KEKRI() failed\n");
  43700. wc_PKCS7_Free(pkcs7);
  43701. return WC_TEST_RET_ENC_EC(ret);
  43702. }
  43703. /* encode envelopedData, returns size */
  43704. ret = wc_PKCS7_EncodeEnvelopedData(pkcs7, out, outSz);
  43705. if (ret <= 0) {
  43706. printf("wc_PKCS7_EncodeEnvelopedData() failed\n");
  43707. wc_PKCS7_Free(pkcs7);
  43708. return WC_TEST_RET_ENC_EC(ret);
  43709. }
  43710. wc_PKCS7_Free(pkcs7);
  43711. return ret;
  43712. }
  43713. /*
  43714. * keyHint is the KeyID to be set in the fwDecryptKeyID attribute
  43715. * returns size of buffer output on success
  43716. */
  43717. static wc_test_ret_t generateBundle(byte* out, word32 *outSz, const byte* encryptKey,
  43718. word32 encryptKeySz, byte keyHint, byte* cert, word32 certSz,
  43719. byte* key, word32 keySz)
  43720. {
  43721. wc_test_ret_t ret;
  43722. int attribNum = 1;
  43723. PKCS7* pkcs7;
  43724. /* KEY ID
  43725. * fwDecryptKeyID OID 1.2.840.113549.1.9.16.2.37
  43726. */
  43727. const unsigned char fwDecryptKeyID[] = {
  43728. 0x06, 0x0B,
  43729. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  43730. 0x01, 0x09, 0x10, 0x02, 0x25
  43731. };
  43732. /* fwWrappedFirmwareKey 1.2.840.113529.1.9.16.1.16 */
  43733. const unsigned char fwWrappedFirmwareKey[] = {
  43734. 0x06, 0x0B, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  43735. 0x01, 0x09, 0x10, 0x02, 0x27
  43736. };
  43737. byte keyID[] = { 0x04, 0x01, 0x00 };
  43738. byte env[256];
  43739. char data[] = "Test of wolfSSL PKCS7 decrypt callback";
  43740. PKCS7Attrib attribs[] =
  43741. {
  43742. { fwDecryptKeyID, sizeof(fwDecryptKeyID), keyID, sizeof(keyID) },
  43743. { fwWrappedFirmwareKey, sizeof(fwWrappedFirmwareKey), env, 0 }
  43744. };
  43745. keyID[2] = keyHint;
  43746. /* If using keyHint 0 then create a bundle with fwWrappedFirmwareKey */
  43747. if (keyHint == 0) {
  43748. ret = envelopedData_encrypt((byte*)p7DefKey, sizeof(p7DefKey), env,
  43749. sizeof(env));
  43750. if (ret <= 0) {
  43751. return ret;
  43752. }
  43753. attribs[1].valueSz = (word32)ret;
  43754. attribNum++;
  43755. }
  43756. /* init PKCS7 */
  43757. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  43758. if (pkcs7 == NULL)
  43759. return WC_TEST_RET_ENC_ERRNO;
  43760. ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
  43761. if (ret != 0) {
  43762. printf("ERROR: wc_PKCS7_InitWithCert() failed, ret = %d\n", ret);
  43763. wc_PKCS7_Free(pkcs7);
  43764. return WC_TEST_RET_ENC_EC(ret);
  43765. }
  43766. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  43767. if (ret != 0) {
  43768. wc_PKCS7_Free(pkcs7);
  43769. return WC_TEST_RET_ENC_EC(ret);
  43770. }
  43771. /* encode Signed Encrypted FirmwarePkgData */
  43772. if (encryptKeySz == 16) {
  43773. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  43774. encryptKeySz, key, keySz, AES128CBCb, RSAk, SHA256h,
  43775. (byte*)data, sizeof(data), NULL, 0,
  43776. attribs, (word32)attribNum, out, *outSz);
  43777. }
  43778. else {
  43779. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  43780. encryptKeySz, key, keySz, AES256CBCb, RSAk, SHA256h,
  43781. (byte*)data, sizeof(data), NULL, 0,
  43782. attribs, (word32)attribNum, out, *outSz);
  43783. }
  43784. if (ret <= 0) {
  43785. printf("ERROR: wc_PKCS7_EncodeSignedEncryptedFPD() failed, "
  43786. "ret = %d\n", ret);
  43787. wc_PKCS7_Free(pkcs7);
  43788. return WC_TEST_RET_ENC_EC(ret);
  43789. } else {
  43790. *outSz = (word32)ret;
  43791. }
  43792. wc_PKCS7_Free(pkcs7);
  43793. return ret;
  43794. }
  43795. /* test verification and decryption of PKCS7 bundle
  43796. * return 0 on success
  43797. */
  43798. static wc_test_ret_t verifyBundle(byte* derBuf, word32 derSz, int keyHint)
  43799. {
  43800. wc_test_ret_t ret = 0;
  43801. int usrCtx = 1; /* test value to pass as user context to callback */
  43802. PKCS7* pkcs7 = NULL;
  43803. byte* sid = NULL;
  43804. word32 sidSz;
  43805. byte key[256];
  43806. word32 keySz = sizeof(key);
  43807. byte *decoded = NULL;
  43808. int decodedSz = FOURK_BUF/2;
  43809. WOLFSSL_SMALL_STACK_STATIC const byte expectedSid[] = {
  43810. #ifdef NO_SHA
  43811. #ifdef USE_CERT_BUFFERS_1024
  43812. 0x70, 0xe7, 0x79, 0x60, 0x8f, 0x41, 0xdc, 0xe9,
  43813. 0xad, 0x8b, 0x3d, 0x0c, 0x20, 0xf4, 0xc3, 0xf2,
  43814. 0x8e, 0x05, 0xe8, 0xa1, 0xb6, 0x68, 0x74, 0x06,
  43815. 0xbc, 0xe7, 0xc5, 0x3c, 0x13, 0x99, 0x79, 0xb9
  43816. #else
  43817. 0xce, 0x06, 0x07, 0xbe, 0xf1, 0xa6, 0x1e, 0x36,
  43818. 0xef, 0xfa, 0xbc, 0x89, 0x71, 0xf3, 0x23, 0x9e,
  43819. 0x34, 0x6d, 0xae, 0x86, 0xae, 0x2b, 0xdc, 0xf4,
  43820. 0x4a, 0x27, 0xd5, 0x63, 0x59, 0x4f, 0x4a, 0x71
  43821. #endif
  43822. #else /* !NO_SHA */
  43823. #ifdef USE_CERT_BUFFERS_1024
  43824. 0x81, 0x69, 0x0f, 0xf8, 0xdf, 0xdd, 0xcf, 0x34,
  43825. 0x29, 0xd5, 0x67, 0x75, 0x71, 0x85, 0xc7, 0x75,
  43826. 0x10, 0x69, 0x59, 0xec,
  43827. #else
  43828. 0x33, 0xD8, 0x45, 0x66, 0xD7, 0x68, 0x87, 0x18,
  43829. 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26,
  43830. 0xD7, 0x85, 0x65, 0xC0
  43831. #endif
  43832. #endif /* !NO_SHA */
  43833. };
  43834. decoded = (byte *)XMALLOC(decodedSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43835. if (decoded == NULL) {
  43836. ret = MEMORY_E;
  43837. goto out;
  43838. }
  43839. pkcs7 = wc_PKCS7_New(HEAP_HINT, INVALID_DEVID);
  43840. if (pkcs7 == NULL) {
  43841. ret = MEMORY_E;
  43842. goto out;
  43843. }
  43844. /* Test verify */
  43845. ret = wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID);
  43846. if (ret != 0)
  43847. goto out;
  43848. ret = wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  43849. if (ret != 0)
  43850. goto out;
  43851. ret = wc_PKCS7_VerifySignedData(pkcs7, derBuf, derSz);
  43852. if (ret != 0)
  43853. goto out;
  43854. /* Get size of SID and print it out */
  43855. ret = wc_PKCS7_GetSignerSID(pkcs7, NULL, &sidSz);
  43856. if (ret != LENGTH_ONLY_E)
  43857. goto out;
  43858. sid = (byte*)XMALLOC(sidSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43859. if (sid == NULL) {
  43860. ret = MEMORY_E;
  43861. goto out;
  43862. }
  43863. ret = wc_PKCS7_GetSignerSID(pkcs7, sid, &sidSz);
  43864. if (ret != 0)
  43865. goto out;
  43866. ret = XMEMCMP(sid, expectedSid, sidSz);
  43867. if (ret != 0) {
  43868. ret = PKCS7_NO_SIGNER_E; /* close enough */
  43869. goto out;
  43870. }
  43871. /* get expected fwWrappedFirmwareKey */
  43872. if (keyHint == 0) {
  43873. ret = getFirmwareKey(pkcs7, key, keySz);
  43874. if (ret < 0)
  43875. goto out;
  43876. pkcs7->encryptionKey = key;
  43877. pkcs7->encryptionKeySz = (word32)ret;
  43878. }
  43879. else {
  43880. decodedSz = PKCS7_BUF_SIZE;
  43881. ret = wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc);
  43882. if (ret != 0)
  43883. goto out;
  43884. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)&usrCtx);
  43885. if (ret != 0)
  43886. goto out;
  43887. }
  43888. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  43889. pkcs7->contentSz, decoded, (word32)decodedSz);
  43890. if (decodedSz < 0) {
  43891. ret = decodedSz;
  43892. goto out;
  43893. }
  43894. ret = 0;
  43895. out:
  43896. if (decoded)
  43897. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43898. if (pkcs7)
  43899. wc_PKCS7_Free(pkcs7);
  43900. if (sid)
  43901. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43902. return ret;
  43903. }
  43904. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7callback_test(byte* cert, word32 certSz, byte* key, word32 keySz)
  43905. {
  43906. wc_test_ret_t ret = 0;
  43907. word32 derSz;
  43908. byte *derBuf = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43909. WOLFSSL_ENTER("pkcs7callback_test");
  43910. if (! derBuf)
  43911. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  43912. /* Doing default generation and verify */
  43913. derSz = FOURK_BUF;
  43914. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 0, cert,
  43915. certSz, key, keySz);
  43916. if (ret <= 0) {
  43917. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  43918. }
  43919. ret = verifyBundle(derBuf, derSz, 0);
  43920. if (ret != 0)
  43921. ERROR_OUT(ret, out);
  43922. /* test choosing other key with keyID */
  43923. derSz = FOURK_BUF;
  43924. ret = generateBundle(derBuf, &derSz, p7AltKey, sizeof(p7AltKey), 1,
  43925. cert, certSz, key, keySz);
  43926. if (ret <= 0) {
  43927. ERROR_OUT(ret, out);
  43928. }
  43929. ret = verifyBundle(derBuf, derSz, 1);
  43930. if (ret != 0)
  43931. ERROR_OUT(ret, out);
  43932. /* test fail case with wrong keyID */
  43933. derSz = FOURK_BUF;
  43934. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 1,
  43935. cert, certSz, key, keySz);
  43936. if (ret <= 0) {
  43937. ERROR_OUT(ret, out);
  43938. }
  43939. ret = verifyBundle(derBuf, derSz, 1);
  43940. if (ret == 0) {
  43941. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  43942. }
  43943. ret = 0;
  43944. out:
  43945. if (derBuf)
  43946. XFREE(derBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43947. return ret;
  43948. }
  43949. #endif /* !NO_AES && HAVE_AES_CBC */
  43950. #ifndef NO_PKCS7_ENCRYPTED_DATA
  43951. typedef struct {
  43952. const byte* content;
  43953. word32 contentSz;
  43954. int contentOID;
  43955. int encryptOID;
  43956. byte* encryptionKey;
  43957. word32 encryptionKeySz;
  43958. PKCS7Attrib* attribs;
  43959. word32 attribsSz;
  43960. const char* outFileName;
  43961. } pkcs7EncryptedVector;
  43962. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7encrypted_test(void)
  43963. {
  43964. wc_test_ret_t ret = 0;
  43965. int i, testSz;
  43966. int encryptedSz, decodedSz, attribIdx;
  43967. PKCS7* pkcs7;
  43968. byte *encrypted;
  43969. byte *decoded;
  43970. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  43971. XFILE pkcs7File;
  43972. #endif
  43973. PKCS7Attrib* expectedAttrib;
  43974. PKCS7DecodedAttrib* decodedAttrib;
  43975. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  43976. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  43977. 0x72,0x6c,0x64
  43978. };
  43979. #ifndef NO_DES3
  43980. byte desKey[] = {
  43981. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  43982. };
  43983. byte des3Key[] = {
  43984. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  43985. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  43986. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  43987. };
  43988. #endif
  43989. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  43990. #ifdef WOLFSSL_AES_128
  43991. byte aes128Key[] = {
  43992. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  43993. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  43994. };
  43995. #endif
  43996. #ifdef WOLFSSL_AES_192
  43997. byte aes192Key[] = {
  43998. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  43999. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  44000. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  44001. };
  44002. #endif
  44003. #ifdef WOLFSSL_AES_256
  44004. byte aes256Key[] = {
  44005. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  44006. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  44007. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  44008. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  44009. };
  44010. #endif
  44011. #ifdef WOLFSSL_AES_256
  44012. /* Attribute example from RFC 4134, Section 7.2
  44013. * OID = 1.2.5555
  44014. * OCTET STRING = 'This is a test General ASN Attribute, number 1.' */
  44015. static const byte genAttrOid[] = { 0x06, 0x03, 0x2a, 0xab, 0x33 };
  44016. static const byte genAttr[] = { 0x04, 47,
  44017. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  44018. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  44019. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  44020. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  44021. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  44022. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x31, 0x2e };
  44023. static const byte genAttrOid2[] = { 0x06, 0x03, 0x2a, 0xab, 0x34 };
  44024. static const byte genAttr2[] = { 0x04, 47,
  44025. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  44026. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  44027. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  44028. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  44029. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  44030. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x32, 0x2e };
  44031. PKCS7Attrib attribs[] =
  44032. {
  44033. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) }
  44034. };
  44035. PKCS7Attrib multiAttribs[] =
  44036. {
  44037. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) },
  44038. { genAttrOid2, sizeof(genAttrOid2), genAttr2, sizeof(genAttr2) }
  44039. };
  44040. #endif
  44041. #endif /* NO_AES */
  44042. const pkcs7EncryptedVector testVectors[] =
  44043. {
  44044. #ifndef NO_DES3
  44045. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key),
  44046. NULL, 0, "pkcs7encryptedDataDES3.der"},
  44047. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey),
  44048. NULL, 0, "pkcs7encryptedDataDES.der"},
  44049. #endif /* NO_DES3 */
  44050. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  44051. #ifdef WOLFSSL_AES_128
  44052. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  44053. sizeof(aes128Key), NULL, 0, "pkcs7encryptedDataAES128CBC.der"},
  44054. #endif
  44055. #ifdef WOLFSSL_AES_192
  44056. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  44057. sizeof(aes192Key), NULL, 0, "pkcs7encryptedDataAES192CBC.der"},
  44058. #endif
  44059. #ifdef WOLFSSL_AES_256
  44060. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  44061. sizeof(aes256Key), NULL, 0, "pkcs7encryptedDataAES256CBC.der"},
  44062. /* test with optional unprotected attributes */
  44063. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  44064. sizeof(aes256Key), attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44065. "pkcs7encryptedDataAES256CBC_attribs.der"},
  44066. /* test with multiple optional unprotected attributes */
  44067. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  44068. sizeof(aes256Key), multiAttribs,
  44069. (sizeof(multiAttribs)/sizeof(PKCS7Attrib)),
  44070. "pkcs7encryptedDataAES256CBC_multi_attribs.der"},
  44071. /* test with contentType set to FirmwarePkgData */
  44072. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256CBCb, aes256Key,
  44073. sizeof(aes256Key), NULL, 0,
  44074. "pkcs7encryptedDataAES256CBC_firmwarePkgData.der"},
  44075. #endif
  44076. #endif /* !NO_AES && HAVE_AES_CBC */
  44077. };
  44078. WOLFSSL_ENTER("pkcs7encrypted_test");
  44079. encrypted = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44080. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44081. if ((! encrypted) || (! decoded)) {
  44082. ERROR_OUT(MEMORY_E, out);
  44083. }
  44084. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  44085. for (i = 0; i < testSz; i++) {
  44086. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  44087. if (pkcs7 == NULL) {
  44088. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  44089. }
  44090. pkcs7->content = (byte*)testVectors[i].content;
  44091. pkcs7->contentSz = testVectors[i].contentSz;
  44092. pkcs7->contentOID = testVectors[i].contentOID;
  44093. pkcs7->encryptOID = testVectors[i].encryptOID;
  44094. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  44095. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  44096. pkcs7->unprotectedAttribs = testVectors[i].attribs;
  44097. pkcs7->unprotectedAttribsSz = testVectors[i].attribsSz;
  44098. /* encode encryptedData */
  44099. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  44100. PKCS7_BUF_SIZE);
  44101. if (encryptedSz <= 0) {
  44102. wc_PKCS7_Free(pkcs7);
  44103. ERROR_OUT(WC_TEST_RET_ENC_EC(encryptedSz), out);
  44104. }
  44105. /* decode encryptedData */
  44106. #ifndef NO_PKCS7_STREAM
  44107. { /* test reading byte by byte */
  44108. int z;
  44109. for (z = 0; z < encryptedSz; z++) {
  44110. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted + z, 1,
  44111. decoded, PKCS7_BUF_SIZE);
  44112. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  44113. printf("unexpected error %d\n", decodedSz);
  44114. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  44115. }
  44116. }
  44117. /* test decode result */
  44118. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  44119. printf("stream read failed\n");
  44120. wc_PKCS7_Free(pkcs7);
  44121. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  44122. }
  44123. }
  44124. #endif
  44125. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  44126. decoded, PKCS7_BUF_SIZE);
  44127. if (decodedSz <= 0){
  44128. wc_PKCS7_Free(pkcs7);
  44129. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  44130. }
  44131. /* test decode result */
  44132. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  44133. wc_PKCS7_Free(pkcs7);
  44134. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  44135. }
  44136. /* verify decoded unprotected attributes */
  44137. if (pkcs7->decodedAttrib != NULL) {
  44138. decodedAttrib = pkcs7->decodedAttrib;
  44139. attribIdx = 1;
  44140. while (decodedAttrib != NULL) {
  44141. /* expected attribute, stored list is reversed */
  44142. expectedAttrib = &(pkcs7->unprotectedAttribs
  44143. [pkcs7->unprotectedAttribsSz - attribIdx]);
  44144. /* verify oid */
  44145. if (XMEMCMP(decodedAttrib->oid, expectedAttrib->oid,
  44146. decodedAttrib->oidSz) != 0) {
  44147. wc_PKCS7_Free(pkcs7);
  44148. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  44149. }
  44150. /* verify value */
  44151. if (XMEMCMP(decodedAttrib->value, expectedAttrib->value,
  44152. decodedAttrib->valueSz) != 0) {
  44153. wc_PKCS7_Free(pkcs7);
  44154. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  44155. }
  44156. decodedAttrib = decodedAttrib->next;
  44157. attribIdx++;
  44158. }
  44159. }
  44160. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  44161. /* output pkcs7 envelopedData for external testing */
  44162. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  44163. if (!pkcs7File) {
  44164. wc_PKCS7_Free(pkcs7);
  44165. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  44166. }
  44167. ret = (int)XFWRITE(encrypted, encryptedSz, 1, pkcs7File);
  44168. if (ret < 0)
  44169. ret = WC_TEST_RET_ENC_ERRNO;
  44170. else
  44171. ret = 0;
  44172. XFCLOSE(pkcs7File);
  44173. #endif
  44174. wc_PKCS7_Free(pkcs7);
  44175. }
  44176. out:
  44177. if (encrypted)
  44178. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44179. if (decoded)
  44180. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44181. return ret;
  44182. }
  44183. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  44184. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  44185. typedef struct {
  44186. const byte* content;
  44187. word32 contentSz;
  44188. int contentOID;
  44189. const char* outFileName;
  44190. } pkcs7CompressedVector;
  44191. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7compressed_test(void)
  44192. {
  44193. wc_test_ret_t ret = 0;
  44194. int i, testSz;
  44195. int compressedSz, decodedSz;
  44196. PKCS7* pkcs7;
  44197. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  44198. byte *compressed;
  44199. byte *decoded;
  44200. #else
  44201. byte compressed[PKCS7_BUF_SIZE];
  44202. byte decoded[PKCS7_BUF_SIZE];
  44203. #endif
  44204. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  44205. XFILE pkcs7File;
  44206. #endif
  44207. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  44208. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  44209. 0x72,0x6c,0x64
  44210. };
  44211. const pkcs7CompressedVector testVectors[] =
  44212. {
  44213. {data, (word32)sizeof(data), DATA,
  44214. "pkcs7compressedData_data_zlib.der"},
  44215. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA,
  44216. "pkcs7compressedData_firmwarePkgData_zlib.der"},
  44217. };
  44218. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  44219. compressed = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44220. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44221. if ((! compressed) || (! decoded)) {
  44222. ERROR_OUT(MEMORY_E, out);
  44223. }
  44224. #endif
  44225. WOLFSSL_ENTER("pkcs7compressed_test");
  44226. testSz = sizeof(testVectors) / sizeof(pkcs7CompressedVector);
  44227. for (i = 0; i < testSz; i++) {
  44228. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  44229. if (pkcs7 == NULL) {
  44230. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  44231. }
  44232. pkcs7->content = (byte*)testVectors[i].content;
  44233. pkcs7->contentSz = testVectors[i].contentSz;
  44234. pkcs7->contentOID = testVectors[i].contentOID;
  44235. /* encode compressedData */
  44236. compressedSz = wc_PKCS7_EncodeCompressedData(pkcs7, compressed,
  44237. PKCS7_BUF_SIZE);
  44238. if (compressedSz <= 0) {
  44239. wc_PKCS7_Free(pkcs7);
  44240. ERROR_OUT(WC_TEST_RET_ENC_EC(compressedSz), out);
  44241. }
  44242. /* decode compressedData */
  44243. decodedSz = wc_PKCS7_DecodeCompressedData(pkcs7, compressed,
  44244. compressedSz, decoded,
  44245. PKCS7_BUF_SIZE);
  44246. if (decodedSz <= 0){
  44247. wc_PKCS7_Free(pkcs7);
  44248. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  44249. }
  44250. /* test decode result */
  44251. if (XMEMCMP(decoded, testVectors[i].content,
  44252. testVectors[i].contentSz) != 0) {
  44253. wc_PKCS7_Free(pkcs7);
  44254. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  44255. }
  44256. /* make sure content type is the same */
  44257. if (testVectors[i].contentOID != pkcs7->contentOID) {
  44258. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  44259. }
  44260. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  44261. /* output pkcs7 compressedData for external testing */
  44262. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  44263. if (!pkcs7File) {
  44264. wc_PKCS7_Free(pkcs7);
  44265. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  44266. }
  44267. ret = (int)XFWRITE(compressed, compressedSz, 1, pkcs7File);
  44268. if (ret < 0)
  44269. ret = WC_TEST_RET_ENC_ERRNO;
  44270. else
  44271. ret = 0;
  44272. XFCLOSE(pkcs7File);
  44273. #endif
  44274. wc_PKCS7_Free(pkcs7);
  44275. }
  44276. out:
  44277. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  44278. if (compressed)
  44279. XFREE(compressed, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44280. if (decoded)
  44281. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44282. #endif
  44283. return ret;
  44284. } /* pkcs7compressed_test() */
  44285. #undef PKCS7_BUF_SIZE
  44286. #endif /* HAVE_LIBZ */
  44287. typedef struct {
  44288. const byte* content;
  44289. word32 contentSz;
  44290. int hashOID;
  44291. int signOID;
  44292. byte* privateKey;
  44293. word32 privateKeySz;
  44294. byte* cert;
  44295. size_t certSz;
  44296. byte* caCert;
  44297. size_t caCertSz;
  44298. PKCS7Attrib* signedAttribs;
  44299. word32 signedAttribsSz;
  44300. const char* outFileName;
  44301. int contentOID;
  44302. const byte* contentType;
  44303. word32 contentTypeSz;
  44304. int sidType;
  44305. int encryptOID; /* for single-shot encrypt alg OID */
  44306. int encCompFlag; /* for single-shot. 1 = enc, 2 = comp, 3 = both*/
  44307. const byte* encryptKey; /* for single-shot, encryptedData */
  44308. word32 encryptKeySz; /* for single-shot, encryptedData */
  44309. PKCS7Attrib* unprotectedAttribs; /* for single-shot, encryptedData */
  44310. word32 unprotectedAttribsSz; /* for single-shot, encryptedData */
  44311. word16 detachedSignature; /* generate detached signature (0:1) */
  44312. } pkcs7SignedVector;
  44313. static wc_test_ret_t pkcs7signed_run_vectors(
  44314. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  44315. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  44316. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  44317. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  44318. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  44319. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  44320. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  44321. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  44322. {
  44323. wc_test_ret_t ret;
  44324. int testSz = 0, i;
  44325. int encodedSz;
  44326. byte* out = NULL;
  44327. word32 outSz;
  44328. WC_RNG rng;
  44329. PKCS7* pkcs7 = NULL;
  44330. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  44331. XFILE file;
  44332. #endif
  44333. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  44334. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  44335. 0x72,0x6c,0x64
  44336. };
  44337. static const byte transIdOid[] =
  44338. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  44339. 0x09, 0x07 };
  44340. static const byte messageTypeOid[] =
  44341. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  44342. 0x09, 0x02 };
  44343. static const byte senderNonceOid[] =
  44344. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  44345. 0x09, 0x05 };
  44346. #ifndef NO_SHA
  44347. byte transId[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  44348. #else
  44349. byte transId[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  44350. #endif
  44351. static const byte messageType[] = { 0x13, 2, '1', '9' };
  44352. byte senderNonce[PKCS7_NONCE_SZ + 2];
  44353. PKCS7Attrib attribs[] =
  44354. {
  44355. { transIdOid, sizeof(transIdOid), transId,
  44356. sizeof(transId) - 1 }, /* take off the null */
  44357. { messageTypeOid, sizeof(messageTypeOid), messageType,
  44358. sizeof(messageType) },
  44359. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  44360. sizeof(senderNonce) }
  44361. };
  44362. /* for testing custom contentType, FirmwarePkgData */
  44363. static const byte customContentType[] = { 0x06, 0x0B, 0x2A, 0x86,
  44364. 0x48, 0x86, 0xF7, 0x0D,
  44365. 0x01, 0x09, 0x10, 0x01, 0x10 };
  44366. #define MAX_TESTVECTORS_LEN 20
  44367. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  44368. const pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  44369. if (testSz == MAX_TESTVECTORS_LEN) { \
  44370. ret = WC_TEST_RET_ENC_NC; \
  44371. goto out; \
  44372. } \
  44373. XMEMCPY(&testVectors[testSz++], &_this_vector, \
  44374. sizeof _this_vector); \
  44375. }
  44376. pkcs7SignedVector *testVectors = NULL;
  44377. XMEMSET(&rng, 0, sizeof(rng));
  44378. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  44379. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44380. if (testVectors == NULL) {
  44381. ret = WC_TEST_RET_ENC_ERRNO;
  44382. goto out;
  44383. }
  44384. {
  44385. #ifndef NO_RSA
  44386. #ifndef NO_SHA
  44387. /* RSA with SHA */
  44388. ADD_PKCS7SIGNEDVECTOR(
  44389. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  44390. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44391. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44392. "pkcs7signedData_RSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0, NULL,
  44393. 0, 0);
  44394. /* RSA with SHA, no signed attributes */
  44395. ADD_PKCS7SIGNEDVECTOR(
  44396. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  44397. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz,
  44398. NULL, 0, NULL, 0,
  44399. "pkcs7signedData_RSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  44400. NULL, 0, 0);
  44401. #endif
  44402. #ifdef WOLFSSL_SHA224
  44403. /* RSA with SHA224 */
  44404. ADD_PKCS7SIGNEDVECTOR(
  44405. data, (word32)sizeof(data), SHA224h, RSAk, rsaClientPrivKeyBuf,
  44406. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44407. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44408. "pkcs7signedData_RSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  44409. NULL, 0, 0);
  44410. #endif
  44411. #ifndef NO_SHA256
  44412. /* RSA with SHA256 */
  44413. ADD_PKCS7SIGNEDVECTOR(
  44414. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  44415. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44416. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44417. "pkcs7signedData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  44418. NULL, 0, 0);
  44419. /* RSA with SHA256, detached signature */
  44420. ADD_PKCS7SIGNEDVECTOR(
  44421. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  44422. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44423. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44424. "pkcs7signedData_RSA_SHA256_detachedSig.der", 0, NULL, 0, 0, 0, 0,
  44425. NULL, 0, NULL, 0, 1);
  44426. /* RSA with SHA256 and SubjectKeyIdentifier in SignerIdentifier */
  44427. ADD_PKCS7SIGNEDVECTOR(
  44428. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  44429. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44430. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44431. "pkcs7signedData_RSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  44432. NULL, 0, NULL, 0, 0);
  44433. /* RSA with SHA256 and custom contentType */
  44434. ADD_PKCS7SIGNEDVECTOR(
  44435. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  44436. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44437. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44438. "pkcs7signedData_RSA_SHA256_custom_contentType.der", 0,
  44439. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  44440. NULL, 0, 0);
  44441. /* RSA with SHA256 and FirmwarePkgData contentType */
  44442. ADD_PKCS7SIGNEDVECTOR(
  44443. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  44444. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44445. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44446. "pkcs7signedData_RSA_SHA256_firmwarePkgData.der",
  44447. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  44448. /* RSA with SHA256 using server cert and ca cert */
  44449. ADD_PKCS7SIGNEDVECTOR(
  44450. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  44451. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  44452. rsaCaCertBuf, rsaCaCertBufSz,
  44453. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44454. "pkcs7signedData_RSA_SHA256_with_ca_cert.der", 0, NULL, 0, 0, 0, 0,
  44455. NULL, 0, NULL, 0, 0);
  44456. #endif
  44457. #if defined(WOLFSSL_SHA384)
  44458. /* RSA with SHA384 */
  44459. ADD_PKCS7SIGNEDVECTOR(
  44460. data, (word32)sizeof(data), SHA384h, RSAk, rsaClientPrivKeyBuf,
  44461. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44462. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44463. "pkcs7signedData_RSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  44464. NULL, 0, 0);
  44465. #endif
  44466. #if defined(WOLFSSL_SHA512)
  44467. /* RSA with SHA512 */
  44468. ADD_PKCS7SIGNEDVECTOR(
  44469. data, (word32)sizeof(data), SHA512h, RSAk, rsaClientPrivKeyBuf,
  44470. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44471. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44472. "pkcs7signedData_RSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  44473. NULL, 0, 0);
  44474. #endif
  44475. #endif /* NO_RSA */
  44476. #ifdef HAVE_ECC
  44477. #ifndef NO_SHA
  44478. /* ECDSA with SHA */
  44479. ADD_PKCS7SIGNEDVECTOR(
  44480. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  44481. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44482. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44483. "pkcs7signedData_ECDSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  44484. NULL, 0, 0);
  44485. /* ECDSA with SHA, no signed attributes */
  44486. ADD_PKCS7SIGNEDVECTOR(
  44487. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  44488. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz,
  44489. NULL, 0, NULL, 0,
  44490. "pkcs7signedData_ECDSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  44491. NULL, 0, 0);
  44492. #endif
  44493. #ifdef WOLFSSL_SHA224
  44494. /* ECDSA with SHA224 */
  44495. ADD_PKCS7SIGNEDVECTOR(
  44496. data, (word32)sizeof(data), SHA224h, ECDSAk, eccClientPrivKeyBuf,
  44497. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44498. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44499. "pkcs7signedData_ECDSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  44500. NULL, 0, 0);
  44501. #endif
  44502. #ifndef NO_SHA256
  44503. /* ECDSA with SHA256 */
  44504. ADD_PKCS7SIGNEDVECTOR(
  44505. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  44506. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44507. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44508. "pkcs7signedData_ECDSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  44509. NULL, 0, 0);
  44510. /* ECDSA with SHA256 and SubjectKeyIdentifier in SigherIdentifier */
  44511. ADD_PKCS7SIGNEDVECTOR(
  44512. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  44513. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44514. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44515. "pkcs7signedData_ECDSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  44516. NULL, 0, NULL, 0, 0);
  44517. /* ECDSA with SHA256 and custom contentType */
  44518. ADD_PKCS7SIGNEDVECTOR(
  44519. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  44520. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44521. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44522. "pkcs7signedData_ECDSA_SHA256_custom_contentType.der", 0,
  44523. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  44524. NULL, 0, 0);
  44525. /* ECDSA with SHA256 and FirmwarePkgData contentType */
  44526. ADD_PKCS7SIGNEDVECTOR(
  44527. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  44528. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44529. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44530. "pkcs7signedData_ECDSA_SHA256_firmwarePkgData.der",
  44531. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  44532. #endif
  44533. #ifdef WOLFSSL_SHA384
  44534. /* ECDSA with SHA384 */
  44535. ADD_PKCS7SIGNEDVECTOR(
  44536. data, (word32)sizeof(data), SHA384h, ECDSAk, eccClientPrivKeyBuf,
  44537. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44538. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44539. "pkcs7signedData_ECDSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  44540. NULL, 0, 0);
  44541. #endif
  44542. #ifdef WOLFSSL_SHA512
  44543. /* ECDSA with SHA512 */
  44544. ADD_PKCS7SIGNEDVECTOR(
  44545. data, (word32)sizeof(data), SHA512h, ECDSAk, eccClientPrivKeyBuf,
  44546. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44547. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44548. "pkcs7signedData_ECDSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  44549. NULL, 0, 0);
  44550. #endif
  44551. #endif /* HAVE_ECC */
  44552. };
  44553. #undef MAX_TESTVECTORS_LEN
  44554. #undef ADD_PKCS7SIGNEDVECTOR
  44555. outSz = FOURK_BUF;
  44556. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44557. if (out == NULL)
  44558. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  44559. XMEMSET(out, 0, outSz);
  44560. /* test inner pad size error with block size being 0 */
  44561. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 0);
  44562. if (ret > 0)
  44563. ERROR_OUT(-1, out);
  44564. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  44565. if (ret < 0)
  44566. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  44567. #ifndef HAVE_FIPS
  44568. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  44569. #else
  44570. ret = wc_InitRng(&rng);
  44571. #endif
  44572. if (ret != 0)
  44573. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  44574. for (i = 0; i < testSz; i++) {
  44575. if (pkcs7)
  44576. wc_PKCS7_Free(pkcs7);
  44577. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  44578. if (pkcs7 == NULL)
  44579. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  44580. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  44581. (word32)testVectors[i].certSz);
  44582. if (ret != 0)
  44583. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  44584. /* load CA certificate, if present */
  44585. if (testVectors[i].caCert != NULL) {
  44586. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  44587. (word32)testVectors[i].caCertSz);
  44588. if (ret != 0)
  44589. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  44590. }
  44591. pkcs7->rng = &rng;
  44592. pkcs7->content = (byte*)testVectors[i].content;
  44593. pkcs7->contentSz = testVectors[i].contentSz;
  44594. pkcs7->contentOID = testVectors[i].contentOID;
  44595. pkcs7->hashOID = testVectors[i].hashOID;
  44596. pkcs7->encryptOID = testVectors[i].signOID;
  44597. pkcs7->privateKey = testVectors[i].privateKey;
  44598. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  44599. pkcs7->signedAttribs = testVectors[i].signedAttribs;
  44600. pkcs7->signedAttribsSz = testVectors[i].signedAttribsSz;
  44601. /* optional custom contentType, default is DATA,
  44602. overrides contentOID if set */
  44603. if (testVectors[i].contentType != NULL) {
  44604. ret = wc_PKCS7_SetContentType(pkcs7,
  44605. (byte *)testVectors[i].contentType,
  44606. testVectors[i].contentTypeSz);
  44607. if (ret != 0)
  44608. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  44609. }
  44610. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  44611. default is IssuerAndSerialNumber */
  44612. if (testVectors[i].sidType == CMS_SKID) {
  44613. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  44614. if (ret != 0)
  44615. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  44616. }
  44617. /* generate senderNonce */
  44618. {
  44619. senderNonce[0] = 0x04;
  44620. senderNonce[1] = PKCS7_NONCE_SZ;
  44621. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  44622. if (ret != 0)
  44623. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  44624. }
  44625. /* generate transactionID (used with SCEP) */
  44626. {
  44627. #ifndef NO_SHA
  44628. wc_Sha sha;
  44629. byte digest[WC_SHA_DIGEST_SIZE];
  44630. #else
  44631. wc_Sha256 sha;
  44632. byte digest[WC_SHA256_DIGEST_SIZE];
  44633. #endif
  44634. int j,k;
  44635. transId[0] = 0x13;
  44636. transId[1] = sizeof(digest) * 2;
  44637. #ifndef NO_SHA
  44638. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  44639. if (ret != 0)
  44640. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  44641. wc_ShaUpdate(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  44642. wc_ShaFinal(&sha, digest);
  44643. wc_ShaFree(&sha);
  44644. #else
  44645. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  44646. if (ret != 0)
  44647. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  44648. wc_Sha256Update(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  44649. wc_Sha256Final(&sha, digest);
  44650. wc_Sha256Free(&sha);
  44651. #endif
  44652. for (j = 0, k = 2; j < (int)sizeof(digest); j++, k += 2) {
  44653. #if defined(WOLF_C89)
  44654. XSPRINTF((char*)&transId[k], "%02x", digest[j]);
  44655. #else
  44656. (void)XSNPRINTF((char*)&transId[k], 3, "%02x", digest[j]);
  44657. #endif
  44658. }
  44659. }
  44660. /* enable detached signature generation, if set */
  44661. if (testVectors[i].detachedSignature == 1) {
  44662. ret = wc_PKCS7_SetDetached(pkcs7, 1);
  44663. if (ret != 0)
  44664. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  44665. }
  44666. encodedSz = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
  44667. if (encodedSz < 0)
  44668. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  44669. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  44670. /* write PKCS#7 to output file for more testing */
  44671. file = XFOPEN(testVectors[i].outFileName, "wb");
  44672. if (!file) {
  44673. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  44674. }
  44675. ret = (int)XFWRITE(out, 1, encodedSz, file);
  44676. XFCLOSE(file);
  44677. if (ret != (int)encodedSz)
  44678. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  44679. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  44680. wc_PKCS7_Free(pkcs7);
  44681. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  44682. if (pkcs7 == NULL)
  44683. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  44684. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  44685. if (testVectors[i].detachedSignature == 1) {
  44686. /* set content for verifying detached signatures */
  44687. pkcs7->content = (byte*)testVectors[i].content;
  44688. pkcs7->contentSz = testVectors[i].contentSz;
  44689. }
  44690. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  44691. if (ret < 0)
  44692. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  44693. /* verify contentType extracted successfully for custom content types */
  44694. if (testVectors[i].contentTypeSz > 0) {
  44695. if (pkcs7->contentTypeSz != testVectors[i].contentTypeSz) {
  44696. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  44697. } else if (XMEMCMP(pkcs7->contentType, testVectors[i].contentType,
  44698. pkcs7->contentTypeSz) != 0) {
  44699. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  44700. }
  44701. }
  44702. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  44703. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  44704. {
  44705. /* check getting signed attributes */
  44706. #ifndef NO_SHA
  44707. byte buf[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  44708. #else
  44709. byte buf[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  44710. #endif
  44711. const byte* oidPt = transIdOid + 2; /* skip object id tag and size */
  44712. int oidSz = (int)sizeof(transIdOid) - 2;
  44713. int bufSz = 0;
  44714. if (testVectors[i].signedAttribs != NULL) {
  44715. ret = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, (word32)oidSz,
  44716. NULL, (word32*)&bufSz);
  44717. if (ret != LENGTH_ONLY_E)
  44718. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  44719. ret = 0;
  44720. }
  44721. if (bufSz > (int)sizeof(buf))
  44722. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  44723. bufSz = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, (word32)oidSz,
  44724. buf, (word32*)&bufSz);
  44725. if ((testVectors[i].signedAttribs != NULL && bufSz < 0) ||
  44726. (testVectors[i].signedAttribs == NULL && bufSz > 0))
  44727. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  44728. }
  44729. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  44730. file = XFOPEN("./pkcs7cert.der", "wb");
  44731. if (!file)
  44732. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  44733. ret = (int)XFWRITE(pkcs7->singleCert, 1, pkcs7->singleCertSz, file);
  44734. if (ret < 0)
  44735. ret = WC_TEST_RET_ENC_ERRNO;
  44736. else
  44737. ret = 0;
  44738. XFCLOSE(file);
  44739. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  44740. }
  44741. out:
  44742. if (pkcs7 != NULL)
  44743. wc_PKCS7_Free(pkcs7);
  44744. if (out != NULL)
  44745. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44746. if (testVectors != NULL)
  44747. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44748. wc_FreeRng(&rng);
  44749. if (ret > 0)
  44750. return 0;
  44751. (void)rsaClientCertBuf;
  44752. (void)rsaClientCertBufSz;
  44753. (void)rsaClientPrivKeyBuf;
  44754. (void)rsaClientPrivKeyBufSz;
  44755. (void)rsaServerCertBuf;
  44756. (void)rsaServerCertBufSz;
  44757. (void)rsaServerPrivKeyBuf;
  44758. (void)rsaServerPrivKeyBufSz;
  44759. (void)rsaCaCertBuf;
  44760. (void)rsaCaCertBufSz;
  44761. (void)rsaCaPrivKeyBuf;
  44762. (void)rsaCaPrivKeyBufSz;
  44763. (void)eccClientCertBuf;
  44764. (void)eccClientCertBufSz;
  44765. (void)eccClientPrivKeyBuf;
  44766. (void)eccClientPrivKeyBufSz;
  44767. return ret;
  44768. }
  44769. static wc_test_ret_t pkcs7signed_run_SingleShotVectors(
  44770. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  44771. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  44772. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  44773. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  44774. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  44775. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  44776. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  44777. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  44778. {
  44779. wc_test_ret_t ret;
  44780. int testSz = 0, i;
  44781. int encodedSz;
  44782. byte* out = NULL;
  44783. word32 outSz;
  44784. WC_RNG rng;
  44785. PKCS7* pkcs7 = NULL;
  44786. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  44787. XFILE file;
  44788. #endif
  44789. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  44790. !defined(NO_PKCS7_ENCRYPTED_DATA)
  44791. byte* encryptedTmp = NULL;
  44792. int encryptedTmpSz;
  44793. #endif
  44794. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  44795. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  44796. 0x72,0x6c,0x64
  44797. };
  44798. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  44799. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  44800. static const byte aes256Key[] = {
  44801. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  44802. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  44803. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  44804. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  44805. };
  44806. #endif
  44807. static const byte messageTypeOid[] =
  44808. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  44809. 0x09, 0x02 };
  44810. static const byte messageType[] = { 0x13, 2, '1', '9' };
  44811. PKCS7Attrib attribs[] =
  44812. {
  44813. { messageTypeOid, sizeof(messageTypeOid), messageType,
  44814. sizeof(messageType) },
  44815. };
  44816. #define MAX_TESTVECTORS_LEN 19
  44817. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  44818. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  44819. if (testSz == MAX_TESTVECTORS_LEN) { \
  44820. ret = WC_TEST_RET_ENC_NC; \
  44821. goto out; \
  44822. } \
  44823. XMEMCPY(&testVectors[testSz++], &_this_vector, \
  44824. sizeof _this_vector); \
  44825. }
  44826. pkcs7SignedVector *testVectors = NULL;
  44827. XMEMSET(&rng, 0, sizeof(rng));
  44828. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  44829. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44830. if (testVectors == NULL) {
  44831. ret = WC_TEST_RET_ENC_ERRNO;
  44832. goto out;
  44833. }
  44834. {
  44835. #ifndef NO_RSA
  44836. #ifndef NO_SHA256
  44837. /* Signed FirmwarePkgData, RSA, SHA256, no attribs */
  44838. ADD_PKCS7SIGNEDVECTOR(
  44839. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  44840. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44841. NULL, 0,
  44842. "pkcs7signedFirmwarePkgData_RSA_SHA256_noattr.der", 0, NULL, 0, 0,
  44843. 0, 0, NULL, 0, NULL, 0, 0);
  44844. /* Signed FirmwarePkgData, RSA, SHA256, attrs */
  44845. ADD_PKCS7SIGNEDVECTOR(
  44846. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  44847. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44848. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44849. "pkcs7signedFirmwarePkgData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0,
  44850. NULL, 0, NULL, 0, 0);
  44851. /* Signed FirmwarePkgData, RSA, SHA256, SubjectKeyIdentifier, attrs */
  44852. ADD_PKCS7SIGNEDVECTOR(
  44853. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  44854. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44855. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44856. "pkcs7signedFirmwarePkgData_RSA_SHA256_SKID.der", 0, NULL,
  44857. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  44858. /* Signed FirmwraePkgData, RSA, SHA256, server cert and ca cert, attr */
  44859. ADD_PKCS7SIGNEDVECTOR(
  44860. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  44861. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  44862. rsaCaCertBuf, rsaCaCertBufSz,
  44863. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44864. "pkcs7signedFirmwarePkgData_RSA_SHA256_with_ca_cert.der", 0, NULL,
  44865. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  44866. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  44867. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  44868. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, no attribs */
  44869. ADD_PKCS7SIGNEDVECTOR(
  44870. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  44871. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44872. NULL, 0,
  44873. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  44874. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  44875. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, attribs */
  44876. ADD_PKCS7SIGNEDVECTOR(
  44877. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  44878. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44879. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44880. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256.der", 0,
  44881. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  44882. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  44883. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  44884. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  44885. /* Signed Compressed FirmwarePkgData, RSA, SHA256, no attribs */
  44886. ADD_PKCS7SIGNEDVECTOR(
  44887. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  44888. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44889. NULL, 0,
  44890. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  44891. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  44892. /* Signed Compressed FirmwarePkgData, RSA, SHA256, attribs */
  44893. ADD_PKCS7SIGNEDVECTOR(
  44894. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  44895. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44896. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44897. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256.der", 0,
  44898. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  44899. #ifndef NO_PKCS7_ENCRYPTED_DATA
  44900. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  44901. no attribs */
  44902. ADD_PKCS7SIGNEDVECTOR(
  44903. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  44904. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44905. NULL, 0,
  44906. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256_noattr.der",
  44907. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  44908. 0, 0);
  44909. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  44910. attribs */
  44911. ADD_PKCS7SIGNEDVECTOR(
  44912. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  44913. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  44914. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44915. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256.der",
  44916. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  44917. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  44918. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  44919. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  44920. #endif /* NO_SHA256 */
  44921. #endif /* NO_RSA */
  44922. #ifdef HAVE_ECC
  44923. #ifndef NO_SHA256
  44924. /* Signed FirmwarePkgData, ECDSA, SHA256, no attribs */
  44925. ADD_PKCS7SIGNEDVECTOR(
  44926. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  44927. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44928. NULL, 0,
  44929. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  44930. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  44931. /* Signed FirmwarePkgData, ECDSA, SHA256, attribs */
  44932. ADD_PKCS7SIGNEDVECTOR(
  44933. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  44934. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44935. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44936. "pkcs7signedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  44937. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  44938. /* Signed FirmwarePkgData, ECDSA, SHA256, SubjectKeyIdentifier, attr */
  44939. ADD_PKCS7SIGNEDVECTOR(
  44940. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  44941. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44942. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44943. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_SKID.der", 0, NULL,
  44944. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  44945. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  44946. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  44947. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, no attribs */
  44948. ADD_PKCS7SIGNEDVECTOR(
  44949. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  44950. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44951. NULL, 0,
  44952. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  44953. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  44954. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, attribs */
  44955. ADD_PKCS7SIGNEDVECTOR(
  44956. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  44957. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44958. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44959. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  44960. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  44961. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  44962. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  44963. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  44964. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, no attribs */
  44965. ADD_PKCS7SIGNEDVECTOR(
  44966. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  44967. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44968. NULL, 0,
  44969. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  44970. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  44971. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, attrib */
  44972. ADD_PKCS7SIGNEDVECTOR(
  44973. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  44974. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44975. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44976. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  44977. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  44978. #ifndef NO_PKCS7_ENCRYPTED_DATA
  44979. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  44980. no attribs */
  44981. ADD_PKCS7SIGNEDVECTOR(
  44982. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  44983. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44984. NULL, 0,
  44985. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der",
  44986. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  44987. 0, 0);
  44988. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  44989. attribs */
  44990. ADD_PKCS7SIGNEDVECTOR(
  44991. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  44992. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  44993. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  44994. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256.der",
  44995. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  44996. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  44997. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  44998. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  44999. #endif /* NO_SHA256 */
  45000. #endif /* HAVE_ECC */
  45001. };
  45002. #undef MAX_TESTVECTORS_LEN
  45003. #undef ADD_PKCS7SIGNEDVECTOR
  45004. outSz = FOURK_BUF;
  45005. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45006. if (out == NULL)
  45007. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  45008. XMEMSET(out, 0, outSz);
  45009. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  45010. if (ret < 0)
  45011. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45012. #ifndef HAVE_FIPS
  45013. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  45014. #else
  45015. ret = wc_InitRng(&rng);
  45016. #endif
  45017. if (ret != 0)
  45018. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45019. for (i = 0; i < testSz; i++) {
  45020. if (pkcs7)
  45021. wc_PKCS7_Free(pkcs7);
  45022. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  45023. if (pkcs7 == NULL)
  45024. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  45025. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  45026. (word32)testVectors[i].certSz);
  45027. if (ret != 0)
  45028. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45029. /* load CA certificate, if present */
  45030. if (testVectors[i].caCert != NULL) {
  45031. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  45032. (word32)testVectors[i].caCertSz);
  45033. if (ret != 0)
  45034. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45035. }
  45036. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  45037. default is IssuerAndSerialNumber */
  45038. if (testVectors[i].sidType == CMS_SKID) {
  45039. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  45040. if (ret != 0)
  45041. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45042. }
  45043. if (testVectors[i].encCompFlag == 0) {
  45044. /* encode Signed FirmwarePkgData */
  45045. encodedSz = wc_PKCS7_EncodeSignedFPD(pkcs7,
  45046. testVectors[i].privateKey, testVectors[i].privateKeySz,
  45047. testVectors[i].signOID, testVectors[i].hashOID,
  45048. (byte*)testVectors[i].content, testVectors[i].contentSz,
  45049. testVectors[i].signedAttribs,
  45050. testVectors[i].signedAttribsSz, out, outSz);
  45051. if (encodedSz < 0)
  45052. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  45053. #ifndef NO_PKCS7_ENCRYPTED_DATA
  45054. } else if (testVectors[i].encCompFlag == 1) {
  45055. /* encode Signed Encrypted FirmwarePkgData */
  45056. encodedSz = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7,
  45057. (byte *)testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  45058. testVectors[i].privateKey, testVectors[i].privateKeySz,
  45059. testVectors[i].encryptOID, testVectors[i].signOID,
  45060. testVectors[i].hashOID, (byte*)testVectors[i].content,
  45061. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  45062. testVectors[i].unprotectedAttribsSz,
  45063. testVectors[i].signedAttribs,
  45064. testVectors[i].signedAttribsSz, out, outSz);
  45065. if (encodedSz <= 0)
  45066. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  45067. #endif
  45068. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  45069. } else if (testVectors[i].encCompFlag == 2) {
  45070. /* encode Signed Compressed FirmwarePkgData */
  45071. encodedSz = wc_PKCS7_EncodeSignedCompressedFPD(pkcs7,
  45072. testVectors[i].privateKey, testVectors[i].privateKeySz,
  45073. testVectors[i].signOID, testVectors[i].hashOID,
  45074. (byte*)testVectors[i].content, testVectors[i].contentSz,
  45075. testVectors[i].signedAttribs,
  45076. testVectors[i].signedAttribsSz, out, outSz);
  45077. if (encodedSz <= 0)
  45078. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  45079. #ifndef NO_PKCS7_ENCRYPTED_DATA
  45080. } else if (testVectors[i].encCompFlag == 3) {
  45081. /* encode Signed Encrypted Compressed FirmwarePkgData */
  45082. encodedSz = wc_PKCS7_EncodeSignedEncryptedCompressedFPD(pkcs7,
  45083. (byte*)testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  45084. testVectors[i].privateKey, testVectors[i].privateKeySz,
  45085. testVectors[i].encryptOID, testVectors[i].signOID,
  45086. testVectors[i].hashOID, (byte*)testVectors[i].content,
  45087. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  45088. testVectors[i].unprotectedAttribsSz,
  45089. testVectors[i].signedAttribs,
  45090. testVectors[i].signedAttribsSz, out, outSz);
  45091. if (encodedSz <= 0)
  45092. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  45093. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  45094. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  45095. } else {
  45096. /* unsupported SignedData single-shot combination */
  45097. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  45098. }
  45099. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  45100. /* write PKCS#7 to output file for more testing */
  45101. file = XFOPEN(testVectors[i].outFileName, "wb");
  45102. if (!file)
  45103. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  45104. ret = (int)XFWRITE(out, 1, encodedSz, file);
  45105. XFCLOSE(file);
  45106. file = NULL;
  45107. if (ret != (int)encodedSz)
  45108. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  45109. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  45110. wc_PKCS7_Free(pkcs7);
  45111. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  45112. if (pkcs7 == NULL)
  45113. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  45114. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  45115. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  45116. if (ret < 0)
  45117. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45118. #ifndef NO_PKCS7_STREAM
  45119. {
  45120. word32 z;
  45121. for (z = 0; z < outSz && ret != 0; z++) {
  45122. ret = wc_PKCS7_VerifySignedData(pkcs7, out + z, 1);
  45123. if (ret < 0 && ret != WC_PKCS7_WANT_READ_E) {
  45124. printf("unexpected error %d\n", ret);
  45125. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45126. }
  45127. }
  45128. }
  45129. #endif
  45130. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  45131. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  45132. if (testVectors[i].encCompFlag == 0) {
  45133. /* verify decoded content matches expected */
  45134. if ((pkcs7->contentSz != testVectors[i].contentSz) ||
  45135. XMEMCMP(pkcs7->content, testVectors[i].content,
  45136. pkcs7->contentSz)) {
  45137. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  45138. }
  45139. }
  45140. #ifndef NO_PKCS7_ENCRYPTED_DATA
  45141. else if (testVectors[i].encCompFlag == 1) {
  45142. /* decrypt inner encryptedData */
  45143. pkcs7->encryptionKey = (byte *)testVectors[i].encryptKey;
  45144. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  45145. ret = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  45146. pkcs7->contentSz, out, outSz);
  45147. if (ret < 0)
  45148. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45149. /* compare decrypted to expected */
  45150. if (((word32)ret != testVectors[i].contentSz) ||
  45151. XMEMCMP(out, testVectors[i].content, ret))
  45152. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  45153. }
  45154. #endif
  45155. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  45156. else if (testVectors[i].encCompFlag == 2) {
  45157. /* decompress inner compressedData */
  45158. ret = wc_PKCS7_DecodeCompressedData(pkcs7, pkcs7->content,
  45159. pkcs7->contentSz, out, outSz);
  45160. if (ret < 0)
  45161. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45162. /* compare decompressed to expected */
  45163. if (((word32)ret != testVectors[i].contentSz) ||
  45164. XMEMCMP(out, testVectors[i].content, ret))
  45165. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  45166. }
  45167. #ifndef NO_PKCS7_ENCRYPTED_DATA
  45168. else if (testVectors[i].encCompFlag == 3) {
  45169. encryptedTmpSz = FOURK_BUF;
  45170. encryptedTmp = (byte*)XMALLOC(encryptedTmpSz, HEAP_HINT,
  45171. DYNAMIC_TYPE_TMP_BUFFER);
  45172. if (encryptedTmp == NULL)
  45173. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  45174. XMEMSET(encryptedTmp, 0, encryptedTmpSz);
  45175. /* decrypt inner encryptedData */
  45176. pkcs7->encryptionKey = (byte*)testVectors[i].encryptKey;
  45177. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  45178. encryptedTmpSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  45179. pkcs7->contentSz, encryptedTmp,
  45180. encryptedTmpSz);
  45181. if (encryptedTmpSz < 0 || pkcs7->contentOID != COMPRESSED_DATA)
  45182. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  45183. /* decompress inner compressedData */
  45184. ret = wc_PKCS7_DecodeCompressedData(pkcs7, encryptedTmp,
  45185. encryptedTmpSz, out, outSz);
  45186. if (ret < 0)
  45187. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  45188. /* compare decompressed to expected */
  45189. if (((word32)ret != testVectors[i].contentSz) ||
  45190. XMEMCMP(out, testVectors[i].content, ret))
  45191. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  45192. }
  45193. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  45194. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  45195. }
  45196. out:
  45197. if (pkcs7 != NULL)
  45198. wc_PKCS7_Free(pkcs7);
  45199. if (out != NULL)
  45200. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45201. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  45202. !defined(NO_PKCS7_ENCRYPTED_DATA)
  45203. if (encryptedTmp != NULL)
  45204. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45205. #endif
  45206. if (testVectors != NULL)
  45207. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45208. wc_FreeRng(&rng);
  45209. if (ret > 0)
  45210. return 0;
  45211. (void)eccClientCertBuf;
  45212. (void)eccClientCertBufSz;
  45213. (void)eccClientPrivKeyBuf;
  45214. (void)eccClientPrivKeyBufSz;
  45215. (void)rsaClientCertBuf;
  45216. (void)rsaClientCertBufSz;
  45217. (void)rsaClientPrivKeyBuf;
  45218. (void)rsaClientPrivKeyBufSz;
  45219. (void)rsaServerCertBuf;
  45220. (void)rsaServerCertBufSz;
  45221. (void)rsaServerPrivKeyBuf;
  45222. (void)rsaServerPrivKeyBufSz;
  45223. (void)rsaCaCertBuf;
  45224. (void)rsaCaCertBufSz;
  45225. (void)rsaCaPrivKeyBuf;
  45226. (void)rsaCaPrivKeyBufSz;
  45227. return ret;
  45228. }
  45229. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7signed_test(void)
  45230. {
  45231. wc_test_ret_t ret = 0;
  45232. byte* rsaClientCertBuf = NULL;
  45233. byte* rsaServerCertBuf = NULL;
  45234. byte* rsaCaCertBuf = NULL;
  45235. byte* eccClientCertBuf = NULL;
  45236. byte* rsaClientPrivKeyBuf = NULL;
  45237. byte* rsaServerPrivKeyBuf = NULL;
  45238. byte* rsaCaPrivKeyBuf = NULL;
  45239. byte* eccClientPrivKeyBuf = NULL;
  45240. word32 rsaClientCertBufSz = 0;
  45241. word32 rsaServerCertBufSz = 0;
  45242. word32 rsaCaCertBufSz = 0;
  45243. word32 eccClientCertBufSz = 0;
  45244. word32 rsaClientPrivKeyBufSz = 0;
  45245. word32 rsaServerPrivKeyBufSz = 0;
  45246. word32 rsaCaPrivKeyBufSz = 0;
  45247. word32 eccClientPrivKeyBufSz = 0;
  45248. WOLFSSL_ENTER("pkcs7signed_test");
  45249. #ifndef NO_RSA
  45250. /* read client RSA cert and key in DER format */
  45251. rsaClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  45252. DYNAMIC_TYPE_TMP_BUFFER);
  45253. if (rsaClientCertBuf == NULL)
  45254. ret = WC_TEST_RET_ENC_NC;
  45255. rsaClientPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  45256. DYNAMIC_TYPE_TMP_BUFFER);
  45257. if (ret == 0 && rsaClientPrivKeyBuf == NULL) {
  45258. ret = WC_TEST_RET_ENC_ERRNO;
  45259. }
  45260. rsaClientCertBufSz = FOURK_BUF;
  45261. rsaClientPrivKeyBufSz = FOURK_BUF;
  45262. /* read server RSA cert and key in DER format */
  45263. rsaServerCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  45264. DYNAMIC_TYPE_TMP_BUFFER);
  45265. if (ret == 0 && rsaServerCertBuf == NULL)
  45266. ret = WC_TEST_RET_ENC_NC;
  45267. rsaServerPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  45268. DYNAMIC_TYPE_TMP_BUFFER);
  45269. if (ret == 0 && rsaServerPrivKeyBuf == NULL) {
  45270. ret = WC_TEST_RET_ENC_ERRNO;
  45271. }
  45272. rsaServerCertBufSz = FOURK_BUF;
  45273. rsaServerPrivKeyBufSz = FOURK_BUF;
  45274. /* read CA RSA cert and key in DER format, for use with server cert */
  45275. rsaCaCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  45276. DYNAMIC_TYPE_TMP_BUFFER);
  45277. if (ret == 0 && rsaCaCertBuf == NULL)
  45278. ret = WC_TEST_RET_ENC_NC;
  45279. rsaCaPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  45280. DYNAMIC_TYPE_TMP_BUFFER);
  45281. if (ret == 0 && rsaCaPrivKeyBuf == NULL) {
  45282. ret = WC_TEST_RET_ENC_ERRNO;
  45283. }
  45284. rsaCaCertBufSz = FOURK_BUF;
  45285. rsaCaPrivKeyBufSz = FOURK_BUF;
  45286. #endif /* NO_RSA */
  45287. #ifdef HAVE_ECC
  45288. /* read client ECC cert and key in DER format */
  45289. eccClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  45290. DYNAMIC_TYPE_TMP_BUFFER);
  45291. if (ret == 0 && eccClientCertBuf == NULL) {
  45292. ret = WC_TEST_RET_ENC_ERRNO;
  45293. }
  45294. eccClientPrivKeyBuf =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  45295. DYNAMIC_TYPE_TMP_BUFFER);
  45296. if (ret == 0 && eccClientPrivKeyBuf == NULL) {
  45297. ret = WC_TEST_RET_ENC_ERRNO;
  45298. }
  45299. eccClientCertBufSz = FOURK_BUF;
  45300. eccClientPrivKeyBufSz = FOURK_BUF;
  45301. #endif /* HAVE_ECC */
  45302. if (ret >= 0)
  45303. ret = pkcs7_load_certs_keys(rsaClientCertBuf, &rsaClientCertBufSz,
  45304. rsaClientPrivKeyBuf, &rsaClientPrivKeyBufSz,
  45305. rsaServerCertBuf, &rsaServerCertBufSz,
  45306. rsaServerPrivKeyBuf, &rsaServerPrivKeyBufSz,
  45307. rsaCaCertBuf, &rsaCaCertBufSz,
  45308. rsaCaPrivKeyBuf, &rsaCaPrivKeyBufSz,
  45309. eccClientCertBuf, &eccClientCertBufSz,
  45310. eccClientPrivKeyBuf, &eccClientPrivKeyBufSz);
  45311. if (ret < 0) {
  45312. ret = WC_TEST_RET_ENC_EC(ret);
  45313. }
  45314. if (ret >= 0)
  45315. ret = pkcs7signed_run_vectors(rsaClientCertBuf, (word32)rsaClientCertBufSz,
  45316. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  45317. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  45318. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  45319. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  45320. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  45321. eccClientCertBuf, (word32)eccClientCertBufSz,
  45322. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  45323. if (ret >= 0)
  45324. ret = pkcs7signed_run_SingleShotVectors(
  45325. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  45326. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  45327. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  45328. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  45329. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  45330. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  45331. eccClientCertBuf, (word32)eccClientCertBufSz,
  45332. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  45333. #if !defined(NO_RSA) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  45334. if (ret >= 0)
  45335. ret = pkcs7callback_test(
  45336. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  45337. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz);
  45338. #endif
  45339. XFREE(rsaClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45340. XFREE(rsaClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45341. XFREE(rsaServerCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45342. XFREE(rsaServerPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45343. XFREE(rsaCaCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45344. XFREE(rsaCaPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45345. XFREE(eccClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45346. XFREE(eccClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45347. return ret;
  45348. }
  45349. #endif /* HAVE_PKCS7 */
  45350. #if defined(WOLFSSL_PUBLIC_MP) && \
  45351. ((defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  45352. defined(USE_FAST_MATH))
  45353. /* Maximum number of bytes in a number to test. */
  45354. #define MP_MAX_TEST_BYTE_LEN 32
  45355. static wc_test_ret_t randNum(mp_int* n, int len, WC_RNG* rng, void* heap)
  45356. {
  45357. byte d[MP_MAX_TEST_BYTE_LEN];
  45358. wc_test_ret_t ret;
  45359. (void)heap;
  45360. do {
  45361. ret = wc_RNG_GenerateBlock(rng, d, (word32)len);
  45362. if (ret != 0)
  45363. return ret;
  45364. ret = mp_read_unsigned_bin(n, d, (word32)len);
  45365. if (ret != 0)
  45366. return ret;
  45367. } while (mp_iszero(n));
  45368. return 0;
  45369. }
  45370. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  45371. static wc_test_ret_t mp_test_div_3(mp_int* a, mp_int* r, WC_RNG* rng)
  45372. {
  45373. int i, j;
  45374. mp_digit rem;
  45375. mp_digit rem2;
  45376. wc_test_ret_t ret;
  45377. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  45378. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  45379. for (i = 0; i < 10; i++) {
  45380. for (j = 1; j < 10; j++) {
  45381. ret = randNum(a, j, rng, NULL);
  45382. if (ret != 0)
  45383. return WC_TEST_RET_ENC_EC(ret);
  45384. ret = mp_div_3(a, r, &rem);
  45385. if (ret != 0)
  45386. return WC_TEST_RET_ENC_EC(ret);
  45387. ret = mp_mul_d(r, 3, r);
  45388. if (ret != 0)
  45389. return WC_TEST_RET_ENC_EC(ret);
  45390. ret = mp_add_d(r, rem, r);
  45391. if (ret != 0)
  45392. return WC_TEST_RET_ENC_EC(ret);
  45393. ret = mp_cmp(r, a);
  45394. if (ret != MP_EQ)
  45395. return WC_TEST_RET_ENC_NC;
  45396. }
  45397. }
  45398. ret = mp_div_3(a, r, &rem);
  45399. if (ret != 0)
  45400. return WC_TEST_RET_ENC_NC;
  45401. ret = mp_div_3(a, a, NULL);
  45402. if (ret != 0)
  45403. return WC_TEST_RET_ENC_NC;
  45404. ret = mp_cmp(r, a);
  45405. if (ret != MP_EQ)
  45406. return WC_TEST_RET_ENC_NC;
  45407. #endif
  45408. #if defined(WOLFSSL_SP_MATH_ALL)
  45409. ret = mp_div_d(a, 10, r, &rem);
  45410. if (ret != 0)
  45411. return WC_TEST_RET_ENC_EC(ret);
  45412. ret = mp_div_d(a, 10, a, NULL);
  45413. if (ret != 0)
  45414. return WC_TEST_RET_ENC_EC(ret);
  45415. ret = mp_cmp(r, a);
  45416. if (ret != MP_EQ)
  45417. return WC_TEST_RET_ENC_NC;
  45418. ret = mp_div_d(a, 12, r, &rem);
  45419. if (ret != 0)
  45420. return WC_TEST_RET_ENC_EC(ret);
  45421. ret = mp_div_d(a, 12, a, NULL);
  45422. if (ret != 0)
  45423. return WC_TEST_RET_ENC_EC(ret);
  45424. ret = mp_cmp(r, a);
  45425. if (ret != MP_EQ)
  45426. return WC_TEST_RET_ENC_NC;
  45427. ret = mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), r, &rem);
  45428. if (ret != 0)
  45429. return WC_TEST_RET_ENC_EC(ret);
  45430. ret = mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), NULL, &rem2);
  45431. if (ret != 0)
  45432. return WC_TEST_RET_ENC_EC(ret);
  45433. ret = mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), a, NULL);
  45434. if (ret != 0)
  45435. return WC_TEST_RET_ENC_EC(ret);
  45436. ret = mp_cmp(r, a);
  45437. if (ret != MP_EQ)
  45438. return WC_TEST_RET_ENC_NC;
  45439. if (rem != rem2)
  45440. return WC_TEST_RET_ENC_NC;
  45441. #endif
  45442. (void)a;
  45443. (void)r;
  45444. (void)rng;
  45445. (void)i;
  45446. (void)j;
  45447. (void)rem;
  45448. (void)rem2;
  45449. (void)ret;
  45450. return 0;
  45451. }
  45452. #endif /* WOLFSSL_SP_MATH || !USE_FAST_MATH */
  45453. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  45454. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  45455. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  45456. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  45457. static wc_test_ret_t mp_test_radix_10(mp_int* a, mp_int* r, WC_RNG* rng)
  45458. {
  45459. wc_test_ret_t ret;
  45460. int i, j;
  45461. int size;
  45462. char str[30];
  45463. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = "A";
  45464. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "a";
  45465. WOLFSSL_SMALL_STACK_STATIC const char* empty2 = " ";
  45466. WOLFSSL_SMALL_STACK_STATIC const char* zeros = "000";
  45467. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  45468. for (i = 0; i < 10; i++) {
  45469. for (j = 2; j < 12; j++) {
  45470. ret = randNum(a, j, rng, NULL);
  45471. if (ret != 0)
  45472. return WC_TEST_RET_ENC_EC(ret);
  45473. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  45474. if (ret != MP_OKAY)
  45475. return WC_TEST_RET_ENC_EC(ret);
  45476. ret = mp_toradix(a, str, MP_RADIX_DEC);
  45477. if (ret != MP_OKAY)
  45478. return WC_TEST_RET_ENC_EC(ret);
  45479. if ((int)XSTRLEN(str) != size - 1)
  45480. return WC_TEST_RET_ENC_NC;
  45481. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  45482. if (ret != MP_OKAY)
  45483. return WC_TEST_RET_ENC_EC(ret);
  45484. ret = mp_cmp(a, r);
  45485. if (ret != MP_EQ)
  45486. return WC_TEST_RET_ENC_NC;
  45487. }
  45488. }
  45489. ret = mp_read_radix(r, badStr1, MP_RADIX_DEC);
  45490. if (ret != MP_VAL)
  45491. return WC_TEST_RET_ENC_EC(ret);
  45492. ret = mp_read_radix(r, badStr2, MP_RADIX_DEC);
  45493. if (ret != MP_VAL)
  45494. return WC_TEST_RET_ENC_EC(ret);
  45495. ret = mp_read_radix(r, empty2, MP_RADIX_DEC);
  45496. if (ret != MP_OKAY)
  45497. return WC_TEST_RET_ENC_EC(ret);
  45498. ret = mp_read_radix(r, zeros, MP_RADIX_DEC);
  45499. if (ret != MP_OKAY)
  45500. return WC_TEST_RET_ENC_EC(ret);
  45501. if (!mp_iszero(r))
  45502. return WC_TEST_RET_ENC_NC;
  45503. mp_set(r, 1);
  45504. ret = mp_read_radix(r, empty, MP_RADIX_DEC);
  45505. if (ret != MP_OKAY)
  45506. return WC_TEST_RET_ENC_EC(ret);
  45507. if (!mp_iszero(r))
  45508. return WC_TEST_RET_ENC_NC;
  45509. mp_zero(a);
  45510. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  45511. if (ret != 0)
  45512. return WC_TEST_RET_ENC_EC(ret);
  45513. if (size != 2)
  45514. return WC_TEST_RET_ENC_NC;
  45515. ret = mp_toradix(a, str, MP_RADIX_DEC);
  45516. if (ret != 0)
  45517. return WC_TEST_RET_ENC_EC(ret);
  45518. if ((int)XSTRLEN(str) != size - 1)
  45519. return WC_TEST_RET_ENC_NC;
  45520. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  45521. if (ret != 0)
  45522. return WC_TEST_RET_ENC_EC(ret);
  45523. if (!mp_iszero(r))
  45524. return WC_TEST_RET_ENC_NC;
  45525. return 0;
  45526. }
  45527. #endif
  45528. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  45529. defined(HAVE_ECC))
  45530. static wc_test_ret_t mp_test_radix_16(mp_int* a, mp_int* r, WC_RNG* rng)
  45531. {
  45532. wc_test_ret_t ret;
  45533. int i, j;
  45534. int size;
  45535. char str[30];
  45536. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  45537. static char longStr[2 * sizeof(a->dp) + 2];
  45538. #endif
  45539. WOLFSSL_SMALL_STACK_STATIC const char* empty2 = " ";
  45540. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "}";
  45541. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  45542. for (i = 0; i < 10; i++) {
  45543. for (j = 2; j < 12; j++) {
  45544. ret = randNum(a, j, rng, NULL);
  45545. if (ret != 0)
  45546. return WC_TEST_RET_ENC_EC(ret);
  45547. mp_radix_size(a, MP_RADIX_HEX, &size);
  45548. mp_toradix(a, str, MP_RADIX_HEX);
  45549. if ((int)XSTRLEN(str) != size - 1)
  45550. return WC_TEST_RET_ENC_NC;
  45551. mp_read_radix(r, str, MP_RADIX_HEX);
  45552. ret = mp_cmp(a, r);
  45553. if (ret != MP_EQ)
  45554. return WC_TEST_RET_ENC_NC;
  45555. }
  45556. }
  45557. ret = mp_read_radix(r, empty2, MP_RADIX_HEX);
  45558. if (ret != MP_OKAY)
  45559. return WC_TEST_RET_ENC_EC(ret);
  45560. ret = mp_read_radix(r, badStr2, MP_RADIX_HEX);
  45561. if (ret != MP_VAL)
  45562. return WC_TEST_RET_ENC_EC(ret);
  45563. mp_set(r, 1);
  45564. ret = mp_read_radix(r, empty, MP_RADIX_HEX);
  45565. if (ret != MP_OKAY)
  45566. return WC_TEST_RET_ENC_EC(ret);
  45567. if (!mp_iszero(r))
  45568. return WC_TEST_RET_ENC_NC;
  45569. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  45570. /* Fixed MP data size - string can be too long. */
  45571. longStr[0] = '8';
  45572. XMEMSET(longStr+1, '0', sizeof(longStr) - 2);
  45573. longStr[sizeof(longStr)-1] = '\0';
  45574. ret = mp_read_radix(r, longStr, MP_RADIX_HEX);
  45575. if (ret != MP_VAL)
  45576. return WC_TEST_RET_ENC_EC(ret);
  45577. #endif
  45578. mp_zero(a);
  45579. ret = mp_radix_size(a, MP_RADIX_HEX, &size);
  45580. if (ret != 0)
  45581. return WC_TEST_RET_ENC_EC(ret);
  45582. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  45583. if (size != 3)
  45584. #else
  45585. if (size != 2)
  45586. #endif
  45587. return WC_TEST_RET_ENC_NC;
  45588. ret = mp_toradix(a, str, MP_RADIX_HEX);
  45589. if (ret != 0)
  45590. return WC_TEST_RET_ENC_EC(ret);
  45591. if ((int)XSTRLEN(str) != size - 1)
  45592. return WC_TEST_RET_ENC_NC;
  45593. ret = mp_read_radix(r, str, MP_RADIX_HEX);
  45594. if (ret != 0)
  45595. return WC_TEST_RET_ENC_EC(ret);
  45596. if (!mp_iszero(r))
  45597. return WC_TEST_RET_ENC_NC;
  45598. #ifdef WOLFSSL_SP_MATH
  45599. ret = mp_toradix(a, str, 8);
  45600. if (ret != MP_VAL)
  45601. return WC_TEST_RET_ENC_EC(ret);
  45602. ret = mp_radix_size(a, 8, &size);
  45603. if (ret != MP_VAL)
  45604. return WC_TEST_RET_ENC_EC(ret);
  45605. #endif
  45606. return 0;
  45607. }
  45608. #endif
  45609. static wc_test_ret_t mp_test_shift(mp_int* a, mp_int* r1, WC_RNG* rng)
  45610. {
  45611. int i;
  45612. wc_test_ret_t ret;
  45613. ret = randNum(a, 4, rng, NULL);
  45614. if (ret != 0)
  45615. return WC_TEST_RET_ENC_EC(ret);
  45616. for (i = 0; i < 4; i++) {
  45617. mp_copy(r1, a);
  45618. ret = mp_lshd(r1, i);
  45619. if (ret != MP_OKAY)
  45620. return WC_TEST_RET_ENC_EC(ret);
  45621. #ifndef WOLFSSL_SP_MATH
  45622. mp_rshd(r1, i);
  45623. #else
  45624. mp_rshb(r1, i * SP_WORD_SIZE);
  45625. #endif
  45626. ret = mp_cmp(a, r1);
  45627. if (ret != MP_EQ)
  45628. return WC_TEST_RET_ENC_NC;
  45629. }
  45630. #ifndef WOLFSSL_SP_MATH
  45631. for (i = 0; i < DIGIT_BIT+1; i++) {
  45632. ret = mp_mul_2d(a, i, r1);
  45633. if (ret != MP_OKAY)
  45634. return WC_TEST_RET_ENC_EC(ret);
  45635. mp_rshb(r1, i);
  45636. ret = mp_cmp(a, r1);
  45637. if (ret != MP_EQ)
  45638. return WC_TEST_RET_ENC_NC;
  45639. }
  45640. #endif
  45641. return 0;
  45642. }
  45643. static wc_test_ret_t mp_test_add_sub_d(mp_int* a, mp_int* r1)
  45644. {
  45645. int i, j;
  45646. wc_test_ret_t ret;
  45647. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  45648. mp_zero(a);
  45649. mp_set_bit(a, i);
  45650. if ((int)a->used != (i + DIGIT_BIT) / DIGIT_BIT)
  45651. return WC_TEST_RET_ENC_NC;
  45652. for (j = 0; j < i && j < DIGIT_BIT; j++) {
  45653. mp_zero(r1);
  45654. mp_set_bit(r1, i);
  45655. ret = mp_sub_d(r1, (mp_digit)1 << j, r1);
  45656. if (ret != MP_OKAY)
  45657. return WC_TEST_RET_ENC_EC(ret);
  45658. ret = mp_add_d(r1, (mp_digit)1 << j, r1);
  45659. if (ret != MP_OKAY)
  45660. return WC_TEST_RET_ENC_EC(ret);
  45661. ret = mp_cmp(a, r1);
  45662. if (ret != MP_EQ)
  45663. return WC_TEST_RET_ENC_NC;
  45664. }
  45665. }
  45666. mp_zero(r1);
  45667. ret = mp_add_d(r1, 1, r1);
  45668. if (ret != MP_OKAY)
  45669. return WC_TEST_RET_ENC_EC(ret);
  45670. if (r1->used != 1)
  45671. return WC_TEST_RET_ENC_NC;
  45672. ret = mp_sub_d(r1, 1, r1);
  45673. if (ret != MP_OKAY)
  45674. return WC_TEST_RET_ENC_EC(ret);
  45675. if (r1->used != 0)
  45676. return WC_TEST_RET_ENC_NC;
  45677. return 0;
  45678. }
  45679. static wc_test_ret_t mp_test_read_to_bin(mp_int* a)
  45680. {
  45681. WOLFSSL_SMALL_STACK_STATIC const byte in[16] = {
  45682. 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7, 0x08,
  45683. 0x93, 0xa4, 0xb4, 0xc5, 0xd6, 0xe7, 0xf8, 0x09
  45684. };
  45685. byte out[24];
  45686. int i, j, k;
  45687. const byte* p;
  45688. wc_test_ret_t ret;
  45689. for (i = 0; i < (int)sizeof(in); i++) {
  45690. p = in + sizeof(in) - i;
  45691. ret = mp_read_unsigned_bin(a, p, (word32)i);
  45692. if (ret != 0)
  45693. return WC_TEST_RET_ENC_EC(ret);
  45694. for (j = i; j < (int)sizeof(out); j++) {
  45695. XMEMSET(out, 0xff, sizeof(out));
  45696. ret = mp_to_unsigned_bin_len(a, out, j);
  45697. if (ret != 0)
  45698. return WC_TEST_RET_ENC_EC(ret);
  45699. for (k = 0; k < j - i; k++) {
  45700. if (out[k] != 0)
  45701. return WC_TEST_RET_ENC_NC;
  45702. }
  45703. for (; k < j; k++) {
  45704. if (out[k] != p[k - (j - i)])
  45705. return WC_TEST_RET_ENC_NC;
  45706. }
  45707. }
  45708. }
  45709. /* Length too small. */
  45710. ret = mp_to_unsigned_bin_len(a, out, 1);
  45711. if (ret != MP_VAL)
  45712. return WC_TEST_RET_ENC_EC(ret);
  45713. ret = mp_read_unsigned_bin(a, NULL, 0);
  45714. if (ret != 0)
  45715. return WC_TEST_RET_ENC_EC(ret);
  45716. if (!mp_iszero(a))
  45717. return WC_TEST_RET_ENC_NC;
  45718. return 0;
  45719. }
  45720. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  45721. static wc_test_ret_t mp_test_set_int(mp_int* a)
  45722. {
  45723. #if SP_ULONG_BITS == 64
  45724. unsigned long n = 0xfedcba9876543210UL;
  45725. byte exp[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
  45726. byte out[8] = { 0 };
  45727. #elif SP_ULONG_BITS == 32
  45728. unsigned long n = 0xfedcba98UL;
  45729. byte exp[4] = { 0xfe, 0xdc, 0xba, 0x98 };
  45730. byte out[4] = { 0 };
  45731. #elif SP_ULONG_BITS == 16
  45732. unsigned long n = 0xfedc;
  45733. byte exp[2] = { 0xfe, 0xdc };
  45734. byte out[2] = { 0 };
  45735. #elif SP_ULONG_BITS == 8
  45736. unsigned long n = 0xfe;
  45737. byte exp[1] = { 0xfe };
  45738. byte out[1] = { 0 };
  45739. #endif
  45740. wc_test_ret_t ret;
  45741. ret = mp_set_int(a, n);
  45742. if (ret != 0)
  45743. return WC_TEST_RET_ENC_EC(ret);
  45744. ret = mp_unsigned_bin_size(a);
  45745. if (ret != sizeof(exp))
  45746. return WC_TEST_RET_ENC_NC;
  45747. ret = mp_to_unsigned_bin(a, out);
  45748. if (ret != 0)
  45749. return WC_TEST_RET_ENC_EC(ret);
  45750. if (XMEMCMP(exp, out, sizeof(exp)) != 0)
  45751. return WC_TEST_RET_ENC_NC;
  45752. return 0;
  45753. }
  45754. #endif
  45755. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  45756. static wc_test_ret_t mp_test_param(mp_int* a, mp_int* b, mp_int* r, WC_RNG* rng)
  45757. {
  45758. byte buffer[16];
  45759. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  45760. char hexStr[] = "abcdef0123456789";
  45761. #ifndef WOLFSSL_SP_INT_NEGATIVE
  45762. char negStr[] = "-1234";
  45763. #endif
  45764. #endif
  45765. #if !defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_KEY_GEN) || \
  45766. defined(HAVE_COMP_KEY)
  45767. char decStr[] = "0987654321";
  45768. #endif
  45769. wc_test_ret_t ret;
  45770. #ifdef WOLFSSL_SP_MATH_ALL
  45771. mp_digit rho;
  45772. int size;
  45773. #endif
  45774. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  45775. int result;
  45776. #endif
  45777. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  45778. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  45779. mp_digit rd;
  45780. #endif
  45781. (void)rng;
  45782. (void)r;
  45783. ret = mp_init(NULL);
  45784. if (ret != MP_VAL)
  45785. return WC_TEST_RET_ENC_EC(ret);
  45786. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  45787. ret = mp_init_multi(NULL, NULL, NULL, NULL, NULL, NULL);
  45788. if (ret != MP_OKAY)
  45789. return WC_TEST_RET_ENC_EC(ret);
  45790. #endif
  45791. mp_free(NULL);
  45792. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || !defined(NO_DH) || defined(HAVE_ECC)
  45793. ret = mp_grow(NULL, 1);
  45794. if (ret != MP_VAL)
  45795. return WC_TEST_RET_ENC_EC(ret);
  45796. #ifdef WOLFSSL_SP_MATH
  45797. ret = mp_grow(a, SP_INT_DIGITS + 1);
  45798. if (ret != MP_MEM)
  45799. return WC_TEST_RET_ENC_EC(ret);
  45800. #endif
  45801. #endif
  45802. mp_clear(NULL);
  45803. ret = mp_abs(NULL, NULL);
  45804. if (ret != MP_VAL)
  45805. return WC_TEST_RET_ENC_EC(ret);
  45806. ret = mp_abs(a, NULL);
  45807. if (ret != MP_VAL)
  45808. return WC_TEST_RET_ENC_EC(ret);
  45809. ret = mp_abs(NULL, b);
  45810. if (ret != MP_VAL)
  45811. return WC_TEST_RET_ENC_EC(ret);
  45812. ret = mp_unsigned_bin_size(NULL);
  45813. if (ret != 0)
  45814. return WC_TEST_RET_ENC_EC(ret);
  45815. /* clear buffer to avoid provoking uninitvar errors. */
  45816. XMEMSET(buffer, 0, sizeof(buffer));
  45817. ret = mp_read_unsigned_bin(NULL, NULL, sizeof(buffer));
  45818. if (ret != MP_VAL)
  45819. return WC_TEST_RET_ENC_EC(ret);
  45820. ret = mp_read_unsigned_bin(NULL, buffer, sizeof(buffer));
  45821. if (ret != MP_VAL)
  45822. return WC_TEST_RET_ENC_EC(ret);
  45823. ret = mp_read_unsigned_bin(a, NULL, sizeof(buffer));
  45824. if (ret != MP_VAL)
  45825. return WC_TEST_RET_ENC_EC(ret);
  45826. ret = mp_read_unsigned_bin(a, buffer, SP_INT_DIGITS * SP_WORD_SIZEOF + 1);
  45827. if (ret != MP_VAL)
  45828. return WC_TEST_RET_ENC_EC(ret);
  45829. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  45830. ret = mp_read_radix(NULL, NULL, 16);
  45831. if (ret != MP_VAL)
  45832. return WC_TEST_RET_ENC_EC(ret);
  45833. ret = mp_read_radix(a, NULL, 16);
  45834. if (ret != MP_VAL)
  45835. return WC_TEST_RET_ENC_EC(ret);
  45836. ret = mp_read_radix(NULL, hexStr, 16);
  45837. if (ret != MP_VAL)
  45838. return WC_TEST_RET_ENC_EC(ret);
  45839. #ifndef WOLFSSL_SP_INT_NEGATIVE
  45840. ret = mp_read_radix(a, negStr, 16);
  45841. if (ret != MP_VAL)
  45842. return WC_TEST_RET_ENC_EC(ret);
  45843. #ifdef WOLFSSL_SP_MATH_ALL
  45844. ret = mp_read_radix(a, negStr, 10);
  45845. if (ret != MP_VAL)
  45846. return WC_TEST_RET_ENC_EC(ret);
  45847. #endif /* WOLFSSL_SP_MATH_ALL */
  45848. #endif /* WOLFSSL_SP_INT_NEGATIVE */
  45849. #endif
  45850. #ifndef WOLFSSL_SP_MATH_ALL
  45851. /* Radix 10 only supported with ALL. */
  45852. ret = mp_read_radix(a, decStr, 10);
  45853. if (ret != MP_VAL)
  45854. return WC_TEST_RET_ENC_EC(ret);
  45855. #endif
  45856. /* Radix 8 not supported SP_INT. */
  45857. ret = mp_read_radix(a, "0123", 8);
  45858. if (ret != MP_VAL)
  45859. return WC_TEST_RET_ENC_EC(ret);
  45860. ret = mp_count_bits(NULL);
  45861. if (ret != 0)
  45862. return WC_TEST_RET_ENC_EC(ret);
  45863. ret = mp_is_bit_set(NULL, 0);
  45864. if (ret != 0)
  45865. return WC_TEST_RET_ENC_EC(ret);
  45866. ret = mp_leading_bit(NULL);
  45867. if (ret != 0)
  45868. return WC_TEST_RET_ENC_EC(ret);
  45869. mp_zero(a);
  45870. ret = mp_leading_bit(a);
  45871. if (ret != 0)
  45872. return WC_TEST_RET_ENC_EC(ret);
  45873. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  45874. defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || \
  45875. !defined(NO_RSA)
  45876. ret = mp_set_bit(NULL, 1);
  45877. if (ret != MP_VAL)
  45878. return WC_TEST_RET_ENC_EC(ret);
  45879. #endif
  45880. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  45881. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  45882. ret = mp_to_unsigned_bin(NULL, NULL);
  45883. if (ret != MP_VAL)
  45884. return WC_TEST_RET_ENC_EC(ret);
  45885. ret = mp_to_unsigned_bin(a, NULL);
  45886. if (ret != MP_VAL)
  45887. return WC_TEST_RET_ENC_EC(ret);
  45888. ret = mp_to_unsigned_bin(NULL, buffer);
  45889. if (ret != MP_VAL)
  45890. return WC_TEST_RET_ENC_EC(ret);
  45891. #endif
  45892. ret = mp_to_unsigned_bin_len(NULL, NULL, 1);
  45893. if (ret != MP_VAL)
  45894. return WC_TEST_RET_ENC_EC(ret);
  45895. ret = mp_to_unsigned_bin_len(a, NULL, 1);
  45896. if (ret != MP_VAL)
  45897. return WC_TEST_RET_ENC_EC(ret);
  45898. ret = mp_to_unsigned_bin_len(NULL, buffer, 1);
  45899. if (ret != MP_VAL)
  45900. return WC_TEST_RET_ENC_EC(ret);
  45901. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  45902. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  45903. ret = mp_to_unsigned_bin_at_pos(0, NULL, NULL);
  45904. if (ret != MP_VAL)
  45905. return WC_TEST_RET_ENC_EC(ret);
  45906. ret = mp_to_unsigned_bin_at_pos(0, a, NULL);
  45907. if (ret != MP_VAL)
  45908. return WC_TEST_RET_ENC_EC(ret);
  45909. ret = mp_to_unsigned_bin_at_pos(0, NULL, buffer);
  45910. if (ret != MP_VAL)
  45911. return WC_TEST_RET_ENC_EC(ret);
  45912. ret = mp_to_unsigned_bin_at_pos(0, a, buffer);
  45913. if (ret != MP_OKAY)
  45914. return WC_TEST_RET_ENC_EC(ret);
  45915. #endif
  45916. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  45917. ret = mp_copy(NULL, NULL);
  45918. if (ret != MP_VAL)
  45919. return WC_TEST_RET_ENC_EC(ret);
  45920. ret = mp_copy(a, NULL);
  45921. if (ret != MP_VAL)
  45922. return WC_TEST_RET_ENC_EC(ret);
  45923. ret = mp_copy(NULL, b);
  45924. if (ret != MP_VAL)
  45925. return WC_TEST_RET_ENC_EC(ret);
  45926. #endif
  45927. #if defined(WOLFSSL_KEY_GEN) || !defined(NO_DH)
  45928. ret = sp_2expt(NULL, 1);
  45929. if (ret != MP_VAL)
  45930. return WC_TEST_RET_ENC_EC(ret);
  45931. #endif
  45932. ret = mp_set(NULL, 0);
  45933. if (ret != MP_VAL)
  45934. return WC_TEST_RET_ENC_EC(ret);
  45935. ret = mp_cmp_d(NULL, 0);
  45936. if (ret != MP_LT)
  45937. return WC_TEST_RET_ENC_EC(ret);
  45938. ret = mp_cmp(NULL, NULL);
  45939. if (ret != MP_EQ)
  45940. return WC_TEST_RET_ENC_NC;
  45941. ret = mp_cmp(a, NULL);
  45942. if (ret != MP_GT)
  45943. return WC_TEST_RET_ENC_NC;
  45944. ret = mp_cmp(NULL, b);
  45945. if (ret != MP_LT)
  45946. return WC_TEST_RET_ENC_NC;
  45947. #ifdef WOLFSSL_SP_MATH_ALL
  45948. mp_rshd(NULL, 1);
  45949. #endif
  45950. mp_zero(NULL);
  45951. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  45952. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  45953. ret = mp_lshd(NULL, 0);
  45954. if (ret != MP_VAL)
  45955. return WC_TEST_RET_ENC_EC(ret);
  45956. ret = mp_lshd(a, SP_INT_DIGITS + 1);
  45957. if (ret != MP_VAL)
  45958. return WC_TEST_RET_ENC_EC(ret);
  45959. #endif
  45960. #if defined(WOLFSSL_SP_MATH_ALL)
  45961. ret = mp_div(NULL, NULL, a, b);
  45962. if (ret != MP_VAL)
  45963. return WC_TEST_RET_ENC_EC(ret);
  45964. ret = mp_div(a, NULL, a, b);
  45965. if (ret != MP_VAL)
  45966. return WC_TEST_RET_ENC_EC(ret);
  45967. ret = mp_div(NULL, b, a, b);
  45968. if (ret != MP_VAL)
  45969. return WC_TEST_RET_ENC_EC(ret);
  45970. ret = mp_div(a, b, NULL, NULL);
  45971. if (ret != MP_VAL)
  45972. return WC_TEST_RET_ENC_EC(ret);
  45973. #endif
  45974. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  45975. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  45976. ret = mp_mod(NULL, NULL, NULL);
  45977. if (ret != MP_VAL)
  45978. return WC_TEST_RET_ENC_EC(ret);
  45979. ret = mp_mod(a, NULL, NULL);
  45980. if (ret != MP_VAL)
  45981. return WC_TEST_RET_ENC_EC(ret);
  45982. ret = mp_mod(NULL, b, NULL);
  45983. if (ret != MP_VAL)
  45984. return WC_TEST_RET_ENC_EC(ret);
  45985. ret = mp_mod(NULL, NULL, r);
  45986. if (ret != MP_VAL)
  45987. return WC_TEST_RET_ENC_EC(ret);
  45988. ret = mp_mod(a, b, NULL);
  45989. if (ret != MP_VAL)
  45990. return WC_TEST_RET_ENC_EC(ret);
  45991. ret = mp_mod(a, NULL, r);
  45992. if (ret != MP_VAL)
  45993. return WC_TEST_RET_ENC_EC(ret);
  45994. ret = mp_mod(NULL, b, r);
  45995. if (ret != MP_VAL)
  45996. return WC_TEST_RET_ENC_EC(ret);
  45997. #endif
  45998. #if !defined(NO_RSA) || defined(WOLFSSL_SP_MATH_ALL)
  45999. ret = mp_set_int(NULL, 0);
  46000. if (ret != MP_VAL)
  46001. return WC_TEST_RET_ENC_EC(ret);
  46002. #endif
  46003. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  46004. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  46005. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  46006. if (ret != MP_VAL)
  46007. return WC_TEST_RET_ENC_EC(ret);
  46008. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  46009. if (ret != MP_VAL)
  46010. return WC_TEST_RET_ENC_EC(ret);
  46011. ret = mp_exptmod_ex(NULL, a, 1, NULL, NULL);
  46012. if (ret != MP_VAL)
  46013. return WC_TEST_RET_ENC_EC(ret);
  46014. ret = mp_exptmod_ex(NULL, NULL, 1, a, NULL);
  46015. if (ret != MP_VAL)
  46016. return WC_TEST_RET_ENC_EC(ret);
  46017. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  46018. if (ret != MP_VAL)
  46019. return WC_TEST_RET_ENC_EC(ret);
  46020. ret = mp_exptmod_ex(a, a, 1, a, NULL);
  46021. if (ret != MP_VAL)
  46022. return WC_TEST_RET_ENC_EC(ret);
  46023. ret = mp_exptmod_ex(a, a, 1, NULL, a);
  46024. if (ret != MP_VAL)
  46025. return WC_TEST_RET_ENC_EC(ret);
  46026. ret = mp_exptmod_ex(a, NULL, 1, a, a);
  46027. if (ret != MP_VAL)
  46028. return WC_TEST_RET_ENC_EC(ret);
  46029. ret = mp_exptmod_ex(NULL, a, 1, a, a);
  46030. if (ret != MP_VAL)
  46031. return WC_TEST_RET_ENC_EC(ret);
  46032. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  46033. if (ret != MP_VAL)
  46034. return WC_TEST_RET_ENC_EC(ret);
  46035. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  46036. if (ret != MP_VAL)
  46037. return WC_TEST_RET_ENC_EC(ret);
  46038. ret = mp_exptmod_nct(NULL, a, NULL, NULL);
  46039. if (ret != MP_VAL)
  46040. return WC_TEST_RET_ENC_EC(ret);
  46041. ret = mp_exptmod_nct(NULL, NULL, a, NULL);
  46042. if (ret != MP_VAL)
  46043. return WC_TEST_RET_ENC_EC(ret);
  46044. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  46045. if (ret != MP_VAL)
  46046. return WC_TEST_RET_ENC_EC(ret);
  46047. ret = mp_exptmod_nct(a, a, a, NULL);
  46048. if (ret != MP_VAL)
  46049. return WC_TEST_RET_ENC_EC(ret);
  46050. ret = mp_exptmod_nct(a, a, NULL, a);
  46051. if (ret != MP_VAL)
  46052. return WC_TEST_RET_ENC_EC(ret);
  46053. ret = mp_exptmod_nct(a, NULL, a, a);
  46054. if (ret != MP_VAL)
  46055. return WC_TEST_RET_ENC_EC(ret);
  46056. ret = mp_exptmod_nct(NULL, a, a, a);
  46057. if (ret != MP_VAL)
  46058. return WC_TEST_RET_ENC_EC(ret);
  46059. #endif
  46060. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  46061. !defined(WC_NO_RNG)
  46062. ret = mp_rand_prime(NULL, 32, NULL, NULL);
  46063. if (ret != MP_VAL)
  46064. return WC_TEST_RET_ENC_EC(ret);
  46065. ret = mp_rand_prime(a, 32, NULL, NULL);
  46066. if (ret != MP_VAL)
  46067. return WC_TEST_RET_ENC_EC(ret);
  46068. ret = mp_rand_prime(NULL, 32, rng, NULL);
  46069. if (ret != MP_VAL)
  46070. return WC_TEST_RET_ENC_EC(ret);
  46071. ret = mp_rand_prime(a, 0, rng, NULL);
  46072. if (ret != MP_VAL)
  46073. return WC_TEST_RET_ENC_EC(ret);
  46074. #endif
  46075. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  46076. ret = mp_mul(NULL, NULL, NULL);
  46077. if (ret != MP_VAL)
  46078. return WC_TEST_RET_ENC_EC(ret);
  46079. ret = mp_mul(a, NULL, NULL);
  46080. if (ret != MP_VAL)
  46081. return WC_TEST_RET_ENC_EC(ret);
  46082. ret = mp_mul(NULL, b, NULL);
  46083. if (ret != MP_VAL)
  46084. return WC_TEST_RET_ENC_EC(ret);
  46085. ret = mp_mul(NULL, NULL, r);
  46086. if (ret != MP_VAL)
  46087. return WC_TEST_RET_ENC_EC(ret);
  46088. ret = mp_mul(a, b, NULL);
  46089. if (ret != MP_VAL)
  46090. return WC_TEST_RET_ENC_EC(ret);
  46091. ret = mp_mul(a, NULL, r);
  46092. if (ret != MP_VAL)
  46093. return WC_TEST_RET_ENC_EC(ret);
  46094. ret = mp_mul(NULL, b, r);
  46095. if (ret != MP_VAL)
  46096. return WC_TEST_RET_ENC_EC(ret);
  46097. #endif
  46098. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  46099. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  46100. ret = mp_sqr(NULL, NULL);
  46101. if (ret != MP_VAL)
  46102. return WC_TEST_RET_ENC_EC(ret);
  46103. ret = mp_sqr(a, NULL);
  46104. if (ret != MP_VAL)
  46105. return WC_TEST_RET_ENC_EC(ret);
  46106. ret = mp_sqr(NULL, r);
  46107. if (ret != MP_VAL)
  46108. return WC_TEST_RET_ENC_EC(ret);
  46109. #endif
  46110. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  46111. ret = mp_sqrmod(NULL, NULL, NULL);
  46112. if (ret != MP_VAL)
  46113. return WC_TEST_RET_ENC_EC(ret);
  46114. ret = mp_sqrmod(a, NULL, NULL);
  46115. if (ret != MP_VAL)
  46116. return WC_TEST_RET_ENC_EC(ret);
  46117. ret = mp_sqrmod(NULL, a, NULL);
  46118. if (ret != MP_VAL)
  46119. return WC_TEST_RET_ENC_EC(ret);
  46120. ret = mp_sqrmod(NULL, NULL, a);
  46121. if (ret != MP_VAL)
  46122. return WC_TEST_RET_ENC_EC(ret);
  46123. ret = mp_sqrmod(a, b, NULL);
  46124. if (ret != MP_VAL)
  46125. return WC_TEST_RET_ENC_EC(ret);
  46126. ret = mp_sqrmod(a, NULL, b);
  46127. if (ret != MP_VAL)
  46128. return WC_TEST_RET_ENC_EC(ret);
  46129. ret = mp_sqrmod(NULL, a, b);
  46130. if (ret != MP_VAL)
  46131. return WC_TEST_RET_ENC_EC(ret);
  46132. ret = mp_mulmod(NULL, NULL, NULL, NULL);
  46133. if (ret != MP_VAL)
  46134. return WC_TEST_RET_ENC_EC(ret);
  46135. ret = mp_mulmod(a, NULL, NULL, NULL);
  46136. if (ret != MP_VAL)
  46137. return WC_TEST_RET_ENC_EC(ret);
  46138. ret = mp_mulmod(NULL, a, NULL, NULL);
  46139. if (ret != MP_VAL)
  46140. return WC_TEST_RET_ENC_EC(ret);
  46141. ret = mp_mulmod(NULL, NULL, a, NULL);
  46142. if (ret != MP_VAL)
  46143. return WC_TEST_RET_ENC_EC(ret);
  46144. ret = mp_mulmod(NULL, NULL, NULL, a);
  46145. if (ret != MP_VAL)
  46146. return WC_TEST_RET_ENC_EC(ret);
  46147. ret = mp_mulmod(a, b, b, NULL);
  46148. if (ret != MP_VAL)
  46149. return WC_TEST_RET_ENC_EC(ret);
  46150. ret = mp_mulmod(a, b, NULL, a);
  46151. if (ret != MP_VAL)
  46152. return WC_TEST_RET_ENC_EC(ret);
  46153. ret = mp_mulmod(a, NULL, b, a);
  46154. if (ret != MP_VAL)
  46155. return WC_TEST_RET_ENC_EC(ret);
  46156. ret = mp_mulmod(NULL, b, b, a);
  46157. if (ret != MP_VAL)
  46158. return WC_TEST_RET_ENC_EC(ret);
  46159. #endif
  46160. #if !defined(NO_PWDBASED) || defined(WOLFSSL_KEY_GEN) || !defined(NO_DH) || \
  46161. !defined(NO_RSA) || !defined(NO_DSA)
  46162. ret = mp_add_d(NULL, 1, NULL);
  46163. if (ret != MP_VAL)
  46164. return WC_TEST_RET_ENC_EC(ret);
  46165. ret = mp_add_d(a, 1, NULL);
  46166. if (ret != MP_VAL)
  46167. return WC_TEST_RET_ENC_EC(ret);
  46168. ret = mp_add_d(NULL, 1, b);
  46169. if (ret != MP_VAL)
  46170. return WC_TEST_RET_ENC_EC(ret);
  46171. #endif
  46172. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  46173. !defined(NO_DH) || defined(HAVE_ECC) || !defined(NO_DSA)
  46174. ret = mp_sub_d(NULL, 1, NULL);
  46175. if (ret != MP_VAL)
  46176. return WC_TEST_RET_ENC_EC(ret);
  46177. ret = mp_sub_d(a, 1, NULL);
  46178. if (ret != MP_VAL)
  46179. return WC_TEST_RET_ENC_EC(ret);
  46180. ret = mp_sub_d(NULL, 1, b);
  46181. if (ret != MP_VAL)
  46182. return WC_TEST_RET_ENC_EC(ret);
  46183. #endif
  46184. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  46185. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  46186. ret = mp_div_d(NULL, 0, NULL, NULL);
  46187. if (ret != MP_VAL)
  46188. return WC_TEST_RET_ENC_EC(ret);
  46189. ret = mp_div_d(a, 0, NULL, NULL);
  46190. if (ret != MP_VAL)
  46191. return WC_TEST_RET_ENC_EC(ret);
  46192. ret = mp_div_d(NULL, 1, NULL, NULL);
  46193. if (ret != MP_VAL)
  46194. return WC_TEST_RET_ENC_EC(ret);
  46195. #endif
  46196. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  46197. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  46198. ret = mp_mod_d(NULL, 0, NULL);
  46199. if (ret != MP_VAL)
  46200. return WC_TEST_RET_ENC_EC(ret);
  46201. ret = mp_mod_d(a, 0, NULL);
  46202. if (ret != MP_VAL)
  46203. return WC_TEST_RET_ENC_EC(ret);
  46204. ret = mp_mod_d(NULL, 0, &rd);
  46205. if (ret != MP_VAL)
  46206. return WC_TEST_RET_ENC_EC(ret);
  46207. #endif
  46208. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  46209. ret = mp_gcd(NULL, NULL, NULL);
  46210. if (ret != MP_VAL)
  46211. return WC_TEST_RET_ENC_EC(ret);
  46212. ret = mp_gcd(a, NULL, NULL);
  46213. if (ret != MP_VAL)
  46214. return WC_TEST_RET_ENC_EC(ret);
  46215. ret = mp_gcd(NULL, a, NULL);
  46216. if (ret != MP_VAL)
  46217. return WC_TEST_RET_ENC_EC(ret);
  46218. ret = mp_gcd(NULL, NULL, a);
  46219. if (ret != MP_VAL)
  46220. return WC_TEST_RET_ENC_EC(ret);
  46221. ret = mp_gcd(a, b, NULL);
  46222. if (ret != MP_VAL)
  46223. return WC_TEST_RET_ENC_EC(ret);
  46224. ret = mp_gcd(a, NULL, b);
  46225. if (ret != MP_VAL)
  46226. return WC_TEST_RET_ENC_EC(ret);
  46227. ret = mp_gcd(NULL, a, b);
  46228. if (ret != MP_VAL)
  46229. return WC_TEST_RET_ENC_EC(ret);
  46230. #endif
  46231. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  46232. ret = mp_div_2_mod_ct(NULL, NULL, NULL);
  46233. if (ret != MP_VAL)
  46234. return WC_TEST_RET_ENC_EC(ret);
  46235. ret = mp_div_2_mod_ct(a, NULL, NULL);
  46236. if (ret != MP_VAL)
  46237. return WC_TEST_RET_ENC_EC(ret);
  46238. ret = mp_div_2_mod_ct(NULL, b, NULL);
  46239. if (ret != MP_VAL)
  46240. return WC_TEST_RET_ENC_EC(ret);
  46241. ret = mp_div_2_mod_ct(NULL, NULL, a);
  46242. if (ret != MP_VAL)
  46243. return WC_TEST_RET_ENC_EC(ret);
  46244. ret = mp_div_2_mod_ct(a, b, NULL);
  46245. if (ret != MP_VAL)
  46246. return WC_TEST_RET_ENC_EC(ret);
  46247. ret = mp_div_2_mod_ct(a, b, NULL);
  46248. if (ret != MP_VAL)
  46249. return WC_TEST_RET_ENC_EC(ret);
  46250. ret = mp_div_2_mod_ct(NULL, b, a);
  46251. if (ret != MP_VAL)
  46252. return WC_TEST_RET_ENC_EC(ret);
  46253. ret = mp_div_2(NULL, NULL);
  46254. if (ret != MP_VAL)
  46255. return WC_TEST_RET_ENC_EC(ret);
  46256. ret = mp_div_2(a, NULL);
  46257. if (ret != MP_VAL)
  46258. return WC_TEST_RET_ENC_EC(ret);
  46259. ret = mp_div_2(NULL, a);
  46260. if (ret != MP_VAL)
  46261. return WC_TEST_RET_ENC_EC(ret);
  46262. #endif
  46263. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  46264. defined(HAVE_ECC) || !defined(NO_DSA) || defined(OPENSSL_EXTRA)
  46265. ret = mp_invmod(NULL, NULL, NULL);
  46266. if (ret != MP_VAL)
  46267. return WC_TEST_RET_ENC_EC(ret);
  46268. ret = mp_invmod(a, NULL, NULL);
  46269. if (ret != MP_VAL)
  46270. return WC_TEST_RET_ENC_EC(ret);
  46271. ret = mp_invmod(NULL, b, NULL);
  46272. if (ret != MP_VAL)
  46273. return WC_TEST_RET_ENC_EC(ret);
  46274. ret = mp_invmod(NULL, NULL, a);
  46275. if (ret != MP_VAL)
  46276. return WC_TEST_RET_ENC_EC(ret);
  46277. ret = mp_invmod(a, b, NULL);
  46278. if (ret != MP_VAL)
  46279. return WC_TEST_RET_ENC_EC(ret);
  46280. ret = mp_invmod(a, NULL, a);
  46281. if (ret != MP_VAL)
  46282. return WC_TEST_RET_ENC_EC(ret);
  46283. ret = mp_invmod(NULL, b, a);
  46284. if (ret != MP_VAL)
  46285. return WC_TEST_RET_ENC_EC(ret);
  46286. #endif
  46287. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  46288. ret = mp_invmod_mont_ct(NULL, NULL, NULL, 1);
  46289. if (ret != MP_VAL)
  46290. return WC_TEST_RET_ENC_EC(ret);
  46291. ret = mp_invmod_mont_ct(a, NULL, NULL, 1);
  46292. if (ret != MP_VAL)
  46293. return WC_TEST_RET_ENC_EC(ret);
  46294. ret = mp_invmod_mont_ct(NULL, b, NULL, 1);
  46295. if (ret != MP_VAL)
  46296. return WC_TEST_RET_ENC_EC(ret);
  46297. ret = mp_invmod_mont_ct(NULL, NULL, a, 1);
  46298. if (ret != MP_VAL)
  46299. return WC_TEST_RET_ENC_EC(ret);
  46300. ret = mp_invmod_mont_ct(a, b, NULL, 1);
  46301. if (ret != MP_VAL)
  46302. return WC_TEST_RET_ENC_EC(ret);
  46303. ret = mp_invmod_mont_ct(a, NULL, a, 1);
  46304. if (ret != MP_VAL)
  46305. return WC_TEST_RET_ENC_EC(ret);
  46306. ret = mp_invmod_mont_ct(NULL, b, a, 1);
  46307. if (ret != MP_VAL)
  46308. return WC_TEST_RET_ENC_EC(ret);
  46309. #endif
  46310. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  46311. ret = mp_lcm(NULL, NULL, NULL);
  46312. if (ret != MP_VAL)
  46313. return WC_TEST_RET_ENC_EC(ret);
  46314. ret = mp_lcm(a, NULL, NULL);
  46315. if (ret != MP_VAL)
  46316. return WC_TEST_RET_ENC_EC(ret);
  46317. ret = mp_lcm(NULL, b, NULL);
  46318. if (ret != MP_VAL)
  46319. return WC_TEST_RET_ENC_EC(ret);
  46320. ret = mp_lcm(NULL, NULL, a);
  46321. if (ret != MP_VAL)
  46322. return WC_TEST_RET_ENC_EC(ret);
  46323. ret = mp_lcm(a, b, NULL);
  46324. if (ret != MP_VAL)
  46325. return WC_TEST_RET_ENC_EC(ret);
  46326. ret = mp_lcm(a, NULL, a);
  46327. if (ret != MP_VAL)
  46328. return WC_TEST_RET_ENC_EC(ret);
  46329. ret = mp_lcm(NULL, b, a);
  46330. if (ret != MP_VAL)
  46331. return WC_TEST_RET_ENC_EC(ret);
  46332. #endif
  46333. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  46334. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  46335. if (ret != MP_VAL)
  46336. return WC_TEST_RET_ENC_EC(ret);
  46337. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  46338. if (ret != MP_VAL)
  46339. return WC_TEST_RET_ENC_EC(ret);
  46340. ret = mp_exptmod_ex(NULL, b, 1, NULL, NULL);
  46341. if (ret != MP_VAL)
  46342. return WC_TEST_RET_ENC_EC(ret);
  46343. ret = mp_exptmod_ex(NULL, NULL, 1, b, NULL);
  46344. if (ret != MP_VAL)
  46345. return WC_TEST_RET_ENC_EC(ret);
  46346. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  46347. if (ret != MP_VAL)
  46348. return WC_TEST_RET_ENC_EC(ret);
  46349. ret = mp_exptmod_ex(a, b, 1, b, NULL);
  46350. if (ret != MP_VAL)
  46351. return WC_TEST_RET_ENC_EC(ret);
  46352. ret = mp_exptmod_ex(a, b, 1, NULL, a);
  46353. if (ret != MP_VAL)
  46354. return WC_TEST_RET_ENC_EC(ret);
  46355. ret = mp_exptmod_ex(a, NULL, 1, b, a);
  46356. if (ret != MP_VAL)
  46357. return WC_TEST_RET_ENC_EC(ret);
  46358. ret = mp_exptmod_ex(NULL, b, 1, b, a);
  46359. if (ret != MP_VAL)
  46360. return WC_TEST_RET_ENC_EC(ret);
  46361. ret = mp_exptmod(NULL, NULL, NULL, NULL);
  46362. if (ret != MP_VAL)
  46363. return WC_TEST_RET_ENC_EC(ret);
  46364. ret = mp_exptmod(a, NULL, NULL, NULL);
  46365. if (ret != MP_VAL)
  46366. return WC_TEST_RET_ENC_EC(ret);
  46367. ret = mp_exptmod(NULL, b, NULL, NULL);
  46368. if (ret != MP_VAL)
  46369. return WC_TEST_RET_ENC_EC(ret);
  46370. ret = mp_exptmod(NULL, NULL, b, NULL);
  46371. if (ret != MP_VAL)
  46372. return WC_TEST_RET_ENC_EC(ret);
  46373. ret = mp_exptmod(NULL, NULL, NULL, a);
  46374. if (ret != MP_VAL)
  46375. return WC_TEST_RET_ENC_EC(ret);
  46376. ret = mp_exptmod(a, b, b, NULL);
  46377. if (ret != MP_VAL)
  46378. return WC_TEST_RET_ENC_EC(ret);
  46379. ret = mp_exptmod(a, b, NULL, a);
  46380. if (ret != MP_VAL)
  46381. return WC_TEST_RET_ENC_EC(ret);
  46382. ret = mp_exptmod(a, NULL, b, a);
  46383. if (ret != MP_VAL)
  46384. return WC_TEST_RET_ENC_EC(ret);
  46385. ret = mp_exptmod(NULL, b, b, a);
  46386. if (ret != MP_VAL)
  46387. return WC_TEST_RET_ENC_EC(ret);
  46388. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  46389. if (ret != MP_VAL)
  46390. return WC_TEST_RET_ENC_EC(ret);
  46391. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  46392. if (ret != MP_VAL)
  46393. return WC_TEST_RET_ENC_EC(ret);
  46394. ret = mp_exptmod_nct(NULL, b, NULL, NULL);
  46395. if (ret != MP_VAL)
  46396. return WC_TEST_RET_ENC_EC(ret);
  46397. ret = mp_exptmod_nct(NULL, NULL, b, NULL);
  46398. if (ret != MP_VAL)
  46399. return WC_TEST_RET_ENC_EC(ret);
  46400. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  46401. if (ret != MP_VAL)
  46402. return WC_TEST_RET_ENC_EC(ret);
  46403. ret = mp_exptmod_nct(a, b, b, NULL);
  46404. if (ret != MP_VAL)
  46405. return WC_TEST_RET_ENC_EC(ret);
  46406. ret = mp_exptmod_nct(a, b, NULL, a);
  46407. if (ret != MP_VAL)
  46408. return WC_TEST_RET_ENC_EC(ret);
  46409. ret = mp_exptmod_nct(a, NULL, b, a);
  46410. if (ret != MP_VAL)
  46411. return WC_TEST_RET_ENC_EC(ret);
  46412. ret = mp_exptmod_nct(NULL, b, b, a);
  46413. if (ret != MP_VAL)
  46414. return WC_TEST_RET_ENC_EC(ret);
  46415. #endif
  46416. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  46417. ret = mp_cnt_lsb(NULL);
  46418. if (ret != 0)
  46419. return WC_TEST_RET_ENC_EC(ret);
  46420. #endif
  46421. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  46422. ret = mp_prime_is_prime(NULL, 1, NULL);
  46423. if (ret != MP_VAL)
  46424. return WC_TEST_RET_ENC_EC(ret);
  46425. ret = mp_prime_is_prime(a, 1, NULL);
  46426. if (ret != MP_VAL)
  46427. return WC_TEST_RET_ENC_EC(ret);
  46428. ret = mp_prime_is_prime(NULL, 1, &result);
  46429. if (ret != MP_VAL)
  46430. return WC_TEST_RET_ENC_EC(ret);
  46431. ret = mp_prime_is_prime(a, 0, &result);
  46432. if (ret != MP_VAL)
  46433. return WC_TEST_RET_ENC_EC(ret);
  46434. ret = mp_prime_is_prime(a, 1024, &result);
  46435. if (ret != MP_VAL)
  46436. return WC_TEST_RET_ENC_EC(ret);
  46437. ret = mp_prime_is_prime_ex(NULL, 1, NULL, NULL);
  46438. if (ret != MP_VAL)
  46439. return WC_TEST_RET_ENC_EC(ret);
  46440. ret = mp_prime_is_prime_ex(a, 1, NULL, NULL);
  46441. if (ret != MP_VAL)
  46442. return WC_TEST_RET_ENC_EC(ret);
  46443. ret = mp_prime_is_prime_ex(NULL, 1, &result, NULL);
  46444. if (ret != MP_VAL)
  46445. return WC_TEST_RET_ENC_EC(ret);
  46446. ret = mp_prime_is_prime_ex(NULL, 1, NULL, rng);
  46447. if (ret != MP_VAL)
  46448. return WC_TEST_RET_ENC_EC(ret);
  46449. ret = mp_prime_is_prime_ex(a, 1, &result, NULL);
  46450. if (ret != MP_VAL)
  46451. return WC_TEST_RET_ENC_EC(ret);
  46452. ret = mp_prime_is_prime_ex(a, 1, NULL, rng);
  46453. if (ret != MP_VAL)
  46454. return WC_TEST_RET_ENC_EC(ret);
  46455. ret = mp_prime_is_prime_ex(NULL, 1, &result, rng);
  46456. if (ret != MP_VAL)
  46457. return WC_TEST_RET_ENC_EC(ret);
  46458. #endif
  46459. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || !defined(NO_DSA)
  46460. ret = mp_exch(NULL, NULL);
  46461. if (ret != MP_VAL)
  46462. return WC_TEST_RET_ENC_EC(ret);
  46463. ret = mp_exch(a, NULL);
  46464. if (ret != MP_VAL)
  46465. return WC_TEST_RET_ENC_EC(ret);
  46466. ret = mp_exch(NULL, b);
  46467. if (ret != MP_VAL)
  46468. return WC_TEST_RET_ENC_EC(ret);
  46469. #endif
  46470. #if (defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)) || \
  46471. defined(WOLFSSL_SP_MATH_ALL)
  46472. ret = mp_mul_d(NULL, 1, NULL);
  46473. if (ret != MP_VAL)
  46474. return WC_TEST_RET_ENC_EC(ret);
  46475. ret = mp_mul_d(a, 1, NULL);
  46476. if (ret != MP_VAL)
  46477. return WC_TEST_RET_ENC_EC(ret);
  46478. ret = mp_mul_d(NULL, 1, b);
  46479. if (ret != MP_VAL)
  46480. return WC_TEST_RET_ENC_EC(ret);
  46481. #endif
  46482. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  46483. ret = mp_add(NULL, NULL, NULL);
  46484. if (ret != MP_VAL)
  46485. return WC_TEST_RET_ENC_EC(ret);
  46486. ret = mp_add(a, NULL, NULL);
  46487. if (ret != MP_VAL)
  46488. return WC_TEST_RET_ENC_EC(ret);
  46489. ret = mp_add(NULL, b, NULL);
  46490. if (ret != MP_VAL)
  46491. return WC_TEST_RET_ENC_EC(ret);
  46492. ret = mp_add(NULL, NULL, r);
  46493. if (ret != MP_VAL)
  46494. return WC_TEST_RET_ENC_EC(ret);
  46495. ret = mp_add(a, b, NULL);
  46496. if (ret != MP_VAL)
  46497. return WC_TEST_RET_ENC_EC(ret);
  46498. ret = mp_add(a, NULL, r);
  46499. if (ret != MP_VAL)
  46500. return WC_TEST_RET_ENC_EC(ret);
  46501. ret = mp_add(NULL, b, r);
  46502. if (ret != MP_VAL)
  46503. return WC_TEST_RET_ENC_EC(ret);
  46504. #endif
  46505. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  46506. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  46507. ret = mp_sub(NULL, NULL, NULL);
  46508. if (ret != MP_VAL)
  46509. return WC_TEST_RET_ENC_EC(ret);
  46510. ret = mp_sub(a, NULL, NULL);
  46511. if (ret != MP_VAL)
  46512. return WC_TEST_RET_ENC_EC(ret);
  46513. ret = mp_sub(NULL, b, NULL);
  46514. if (ret != MP_VAL)
  46515. return WC_TEST_RET_ENC_EC(ret);
  46516. ret = mp_sub(NULL, NULL, r);
  46517. if (ret != MP_VAL)
  46518. return WC_TEST_RET_ENC_EC(ret);
  46519. ret = mp_sub(a, b, NULL);
  46520. if (ret != MP_VAL)
  46521. return WC_TEST_RET_ENC_EC(ret);
  46522. ret = mp_sub(a, NULL, r);
  46523. if (ret != MP_VAL)
  46524. return WC_TEST_RET_ENC_EC(ret);
  46525. ret = mp_sub(NULL, b, r);
  46526. if (ret != MP_VAL)
  46527. return WC_TEST_RET_ENC_EC(ret);
  46528. #endif
  46529. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined(WOLFSSL_SP_MATH) && \
  46530. defined(WOLFSSL_CUSTOM_CURVES))
  46531. ret = mp_addmod(NULL, NULL, NULL, NULL);
  46532. if (ret != MP_VAL)
  46533. return WC_TEST_RET_ENC_EC(ret);
  46534. ret = mp_addmod(a, NULL, NULL, NULL);
  46535. if (ret != MP_VAL)
  46536. return WC_TEST_RET_ENC_EC(ret);
  46537. ret = mp_addmod(NULL, b, NULL, NULL);
  46538. if (ret != MP_VAL)
  46539. return WC_TEST_RET_ENC_EC(ret);
  46540. ret = mp_addmod(NULL, NULL, b, NULL);
  46541. if (ret != MP_VAL)
  46542. return WC_TEST_RET_ENC_EC(ret);
  46543. ret = mp_addmod(NULL, NULL, NULL, a);
  46544. if (ret != MP_VAL)
  46545. return WC_TEST_RET_ENC_EC(ret);
  46546. ret = mp_addmod(a, b, b, NULL);
  46547. if (ret != MP_VAL)
  46548. return WC_TEST_RET_ENC_EC(ret);
  46549. ret = mp_addmod(a, b, NULL, a);
  46550. if (ret != MP_VAL)
  46551. return WC_TEST_RET_ENC_EC(ret);
  46552. ret = mp_addmod(a, NULL, b, a);
  46553. if (ret != MP_VAL)
  46554. return WC_TEST_RET_ENC_EC(ret);
  46555. ret = mp_addmod(NULL, b, b, a);
  46556. if (ret != MP_VAL)
  46557. return WC_TEST_RET_ENC_EC(ret);
  46558. #endif
  46559. #ifdef WOLFSSL_SP_MATH_ALL
  46560. ret = mp_submod(NULL, NULL, NULL, NULL);
  46561. if (ret != MP_VAL)
  46562. return WC_TEST_RET_ENC_EC(ret);
  46563. ret = mp_submod(a, NULL, NULL, NULL);
  46564. if (ret != MP_VAL)
  46565. return WC_TEST_RET_ENC_EC(ret);
  46566. ret = mp_submod(NULL, b, NULL, NULL);
  46567. if (ret != MP_VAL)
  46568. return WC_TEST_RET_ENC_EC(ret);
  46569. ret = mp_submod(NULL, NULL, b, NULL);
  46570. if (ret != MP_VAL)
  46571. return WC_TEST_RET_ENC_EC(ret);
  46572. ret = mp_submod(NULL, NULL, NULL, a);
  46573. if (ret != MP_VAL)
  46574. return WC_TEST_RET_ENC_EC(ret);
  46575. ret = mp_submod(a, b, b, NULL);
  46576. if (ret != MP_VAL)
  46577. return WC_TEST_RET_ENC_EC(ret);
  46578. ret = mp_submod(a, b, NULL, a);
  46579. if (ret != MP_VAL)
  46580. return WC_TEST_RET_ENC_EC(ret);
  46581. ret = mp_submod(a, NULL, b, a);
  46582. if (ret != MP_VAL)
  46583. return WC_TEST_RET_ENC_EC(ret);
  46584. ret = mp_submod(NULL, b, b, a);
  46585. if (ret != MP_VAL)
  46586. return WC_TEST_RET_ENC_EC(ret);
  46587. #endif
  46588. #ifdef WOLFSSL_SP_MATH_ALL
  46589. ret = mp_div_2d(NULL, 1, a, b);
  46590. if (ret != MP_VAL)
  46591. return WC_TEST_RET_ENC_EC(ret);
  46592. ret = mp_mod_2d(NULL, 1, NULL);
  46593. if (ret != MP_VAL)
  46594. return WC_TEST_RET_ENC_EC(ret);
  46595. ret = mp_mod_2d(a, 1, NULL);
  46596. if (ret != MP_VAL)
  46597. return WC_TEST_RET_ENC_EC(ret);
  46598. ret = mp_mod_2d(NULL, 1, b);
  46599. if (ret != MP_VAL)
  46600. return WC_TEST_RET_ENC_EC(ret);
  46601. ret = mp_mul_2d(NULL, 1, NULL);
  46602. if (ret != MP_VAL)
  46603. return WC_TEST_RET_ENC_EC(ret);
  46604. ret = mp_mul_2d(a, 1, NULL);
  46605. if (ret != MP_VAL)
  46606. return WC_TEST_RET_ENC_EC(ret);
  46607. ret = mp_mul_2d(NULL, 1, b);
  46608. if (ret != MP_VAL)
  46609. return WC_TEST_RET_ENC_EC(ret);
  46610. #endif
  46611. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  46612. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  46613. ret = mp_montgomery_reduce(NULL, NULL, 1);
  46614. if (ret != MP_VAL)
  46615. return WC_TEST_RET_ENC_EC(ret);
  46616. ret = mp_montgomery_reduce(a, NULL, 1);
  46617. if (ret != MP_VAL)
  46618. return WC_TEST_RET_ENC_EC(ret);
  46619. ret = mp_montgomery_reduce(NULL, b, 1);
  46620. if (ret != MP_VAL)
  46621. return WC_TEST_RET_ENC_EC(ret);
  46622. mp_zero(b);
  46623. ret = mp_montgomery_reduce(a, b, 1);
  46624. if (ret != MP_VAL)
  46625. return WC_TEST_RET_ENC_EC(ret);
  46626. #endif
  46627. #ifdef WOLFSSL_SP_MATH_ALL
  46628. ret = mp_montgomery_setup(NULL, NULL);
  46629. if (ret != MP_VAL)
  46630. return WC_TEST_RET_ENC_EC(ret);
  46631. ret = mp_montgomery_setup(a, NULL);
  46632. if (ret != MP_VAL)
  46633. return WC_TEST_RET_ENC_EC(ret);
  46634. ret = mp_montgomery_setup(NULL, &rho);
  46635. if (ret != MP_VAL)
  46636. return WC_TEST_RET_ENC_EC(ret);
  46637. ret = mp_montgomery_calc_normalization(NULL, NULL);
  46638. if (ret != MP_VAL)
  46639. return WC_TEST_RET_ENC_EC(ret);
  46640. ret = mp_montgomery_calc_normalization(a, NULL);
  46641. if (ret != MP_VAL)
  46642. return WC_TEST_RET_ENC_EC(ret);
  46643. ret = mp_montgomery_calc_normalization(NULL, b);
  46644. if (ret != MP_VAL)
  46645. return WC_TEST_RET_ENC_EC(ret);
  46646. #endif
  46647. ret = mp_unsigned_bin_size(NULL);
  46648. if (ret != 0)
  46649. return WC_TEST_RET_ENC_EC(ret);
  46650. #if defined(WC_MP_TO_RADIX) || defined(WOLFSSL_SP_MATH_ALL)
  46651. ret = mp_tohex(NULL, NULL);
  46652. if (ret != MP_VAL)
  46653. return WC_TEST_RET_ENC_EC(ret);
  46654. ret = mp_tohex(a, NULL);
  46655. if (ret != MP_VAL)
  46656. return WC_TEST_RET_ENC_EC(ret);
  46657. ret = mp_tohex(NULL, hexStr);
  46658. if (ret != MP_VAL)
  46659. return WC_TEST_RET_ENC_EC(ret);
  46660. #endif
  46661. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  46662. ret = mp_todecimal(NULL, NULL);
  46663. if (ret != MP_VAL)
  46664. return WC_TEST_RET_ENC_EC(ret);
  46665. ret = mp_todecimal(a, NULL);
  46666. if (ret != MP_VAL)
  46667. return WC_TEST_RET_ENC_EC(ret);
  46668. ret = mp_todecimal(NULL, decStr);
  46669. if (ret != MP_VAL)
  46670. return WC_TEST_RET_ENC_EC(ret);
  46671. #endif
  46672. #ifdef WOLFSSL_SP_MATH_ALL
  46673. ret = mp_toradix(NULL, NULL, MP_RADIX_HEX);
  46674. if (ret != MP_VAL)
  46675. return WC_TEST_RET_ENC_EC(ret);
  46676. ret = mp_toradix(a, NULL, MP_RADIX_HEX);
  46677. if (ret != MP_VAL)
  46678. return WC_TEST_RET_ENC_EC(ret);
  46679. ret = mp_toradix(NULL, hexStr, MP_RADIX_HEX);
  46680. if (ret != MP_VAL)
  46681. return WC_TEST_RET_ENC_EC(ret);
  46682. ret = mp_toradix(a, hexStr, 3);
  46683. if (ret != MP_VAL)
  46684. return WC_TEST_RET_ENC_EC(ret);
  46685. ret = mp_radix_size(NULL, MP_RADIX_HEX, NULL);
  46686. if (ret != MP_VAL)
  46687. return WC_TEST_RET_ENC_EC(ret);
  46688. ret = mp_radix_size(a, MP_RADIX_HEX, NULL);
  46689. if (ret != MP_VAL)
  46690. return WC_TEST_RET_ENC_EC(ret);
  46691. ret = mp_radix_size(NULL, MP_RADIX_HEX, &size);
  46692. if (ret != MP_VAL)
  46693. return WC_TEST_RET_ENC_EC(ret);
  46694. ret = mp_radix_size(a, 3, &size);
  46695. if (ret != MP_VAL)
  46696. return WC_TEST_RET_ENC_EC(ret);
  46697. #endif
  46698. return 0;
  46699. }
  46700. #endif
  46701. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  46702. static wc_test_ret_t mp_test_set_is_bit(mp_int* a)
  46703. {
  46704. int i, j;
  46705. wc_test_ret_t ret;
  46706. mp_zero(a);
  46707. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  46708. if (mp_is_bit_set(a, i))
  46709. return WC_TEST_RET_ENC_NC;
  46710. for (j = 0; j < i; j++) {
  46711. if (!mp_is_bit_set(a, j))
  46712. return WC_TEST_RET_ENC_NC;
  46713. }
  46714. if (mp_set_bit(a, i) != 0)
  46715. return WC_TEST_RET_ENC_NC;
  46716. if (!mp_is_bit_set(a, i))
  46717. return WC_TEST_RET_ENC_NC;
  46718. }
  46719. mp_zero(a);
  46720. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  46721. if (mp_is_bit_set(a, i))
  46722. return WC_TEST_RET_ENC_NC;
  46723. }
  46724. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  46725. mp_zero(a);
  46726. if (mp_set_bit(a, i) != 0)
  46727. return WC_TEST_RET_ENC_NC;
  46728. for (j = 0; j < i; j++) {
  46729. if (mp_is_bit_set(a, j))
  46730. return WC_TEST_RET_ENC_NC;
  46731. }
  46732. if (!mp_is_bit_set(a, i))
  46733. return WC_TEST_RET_ENC_NC;
  46734. }
  46735. #ifdef WOLFSSL_KEY_GEN
  46736. for (i = 0; i < DIGIT_BIT * 2; i++) {
  46737. mp_set(a, 1);
  46738. ret = mp_2expt(a, i);
  46739. if (ret != 0)
  46740. return WC_TEST_RET_ENC_EC(ret);
  46741. for (j = 0; j < i; j++) {
  46742. if (mp_is_bit_set(a, j))
  46743. return WC_TEST_RET_ENC_NC;
  46744. }
  46745. if (!mp_is_bit_set(a, i))
  46746. return WC_TEST_RET_ENC_NC;
  46747. }
  46748. #endif
  46749. #ifdef WOLFSSL_SP_MATH
  46750. mp_zero(a);
  46751. for (j = 1; j <= 3; j++) {
  46752. i = SP_INT_MAX_BITS - j;
  46753. if (mp_is_bit_set(a, i))
  46754. return WC_TEST_RET_ENC_NC;
  46755. if (mp_set_bit(a, i) != 0)
  46756. return WC_TEST_RET_ENC_NC;
  46757. if (!mp_is_bit_set(a, i))
  46758. return WC_TEST_RET_ENC_NC;
  46759. #ifdef WOLFSSL_KEY_GEN
  46760. ret = mp_2expt(a, i);
  46761. if (ret != 0)
  46762. return WC_TEST_RET_ENC_EC(ret);
  46763. if (!mp_is_bit_set(a, i))
  46764. return WC_TEST_RET_ENC_NC;
  46765. #endif
  46766. }
  46767. mp_zero(a);
  46768. for (j = 0; j <= 3; j++) {
  46769. i = SP_INT_MAX_BITS + j;
  46770. if (mp_is_bit_set(a, i))
  46771. return WC_TEST_RET_ENC_NC;
  46772. if (mp_set_bit(a, i) != MP_VAL)
  46773. return WC_TEST_RET_ENC_NC;
  46774. #ifdef WOLFSSL_KEY_GEN
  46775. ret = mp_2expt(a, i);
  46776. if (ret != MP_VAL)
  46777. return WC_TEST_RET_ENC_EC(ret);
  46778. #endif
  46779. }
  46780. #endif
  46781. (void)ret;
  46782. return 0;
  46783. }
  46784. #endif /* !WOLFSSL_SP_MATH || WOLFSSL_SP_MATH_ALL */
  46785. static wc_test_ret_t mp_test_cmp(mp_int* a, mp_int* b)
  46786. {
  46787. wc_test_ret_t ret;
  46788. mp_zero(a);
  46789. mp_zero(b);
  46790. ret = mp_cmp_d(a, 0);
  46791. if (ret != MP_EQ)
  46792. return WC_TEST_RET_ENC_EC(ret);
  46793. ret = mp_cmp_d(a, 1);
  46794. if (ret != MP_LT)
  46795. return WC_TEST_RET_ENC_EC(ret);
  46796. ret = mp_cmp(a, b);
  46797. if (ret != MP_EQ)
  46798. return WC_TEST_RET_ENC_NC;
  46799. mp_set(a, 1);
  46800. ret = mp_cmp_d(a, 0);
  46801. if (ret != MP_GT)
  46802. return WC_TEST_RET_ENC_EC(ret);
  46803. ret = mp_cmp_d(a, 1);
  46804. if (ret != MP_EQ)
  46805. return WC_TEST_RET_ENC_EC(ret);
  46806. ret = mp_cmp_d(a, 2);
  46807. if (ret != MP_LT)
  46808. return WC_TEST_RET_ENC_EC(ret);
  46809. ret = mp_cmp(a, b);
  46810. if (ret != MP_GT)
  46811. return WC_TEST_RET_ENC_NC;
  46812. mp_read_radix(b, "1234567890123456789", MP_RADIX_HEX);
  46813. ret = mp_cmp_d(b, -1);
  46814. if (ret != MP_GT)
  46815. return WC_TEST_RET_ENC_EC(ret);
  46816. ret = mp_cmp(a, b);
  46817. if (ret != MP_LT)
  46818. return WC_TEST_RET_ENC_NC;
  46819. ret = mp_cmp(b, a);
  46820. if (ret != MP_GT)
  46821. return WC_TEST_RET_ENC_NC;
  46822. ret = mp_cmp(b, b);
  46823. if (ret != MP_EQ)
  46824. return WC_TEST_RET_ENC_NC;
  46825. #if (!defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)) || \
  46826. defined(WOLFSSL_SP_INT_NEGATIVE)
  46827. mp_read_radix(a, "-1", MP_RADIX_HEX);
  46828. mp_read_radix(a, "1", MP_RADIX_HEX);
  46829. ret = mp_cmp(a, b);
  46830. if (ret != MP_LT)
  46831. return WC_TEST_RET_ENC_NC;
  46832. ret = mp_cmp(b, a);
  46833. if (ret != MP_GT)
  46834. return WC_TEST_RET_ENC_NC;
  46835. mp_read_radix(b, "-2", MP_RADIX_HEX);
  46836. ret = mp_cmp(a, b);
  46837. if (ret != MP_GT)
  46838. return WC_TEST_RET_ENC_NC;
  46839. ret = mp_cmp(b, a);
  46840. if (ret != MP_LT)
  46841. return WC_TEST_RET_ENC_NC;
  46842. mp_read_radix(a, "-2", MP_RADIX_HEX);
  46843. ret = mp_cmp(a, b);
  46844. if (ret != MP_EQ)
  46845. return WC_TEST_RET_ENC_NC;
  46846. #endif
  46847. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  46848. defined(WOLFSSL_ECC_GEN_REJECT_SAMPLING)
  46849. mp_zero(a);
  46850. mp_zero(b);
  46851. ret = mp_cmp_ct(a, b, 1);
  46852. if (ret != MP_EQ)
  46853. return WC_TEST_RET_ENC_EC(ret);
  46854. ret = mp_cmp_ct(a, a, a->used);
  46855. if (ret != MP_EQ)
  46856. return WC_TEST_RET_ENC_EC(ret);
  46857. #ifdef WOLFSSL_SP_MATH_ALL
  46858. ret = mp_cmp_ct(a, NULL, a->used);
  46859. if (ret != MP_GT)
  46860. return WC_TEST_RET_ENC_EC(ret);
  46861. ret = mp_cmp_ct(NULL, a, a->used);
  46862. if (ret != MP_LT)
  46863. return WC_TEST_RET_ENC_EC(ret);
  46864. #endif
  46865. mp_read_radix(a, "1", MP_RADIX_HEX);
  46866. ret = mp_cmp_ct(a, b, 1);
  46867. if (ret != MP_GT)
  46868. return WC_TEST_RET_ENC_EC(ret);
  46869. ret = mp_cmp_ct(b, a, 1);
  46870. if (ret != MP_LT)
  46871. return WC_TEST_RET_ENC_EC(ret);
  46872. mp_read_radix(a, "0123456789abcdef0123456789abcdef", MP_RADIX_HEX);
  46873. ret = mp_cmp_ct(a, b, a->used);
  46874. if (ret != MP_GT)
  46875. return WC_TEST_RET_ENC_EC(ret);
  46876. ret = mp_cmp_ct(b, a, a->used);
  46877. if (ret != MP_LT)
  46878. return WC_TEST_RET_ENC_EC(ret);
  46879. mp_read_radix(b, "1123456789abcdef0123456789abcdef", MP_RADIX_HEX);
  46880. ret = mp_cmp_ct(b, a, a->used);
  46881. if (ret != MP_GT)
  46882. return WC_TEST_RET_ENC_EC(ret);
  46883. ret = mp_cmp_ct(a, b, a->used);
  46884. if (ret != MP_LT)
  46885. return WC_TEST_RET_ENC_EC(ret);
  46886. mp_read_radix(b, "0123456789abcdef0123456789abcdf0", MP_RADIX_HEX);
  46887. ret = mp_cmp_ct(b, a, a->used);
  46888. if (ret != MP_GT)
  46889. return WC_TEST_RET_ENC_EC(ret);
  46890. ret = mp_cmp_ct(a, b, a->used);
  46891. if (ret != MP_LT)
  46892. return WC_TEST_RET_ENC_EC(ret);
  46893. mp_read_radix(b, "0123456789abcdf0", MP_RADIX_HEX);
  46894. ret = mp_cmp_ct(a, b, a->used);
  46895. if (ret != MP_GT)
  46896. return WC_TEST_RET_ENC_EC(ret);
  46897. ret = mp_cmp_ct(b, a, a->used);
  46898. if (ret != MP_LT)
  46899. return WC_TEST_RET_ENC_EC(ret);
  46900. #endif
  46901. return 0;
  46902. }
  46903. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  46904. static wc_test_ret_t mp_test_shbd(mp_int* a, mp_int* b, WC_RNG* rng)
  46905. {
  46906. wc_test_ret_t ret;
  46907. int i, j, k;
  46908. #ifndef WOLFSSL_SP_MATH
  46909. for (i = 0; i < 10; i++) {
  46910. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  46911. ret = randNum(a, j, rng, NULL);
  46912. if (ret != MP_OKAY)
  46913. return WC_TEST_RET_ENC_EC(ret);
  46914. mp_copy(a, b);
  46915. for (k = 0; k <= DIGIT_BIT * 2; k++) {
  46916. ret = mp_mul_2d(a, k, a);
  46917. if (ret != MP_OKAY)
  46918. return WC_TEST_RET_ENC_EC(ret);
  46919. mp_rshb(a, k);
  46920. ret = mp_cmp(a, b);
  46921. if (ret != MP_EQ)
  46922. return WC_TEST_RET_ENC_NC;
  46923. }
  46924. }
  46925. }
  46926. #endif
  46927. for (i = 0; i < 10; i++) {
  46928. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  46929. ret = randNum(a, j, rng, NULL);
  46930. if (ret != MP_OKAY)
  46931. return WC_TEST_RET_ENC_EC(ret);
  46932. mp_copy(a, b);
  46933. for (k = 0; k < 10; k++) {
  46934. ret = mp_lshd(a, k);
  46935. if (ret != MP_OKAY)
  46936. return WC_TEST_RET_ENC_EC(ret);
  46937. #ifndef WOLFSSL_SP_MATH
  46938. mp_rshd(a, k);
  46939. #else
  46940. mp_rshb(a, k * SP_WORD_SIZE);
  46941. #endif
  46942. ret = mp_cmp(a, b);
  46943. if (ret != MP_EQ)
  46944. return WC_TEST_RET_ENC_NC;
  46945. }
  46946. }
  46947. }
  46948. #ifndef WOLFSSL_SP_MATH
  46949. mp_zero(a);
  46950. mp_rshd(a, 1);
  46951. if (!mp_iszero(a))
  46952. return WC_TEST_RET_ENC_NC;
  46953. mp_set(a, 1);
  46954. mp_rshd(a, 1);
  46955. if (!mp_iszero(a))
  46956. return WC_TEST_RET_ENC_NC;
  46957. mp_set(a, 1);
  46958. mp_rshd(a, 2);
  46959. if (!mp_iszero(a))
  46960. return WC_TEST_RET_ENC_NC;
  46961. #endif
  46962. return 0;
  46963. }
  46964. #endif
  46965. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  46966. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && \
  46967. !defined(WOLFSSL_RSA_PUBLIC_ONLY))
  46968. static wc_test_ret_t mp_test_div(mp_int* a, mp_int* d, mp_int* r, mp_int* rem,
  46969. WC_RNG* rng)
  46970. {
  46971. wc_test_ret_t ret;
  46972. int i, j, k;
  46973. mp_zero(a);
  46974. mp_zero(d);
  46975. ret = mp_div(a, d, r, rem);
  46976. if (ret != MP_VAL)
  46977. return WC_TEST_RET_ENC_EC(ret);
  46978. mp_set(d, 1);
  46979. ret = mp_div(a, d, r, rem);
  46980. if (ret != MP_OKAY)
  46981. return WC_TEST_RET_ENC_EC(ret);
  46982. if (!mp_iszero(r))
  46983. return WC_TEST_RET_ENC_EC(ret);
  46984. if (!mp_iszero(rem))
  46985. return WC_TEST_RET_ENC_EC(ret);
  46986. mp_set(a, 1);
  46987. ret = mp_div(a, d, r, rem);
  46988. if (ret != MP_OKAY)
  46989. return WC_TEST_RET_ENC_EC(ret);
  46990. if (!mp_isone(r))
  46991. return WC_TEST_RET_ENC_EC(ret);
  46992. if (!mp_iszero(rem))
  46993. return WC_TEST_RET_ENC_EC(ret);
  46994. for (i = 0; i < 100; i++) {
  46995. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 2; j++) {
  46996. ret = randNum(d, j, rng, NULL);
  46997. if (ret != MP_OKAY)
  46998. return WC_TEST_RET_ENC_EC(ret);
  46999. for (k = 1; k < (DIGIT_BIT + 7) / 8 * 2 + 1; k++) {
  47000. ret = randNum(a, k, rng, NULL);
  47001. if (ret != MP_OKAY)
  47002. return WC_TEST_RET_ENC_EC(ret);
  47003. ret = mp_div(a, d, NULL, rem);
  47004. if (ret != MP_OKAY)
  47005. return WC_TEST_RET_ENC_EC(ret);
  47006. ret = mp_div(a, d, r, NULL);
  47007. if (ret != MP_OKAY)
  47008. return WC_TEST_RET_ENC_EC(ret);
  47009. ret = mp_div(a, d, r, rem);
  47010. if (ret != MP_OKAY)
  47011. return WC_TEST_RET_ENC_EC(ret);
  47012. mp_mul(r, d, r);
  47013. mp_add(r, rem, r);
  47014. if (mp_cmp(r, a) != MP_EQ)
  47015. return WC_TEST_RET_ENC_NC;
  47016. }
  47017. }
  47018. }
  47019. ret = randNum(d, (DIGIT_BIT + 7) / 8 * 2, rng, NULL);
  47020. if (ret != MP_OKAY)
  47021. return WC_TEST_RET_ENC_EC(ret);
  47022. mp_add(d, d, a);
  47023. mp_set(rem, 1);
  47024. mp_div(a, d, NULL, rem);
  47025. if (ret != MP_OKAY)
  47026. return WC_TEST_RET_ENC_EC(ret);
  47027. if (!mp_iszero(rem))
  47028. return WC_TEST_RET_ENC_EC(ret);
  47029. mp_set(r, 1);
  47030. mp_div(a, d, r, NULL);
  47031. if (ret != MP_OKAY)
  47032. return WC_TEST_RET_ENC_EC(ret);
  47033. if (mp_cmp_d(r, 2) != MP_EQ)
  47034. return WC_TEST_RET_ENC_EC(ret);
  47035. mp_set(r, 1);
  47036. mp_set(rem, 1);
  47037. mp_div(a, d, r, rem);
  47038. if (ret != MP_OKAY)
  47039. return WC_TEST_RET_ENC_EC(ret);
  47040. if (mp_cmp_d(r, 2) != MP_EQ)
  47041. return WC_TEST_RET_ENC_EC(ret);
  47042. if (!mp_iszero(rem))
  47043. return WC_TEST_RET_ENC_EC(ret);
  47044. mp_set(a, 0xfe);
  47045. mp_lshd(a, 3);
  47046. mp_add_d(a, 0xff, a);
  47047. mp_set(d, 0xfe);
  47048. mp_lshd(d, 2);
  47049. ret = mp_div(a, d, r, rem);
  47050. if (ret != MP_OKAY)
  47051. return WC_TEST_RET_ENC_EC(ret);
  47052. mp_mul(r, d, d);
  47053. mp_add(rem, d, d);
  47054. if (mp_cmp(a, d) != MP_EQ)
  47055. return WC_TEST_RET_ENC_NC;
  47056. /* Force (hi | lo) / d to be (d | 0) / d which will would not fit in
  47057. * a digit. So mp_div must detect and handle.
  47058. * For example: 0x800000 / 0x8001, DIGIT_BIT = 8
  47059. */
  47060. mp_zero(a);
  47061. mp_set_bit(a, DIGIT_BIT * 3 - 1);
  47062. mp_zero(d);
  47063. mp_set_bit(d, DIGIT_BIT * 2 - 1);
  47064. mp_add_d(d, 1, d);
  47065. ret = mp_div(a, d, r, rem);
  47066. if (ret != MP_OKAY)
  47067. return WC_TEST_RET_ENC_EC(ret);
  47068. /* Make sure [d | d] / d is handled. */
  47069. mp_zero(a);
  47070. mp_set_bit(a, DIGIT_BIT * 2 - 1);
  47071. mp_set_bit(a, DIGIT_BIT * 1 - 1);
  47072. mp_zero(d);
  47073. mp_set_bit(d, DIGIT_BIT - 1);
  47074. ret = mp_div(a, d, r, rem);
  47075. if (ret != MP_OKAY)
  47076. return WC_TEST_RET_ENC_EC(ret);
  47077. mp_zero(a);
  47078. mp_set_bit(a, DIGIT_BIT);
  47079. mp_set_bit(a, 0);
  47080. mp_zero(d);
  47081. if (mp_cmp(r, a) != MP_EQ)
  47082. return WC_TEST_RET_ENC_NC;
  47083. if (mp_cmp(rem, d) != MP_EQ)
  47084. return WC_TEST_RET_ENC_NC;
  47085. return 0;
  47086. }
  47087. #endif
  47088. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  47089. !defined(WC_NO_RNG)
  47090. static wc_test_ret_t mp_test_prime(mp_int* a, WC_RNG* rng)
  47091. {
  47092. wc_test_ret_t ret;
  47093. int res;
  47094. ret = mp_rand_prime(a, 1, rng, NULL);
  47095. #if defined(WOLFSSL_SP_MATH_ALL)
  47096. if (ret != 0)
  47097. return WC_TEST_RET_ENC_EC(ret);
  47098. #else
  47099. if (ret != MP_VAL)
  47100. return WC_TEST_RET_ENC_NC;
  47101. #endif
  47102. #ifndef WOLFSSL_SP_MATH
  47103. ret = mp_rand_prime(a, -5, rng, NULL);
  47104. if (ret != 0 || (a->dp[0] & 3) != 3)
  47105. return WC_TEST_RET_ENC_NC;
  47106. #endif
  47107. ret = mp_prime_is_prime(a, 1, &res);
  47108. if (ret != MP_OKAY)
  47109. return WC_TEST_RET_ENC_EC(ret);
  47110. #ifndef WOLFSSL_SP_MATH
  47111. if (res != MP_YES)
  47112. return WC_TEST_RET_ENC_EC(res);
  47113. #else
  47114. if (res != MP_NO)
  47115. return WC_TEST_RET_ENC_EC(res);
  47116. #endif
  47117. ret = mp_prime_is_prime(a, 0, &res);
  47118. if (ret != MP_VAL)
  47119. return WC_TEST_RET_ENC_EC(ret);
  47120. ret = mp_prime_is_prime(a, -1, &res);
  47121. if (ret != MP_VAL)
  47122. return WC_TEST_RET_ENC_EC(ret);
  47123. ret = mp_prime_is_prime(a, 257, &res);
  47124. if (ret != MP_VAL)
  47125. return WC_TEST_RET_ENC_EC(ret);
  47126. mp_set(a, 1);
  47127. ret = mp_prime_is_prime(a, 1, &res);
  47128. if (ret != MP_OKAY)
  47129. return WC_TEST_RET_ENC_EC(ret);
  47130. if (res != MP_NO)
  47131. return WC_TEST_RET_ENC_EC(res);
  47132. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  47133. if (ret != MP_OKAY)
  47134. return WC_TEST_RET_ENC_EC(ret);
  47135. if (res != MP_NO)
  47136. return WC_TEST_RET_ENC_EC(res);
  47137. mp_set(a, 2);
  47138. ret = mp_prime_is_prime(a, 1, &res);
  47139. if (ret != MP_OKAY)
  47140. return WC_TEST_RET_ENC_EC(ret);
  47141. if (res != MP_YES)
  47142. return WC_TEST_RET_ENC_EC(res);
  47143. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  47144. if (ret != MP_OKAY)
  47145. return WC_TEST_RET_ENC_EC(ret);
  47146. if (res != MP_YES)
  47147. return WC_TEST_RET_ENC_EC(res);
  47148. mp_set(a, 0xfb);
  47149. ret = mp_prime_is_prime(a, 1, &res);
  47150. if (ret != MP_OKAY)
  47151. return WC_TEST_RET_ENC_EC(ret);
  47152. if (res != MP_YES)
  47153. return WC_TEST_RET_ENC_EC(res);
  47154. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  47155. if (ret != MP_OKAY)
  47156. return WC_TEST_RET_ENC_EC(ret);
  47157. if (res != MP_YES)
  47158. return WC_TEST_RET_ENC_EC(res);
  47159. mp_set(a, 0x6);
  47160. ret = mp_prime_is_prime(a, 1, &res);
  47161. if (ret != MP_OKAY)
  47162. return WC_TEST_RET_ENC_EC(ret);
  47163. if (res != MP_NO)
  47164. return WC_TEST_RET_ENC_EC(res);
  47165. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  47166. if (ret != MP_OKAY)
  47167. return WC_TEST_RET_ENC_EC(ret);
  47168. if (res != MP_NO)
  47169. return WC_TEST_RET_ENC_EC(res);
  47170. mp_set_int(a, 0x655 * 0x65b);
  47171. ret = mp_prime_is_prime(a, 10, &res);
  47172. if (ret != MP_OKAY)
  47173. return WC_TEST_RET_ENC_EC(ret);
  47174. if (res != MP_NO)
  47175. return WC_TEST_RET_ENC_EC(res);
  47176. ret = mp_prime_is_prime_ex(a, 10, &res, rng);
  47177. if (ret != MP_OKAY)
  47178. return WC_TEST_RET_ENC_EC(ret);
  47179. if (res != MP_NO)
  47180. return WC_TEST_RET_ENC_EC(res);
  47181. return 0;
  47182. }
  47183. #endif
  47184. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  47185. static wc_test_ret_t mp_test_lcm_gcd(mp_int* a, mp_int* b, mp_int* r, mp_int* exp,
  47186. WC_RNG* rng)
  47187. {
  47188. wc_test_ret_t ret;
  47189. int i;
  47190. WOLFSSL_SMALL_STACK_STATIC const int kat[][3] = {
  47191. { 1, 1, 1 }, { 2, 1, 2 }, { 1, 2, 2 }, { 2, 4, 4 }, { 4, 2, 4 },
  47192. { 12, 56, 168 }, { 56, 12, 168 }
  47193. };
  47194. (void)exp;
  47195. mp_set(a, 0);
  47196. mp_set(b, 1);
  47197. ret = mp_lcm(a, a, r);
  47198. if (ret != MP_VAL)
  47199. return WC_TEST_RET_ENC_EC(ret);
  47200. ret = mp_lcm(a, b, r);
  47201. if (ret != MP_VAL)
  47202. return WC_TEST_RET_ENC_EC(ret);
  47203. ret = mp_lcm(b, a, r);
  47204. if (ret != MP_VAL)
  47205. return WC_TEST_RET_ENC_EC(ret);
  47206. for (i = 0; i < (int)(sizeof(kat) / sizeof(*kat)); i++) {
  47207. mp_set(a, kat[i][0]);
  47208. mp_set(b, kat[i][1]);
  47209. ret = mp_lcm(a, b, r);
  47210. if (ret != MP_OKAY)
  47211. return WC_TEST_RET_ENC_EC(ret);
  47212. mp_set(exp, kat[i][2]);
  47213. ret = mp_cmp(r, exp);
  47214. if (ret != MP_EQ)
  47215. return WC_TEST_RET_ENC_NC;
  47216. }
  47217. (void)rng;
  47218. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  47219. !defined(WC_NO_RNG)
  47220. ret = mp_rand_prime(a, 20, rng, NULL);
  47221. if (ret != MP_OKAY)
  47222. return WC_TEST_RET_ENC_EC(ret);
  47223. ret = mp_rand_prime(b, 20, rng, NULL);
  47224. if (ret != MP_OKAY)
  47225. return WC_TEST_RET_ENC_EC(ret);
  47226. ret = mp_mul(a, b, exp);
  47227. if (ret != MP_OKAY)
  47228. return WC_TEST_RET_ENC_EC(ret);
  47229. ret = mp_lcm(a, b, r);
  47230. if (ret != MP_OKAY)
  47231. return WC_TEST_RET_ENC_EC(ret);
  47232. ret = mp_cmp(r, exp);
  47233. if (ret != MP_EQ)
  47234. return WC_TEST_RET_ENC_NC;
  47235. ret = mp_lcm(b, a, r);
  47236. if (ret != MP_OKAY)
  47237. return WC_TEST_RET_ENC_EC(ret);
  47238. ret = mp_cmp(r, exp);
  47239. if (ret != MP_EQ)
  47240. return WC_TEST_RET_ENC_NC;
  47241. #endif
  47242. mp_set(a, 11);
  47243. mp_zero(b);
  47244. ret = mp_gcd(a, b, r);
  47245. if (ret != MP_OKAY)
  47246. return WC_TEST_RET_ENC_EC(ret);
  47247. ret = mp_cmp_d(r, 11);
  47248. if (ret != MP_EQ)
  47249. return WC_TEST_RET_ENC_EC(ret);
  47250. ret = mp_gcd(b, a, r);
  47251. if (ret != MP_OKAY)
  47252. return WC_TEST_RET_ENC_EC(ret);
  47253. ret = mp_cmp_d(r, 11);
  47254. if (ret != MP_EQ)
  47255. return WC_TEST_RET_ENC_EC(ret);
  47256. ret = mp_gcd(b, b, r);
  47257. if (ret != MP_VAL)
  47258. return WC_TEST_RET_ENC_EC(ret);
  47259. return 0;
  47260. }
  47261. #endif
  47262. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  47263. defined(WOLFSSL_SP_MATH_ALL)
  47264. static wc_test_ret_t mp_test_mod_2d(mp_int* a, mp_int* r, mp_int* t, WC_RNG* rng)
  47265. {
  47266. wc_test_ret_t ret;
  47267. int i;
  47268. int j;
  47269. mp_set(a, 10);
  47270. ret = mp_mod_2d(a, 0, r);
  47271. if (ret != MP_OKAY)
  47272. return WC_TEST_RET_ENC_EC(ret);
  47273. if (!mp_iszero(r))
  47274. return WC_TEST_RET_ENC_NC;
  47275. ret = mp_mod_2d(a, 1, r);
  47276. if (ret != MP_OKAY)
  47277. return WC_TEST_RET_ENC_EC(ret);
  47278. if (!mp_iszero(r))
  47279. return WC_TEST_RET_ENC_NC;
  47280. ret = mp_mod_2d(a, 2, r);
  47281. if (ret != MP_OKAY)
  47282. return WC_TEST_RET_ENC_EC(ret);
  47283. ret = mp_cmp_d(r, 2);
  47284. if (ret != 0)
  47285. return WC_TEST_RET_ENC_EC(ret);
  47286. for (i = 2; i < 20; i++) {
  47287. ret = randNum(a, i, rng, NULL);
  47288. if (ret != 0)
  47289. return WC_TEST_RET_ENC_EC(ret);
  47290. for (j = 1; j <= mp_count_bits(a); j++) {
  47291. /* Get top part */
  47292. ret = mp_div_2d(a, j, t, NULL);
  47293. if (ret != 0)
  47294. return WC_TEST_RET_ENC_EC(ret);
  47295. ret = mp_mul_2d(t, j, t);
  47296. if (ret != 0)
  47297. return WC_TEST_RET_ENC_EC(ret);
  47298. /* Get bottom part */
  47299. ret = mp_mod_2d(a, j, r);
  47300. if (ret != 0)
  47301. return WC_TEST_RET_ENC_EC(ret);
  47302. /* Reassemble */
  47303. ret = mp_add(t, r, r);
  47304. if (ret != 0)
  47305. return WC_TEST_RET_ENC_EC(ret);
  47306. ret = mp_cmp(a, r);
  47307. if (ret != MP_EQ)
  47308. return WC_TEST_RET_ENC_NC;
  47309. }
  47310. }
  47311. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_SP_INT_NEGATIVE)
  47312. /* Test negative value being moded. */
  47313. for (j = 0; j < 20; j++) {
  47314. ret = randNum(a, 2, rng, NULL);
  47315. if (ret != 0)
  47316. return WC_TEST_RET_ENC_EC(ret);
  47317. a->sign = MP_NEG;
  47318. for (i = 1; i < DIGIT_BIT * 3 + 1; i++) {
  47319. ret = mp_mod_2d(a, i, r);
  47320. if (ret != 0)
  47321. return WC_TEST_RET_ENC_EC(ret);
  47322. mp_zero(t);
  47323. ret = mp_set_bit(t, i);
  47324. if (ret != 0)
  47325. return WC_TEST_RET_ENC_EC(ret);
  47326. ret = mp_mod(a, t, t);
  47327. if (ret != 0)
  47328. return WC_TEST_RET_ENC_EC(ret);
  47329. ret = mp_cmp(r, t);
  47330. if (ret != MP_EQ)
  47331. return WC_TEST_RET_ENC_NC;
  47332. }
  47333. }
  47334. #endif
  47335. return 0;
  47336. }
  47337. #endif
  47338. #if defined(WOLFSSL_SP_MATH_ALL) || defined(OPENSSL_EXTRA) || \
  47339. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY))
  47340. static wc_test_ret_t mp_test_mod_d(mp_int* a, WC_RNG* rng)
  47341. {
  47342. wc_test_ret_t ret;
  47343. mp_digit r;
  47344. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)
  47345. mp_digit rem;
  47346. int i;
  47347. #endif
  47348. (void)rng;
  47349. ret = mp_set(a, 1);
  47350. if (ret != MP_OKAY)
  47351. return WC_TEST_RET_ENC_EC(ret);
  47352. ret = mp_mod_d(a, 0, &r);
  47353. if (ret != MP_VAL)
  47354. return WC_TEST_RET_ENC_EC(ret);
  47355. mp_zero(a);
  47356. ret = mp_mod_d(a, 1, &r);
  47357. if (ret != MP_OKAY)
  47358. return WC_TEST_RET_ENC_EC(ret);
  47359. ret = mp_mod_d(a, 3, &r);
  47360. if (ret != MP_OKAY)
  47361. return WC_TEST_RET_ENC_EC(ret);
  47362. ret = mp_mod_d(a, 5, &r);
  47363. if (ret != MP_OKAY)
  47364. return WC_TEST_RET_ENC_EC(ret);
  47365. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)
  47366. for (i = MP_MAX_TEST_BYTE_LEN - 16; i <= MP_MAX_TEST_BYTE_LEN; i++) {
  47367. ret = randNum(a, i, rng, NULL);
  47368. if (ret != MP_OKAY)
  47369. return WC_TEST_RET_ENC_EC(ret);
  47370. ret = mp_mod_d(a, 3, &r);
  47371. if (ret != MP_OKAY)
  47372. return WC_TEST_RET_ENC_EC(ret);
  47373. ret = mp_div_d(a, 3, a, &rem);
  47374. if (ret != MP_OKAY)
  47375. return WC_TEST_RET_ENC_EC(ret);
  47376. if (r != rem)
  47377. return WC_TEST_RET_ENC_NC;
  47378. }
  47379. #endif
  47380. return 0;
  47381. }
  47382. #endif
  47383. static wc_test_ret_t mp_test_mul_sqr(mp_int* a, mp_int* b, mp_int* r1, mp_int* r2,
  47384. WC_RNG* rng)
  47385. {
  47386. wc_test_ret_t ret;
  47387. int i;
  47388. for (i = 1; i < 16; i++) {
  47389. ret = randNum(a, i, rng, NULL);
  47390. if (ret != 0)
  47391. return WC_TEST_RET_ENC_EC(ret);
  47392. ret = mp_mul(a, a, r1);
  47393. if (ret != 0)
  47394. return WC_TEST_RET_ENC_EC(ret);
  47395. ret = mp_sqr(a, r2);
  47396. if (ret != 0)
  47397. return WC_TEST_RET_ENC_EC(ret);
  47398. ret = mp_cmp(r1, r2);
  47399. if (ret != MP_EQ)
  47400. return WC_TEST_RET_ENC_NC;
  47401. }
  47402. ret = mp_set(b, 0);
  47403. if (ret != MP_OKAY)
  47404. return WC_TEST_RET_ENC_EC(ret);
  47405. ret = mp_mul(a, b, r1);
  47406. if (ret != MP_OKAY)
  47407. return WC_TEST_RET_ENC_EC(ret);
  47408. if (!mp_iszero(r1))
  47409. return WC_TEST_RET_ENC_EC(ret);
  47410. ret = mp_sqr(b, r1);
  47411. if (ret != MP_OKAY)
  47412. return WC_TEST_RET_ENC_EC(ret);
  47413. if (!mp_iszero(r1))
  47414. return WC_TEST_RET_ENC_NC;
  47415. #ifdef WOLFSSL_SP_MATH_ALL
  47416. ret = mp_set(a, 1);
  47417. if (ret != MP_OKAY)
  47418. return WC_TEST_RET_ENC_EC(ret);
  47419. i = (SP_INT_DIGITS / 2) + 1;
  47420. ret = mp_mul_2d(a, i * SP_WORD_SIZE - 1, a);
  47421. if (ret != MP_OKAY)
  47422. return WC_TEST_RET_ENC_EC(ret);
  47423. ret = mp_set(b, 1);
  47424. if (ret != MP_OKAY)
  47425. return WC_TEST_RET_ENC_EC(ret);
  47426. ret = mp_mul_2d(b, (SP_INT_DIGITS - 1 - i) * SP_WORD_SIZE - 1, b);
  47427. if (ret != MP_OKAY)
  47428. return WC_TEST_RET_ENC_EC(ret);
  47429. ret = mp_mul(a, b, r1);
  47430. if (ret != MP_OKAY)
  47431. return WC_TEST_RET_ENC_EC(ret);
  47432. ret = mp_mul(a, a, r1);
  47433. if (ret == MP_OKAY)
  47434. return WC_TEST_RET_ENC_NC;
  47435. ret = mp_sqr(a, r1);
  47436. if (ret == MP_OKAY)
  47437. return WC_TEST_RET_ENC_NC;
  47438. ret = mp_sqr(b, r1);
  47439. if (ret != MP_OKAY)
  47440. return WC_TEST_RET_ENC_EC(ret);
  47441. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  47442. (defined(HAVE_ECC) && defined(FP_ECC))
  47443. ret = mp_mulmod(a, b, b, r1);
  47444. if (ret != MP_OKAY)
  47445. return WC_TEST_RET_ENC_EC(ret);
  47446. ret = mp_mulmod(a, a, b, r1);
  47447. if (ret == MP_OKAY)
  47448. return WC_TEST_RET_ENC_NC;
  47449. #if defined(HAVE_ECC) && (defined(ECC_SHAMIR) || defined(FP_ECC))
  47450. ret = mp_sqrmod(a, b, r1);
  47451. if (ret == MP_OKAY)
  47452. return WC_TEST_RET_ENC_NC;
  47453. ret = mp_sqrmod(b, a, r1);
  47454. if (ret != MP_OKAY)
  47455. return WC_TEST_RET_ENC_EC(ret);
  47456. #endif /* HAVE_ECC && (ECC_SHAMIR || FP_ECC) */
  47457. #endif /* WOLFSSL_SP_MATH_ALL || WOLFSSL_HAVE_SP_DH || (HAVE_ECC && FP_ECC) */
  47458. #endif /* WOLFSSL_SP_MATH_ALL */
  47459. return 0;
  47460. }
  47461. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  47462. defined(OPENSSL_EXTRA)
  47463. static wc_test_ret_t mp_test_invmod(mp_int* a, mp_int* m, mp_int* r)
  47464. {
  47465. wc_test_ret_t ret;
  47466. mp_set(a, 0);
  47467. mp_set(m, 1);
  47468. ret = mp_invmod(a, m, r);
  47469. if (ret != MP_VAL)
  47470. return WC_TEST_RET_ENC_EC(ret);
  47471. ret = mp_invmod(m, a, r);
  47472. if (ret != MP_VAL)
  47473. return WC_TEST_RET_ENC_EC(ret);
  47474. mp_set(a, 2);
  47475. mp_set(m, 4);
  47476. ret = mp_invmod(a, m, r);
  47477. if (ret != MP_VAL)
  47478. return WC_TEST_RET_ENC_EC(ret);
  47479. mp_set(a, 3);
  47480. mp_set(m, 6);
  47481. ret = mp_invmod(a, m, r);
  47482. if (ret != MP_VAL)
  47483. return WC_TEST_RET_ENC_EC(ret);
  47484. mp_set(a, 5*9);
  47485. mp_set(m, 6*9);
  47486. ret = mp_invmod(a, m, r);
  47487. if (ret != MP_VAL)
  47488. return WC_TEST_RET_ENC_EC(ret);
  47489. mp_set(a, 1);
  47490. mp_set(m, 4);
  47491. ret = mp_invmod(a, m, r);
  47492. if (ret != MP_OKAY)
  47493. return WC_TEST_RET_ENC_EC(ret);
  47494. if (!mp_isone(r))
  47495. return WC_TEST_RET_ENC_NC;
  47496. mp_set(a, 3);
  47497. mp_set(m, 4);
  47498. ret = mp_invmod(a, m, r);
  47499. if (ret != MP_OKAY)
  47500. return WC_TEST_RET_ENC_EC(ret);
  47501. ret = mp_cmp_d(r, 3);
  47502. if (ret != 0)
  47503. return WC_TEST_RET_ENC_EC(ret);
  47504. mp_set(a, 3);
  47505. mp_set(m, 5);
  47506. ret = mp_invmod(a, m, r);
  47507. if (ret != MP_OKAY)
  47508. return WC_TEST_RET_ENC_EC(ret);
  47509. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  47510. /* Maximum 'a' */
  47511. mp_set(a, 0);
  47512. mp_set_bit(a, (r->size / 2)* SP_WORD_SIZE - 1);
  47513. mp_sub_d(a, 1, a);
  47514. /* Modulus too big. */
  47515. mp_set(m, 0);
  47516. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE);
  47517. ret = mp_invmod(a, m, r);
  47518. if (ret != MP_VAL)
  47519. return WC_TEST_RET_ENC_EC(ret);
  47520. /* Maximum modulus - even. */
  47521. mp_set(m, 0);
  47522. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE - 1);
  47523. ret = mp_invmod(a, m, r);
  47524. if (ret != MP_OKAY)
  47525. return WC_TEST_RET_ENC_EC(ret);
  47526. #endif
  47527. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_INT_NEGATIVE)
  47528. mp_read_radix(a, "-3", 16);
  47529. ret = mp_invmod(a, m, r);
  47530. if (ret != MP_OKAY)
  47531. return WC_TEST_RET_ENC_EC(ret);
  47532. #endif
  47533. #if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC)
  47534. mp_set(a, 0);
  47535. mp_set(m, 3);
  47536. ret = mp_invmod_mont_ct(a, m, r, 1);
  47537. if (ret != MP_VAL)
  47538. return WC_TEST_RET_ENC_EC(ret);
  47539. mp_set(a, 1);
  47540. mp_set(m, 0);
  47541. ret = mp_invmod_mont_ct(a, m, r, 1);
  47542. if (ret != MP_VAL)
  47543. return WC_TEST_RET_ENC_EC(ret);
  47544. mp_set(a, 1);
  47545. mp_set(m, 1);
  47546. ret = mp_invmod_mont_ct(a, m, r, 1);
  47547. if (ret != MP_VAL)
  47548. return WC_TEST_RET_ENC_EC(ret);
  47549. mp_set(a, 1);
  47550. mp_set(m, 2);
  47551. ret = mp_invmod_mont_ct(a, m, r, 1);
  47552. if (ret != MP_VAL)
  47553. return WC_TEST_RET_ENC_EC(ret);
  47554. mp_set(a, 1);
  47555. mp_set(m, 3);
  47556. ret = mp_invmod_mont_ct(a, m, r, 1);
  47557. if (ret != MP_OKAY)
  47558. return WC_TEST_RET_ENC_EC(ret);
  47559. #endif
  47560. return 0;
  47561. }
  47562. #endif /* !NO_RSA || HAVE_ECC || !NO_DSA || OPENSSL_EXTRA */
  47563. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  47564. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  47565. static wc_test_ret_t mp_test_exptmod(mp_int* b, mp_int* e, mp_int* m, mp_int* r)
  47566. {
  47567. wc_test_ret_t ret;
  47568. mp_set(b, 0x2);
  47569. mp_set(e, 0x3);
  47570. mp_set(m, 0x0);
  47571. ret = mp_exptmod_ex(b, e, 1, m, r);
  47572. if (ret != MP_VAL)
  47573. return WC_TEST_RET_ENC_EC(ret);
  47574. ret = mp_exptmod_nct(b, e, m, r);
  47575. if (ret != MP_VAL)
  47576. return WC_TEST_RET_ENC_EC(ret);
  47577. mp_set(b, 0x2);
  47578. mp_set(e, 0x3);
  47579. mp_set(m, 0x1);
  47580. ret = mp_exptmod_ex(b, e, 1, m, r);
  47581. if (ret != MP_OKAY)
  47582. return WC_TEST_RET_ENC_EC(ret);
  47583. if (!mp_iszero(r))
  47584. return WC_TEST_RET_ENC_NC;
  47585. ret = mp_exptmod_nct(b, e, m, r);
  47586. if (ret != MP_OKAY)
  47587. return WC_TEST_RET_ENC_EC(ret);
  47588. if (!mp_iszero(r))
  47589. return WC_TEST_RET_ENC_NC;
  47590. mp_set(b, 0x2);
  47591. mp_set(e, 0x0);
  47592. mp_set(m, 0x7);
  47593. ret = mp_exptmod_ex(b, e, 1, m, r);
  47594. if (ret != MP_OKAY)
  47595. return WC_TEST_RET_ENC_EC(ret);
  47596. if (!mp_isone(r))
  47597. return WC_TEST_RET_ENC_NC;
  47598. ret = mp_exptmod_nct(b, e, m, r);
  47599. if (ret != MP_OKAY)
  47600. return WC_TEST_RET_ENC_EC(ret);
  47601. if (!mp_isone(r))
  47602. return WC_TEST_RET_ENC_NC;
  47603. mp_set(b, 0x0);
  47604. mp_set(e, 0x3);
  47605. mp_set(m, 0x7);
  47606. ret = mp_exptmod_ex(b, e, 1, m, r);
  47607. if (ret != MP_OKAY)
  47608. return WC_TEST_RET_ENC_EC(ret);
  47609. if (!mp_iszero(r))
  47610. return WC_TEST_RET_ENC_NC;
  47611. ret = mp_exptmod_nct(b, e, m, r);
  47612. if (ret != MP_OKAY)
  47613. return WC_TEST_RET_ENC_EC(ret);
  47614. if (!mp_iszero(r))
  47615. return WC_TEST_RET_ENC_NC;
  47616. mp_set(b, 0x10);
  47617. mp_set(e, 0x3);
  47618. mp_set(m, 0x7);
  47619. ret = mp_exptmod_ex(b, e, 1, m, r);
  47620. if (ret != MP_OKAY)
  47621. return WC_TEST_RET_ENC_EC(ret);
  47622. ret = mp_exptmod_nct(b, e, m, r);
  47623. if (ret != MP_OKAY)
  47624. return WC_TEST_RET_ENC_EC(ret);
  47625. mp_set(b, 0x7);
  47626. mp_set(e, 0x3);
  47627. mp_set(m, 0x7);
  47628. ret = mp_exptmod_ex(b, e, 1, m, r);
  47629. if (ret != MP_OKAY)
  47630. return WC_TEST_RET_ENC_EC(ret);
  47631. if (!mp_iszero(r))
  47632. return WC_TEST_RET_ENC_NC;
  47633. ret = mp_exptmod_nct(b, e, m, r);
  47634. if (ret != MP_OKAY)
  47635. return WC_TEST_RET_ENC_EC(ret);
  47636. if (!mp_iszero(r))
  47637. return WC_TEST_RET_ENC_NC;
  47638. #ifndef WOLFSSL_SP_MATH
  47639. mp_set(b, 0x01);
  47640. mp_mul_2d(b, DIGIT_BIT, b);
  47641. mp_add_d(b, 1, b);
  47642. mp_set(e, 0x3);
  47643. mp_copy(b, m);
  47644. ret = mp_exptmod_ex(b, e, 1, m, r);
  47645. if (ret != MP_OKAY)
  47646. return WC_TEST_RET_ENC_EC(ret);
  47647. if (!mp_iszero(r))
  47648. return WC_TEST_RET_ENC_NC;
  47649. ret = mp_exptmod_nct(b, e, m, r);
  47650. if (ret != MP_OKAY)
  47651. return WC_TEST_RET_ENC_EC(ret);
  47652. if (!mp_iszero(r))
  47653. return WC_TEST_RET_ENC_NC;
  47654. #endif
  47655. mp_set(b, 0x2);
  47656. mp_set(e, 0x3);
  47657. mp_set(m, 0x7);
  47658. ret = mp_exptmod_ex(b, e, 1, m, r);
  47659. if (ret != MP_OKAY)
  47660. return WC_TEST_RET_ENC_EC(ret);
  47661. ret = mp_exptmod_nct(b, e, m, r);
  47662. if (ret != MP_OKAY)
  47663. return WC_TEST_RET_ENC_EC(ret);
  47664. #ifdef WOLFSSL_SP_MATH_ALL
  47665. mp_set(b, 0x2);
  47666. mp_set(e, 0x3);
  47667. mp_set(m, 0x01);
  47668. mp_mul_2d(m, SP_WORD_SIZE * SP_INT_DIGITS / 2, m);
  47669. mp_add_d(m, 0x01, m);
  47670. ret = mp_exptmod_ex(b, e, 1, m, r);
  47671. if (ret != MP_VAL)
  47672. return WC_TEST_RET_ENC_EC(ret);
  47673. ret = mp_exptmod_nct(b, e, m, r);
  47674. if (ret != MP_VAL)
  47675. return WC_TEST_RET_ENC_EC(ret);
  47676. #endif
  47677. return 0;
  47678. }
  47679. #endif /* !NO_RSA || !NO_DSA || !NO_DH || (HAVE_ECC && HAVE_COMP_KEY) ||
  47680. * OPENSSL_EXTRA */
  47681. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  47682. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  47683. static wc_test_ret_t mp_test_mont(mp_int* a, mp_int* m, mp_int* n, mp_int* r, WC_RNG* rng)
  47684. {
  47685. wc_test_ret_t ret;
  47686. mp_digit mp;
  47687. static int exp[] = { 7, 8, 16, 27, 32, 64,
  47688. 127, 128, 255, 256,
  47689. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  47690. 383, 384, 2033, 2048
  47691. #endif
  47692. };
  47693. static mp_digit sub[] = { 0x01, 0x05, 0x0f, 0x27, 0x05, 0x3b,
  47694. 0x01, 0x9f, 0x13, 0xbd,
  47695. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  47696. 0x1f, 0x13d, 0x45, 0x615
  47697. #endif
  47698. };
  47699. int bits[] = { 256, 384,
  47700. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 4096
  47701. 2048,
  47702. #endif
  47703. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 6144
  47704. 3072
  47705. #endif
  47706. };
  47707. int i;
  47708. int j;
  47709. for (i = 0; i < (int)(sizeof(exp) / sizeof(*exp)); i++) {
  47710. if (exp[i] >= DIGIT_BIT)
  47711. continue;
  47712. mp_zero(m);
  47713. ret = mp_set_bit(m, exp[i]);
  47714. if (ret != MP_OKAY)
  47715. return WC_TEST_RET_ENC_EC(ret);
  47716. ret = mp_sub_d(m, sub[i], m);
  47717. if (ret != MP_OKAY)
  47718. return WC_TEST_RET_ENC_EC(ret);
  47719. ret = mp_montgomery_setup(m, &mp);
  47720. if (ret != MP_OKAY)
  47721. return WC_TEST_RET_ENC_EC(ret);
  47722. ret = mp_montgomery_calc_normalization(n, m);
  47723. if (ret != MP_OKAY)
  47724. return WC_TEST_RET_ENC_EC(ret);
  47725. for (j = 0; j < 10; j++) {
  47726. ret = randNum(a, (exp[i] + DIGIT_BIT - 1) / DIGIT_BIT, rng, NULL);
  47727. if (ret != 0)
  47728. return WC_TEST_RET_ENC_EC(ret);
  47729. ret = mp_mod(a, m, a);
  47730. if (ret != 0)
  47731. return WC_TEST_RET_ENC_EC(ret);
  47732. /* r = a * a */
  47733. ret = mp_sqrmod(a, m, r);
  47734. if (ret != MP_OKAY)
  47735. return WC_TEST_RET_ENC_EC(ret);
  47736. /* Convert to Montgomery form = a*n */
  47737. ret = mp_mulmod(a, n, m, a);
  47738. if (ret != MP_OKAY)
  47739. return WC_TEST_RET_ENC_EC(ret);
  47740. /* a*a mod m == ((a*n) * (a*n)) / n / n */
  47741. ret = mp_sqr(a, a);
  47742. if (ret != MP_OKAY)
  47743. return WC_TEST_RET_ENC_EC(ret);
  47744. ret = mp_montgomery_reduce(a, m, mp);
  47745. if (ret != MP_OKAY)
  47746. return WC_TEST_RET_ENC_EC(ret);
  47747. ret = mp_montgomery_reduce(a, m, mp);
  47748. if (ret != MP_OKAY)
  47749. return WC_TEST_RET_ENC_EC(ret);
  47750. if (mp_cmp(a, r) != MP_EQ)
  47751. return WC_TEST_RET_ENC_NC;
  47752. }
  47753. }
  47754. /* Force carries. */
  47755. for (i = 0; i < (int)(sizeof(bits) / sizeof(*bits)); i++) {
  47756. /* a = 2^(bits*2) - 1 */
  47757. mp_zero(a);
  47758. mp_set_bit(a, bits[i] * 2);
  47759. mp_sub_d(a, 1, a);
  47760. /* m = 2^(bits) - 1 */
  47761. mp_zero(m);
  47762. mp_set_bit(m, bits[i]);
  47763. mp_sub_d(m, 1, m);
  47764. mp = 1;
  47765. /* result = r = 2^(bits) - 1 */
  47766. mp_zero(r);
  47767. mp_set_bit(r, bits[i]);
  47768. mp_sub_d(r, 1, r);
  47769. ret = mp_montgomery_reduce(a, m, mp);
  47770. if (ret != MP_OKAY)
  47771. return WC_TEST_RET_ENC_EC(ret);
  47772. /* Result is m or 0 if reduced to range of modulus. */
  47773. if (mp_cmp(a, r) != MP_EQ && mp_iszero(a) != MP_YES)
  47774. return WC_TEST_RET_ENC_NC;
  47775. }
  47776. return 0;
  47777. }
  47778. #endif
  47779. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mp_test(void)
  47780. {
  47781. WC_RNG rng;
  47782. int rng_inited = 0;
  47783. wc_test_ret_t ret;
  47784. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  47785. int i, j;
  47786. #ifndef WOLFSSL_SP_MATH
  47787. int k;
  47788. #endif
  47789. mp_digit d = 0;
  47790. #endif
  47791. #ifdef WOLFSSL_SMALL_STACK
  47792. mp_int *a = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  47793. DYNAMIC_TYPE_TMP_BUFFER),
  47794. *b = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  47795. DYNAMIC_TYPE_TMP_BUFFER),
  47796. *r1 = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  47797. DYNAMIC_TYPE_TMP_BUFFER),
  47798. *r2 = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  47799. DYNAMIC_TYPE_TMP_BUFFER),
  47800. *p = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  47801. DYNAMIC_TYPE_TMP_BUFFER);
  47802. if ((a == NULL) ||
  47803. (b == NULL) ||
  47804. (r1 == NULL) ||
  47805. (r2 == NULL) ||
  47806. (p == NULL))
  47807. {
  47808. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  47809. }
  47810. #else
  47811. mp_int a[1], b[1], r1[1], r2[1], p[1];
  47812. #endif
  47813. WOLFSSL_ENTER("mp_test");
  47814. ret = mp_init_multi(a, b, r1, r2, NULL, NULL);
  47815. if (ret != 0)
  47816. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47817. #ifdef WOLFSSL_SP_MATH_ALL
  47818. mp_init_copy(p, a);
  47819. #else
  47820. ret = mp_init(p);
  47821. if (ret != 0)
  47822. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47823. #endif
  47824. #ifndef HAVE_FIPS
  47825. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  47826. #else
  47827. ret = wc_InitRng(&rng);
  47828. #endif
  47829. if (ret != 0)
  47830. goto done;
  47831. rng_inited = 1;
  47832. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  47833. mp_set_int(a, 0);
  47834. if (a->used != 0 || a->dp[0] != 0)
  47835. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  47836. for (j = 1; j <= MP_MAX_TEST_BYTE_LEN; j++) {
  47837. for (i = 0; i < 4 * j; i++) {
  47838. /* New values to use. */
  47839. ret = randNum(p, j, &rng, NULL);
  47840. if (ret != 0)
  47841. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47842. ret = randNum(a, j, &rng, NULL);
  47843. if (ret != 0)
  47844. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47845. ret = randNum(b, j, &rng, NULL);
  47846. if (ret != 0)
  47847. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47848. ret = wc_RNG_GenerateBlock(&rng, (byte*)&d, sizeof(d));
  47849. if (ret != 0)
  47850. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47851. d &= MP_MASK;
  47852. #if !defined(WOLFSSL_SP_MATH) || (defined(HAVE_ECC) && \
  47853. (defined(ECC_SHAMIR) || defined(FP_ECC)))
  47854. /* Ensure sqrmod produce same result as mulmod. */
  47855. ret = mp_sqrmod(a, p, r1);
  47856. if (ret != 0)
  47857. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47858. ret = mp_mulmod(a, a, p, r2);
  47859. if (ret != 0)
  47860. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47861. if (mp_cmp(r1, r2) != 0) {
  47862. WOLFSSL_MSG("Fail: mp_mulmod result does not match mp_sqrmod!");
  47863. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  47864. }
  47865. #endif
  47866. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  47867. #if defined(WOLFSSL_SP_MATH) || (defined(WOLFSSL_SP_MATH_ALL) && \
  47868. !defined(WOLFSSL_SP_INT_NEGATIVE))
  47869. ret = mp_addmod(a, b, p, r1);
  47870. if (ret != 0)
  47871. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47872. ret = mp_submod(r1, b, p, r2);
  47873. if (ret != 0)
  47874. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47875. ret = mp_mod(a, p, r1);
  47876. if (ret != 0)
  47877. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47878. if (mp_cmp(r1, r2) != MP_EQ)
  47879. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  47880. #else
  47881. /* Ensure add with mod produce same result as sub with mod. */
  47882. ret = mp_addmod(a, b, p, r1);
  47883. if (ret != 0)
  47884. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47885. b->sign ^= 1;
  47886. ret = mp_submod(a, b, p, r2);
  47887. if (ret != 0)
  47888. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47889. if (mp_cmp(r1, r2) != 0)
  47890. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  47891. #endif
  47892. #endif
  47893. /* Ensure add digit produce same result as sub digit. */
  47894. ret = mp_add_d(a, d, r1);
  47895. if (ret != 0)
  47896. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47897. ret = mp_sub_d(r1, d, r2);
  47898. if (ret != 0)
  47899. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47900. if (mp_cmp(a, r2) != 0)
  47901. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  47902. /* Invert - if p is even it will use the slow impl.
  47903. * - if p and a are even it will fail.
  47904. */
  47905. ret = mp_invmod(a, p, r1);
  47906. if (ret != 0 && ret != MP_VAL)
  47907. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47908. #ifndef WOLFSSL_SP_MATH
  47909. /* Shift up and down number all bits in a digit. */
  47910. for (k = 0; k < DIGIT_BIT; k++) {
  47911. mp_mul_2d(a, k, r1);
  47912. mp_div_2d(r1, k, r2, p);
  47913. if (mp_cmp(a, r2) != 0)
  47914. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  47915. if (!mp_iszero(p))
  47916. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  47917. mp_rshb(r1, k);
  47918. if (mp_cmp(a, r1) != 0)
  47919. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  47920. }
  47921. #endif
  47922. }
  47923. }
  47924. /* Test adding and subtracting zero from zero. */
  47925. mp_zero(a);
  47926. ret = mp_add_d(a, 0, r1);
  47927. if (ret != 0)
  47928. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47929. if (!mp_iszero(r1)) {
  47930. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  47931. }
  47932. ret = mp_sub_d(a, 0, r2);
  47933. if (ret != 0)
  47934. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  47935. if (!mp_iszero(r2)) {
  47936. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  47937. }
  47938. #if DIGIT_BIT >= 32
  47939. /* Check that setting a 32-bit digit works. */
  47940. d &= 0xffffffffU;
  47941. mp_set_int(a, d);
  47942. if (a->used != 1 || a->dp[0] != d)
  47943. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  47944. #endif
  47945. /* Check setting a bit and testing a bit works. */
  47946. for (i = 0; i < MP_MAX_TEST_BYTE_LEN * 8; i++) {
  47947. mp_zero(a);
  47948. mp_set_bit(a, i);
  47949. if (!mp_is_bit_set(a, i))
  47950. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  47951. }
  47952. #endif
  47953. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  47954. mp_zero(a);
  47955. i = mp_cnt_lsb(a);
  47956. if (i != 0)
  47957. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  47958. mp_set(a, 1);
  47959. i = mp_cnt_lsb(a);
  47960. if (i != 0)
  47961. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  47962. mp_set(a, 32);
  47963. i = mp_cnt_lsb(a);
  47964. if (i != 5)
  47965. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  47966. mp_zero(a);
  47967. mp_set_bit(a, 129);
  47968. i = mp_cnt_lsb(a);
  47969. if (i != 129)
  47970. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  47971. #endif
  47972. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  47973. if ((ret = mp_test_param(a, b, r1, &rng)) != 0)
  47974. goto done;
  47975. #endif
  47976. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  47977. if ((ret = mp_test_div_3(a, r1, &rng)) != 0)
  47978. goto done;
  47979. #endif
  47980. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  47981. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  47982. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  47983. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  47984. if ((ret = mp_test_radix_10(a, r1, &rng)) != 0)
  47985. goto done;
  47986. #endif
  47987. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  47988. defined(HAVE_ECC))
  47989. if ((ret = mp_test_radix_16(a, r1, &rng)) != 0)
  47990. goto done;
  47991. #endif
  47992. if ((ret = mp_test_shift(a, r1, &rng)) != 0)
  47993. goto done;
  47994. if ((ret = mp_test_add_sub_d(a, r1)) != 0)
  47995. goto done;
  47996. if ((ret = mp_test_read_to_bin(a)) != 0)
  47997. goto done;
  47998. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  47999. if ((ret = mp_test_set_int(a)) != 0)
  48000. goto done;
  48001. #endif
  48002. if ((ret = mp_test_cmp(a, r1)) != 0)
  48003. goto done;
  48004. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  48005. if ((ret = mp_test_shbd(a, b, &rng)) != 0)
  48006. goto done;
  48007. #endif
  48008. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  48009. if ((ret = mp_test_set_is_bit(a)) != 0)
  48010. goto done;
  48011. #endif
  48012. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  48013. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && \
  48014. !defined(WOLFSSL_RSA_PUBLIC_ONLY))
  48015. if ((ret = mp_test_div(a, b, r1, r2, &rng)) != 0)
  48016. goto done;
  48017. #endif
  48018. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  48019. !defined(WC_NO_RNG)
  48020. if ((ret = mp_test_prime(a, &rng)) != 0)
  48021. goto done;
  48022. #endif
  48023. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  48024. if ((ret = mp_test_lcm_gcd(a, b, r1, r2, &rng)) != 0)
  48025. goto done;
  48026. #endif
  48027. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  48028. defined(WOLFSSL_SP_MATH_ALL)
  48029. if ((ret = mp_test_mod_2d(a, r1, p, &rng)) != 0)
  48030. goto done;
  48031. #endif
  48032. #if defined(WOLFSSL_SP_MATH_ALL) || defined(OPENSSL_EXTRA) || \
  48033. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY))
  48034. if ((ret = mp_test_mod_d(a, &rng)) != 0)
  48035. goto done;
  48036. #endif
  48037. if ((ret = mp_test_mul_sqr(a, b, r1, r2, &rng)) != 0)
  48038. goto done;
  48039. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  48040. defined(OPENSSL_EXTRA)
  48041. if ((ret = mp_test_invmod(a, b, r1)) != 0)
  48042. goto done;
  48043. #endif
  48044. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  48045. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  48046. if ((ret = mp_test_exptmod(a, b, r1, r2)) != 0)
  48047. goto done;
  48048. #endif
  48049. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  48050. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  48051. if ((ret = mp_test_mont(a, b, r1, r2, &rng)) != 0)
  48052. goto done;
  48053. #endif
  48054. done:
  48055. #ifdef WOLFSSL_SMALL_STACK
  48056. if (p) {
  48057. mp_clear(p);
  48058. XFREE(p, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48059. }
  48060. if (r2) {
  48061. mp_clear(r2);
  48062. XFREE(r2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48063. }
  48064. if (r1) {
  48065. mp_clear(r1);
  48066. XFREE(r1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48067. }
  48068. if (b) {
  48069. mp_clear(b);
  48070. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48071. }
  48072. if (a) {
  48073. mp_clear(a);
  48074. XFREE(a, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48075. }
  48076. #else
  48077. mp_clear(p);
  48078. mp_clear(r2);
  48079. mp_clear(r1);
  48080. mp_clear(b);
  48081. mp_clear(a);
  48082. #endif
  48083. if (rng_inited)
  48084. wc_FreeRng(&rng);
  48085. return ret;
  48086. }
  48087. #endif /* WOLFSSL_PUBLIC_MP && ((WOLFSSL_SP_MATH_ALL &&
  48088. * !WOLFSSL_RSA_VERIFY_ONLY) || USE_FAST_MATH) */
  48089. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  48090. typedef struct pairs_t {
  48091. const unsigned char* coeff;
  48092. int coeffSz;
  48093. int exp;
  48094. } pairs_t;
  48095. /*
  48096. n =p1p2p3, where pi = ki(p1-1)+1 with (k2,k3) = (173,293)
  48097. p1 = 2^192 * 0x000000000000e24fd4f6d6363200bf2323ec46285cac1d3a
  48098. + 2^0 * 0x0b2488b0c29d96c5e67f8bec15b54b189ae5636efe89b45b
  48099. */
  48100. static const unsigned char c192a[] =
  48101. {
  48102. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x4f,
  48103. 0xd4, 0xf6, 0xd6, 0x36, 0x32, 0x00, 0xbf, 0x23,
  48104. 0x23, 0xec, 0x46, 0x28, 0x5c, 0xac, 0x1d, 0x3a
  48105. };
  48106. static const unsigned char c0a[] =
  48107. {
  48108. 0x0b, 0x24, 0x88, 0xb0, 0xc2, 0x9d, 0x96, 0xc5,
  48109. 0xe6, 0x7f, 0x8b, 0xec, 0x15, 0xb5, 0x4b, 0x18,
  48110. 0x9a, 0xe5, 0x63, 0x6e, 0xfe, 0x89, 0xb4, 0x5b
  48111. };
  48112. static const pairs_t ecPairsA[] =
  48113. {
  48114. {c192a, sizeof(c192a), 192},
  48115. {c0a, sizeof(c0a), 0}
  48116. };
  48117. static const int kA[] = {173, 293};
  48118. static const unsigned char controlPrime[] = {
  48119. 0xe1, 0x76, 0x45, 0x80, 0x59, 0xb6, 0xd3, 0x49,
  48120. 0xdf, 0x0a, 0xef, 0x12, 0xd6, 0x0f, 0xf0, 0xb7,
  48121. 0xcb, 0x2a, 0x37, 0xbf, 0xa7, 0xf8, 0xb5, 0x4d,
  48122. 0xf5, 0x31, 0x35, 0xad, 0xe4, 0xa3, 0x94, 0xa1,
  48123. 0xdb, 0xf1, 0x96, 0xad, 0xb5, 0x05, 0x64, 0x85,
  48124. 0x83, 0xfc, 0x1b, 0x5b, 0x29, 0xaa, 0xbe, 0xf8,
  48125. 0x26, 0x3f, 0x76, 0x7e, 0xad, 0x1c, 0xf0, 0xcb,
  48126. 0xd7, 0x26, 0xb4, 0x1b, 0x05, 0x8e, 0x56, 0x86,
  48127. 0x7e, 0x08, 0x62, 0x21, 0xc1, 0x86, 0xd6, 0x47,
  48128. 0x79, 0x3e, 0xb7, 0x5d, 0xa4, 0xc6, 0x3a, 0xd7,
  48129. 0xb1, 0x74, 0x20, 0xf6, 0x50, 0x97, 0x41, 0x04,
  48130. 0x53, 0xed, 0x3f, 0x26, 0xd6, 0x6f, 0x91, 0xfa,
  48131. 0x68, 0x26, 0xec, 0x2a, 0xdc, 0x9a, 0xf1, 0xe7,
  48132. 0xdc, 0xfb, 0x73, 0xf0, 0x79, 0x43, 0x1b, 0x21,
  48133. 0xa3, 0x59, 0x04, 0x63, 0x52, 0x07, 0xc9, 0xd7,
  48134. 0xe6, 0xd1, 0x1b, 0x5d, 0x5e, 0x96, 0xfa, 0x53
  48135. };
  48136. static const unsigned char testOne[] = { 1 };
  48137. static wc_test_ret_t GenerateNextP(mp_int* p1, mp_int* p2, int k)
  48138. {
  48139. wc_test_ret_t ret;
  48140. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  48141. mp_int *ki = (mp_int *)XMALLOC(sizeof(*ki), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48142. if (ki == NULL)
  48143. return MEMORY_E;
  48144. #else
  48145. mp_int ki[1];
  48146. #endif
  48147. ret = mp_init(ki);
  48148. if (ret != 0)
  48149. ret = WC_TEST_RET_ENC_EC(ret);
  48150. if (ret == 0) {
  48151. ret = mp_set(ki, (mp_digit)k);
  48152. if (ret != 0)
  48153. ret = WC_TEST_RET_ENC_EC(ret);
  48154. }
  48155. if (ret == 0) {
  48156. ret = mp_sub_d(p1, 1, p2);
  48157. if (ret != 0)
  48158. ret = WC_TEST_RET_ENC_EC(ret);
  48159. }
  48160. if (ret == 0) {
  48161. ret = mp_mul(p2, ki, p2);
  48162. if (ret != 0)
  48163. ret = WC_TEST_RET_ENC_EC(ret);
  48164. }
  48165. if (ret == 0) {
  48166. ret = mp_add_d(p2, 1, p2);
  48167. if (ret != 0)
  48168. ret = WC_TEST_RET_ENC_EC(ret);
  48169. }
  48170. mp_clear(ki);
  48171. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  48172. XFREE(ki, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48173. #endif
  48174. return ret;
  48175. }
  48176. static wc_test_ret_t GenerateP(mp_int* p1, mp_int* p2, mp_int* p3,
  48177. const pairs_t* ecPairs, int ecPairsSz,
  48178. const int* k)
  48179. {
  48180. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  48181. mp_int *x = NULL, *y = NULL;
  48182. #else
  48183. mp_int x[1], y[1];
  48184. #endif
  48185. wc_test_ret_t ret;
  48186. int i;
  48187. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  48188. if (((x = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) ||
  48189. ((y = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) {
  48190. ret = MEMORY_E;
  48191. goto out;
  48192. }
  48193. #endif
  48194. ret = mp_init_multi(x, y, NULL, NULL, NULL, NULL);
  48195. if (ret != 0) {
  48196. ret = WC_TEST_RET_ENC_EC(ret);
  48197. goto out;
  48198. }
  48199. for (i = 0; ret == 0 && i < ecPairsSz; i++) {
  48200. ret = mp_read_unsigned_bin(x, ecPairs[i].coeff, (word32)ecPairs[i].coeffSz);
  48201. if (ret != 0) {
  48202. ret = WC_TEST_RET_ENC_EC(ret);
  48203. break;
  48204. }
  48205. /* p1 = 2^exp */
  48206. ret = mp_2expt(y, ecPairs[i].exp);
  48207. if (ret != 0) {
  48208. ret = WC_TEST_RET_ENC_EC(ret);
  48209. break;
  48210. }
  48211. /* p1 = p1 * m */
  48212. ret = mp_mul(x, y, x);
  48213. if (ret != 0) {
  48214. ret = WC_TEST_RET_ENC_EC(ret);
  48215. break;
  48216. }
  48217. /* p1 += */
  48218. ret = mp_add(p1, x, p1);
  48219. if (ret != 0) {
  48220. ret = WC_TEST_RET_ENC_EC(ret);
  48221. break;
  48222. }
  48223. mp_zero(x);
  48224. mp_zero(y);
  48225. }
  48226. if (ret == 0)
  48227. ret = GenerateNextP(p1, p2, k[0]);
  48228. if (ret == 0)
  48229. ret = GenerateNextP(p1, p3, k[1]);
  48230. out:
  48231. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  48232. if (x != NULL) {
  48233. mp_clear(x);
  48234. XFREE(x, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48235. }
  48236. if (y != NULL) {
  48237. mp_clear(y);
  48238. XFREE(y, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48239. }
  48240. #else
  48241. mp_clear(x);
  48242. mp_clear(y);
  48243. #endif
  48244. return ret;
  48245. }
  48246. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t prime_test(void)
  48247. {
  48248. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  48249. mp_int *n = (mp_int *)XMALLOC(sizeof *n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  48250. *p1 = (mp_int *)XMALLOC(sizeof *p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  48251. *p2 = (mp_int *)XMALLOC(sizeof *p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  48252. *p3 = (mp_int *)XMALLOC(sizeof *p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48253. #else
  48254. mp_int n[1],
  48255. p1[1],
  48256. p2[1],
  48257. p3[1];
  48258. #endif
  48259. wc_test_ret_t ret;
  48260. int isPrime = 0;
  48261. WC_RNG rng;
  48262. WOLFSSL_ENTER("prime_test");
  48263. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  48264. if ((n == NULL) ||
  48265. (p1 == NULL) ||
  48266. (p2 == NULL) ||
  48267. (p3 == NULL))
  48268. ERROR_OUT(MEMORY_E, out);
  48269. #endif
  48270. ret = wc_InitRng(&rng);
  48271. if (ret != 0)
  48272. ret = WC_TEST_RET_ENC_EC(ret);
  48273. if (ret == 0) {
  48274. ret = mp_init_multi(n, p1, p2, p3, NULL, NULL);
  48275. if (ret != 0)
  48276. ret = WC_TEST_RET_ENC_EC(ret);
  48277. }
  48278. if (ret == 0)
  48279. ret = GenerateP(p1, p2, p3,
  48280. ecPairsA, sizeof(ecPairsA) / sizeof(ecPairsA[0]), kA);
  48281. if (ret == 0) {
  48282. ret = mp_mul(p1, p2, n);
  48283. if (ret != 0)
  48284. ret = WC_TEST_RET_ENC_EC(ret);
  48285. }
  48286. if (ret == 0) {
  48287. ret = mp_mul(n, p3, n);
  48288. if (ret != 0)
  48289. ret = WC_TEST_RET_ENC_EC(ret);
  48290. }
  48291. if (ret != 0)
  48292. ERROR_OUT(ret, out);
  48293. /* Check the old prime test using the number that false positives.
  48294. * This test result should indicate as not prime. */
  48295. ret = mp_prime_is_prime(n, 40, &isPrime);
  48296. if (ret != 0)
  48297. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  48298. if (isPrime)
  48299. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  48300. /* This test result should fail. It should indicate the value as prime. */
  48301. ret = mp_prime_is_prime(n, 8, &isPrime);
  48302. if (ret != 0)
  48303. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  48304. if (!isPrime)
  48305. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  48306. /* This test result should indicate the value as not prime. */
  48307. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  48308. if (ret != 0)
  48309. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  48310. if (isPrime)
  48311. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  48312. ret = mp_read_unsigned_bin(n, controlPrime, sizeof(controlPrime));
  48313. if (ret != 0)
  48314. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  48315. /* This test result should indicate the value as prime. */
  48316. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  48317. if (ret != 0)
  48318. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  48319. if (!isPrime)
  48320. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  48321. /* This test result should indicate the value as prime. */
  48322. isPrime = -1;
  48323. ret = mp_prime_is_prime(n, 8, &isPrime);
  48324. if (ret != 0)
  48325. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  48326. if (!isPrime)
  48327. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  48328. ret = mp_read_unsigned_bin(n, testOne, sizeof(testOne));
  48329. if (ret != 0)
  48330. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  48331. /* This test result should indicate the value as not prime. */
  48332. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  48333. if (ret != 0)
  48334. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  48335. if (isPrime)
  48336. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  48337. ret = mp_prime_is_prime(n, 8, &isPrime);
  48338. if (ret != 0)
  48339. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  48340. if (isPrime)
  48341. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  48342. ret = 0;
  48343. out:
  48344. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  48345. if (n != NULL) {
  48346. mp_clear(n);
  48347. XFREE(n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48348. }
  48349. if (p1 != NULL) {
  48350. mp_clear(p1);
  48351. XFREE(p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48352. }
  48353. if (p2 != NULL) {
  48354. mp_clear(p2);
  48355. XFREE(p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48356. }
  48357. if (p3 != NULL) {
  48358. mp_clear(p3);
  48359. XFREE(p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48360. }
  48361. #else
  48362. mp_clear(p3);
  48363. mp_clear(p2);
  48364. mp_clear(p1);
  48365. mp_clear(n);
  48366. #endif
  48367. wc_FreeRng(&rng);
  48368. return ret;
  48369. }
  48370. #endif /* WOLFSSL_PUBLIC_MP */
  48371. #if defined(ASN_BER_TO_DER) && \
  48372. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  48373. defined(OPENSSL_EXTRA_X509_SMALL))
  48374. /* wc_BerToDer is only public facing in the case of test cert or opensslextra */
  48375. typedef struct berDerTestData {
  48376. const byte *in;
  48377. word32 inSz;
  48378. const byte *out;
  48379. word32 outSz;
  48380. } berDerTestData;
  48381. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t berder_test(void)
  48382. {
  48383. wc_test_ret_t ret;
  48384. int i;
  48385. word32 len = 0, l;
  48386. byte out[32];
  48387. WOLFSSL_SMALL_STACK_STATIC const byte good1_in[] = { 0x30, 0x80, 0x00, 0x00 };
  48388. WOLFSSL_SMALL_STACK_STATIC const byte good1_out[] = { 0x30, 0x00 };
  48389. WOLFSSL_SMALL_STACK_STATIC const byte good2_in[] = { 0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00 };
  48390. WOLFSSL_SMALL_STACK_STATIC const byte good2_out[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  48391. WOLFSSL_SMALL_STACK_STATIC const byte good3_in[] = {
  48392. 0x24, 0x80, 0x04, 0x01, 0x01, 0x00, 0x00
  48393. };
  48394. WOLFSSL_SMALL_STACK_STATIC const byte good3_out[] = { 0x04, 0x1, 0x01 };
  48395. WOLFSSL_SMALL_STACK_STATIC const byte good4_in[] = {
  48396. 0x30, 0x80,
  48397. 0x02, 0x01, 0x01,
  48398. 0x30, 0x80,
  48399. 0x24, 0x80,
  48400. 0x04, 0x01, 0x01,
  48401. 0x04, 0x02, 0x02, 0x03,
  48402. 0x00, 0x00,
  48403. 0x06, 0x01, 0x01,
  48404. 0x00, 0x00,
  48405. 0x31, 0x80,
  48406. 0x06, 0x01, 0x01,
  48407. 0x00, 0x00,
  48408. 0x00, 0x00,
  48409. };
  48410. WOLFSSL_SMALL_STACK_STATIC const byte good4_out[] = {
  48411. 0x30, 0x12,
  48412. 0x02, 0x01, 0x01,
  48413. 0x30, 0x08,
  48414. 0x04, 0x03, 0x01, 0x02, 0x03,
  48415. 0x06, 0x01, 0x01,
  48416. 0x31, 0x03,
  48417. 0x06, 0x01, 0x01
  48418. };
  48419. WOLFSSL_SMALL_STACK_STATIC const byte good5_in[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  48420. berDerTestData testData[] = {
  48421. { good1_in, sizeof(good1_in), good1_out, sizeof(good1_out) },
  48422. { good2_in, sizeof(good2_in), good2_out, sizeof(good2_out) },
  48423. { good3_in, sizeof(good3_in), good3_out, sizeof(good3_out) },
  48424. { good4_in, sizeof(good4_in), good4_out, sizeof(good4_out) },
  48425. { good5_in, sizeof(good5_in), good5_in , sizeof(good5_in ) },
  48426. };
  48427. WOLFSSL_ENTER("berder_test");
  48428. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  48429. ret = wc_BerToDer(testData[i].in, testData[i].inSz, NULL, &len);
  48430. if (ret != LENGTH_ONLY_E)
  48431. return WC_TEST_RET_ENC_I(i);
  48432. if (len != testData[i].outSz)
  48433. return WC_TEST_RET_ENC_I(i);
  48434. len = testData[i].outSz;
  48435. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &len);
  48436. if (ret != 0)
  48437. return WC_TEST_RET_ENC_I(i);
  48438. if (XMEMCMP(out, testData[i].out, len) != 0)
  48439. return WC_TEST_RET_ENC_I(i);
  48440. for (l = 1; l < testData[i].inSz; l++) {
  48441. ret = wc_BerToDer(testData[i].in, l, NULL, &len);
  48442. if (ret != ASN_PARSE_E)
  48443. return WC_TEST_RET_ENC_EC(ret);
  48444. len = testData[i].outSz;
  48445. ret = wc_BerToDer(testData[i].in, l, out, &len);
  48446. if (ret != ASN_PARSE_E)
  48447. return WC_TEST_RET_ENC_EC(ret);
  48448. }
  48449. for (l = 0; l < testData[i].outSz-1; l++) {
  48450. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &l);
  48451. if (ret != BUFFER_E)
  48452. return WC_TEST_RET_ENC_EC(ret);
  48453. }
  48454. }
  48455. ret = wc_BerToDer(NULL, 4, NULL, NULL);
  48456. if (ret != BAD_FUNC_ARG)
  48457. return WC_TEST_RET_ENC_EC(ret);
  48458. ret = wc_BerToDer(out, 4, NULL, NULL);
  48459. if (ret != BAD_FUNC_ARG)
  48460. return WC_TEST_RET_ENC_EC(ret);
  48461. ret = wc_BerToDer(NULL, 4, NULL, &len);
  48462. if (ret != BAD_FUNC_ARG)
  48463. return WC_TEST_RET_ENC_EC(ret);
  48464. ret = wc_BerToDer(NULL, 4, out, NULL);
  48465. if (ret != BAD_FUNC_ARG)
  48466. return WC_TEST_RET_ENC_EC(ret);
  48467. ret = wc_BerToDer(out, 4, out, NULL);
  48468. if (ret != BAD_FUNC_ARG)
  48469. return WC_TEST_RET_ENC_EC(ret);
  48470. ret = wc_BerToDer(NULL, 4, out, &len);
  48471. if (ret != BAD_FUNC_ARG)
  48472. return WC_TEST_RET_ENC_EC(ret);
  48473. for (l = 1; l < sizeof(good4_out); l++) {
  48474. len = l;
  48475. ret = wc_BerToDer(good4_in, sizeof(good4_in), out, &len);
  48476. if (ret != BUFFER_E)
  48477. return WC_TEST_RET_ENC_EC(ret);
  48478. }
  48479. return 0;
  48480. }
  48481. #endif
  48482. #ifdef DEBUG_WOLFSSL
  48483. static THREAD_LS_T int log_cnt = 0;
  48484. static void my_Logging_cb(const int logLevel, const char *const logMessage)
  48485. {
  48486. (void)logLevel;
  48487. (void)logMessage;
  48488. log_cnt++;
  48489. }
  48490. #endif /* DEBUG_WOLFSSL */
  48491. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t logging_test(void)
  48492. {
  48493. wc_test_ret_t ret;
  48494. #ifdef DEBUG_WOLFSSL
  48495. const char* msg = "Testing, testing. 1, 2, 3, 4 ...";
  48496. byte a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  48497. byte b[256];
  48498. int i;
  48499. WOLFSSL_ENTER("logging_test (debug)");
  48500. for (i = 0; i < (int)sizeof(b); i++)
  48501. b[i] = i;
  48502. ret = wolfSSL_Debugging_ON();
  48503. if (ret != 0)
  48504. return WC_TEST_RET_ENC_EC(ret);
  48505. ret = wolfSSL_SetLoggingCb(my_Logging_cb);
  48506. if (ret != 0)
  48507. return WC_TEST_RET_ENC_EC(ret);
  48508. WOLFSSL_MSG(msg);
  48509. WOLFSSL_BUFFER(a, sizeof(a));
  48510. WOLFSSL_BUFFER(b, sizeof(b));
  48511. WOLFSSL_BUFFER(NULL, 0);
  48512. WOLFSSL_ERROR(MEMORY_E);
  48513. WOLFSSL_ERROR_MSG(msg);
  48514. /* turn off logs */
  48515. wolfSSL_Debugging_OFF();
  48516. /* capture log count */
  48517. i = log_cnt;
  48518. /* validate no logs are output when disabled */
  48519. WOLFSSL_MSG(msg);
  48520. WOLFSSL_BUFFER(a, sizeof(a));
  48521. WOLFSSL_BUFFER(b, sizeof(b));
  48522. WOLFSSL_BUFFER(NULL, 0);
  48523. WOLFSSL_ERROR(MEMORY_E);
  48524. WOLFSSL_ERROR_MSG(msg);
  48525. /* check the logs were disabled */
  48526. if (i != log_cnt)
  48527. return WC_TEST_RET_ENC_NC;
  48528. /* restore callback and leave logging enabled */
  48529. wolfSSL_SetLoggingCb(NULL);
  48530. wolfSSL_Debugging_ON();
  48531. /* suppress unused args */
  48532. (void)a;
  48533. (void)b;
  48534. #else
  48535. WOLFSSL_ENTER("logging_test");
  48536. ret = wolfSSL_Debugging_ON();
  48537. if (ret != NOT_COMPILED_IN)
  48538. return WC_TEST_RET_ENC_EC(ret);
  48539. wolfSSL_Debugging_OFF();
  48540. ret = wolfSSL_SetLoggingCb(NULL);
  48541. if (ret != NOT_COMPILED_IN)
  48542. return WC_TEST_RET_ENC_EC(ret);
  48543. #endif /* DEBUG_WOLFSSL */
  48544. return 0;
  48545. }
  48546. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  48547. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t wolfcrypt_mutex_test(void)
  48548. #else
  48549. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mutex_test(void)
  48550. #endif
  48551. {
  48552. #ifdef WOLFSSL_PTHREADS
  48553. wolfSSL_Mutex m;
  48554. #endif
  48555. #if defined(WOLFSSL_PTHREADS) || (!defined(WOLFSSL_NO_MALLOC) && \
  48556. !defined(WOLFSSL_USER_MUTEX) && defined(WOLFSSL_STATIC_MEMORY))
  48557. wc_test_ret_t ret;
  48558. #endif
  48559. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_USER_MUTEX)
  48560. #ifndef WOLFSSL_STATIC_MEMORY
  48561. wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
  48562. WOLFSSL_ENTER("[wolfcrypt_]mutex_test (1)");
  48563. #else
  48564. wolfSSL_Mutex *mm = (wolfSSL_Mutex*) XMALLOC(sizeof(wolfSSL_Mutex),
  48565. HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  48566. WOLFSSL_ENTER("[wolfcrypt_]mutex_test (2)");
  48567. if (mm != NULL) {
  48568. ret = wc_InitMutex(mm);
  48569. if (ret != 0) {
  48570. WOLFSSL_MSG("Init Mutex failed");
  48571. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  48572. return WC_TEST_RET_ENC_EC(ret);
  48573. }
  48574. }
  48575. #endif
  48576. if (mm == NULL)
  48577. return WC_TEST_RET_ENC_ERRNO;
  48578. wc_FreeMutex(mm);
  48579. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  48580. #endif
  48581. /* Can optionally enable advanced pthread tests using "ENABLE_PTHREAD_LOCKFREE_TESTS" */
  48582. #ifdef WOLFSSL_PTHREADS
  48583. ret = wc_InitMutex(&m);
  48584. if (ret != 0)
  48585. return WC_TEST_RET_ENC_EC(ret);
  48586. ret = wc_LockMutex(&m);
  48587. if (ret != 0)
  48588. return WC_TEST_RET_ENC_EC(ret);
  48589. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  48590. /* trying to free a locked mutex is not portable behavior with pthread */
  48591. /* Attempting to destroy a locked mutex results in undefined behavior */
  48592. ret = wc_FreeMutex(&m);
  48593. if (ret != BAD_MUTEX_E)
  48594. return WC_TEST_RET_ENC_EC(ret);
  48595. #endif
  48596. ret = wc_UnLockMutex(&m);
  48597. if (ret != 0)
  48598. return WC_TEST_RET_ENC_EC(ret);
  48599. ret = wc_FreeMutex(&m);
  48600. if (ret != 0)
  48601. return WC_TEST_RET_ENC_EC(ret);
  48602. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  48603. /* Trying to use a pthread after free'ing is not portable behavior */
  48604. ret = wc_LockMutex(&m);
  48605. if (ret != BAD_MUTEX_E)
  48606. return WC_TEST_RET_ENC_EC(ret);
  48607. ret = wc_UnLockMutex(&m);
  48608. if (ret != BAD_MUTEX_E)
  48609. return WC_TEST_RET_ENC_EC(ret);
  48610. #endif
  48611. #endif
  48612. return 0;
  48613. }
  48614. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  48615. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  48616. !defined(WOLFSSL_STATIC_MEMORY)
  48617. static wc_test_ret_t malloc_cnt = 0;
  48618. static wc_test_ret_t realloc_cnt = 0;
  48619. static wc_test_ret_t free_cnt = 0;
  48620. #ifdef WOLFSSL_DEBUG_MEMORY
  48621. static void *my_Malloc_cb(size_t size, const char* func, unsigned int line)
  48622. {
  48623. (void) func;
  48624. (void) line;
  48625. #else
  48626. static void *my_Malloc_cb(size_t size)
  48627. {
  48628. #endif
  48629. malloc_cnt++;
  48630. #ifndef WOLFSSL_NO_MALLOC
  48631. return malloc(size);
  48632. #else
  48633. WOLFSSL_MSG("No malloc available");
  48634. (void)size;
  48635. return NULL;
  48636. #endif
  48637. }
  48638. #ifdef WOLFSSL_DEBUG_MEMORY
  48639. static void my_Free_cb(void *ptr, const char* func, unsigned int line)
  48640. {
  48641. (void) func;
  48642. (void) line;
  48643. #else
  48644. static void my_Free_cb(void *ptr)
  48645. {
  48646. #endif
  48647. free_cnt++;
  48648. #ifndef WOLFSSL_NO_MALLOC
  48649. free(ptr);
  48650. #else
  48651. WOLFSSL_MSG("No free available");
  48652. (void)ptr;
  48653. #endif
  48654. }
  48655. #ifdef WOLFSSL_DEBUG_MEMORY
  48656. static void *my_Realloc_cb(void *ptr, size_t size, const char* func, unsigned int line)
  48657. {
  48658. (void) func;
  48659. (void) line;
  48660. #else
  48661. static void *my_Realloc_cb(void *ptr, size_t size)
  48662. {
  48663. #endif
  48664. realloc_cnt++;
  48665. #ifndef WOLFSSL_NO_MALLOC
  48666. return realloc(ptr, size);
  48667. #else
  48668. WOLFSSL_MSG("No realloc available");
  48669. (void)ptr;
  48670. (void)size;
  48671. return NULL;
  48672. #endif
  48673. }
  48674. #endif /* !WOLFSSL_NO_MALLOC */
  48675. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memcb_test(void)
  48676. {
  48677. wc_test_ret_t ret = 0;
  48678. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_NO_REALLOC) && \
  48679. !defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_STATIC_MEMORY)
  48680. byte* b = NULL;
  48681. #endif
  48682. wolfSSL_Malloc_cb mc;
  48683. wolfSSL_Free_cb fc;
  48684. wolfSSL_Realloc_cb rc;
  48685. WOLFSSL_ENTER("memcb_test");
  48686. /* Save existing memory callbacks */
  48687. ret = wolfSSL_GetAllocators(&mc, &fc, &rc);
  48688. if (ret != 0)
  48689. return WC_TEST_RET_ENC_EC(ret);
  48690. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_NO_REALLOC) && \
  48691. !defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_STATIC_MEMORY)
  48692. /* test realloc */
  48693. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48694. if (b == NULL) {
  48695. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_memcb);
  48696. }
  48697. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48698. b = NULL;
  48699. /* Use API. */
  48700. ret = wolfSSL_SetAllocators((wolfSSL_Malloc_cb)my_Malloc_cb,
  48701. (wolfSSL_Free_cb)my_Free_cb,
  48702. (wolfSSL_Realloc_cb)my_Realloc_cb);
  48703. if (ret != 0) {
  48704. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_memcb);
  48705. }
  48706. b = (byte*)XMALLOC(1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48707. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48708. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48709. #ifndef WOLFSSL_STATIC_MEMORY
  48710. #ifndef WOLFSSL_CHECK_MEM_ZERO
  48711. if (malloc_cnt != 1 || free_cnt != 1 || realloc_cnt != 1)
  48712. #else
  48713. /* Checking zeroized memory means realloc is a malloc and free. */
  48714. if (malloc_cnt != 2 || free_cnt != 2 || realloc_cnt != 0)
  48715. #endif
  48716. #else
  48717. if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
  48718. #endif
  48719. ret = WC_TEST_RET_ENC_NC;
  48720. #endif /* !WOLFSSL_NO_MALLOC */
  48721. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_NO_REALLOC) && \
  48722. !defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_STATIC_MEMORY)
  48723. exit_memcb:
  48724. /* reset malloc/free/realloc counts */
  48725. malloc_cnt = 0;
  48726. free_cnt = 0;
  48727. realloc_cnt = 0;
  48728. #endif
  48729. /* restore memory callbacks */
  48730. wolfSSL_SetAllocators(mc, fc, rc);
  48731. return ret;
  48732. }
  48733. #endif /* USE_WOLFSSL_MEMORY && !WOLFSSL_NO_MALLOC */
  48734. #if defined(WOLFSSL_CAAM_BLOB)
  48735. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blob_test(void)
  48736. {
  48737. wc_test_ret_t ret = 0;
  48738. byte out[112];
  48739. byte blob[112];
  48740. word32 outSz;
  48741. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  48742. {
  48743. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  48744. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  48745. };
  48746. WOLFSSL_SMALL_STACK_STATIC const byte text[] =
  48747. {
  48748. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  48749. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  48750. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  48751. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  48752. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  48753. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  48754. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  48755. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  48756. };
  48757. WOLFSSL_ENTER("blob_test");
  48758. XMEMSET(blob, 0, sizeof(blob));
  48759. XMEMSET(out, 0, sizeof(out));
  48760. outSz = sizeof(blob);
  48761. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  48762. if (ret != 0)
  48763. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  48764. blob[outSz - 2] += 1;
  48765. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  48766. if (ret == 0) { /* should fail with altered blob */
  48767. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_blob);
  48768. }
  48769. XMEMSET(blob, 0, sizeof(blob));
  48770. outSz = sizeof(blob);
  48771. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  48772. if (ret != 0)
  48773. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  48774. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  48775. if (ret != 0)
  48776. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  48777. if (XMEMCMP(out, iv, sizeof(iv))) {
  48778. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_blob);
  48779. }
  48780. XMEMSET(blob, 0, sizeof(blob));
  48781. outSz = sizeof(blob);
  48782. ret = wc_caamCreateBlob((byte*)text, sizeof(text), blob, &outSz);
  48783. if (ret != 0)
  48784. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  48785. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  48786. if (ret != 0)
  48787. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  48788. if (XMEMCMP(out, text, sizeof(text))) {
  48789. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_blob);
  48790. }
  48791. exit_blob:
  48792. return ret;
  48793. }
  48794. #endif /* WOLFSSL_CAAM_BLOB */
  48795. #ifdef WOLF_CRYPTO_CB
  48796. /* Example custom context for crypto callback */
  48797. typedef struct {
  48798. int exampleVar; /* flag for testing if only crypt is enabled. */
  48799. } myCryptoDevCtx;
  48800. #ifdef WOLF_CRYPTO_CB_ONLY_RSA
  48801. /* Testing rsa cb when CB_ONLY_RSA is enabled
  48802. * When CB_ONLY_RSA is enabled, software imple. is not available.
  48803. *
  48804. * ctx callback ctx
  48805. * returen 0 on success, otherwise return negative
  48806. */
  48807. static wc_test_ret_t rsa_onlycb_test(myCryptoDevCtx *ctx)
  48808. {
  48809. wc_test_ret_t ret = 0;
  48810. #if !defined(NO_RSA)
  48811. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  48812. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key,
  48813. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48814. byte* tmp = NULL;
  48815. #else
  48816. RsaKey key[1];
  48817. byte tmp[FOURK_BUF];
  48818. #endif
  48819. size_t bytes;
  48820. const word32 inLen = (word32)TEST_STRING_SZ;
  48821. word32 idx = 0;
  48822. word32 sigSz;
  48823. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  48824. byte out[RSA_TEST_BYTES];
  48825. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  48826. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  48827. !defined(NO_FILESYSTEM)
  48828. XFILE file;
  48829. #endif
  48830. #ifdef WOLFSSL_KEY_GEN
  48831. WC_RNG rng;
  48832. #endif
  48833. #ifdef USE_CERT_BUFFERS_1024
  48834. bytes = (size_t)sizeof_client_key_der_1024;
  48835. if (bytes < (size_t)sizeof_client_cert_der_1024)
  48836. bytes = (size_t)sizeof_client_cert_der_1024;
  48837. #elif defined(USE_CERT_BUFFERS_2048)
  48838. bytes = (size_t)sizeof_client_key_der_2048;
  48839. if (bytes < (size_t)sizeof_client_cert_der_2048)
  48840. bytes = (size_t)sizeof_client_cert_der_2048;
  48841. #elif defined(USE_CERT_BUFFERS_3072)
  48842. bytes = (size_t)sizeof_client_key_der_3072;
  48843. if (bytes < (size_t)sizeof_client_cert_der_3072)
  48844. bytes = (size_t)sizeof_client_cert_der_3072;
  48845. #elif defined(USE_CERT_BUFFERS_4096)
  48846. bytes = (size_t)sizeof_client_key_der_4096;
  48847. if (bytes < (size_t)sizeof_client_cert_der_4096)
  48848. bytes = (size_t)sizeof_client_cert_der_4096;
  48849. #else
  48850. bytes = FOURK_BUF;
  48851. #endif
  48852. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  48853. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48854. if (tmp == NULL)
  48855. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_onlycb);
  48856. #endif
  48857. #ifdef USE_CERT_BUFFERS_1024
  48858. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  48859. #elif defined(USE_CERT_BUFFERS_2048)
  48860. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  48861. #elif defined(USE_CERT_BUFFERS_3072)
  48862. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  48863. #elif defined(USE_CERT_BUFFERS_4096)
  48864. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  48865. #elif !defined(NO_FILESYSTEM)
  48866. file = XFOPEN(clientKey, "rb");
  48867. if (!file) {
  48868. ret = WC_TEST_RET_ENC_ERRNO;
  48869. err_sys("can't open ./certs/client-key.der, "
  48870. "Please run from wolfSSL home dir", ret);
  48871. ERROR_OUT(ret, exit_onlycb);
  48872. }
  48873. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  48874. XFCLOSE(file);
  48875. if (bytes == 0)
  48876. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_onlycb);
  48877. #endif
  48878. #ifdef WOLFSSL_KEY_GEN
  48879. /* wc_CryptoCb_MakeRsaKey cb test, no actual making key
  48880. * wc_MakeRsaKey() -> rsa cb ->
  48881. * myCryptoDevCb -> wc_MakeRsaKey(CBONLY_TEST_DEVID)
  48882. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return 0(success)
  48883. */
  48884. ctx->exampleVar = 99;
  48885. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  48886. if (ret != 0)
  48887. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48888. /* wc_MakeRsaKey() -> rsa cb ->
  48889. * myCryptoDevCb -> wc_MakeRsaKey(INVALID_DEVID)
  48890. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return NO_VALID_DEVID(failure)
  48891. */
  48892. ctx->exampleVar = 1;
  48893. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  48894. if (ret != NO_VALID_DEVID) {
  48895. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48896. } else
  48897. /* reset return code */
  48898. ret = 0;
  48899. #endif
  48900. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  48901. if (ret != 0)
  48902. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48903. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  48904. if (ret != 0)
  48905. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48906. sigSz = (word32)wc_RsaEncryptSize(key);
  48907. /* wc_CryptoCb_Rsa cb test, no actual rsa operation */
  48908. if (ret == 0) {
  48909. /* wc_SignatureGenerate() -> rsa cb ->
  48910. * myCryptoDevCb -> wc_RsaFunction(CBONLY_TEST_DEVID)
  48911. * wc_RsaFunction(CBONLY_TEST_DEVID) expects to return 0(success)
  48912. */
  48913. ctx->exampleVar = 99;
  48914. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  48915. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  48916. if (ret != 0)
  48917. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48918. }
  48919. if (ret == 0) {
  48920. /* wc_SignatureGenerate() -> rsa cb ->
  48921. * myCryptoDevCb -> wc_RsaFunction(INVALID_DEVID)
  48922. * wc_SignatureGenerate(INVALID_DEVID) expects to
  48923. * return NO_VALID_DEVID(failure)
  48924. */
  48925. ctx->exampleVar = 1;
  48926. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  48927. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  48928. if (ret != NO_VALID_DEVID) {
  48929. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  48930. } else
  48931. /* reset return code */
  48932. ret = 0;
  48933. }
  48934. exit_onlycb:
  48935. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  48936. if (key != NULL) {
  48937. wc_FreeRsaKey(key);
  48938. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48939. }
  48940. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48941. #else
  48942. wc_FreeRsaKey(key);
  48943. #endif
  48944. #endif
  48945. return ret;
  48946. }
  48947. #endif
  48948. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  48949. /* Testing rsa cb when CB_ONLY_ECC is enabled
  48950. * When CB_ONLY_ECC is enabled, software imple. is not available.
  48951. *
  48952. * ctx callback ctx
  48953. * returen 0 on success, otherwise return negative
  48954. */
  48955. static wc_test_ret_t ecc_onlycb_test(myCryptoDevCtx *ctx)
  48956. {
  48957. wc_test_ret_t ret = 0;
  48958. #if defined(HAVE_ECC)
  48959. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  48960. ecc_key* key = (ecc_key *)XMALLOC(sizeof *key,
  48961. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48962. ecc_key* pub = (ecc_key *)XMALLOC(sizeof *pub,
  48963. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48964. byte* out = (byte*)XMALLOC(sizeof(byte),
  48965. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48966. #ifdef OPENSSL_EXTRA
  48967. byte* check = (byte*)XMALLOC(sizeof(byte)*(256), HEAP_HINT,
  48968. DYNAMIC_TYPE_TMP_BUFFER);
  48969. #endif
  48970. #else
  48971. ecc_key key[1];
  48972. ecc_key pub[1];
  48973. byte out[256];
  48974. #ifdef OPENSSL_EXTRA
  48975. unsigned char check[256];
  48976. #endif
  48977. #endif
  48978. #ifdef OPENSSL_EXTRA
  48979. EVP_PKEY* privKey = NULL;
  48980. EVP_PKEY* pubKey = NULL;
  48981. #ifdef USE_CERT_BUFFERS_256
  48982. ecc_key* pkey;
  48983. const unsigned char* cp;
  48984. #endif
  48985. EVP_MD_CTX mdCtx;
  48986. const char testData[] = "Hi There";
  48987. size_t checkSz = -1;
  48988. const unsigned char* p;
  48989. const unsigned char check_v[256] = {
  48990. 0x30,0x45,0x02,0x20,0x1b,0x5c,0x2a,0xf0,0x18,0x09,
  48991. 0x74,0x65,0xa1,0x04,0x76,0x3a,0xce,0xcc,0xe5,0x34,
  48992. 0x5e,0x89,0xed,0x40,0x1e,0x5a,0xb1,0x53,0xb4,0xff,
  48993. 0xc7,0x18,0xfe,0x0f,0xc7,0xa6,0x02,0x21,0x00,0xe5,
  48994. 0x70,0x21,0xfc,0xf9,0x63,0x36,0xfd,0x16,0x18,0x08,
  48995. 0x9a,0x63,0x61,0x0f,0xe7,0x7c,0xa3,0xc9,0x14,0xa3,
  48996. 0x30,0x87,0xf7,0xf5,0x70,0x19,0xaf,0x56,0x96,0x9b,
  48997. 0xd8,0x64,0xcd,0xd9,0xff,0x7b,0x2a,0x55,0x52,0xca,
  48998. 0x41,0xb2,0xa6,0xa4,0x8a,0x3b,0x02,0x20,0x8c,0xc5,
  48999. 0xf9,0xc1,0x7d,0x2a,0x65,0x6c,0xe6,0x5a,0xe3,0x76,
  49000. 0x9b,0xab,0x0b,0x9f,0xaf,0x62,0x5d,0xb2,0x60,0xd7,
  49001. 0xeb,0xb4,0x1b,0x73,0xdc,0x01,0x7d,0x7b,0xab,0xc1,
  49002. 0x0c,0x74,0x96,0x41,0xe6,0x3f,0xc5,0x86,0xe6,0x7d,
  49003. 0x2b,0x9d,0x54,0x6b,0xcd,0x31,0x35,0x1f,0xdb,0x49,
  49004. 0x1f,0x32,0x34,0xf8,0x57,0x12,0x86,0x5c,0x0e,0x80,
  49005. 0x55,0x8d,0xff,0xd8,0xbd,0xdf,0x32,0x26,0x62,0x42,
  49006. 0x09,0xda,0xf7,0x74,0xf2,0x3f,0xe6,0xf1,0x77,0x82,
  49007. 0xce,0xe4,0xbb,0x61,0xa6,0xc0,0x17,0x0c,0x6c,0x47,
  49008. 0x2a,0x40,0x1c,0x2b,0xe0,0x98,0x3b,0xbf,0xc6,0xf8,
  49009. 0x6d,0xfd,0xd0,0xfa,0xc1,0x02,0xfb,0x5f,0xfb,0xb0,
  49010. 0xcb,0xd9,0xa3,0x59,0x94,0xe9,0x0f,0x74,0xbb,0x3f,
  49011. 0x64,0xa3,0x83,0xc4,0x2b,0xf7,0xd2,0x97,0xbf,0x3b,
  49012. 0xcf,0xbb,0x60,0x81,0x33,0x94,0xfa,0x0d,0x35,0xd2,
  49013. 0x3d,0xb9,0x99,0xe3,0x12,0xf8,0xf4,0xa3,0x74,0xf4,
  49014. 0x94,0x1d,0x7a,0x66,0xf8,0xd1,0x1d,0xcf,0xb0,0x48,
  49015. 0xef,0x8c,0x94,0x6f,0xdd,0x62,
  49016. };
  49017. #endif
  49018. WC_RNG rng;
  49019. EncryptedInfo encInfo;
  49020. int keyFormat = 0;
  49021. word32 keyIdx = 0;
  49022. byte in[] = "Everyone gets Friday off. ecc p";
  49023. word32 inLen = (word32)XSTRLEN((char*)in);
  49024. word32 outLen;
  49025. int verify;
  49026. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  49027. if (key == NULL || pub == NULL) {
  49028. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_onlycb);
  49029. }
  49030. #endif
  49031. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  49032. if (ret != 0)
  49033. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  49034. /* wc_CryptoCb_MakeEccKey cb test, , no actual testing */
  49035. ctx->exampleVar = 99;
  49036. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  49037. if (ret != 0)
  49038. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  49039. ctx->exampleVar = 1;
  49040. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  49041. if (ret != NO_VALID_DEVID) {
  49042. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  49043. } else
  49044. /* reset return code */
  49045. ret = 0;
  49046. #ifdef USE_CERT_BUFFERS_256
  49047. if (ret == 0) {
  49048. /* load ECC private key and perform private transform */
  49049. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &keyIdx,
  49050. key, sizeof_ecc_key_der_256);
  49051. }
  49052. if (ret != 0)
  49053. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  49054. /* wc_CryptoCb_EccSign cb test, no actual testing */
  49055. ctx->exampleVar = 99;
  49056. if (ret == 0) {
  49057. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  49058. }
  49059. if (ret != 0)
  49060. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  49061. ctx->exampleVar = 1;
  49062. if (ret == 0) {
  49063. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  49064. }
  49065. if (ret != NO_VALID_DEVID) {
  49066. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  49067. }
  49068. else
  49069. ret = 0;
  49070. /* wc_CryptoCb_EccVerify cb test, no actual testing */
  49071. ctx->exampleVar = 99;
  49072. if (ret == 0) {
  49073. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  49074. }
  49075. if (ret != 0)
  49076. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  49077. ctx->exampleVar = 1;
  49078. if (ret == 0) {
  49079. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  49080. }
  49081. if (ret != NO_VALID_DEVID) {
  49082. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  49083. }
  49084. else
  49085. ret = 0;
  49086. /* wc_CryptoCb_Ecdh cb test, no actual testing */
  49087. /* make public key for shared secret */
  49088. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  49089. ctx->exampleVar = 99;
  49090. if (ret == 0) {
  49091. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  49092. }
  49093. if (ret != 0)
  49094. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  49095. ctx->exampleVar = 1;
  49096. if (ret == 0) {
  49097. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  49098. }
  49099. if (ret != NO_VALID_DEVID) {
  49100. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  49101. }
  49102. else
  49103. ret = 0;
  49104. #ifdef OPENSSL_EXTRA
  49105. (void)pkey;
  49106. cp = ecc_clikey_der_256;
  49107. privKey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  49108. sizeof_ecc_clikey_der_256);
  49109. if (privKey == NULL) {
  49110. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  49111. }
  49112. pkey = (ecc_key*)privKey->ecc->internal;
  49113. pkey->devId = devId;
  49114. p = ecc_clikeypub_der_256;
  49115. pubKey = d2i_PUBKEY(NULL, &p, sizeof_ecc_clikeypub_der_256);
  49116. if (pubKey == NULL) {
  49117. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  49118. }
  49119. pkey = (ecc_key*)pubKey->ecc->internal;
  49120. pkey->devId = devId;
  49121. /* sign */
  49122. EVP_MD_CTX_init(&mdCtx);
  49123. ret = EVP_DigestSignInit(&mdCtx, NULL, EVP_sha256(), NULL, privKey);
  49124. if (ret != WOLFSSL_SUCCESS) {
  49125. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  49126. }
  49127. ret = EVP_DigestSignUpdate(&mdCtx, testData,
  49128. (unsigned int)XSTRLEN(testData));
  49129. if (ret != WOLFSSL_SUCCESS) {
  49130. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  49131. }
  49132. ret = EVP_DigestSignFinal(&mdCtx, NULL, &checkSz);
  49133. if (ret != WOLFSSL_SUCCESS) {
  49134. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  49135. }
  49136. ctx->exampleVar = 99;
  49137. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  49138. /* just called crypt callback as dummy
  49139. * EVP_DigestSignFinal returns 0 internally.
  49140. */
  49141. if (ret != 0)
  49142. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  49143. ctx->exampleVar = 1;
  49144. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  49145. /* just called crypt callback as dummy
  49146. * EVP_DigestSignFinal returns 0 internally.
  49147. */
  49148. if (ret != 0)
  49149. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  49150. /* restore checkSz for verify */
  49151. checkSz = 71;
  49152. ret = EVP_MD_CTX_cleanup(&mdCtx);
  49153. if (ret != SSL_SUCCESS) {
  49154. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  49155. }
  49156. /* verify */
  49157. EVP_MD_CTX_init(&mdCtx);
  49158. if (ret == SSL_SUCCESS) {
  49159. ret = EVP_DigestVerifyInit(&mdCtx, NULL, EVP_sha256(), NULL, pubKey);
  49160. }
  49161. if (ret != WOLFSSL_SUCCESS) {
  49162. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  49163. }
  49164. if (ret == WOLFSSL_SUCCESS) {
  49165. ret = EVP_DigestVerifyUpdate(&mdCtx, testData,
  49166. (unsigned int)XSTRLEN(testData));
  49167. }
  49168. if (ret != WOLFSSL_SUCCESS) {
  49169. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  49170. }
  49171. ctx->exampleVar = 99;
  49172. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  49173. /* just called crypt callback as dummy
  49174. * EVP_DigestSignFinal returns 0 internally.
  49175. */
  49176. if (ret != 0)
  49177. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  49178. ctx->exampleVar = 1;
  49179. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  49180. /* just called crypt callback as dummy
  49181. * EVP_DigestVerifyFinal returns -1 internally rather than NO_VALID_DEVID.
  49182. */
  49183. if (ret != -1) {
  49184. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  49185. }
  49186. ret = EVP_MD_CTX_cleanup(&mdCtx);
  49187. if (ret != SSL_SUCCESS) {
  49188. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  49189. } else
  49190. ret = 0;
  49191. #endif
  49192. #else
  49193. (void)verify;
  49194. (void)outLen;
  49195. (void)inLen;
  49196. (void)out;
  49197. (void)pub;
  49198. #ifdef OPENSSL_EXTRA
  49199. (void)privKey;
  49200. (void)pubKey;
  49201. (void)mdCtx;
  49202. (void)check;
  49203. (void)checkSz;
  49204. (void)p;
  49205. #endif
  49206. #endif
  49207. (void)keyFormat;
  49208. (void)encInfo;
  49209. exit_onlycb:
  49210. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  49211. if (key != NULL) {
  49212. wc_ecc_free(key);
  49213. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  49214. }
  49215. if (pub != NULL) {
  49216. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  49217. }
  49218. if (out != NULL) {
  49219. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  49220. }
  49221. #ifdef OPENSSL_EXTRA
  49222. if (check) {
  49223. FREE(check, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  49224. }
  49225. #endif
  49226. #else
  49227. wc_ecc_free(key);
  49228. #ifdef OPENSSL_EXTRA
  49229. if (privKey)
  49230. EVP_PKEY_free(privKey);
  49231. if (pubKey)
  49232. EVP_PKEY_free(pubKey);
  49233. #endif
  49234. #endif
  49235. #endif /* HAVE_ECC */
  49236. return ret;
  49237. }
  49238. #endif
  49239. /* Example crypto dev callback function that calls software version */
  49240. static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
  49241. {
  49242. int ret = NOT_COMPILED_IN; /* return this to bypass HW and use SW */
  49243. myCryptoDevCtx* myCtx = (myCryptoDevCtx*)ctx;
  49244. if (info == NULL)
  49245. return BAD_FUNC_ARG;
  49246. #ifdef DEBUG_WOLFSSL
  49247. WOLFSSL_MSG_EX("CryptoDevCb: Algo Type %d\n", info->algo_type);
  49248. #endif
  49249. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  49250. #if defined(WOLF_CRYPTO_CB) && !defined(HAVE_HASHDRBG) && \
  49251. !defined(WC_NO_RNG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  49252. /* if RNG only supports crypto callback, just use seed */
  49253. ret = wc_GenerateSeed(&info->rng.rng->seed,
  49254. info->rng.out, info->rng.sz);
  49255. #elif !defined(WC_NO_RNG)
  49256. /* set devId to invalid, so software is used */
  49257. info->rng.rng->devId = INVALID_DEVID;
  49258. ret = wc_RNG_GenerateBlock(info->rng.rng,
  49259. info->rng.out, info->rng.sz);
  49260. /* reset devId */
  49261. info->rng.rng->devId = devIdArg;
  49262. #endif
  49263. }
  49264. else if (info->algo_type == WC_ALGO_TYPE_SEED) {
  49265. #ifndef WC_NO_RNG
  49266. ALIGN32 static byte seed[sizeof(word32)] = { 0x00, 0x00, 0x00, 0x01 };
  49267. word32* seedWord32 = (word32*)seed;
  49268. word32 len;
  49269. /* wc_GenerateSeed is a local symbol so we need to fake the entropy. */
  49270. while (info->seed.sz > 0) {
  49271. len = (word32)sizeof(seed);
  49272. if (info->seed.sz < len)
  49273. len = info->seed.sz;
  49274. XMEMCPY(info->seed.seed, seed, sizeof(seed));
  49275. info->seed.seed += len;
  49276. info->seed.sz -= len;
  49277. (*seedWord32)++;
  49278. }
  49279. ret = 0;
  49280. #endif
  49281. }
  49282. else if (info->algo_type == WC_ALGO_TYPE_PK) {
  49283. #ifdef DEBUG_WOLFSSL
  49284. WOLFSSL_MSG_EX("CryptoDevCb: Pk Type %d\n", info->pk.type);
  49285. #endif
  49286. #ifndef NO_RSA
  49287. if (info->pk.type == WC_PK_TYPE_RSA) {
  49288. /* set devId to invalid, so software is used */
  49289. info->pk.rsa.key->devId = INVALID_DEVID;
  49290. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  49291. #ifdef DEBUG_WOLFSSL
  49292. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  49293. #endif
  49294. if (myCtx->exampleVar == 99) {
  49295. info->pk.rsa.key->devId = devIdArg;
  49296. return 0;
  49297. }
  49298. #endif
  49299. switch (info->pk.rsa.type) {
  49300. case RSA_PUBLIC_ENCRYPT:
  49301. case RSA_PUBLIC_DECRYPT:
  49302. /* perform software based RSA public op */
  49303. ret = wc_RsaFunction(
  49304. info->pk.rsa.in, info->pk.rsa.inLen,
  49305. info->pk.rsa.out, info->pk.rsa.outLen,
  49306. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  49307. break;
  49308. case RSA_PRIVATE_ENCRYPT:
  49309. case RSA_PRIVATE_DECRYPT:
  49310. /* perform software based RSA private op */
  49311. ret = wc_RsaFunction(
  49312. info->pk.rsa.in, info->pk.rsa.inLen,
  49313. info->pk.rsa.out, info->pk.rsa.outLen,
  49314. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  49315. break;
  49316. }
  49317. /* reset devId */
  49318. info->pk.rsa.key->devId = devIdArg;
  49319. }
  49320. #ifdef WOLFSSL_KEY_GEN
  49321. else if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
  49322. info->pk.rsakg.key->devId = INVALID_DEVID;
  49323. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  49324. #ifdef DEBUG_WOLFSSL
  49325. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  49326. #endif
  49327. if (myCtx->exampleVar == 99) {
  49328. info->pk.rsakg.key->devId = devIdArg;
  49329. return 0;
  49330. }
  49331. #endif
  49332. #ifdef HAVE_FIPS
  49333. for (;;) {
  49334. #endif
  49335. ret = wc_MakeRsaKey(info->pk.rsakg.key, info->pk.rsakg.size,
  49336. info->pk.rsakg.e, info->pk.rsakg.rng);
  49337. #ifdef HAVE_FIPS
  49338. if (ret == PRIME_GEN_E)
  49339. continue;
  49340. break;
  49341. }
  49342. #endif
  49343. /* reset devId */
  49344. info->pk.rsakg.key->devId = devIdArg;
  49345. }
  49346. #endif
  49347. #endif /* !NO_RSA */
  49348. #ifdef HAVE_ECC
  49349. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  49350. /* set devId to invalid, so software is used */
  49351. info->pk.eckg.key->devId = INVALID_DEVID;
  49352. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  49353. #ifdef DEBUG_WOLFSSL
  49354. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  49355. #endif
  49356. if (myCtx->exampleVar == 99) {
  49357. info->pk.eckg.key->devId = devIdArg;
  49358. return 0;
  49359. }
  49360. #endif
  49361. ret = wc_ecc_make_key_ex(info->pk.eckg.rng, info->pk.eckg.size,
  49362. info->pk.eckg.key, info->pk.eckg.curveId);
  49363. /* reset devId */
  49364. info->pk.eckg.key->devId = devIdArg;
  49365. }
  49366. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  49367. /* set devId to invalid, so software is used */
  49368. info->pk.eccsign.key->devId = INVALID_DEVID;
  49369. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  49370. #ifdef DEBUG_WOLFSSL
  49371. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  49372. #endif
  49373. if (myCtx->exampleVar == 99) {
  49374. info->pk.eccsign.key->devId = devIdArg;
  49375. return 0;
  49376. }
  49377. #endif
  49378. ret = wc_ecc_sign_hash(
  49379. info->pk.eccsign.in, info->pk.eccsign.inlen,
  49380. info->pk.eccsign.out, info->pk.eccsign.outlen,
  49381. info->pk.eccsign.rng, info->pk.eccsign.key);
  49382. /* reset devId */
  49383. info->pk.eccsign.key->devId = devIdArg;
  49384. }
  49385. else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
  49386. /* set devId to invalid, so software is used */
  49387. info->pk.eccverify.key->devId = INVALID_DEVID;
  49388. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  49389. #ifdef DEBUG_WOLFSSL
  49390. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  49391. #endif
  49392. if (myCtx->exampleVar == 99) {
  49393. info->pk.eccverify.key->devId = devIdArg;
  49394. return 0;
  49395. }
  49396. #endif
  49397. ret = wc_ecc_verify_hash(
  49398. info->pk.eccverify.sig, info->pk.eccverify.siglen,
  49399. info->pk.eccverify.hash, info->pk.eccverify.hashlen,
  49400. info->pk.eccverify.res, info->pk.eccverify.key);
  49401. /* reset devId */
  49402. info->pk.eccverify.key->devId = devIdArg;
  49403. }
  49404. else if (info->pk.type == WC_PK_TYPE_ECDH) {
  49405. /* set devId to invalid, so software is used */
  49406. info->pk.ecdh.private_key->devId = INVALID_DEVID;
  49407. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  49408. #ifdef DEBUG_WOLFSSL
  49409. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  49410. #endif
  49411. if (myCtx->exampleVar == 99) {
  49412. info->pk.ecdh.private_key->devId = devIdArg;
  49413. return 0;
  49414. }
  49415. #endif
  49416. ret = wc_ecc_shared_secret(
  49417. info->pk.ecdh.private_key, info->pk.ecdh.public_key,
  49418. info->pk.ecdh.out, info->pk.ecdh.outlen);
  49419. /* reset devId */
  49420. info->pk.ecdh.private_key->devId = devIdArg;
  49421. }
  49422. #endif /* HAVE_ECC */
  49423. #ifdef HAVE_CURVE25519
  49424. if (info->pk.type == WC_PK_TYPE_CURVE25519_KEYGEN) {
  49425. /* set devId to invalid, so software is used */
  49426. info->pk.curve25519kg.key->devId = INVALID_DEVID;
  49427. ret = wc_curve25519_make_key(info->pk.curve25519kg.rng,
  49428. info->pk.curve25519kg.size, info->pk.curve25519kg.key);
  49429. /* reset devId */
  49430. info->pk.curve25519kg.key->devId = devIdArg;
  49431. }
  49432. else if (info->pk.type == WC_PK_TYPE_CURVE25519) {
  49433. /* set devId to invalid, so software is used */
  49434. info->pk.curve25519.private_key->devId = INVALID_DEVID;
  49435. ret = wc_curve25519_shared_secret_ex(
  49436. info->pk.curve25519.private_key, info->pk.curve25519.public_key,
  49437. info->pk.curve25519.out, info->pk.curve25519.outlen,
  49438. info->pk.curve25519.endian);
  49439. /* reset devId */
  49440. info->pk.curve25519.private_key->devId = devIdArg;
  49441. }
  49442. #endif /* HAVE_CURVE25519 */
  49443. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_MAKE_KEY)
  49444. if (info->pk.type == WC_PK_TYPE_ED25519_KEYGEN) {
  49445. /* set devId to invalid, so software is used */
  49446. info->pk.ed25519kg.key->devId = INVALID_DEVID;
  49447. ret = wc_ed25519_make_key(info->pk.ed25519kg.rng,
  49448. info->pk.ed25519kg.size, info->pk.ed25519kg.key);
  49449. /* reset devId */
  49450. info->pk.ed25519kg.key->devId = devIdArg;
  49451. }
  49452. #ifdef HAVE_ED25519_SIGN
  49453. else if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  49454. /* set devId to invalid, so software is used */
  49455. info->pk.ed25519sign.key->devId = INVALID_DEVID;
  49456. ret = wc_ed25519_sign_msg_ex(
  49457. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  49458. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  49459. info->pk.ed25519sign.key, info->pk.ed25519sign.type,
  49460. info->pk.ed25519sign.context, info->pk.ed25519sign.contextLen);
  49461. /* reset devId */
  49462. info->pk.ed25519sign.key->devId = devIdArg;
  49463. }
  49464. #endif
  49465. #ifdef HAVE_ED25519_VERIFY
  49466. else if (info->pk.type == WC_PK_TYPE_ED25519_VERIFY) {
  49467. /* set devId to invalid, so software is used */
  49468. info->pk.ed25519verify.key->devId = INVALID_DEVID;
  49469. ret = wc_ed25519_verify_msg_ex(
  49470. info->pk.ed25519verify.sig, info->pk.ed25519verify.sigLen,
  49471. info->pk.ed25519verify.msg, info->pk.ed25519verify.msgLen,
  49472. info->pk.ed25519verify.res, info->pk.ed25519verify.key,
  49473. info->pk.ed25519verify.type, info->pk.ed25519verify.context,
  49474. info->pk.ed25519verify.contextLen);
  49475. /* reset devId */
  49476. info->pk.ed25519verify.key->devId = devIdArg;
  49477. }
  49478. #endif
  49479. #endif /* HAVE_ED25519 */
  49480. }
  49481. else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  49482. #if !defined(NO_AES) || !defined(NO_DES3)
  49483. #ifdef HAVE_AESGCM
  49484. if (info->cipher.type == WC_CIPHER_AES_GCM) {
  49485. if (info->cipher.enc) {
  49486. /* set devId to invalid, so software is used */
  49487. info->cipher.aesgcm_enc.aes->devId = INVALID_DEVID;
  49488. ret = wc_AesGcmEncrypt(
  49489. info->cipher.aesgcm_enc.aes,
  49490. info->cipher.aesgcm_enc.out,
  49491. info->cipher.aesgcm_enc.in,
  49492. info->cipher.aesgcm_enc.sz,
  49493. info->cipher.aesgcm_enc.iv,
  49494. info->cipher.aesgcm_enc.ivSz,
  49495. info->cipher.aesgcm_enc.authTag,
  49496. info->cipher.aesgcm_enc.authTagSz,
  49497. info->cipher.aesgcm_enc.authIn,
  49498. info->cipher.aesgcm_enc.authInSz);
  49499. /* reset devId */
  49500. info->cipher.aesgcm_enc.aes->devId = devIdArg;
  49501. }
  49502. else {
  49503. /* set devId to invalid, so software is used */
  49504. info->cipher.aesgcm_dec.aes->devId = INVALID_DEVID;
  49505. ret = wc_AesGcmDecrypt(
  49506. info->cipher.aesgcm_dec.aes,
  49507. info->cipher.aesgcm_dec.out,
  49508. info->cipher.aesgcm_dec.in,
  49509. info->cipher.aesgcm_dec.sz,
  49510. info->cipher.aesgcm_dec.iv,
  49511. info->cipher.aesgcm_dec.ivSz,
  49512. info->cipher.aesgcm_dec.authTag,
  49513. info->cipher.aesgcm_dec.authTagSz,
  49514. info->cipher.aesgcm_dec.authIn,
  49515. info->cipher.aesgcm_dec.authInSz);
  49516. /* reset devId */
  49517. info->cipher.aesgcm_dec.aes->devId = devIdArg;
  49518. }
  49519. }
  49520. #endif /* HAVE_AESGCM */
  49521. #ifdef HAVE_AES_CBC
  49522. if (info->cipher.type == WC_CIPHER_AES_CBC) {
  49523. if (info->cipher.enc) {
  49524. /* set devId to invalid, so software is used */
  49525. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  49526. ret = wc_AesCbcEncrypt(
  49527. info->cipher.aescbc.aes,
  49528. info->cipher.aescbc.out,
  49529. info->cipher.aescbc.in,
  49530. info->cipher.aescbc.sz);
  49531. /* reset devId */
  49532. info->cipher.aescbc.aes->devId = devIdArg;
  49533. }
  49534. else {
  49535. /* set devId to invalid, so software is used */
  49536. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  49537. ret = wc_AesCbcDecrypt(
  49538. info->cipher.aescbc.aes,
  49539. info->cipher.aescbc.out,
  49540. info->cipher.aescbc.in,
  49541. info->cipher.aescbc.sz);
  49542. /* reset devId */
  49543. info->cipher.aescbc.aes->devId = devIdArg;
  49544. }
  49545. }
  49546. #endif /* HAVE_AES_CBC */
  49547. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  49548. if (info->cipher.type == WC_CIPHER_AES_ECB) {
  49549. if (info->cipher.enc) {
  49550. /* set devId to invalid, so software is used */
  49551. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  49552. ret = wc_AesEcbEncrypt(
  49553. info->cipher.aesecb.aes,
  49554. info->cipher.aesecb.out,
  49555. info->cipher.aesecb.in,
  49556. info->cipher.aesecb.sz);
  49557. /* reset devId */
  49558. info->cipher.aesecb.aes->devId = devIdArg;
  49559. }
  49560. else {
  49561. /* set devId to invalid, so software is used */
  49562. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  49563. ret = wc_AesEcbDecrypt(
  49564. info->cipher.aesecb.aes,
  49565. info->cipher.aesecb.out,
  49566. info->cipher.aesecb.in,
  49567. info->cipher.aesecb.sz);
  49568. /* reset devId */
  49569. info->cipher.aesecb.aes->devId = devIdArg;
  49570. }
  49571. }
  49572. #endif /* HAVE_AES_ECB */
  49573. #if defined(WOLFSSL_AES_COUNTER) && !defined(HAVE_FIPS) && \
  49574. !defined(HAVE_SELFTEST)
  49575. if (info->cipher.type == WC_CIPHER_AES_CTR) {
  49576. /* set devId to invalid, so software is used */
  49577. info->cipher.aesctr.aes->devId = INVALID_DEVID;
  49578. ret = wc_AesCtrEncrypt(
  49579. info->cipher.aesctr.aes,
  49580. info->cipher.aesctr.out,
  49581. info->cipher.aesctr.in,
  49582. info->cipher.aesctr.sz);
  49583. /* reset devId */
  49584. info->cipher.aesctr.aes->devId = devIdArg;
  49585. }
  49586. #endif /* WOLFSSL_AES_COUNTER */
  49587. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  49588. if (info->cipher.type == WC_CIPHER_AES_CCM) {
  49589. if (info->cipher.enc) {
  49590. /* set devId to invalid, so software is used */
  49591. info->cipher.aesccm_enc.aes->devId = INVALID_DEVID;
  49592. ret = wc_AesCcmEncrypt(
  49593. info->cipher.aesccm_enc.aes,
  49594. info->cipher.aesccm_enc.out,
  49595. info->cipher.aesccm_enc.in,
  49596. info->cipher.aesccm_enc.sz,
  49597. info->cipher.aesccm_enc.nonce,
  49598. info->cipher.aesccm_enc.nonceSz,
  49599. info->cipher.aesccm_enc.authTag,
  49600. info->cipher.aesccm_enc.authTagSz,
  49601. info->cipher.aesccm_enc.authIn,
  49602. info->cipher.aesccm_enc.authInSz);
  49603. /* reset devId */
  49604. info->cipher.aesccm_enc.aes->devId = devIdArg;
  49605. }
  49606. else {
  49607. /* set devId to invalid, so software is used */
  49608. info->cipher.aesccm_dec.aes->devId = INVALID_DEVID;
  49609. ret = wc_AesCcmDecrypt(
  49610. info->cipher.aesccm_dec.aes,
  49611. info->cipher.aesccm_dec.out,
  49612. info->cipher.aesccm_dec.in,
  49613. info->cipher.aesccm_dec.sz,
  49614. info->cipher.aesccm_dec.nonce,
  49615. info->cipher.aesccm_dec.nonceSz,
  49616. info->cipher.aesccm_dec.authTag,
  49617. info->cipher.aesccm_dec.authTagSz,
  49618. info->cipher.aesccm_dec.authIn,
  49619. info->cipher.aesccm_dec.authInSz);
  49620. /* reset devId */
  49621. info->cipher.aesccm_dec.aes->devId = devIdArg;
  49622. }
  49623. }
  49624. #endif
  49625. #ifndef NO_DES3
  49626. if (info->cipher.type == WC_CIPHER_DES3) {
  49627. if (info->cipher.enc) {
  49628. /* set devId to invalid, so software is used */
  49629. info->cipher.des3.des->devId = INVALID_DEVID;
  49630. ret = wc_Des3_CbcEncrypt(
  49631. info->cipher.des3.des,
  49632. info->cipher.des3.out,
  49633. info->cipher.des3.in,
  49634. info->cipher.des3.sz);
  49635. /* reset devId */
  49636. info->cipher.des3.des->devId = devIdArg;
  49637. }
  49638. else {
  49639. /* set devId to invalid, so software is used */
  49640. info->cipher.des3.des->devId = INVALID_DEVID;
  49641. ret = wc_Des3_CbcDecrypt(
  49642. info->cipher.des3.des,
  49643. info->cipher.des3.out,
  49644. info->cipher.des3.in,
  49645. info->cipher.des3.sz);
  49646. /* reset devId */
  49647. info->cipher.des3.des->devId = devIdArg;
  49648. }
  49649. }
  49650. #endif /* !NO_DES3 */
  49651. #endif /* !NO_AES || !NO_DES3 */
  49652. }
  49653. #if !defined(NO_SHA) || !defined(NO_SHA256) || \
  49654. defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
  49655. else if (info->algo_type == WC_ALGO_TYPE_HASH) {
  49656. #if !defined(NO_SHA)
  49657. if (info->hash.type == WC_HASH_TYPE_SHA) {
  49658. if (info->hash.sha1 == NULL)
  49659. return NOT_COMPILED_IN;
  49660. /* set devId to invalid, so software is used */
  49661. info->hash.sha1->devId = INVALID_DEVID;
  49662. if (info->hash.in != NULL) {
  49663. ret = wc_ShaUpdate(
  49664. info->hash.sha1,
  49665. info->hash.in,
  49666. info->hash.inSz);
  49667. }
  49668. if (info->hash.digest != NULL) {
  49669. ret = wc_ShaFinal(
  49670. info->hash.sha1,
  49671. info->hash.digest);
  49672. }
  49673. /* reset devId */
  49674. info->hash.sha1->devId = devIdArg;
  49675. }
  49676. else
  49677. #endif
  49678. #if !defined(NO_SHA256)
  49679. if (info->hash.type == WC_HASH_TYPE_SHA256) {
  49680. if (info->hash.sha256 == NULL)
  49681. return NOT_COMPILED_IN;
  49682. /* set devId to invalid, so software is used */
  49683. info->hash.sha256->devId = INVALID_DEVID;
  49684. if (info->hash.in != NULL) {
  49685. ret = wc_Sha256Update(
  49686. info->hash.sha256,
  49687. info->hash.in,
  49688. info->hash.inSz);
  49689. }
  49690. if (info->hash.digest != NULL) {
  49691. ret = wc_Sha256Final(
  49692. info->hash.sha256,
  49693. info->hash.digest);
  49694. }
  49695. /* reset devId */
  49696. info->hash.sha256->devId = devIdArg;
  49697. }
  49698. else
  49699. #endif
  49700. #ifdef WOLFSSL_SHA384
  49701. if (info->hash.type == WC_HASH_TYPE_SHA384) {
  49702. if (info->hash.sha384 == NULL)
  49703. return NOT_COMPILED_IN;
  49704. #ifndef NO_SHA2_CRYPTO_CB
  49705. /* set devId to invalid, so software is used */
  49706. info->hash.sha384->devId = INVALID_DEVID;
  49707. #endif
  49708. if (info->hash.in != NULL) {
  49709. ret = wc_Sha384Update(
  49710. info->hash.sha384,
  49711. info->hash.in,
  49712. info->hash.inSz);
  49713. }
  49714. if (info->hash.digest != NULL) {
  49715. ret = wc_Sha384Final(
  49716. info->hash.sha384,
  49717. info->hash.digest);
  49718. }
  49719. #ifndef NO_SHA2_CRYPTO_CB
  49720. /* reset devId */
  49721. info->hash.sha384->devId = devIdArg;
  49722. #endif
  49723. }
  49724. else
  49725. #endif
  49726. #ifdef WOLFSSL_SHA512
  49727. if (info->hash.type == WC_HASH_TYPE_SHA512) {
  49728. if (info->hash.sha512 == NULL)
  49729. return NOT_COMPILED_IN;
  49730. #ifndef NO_SHA2_CRYPTO_CB
  49731. /* set devId to invalid, so software is used */
  49732. info->hash.sha512->devId = INVALID_DEVID;
  49733. #endif
  49734. if (info->hash.in != NULL) {
  49735. ret = wc_Sha512Update(
  49736. info->hash.sha512,
  49737. info->hash.in,
  49738. info->hash.inSz);
  49739. }
  49740. if (info->hash.digest != NULL) {
  49741. ret = wc_Sha512Final(
  49742. info->hash.sha512,
  49743. info->hash.digest);
  49744. }
  49745. #ifndef NO_SHA2_CRYPTO_CB
  49746. /* reset devId */
  49747. info->hash.sha512->devId = devIdArg;
  49748. #endif
  49749. }
  49750. else
  49751. #endif
  49752. #if defined(WOLFSSL_SHA3) && (!defined(HAVE_FIPS) || FIPS_VERSION_GE(6, 0))
  49753. if (info->hash.type == WC_HASH_TYPE_SHA3_224) {
  49754. if (info->hash.sha3 == NULL)
  49755. return NOT_COMPILED_IN;
  49756. /* set devId to invalid, so software is used */
  49757. info->hash.sha3->devId = INVALID_DEVID;
  49758. if (info->hash.in != NULL) {
  49759. ret = wc_Sha3_224_Update(
  49760. info->hash.sha3,
  49761. info->hash.in,
  49762. info->hash.inSz);
  49763. }
  49764. if (info->hash.digest != NULL) {
  49765. ret = wc_Sha3_224_Final(
  49766. info->hash.sha3,
  49767. info->hash.digest);
  49768. }
  49769. /* reset devId */
  49770. info->hash.sha3->devId = devIdArg;
  49771. }
  49772. else if (info->hash.type == WC_HASH_TYPE_SHA3_256) {
  49773. if (info->hash.sha3 == NULL)
  49774. return NOT_COMPILED_IN;
  49775. /* set devId to invalid, so software is used */
  49776. info->hash.sha3->devId = INVALID_DEVID;
  49777. if (info->hash.in != NULL) {
  49778. ret = wc_Sha3_256_Update(
  49779. info->hash.sha3,
  49780. info->hash.in,
  49781. info->hash.inSz);
  49782. }
  49783. if (info->hash.digest != NULL) {
  49784. ret = wc_Sha3_256_Final(
  49785. info->hash.sha3,
  49786. info->hash.digest);
  49787. }
  49788. /* reset devId */
  49789. info->hash.sha3->devId = devIdArg;
  49790. }
  49791. else if (info->hash.type == WC_HASH_TYPE_SHA3_384) {
  49792. if (info->hash.sha3 == NULL)
  49793. return NOT_COMPILED_IN;
  49794. /* set devId to invalid, so software is used */
  49795. info->hash.sha3->devId = INVALID_DEVID;
  49796. if (info->hash.in != NULL) {
  49797. ret = wc_Sha3_384_Update(
  49798. info->hash.sha3,
  49799. info->hash.in,
  49800. info->hash.inSz);
  49801. }
  49802. if (info->hash.digest != NULL) {
  49803. ret = wc_Sha3_384_Final(
  49804. info->hash.sha3,
  49805. info->hash.digest);
  49806. }
  49807. /* reset devId */
  49808. info->hash.sha3->devId = devIdArg;
  49809. }
  49810. else if (info->hash.type == WC_HASH_TYPE_SHA3_512) {
  49811. if (info->hash.sha3 == NULL)
  49812. return NOT_COMPILED_IN;
  49813. /* set devId to invalid, so software is used */
  49814. info->hash.sha3->devId = INVALID_DEVID;
  49815. if (info->hash.in != NULL) {
  49816. ret = wc_Sha3_512_Update(
  49817. info->hash.sha3,
  49818. info->hash.in,
  49819. info->hash.inSz);
  49820. }
  49821. if (info->hash.digest != NULL) {
  49822. ret = wc_Sha3_512_Final(
  49823. info->hash.sha3,
  49824. info->hash.digest);
  49825. }
  49826. /* reset devId */
  49827. info->hash.sha3->devId = devIdArg;
  49828. }
  49829. else
  49830. #endif
  49831. {
  49832. }
  49833. }
  49834. #endif /* !NO_SHA || !NO_SHA256 */
  49835. #ifndef NO_HMAC
  49836. else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
  49837. if (info->hmac.hmac == NULL)
  49838. return NOT_COMPILED_IN;
  49839. /* set devId to invalid, so software is used */
  49840. info->hmac.hmac->devId = INVALID_DEVID;
  49841. if (info->hmac.in != NULL) {
  49842. ret = wc_HmacUpdate(
  49843. info->hmac.hmac,
  49844. info->hmac.in,
  49845. info->hmac.inSz);
  49846. }
  49847. else if (info->hmac.digest != NULL) {
  49848. ret = wc_HmacFinal(
  49849. info->hmac.hmac,
  49850. info->hmac.digest);
  49851. }
  49852. /* reset devId */
  49853. info->hmac.hmac->devId = devIdArg;
  49854. }
  49855. #endif
  49856. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  49857. else if (info->algo_type == WC_ALGO_TYPE_CMAC) {
  49858. if (info->cmac.cmac == NULL) {
  49859. return NOT_COMPILED_IN;
  49860. }
  49861. /* set devId to invalid so software is used */
  49862. info->cmac.cmac->devId = INVALID_DEVID;
  49863. /* Handle one-shot cases */
  49864. if (info->cmac.key != NULL && info->cmac.in != NULL
  49865. && info->cmac.out != NULL) {
  49866. ret = wc_AesCmacGenerate(info->cmac.out,
  49867. info->cmac.outSz,
  49868. info->cmac.in,
  49869. info->cmac.inSz,
  49870. info->cmac.key,
  49871. info->cmac.keySz);
  49872. /* Sequentially handle incremental cases */
  49873. } else {
  49874. if (info->cmac.key != NULL) {
  49875. ret = wc_InitCmac(info->cmac.cmac,
  49876. info->cmac.key,
  49877. info->cmac.keySz,
  49878. info->cmac.type,
  49879. NULL);
  49880. }
  49881. if ((ret == 0) && (info->cmac.in != NULL)) {
  49882. ret = wc_CmacUpdate(info->cmac.cmac,
  49883. info->cmac.in,
  49884. info->cmac.inSz);
  49885. }
  49886. if ((ret ==0) && (info->cmac.out != NULL)) {
  49887. ret = wc_CmacFinal(info->cmac.cmac,
  49888. info->cmac.out,
  49889. info->cmac.outSz);
  49890. }
  49891. }
  49892. /* reset devId */
  49893. info->cmac.cmac->devId = devIdArg;
  49894. }
  49895. #endif /* WOLFSSL_CMAC && !(NO_AES) && WOLFSSL_AES_DIRECT */
  49896. (void)devIdArg;
  49897. (void)myCtx;
  49898. return ret;
  49899. }
  49900. #ifdef WOLF_CRYPTO_CB_FIND
  49901. static int myCryptoCbFind(int currentId, int algoType)
  49902. {
  49903. /* can have algo specific overrides here
  49904. switch (algoType) {
  49905. i.e.
  49906. WC_ALGO_TYPE_CMAC
  49907. WC_ALGO_TYPE_SEED
  49908. WC_ALGO_TYPE_HMAC
  49909. WC_ALGO_TYPE_HASH
  49910. WC_ALGO_TYPE_CIPHER
  49911. WC_ALGO_TYPE_PK
  49912. }
  49913. */
  49914. (void)algoType;
  49915. if (currentId == INVALID_DEVID) {
  49916. /* can override invalid devid found with 1 */
  49917. }
  49918. return currentId;
  49919. }
  49920. #endif /* WOLF_CRYPTO_CB_FIND */
  49921. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cryptocb_test(void)
  49922. {
  49923. wc_test_ret_t ret = 0;
  49924. int origDevId = devId;
  49925. myCryptoDevCtx myCtx;
  49926. WOLFSSL_ENTER("cryptocb_test");
  49927. /* example data for callback */
  49928. myCtx.exampleVar = 1;
  49929. /* set devId to something other than INVALID_DEVID */
  49930. devId = 1;
  49931. ret = wc_CryptoCb_RegisterDevice(devId, myCryptoDevCb, &myCtx);
  49932. #ifdef WOLF_CRYPTO_CB_FIND
  49933. wc_CryptoCb_SetDeviceFindCb(myCryptoCbFind);
  49934. #endif /* WOLF_CRYPTO_CB_FIND */
  49935. #ifndef WC_NO_RNG
  49936. if (ret == 0)
  49937. ret = random_test();
  49938. #endif /* WC_NO_RNG */
  49939. #if !defined(NO_RSA)
  49940. PRIVATE_KEY_UNLOCK();
  49941. if (ret == 0)
  49942. ret = rsa_test();
  49943. PRIVATE_KEY_LOCK();
  49944. #endif
  49945. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  49946. PRIVATE_KEY_UNLOCK();
  49947. if (ret == 0)
  49948. ret = rsa_onlycb_test(&myCtx);
  49949. PRIVATE_KEY_LOCK();
  49950. #endif
  49951. #if defined(HAVE_ECC)
  49952. PRIVATE_KEY_UNLOCK();
  49953. if (ret == 0)
  49954. ret = ecc_test();
  49955. PRIVATE_KEY_LOCK();
  49956. #endif
  49957. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  49958. PRIVATE_KEY_UNLOCK();
  49959. if (ret == 0)
  49960. ret = ecc_onlycb_test(&myCtx);
  49961. PRIVATE_KEY_LOCK();
  49962. #endif
  49963. #ifdef HAVE_ED25519
  49964. PRIVATE_KEY_UNLOCK();
  49965. if (ret == 0)
  49966. ret = ed25519_test();
  49967. PRIVATE_KEY_LOCK();
  49968. #endif
  49969. #ifdef HAVE_CURVE25519
  49970. if (ret == 0)
  49971. ret = curve25519_test();
  49972. #endif
  49973. #ifndef NO_AES
  49974. #ifdef HAVE_AESGCM
  49975. if (ret == 0)
  49976. ret = aesgcm_test();
  49977. #endif
  49978. #ifdef HAVE_AES_CBC
  49979. if (ret == 0)
  49980. ret = aes_test();
  49981. #endif
  49982. #ifdef WOLFSSL_AES_XTS
  49983. if (ret == 0)
  49984. ret = aes_xts_test();
  49985. #endif
  49986. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  49987. if (ret == 0)
  49988. ret = aesccm_test();
  49989. #endif
  49990. #endif /* !NO_AES */
  49991. #ifndef NO_DES3
  49992. if (ret == 0)
  49993. ret = des3_test();
  49994. #endif /* !NO_DES3 */
  49995. #ifndef NO_SHA
  49996. if (ret == 0)
  49997. ret = sha_test();
  49998. #endif
  49999. #ifndef NO_SHA256
  50000. if (ret == 0)
  50001. ret = sha256_test();
  50002. #endif
  50003. #ifdef WOLFSSL_SHA384
  50004. if (ret == 0)
  50005. ret = sha384_test();
  50006. #endif
  50007. #ifdef WOLFSSL_SHA512
  50008. if (ret == 0)
  50009. ret = sha512_test();
  50010. #ifdef WOLFSSL_SHA3
  50011. if (ret == 0)
  50012. ret = sha3_test();
  50013. #endif
  50014. #endif
  50015. #ifndef NO_HMAC
  50016. #ifndef NO_SHA
  50017. if (ret == 0)
  50018. ret = hmac_sha_test();
  50019. #endif
  50020. #ifndef NO_SHA256
  50021. if (ret == 0)
  50022. ret = hmac_sha256_test();
  50023. #endif
  50024. #ifdef WOLFSSL_SHA3
  50025. if (ret == 0)
  50026. ret = hmac_sha3_test();
  50027. #endif
  50028. #endif
  50029. #ifndef NO_PWDBASED
  50030. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  50031. PRIVATE_KEY_UNLOCK();
  50032. if (ret == 0)
  50033. ret = pbkdf2_test();
  50034. PRIVATE_KEY_LOCK();
  50035. #endif
  50036. #endif
  50037. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  50038. if (ret == 0)
  50039. ret = cmac_test();
  50040. #endif
  50041. /* restore devId */
  50042. devId = origDevId;
  50043. return ret;
  50044. }
  50045. #endif /* WOLF_CRYPTO_CB */
  50046. #ifdef WOLFSSL_CERT_PIV
  50047. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certpiv_test(void)
  50048. {
  50049. wc_test_ret_t ret;
  50050. wc_CertPIV piv;
  50051. /* Template for Identiv PIV cert, nonce and signature */
  50052. WOLFSSL_SMALL_STACK_STATIC const byte pivCertIdentiv[] = {
  50053. 0x0A, 0x0B,
  50054. 0x53, 0x09, /* NIST PIV Cert */
  50055. 0x70, 0x02, /* Certificate */
  50056. 0x30, 0x00,
  50057. 0x71, 0x01, 0x05, /* Cert Info */
  50058. 0xFE, 0x00, /* Error Detection */
  50059. 0x0B, 0x01, 0x00, /* Nonce */
  50060. 0x0C, 0x01, 0x00, /* Signed Nonce */
  50061. };
  50062. /* PIV certificate data including certificate, info and error detection. */
  50063. WOLFSSL_SMALL_STACK_STATIC const byte pivCert[] = {
  50064. 0x53, 0x09, /* NIST PIV Cert */
  50065. 0x70, 0x02, /* Certificate */
  50066. 0x30, 0x00,
  50067. 0x71, 0x01, 0x04, /* Cert Info */
  50068. 0xFE, 0x00, /* Error Detection */
  50069. };
  50070. WOLFSSL_ENTER("certpiv_test");
  50071. XMEMSET(&piv, 0, sizeof(piv));
  50072. /* Test with Identiv 0x0A, 0x0B and 0x0C markers */
  50073. ret = wc_ParseCertPIV(&piv, pivCertIdentiv, sizeof(pivCertIdentiv));
  50074. if (ret != 0) {
  50075. return WC_TEST_RET_ENC_EC(ret);
  50076. }
  50077. if (!piv.isIdentiv) {
  50078. return WC_TEST_RET_ENC_NC;
  50079. }
  50080. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  50081. return WC_TEST_RET_ENC_NC;
  50082. }
  50083. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  50084. return WC_TEST_RET_ENC_NC;
  50085. }
  50086. if ((piv.compression != ASN_PIV_CERT_INFO_GZIP)) {
  50087. return WC_TEST_RET_ENC_NC;
  50088. }
  50089. if (!piv.isX509) {
  50090. return WC_TEST_RET_ENC_NC;
  50091. }
  50092. if ((piv.nonce == NULL) || (piv.nonceSz != 1)) {
  50093. return WC_TEST_RET_ENC_NC;
  50094. }
  50095. if ((piv.signedNonce == NULL) || (piv.signedNonceSz != 1)) {
  50096. return WC_TEST_RET_ENC_NC;
  50097. }
  50098. XMEMSET(&piv, 0, sizeof(piv));
  50099. /* Test with NIST PIV format */
  50100. ret = wc_ParseCertPIV(&piv, pivCert, sizeof(pivCert));
  50101. if (ret != 0) {
  50102. return WC_TEST_RET_ENC_EC(ret);
  50103. }
  50104. if (piv.isIdentiv) {
  50105. return WC_TEST_RET_ENC_NC;
  50106. }
  50107. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  50108. return WC_TEST_RET_ENC_NC;
  50109. }
  50110. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  50111. return WC_TEST_RET_ENC_NC;
  50112. }
  50113. if ((piv.compression != 0)) {
  50114. return WC_TEST_RET_ENC_NC;
  50115. }
  50116. if (!piv.isX509) {
  50117. return WC_TEST_RET_ENC_NC;
  50118. }
  50119. return ret;
  50120. }
  50121. #endif /* WOLFSSL_CERT_PIV */
  50122. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  50123. static time_t time_cb(time_t* t)
  50124. {
  50125. if (t != NULL) {
  50126. *t = 99;
  50127. }
  50128. return 99;
  50129. }
  50130. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t time_test(void)
  50131. {
  50132. time_t t;
  50133. wc_test_ret_t ret;
  50134. WOLFSSL_ENTER("time_test");
  50135. ret = wc_SetTimeCb(time_cb);
  50136. if (ret != 0)
  50137. return WC_TEST_RET_ENC_EC(ret);
  50138. t = wc_Time(NULL);
  50139. if (t != 99)
  50140. return WC_TEST_RET_ENC_NC;
  50141. ret = wc_GetTime(&t, sizeof(time_t));
  50142. if (ret != 0)
  50143. return WC_TEST_RET_ENC_EC(ret);
  50144. if (t != 99)
  50145. return WC_TEST_RET_ENC_NC;
  50146. ret = wc_SetTimeCb(NULL);
  50147. if (ret != 0)
  50148. return WC_TEST_RET_ENC_EC(ret);
  50149. return 0;
  50150. }
  50151. #endif
  50152. #ifdef WOLFSSL_AES_SIV
  50153. typedef struct {
  50154. const byte key[33];
  50155. word32 keySz;
  50156. const byte nonce[49];
  50157. word32 nonceSz;
  50158. const byte assoc[81];
  50159. word32 assocSz;
  50160. const byte plaintext[83];
  50161. word32 plaintextSz;
  50162. const byte siv[AES_BLOCK_SIZE+1];
  50163. const byte ciphertext[82];
  50164. word32 ciphertextSz;
  50165. } AesSivTestVector;
  50166. #define AES_SIV_TEST_VECTORS 7
  50167. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_siv_test(void)
  50168. {
  50169. /* These test vectors come from chrony 4.1's SIV unit tests. */
  50170. WOLFSSL_SMALL_STACK_STATIC const AesSivTestVector testVectors[AES_SIV_TEST_VECTORS] = {
  50171. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  50172. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  50173. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  50174. "", 0,
  50175. "", 0,
  50176. "\x22\x3e\xb5\x94\xe0\xe0\x25\x4b\x00\x25\x8e\x21\x9a\x1c\xa4\x21",
  50177. "", 0
  50178. },
  50179. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  50180. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  50181. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  50182. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  50183. "", 0,
  50184. "\xd7\x20\x19\x89\xc6\xdb\xc6\xd6\x61\xfc\x62\xbc\x86\x5e\xee\xef",
  50185. "", 0
  50186. },
  50187. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  50188. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  50189. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  50190. "", 0,
  50191. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  50192. "\xb6\xc1\x60\xe9\xc2\xfd\x2a\xe8\xde\xc5\x36\x8b\x2a\x33\xed\xe1",
  50193. "\x14\xff\xb3\x97\x34\x5c\xcb\xe4\x4a\xa4\xde\xac\xd9\x36\x90\x46", 16
  50194. },
  50195. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  50196. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  50197. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e", 15,
  50198. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c", 15,
  50199. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4", 15,
  50200. "\x03\x8c\x41\x51\xba\x7a\x8f\x77\x6e\x56\x31\x99\x42\x0b\xc7\x03",
  50201. "\xe7\x6c\x67\xc9\xda\xb7\x0d\x5b\x44\x06\x26\x5a\xd0\xd2\x3b", 15
  50202. },
  50203. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  50204. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  50205. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  50206. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  50207. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7", 16,
  50208. "\x5c\x05\x23\x65\xf4\x57\x0a\xa0\xfb\x38\x3e\xce\x9b\x75\x85\xeb",
  50209. "\x68\x85\x19\x36\x0c\x7c\x48\x11\x40\xcb\x9b\x57\x9a\x0e\x65\x32", 16
  50210. },
  50211. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  50212. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  50213. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
  50214. "\xd5", 17,
  50215. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b"
  50216. "\xa0", 17,
  50217. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7"
  50218. "\x08", 17,
  50219. "\xaf\x58\x4b\xe7\x82\x1e\x96\x19\x29\x91\x25\xe0\xdd\x80\x3b\x49",
  50220. "\xa5\x11\xcd\xb6\x08\xf3\x76\xa0\xb6\xfa\x15\x82\xf3\x95\xe1\xeb"
  50221. "\xbd", 17
  50222. },
  50223. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  50224. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  50225. "\xb0\x5a\x1b\xc7\x56\xe7\xb6\x2c\xb4\x85\xe5\x56\xa5\x28\xc0\x6c"
  50226. "\x2f\x3b\x0b\x9d\x1a\x0c\xdf\x69\x47\xe0\xcc\xc0\x87\xaa\x5c\x09"
  50227. "\x98\x48\x8d\x6a\x8e\x1e\x05\xd7\x8b\x68\x74\x83\xb5\x1d\xf1\x2c", 48,
  50228. "\xe5\x8b\xd2\x6a\x30\xc5\xc5\x61\xcc\xbd\x7c\x27\xbf\xfe\xf9\x06"
  50229. "\x00\x5b\xd7\xfc\x11\x0b\xcf\x16\x61\xef\xac\x05\xa7\xaf\xec\x27"
  50230. "\x41\xc8\x5e\x9e\x0d\xf9\x2f\xaf\x20\x79\x17\xe5\x17\x91\x2a\x27"
  50231. "\x34\x1c\xbc\xaf\xeb\xef\x7f\x52\xe7\x1e\x4c\x2a\xca\xbd\x2b\xbe"
  50232. "\x34\xd6\xfb\x69\xd3\x3e\x49\x59\x60\xb4\x26\xc9\xb8\xce\xba", 79,
  50233. "\x6c\xe7\xcf\x7e\xab\x7b\xa0\xe1\xa7\x22\xcb\x88\xde\x5e\x42\xd2"
  50234. "\xec\x79\xe0\xa2\xcf\x5f\x0f\x6f\x6b\x89\x57\xcd\xae\x17\xd4\xc2"
  50235. "\xf3\x1b\xa2\xa8\x13\x78\x23\x2f\x83\xa8\xd4\x0c\xc0\xd2\xf3\x99"
  50236. "\xae\x81\xa1\xca\x5b\x5f\x45\xa6\x6f\x0c\x8a\xf3\xd4\x67\x40\x81"
  50237. "\x26\xe2\x01\x86\xe8\x5a\xd5\xf8\x58\x80\x9f\x56\xaa\x76\x96\xbf"
  50238. "\x31", 81,
  50239. "\x9a\x06\x33\xe0\xee\x00\x6a\x9b\xc8\x20\xd5\xe2\xc2\xed\xb5\x75",
  50240. "\xfa\x9e\x42\x2a\x31\x6b\xda\xca\xaa\x7d\x31\x8b\x84\x7a\xb8\xd7"
  50241. "\x8a\x81\x25\x64\xed\x41\x9b\xa9\x77\x10\xbd\x05\x0c\x4e\xc5\x31"
  50242. "\x0c\xa2\x86\xec\x8a\x94\xc8\x24\x23\x3c\x13\xee\xa5\x51\xc9\xdf"
  50243. "\x48\xc9\x55\xc5\x2f\x40\x73\x3f\x98\xbb\x8d\x69\x78\x46\x64\x17"
  50244. "\x8d\x49\x2f\x14\x62\xa4\x7c\x2a\x57\x38\x87\xce\xc6\x72\xd3\x5c"
  50245. "\xa1", 81
  50246. }};
  50247. int i;
  50248. byte computedCiphertext[82];
  50249. byte computedPlaintext[82];
  50250. byte siv[AES_BLOCK_SIZE];
  50251. wc_test_ret_t ret = 0;
  50252. WOLFSSL_ENTER("aes_siv_test");
  50253. for (i = 0; i < AES_SIV_TEST_VECTORS; ++i) {
  50254. ret = wc_AesSivEncrypt(testVectors[i].key, testVectors[i].keySz,
  50255. testVectors[i].assoc, testVectors[i].assocSz,
  50256. testVectors[i].nonce, testVectors[i].nonceSz,
  50257. testVectors[i].plaintext,
  50258. testVectors[i].plaintextSz, siv,
  50259. computedCiphertext);
  50260. if (ret != 0) {
  50261. return WC_TEST_RET_ENC_EC(ret);
  50262. }
  50263. ret = XMEMCMP(siv, testVectors[i].siv, AES_BLOCK_SIZE);
  50264. if (ret != 0) {
  50265. return WC_TEST_RET_ENC_NC;
  50266. }
  50267. ret = XMEMCMP(computedCiphertext, testVectors[i].ciphertext,
  50268. testVectors[i].ciphertextSz);
  50269. if (ret != 0) {
  50270. return WC_TEST_RET_ENC_NC;
  50271. }
  50272. ret = wc_AesSivDecrypt(testVectors[i].key, testVectors[i].keySz,
  50273. testVectors[i].assoc, testVectors[i].assocSz,
  50274. testVectors[i].nonce, testVectors[i].nonceSz,
  50275. computedCiphertext, testVectors[i].plaintextSz,
  50276. siv, computedPlaintext);
  50277. if (ret != 0) {
  50278. return WC_TEST_RET_ENC_EC(ret);
  50279. }
  50280. ret = XMEMCMP(computedPlaintext, testVectors[i].plaintext,
  50281. testVectors[i].plaintextSz);
  50282. if (ret != 0) {
  50283. return WC_TEST_RET_ENC_NC;
  50284. }
  50285. }
  50286. return 0;
  50287. }
  50288. #endif
  50289. #undef ERROR_OUT
  50290. static const int fiducial4 = WC_TEST_RET_LN;
  50291. /* print the fiducial line numbers assigned above, allowing confirmation of
  50292. * source code version match when in doubt.
  50293. */
  50294. static void print_fiducials(void) {
  50295. printf(" [fiducial line numbers: %d %d %d %d]\n",
  50296. fiducial1, fiducial2, fiducial3, fiducial4);
  50297. }
  50298. #else
  50299. #ifndef NO_MAIN_DRIVER
  50300. int main(void) { return 0; }
  50301. #endif
  50302. #endif /* NO_CRYPT_TEST */